Compare commits
No commits in common. "5839badadd732262dc24af2971a94dde60f7103e" and "2bb788e4bbfb77fd133c624a40465638b33ff957" have entirely different histories.
5839badadd
...
2bb788e4bb
|
@ -1,6 +1,6 @@
|
|||
use std::rc::Rc;
|
||||
|
||||
use nac3parser::ast::{fold::Fold, ExprKind, Ident};
|
||||
use nac3parser::ast::{fold::Fold, ExprKind};
|
||||
|
||||
use super::*;
|
||||
use crate::{
|
||||
|
@ -382,66 +382,32 @@ impl TopLevelComposer {
|
|||
))
|
||||
}
|
||||
|
||||
ast::StmtKind::Assign { .. } => {
|
||||
// Assignment statements can assign to (and therefore create) more than one
|
||||
// variable, but this function only allows returning one set of symbol information.
|
||||
// We want to avoid changing this to return a `Vec` of symbol info, as this would
|
||||
// require `iter().next().unwrap()` on every variable created from a non-Assign
|
||||
// statement.
|
||||
//
|
||||
// Make callers use `register_top_level_var` instead, as it provides more
|
||||
// fine-grained control over which symbols to register, while also simplifying the
|
||||
// usage of this function.
|
||||
panic!("Registration of top-level Assign statements must use TopLevelComposer::register_top_level_var (at {})", ast.location);
|
||||
}
|
||||
|
||||
ast::StmtKind::AnnAssign { target, annotation, .. } => {
|
||||
let ExprKind::Name { id: name, .. } = target.node else {
|
||||
return Err(format!(
|
||||
"global variable declaration must be an identifier (at {})",
|
||||
target.location
|
||||
ast.location
|
||||
));
|
||||
};
|
||||
|
||||
self.register_top_level_var(
|
||||
name,
|
||||
Some(annotation.as_ref().clone()),
|
||||
resolver,
|
||||
mod_path,
|
||||
target.location,
|
||||
)
|
||||
}
|
||||
|
||||
_ => Err(format!(
|
||||
"registrations of constructs other than top level classes/functions/variables are not supported (at {})",
|
||||
ast.location
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Registers a top-level variable with the given `name` into the composer.
|
||||
///
|
||||
/// `annotation` - The type annotation of the top-level variable, or [`None`] if no type
|
||||
/// annotation is provided.
|
||||
/// `location` - The location of the top-level variable.
|
||||
pub fn register_top_level_var(
|
||||
&mut self,
|
||||
name: Ident,
|
||||
annotation: Option<Expr>,
|
||||
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
||||
mod_path: &str,
|
||||
location: Location,
|
||||
) -> Result<(StrRef, DefinitionId, Option<Type>), String> {
|
||||
if self.keyword_list.contains(&name) {
|
||||
return Err(format!("cannot use keyword `{name}` as a class name (at {location})"));
|
||||
return Err(format!(
|
||||
"cannot use keyword `{}` as a class name (at {})",
|
||||
name,
|
||||
ast.location
|
||||
));
|
||||
}
|
||||
|
||||
let global_var_name =
|
||||
if mod_path.is_empty() { name.to_string() } else { format!("{mod_path}.{name}") };
|
||||
|
||||
if !self.defined_names.insert(global_var_name.clone()) {
|
||||
let global_var_name = if mod_path.is_empty() {
|
||||
name.to_string()
|
||||
} else {
|
||||
format!("{mod_path}.{name}")
|
||||
};
|
||||
if !defined_names.insert(global_var_name.clone()) {
|
||||
return Err(format!(
|
||||
"global variable `{global_var_name}` defined twice (at {location})"
|
||||
"global variable `{}` defined twice (at {})",
|
||||
global_var_name,
|
||||
ast.location
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -452,15 +418,25 @@ impl TopLevelComposer {
|
|||
name,
|
||||
// dummy here, unify with correct type later,
|
||||
ty_to_be_unified,
|
||||
annotation,
|
||||
*(annotation.clone()),
|
||||
resolver,
|
||||
Some(location),
|
||||
))
|
||||
.into(),
|
||||
Some(ast.location),
|
||||
)).into(),
|
||||
None,
|
||||
));
|
||||
|
||||
Ok((name, DefinitionId(self.definition_ast_list.len() - 1), Some(ty_to_be_unified)))
|
||||
Ok((
|
||||
name,
|
||||
DefinitionId(self.definition_ast_list.len() - 1),
|
||||
Some(ty_to_be_unified),
|
||||
))
|
||||
}
|
||||
|
||||
_ => Err(format!(
|
||||
"registrations of constructs other than top level classes/functions/variables are not supported (at {})",
|
||||
ast.location
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_analysis(&mut self, inference: bool) -> Result<(), HashSet<String>> {
|
||||
|
@ -2273,8 +2249,9 @@ impl TopLevelComposer {
|
|||
let primitives_store = &self.primitives_ty;
|
||||
|
||||
let mut analyze = |variable_def: &Arc<RwLock<TopLevelDef>>| -> Result<_, HashSet<String>> {
|
||||
let TopLevelDef::Variable { ty: dummy_ty, ty_decl, resolver, loc, .. } =
|
||||
&*variable_def.read()
|
||||
let variable_def = &mut *variable_def.write();
|
||||
|
||||
let TopLevelDef::Variable { ty: dummy_ty, ty_decl, resolver, loc, .. } = variable_def
|
||||
else {
|
||||
// not top level variable def, skip
|
||||
return Ok(());
|
||||
|
@ -2282,7 +2259,6 @@ impl TopLevelComposer {
|
|||
|
||||
let resolver = &**resolver.as_ref().unwrap();
|
||||
|
||||
if let Some(ty_decl) = ty_decl {
|
||||
let ty_annotation = parse_ast_to_type_annotation_kinds(
|
||||
resolver,
|
||||
&temp_def_list,
|
||||
|
@ -2302,8 +2278,6 @@ impl TopLevelComposer {
|
|||
unifier.unify(*dummy_ty, ty_from_ty_annotation).map_err(|e| {
|
||||
HashSet::from([e.at(Some(loc.unwrap())).to_display(unifier).to_string()])
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
|
|
|
@ -600,7 +600,7 @@ impl TopLevelComposer {
|
|||
name: String,
|
||||
simple_name: StrRef,
|
||||
ty: Type,
|
||||
ty_decl: Option<Expr>,
|
||||
ty_decl: Expr,
|
||||
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
||||
loc: Option<Location>,
|
||||
) -> TopLevelDef {
|
||||
|
|
|
@ -158,8 +158,8 @@ pub enum TopLevelDef {
|
|||
/// Type of the global variable.
|
||||
ty: Type,
|
||||
|
||||
/// The declared type of the global variable, or [`None`] if no type annotation is provided.
|
||||
ty_decl: Option<Expr>,
|
||||
/// The declared type of the global variable.
|
||||
ty_decl: Expr,
|
||||
|
||||
/// Symbol resolver of the module defined the class.
|
||||
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
|
||||
|
|
|
@ -36,7 +36,7 @@ impl<'a> Inferencer<'a> {
|
|||
ExprKind::Name { id, .. } => {
|
||||
// If `id` refers to a declared symbol, reject this assignment if it is used in the
|
||||
// context of an (implicit) global variable
|
||||
if let Some(id_info) = defined_identifiers.get(id) {
|
||||
if let Some(id_info) = self.defined_identifiers.get(id) {
|
||||
if matches!(
|
||||
id_info.source,
|
||||
DeclarationSource::Global { is_explicit: Some(false) }
|
||||
|
|
|
@ -7,7 +7,7 @@ def output_int64(x: int64):
|
|||
...
|
||||
|
||||
X: int32 = 0
|
||||
Y = int64(1)
|
||||
Y: int64 = int64(1)
|
||||
|
||||
def f():
|
||||
global X, Y
|
||||
|
|
|
@ -174,49 +174,46 @@ fn handle_typevar_definition(
|
|||
fn handle_assignment_pattern(
|
||||
targets: &[Expr],
|
||||
value: &Expr,
|
||||
resolver: Arc<dyn SymbolResolver + Send + Sync>,
|
||||
resolver: &(dyn SymbolResolver + Send + Sync),
|
||||
internal_resolver: &ResolverInternal,
|
||||
composer: &mut TopLevelComposer,
|
||||
def_list: &[Arc<RwLock<TopLevelDef>>],
|
||||
unifier: &mut Unifier,
|
||||
primitives: &PrimitiveStore,
|
||||
) -> Result<(), String> {
|
||||
if targets.len() == 1 {
|
||||
let target = &targets[0];
|
||||
|
||||
match &target.node {
|
||||
match &targets[0].node {
|
||||
ExprKind::Name { id, .. } => {
|
||||
let def_list = composer.extract_def_list();
|
||||
let unifier = &mut composer.unifier;
|
||||
let primitives = &composer.primitives_ty;
|
||||
|
||||
if let Ok(var) =
|
||||
handle_typevar_definition(value, &*resolver, &def_list, unifier, primitives)
|
||||
handle_typevar_definition(value, resolver, def_list, unifier, primitives)
|
||||
{
|
||||
internal_resolver.add_id_type(*id, var);
|
||||
Ok(())
|
||||
} else if let Ok(val) = parse_parameter_default_value(value, &*resolver) {
|
||||
} else if let Ok(val) = parse_parameter_default_value(value, resolver) {
|
||||
internal_resolver.add_module_global(*id, val);
|
||||
let (name, def_id, _) = composer
|
||||
.register_top_level_var(
|
||||
*id,
|
||||
None,
|
||||
Some(resolver.clone()),
|
||||
"__main__",
|
||||
target.location,
|
||||
)
|
||||
.unwrap();
|
||||
internal_resolver.add_id_def(name, def_id);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("fails to evaluate this expression `{:?}` as a constant or generic parameter at {}",
|
||||
target.node,
|
||||
target.location,
|
||||
targets[0].node,
|
||||
targets[0].location,
|
||||
))
|
||||
}
|
||||
}
|
||||
ExprKind::List { elts, .. } | ExprKind::Tuple { elts, .. } => {
|
||||
handle_assignment_pattern(elts, value, resolver, internal_resolver, composer)?;
|
||||
handle_assignment_pattern(
|
||||
elts,
|
||||
value,
|
||||
resolver,
|
||||
internal_resolver,
|
||||
def_list,
|
||||
unifier,
|
||||
primitives,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(format!("assignment to {target:?} is not supported at {}", target.location)),
|
||||
_ => Err(format!(
|
||||
"assignment to {:?} is not supported at {}",
|
||||
targets[0], targets[0].location
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
match &value.node {
|
||||
|
@ -226,9 +223,11 @@ fn handle_assignment_pattern(
|
|||
handle_assignment_pattern(
|
||||
std::slice::from_ref(tar),
|
||||
val,
|
||||
resolver.clone(),
|
||||
resolver,
|
||||
internal_resolver,
|
||||
composer,
|
||||
def_list,
|
||||
unifier,
|
||||
primitives,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -249,9 +248,8 @@ fn handle_assignment_pattern(
|
|||
fn handle_global_var(
|
||||
target: &Expr,
|
||||
value: Option<&Expr>,
|
||||
resolver: &Arc<dyn SymbolResolver + Send + Sync>,
|
||||
resolver: &(dyn SymbolResolver + Send + Sync),
|
||||
internal_resolver: &ResolverInternal,
|
||||
composer: &mut TopLevelComposer,
|
||||
) -> Result<(), String> {
|
||||
let ExprKind::Name { id, .. } = target.node else {
|
||||
return Err(format!(
|
||||
|
@ -264,12 +262,8 @@ fn handle_global_var(
|
|||
return Err(format!("global variable `{id}` must be initialized in its definition"));
|
||||
};
|
||||
|
||||
if let Ok(val) = parse_parameter_default_value(value, &**resolver) {
|
||||
if let Ok(val) = parse_parameter_default_value(value, resolver) {
|
||||
internal_resolver.add_module_global(id, val);
|
||||
let (name, def_id, _) = composer
|
||||
.register_top_level_var(id, None, Some(resolver.clone()), "__main__", target.location)
|
||||
.unwrap();
|
||||
internal_resolver.add_id_def(name, def_id);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
|
@ -361,12 +355,17 @@ fn main() {
|
|||
for stmt in parser_result {
|
||||
match &stmt.node {
|
||||
StmtKind::Assign { targets, value, .. } => {
|
||||
let def_list = composer.extract_def_list();
|
||||
let unifier = &mut composer.unifier;
|
||||
let primitives = &composer.primitives_ty;
|
||||
if let Err(err) = handle_assignment_pattern(
|
||||
targets,
|
||||
value,
|
||||
resolver.clone(),
|
||||
resolver.as_ref(),
|
||||
internal_resolver.as_ref(),
|
||||
&mut composer,
|
||||
&def_list,
|
||||
unifier,
|
||||
primitives,
|
||||
) {
|
||||
panic!("{err}");
|
||||
}
|
||||
|
@ -376,12 +375,16 @@ fn main() {
|
|||
if let Err(err) = handle_global_var(
|
||||
target,
|
||||
value.as_ref().map(Box::as_ref),
|
||||
&resolver,
|
||||
resolver.as_ref(),
|
||||
internal_resolver.as_ref(),
|
||||
&mut composer,
|
||||
) {
|
||||
panic!("{err}");
|
||||
}
|
||||
|
||||
let (name, def_id, _) = composer
|
||||
.register_top_level(stmt, Some(resolver.clone()), "__main__", true)
|
||||
.unwrap();
|
||||
internal_resolver.add_id_def(name, def_id);
|
||||
}
|
||||
|
||||
// allow (and ignore) "from __future__ import annotations"
|
||||
|
|
Loading…
Reference in New Issue