PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib_pypy/cffi/cparser.py

https://bitbucket.org/kostialopuhin/pypy
Python | 772 lines | 658 code | 23 blank | 91 comment | 153 complexity | 0a1a3e215c5b7e5c1a42659c5f6c68b0 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0, BSD-3-Clause
  1. from . import api, model
  2. from .commontypes import COMMON_TYPES, resolve_common_type
  3. try:
  4. from . import _pycparser as pycparser
  5. except ImportError:
  6. import pycparser
  7. import weakref, re, sys
  8. try:
  9. if sys.version_info < (3,):
  10. import thread as _thread
  11. else:
  12. import _thread
  13. lock = _thread.allocate_lock()
  14. except ImportError:
  15. lock = None
  16. _r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
  17. re.DOTALL | re.MULTILINE)
  18. _r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
  19. r"\b((?:[^\n\\]|\\.)*?)$",
  20. re.DOTALL | re.MULTILINE)
  21. _r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}")
  22. _r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
  23. _r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]")
  24. _r_words = re.compile(r"\w+|\S")
  25. _parser_cache = None
  26. _r_int_literal = re.compile(r"-?0?x?[0-9a-f]+[lu]*$", re.IGNORECASE)
  27. _r_stdcall1 = re.compile(r"\b(__stdcall|WINAPI)\b")
  28. _r_stdcall2 = re.compile(r"[(]\s*(__stdcall|WINAPI)\b")
  29. _r_cdecl = re.compile(r"\b__cdecl\b")
  30. _r_star_const_space = re.compile( # matches "* const "
  31. r"[*]\s*((const|volatile|restrict)\b\s*)+")
  32. def _get_parser():
  33. global _parser_cache
  34. if _parser_cache is None:
  35. _parser_cache = pycparser.CParser()
  36. return _parser_cache
  37. def _workaround_for_old_pycparser(csource):
  38. # Workaround for a pycparser issue (fixed between pycparser 2.10 and
  39. # 2.14): "char*const***" gives us a wrong syntax tree, the same as
  40. # for "char***(*const)". This means we can't tell the difference
  41. # afterwards. But "char(*const(***))" gives us the right syntax
  42. # tree. The issue only occurs if there are several stars in
  43. # sequence with no parenthesis inbetween, just possibly qualifiers.
  44. # Attempt to fix it by adding some parentheses in the source: each
  45. # time we see "* const" or "* const *", we add an opening
  46. # parenthesis before each star---the hard part is figuring out where
  47. # to close them.
  48. parts = []
  49. while True:
  50. match = _r_star_const_space.search(csource)
  51. if not match:
  52. break
  53. #print repr(''.join(parts)+csource), '=>',
  54. parts.append(csource[:match.start()])
  55. parts.append('('); closing = ')'
  56. parts.append(match.group()) # e.g. "* const "
  57. endpos = match.end()
  58. if csource.startswith('*', endpos):
  59. parts.append('('); closing += ')'
  60. level = 0
  61. i = endpos
  62. while i < len(csource):
  63. c = csource[i]
  64. if c == '(':
  65. level += 1
  66. elif c == ')':
  67. if level == 0:
  68. break
  69. level -= 1
  70. elif c in ',;=':
  71. if level == 0:
  72. break
  73. i += 1
  74. csource = csource[endpos:i] + closing + csource[i:]
  75. #print repr(''.join(parts)+csource)
  76. parts.append(csource)
  77. return ''.join(parts)
  78. def _preprocess(csource):
  79. # Remove comments. NOTE: this only work because the cdef() section
  80. # should not contain any string literal!
  81. csource = _r_comment.sub(' ', csource)
  82. # Remove the "#define FOO x" lines
  83. macros = {}
  84. for match in _r_define.finditer(csource):
  85. macroname, macrovalue = match.groups()
  86. macrovalue = macrovalue.replace('\\\n', '').strip()
  87. macros[macroname] = macrovalue
  88. csource = _r_define.sub('', csource)
  89. #
  90. if pycparser.__version__ < '2.14':
  91. csource = _workaround_for_old_pycparser(csource)
  92. #
  93. # BIG HACK: replace WINAPI or __stdcall with "volatile const".
  94. # It doesn't make sense for the return type of a function to be
  95. # "volatile volatile const", so we abuse it to detect __stdcall...
  96. # Hack number 2 is that "int(volatile *fptr)();" is not valid C
  97. # syntax, so we place the "volatile" before the opening parenthesis.
  98. csource = _r_stdcall2.sub(' volatile volatile const(', csource)
  99. csource = _r_stdcall1.sub(' volatile volatile const ', csource)
  100. csource = _r_cdecl.sub(' ', csource)
  101. # Replace "[...]" with "[__dotdotdotarray__]"
  102. csource = _r_partial_array.sub('[__dotdotdotarray__]', csource)
  103. # Replace "...}" with "__dotdotdotNUM__}". This construction should
  104. # occur only at the end of enums; at the end of structs we have "...;}"
  105. # and at the end of vararg functions "...);". Also replace "=...[,}]"
  106. # with ",__dotdotdotNUM__[,}]": this occurs in the enums too, when
  107. # giving an unknown value.
  108. matches = list(_r_partial_enum.finditer(csource))
  109. for number, match in enumerate(reversed(matches)):
  110. p = match.start()
  111. if csource[p] == '=':
  112. p2 = csource.find('...', p, match.end())
  113. assert p2 > p
  114. csource = '%s,__dotdotdot%d__ %s' % (csource[:p], number,
  115. csource[p2+3:])
  116. else:
  117. assert csource[p:p+3] == '...'
  118. csource = '%s __dotdotdot%d__ %s' % (csource[:p], number,
  119. csource[p+3:])
  120. # Replace all remaining "..." with the same name, "__dotdotdot__",
  121. # which is declared with a typedef for the purpose of C parsing.
  122. return csource.replace('...', ' __dotdotdot__ '), macros
  123. def _common_type_names(csource):
  124. # Look in the source for what looks like usages of types from the
  125. # list of common types. A "usage" is approximated here as the
  126. # appearance of the word, minus a "definition" of the type, which
  127. # is the last word in a "typedef" statement. Approximative only
  128. # but should be fine for all the common types.
  129. look_for_words = set(COMMON_TYPES)
  130. look_for_words.add(';')
  131. look_for_words.add(',')
  132. look_for_words.add('(')
  133. look_for_words.add(')')
  134. look_for_words.add('typedef')
  135. words_used = set()
  136. is_typedef = False
  137. paren = 0
  138. previous_word = ''
  139. for word in _r_words.findall(csource):
  140. if word in look_for_words:
  141. if word == ';':
  142. if is_typedef:
  143. words_used.discard(previous_word)
  144. look_for_words.discard(previous_word)
  145. is_typedef = False
  146. elif word == 'typedef':
  147. is_typedef = True
  148. paren = 0
  149. elif word == '(':
  150. paren += 1
  151. elif word == ')':
  152. paren -= 1
  153. elif word == ',':
  154. if is_typedef and paren == 0:
  155. words_used.discard(previous_word)
  156. look_for_words.discard(previous_word)
  157. else: # word in COMMON_TYPES
  158. words_used.add(word)
  159. previous_word = word
  160. return words_used
  161. class Parser(object):
  162. def __init__(self):
  163. self._declarations = {}
  164. self._included_declarations = set()
  165. self._anonymous_counter = 0
  166. self._structnode2type = weakref.WeakKeyDictionary()
  167. self._override = False
  168. self._packed = False
  169. self._int_constants = {}
  170. self._recomplete = []
  171. self._uses_new_feature = None
  172. def _parse(self, csource):
  173. csource, macros = _preprocess(csource)
  174. # XXX: for more efficiency we would need to poke into the
  175. # internals of CParser... the following registers the
  176. # typedefs, because their presence or absence influences the
  177. # parsing itself (but what they are typedef'ed to plays no role)
  178. ctn = _common_type_names(csource)
  179. typenames = []
  180. for name in sorted(self._declarations):
  181. if name.startswith('typedef '):
  182. name = name[8:]
  183. typenames.append(name)
  184. ctn.discard(name)
  185. typenames += sorted(ctn)
  186. #
  187. csourcelines = ['typedef int %s;' % typename for typename in typenames]
  188. csourcelines.append('typedef int __dotdotdot__;')
  189. csourcelines.append(csource)
  190. csource = '\n'.join(csourcelines)
  191. if lock is not None:
  192. lock.acquire() # pycparser is not thread-safe...
  193. try:
  194. ast = _get_parser().parse(csource)
  195. except pycparser.c_parser.ParseError as e:
  196. self.convert_pycparser_error(e, csource)
  197. finally:
  198. if lock is not None:
  199. lock.release()
  200. # csource will be used to find buggy source text
  201. return ast, macros, csource
  202. def _convert_pycparser_error(self, e, csource):
  203. # xxx look for ":NUM:" at the start of str(e) and try to interpret
  204. # it as a line number
  205. line = None
  206. msg = str(e)
  207. if msg.startswith(':') and ':' in msg[1:]:
  208. linenum = msg[1:msg.find(':',1)]
  209. if linenum.isdigit():
  210. linenum = int(linenum, 10)
  211. csourcelines = csource.splitlines()
  212. if 1 <= linenum <= len(csourcelines):
  213. line = csourcelines[linenum-1]
  214. return line
  215. def convert_pycparser_error(self, e, csource):
  216. line = self._convert_pycparser_error(e, csource)
  217. msg = str(e)
  218. if line:
  219. msg = 'cannot parse "%s"\n%s' % (line.strip(), msg)
  220. else:
  221. msg = 'parse error\n%s' % (msg,)
  222. raise api.CDefError(msg)
  223. def parse(self, csource, override=False, packed=False):
  224. prev_override = self._override
  225. prev_packed = self._packed
  226. try:
  227. self._override = override
  228. self._packed = packed
  229. self._internal_parse(csource)
  230. finally:
  231. self._override = prev_override
  232. self._packed = prev_packed
  233. def _internal_parse(self, csource):
  234. ast, macros, csource = self._parse(csource)
  235. # add the macros
  236. self._process_macros(macros)
  237. # find the first "__dotdotdot__" and use that as a separator
  238. # between the repeated typedefs and the real csource
  239. iterator = iter(ast.ext)
  240. for decl in iterator:
  241. if decl.name == '__dotdotdot__':
  242. break
  243. #
  244. try:
  245. for decl in iterator:
  246. if isinstance(decl, pycparser.c_ast.Decl):
  247. self._parse_decl(decl)
  248. elif isinstance(decl, pycparser.c_ast.Typedef):
  249. if not decl.name:
  250. raise api.CDefError("typedef does not declare any name",
  251. decl)
  252. quals = 0
  253. if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType)
  254. and decl.type.type.names[-1] == '__dotdotdot__'):
  255. realtype = self._get_unknown_type(decl)
  256. elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
  257. isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
  258. isinstance(decl.type.type.type,
  259. pycparser.c_ast.IdentifierType) and
  260. decl.type.type.type.names == ['__dotdotdot__']):
  261. realtype = model.unknown_ptr_type(decl.name)
  262. else:
  263. realtype, quals = self._get_type_and_quals(
  264. decl.type, name=decl.name)
  265. self._declare('typedef ' + decl.name, realtype, quals=quals)
  266. else:
  267. raise api.CDefError("unrecognized construct", decl)
  268. except api.FFIError as e:
  269. msg = self._convert_pycparser_error(e, csource)
  270. if msg:
  271. e.args = (e.args[0] + "\n *** Err: %s" % msg,)
  272. raise
  273. def _add_constants(self, key, val):
  274. if key in self._int_constants:
  275. if self._int_constants[key] == val:
  276. return # ignore identical double declarations
  277. raise api.FFIError(
  278. "multiple declarations of constant: %s" % (key,))
  279. self._int_constants[key] = val
  280. def _add_integer_constant(self, name, int_str):
  281. int_str = int_str.lower().rstrip("ul")
  282. neg = int_str.startswith('-')
  283. if neg:
  284. int_str = int_str[1:]
  285. # "010" is not valid oct in py3
  286. if (int_str.startswith("0") and int_str != '0'
  287. and not int_str.startswith("0x")):
  288. int_str = "0o" + int_str[1:]
  289. pyvalue = int(int_str, 0)
  290. if neg:
  291. pyvalue = -pyvalue
  292. self._add_constants(name, pyvalue)
  293. self._declare('macro ' + name, pyvalue)
  294. def _process_macros(self, macros):
  295. for key, value in macros.items():
  296. value = value.strip()
  297. if _r_int_literal.match(value):
  298. self._add_integer_constant(key, value)
  299. elif value == '...':
  300. self._declare('macro ' + key, value)
  301. else:
  302. raise api.CDefError(
  303. 'only supports one of the following syntax:\n'
  304. ' #define %s ... (literally dot-dot-dot)\n'
  305. ' #define %s NUMBER (with NUMBER an integer'
  306. ' constant, decimal/hex/octal)\n'
  307. 'got:\n'
  308. ' #define %s %s'
  309. % (key, key, key, value))
  310. def _parse_decl(self, decl):
  311. node = decl.type
  312. if isinstance(node, pycparser.c_ast.FuncDecl):
  313. tp, quals = self._get_type_and_quals(node, name=decl.name)
  314. assert isinstance(tp, model.RawFunctionType)
  315. tp = self._get_type_pointer(tp, quals)
  316. self._declare('function ' + decl.name, tp)
  317. else:
  318. if isinstance(node, pycparser.c_ast.Struct):
  319. self._get_struct_union_enum_type('struct', node)
  320. elif isinstance(node, pycparser.c_ast.Union):
  321. self._get_struct_union_enum_type('union', node)
  322. elif isinstance(node, pycparser.c_ast.Enum):
  323. self._get_struct_union_enum_type('enum', node)
  324. elif not decl.name:
  325. raise api.CDefError("construct does not declare any variable",
  326. decl)
  327. #
  328. if decl.name:
  329. tp, quals = self._get_type_and_quals(node,
  330. partial_length_ok=True)
  331. if tp.is_raw_function:
  332. tp = self._get_type_pointer(tp, quals)
  333. self._declare('function ' + decl.name, tp)
  334. elif (tp.is_integer_type() and
  335. hasattr(decl, 'init') and
  336. hasattr(decl.init, 'value') and
  337. _r_int_literal.match(decl.init.value)):
  338. self._add_integer_constant(decl.name, decl.init.value)
  339. elif (tp.is_integer_type() and
  340. isinstance(decl.init, pycparser.c_ast.UnaryOp) and
  341. decl.init.op == '-' and
  342. hasattr(decl.init.expr, 'value') and
  343. _r_int_literal.match(decl.init.expr.value)):
  344. self._add_integer_constant(decl.name,
  345. '-' + decl.init.expr.value)
  346. elif (quals & model.Q_CONST) and not tp.is_array_type:
  347. self._declare('constant ' + decl.name, tp, quals=quals)
  348. else:
  349. self._declare('variable ' + decl.name, tp, quals=quals)
  350. def parse_type(self, cdecl):
  351. return self.parse_type_and_quals(cdecl)[0]
  352. def parse_type_and_quals(self, cdecl):
  353. ast, macros = self._parse('void __dummy(\n%s\n);' % cdecl)[:2]
  354. assert not macros
  355. exprnode = ast.ext[-1].type.args.params[0]
  356. if isinstance(exprnode, pycparser.c_ast.ID):
  357. raise api.CDefError("unknown identifier '%s'" % (exprnode.name,))
  358. return self._get_type_and_quals(exprnode.type)
  359. def _declare(self, name, obj, included=False, quals=0):
  360. if name in self._declarations:
  361. prevobj, prevquals = self._declarations[name]
  362. if prevobj is obj and prevquals == quals:
  363. return
  364. if not self._override:
  365. raise api.FFIError(
  366. "multiple declarations of %s (for interactive usage, "
  367. "try cdef(xx, override=True))" % (name,))
  368. assert '__dotdotdot__' not in name.split()
  369. self._declarations[name] = (obj, quals)
  370. if included:
  371. self._included_declarations.add(obj)
  372. def _extract_quals(self, type):
  373. quals = 0
  374. if isinstance(type, (pycparser.c_ast.TypeDecl,
  375. pycparser.c_ast.PtrDecl)):
  376. if 'const' in type.quals:
  377. quals |= model.Q_CONST
  378. if 'volatile' in type.quals:
  379. quals |= model.Q_VOLATILE
  380. if 'restrict' in type.quals:
  381. quals |= model.Q_RESTRICT
  382. return quals
  383. def _get_type_pointer(self, type, quals, declname=None):
  384. if isinstance(type, model.RawFunctionType):
  385. return type.as_function_pointer()
  386. if (isinstance(type, model.StructOrUnionOrEnum) and
  387. type.name.startswith('$') and type.name[1:].isdigit() and
  388. type.forcename is None and declname is not None):
  389. return model.NamedPointerType(type, declname, quals)
  390. return model.PointerType(type, quals)
  391. def _get_type_and_quals(self, typenode, name=None, partial_length_ok=False):
  392. # first, dereference typedefs, if we have it already parsed, we're good
  393. if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
  394. isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
  395. len(typenode.type.names) == 1 and
  396. ('typedef ' + typenode.type.names[0]) in self._declarations):
  397. tp, quals = self._declarations['typedef ' + typenode.type.names[0]]
  398. quals |= self._extract_quals(typenode)
  399. return tp, quals
  400. #
  401. if isinstance(typenode, pycparser.c_ast.ArrayDecl):
  402. # array type
  403. if typenode.dim is None:
  404. length = None
  405. else:
  406. length = self._parse_constant(
  407. typenode.dim, partial_length_ok=partial_length_ok)
  408. tp, quals = self._get_type_and_quals(typenode.type,
  409. partial_length_ok=partial_length_ok)
  410. return model.ArrayType(tp, length), quals
  411. #
  412. if isinstance(typenode, pycparser.c_ast.PtrDecl):
  413. # pointer type
  414. itemtype, itemquals = self._get_type_and_quals(typenode.type)
  415. tp = self._get_type_pointer(itemtype, itemquals, declname=name)
  416. quals = self._extract_quals(typenode)
  417. return tp, quals
  418. #
  419. if isinstance(typenode, pycparser.c_ast.TypeDecl):
  420. quals = self._extract_quals(typenode)
  421. type = typenode.type
  422. if isinstance(type, pycparser.c_ast.IdentifierType):
  423. # assume a primitive type. get it from .names, but reduce
  424. # synonyms to a single chosen combination
  425. names = list(type.names)
  426. if names != ['signed', 'char']: # keep this unmodified
  427. prefixes = {}
  428. while names:
  429. name = names[0]
  430. if name in ('short', 'long', 'signed', 'unsigned'):
  431. prefixes[name] = prefixes.get(name, 0) + 1
  432. del names[0]
  433. else:
  434. break
  435. # ignore the 'signed' prefix below, and reorder the others
  436. newnames = []
  437. for prefix in ('unsigned', 'short', 'long'):
  438. for i in range(prefixes.get(prefix, 0)):
  439. newnames.append(prefix)
  440. if not names:
  441. names = ['int'] # implicitly
  442. if names == ['int']: # but kill it if 'short' or 'long'
  443. if 'short' in prefixes or 'long' in prefixes:
  444. names = []
  445. names = newnames + names
  446. ident = ' '.join(names)
  447. if ident == 'void':
  448. return model.void_type, quals
  449. if ident == '__dotdotdot__':
  450. raise api.FFIError(':%d: bad usage of "..."' %
  451. typenode.coord.line)
  452. tp0, quals0 = resolve_common_type(self, ident)
  453. return tp0, (quals | quals0)
  454. #
  455. if isinstance(type, pycparser.c_ast.Struct):
  456. # 'struct foobar'
  457. tp = self._get_struct_union_enum_type('struct', type, name)
  458. return tp, quals
  459. #
  460. if isinstance(type, pycparser.c_ast.Union):
  461. # 'union foobar'
  462. tp = self._get_struct_union_enum_type('union', type, name)
  463. return tp, quals
  464. #
  465. if isinstance(type, pycparser.c_ast.Enum):
  466. # 'enum foobar'
  467. tp = self._get_struct_union_enum_type('enum', type, name)
  468. return tp, quals
  469. #
  470. if isinstance(typenode, pycparser.c_ast.FuncDecl):
  471. # a function type
  472. return self._parse_function_type(typenode, name), 0
  473. #
  474. # nested anonymous structs or unions end up here
  475. if isinstance(typenode, pycparser.c_ast.Struct):
  476. return self._get_struct_union_enum_type('struct', typenode, name,
  477. nested=True), 0
  478. if isinstance(typenode, pycparser.c_ast.Union):
  479. return self._get_struct_union_enum_type('union', typenode, name,
  480. nested=True), 0
  481. #
  482. raise api.FFIError(":%d: bad or unsupported type declaration" %
  483. typenode.coord.line)
  484. def _parse_function_type(self, typenode, funcname=None):
  485. params = list(getattr(typenode.args, 'params', []))
  486. for i, arg in enumerate(params):
  487. if not hasattr(arg, 'type'):
  488. raise api.CDefError("%s arg %d: unknown type '%s'"
  489. " (if you meant to use the old C syntax of giving"
  490. " untyped arguments, it is not supported)"
  491. % (funcname or 'in expression', i + 1,
  492. getattr(arg, 'name', '?')))
  493. ellipsis = (
  494. len(params) > 0 and
  495. isinstance(params[-1].type, pycparser.c_ast.TypeDecl) and
  496. isinstance(params[-1].type.type,
  497. pycparser.c_ast.IdentifierType) and
  498. params[-1].type.type.names == ['__dotdotdot__'])
  499. if ellipsis:
  500. params.pop()
  501. if not params:
  502. raise api.CDefError(
  503. "%s: a function with only '(...)' as argument"
  504. " is not correct C" % (funcname or 'in expression'))
  505. args = [self._as_func_arg(*self._get_type_and_quals(argdeclnode.type))
  506. for argdeclnode in params]
  507. if not ellipsis and args == [model.void_type]:
  508. args = []
  509. result, quals = self._get_type_and_quals(typenode.type)
  510. # the 'quals' on the result type are ignored. HACK: we absure them
  511. # to detect __stdcall functions: we textually replace "__stdcall"
  512. # with "volatile volatile const" above.
  513. abi = None
  514. if hasattr(typenode.type, 'quals'): # else, probable syntax error anyway
  515. if typenode.type.quals[-3:] == ['volatile', 'volatile', 'const']:
  516. abi = '__stdcall'
  517. return model.RawFunctionType(tuple(args), result, ellipsis, abi)
  518. def _as_func_arg(self, type, quals):
  519. if isinstance(type, model.ArrayType):
  520. return model.PointerType(type.item, quals)
  521. elif isinstance(type, model.RawFunctionType):
  522. return type.as_function_pointer()
  523. else:
  524. return type
  525. def _get_struct_union_enum_type(self, kind, type, name=None, nested=False):
  526. # First, a level of caching on the exact 'type' node of the AST.
  527. # This is obscure, but needed because pycparser "unrolls" declarations
  528. # such as "typedef struct { } foo_t, *foo_p" and we end up with
  529. # an AST that is not a tree, but a DAG, with the "type" node of the
  530. # two branches foo_t and foo_p of the trees being the same node.
  531. # It's a bit silly but detecting "DAG-ness" in the AST tree seems
  532. # to be the only way to distinguish this case from two independent
  533. # structs. See test_struct_with_two_usages.
  534. try:
  535. return self._structnode2type[type]
  536. except KeyError:
  537. pass
  538. #
  539. # Note that this must handle parsing "struct foo" any number of
  540. # times and always return the same StructType object. Additionally,
  541. # one of these times (not necessarily the first), the fields of
  542. # the struct can be specified with "struct foo { ...fields... }".
  543. # If no name is given, then we have to create a new anonymous struct
  544. # with no caching; in this case, the fields are either specified
  545. # right now or never.
  546. #
  547. force_name = name
  548. name = type.name
  549. #
  550. # get the type or create it if needed
  551. if name is None:
  552. # 'force_name' is used to guess a more readable name for
  553. # anonymous structs, for the common case "typedef struct { } foo".
  554. if force_name is not None:
  555. explicit_name = '$%s' % force_name
  556. else:
  557. self._anonymous_counter += 1
  558. explicit_name = '$%d' % self._anonymous_counter
  559. tp = None
  560. else:
  561. explicit_name = name
  562. key = '%s %s' % (kind, name)
  563. tp, _ = self._declarations.get(key, (None, None))
  564. #
  565. if tp is None:
  566. if kind == 'struct':
  567. tp = model.StructType(explicit_name, None, None, None)
  568. elif kind == 'union':
  569. tp = model.UnionType(explicit_name, None, None, None)
  570. elif kind == 'enum':
  571. if explicit_name == '__dotdotdot__':
  572. raise CDefError("Enums cannot be declared with ...")
  573. tp = self._build_enum_type(explicit_name, type.values)
  574. else:
  575. raise AssertionError("kind = %r" % (kind,))
  576. if name is not None:
  577. self._declare(key, tp)
  578. else:
  579. if kind == 'enum' and type.values is not None:
  580. raise NotImplementedError(
  581. "enum %s: the '{}' declaration should appear on the first "
  582. "time the enum is mentioned, not later" % explicit_name)
  583. if not tp.forcename:
  584. tp.force_the_name(force_name)
  585. if tp.forcename and '$' in tp.name:
  586. self._declare('anonymous %s' % tp.forcename, tp)
  587. #
  588. self._structnode2type[type] = tp
  589. #
  590. # enums: done here
  591. if kind == 'enum':
  592. return tp
  593. #
  594. # is there a 'type.decls'? If yes, then this is the place in the
  595. # C sources that declare the fields. If no, then just return the
  596. # existing type, possibly still incomplete.
  597. if type.decls is None:
  598. return tp
  599. #
  600. if tp.fldnames is not None:
  601. raise api.CDefError("duplicate declaration of struct %s" % name)
  602. fldnames = []
  603. fldtypes = []
  604. fldbitsize = []
  605. fldquals = []
  606. for decl in type.decls:
  607. if (isinstance(decl.type, pycparser.c_ast.IdentifierType) and
  608. ''.join(decl.type.names) == '__dotdotdot__'):
  609. # XXX pycparser is inconsistent: 'names' should be a list
  610. # of strings, but is sometimes just one string. Use
  611. # str.join() as a way to cope with both.
  612. self._make_partial(tp, nested)
  613. continue
  614. if decl.bitsize is None:
  615. bitsize = -1
  616. else:
  617. bitsize = self._parse_constant(decl.bitsize)
  618. self._partial_length = False
  619. type, fqual = self._get_type_and_quals(decl.type,
  620. partial_length_ok=True)
  621. if self._partial_length:
  622. self._make_partial(tp, nested)
  623. if isinstance(type, model.StructType) and type.partial:
  624. self._make_partial(tp, nested)
  625. fldnames.append(decl.name or '')
  626. fldtypes.append(type)
  627. fldbitsize.append(bitsize)
  628. fldquals.append(fqual)
  629. tp.fldnames = tuple(fldnames)
  630. tp.fldtypes = tuple(fldtypes)
  631. tp.fldbitsize = tuple(fldbitsize)
  632. tp.fldquals = tuple(fldquals)
  633. if fldbitsize != [-1] * len(fldbitsize):
  634. if isinstance(tp, model.StructType) and tp.partial:
  635. raise NotImplementedError("%s: using both bitfields and '...;'"
  636. % (tp,))
  637. tp.packed = self._packed
  638. if tp.completed: # must be re-completed: it is not opaque any more
  639. tp.completed = 0
  640. self._recomplete.append(tp)
  641. return tp
  642. def _make_partial(self, tp, nested):
  643. if not isinstance(tp, model.StructOrUnion):
  644. raise api.CDefError("%s cannot be partial" % (tp,))
  645. if not tp.has_c_name() and not nested:
  646. raise NotImplementedError("%s is partial but has no C name" %(tp,))
  647. tp.partial = True
  648. def _parse_constant(self, exprnode, partial_length_ok=False):
  649. # for now, limited to expressions that are an immediate number
  650. # or positive/negative number
  651. if isinstance(exprnode, pycparser.c_ast.Constant):
  652. s = exprnode.value
  653. if s.startswith('0'):
  654. if s.startswith('0x') or s.startswith('0X'):
  655. return int(s, 16)
  656. return int(s, 8)
  657. elif '1' <= s[0] <= '9':
  658. return int(s, 10)
  659. elif s[0] == "'" and s[-1] == "'" and (
  660. len(s) == 3 or (len(s) == 4 and s[1] == "\\")):
  661. return ord(s[-2])
  662. else:
  663. raise api.CDefError("invalid constant %r" % (s,))
  664. #
  665. if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and
  666. exprnode.op == '+'):
  667. return self._parse_constant(exprnode.expr)
  668. #
  669. if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and
  670. exprnode.op == '-'):
  671. return -self._parse_constant(exprnode.expr)
  672. # load previously defined int constant
  673. if (isinstance(exprnode, pycparser.c_ast.ID) and
  674. exprnode.name in self._int_constants):
  675. return self._int_constants[exprnode.name]
  676. #
  677. if partial_length_ok:
  678. if (isinstance(exprnode, pycparser.c_ast.ID) and
  679. exprnode.name == '__dotdotdotarray__'):
  680. self._partial_length = True
  681. return '...'
  682. #
  683. raise api.FFIError(":%d: unsupported expression: expected a "
  684. "simple numeric constant" % exprnode.coord.line)
  685. def _build_enum_type(self, explicit_name, decls):
  686. if decls is not None:
  687. partial = False
  688. enumerators = []
  689. enumvalues = []
  690. nextenumvalue = 0
  691. for enum in decls.enumerators:
  692. if _r_enum_dotdotdot.match(enum.name):
  693. partial = True
  694. continue
  695. if enum.value is not None:
  696. nextenumvalue = self._parse_constant(enum.value)
  697. enumerators.append(enum.name)
  698. enumvalues.append(nextenumvalue)
  699. self._add_constants(enum.name, nextenumvalue)
  700. nextenumvalue += 1
  701. enumerators = tuple(enumerators)
  702. enumvalues = tuple(enumvalues)
  703. tp = model.EnumType(explicit_name, enumerators, enumvalues)
  704. tp.partial = partial
  705. else: # opaque enum
  706. tp = model.EnumType(explicit_name, (), ())
  707. return tp
  708. def include(self, other):
  709. for name, (tp, quals) in other._declarations.items():
  710. if name.startswith('anonymous $enum_$'):
  711. continue # fix for test_anonymous_enum_include
  712. kind = name.split(' ', 1)[0]
  713. if kind in ('struct', 'union', 'enum', 'anonymous', 'typedef'):
  714. self._declare(name, tp, included=True, quals=quals)
  715. for k, v in other._int_constants.items():
  716. self._add_constants(k, v)
  717. def _get_unknown_type(self, decl):
  718. typenames = decl.type.type.names
  719. assert typenames[-1] == '__dotdotdot__'
  720. if len(typenames) == 1:
  721. return model.unknown_type(decl.name)
  722. if (typenames[:-1] == ['float'] or
  723. typenames[:-1] == ['double']):
  724. # not for 'long double' so far
  725. result = model.UnknownFloatType(decl.name)
  726. else:
  727. for t in typenames[:-1]:
  728. if t not in ['int', 'short', 'long', 'signed',
  729. 'unsigned', 'char']:
  730. raise api.FFIError(':%d: bad usage of "..."' %
  731. decl.coord.line)
  732. result = model.UnknownIntegerType(decl.name)
  733. if self._uses_new_feature is None:
  734. self._uses_new_feature = "'typedef %s... %s'" % (
  735. ' '.join(typenames[:-1]), decl.name)
  736. return result