-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast-type.lua
More file actions
719 lines (654 loc) · 16.7 KB
/
Copy pathast-type.lua
File metadata and controls
719 lines (654 loc) · 16.7 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
local function deepcopy(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
Type = Class("Type", {
intmap = {
["char"] = {
["unsigned"] = "uint8";
["signed"] = "int8";
"signed";
};
["short"] = {
["int"] = {
["unsigned"] = "uint16";
["signed"] = "int16";
"signed";
};
["unsigned"] = {
["int"] = "uint16";
"int";
};
["signed"] = {
["int"] = "int16";
"int";
};
"signed";
};
["int"] = {
["short"] = {
["unsigned"] = "uint16";
["signed"] = "int16";
"signed";
};
["long"] = {
["unsigned"] = "uint64";
["signed"] = "int64";
"signed";
};
["unsigned"] = {
["short"] = "uint16";
["long"] = "uint64";
["@"] = "uint32";
"@";
};
["signed"] = {
["short"] = "int16";
["long"] = "int64";
["@"] = "int32";
"@";
};
"signed";
};
["long"] = {
["int"] = {
["unsigned"] = "uint64";
["signed"] = "int64";
"signed";
};
["unsigned"] = {
["int"] = "uint64";
"int";
};
["signed"] = {
["int"] = "int64";
"int";
};
"signed";
};
["unsigned"] = {
["char"] = "uint8";
["int"] = {
["short"] = "uint16";
["@"] = "uint32";
["long"] = "uint64";
"@"
};
["short"] = {
["int"] = "uint16";
"int";
};
"int";
};
["signed"] = {
["char"] = "int8";
["int"] = {
["short"] = "int16";
["@"] = "int32";
["long"] = "int64";
"@"
};
["short"] = {
["int"] = "int16";
"int";
};
"int";
};
["uint8"] = "uint8";
["uint16"] = "uint16";
["uint32"] = "uint32";
["uint64"] = "uint64";
["int8"] = "int8";
["int16"] = "int16";
["int32"] = "int32";
["int64"] = "int64";
},
fpnmap = {
["float"] = "float";
["double"] = "double";
},
abstract_decl = function(self, env, tree)
if tree.adecl then
self.abs = true
local ad = tree.adecl
if ad.pointer then
--dump(ad, true)
local ptr = {}
self.pointer = ptr
for j, p in ipairs(ad.pointer) do
local quals = {}
--dump(ad.pointer, true)
if p.qual then
for k, q in ipairs(p.qual) do
local n = q.value
if n == "const" then
quals.const = true
elseif n == "volatile" then
quals.volatile = true
elseif n == "restrict" then
quals.restrict = true
else
dump(self, true)
tassert(env.loc[self], false, "AST/Type not reached")
end
end
end
ptr[#ptr+1] = quals
end
end
if ad.array then
--dump(ad, true)
local arr = {}
self.array = arr
for j, a in ipairs(ad.array) do
local quals = {}
if a.qual then
for k, q in ipairs(a.qual) do
local n = q.value
if n == "const" then
quals.const = true
else
tassert(env.loc[self], false,
"qualifier '%s' is not valid in this context", n)
end
end
end
if a.static then
quals.static = true
end
local s = a.expr
if not s then
quals.flexible = true
else
local expr = tassert(env.loc[self], s, "AST/Type no expression")
if expr:is_constant() or env.kind == "block" then
quals.size = expr
elseif expr:is_symbol() then
local sym = tassert(env.loc[self], expr:symbol_eval(), "not a symbol?")
quals.size = expr
quals.ref = sym.ref
quals.vla = true
else
tassert(env.loc[expr], false,
"invalid expression '%s' in this context", expr:repr(""))
end
end
arr[#arr+1] = quals
end
end
end
end,
repr = function(self, indent)
local ret = ""
if self.complete then
local first = true
for k, v in pairs(self.qual) do
if not first then
ret = ret .. " " .. k
else
ret = ret .. k
first = false
end
end
if not first then
ret = ret .. " "
end
if self.reg == "s" then
ret = ret .. self.struct:repr(indent)
elseif self.reg == "e" then
ret = ret .. self.enum:repr(indent)
elseif self.reg == "x" then
ret = ret .. self.fixed
elseif self.reg == "r" then
ret = ret .. self.id
elseif self.reg == "v" then
ret = ret .. "void"
elseif self.reg == "b" then
ret = ret .. "bool"
elseif self.reg == "c" then
local tab = {}
for k, v in ipairs(self.list) do
tab[#tab+1] = v:repr(indent)
end
ret = ret .. "[" .. self.rctype:repr(indent) .. " : " .. concat(tab, ", ") .. "]"
elseif self.reg == "i" then
ret = ret .. "interface " .. self.id
end
if self.abs then
if self.pointer then
local tab = {}
for k, v in ipairs(self.pointer) do
if next(v) then
local qt = {}
for q, b in pairs(v) do
qt[#qt+1] = q
end
tab[#tab+1] = "* " .. concat(qt, " ")
else
tab[#tab+1] = "*"
end
end
ret = ret .. " " .. concat(tab, " ")
end
if self.array then
local tab = {}
for k, v in ipairs(self.array) do
local prt = {}
if v.static then
prt[#prt+1] = "static"
end
if v.const then
prt[#prt+1] = "const"
end
if not v.flexible then
prt[#prt+1] = v.size:repr(indent)
end
ret = ret .. "[" .. concat(prt, " ") .. "]"
end
--dump(self.array, true)
end
end
else
dump(self, true)
ret = ret .. "<incomplete type>"
end
return ret
end,
compare = function(a, b)
--dump({a,b}, true, nil, nil, "inref")
while a.reg == "r" do
a = a.ref.ctype
end
while b.reg == "r" do
b = b.ref.ctype
end
--dump({a,b}, true, nil, nil, "outref")
--dump({a,b}, nil, nil, nil, "outref")
tassert(nil, a.complete and b.complete, "cannot compare incomplete types")
if a.abs ~= b.abs then return false end
if not deepcompare(a.qual, b.qual) then return false end
if a.reg ~= b.reg then return false end
if a.reg == "x" or a.reg == "f" then
if a.fixed ~= b.fixed then return false end
end
if a.abs then
if not deepcompare(a.pointer, b.pointer) then return false end
if not deepcompare(a.array, b.array) then return false end
end
if a.reg ~= b.reg then return false end
return true
end,
is_integer = function(self)
while self.reg == "r" do
self = self.ref.ctype
end
return self.reg == "x" and self.intsmap[self.rex.fixed] ~= nil
end,
get_struct = function(self, env)
while self.reg == "r" do
self = self.ref.ctype
end
if self.reg == "s" then
return self.struct
elseif self.reg == "i" then
return tassert(nil, env:iface_get_r(self.id), "no interface '%s' in this scope", self.id).struct
end
end,
get_enum = function(self, env)
while self.reg == "r" do
self = self.ref.ctype
end
if self.reg == "e" then
return self.enum
end
end,
get_function = function(self, env)
while self.reg == "r" do
self = self.ref.ctype
end
--dump(self.rctype)
return self
end,
get_arith_type = function(T, a, b)
if a.reg == "x" and b.reg == "x" then
--print(a.fixed, "&", b.fixed)
local n = T.arithmap[a.fixed][b.fixed]
if a.fixed == n then
return a
else
return b
end
elseif a.reg == "e" and b.reg == "e" then -- TODO FSA
return T.int_res
elseif a.reg == "x" and b.reg == "e" then
return T.int_res
elseif a.reg == "e" and b.reg == "x" then
return T.int_res
end
print(a.reg, a:repr(""), b.reg, b:repr(""))
end,
to_pointer = function(self)
local t = deepcopy(self)
t.abs = true
if not t.pointer then
t.pointer = {{}}
else
t.pointer[t.pointer+1] = {}
end
return t
end,
strip_pointer = function(self)
local t = deepcopy(self)
tassert(nil, t.abs and t.pointer, "not a pointer")
if #t.pointer > 1 then
remove(t.pointer, #t.pointer)
else
t.pointer = nil
end
return t
end,
strip_dep = function(self)
if self.abs then
if self.array then
local t = deepcopy(self)
for k, a in ipairs(t.array) do
if a.vla then
a.size = nil
a.ref = nil
a.vla = nil
a.flexible = true
end
end
end
end
return self
end,
},
-----------------------------------------------
-----------------------------------------------
-----------------------------------------------
function(T, env, tree)
--dump(tree, nil, nil, nil, "ctype")
local self = mktab(env, tree, {qual = {}}, T)
if tree.functype then
self.reg = "c"
self.rctype = tree.functype.ret
local list = {}
for k, v in ipairs(tree.functype.list) do
list[#list+1] = v
end
self.list = list
self.complete = true
elseif tree.iface then
self.reg = "i"
self.id = tree.iface.value
self.cid = tassert(nil, env:ns_get_iface(self.id), "AST/Type no c id")
self.complete = true
else
for v, q in ipairs(tree.sqlist.qual) do
if q["@tag"] == "token" then
local v = q.value
if v == "const" then
tassert(q, not self.qual.volatile, "const vs. volatile")
self.qual.const = true
elseif v == "volatile" then
tassert(q, not self.qual.const, "volatile vs. const")
self.qual.volatile = true
elseif v == "restrict" then
tassert(q, false, "restrict is only valid for pointers")
else
dump(self, true)
tassert(q, false, "AST/Type NIY")
end
else
dump(self, true)
tassert(env.loc[self], false, "AST/Type not reached")
end
end
local intmap, intp, int = T.intmap, false, nil
local fpnmap, fpnp, fpn = T.fpnmap, false, nil
for k, s in ipairs(tree.sqlist.spec) do
if s["@tag"] == "token" then
local v = s.value
tassert(s, type(intmap) ~= "string",
"attempted to add specifier '%s' to complete type '%s'", v, intmap)
if intp then
int = tassert(s, intmap[v], "unknown specifier '%s' in this context", v)
intmap = int
else
int = intmap[v]
if int then
tassert(s, k == 1, "unexpected '%s'", v)
intp = true
intmap = int
end
end
if fpnp then
fpn = tassert(s, fpnmap[v], "unknown specifier '%s' in this context", v)
fpnmap = fpn
else
fpn = fpnmap[v]
if fpn then
tassert(s, k == 1, "unexpected '%s'", v)
fpnp = true
fpnmap = fpn
end
end
if not int and not fpn then
tassert(s, #tree.sqlist.spec == 1, "more than one type in type name")
if v == "void" then
self.reg = "v"
self.complete = true
break
elseif v == "bool" then
self.reg = "b"
self.complete = true
break
else
--dump(tree)
self.reg = "r"
local ref = tassert(s, env:type_get_r(v), "reference to undefined type '%s'", v)
--dump(self.ref)
if v ~= "selftype" then
self.id = v
self.cid = tassert(nil, env:ns_get_type(v), "AST/Type no c id")
self.complete = true
self.ref = ref
else
self.reg = "i"
self.id = ref.id
self.cid = tassert(nil, ref.cid, "AST/Type no c id")
self.complete = true
end
break
end
end
elseif s["@tag"] == "Struct" then
tassert(s, #tree.sqlist.spec == 1, "more than one type in type name")
local struct = s
self.reg = "s"
self.struct = struct
self.complete = true
break
elseif s["@tag"] == "Enum" then
tassert(s, #tree.sqlist.spec == 1, "more than one type in type name")
local enum = s -- create a new type here
self.reg = "e"
self.enum = enum
self.complete = true
else
dump(tree)
tassert(nil, false, "AST/Type NIY")
end
end
if intp then
-- resolve type
while type(intmap) ~= "string" do
intmap = intmap[intmap[1]]
end
self.reg = "x"
self.fixed = intmap
self.complete = true
elseif fpnp then
-- resolve type
while type(fpnmap) ~= "string" do
fpnmap = fpnmap[fpnmap[1]]
end
self.reg = "f"
self.fixed = fpnmap
self.complete = true
end
end
T.abstract_decl(self, env, tree)
--dump(self, true, nil, nil, "Type")
return self
end)
Type.int_literal = setmetatable({
["complete"] = "true";
["reg"] = "x";
["fixed"] = "int64";
["qual"] = {
["const"] = "true";
};
}, Type)
Type.uint_literal = setmetatable({
["complete"] = "true";
["reg"] = "x";
["fixed"] = "uint64";
["qual"] = {
["const"] = "true";
};
}, Type)
Type.char_literal = setmetatable({
["complete"] = "true";
["reg"] = "x";
["fixed"] = "int8";
["qual"] = {
["const"] = "true";
};
}, Type)
Type.cstr_literal = setmetatable({
["pointer"] = {
[1] = {
["const"] = "true"; -- right?
};
};
["qual"] = {
["const"] = "true";
};
["int"] = {
["char"] = "true";
};
["complete"] = "true";
["abs"] = "true";
["reg"] = "x";
["complete"] = "true";
["reg"] = "x";
["fixed"] = "int8";
}, Type)
Type.bool_res = setmetatable({
["complete"] = "true";
["reg"] = "b";
["qual"] = {};
}, Type);
Type.int_res = setmetatable({
["complete"] = "true";
["reg"] = "x";
["fixed"] = "int64";
["qual"] = {};
}, Type)
Type.arithmap = {
["int8"] = {
["int8"] = "int8";
["int16"] = "int16";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "int8";
["uint16"] = "int16";
["uint32"] = "int32";
["uint64"] = "int64";
};
["int16"] = {
["int8"] = "int16";
["int16"] = "int16";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "int16";
["uint16"] = "int16";
["uint32"] = "int32";
["uint64"] = "int64";
};
["int32"] = {
["int8"] = "int32";
["int16"] = "int32";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "int32";
["uint16"] = "int32";
["uint32"] = "int32";
["uint64"] = "int64";
};
["int64"] = {
["int8"] = "int32";
["int16"] = "int32";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "int64";
["uint16"] = "int64";
["uint32"] = "int64";
["uint64"] = "int64";
};
["uint8"] = {
["int8"] = "int8";
["int16"] = "int16";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "uint8";
["uint16"] = "uint16";
["uint32"] = "uint32";
["uint64"] = "uint64";
};
["uint16"] = {
["int8"] = "int16";
["int16"] = "int16";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "uint16";
["uint16"] = "uint16";
["uint32"] = "uint32";
["uint64"] = "uint64";
};
["uint32"] = {
["int8"] = "int32";
["int16"] = "int32";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "uint32";
["uint16"] = "uint32";
["uint32"] = "uint32";
["uint64"] = "uint64";
};
["uint64"] = {
["int8"] = "int32";
["int16"] = "int32";
["int32"] = "int32";
["int64"] = "int64";
["uint8"] = "uint64";
["uint16"] = "uint64";
["uint32"] = "uint64";
["uint64"] = "uint64";
};
}