-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgccutils.lua
190 lines (177 loc) · 4.53 KB
/
gccutils.lua
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
local gccutils = {}
local function chain_next(node, itnode)
if itnode == nil then
return node
end
return itnode:chain()
end
function gccutils.chain(node)
return chain_next, node, nil
end
function gccutils.get_id(node)
if not node then return nil end
if node:code() == 'identifier_node' then
return node:value()
else
return gccutils.get_id(node:name())
end
end
function gccutils.get_inner_id(node)
if not node then return nil end
if node:code() == 'identifier_node' then
return node:value()
else
local subnode = node:name()
if not subnode and node.type then
subnode = node:type()
end
return gccutils.get_inner_id(subnode)
end
end
function gccutils.has_cvarargs(functype)
local arg = functype:args()
if not arg then
return false
end
while true do
if not arg then
return true
elseif arg:value():code() == 'void_type' then
return false
end
arg = arg:chain()
end
end
local ctype2nltype = {
['char'] = 'cchar',
['signed char'] = 'cschar',
['short int'] = 'cshort',
['int'] = 'cint',
['long int'] = 'clong',
['long long int'] = 'clonglong',
['ptrdiff_t'] = 'cptrdiff',
['unsigned char'] = 'cuchar',
['short unsigned int'] = 'cushort',
['unsigned int'] = 'cuint',
['long unsigned int'] = 'culong',
['long long unsigned int'] = 'culonglong',
['size_t'] = 'csize',
['long double'] = 'clongdouble',
['intptr_t'] = 'isize',
['int8_t'] = 'int8',
['int16_t'] = 'int16',
['int32_t'] = 'int32',
['int64_t'] = 'int64',
['__int128'] = 'int128',
['uintptr_t'] = 'usize',
['uint8_t'] = 'uint8',
['uint16_t'] = 'uint16',
['uint32_t'] = 'uint32',
['uint64_t'] = 'uint64',
-- ['unsigned __int128'] = 'uint128',
['__int128 unsigned'] = 'uint128',
['float'] = 'float32',
['double'] = 'float64',
['_Float128x'] = 'float128',
['_Float128'] = 'float128',
['_Float64x'] = 'float64',
['_Float64'] = 'float64',
['_Float32x'] = 'float32',
['_Float32'] = 'float32',
['bool'] = 'boolean',
['void'] = 'void',
['clock_t'] = 'cclock_t',
['__clock_t'] = 'cclock_t',
['time_t'] = 'ctime_t',
['__time_t'] = 'ctime_t',
['wchar_t'] = 'cwchar_t',
['__wchar_t'] = 'cwchar_t',
['WCHAR'] = 'cwchar_t',
}
function gccutils.is_primitive_name(name)
return ctype2nltype[name] ~= nil
end
function gccutils.node_ctype2nltype(node)
if not node then return end
local ctype = gccutils.get_id(node)
local nltype = ctype2nltype[ctype]
if not nltype then -- try with canonical name
local canonctype = gccutils.get_id(node:canonical())
nltype = ctype2nltype[canonctype]
-- try to detect fixed types using the name
if nltype and node:code() == 'integer_type' and nltype:match('^c') then
ctype = ctype:lower()
if nltype:match('^cu') then
if ctype:match('int64') then nltype = 'uint64'
elseif ctype:match('int32') then nltype = 'uint32'
elseif ctype:match('int16') then nltype = 'uint16'
elseif ctype:match('int8') then nltype = 'uint8'
elseif ctype:match('intptr') then nltype = 'usize'
end
else
if ctype:match('int64') then nltype = 'int64'
elseif ctype:match('int32') then nltype = 'int32'
elseif ctype:match('int16') then nltype = 'int16'
elseif ctype:match('int8') then nltype = 'int8'
elseif ctype:match('intptr') or ctype:match('sizeiptr') then nltype = 'isize'
end
end
end
end
return nltype
end
function gccutils.get_enum_nltype(node)
local negative
local large
for fieldnode in gccutils.chain(node:values()) do
local fieldvalue = fieldnode:value():value()
if fieldvalue >= 2147483648 then
large = true
elseif fieldvalue < 0 then
negative = true
end
end
if not negative and large then
return 'cuint'
else
return 'cint'
end
end
local reserved_names = {
["and"] = true,
["break"] = true,
["do"] = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["for"] = true,
["false"] = true,
["function"] = true,
["goto"] = true,
["if"] = true,
["in"] = true,
["local"] = true,
["nil"] = true,
["not"] = true,
["or"] = true,
["repeat"] = true,
["return"] = true,
["then"] = true,
["true"] = true,
["until"] = true,
["while"] = true,
-- nelua additional keywords
["case"] = true,
["continue"] = true,
["defer"] = true,
["global"] = true,
["switch"] = true,
["nilptr"] = true,
}
function gccutils.normalize_name(name)
if reserved_names[name] then
return name:sub(1,1):upper()..name:sub(2)
end
return name
end
return gccutils