* defs.h (sevenbit_strings): Add declaration.
[binutils-gdb.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
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.
28
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator. */
36
37 %{
38
39 #include <stdio.h>
40 #include <string.h>
41 #include "defs.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "frame.h"
45 #include "expression.h"
46 #include "parser-defs.h"
47 #include "value.h"
48 #include "language.h"
49 #include "bfd.h"
50 #include "symfile.h"
51 #include "objfiles.h"
52
53 /* These MUST be included in any grammar file!!!! Please choose unique names!
54 Note that this are a combined list of variables that can be produced
55 by any one of bison, byacc, or yacc. */
56 #define yymaxdepth c_maxdepth
57 #define yyparse c_parse
58 #define yylex c_lex
59 #define yyerror c_error
60 #define yylval c_lval
61 #define yychar c_char
62 #define yydebug c_debug
63 #define yypact c_pact
64 #define yyr1 c_r1
65 #define yyr2 c_r2
66 #define yydef c_def
67 #define yychk c_chk
68 #define yypgo c_pgo
69 #define yyact c_act
70 #define yyexca c_exca
71 #define yyerrflag c_errflag
72 #define yynerrs c_nerrs
73 #define yyps c_ps
74 #define yypv c_pv
75 #define yys c_s
76 #define yy_yys c_yys
77 #define yystate c_state
78 #define yytmp c_tmp
79 #define yyv c_v
80 #define yy_yyv c_yyv
81 #define yyval c_val
82 #define yylloc c_lloc
83 #define yyss c_yyss /* byacc */
84 #define yyssp c_yysp /* byacc */
85 #define yyvs c_yyvs /* byacc */
86 #define yyvsp c_yyvsp /* byacc */
87
88 int
89 yyparse PARAMS ((void));
90
91 int
92 yylex PARAMS ((void));
93
94 void
95 yyerror PARAMS ((char *));
96
97 /* #define YYDEBUG 1 */
98
99 %}
100
101 /* Although the yacc "value" of an expression is not used,
102 since the result is stored in the structure being created,
103 other node types do have values. */
104
105 %union
106 {
107 LONGEST lval;
108 unsigned LONGEST ulval;
109 struct {
110 LONGEST val;
111 struct type *type;
112 } typed_val;
113 double dval;
114 struct symbol *sym;
115 struct type *tval;
116 struct stoken sval;
117 struct ttype tsym;
118 struct symtoken ssym;
119 int voidval;
120 struct block *bval;
121 enum exp_opcode opcode;
122 struct internalvar *ivar;
123
124 struct type **tvec;
125 int *ivec;
126 }
127
128 %{
129 /* YYSTYPE gets defined by %union */
130 static int
131 parse_number PARAMS ((char *, int, int, YYSTYPE *));
132 %}
133
134 %type <voidval> exp exp1 type_exp start variable qualified_name
135 %type <tval> type typebase
136 %type <tvec> nonempty_typelist
137 /* %type <bval> block */
138
139 /* Fancy type parsing. */
140 %type <voidval> func_mod direct_abs_decl abs_decl
141 %type <tval> ptype
142 %type <lval> array_mod
143
144 %token <typed_val> INT
145 %token <dval> FLOAT
146
147 /* Both NAME and TYPENAME tokens represent symbols in the input,
148 and both convey their data as strings.
149 But a TYPENAME is a string that happens to be defined as a typedef
150 or builtin type name (such as int or char)
151 and a NAME is any other symbol.
152 Contexts where this distinction is not important can use the
153 nonterminal "name", which matches either NAME or TYPENAME. */
154
155 %token <sval> STRING
156 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
157 %token <tsym> TYPENAME
158 %type <sval> name
159 %type <ssym> name_not_typename
160 %type <tsym> typename
161
162 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
163 but which would parse as a valid number in the current input radix.
164 E.g. "c" when input_radix==16. Depending on the parse, it will be
165 turned into a name or into a number. */
166
167 %token <ssym> NAME_OR_INT
168
169 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
170 %token TEMPLATE
171 %token ERROR
172
173 /* Special type cases, put in to allow the parser to distinguish different
174 legal basetypes. */
175 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD
176 %token <lval> LAST REGNAME
177
178 %token <ivar> VARIABLE
179
180 %token <opcode> ASSIGN_MODIFY
181
182 /* C++ */
183 %token THIS
184
185 %left ','
186 %left ABOVE_COMMA
187 %right '=' ASSIGN_MODIFY
188 %right '?'
189 %left OROR
190 %left ANDAND
191 %left '|'
192 %left '^'
193 %left '&'
194 %left EQUAL NOTEQUAL
195 %left '<' '>' LEQ GEQ
196 %left LSH RSH
197 %left '@'
198 %left '+' '-'
199 %left '*' '/' '%'
200 %right UNARY INCREMENT DECREMENT
201 %right ARROW '.' '[' '('
202 %token <ssym> BLOCKNAME
203 %type <bval> block
204 %left COLONCOLON
205
206 \f
207 %%
208
209 start : exp1
210 | type_exp
211 ;
212
213 type_exp: type
214 { write_exp_elt_opcode(OP_TYPE);
215 write_exp_elt_type($1);
216 write_exp_elt_opcode(OP_TYPE);}
217 ;
218
219 /* Expressions, including the comma operator. */
220 exp1 : exp
221 | exp1 ',' exp
222 { write_exp_elt_opcode (BINOP_COMMA); }
223 ;
224
225 /* Expressions, not including the comma operator. */
226 exp : '*' exp %prec UNARY
227 { write_exp_elt_opcode (UNOP_IND); }
228
229 exp : '&' exp %prec UNARY
230 { write_exp_elt_opcode (UNOP_ADDR); }
231
232 exp : '-' exp %prec UNARY
233 { write_exp_elt_opcode (UNOP_NEG); }
234 ;
235
236 exp : '!' exp %prec UNARY
237 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
238 ;
239
240 exp : '~' exp %prec UNARY
241 { write_exp_elt_opcode (UNOP_COMPLEMENT); }
242 ;
243
244 exp : INCREMENT exp %prec UNARY
245 { write_exp_elt_opcode (UNOP_PREINCREMENT); }
246 ;
247
248 exp : DECREMENT exp %prec UNARY
249 { write_exp_elt_opcode (UNOP_PREDECREMENT); }
250 ;
251
252 exp : exp INCREMENT %prec UNARY
253 { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
254 ;
255
256 exp : exp DECREMENT %prec UNARY
257 { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
258 ;
259
260 exp : SIZEOF exp %prec UNARY
261 { write_exp_elt_opcode (UNOP_SIZEOF); }
262 ;
263
264 exp : exp ARROW name
265 { write_exp_elt_opcode (STRUCTOP_PTR);
266 write_exp_string ($3);
267 write_exp_elt_opcode (STRUCTOP_PTR); }
268 ;
269
270 exp : exp ARROW qualified_name
271 { /* exp->type::name becomes exp->*(&type::name) */
272 /* Note: this doesn't work if name is a
273 static member! FIXME */
274 write_exp_elt_opcode (UNOP_ADDR);
275 write_exp_elt_opcode (STRUCTOP_MPTR); }
276 ;
277 exp : exp ARROW '*' exp
278 { write_exp_elt_opcode (STRUCTOP_MPTR); }
279 ;
280
281 exp : exp '.' name
282 { write_exp_elt_opcode (STRUCTOP_STRUCT);
283 write_exp_string ($3);
284 write_exp_elt_opcode (STRUCTOP_STRUCT); }
285 ;
286
287 exp : exp '.' qualified_name
288 { /* exp.type::name becomes exp.*(&type::name) */
289 /* Note: this doesn't work if name is a
290 static member! FIXME */
291 write_exp_elt_opcode (UNOP_ADDR);
292 write_exp_elt_opcode (STRUCTOP_MEMBER); }
293 ;
294
295 exp : exp '.' '*' exp
296 { write_exp_elt_opcode (STRUCTOP_MEMBER); }
297 ;
298
299 exp : exp '[' exp1 ']'
300 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
301 ;
302
303 exp : exp '('
304 /* This is to save the value of arglist_len
305 being accumulated by an outer function call. */
306 { start_arglist (); }
307 arglist ')' %prec ARROW
308 { write_exp_elt_opcode (OP_FUNCALL);
309 write_exp_elt_longcst ((LONGEST) end_arglist ());
310 write_exp_elt_opcode (OP_FUNCALL); }
311 ;
312
313 arglist :
314 ;
315
316 arglist : exp
317 { arglist_len = 1; }
318 ;
319
320 arglist : arglist ',' exp %prec ABOVE_COMMA
321 { arglist_len++; }
322 ;
323
324 exp : '{' type '}' exp %prec UNARY
325 { write_exp_elt_opcode (UNOP_MEMVAL);
326 write_exp_elt_type ($2);
327 write_exp_elt_opcode (UNOP_MEMVAL); }
328 ;
329
330 exp : '(' type ')' exp %prec UNARY
331 { write_exp_elt_opcode (UNOP_CAST);
332 write_exp_elt_type ($2);
333 write_exp_elt_opcode (UNOP_CAST); }
334 ;
335
336 exp : '(' exp1 ')'
337 { }
338 ;
339
340 /* Binary operators in order of decreasing precedence. */
341
342 exp : exp '@' exp
343 { write_exp_elt_opcode (BINOP_REPEAT); }
344 ;
345
346 exp : exp '*' exp
347 { write_exp_elt_opcode (BINOP_MUL); }
348 ;
349
350 exp : exp '/' exp
351 { write_exp_elt_opcode (BINOP_DIV); }
352 ;
353
354 exp : exp '%' exp
355 { write_exp_elt_opcode (BINOP_REM); }
356 ;
357
358 exp : exp '+' exp
359 { write_exp_elt_opcode (BINOP_ADD); }
360 ;
361
362 exp : exp '-' exp
363 { write_exp_elt_opcode (BINOP_SUB); }
364 ;
365
366 exp : exp LSH exp
367 { write_exp_elt_opcode (BINOP_LSH); }
368 ;
369
370 exp : exp RSH exp
371 { write_exp_elt_opcode (BINOP_RSH); }
372 ;
373
374 exp : exp EQUAL exp
375 { write_exp_elt_opcode (BINOP_EQUAL); }
376 ;
377
378 exp : exp NOTEQUAL exp
379 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
380 ;
381
382 exp : exp LEQ exp
383 { write_exp_elt_opcode (BINOP_LEQ); }
384 ;
385
386 exp : exp GEQ exp
387 { write_exp_elt_opcode (BINOP_GEQ); }
388 ;
389
390 exp : exp '<' exp
391 { write_exp_elt_opcode (BINOP_LESS); }
392 ;
393
394 exp : exp '>' exp
395 { write_exp_elt_opcode (BINOP_GTR); }
396 ;
397
398 exp : exp '&' exp
399 { write_exp_elt_opcode (BINOP_BITWISE_AND); }
400 ;
401
402 exp : exp '^' exp
403 { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
404 ;
405
406 exp : exp '|' exp
407 { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
408 ;
409
410 exp : exp ANDAND exp
411 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
412 ;
413
414 exp : exp OROR exp
415 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
416 ;
417
418 exp : exp '?' exp ':' exp %prec '?'
419 { write_exp_elt_opcode (TERNOP_COND); }
420 ;
421
422 exp : exp '=' exp
423 { write_exp_elt_opcode (BINOP_ASSIGN); }
424 ;
425
426 exp : exp ASSIGN_MODIFY exp
427 { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
428 write_exp_elt_opcode ($2);
429 write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
430 ;
431
432 exp : INT
433 { write_exp_elt_opcode (OP_LONG);
434 write_exp_elt_type ($1.type);
435 write_exp_elt_longcst ((LONGEST)($1.val));
436 write_exp_elt_opcode (OP_LONG); }
437 ;
438
439 exp : NAME_OR_INT
440 { YYSTYPE val;
441 parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
442 write_exp_elt_opcode (OP_LONG);
443 write_exp_elt_type (val.typed_val.type);
444 write_exp_elt_longcst ((LONGEST)val.typed_val.val);
445 write_exp_elt_opcode (OP_LONG);
446 }
447 ;
448
449
450 exp : FLOAT
451 { write_exp_elt_opcode (OP_DOUBLE);
452 write_exp_elt_type (builtin_type_double);
453 write_exp_elt_dblcst ($1);
454 write_exp_elt_opcode (OP_DOUBLE); }
455 ;
456
457 exp : variable
458 ;
459
460 exp : LAST
461 { write_exp_elt_opcode (OP_LAST);
462 write_exp_elt_longcst ((LONGEST) $1);
463 write_exp_elt_opcode (OP_LAST); }
464 ;
465
466 exp : REGNAME
467 { write_exp_elt_opcode (OP_REGISTER);
468 write_exp_elt_longcst ((LONGEST) $1);
469 write_exp_elt_opcode (OP_REGISTER); }
470 ;
471
472 exp : VARIABLE
473 { write_exp_elt_opcode (OP_INTERNALVAR);
474 write_exp_elt_intern ($1);
475 write_exp_elt_opcode (OP_INTERNALVAR); }
476 ;
477
478 exp : SIZEOF '(' type ')' %prec UNARY
479 { write_exp_elt_opcode (OP_LONG);
480 write_exp_elt_type (builtin_type_int);
481 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
482 write_exp_elt_opcode (OP_LONG); }
483 ;
484
485 exp : STRING
486 { write_exp_elt_opcode (OP_STRING);
487 write_exp_string ($1);
488 write_exp_elt_opcode (OP_STRING); }
489 ;
490
491 /* C++. */
492 exp : THIS
493 { write_exp_elt_opcode (OP_THIS);
494 write_exp_elt_opcode (OP_THIS); }
495 ;
496
497 /* end of C++. */
498
499 block : BLOCKNAME
500 {
501 if ($1.sym != 0)
502 $$ = SYMBOL_BLOCK_VALUE ($1.sym);
503 else
504 {
505 struct symtab *tem =
506 lookup_symtab (copy_name ($1.stoken));
507 if (tem)
508 $$ = BLOCKVECTOR_BLOCK
509 (BLOCKVECTOR (tem), STATIC_BLOCK);
510 else
511 error ("No file or function \"%s\".",
512 copy_name ($1.stoken));
513 }
514 }
515 ;
516
517 block : block COLONCOLON name
518 { struct symbol *tem
519 = lookup_symbol (copy_name ($3), $1,
520 VAR_NAMESPACE, 0, NULL);
521 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
522 error ("No function \"%s\" in specified context.",
523 copy_name ($3));
524 $$ = SYMBOL_BLOCK_VALUE (tem); }
525 ;
526
527 variable: block COLONCOLON name
528 { struct symbol *sym;
529 sym = lookup_symbol (copy_name ($3), $1,
530 VAR_NAMESPACE, 0, NULL);
531 if (sym == 0)
532 error ("No symbol \"%s\" in specified context.",
533 copy_name ($3));
534
535 write_exp_elt_opcode (OP_VAR_VALUE);
536 write_exp_elt_sym (sym);
537 write_exp_elt_opcode (OP_VAR_VALUE); }
538 ;
539
540 qualified_name: typebase COLONCOLON name
541 {
542 struct type *type = $1;
543 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
544 && TYPE_CODE (type) != TYPE_CODE_UNION)
545 error ("`%s' is not defined as an aggregate type.",
546 TYPE_NAME (type));
547
548 write_exp_elt_opcode (OP_SCOPE);
549 write_exp_elt_type (type);
550 write_exp_string ($3);
551 write_exp_elt_opcode (OP_SCOPE);
552 }
553 | typebase COLONCOLON '~' name
554 {
555 struct type *type = $1;
556 struct stoken tmp_token;
557 if (TYPE_CODE (type) != TYPE_CODE_STRUCT
558 && TYPE_CODE (type) != TYPE_CODE_UNION)
559 error ("`%s' is not defined as an aggregate type.",
560 TYPE_NAME (type));
561
562 if (strcmp (type_name_no_tag (type), $4.ptr))
563 error ("invalid destructor `%s::~%s'",
564 type_name_no_tag (type), $4.ptr);
565
566 tmp_token.ptr = (char*) alloca ($4.length + 2);
567 tmp_token.length = $4.length + 1;
568 tmp_token.ptr[0] = '~';
569 memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
570 tmp_token.ptr[tmp_token.length] = 0;
571 write_exp_elt_opcode (OP_SCOPE);
572 write_exp_elt_type (type);
573 write_exp_string (tmp_token);
574 write_exp_elt_opcode (OP_SCOPE);
575 }
576 ;
577
578 variable: qualified_name
579 | COLONCOLON name
580 {
581 char *name = copy_name ($2);
582 struct symbol *sym;
583 struct minimal_symbol *msymbol;
584
585 sym =
586 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
587 if (sym)
588 {
589 write_exp_elt_opcode (OP_VAR_VALUE);
590 write_exp_elt_sym (sym);
591 write_exp_elt_opcode (OP_VAR_VALUE);
592 break;
593 }
594
595 msymbol = lookup_minimal_symbol (name,
596 (struct objfile *) NULL);
597 if (msymbol != NULL)
598 {
599 write_exp_elt_opcode (OP_LONG);
600 write_exp_elt_type (builtin_type_int);
601 write_exp_elt_longcst ((LONGEST) msymbol -> address);
602 write_exp_elt_opcode (OP_LONG);
603 write_exp_elt_opcode (UNOP_MEMVAL);
604 if (msymbol -> type == mst_data ||
605 msymbol -> type == mst_bss)
606 write_exp_elt_type (builtin_type_int);
607 else if (msymbol -> type == mst_text)
608 write_exp_elt_type (lookup_function_type (builtin_type_int));
609 else
610 write_exp_elt_type (builtin_type_char);
611 write_exp_elt_opcode (UNOP_MEMVAL);
612 }
613 else
614 if (!have_full_symbols () && !have_partial_symbols ())
615 error ("No symbol table is loaded. Use the \"file\" command.");
616 else
617 error ("No symbol \"%s\" in current context.", name);
618 }
619 ;
620
621 variable: name_not_typename
622 { struct symbol *sym = $1.sym;
623
624 if (sym)
625 {
626 switch (SYMBOL_CLASS (sym))
627 {
628 case LOC_REGISTER:
629 case LOC_ARG:
630 case LOC_REF_ARG:
631 case LOC_REGPARM:
632 case LOC_LOCAL:
633 case LOC_LOCAL_ARG:
634 if (innermost_block == 0 ||
635 contained_in (block_found,
636 innermost_block))
637 innermost_block = block_found;
638 case LOC_UNDEF:
639 case LOC_CONST:
640 case LOC_STATIC:
641 case LOC_TYPEDEF:
642 case LOC_LABEL:
643 case LOC_BLOCK:
644 case LOC_CONST_BYTES:
645
646 /* In this case the expression can
647 be evaluated regardless of what
648 frame we are in, so there is no
649 need to check for the
650 innermost_block. These cases are
651 listed so that gcc -Wall will
652 report types that may not have
653 been considered. */
654
655 break;
656 }
657 write_exp_elt_opcode (OP_VAR_VALUE);
658 write_exp_elt_sym (sym);
659 write_exp_elt_opcode (OP_VAR_VALUE);
660 }
661 else if ($1.is_a_field_of_this)
662 {
663 /* C++: it hangs off of `this'. Must
664 not inadvertently convert from a method call
665 to data ref. */
666 if (innermost_block == 0 ||
667 contained_in (block_found, innermost_block))
668 innermost_block = block_found;
669 write_exp_elt_opcode (OP_THIS);
670 write_exp_elt_opcode (OP_THIS);
671 write_exp_elt_opcode (STRUCTOP_PTR);
672 write_exp_string ($1.stoken);
673 write_exp_elt_opcode (STRUCTOP_PTR);
674 }
675 else
676 {
677 struct minimal_symbol *msymbol;
678 register char *arg = copy_name ($1.stoken);
679
680 msymbol = lookup_minimal_symbol (arg,
681 (struct objfile *) NULL);
682 if (msymbol != NULL)
683 {
684 write_exp_elt_opcode (OP_LONG);
685 write_exp_elt_type (builtin_type_int);
686 write_exp_elt_longcst ((LONGEST) msymbol -> address);
687 write_exp_elt_opcode (OP_LONG);
688 write_exp_elt_opcode (UNOP_MEMVAL);
689 if (msymbol -> type == mst_data ||
690 msymbol -> type == mst_bss)
691 write_exp_elt_type (builtin_type_int);
692 else if (msymbol -> type == mst_text)
693 write_exp_elt_type (lookup_function_type (builtin_type_int));
694 else
695 write_exp_elt_type (builtin_type_char);
696 write_exp_elt_opcode (UNOP_MEMVAL);
697 }
698 else if (!have_full_symbols () && !have_partial_symbols ())
699 error ("No symbol table is loaded. Use the \"file\" command.");
700 else
701 error ("No symbol \"%s\" in current context.",
702 copy_name ($1.stoken));
703 }
704 }
705 ;
706
707
708 ptype : typebase
709 | typebase abs_decl
710 {
711 /* This is where the interesting stuff happens. */
712 int done = 0;
713 int array_size;
714 struct type *follow_type = $1;
715
716 while (!done)
717 switch (pop_type ())
718 {
719 case tp_end:
720 done = 1;
721 break;
722 case tp_pointer:
723 follow_type = lookup_pointer_type (follow_type);
724 break;
725 case tp_reference:
726 follow_type = lookup_reference_type (follow_type);
727 break;
728 case tp_array:
729 array_size = pop_type_int ();
730 if (array_size != -1)
731 follow_type = create_array_type (follow_type,
732 array_size);
733 else
734 follow_type = lookup_pointer_type (follow_type);
735 break;
736 case tp_function:
737 follow_type = lookup_function_type (follow_type);
738 break;
739 }
740 $$ = follow_type;
741 }
742 ;
743
744 abs_decl: '*'
745 { push_type (tp_pointer); $$ = 0; }
746 | '*' abs_decl
747 { push_type (tp_pointer); $$ = $2; }
748 | '&'
749 { push_type (tp_reference); $$ = 0; }
750 | '&' abs_decl
751 { push_type (tp_reference); $$ = $2; }
752 | direct_abs_decl
753 ;
754
755 direct_abs_decl: '(' abs_decl ')'
756 { $$ = $2; }
757 | direct_abs_decl array_mod
758 {
759 push_type_int ($2);
760 push_type (tp_array);
761 }
762 | array_mod
763 {
764 push_type_int ($1);
765 push_type (tp_array);
766 $$ = 0;
767 }
768 | direct_abs_decl func_mod
769 { push_type (tp_function); }
770 | func_mod
771 { push_type (tp_function); }
772 ;
773
774 array_mod: '[' ']'
775 { $$ = -1; }
776 | '[' INT ']'
777 { $$ = $2.val; }
778 ;
779
780 func_mod: '(' ')'
781 { $$ = 0; }
782 | '(' nonempty_typelist ')'
783 { free ((PTR)$2); $$ = 0; }
784 ;
785
786 type : ptype
787 | typebase COLONCOLON '*'
788 { $$ = lookup_member_type (builtin_type_int, $1); }
789 | type '(' typebase COLONCOLON '*' ')'
790 { $$ = lookup_member_type ($1, $3); }
791 | type '(' typebase COLONCOLON '*' ')' '(' ')'
792 { $$ = lookup_member_type
793 (lookup_function_type ($1), $3); }
794 | type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
795 { $$ = lookup_member_type
796 (lookup_function_type ($1), $3);
797 free ((PTR)$8); }
798 ;
799
800 typebase /* Implements (approximately): (type-qualifier)* type-specifier */
801 : TYPENAME
802 { $$ = $1.type; }
803 | INT_KEYWORD
804 { $$ = builtin_type_int; }
805 | LONG
806 { $$ = builtin_type_long; }
807 | SHORT
808 { $$ = builtin_type_short; }
809 | LONG INT_KEYWORD
810 { $$ = builtin_type_long; }
811 | UNSIGNED LONG INT_KEYWORD
812 { $$ = builtin_type_unsigned_long; }
813 | LONG LONG
814 { $$ = builtin_type_long_long; }
815 | LONG LONG INT_KEYWORD
816 { $$ = builtin_type_long_long; }
817 | UNSIGNED LONG LONG
818 { $$ = builtin_type_unsigned_long_long; }
819 | UNSIGNED LONG LONG INT_KEYWORD
820 { $$ = builtin_type_unsigned_long_long; }
821 | SHORT INT_KEYWORD
822 { $$ = builtin_type_short; }
823 | UNSIGNED SHORT INT_KEYWORD
824 { $$ = builtin_type_unsigned_short; }
825 | STRUCT name
826 { $$ = lookup_struct (copy_name ($2),
827 expression_context_block); }
828 | CLASS name
829 { $$ = lookup_struct (copy_name ($2),
830 expression_context_block); }
831 | UNION name
832 { $$ = lookup_union (copy_name ($2),
833 expression_context_block); }
834 | ENUM name
835 { $$ = lookup_enum (copy_name ($2),
836 expression_context_block); }
837 | UNSIGNED typename
838 { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
839 | UNSIGNED
840 { $$ = builtin_type_unsigned_int; }
841 | SIGNED_KEYWORD typename
842 { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
843 | SIGNED_KEYWORD
844 { $$ = builtin_type_int; }
845 | TEMPLATE name '<' type '>'
846 { $$ = lookup_template_type(copy_name($2), $4,
847 expression_context_block);
848 }
849 /* "const" and "volatile" are curently ignored. */
850 | CONST_KEYWORD typebase { $$ = $2; }
851 | VOLATILE_KEYWORD typebase { $$ = $2; }
852 ;
853
854 typename: TYPENAME
855 | INT_KEYWORD
856 {
857 $$.stoken.ptr = "int";
858 $$.stoken.length = 3;
859 $$.type = builtin_type_int;
860 }
861 | LONG
862 {
863 $$.stoken.ptr = "long";
864 $$.stoken.length = 4;
865 $$.type = builtin_type_long;
866 }
867 | SHORT
868 {
869 $$.stoken.ptr = "short";
870 $$.stoken.length = 5;
871 $$.type = builtin_type_short;
872 }
873 ;
874
875 nonempty_typelist
876 : type
877 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
878 $<ivec>$[0] = 1; /* Number of types in vector */
879 $$[1] = $1;
880 }
881 | nonempty_typelist ',' type
882 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
883 $$ = (struct type **) realloc ((char *) $1, len);
884 $$[$<ivec>$[0]] = $3;
885 }
886 ;
887
888 name : NAME { $$ = $1.stoken; }
889 | BLOCKNAME { $$ = $1.stoken; }
890 | TYPENAME { $$ = $1.stoken; }
891 | NAME_OR_INT { $$ = $1.stoken; }
892 ;
893
894 name_not_typename : NAME
895 | BLOCKNAME
896 /* These would be useful if name_not_typename was useful, but it is just
897 a fake for "variable", so these cause reduce/reduce conflicts because
898 the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
899 =exp) or just an exp. If name_not_typename was ever used in an lvalue
900 context where only a name could occur, this might be useful.
901 | NAME_OR_INT
902 */
903 ;
904
905 %%
906
907 /* Take care of parsing a number (anything that starts with a digit).
908 Set yylval and return the token type; update lexptr.
909 LEN is the number of characters in it. */
910
911 /*** Needs some error checking for the float case ***/
912
913 static int
914 parse_number (p, len, parsed_float, putithere)
915 register char *p;
916 register int len;
917 int parsed_float;
918 YYSTYPE *putithere;
919 {
920 register LONGEST n = 0;
921 register LONGEST prevn = 0;
922 register int i;
923 register int c;
924 register int base = input_radix;
925 int unsigned_p = 0;
926 int long_p = 0;
927 LONGEST high_bit;
928 struct type *signed_type;
929 struct type *unsigned_type;
930
931 if (parsed_float)
932 {
933 /* It's a float since it contains a point or an exponent. */
934 putithere->dval = atof (p);
935 return FLOAT;
936 }
937
938 /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
939 if (p[0] == '0')
940 switch (p[1])
941 {
942 case 'x':
943 case 'X':
944 if (len >= 3)
945 {
946 p += 2;
947 base = 16;
948 len -= 2;
949 }
950 break;
951
952 case 't':
953 case 'T':
954 case 'd':
955 case 'D':
956 if (len >= 3)
957 {
958 p += 2;
959 base = 10;
960 len -= 2;
961 }
962 break;
963
964 default:
965 base = 8;
966 break;
967 }
968
969 while (len-- > 0)
970 {
971 c = *p++;
972 if (c >= 'A' && c <= 'Z')
973 c += 'a' - 'A';
974 if (c != 'l' && c != 'u')
975 n *= base;
976 if (c >= '0' && c <= '9')
977 n += i = c - '0';
978 else
979 {
980 if (base > 10 && c >= 'a' && c <= 'f')
981 n += i = c - 'a' + 10;
982 else if (len == 0 && c == 'l')
983 long_p = 1;
984 else if (len == 0 && c == 'u')
985 unsigned_p = 1;
986 else
987 return ERROR; /* Char not a digit */
988 }
989 if (i >= base)
990 return ERROR; /* Invalid digit in this base */
991
992 /* Portably test for overflow (only works for nonzero values, so make
993 a second check for zero). */
994 if((prevn >= n) && n != 0)
995 unsigned_p=1; /* Try something unsigned */
996 /* If range checking enabled, portably test for unsigned overflow. */
997 if(RANGE_CHECK && n!=0)
998 {
999 if((unsigned_p && (unsigned)prevn >= (unsigned)n))
1000 range_error("Overflow on numeric constant.");
1001 }
1002 prevn=n;
1003 }
1004
1005 /* If the number is too big to be an int, or it's got an l suffix
1006 then it's a long. Work out if this has to be a long by
1007 shifting right and and seeing if anything remains, and the
1008 target int size is different to the target long size. */
1009
1010 if ((TARGET_INT_BIT != TARGET_LONG_BIT && (n >> TARGET_INT_BIT)) || long_p)
1011 {
1012 high_bit = ((LONGEST)1) << (TARGET_LONG_BIT-1);
1013 unsigned_type = builtin_type_unsigned_long;
1014 signed_type = builtin_type_long;
1015 }
1016 else
1017 {
1018 high_bit = ((LONGEST)1) << (TARGET_INT_BIT-1);
1019 unsigned_type = builtin_type_unsigned_int;
1020 signed_type = builtin_type_int;
1021 }
1022
1023 putithere->typed_val.val = n;
1024
1025 /* If the high bit of the worked out type is set then this number
1026 has to be unsigned. */
1027
1028 if (unsigned_p || (n & high_bit))
1029 {
1030 putithere->typed_val.type = unsigned_type;
1031 }
1032 else
1033 {
1034 putithere->typed_val.type = signed_type;
1035 }
1036
1037 return INT;
1038 }
1039
1040 struct token
1041 {
1042 char *operator;
1043 int token;
1044 enum exp_opcode opcode;
1045 };
1046
1047 const static struct token tokentab3[] =
1048 {
1049 {">>=", ASSIGN_MODIFY, BINOP_RSH},
1050 {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1051 };
1052
1053 const static struct token tokentab2[] =
1054 {
1055 {"+=", ASSIGN_MODIFY, BINOP_ADD},
1056 {"-=", ASSIGN_MODIFY, BINOP_SUB},
1057 {"*=", ASSIGN_MODIFY, BINOP_MUL},
1058 {"/=", ASSIGN_MODIFY, BINOP_DIV},
1059 {"%=", ASSIGN_MODIFY, BINOP_REM},
1060 {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1061 {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1062 {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1063 {"++", INCREMENT, BINOP_END},
1064 {"--", DECREMENT, BINOP_END},
1065 {"->", ARROW, BINOP_END},
1066 {"&&", ANDAND, BINOP_END},
1067 {"||", OROR, BINOP_END},
1068 {"::", COLONCOLON, BINOP_END},
1069 {"<<", LSH, BINOP_END},
1070 {">>", RSH, BINOP_END},
1071 {"==", EQUAL, BINOP_END},
1072 {"!=", NOTEQUAL, BINOP_END},
1073 {"<=", LEQ, BINOP_END},
1074 {">=", GEQ, BINOP_END}
1075 };
1076
1077 /* Read one token, getting characters through lexptr. */
1078
1079 int
1080 yylex ()
1081 {
1082 register int c;
1083 register int namelen;
1084 register unsigned i;
1085 register char *tokstart;
1086
1087 retry:
1088
1089 tokstart = lexptr;
1090 /* See if it is a special token of length 3. */
1091 for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1092 if (!strncmp (tokstart, tokentab3[i].operator, 3))
1093 {
1094 lexptr += 3;
1095 yylval.opcode = tokentab3[i].opcode;
1096 return tokentab3[i].token;
1097 }
1098
1099 /* See if it is a special token of length 2. */
1100 for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1101 if (!strncmp (tokstart, tokentab2[i].operator, 2))
1102 {
1103 lexptr += 2;
1104 yylval.opcode = tokentab2[i].opcode;
1105 return tokentab2[i].token;
1106 }
1107
1108 switch (c = *tokstart)
1109 {
1110 case 0:
1111 return 0;
1112
1113 case ' ':
1114 case '\t':
1115 case '\n':
1116 lexptr++;
1117 goto retry;
1118
1119 case '\'':
1120 /* We either have a character constant ('0' or '\177' for example)
1121 or we have a quoted symbol reference ('foo(int,int)' in C++
1122 for example). */
1123 lexptr++;
1124 c = *lexptr++;
1125 if (c == '\\')
1126 c = parse_escape (&lexptr);
1127
1128 yylval.typed_val.val = c;
1129 yylval.typed_val.type = builtin_type_char;
1130
1131 c = *lexptr++;
1132 if (c != '\'')
1133 {
1134 namelen = skip_quoted (tokstart) - tokstart;
1135 if (namelen > 2)
1136 {
1137 lexptr = tokstart + namelen;
1138 namelen -= 2;
1139 tokstart++;
1140 goto tryname;
1141 }
1142 error ("Invalid character constant.");
1143 }
1144 return INT;
1145
1146 case '(':
1147 paren_depth++;
1148 lexptr++;
1149 return c;
1150
1151 case ')':
1152 if (paren_depth == 0)
1153 return 0;
1154 paren_depth--;
1155 lexptr++;
1156 return c;
1157
1158 case ',':
1159 if (comma_terminates && paren_depth == 0)
1160 return 0;
1161 lexptr++;
1162 return c;
1163
1164 case '.':
1165 /* Might be a floating point number. */
1166 if (lexptr[1] < '0' || lexptr[1] > '9')
1167 goto symbol; /* Nope, must be a symbol. */
1168 /* FALL THRU into number case. */
1169
1170 case '0':
1171 case '1':
1172 case '2':
1173 case '3':
1174 case '4':
1175 case '5':
1176 case '6':
1177 case '7':
1178 case '8':
1179 case '9':
1180 {
1181 /* It's a number. */
1182 int got_dot = 0, got_e = 0, toktype;
1183 register char *p = tokstart;
1184 int hex = input_radix > 10;
1185
1186 if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1187 {
1188 p += 2;
1189 hex = 1;
1190 }
1191 else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1192 {
1193 p += 2;
1194 hex = 0;
1195 }
1196
1197 for (;; ++p)
1198 {
1199 if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1200 got_dot = got_e = 1;
1201 else if (!hex && !got_dot && *p == '.')
1202 got_dot = 1;
1203 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1204 && (*p == '-' || *p == '+'))
1205 /* This is the sign of the exponent, not the end of the
1206 number. */
1207 continue;
1208 /* We will take any letters or digits. parse_number will
1209 complain if past the radix, or if L or U are not final. */
1210 else if ((*p < '0' || *p > '9')
1211 && ((*p < 'a' || *p > 'z')
1212 && (*p < 'A' || *p > 'Z')))
1213 break;
1214 }
1215 toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1216 if (toktype == ERROR)
1217 {
1218 char *err_copy = (char *) alloca (p - tokstart + 1);
1219
1220 memcpy (err_copy, tokstart, p - tokstart);
1221 err_copy[p - tokstart] = 0;
1222 error ("Invalid number \"%s\".", err_copy);
1223 }
1224 lexptr = p;
1225 return toktype;
1226 }
1227
1228 case '+':
1229 case '-':
1230 case '*':
1231 case '/':
1232 case '%':
1233 case '|':
1234 case '&':
1235 case '^':
1236 case '~':
1237 case '!':
1238 case '@':
1239 case '<':
1240 case '>':
1241 case '[':
1242 case ']':
1243 case '?':
1244 case ':':
1245 case '=':
1246 case '{':
1247 case '}':
1248 symbol:
1249 lexptr++;
1250 return c;
1251
1252 case '"':
1253 for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
1254 if (c == '\\')
1255 {
1256 c = tokstart[++namelen];
1257 if (c >= '0' && c <= '9')
1258 {
1259 c = tokstart[++namelen];
1260 if (c >= '0' && c <= '9')
1261 c = tokstart[++namelen];
1262 }
1263 }
1264 yylval.sval.ptr = tokstart + 1;
1265 yylval.sval.length = namelen - 1;
1266 lexptr += namelen + 1;
1267 return STRING;
1268 }
1269
1270 if (!(c == '_' || c == '$'
1271 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1272 /* We must have come across a bad character (e.g. ';'). */
1273 error ("Invalid character '%c' in expression.", c);
1274
1275 /* It's a name. See how long it is. */
1276 namelen = 0;
1277 for (c = tokstart[namelen];
1278 (c == '_' || c == '$' || (c >= '0' && c <= '9')
1279 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
1280 c = tokstart[++namelen])
1281 ;
1282
1283 /* The token "if" terminates the expression and is NOT
1284 removed from the input stream. */
1285 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1286 {
1287 return 0;
1288 }
1289
1290 lexptr += namelen;
1291
1292 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1293 and $$digits (equivalent to $<-digits> if you could type that).
1294 Make token type LAST, and put the number (the digits) in yylval. */
1295
1296 tryname:
1297 if (*tokstart == '$')
1298 {
1299 register int negate = 0;
1300 c = 1;
1301 /* Double dollar means negate the number and add -1 as well.
1302 Thus $$ alone means -1. */
1303 if (namelen >= 2 && tokstart[1] == '$')
1304 {
1305 negate = 1;
1306 c = 2;
1307 }
1308 if (c == namelen)
1309 {
1310 /* Just dollars (one or two) */
1311 yylval.lval = - negate;
1312 return LAST;
1313 }
1314 /* Is the rest of the token digits? */
1315 for (; c < namelen; c++)
1316 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1317 break;
1318 if (c == namelen)
1319 {
1320 yylval.lval = atoi (tokstart + 1 + negate);
1321 if (negate)
1322 yylval.lval = - yylval.lval;
1323 return LAST;
1324 }
1325 }
1326
1327 /* Handle tokens that refer to machine registers:
1328 $ followed by a register name. */
1329
1330 if (*tokstart == '$') {
1331 for (c = 0; c < NUM_REGS; c++)
1332 if (namelen - 1 == strlen (reg_names[c])
1333 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1334 {
1335 yylval.lval = c;
1336 return REGNAME;
1337 }
1338 for (c = 0; c < num_std_regs; c++)
1339 if (namelen - 1 == strlen (std_regs[c].name)
1340 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1341 {
1342 yylval.lval = std_regs[c].regnum;
1343 return REGNAME;
1344 }
1345 }
1346 /* Catch specific keywords. Should be done with a data structure. */
1347 switch (namelen)
1348 {
1349 case 8:
1350 if (!strncmp (tokstart, "unsigned", 8))
1351 return UNSIGNED;
1352 if (current_language->la_language == language_cplus
1353 && !strncmp (tokstart, "template", 8))
1354 return TEMPLATE;
1355 if (!strncmp (tokstart, "volatile", 8))
1356 return VOLATILE_KEYWORD;
1357 break;
1358 case 6:
1359 if (!strncmp (tokstart, "struct", 6))
1360 return STRUCT;
1361 if (!strncmp (tokstart, "signed", 6))
1362 return SIGNED_KEYWORD;
1363 if (!strncmp (tokstart, "sizeof", 6))
1364 return SIZEOF;
1365 break;
1366 case 5:
1367 if (current_language->la_language == language_cplus
1368 && !strncmp (tokstart, "class", 5))
1369 return CLASS;
1370 if (!strncmp (tokstart, "union", 5))
1371 return UNION;
1372 if (!strncmp (tokstart, "short", 5))
1373 return SHORT;
1374 if (!strncmp (tokstart, "const", 5))
1375 return CONST_KEYWORD;
1376 break;
1377 case 4:
1378 if (!strncmp (tokstart, "enum", 4))
1379 return ENUM;
1380 if (!strncmp (tokstart, "long", 4))
1381 return LONG;
1382 if (current_language->la_language == language_cplus
1383 && !strncmp (tokstart, "this", 4))
1384 {
1385 static const char this_name[] =
1386 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1387
1388 if (lookup_symbol (this_name, expression_context_block,
1389 VAR_NAMESPACE, 0, NULL))
1390 return THIS;
1391 }
1392 break;
1393 case 3:
1394 if (!strncmp (tokstart, "int", 3))
1395 return INT_KEYWORD;
1396 break;
1397 default:
1398 break;
1399 }
1400
1401 yylval.sval.ptr = tokstart;
1402 yylval.sval.length = namelen;
1403
1404 /* Any other names starting in $ are debugger internal variables. */
1405
1406 if (*tokstart == '$')
1407 {
1408 yylval.ivar = lookup_internalvar (copy_name (yylval.sval) + 1);
1409 return VARIABLE;
1410 }
1411
1412 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1413 functions or symtabs. If this is not so, then ...
1414 Use token-type TYPENAME for symbols that happen to be defined
1415 currently as names of types; NAME for other symbols.
1416 The caller is not constrained to care about the distinction. */
1417 {
1418 char *tmp = copy_name (yylval.sval);
1419 struct symbol *sym;
1420 int is_a_field_of_this = 0;
1421 int hextype;
1422
1423 sym = lookup_symbol (tmp, expression_context_block,
1424 VAR_NAMESPACE,
1425 current_language->la_language == language_cplus
1426 ? &is_a_field_of_this : NULL,
1427 NULL);
1428 if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
1429 lookup_partial_symtab (tmp))
1430 {
1431 yylval.ssym.sym = sym;
1432 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1433 return BLOCKNAME;
1434 }
1435 if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1436 {
1437 yylval.tsym.type = SYMBOL_TYPE (sym);
1438 return TYPENAME;
1439 }
1440 if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1441 return TYPENAME;
1442
1443 /* Input names that aren't symbols but ARE valid hex numbers,
1444 when the input radix permits them, can be names or numbers
1445 depending on the parse. Note we support radixes > 16 here. */
1446 if (!sym &&
1447 ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1448 (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1449 {
1450 YYSTYPE newlval; /* Its value is ignored. */
1451 hextype = parse_number (tokstart, namelen, 0, &newlval);
1452 if (hextype == INT)
1453 {
1454 yylval.ssym.sym = sym;
1455 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1456 return NAME_OR_INT;
1457 }
1458 }
1459
1460 /* Any other kind of symbol */
1461 yylval.ssym.sym = sym;
1462 yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1463 return NAME;
1464 }
1465 }
1466
1467 void
1468 yyerror (msg)
1469 char *msg;
1470 {
1471 error (msg ? msg : "Invalid syntax in expression.");
1472 }
1473 \f
1474 /* Print the character C on STREAM as part of the contents of a literal
1475 string whose delimiter is QUOTER. Note that that format for printing
1476 characters and strings is language specific. */
1477
1478 static void
1479 emit_char (c, stream, quoter)
1480 register int c;
1481 FILE *stream;
1482 int quoter;
1483 {
1484
1485 c &= 0xFF; /* Avoid sign bit follies */
1486
1487 if ( c < 0x20 || /* Low control chars */
1488 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1489 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
1490 switch (c)
1491 {
1492 case '\n':
1493 fputs_filtered ("\\n", stream);
1494 break;
1495 case '\b':
1496 fputs_filtered ("\\b", stream);
1497 break;
1498 case '\t':
1499 fputs_filtered ("\\t", stream);
1500 break;
1501 case '\f':
1502 fputs_filtered ("\\f", stream);
1503 break;
1504 case '\r':
1505 fputs_filtered ("\\r", stream);
1506 break;
1507 case '\033':
1508 fputs_filtered ("\\e", stream);
1509 break;
1510 case '\007':
1511 fputs_filtered ("\\a", stream);
1512 break;
1513 default:
1514 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1515 break;
1516 }
1517 } else {
1518 if (c == '\\' || c == quoter)
1519 {
1520 fputs_filtered ("\\", stream);
1521 }
1522 fprintf_filtered (stream, "%c", c);
1523 }
1524 }
1525
1526 static void
1527 c_printchar (c, stream)
1528 int c;
1529 FILE *stream;
1530 {
1531 fputs_filtered ("'", stream);
1532 emit_char (c, stream, '\'');
1533 fputs_filtered ("'", stream);
1534 }
1535
1536 /* Print the character string STRING, printing at most LENGTH characters.
1537 Printing stops early if the number hits print_max; repeat counts
1538 are printed as appropriate. Print ellipses at the end if we
1539 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
1540
1541 static void
1542 c_printstr (stream, string, length, force_ellipses)
1543 FILE *stream;
1544 char *string;
1545 unsigned int length;
1546 int force_ellipses;
1547 {
1548 register unsigned int i;
1549 unsigned int things_printed = 0;
1550 int in_quotes = 0;
1551 int need_comma = 0;
1552 extern int inspect_it;
1553 extern int repeat_count_threshold;
1554 extern int print_max;
1555
1556 if (length == 0)
1557 {
1558 fputs_filtered ("\"\"", stdout);
1559 return;
1560 }
1561
1562 for (i = 0; i < length && things_printed < print_max; ++i)
1563 {
1564 /* Position of the character we are examining
1565 to see whether it is repeated. */
1566 unsigned int rep1;
1567 /* Number of repetitions we have detected so far. */
1568 unsigned int reps;
1569
1570 QUIT;
1571
1572 if (need_comma)
1573 {
1574 fputs_filtered (", ", stream);
1575 need_comma = 0;
1576 }
1577
1578 rep1 = i + 1;
1579 reps = 1;
1580 while (rep1 < length && string[rep1] == string[i])
1581 {
1582 ++rep1;
1583 ++reps;
1584 }
1585
1586 if (reps > repeat_count_threshold)
1587 {
1588 if (in_quotes)
1589 {
1590 if (inspect_it)
1591 fputs_filtered ("\\\", ", stream);
1592 else
1593 fputs_filtered ("\", ", stream);
1594 in_quotes = 0;
1595 }
1596 c_printchar (string[i], stream);
1597 fprintf_filtered (stream, " <repeats %u times>", reps);
1598 i = rep1 - 1;
1599 things_printed += repeat_count_threshold;
1600 need_comma = 1;
1601 }
1602 else
1603 {
1604 if (!in_quotes)
1605 {
1606 if (inspect_it)
1607 fputs_filtered ("\\\"", stream);
1608 else
1609 fputs_filtered ("\"", stream);
1610 in_quotes = 1;
1611 }
1612 emit_char (string[i], stream, '"');
1613 ++things_printed;
1614 }
1615 }
1616
1617 /* Terminate the quotes if necessary. */
1618 if (in_quotes)
1619 {
1620 if (inspect_it)
1621 fputs_filtered ("\\\"", stream);
1622 else
1623 fputs_filtered ("\"", stream);
1624 }
1625
1626 if (force_ellipses || i < length)
1627 fputs_filtered ("...", stream);
1628 }
1629
1630 \f
1631 /* Table mapping opcodes into strings for printing operators
1632 and precedences of the operators. */
1633
1634 const static struct op_print c_op_print_tab[] =
1635 {
1636 {",", BINOP_COMMA, PREC_COMMA, 0},
1637 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1638 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1639 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1640 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
1641 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
1642 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
1643 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
1644 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1645 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1646 {">=", BINOP_GEQ, PREC_ORDER, 0},
1647 {">", BINOP_GTR, PREC_ORDER, 0},
1648 {"<", BINOP_LESS, PREC_ORDER, 0},
1649 {">>", BINOP_RSH, PREC_SHIFT, 0},
1650 {"<<", BINOP_LSH, PREC_SHIFT, 0},
1651 {"+", BINOP_ADD, PREC_ADD, 0},
1652 {"-", BINOP_SUB, PREC_ADD, 0},
1653 {"*", BINOP_MUL, PREC_MUL, 0},
1654 {"/", BINOP_DIV, PREC_MUL, 0},
1655 {"%", BINOP_REM, PREC_MUL, 0},
1656 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
1657 {"-", UNOP_NEG, PREC_PREFIX, 0},
1658 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1659 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
1660 {"*", UNOP_IND, PREC_PREFIX, 0},
1661 {"&", UNOP_ADDR, PREC_PREFIX, 0},
1662 {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
1663 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
1664 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
1665 /* C++ */
1666 {"::", BINOP_SCOPE, PREC_PREFIX, 0},
1667 {NULL, 0, 0, 0}
1668 };
1669 \f
1670 /* These variables point to the objects
1671 representing the predefined C data types. */
1672
1673 struct type *builtin_type_void;
1674 struct type *builtin_type_char;
1675 struct type *builtin_type_short;
1676 struct type *builtin_type_int;
1677 struct type *builtin_type_long;
1678 struct type *builtin_type_long_long;
1679 struct type *builtin_type_signed_char;
1680 struct type *builtin_type_unsigned_char;
1681 struct type *builtin_type_unsigned_short;
1682 struct type *builtin_type_unsigned_int;
1683 struct type *builtin_type_unsigned_long;
1684 struct type *builtin_type_unsigned_long_long;
1685 struct type *builtin_type_float;
1686 struct type *builtin_type_double;
1687 struct type *builtin_type_long_double;
1688 struct type *builtin_type_complex;
1689 struct type *builtin_type_double_complex;
1690
1691 struct type ** const (c_builtin_types[]) =
1692 {
1693 &builtin_type_int,
1694 &builtin_type_long,
1695 &builtin_type_short,
1696 &builtin_type_char,
1697 &builtin_type_float,
1698 &builtin_type_double,
1699 &builtin_type_void,
1700 &builtin_type_long_long,
1701 &builtin_type_signed_char,
1702 &builtin_type_unsigned_char,
1703 &builtin_type_unsigned_short,
1704 &builtin_type_unsigned_int,
1705 &builtin_type_unsigned_long,
1706 &builtin_type_unsigned_long_long,
1707 &builtin_type_long_double,
1708 &builtin_type_complex,
1709 &builtin_type_double_complex,
1710 0
1711 };
1712
1713 const struct language_defn c_language_defn = {
1714 "c", /* Language name */
1715 language_c,
1716 c_builtin_types,
1717 range_check_off,
1718 type_check_off,
1719 c_parse,
1720 c_error,
1721 c_printchar, /* Print a character constant */
1722 c_printstr, /* Function to print string constant */
1723 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1724 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1725 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1726 {"", "", "", ""}, /* Binary format info */
1727 {"0%o", "0", "o", ""}, /* Octal format info */
1728 {"%d", "", "d", ""}, /* Decimal format info */
1729 {"0x%x", "0x", "x", ""}, /* Hex format info */
1730 c_op_print_tab, /* expression operators for printing */
1731 LANG_MAGIC
1732 };
1733
1734 const struct language_defn cplus_language_defn = {
1735 "c++", /* Language name */
1736 language_cplus,
1737 c_builtin_types,
1738 range_check_off,
1739 type_check_off,
1740 c_parse,
1741 c_error,
1742 c_printchar, /* Print a character constant */
1743 c_printstr, /* Function to print string constant */
1744 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1745 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1746 &builtin_type_double, /* longest floating point type */ /*FIXME*/
1747 {"", "", "", ""}, /* Binary format info */
1748 {"0%o", "0", "o", ""}, /* Octal format info */
1749 {"%d", "", "d", ""}, /* Decimal format info */
1750 {"0x%x", "0x", "x", ""}, /* Hex format info */
1751 c_op_print_tab, /* expression operators for printing */
1752 LANG_MAGIC
1753 };
1754
1755 void
1756 _initialize_c_exp ()
1757 {
1758 builtin_type_void =
1759 init_type (TYPE_CODE_VOID, 1,
1760 0,
1761 "void", (struct objfile *) NULL);
1762 builtin_type_char =
1763 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1764 0,
1765 "char", (struct objfile *) NULL);
1766 builtin_type_signed_char =
1767 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1768 TYPE_FLAG_SIGNED,
1769 "signed char", (struct objfile *) NULL);
1770 builtin_type_unsigned_char =
1771 init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1772 TYPE_FLAG_UNSIGNED,
1773 "unsigned char", (struct objfile *) NULL);
1774 builtin_type_short =
1775 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1776 0,
1777 "short", (struct objfile *) NULL);
1778 builtin_type_unsigned_short =
1779 init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
1780 TYPE_FLAG_UNSIGNED,
1781 "unsigned short", (struct objfile *) NULL);
1782 builtin_type_int =
1783 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1784 0,
1785 "int", (struct objfile *) NULL);
1786 builtin_type_unsigned_int =
1787 init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
1788 TYPE_FLAG_UNSIGNED,
1789 "unsigned int", (struct objfile *) NULL);
1790 builtin_type_long =
1791 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1792 0,
1793 "long", (struct objfile *) NULL);
1794 builtin_type_unsigned_long =
1795 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1796 TYPE_FLAG_UNSIGNED,
1797 "unsigned long", (struct objfile *) NULL);
1798 builtin_type_long_long =
1799 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1800 0,
1801 "long long", (struct objfile *) NULL);
1802 builtin_type_unsigned_long_long =
1803 init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
1804 TYPE_FLAG_UNSIGNED,
1805 "unsigned long long", (struct objfile *) NULL);
1806 builtin_type_float =
1807 init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
1808 0,
1809 "float", (struct objfile *) NULL);
1810 builtin_type_double =
1811 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1812 0,
1813 "double", (struct objfile *) NULL);
1814 builtin_type_long_double =
1815 init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
1816 0,
1817 "long double", (struct objfile *) NULL);
1818 builtin_type_complex =
1819 init_type (TYPE_CODE_FLT, TARGET_COMPLEX_BIT / TARGET_CHAR_BIT,
1820 0,
1821 "complex", (struct objfile *) NULL);
1822 builtin_type_double_complex =
1823 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_COMPLEX_BIT / TARGET_CHAR_BIT,
1824 0,
1825 "double complex", (struct objfile *) NULL);
1826
1827 add_language (&c_language_defn);
1828 add_language (&cplus_language_defn);
1829 }