c-exp.y 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515
  1. /* YACC parser for C expressions, for GDB.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Parse a C expression from text in a string,
  15. and return the result as a struct expression pointer.
  16. That structure contains arithmetic operations in reverse polish,
  17. with constants represented by operations that are followed by special data.
  18. See expression.h for the details of the format.
  19. What is important here is that it can be built up sequentially
  20. during the process of parsing; the lower levels of the tree always
  21. come first in the result.
  22. Note that malloc's and realloc's in this file are transformed to
  23. xmalloc and xrealloc respectively by the same sed command in the
  24. makefile that remaps any other malloc/realloc inserted by the parser
  25. generator. Doing this with #defines and trying to control the interaction
  26. with include files (<malloc.h> and <stdlib.h> for example) just became
  27. too messy, particularly when such includes can be inserted at random
  28. times by the parser generator. */
  29. %{
  30. #include "defs.h"
  31. #include <ctype.h>
  32. #include "expression.h"
  33. #include "value.h"
  34. #include "parser-defs.h"
  35. #include "language.h"
  36. #include "c-lang.h"
  37. #include "c-support.h"
  38. #include "bfd.h" /* Required by objfiles.h. */
  39. #include "symfile.h" /* Required by objfiles.h. */
  40. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  41. #include "charset.h"
  42. #include "block.h"
  43. #include "cp-support.h"
  44. #include "macroscope.h"
  45. #include "objc-lang.h"
  46. #include "typeprint.h"
  47. #include "cp-abi.h"
  48. #include "type-stack.h"
  49. #include "target-float.h"
  50. #include "c-exp.h"
  51. #define parse_type(ps) builtin_type (ps->gdbarch ())
  52. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  53. etc). */
  54. #define GDB_YY_REMAP_PREFIX c_
  55. #include "yy-remap.h"
  56. /* The state of the parser, used internally when we are parsing the
  57. expression. */
  58. static struct parser_state *pstate = NULL;
  59. /* Data that must be held for the duration of a parse. */
  60. struct c_parse_state
  61. {
  62. /* These are used to hold type lists and type stacks that are
  63. allocated during the parse. */
  64. std::vector<std::unique_ptr<std::vector<struct type *>>> type_lists;
  65. std::vector<std::unique_ptr<struct type_stack>> type_stacks;
  66. /* Storage for some strings allocated during the parse. */
  67. std::vector<gdb::unique_xmalloc_ptr<char>> strings;
  68. /* When we find that lexptr (the global var defined in parse.c) is
  69. pointing at a macro invocation, we expand the invocation, and call
  70. scan_macro_expansion to save the old lexptr here and point lexptr
  71. into the expanded text. When we reach the end of that, we call
  72. end_macro_expansion to pop back to the value we saved here. The
  73. macro expansion code promises to return only fully-expanded text,
  74. so we don't need to "push" more than one level.
  75. This is disgusting, of course. It would be cleaner to do all macro
  76. expansion beforehand, and then hand that to lexptr. But we don't
  77. really know where the expression ends. Remember, in a command like
  78. (gdb) break *ADDRESS if CONDITION
  79. we evaluate ADDRESS in the scope of the current frame, but we
  80. evaluate CONDITION in the scope of the breakpoint's location. So
  81. it's simply wrong to try to macro-expand the whole thing at once. */
  82. const char *macro_original_text = nullptr;
  83. /* We save all intermediate macro expansions on this obstack for the
  84. duration of a single parse. The expansion text may sometimes have
  85. to live past the end of the expansion, due to yacc lookahead.
  86. Rather than try to be clever about saving the data for a single
  87. token, we simply keep it all and delete it after parsing has
  88. completed. */
  89. auto_obstack expansion_obstack;
  90. /* The type stack. */
  91. struct type_stack type_stack;
  92. };
  93. /* This is set and cleared in c_parse. */
  94. static struct c_parse_state *cpstate;
  95. int yyparse (void);
  96. static int yylex (void);
  97. static void yyerror (const char *);
  98. static int type_aggregate_p (struct type *);
  99. using namespace expr;
  100. %}
  101. /* Although the yacc "value" of an expression is not used,
  102. since the result is stored in the structure being created,
  103. other node types do have values. */
  104. %union
  105. {
  106. LONGEST lval;
  107. struct {
  108. LONGEST val;
  109. struct type *type;
  110. } typed_val_int;
  111. struct {
  112. gdb_byte val[16];
  113. struct type *type;
  114. } typed_val_float;
  115. struct type *tval;
  116. struct stoken sval;
  117. struct typed_stoken tsval;
  118. struct ttype tsym;
  119. struct symtoken ssym;
  120. int voidval;
  121. const struct block *bval;
  122. enum exp_opcode opcode;
  123. struct stoken_vector svec;
  124. std::vector<struct type *> *tvec;
  125. struct type_stack *type_stack;
  126. struct objc_class_str theclass;
  127. }
  128. %{
  129. /* YYSTYPE gets defined by %union */
  130. static int parse_number (struct parser_state *par_state,
  131. const char *, int, int, YYSTYPE *);
  132. static struct stoken operator_stoken (const char *);
  133. static struct stoken typename_stoken (const char *);
  134. static void check_parameter_typelist (std::vector<struct type *> *);
  135. #if defined(YYBISON) && YYBISON < 30800
  136. static void c_print_token (FILE *file, int type, YYSTYPE value);
  137. #define YYPRINT(FILE, TYPE, VALUE) c_print_token (FILE, TYPE, VALUE)
  138. #endif
  139. %}
  140. %type <voidval> exp exp1 type_exp start variable qualified_name lcurly function_method
  141. %type <lval> rcurly
  142. %type <tval> type typebase scalar_type
  143. %type <tvec> nonempty_typelist func_mod parameter_typelist
  144. /* %type <bval> block */
  145. /* Fancy type parsing. */
  146. %type <tval> ptype
  147. %type <lval> array_mod
  148. %type <tval> conversion_type_id
  149. %type <type_stack> ptr_operator_ts abs_decl direct_abs_decl
  150. %token <typed_val_int> INT COMPLEX_INT
  151. %token <typed_val_float> FLOAT COMPLEX_FLOAT
  152. /* Both NAME and TYPENAME tokens represent symbols in the input,
  153. and both convey their data as strings.
  154. But a TYPENAME is a string that happens to be defined as a typedef
  155. or builtin type name (such as int or char)
  156. and a NAME is any other symbol.
  157. Contexts where this distinction is not important can use the
  158. nonterminal "name", which matches either NAME or TYPENAME. */
  159. %token <tsval> STRING
  160. %token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
  161. %token SELECTOR /* ObjC "@selector" pseudo-operator */
  162. %token <tsval> CHAR
  163. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  164. %token <ssym> UNKNOWN_CPP_NAME
  165. %token <voidval> COMPLETE
  166. %token <tsym> TYPENAME
  167. %token <theclass> CLASSNAME /* ObjC Class name */
  168. %type <sval> name field_name
  169. %type <svec> string_exp
  170. %type <ssym> name_not_typename
  171. %type <tsym> type_name
  172. /* This is like a '[' token, but is only generated when parsing
  173. Objective C. This lets us reuse the same parser without
  174. erroneously parsing ObjC-specific expressions in C. */
  175. %token OBJC_LBRAC
  176. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  177. but which would parse as a valid number in the current input radix.
  178. E.g. "c" when input_radix==16. Depending on the parse, it will be
  179. turned into a name or into a number. */
  180. %token <ssym> NAME_OR_INT
  181. %token OPERATOR
  182. %token STRUCT CLASS UNION ENUM SIZEOF ALIGNOF UNSIGNED COLONCOLON
  183. %token TEMPLATE
  184. %token ERROR
  185. %token NEW DELETE
  186. %type <sval> oper
  187. %token REINTERPRET_CAST DYNAMIC_CAST STATIC_CAST CONST_CAST
  188. %token ENTRY
  189. %token TYPEOF
  190. %token DECLTYPE
  191. %token TYPEID
  192. /* Special type cases, put in to allow the parser to distinguish different
  193. legal basetypes. */
  194. %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
  195. %token RESTRICT ATOMIC
  196. %token FLOAT_KEYWORD COMPLEX
  197. %token <sval> DOLLAR_VARIABLE
  198. %token <opcode> ASSIGN_MODIFY
  199. /* C++ */
  200. %token TRUEKEYWORD
  201. %token FALSEKEYWORD
  202. %left ','
  203. %left ABOVE_COMMA
  204. %right '=' ASSIGN_MODIFY
  205. %right '?'
  206. %left OROR
  207. %left ANDAND
  208. %left '|'
  209. %left '^'
  210. %left '&'
  211. %left EQUAL NOTEQUAL
  212. %left '<' '>' LEQ GEQ
  213. %left LSH RSH
  214. %left '@'
  215. %left '+' '-'
  216. %left '*' '/' '%'
  217. %right UNARY INCREMENT DECREMENT
  218. %right ARROW ARROW_STAR '.' DOT_STAR '[' OBJC_LBRAC '('
  219. %token <ssym> BLOCKNAME
  220. %token <bval> FILENAME
  221. %type <bval> block
  222. %left COLONCOLON
  223. %token DOTDOTDOT
  224. %%
  225. start : exp1
  226. | type_exp
  227. ;
  228. type_exp: type
  229. {
  230. pstate->push_new<type_operation> ($1);
  231. }
  232. | TYPEOF '(' exp ')'
  233. {
  234. pstate->wrap<typeof_operation> ();
  235. }
  236. | TYPEOF '(' type ')'
  237. {
  238. pstate->push_new<type_operation> ($3);
  239. }
  240. | DECLTYPE '(' exp ')'
  241. {
  242. pstate->wrap<decltype_operation> ();
  243. }
  244. ;
  245. /* Expressions, including the comma operator. */
  246. exp1 : exp
  247. | exp1 ',' exp
  248. { pstate->wrap2<comma_operation> (); }
  249. ;
  250. /* Expressions, not including the comma operator. */
  251. exp : '*' exp %prec UNARY
  252. { pstate->wrap<unop_ind_operation> (); }
  253. ;
  254. exp : '&' exp %prec UNARY
  255. { pstate->wrap<unop_addr_operation> (); }
  256. ;
  257. exp : '-' exp %prec UNARY
  258. { pstate->wrap<unary_neg_operation> (); }
  259. ;
  260. exp : '+' exp %prec UNARY
  261. { pstate->wrap<unary_plus_operation> (); }
  262. ;
  263. exp : '!' exp %prec UNARY
  264. {
  265. if (pstate->language ()->la_language
  266. == language_opencl)
  267. pstate->wrap<opencl_not_operation> ();
  268. else
  269. pstate->wrap<unary_logical_not_operation> ();
  270. }
  271. ;
  272. exp : '~' exp %prec UNARY
  273. { pstate->wrap<unary_complement_operation> (); }
  274. ;
  275. exp : INCREMENT exp %prec UNARY
  276. { pstate->wrap<preinc_operation> (); }
  277. ;
  278. exp : DECREMENT exp %prec UNARY
  279. { pstate->wrap<predec_operation> (); }
  280. ;
  281. exp : exp INCREMENT %prec UNARY
  282. { pstate->wrap<postinc_operation> (); }
  283. ;
  284. exp : exp DECREMENT %prec UNARY
  285. { pstate->wrap<postdec_operation> (); }
  286. ;
  287. exp : TYPEID '(' exp ')' %prec UNARY
  288. { pstate->wrap<typeid_operation> (); }
  289. ;
  290. exp : TYPEID '(' type_exp ')' %prec UNARY
  291. { pstate->wrap<typeid_operation> (); }
  292. ;
  293. exp : SIZEOF exp %prec UNARY
  294. { pstate->wrap<unop_sizeof_operation> (); }
  295. ;
  296. exp : ALIGNOF '(' type_exp ')' %prec UNARY
  297. { pstate->wrap<unop_alignof_operation> (); }
  298. ;
  299. exp : exp ARROW field_name
  300. {
  301. pstate->push_new<structop_ptr_operation>
  302. (pstate->pop (), copy_name ($3));
  303. }
  304. ;
  305. exp : exp ARROW field_name COMPLETE
  306. {
  307. structop_base_operation *op
  308. = new structop_ptr_operation (pstate->pop (),
  309. copy_name ($3));
  310. pstate->mark_struct_expression (op);
  311. pstate->push (operation_up (op));
  312. }
  313. ;
  314. exp : exp ARROW COMPLETE
  315. {
  316. structop_base_operation *op
  317. = new structop_ptr_operation (pstate->pop (), "");
  318. pstate->mark_struct_expression (op);
  319. pstate->push (operation_up (op));
  320. }
  321. ;
  322. exp : exp ARROW '~' name
  323. {
  324. pstate->push_new<structop_ptr_operation>
  325. (pstate->pop (), "~" + copy_name ($4));
  326. }
  327. ;
  328. exp : exp ARROW '~' name COMPLETE
  329. {
  330. structop_base_operation *op
  331. = new structop_ptr_operation (pstate->pop (),
  332. "~" + copy_name ($4));
  333. pstate->mark_struct_expression (op);
  334. pstate->push (operation_up (op));
  335. }
  336. ;
  337. exp : exp ARROW qualified_name
  338. { /* exp->type::name becomes exp->*(&type::name) */
  339. /* Note: this doesn't work if name is a
  340. static member! FIXME */
  341. pstate->wrap<unop_addr_operation> ();
  342. pstate->wrap2<structop_mptr_operation> (); }
  343. ;
  344. exp : exp ARROW_STAR exp
  345. { pstate->wrap2<structop_mptr_operation> (); }
  346. ;
  347. exp : exp '.' field_name
  348. {
  349. if (pstate->language ()->la_language
  350. == language_opencl)
  351. pstate->push_new<opencl_structop_operation>
  352. (pstate->pop (), copy_name ($3));
  353. else
  354. pstate->push_new<structop_operation>
  355. (pstate->pop (), copy_name ($3));
  356. }
  357. ;
  358. exp : exp '.' field_name COMPLETE
  359. {
  360. structop_base_operation *op
  361. = new structop_operation (pstate->pop (),
  362. copy_name ($3));
  363. pstate->mark_struct_expression (op);
  364. pstate->push (operation_up (op));
  365. }
  366. ;
  367. exp : exp '.' COMPLETE
  368. {
  369. structop_base_operation *op
  370. = new structop_operation (pstate->pop (), "");
  371. pstate->mark_struct_expression (op);
  372. pstate->push (operation_up (op));
  373. }
  374. ;
  375. exp : exp '.' '~' name
  376. {
  377. pstate->push_new<structop_operation>
  378. (pstate->pop (), "~" + copy_name ($4));
  379. }
  380. ;
  381. exp : exp '.' '~' name COMPLETE
  382. {
  383. structop_base_operation *op
  384. = new structop_operation (pstate->pop (),
  385. "~" + copy_name ($4));
  386. pstate->mark_struct_expression (op);
  387. pstate->push (operation_up (op));
  388. }
  389. ;
  390. exp : exp '.' qualified_name
  391. { /* exp.type::name becomes exp.*(&type::name) */
  392. /* Note: this doesn't work if name is a
  393. static member! FIXME */
  394. pstate->wrap<unop_addr_operation> ();
  395. pstate->wrap2<structop_member_operation> (); }
  396. ;
  397. exp : exp DOT_STAR exp
  398. { pstate->wrap2<structop_member_operation> (); }
  399. ;
  400. exp : exp '[' exp1 ']'
  401. { pstate->wrap2<subscript_operation> (); }
  402. ;
  403. exp : exp OBJC_LBRAC exp1 ']'
  404. { pstate->wrap2<subscript_operation> (); }
  405. ;
  406. /*
  407. * The rules below parse ObjC message calls of the form:
  408. * '[' target selector {':' argument}* ']'
  409. */
  410. exp : OBJC_LBRAC TYPENAME
  411. {
  412. CORE_ADDR theclass;
  413. std::string copy = copy_name ($2.stoken);
  414. theclass = lookup_objc_class (pstate->gdbarch (),
  415. copy.c_str ());
  416. if (theclass == 0)
  417. error (_("%s is not an ObjC Class"),
  418. copy.c_str ());
  419. pstate->push_new<long_const_operation>
  420. (parse_type (pstate)->builtin_int,
  421. (LONGEST) theclass);
  422. start_msglist();
  423. }
  424. msglist ']'
  425. { end_msglist (pstate); }
  426. ;
  427. exp : OBJC_LBRAC CLASSNAME
  428. {
  429. pstate->push_new<long_const_operation>
  430. (parse_type (pstate)->builtin_int,
  431. (LONGEST) $2.theclass);
  432. start_msglist();
  433. }
  434. msglist ']'
  435. { end_msglist (pstate); }
  436. ;
  437. exp : OBJC_LBRAC exp
  438. { start_msglist(); }
  439. msglist ']'
  440. { end_msglist (pstate); }
  441. ;
  442. msglist : name
  443. { add_msglist(&$1, 0); }
  444. | msgarglist
  445. ;
  446. msgarglist : msgarg
  447. | msgarglist msgarg
  448. ;
  449. msgarg : name ':' exp
  450. { add_msglist(&$1, 1); }
  451. | ':' exp /* Unnamed arg. */
  452. { add_msglist(0, 1); }
  453. | ',' exp /* Variable number of args. */
  454. { add_msglist(0, 0); }
  455. ;
  456. exp : exp '('
  457. /* This is to save the value of arglist_len
  458. being accumulated by an outer function call. */
  459. { pstate->start_arglist (); }
  460. arglist ')' %prec ARROW
  461. {
  462. std::vector<operation_up> args
  463. = pstate->pop_vector (pstate->end_arglist ());
  464. pstate->push_new<funcall_operation>
  465. (pstate->pop (), std::move (args));
  466. }
  467. ;
  468. /* This is here to disambiguate with the production for
  469. "func()::static_var" further below, which uses
  470. function_method_void. */
  471. exp : exp '(' ')' %prec ARROW
  472. {
  473. pstate->push_new<funcall_operation>
  474. (pstate->pop (), std::vector<operation_up> ());
  475. }
  476. ;
  477. exp : UNKNOWN_CPP_NAME '('
  478. {
  479. /* This could potentially be a an argument defined
  480. lookup function (Koenig). */
  481. /* This is to save the value of arglist_len
  482. being accumulated by an outer function call. */
  483. pstate->start_arglist ();
  484. }
  485. arglist ')' %prec ARROW
  486. {
  487. std::vector<operation_up> args
  488. = pstate->pop_vector (pstate->end_arglist ());
  489. pstate->push_new<adl_func_operation>
  490. (copy_name ($1.stoken),
  491. pstate->expression_context_block,
  492. std::move (args));
  493. }
  494. ;
  495. lcurly : '{'
  496. { pstate->start_arglist (); }
  497. ;
  498. arglist :
  499. ;
  500. arglist : exp
  501. { pstate->arglist_len = 1; }
  502. ;
  503. arglist : arglist ',' exp %prec ABOVE_COMMA
  504. { pstate->arglist_len++; }
  505. ;
  506. function_method: exp '(' parameter_typelist ')' const_or_volatile
  507. {
  508. std::vector<struct type *> *type_list = $3;
  509. /* Save the const/volatile qualifiers as
  510. recorded by the const_or_volatile
  511. production's actions. */
  512. type_instance_flags flags
  513. = (cpstate->type_stack
  514. .follow_type_instance_flags ());
  515. pstate->push_new<type_instance_operation>
  516. (flags, std::move (*type_list),
  517. pstate->pop ());
  518. }
  519. ;
  520. function_method_void: exp '(' ')' const_or_volatile
  521. {
  522. type_instance_flags flags
  523. = (cpstate->type_stack
  524. .follow_type_instance_flags ());
  525. pstate->push_new<type_instance_operation>
  526. (flags, std::vector<type *> (), pstate->pop ());
  527. }
  528. ;
  529. exp : function_method
  530. ;
  531. /* Normally we must interpret "func()" as a function call, instead of
  532. a type. The user needs to write func(void) to disambiguate.
  533. However, in the "func()::static_var" case, there's no
  534. ambiguity. */
  535. function_method_void_or_typelist: function_method
  536. | function_method_void
  537. ;
  538. exp : function_method_void_or_typelist COLONCOLON name
  539. {
  540. pstate->push_new<func_static_var_operation>
  541. (pstate->pop (), copy_name ($3));
  542. }
  543. ;
  544. rcurly : '}'
  545. { $$ = pstate->end_arglist () - 1; }
  546. ;
  547. exp : lcurly arglist rcurly %prec ARROW
  548. {
  549. std::vector<operation_up> args
  550. = pstate->pop_vector ($3 + 1);
  551. pstate->push_new<array_operation> (0, $3,
  552. std::move (args));
  553. }
  554. ;
  555. exp : lcurly type_exp rcurly exp %prec UNARY
  556. { pstate->wrap2<unop_memval_type_operation> (); }
  557. ;
  558. exp : '(' type_exp ')' exp %prec UNARY
  559. {
  560. if (pstate->language ()->la_language
  561. == language_opencl)
  562. pstate->wrap2<opencl_cast_type_operation> ();
  563. else
  564. pstate->wrap2<unop_cast_type_operation> ();
  565. }
  566. ;
  567. exp : '(' exp1 ')'
  568. { }
  569. ;
  570. /* Binary operators in order of decreasing precedence. */
  571. exp : exp '@' exp
  572. { pstate->wrap2<repeat_operation> (); }
  573. ;
  574. exp : exp '*' exp
  575. { pstate->wrap2<mul_operation> (); }
  576. ;
  577. exp : exp '/' exp
  578. { pstate->wrap2<div_operation> (); }
  579. ;
  580. exp : exp '%' exp
  581. { pstate->wrap2<rem_operation> (); }
  582. ;
  583. exp : exp '+' exp
  584. { pstate->wrap2<add_operation> (); }
  585. ;
  586. exp : exp '-' exp
  587. { pstate->wrap2<sub_operation> (); }
  588. ;
  589. exp : exp LSH exp
  590. { pstate->wrap2<lsh_operation> (); }
  591. ;
  592. exp : exp RSH exp
  593. { pstate->wrap2<rsh_operation> (); }
  594. ;
  595. exp : exp EQUAL exp
  596. {
  597. if (pstate->language ()->la_language
  598. == language_opencl)
  599. pstate->wrap2<opencl_equal_operation> ();
  600. else
  601. pstate->wrap2<equal_operation> ();
  602. }
  603. ;
  604. exp : exp NOTEQUAL exp
  605. {
  606. if (pstate->language ()->la_language
  607. == language_opencl)
  608. pstate->wrap2<opencl_notequal_operation> ();
  609. else
  610. pstate->wrap2<notequal_operation> ();
  611. }
  612. ;
  613. exp : exp LEQ exp
  614. {
  615. if (pstate->language ()->la_language
  616. == language_opencl)
  617. pstate->wrap2<opencl_leq_operation> ();
  618. else
  619. pstate->wrap2<leq_operation> ();
  620. }
  621. ;
  622. exp : exp GEQ exp
  623. {
  624. if (pstate->language ()->la_language
  625. == language_opencl)
  626. pstate->wrap2<opencl_geq_operation> ();
  627. else
  628. pstate->wrap2<geq_operation> ();
  629. }
  630. ;
  631. exp : exp '<' exp
  632. {
  633. if (pstate->language ()->la_language
  634. == language_opencl)
  635. pstate->wrap2<opencl_less_operation> ();
  636. else
  637. pstate->wrap2<less_operation> ();
  638. }
  639. ;
  640. exp : exp '>' exp
  641. {
  642. if (pstate->language ()->la_language
  643. == language_opencl)
  644. pstate->wrap2<opencl_gtr_operation> ();
  645. else
  646. pstate->wrap2<gtr_operation> ();
  647. }
  648. ;
  649. exp : exp '&' exp
  650. { pstate->wrap2<bitwise_and_operation> (); }
  651. ;
  652. exp : exp '^' exp
  653. { pstate->wrap2<bitwise_xor_operation> (); }
  654. ;
  655. exp : exp '|' exp
  656. { pstate->wrap2<bitwise_ior_operation> (); }
  657. ;
  658. exp : exp ANDAND exp
  659. {
  660. if (pstate->language ()->la_language
  661. == language_opencl)
  662. {
  663. operation_up rhs = pstate->pop ();
  664. operation_up lhs = pstate->pop ();
  665. pstate->push_new<opencl_logical_binop_operation>
  666. (BINOP_LOGICAL_AND, std::move (lhs),
  667. std::move (rhs));
  668. }
  669. else
  670. pstate->wrap2<logical_and_operation> ();
  671. }
  672. ;
  673. exp : exp OROR exp
  674. {
  675. if (pstate->language ()->la_language
  676. == language_opencl)
  677. {
  678. operation_up rhs = pstate->pop ();
  679. operation_up lhs = pstate->pop ();
  680. pstate->push_new<opencl_logical_binop_operation>
  681. (BINOP_LOGICAL_OR, std::move (lhs),
  682. std::move (rhs));
  683. }
  684. else
  685. pstate->wrap2<logical_or_operation> ();
  686. }
  687. ;
  688. exp : exp '?' exp ':' exp %prec '?'
  689. {
  690. operation_up last = pstate->pop ();
  691. operation_up mid = pstate->pop ();
  692. operation_up first = pstate->pop ();
  693. if (pstate->language ()->la_language
  694. == language_opencl)
  695. pstate->push_new<opencl_ternop_cond_operation>
  696. (std::move (first), std::move (mid),
  697. std::move (last));
  698. else
  699. pstate->push_new<ternop_cond_operation>
  700. (std::move (first), std::move (mid),
  701. std::move (last));
  702. }
  703. ;
  704. exp : exp '=' exp
  705. {
  706. if (pstate->language ()->la_language
  707. == language_opencl)
  708. pstate->wrap2<opencl_assign_operation> ();
  709. else
  710. pstate->wrap2<assign_operation> ();
  711. }
  712. ;
  713. exp : exp ASSIGN_MODIFY exp
  714. {
  715. operation_up rhs = pstate->pop ();
  716. operation_up lhs = pstate->pop ();
  717. pstate->push_new<assign_modify_operation>
  718. ($2, std::move (lhs), std::move (rhs));
  719. }
  720. ;
  721. exp : INT
  722. {
  723. pstate->push_new<long_const_operation>
  724. ($1.type, $1.val);
  725. }
  726. ;
  727. exp : COMPLEX_INT
  728. {
  729. operation_up real
  730. = (make_operation<long_const_operation>
  731. (TYPE_TARGET_TYPE ($1.type), 0));
  732. operation_up imag
  733. = (make_operation<long_const_operation>
  734. (TYPE_TARGET_TYPE ($1.type), $1.val));
  735. pstate->push_new<complex_operation>
  736. (std::move (real), std::move (imag), $1.type);
  737. }
  738. ;
  739. exp : CHAR
  740. {
  741. struct stoken_vector vec;
  742. vec.len = 1;
  743. vec.tokens = &$1;
  744. pstate->push_c_string ($1.type, &vec);
  745. }
  746. ;
  747. exp : NAME_OR_INT
  748. { YYSTYPE val;
  749. parse_number (pstate, $1.stoken.ptr,
  750. $1.stoken.length, 0, &val);
  751. pstate->push_new<long_const_operation>
  752. (val.typed_val_int.type,
  753. val.typed_val_int.val);
  754. }
  755. ;
  756. exp : FLOAT
  757. {
  758. float_data data;
  759. std::copy (std::begin ($1.val), std::end ($1.val),
  760. std::begin (data));
  761. pstate->push_new<float_const_operation> ($1.type, data);
  762. }
  763. ;
  764. exp : COMPLEX_FLOAT
  765. {
  766. struct type *underlying
  767. = TYPE_TARGET_TYPE ($1.type);
  768. float_data val;
  769. target_float_from_host_double (val.data (),
  770. underlying, 0);
  771. operation_up real
  772. = (make_operation<float_const_operation>
  773. (underlying, val));
  774. std::copy (std::begin ($1.val), std::end ($1.val),
  775. std::begin (val));
  776. operation_up imag
  777. = (make_operation<float_const_operation>
  778. (underlying, val));
  779. pstate->push_new<complex_operation>
  780. (std::move (real), std::move (imag),
  781. $1.type);
  782. }
  783. ;
  784. exp : variable
  785. ;
  786. exp : DOLLAR_VARIABLE
  787. {
  788. pstate->push_dollar ($1);
  789. }
  790. ;
  791. exp : SELECTOR '(' name ')'
  792. {
  793. pstate->push_new<objc_selector_operation>
  794. (copy_name ($3));
  795. }
  796. ;
  797. exp : SIZEOF '(' type ')' %prec UNARY
  798. { struct type *type = $3;
  799. struct type *int_type
  800. = lookup_signed_typename (pstate->language (),
  801. "int");
  802. type = check_typedef (type);
  803. /* $5.3.3/2 of the C++ Standard (n3290 draft)
  804. says of sizeof: "When applied to a reference
  805. or a reference type, the result is the size of
  806. the referenced type." */
  807. if (TYPE_IS_REFERENCE (type))
  808. type = check_typedef (TYPE_TARGET_TYPE (type));
  809. pstate->push_new<long_const_operation>
  810. (int_type, TYPE_LENGTH (type));
  811. }
  812. ;
  813. exp : REINTERPRET_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
  814. { pstate->wrap2<reinterpret_cast_operation> (); }
  815. ;
  816. exp : STATIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
  817. { pstate->wrap2<unop_cast_type_operation> (); }
  818. ;
  819. exp : DYNAMIC_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
  820. { pstate->wrap2<dynamic_cast_operation> (); }
  821. ;
  822. exp : CONST_CAST '<' type_exp '>' '(' exp ')' %prec UNARY
  823. { /* We could do more error checking here, but
  824. it doesn't seem worthwhile. */
  825. pstate->wrap2<unop_cast_type_operation> (); }
  826. ;
  827. string_exp:
  828. STRING
  829. {
  830. /* We copy the string here, and not in the
  831. lexer, to guarantee that we do not leak a
  832. string. Note that we follow the
  833. NUL-termination convention of the
  834. lexer. */
  835. struct typed_stoken *vec = XNEW (struct typed_stoken);
  836. $$.len = 1;
  837. $$.tokens = vec;
  838. vec->type = $1.type;
  839. vec->length = $1.length;
  840. vec->ptr = (char *) malloc ($1.length + 1);
  841. memcpy (vec->ptr, $1.ptr, $1.length + 1);
  842. }
  843. | string_exp STRING
  844. {
  845. /* Note that we NUL-terminate here, but just
  846. for convenience. */
  847. char *p;
  848. ++$$.len;
  849. $$.tokens = XRESIZEVEC (struct typed_stoken,
  850. $$.tokens, $$.len);
  851. p = (char *) malloc ($2.length + 1);
  852. memcpy (p, $2.ptr, $2.length + 1);
  853. $$.tokens[$$.len - 1].type = $2.type;
  854. $$.tokens[$$.len - 1].length = $2.length;
  855. $$.tokens[$$.len - 1].ptr = p;
  856. }
  857. ;
  858. exp : string_exp
  859. {
  860. int i;
  861. c_string_type type = C_STRING;
  862. for (i = 0; i < $1.len; ++i)
  863. {
  864. switch ($1.tokens[i].type)
  865. {
  866. case C_STRING:
  867. break;
  868. case C_WIDE_STRING:
  869. case C_STRING_16:
  870. case C_STRING_32:
  871. if (type != C_STRING
  872. && type != $1.tokens[i].type)
  873. error (_("Undefined string concatenation."));
  874. type = (enum c_string_type_values) $1.tokens[i].type;
  875. break;
  876. default:
  877. /* internal error */
  878. internal_error (__FILE__, __LINE__,
  879. "unrecognized type in string concatenation");
  880. }
  881. }
  882. pstate->push_c_string (type, &$1);
  883. for (i = 0; i < $1.len; ++i)
  884. free ($1.tokens[i].ptr);
  885. free ($1.tokens);
  886. }
  887. ;
  888. exp : NSSTRING /* ObjC NextStep NSString constant
  889. * of the form '@' '"' string '"'.
  890. */
  891. {
  892. pstate->push_new<objc_nsstring_operation>
  893. (copy_name ($1));
  894. }
  895. ;
  896. /* C++. */
  897. exp : TRUEKEYWORD
  898. { pstate->push_new<long_const_operation>
  899. (parse_type (pstate)->builtin_bool, 1);
  900. }
  901. ;
  902. exp : FALSEKEYWORD
  903. { pstate->push_new<long_const_operation>
  904. (parse_type (pstate)->builtin_bool, 0);
  905. }
  906. ;
  907. /* end of C++. */
  908. block : BLOCKNAME
  909. {
  910. if ($1.sym.symbol)
  911. $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
  912. else
  913. error (_("No file or function \"%s\"."),
  914. copy_name ($1.stoken).c_str ());
  915. }
  916. | FILENAME
  917. {
  918. $$ = $1;
  919. }
  920. ;
  921. block : block COLONCOLON name
  922. {
  923. std::string copy = copy_name ($3);
  924. struct symbol *tem
  925. = lookup_symbol (copy.c_str (), $1,
  926. VAR_DOMAIN, NULL).symbol;
  927. if (!tem || tem->aclass () != LOC_BLOCK)
  928. error (_("No function \"%s\" in specified context."),
  929. copy.c_str ());
  930. $$ = SYMBOL_BLOCK_VALUE (tem); }
  931. ;
  932. variable: name_not_typename ENTRY
  933. { struct symbol *sym = $1.sym.symbol;
  934. if (sym == NULL || !sym->is_argument ()
  935. || !symbol_read_needs_frame (sym))
  936. error (_("@entry can be used only for function "
  937. "parameters, not for \"%s\""),
  938. copy_name ($1.stoken).c_str ());
  939. pstate->push_new<var_entry_value_operation> (sym);
  940. }
  941. ;
  942. variable: block COLONCOLON name
  943. {
  944. std::string copy = copy_name ($3);
  945. struct block_symbol sym
  946. = lookup_symbol (copy.c_str (), $1,
  947. VAR_DOMAIN, NULL);
  948. if (sym.symbol == 0)
  949. error (_("No symbol \"%s\" in specified context."),
  950. copy.c_str ());
  951. if (symbol_read_needs_frame (sym.symbol))
  952. pstate->block_tracker->update (sym);
  953. pstate->push_new<var_value_operation> (sym);
  954. }
  955. ;
  956. qualified_name: TYPENAME COLONCOLON name
  957. {
  958. struct type *type = $1.type;
  959. type = check_typedef (type);
  960. if (!type_aggregate_p (type))
  961. error (_("`%s' is not defined as an aggregate type."),
  962. TYPE_SAFE_NAME (type));
  963. pstate->push_new<scope_operation> (type,
  964. copy_name ($3));
  965. }
  966. | TYPENAME COLONCOLON '~' name
  967. {
  968. struct type *type = $1.type;
  969. type = check_typedef (type);
  970. if (!type_aggregate_p (type))
  971. error (_("`%s' is not defined as an aggregate type."),
  972. TYPE_SAFE_NAME (type));
  973. std::string name = "~" + std::string ($4.ptr,
  974. $4.length);
  975. /* Check for valid destructor name. */
  976. destructor_name_p (name.c_str (), $1.type);
  977. pstate->push_new<scope_operation> (type,
  978. std::move (name));
  979. }
  980. | TYPENAME COLONCOLON name COLONCOLON name
  981. {
  982. std::string copy = copy_name ($3);
  983. error (_("No type \"%s\" within class "
  984. "or namespace \"%s\"."),
  985. copy.c_str (), TYPE_SAFE_NAME ($1.type));
  986. }
  987. ;
  988. variable: qualified_name
  989. | COLONCOLON name_not_typename
  990. {
  991. std::string name = copy_name ($2.stoken);
  992. struct block_symbol sym
  993. = lookup_symbol (name.c_str (),
  994. (const struct block *) NULL,
  995. VAR_DOMAIN, NULL);
  996. pstate->push_symbol (name.c_str (), sym);
  997. }
  998. ;
  999. variable: name_not_typename
  1000. { struct block_symbol sym = $1.sym;
  1001. if (sym.symbol)
  1002. {
  1003. if (symbol_read_needs_frame (sym.symbol))
  1004. pstate->block_tracker->update (sym);
  1005. /* If we found a function, see if it's
  1006. an ifunc resolver that has the same
  1007. address as the ifunc symbol itself.
  1008. If so, prefer the ifunc symbol. */
  1009. bound_minimal_symbol resolver
  1010. = find_gnu_ifunc (sym.symbol);
  1011. if (resolver.minsym != NULL)
  1012. pstate->push_new<var_msym_value_operation>
  1013. (resolver);
  1014. else
  1015. pstate->push_new<var_value_operation> (sym);
  1016. }
  1017. else if ($1.is_a_field_of_this)
  1018. {
  1019. /* C++: it hangs off of `this'. Must
  1020. not inadvertently convert from a method call
  1021. to data ref. */
  1022. pstate->block_tracker->update (sym);
  1023. operation_up thisop
  1024. = make_operation<op_this_operation> ();
  1025. pstate->push_new<structop_ptr_operation>
  1026. (std::move (thisop), copy_name ($1.stoken));
  1027. }
  1028. else
  1029. {
  1030. std::string arg = copy_name ($1.stoken);
  1031. bound_minimal_symbol msymbol
  1032. = lookup_bound_minimal_symbol (arg.c_str ());
  1033. if (msymbol.minsym == NULL)
  1034. {
  1035. if (!have_full_symbols () && !have_partial_symbols ())
  1036. error (_("No symbol table is loaded. Use the \"file\" command."));
  1037. else
  1038. error (_("No symbol \"%s\" in current context."),
  1039. arg.c_str ());
  1040. }
  1041. /* This minsym might be an alias for
  1042. another function. See if we can find
  1043. the debug symbol for the target, and
  1044. if so, use it instead, since it has
  1045. return type / prototype info. This
  1046. is important for example for "p
  1047. *__errno_location()". */
  1048. symbol *alias_target
  1049. = ((msymbol.minsym->type != mst_text_gnu_ifunc
  1050. && msymbol.minsym->type != mst_data_gnu_ifunc)
  1051. ? find_function_alias_target (msymbol)
  1052. : NULL);
  1053. if (alias_target != NULL)
  1054. {
  1055. block_symbol bsym { alias_target,
  1056. SYMBOL_BLOCK_VALUE (alias_target) };
  1057. pstate->push_new<var_value_operation> (bsym);
  1058. }
  1059. else
  1060. pstate->push_new<var_msym_value_operation>
  1061. (msymbol);
  1062. }
  1063. }
  1064. ;
  1065. const_or_volatile: const_or_volatile_noopt
  1066. |
  1067. ;
  1068. single_qualifier:
  1069. CONST_KEYWORD
  1070. { cpstate->type_stack.insert (tp_const); }
  1071. | VOLATILE_KEYWORD
  1072. { cpstate->type_stack.insert (tp_volatile); }
  1073. | ATOMIC
  1074. { cpstate->type_stack.insert (tp_atomic); }
  1075. | RESTRICT
  1076. { cpstate->type_stack.insert (tp_restrict); }
  1077. | '@' NAME
  1078. {
  1079. cpstate->type_stack.insert (pstate,
  1080. copy_name ($2.stoken).c_str ());
  1081. }
  1082. | '@' UNKNOWN_CPP_NAME
  1083. {
  1084. cpstate->type_stack.insert (pstate,
  1085. copy_name ($2.stoken).c_str ());
  1086. }
  1087. ;
  1088. qualifier_seq_noopt:
  1089. single_qualifier
  1090. | qualifier_seq_noopt single_qualifier
  1091. ;
  1092. qualifier_seq:
  1093. qualifier_seq_noopt
  1094. |
  1095. ;
  1096. ptr_operator:
  1097. ptr_operator '*'
  1098. { cpstate->type_stack.insert (tp_pointer); }
  1099. qualifier_seq
  1100. | '*'
  1101. { cpstate->type_stack.insert (tp_pointer); }
  1102. qualifier_seq
  1103. | '&'
  1104. { cpstate->type_stack.insert (tp_reference); }
  1105. | '&' ptr_operator
  1106. { cpstate->type_stack.insert (tp_reference); }
  1107. | ANDAND
  1108. { cpstate->type_stack.insert (tp_rvalue_reference); }
  1109. | ANDAND ptr_operator
  1110. { cpstate->type_stack.insert (tp_rvalue_reference); }
  1111. ;
  1112. ptr_operator_ts: ptr_operator
  1113. {
  1114. $$ = cpstate->type_stack.create ();
  1115. cpstate->type_stacks.emplace_back ($$);
  1116. }
  1117. ;
  1118. abs_decl: ptr_operator_ts direct_abs_decl
  1119. { $$ = $2->append ($1); }
  1120. | ptr_operator_ts
  1121. | direct_abs_decl
  1122. ;
  1123. direct_abs_decl: '(' abs_decl ')'
  1124. { $$ = $2; }
  1125. | direct_abs_decl array_mod
  1126. {
  1127. cpstate->type_stack.push ($1);
  1128. cpstate->type_stack.push ($2);
  1129. cpstate->type_stack.push (tp_array);
  1130. $$ = cpstate->type_stack.create ();
  1131. cpstate->type_stacks.emplace_back ($$);
  1132. }
  1133. | array_mod
  1134. {
  1135. cpstate->type_stack.push ($1);
  1136. cpstate->type_stack.push (tp_array);
  1137. $$ = cpstate->type_stack.create ();
  1138. cpstate->type_stacks.emplace_back ($$);
  1139. }
  1140. | direct_abs_decl func_mod
  1141. {
  1142. cpstate->type_stack.push ($1);
  1143. cpstate->type_stack.push ($2);
  1144. $$ = cpstate->type_stack.create ();
  1145. cpstate->type_stacks.emplace_back ($$);
  1146. }
  1147. | func_mod
  1148. {
  1149. cpstate->type_stack.push ($1);
  1150. $$ = cpstate->type_stack.create ();
  1151. cpstate->type_stacks.emplace_back ($$);
  1152. }
  1153. ;
  1154. array_mod: '[' ']'
  1155. { $$ = -1; }
  1156. | OBJC_LBRAC ']'
  1157. { $$ = -1; }
  1158. | '[' INT ']'
  1159. { $$ = $2.val; }
  1160. | OBJC_LBRAC INT ']'
  1161. { $$ = $2.val; }
  1162. ;
  1163. func_mod: '(' ')'
  1164. {
  1165. $$ = new std::vector<struct type *>;
  1166. cpstate->type_lists.emplace_back ($$);
  1167. }
  1168. | '(' parameter_typelist ')'
  1169. { $$ = $2; }
  1170. ;
  1171. /* We used to try to recognize pointer to member types here, but
  1172. that didn't work (shift/reduce conflicts meant that these rules never
  1173. got executed). The problem is that
  1174. int (foo::bar::baz::bizzle)
  1175. is a function type but
  1176. int (foo::bar::baz::bizzle::*)
  1177. is a pointer to member type. Stroustrup loses again! */
  1178. type : ptype
  1179. ;
  1180. /* A helper production that recognizes scalar types that can validly
  1181. be used with _Complex. */
  1182. scalar_type:
  1183. INT_KEYWORD
  1184. { $$ = lookup_signed_typename (pstate->language (),
  1185. "int"); }
  1186. | LONG
  1187. { $$ = lookup_signed_typename (pstate->language (),
  1188. "long"); }
  1189. | SHORT
  1190. { $$ = lookup_signed_typename (pstate->language (),
  1191. "short"); }
  1192. | LONG INT_KEYWORD
  1193. { $$ = lookup_signed_typename (pstate->language (),
  1194. "long"); }
  1195. | LONG SIGNED_KEYWORD INT_KEYWORD
  1196. { $$ = lookup_signed_typename (pstate->language (),
  1197. "long"); }
  1198. | LONG SIGNED_KEYWORD
  1199. { $$ = lookup_signed_typename (pstate->language (),
  1200. "long"); }
  1201. | SIGNED_KEYWORD LONG INT_KEYWORD
  1202. { $$ = lookup_signed_typename (pstate->language (),
  1203. "long"); }
  1204. | UNSIGNED LONG INT_KEYWORD
  1205. { $$ = lookup_unsigned_typename (pstate->language (),
  1206. "long"); }
  1207. | LONG UNSIGNED INT_KEYWORD
  1208. { $$ = lookup_unsigned_typename (pstate->language (),
  1209. "long"); }
  1210. | LONG UNSIGNED
  1211. { $$ = lookup_unsigned_typename (pstate->language (),
  1212. "long"); }
  1213. | LONG LONG
  1214. { $$ = lookup_signed_typename (pstate->language (),
  1215. "long long"); }
  1216. | LONG LONG INT_KEYWORD
  1217. { $$ = lookup_signed_typename (pstate->language (),
  1218. "long long"); }
  1219. | LONG LONG SIGNED_KEYWORD INT_KEYWORD
  1220. { $$ = lookup_signed_typename (pstate->language (),
  1221. "long long"); }
  1222. | LONG LONG SIGNED_KEYWORD
  1223. { $$ = lookup_signed_typename (pstate->language (),
  1224. "long long"); }
  1225. | SIGNED_KEYWORD LONG LONG
  1226. { $$ = lookup_signed_typename (pstate->language (),
  1227. "long long"); }
  1228. | SIGNED_KEYWORD LONG LONG INT_KEYWORD
  1229. { $$ = lookup_signed_typename (pstate->language (),
  1230. "long long"); }
  1231. | UNSIGNED LONG LONG
  1232. { $$ = lookup_unsigned_typename (pstate->language (),
  1233. "long long"); }
  1234. | UNSIGNED LONG LONG INT_KEYWORD
  1235. { $$ = lookup_unsigned_typename (pstate->language (),
  1236. "long long"); }
  1237. | LONG LONG UNSIGNED
  1238. { $$ = lookup_unsigned_typename (pstate->language (),
  1239. "long long"); }
  1240. | LONG LONG UNSIGNED INT_KEYWORD
  1241. { $$ = lookup_unsigned_typename (pstate->language (),
  1242. "long long"); }
  1243. | SHORT INT_KEYWORD
  1244. { $$ = lookup_signed_typename (pstate->language (),
  1245. "short"); }
  1246. | SHORT SIGNED_KEYWORD INT_KEYWORD
  1247. { $$ = lookup_signed_typename (pstate->language (),
  1248. "short"); }
  1249. | SHORT SIGNED_KEYWORD
  1250. { $$ = lookup_signed_typename (pstate->language (),
  1251. "short"); }
  1252. | UNSIGNED SHORT INT_KEYWORD
  1253. { $$ = lookup_unsigned_typename (pstate->language (),
  1254. "short"); }
  1255. | SHORT UNSIGNED
  1256. { $$ = lookup_unsigned_typename (pstate->language (),
  1257. "short"); }
  1258. | SHORT UNSIGNED INT_KEYWORD
  1259. { $$ = lookup_unsigned_typename (pstate->language (),
  1260. "short"); }
  1261. | DOUBLE_KEYWORD
  1262. { $$ = lookup_typename (pstate->language (),
  1263. "double",
  1264. NULL,
  1265. 0); }
  1266. | FLOAT_KEYWORD
  1267. { $$ = lookup_typename (pstate->language (),
  1268. "float",
  1269. NULL,
  1270. 0); }
  1271. | LONG DOUBLE_KEYWORD
  1272. { $$ = lookup_typename (pstate->language (),
  1273. "long double",
  1274. NULL,
  1275. 0); }
  1276. | UNSIGNED type_name
  1277. { $$ = lookup_unsigned_typename (pstate->language (),
  1278. $2.type->name ()); }
  1279. | UNSIGNED
  1280. { $$ = lookup_unsigned_typename (pstate->language (),
  1281. "int"); }
  1282. | SIGNED_KEYWORD type_name
  1283. { $$ = lookup_signed_typename (pstate->language (),
  1284. $2.type->name ()); }
  1285. | SIGNED_KEYWORD
  1286. { $$ = lookup_signed_typename (pstate->language (),
  1287. "int"); }
  1288. ;
  1289. /* Implements (approximately): (type-qualifier)* type-specifier.
  1290. When type-specifier is only ever a single word, like 'float' then these
  1291. arrive as pre-built TYPENAME tokens thanks to the classify_name
  1292. function. However, when a type-specifier can contain multiple words,
  1293. for example 'double' can appear as just 'double' or 'long double', and
  1294. similarly 'long' can appear as just 'long' or in 'long double', then
  1295. these type-specifiers are parsed into their own tokens in the function
  1296. lex_one_token and the ident_tokens array. These separate tokens are all
  1297. recognised here. */
  1298. typebase
  1299. : TYPENAME
  1300. { $$ = $1.type; }
  1301. | scalar_type
  1302. { $$ = $1; }
  1303. | COMPLEX scalar_type
  1304. {
  1305. $$ = init_complex_type (nullptr, $2);
  1306. }
  1307. | STRUCT name
  1308. { $$
  1309. = lookup_struct (copy_name ($2).c_str (),
  1310. pstate->expression_context_block);
  1311. }
  1312. | STRUCT COMPLETE
  1313. {
  1314. pstate->mark_completion_tag (TYPE_CODE_STRUCT,
  1315. "", 0);
  1316. $$ = NULL;
  1317. }
  1318. | STRUCT name COMPLETE
  1319. {
  1320. pstate->mark_completion_tag (TYPE_CODE_STRUCT,
  1321. $2.ptr, $2.length);
  1322. $$ = NULL;
  1323. }
  1324. | CLASS name
  1325. { $$ = lookup_struct
  1326. (copy_name ($2).c_str (),
  1327. pstate->expression_context_block);
  1328. }
  1329. | CLASS COMPLETE
  1330. {
  1331. pstate->mark_completion_tag (TYPE_CODE_STRUCT,
  1332. "", 0);
  1333. $$ = NULL;
  1334. }
  1335. | CLASS name COMPLETE
  1336. {
  1337. pstate->mark_completion_tag (TYPE_CODE_STRUCT,
  1338. $2.ptr, $2.length);
  1339. $$ = NULL;
  1340. }
  1341. | UNION name
  1342. { $$
  1343. = lookup_union (copy_name ($2).c_str (),
  1344. pstate->expression_context_block);
  1345. }
  1346. | UNION COMPLETE
  1347. {
  1348. pstate->mark_completion_tag (TYPE_CODE_UNION,
  1349. "", 0);
  1350. $$ = NULL;
  1351. }
  1352. | UNION name COMPLETE
  1353. {
  1354. pstate->mark_completion_tag (TYPE_CODE_UNION,
  1355. $2.ptr, $2.length);
  1356. $$ = NULL;
  1357. }
  1358. | ENUM name
  1359. { $$ = lookup_enum (copy_name ($2).c_str (),
  1360. pstate->expression_context_block);
  1361. }
  1362. | ENUM COMPLETE
  1363. {
  1364. pstate->mark_completion_tag (TYPE_CODE_ENUM, "", 0);
  1365. $$ = NULL;
  1366. }
  1367. | ENUM name COMPLETE
  1368. {
  1369. pstate->mark_completion_tag (TYPE_CODE_ENUM, $2.ptr,
  1370. $2.length);
  1371. $$ = NULL;
  1372. }
  1373. /* It appears that this rule for templates is never
  1374. reduced; template recognition happens by lookahead
  1375. in the token processing code in yylex. */
  1376. | TEMPLATE name '<' type '>'
  1377. { $$ = lookup_template_type
  1378. (copy_name($2).c_str (), $4,
  1379. pstate->expression_context_block);
  1380. }
  1381. | qualifier_seq_noopt typebase
  1382. { $$ = cpstate->type_stack.follow_types ($2); }
  1383. | typebase qualifier_seq_noopt
  1384. { $$ = cpstate->type_stack.follow_types ($1); }
  1385. ;
  1386. type_name: TYPENAME
  1387. | INT_KEYWORD
  1388. {
  1389. $$.stoken.ptr = "int";
  1390. $$.stoken.length = 3;
  1391. $$.type = lookup_signed_typename (pstate->language (),
  1392. "int");
  1393. }
  1394. | LONG
  1395. {
  1396. $$.stoken.ptr = "long";
  1397. $$.stoken.length = 4;
  1398. $$.type = lookup_signed_typename (pstate->language (),
  1399. "long");
  1400. }
  1401. | SHORT
  1402. {
  1403. $$.stoken.ptr = "short";
  1404. $$.stoken.length = 5;
  1405. $$.type = lookup_signed_typename (pstate->language (),
  1406. "short");
  1407. }
  1408. ;
  1409. parameter_typelist:
  1410. nonempty_typelist
  1411. { check_parameter_typelist ($1); }
  1412. | nonempty_typelist ',' DOTDOTDOT
  1413. {
  1414. $1->push_back (NULL);
  1415. check_parameter_typelist ($1);
  1416. $$ = $1;
  1417. }
  1418. ;
  1419. nonempty_typelist
  1420. : type
  1421. {
  1422. std::vector<struct type *> *typelist
  1423. = new std::vector<struct type *>;
  1424. cpstate->type_lists.emplace_back (typelist);
  1425. typelist->push_back ($1);
  1426. $$ = typelist;
  1427. }
  1428. | nonempty_typelist ',' type
  1429. {
  1430. $1->push_back ($3);
  1431. $$ = $1;
  1432. }
  1433. ;
  1434. ptype : typebase
  1435. | ptype abs_decl
  1436. {
  1437. cpstate->type_stack.push ($2);
  1438. $$ = cpstate->type_stack.follow_types ($1);
  1439. }
  1440. ;
  1441. conversion_type_id: typebase conversion_declarator
  1442. { $$ = cpstate->type_stack.follow_types ($1); }
  1443. ;
  1444. conversion_declarator: /* Nothing. */
  1445. | ptr_operator conversion_declarator
  1446. ;
  1447. const_and_volatile: CONST_KEYWORD VOLATILE_KEYWORD
  1448. | VOLATILE_KEYWORD CONST_KEYWORD
  1449. ;
  1450. const_or_volatile_noopt: const_and_volatile
  1451. { cpstate->type_stack.insert (tp_const);
  1452. cpstate->type_stack.insert (tp_volatile);
  1453. }
  1454. | CONST_KEYWORD
  1455. { cpstate->type_stack.insert (tp_const); }
  1456. | VOLATILE_KEYWORD
  1457. { cpstate->type_stack.insert (tp_volatile); }
  1458. ;
  1459. oper: OPERATOR NEW
  1460. { $$ = operator_stoken (" new"); }
  1461. | OPERATOR DELETE
  1462. { $$ = operator_stoken (" delete"); }
  1463. | OPERATOR NEW '[' ']'
  1464. { $$ = operator_stoken (" new[]"); }
  1465. | OPERATOR DELETE '[' ']'
  1466. { $$ = operator_stoken (" delete[]"); }
  1467. | OPERATOR NEW OBJC_LBRAC ']'
  1468. { $$ = operator_stoken (" new[]"); }
  1469. | OPERATOR DELETE OBJC_LBRAC ']'
  1470. { $$ = operator_stoken (" delete[]"); }
  1471. | OPERATOR '+'
  1472. { $$ = operator_stoken ("+"); }
  1473. | OPERATOR '-'
  1474. { $$ = operator_stoken ("-"); }
  1475. | OPERATOR '*'
  1476. { $$ = operator_stoken ("*"); }
  1477. | OPERATOR '/'
  1478. { $$ = operator_stoken ("/"); }
  1479. | OPERATOR '%'
  1480. { $$ = operator_stoken ("%"); }
  1481. | OPERATOR '^'
  1482. { $$ = operator_stoken ("^"); }
  1483. | OPERATOR '&'
  1484. { $$ = operator_stoken ("&"); }
  1485. | OPERATOR '|'
  1486. { $$ = operator_stoken ("|"); }
  1487. | OPERATOR '~'
  1488. { $$ = operator_stoken ("~"); }
  1489. | OPERATOR '!'
  1490. { $$ = operator_stoken ("!"); }
  1491. | OPERATOR '='
  1492. { $$ = operator_stoken ("="); }
  1493. | OPERATOR '<'
  1494. { $$ = operator_stoken ("<"); }
  1495. | OPERATOR '>'
  1496. { $$ = operator_stoken (">"); }
  1497. | OPERATOR ASSIGN_MODIFY
  1498. { const char *op = " unknown";
  1499. switch ($2)
  1500. {
  1501. case BINOP_RSH:
  1502. op = ">>=";
  1503. break;
  1504. case BINOP_LSH:
  1505. op = "<<=";
  1506. break;
  1507. case BINOP_ADD:
  1508. op = "+=";
  1509. break;
  1510. case BINOP_SUB:
  1511. op = "-=";
  1512. break;
  1513. case BINOP_MUL:
  1514. op = "*=";
  1515. break;
  1516. case BINOP_DIV:
  1517. op = "/=";
  1518. break;
  1519. case BINOP_REM:
  1520. op = "%=";
  1521. break;
  1522. case BINOP_BITWISE_IOR:
  1523. op = "|=";
  1524. break;
  1525. case BINOP_BITWISE_AND:
  1526. op = "&=";
  1527. break;
  1528. case BINOP_BITWISE_XOR:
  1529. op = "^=";
  1530. break;
  1531. default:
  1532. break;
  1533. }
  1534. $$ = operator_stoken (op);
  1535. }
  1536. | OPERATOR LSH
  1537. { $$ = operator_stoken ("<<"); }
  1538. | OPERATOR RSH
  1539. { $$ = operator_stoken (">>"); }
  1540. | OPERATOR EQUAL
  1541. { $$ = operator_stoken ("=="); }
  1542. | OPERATOR NOTEQUAL
  1543. { $$ = operator_stoken ("!="); }
  1544. | OPERATOR LEQ
  1545. { $$ = operator_stoken ("<="); }
  1546. | OPERATOR GEQ
  1547. { $$ = operator_stoken (">="); }
  1548. | OPERATOR ANDAND
  1549. { $$ = operator_stoken ("&&"); }
  1550. | OPERATOR OROR
  1551. { $$ = operator_stoken ("||"); }
  1552. | OPERATOR INCREMENT
  1553. { $$ = operator_stoken ("++"); }
  1554. | OPERATOR DECREMENT
  1555. { $$ = operator_stoken ("--"); }
  1556. | OPERATOR ','
  1557. { $$ = operator_stoken (","); }
  1558. | OPERATOR ARROW_STAR
  1559. { $$ = operator_stoken ("->*"); }
  1560. | OPERATOR ARROW
  1561. { $$ = operator_stoken ("->"); }
  1562. | OPERATOR '(' ')'
  1563. { $$ = operator_stoken ("()"); }
  1564. | OPERATOR '[' ']'
  1565. { $$ = operator_stoken ("[]"); }
  1566. | OPERATOR OBJC_LBRAC ']'
  1567. { $$ = operator_stoken ("[]"); }
  1568. | OPERATOR conversion_type_id
  1569. {
  1570. string_file buf;
  1571. c_print_type ($2, NULL, &buf, -1, 0,
  1572. &type_print_raw_options);
  1573. std::string name = buf.release ();
  1574. /* This also needs canonicalization. */
  1575. gdb::unique_xmalloc_ptr<char> canon
  1576. = cp_canonicalize_string (name.c_str ());
  1577. if (canon != nullptr)
  1578. name = canon.get ();
  1579. $$ = operator_stoken ((" " + name).c_str ());
  1580. }
  1581. ;
  1582. /* This rule exists in order to allow some tokens that would not normally
  1583. match the 'name' rule to appear as fields within a struct. The example
  1584. that initially motivated this was the RISC-V target which models the
  1585. floating point registers as a union with fields called 'float' and
  1586. 'double'. */
  1587. field_name
  1588. : name
  1589. | DOUBLE_KEYWORD { $$ = typename_stoken ("double"); }
  1590. | FLOAT_KEYWORD { $$ = typename_stoken ("float"); }
  1591. | INT_KEYWORD { $$ = typename_stoken ("int"); }
  1592. | LONG { $$ = typename_stoken ("long"); }
  1593. | SHORT { $$ = typename_stoken ("short"); }
  1594. | SIGNED_KEYWORD { $$ = typename_stoken ("signed"); }
  1595. | UNSIGNED { $$ = typename_stoken ("unsigned"); }
  1596. ;
  1597. name : NAME { $$ = $1.stoken; }
  1598. | BLOCKNAME { $$ = $1.stoken; }
  1599. | TYPENAME { $$ = $1.stoken; }
  1600. | NAME_OR_INT { $$ = $1.stoken; }
  1601. | UNKNOWN_CPP_NAME { $$ = $1.stoken; }
  1602. | oper { $$ = $1; }
  1603. ;
  1604. name_not_typename : NAME
  1605. | BLOCKNAME
  1606. /* These would be useful if name_not_typename was useful, but it is just
  1607. a fake for "variable", so these cause reduce/reduce conflicts because
  1608. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  1609. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  1610. context where only a name could occur, this might be useful.
  1611. | NAME_OR_INT
  1612. */
  1613. | oper
  1614. {
  1615. struct field_of_this_result is_a_field_of_this;
  1616. $$.stoken = $1;
  1617. $$.sym
  1618. = lookup_symbol ($1.ptr,
  1619. pstate->expression_context_block,
  1620. VAR_DOMAIN,
  1621. &is_a_field_of_this);
  1622. $$.is_a_field_of_this
  1623. = is_a_field_of_this.type != NULL;
  1624. }
  1625. | UNKNOWN_CPP_NAME
  1626. ;
  1627. %%
  1628. /* Returns a stoken of the operator name given by OP (which does not
  1629. include the string "operator"). */
  1630. static struct stoken
  1631. operator_stoken (const char *op)
  1632. {
  1633. struct stoken st = { NULL, 0 };
  1634. char *buf;
  1635. st.length = CP_OPERATOR_LEN + strlen (op);
  1636. buf = (char *) malloc (st.length + 1);
  1637. strcpy (buf, CP_OPERATOR_STR);
  1638. strcat (buf, op);
  1639. st.ptr = buf;
  1640. /* The toplevel (c_parse) will free the memory allocated here. */
  1641. cpstate->strings.emplace_back (buf);
  1642. return st;
  1643. };
  1644. /* Returns a stoken of the type named TYPE. */
  1645. static struct stoken
  1646. typename_stoken (const char *type)
  1647. {
  1648. struct stoken st = { type, 0 };
  1649. st.length = strlen (type);
  1650. return st;
  1651. };
  1652. /* Return true if the type is aggregate-like. */
  1653. static int
  1654. type_aggregate_p (struct type *type)
  1655. {
  1656. return (type->code () == TYPE_CODE_STRUCT
  1657. || type->code () == TYPE_CODE_UNION
  1658. || type->code () == TYPE_CODE_NAMESPACE
  1659. || (type->code () == TYPE_CODE_ENUM
  1660. && type->is_declared_class ()));
  1661. }
  1662. /* Validate a parameter typelist. */
  1663. static void
  1664. check_parameter_typelist (std::vector<struct type *> *params)
  1665. {
  1666. struct type *type;
  1667. int ix;
  1668. for (ix = 0; ix < params->size (); ++ix)
  1669. {
  1670. type = (*params)[ix];
  1671. if (type != NULL && check_typedef (type)->code () == TYPE_CODE_VOID)
  1672. {
  1673. if (ix == 0)
  1674. {
  1675. if (params->size () == 1)
  1676. {
  1677. /* Ok. */
  1678. break;
  1679. }
  1680. error (_("parameter types following 'void'"));
  1681. }
  1682. else
  1683. error (_("'void' invalid as parameter type"));
  1684. }
  1685. }
  1686. }
  1687. /* Take care of parsing a number (anything that starts with a digit).
  1688. Set yylval and return the token type; update lexptr.
  1689. LEN is the number of characters in it. */
  1690. /*** Needs some error checking for the float case ***/
  1691. static int
  1692. parse_number (struct parser_state *par_state,
  1693. const char *buf, int len, int parsed_float, YYSTYPE *putithere)
  1694. {
  1695. ULONGEST n = 0;
  1696. ULONGEST prevn = 0;
  1697. ULONGEST un;
  1698. int i = 0;
  1699. int c;
  1700. int base = input_radix;
  1701. int unsigned_p = 0;
  1702. /* Number of "L" suffixes encountered. */
  1703. int long_p = 0;
  1704. /* Imaginary number. */
  1705. bool imaginary_p = false;
  1706. /* We have found a "L" or "U" (or "i") suffix. */
  1707. int found_suffix = 0;
  1708. ULONGEST high_bit;
  1709. struct type *signed_type;
  1710. struct type *unsigned_type;
  1711. char *p;
  1712. p = (char *) alloca (len);
  1713. memcpy (p, buf, len);
  1714. if (parsed_float)
  1715. {
  1716. if (len >= 1 && p[len - 1] == 'i')
  1717. {
  1718. imaginary_p = true;
  1719. --len;
  1720. }
  1721. /* Handle suffixes for decimal floating-point: "df", "dd" or "dl". */
  1722. if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'f')
  1723. {
  1724. putithere->typed_val_float.type
  1725. = parse_type (par_state)->builtin_decfloat;
  1726. len -= 2;
  1727. }
  1728. else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'd')
  1729. {
  1730. putithere->typed_val_float.type
  1731. = parse_type (par_state)->builtin_decdouble;
  1732. len -= 2;
  1733. }
  1734. else if (len >= 2 && p[len - 2] == 'd' && p[len - 1] == 'l')
  1735. {
  1736. putithere->typed_val_float.type
  1737. = parse_type (par_state)->builtin_declong;
  1738. len -= 2;
  1739. }
  1740. /* Handle suffixes: 'f' for float, 'l' for long double. */
  1741. else if (len >= 1 && TOLOWER (p[len - 1]) == 'f')
  1742. {
  1743. putithere->typed_val_float.type
  1744. = parse_type (par_state)->builtin_float;
  1745. len -= 1;
  1746. }
  1747. else if (len >= 1 && TOLOWER (p[len - 1]) == 'l')
  1748. {
  1749. putithere->typed_val_float.type
  1750. = parse_type (par_state)->builtin_long_double;
  1751. len -= 1;
  1752. }
  1753. /* Default type for floating-point literals is double. */
  1754. else
  1755. {
  1756. putithere->typed_val_float.type
  1757. = parse_type (par_state)->builtin_double;
  1758. }
  1759. if (!parse_float (p, len,
  1760. putithere->typed_val_float.type,
  1761. putithere->typed_val_float.val))
  1762. return ERROR;
  1763. if (imaginary_p)
  1764. putithere->typed_val_float.type
  1765. = init_complex_type (nullptr, putithere->typed_val_float.type);
  1766. return imaginary_p ? COMPLEX_FLOAT : FLOAT;
  1767. }
  1768. /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  1769. if (p[0] == '0' && len > 1)
  1770. switch (p[1])
  1771. {
  1772. case 'x':
  1773. case 'X':
  1774. if (len >= 3)
  1775. {
  1776. p += 2;
  1777. base = 16;
  1778. len -= 2;
  1779. }
  1780. break;
  1781. case 'b':
  1782. case 'B':
  1783. if (len >= 3)
  1784. {
  1785. p += 2;
  1786. base = 2;
  1787. len -= 2;
  1788. }
  1789. break;
  1790. case 't':
  1791. case 'T':
  1792. case 'd':
  1793. case 'D':
  1794. if (len >= 3)
  1795. {
  1796. p += 2;
  1797. base = 10;
  1798. len -= 2;
  1799. }
  1800. break;
  1801. default:
  1802. base = 8;
  1803. break;
  1804. }
  1805. while (len-- > 0)
  1806. {
  1807. c = *p++;
  1808. if (c >= 'A' && c <= 'Z')
  1809. c += 'a' - 'A';
  1810. if (c != 'l' && c != 'u' && c != 'i')
  1811. n *= base;
  1812. if (c >= '0' && c <= '9')
  1813. {
  1814. if (found_suffix)
  1815. return ERROR;
  1816. n += i = c - '0';
  1817. }
  1818. else
  1819. {
  1820. if (base > 10 && c >= 'a' && c <= 'f')
  1821. {
  1822. if (found_suffix)
  1823. return ERROR;
  1824. n += i = c - 'a' + 10;
  1825. }
  1826. else if (c == 'l')
  1827. {
  1828. ++long_p;
  1829. found_suffix = 1;
  1830. }
  1831. else if (c == 'u')
  1832. {
  1833. unsigned_p = 1;
  1834. found_suffix = 1;
  1835. }
  1836. else if (c == 'i')
  1837. {
  1838. imaginary_p = true;
  1839. found_suffix = 1;
  1840. }
  1841. else
  1842. return ERROR; /* Char not a digit */
  1843. }
  1844. if (i >= base)
  1845. return ERROR; /* Invalid digit in this base */
  1846. /* Portably test for overflow (only works for nonzero values, so make
  1847. a second check for zero). FIXME: Can't we just make n and prevn
  1848. unsigned and avoid this? */
  1849. if (c != 'l' && c != 'u' && c != 'i' && (prevn >= n) && n != 0)
  1850. unsigned_p = 1; /* Try something unsigned */
  1851. /* Portably test for unsigned overflow.
  1852. FIXME: This check is wrong; for example it doesn't find overflow
  1853. on 0x123456789 when LONGEST is 32 bits. */
  1854. if (c != 'l' && c != 'u' && c != 'i' && n != 0)
  1855. {
  1856. if (unsigned_p && prevn >= n)
  1857. error (_("Numeric constant too large."));
  1858. }
  1859. prevn = n;
  1860. }
  1861. /* An integer constant is an int, a long, or a long long. An L
  1862. suffix forces it to be long; an LL suffix forces it to be long
  1863. long. If not forced to a larger size, it gets the first type of
  1864. the above that it fits in. To figure out whether it fits, we
  1865. shift it right and see whether anything remains. Note that we
  1866. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  1867. operation, because many compilers will warn about such a shift
  1868. (which always produces a zero result). Sometimes gdbarch_int_bit
  1869. or gdbarch_long_bit will be that big, sometimes not. To deal with
  1870. the case where it is we just always shift the value more than
  1871. once, with fewer bits each time. */
  1872. un = n >> 2;
  1873. if (long_p == 0
  1874. && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
  1875. {
  1876. high_bit
  1877. = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
  1878. /* A large decimal (not hex or octal) constant (between INT_MAX
  1879. and UINT_MAX) is a long or unsigned long, according to ANSI,
  1880. never an unsigned int, but this code treats it as unsigned
  1881. int. This probably should be fixed. GCC gives a warning on
  1882. such constants. */
  1883. unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  1884. signed_type = parse_type (par_state)->builtin_int;
  1885. }
  1886. else if (long_p <= 1
  1887. && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
  1888. {
  1889. high_bit
  1890. = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
  1891. unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  1892. signed_type = parse_type (par_state)->builtin_long;
  1893. }
  1894. else
  1895. {
  1896. int shift;
  1897. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  1898. < gdbarch_long_long_bit (par_state->gdbarch ()))
  1899. /* A long long does not fit in a LONGEST. */
  1900. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  1901. else
  1902. shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
  1903. high_bit = (ULONGEST) 1 << shift;
  1904. unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
  1905. signed_type = parse_type (par_state)->builtin_long_long;
  1906. }
  1907. putithere->typed_val_int.val = n;
  1908. /* If the high bit of the worked out type is set then this number
  1909. has to be unsigned. */
  1910. if (unsigned_p || (n & high_bit))
  1911. {
  1912. putithere->typed_val_int.type = unsigned_type;
  1913. }
  1914. else
  1915. {
  1916. putithere->typed_val_int.type = signed_type;
  1917. }
  1918. if (imaginary_p)
  1919. putithere->typed_val_int.type
  1920. = init_complex_type (nullptr, putithere->typed_val_int.type);
  1921. return imaginary_p ? COMPLEX_INT : INT;
  1922. }
  1923. /* Temporary obstack used for holding strings. */
  1924. static struct obstack tempbuf;
  1925. static int tempbuf_init;
  1926. /* Parse a C escape sequence. The initial backslash of the sequence
  1927. is at (*PTR)[-1]. *PTR will be updated to point to just after the
  1928. last character of the sequence. If OUTPUT is not NULL, the
  1929. translated form of the escape sequence will be written there. If
  1930. OUTPUT is NULL, no output is written and the call will only affect
  1931. *PTR. If an escape sequence is expressed in target bytes, then the
  1932. entire sequence will simply be copied to OUTPUT. Return 1 if any
  1933. character was emitted, 0 otherwise. */
  1934. int
  1935. c_parse_escape (const char **ptr, struct obstack *output)
  1936. {
  1937. const char *tokptr = *ptr;
  1938. int result = 1;
  1939. /* Some escape sequences undergo character set conversion. Those we
  1940. translate here. */
  1941. switch (*tokptr)
  1942. {
  1943. /* Hex escapes do not undergo character set conversion, so keep
  1944. the escape sequence for later. */
  1945. case 'x':
  1946. if (output)
  1947. obstack_grow_str (output, "\\x");
  1948. ++tokptr;
  1949. if (!ISXDIGIT (*tokptr))
  1950. error (_("\\x escape without a following hex digit"));
  1951. while (ISXDIGIT (*tokptr))
  1952. {
  1953. if (output)
  1954. obstack_1grow (output, *tokptr);
  1955. ++tokptr;
  1956. }
  1957. break;
  1958. /* Octal escapes do not undergo character set conversion, so
  1959. keep the escape sequence for later. */
  1960. case '0':
  1961. case '1':
  1962. case '2':
  1963. case '3':
  1964. case '4':
  1965. case '5':
  1966. case '6':
  1967. case '7':
  1968. {
  1969. int i;
  1970. if (output)
  1971. obstack_grow_str (output, "\\");
  1972. for (i = 0;
  1973. i < 3 && ISDIGIT (*tokptr) && *tokptr != '8' && *tokptr != '9';
  1974. ++i)
  1975. {
  1976. if (output)
  1977. obstack_1grow (output, *tokptr);
  1978. ++tokptr;
  1979. }
  1980. }
  1981. break;
  1982. /* We handle UCNs later. We could handle them here, but that
  1983. would mean a spurious error in the case where the UCN could
  1984. be converted to the target charset but not the host
  1985. charset. */
  1986. case 'u':
  1987. case 'U':
  1988. {
  1989. char c = *tokptr;
  1990. int i, len = c == 'U' ? 8 : 4;
  1991. if (output)
  1992. {
  1993. obstack_1grow (output, '\\');
  1994. obstack_1grow (output, *tokptr);
  1995. }
  1996. ++tokptr;
  1997. if (!ISXDIGIT (*tokptr))
  1998. error (_("\\%c escape without a following hex digit"), c);
  1999. for (i = 0; i < len && ISXDIGIT (*tokptr); ++i)
  2000. {
  2001. if (output)
  2002. obstack_1grow (output, *tokptr);
  2003. ++tokptr;
  2004. }
  2005. }
  2006. break;
  2007. /* We must pass backslash through so that it does not
  2008. cause quoting during the second expansion. */
  2009. case '\\':
  2010. if (output)
  2011. obstack_grow_str (output, "\\\\");
  2012. ++tokptr;
  2013. break;
  2014. /* Escapes which undergo conversion. */
  2015. case 'a':
  2016. if (output)
  2017. obstack_1grow (output, '\a');
  2018. ++tokptr;
  2019. break;
  2020. case 'b':
  2021. if (output)
  2022. obstack_1grow (output, '\b');
  2023. ++tokptr;
  2024. break;
  2025. case 'f':
  2026. if (output)
  2027. obstack_1grow (output, '\f');
  2028. ++tokptr;
  2029. break;
  2030. case 'n':
  2031. if (output)
  2032. obstack_1grow (output, '\n');
  2033. ++tokptr;
  2034. break;
  2035. case 'r':
  2036. if (output)
  2037. obstack_1grow (output, '\r');
  2038. ++tokptr;
  2039. break;
  2040. case 't':
  2041. if (output)
  2042. obstack_1grow (output, '\t');
  2043. ++tokptr;
  2044. break;
  2045. case 'v':
  2046. if (output)
  2047. obstack_1grow (output, '\v');
  2048. ++tokptr;
  2049. break;
  2050. /* GCC extension. */
  2051. case 'e':
  2052. if (output)
  2053. obstack_1grow (output, HOST_ESCAPE_CHAR);
  2054. ++tokptr;
  2055. break;
  2056. /* Backslash-newline expands to nothing at all. */
  2057. case '\n':
  2058. ++tokptr;
  2059. result = 0;
  2060. break;
  2061. /* A few escapes just expand to the character itself. */
  2062. case '\'':
  2063. case '\"':
  2064. case '?':
  2065. /* GCC extensions. */
  2066. case '(':
  2067. case '{':
  2068. case '[':
  2069. case '%':
  2070. /* Unrecognized escapes turn into the character itself. */
  2071. default:
  2072. if (output)
  2073. obstack_1grow (output, *tokptr);
  2074. ++tokptr;
  2075. break;
  2076. }
  2077. *ptr = tokptr;
  2078. return result;
  2079. }
  2080. /* Parse a string or character literal from TOKPTR. The string or
  2081. character may be wide or unicode. *OUTPTR is set to just after the
  2082. end of the literal in the input string. The resulting token is
  2083. stored in VALUE. This returns a token value, either STRING or
  2084. CHAR, depending on what was parsed. *HOST_CHARS is set to the
  2085. number of host characters in the literal. */
  2086. static int
  2087. parse_string_or_char (const char *tokptr, const char **outptr,
  2088. struct typed_stoken *value, int *host_chars)
  2089. {
  2090. int quote;
  2091. c_string_type type;
  2092. int is_objc = 0;
  2093. /* Build the gdb internal form of the input string in tempbuf. Note
  2094. that the buffer is null byte terminated *only* for the
  2095. convenience of debugging gdb itself and printing the buffer
  2096. contents when the buffer contains no embedded nulls. Gdb does
  2097. not depend upon the buffer being null byte terminated, it uses
  2098. the length string instead. This allows gdb to handle C strings
  2099. (as well as strings in other languages) with embedded null
  2100. bytes */
  2101. if (!tempbuf_init)
  2102. tempbuf_init = 1;
  2103. else
  2104. obstack_free (&tempbuf, NULL);
  2105. obstack_init (&tempbuf);
  2106. /* Record the string type. */
  2107. if (*tokptr == 'L')
  2108. {
  2109. type = C_WIDE_STRING;
  2110. ++tokptr;
  2111. }
  2112. else if (*tokptr == 'u')
  2113. {
  2114. type = C_STRING_16;
  2115. ++tokptr;
  2116. }
  2117. else if (*tokptr == 'U')
  2118. {
  2119. type = C_STRING_32;
  2120. ++tokptr;
  2121. }
  2122. else if (*tokptr == '@')
  2123. {
  2124. /* An Objective C string. */
  2125. is_objc = 1;
  2126. type = C_STRING;
  2127. ++tokptr;
  2128. }
  2129. else
  2130. type = C_STRING;
  2131. /* Skip the quote. */
  2132. quote = *tokptr;
  2133. if (quote == '\'')
  2134. type |= C_CHAR;
  2135. ++tokptr;
  2136. *host_chars = 0;
  2137. while (*tokptr)
  2138. {
  2139. char c = *tokptr;
  2140. if (c == '\\')
  2141. {
  2142. ++tokptr;
  2143. *host_chars += c_parse_escape (&tokptr, &tempbuf);
  2144. }
  2145. else if (c == quote)
  2146. break;
  2147. else
  2148. {
  2149. obstack_1grow (&tempbuf, c);
  2150. ++tokptr;
  2151. /* FIXME: this does the wrong thing with multi-byte host
  2152. characters. We could use mbrlen here, but that would
  2153. make "set host-charset" a bit less useful. */
  2154. ++*host_chars;
  2155. }
  2156. }
  2157. if (*tokptr != quote)
  2158. {
  2159. if (quote == '"')
  2160. error (_("Unterminated string in expression."));
  2161. else
  2162. error (_("Unmatched single quote."));
  2163. }
  2164. ++tokptr;
  2165. value->type = type;
  2166. value->ptr = (char *) obstack_base (&tempbuf);
  2167. value->length = obstack_object_size (&tempbuf);
  2168. *outptr = tokptr;
  2169. return quote == '"' ? (is_objc ? NSSTRING : STRING) : CHAR;
  2170. }
  2171. /* This is used to associate some attributes with a token. */
  2172. enum token_flag
  2173. {
  2174. /* If this bit is set, the token is C++-only. */
  2175. FLAG_CXX = 1,
  2176. /* If this bit is set, the token is C-only. */
  2177. FLAG_C = 2,
  2178. /* If this bit is set, the token is conditional: if there is a
  2179. symbol of the same name, then the token is a symbol; otherwise,
  2180. the token is a keyword. */
  2181. FLAG_SHADOW = 4
  2182. };
  2183. DEF_ENUM_FLAGS_TYPE (enum token_flag, token_flags);
  2184. struct token
  2185. {
  2186. const char *oper;
  2187. int token;
  2188. enum exp_opcode opcode;
  2189. token_flags flags;
  2190. };
  2191. static const struct token tokentab3[] =
  2192. {
  2193. {">>=", ASSIGN_MODIFY, BINOP_RSH, 0},
  2194. {"<<=", ASSIGN_MODIFY, BINOP_LSH, 0},
  2195. {"->*", ARROW_STAR, OP_NULL, FLAG_CXX},
  2196. {"...", DOTDOTDOT, OP_NULL, 0}
  2197. };
  2198. static const struct token tokentab2[] =
  2199. {
  2200. {"+=", ASSIGN_MODIFY, BINOP_ADD, 0},
  2201. {"-=", ASSIGN_MODIFY, BINOP_SUB, 0},
  2202. {"*=", ASSIGN_MODIFY, BINOP_MUL, 0},
  2203. {"/=", ASSIGN_MODIFY, BINOP_DIV, 0},
  2204. {"%=", ASSIGN_MODIFY, BINOP_REM, 0},
  2205. {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR, 0},
  2206. {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND, 0},
  2207. {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR, 0},
  2208. {"++", INCREMENT, OP_NULL, 0},
  2209. {"--", DECREMENT, OP_NULL, 0},
  2210. {"->", ARROW, OP_NULL, 0},
  2211. {"&&", ANDAND, OP_NULL, 0},
  2212. {"||", OROR, OP_NULL, 0},
  2213. /* "::" is *not* only C++: gdb overrides its meaning in several
  2214. different ways, e.g., 'filename'::func, function::variable. */
  2215. {"::", COLONCOLON, OP_NULL, 0},
  2216. {"<<", LSH, OP_NULL, 0},
  2217. {">>", RSH, OP_NULL, 0},
  2218. {"==", EQUAL, OP_NULL, 0},
  2219. {"!=", NOTEQUAL, OP_NULL, 0},
  2220. {"<=", LEQ, OP_NULL, 0},
  2221. {">=", GEQ, OP_NULL, 0},
  2222. {".*", DOT_STAR, OP_NULL, FLAG_CXX}
  2223. };
  2224. /* Identifier-like tokens. Only type-specifiers than can appear in
  2225. multi-word type names (for example 'double' can appear in 'long
  2226. double') need to be listed here. type-specifiers that are only ever
  2227. single word (like 'char') are handled by the classify_name function. */
  2228. static const struct token ident_tokens[] =
  2229. {
  2230. {"unsigned", UNSIGNED, OP_NULL, 0},
  2231. {"template", TEMPLATE, OP_NULL, FLAG_CXX},
  2232. {"volatile", VOLATILE_KEYWORD, OP_NULL, 0},
  2233. {"struct", STRUCT, OP_NULL, 0},
  2234. {"signed", SIGNED_KEYWORD, OP_NULL, 0},
  2235. {"sizeof", SIZEOF, OP_NULL, 0},
  2236. {"_Alignof", ALIGNOF, OP_NULL, 0},
  2237. {"alignof", ALIGNOF, OP_NULL, FLAG_CXX},
  2238. {"double", DOUBLE_KEYWORD, OP_NULL, 0},
  2239. {"float", FLOAT_KEYWORD, OP_NULL, 0},
  2240. {"false", FALSEKEYWORD, OP_NULL, FLAG_CXX},
  2241. {"class", CLASS, OP_NULL, FLAG_CXX},
  2242. {"union", UNION, OP_NULL, 0},
  2243. {"short", SHORT, OP_NULL, 0},
  2244. {"const", CONST_KEYWORD, OP_NULL, 0},
  2245. {"restrict", RESTRICT, OP_NULL, FLAG_C | FLAG_SHADOW},
  2246. {"__restrict__", RESTRICT, OP_NULL, 0},
  2247. {"__restrict", RESTRICT, OP_NULL, 0},
  2248. {"_Atomic", ATOMIC, OP_NULL, 0},
  2249. {"enum", ENUM, OP_NULL, 0},
  2250. {"long", LONG, OP_NULL, 0},
  2251. {"_Complex", COMPLEX, OP_NULL, 0},
  2252. {"__complex__", COMPLEX, OP_NULL, 0},
  2253. {"true", TRUEKEYWORD, OP_NULL, FLAG_CXX},
  2254. {"int", INT_KEYWORD, OP_NULL, 0},
  2255. {"new", NEW, OP_NULL, FLAG_CXX},
  2256. {"delete", DELETE, OP_NULL, FLAG_CXX},
  2257. {"operator", OPERATOR, OP_NULL, FLAG_CXX},
  2258. {"and", ANDAND, OP_NULL, FLAG_CXX},
  2259. {"and_eq", ASSIGN_MODIFY, BINOP_BITWISE_AND, FLAG_CXX},
  2260. {"bitand", '&', OP_NULL, FLAG_CXX},
  2261. {"bitor", '|', OP_NULL, FLAG_CXX},
  2262. {"compl", '~', OP_NULL, FLAG_CXX},
  2263. {"not", '!', OP_NULL, FLAG_CXX},
  2264. {"not_eq", NOTEQUAL, OP_NULL, FLAG_CXX},
  2265. {"or", OROR, OP_NULL, FLAG_CXX},
  2266. {"or_eq", ASSIGN_MODIFY, BINOP_BITWISE_IOR, FLAG_CXX},
  2267. {"xor", '^', OP_NULL, FLAG_CXX},
  2268. {"xor_eq", ASSIGN_MODIFY, BINOP_BITWISE_XOR, FLAG_CXX},
  2269. {"const_cast", CONST_CAST, OP_NULL, FLAG_CXX },
  2270. {"dynamic_cast", DYNAMIC_CAST, OP_NULL, FLAG_CXX },
  2271. {"static_cast", STATIC_CAST, OP_NULL, FLAG_CXX },
  2272. {"reinterpret_cast", REINTERPRET_CAST, OP_NULL, FLAG_CXX },
  2273. {"__typeof__", TYPEOF, OP_TYPEOF, 0 },
  2274. {"__typeof", TYPEOF, OP_TYPEOF, 0 },
  2275. {"typeof", TYPEOF, OP_TYPEOF, FLAG_SHADOW },
  2276. {"__decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX },
  2277. {"decltype", DECLTYPE, OP_DECLTYPE, FLAG_CXX | FLAG_SHADOW },
  2278. {"typeid", TYPEID, OP_TYPEID, FLAG_CXX}
  2279. };
  2280. static void
  2281. scan_macro_expansion (const char *expansion)
  2282. {
  2283. /* We'd better not be trying to push the stack twice. */
  2284. gdb_assert (! cpstate->macro_original_text);
  2285. /* Copy to the obstack. */
  2286. const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
  2287. /* Save the old lexptr value, so we can return to it when we're done
  2288. parsing the expanded text. */
  2289. cpstate->macro_original_text = pstate->lexptr;
  2290. pstate->lexptr = copy;
  2291. }
  2292. static int
  2293. scanning_macro_expansion (void)
  2294. {
  2295. return cpstate->macro_original_text != 0;
  2296. }
  2297. static void
  2298. finished_macro_expansion (void)
  2299. {
  2300. /* There'd better be something to pop back to. */
  2301. gdb_assert (cpstate->macro_original_text);
  2302. /* Pop back to the original text. */
  2303. pstate->lexptr = cpstate->macro_original_text;
  2304. cpstate->macro_original_text = 0;
  2305. }
  2306. /* Return true iff the token represents a C++ cast operator. */
  2307. static int
  2308. is_cast_operator (const char *token, int len)
  2309. {
  2310. return (! strncmp (token, "dynamic_cast", len)
  2311. || ! strncmp (token, "static_cast", len)
  2312. || ! strncmp (token, "reinterpret_cast", len)
  2313. || ! strncmp (token, "const_cast", len));
  2314. }
  2315. /* The scope used for macro expansion. */
  2316. static struct macro_scope *expression_macro_scope;
  2317. /* This is set if a NAME token appeared at the very end of the input
  2318. string, with no whitespace separating the name from the EOF. This
  2319. is used only when parsing to do field name completion. */
  2320. static int saw_name_at_eof;
  2321. /* This is set if the previously-returned token was a structure
  2322. operator -- either '.' or ARROW. */
  2323. static bool last_was_structop;
  2324. /* Depth of parentheses. */
  2325. static int paren_depth;
  2326. /* Read one token, getting characters through lexptr. */
  2327. static int
  2328. lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
  2329. {
  2330. int c;
  2331. int namelen;
  2332. const char *tokstart;
  2333. bool saw_structop = last_was_structop;
  2334. last_was_structop = false;
  2335. *is_quoted_name = false;
  2336. retry:
  2337. /* Check if this is a macro invocation that we need to expand. */
  2338. if (! scanning_macro_expansion ())
  2339. {
  2340. gdb::unique_xmalloc_ptr<char> expanded
  2341. = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
  2342. if (expanded != nullptr)
  2343. scan_macro_expansion (expanded.get ());
  2344. }
  2345. pstate->prev_lexptr = pstate->lexptr;
  2346. tokstart = pstate->lexptr;
  2347. /* See if it is a special token of length 3. */
  2348. for (const auto &token : tokentab3)
  2349. if (strncmp (tokstart, token.oper, 3) == 0)
  2350. {
  2351. if ((token.flags & FLAG_CXX) != 0
  2352. && par_state->language ()->la_language != language_cplus)
  2353. break;
  2354. gdb_assert ((token.flags & FLAG_C) == 0);
  2355. pstate->lexptr += 3;
  2356. yylval.opcode = token.opcode;
  2357. return token.token;
  2358. }
  2359. /* See if it is a special token of length 2. */
  2360. for (const auto &token : tokentab2)
  2361. if (strncmp (tokstart, token.oper, 2) == 0)
  2362. {
  2363. if ((token.flags & FLAG_CXX) != 0
  2364. && par_state->language ()->la_language != language_cplus)
  2365. break;
  2366. gdb_assert ((token.flags & FLAG_C) == 0);
  2367. pstate->lexptr += 2;
  2368. yylval.opcode = token.opcode;
  2369. if (token.token == ARROW)
  2370. last_was_structop = 1;
  2371. return token.token;
  2372. }
  2373. switch (c = *tokstart)
  2374. {
  2375. case 0:
  2376. /* If we were just scanning the result of a macro expansion,
  2377. then we need to resume scanning the original text.
  2378. If we're parsing for field name completion, and the previous
  2379. token allows such completion, return a COMPLETE token.
  2380. Otherwise, we were already scanning the original text, and
  2381. we're really done. */
  2382. if (scanning_macro_expansion ())
  2383. {
  2384. finished_macro_expansion ();
  2385. goto retry;
  2386. }
  2387. else if (saw_name_at_eof)
  2388. {
  2389. saw_name_at_eof = 0;
  2390. return COMPLETE;
  2391. }
  2392. else if (par_state->parse_completion && saw_structop)
  2393. return COMPLETE;
  2394. else
  2395. return 0;
  2396. case ' ':
  2397. case '\t':
  2398. case '\n':
  2399. pstate->lexptr++;
  2400. goto retry;
  2401. case '[':
  2402. case '(':
  2403. paren_depth++;
  2404. pstate->lexptr++;
  2405. if (par_state->language ()->la_language == language_objc
  2406. && c == '[')
  2407. return OBJC_LBRAC;
  2408. return c;
  2409. case ']':
  2410. case ')':
  2411. if (paren_depth == 0)
  2412. return 0;
  2413. paren_depth--;
  2414. pstate->lexptr++;
  2415. return c;
  2416. case ',':
  2417. if (pstate->comma_terminates
  2418. && paren_depth == 0
  2419. && ! scanning_macro_expansion ())
  2420. return 0;
  2421. pstate->lexptr++;
  2422. return c;
  2423. case '.':
  2424. /* Might be a floating point number. */
  2425. if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
  2426. {
  2427. last_was_structop = true;
  2428. goto symbol; /* Nope, must be a symbol. */
  2429. }
  2430. /* FALL THRU. */
  2431. case '0':
  2432. case '1':
  2433. case '2':
  2434. case '3':
  2435. case '4':
  2436. case '5':
  2437. case '6':
  2438. case '7':
  2439. case '8':
  2440. case '9':
  2441. {
  2442. /* It's a number. */
  2443. int got_dot = 0, got_e = 0, got_p = 0, toktype;
  2444. const char *p = tokstart;
  2445. int hex = input_radix > 10;
  2446. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  2447. {
  2448. p += 2;
  2449. hex = 1;
  2450. }
  2451. else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  2452. {
  2453. p += 2;
  2454. hex = 0;
  2455. }
  2456. for (;; ++p)
  2457. {
  2458. /* This test includes !hex because 'e' is a valid hex digit
  2459. and thus does not indicate a floating point number when
  2460. the radix is hex. */
  2461. if (!hex && !got_e && !got_p && (*p == 'e' || *p == 'E'))
  2462. got_dot = got_e = 1;
  2463. else if (!got_e && !got_p && (*p == 'p' || *p == 'P'))
  2464. got_dot = got_p = 1;
  2465. /* This test does not include !hex, because a '.' always indicates
  2466. a decimal floating point number regardless of the radix. */
  2467. else if (!got_dot && *p == '.')
  2468. got_dot = 1;
  2469. else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
  2470. || (got_p && (p[-1] == 'p' || p[-1] == 'P')))
  2471. && (*p == '-' || *p == '+'))
  2472. /* This is the sign of the exponent, not the end of the
  2473. number. */
  2474. continue;
  2475. /* We will take any letters or digits. parse_number will
  2476. complain if past the radix, or if L or U are not final. */
  2477. else if ((*p < '0' || *p > '9')
  2478. && ((*p < 'a' || *p > 'z')
  2479. && (*p < 'A' || *p > 'Z')))
  2480. break;
  2481. }
  2482. toktype = parse_number (par_state, tokstart, p - tokstart,
  2483. got_dot | got_e | got_p, &yylval);
  2484. if (toktype == ERROR)
  2485. {
  2486. char *err_copy = (char *) alloca (p - tokstart + 1);
  2487. memcpy (err_copy, tokstart, p - tokstart);
  2488. err_copy[p - tokstart] = 0;
  2489. error (_("Invalid number \"%s\"."), err_copy);
  2490. }
  2491. pstate->lexptr = p;
  2492. return toktype;
  2493. }
  2494. case '@':
  2495. {
  2496. const char *p = &tokstart[1];
  2497. if (par_state->language ()->la_language == language_objc)
  2498. {
  2499. size_t len = strlen ("selector");
  2500. if (strncmp (p, "selector", len) == 0
  2501. && (p[len] == '\0' || ISSPACE (p[len])))
  2502. {
  2503. pstate->lexptr = p + len;
  2504. return SELECTOR;
  2505. }
  2506. else if (*p == '"')
  2507. goto parse_string;
  2508. }
  2509. while (ISSPACE (*p))
  2510. p++;
  2511. size_t len = strlen ("entry");
  2512. if (strncmp (p, "entry", len) == 0 && !c_ident_is_alnum (p[len])
  2513. && p[len] != '_')
  2514. {
  2515. pstate->lexptr = &p[len];
  2516. return ENTRY;
  2517. }
  2518. }
  2519. /* FALLTHRU */
  2520. case '+':
  2521. case '-':
  2522. case '*':
  2523. case '/':
  2524. case '%':
  2525. case '|':
  2526. case '&':
  2527. case '^':
  2528. case '~':
  2529. case '!':
  2530. case '<':
  2531. case '>':
  2532. case '?':
  2533. case ':':
  2534. case '=':
  2535. case '{':
  2536. case '}':
  2537. symbol:
  2538. pstate->lexptr++;
  2539. return c;
  2540. case 'L':
  2541. case 'u':
  2542. case 'U':
  2543. if (tokstart[1] != '"' && tokstart[1] != '\'')
  2544. break;
  2545. /* Fall through. */
  2546. case '\'':
  2547. case '"':
  2548. parse_string:
  2549. {
  2550. int host_len;
  2551. int result = parse_string_or_char (tokstart, &pstate->lexptr,
  2552. &yylval.tsval, &host_len);
  2553. if (result == CHAR)
  2554. {
  2555. if (host_len == 0)
  2556. error (_("Empty character constant."));
  2557. else if (host_len > 2 && c == '\'')
  2558. {
  2559. ++tokstart;
  2560. namelen = pstate->lexptr - tokstart - 1;
  2561. *is_quoted_name = true;
  2562. goto tryname;
  2563. }
  2564. else if (host_len > 1)
  2565. error (_("Invalid character constant."));
  2566. }
  2567. return result;
  2568. }
  2569. }
  2570. if (!(c == '_' || c == '$' || c_ident_is_alpha (c)))
  2571. /* We must have come across a bad character (e.g. ';'). */
  2572. error (_("Invalid character '%c' in expression."), c);
  2573. /* It's a name. See how long it is. */
  2574. namelen = 0;
  2575. for (c = tokstart[namelen];
  2576. (c == '_' || c == '$' || c_ident_is_alnum (c) || c == '<');)
  2577. {
  2578. /* Template parameter lists are part of the name.
  2579. FIXME: This mishandles `print $a<4&&$a>3'. */
  2580. if (c == '<')
  2581. {
  2582. if (! is_cast_operator (tokstart, namelen))
  2583. {
  2584. /* Scan ahead to get rest of the template specification. Note
  2585. that we look ahead only when the '<' adjoins non-whitespace
  2586. characters; for comparison expressions, e.g. "a < b > c",
  2587. there must be spaces before the '<', etc. */
  2588. const char *p = find_template_name_end (tokstart + namelen);
  2589. if (p)
  2590. namelen = p - tokstart;
  2591. }
  2592. break;
  2593. }
  2594. c = tokstart[++namelen];
  2595. }
  2596. /* The token "if" terminates the expression and is NOT removed from
  2597. the input stream. It doesn't count if it appears in the
  2598. expansion of a macro. */
  2599. if (namelen == 2
  2600. && tokstart[0] == 'i'
  2601. && tokstart[1] == 'f'
  2602. && ! scanning_macro_expansion ())
  2603. {
  2604. return 0;
  2605. }
  2606. /* For the same reason (breakpoint conditions), "thread N"
  2607. terminates the expression. "thread" could be an identifier, but
  2608. an identifier is never followed by a number without intervening
  2609. punctuation. "task" is similar. Handle abbreviations of these,
  2610. similarly to breakpoint.c:find_condition_and_thread. */
  2611. if (namelen >= 1
  2612. && (strncmp (tokstart, "thread", namelen) == 0
  2613. || strncmp (tokstart, "task", namelen) == 0)
  2614. && (tokstart[namelen] == ' ' || tokstart[namelen] == '\t')
  2615. && ! scanning_macro_expansion ())
  2616. {
  2617. const char *p = tokstart + namelen + 1;
  2618. while (*p == ' ' || *p == '\t')
  2619. p++;
  2620. if (*p >= '0' && *p <= '9')
  2621. return 0;
  2622. }
  2623. pstate->lexptr += namelen;
  2624. tryname:
  2625. yylval.sval.ptr = tokstart;
  2626. yylval.sval.length = namelen;
  2627. /* Catch specific keywords. */
  2628. std::string copy = copy_name (yylval.sval);
  2629. for (const auto &token : ident_tokens)
  2630. if (copy == token.oper)
  2631. {
  2632. if ((token.flags & FLAG_CXX) != 0
  2633. && par_state->language ()->la_language != language_cplus)
  2634. break;
  2635. if ((token.flags & FLAG_C) != 0
  2636. && par_state->language ()->la_language != language_c
  2637. && par_state->language ()->la_language != language_objc)
  2638. break;
  2639. if ((token.flags & FLAG_SHADOW) != 0)
  2640. {
  2641. struct field_of_this_result is_a_field_of_this;
  2642. if (lookup_symbol (copy.c_str (),
  2643. pstate->expression_context_block,
  2644. VAR_DOMAIN,
  2645. (par_state->language ()->la_language
  2646. == language_cplus ? &is_a_field_of_this
  2647. : NULL)).symbol
  2648. != NULL)
  2649. {
  2650. /* The keyword is shadowed. */
  2651. break;
  2652. }
  2653. }
  2654. /* It is ok to always set this, even though we don't always
  2655. strictly need to. */
  2656. yylval.opcode = token.opcode;
  2657. return token.token;
  2658. }
  2659. if (*tokstart == '$')
  2660. return DOLLAR_VARIABLE;
  2661. if (pstate->parse_completion && *pstate->lexptr == '\0')
  2662. saw_name_at_eof = 1;
  2663. yylval.ssym.stoken = yylval.sval;
  2664. yylval.ssym.sym.symbol = NULL;
  2665. yylval.ssym.sym.block = NULL;
  2666. yylval.ssym.is_a_field_of_this = 0;
  2667. return NAME;
  2668. }
  2669. /* An object of this type is pushed on a FIFO by the "outer" lexer. */
  2670. struct token_and_value
  2671. {
  2672. int token;
  2673. YYSTYPE value;
  2674. };
  2675. /* A FIFO of tokens that have been read but not yet returned to the
  2676. parser. */
  2677. static std::vector<token_and_value> token_fifo;
  2678. /* Non-zero if the lexer should return tokens from the FIFO. */
  2679. static int popping;
  2680. /* Temporary storage for c_lex; this holds symbol names as they are
  2681. built up. */
  2682. static auto_obstack name_obstack;
  2683. /* Classify a NAME token. The contents of the token are in `yylval'.
  2684. Updates yylval and returns the new token type. BLOCK is the block
  2685. in which lookups start; this can be NULL to mean the global scope.
  2686. IS_QUOTED_NAME is non-zero if the name token was originally quoted
  2687. in single quotes. IS_AFTER_STRUCTOP is true if this name follows
  2688. a structure operator -- either '.' or ARROW */
  2689. static int
  2690. classify_name (struct parser_state *par_state, const struct block *block,
  2691. bool is_quoted_name, bool is_after_structop)
  2692. {
  2693. struct block_symbol bsym;
  2694. struct field_of_this_result is_a_field_of_this;
  2695. std::string copy = copy_name (yylval.sval);
  2696. /* Initialize this in case we *don't* use it in this call; that way
  2697. we can refer to it unconditionally below. */
  2698. memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));
  2699. bsym = lookup_symbol (copy.c_str (), block, VAR_DOMAIN,
  2700. par_state->language ()->name_of_this ()
  2701. ? &is_a_field_of_this : NULL);
  2702. if (bsym.symbol && bsym.symbol->aclass () == LOC_BLOCK)
  2703. {
  2704. yylval.ssym.sym = bsym;
  2705. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  2706. return BLOCKNAME;
  2707. }
  2708. else if (!bsym.symbol)
  2709. {
  2710. /* If we found a field of 'this', we might have erroneously
  2711. found a constructor where we wanted a type name. Handle this
  2712. case by noticing that we found a constructor and then look up
  2713. the type tag instead. */
  2714. if (is_a_field_of_this.type != NULL
  2715. && is_a_field_of_this.fn_field != NULL
  2716. && TYPE_FN_FIELD_CONSTRUCTOR (is_a_field_of_this.fn_field->fn_fields,
  2717. 0))
  2718. {
  2719. struct field_of_this_result inner_is_a_field_of_this;
  2720. bsym = lookup_symbol (copy.c_str (), block, STRUCT_DOMAIN,
  2721. &inner_is_a_field_of_this);
  2722. if (bsym.symbol != NULL)
  2723. {
  2724. yylval.tsym.type = bsym.symbol->type ();
  2725. return TYPENAME;
  2726. }
  2727. }
  2728. /* If we found a field on the "this" object, or we are looking
  2729. up a field on a struct, then we want to prefer it over a
  2730. filename. However, if the name was quoted, then it is better
  2731. to check for a filename or a block, since this is the only
  2732. way the user has of requiring the extension to be used. */
  2733. if ((is_a_field_of_this.type == NULL && !is_after_structop)
  2734. || is_quoted_name)
  2735. {
  2736. /* See if it's a file name. */
  2737. struct symtab *symtab;
  2738. symtab = lookup_symtab (copy.c_str ());
  2739. if (symtab)
  2740. {
  2741. yylval.bval
  2742. = BLOCKVECTOR_BLOCK (symtab->compunit ()->blockvector (),
  2743. STATIC_BLOCK);
  2744. return FILENAME;
  2745. }
  2746. }
  2747. }
  2748. if (bsym.symbol && bsym.symbol->aclass () == LOC_TYPEDEF)
  2749. {
  2750. yylval.tsym.type = bsym.symbol->type ();
  2751. return TYPENAME;
  2752. }
  2753. /* See if it's an ObjC classname. */
  2754. if (par_state->language ()->la_language == language_objc && !bsym.symbol)
  2755. {
  2756. CORE_ADDR Class = lookup_objc_class (par_state->gdbarch (),
  2757. copy.c_str ());
  2758. if (Class)
  2759. {
  2760. struct symbol *sym;
  2761. yylval.theclass.theclass = Class;
  2762. sym = lookup_struct_typedef (copy.c_str (),
  2763. par_state->expression_context_block, 1);
  2764. if (sym)
  2765. yylval.theclass.type = sym->type ();
  2766. return CLASSNAME;
  2767. }
  2768. }
  2769. /* Input names that aren't symbols but ARE valid hex numbers, when
  2770. the input radix permits them, can be names or numbers depending
  2771. on the parse. Note we support radixes > 16 here. */
  2772. if (!bsym.symbol
  2773. && ((copy[0] >= 'a' && copy[0] < 'a' + input_radix - 10)
  2774. || (copy[0] >= 'A' && copy[0] < 'A' + input_radix - 10)))
  2775. {
  2776. YYSTYPE newlval; /* Its value is ignored. */
  2777. int hextype = parse_number (par_state, copy.c_str (), yylval.sval.length,
  2778. 0, &newlval);
  2779. if (hextype == INT)
  2780. {
  2781. yylval.ssym.sym = bsym;
  2782. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  2783. return NAME_OR_INT;
  2784. }
  2785. }
  2786. /* Any other kind of symbol */
  2787. yylval.ssym.sym = bsym;
  2788. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  2789. if (bsym.symbol == NULL
  2790. && par_state->language ()->la_language == language_cplus
  2791. && is_a_field_of_this.type == NULL
  2792. && lookup_minimal_symbol (copy.c_str (), NULL, NULL).minsym == NULL)
  2793. return UNKNOWN_CPP_NAME;
  2794. return NAME;
  2795. }
  2796. /* Like classify_name, but used by the inner loop of the lexer, when a
  2797. name might have already been seen. CONTEXT is the context type, or
  2798. NULL if this is the first component of a name. */
  2799. static int
  2800. classify_inner_name (struct parser_state *par_state,
  2801. const struct block *block, struct type *context)
  2802. {
  2803. struct type *type;
  2804. if (context == NULL)
  2805. return classify_name (par_state, block, false, false);
  2806. type = check_typedef (context);
  2807. if (!type_aggregate_p (type))
  2808. return ERROR;
  2809. std::string copy = copy_name (yylval.ssym.stoken);
  2810. /* N.B. We assume the symbol can only be in VAR_DOMAIN. */
  2811. yylval.ssym.sym = cp_lookup_nested_symbol (type, copy.c_str (), block,
  2812. VAR_DOMAIN);
  2813. /* If no symbol was found, search for a matching base class named
  2814. COPY. This will allow users to enter qualified names of class members
  2815. relative to the `this' pointer. */
  2816. if (yylval.ssym.sym.symbol == NULL)
  2817. {
  2818. struct type *base_type = cp_find_type_baseclass_by_name (type,
  2819. copy.c_str ());
  2820. if (base_type != NULL)
  2821. {
  2822. yylval.tsym.type = base_type;
  2823. return TYPENAME;
  2824. }
  2825. return ERROR;
  2826. }
  2827. switch (yylval.ssym.sym.symbol->aclass ())
  2828. {
  2829. case LOC_BLOCK:
  2830. case LOC_LABEL:
  2831. /* cp_lookup_nested_symbol might have accidentally found a constructor
  2832. named COPY when we really wanted a base class of the same name.
  2833. Double-check this case by looking for a base class. */
  2834. {
  2835. struct type *base_type
  2836. = cp_find_type_baseclass_by_name (type, copy.c_str ());
  2837. if (base_type != NULL)
  2838. {
  2839. yylval.tsym.type = base_type;
  2840. return TYPENAME;
  2841. }
  2842. }
  2843. return ERROR;
  2844. case LOC_TYPEDEF:
  2845. yylval.tsym.type = yylval.ssym.sym.symbol->type ();
  2846. return TYPENAME;
  2847. default:
  2848. return NAME;
  2849. }
  2850. internal_error (__FILE__, __LINE__, _("not reached"));
  2851. }
  2852. /* The outer level of a two-level lexer. This calls the inner lexer
  2853. to return tokens. It then either returns these tokens, or
  2854. aggregates them into a larger token. This lets us work around a
  2855. problem in our parsing approach, where the parser could not
  2856. distinguish between qualified names and qualified types at the
  2857. right point.
  2858. This approach is still not ideal, because it mishandles template
  2859. types. See the comment in lex_one_token for an example. However,
  2860. this is still an improvement over the earlier approach, and will
  2861. suffice until we move to better parsing technology. */
  2862. static int
  2863. yylex (void)
  2864. {
  2865. token_and_value current;
  2866. int first_was_coloncolon, last_was_coloncolon;
  2867. struct type *context_type = NULL;
  2868. int last_to_examine, next_to_examine, checkpoint;
  2869. const struct block *search_block;
  2870. bool is_quoted_name, last_lex_was_structop;
  2871. if (popping && !token_fifo.empty ())
  2872. goto do_pop;
  2873. popping = 0;
  2874. last_lex_was_structop = last_was_structop;
  2875. /* Read the first token and decide what to do. Most of the
  2876. subsequent code is C++-only; but also depends on seeing a "::" or
  2877. name-like token. */
  2878. current.token = lex_one_token (pstate, &is_quoted_name);
  2879. if (current.token == NAME)
  2880. current.token = classify_name (pstate, pstate->expression_context_block,
  2881. is_quoted_name, last_lex_was_structop);
  2882. if (pstate->language ()->la_language != language_cplus
  2883. || (current.token != TYPENAME && current.token != COLONCOLON
  2884. && current.token != FILENAME))
  2885. return current.token;
  2886. /* Read any sequence of alternating "::" and name-like tokens into
  2887. the token FIFO. */
  2888. current.value = yylval;
  2889. token_fifo.push_back (current);
  2890. last_was_coloncolon = current.token == COLONCOLON;
  2891. while (1)
  2892. {
  2893. bool ignore;
  2894. /* We ignore quoted names other than the very first one.
  2895. Subsequent ones do not have any special meaning. */
  2896. current.token = lex_one_token (pstate, &ignore);
  2897. current.value = yylval;
  2898. token_fifo.push_back (current);
  2899. if ((last_was_coloncolon && current.token != NAME)
  2900. || (!last_was_coloncolon && current.token != COLONCOLON))
  2901. break;
  2902. last_was_coloncolon = !last_was_coloncolon;
  2903. }
  2904. popping = 1;
  2905. /* We always read one extra token, so compute the number of tokens
  2906. to examine accordingly. */
  2907. last_to_examine = token_fifo.size () - 2;
  2908. next_to_examine = 0;
  2909. current = token_fifo[next_to_examine];
  2910. ++next_to_examine;
  2911. name_obstack.clear ();
  2912. checkpoint = 0;
  2913. if (current.token == FILENAME)
  2914. search_block = current.value.bval;
  2915. else if (current.token == COLONCOLON)
  2916. search_block = NULL;
  2917. else
  2918. {
  2919. gdb_assert (current.token == TYPENAME);
  2920. search_block = pstate->expression_context_block;
  2921. obstack_grow (&name_obstack, current.value.sval.ptr,
  2922. current.value.sval.length);
  2923. context_type = current.value.tsym.type;
  2924. checkpoint = 1;
  2925. }
  2926. first_was_coloncolon = current.token == COLONCOLON;
  2927. last_was_coloncolon = first_was_coloncolon;
  2928. while (next_to_examine <= last_to_examine)
  2929. {
  2930. token_and_value next;
  2931. next = token_fifo[next_to_examine];
  2932. ++next_to_examine;
  2933. if (next.token == NAME && last_was_coloncolon)
  2934. {
  2935. int classification;
  2936. yylval = next.value;
  2937. classification = classify_inner_name (pstate, search_block,
  2938. context_type);
  2939. /* We keep going until we either run out of names, or until
  2940. we have a qualified name which is not a type. */
  2941. if (classification != TYPENAME && classification != NAME)
  2942. break;
  2943. /* Accept up to this token. */
  2944. checkpoint = next_to_examine;
  2945. /* Update the partial name we are constructing. */
  2946. if (context_type != NULL)
  2947. {
  2948. /* We don't want to put a leading "::" into the name. */
  2949. obstack_grow_str (&name_obstack, "::");
  2950. }
  2951. obstack_grow (&name_obstack, next.value.sval.ptr,
  2952. next.value.sval.length);
  2953. yylval.sval.ptr = (const char *) obstack_base (&name_obstack);
  2954. yylval.sval.length = obstack_object_size (&name_obstack);
  2955. current.value = yylval;
  2956. current.token = classification;
  2957. last_was_coloncolon = 0;
  2958. if (classification == NAME)
  2959. break;
  2960. context_type = yylval.tsym.type;
  2961. }
  2962. else if (next.token == COLONCOLON && !last_was_coloncolon)
  2963. last_was_coloncolon = 1;
  2964. else
  2965. {
  2966. /* We've reached the end of the name. */
  2967. break;
  2968. }
  2969. }
  2970. /* If we have a replacement token, install it as the first token in
  2971. the FIFO, and delete the other constituent tokens. */
  2972. if (checkpoint > 0)
  2973. {
  2974. current.value.sval.ptr
  2975. = obstack_strndup (&cpstate->expansion_obstack,
  2976. current.value.sval.ptr,
  2977. current.value.sval.length);
  2978. token_fifo[0] = current;
  2979. if (checkpoint > 1)
  2980. token_fifo.erase (token_fifo.begin () + 1,
  2981. token_fifo.begin () + checkpoint);
  2982. }
  2983. do_pop:
  2984. current = token_fifo[0];
  2985. token_fifo.erase (token_fifo.begin ());
  2986. yylval = current.value;
  2987. return current.token;
  2988. }
  2989. int
  2990. c_parse (struct parser_state *par_state)
  2991. {
  2992. /* Setting up the parser state. */
  2993. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  2994. gdb_assert (par_state != NULL);
  2995. pstate = par_state;
  2996. c_parse_state cstate;
  2997. scoped_restore cstate_restore = make_scoped_restore (&cpstate, &cstate);
  2998. gdb::unique_xmalloc_ptr<struct macro_scope> macro_scope;
  2999. if (par_state->expression_context_block)
  3000. macro_scope
  3001. = sal_macro_scope (find_pc_line (par_state->expression_context_pc, 0));
  3002. else
  3003. macro_scope = default_macro_scope ();
  3004. if (! macro_scope)
  3005. macro_scope = user_macro_scope ();
  3006. scoped_restore restore_macro_scope
  3007. = make_scoped_restore (&expression_macro_scope, macro_scope.get ());
  3008. scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
  3009. parser_debug);
  3010. /* Initialize some state used by the lexer. */
  3011. last_was_structop = false;
  3012. saw_name_at_eof = 0;
  3013. paren_depth = 0;
  3014. token_fifo.clear ();
  3015. popping = 0;
  3016. name_obstack.clear ();
  3017. int result = yyparse ();
  3018. if (!result)
  3019. pstate->set_operation (pstate->pop ());
  3020. return result;
  3021. }
  3022. #if defined(YYBISON) && YYBISON < 30800
  3023. /* This is called via the YYPRINT macro when parser debugging is
  3024. enabled. It prints a token's value. */
  3025. static void
  3026. c_print_token (FILE *file, int type, YYSTYPE value)
  3027. {
  3028. switch (type)
  3029. {
  3030. case INT:
  3031. parser_fprintf (file, "typed_val_int<%s, %s>",
  3032. TYPE_SAFE_NAME (value.typed_val_int.type),
  3033. pulongest (value.typed_val_int.val));
  3034. break;
  3035. case CHAR:
  3036. case STRING:
  3037. {
  3038. char *copy = (char *) alloca (value.tsval.length + 1);
  3039. memcpy (copy, value.tsval.ptr, value.tsval.length);
  3040. copy[value.tsval.length] = '\0';
  3041. parser_fprintf (file, "tsval<type=%d, %s>", value.tsval.type, copy);
  3042. }
  3043. break;
  3044. case NSSTRING:
  3045. case DOLLAR_VARIABLE:
  3046. parser_fprintf (file, "sval<%s>", copy_name (value.sval).c_str ());
  3047. break;
  3048. case TYPENAME:
  3049. parser_fprintf (file, "tsym<type=%s, name=%s>",
  3050. TYPE_SAFE_NAME (value.tsym.type),
  3051. copy_name (value.tsym.stoken).c_str ());
  3052. break;
  3053. case NAME:
  3054. case UNKNOWN_CPP_NAME:
  3055. case NAME_OR_INT:
  3056. case BLOCKNAME:
  3057. parser_fprintf (file, "ssym<name=%s, sym=%s, field_of_this=%d>",
  3058. copy_name (value.ssym.stoken).c_str (),
  3059. (value.ssym.sym.symbol == NULL
  3060. ? "(null)" : value.ssym.sym.symbol->print_name ()),
  3061. value.ssym.is_a_field_of_this);
  3062. break;
  3063. case FILENAME:
  3064. parser_fprintf (file, "bval<%s>", host_address_to_string (value.bval));
  3065. break;
  3066. }
  3067. }
  3068. #endif
  3069. static void
  3070. yyerror (const char *msg)
  3071. {
  3072. if (pstate->prev_lexptr)
  3073. pstate->lexptr = pstate->prev_lexptr;
  3074. error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
  3075. }