cppBison.yxx 64 KB

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