@@ -7,9 +7,10 @@ use crate::{
77 error:: { ResolveError , ResolveErrorKind } ,
88 Initialized ,
99 } ,
10- resolved:: { self , Cast , CastFrom , TypedExpr } ,
10+ resolved:: { self , Cast , CastFrom , PolyValue , TypedExpr } ,
1111 source_files:: Source ,
1212} ;
13+ use indexmap:: IndexMap ;
1314use itertools:: Itertools ;
1415use num:: BigInt ;
1516
@@ -265,8 +266,6 @@ pub fn resolve_call_expr(
265266 } ;
266267
267268 let function = ctx. resolved_ast . functions . get ( callee. function ) . unwrap ( ) ;
268- let return_type = function. return_type . clone ( ) ;
269-
270269 let num_required = function. parameters . required . len ( ) ;
271270
272271 for ( i, argument) in arguments. iter_mut ( ) . enumerate ( ) {
@@ -329,6 +328,8 @@ pub fn resolve_call_expr(
329328 }
330329 }
331330
331+ let return_type = resolve_polymorphs ( & callee. recipe , & function. return_type ) ?;
332+
332333 if let Some ( required_ty) = & call. expected_to_return {
333334 let resolved_required_ty = ctx. type_ctx ( ) . resolve ( required_ty) ?;
334335
@@ -355,3 +356,48 @@ pub fn resolve_call_expr(
355356 ) ,
356357 ) )
357358}
359+
360+ pub fn resolve_polymorphs < ' a > (
361+ polymorphs : & IndexMap < String , PolyValue > ,
362+ ty : & resolved:: Type ,
363+ ) -> Result < resolved:: Type , ResolveError > {
364+ Ok ( match & ty. kind {
365+ resolved:: TypeKind :: Unresolved => panic ! ( ) ,
366+ resolved:: TypeKind :: Boolean
367+ | resolved:: TypeKind :: Integer ( _, _)
368+ | resolved:: TypeKind :: CInteger ( _, _)
369+ | resolved:: TypeKind :: IntegerLiteral ( _)
370+ | resolved:: TypeKind :: FloatLiteral ( _)
371+ | resolved:: TypeKind :: Void
372+ | resolved:: TypeKind :: Floating ( _) => ty. clone ( ) ,
373+ resolved:: TypeKind :: Pointer ( inner) => {
374+ resolved:: TypeKind :: Pointer ( Box :: new ( resolve_polymorphs ( polymorphs, inner) ?) )
375+ . at ( ty. source )
376+ }
377+ resolved:: TypeKind :: AnonymousStruct ( ) => todo ! ( ) ,
378+ resolved:: TypeKind :: AnonymousUnion ( ) => todo ! ( ) ,
379+ resolved:: TypeKind :: AnonymousEnum ( _) => todo ! ( ) ,
380+ resolved:: TypeKind :: FixedArray ( fixed_array) => {
381+ resolved:: TypeKind :: FixedArray ( Box :: new ( resolved:: FixedArray {
382+ size : fixed_array. size ,
383+ inner : resolve_polymorphs ( polymorphs, & fixed_array. inner ) ?,
384+ } ) )
385+ . at ( ty. source )
386+ }
387+ resolved:: TypeKind :: FunctionPointer ( _) => todo ! ( ) ,
388+ resolved:: TypeKind :: Enum ( _, _) => todo ! ( ) ,
389+ resolved:: TypeKind :: Structure ( _, _) => todo ! ( ) ,
390+ resolved:: TypeKind :: TypeAlias ( _, _) => todo ! ( ) ,
391+ resolved:: TypeKind :: Polymorph ( name, _) => {
392+ let Some ( value) = polymorphs. get ( name) else {
393+ return Err ( ResolveErrorKind :: NonExistentPolymorph ( name. clone ( ) ) . at ( ty. source ) ) ;
394+ } ;
395+
396+ let PolyValue :: PolyType ( poly_type) = value else {
397+ return Err ( ResolveErrorKind :: PolymorphIsNotAType ( name. clone ( ) ) . at ( ty. source ) ) ;
398+ } ;
399+
400+ poly_type. resolved_type . clone ( )
401+ }
402+ } )
403+ }
0 commit comments