Merge from mainline
[official-gcc.git] / gcc / cp / lex.c
blobd656d0824e3eb9f38d4ffd7510606b60c2c90a1b
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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)
11 any later version.
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. */
27 #define YYDEBUG 1
29 #include "config.h"
30 #include "system.h"
31 #include "input.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "cpplib.h"
35 #include "c-lex.h"
36 #include "lex.h"
37 #include "flags.h"
38 #include "c-pragma.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "ggc.h"
42 #include "tm_p.h"
43 #include "timevar.h"
44 #include "diagnostic.h"
46 #ifdef MULTIBYTE_CHARS
47 #include "mbchar.h"
48 #include <locale.h>
49 #endif
52 static int interface_strcmp PARAMS ((const char *));
53 static void init_reswords PARAMS ((void));
54 static void init_cp_pragma PARAMS ((void));
56 static tree parse_strconst_pragma PARAMS ((const char *, int));
57 static void handle_pragma_vtable PARAMS ((cpp_reader *));
58 static void handle_pragma_unit PARAMS ((cpp_reader *));
59 static void handle_pragma_interface PARAMS ((cpp_reader *));
60 static void handle_pragma_implementation PARAMS ((cpp_reader *));
61 static void handle_pragma_java_exceptions PARAMS ((cpp_reader *));
62 static void cxx_init PARAMS ((void));
63 static void cxx_finish PARAMS ((void));
64 static void cxx_init_options PARAMS ((void));
65 static void cxx_post_options PARAMS ((void));
67 #ifdef GATHER_STATISTICS
68 #ifdef REDUCE_LENGTH
69 static int reduce_cmp PARAMS ((int *, int *));
70 static int token_cmp PARAMS ((int *, int *));
71 #endif
72 #endif
73 static int is_global PARAMS ((tree));
74 static void init_operators PARAMS ((void));
76 /* A constraint that can be tested at compile time. */
77 #ifdef __STDC__
78 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
79 #else
80 #define CONSTRAINT(name, expr) extern int constraint_/**/name [(expr) ? 1 : -1]
81 #endif
83 #include "cpplib.h"
85 /* These flags are used by c-lex.c. In C++, they're always off and on,
86 respectively. */
87 int warn_traditional = 0;
88 int flag_digraphs = 1;
90 /* Functions and data structures for #pragma interface.
92 `#pragma implementation' means that the main file being compiled
93 is considered to implement (provide) the classes that appear in
94 its main body. I.e., if this is file "foo.cc", and class `bar'
95 is defined in "foo.cc", then we say that "foo.cc implements bar".
97 All main input files "implement" themselves automagically.
99 `#pragma interface' means that unless this file (of the form "foo.h"
100 is not presently being included by file "foo.cc", the
101 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
102 of the vtables nor any of the inline functions defined in foo.h
103 will ever be output.
105 There are cases when we want to link files such as "defs.h" and
106 "main.cc". In this case, we give "defs.h" a `#pragma interface',
107 and "main.cc" has `#pragma implementation "defs.h"'. */
109 struct impl_files
111 const char *filename;
112 struct impl_files *next;
115 static struct impl_files *impl_file_chain;
118 /* Return something to represent absolute declarators containing a *.
119 TARGET is the absolute declarator that the * contains.
120 CV_QUALIFIERS is a list of modifiers such as const or volatile
121 to apply to the pointer type, represented as identifiers.
123 We return an INDIRECT_REF whose "contents" are TARGET
124 and whose type is the modifier list. */
126 tree
127 make_pointer_declarator (cv_qualifiers, target)
128 tree cv_qualifiers, target;
130 if (target && TREE_CODE (target) == IDENTIFIER_NODE
131 && ANON_AGGRNAME_P (target))
132 error ("type name expected before `*'");
133 target = build_nt (INDIRECT_REF, target);
134 TREE_TYPE (target) = cv_qualifiers;
135 return target;
138 /* Return something to represent absolute declarators containing a &.
139 TARGET is the absolute declarator that the & contains.
140 CV_QUALIFIERS is a list of modifiers such as const or volatile
141 to apply to the reference type, represented as identifiers.
143 We return an ADDR_EXPR whose "contents" are TARGET
144 and whose type is the modifier list. */
146 tree
147 make_reference_declarator (cv_qualifiers, target)
148 tree cv_qualifiers, target;
150 if (target)
152 if (TREE_CODE (target) == ADDR_EXPR)
154 error ("cannot declare references to references");
155 return target;
157 if (TREE_CODE (target) == INDIRECT_REF)
159 error ("cannot declare pointers to references");
160 return target;
162 if (TREE_CODE (target) == IDENTIFIER_NODE && ANON_AGGRNAME_P (target))
163 error ("type name expected before `&'");
165 target = build_nt (ADDR_EXPR, target);
166 TREE_TYPE (target) = cv_qualifiers;
167 return target;
170 /* Make a new function-declarator, which is represented as a
171 CALL_EXPR. The DECLARATOR (if non-NULL) is the direct-declarator
172 or direct-abstract-declarator giving the name of the function. */
174 tree
175 make_function_declarator (declarator, parms, cv_qualifiers,
176 exception_specification)
177 tree declarator, parms, cv_qualifiers, exception_specification;
179 declarator
180 = build_nt (CALL_EXPR, declarator,
181 tree_cons (parms, cv_qualifiers, NULL_TREE),
182 /* The third operand is really RTL. We
183 shouldn't put anything there. */
184 NULL_TREE);
185 CALL_DECLARATOR_EXCEPTION_SPEC (declarator) = exception_specification;
186 return declarator;
189 void
190 set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification)
191 tree call_declarator, cv_qualifiers, exception_specification;
193 CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
194 CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
197 int interface_only; /* whether or not current file is only for
198 interface definitions. */
199 int interface_unknown; /* whether or not we know this class
200 to behave according to #pragma interface. */
202 /* Tree code classes. */
204 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
206 static char cplus_tree_code_type[] = {
207 'x',
208 #include "cp-tree.def"
210 #undef DEFTREECODE
212 /* Table indexed by tree code giving number of expression
213 operands beyond the fixed part of the node structure.
214 Not used for types or decls. */
216 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
218 static int cplus_tree_code_length[] = {
220 #include "cp-tree.def"
222 #undef DEFTREECODE
224 /* Names of tree components.
225 Used for printing out the tree and error messages. */
226 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
228 static const char *cplus_tree_code_name[] = {
229 "@@dummy",
230 #include "cp-tree.def"
232 #undef DEFTREECODE
234 /* Each front end provides its own hooks, for toplev.c. */
235 struct lang_hooks lang_hooks = {cxx_init,
236 cxx_finish,
237 cxx_init_options,
238 cxx_decode_option,
239 cxx_post_options};
241 /* Post-switch processing. */
242 static void
243 cxx_post_options ()
245 cpp_post_options (parse_in);
248 static void
249 cxx_init_options ()
251 /* Make identifier nodes long enough for the language-specific slots. */
252 set_identifier_size (sizeof (struct lang_identifier));
254 parse_in = cpp_create_reader (ident_hash, CLK_GNUCXX);
256 /* Default exceptions on. */
257 flag_exceptions = 1;
258 /* Mark as "unspecified". */
259 flag_bounds_check = -1;
260 /* By default wrap lines at 80 characters. Is getenv ("COLUMNS")
261 preferable? */
262 diagnostic_line_cutoff (global_dc) = 80;
263 /* By default, emit location information once for every
264 diagnostic message. */
265 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
268 static void
269 cxx_init ()
271 c_common_lang_init ();
273 if (flag_gnu_xref) GNU_xref_begin (input_filename);
274 init_repo (input_filename);
277 static void
278 cxx_finish ()
280 if (flag_gnu_xref) GNU_xref_end (errorcount+sorrycount);
283 const char *
284 lang_identify ()
286 return "cplusplus";
289 /* A mapping from tree codes to operator name information. */
290 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
291 /* Similar, but for assignment operators. */
292 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
294 /* Initialize data structures that keep track of operator names. */
296 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
297 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
298 #include "operators.def"
299 #undef DEF_OPERATOR
301 static void
302 init_operators ()
304 tree identifier;
305 char buffer[256];
306 struct operator_name_info_t *oni;
308 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
309 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
310 identifier = get_identifier (buffer); \
311 IDENTIFIER_OPNAME_P (identifier) = 1; \
313 oni = (ASSN_P \
314 ? &assignment_operator_name_info[(int) CODE] \
315 : &operator_name_info[(int) CODE]); \
316 oni->identifier = identifier; \
317 oni->name = NAME; \
318 oni->mangled_name = MANGLING;
320 #include "operators.def"
321 #undef DEF_OPERATOR
323 operator_name_info[(int) ERROR_MARK].identifier
324 = get_identifier ("<invalid operator>");
326 /* Handle some special cases. These operators are not defined in
327 the language, but can be produced internally. We may need them
328 for error-reporting. (Eventually, we should ensure that this
329 does not happen. Error messages involving these operators will
330 be confusing to users.) */
332 operator_name_info [(int) INIT_EXPR].name
333 = operator_name_info [(int) MODIFY_EXPR].name;
334 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
335 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
336 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
337 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
338 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
339 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
340 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
341 operator_name_info [(int) ABS_EXPR].name = "abs";
342 operator_name_info [(int) FFS_EXPR].name = "ffs";
343 operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
344 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
345 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
346 operator_name_info [(int) IN_EXPR].name = "in";
347 operator_name_info [(int) RANGE_EXPR].name = "...";
348 operator_name_info [(int) CONVERT_EXPR].name = "+";
350 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
351 = "(exact /=)";
352 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
353 = "(ceiling /=)";
354 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
355 = "(floor /=)";
356 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
357 = "(round /=)";
358 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
359 = "(ceiling %=)";
360 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
361 = "(floor %=)";
362 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
363 = "(round %=)";
366 /* The reserved keyword table. */
367 struct resword
369 const char *word;
370 ENUM_BITFIELD(rid) rid : 16;
371 unsigned int disable : 16;
374 /* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is
375 _true_. */
376 #define D_EXT 0x01 /* GCC extension */
377 #define D_ASM 0x02 /* in C99, but has a switch to turn it off */
378 #define D_OPNAME 0x04 /* operator names */
380 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
382 static const struct resword reswords[] =
384 { "_Complex", RID_COMPLEX, 0 },
385 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
386 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
387 { "__alignof", RID_ALIGNOF, 0 },
388 { "__alignof__", RID_ALIGNOF, 0 },
389 { "__asm", RID_ASM, 0 },
390 { "__asm__", RID_ASM, 0 },
391 { "__attribute", RID_ATTRIBUTE, 0 },
392 { "__attribute__", RID_ATTRIBUTE, 0 },
393 { "__builtin_va_arg", RID_VA_ARG, 0 },
394 { "__complex", RID_COMPLEX, 0 },
395 { "__complex__", RID_COMPLEX, 0 },
396 { "__const", RID_CONST, 0 },
397 { "__const__", RID_CONST, 0 },
398 { "__extension__", RID_EXTENSION, 0 },
399 { "__func__", RID_C99_FUNCTION_NAME, 0 },
400 { "__imag", RID_IMAGPART, 0 },
401 { "__imag__", RID_IMAGPART, 0 },
402 { "__inline", RID_INLINE, 0 },
403 { "__inline__", RID_INLINE, 0 },
404 { "__label__", RID_LABEL, 0 },
405 { "__null", RID_NULL, 0 },
406 { "__real", RID_REALPART, 0 },
407 { "__real__", RID_REALPART, 0 },
408 { "__restrict", RID_RESTRICT, 0 },
409 { "__restrict__", RID_RESTRICT, 0 },
410 { "__signed", RID_SIGNED, 0 },
411 { "__signed__", RID_SIGNED, 0 },
412 { "__typeof", RID_TYPEOF, 0 },
413 { "__typeof__", RID_TYPEOF, 0 },
414 { "__volatile", RID_VOLATILE, 0 },
415 { "__volatile__", RID_VOLATILE, 0 },
416 { "asm", RID_ASM, D_ASM },
417 { "and", RID_AND, D_OPNAME },
418 { "and_eq", RID_AND_EQ, D_OPNAME },
419 { "auto", RID_AUTO, 0 },
420 { "bitand", RID_BITAND, D_OPNAME },
421 { "bitor", RID_BITOR, D_OPNAME },
422 { "bool", RID_BOOL, 0 },
423 { "break", RID_BREAK, 0 },
424 { "case", RID_CASE, 0 },
425 { "catch", RID_CATCH, 0 },
426 { "char", RID_CHAR, 0 },
427 { "class", RID_CLASS, 0 },
428 { "compl", RID_COMPL, D_OPNAME },
429 { "const", RID_CONST, 0 },
430 { "const_cast", RID_CONSTCAST, 0 },
431 { "continue", RID_CONTINUE, 0 },
432 { "default", RID_DEFAULT, 0 },
433 { "delete", RID_DELETE, 0 },
434 { "do", RID_DO, 0 },
435 { "double", RID_DOUBLE, 0 },
436 { "dynamic_cast", RID_DYNCAST, 0 },
437 { "else", RID_ELSE, 0 },
438 { "enum", RID_ENUM, 0 },
439 { "explicit", RID_EXPLICIT, 0 },
440 { "export", RID_EXPORT, 0 },
441 { "extern", RID_EXTERN, 0 },
442 { "false", RID_FALSE, 0 },
443 { "float", RID_FLOAT, 0 },
444 { "for", RID_FOR, 0 },
445 { "friend", RID_FRIEND, 0 },
446 { "goto", RID_GOTO, 0 },
447 { "if", RID_IF, 0 },
448 { "inline", RID_INLINE, 0 },
449 { "int", RID_INT, 0 },
450 { "long", RID_LONG, 0 },
451 { "mutable", RID_MUTABLE, 0 },
452 { "namespace", RID_NAMESPACE, 0 },
453 { "new", RID_NEW, 0 },
454 { "not", RID_NOT, D_OPNAME },
455 { "not_eq", RID_NOT_EQ, D_OPNAME },
456 { "operator", RID_OPERATOR, 0 },
457 { "or", RID_OR, D_OPNAME },
458 { "or_eq", RID_OR_EQ, D_OPNAME },
459 { "private", RID_PRIVATE, 0 },
460 { "protected", RID_PROTECTED, 0 },
461 { "public", RID_PUBLIC, 0 },
462 { "register", RID_REGISTER, 0 },
463 { "reinterpret_cast", RID_REINTCAST, 0 },
464 { "return", RID_RETURN, 0 },
465 { "short", RID_SHORT, 0 },
466 { "signed", RID_SIGNED, 0 },
467 { "sizeof", RID_SIZEOF, 0 },
468 { "static", RID_STATIC, 0 },
469 { "static_cast", RID_STATCAST, 0 },
470 { "struct", RID_STRUCT, 0 },
471 { "switch", RID_SWITCH, 0 },
472 { "template", RID_TEMPLATE, 0 },
473 { "this", RID_THIS, 0 },
474 { "throw", RID_THROW, 0 },
475 { "true", RID_TRUE, 0 },
476 { "try", RID_TRY, 0 },
477 { "typedef", RID_TYPEDEF, 0 },
478 { "typename", RID_TYPENAME, 0 },
479 { "typeid", RID_TYPEID, 0 },
480 { "typeof", RID_TYPEOF, D_ASM|D_EXT },
481 { "union", RID_UNION, 0 },
482 { "unsigned", RID_UNSIGNED, 0 },
483 { "using", RID_USING, 0 },
484 { "virtual", RID_VIRTUAL, 0 },
485 { "void", RID_VOID, 0 },
486 { "volatile", RID_VOLATILE, 0 },
487 { "wchar_t", RID_WCHAR, 0 },
488 { "while", RID_WHILE, 0 },
489 { "xor", RID_XOR, D_OPNAME },
490 { "xor_eq", RID_XOR_EQ, D_OPNAME },
493 #define N_reswords (sizeof reswords / sizeof (struct resword))
495 static void
496 init_reswords ()
498 unsigned int i;
499 tree id;
500 int mask = ((flag_operator_names ? 0 : D_OPNAME)
501 | (flag_no_asm ? D_ASM : 0)
502 | (flag_no_gnu_keywords ? D_EXT : 0));
504 /* It is not necessary to register ridpointers as a GC root, because
505 all the trees it points to are permanently interned in the
506 get_identifier hash anyway. */
507 ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
508 for (i = 0; i < N_reswords; i++)
510 id = get_identifier (reswords[i].word);
511 C_RID_CODE (id) = reswords[i].rid;
512 ridpointers [(int) reswords[i].rid] = id;
513 if (! (reswords[i].disable & mask))
514 C_IS_RESERVED_WORD (id) = 1;
518 static void
519 init_cp_pragma ()
521 cpp_register_pragma (parse_in, 0, "vtable", handle_pragma_vtable);
522 cpp_register_pragma (parse_in, 0, "unit", handle_pragma_unit);
524 cpp_register_pragma (parse_in, 0, "interface", handle_pragma_interface);
525 cpp_register_pragma (parse_in, 0, "implementation",
526 handle_pragma_implementation);
528 cpp_register_pragma_space (parse_in, "GCC");
529 cpp_register_pragma (parse_in, "GCC", "interface", handle_pragma_interface);
530 cpp_register_pragma (parse_in, "GCC", "implementation",
531 handle_pragma_implementation);
532 cpp_register_pragma (parse_in, "GCC", "java_exceptions",
533 handle_pragma_java_exceptions);
536 const char *
537 init_parse (filename)
538 const char *filename;
540 decl_printable_name = lang_printable_name;
542 input_filename = "<internal>";
544 init_reswords ();
545 init_pragma ();
546 init_cp_pragma ();
547 init_tree ();
548 init_cplus_expand ();
549 init_cp_semantics ();
551 add_c_tree_codes ();
553 memcpy (tree_code_type + (int) LAST_C_TREE_CODE,
554 cplus_tree_code_type,
555 (int)LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE);
556 memcpy (tree_code_length + (int) LAST_C_TREE_CODE,
557 cplus_tree_code_length,
558 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (int));
559 memcpy (tree_code_name + (int) LAST_C_TREE_CODE,
560 cplus_tree_code_name,
561 (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (char *));
563 init_operators ();
564 init_method ();
565 init_error ();
567 current_function_decl = NULL;
569 class_type_node = build_int_2 (ctk_class, 0);
570 TREE_TYPE (class_type_node) = class_type_node;
571 ridpointers[(int) RID_CLASS] = class_type_node;
573 record_type_node = build_int_2 (ctk_struct, 0);
574 TREE_TYPE (record_type_node) = record_type_node;
575 ridpointers[(int) RID_STRUCT] = record_type_node;
577 union_type_node = build_int_2 (ctk_union, 0);
578 TREE_TYPE (union_type_node) = union_type_node;
579 ridpointers[(int) RID_UNION] = union_type_node;
581 enum_type_node = build_int_2 (ctk_enum, 0);
582 TREE_TYPE (enum_type_node) = enum_type_node;
583 ridpointers[(int) RID_ENUM] = enum_type_node;
585 /* Create the built-in __null node. Note that we can't yet call for
586 type_for_size here because integer_type_node and so forth are not
587 set up. Therefore, we don't set the type of these nodes until
588 init_decl_processing. */
589 null_node = build_int_2 (0, 0);
590 ridpointers[RID_NULL] = null_node;
592 interface_unknown = 1;
594 return init_c_lex (filename);
597 void
598 finish_parse ()
600 cpp_finish (parse_in);
601 /* Call to cpp_destroy () omitted for performance reasons. */
602 errorcount += cpp_errors (parse_in);
605 /* Helper function to load global variables with interface
606 information. */
608 void
609 extract_interface_info ()
611 struct c_fileinfo *finfo = 0;
613 if (flag_alt_external_templates)
615 tree til = tinst_for_decl ();
617 if (til)
618 finfo = get_fileinfo (TINST_FILE (til));
620 if (!finfo)
621 finfo = get_fileinfo (input_filename);
623 interface_only = finfo->interface_only;
624 interface_unknown = finfo->interface_unknown;
626 /* This happens to be a convenient place to put this. */
627 if (flag_gnu_xref) GNU_xref_file (input_filename);
630 /* Return nonzero if S is not considered part of an
631 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
633 static int
634 interface_strcmp (s)
635 const char *s;
637 /* Set the interface/implementation bits for this scope. */
638 struct impl_files *ifiles;
639 const char *s1;
641 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
643 const char *t1 = ifiles->filename;
644 s1 = s;
646 if (*s1 != *t1 || *s1 == 0)
647 continue;
649 while (*s1 == *t1 && *s1 != 0)
650 s1++, t1++;
652 /* A match. */
653 if (*s1 == *t1)
654 return 0;
656 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
657 if (strchr (s1, '.') || strchr (t1, '.'))
658 continue;
660 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
661 continue;
663 /* A match. */
664 return 0;
667 /* No matches. */
668 return 1;
671 void
672 note_got_semicolon (type)
673 tree type;
675 if (!TYPE_P (type))
676 my_friendly_abort (60);
677 if (CLASS_TYPE_P (type))
678 CLASSTYPE_GOT_SEMICOLON (type) = 1;
681 void
682 note_list_got_semicolon (declspecs)
683 tree declspecs;
685 tree link;
687 for (link = declspecs; link; link = TREE_CHAIN (link))
689 tree type = TREE_VALUE (link);
690 if (TYPE_P (type))
691 note_got_semicolon (type);
693 clear_anon_tags ();
697 /* Parse a #pragma whose sole argument is a string constant.
698 If OPT is true, the argument is optional. */
699 static tree
700 parse_strconst_pragma (name, opt)
701 const char *name;
702 int opt;
704 tree result, x;
705 enum cpp_ttype t;
707 t = c_lex (&x);
708 if (t == CPP_STRING)
710 result = x;
711 if (c_lex (&x) != CPP_EOF)
712 warning ("junk at end of #pragma %s", name);
713 return result;
716 if (t == CPP_EOF && opt)
717 return 0;
719 error ("invalid #pragma %s", name);
720 return (tree)-1;
723 static void
724 handle_pragma_vtable (dfile)
725 cpp_reader *dfile ATTRIBUTE_UNUSED;
727 parse_strconst_pragma ("vtable", 0);
728 sorry ("#pragma vtable no longer supported");
731 static void
732 handle_pragma_unit (dfile)
733 cpp_reader *dfile ATTRIBUTE_UNUSED;
735 /* Validate syntax, but don't do anything. */
736 parse_strconst_pragma ("unit", 0);
739 static void
740 handle_pragma_interface (dfile)
741 cpp_reader *dfile ATTRIBUTE_UNUSED;
743 tree fname = parse_strconst_pragma ("interface", 1);
744 struct c_fileinfo *finfo;
745 const char *main_filename;
747 if (fname == (tree)-1)
748 return;
749 else if (fname == 0)
750 main_filename = lbasename (input_filename);
751 else
752 main_filename = TREE_STRING_POINTER (fname);
754 finfo = get_fileinfo (input_filename);
756 if (impl_file_chain == 0)
758 /* If this is zero at this point, then we are
759 auto-implementing. */
760 if (main_input_filename == 0)
761 main_input_filename = input_filename;
764 interface_only = interface_strcmp (main_filename);
765 #ifdef MULTIPLE_SYMBOL_SPACES
766 if (! interface_only)
767 #endif
768 interface_unknown = 0;
770 finfo->interface_only = interface_only;
771 finfo->interface_unknown = interface_unknown;
774 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
775 We used to only allow this at toplevel, but that restriction was buggy
776 in older compilers and it seems reasonable to allow it in the headers
777 themselves, too. It only needs to precede the matching #p interface.
779 We don't touch interface_only or interface_unknown; the user must specify
780 a matching #p interface for this to have any effect. */
782 static void
783 handle_pragma_implementation (dfile)
784 cpp_reader *dfile ATTRIBUTE_UNUSED;
786 tree fname = parse_strconst_pragma ("implementation", 1);
787 const char *main_filename;
788 struct impl_files *ifiles = impl_file_chain;
790 if (fname == (tree)-1)
791 return;
793 if (fname == 0)
795 if (main_input_filename)
796 main_filename = main_input_filename;
797 else
798 main_filename = input_filename;
799 main_filename = lbasename (main_filename);
801 else
803 main_filename = TREE_STRING_POINTER (fname);
804 if (cpp_included (parse_in, main_filename))
805 warning ("#pragma implementation for %s appears after file is included",
806 main_filename);
809 for (; ifiles; ifiles = ifiles->next)
811 if (! strcmp (ifiles->filename, main_filename))
812 break;
814 if (ifiles == 0)
816 ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
817 ifiles->filename = main_filename;
818 ifiles->next = impl_file_chain;
819 impl_file_chain = ifiles;
823 /* Indicate that this file uses Java-personality exception handling. */
824 static void
825 handle_pragma_java_exceptions (dfile)
826 cpp_reader *dfile ATTRIBUTE_UNUSED;
828 tree x;
829 if (c_lex (&x) != CPP_EOF)
830 warning ("junk at end of #pragma GCC java_exceptions");
832 choose_personality_routine (lang_java);
835 void
836 do_pending_lang_change ()
838 for (; pending_lang_change > 0; --pending_lang_change)
839 push_lang_context (lang_name_c);
840 for (; pending_lang_change < 0; ++pending_lang_change)
841 pop_lang_context ();
844 /* Return true if d is in a global scope. */
846 static int
847 is_global (d)
848 tree d;
850 while (1)
851 switch (TREE_CODE (d))
853 case ERROR_MARK:
854 return 1;
856 case OVERLOAD: d = OVL_FUNCTION (d); continue;
857 case TREE_LIST: d = TREE_VALUE (d); continue;
858 default:
859 my_friendly_assert (DECL_P (d), 980629);
861 return DECL_NAMESPACE_SCOPE_P (d);
865 tree
866 do_identifier (token, parsing, args)
867 register tree token;
868 int parsing;
869 tree args;
871 register tree id;
873 id = lookup_name (token, 0);
875 /* Do Koenig lookup if appropriate (inside templates we build lookup
876 expressions instead).
878 [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
879 finds the declaration of a class member function, the associated
880 namespaces and classes are not considered. */
882 if (args && !current_template_parms && (!id || is_global (id)))
883 id = lookup_arg_dependent (token, id, args);
885 /* Remember that this name has been used in the class definition, as per
886 [class.scope0] */
887 if (id && parsing)
888 maybe_note_name_used_in_class (token, id);
890 if (!id || (TREE_CODE (id) == FUNCTION_DECL
891 && DECL_ANTICIPATED (id)))
893 unqualified_name_lookup_error (token);
894 return error_mark_node;
897 id = check_for_out_of_scope_variable (id);
899 /* TREE_USED is set in `hack_identifier'. */
900 if (TREE_CODE (id) == CONST_DECL)
902 if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
903 id = DECL_INITIAL (id);
905 else
906 id = hack_identifier (id, token);
908 /* We must look up dependent names when the template is
909 instantiated, not while parsing it. For now, we don't
910 distinguish between dependent and independent names. So, for
911 example, we look up all overloaded functions at
912 instantiation-time, even though in some cases we should just use
913 the DECL we have here. We also use LOOKUP_EXPRs to find things
914 like local variables, rather than creating TEMPLATE_DECLs for the
915 local variables and then finding matching instantiations. */
916 if (current_template_parms
917 && (is_overloaded_fn (id)
918 || (TREE_CODE (id) == VAR_DECL
919 && CP_DECL_CONTEXT (id)
920 && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
921 || TREE_CODE (id) == PARM_DECL
922 || TREE_CODE (id) == RESULT_DECL
923 || TREE_CODE (id) == USING_DECL))
924 id = build_min_nt (LOOKUP_EXPR, token);
926 return id;
929 tree
930 do_scoped_id (token, parsing)
931 tree token;
932 int parsing;
934 tree id;
935 /* during parsing, this is ::name. Otherwise, it is black magic. */
936 if (parsing)
938 id = make_node (CPLUS_BINDING);
939 if (!qualified_lookup_using_namespace (token, global_namespace, id, 0))
940 id = NULL_TREE;
941 else
942 id = BINDING_VALUE (id);
944 else
945 id = IDENTIFIER_GLOBAL_VALUE (token);
946 if (! id)
948 if (processing_template_decl)
950 id = build_min_nt (LOOKUP_EXPR, token);
951 LOOKUP_EXPR_GLOBAL (id) = 1;
952 return id;
954 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
955 cp_error ("`::%D' undeclared (first use here)", token);
956 id = error_mark_node;
957 /* Prevent repeated error messages. */
958 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
960 else
962 if (TREE_CODE (id) == ADDR_EXPR)
963 mark_used (TREE_OPERAND (id, 0));
964 else if (TREE_CODE (id) != OVERLOAD)
965 mark_used (id);
967 if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
969 /* XXX CHS - should we set TREE_USED of the constant? */
970 id = DECL_INITIAL (id);
971 /* This is to prevent an enum whose value is 0
972 from being considered a null pointer constant. */
973 id = build1 (NOP_EXPR, TREE_TYPE (id), id);
974 TREE_CONSTANT (id) = 1;
977 if (processing_template_decl)
979 if (is_overloaded_fn (id))
981 id = build_min_nt (LOOKUP_EXPR, token);
982 LOOKUP_EXPR_GLOBAL (id) = 1;
983 return id;
985 /* else just use the decl */
987 return convert_from_reference (id);
990 tree
991 identifier_typedecl_value (node)
992 tree node;
994 tree t, type;
995 type = IDENTIFIER_TYPE_VALUE (node);
996 if (type == NULL_TREE)
997 return NULL_TREE;
999 if (IDENTIFIER_BINDING (node))
1001 t = IDENTIFIER_VALUE (node);
1002 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1003 return t;
1005 if (IDENTIFIER_NAMESPACE_VALUE (node))
1007 t = IDENTIFIER_NAMESPACE_VALUE (node);
1008 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1009 return t;
1012 /* Will this one ever happen? */
1013 if (TYPE_MAIN_DECL (type))
1014 return TYPE_MAIN_DECL (type);
1016 /* We used to do an internal error of 62 here, but instead we will
1017 handle the return of a null appropriately in the callers. */
1018 return NULL_TREE;
1021 #ifdef GATHER_STATISTICS
1022 /* The original for tree_node_kind is in the toplevel tree.c; changes there
1023 need to be brought into here, unless this were actually put into a header
1024 instead. */
1025 /* Statistics-gathering stuff. */
1026 typedef enum
1028 d_kind,
1029 t_kind,
1030 b_kind,
1031 s_kind,
1032 r_kind,
1033 e_kind,
1034 c_kind,
1035 id_kind,
1036 op_id_kind,
1037 perm_list_kind,
1038 temp_list_kind,
1039 vec_kind,
1040 x_kind,
1041 lang_decl,
1042 lang_type,
1043 all_kinds
1044 } tree_node_kind;
1046 extern int tree_node_counts[];
1047 extern int tree_node_sizes[];
1048 #endif
1050 tree
1051 build_lang_decl (code, name, type)
1052 enum tree_code code;
1053 tree name;
1054 tree type;
1056 tree t;
1058 t = build_decl (code, name, type);
1059 retrofit_lang_decl (t);
1061 return t;
1064 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
1065 and pushdecl (for functions generated by the backend). */
1067 void
1068 retrofit_lang_decl (t)
1069 tree t;
1071 struct lang_decl *ld;
1072 size_t size;
1074 if (CAN_HAVE_FULL_LANG_DECL_P (t))
1075 size = sizeof (struct lang_decl);
1076 else
1077 size = sizeof (struct lang_decl_flags);
1079 ld = (struct lang_decl *) ggc_alloc_cleared (size);
1081 DECL_LANG_SPECIFIC (t) = ld;
1082 if (current_lang_name == lang_name_cplusplus)
1083 SET_DECL_LANGUAGE (t, lang_cplusplus);
1084 else if (current_lang_name == lang_name_c)
1085 SET_DECL_LANGUAGE (t, lang_c);
1086 else if (current_lang_name == lang_name_java)
1087 SET_DECL_LANGUAGE (t, lang_java);
1088 else my_friendly_abort (64);
1090 #ifdef GATHER_STATISTICS
1091 tree_node_counts[(int)lang_decl] += 1;
1092 tree_node_sizes[(int)lang_decl] += size;
1093 #endif
1096 void
1097 copy_lang_decl (node)
1098 tree node;
1100 int size;
1101 struct lang_decl *ld;
1103 if (! DECL_LANG_SPECIFIC (node))
1104 return;
1106 if (!CAN_HAVE_FULL_LANG_DECL_P (node))
1107 size = sizeof (struct lang_decl_flags);
1108 else
1109 size = sizeof (struct lang_decl);
1110 ld = (struct lang_decl *) ggc_alloc (size);
1111 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1112 DECL_LANG_SPECIFIC (node) = ld;
1115 /* Copy DECL, including any language-specific parts. */
1117 tree
1118 copy_decl (decl)
1119 tree decl;
1121 tree copy;
1123 copy = copy_node (decl);
1124 copy_lang_decl (copy);
1125 return copy;
1128 tree
1129 cp_make_lang_type (code)
1130 enum tree_code code;
1132 register tree t = make_node (code);
1134 /* Set up some flags that give proper default behavior. */
1135 if (IS_AGGR_TYPE_CODE (code))
1137 struct lang_type *pi;
1139 pi = ((struct lang_type *)
1140 ggc_alloc_cleared (sizeof (struct lang_type)));
1142 TYPE_LANG_SPECIFIC (t) = pi;
1143 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1144 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1146 /* Make sure this is laid out, for ease of use later. In the
1147 presence of parse errors, the normal was of assuring this
1148 might not ever get executed, so we lay it out *immediately*. */
1149 build_pointer_type (t);
1151 #ifdef GATHER_STATISTICS
1152 tree_node_counts[(int)lang_type] += 1;
1153 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1154 #endif
1156 else
1157 /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But,
1158 TYPE_ALIAS_SET is initialized to -1 by default, so we must
1159 clear it here. */
1160 TYPE_ALIAS_SET (t) = 0;
1162 /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1163 since they can be virtual base types, and we then need a
1164 canonical binfo for them. Ideally, this would be done lazily for
1165 all types. */
1166 if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM)
1167 TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1169 return t;
1172 tree
1173 make_aggr_type (code)
1174 enum tree_code code;
1176 tree t = cp_make_lang_type (code);
1178 if (IS_AGGR_TYPE_CODE (code))
1179 SET_IS_AGGR_TYPE (t, 1);
1181 return t;
1184 void
1185 compiler_error VPARAMS ((const char *msg, ...))
1187 #ifndef ANSI_PROTOTYPES
1188 const char *msg;
1189 #endif
1190 char buf[1024];
1191 va_list ap;
1193 VA_START (ap, msg);
1195 #ifndef ANSI_PROTOTYPES
1196 msg = va_arg (ap, const char *);
1197 #endif
1199 vsprintf (buf, msg, ap);
1200 va_end (ap);
1201 error_with_file_and_line (input_filename, lineno, "%s (compiler error)", buf);
1204 /* Return the type-qualifier corresponding to the identifier given by
1205 RID. */
1208 cp_type_qual_from_rid (rid)
1209 tree rid;
1211 if (rid == ridpointers[(int) RID_CONST])
1212 return TYPE_QUAL_CONST;
1213 else if (rid == ridpointers[(int) RID_VOLATILE])
1214 return TYPE_QUAL_VOLATILE;
1215 else if (rid == ridpointers[(int) RID_RESTRICT])
1216 return TYPE_QUAL_RESTRICT;
1218 my_friendly_abort (0);
1219 return TYPE_UNQUALIFIED;