cppBison.yxx 62 KB

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