1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a C expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result. */
37 #include "expression.h"
38 #include "parser-defs.h"
42 /* These MUST be included in any grammar file!!!!
43 Please choose unique names! */
44 #define yymaxdepth c_maxdepth
45 #define yyparse c_parse
47 #define yyerror c_error
50 #define yydebug c_debug
59 #define yyerrflag c_errflag
60 #define yynerrs c_nerrs
65 #define yystate c_state
73 __yy_bcopy PARAMS ((char *, char *, int));
76 yyparse PARAMS ((void));
79 yylex PARAMS ((void));
82 yyerror PARAMS ((char *));
84 /* #define YYDEBUG 1 */
88 /* Although the yacc "value" of an expression is not used,
89 since the result is stored in the structure being created,
90 other node types do have values. */
95 unsigned LONGEST ulval;
101 struct symtoken ssym;
104 enum exp_opcode opcode;
105 struct internalvar *ivar;
112 /* YYSTYPE gets defined by %union */
114 parse_number PARAMS ((char *, int, int, YYSTYPE *));
117 %type <voidval> exp exp1 type_exp start variable
118 %type <tval> type typebase
119 %type <tvec> nonempty_typelist
120 /* %type <bval> block */
122 /* Fancy type parsing. */
123 %type <voidval> func_mod direct_abs_decl abs_decl
125 %type <lval> array_mod
127 %token <lval> INT CHAR
131 /* Both NAME and TYPENAME tokens represent symbols in the input,
132 and both convey their data as strings.
133 But a TYPENAME is a string that happens to be defined as a typedef
134 or builtin type name (such as int or char)
135 and a NAME is any other symbol.
136 Contexts where this distinction is not important can use the
137 nonterminal "name", which matches either NAME or TYPENAME. */
140 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
141 %token <tsym> TYPENAME
143 %type <ssym> name_not_typename
144 %type <tsym> typename
146 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
147 but which would parse as a valid number in the current input radix.
148 E.g. "c" when input_radix==16. Depending on the parse, it will be
149 turned into a name or into a number. NAME_OR_UINT ditto. */
151 %token <ssym> NAME_OR_INT NAME_OR_UINT
153 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
157 /* Special type cases, put in to allow the parser to distinguish different
159 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD
161 %token <lval> LAST REGNAME
163 %token <ivar> VARIABLE
165 %token <opcode> ASSIGN_MODIFY
172 %right '=' ASSIGN_MODIFY
180 %left '<' '>' LEQ GEQ
185 %right UNARY INCREMENT DECREMENT
186 %right ARROW '.' '[' '('
187 %token <ssym> BLOCKNAME
198 { write_exp_elt_opcode(OP_TYPE);
199 write_exp_elt_type($1);
200 write_exp_elt_opcode(OP_TYPE);}
203 /* Expressions, including the comma operator. */
206 { write_exp_elt_opcode (BINOP_COMMA); }
209 /* Expressions, not including the comma operator. */
210 exp : '*' exp %prec UNARY
211 { write_exp_elt_opcode (UNOP_IND); }
213 exp : '&' exp %prec UNARY
214 { write_exp_elt_opcode (UNOP_ADDR); }
216 exp : '-' exp %prec UNARY
217 { write_exp_elt_opcode (UNOP_NEG); }
220 exp : '!' exp %prec UNARY
221 { write_exp_elt_opcode (UNOP_ZEROP); }
224 exp : '~' exp %prec UNARY
225 { write_exp_elt_opcode (UNOP_LOGNOT); }
228 exp : INCREMENT exp %prec UNARY
229 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
232 exp : DECREMENT exp %prec UNARY
233 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
236 exp : exp INCREMENT %prec UNARY
237 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
240 exp : exp DECREMENT %prec UNARY
241 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
244 exp : SIZEOF exp %prec UNARY
245 { write_exp_elt_opcode (UNOP_SIZEOF); }
249 { write_exp_elt_opcode (STRUCTOP_PTR);
250 write_exp_string ($3);
251 write_exp_elt_opcode (STRUCTOP_PTR); }
254 exp : exp ARROW '*' exp
255 { write_exp_elt_opcode (STRUCTOP_MPTR); }
259 { write_exp_elt_opcode (STRUCTOP_STRUCT);
260 write_exp_string ($3);
261 write_exp_elt_opcode (STRUCTOP_STRUCT); }
264 exp : exp '.' '*' exp
265 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
268 exp : exp '[' exp1 ']'
269 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
273 /* This is to save the value of arglist_len
274 being accumulated by an outer function call. */
275 { start_arglist (); }
276 arglist ')' %prec ARROW
277 { write_exp_elt_opcode (OP_FUNCALL);
278 write_exp_elt_longcst ((LONGEST) end_arglist ());
279 write_exp_elt_opcode (OP_FUNCALL); }
289 arglist : arglist ',' exp %prec ABOVE_COMMA
293 exp : '{' type '}' exp %prec UNARY
294 { write_exp_elt_opcode (UNOP_MEMVAL);
295 write_exp_elt_type ($2);
296 write_exp_elt_opcode (UNOP_MEMVAL); }
299 exp : '(' type ')' exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_CAST);
301 write_exp_elt_type ($2);
302 write_exp_elt_opcode (UNOP_CAST); }
309 /* Binary operators in order of decreasing precedence. */
312 { write_exp_elt_opcode (BINOP_REPEAT); }
316 { write_exp_elt_opcode (BINOP_MUL); }
320 { write_exp_elt_opcode (BINOP_DIV); }
324 { write_exp_elt_opcode (BINOP_REM); }
328 { write_exp_elt_opcode (BINOP_ADD); }
332 { write_exp_elt_opcode (BINOP_SUB); }
336 { write_exp_elt_opcode (BINOP_LSH); }
340 { write_exp_elt_opcode (BINOP_RSH); }
344 { write_exp_elt_opcode (BINOP_EQUAL); }
347 exp : exp NOTEQUAL exp
348 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
352 { write_exp_elt_opcode (BINOP_LEQ); }
356 { write_exp_elt_opcode (BINOP_GEQ); }
360 { write_exp_elt_opcode (BINOP_LESS); }
364 { write_exp_elt_opcode (BINOP_GTR); }
368 { write_exp_elt_opcode (BINOP_LOGAND); }
372 { write_exp_elt_opcode (BINOP_LOGXOR); }
376 { write_exp_elt_opcode (BINOP_LOGIOR); }
380 { write_exp_elt_opcode (BINOP_AND); }
384 { write_exp_elt_opcode (BINOP_OR); }
387 exp : exp '?' exp ':' exp %prec '?'
388 { write_exp_elt_opcode (TERNOP_COND); }
392 { write_exp_elt_opcode (BINOP_ASSIGN); }
395 exp : exp ASSIGN_MODIFY exp
396 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
397 write_exp_elt_opcode ($2);
398 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
402 { write_exp_elt_opcode (OP_LONG);
403 if ($1 == (int) $1 || $1 == (unsigned int) $1)
404 write_exp_elt_type (builtin_type_int);
406 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
407 write_exp_elt_longcst ((LONGEST) $1);
408 write_exp_elt_opcode (OP_LONG); }
413 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
414 write_exp_elt_opcode (OP_LONG);
415 if (val.lval == (int) val.lval ||
416 val.lval == (unsigned int) val.lval)
417 write_exp_elt_type (builtin_type_int);
419 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
420 write_exp_elt_longcst (val.lval);
421 write_exp_elt_opcode (OP_LONG); }
426 write_exp_elt_opcode (OP_LONG);
427 if ($1 == (unsigned int) $1)
428 write_exp_elt_type (builtin_type_unsigned_int);
430 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
431 write_exp_elt_longcst ((LONGEST) $1);
432 write_exp_elt_opcode (OP_LONG);
438 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
439 write_exp_elt_opcode (OP_LONG);
440 if (val.ulval == (unsigned int) val.ulval)
441 write_exp_elt_type (builtin_type_unsigned_int);
443 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
444 write_exp_elt_longcst ((LONGEST)val.ulval);
445 write_exp_elt_opcode (OP_LONG);
450 { write_exp_elt_opcode (OP_LONG);
451 write_exp_elt_type (builtin_type_char);
452 write_exp_elt_longcst ((LONGEST) $1);
453 write_exp_elt_opcode (OP_LONG); }
457 { write_exp_elt_opcode (OP_DOUBLE);
458 write_exp_elt_type (builtin_type_double);
459 write_exp_elt_dblcst ($1);
460 write_exp_elt_opcode (OP_DOUBLE); }
467 { write_exp_elt_opcode (OP_LAST);
468 write_exp_elt_longcst ((LONGEST) $1);
469 write_exp_elt_opcode (OP_LAST); }
473 { write_exp_elt_opcode (OP_REGISTER);
474 write_exp_elt_longcst ((LONGEST) $1);
475 write_exp_elt_opcode (OP_REGISTER); }
479 { write_exp_elt_opcode (OP_INTERNALVAR);
480 write_exp_elt_intern ($1);
481 write_exp_elt_opcode (OP_INTERNALVAR); }
484 exp : SIZEOF '(' type ')' %prec UNARY
485 { write_exp_elt_opcode (OP_LONG);
486 write_exp_elt_type (builtin_type_int);
487 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
488 write_exp_elt_opcode (OP_LONG); }
492 { write_exp_elt_opcode (OP_STRING);
493 write_exp_string ($1);
494 write_exp_elt_opcode (OP_STRING); }
499 { write_exp_elt_opcode (OP_THIS);
500 write_exp_elt_opcode (OP_THIS); }
508 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
512 lookup_symtab (copy_name ($1.stoken));
514 $$ = BLOCKVECTOR_BLOCK
515 (BLOCKVECTOR (tem), STATIC_BLOCK);
517 error ("No file or function \"%s\".",
518 copy_name ($1.stoken));
523 block : block COLONCOLON name
525 = lookup_symbol (copy_name ($3), $1,
526 VAR_NAMESPACE, 0, NULL);
527 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
528 error ("No function \"%s\" in specified context.",
530 $$ = SYMBOL_BLOCK_VALUE (tem); }
533 variable: block COLONCOLON name
534 { struct symbol *sym;
535 sym = lookup_symbol (copy_name ($3), $1,
536 VAR_NAMESPACE, 0, NULL);
538 error ("No symbol \"%s\" in specified context.",
541 write_exp_elt_opcode (OP_VAR_VALUE);
542 write_exp_elt_sym (sym);
543 write_exp_elt_opcode (OP_VAR_VALUE); }
546 variable: typebase COLONCOLON name
548 struct type *type = $1;
549 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
550 && TYPE_CODE (type) != TYPE_CODE_UNION)
551 error ("`%s' is not defined as an aggregate type.",
554 write_exp_elt_opcode (OP_SCOPE);
555 write_exp_elt_type (type);
556 write_exp_string ($3);
557 write_exp_elt_opcode (OP_SCOPE);
559 | typebase COLONCOLON '~' name
561 struct type *type = $1;
562 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
563 && TYPE_CODE (type) != TYPE_CODE_UNION)
564 error ("`%s' is not defined as an aggregate type.",
567 if (strcmp (type_name_no_tag (type), $4.ptr))
568 error ("invalid destructor `%s::~%s'",
569 type_name_no_tag (type), $4.ptr);
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string ($4);
574 write_exp_elt_opcode (OP_SCOPE);
575 write_exp_elt_opcode (UNOP_LOGNOT);
579 char *name = copy_name ($2);
581 struct minimal_symbol *msymbol;
584 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
587 write_exp_elt_opcode (OP_VAR_VALUE);
588 write_exp_elt_sym (sym);
589 write_exp_elt_opcode (OP_VAR_VALUE);
593 msymbol = lookup_minimal_symbol (name,
594 (struct objfile *) NULL);
597 write_exp_elt_opcode (OP_LONG);
598 write_exp_elt_type (builtin_type_int);
599 write_exp_elt_longcst ((LONGEST) msymbol -> address);
600 write_exp_elt_opcode (OP_LONG);
601 write_exp_elt_opcode (UNOP_MEMVAL);
602 if (msymbol -> type == mst_data ||
603 msymbol -> type == mst_bss)
604 write_exp_elt_type (builtin_type_int);
605 else if (msymbol -> type == mst_text)
606 write_exp_elt_type (lookup_function_type (builtin_type_int));
608 write_exp_elt_type (builtin_type_char);
609 write_exp_elt_opcode (UNOP_MEMVAL);
612 if (!have_full_symbols () && !have_partial_symbols ())
613 error ("No symbol table is loaded. Use the \"file\" command.");
615 error ("No symbol \"%s\" in current context.", name);
619 variable: name_not_typename
620 { struct symbol *sym = $1.sym;
624 switch (SYMBOL_CLASS (sym))
632 if (innermost_block == 0 ||
633 contained_in (block_found,
635 innermost_block = block_found;
642 case LOC_CONST_BYTES:
644 /* In this case the expression can
645 be evaluated regardless of what
646 frame we are in, so there is no
647 need to check for the
648 innermost_block. These cases are
649 listed so that gcc -Wall will
650 report types that may not have
655 write_exp_elt_opcode (OP_VAR_VALUE);
656 write_exp_elt_sym (sym);
657 write_exp_elt_opcode (OP_VAR_VALUE);
659 else if ($1.is_a_field_of_this)
661 /* C++: it hangs off of `this'. Must
662 not inadvertently convert from a method call
664 if (innermost_block == 0 ||
665 contained_in (block_found, innermost_block))
666 innermost_block = block_found;
667 write_exp_elt_opcode (OP_THIS);
668 write_exp_elt_opcode (OP_THIS);
669 write_exp_elt_opcode (STRUCTOP_PTR);
670 write_exp_string ($1.stoken);
671 write_exp_elt_opcode (STRUCTOP_PTR);
675 struct minimal_symbol *msymbol;
676 register char *arg = copy_name ($1.stoken);
678 msymbol = lookup_minimal_symbol (arg,
679 (struct objfile *) NULL);
682 write_exp_elt_opcode (OP_LONG);
683 write_exp_elt_type (builtin_type_int);
684 write_exp_elt_longcst ((LONGEST) msymbol -> address);
685 write_exp_elt_opcode (OP_LONG);
686 write_exp_elt_opcode (UNOP_MEMVAL);
687 if (msymbol -> type == mst_data ||
688 msymbol -> type == mst_bss)
689 write_exp_elt_type (builtin_type_int);
690 else if (msymbol -> type == mst_text)
691 write_exp_elt_type (lookup_function_type (builtin_type_int));
693 write_exp_elt_type (builtin_type_char);
694 write_exp_elt_opcode (UNOP_MEMVAL);
696 else if (!have_full_symbols () && !have_partial_symbols ())
697 error ("No symbol table is loaded. Use the \"file\" command.");
699 error ("No symbol \"%s\" in current context.",
700 copy_name ($1.stoken));
709 /* This is where the interesting stuff happens. */
712 struct type *follow_type = $1;
721 follow_type = lookup_pointer_type (follow_type);
724 follow_type = lookup_reference_type (follow_type);
727 array_size = pop_type_int ();
728 if (array_size != -1)
729 follow_type = create_array_type (follow_type,
732 follow_type = lookup_pointer_type (follow_type);
735 follow_type = lookup_function_type (follow_type);
743 { push_type (tp_pointer); $$ = 0; }
745 { push_type (tp_pointer); $$ = $2; }
747 { push_type (tp_reference); $$ = 0; }
749 { push_type (tp_reference); $$ = $2; }
753 direct_abs_decl: '(' abs_decl ')'
755 | direct_abs_decl array_mod
758 push_type (tp_array);
763 push_type (tp_array);
766 | direct_abs_decl func_mod
767 { push_type (tp_function); }
769 { push_type (tp_function); }
780 | '(' nonempty_typelist ')'
781 { free ($2); $$ = 0; }
785 | typebase COLONCOLON '*'
786 { $$ = lookup_member_type (builtin_type_int, $1); }
787 | type '(' typebase COLONCOLON '*' ')'
788 { $$ = lookup_member_type ($1, $3); }
789 | type '(' typebase COLONCOLON '*' ')' '(' ')'
790 { $$ = lookup_member_type
791 (lookup_function_type ($1), $3); }
792 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
793 { $$ = lookup_member_type
794 (lookup_function_type ($1), $3);
802 { $$ = builtin_type_int; }
804 { $$ = builtin_type_long; }
806 { $$ = builtin_type_short; }
808 { $$ = builtin_type_long; }
809 | UNSIGNED LONG INT_KEYWORD
810 { $$ = builtin_type_unsigned_long; }
812 { $$ = builtin_type_long_long; }
813 | LONG LONG INT_KEYWORD
814 { $$ = builtin_type_long_long; }
816 { $$ = builtin_type_unsigned_long_long; }
817 | UNSIGNED LONG LONG INT_KEYWORD
818 { $$ = builtin_type_unsigned_long_long; }
820 { $$ = builtin_type_short; }
821 | UNSIGNED SHORT INT_KEYWORD
822 { $$ = builtin_type_unsigned_short; }
824 { $$ = lookup_struct (copy_name ($2),
825 expression_context_block); }
827 { $$ = lookup_union (copy_name ($2),
828 expression_context_block); }
830 { $$ = lookup_enum (copy_name ($2),
831 expression_context_block); }
833 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
835 { $$ = builtin_type_unsigned_int; }
836 | SIGNED_KEYWORD typename
839 { $$ = builtin_type_int; }
840 | TEMPLATE name '<' type '>'
841 { $$ = lookup_template_type(copy_name($2), $4,
842 expression_context_block);
849 $$.stoken.ptr = "int";
850 $$.stoken.length = 3;
851 $$.type = builtin_type_int;
855 $$.stoken.ptr = "long";
856 $$.stoken.length = 4;
857 $$.type = builtin_type_long;
861 $$.stoken.ptr = "short";
862 $$.stoken.length = 5;
863 $$.type = builtin_type_short;
869 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
870 $$[0] = (struct type *)0;
873 | nonempty_typelist ',' type
874 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
875 $$ = (struct type **)xrealloc ((char *) $1, len);
876 $$[$<ivec>$[0]] = $3;
880 name : NAME { $$ = $1.stoken; }
881 | BLOCKNAME { $$ = $1.stoken; }
882 | TYPENAME { $$ = $1.stoken; }
883 | NAME_OR_INT { $$ = $1.stoken; }
884 | NAME_OR_UINT { $$ = $1.stoken; }
887 name_not_typename : NAME
889 /* These would be useful if name_not_typename was useful, but it is just
890 a fake for "variable", so these cause reduce/reduce conflicts because
891 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
892 =exp) or just an exp. If name_not_typename was ever used in an lvalue
893 context where only a name could occur, this might be useful.
901 /* Take care of parsing a number (anything that starts with a digit).
902 Set yylval and return the token type; update lexptr.
903 LEN is the number of characters in it. */
905 /*** Needs some error checking for the float case ***/
908 parse_number (p, len, parsed_float, putithere)
914 register LONGEST n = 0;
915 register LONGEST prevn = 0;
918 register int base = input_radix;
923 /* It's a float since it contains a point or an exponent. */
924 putithere->dval = atof (p);
928 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
962 if (c >= 'A' && c <= 'Z')
964 if (c != 'l' && c != 'u')
966 if (c >= '0' && c <= '9')
970 if (base > 10 && c >= 'a' && c <= 'f')
971 n += i = c - 'a' + 10;
972 else if (len == 0 && c == 'l')
974 else if (len == 0 && c == 'u')
977 return ERROR; /* Char not a digit */
980 return ERROR; /* Invalid digit in this base */
981 /* Portably test for overflow (only works for nonzero values, so make
982 a second check for zero). */
983 if((prevn >= n) && n != 0)
984 unsigned_p=1; /* Try something unsigned */
985 /* If range checking enabled, portably test for unsigned overflow. */
986 if(RANGE_CHECK && n!=0)
988 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
989 range_error("Overflow on numeric constant.");
996 putithere->ulval = n;
1001 putithere->lval = n;
1010 enum exp_opcode opcode;
1013 const static struct token tokentab3[] =
1015 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1016 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1019 const static struct token tokentab2[] =
1021 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1022 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1023 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1024 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1025 {"%=", ASSIGN_MODIFY, BINOP_REM},
1026 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1027 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1028 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1029 {"++", INCREMENT, BINOP_END},
1030 {"--", DECREMENT, BINOP_END},
1031 {"->", ARROW, BINOP_END},
1032 {"&&", ANDAND, BINOP_END},
1033 {"||", OROR, BINOP_END},
1034 {"::", COLONCOLON, BINOP_END},
1035 {"<<", LSH, BINOP_END},
1036 {">>", RSH, BINOP_END},
1037 {"==", EQUAL, BINOP_END},
1038 {"!=", NOTEQUAL, BINOP_END},
1039 {"<=", LEQ, BINOP_END},
1040 {">=", GEQ, BINOP_END}
1043 /* Read one token, getting characters through lexptr. */
1049 register int namelen;
1050 register unsigned i;
1051 register char *tokstart;
1056 /* See if it is a special token of length 3. */
1057 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1058 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1061 yylval.opcode = tokentab3[i].opcode;
1062 return tokentab3[i].token;
1065 /* See if it is a special token of length 2. */
1066 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1067 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1070 yylval.opcode = tokentab2[i].opcode;
1071 return tokentab2[i].token;
1074 switch (c = *tokstart)
1089 c = parse_escape (&lexptr);
1093 error ("Invalid character constant.");
1102 if (paren_depth == 0)
1109 if (comma_terminates && paren_depth == 0)
1115 /* Might be a floating point number. */
1116 if (lexptr[1] < '0' || lexptr[1] > '9')
1117 goto symbol; /* Nope, must be a symbol. */
1118 /* FALL THRU into number case. */
1131 /* It's a number. */
1132 int got_dot = 0, got_e = 0, toktype;
1133 register char *p = tokstart;
1134 int hex = input_radix > 10;
1136 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1141 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1149 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1150 got_dot = got_e = 1;
1151 else if (!hex && !got_dot && *p == '.')
1153 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1154 && (*p == '-' || *p == '+'))
1155 /* This is the sign of the exponent, not the end of the
1158 /* We will take any letters or digits. parse_number will
1159 complain if past the radix, or if L or U are not final. */
1160 else if ((*p < '0' || *p > '9')
1161 && ((*p < 'a' || *p > 'z')
1162 && (*p < 'A' || *p > 'Z')))
1165 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1166 if (toktype == ERROR)
1168 char *err_copy = (char *) alloca (p - tokstart + 1);
1170 bcopy (tokstart, err_copy, p - tokstart);
1171 err_copy[p - tokstart] = 0;
1172 error ("Invalid number \"%s\".", err_copy);
1203 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1206 c = tokstart[++namelen];
1207 if (c >= '0' && c <= '9')
1209 c = tokstart[++namelen];
1210 if (c >= '0' && c <= '9')
1211 c = tokstart[++namelen];
1214 yylval.sval.ptr = tokstart + 1;
1215 yylval.sval.length = namelen - 1;
1216 lexptr += namelen + 1;
1220 if (!(c == '_' || c == '$'
1221 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1222 /* We must have come across a bad character (e.g. ';'). */
1223 error ("Invalid character '%c' in expression.", c);
1225 /* It's a name. See how long it is. */
1227 for (c = tokstart[namelen];
1228 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1229 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1230 c = tokstart[++namelen])
1233 /* The token "if" terminates the expression and is NOT
1234 removed from the input stream. */
1235 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1242 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1243 and $$digits (equivalent to $<-digits> if you could type that).
1244 Make token type LAST, and put the number (the digits) in yylval. */
1246 if (*tokstart == '$')
1248 register int negate = 0;
1250 /* Double dollar means negate the number and add -1 as well.
1251 Thus $$ alone means -1. */
1252 if (namelen >= 2 && tokstart[1] == '$')
1259 /* Just dollars (one or two) */
1260 yylval.lval = - negate;
1263 /* Is the rest of the token digits? */
1264 for (; c < namelen; c++)
1265 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1269 yylval.lval = atoi (tokstart + 1 + negate);
1271 yylval.lval = - yylval.lval;
1276 /* Handle tokens that refer to machine registers:
1277 $ followed by a register name. */
1279 if (*tokstart == '$') {
1280 for (c = 0; c < NUM_REGS; c++)
1281 if (namelen - 1 == strlen (reg_names[c])
1282 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1287 for (c = 0; c < num_std_regs; c++)
1288 if (namelen - 1 == strlen (std_regs[c].name)
1289 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1291 yylval.lval = std_regs[c].regnum;
1295 /* Catch specific keywords. Should be done with a data structure. */
1299 if (!strncmp (tokstart, "unsigned", 8))
1301 if (current_language->la_language == language_cplus
1302 && !strncmp (tokstart, "template", 8))
1306 if (!strncmp (tokstart, "struct", 6))
1308 if (!strncmp (tokstart, "signed", 6))
1309 return SIGNED_KEYWORD;
1310 if (!strncmp (tokstart, "sizeof", 6))
1314 if (!strncmp (tokstart, "union", 5))
1316 if (!strncmp (tokstart, "short", 5))
1320 if (!strncmp (tokstart, "enum", 4))
1322 if (!strncmp (tokstart, "long", 4))
1324 if (current_language->la_language == language_cplus
1325 && !strncmp (tokstart, "this", 4))
1327 static const char this_name[] =
1328 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1330 if (lookup_symbol (this_name, expression_context_block,
1331 VAR_NAMESPACE, 0, NULL))
1336 if (!strncmp (tokstart, "int", 3))
1343 yylval.sval.ptr = tokstart;
1344 yylval.sval.length = namelen;
1346 /* Any other names starting in $ are debugger internal variables. */
1348 if (*tokstart == '$')
1350 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1354 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1355 functions or symtabs. If this is not so, then ...
1356 Use token-type TYPENAME for symbols that happen to be defined
1357 currently as names of types; NAME for other symbols.
1358 The caller is not constrained to care about the distinction. */
1360 char *tmp = copy_name (yylval.sval);
1362 int is_a_field_of_this = 0;
1365 sym = lookup_symbol (tmp, expression_context_block,
1367 current_language->la_language == language_cplus
1368 ? &is_a_field_of_this : NULL,
1370 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1371 lookup_partial_symtab (tmp))
1373 yylval.ssym.sym = sym;
1374 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1377 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1379 yylval.tsym.type = SYMBOL_TYPE (sym);
1382 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1385 /* Input names that aren't symbols but ARE valid hex numbers,
1386 when the input radix permits them, can be names or numbers
1387 depending on the parse. Note we support radixes > 16 here. */
1389 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1390 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1392 YYSTYPE newlval; /* Its value is ignored. */
1393 hextype = parse_number (tokstart, namelen, 0, &newlval);
1396 yylval.ssym.sym = sym;
1397 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1400 if (hextype == UINT)
1402 yylval.ssym.sym = sym;
1403 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1404 return NAME_OR_UINT;
1408 /* Any other kind of symbol */
1409 yylval.ssym.sym = sym;
1410 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1419 error (msg ? msg : "Invalid syntax in expression.");
1422 /* Table mapping opcodes into strings for printing operators
1423 and precedences of the operators. */
1425 const static struct op_print c_op_print_tab[] =
1427 {",", BINOP_COMMA, PREC_COMMA, 0},
1428 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1429 {"||", BINOP_OR, PREC_OR, 0},
1430 {"&&", BINOP_AND, PREC_AND, 0},
1431 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1432 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1433 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1434 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1435 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1436 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1437 {">=", BINOP_GEQ, PREC_ORDER, 0},
1438 {">", BINOP_GTR, PREC_ORDER, 0},
1439 {"<", BINOP_LESS, PREC_ORDER, 0},
1440 {">>", BINOP_RSH, PREC_SHIFT, 0},
1441 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1442 {"+", BINOP_ADD, PREC_ADD, 0},
1443 {"-", BINOP_SUB, PREC_ADD, 0},
1444 {"*", BINOP_MUL, PREC_MUL, 0},
1445 {"/", BINOP_DIV, PREC_MUL, 0},
1446 {"%", BINOP_REM, PREC_MUL, 0},
1447 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1448 {"-", UNOP_NEG, PREC_PREFIX, 0},
1449 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1450 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1451 {"*", UNOP_IND, PREC_PREFIX, 0},
1452 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1453 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1454 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1455 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1457 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1460 /* These variables point to the objects
1461 representing the predefined C data types. */
1463 struct type *builtin_type_void;
1464 struct type *builtin_type_char;
1465 struct type *builtin_type_short;
1466 struct type *builtin_type_int;
1467 struct type *builtin_type_long;
1468 struct type *builtin_type_long_long;
1469 struct type *builtin_type_unsigned_char;
1470 struct type *builtin_type_unsigned_short;
1471 struct type *builtin_type_unsigned_int;
1472 struct type *builtin_type_unsigned_long;
1473 struct type *builtin_type_unsigned_long_long;
1474 struct type *builtin_type_float;
1475 struct type *builtin_type_double;
1476 struct type *builtin_type_long_double;
1477 struct type *builtin_type_complex;
1478 struct type *builtin_type_double_complex;
1480 struct type ** const (c_builtin_types[]) =
1484 &builtin_type_short,
1486 &builtin_type_float,
1487 &builtin_type_double,
1489 &builtin_type_long_long,
1490 &builtin_type_unsigned_char,
1491 &builtin_type_unsigned_short,
1492 &builtin_type_unsigned_int,
1493 &builtin_type_unsigned_long,
1494 &builtin_type_unsigned_long_long,
1495 &builtin_type_long_double,
1496 &builtin_type_complex,
1497 &builtin_type_double_complex,
1501 const struct language_defn c_language_defn = {
1502 "c", /* Language name */
1509 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1510 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1511 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1512 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1513 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1514 c_op_print_tab, /* expression operators for printing */
1518 const struct language_defn cplus_language_defn = {
1519 "c++", /* Language name */
1526 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1527 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1528 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1529 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1530 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1531 c_op_print_tab, /* expression operators for printing */
1536 _initialize_c_exp ()
1539 init_type (TYPE_CODE_VOID, 1,
1541 "void", (struct objfile *) NULL);
1543 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1545 "char", (struct objfile *) NULL);
1546 builtin_type_unsigned_char =
1547 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1549 "unsigned char", (struct objfile *) NULL);
1550 builtin_type_short =
1551 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1553 "short", (struct objfile *) NULL);
1554 builtin_type_unsigned_short =
1555 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1557 "unsigned short", (struct objfile *) NULL);
1559 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1561 "int", (struct objfile *) NULL);
1562 builtin_type_unsigned_int =
1563 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1565 "unsigned int", (struct objfile *) NULL);
1567 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1569 "long", (struct objfile *) NULL);
1570 builtin_type_unsigned_long =
1571 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1573 "unsigned long", (struct objfile *) NULL);
1574 builtin_type_long_long =
1575 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1577 "long long", (struct objfile *) NULL);
1578 builtin_type_unsigned_long_long =
1579 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1581 "unsigned long long", (struct objfile *) NULL);
1582 builtin_type_float =
1583 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1585 "float", (struct objfile *) NULL);
1586 builtin_type_double =
1587 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1589 "double", (struct objfile *) NULL);
1590 builtin_type_long_double =
1591 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1593 "long double", (struct objfile *) NULL);
1594 builtin_type_complex =
1595 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1597 "complex", (struct objfile *) NULL);
1598 builtin_type_double_complex =
1599 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1601 "double complex", (struct objfile *) NULL);
1603 add_language (&c_language_defn);
1604 add_language (&cplus_language_defn);