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 #define yystate c_state
71 static int parse_number ();
74 /* #define YYDEBUG 1 */
78 /* Although the yacc "value" of an expression is not used,
79 since the result is stored in the structure being created,
80 other node types do have values. */
85 unsigned LONGEST ulval;
94 enum exp_opcode opcode;
95 struct internalvar *ivar;
101 %type <voidval> exp exp1 type_exp start variable
102 %type <tval> type typebase
103 %type <tvec> nonempty_typelist
104 /* %type <bval> block */
106 /* Fancy type parsing. */
107 %type <voidval> func_mod direct_abs_decl abs_decl
109 %type <lval> array_mod
111 %token <lval> INT CHAR
115 /* Both NAME and TYPENAME tokens represent symbols in the input,
116 and both convey their data as strings.
117 But a TYPENAME is a string that happens to be defined as a typedef
118 or builtin type name (such as int or char)
119 and a NAME is any other symbol.
120 Contexts where this distinction is not important can use the
121 nonterminal "name", which matches either NAME or TYPENAME. */
124 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
125 %token <tsym> TYPENAME
127 %type <ssym> name_not_typename
128 %type <tsym> typename
130 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
131 but which would parse as a valid number in the current input radix.
132 E.g. "c" when input_radix==16. Depending on the parse, it will be
133 turned into a name or into a number. NAME_OR_UINT ditto. */
135 %token <ssym> NAME_OR_INT NAME_OR_UINT
137 %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
140 /* Special type cases, put in to allow the parser to distinguish different
142 %token SIGNED LONG SHORT INT_KEYWORD
144 %token <lval> LAST REGNAME
146 %token <ivar> VARIABLE
148 %token <opcode> ASSIGN_MODIFY
155 %right '=' ASSIGN_MODIFY
163 %left '<' '>' LEQ GEQ
168 %right UNARY INCREMENT DECREMENT
169 %right ARROW '.' '[' '('
170 %token <ssym> BLOCKNAME
181 { write_exp_elt_opcode(OP_TYPE);
182 write_exp_elt_type($1);
183 write_exp_elt_opcode(OP_TYPE);}
186 /* Expressions, including the comma operator. */
189 { write_exp_elt_opcode (BINOP_COMMA); }
192 /* Expressions, not including the comma operator. */
193 exp : '*' exp %prec UNARY
194 { write_exp_elt_opcode (UNOP_IND); }
196 exp : '&' exp %prec UNARY
197 { write_exp_elt_opcode (UNOP_ADDR); }
199 exp : '-' exp %prec UNARY
200 { write_exp_elt_opcode (UNOP_NEG); }
203 exp : '!' exp %prec UNARY
204 { write_exp_elt_opcode (UNOP_ZEROP); }
207 exp : '~' exp %prec UNARY
208 { write_exp_elt_opcode (UNOP_LOGNOT); }
211 exp : INCREMENT exp %prec UNARY
212 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
215 exp : DECREMENT exp %prec UNARY
216 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
219 exp : exp INCREMENT %prec UNARY
220 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
223 exp : exp DECREMENT %prec UNARY
224 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
227 exp : SIZEOF exp %prec UNARY
228 { write_exp_elt_opcode (UNOP_SIZEOF); }
232 { write_exp_elt_opcode (STRUCTOP_PTR);
233 write_exp_string ($3);
234 write_exp_elt_opcode (STRUCTOP_PTR); }
237 exp : exp ARROW '*' exp
238 { write_exp_elt_opcode (STRUCTOP_MPTR); }
242 { write_exp_elt_opcode (STRUCTOP_STRUCT);
243 write_exp_string ($3);
244 write_exp_elt_opcode (STRUCTOP_STRUCT); }
247 exp : exp '.' '*' exp
248 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
251 exp : exp '[' exp1 ']'
252 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
256 /* This is to save the value of arglist_len
257 being accumulated by an outer function call. */
258 { start_arglist (); }
259 arglist ')' %prec ARROW
260 { write_exp_elt_opcode (OP_FUNCALL);
261 write_exp_elt_longcst ((LONGEST) end_arglist ());
262 write_exp_elt_opcode (OP_FUNCALL); }
272 arglist : arglist ',' exp %prec ABOVE_COMMA
276 exp : '{' type '}' exp %prec UNARY
277 { write_exp_elt_opcode (UNOP_MEMVAL);
278 write_exp_elt_type ($2);
279 write_exp_elt_opcode (UNOP_MEMVAL); }
282 exp : '(' type ')' exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_CAST);
284 write_exp_elt_type ($2);
285 write_exp_elt_opcode (UNOP_CAST); }
292 /* Binary operators in order of decreasing precedence. */
295 { write_exp_elt_opcode (BINOP_REPEAT); }
299 { write_exp_elt_opcode (BINOP_MUL); }
303 { write_exp_elt_opcode (BINOP_DIV); }
307 { write_exp_elt_opcode (BINOP_REM); }
311 { write_exp_elt_opcode (BINOP_ADD); }
315 { write_exp_elt_opcode (BINOP_SUB); }
319 { write_exp_elt_opcode (BINOP_LSH); }
323 { write_exp_elt_opcode (BINOP_RSH); }
327 { write_exp_elt_opcode (BINOP_EQUAL); }
330 exp : exp NOTEQUAL exp
331 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
335 { write_exp_elt_opcode (BINOP_LEQ); }
339 { write_exp_elt_opcode (BINOP_GEQ); }
343 { write_exp_elt_opcode (BINOP_LESS); }
347 { write_exp_elt_opcode (BINOP_GTR); }
351 { write_exp_elt_opcode (BINOP_LOGAND); }
355 { write_exp_elt_opcode (BINOP_LOGXOR); }
359 { write_exp_elt_opcode (BINOP_LOGIOR); }
363 { write_exp_elt_opcode (BINOP_AND); }
367 { write_exp_elt_opcode (BINOP_OR); }
370 exp : exp '?' exp ':' exp %prec '?'
371 { write_exp_elt_opcode (TERNOP_COND); }
375 { write_exp_elt_opcode (BINOP_ASSIGN); }
378 exp : exp ASSIGN_MODIFY exp
379 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
380 write_exp_elt_opcode ($2);
381 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
385 { write_exp_elt_opcode (OP_LONG);
386 if ($1 == (int) $1 || $1 == (unsigned int) $1)
387 write_exp_elt_type (builtin_type_int);
389 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
390 write_exp_elt_longcst ((LONGEST) $1);
391 write_exp_elt_opcode (OP_LONG); }
396 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
397 write_exp_elt_opcode (OP_LONG);
398 if (val.lval == (int) val.lval ||
399 val.lval == (unsigned int) val.lval)
400 write_exp_elt_type (builtin_type_int);
402 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
403 write_exp_elt_longcst (val.lval);
404 write_exp_elt_opcode (OP_LONG); }
409 write_exp_elt_opcode (OP_LONG);
410 if ($1 == (unsigned int) $1)
411 write_exp_elt_type (builtin_type_unsigned_int);
413 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
414 write_exp_elt_longcst ((LONGEST) $1);
415 write_exp_elt_opcode (OP_LONG);
421 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
422 write_exp_elt_opcode (OP_LONG);
423 if (val.ulval == (unsigned int) val.ulval)
424 write_exp_elt_type (builtin_type_unsigned_int);
426 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
427 write_exp_elt_longcst ((LONGEST)val.ulval);
428 write_exp_elt_opcode (OP_LONG);
433 { write_exp_elt_opcode (OP_LONG);
434 write_exp_elt_type (builtin_type_char);
435 write_exp_elt_longcst ((LONGEST) $1);
436 write_exp_elt_opcode (OP_LONG); }
440 { write_exp_elt_opcode (OP_DOUBLE);
441 write_exp_elt_type (builtin_type_double);
442 write_exp_elt_dblcst ($1);
443 write_exp_elt_opcode (OP_DOUBLE); }
450 { write_exp_elt_opcode (OP_LAST);
451 write_exp_elt_longcst ((LONGEST) $1);
452 write_exp_elt_opcode (OP_LAST); }
456 { write_exp_elt_opcode (OP_REGISTER);
457 write_exp_elt_longcst ((LONGEST) $1);
458 write_exp_elt_opcode (OP_REGISTER); }
462 { write_exp_elt_opcode (OP_INTERNALVAR);
463 write_exp_elt_intern ($1);
464 write_exp_elt_opcode (OP_INTERNALVAR); }
467 exp : SIZEOF '(' type ')' %prec UNARY
468 { write_exp_elt_opcode (OP_LONG);
469 write_exp_elt_type (builtin_type_int);
470 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
471 write_exp_elt_opcode (OP_LONG); }
475 { write_exp_elt_opcode (OP_STRING);
476 write_exp_string ($1);
477 write_exp_elt_opcode (OP_STRING); }
482 { write_exp_elt_opcode (OP_THIS);
483 write_exp_elt_opcode (OP_THIS); }
491 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
495 lookup_symtab (copy_name ($1.stoken));
497 $$ = BLOCKVECTOR_BLOCK
498 (BLOCKVECTOR (tem), STATIC_BLOCK);
500 error ("No file or function \"%s\".",
501 copy_name ($1.stoken));
506 block : block COLONCOLON name
508 = lookup_symbol (copy_name ($3), $1,
509 VAR_NAMESPACE, 0, NULL);
510 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
511 error ("No function \"%s\" in specified context.",
513 $$ = SYMBOL_BLOCK_VALUE (tem); }
516 variable: block COLONCOLON name
517 { struct symbol *sym;
518 sym = lookup_symbol (copy_name ($3), $1,
519 VAR_NAMESPACE, 0, NULL);
521 error ("No symbol \"%s\" in specified context.",
524 write_exp_elt_opcode (OP_VAR_VALUE);
525 write_exp_elt_sym (sym);
526 write_exp_elt_opcode (OP_VAR_VALUE); }
529 variable: 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 write_exp_elt_opcode (OP_SCOPE);
538 write_exp_elt_type (type);
539 write_exp_string ($3);
540 write_exp_elt_opcode (OP_SCOPE);
542 | typebase COLONCOLON '~' name
544 struct type *type = $1;
545 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
546 && TYPE_CODE (type) != TYPE_CODE_UNION)
547 error ("`%s' is not defined as an aggregate type.",
550 if (strcmp (type_name_no_tag (type), $4.ptr))
551 error ("invalid destructor `%s::~%s'",
552 type_name_no_tag (type), $4.ptr);
554 write_exp_elt_opcode (OP_SCOPE);
555 write_exp_elt_type (type);
556 write_exp_string ($4);
557 write_exp_elt_opcode (OP_SCOPE);
558 write_exp_elt_opcode (UNOP_LOGNOT);
562 char *name = copy_name ($2);
567 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
570 write_exp_elt_opcode (OP_VAR_VALUE);
571 write_exp_elt_sym (sym);
572 write_exp_elt_opcode (OP_VAR_VALUE);
575 for (i = 0; i < misc_function_count; i++)
576 if (!strcmp (misc_function_vector[i].name, name))
579 if (i < misc_function_count)
581 enum misc_function_type mft =
582 misc_function_vector[i].type;
584 write_exp_elt_opcode (OP_LONG);
585 write_exp_elt_type (builtin_type_int);
586 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
587 write_exp_elt_opcode (OP_LONG);
588 write_exp_elt_opcode (UNOP_MEMVAL);
589 if (mft == mf_data || mft == mf_bss)
590 write_exp_elt_type (builtin_type_int);
591 else if (mft == mf_text)
592 write_exp_elt_type (lookup_function_type (builtin_type_int));
594 write_exp_elt_type (builtin_type_char);
595 write_exp_elt_opcode (UNOP_MEMVAL);
599 && partial_symtab_list == 0)
600 error ("No symbol table is loaded. Use the \"file\" command.");
602 error ("No symbol \"%s\" in current context.", name);
606 variable: name_not_typename
607 { struct symbol *sym = $1.sym;
611 switch (SYMBOL_CLASS (sym))
619 if (innermost_block == 0 ||
620 contained_in (block_found,
622 innermost_block = block_found;
629 case LOC_CONST_BYTES:
631 /* In this case the expression can
632 be evaluated regardless of what
633 frame we are in, so there is no
634 need to check for the
635 innermost_block. These cases are
636 listed so that gcc -Wall will
637 report types that may not have
642 write_exp_elt_opcode (OP_VAR_VALUE);
643 write_exp_elt_sym (sym);
644 write_exp_elt_opcode (OP_VAR_VALUE);
646 else if ($1.is_a_field_of_this)
648 /* C++: it hangs off of `this'. Must
649 not inadvertently convert from a method call
651 if (innermost_block == 0 ||
652 contained_in (block_found, innermost_block))
653 innermost_block = block_found;
654 write_exp_elt_opcode (OP_THIS);
655 write_exp_elt_opcode (OP_THIS);
656 write_exp_elt_opcode (STRUCTOP_PTR);
657 write_exp_string ($1.stoken);
658 write_exp_elt_opcode (STRUCTOP_PTR);
663 register char *arg = copy_name ($1.stoken);
665 /* FIXME, this search is linear! At least
666 optimize the strcmp with a 1-char cmp... */
667 for (i = 0; i < misc_function_count; i++)
668 if (!strcmp (misc_function_vector[i].name, arg))
671 if (i < misc_function_count)
673 enum misc_function_type mft =
674 misc_function_vector[i].type;
676 write_exp_elt_opcode (OP_LONG);
677 write_exp_elt_type (builtin_type_int);
678 write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
679 write_exp_elt_opcode (OP_LONG);
680 write_exp_elt_opcode (UNOP_MEMVAL);
681 if (mft == mf_data || mft == mf_bss)
682 write_exp_elt_type (builtin_type_int);
683 else if (mft == mf_text)
684 write_exp_elt_type (lookup_function_type (builtin_type_int));
686 write_exp_elt_type (builtin_type_char);
687 write_exp_elt_opcode (UNOP_MEMVAL);
689 else if (symtab_list == 0
690 && partial_symtab_list == 0)
691 error ("No symbol table is loaded. Use the \"file\" command.");
693 error ("No symbol \"%s\" in current context.",
694 copy_name ($1.stoken));
703 /* This is where the interesting stuff happens. */
706 struct type *follow_type = $1;
715 follow_type = lookup_pointer_type (follow_type);
718 follow_type = lookup_reference_type (follow_type);
721 array_size = pop_type_int ();
722 if (array_size != -1)
723 follow_type = create_array_type (follow_type,
726 follow_type = lookup_pointer_type (follow_type);
729 follow_type = lookup_function_type (follow_type);
737 { push_type (tp_pointer); $$ = 0; }
739 { push_type (tp_pointer); $$ = $2; }
741 { push_type (tp_reference); $$ = 0; }
743 { push_type (tp_reference); $$ = $2; }
747 direct_abs_decl: '(' abs_decl ')'
749 | direct_abs_decl array_mod
752 push_type (tp_array);
757 push_type (tp_array);
760 | direct_abs_decl func_mod
761 { push_type (tp_function); }
763 { push_type (tp_function); }
777 | typebase COLONCOLON '*'
778 { $$ = lookup_member_type (builtin_type_int, $1); }
779 | type '(' typebase COLONCOLON '*' ')'
780 { $$ = lookup_member_type ($1, $3); }
781 | type '(' typebase COLONCOLON '*' ')' '(' ')'
782 { $$ = lookup_member_type
783 (lookup_function_type ($1), $3); }
784 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
785 { $$ = lookup_member_type
786 (lookup_function_type ($1), $3);
794 { $$ = builtin_type_int; }
796 { $$ = builtin_type_long; }
798 { $$ = builtin_type_short; }
800 { $$ = builtin_type_long; }
801 | UNSIGNED LONG INT_KEYWORD
802 { $$ = builtin_type_unsigned_long; }
804 { $$ = builtin_type_long_long; }
805 | LONG LONG INT_KEYWORD
806 { $$ = builtin_type_long_long; }
808 { $$ = builtin_type_unsigned_long_long; }
809 | UNSIGNED LONG LONG INT_KEYWORD
810 { $$ = builtin_type_unsigned_long_long; }
812 { $$ = builtin_type_short; }
813 | UNSIGNED SHORT INT_KEYWORD
814 { $$ = builtin_type_unsigned_short; }
816 { $$ = lookup_struct (copy_name ($2),
817 expression_context_block); }
819 { $$ = lookup_union (copy_name ($2),
820 expression_context_block); }
822 { $$ = lookup_enum (copy_name ($2),
823 expression_context_block); }
825 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
827 { $$ = builtin_type_unsigned_int; }
831 { $$ = builtin_type_int; }
837 $$.stoken.ptr = "int";
838 $$.stoken.length = 3;
839 $$.type = builtin_type_int;
843 $$.stoken.ptr = "long";
844 $$.stoken.length = 4;
845 $$.type = builtin_type_long;
849 $$.stoken.ptr = "short";
850 $$.stoken.length = 5;
851 $$.type = builtin_type_short;
857 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
858 $$[0] = (struct type *)0;
861 | nonempty_typelist ',' type
862 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
863 $$ = (struct type **)xrealloc ($1, len);
864 $$[$<ivec>$[0]] = $3;
868 name : NAME { $$ = $1.stoken; }
869 | BLOCKNAME { $$ = $1.stoken; }
870 | TYPENAME { $$ = $1.stoken; }
871 | NAME_OR_INT { $$ = $1.stoken; }
872 | NAME_OR_UINT { $$ = $1.stoken; }
875 name_not_typename : NAME
877 /* These would be useful if name_not_typename was useful, but it is just
878 a fake for "variable", so these cause reduce/reduce conflicts because
879 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
880 =exp) or just an exp. If name_not_typename was ever used in an lvalue
881 context where only a name could occur, this might be useful.
889 /* Take care of parsing a number (anything that starts with a digit).
890 Set yylval and return the token type; update lexptr.
891 LEN is the number of characters in it. */
893 /*** Needs some error checking for the float case ***/
896 parse_number (p, len, parsed_float, putithere)
902 register LONGEST n = 0;
903 register LONGEST prevn = 0;
906 register int base = input_radix;
909 extern double atof ();
913 /* It's a float since it contains a point or an exponent. */
914 putithere->dval = atof (p);
918 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
952 if (c >= 'A' && c <= 'Z')
954 if (c != 'l' && c != 'u')
956 if (c >= '0' && c <= '9')
960 if (base > 10 && c >= 'a' && c <= 'f')
961 n += i = c - 'a' + 10;
962 else if (len == 0 && c == 'l')
964 else if (len == 0 && c == 'u')
967 return ERROR; /* Char not a digit */
970 return ERROR; /* Invalid digit in this base */
971 if(!unsigned_p && (prevn >= n))
972 unsigned_p=1; /* Try something unsigned */
973 /* Don't do the range check if n==i and i==0, since that special
974 case will give an overflow error. */
975 if(RANGE_CHECK && n!=0)
977 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
978 range_error("Overflow on numeric constant.");
985 putithere->ulval = n;
999 enum exp_opcode opcode;
1002 const static struct token tokentab3[] =
1004 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1005 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1008 const static struct token tokentab2[] =
1010 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1011 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1012 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1013 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1014 {"%=", ASSIGN_MODIFY, BINOP_REM},
1015 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1016 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1017 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1018 {"++", INCREMENT, BINOP_END},
1019 {"--", DECREMENT, BINOP_END},
1020 {"->", ARROW, BINOP_END},
1021 {"&&", AND, BINOP_END},
1022 {"||", OR, BINOP_END},
1023 {"::", COLONCOLON, BINOP_END},
1024 {"<<", LSH, BINOP_END},
1025 {">>", RSH, BINOP_END},
1026 {"==", EQUAL, BINOP_END},
1027 {"!=", NOTEQUAL, BINOP_END},
1028 {"<=", LEQ, BINOP_END},
1029 {">=", GEQ, BINOP_END}
1032 /* Read one token, getting characters through lexptr. */
1038 register int namelen;
1039 register unsigned i;
1040 register char *tokstart;
1045 /* See if it is a special token of length 3. */
1046 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1047 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1050 yylval.opcode = tokentab3[i].opcode;
1051 return tokentab3[i].token;
1054 /* See if it is a special token of length 2. */
1055 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1056 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1059 yylval.opcode = tokentab2[i].opcode;
1060 return tokentab2[i].token;
1063 switch (c = *tokstart)
1078 c = parse_escape (&lexptr);
1082 error ("Invalid character constant.");
1091 if (paren_depth == 0)
1098 if (comma_terminates && paren_depth == 0)
1104 /* Might be a floating point number. */
1105 if (lexptr[1] < '0' || lexptr[1] > '9')
1106 goto symbol; /* Nope, must be a symbol. */
1107 /* FALL THRU into number case. */
1120 /* It's a number. */
1121 int got_dot = 0, got_e = 0, toktype;
1122 register char *p = tokstart;
1123 int hex = input_radix > 10;
1125 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1130 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1138 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1139 got_dot = got_e = 1;
1140 else if (!hex && !got_dot && *p == '.')
1142 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1143 && (*p == '-' || *p == '+'))
1144 /* This is the sign of the exponent, not the end of the
1147 /* We will take any letters or digits. parse_number will
1148 complain if past the radix, or if L or U are not final. */
1149 else if ((*p < '0' || *p > '9')
1150 && ((*p < 'a' || *p > 'z')
1151 && (*p < 'A' || *p > 'Z')))
1154 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1155 if (toktype == ERROR)
1157 char *err_copy = (char *) alloca (p - tokstart + 1);
1159 bcopy (tokstart, err_copy, p - tokstart);
1160 err_copy[p - tokstart] = 0;
1161 error ("Invalid number \"%s\".", err_copy);
1192 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1195 c = tokstart[++namelen];
1196 if (c >= '0' && c <= '9')
1198 c = tokstart[++namelen];
1199 if (c >= '0' && c <= '9')
1200 c = tokstart[++namelen];
1203 yylval.sval.ptr = tokstart + 1;
1204 yylval.sval.length = namelen - 1;
1205 lexptr += namelen + 1;
1209 if (!(c == '_' || c == '$'
1210 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1211 /* We must have come across a bad character (e.g. ';'). */
1212 error ("Invalid character '%c' in expression.", c);
1214 /* It's a name. See how long it is. */
1216 for (c = tokstart[namelen];
1217 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1218 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1219 c = tokstart[++namelen])
1222 /* The token "if" terminates the expression and is NOT
1223 removed from the input stream. */
1224 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1231 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1232 and $$digits (equivalent to $<-digits> if you could type that).
1233 Make token type LAST, and put the number (the digits) in yylval. */
1235 if (*tokstart == '$')
1237 register int negate = 0;
1239 /* Double dollar means negate the number and add -1 as well.
1240 Thus $$ alone means -1. */
1241 if (namelen >= 2 && tokstart[1] == '$')
1248 /* Just dollars (one or two) */
1249 yylval.lval = - negate;
1252 /* Is the rest of the token digits? */
1253 for (; c < namelen; c++)
1254 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1258 yylval.lval = atoi (tokstart + 1 + negate);
1260 yylval.lval = - yylval.lval;
1265 /* Handle tokens that refer to machine registers:
1266 $ followed by a register name. */
1268 if (*tokstart == '$') {
1269 for (c = 0; c < NUM_REGS; c++)
1270 if (namelen - 1 == strlen (reg_names[c])
1271 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1276 for (c = 0; c < num_std_regs; c++)
1277 if (namelen - 1 == strlen (std_regs[c].name)
1278 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1280 yylval.lval = std_regs[c].regnum;
1284 /* Catch specific keywords. Should be done with a data structure. */
1288 if (!strncmp (tokstart, "unsigned", 8))
1292 if (!strncmp (tokstart, "struct", 6))
1294 if (!strncmp (tokstart, "signed", 6))
1296 if (!strncmp (tokstart, "sizeof", 6))
1300 if (!strncmp (tokstart, "union", 5))
1302 if (!strncmp (tokstart, "short", 5))
1306 if (!strncmp (tokstart, "enum", 4))
1308 if (!strncmp (tokstart, "long", 4))
1310 if (!strncmp (tokstart, "this", 4))
1312 static const char this_name[] =
1313 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1315 if (lookup_symbol (this_name, expression_context_block,
1316 VAR_NAMESPACE, 0, NULL))
1321 if (!strncmp (tokstart, "int", 3))
1328 yylval.sval.ptr = tokstart;
1329 yylval.sval.length = namelen;
1331 /* Any other names starting in $ are debugger internal variables. */
1333 if (*tokstart == '$')
1335 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1339 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1340 functions or symtabs. If this is not so, then ...
1341 Use token-type TYPENAME for symbols that happen to be defined
1342 currently as names of types; NAME for other symbols.
1343 The caller is not constrained to care about the distinction. */
1345 char *tmp = copy_name (yylval.sval);
1347 int is_a_field_of_this = 0;
1350 sym = lookup_symbol (tmp, expression_context_block,
1351 VAR_NAMESPACE, &is_a_field_of_this, NULL);
1352 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1353 lookup_partial_symtab (tmp))
1355 yylval.ssym.sym = sym;
1356 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1359 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1361 yylval.tsym.type = SYMBOL_TYPE (sym);
1364 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1367 /* Input names that aren't symbols but ARE valid hex numbers,
1368 when the input radix permits them, can be names or numbers
1369 depending on the parse. Note we support radixes > 16 here. */
1371 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1372 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1374 YYSTYPE newlval; /* Its value is ignored. */
1375 hextype = parse_number (tokstart, namelen, 0, &newlval);
1378 yylval.ssym.sym = sym;
1379 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1382 if (hextype == UINT)
1384 yylval.ssym.sym = sym;
1385 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1386 return NAME_OR_UINT;
1390 /* Any other kind of symbol */
1391 yylval.ssym.sym = sym;
1392 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1401 error ("Invalid syntax in expression.");
1404 /* Table mapping opcodes into strings for printing operators
1405 and precedences of the operators. */
1407 const static struct op_print c_op_print_tab[] =
1409 {",", BINOP_COMMA, PREC_COMMA, 0},
1410 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1411 {"||", BINOP_OR, PREC_OR, 0},
1412 {"&&", BINOP_AND, PREC_AND, 0},
1413 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1414 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1415 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1416 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1417 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1418 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1419 {">=", BINOP_GEQ, PREC_ORDER, 0},
1420 {">", BINOP_GTR, PREC_ORDER, 0},
1421 {"<", BINOP_LESS, PREC_ORDER, 0},
1422 {">>", BINOP_RSH, PREC_SHIFT, 0},
1423 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1424 {"+", BINOP_ADD, PREC_ADD, 0},
1425 {"-", BINOP_SUB, PREC_ADD, 0},
1426 {"*", BINOP_MUL, PREC_MUL, 0},
1427 {"/", BINOP_DIV, PREC_MUL, 0},
1428 {"%", BINOP_REM, PREC_MUL, 0},
1429 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1430 {"-", UNOP_NEG, PREC_PREFIX, 0},
1431 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1432 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1433 {"*", UNOP_IND, PREC_PREFIX, 0},
1434 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1435 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1436 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1437 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1439 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1442 /* These variables point to the objects
1443 representing the predefined C data types. */
1445 struct type *builtin_type_void;
1446 struct type *builtin_type_char;
1447 struct type *builtin_type_short;
1448 struct type *builtin_type_int;
1449 struct type *builtin_type_long;
1450 struct type *builtin_type_long_long;
1451 struct type *builtin_type_unsigned_char;
1452 struct type *builtin_type_unsigned_short;
1453 struct type *builtin_type_unsigned_int;
1454 struct type *builtin_type_unsigned_long;
1455 struct type *builtin_type_unsigned_long_long;
1456 struct type *builtin_type_float;
1457 struct type *builtin_type_double;
1458 struct type *builtin_type_long_double;
1459 struct type *builtin_type_complex;
1460 struct type *builtin_type_double_complex;
1462 struct type ** const (c_builtin_types[]) =
1466 &builtin_type_short,
1468 &builtin_type_float,
1469 &builtin_type_double,
1471 &builtin_type_long_long,
1472 &builtin_type_unsigned_char,
1473 &builtin_type_unsigned_short,
1474 &builtin_type_unsigned_int,
1475 &builtin_type_unsigned_long,
1476 &builtin_type_unsigned_long_long,
1477 &builtin_type_long_double,
1478 &builtin_type_complex,
1479 &builtin_type_double_complex,
1483 /* FIXME: Eventually do a separate defintion for C++. */
1485 const struct language_defn c_language_defn = {
1486 "c", /* Language name */
1493 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1494 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1495 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1496 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1497 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1498 c_op_print_tab, /* expression operators for printing */
1503 _initialize_c_exp ()
1506 init_type (TYPE_CODE_VOID, 1, 0,
1509 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, 0,
1511 builtin_type_unsigned_char =
1512 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, 1,
1514 builtin_type_short =
1515 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, 0,
1517 builtin_type_unsigned_short =
1518 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, 1,
1521 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 0,
1523 builtin_type_unsigned_int =
1524 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 1,
1527 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, 0,
1529 builtin_type_unsigned_long =
1530 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, 1,
1532 builtin_type_long_long =
1533 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, 0,
1535 builtin_type_unsigned_long_long =
1536 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, 1,
1537 "unsigned long long");
1538 builtin_type_float =
1539 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT, 0,
1541 builtin_type_double =
1542 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT, 0,
1544 builtin_type_long_double =
1545 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT, 0,
1547 builtin_type_complex =
1548 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT, 0,
1550 builtin_type_double_complex =
1551 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT, 0,
1554 add_language (&c_language_defn);
1555 set_language (language_c); /* Make C the default language */