c_parser.py 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. #------------------------------------------------------------------------------
  2. # pycparser: c_parser.py
  3. #
  4. # CParser class: Parser and AST builder for the C language
  5. #
  6. # Eli Bendersky [https://eli.thegreenplace.net/]
  7. # License: BSD
  8. #------------------------------------------------------------------------------
  9. from .ply import yacc
  10. from . import c_ast
  11. from .c_lexer import CLexer
  12. from .plyparser import PLYParser, ParseError, parameterized, template
  13. from .ast_transforms import fix_switch_cases, fix_atomic_specifiers
  14. @template
  15. class CParser(PLYParser):
  16. def __init__(
  17. self,
  18. lex_optimize=True,
  19. lexer=CLexer,
  20. lextab='pycparser.lextab',
  21. yacc_optimize=True,
  22. yacctab='pycparser.yacctab',
  23. yacc_debug=False,
  24. taboutputdir=''):
  25. """ Create a new CParser.
  26. Some arguments for controlling the debug/optimization
  27. level of the parser are provided. The defaults are
  28. tuned for release/performance mode.
  29. The simple rules for using them are:
  30. *) When tweaking CParser/CLexer, set these to False
  31. *) When releasing a stable parser, set to True
  32. lex_optimize:
  33. Set to False when you're modifying the lexer.
  34. Otherwise, changes in the lexer won't be used, if
  35. some lextab.py file exists.
  36. When releasing with a stable lexer, set to True
  37. to save the re-generation of the lexer table on
  38. each run.
  39. lexer:
  40. Set this parameter to define the lexer to use if
  41. you're not using the default CLexer.
  42. lextab:
  43. Points to the lex table that's used for optimized
  44. mode. Only if you're modifying the lexer and want
  45. some tests to avoid re-generating the table, make
  46. this point to a local lex table file (that's been
  47. earlier generated with lex_optimize=True)
  48. yacc_optimize:
  49. Set to False when you're modifying the parser.
  50. Otherwise, changes in the parser won't be used, if
  51. some parsetab.py file exists.
  52. When releasing with a stable parser, set to True
  53. to save the re-generation of the parser table on
  54. each run.
  55. yacctab:
  56. Points to the yacc table that's used for optimized
  57. mode. Only if you're modifying the parser, make
  58. this point to a local yacc table file
  59. yacc_debug:
  60. Generate a parser.out file that explains how yacc
  61. built the parsing table from the grammar.
  62. taboutputdir:
  63. Set this parameter to control the location of generated
  64. lextab and yacctab files.
  65. """
  66. self.clex = lexer(
  67. error_func=self._lex_error_func,
  68. on_lbrace_func=self._lex_on_lbrace_func,
  69. on_rbrace_func=self._lex_on_rbrace_func,
  70. type_lookup_func=self._lex_type_lookup_func)
  71. self.clex.build(
  72. optimize=lex_optimize,
  73. lextab=lextab,
  74. outputdir=taboutputdir)
  75. self.tokens = self.clex.tokens
  76. rules_with_opt = [
  77. 'abstract_declarator',
  78. 'assignment_expression',
  79. 'declaration_list',
  80. 'declaration_specifiers_no_type',
  81. 'designation',
  82. 'expression',
  83. 'identifier_list',
  84. 'init_declarator_list',
  85. 'id_init_declarator_list',
  86. 'initializer_list',
  87. 'parameter_type_list',
  88. 'block_item_list',
  89. 'type_qualifier_list',
  90. 'struct_declarator_list'
  91. ]
  92. for rule in rules_with_opt:
  93. self._create_opt_rule(rule)
  94. self.cparser = yacc.yacc(
  95. module=self,
  96. start='translation_unit_or_empty',
  97. debug=yacc_debug,
  98. optimize=yacc_optimize,
  99. tabmodule=yacctab,
  100. outputdir=taboutputdir)
  101. # Stack of scopes for keeping track of symbols. _scope_stack[-1] is
  102. # the current (topmost) scope. Each scope is a dictionary that
  103. # specifies whether a name is a type. If _scope_stack[n][name] is
  104. # True, 'name' is currently a type in the scope. If it's False,
  105. # 'name' is used in the scope but not as a type (for instance, if we
  106. # saw: int name;
  107. # If 'name' is not a key in _scope_stack[n] then 'name' was not defined
  108. # in this scope at all.
  109. self._scope_stack = [dict()]
  110. # Keeps track of the last token given to yacc (the lookahead token)
  111. self._last_yielded_token = None
  112. def parse(self, text, filename='', debug=False):
  113. """ Parses C code and returns an AST.
  114. text:
  115. A string containing the C source code
  116. filename:
  117. Name of the file being parsed (for meaningful
  118. error messages)
  119. debug:
  120. Debug flag to YACC
  121. """
  122. self.clex.filename = filename
  123. self.clex.reset_lineno()
  124. self._scope_stack = [dict()]
  125. self._last_yielded_token = None
  126. return self.cparser.parse(
  127. input=text,
  128. lexer=self.clex,
  129. debug=debug)
  130. ######################-- PRIVATE --######################
  131. def _push_scope(self):
  132. self._scope_stack.append(dict())
  133. def _pop_scope(self):
  134. assert len(self._scope_stack) > 1
  135. self._scope_stack.pop()
  136. def _add_typedef_name(self, name, coord):
  137. """ Add a new typedef name (ie a TYPEID) to the current scope
  138. """
  139. if not self._scope_stack[-1].get(name, True):
  140. self._parse_error(
  141. "Typedef %r previously declared as non-typedef "
  142. "in this scope" % name, coord)
  143. self._scope_stack[-1][name] = True
  144. def _add_identifier(self, name, coord):
  145. """ Add a new object, function, or enum member name (ie an ID) to the
  146. current scope
  147. """
  148. if self._scope_stack[-1].get(name, False):
  149. self._parse_error(
  150. "Non-typedef %r previously declared as typedef "
  151. "in this scope" % name, coord)
  152. self._scope_stack[-1][name] = False
  153. def _is_type_in_scope(self, name):
  154. """ Is *name* a typedef-name in the current scope?
  155. """
  156. for scope in reversed(self._scope_stack):
  157. # If name is an identifier in this scope it shadows typedefs in
  158. # higher scopes.
  159. in_scope = scope.get(name)
  160. if in_scope is not None: return in_scope
  161. return False
  162. def _lex_error_func(self, msg, line, column):
  163. self._parse_error(msg, self._coord(line, column))
  164. def _lex_on_lbrace_func(self):
  165. self._push_scope()
  166. def _lex_on_rbrace_func(self):
  167. self._pop_scope()
  168. def _lex_type_lookup_func(self, name):
  169. """ Looks up types that were previously defined with
  170. typedef.
  171. Passed to the lexer for recognizing identifiers that
  172. are types.
  173. """
  174. is_type = self._is_type_in_scope(name)
  175. return is_type
  176. def _get_yacc_lookahead_token(self):
  177. """ We need access to yacc's lookahead token in certain cases.
  178. This is the last token yacc requested from the lexer, so we
  179. ask the lexer.
  180. """
  181. return self.clex.last_token
  182. # To understand what's going on here, read sections A.8.5 and
  183. # A.8.6 of K&R2 very carefully.
  184. #
  185. # A C type consists of a basic type declaration, with a list
  186. # of modifiers. For example:
  187. #
  188. # int *c[5];
  189. #
  190. # The basic declaration here is 'int c', and the pointer and
  191. # the array are the modifiers.
  192. #
  193. # Basic declarations are represented by TypeDecl (from module c_ast) and the
  194. # modifiers are FuncDecl, PtrDecl and ArrayDecl.
  195. #
  196. # The standard states that whenever a new modifier is parsed, it should be
  197. # added to the end of the list of modifiers. For example:
  198. #
  199. # K&R2 A.8.6.2: Array Declarators
  200. #
  201. # In a declaration T D where D has the form
  202. # D1 [constant-expression-opt]
  203. # and the type of the identifier in the declaration T D1 is
  204. # "type-modifier T", the type of the
  205. # identifier of D is "type-modifier array of T"
  206. #
  207. # This is what this method does. The declarator it receives
  208. # can be a list of declarators ending with TypeDecl. It
  209. # tacks the modifier to the end of this list, just before
  210. # the TypeDecl.
  211. #
  212. # Additionally, the modifier may be a list itself. This is
  213. # useful for pointers, that can come as a chain from the rule
  214. # p_pointer. In this case, the whole modifier list is spliced
  215. # into the new location.
  216. def _type_modify_decl(self, decl, modifier):
  217. """ Tacks a type modifier on a declarator, and returns
  218. the modified declarator.
  219. Note: the declarator and modifier may be modified
  220. """
  221. #~ print '****'
  222. #~ decl.show(offset=3)
  223. #~ modifier.show(offset=3)
  224. #~ print '****'
  225. modifier_head = modifier
  226. modifier_tail = modifier
  227. # The modifier may be a nested list. Reach its tail.
  228. while modifier_tail.type:
  229. modifier_tail = modifier_tail.type
  230. # If the decl is a basic type, just tack the modifier onto it.
  231. if isinstance(decl, c_ast.TypeDecl):
  232. modifier_tail.type = decl
  233. return modifier
  234. else:
  235. # Otherwise, the decl is a list of modifiers. Reach
  236. # its tail and splice the modifier onto the tail,
  237. # pointing to the underlying basic type.
  238. decl_tail = decl
  239. while not isinstance(decl_tail.type, c_ast.TypeDecl):
  240. decl_tail = decl_tail.type
  241. modifier_tail.type = decl_tail.type
  242. decl_tail.type = modifier_head
  243. return decl
  244. # Due to the order in which declarators are constructed,
  245. # they have to be fixed in order to look like a normal AST.
  246. #
  247. # When a declaration arrives from syntax construction, it has
  248. # these problems:
  249. # * The innermost TypeDecl has no type (because the basic
  250. # type is only known at the uppermost declaration level)
  251. # * The declaration has no variable name, since that is saved
  252. # in the innermost TypeDecl
  253. # * The typename of the declaration is a list of type
  254. # specifiers, and not a node. Here, basic identifier types
  255. # should be separated from more complex types like enums
  256. # and structs.
  257. #
  258. # This method fixes these problems.
  259. def _fix_decl_name_type(self, decl, typename):
  260. """ Fixes a declaration. Modifies decl.
  261. """
  262. # Reach the underlying basic type
  263. #
  264. type = decl
  265. while not isinstance(type, c_ast.TypeDecl):
  266. type = type.type
  267. decl.name = type.declname
  268. type.quals = decl.quals[:]
  269. # The typename is a list of types. If any type in this
  270. # list isn't an IdentifierType, it must be the only
  271. # type in the list (it's illegal to declare "int enum ..")
  272. # If all the types are basic, they're collected in the
  273. # IdentifierType holder.
  274. for tn in typename:
  275. if not isinstance(tn, c_ast.IdentifierType):
  276. if len(typename) > 1:
  277. self._parse_error(
  278. "Invalid multiple types specified", tn.coord)
  279. else:
  280. type.type = tn
  281. return decl
  282. if not typename:
  283. # Functions default to returning int
  284. #
  285. if not isinstance(decl.type, c_ast.FuncDecl):
  286. self._parse_error(
  287. "Missing type in declaration", decl.coord)
  288. type.type = c_ast.IdentifierType(
  289. ['int'],
  290. coord=decl.coord)
  291. else:
  292. # At this point, we know that typename is a list of IdentifierType
  293. # nodes. Concatenate all the names into a single list.
  294. #
  295. type.type = c_ast.IdentifierType(
  296. [name for id in typename for name in id.names],
  297. coord=typename[0].coord)
  298. return decl
  299. def _add_declaration_specifier(self, declspec, newspec, kind, append=False):
  300. """ Declaration specifiers are represented by a dictionary
  301. with the entries:
  302. * qual: a list of type qualifiers
  303. * storage: a list of storage type qualifiers
  304. * type: a list of type specifiers
  305. * function: a list of function specifiers
  306. * alignment: a list of alignment specifiers
  307. This method is given a declaration specifier, and a
  308. new specifier of a given kind.
  309. If `append` is True, the new specifier is added to the end of
  310. the specifiers list, otherwise it's added at the beginning.
  311. Returns the declaration specifier, with the new
  312. specifier incorporated.
  313. """
  314. spec = declspec or dict(qual=[], storage=[], type=[], function=[], alignment=[])
  315. if append:
  316. spec[kind].append(newspec)
  317. else:
  318. spec[kind].insert(0, newspec)
  319. return spec
  320. def _build_declarations(self, spec, decls, typedef_namespace=False):
  321. """ Builds a list of declarations all sharing the given specifiers.
  322. If typedef_namespace is true, each declared name is added
  323. to the "typedef namespace", which also includes objects,
  324. functions, and enum constants.
  325. """
  326. is_typedef = 'typedef' in spec['storage']
  327. declarations = []
  328. # Bit-fields are allowed to be unnamed.
  329. if decls[0].get('bitsize') is not None:
  330. pass
  331. # When redeclaring typedef names as identifiers in inner scopes, a
  332. # problem can occur where the identifier gets grouped into
  333. # spec['type'], leaving decl as None. This can only occur for the
  334. # first declarator.
  335. elif decls[0]['decl'] is None:
  336. if len(spec['type']) < 2 or len(spec['type'][-1].names) != 1 or \
  337. not self._is_type_in_scope(spec['type'][-1].names[0]):
  338. coord = '?'
  339. for t in spec['type']:
  340. if hasattr(t, 'coord'):
  341. coord = t.coord
  342. break
  343. self._parse_error('Invalid declaration', coord)
  344. # Make this look as if it came from "direct_declarator:ID"
  345. decls[0]['decl'] = c_ast.TypeDecl(
  346. declname=spec['type'][-1].names[0],
  347. type=None,
  348. quals=None,
  349. align=spec['alignment'],
  350. coord=spec['type'][-1].coord)
  351. # Remove the "new" type's name from the end of spec['type']
  352. del spec['type'][-1]
  353. # A similar problem can occur where the declaration ends up looking
  354. # like an abstract declarator. Give it a name if this is the case.
  355. elif not isinstance(decls[0]['decl'], (
  356. c_ast.Enum, c_ast.Struct, c_ast.Union, c_ast.IdentifierType)):
  357. decls_0_tail = decls[0]['decl']
  358. while not isinstance(decls_0_tail, c_ast.TypeDecl):
  359. decls_0_tail = decls_0_tail.type
  360. if decls_0_tail.declname is None:
  361. decls_0_tail.declname = spec['type'][-1].names[0]
  362. del spec['type'][-1]
  363. for decl in decls:
  364. assert decl['decl'] is not None
  365. if is_typedef:
  366. declaration = c_ast.Typedef(
  367. name=None,
  368. quals=spec['qual'],
  369. storage=spec['storage'],
  370. type=decl['decl'],
  371. coord=decl['decl'].coord)
  372. else:
  373. declaration = c_ast.Decl(
  374. name=None,
  375. quals=spec['qual'],
  376. align=spec['alignment'],
  377. storage=spec['storage'],
  378. funcspec=spec['function'],
  379. type=decl['decl'],
  380. init=decl.get('init'),
  381. bitsize=decl.get('bitsize'),
  382. coord=decl['decl'].coord)
  383. if isinstance(declaration.type, (
  384. c_ast.Enum, c_ast.Struct, c_ast.Union,
  385. c_ast.IdentifierType)):
  386. fixed_decl = declaration
  387. else:
  388. fixed_decl = self._fix_decl_name_type(declaration, spec['type'])
  389. # Add the type name defined by typedef to a
  390. # symbol table (for usage in the lexer)
  391. if typedef_namespace:
  392. if is_typedef:
  393. self._add_typedef_name(fixed_decl.name, fixed_decl.coord)
  394. else:
  395. self._add_identifier(fixed_decl.name, fixed_decl.coord)
  396. fixed_decl = fix_atomic_specifiers(fixed_decl)
  397. declarations.append(fixed_decl)
  398. return declarations
  399. def _build_function_definition(self, spec, decl, param_decls, body):
  400. """ Builds a function definition.
  401. """
  402. if 'typedef' in spec['storage']:
  403. self._parse_error("Invalid typedef", decl.coord)
  404. declaration = self._build_declarations(
  405. spec=spec,
  406. decls=[dict(decl=decl, init=None)],
  407. typedef_namespace=True)[0]
  408. return c_ast.FuncDef(
  409. decl=declaration,
  410. param_decls=param_decls,
  411. body=body,
  412. coord=decl.coord)
  413. def _select_struct_union_class(self, token):
  414. """ Given a token (either STRUCT or UNION), selects the
  415. appropriate AST class.
  416. """
  417. if token == 'struct':
  418. return c_ast.Struct
  419. else:
  420. return c_ast.Union
  421. ##
  422. ## Precedence and associativity of operators
  423. ##
  424. # If this changes, c_generator.CGenerator.precedence_map needs to change as
  425. # well
  426. precedence = (
  427. ('left', 'LOR'),
  428. ('left', 'LAND'),
  429. ('left', 'OR'),
  430. ('left', 'XOR'),
  431. ('left', 'AND'),
  432. ('left', 'EQ', 'NE'),
  433. ('left', 'GT', 'GE', 'LT', 'LE'),
  434. ('left', 'RSHIFT', 'LSHIFT'),
  435. ('left', 'PLUS', 'MINUS'),
  436. ('left', 'TIMES', 'DIVIDE', 'MOD')
  437. )
  438. ##
  439. ## Grammar productions
  440. ## Implementation of the BNF defined in K&R2 A.13
  441. ##
  442. # Wrapper around a translation unit, to allow for empty input.
  443. # Not strictly part of the C99 Grammar, but useful in practice.
  444. def p_translation_unit_or_empty(self, p):
  445. """ translation_unit_or_empty : translation_unit
  446. | empty
  447. """
  448. if p[1] is None:
  449. p[0] = c_ast.FileAST([])
  450. else:
  451. p[0] = c_ast.FileAST(p[1])
  452. def p_translation_unit_1(self, p):
  453. """ translation_unit : external_declaration
  454. """
  455. # Note: external_declaration is already a list
  456. p[0] = p[1]
  457. def p_translation_unit_2(self, p):
  458. """ translation_unit : translation_unit external_declaration
  459. """
  460. p[1].extend(p[2])
  461. p[0] = p[1]
  462. # Declarations always come as lists (because they can be
  463. # several in one line), so we wrap the function definition
  464. # into a list as well, to make the return value of
  465. # external_declaration homogeneous.
  466. def p_external_declaration_1(self, p):
  467. """ external_declaration : function_definition
  468. """
  469. p[0] = [p[1]]
  470. def p_external_declaration_2(self, p):
  471. """ external_declaration : declaration
  472. """
  473. p[0] = p[1]
  474. def p_external_declaration_3(self, p):
  475. """ external_declaration : pp_directive
  476. | pppragma_directive
  477. """
  478. p[0] = [p[1]]
  479. def p_external_declaration_4(self, p):
  480. """ external_declaration : SEMI
  481. """
  482. p[0] = []
  483. def p_external_declaration_5(self, p):
  484. """ external_declaration : static_assert
  485. """
  486. p[0] = p[1]
  487. def p_static_assert_declaration(self, p):
  488. """ static_assert : _STATIC_ASSERT LPAREN constant_expression COMMA unified_string_literal RPAREN
  489. | _STATIC_ASSERT LPAREN constant_expression RPAREN
  490. """
  491. if len(p) == 5:
  492. p[0] = [c_ast.StaticAssert(p[3], None, self._token_coord(p, 1))]
  493. else:
  494. p[0] = [c_ast.StaticAssert(p[3], p[5], self._token_coord(p, 1))]
  495. def p_pp_directive(self, p):
  496. """ pp_directive : PPHASH
  497. """
  498. self._parse_error('Directives not supported yet',
  499. self._token_coord(p, 1))
  500. # This encompasses two types of C99-compatible pragmas:
  501. # - The #pragma directive:
  502. # # pragma character_sequence
  503. # - The _Pragma unary operator:
  504. # _Pragma ( " string_literal " )
  505. def p_pppragma_directive(self, p):
  506. """ pppragma_directive : PPPRAGMA
  507. | PPPRAGMA PPPRAGMASTR
  508. | _PRAGMA LPAREN unified_string_literal RPAREN
  509. """
  510. if len(p) == 5:
  511. p[0] = c_ast.Pragma(p[3], self._token_coord(p, 2))
  512. elif len(p) == 3:
  513. p[0] = c_ast.Pragma(p[2], self._token_coord(p, 2))
  514. else:
  515. p[0] = c_ast.Pragma("", self._token_coord(p, 1))
  516. def p_pppragma_directive_list(self, p):
  517. """ pppragma_directive_list : pppragma_directive
  518. | pppragma_directive_list pppragma_directive
  519. """
  520. p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]
  521. # In function definitions, the declarator can be followed by
  522. # a declaration list, for old "K&R style" function definitios.
  523. def p_function_definition_1(self, p):
  524. """ function_definition : id_declarator declaration_list_opt compound_statement
  525. """
  526. # no declaration specifiers - 'int' becomes the default type
  527. spec = dict(
  528. qual=[],
  529. alignment=[],
  530. storage=[],
  531. type=[c_ast.IdentifierType(['int'],
  532. coord=self._token_coord(p, 1))],
  533. function=[])
  534. p[0] = self._build_function_definition(
  535. spec=spec,
  536. decl=p[1],
  537. param_decls=p[2],
  538. body=p[3])
  539. def p_function_definition_2(self, p):
  540. """ function_definition : declaration_specifiers id_declarator declaration_list_opt compound_statement
  541. """
  542. spec = p[1]
  543. p[0] = self._build_function_definition(
  544. spec=spec,
  545. decl=p[2],
  546. param_decls=p[3],
  547. body=p[4])
  548. # Note, according to C18 A.2.2 6.7.10 static_assert-declaration _Static_assert
  549. # is a declaration, not a statement. We additionally recognise it as a statement
  550. # to fix parsing of _Static_assert inside the functions.
  551. #
  552. def p_statement(self, p):
  553. """ statement : labeled_statement
  554. | expression_statement
  555. | compound_statement
  556. | selection_statement
  557. | iteration_statement
  558. | jump_statement
  559. | pppragma_directive
  560. | static_assert
  561. """
  562. p[0] = p[1]
  563. # A pragma is generally considered a decorator rather than an actual
  564. # statement. Still, for the purposes of analyzing an abstract syntax tree of
  565. # C code, pragma's should not be ignored and were previously treated as a
  566. # statement. This presents a problem for constructs that take a statement
  567. # such as labeled_statements, selection_statements, and
  568. # iteration_statements, causing a misleading structure in the AST. For
  569. # example, consider the following C code.
  570. #
  571. # for (int i = 0; i < 3; i++)
  572. # #pragma omp critical
  573. # sum += 1;
  574. #
  575. # This code will compile and execute "sum += 1;" as the body of the for
  576. # loop. Previous implementations of PyCParser would render the AST for this
  577. # block of code as follows:
  578. #
  579. # For:
  580. # DeclList:
  581. # Decl: i, [], [], []
  582. # TypeDecl: i, []
  583. # IdentifierType: ['int']
  584. # Constant: int, 0
  585. # BinaryOp: <
  586. # ID: i
  587. # Constant: int, 3
  588. # UnaryOp: p++
  589. # ID: i
  590. # Pragma: omp critical
  591. # Assignment: +=
  592. # ID: sum
  593. # Constant: int, 1
  594. #
  595. # This AST misleadingly takes the Pragma as the body of the loop and the
  596. # assignment then becomes a sibling of the loop.
  597. #
  598. # To solve edge cases like these, the pragmacomp_or_statement rule groups
  599. # a pragma and its following statement (which would otherwise be orphaned)
  600. # using a compound block, effectively turning the above code into:
  601. #
  602. # for (int i = 0; i < 3; i++) {
  603. # #pragma omp critical
  604. # sum += 1;
  605. # }
  606. def p_pragmacomp_or_statement(self, p):
  607. """ pragmacomp_or_statement : pppragma_directive_list statement
  608. | statement
  609. """
  610. if len(p) == 3:
  611. p[0] = c_ast.Compound(
  612. block_items=p[1]+[p[2]],
  613. coord=self._token_coord(p, 1))
  614. else:
  615. p[0] = p[1]
  616. # In C, declarations can come several in a line:
  617. # int x, *px, romulo = 5;
  618. #
  619. # However, for the AST, we will split them to separate Decl
  620. # nodes.
  621. #
  622. # This rule splits its declarations and always returns a list
  623. # of Decl nodes, even if it's one element long.
  624. #
  625. def p_decl_body(self, p):
  626. """ decl_body : declaration_specifiers init_declarator_list_opt
  627. | declaration_specifiers_no_type id_init_declarator_list_opt
  628. """
  629. spec = p[1]
  630. # p[2] (init_declarator_list_opt) is either a list or None
  631. #
  632. if p[2] is None:
  633. # By the standard, you must have at least one declarator unless
  634. # declaring a structure tag, a union tag, or the members of an
  635. # enumeration.
  636. #
  637. ty = spec['type']
  638. s_u_or_e = (c_ast.Struct, c_ast.Union, c_ast.Enum)
  639. if len(ty) == 1 and isinstance(ty[0], s_u_or_e):
  640. decls = [c_ast.Decl(
  641. name=None,
  642. quals=spec['qual'],
  643. align=spec['alignment'],
  644. storage=spec['storage'],
  645. funcspec=spec['function'],
  646. type=ty[0],
  647. init=None,
  648. bitsize=None,
  649. coord=ty[0].coord)]
  650. # However, this case can also occur on redeclared identifiers in
  651. # an inner scope. The trouble is that the redeclared type's name
  652. # gets grouped into declaration_specifiers; _build_declarations
  653. # compensates for this.
  654. #
  655. else:
  656. decls = self._build_declarations(
  657. spec=spec,
  658. decls=[dict(decl=None, init=None)],
  659. typedef_namespace=True)
  660. else:
  661. decls = self._build_declarations(
  662. spec=spec,
  663. decls=p[2],
  664. typedef_namespace=True)
  665. p[0] = decls
  666. # The declaration has been split to a decl_body sub-rule and
  667. # SEMI, because having them in a single rule created a problem
  668. # for defining typedefs.
  669. #
  670. # If a typedef line was directly followed by a line using the
  671. # type defined with the typedef, the type would not be
  672. # recognized. This is because to reduce the declaration rule,
  673. # the parser's lookahead asked for the token after SEMI, which
  674. # was the type from the next line, and the lexer had no chance
  675. # to see the updated type symbol table.
  676. #
  677. # Splitting solves this problem, because after seeing SEMI,
  678. # the parser reduces decl_body, which actually adds the new
  679. # type into the table to be seen by the lexer before the next
  680. # line is reached.
  681. def p_declaration(self, p):
  682. """ declaration : decl_body SEMI
  683. """
  684. p[0] = p[1]
  685. # Since each declaration is a list of declarations, this
  686. # rule will combine all the declarations and return a single
  687. # list
  688. #
  689. def p_declaration_list(self, p):
  690. """ declaration_list : declaration
  691. | declaration_list declaration
  692. """
  693. p[0] = p[1] if len(p) == 2 else p[1] + p[2]
  694. # To know when declaration-specifiers end and declarators begin,
  695. # we require declaration-specifiers to have at least one
  696. # type-specifier, and disallow typedef-names after we've seen any
  697. # type-specifier. These are both required by the spec.
  698. #
  699. def p_declaration_specifiers_no_type_1(self, p):
  700. """ declaration_specifiers_no_type : type_qualifier declaration_specifiers_no_type_opt
  701. """
  702. p[0] = self._add_declaration_specifier(p[2], p[1], 'qual')
  703. def p_declaration_specifiers_no_type_2(self, p):
  704. """ declaration_specifiers_no_type : storage_class_specifier declaration_specifiers_no_type_opt
  705. """
  706. p[0] = self._add_declaration_specifier(p[2], p[1], 'storage')
  707. def p_declaration_specifiers_no_type_3(self, p):
  708. """ declaration_specifiers_no_type : function_specifier declaration_specifiers_no_type_opt
  709. """
  710. p[0] = self._add_declaration_specifier(p[2], p[1], 'function')
  711. # Without this, `typedef _Atomic(T) U` will parse incorrectly because the
  712. # _Atomic qualifier will match, instead of the specifier.
  713. def p_declaration_specifiers_no_type_4(self, p):
  714. """ declaration_specifiers_no_type : atomic_specifier declaration_specifiers_no_type_opt
  715. """
  716. p[0] = self._add_declaration_specifier(p[2], p[1], 'type')
  717. def p_declaration_specifiers_no_type_5(self, p):
  718. """ declaration_specifiers_no_type : alignment_specifier declaration_specifiers_no_type_opt
  719. """
  720. p[0] = self._add_declaration_specifier(p[2], p[1], 'alignment')
  721. def p_declaration_specifiers_1(self, p):
  722. """ declaration_specifiers : declaration_specifiers type_qualifier
  723. """
  724. p[0] = self._add_declaration_specifier(p[1], p[2], 'qual', append=True)
  725. def p_declaration_specifiers_2(self, p):
  726. """ declaration_specifiers : declaration_specifiers storage_class_specifier
  727. """
  728. p[0] = self._add_declaration_specifier(p[1], p[2], 'storage', append=True)
  729. def p_declaration_specifiers_3(self, p):
  730. """ declaration_specifiers : declaration_specifiers function_specifier
  731. """
  732. p[0] = self._add_declaration_specifier(p[1], p[2], 'function', append=True)
  733. def p_declaration_specifiers_4(self, p):
  734. """ declaration_specifiers : declaration_specifiers type_specifier_no_typeid
  735. """
  736. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  737. def p_declaration_specifiers_5(self, p):
  738. """ declaration_specifiers : type_specifier
  739. """
  740. p[0] = self._add_declaration_specifier(None, p[1], 'type')
  741. def p_declaration_specifiers_6(self, p):
  742. """ declaration_specifiers : declaration_specifiers_no_type type_specifier
  743. """
  744. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  745. def p_declaration_specifiers_7(self, p):
  746. """ declaration_specifiers : declaration_specifiers alignment_specifier
  747. """
  748. p[0] = self._add_declaration_specifier(p[1], p[2], 'alignment', append=True)
  749. def p_storage_class_specifier(self, p):
  750. """ storage_class_specifier : AUTO
  751. | REGISTER
  752. | STATIC
  753. | EXTERN
  754. | TYPEDEF
  755. | _THREAD_LOCAL
  756. """
  757. p[0] = p[1]
  758. def p_function_specifier(self, p):
  759. """ function_specifier : INLINE
  760. | _NORETURN
  761. """
  762. p[0] = p[1]
  763. def p_type_specifier_no_typeid(self, p):
  764. """ type_specifier_no_typeid : VOID
  765. | _BOOL
  766. | CHAR
  767. | SHORT
  768. | INT
  769. | LONG
  770. | FLOAT
  771. | DOUBLE
  772. | _COMPLEX
  773. | SIGNED
  774. | UNSIGNED
  775. | __INT128
  776. """
  777. p[0] = c_ast.IdentifierType([p[1]], coord=self._token_coord(p, 1))
  778. def p_type_specifier(self, p):
  779. """ type_specifier : typedef_name
  780. | enum_specifier
  781. | struct_or_union_specifier
  782. | type_specifier_no_typeid
  783. | atomic_specifier
  784. """
  785. p[0] = p[1]
  786. # See section 6.7.2.4 of the C11 standard.
  787. def p_atomic_specifier(self, p):
  788. """ atomic_specifier : _ATOMIC LPAREN type_name RPAREN
  789. """
  790. typ = p[3]
  791. typ.quals.append('_Atomic')
  792. p[0] = typ
  793. def p_type_qualifier(self, p):
  794. """ type_qualifier : CONST
  795. | RESTRICT
  796. | VOLATILE
  797. | _ATOMIC
  798. """
  799. p[0] = p[1]
  800. def p_init_declarator_list(self, p):
  801. """ init_declarator_list : init_declarator
  802. | init_declarator_list COMMA init_declarator
  803. """
  804. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  805. # Returns a {decl=<declarator> : init=<initializer>} dictionary
  806. # If there's no initializer, uses None
  807. #
  808. def p_init_declarator(self, p):
  809. """ init_declarator : declarator
  810. | declarator EQUALS initializer
  811. """
  812. p[0] = dict(decl=p[1], init=(p[3] if len(p) > 2 else None))
  813. def p_id_init_declarator_list(self, p):
  814. """ id_init_declarator_list : id_init_declarator
  815. | id_init_declarator_list COMMA init_declarator
  816. """
  817. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  818. def p_id_init_declarator(self, p):
  819. """ id_init_declarator : id_declarator
  820. | id_declarator EQUALS initializer
  821. """
  822. p[0] = dict(decl=p[1], init=(p[3] if len(p) > 2 else None))
  823. # Require at least one type specifier in a specifier-qualifier-list
  824. #
  825. def p_specifier_qualifier_list_1(self, p):
  826. """ specifier_qualifier_list : specifier_qualifier_list type_specifier_no_typeid
  827. """
  828. p[0] = self._add_declaration_specifier(p[1], p[2], 'type', append=True)
  829. def p_specifier_qualifier_list_2(self, p):
  830. """ specifier_qualifier_list : specifier_qualifier_list type_qualifier
  831. """
  832. p[0] = self._add_declaration_specifier(p[1], p[2], 'qual', append=True)
  833. def p_specifier_qualifier_list_3(self, p):
  834. """ specifier_qualifier_list : type_specifier
  835. """
  836. p[0] = self._add_declaration_specifier(None, p[1], 'type')
  837. def p_specifier_qualifier_list_4(self, p):
  838. """ specifier_qualifier_list : type_qualifier_list type_specifier
  839. """
  840. p[0] = dict(qual=p[1], alignment=[], storage=[], type=[p[2]], function=[])
  841. def p_specifier_qualifier_list_5(self, p):
  842. """ specifier_qualifier_list : alignment_specifier
  843. """
  844. p[0] = dict(qual=[], alignment=[p[1]], storage=[], type=[], function=[])
  845. def p_specifier_qualifier_list_6(self, p):
  846. """ specifier_qualifier_list : specifier_qualifier_list alignment_specifier
  847. """
  848. p[0] = self._add_declaration_specifier(p[1], p[2], 'alignment')
  849. # TYPEID is allowed here (and in other struct/enum related tag names), because
  850. # struct/enum tags reside in their own namespace and can be named the same as types
  851. #
  852. def p_struct_or_union_specifier_1(self, p):
  853. """ struct_or_union_specifier : struct_or_union ID
  854. | struct_or_union TYPEID
  855. """
  856. klass = self._select_struct_union_class(p[1])
  857. # None means no list of members
  858. p[0] = klass(
  859. name=p[2],
  860. decls=None,
  861. coord=self._token_coord(p, 2))
  862. def p_struct_or_union_specifier_2(self, p):
  863. """ struct_or_union_specifier : struct_or_union brace_open struct_declaration_list brace_close
  864. | struct_or_union brace_open brace_close
  865. """
  866. klass = self._select_struct_union_class(p[1])
  867. if len(p) == 4:
  868. # Empty sequence means an empty list of members
  869. p[0] = klass(
  870. name=None,
  871. decls=[],
  872. coord=self._token_coord(p, 2))
  873. else:
  874. p[0] = klass(
  875. name=None,
  876. decls=p[3],
  877. coord=self._token_coord(p, 2))
  878. def p_struct_or_union_specifier_3(self, p):
  879. """ struct_or_union_specifier : struct_or_union ID brace_open struct_declaration_list brace_close
  880. | struct_or_union ID brace_open brace_close
  881. | struct_or_union TYPEID brace_open struct_declaration_list brace_close
  882. | struct_or_union TYPEID brace_open brace_close
  883. """
  884. klass = self._select_struct_union_class(p[1])
  885. if len(p) == 5:
  886. # Empty sequence means an empty list of members
  887. p[0] = klass(
  888. name=p[2],
  889. decls=[],
  890. coord=self._token_coord(p, 2))
  891. else:
  892. p[0] = klass(
  893. name=p[2],
  894. decls=p[4],
  895. coord=self._token_coord(p, 2))
  896. def p_struct_or_union(self, p):
  897. """ struct_or_union : STRUCT
  898. | UNION
  899. """
  900. p[0] = p[1]
  901. # Combine all declarations into a single list
  902. #
  903. def p_struct_declaration_list(self, p):
  904. """ struct_declaration_list : struct_declaration
  905. | struct_declaration_list struct_declaration
  906. """
  907. if len(p) == 2:
  908. p[0] = p[1] or []
  909. else:
  910. p[0] = p[1] + (p[2] or [])
  911. def p_struct_declaration_1(self, p):
  912. """ struct_declaration : specifier_qualifier_list struct_declarator_list_opt SEMI
  913. """
  914. spec = p[1]
  915. assert 'typedef' not in spec['storage']
  916. if p[2] is not None:
  917. decls = self._build_declarations(
  918. spec=spec,
  919. decls=p[2])
  920. elif len(spec['type']) == 1:
  921. # Anonymous struct/union, gcc extension, C1x feature.
  922. # Although the standard only allows structs/unions here, I see no
  923. # reason to disallow other types since some compilers have typedefs
  924. # here, and pycparser isn't about rejecting all invalid code.
  925. #
  926. node = spec['type'][0]
  927. if isinstance(node, c_ast.Node):
  928. decl_type = node
  929. else:
  930. decl_type = c_ast.IdentifierType(node)
  931. decls = self._build_declarations(
  932. spec=spec,
  933. decls=[dict(decl=decl_type)])
  934. else:
  935. # Structure/union members can have the same names as typedefs.
  936. # The trouble is that the member's name gets grouped into
  937. # specifier_qualifier_list; _build_declarations compensates.
  938. #
  939. decls = self._build_declarations(
  940. spec=spec,
  941. decls=[dict(decl=None, init=None)])
  942. p[0] = decls
  943. def p_struct_declaration_2(self, p):
  944. """ struct_declaration : SEMI
  945. """
  946. p[0] = None
  947. def p_struct_declaration_3(self, p):
  948. """ struct_declaration : pppragma_directive
  949. """
  950. p[0] = [p[1]]
  951. def p_struct_declarator_list(self, p):
  952. """ struct_declarator_list : struct_declarator
  953. | struct_declarator_list COMMA struct_declarator
  954. """
  955. p[0] = p[1] + [p[3]] if len(p) == 4 else [p[1]]
  956. # struct_declarator passes up a dict with the keys: decl (for
  957. # the underlying declarator) and bitsize (for the bitsize)
  958. #
  959. def p_struct_declarator_1(self, p):
  960. """ struct_declarator : declarator
  961. """
  962. p[0] = {'decl': p[1], 'bitsize': None}
  963. def p_struct_declarator_2(self, p):
  964. """ struct_declarator : declarator COLON constant_expression
  965. | COLON constant_expression
  966. """
  967. if len(p) > 3:
  968. p[0] = {'decl': p[1], 'bitsize': p[3]}
  969. else:
  970. p[0] = {'decl': c_ast.TypeDecl(None, None, None, None), 'bitsize': p[2]}
  971. def p_enum_specifier_1(self, p):
  972. """ enum_specifier : ENUM ID
  973. | ENUM TYPEID
  974. """
  975. p[0] = c_ast.Enum(p[2], None, self._token_coord(p, 1))
  976. def p_enum_specifier_2(self, p):
  977. """ enum_specifier : ENUM brace_open enumerator_list brace_close
  978. """
  979. p[0] = c_ast.Enum(None, p[3], self._token_coord(p, 1))
  980. def p_enum_specifier_3(self, p):
  981. """ enum_specifier : ENUM ID brace_open enumerator_list brace_close
  982. | ENUM TYPEID brace_open enumerator_list brace_close
  983. """
  984. p[0] = c_ast.Enum(p[2], p[4], self._token_coord(p, 1))
  985. def p_enumerator_list(self, p):
  986. """ enumerator_list : enumerator
  987. | enumerator_list COMMA
  988. | enumerator_list COMMA enumerator
  989. """
  990. if len(p) == 2:
  991. p[0] = c_ast.EnumeratorList([p[1]], p[1].coord)
  992. elif len(p) == 3:
  993. p[0] = p[1]
  994. else:
  995. p[1].enumerators.append(p[3])
  996. p[0] = p[1]
  997. def p_alignment_specifier(self, p):
  998. """ alignment_specifier : _ALIGNAS LPAREN type_name RPAREN
  999. | _ALIGNAS LPAREN constant_expression RPAREN
  1000. """
  1001. p[0] = c_ast.Alignas(p[3], self._token_coord(p, 1))
  1002. def p_enumerator(self, p):
  1003. """ enumerator : ID
  1004. | ID EQUALS constant_expression
  1005. """
  1006. if len(p) == 2:
  1007. enumerator = c_ast.Enumerator(
  1008. p[1], None,
  1009. self._token_coord(p, 1))
  1010. else:
  1011. enumerator = c_ast.Enumerator(
  1012. p[1], p[3],
  1013. self._token_coord(p, 1))
  1014. self._add_identifier(enumerator.name, enumerator.coord)
  1015. p[0] = enumerator
  1016. def p_declarator(self, p):
  1017. """ declarator : id_declarator
  1018. | typeid_declarator
  1019. """
  1020. p[0] = p[1]
  1021. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1022. def p_xxx_declarator_1(self, p):
  1023. """ xxx_declarator : direct_xxx_declarator
  1024. """
  1025. p[0] = p[1]
  1026. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1027. def p_xxx_declarator_2(self, p):
  1028. """ xxx_declarator : pointer direct_xxx_declarator
  1029. """
  1030. p[0] = self._type_modify_decl(p[2], p[1])
  1031. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1032. def p_direct_xxx_declarator_1(self, p):
  1033. """ direct_xxx_declarator : yyy
  1034. """
  1035. p[0] = c_ast.TypeDecl(
  1036. declname=p[1],
  1037. type=None,
  1038. quals=None,
  1039. align=None,
  1040. coord=self._token_coord(p, 1))
  1041. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'))
  1042. def p_direct_xxx_declarator_2(self, p):
  1043. """ direct_xxx_declarator : LPAREN xxx_declarator RPAREN
  1044. """
  1045. p[0] = p[2]
  1046. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1047. def p_direct_xxx_declarator_3(self, p):
  1048. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET
  1049. """
  1050. quals = (p[3] if len(p) > 5 else []) or []
  1051. # Accept dimension qualifiers
  1052. # Per C99 6.7.5.3 p7
  1053. arr = c_ast.ArrayDecl(
  1054. type=None,
  1055. dim=p[4] if len(p) > 5 else p[3],
  1056. dim_quals=quals,
  1057. coord=p[1].coord)
  1058. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1059. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1060. def p_direct_xxx_declarator_4(self, p):
  1061. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET STATIC type_qualifier_list_opt assignment_expression RBRACKET
  1062. | direct_xxx_declarator LBRACKET type_qualifier_list STATIC assignment_expression RBRACKET
  1063. """
  1064. # Using slice notation for PLY objects doesn't work in Python 3 for the
  1065. # version of PLY embedded with pycparser; see PLY Google Code issue 30.
  1066. # Work around that here by listing the two elements separately.
  1067. listed_quals = [item if isinstance(item, list) else [item]
  1068. for item in [p[3],p[4]]]
  1069. dim_quals = [qual for sublist in listed_quals for qual in sublist
  1070. if qual is not None]
  1071. arr = c_ast.ArrayDecl(
  1072. type=None,
  1073. dim=p[5],
  1074. dim_quals=dim_quals,
  1075. coord=p[1].coord)
  1076. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1077. # Special for VLAs
  1078. #
  1079. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1080. def p_direct_xxx_declarator_5(self, p):
  1081. """ direct_xxx_declarator : direct_xxx_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET
  1082. """
  1083. arr = c_ast.ArrayDecl(
  1084. type=None,
  1085. dim=c_ast.ID(p[4], self._token_coord(p, 4)),
  1086. dim_quals=p[3] if p[3] is not None else [],
  1087. coord=p[1].coord)
  1088. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1089. @parameterized(('id', 'ID'), ('typeid', 'TYPEID'), ('typeid_noparen', 'TYPEID'))
  1090. def p_direct_xxx_declarator_6(self, p):
  1091. """ direct_xxx_declarator : direct_xxx_declarator LPAREN parameter_type_list RPAREN
  1092. | direct_xxx_declarator LPAREN identifier_list_opt RPAREN
  1093. """
  1094. func = c_ast.FuncDecl(
  1095. args=p[3],
  1096. type=None,
  1097. coord=p[1].coord)
  1098. # To see why _get_yacc_lookahead_token is needed, consider:
  1099. # typedef char TT;
  1100. # void foo(int TT) { TT = 10; }
  1101. # Outside the function, TT is a typedef, but inside (starting and
  1102. # ending with the braces) it's a parameter. The trouble begins with
  1103. # yacc's lookahead token. We don't know if we're declaring or
  1104. # defining a function until we see LBRACE, but if we wait for yacc to
  1105. # trigger a rule on that token, then TT will have already been read
  1106. # and incorrectly interpreted as TYPEID. We need to add the
  1107. # parameters to the scope the moment the lexer sees LBRACE.
  1108. #
  1109. if self._get_yacc_lookahead_token().type == "LBRACE":
  1110. if func.args is not None:
  1111. for param in func.args.params:
  1112. if isinstance(param, c_ast.EllipsisParam): break
  1113. self._add_identifier(param.name, param.coord)
  1114. p[0] = self._type_modify_decl(decl=p[1], modifier=func)
  1115. def p_pointer(self, p):
  1116. """ pointer : TIMES type_qualifier_list_opt
  1117. | TIMES type_qualifier_list_opt pointer
  1118. """
  1119. coord = self._token_coord(p, 1)
  1120. # Pointer decls nest from inside out. This is important when different
  1121. # levels have different qualifiers. For example:
  1122. #
  1123. # char * const * p;
  1124. #
  1125. # Means "pointer to const pointer to char"
  1126. #
  1127. # While:
  1128. #
  1129. # char ** const p;
  1130. #
  1131. # Means "const pointer to pointer to char"
  1132. #
  1133. # So when we construct PtrDecl nestings, the leftmost pointer goes in
  1134. # as the most nested type.
  1135. nested_type = c_ast.PtrDecl(quals=p[2] or [], type=None, coord=coord)
  1136. if len(p) > 3:
  1137. tail_type = p[3]
  1138. while tail_type.type is not None:
  1139. tail_type = tail_type.type
  1140. tail_type.type = nested_type
  1141. p[0] = p[3]
  1142. else:
  1143. p[0] = nested_type
  1144. def p_type_qualifier_list(self, p):
  1145. """ type_qualifier_list : type_qualifier
  1146. | type_qualifier_list type_qualifier
  1147. """
  1148. p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]
  1149. def p_parameter_type_list(self, p):
  1150. """ parameter_type_list : parameter_list
  1151. | parameter_list COMMA ELLIPSIS
  1152. """
  1153. if len(p) > 2:
  1154. p[1].params.append(c_ast.EllipsisParam(self._token_coord(p, 3)))
  1155. p[0] = p[1]
  1156. def p_parameter_list(self, p):
  1157. """ parameter_list : parameter_declaration
  1158. | parameter_list COMMA parameter_declaration
  1159. """
  1160. if len(p) == 2: # single parameter
  1161. p[0] = c_ast.ParamList([p[1]], p[1].coord)
  1162. else:
  1163. p[1].params.append(p[3])
  1164. p[0] = p[1]
  1165. # From ISO/IEC 9899:TC2, 6.7.5.3.11:
  1166. # "If, in a parameter declaration, an identifier can be treated either
  1167. # as a typedef name or as a parameter name, it shall be taken as a
  1168. # typedef name."
  1169. #
  1170. # Inside a parameter declaration, once we've reduced declaration specifiers,
  1171. # if we shift in an LPAREN and see a TYPEID, it could be either an abstract
  1172. # declarator or a declarator nested inside parens. This rule tells us to
  1173. # always treat it as an abstract declarator. Therefore, we only accept
  1174. # `id_declarator`s and `typeid_noparen_declarator`s.
  1175. def p_parameter_declaration_1(self, p):
  1176. """ parameter_declaration : declaration_specifiers id_declarator
  1177. | declaration_specifiers typeid_noparen_declarator
  1178. """
  1179. spec = p[1]
  1180. if not spec['type']:
  1181. spec['type'] = [c_ast.IdentifierType(['int'],
  1182. coord=self._token_coord(p, 1))]
  1183. p[0] = self._build_declarations(
  1184. spec=spec,
  1185. decls=[dict(decl=p[2])])[0]
  1186. def p_parameter_declaration_2(self, p):
  1187. """ parameter_declaration : declaration_specifiers abstract_declarator_opt
  1188. """
  1189. spec = p[1]
  1190. if not spec['type']:
  1191. spec['type'] = [c_ast.IdentifierType(['int'],
  1192. coord=self._token_coord(p, 1))]
  1193. # Parameters can have the same names as typedefs. The trouble is that
  1194. # the parameter's name gets grouped into declaration_specifiers, making
  1195. # it look like an old-style declaration; compensate.
  1196. #
  1197. if len(spec['type']) > 1 and len(spec['type'][-1].names) == 1 and \
  1198. self._is_type_in_scope(spec['type'][-1].names[0]):
  1199. decl = self._build_declarations(
  1200. spec=spec,
  1201. decls=[dict(decl=p[2], init=None)])[0]
  1202. # This truly is an old-style parameter declaration
  1203. #
  1204. else:
  1205. decl = c_ast.Typename(
  1206. name='',
  1207. quals=spec['qual'],
  1208. align=None,
  1209. type=p[2] or c_ast.TypeDecl(None, None, None, None),
  1210. coord=self._token_coord(p, 2))
  1211. typename = spec['type']
  1212. decl = self._fix_decl_name_type(decl, typename)
  1213. p[0] = decl
  1214. def p_identifier_list(self, p):
  1215. """ identifier_list : identifier
  1216. | identifier_list COMMA identifier
  1217. """
  1218. if len(p) == 2: # single parameter
  1219. p[0] = c_ast.ParamList([p[1]], p[1].coord)
  1220. else:
  1221. p[1].params.append(p[3])
  1222. p[0] = p[1]
  1223. def p_initializer_1(self, p):
  1224. """ initializer : assignment_expression
  1225. """
  1226. p[0] = p[1]
  1227. def p_initializer_2(self, p):
  1228. """ initializer : brace_open initializer_list_opt brace_close
  1229. | brace_open initializer_list COMMA brace_close
  1230. """
  1231. if p[2] is None:
  1232. p[0] = c_ast.InitList([], self._token_coord(p, 1))
  1233. else:
  1234. p[0] = p[2]
  1235. def p_initializer_list(self, p):
  1236. """ initializer_list : designation_opt initializer
  1237. | initializer_list COMMA designation_opt initializer
  1238. """
  1239. if len(p) == 3: # single initializer
  1240. init = p[2] if p[1] is None else c_ast.NamedInitializer(p[1], p[2])
  1241. p[0] = c_ast.InitList([init], p[2].coord)
  1242. else:
  1243. init = p[4] if p[3] is None else c_ast.NamedInitializer(p[3], p[4])
  1244. p[1].exprs.append(init)
  1245. p[0] = p[1]
  1246. def p_designation(self, p):
  1247. """ designation : designator_list EQUALS
  1248. """
  1249. p[0] = p[1]
  1250. # Designators are represented as a list of nodes, in the order in which
  1251. # they're written in the code.
  1252. #
  1253. def p_designator_list(self, p):
  1254. """ designator_list : designator
  1255. | designator_list designator
  1256. """
  1257. p[0] = [p[1]] if len(p) == 2 else p[1] + [p[2]]
  1258. def p_designator(self, p):
  1259. """ designator : LBRACKET constant_expression RBRACKET
  1260. | PERIOD identifier
  1261. """
  1262. p[0] = p[2]
  1263. def p_type_name(self, p):
  1264. """ type_name : specifier_qualifier_list abstract_declarator_opt
  1265. """
  1266. typename = c_ast.Typename(
  1267. name='',
  1268. quals=p[1]['qual'][:],
  1269. align=None,
  1270. type=p[2] or c_ast.TypeDecl(None, None, None, None),
  1271. coord=self._token_coord(p, 2))
  1272. p[0] = self._fix_decl_name_type(typename, p[1]['type'])
  1273. def p_abstract_declarator_1(self, p):
  1274. """ abstract_declarator : pointer
  1275. """
  1276. dummytype = c_ast.TypeDecl(None, None, None, None)
  1277. p[0] = self._type_modify_decl(
  1278. decl=dummytype,
  1279. modifier=p[1])
  1280. def p_abstract_declarator_2(self, p):
  1281. """ abstract_declarator : pointer direct_abstract_declarator
  1282. """
  1283. p[0] = self._type_modify_decl(p[2], p[1])
  1284. def p_abstract_declarator_3(self, p):
  1285. """ abstract_declarator : direct_abstract_declarator
  1286. """
  1287. p[0] = p[1]
  1288. # Creating and using direct_abstract_declarator_opt here
  1289. # instead of listing both direct_abstract_declarator and the
  1290. # lack of it in the beginning of _1 and _2 caused two
  1291. # shift/reduce errors.
  1292. #
  1293. def p_direct_abstract_declarator_1(self, p):
  1294. """ direct_abstract_declarator : LPAREN abstract_declarator RPAREN """
  1295. p[0] = p[2]
  1296. def p_direct_abstract_declarator_2(self, p):
  1297. """ direct_abstract_declarator : direct_abstract_declarator LBRACKET assignment_expression_opt RBRACKET
  1298. """
  1299. arr = c_ast.ArrayDecl(
  1300. type=None,
  1301. dim=p[3],
  1302. dim_quals=[],
  1303. coord=p[1].coord)
  1304. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1305. def p_direct_abstract_declarator_3(self, p):
  1306. """ direct_abstract_declarator : LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET
  1307. """
  1308. quals = (p[2] if len(p) > 4 else []) or []
  1309. p[0] = c_ast.ArrayDecl(
  1310. type=c_ast.TypeDecl(None, None, None, None),
  1311. dim=p[3] if len(p) > 4 else p[2],
  1312. dim_quals=quals,
  1313. coord=self._token_coord(p, 1))
  1314. def p_direct_abstract_declarator_4(self, p):
  1315. """ direct_abstract_declarator : direct_abstract_declarator LBRACKET TIMES RBRACKET
  1316. """
  1317. arr = c_ast.ArrayDecl(
  1318. type=None,
  1319. dim=c_ast.ID(p[3], self._token_coord(p, 3)),
  1320. dim_quals=[],
  1321. coord=p[1].coord)
  1322. p[0] = self._type_modify_decl(decl=p[1], modifier=arr)
  1323. def p_direct_abstract_declarator_5(self, p):
  1324. """ direct_abstract_declarator : LBRACKET TIMES RBRACKET
  1325. """
  1326. p[0] = c_ast.ArrayDecl(
  1327. type=c_ast.TypeDecl(None, None, None, None),
  1328. dim=c_ast.ID(p[3], self._token_coord(p, 3)),
  1329. dim_quals=[],
  1330. coord=self._token_coord(p, 1))
  1331. def p_direct_abstract_declarator_6(self, p):
  1332. """ direct_abstract_declarator : direct_abstract_declarator LPAREN parameter_type_list_opt RPAREN
  1333. """
  1334. func = c_ast.FuncDecl(
  1335. args=p[3],
  1336. type=None,
  1337. coord=p[1].coord)
  1338. p[0] = self._type_modify_decl(decl=p[1], modifier=func)
  1339. def p_direct_abstract_declarator_7(self, p):
  1340. """ direct_abstract_declarator : LPAREN parameter_type_list_opt RPAREN
  1341. """
  1342. p[0] = c_ast.FuncDecl(
  1343. args=p[2],
  1344. type=c_ast.TypeDecl(None, None, None, None),
  1345. coord=self._token_coord(p, 1))
  1346. # declaration is a list, statement isn't. To make it consistent, block_item
  1347. # will always be a list
  1348. #
  1349. def p_block_item(self, p):
  1350. """ block_item : declaration
  1351. | statement
  1352. """
  1353. p[0] = p[1] if isinstance(p[1], list) else [p[1]]
  1354. # Since we made block_item a list, this just combines lists
  1355. #
  1356. def p_block_item_list(self, p):
  1357. """ block_item_list : block_item
  1358. | block_item_list block_item
  1359. """
  1360. # Empty block items (plain ';') produce [None], so ignore them
  1361. p[0] = p[1] if (len(p) == 2 or p[2] == [None]) else p[1] + p[2]
  1362. def p_compound_statement_1(self, p):
  1363. """ compound_statement : brace_open block_item_list_opt brace_close """
  1364. p[0] = c_ast.Compound(
  1365. block_items=p[2],
  1366. coord=self._token_coord(p, 1))
  1367. def p_labeled_statement_1(self, p):
  1368. """ labeled_statement : ID COLON pragmacomp_or_statement """
  1369. p[0] = c_ast.Label(p[1], p[3], self._token_coord(p, 1))
  1370. def p_labeled_statement_2(self, p):
  1371. """ labeled_statement : CASE constant_expression COLON pragmacomp_or_statement """
  1372. p[0] = c_ast.Case(p[2], [p[4]], self._token_coord(p, 1))
  1373. def p_labeled_statement_3(self, p):
  1374. """ labeled_statement : DEFAULT COLON pragmacomp_or_statement """
  1375. p[0] = c_ast.Default([p[3]], self._token_coord(p, 1))
  1376. def p_selection_statement_1(self, p):
  1377. """ selection_statement : IF LPAREN expression RPAREN pragmacomp_or_statement """
  1378. p[0] = c_ast.If(p[3], p[5], None, self._token_coord(p, 1))
  1379. def p_selection_statement_2(self, p):
  1380. """ selection_statement : IF LPAREN expression RPAREN statement ELSE pragmacomp_or_statement """
  1381. p[0] = c_ast.If(p[3], p[5], p[7], self._token_coord(p, 1))
  1382. def p_selection_statement_3(self, p):
  1383. """ selection_statement : SWITCH LPAREN expression RPAREN pragmacomp_or_statement """
  1384. p[0] = fix_switch_cases(
  1385. c_ast.Switch(p[3], p[5], self._token_coord(p, 1)))
  1386. def p_iteration_statement_1(self, p):
  1387. """ iteration_statement : WHILE LPAREN expression RPAREN pragmacomp_or_statement """
  1388. p[0] = c_ast.While(p[3], p[5], self._token_coord(p, 1))
  1389. def p_iteration_statement_2(self, p):
  1390. """ iteration_statement : DO pragmacomp_or_statement WHILE LPAREN expression RPAREN SEMI """
  1391. p[0] = c_ast.DoWhile(p[5], p[2], self._token_coord(p, 1))
  1392. def p_iteration_statement_3(self, p):
  1393. """ iteration_statement : FOR LPAREN expression_opt SEMI expression_opt SEMI expression_opt RPAREN pragmacomp_or_statement """
  1394. p[0] = c_ast.For(p[3], p[5], p[7], p[9], self._token_coord(p, 1))
  1395. def p_iteration_statement_4(self, p):
  1396. """ iteration_statement : FOR LPAREN declaration expression_opt SEMI expression_opt RPAREN pragmacomp_or_statement """
  1397. p[0] = c_ast.For(c_ast.DeclList(p[3], self._token_coord(p, 1)),
  1398. p[4], p[6], p[8], self._token_coord(p, 1))
  1399. def p_jump_statement_1(self, p):
  1400. """ jump_statement : GOTO ID SEMI """
  1401. p[0] = c_ast.Goto(p[2], self._token_coord(p, 1))
  1402. def p_jump_statement_2(self, p):
  1403. """ jump_statement : BREAK SEMI """
  1404. p[0] = c_ast.Break(self._token_coord(p, 1))
  1405. def p_jump_statement_3(self, p):
  1406. """ jump_statement : CONTINUE SEMI """
  1407. p[0] = c_ast.Continue(self._token_coord(p, 1))
  1408. def p_jump_statement_4(self, p):
  1409. """ jump_statement : RETURN expression SEMI
  1410. | RETURN SEMI
  1411. """
  1412. p[0] = c_ast.Return(p[2] if len(p) == 4 else None, self._token_coord(p, 1))
  1413. def p_expression_statement(self, p):
  1414. """ expression_statement : expression_opt SEMI """
  1415. if p[1] is None:
  1416. p[0] = c_ast.EmptyStatement(self._token_coord(p, 2))
  1417. else:
  1418. p[0] = p[1]
  1419. def p_expression(self, p):
  1420. """ expression : assignment_expression
  1421. | expression COMMA assignment_expression
  1422. """
  1423. if len(p) == 2:
  1424. p[0] = p[1]
  1425. else:
  1426. if not isinstance(p[1], c_ast.ExprList):
  1427. p[1] = c_ast.ExprList([p[1]], p[1].coord)
  1428. p[1].exprs.append(p[3])
  1429. p[0] = p[1]
  1430. def p_parenthesized_compound_expression(self, p):
  1431. """ assignment_expression : LPAREN compound_statement RPAREN """
  1432. p[0] = p[2]
  1433. def p_typedef_name(self, p):
  1434. """ typedef_name : TYPEID """
  1435. p[0] = c_ast.IdentifierType([p[1]], coord=self._token_coord(p, 1))
  1436. def p_assignment_expression(self, p):
  1437. """ assignment_expression : conditional_expression
  1438. | unary_expression assignment_operator assignment_expression
  1439. """
  1440. if len(p) == 2:
  1441. p[0] = p[1]
  1442. else:
  1443. p[0] = c_ast.Assignment(p[2], p[1], p[3], p[1].coord)
  1444. # K&R2 defines these as many separate rules, to encode
  1445. # precedence and associativity. Why work hard ? I'll just use
  1446. # the built in precedence/associativity specification feature
  1447. # of PLY. (see precedence declaration above)
  1448. #
  1449. def p_assignment_operator(self, p):
  1450. """ assignment_operator : EQUALS
  1451. | XOREQUAL
  1452. | TIMESEQUAL
  1453. | DIVEQUAL
  1454. | MODEQUAL
  1455. | PLUSEQUAL
  1456. | MINUSEQUAL
  1457. | LSHIFTEQUAL
  1458. | RSHIFTEQUAL
  1459. | ANDEQUAL
  1460. | OREQUAL
  1461. """
  1462. p[0] = p[1]
  1463. def p_constant_expression(self, p):
  1464. """ constant_expression : conditional_expression """
  1465. p[0] = p[1]
  1466. def p_conditional_expression(self, p):
  1467. """ conditional_expression : binary_expression
  1468. | binary_expression CONDOP expression COLON conditional_expression
  1469. """
  1470. if len(p) == 2:
  1471. p[0] = p[1]
  1472. else:
  1473. p[0] = c_ast.TernaryOp(p[1], p[3], p[5], p[1].coord)
  1474. def p_binary_expression(self, p):
  1475. """ binary_expression : cast_expression
  1476. | binary_expression TIMES binary_expression
  1477. | binary_expression DIVIDE binary_expression
  1478. | binary_expression MOD binary_expression
  1479. | binary_expression PLUS binary_expression
  1480. | binary_expression MINUS binary_expression
  1481. | binary_expression RSHIFT binary_expression
  1482. | binary_expression LSHIFT binary_expression
  1483. | binary_expression LT binary_expression
  1484. | binary_expression LE binary_expression
  1485. | binary_expression GE binary_expression
  1486. | binary_expression GT binary_expression
  1487. | binary_expression EQ binary_expression
  1488. | binary_expression NE binary_expression
  1489. | binary_expression AND binary_expression
  1490. | binary_expression OR binary_expression
  1491. | binary_expression XOR binary_expression
  1492. | binary_expression LAND binary_expression
  1493. | binary_expression LOR binary_expression
  1494. """
  1495. if len(p) == 2:
  1496. p[0] = p[1]
  1497. else:
  1498. p[0] = c_ast.BinaryOp(p[2], p[1], p[3], p[1].coord)
  1499. def p_cast_expression_1(self, p):
  1500. """ cast_expression : unary_expression """
  1501. p[0] = p[1]
  1502. def p_cast_expression_2(self, p):
  1503. """ cast_expression : LPAREN type_name RPAREN cast_expression """
  1504. p[0] = c_ast.Cast(p[2], p[4], self._token_coord(p, 1))
  1505. def p_unary_expression_1(self, p):
  1506. """ unary_expression : postfix_expression """
  1507. p[0] = p[1]
  1508. def p_unary_expression_2(self, p):
  1509. """ unary_expression : PLUSPLUS unary_expression
  1510. | MINUSMINUS unary_expression
  1511. | unary_operator cast_expression
  1512. """
  1513. p[0] = c_ast.UnaryOp(p[1], p[2], p[2].coord)
  1514. def p_unary_expression_3(self, p):
  1515. """ unary_expression : SIZEOF unary_expression
  1516. | SIZEOF LPAREN type_name RPAREN
  1517. | _ALIGNOF LPAREN type_name RPAREN
  1518. """
  1519. p[0] = c_ast.UnaryOp(
  1520. p[1],
  1521. p[2] if len(p) == 3 else p[3],
  1522. self._token_coord(p, 1))
  1523. def p_unary_operator(self, p):
  1524. """ unary_operator : AND
  1525. | TIMES
  1526. | PLUS
  1527. | MINUS
  1528. | NOT
  1529. | LNOT
  1530. """
  1531. p[0] = p[1]
  1532. def p_postfix_expression_1(self, p):
  1533. """ postfix_expression : primary_expression """
  1534. p[0] = p[1]
  1535. def p_postfix_expression_2(self, p):
  1536. """ postfix_expression : postfix_expression LBRACKET expression RBRACKET """
  1537. p[0] = c_ast.ArrayRef(p[1], p[3], p[1].coord)
  1538. def p_postfix_expression_3(self, p):
  1539. """ postfix_expression : postfix_expression LPAREN argument_expression_list RPAREN
  1540. | postfix_expression LPAREN RPAREN
  1541. """
  1542. p[0] = c_ast.FuncCall(p[1], p[3] if len(p) == 5 else None, p[1].coord)
  1543. def p_postfix_expression_4(self, p):
  1544. """ postfix_expression : postfix_expression PERIOD ID
  1545. | postfix_expression PERIOD TYPEID
  1546. | postfix_expression ARROW ID
  1547. | postfix_expression ARROW TYPEID
  1548. """
  1549. field = c_ast.ID(p[3], self._token_coord(p, 3))
  1550. p[0] = c_ast.StructRef(p[1], p[2], field, p[1].coord)
  1551. def p_postfix_expression_5(self, p):
  1552. """ postfix_expression : postfix_expression PLUSPLUS
  1553. | postfix_expression MINUSMINUS
  1554. """
  1555. p[0] = c_ast.UnaryOp('p' + p[2], p[1], p[1].coord)
  1556. def p_postfix_expression_6(self, p):
  1557. """ postfix_expression : LPAREN type_name RPAREN brace_open initializer_list brace_close
  1558. | LPAREN type_name RPAREN brace_open initializer_list COMMA brace_close
  1559. """
  1560. p[0] = c_ast.CompoundLiteral(p[2], p[5])
  1561. def p_primary_expression_1(self, p):
  1562. """ primary_expression : identifier """
  1563. p[0] = p[1]
  1564. def p_primary_expression_2(self, p):
  1565. """ primary_expression : constant """
  1566. p[0] = p[1]
  1567. def p_primary_expression_3(self, p):
  1568. """ primary_expression : unified_string_literal
  1569. | unified_wstring_literal
  1570. """
  1571. p[0] = p[1]
  1572. def p_primary_expression_4(self, p):
  1573. """ primary_expression : LPAREN expression RPAREN """
  1574. p[0] = p[2]
  1575. def p_primary_expression_5(self, p):
  1576. """ primary_expression : OFFSETOF LPAREN type_name COMMA offsetof_member_designator RPAREN
  1577. """
  1578. coord = self._token_coord(p, 1)
  1579. p[0] = c_ast.FuncCall(c_ast.ID(p[1], coord),
  1580. c_ast.ExprList([p[3], p[5]], coord),
  1581. coord)
  1582. def p_offsetof_member_designator(self, p):
  1583. """ offsetof_member_designator : identifier
  1584. | offsetof_member_designator PERIOD identifier
  1585. | offsetof_member_designator LBRACKET expression RBRACKET
  1586. """
  1587. if len(p) == 2:
  1588. p[0] = p[1]
  1589. elif len(p) == 4:
  1590. p[0] = c_ast.StructRef(p[1], p[2], p[3], p[1].coord)
  1591. elif len(p) == 5:
  1592. p[0] = c_ast.ArrayRef(p[1], p[3], p[1].coord)
  1593. else:
  1594. raise NotImplementedError("Unexpected parsing state. len(p): %u" % len(p))
  1595. def p_argument_expression_list(self, p):
  1596. """ argument_expression_list : assignment_expression
  1597. | argument_expression_list COMMA assignment_expression
  1598. """
  1599. if len(p) == 2: # single expr
  1600. p[0] = c_ast.ExprList([p[1]], p[1].coord)
  1601. else:
  1602. p[1].exprs.append(p[3])
  1603. p[0] = p[1]
  1604. def p_identifier(self, p):
  1605. """ identifier : ID """
  1606. p[0] = c_ast.ID(p[1], self._token_coord(p, 1))
  1607. def p_constant_1(self, p):
  1608. """ constant : INT_CONST_DEC
  1609. | INT_CONST_OCT
  1610. | INT_CONST_HEX
  1611. | INT_CONST_BIN
  1612. | INT_CONST_CHAR
  1613. """
  1614. uCount = 0
  1615. lCount = 0
  1616. for x in p[1][-3:]:
  1617. if x in ('l', 'L'):
  1618. lCount += 1
  1619. elif x in ('u', 'U'):
  1620. uCount += 1
  1621. t = ''
  1622. if uCount > 1:
  1623. raise ValueError('Constant cannot have more than one u/U suffix.')
  1624. elif lCount > 2:
  1625. raise ValueError('Constant cannot have more than two l/L suffix.')
  1626. prefix = 'unsigned ' * uCount + 'long ' * lCount
  1627. p[0] = c_ast.Constant(
  1628. prefix + 'int', p[1], self._token_coord(p, 1))
  1629. def p_constant_2(self, p):
  1630. """ constant : FLOAT_CONST
  1631. | HEX_FLOAT_CONST
  1632. """
  1633. if 'x' in p[1].lower():
  1634. t = 'float'
  1635. else:
  1636. if p[1][-1] in ('f', 'F'):
  1637. t = 'float'
  1638. elif p[1][-1] in ('l', 'L'):
  1639. t = 'long double'
  1640. else:
  1641. t = 'double'
  1642. p[0] = c_ast.Constant(
  1643. t, p[1], self._token_coord(p, 1))
  1644. def p_constant_3(self, p):
  1645. """ constant : CHAR_CONST
  1646. | WCHAR_CONST
  1647. | U8CHAR_CONST
  1648. | U16CHAR_CONST
  1649. | U32CHAR_CONST
  1650. """
  1651. p[0] = c_ast.Constant(
  1652. 'char', p[1], self._token_coord(p, 1))
  1653. # The "unified" string and wstring literal rules are for supporting
  1654. # concatenation of adjacent string literals.
  1655. # I.e. "hello " "world" is seen by the C compiler as a single string literal
  1656. # with the value "hello world"
  1657. #
  1658. def p_unified_string_literal(self, p):
  1659. """ unified_string_literal : STRING_LITERAL
  1660. | unified_string_literal STRING_LITERAL
  1661. """
  1662. if len(p) == 2: # single literal
  1663. p[0] = c_ast.Constant(
  1664. 'string', p[1], self._token_coord(p, 1))
  1665. else:
  1666. p[1].value = p[1].value[:-1] + p[2][1:]
  1667. p[0] = p[1]
  1668. def p_unified_wstring_literal(self, p):
  1669. """ unified_wstring_literal : WSTRING_LITERAL
  1670. | U8STRING_LITERAL
  1671. | U16STRING_LITERAL
  1672. | U32STRING_LITERAL
  1673. | unified_wstring_literal WSTRING_LITERAL
  1674. | unified_wstring_literal U8STRING_LITERAL
  1675. | unified_wstring_literal U16STRING_LITERAL
  1676. | unified_wstring_literal U32STRING_LITERAL
  1677. """
  1678. if len(p) == 2: # single literal
  1679. p[0] = c_ast.Constant(
  1680. 'string', p[1], self._token_coord(p, 1))
  1681. else:
  1682. p[1].value = p[1].value.rstrip()[:-1] + p[2][2:]
  1683. p[0] = p[1]
  1684. def p_brace_open(self, p):
  1685. """ brace_open : LBRACE
  1686. """
  1687. p[0] = p[1]
  1688. p.set_lineno(0, p.lineno(1))
  1689. def p_brace_close(self, p):
  1690. """ brace_close : RBRACE
  1691. """
  1692. p[0] = p[1]
  1693. p.set_lineno(0, p.lineno(1))
  1694. def p_empty(self, p):
  1695. 'empty : '
  1696. p[0] = None
  1697. def p_error(self, p):
  1698. # If error recovery is added here in the future, make sure
  1699. # _get_yacc_lookahead_token still works!
  1700. #
  1701. if p:
  1702. self._parse_error(
  1703. 'before: %s' % p.value,
  1704. self._coord(lineno=p.lineno,
  1705. column=self.clex.find_tok_column(p)))
  1706. else:
  1707. self._parse_error('At end of input', self.clex.filename)