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
|
local eolian = require("eolian")
local keyref = require("docgen.keyref")
local dtree = require("docgen.doctree")
local M = {}
M.get_ctype_str = function(tp, suffix)
tp = tp or "void"
local ct = (type(tp) == "string") and tp or tp:c_type_get()
if not suffix then
return ct
end
if ct:sub(#ct) == "*" then
return ct .. suffix
else
return ct .. " " .. suffix
end
end
local wrap_type_attrs = function(tp, str)
if tp:is_const() then
str = "const(" .. str .. ")"
end
if tp:is_own() then
str = "own(" .. str .. ")"
end
local ffunc = tp:free_func_get()
if ffunc then
str = "free(" .. str .. ", " .. ffunc .. ")"
end
if tp:is_ref() then
str = "ref(" .. str .. ")"
end
return str
end
M.get_type_str = function(tp)
local tps = eolian.type_type
local tpt = tp:type_get()
if tpt == tps.UNKNOWN then
error("unknown type: " .. tp:full_name_get())
elseif tpt == tps.VOID then
return wrap_type_attrs(tp, "void")
elseif tpt == tps.UNDEFINED then
return wrap_type_attrs(tp, "__undefined_type")
elseif tpt == tps.REGULAR or tpt == tps.CLASS then
return wrap_type_attrs(tp, tp:full_name_get())
elseif tpt == tps.COMPLEX then
local stypes = {}
local stp = tp:base_type_get()
while stp do
stypes[#stypes + 1] = M.get_type_str(stp)
stp = stp:next_type_get()
end
return wrap_type_attrs(tp, tp:full_name_get() .. "<"
.. table.concat(stypes, ", ") .. ">")
elseif tpt == tps.POINTER then
local btp = tp:base_type_get()
local suffix = " *"
if btp:type_get() == tps.POINTER then
suffix = "*"
end
return wrap_type_attrs(tp, M.get_type_str(btp) .. suffix)
elseif tpt == tps.STATIC_ARRAY then
return wrap_type_attrs(tp, "static_array<"
.. M.get_type_str(tp:base_type_get()) .. ", "
.. tp:array_size_get() .. ">")
elseif tpt == tps.TERMINATED_ARRAY then
return wrap_type_attrs(tp, "terminated_array<"
.. M.get_type_str(tp:base_type_get()) .. ">")
end
error("unhandled type type: " .. tpt)
end
local add_typedecl_attrs = function(tp, buf)
if tp:is_extern() then
buf[#buf + 1] = "@extern "
end
local ffunc = tp:free_func_get()
if ffunc then
buf[#buf + 1] = "@free("
buf[#buf + 1] = ffunc
buf[#buf + 1] = ") "
end
end
M.get_typedecl_str = function(tp)
local tpt = tp:type_get()
if tpt == dtree.Typedecl.UNKNOWN then
error("unknown typedecl: " .. tp:full_name_get())
elseif tpt == dtree.Typedecl.STRUCT or
tpt == dtree.Typedecl.STRUCT_OPAQUE then
local buf = { "struct " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
if tpt == dtree.Typedecl.STRUCT_OPAQUE then
buf[#buf + 1] = ";"
return table.concat(buf)
end
local fields = tp:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = M.get_type_str(fld:type_get())
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ENUM then
local buf = { "enum " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
local fields = tp:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
local val = fld:value_get()
if val then
buf[#buf + 1] = ": "
buf[#buf + 1] = val:serialize()
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ALIAS then
local buf = { "type " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = M.get_type_str(tp:base_type_get())
buf[#buf + 1] = ";"
return table.concat(buf)
end
error("unhandled typedecl type: " .. tpt)
end
M.get_typedecl_cstr = function(tp)
local tpt = tp:type_get()
if tpt == dtree.Typedecl.UNKNOWN then
error("unknown typedecl: " .. tp:full_name_get())
elseif tpt == dtree.Typedecl.STRUCT or
tpt == dtree.Typedecl.STRUCT_OPAQUE then
local buf = { "typedef struct " }
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
buf[#buf + 1] = "_" .. fulln;
if tpt == dtree.Typedecl.STRUCT_OPAQUE then
buf[#buf + 1] = " " .. fulln .. ";"
return table.concat(buf)
end
local fields = tp:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = M.get_ctype_str(fld:type_get(), fld:name_get())
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ENUM then
local buf = { "typedef enum" }
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
local fields = tp:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
local cn = fld:c_name_get()
buf[#buf + 1] = cn
keyref.add(cn, "c")
local val = fld:value_get()
if val then
buf[#buf + 1] = " = "
local ev = val:eval(eolian.expression_mask.INT)
local lit = ev:to_literal()
buf[#buf + 1] = lit
local ser = val:serialize()
if ser and ser ~= lit then
buf[#buf + 1] = " /* " .. ser .. " */"
end
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ALIAS then
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
return "typedef " .. M.get_ctype_str(tp:base_type_get(), fulln) .. ";"
end
error("unhandled typedecl type: " .. tpt)
end
return M
|