@@ -25,10 +25,13 @@ use crate::{
25
25
FunctionReturnType , IntegerBitSize , LValue , Literal , Statement , StatementKind , UnaryOp ,
26
26
UnresolvedType , UnresolvedTypeData , Visibility ,
27
27
} ,
28
- hir:: comptime:: {
29
- errors:: IResult ,
30
- value:: { ExprValue , TypedExpr } ,
31
- InterpreterError , Value ,
28
+ hir:: {
29
+ comptime:: {
30
+ errors:: IResult ,
31
+ value:: { ExprValue , TypedExpr } ,
32
+ InterpreterError , Value ,
33
+ } ,
34
+ def_map:: ModuleId ,
32
35
} ,
33
36
hir_def:: function:: FunctionBody ,
34
37
lexer:: Lexer ,
@@ -102,6 +105,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
102
105
"function_def_has_named_attribute" => {
103
106
function_def_has_named_attribute ( interner, arguments, location)
104
107
}
108
+ "function_def_module" => function_def_module ( interner, arguments, location) ,
105
109
"function_def_name" => function_def_name ( interner, arguments, location) ,
106
110
"function_def_parameters" => function_def_parameters ( interner, arguments, location) ,
107
111
"function_def_return_type" => function_def_return_type ( interner, arguments, location) ,
@@ -142,6 +146,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
142
146
"struct_def_has_named_attribute" => {
143
147
struct_def_has_named_attribute ( interner, arguments, location)
144
148
}
149
+ "struct_def_module" => struct_def_module ( self , arguments, location) ,
145
150
"struct_def_set_fields" => struct_def_set_fields ( interner, arguments, location) ,
146
151
"to_le_radix" => to_le_radix ( arguments, return_type, location) ,
147
152
"trait_constraint_eq" => trait_constraint_eq ( interner, arguments, location) ,
@@ -399,6 +404,25 @@ fn struct_def_fields(
399
404
Ok ( Value :: Slice ( fields, typ) )
400
405
}
401
406
407
+ // fn module(self) -> Module
408
+ fn struct_def_module (
409
+ interpreter : & Interpreter ,
410
+ arguments : Vec < ( Value , Location ) > ,
411
+ location : Location ,
412
+ ) -> IResult < Value > {
413
+ let self_argument = check_one_argument ( arguments, location) ?;
414
+ let struct_id = get_struct ( self_argument) ?;
415
+ let struct_module_id = struct_id. module_id ( ) ;
416
+
417
+ // A struct's module is its own module. To get the module where its defined we need
418
+ // to look for its parent.
419
+ let module_data = interpreter. elaborator . get_module ( struct_module_id) ;
420
+ let parent_local_id = module_data. parent . expect ( "Expected struct module parent to exist" ) ;
421
+ let parent = ModuleId { krate : struct_module_id. krate , local_id : parent_local_id } ;
422
+
423
+ Ok ( Value :: ModuleDefinition ( parent) )
424
+ }
425
+
402
426
/// fn set_fields(self, new_fields: [(Quoted, Type)]) {}
403
427
/// Returns (name, type) pairs of each field of this StructDefinition
404
428
fn struct_def_set_fields (
@@ -1827,6 +1851,18 @@ fn function_def_has_named_attribute(
1827
1851
Ok ( Value :: Bool ( has_named_attribute ( & name, attributes, location) ) )
1828
1852
}
1829
1853
1854
+ // fn module(self) -> Module
1855
+ fn function_def_module (
1856
+ interner : & NodeInterner ,
1857
+ arguments : Vec < ( Value , Location ) > ,
1858
+ location : Location ,
1859
+ ) -> IResult < Value > {
1860
+ let self_argument = check_one_argument ( arguments, location) ?;
1861
+ let func_id = get_function_def ( self_argument) ?;
1862
+ let module = interner. function_module ( func_id) ;
1863
+ Ok ( Value :: ModuleDefinition ( module) )
1864
+ }
1865
+
1830
1866
// fn name(self) -> Quoted
1831
1867
fn function_def_name (
1832
1868
interner : & NodeInterner ,
0 commit comments