From c7697606e149d816963471a4e925e544e7c5e8b5 Mon Sep 17 00:00:00 2001 From: David Mak Date: Thu, 3 Oct 2024 14:21:11 +0800 Subject: [PATCH] [core] Add TopLevelDef::Variable --- nac3artiq/src/lib.rs | 5 +++++ nac3core/src/codegen/expr.rs | 1 + nac3core/src/toplevel/composer.rs | 3 ++- nac3core/src/toplevel/helper.rs | 15 +++++++++++++++ nac3core/src/toplevel/mod.rs | 21 ++++++++++++++++++++- 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index c5d66a7c..7e3fddb4 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -650,6 +650,11 @@ impl Nac3 { } } } + TopLevelDef::Variable { .. } => { + return Err(CompileError::new_err(String::from( + "Unsupported @rpc annotation on global variable", + ))) + } } } } diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index c478fb3f..f9534fb1 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -977,6 +977,7 @@ pub fn gen_call<'ctx, G: CodeGenerator>( TopLevelDef::Class { .. } => { return Ok(Some(generator.gen_constructor(ctx, fun.0, &def, params)?)) } + TopLevelDef::Variable { .. } => unreachable!(), } } .or_else(|_: String| { diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index d3103894..5906e159 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -101,7 +101,8 @@ impl TopLevelComposer { .iter() .map(|def_ast| match *def_ast.0.read() { TopLevelDef::Class { name, .. } => name.to_string(), - TopLevelDef::Function { simple_name, .. } => simple_name.to_string(), + TopLevelDef::Function { simple_name, .. } + | TopLevelDef::Variable { simple_name, .. } => simple_name.to_string(), }) .collect_vec(); diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 66233ce0..d674c51b 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -391,6 +391,9 @@ impl TopLevelDef { r } ), + TopLevelDef::Variable { name, ty, .. } => { + format!("Variable {{ name: {name:?}, ty: {:?} }}", unifier.stringify(*ty),) + } } } } @@ -592,6 +595,18 @@ impl TopLevelComposer { } } + #[must_use] + pub fn make_top_level_variable_def( + name: String, + simple_name: StrRef, + ty: Type, + ty_decl: Expr, + resolver: Option>, + loc: Option, + ) -> TopLevelDef { + TopLevelDef::Variable { name, simple_name, ty, ty_decl, resolver, loc } + } + #[must_use] pub fn make_class_method_name(mut class_name: String, method_name: &str) -> String { class_name.push('.'); diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index b786aa73..8241c3ac 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -10,7 +10,7 @@ use inkwell::values::BasicValueEnum; use itertools::Itertools; use parking_lot::RwLock; -use nac3parser::ast::{self, Location, Stmt, StrRef}; +use nac3parser::ast::{self, Expr, Location, Stmt, StrRef}; use crate::{ codegen::{CodeGenContext, CodeGenerator}, @@ -148,6 +148,25 @@ pub enum TopLevelDef { /// Definition location. loc: Option, }, + Variable { + /// Qualified name of the global variable, should be unique globally. + name: String, + + /// Simple name, the same as in method/function definition. + simple_name: StrRef, + + /// Type of the global variable. + ty: Type, + + /// The declared type of the global variable. + ty_decl: Expr, + + /// Symbol resolver of the module defined the class. + resolver: Option>, + + /// Definition location. + loc: Option, + }, } pub struct TopLevelContext {