From 91625dd327d9feb7a6744451a039811bff3f74cb Mon Sep 17 00:00:00 2001 From: ychenfo Date: Sun, 19 Dec 2021 11:04:53 +0800 Subject: [PATCH] update kernel-only attribute annotation Reviewed-on: https://git.m-labs.hk/M-Labs/nac3/pulls/127 Co-authored-by: ychenfo Co-committed-by: ychenfo --- nac3artiq/demo/min_artiq.py | 5 +++- nac3artiq/src/lib.rs | 12 ++++++-- nac3core/src/toplevel/composer.rs | 49 ++++++++++++++++++++----------- nac3standalone/src/main.rs | 16 +++++----- 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/nac3artiq/demo/min_artiq.py b/nac3artiq/demo/min_artiq.py index 9adf1667b..64f41c735 100644 --- a/nac3artiq/demo/min_artiq.py +++ b/nac3artiq/demo/min_artiq.py @@ -9,7 +9,7 @@ import nac3artiq __all__ = [ - "KernelInvariant", "virtual", + "Kernel", "KernelInvariant", "virtual", "round64", "floor64", "ceil64", "extern", "kernel", "portable", "nac3", "ms", "us", "ns", @@ -21,6 +21,9 @@ __all__ = [ T = TypeVar('T') +class Kernel(Generic[T]): + pass + class KernelInvariant(Generic[T]): pass diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 405645d85..de34180e2 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -22,7 +22,7 @@ use parking_lot::{Mutex, RwLock}; use nac3core::{ codegen::{concrete_type::ConcreteTypeStore, CodeGenTask, WithCall, WorkerRegistry}, symbol_resolver::SymbolResolver, - toplevel::{composer::TopLevelComposer, DefinitionId, GenCall, TopLevelDef}, + toplevel::{composer::{TopLevelComposer, ComposerConfig}, DefinitionId, GenCall, TopLevelDef}, typecheck::typedef::{FunSignature, FuncArg}, typecheck::{type_inferencer::PrimitiveStore, typedef::Type}, }; @@ -239,7 +239,10 @@ impl Nac3 { }))), ), ]; - let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone()); + let (_, builtins_def, builtins_ty) = TopLevelComposer::new(builtins.clone(), ComposerConfig { + kernel_ann: Some("Kernel"), + kernel_invariant_ann: "KernelInvariant" + }); let builtins_mod = PyModule::import(py, "builtins").unwrap(); let id_fn = builtins_mod.getattr("id").unwrap(); @@ -375,7 +378,10 @@ impl Nac3 { filename: &str, py: Python, ) -> PyResult<()> { - let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone()); + let (mut composer, _, _) = TopLevelComposer::new(self.builtins.clone(), ComposerConfig { + kernel_ann: Some("Kernel"), + kernel_invariant_ann: "KernelInvariant" + }); let mut id_to_def = HashMap::new(); let mut id_to_type = HashMap::new(); diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 02b800da5..4c6336919 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -9,6 +9,20 @@ use crate::{ use super::*; +pub struct ComposerConfig { + pub kernel_ann: Option<&'static str>, + pub kernel_invariant_ann: &'static str, +} + +impl Default for ComposerConfig { + fn default() -> Self { + ComposerConfig { + kernel_ann: None, + kernel_invariant_ann: "Invariant" + } + } +} + type DefAst = (Arc>, Option>); pub struct TopLevelComposer { // list of top level definitions, same as top level context @@ -25,11 +39,12 @@ pub struct TopLevelComposer { pub method_class: HashMap, // number of built-in function and classes in the definition list, later skip pub builtin_num: usize, + pub core_config: ComposerConfig, } impl Default for TopLevelComposer { fn default() -> Self { - Self::new(vec![]).0 + Self::new(vec![], Default::default()).0 } } @@ -38,6 +53,7 @@ impl TopLevelComposer { /// resolver can later figure out primitive type definitions when passed a primitive type name pub fn new( builtins: Vec<(StrRef, FunSignature, Arc)>, + core_config: ComposerConfig ) -> (Self, HashMap, HashMap) { let mut primitives = Self::make_primitives(); let (mut definition_ast_list, builtin_name_list) = builtins::get_builtins(&mut primitives); @@ -108,6 +124,7 @@ impl TopLevelComposer { keyword_list, defined_names, method_class, + core_config, }, builtin_id, builtin_ty, @@ -554,7 +571,7 @@ impl TopLevelComposer { unifier, primitives, &mut type_var_to_concrete_def, - &self.keyword_list, + (&self.keyword_list, &self.core_config) )? } } @@ -827,8 +844,9 @@ impl TopLevelComposer { unifier: &mut Unifier, primitives: &PrimitiveStore, type_var_to_concrete_def: &mut HashMap, - keyword_list: &HashSet, + core_info: (&HashSet, &ComposerConfig), ) -> Result<(), String> { + let (keyword_list, core_config) = core_info; let mut class_def = class_def.write(); let ( class_id, @@ -1058,20 +1076,17 @@ impl TopLevelComposer { let dummy_field_type = unifier.get_fresh_var().0; // handle Kernel[T], KernelInvariant[T] - let (annotation, mutable) = { - let mut result = None; - if let ast::ExprKind::Subscript { value, slice, .. } = &annotation.as_ref().node { - if let ast::ExprKind::Name { id, .. } = &value.node { - result = if id == &"Kernel".into() { - Some((slice, true)) - } else if id == &"KernelInvariant".into() { - Some((slice, false)) - } else { - None - } - } - } - result.unwrap_or((annotation, true)) + let (annotation, mutable) = match &annotation.node { + ast::ExprKind::Subscript { value, slice, .. } if matches!( + &value.node, + ast::ExprKind::Name { id, .. } if id == &core_config.kernel_invariant_ann.into() + ) => (slice, false), + ast::ExprKind::Subscript { value, slice, .. } if matches!( + &value.node, + ast::ExprKind::Name { id, .. } if core_config.kernel_ann.map_or(false, |c| id == &c.into()) + ) => (slice, true), + _ if core_config.kernel_ann.is_none() => (annotation, true), + _ => continue // ignore fields annotated otherwise }; class_fields_def.push((*attr, dummy_field_type, mutable)); diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 238511c25..8212b9234 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -3,13 +3,10 @@ use inkwell::{ targets::*, OptimizationLevel, }; -use nac3core::typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier}}; -use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser}; +use std::{borrow::Borrow, collections::HashMap, env, fs, path::Path, sync::Arc, time::SystemTime}; use parking_lot::RwLock; -use std::{borrow::Borrow, env}; -use std::fs; -use std::{collections::HashMap, path::Path, sync::Arc, time::SystemTime}; +use nac3parser::{ast::{Expr, ExprKind, StmtKind}, parser}; use nac3core::{ codegen::{ concrete_type::ConcreteTypeStore, CodeGenTask, DefaultCodeGenerator, WithCall, @@ -17,11 +14,11 @@ use nac3core::{ }, symbol_resolver::SymbolResolver, toplevel::{ - composer::TopLevelComposer, + composer::{TopLevelComposer, ComposerConfig}, TopLevelDef, helper::parse_parameter_default_value, type_annotation::*, }, - typecheck::typedef::FunSignature, + typecheck::{type_inferencer::PrimitiveStore, typedef::{Type, Unifier, FunSignature}} }; mod basic_symbol_resolver; @@ -47,7 +44,10 @@ fn main() { }; let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; - let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new(vec![]); + let (mut composer, builtins_def, builtins_ty) = TopLevelComposer::new( + vec![], + Default::default() + ); let internal_resolver: Arc = ResolverInternal { id_to_type: builtins_ty.into(),