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

/lib_pypy/cffi/cparser.py

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