-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathStaticArrayTemplates.hs
159 lines (150 loc) · 6.09 KB
/
StaticArrayTemplates.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
module StaticArrayTemplates where
import qualified ArrayTemplates
import Concretize
import Obj
import Template
import ToTemplate
import Types
-- | NOTE: The code for these templates is copied from ArrayTemplates.hs but
-- since there are some small differences here and there I'v decided to not
-- try to abstract over them and just duplicate the templates instead.
concreteArray :: Ty
concreteArray = ConcreteNameTy (SymPath [] "StaticArray")
templateUnsafeNth :: (String, Binder)
templateUnsafeNth =
let t = VarTy "t"
in defineTemplate
(SymPath ["StaticArray"] "unsafe-nth")
(FuncTy [RefTy (StructTy concreteArray [t]) (VarTy "q"), IntTy] (RefTy t (VarTy "q")) StaticLifetimeTy)
"gets a reference to the `n`th element from a static array `a`."
(toTemplate "$t* $NAME (Array *aRef, int n)")
( toTemplate $
unlines
[ "$DECL {",
" Array a = *aRef;",
" assert(n >= 0);",
" assert(n < a.len);",
" return &((($t*)a.data)[n]);",
"}"
]
)
( \(FuncTy [RefTy _ _, _] _ _) ->
[]
)
templateLength :: (String, Binder)
templateLength = defineTypeParameterizedTemplate templateCreator path t docs
where
path = SymPath ["StaticArray"] "length"
t = FuncTy [RefTy (StructTy concreteArray [VarTy "t"]) (VarTy "q")] IntTy StaticLifetimeTy
docs = "gets the length of the static array."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "int $NAME (Array *a)"))
(const (toTemplate "$DECL { return (*a).len; }"))
( \(FuncTy [RefTy arrayType _] _ _) ->
depsForDeleteFunc typeEnv env arrayType
)
templateDeleteArray :: (String, Binder)
templateDeleteArray = defineTypeParameterizedTemplate templateCreator path t docs
where
path = SymPath ["StaticArray"] "delete"
t = FuncTy [StructTy concreteArray [VarTy "a"]] UnitTy StaticLifetimeTy
docs = "deletes a static array. This function should not be called manually (there shouldn't be a way to create value types of type StaticArray)."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "void $NAME (Array a)"))
( \(FuncTy [arrayType] UnitTy _) ->
[TokDecl, TokC "{\n"]
++ deleteTy typeEnv env arrayType
++ [TokC "}\n"]
)
( \(FuncTy [StructTy _ [insideType]] UnitTy _) ->
depsForDeleteFunc typeEnv env insideType
)
deleteTy :: TypeEnv -> Env -> Ty -> [Token]
deleteTy typeEnv env (StructTy _ [innerType]) =
[ TokC " for(int i = 0; i < a.len; i++) {\n",
TokC $ " " ++ ArrayTemplates.insideArrayDeletion typeEnv env innerType "i",
TokC " }\n"
]
deleteTy _ _ _ = []
templateAsetBang :: (String, Binder)
templateAsetBang = defineTypeParameterizedTemplate templateCreator path t docs
where
path = SymPath ["StaticArray"] "aset!"
t = FuncTy [RefTy (StructTy concreteArray [VarTy "t"]) (VarTy "q"), IntTy, VarTy "t"] UnitTy StaticLifetimeTy
docs = "sets a static array element at the index `n` to a new value in place."
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "void $NAME (Array *aRef, int n, $t newValue)"))
( \(FuncTy [_, _, insideTy] _ _) ->
let deleter = ArrayTemplates.insideArrayDeletion typeEnv env insideTy
in ( toTemplate $
unlines
[ "$DECL {",
" Array a = *aRef;",
" assert(n >= 0);",
" assert(n < a.len);",
deleter "n",
" (($t*)a.data)[n] = newValue;",
"}"
]
)
)
( \(FuncTy [RefTy arrayType _, _, _] _ _) ->
depsForDeleteFunc typeEnv env arrayType
)
templateAsetUninitializedBang :: (String, Binder)
templateAsetUninitializedBang = defineTypeParameterizedTemplate templateCreator path t docs
where
path = SymPath ["StaticArray"] "aset-uninitialized!"
t = FuncTy [RefTy (StructTy (ConcreteNameTy (SymPath [] "StaticArray")) [VarTy "t"]) (VarTy "q"), IntTy, VarTy "t"] UnitTy StaticLifetimeTy
docs = "sets an uninitialized static array member. The old member will not be deleted."
templateCreator = TemplateCreator $
\_ _ ->
Template
t
( \(FuncTy [_, _, valueType] _ _) ->
case valueType of
UnitTy -> toTemplate "void $NAME (Array *aRef, int n)"
_ -> toTemplate "void $NAME (Array *aRef, int n, $t newValue)"
)
( \(FuncTy [_, _, valueType] _ _) ->
case valueType of
UnitTy -> ArrayTemplates.unitSetterTemplate
_ ->
multilineTemplate
[ "$DECL {",
" Array a = *aRef;",
" assert(n >= 0);",
" assert(n < a.len);",
" (($t*)a.data)[n] = newValue;",
"}"
]
)
(const [])
templateStrArray :: (String, Binder)
templateStrArray = defineTypeParameterizedTemplate templateCreator path t docs
where
templateCreator = TemplateCreator $
\typeEnv env ->
Template
t
(const (toTemplate "String $NAME (Array* a)"))
( \(FuncTy [RefTy arrayType _] StringTy _) ->
[TokDecl, TokC " {\n"]
++ ArrayTemplates.strTy typeEnv env arrayType
++ [TokC "}\n"]
)
( \(FuncTy [RefTy (StructTy _ [insideType]) _] StringTy _) ->
depsForPrnFunc typeEnv env insideType
)
path = SymPath ["StaticArray"] "str"
t = FuncTy [RefTy (StructTy concreteArray [VarTy "a"]) (VarTy "q")] StringTy StaticLifetimeTy
docs = "converts a static array to a string."