@@ -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) ,
@@ -607,9 +609,10 @@ fn quoted_as_module(
607
609
608
610
let path = parse ( argument, parser:: path_no_turbofish ( ) , "a path" ) . ok ( ) ;
609
611
let option_value = path. and_then ( |path| {
610
- let module = interpreter. elaborate_item ( interpreter. current_function , |elaborator| {
611
- elaborator. resolve_module_by_path ( path)
612
- } ) ;
612
+ let module = interpreter
613
+ . elaborate_in_function ( interpreter. current_function , |elaborator| {
614
+ elaborator. resolve_module_by_path ( path)
615
+ } ) ;
613
616
module. map ( Value :: ModuleDefinition )
614
617
} ) ;
615
618
@@ -625,7 +628,7 @@ fn quoted_as_trait_constraint(
625
628
let argument = check_one_argument ( arguments, location) ?;
626
629
let trait_bound = parse ( argument, parser:: trait_bound ( ) , "a trait constraint" ) ?;
627
630
let bound = interpreter
628
- . elaborate_item ( interpreter. current_function , |elaborator| {
631
+ . elaborate_in_function ( interpreter. current_function , |elaborator| {
629
632
elaborator. resolve_trait_bound ( & trait_bound, Type :: Unit )
630
633
} )
631
634
. ok_or ( InterpreterError :: FailedToResolveTraitBound { trait_bound, location } ) ?;
@@ -641,8 +644,8 @@ fn quoted_as_type(
641
644
) -> IResult < Value > {
642
645
let argument = check_one_argument ( arguments, location) ?;
643
646
let typ = parse ( argument, parser:: parse_type ( ) , "a type" ) ?;
644
- let typ =
645
- interpreter . elaborate_item ( interpreter. current_function , |elab| elab. resolve_type ( typ) ) ;
647
+ let typ = interpreter
648
+ . elaborate_in_function ( interpreter. current_function , |elab| elab. resolve_type ( typ) ) ;
646
649
Ok ( Value :: Type ( typ) )
647
650
}
648
651
@@ -1712,23 +1715,25 @@ fn expr_resolve(
1712
1715
interpreter. current_function
1713
1716
} ;
1714
1717
1715
- let value = interpreter. elaborate_item ( function_to_resolve_in, |elaborator| match expr_value {
1716
- ExprValue :: Expression ( expression_kind) => {
1717
- let expr = Expression { kind : expression_kind, span : self_argument_location. span } ;
1718
- let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1719
- Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1720
- }
1721
- ExprValue :: Statement ( statement_kind) => {
1722
- let statement = Statement { kind : statement_kind, span : self_argument_location. span } ;
1723
- let ( stmt_id, _) = elaborator. elaborate_statement ( statement) ;
1724
- Value :: TypedExpr ( TypedExpr :: StmtId ( stmt_id) )
1725
- }
1726
- ExprValue :: LValue ( lvalue) => {
1727
- let expr = lvalue. as_expression ( ) ;
1728
- let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1729
- Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1730
- }
1731
- } ) ;
1718
+ let value =
1719
+ interpreter. elaborate_in_function ( function_to_resolve_in, |elaborator| match expr_value {
1720
+ ExprValue :: Expression ( expression_kind) => {
1721
+ let expr = Expression { kind : expression_kind, span : self_argument_location. span } ;
1722
+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1723
+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1724
+ }
1725
+ ExprValue :: Statement ( statement_kind) => {
1726
+ let statement =
1727
+ Statement { kind : statement_kind, span : self_argument_location. span } ;
1728
+ let ( stmt_id, _) = elaborator. elaborate_statement ( statement) ;
1729
+ Value :: TypedExpr ( TypedExpr :: StmtId ( stmt_id) )
1730
+ }
1731
+ ExprValue :: LValue ( lvalue) => {
1732
+ let expr = lvalue. as_expression ( ) ;
1733
+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1734
+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1735
+ }
1736
+ } ) ;
1732
1737
1733
1738
Ok ( value)
1734
1739
}
@@ -1996,7 +2001,7 @@ fn function_def_set_parameters(
1996
2001
"a pattern" ,
1997
2002
) ?;
1998
2003
1999
- let hir_pattern = interpreter. elaborate_item ( Some ( func_id) , |elaborator| {
2004
+ let hir_pattern = interpreter. elaborate_in_function ( Some ( func_id) , |elaborator| {
2000
2005
elaborator. elaborate_pattern_and_store_ids (
2001
2006
parameter_pattern,
2002
2007
parameter_type. clone ( ) ,
@@ -2063,6 +2068,34 @@ fn function_def_set_return_public(
2063
2068
Ok ( Value :: Unit )
2064
2069
}
2065
2070
2071
+ // fn add_item(self, item: Quoted)
2072
+ fn module_add_item (
2073
+ interpreter : & mut Interpreter ,
2074
+ arguments : Vec < ( Value , Location ) > ,
2075
+ location : Location ,
2076
+ ) -> IResult < Value > {
2077
+ let ( self_argument, item) = check_two_arguments ( arguments, location) ?;
2078
+ let module_id = get_module ( self_argument) ?;
2079
+ let module_data = interpreter. elaborator . get_module ( module_id) ;
2080
+
2081
+ let parser = parser:: top_level_items ( ) ;
2082
+ let top_level_statements = parse ( item, parser, "a top-level item" ) ?;
2083
+
2084
+ interpreter. elaborate_in_module ( module_id, module_data. location . file , |elaborator| {
2085
+ let mut generated_items = CollectedItems :: default ( ) ;
2086
+
2087
+ for top_level_statement in top_level_statements {
2088
+ elaborator. add_item ( top_level_statement, & mut generated_items, location) ;
2089
+ }
2090
+
2091
+ if !generated_items. is_empty ( ) {
2092
+ elaborator. elaborate_items ( generated_items) ;
2093
+ }
2094
+ } ) ;
2095
+
2096
+ Ok ( Value :: Unit )
2097
+ }
2098
+
2066
2099
// fn functions(self) -> [FunctionDefinition]
2067
2100
fn module_functions (
2068
2101
interpreter : & Interpreter ,
0 commit comments