cppBison.yxx 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301
  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. optional_attributes:
  931. empty
  932. | ATTR_LEFT attribute_specifiers ATTR_RIGHT optional_attributes
  933. | KW_ALIGNAS '(' const_expr ')' optional_attributes
  934. | KW_ALIGNAS '(' type_decl ')' optional_attributes
  935. ;
  936. attribute_specifiers:
  937. attribute_specifier
  938. | attribute_specifier ',' attribute_specifiers
  939. ;
  940. attribute_specifier:
  941. name
  942. | name '(' formal_parameter_list ')'
  943. | KW_USING name ':' attribute_specifier
  944. ;
  945. type_like_declaration:
  946. storage_class var_type_decl
  947. {
  948. // We don't need to push/pop type, because we can't nest
  949. // type_like_declaration.
  950. if ($2->as_type_declaration()) {
  951. current_type = $2->as_type_declaration()->_type;
  952. } else {
  953. current_type = $2->as_type();
  954. }
  955. push_storage_class($1);
  956. }
  957. multiple_instance_identifiers
  958. {
  959. pop_storage_class();
  960. }
  961. | storage_class type_decl ';'
  962. {
  963. // We don't really care about the storage class here. In fact, it's
  964. // not actually legal to define a class or struct using a particular
  965. // storage class, but we require it just to help yacc out in its
  966. // parsing.
  967. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  968. }
  969. | storage_class constructor_prototype
  970. {
  971. if ($2 != nullptr) {
  972. // Push the scope so that the initializers can make use of things defined
  973. // in the class body.
  974. push_scope($2->get_scope(current_scope, global_scope));
  975. $2->_storage_class |= (current_storage_class | $1);
  976. }
  977. }
  978. maybe_initialize_or_constructor_body
  979. {
  980. if ($2 != nullptr) {
  981. pop_scope();
  982. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  983. $2->set_initializer($4);
  984. }
  985. }
  986. | storage_class function_prototype maybe_initialize_or_function_body
  987. {
  988. if ($2 != nullptr) {
  989. $2->_storage_class |= (current_storage_class | $1);
  990. current_scope->add_declaration($2, global_scope, current_lexer, @2);
  991. $2->set_initializer($3);
  992. }
  993. }
  994. | using_declaration
  995. /* We don't need to include a rule for variables that point to
  996. functions, because we get those from the function_prototype
  997. definition. */
  998. ;
  999. multiple_instance_identifiers:
  1000. instance_identifier_and_maybe_trailing_return_type maybe_initialize_or_function_body
  1001. {
  1002. if (current_storage_class & CPPInstance::SC_const) {
  1003. $1->add_modifier(IIT_const);
  1004. }
  1005. CPPInstance *inst = new CPPInstance(current_type, $1,
  1006. current_storage_class,
  1007. @1.file);
  1008. inst->set_initializer($2);
  1009. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  1010. }
  1011. | instance_identifier_and_maybe_trailing_return_type maybe_initialize ',' multiple_instance_identifiers
  1012. {
  1013. if (current_storage_class & CPPInstance::SC_const) {
  1014. $1->add_modifier(IIT_const);
  1015. }
  1016. CPPInstance *inst = new CPPInstance(current_type, $1,
  1017. current_storage_class,
  1018. @1.file);
  1019. inst->set_initializer($2);
  1020. current_scope->add_declaration(inst, global_scope, current_lexer, @1);
  1021. }
  1022. ;
  1023. typedef_declaration:
  1024. storage_class var_type_decl
  1025. {
  1026. // We don't need to push/pop type, because we can't nest
  1027. // multiple_var_declarations.
  1028. if ($2->as_type_declaration()) {
  1029. current_type = $2->as_type_declaration()->_type;
  1030. } else {
  1031. current_type = $2->as_type();
  1032. }
  1033. push_storage_class($1);
  1034. }
  1035. typedef_instance_identifiers
  1036. {
  1037. pop_storage_class();
  1038. }
  1039. | storage_class function_prototype maybe_initialize_or_function_body
  1040. {
  1041. if ($2 != nullptr) {
  1042. CPPInstance *inst = $2->as_instance();
  1043. if (inst != nullptr) {
  1044. inst->_storage_class |= (current_storage_class | $1);
  1045. current_scope->add_declaration(inst, global_scope, current_lexer, @2);
  1046. CPPTypedefType *typedef_type = new CPPTypedefType(inst->_type, inst->_ident, current_scope);
  1047. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @2);
  1048. }
  1049. }
  1050. }
  1051. ;
  1052. typedef_instance_identifiers:
  1053. instance_identifier_and_maybe_trailing_return_type maybe_initialize_or_function_body
  1054. {
  1055. if (current_storage_class & CPPInstance::SC_const) {
  1056. $1->add_modifier(IIT_const);
  1057. }
  1058. CPPType *target_type = current_type;
  1059. CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
  1060. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  1061. }
  1062. | instance_identifier_and_maybe_trailing_return_type maybe_initialize ',' typedef_instance_identifiers
  1063. {
  1064. if (current_storage_class & CPPInstance::SC_const) {
  1065. $1->add_modifier(IIT_const);
  1066. }
  1067. CPPType *target_type = current_type;
  1068. CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
  1069. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  1070. }
  1071. ;
  1072. constructor_prototype:
  1073. /* Functions with implicit return types, and constructors */
  1074. IDENTIFIER '('
  1075. {
  1076. // Create a scope for this function.
  1077. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1078. $1->_names.back(), V_private);
  1079. // It still needs to be able to pick up any template arguments, if this is
  1080. // a definition for a method template. Add a fake "using" declaration to
  1081. // accomplish this.
  1082. scope->_using.insert(current_scope);
  1083. push_scope(scope);
  1084. }
  1085. function_parameter_list ')' function_post
  1086. {
  1087. CPPScope *scope = $1->get_scope(current_scope, global_scope);
  1088. CPPType *type;
  1089. std::string simple_name = $1->get_simple_name();
  1090. if (!simple_name.empty() && simple_name[0] == '~') {
  1091. // A destructor has no return type.
  1092. type = new CPPSimpleType(CPPSimpleType::T_void);
  1093. } else if (scope != nullptr && simple_name == scope->get_simple_name()) {
  1094. // Neither does a constructor.
  1095. type = new CPPSimpleType(CPPSimpleType::T_void);
  1096. } else {
  1097. // This isn't a constructor, so it has an implicit return type of
  1098. // int.
  1099. yywarning("function has no return type, assuming int", @1);
  1100. type = new CPPSimpleType(CPPSimpleType::T_int);
  1101. }
  1102. pop_scope();
  1103. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  1104. ii->add_func_modifier($4, $6);
  1105. $$ = new CPPInstance(type, ii, 0, @1.file);
  1106. }
  1107. /* This is a hack to support functions with the identifier enveloped by a
  1108. pair of parentheses. */
  1109. | TYPENAME_IDENTIFIER '(' IDENTIFIER ')' '('
  1110. {
  1111. // Create a scope for this function.
  1112. CPPScope *scope = new CPPScope($3->get_scope(current_scope, global_scope),
  1113. $3->_names.back(), V_private);
  1114. // It still needs to be able to pick up any template arguments, if this is
  1115. // a definition for a method template. Add a fake "using" declaration to
  1116. // accomplish this.
  1117. scope->_using.insert(current_scope);
  1118. push_scope(scope);
  1119. }
  1120. function_parameter_list ')' function_post
  1121. {
  1122. pop_scope();
  1123. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  1124. if (type == nullptr) {
  1125. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1126. }
  1127. assert(type != nullptr);
  1128. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($3);
  1129. ii->add_func_modifier($7, $9);
  1130. $$ = new CPPInstance(type, ii, 0, @1.file);
  1131. }
  1132. | TYPENAME_IDENTIFIER '('
  1133. {
  1134. // Create a scope for this function.
  1135. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1136. $1->_names.back(), V_private);
  1137. // It still needs to be able to pick up any template arguments, if this is
  1138. // a definition for a method template. Add a fake "using" declaration to
  1139. // accomplish this.
  1140. scope->_using.insert(current_scope);
  1141. push_scope(scope);
  1142. }
  1143. function_parameter_list ')' function_post
  1144. {
  1145. pop_scope();
  1146. CPPType *type;
  1147. if ($1->get_simple_name() == current_scope->get_simple_name()) {
  1148. // This is a constructor, and has no return.
  1149. type = new CPPSimpleType(CPPSimpleType::T_void);
  1150. } else {
  1151. // This isn't a constructor, so it has an implicit return type of
  1152. // int.
  1153. type = new CPPSimpleType(CPPSimpleType::T_int);
  1154. }
  1155. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier($1);
  1156. ii->add_func_modifier($4, $6);
  1157. $$ = new CPPInstance(type, ii, 0, @1.file);
  1158. }
  1159. ;
  1160. function_prototype:
  1161. /* Destructors */
  1162. '~' name '('
  1163. {
  1164. push_scope($2->get_scope(current_scope, global_scope));
  1165. }
  1166. function_parameter_list ')' function_post
  1167. {
  1168. pop_scope();
  1169. if ($2->is_scoped()) {
  1170. yyerror("Invalid destructor name: ~" + $2->get_fully_scoped_name(), @2);
  1171. } else {
  1172. CPPIdentifier *ident =
  1173. new CPPIdentifier("~" + $2->get_simple_name(), @2);
  1174. delete $2;
  1175. CPPType *type;
  1176. type = new CPPSimpleType(CPPSimpleType::T_void);
  1177. CPPInstanceIdentifier *ii = new CPPInstanceIdentifier(ident);
  1178. ii->add_func_modifier($5, $7);
  1179. $$ = new CPPInstance(type, ii, 0, @2.file);
  1180. }
  1181. }
  1182. /* This is a special case: a function pointer declaration that looks
  1183. at first a lot like a constructor declaration. This is provided to
  1184. help yacc sort out the differences. It isn't an ideal solution,
  1185. because it doesn't catch a lot of subtle variants on this form--but
  1186. this will get at least the 99% most common uses. */
  1187. | TYPENAME_IDENTIFIER '(' '*' instance_identifier ')' '('
  1188. {
  1189. push_scope($4->get_scope(current_scope, global_scope));
  1190. }
  1191. function_parameter_list ')' function_post maybe_trailing_return_type
  1192. {
  1193. pop_scope();
  1194. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  1195. if (type == nullptr) {
  1196. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1197. }
  1198. assert(type != nullptr);
  1199. CPPInstanceIdentifier *ii = $4;
  1200. ii->add_modifier(IIT_pointer);
  1201. ii->add_func_modifier($8, $10);
  1202. $$ = new CPPInstance(type, ii, 0, @1.file);
  1203. }
  1204. | TYPENAME_IDENTIFIER '(' SCOPING '*' instance_identifier ')' '('
  1205. {
  1206. push_scope($5->get_scope(current_scope, global_scope));
  1207. }
  1208. function_parameter_list ')' function_post maybe_trailing_return_type
  1209. {
  1210. pop_scope();
  1211. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  1212. if (type == nullptr) {
  1213. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1214. }
  1215. assert(type != nullptr);
  1216. CPPInstanceIdentifier *ii = $5;
  1217. ii->add_scoped_pointer_modifier($3);
  1218. ii->add_func_modifier($9, $11);
  1219. $$ = new CPPInstance(type, ii, 0, @1.file);
  1220. }
  1221. /* Typecast operators */
  1222. | KW_OPERATOR type not_paren_formal_parameter_identifier '('
  1223. {
  1224. if ($1 != nullptr) {
  1225. push_scope($1->get_scope(current_scope, global_scope));
  1226. }
  1227. }
  1228. function_parameter_list ')' function_post
  1229. {
  1230. if ($1 != nullptr) {
  1231. pop_scope();
  1232. }
  1233. // We use formal_parameter_identifier, because that can match a type
  1234. // name with or without an identifier, but especially without, which
  1235. // is what follows the keyword "operator" in a typecast function.
  1236. // As an added bonus, the type of the formal_parameter will be the
  1237. // typecast type, i.e. the return type of the typecast function.
  1238. // We give typecast operators the name "operator typecast <name>",
  1239. // where <name> is a simple name of the type to be typecast. Use
  1240. // the method's return type to determine the full type description.
  1241. string name = "operator typecast " + $2->get_simple_name();
  1242. CPPIdentifier *ident = $1;
  1243. if (ident == nullptr) {
  1244. ident = new CPPIdentifier(name, @2);
  1245. } else {
  1246. ident->add_name(name);
  1247. }
  1248. $$ = CPPInstance::make_typecast_function
  1249. (new CPPInstance($2, $3, 0, @3.file), ident, $6, $8);
  1250. }
  1251. | KW_OPERATOR KW_CONST type not_paren_formal_parameter_identifier '('
  1252. {
  1253. if ($1 != nullptr) {
  1254. push_scope($1->get_scope(current_scope, global_scope));
  1255. }
  1256. }
  1257. function_parameter_list ')' function_post
  1258. {
  1259. if ($1 != nullptr) {
  1260. pop_scope();
  1261. }
  1262. CPPIdentifier *ident = $1;
  1263. if (ident == nullptr) {
  1264. ident = new CPPIdentifier("operator typecast", @4);
  1265. } else {
  1266. ident->add_name("operator typecast");
  1267. }
  1268. $4->add_modifier(IIT_const);
  1269. $$ = CPPInstance::make_typecast_function
  1270. (new CPPInstance($3, $4, 0, @4.file), ident, $7, $9);
  1271. }
  1272. /* Not actually a function prototype, but maybe a template
  1273. instantiation. Just included here (instead of somewhere else) to
  1274. avoid shift/reduce conflicts. */
  1275. | IDENTIFIER
  1276. {
  1277. CPPDeclaration *decl =
  1278. $1->find_symbol(current_scope, global_scope, current_lexer);
  1279. if (decl != nullptr) {
  1280. $$ = decl->as_instance();
  1281. } else {
  1282. $$ = nullptr;
  1283. }
  1284. }
  1285. ;
  1286. function_post:
  1287. empty
  1288. {
  1289. $$ = 0;
  1290. }
  1291. | function_post KW_CONST
  1292. {
  1293. $$ = $1 | (int)CPPFunctionType::F_const_method;
  1294. }
  1295. | function_post KW_VOLATILE
  1296. {
  1297. $$ = $1 | (int)CPPFunctionType::F_volatile_method;
  1298. }
  1299. | function_post KW_NOEXCEPT
  1300. {
  1301. $$ = $1 | (int)CPPFunctionType::F_noexcept;
  1302. }
  1303. /* | function_post KW_NOEXCEPT '(' const_expr ')'
  1304. {
  1305. CPPExpression::Result result = $4->evaluate();
  1306. if (result._type == CPPExpression::RT_error) {
  1307. yywarning("noexcept requires a constant expression", @4);
  1308. } else if (result.as_boolean()) {
  1309. $$ = $1 | (int)CPPFunctionType::F_noexcept;
  1310. }
  1311. }*/
  1312. | function_post KW_FINAL
  1313. {
  1314. $$ = $1 | (int)CPPFunctionType::F_final;
  1315. }
  1316. | function_post KW_OVERRIDE
  1317. {
  1318. $$ = $1 | (int)CPPFunctionType::F_override;
  1319. }
  1320. | function_post '&'
  1321. {
  1322. $$ = $1 | (int)CPPFunctionType::F_lvalue_method;
  1323. }
  1324. | function_post ANDAND
  1325. {
  1326. $$ = $1 | (int)CPPFunctionType::F_rvalue_method;
  1327. }
  1328. | function_post KW_MUTABLE
  1329. {
  1330. // Used for lambdas, currently ignored.
  1331. $$ = $1;
  1332. }
  1333. | function_post KW_CONSTEXPR
  1334. {
  1335. // Used for lambdas in C++17, currently ignored.
  1336. $$ = $1;
  1337. }
  1338. | function_post KW_THROW '(' ')'
  1339. {
  1340. $$ = $1;
  1341. }
  1342. | function_post KW_THROW '(' name ')'
  1343. {
  1344. $$ = $1;
  1345. }
  1346. | function_post KW_THROW '(' name ELLIPSIS ')'
  1347. {
  1348. $$ = $1;
  1349. }
  1350. | function_post ATTR_LEFT attribute_specifiers ATTR_RIGHT
  1351. {
  1352. $$ = $1;
  1353. }
  1354. ;
  1355. function_operator:
  1356. '!'
  1357. {
  1358. $$ = "!";
  1359. }
  1360. | '~'
  1361. {
  1362. $$ = "~";
  1363. }
  1364. | '*'
  1365. {
  1366. $$ = "*";
  1367. }
  1368. | '/'
  1369. {
  1370. $$ = "/";
  1371. }
  1372. | '%'
  1373. {
  1374. $$ = "%";
  1375. }
  1376. | '+'
  1377. {
  1378. $$ = "+";
  1379. }
  1380. | '-'
  1381. {
  1382. $$ = "-";
  1383. }
  1384. | '|'
  1385. {
  1386. $$ = "|";
  1387. }
  1388. | '&'
  1389. {
  1390. $$ = "&";
  1391. }
  1392. | '^'
  1393. {
  1394. $$ = "^";
  1395. }
  1396. | OROR
  1397. {
  1398. $$ = "||";
  1399. }
  1400. | ANDAND
  1401. {
  1402. $$ = "&&";
  1403. }
  1404. | EQCOMPARE
  1405. {
  1406. $$ = "==";
  1407. }
  1408. | NECOMPARE
  1409. {
  1410. $$ = "!=";
  1411. }
  1412. | LECOMPARE
  1413. {
  1414. $$ = "<=";
  1415. }
  1416. | GECOMPARE
  1417. {
  1418. $$ = ">=";
  1419. }
  1420. | '<'
  1421. {
  1422. $$ = "<";
  1423. }
  1424. | '>'
  1425. {
  1426. $$ = ">";
  1427. }
  1428. | LSHIFT
  1429. {
  1430. $$ = "<<";
  1431. }
  1432. | RSHIFT
  1433. {
  1434. $$ = ">>";
  1435. }
  1436. | '='
  1437. {
  1438. $$ = "=";
  1439. }
  1440. | ','
  1441. {
  1442. $$ = ",";
  1443. }
  1444. | PLUSPLUS
  1445. {
  1446. $$ = "++";
  1447. }
  1448. | MINUSMINUS
  1449. {
  1450. $$ = "--";
  1451. }
  1452. | TIMESEQUAL
  1453. {
  1454. $$ = "*=";
  1455. }
  1456. | DIVIDEEQUAL
  1457. {
  1458. $$ = "/=";
  1459. }
  1460. | MODEQUAL
  1461. {
  1462. $$ = "%=";
  1463. }
  1464. | PLUSEQUAL
  1465. {
  1466. $$ = "+=";
  1467. }
  1468. | MINUSEQUAL
  1469. {
  1470. $$ = "-=";
  1471. }
  1472. | OREQUAL
  1473. {
  1474. $$ = "|=";
  1475. }
  1476. | ANDEQUAL
  1477. {
  1478. $$ = "&=";
  1479. }
  1480. | XOREQUAL
  1481. {
  1482. $$ = "^=";
  1483. }
  1484. | LSHIFTEQUAL
  1485. {
  1486. $$ = "<<=";
  1487. }
  1488. | RSHIFTEQUAL
  1489. {
  1490. $$ = ">>=";
  1491. }
  1492. | POINTSAT
  1493. {
  1494. $$ = "->";
  1495. }
  1496. | '[' ']'
  1497. {
  1498. $$ = "[]";
  1499. }
  1500. | '(' ')'
  1501. {
  1502. $$ = "()";
  1503. }
  1504. | KW_NEW
  1505. {
  1506. $$ = "new";
  1507. }
  1508. | KW_DELETE
  1509. {
  1510. $$ = "delete";
  1511. }
  1512. ;
  1513. more_template_declaration:
  1514. type_like_declaration
  1515. | template_declaration
  1516. | friend_declaration
  1517. ;
  1518. template_declaration:
  1519. KW_EXTERN template_declaration
  1520. | KW_TEMPLATE
  1521. {
  1522. push_scope(new CPPTemplateScope(current_scope));
  1523. }
  1524. '<' template_formal_parameters '>' more_template_declaration
  1525. {
  1526. pop_scope();
  1527. }
  1528. | KW_TEMPLATE type_like_declaration
  1529. | KW_TEMPLATE friend_declaration
  1530. ;
  1531. template_formal_parameters:
  1532. empty
  1533. | template_nonempty_formal_parameters
  1534. ;
  1535. template_nonempty_formal_parameters:
  1536. template_formal_parameter
  1537. {
  1538. CPPTemplateScope *ts = current_scope->as_template_scope();
  1539. assert(ts != nullptr);
  1540. ts->add_template_parameter($1);
  1541. }
  1542. | template_nonempty_formal_parameters ',' template_formal_parameter
  1543. {
  1544. CPPTemplateScope *ts = current_scope->as_template_scope();
  1545. assert(ts != nullptr);
  1546. ts->add_template_parameter($3);
  1547. }
  1548. ;
  1549. typename_keyword:
  1550. KW_CLASS
  1551. | KW_TYPENAME
  1552. ;
  1553. template_formal_parameter:
  1554. typename_keyword
  1555. {
  1556. $$ = CPPType::new_type(new CPPClassTemplateParameter(nullptr));
  1557. }
  1558. | typename_keyword name
  1559. {
  1560. $$ = CPPType::new_type(new CPPClassTemplateParameter($2));
  1561. }
  1562. | typename_keyword name '=' full_type
  1563. {
  1564. $$ = CPPType::new_type(new CPPClassTemplateParameter($2, $4));
  1565. }
  1566. | typename_keyword ELLIPSIS
  1567. {
  1568. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter(nullptr);
  1569. ctp->_packed = true;
  1570. $$ = CPPType::new_type(ctp);
  1571. }
  1572. | typename_keyword ELLIPSIS name
  1573. {
  1574. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter($3);
  1575. ctp->_packed = true;
  1576. $$ = CPPType::new_type(ctp);
  1577. }
  1578. | template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1579. {
  1580. CPPInstance *inst = new CPPInstance($1, $2, 0, @2.file);
  1581. inst->set_initializer($3);
  1582. $$ = inst;
  1583. }
  1584. | KW_CONST template_formal_parameter_type formal_parameter_identifier template_parameter_maybe_initialize
  1585. {
  1586. $3->add_modifier(IIT_const);
  1587. CPPInstance *inst = new CPPInstance($2, $3, 0, @3.file);
  1588. inst->set_initializer($4);
  1589. $$ = inst;
  1590. }
  1591. | template_formal_parameter_type parameter_pack_identifier
  1592. {
  1593. CPPInstance *inst = new CPPInstance($1, $2, 0, @2.file);
  1594. $$ = inst;
  1595. }
  1596. | KW_CONST template_formal_parameter_type parameter_pack_identifier
  1597. {
  1598. $3->add_modifier(IIT_const);
  1599. CPPInstance *inst = new CPPInstance($2, $3, 0, @3.file);
  1600. $$ = inst;
  1601. }
  1602. ;
  1603. template_formal_parameter_type:
  1604. simple_type
  1605. {
  1606. $$ = CPPType::new_type($1);
  1607. }
  1608. | IDENTIFIER
  1609. {
  1610. yywarning("Not a type: " + $1->get_fully_scoped_name(), @1);
  1611. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  1612. }
  1613. | TYPENAME_IDENTIFIER
  1614. {
  1615. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1616. if ($$ == nullptr) {
  1617. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1618. }
  1619. assert($$ != nullptr);
  1620. }
  1621. | TYPEPACK_IDENTIFIER
  1622. {
  1623. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  1624. if ($$ == nullptr) {
  1625. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  1626. }
  1627. assert($$ != nullptr);
  1628. }
  1629. ;
  1630. instance_identifier:
  1631. name_no_final optional_attributes
  1632. {
  1633. $$ = new CPPInstanceIdentifier($1);
  1634. }
  1635. | KW_OPERATOR function_operator optional_attributes
  1636. {
  1637. // For an operator function. We implement this simply by building a
  1638. // ficticious name for the function; in other respects it's just
  1639. // like a regular function.
  1640. CPPIdentifier *ident = $1;
  1641. if (ident == nullptr) {
  1642. ident = new CPPIdentifier("operator "+$2, @2);
  1643. } else {
  1644. ident->_names.push_back("operator "+$2);
  1645. }
  1646. $$ = new CPPInstanceIdentifier(ident);
  1647. }
  1648. | KW_OPERATOR SIMPLE_STRING IDENTIFIER optional_attributes
  1649. {
  1650. // A C++11 literal operator.
  1651. if (!$2.empty()) {
  1652. yyerror("expected empty string", @2);
  1653. }
  1654. CPPIdentifier *ident = $1;
  1655. if (ident == nullptr) {
  1656. ident = new CPPIdentifier("operator \"\" "+$3->get_simple_name(), @3);
  1657. } else {
  1658. ident->_names.push_back("operator \"\" "+$3->get_simple_name());
  1659. }
  1660. $$ = new CPPInstanceIdentifier(ident);
  1661. }
  1662. | KW_CONST instance_identifier %prec UNARY
  1663. {
  1664. $$ = $2;
  1665. $$->add_modifier(IIT_const);
  1666. }
  1667. | KW_VOLATILE instance_identifier %prec UNARY
  1668. {
  1669. $$ = $2;
  1670. $$->add_modifier(IIT_volatile);
  1671. }
  1672. | '*' optional_attributes instance_identifier %prec UNARY
  1673. {
  1674. $$ = $3;
  1675. $$->add_modifier(IIT_pointer);
  1676. }
  1677. | '&' optional_attributes instance_identifier %prec UNARY
  1678. {
  1679. $$ = $3;
  1680. $$->add_modifier(IIT_reference);
  1681. }
  1682. | ANDAND optional_attributes instance_identifier %prec UNARY
  1683. {
  1684. $$ = $3;
  1685. $$->add_modifier(IIT_rvalue_reference);
  1686. }
  1687. | SCOPING '*' optional_attributes instance_identifier %prec UNARY
  1688. {
  1689. $$ = $4;
  1690. $$->add_scoped_pointer_modifier($1);
  1691. }
  1692. | instance_identifier '[' optional_const_expr ']' optional_attributes
  1693. {
  1694. $$ = $1;
  1695. $$->add_array_modifier($3);
  1696. }
  1697. | '(' instance_identifier ')'
  1698. {
  1699. $$ = $2;
  1700. $$->add_modifier(IIT_paren);
  1701. }
  1702. | instance_identifier '('
  1703. {
  1704. // Create a scope for this function (in case it is a function)
  1705. CPPScope *scope = new CPPScope($1->get_scope(current_scope, global_scope),
  1706. CPPNameComponent(""), V_private);
  1707. // It still needs to be able to pick up any template arguments, if this is
  1708. // a definition for a method template. Add a fake "using" declaration to
  1709. // accomplish this.
  1710. scope->_using.insert(current_scope);
  1711. push_scope(scope);
  1712. }
  1713. formal_parameter_list ')' function_post
  1714. {
  1715. pop_scope();
  1716. $$ = $1;
  1717. if ($4->is_parameter_expr() && $6 == 0) {
  1718. // Oops, this must have been an instance declaration with a
  1719. // parameter list, not a function prototype.
  1720. $$->add_initializer_modifier($4);
  1721. } else {
  1722. // This was (probably) a function prototype.
  1723. $$->add_func_modifier($4, $6);
  1724. }
  1725. }
  1726. ;
  1727. instance_identifier_and_maybe_trailing_return_type:
  1728. instance_identifier maybe_trailing_return_type
  1729. {
  1730. // This is handled a bit awkwardly right now. Ideally it'd be wrapped
  1731. // up in the instance_identifier rule, but then more needs to happen in
  1732. // order to avoid shift/reduce conflicts.
  1733. if ($2 != nullptr) {
  1734. $1->add_trailing_return_type($2);
  1735. }
  1736. $$ = $1;
  1737. }
  1738. | instance_identifier ':' const_expr
  1739. {
  1740. // Bitfield definition.
  1741. $1->_bit_width = $3;
  1742. $$ = $1;
  1743. }
  1744. ;
  1745. maybe_trailing_return_type:
  1746. empty
  1747. {
  1748. $$ = nullptr;
  1749. }
  1750. | POINTSAT predefined_type empty_instance_identifier
  1751. {
  1752. $$ = $3->unroll_type($2);
  1753. }
  1754. | POINTSAT KW_CONST predefined_type empty_instance_identifier
  1755. {
  1756. $4->add_modifier(IIT_const);
  1757. $$ = $4->unroll_type($3);
  1758. }
  1759. ;
  1760. maybe_comma_identifier:
  1761. empty
  1762. {
  1763. $$ = nullptr;
  1764. }
  1765. | ',' IDENTIFIER
  1766. {
  1767. $$ = $2;
  1768. }
  1769. ;
  1770. function_parameter_list:
  1771. empty
  1772. {
  1773. $$ = new CPPParameterList;
  1774. }
  1775. | ELLIPSIS
  1776. {
  1777. $$ = new CPPParameterList;
  1778. $$->_includes_ellipsis = true;
  1779. }
  1780. | function_parameters
  1781. {
  1782. $$ = $1;
  1783. }
  1784. | function_parameters ',' ELLIPSIS
  1785. {
  1786. $$ = $1;
  1787. $$->_includes_ellipsis = true;
  1788. }
  1789. | function_parameters ELLIPSIS
  1790. {
  1791. $$ = $1;
  1792. $$->_includes_ellipsis = true;
  1793. }
  1794. ;
  1795. function_parameters:
  1796. function_parameter
  1797. {
  1798. $$ = new CPPParameterList;
  1799. $$->_parameters.push_back($1);
  1800. }
  1801. | function_parameters ',' function_parameter
  1802. {
  1803. $$ = $1;
  1804. $$->_parameters.push_back($3);
  1805. }
  1806. ;
  1807. formal_parameter_list:
  1808. empty
  1809. {
  1810. $$ = new CPPParameterList;
  1811. }
  1812. | ELLIPSIS
  1813. {
  1814. $$ = new CPPParameterList;
  1815. $$->_includes_ellipsis = true;
  1816. }
  1817. | formal_parameters
  1818. {
  1819. $$ = $1;
  1820. }
  1821. | formal_parameters ',' ELLIPSIS
  1822. {
  1823. $$ = $1;
  1824. $$->_includes_ellipsis = true;
  1825. }
  1826. | formal_parameters ELLIPSIS
  1827. {
  1828. $$ = $1;
  1829. $$->_includes_ellipsis = true;
  1830. }
  1831. ;
  1832. formal_parameters:
  1833. formal_parameter
  1834. {
  1835. $$ = new CPPParameterList;
  1836. $$->_parameters.push_back($1);
  1837. }
  1838. | formal_parameters ',' formal_parameter
  1839. {
  1840. $$ = $1;
  1841. $$->_parameters.push_back($3);
  1842. }
  1843. ;
  1844. template_parameter_maybe_initialize:
  1845. empty
  1846. {
  1847. $$ = nullptr;
  1848. }
  1849. | '=' no_angle_bracket_const_expr
  1850. {
  1851. $$ = $2;
  1852. }
  1853. ;
  1854. maybe_initialize:
  1855. empty
  1856. {
  1857. $$ = nullptr;
  1858. }
  1859. | '=' const_expr
  1860. {
  1861. $$ = $2;
  1862. }
  1863. ;
  1864. maybe_initialize_or_constructor_body:
  1865. ';'
  1866. {
  1867. $$ = nullptr;
  1868. }
  1869. | '{' code '}'
  1870. {
  1871. $$ = nullptr;
  1872. }
  1873. | ':' constructor_inits '{' code '}'
  1874. {
  1875. $$ = nullptr;
  1876. }
  1877. | '=' KW_DEFAULT ';'
  1878. {
  1879. $$ = new CPPExpression(CPPExpression::get_default());
  1880. }
  1881. | '=' KW_DELETE ';'
  1882. {
  1883. $$ = new CPPExpression(CPPExpression::get_delete());
  1884. }
  1885. ;
  1886. maybe_initialize_or_function_body:
  1887. ';'
  1888. {
  1889. $$ = nullptr;
  1890. }
  1891. | '{' code '}'
  1892. {
  1893. $$ = nullptr;
  1894. }
  1895. | '=' const_expr ';'
  1896. {
  1897. $$ = $2;
  1898. }
  1899. | '=' KW_DEFAULT ';'
  1900. {
  1901. $$ = new CPPExpression(CPPExpression::get_default());
  1902. }
  1903. | '=' KW_DELETE ';'
  1904. {
  1905. $$ = new CPPExpression(CPPExpression::get_delete());
  1906. }
  1907. | '=' '{' structure_init '}'
  1908. {
  1909. $$ = nullptr;
  1910. }
  1911. ;
  1912. structure_init:
  1913. empty
  1914. | structure_init_body
  1915. | structure_init_body ','
  1916. ;
  1917. structure_init_body:
  1918. const_expr
  1919. {
  1920. }
  1921. | '{' structure_init '}'
  1922. | structure_init_body ',' const_expr
  1923. | structure_init_body ',' '{' structure_init '}'
  1924. ;
  1925. function_parameter:
  1926. type formal_parameter_identifier maybe_initialize
  1927. {
  1928. $$ = new CPPInstance($1, $2, 0, @2.file);
  1929. $$->set_initializer($3);
  1930. }
  1931. | KW_CONST type formal_parameter_identifier maybe_initialize
  1932. {
  1933. $3->add_modifier(IIT_const);
  1934. $$ = new CPPInstance($2, $3, 0, @3.file);
  1935. $$->set_initializer($4);
  1936. }
  1937. | KW_CONST KW_REGISTER type formal_parameter_identifier maybe_initialize
  1938. {
  1939. $4->add_modifier(IIT_const);
  1940. $$ = new CPPInstance($3, $4, 0, @3.file);
  1941. $$->set_initializer($5);
  1942. }
  1943. | type_pack parameter_pack_identifier maybe_initialize
  1944. {
  1945. $$ = new CPPInstance($1, $2, 0, @2.file);
  1946. $$->set_initializer($3);
  1947. }
  1948. | KW_CONST type_pack parameter_pack_identifier maybe_initialize
  1949. {
  1950. $3->add_modifier(IIT_const);
  1951. $$ = new CPPInstance($2, $3, 0, @3.file);
  1952. $$->set_initializer($4);
  1953. }
  1954. | KW_CONST KW_REGISTER type_pack parameter_pack_identifier maybe_initialize
  1955. {
  1956. $4->add_modifier(IIT_const);
  1957. $$ = new CPPInstance($3, $4, 0, @3.file);
  1958. $$->set_initializer($5);
  1959. }
  1960. | KW_REGISTER function_parameter
  1961. {
  1962. $$ = $2;
  1963. }
  1964. | ATTR_LEFT attribute_specifiers ATTR_RIGHT function_parameter
  1965. {
  1966. $$ = $4;
  1967. }
  1968. ;
  1969. /* A "formal parameter" is like a function parameter, except that it parses
  1970. * instance declarations (that look like functions declaration) as well, and
  1971. * as such accepts constexpr parameters.
  1972. */
  1973. formal_parameter:
  1974. function_parameter
  1975. {
  1976. $$ = $1;
  1977. }
  1978. | formal_const_expr
  1979. {
  1980. CPPType *type =
  1981. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_parameter));
  1982. $$ = new CPPInstance(type, "expr");
  1983. $$->set_initializer($1);
  1984. }
  1985. ;
  1986. not_paren_formal_parameter_identifier:
  1987. empty
  1988. {
  1989. $$ = new CPPInstanceIdentifier(nullptr);
  1990. }
  1991. | name_no_final optional_attributes
  1992. {
  1993. $$ = new CPPInstanceIdentifier($1);
  1994. }
  1995. | KW_CONST not_paren_formal_parameter_identifier %prec UNARY
  1996. {
  1997. $$ = $2;
  1998. $$->add_modifier(IIT_const);
  1999. }
  2000. | KW_VOLATILE not_paren_formal_parameter_identifier %prec UNARY
  2001. {
  2002. $$ = $2;
  2003. $$->add_modifier(IIT_volatile);
  2004. }
  2005. | '*' optional_attributes not_paren_formal_parameter_identifier %prec UNARY
  2006. {
  2007. $$ = $3;
  2008. $$->add_modifier(IIT_pointer);
  2009. }
  2010. | '&' optional_attributes not_paren_formal_parameter_identifier %prec UNARY
  2011. {
  2012. $$ = $3;
  2013. $$->add_modifier(IIT_reference);
  2014. }
  2015. | ANDAND optional_attributes not_paren_formal_parameter_identifier %prec UNARY
  2016. {
  2017. $$ = $3;
  2018. $$->add_modifier(IIT_rvalue_reference);
  2019. }
  2020. | SCOPING '*' optional_attributes not_paren_formal_parameter_identifier %prec UNARY
  2021. {
  2022. $$ = $4;
  2023. $$->add_scoped_pointer_modifier($1);
  2024. }
  2025. | not_paren_formal_parameter_identifier '[' optional_const_expr ']' optional_attributes
  2026. {
  2027. $$ = $1;
  2028. $$->add_array_modifier($3);
  2029. }
  2030. ;
  2031. formal_parameter_identifier:
  2032. empty
  2033. {
  2034. $$ = new CPPInstanceIdentifier(nullptr);
  2035. }
  2036. | name_no_final optional_attributes
  2037. {
  2038. $$ = new CPPInstanceIdentifier($1);
  2039. }
  2040. | KW_CONST formal_parameter_identifier %prec UNARY
  2041. {
  2042. $$ = $2;
  2043. $$->add_modifier(IIT_const);
  2044. }
  2045. | KW_VOLATILE formal_parameter_identifier %prec UNARY
  2046. {
  2047. $$ = $2;
  2048. $$->add_modifier(IIT_volatile);
  2049. }
  2050. | '*' optional_attributes formal_parameter_identifier %prec UNARY
  2051. {
  2052. $$ = $3;
  2053. $$->add_modifier(IIT_pointer);
  2054. }
  2055. | '&' optional_attributes formal_parameter_identifier %prec UNARY
  2056. {
  2057. $$ = $3;
  2058. $$->add_modifier(IIT_reference);
  2059. }
  2060. | ANDAND optional_attributes formal_parameter_identifier %prec UNARY
  2061. {
  2062. $$ = $3;
  2063. $$->add_modifier(IIT_rvalue_reference);
  2064. }
  2065. | SCOPING '*' optional_attributes formal_parameter_identifier %prec UNARY
  2066. {
  2067. $$ = $4;
  2068. $$->add_scoped_pointer_modifier($1);
  2069. }
  2070. | formal_parameter_identifier '[' optional_const_expr ']' optional_attributes
  2071. {
  2072. $$ = $1;
  2073. $$->add_array_modifier($3);
  2074. }
  2075. | '(' formal_parameter_identifier ')' '(' function_parameter_list ')' function_post
  2076. {
  2077. $$ = $2;
  2078. $$->add_modifier(IIT_paren);
  2079. $$->add_func_modifier($5, $7);
  2080. }
  2081. | '(' formal_parameter_identifier ')'
  2082. {
  2083. $$ = $2;
  2084. $$->add_modifier(IIT_paren);
  2085. }
  2086. ;
  2087. parameter_pack_identifier:
  2088. ELLIPSIS
  2089. {
  2090. $$ = new CPPInstanceIdentifier(nullptr);
  2091. $$->_packed = true;
  2092. }
  2093. | ELLIPSIS name optional_attributes
  2094. {
  2095. $$ = new CPPInstanceIdentifier($2);
  2096. $$->_packed = true;
  2097. }
  2098. | KW_CONST parameter_pack_identifier %prec UNARY
  2099. {
  2100. $$ = $2;
  2101. $$->add_modifier(IIT_const);
  2102. }
  2103. | KW_VOLATILE parameter_pack_identifier %prec UNARY
  2104. {
  2105. $$ = $2;
  2106. $$->add_modifier(IIT_volatile);
  2107. }
  2108. | '*' optional_attributes parameter_pack_identifier %prec UNARY
  2109. {
  2110. $$ = $3;
  2111. $$->add_modifier(IIT_pointer);
  2112. }
  2113. | '&' optional_attributes parameter_pack_identifier %prec UNARY
  2114. {
  2115. $$ = $3;
  2116. $$->add_modifier(IIT_reference);
  2117. }
  2118. | ANDAND optional_attributes parameter_pack_identifier %prec UNARY
  2119. {
  2120. $$ = $3;
  2121. $$->add_modifier(IIT_rvalue_reference);
  2122. }
  2123. | SCOPING '*' optional_attributes parameter_pack_identifier %prec UNARY
  2124. {
  2125. $$ = $4;
  2126. $$->add_scoped_pointer_modifier($1);
  2127. }
  2128. | parameter_pack_identifier '[' optional_const_expr ']' optional_attributes
  2129. {
  2130. $$ = $1;
  2131. $$->add_array_modifier($3);
  2132. }
  2133. | '(' parameter_pack_identifier ')' '(' function_parameter_list ')' function_post
  2134. {
  2135. $$ = $2;
  2136. $$->add_modifier(IIT_paren);
  2137. $$->add_func_modifier($5, $7);
  2138. }
  2139. | '(' parameter_pack_identifier ')'
  2140. {
  2141. $$ = $2;
  2142. $$->add_modifier(IIT_paren);
  2143. }
  2144. ;
  2145. not_paren_empty_instance_identifier:
  2146. empty
  2147. {
  2148. $$ = new CPPInstanceIdentifier(nullptr);
  2149. }
  2150. | ELLIPSIS
  2151. {
  2152. $$ = new CPPInstanceIdentifier(nullptr);
  2153. $$->_packed = true;
  2154. }
  2155. | ELLIPSIS name optional_attributes
  2156. {
  2157. $$ = new CPPInstanceIdentifier($2);
  2158. $$->_packed = true;
  2159. }
  2160. | KW_CONST not_paren_empty_instance_identifier %prec UNARY
  2161. {
  2162. $$ = $2;
  2163. $$->add_modifier(IIT_const);
  2164. }
  2165. | KW_VOLATILE not_paren_empty_instance_identifier %prec UNARY
  2166. {
  2167. $$ = $2;
  2168. $$->add_modifier(IIT_volatile);
  2169. }
  2170. | '*' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2171. {
  2172. $$ = $3;
  2173. $$->add_modifier(IIT_pointer);
  2174. }
  2175. | '&' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2176. {
  2177. $$ = $3;
  2178. $$->add_modifier(IIT_reference);
  2179. }
  2180. | ANDAND optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2181. {
  2182. $$ = $3;
  2183. $$->add_modifier(IIT_rvalue_reference);
  2184. }
  2185. | SCOPING '*' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2186. {
  2187. $$ = $4;
  2188. $$->add_scoped_pointer_modifier($1);
  2189. }
  2190. | not_paren_empty_instance_identifier '[' optional_const_expr ']' optional_attributes
  2191. {
  2192. $$ = $1;
  2193. $$->add_array_modifier($3);
  2194. }
  2195. ;
  2196. empty_instance_identifier:
  2197. empty
  2198. {
  2199. $$ = new CPPInstanceIdentifier(nullptr);
  2200. }
  2201. | ELLIPSIS
  2202. {
  2203. $$ = new CPPInstanceIdentifier(nullptr);
  2204. $$->_packed = true;
  2205. }
  2206. | ELLIPSIS name optional_attributes
  2207. {
  2208. $$ = new CPPInstanceIdentifier($2);
  2209. $$->_packed = true;
  2210. }
  2211. | KW_CONST empty_instance_identifier %prec UNARY
  2212. {
  2213. $$ = $2;
  2214. $$->add_modifier(IIT_const);
  2215. }
  2216. | KW_VOLATILE empty_instance_identifier %prec UNARY
  2217. {
  2218. $$ = $2;
  2219. $$->add_modifier(IIT_volatile);
  2220. }
  2221. | '*' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2222. {
  2223. $$ = $3;
  2224. $$->add_modifier(IIT_pointer);
  2225. }
  2226. | '&' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2227. {
  2228. $$ = $3;
  2229. $$->add_modifier(IIT_reference);
  2230. }
  2231. | ANDAND optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2232. {
  2233. $$ = $3;
  2234. $$->add_modifier(IIT_rvalue_reference);
  2235. }
  2236. | SCOPING '*' optional_attributes not_paren_empty_instance_identifier %prec UNARY
  2237. {
  2238. $$ = $4;
  2239. $$->add_scoped_pointer_modifier($1);
  2240. }
  2241. | not_paren_empty_instance_identifier '[' optional_const_expr ']' optional_attributes
  2242. {
  2243. $$ = $1;
  2244. $$->add_array_modifier($3);
  2245. }
  2246. | '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2247. {
  2248. $$ = new CPPInstanceIdentifier(nullptr);
  2249. $$->add_modifier(IIT_paren);
  2250. $$->add_func_modifier($2, $4, $5);
  2251. }
  2252. | '(' '*' optional_attributes not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2253. {
  2254. $$ = $4;
  2255. $$->add_modifier(IIT_pointer);
  2256. $$->add_modifier(IIT_paren);
  2257. $$->add_func_modifier($7, $9, $10);
  2258. }
  2259. | '(' '&' optional_attributes not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2260. {
  2261. $$ = $4;
  2262. $$->add_modifier(IIT_reference);
  2263. $$->add_modifier(IIT_paren);
  2264. $$->add_func_modifier($7, $9, $10);
  2265. }
  2266. | '(' ANDAND optional_attributes not_paren_empty_instance_identifier ')' '(' function_parameter_list ')' function_post maybe_trailing_return_type
  2267. {
  2268. $$ = $4;
  2269. $$->add_modifier(IIT_rvalue_reference);
  2270. $$->add_modifier(IIT_paren);
  2271. $$->add_func_modifier($7, $9, $10);
  2272. }
  2273. ;
  2274. type:
  2275. simple_type
  2276. {
  2277. $$ = CPPType::new_type($1);
  2278. }
  2279. | TYPENAME_IDENTIFIER
  2280. {
  2281. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2282. if ($$ == nullptr) {
  2283. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2284. }
  2285. assert($$ != nullptr);
  2286. }
  2287. | KW_TYPENAME name
  2288. {
  2289. $$ = CPPType::new_type(new CPPTBDType($2));
  2290. }
  2291. | anonymous_struct
  2292. {
  2293. $$ = CPPType::new_type($1);
  2294. }
  2295. | named_struct
  2296. {
  2297. $$ = CPPType::new_type($1);
  2298. }
  2299. | enum
  2300. {
  2301. $$ = CPPType::new_type($1);
  2302. }
  2303. | struct_keyword struct_attributes name
  2304. {
  2305. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2306. if (type != nullptr) {
  2307. $$ = type;
  2308. } else {
  2309. CPPExtensionType *et =
  2310. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2311. ->as_extension_type();
  2312. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2313. if (scope != nullptr) {
  2314. scope->define_extension_type(et);
  2315. }
  2316. $$ = et;
  2317. }
  2318. }
  2319. | enum_keyword name_no_final ':' enum_element_type
  2320. {
  2321. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2322. if (type != nullptr) {
  2323. $$ = type;
  2324. } else {
  2325. CPPExtensionType *et =
  2326. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2327. ->as_extension_type();
  2328. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2329. if (scope != nullptr) {
  2330. scope->define_extension_type(et);
  2331. }
  2332. $$ = et;
  2333. }
  2334. }
  2335. | KW_DECLTYPE '(' const_expr ')'
  2336. {
  2337. $$ = $3->determine_type();
  2338. if ($$ == nullptr) {
  2339. stringstream str;
  2340. str << *$3;
  2341. yyerror("could not determine type of " + str.str(), @3);
  2342. }
  2343. }
  2344. | KW_DECLTYPE '(' KW_AUTO ')'
  2345. {
  2346. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2347. }
  2348. | KW_UNDERLYING_TYPE '(' full_type ')'
  2349. {
  2350. CPPEnumType *enum_type = $3->as_enum_type();
  2351. if (enum_type == nullptr) {
  2352. yyerror("an enumeration type is required", @3);
  2353. $$ = $3;
  2354. } else {
  2355. $$ = enum_type->get_underlying_type();
  2356. }
  2357. }
  2358. | KW_AUTO
  2359. {
  2360. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2361. }
  2362. ;
  2363. type_pack:
  2364. TYPEPACK_IDENTIFIER
  2365. {
  2366. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2367. if ($$ == nullptr) {
  2368. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2369. }
  2370. assert($$ != nullptr);
  2371. }
  2372. ;
  2373. type_decl:
  2374. simple_type
  2375. {
  2376. $$ = CPPType::new_type($1);
  2377. }
  2378. | TYPENAME_IDENTIFIER
  2379. {
  2380. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2381. if ($$ == nullptr) {
  2382. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2383. }
  2384. assert($$ != nullptr);
  2385. }
  2386. | KW_TYPENAME name
  2387. {
  2388. $$ = CPPType::new_type(new CPPTBDType($2));
  2389. }
  2390. | anonymous_struct
  2391. {
  2392. $$ = CPPType::new_type($1);
  2393. }
  2394. | named_struct
  2395. {
  2396. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  2397. }
  2398. | enum
  2399. {
  2400. $$ = new CPPTypeDeclaration(CPPType::new_type($1));
  2401. }
  2402. | struct_keyword struct_attributes name
  2403. {
  2404. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2405. if (type != nullptr) {
  2406. $$ = type;
  2407. } else {
  2408. CPPExtensionType *et =
  2409. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2410. ->as_extension_type();
  2411. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2412. if (scope != nullptr) {
  2413. scope->define_extension_type(et);
  2414. }
  2415. $$ = et;
  2416. }
  2417. }
  2418. | enum_keyword name_no_final ':' enum_element_type
  2419. {
  2420. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2421. if (type != nullptr) {
  2422. $$ = type;
  2423. } else {
  2424. CPPExtensionType *et =
  2425. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2426. ->as_extension_type();
  2427. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2428. if (scope != nullptr) {
  2429. scope->define_extension_type(et);
  2430. }
  2431. $$ = et;
  2432. }
  2433. }
  2434. | enum_keyword name
  2435. {
  2436. yywarning(string("C++ does not permit forward declaration of untyped enum ") + $2->get_fully_scoped_name(), @1);
  2437. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2438. if (type != nullptr) {
  2439. $$ = type;
  2440. } else {
  2441. CPPExtensionType *et =
  2442. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2443. ->as_extension_type();
  2444. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2445. if (scope != nullptr) {
  2446. scope->define_extension_type(et);
  2447. }
  2448. $$ = et;
  2449. }
  2450. }
  2451. | KW_DECLTYPE '(' const_expr ')'
  2452. {
  2453. $$ = $3->determine_type();
  2454. if ($$ == nullptr) {
  2455. stringstream str;
  2456. str << *$3;
  2457. yyerror("could not determine type of " + str.str(), @3);
  2458. }
  2459. }
  2460. | KW_DECLTYPE '(' KW_AUTO ')'
  2461. {
  2462. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2463. }
  2464. | KW_UNDERLYING_TYPE '(' full_type ')'
  2465. {
  2466. CPPEnumType *enum_type = $3->as_enum_type();
  2467. if (enum_type == nullptr) {
  2468. yyerror("an enumeration type is required", @3);
  2469. $$ = $3;
  2470. } else {
  2471. $$ = enum_type->get_underlying_type();
  2472. }
  2473. }
  2474. | KW_AUTO
  2475. {
  2476. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2477. }
  2478. ;
  2479. predefined_type:
  2480. simple_type
  2481. {
  2482. $$ = CPPType::new_type($1);
  2483. }
  2484. | TYPENAME_IDENTIFIER
  2485. {
  2486. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2487. if ($$ == nullptr) {
  2488. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  2489. }
  2490. assert($$ != nullptr);
  2491. }
  2492. | KW_TYPENAME name
  2493. {
  2494. $$ = CPPType::new_type(new CPPTBDType($2));
  2495. }
  2496. | struct_keyword struct_attributes name
  2497. {
  2498. CPPType *type = $3->find_type(current_scope, global_scope, false, current_lexer);
  2499. if (type != nullptr) {
  2500. $$ = type;
  2501. } else {
  2502. CPPExtensionType *et =
  2503. CPPType::new_type(new CPPExtensionType($1, $3, current_scope, @1.file))
  2504. ->as_extension_type();
  2505. CPPScope *scope = $3->get_scope(current_scope, global_scope);
  2506. if (scope != nullptr) {
  2507. scope->define_extension_type(et);
  2508. }
  2509. $$ = et;
  2510. }
  2511. }
  2512. | enum_keyword name
  2513. {
  2514. CPPType *type = $2->find_type(current_scope, global_scope, false, current_lexer);
  2515. if (type != nullptr) {
  2516. $$ = type;
  2517. } else {
  2518. CPPExtensionType *et =
  2519. CPPType::new_type(new CPPExtensionType($1, $2, current_scope, @1.file))
  2520. ->as_extension_type();
  2521. CPPScope *scope = $2->get_scope(current_scope, global_scope);
  2522. if (scope != nullptr) {
  2523. scope->define_extension_type(et);
  2524. }
  2525. $$ = et;
  2526. }
  2527. }
  2528. | KW_DECLTYPE '(' const_expr ')'
  2529. {
  2530. $$ = $3->determine_type();
  2531. if ($$ == nullptr) {
  2532. stringstream str;
  2533. str << *$3;
  2534. yyerror("could not determine type of " + str.str(), @3);
  2535. }
  2536. }
  2537. | KW_UNDERLYING_TYPE '(' full_type ')'
  2538. {
  2539. CPPEnumType *enum_type = $3->as_enum_type();
  2540. if (enum_type == nullptr) {
  2541. yyerror("an enumeration type is required", @3);
  2542. $$ = $3;
  2543. } else {
  2544. $$ = enum_type->get_underlying_type();
  2545. }
  2546. }
  2547. | KW_AUTO
  2548. {
  2549. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_auto));
  2550. }
  2551. ;
  2552. var_type_decl:
  2553. type_decl
  2554. {
  2555. $$ = $1;
  2556. }
  2557. | IDENTIFIER
  2558. {
  2559. yyerror(string("unknown type '") + $1->get_fully_scoped_name() + "'", @1);
  2560. $$ = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  2561. }
  2562. full_type:
  2563. type empty_instance_identifier
  2564. {
  2565. $$ = $2->unroll_type($1);
  2566. }
  2567. | KW_CONST type empty_instance_identifier
  2568. {
  2569. $3->add_modifier(IIT_const);
  2570. $$ = $3->unroll_type($2);
  2571. }
  2572. | type_pack empty_instance_identifier
  2573. {
  2574. $$ = $2->unroll_type($1);
  2575. }
  2576. | KW_CONST type_pack empty_instance_identifier
  2577. {
  2578. $3->add_modifier(IIT_const);
  2579. $$ = $3->unroll_type($2);
  2580. }
  2581. ;
  2582. struct_attributes:
  2583. empty
  2584. | struct_attributes ATTR_LEFT attribute_specifiers ATTR_RIGHT
  2585. | struct_attributes KW_ALIGNAS '(' const_expr ')'
  2586. | struct_attributes KW_ALIGNAS '(' type_decl ')'
  2587. ;
  2588. anonymous_struct:
  2589. struct_keyword struct_attributes '{'
  2590. {
  2591. CPPVisibility starting_vis =
  2592. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  2593. CPPScope *new_scope = new CPPScope(current_scope, CPPNameComponent("anon"),
  2594. starting_vis);
  2595. CPPStructType *st = new CPPStructType($1, nullptr, current_scope,
  2596. new_scope, @1.file);
  2597. new_scope->set_struct_type(st);
  2598. push_scope(new_scope);
  2599. push_struct(st);
  2600. }
  2601. cpp '}'
  2602. {
  2603. $$ = current_struct;
  2604. current_struct->_incomplete = false;
  2605. pop_struct();
  2606. pop_scope();
  2607. }
  2608. ;
  2609. named_struct:
  2610. struct_keyword struct_attributes name_no_final
  2611. {
  2612. CPPVisibility starting_vis =
  2613. ($1 == CPPExtensionType::T_class) ? V_private : V_public;
  2614. CPPScope *scope = $3->get_scope(current_scope, global_scope, current_lexer);
  2615. if (scope == nullptr) {
  2616. scope = current_scope;
  2617. }
  2618. CPPScope *new_scope = new CPPScope(scope, $3->_names.back(),
  2619. starting_vis);
  2620. CPPStructType *st = new CPPStructType($1, $3, current_scope,
  2621. new_scope, @1.file);
  2622. new_scope->set_struct_type(st);
  2623. current_scope->define_extension_type(st);
  2624. push_scope(new_scope);
  2625. push_struct(st);
  2626. }
  2627. maybe_final maybe_class_derivation '{' cpp '}'
  2628. {
  2629. $$ = current_struct;
  2630. current_struct->_incomplete = false;
  2631. pop_struct();
  2632. pop_scope();
  2633. }
  2634. ;
  2635. maybe_final:
  2636. empty
  2637. | KW_FINAL
  2638. {
  2639. current_struct->_final = true;
  2640. }
  2641. ;
  2642. maybe_class_derivation:
  2643. empty
  2644. | class_derivation
  2645. ;
  2646. class_derivation:
  2647. ':' base_specification
  2648. | class_derivation ',' base_specification
  2649. ;
  2650. base_specification:
  2651. class_derivation_name
  2652. {
  2653. current_struct->append_derivation($1, V_unknown, false);
  2654. }
  2655. | KW_PUBLIC class_derivation_name
  2656. {
  2657. current_struct->append_derivation($2, V_public, false);
  2658. }
  2659. | KW_PROTECTED class_derivation_name
  2660. {
  2661. current_struct->append_derivation($2, V_protected, false);
  2662. }
  2663. | KW_PRIVATE class_derivation_name
  2664. {
  2665. current_struct->append_derivation($2, V_private, false);
  2666. }
  2667. | KW_VIRTUAL KW_PUBLIC class_derivation_name
  2668. {
  2669. current_struct->append_derivation($3, V_public, true);
  2670. }
  2671. | KW_VIRTUAL KW_PROTECTED class_derivation_name
  2672. {
  2673. current_struct->append_derivation($3, V_protected, true);
  2674. }
  2675. | KW_VIRTUAL KW_PRIVATE class_derivation_name
  2676. {
  2677. current_struct->append_derivation($3, V_private, true);
  2678. }
  2679. | KW_PUBLIC KW_VIRTUAL class_derivation_name
  2680. {
  2681. current_struct->append_derivation($3, V_public, true);
  2682. }
  2683. | KW_PROTECTED KW_VIRTUAL class_derivation_name
  2684. {
  2685. current_struct->append_derivation($3, V_protected, true);
  2686. }
  2687. | KW_PRIVATE KW_VIRTUAL class_derivation_name
  2688. {
  2689. current_struct->append_derivation($3, V_private, true);
  2690. }
  2691. ;
  2692. enum:
  2693. enum_decl '{' enum_body '}'
  2694. {
  2695. $$ = current_enum;
  2696. current_enum = nullptr;
  2697. }
  2698. ;
  2699. enum_decl:
  2700. enum_keyword ':' enum_element_type
  2701. {
  2702. current_enum = new CPPEnumType($1, nullptr, $3, current_scope, nullptr, @1.file);
  2703. }
  2704. | enum_keyword
  2705. {
  2706. current_enum = new CPPEnumType($1, nullptr, current_scope, nullptr, @1.file);
  2707. }
  2708. | enum_keyword name_no_final ':' enum_element_type
  2709. {
  2710. CPPScope *new_scope = new CPPScope(current_scope, $2->_names.back(), V_public);
  2711. current_enum = new CPPEnumType($1, $2, $4, current_scope, new_scope, @1.file);
  2712. }
  2713. | enum_keyword name_no_final
  2714. {
  2715. CPPScope *new_scope = new CPPScope(current_scope, $2->_names.back(), V_public);
  2716. current_enum = new CPPEnumType($1, $2, current_scope, new_scope, @1.file);
  2717. }
  2718. ;
  2719. enum_element_type:
  2720. simple_int_type
  2721. {
  2722. $$ = CPPType::new_type($1);
  2723. }
  2724. | TYPENAME_IDENTIFIER
  2725. {
  2726. $$ = $1->find_type(current_scope, global_scope, false, current_lexer);
  2727. }
  2728. ;
  2729. enum_body_trailing_comma:
  2730. empty
  2731. | enum_body_trailing_comma name ','
  2732. {
  2733. assert(current_enum != nullptr);
  2734. current_enum->add_element($2->get_simple_name(), nullptr, current_lexer, @2);
  2735. }
  2736. | enum_body_trailing_comma name '=' const_expr ','
  2737. {
  2738. assert(current_enum != nullptr);
  2739. current_enum->add_element($2->get_simple_name(), $4, current_lexer, @2);
  2740. };
  2741. enum_body:
  2742. enum_body_trailing_comma
  2743. | enum_body_trailing_comma name
  2744. {
  2745. assert(current_enum != nullptr);
  2746. current_enum->add_element($2->get_simple_name(), nullptr, current_lexer, @2);
  2747. }
  2748. | enum_body_trailing_comma name '=' const_expr
  2749. {
  2750. assert(current_enum != nullptr);
  2751. current_enum->add_element($2->get_simple_name(), $4, current_lexer, @2);
  2752. }
  2753. ;
  2754. enum_keyword:
  2755. KW_ENUM
  2756. {
  2757. $$ = CPPExtensionType::T_enum;
  2758. }
  2759. | KW_ENUM KW_CLASS
  2760. {
  2761. $$ = CPPExtensionType::T_enum_class;
  2762. }
  2763. | KW_ENUM KW_STRUCT
  2764. {
  2765. $$ = CPPExtensionType::T_enum_struct;
  2766. }
  2767. ;
  2768. struct_keyword:
  2769. KW_CLASS
  2770. {
  2771. $$ = CPPExtensionType::T_class;
  2772. }
  2773. | KW_STRUCT
  2774. {
  2775. $$ = CPPExtensionType::T_struct;
  2776. }
  2777. | KW_UNION
  2778. {
  2779. $$ = CPPExtensionType::T_union;
  2780. }
  2781. ;
  2782. namespace_declaration:
  2783. KW_NAMESPACE name '{'
  2784. {
  2785. CPPScope *scope = $2->find_scope(current_scope, global_scope, current_lexer);
  2786. if (scope == nullptr) {
  2787. // This must be a new namespace declaration.
  2788. CPPScope *parent_scope =
  2789. $2->get_scope(current_scope, global_scope, current_lexer);
  2790. if (parent_scope == nullptr) {
  2791. parent_scope = current_scope;
  2792. }
  2793. scope = new CPPScope(parent_scope, $2->_names.back(), V_public);
  2794. }
  2795. CPPNamespace *nspace = new CPPNamespace($2, scope, @1.file);
  2796. current_scope->add_declaration(nspace, global_scope, current_lexer, @1);
  2797. current_scope->define_namespace(nspace);
  2798. push_scope(scope);
  2799. }
  2800. cpp '}'
  2801. {
  2802. pop_scope();
  2803. }
  2804. | KW_INLINE KW_NAMESPACE name '{'
  2805. {
  2806. CPPScope *scope = $3->find_scope(current_scope, global_scope, current_lexer);
  2807. if (scope == nullptr) {
  2808. // This must be a new namespace declaration.
  2809. CPPScope *parent_scope =
  2810. $3->get_scope(current_scope, global_scope, current_lexer);
  2811. if (parent_scope == nullptr) {
  2812. parent_scope = current_scope;
  2813. }
  2814. scope = new CPPScope(parent_scope, $3->_names.back(), V_public);
  2815. }
  2816. CPPNamespace *nspace = new CPPNamespace($3, scope, @2.file);
  2817. nspace->_is_inline = true;
  2818. current_scope->add_declaration(nspace, global_scope, current_lexer, @2);
  2819. current_scope->define_namespace(nspace);
  2820. push_scope(scope);
  2821. }
  2822. cpp '}'
  2823. {
  2824. pop_scope();
  2825. }
  2826. | KW_NAMESPACE '{' cpp '}'
  2827. | KW_INLINE KW_NAMESPACE '{' cpp '}'
  2828. ;
  2829. using_declaration:
  2830. KW_USING name ';'
  2831. {
  2832. CPPUsing *using_decl = new CPPUsing($2, false, @1.file);
  2833. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  2834. current_scope->add_using(using_decl, global_scope, current_lexer);
  2835. }
  2836. | KW_USING name '=' full_type ';'
  2837. {
  2838. // This is really just an alternative way to declare a typedef.
  2839. CPPTypedefType *typedef_type = new CPPTypedefType($4, $2, current_scope);
  2840. typedef_type->_using = true;
  2841. current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
  2842. }
  2843. | KW_USING KW_NAMESPACE name ';'
  2844. {
  2845. CPPUsing *using_decl = new CPPUsing($3, true, @1.file);
  2846. current_scope->add_declaration(using_decl, global_scope, current_lexer, @1);
  2847. current_scope->add_using(using_decl, global_scope, current_lexer);
  2848. }
  2849. ;
  2850. simple_type:
  2851. simple_int_type
  2852. | simple_float_type
  2853. | simple_void_type
  2854. ;
  2855. simple_int_type:
  2856. KW_BOOL
  2857. {
  2858. $$ = new CPPSimpleType(CPPSimpleType::T_bool);
  2859. }
  2860. | KW_CHAR
  2861. {
  2862. $$ = new CPPSimpleType(CPPSimpleType::T_char);
  2863. }
  2864. | KW_WCHAR_T
  2865. {
  2866. $$ = new CPPSimpleType(CPPSimpleType::T_wchar_t);
  2867. }
  2868. | KW_CHAR16_T
  2869. {
  2870. $$ = new CPPSimpleType(CPPSimpleType::T_char16_t);
  2871. }
  2872. | KW_CHAR32_T
  2873. {
  2874. $$ = new CPPSimpleType(CPPSimpleType::T_char32_t);
  2875. }
  2876. | KW_SHORT
  2877. {
  2878. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2879. CPPSimpleType::F_short);
  2880. }
  2881. | KW_LONG
  2882. {
  2883. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2884. CPPSimpleType::F_long);
  2885. }
  2886. | KW_UNSIGNED
  2887. {
  2888. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2889. CPPSimpleType::F_unsigned);
  2890. }
  2891. | KW_SIGNED
  2892. {
  2893. $$ = new CPPSimpleType(CPPSimpleType::T_int,
  2894. CPPSimpleType::F_signed);
  2895. }
  2896. | KW_INT
  2897. {
  2898. $$ = new CPPSimpleType(CPPSimpleType::T_int);
  2899. }
  2900. | KW_SHORT simple_int_type
  2901. {
  2902. $$ = $2;
  2903. $$->_flags |= CPPSimpleType::F_short;
  2904. }
  2905. | KW_LONG simple_int_type
  2906. {
  2907. $$ = $2;
  2908. if ($$->_flags & CPPSimpleType::F_long) {
  2909. $$->_flags |= CPPSimpleType::F_longlong;
  2910. } else {
  2911. $$->_flags |= CPPSimpleType::F_long;
  2912. }
  2913. }
  2914. | KW_UNSIGNED simple_int_type
  2915. {
  2916. $$ = $2;
  2917. $$->_flags |= CPPSimpleType::F_unsigned;
  2918. }
  2919. | KW_SIGNED simple_int_type
  2920. {
  2921. $$ = $2;
  2922. $$->_flags |= CPPSimpleType::F_signed;
  2923. }
  2924. ;
  2925. simple_float_type:
  2926. KW_FLOAT
  2927. {
  2928. $$ = new CPPSimpleType(CPPSimpleType::T_float);
  2929. }
  2930. | KW_DOUBLE
  2931. {
  2932. $$ = new CPPSimpleType(CPPSimpleType::T_double);
  2933. }
  2934. | KW_LONG KW_DOUBLE
  2935. {
  2936. $$ = new CPPSimpleType(CPPSimpleType::T_double,
  2937. CPPSimpleType::F_long);
  2938. }
  2939. ;
  2940. simple_void_type:
  2941. KW_VOID
  2942. {
  2943. $$ = new CPPSimpleType(CPPSimpleType::T_void);
  2944. }
  2945. ;
  2946. /* We don't care what the code is. We just want to be sure we match
  2947. up opening and closing braces properly. For anything else, we'll
  2948. accept just token salad. */
  2949. code:
  2950. {
  2951. current_lexer->_resolve_identifiers = false;
  2952. }
  2953. code_block
  2954. {
  2955. current_lexer->_resolve_identifiers = true;
  2956. }
  2957. ;
  2958. code_block:
  2959. empty
  2960. | code_block element
  2961. ;
  2962. element:
  2963. REAL
  2964. | INTEGER
  2965. | SIMPLE_STRING
  2966. | STRING_LITERAL
  2967. | CUSTOM_LITERAL
  2968. | CHAR_TOK
  2969. | IDENTIFIER
  2970. | TYPENAME_IDENTIFIER
  2971. | TYPEPACK_IDENTIFIER
  2972. | SCOPING
  2973. | SIMPLE_IDENTIFIER
  2974. | ELLIPSIS | OROR | ANDAND
  2975. | EQCOMPARE | NECOMPARE | LECOMPARE | GECOMPARE
  2976. | LSHIFT | RSHIFT | POINTSAT_STAR | DOT_STAR | POINTSAT
  2977. | SCOPE | PLUSPLUS | MINUSMINUS
  2978. | TIMESEQUAL | DIVIDEEQUAL | MODEQUAL | PLUSEQUAL | MINUSEQUAL
  2979. | OREQUAL | ANDEQUAL | XOREQUAL | LSHIFTEQUAL | RSHIFTEQUAL
  2980. | ATTR_LEFT | ATTR_RIGHT
  2981. | KW_ALIGNAS | KW_ALIGNOF | KW_AUTO | KW_BOOL | KW_CATCH
  2982. | KW_CHAR | KW_CHAR16_T | KW_CHAR32_T | KW_CLASS | KW_CONST
  2983. | KW_CONSTEXPR | KW_CONST_CAST | KW_DECLTYPE | KW_DEFAULT
  2984. | KW_DELETE | KW_DOUBLE | KW_DYNAMIC_CAST | KW_ELSE | KW_ENUM
  2985. | KW_EXTERN | KW_EXPLICIT | KW_FALSE | KW_FINAL | KW_FLOAT
  2986. | KW_FRIEND | KW_FOR | KW_GOTO | KW_IF | KW_INLINE | KW_INT
  2987. | KW_LONG | KW_MUTABLE | KW_NAMESPACE | KW_NEW | KW_NULLPTR
  2988. | KW_OPERATOR | KW_OVERRIDE | KW_PRIVATE | KW_PROTECTED
  2989. | KW_PUBLIC | KW_PUBLISHED | KW_REGISTER | KW_REINTERPRET_CAST
  2990. | KW_RETURN | KW_SHORT | KW_SIGNED | KW_SIZEOF | KW_STATIC
  2991. | KW_STATIC_ASSERT | KW_STATIC_CAST | KW_STRUCT | KW_TEMPLATE
  2992. | KW_THREAD_LOCAL | KW_THROW | KW_TRUE | KW_TRY | KW_TYPEDEF
  2993. | KW_TYPEID | KW_TYPENAME | KW_UNDERLYING_TYPE | KW_UNION
  2994. | KW_UNSIGNED | KW_USING | KW_VIRTUAL | KW_VOID | KW_VOLATILE
  2995. | KW_WCHAR_T | KW_WHILE
  2996. {
  2997. }
  2998. | '+' | '-' | '*' | '/' | '&' | '|' | '^' | '!' | '~' | '=' | '%'
  2999. | '<' | '>' | '(' | ')' | '.' | ',' | ';' | ':' | '[' | ']'
  3000. | '?' | '{' code_block '}'
  3001. ;
  3002. optional_const_expr:
  3003. empty
  3004. {
  3005. $$ = nullptr;
  3006. }
  3007. | const_expr
  3008. {
  3009. $$ = $1;
  3010. }
  3011. ;
  3012. optional_const_expr_comma:
  3013. empty
  3014. {
  3015. $$ = nullptr;
  3016. }
  3017. | const_expr_comma
  3018. {
  3019. $$ = $1;
  3020. }
  3021. ;
  3022. const_expr_comma:
  3023. const_expr
  3024. {
  3025. $$ = $1;
  3026. }
  3027. | const_expr_comma ',' const_expr
  3028. {
  3029. $$ = new CPPExpression(',', $1, $3);
  3030. }
  3031. ;
  3032. no_angle_bracket_const_expr:
  3033. const_operand
  3034. {
  3035. $$ = $1;
  3036. }
  3037. | '(' full_type ')' no_angle_bracket_const_expr %prec UNARY
  3038. {
  3039. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3040. }
  3041. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3042. {
  3043. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3044. }
  3045. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3046. {
  3047. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3048. }
  3049. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3050. {
  3051. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3052. }
  3053. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3054. {
  3055. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3056. }
  3057. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3058. {
  3059. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3060. }
  3061. | KW_SIZEOF no_angle_bracket_const_expr %prec UNARY
  3062. {
  3063. $$ = new CPPExpression(CPPExpression::sizeof_func($2));
  3064. }
  3065. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3066. {
  3067. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3068. }
  3069. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3070. {
  3071. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3072. }
  3073. | '!' no_angle_bracket_const_expr %prec UNARY
  3074. {
  3075. $$ = new CPPExpression(UNARY_NOT, $2);
  3076. }
  3077. | '~' no_angle_bracket_const_expr %prec UNARY
  3078. {
  3079. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3080. }
  3081. | '-' no_angle_bracket_const_expr %prec UNARY
  3082. {
  3083. $$ = new CPPExpression(UNARY_MINUS, $2);
  3084. }
  3085. | '+' no_angle_bracket_const_expr %prec UNARY
  3086. {
  3087. $$ = new CPPExpression(UNARY_PLUS, $2);
  3088. }
  3089. | '*' no_angle_bracket_const_expr %prec UNARY
  3090. {
  3091. $$ = new CPPExpression(UNARY_STAR, $2);
  3092. }
  3093. | '&' no_angle_bracket_const_expr %prec UNARY
  3094. {
  3095. $$ = new CPPExpression(UNARY_REF, $2);
  3096. }
  3097. | no_angle_bracket_const_expr '*' no_angle_bracket_const_expr
  3098. {
  3099. $$ = new CPPExpression('*', $1, $3);
  3100. }
  3101. | no_angle_bracket_const_expr '/' no_angle_bracket_const_expr
  3102. {
  3103. $$ = new CPPExpression('/', $1, $3);
  3104. }
  3105. | no_angle_bracket_const_expr '%' no_angle_bracket_const_expr
  3106. {
  3107. $$ = new CPPExpression('%', $1, $3);
  3108. }
  3109. | no_angle_bracket_const_expr '+' no_angle_bracket_const_expr
  3110. {
  3111. $$ = new CPPExpression('+', $1, $3);
  3112. }
  3113. | no_angle_bracket_const_expr '-' no_angle_bracket_const_expr
  3114. {
  3115. $$ = new CPPExpression('-', $1, $3);
  3116. }
  3117. | no_angle_bracket_const_expr '|' no_angle_bracket_const_expr
  3118. {
  3119. $$ = new CPPExpression('|', $1, $3);
  3120. }
  3121. | no_angle_bracket_const_expr '^' no_angle_bracket_const_expr
  3122. {
  3123. $$ = new CPPExpression('^', $1, $3);
  3124. }
  3125. | no_angle_bracket_const_expr '&' no_angle_bracket_const_expr
  3126. {
  3127. $$ = new CPPExpression('&', $1, $3);
  3128. }
  3129. | no_angle_bracket_const_expr OROR no_angle_bracket_const_expr
  3130. {
  3131. $$ = new CPPExpression(OROR, $1, $3);
  3132. }
  3133. | no_angle_bracket_const_expr ANDAND no_angle_bracket_const_expr
  3134. {
  3135. $$ = new CPPExpression(ANDAND, $1, $3);
  3136. }
  3137. | no_angle_bracket_const_expr EQCOMPARE no_angle_bracket_const_expr
  3138. {
  3139. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3140. }
  3141. | no_angle_bracket_const_expr NECOMPARE no_angle_bracket_const_expr
  3142. {
  3143. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3144. }
  3145. | no_angle_bracket_const_expr LECOMPARE no_angle_bracket_const_expr
  3146. {
  3147. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3148. }
  3149. | no_angle_bracket_const_expr GECOMPARE no_angle_bracket_const_expr
  3150. {
  3151. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3152. }
  3153. | no_angle_bracket_const_expr LSHIFT no_angle_bracket_const_expr
  3154. {
  3155. $$ = new CPPExpression(LSHIFT, $1, $3);
  3156. }
  3157. | no_angle_bracket_const_expr RSHIFT no_angle_bracket_const_expr
  3158. {
  3159. $$ = new CPPExpression(RSHIFT, $1, $3);
  3160. }
  3161. | no_angle_bracket_const_expr '?' no_angle_bracket_const_expr ':' no_angle_bracket_const_expr
  3162. {
  3163. $$ = new CPPExpression('?', $1, $3, $5);
  3164. }
  3165. | no_angle_bracket_const_expr '[' const_expr ']'
  3166. {
  3167. $$ = new CPPExpression('[', $1, $3);
  3168. }
  3169. | no_angle_bracket_const_expr '(' const_expr_comma ')'
  3170. {
  3171. $$ = new CPPExpression('f', $1, $3);
  3172. }
  3173. | no_angle_bracket_const_expr '(' ')'
  3174. {
  3175. $$ = new CPPExpression('f', $1);
  3176. }
  3177. | no_angle_bracket_const_expr '.' name
  3178. {
  3179. $$ = new CPPExpression('.', $1, new CPPExpression($3, current_scope, global_scope, current_lexer));
  3180. }
  3181. | no_angle_bracket_const_expr POINTSAT no_angle_bracket_const_expr
  3182. {
  3183. $$ = new CPPExpression(POINTSAT, $1, $3);
  3184. }
  3185. | '(' const_expr_comma ')'
  3186. {
  3187. $$ = $2;
  3188. }
  3189. ;
  3190. const_expr:
  3191. const_operand
  3192. {
  3193. $$ = $1;
  3194. }
  3195. | '(' full_type ')' const_expr %prec UNARY
  3196. {
  3197. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3198. }
  3199. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3200. {
  3201. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3202. }
  3203. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3204. {
  3205. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3206. }
  3207. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3208. {
  3209. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3210. }
  3211. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3212. {
  3213. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3214. }
  3215. | TYPENAME_IDENTIFIER '(' optional_const_expr_comma ')'
  3216. {
  3217. // A constructor call.
  3218. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3219. if (type == nullptr) {
  3220. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3221. }
  3222. assert(type != nullptr);
  3223. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3224. }
  3225. | TYPENAME_IDENTIFIER '{' optional_const_expr_comma '}'
  3226. {
  3227. // Aggregate initialization.
  3228. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3229. if (type == nullptr) {
  3230. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3231. }
  3232. assert(type != nullptr);
  3233. $$ = new CPPExpression(CPPExpression::aggregate_init_op(type, $3));
  3234. }
  3235. | KW_INT '(' optional_const_expr_comma ')'
  3236. {
  3237. CPPType *type =
  3238. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int));
  3239. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3240. }
  3241. | KW_CHAR '(' optional_const_expr_comma ')'
  3242. {
  3243. CPPType *type =
  3244. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char));
  3245. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3246. }
  3247. | KW_WCHAR_T '(' optional_const_expr_comma ')'
  3248. {
  3249. CPPType *type =
  3250. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_wchar_t));
  3251. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3252. }
  3253. | KW_CHAR16_T '(' optional_const_expr_comma ')'
  3254. {
  3255. CPPType *type =
  3256. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char16_t));
  3257. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3258. }
  3259. | KW_CHAR32_T '(' optional_const_expr_comma ')'
  3260. {
  3261. CPPType *type =
  3262. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_char32_t));
  3263. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3264. }
  3265. | KW_BOOL '(' optional_const_expr_comma ')'
  3266. {
  3267. CPPType *type =
  3268. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_bool));
  3269. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3270. }
  3271. | KW_SHORT '(' optional_const_expr_comma ')'
  3272. {
  3273. CPPType *type =
  3274. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3275. CPPSimpleType::F_short));
  3276. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3277. }
  3278. | KW_LONG '(' optional_const_expr_comma ')'
  3279. {
  3280. CPPType *type =
  3281. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3282. CPPSimpleType::F_long));
  3283. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3284. }
  3285. | KW_UNSIGNED '(' optional_const_expr_comma ')'
  3286. {
  3287. CPPType *type =
  3288. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3289. CPPSimpleType::F_unsigned));
  3290. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3291. }
  3292. | KW_SIGNED '(' optional_const_expr_comma ')'
  3293. {
  3294. CPPType *type =
  3295. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_int,
  3296. CPPSimpleType::F_signed));
  3297. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3298. }
  3299. | KW_FLOAT '(' optional_const_expr_comma ')'
  3300. {
  3301. CPPType *type =
  3302. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_float));
  3303. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3304. }
  3305. | KW_DOUBLE '(' optional_const_expr_comma ')'
  3306. {
  3307. CPPType *type =
  3308. CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_double));
  3309. $$ = new CPPExpression(CPPExpression::construct_op(type, $3));
  3310. }
  3311. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3312. {
  3313. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3314. }
  3315. | KW_SIZEOF const_expr %prec UNARY
  3316. {
  3317. $$ = new CPPExpression(CPPExpression::sizeof_func($2));
  3318. }
  3319. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3320. {
  3321. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3322. }
  3323. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3324. {
  3325. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3326. }
  3327. | KW_NEW predefined_type %prec UNARY
  3328. {
  3329. $$ = new CPPExpression(CPPExpression::new_op($2));
  3330. }
  3331. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  3332. {
  3333. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  3334. }
  3335. | KW_TYPEID '(' full_type ')'
  3336. {
  3337. CPPIdentifier ident("");
  3338. ident.add_name("std");
  3339. ident.add_name("type_info");
  3340. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3341. if (!std_type_info) {
  3342. yywarning("cannot use typeid before including <typeinfo>", @1);
  3343. }
  3344. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3345. }
  3346. | KW_TYPEID '(' const_expr ')'
  3347. {
  3348. CPPIdentifier ident("");
  3349. ident.add_name("std");
  3350. ident.add_name("type_info");
  3351. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3352. if (!std_type_info) {
  3353. yywarning("cannot use typeid before including <typeinfo>", @1);
  3354. }
  3355. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3356. }
  3357. | '!' const_expr %prec UNARY
  3358. {
  3359. $$ = new CPPExpression(UNARY_NOT, $2);
  3360. }
  3361. | '~' const_expr %prec UNARY
  3362. {
  3363. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3364. }
  3365. | '-' const_expr %prec UNARY
  3366. {
  3367. $$ = new CPPExpression(UNARY_MINUS, $2);
  3368. }
  3369. | '+' const_expr %prec UNARY
  3370. {
  3371. $$ = new CPPExpression(UNARY_PLUS, $2);
  3372. }
  3373. | '*' const_expr %prec UNARY
  3374. {
  3375. $$ = new CPPExpression(UNARY_STAR, $2);
  3376. }
  3377. | '&' const_expr %prec UNARY
  3378. {
  3379. $$ = new CPPExpression(UNARY_REF, $2);
  3380. }
  3381. | const_expr '*' const_expr
  3382. {
  3383. $$ = new CPPExpression('*', $1, $3);
  3384. }
  3385. | const_expr '/' const_expr
  3386. {
  3387. $$ = new CPPExpression('/', $1, $3);
  3388. }
  3389. | const_expr '%' const_expr
  3390. {
  3391. $$ = new CPPExpression('%', $1, $3);
  3392. }
  3393. | const_expr '+' const_expr
  3394. {
  3395. $$ = new CPPExpression('+', $1, $3);
  3396. }
  3397. | const_expr '-' const_expr
  3398. {
  3399. $$ = new CPPExpression('-', $1, $3);
  3400. }
  3401. | const_expr '|' const_expr
  3402. {
  3403. $$ = new CPPExpression('|', $1, $3);
  3404. }
  3405. | const_expr '^' const_expr
  3406. {
  3407. $$ = new CPPExpression('^', $1, $3);
  3408. }
  3409. | const_expr '&' const_expr
  3410. {
  3411. $$ = new CPPExpression('&', $1, $3);
  3412. }
  3413. | const_expr OROR const_expr
  3414. {
  3415. $$ = new CPPExpression(OROR, $1, $3);
  3416. }
  3417. | const_expr ANDAND const_expr
  3418. {
  3419. $$ = new CPPExpression(ANDAND, $1, $3);
  3420. }
  3421. | const_expr EQCOMPARE const_expr
  3422. {
  3423. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3424. }
  3425. | const_expr NECOMPARE const_expr
  3426. {
  3427. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3428. }
  3429. | const_expr LECOMPARE const_expr
  3430. {
  3431. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3432. }
  3433. | const_expr GECOMPARE const_expr
  3434. {
  3435. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3436. }
  3437. | const_expr '<' const_expr
  3438. {
  3439. $$ = new CPPExpression('<', $1, $3);
  3440. }
  3441. | const_expr '>' const_expr
  3442. {
  3443. $$ = new CPPExpression('>', $1, $3);
  3444. }
  3445. | const_expr LSHIFT const_expr
  3446. {
  3447. $$ = new CPPExpression(LSHIFT, $1, $3);
  3448. }
  3449. | const_expr RSHIFT const_expr
  3450. {
  3451. $$ = new CPPExpression(RSHIFT, $1, $3);
  3452. }
  3453. | const_expr '?' const_expr ':' const_expr
  3454. {
  3455. $$ = new CPPExpression('?', $1, $3, $5);
  3456. }
  3457. | const_expr '[' const_expr ']'
  3458. {
  3459. $$ = new CPPExpression('[', $1, $3);
  3460. }
  3461. | const_expr '(' const_expr_comma ')'
  3462. {
  3463. $$ = new CPPExpression('f', $1, $3);
  3464. }
  3465. | const_expr '(' ')'
  3466. {
  3467. $$ = new CPPExpression('f', $1);
  3468. }
  3469. | const_expr '.' name
  3470. {
  3471. $$ = new CPPExpression('.', $1, new CPPExpression($3, current_scope, global_scope, current_lexer));
  3472. }
  3473. | const_expr POINTSAT const_expr
  3474. {
  3475. $$ = new CPPExpression(POINTSAT, $1, $3);
  3476. }
  3477. | '(' const_expr_comma ')'
  3478. {
  3479. $$ = $2;
  3480. }
  3481. ;
  3482. const_operand:
  3483. INTEGER
  3484. {
  3485. $$ = new CPPExpression($1);
  3486. }
  3487. | KW_TRUE
  3488. {
  3489. $$ = new CPPExpression(true);
  3490. }
  3491. | KW_FALSE
  3492. {
  3493. $$ = new CPPExpression(false);
  3494. }
  3495. | CHAR_TOK
  3496. {
  3497. $$ = new CPPExpression($1);
  3498. }
  3499. | REAL
  3500. {
  3501. $$ = new CPPExpression($1);
  3502. }
  3503. | string_literal
  3504. {
  3505. $$ = $1;
  3506. }
  3507. | CUSTOM_LITERAL
  3508. {
  3509. $$ = $1;
  3510. }
  3511. | IDENTIFIER
  3512. {
  3513. $$ = new CPPExpression($1, current_scope, global_scope, current_lexer);
  3514. }
  3515. | KW_FINAL
  3516. {
  3517. // A variable named "final". C++11 explicitly permits this.
  3518. CPPIdentifier *ident = new CPPIdentifier("final", @1);
  3519. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3520. }
  3521. | KW_OVERRIDE
  3522. {
  3523. // A variable named "override". C++11 explicitly permits this.
  3524. CPPIdentifier *ident = new CPPIdentifier("override", @1);
  3525. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3526. }
  3527. | KW_NULLPTR
  3528. {
  3529. $$ = new CPPExpression(CPPExpression::get_nullptr());
  3530. }
  3531. | '[' capture_list ']' function_post maybe_trailing_return_type '{' code '}'
  3532. {
  3533. $2->_flags = $4;
  3534. $2->_return_type = $5;
  3535. $$ = new CPPExpression(CPPExpression::lambda($2));
  3536. }
  3537. | '[' capture_list ']' '(' function_parameter_list ')' function_post maybe_trailing_return_type '{' code '}'
  3538. {
  3539. $2->_parameters = $5;
  3540. $2->_flags = $7;
  3541. $2->_return_type = $8;
  3542. $$ = new CPPExpression(CPPExpression::lambda($2));
  3543. }
  3544. | KW_HAS_VIRTUAL_DESTRUCTOR '(' full_type ')'
  3545. {
  3546. $$ = new CPPExpression(CPPExpression::type_trait(KW_HAS_VIRTUAL_DESTRUCTOR, $3));
  3547. }
  3548. | KW_IS_ABSTRACT '(' full_type ')'
  3549. {
  3550. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_ABSTRACT, $3));
  3551. }
  3552. | KW_IS_BASE_OF '(' full_type ',' full_type ')'
  3553. {
  3554. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CLASS, $3, $5));
  3555. }
  3556. | KW_IS_CLASS '(' full_type ')'
  3557. {
  3558. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CLASS, $3));
  3559. }
  3560. | KW_IS_CONSTRUCTIBLE '(' full_type ')'
  3561. {
  3562. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONSTRUCTIBLE, $3));
  3563. }
  3564. | KW_IS_CONSTRUCTIBLE '(' full_type ',' full_type ')'
  3565. {
  3566. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONSTRUCTIBLE, $3, $5));
  3567. }
  3568. | KW_IS_CONVERTIBLE_TO '(' full_type ',' full_type ')'
  3569. {
  3570. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_CONVERTIBLE_TO, $3, $5));
  3571. }
  3572. | KW_IS_DESTRUCTIBLE '(' full_type ')'
  3573. {
  3574. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_DESTRUCTIBLE, $3));
  3575. }
  3576. | KW_IS_EMPTY '(' full_type ')'
  3577. {
  3578. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_EMPTY, $3));
  3579. }
  3580. | KW_IS_ENUM '(' full_type ')'
  3581. {
  3582. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_ENUM, $3));
  3583. }
  3584. | KW_IS_FINAL '(' full_type ')'
  3585. {
  3586. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_FINAL, $3));
  3587. }
  3588. | KW_IS_FUNDAMENTAL '(' full_type ')'
  3589. {
  3590. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_FUNDAMENTAL, $3));
  3591. }
  3592. | KW_IS_POD '(' full_type ')'
  3593. {
  3594. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_POD, $3));
  3595. }
  3596. | KW_IS_POLYMORPHIC '(' full_type ')'
  3597. {
  3598. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_POLYMORPHIC, $3));
  3599. }
  3600. | KW_IS_STANDARD_LAYOUT '(' full_type ')'
  3601. {
  3602. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_STANDARD_LAYOUT, $3));
  3603. }
  3604. | KW_IS_TRIVIAL '(' full_type ')'
  3605. {
  3606. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_TRIVIAL, $3));
  3607. }
  3608. | KW_IS_UNION '(' full_type ')'
  3609. {
  3610. $$ = new CPPExpression(CPPExpression::type_trait(KW_IS_UNION, $3));
  3611. }
  3612. ;
  3613. /* This is used for a const_expr as a "formal parameter", which really
  3614. means an instance declaration using a parameter list (which looks a
  3615. lot like a function prototype). It differs from const_expr mainly
  3616. in that it forbids some expressions unless they are parenthesized,
  3617. to avoid shift/reduce conflicts with the actual formal parameter
  3618. definition. */
  3619. formal_const_expr:
  3620. formal_const_operand
  3621. {
  3622. $$ = $1;
  3623. }
  3624. | '(' full_type ')' const_expr %prec UNARY
  3625. {
  3626. $$ = new CPPExpression(CPPExpression::typecast_op($2, $4));
  3627. }
  3628. | KW_STATIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3629. {
  3630. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_static_cast));
  3631. }
  3632. | KW_DYNAMIC_CAST '<' full_type '>' '(' const_expr_comma ')'
  3633. {
  3634. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_dynamic_cast));
  3635. }
  3636. | KW_CONST_CAST '<' full_type '>' '(' const_expr_comma ')'
  3637. {
  3638. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_const_cast));
  3639. }
  3640. | KW_REINTERPRET_CAST '<' full_type '>' '(' const_expr_comma ')'
  3641. {
  3642. $$ = new CPPExpression(CPPExpression::typecast_op($3, $6, CPPExpression::T_reinterpret_cast));
  3643. }
  3644. | KW_SIZEOF '(' full_type ')' %prec UNARY
  3645. {
  3646. $$ = new CPPExpression(CPPExpression::sizeof_func($3));
  3647. }
  3648. | KW_SIZEOF formal_const_expr %prec UNARY
  3649. {
  3650. $$ = new CPPExpression(CPPExpression::sizeof_func($2));
  3651. }
  3652. | KW_SIZEOF ELLIPSIS '(' name ')' %prec UNARY
  3653. {
  3654. $$ = new CPPExpression(CPPExpression::sizeof_ellipsis_func($4));
  3655. }
  3656. | KW_ALIGNOF '(' full_type ')' %prec UNARY
  3657. {
  3658. $$ = new CPPExpression(CPPExpression::alignof_func($3));
  3659. }
  3660. | KW_NEW predefined_type %prec UNARY
  3661. {
  3662. $$ = new CPPExpression(CPPExpression::new_op($2));
  3663. }
  3664. | KW_NEW predefined_type '(' optional_const_expr_comma ')' %prec UNARY
  3665. {
  3666. $$ = new CPPExpression(CPPExpression::new_op($2, $4));
  3667. }
  3668. | KW_TYPEID '(' full_type ')'
  3669. {
  3670. CPPIdentifier ident("");
  3671. ident.add_name("std");
  3672. ident.add_name("type_info");
  3673. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3674. if (!std_type_info) {
  3675. yywarning("cannot use typeid before including <typeinfo>", @1);
  3676. }
  3677. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3678. }
  3679. | KW_TYPEID '(' const_expr ')'
  3680. {
  3681. CPPIdentifier ident("");
  3682. ident.add_name("std");
  3683. ident.add_name("type_info");
  3684. CPPType *std_type_info = ident.find_type(current_scope, global_scope, false, current_lexer);
  3685. if (!std_type_info) {
  3686. yywarning("cannot use typeid before including <typeinfo>", @1);
  3687. }
  3688. $$ = new CPPExpression(CPPExpression::typeid_op($3, std_type_info));
  3689. }
  3690. | '!' const_expr %prec UNARY
  3691. {
  3692. $$ = new CPPExpression(UNARY_NOT, $2);
  3693. }
  3694. | '~' const_expr %prec UNARY
  3695. {
  3696. $$ = new CPPExpression(UNARY_NEGATE, $2);
  3697. }
  3698. | '-' const_expr %prec UNARY
  3699. {
  3700. $$ = new CPPExpression(UNARY_MINUS, $2);
  3701. }
  3702. | '+' const_expr %prec UNARY
  3703. {
  3704. $$ = new CPPExpression(UNARY_PLUS, $2);
  3705. }
  3706. | '&' const_expr %prec UNARY
  3707. {
  3708. $$ = new CPPExpression(UNARY_REF, $2);
  3709. }
  3710. | formal_const_expr '*' const_expr
  3711. {
  3712. $$ = new CPPExpression('*', $1, $3);
  3713. }
  3714. | formal_const_expr '/' const_expr
  3715. {
  3716. $$ = new CPPExpression('/', $1, $3);
  3717. }
  3718. | formal_const_expr '%' const_expr
  3719. {
  3720. $$ = new CPPExpression('%', $1, $3);
  3721. }
  3722. | formal_const_expr '+' const_expr
  3723. {
  3724. $$ = new CPPExpression('+', $1, $3);
  3725. }
  3726. | formal_const_expr '-' const_expr
  3727. {
  3728. $$ = new CPPExpression('-', $1, $3);
  3729. }
  3730. | formal_const_expr '|' const_expr
  3731. {
  3732. $$ = new CPPExpression('|', $1, $3);
  3733. }
  3734. | formal_const_expr '^' const_expr
  3735. {
  3736. $$ = new CPPExpression('^', $1, $3);
  3737. }
  3738. | formal_const_expr '&' const_expr
  3739. {
  3740. $$ = new CPPExpression('&', $1, $3);
  3741. }
  3742. | formal_const_expr OROR const_expr
  3743. {
  3744. $$ = new CPPExpression(OROR, $1, $3);
  3745. }
  3746. | formal_const_expr ANDAND const_expr
  3747. {
  3748. $$ = new CPPExpression(ANDAND, $1, $3);
  3749. }
  3750. | formal_const_expr EQCOMPARE const_expr
  3751. {
  3752. $$ = new CPPExpression(EQCOMPARE, $1, $3);
  3753. }
  3754. | formal_const_expr NECOMPARE const_expr
  3755. {
  3756. $$ = new CPPExpression(NECOMPARE, $1, $3);
  3757. }
  3758. | formal_const_expr LECOMPARE const_expr
  3759. {
  3760. $$ = new CPPExpression(LECOMPARE, $1, $3);
  3761. }
  3762. | formal_const_expr GECOMPARE const_expr
  3763. {
  3764. $$ = new CPPExpression(GECOMPARE, $1, $3);
  3765. }
  3766. | formal_const_expr '<' const_expr
  3767. {
  3768. $$ = new CPPExpression('<', $1, $3);
  3769. }
  3770. | formal_const_expr '>' const_expr
  3771. {
  3772. $$ = new CPPExpression('>', $1, $3);
  3773. }
  3774. | formal_const_expr LSHIFT const_expr
  3775. {
  3776. $$ = new CPPExpression(LSHIFT, $1, $3);
  3777. }
  3778. | formal_const_expr RSHIFT const_expr
  3779. {
  3780. $$ = new CPPExpression(RSHIFT, $1, $3);
  3781. }
  3782. | formal_const_expr '?' const_expr ':' const_expr
  3783. {
  3784. $$ = new CPPExpression('?', $1, $3, $5);
  3785. }
  3786. | formal_const_expr '[' const_expr ']'
  3787. {
  3788. $$ = new CPPExpression('[', $1, $3);
  3789. }
  3790. | formal_const_expr '(' const_expr_comma ')'
  3791. {
  3792. $$ = new CPPExpression('f', $1, $3);
  3793. }
  3794. | formal_const_expr '(' ')'
  3795. {
  3796. $$ = new CPPExpression('f', $1);
  3797. }
  3798. | formal_const_expr '.' name
  3799. {
  3800. $$ = new CPPExpression('.', $1, new CPPExpression($3, current_scope, global_scope, current_lexer));
  3801. }
  3802. | formal_const_expr POINTSAT const_expr
  3803. {
  3804. $$ = new CPPExpression(POINTSAT, $1, $3);
  3805. }
  3806. | '(' const_expr_comma ')'
  3807. {
  3808. $$ = $2;
  3809. }
  3810. ;
  3811. formal_const_operand:
  3812. INTEGER
  3813. {
  3814. $$ = new CPPExpression($1);
  3815. }
  3816. | KW_TRUE
  3817. {
  3818. $$ = new CPPExpression(true);
  3819. }
  3820. | KW_FALSE
  3821. {
  3822. $$ = new CPPExpression(false);
  3823. }
  3824. | CHAR_TOK
  3825. {
  3826. $$ = new CPPExpression($1);
  3827. }
  3828. | REAL
  3829. {
  3830. $$ = new CPPExpression($1);
  3831. }
  3832. | string_literal
  3833. {
  3834. $$ = $1;
  3835. }
  3836. | CUSTOM_LITERAL
  3837. {
  3838. $$ = $1;
  3839. }
  3840. | IDENTIFIER
  3841. {
  3842. $$ = new CPPExpression($1, current_scope, global_scope, current_lexer);
  3843. }
  3844. | KW_FINAL
  3845. {
  3846. // A variable named "final". C++11 explicitly permits this.
  3847. CPPIdentifier *ident = new CPPIdentifier("final", @1);
  3848. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3849. }
  3850. | KW_OVERRIDE
  3851. {
  3852. // A variable named "override". C++11 explicitly permits this.
  3853. CPPIdentifier *ident = new CPPIdentifier("override", @1);
  3854. $$ = new CPPExpression(ident, current_scope, global_scope, current_lexer);
  3855. }
  3856. | KW_NULLPTR
  3857. {
  3858. $$ = new CPPExpression(CPPExpression::get_nullptr());
  3859. }
  3860. ;
  3861. /* The contents of the [] list preceding a lambda expression. */
  3862. capture_list:
  3863. empty
  3864. {
  3865. $$ = new CPPClosureType();
  3866. }
  3867. | '='
  3868. {
  3869. $$ = new CPPClosureType(CPPClosureType::CT_by_value);
  3870. }
  3871. | '&'
  3872. {
  3873. $$ = new CPPClosureType(CPPClosureType::CT_by_reference);
  3874. }
  3875. | capture maybe_initialize
  3876. {
  3877. $$ = new CPPClosureType();
  3878. $1->_initializer = $2;
  3879. $$->_captures.push_back(*$1);
  3880. delete $1;
  3881. }
  3882. | capture_list ',' capture maybe_initialize
  3883. {
  3884. $$ = $1;
  3885. $3->_initializer = $4;
  3886. $$->_captures.push_back(*$3);
  3887. delete $3;
  3888. }
  3889. ;
  3890. capture:
  3891. '&' name
  3892. {
  3893. $$ = new CPPClosureType::Capture;
  3894. $$->_name = $2->get_simple_name();
  3895. $$->_type = CPPClosureType::CT_by_reference;
  3896. }
  3897. | '&' name ELLIPSIS
  3898. {
  3899. $$ = new CPPClosureType::Capture;
  3900. $$->_name = $2->get_simple_name();
  3901. $$->_type = CPPClosureType::CT_by_reference;
  3902. }
  3903. | name
  3904. {
  3905. $$ = new CPPClosureType::Capture;
  3906. $$->_name = $1->get_simple_name();
  3907. if ($$->_name == "this") {
  3908. $$->_type = CPPClosureType::CT_by_reference;
  3909. } else {
  3910. $$->_type = CPPClosureType::CT_by_value;
  3911. }
  3912. }
  3913. | '*' name
  3914. {
  3915. $$ = new CPPClosureType::Capture;
  3916. $$->_name = $2->get_simple_name();
  3917. $$->_type = CPPClosureType::CT_by_value;
  3918. if ($$->_name != "this") {
  3919. yywarning("only capture name 'this' may be preceded by an asterisk", @2);
  3920. }
  3921. }
  3922. ;
  3923. class_derivation_name:
  3924. name
  3925. {
  3926. CPPType *type = $1->find_type(current_scope, global_scope, true);
  3927. if (type == nullptr) {
  3928. type = CPPType::new_type(new CPPTBDType($1));
  3929. }
  3930. $$ = type;
  3931. }
  3932. | KW_TYPENAME name
  3933. {
  3934. $$ = CPPType::new_type(new CPPTBDType($2));
  3935. }
  3936. | name ELLIPSIS
  3937. {
  3938. CPPClassTemplateParameter *ctp = new CPPClassTemplateParameter($1);
  3939. ctp->_packed = true;
  3940. $$ = CPPType::new_type(ctp);
  3941. }
  3942. ;
  3943. /*
  3944. typedefname:
  3945. TYPENAME_IDENTIFIER
  3946. {
  3947. CPPType *type = $1->find_type(current_scope, global_scope, false, current_lexer);
  3948. if (type == NULL) {
  3949. yyerror(string("internal error resolving type ") + $1->get_fully_scoped_name(), @1);
  3950. }
  3951. assert(type != NULL);
  3952. $$ = type;
  3953. }
  3954. | KW_TYPENAME name
  3955. {
  3956. $$ = CPPType::new_type(new CPPTBDType($2));
  3957. }
  3958. ;
  3959. */
  3960. /* Note that we should also accept "final" and "override" as valid names,
  3961. * according to C++11. */
  3962. name:
  3963. IDENTIFIER
  3964. {
  3965. $$ = $1;
  3966. }
  3967. | TYPENAME_IDENTIFIER
  3968. {
  3969. $$ = $1;
  3970. }
  3971. | TYPEPACK_IDENTIFIER
  3972. {
  3973. $$ = $1;
  3974. }
  3975. | KW_FINAL
  3976. {
  3977. $$ = new CPPIdentifier("final", @1);
  3978. }
  3979. | KW_OVERRIDE
  3980. {
  3981. $$ = new CPPIdentifier("override", @1);
  3982. }
  3983. | KW_SIGNED
  3984. {
  3985. // This is not a keyword in Python, so it is useful to be able to use this
  3986. // in MAKE_PROPERTY definitions, etc.
  3987. $$ = new CPPIdentifier("signed", @1);
  3988. }
  3989. | KW_FLOAT
  3990. {
  3991. $$ = new CPPIdentifier("float", @1);
  3992. }
  3993. | KW_PUBLIC
  3994. {
  3995. $$ = new CPPIdentifier("public", @1);
  3996. }
  3997. | KW_PRIVATE
  3998. {
  3999. $$ = new CPPIdentifier("private", @1);
  4000. }
  4001. | KW_STATIC
  4002. {
  4003. $$ = new CPPIdentifier("static", @1);
  4004. }
  4005. | KW_DEFAULT
  4006. {
  4007. $$ = new CPPIdentifier("default", @1);
  4008. }
  4009. ;
  4010. /* A variant on name that's used for structs, where we disallow final
  4011. * to disambiguate the case of struct A final {};
  4012. */
  4013. name_no_final:
  4014. IDENTIFIER
  4015. {
  4016. $$ = $1;
  4017. }
  4018. | TYPENAME_IDENTIFIER
  4019. {
  4020. $$ = $1;
  4021. }
  4022. | TYPEPACK_IDENTIFIER
  4023. {
  4024. $$ = $1;
  4025. }
  4026. | KW_OVERRIDE
  4027. {
  4028. $$ = new CPPIdentifier("override", @1);
  4029. }
  4030. ;
  4031. string_literal:
  4032. SIMPLE_STRING
  4033. {
  4034. $$ = new CPPExpression($1);
  4035. }
  4036. | STRING_LITERAL
  4037. {
  4038. $$ = $1;
  4039. }
  4040. | string_literal SIMPLE_STRING
  4041. {
  4042. // The right string takes on the literal type of the left.
  4043. $$ = $1;
  4044. $$->_str += $2;
  4045. }
  4046. | string_literal STRING_LITERAL
  4047. {
  4048. // We have to check that the two literal types match up.
  4049. $$ = $1;
  4050. if ($2->_type != CPPExpression::T_string && $2->_type != $1->_type) {
  4051. yywarning("cannot concatenate two string literals of different types", @$);
  4052. }
  4053. $$->_str += $2->_str;
  4054. }
  4055. ;
  4056. empty:
  4057. ;