@@ -3,6 +3,8 @@ module RecType
3
3
recursiveMembersToPointers ,
4
4
isRecursive ,
5
5
recursiveProductMakeBinder ,
6
+ recursiveProductInitBinder ,
7
+ recTemplateGetter ,
6
8
)
7
9
where
8
10
@@ -18,7 +20,7 @@ import Data.Maybe (fromJust)
18
20
import Concretize
19
21
import ToTemplate
20
22
21
- isRecursive :: Ty -> XObj -> Bool
23
+ isRecursive :: Ty -> XObj -> Bool
22
24
isRecursive structTy@ (StructTy _ _) (XObj (Arr members) _ _) =
23
25
any go members
24
26
where go :: XObj -> Bool
@@ -44,7 +46,7 @@ recursiveMembersToPointers _ xobj = xobj
44
46
45
47
recursiveProductMakeBinder :: [String ] -> Ty -> [XObj ] -> Either TypeError (String , Binder )
46
48
recursiveProductMakeBinder insidePath structTy@ (StructTy (ConcreteNameTy _) _) [XObj (Arr membersXObjs) _ _] =
47
- Right $
49
+ Right $
48
50
instanceBinder
49
51
(SymPath insidePath " make" )
50
52
(FuncTy (initArgListTypes membersXObjs) structTy StaticLifetimeTy )
@@ -55,6 +57,84 @@ recursiveProductMakeBinder insidePath structTy@(StructTy (ConcreteNameTy _) _) [
55
57
map (fromJust . xobjToTy . snd ) (remove (isRecType . fromJust . xobjToTy . snd ) (pairwise xobjs))
56
58
recursiveProductMakeBinder _ _ _ = error " TODO"
57
59
60
+ recursiveProductInitBinder :: [String ] -> Ty -> [XObj ] -> Either TypeError (String , Binder )
61
+ recursiveProductInitBinder insidePath structTy@ (StructTy (ConcreteNameTy _) _) [XObj (Arr membersXObjs) _ _] =
62
+ Right $
63
+ instanceBinder
64
+ (SymPath insidePath " init" )
65
+ (FuncTy (initArgListTypes membersXObjs) structTy StaticLifetimeTy )
66
+ (recursiveProductInit HeapAlloc structTy membersXObjs)
67
+ (" creates a `" ++ show structTy ++ " `." )
68
+ where initArgListTypes :: [XObj ] -> [Ty ]
69
+ initArgListTypes xobjs =
70
+ map (fixRec . fromJust . xobjToTy . snd ) (pairwise xobjs)
71
+ fixRec (RecTy t) = t
72
+ fixRec t = t
73
+ recursiveProductInitBinder _ _ _ = error " TODO"
74
+
75
+ -- | The template for the 'make' and 'new' functions for a concrete deftype.
76
+ recursiveProductInit :: AllocationMode -> Ty -> [XObj ] -> Template
77
+ recursiveProductInit allocationMode originalStructTy@ (StructTy (ConcreteNameTy _) _) membersXObjs =
78
+ let pairs = memberXObjsToPairs membersXObjs
79
+ unitless = remove (isUnit . snd )
80
+ unrec = map go . unitless
81
+ go (x, (RecTy t)) = (x, t)
82
+ go (x, t) = (x, t)
83
+ in Template
84
+ (FuncTy (map snd (unrec pairs)) (VarTy " p" ) StaticLifetimeTy )
85
+ ( \ (FuncTy _ concreteStructTy _) ->
86
+ let mappings = unifySignatures originalStructTy concreteStructTy
87
+ correctedMembers = replaceGenericTypeSymbolsOnMembers mappings membersXObjs
88
+ memberPairs = memberXObjsToPairs correctedMembers
89
+ in (toTemplate $ " $p $NAME(" ++ joinWithComma (map memberArg (unrec memberPairs)) ++ " )" )
90
+ )
91
+ ( \ (FuncTy _ concreteStructTy _) ->
92
+ let mappings = unifySignatures originalStructTy concreteStructTy
93
+ correctedMembers = replaceGenericTypeSymbolsOnMembers mappings membersXObjs
94
+ in productInitTokens allocationMode (show originalStructTy) correctedMembers
95
+ )
96
+ (\ FuncTy {} -> [] )
97
+ where memberArg :: (String , Ty ) -> String
98
+ memberArg (memberName, memberTy) =
99
+ tyToCLambdaFix (templatizeTy memberTy) ++ " " ++ memberName
100
+ templatizeTy :: Ty -> Ty
101
+ templatizeTy (VarTy vt) = VarTy (" $" ++ vt)
102
+ templatizeTy (FuncTy argTys retTy ltTy) = FuncTy (map templatizeTy argTys) (templatizeTy retTy) (templatizeTy ltTy)
103
+ templatizeTy (StructTy name tys) = StructTy name (map templatizeTy tys)
104
+ templatizeTy (RefTy t lt) = RefTy (templatizeTy t) (templatizeTy lt)
105
+ templatizeTy (PointerTy t) = PointerTy (templatizeTy t)
106
+ templatizeTy t = t
107
+ recursiveProductInit _ _ _ = error " concreteinit"
108
+
109
+ productInitTokens :: AllocationMode -> String -> [XObj ] -> [Token ]
110
+ productInitTokens allocationMode typeName membersXObjs =
111
+ let pairs = (memberXObjsToPairs membersXObjs)
112
+ in toTemplate $
113
+ unlines
114
+ [ " $DECL {" ,
115
+ case allocationMode of
116
+ StackAlloc -> " $p instance;"
117
+ HeapAlloc -> " $p *instance = CARP_MALLOC(sizeof(" ++ typeName ++ " ));" ,
118
+ assignments pairs,
119
+ " return *instance;" ,
120
+ " }"
121
+ ]
122
+ where
123
+ assignments ps = go (remove (isUnit . snd ) ps)
124
+ where
125
+ go [] = " "
126
+ go xobjs = joinLines $ assign allocationMode <$> xobjs
127
+ assign _ (name, (RecTy _)) =
128
+ " instance" ++ " ->" ++ name ++ " = " ++ " CARP_MALLOC(sizeof(" ++ typeName ++ " ));\n "
129
+ ++ " *instance->" ++ name ++ " = " ++ name ++ " ;\n "
130
+ -- ++ " instance" ++ "->" ++ name ++ " = " ++ "&" ++ name ++ ";\n"
131
+ -- ++ " " ++ typeName ++"_delete(" ++ name ++ ");"
132
+ assign alloc (name, _) =
133
+ let accessor = case alloc of
134
+ StackAlloc -> " ."
135
+ HeapAlloc -> " ->"
136
+ in " instance" ++ accessor ++ name ++ " = " ++ name ++ " ;"
137
+
58
138
-- | The template for the 'make' and 'new' functions for a concrete deftype.
59
139
recursiveProductMake :: AllocationMode -> Ty -> [XObj ] -> Template
60
140
recursiveProductMake allocationMode originalStructTy@ (StructTy (ConcreteNameTy _) _) membersXObjs =
@@ -104,82 +184,21 @@ productMakeTokens allocationMode typeName membersXObjs =
104
184
where
105
185
go [] = " "
106
186
go xobjs = joinLines $ assign allocationMode <$> xobjs
107
- assign alloc (name, ty) =
187
+ assign alloc (name, ty) =
108
188
let accessor = case alloc of
109
189
StackAlloc -> " ."
110
190
HeapAlloc -> " ->"
111
- in if isRecType ty
191
+ in if isRecType ty
112
192
then " instance" ++ accessor ++ name ++ " = " ++ " NULL ;"
113
193
else " instance" ++ accessor ++ name ++ " = " ++ name ++ " ;"
114
194
115
- ---- | Generate a list of types from a deftype declaration.
116
- -- initArgListTypes :: [XObj] -> [Ty]
117
- -- initArgListTypes xobjs =
118
- -- map (fromJust . xobjToTy . snd) (pairwise xobjs)
119
-
120
- -- tokensForRecInit :: AllocationMode -> String -> [XObj] -> [Token]
121
- -- tokensForRecInit allocationMode typeName membersXObjs =
122
- -- toTemplate $
123
- -- unlines
124
- -- [ "$DECL {",
125
- -- case allocationMode of
126
- -- StackAlloc -> case unitless of
127
- -- -- if this is truly a memberless struct, init it to 0;
128
- -- -- This can happen, e.g. in cases where *all* members of the struct are of type Unit.
129
- -- -- Since we do not generate members for Unit types.
130
- -- [] -> " $p instance = {};"
131
- -- _ -> " $p instance;"
132
- -- HeapAlloc -> " $p instance = CARP_MALLOC(sizeof(" ++ typeName ++ "));",
133
- -- assignments membersXObjs,
134
- -- recAssignment recmembers,
135
- -- " return instance;",
136
- -- "}"
137
- -- ]
138
- -- where
139
- -- recmembers = filter (isRecType . snd) (memberXObjsToPairs membersXObjs)
140
- -- assignments [] = " instance.__dummy = 0;"
141
- -- assignments _ = go unitless
142
- -- where
143
- -- go [] = ""
144
- -- go xobjs = joinLines $ memberAssignment allocationMode . fst <$> xobjs
145
- -- unitless = remove isRecType (remove (isUnit . snd) (memberXObjsToPairs membersXObjs))
146
- -- recAssignment xs =
147
- --
148
- -- memberAssignment :: AllocationMode -> String -> String
149
- -- memberAssignment allocationMode memberName = " instance" ++ sep ++ memberName ++ " = " ++ memberName ++ ";"
150
- -- where
151
- -- sep = case allocationMode of
152
- -- StackAlloc -> "."
153
- -- HeapAlloc -> "->"
154
-
195
+ -- | The template for getters of recursive types.
196
+ recTemplateGetter :: String -> Ty -> Template
197
+ recTemplateGetter member (RecTy t) =
198
+ Template
199
+ (FuncTy [RefTy (VarTy " p" ) (VarTy " q" )] (RefTy t (VarTy " q" )) StaticLifetimeTy )
200
+ (const (toTemplate ((tyToC (PointerTy t)) ++ " $NAME($(Ref p) p)" )))
201
+ (const $ toTemplate (" $DECL { return p->" ++ member ++ " ; }\n " ))
202
+ (const [] )
203
+ recTemplateGetter _ _ = error " rectemplate getter"
155
204
156
- --
157
- ---- | The template for the 'init' and 'new' functions for a generic deftype.
158
- -- genericInit :: AllocationMode -> [String] -> Ty -> [XObj] -> (String, Binder)
159
- -- genericInit allocationMode pathStrings originalStructTy@(StructTy (ConcreteNameTy _) _) membersXObjs =
160
- -- defineTypeParameterizedTemplate templateCreator path t docs
161
- -- where
162
- -- path = SymPath pathStrings "init"
163
- -- t = FuncTy (map snd (memberXObjsToPairs membersXObjs)) originalStructTy StaticLifetimeTy
164
- -- docs = "creates a `" ++ show originalStructTy ++ "`."
165
- -- templateCreator = TemplateCreator $
166
- -- \typeEnv env ->
167
- -- Template
168
- -- (FuncTy (map snd (memberXObjsToPairs membersXObjs)) (VarTy "p") StaticLifetimeTy)
169
- -- ( \(FuncTy _ concreteStructTy _) ->
170
- -- let mappings = unifySignatures originalStructTy concreteStructTy
171
- -- correctedMembers = replaceGenericTypeSymbolsOnMembers mappings membersXObjs
172
- -- memberPairs = memberXObjsToPairs correctedMembers
173
- -- in (toTemplate $ "$p $NAME(" ++ joinWithComma (map memberArg (remove (isUnit . snd) memberPairs)) ++ ")")
174
- -- )
175
- -- ( \(FuncTy _ concreteStructTy _) ->
176
- -- let mappings = unifySignatures originalStructTy concreteStructTy
177
- -- correctedMembers = replaceGenericTypeSymbolsOnMembers mappings membersXObjs
178
- -- in tokensForInit allocationMode (show originalStructTy) correctedMembers
179
- -- )
180
- -- ( \(FuncTy _ concreteStructTy _) ->
181
- -- case concretizeType typeEnv env concreteStructTy of
182
- -- Left _ -> []
183
- -- Right ok -> ok
184
- -- )
185
- -- genericInit _ _ _ _ = error "genericinit"
0 commit comments