@@ -25,6 +25,7 @@ use crate::{
2525 FunctionReturnType , IntegerBitSize , LValue , Literal , Statement , StatementKind , UnaryOp ,
2626 UnresolvedType , UnresolvedTypeData , Visibility ,
2727 } ,
28+ hir:: def_collector:: dc_crate:: CollectedItems ,
2829 hir:: {
2930 comptime:: {
3031 errors:: IResult ,
@@ -117,6 +118,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
117118 "function_def_set_return_public" => {
118119 function_def_set_return_public ( self , arguments, location)
119120 }
121+ "module_add_item" => module_add_item ( self , arguments, location) ,
120122 "module_functions" => module_functions ( self , arguments, location) ,
121123 "module_has_named_attribute" => module_has_named_attribute ( self , arguments, location) ,
122124 "module_is_contract" => module_is_contract ( self , arguments, location) ,
@@ -663,9 +665,10 @@ fn quoted_as_module(
663665
664666 let path = parse ( argument, parser:: path_no_turbofish ( ) , "a path" ) . ok ( ) ;
665667 let option_value = path. and_then ( |path| {
666- let module = interpreter. elaborate_item ( interpreter. current_function , |elaborator| {
667- elaborator. resolve_module_by_path ( path)
668- } ) ;
668+ let module = interpreter
669+ . elaborate_in_function ( interpreter. current_function , |elaborator| {
670+ elaborator. resolve_module_by_path ( path)
671+ } ) ;
669672 module. map ( Value :: ModuleDefinition )
670673 } ) ;
671674
@@ -681,7 +684,7 @@ fn quoted_as_trait_constraint(
681684 let argument = check_one_argument ( arguments, location) ?;
682685 let trait_bound = parse ( argument, parser:: trait_bound ( ) , "a trait constraint" ) ?;
683686 let bound = interpreter
684- . elaborate_item ( interpreter. current_function , |elaborator| {
687+ . elaborate_in_function ( interpreter. current_function , |elaborator| {
685688 elaborator. resolve_trait_bound ( & trait_bound, Type :: Unit )
686689 } )
687690 . ok_or ( InterpreterError :: FailedToResolveTraitBound { trait_bound, location } ) ?;
@@ -697,8 +700,8 @@ fn quoted_as_type(
697700) -> IResult < Value > {
698701 let argument = check_one_argument ( arguments, location) ?;
699702 let typ = parse ( argument, parser:: parse_type ( ) , "a type" ) ?;
700- let typ =
701- interpreter . elaborate_item ( interpreter. current_function , |elab| elab. resolve_type ( typ) ) ;
703+ let typ = interpreter
704+ . elaborate_in_function ( interpreter. current_function , |elab| elab. resolve_type ( typ) ) ;
702705 Ok ( Value :: Type ( typ) )
703706}
704707
@@ -1768,23 +1771,25 @@ fn expr_resolve(
17681771 interpreter. current_function
17691772 } ;
17701773
1771- let value = interpreter. elaborate_item ( function_to_resolve_in, |elaborator| match expr_value {
1772- ExprValue :: Expression ( expression_kind) => {
1773- let expr = Expression { kind : expression_kind, span : self_argument_location. span } ;
1774- let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1775- Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1776- }
1777- ExprValue :: Statement ( statement_kind) => {
1778- let statement = Statement { kind : statement_kind, span : self_argument_location. span } ;
1779- let ( stmt_id, _) = elaborator. elaborate_statement ( statement) ;
1780- Value :: TypedExpr ( TypedExpr :: StmtId ( stmt_id) )
1781- }
1782- ExprValue :: LValue ( lvalue) => {
1783- let expr = lvalue. as_expression ( ) ;
1784- let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1785- Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1786- }
1787- } ) ;
1774+ let value =
1775+ interpreter. elaborate_in_function ( function_to_resolve_in, |elaborator| match expr_value {
1776+ ExprValue :: Expression ( expression_kind) => {
1777+ let expr = Expression { kind : expression_kind, span : self_argument_location. span } ;
1778+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1779+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1780+ }
1781+ ExprValue :: Statement ( statement_kind) => {
1782+ let statement =
1783+ Statement { kind : statement_kind, span : self_argument_location. span } ;
1784+ let ( stmt_id, _) = elaborator. elaborate_statement ( statement) ;
1785+ Value :: TypedExpr ( TypedExpr :: StmtId ( stmt_id) )
1786+ }
1787+ ExprValue :: LValue ( lvalue) => {
1788+ let expr = lvalue. as_expression ( ) ;
1789+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1790+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1791+ }
1792+ } ) ;
17881793
17891794 Ok ( value)
17901795}
@@ -2052,7 +2057,7 @@ fn function_def_set_parameters(
20522057 "a pattern" ,
20532058 ) ?;
20542059
2055- let hir_pattern = interpreter. elaborate_item ( Some ( func_id) , |elaborator| {
2060+ let hir_pattern = interpreter. elaborate_in_function ( Some ( func_id) , |elaborator| {
20562061 elaborator. elaborate_pattern_and_store_ids (
20572062 parameter_pattern,
20582063 parameter_type. clone ( ) ,
@@ -2119,6 +2124,34 @@ fn function_def_set_return_public(
21192124 Ok ( Value :: Unit )
21202125}
21212126
2127+ // fn add_item(self, item: Quoted)
2128+ fn module_add_item (
2129+ interpreter : & mut Interpreter ,
2130+ arguments : Vec < ( Value , Location ) > ,
2131+ location : Location ,
2132+ ) -> IResult < Value > {
2133+ let ( self_argument, item) = check_two_arguments ( arguments, location) ?;
2134+ let module_id = get_module ( self_argument) ?;
2135+ let module_data = interpreter. elaborator . get_module ( module_id) ;
2136+
2137+ let parser = parser:: top_level_items ( ) ;
2138+ let top_level_statements = parse ( item, parser, "a top-level item" ) ?;
2139+
2140+ interpreter. elaborate_in_module ( module_id, module_data. location . file , |elaborator| {
2141+ let mut generated_items = CollectedItems :: default ( ) ;
2142+
2143+ for top_level_statement in top_level_statements {
2144+ elaborator. add_item ( top_level_statement, & mut generated_items, location) ;
2145+ }
2146+
2147+ if !generated_items. is_empty ( ) {
2148+ elaborator. elaborate_items ( generated_items) ;
2149+ }
2150+ } ) ;
2151+
2152+ Ok ( Value :: Unit )
2153+ }
2154+
21222155// fn functions(self) -> [FunctionDefinition]
21232156fn module_functions (
21242157 interpreter : & Interpreter ,
0 commit comments