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
61 /* #define YYDEBUG 1 */
65 /* Although the yacc "value" of an expression is not used,
66 since the result is stored in the structure being created,
67 other node types do have values. */
72 unsigned LONGEST ulval;
81 enum exp_opcode opcode;
82 struct internalvar *ivar;
88 %type <voidval> exp exp1 type_exp start variable
89 %type <tval> type typebase
90 %type <tvec> nonempty_typelist
91 /* %type <bval> block */
93 /* Fancy type parsing. */
94 %type <voidval> func_mod direct_abs_decl abs_decl
96 %type <lval> array_mod
98 %token <lval> INT CHAR
102 /* Both NAME and TYPENAME tokens represent symbols in the input,
103 and both convey their data as strings.
104 But a TYPENAME is a string that happens to be defined as a typedef
105 or builtin type name (such as int or char)
106 and a NAME is any other symbol.
107 Contexts where this distinction is not important can use the
108 nonterminal "name", which matches either NAME or TYPENAME. */
111 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
112 %token <tsym> TYPENAME
114 %type <ssym> name_not_typename
115 %type <tsym> typename
117 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
118 but which would parse as a valid number in the current input radix.
119 E.g. "c" when input_radix==16. Depending on the parse, it will be
120 turned into a name or into a number. NAME_OR_UINT ditto. */
122 %token <ssym> NAME_OR_INT NAME_OR_UINT
124 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
127 /* Special type cases, put in to allow the parser to distinguish different
129 %token SIGNED LONG SHORT INT_KEYWORD
131 %token <lval> LAST REGNAME
133 %token <ivar> VARIABLE
135 %token <opcode> ASSIGN_MODIFY
142 %right '=' ASSIGN_MODIFY
150 %left '<' '>' LEQ GEQ
155 %right UNARY INCREMENT DECREMENT
156 %right ARROW '.' '[' '('
157 %token <ssym> BLOCKNAME
168 { write_exp_elt_opcode(OP_TYPE);
169 write_exp_elt_type($1);
170 write_exp_elt_opcode(OP_TYPE);}
173 /* Expressions, including the comma operator. */
176 { write_exp_elt_opcode (BINOP_COMMA); }
179 /* Expressions, not including the comma operator. */
180 exp : '*' exp %prec UNARY
181 { write_exp_elt_opcode (UNOP_IND); }
183 exp : '&' exp %prec UNARY
184 { write_exp_elt_opcode (UNOP_ADDR); }
186 exp : '-' exp %prec UNARY
187 { write_exp_elt_opcode (UNOP_NEG); }
190 exp : '!' exp %prec UNARY
191 { write_exp_elt_opcode (UNOP_ZEROP); }
194 exp : '~' exp %prec UNARY
195 { write_exp_elt_opcode (UNOP_LOGNOT); }
198 exp : INCREMENT exp %prec UNARY
199 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
202 exp : DECREMENT exp %prec UNARY
203 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
206 exp : exp INCREMENT %prec UNARY
207 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
210 exp : exp DECREMENT %prec UNARY
211 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
214 exp : SIZEOF exp %prec UNARY
215 { write_exp_elt_opcode (UNOP_SIZEOF); }
219 { write_exp_elt_opcode (STRUCTOP_PTR);
220 write_exp_string ($3);
221 write_exp_elt_opcode (STRUCTOP_PTR); }
224 exp : exp ARROW '*' exp
225 { write_exp_elt_opcode (STRUCTOP_MPTR); }
229 { write_exp_elt_opcode (STRUCTOP_STRUCT);
230 write_exp_string ($3);
231 write_exp_elt_opcode (STRUCTOP_STRUCT); }
234 exp : exp '.' '*' exp
235 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
238 exp : exp '[' exp1 ']'
239 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
243 /* This is to save the value of arglist_len
244 being accumulated by an outer function call. */
245 { start_arglist (); }
246 arglist ')' %prec ARROW
247 { write_exp_elt_opcode (OP_FUNCALL);
248 write_exp_elt_longcst ((LONGEST) end_arglist ());
249 write_exp_elt_opcode (OP_FUNCALL); }
259 arglist : arglist ',' exp %prec ABOVE_COMMA
263 exp : '{' type '}' exp %prec UNARY
264 { write_exp_elt_opcode (UNOP_MEMVAL);
265 write_exp_elt_type ($2);
266 write_exp_elt_opcode (UNOP_MEMVAL); }
269 exp : '(' type ')' exp %prec UNARY
270 { write_exp_elt_opcode (UNOP_CAST);
271 write_exp_elt_type ($2);
272 write_exp_elt_opcode (UNOP_CAST); }
279 /* Binary operators in order of decreasing precedence. */
282 { write_exp_elt_opcode (BINOP_REPEAT); }
286 { write_exp_elt_opcode (BINOP_MUL); }
290 { write_exp_elt_opcode (BINOP_DIV); }
294 { write_exp_elt_opcode (BINOP_REM); }
298 { write_exp_elt_opcode (BINOP_ADD); }
302 { write_exp_elt_opcode (BINOP_SUB); }
306 { write_exp_elt_opcode (BINOP_LSH); }
310 { write_exp_elt_opcode (BINOP_RSH); }
314 { write_exp_elt_opcode (BINOP_EQUAL); }
317 exp : exp NOTEQUAL exp
318 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
322 { write_exp_elt_opcode (BINOP_LEQ); }
326 { write_exp_elt_opcode (BINOP_GEQ); }
330 { write_exp_elt_opcode (BINOP_LESS); }
334 { write_exp_elt_opcode (BINOP_GTR); }
338 { write_exp_elt_opcode (BINOP_LOGAND); }
342 { write_exp_elt_opcode (BINOP_LOGXOR); }
346 { write_exp_elt_opcode (BINOP_LOGIOR); }
350 { write_exp_elt_opcode (BINOP_AND); }
354 { write_exp_elt_opcode (BINOP_OR); }
357 exp : exp '?' exp ':' exp %prec '?'
358 { write_exp_elt_opcode (TERNOP_COND); }
362 { write_exp_elt_opcode (BINOP_ASSIGN); }
365 exp : exp ASSIGN_MODIFY exp
366 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
367 write_exp_elt_opcode ($2);
368 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
372 { write_exp_elt_opcode (OP_LONG);
373 if ($1 == (int) $1 || $1 == (unsigned int) $1)
374 write_exp_elt_type (builtin_type_int);
376 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
377 write_exp_elt_longcst ((LONGEST) $1);
378 write_exp_elt_opcode (OP_LONG); }
383 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
384 write_exp_elt_opcode (OP_LONG);
385 if (val.lval == (int) val.lval ||
386 val.lval == (unsigned int) val.lval)
387 write_exp_elt_type (builtin_type_int);
389 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
390 write_exp_elt_longcst (val.lval);
391 write_exp_elt_opcode (OP_LONG); }
396 write_exp_elt_opcode (OP_LONG);
397 if ($1 == (unsigned int) $1)
398 write_exp_elt_type (builtin_type_unsigned_int);
400 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
401 write_exp_elt_longcst ((LONGEST) $1);
402 write_exp_elt_opcode (OP_LONG);
408 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
409 write_exp_elt_opcode (OP_LONG);
410 if (val.ulval == (unsigned int) val.ulval)
411 write_exp_elt_type (builtin_type_unsigned_int);
413 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
414 write_exp_elt_longcst ((LONGEST)val.ulval);
415 write_exp_elt_opcode (OP_LONG);
420 { write_exp_elt_opcode (OP_LONG);
421 write_exp_elt_type (builtin_type_char);
422 write_exp_elt_longcst ((LONGEST) $1);
423 write_exp_elt_opcode (OP_LONG); }
427 { write_exp_elt_opcode (OP_DOUBLE);
428 write_exp_elt_type (builtin_type_double);
429 write_exp_elt_dblcst ($1);
430 write_exp_elt_opcode (OP_DOUBLE); }
437 { write_exp_elt_opcode (OP_LAST);
438 write_exp_elt_longcst ((LONGEST) $1);
439 write_exp_elt_opcode (OP_LAST); }
443 { write_exp_elt_opcode (OP_REGISTER);
444 write_exp_elt_longcst ((LONGEST) $1);
445 write_exp_elt_opcode (OP_REGISTER); }
449 { write_exp_elt_opcode (OP_INTERNALVAR);
450 write_exp_elt_intern ($1);
451 write_exp_elt_opcode (OP_INTERNALVAR); }
454 exp : SIZEOF '(' type ')' %prec UNARY
455 { write_exp_elt_opcode (OP_LONG);
456 write_exp_elt_type (builtin_type_int);
457 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
458 write_exp_elt_opcode (OP_LONG); }
462 { write_exp_elt_opcode (OP_STRING);
463 write_exp_string ($1);
464 write_exp_elt_opcode (OP_STRING); }
469 { write_exp_elt_opcode (OP_THIS);
470 write_exp_elt_opcode (OP_THIS); }
478 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
482 lookup_symtab (copy_name ($1.stoken));
484 $$ = BLOCKVECTOR_BLOCK
485 (BLOCKVECTOR (tem), STATIC_BLOCK);
487 error ("No file or function \"%s\".",
488 copy_name ($1.stoken));
493 block : block COLONCOLON name
495 = lookup_symbol (copy_name ($3), $1,
496 VAR_NAMESPACE, 0, NULL);
497 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
498 error ("No function \"%s\" in specified context.",
500 $$ = SYMBOL_BLOCK_VALUE (tem); }
503 variable: block COLONCOLON name
504 { struct symbol *sym;
505 sym = lookup_symbol (copy_name ($3), $1,
506 VAR_NAMESPACE, 0, NULL);
508 error ("No symbol \"%s\" in specified context.",
511 write_exp_elt_opcode (OP_VAR_VALUE);
512 write_exp_elt_sym (sym);
513 write_exp_elt_opcode (OP_VAR_VALUE); }
516 variable: typebase COLONCOLON name
518 struct type *type = $1;
519 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
520 && TYPE_CODE (type) != TYPE_CODE_UNION)
521 error ("`%s' is not defined as an aggregate type.",
524 write_exp_elt_opcode (OP_SCOPE);
525 write_exp_elt_type (type);
526 write_exp_string ($3);
527 write_exp_elt_opcode (OP_SCOPE);
529 | typebase COLONCOLON '~' name
531 struct type *type = $1;
532 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
533 && TYPE_CODE (type) != TYPE_CODE_UNION)
534 error ("`%s' is not defined as an aggregate type.",
537 if (strcmp (type_name_no_tag (type), $4.ptr))
538 error ("invalid destructor `%s::~%s'",
539 type_name_no_tag (type), $4.ptr);
541 write_exp_elt_opcode (OP_SCOPE);
542 write_exp_elt_type (type);
543 write_exp_string ($4);
544 write_exp_elt_opcode (OP_SCOPE);
545 write_exp_elt_opcode (UNOP_LOGNOT);
549 char *name = copy_name ($2);
554 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
557 write_exp_elt_opcode (OP_VAR_VALUE);
558 write_exp_elt_sym (sym);
559 write_exp_elt_opcode (OP_VAR_VALUE);
562 for (i = 0; i < misc_function_count; i++)
563 if (!strcmp (misc_function_vector[i].name, name))
566 if (i < misc_function_count)
568 enum misc_function_type mft =
569 misc_function_vector[i].type;
571 write_exp_elt_opcode (OP_LONG);
572 write_exp_elt_type (builtin_type_int);
573 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
574 write_exp_elt_opcode (OP_LONG);
575 write_exp_elt_opcode (UNOP_MEMVAL);
576 if (mft == mf_data || mft == mf_bss)
577 write_exp_elt_type (builtin_type_int);
578 else if (mft == mf_text)
579 write_exp_elt_type (lookup_function_type (builtin_type_int));
581 write_exp_elt_type (builtin_type_char);
582 write_exp_elt_opcode (UNOP_MEMVAL);
586 && partial_symtab_list == 0)
587 error ("No symbol table is loaded. Use the \"file\" command.");
589 error ("No symbol \"%s\" in current context.", name);
593 variable: name_not_typename
594 { struct symbol *sym = $1.sym;
606 if (innermost_block == 0 ||
607 contained_in (block_found,
609 innermost_block = block_found;
616 case LOC_CONST_BYTES:
618 /* In this case the expression can
619 be evaluated regardless of what
620 frame we are in, so there is no
621 need to check for the
622 innermost_block. These cases are
623 listed so that gcc -Wall will
624 report types that may not have
629 write_exp_elt_opcode (OP_VAR_VALUE);
630 write_exp_elt_sym (sym);
631 write_exp_elt_opcode (OP_VAR_VALUE);
633 else if ($1.is_a_field_of_this)
635 /* C++: it hangs off of `this'. Must
636 not inadvertently convert from a method call
638 if (innermost_block == 0 ||
639 contained_in (block_found, innermost_block))
640 innermost_block = block_found;
641 write_exp_elt_opcode (OP_THIS);
642 write_exp_elt_opcode (OP_THIS);
643 write_exp_elt_opcode (STRUCTOP_PTR);
644 write_exp_string ($1.stoken);
645 write_exp_elt_opcode (STRUCTOP_PTR);
650 register char *arg = copy_name ($1.stoken);
652 /* FIXME, this search is linear! At least
653 optimize the strcmp with a 1-char cmp... */
654 for (i = 0; i < misc_function_count; i++)
655 if (!strcmp (misc_function_vector[i].name, arg))
658 if (i < misc_function_count)
660 enum misc_function_type mft =
661 misc_function_vector[i].type;
663 write_exp_elt_opcode (OP_LONG);
664 write_exp_elt_type (builtin_type_int);
665 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
666 write_exp_elt_opcode (OP_LONG);
667 write_exp_elt_opcode (UNOP_MEMVAL);
668 if (mft == mf_data || mft == mf_bss)
669 write_exp_elt_type (builtin_type_int);
670 else if (mft == mf_text)
671 write_exp_elt_type (lookup_function_type (builtin_type_int));
673 write_exp_elt_type (builtin_type_char);
674 write_exp_elt_opcode (UNOP_MEMVAL);
676 else if (symtab_list == 0
677 && partial_symtab_list == 0)
678 error ("No symbol table is loaded. Use the \"file\" command.");
680 error ("No symbol \"%s\" in current context.",
681 copy_name ($1.stoken));
690 /* This is where the interesting stuff happens. */
693 struct type *follow_type = $1;
702 follow_type = lookup_pointer_type (follow_type);
705 follow_type = lookup_reference_type (follow_type);
708 array_size = pop_type_int ();
709 if (array_size != -1)
710 follow_type = create_array_type (follow_type,
713 follow_type = lookup_pointer_type (follow_type);
716 follow_type = lookup_function_type (follow_type);
724 { push_type (tp_pointer); $$ = 0; }
726 { push_type (tp_pointer); $$ = $2; }
728 { push_type (tp_reference); $$ = 0; }
730 { push_type (tp_reference); $$ = $2; }
734 direct_abs_decl: '(' abs_decl ')'
736 | direct_abs_decl array_mod
739 push_type (tp_array);
744 push_type (tp_array);
747 | direct_abs_decl func_mod
748 { push_type (tp_function); }
750 { push_type (tp_function); }
764 | typebase COLONCOLON '*'
765 { $$ = lookup_member_type (builtin_type_int, $1); }
766 | type '(' typebase COLONCOLON '*' ')'
767 { $$ = lookup_member_type ($1, $3); }
768 | type '(' typebase COLONCOLON '*' ')' '(' ')'
769 { $$ = lookup_member_type
770 (lookup_function_type ($1), $3); }
771 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
772 { $$ = lookup_member_type
773 (lookup_function_type ($1), $3);
781 { $$ = builtin_type_int; }
783 { $$ = builtin_type_long; }
785 { $$ = builtin_type_short; }
787 { $$ = builtin_type_long; }
788 | UNSIGNED LONG INT_KEYWORD
789 { $$ = builtin_type_unsigned_long; }
791 { $$ = builtin_type_long_long; }
792 | LONG LONG INT_KEYWORD
793 { $$ = builtin_type_long_long; }
795 { $$ = builtin_type_unsigned_long_long; }
796 | UNSIGNED LONG LONG INT_KEYWORD
797 { $$ = builtin_type_unsigned_long_long; }
799 { $$ = builtin_type_short; }
800 | UNSIGNED SHORT INT_KEYWORD
801 { $$ = builtin_type_unsigned_short; }
803 { $$ = lookup_struct (copy_name ($2),
804 expression_context_block); }
806 { $$ = lookup_union (copy_name ($2),
807 expression_context_block); }
809 { $$ = lookup_enum (copy_name ($2),
810 expression_context_block); }
812 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
814 { $$ = builtin_type_unsigned_int; }
818 { $$ = builtin_type_int; }
824 $$.stoken.ptr = "int";
825 $$.stoken.length = 3;
826 $$.type = builtin_type_int;
830 $$.stoken.ptr = "long";
831 $$.stoken.length = 4;
832 $$.type = builtin_type_long;
836 $$.stoken.ptr = "short";
837 $$.stoken.length = 5;
838 $$.type = builtin_type_short;
844 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
845 $$[0] = (struct type *)0;
848 | nonempty_typelist ',' type
849 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
850 $$ = (struct type **)xrealloc ($1, len);
851 $$[$<ivec>$[0]] = $3;
855 name : NAME { $$ = $1.stoken; }
856 | BLOCKNAME { $$ = $1.stoken; }
857 | TYPENAME { $$ = $1.stoken; }
858 | NAME_OR_INT { $$ = $1.stoken; }
859 | NAME_OR_UINT { $$ = $1.stoken; }
862 name_not_typename : NAME
864 /* These would be useful if name_not_typename was useful, but it is just
865 a fake for "variable", so these cause reduce/reduce conflicts because
866 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
867 =exp) or just an exp. If name_not_typename was ever used in an lvalue
868 context where only a name could occur, this might be useful.
876 /* Take care of parsing a number (anything that starts with a digit).
877 Set yylval and return the token type; update lexptr.
878 LEN is the number of characters in it. */
880 /*** Needs some error checking for the float case ***/
883 parse_number (p, len, parsed_float, putithere)
889 register LONGEST n = 0;
890 register LONGEST prevn = 0;
893 register int base = input_radix;
896 extern double atof ();
900 /* It's a float since it contains a point or an exponent. */
901 putithere->dval = atof (p);
905 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
939 if (c >= 'A' && c <= 'Z')
941 if (c != 'l' && c != 'u')
943 if (c >= '0' && c <= '9')
947 if (base > 10 && c >= 'a' && c <= 'f')
948 n += i = c - 'a' + 10;
949 else if (len == 0 && c == 'l')
951 else if (len == 0 && c == 'u')
954 return ERROR; /* Char not a digit */
957 return ERROR; /* Invalid digit in this base */
958 if(!unsigned_p && (prevn >= n))
959 unsigned_p=1; /* Try something unsigned */
960 /* Don't do the range check if n==i and i==0, since that special
961 case will give an overflow error. */
962 if(RANGE_CHECK && n!=0)
964 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
965 range_error("Overflow on numeric constant.");
972 putithere->ulval = n;
986 enum exp_opcode opcode;
989 const static struct token tokentab3[] =
991 {">>=", ASSIGN_MODIFY, BINOP_RSH},
992 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
995 const static struct token tokentab2[] =
997 {"+=", ASSIGN_MODIFY, BINOP_ADD},
998 {"-=", ASSIGN_MODIFY, BINOP_SUB},
999 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1000 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1001 {"%=", ASSIGN_MODIFY, BINOP_REM},
1002 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1003 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1004 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1005 {"++", INCREMENT, BINOP_END},
1006 {"--", DECREMENT, BINOP_END},
1007 {"->", ARROW, BINOP_END},
1008 {"&&", AND, BINOP_END},
1009 {"||", OR, BINOP_END},
1010 {"::", COLONCOLON, BINOP_END},
1011 {"<<", LSH, BINOP_END},
1012 {">>", RSH, BINOP_END},
1013 {"==", EQUAL, BINOP_END},
1014 {"!=", NOTEQUAL, BINOP_END},
1015 {"<=", LEQ, BINOP_END},
1016 {">=", GEQ, BINOP_END}
1019 /* Read one token, getting characters through lexptr. */
1025 register int namelen;
1026 register unsigned i;
1027 register char *tokstart;
1032 /* See if it is a special token of length 3. */
1033 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1034 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1037 yylval.opcode = tokentab3[i].opcode;
1038 return tokentab3[i].token;
1041 /* See if it is a special token of length 2. */
1042 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1043 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1046 yylval.opcode = tokentab2[i].opcode;
1047 return tokentab2[i].token;
1050 switch (c = *tokstart)
1065 c = parse_escape (&lexptr);
1069 error ("Invalid character constant.");
1078 if (paren_depth == 0)
1085 if (comma_terminates && paren_depth == 0)
1091 /* Might be a floating point number. */
1092 if (lexptr[1] < '0' || lexptr[1] > '9')
1093 goto symbol; /* Nope, must be a symbol. */
1094 /* FALL THRU into number case. */
1107 /* It's a number. */
1108 int got_dot = 0, got_e = 0, toktype;
1109 register char *p = tokstart;
1110 int hex = input_radix > 10;
1112 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1117 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1125 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1126 got_dot = got_e = 1;
1127 else if (!hex && !got_dot && *p == '.')
1129 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1130 && (*p == '-' || *p == '+'))
1131 /* This is the sign of the exponent, not the end of the
1134 /* We will take any letters or digits. parse_number will
1135 complain if past the radix, or if L or U are not final. */
1136 else if ((*p < '0' || *p > '9')
1137 && ((*p < 'a' || *p > 'z')
1138 && (*p < 'A' || *p > 'Z')))
1141 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1142 if (toktype == ERROR)
1144 char *err_copy = (char *) alloca (p - tokstart + 1);
1146 bcopy (tokstart, err_copy, p - tokstart);
1147 err_copy[p - tokstart] = 0;
1148 error ("Invalid number \"%s\".", err_copy);
1179 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1182 c = tokstart[++namelen];
1183 if (c >= '0' && c <= '9')
1185 c = tokstart[++namelen];
1186 if (c >= '0' && c <= '9')
1187 c = tokstart[++namelen];
1190 yylval.sval.ptr = tokstart + 1;
1191 yylval.sval.length = namelen - 1;
1192 lexptr += namelen + 1;
1196 if (!(c == '_' || c == '$'
1197 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1198 /* We must have come across a bad character (e.g. ';'). */
1199 error ("Invalid character '%c' in expression.", c);
1201 /* It's a name. See how long it is. */
1203 for (c = tokstart[namelen];
1204 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1205 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1206 c = tokstart[++namelen])
1209 /* The token "if" terminates the expression and is NOT
1210 removed from the input stream. */
1211 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1218 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1219 and $$digits (equivalent to $<-digits> if you could type that).
1220 Make token type LAST, and put the number (the digits) in yylval. */
1222 if (*tokstart == '$')
1224 register int negate = 0;
1226 /* Double dollar means negate the number and add -1 as well.
1227 Thus $$ alone means -1. */
1228 if (namelen >= 2 && tokstart[1] == '$')
1235 /* Just dollars (one or two) */
1236 yylval.lval = - negate;
1239 /* Is the rest of the token digits? */
1240 for (; c < namelen; c++)
1241 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1245 yylval.lval = atoi (tokstart + 1 + negate);
1247 yylval.lval = - yylval.lval;
1252 /* Handle tokens that refer to machine registers:
1253 $ followed by a register name. */
1255 if (*tokstart == '$') {
1256 for (c = 0; c < NUM_REGS; c++)
1257 if (namelen - 1 == strlen (reg_names[c])
1258 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1263 for (c = 0; c < num_std_regs; c++)
1264 if (namelen - 1 == strlen (std_regs[c].name)
1265 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1267 yylval.lval = std_regs[c].regnum;
1271 /* Catch specific keywords. Should be done with a data structure. */
1275 if (!strncmp (tokstart, "unsigned", 8))
1279 if (!strncmp (tokstart, "struct", 6))
1281 if (!strncmp (tokstart, "signed", 6))
1283 if (!strncmp (tokstart, "sizeof", 6))
1287 if (!strncmp (tokstart, "union", 5))
1289 if (!strncmp (tokstart, "short", 5))
1293 if (!strncmp (tokstart, "enum", 4))
1295 if (!strncmp (tokstart, "long", 4))
1297 if (!strncmp (tokstart, "this", 4))
1299 static const char this_name[] =
1300 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1302 if (lookup_symbol (this_name, expression_context_block,
1303 VAR_NAMESPACE, 0, NULL))
1308 if (!strncmp (tokstart, "int", 3))
1315 yylval.sval.ptr = tokstart;
1316 yylval.sval.length = namelen;
1318 /* Any other names starting in $ are debugger internal variables. */
1320 if (*tokstart == '$')
1322 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1326 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1327 functions or symtabs. If this is not so, then ...
1328 Use token-type TYPENAME for symbols that happen to be defined
1329 currently as names of types; NAME for other symbols.
1330 The caller is not constrained to care about the distinction. */
1332 char *tmp = copy_name (yylval.sval);
1334 int is_a_field_of_this = 0;
1337 sym = lookup_symbol (tmp, expression_context_block,
1338 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1339 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1340 lookup_partial_symtab (tmp))
1342 yylval.ssym.sym = sym;
1343 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1346 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1348 yylval.tsym.type = SYMBOL_TYPE (sym);
1351 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1354 /* Input names that aren't symbols but ARE valid hex numbers,
1355 when the input radix permits them, can be names or numbers
1356 depending on the parse. Note we support radixes > 16 here. */
1358 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1359 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1361 YYSTYPE newlval; /* Its value is ignored. */
1362 hextype = parse_number (tokstart, namelen, 0, &newlval);
1365 yylval.ssym.sym = sym;
1366 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1369 if (hextype == UINT)
1371 yylval.ssym.sym = sym;
1372 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1373 return NAME_OR_UINT;
1377 /* Any other kind of symbol */
1378 yylval.ssym.sym = sym;
1379 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1388 error ("Invalid syntax in expression.");
1391 /* Table mapping opcodes into strings for printing operators
1392 and precedences of the operators. */
1394 const static struct op_print c_op_print_tab[] =
1396 {",", BINOP_COMMA, PREC_COMMA, 0},
1397 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1398 {"||", BINOP_OR, PREC_OR, 0},
1399 {"&&", BINOP_AND, PREC_AND, 0},
1400 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1401 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1402 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1403 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1404 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1405 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1406 {">=", BINOP_GEQ, PREC_ORDER, 0},
1407 {">", BINOP_GTR, PREC_ORDER, 0},
1408 {"<", BINOP_LESS, PREC_ORDER, 0},
1409 {">>", BINOP_RSH, PREC_SHIFT, 0},
1410 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1411 {"+", BINOP_ADD, PREC_ADD, 0},
1412 {"-", BINOP_SUB, PREC_ADD, 0},
1413 {"*", BINOP_MUL, PREC_MUL, 0},
1414 {"/", BINOP_DIV, PREC_MUL, 0},
1415 {"%", BINOP_REM, PREC_MUL, 0},
1416 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1417 {"-", UNOP_NEG, PREC_PREFIX, 0},
1418 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1419 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1420 {"*", UNOP_IND, PREC_PREFIX, 0},
1421 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1422 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1423 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1424 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1426 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1429 /* These variables point to the objects
1430 representing the predefined C data types. */
1432 struct type *builtin_type_void;
1433 struct type *builtin_type_char;
1434 struct type *builtin_type_short;
1435 struct type *builtin_type_int;
1436 struct type *builtin_type_long;
1437 struct type *builtin_type_long_long;
1438 struct type *builtin_type_unsigned_char;
1439 struct type *builtin_type_unsigned_short;
1440 struct type *builtin_type_unsigned_int;
1441 struct type *builtin_type_unsigned_long;
1442 struct type *builtin_type_unsigned_long_long;
1443 struct type *builtin_type_float;
1444 struct type *builtin_type_double;
1446 struct type **(c_builtin_types[]) =
1450 &builtin_type_short,
1452 &builtin_type_float,
1453 &builtin_type_double,
1455 &builtin_type_long_long,
1456 &builtin_type_unsigned_char,
1457 &builtin_type_unsigned_short,
1458 &builtin_type_unsigned_int,
1459 &builtin_type_unsigned_long,
1460 &builtin_type_unsigned_long_long,
1464 /* FIXME: Eventually do a separate defintion for C++. */
1466 struct language_defn c_language_defn = {
1467 "c", /* Language name */
1474 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1475 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1476 &builtin_type_double, /* longest floating point type */
1477 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1478 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1479 c_op_print_tab, /* expression operators for printing */
1484 _initialize_c_exp ()
1486 /* FIXME: The code below assumes that the sizes of the basic data
1487 types are the same on the host and target machines!!! */
1489 builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
1491 builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
1492 builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
1494 builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
1495 builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
1496 builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
1497 builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
1499 builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
1500 builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
1501 builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
1502 builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
1504 builtin_type_long_long =
1505 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1507 builtin_type_unsigned_long_long =
1508 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1509 1, "unsigned long long");
1511 add_language (&c_language_defn);
1512 set_language (language_c); /* Make C the default language */