-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast-struct.lua
More file actions
224 lines (196 loc) · 5.19 KB
/
Copy pathast-struct.lua
File metadata and controls
224 lines (196 loc) · 5.19 KB
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
StructDeclarator = Class("StructDeclarator", {},
function(S, env, ctype, tree)
--dump(tree, nil, nil, nil, "structdeclr")
local self = mktab(env, tree, {}, S)
if tree.width then
local expr = tree.width
local loc = env.loc[expr]
tassert(loc, expr:is_constant(), "not constant width of struct field")
self.width = expr
tassert(loc, ctype.reg == "x", "only integer widths allowed")
tassert(loc, not ctype.abs, "only integer widths allowed")
end
local loc = env.loc[self]
local v = tree.iden.value
self.id = v
self.ctype = ctype
return self
end)
StructDeclaration = Class("StructDeclaration", {
repr = function(self, indent)
if not self.anon then
local tab = {}
for k, v in ipairs(self) do
if v.width then
tab[#tab+1] = v.id .. " : " .. v.width:repr(indent)
else
tab[#tab+1] = v.id
end
end
if indent then
return indent .. self.ctype:repr(indent) .. " " .. concat(tab, ", ") .. ";\n"
else
return self.ctype:repr(indent) .. " " .. concat(tab, ", ") .. ";"
end
else
return indent .. self.anon:repr(indent) .. ";\n"
end
end,
},
function(S, env, tree)
--dump(tree, nil, nil, nil, "structdecl")
if tree.decl then
local self = mktab(env, tree, {}, S)
local ctype = tree.ctype
for k, v in ipairs(tree.decl) do
local dr = StructDeclarator( env, ctype, v)
self[#self+1] = dr
end
self.ctype = ctype
return self
elseif tree.anon then
if tree.ctype then
tassert(tree._m, tree.ctype.reg == "s", "AST/Struct, not a child")
local self = mktab(env, tree, {
anon = tree.ctype.struct
}, S)
return self
elseif tree.struct then
local self = mktab(env, tree, {
anon = tree.struct
}, S)
return self
end
else
dump(tree, true)
tassert(tree._m, false, "AST/StructDeclaration not reached")
end
end)
local function merge_scopes(self, scope)
scope = scope or self.scope
-- dump({self,scope})
for k, decl in ipairs(self) do
if decl.anon then
local chld = decl.anon
for id, f in pairs(chld.scope) do
if type(id) == "string" then
tassert(nil, not scope[id], "field %s already defined in this scope", id)
scope[id] = f
end
end
scope[0] = chld.scope[0] + scope[0]
chld.scope = scope
chld.id = "anonymous" .. (self.scope[0] - 1)
end
end
end
Struct = Class("Struct", {
repr = function(self, indent)
local tab = {}
if self.ref then
local ref = self.ref
if ref.union then
tab[#tab+1] = "union"
else
tab[#tab+1] = "struct"
end
tab[#tab+1] = " "
tab[#tab+1] = ref.id
else
if self.union then
tab[#tab+1] = "union"
else
tab[#tab+1] = "struct"
end
tab[#tab+1] = " "
if not self.anon then
tab[#tab+1] = self.id
tab[#tab+1] = " "
end
if indent then
tab[#tab+1] = "{\n"
do
local indent1 = indent .. " "
for k,v in ipairs(self) do
tab[#tab+1] = v:repr(indent1)
end
end
tab[#tab+1] = indent
tab[#tab+1] = "}"
else
-- not in source
end
end
--dump(tab)
return concat(tab, "")
end,
dereference = function(self)
if self.ref then
return self.ref
else
return self
end
end,
get_field = function(self, fid)
local e = self.scope[fid]
if e then return
e.field
end
end,
},
function(S, env, tree, id)
--dump(tree, nil, nil, nil, "struct")
if not tree then
local fwd = setmetatable({empty = true}, S)
tassert(nil, id, "no id in forward declaration")
env:struct_reg(id, fwd)
return fwd
elseif type(tree.struct) == "table" then
local id = tree.struct.value
local ref = tassert(tree.struct, env:struct_get_r(id),
"undefined struct '%s'", id)
local self = mktab(env, tree, {ref = ref}, S)
self.cid = tassert(nil, env:ns_get_struct(id), "AST/Struct no c id")
return self
else
local self = env:struct_get(id)
if self then
tassert(nil, self.empty, "attempt to redeclare struct")
else
self = mktab(env, tree, {}, S)
if id then
env:struct_reg(id, self)
end
end
self.empty = nil
self.scope = {[0] = 1}
if tree.union then
self.union = true
end
if id then
self.id = id
self.cid = tassert(nil, env:ns_get_struct(id), "AST/Struct no c id")
else
self.anon = true
end
for k, decl in ipairs(tree.decl) do
local scope = self.scope
tassert(nil, scope, "AST/Struct no scope")
if decl["@tag"] == "StructDeclaration" then
for k, dr in ipairs(decl) do
tassert(nil, not scope[dr.id], "field %s already defined in this scope", k)
scope[dr.id] = {field = dr, struct = struct}
end
self[#self+1] = decl
elseif decl.inherit then
self[#self+1] = decl
else
dump(tree, nil, nil, nil, "struct")
tassert(nil, false, "AST/Struct NIY")
end
end
merge_scopes(self)
--dump(self, true, nil, nil, "Struct")
return self
end
end)