Skip to content

Commit 74328ad

Browse files
committed
add expandSingle
1 parent b4fc4f2 commit 74328ad

File tree

3 files changed

+38
-55
lines changed

3 files changed

+38
-55
lines changed

script/plugins/ffi/c-parser/ctypes.lua

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local ctypes = { TESTMODE = false }
22

33
local inspect = require("inspect")
44
local utility = require 'utility'
5+
local util = require 'plugins.ffi.c-parser.util'
56
local typed = require("plugins.ffi.c-parser.typed")
67

78
local equal_declarations
@@ -102,6 +103,8 @@ local convert_value = typed("TypeList, table -> CType?, string?", function (lst,
102103
local idxs = nil
103104

104105
if type(src.id) == "table" or type(src.ids) == "table" then
106+
src.id = util.expandSingle(src.id)
107+
src.ids = util.expandSingle(src.ids)
105108
-- FIXME multiple ids, e.g.: int *x, y, *z;
106109
local ok
107110
ok, name, ret_pointer, idxs = get_name(src.id or src.ids)
@@ -122,22 +125,6 @@ local convert_value = typed("TypeList, table -> CType?, string?", function (lst,
122125
}), nil
123126
end)
124127

125-
local function convert_fields(lst, field_src, fields)
126-
if field_src.ids then
127-
for i, id in ipairs(field_src.ids) do
128-
id.type = utility.deepCopy(field_src.type)
129-
if id.type and id[1] then
130-
for i, v in ipairs(id[1]) do
131-
table.insert(id.type, v)
132-
end
133-
id[1] = nil
134-
end
135-
table.insert(fields, id)
136-
end
137-
return true
138-
end
139-
end
140-
141128
-- Interpret field data from `field_src` and add it to `fields`.
142129
local function add_to_fields(lst, field_src, fields)
143130
if type(field_src) == "table" and not field_src.ids then
@@ -149,9 +136,6 @@ local function add_to_fields(lst, field_src, fields)
149136
return true
150137
end
151138

152-
if convert_fields(lst, field_src, fields) then
153-
return true
154-
end
155139
local field, err = convert_value(lst, field_src)
156140
if not field then
157141
return nil, err
@@ -531,14 +515,6 @@ local function to_set(array)
531515
return set
532516
end
533517

534-
local function need_expand(t)
535-
if #t ~= 1 then
536-
return false
537-
end
538-
local tt = t[1].type
539-
return tt == 'struct' or tt == 'union' or tt == 'enum'
540-
end
541-
542518
ctypes.register_types = typed("{Decl} -> TypeList?, string?", function (parsed)
543519
local lst = typed.table("TypeList", {})
544520
for _, item in ipairs(parsed) do
@@ -565,9 +541,7 @@ ctypes.register_types = typed("{Decl} -> TypeList?, string?", function (parsed)
565541
return nil, err or "failed typedef"
566542
end
567543
else
568-
if not item.spec.type and need_expand(item.spec) then
569-
item.spec = item.spec[1]
570-
end
544+
item.spec = util.expandSingle(item.spec)
571545
if item.spec.type == "struct" or item.spec.type == "union" then
572546
local ok, err = register_structunion(lst, item)
573547
if not ok then
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local m = {}
2+
3+
local function tableLenEqual(t, len)
4+
for key, value in pairs(t) do
5+
len = len - 1
6+
if len < 0 then
7+
return false
8+
end
9+
end
10+
return true
11+
end
12+
13+
local function isSingleNode(ast)
14+
if type(ast) ~= 'table' then
15+
return false
16+
end
17+
local len = #ast
18+
return len == 1 and tableLenEqual(ast, len)
19+
end
20+
21+
function m.expandSingle(ast)
22+
if isSingleNode(ast) then
23+
return ast[1]
24+
end
25+
return ast
26+
end
27+
28+
return m

script/plugins/ffi/init.lua

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
local searchCode = require 'plugins.ffi.searchCode'
22
local cdefRerence = require 'plugins.ffi.cdefRerence'
33
local cdriver = require 'plugins.ffi.c-parser.cdriver'
4-
local util = require 'utility'
4+
local util = require 'plugins.ffi.c-parser.util'
5+
local utility = require 'utility'
56
local SDBMHash = require 'SDBMHash'
67
local ws = require 'workspace'
78
local files = require 'files'
@@ -12,22 +13,6 @@ local scope = require 'workspace.scope'
1213

1314
local namespace <const> = 'ffi.namespace*.'
1415

15-
local function nkeys(t)
16-
local n = 0
17-
for key, value in pairs(t) do
18-
n = n + 1
19-
end
20-
return n
21-
end
22-
23-
local function isSingleNode(ast)
24-
if type(ast) ~= 'table' then
25-
return false
26-
end
27-
local len = #ast
28-
return len == 1 and len == nkeys(ast)
29-
end
30-
3116
--TODO:supprot 32bit ffi, need config
3217
local knownTypes = {
3318
["bool"] = 'boolean',
@@ -81,7 +66,7 @@ local knownTypes = {
8166
local constName <const> = 'm'
8267

8368
---@class ffi.builder
84-
local builder = { switch_ast = util.switch() }
69+
local builder = { switch_ast = utility.switch() }
8570

8671
function builder:getTypeAst(name)
8772
for i, asts in ipairs(self.globalAsts) do
@@ -216,9 +201,7 @@ do
216201
if ops[val.op] then
217202
return binop(enumer, val, ops[val.op])
218203
end
219-
if isSingleNode(val) then
220-
val = val[1]
221-
end
204+
val = util.expandSingle(val)
222205
if type(val) == "string" then
223206
if enumer[val] then
224207
return enumer[val]
@@ -230,9 +213,7 @@ do
230213
end
231214

232215
local function pushEnumValue(enumer, name, v)
233-
if isSingleNode(v) then
234-
v = tonumber(v[1])
235-
end
216+
v = tonumber(util.expandSingle(v))
236217
enumer[name] = v
237218
enumer[#enumer+1] = v
238219
return v
@@ -359,7 +340,7 @@ function m.initBuilder(fileDir)
359340
local encoding = config.get(nil, 'Lua.runtime.fileEncoding')
360341
local filePath = fileDir / table.concat({ hash, encoding }, '_')
361342

362-
util.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
343+
utility.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
363344
end
364345
end
365346

0 commit comments

Comments
 (0)