1- local searchCode = require ' plugins.ffi.searchCode'
2- local cdefRerence = require ' plugins.ffi.cdefRerence'
3- local cdriver = require ' plugins.ffi.c-parser.cdriver'
4- local util = require ' plugins.ffi.c-parser.util'
5- local utility = require ' utility'
6- local SDBMHash = require ' SDBMHash'
7- local config = require ' config'
8- local fs = require ' bee.filesystem'
9- local scope = require ' workspace.scope'
1+ local searchCode = require ' plugins.ffi.searchCode'
2+ local cdefRerence = require ' plugins.ffi.cdefRerence'
3+ local cdriver = require ' plugins.ffi.c-parser.cdriver'
4+ local util = require ' plugins.ffi.c-parser.util'
5+ local utility = require ' utility'
6+ local SDBMHash = require ' SDBMHash'
7+ local config = require ' config'
8+ local fs = require ' bee.filesystem'
9+ local scope = require ' workspace.scope'
1010
11- local namespace <const> = ' ffi.namespace*.'
11+ local namespace <const> = ' ffi.namespace*.'
1212
1313-- TODO:supprot 32bit ffi, need config
14- local knownTypes = {
14+ local knownTypes = {
1515 [" bool" ] = ' boolean' ,
1616 [" char" ] = ' integer' ,
1717 [" short" ] = ' integer' ,
@@ -60,10 +60,33 @@ local knownTypes = {
6060 [" signedlong" ] = ' integer' ,
6161}
6262
63- local constName <const> = ' m'
63+ local blackKeyWord <const> = {
64+ [' and' ] = " _and" ,
65+ [' do' ] = " _do" ,
66+ [' elseif' ] = " _elseif" ,
67+ [' end' ] = " _end" ,
68+ [' false' ] = " _false" ,
69+ [' function' ] = " _function" ,
70+ [' in' ] = " _in" ,
71+ [' local' ] = " _local" ,
72+ [' nil' ] = " _nil" ,
73+ [' not' ] = " _not" ,
74+ [' or' ] = " _or" ,
75+ [' repeat' ] = " _repeat" ,
76+ [' then' ] = " _then" ,
77+ [' true' ] = " _true" ,
78+ }
79+
80+ local invaildKeyWord <const> = {
81+ const = true ,
82+ restrict = true ,
83+ volatile = true ,
84+ }
85+
86+ local constName <const> = ' m'
6487
6588--- @class ffi.builder
66- local builder = { switch_ast = utility .switch () }
89+ local builder = { switch_ast = utility .switch () }
6790
6891function builder :getTypeAst (name )
6992 for i , asts in ipairs (self .globalAsts ) do
@@ -95,14 +118,22 @@ function builder:getType(name)
95118 if type (name ) == ' table' then
96119 local t = " "
97120 local isStruct
121+ if name .type then
122+ t = t .. name .type .. " @"
123+ name = name .name
124+ end
98125 for _ , n in ipairs (name ) do
99126 if type (n ) == ' table' then
100127 n = n .full_name
101128 end
129+ if invaildKeyWord [n ] then
130+ goto continue
131+ end
102132 if not isStruct then
103133 isStruct = self :needDeref (self :getTypeAst (n ))
104134 end
105135 t = t .. n
136+ :: continue::
106137 end
107138 -- deref 一级指针
108139 if isStruct and t :sub (# t ) == ' *' then
@@ -142,11 +173,15 @@ local function getArrayType(arr)
142173 return res
143174end
144175
176+ local function getValidName (name )
177+ return blackKeyWord [name ] or name
178+ end
179+
145180function builder :buildStructOrUnion (lines , tt , name )
146181 lines [# lines + 1 ] = ' ---@class ' .. self :getType (name )
147182 for _ , field in ipairs (tt .fields or {}) do
148183 if field .name and field .type then
149- lines [# lines + 1 ] = (' ---@field %s %s%s' ):format (field .name , self :getType (field .type ),
184+ lines [# lines + 1 ] = (' ---@field %s %s%s' ):format (getValidName ( field .name ) , self :getType (field .type ),
150185 getArrayType (field .isarray ))
151186 end
152187 end
155190function builder :buildFunction (lines , tt , name )
156191 local param_names = {}
157192 for i , param in ipairs (tt .params or {}) do
158- lines [# lines + 1 ] = (' ---@param %s %s%s' ):format (param .name , self :getType (param .type ), getArrayType (param .idxs ))
159- param_names [# param_names + 1 ] = param .name
193+ local param_name = getValidName (param .name )
194+ lines [# lines + 1 ] = (' ---@param %s %s%s' ):format (param_name , self :getType (param .type ), getArrayType (param .idxs ))
195+ param_names [# param_names + 1 ] = param_name
160196 end
161197 if tt .vararg then
162198 param_names [# param_names + 1 ] = ' ...'
@@ -175,7 +211,7 @@ function builder:buildTypedef(lines, tt, name)
175211 -- 这个时候没有主类型,只有一个别名,直接创建一个别名结构体
176212 self .switch_ast (def .type , self , lines , def , name )
177213 else
178- lines [# lines + 1 ] = (' ---@alias %s %s' ):format (name , self :getType (def ))
214+ lines [# lines + 1 ] = (' ---@alias %s %s' ):format (self : getType ( name ) , self :getType (def ))
179215 end
180216end
181217
0 commit comments