cppBison.yxx 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289
  1. /**
  2. * @file cppBison.yxx
  3. * @author drose
  4. * @date 1999-01-16
  5. */
  6. %{
  7. #include "cppBisonDefs.h"
  8. #include "cppParser.h"
  9. #include "cppClosureType.h"
  10. #include "cppExpression.h"
  11. #include "cppSimpleType.h"
  12. #include "cppExtensionType.h"
  13. #include "cppStructType.h"
  14. #include "cppEnumType.h"
  15. #include "cppFunctionType.h"
  16. #include "cppTBDType.h"
  17. #include "cppMakeProperty.h"
  18. #include "cppMakeSeq.h"
  19. #include "cppParameterList.h"
  20. #include "cppInstance.h"
  21. #include "cppClassTemplateParameter.h"
  22. #include "cppTemplateParameterList.h"
  23. #include "cppInstanceIdentifier.h"
  24. #include "cppTypedefType.h"
  25. #include "cppTypeDeclaration.h"
  26. #include "cppVisibility.h"
  27. #include "cppIdentifier.h"
  28. #include "cppScope.h"
  29. #include "cppTemplateScope.h"
  30. #include "cppNamespace.h"
  31. #include "cppUsing.h"
  32. using std::stringstream;
  33. using std::string;
  34. ////////////////////////////////////////////////////////////////////
  35. // Defining the interface to the parser.
  36. ////////////////////////////////////////////////////////////////////
  37. CPPScope *current_scope = nullptr;
  38. CPPScope *global_scope = nullptr;
  39. CPPPreprocessor *current_lexer = nullptr;
  40. static CPPStructType *current_struct = nullptr;
  41. static CPPEnumType *current_enum = nullptr;
  42. static int current_storage_class = 0;
  43. static CPPType *current_type = nullptr;
  44. static CPPExpression *current_expr = nullptr;
  45. static int publish_nest_level = 0;
  46. static CPPVisibility publish_previous;
  47. static YYLTYPE publish_loc;
  48. static std::vector<CPPScope *> last_scopes;
  49. static std::vector<int> last_storage_classes;
  50. static std::vector<CPPStructType *> last_structs;
  51. int yyparse();
  52. #define YYERROR_VERBOSE
  53. static void
  54. yyerror(const string &msg) {
  55. current_lexer->error(msg, current_lexer->_last_token_loc);
  56. }
  57. static void
  58. yyerror(YYLTYPE *loc, const string &msg) {
  59. current_lexer->error(msg, *loc);
  60. }
  61. static void
  62. yyerror(const string &msg, YYLTYPE &loc) {
  63. current_lexer->error(msg, loc);
  64. }
  65. static void
  66. yywarning(const string &msg, YYLTYPE &loc) {
  67. current_lexer->warning(msg, loc);
  68. }
  69. static int
  70. yylex(YYSTYPE *lval, YYLTYPE *lloc) {
  71. CPPToken token = current_lexer->get_next_token();
  72. *lval = token._lval;
  73. *lloc = token._lloc;
  74. return token._token;
  75. }
  76. void
  77. parse_cpp(CPPParser *cp) {
  78. CPPScope *old_scope = current_scope;
  79. CPPScope *old_global_scope = global_scope;
  80. CPPPreprocessor *old_lexer = current_lexer;
  81. current_scope = cp;
  82. global_scope = cp;
  83. current_lexer = cp;
  84. publish_nest_level = 0;
  85. yyparse();
  86. if (publish_nest_level != 0) {
  87. yyerror("Unclosed __begin_publish", publish_loc);
  88. publish_nest_level = 0;
  89. }
  90. current_scope = old_scope;
  91. global_scope = old_global_scope;
  92. current_lexer = old_lexer;
  93. }
  94. CPPExpression *
  95. parse_const_expr(CPPPreprocessor *pp, CPPScope *new_current_scope,
  96. CPPScope *new_global_scope) {
  97. CPPScope *old_scope = current_scope;
  98. CPPScope *old_global_scope = global_scope;
  99. CPPPreprocessor *old_lexer = current_lexer;
  100. CPPExpression *old_expr = current_expr;
  101. current_scope = new_current_scope;
  102. global_scope = new_global_scope;
  103. current_expr = nullptr;
  104. current_lexer = pp;
  105. yyparse();
  106. CPPExpression *result = current_expr;
  107. current_scope = old_scope;
  108. global_scope = old_global_scope;
  109. current_lexer = old_lexer;
  110. current_expr = old_expr;
  111. return result;
  112. }
  113. CPPType *
  114. parse_type(CPPPreprocessor *pp, CPPScope *new_current_scope,
  115. CPPScope *new_global_scope) {
  116. CPPScope *old_scope = current_scope;
  117. CPPScope *old_global_scope = global_scope;
  118. CPPPreprocessor *old_lexer = current_lexer;
  119. CPPType *old_type = current_type;
  120. current_scope = new_current_scope;
  121. global_scope = new_global_scope;
  122. current_type = nullptr;
  123. current_lexer = pp;
  124. yyparse();
  125. CPPType *result = current_type;
  126. current_scope = old_scope;
  127. global_scope = old_global_scope;
  128. current_lexer = old_lexer;
  129. current_type = old_type;
  130. return result;
  131. }
  132. static void
  133. push_scope(CPPScope *new_scope) {
  134. last_scopes.push_back(current_scope);
  135. if (new_scope != nullptr) {
  136. current_scope = new_scope;
  137. }
  138. }
  139. static void
  140. pop_scope() {
  141. assert(!last_scopes.empty());
  142. current_scope = last_scopes.back();
  143. last_scopes.pop_back();
  144. }
  145. static void
  146. push_storage_class(int new_storage_class) {
  147. last_storage_classes.push_back(current_storage_class);
  148. current_storage_class = new_storage_class;
  149. }
  150. static void
  151. pop_storage_class() {
  152. assert(!last_storage_classes.empty());
  153. current_storage_class = last_storage_classes.back();
  154. last_storage_classes.pop_back();
  155. }
  156. static void
  157. push_struct(CPPStructType *new_struct) {
  158. last_structs.push_back(current_struct);
  159. current_struct = new_struct;
  160. }
  161. static void
  162. pop_struct() {
  163. assert(!last_structs.empty());
  164. current_struct = last_structs.back();
  165. last_structs.pop_back();
  166. }
  167. %}
  168. /* This is a bison-specific declaration to enable recursive calls to
  169. yyparse(). It changes the calling sequence to yylex(), passing
  170. pointers to the current yylval and yylloc. Bison 2.7 introduces a
  171. different syntax that will also pass the current yylloc to yyerror,
  172. but we have to support Bison versions as old as 2.5 for now. */
  173. /*%define api.pure full*/
  174. %pure-parser
  175. %locations
  176. %token <u.real> REAL
  177. %token <u.integer> INTEGER
  178. %token <u.integer> CHAR_TOK
  179. %token <str> SIMPLE_STRING SIMPLE_IDENTIFIER
  180. %token <u.expr> STRING_LITERAL CUSTOM_LITERAL
  181. %token <u.identifier> IDENTIFIER TYPENAME_IDENTIFIER TYPEPACK_IDENTIFIER SCOPING
  182. %token <u.type> TYPEDEFNAME
  183. %token ELLIPSIS
  184. %token OROR
  185. %token ANDAND
  186. %token EQCOMPARE
  187. %token NECOMPARE
  188. %token LECOMPARE
  189. %token GECOMPARE
  190. %token LSHIFT
  191. %token RSHIFT
  192. %token POINTSAT_STAR
  193. %token DOT_STAR
  194. %token UNARY
  195. %token UNARY_NOT
  196. %token UNARY_NEGATE
  197. %token UNARY_MINUS
  198. %token UNARY_PLUS
  199. %token UNARY_STAR
  200. %token UNARY_REF
  201. %token POINTSAT
  202. %token SCOPE
  203. %token PLUSPLUS
  204. %token MINUSMINUS
  205. %token TIMESEQUAL
  206. %token DIVIDEEQUAL
  207. %token MODEQUAL
  208. %token PLUSEQUAL
  209. %token MINUSEQUAL
  210. %token OREQUAL
  211. %token ANDEQUAL
  212. %token XOREQUAL
  213. %token LSHIFTEQUAL
  214. %token RSHIFTEQUAL
  215. %token ATTR_LEFT
  216. %token ATTR_RIGHT
  217. %token KW_ALIGNAS
  218. %token KW_ALIGNOF
  219. %token KW_AUTO
  220. %token KW_BEGIN_PUBLISH
  221. %token KW_BLOCKING
  222. %token KW_BOOL
  223. %token KW_CATCH
  224. %token KW_CHAR
  225. %token KW_CHAR16_T
  226. %token KW_CHAR32_T
  227. %token KW_CLASS
  228. %token KW_CONST
  229. %token KW_CONSTEXPR
  230. %token KW_CONST_CAST
  231. %token KW_DECLTYPE
  232. %token KW_DEFAULT
  233. %token KW_DELETE
  234. %token KW_DOUBLE
  235. %token KW_DYNAMIC_CAST
  236. %token KW_ELSE
  237. %token KW_END_PUBLISH
  238. %token KW_ENUM
  239. %token KW_EXTENSION
  240. %token KW_EXTERN
  241. %token KW_EXPLICIT
  242. %token KW_PUBLISHED
  243. %token KW_FALSE
  244. %token KW_FINAL
  245. %token KW_FLOAT
  246. %token KW_FRIEND
  247. %token KW_FOR
  248. %token KW_GOTO
  249. %token KW_HAS_VIRTUAL_DESTRUCTOR
  250. %token KW_IF
  251. %token KW_INLINE
  252. %token KW_INT
  253. %token KW_IS_ABSTRACT
  254. %token KW_IS_BASE_OF
  255. %token KW_IS_CLASS
  256. %token KW_IS_CONSTRUCTIBLE
  257. %token KW_IS_CONVERTIBLE_TO
  258. %token KW_IS_DESTRUCTIBLE
  259. %token KW_IS_EMPTY
  260. %token KW_IS_ENUM
  261. %token KW_IS_FINAL
  262. %token KW_IS_FUNDAMENTAL
  263. %token KW_IS_POD
  264. %token KW_IS_POLYMORPHIC
  265. %token KW_IS_STANDARD_LAYOUT
  266. %token KW_IS_TRIVIAL
  267. %token KW_IS_UNION
  268. %token KW_LONG
  269. %token KW_MAKE_MAP_KEYS_SEQ
  270. %token KW_MAKE_MAP_PROPERTY
  271. %token KW_MAKE_PROPERTY
  272. %token KW_MAKE_PROPERTY2
  273. %token KW_MAKE_SEQ
  274. %token KW_MAKE_SEQ_PROPERTY
  275. %token KW_MUTABLE
  276. %token KW_NAMESPACE
  277. %token KW_NEW
  278. %token KW_NOEXCEPT
  279. %token KW_NULLPTR
  280. %token KW_OPERATOR
  281. %token KW_OVERRIDE
  282. %token KW_PRIVATE
  283. %token KW_PROTECTED
  284. %token KW_PUBLIC
  285. %token KW_REGISTER
  286. %token KW_REINTERPRET_CAST
  287. %token KW_RETURN
  288. %token KW_SHORT
  289. %token KW_SIGNED
  290. %token KW_SIZEOF
  291. %token KW_STATIC
  292. %token KW_STATIC_ASSERT
  293. %token KW_STATIC_CAST
  294. %token KW_STRUCT
  295. %token KW_TEMPLATE
  296. %token KW_THREAD_LOCAL
  297. %token KW_THROW
  298. %token KW_TRUE
  299. %token KW_TRY
  300. %token KW_TYPEDEF
  301. %token KW_TYPEID
  302. %token KW_TYPENAME
  303. %token KW_UNDERLYING_TYPE
  304. %token KW_UNION
  305. %token KW_UNSIGNED
  306. %token KW_USING
  307. %token KW_VIRTUAL
  308. %token KW_VOID
  309. %token KW_VOLATILE
  310. %token KW_WCHAR_T
  311. %token KW_WHILE
  312. /* These special tokens are used to set the starting state of the
  313. parser. The lexer places the appropriate one of these on the head
  314. of the input stream. */
  315. %token START_CPP
  316. %token START_CONST_EXPR
  317. %token START_TYPE
  318. %type <u.integer> storage_class
  319. %type <u.instance> constructor_prototype
  320. %type <u.instance> function_prototype
  321. %type <u.integer> function_post
  322. %type <str> function_operator
  323. %type <u.decl> template_formal_parameter
  324. %type <u.type> template_formal_parameter_type
  325. %type <u.inst_ident> instance_identifier
  326. %type <u.inst_ident> instance_identifier_and_maybe_trailing_return_type
  327. %type <u.param_list> function_parameter_list
  328. %type <u.param_list> function_parameters
  329. %type <u.param_list> formal_parameter_list
  330. %type <u.param_list> formal_parameters
  331. %type <u.closure_type> capture_list
  332. %type <u.capture> capture
  333. %type <u.expr> template_parameter_maybe_initialize
  334. %type <u.expr> maybe_initialize
  335. %type <u.expr> maybe_initialize_or_constructor_body
  336. %type <u.expr> maybe_initialize_or_function_body
  337. %type <u.instance> function_parameter
  338. %type <u.instance> formal_parameter
  339. %type <u.inst_ident> not_paren_formal_parameter_identifier
  340. %type <u.inst_ident> formal_parameter_identifier
  341. %type <u.inst_ident> parameter_pack_identifier
  342. %type <u.inst_ident> not_paren_empty_instance_identifier
  343. %type <u.inst_ident> empty_instance_identifier
  344. %type <u.type> type type_pack
  345. %type <u.decl> type_decl
  346. %type <u.decl> var_type_decl
  347. %type <u.type> predefined_type
  348. %type <u.type> full_type
  349. %type <u.struct_type> anonymous_struct
  350. %type <u.struct_type> named_struct
  351. %type <u.enum_type> enum
  352. %type <u.extension_enum> enum_keyword
  353. %type <u.extension_enum> struct_keyword
  354. %type <u.simple_type> simple_type
  355. %type <u.simple_type> simple_int_type
  356. %type <u.simple_type> simple_float_type
  357. %type <u.simple_type> simple_void_type
  358. %type <u.type> class_derivation_name
  359. %type <u.type> enum_element_type
  360. %type <u.type> maybe_trailing_return_type
  361. %type <u.identifier> maybe_comma_identifier
  362. /*%type <u.type> typedefname*/
  363. %type <u.identifier> name
  364. %type <u.identifier> name_no_final
  365. %type <u.expr> string_literal
  366. /* We need to treat KW_OPERATOR as a scopable keyword. */
  367. %type <u.identifier> KW_OPERATOR
  368. %type <u.expr> optional_const_expr
  369. %type <u.expr> optional_const_expr_comma
  370. %type <u.expr> const_expr_comma
  371. %type <u.expr> no_angle_bracket_const_expr
  372. %type <u.expr> const_expr
  373. %type <u.expr> const_operand
  374. %type <u.expr> formal_const_expr
  375. %type <u.expr> formal_const_operand
  376. /* Precedence rules. */
  377. %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_CHAR16_T KW_CHAR32_T KW_BOOL
  378. %left '{' ',' ';'
  379. %nonassoc KW_THROW
  380. %right ':'
  381. %right '='
  382. %right '?'
  383. %left OROR
  384. %left ANDAND
  385. %left '|'
  386. %left '^'
  387. %left '&'
  388. %left EQCOMPARE NECOMPARE
  389. %left LECOMPARE GECOMPARE '<' '>'
  390. %left LSHIFT RSHIFT
  391. %left '+' '-'
  392. %left '*' '/' '%'
  393. %left POINTSAT_STAR DOT_STAR
  394. %right UNARY PLUSPLUS MINUSMINUS '~'
  395. %left POINTSAT '.' '(' '['
  396. %right SCOPE
  397. %nonassoc KW_NEW KW_DELETE KW_TRY KW_CATCH
  398. %%
  399. grammar:
  400. START_CPP cpp
  401. | START_CONST_EXPR const_expr
  402. {
  403. current_expr = $2;
  404. }
  405. | START_TYPE full_type
  406. {
  407. current_type = $2;
  408. }
  409. ;
  410. cpp:
  411. empty
  412. | cpp ';'
  413. | cpp declaration
  414. ;
  415. constructor_inits:
  416. constructor_init
  417. | constructor_inits ',' constructor_init
  418. ;
  419. constructor_init:
  420. name '(' optional_const_expr_comma ')'
  421. {
  422. delete $3;
  423. }
  424. | name '(' optional_const_expr_comma ')' ELLIPSIS
  425. {
  426. delete $3;
  427. }
  428. | name '{' optional_const_expr_comma '}'
  429. {
  430. delete $3;
  431. }
  432. ;
  433. /* This is principally for the syntax: extern "C" { ... }.
  434. We use storage_class instead of simply KW_EXTERN to avoid
  435. shift/reduce conflicts with yacc's limited differentiation
  436. ability. */
  437. extern_c:
  438. storage_class '{'
  439. {
  440. push_storage_class((current_storage_class & ~CPPInstance::SC_c_binding) |
  441. ($1 & CPPInstance::SC_c_binding));
  442. }
  443. cpp '}'
  444. {
  445. pop_storage_class();
  446. }
  447. ;
  448. declaration:
  449. type_like_declaration
  450. | template_declaration
  451. | extern_c
  452. | namespace_declaration
  453. | friend_declaration
  454. | KW_TYPEDEF typedef_declaration
  455. | KW_BEGIN_PUBLISH
  456. {
  457. if (publish_nest_level != 0) {
  458. yyerror("Unclosed __begin_publish", publish_loc);
  459. publish_nest_level = 0;
  460. current_scope->set_current_vis(V_public);
  461. }
  462. publish_previous = current_scope->get_current_vis();
  463. publish_loc = @1;
  464. publish_nest_level++;
  465. current_scope->set_current_vis(V_published);
  466. }
  467. | KW_END_PUBLISH
  468. {
  469. if (publish_nest_level != 1) {
  470. yyerror("Unmatched __end_publish", @1);
  471. } else {
  472. current_scope->set_current_vis(publish_previous);
  473. }
  474. publish_nest_level = 0;
  475. }
  476. | KW_PUBLISHED ':'
  477. {
  478. current_scope->set_current_vis(V_published);
  479. }
  480. | KW_PUBLIC ':'
  481. {
  482. if (publish_nest_level > 0) {
  483. current_scope->set_current_vis(V_published);
  484. } else {
  485. current_scope->set_current_vis(V_public);
  486. }
  487. }
  488. | KW_PROTECTED ':'
  489. {
  490. current_scope->set_current_vis(V_protected);
  491. }
  492. | KW_PRIVATE ':'
  493. {
  494. current_scope->set_current_vis(V_private);
  495. }
  496. | KW_MAKE_PROPERTY '(' name ',' IDENTIFIER maybe_comma_identifier ')' ';'
  497. {
  498. CPPDeclaration *getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  499. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  500. yyerror("Reference to non-existent or invalid getter: " + $5->get_fully_scoped_name(), @5);
  501. } else {
  502. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_normal, current_scope, @1.file);
  503. make_property->_get_function = getter->as_function_group();
  504. if ($6 != nullptr) {
  505. CPPDeclaration *setter = $6->find_symbol(current_scope, global_scope, current_lexer);
  506. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  507. yyerror("Reference to non-existent or invalid setter: " + $6->get_fully_scoped_name(), @6);
  508. } else {
  509. make_property->_set_function = setter->as_function_group();
  510. }
  511. }
  512. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  513. }
  514. }
  515. | KW_MAKE_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  516. {
  517. CPPDeclaration *getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  518. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  519. yyerror("Reference to non-existent or invalid getter: " + $5->get_fully_scoped_name(), @5);
  520. } else {
  521. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_normal, current_scope, @1.file);
  522. make_property->_get_function = getter->as_function_group();
  523. CPPDeclaration *setter = $7->find_symbol(current_scope, global_scope, current_lexer);
  524. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  525. yyerror("Reference to non-existent or invalid setter: " + $7->get_fully_scoped_name(), @7);
  526. } else {
  527. make_property->_set_function = setter->as_function_group();
  528. }
  529. CPPDeclaration *deleter = $9->find_symbol(current_scope, global_scope, current_lexer);
  530. if (deleter == nullptr || deleter->get_subtype() != CPPDeclaration::ST_function_group) {
  531. yyerror("reference to non-existent or invalid delete method: " + $9->get_fully_scoped_name(), @9);
  532. } else {
  533. make_property->_del_function = deleter->as_function_group();
  534. }
  535. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  536. }
  537. }
  538. | KW_MAKE_SEQ_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  539. {
  540. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  541. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  542. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  543. length_getter = nullptr;
  544. }
  545. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  546. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  547. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  548. getter = nullptr;
  549. }
  550. if (getter != nullptr && length_getter != nullptr) {
  551. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_sequence, current_scope, @1.file);
  552. make_property->_get_function = getter->as_function_group();
  553. make_property->_length_function = length_getter->as_function_group();
  554. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  555. }
  556. }
  557. | KW_MAKE_SEQ_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  558. {
  559. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  560. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  561. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  562. length_getter = nullptr;
  563. }
  564. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  565. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  566. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  567. getter = nullptr;
  568. }
  569. if (getter != nullptr && length_getter != nullptr) {
  570. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_sequence, current_scope, @1.file);
  571. make_property->_get_function = getter->as_function_group();
  572. make_property->_length_function = length_getter->as_function_group();
  573. CPPDeclaration *setter = $9->find_symbol(current_scope, global_scope, current_lexer);
  574. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  575. yyerror("Reference to non-existent or invalid setter: " + $9->get_fully_scoped_name(), @9);
  576. } else {
  577. make_property->_set_function = setter->as_function_group();
  578. }
  579. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  580. }
  581. }
  582. | KW_MAKE_SEQ_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  583. {
  584. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  585. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  586. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  587. length_getter = nullptr;
  588. }
  589. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  590. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  591. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  592. getter = nullptr;
  593. }
  594. if (getter != nullptr && length_getter != nullptr) {
  595. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_sequence, current_scope, @1.file);
  596. make_property->_get_function = getter->as_function_group();
  597. make_property->_length_function = length_getter->as_function_group();
  598. CPPDeclaration *setter = $9->find_symbol(current_scope, global_scope, current_lexer);
  599. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  600. yyerror("Reference to non-existent or invalid setter: " + $9->get_fully_scoped_name(), @9);
  601. } else {
  602. make_property->_set_function = setter->as_function_group();
  603. }
  604. CPPDeclaration *deleter = $11->find_symbol(current_scope, global_scope, current_lexer);
  605. if (deleter == nullptr || deleter->get_subtype() != CPPDeclaration::ST_function_group) {
  606. yyerror("reference to non-existent or invalid delete method: " + $11->get_fully_scoped_name(), @11);
  607. } else {
  608. make_property->_del_function = deleter->as_function_group();
  609. }
  610. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  611. }
  612. }
  613. | KW_MAKE_SEQ_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  614. {
  615. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  616. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  617. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  618. length_getter = nullptr;
  619. }
  620. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  621. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  622. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  623. getter = nullptr;
  624. }
  625. if (getter != nullptr && length_getter != nullptr) {
  626. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_sequence, current_scope, @1.file);
  627. make_property->_get_function = getter->as_function_group();
  628. make_property->_length_function = length_getter->as_function_group();
  629. CPPDeclaration *setter = $9->find_symbol(current_scope, global_scope, current_lexer);
  630. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  631. yyerror("Reference to non-existent or invalid setter: " + $9->get_fully_scoped_name(), @9);
  632. } else {
  633. make_property->_set_function = setter->as_function_group();
  634. }
  635. CPPDeclaration *deleter = $11->find_symbol(current_scope, global_scope, current_lexer);
  636. if (deleter == nullptr || deleter->get_subtype() != CPPDeclaration::ST_function_group) {
  637. yyerror("reference to non-existent or invalid delete method: " + $11->get_fully_scoped_name(), @11);
  638. } else {
  639. make_property->_del_function = deleter->as_function_group();
  640. }
  641. CPPDeclaration *inserter = $13->find_symbol(current_scope, global_scope, current_lexer);
  642. if (inserter == nullptr || inserter->get_subtype() != CPPDeclaration::ST_function_group) {
  643. yyerror("reference to non-existent or invalid append method: " + $13->get_fully_scoped_name(), @13);
  644. } else {
  645. make_property->_insert_function = inserter->as_function_group();
  646. }
  647. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  648. }
  649. }
  650. | KW_MAKE_MAP_PROPERTY '(' name ',' IDENTIFIER ')' ';'
  651. {
  652. CPPDeclaration *getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  653. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  654. yyerror("reference to non-existent or invalid item getter method: " + $5->get_fully_scoped_name(), @5);
  655. } else {
  656. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_mapping, current_scope, @1.file);
  657. make_property->_get_function = getter->as_function_group();
  658. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  659. }
  660. }
  661. | KW_MAKE_MAP_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  662. {
  663. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  664. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  665. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  666. } else {
  667. CPPMakeProperty *make_property;
  668. make_property = new CPPMakeProperty($3, CPPMakeProperty::T_mapping, current_scope, @1.file);
  669. make_property->_get_function = getter->as_function_group();
  670. CPPDeclaration *hasser = $5->find_symbol(current_scope, global_scope, current_lexer);
  671. if (hasser == nullptr || hasser->get_subtype() != CPPDeclaration::ST_function_group) {
  672. yyerror("reference to non-existent or invalid has/find method: " + $5->get_fully_scoped_name(), @5);
  673. } else {
  674. make_property->_has_function = hasser->as_function_group();
  675. }
  676. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  677. }
  678. }
  679. | KW_MAKE_MAP_PROPERTY '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER maybe_comma_identifier ')' ';'
  680. {
  681. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  682. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  683. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  684. } else {
  685. CPPMakeProperty *make_property = new CPPMakeProperty($3, CPPMakeProperty::T_mapping, current_scope, @1.file);
  686. make_property->_get_function = getter->as_function_group();
  687. CPPDeclaration *hasser = $5->find_symbol(current_scope, global_scope, current_lexer);
  688. if (hasser == nullptr || hasser->get_subtype() != CPPDeclaration::ST_function_group) {
  689. yyerror("reference to non-existent or invalid has/find method: " + $5->get_fully_scoped_name(), @5);
  690. } else {
  691. make_property->_has_function = hasser->as_function_group();
  692. }
  693. CPPDeclaration *setter = $9->find_symbol(current_scope, global_scope, current_lexer);
  694. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  695. yyerror("Reference to non-existent or invalid setter: " + $9->get_fully_scoped_name(), @9);
  696. } else {
  697. make_property->_set_function = setter->as_function_group();
  698. }
  699. if ($10 != nullptr) {
  700. CPPDeclaration *deleter = $10->find_symbol(current_scope, global_scope, current_lexer);
  701. if (deleter == nullptr || deleter->get_subtype() != CPPDeclaration::ST_function_group) {
  702. yyerror("reference to non-existent or invalid delete method: " + $10->get_fully_scoped_name(), @10);
  703. } else {
  704. make_property->_del_function = deleter->as_function_group();
  705. }
  706. }
  707. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  708. }
  709. }
  710. | KW_MAKE_MAP_KEYS_SEQ '(' name ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  711. {
  712. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  713. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  714. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  715. length_getter = nullptr;
  716. }
  717. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  718. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  719. yyerror("reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  720. getter = nullptr;
  721. }
  722. if (getter != nullptr && length_getter != nullptr) {
  723. CPPMakeProperty *make_property = nullptr;
  724. for (size_t i = 0; i < current_scope->_declarations.size(); ++i) {
  725. make_property = current_scope->_declarations[i]->as_make_property();
  726. if (make_property != nullptr) {
  727. if (make_property->get_fully_scoped_name() == $3->get_fully_scoped_name()) {
  728. break;
  729. } else {
  730. make_property = nullptr;
  731. }
  732. }
  733. }
  734. if (make_property != nullptr) {
  735. make_property->_get_key_function = getter->as_function_group();
  736. make_property->_length_function = length_getter->as_function_group();
  737. } else {
  738. yyerror("reference to non-existent MAKE_MAP_PROPERTY: " + $3->get_fully_scoped_name(), @3);
  739. }
  740. }
  741. }
  742. | KW_MAKE_PROPERTY2 '(' name ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  743. {
  744. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  745. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  746. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  747. } else {
  748. CPPMakeProperty *make_property;
  749. make_property = new CPPMakeProperty($3, CPPMakeProperty::T_normal,
  750. current_scope, @1.file);
  751. make_property->_get_function = getter->as_function_group();
  752. CPPDeclaration *hasser = $5->find_symbol(current_scope, global_scope, current_lexer);
  753. if (hasser == nullptr || hasser->get_subtype() != CPPDeclaration::ST_function_group) {
  754. yyerror("reference to non-existent or invalid has/find method: " + $5->get_fully_scoped_name(), @5);
  755. } else {
  756. make_property->_has_function = hasser->as_function_group();
  757. }
  758. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  759. }
  760. }
  761. | KW_MAKE_PROPERTY2 '(' name ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  762. {
  763. CPPDeclaration *getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  764. if (getter == nullptr || getter->get_subtype() != CPPDeclaration::ST_function_group) {
  765. yyerror("Reference to non-existent or invalid getter: " + $7->get_fully_scoped_name(), @7);
  766. } else {
  767. CPPMakeProperty *make_property;
  768. make_property = new CPPMakeProperty($3, CPPMakeProperty::T_normal,
  769. current_scope, @1.file);
  770. make_property->_get_function = getter->as_function_group();
  771. CPPDeclaration *hasser = $5->find_symbol(current_scope, global_scope, current_lexer);
  772. if (hasser == nullptr || hasser->get_subtype() != CPPDeclaration::ST_function_group) {
  773. yyerror("reference to non-existent or invalid has/find method: " + $5->get_fully_scoped_name(), @5);
  774. } else {
  775. make_property->_has_function = hasser->as_function_group();
  776. }
  777. CPPDeclaration *setter = $9->find_symbol(current_scope, global_scope, current_lexer);
  778. if (setter == nullptr || setter->get_subtype() != CPPDeclaration::ST_function_group) {
  779. yyerror("reference to non-existent or invalid setter: " + $9->get_fully_scoped_name(), @9);
  780. } else {
  781. make_property->_set_function = setter->as_function_group();
  782. }
  783. CPPDeclaration *clearer = $11->find_symbol(current_scope, global_scope, current_lexer);
  784. if (clearer == nullptr || clearer->get_subtype() != CPPDeclaration::ST_function_group) {
  785. yyerror("reference to non-existent or invalid clear method: " + $11->get_fully_scoped_name(), @11);
  786. } else {
  787. make_property->_clear_function = clearer->as_function_group();
  788. }
  789. current_scope->add_declaration(make_property, global_scope, current_lexer, @1);
  790. }
  791. }
  792. | KW_MAKE_SEQ '(' name ',' IDENTIFIER ',' IDENTIFIER ')' ';'
  793. {
  794. CPPDeclaration *length_getter = $5->find_symbol(current_scope, global_scope, current_lexer);
  795. if (length_getter == nullptr || length_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  796. yyerror("reference to non-existent or invalid length method: " + $5->get_fully_scoped_name(), @5);
  797. length_getter = nullptr;
  798. }
  799. CPPDeclaration *element_getter = $7->find_symbol(current_scope, global_scope, current_lexer);
  800. if (element_getter == nullptr || element_getter->get_subtype() != CPPDeclaration::ST_function_group) {
  801. yyerror("reference to non-existent or invalid element method: " + $7->get_fully_scoped_name(), @5);
  802. element_getter = nullptr;
  803. }
  804. if (length_getter != nullptr && element_getter != nullptr) {
  805. CPPMakeSeq *make_seq = new CPPMakeSeq($3,
  806. length_getter->as_function_group(),
  807. element_getter->as_function_group(),
  808. current_scope, @1.file);
  809. current_scope->add_declaration(make_seq, global_scope, current_lexer, @1);
  810. }
  811. }
  812. | KW_STATIC_ASSERT '(' const_expr ',' string_literal ')' ';'
  813. {
  814. CPPExpression::Result result = $3->evaluate();
  815. if (result._type == CPPExpression::RT_error) {
  816. yywarning("static_assert requires a constant expression", @3);
  817. } else if (!result.as_boolean()) {
  818. stringstream str;
  819. str << *$5;
  820. yywarning("static_assert failed: " + str.str(), @3);
  821. }
  822. }
  823. | KW_STATIC_ASSERT '(' const_expr ')' ';'
  824. {
  825. // This alternative version of static_assert was introduced in C++17.
  826. CPPExpression::Result result = $3->evaluate();
  827. if (result._type == CPPExpression::RT_error) {
  828. yywarning("static_assert requires a constant expression", @3);
  829. } else if (!result.as_boolean()) {
  830. yywarning("static_assert failed", @3);
  831. }
  832. }
  833. ;
  834. friend_declaration:
  835. KW_FRIEND
  836. {
  837. CPPScope *new_scope = new CPPScope(current_scope, CPPNameComponent("temp"),
  838. V_public);
  839. push_scope(new_scope);
  840. }
  841. declaration
  842. {
  843. delete current_scope;
  844. pop_scope();
  845. }
  846. ;
  847. storage_class:
  848. empty
  849. {
  850. $$ = 0;
  851. }
  852. | KW_CONST storage_class
  853. {
  854. // This isn't really a storage class, but it helps with parsing.
  855. $$ = $2 | (int)CPPInstance::SC_const;
  856. }
  857. | KW_EXTERN storage_class
  858. {
  859. $$ = $2 | (int)CPPInstance::SC_extern;
  860. }
  861. | KW_EXTERN SIMPLE_STRING storage_class
  862. {
  863. $$ = $3 | (int)CPPInstance::SC_extern;
  864. if ($2 == "C") {
  865. $$ |= (int)CPPInstance::SC_c_binding;
  866. } else if ($2 == "C++") {
  867. $$ &= ~(int)CPPInstance::SC_c_binding;
  868. } else {
  869. yywarning("Ignoring unknown linkage type \"" + $2 + "\"", @2);
  870. }
  871. }
  872. | KW_STATIC storage_class
  873. {
  874. $$ = $2 | (int)CPPInstance::SC_static;
  875. }
  876. | KW_INLINE storage_class
  877. {
  878. $$ = $2 | (int)CPPInstance::SC_inline;
  879. }
  880. | KW_VIRTUAL storage_class
  881. {
  882. $$ = $2 | (int)CPPInstance::SC_virtual;
  883. }
  884. | KW_EXPLICIT storage_class
  885. {
  886. $$ = $2 | (int)CPPInstance::SC_explicit;
  887. }
  888. | KW_REGISTER storage_class
  889. {
  890. $$ = $2 | (int)CPPInstance::SC_register;
  891. }
  892. | KW_VOLATILE storage_class
  893. {
  894. $$ = $2 | (int)CPPInstance::SC_volatile;
  895. }
  896. | KW_MUTABLE storage_class
  897. {
  898. $$ = $2 | (int)CPPInstance::SC_mutable;
  899. }
  900. | KW_CONSTEXPR storage_class
  901. {
  902. $$ = $2 | (int)CPPInstance::SC_constexpr;
  903. }
  904. | KW_BLOCKING storage_class
  905. {
  906. $$ = $2 | (int)CPPInstance::SC_blocking;
  907. }
  908. | KW_EXTENSION storage_class
  909. {
  910. $$ = $2 | (int)CPPInstance::SC_extension;
  911. }
  912. | KW_THREAD_LOCAL storage_class
  913. {
  914. $$ = $2 | (int)CPPInstance::SC_thread_local;
  915. }
  916. | ATTR_LEFT attribute_specifiers ATTR_RIGHT storage_class
  917. {
  918. // Ignore attribute specifiers for now.
  919. $$ = $4;
  920. }
  921. | KW_ALIGNAS '(' const_expr ')' storage_class
  922. {
  923. $$ = $5;
  924. }
  925. | KW_ALIGNAS '(' type_decl ')' storage_class
  926. {
  927. $$ = $5;
  928. }
  929. ;
  930. attribute_specifiers:
  931. attribute_specifier
  932. | attribute_specifier ',' attribute_specifiers
  933. ;
  934. attribute_specifier:
  935. name
  936. | name '(' formal_parameter_list ')'
  937. | KW_USING name ':' attribute_specifier
  938. ;
  939. type_like_declaration:
  940. storage_class var_type_decl
  941. {
  942. // We don't need to push/pop type, because we can't nest
  943. // type_like_declaration.
  944. if ($2->as_type_declaration()) {
  945. current_type = $2->as_type_declaration()->_type;
  946. } else {
  947. current_type = $2->as_type();
  948. }
  949. push_storage_class($1);
  950. }
  951. multiple_instance_identifiers
  952. {
  953. pop_storage_class();
  954. }
  955. | storage_class type_decl ';'
  956. {
  957. // We don't really care about the storage class here. In fact, it's
  958. // not actually legal to define a class or struct using a particular
  959. // storage class, but we require it just to help yacc out in its
  960. // parsing.
  961. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  962. }
  963. | storage_class constructor_prototype
  964. {
  965. if ($2 != nullptr) {
  966. // Push the scope so that the initializers can make use of things defined
  967. // in the class body.
  968. push_scope($2->get_scope(current_scope, global_scope));
  969. $2->_storage_class |= (current_storage_class | $1);
  970. }
  971. }
  972. maybe_initialize_or_constructor_body
  973. {
  974. if ($2 != nullptr) {
  975. pop_scope();
  976. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  977. $2->set_initializer($4);
  978. }
  979. }
  980. | storage_class function_prototype maybe_initialize_or_function_body
  981. {
  982. if ($2 != nullptr) {
  983. $2->_storage_class |= (current_storage_class | $1);
  984. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  985. $2->set_initializer($3);
  986. }
  987. }
  988. | using_declaration
  989. /* We don't need to include a rule for variables that point to
  990. functions, because we get those from the function_prototype
  991. definition. */
  992. ;
  993. multiple_instance_identifiers:
  994. instance_identifier_and_maybe_trailing_return_type maybe_initialize_or_function_body
  995. {
  996. if (current_storage_class & CPPInstance::SC_const) {
  997. $1->add_modifier(IIT_const);
  998. }
  999. CPPInstance *inst = new CPPInstance(current_type, $1,
  1000. current_storage_class,
  1001. @1.file);
  1002. inst->set_initializer($2);
  1003. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  1004. }
  1005. | instance_identifier_and_maybe_trailing_return_type maybe_initialize ',' multiple_instance_identifiers
  1006. {
  1007. if (current_storage_class & CPPInstance::SC_const) {
  1008. $1->add_modifier(IIT_const);
  1009. }
  1010. CPPInstance *inst = new CPPInstance(current_type, $1,
  1011. current_storage_class,
  1012. @1.file);
  1013. inst->set_initializer($2);
  1014. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  1015. }
  1016. ;
  1017. typedef_declaration:
  1018. storage_class var_type_decl
  1019. {
  1020. // We don't need to push/pop type, because we can't nest
  1021. // multiple_var_declarations.
  1022. if ($2->as_type_declaration()) {
  1023. current_type = $2->as_type_declaration()->_type;
  1024. } else {
  1025. current_type = $2->as_type();
  1026. }
  1027. push_storage_class($1);
  1028. }
  1029. typedef_instance_identifiers
  1030. {
  1031. pop_storage_class();
  1032. }
  1033. | storage_class function_prototype maybe_initialize_or_function_body
  1034. {
  1035. if ($2 != nullptr) {
  1036. CPPInstance *inst = $2->as_instance();
  1037. if (inst != nullptr) {
  1038. inst->_storage_class |= (current_storage_class | $1);
  1039. current_scope->add_declaration(inst, global_scope, current_lexer, @2);
  1040. CPPTypedefType *typedef_type = new CPPTypedefType(inst->_type, inst->_ident, current_scope);
  1041. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @2);
  1042. }
  1043. }
  1044. }
  1045. ;
  1046. typedef_instance_identifiers:
  1047. instance_identifier_and_maybe_trailing_return_type maybe_initialize_or_function_body
  1048. {
  1049. if (current_storage_class & CPPInstance::SC_const) {
  1050. $1->add_modifier(IIT_const);
  1051. }
  1052. CPPType *target_type = current_type;
  1053. CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
  1054. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  1055. }
  1056. | instance_identifier_and_maybe_trailing_return_type maybe_initialize ',' typedef_instance_identifiers
  1057. {
  1058. if (current_storage_class & CPPInstance::SC_const) {
  1059. $1->add_modifier(IIT_const);
  1060. }
  1061. CPPType *target_type = current_type;
  1062. CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
  1063. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  1064. }
  1065. ;
  1066. constructor_prototype:
  1067. /* Functions with implicit return types, and constructors */
  1068. IDENTIFIER '('
  1069. {
  1070. // Create a scope for this function.
  1071. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1072. $1->_names.back(), V_private);
  1073. // It still needs to be able to pick up any template arguments, if this is
  1074. // a definition for a method template. Add a fake "using" declaration to
  1075. // accomplish this.
  1076. scope->_using.insert(current_scope);
  1077. push_scope(scope);
  1078. }
  1079. function_parameter_list ')' function_post
  1080. {
  1081. CPPScope *scope = $1->get_scope(current_scope, global_scope);
  1082. CPPType *type;
  1083. std::string simple_name = $1->get_simple_name();
  1084. if (!simple_name.empty() && simple_name[0] == '~') {
  1085. // A destructor has no return type.
  1086. type = new CPPSimpleType(CPPSimpleType::T_void);
  1087. } else if (scope != nullptr && simple_name == scope->get_simple_name()) {
  1088. // Neither does a constructor.
  1089. type = new CPPSimpleType(CPPSimpleType::T_void);
  1090. } else {
  1091. // This isn't a constructor, so it has an implicit return type of
  1092. // int.
  1093. yywarning("function has no return type, assuming int", @1);
  1094. type = new CPPSimpleType(CPPSimpleType::T_int);
  1095. }
  1096. pop_scope();
  1097. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  1098. ii->add_func_modifier($4, $6);
  1099. $$ = new CPPInstance(type, ii, 0, @1.file);
  1100. }
  1101. | TYPENAME_IDENTIFIER '('
  1102. {
  1103. // Create a scope for this function.
  1104. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1105. $1->_names.back(), V_private);
  1106. // It still needs to be able to pick up any template arguments, if this is
  1107. // a definition for a method template. Add a fake "using" declaration to
  1108. // accomplish this.
  1109. scope->_using.insert(current_scope);
  1110. push_scope(scope);
  1111. }
  1112. function_parameter_list ')' function_post
  1113. {
  1114. pop_scope();
  1115. CPPType *type;
  1116. if ($1->get_simple_name() == current_scope->get_simple_name()) {
  1117. // This is a constructor, and has no return.
  1118. type = new CPPSimpleType(CPPSimpleType::T_void);
  1119. } else {
  1120. // This isn't a constructor, so it has an implicit return type of
  1121. // int.
  1122. type = new CPPSimpleType(CPPSimpleType::T_int);
  1123. }
  1124. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  1125. ii->add_func_modifier($4, $6);
  1126. $$ = new CPPInstance(type, ii, 0, @1.file);
  1127. }
  1128. ;
  1129. function_prototype:
  1130. /* Destructors */
  1131. '~' name '('
  1132. {
  1133. push_scope($2->get_scope(current_scope, global_scope));
  1134. }
  1135. function_parameter_list ')' function_post
  1136. {
  1137. pop_scope();
  1138. if ($2->is_scoped()) {
  1139. yyerror("Invalid destructor name: ~" + $2->get_fully_scoped_name(), @2);
  1140. } else {
  1141. CPPIdentifier *ident =
  1142. new CPPIdentifier("~" + $2->get_simple_name(), @2);
  1143. delete $2;
  1144. CPPType *type;
  1145. type = new CPPSimpleType(CPPSimpleType::T_void);
  1146. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier(ident);
  1147. ii->add_func_modifier($5, $7);
  1148. $$ = new CPPInstance(type, ii, 0, @2.file);
  1149. }
  1150. }
  1151. /* This is a special case: a function pointer declaration that looks
  1152. at first a lot like a constructor declaration. This is provided to
  1153. help yacc sort out the differences. It isn't an ideal solution,
  1154. because it doesn't catch a lot of subtle variants on this form--but
  1155. this will get at least the 99% most common uses. */
  1156. | TYPENAME_IDENTIFIER '(' '*' instance_identifier ')' '('
  1157. {
  1158. push_scope($4->get_scope(current_scope, global_scope));
  1159. }
  1160. function_parameter_list ')' function_post maybe_trailing_return_type
  1161. {
  1162. pop_scope();
  1163. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  1164. if (type == nullptr) {
  1165. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1166. }
  1167. assert(type != nullptr);
  1168. CPPInstanceIdentifier *ii = $4;
  1169. ii->add_modifier(IIT_pointer);
  1170. ii->add_func_modifier($8, $10);
  1171. $$ = new CPPInstance(type, ii, 0, @1.file);
  1172. }
  1173. | TYPENAME_IDENTIFIER '(' SCOPING '*' instance_identifier ')' '('
  1174. {
  1175. push_scope($5->get_scope(current_scope, global_scope));
  1176. }
  1177. function_parameter_list ')' function_post maybe_trailing_return_type
  1178. {
  1179. pop_scope();
  1180. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  1181. if (type == nullptr) {
  1182. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1183. }
  1184. assert(type != nullptr);
  1185. CPPInstanceIdentifier *ii = $5;
  1186. ii->add_scoped_pointer_modifier($3);
  1187. ii->add_func_modifier($9, $11);
  1188. $$ = new CPPInstance(type, ii, 0, @1.file);
  1189. }
  1190. /* Typecast operators */
  1191. | KW_OPERATOR type not_paren_formal_parameter_identifier '('
  1192. {
  1193. if ($1 != nullptr) {
  1194. push_scope($1->get_scope(current_scope, global_scope));
  1195. }
  1196. }
  1197. function_parameter_list ')' function_post
  1198. {
  1199. if ($1 != nullptr) {
  1200. pop_scope();
  1201. }
  1202. // We use formal_parameter_identifier, because that can match a type
  1203. // name with or without an identifier, but especially without, which
  1204. // is what follows the keyword "operator" in a typecast function.
  1205. // As an added bonus, the type of the formal_parameter will be the
  1206. // typecast type, i.e. the return type of the typecast function.
  1207. // We give typecast operators the name "operator typecast <name>",
  1208. // where <name> is a simple name of the type to be typecast. Use
  1209. // the method's return type to determine the full type description.
  1210. string name = "operator typecast " + $2->get_simple_name();
  1211. CPPIdentifier *ident = $1;
  1212. if (ident == nullptr) {
  1213. ident = new CPPIdentifier(name, @2);
  1214. } else {
  1215. ident->add_name(name);
  1216. }
  1217. $$ = CPPInstance::make_typecast_function
  1218. (new CPPInstance($2, $3, 0, @3.file), ident, $6, $8);
  1219. }
  1220. | KW_OPERATOR KW_CONST type not_paren_formal_parameter_identifier '('
  1221. {
  1222. if ($1 != nullptr) {
  1223. push_scope($1->get_scope(current_scope, global_scope));
  1224. }
  1225. }
  1226. function_parameter_list ')' function_post
  1227. {
  1228. if ($1 != nullptr) {
  1229. pop_scope();
  1230. }
  1231. CPPIdentifier *ident = $1;
  1232. if (ident == nullptr) {
  1233. ident = new CPPIdentifier("operator typecast", @4);
  1234. } else {
  1235. ident->add_name("operator typecast");
  1236. }
  1237. $4->add_modifier(IIT_const);
  1238. $$ = CPPInstance::make_typecast_function
  1239. (new CPPInstance($3, $4, 0, @4.file), ident, $7, $9);
  1240. }
  1241. /* Not actually a function prototype, but maybe a template
  1242. instantiation. Just included here (instead of somewhere else) to
  1243. avoid shift/reduce conflicts. */
  1244. | IDENTIFIER
  1245. {
  1246. CPPDeclaration *decl =
  1247. $1->find_symbol(current_scope, global_scope, current_lexer);
  1248. if (decl != nullptr) {
  1249. $$ = decl->as_instance();
  1250. } else {
  1251. $$ = nullptr;
  1252. }
  1253. }
  1254. ;
  1255. function_post:
  1256. empty
  1257. {
  1258. $$ = 0;
  1259. }
  1260. | function_post KW_CONST
  1261. {
  1262. $$ = $1 | (int)CPPFunctionType::F_const_method;
  1263. }
  1264. | function_post KW_VOLATILE
  1265. {
  1266. $$ = $1 | (int)CPPFunctionType::F_volatile_method;
  1267. }
  1268. | function_post KW_NOEXCEPT
  1269. {
  1270. $$ = $1 | (int)CPPFunctionType::F_noexcept;
  1271. }
  1272. /* | function_post KW_NOEXCEPT '(' const_expr ')'
  1273. {
  1274. CPPExpression::Result result = $4->evaluate();
  1275. if (result._type == CPPExpression::RT_error) {
  1276. yywarning("noexcept requires a constant expression", @4);
  1277. } else if (result.as_boolean()) {
  1278. $$ = $1 | (int)CPPFunctionType::F_noexcept;
  1279. }
  1280. }*/
  1281. | function_post KW_FINAL
  1282. {
  1283. $$ = $1 | (int)CPPFunctionType::F_final;
  1284. }
  1285. | function_post KW_OVERRIDE
  1286. {
  1287. $$ = $1 | (int)CPPFunctionType::F_override;
  1288. }
  1289. | function_post '&'
  1290. {
  1291. $$ = $1 | (int)CPPFunctionType::F_lvalue_method;
  1292. }
  1293. | function_post ANDAND
  1294. {
  1295. $$ = $1 | (int)CPPFunctionType::F_rvalue_method;
  1296. }
  1297. | function_post KW_MUTABLE
  1298. {
  1299. // Used for lambdas, currently ignored.
  1300. $$ = $1;
  1301. }
  1302. | function_post KW_CONSTEXPR
  1303. {
  1304. // Used for lambdas in C++17, currently ignored.
  1305. $$ = $1;
  1306. }
  1307. | function_post KW_THROW '(' ')'
  1308. {
  1309. $$ = $1;
  1310. }
  1311. | function_post KW_THROW '(' name ')'
  1312. {
  1313. $$ = $1;
  1314. }
  1315. | function_post KW_THROW '(' name ELLIPSIS ')'
  1316. {
  1317. $$ = $1;
  1318. }
  1319. | function_post ATTR_LEFT attribute_specifiers ATTR_RIGHT
  1320. {
  1321. $$ = $1;
  1322. }
  1323. ;
  1324. function_operator:
  1325. '!'
  1326. {
  1327. $$ = "!";
  1328. }
  1329. | '~'
  1330. {
  1331. $$ = "~";
  1332. }
  1333. | '*'
  1334. {
  1335. $$ = "*";
  1336. }
  1337. | '/'
  1338. {
  1339. $$ = "/";
  1340. }
  1341. | '%'
  1342. {
  1343. $$ = "%";
  1344. }
  1345. | '+'
  1346. {
  1347. $$ = "+";
  1348. }
  1349. | '-'
  1350. {
  1351. $$ = "-";
  1352. }
  1353. | '|'
  1354. {
  1355. $$ = "|";
  1356. }
  1357. | '&'
  1358. {
  1359. $$ = "&";
  1360. }
  1361. | '^'
  1362. {
  1363. $$ = "^";
  1364. }
  1365. | OROR
  1366. {
  1367. $$ = "||";
  1368. }
  1369. | ANDAND
  1370. {
  1371. $$ = "&&";
  1372. }
  1373. | EQCOMPARE
  1374. {
  1375. $$ = "==";
  1376. }
  1377. | NECOMPARE
  1378. {
  1379. $$ = "!=";
  1380. }
  1381. | LECOMPARE
  1382. {
  1383. $$ = "<=";
  1384. }
  1385. | GECOMPARE
  1386. {
  1387. $$ = ">=";
  1388. }
  1389. | '<'
  1390. {
  1391. $$ = "<";
  1392. }
  1393. | '>'
  1394. {
  1395. $$ = ">";
  1396. }
  1397. | LSHIFT
  1398. {
  1399. $$ = "<<";
  1400. }
  1401. | RSHIFT
  1402. {
  1403. $$ = ">>";
  1404. }
  1405. | '='
  1406. {
  1407. $$ = "=";
  1408. }
  1409. | ','
  1410. {
  1411. $$ = ",";
  1412. }
  1413. | PLUSPLUS
  1414. {
  1415. $$ = "++";
  1416. }
  1417. | MINUSMINUS
  1418. {
  1419. $$ = "--";
  1420. }
  1421. | TIMESEQUAL
  1422. {
  1423. $$ = "*=";
  1424. }
  1425. | DIVIDEEQUAL
  1426. {
  1427. $$ = "/=";
  1428. }
  1429. | MODEQUAL
  1430. {
  1431. $$ = "%=";
  1432. }
  1433. | PLUSEQUAL
  1434. {
  1435. $$ = "+=";
  1436. }
  1437. | MINUSEQUAL
  1438. {
  1439. $$ = "-=";
  1440. }
  1441. | OREQUAL
  1442. {
  1443. $$ = "|=";
  1444. }
  1445. | ANDEQUAL
  1446. {
  1447. $$ = "&=";
  1448. }
  1449. | XOREQUAL
  1450. {
  1451. $$ = "^=";
  1452. }
  1453. | LSHIFTEQUAL
  1454. {
  1455. $$ = "<<=";
  1456. }
  1457. | RSHIFTEQUAL
  1458. {
  1459. $$ = ">>=";
  1460. }
  1461. | POINTSAT
  1462. {
  1463. $$ = "->";
  1464. }
  1465. | '[' ']'
  1466. {
  1467. $$ = "[]";
  1468. }
  1469. | '(' ')'
  1470. {
  1471. $$ = "()";
  1472. }
  1473. | KW_NEW
  1474. {
  1475. $$ = "new";
  1476. }
  1477. | KW_DELETE
  1478. {
  1479. $$ = "delete";
  1480. }
  1481. ;
  1482. more_template_declaration:
  1483. type_like_declaration
  1484. | template_declaration
  1485. | friend_declaration
  1486. ;
  1487. template_declaration:
  1488. KW_EXTERN template_declaration
  1489. | KW_TEMPLATE
  1490. {
  1491. push_scope(new CPPTemplateScope(current_scope));
  1492. }
  1493. '<' template_formal_parameters '>' more_template_declaration
  1494. {
  1495. pop_scope();
  1496. }
  1497. | KW_TEMPLATE type_like_declaration
  1498. | KW_TEMPLATE friend_declaration
  1499. ;
  1500. template_formal_parameters:
  1501. empty
  1502. | template_nonempty_formal_parameters
  1503. ;
  1504. template_nonempty_formal_parameters:
  1505. template_formal_parameter
  1506. {
  1507. CPPTemplateScope *ts = current_scope->as_template_scope();
  1508. assert(ts != nullptr);
  1509. ts->add_template_parameter($1);
  1510. }
  1511. | template_nonempty_formal_parameters ',' template_formal_parameter
  1512. {
  1513. CPPTemplateScope *ts = current_scope->as_template_scope();
  1514. assert(ts != nullptr);
  1515. ts->add_template_parameter($3);
  1516. }
  1517. ;
  1518. typename_keyword:
  1519. KW_CLASS
  1520. | KW_TYPENAME
  1521. ;
  1522. template_formal_parameter:
  1523. typename_keyword
  1524. {
  1525. $$ = CPPType::new_type(new CPPClassTemplateParameter(nullptr));
  1526. }
  1527. | typename_keyword name
  1528. {
  1529. $$ = CPPType::new_type(new CPPClassTemplateParameter($2));
  1530. }
  1531. | typename_keyword name '=' full_type
  1532. {
  1533. $$ = CPPType::new_type(new CPPClassTemplateParameter($2, $4));
  1534. }
  1535. | typename_keyword ELLIPSIS
  1536. {
  1537. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter(nullptr);
  1538. ctp->_packed = true;
  1539. $$ = CPPType::new_type(ctp);
  1540. }
  1541. | typename_keyword ELLIPSIS name
  1542. {
  1543. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter($3);
  1544. ctp->_packed = true;
  1545. $$ = CPPType::new_type(ctp);
  1546. }
  1547. | template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1548. {
  1549. CPPInstance *inst = new CPPInstance($1, $2, 0, @2.file);
  1550. inst->set_initializer($3);
  1551. $$ = inst;
  1552. }
  1553. | KW_CONST template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1554. {
  1555. $3->add_modifier(IIT_const);
  1556. CPPInstance *inst = new CPPInstance($2, $3, 0, @3.file);
  1557. inst->set_initializer($4);
  1558. $$ = inst;
  1559. }
  1560. | template_formal_parameter_type parameter_pack_identifier
  1561. {
  1562. CPPInstance *inst = new CPPInstance($1, $2, 0, @2.file);
  1563. $$ = inst;
  1564. }
  1565. | KW_CONST template_formal_parameter_type parameter_pack_identifier
  1566. {
  1567. $3->add_modifier(IIT_const);
  1568. CPPInstance *inst = new CPPInstance($2, $3, 0, @3.file);
  1569. $$ = inst;
  1570. }
  1571. ;
  1572. template_formal_parameter_type:
  1573. simple_type
  1574. {
  1575. $$ = CPPType::new_type($1);
  1576. }
  1577. | IDENTIFIER
  1578. {
  1579. yywarning("Not a type: " + $1->get_fully_scoped_name(), @1);
  1580. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  1581. }
  1582. | TYPENAME_IDENTIFIER
  1583. {
  1584. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1585. if ($$ == nullptr) {
  1586. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1587. }
  1588. assert($$ != nullptr);
  1589. }
  1590. | TYPEPACK_IDENTIFIER
  1591. {
  1592. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1593. if ($$ == nullptr) {
  1594. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1595. }
  1596. assert($$ != nullptr);
  1597. }
  1598. ;
  1599. instance_identifier:
  1600. name_no_final
  1601. {
  1602. $$ = new CPPInstanceIdentifier($1);
  1603. }
  1604. | KW_OPERATOR function_operator
  1605. {
  1606. // For an operator function. We implement this simply by building a
  1607. // ficticious name for the function; in other respects it's just
  1608. // like a regular function.
  1609. CPPIdentifier *ident = $1;
  1610. if (ident == nullptr) {
  1611. ident = new CPPIdentifier("operator "+$2, @2);
  1612. } else {
  1613. ident->_names.push_back("operator "+$2);
  1614. }
  1615. $$ = new CPPInstanceIdentifier(ident);
  1616. }
  1617. | KW_OPERATOR SIMPLE_STRING IDENTIFIER
  1618. {
  1619. // A C++11 literal operator.
  1620. if (!$2.empty()) {
  1621. yyerror("expected empty string", @2);
  1622. }
  1623. CPPIdentifier *ident = $1;
  1624. if (ident == nullptr) {
  1625. ident = new CPPIdentifier("operator \"\" "+$3->get_simple_name(), @3);
  1626. } else {
  1627. ident->_names.push_back("operator \"\" "+$3->get_simple_name());
  1628. }
  1629. $$ = new CPPInstanceIdentifier(ident);
  1630. }
  1631. | KW_CONST instance_identifier %prec UNARY
  1632. {
  1633. $$ = $2;
  1634. $$->add_modifier(IIT_const);
  1635. }
  1636. | KW_VOLATILE instance_identifier %prec UNARY
  1637. {
  1638. $$ = $2;
  1639. $$->add_modifier(IIT_volatile);
  1640. }
  1641. | '*' instance_identifier %prec UNARY
  1642. {
  1643. $$ = $2;
  1644. $$->add_modifier(IIT_pointer);
  1645. }
  1646. | '&' instance_identifier %prec UNARY
  1647. {
  1648. $$ = $2;
  1649. $$->add_modifier(IIT_reference);
  1650. }
  1651. | ANDAND instance_identifier %prec UNARY
  1652. {
  1653. $$ = $2;
  1654. $$->add_modifier(IIT_rvalue_reference);
  1655. }
  1656. | SCOPING '*' instance_identifier %prec UNARY
  1657. {
  1658. $$ = $3;
  1659. $$->add_scoped_pointer_modifier($1);
  1660. }
  1661. | instance_identifier '[' optional_const_expr ']'
  1662. {
  1663. $$ = $1;
  1664. $$->add_array_modifier($3);
  1665. }
  1666. | '(' instance_identifier ')'
  1667. {
  1668. $$ = $2;
  1669. $$->add_modifier(IIT_paren);
  1670. }
  1671. | instance_identifier '('
  1672. {
  1673. // Create a scope for this function (in case it is a function)
  1674. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1675. CPPNameComponent(""), V_private);
  1676. // It still needs to be able to pick up any template arguments, if this is
  1677. // a definition for a method template. Add a fake "using" declaration to
  1678. // accomplish this.
  1679. scope->_using.insert(current_scope);
  1680. push_scope(scope);
  1681. }
  1682. formal_parameter_list ')' function_post
  1683. {
  1684. pop_scope();
  1685. $$ = $1;
  1686. if ($4->is_parameter_expr() && $6 == 0) {
  1687. // Oops, this must have been an instance declaration with a
  1688. // parameter list, not a function prototype.
  1689. $$->add_initializer_modifier($4);
  1690. } else {
  1691. // This was (probably) a function prototype.
  1692. $$->add_func_modifier($4, $6);
  1693. }
  1694. }
  1695. ;
  1696. instance_identifier_and_maybe_trailing_return_type:
  1697. instance_identifier maybe_trailing_return_type
  1698. {
  1699. // This is handled a bit awkwardly right now. Ideally it'd be wrapped
  1700. // up in the instance_identifier rule, but then more needs to happen in
  1701. // order to avoid shift/reduce conflicts.
  1702. if ($2 != nullptr) {
  1703. $1->add_trailing_return_type($2);
  1704. }
  1705. $$ = $1;
  1706. }
  1707. | instance_identifier ':' INTEGER
  1708. {
  1709. // Bitfield definition.
  1710. $1->_bit_width = $3;
  1711. $$ = $1;
  1712. }
  1713. ;
  1714. maybe_trailing_return_type:
  1715. empty
  1716. {
  1717. $$ = nullptr;
  1718. }
  1719. | POINTSAT predefined_type empty_instance_identifier
  1720. {
  1721. $$ = $3->unroll_type($2);
  1722. }
  1723. | POINTSAT KW_CONST predefined_type empty_instance_identifier
  1724. {
  1725. $4->add_modifier(IIT_const);
  1726. $$ = $4->unroll_type($3);
  1727. }
  1728. ;
  1729. maybe_comma_identifier:
  1730. empty
  1731. {
  1732. $$ = nullptr;
  1733. }
  1734. | ',' IDENTIFIER
  1735. {
  1736. $$ = $2;
  1737. }
  1738. ;
  1739. function_parameter_list:
  1740. empty
  1741. {
  1742. $$ = new CPPParameterList;
  1743. }
  1744. | ELLIPSIS
  1745. {
  1746. $$ = new CPPParameterList;
  1747. $$->_includes_ellipsis = true;
  1748. }
  1749. | function_parameters
  1750. {
  1751. $$ = $1;
  1752. }
  1753. | function_parameters ',' ELLIPSIS
  1754. {
  1755. $$ = $1;
  1756. $$->_includes_ellipsis = true;
  1757. }
  1758. | function_parameters ELLIPSIS
  1759. {
  1760. $$ = $1;
  1761. $$->_includes_ellipsis = true;
  1762. }
  1763. ;
  1764. function_parameters:
  1765. function_parameter
  1766. {
  1767. $$ = new CPPParameterList;
  1768. $$->_parameters.push_back($1);
  1769. }
  1770. | function_parameters ',' function_parameter
  1771. {
  1772. $$ = $1;
  1773. $$->_parameters.push_back($3);
  1774. }
  1775. ;
  1776. formal_parameter_list:
  1777. empty
  1778. {
  1779. $$ = new CPPParameterList;
  1780. }
  1781. | ELLIPSIS
  1782. {
  1783. $$ = new CPPParameterList;
  1784. $$->_includes_ellipsis = true;
  1785. }
  1786. | formal_parameters
  1787. {
  1788. $$ = $1;
  1789. }
  1790. | formal_parameters ',' ELLIPSIS
  1791. {
  1792. $$ = $1;
  1793. $$->_includes_ellipsis = true;
  1794. }
  1795. | formal_parameters ELLIPSIS
  1796. {
  1797. $$ = $1;
  1798. $$->_includes_ellipsis = true;
  1799. }
  1800. ;
  1801. formal_parameters:
  1802. formal_parameter
  1803. {
  1804. $$ = new CPPParameterList;
  1805. $$->_parameters.push_back($1);
  1806. }
  1807. | formal_parameters ',' formal_parameter
  1808. {
  1809. $$ = $1;
  1810. $$->_parameters.push_back($3);
  1811. }
  1812. ;
  1813. template_parameter_maybe_initialize:
  1814. empty
  1815. {
  1816. $$ = nullptr;
  1817. }
  1818. | '=' no_angle_bracket_const_expr
  1819. {
  1820. $$ = $2;
  1821. }
  1822. ;
  1823. maybe_initialize:
  1824. empty
  1825. {
  1826. $$ = nullptr;
  1827. }
  1828. | '=' const_expr
  1829. {
  1830. $$ = $2;
  1831. }
  1832. ;
  1833. maybe_initialize_or_constructor_body:
  1834. ';'
  1835. {
  1836. $$ = nullptr;
  1837. }
  1838. | '{' code '}'
  1839. {
  1840. $$ = nullptr;
  1841. }
  1842. | ':' constructor_inits '{' code '}'
  1843. {
  1844. $$ = nullptr;
  1845. }
  1846. | '=' KW_DEFAULT ';'
  1847. {
  1848. $$ = new CPPExpression(CPPExpression::get_default());
  1849. }
  1850. | '=' KW_DELETE ';'
  1851. {
  1852. $$ = new CPPExpression(CPPExpression::get_delete());
  1853. }
  1854. ;
  1855. maybe_initialize_or_function_body:
  1856. ';'
  1857. {
  1858. $$ = nullptr;
  1859. }
  1860. | '{' code '}'
  1861. {
  1862. $$ = nullptr;
  1863. }
  1864. | '=' const_expr ';'
  1865. {
  1866. $$ = $2;
  1867. }
  1868. | '=' KW_DEFAULT ';'
  1869. {
  1870. $$ = new CPPExpression(CPPExpression::get_default());
  1871. }
  1872. | '=' KW_DELETE ';'
  1873. {
  1874. $$ = new CPPExpression(CPPExpression::get_delete());
  1875. }
  1876. | '=' '{' structure_init '}'
  1877. {
  1878. $$ = nullptr;
  1879. }
  1880. ;
  1881. structure_init:
  1882. empty
  1883. | structure_init_body
  1884. | structure_init_body ','
  1885. ;
  1886. structure_init_body:
  1887. const_expr
  1888. {
  1889. }
  1890. | '{' structure_init '}'
  1891. | structure_init_body ',' const_expr
  1892. | structure_init_body ',' '{' structure_init '}'
  1893. ;
  1894. function_parameter:
  1895. type formal_parameter_identifier maybe_initialize
  1896. {
  1897. $$ = new CPPInstance($1, $2, 0, @2.file);
  1898. $$->set_initializer($3);
  1899. }
  1900. | KW_CONST type formal_parameter_identifier maybe_initialize
  1901. {
  1902. $3->add_modifier(IIT_const);
  1903. $$ = new CPPInstance($2, $3, 0, @3.file);
  1904. $$->set_initializer($4);
  1905. }
  1906. | KW_CONST KW_REGISTER type formal_parameter_identifier maybe_initialize
  1907. {
  1908. $4->add_modifier(IIT_const);
  1909. $$ = new CPPInstance($3, $4, 0, @3.file);
  1910. $$->set_initializer($5);
  1911. }
  1912. | type_pack parameter_pack_identifier maybe_initialize
  1913. {
  1914. $$ = new CPPInstance($1, $2, 0, @2.file);
  1915. $$->set_initializer($3);
  1916. }
  1917. | KW_CONST type_pack parameter_pack_identifier maybe_initialize
  1918. {
  1919. $3->add_modifier(IIT_const);
  1920. $$ = new CPPInstance($2, $3, 0, @3.file);
  1921. $$->set_initializer($4);
  1922. }
  1923. | KW_CONST KW_REGISTER type_pack parameter_pack_identifier maybe_initialize
  1924. {
  1925. $4->add_modifier(IIT_const);
  1926. $$ = new CPPInstance($3, $4, 0, @3.file);
  1927. $$->set_initializer($5);
  1928. }
  1929. | KW_REGISTER function_parameter
  1930. {
  1931. $$ = $2;
  1932. }
  1933. | ATTR_LEFT attribute_specifiers ATTR_RIGHT function_parameter
  1934. {
  1935. $$ = $4;
  1936. }
  1937. ;
  1938. /* A "formal parameter" is like a function parameter, except that it parses
  1939. * instance declarations (that look like functions declaration) as well, and
  1940. * as such accepts constexpr parameters.
  1941. */
  1942. formal_parameter:
  1943. function_parameter
  1944. {
  1945. $$ = $1;
  1946. }
  1947. | formal_const_expr
  1948. {
  1949. CPPType *type =
  1950. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_parameter));
  1951. $$ = new CPPInstance(type, "expr");
  1952. $$->set_initializer($1);
  1953. }
  1954. ;
  1955. not_paren_formal_parameter_identifier:
  1956. empty
  1957. {
  1958. $$ = new CPPInstanceIdentifier(nullptr);
  1959. }
  1960. | name_no_final
  1961. {
  1962. $$ = new CPPInstanceIdentifier($1);
  1963. }
  1964. | KW_CONST not_paren_formal_parameter_identifier %prec UNARY
  1965. {
  1966. $$ = $2;
  1967. $$->add_modifier(IIT_const);
  1968. }
  1969. | KW_VOLATILE not_paren_formal_parameter_identifier %prec UNARY
  1970. {
  1971. $$ = $2;
  1972. $$->add_modifier(IIT_volatile);
  1973. }
  1974. | '*' not_paren_formal_parameter_identifier %prec UNARY
  1975. {
  1976. $$ = $2;
  1977. $$->add_modifier(IIT_pointer);
  1978. }
  1979. | '&' not_paren_formal_parameter_identifier %prec UNARY
  1980. {
  1981. $$ = $2;
  1982. $$->add_modifier(IIT_reference);
  1983. }
  1984. | ANDAND not_paren_formal_parameter_identifier %prec UNARY
  1985. {
  1986. $$ = $2;
  1987. $$->add_modifier(IIT_rvalue_reference);
  1988. }
  1989. | SCOPING '*' not_paren_formal_parameter_identifier %prec UNARY
  1990. {
  1991. $$ = $3;
  1992. $$->add_scoped_pointer_modifier($1);
  1993. }
  1994. | not_paren_formal_parameter_identifier '[' optional_const_expr ']'
  1995. {
  1996. $$ = $1;
  1997. $$->add_array_modifier($3);
  1998. }
  1999. ;
  2000. formal_parameter_identifier:
  2001. empty
  2002. {
  2003. $$ = new CPPInstanceIdentifier(nullptr);
  2004. }
  2005. | name_no_final
  2006. {
  2007. $$ = new CPPInstanceIdentifier($1);
  2008. }
  2009. | KW_CONST formal_parameter_identifier %prec UNARY
  2010. {
  2011. $$ = $2;
  2012. $$->add_modifier(IIT_const);
  2013. }
  2014. | KW_VOLATILE formal_parameter_identifier %prec UNARY
  2015. {
  2016. $$ = $2;
  2017. $$->add_modifier(IIT_volatile);
  2018. }
  2019. | '*' formal_parameter_identifier %prec UNARY
  2020. {
  2021. $$ = $2;
  2022. $$->add_modifier(IIT_pointer);
  2023. }
  2024. | '&' formal_parameter_identifier %prec UNARY
  2025. {
  2026. $$ = $2;
  2027. $$->add_modifier(IIT_reference);
  2028. }
  2029. | ANDAND formal_parameter_identifier %prec UNARY
  2030. {
  2031. $$ = $2;
  2032. $$->add_modifier(IIT_rvalue_reference);
  2033. }
  2034. | SCOPING '*' formal_parameter_identifier %prec UNARY
  2035. {
  2036. $$ = $3;
  2037. $$->add_scoped_pointer_modifier($1);
  2038. }
  2039. | formal_parameter_identifier '[' optional_const_expr ']'
  2040. {
  2041. $$ = $1;
  2042. $$->add_array_modifier($3);
  2043. }
  2044. | '(' formal_parameter_identifier ')' '(' function_parameter_list ')' function_post
  2045. {
  2046. $$ = $2;
  2047. $$->add_modifier(IIT_paren);
  2048. $$->add_func_modifier($5, $7);
  2049. }
  2050. | '(' formal_parameter_identifier ')'
  2051. {
  2052. $$ = $2;
  2053. $$->add_modifier(IIT_paren);
  2054. }
  2055. ;
  2056. parameter_pack_identifier:
  2057. ELLIPSIS
  2058. {
  2059. $$ = new CPPInstanceIdentifier(nullptr);
  2060. $$->_packed = true;
  2061. }
  2062. | ELLIPSIS name
  2063. {
  2064. $$ = new CPPInstanceIdentifier($2);
  2065. $$->_packed = true;
  2066. }
  2067. | KW_CONST parameter_pack_identifier %prec UNARY
  2068. {
  2069. $$ = $2;
  2070. $$->add_modifier(IIT_const);
  2071. }
  2072. | KW_VOLATILE parameter_pack_identifier %prec UNARY
  2073. {
  2074. $$ = $2;
  2075. $$->add_modifier(IIT_volatile);
  2076. }
  2077. | '*' parameter_pack_identifier %prec UNARY
  2078. {
  2079. $$ = $2;
  2080. $$->add_modifier(IIT_pointer);
  2081. }
  2082. | '&' parameter_pack_identifier %prec UNARY
  2083. {
  2084. $$ = $2;
  2085. $$->add_modifier(IIT_reference);
  2086. }
  2087. | ANDAND parameter_pack_identifier %prec UNARY
  2088. {
  2089. $$ = $2;
  2090. $$->add_modifier(IIT_rvalue_reference);
  2091. }
  2092. | SCOPING '*' parameter_pack_identifier %prec UNARY
  2093. {
  2094. $$ = $3;
  2095. $$->add_scoped_pointer_modifier($1);
  2096. }
  2097. | parameter_pack_identifier '[' optional_const_expr ']'
  2098. {
  2099. $$ = $1;
  2100. $$->add_array_modifier($3);
  2101. }
  2102. | '(' parameter_pack_identifier ')' '(' function_parameter_list ')' function_post
  2103. {
  2104. $$ = $2;
  2105. $$->add_modifier(IIT_paren);
  2106. $$->add_func_modifier($5, $7);
  2107. }
  2108. | '(' parameter_pack_identifier ')'
  2109. {
  2110. $$ = $2;
  2111. $$->add_modifier(IIT_paren);
  2112. }
  2113. ;
  2114. not_paren_empty_instance_identifier:
  2115. empty
  2116. {
  2117. $$ = new CPPInstanceIdentifier(nullptr);
  2118. }
  2119. | ELLIPSIS
  2120. {
  2121. $$ = new CPPInstanceIdentifier(nullptr);
  2122. $$->_packed = true;
  2123. }
  2124. | ELLIPSIS name
  2125. {
  2126. $$ = new CPPInstanceIdentifier($2);
  2127. $$->_packed = true;
  2128. }
  2129. | KW_CONST not_paren_empty_instance_identifier %prec UNARY
  2130. {
  2131. $$ = $2;
  2132. $$->add_modifier(IIT_const);
  2133. }
  2134. | KW_VOLATILE not_paren_empty_instance_identifier %prec UNARY
  2135. {
  2136. $$ = $2;
  2137. $$->add_modifier(IIT_volatile);
  2138. }
  2139. | '*' not_paren_empty_instance_identifier %prec UNARY
  2140. {
  2141. $$ = $2;
  2142. $$->add_modifier(IIT_pointer);
  2143. }
  2144. | '&' not_paren_empty_instance_identifier %prec UNARY
  2145. {
  2146. $$ = $2;
  2147. $$->add_modifier(IIT_reference);
  2148. }
  2149. | ANDAND not_paren_empty_instance_identifier %prec UNARY
  2150. {
  2151. $$ = $2;
  2152. $$->add_modifier(IIT_rvalue_reference);
  2153. }
  2154. | SCOPING '*' not_paren_empty_instance_identifier %prec UNARY
  2155. {
  2156. $$ = $3;
  2157. $$->add_scoped_pointer_modifier($1);
  2158. }
  2159. | not_paren_empty_instance_identifier '[' optional_const_expr ']'
  2160. {
  2161. $$ = $1;
  2162. $$->add_array_modifier($3);
  2163. }
  2164. ;
  2165. empty_instance_identifier:
  2166. empty
  2167. {
  2168. $$ = new CPPInstanceIdentifier(nullptr);
  2169. }
  2170. | ELLIPSIS
  2171. {
  2172. $$ = new CPPInstanceIdentifier(nullptr);
  2173. $$->_packed = true;
  2174. }
  2175. | ELLIPSIS name
  2176. {
  2177. $$ = new CPPInstanceIdentifier($2);
  2178. $$->_packed = true;
  2179. }
  2180. | KW_CONST empty_instance_identifier %prec UNARY
  2181. {
  2182. $$ = $2;
  2183. $$->add_modifier(IIT_const);
  2184. }
  2185. | KW_VOLATILE empty_instance_identifier %prec UNARY
  2186. {
  2187. $$ = $2;
  2188. $$->add_modifier(IIT_volatile);
  2189. }
  2190. | '*' not_paren_empty_instance_identifier %prec UNARY
  2191. {
  2192. $$ = $2;
  2193. $$->add_modifier(IIT_pointer);
  2194. }
  2195. | '&' not_paren_empty_instance_identifier %prec UNARY
  2196. {
  2197. $$ = $2;
  2198. $$->add_modifier(IIT_reference);
  2199. }
  2200. | ANDAND not_paren_empty_instance_identifier %prec UNARY
  2201. {
  2202. $$ = $2;
  2203. $$->add_modifier(IIT_rvalue_reference);
  2204. }
  2205. | SCOPING '*' not_paren_empty_instance_identifier %prec UNARY
  2206. {
  2207. $$ = $3;
  2208. $$->add_scoped_pointer_modifier($1);
  2209. }
  2210. | not_paren_empty_instance_identifier '[' optional_const_expr ']'
  2211. {
  2212. $$ = $1;
  2213. $$->add_array_modifier($3);
  2214. }
  2215. | '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2216. {
  2217. $$ = new CPPInstanceIdentifier(nullptr);
  2218. $$->add_modifier(IIT_paren);
  2219. $$->add_func_modifier($2, $4, $5);
  2220. }
  2221. | '(' '*' not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2222. {
  2223. $$ = $3;
  2224. $$->add_modifier(IIT_pointer);
  2225. $$->add_modifier(IIT_paren);
  2226. $$->add_func_modifier($6, $8, $9);
  2227. }
  2228. | '(' '&' not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2229. {
  2230. $$ = $3;
  2231. $$->add_modifier(IIT_reference);
  2232. $$->add_modifier(IIT_paren);
  2233. $$->add_func_modifier($6, $8, $9);
  2234. }
  2235. | '(' ANDAND not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2236. {
  2237. $$ = $3;
  2238. $$->add_modifier(IIT_rvalue_reference);
  2239. $$->add_modifier(IIT_paren);
  2240. $$->add_func_modifier($6, $8, $9);
  2241. }
  2242. ;
  2243. type:
  2244. simple_type
  2245. {
  2246. $$ = CPPType::new_type($1);
  2247. }
  2248. | TYPENAME_IDENTIFIER
  2249. {
  2250. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2251. if ($$ == nullptr) {
  2252. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2253. }
  2254. assert($$ != nullptr);
  2255. }
  2256. | KW_TYPENAME name
  2257. {
  2258. $$ = CPPType::new_type(new CPPTBDType($2));
  2259. }
  2260. | anonymous_struct
  2261. {
  2262. $$ = CPPType::new_type($1);
  2263. }
  2264. | named_struct
  2265. {
  2266. $$ = CPPType::new_type($1);
  2267. }
  2268. | enum
  2269. {
  2270. $$ = CPPType::new_type($1);
  2271. }
  2272. | struct_keyword struct_attributes name
  2273. {
  2274. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2275. if (type != nullptr) {
  2276. $$ = type;
  2277. } else {
  2278. CPPExtensionType *et =
  2279. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2280. ->as_extension_type();
  2281. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2282. if (scope != nullptr) {
  2283. scope->define_extension_type(et);
  2284. }
  2285. $$ = et;
  2286. }
  2287. }
  2288. | enum_keyword name_no_final ':' enum_element_type
  2289. {
  2290. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2291. if (type != nullptr) {
  2292. $$ = type;
  2293. } else {
  2294. CPPExtensionType *et =
  2295. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2296. ->as_extension_type();
  2297. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2298. if (scope != nullptr) {
  2299. scope->define_extension_type(et);
  2300. }
  2301. $$ = et;
  2302. }
  2303. }
  2304. | KW_DECLTYPE '(' const_expr ')'
  2305. {
  2306. $$ = $3->determine_type();
  2307. if ($$ == nullptr) {
  2308. stringstream str;
  2309. str << *$3;
  2310. yyerror("could not determine type of " + str.str(), @3);
  2311. }
  2312. }
  2313. | KW_DECLTYPE '(' KW_AUTO ')'
  2314. {
  2315. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2316. }
  2317. | KW_UNDERLYING_TYPE '(' full_type ')'
  2318. {
  2319. CPPEnumType *enum_type = $3->as_enum_type();
  2320. if (enum_type == nullptr) {
  2321. yyerror("an enumeration type is required", @3);
  2322. $$ = $3;
  2323. } else {
  2324. $$ = enum_type->get_underlying_type();
  2325. }
  2326. }
  2327. | KW_AUTO
  2328. {
  2329. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2330. }
  2331. ;
  2332. type_pack:
  2333. TYPEPACK_IDENTIFIER
  2334. {
  2335. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2336. if ($$ == nullptr) {
  2337. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2338. }
  2339. assert($$ != nullptr);
  2340. }
  2341. ;
  2342. type_decl:
  2343. simple_type
  2344. {
  2345. $$ = CPPType::new_type($1);
  2346. }
  2347. | TYPENAME_IDENTIFIER
  2348. {
  2349. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2350. if ($$ == nullptr) {
  2351. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2352. }
  2353. assert($$ != nullptr);
  2354. }
  2355. | KW_TYPENAME name
  2356. {
  2357. $$ = CPPType::new_type(new CPPTBDType($2));
  2358. }
  2359. | anonymous_struct
  2360. {
  2361. $$ = CPPType::new_type($1);
  2362. }
  2363. | named_struct
  2364. {
  2365. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  2366. }
  2367. | enum
  2368. {
  2369. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  2370. }
  2371. | struct_keyword struct_attributes name
  2372. {
  2373. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2374. if (type != nullptr) {
  2375. $$ = type;
  2376. } else {
  2377. CPPExtensionType *et =
  2378. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2379. ->as_extension_type();
  2380. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2381. if (scope != nullptr) {
  2382. scope->define_extension_type(et);
  2383. }
  2384. $$ = et;
  2385. }
  2386. }
  2387. | enum_keyword name_no_final ':' enum_element_type
  2388. {
  2389. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2390. if (type != nullptr) {
  2391. $$ = type;
  2392. } else {
  2393. CPPExtensionType *et =
  2394. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2395. ->as_extension_type();
  2396. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2397. if (scope != nullptr) {
  2398. scope->define_extension_type(et);
  2399. }
  2400. $$ = et;
  2401. }
  2402. }
  2403. | enum_keyword name
  2404. {
  2405. yywarning(string("C++ does not permit forward declaration of untyped enum ") + $2->get_fully_scoped_name(), @1);
  2406. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2407. if (type != nullptr) {
  2408. $$ = type;
  2409. } else {
  2410. CPPExtensionType *et =
  2411. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2412. ->as_extension_type();
  2413. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2414. if (scope != nullptr) {
  2415. scope->define_extension_type(et);
  2416. }
  2417. $$ = et;
  2418. }
  2419. }
  2420. | KW_DECLTYPE '(' const_expr ')'
  2421. {
  2422. $$ = $3->determine_type();
  2423. if ($$ == nullptr) {
  2424. stringstream str;
  2425. str << *$3;
  2426. yyerror("could not determine type of " + str.str(), @3);
  2427. }
  2428. }
  2429. | KW_DECLTYPE '(' KW_AUTO ')'
  2430. {
  2431. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2432. }
  2433. | KW_UNDERLYING_TYPE '(' full_type ')'
  2434. {
  2435. CPPEnumType *enum_type = $3->as_enum_type();
  2436. if (enum_type == nullptr) {
  2437. yyerror("an enumeration type is required", @3);
  2438. $$ = $3;
  2439. } else {
  2440. $$ = enum_type->get_underlying_type();
  2441. }
  2442. }
  2443. | KW_AUTO
  2444. {
  2445. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2446. }
  2447. ;
  2448. predefined_type:
  2449. simple_type
  2450. {
  2451. $$ = CPPType::new_type($1);
  2452. }
  2453. | TYPENAME_IDENTIFIER
  2454. {
  2455. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2456. if ($$ == nullptr) {
  2457. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2458. }
  2459. assert($$ != nullptr);
  2460. }
  2461. | KW_TYPENAME name
  2462. {
  2463. $$ = CPPType::new_type(new CPPTBDType($2));
  2464. }
  2465. | struct_keyword struct_attributes name
  2466. {
  2467. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2468. if (type != nullptr) {
  2469. $$ = type;
  2470. } else {
  2471. CPPExtensionType *et =
  2472. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2473. ->as_extension_type();
  2474. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2475. if (scope != nullptr) {
  2476. scope->define_extension_type(et);
  2477. }
  2478. $$ = et;
  2479. }
  2480. }
  2481. | enum_keyword name
  2482. {
  2483. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2484. if (type != nullptr) {
  2485. $$ = type;
  2486. } else {
  2487. CPPExtensionType *et =
  2488. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2489. ->as_extension_type();
  2490. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2491. if (scope != nullptr) {
  2492. scope->define_extension_type(et);
  2493. }
  2494. $$ = et;
  2495. }
  2496. }
  2497. | KW_DECLTYPE '(' const_expr ')'
  2498. {
  2499. $$ = $3->determine_type();
  2500. if ($$ == nullptr) {
  2501. stringstream str;
  2502. str << *$3;
  2503. yyerror("could not determine type of " + str.str(), @3);
  2504. }
  2505. }
  2506. | KW_UNDERLYING_TYPE '(' full_type ')'
  2507. {
  2508. CPPEnumType *enum_type = $3->as_enum_type();
  2509. if (enum_type == nullptr) {
  2510. yyerror("an enumeration type is required", @3);
  2511. $$ = $3;
  2512. } else {
  2513. $$ = enum_type->get_underlying_type();
  2514. }
  2515. }
  2516. | KW_AUTO
  2517. {
  2518. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2519. }
  2520. ;
  2521. var_type_decl:
  2522. type_decl
  2523. {
  2524. $$ = $1;
  2525. }
  2526. | IDENTIFIER
  2527. {
  2528. yyerror(string("unknown type '") + $1->get_fully_scoped_name() + "'", @1);
  2529. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  2530. }
  2531. full_type:
  2532. type empty_instance_identifier
  2533. {
  2534. $$ = $2->unroll_type($1);
  2535. }
  2536. | KW_CONST type empty_instance_identifier
  2537. {
  2538. $3->add_modifier(IIT_const);
  2539. $$ = $3->unroll_type($2);
  2540. }
  2541. | type_pack empty_instance_identifier
  2542. {
  2543. $$ = $2->unroll_type($1);
  2544. }
  2545. | KW_CONST type_pack empty_instance_identifier
  2546. {
  2547. $3->add_modifier(IIT_const);
  2548. $$ = $3->unroll_type($2);
  2549. }
  2550. ;
  2551. struct_attributes:
  2552. empty
  2553. | struct_attributes ATTR_LEFT attribute_specifiers ATTR_RIGHT
  2554. | struct_attributes KW_ALIGNAS '(' const_expr ')'
  2555. | struct_attributes KW_ALIGNAS '(' type_decl ')'
  2556. ;
  2557. anonymous_struct:
  2558. struct_keyword struct_attributes '{'
  2559. {
  2560. CPPVisibility starting_vis =
  2561. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  2562. CPPScope *new_scope = new CPPScope(current_scope, CPPNameComponent("anon"),
  2563. starting_vis);
  2564. CPPStructType *st = new CPPStructType($1, nullptr, current_scope,
  2565. new_scope, @1.file);
  2566. new_scope->set_struct_type(st);
  2567. push_scope(new_scope);
  2568. push_struct(st);
  2569. }
  2570. cpp '}'
  2571. {
  2572. $$ = current_struct;
  2573. current_struct->_incomplete = false;
  2574. pop_struct();
  2575. pop_scope();
  2576. }
  2577. ;
  2578. named_struct:
  2579. struct_keyword struct_attributes name_no_final
  2580. {
  2581. CPPVisibility starting_vis =
  2582. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  2583. CPPScope *scope = $3->get_scope(current_scope, global_scope, current_lexer);
  2584. if (scope == nullptr) {
  2585. scope = current_scope;
  2586. }
  2587. CPPScope *new_scope = new CPPScope(scope, $3->_names.back(),
  2588. starting_vis);
  2589. CPPStructType *st = new CPPStructType($1, $3, current_scope,
  2590. new_scope, @1.file);
  2591. new_scope->set_struct_type(st);
  2592. current_scope->define_extension_type(st);
  2593. push_scope(new_scope);
  2594. push_struct(st);
  2595. }
  2596. maybe_final maybe_class_derivation '{' cpp '}'
  2597. {
  2598. $$ = current_struct;
  2599. current_struct->_incomplete = false;
  2600. pop_struct();
  2601. pop_scope();
  2602. }
  2603. ;
  2604. maybe_final:
  2605. empty
  2606. | KW_FINAL
  2607. {
  2608. current_struct->_final = true;
  2609. }
  2610. ;
  2611. maybe_class_derivation:
  2612. empty
  2613. | class_derivation
  2614. ;
  2615. class_derivation:
  2616. ':' base_specification
  2617. | class_derivation ',' base_specification
  2618. ;
  2619. base_specification:
  2620. class_derivation_name
  2621. {
  2622. current_struct->append_derivation($1, V_unknown, false);
  2623. }
  2624. | KW_PUBLIC class_derivation_name
  2625. {
  2626. current_struct->append_derivation($2, V_public, false);
  2627. }
  2628. | KW_PROTECTED class_derivation_name
  2629. {
  2630. current_struct->append_derivation($2, V_protected, false);
  2631. }
  2632. | KW_PRIVATE class_derivation_name
  2633. {
  2634. current_struct->append_derivation($2, V_private, false);
  2635. }
  2636. | KW_VIRTUAL KW_PUBLIC class_derivation_name
  2637. {
  2638. current_struct->append_derivation($3, V_public, true);
  2639. }
  2640. | KW_VIRTUAL KW_PROTECTED class_derivation_name
  2641. {
  2642. current_struct->append_derivation($3, V_protected, true);
  2643. }
  2644. | KW_VIRTUAL KW_PRIVATE class_derivation_name
  2645. {
  2646. current_struct->append_derivation($3, V_private, true);
  2647. }
  2648. | KW_PUBLIC KW_VIRTUAL class_derivation_name
  2649. {
  2650. current_struct->append_derivation($3, V_public, true);
  2651. }
  2652. | KW_PROTECTED KW_VIRTUAL class_derivation_name
  2653. {
  2654. current_struct->append_derivation($3, V_protected, true);
  2655. }
  2656. | KW_PRIVATE KW_VIRTUAL class_derivation_name
  2657. {
  2658. current_struct->append_derivation($3, V_private, true);
  2659. }
  2660. ;
  2661. enum:
  2662. enum_decl '{' enum_body '}'
  2663. {
  2664. $$ = current_enum;
  2665. current_enum = nullptr;
  2666. }
  2667. ;
  2668. enum_decl:
  2669. enum_keyword ':' enum_element_type
  2670. {
  2671. current_enum = new CPPEnumType($1, nullptr, $3, current_scope, nullptr, @1.file);
  2672. }
  2673. | enum_keyword
  2674. {
  2675. current_enum = new CPPEnumType($1, nullptr, current_scope, nullptr, @1.file);
  2676. }
  2677. | enum_keyword name_no_final ':' enum_element_type
  2678. {
  2679. CPPScope *new_scope = new CPPScope(current_scope, $2->_names.back(), V_public);
  2680. current_enum = new CPPEnumType($1, $2, $4, current_scope, new_scope, @1.file);
  2681. }
  2682. | enum_keyword name_no_final
  2683. {
  2684. CPPScope *new_scope = new CPPScope(current_scope, $2->_names.back(), V_public);
  2685. current_enum = new CPPEnumType($1, $2, current_scope, new_scope, @1.file);
  2686. }
  2687. ;
  2688. enum_element_type:
  2689. simple_int_type
  2690. {
  2691. $$ = CPPType::new_type($1);
  2692. }
  2693. | TYPENAME_IDENTIFIER
  2694. {
  2695. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2696. }
  2697. ;
  2698. enum_body_trailing_comma:
  2699. empty
  2700. | enum_body_trailing_comma name ','
  2701. {
  2702. assert(current_enum != nullptr);
  2703. current_enum->add_element($2->get_simple_name(), nullptr, current_lexer, @2);
  2704. }
  2705. | enum_body_trailing_comma name '=' const_expr ','
  2706. {
  2707. assert(current_enum != nullptr);
  2708. current_enum->add_element($2->get_simple_name(), $4, current_lexer, @2);
  2709. };
  2710. enum_body:
  2711. enum_body_trailing_comma
  2712. | enum_body_trailing_comma name
  2713. {
  2714. assert(current_enum != nullptr);
  2715. current_enum->add_element($2->get_simple_name(), nullptr, current_lexer, @2);
  2716. }
  2717. | enum_body_trailing_comma name '=' const_expr
  2718. {
  2719. assert(current_enum != nullptr);
  2720. current_enum->add_element($2->get_simple_name(), $4, current_lexer, @2);
  2721. }
  2722. ;
  2723. enum_keyword:
  2724. KW_ENUM
  2725. {
  2726. $$ = CPPExtensionType::T_enum;
  2727. }
  2728. | KW_ENUM KW_CLASS
  2729. {
  2730. $$ = CPPExtensionType::T_enum_class;
  2731. }
  2732. | KW_ENUM KW_STRUCT
  2733. {
  2734. $$ = CPPExtensionType::T_enum_struct;
  2735. }
  2736. ;
  2737. struct_keyword:
  2738. KW_CLASS
  2739. {
  2740. $$ = CPPExtensionType::T_class;
  2741. }
  2742. | KW_STRUCT
  2743. {
  2744. $$ = CPPExtensionType::T_struct;
  2745. }
  2746. | KW_UNION
  2747. {
  2748. $$ = CPPExtensionType::T_union;
  2749. }
  2750. ;
  2751. namespace_declaration:
  2752. KW_NAMESPACE name '{'
  2753. {
  2754. CPPScope *scope = $2->find_scope(current_scope, global_scope, current_lexer);
  2755. if (scope == nullptr) {
  2756. // This must be a new namespace declaration.
  2757. CPPScope *parent_scope =
  2758. $2->get_scope(current_scope, global_scope, current_lexer);
  2759. if (parent_scope == nullptr) {
  2760. parent_scope = current_scope;
  2761. }
  2762. scope = new CPPScope(parent_scope, $2->_names.back(), V_public);
  2763. }
  2764. CPPNamespace *nspace = new CPPNamespace($2, scope, @1.file);
  2765. current_scope->add_declaration(nspace, global_scope, current_lexer, @1);
  2766. current_scope->define_namespace(nspace);
  2767. push_scope(scope);
  2768. }
  2769. cpp '}'
  2770. {
  2771. pop_scope();
  2772. }
  2773. | KW_INLINE KW_NAMESPACE name '{'
  2774. {
  2775. CPPScope *scope = $3->find_scope(current_scope, global_scope, current_lexer);
  2776. if (scope == nullptr) {
  2777. // This must be a new namespace declaration.
  2778. CPPScope *parent_scope =
  2779. $3->get_scope(current_scope, global_scope, current_lexer);
  2780. if (parent_scope == nullptr) {
  2781. parent_scope = current_scope;
  2782. }
  2783. scope = new CPPScope(parent_scope, $3->_names.back(), V_public);
  2784. }
  2785. CPPNamespace *nspace = new CPPNamespace($3, scope, @2.file);
  2786. nspace->_is_inline = true;
  2787. current_scope->add_declaration(nspace, global_scope, current_lexer, @2);
  2788. current_scope->define_namespace(nspace);
  2789. push_scope(scope);
  2790. }
  2791. cpp '}'
  2792. {
  2793. pop_scope();
  2794. }
  2795. | KW_NAMESPACE '{' cpp '}'
  2796. | KW_INLINE KW_NAMESPACE '{' cpp '}'
  2797. ;
  2798. using_declaration:
  2799. KW_USING name ';'
  2800. {
  2801. CPPUsing *using_decl = new CPPUsing($2, false, @1.file);
  2802. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  2803. current_scope->add_using(using_decl, global_scope, current_lexer);
  2804. }
  2805. | KW_USING name '=' full_type ';'
  2806. {
  2807. // This is really just an alternative way to declare a typedef.
  2808. CPPTypedefType *typedef_type = new CPPTypedefType($4, $2, current_scope);
  2809. typedef_type->_using = true;
  2810. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  2811. }
  2812. | KW_USING KW_NAMESPACE name ';'
  2813. {
  2814. CPPUsing *using_decl = new CPPUsing($3, true, @1.file);
  2815. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  2816. current_scope->add_using(using_decl, global_scope, current_lexer);
  2817. }
  2818. ;
  2819. simple_type:
  2820. simple_int_type
  2821. | simple_float_type
  2822. | simple_void_type
  2823. ;
  2824. simple_int_type:
  2825. KW_BOOL
  2826. {
  2827. $$ = new CPPSimpleType(CPPSimpleType::T_bool);
  2828. }
  2829. | KW_CHAR
  2830. {
  2831. $$ = new CPPSimpleType(CPPSimpleType::T_char);
  2832. }
  2833. | KW_WCHAR_T
  2834. {
  2835. $$ = new CPPSimpleType(CPPSimpleType::T_wchar_t);
  2836. }
  2837. | KW_CHAR16_T
  2838. {
  2839. $$ = new CPPSimpleType(CPPSimpleType::T_char16_t);
  2840. }
  2841. | KW_CHAR32_T
  2842. {
  2843. $$ = new CPPSimpleType(CPPSimpleType::T_char32_t);
  2844. }
  2845. | KW_SHORT
  2846. {
  2847. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2848. CPPSimpleType::F_short);
  2849. }
  2850. | KW_LONG
  2851. {
  2852. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2853. CPPSimpleType::F_long);
  2854. }
  2855. | KW_UNSIGNED
  2856. {
  2857. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2858. CPPSimpleType::F_unsigned);
  2859. }
  2860. | KW_SIGNED
  2861. {
  2862. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2863. CPPSimpleType::F_signed);
  2864. }
  2865. | KW_INT
  2866. {
  2867. $$ = new CPPSimpleType(CPPSimpleType::T_int);
  2868. }
  2869. | KW_SHORT simple_int_type
  2870. {
  2871. $$ = $2;
  2872. $$->_flags |= CPPSimpleType::F_short;
  2873. }
  2874. | KW_LONG simple_int_type
  2875. {
  2876. $$ = $2;
  2877. if ($$->_flags & CPPSimpleType::F_long) {
  2878. $$->_flags |= CPPSimpleType::F_longlong;
  2879. } else {
  2880. $$->_flags |= CPPSimpleType::F_long;
  2881. }
  2882. }
  2883. | KW_UNSIGNED simple_int_type
  2884. {
  2885. $$ = $2;
  2886. $$->_flags |= CPPSimpleType::F_unsigned;
  2887. }
  2888. | KW_SIGNED simple_int_type
  2889. {
  2890. $$ = $2;
  2891. $$->_flags |= CPPSimpleType::F_signed;
  2892. }
  2893. ;
  2894. simple_float_type:
  2895. KW_FLOAT
  2896. {
  2897. $$ = new CPPSimpleType(CPPSimpleType::T_float);
  2898. }
  2899. | KW_DOUBLE
  2900. {
  2901. $$ = new CPPSimpleType(CPPSimpleType::T_double);
  2902. }
  2903. | KW_LONG KW_DOUBLE
  2904. {
  2905. $$ = new CPPSimpleType(CPPSimpleType::T_double,
  2906. CPPSimpleType::F_long);
  2907. }
  2908. ;
  2909. simple_void_type:
  2910. KW_VOID
  2911. {
  2912. $$ = new CPPSimpleType(CPPSimpleType::T_void);
  2913. }
  2914. ;
  2915. /* We don't care what the code is. We just want to be sure we match
  2916. up opening and closing braces properly. For anything else, we'll
  2917. accept just token salad. */
  2918. code:
  2919. {
  2920. current_lexer->_resolve_identifiers = false;
  2921. }
  2922. code_block
  2923. {
  2924. current_lexer->_resolve_identifiers = true;
  2925. }
  2926. ;
  2927. code_block:
  2928. empty
  2929. | code_block element
  2930. ;
  2931. element:
  2932. REAL
  2933. | INTEGER
  2934. | SIMPLE_STRING
  2935. | STRING_LITERAL
  2936. | CUSTOM_LITERAL
  2937. | CHAR_TOK
  2938. | IDENTIFIER
  2939. | TYPENAME_IDENTIFIER
  2940. | TYPEPACK_IDENTIFIER
  2941. | SCOPING
  2942. | SIMPLE_IDENTIFIER
  2943. | ELLIPSIS | OROR | ANDAND
  2944. | EQCOMPARE | NECOMPARE | LECOMPARE | GECOMPARE
  2945. | LSHIFT | RSHIFT | POINTSAT_STAR | DOT_STAR | POINTSAT
  2946. | SCOPE | PLUSPLUS | MINUSMINUS
  2947. | TIMESEQUAL | DIVIDEEQUAL | MODEQUAL | PLUSEQUAL | MINUSEQUAL
  2948. | OREQUAL | ANDEQUAL | XOREQUAL | LSHIFTEQUAL | RSHIFTEQUAL
  2949. | ATTR_LEFT | ATTR_RIGHT
  2950. | KW_ALIGNAS | KW_ALIGNOF | KW_AUTO | KW_BOOL | KW_CATCH
  2951. | KW_CHAR | KW_CHAR16_T | KW_CHAR32_T | KW_CLASS | KW_CONST
  2952. | KW_CONSTEXPR | KW_CONST_CAST | KW_DECLTYPE | KW_DEFAULT
  2953. | KW_DELETE | KW_DOUBLE | KW_DYNAMIC_CAST | KW_ELSE | KW_ENUM
  2954. | KW_EXTERN | KW_EXPLICIT | KW_FALSE | KW_FINAL | KW_FLOAT
  2955. | KW_FRIEND | KW_FOR | KW_GOTO | KW_IF | KW_INLINE | KW_INT
  2956. | KW_LONG | KW_MUTABLE | KW_NAMESPACE | KW_NEW | KW_NULLPTR
  2957. | KW_OPERATOR | KW_OVERRIDE | KW_PRIVATE | KW_PROTECTED
  2958. | KW_PUBLIC | KW_PUBLISHED | KW_REGISTER | KW_REINTERPRET_CAST
  2959. | KW_RETURN | KW_SHORT | KW_SIGNED | KW_SIZEOF | KW_STATIC
  2960. | KW_STATIC_ASSERT | KW_STATIC_CAST | KW_STRUCT | KW_TEMPLATE
  2961. | KW_THREAD_LOCAL | KW_THROW | KW_TRUE | KW_TRY | KW_TYPEDEF
  2962. | KW_TYPEID | KW_TYPENAME | KW_UNDERLYING_TYPE | KW_UNION
  2963. | KW_UNSIGNED | KW_USING | KW_VIRTUAL | KW_VOID | KW_VOLATILE
  2964. | KW_WCHAR_T | KW_WHILE
  2965. {
  2966. }
  2967. | '+' | '-' | '*' | '/' | '&' | '|' | '^' | '!' | '~' | '=' | '%'
  2968. | '<' | '>' | '(' | ')' | '.' | ',' | ';' | ':' | '[' | ']'
  2969. | '?' | '{' code_block '}'
  2970. ;
  2971. optional_const_expr:
  2972. empty
  2973. {
  2974. $$ = nullptr;
  2975. }
  2976. | const_expr
  2977. {
  2978. $$ = $1;
  2979. }
  2980. ;
  2981. optional_const_expr_comma:
  2982. empty
  2983. {
  2984. $$ = nullptr;
  2985. }
  2986. | const_expr_comma
  2987. {
  2988. $$ = $1;
  2989. }
  2990. ;
  2991. const_expr_comma:
  2992. const_expr
  2993. {
  2994. $$ = $1;
  2995. }
  2996. | const_expr_comma ',' const_expr
  2997. {
  2998. $$ = new CPPExpression(',', $1, $3);
  2999. }
  3000. ;
  3001. no_angle_bracket_const_expr:
  3002. const_operand
  3003. {
  3004. $$ = $1;
  3005. }
  3006. | '(' full_type ')' no_angle_bracket_const_expr %prec UNARY
  3007. {
  3008. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3009. }
  3010. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3011. {
  3012. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3013. }
  3014. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3015. {
  3016. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3017. }
  3018. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3019. {
  3020. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3021. }
  3022. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3023. {
  3024. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3025. }
  3026. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3027. {
  3028. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3029. }
  3030. | KW_SIZEOF '(' IDENTIFIER ')' %prec UNARY
  3031. {
  3032. CPPDeclaration *arg = $3->find_symbol(current_scope, global_scope, current_lexer);
  3033. if (arg == nullptr) {
  3034. yyerror("undefined sizeof argument: " + $3->get_fully_scoped_name(), @3);
  3035. } else if (arg->get_subtype() == CPPDeclaration::ST_instance) {
  3036. CPPInstance *inst = arg->as_instance();
  3037. $$ = new CPPExpression(CPPExpression::sizeof_func(inst->_type));
  3038. } else {
  3039. $$ = new CPPExpression(CPPExpression::sizeof_func(arg->as_type()));
  3040. }
  3041. }
  3042. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3043. {
  3044. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3045. }
  3046. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3047. {
  3048. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3049. }
  3050. | '!' no_angle_bracket_const_expr %prec UNARY
  3051. {
  3052. $$ = new CPPExpression(UNARY_NOT, $2);
  3053. }
  3054. | '~' no_angle_bracket_const_expr %prec UNARY
  3055. {
  3056. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3057. }
  3058. | '-' no_angle_bracket_const_expr %prec UNARY
  3059. {
  3060. $$ = new CPPExpression(UNARY_MINUS, $2);
  3061. }
  3062. | '+' no_angle_bracket_const_expr %prec UNARY
  3063. {
  3064. $$ = new CPPExpression(UNARY_PLUS, $2);
  3065. }
  3066. | '*' no_angle_bracket_const_expr %prec UNARY
  3067. {
  3068. $$ = new CPPExpression(UNARY_STAR, $2);
  3069. }
  3070. | '&' no_angle_bracket_const_expr %prec UNARY
  3071. {
  3072. $$ = new CPPExpression(UNARY_REF, $2);
  3073. }
  3074. | no_angle_bracket_const_expr '*' no_angle_bracket_const_expr
  3075. {
  3076. $$ = new CPPExpression('*', $1, $3);
  3077. }
  3078. | no_angle_bracket_const_expr '/' no_angle_bracket_const_expr
  3079. {
  3080. $$ = new CPPExpression('/', $1, $3);
  3081. }
  3082. | no_angle_bracket_const_expr '%' no_angle_bracket_const_expr
  3083. {
  3084. $$ = new CPPExpression('%', $1, $3);
  3085. }
  3086. | no_angle_bracket_const_expr '+' no_angle_bracket_const_expr
  3087. {
  3088. $$ = new CPPExpression('+', $1, $3);
  3089. }
  3090. | no_angle_bracket_const_expr '-' no_angle_bracket_const_expr
  3091. {
  3092. $$ = new CPPExpression('-', $1, $3);
  3093. }
  3094. | no_angle_bracket_const_expr '|' no_angle_bracket_const_expr
  3095. {
  3096. $$ = new CPPExpression('|', $1, $3);
  3097. }
  3098. | no_angle_bracket_const_expr '^' no_angle_bracket_const_expr
  3099. {
  3100. $$ = new CPPExpression('^', $1, $3);
  3101. }
  3102. | no_angle_bracket_const_expr '&' no_angle_bracket_const_expr
  3103. {
  3104. $$ = new CPPExpression('&', $1, $3);
  3105. }
  3106. | no_angle_bracket_const_expr OROR no_angle_bracket_const_expr
  3107. {
  3108. $$ = new CPPExpression(OROR, $1, $3);
  3109. }
  3110. | no_angle_bracket_const_expr ANDAND no_angle_bracket_const_expr
  3111. {
  3112. $$ = new CPPExpression(ANDAND, $1, $3);
  3113. }
  3114. | no_angle_bracket_const_expr EQCOMPARE no_angle_bracket_const_expr
  3115. {
  3116. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3117. }
  3118. | no_angle_bracket_const_expr NECOMPARE no_angle_bracket_const_expr
  3119. {
  3120. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3121. }
  3122. | no_angle_bracket_const_expr LECOMPARE no_angle_bracket_const_expr
  3123. {
  3124. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3125. }
  3126. | no_angle_bracket_const_expr GECOMPARE no_angle_bracket_const_expr
  3127. {
  3128. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3129. }
  3130. | no_angle_bracket_const_expr LSHIFT no_angle_bracket_const_expr
  3131. {
  3132. $$ = new CPPExpression(LSHIFT, $1, $3);
  3133. }
  3134. | no_angle_bracket_const_expr RSHIFT no_angle_bracket_const_expr
  3135. {
  3136. $$ = new CPPExpression(RSHIFT, $1, $3);
  3137. }
  3138. | no_angle_bracket_const_expr '?' no_angle_bracket_const_expr ':' no_angle_bracket_const_expr
  3139. {
  3140. $$ = new CPPExpression('?', $1, $3, $5);
  3141. }
  3142. | no_angle_bracket_const_expr '[' const_expr ']'
  3143. {
  3144. $$ = new CPPExpression('[', $1, $3);
  3145. }
  3146. | no_angle_bracket_const_expr '(' const_expr_comma ')'
  3147. {
  3148. $$ = new CPPExpression('f', $1, $3);
  3149. }
  3150. | no_angle_bracket_const_expr '(' ')'
  3151. {
  3152. $$ = new CPPExpression('f', $1);
  3153. }
  3154. | no_angle_bracket_const_expr '.' no_angle_bracket_const_expr
  3155. {
  3156. $$ = new CPPExpression('.', $1, $3);
  3157. }
  3158. | no_angle_bracket_const_expr POINTSAT no_angle_bracket_const_expr
  3159. {
  3160. $$ = new CPPExpression(POINTSAT, $1, $3);
  3161. }
  3162. | '(' const_expr_comma ')'
  3163. {
  3164. $$ = $2;
  3165. }
  3166. ;
  3167. const_expr:
  3168. const_operand
  3169. {
  3170. $$ = $1;
  3171. }
  3172. | '(' full_type ')' const_expr %prec UNARY
  3173. {
  3174. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3175. }
  3176. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3177. {
  3178. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3179. }
  3180. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3181. {
  3182. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3183. }
  3184. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3185. {
  3186. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3187. }
  3188. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3189. {
  3190. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3191. }
  3192. | TYPENAME_IDENTIFIER '(' optional_const_expr_comma ')'
  3193. {
  3194. // A constructor call.
  3195. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3196. if (type == nullptr) {
  3197. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3198. }
  3199. assert(type != nullptr);
  3200. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3201. }
  3202. | TYPENAME_IDENTIFIER '{' optional_const_expr_comma '}'
  3203. {
  3204. // Aggregate initialization.
  3205. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3206. if (type == nullptr) {
  3207. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3208. }
  3209. assert(type != nullptr);
  3210. $$ = new CPPExpression(CPPExpression::aggregate_init_op(type, $3));
  3211. }
  3212. | KW_INT '(' optional_const_expr_comma ')'
  3213. {
  3214. CPPType *type =
  3215. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  3216. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3217. }
  3218. | KW_CHAR '(' optional_const_expr_comma ')'
  3219. {
  3220. CPPType *type =
  3221. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  3222. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3223. }
  3224. | KW_WCHAR_T '(' optional_const_expr_comma ')'
  3225. {
  3226. CPPType *type =
  3227. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
  3228. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3229. }
  3230. | KW_CHAR16_T '(' optional_const_expr_comma ')'
  3231. {
  3232. CPPType *type =
  3233. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char16_t));
  3234. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3235. }
  3236. | KW_CHAR32_T '(' optional_const_expr_comma ')'
  3237. {
  3238. CPPType *type =
  3239. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char32_t));
  3240. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3241. }
  3242. | KW_BOOL '(' optional_const_expr_comma ')'
  3243. {
  3244. CPPType *type =
  3245. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  3246. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3247. }
  3248. | KW_SHORT '(' optional_const_expr_comma ')'
  3249. {
  3250. CPPType *type =
  3251. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3252. CPPSimpleType::F_short));
  3253. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3254. }
  3255. | KW_LONG '(' optional_const_expr_comma ')'
  3256. {
  3257. CPPType *type =
  3258. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3259. CPPSimpleType::F_long));
  3260. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3261. }
  3262. | KW_UNSIGNED '(' optional_const_expr_comma ')'
  3263. {
  3264. CPPType *type =
  3265. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3266. CPPSimpleType::F_unsigned));
  3267. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3268. }
  3269. | KW_SIGNED '(' optional_const_expr_comma ')'
  3270. {
  3271. CPPType *type =
  3272. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3273. CPPSimpleType::F_signed));
  3274. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3275. }
  3276. | KW_FLOAT '(' optional_const_expr_comma ')'
  3277. {
  3278. CPPType *type =
  3279. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_float));
  3280. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3281. }
  3282. | KW_DOUBLE '(' optional_const_expr_comma ')'
  3283. {
  3284. CPPType *type =
  3285. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  3286. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3287. }
  3288. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3289. {
  3290. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3291. }
  3292. | KW_SIZEOF '(' IDENTIFIER ')' %prec UNARY
  3293. {
  3294. CPPDeclaration *arg = $3->find_symbol(current_scope, global_scope, current_lexer);
  3295. if (arg == nullptr) {
  3296. yyerror("undefined sizeof argument: " + $3->get_fully_scoped_name(), @3);
  3297. } else if (arg->get_subtype() == CPPDeclaration::ST_instance) {
  3298. CPPInstance *inst = arg->as_instance();
  3299. $$ = new CPPExpression(CPPExpression::sizeof_func(inst->_type));
  3300. } else {
  3301. $$ = new CPPExpression(CPPExpression::sizeof_func(arg->as_type()));
  3302. }
  3303. }
  3304. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3305. {
  3306. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3307. }
  3308. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3309. {
  3310. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3311. }
  3312. | KW_NEW predefined_type %prec UNARY
  3313. {
  3314. $$ = new CPPExpression(CPPExpression::new_op($2));
  3315. }
  3316. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  3317. {
  3318. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  3319. }
  3320. | KW_TYPEID '(' full_type ')'
  3321. {
  3322. CPPIdentifier ident("");
  3323. ident.add_name("std");
  3324. ident.add_name("type_info");
  3325. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3326. if (!std_type_info) {
  3327. yywarning("cannot use typeid before including <typeinfo>", @1);
  3328. }
  3329. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3330. }
  3331. | KW_TYPEID '(' const_expr ')'
  3332. {
  3333. CPPIdentifier ident("");
  3334. ident.add_name("std");
  3335. ident.add_name("type_info");
  3336. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3337. if (!std_type_info) {
  3338. yywarning("cannot use typeid before including <typeinfo>", @1);
  3339. }
  3340. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3341. }
  3342. | '!' const_expr %prec UNARY
  3343. {
  3344. $$ = new CPPExpression(UNARY_NOT, $2);
  3345. }
  3346. | '~' const_expr %prec UNARY
  3347. {
  3348. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3349. }
  3350. | '-' const_expr %prec UNARY
  3351. {
  3352. $$ = new CPPExpression(UNARY_MINUS, $2);
  3353. }
  3354. | '+' const_expr %prec UNARY
  3355. {
  3356. $$ = new CPPExpression(UNARY_PLUS, $2);
  3357. }
  3358. | '*' const_expr %prec UNARY
  3359. {
  3360. $$ = new CPPExpression(UNARY_STAR, $2);
  3361. }
  3362. | '&' const_expr %prec UNARY
  3363. {
  3364. $$ = new CPPExpression(UNARY_REF, $2);
  3365. }
  3366. | const_expr '*' const_expr
  3367. {
  3368. $$ = new CPPExpression('*', $1, $3);
  3369. }
  3370. | const_expr '/' const_expr
  3371. {
  3372. $$ = new CPPExpression('/', $1, $3);
  3373. }
  3374. | const_expr '%' const_expr
  3375. {
  3376. $$ = new CPPExpression('%', $1, $3);
  3377. }
  3378. | const_expr '+' const_expr
  3379. {
  3380. $$ = new CPPExpression('+', $1, $3);
  3381. }
  3382. | const_expr '-' const_expr
  3383. {
  3384. $$ = new CPPExpression('-', $1, $3);
  3385. }
  3386. | const_expr '|' const_expr
  3387. {
  3388. $$ = new CPPExpression('|', $1, $3);
  3389. }
  3390. | const_expr '^' const_expr
  3391. {
  3392. $$ = new CPPExpression('^', $1, $3);
  3393. }
  3394. | const_expr '&' const_expr
  3395. {
  3396. $$ = new CPPExpression('&', $1, $3);
  3397. }
  3398. | const_expr OROR const_expr
  3399. {
  3400. $$ = new CPPExpression(OROR, $1, $3);
  3401. }
  3402. | const_expr ANDAND const_expr
  3403. {
  3404. $$ = new CPPExpression(ANDAND, $1, $3);
  3405. }
  3406. | const_expr EQCOMPARE const_expr
  3407. {
  3408. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3409. }
  3410. | const_expr NECOMPARE const_expr
  3411. {
  3412. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3413. }
  3414. | const_expr LECOMPARE const_expr
  3415. {
  3416. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3417. }
  3418. | const_expr GECOMPARE const_expr
  3419. {
  3420. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3421. }
  3422. | const_expr '<' const_expr
  3423. {
  3424. $$ = new CPPExpression('<', $1, $3);
  3425. }
  3426. | const_expr '>' const_expr
  3427. {
  3428. $$ = new CPPExpression('>', $1, $3);
  3429. }
  3430. | const_expr LSHIFT const_expr
  3431. {
  3432. $$ = new CPPExpression(LSHIFT, $1, $3);
  3433. }
  3434. | const_expr RSHIFT const_expr
  3435. {
  3436. $$ = new CPPExpression(RSHIFT, $1, $3);
  3437. }
  3438. | const_expr '?' const_expr ':' const_expr
  3439. {
  3440. $$ = new CPPExpression('?', $1, $3, $5);
  3441. }
  3442. | const_expr '[' const_expr ']'
  3443. {
  3444. $$ = new CPPExpression('[', $1, $3);
  3445. }
  3446. | const_expr '(' const_expr_comma ')'
  3447. {
  3448. $$ = new CPPExpression('f', $1, $3);
  3449. }
  3450. | const_expr '(' ')'
  3451. {
  3452. $$ = new CPPExpression('f', $1);
  3453. }
  3454. | const_expr '.' const_expr
  3455. {
  3456. $$ = new CPPExpression('.', $1, $3);
  3457. }
  3458. | const_expr POINTSAT const_expr
  3459. {
  3460. $$ = new CPPExpression(POINTSAT, $1, $3);
  3461. }
  3462. | '(' const_expr_comma ')'
  3463. {
  3464. $$ = $2;
  3465. }
  3466. ;
  3467. const_operand:
  3468. INTEGER
  3469. {
  3470. $$ = new CPPExpression($1);
  3471. }
  3472. | KW_TRUE
  3473. {
  3474. $$ = new CPPExpression(true);
  3475. }
  3476. | KW_FALSE
  3477. {
  3478. $$ = new CPPExpression(false);
  3479. }
  3480. | CHAR_TOK
  3481. {
  3482. $$ = new CPPExpression($1);
  3483. }
  3484. | REAL
  3485. {
  3486. $$ = new CPPExpression($1);
  3487. }
  3488. | string_literal
  3489. {
  3490. $$ = $1;
  3491. }
  3492. | CUSTOM_LITERAL
  3493. {
  3494. $$ = $1;
  3495. }
  3496. | IDENTIFIER
  3497. {
  3498. $$ = new CPPExpression($1, current_scope, global_scope, current_lexer);
  3499. }
  3500. | KW_FINAL
  3501. {
  3502. // A variable named "final". C++11 explicitly permits this.
  3503. CPPIdentifier *ident = new CPPIdentifier("final", @1);
  3504. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3505. }
  3506. | KW_OVERRIDE
  3507. {
  3508. // A variable named "override". C++11 explicitly permits this.
  3509. CPPIdentifier *ident = new CPPIdentifier("override", @1);
  3510. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3511. }
  3512. | KW_NULLPTR
  3513. {
  3514. $$ = new CPPExpression(CPPExpression::get_nullptr());
  3515. }
  3516. | '[' capture_list ']' function_post maybe_trailing_return_type '{' code '}'
  3517. {
  3518. $2->_flags = $4;
  3519. $2->_return_type = $5;
  3520. $$ = new CPPExpression(CPPExpression::lambda($2));
  3521. }
  3522. | '[' capture_list ']' '(' function_parameter_list ')' function_post maybe_trailing_return_type '{' code '}'
  3523. {
  3524. $2->_parameters = $5;
  3525. $2->_flags = $7;
  3526. $2->_return_type = $8;
  3527. $$ = new CPPExpression(CPPExpression::lambda($2));
  3528. }
  3529. | KW_HAS_VIRTUAL_DESTRUCTOR '(' full_type ')'
  3530. {
  3531. $$ = new CPPExpression(CPPExpression::type_trait(KW_HAS_VIRTUAL_DESTRUCTOR, $3));
  3532. }
  3533. | KW_IS_ABSTRACT '(' full_type ')'
  3534. {
  3535. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_ABSTRACT, $3));
  3536. }
  3537. | KW_IS_BASE_OF '(' full_type ',' full_type ')'
  3538. {
  3539. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CLASS, $3, $5));
  3540. }
  3541. | KW_IS_CLASS '(' full_type ')'
  3542. {
  3543. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CLASS, $3));
  3544. }
  3545. | KW_IS_CONSTRUCTIBLE '(' full_type ')'
  3546. {
  3547. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONSTRUCTIBLE, $3));
  3548. }
  3549. | KW_IS_CONSTRUCTIBLE '(' full_type ',' full_type ')'
  3550. {
  3551. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONSTRUCTIBLE, $3, $5));
  3552. }
  3553. | KW_IS_CONVERTIBLE_TO '(' full_type ',' full_type ')'
  3554. {
  3555. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONVERTIBLE_TO, $3, $5));
  3556. }
  3557. | KW_IS_DESTRUCTIBLE '(' full_type ')'
  3558. {
  3559. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_DESTRUCTIBLE, $3));
  3560. }
  3561. | KW_IS_EMPTY '(' full_type ')'
  3562. {
  3563. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_EMPTY, $3));
  3564. }
  3565. | KW_IS_ENUM '(' full_type ')'
  3566. {
  3567. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_ENUM, $3));
  3568. }
  3569. | KW_IS_FINAL '(' full_type ')'
  3570. {
  3571. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_FINAL, $3));
  3572. }
  3573. | KW_IS_FUNDAMENTAL '(' full_type ')'
  3574. {
  3575. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_FUNDAMENTAL, $3));
  3576. }
  3577. | KW_IS_POD '(' full_type ')'
  3578. {
  3579. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_POD, $3));
  3580. }
  3581. | KW_IS_POLYMORPHIC '(' full_type ')'
  3582. {
  3583. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_POLYMORPHIC, $3));
  3584. }
  3585. | KW_IS_STANDARD_LAYOUT '(' full_type ')'
  3586. {
  3587. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_STANDARD_LAYOUT, $3));
  3588. }
  3589. | KW_IS_TRIVIAL '(' full_type ')'
  3590. {
  3591. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_TRIVIAL, $3));
  3592. }
  3593. | KW_IS_UNION '(' full_type ')'
  3594. {
  3595. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_UNION, $3));
  3596. }
  3597. ;
  3598. /* This is used for a const_expr as a "formal parameter", which really
  3599. means an instance declaration using a parameter list (which looks a
  3600. lot like a function prototype). It differs from const_expr mainly
  3601. in that it forbids some expressions unless they are parenthesized,
  3602. to avoid shift/reduce conflicts with the actual formal parameter
  3603. definition. */
  3604. formal_const_expr:
  3605. formal_const_operand
  3606. {
  3607. $$ = $1;
  3608. }
  3609. | '(' full_type ')' const_expr %prec UNARY
  3610. {
  3611. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3612. }
  3613. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3614. {
  3615. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3616. }
  3617. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3618. {
  3619. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3620. }
  3621. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3622. {
  3623. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3624. }
  3625. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3626. {
  3627. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3628. }
  3629. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3630. {
  3631. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3632. }
  3633. | KW_SIZEOF '(' IDENTIFIER ')' %prec UNARY
  3634. {
  3635. CPPDeclaration *arg = $3->find_symbol(current_scope, global_scope, current_lexer);
  3636. if (arg == nullptr) {
  3637. yyerror("undefined sizeof argument: " + $3->get_fully_scoped_name(), @3);
  3638. } else if (arg->get_subtype() == CPPDeclaration::ST_instance) {
  3639. CPPInstance *inst = arg->as_instance();
  3640. $$ = new CPPExpression(CPPExpression::sizeof_func(inst->_type));
  3641. } else {
  3642. $$ = new CPPExpression(CPPExpression::sizeof_func(arg->as_type()));
  3643. }
  3644. }
  3645. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3646. {
  3647. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3648. }
  3649. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3650. {
  3651. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3652. }
  3653. | KW_NEW predefined_type %prec UNARY
  3654. {
  3655. $$ = new CPPExpression(CPPExpression::new_op($2));
  3656. }
  3657. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  3658. {
  3659. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  3660. }
  3661. | KW_TYPEID '(' full_type ')'
  3662. {
  3663. CPPIdentifier ident("");
  3664. ident.add_name("std");
  3665. ident.add_name("type_info");
  3666. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3667. if (!std_type_info) {
  3668. yywarning("cannot use typeid before including <typeinfo>", @1);
  3669. }
  3670. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3671. }
  3672. | KW_TYPEID '(' const_expr ')'
  3673. {
  3674. CPPIdentifier ident("");
  3675. ident.add_name("std");
  3676. ident.add_name("type_info");
  3677. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3678. if (!std_type_info) {
  3679. yywarning("cannot use typeid before including <typeinfo>", @1);
  3680. }
  3681. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3682. }
  3683. | '!' const_expr %prec UNARY
  3684. {
  3685. $$ = new CPPExpression(UNARY_NOT, $2);
  3686. }
  3687. | '~' const_expr %prec UNARY
  3688. {
  3689. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3690. }
  3691. | '-' const_expr %prec UNARY
  3692. {
  3693. $$ = new CPPExpression(UNARY_MINUS, $2);
  3694. }
  3695. | '+' const_expr %prec UNARY
  3696. {
  3697. $$ = new CPPExpression(UNARY_PLUS, $2);
  3698. }
  3699. | '&' const_expr %prec UNARY
  3700. {
  3701. $$ = new CPPExpression(UNARY_REF, $2);
  3702. }
  3703. | formal_const_expr '*' const_expr
  3704. {
  3705. $$ = new CPPExpression('*', $1, $3);
  3706. }
  3707. | formal_const_expr '/' const_expr
  3708. {
  3709. $$ = new CPPExpression('/', $1, $3);
  3710. }
  3711. | formal_const_expr '%' const_expr
  3712. {
  3713. $$ = new CPPExpression('%', $1, $3);
  3714. }
  3715. | formal_const_expr '+' const_expr
  3716. {
  3717. $$ = new CPPExpression('+', $1, $3);
  3718. }
  3719. | formal_const_expr '-' const_expr
  3720. {
  3721. $$ = new CPPExpression('-', $1, $3);
  3722. }
  3723. | formal_const_expr '|' const_expr
  3724. {
  3725. $$ = new CPPExpression('|', $1, $3);
  3726. }
  3727. | formal_const_expr '^' const_expr
  3728. {
  3729. $$ = new CPPExpression('^', $1, $3);
  3730. }
  3731. | formal_const_expr '&' const_expr
  3732. {
  3733. $$ = new CPPExpression('&', $1, $3);
  3734. }
  3735. | formal_const_expr OROR const_expr
  3736. {
  3737. $$ = new CPPExpression(OROR, $1, $3);
  3738. }
  3739. | formal_const_expr ANDAND const_expr
  3740. {
  3741. $$ = new CPPExpression(ANDAND, $1, $3);
  3742. }
  3743. | formal_const_expr EQCOMPARE const_expr
  3744. {
  3745. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3746. }
  3747. | formal_const_expr NECOMPARE const_expr
  3748. {
  3749. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3750. }
  3751. | formal_const_expr LECOMPARE const_expr
  3752. {
  3753. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3754. }
  3755. | formal_const_expr GECOMPARE const_expr
  3756. {
  3757. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3758. }
  3759. | formal_const_expr '<' const_expr
  3760. {
  3761. $$ = new CPPExpression('<', $1, $3);
  3762. }
  3763. | formal_const_expr '>' const_expr
  3764. {
  3765. $$ = new CPPExpression('>', $1, $3);
  3766. }
  3767. | formal_const_expr LSHIFT const_expr
  3768. {
  3769. $$ = new CPPExpression(LSHIFT, $1, $3);
  3770. }
  3771. | formal_const_expr RSHIFT const_expr
  3772. {
  3773. $$ = new CPPExpression(RSHIFT, $1, $3);
  3774. }
  3775. | formal_const_expr '?' const_expr ':' const_expr
  3776. {
  3777. $$ = new CPPExpression('?', $1, $3, $5);
  3778. }
  3779. | formal_const_expr '[' const_expr ']'
  3780. {
  3781. $$ = new CPPExpression('[', $1, $3);
  3782. }
  3783. | formal_const_expr '(' const_expr_comma ')'
  3784. {
  3785. $$ = new CPPExpression('f', $1, $3);
  3786. }
  3787. | formal_const_expr '(' ')'
  3788. {
  3789. $$ = new CPPExpression('f', $1);
  3790. }
  3791. | formal_const_expr '.' const_expr
  3792. {
  3793. $$ = new CPPExpression('.', $1, $3);
  3794. }
  3795. | formal_const_expr POINTSAT const_expr
  3796. {
  3797. $$ = new CPPExpression(POINTSAT, $1, $3);
  3798. }
  3799. | '(' const_expr_comma ')'
  3800. {
  3801. $$ = $2;
  3802. }
  3803. ;
  3804. formal_const_operand:
  3805. INTEGER
  3806. {
  3807. $$ = new CPPExpression($1);
  3808. }
  3809. | KW_TRUE
  3810. {
  3811. $$ = new CPPExpression(true);
  3812. }
  3813. | KW_FALSE
  3814. {
  3815. $$ = new CPPExpression(false);
  3816. }
  3817. | CHAR_TOK
  3818. {
  3819. $$ = new CPPExpression($1);
  3820. }
  3821. | REAL
  3822. {
  3823. $$ = new CPPExpression($1);
  3824. }
  3825. | string_literal
  3826. {
  3827. $$ = $1;
  3828. }
  3829. | CUSTOM_LITERAL
  3830. {
  3831. $$ = $1;
  3832. }
  3833. | IDENTIFIER
  3834. {
  3835. $$ = new CPPExpression($1, current_scope, global_scope, current_lexer);
  3836. }
  3837. | KW_FINAL
  3838. {
  3839. // A variable named "final". C++11 explicitly permits this.
  3840. CPPIdentifier *ident = new CPPIdentifier("final", @1);
  3841. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3842. }
  3843. | KW_OVERRIDE
  3844. {
  3845. // A variable named "override". C++11 explicitly permits this.
  3846. CPPIdentifier *ident = new CPPIdentifier("override", @1);
  3847. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3848. }
  3849. | KW_NULLPTR
  3850. {
  3851. $$ = new CPPExpression(CPPExpression::get_nullptr());
  3852. }
  3853. ;
  3854. /* The contents of the [] list preceding a lambda expression. */
  3855. capture_list:
  3856. empty
  3857. {
  3858. $$ = new CPPClosureType();
  3859. }
  3860. | '='
  3861. {
  3862. $$ = new CPPClosureType(CPPClosureType::CT_by_value);
  3863. }
  3864. | '&'
  3865. {
  3866. $$ = new CPPClosureType(CPPClosureType::CT_by_reference);
  3867. }
  3868. | capture maybe_initialize
  3869. {
  3870. $$ = new CPPClosureType();
  3871. $1->_initializer = $2;
  3872. $$->_captures.push_back(*$1);
  3873. delete $1;
  3874. }
  3875. | capture_list ',' capture maybe_initialize
  3876. {
  3877. $$ = $1;
  3878. $3->_initializer = $4;
  3879. $$->_captures.push_back(*$3);
  3880. delete $3;
  3881. }
  3882. ;
  3883. capture:
  3884. '&' name
  3885. {
  3886. $$ = new CPPClosureType::Capture;
  3887. $$->_name = $2->get_simple_name();
  3888. $$->_type = CPPClosureType::CT_by_reference;
  3889. }
  3890. | '&' name ELLIPSIS
  3891. {
  3892. $$ = new CPPClosureType::Capture;
  3893. $$->_name = $2->get_simple_name();
  3894. $$->_type = CPPClosureType::CT_by_reference;
  3895. }
  3896. | name
  3897. {
  3898. $$ = new CPPClosureType::Capture;
  3899. $$->_name = $1->get_simple_name();
  3900. if ($$->_name == "this") {
  3901. $$->_type = CPPClosureType::CT_by_reference;
  3902. } else {
  3903. $$->_type = CPPClosureType::CT_by_value;
  3904. }
  3905. }
  3906. | '*' name
  3907. {
  3908. $$ = new CPPClosureType::Capture;
  3909. $$->_name = $2->get_simple_name();
  3910. $$->_type = CPPClosureType::CT_by_value;
  3911. if ($$->_name != "this") {
  3912. yywarning("only capture name 'this' may be preceded by an asterisk", @2);
  3913. }
  3914. }
  3915. ;
  3916. class_derivation_name:
  3917. name
  3918. {
  3919. CPPType *type = $1->find_type(current_scope, global_scope, true);
  3920. if (type == nullptr) {
  3921. type = CPPType::new_type(new CPPTBDType($1));
  3922. }
  3923. $$ = type;
  3924. }
  3925. | KW_TYPENAME name
  3926. {
  3927. $$ = CPPType::new_type(new CPPTBDType($2));
  3928. }
  3929. | name ELLIPSIS
  3930. {
  3931. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter($1);
  3932. ctp->_packed = true;
  3933. $$ = CPPType::new_type(ctp);
  3934. }
  3935. ;
  3936. /*
  3937. typedefname:
  3938. TYPENAME_IDENTIFIER
  3939. {
  3940. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3941. if (type == NULL) {
  3942. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3943. }
  3944. assert(type != NULL);
  3945. $$ = type;
  3946. }
  3947. | KW_TYPENAME name
  3948. {
  3949. $$ = CPPType::new_type(new CPPTBDType($2));
  3950. }
  3951. ;
  3952. */
  3953. /* Note that we should also accept "final" and "override" as valid names,
  3954. * according to C++11. */
  3955. name:
  3956. IDENTIFIER
  3957. {
  3958. $$ = $1;
  3959. }
  3960. | TYPENAME_IDENTIFIER
  3961. {
  3962. $$ = $1;
  3963. }
  3964. | TYPEPACK_IDENTIFIER
  3965. {
  3966. $$ = $1;
  3967. }
  3968. | KW_FINAL
  3969. {
  3970. $$ = new CPPIdentifier("final", @1);
  3971. }
  3972. | KW_OVERRIDE
  3973. {
  3974. $$ = new CPPIdentifier("override", @1);
  3975. }
  3976. | KW_SIGNED
  3977. {
  3978. // This is not a keyword in Python, so it is useful to be able to use this
  3979. // in MAKE_PROPERTY definitions, etc.
  3980. $$ = new CPPIdentifier("signed", @1);
  3981. }
  3982. | KW_FLOAT
  3983. {
  3984. $$ = new CPPIdentifier("float", @1);
  3985. }
  3986. | KW_PUBLIC
  3987. {
  3988. $$ = new CPPIdentifier("public", @1);
  3989. }
  3990. | KW_PRIVATE
  3991. {
  3992. $$ = new CPPIdentifier("private", @1);
  3993. }
  3994. | KW_STATIC
  3995. {
  3996. $$ = new CPPIdentifier("static", @1);
  3997. }
  3998. | KW_DEFAULT
  3999. {
  4000. $$ = new CPPIdentifier("default", @1);
  4001. }
  4002. ;
  4003. /* A variant on name that's used for structs, where we disallow final
  4004. * to disambiguate the case of struct A final {};
  4005. */
  4006. name_no_final:
  4007. IDENTIFIER
  4008. {
  4009. $$ = $1;
  4010. }
  4011. | TYPENAME_IDENTIFIER
  4012. {
  4013. $$ = $1;
  4014. }
  4015. | TYPEPACK_IDENTIFIER
  4016. {
  4017. $$ = $1;
  4018. }
  4019. | KW_OVERRIDE
  4020. {
  4021. $$ = new CPPIdentifier("override", @1);
  4022. }
  4023. ;
  4024. string_literal:
  4025. SIMPLE_STRING
  4026. {
  4027. $$ = new CPPExpression($1);
  4028. }
  4029. | STRING_LITERAL
  4030. {
  4031. $$ = $1;
  4032. }
  4033. | string_literal SIMPLE_STRING
  4034. {
  4035. // The right string takes on the literal type of the left.
  4036. $$ = $1;
  4037. $$->_str += $2;
  4038. }
  4039. | string_literal STRING_LITERAL
  4040. {
  4041. // We have to check that the two literal types match up.
  4042. $$ = $1;
  4043. if ($2->_type != CPPExpression::T_string && $2->_type != $1->_type) {
  4044. yywarning("cannot concatenate two string literals of different types", @$);
  4045. }
  4046. $$->_str += $2->_str;
  4047. }
  4048. ;
  4049. empty:
  4050. ;