cppBison.yxx 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. // Filename: cppBison.y
  2. // Created by: drose (16Jan99)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. %{
  6. #include "cppBisonDefs.h"
  7. #include "cppParser.h"
  8. #include "cppExpression.h"
  9. #include "cppSimpleType.h"
  10. #include "cppExtensionType.h"
  11. #include "cppStructType.h"
  12. #include "cppEnumType.h"
  13. #include "cppFunctionType.h"
  14. #include "cppTBDType.h"
  15. #include "cppMakeSeq.h"
  16. #include "cppParameterList.h"
  17. #include "cppInstance.h"
  18. #include "cppClassTemplateParameter.h"
  19. #include "cppTemplateParameterList.h"
  20. #include "cppInstanceIdentifier.h"
  21. #include "cppTypedef.h"
  22. #include "cppTypeDeclaration.h"
  23. #include "cppVisibility.h"
  24. #include "cppIdentifier.h"
  25. #include "cppScope.h"
  26. #include "cppTemplateScope.h"
  27. #include "cppNamespace.h"
  28. #include "cppUsing.h"
  29. ////////////////////////////////////////////////////////////////////
  30. // Defining the interface to the parser.
  31. ////////////////////////////////////////////////////////////////////
  32. CPPScope *current_scope = NULL;
  33. CPPScope *global_scope = NULL;
  34. CPPPreprocessor *current_lexer = NULL;
  35. static CPPStructType *current_struct = NULL;
  36. static CPPEnumType *current_enum = NULL;
  37. static int current_storage_class = 0;
  38. static CPPType *current_type = NULL;
  39. static CPPExpression *current_expr = NULL;
  40. static int publish_nest_level = 0;
  41. static CPPVisibility publish_previous;
  42. static YYLTYPE publish_loc;
  43. static vector<CPPScope *> last_scopes;
  44. static vector<int> last_storage_classes;
  45. static vector<CPPStructType *> last_structs;
  46. int yyparse();
  47. #define YYERROR_VERBOSE
  48. static void
  49. yyerror(const string &msg) {
  50. current_lexer->error(msg);
  51. }
  52. static void
  53. yyerror(const string &msg, YYLTYPE &loc) {
  54. current_lexer->error(msg, loc.first_line, loc.first_column);
  55. }
  56. static void
  57. yywarning(const string &msg, YYLTYPE &loc) {
  58. current_lexer->warning(msg, loc.first_line, loc.first_column);
  59. }
  60. static int
  61. yylex(YYSTYPE *lval, YYLTYPE *lloc) {
  62. CPPToken token = current_lexer->get_next_token();
  63. *lval = token._lval;
  64. *lloc = token._lloc;
  65. return token._token;
  66. }
  67. void
  68. parse_cpp(CPPParser *cp) {
  69. CPPScope *old_scope = current_scope;
  70. CPPScope *old_global_scope = global_scope;
  71. CPPPreprocessor *old_lexer = current_lexer;
  72. current_scope = cp;
  73. global_scope = cp;
  74. current_lexer = cp;
  75. publish_nest_level = 0;
  76. yyparse();
  77. if (publish_nest_level != 0) {
  78. yyerror("Unclosed __begin_publish", publish_loc);
  79. publish_nest_level = 0;
  80. }
  81. current_scope = old_scope;
  82. global_scope = old_global_scope;
  83. current_lexer = old_lexer;
  84. }
  85. CPPExpression *
  86. parse_const_expr(CPPPreprocessor *pp, CPPScope *new_current_scope,
  87. CPPScope *new_global_scope) {
  88. CPPScope *old_scope = current_scope;
  89. CPPScope *old_global_scope = global_scope;
  90. CPPPreprocessor *old_lexer = current_lexer;
  91. CPPExpression *old_expr = current_expr;
  92. current_scope = new_current_scope;
  93. global_scope = new_global_scope;
  94. current_expr = (CPPExpression *)NULL;
  95. current_lexer = pp;
  96. yyparse();
  97. CPPExpression *result = current_expr;
  98. current_scope = old_scope;
  99. global_scope = old_global_scope;
  100. current_lexer = old_lexer;
  101. current_expr = old_expr;
  102. return result;
  103. }
  104. CPPType *
  105. parse_type(CPPPreprocessor *pp, CPPScope *new_current_scope,
  106. CPPScope *new_global_scope) {
  107. CPPScope *old_scope = current_scope;
  108. CPPScope *old_global_scope = global_scope;
  109. CPPPreprocessor *old_lexer = current_lexer;
  110. CPPType *old_type = current_type;
  111. current_scope = new_current_scope;
  112. global_scope = new_global_scope;
  113. current_type = (CPPType *)NULL;
  114. current_lexer = pp;
  115. yyparse();
  116. CPPType *result = current_type;
  117. current_scope = old_scope;
  118. global_scope = old_global_scope;
  119. current_lexer = old_lexer;
  120. current_type = old_type;
  121. return result;
  122. }
  123. static void
  124. push_scope(CPPScope *new_scope) {
  125. last_scopes.push_back(current_scope);
  126. if (new_scope != NULL) {
  127. current_scope = new_scope;
  128. }
  129. }
  130. static void
  131. pop_scope() {
  132. assert(!last_scopes.empty());
  133. current_scope = last_scopes.back();
  134. last_scopes.pop_back();
  135. }
  136. static void
  137. push_storage_class(int new_storage_class) {
  138. last_storage_classes.push_back(current_storage_class);
  139. current_storage_class = new_storage_class;
  140. }
  141. static void
  142. pop_storage_class() {
  143. assert(!last_storage_classes.empty());
  144. current_storage_class = last_storage_classes.back();
  145. last_storage_classes.pop_back();
  146. }
  147. static void
  148. push_struct(CPPStructType *new_struct) {
  149. last_structs.push_back(current_struct);
  150. current_struct = new_struct;
  151. }
  152. static void
  153. pop_struct() {
  154. assert(!last_structs.empty());
  155. current_struct = last_structs.back();
  156. last_structs.pop_back();
  157. }
  158. %}
  159. /* This is a bison-specific declaration to enable recursive calls to
  160. yyparse(). It changes the calling sequence to yylex(), passing
  161. pointers to the current yylval and yylloc. */
  162. %pure_parser
  163. %token <u.real> REAL
  164. %token <u.integer> INTEGER
  165. %token <u.integer> CHAR_TOK
  166. %token <str> STRING SIMPLE_IDENTIFIER
  167. %token <u.identifier> IDENTIFIER TYPENAME_IDENTIFIER SCOPING
  168. %token <u.type> TYPEDEFNAME
  169. %token ELLIPSIS
  170. %token OROR
  171. %token ANDAND
  172. %token EQCOMPARE
  173. %token NECOMPARE
  174. %token LECOMPARE
  175. %token GECOMPARE
  176. %token LSHIFT
  177. %token RSHIFT
  178. %token POINTSAT_STAR
  179. %token DOT_STAR
  180. %token UNARY
  181. %token UNARY_NOT
  182. %token UNARY_NEGATE
  183. %token UNARY_MINUS
  184. %token UNARY_STAR
  185. %token UNARY_REF
  186. %token POINTSAT
  187. %token SCOPE
  188. %token PLUSPLUS
  189. %token MINUSMINUS
  190. %token TIMESEQUAL
  191. %token DIVIDEEQUAL
  192. %token MODEQUAL
  193. %token PLUSEQUAL
  194. %token MINUSEQUAL
  195. %token OREQUAL
  196. %token ANDEQUAL
  197. %token XOREQUAL
  198. %token LSHIFTEQUAL
  199. %token RSHIFTEQUAL
  200. %token TOKENPASTE
  201. %token KW_BEGIN_PUBLISH
  202. %token KW_BLOCKING
  203. %token KW_BOOL
  204. %token KW_CATCH
  205. %token KW_CHAR
  206. %token KW_WCHAR_T
  207. %token KW_CLASS
  208. %token KW_CONST
  209. %token KW_DELETE
  210. %token KW_DOUBLE
  211. %token KW_DYNAMIC_CAST
  212. %token KW_ELSE
  213. %token KW_END_PUBLISH
  214. %token KW_ENUM
  215. %token KW_EXTERN
  216. %token KW_EXPLICIT
  217. %token KW_PUBLISHED
  218. %token KW_FALSE
  219. %token KW_FLOAT
  220. %token KW_FRIEND
  221. %token KW_FOR
  222. %token KW_GOTO
  223. %token KW_IF
  224. %token KW_INLINE
  225. %token KW_INT
  226. %token KW_LONG
  227. %token KW_LONGLONG
  228. %token KW_MAKE_SEQ
  229. %token KW_MUTABLE
  230. %token KW_NAMESPACE
  231. %token KW_NEW
  232. %token KW_OPERATOR
  233. %token KW_PRIVATE
  234. %token KW_PROTECTED
  235. %token KW_PUBLIC
  236. %token KW_REGISTER
  237. %token KW_RETURN
  238. %token KW_SHORT
  239. %token KW_SIGNED
  240. %token KW_SIZEOF
  241. %token KW_STATIC
  242. %token KW_STATIC_CAST
  243. %token KW_STRUCT
  244. %token KW_TEMPLATE
  245. %token KW_THROW
  246. %token KW_TRUE
  247. %token KW_TRY
  248. %token KW_TYPEDEF
  249. %token KW_TYPENAME
  250. %token KW_UNION
  251. %token KW_UNSIGNED
  252. %token KW_USING
  253. %token KW_VIRTUAL
  254. %token KW_VOID
  255. %token KW_VOLATILE
  256. %token KW_WHILE
  257. /* These special tokens are used to set the starting state of the
  258. parser. The lexer places the appropriate one of these on the head
  259. of the input stream. */
  260. %token START_CPP
  261. %token START_CONST_EXPR
  262. %token START_TYPE
  263. %type <u.integer> storage_class
  264. %type <u.instance> function_prototype
  265. %type <u.integer> function_post
  266. %type <str> function_operator
  267. %type <u.decl> template_formal_parameter
  268. %type <u.type> template_formal_parameter_type
  269. %type <u.inst_ident> instance_identifier
  270. %type <u.param_list> formal_parameter_list
  271. %type <u.param_list> formal_parameters
  272. %type <u.expr> template_parameter_maybe_initialize
  273. %type <u.expr> maybe_initialize
  274. %type <u.expr> maybe_initialize_or_function_body
  275. %type <u.instance> formal_parameter
  276. %type <u.inst_ident> not_paren_formal_parameter_identifier
  277. %type <u.inst_ident> formal_parameter_identifier
  278. %type <u.inst_ident> empty_instance_identifier
  279. %type <u.type> type
  280. %type <u.decl> type_decl
  281. %type <u.type> predefined_type
  282. %type <u.type> full_type
  283. %type <u.struct_type> anonymous_struct
  284. %type <u.struct_type> named_struct
  285. %type <u.enum_type> anonymous_enum
  286. %type <u.enum_type> named_enum
  287. %type <u.extension_enum> enum_keyword
  288. %type <u.extension_enum> struct_keyword
  289. %type <u.simple_type> simple_type
  290. %type <u.simple_type> simple_int_type
  291. %type <u.simple_type> simple_float_type
  292. %type <u.simple_type> simple_void_type
  293. %type <u.type> class_derivation_name
  294. /*%type <u.type> typedefname*/
  295. %type <u.identifier> name
  296. %type <str> string
  297. /* We need to treat KW_OPERATOR as a scopable keyword. */
  298. %type <u.identifier> KW_OPERATOR
  299. %type <u.expr> optional_const_expr
  300. %type <u.expr> optional_const_expr_comma
  301. %type <u.expr> const_expr_comma
  302. %type <u.expr> no_angle_bracket_const_expr
  303. %type <u.expr> const_expr
  304. %type <u.expr> const_operand
  305. %type <u.expr> formal_const_expr
  306. %type <u.expr> formal_const_operand
  307. /* Precedence rules. */
  308. %left IDENTIFIER TYPENAME_IDENTIFIER TYPEDEFNAME KW_ENUM ELLIPSIS KW_OPERATOR KW_TYPENAME KW_INT KW_SHORT KW_UNSIGNED KW_SIGNED KW_LONG KW_FLOAT KW_DOUBLE KW_CHAR KW_WCHAR_T KW_BOOL
  309. %left '{' ',' ';'
  310. %nonassoc KW_THROW
  311. %right ':'
  312. %right '='
  313. %right '?'
  314. %left OROR
  315. %left ANDAND
  316. %left '|'
  317. %left '^'
  318. %left '&'
  319. %left EQCOMPARE NECOMPARE
  320. %left LECOMPARE GECOMPARE '<' '>'
  321. %left LSHIFT RSHIFT
  322. %left '+' '-'
  323. %left '*' '/' '%'
  324. %left POINTSAT_STAR DOT_STAR
  325. %right UNARY PLUSPLUS MINUSMINUS '~'
  326. %left POINTSAT '.' '(' '['
  327. %right SCOPE
  328. %nonassoc KW_NEW KW_DELETE KW_TRY KW_CATCH
  329. %%
  330. grammar:
  331. START_CPP cpp
  332. | START_CONST_EXPR const_expr
  333. {
  334. current_expr = $2;
  335. }
  336. | START_TYPE full_type
  337. {
  338. current_type = $2;
  339. }
  340. ;
  341. cpp:
  342. empty
  343. | cpp ';'
  344. | cpp declaration
  345. ;
  346. constructor_inits:
  347. constructor_init
  348. | constructor_inits ',' constructor_init
  349. ;
  350. constructor_init:
  351. name '(' optional_const_expr_comma ')'
  352. {
  353. delete $3;
  354. }
  355. ;
  356. /* This is principally for the syntax: extern "C" { ... }.
  357. We use storage_class instead of simply KW_EXTERN to avoid
  358. shift/reduce conflicts with yacc's limited differentiation
  359. ability. */
  360. extern_c:
  361. storage_class '{'
  362. {
  363. push_storage_class((current_storage_class & ~CPPInstance::SC_c_binding) |
  364. ($1 & CPPInstance::SC_c_binding));
  365. }
  366. cpp '}'
  367. {
  368. pop_storage_class();
  369. }
  370. ;
  371. declaration:
  372. type_like_declaration
  373. | template_declaration
  374. | extern_c
  375. | namespace_declaration
  376. | using_declaration
  377. | friend_declaration
  378. | KW_TYPEDEF typedef_declaration
  379. | KW_BEGIN_PUBLISH
  380. {
  381. if (publish_nest_level != 0) {
  382. yyerror("Unclosed __begin_publish", publish_loc);
  383. publish_nest_level = 0;
  384. current_scope->set_current_vis(V_public);
  385. }
  386. publish_previous = current_scope->get_current_vis();
  387. publish_loc = @1;
  388. publish_nest_level++;
  389. current_scope->set_current_vis(V_published);
  390. }
  391. | KW_END_PUBLISH
  392. {
  393. if (publish_nest_level != 1) {
  394. yyerror("Unmatched __end_publish", @1);
  395. } else {
  396. current_scope->set_current_vis(publish_previous);
  397. }
  398. publish_nest_level = 0;
  399. }
  400. | KW_PUBLISHED ':'
  401. {
  402. current_scope->set_current_vis(V_published);
  403. }
  404. | KW_PUBLIC ':'
  405. {
  406. if (publish_nest_level > 0) {
  407. current_scope->set_current_vis(V_published);
  408. } else {
  409. current_scope->set_current_vis(V_public);
  410. }
  411. }
  412. | KW_PROTECTED ':'
  413. {
  414. current_scope->set_current_vis(V_protected);
  415. }
  416. | KW_PRIVATE ':'
  417. {
  418. current_scope->set_current_vis(V_private);
  419. }
  420. | KW_MAKE_SEQ '(' name ',' name ',' name ')' ';'
  421. {
  422. CPPMakeSeq *make_seq = new CPPMakeSeq($3->get_simple_name(), $5->get_simple_name(), $7->get_simple_name(), @1.file);
  423. current_scope->add_declaration(make_seq, global_scope, current_lexer, @1);
  424. }
  425. ;
  426. friend_declaration:
  427. KW_FRIEND
  428. {
  429. CPPScope *new_scope = new CPPScope(current_scope, CPPNameComponent("temp"),
  430. V_public);
  431. push_scope(new_scope);
  432. }
  433. declaration
  434. {
  435. delete current_scope;
  436. pop_scope();
  437. }
  438. ;
  439. storage_class:
  440. empty
  441. {
  442. $$ = 0;
  443. }
  444. | storage_class KW_EXTERN
  445. {
  446. $$ = $1 | (int)CPPInstance::SC_extern;
  447. }
  448. | storage_class KW_EXTERN string
  449. {
  450. $$ = $1 | (int)CPPInstance::SC_extern;
  451. if ($3 == "C") {
  452. $$ |= (int)CPPInstance::SC_c_binding;
  453. } else if ($3 == "C++") {
  454. $$ &= ~(int)CPPInstance::SC_c_binding;
  455. } else {
  456. yywarning("Ignoring unknown linkage type \"" + $3 + "\"", @3);
  457. }
  458. }
  459. | storage_class KW_STATIC
  460. {
  461. $$ = $1 | (int)CPPInstance::SC_static;
  462. }
  463. | storage_class KW_INLINE
  464. {
  465. $$ = $1 | (int)CPPInstance::SC_inline;
  466. }
  467. | storage_class KW_VIRTUAL
  468. {
  469. $$ = $1 | (int)CPPInstance::SC_virtual;
  470. }
  471. | storage_class KW_EXPLICIT
  472. {
  473. $$ = $1 | (int)CPPInstance::SC_explicit;
  474. }
  475. | storage_class KW_VOLATILE
  476. {
  477. $$ = $1 | (int)CPPInstance::SC_volatile;
  478. }
  479. | storage_class KW_MUTABLE
  480. {
  481. $$ = $1 | (int)CPPInstance::SC_mutable;
  482. }
  483. | storage_class KW_REGISTER
  484. {
  485. $$ = $1 | (int)CPPInstance::SC_register;
  486. }
  487. | storage_class KW_BLOCKING
  488. {
  489. $$ = $1 | (int)CPPInstance::SC_blocking;
  490. }
  491. ;
  492. type_like_declaration:
  493. multiple_var_declaration
  494. {
  495. /* multiple_var_declaration adds itself to the scope. */
  496. }
  497. | storage_class type_decl ';'
  498. {
  499. // We don't really care about the storage class here. In fact, it's
  500. // not actually legal to define a class or struct using a particular
  501. // storage class, but we require it just to help yacc out in its
  502. // parsing.
  503. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  504. }
  505. | storage_class function_prototype maybe_initialize_or_function_body
  506. {
  507. if ($2 != (CPPInstance *)NULL) {
  508. $2->_storage_class |= (current_storage_class | $1);
  509. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  510. $2->set_initializer($3);
  511. }
  512. }
  513. ;
  514. multiple_var_declaration:
  515. storage_class type_decl
  516. {
  517. // We don't need to push/pop type, because we can't nest
  518. // multiple_var_declarations.
  519. if ($2->as_type_declaration()) {
  520. current_type = $2->as_type_declaration()->_type;
  521. } else {
  522. current_type = $2->as_type();
  523. }
  524. push_storage_class($1);
  525. }
  526. multiple_instance_identifiers
  527. {
  528. pop_storage_class();
  529. }
  530. | storage_class KW_CONST type
  531. {
  532. // We don't need to push/pop type, because we can't nest
  533. // multiple_var_declarations.
  534. current_type = $3;
  535. push_storage_class($1);
  536. }
  537. multiple_const_instance_identifiers
  538. {
  539. pop_storage_class();
  540. }
  541. /* We don't need to include a rule for variables that point to
  542. functions, because we get those from the function_prototype
  543. definition. */
  544. ;
  545. multiple_instance_identifiers:
  546. instance_identifier maybe_initialize_or_function_body
  547. {
  548. CPPInstance *inst = new CPPInstance(current_type, $1,
  549. current_storage_class,
  550. @1.file);
  551. inst->set_initializer($2);
  552. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  553. }
  554. | instance_identifier maybe_initialize ',' multiple_instance_identifiers
  555. {
  556. CPPInstance *inst = new CPPInstance(current_type, $1,
  557. current_storage_class,
  558. @1.file);
  559. inst->set_initializer($2);
  560. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  561. }
  562. ;
  563. multiple_const_instance_identifiers:
  564. instance_identifier maybe_initialize_or_function_body
  565. {
  566. $1->add_modifier(IIT_const);
  567. CPPInstance *inst = new CPPInstance(current_type, $1,
  568. current_storage_class,
  569. @1.file);
  570. inst->set_initializer($2);
  571. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  572. }
  573. | instance_identifier maybe_initialize ',' multiple_const_instance_identifiers
  574. {
  575. $1->add_modifier(IIT_const);
  576. CPPInstance *inst = new CPPInstance(current_type, $1,
  577. current_storage_class,
  578. @1.file);
  579. inst->set_initializer($2);
  580. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  581. }
  582. ;
  583. typedef_declaration:
  584. storage_class type_decl
  585. {
  586. // We don't need to push/pop type, because we can't nest
  587. // multiple_var_declarations.
  588. if ($2->as_type_declaration()) {
  589. current_type = $2->as_type_declaration()->_type;
  590. } else {
  591. current_type = $2->as_type();
  592. }
  593. push_storage_class($1);
  594. }
  595. typedef_instance_identifiers
  596. {
  597. pop_storage_class();
  598. }
  599. | storage_class KW_CONST type
  600. {
  601. // We don't need to push/pop type, because we can't nest
  602. // multiple_var_declarations.
  603. current_type = $3;
  604. push_storage_class($1);
  605. }
  606. typedef_const_instance_identifiers
  607. {
  608. pop_storage_class();
  609. }
  610. | storage_class function_prototype maybe_initialize_or_function_body
  611. {
  612. if ($2 != (CPPDeclaration *)NULL) {
  613. CPPInstance *inst = $2->as_instance();
  614. if (inst != (CPPInstance *)NULL) {
  615. inst->_storage_class |= (current_storage_class | $1);
  616. current_scope->add_declaration(inst, global_scope, current_lexer, @2);
  617. current_scope->add_declaration(new CPPTypedef(inst, current_scope == global_scope), global_scope, current_lexer, @2);
  618. }
  619. }
  620. }
  621. ;
  622. typedef_instance_identifiers:
  623. instance_identifier maybe_initialize_or_function_body
  624. {
  625. CPPInstance *inst = new CPPInstance(current_type, $1,
  626. current_storage_class,
  627. @1.file);
  628. inst->set_initializer($2);
  629. current_scope->add_declaration(new CPPTypedef(inst, current_scope == global_scope), global_scope, current_lexer, @1);
  630. }
  631. | instance_identifier maybe_initialize ',' typedef_instance_identifiers
  632. {
  633. CPPInstance *inst = new CPPInstance(current_type, $1,
  634. current_storage_class,
  635. @1.file);
  636. inst->set_initializer($2);
  637. current_scope->add_declaration(new CPPTypedef(inst, current_scope == global_scope), global_scope, current_lexer, @1);
  638. }
  639. ;
  640. typedef_const_instance_identifiers:
  641. instance_identifier maybe_initialize_or_function_body
  642. {
  643. $1->add_modifier(IIT_const);
  644. CPPInstance *inst = new CPPInstance(current_type, $1,
  645. current_storage_class,
  646. @1.file);
  647. inst->set_initializer($2);
  648. current_scope->add_declaration(new CPPTypedef(inst, current_scope == global_scope), global_scope, current_lexer, @1);
  649. }
  650. | instance_identifier maybe_initialize ',' typedef_const_instance_identifiers
  651. {
  652. $1->add_modifier(IIT_const);
  653. CPPInstance *inst = new CPPInstance(current_type, $1,
  654. current_storage_class,
  655. @1.file);
  656. inst->set_initializer($2);
  657. current_scope->add_declaration(new CPPTypedef(inst, current_scope == global_scope), global_scope, current_lexer, @1);
  658. }
  659. ;
  660. function_prototype:
  661. /* Functions with implicit return types, and constructors */
  662. IDENTIFIER '('
  663. {
  664. push_scope($1->get_scope(current_scope, global_scope));
  665. }
  666. formal_parameter_list ')' function_post
  667. {
  668. pop_scope();
  669. CPPType *type;
  670. if ($1->get_simple_name() == current_scope->get_simple_name()) {
  671. // This is a constructor, and has no return.
  672. type = new CPPSimpleType(CPPSimpleType::T_void);
  673. } else {
  674. // This isn't a constructor, so it has an implicit return type of
  675. // int.
  676. type = new CPPSimpleType(CPPSimpleType::T_int);
  677. }
  678. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  679. ii->add_func_modifier($4, $6);
  680. $$ = new CPPInstance(type, ii, 0, @1.file);
  681. }
  682. | TYPENAME_IDENTIFIER '('
  683. {
  684. push_scope($1->get_scope(current_scope, global_scope));
  685. }
  686. formal_parameter_list ')' function_post
  687. {
  688. pop_scope();
  689. CPPType *type;
  690. if ($1->get_simple_name() == current_scope->get_simple_name()) {
  691. // This is a constructor, and has no return.
  692. type = new CPPSimpleType(CPPSimpleType::T_void);
  693. } else {
  694. // This isn't a constructor, so it has an implicit return type of
  695. // int.
  696. type = new CPPSimpleType(CPPSimpleType::T_int);
  697. }
  698. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  699. ii->add_func_modifier($4, $6);
  700. $$ = new CPPInstance(type, ii, 0, @1.file);
  701. }
  702. /* Destructors */
  703. | '~' name '('
  704. {
  705. push_scope($2->get_scope(current_scope, global_scope));
  706. }
  707. formal_parameter_list ')' function_post
  708. {
  709. pop_scope();
  710. if ($2->is_scoped()) {
  711. yyerror("Invalid destructor name: ~" + $2->get_fully_scoped_name(), @2);
  712. } else {
  713. CPPIdentifier *ident =
  714. new CPPIdentifier("~" + $2->get_simple_name(), @2.file);
  715. delete $2;
  716. CPPType *type;
  717. type = new CPPSimpleType(CPPSimpleType::T_void);
  718. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier(ident);
  719. ii->add_func_modifier($5, $7);
  720. $$ = new CPPInstance(type, ii, 0, @2.file);
  721. }
  722. }
  723. /* This is a special case: a function pointer declaration that looks
  724. at first a lot like a constructor declaration. This is provided to
  725. help yacc sort out the differences. It isn't an ideal solution,
  726. because it doesn't catch a lot of subtle variants on this form--but
  727. this will get at least the 99% most common uses. */
  728. | TYPENAME_IDENTIFIER '(' '*' instance_identifier ')' '('
  729. {
  730. push_scope($4->get_scope(current_scope, global_scope));
  731. }
  732. formal_parameter_list ')' function_post
  733. {
  734. pop_scope();
  735. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  736. assert(type != NULL);
  737. CPPInstanceIdentifier *ii = $4;
  738. ii->add_modifier(IIT_pointer);
  739. ii->add_func_modifier($8, $10);
  740. $$ = new CPPInstance(type, ii, 0, @1.file);
  741. }
  742. | TYPENAME_IDENTIFIER '(' SCOPING '*' instance_identifier ')' '('
  743. {
  744. push_scope($5->get_scope(current_scope, global_scope));
  745. }
  746. formal_parameter_list ')' function_post
  747. {
  748. pop_scope();
  749. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  750. assert(type != NULL);
  751. CPPInstanceIdentifier *ii = $5;
  752. ii->add_scoped_pointer_modifier($3);
  753. ii->add_func_modifier($9, $11);
  754. $$ = new CPPInstance(type, ii, 0, @1.file);
  755. }
  756. /* Typecast operators */
  757. | KW_OPERATOR type not_paren_formal_parameter_identifier '('
  758. {
  759. if ($1 != NULL) {
  760. push_scope($1->get_scope(current_scope, global_scope));
  761. }
  762. }
  763. formal_parameter_list ')' function_post
  764. {
  765. if ($1 != NULL) {
  766. pop_scope();
  767. }
  768. // We use formal_parameter_identifier, because that can match a type
  769. // name with or without an identifier, but especially without, which
  770. // is what follows the keyword "operator" in a typecast function.
  771. // As an added bonus, the type of the formal_parameter will be the
  772. // typecast type, i.e. the return type of the typecast function.
  773. // We give typecast operators the name "operator typecast <name>",
  774. // where <name> is a simple name of the type to be typecast. Use
  775. // the method's return type to determine the full type description.
  776. string name = "operator typecast " + $2->get_simple_name();
  777. CPPIdentifier *ident = $1;
  778. if (ident == NULL) {
  779. ident = new CPPIdentifier(name, @1.file);
  780. } else {
  781. ident->add_name(name);
  782. }
  783. $$ = CPPInstance::make_typecast_function
  784. (new CPPInstance($2, $3, 0, @3.file), ident, $6, $8);
  785. }
  786. | KW_OPERATOR KW_CONST type not_paren_formal_parameter_identifier '('
  787. {
  788. if ($1 != NULL) {
  789. push_scope($1->get_scope(current_scope, global_scope));
  790. }
  791. }
  792. formal_parameter_list ')' function_post
  793. {
  794. if ($1 != NULL) {
  795. pop_scope();
  796. }
  797. CPPIdentifier *ident = $1;
  798. if (ident == NULL) {
  799. ident = new CPPIdentifier("operator typecast", @1.file);
  800. } else {
  801. ident->add_name("operator typecast");
  802. }
  803. $4->add_modifier(IIT_const);
  804. $$ = CPPInstance::make_typecast_function
  805. (new CPPInstance($3, $4, 0, @4.file), ident, $7, $9);
  806. }
  807. /* Not actually a function prototype, but maybe a template
  808. instantiation. Just included here (instead of somewhere else) to
  809. avoid shift/reduce conflicts. */
  810. | IDENTIFIER
  811. {
  812. CPPDeclaration *decl =
  813. $1->find_symbol(current_scope, global_scope, current_lexer);
  814. if (decl != (CPPDeclaration *)NULL) {
  815. $$ = decl->as_instance();
  816. } else {
  817. $$ = (CPPInstance *)NULL;
  818. }
  819. }
  820. ;
  821. function_post:
  822. empty
  823. {
  824. $$ = 0;
  825. }
  826. | KW_CONST
  827. {
  828. $$ = (int)CPPFunctionType::F_const_method;
  829. }
  830. | function_post KW_THROW '(' ')'
  831. {
  832. $$ = $1;
  833. }
  834. | function_post KW_THROW '(' name ')'
  835. {
  836. $$ = $1;
  837. }
  838. ;
  839. function_operator:
  840. '!'
  841. {
  842. $$ = "!";
  843. }
  844. | '~'
  845. {
  846. $$ = "~";
  847. }
  848. | '*'
  849. {
  850. $$ = "*";
  851. }
  852. | '/'
  853. {
  854. $$ = "/";
  855. }
  856. | '%'
  857. {
  858. $$ = "%";
  859. }
  860. | '+'
  861. {
  862. $$ = "+";
  863. }
  864. | '-'
  865. {
  866. $$ = "-";
  867. }
  868. | '|'
  869. {
  870. $$ = "|";
  871. }
  872. | '&'
  873. {
  874. $$ = "&";
  875. }
  876. | '^'
  877. {
  878. $$ = "^";
  879. }
  880. | OROR
  881. {
  882. $$ = "||";
  883. }
  884. | ANDAND
  885. {
  886. $$ = "&&";
  887. }
  888. | EQCOMPARE
  889. {
  890. $$ = "==";
  891. }
  892. | NECOMPARE
  893. {
  894. $$ = "!=";
  895. }
  896. | LECOMPARE
  897. {
  898. $$ = "<=";
  899. }
  900. | GECOMPARE
  901. {
  902. $$ = ">=";
  903. }
  904. | '<'
  905. {
  906. $$ = "<";
  907. }
  908. | '>'
  909. {
  910. $$ = ">";
  911. }
  912. | LSHIFT
  913. {
  914. $$ = "<<";
  915. }
  916. | RSHIFT
  917. {
  918. $$ = ">>";
  919. }
  920. | '='
  921. {
  922. $$ = "=";
  923. }
  924. | ','
  925. {
  926. $$ = ",";
  927. }
  928. | PLUSPLUS
  929. {
  930. $$ = "++";
  931. }
  932. | MINUSMINUS
  933. {
  934. $$ = "--";
  935. }
  936. | TIMESEQUAL
  937. {
  938. $$ = "*=";
  939. }
  940. | DIVIDEEQUAL
  941. {
  942. $$ = "/=";
  943. }
  944. | MODEQUAL
  945. {
  946. $$ = "%=";
  947. }
  948. | PLUSEQUAL
  949. {
  950. $$ = "+=";
  951. }
  952. | MINUSEQUAL
  953. {
  954. $$ = "-=";
  955. }
  956. | OREQUAL
  957. {
  958. $$ = "|=";
  959. }
  960. | ANDEQUAL
  961. {
  962. $$ = "&=";
  963. }
  964. | XOREQUAL
  965. {
  966. $$ = "^=";
  967. }
  968. | LSHIFTEQUAL
  969. {
  970. $$ = "<<=";
  971. }
  972. | RSHIFTEQUAL
  973. {
  974. $$ = ">>=";
  975. }
  976. | POINTSAT
  977. {
  978. $$ = "->";
  979. }
  980. | '[' ']'
  981. {
  982. $$ = "[]";
  983. }
  984. | '(' ')'
  985. {
  986. $$ = "()";
  987. }
  988. | KW_NEW
  989. {
  990. $$ = "new";
  991. }
  992. | KW_DELETE
  993. {
  994. $$ = "delete";
  995. }
  996. ;
  997. more_template_declaration:
  998. type_like_declaration
  999. | template_declaration
  1000. ;
  1001. template_declaration:
  1002. KW_TEMPLATE
  1003. {
  1004. push_scope(new CPPTemplateScope(current_scope));
  1005. }
  1006. '<' template_formal_parameters '>' more_template_declaration
  1007. {
  1008. pop_scope();
  1009. }
  1010. ;
  1011. template_formal_parameters:
  1012. empty
  1013. | template_nonempty_formal_parameters
  1014. ;
  1015. template_nonempty_formal_parameters:
  1016. template_formal_parameter
  1017. {
  1018. CPPTemplateScope *ts = current_scope->as_template_scope();
  1019. assert(ts != NULL);
  1020. ts->add_template_parameter($1);
  1021. }
  1022. | template_nonempty_formal_parameters ',' template_formal_parameter
  1023. {
  1024. CPPTemplateScope *ts = current_scope->as_template_scope();
  1025. assert(ts != NULL);
  1026. ts->add_template_parameter($3);
  1027. }
  1028. ;
  1029. template_formal_parameter:
  1030. KW_CLASS name
  1031. {
  1032. $$ = CPPType::new_type(new CPPClassTemplateParameter($2));
  1033. }
  1034. | KW_CLASS name '=' full_type
  1035. {
  1036. $$ = CPPType::new_type(new CPPClassTemplateParameter($2, $4));
  1037. }
  1038. | template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1039. {
  1040. CPPInstance *inst = new CPPInstance($1, $2, 0, @2.file);
  1041. inst->set_initializer($3);
  1042. $$ = inst;
  1043. }
  1044. | KW_CONST template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1045. {
  1046. $3->add_modifier(IIT_const);
  1047. CPPInstance *inst = new CPPInstance($2, $3, 0, @3.file);
  1048. inst->set_initializer($4);
  1049. $$ = inst;
  1050. }
  1051. ;
  1052. template_formal_parameter_type:
  1053. simple_type
  1054. {
  1055. $$ = CPPType::new_type($1);
  1056. }
  1057. | IDENTIFIER
  1058. {
  1059. yywarning("Not a type: " + $1->get_fully_scoped_name(), @1);
  1060. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  1061. }
  1062. | TYPENAME_IDENTIFIER
  1063. {
  1064. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1065. assert($$ != NULL);
  1066. }
  1067. | KW_TYPENAME name
  1068. {
  1069. $$ = CPPType::new_type(new CPPTBDType($2));
  1070. }
  1071. ;
  1072. instance_identifier:
  1073. name
  1074. {
  1075. $$ = new CPPInstanceIdentifier($1);
  1076. }
  1077. | KW_OPERATOR function_operator
  1078. {
  1079. // For an operator function. We implement this simply by building a
  1080. // ficticious name for the function; in other respects it's just
  1081. // like a regular function.
  1082. CPPIdentifier *ident = $1;
  1083. if (ident == NULL) {
  1084. ident = new CPPIdentifier("operator "+$2, @2.file);
  1085. } else {
  1086. ident->_names.push_back("operator "+$2);
  1087. }
  1088. $$ = new CPPInstanceIdentifier(ident);
  1089. }
  1090. | KW_CONST instance_identifier %prec UNARY
  1091. {
  1092. $$ = $2;
  1093. $$->add_modifier(IIT_const);
  1094. }
  1095. | '*' instance_identifier %prec UNARY
  1096. {
  1097. $$ = $2;
  1098. $$->add_modifier(IIT_pointer);
  1099. }
  1100. | '&' instance_identifier %prec UNARY
  1101. {
  1102. $$ = $2;
  1103. $$->add_modifier(IIT_reference);
  1104. }
  1105. | SCOPING '*' instance_identifier %prec UNARY
  1106. {
  1107. $$ = $3;
  1108. $$->add_scoped_pointer_modifier($1);
  1109. }
  1110. | instance_identifier '[' optional_const_expr ']'
  1111. {
  1112. $$ = $1;
  1113. $$->add_array_modifier($3);
  1114. }
  1115. | instance_identifier ':' INTEGER
  1116. {
  1117. // bitfield definition. We ignore the bitfield for now.
  1118. $$ = $1;
  1119. }
  1120. | '(' instance_identifier ')'
  1121. {
  1122. $$ = $2;
  1123. $$->add_modifier(IIT_paren);
  1124. }
  1125. | instance_identifier '('
  1126. {
  1127. push_scope($1->get_scope(current_scope, global_scope));
  1128. }
  1129. formal_parameter_list ')' function_post
  1130. {
  1131. pop_scope();
  1132. $$ = $1;
  1133. if ($4->is_parameter_expr() && $6 == 0) {
  1134. // Oops, this must have been an instance declaration with a
  1135. // parameter list, not a function prototype.
  1136. $$->add_initializer_modifier($4);
  1137. } else {
  1138. // This was (probably) a function prototype.
  1139. $$->add_func_modifier($4, $6);
  1140. }
  1141. }
  1142. ;
  1143. formal_parameter_list:
  1144. empty
  1145. {
  1146. $$ = new CPPParameterList;
  1147. }
  1148. | ELLIPSIS
  1149. {
  1150. $$ = new CPPParameterList;
  1151. $$->_includes_ellipsis = true;
  1152. }
  1153. | formal_parameters
  1154. {
  1155. $$ = $1;
  1156. }
  1157. | formal_parameters ',' ELLIPSIS
  1158. {
  1159. $$ = $1;
  1160. $$->_includes_ellipsis = true;
  1161. }
  1162. | formal_parameters ELLIPSIS
  1163. {
  1164. $$ = $1;
  1165. $$->_includes_ellipsis = true;
  1166. }
  1167. ;
  1168. formal_parameters:
  1169. formal_parameter
  1170. {
  1171. $$ = new CPPParameterList;
  1172. $$->_parameters.push_back($1);
  1173. }
  1174. | formal_parameters ',' formal_parameter
  1175. {
  1176. $$ = $1;
  1177. $$->_parameters.push_back($3);
  1178. }
  1179. ;
  1180. template_parameter_maybe_initialize:
  1181. empty
  1182. {
  1183. $$ = (CPPExpression *)NULL;
  1184. }
  1185. | '=' no_angle_bracket_const_expr
  1186. {
  1187. $$ = $2;
  1188. }
  1189. ;
  1190. maybe_initialize:
  1191. empty
  1192. {
  1193. $$ = (CPPExpression *)NULL;
  1194. }
  1195. | '=' const_expr
  1196. {
  1197. $$ = $2;
  1198. }
  1199. ;
  1200. maybe_initialize_or_function_body:
  1201. ';'
  1202. {
  1203. $$ = (CPPExpression *)NULL;
  1204. }
  1205. | '{' code '}'
  1206. {
  1207. $$ = (CPPExpression *)NULL;
  1208. }
  1209. | ':' constructor_inits '{' code '}'
  1210. {
  1211. $$ = (CPPExpression *)NULL;
  1212. }
  1213. | '=' const_expr ';'
  1214. {
  1215. $$ = $2;
  1216. }
  1217. | '=' '{' structure_init '}'
  1218. {
  1219. $$ = (CPPExpression *)NULL;
  1220. }
  1221. ;
  1222. structure_init:
  1223. empty
  1224. | structure_init_body
  1225. | structure_init_body ','
  1226. ;
  1227. structure_init_body:
  1228. const_expr
  1229. {
  1230. }
  1231. | '{' structure_init '}'
  1232. | structure_init_body ',' const_expr
  1233. | structure_init_body ',' '{' structure_init '}'
  1234. ;
  1235. formal_parameter:
  1236. type formal_parameter_identifier maybe_initialize
  1237. {
  1238. $$ = new CPPInstance($1, $2, 0, @2.file);
  1239. $$->set_initializer($3);
  1240. }
  1241. | IDENTIFIER formal_parameter_identifier maybe_initialize
  1242. {
  1243. yywarning("Not a type: " + $1->get_fully_scoped_name(), @1);
  1244. CPPType *type =
  1245. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  1246. $$ = new CPPInstance(type, $2, 0, @2.file);
  1247. $$->set_initializer($3);
  1248. }
  1249. | KW_CONST type formal_parameter_identifier maybe_initialize
  1250. {
  1251. $3->add_modifier(IIT_const);
  1252. $$ = new CPPInstance($2, $3, 0, @3.file);
  1253. $$->set_initializer($4);
  1254. }
  1255. | formal_const_expr
  1256. {
  1257. CPPType *type =
  1258. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_parameter));
  1259. $$ = new CPPInstance(type, "expr");
  1260. $$->set_initializer($1);
  1261. }
  1262. ;
  1263. not_paren_formal_parameter_identifier:
  1264. empty
  1265. {
  1266. $$ = new CPPInstanceIdentifier((CPPIdentifier *)NULL);
  1267. }
  1268. | IDENTIFIER
  1269. {
  1270. $$ = new CPPInstanceIdentifier($1);
  1271. }
  1272. | TYPENAME_IDENTIFIER
  1273. {
  1274. $$ = new CPPInstanceIdentifier($1);
  1275. }
  1276. | KW_CONST not_paren_formal_parameter_identifier %prec UNARY
  1277. {
  1278. $$ = $2;
  1279. $$->add_modifier(IIT_const);
  1280. }
  1281. | '*' not_paren_formal_parameter_identifier %prec UNARY
  1282. {
  1283. $$ = $2;
  1284. $$->add_modifier(IIT_pointer);
  1285. }
  1286. | '&' not_paren_formal_parameter_identifier %prec UNARY
  1287. {
  1288. $$ = $2;
  1289. $$->add_modifier(IIT_reference);
  1290. }
  1291. | SCOPING '*' not_paren_formal_parameter_identifier %prec UNARY
  1292. {
  1293. $$ = $3;
  1294. $$->add_scoped_pointer_modifier($1);
  1295. }
  1296. | not_paren_formal_parameter_identifier '[' optional_const_expr ']'
  1297. {
  1298. $$ = $1;
  1299. $$->add_array_modifier($3);
  1300. }
  1301. ;
  1302. formal_parameter_identifier:
  1303. empty
  1304. {
  1305. $$ = new CPPInstanceIdentifier((CPPIdentifier *)NULL);
  1306. }
  1307. | IDENTIFIER
  1308. {
  1309. $$ = new CPPInstanceIdentifier($1);
  1310. }
  1311. | TYPENAME_IDENTIFIER
  1312. {
  1313. $$ = new CPPInstanceIdentifier($1);
  1314. }
  1315. | KW_CONST formal_parameter_identifier %prec UNARY
  1316. {
  1317. $$ = $2;
  1318. $$->add_modifier(IIT_const);
  1319. }
  1320. | '*' formal_parameter_identifier %prec UNARY
  1321. {
  1322. $$ = $2;
  1323. $$->add_modifier(IIT_pointer);
  1324. }
  1325. | '&' formal_parameter_identifier %prec UNARY
  1326. {
  1327. $$ = $2;
  1328. $$->add_modifier(IIT_reference);
  1329. }
  1330. | SCOPING '*' formal_parameter_identifier %prec UNARY
  1331. {
  1332. $$ = $3;
  1333. $$->add_scoped_pointer_modifier($1);
  1334. }
  1335. | formal_parameter_identifier '[' optional_const_expr ']'
  1336. {
  1337. $$ = $1;
  1338. $$->add_array_modifier($3);
  1339. }
  1340. | '(' formal_parameter_identifier ')' '(' formal_parameter_list ')' function_post
  1341. {
  1342. $$ = $2;
  1343. $$->add_modifier(IIT_paren);
  1344. $$->add_func_modifier($5, $7);
  1345. }
  1346. | '(' formal_parameter_identifier ')'
  1347. {
  1348. $$ = $2;
  1349. $$->add_modifier(IIT_paren);
  1350. }
  1351. ;
  1352. empty_instance_identifier:
  1353. empty
  1354. {
  1355. $$ = new CPPInstanceIdentifier((CPPIdentifier *)NULL);
  1356. }
  1357. | KW_CONST empty_instance_identifier %prec UNARY
  1358. {
  1359. $$ = $2;
  1360. $$->add_modifier(IIT_const);
  1361. }
  1362. | '*' empty_instance_identifier %prec UNARY
  1363. {
  1364. $$ = $2;
  1365. $$->add_modifier(IIT_pointer);
  1366. }
  1367. | '&' empty_instance_identifier %prec UNARY
  1368. {
  1369. $$ = $2;
  1370. $$->add_modifier(IIT_reference);
  1371. }
  1372. | SCOPING '*' empty_instance_identifier %prec UNARY
  1373. {
  1374. $$ = $3;
  1375. $$->add_scoped_pointer_modifier($1);
  1376. }
  1377. | empty_instance_identifier '[' optional_const_expr ']'
  1378. {
  1379. $$ = $1;
  1380. $$->add_array_modifier($3);
  1381. }
  1382. | '(' empty_instance_identifier ')' '(' formal_parameter_list ')' function_post
  1383. {
  1384. $$ = $2;
  1385. $$->add_modifier(IIT_paren);
  1386. $$->add_func_modifier($5, $7);
  1387. }
  1388. ;
  1389. type:
  1390. simple_type
  1391. {
  1392. $$ = CPPType::new_type($1);
  1393. }
  1394. | TYPENAME_IDENTIFIER
  1395. {
  1396. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1397. assert($$ != NULL);
  1398. }
  1399. | KW_TYPENAME name
  1400. {
  1401. $$ = CPPType::new_type(new CPPTBDType($2));
  1402. }
  1403. | anonymous_struct
  1404. {
  1405. $$ = CPPType::new_type($1);
  1406. }
  1407. | named_struct
  1408. {
  1409. $$ = CPPType::new_type($1);
  1410. }
  1411. | anonymous_enum
  1412. {
  1413. $$ = CPPType::new_type($1);
  1414. }
  1415. | named_enum
  1416. {
  1417. $$ = CPPType::new_type($1);
  1418. }
  1419. | struct_keyword name
  1420. {
  1421. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1422. if (type != NULL) {
  1423. $$ = type;
  1424. } else {
  1425. CPPExtensionType *et =
  1426. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1427. ->as_extension_type();
  1428. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1429. if (scope != NULL) {
  1430. scope->define_extension_type(et);
  1431. }
  1432. $$ = et;
  1433. }
  1434. }
  1435. | enum_keyword name
  1436. {
  1437. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1438. if (type != NULL) {
  1439. $$ = type;
  1440. } else {
  1441. CPPExtensionType *et =
  1442. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1443. ->as_extension_type();
  1444. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1445. if (scope != NULL) {
  1446. scope->define_extension_type(et);
  1447. }
  1448. $$ = et;
  1449. }
  1450. }
  1451. ;
  1452. type_decl:
  1453. simple_type
  1454. {
  1455. $$ = CPPType::new_type($1);
  1456. }
  1457. | TYPENAME_IDENTIFIER
  1458. {
  1459. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1460. assert($$ != NULL);
  1461. }
  1462. | KW_TYPENAME name
  1463. {
  1464. $$ = CPPType::new_type(new CPPTBDType($2));
  1465. }
  1466. | anonymous_struct
  1467. {
  1468. $$ = CPPType::new_type($1);
  1469. }
  1470. | named_struct
  1471. {
  1472. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  1473. }
  1474. | anonymous_enum
  1475. {
  1476. $$ = CPPType::new_type($1);
  1477. }
  1478. | named_enum
  1479. {
  1480. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  1481. }
  1482. | struct_keyword name
  1483. {
  1484. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1485. if (type != NULL) {
  1486. $$ = type;
  1487. } else {
  1488. CPPExtensionType *et =
  1489. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1490. ->as_extension_type();
  1491. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1492. if (scope != NULL) {
  1493. scope->define_extension_type(et);
  1494. }
  1495. $$ = et;
  1496. }
  1497. }
  1498. | enum_keyword name
  1499. {
  1500. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1501. if (type != NULL) {
  1502. $$ = type;
  1503. } else {
  1504. CPPExtensionType *et =
  1505. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1506. ->as_extension_type();
  1507. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1508. if (scope != NULL) {
  1509. scope->define_extension_type(et);
  1510. }
  1511. $$ = et;
  1512. }
  1513. }
  1514. ;
  1515. predefined_type:
  1516. simple_type
  1517. {
  1518. $$ = CPPType::new_type($1);
  1519. }
  1520. | TYPENAME_IDENTIFIER
  1521. {
  1522. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1523. assert($$ != NULL);
  1524. }
  1525. | KW_TYPENAME name
  1526. {
  1527. $$ = CPPType::new_type(new CPPTBDType($2));
  1528. }
  1529. | struct_keyword name
  1530. {
  1531. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1532. if (type != NULL) {
  1533. $$ = type;
  1534. } else {
  1535. CPPExtensionType *et =
  1536. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1537. ->as_extension_type();
  1538. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1539. if (scope != NULL) {
  1540. scope->define_extension_type(et);
  1541. }
  1542. $$ = et;
  1543. }
  1544. }
  1545. | enum_keyword name
  1546. {
  1547. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  1548. if (type != NULL) {
  1549. $$ = type;
  1550. } else {
  1551. CPPExtensionType *et =
  1552. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  1553. ->as_extension_type();
  1554. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  1555. if (scope != NULL) {
  1556. scope->define_extension_type(et);
  1557. }
  1558. $$ = et;
  1559. }
  1560. }
  1561. ;
  1562. full_type:
  1563. type empty_instance_identifier
  1564. {
  1565. CPPInstance *inst = new CPPInstance($1, $2, 0, @1.file);
  1566. $$ = inst->_type;
  1567. delete inst;
  1568. }
  1569. | KW_CONST type empty_instance_identifier
  1570. {
  1571. $3->add_modifier(IIT_const);
  1572. CPPInstance *inst = new CPPInstance($2, $3, 0, @1.file);
  1573. $$ = inst->_type;
  1574. delete inst;
  1575. }
  1576. ;
  1577. anonymous_struct:
  1578. struct_keyword '{'
  1579. {
  1580. CPPVisibility starting_vis =
  1581. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  1582. CPPScope *new_scope = new CPPScope(current_scope, CPPNameComponent("anon"),
  1583. starting_vis);
  1584. CPPStructType *st = new CPPStructType($1, NULL, current_scope,
  1585. new_scope, @1.file);
  1586. new_scope->set_struct_type(st);
  1587. push_scope(new_scope);
  1588. push_struct(st);
  1589. }
  1590. cpp '}'
  1591. {
  1592. $$ = current_struct;
  1593. current_struct->_incomplete = false;
  1594. pop_struct();
  1595. pop_scope();
  1596. }
  1597. ;
  1598. named_struct:
  1599. struct_keyword name
  1600. {
  1601. CPPVisibility starting_vis =
  1602. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  1603. CPPScope *scope = $2->get_scope(current_scope, global_scope, current_lexer);
  1604. if (scope == NULL) {
  1605. scope = current_scope;
  1606. }
  1607. CPPScope *new_scope = new CPPScope(scope, $2->_names.back(),
  1608. starting_vis);
  1609. CPPStructType *st = new CPPStructType($1, $2, current_scope,
  1610. new_scope, @1.file);
  1611. new_scope->set_struct_type(st);
  1612. current_scope->define_extension_type(st);
  1613. push_scope(new_scope);
  1614. push_struct(st);
  1615. }
  1616. maybe_class_derivation '{' cpp '}'
  1617. {
  1618. $$ = current_struct;
  1619. current_struct->_incomplete = false;
  1620. pop_struct();
  1621. pop_scope();
  1622. }
  1623. ;
  1624. maybe_class_derivation:
  1625. empty
  1626. | class_derivation
  1627. ;
  1628. class_derivation:
  1629. ':' base_specification
  1630. | class_derivation ',' base_specification
  1631. ;
  1632. base_specification:
  1633. KW_PUBLIC class_derivation_name
  1634. {
  1635. current_struct->append_derivation($2, V_public, false);
  1636. }
  1637. | KW_PROTECTED class_derivation_name
  1638. {
  1639. current_struct->append_derivation($2, V_protected, false);
  1640. }
  1641. | KW_PRIVATE class_derivation_name
  1642. {
  1643. current_struct->append_derivation($2, V_private, false);
  1644. }
  1645. | KW_VIRTUAL KW_PUBLIC class_derivation_name
  1646. {
  1647. current_struct->append_derivation($3, V_public, true);
  1648. }
  1649. | KW_VIRTUAL KW_PROTECTED class_derivation_name
  1650. {
  1651. current_struct->append_derivation($3, V_protected, true);
  1652. }
  1653. | KW_VIRTUAL KW_PRIVATE class_derivation_name
  1654. {
  1655. current_struct->append_derivation($3, V_private, true);
  1656. }
  1657. | KW_PUBLIC KW_VIRTUAL class_derivation_name
  1658. {
  1659. current_struct->append_derivation($3, V_public, true);
  1660. }
  1661. | KW_PROTECTED KW_VIRTUAL class_derivation_name
  1662. {
  1663. current_struct->append_derivation($3, V_protected, true);
  1664. }
  1665. | KW_PRIVATE KW_VIRTUAL class_derivation_name
  1666. {
  1667. current_struct->append_derivation($3, V_private, true);
  1668. }
  1669. ;
  1670. anonymous_enum:
  1671. enum_keyword '{'
  1672. {
  1673. current_enum = new CPPEnumType(NULL, current_scope, @1.file);
  1674. }
  1675. enum_body '}'
  1676. {
  1677. $$ = current_enum;
  1678. current_enum = NULL;
  1679. }
  1680. ;
  1681. named_enum:
  1682. enum_keyword name '{'
  1683. {
  1684. current_enum = new CPPEnumType($2, current_scope, @1.file);
  1685. }
  1686. enum_body '}'
  1687. {
  1688. $$ = current_enum;
  1689. current_enum = NULL;
  1690. }
  1691. ;
  1692. enum_body:
  1693. empty
  1694. | enum_body_no_trailing_comma
  1695. | enum_body_no_trailing_comma ','
  1696. ;
  1697. enum_body_no_trailing_comma:
  1698. name
  1699. {
  1700. assert(current_enum != NULL);
  1701. current_enum->add_element($1->get_simple_name(), current_scope);
  1702. }
  1703. | name '=' const_expr
  1704. {
  1705. assert(current_enum != NULL);
  1706. current_enum->add_element($1->get_simple_name(), current_scope, $3);
  1707. }
  1708. | enum_body_no_trailing_comma ',' name
  1709. {
  1710. assert(current_enum != NULL);
  1711. current_enum->add_element($3->get_simple_name(), current_scope);
  1712. }
  1713. | enum_body_no_trailing_comma ',' name '=' const_expr
  1714. {
  1715. assert(current_enum != NULL);
  1716. current_enum->add_element($3->get_simple_name(), current_scope, $5);
  1717. }
  1718. ;
  1719. enum_keyword:
  1720. KW_ENUM
  1721. {
  1722. $$ = CPPExtensionType::T_enum;
  1723. }
  1724. ;
  1725. struct_keyword:
  1726. KW_CLASS
  1727. {
  1728. $$ = CPPExtensionType::T_class;
  1729. }
  1730. | KW_STRUCT
  1731. {
  1732. $$ = CPPExtensionType::T_struct;
  1733. }
  1734. | KW_UNION
  1735. {
  1736. $$ = CPPExtensionType::T_union;
  1737. }
  1738. ;
  1739. namespace_declaration:
  1740. KW_NAMESPACE name '{'
  1741. {
  1742. CPPScope *scope = $2->find_scope(current_scope, global_scope, current_lexer);
  1743. if (scope == NULL) {
  1744. // This must be a new namespace declaration.
  1745. CPPScope *parent_scope =
  1746. $2->get_scope(current_scope, global_scope, current_lexer);
  1747. if (parent_scope == NULL) {
  1748. parent_scope = current_scope;
  1749. }
  1750. scope = new CPPScope(parent_scope, $2->_names.back(), V_public);
  1751. }
  1752. CPPNamespace *nspace = new CPPNamespace($2, scope, @1.file);
  1753. current_scope->add_declaration(nspace, global_scope, current_lexer, @1);
  1754. current_scope->define_namespace(nspace);
  1755. push_scope(scope);
  1756. }
  1757. cpp '}'
  1758. {
  1759. pop_scope();
  1760. }
  1761. | KW_NAMESPACE '{' cpp '}'
  1762. ;
  1763. using_declaration:
  1764. KW_USING name
  1765. {
  1766. CPPUsing *using_decl = new CPPUsing($2, false, @1.file);
  1767. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  1768. current_scope->add_using(using_decl, global_scope, current_lexer);
  1769. }
  1770. | KW_USING KW_NAMESPACE name
  1771. {
  1772. CPPUsing *using_decl = new CPPUsing($3, true, @1.file);
  1773. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  1774. current_scope->add_using(using_decl, global_scope, current_lexer);
  1775. }
  1776. ;
  1777. simple_type:
  1778. simple_int_type
  1779. | simple_float_type
  1780. | simple_void_type
  1781. ;
  1782. simple_int_type:
  1783. KW_BOOL
  1784. {
  1785. $$ = new CPPSimpleType(CPPSimpleType::T_bool);
  1786. }
  1787. | KW_CHAR
  1788. {
  1789. $$ = new CPPSimpleType(CPPSimpleType::T_char);
  1790. }
  1791. | KW_WCHAR_T
  1792. {
  1793. $$ = new CPPSimpleType(CPPSimpleType::T_wchar_t);
  1794. }
  1795. | KW_SHORT
  1796. {
  1797. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  1798. CPPSimpleType::F_short);
  1799. }
  1800. | KW_LONG
  1801. {
  1802. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  1803. CPPSimpleType::F_long);
  1804. }
  1805. | KW_LONGLONG
  1806. {
  1807. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  1808. CPPSimpleType::F_longlong);
  1809. }
  1810. | KW_UNSIGNED
  1811. {
  1812. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  1813. CPPSimpleType::F_unsigned);
  1814. }
  1815. | KW_SIGNED
  1816. {
  1817. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  1818. CPPSimpleType::F_signed);
  1819. }
  1820. | KW_INT
  1821. {
  1822. $$ = new CPPSimpleType(CPPSimpleType::T_int);
  1823. }
  1824. | KW_SHORT simple_int_type
  1825. {
  1826. $$ = $2;
  1827. $$->_flags |= CPPSimpleType::F_short;
  1828. }
  1829. | KW_LONG simple_int_type
  1830. {
  1831. $$ = $2;
  1832. if ($$->_flags & CPPSimpleType::F_long) {
  1833. $$->_flags |= CPPSimpleType::F_longlong;
  1834. } else {
  1835. $$->_flags |= CPPSimpleType::F_long;
  1836. }
  1837. }
  1838. | KW_UNSIGNED simple_int_type
  1839. {
  1840. $$ = $2;
  1841. $$->_flags |= CPPSimpleType::F_unsigned;
  1842. }
  1843. | KW_SIGNED simple_int_type
  1844. {
  1845. $$ = $2;
  1846. $$->_flags |= CPPSimpleType::F_signed;
  1847. }
  1848. ;
  1849. simple_float_type:
  1850. KW_FLOAT
  1851. {
  1852. $$ = new CPPSimpleType(CPPSimpleType::T_float);
  1853. }
  1854. | KW_LONG KW_FLOAT
  1855. {
  1856. $$ = new CPPSimpleType(CPPSimpleType::T_float,
  1857. CPPSimpleType::F_long);
  1858. }
  1859. | KW_DOUBLE
  1860. {
  1861. $$ = new CPPSimpleType(CPPSimpleType::T_double);
  1862. }
  1863. | KW_LONG KW_LONG KW_FLOAT
  1864. {
  1865. $$ = new CPPSimpleType(CPPSimpleType::T_float,
  1866. CPPSimpleType::F_longlong);
  1867. }
  1868. | KW_LONG KW_DOUBLE
  1869. {
  1870. $$ = new CPPSimpleType(CPPSimpleType::T_double,
  1871. CPPSimpleType::F_long);
  1872. }
  1873. ;
  1874. simple_void_type:
  1875. KW_VOID
  1876. {
  1877. $$ = new CPPSimpleType(CPPSimpleType::T_void);
  1878. }
  1879. ;
  1880. /* We don't care what the code is. We just want to be sure we match
  1881. up opening and closing braces properly. For anything else, we'll
  1882. accept just token salad. */
  1883. code:
  1884. {
  1885. current_lexer->_resolve_identifiers = false;
  1886. }
  1887. code_block
  1888. {
  1889. current_lexer->_resolve_identifiers = true;
  1890. }
  1891. ;
  1892. code_block:
  1893. empty
  1894. | code_block element
  1895. ;
  1896. element:
  1897. REAL
  1898. {
  1899. }
  1900. | INTEGER
  1901. {
  1902. }
  1903. | STRING
  1904. {
  1905. }
  1906. | CHAR_TOK
  1907. {
  1908. }
  1909. | IDENTIFIER
  1910. {
  1911. }
  1912. | TYPENAME_IDENTIFIER
  1913. {
  1914. }
  1915. | SCOPING
  1916. {
  1917. }
  1918. | SIMPLE_IDENTIFIER
  1919. {
  1920. }
  1921. | ELLIPSIS | OROR | ANDAND
  1922. | EQCOMPARE | NECOMPARE | LECOMPARE | GECOMPARE
  1923. | LSHIFT | RSHIFT | POINTSAT_STAR | DOT_STAR | POINTSAT
  1924. | SCOPE | PLUSPLUS | MINUSMINUS
  1925. | TIMESEQUAL | DIVIDEEQUAL | MODEQUAL | PLUSEQUAL | MINUSEQUAL
  1926. | OREQUAL | ANDEQUAL | XOREQUAL | LSHIFTEQUAL | RSHIFTEQUAL
  1927. | KW_BOOL | KW_CATCH | KW_CHAR | KW_WCHAR_T | KW_CLASS | KW_CONST
  1928. | KW_DELETE | KW_DOUBLE | KW_DYNAMIC_CAST | KW_ELSE | KW_ENUM
  1929. | KW_EXTERN | KW_EXPLICIT | KW_FALSE
  1930. | KW_FLOAT | KW_FRIEND | KW_FOR | KW_GOTO
  1931. | KW_IF | KW_INLINE | KW_INT
  1932. | KW_LONG | KW_MUTABLE | KW_NEW | KW_PRIVATE | KW_PROTECTED
  1933. | KW_PUBLIC | KW_PUBLISHED | KW_REGISTER | KW_RETURN
  1934. | KW_SHORT | KW_SIGNED | KW_SIZEOF | KW_STATIC | KW_STATIC_CAST
  1935. | KW_STRUCT | KW_THROW | KW_TRUE | KW_TRY | KW_TYPEDEF | KW_TYPENAME
  1936. | KW_UNION | KW_UNSIGNED | KW_VIRTUAL | KW_VOID | KW_VOLATILE
  1937. | KW_WHILE | TOKENPASTE
  1938. | KW_OPERATOR
  1939. {
  1940. }
  1941. | '+' | '-' | '*' | '/' | '&' | '|' | '^' | '!' | '~' | '=' | '%'
  1942. | '<' | '>' | '(' | ')' | '.' | ',' | ';' | ':' | '[' | ']'
  1943. | '?' | '{' code_block '}'
  1944. ;
  1945. optional_const_expr:
  1946. empty
  1947. {
  1948. $$ = (CPPExpression *)NULL;
  1949. }
  1950. | const_expr
  1951. {
  1952. $$ = $1;
  1953. }
  1954. ;
  1955. optional_const_expr_comma:
  1956. empty
  1957. {
  1958. $$ = (CPPExpression *)NULL;
  1959. }
  1960. | const_expr_comma
  1961. {
  1962. $$ = $1;
  1963. }
  1964. ;
  1965. const_expr_comma:
  1966. const_expr
  1967. {
  1968. $$ = $1;
  1969. }
  1970. | const_expr_comma ',' const_expr
  1971. {
  1972. $$ = new CPPExpression(',', $1, $3);
  1973. }
  1974. ;
  1975. no_angle_bracket_const_expr:
  1976. const_operand
  1977. {
  1978. $$ = $1;
  1979. }
  1980. | '(' full_type ')' no_angle_bracket_const_expr %prec UNARY
  1981. {
  1982. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  1983. }
  1984. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  1985. {
  1986. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  1987. }
  1988. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  1989. {
  1990. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  1991. }
  1992. | KW_SIZEOF '(' full_type ')' %prec UNARY
  1993. {
  1994. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  1995. }
  1996. | '!' no_angle_bracket_const_expr %prec UNARY
  1997. {
  1998. $$ = new CPPExpression(UNARY_NOT, $2);
  1999. }
  2000. | '~' no_angle_bracket_const_expr %prec UNARY
  2001. {
  2002. $$ = new CPPExpression(UNARY_NEGATE, $2);
  2003. }
  2004. | '-' no_angle_bracket_const_expr %prec UNARY
  2005. {
  2006. if ($2->_type == CPPExpression::T_integer) {
  2007. $$ = $2;
  2008. $$->_u._integer = -$$->_u._integer;
  2009. } else if ($2->_type == CPPExpression::T_real) {
  2010. $$ = $2;
  2011. $$->_u._real = -$$->_u._real;
  2012. } else {
  2013. $$ = new CPPExpression(UNARY_MINUS, $2);
  2014. }
  2015. }
  2016. | '*' no_angle_bracket_const_expr %prec UNARY
  2017. {
  2018. $$ = new CPPExpression(UNARY_STAR, $2);
  2019. }
  2020. | '&' no_angle_bracket_const_expr %prec UNARY
  2021. {
  2022. $$ = new CPPExpression(UNARY_REF, $2);
  2023. }
  2024. | no_angle_bracket_const_expr '*' no_angle_bracket_const_expr
  2025. {
  2026. $$ = new CPPExpression('*', $1, $3);
  2027. }
  2028. | no_angle_bracket_const_expr '/' no_angle_bracket_const_expr
  2029. {
  2030. $$ = new CPPExpression('/', $1, $3);
  2031. }
  2032. | no_angle_bracket_const_expr '%' no_angle_bracket_const_expr
  2033. {
  2034. $$ = new CPPExpression('%', $1, $3);
  2035. }
  2036. | no_angle_bracket_const_expr '+' no_angle_bracket_const_expr
  2037. {
  2038. $$ = new CPPExpression('+', $1, $3);
  2039. }
  2040. | no_angle_bracket_const_expr '-' no_angle_bracket_const_expr
  2041. {
  2042. $$ = new CPPExpression('-', $1, $3);
  2043. }
  2044. | no_angle_bracket_const_expr '|' no_angle_bracket_const_expr
  2045. {
  2046. $$ = new CPPExpression('|', $1, $3);
  2047. }
  2048. | no_angle_bracket_const_expr '^' no_angle_bracket_const_expr
  2049. {
  2050. $$ = new CPPExpression('^', $1, $3);
  2051. }
  2052. | no_angle_bracket_const_expr '&' no_angle_bracket_const_expr
  2053. {
  2054. $$ = new CPPExpression('&', $1, $3);
  2055. }
  2056. | no_angle_bracket_const_expr OROR no_angle_bracket_const_expr
  2057. {
  2058. $$ = new CPPExpression(OROR, $1, $3);
  2059. }
  2060. | no_angle_bracket_const_expr ANDAND no_angle_bracket_const_expr
  2061. {
  2062. $$ = new CPPExpression(ANDAND, $1, $3);
  2063. }
  2064. | no_angle_bracket_const_expr EQCOMPARE no_angle_bracket_const_expr
  2065. {
  2066. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  2067. }
  2068. | no_angle_bracket_const_expr NECOMPARE no_angle_bracket_const_expr
  2069. {
  2070. $$ = new CPPExpression(NECOMPARE, $1, $3);
  2071. }
  2072. | no_angle_bracket_const_expr LECOMPARE no_angle_bracket_const_expr
  2073. {
  2074. $$ = new CPPExpression(LECOMPARE, $1, $3);
  2075. }
  2076. | no_angle_bracket_const_expr GECOMPARE no_angle_bracket_const_expr
  2077. {
  2078. $$ = new CPPExpression(GECOMPARE, $1, $3);
  2079. }
  2080. | no_angle_bracket_const_expr LSHIFT no_angle_bracket_const_expr
  2081. {
  2082. $$ = new CPPExpression(LSHIFT, $1, $3);
  2083. }
  2084. | no_angle_bracket_const_expr RSHIFT no_angle_bracket_const_expr
  2085. {
  2086. $$ = new CPPExpression(RSHIFT, $1, $3);
  2087. }
  2088. | no_angle_bracket_const_expr '?' no_angle_bracket_const_expr ':' no_angle_bracket_const_expr
  2089. {
  2090. $$ = new CPPExpression('?', $1, $3, $5);
  2091. }
  2092. | no_angle_bracket_const_expr '[' const_expr ']'
  2093. {
  2094. $$ = new CPPExpression('[', $1, $3);
  2095. }
  2096. | no_angle_bracket_const_expr '(' const_expr_comma ')'
  2097. {
  2098. $$ = new CPPExpression('f', $1, $3);
  2099. }
  2100. | no_angle_bracket_const_expr '(' ')'
  2101. {
  2102. $$ = new CPPExpression('f', $1);
  2103. }
  2104. | no_angle_bracket_const_expr '.' no_angle_bracket_const_expr
  2105. {
  2106. $$ = new CPPExpression('.', $1, $3);
  2107. }
  2108. | no_angle_bracket_const_expr POINTSAT no_angle_bracket_const_expr
  2109. {
  2110. $$ = new CPPExpression(POINTSAT, $1, $3);
  2111. }
  2112. | '(' const_expr_comma ')'
  2113. {
  2114. $$ = $2;
  2115. }
  2116. ;
  2117. const_expr:
  2118. const_operand
  2119. {
  2120. $$ = $1;
  2121. }
  2122. | '(' full_type ')' const_expr %prec UNARY
  2123. {
  2124. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  2125. }
  2126. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  2127. {
  2128. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  2129. }
  2130. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  2131. {
  2132. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  2133. }
  2134. | TYPENAME_IDENTIFIER '(' optional_const_expr_comma ')'
  2135. {
  2136. // A constructor call.
  2137. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  2138. assert(type != NULL);
  2139. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2140. }
  2141. | KW_INT '(' optional_const_expr_comma ')'
  2142. {
  2143. CPPType *type =
  2144. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  2145. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2146. }
  2147. | KW_CHAR '(' optional_const_expr_comma ')'
  2148. {
  2149. CPPType *type =
  2150. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  2151. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2152. }
  2153. | KW_WCHAR_T '(' optional_const_expr_comma ')'
  2154. {
  2155. CPPType *type =
  2156. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
  2157. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2158. }
  2159. | KW_BOOL '(' optional_const_expr_comma ')'
  2160. {
  2161. CPPType *type =
  2162. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  2163. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2164. }
  2165. | KW_SHORT '(' optional_const_expr_comma ')'
  2166. {
  2167. CPPType *type =
  2168. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  2169. CPPSimpleType::F_short));
  2170. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2171. }
  2172. | KW_LONG '(' optional_const_expr_comma ')'
  2173. {
  2174. CPPType *type =
  2175. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  2176. CPPSimpleType::F_long));
  2177. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2178. }
  2179. | KW_UNSIGNED '(' optional_const_expr_comma ')'
  2180. {
  2181. CPPType *type =
  2182. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  2183. CPPSimpleType::F_unsigned));
  2184. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2185. }
  2186. | KW_SIGNED '(' optional_const_expr_comma ')'
  2187. {
  2188. CPPType *type =
  2189. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  2190. CPPSimpleType::F_signed));
  2191. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2192. }
  2193. | KW_FLOAT '(' optional_const_expr_comma ')'
  2194. {
  2195. CPPType *type =
  2196. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_float));
  2197. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2198. }
  2199. | KW_DOUBLE '(' optional_const_expr_comma ')'
  2200. {
  2201. CPPType *type =
  2202. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  2203. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  2204. }
  2205. | KW_SIZEOF '(' full_type ')' %prec UNARY
  2206. {
  2207. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  2208. }
  2209. | KW_NEW predefined_type %prec UNARY
  2210. {
  2211. $$ = new CPPExpression(CPPExpression::new_op($2));
  2212. }
  2213. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  2214. {
  2215. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  2216. }
  2217. | '!' const_expr %prec UNARY
  2218. {
  2219. $$ = new CPPExpression(UNARY_NOT, $2);
  2220. }
  2221. | '~' const_expr %prec UNARY
  2222. {
  2223. $$ = new CPPExpression(UNARY_NEGATE, $2);
  2224. }
  2225. | '-' const_expr %prec UNARY
  2226. {
  2227. if ($2->_type == CPPExpression::T_integer) {
  2228. $$ = $2;
  2229. $$->_u._integer = -$$->_u._integer;
  2230. } else if ($2->_type == CPPExpression::T_real) {
  2231. $$ = $2;
  2232. $$->_u._real = -$$->_u._real;
  2233. } else {
  2234. $$ = new CPPExpression(UNARY_MINUS, $2);
  2235. }
  2236. }
  2237. | '*' const_expr %prec UNARY
  2238. {
  2239. $$ = new CPPExpression(UNARY_STAR, $2);
  2240. }
  2241. | '&' const_expr %prec UNARY
  2242. {
  2243. $$ = new CPPExpression(UNARY_REF, $2);
  2244. }
  2245. | const_expr '*' const_expr
  2246. {
  2247. $$ = new CPPExpression('*', $1, $3);
  2248. }
  2249. | const_expr '/' const_expr
  2250. {
  2251. $$ = new CPPExpression('/', $1, $3);
  2252. }
  2253. | const_expr '%' const_expr
  2254. {
  2255. $$ = new CPPExpression('%', $1, $3);
  2256. }
  2257. | const_expr '+' const_expr
  2258. {
  2259. $$ = new CPPExpression('+', $1, $3);
  2260. }
  2261. | const_expr '-' const_expr
  2262. {
  2263. $$ = new CPPExpression('-', $1, $3);
  2264. }
  2265. | const_expr '|' const_expr
  2266. {
  2267. $$ = new CPPExpression('|', $1, $3);
  2268. }
  2269. | const_expr '^' const_expr
  2270. {
  2271. $$ = new CPPExpression('^', $1, $3);
  2272. }
  2273. | const_expr '&' const_expr
  2274. {
  2275. $$ = new CPPExpression('&', $1, $3);
  2276. }
  2277. | const_expr OROR const_expr
  2278. {
  2279. $$ = new CPPExpression(OROR, $1, $3);
  2280. }
  2281. | const_expr ANDAND const_expr
  2282. {
  2283. $$ = new CPPExpression(ANDAND, $1, $3);
  2284. }
  2285. | const_expr EQCOMPARE const_expr
  2286. {
  2287. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  2288. }
  2289. | const_expr NECOMPARE const_expr
  2290. {
  2291. $$ = new CPPExpression(NECOMPARE, $1, $3);
  2292. }
  2293. | const_expr LECOMPARE const_expr
  2294. {
  2295. $$ = new CPPExpression(LECOMPARE, $1, $3);
  2296. }
  2297. | const_expr GECOMPARE const_expr
  2298. {
  2299. $$ = new CPPExpression(GECOMPARE, $1, $3);
  2300. }
  2301. | const_expr '<' const_expr
  2302. {
  2303. $$ = new CPPExpression('<', $1, $3);
  2304. }
  2305. | const_expr '>' const_expr
  2306. {
  2307. $$ = new CPPExpression('>', $1, $3);
  2308. }
  2309. | const_expr LSHIFT const_expr
  2310. {
  2311. $$ = new CPPExpression(LSHIFT, $1, $3);
  2312. }
  2313. | const_expr RSHIFT const_expr
  2314. {
  2315. $$ = new CPPExpression(RSHIFT, $1, $3);
  2316. }
  2317. | const_expr '?' const_expr ':' const_expr
  2318. {
  2319. $$ = new CPPExpression('?', $1, $3, $5);
  2320. }
  2321. | const_expr '[' const_expr ']'
  2322. {
  2323. $$ = new CPPExpression('[', $1, $3);
  2324. }
  2325. | const_expr '(' const_expr_comma ')'
  2326. {
  2327. $$ = new CPPExpression('f', $1, $3);
  2328. }
  2329. | const_expr '(' ')'
  2330. {
  2331. $$ = new CPPExpression('f', $1);
  2332. }
  2333. | const_expr '.' const_expr
  2334. {
  2335. $$ = new CPPExpression('.', $1, $3);
  2336. }
  2337. | const_expr POINTSAT const_expr
  2338. {
  2339. $$ = new CPPExpression(POINTSAT, $1, $3);
  2340. }
  2341. | '(' const_expr_comma ')'
  2342. {
  2343. $$ = $2;
  2344. }
  2345. ;
  2346. const_operand:
  2347. INTEGER
  2348. {
  2349. $$ = new CPPExpression($1);
  2350. }
  2351. | KW_TRUE
  2352. {
  2353. $$ = new CPPExpression(true);
  2354. }
  2355. | KW_FALSE
  2356. {
  2357. $$ = new CPPExpression(false);
  2358. }
  2359. | CHAR_TOK
  2360. {
  2361. $$ = new CPPExpression($1);
  2362. }
  2363. | REAL
  2364. {
  2365. $$ = new CPPExpression($1);
  2366. }
  2367. | string
  2368. {
  2369. $$ = new CPPExpression($1);
  2370. }
  2371. | IDENTIFIER
  2372. {
  2373. $$ = new CPPExpression($1, current_scope, global_scope, current_lexer);
  2374. }
  2375. ;
  2376. /* This is used for a const_expr as a "formal parameter", which really
  2377. means an instance declaration using a parameter list (which looks a
  2378. lot like a function prototype). It differs from const_expr mainly
  2379. in that it forbids some expressions unless they are parenthesized,
  2380. to avoid shift/reduce conflicts with the actual formal parameter
  2381. definition. */
  2382. formal_const_expr:
  2383. formal_const_operand
  2384. {
  2385. $$ = $1;
  2386. }
  2387. | '(' full_type ')' const_expr %prec UNARY
  2388. {
  2389. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  2390. }
  2391. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  2392. {
  2393. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  2394. }
  2395. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  2396. {
  2397. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6));
  2398. }
  2399. | KW_SIZEOF '(' full_type ')' %prec UNARY
  2400. {
  2401. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  2402. }
  2403. | KW_NEW predefined_type %prec UNARY
  2404. {
  2405. $$ = new CPPExpression(CPPExpression::new_op($2));
  2406. }
  2407. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  2408. {
  2409. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  2410. }
  2411. | '!' const_expr %prec UNARY
  2412. {
  2413. $$ = new CPPExpression(UNARY_NOT, $2);
  2414. }
  2415. | '~' const_expr %prec UNARY
  2416. {
  2417. $$ = new CPPExpression(UNARY_NEGATE, $2);
  2418. }
  2419. | '-' const_expr %prec UNARY
  2420. {
  2421. if ($2->_type == CPPExpression::T_integer) {
  2422. $$ = $2;
  2423. $$->_u._integer = -$$->_u._integer;
  2424. } else if ($2->_type == CPPExpression::T_real) {
  2425. $$ = $2;
  2426. $$->_u._real = -$$->_u._real;
  2427. } else {
  2428. $$ = new CPPExpression(UNARY_MINUS, $2);
  2429. }
  2430. }
  2431. | '&' const_expr %prec UNARY
  2432. {
  2433. $$ = new CPPExpression(UNARY_REF, $2);
  2434. }
  2435. | formal_const_expr '*' const_expr
  2436. {
  2437. $$ = new CPPExpression('*', $1, $3);
  2438. }
  2439. | formal_const_expr '/' const_expr
  2440. {
  2441. $$ = new CPPExpression('/', $1, $3);
  2442. }
  2443. | formal_const_expr '%' const_expr
  2444. {
  2445. $$ = new CPPExpression('%', $1, $3);
  2446. }
  2447. | formal_const_expr '+' const_expr
  2448. {
  2449. $$ = new CPPExpression('+', $1, $3);
  2450. }
  2451. | formal_const_expr '-' const_expr
  2452. {
  2453. $$ = new CPPExpression('-', $1, $3);
  2454. }
  2455. | formal_const_expr '|' const_expr
  2456. {
  2457. $$ = new CPPExpression('|', $1, $3);
  2458. }
  2459. | formal_const_expr '^' const_expr
  2460. {
  2461. $$ = new CPPExpression('^', $1, $3);
  2462. }
  2463. | formal_const_expr '&' const_expr
  2464. {
  2465. $$ = new CPPExpression('&', $1, $3);
  2466. }
  2467. | formal_const_expr OROR const_expr
  2468. {
  2469. $$ = new CPPExpression(OROR, $1, $3);
  2470. }
  2471. | formal_const_expr ANDAND const_expr
  2472. {
  2473. $$ = new CPPExpression(ANDAND, $1, $3);
  2474. }
  2475. | formal_const_expr EQCOMPARE const_expr
  2476. {
  2477. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  2478. }
  2479. | formal_const_expr NECOMPARE const_expr
  2480. {
  2481. $$ = new CPPExpression(NECOMPARE, $1, $3);
  2482. }
  2483. | formal_const_expr LECOMPARE const_expr
  2484. {
  2485. $$ = new CPPExpression(LECOMPARE, $1, $3);
  2486. }
  2487. | formal_const_expr GECOMPARE const_expr
  2488. {
  2489. $$ = new CPPExpression(GECOMPARE, $1, $3);
  2490. }
  2491. | formal_const_expr '<' const_expr
  2492. {
  2493. $$ = new CPPExpression('<', $1, $3);
  2494. }
  2495. | formal_const_expr '>' const_expr
  2496. {
  2497. $$ = new CPPExpression('>', $1, $3);
  2498. }
  2499. | formal_const_expr LSHIFT const_expr
  2500. {
  2501. $$ = new CPPExpression(LSHIFT, $1, $3);
  2502. }
  2503. | formal_const_expr RSHIFT const_expr
  2504. {
  2505. $$ = new CPPExpression(RSHIFT, $1, $3);
  2506. }
  2507. | formal_const_expr '?' const_expr ':' const_expr
  2508. {
  2509. $$ = new CPPExpression('?', $1, $3, $5);
  2510. }
  2511. | formal_const_expr '[' const_expr ']'
  2512. {
  2513. $$ = new CPPExpression('[', $1, $3);
  2514. }
  2515. | formal_const_expr '(' const_expr_comma ')'
  2516. {
  2517. $$ = new CPPExpression('f', $1, $3);
  2518. }
  2519. | formal_const_expr '(' ')'
  2520. {
  2521. $$ = new CPPExpression('f', $1);
  2522. }
  2523. | formal_const_expr '.' const_expr
  2524. {
  2525. $$ = new CPPExpression('.', $1, $3);
  2526. }
  2527. | formal_const_expr POINTSAT const_expr
  2528. {
  2529. $$ = new CPPExpression(POINTSAT, $1, $3);
  2530. }
  2531. | '(' const_expr_comma ')'
  2532. {
  2533. $$ = $2;
  2534. }
  2535. ;
  2536. formal_const_operand:
  2537. INTEGER
  2538. {
  2539. $$ = new CPPExpression($1);
  2540. }
  2541. | KW_TRUE
  2542. {
  2543. $$ = new CPPExpression(true);
  2544. }
  2545. | KW_FALSE
  2546. {
  2547. $$ = new CPPExpression(false);
  2548. }
  2549. | CHAR_TOK
  2550. {
  2551. $$ = new CPPExpression($1);
  2552. }
  2553. | REAL
  2554. {
  2555. $$ = new CPPExpression($1);
  2556. }
  2557. | string
  2558. {
  2559. $$ = new CPPExpression($1);
  2560. }
  2561. ;
  2562. class_derivation_name:
  2563. name
  2564. {
  2565. CPPType *type = $1->find_type(current_scope, global_scope, true);
  2566. if (type == NULL) {
  2567. type = CPPType::new_type(new CPPTBDType($1));
  2568. }
  2569. $$ = type;
  2570. }
  2571. | struct_keyword name
  2572. {
  2573. CPPType *type = $2->find_type(current_scope, global_scope, true, current_lexer);
  2574. if (type == NULL) {
  2575. type = CPPType::new_type(new CPPTBDType($2));
  2576. }
  2577. $$ = type;
  2578. }
  2579. | KW_TYPENAME name
  2580. {
  2581. $$ = CPPType::new_type(new CPPTBDType($2));
  2582. }
  2583. ;
  2584. /*
  2585. typedefname:
  2586. TYPENAME_IDENTIFIER
  2587. {
  2588. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  2589. assert(type != NULL);
  2590. $$ = type;
  2591. }
  2592. | KW_TYPENAME name
  2593. {
  2594. $$ = CPPType::new_type(new CPPTBDType($2));
  2595. }
  2596. ;
  2597. */
  2598. name:
  2599. IDENTIFIER
  2600. {
  2601. $$ = $1;
  2602. }
  2603. | TYPENAME_IDENTIFIER
  2604. {
  2605. $$ = $1;
  2606. }
  2607. ;
  2608. string:
  2609. STRING
  2610. {
  2611. $$ = $1;
  2612. }
  2613. | string STRING
  2614. {
  2615. $$ = $1 + $2;
  2616. }
  2617. ;
  2618. empty:
  2619. ;