summaryrefslogtreecommitdiff
path: root/script/plugins/ffi
diff options
context:
space:
mode:
Diffstat (limited to 'script/plugins/ffi')
-rw-r--r--script/plugins/ffi/c-parser/ctypes.lua34
-rw-r--r--script/plugins/ffi/c-parser/util.lua28
-rw-r--r--script/plugins/ffi/init.lua31
3 files changed, 38 insertions, 55 deletions
diff --git a/script/plugins/ffi/c-parser/ctypes.lua b/script/plugins/ffi/c-parser/ctypes.lua
index 72d19ab9..ec8294c8 100644
--- a/script/plugins/ffi/c-parser/ctypes.lua
+++ b/script/plugins/ffi/c-parser/ctypes.lua
@@ -2,6 +2,7 @@ local ctypes = { TESTMODE = false }
local inspect = require("inspect")
local utility = require 'utility'
+local util = require 'plugins.ffi.c-parser.util'
local typed = require("plugins.ffi.c-parser.typed")
local equal_declarations
@@ -102,6 +103,8 @@ local convert_value = typed("TypeList, table -> CType?, string?", function (lst,
local idxs = nil
if type(src.id) == "table" or type(src.ids) == "table" then
+ src.id = util.expandSingle(src.id)
+ src.ids = util.expandSingle(src.ids)
-- FIXME multiple ids, e.g.: int *x, y, *z;
local ok
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,
}), nil
end)
-local function convert_fields(lst, field_src, fields)
- if field_src.ids then
- for i, id in ipairs(field_src.ids) do
- id.type = utility.deepCopy(field_src.type)
- if id.type and id[1] then
- for i, v in ipairs(id[1]) do
- table.insert(id.type, v)
- end
- id[1] = nil
- end
- table.insert(fields, id)
- end
- return true
- end
-end
-
-- Interpret field data from `field_src` and add it to `fields`.
local function add_to_fields(lst, field_src, fields)
if type(field_src) == "table" and not field_src.ids then
@@ -149,9 +136,6 @@ local function add_to_fields(lst, field_src, fields)
return true
end
- if convert_fields(lst, field_src, fields) then
- return true
- end
local field, err = convert_value(lst, field_src)
if not field then
return nil, err
@@ -531,14 +515,6 @@ local function to_set(array)
return set
end
-local function need_expand(t)
- if #t ~= 1 then
- return false
- end
- local tt = t[1].type
- return tt == 'struct' or tt == 'union' or tt == 'enum'
-end
-
ctypes.register_types = typed("{Decl} -> TypeList?, string?", function (parsed)
local lst = typed.table("TypeList", {})
for _, item in ipairs(parsed) do
@@ -565,9 +541,7 @@ ctypes.register_types = typed("{Decl} -> TypeList?, string?", function (parsed)
return nil, err or "failed typedef"
end
else
- if not item.spec.type and need_expand(item.spec) then
- item.spec = item.spec[1]
- end
+ item.spec = util.expandSingle(item.spec)
if item.spec.type == "struct" or item.spec.type == "union" then
local ok, err = register_structunion(lst, item)
if not ok then
diff --git a/script/plugins/ffi/c-parser/util.lua b/script/plugins/ffi/c-parser/util.lua
new file mode 100644
index 00000000..cb493efa
--- /dev/null
+++ b/script/plugins/ffi/c-parser/util.lua
@@ -0,0 +1,28 @@
+local m = {}
+
+local function tableLenEqual(t, len)
+ for key, value in pairs(t) do
+ len = len - 1
+ if len < 0 then
+ return false
+ end
+ end
+ return true
+end
+
+local function isSingleNode(ast)
+ if type(ast) ~= 'table' then
+ return false
+ end
+ local len = #ast
+ return len == 1 and tableLenEqual(ast, len)
+end
+
+function m.expandSingle(ast)
+ if isSingleNode(ast) then
+ return ast[1]
+ end
+ return ast
+end
+
+return m
diff --git a/script/plugins/ffi/init.lua b/script/plugins/ffi/init.lua
index 57a64958..66101f90 100644
--- a/script/plugins/ffi/init.lua
+++ b/script/plugins/ffi/init.lua
@@ -1,7 +1,8 @@
local searchCode = require 'plugins.ffi.searchCode'
local cdefRerence = require 'plugins.ffi.cdefRerence'
local cdriver = require 'plugins.ffi.c-parser.cdriver'
-local util = require 'utility'
+local util = require 'plugins.ffi.c-parser.util'
+local utility = require 'utility'
local SDBMHash = require 'SDBMHash'
local ws = require 'workspace'
local files = require 'files'
@@ -12,22 +13,6 @@ local scope = require 'workspace.scope'
local namespace <const> = 'ffi.namespace*.'
-local function nkeys(t)
- local n = 0
- for key, value in pairs(t) do
- n = n + 1
- end
- return n
-end
-
-local function isSingleNode(ast)
- if type(ast) ~= 'table' then
- return false
- end
- local len = #ast
- return len == 1 and len == nkeys(ast)
-end
-
--TODO:supprot 32bit ffi, need config
local knownTypes = {
["bool"] = 'boolean',
@@ -81,7 +66,7 @@ local knownTypes = {
local constName <const> = 'm'
---@class ffi.builder
-local builder = { switch_ast = util.switch() }
+local builder = { switch_ast = utility.switch() }
function builder:getTypeAst(name)
for i, asts in ipairs(self.globalAsts) do
@@ -216,9 +201,7 @@ do
if ops[val.op] then
return binop(enumer, val, ops[val.op])
end
- if isSingleNode(val) then
- val = val[1]
- end
+ val = util.expandSingle(val)
if type(val) == "string" then
if enumer[val] then
return enumer[val]
@@ -230,9 +213,7 @@ do
end
local function pushEnumValue(enumer, name, v)
- if isSingleNode(v) then
- v = tonumber(v[1])
- end
+ v = tonumber(util.expandSingle(v))
enumer[name] = v
enumer[#enumer+1] = v
return v
@@ -359,7 +340,7 @@ function m.initBuilder(fileDir)
local encoding = config.get(nil, 'Lua.runtime.fileEncoding')
local filePath = fileDir / table.concat({ hash, encoding }, '_')
- util.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
+ utility.saveFile(tostring(filePath) .. '.d.lua', table.concat(texts, '\n'))
end
end