@@ -8,9 +8,9 @@ use builtin_helpers::{
88 block_expression_to_value, check_argument_count, check_function_not_yet_resolved,
99 check_one_argument, check_three_arguments, check_two_arguments, get_expr, get_field,
1010 get_function_def, get_module, get_quoted, get_slice, get_struct, get_trait_constraint,
11- get_trait_def, get_trait_impl, get_tuple, get_type, get_u32 , get_unresolved_type ,
12- hir_pattern_to_tokens, mutate_func_meta_type, parse, replace_func_meta_parameters ,
13- replace_func_meta_return_type,
11+ get_trait_def, get_trait_impl, get_tuple, get_type, get_typed_expr , get_u32 ,
12+ get_unresolved_type , hir_pattern_to_tokens, mutate_func_meta_type, parse,
13+ replace_func_meta_parameters , replace_func_meta_return_type,
1414} ;
1515use chumsky:: { prelude:: choice, Parser } ;
1616use im:: Vector ;
@@ -25,7 +25,11 @@ use crate::{
2525 FunctionReturnType , IntegerBitSize , LValue , Literal , Statement , StatementKind , UnaryOp ,
2626 UnresolvedType , UnresolvedTypeData , Visibility ,
2727 } ,
28- hir:: comptime:: { errors:: IResult , value:: ExprValue , InterpreterError , Value } ,
28+ hir:: comptime:: {
29+ errors:: IResult ,
30+ value:: { ExprValue , TypedExpr } ,
31+ InterpreterError , Value ,
32+ } ,
2933 hir_def:: function:: FunctionBody ,
3034 macros_api:: { HirExpression , HirLiteral , ModuleDefId , NodeInterner , Signedness } ,
3135 node_interner:: { DefinitionKind , TraitImplKind } ,
@@ -87,6 +91,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
8791 "expr_has_semicolon" => expr_has_semicolon ( interner, arguments, location) ,
8892 "expr_is_break" => expr_is_break ( interner, arguments, location) ,
8993 "expr_is_continue" => expr_is_continue ( interner, arguments, location) ,
94+ "expr_resolve" => expr_resolve ( self , arguments, location) ,
9095 "is_unconstrained" => Ok ( Value :: Bool ( true ) ) ,
9196 "function_def_body" => function_def_body ( interner, arguments, location) ,
9297 "function_def_name" => function_def_name ( interner, arguments, location) ,
@@ -145,6 +150,9 @@ impl<'local, 'context> Interpreter<'local, 'context> {
145150 "type_is_bool" => type_is_bool ( arguments, location) ,
146151 "type_is_field" => type_is_field ( arguments, location) ,
147152 "type_of" => type_of ( arguments, location) ,
153+ "typed_expr_as_function_definition" => {
154+ typed_expr_as_function_definition ( interner, arguments, return_type, location)
155+ }
148156 "unresolved_type_is_field" => unresolved_type_is_field ( interner, arguments, location) ,
149157 "zeroed" => zeroed ( return_type) ,
150158 _ => {
@@ -763,6 +771,23 @@ fn trait_impl_trait_generic_args(
763771 Ok ( Value :: Slice ( trait_generics, slice_type) )
764772}
765773
774+ fn typed_expr_as_function_definition (
775+ interner : & NodeInterner ,
776+ arguments : Vec < ( Value , Location ) > ,
777+ return_type : Type ,
778+ location : Location ,
779+ ) -> IResult < Value > {
780+ let self_argument = check_one_argument ( arguments, location) ?;
781+ let typed_expr = get_typed_expr ( self_argument) ?;
782+ let option_value = if let TypedExpr :: ExprId ( expr_id) = typed_expr {
783+ let func_id = interner. lookup_function_from_expr ( & expr_id) ;
784+ func_id. map ( Value :: FunctionDefinition )
785+ } else {
786+ None
787+ } ;
788+ option ( return_type, option_value)
789+ }
790+
766791// fn is_field(self) -> bool
767792fn unresolved_type_is_field (
768793 interner : & NodeInterner ,
@@ -1380,7 +1405,48 @@ where
13801405 F : FnOnce ( ExprValue ) -> Option < Value > ,
13811406{
13821407 let self_argument = check_one_argument ( arguments, location) ?;
1383- let mut expr_value = get_expr ( interner, self_argument) ?;
1408+ let expr_value = get_expr ( interner, self_argument) ?;
1409+ let expr_value = unwrap_expr_value ( interner, expr_value) ;
1410+
1411+ let option_value = f ( expr_value) ;
1412+ option ( return_type, option_value)
1413+ }
1414+
1415+ // fn resolve(self) -> TypedExpr
1416+ fn expr_resolve (
1417+ interpreter : & mut Interpreter ,
1418+ arguments : Vec < ( Value , Location ) > ,
1419+ location : Location ,
1420+ ) -> IResult < Value > {
1421+ let self_argument = check_one_argument ( arguments, location) ?;
1422+ let self_argument_location = self_argument. 1 ;
1423+ let expr_value = get_expr ( interpreter. elaborator . interner , self_argument) ?;
1424+ let expr_value = unwrap_expr_value ( interpreter. elaborator . interner , expr_value) ;
1425+
1426+ let value =
1427+ interpreter. elaborate_item ( interpreter. current_function , |elaborator| match expr_value {
1428+ ExprValue :: Expression ( expression_kind) => {
1429+ let expr = Expression { kind : expression_kind, span : self_argument_location. span } ;
1430+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1431+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1432+ }
1433+ ExprValue :: Statement ( statement_kind) => {
1434+ let statement =
1435+ Statement { kind : statement_kind, span : self_argument_location. span } ;
1436+ let ( stmt_id, _) = elaborator. elaborate_statement ( statement) ;
1437+ Value :: TypedExpr ( TypedExpr :: StmtId ( stmt_id) )
1438+ }
1439+ ExprValue :: LValue ( lvalue) => {
1440+ let expr = lvalue. as_expression ( ) ;
1441+ let ( expr_id, _) = elaborator. elaborate_expression ( expr) ;
1442+ Value :: TypedExpr ( TypedExpr :: ExprId ( expr_id) )
1443+ }
1444+ } ) ;
1445+
1446+ Ok ( value)
1447+ }
1448+
1449+ fn unwrap_expr_value ( interner : & NodeInterner , mut expr_value : ExprValue ) -> ExprValue {
13841450 loop {
13851451 match expr_value {
13861452 ExprValue :: Expression ( ExpressionKind :: Parenthesized ( expression) ) => {
@@ -1402,9 +1468,7 @@ where
14021468 _ => break ,
14031469 }
14041470 }
1405-
1406- let option_value = f ( expr_value) ;
1407- option ( return_type, option_value)
1471+ expr_value
14081472}
14091473
14101474// fn body(self) -> Expr
0 commit comments