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); }
774 | '(' nonempty_typelist ')'
775 { free ($2); $$ = 0; }
779 | typebase COLONCOLON '*'
780 { $$ = lookup_member_type (builtin_type_int, $1); }
781 | type '(' typebase COLONCOLON '*' ')'
782 { $$ = lookup_member_type ($1, $3); }
783 | type '(' typebase COLONCOLON '*' ')' '(' ')'
784 { $$ = lookup_member_type
785 (lookup_function_type ($1), $3); }
786 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
787 { $$ = lookup_member_type
788 (lookup_function_type ($1), $3);
796 { $$ = builtin_type_int; }
798 { $$ = builtin_type_long; }
800 { $$ = builtin_type_short; }
802 { $$ = builtin_type_long; }
803 | UNSIGNED LONG INT_KEYWORD
804 { $$ = builtin_type_unsigned_long; }
806 { $$ = builtin_type_long_long; }
807 | LONG LONG INT_KEYWORD
808 { $$ = builtin_type_long_long; }
810 { $$ = builtin_type_unsigned_long_long; }
811 | UNSIGNED LONG LONG INT_KEYWORD
812 { $$ = builtin_type_unsigned_long_long; }
814 { $$ = builtin_type_short; }
815 | UNSIGNED SHORT INT_KEYWORD
816 { $$ = builtin_type_unsigned_short; }
818 { $$ = lookup_struct (copy_name ($2),
819 expression_context_block); }
821 { $$ = lookup_union (copy_name ($2),
822 expression_context_block); }
824 { $$ = lookup_enum (copy_name ($2),
825 expression_context_block); }
827 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
829 { $$ = builtin_type_unsigned_int; }
833 { $$ = builtin_type_int; }
839 $$.stoken.ptr = "int";
840 $$.stoken.length = 3;
841 $$.type = builtin_type_int;
845 $$.stoken.ptr = "long";
846 $$.stoken.length = 4;
847 $$.type = builtin_type_long;
851 $$.stoken.ptr = "short";
852 $$.stoken.length = 5;
853 $$.type = builtin_type_short;
859 { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
860 $$[0] = (struct type *)0;
863 | nonempty_typelist ',' type
864 { int len = sizeof (struct type *) * ++($<ivec>1[0]);
865 $$ = (struct type **)xrealloc ($1, len);
866 $$[$<ivec>$[0]] = $3;
870 name : NAME { $$ = $1.stoken; }
871 | BLOCKNAME { $$ = $1.stoken; }
872 | TYPENAME { $$ = $1.stoken; }
873 | NAME_OR_INT { $$ = $1.stoken; }
874 | NAME_OR_UINT { $$ = $1.stoken; }
877 name_not_typename : NAME
879 /* These would be useful if name_not_typename was useful, but it is just
880 a fake for "variable", so these cause reduce/reduce conflicts because
881 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
882 =exp) or just an exp. If name_not_typename was ever used in an lvalue
883 context where only a name could occur, this might be useful.
891 /* Take care of parsing a number (anything that starts with a digit).
892 Set yylval and return the token type; update lexptr.
893 LEN is the number of characters in it. */
895 /*** Needs some error checking for the float case ***/
898 parse_number (p, len, parsed_float, putithere)
904 register LONGEST n = 0;
905 register LONGEST prevn = 0;
908 register int base = input_radix;
911 extern double atof ();
915 /* It's a float since it contains a point or an exponent. */
916 putithere->dval = atof (p);
920 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
954 if (c >= 'A' && c <= 'Z')
956 if (c != 'l' && c != 'u')
958 if (c >= '0' && c <= '9')
962 if (base > 10 && c >= 'a' && c <= 'f')
963 n += i = c - 'a' + 10;
964 else if (len == 0 && c == 'l')
966 else if (len == 0 && c == 'u')
969 return ERROR; /* Char not a digit */
972 return ERROR; /* Invalid digit in this base */
973 if(!unsigned_p && (prevn >= n))
974 unsigned_p=1; /* Try something unsigned */
975 /* Don't do the range check if n==i and i==0, since that special
976 case will give an overflow error. */
977 if(RANGE_CHECK && n!=0)
979 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
980 range_error("Overflow on numeric constant.");
987 putithere->ulval = n;
1001 enum exp_opcode opcode;
1004 const static struct token tokentab3[] =
1006 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1007 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1010 const static struct token tokentab2[] =
1012 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1013 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1014 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1015 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1016 {"%=", ASSIGN_MODIFY, BINOP_REM},
1017 {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
1018 {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
1019 {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
1020 {"++", INCREMENT, BINOP_END},
1021 {"--", DECREMENT, BINOP_END},
1022 {"->", ARROW, BINOP_END},
1023 {"&&", AND, BINOP_END},
1024 {"||", OR, BINOP_END},
1025 {"::", COLONCOLON, BINOP_END},
1026 {"<<", LSH, BINOP_END},
1027 {">>", RSH, BINOP_END},
1028 {"==", EQUAL, BINOP_END},
1029 {"!=", NOTEQUAL, BINOP_END},
1030 {"<=", LEQ, BINOP_END},
1031 {">=", GEQ, BINOP_END}
1034 /* Read one token, getting characters through lexptr. */
1040 register int namelen;
1041 register unsigned i;
1042 register char *tokstart;
1047 /* See if it is a special token of length 3. */
1048 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1049 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1052 yylval.opcode = tokentab3[i].opcode;
1053 return tokentab3[i].token;
1056 /* See if it is a special token of length 2. */
1057 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1058 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1061 yylval.opcode = tokentab2[i].opcode;
1062 return tokentab2[i].token;
1065 switch (c = *tokstart)
1080 c = parse_escape (&lexptr);
1084 error ("Invalid character constant.");
1093 if (paren_depth == 0)
1100 if (comma_terminates && paren_depth == 0)
1106 /* Might be a floating point number. */
1107 if (lexptr[1] < '0' || lexptr[1] > '9')
1108 goto symbol; /* Nope, must be a symbol. */
1109 /* FALL THRU into number case. */
1122 /* It's a number. */
1123 int got_dot = 0, got_e = 0, toktype;
1124 register char *p = tokstart;
1125 int hex = input_radix > 10;
1127 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1132 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1140 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1141 got_dot = got_e = 1;
1142 else if (!hex && !got_dot && *p == '.')
1144 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1145 && (*p == '-' || *p == '+'))
1146 /* This is the sign of the exponent, not the end of the
1149 /* We will take any letters or digits. parse_number will
1150 complain if past the radix, or if L or U are not final. */
1151 else if ((*p < '0' || *p > '9')
1152 && ((*p < 'a' || *p > 'z')
1153 && (*p < 'A' || *p > 'Z')))
1156 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1157 if (toktype == ERROR)
1159 char *err_copy = (char *) alloca (p - tokstart + 1);
1161 bcopy (tokstart, err_copy, p - tokstart);
1162 err_copy[p - tokstart] = 0;
1163 error ("Invalid number \"%s\".", err_copy);
1194 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1197 c = tokstart[++namelen];
1198 if (c >= '0' && c <= '9')
1200 c = tokstart[++namelen];
1201 if (c >= '0' && c <= '9')
1202 c = tokstart[++namelen];
1205 yylval.sval.ptr = tokstart + 1;
1206 yylval.sval.length = namelen - 1;
1207 lexptr += namelen + 1;
1211 if (!(c == '_' || c == '$'
1212 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1213 /* We must have come across a bad character (e.g. ';'). */
1214 error ("Invalid character '%c' in expression.", c);
1216 /* It's a name. See how long it is. */
1218 for (c = tokstart[namelen];
1219 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1220 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1221 c = tokstart[++namelen])
1224 /* The token "if" terminates the expression and is NOT
1225 removed from the input stream. */
1226 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1233 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1234 and $$digits (equivalent to $<-digits> if you could type that).
1235 Make token type LAST, and put the number (the digits) in yylval. */
1237 if (*tokstart == '$')
1239 register int negate = 0;
1241 /* Double dollar means negate the number and add -1 as well.
1242 Thus $$ alone means -1. */
1243 if (namelen >= 2 && tokstart[1] == '$')
1250 /* Just dollars (one or two) */
1251 yylval.lval = - negate;
1254 /* Is the rest of the token digits? */
1255 for (; c < namelen; c++)
1256 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1260 yylval.lval = atoi (tokstart + 1 + negate);
1262 yylval.lval = - yylval.lval;
1267 /* Handle tokens that refer to machine registers:
1268 $ followed by a register name. */
1270 if (*tokstart == '$') {
1271 for (c = 0; c < NUM_REGS; c++)
1272 if (namelen - 1 == strlen (reg_names[c])
1273 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1278 for (c = 0; c < num_std_regs; c++)
1279 if (namelen - 1 == strlen (std_regs[c].name)
1280 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1282 yylval.lval = std_regs[c].regnum;
1286 /* Catch specific keywords. Should be done with a data structure. */
1290 if (!strncmp (tokstart, "unsigned", 8))
1294 if (!strncmp (tokstart, "struct", 6))
1296 if (!strncmp (tokstart, "signed", 6))
1298 if (!strncmp (tokstart, "sizeof", 6))
1302 if (!strncmp (tokstart, "union", 5))
1304 if (!strncmp (tokstart, "short", 5))
1308 if (!strncmp (tokstart, "enum", 4))
1310 if (!strncmp (tokstart, "long", 4))
1312 if (!strncmp (tokstart, "this", 4))
1314 static const char this_name[] =
1315 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1317 if (lookup_symbol (this_name, expression_context_block,
1318 VAR_NAMESPACE, 0, NULL))
1323 if (!strncmp (tokstart, "int", 3))
1330 yylval.sval.ptr = tokstart;
1331 yylval.sval.length = namelen;
1333 /* Any other names starting in $ are debugger internal variables. */
1335 if (*tokstart == '$')
1337 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1341 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1342 functions or symtabs. If this is not so, then ...
1343 Use token-type TYPENAME for symbols that happen to be defined
1344 currently as names of types; NAME for other symbols.
1345 The caller is not constrained to care about the distinction. */
1347 char *tmp = copy_name (yylval.sval);
1349 int is_a_field_of_this = 0;
1352 sym = lookup_symbol (tmp, expression_context_block,
1354 current_language->la_language == language_cplus
1355 ? &is_a_field_of_this : NULL,
1357 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1358 lookup_partial_symtab (tmp))
1360 yylval.ssym.sym = sym;
1361 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1364 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1366 yylval.tsym.type = SYMBOL_TYPE (sym);
1369 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1372 /* Input names that aren't symbols but ARE valid hex numbers,
1373 when the input radix permits them, can be names or numbers
1374 depending on the parse. Note we support radixes > 16 here. */
1376 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1377 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1379 YYSTYPE newlval; /* Its value is ignored. */
1380 hextype = parse_number (tokstart, namelen, 0, &newlval);
1383 yylval.ssym.sym = sym;
1384 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1387 if (hextype == UINT)
1389 yylval.ssym.sym = sym;
1390 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1391 return NAME_OR_UINT;
1395 /* Any other kind of symbol */
1396 yylval.ssym.sym = sym;
1397 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1406 error (msg ? msg : "Invalid syntax in expression.");
1409 /* Table mapping opcodes into strings for printing operators
1410 and precedences of the operators. */
1412 const static struct op_print c_op_print_tab[] =
1414 {",", BINOP_COMMA, PREC_COMMA, 0},
1415 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1416 {"||", BINOP_OR, PREC_OR, 0},
1417 {"&&", BINOP_AND, PREC_AND, 0},
1418 {"|", BINOP_LOGIOR, PREC_LOGIOR, 0},
1419 {"&", BINOP_LOGAND, PREC_LOGAND, 0},
1420 {"^", BINOP_LOGXOR, PREC_LOGXOR, 0},
1421 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1422 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1423 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1424 {">=", BINOP_GEQ, PREC_ORDER, 0},
1425 {">", BINOP_GTR, PREC_ORDER, 0},
1426 {"<", BINOP_LESS, PREC_ORDER, 0},
1427 {">>", BINOP_RSH, PREC_SHIFT, 0},
1428 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1429 {"+", BINOP_ADD, PREC_ADD, 0},
1430 {"-", BINOP_SUB, PREC_ADD, 0},
1431 {"*", BINOP_MUL, PREC_MUL, 0},
1432 {"/", BINOP_DIV, PREC_MUL, 0},
1433 {"%", BINOP_REM, PREC_MUL, 0},
1434 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1435 {"-", UNOP_NEG, PREC_PREFIX, 0},
1436 {"!", UNOP_ZEROP, PREC_PREFIX, 0},
1437 {"~", UNOP_LOGNOT, PREC_PREFIX, 0},
1438 {"*", UNOP_IND, PREC_PREFIX, 0},
1439 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1440 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1441 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1442 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1444 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1447 /* These variables point to the objects
1448 representing the predefined C data types. */
1450 struct type *builtin_type_void;
1451 struct type *builtin_type_char;
1452 struct type *builtin_type_short;
1453 struct type *builtin_type_int;
1454 struct type *builtin_type_long;
1455 struct type *builtin_type_long_long;
1456 struct type *builtin_type_unsigned_char;
1457 struct type *builtin_type_unsigned_short;
1458 struct type *builtin_type_unsigned_int;
1459 struct type *builtin_type_unsigned_long;
1460 struct type *builtin_type_unsigned_long_long;
1461 struct type *builtin_type_float;
1462 struct type *builtin_type_double;
1463 struct type *builtin_type_long_double;
1464 struct type *builtin_type_complex;
1465 struct type *builtin_type_double_complex;
1467 struct type ** const (c_builtin_types[]) =
1471 &builtin_type_short,
1473 &builtin_type_float,
1474 &builtin_type_double,
1476 &builtin_type_long_long,
1477 &builtin_type_unsigned_char,
1478 &builtin_type_unsigned_short,
1479 &builtin_type_unsigned_int,
1480 &builtin_type_unsigned_long,
1481 &builtin_type_unsigned_long_long,
1482 &builtin_type_long_double,
1483 &builtin_type_complex,
1484 &builtin_type_double_complex,
1488 const struct language_defn c_language_defn = {
1489 "c", /* Language name */
1496 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1497 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1498 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1499 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1500 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1501 c_op_print_tab, /* expression operators for printing */
1505 const struct language_defn cplus_language_defn = {
1506 "c++", /* Language name */
1513 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1514 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1515 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1516 "0x%x", "0x%", "x", /* Hex format, prefix, suffix */
1517 "0%o", "0%", "o", /* Octal format, prefix, suffix */
1518 c_op_print_tab, /* expression operators for printing */
1523 _initialize_c_exp ()
1526 init_type (TYPE_CODE_VOID, 1, 0,
1529 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, 0,
1531 builtin_type_unsigned_char =
1532 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT, 1,
1534 builtin_type_short =
1535 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, 0,
1537 builtin_type_unsigned_short =
1538 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT, 1,
1541 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 0,
1543 builtin_type_unsigned_int =
1544 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT, 1,
1547 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, 0,
1549 builtin_type_unsigned_long =
1550 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT, 1,
1552 builtin_type_long_long =
1553 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, 0,
1555 builtin_type_unsigned_long_long =
1556 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT, 1,
1557 "unsigned long long");
1558 builtin_type_float =
1559 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT, 0,
1561 builtin_type_double =
1562 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT, 0,
1564 builtin_type_long_double =
1565 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT, 0,
1567 builtin_type_complex =
1568 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT, 0,
1570 builtin_type_double_complex =
1571 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT, 0,
1574 add_language (&c_language_defn);
1575 add_language (&cplus_language_defn);