Change dwarf2_cu::method_info to be a std::vector
[external/binutils.git] / gdb / m2-exp.y
1 /* YACC grammar for Modula-2 expressions, for GDB.
2    Copyright (C) 1986-2018 Free Software Foundation, Inc.
3    Generated from expread.y (now c-exp.y) and contributed by the Department
4    of Computer Science at the State University of New York at Buffalo, 1991.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Parse a Modula-2 expression from text in a string,
22    and return the result as a  struct expression  pointer.
23    That structure contains arithmetic operations in reverse polish,
24    with constants represented by operations that are followed by special data.
25    See expression.h for the details of the format.
26    What is important here is that it can be built up sequentially
27    during the process of parsing; the lower levels of the tree always
28    come first in the result.
29
30    Note that malloc's and realloc's in this file are transformed to
31    xmalloc and xrealloc respectively by the same sed command in the
32    makefile that remaps any other malloc/realloc inserted by the parser
33    generator.  Doing this with #defines and trying to control the interaction
34    with include files (<malloc.h> and <stdlib.h> for example) just became
35    too messy, particularly when such includes can be inserted at random
36    times by the parser generator.  */
37    
38 %{
39
40 #include "defs.h"
41 #include "expression.h"
42 #include "language.h"
43 #include "value.h"
44 #include "parser-defs.h"
45 #include "m2-lang.h"
46 #include "bfd.h" /* Required by objfiles.h.  */
47 #include "symfile.h" /* Required by objfiles.h.  */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49 #include "block.h"
50
51 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
52 #define parse_m2_type(ps) builtin_m2_type (parse_gdbarch (ps))
53
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
55    etc).  */
56 #define GDB_YY_REMAP_PREFIX m2_
57 #include "yy-remap.h"
58
59 /* The state of the parser, used internally when we are parsing the
60    expression.  */
61
62 static struct parser_state *pstate = NULL;
63
64 int yyparse (void);
65
66 static int yylex (void);
67
68 void yyerror (const char *);
69
70 static int parse_number (int);
71
72 /* The sign of the number being parsed.  */
73 static int number_sign = 1;
74
75 %}
76
77 /* Although the yacc "value" of an expression is not used,
78    since the result is stored in the structure being created,
79    other node types do have values.  */
80
81 %union
82   {
83     LONGEST lval;
84     ULONGEST ulval;
85     gdb_byte val[16];
86     struct symbol *sym;
87     struct type *tval;
88     struct stoken sval;
89     int voidval;
90     const struct block *bval;
91     enum exp_opcode opcode;
92     struct internalvar *ivar;
93
94     struct type **tvec;
95     int *ivec;
96   }
97
98 %type <voidval> exp type_exp start set
99 %type <voidval> variable
100 %type <tval> type
101 %type <bval> block 
102 %type <sym> fblock 
103
104 %token <lval> INT HEX ERROR
105 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
106 %token <val> FLOAT
107
108 /* Both NAME and TYPENAME tokens represent symbols in the input,
109    and both convey their data as strings.
110    But a TYPENAME is a string that happens to be defined as a typedef
111    or builtin type name (such as int or char)
112    and a NAME is any other symbol.
113
114    Contexts where this distinction is not important can use the
115    nonterminal "name", which matches either NAME or TYPENAME.  */
116
117 %token <sval> STRING
118 %token <sval> NAME BLOCKNAME IDENT VARNAME
119 %token <sval> TYPENAME
120
121 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
122 %token TSIZE
123 %token INC DEC INCL EXCL
124
125 /* The GDB scope operator */
126 %token COLONCOLON
127
128 %token <voidval> INTERNAL_VAR
129
130 /* M2 tokens */
131 %left ','
132 %left ABOVE_COMMA
133 %nonassoc ASSIGN
134 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
135 %left OROR
136 %left LOGICAL_AND '&'
137 %left '@'
138 %left '+' '-'
139 %left '*' '/' DIV MOD
140 %right UNARY
141 %right '^' DOT '[' '('
142 %right NOT '~'
143 %left COLONCOLON QID
144 /* This is not an actual token ; it is used for precedence. 
145 %right QID
146 */
147
148 \f
149 %%
150
151 start   :       exp
152         |       type_exp
153         ;
154
155 type_exp:       type
156                 { write_exp_elt_opcode (pstate, OP_TYPE);
157                   write_exp_elt_type (pstate, $1);
158                   write_exp_elt_opcode (pstate, OP_TYPE);
159                 }
160         ;
161
162 /* Expressions */
163
164 exp     :       exp '^'   %prec UNARY
165                         { write_exp_elt_opcode (pstate, UNOP_IND); }
166         ;
167
168 exp     :       '-'
169                         { number_sign = -1; }
170                 exp    %prec UNARY
171                         { number_sign = 1;
172                           write_exp_elt_opcode (pstate, UNOP_NEG); }
173         ;
174
175 exp     :       '+' exp    %prec UNARY
176                 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
177         ;
178
179 exp     :       not_exp exp %prec UNARY
180                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
181         ;
182
183 not_exp :       NOT
184         |       '~'
185         ;
186
187 exp     :       CAP '(' exp ')'
188                         { write_exp_elt_opcode (pstate, UNOP_CAP); }
189         ;
190
191 exp     :       ORD '(' exp ')'
192                         { write_exp_elt_opcode (pstate, UNOP_ORD); }
193         ;
194
195 exp     :       ABS '(' exp ')'
196                         { write_exp_elt_opcode (pstate, UNOP_ABS); }
197         ;
198
199 exp     :       HIGH '(' exp ')'
200                         { write_exp_elt_opcode (pstate, UNOP_HIGH); }
201         ;
202
203 exp     :       MIN_FUNC '(' type ')'
204                         { write_exp_elt_opcode (pstate, UNOP_MIN);
205                           write_exp_elt_type (pstate, $3);
206                           write_exp_elt_opcode (pstate, UNOP_MIN); }
207         ;
208
209 exp     :       MAX_FUNC '(' type ')'
210                         { write_exp_elt_opcode (pstate, UNOP_MAX);
211                           write_exp_elt_type (pstate, $3);
212                           write_exp_elt_opcode (pstate, UNOP_MAX); }
213         ;
214
215 exp     :       FLOAT_FUNC '(' exp ')'
216                         { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
217         ;
218
219 exp     :       VAL '(' type ',' exp ')'
220                         { write_exp_elt_opcode (pstate, BINOP_VAL);
221                           write_exp_elt_type (pstate, $3);
222                           write_exp_elt_opcode (pstate, BINOP_VAL); }
223         ;
224
225 exp     :       CHR '(' exp ')'
226                         { write_exp_elt_opcode (pstate, UNOP_CHR); }
227         ;
228
229 exp     :       ODD '(' exp ')'
230                         { write_exp_elt_opcode (pstate, UNOP_ODD); }
231         ;
232
233 exp     :       TRUNC '(' exp ')'
234                         { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
235         ;
236
237 exp     :       TSIZE '(' exp ')'
238                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
239         ;
240
241 exp     :       SIZE exp       %prec UNARY
242                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
243         ;
244
245
246 exp     :       INC '(' exp ')'
247                         { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
248         ;
249
250 exp     :       INC '(' exp ',' exp ')'
251                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
252                           write_exp_elt_opcode (pstate, BINOP_ADD);
253                           write_exp_elt_opcode (pstate,
254                                                 BINOP_ASSIGN_MODIFY); }
255         ;
256
257 exp     :       DEC '(' exp ')'
258                         { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
259         ;
260
261 exp     :       DEC '(' exp ',' exp ')'
262                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
263                           write_exp_elt_opcode (pstate, BINOP_SUB);
264                           write_exp_elt_opcode (pstate,
265                                                 BINOP_ASSIGN_MODIFY); }
266         ;
267
268 exp     :       exp DOT NAME
269                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
270                           write_exp_string (pstate, $3);
271                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
272         ;
273
274 exp     :       set
275         ;
276
277 exp     :       exp IN set
278                         { error (_("Sets are not implemented."));}
279         ;
280
281 exp     :       INCL '(' exp ',' exp ')'
282                         { error (_("Sets are not implemented."));}
283         ;
284
285 exp     :       EXCL '(' exp ',' exp ')'
286                         { error (_("Sets are not implemented."));}
287         ;
288
289 set     :       '{' arglist '}'
290                         { error (_("Sets are not implemented."));}
291         |       type '{' arglist '}'
292                         { error (_("Sets are not implemented."));}
293         ;
294
295
296 /* Modula-2 array subscript notation [a,b,c...] */
297 exp     :       exp '['
298                         /* This function just saves the number of arguments
299                            that follow in the list.  It is *not* specific to
300                            function types */
301                         { start_arglist(); }
302                 non_empty_arglist ']'  %prec DOT
303                         { write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
304                           write_exp_elt_longcst (pstate,
305                                                  (LONGEST) end_arglist());
306                           write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); }
307         ;
308
309 exp     :       exp '[' exp ']'
310                         { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
311         ;
312
313 exp     :       exp '('
314                         /* This is to save the value of arglist_len
315                            being accumulated by an outer function call.  */
316                         { start_arglist (); }
317                 arglist ')'     %prec DOT
318                         { write_exp_elt_opcode (pstate, OP_FUNCALL);
319                           write_exp_elt_longcst (pstate,
320                                                  (LONGEST) end_arglist ());
321                           write_exp_elt_opcode (pstate, OP_FUNCALL); }
322         ;
323
324 arglist :
325         ;
326
327 arglist :       exp
328                         { arglist_len = 1; }
329         ;
330
331 arglist :       arglist ',' exp   %prec ABOVE_COMMA
332                         { arglist_len++; }
333         ;
334
335 non_empty_arglist
336         :       exp
337                         { arglist_len = 1; }
338         ;
339
340 non_empty_arglist
341         :       non_empty_arglist ',' exp %prec ABOVE_COMMA
342                         { arglist_len++; }
343         ;
344
345 /* GDB construct */
346 exp     :       '{' type '}' exp  %prec UNARY
347                         { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
348                           write_exp_elt_type (pstate, $2);
349                           write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
350         ;
351
352 exp     :       type '(' exp ')' %prec UNARY
353                         { write_exp_elt_opcode (pstate, UNOP_CAST);
354                           write_exp_elt_type (pstate, $1);
355                           write_exp_elt_opcode (pstate, UNOP_CAST); }
356         ;
357
358 exp     :       '(' exp ')'
359                         { }
360         ;
361
362 /* Binary operators in order of decreasing precedence.  Note that some
363    of these operators are overloaded!  (ie. sets) */
364
365 /* GDB construct */
366 exp     :       exp '@' exp
367                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
368         ;
369
370 exp     :       exp '*' exp
371                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
372         ;
373
374 exp     :       exp '/' exp
375                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
376         ;
377
378 exp     :       exp DIV exp
379                         { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
380         ;
381
382 exp     :       exp MOD exp
383                         { write_exp_elt_opcode (pstate, BINOP_REM); }
384         ;
385
386 exp     :       exp '+' exp
387                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
388         ;
389
390 exp     :       exp '-' exp
391                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
392         ;
393
394 exp     :       exp '=' exp
395                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
396         ;
397
398 exp     :       exp NOTEQUAL exp
399                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
400         |       exp '#' exp
401                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
402         ;
403
404 exp     :       exp LEQ exp
405                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
406         ;
407
408 exp     :       exp GEQ exp
409                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
410         ;
411
412 exp     :       exp '<' exp
413                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
414         ;
415
416 exp     :       exp '>' exp
417                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
418         ;
419
420 exp     :       exp LOGICAL_AND exp
421                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
422         ;
423
424 exp     :       exp OROR exp
425                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
426         ;
427
428 exp     :       exp ASSIGN exp
429                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
430         ;
431
432
433 /* Constants */
434
435 exp     :       M2_TRUE
436                         { write_exp_elt_opcode (pstate, OP_BOOL);
437                           write_exp_elt_longcst (pstate, (LONGEST) $1);
438                           write_exp_elt_opcode (pstate, OP_BOOL); }
439         ;
440
441 exp     :       M2_FALSE
442                         { write_exp_elt_opcode (pstate, OP_BOOL);
443                           write_exp_elt_longcst (pstate, (LONGEST) $1);
444                           write_exp_elt_opcode (pstate, OP_BOOL); }
445         ;
446
447 exp     :       INT
448                         { write_exp_elt_opcode (pstate, OP_LONG);
449                           write_exp_elt_type (pstate,
450                                         parse_m2_type (pstate)->builtin_int);
451                           write_exp_elt_longcst (pstate, (LONGEST) $1);
452                           write_exp_elt_opcode (pstate, OP_LONG); }
453         ;
454
455 exp     :       UINT
456                         {
457                           write_exp_elt_opcode (pstate, OP_LONG);
458                           write_exp_elt_type (pstate,
459                                               parse_m2_type (pstate)
460                                               ->builtin_card);
461                           write_exp_elt_longcst (pstate, (LONGEST) $1);
462                           write_exp_elt_opcode (pstate, OP_LONG);
463                         }
464         ;
465
466 exp     :       CHAR
467                         { write_exp_elt_opcode (pstate, OP_LONG);
468                           write_exp_elt_type (pstate,
469                                               parse_m2_type (pstate)
470                                               ->builtin_char);
471                           write_exp_elt_longcst (pstate, (LONGEST) $1);
472                           write_exp_elt_opcode (pstate, OP_LONG); }
473         ;
474
475
476 exp     :       FLOAT
477                         { write_exp_elt_opcode (pstate, OP_FLOAT);
478                           write_exp_elt_type (pstate,
479                                               parse_m2_type (pstate)
480                                               ->builtin_real);
481                           write_exp_elt_floatcst (pstate, $1);
482                           write_exp_elt_opcode (pstate, OP_FLOAT); }
483         ;
484
485 exp     :       variable
486         ;
487
488 exp     :       SIZE '(' type ')'       %prec UNARY
489                         { write_exp_elt_opcode (pstate, OP_LONG);
490                           write_exp_elt_type (pstate,
491                                             parse_type (pstate)->builtin_int);
492                           write_exp_elt_longcst (pstate,
493                                                  (LONGEST) TYPE_LENGTH ($3));
494                           write_exp_elt_opcode (pstate, OP_LONG); }
495         ;
496
497 exp     :       STRING
498                         { write_exp_elt_opcode (pstate, OP_M2_STRING);
499                           write_exp_string (pstate, $1);
500                           write_exp_elt_opcode (pstate, OP_M2_STRING); }
501         ;
502
503 /* This will be used for extensions later.  Like adding modules.  */
504 block   :       fblock  
505                         { $$ = SYMBOL_BLOCK_VALUE($1); }
506         ;
507
508 fblock  :       BLOCKNAME
509                         { struct symbol *sym
510                             = lookup_symbol (copy_name ($1),
511                                              expression_context_block,
512                                              VAR_DOMAIN, 0).symbol;
513                           $$ = sym;}
514         ;
515                              
516
517 /* GDB scope operator */
518 fblock  :       block COLONCOLON BLOCKNAME
519                         { struct symbol *tem
520                             = lookup_symbol (copy_name ($3), $1,
521                                              VAR_DOMAIN, 0).symbol;
522                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
523                             error (_("No function \"%s\" in specified context."),
524                                    copy_name ($3));
525                           $$ = tem;
526                         }
527         ;
528
529 /* Useful for assigning to PROCEDURE variables */
530 variable:       fblock
531                         { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
532                           write_exp_elt_block (pstate, NULL);
533                           write_exp_elt_sym (pstate, $1);
534                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
535         ;
536
537 /* GDB internal ($foo) variable */
538 variable:       INTERNAL_VAR
539         ;
540
541 /* GDB scope operator */
542 variable:       block COLONCOLON NAME
543                         { struct block_symbol sym
544                             = lookup_symbol (copy_name ($3), $1,
545                                              VAR_DOMAIN, 0);
546
547                           if (sym.symbol == 0)
548                             error (_("No symbol \"%s\" in specified context."),
549                                    copy_name ($3));
550                           if (symbol_read_needs_frame (sym.symbol))
551                             {
552                               if (innermost_block == 0
553                                   || contained_in (sym.block,
554                                                    innermost_block))
555                                 innermost_block = sym.block;
556                             }
557
558                           write_exp_elt_opcode (pstate, OP_VAR_VALUE);
559                           write_exp_elt_block (pstate, sym.block);
560                           write_exp_elt_sym (pstate, sym.symbol);
561                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
562         ;
563
564 /* Base case for variables.  */
565 variable:       NAME
566                         { struct block_symbol sym;
567                           struct field_of_this_result is_a_field_of_this;
568
569                           sym = lookup_symbol (copy_name ($1),
570                                                expression_context_block,
571                                                VAR_DOMAIN,
572                                                &is_a_field_of_this);
573
574                           if (sym.symbol)
575                             {
576                               if (symbol_read_needs_frame (sym.symbol))
577                                 {
578                                   if (innermost_block == 0 ||
579                                       contained_in (sym.block,
580                                                     innermost_block))
581                                     innermost_block = sym.block;
582                                 }
583
584                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
585                               write_exp_elt_block (pstate, sym.block);
586                               write_exp_elt_sym (pstate, sym.symbol);
587                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
588                             }
589                           else
590                             {
591                               struct bound_minimal_symbol msymbol;
592                               char *arg = copy_name ($1);
593
594                               msymbol =
595                                 lookup_bound_minimal_symbol (arg);
596                               if (msymbol.minsym != NULL)
597                                 write_exp_msymbol (pstate, msymbol);
598                               else if (!have_full_symbols () && !have_partial_symbols ())
599                                 error (_("No symbol table is loaded.  Use the \"symbol-file\" command."));
600                               else
601                                 error (_("No symbol \"%s\" in current context."),
602                                        copy_name ($1));
603                             }
604                         }
605         ;
606
607 type
608         :       TYPENAME
609                         { $$ = lookup_typename (parse_language (pstate),
610                                                 parse_gdbarch (pstate),
611                                                 copy_name ($1),
612                                                 expression_context_block, 0); }
613
614         ;
615
616 %%
617
618 /* Take care of parsing a number (anything that starts with a digit).
619    Set yylval and return the token type; update lexptr.
620    LEN is the number of characters in it.  */
621
622 /*** Needs some error checking for the float case ***/
623
624 static int
625 parse_number (int olen)
626 {
627   const char *p = lexptr;
628   LONGEST n = 0;
629   LONGEST prevn = 0;
630   int c,i,ischar=0;
631   int base = input_radix;
632   int len = olen;
633   int unsigned_p = number_sign == 1 ? 1 : 0;
634
635   if(p[len-1] == 'H')
636   {
637      base = 16;
638      len--;
639   }
640   else if(p[len-1] == 'C' || p[len-1] == 'B')
641   {
642      base = 8;
643      ischar = p[len-1] == 'C';
644      len--;
645   }
646
647   /* Scan the number */
648   for (c = 0; c < len; c++)
649   {
650     if (p[c] == '.' && base == 10)
651       {
652         /* It's a float since it contains a point.  */
653         if (!parse_float (p, len,
654                           parse_m2_type (pstate)->builtin_real,
655                           yylval.val))
656           return ERROR;
657
658         lexptr += len;
659         return FLOAT;
660       }
661     if (p[c] == '.' && base != 10)
662        error (_("Floating point numbers must be base 10."));
663     if (base == 10 && (p[c] < '0' || p[c] > '9'))
664        error (_("Invalid digit \'%c\' in number."),p[c]);
665  }
666
667   while (len-- > 0)
668     {
669       c = *p++;
670       n *= base;
671       if( base == 8 && (c == '8' || c == '9'))
672          error (_("Invalid digit \'%c\' in octal number."),c);
673       if (c >= '0' && c <= '9')
674         i = c - '0';
675       else
676         {
677           if (base == 16 && c >= 'A' && c <= 'F')
678             i = c - 'A' + 10;
679           else
680              return ERROR;
681         }
682       n+=i;
683       if(i >= base)
684          return ERROR;
685       if(!unsigned_p && number_sign == 1 && (prevn >= n))
686          unsigned_p=1;          /* Try something unsigned */
687       /* Don't do the range check if n==i and i==0, since that special
688          case will give an overflow error.  */
689       if(RANGE_CHECK && n!=i && i)
690       {
691          if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
692             ((!unsigned_p && number_sign==-1) && -prevn <= -n))
693             range_error (_("Overflow on numeric constant."));
694       }
695          prevn=n;
696     }
697
698   lexptr = p;
699   if(*p == 'B' || *p == 'C' || *p == 'H')
700      lexptr++;                  /* Advance past B,C or H */
701
702   if (ischar)
703   {
704      yylval.ulval = n;
705      return CHAR;
706   }
707   else if ( unsigned_p && number_sign == 1)
708   {
709      yylval.ulval = n;
710      return UINT;
711   }
712   else if((unsigned_p && (n<0))) {
713      range_error (_("Overflow on numeric constant -- number too large."));
714      /* But, this can return if range_check == range_warn.  */
715   }
716   yylval.lval = n;
717   return INT;
718 }
719
720
721 /* Some tokens */
722
723 static struct
724 {
725    char name[2];
726    int token;
727 } tokentab2[] =
728 {
729     { {'<', '>'},    NOTEQUAL   },
730     { {':', '='},    ASSIGN     },
731     { {'<', '='},    LEQ        },
732     { {'>', '='},    GEQ        },
733     { {':', ':'},    COLONCOLON },
734
735 };
736
737 /* Some specific keywords */
738
739 struct keyword {
740    char keyw[10];
741    int token;
742 };
743
744 static struct keyword keytab[] =
745 {
746     {"OR" ,   OROR       },
747     {"IN",    IN         },/* Note space after IN */
748     {"AND",   LOGICAL_AND},
749     {"ABS",   ABS        },
750     {"CHR",   CHR        },
751     {"DEC",   DEC        },
752     {"NOT",   NOT        },
753     {"DIV",   DIV        },
754     {"INC",   INC        },
755     {"MAX",   MAX_FUNC   },
756     {"MIN",   MIN_FUNC   },
757     {"MOD",   MOD        },
758     {"ODD",   ODD        },
759     {"CAP",   CAP        },
760     {"ORD",   ORD        },
761     {"VAL",   VAL        },
762     {"EXCL",  EXCL       },
763     {"HIGH",  HIGH       },
764     {"INCL",  INCL       },
765     {"SIZE",  SIZE       },
766     {"FLOAT", FLOAT_FUNC },
767     {"TRUNC", TRUNC      },
768     {"TSIZE", SIZE       },
769 };
770
771
772 /* Read one token, getting characters through lexptr.  */
773
774 /* This is where we will check to make sure that the language and the
775    operators used are compatible  */
776
777 static int
778 yylex (void)
779 {
780   int c;
781   int namelen;
782   int i;
783   const char *tokstart;
784   char quote;
785
786  retry:
787
788   prev_lexptr = lexptr;
789
790   tokstart = lexptr;
791
792
793   /* See if it is a special token of length 2 */
794   for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
795      if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
796      {
797         lexptr += 2;
798         return tokentab2[i].token;
799      }
800
801   switch (c = *tokstart)
802     {
803     case 0:
804       return 0;
805
806     case ' ':
807     case '\t':
808     case '\n':
809       lexptr++;
810       goto retry;
811
812     case '(':
813       paren_depth++;
814       lexptr++;
815       return c;
816
817     case ')':
818       if (paren_depth == 0)
819         return 0;
820       paren_depth--;
821       lexptr++;
822       return c;
823
824     case ',':
825       if (comma_terminates && paren_depth == 0)
826         return 0;
827       lexptr++;
828       return c;
829
830     case '.':
831       /* Might be a floating point number.  */
832       if (lexptr[1] >= '0' && lexptr[1] <= '9')
833         break;                  /* Falls into number code.  */
834       else
835       {
836          lexptr++;
837          return DOT;
838       }
839
840 /* These are character tokens that appear as-is in the YACC grammar */
841     case '+':
842     case '-':
843     case '*':
844     case '/':
845     case '^':
846     case '<':
847     case '>':
848     case '[':
849     case ']':
850     case '=':
851     case '{':
852     case '}':
853     case '#':
854     case '@':
855     case '~':
856     case '&':
857       lexptr++;
858       return c;
859
860     case '\'' :
861     case '"':
862       quote = c;
863       for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
864         if (c == '\\')
865           {
866             c = tokstart[++namelen];
867             if (c >= '0' && c <= '9')
868               {
869                 c = tokstart[++namelen];
870                 if (c >= '0' && c <= '9')
871                   c = tokstart[++namelen];
872               }
873           }
874       if(c != quote)
875          error (_("Unterminated string or character constant."));
876       yylval.sval.ptr = tokstart + 1;
877       yylval.sval.length = namelen - 1;
878       lexptr += namelen + 1;
879
880       if(namelen == 2)          /* Single character */
881       {
882            yylval.ulval = tokstart[1];
883            return CHAR;
884       }
885       else
886          return STRING;
887     }
888
889   /* Is it a number?  */
890   /* Note:  We have already dealt with the case of the token '.'.
891      See case '.' above.  */
892   if ((c >= '0' && c <= '9'))
893     {
894       /* It's a number.  */
895       int got_dot = 0, got_e = 0;
896       const char *p = tokstart;
897       int toktype;
898
899       for (++p ;; ++p)
900         {
901           if (!got_e && (*p == 'e' || *p == 'E'))
902             got_dot = got_e = 1;
903           else if (!got_dot && *p == '.')
904             got_dot = 1;
905           else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
906                    && (*p == '-' || *p == '+'))
907             /* This is the sign of the exponent, not the end of the
908                number.  */
909             continue;
910           else if ((*p < '0' || *p > '9') &&
911                    (*p < 'A' || *p > 'F') &&
912                    (*p != 'H'))  /* Modula-2 hexadecimal number */
913             break;
914         }
915         toktype = parse_number (p - tokstart);
916         if (toktype == ERROR)
917           {
918             char *err_copy = (char *) alloca (p - tokstart + 1);
919
920             memcpy (err_copy, tokstart, p - tokstart);
921             err_copy[p - tokstart] = 0;
922             error (_("Invalid number \"%s\"."), err_copy);
923           }
924         lexptr = p;
925         return toktype;
926     }
927
928   if (!(c == '_' || c == '$'
929         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
930     /* We must have come across a bad character (e.g. ';').  */
931     error (_("Invalid character '%c' in expression."), c);
932
933   /* It's a name.  See how long it is.  */
934   namelen = 0;
935   for (c = tokstart[namelen];
936        (c == '_' || c == '$' || (c >= '0' && c <= '9')
937         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
938        c = tokstart[++namelen])
939     ;
940
941   /* The token "if" terminates the expression and is NOT
942      removed from the input stream.  */
943   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
944     {
945       return 0;
946     }
947
948   lexptr += namelen;
949
950   /*  Lookup special keywords */
951   for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
952      if (namelen == strlen (keytab[i].keyw)
953          && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
954            return keytab[i].token;
955
956   yylval.sval.ptr = tokstart;
957   yylval.sval.length = namelen;
958
959   if (*tokstart == '$')
960     {
961       write_dollar_variable (pstate, yylval.sval);
962       return INTERNAL_VAR;
963     }
964
965   /* Use token-type BLOCKNAME for symbols that happen to be defined as
966      functions.  If this is not so, then ...
967      Use token-type TYPENAME for symbols that happen to be defined
968      currently as names of types; NAME for other symbols.
969      The caller is not constrained to care about the distinction.  */
970  {
971
972
973     char *tmp = copy_name (yylval.sval);
974     struct symbol *sym;
975
976     if (lookup_symtab (tmp))
977       return BLOCKNAME;
978     sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0).symbol;
979     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
980       return BLOCKNAME;
981     if (lookup_typename (parse_language (pstate), parse_gdbarch (pstate),
982                          copy_name (yylval.sval),
983                          expression_context_block, 1))
984       return TYPENAME;
985
986     if(sym)
987     {
988       switch(SYMBOL_CLASS (sym))
989        {
990        case LOC_STATIC:
991        case LOC_REGISTER:
992        case LOC_ARG:
993        case LOC_REF_ARG:
994        case LOC_REGPARM_ADDR:
995        case LOC_LOCAL:
996        case LOC_CONST:
997        case LOC_CONST_BYTES:
998        case LOC_OPTIMIZED_OUT:
999        case LOC_COMPUTED:
1000           return NAME;
1001
1002        case LOC_TYPEDEF:
1003           return TYPENAME;
1004
1005        case LOC_BLOCK:
1006           return BLOCKNAME;
1007
1008        case LOC_UNDEF:
1009           error (_("internal:  Undefined class in m2lex()"));
1010
1011        case LOC_LABEL:
1012        case LOC_UNRESOLVED:
1013           error (_("internal:  Unforseen case in m2lex()"));
1014
1015        default:
1016           error (_("unhandled token in m2lex()"));
1017           break;
1018        }
1019     }
1020     else
1021     {
1022        /* Built-in BOOLEAN type.  This is sort of a hack.  */
1023        if (strncmp (tokstart, "TRUE", 4) == 0)
1024        {
1025           yylval.ulval = 1;
1026           return M2_TRUE;
1027        }
1028        else if (strncmp (tokstart, "FALSE", 5) == 0)
1029        {
1030           yylval.ulval = 0;
1031           return M2_FALSE;
1032        }
1033     }
1034
1035     /* Must be another type of name...  */
1036     return NAME;
1037  }
1038 }
1039
1040 int
1041 m2_parse (struct parser_state *par_state)
1042 {
1043   /* Setting up the parser state.  */
1044   scoped_restore pstate_restore = make_scoped_restore (&pstate);
1045   gdb_assert (par_state != NULL);
1046   pstate = par_state;
1047
1048   return yyparse ();
1049 }
1050
1051 void
1052 yyerror (const char *msg)
1053 {
1054   if (prev_lexptr)
1055     lexptr = prev_lexptr;
1056
1057   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
1058 }