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 yyparse c_parse
46 #define yyerror c_error
49 #define yydebug c_debug
58 #define yyerrflag c_errflag
59 #define yynerrs c_nerrs
63 static int parse_number ();
66 /* #define YYDEBUG 1 */
70 /* Although the yacc "value" of an expression is not used,
71 since the result is stored in the structure being created,
72 other node types do have values. */
77 unsigned LONGEST ulval;
86 enum exp_opcode opcode;
87 struct internalvar *ivar;
93 %type <voidval> exp exp1 type_exp start variable
94 %type <tval> type typebase
95 %type <tvec> nonempty_typelist
96 /* %type <bval> block */
98 /* Fancy type parsing. */
99 %type <voidval> func_mod direct_abs_decl abs_decl
101 %type <lval> array_mod
103 %token <lval> INT CHAR
107 /* Both NAME and TYPENAME tokens represent symbols in the input,
108 and both convey their data as strings.
109 But a TYPENAME is a string that happens to be defined as a typedef
110 or builtin type name (such as int or char)
111 and a NAME is any other symbol.
112 Contexts where this distinction is not important can use the
113 nonterminal "name", which matches either NAME or TYPENAME. */
116 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
117 %token <tsym> TYPENAME
119 %type <ssym> name_not_typename
120 %type <tsym> typename
122 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
123 but which would parse as a valid number in the current input radix.
124 E.g. "c" when input_radix==16. Depending on the parse, it will be
125 turned into a name or into a number. NAME_OR_UINT ditto. */
127 %token <ssym> NAME_OR_INT NAME_OR_UINT
129 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
132 /* Special type cases, put in to allow the parser to distinguish different
134 %token SIGNED LONG SHORT INT_KEYWORD
136 %token <lval> LAST REGNAME
138 %token <ivar> VARIABLE
140 %token <opcode> ASSIGN_MODIFY
147 %right '=' ASSIGN_MODIFY
155 %left '<' '>' LEQ GEQ
160 %right UNARY INCREMENT DECREMENT
161 %right ARROW '.' '[' '('
162 %token <ssym> BLOCKNAME
173 { write_exp_elt_opcode(OP_TYPE);
174 write_exp_elt_type($1);
175 write_exp_elt_opcode(OP_TYPE);}
178 /* Expressions, including the comma operator. */
181 { write_exp_elt_opcode (BINOP_COMMA); }
184 /* Expressions, not including the comma operator. */
185 exp : '*' exp %prec UNARY
186 { write_exp_elt_opcode (UNOP_IND); }
188 exp : '&' exp %prec UNARY
189 { write_exp_elt_opcode (UNOP_ADDR); }
191 exp : '-' exp %prec UNARY
192 { write_exp_elt_opcode (UNOP_NEG); }
195 exp : '!' exp %prec UNARY
196 { write_exp_elt_opcode (UNOP_ZEROP); }
199 exp : '~' exp %prec UNARY
200 { write_exp_elt_opcode (UNOP_LOGNOT); }
203 exp : INCREMENT exp %prec UNARY
204 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
207 exp : DECREMENT exp %prec UNARY
208 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
211 exp : exp INCREMENT %prec UNARY
212 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
215 exp : exp DECREMENT %prec UNARY
216 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
219 exp : SIZEOF exp %prec UNARY
220 { write_exp_elt_opcode (UNOP_SIZEOF); }
224 { write_exp_elt_opcode (STRUCTOP_PTR);
225 write_exp_string ($3);
226 write_exp_elt_opcode (STRUCTOP_PTR); }
229 exp : exp ARROW '*' exp
230 { write_exp_elt_opcode (STRUCTOP_MPTR); }
234 { write_exp_elt_opcode (STRUCTOP_STRUCT);
235 write_exp_string ($3);
236 write_exp_elt_opcode (STRUCTOP_STRUCT); }
239 exp : exp '.' '*' exp
240 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
243 exp : exp '[' exp1 ']'
244 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
248 /* This is to save the value of arglist_len
249 being accumulated by an outer function call. */
250 { start_arglist (); }
251 arglist ')' %prec ARROW
252 { write_exp_elt_opcode (OP_FUNCALL);
253 write_exp_elt_longcst ((LONGEST) end_arglist ());
254 write_exp_elt_opcode (OP_FUNCALL); }
264 arglist : arglist ',' exp %prec ABOVE_COMMA
268 exp : '{' type '}' exp %prec UNARY
269 { write_exp_elt_opcode (UNOP_MEMVAL);
270 write_exp_elt_type ($2);
271 write_exp_elt_opcode (UNOP_MEMVAL); }
274 exp : '(' type ')' exp %prec UNARY
275 { write_exp_elt_opcode (UNOP_CAST);
276 write_exp_elt_type ($2);
277 write_exp_elt_opcode (UNOP_CAST); }
284 /* Binary operators in order of decreasing precedence. */
287 { write_exp_elt_opcode (BINOP_REPEAT); }
291 { write_exp_elt_opcode (BINOP_MUL); }
295 { write_exp_elt_opcode (BINOP_DIV); }
299 { write_exp_elt_opcode (BINOP_REM); }
303 { write_exp_elt_opcode (BINOP_ADD); }
307 { write_exp_elt_opcode (BINOP_SUB); }
311 { write_exp_elt_opcode (BINOP_LSH); }
315 { write_exp_elt_opcode (BINOP_RSH); }
319 { write_exp_elt_opcode (BINOP_EQUAL); }
322 exp : exp NOTEQUAL exp
323 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
327 { write_exp_elt_opcode (BINOP_LEQ); }
331 { write_exp_elt_opcode (BINOP_GEQ); }
335 { write_exp_elt_opcode (BINOP_LESS); }
339 { write_exp_elt_opcode (BINOP_GTR); }
343 { write_exp_elt_opcode (BINOP_LOGAND); }
347 { write_exp_elt_opcode (BINOP_LOGXOR); }
351 { write_exp_elt_opcode (BINOP_LOGIOR); }
355 { write_exp_elt_opcode (BINOP_AND); }
359 { write_exp_elt_opcode (BINOP_OR); }
362 exp : exp '?' exp ':' exp %prec '?'
363 { write_exp_elt_opcode (TERNOP_COND); }
367 { write_exp_elt_opcode (BINOP_ASSIGN); }
370 exp : exp ASSIGN_MODIFY exp
371 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
372 write_exp_elt_opcode ($2);
373 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
377 { write_exp_elt_opcode (OP_LONG);
378 if ($1 == (int) $1 || $1 == (unsigned int) $1)
379 write_exp_elt_type (builtin_type_int);
381 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
382 write_exp_elt_longcst ((LONGEST) $1);
383 write_exp_elt_opcode (OP_LONG); }
388 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
389 write_exp_elt_opcode (OP_LONG);
390 if (val.lval == (int) val.lval ||
391 val.lval == (unsigned int) val.lval)
392 write_exp_elt_type (builtin_type_int);
394 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
395 write_exp_elt_longcst (val.lval);
396 write_exp_elt_opcode (OP_LONG); }
401 write_exp_elt_opcode (OP_LONG);
402 if ($1 == (unsigned int) $1)
403 write_exp_elt_type (builtin_type_unsigned_int);
405 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
406 write_exp_elt_longcst ((LONGEST) $1);
407 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.ulval == (unsigned int) val.ulval)
416 write_exp_elt_type (builtin_type_unsigned_int);
418 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
419 write_exp_elt_longcst ((LONGEST)val.ulval);
420 write_exp_elt_opcode (OP_LONG);
425 { write_exp_elt_opcode (OP_LONG);
426 write_exp_elt_type (builtin_type_char);
427 write_exp_elt_longcst ((LONGEST) $1);
428 write_exp_elt_opcode (OP_LONG); }
432 { write_exp_elt_opcode (OP_DOUBLE);
433 write_exp_elt_type (builtin_type_double);
434 write_exp_elt_dblcst ($1);
435 write_exp_elt_opcode (OP_DOUBLE); }
442 { write_exp_elt_opcode (OP_LAST);
443 write_exp_elt_longcst ((LONGEST) $1);
444 write_exp_elt_opcode (OP_LAST); }
448 { write_exp_elt_opcode (OP_REGISTER);
449 write_exp_elt_longcst ((LONGEST) $1);
450 write_exp_elt_opcode (OP_REGISTER); }
454 { write_exp_elt_opcode (OP_INTERNALVAR);
455 write_exp_elt_intern ($1);
456 write_exp_elt_opcode (OP_INTERNALVAR); }
459 exp : SIZEOF '(' type ')' %prec UNARY
460 { write_exp_elt_opcode (OP_LONG);
461 write_exp_elt_type (builtin_type_int);
462 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
463 write_exp_elt_opcode (OP_LONG); }
467 { write_exp_elt_opcode (OP_STRING);
468 write_exp_string ($1);
469 write_exp_elt_opcode (OP_STRING); }
474 { write_exp_elt_opcode (OP_THIS);
475 write_exp_elt_opcode (OP_THIS); }
483 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
487 lookup_symtab (copy_name ($1.stoken));
489 $$ = BLOCKVECTOR_BLOCK
490 (BLOCKVECTOR (tem), STATIC_BLOCK);
492 error ("No file or function \"%s\".",
493 copy_name ($1.stoken));
498 block : block COLONCOLON name
500 = lookup_symbol (copy_name ($3), $1,
501 VAR_NAMESPACE, 0, NULL);
502 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
503 error ("No function \"%s\" in specified context.",
505 $$ = SYMBOL_BLOCK_VALUE (tem); }
508 variable: block COLONCOLON name
509 { struct symbol *sym;
510 sym = lookup_symbol (copy_name ($3), $1,
511 VAR_NAMESPACE, 0, NULL);
513 error ("No symbol \"%s\" in specified context.",
516 write_exp_elt_opcode (OP_VAR_VALUE);
517 write_exp_elt_sym (sym);
518 write_exp_elt_opcode (OP_VAR_VALUE); }
521 variable: typebase COLONCOLON name
523 struct type *type = $1;
524 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
525 && TYPE_CODE (type) != TYPE_CODE_UNION)
526 error ("`%s' is not defined as an aggregate type.",
529 write_exp_elt_opcode (OP_SCOPE);
530 write_exp_elt_type (type);
531 write_exp_string ($3);
532 write_exp_elt_opcode (OP_SCOPE);
534 | typebase COLONCOLON '~' name
536 struct type *type = $1;
537 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
538 && TYPE_CODE (type) != TYPE_CODE_UNION)
539 error ("`%s' is not defined as an aggregate type.",
542 if (strcmp (type_name_no_tag (type), $4.ptr))
543 error ("invalid destructor `%s::~%s'",
544 type_name_no_tag (type), $4.ptr);
546 write_exp_elt_opcode (OP_SCOPE);
547 write_exp_elt_type (type);
548 write_exp_string ($4);
549 write_exp_elt_opcode (OP_SCOPE);
550 write_exp_elt_opcode (UNOP_LOGNOT);
554 char *name = copy_name ($2);
559 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
562 write_exp_elt_opcode (OP_VAR_VALUE);
563 write_exp_elt_sym (sym);
564 write_exp_elt_opcode (OP_VAR_VALUE);
567 for (i = 0; i < misc_function_count; i++)
568 if (!strcmp (misc_function_vector[i].name, name))
571 if (i < misc_function_count)
573 enum misc_function_type mft =
574 misc_function_vector[i].type;
576 write_exp_elt_opcode (OP_LONG);
577 write_exp_elt_type (builtin_type_int);
578 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
579 write_exp_elt_opcode (OP_LONG);
580 write_exp_elt_opcode (UNOP_MEMVAL);
581 if (mft == mf_data || mft == mf_bss)
582 write_exp_elt_type (builtin_type_int);
583 else if (mft == mf_text)
584 write_exp_elt_type (lookup_function_type (builtin_type_int));
586 write_exp_elt_type (builtin_type_char);
587 write_exp_elt_opcode (UNOP_MEMVAL);
591 && partial_symtab_list == 0)
592 error ("No symbol table is loaded. Use the \"file\" command.");
594 error ("No symbol \"%s\" in current context.", name);
598 variable: name_not_typename
599 { struct symbol *sym = $1.sym;
611 if (innermost_block == 0 ||
612 contained_in (block_found,
614 innermost_block = block_found;
621 case LOC_CONST_BYTES:
623 /* In this case the expression can
624 be evaluated regardless of what
625 frame we are in, so there is no
626 need to check for the
627 innermost_block. These cases are
628 listed so that gcc -Wall will
629 report types that may not have
634 write_exp_elt_opcode (OP_VAR_VALUE);
635 write_exp_elt_sym (sym);
636 write_exp_elt_opcode (OP_VAR_VALUE);
638 else if ($1.is_a_field_of_this)
640 /* C++: it hangs off of `this'. Must
641 not inadvertently convert from a method call
643 if (innermost_block == 0 ||
644 contained_in (block_found, innermost_block))
645 innermost_block = block_found;
646 write_exp_elt_opcode (OP_THIS);
647 write_exp_elt_opcode (OP_THIS);
648 write_exp_elt_opcode (STRUCTOP_PTR);
649 write_exp_string ($1.stoken);
650 write_exp_elt_opcode (STRUCTOP_PTR);
655 register char *arg = copy_name ($1.stoken);
657 /* FIXME, this search is linear! At least
658 optimize the strcmp with a 1-char cmp... */
659 for (i = 0; i < misc_function_count; i++)
660 if (!strcmp (misc_function_vector[i].name, arg))
663 if (i < misc_function_count)
665 enum misc_function_type mft =
666 misc_function_vector[i].type;
668 write_exp_elt_opcode (OP_LONG);
669 write_exp_elt_type (builtin_type_int);
670 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
671 write_exp_elt_opcode (OP_LONG);
672 write_exp_elt_opcode (UNOP_MEMVAL);
673 if (mft == mf_data || mft == mf_bss)
674 write_exp_elt_type (builtin_type_int);
675 else if (mft == mf_text)
676 write_exp_elt_type (lookup_function_type (builtin_type_int));
678 write_exp_elt_type (builtin_type_char);
679 write_exp_elt_opcode (UNOP_MEMVAL);
681 else if (symtab_list == 0
682 && partial_symtab_list == 0)
683 error ("No symbol table is loaded. Use the \"file\" command.");
685 error ("No symbol \"%s\" in current context.",
686 copy_name ($1.stoken));
695 /* This is where the interesting stuff happens. */
698 struct type *follow_type = $1;
707 follow_type = lookup_pointer_type (follow_type);
710 follow_type = lookup_reference_type (follow_type);
713 array_size = pop_type_int ();
714 if (array_size != -1)
715 follow_type = create_array_type (follow_type,
718 follow_type = lookup_pointer_type (follow_type);
721 follow_type = lookup_function_type (follow_type);
729 { push_type (tp_pointer); $$ = 0; }
731 { push_type (tp_pointer); $$ = $2; }
733 { push_type (tp_reference); $$ = 0; }
735 { push_type (tp_reference); $$ = $2; }
739 direct_abs_decl: '(' abs_decl ')'
741 | direct_abs_decl array_mod
744 push_type (tp_array);
749 push_type (tp_array);
752 | direct_abs_decl func_mod
753 { push_type (tp_function); }
755 { push_type (tp_function); }
769 | typebase COLONCOLON '*'
770 { $$ = lookup_member_type (builtin_type_int, $1); }
771 | type '(' typebase COLONCOLON '*' ')'
772 { $$ = lookup_member_type ($1, $3); }
773 | type '(' typebase COLONCOLON '*' ')' '(' ')'
774 { $$ = lookup_member_type
775 (lookup_function_type ($1), $3); }
776 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
777 { $$ = lookup_member_type
778 (lookup_function_type ($1), $3);
786 { $$ = builtin_type_int; }
788 { $$ = builtin_type_long; }
790 { $$ = builtin_type_short; }
792 { $$ = builtin_type_long; }
793 | UNSIGNED LONG INT_KEYWORD
794 { $$ = builtin_type_unsigned_long; }
796 { $$ = builtin_type_long_long; }
797 | LONG LONG INT_KEYWORD
798 { $$ = builtin_type_long_long; }
800 { $$ = builtin_type_unsigned_long_long; }
801 | UNSIGNED LONG LONG INT_KEYWORD
802 { $$ = builtin_type_unsigned_long_long; }
804 { $$ = builtin_type_short; }
805 | UNSIGNED SHORT INT_KEYWORD
806 { $$ = builtin_type_unsigned_short; }
808 { $$ = lookup_struct (copy_name ($2),
809 expression_context_block); }
811 { $$ = lookup_union (copy_name ($2),
812 expression_context_block); }
814 { $$ = lookup_enum (copy_name ($2),
815 expression_context_block); }
817 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
819 { $$ = builtin_type_unsigned_int; }
823 { $$ = builtin_type_int; }
829 $$.stoken.ptr = "int";
830 $$.stoken.length = 3;
831 $$.type = builtin_type_int;
835 $$.stoken.ptr = "long";
836 $$.stoken.length = 4;
837 $$.type = builtin_type_long;
841 $$.stoken.ptr = "short";
842 $$.stoken.length = 5;
843 $$.type = builtin_type_short;
849 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
850 $$[0] = (struct type *)0;
853 | nonempty_typelist ',' type
854 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
855 $$ = (struct type **)xrealloc ($1, len);
856 $$[$<ivec>$[0]] = $3;
860 name : NAME { $$ = $1.stoken; }
861 | BLOCKNAME { $$ = $1.stoken; }
862 | TYPENAME { $$ = $1.stoken; }
863 | NAME_OR_INT { $$ = $1.stoken; }
864 | NAME_OR_UINT { $$ = $1.stoken; }
867 name_not_typename : NAME
869 /* These would be useful if name_not_typename was useful, but it is just
870 a fake for "variable", so these cause reduce/reduce conflicts because
871 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
872 =exp) or just an exp. If name_not_typename was ever used in an lvalue
873 context where only a name could occur, this might be useful.
881 /* Take care of parsing a number (anything that starts with a digit).
882 Set yylval and return the token type; update lexptr.
883 LEN is the number of characters in it. */
885 /*** Needs some error checking for the float case ***/
888 parse_number (p, len, parsed_float, putithere)
894 register LONGEST n = 0;
895 register LONGEST prevn = 0;
898 register int base = input_radix;
901 extern double atof ();
905 /* It's a float since it contains a point or an exponent. */
906 putithere->dval = atof (p);
910 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
944 if (c >= 'A' && c <= 'Z')
946 if (c != 'l' && c != 'u')
948 if (c >= '0' && c <= '9')
952 if (base > 10 && c >= 'a' && c <= 'f')
953 n += i = c - 'a' + 10;
954 else if (len == 0 && c == 'l')
956 else if (len == 0 && c == 'u')
959 return ERROR; /* Char not a digit */
962 return ERROR; /* Invalid digit in this base */
963 if(!unsigned_p && (prevn >= n))
964 unsigned_p=1; /* Try something unsigned */
965 /* Don't do the range check if n==i and i==0, since that special
966 case will give an overflow error. */
967 if(RANGE_CHECK && n!=0)
969 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
970 range_error("Overflow on numeric constant.");
977 putithere->ulval = n;
991 enum exp_opcode opcode;
994 const static struct token tokentab3[] =
996 {">>=", ASSIGN_MODIFY, BINOP_RSH},
997 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1000 const static struct token tokentab2[] =
1002 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1003 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1004 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1005 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1006 {"%=", ASSIGN_MODIFY, BINOP_REM},
1007 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1008 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1009 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1010 {"++", INCREMENT, BINOP_END},
1011 {"--", DECREMENT, BINOP_END},
1012 {"->", ARROW, BINOP_END},
1013 {"&&", AND, BINOP_END},
1014 {"||", OR, BINOP_END},
1015 {"::", COLONCOLON, BINOP_END},
1016 {"<<", LSH, BINOP_END},
1017 {">>", RSH, BINOP_END},
1018 {"==", EQUAL, BINOP_END},
1019 {"!=", NOTEQUAL, BINOP_END},
1020 {"<=", LEQ, BINOP_END},
1021 {">=", GEQ, BINOP_END}
1024 /* Read one token, getting characters through lexptr. */
1030 register int namelen;
1031 register unsigned i;
1032 register char *tokstart;
1037 /* See if it is a special token of length 3. */
1038 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1039 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1042 yylval.opcode = tokentab3[i].opcode;
1043 return tokentab3[i].token;
1046 /* See if it is a special token of length 2. */
1047 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1048 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1051 yylval.opcode = tokentab2[i].opcode;
1052 return tokentab2[i].token;
1055 switch (c = *tokstart)
1070 c = parse_escape (&lexptr);
1074 error ("Invalid character constant.");
1083 if (paren_depth == 0)
1090 if (comma_terminates && paren_depth == 0)
1096 /* Might be a floating point number. */
1097 if (lexptr[1] < '0' || lexptr[1] > '9')
1098 goto symbol; /* Nope, must be a symbol. */
1099 /* FALL THRU into number case. */
1112 /* It's a number. */
1113 int got_dot = 0, got_e = 0, toktype;
1114 register char *p = tokstart;
1115 int hex = input_radix > 10;
1117 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1122 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1130 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1131 got_dot = got_e = 1;
1132 else if (!hex && !got_dot && *p == '.')
1134 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1135 && (*p == '-' || *p == '+'))
1136 /* This is the sign of the exponent, not the end of the
1139 /* We will take any letters or digits. parse_number will
1140 complain if past the radix, or if L or U are not final. */
1141 else if ((*p < '0' || *p > '9')
1142 && ((*p < 'a' || *p > 'z')
1143 && (*p < 'A' || *p > 'Z')))
1146 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1147 if (toktype == ERROR)
1149 char *err_copy = (char *) alloca (p - tokstart + 1);
1151 bcopy (tokstart, err_copy, p - tokstart);
1152 err_copy[p - tokstart] = 0;
1153 error ("Invalid number \"%s\".", err_copy);
1184 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1187 c = tokstart[++namelen];
1188 if (c >= '0' && c <= '9')
1190 c = tokstart[++namelen];
1191 if (c >= '0' && c <= '9')
1192 c = tokstart[++namelen];
1195 yylval.sval.ptr = tokstart + 1;
1196 yylval.sval.length = namelen - 1;
1197 lexptr += namelen + 1;
1201 if (!(c == '_' || c == '$'
1202 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1203 /* We must have come across a bad character (e.g. ';'). */
1204 error ("Invalid character '%c' in expression.", c);
1206 /* It's a name. See how long it is. */
1208 for (c = tokstart[namelen];
1209 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1210 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1211 c = tokstart[++namelen])
1214 /* The token "if" terminates the expression and is NOT
1215 removed from the input stream. */
1216 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1223 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1224 and $$digits (equivalent to $<-digits> if you could type that).
1225 Make token type LAST, and put the number (the digits) in yylval. */
1227 if (*tokstart == '$')
1229 register int negate = 0;
1231 /* Double dollar means negate the number and add -1 as well.
1232 Thus $$ alone means -1. */
1233 if (namelen >= 2 && tokstart[1] == '$')
1240 /* Just dollars (one or two) */
1241 yylval.lval = - negate;
1244 /* Is the rest of the token digits? */
1245 for (; c < namelen; c++)
1246 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1250 yylval.lval = atoi (tokstart + 1 + negate);
1252 yylval.lval = - yylval.lval;
1257 /* Handle tokens that refer to machine registers:
1258 $ followed by a register name. */
1260 if (*tokstart == '$') {
1261 for (c = 0; c < NUM_REGS; c++)
1262 if (namelen - 1 == strlen (reg_names[c])
1263 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1268 for (c = 0; c < num_std_regs; c++)
1269 if (namelen - 1 == strlen (std_regs[c].name)
1270 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1272 yylval.lval = std_regs[c].regnum;
1276 /* Catch specific keywords. Should be done with a data structure. */
1280 if (!strncmp (tokstart, "unsigned", 8))
1284 if (!strncmp (tokstart, "struct", 6))
1286 if (!strncmp (tokstart, "signed", 6))
1288 if (!strncmp (tokstart, "sizeof", 6))
1292 if (!strncmp (tokstart, "union", 5))
1294 if (!strncmp (tokstart, "short", 5))
1298 if (!strncmp (tokstart, "enum", 4))
1300 if (!strncmp (tokstart, "long", 4))
1302 if (!strncmp (tokstart, "this", 4))
1304 static const char this_name[] =
1305 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1307 if (lookup_symbol (this_name, expression_context_block,
1308 VAR_NAMESPACE, 0, NULL))
1313 if (!strncmp (tokstart, "int", 3))
1320 yylval.sval.ptr = tokstart;
1321 yylval.sval.length = namelen;
1323 /* Any other names starting in $ are debugger internal variables. */
1325 if (*tokstart == '$')
1327 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1331 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1332 functions or symtabs. If this is not so, then ...
1333 Use token-type TYPENAME for symbols that happen to be defined
1334 currently as names of types; NAME for other symbols.
1335 The caller is not constrained to care about the distinction. */
1337 char *tmp = copy_name (yylval.sval);
1339 int is_a_field_of_this = 0;
1342 sym = lookup_symbol (tmp, expression_context_block,
1343 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1344 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1345 lookup_partial_symtab (tmp))
1347 yylval.ssym.sym = sym;
1348 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1351 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1353 yylval.tsym.type = SYMBOL_TYPE (sym);
1356 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1359 /* Input names that aren't symbols but ARE valid hex numbers,
1360 when the input radix permits them, can be names or numbers
1361 depending on the parse. Note we support radixes > 16 here. */
1363 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1364 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1366 YYSTYPE newlval; /* Its value is ignored. */
1367 hextype = parse_number (tokstart, namelen, 0, &newlval);
1370 yylval.ssym.sym = sym;
1371 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1374 if (hextype == UINT)
1376 yylval.ssym.sym = sym;
1377 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1378 return NAME_OR_UINT;
1382 /* Any other kind of symbol */
1383 yylval.ssym.sym = sym;
1384 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1393 error ("Invalid syntax in expression.");
1396 /* Table mapping opcodes into strings for printing operators
1397 and precedences of the operators. */
1399 const static struct op_print c_op_print_tab[] =
1401 {",", BINOP_COMMA, PREC_COMMA, 0},
1402 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1403 {"||", BINOP_OR, PREC_OR, 0},
1404 {"&&", BINOP_AND, PREC_AND, 0},
1405 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1406 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1407 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1408 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1409 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1410 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1411 {">=", BINOP_GEQ, PREC_ORDER, 0},
1412 {">", BINOP_GTR, PREC_ORDER, 0},
1413 {"<", BINOP_LESS, PREC_ORDER, 0},
1414 {">>", BINOP_RSH, PREC_SHIFT, 0},
1415 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1416 {"+", BINOP_ADD, PREC_ADD, 0},
1417 {"-", BINOP_SUB, PREC_ADD, 0},
1418 {"*", BINOP_MUL, PREC_MUL, 0},
1419 {"/", BINOP_DIV, PREC_MUL, 0},
1420 {"%", BINOP_REM, PREC_MUL, 0},
1421 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1422 {"-", UNOP_NEG, PREC_PREFIX, 0},
1423 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1424 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1425 {"*", UNOP_IND, PREC_PREFIX, 0},
1426 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1427 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1428 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1429 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1431 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1434 /* These variables point to the objects
1435 representing the predefined C data types. */
1437 struct type *builtin_type_void;
1438 struct type *builtin_type_char;
1439 struct type *builtin_type_short;
1440 struct type *builtin_type_int;
1441 struct type *builtin_type_long;
1442 struct type *builtin_type_long_long;
1443 struct type *builtin_type_unsigned_char;
1444 struct type *builtin_type_unsigned_short;
1445 struct type *builtin_type_unsigned_int;
1446 struct type *builtin_type_unsigned_long;
1447 struct type *builtin_type_unsigned_long_long;
1448 struct type *builtin_type_float;
1449 struct type *builtin_type_double;
1451 struct type ** const (c_builtin_types[]) =
1455 &builtin_type_short,
1457 &builtin_type_float,
1458 &builtin_type_double,
1460 &builtin_type_long_long,
1461 &builtin_type_unsigned_char,
1462 &builtin_type_unsigned_short,
1463 &builtin_type_unsigned_int,
1464 &builtin_type_unsigned_long,
1465 &builtin_type_unsigned_long_long,
1469 /* FIXME: Eventually do a separate defintion for C++. */
1471 const struct language_defn c_language_defn = {
1472 "c", /* Language name */
1479 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1480 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1481 &builtin_type_double, /* longest floating point type */
1482 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1483 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1484 c_op_print_tab, /* expression operators for printing */
1489 _initialize_c_exp ()
1491 /* FIXME: The code below assumes that the sizes of the basic data
1492 types are the same on the host and target machines!!! */
1494 builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
1496 builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
1497 builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
1499 builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
1500 builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
1501 builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
1502 builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
1504 builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
1505 builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
1506 builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
1507 builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
1509 builtin_type_long_long =
1510 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1512 builtin_type_unsigned_long_long =
1513 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1514 1, "unsigned long long");
1516 add_language (&c_language_defn);
1517 set_language (language_c); /* Make C the default language */