cppBison.yxx 73 KB

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