@@ -25,6 +25,7 @@ use crate::{
25
25
FunctionReturnType , IntegerBitSize , LValue , Literal , Statement , StatementKind , UnaryOp ,
26
26
UnresolvedType , UnresolvedTypeData , Visibility ,
27
27
} ,
28
+ hir:: def_collector:: dc_crate:: CollectedItems ,
28
29
hir:: {
29
30
comptime:: {
30
31
errors:: IResult ,
@@ -117,6 +118,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
117
118
"function_def_set_return_public" => {
118
119
function_def_set_return_public ( self , arguments, location)
119
120
}
121
+ "module_add_item" => module_add_item ( self , arguments, location) ,
120
122
"module_functions" => module_functions ( self , arguments, location) ,
121
123
"module_has_named_attribute" => module_has_named_attribute ( self , arguments, location) ,
122
124
"module_is_contract" => module_is_contract ( self , arguments, location) ,
@@ -663,9 +665,10 @@ fn quoted_as_module(
663
665
664
666
let path = parse ( argument, parser:: path_no_turbofish ( ) , "a path" ) . ok ( ) ;
665
667
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
+ } ) ;
669
672
module. map ( Value :: ModuleDefinition )
670
673
} ) ;
671
674
@@ -681,7 +684,7 @@ fn quoted_as_trait_constraint(
681
684
let argument = check_one_argument ( arguments, location) ?;
682
685
let trait_bound = parse ( argument, parser:: trait_bound ( ) , "a trait constraint" ) ?;
683
686
let bound = interpreter
684
- . elaborate_item ( interpreter. current_function , |elaborator| {
687
+ . elaborate_in_function ( interpreter. current_function , |elaborator| {
685
688
elaborator. resolve_trait_bound ( & trait_bound, Type :: Unit )
686
689
} )
687
690
. ok_or ( InterpreterError :: FailedToResolveTraitBound { trait_bound, location } ) ?;
@@ -697,8 +700,8 @@ fn quoted_as_type(
697
700
) -> IResult < Value > {
698
701
let argument = check_one_argument ( arguments, location) ?;
699
702
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) ) ;
702
705
Ok ( Value :: Type ( typ) )
703
706
}
704
707
@@ -1768,23 +1771,25 @@ fn expr_resolve(
1768
1771
interpreter. current_function
1769
1772
} ;
1770
1773
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
+ } ) ;
1788
1793
1789
1794
Ok ( value)
1790
1795
}
@@ -2052,7 +2057,7 @@ fn function_def_set_parameters(
2052
2057
"a pattern" ,
2053
2058
) ?;
2054
2059
2055
- let hir_pattern = interpreter. elaborate_item ( Some ( func_id) , |elaborator| {
2060
+ let hir_pattern = interpreter. elaborate_in_function ( Some ( func_id) , |elaborator| {
2056
2061
elaborator. elaborate_pattern_and_store_ids (
2057
2062
parameter_pattern,
2058
2063
parameter_type. clone ( ) ,
@@ -2119,6 +2124,34 @@ fn function_def_set_return_public(
2119
2124
Ok ( Value :: Unit )
2120
2125
}
2121
2126
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
+
2122
2155
// fn functions(self) -> [FunctionDefinition]
2123
2156
fn module_functions (
2124
2157
interpreter : & Interpreter ,
0 commit comments