p-exp.y 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /* YACC parser for Pascal expressions, for GDB.
  2. Copyright (C) 2000-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. /* This file is derived from c-exp.y */
  15. /* Parse a Pascal expression from text in a string,
  16. and return the result as a struct expression pointer.
  17. That structure contains arithmetic operations in reverse polish,
  18. with constants represented by operations that are followed by special data.
  19. See expression.h for the details of the format.
  20. What is important here is that it can be built up sequentially
  21. during the process of parsing; the lower levels of the tree always
  22. come first in the result.
  23. Note that malloc's and realloc's in this file are transformed to
  24. xmalloc and xrealloc respectively by the same sed command in the
  25. makefile that remaps any other malloc/realloc inserted by the parser
  26. generator. Doing this with #defines and trying to control the interaction
  27. with include files (<malloc.h> and <stdlib.h> for example) just became
  28. too messy, particularly when such includes can be inserted at random
  29. times by the parser generator. */
  30. /* Known bugs or limitations:
  31. - pascal string operations are not supported at all.
  32. - there are some problems with boolean types.
  33. - Pascal type hexadecimal constants are not supported
  34. because they conflict with the internal variables format.
  35. Probably also lots of other problems, less well defined PM. */
  36. %{
  37. #include "defs.h"
  38. #include <ctype.h>
  39. #include "expression.h"
  40. #include "value.h"
  41. #include "parser-defs.h"
  42. #include "language.h"
  43. #include "p-lang.h"
  44. #include "bfd.h" /* Required by objfiles.h. */
  45. #include "symfile.h" /* Required by objfiles.h. */
  46. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols. */
  47. #include "block.h"
  48. #include "completer.h"
  49. #include "expop.h"
  50. #define parse_type(ps) builtin_type (ps->gdbarch ())
  51. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
  52. etc). */
  53. #define GDB_YY_REMAP_PREFIX pascal_
  54. #include "yy-remap.h"
  55. /* The state of the parser, used internally when we are parsing the
  56. expression. */
  57. static struct parser_state *pstate = NULL;
  58. /* Depth of parentheses. */
  59. static int paren_depth;
  60. int yyparse (void);
  61. static int yylex (void);
  62. static void yyerror (const char *);
  63. static char *uptok (const char *, int);
  64. using namespace expr;
  65. %}
  66. /* Although the yacc "value" of an expression is not used,
  67. since the result is stored in the structure being created,
  68. other node types do have values. */
  69. %union
  70. {
  71. LONGEST lval;
  72. struct {
  73. LONGEST val;
  74. struct type *type;
  75. } typed_val_int;
  76. struct {
  77. gdb_byte val[16];
  78. struct type *type;
  79. } typed_val_float;
  80. struct symbol *sym;
  81. struct type *tval;
  82. struct stoken sval;
  83. struct ttype tsym;
  84. struct symtoken ssym;
  85. int voidval;
  86. const struct block *bval;
  87. enum exp_opcode opcode;
  88. struct internalvar *ivar;
  89. struct type **tvec;
  90. int *ivec;
  91. }
  92. %{
  93. /* YYSTYPE gets defined by %union */
  94. static int parse_number (struct parser_state *,
  95. const char *, int, int, YYSTYPE *);
  96. static struct type *current_type;
  97. static int leftdiv_is_integer;
  98. static void push_current_type (void);
  99. static void pop_current_type (void);
  100. static int search_field;
  101. %}
  102. %type <voidval> exp exp1 type_exp start normal_start variable qualified_name
  103. %type <tval> type typebase
  104. /* %type <bval> block */
  105. /* Fancy type parsing. */
  106. %type <tval> ptype
  107. %token <typed_val_int> INT
  108. %token <typed_val_float> FLOAT
  109. /* Both NAME and TYPENAME tokens represent symbols in the input,
  110. and both convey their data as strings.
  111. But a TYPENAME is a string that happens to be defined as a typedef
  112. or builtin type name (such as int or char)
  113. and a NAME is any other symbol.
  114. Contexts where this distinction is not important can use the
  115. nonterminal "name", which matches either NAME or TYPENAME. */
  116. %token <sval> STRING
  117. %token <sval> FIELDNAME
  118. %token <voidval> COMPLETE
  119. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  120. %token <tsym> TYPENAME
  121. %type <sval> name
  122. %type <ssym> name_not_typename
  123. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  124. but which would parse as a valid number in the current input radix.
  125. E.g. "c" when input_radix==16. Depending on the parse, it will be
  126. turned into a name or into a number. */
  127. %token <ssym> NAME_OR_INT
  128. %token STRUCT CLASS SIZEOF COLONCOLON
  129. %token ERROR
  130. /* Special type cases, put in to allow the parser to distinguish different
  131. legal basetypes. */
  132. %token <sval> DOLLAR_VARIABLE
  133. /* Object pascal */
  134. %token THIS
  135. %token <lval> TRUEKEYWORD FALSEKEYWORD
  136. %left ','
  137. %left ABOVE_COMMA
  138. %right ASSIGN
  139. %left NOT
  140. %left OR
  141. %left XOR
  142. %left ANDAND
  143. %left '=' NOTEQUAL
  144. %left '<' '>' LEQ GEQ
  145. %left LSH RSH DIV MOD
  146. %left '@'
  147. %left '+' '-'
  148. %left '*' '/'
  149. %right UNARY INCREMENT DECREMENT
  150. %right ARROW '.' '[' '('
  151. %left '^'
  152. %token <ssym> BLOCKNAME
  153. %type <bval> block
  154. %left COLONCOLON
  155. %%
  156. start : { current_type = NULL;
  157. search_field = 0;
  158. leftdiv_is_integer = 0;
  159. }
  160. normal_start {}
  161. ;
  162. normal_start :
  163. exp1
  164. | type_exp
  165. ;
  166. type_exp: type
  167. {
  168. pstate->push_new<type_operation> ($1);
  169. current_type = $1; } ;
  170. /* Expressions, including the comma operator. */
  171. exp1 : exp
  172. | exp1 ',' exp
  173. { pstate->wrap2<comma_operation> (); }
  174. ;
  175. /* Expressions, not including the comma operator. */
  176. exp : exp '^' %prec UNARY
  177. { pstate->wrap<unop_ind_operation> ();
  178. if (current_type)
  179. current_type = TYPE_TARGET_TYPE (current_type); }
  180. ;
  181. exp : '@' exp %prec UNARY
  182. { pstate->wrap<unop_addr_operation> ();
  183. if (current_type)
  184. current_type = TYPE_POINTER_TYPE (current_type); }
  185. ;
  186. exp : '-' exp %prec UNARY
  187. { pstate->wrap<unary_neg_operation> (); }
  188. ;
  189. exp : NOT exp %prec UNARY
  190. { pstate->wrap<unary_logical_not_operation> (); }
  191. ;
  192. exp : INCREMENT '(' exp ')' %prec UNARY
  193. { pstate->wrap<preinc_operation> (); }
  194. ;
  195. exp : DECREMENT '(' exp ')' %prec UNARY
  196. { pstate->wrap<predec_operation> (); }
  197. ;
  198. field_exp : exp '.' %prec UNARY
  199. { search_field = 1; }
  200. ;
  201. exp : field_exp FIELDNAME
  202. {
  203. pstate->push_new<structop_operation>
  204. (pstate->pop (), copy_name ($2));
  205. search_field = 0;
  206. if (current_type)
  207. {
  208. while (current_type->code ()
  209. == TYPE_CODE_PTR)
  210. current_type =
  211. TYPE_TARGET_TYPE (current_type);
  212. current_type = lookup_struct_elt_type (
  213. current_type, $2.ptr, 0);
  214. }
  215. }
  216. ;
  217. exp : field_exp name
  218. {
  219. pstate->push_new<structop_operation>
  220. (pstate->pop (), copy_name ($2));
  221. search_field = 0;
  222. if (current_type)
  223. {
  224. while (current_type->code ()
  225. == TYPE_CODE_PTR)
  226. current_type =
  227. TYPE_TARGET_TYPE (current_type);
  228. current_type = lookup_struct_elt_type (
  229. current_type, $2.ptr, 0);
  230. }
  231. }
  232. ;
  233. exp : field_exp name COMPLETE
  234. {
  235. structop_base_operation *op
  236. = new structop_ptr_operation (pstate->pop (),
  237. copy_name ($2));
  238. pstate->mark_struct_expression (op);
  239. pstate->push (operation_up (op));
  240. }
  241. ;
  242. exp : field_exp COMPLETE
  243. {
  244. structop_base_operation *op
  245. = new structop_ptr_operation (pstate->pop (), "");
  246. pstate->mark_struct_expression (op);
  247. pstate->push (operation_up (op));
  248. }
  249. ;
  250. exp : exp '['
  251. /* We need to save the current_type value. */
  252. { const char *arrayname;
  253. int arrayfieldindex
  254. = pascal_is_string_type (current_type, NULL, NULL,
  255. NULL, NULL, &arrayname);
  256. if (arrayfieldindex)
  257. {
  258. current_type
  259. = (current_type
  260. ->field (arrayfieldindex - 1).type ());
  261. pstate->push_new<structop_operation>
  262. (pstate->pop (), arrayname);
  263. }
  264. push_current_type (); }
  265. exp1 ']'
  266. { pop_current_type ();
  267. pstate->wrap2<subscript_operation> ();
  268. if (current_type)
  269. current_type = TYPE_TARGET_TYPE (current_type); }
  270. ;
  271. exp : exp '('
  272. /* This is to save the value of arglist_len
  273. being accumulated by an outer function call. */
  274. { push_current_type ();
  275. pstate->start_arglist (); }
  276. arglist ')' %prec ARROW
  277. {
  278. std::vector<operation_up> args
  279. = pstate->pop_vector (pstate->end_arglist ());
  280. pstate->push_new<funcall_operation>
  281. (pstate->pop (), std::move (args));
  282. pop_current_type ();
  283. if (current_type)
  284. current_type = TYPE_TARGET_TYPE (current_type);
  285. }
  286. ;
  287. arglist :
  288. | exp
  289. { pstate->arglist_len = 1; }
  290. | arglist ',' exp %prec ABOVE_COMMA
  291. { pstate->arglist_len++; }
  292. ;
  293. exp : type '(' exp ')' %prec UNARY
  294. { if (current_type)
  295. {
  296. /* Allow automatic dereference of classes. */
  297. if ((current_type->code () == TYPE_CODE_PTR)
  298. && (TYPE_TARGET_TYPE (current_type)->code () == TYPE_CODE_STRUCT)
  299. && (($1)->code () == TYPE_CODE_STRUCT))
  300. pstate->wrap<unop_ind_operation> ();
  301. }
  302. pstate->push_new<unop_cast_operation>
  303. (pstate->pop (), $1);
  304. current_type = $1; }
  305. ;
  306. exp : '(' exp1 ')'
  307. { }
  308. ;
  309. /* Binary operators in order of decreasing precedence. */
  310. exp : exp '*' exp
  311. { pstate->wrap2<mul_operation> (); }
  312. ;
  313. exp : exp '/' {
  314. if (current_type && is_integral_type (current_type))
  315. leftdiv_is_integer = 1;
  316. }
  317. exp
  318. {
  319. if (leftdiv_is_integer && current_type
  320. && is_integral_type (current_type))
  321. {
  322. pstate->push_new<unop_cast_operation>
  323. (pstate->pop (),
  324. parse_type (pstate)->builtin_long_double);
  325. current_type
  326. = parse_type (pstate)->builtin_long_double;
  327. leftdiv_is_integer = 0;
  328. }
  329. pstate->wrap2<div_operation> ();
  330. }
  331. ;
  332. exp : exp DIV exp
  333. { pstate->wrap2<intdiv_operation> (); }
  334. ;
  335. exp : exp MOD exp
  336. { pstate->wrap2<rem_operation> (); }
  337. ;
  338. exp : exp '+' exp
  339. { pstate->wrap2<add_operation> (); }
  340. ;
  341. exp : exp '-' exp
  342. { pstate->wrap2<sub_operation> (); }
  343. ;
  344. exp : exp LSH exp
  345. { pstate->wrap2<lsh_operation> (); }
  346. ;
  347. exp : exp RSH exp
  348. { pstate->wrap2<rsh_operation> (); }
  349. ;
  350. exp : exp '=' exp
  351. {
  352. pstate->wrap2<equal_operation> ();
  353. current_type = parse_type (pstate)->builtin_bool;
  354. }
  355. ;
  356. exp : exp NOTEQUAL exp
  357. {
  358. pstate->wrap2<notequal_operation> ();
  359. current_type = parse_type (pstate)->builtin_bool;
  360. }
  361. ;
  362. exp : exp LEQ exp
  363. {
  364. pstate->wrap2<leq_operation> ();
  365. current_type = parse_type (pstate)->builtin_bool;
  366. }
  367. ;
  368. exp : exp GEQ exp
  369. {
  370. pstate->wrap2<geq_operation> ();
  371. current_type = parse_type (pstate)->builtin_bool;
  372. }
  373. ;
  374. exp : exp '<' exp
  375. {
  376. pstate->wrap2<less_operation> ();
  377. current_type = parse_type (pstate)->builtin_bool;
  378. }
  379. ;
  380. exp : exp '>' exp
  381. {
  382. pstate->wrap2<gtr_operation> ();
  383. current_type = parse_type (pstate)->builtin_bool;
  384. }
  385. ;
  386. exp : exp ANDAND exp
  387. { pstate->wrap2<bitwise_and_operation> (); }
  388. ;
  389. exp : exp XOR exp
  390. { pstate->wrap2<bitwise_xor_operation> (); }
  391. ;
  392. exp : exp OR exp
  393. { pstate->wrap2<bitwise_ior_operation> (); }
  394. ;
  395. exp : exp ASSIGN exp
  396. { pstate->wrap2<assign_operation> (); }
  397. ;
  398. exp : TRUEKEYWORD
  399. {
  400. pstate->push_new<bool_operation> ($1);
  401. current_type = parse_type (pstate)->builtin_bool;
  402. }
  403. ;
  404. exp : FALSEKEYWORD
  405. {
  406. pstate->push_new<bool_operation> ($1);
  407. current_type = parse_type (pstate)->builtin_bool;
  408. }
  409. ;
  410. exp : INT
  411. {
  412. pstate->push_new<long_const_operation>
  413. ($1.type, $1.val);
  414. current_type = $1.type;
  415. }
  416. ;
  417. exp : NAME_OR_INT
  418. { YYSTYPE val;
  419. parse_number (pstate, $1.stoken.ptr,
  420. $1.stoken.length, 0, &val);
  421. pstate->push_new<long_const_operation>
  422. (val.typed_val_int.type,
  423. val.typed_val_int.val);
  424. current_type = val.typed_val_int.type;
  425. }
  426. ;
  427. exp : FLOAT
  428. {
  429. float_data data;
  430. std::copy (std::begin ($1.val), std::end ($1.val),
  431. std::begin (data));
  432. pstate->push_new<float_const_operation> ($1.type, data);
  433. }
  434. ;
  435. exp : variable
  436. ;
  437. exp : DOLLAR_VARIABLE
  438. {
  439. pstate->push_dollar ($1);
  440. /* $ is the normal prefix for pascal
  441. hexadecimal values but this conflicts
  442. with the GDB use for debugger variables
  443. so in expression to enter hexadecimal
  444. values we still need to use C syntax with
  445. 0xff */
  446. std::string tmp ($1.ptr, $1.length);
  447. /* Handle current_type. */
  448. struct internalvar *intvar
  449. = lookup_only_internalvar (tmp.c_str () + 1);
  450. if (intvar != nullptr)
  451. {
  452. scoped_value_mark mark;
  453. value *val
  454. = value_of_internalvar (pstate->gdbarch (),
  455. intvar);
  456. current_type = value_type (val);
  457. }
  458. }
  459. ;
  460. exp : SIZEOF '(' type ')' %prec UNARY
  461. {
  462. current_type = parse_type (pstate)->builtin_int;
  463. $3 = check_typedef ($3);
  464. pstate->push_new<long_const_operation>
  465. (parse_type (pstate)->builtin_int,
  466. TYPE_LENGTH ($3)); }
  467. ;
  468. exp : SIZEOF '(' exp ')' %prec UNARY
  469. { pstate->wrap<unop_sizeof_operation> ();
  470. current_type = parse_type (pstate)->builtin_int; }
  471. exp : STRING
  472. { /* C strings are converted into array constants with
  473. an explicit null byte added at the end. Thus
  474. the array upper bound is the string length.
  475. There is no such thing in C as a completely empty
  476. string. */
  477. const char *sp = $1.ptr; int count = $1.length;
  478. std::vector<operation_up> args (count + 1);
  479. for (int i = 0; i < count; ++i)
  480. args[i] = (make_operation<long_const_operation>
  481. (parse_type (pstate)->builtin_char,
  482. *sp++));
  483. args[count] = (make_operation<long_const_operation>
  484. (parse_type (pstate)->builtin_char,
  485. '\0'));
  486. pstate->push_new<array_operation>
  487. (0, $1.length, std::move (args));
  488. }
  489. ;
  490. /* Object pascal */
  491. exp : THIS
  492. {
  493. struct value * this_val;
  494. struct type * this_type;
  495. pstate->push_new<op_this_operation> ();
  496. /* We need type of this. */
  497. this_val
  498. = value_of_this_silent (pstate->language ());
  499. if (this_val)
  500. this_type = value_type (this_val);
  501. else
  502. this_type = NULL;
  503. if (this_type)
  504. {
  505. if (this_type->code () == TYPE_CODE_PTR)
  506. {
  507. this_type = TYPE_TARGET_TYPE (this_type);
  508. pstate->wrap<unop_ind_operation> ();
  509. }
  510. }
  511. current_type = this_type;
  512. }
  513. ;
  514. /* end of object pascal. */
  515. block : BLOCKNAME
  516. {
  517. if ($1.sym.symbol != 0)
  518. $$ = SYMBOL_BLOCK_VALUE ($1.sym.symbol);
  519. else
  520. {
  521. std::string copy = copy_name ($1.stoken);
  522. struct symtab *tem =
  523. lookup_symtab (copy.c_str ());
  524. if (tem)
  525. $$ = BLOCKVECTOR_BLOCK
  526. (tem->compunit ()->blockvector (),
  527. STATIC_BLOCK);
  528. else
  529. error (_("No file or function \"%s\"."),
  530. copy.c_str ());
  531. }
  532. }
  533. ;
  534. block : block COLONCOLON name
  535. {
  536. std::string copy = copy_name ($3);
  537. struct symbol *tem
  538. = lookup_symbol (copy.c_str (), $1,
  539. VAR_DOMAIN, NULL).symbol;
  540. if (!tem || tem->aclass () != LOC_BLOCK)
  541. error (_("No function \"%s\" in specified context."),
  542. copy.c_str ());
  543. $$ = SYMBOL_BLOCK_VALUE (tem); }
  544. ;
  545. variable: block COLONCOLON name
  546. { struct block_symbol sym;
  547. std::string copy = copy_name ($3);
  548. sym = lookup_symbol (copy.c_str (), $1,
  549. VAR_DOMAIN, NULL);
  550. if (sym.symbol == 0)
  551. error (_("No symbol \"%s\" in specified context."),
  552. copy.c_str ());
  553. pstate->push_new<var_value_operation> (sym);
  554. }
  555. ;
  556. qualified_name: typebase COLONCOLON name
  557. {
  558. struct type *type = $1;
  559. if (type->code () != TYPE_CODE_STRUCT
  560. && type->code () != TYPE_CODE_UNION)
  561. error (_("`%s' is not defined as an aggregate type."),
  562. type->name ());
  563. pstate->push_new<scope_operation>
  564. (type, copy_name ($3));
  565. }
  566. ;
  567. variable: qualified_name
  568. | COLONCOLON name
  569. {
  570. std::string name = copy_name ($2);
  571. struct block_symbol sym
  572. = lookup_symbol (name.c_str (), nullptr,
  573. VAR_DOMAIN, nullptr);
  574. pstate->push_symbol (name.c_str (), sym);
  575. }
  576. ;
  577. variable: name_not_typename
  578. { struct block_symbol sym = $1.sym;
  579. if (sym.symbol)
  580. {
  581. if (symbol_read_needs_frame (sym.symbol))
  582. pstate->block_tracker->update (sym);
  583. pstate->push_new<var_value_operation> (sym);
  584. current_type = sym.symbol->type (); }
  585. else if ($1.is_a_field_of_this)
  586. {
  587. struct value * this_val;
  588. struct type * this_type;
  589. /* Object pascal: it hangs off of `this'. Must
  590. not inadvertently convert from a method call
  591. to data ref. */
  592. pstate->block_tracker->update (sym);
  593. operation_up thisop
  594. = make_operation<op_this_operation> ();
  595. pstate->push_new<structop_operation>
  596. (std::move (thisop), copy_name ($1.stoken));
  597. /* We need type of this. */
  598. this_val
  599. = value_of_this_silent (pstate->language ());
  600. if (this_val)
  601. this_type = value_type (this_val);
  602. else
  603. this_type = NULL;
  604. if (this_type)
  605. current_type = lookup_struct_elt_type (
  606. this_type,
  607. copy_name ($1.stoken).c_str (), 0);
  608. else
  609. current_type = NULL;
  610. }
  611. else
  612. {
  613. struct bound_minimal_symbol msymbol;
  614. std::string arg = copy_name ($1.stoken);
  615. msymbol =
  616. lookup_bound_minimal_symbol (arg.c_str ());
  617. if (msymbol.minsym != NULL)
  618. pstate->push_new<var_msym_value_operation>
  619. (msymbol);
  620. else if (!have_full_symbols ()
  621. && !have_partial_symbols ())
  622. error (_("No symbol table is loaded. "
  623. "Use the \"file\" command."));
  624. else
  625. error (_("No symbol \"%s\" in current context."),
  626. arg.c_str ());
  627. }
  628. }
  629. ;
  630. ptype : typebase
  631. ;
  632. /* We used to try to recognize more pointer to member types here, but
  633. that didn't work (shift/reduce conflicts meant that these rules never
  634. got executed). The problem is that
  635. int (foo::bar::baz::bizzle)
  636. is a function type but
  637. int (foo::bar::baz::bizzle::*)
  638. is a pointer to member type. Stroustrup loses again! */
  639. type : ptype
  640. ;
  641. typebase /* Implements (approximately): (type-qualifier)* type-specifier */
  642. : '^' typebase
  643. { $$ = lookup_pointer_type ($2); }
  644. | TYPENAME
  645. { $$ = $1.type; }
  646. | STRUCT name
  647. { $$
  648. = lookup_struct (copy_name ($2).c_str (),
  649. pstate->expression_context_block);
  650. }
  651. | CLASS name
  652. { $$
  653. = lookup_struct (copy_name ($2).c_str (),
  654. pstate->expression_context_block);
  655. }
  656. /* "const" and "volatile" are curently ignored. A type qualifier
  657. after the type is handled in the ptype rule. I think these could
  658. be too. */
  659. ;
  660. name : NAME { $$ = $1.stoken; }
  661. | BLOCKNAME { $$ = $1.stoken; }
  662. | TYPENAME { $$ = $1.stoken; }
  663. | NAME_OR_INT { $$ = $1.stoken; }
  664. ;
  665. name_not_typename : NAME
  666. | BLOCKNAME
  667. /* These would be useful if name_not_typename was useful, but it is just
  668. a fake for "variable", so these cause reduce/reduce conflicts because
  669. the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  670. =exp) or just an exp. If name_not_typename was ever used in an lvalue
  671. context where only a name could occur, this might be useful.
  672. | NAME_OR_INT
  673. */
  674. ;
  675. %%
  676. /* Take care of parsing a number (anything that starts with a digit).
  677. Set yylval and return the token type; update lexptr.
  678. LEN is the number of characters in it. */
  679. /*** Needs some error checking for the float case ***/
  680. static int
  681. parse_number (struct parser_state *par_state,
  682. const char *p, int len, int parsed_float, YYSTYPE *putithere)
  683. {
  684. ULONGEST n = 0;
  685. ULONGEST prevn = 0;
  686. ULONGEST un;
  687. int i = 0;
  688. int c;
  689. int base = input_radix;
  690. int unsigned_p = 0;
  691. /* Number of "L" suffixes encountered. */
  692. int long_p = 0;
  693. /* We have found a "L" or "U" suffix. */
  694. int found_suffix = 0;
  695. ULONGEST high_bit;
  696. struct type *signed_type;
  697. struct type *unsigned_type;
  698. if (parsed_float)
  699. {
  700. /* Handle suffixes: 'f' for float, 'l' for long double.
  701. FIXME: This appears to be an extension -- do we want this? */
  702. if (len >= 1 && tolower (p[len - 1]) == 'f')
  703. {
  704. putithere->typed_val_float.type
  705. = parse_type (par_state)->builtin_float;
  706. len--;
  707. }
  708. else if (len >= 1 && tolower (p[len - 1]) == 'l')
  709. {
  710. putithere->typed_val_float.type
  711. = parse_type (par_state)->builtin_long_double;
  712. len--;
  713. }
  714. /* Default type for floating-point literals is double. */
  715. else
  716. {
  717. putithere->typed_val_float.type
  718. = parse_type (par_state)->builtin_double;
  719. }
  720. if (!parse_float (p, len,
  721. putithere->typed_val_float.type,
  722. putithere->typed_val_float.val))
  723. return ERROR;
  724. return FLOAT;
  725. }
  726. /* Handle base-switching prefixes 0x, 0t, 0d, 0. */
  727. if (p[0] == '0' && len > 1)
  728. switch (p[1])
  729. {
  730. case 'x':
  731. case 'X':
  732. if (len >= 3)
  733. {
  734. p += 2;
  735. base = 16;
  736. len -= 2;
  737. }
  738. break;
  739. case 't':
  740. case 'T':
  741. case 'd':
  742. case 'D':
  743. if (len >= 3)
  744. {
  745. p += 2;
  746. base = 10;
  747. len -= 2;
  748. }
  749. break;
  750. default:
  751. base = 8;
  752. break;
  753. }
  754. while (len-- > 0)
  755. {
  756. c = *p++;
  757. if (c >= 'A' && c <= 'Z')
  758. c += 'a' - 'A';
  759. if (c != 'l' && c != 'u')
  760. n *= base;
  761. if (c >= '0' && c <= '9')
  762. {
  763. if (found_suffix)
  764. return ERROR;
  765. n += i = c - '0';
  766. }
  767. else
  768. {
  769. if (base > 10 && c >= 'a' && c <= 'f')
  770. {
  771. if (found_suffix)
  772. return ERROR;
  773. n += i = c - 'a' + 10;
  774. }
  775. else if (c == 'l')
  776. {
  777. ++long_p;
  778. found_suffix = 1;
  779. }
  780. else if (c == 'u')
  781. {
  782. unsigned_p = 1;
  783. found_suffix = 1;
  784. }
  785. else
  786. return ERROR; /* Char not a digit */
  787. }
  788. if (i >= base)
  789. return ERROR; /* Invalid digit in this base. */
  790. /* Portably test for overflow (only works for nonzero values, so make
  791. a second check for zero). FIXME: Can't we just make n and prevn
  792. unsigned and avoid this? */
  793. if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
  794. unsigned_p = 1; /* Try something unsigned. */
  795. /* Portably test for unsigned overflow.
  796. FIXME: This check is wrong; for example it doesn't find overflow
  797. on 0x123456789 when LONGEST is 32 bits. */
  798. if (c != 'l' && c != 'u' && n != 0)
  799. {
  800. if (unsigned_p && prevn >= n)
  801. error (_("Numeric constant too large."));
  802. }
  803. prevn = n;
  804. }
  805. /* An integer constant is an int, a long, or a long long. An L
  806. suffix forces it to be long; an LL suffix forces it to be long
  807. long. If not forced to a larger size, it gets the first type of
  808. the above that it fits in. To figure out whether it fits, we
  809. shift it right and see whether anything remains. Note that we
  810. can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
  811. operation, because many compilers will warn about such a shift
  812. (which always produces a zero result). Sometimes gdbarch_int_bit
  813. or gdbarch_long_bit will be that big, sometimes not. To deal with
  814. the case where it is we just always shift the value more than
  815. once, with fewer bits each time. */
  816. un = n >> 2;
  817. if (long_p == 0
  818. && (un >> (gdbarch_int_bit (par_state->gdbarch ()) - 2)) == 0)
  819. {
  820. high_bit
  821. = ((ULONGEST)1) << (gdbarch_int_bit (par_state->gdbarch ()) - 1);
  822. /* A large decimal (not hex or octal) constant (between INT_MAX
  823. and UINT_MAX) is a long or unsigned long, according to ANSI,
  824. never an unsigned int, but this code treats it as unsigned
  825. int. This probably should be fixed. GCC gives a warning on
  826. such constants. */
  827. unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  828. signed_type = parse_type (par_state)->builtin_int;
  829. }
  830. else if (long_p <= 1
  831. && (un >> (gdbarch_long_bit (par_state->gdbarch ()) - 2)) == 0)
  832. {
  833. high_bit
  834. = ((ULONGEST)1) << (gdbarch_long_bit (par_state->gdbarch ()) - 1);
  835. unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  836. signed_type = parse_type (par_state)->builtin_long;
  837. }
  838. else
  839. {
  840. int shift;
  841. if (sizeof (ULONGEST) * HOST_CHAR_BIT
  842. < gdbarch_long_long_bit (par_state->gdbarch ()))
  843. /* A long long does not fit in a LONGEST. */
  844. shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
  845. else
  846. shift = (gdbarch_long_long_bit (par_state->gdbarch ()) - 1);
  847. high_bit = (ULONGEST) 1 << shift;
  848. unsigned_type = parse_type (par_state)->builtin_unsigned_long_long;
  849. signed_type = parse_type (par_state)->builtin_long_long;
  850. }
  851. putithere->typed_val_int.val = n;
  852. /* If the high bit of the worked out type is set then this number
  853. has to be unsigned. */
  854. if (unsigned_p || (n & high_bit))
  855. {
  856. putithere->typed_val_int.type = unsigned_type;
  857. }
  858. else
  859. {
  860. putithere->typed_val_int.type = signed_type;
  861. }
  862. return INT;
  863. }
  864. struct type_push
  865. {
  866. struct type *stored;
  867. struct type_push *next;
  868. };
  869. static struct type_push *tp_top = NULL;
  870. static void
  871. push_current_type (void)
  872. {
  873. struct type_push *tpnew;
  874. tpnew = (struct type_push *) malloc (sizeof (struct type_push));
  875. tpnew->next = tp_top;
  876. tpnew->stored = current_type;
  877. current_type = NULL;
  878. tp_top = tpnew;
  879. }
  880. static void
  881. pop_current_type (void)
  882. {
  883. struct type_push *tp = tp_top;
  884. if (tp)
  885. {
  886. current_type = tp->stored;
  887. tp_top = tp->next;
  888. free (tp);
  889. }
  890. }
  891. struct token
  892. {
  893. const char *oper;
  894. int token;
  895. enum exp_opcode opcode;
  896. };
  897. static const struct token tokentab3[] =
  898. {
  899. {"shr", RSH, OP_NULL},
  900. {"shl", LSH, OP_NULL},
  901. {"and", ANDAND, OP_NULL},
  902. {"div", DIV, OP_NULL},
  903. {"not", NOT, OP_NULL},
  904. {"mod", MOD, OP_NULL},
  905. {"inc", INCREMENT, OP_NULL},
  906. {"dec", DECREMENT, OP_NULL},
  907. {"xor", XOR, OP_NULL}
  908. };
  909. static const struct token tokentab2[] =
  910. {
  911. {"or", OR, OP_NULL},
  912. {"<>", NOTEQUAL, OP_NULL},
  913. {"<=", LEQ, OP_NULL},
  914. {">=", GEQ, OP_NULL},
  915. {":=", ASSIGN, OP_NULL},
  916. {"::", COLONCOLON, OP_NULL} };
  917. /* Allocate uppercased var: */
  918. /* make an uppercased copy of tokstart. */
  919. static char *
  920. uptok (const char *tokstart, int namelen)
  921. {
  922. int i;
  923. char *uptokstart = (char *)malloc(namelen+1);
  924. for (i = 0;i <= namelen;i++)
  925. {
  926. if ((tokstart[i]>='a' && tokstart[i]<='z'))
  927. uptokstart[i] = tokstart[i]-('a'-'A');
  928. else
  929. uptokstart[i] = tokstart[i];
  930. }
  931. uptokstart[namelen]='\0';
  932. return uptokstart;
  933. }
  934. /* Read one token, getting characters through lexptr. */
  935. static int
  936. yylex (void)
  937. {
  938. int c;
  939. int namelen;
  940. const char *tokstart;
  941. char *uptokstart;
  942. const char *tokptr;
  943. int explen, tempbufindex;
  944. static char *tempbuf;
  945. static int tempbufsize;
  946. retry:
  947. pstate->prev_lexptr = pstate->lexptr;
  948. tokstart = pstate->lexptr;
  949. explen = strlen (pstate->lexptr);
  950. /* See if it is a special token of length 3. */
  951. if (explen > 2)
  952. for (const auto &token : tokentab3)
  953. if (strncasecmp (tokstart, token.oper, 3) == 0
  954. && (!isalpha (token.oper[0]) || explen == 3
  955. || (!isalpha (tokstart[3])
  956. && !isdigit (tokstart[3]) && tokstart[3] != '_')))
  957. {
  958. pstate->lexptr += 3;
  959. yylval.opcode = token.opcode;
  960. return token.token;
  961. }
  962. /* See if it is a special token of length 2. */
  963. if (explen > 1)
  964. for (const auto &token : tokentab2)
  965. if (strncasecmp (tokstart, token.oper, 2) == 0
  966. && (!isalpha (token.oper[0]) || explen == 2
  967. || (!isalpha (tokstart[2])
  968. && !isdigit (tokstart[2]) && tokstart[2] != '_')))
  969. {
  970. pstate->lexptr += 2;
  971. yylval.opcode = token.opcode;
  972. return token.token;
  973. }
  974. switch (c = *tokstart)
  975. {
  976. case 0:
  977. if (search_field && pstate->parse_completion)
  978. return COMPLETE;
  979. else
  980. return 0;
  981. case ' ':
  982. case '\t':
  983. case '\n':
  984. pstate->lexptr++;
  985. goto retry;
  986. case '\'':
  987. /* We either have a character constant ('0' or '\177' for example)
  988. or we have a quoted symbol reference ('foo(int,int)' in object pascal
  989. for example). */
  990. pstate->lexptr++;
  991. c = *pstate->lexptr++;
  992. if (c == '\\')
  993. c = parse_escape (pstate->gdbarch (), &pstate->lexptr);
  994. else if (c == '\'')
  995. error (_("Empty character constant."));
  996. yylval.typed_val_int.val = c;
  997. yylval.typed_val_int.type = parse_type (pstate)->builtin_char;
  998. c = *pstate->lexptr++;
  999. if (c != '\'')
  1000. {
  1001. namelen = skip_quoted (tokstart) - tokstart;
  1002. if (namelen > 2)
  1003. {
  1004. pstate->lexptr = tokstart + namelen;
  1005. if (pstate->lexptr[-1] != '\'')
  1006. error (_("Unmatched single quote."));
  1007. namelen -= 2;
  1008. tokstart++;
  1009. uptokstart = uptok(tokstart,namelen);
  1010. goto tryname;
  1011. }
  1012. error (_("Invalid character constant."));
  1013. }
  1014. return INT;
  1015. case '(':
  1016. paren_depth++;
  1017. pstate->lexptr++;
  1018. return c;
  1019. case ')':
  1020. if (paren_depth == 0)
  1021. return 0;
  1022. paren_depth--;
  1023. pstate->lexptr++;
  1024. return c;
  1025. case ',':
  1026. if (pstate->comma_terminates && paren_depth == 0)
  1027. return 0;
  1028. pstate->lexptr++;
  1029. return c;
  1030. case '.':
  1031. /* Might be a floating point number. */
  1032. if (pstate->lexptr[1] < '0' || pstate->lexptr[1] > '9')
  1033. {
  1034. goto symbol; /* Nope, must be a symbol. */
  1035. }
  1036. /* FALL THRU. */
  1037. case '0':
  1038. case '1':
  1039. case '2':
  1040. case '3':
  1041. case '4':
  1042. case '5':
  1043. case '6':
  1044. case '7':
  1045. case '8':
  1046. case '9':
  1047. {
  1048. /* It's a number. */
  1049. int got_dot = 0, got_e = 0, toktype;
  1050. const char *p = tokstart;
  1051. int hex = input_radix > 10;
  1052. if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1053. {
  1054. p += 2;
  1055. hex = 1;
  1056. }
  1057. else if (c == '0' && (p[1]=='t' || p[1]=='T'
  1058. || p[1]=='d' || p[1]=='D'))
  1059. {
  1060. p += 2;
  1061. hex = 0;
  1062. }
  1063. for (;; ++p)
  1064. {
  1065. /* This test includes !hex because 'e' is a valid hex digit
  1066. and thus does not indicate a floating point number when
  1067. the radix is hex. */
  1068. if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1069. got_dot = got_e = 1;
  1070. /* This test does not include !hex, because a '.' always indicates
  1071. a decimal floating point number regardless of the radix. */
  1072. else if (!got_dot && *p == '.')
  1073. got_dot = 1;
  1074. else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1075. && (*p == '-' || *p == '+'))
  1076. /* This is the sign of the exponent, not the end of the
  1077. number. */
  1078. continue;
  1079. /* We will take any letters or digits. parse_number will
  1080. complain if past the radix, or if L or U are not final. */
  1081. else if ((*p < '0' || *p > '9')
  1082. && ((*p < 'a' || *p > 'z')
  1083. && (*p < 'A' || *p > 'Z')))
  1084. break;
  1085. }
  1086. toktype = parse_number (pstate, tokstart,
  1087. p - tokstart, got_dot | got_e, &yylval);
  1088. if (toktype == ERROR)
  1089. {
  1090. char *err_copy = (char *) alloca (p - tokstart + 1);
  1091. memcpy (err_copy, tokstart, p - tokstart);
  1092. err_copy[p - tokstart] = 0;
  1093. error (_("Invalid number \"%s\"."), err_copy);
  1094. }
  1095. pstate->lexptr = p;
  1096. return toktype;
  1097. }
  1098. case '+':
  1099. case '-':
  1100. case '*':
  1101. case '/':
  1102. case '|':
  1103. case '&':
  1104. case '^':
  1105. case '~':
  1106. case '!':
  1107. case '@':
  1108. case '<':
  1109. case '>':
  1110. case '[':
  1111. case ']':
  1112. case '?':
  1113. case ':':
  1114. case '=':
  1115. case '{':
  1116. case '}':
  1117. symbol:
  1118. pstate->lexptr++;
  1119. return c;
  1120. case '"':
  1121. /* Build the gdb internal form of the input string in tempbuf,
  1122. translating any standard C escape forms seen. Note that the
  1123. buffer is null byte terminated *only* for the convenience of
  1124. debugging gdb itself and printing the buffer contents when
  1125. the buffer contains no embedded nulls. Gdb does not depend
  1126. upon the buffer being null byte terminated, it uses the length
  1127. string instead. This allows gdb to handle C strings (as well
  1128. as strings in other languages) with embedded null bytes. */
  1129. tokptr = ++tokstart;
  1130. tempbufindex = 0;
  1131. do {
  1132. /* Grow the static temp buffer if necessary, including allocating
  1133. the first one on demand. */
  1134. if (tempbufindex + 1 >= tempbufsize)
  1135. {
  1136. tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
  1137. }
  1138. switch (*tokptr)
  1139. {
  1140. case '\0':
  1141. case '"':
  1142. /* Do nothing, loop will terminate. */
  1143. break;
  1144. case '\\':
  1145. ++tokptr;
  1146. c = parse_escape (pstate->gdbarch (), &tokptr);
  1147. if (c == -1)
  1148. {
  1149. continue;
  1150. }
  1151. tempbuf[tempbufindex++] = c;
  1152. break;
  1153. default:
  1154. tempbuf[tempbufindex++] = *tokptr++;
  1155. break;
  1156. }
  1157. } while ((*tokptr != '"') && (*tokptr != '\0'));
  1158. if (*tokptr++ != '"')
  1159. {
  1160. error (_("Unterminated string in expression."));
  1161. }
  1162. tempbuf[tempbufindex] = '\0'; /* See note above. */
  1163. yylval.sval.ptr = tempbuf;
  1164. yylval.sval.length = tempbufindex;
  1165. pstate->lexptr = tokptr;
  1166. return (STRING);
  1167. }
  1168. if (!(c == '_' || c == '$'
  1169. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1170. /* We must have come across a bad character (e.g. ';'). */
  1171. error (_("Invalid character '%c' in expression."), c);
  1172. /* It's a name. See how long it is. */
  1173. namelen = 0;
  1174. for (c = tokstart[namelen];
  1175. (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1176. || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
  1177. {
  1178. /* Template parameter lists are part of the name.
  1179. FIXME: This mishandles `print $a<4&&$a>3'. */
  1180. if (c == '<')
  1181. {
  1182. int i = namelen;
  1183. int nesting_level = 1;
  1184. while (tokstart[++i])
  1185. {
  1186. if (tokstart[i] == '<')
  1187. nesting_level++;
  1188. else if (tokstart[i] == '>')
  1189. {
  1190. if (--nesting_level == 0)
  1191. break;
  1192. }
  1193. }
  1194. if (tokstart[i] == '>')
  1195. namelen = i;
  1196. else
  1197. break;
  1198. }
  1199. /* do NOT uppercase internals because of registers !!! */
  1200. c = tokstart[++namelen];
  1201. }
  1202. uptokstart = uptok(tokstart,namelen);
  1203. /* The token "if" terminates the expression and is NOT
  1204. removed from the input stream. */
  1205. if (namelen == 2 && uptokstart[0] == 'I' && uptokstart[1] == 'F')
  1206. {
  1207. free (uptokstart);
  1208. return 0;
  1209. }
  1210. pstate->lexptr += namelen;
  1211. tryname:
  1212. /* Catch specific keywords. Should be done with a data structure. */
  1213. switch (namelen)
  1214. {
  1215. case 6:
  1216. if (strcmp (uptokstart, "OBJECT") == 0)
  1217. {
  1218. free (uptokstart);
  1219. return CLASS;
  1220. }
  1221. if (strcmp (uptokstart, "RECORD") == 0)
  1222. {
  1223. free (uptokstart);
  1224. return STRUCT;
  1225. }
  1226. if (strcmp (uptokstart, "SIZEOF") == 0)
  1227. {
  1228. free (uptokstart);
  1229. return SIZEOF;
  1230. }
  1231. break;
  1232. case 5:
  1233. if (strcmp (uptokstart, "CLASS") == 0)
  1234. {
  1235. free (uptokstart);
  1236. return CLASS;
  1237. }
  1238. if (strcmp (uptokstart, "FALSE") == 0)
  1239. {
  1240. yylval.lval = 0;
  1241. free (uptokstart);
  1242. return FALSEKEYWORD;
  1243. }
  1244. break;
  1245. case 4:
  1246. if (strcmp (uptokstart, "TRUE") == 0)
  1247. {
  1248. yylval.lval = 1;
  1249. free (uptokstart);
  1250. return TRUEKEYWORD;
  1251. }
  1252. if (strcmp (uptokstart, "SELF") == 0)
  1253. {
  1254. /* Here we search for 'this' like
  1255. inserted in FPC stabs debug info. */
  1256. static const char this_name[] = "this";
  1257. if (lookup_symbol (this_name, pstate->expression_context_block,
  1258. VAR_DOMAIN, NULL).symbol)
  1259. {
  1260. free (uptokstart);
  1261. return THIS;
  1262. }
  1263. }
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. yylval.sval.ptr = tokstart;
  1269. yylval.sval.length = namelen;
  1270. if (*tokstart == '$')
  1271. {
  1272. free (uptokstart);
  1273. return DOLLAR_VARIABLE;
  1274. }
  1275. /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1276. functions or symtabs. If this is not so, then ...
  1277. Use token-type TYPENAME for symbols that happen to be defined
  1278. currently as names of types; NAME for other symbols.
  1279. The caller is not constrained to care about the distinction. */
  1280. {
  1281. std::string tmp = copy_name (yylval.sval);
  1282. struct symbol *sym;
  1283. struct field_of_this_result is_a_field_of_this;
  1284. int is_a_field = 0;
  1285. int hextype;
  1286. is_a_field_of_this.type = NULL;
  1287. if (search_field && current_type)
  1288. is_a_field = (lookup_struct_elt_type (current_type,
  1289. tmp.c_str (), 1) != NULL);
  1290. if (is_a_field)
  1291. sym = NULL;
  1292. else
  1293. sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
  1294. VAR_DOMAIN, &is_a_field_of_this).symbol;
  1295. /* second chance uppercased (as Free Pascal does). */
  1296. if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
  1297. {
  1298. for (int i = 0; i <= namelen; i++)
  1299. {
  1300. if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1301. tmp[i] -= ('a'-'A');
  1302. }
  1303. if (search_field && current_type)
  1304. is_a_field = (lookup_struct_elt_type (current_type,
  1305. tmp.c_str (), 1) != NULL);
  1306. if (is_a_field)
  1307. sym = NULL;
  1308. else
  1309. sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
  1310. VAR_DOMAIN, &is_a_field_of_this).symbol;
  1311. }
  1312. /* Third chance Capitalized (as GPC does). */
  1313. if (!sym && is_a_field_of_this.type == NULL && !is_a_field)
  1314. {
  1315. for (int i = 0; i <= namelen; i++)
  1316. {
  1317. if (i == 0)
  1318. {
  1319. if ((tmp[i] >= 'a' && tmp[i] <= 'z'))
  1320. tmp[i] -= ('a'-'A');
  1321. }
  1322. else
  1323. if ((tmp[i] >= 'A' && tmp[i] <= 'Z'))
  1324. tmp[i] -= ('A'-'a');
  1325. }
  1326. if (search_field && current_type)
  1327. is_a_field = (lookup_struct_elt_type (current_type,
  1328. tmp.c_str (), 1) != NULL);
  1329. if (is_a_field)
  1330. sym = NULL;
  1331. else
  1332. sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
  1333. VAR_DOMAIN, &is_a_field_of_this).symbol;
  1334. }
  1335. if (is_a_field || (is_a_field_of_this.type != NULL))
  1336. {
  1337. tempbuf = (char *) realloc (tempbuf, namelen + 1);
  1338. strncpy (tempbuf, tmp.c_str (), namelen);
  1339. tempbuf [namelen] = 0;
  1340. yylval.sval.ptr = tempbuf;
  1341. yylval.sval.length = namelen;
  1342. yylval.ssym.sym.symbol = NULL;
  1343. yylval.ssym.sym.block = NULL;
  1344. free (uptokstart);
  1345. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1346. if (is_a_field)
  1347. return FIELDNAME;
  1348. else
  1349. return NAME;
  1350. }
  1351. /* Call lookup_symtab, not lookup_partial_symtab, in case there are
  1352. no psymtabs (coff, xcoff, or some future change to blow away the
  1353. psymtabs once once symbols are read). */
  1354. if ((sym && sym->aclass () == LOC_BLOCK)
  1355. || lookup_symtab (tmp.c_str ()))
  1356. {
  1357. yylval.ssym.sym.symbol = sym;
  1358. yylval.ssym.sym.block = NULL;
  1359. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1360. free (uptokstart);
  1361. return BLOCKNAME;
  1362. }
  1363. if (sym && sym->aclass () == LOC_TYPEDEF)
  1364. {
  1365. #if 1
  1366. /* Despite the following flaw, we need to keep this code enabled.
  1367. Because we can get called from check_stub_method, if we don't
  1368. handle nested types then it screws many operations in any
  1369. program which uses nested types. */
  1370. /* In "A::x", if x is a member function of A and there happens
  1371. to be a type (nested or not, since the stabs don't make that
  1372. distinction) named x, then this code incorrectly thinks we
  1373. are dealing with nested types rather than a member function. */
  1374. const char *p;
  1375. const char *namestart;
  1376. struct symbol *best_sym;
  1377. /* Look ahead to detect nested types. This probably should be
  1378. done in the grammar, but trying seemed to introduce a lot
  1379. of shift/reduce and reduce/reduce conflicts. It's possible
  1380. that it could be done, though. Or perhaps a non-grammar, but
  1381. less ad hoc, approach would work well. */
  1382. /* Since we do not currently have any way of distinguishing
  1383. a nested type from a non-nested one (the stabs don't tell
  1384. us whether a type is nested), we just ignore the
  1385. containing type. */
  1386. p = pstate->lexptr;
  1387. best_sym = sym;
  1388. while (1)
  1389. {
  1390. /* Skip whitespace. */
  1391. while (*p == ' ' || *p == '\t' || *p == '\n')
  1392. ++p;
  1393. if (*p == ':' && p[1] == ':')
  1394. {
  1395. /* Skip the `::'. */
  1396. p += 2;
  1397. /* Skip whitespace. */
  1398. while (*p == ' ' || *p == '\t' || *p == '\n')
  1399. ++p;
  1400. namestart = p;
  1401. while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
  1402. || (*p >= 'a' && *p <= 'z')
  1403. || (*p >= 'A' && *p <= 'Z'))
  1404. ++p;
  1405. if (p != namestart)
  1406. {
  1407. struct symbol *cur_sym;
  1408. /* As big as the whole rest of the expression, which is
  1409. at least big enough. */
  1410. char *ncopy
  1411. = (char *) alloca (tmp.size () + strlen (namestart)
  1412. + 3);
  1413. char *tmp1;
  1414. tmp1 = ncopy;
  1415. memcpy (tmp1, tmp.c_str (), tmp.size ());
  1416. tmp1 += tmp.size ();
  1417. memcpy (tmp1, "::", 2);
  1418. tmp1 += 2;
  1419. memcpy (tmp1, namestart, p - namestart);
  1420. tmp1[p - namestart] = '\0';
  1421. cur_sym
  1422. = lookup_symbol (ncopy,
  1423. pstate->expression_context_block,
  1424. VAR_DOMAIN, NULL).symbol;
  1425. if (cur_sym)
  1426. {
  1427. if (cur_sym->aclass () == LOC_TYPEDEF)
  1428. {
  1429. best_sym = cur_sym;
  1430. pstate->lexptr = p;
  1431. }
  1432. else
  1433. break;
  1434. }
  1435. else
  1436. break;
  1437. }
  1438. else
  1439. break;
  1440. }
  1441. else
  1442. break;
  1443. }
  1444. yylval.tsym.type = best_sym->type ();
  1445. #else /* not 0 */
  1446. yylval.tsym.type = sym->type ();
  1447. #endif /* not 0 */
  1448. free (uptokstart);
  1449. return TYPENAME;
  1450. }
  1451. yylval.tsym.type
  1452. = language_lookup_primitive_type (pstate->language (),
  1453. pstate->gdbarch (), tmp.c_str ());
  1454. if (yylval.tsym.type != NULL)
  1455. {
  1456. free (uptokstart);
  1457. return TYPENAME;
  1458. }
  1459. /* Input names that aren't symbols but ARE valid hex numbers,
  1460. when the input radix permits them, can be names or numbers
  1461. depending on the parse. Note we support radixes > 16 here. */
  1462. if (!sym
  1463. && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1464. || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1465. {
  1466. YYSTYPE newlval; /* Its value is ignored. */
  1467. hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1468. if (hextype == INT)
  1469. {
  1470. yylval.ssym.sym.symbol = sym;
  1471. yylval.ssym.sym.block = NULL;
  1472. yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1473. free (uptokstart);
  1474. return NAME_OR_INT;
  1475. }
  1476. }
  1477. free(uptokstart);
  1478. /* Any other kind of symbol. */
  1479. yylval.ssym.sym.symbol = sym;
  1480. yylval.ssym.sym.block = NULL;
  1481. return NAME;
  1482. }
  1483. }
  1484. /* See language.h. */
  1485. int
  1486. pascal_language::parser (struct parser_state *par_state) const
  1487. {
  1488. /* Setting up the parser state. */
  1489. scoped_restore pstate_restore = make_scoped_restore (&pstate);
  1490. gdb_assert (par_state != NULL);
  1491. pstate = par_state;
  1492. paren_depth = 0;
  1493. int result = yyparse ();
  1494. if (!result)
  1495. pstate->set_operation (pstate->pop ());
  1496. return result;
  1497. }
  1498. static void
  1499. yyerror (const char *msg)
  1500. {
  1501. if (pstate->prev_lexptr)
  1502. pstate->lexptr = pstate->prev_lexptr;
  1503. error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
  1504. }