@@ -56,8 +56,9 @@ moduleForSumtype innerEnv typeEnv env pathStrings typeName typeVariables rest i
56
56
candidate = TypeCandidate {typename = typeName, variables = typeVariables, restriction = AllowOnlyNamesInScope , typemembers = rest, interfaceConstraints = [] , candidateTypeEnv = typeEnv, candidateEnv = env}
57
57
in do
58
58
let structTy = StructTy (ConcreteNameTy (SymPath pathStrings typeName)) typeVariables
59
+ ptrFix = map (recursiveMembersToPointers structTy) rest
59
60
okRecursive candidate
60
- cases <- toCases typeEnv env candidate
61
+ cases <- toCases typeEnv env ( candidate {typemembers = ptrFix})
61
62
okIniters <- initers insidePath structTy cases
62
63
okTag <- binderForTag insidePath structTy
63
64
(okStr, okStrDeps) <- binderForStrOrPrn typeEnv env insidePath structTy cases " str"
@@ -91,19 +92,21 @@ binderForCaseInit _ _ _ = error "binderforcaseinit"
91
92
92
93
concreteCaseInit :: AllocationMode -> [String ] -> Ty -> SumtypeCase -> (String , Binder )
93
94
concreteCaseInit 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
95
96
where
96
97
doc = " creates a `" ++ caseName sumtypeCase ++ " `."
97
98
template =
98
99
Template
99
- (FuncTy (caseTys sumtypeCase) (VarTy " p" ) StaticLifetimeTy )
100
+ (FuncTy (map removeRec ( caseTys sumtypeCase) ) (VarTy " p" ) StaticLifetimeTy )
100
101
( \ (FuncTy _ concreteStructTy _) ->
101
102
let mappings = unifySignatures structTy concreteStructTy
102
- correctedTys = map (replaceTyVars mappings) (caseTys sumtypeCase)
103
+ correctedTys = map (replaceTyVars mappings) (map removeRec ( caseTys sumtypeCase) )
103
104
in (toTemplate $ " $p $NAME(" ++ joinWithComma (zipWith (curry memberArg) anonMemberNames (remove isUnit correctedTys)) ++ " )" )
104
105
)
105
106
(const (tokensForCaseInit allocationMode structTy sumtypeCase))
106
107
(\ FuncTy {} -> [] )
108
+ removeRec (RecTy t) = t
109
+ removeRec t = t
107
110
108
111
genericCaseInit :: AllocationMode -> [String ] -> Ty -> SumtypeCase -> (String , Binder )
109
112
genericCaseInit allocationMode pathStrings originalStructTy sumtypeCase =
@@ -141,13 +144,15 @@ tokensForCaseInit allocationMode sumTy@(StructTy (ConcreteNameTy _) _) sumtypeCa
141
144
StackAlloc -> " $p instance;"
142
145
HeapAlloc -> " $p instance = CARP_MALLOC(sizeof(" ++ show sumTy ++ " ));" ,
143
146
joinLines $ caseMemberAssignment allocationMode correctedName . fst <$> unitless,
147
+ joinLines $ recCaseMemberAssignment allocationMode correctedName sumTy . fst <$> recursive,
144
148
" instance._tag = " ++ tagName sumTy correctedName ++ " ;" ,
145
149
" return instance;" ,
146
150
" }"
147
151
]
148
152
where
149
153
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)
151
156
tokensForCaseInit _ _ _ = error " tokensforcaseinit"
152
157
153
158
caseMemberAssignment :: AllocationMode -> String -> String -> String
@@ -158,6 +163,15 @@ caseMemberAssignment allocationMode caseNm memberName =
158
163
StackAlloc -> " .u."
159
164
HeapAlloc -> " ->u."
160
165
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
+
161
175
binderForTag :: [String ] -> Ty -> Either TypeError (String , Binder )
162
176
binderForTag insidePath originalStructTy@ (StructTy (ConcreteNameTy _) _) =
163
177
Right $ instanceBinder path (FuncTy [RefTy originalStructTy (VarTy " q" )] IntTy StaticLifetimeTy ) template doc
0 commit comments