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 static int parse_number ();
64 /* #define YYDEBUG 1 */
68 /* Although the yacc "value" of an expression is not used,
69 since the result is stored in the structure being created,
70 other node types do have values. */
75 unsigned LONGEST ulval;
84 enum exp_opcode opcode;
85 struct internalvar *ivar;
91 %type <voidval> exp exp1 type_exp start variable
92 %type <tval> type typebase
93 %type <tvec> nonempty_typelist
94 /* %type <bval> block */
96 /* Fancy type parsing. */
97 %type <voidval> func_mod direct_abs_decl abs_decl
99 %type <lval> array_mod
101 %token <lval> INT CHAR
105 /* Both NAME and TYPENAME tokens represent symbols in the input,
106 and both convey their data as strings.
107 But a TYPENAME is a string that happens to be defined as a typedef
108 or builtin type name (such as int or char)
109 and a NAME is any other symbol.
110 Contexts where this distinction is not important can use the
111 nonterminal "name", which matches either NAME or TYPENAME. */
114 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
115 %token <tsym> TYPENAME
117 %type <ssym> name_not_typename
118 %type <tsym> typename
120 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
121 but which would parse as a valid number in the current input radix.
122 E.g. "c" when input_radix==16. Depending on the parse, it will be
123 turned into a name or into a number. NAME_OR_UINT ditto. */
125 %token <ssym> NAME_OR_INT NAME_OR_UINT
127 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
130 /* Special type cases, put in to allow the parser to distinguish different
132 %token SIGNED LONG SHORT INT_KEYWORD
134 %token <lval> LAST REGNAME
136 %token <ivar> VARIABLE
138 %token <opcode> ASSIGN_MODIFY
145 %right '=' ASSIGN_MODIFY
153 %left '<' '>' LEQ GEQ
158 %right UNARY INCREMENT DECREMENT
159 %right ARROW '.' '[' '('
160 %token <ssym> BLOCKNAME
171 { write_exp_elt_opcode(OP_TYPE);
172 write_exp_elt_type($1);
173 write_exp_elt_opcode(OP_TYPE);}
176 /* Expressions, including the comma operator. */
179 { write_exp_elt_opcode (BINOP_COMMA); }
182 /* Expressions, not including the comma operator. */
183 exp : '*' exp %prec UNARY
184 { write_exp_elt_opcode (UNOP_IND); }
186 exp : '&' exp %prec UNARY
187 { write_exp_elt_opcode (UNOP_ADDR); }
189 exp : '-' exp %prec UNARY
190 { write_exp_elt_opcode (UNOP_NEG); }
193 exp : '!' exp %prec UNARY
194 { write_exp_elt_opcode (UNOP_ZEROP); }
197 exp : '~' exp %prec UNARY
198 { write_exp_elt_opcode (UNOP_LOGNOT); }
201 exp : INCREMENT exp %prec UNARY
202 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
205 exp : DECREMENT exp %prec UNARY
206 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
209 exp : exp INCREMENT %prec UNARY
210 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
213 exp : exp DECREMENT %prec UNARY
214 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
217 exp : SIZEOF exp %prec UNARY
218 { write_exp_elt_opcode (UNOP_SIZEOF); }
222 { write_exp_elt_opcode (STRUCTOP_PTR);
223 write_exp_string ($3);
224 write_exp_elt_opcode (STRUCTOP_PTR); }
227 exp : exp ARROW '*' exp
228 { write_exp_elt_opcode (STRUCTOP_MPTR); }
232 { write_exp_elt_opcode (STRUCTOP_STRUCT);
233 write_exp_string ($3);
234 write_exp_elt_opcode (STRUCTOP_STRUCT); }
237 exp : exp '.' '*' exp
238 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
241 exp : exp '[' exp1 ']'
242 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
246 /* This is to save the value of arglist_len
247 being accumulated by an outer function call. */
248 { start_arglist (); }
249 arglist ')' %prec ARROW
250 { write_exp_elt_opcode (OP_FUNCALL);
251 write_exp_elt_longcst ((LONGEST) end_arglist ());
252 write_exp_elt_opcode (OP_FUNCALL); }
262 arglist : arglist ',' exp %prec ABOVE_COMMA
266 exp : '{' type '}' exp %prec UNARY
267 { write_exp_elt_opcode (UNOP_MEMVAL);
268 write_exp_elt_type ($2);
269 write_exp_elt_opcode (UNOP_MEMVAL); }
272 exp : '(' type ')' exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_CAST);
274 write_exp_elt_type ($2);
275 write_exp_elt_opcode (UNOP_CAST); }
282 /* Binary operators in order of decreasing precedence. */
285 { write_exp_elt_opcode (BINOP_REPEAT); }
289 { write_exp_elt_opcode (BINOP_MUL); }
293 { write_exp_elt_opcode (BINOP_DIV); }
297 { write_exp_elt_opcode (BINOP_REM); }
301 { write_exp_elt_opcode (BINOP_ADD); }
305 { write_exp_elt_opcode (BINOP_SUB); }
309 { write_exp_elt_opcode (BINOP_LSH); }
313 { write_exp_elt_opcode (BINOP_RSH); }
317 { write_exp_elt_opcode (BINOP_EQUAL); }
320 exp : exp NOTEQUAL exp
321 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
325 { write_exp_elt_opcode (BINOP_LEQ); }
329 { write_exp_elt_opcode (BINOP_GEQ); }
333 { write_exp_elt_opcode (BINOP_LESS); }
337 { write_exp_elt_opcode (BINOP_GTR); }
341 { write_exp_elt_opcode (BINOP_LOGAND); }
345 { write_exp_elt_opcode (BINOP_LOGXOR); }
349 { write_exp_elt_opcode (BINOP_LOGIOR); }
353 { write_exp_elt_opcode (BINOP_AND); }
357 { write_exp_elt_opcode (BINOP_OR); }
360 exp : exp '?' exp ':' exp %prec '?'
361 { write_exp_elt_opcode (TERNOP_COND); }
365 { write_exp_elt_opcode (BINOP_ASSIGN); }
368 exp : exp ASSIGN_MODIFY exp
369 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
370 write_exp_elt_opcode ($2);
371 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
375 { write_exp_elt_opcode (OP_LONG);
376 if ($1 == (int) $1 || $1 == (unsigned int) $1)
377 write_exp_elt_type (builtin_type_int);
379 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
380 write_exp_elt_longcst ((LONGEST) $1);
381 write_exp_elt_opcode (OP_LONG); }
386 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
387 write_exp_elt_opcode (OP_LONG);
388 if (val.lval == (int) val.lval ||
389 val.lval == (unsigned int) val.lval)
390 write_exp_elt_type (builtin_type_int);
392 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
393 write_exp_elt_longcst (val.lval);
394 write_exp_elt_opcode (OP_LONG); }
399 write_exp_elt_opcode (OP_LONG);
400 if ($1 == (unsigned int) $1)
401 write_exp_elt_type (builtin_type_unsigned_int);
403 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
404 write_exp_elt_longcst ((LONGEST) $1);
405 write_exp_elt_opcode (OP_LONG);
411 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
412 write_exp_elt_opcode (OP_LONG);
413 if (val.ulval == (unsigned int) val.ulval)
414 write_exp_elt_type (builtin_type_unsigned_int);
416 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
417 write_exp_elt_longcst ((LONGEST)val.ulval);
418 write_exp_elt_opcode (OP_LONG);
423 { write_exp_elt_opcode (OP_LONG);
424 write_exp_elt_type (builtin_type_char);
425 write_exp_elt_longcst ((LONGEST) $1);
426 write_exp_elt_opcode (OP_LONG); }
430 { write_exp_elt_opcode (OP_DOUBLE);
431 write_exp_elt_type (builtin_type_double);
432 write_exp_elt_dblcst ($1);
433 write_exp_elt_opcode (OP_DOUBLE); }
440 { write_exp_elt_opcode (OP_LAST);
441 write_exp_elt_longcst ((LONGEST) $1);
442 write_exp_elt_opcode (OP_LAST); }
446 { write_exp_elt_opcode (OP_REGISTER);
447 write_exp_elt_longcst ((LONGEST) $1);
448 write_exp_elt_opcode (OP_REGISTER); }
452 { write_exp_elt_opcode (OP_INTERNALVAR);
453 write_exp_elt_intern ($1);
454 write_exp_elt_opcode (OP_INTERNALVAR); }
457 exp : SIZEOF '(' type ')' %prec UNARY
458 { write_exp_elt_opcode (OP_LONG);
459 write_exp_elt_type (builtin_type_int);
460 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
461 write_exp_elt_opcode (OP_LONG); }
465 { write_exp_elt_opcode (OP_STRING);
466 write_exp_string ($1);
467 write_exp_elt_opcode (OP_STRING); }
472 { write_exp_elt_opcode (OP_THIS);
473 write_exp_elt_opcode (OP_THIS); }
481 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
485 lookup_symtab (copy_name ($1.stoken));
487 $$ = BLOCKVECTOR_BLOCK
488 (BLOCKVECTOR (tem), STATIC_BLOCK);
490 error ("No file or function \"%s\".",
491 copy_name ($1.stoken));
496 block : block COLONCOLON name
498 = lookup_symbol (copy_name ($3), $1,
499 VAR_NAMESPACE, 0, NULL);
500 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
501 error ("No function \"%s\" in specified context.",
503 $$ = SYMBOL_BLOCK_VALUE (tem); }
506 variable: block COLONCOLON name
507 { struct symbol *sym;
508 sym = lookup_symbol (copy_name ($3), $1,
509 VAR_NAMESPACE, 0, NULL);
511 error ("No symbol \"%s\" in specified context.",
514 write_exp_elt_opcode (OP_VAR_VALUE);
515 write_exp_elt_sym (sym);
516 write_exp_elt_opcode (OP_VAR_VALUE); }
519 variable: typebase COLONCOLON name
521 struct type *type = $1;
522 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
523 && TYPE_CODE (type) != TYPE_CODE_UNION)
524 error ("`%s' is not defined as an aggregate type.",
527 write_exp_elt_opcode (OP_SCOPE);
528 write_exp_elt_type (type);
529 write_exp_string ($3);
530 write_exp_elt_opcode (OP_SCOPE);
532 | typebase COLONCOLON '~' name
534 struct type *type = $1;
535 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
536 && TYPE_CODE (type) != TYPE_CODE_UNION)
537 error ("`%s' is not defined as an aggregate type.",
540 if (strcmp (type_name_no_tag (type), $4.ptr))
541 error ("invalid destructor `%s::~%s'",
542 type_name_no_tag (type), $4.ptr);
544 write_exp_elt_opcode (OP_SCOPE);
545 write_exp_elt_type (type);
546 write_exp_string ($4);
547 write_exp_elt_opcode (OP_SCOPE);
548 write_exp_elt_opcode (UNOP_LOGNOT);
552 char *name = copy_name ($2);
557 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
560 write_exp_elt_opcode (OP_VAR_VALUE);
561 write_exp_elt_sym (sym);
562 write_exp_elt_opcode (OP_VAR_VALUE);
565 for (i = 0; i < misc_function_count; i++)
566 if (!strcmp (misc_function_vector[i].name, name))
569 if (i < misc_function_count)
571 enum misc_function_type mft =
572 misc_function_vector[i].type;
574 write_exp_elt_opcode (OP_LONG);
575 write_exp_elt_type (builtin_type_int);
576 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
577 write_exp_elt_opcode (OP_LONG);
578 write_exp_elt_opcode (UNOP_MEMVAL);
579 if (mft == mf_data || mft == mf_bss)
580 write_exp_elt_type (builtin_type_int);
581 else if (mft == mf_text)
582 write_exp_elt_type (lookup_function_type (builtin_type_int));
584 write_exp_elt_type (builtin_type_char);
585 write_exp_elt_opcode (UNOP_MEMVAL);
589 && partial_symtab_list == 0)
590 error ("No symbol table is loaded. Use the \"file\" command.");
592 error ("No symbol \"%s\" in current context.", name);
596 variable: name_not_typename
597 { struct symbol *sym = $1.sym;
609 if (innermost_block == 0 ||
610 contained_in (block_found,
612 innermost_block = block_found;
619 case LOC_CONST_BYTES:
621 /* In this case the expression can
622 be evaluated regardless of what
623 frame we are in, so there is no
624 need to check for the
625 innermost_block. These cases are
626 listed so that gcc -Wall will
627 report types that may not have
632 write_exp_elt_opcode (OP_VAR_VALUE);
633 write_exp_elt_sym (sym);
634 write_exp_elt_opcode (OP_VAR_VALUE);
636 else if ($1.is_a_field_of_this)
638 /* C++: it hangs off of `this'. Must
639 not inadvertently convert from a method call
641 if (innermost_block == 0 ||
642 contained_in (block_found, innermost_block))
643 innermost_block = block_found;
644 write_exp_elt_opcode (OP_THIS);
645 write_exp_elt_opcode (OP_THIS);
646 write_exp_elt_opcode (STRUCTOP_PTR);
647 write_exp_string ($1.stoken);
648 write_exp_elt_opcode (STRUCTOP_PTR);
653 register char *arg = copy_name ($1.stoken);
655 /* FIXME, this search is linear! At least
656 optimize the strcmp with a 1-char cmp... */
657 for (i = 0; i < misc_function_count; i++)
658 if (!strcmp (misc_function_vector[i].name, arg))
661 if (i < misc_function_count)
663 enum misc_function_type mft =
664 misc_function_vector[i].type;
666 write_exp_elt_opcode (OP_LONG);
667 write_exp_elt_type (builtin_type_int);
668 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
669 write_exp_elt_opcode (OP_LONG);
670 write_exp_elt_opcode (UNOP_MEMVAL);
671 if (mft == mf_data || mft == mf_bss)
672 write_exp_elt_type (builtin_type_int);
673 else if (mft == mf_text)
674 write_exp_elt_type (lookup_function_type (builtin_type_int));
676 write_exp_elt_type (builtin_type_char);
677 write_exp_elt_opcode (UNOP_MEMVAL);
679 else if (symtab_list == 0
680 && partial_symtab_list == 0)
681 error ("No symbol table is loaded. Use the \"file\" command.");
683 error ("No symbol \"%s\" in current context.",
684 copy_name ($1.stoken));
693 /* This is where the interesting stuff happens. */
696 struct type *follow_type = $1;
705 follow_type = lookup_pointer_type (follow_type);
708 follow_type = lookup_reference_type (follow_type);
711 array_size = pop_type_int ();
712 if (array_size != -1)
713 follow_type = create_array_type (follow_type,
716 follow_type = lookup_pointer_type (follow_type);
719 follow_type = lookup_function_type (follow_type);
727 { push_type (tp_pointer); $$ = 0; }
729 { push_type (tp_pointer); $$ = $2; }
731 { push_type (tp_reference); $$ = 0; }
733 { push_type (tp_reference); $$ = $2; }
737 direct_abs_decl: '(' abs_decl ')'
739 | direct_abs_decl array_mod
742 push_type (tp_array);
747 push_type (tp_array);
750 | direct_abs_decl func_mod
751 { push_type (tp_function); }
753 { push_type (tp_function); }
767 | typebase COLONCOLON '*'
768 { $$ = lookup_member_type (builtin_type_int, $1); }
769 | type '(' typebase COLONCOLON '*' ')'
770 { $$ = lookup_member_type ($1, $3); }
771 | type '(' typebase COLONCOLON '*' ')' '(' ')'
772 { $$ = lookup_member_type
773 (lookup_function_type ($1), $3); }
774 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
775 { $$ = lookup_member_type
776 (lookup_function_type ($1), $3);
784 { $$ = builtin_type_int; }
786 { $$ = builtin_type_long; }
788 { $$ = builtin_type_short; }
790 { $$ = builtin_type_long; }
791 | UNSIGNED LONG INT_KEYWORD
792 { $$ = builtin_type_unsigned_long; }
794 { $$ = builtin_type_long_long; }
795 | LONG LONG INT_KEYWORD
796 { $$ = builtin_type_long_long; }
798 { $$ = builtin_type_unsigned_long_long; }
799 | UNSIGNED LONG LONG INT_KEYWORD
800 { $$ = builtin_type_unsigned_long_long; }
802 { $$ = builtin_type_short; }
803 | UNSIGNED SHORT INT_KEYWORD
804 { $$ = builtin_type_unsigned_short; }
806 { $$ = lookup_struct (copy_name ($2),
807 expression_context_block); }
809 { $$ = lookup_union (copy_name ($2),
810 expression_context_block); }
812 { $$ = lookup_enum (copy_name ($2),
813 expression_context_block); }
815 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
817 { $$ = builtin_type_unsigned_int; }
821 { $$ = builtin_type_int; }
827 $$.stoken.ptr = "int";
828 $$.stoken.length = 3;
829 $$.type = builtin_type_int;
833 $$.stoken.ptr = "long";
834 $$.stoken.length = 4;
835 $$.type = builtin_type_long;
839 $$.stoken.ptr = "short";
840 $$.stoken.length = 5;
841 $$.type = builtin_type_short;
847 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
848 $$[0] = (struct type *)0;
851 | nonempty_typelist ',' type
852 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
853 $$ = (struct type **)xrealloc ($1, len);
854 $$[$<ivec>$[0]] = $3;
858 name : NAME { $$ = $1.stoken; }
859 | BLOCKNAME { $$ = $1.stoken; }
860 | TYPENAME { $$ = $1.stoken; }
861 | NAME_OR_INT { $$ = $1.stoken; }
862 | NAME_OR_UINT { $$ = $1.stoken; }
865 name_not_typename : NAME
867 /* These would be useful if name_not_typename was useful, but it is just
868 a fake for "variable", so these cause reduce/reduce conflicts because
869 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
870 =exp) or just an exp. If name_not_typename was ever used in an lvalue
871 context where only a name could occur, this might be useful.
879 /* Take care of parsing a number (anything that starts with a digit).
880 Set yylval and return the token type; update lexptr.
881 LEN is the number of characters in it. */
883 /*** Needs some error checking for the float case ***/
886 parse_number (p, len, parsed_float, putithere)
892 register LONGEST n = 0;
893 register LONGEST prevn = 0;
896 register int base = input_radix;
899 extern double atof ();
903 /* It's a float since it contains a point or an exponent. */
904 putithere->dval = atof (p);
908 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
942 if (c >= 'A' && c <= 'Z')
944 if (c != 'l' && c != 'u')
946 if (c >= '0' && c <= '9')
950 if (base > 10 && c >= 'a' && c <= 'f')
951 n += i = c - 'a' + 10;
952 else if (len == 0 && c == 'l')
954 else if (len == 0 && c == 'u')
957 return ERROR; /* Char not a digit */
960 return ERROR; /* Invalid digit in this base */
961 if(!unsigned_p && (prevn >= n))
962 unsigned_p=1; /* Try something unsigned */
963 /* Don't do the range check if n==i and i==0, since that special
964 case will give an overflow error. */
965 if(RANGE_CHECK && n!=0)
967 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
968 range_error("Overflow on numeric constant.");
975 putithere->ulval = n;
989 enum exp_opcode opcode;
992 const static struct token tokentab3[] =
994 {">>=", ASSIGN_MODIFY, BINOP_RSH},
995 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
998 const static struct token tokentab2[] =
1000 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1001 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1002 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1003 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1004 {"%=", ASSIGN_MODIFY, BINOP_REM},
1005 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1006 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1007 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1008 {"++", INCREMENT, BINOP_END},
1009 {"--", DECREMENT, BINOP_END},
1010 {"->", ARROW, BINOP_END},
1011 {"&&", AND, BINOP_END},
1012 {"||", OR, BINOP_END},
1013 {"::", COLONCOLON, BINOP_END},
1014 {"<<", LSH, BINOP_END},
1015 {">>", RSH, BINOP_END},
1016 {"==", EQUAL, BINOP_END},
1017 {"!=", NOTEQUAL, BINOP_END},
1018 {"<=", LEQ, BINOP_END},
1019 {">=", GEQ, BINOP_END}
1022 /* Read one token, getting characters through lexptr. */
1028 register int namelen;
1029 register unsigned i;
1030 register char *tokstart;
1035 /* See if it is a special token of length 3. */
1036 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1037 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1040 yylval.opcode = tokentab3[i].opcode;
1041 return tokentab3[i].token;
1044 /* See if it is a special token of length 2. */
1045 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1046 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1049 yylval.opcode = tokentab2[i].opcode;
1050 return tokentab2[i].token;
1053 switch (c = *tokstart)
1068 c = parse_escape (&lexptr);
1072 error ("Invalid character constant.");
1081 if (paren_depth == 0)
1088 if (comma_terminates && paren_depth == 0)
1094 /* Might be a floating point number. */
1095 if (lexptr[1] < '0' || lexptr[1] > '9')
1096 goto symbol; /* Nope, must be a symbol. */
1097 /* FALL THRU into number case. */
1110 /* It's a number. */
1111 int got_dot = 0, got_e = 0, toktype;
1112 register char *p = tokstart;
1113 int hex = input_radix > 10;
1115 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1120 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1128 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1129 got_dot = got_e = 1;
1130 else if (!hex && !got_dot && *p == '.')
1132 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1133 && (*p == '-' || *p == '+'))
1134 /* This is the sign of the exponent, not the end of the
1137 /* We will take any letters or digits. parse_number will
1138 complain if past the radix, or if L or U are not final. */
1139 else if ((*p < '0' || *p > '9')
1140 && ((*p < 'a' || *p > 'z')
1141 && (*p < 'A' || *p > 'Z')))
1144 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1145 if (toktype == ERROR)
1147 char *err_copy = (char *) alloca (p - tokstart + 1);
1149 bcopy (tokstart, err_copy, p - tokstart);
1150 err_copy[p - tokstart] = 0;
1151 error ("Invalid number \"%s\".", err_copy);
1182 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1185 c = tokstart[++namelen];
1186 if (c >= '0' && c <= '9')
1188 c = tokstart[++namelen];
1189 if (c >= '0' && c <= '9')
1190 c = tokstart[++namelen];
1193 yylval.sval.ptr = tokstart + 1;
1194 yylval.sval.length = namelen - 1;
1195 lexptr += namelen + 1;
1199 if (!(c == '_' || c == '$'
1200 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1201 /* We must have come across a bad character (e.g. ';'). */
1202 error ("Invalid character '%c' in expression.", c);
1204 /* It's a name. See how long it is. */
1206 for (c = tokstart[namelen];
1207 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1208 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1209 c = tokstart[++namelen])
1212 /* The token "if" terminates the expression and is NOT
1213 removed from the input stream. */
1214 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1221 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1222 and $$digits (equivalent to $<-digits> if you could type that).
1223 Make token type LAST, and put the number (the digits) in yylval. */
1225 if (*tokstart == '$')
1227 register int negate = 0;
1229 /* Double dollar means negate the number and add -1 as well.
1230 Thus $$ alone means -1. */
1231 if (namelen >= 2 && tokstart[1] == '$')
1238 /* Just dollars (one or two) */
1239 yylval.lval = - negate;
1242 /* Is the rest of the token digits? */
1243 for (; c < namelen; c++)
1244 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1248 yylval.lval = atoi (tokstart + 1 + negate);
1250 yylval.lval = - yylval.lval;
1255 /* Handle tokens that refer to machine registers:
1256 $ followed by a register name. */
1258 if (*tokstart == '$') {
1259 for (c = 0; c < NUM_REGS; c++)
1260 if (namelen - 1 == strlen (reg_names[c])
1261 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1266 for (c = 0; c < num_std_regs; c++)
1267 if (namelen - 1 == strlen (std_regs[c].name)
1268 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1270 yylval.lval = std_regs[c].regnum;
1274 /* Catch specific keywords. Should be done with a data structure. */
1278 if (!strncmp (tokstart, "unsigned", 8))
1282 if (!strncmp (tokstart, "struct", 6))
1284 if (!strncmp (tokstart, "signed", 6))
1286 if (!strncmp (tokstart, "sizeof", 6))
1290 if (!strncmp (tokstart, "union", 5))
1292 if (!strncmp (tokstart, "short", 5))
1296 if (!strncmp (tokstart, "enum", 4))
1298 if (!strncmp (tokstart, "long", 4))
1300 if (!strncmp (tokstart, "this", 4))
1302 static const char this_name[] =
1303 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1305 if (lookup_symbol (this_name, expression_context_block,
1306 VAR_NAMESPACE, 0, NULL))
1311 if (!strncmp (tokstart, "int", 3))
1318 yylval.sval.ptr = tokstart;
1319 yylval.sval.length = namelen;
1321 /* Any other names starting in $ are debugger internal variables. */
1323 if (*tokstart == '$')
1325 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1329 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1330 functions or symtabs. If this is not so, then ...
1331 Use token-type TYPENAME for symbols that happen to be defined
1332 currently as names of types; NAME for other symbols.
1333 The caller is not constrained to care about the distinction. */
1335 char *tmp = copy_name (yylval.sval);
1337 int is_a_field_of_this = 0;
1340 sym = lookup_symbol (tmp, expression_context_block,
1341 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1342 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1343 lookup_partial_symtab (tmp))
1345 yylval.ssym.sym = sym;
1346 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1349 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1351 yylval.tsym.type = SYMBOL_TYPE (sym);
1354 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1357 /* Input names that aren't symbols but ARE valid hex numbers,
1358 when the input radix permits them, can be names or numbers
1359 depending on the parse. Note we support radixes > 16 here. */
1361 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1362 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1364 YYSTYPE newlval; /* Its value is ignored. */
1365 hextype = parse_number (tokstart, namelen, 0, &newlval);
1368 yylval.ssym.sym = sym;
1369 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1372 if (hextype == UINT)
1374 yylval.ssym.sym = sym;
1375 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1376 return NAME_OR_UINT;
1380 /* Any other kind of symbol */
1381 yylval.ssym.sym = sym;
1382 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1391 error ("Invalid syntax in expression.");
1394 /* Table mapping opcodes into strings for printing operators
1395 and precedences of the operators. */
1397 const static struct op_print c_op_print_tab[] =
1399 {",", BINOP_COMMA, PREC_COMMA, 0},
1400 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1401 {"||", BINOP_OR, PREC_OR, 0},
1402 {"&&", BINOP_AND, PREC_AND, 0},
1403 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1404 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1405 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1406 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1407 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1408 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1409 {">=", BINOP_GEQ, PREC_ORDER, 0},
1410 {">", BINOP_GTR, PREC_ORDER, 0},
1411 {"<", BINOP_LESS, PREC_ORDER, 0},
1412 {">>", BINOP_RSH, PREC_SHIFT, 0},
1413 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1414 {"+", BINOP_ADD, PREC_ADD, 0},
1415 {"-", BINOP_SUB, PREC_ADD, 0},
1416 {"*", BINOP_MUL, PREC_MUL, 0},
1417 {"/", BINOP_DIV, PREC_MUL, 0},
1418 {"%", BINOP_REM, PREC_MUL, 0},
1419 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1420 {"-", UNOP_NEG, PREC_PREFIX, 0},
1421 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1422 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1423 {"*", UNOP_IND, PREC_PREFIX, 0},
1424 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1425 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1426 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1427 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1429 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1432 /* These variables point to the objects
1433 representing the predefined C data types. */
1435 struct type *builtin_type_void;
1436 struct type *builtin_type_char;
1437 struct type *builtin_type_short;
1438 struct type *builtin_type_int;
1439 struct type *builtin_type_long;
1440 struct type *builtin_type_long_long;
1441 struct type *builtin_type_unsigned_char;
1442 struct type *builtin_type_unsigned_short;
1443 struct type *builtin_type_unsigned_int;
1444 struct type *builtin_type_unsigned_long;
1445 struct type *builtin_type_unsigned_long_long;
1446 struct type *builtin_type_float;
1447 struct type *builtin_type_double;
1449 struct type ** const (c_builtin_types[]) =
1453 &builtin_type_short,
1455 &builtin_type_float,
1456 &builtin_type_double,
1458 &builtin_type_long_long,
1459 &builtin_type_unsigned_char,
1460 &builtin_type_unsigned_short,
1461 &builtin_type_unsigned_int,
1462 &builtin_type_unsigned_long,
1463 &builtin_type_unsigned_long_long,
1467 /* FIXME: Eventually do a separate defintion for C++. */
1469 const struct language_defn c_language_defn = {
1470 "c", /* Language name */
1477 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1478 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1479 &builtin_type_double, /* longest floating point type */
1480 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1481 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1482 c_op_print_tab, /* expression operators for printing */
1487 _initialize_c_exp ()
1489 /* FIXME: The code below assumes that the sizes of the basic data
1490 types are the same on the host and target machines!!! */
1492 builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
1494 builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
1495 builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
1497 builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
1498 builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
1499 builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
1500 builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
1502 builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
1503 builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
1504 builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
1505 builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
1507 builtin_type_long_long =
1508 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1510 builtin_type_unsigned_long_long =
1511 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1512 1, "unsigned long long");
1514 add_language (&c_language_defn);
1515 set_language (language_c); /* Make C the default language */