@@ -56,8 +56,9 @@ moduleForSumtype innerEnv typeEnv env pathStrings typeName typeVariables rest i
5656 candidate = TypeCandidate {typename = typeName, variables = typeVariables, restriction = AllowOnlyNamesInScope , typemembers = rest, interfaceConstraints = [] , candidateTypeEnv = typeEnv, candidateEnv = env}
5757 in do
5858 let structTy = StructTy (ConcreteNameTy (SymPath pathStrings typeName)) typeVariables
59+ ptrFix = map (recursiveMembersToPointers structTy) rest
5960 okRecursive candidate
60- cases <- toCases typeEnv env candidate
61+ cases <- toCases typeEnv env ( candidate {typemembers = ptrFix})
6162 okIniters <- initers insidePath structTy cases
6263 okTag <- binderForTag insidePath structTy
6364 (okStr, okStrDeps) <- binderForStrOrPrn typeEnv env insidePath structTy cases " str"
@@ -91,19 +92,21 @@ binderForCaseInit _ _ _ = error "binderforcaseinit"
9192
9293concreteCaseInit :: AllocationMode -> [String ] -> Ty -> SumtypeCase -> (String , Binder )
9394concreteCaseInit allocationMode insidePath structTy sumtypeCase =
94- instanceBinder (SymPath insidePath (caseName sumtypeCase)) (FuncTy (caseTys sumtypeCase) structTy StaticLifetimeTy ) template doc
95+ instanceBinder (SymPath insidePath (caseName sumtypeCase)) (FuncTy (map removeRec ( caseTys sumtypeCase) ) structTy StaticLifetimeTy ) template doc
9596 where
9697 doc = " creates a `" ++ caseName sumtypeCase ++ " `."
9798 template =
9899 Template
99- (FuncTy (caseTys sumtypeCase) (VarTy " p" ) StaticLifetimeTy )
100+ (FuncTy (map removeRec ( caseTys sumtypeCase) ) (VarTy " p" ) StaticLifetimeTy )
100101 ( \ (FuncTy _ concreteStructTy _) ->
101102 let mappings = unifySignatures structTy concreteStructTy
102- correctedTys = map (replaceTyVars mappings) (caseTys sumtypeCase)
103+ correctedTys = map (replaceTyVars mappings) (map removeRec ( caseTys sumtypeCase) )
103104 in (toTemplate $ " $p $NAME(" ++ joinWithComma (zipWith (curry memberArg) anonMemberNames (remove isUnit correctedTys)) ++ " )" )
104105 )
105106 (const (tokensForCaseInit allocationMode structTy sumtypeCase))
106107 (\ FuncTy {} -> [] )
108+ removeRec (RecTy t) = t
109+ removeRec t = t
107110
108111genericCaseInit :: AllocationMode -> [String ] -> Ty -> SumtypeCase -> (String , Binder )
109112genericCaseInit allocationMode pathStrings originalStructTy sumtypeCase =
@@ -141,13 +144,15 @@ tokensForCaseInit allocationMode sumTy@(StructTy (ConcreteNameTy _) _) sumtypeCa
141144 StackAlloc -> " $p instance;"
142145 HeapAlloc -> " $p instance = CARP_MALLOC(sizeof(" ++ show sumTy ++ " ));" ,
143146 joinLines $ caseMemberAssignment allocationMode correctedName . fst <$> unitless,
147+ joinLines $ recCaseMemberAssignment allocationMode correctedName sumTy . fst <$> recursive,
144148 " instance._tag = " ++ tagName sumTy correctedName ++ " ;" ,
145149 " return instance;" ,
146150 " }"
147151 ]
148152 where
149153 correctedName = caseName sumtypeCase
150- unitless = zip anonMemberNames $ remove isUnit (caseTys sumtypeCase)
154+ unitless = remove (isRecType . snd ) $ zip anonMemberNames $ remove isUnit (caseTys sumtypeCase)
155+ recursive = filter (isRecType . snd ) $ zip anonMemberNames (caseTys sumtypeCase)
151156tokensForCaseInit _ _ _ = error " tokensforcaseinit"
152157
153158caseMemberAssignment :: AllocationMode -> String -> String -> String
@@ -158,6 +163,15 @@ caseMemberAssignment allocationMode caseNm memberName =
158163 StackAlloc -> " .u."
159164 HeapAlloc -> " ->u."
160165
166+ recCaseMemberAssignment :: AllocationMode -> String -> Ty -> String -> String
167+ recCaseMemberAssignment allocationMode caseNm sumTy memberName =
168+ " instance" ++ sep ++ caseNm ++ " ." ++ memberName ++ " = CARP_MALLOC(sizeof(" ++ show sumTy ++ " ));\n "
169+ ++ " *instance" ++ sep ++ caseNm ++ " ." ++ memberName ++ " = " ++ memberName ++ " ;"
170+ where
171+ sep = case allocationMode of
172+ StackAlloc -> " .u."
173+ HeapAlloc -> " ->u."
174+
161175binderForTag :: [String ] -> Ty -> Either TypeError (String , Binder )
162176binderForTag insidePath originalStructTy@ (StructTy (ConcreteNameTy _) _) =
163177 Right $ instanceBinder path (FuncTy [RefTy originalStructTy (VarTy " q" )] IntTy StaticLifetimeTy ) template doc
0 commit comments