1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is the lexical analyzer for GNU C++. */
26 /* Cause the `yydebug' variable to be defined. */
45 #include "diagnostic.h"
47 #ifdef MULTIBYTE_CHARS
52 extern void yyprint PARAMS ((FILE *, int, YYSTYPE));
54 static int interface_strcmp PARAMS ((const char *));
55 static int *init_cpp_parse PARAMS ((void));
56 static void init_reswords PARAMS ((void));
57 static void init_cp_pragma PARAMS ((void));
59 static tree parse_strconst_pragma PARAMS ((const char *, int));
60 static void handle_pragma_vtable PARAMS ((cpp_reader *));
61 static void handle_pragma_unit PARAMS ((cpp_reader *));
62 static void handle_pragma_interface PARAMS ((cpp_reader *));
63 static void handle_pragma_implementation PARAMS ((cpp_reader *));
65 #ifdef GATHER_STATISTICS
67 static int reduce_cmp PARAMS ((int *, int *));
68 static int token_cmp PARAMS ((int *, int *));
71 static int is_global PARAMS ((tree));
72 static void init_operators PARAMS ((void));
74 /* A constraint that can be tested at compile time. */
76 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
78 #define CONSTRAINT(name, expr) extern int constraint_/**/name [(expr) ? 1 : -1]
83 extern cpp_reader parse_in;
90 /* Pending language change.
91 Positive is push count, negative is pop count. */
92 int pending_lang_change = 0;
94 extern int yychar; /* the lookahead symbol */
95 extern YYSTYPE yylval; /* the semantic value of the */
96 /* lookahead symbol */
98 /* These flags are used by c-lex.c. In C++, they're always off and on,
100 int warn_traditional = 0;
101 int flag_digraphs = 1;
103 /* the declaration found for the last IDENTIFIER token read in.
104 yylex must look this up to detect typedefs, which get token type TYPENAME,
105 so it is left around in case the identifier is not a typedef but is
106 used in a context which makes it a reference to a variable. */
109 /* Array for holding counts of the numbers of tokens seen. */
110 extern int *token_count;
112 /* Functions and data structures for #pragma interface.
114 `#pragma implementation' means that the main file being compiled
115 is considered to implement (provide) the classes that appear in
116 its main body. I.e., if this is file "foo.cc", and class `bar'
117 is defined in "foo.cc", then we say that "foo.cc implements bar".
119 All main input files "implement" themselves automagically.
121 `#pragma interface' means that unless this file (of the form "foo.h"
122 is not presently being included by file "foo.cc", the
123 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
124 of the vtables nor any of the inline functions defined in foo.h
127 There are cases when we want to link files such as "defs.h" and
128 "main.cc". In this case, we give "defs.h" a `#pragma interface',
129 and "main.cc" has `#pragma implementation "defs.h"'. */
133 const char *filename;
134 struct impl_files *next;
137 static struct impl_files *impl_file_chain;
140 /* Return something to represent absolute declarators containing a *.
141 TARGET is the absolute declarator that the * contains.
142 CV_QUALIFIERS is a list of modifiers such as const or volatile
143 to apply to the pointer type, represented as identifiers.
145 We return an INDIRECT_REF whose "contents" are TARGET
146 and whose type is the modifier list. */
149 make_pointer_declarator (cv_qualifiers, target)
150 tree cv_qualifiers, target;
152 if (target && TREE_CODE (target) == IDENTIFIER_NODE
153 && ANON_AGGRNAME_P (target))
154 error ("type name expected before `*'");
155 target = build_parse_node (INDIRECT_REF, target);
156 TREE_TYPE (target) = cv_qualifiers;
160 /* Return something to represent absolute declarators containing a &.
161 TARGET is the absolute declarator that the & contains.
162 CV_QUALIFIERS is a list of modifiers such as const or volatile
163 to apply to the reference type, represented as identifiers.
165 We return an ADDR_EXPR whose "contents" are TARGET
166 and whose type is the modifier list. */
169 make_reference_declarator (cv_qualifiers, target)
170 tree cv_qualifiers, target;
174 if (TREE_CODE (target) == ADDR_EXPR)
176 error ("cannot declare references to references");
179 if (TREE_CODE (target) == INDIRECT_REF)
181 error ("cannot declare pointers to references");
184 if (TREE_CODE (target) == IDENTIFIER_NODE && ANON_AGGRNAME_P (target))
185 error ("type name expected before `&'");
187 target = build_parse_node (ADDR_EXPR, target);
188 TREE_TYPE (target) = cv_qualifiers;
193 make_call_declarator (target, parms, cv_qualifiers, exception_specification)
194 tree target, parms, cv_qualifiers, exception_specification;
196 target = build_parse_node (CALL_EXPR, target,
197 tree_cons (parms, cv_qualifiers, NULL_TREE),
198 /* The third operand is really RTL. We
199 shouldn't put anything there. */
201 CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification;
206 set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification)
207 tree call_declarator, cv_qualifiers, exception_specification;
209 CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
210 CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
213 int interface_only; /* whether or not current file is only for
214 interface definitions. */
215 int interface_unknown; /* whether or not we know this class
216 to behave according to #pragma interface. */
218 /* Tree code classes. */
220 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
222 static char cplus_tree_code_type[] = {
224 #include "cp-tree.def"
228 /* Table indexed by tree code giving number of expression
229 operands beyond the fixed part of the node structure.
230 Not used for types or decls. */
232 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
234 static int cplus_tree_code_length[] = {
236 #include "cp-tree.def"
240 /* Names of tree components.
241 Used for printing out the tree and error messages. */
242 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
244 static const char *cplus_tree_code_name[] = {
246 #include "cp-tree.def"
250 /* toplev.c needs to call these. */
257 cpp_reader_init (&parse_in, CLK_GNUC89);
260 /* Default exceptions on. */
262 /* Mark as "unspecified". */
263 flag_bounds_check = -1;
264 /* By default wrap lines at 80 characters. Is getenv ("COLUMNS")
266 diagnostic_message_length_per_line = 80;
267 /* By default, emit location information once for every
268 diagnostic message. */
269 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
275 /* If still "unspecified", make it match -fbounded-pointers. */
276 if (flag_bounds_check < 0)
277 flag_bounds_check = flag_bounded_pointers;
280 /* the beginning of the file is a new line; check for # */
281 /* With luck, we discover the real source file's name from that
282 and put it in input_filename. */
283 ungetc (check_newline (), finput);
286 if (flag_gnu_xref) GNU_xref_begin (input_filename);
287 init_repo (input_filename);
293 if (flag_gnu_xref) GNU_xref_end (errorcount+sorrycount);
305 #ifdef GATHER_STATISTICS
307 reduce_count = (int *) xcalloc (sizeof (int), (REDUCE_LENGTH + 1));
309 token_count = (int *) xcalloc (sizeof (int), (TOKEN_LENGTH + 1));
316 /* A mapping from tree codes to operator name information. */
317 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
318 /* Similar, but for assignment operators. */
319 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
321 /* Initialize data structures that keep track of operator names. */
323 #define DEF_OPERATOR(NAME, C, NM, OM, AR, AP) \
324 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
325 #include "operators.def"
333 struct operator_name_info_t *oni;
335 #define DEF_OPERATOR(NAME, CODE, NEW_MANGLING, OLD_MANGLING, ARITY, ASSN_P) \
336 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
337 identifier = get_identifier (buffer); \
338 IDENTIFIER_OPNAME_P (identifier) = 1; \
341 ? &assignment_operator_name_info[(int) CODE] \
342 : &operator_name_info[(int) CODE]); \
343 oni->identifier = identifier; \
345 oni->mangled_name = flag_new_abi ? NEW_MANGLING : OLD_MANGLING;
347 #include "operators.def"
350 operator_name_info[(int) ERROR_MARK].identifier
351 = get_identifier ("<invalid operator>");
353 /* Handle some special cases. These operators are not defined in
354 the language, but can be produced internally. We may need them
355 for error-reporting. (Eventually, we should ensure that this
356 does not happen. Error messages involving these operators will
357 be confusing to users.) */
359 operator_name_info [(int) INIT_EXPR].name
360 = operator_name_info [(int) MODIFY_EXPR].name;
361 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
362 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
363 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
364 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
365 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
366 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
367 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
368 operator_name_info [(int) ABS_EXPR].name = "abs";
369 operator_name_info [(int) FFS_EXPR].name = "ffs";
370 operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
371 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
372 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
373 operator_name_info [(int) IN_EXPR].name = "in";
374 operator_name_info [(int) RANGE_EXPR].name = "...";
375 operator_name_info [(int) CONVERT_EXPR].name = "+";
377 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
379 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
381 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
383 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
385 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
387 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
389 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
393 /* The reserved keyword table. */
397 ENUM_BITFIELD(rid) rid : 16;
398 unsigned int disable : 16;
401 /* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is
403 #define D_EXT 0x01 /* GCC extension */
404 #define D_ASM 0x02 /* in C99, but has a switch to turn it off */
405 #define D_OPNAME 0x04 /* operator names */
407 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
409 static const struct resword reswords[] =
411 { "_Complex", RID_COMPLEX, 0 },
412 { "__alignof", RID_ALIGNOF, 0 },
413 { "__alignof__", RID_ALIGNOF, 0 },
414 { "__asm", RID_ASM, 0 },
415 { "__asm__", RID_ASM, 0 },
416 { "__attribute", RID_ATTRIBUTE, 0 },
417 { "__attribute__", RID_ATTRIBUTE, 0 },
418 { "__builtin_va_arg", RID_VA_ARG, 0 },
419 { "__complex", RID_COMPLEX, 0 },
420 { "__complex__", RID_COMPLEX, 0 },
421 { "__const", RID_CONST, 0 },
422 { "__const__", RID_CONST, 0 },
423 { "__extension__", RID_EXTENSION, 0 },
424 { "__imag", RID_IMAGPART, 0 },
425 { "__imag__", RID_IMAGPART, 0 },
426 { "__inline", RID_INLINE, 0 },
427 { "__inline__", RID_INLINE, 0 },
428 { "__label__", RID_LABEL, 0 },
429 { "__null", RID_NULL, 0 },
430 { "__real", RID_REALPART, 0 },
431 { "__real__", RID_REALPART, 0 },
432 { "__restrict", RID_RESTRICT, 0 },
433 { "__restrict__", RID_RESTRICT, 0 },
434 { "__signed", RID_SIGNED, 0 },
435 { "__signed__", RID_SIGNED, 0 },
436 { "__typeof", RID_TYPEOF, 0 },
437 { "__typeof__", RID_TYPEOF, 0 },
438 { "__volatile", RID_VOLATILE, 0 },
439 { "__volatile__", RID_VOLATILE, 0 },
440 { "asm", RID_ASM, D_ASM },
441 { "and", RID_AND, D_OPNAME },
442 { "and_eq", RID_AND_EQ, D_OPNAME },
443 { "auto", RID_AUTO, 0 },
444 { "bitand", RID_BITAND, D_OPNAME },
445 { "bitor", RID_BITOR, D_OPNAME },
446 { "bool", RID_BOOL, 0 },
447 { "break", RID_BREAK, 0 },
448 { "case", RID_CASE, 0 },
449 { "catch", RID_CATCH, 0 },
450 { "char", RID_CHAR, 0 },
451 { "class", RID_CLASS, 0 },
452 { "compl", RID_COMPL, D_OPNAME },
453 { "const", RID_CONST, 0 },
454 { "const_cast", RID_CONSTCAST, 0 },
455 { "continue", RID_CONTINUE, 0 },
456 { "default", RID_DEFAULT, 0 },
457 { "delete", RID_DELETE, 0 },
459 { "double", RID_DOUBLE, 0 },
460 { "dynamic_cast", RID_DYNCAST, 0 },
461 { "else", RID_ELSE, 0 },
462 { "enum", RID_ENUM, 0 },
463 { "explicit", RID_EXPLICIT, 0 },
464 { "export", RID_EXPORT, 0 },
465 { "extern", RID_EXTERN, 0 },
466 { "false", RID_FALSE, 0 },
467 { "float", RID_FLOAT, 0 },
468 { "for", RID_FOR, 0 },
469 { "friend", RID_FRIEND, 0 },
470 { "goto", RID_GOTO, 0 },
472 { "inline", RID_INLINE, 0 },
473 { "int", RID_INT, 0 },
474 { "long", RID_LONG, 0 },
475 { "mutable", RID_MUTABLE, 0 },
476 { "namespace", RID_NAMESPACE, 0 },
477 { "new", RID_NEW, 0 },
478 { "not", RID_NOT, D_OPNAME },
479 { "not_eq", RID_NOT_EQ, D_OPNAME },
480 { "operator", RID_OPERATOR, 0 },
481 { "or", RID_OR, D_OPNAME },
482 { "or_eq", RID_OR_EQ, D_OPNAME },
483 { "private", RID_PRIVATE, 0 },
484 { "protected", RID_PROTECTED, 0 },
485 { "public", RID_PUBLIC, 0 },
486 { "register", RID_REGISTER, 0 },
487 { "reinterpret_cast", RID_REINTCAST, 0 },
488 { "return", RID_RETURN, 0 },
489 { "short", RID_SHORT, 0 },
490 { "signed", RID_SIGNED, 0 },
491 { "sizeof", RID_SIZEOF, 0 },
492 { "static", RID_STATIC, 0 },
493 { "static_cast", RID_STATCAST, 0 },
494 { "struct", RID_STRUCT, 0 },
495 { "switch", RID_SWITCH, 0 },
496 { "template", RID_TEMPLATE, 0 },
497 { "this", RID_THIS, 0 },
498 { "throw", RID_THROW, 0 },
499 { "true", RID_TRUE, 0 },
500 { "try", RID_TRY, 0 },
501 { "typedef", RID_TYPEDEF, 0 },
502 { "typename", RID_TYPENAME, 0 },
503 { "typeid", RID_TYPEID, 0 },
504 { "typeof", RID_TYPEOF, D_ASM|D_EXT },
505 { "union", RID_UNION, 0 },
506 { "unsigned", RID_UNSIGNED, 0 },
507 { "using", RID_USING, 0 },
508 { "virtual", RID_VIRTUAL, 0 },
509 { "void", RID_VOID, 0 },
510 { "volatile", RID_VOLATILE, 0 },
511 { "wchar_t", RID_WCHAR, 0 },
512 { "while", RID_WHILE, 0 },
513 { "xor", RID_XOR, D_OPNAME },
514 { "xor_eq", RID_XOR_EQ, D_OPNAME },
517 #define N_reswords (sizeof reswords / sizeof (struct resword))
519 /* Table mapping from RID_* constants to yacc token numbers.
520 Unfortunately we have to have entries for all the keywords in all
522 const short rid_to_yy[RID_MAX] =
524 /* RID_STATIC */ SCSPEC,
525 /* RID_UNSIGNED */ TYPESPEC,
526 /* RID_LONG */ TYPESPEC,
527 /* RID_CONST */ CV_QUALIFIER,
528 /* RID_EXTERN */ SCSPEC,
529 /* RID_REGISTER */ SCSPEC,
530 /* RID_TYPEDEF */ SCSPEC,
531 /* RID_SHORT */ TYPESPEC,
532 /* RID_INLINE */ SCSPEC,
533 /* RID_VOLATILE */ CV_QUALIFIER,
534 /* RID_SIGNED */ TYPESPEC,
535 /* RID_AUTO */ SCSPEC,
536 /* RID_RESTRICT */ CV_QUALIFIER,
538 /* C extensions. Bounded pointers are not yet in C++ */
540 /* RID_UNBOUNDED */ 0,
541 /* RID_COMPLEX */ TYPESPEC,
544 /* RID_FRIEND */ SCSPEC,
545 /* RID_VIRTUAL */ SCSPEC,
546 /* RID_EXPLICIT */ SCSPEC,
547 /* RID_EXPORT */ EXPORT,
548 /* RID_MUTABLE */ SCSPEC,
559 /* RID_INT */ TYPESPEC,
560 /* RID_CHAR */ TYPESPEC,
561 /* RID_FLOAT */ TYPESPEC,
562 /* RID_DOUBLE */ TYPESPEC,
563 /* RID_VOID */ TYPESPEC,
565 /* RID_STRUCT */ AGGR,
566 /* RID_UNION */ AGGR,
569 /* RID_WHILE */ WHILE,
572 /* RID_SWITCH */ SWITCH,
574 /* RID_DEFAULT */ DEFAULT,
575 /* RID_BREAK */ BREAK,
576 /* RID_CONTINUE */ CONTINUE,
577 /* RID_RETURN */ RETURN_KEYWORD,
579 /* RID_SIZEOF */ SIZEOF,
582 /* RID_ASM */ ASM_KEYWORD,
583 /* RID_TYPEOF */ TYPEOF,
584 /* RID_ALIGNOF */ ALIGNOF,
585 /* RID_ATTRIBUTE */ ATTRIBUTE,
586 /* RID_VA_ARG */ VA_ARG,
587 /* RID_EXTENSION */ EXTENSION,
588 /* RID_IMAGPART */ IMAGPART,
589 /* RID_REALPART */ REALPART,
590 /* RID_LABEL */ LABEL,
592 /* RID_PTREXTENT */ 0,
593 /* RID_PTRVALUE */ 0,
596 /* RID_BOOL */ TYPESPEC,
597 /* RID_WCHAR */ TYPESPEC,
598 /* RID_CLASS */ AGGR,
599 /* RID_PUBLIC */ VISSPEC,
600 /* RID_PRIVATE */ VISSPEC,
601 /* RID_PROTECTED */ VISSPEC,
602 /* RID_TEMPLATE */ TEMPLATE,
603 /* RID_NULL */ CONSTANT,
604 /* RID_CATCH */ CATCH,
605 /* RID_DELETE */ DELETE,
606 /* RID_FALSE */ CXX_FALSE,
607 /* RID_NAMESPACE */ NAMESPACE,
609 /* RID_OPERATOR */ OPERATOR,
611 /* RID_THROW */ THROW,
612 /* RID_TRUE */ CXX_TRUE,
614 /* RID_TYPENAME */ TYPENAME_KEYWORD,
615 /* RID_TYPEID */ TYPEID,
616 /* RID_USING */ USING,
619 /* RID_CONSTCAST */ CONST_CAST,
620 /* RID_DYNCAST */ DYNAMIC_CAST,
621 /* RID_REINTCAST */ REINTERPRET_CAST,
622 /* RID_STATCAST */ STATIC_CAST,
624 /* alternate spellings */
625 /* RID_AND */ ANDAND,
626 /* RID_AND_EQ */ ASSIGN,
628 /* RID_NOT_EQ */ EQCOMPARE,
630 /* RID_OR_EQ */ ASSIGN,
632 /* RID_XOR_EQ */ ASSIGN,
633 /* RID_BITAND */ '&',
639 /* RID_AT_ENCODE */ 0,
641 /* RID_AT_CLASS */ 0,
642 /* RID_AT_ALIAS */ 0,
644 /* RID_AT_PRIVATE */ 0,
645 /* RID_AT_PROTECTED */ 0,
646 /* RID_AT_PUBLIC */ 0,
647 /* RID_AT_PROTOCOL */ 0,
648 /* RID_AT_SELECTOR */ 0,
649 /* RID_AT_INTERFACE */ 0,
650 /* RID_AT_IMPLEMENTATION */ 0
658 int mask = ((flag_operator_names ? 0 : D_OPNAME)
659 | (flag_no_asm ? D_ASM : 0)
660 | (flag_no_gnu_keywords ? D_EXT : 0));
662 /* It is not necessary to register ridpointers as a GC root, because
663 all the trees it points to are permanently interned in the
664 get_identifier hash anyway. */
665 ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
666 for (i = 0; i < N_reswords; i++)
668 id = get_identifier (reswords[i].word);
669 C_RID_CODE (id) = reswords[i].rid;
670 ridpointers [(int) reswords[i].rid] = id;
671 if (! (reswords[i].disable & mask))
672 C_IS_RESERVED_WORD (id) = 1;
680 #define pfile &parse_in
684 cpp_register_pragma (pfile, 0, "vtable", handle_pragma_vtable);
685 cpp_register_pragma (pfile, 0, "unit", handle_pragma_unit);
687 cpp_register_pragma (pfile, 0, "interface", handle_pragma_interface);
688 cpp_register_pragma (pfile, 0, "implementation",
689 handle_pragma_implementation);
691 cpp_register_pragma_space (pfile, "GCC");
692 cpp_register_pragma (pfile, "GCC", "interface", handle_pragma_interface);
693 cpp_register_pragma (pfile, "GCC", "implementation",
694 handle_pragma_implementation);
698 init_parse (filename)
699 const char *filename;
701 /* Make identifier nodes long enough for the language-specific slots. */
702 set_identifier_size (sizeof (struct lang_identifier));
703 decl_printable_name = lang_printable_name;
705 input_filename = "<internal>";
712 init_cplus_expand ();
713 init_cp_semantics ();
717 memcpy (tree_code_type + (int) LAST_C_TREE_CODE,
718 cplus_tree_code_type,
719 (int)LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE);
720 memcpy (tree_code_length + (int) LAST_C_TREE_CODE,
721 cplus_tree_code_length,
722 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (int));
723 memcpy (tree_code_name + (int) LAST_C_TREE_CODE,
724 cplus_tree_code_name,
725 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (char *));
731 current_function_decl = NULL;
733 class_type_node = build_int_2 (class_type, 0);
734 TREE_TYPE (class_type_node) = class_type_node;
735 ridpointers[(int) RID_CLASS] = class_type_node;
737 record_type_node = build_int_2 (record_type, 0);
738 TREE_TYPE (record_type_node) = record_type_node;
739 ridpointers[(int) RID_STRUCT] = record_type_node;
741 union_type_node = build_int_2 (union_type, 0);
742 TREE_TYPE (union_type_node) = union_type_node;
743 ridpointers[(int) RID_UNION] = union_type_node;
745 enum_type_node = build_int_2 (enum_type, 0);
746 TREE_TYPE (enum_type_node) = enum_type_node;
747 ridpointers[(int) RID_ENUM] = enum_type_node;
749 /* Create the built-in __null node. Note that we can't yet call for
750 type_for_size here because integer_type_node and so forth are not
751 set up. Therefore, we don't set the type of these nodes until
752 init_decl_processing. */
753 null_node = build_int_2 (0, 0);
754 ridpointers[RID_NULL] = null_node;
756 token_count = init_cpp_parse ();
757 interface_unknown = 1;
759 return init_c_lex (filename);
766 cpp_finish (&parse_in);
767 errorcount += parse_in.errors;
774 yyprint (file, yychar, yylval)
787 case IDENTIFIER_DEFN:
791 case PRE_PARSED_CLASS_DECL:
793 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
795 fprintf (file, " `%s'", IDENTIFIER_POINTER (DECL_NAME (t)));
798 my_friendly_assert (TREE_CODE (t) == IDENTIFIER_NODE, 224);
799 if (IDENTIFIER_POINTER (t))
800 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
804 if (yylval.ttype == class_type_node)
805 fprintf (file, " `class'");
806 else if (yylval.ttype == record_type_node)
807 fprintf (file, " `struct'");
808 else if (yylval.ttype == union_type_node)
809 fprintf (file, " `union'");
810 else if (yylval.ttype == enum_type_node)
811 fprintf (file, " `enum'");
813 my_friendly_abort (80);
818 if (TREE_CODE (t) == INTEGER_CST)
820 #if HOST_BITS_PER_WIDE_INT == 64
821 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
824 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
831 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
837 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
842 #if defined(GATHER_STATISTICS) && defined(REDUCE_LENGTH)
843 static int *reduce_count;
849 #define REDUCE_LENGTH (sizeof (yyr2) / sizeof (yyr2[0]))
850 #define TOKEN_LENGTH (256 + sizeof (yytname) / sizeof (yytname[0]))
853 #ifdef GATHER_STATISTICS
859 reduce_count[yyn] += 1;
866 return reduce_count[*q] - reduce_count[*p];
873 return token_count[*q] - token_count[*p];
879 print_parse_statistics ()
881 #ifdef GATHER_STATISTICS
885 int maxlen = REDUCE_LENGTH;
888 if (reduce_count[-1] == 0)
891 if (TOKEN_LENGTH > REDUCE_LENGTH)
892 maxlen = TOKEN_LENGTH;
893 sorted = (unsigned *) alloca (sizeof (int) * maxlen);
895 for (i = 0; i < TOKEN_LENGTH; i++)
897 qsort (sorted, TOKEN_LENGTH, sizeof (int), token_cmp);
898 for (i = 0; i < TOKEN_LENGTH; i++)
901 if (token_count[idx] == 0)
903 if (token_count[idx] < token_count[-1])
905 fprintf (stderr, "token %d, `%s', count = %d\n",
906 idx, yytname[YYTRANSLATE (idx)], token_count[idx]);
908 fprintf (stderr, "\n");
909 for (i = 0; i < REDUCE_LENGTH; i++)
911 qsort (sorted, REDUCE_LENGTH, sizeof (int), reduce_cmp);
912 for (i = 0; i < REDUCE_LENGTH; i++)
915 if (reduce_count[idx] == 0)
917 if (reduce_count[idx] < reduce_count[-1])
919 fprintf (stderr, "rule %d, line %d, count = %d\n",
920 idx, yyrline[idx], reduce_count[idx]);
922 fprintf (stderr, "\n");
928 /* Sets the value of the 'yydebug' variable to VALUE.
929 This is a function so we don't have to have YYDEBUG defined
930 in order to build the compiler. */
940 warning ("YYDEBUG not defined.");
944 /* Helper function to load global variables with interface
948 extract_interface_info ()
950 struct c_fileinfo *finfo = 0;
952 if (flag_alt_external_templates)
954 tree til = tinst_for_decl ();
957 finfo = get_fileinfo (TINST_FILE (til));
960 finfo = get_fileinfo (input_filename);
962 interface_only = finfo->interface_only;
963 interface_unknown = finfo->interface_unknown;
965 /* This happens to be a convenient place to put this. */
966 if (flag_gnu_xref) GNU_xref_file (input_filename);
969 /* Return nonzero if S is not considered part of an
970 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
976 /* Set the interface/implementation bits for this scope. */
977 struct impl_files *ifiles;
980 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
982 const char *t1 = ifiles->filename;
985 if (*s1 != *t1 || *s1 == 0)
988 while (*s1 == *t1 && *s1 != 0)
995 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
996 if (strchr (s1, '.') || strchr (t1, '.'))
999 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
1010 /* Heuristic to tell whether the user is missing a semicolon
1011 after a struct or enum declaration. Emit an error message
1012 if we know the user has blown it. */
1015 check_for_missing_semicolon (type)
1023 && yychar != IDENTIFIER
1024 && yychar != TYPENAME
1025 && yychar != CV_QUALIFIER
1026 && yychar != SELFNAME)
1027 || yychar == 0 /* EOF */)
1029 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
1030 error ("semicolon missing after %s declaration",
1031 TREE_CODE (type) == ENUMERAL_TYPE ? "enum" : "struct");
1033 cp_error ("semicolon missing after declaration of `%T'", type);
1034 shadow_tag (build_tree_list (0, type));
1036 /* Could probably also hack cases where class { ... } f (); appears. */
1041 note_got_semicolon (type)
1045 my_friendly_abort (60);
1046 if (CLASS_TYPE_P (type))
1047 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1051 note_list_got_semicolon (declspecs)
1056 for (link = declspecs; link; link = TREE_CHAIN (link))
1058 tree type = TREE_VALUE (link);
1060 note_got_semicolon (type);
1066 /* Parse a #pragma whose sole argument is a string constant.
1067 If OPT is true, the argument is optional. */
1069 parse_strconst_pragma (name, opt)
1077 if (t == CPP_STRING)
1080 if (c_lex (&x) != CPP_EOF)
1081 warning ("junk at end of #pragma %s", name);
1085 if (t == CPP_EOF && opt)
1088 error ("invalid #pragma %s", name);
1093 handle_pragma_vtable (dfile)
1094 cpp_reader *dfile ATTRIBUTE_UNUSED;
1096 tree vtbl = parse_strconst_pragma ("vtable", 0);
1098 if (vtbl && vtbl != (tree)-1)
1099 pending_vtables = tree_cons (NULL_TREE,
1100 get_identifier (TREE_STRING_POINTER (vtbl)),
1105 handle_pragma_unit (dfile)
1106 cpp_reader *dfile ATTRIBUTE_UNUSED;
1108 /* Validate syntax, but don't do anything. */
1109 parse_strconst_pragma ("unit", 0);
1113 handle_pragma_interface (dfile)
1114 cpp_reader *dfile ATTRIBUTE_UNUSED;
1116 tree fname = parse_strconst_pragma ("interface", 1);
1117 struct c_fileinfo *finfo;
1118 const char *main_filename;
1120 if (fname == (tree)-1)
1122 else if (fname == 0)
1123 main_filename = file_name_nondirectory (input_filename);
1125 main_filename = TREE_STRING_POINTER (fname);
1127 finfo = get_fileinfo (input_filename);
1129 if (impl_file_chain == 0)
1131 /* If this is zero at this point, then we are
1132 auto-implementing. */
1133 if (main_input_filename == 0)
1134 main_input_filename = input_filename;
1137 interface_only = interface_strcmp (main_filename);
1138 #ifdef MULTIPLE_SYMBOL_SPACES
1139 if (! interface_only)
1141 interface_unknown = 0;
1143 finfo->interface_only = interface_only;
1144 finfo->interface_unknown = interface_unknown;
1147 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
1148 We used to only allow this at toplevel, but that restriction was buggy
1149 in older compilers and it seems reasonable to allow it in the headers
1150 themselves, too. It only needs to precede the matching #p interface.
1152 We don't touch interface_only or interface_unknown; the user must specify
1153 a matching #p interface for this to have any effect. */
1156 handle_pragma_implementation (dfile)
1157 cpp_reader *dfile ATTRIBUTE_UNUSED;
1159 tree fname = parse_strconst_pragma ("implementation", 1);
1160 const char *main_filename;
1161 struct impl_files *ifiles = impl_file_chain;
1163 if (fname == (tree)-1)
1168 if (main_input_filename)
1169 main_filename = main_input_filename;
1171 main_filename = input_filename;
1172 main_filename = file_name_nondirectory (main_filename);
1176 main_filename = TREE_STRING_POINTER (fname);
1179 if (cpp_included (&parse_in, main_filename))
1180 warning ("#pragma implementation for %s appears after file is included",
1185 for (; ifiles; ifiles = ifiles->next)
1187 if (! strcmp (ifiles->filename, main_filename))
1192 ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
1193 ifiles->filename = main_filename;
1194 ifiles->next = impl_file_chain;
1195 impl_file_chain = ifiles;
1200 do_pending_lang_change ()
1202 for (; pending_lang_change > 0; --pending_lang_change)
1203 push_lang_context (lang_name_c);
1204 for (; pending_lang_change < 0; ++pending_lang_change)
1205 pop_lang_context ();
1208 /* Return true if d is in a global scope. */
1215 switch (TREE_CODE (d))
1220 case OVERLOAD: d = OVL_FUNCTION (d); continue;
1221 case TREE_LIST: d = TREE_VALUE (d); continue;
1223 my_friendly_assert (DECL_P (d), 980629);
1225 return DECL_NAMESPACE_SCOPE_P (d);
1230 do_identifier (token, parsing, args)
1231 register tree token;
1236 int lexing = (parsing == 1);
1239 id = lookup_name (token, 0);
1243 /* Do Koenig lookup if appropriate (inside templates we build lookup
1244 expressions instead).
1246 [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
1247 finds the declaration of a class member function, the associated
1248 namespaces and classes are not considered. */
1250 if (args && !current_template_parms && (!id || is_global (id)))
1251 id = lookup_arg_dependent (token, id, args);
1253 /* Remember that this name has been used in the class definition, as per
1256 maybe_note_name_used_in_class (token, id);
1258 if (id == error_mark_node)
1260 /* lookup_name quietly returns error_mark_node if we're parsing,
1261 as we don't want to complain about an identifier that ends up
1262 being used as a declarator. So we call it again to get the error
1264 id = lookup_name (token, 0);
1265 return error_mark_node;
1268 if (!id || (TREE_CODE (id) == FUNCTION_DECL
1269 && DECL_ANTICIPATED (id)))
1271 if (current_template_parms)
1272 return build_min_nt (LOOKUP_EXPR, token);
1273 else if (IDENTIFIER_OPNAME_P (token))
1275 if (token != ansi_opname (ERROR_MARK))
1276 cp_error ("`%D' not defined", token);
1277 id = error_mark_node;
1279 else if (current_function_decl == 0)
1281 cp_error ("`%D' was not declared in this scope", token);
1282 id = error_mark_node;
1286 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node
1287 || IDENTIFIER_ERROR_LOCUS (token) != current_function_decl)
1289 static int undeclared_variable_notice;
1291 cp_error ("`%D' undeclared (first use this function)", token);
1293 if (! undeclared_variable_notice)
1295 error ("(Each undeclared identifier is reported only once for each function it appears in.)");
1296 undeclared_variable_notice = 1;
1299 id = error_mark_node;
1300 /* Prevent repeated error messages. */
1301 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
1302 SET_IDENTIFIER_ERROR_LOCUS (token, current_function_decl);
1306 if (TREE_CODE (id) == VAR_DECL && DECL_DEAD_FOR_LOCAL (id))
1308 tree shadowed = DECL_SHADOWED_FOR_VAR (id);
1309 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1310 && DECL_DEAD_FOR_LOCAL (shadowed))
1311 shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1313 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (id));
1316 if (!DECL_ERROR_REPORTED (id))
1318 warning ("name lookup of `%s' changed",
1319 IDENTIFIER_POINTER (token));
1320 cp_warning_at (" matches this `%D' under ISO standard rules",
1322 cp_warning_at (" matches this `%D' under old rules", id);
1323 DECL_ERROR_REPORTED (id) = 1;
1327 else if (!DECL_ERROR_REPORTED (id))
1329 DECL_ERROR_REPORTED (id) = 1;
1330 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (id)))
1332 error ("name lookup of `%s' changed for new ISO `for' scoping",
1333 IDENTIFIER_POINTER (token));
1334 cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", id);
1335 id = error_mark_node;
1339 pedwarn ("name lookup of `%s' changed for new ISO `for' scoping",
1340 IDENTIFIER_POINTER (token));
1341 cp_pedwarn_at (" using obsolete binding at `%D'", id);
1345 /* TREE_USED is set in `hack_identifier'. */
1346 if (TREE_CODE (id) == CONST_DECL)
1349 if (IDENTIFIER_CLASS_VALUE (token) == id)
1350 enforce_access (CP_DECL_CONTEXT(id), id);
1351 if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
1352 id = DECL_INITIAL (id);
1355 id = hack_identifier (id, token);
1357 /* We must look up dependent names when the template is
1358 instantiated, not while parsing it. For now, we don't
1359 distinguish between dependent and independent names. So, for
1360 example, we look up all overloaded functions at
1361 instantiation-time, even though in some cases we should just use
1362 the DECL we have here. We also use LOOKUP_EXPRs to find things
1363 like local variables, rather than creating TEMPLATE_DECLs for the
1364 local variables and then finding matching instantiations. */
1365 if (current_template_parms
1366 && (is_overloaded_fn (id)
1367 || (TREE_CODE (id) == VAR_DECL
1368 && CP_DECL_CONTEXT (id)
1369 && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
1370 || TREE_CODE (id) == PARM_DECL
1371 || TREE_CODE (id) == RESULT_DECL
1372 || TREE_CODE (id) == USING_DECL))
1373 id = build_min_nt (LOOKUP_EXPR, token);
1379 do_scoped_id (token, parsing)
1384 /* during parsing, this is ::name. Otherwise, it is black magic. */
1387 id = make_node (CPLUS_BINDING);
1388 if (!qualified_lookup_using_namespace (token, global_namespace, id, 0))
1391 id = BINDING_VALUE (id);
1394 id = IDENTIFIER_GLOBAL_VALUE (token);
1395 if (parsing && yychar == YYEMPTY)
1399 if (processing_template_decl)
1401 id = build_min_nt (LOOKUP_EXPR, token);
1402 LOOKUP_EXPR_GLOBAL (id) = 1;
1405 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
1406 cp_error ("`::%D' undeclared (first use here)", token);
1407 id = error_mark_node;
1408 /* Prevent repeated error messages. */
1409 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
1413 if (TREE_CODE (id) == ADDR_EXPR)
1414 mark_used (TREE_OPERAND (id, 0));
1415 else if (TREE_CODE (id) != OVERLOAD)
1418 if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
1420 /* XXX CHS - should we set TREE_USED of the constant? */
1421 id = DECL_INITIAL (id);
1422 /* This is to prevent an enum whose value is 0
1423 from being considered a null pointer constant. */
1424 id = build1 (NOP_EXPR, TREE_TYPE (id), id);
1425 TREE_CONSTANT (id) = 1;
1428 if (processing_template_decl)
1430 if (is_overloaded_fn (id))
1432 id = build_min_nt (LOOKUP_EXPR, token);
1433 LOOKUP_EXPR_GLOBAL (id) = 1;
1436 /* else just use the decl */
1438 return convert_from_reference (id);
1442 identifier_typedecl_value (node)
1446 type = IDENTIFIER_TYPE_VALUE (node);
1447 if (type == NULL_TREE)
1450 if (IDENTIFIER_BINDING (node))
1452 t = IDENTIFIER_VALUE (node);
1453 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1456 if (IDENTIFIER_NAMESPACE_VALUE (node))
1458 t = IDENTIFIER_NAMESPACE_VALUE (node);
1459 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1463 /* Will this one ever happen? */
1464 if (TYPE_MAIN_DECL (type))
1465 return TYPE_MAIN_DECL (type);
1467 /* We used to do an internal error of 62 here, but instead we will
1468 handle the return of a null appropriately in the callers. */
1472 #ifdef GATHER_STATISTICS
1473 /* The original for tree_node_kind is in the toplevel tree.c; changes there
1474 need to be brought into here, unless this were actually put into a header
1476 /* Statistics-gathering stuff. */
1497 extern int tree_node_counts[];
1498 extern int tree_node_sizes[];
1502 build_lang_decl (code, name, type)
1503 enum tree_code code;
1509 t = build_decl (code, name, type);
1510 retrofit_lang_decl (t);
1515 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
1516 and pushdecl (for functions generated by the backend). */
1519 retrofit_lang_decl (t)
1522 struct lang_decl *ld;
1525 if (CAN_HAVE_FULL_LANG_DECL_P (t))
1526 size = sizeof (struct lang_decl);
1528 size = sizeof (struct lang_decl_flags);
1530 ld = (struct lang_decl *) ggc_alloc_cleared (size);
1532 DECL_LANG_SPECIFIC (t) = ld;
1533 if (current_lang_name == lang_name_cplusplus)
1534 DECL_LANGUAGE (t) = lang_cplusplus;
1535 else if (current_lang_name == lang_name_c)
1536 DECL_LANGUAGE (t) = lang_c;
1537 else if (current_lang_name == lang_name_java)
1538 DECL_LANGUAGE (t) = lang_java;
1539 else my_friendly_abort (64);
1541 #ifdef GATHER_STATISTICS
1542 tree_node_counts[(int)lang_decl] += 1;
1543 tree_node_sizes[(int)lang_decl] += size;
1548 copy_lang_decl (node)
1552 struct lang_decl *ld;
1554 if (! DECL_LANG_SPECIFIC (node))
1557 if (!CAN_HAVE_FULL_LANG_DECL_P (node))
1558 size = sizeof (struct lang_decl_flags);
1560 size = sizeof (struct lang_decl);
1561 ld = (struct lang_decl *) ggc_alloc (size);
1562 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1563 DECL_LANG_SPECIFIC (node) = ld;
1566 /* Copy DECL, including any language-specific parts. */
1574 copy = copy_node (decl);
1575 copy_lang_decl (copy);
1580 cp_make_lang_type (code)
1581 enum tree_code code;
1583 register tree t = make_node (code);
1585 /* Set up some flags that give proper default behavior. */
1586 if (IS_AGGR_TYPE_CODE (code))
1588 struct lang_type *pi;
1590 pi = ((struct lang_type *)
1591 ggc_alloc_cleared (sizeof (struct lang_type)));
1593 TYPE_LANG_SPECIFIC (t) = pi;
1594 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1595 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1597 /* Make sure this is laid out, for ease of use later. In the
1598 presence of parse errors, the normal was of assuring this
1599 might not ever get executed, so we lay it out *immediately*. */
1600 build_pointer_type (t);
1602 #ifdef GATHER_STATISTICS
1603 tree_node_counts[(int)lang_type] += 1;
1604 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1608 /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But,
1609 TYPE_ALIAS_SET is initialized to -1 by default, so we must
1611 TYPE_ALIAS_SET (t) = 0;
1613 /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1614 since they can be virtual base types, and we then need a
1615 canonical binfo for them. Ideally, this would be done lazily for
1617 if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM)
1618 TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1624 make_aggr_type (code)
1625 enum tree_code code;
1627 tree t = cp_make_lang_type (code);
1629 if (IS_AGGR_TYPE_CODE (code))
1630 SET_IS_AGGR_TYPE (t, 1);
1636 compiler_error VPARAMS ((const char *msg, ...))
1638 #ifndef ANSI_PROTOTYPES
1646 #ifndef ANSI_PROTOTYPES
1647 msg = va_arg (ap, const char *);
1650 vsprintf (buf, msg, ap);
1652 error_with_file_and_line (input_filename, lineno, "%s (compiler error)", buf);
1655 /* Return the type-qualifier corresponding to the identifier given by
1659 cp_type_qual_from_rid (rid)
1662 if (rid == ridpointers[(int) RID_CONST])
1663 return TYPE_QUAL_CONST;
1664 else if (rid == ridpointers[(int) RID_VOLATILE])
1665 return TYPE_QUAL_VOLATILE;
1666 else if (rid == ridpointers[(int) RID_RESTRICT])
1667 return TYPE_QUAL_RESTRICT;
1669 my_friendly_abort (0);
1670 return TYPE_UNQUALIFIED;