forked from carp-lang/Carp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxTemplates.hs
272 lines (262 loc) · 10.1 KB
/
BoxTemplates.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
-- | Module BoxTemplates defines Carp's Box type, a container for managed,
-- heap allocated objects.
module BoxTemplates
( delete,
str,
prn,
BoxTemplates.init,
copy,
unbox,
peek,
)
where
import Concretize
import Obj
import Polymorphism
import Template
import ToTemplate
import Types
boxTy :: Ty
boxTy = StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]
-- | Defines a template for initializing Boxes.
init :: (String, Binder)
init =
let path = SymPath ["Box"] "init"
t = FuncTy [(VarTy "t")] boxTy StaticLifetimeTy
docs = "Initializes a box pointing to value t."
decl = templateLiteral "$t* $NAME ($t t)"
body =
const
( multilineTemplate
[ "$DECL {",
" $t* instance;",
" instance = CARP_MALLOC(sizeof($t));",
" *instance = t;",
" return instance;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
-- | Defines a template for converting a boxed value to a local value.
unbox :: (String, Binder)
unbox =
let path = SymPath ["Box"] "unbox"
t = FuncTy [(StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")])] (VarTy "t") StaticLifetimeTy
docs = "Converts a boxed value to a reference to the value and delete the box."
decl = templateLiteral "$t $NAME($t* box)"
body =
const
( multilineTemplate
[ "$DECL {",
" $t local;",
" local = *box;",
" CARP_FREE(box);",
" return local;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
-- | Defines a template for getting a reference to the value stored in a box without performing an additional allocation.
peek :: (String, Binder)
peek =
let path = SymPath ["Box"] "peek"
t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) (VarTy "q"))] (RefTy (VarTy "t") (VarTy "q")) StaticLifetimeTy
docs = "Returns a reference to the value stored in a box without performing an additional allocation."
decl = templateLiteral "$t* $NAME($t** box_ref)"
body =
const
( multilineTemplate
[ "$DECL {",
" return *box_ref;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
-- | Defines a template for copying a box. The copy will also be heap allocated.
copy :: (String, Binder)
copy =
let path = SymPath ["Box"] "copy"
t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) (VarTy "q"))] (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) StaticLifetimeTy
docs = "Copies a box."
decl = (templateLiteral "$t* $NAME ($t** box)")
template = TemplateCreator $
\tenv env ->
Template
t
decl
( \ft ->
case ft of
(FuncTy [RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] _ _) ->
innerCopy tenv env inner
_ -> error "box templates: copy called with non box"
)
( \ft ->
case ft of
(FuncTy [RefTy boxType@(StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] _ _) ->
depsForCopyFunc tenv env inner
++ depsForDeleteFunc tenv env boxType
_ -> error "box templates: copy called with non box"
)
in defineTypeParameterizedTemplate template path t docs
where
innerCopy typeEnv valEnv innerTy =
case findFunctionForMemberIncludePrimitives typeEnv valEnv "copy" (typesCopyFunctionType innerTy) ("Inside box.", innerTy) of
FunctionFound functionFullName ->
multilineTemplate
[ "$DECL {",
" $t* copy;",
" copy = CARP_MALLOC(sizeof($t));",
" *copy = " ++ functionFullName ++ "(*box);",
" return copy;",
"}"
]
_ ->
multilineTemplate
[ "$DECL {",
" $t* copy;",
" copy = CARP_MALLOC(sizeof($t));",
" *copy = *box;",
" return copy;",
"}"
]
-- | Defines a template for deleting a box.
delete :: (String, Binder)
delete =
let path = SymPath ["Box"] "delete"
t = FuncTy [boxTy] UnitTy StaticLifetimeTy
docs = "Deletes a box, freeing its associated memory."
decl = (templateLiteral "void $NAME ($t* box)")
templateCreator = TemplateCreator $
\tenv env ->
Template
t
decl
( \ft ->
case ft of
(FuncTy [bTy] UnitTy _) ->
multilineTemplate
[ "$DECL {",
" " ++ innerDelete tenv env bTy,
"}"
]
_ -> error "box templates: delete called with non box"
)
( \ft ->
case ft of
(FuncTy [StructTy (ConcreteNameTy (SymPath [] "Box")) [insideType]] UnitTy _) ->
depsForDeleteFunc tenv env insideType
_ -> error "box templates: delete called with non box"
)
in defineTypeParameterizedTemplate templateCreator path t docs
where
innerDelete :: TypeEnv -> Env -> Ty -> String
innerDelete tenv env (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) =
case findFunctionForMember tenv env "delete" (typesDeleterFunctionType inner) ("Inside box.", inner) of
FunctionFound functionFullName ->
" " ++ functionFullName ++ "(*box);\n"
++ " CARP_FREE(box);"
FunctionNotFound msg -> error msg
FunctionIgnored ->
" /* Ignore non-managed type inside Box: '" ++ show inner ++ "' */\n"
++ " CARP_FREE(box);"
innerDelete _ _ _ = ""
-- | Defines a template for printing a box as a string.
prn :: (String, Binder)
prn =
let path = SymPath ["Box"] "prn"
t = FuncTy [boxTy] StringTy StaticLifetimeTy
docs = "Returns a string representation of a Box."
decl = templateLiteral "String $NAME ($t* box)"
templateCreator =
TemplateCreator $
( \tenv env ->
Template
t
decl
( \ft ->
case ft of
(FuncTy [boxT] StringTy _) ->
multilineTemplate
[ "$DECL {",
" if(!box){",
" String buffer = CARP_MALLOC(4);",
" sprintf(buffer, \"Nil\");",
" return buffer;",
" }",
innerStr tenv env boxT,
" return buffer;",
"}"
]
_ -> error "box templates: prn called with non box"
)
( \ft ->
case ft of
(FuncTy [(StructTy (ConcreteNameTy (SymPath [] "Box")) [inner])] StringTy _) ->
depsForPrnFunc tenv env inner
_ -> error "box tempaltes: prn called with non box"
)
)
in defineTypeParameterizedTemplate templateCreator path t docs
-- | Defines a template for printing a reference to a box as a string.
str :: (String, Binder)
str =
let path = SymPath ["Box"] "str"
t = FuncTy [(RefTy boxTy (VarTy "q"))] StringTy StaticLifetimeTy
docs = "Returns a string representation of a Box."
templateCreator =
TemplateCreator $
( \tenv env ->
Template
t
(templateLiteral "String $NAME ($t** box)")
( \ft ->
case ft of
(FuncTy [RefTy boxT _] StringTy _) ->
multilineTemplate
[ "$DECL {",
" if(!box){",
" String buffer = CARP_MALLOC(4);",
" sprintf(buffer, \"Nil\");",
" return buffer;",
" }",
innerStr tenv env boxT,
" return buffer;",
"}"
]
_ -> error "box templates: str called with non box ref"
)
( \ft ->
case ft of
(FuncTy [RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] StringTy _) ->
depsForPrnFunc tenv env inner
_ -> error "box templates: str template called with non box type"
)
)
in defineTypeParameterizedTemplate templateCreator path t docs
innerStr :: TypeEnv -> Env -> Ty -> String
innerStr tenv env (StructTy _ [t]) =
case findFunctionForMemberIncludePrimitives tenv env "prn" (typesStrFunctionType tenv env (RefTy t StaticLifetimeTy)) ("Inside box.", t) of
FunctionFound functionFullName ->
unlines
[ " char* temp = " ++ functionFullName ++ "(*box);",
" int size = snprintf(NULL, 0, \"(Box %s)\", temp);",
" String buffer = CARP_MALLOC(size);",
" sprintf(buffer, \"(Box %s)\", temp);",
" if(temp) {",
" CARP_FREE(temp);",
" temp = NULL;",
" }"
]
FunctionNotFound _ ->
unlines
[ " String buffer = CARP_MALLOC(14);",
" sprintf(buffer, \"(Box unknown)\");"
]
FunctionIgnored -> " /* Ignore type inside Box: '" ++ show t ++ "' ??? */\n"
innerStr _ _ _ = ""