2003-03-15 Glen Nakamura <glen@imodulo.com>
[official-gcc.git] / gcc / cp / lex.c
blob6433f2b1e0bf6dd69fe94c6741ac78c97aa6d34e
1 /* Separate lexical analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; 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 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "input.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "cpplib.h"
34 #include "lex.h"
35 #include "flags.h"
36 #include "c-pragma.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "tm_p.h"
40 #include "timevar.h"
41 #include "diagnostic.h"
43 #ifdef MULTIBYTE_CHARS
44 #include "mbchar.h"
45 #include <locale.h>
46 #endif
48 static int interface_strcmp PARAMS ((const char *));
49 static void init_cp_pragma PARAMS ((void));
51 static tree parse_strconst_pragma PARAMS ((const char *, int));
52 static void handle_pragma_vtable PARAMS ((cpp_reader *));
53 static void handle_pragma_unit PARAMS ((cpp_reader *));
54 static void handle_pragma_interface PARAMS ((cpp_reader *));
55 static void handle_pragma_implementation PARAMS ((cpp_reader *));
56 static void handle_pragma_java_exceptions PARAMS ((cpp_reader *));
58 static int is_global PARAMS ((tree));
59 static void init_operators PARAMS ((void));
60 static void copy_lang_type PARAMS ((tree));
62 /* A constraint that can be tested at compile time. */
63 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
65 /* Functions and data structures for #pragma interface.
67 `#pragma implementation' means that the main file being compiled
68 is considered to implement (provide) the classes that appear in
69 its main body. I.e., if this is file "foo.cc", and class `bar'
70 is defined in "foo.cc", then we say that "foo.cc implements bar".
72 All main input files "implement" themselves automagically.
74 `#pragma interface' means that unless this file (of the form "foo.h"
75 is not presently being included by file "foo.cc", the
76 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
77 of the vtables nor any of the inline functions defined in foo.h
78 will ever be output.
80 There are cases when we want to link files such as "defs.h" and
81 "main.cc". In this case, we give "defs.h" a `#pragma interface',
82 and "main.cc" has `#pragma implementation "defs.h"'. */
84 struct impl_files
86 const char *filename;
87 struct impl_files *next;
90 static struct impl_files *impl_file_chain;
93 /* Return something to represent absolute declarators containing a *.
94 TARGET is the absolute declarator that the * contains.
95 CV_QUALIFIERS is a list of modifiers such as const or volatile
96 to apply to the pointer type, represented as identifiers.
98 We return an INDIRECT_REF whose "contents" are TARGET
99 and whose type is the modifier list. */
101 tree
102 make_pointer_declarator (cv_qualifiers, target)
103 tree cv_qualifiers, target;
105 if (target && TREE_CODE (target) == IDENTIFIER_NODE
106 && ANON_AGGRNAME_P (target))
107 error ("type name expected before `*'");
108 target = build_nt (INDIRECT_REF, target);
109 TREE_TYPE (target) = cv_qualifiers;
110 return target;
113 /* Return something to represent absolute declarators containing a &.
114 TARGET is the absolute declarator that the & contains.
115 CV_QUALIFIERS is a list of modifiers such as const or volatile
116 to apply to the reference type, represented as identifiers.
118 We return an ADDR_EXPR whose "contents" are TARGET
119 and whose type is the modifier list. */
121 tree
122 make_reference_declarator (cv_qualifiers, target)
123 tree cv_qualifiers, target;
125 target = build_nt (ADDR_EXPR, target);
126 TREE_TYPE (target) = cv_qualifiers;
127 return target;
130 tree
131 make_call_declarator (target, parms, cv_qualifiers, exception_specification)
132 tree target, parms, cv_qualifiers, exception_specification;
134 target = build_nt (CALL_EXPR, target,
135 tree_cons (parms, cv_qualifiers, NULL_TREE),
136 /* The third operand is really RTL. We
137 shouldn't put anything there. */
138 NULL_TREE);
139 CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification;
140 return target;
143 void
144 set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification)
145 tree call_declarator, cv_qualifiers, exception_specification;
147 CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
148 CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
151 int interface_only; /* whether or not current file is only for
152 interface definitions. */
153 int interface_unknown; /* whether or not we know this class
154 to behave according to #pragma interface. */
157 /* Initialization before switch parsing. */
158 void
159 cxx_init_options ()
161 c_common_init_options (clk_cplusplus);
163 /* Default exceptions on. */
164 flag_exceptions = 1;
165 /* By default wrap lines at 80 characters. Is getenv ("COLUMNS")
166 preferable? */
167 diagnostic_line_cutoff (global_dc) = 80;
168 /* By default, emit location information once for every
169 diagnostic message. */
170 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
173 void
174 cxx_finish ()
176 c_common_finish ();
179 /* A mapping from tree codes to operator name information. */
180 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
181 /* Similar, but for assignment operators. */
182 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
184 /* Initialize data structures that keep track of operator names. */
186 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
187 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
188 #include "operators.def"
189 #undef DEF_OPERATOR
191 static void
192 init_operators ()
194 tree identifier;
195 char buffer[256];
196 struct operator_name_info_t *oni;
198 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
199 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
200 identifier = get_identifier (buffer); \
201 IDENTIFIER_OPNAME_P (identifier) = 1; \
203 oni = (ASSN_P \
204 ? &assignment_operator_name_info[(int) CODE] \
205 : &operator_name_info[(int) CODE]); \
206 oni->identifier = identifier; \
207 oni->name = NAME; \
208 oni->mangled_name = MANGLING; \
209 oni->arity = ARITY;
211 #include "operators.def"
212 #undef DEF_OPERATOR
214 operator_name_info[(int) ERROR_MARK].identifier
215 = get_identifier ("<invalid operator>");
217 /* Handle some special cases. These operators are not defined in
218 the language, but can be produced internally. We may need them
219 for error-reporting. (Eventually, we should ensure that this
220 does not happen. Error messages involving these operators will
221 be confusing to users.) */
223 operator_name_info [(int) INIT_EXPR].name
224 = operator_name_info [(int) MODIFY_EXPR].name;
225 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
226 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
227 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
228 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
229 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
230 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
231 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
232 operator_name_info [(int) ABS_EXPR].name = "abs";
233 operator_name_info [(int) FFS_EXPR].name = "ffs";
234 operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
235 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
236 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
237 operator_name_info [(int) IN_EXPR].name = "in";
238 operator_name_info [(int) RANGE_EXPR].name = "...";
239 operator_name_info [(int) CONVERT_EXPR].name = "+";
241 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
242 = "(exact /=)";
243 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
244 = "(ceiling /=)";
245 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
246 = "(floor /=)";
247 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
248 = "(round /=)";
249 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
250 = "(ceiling %=)";
251 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
252 = "(floor %=)";
253 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
254 = "(round %=)";
257 /* The reserved keyword table. */
258 struct resword
260 const char *const word;
261 const ENUM_BITFIELD(rid) rid : 16;
262 const unsigned int disable : 16;
265 /* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is
266 _true_. */
267 #define D_EXT 0x01 /* GCC extension */
268 #define D_ASM 0x02 /* in C99, but has a switch to turn it off */
270 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
272 static const struct resword reswords[] =
274 { "_Complex", RID_COMPLEX, 0 },
275 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
276 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
277 { "__alignof", RID_ALIGNOF, 0 },
278 { "__alignof__", RID_ALIGNOF, 0 },
279 { "__asm", RID_ASM, 0 },
280 { "__asm__", RID_ASM, 0 },
281 { "__attribute", RID_ATTRIBUTE, 0 },
282 { "__attribute__", RID_ATTRIBUTE, 0 },
283 { "__builtin_va_arg", RID_VA_ARG, 0 },
284 { "__complex", RID_COMPLEX, 0 },
285 { "__complex__", RID_COMPLEX, 0 },
286 { "__const", RID_CONST, 0 },
287 { "__const__", RID_CONST, 0 },
288 { "__extension__", RID_EXTENSION, 0 },
289 { "__func__", RID_C99_FUNCTION_NAME, 0 },
290 { "__imag", RID_IMAGPART, 0 },
291 { "__imag__", RID_IMAGPART, 0 },
292 { "__inline", RID_INLINE, 0 },
293 { "__inline__", RID_INLINE, 0 },
294 { "__label__", RID_LABEL, 0 },
295 { "__null", RID_NULL, 0 },
296 { "__real", RID_REALPART, 0 },
297 { "__real__", RID_REALPART, 0 },
298 { "__restrict", RID_RESTRICT, 0 },
299 { "__restrict__", RID_RESTRICT, 0 },
300 { "__signed", RID_SIGNED, 0 },
301 { "__signed__", RID_SIGNED, 0 },
302 { "__thread", RID_THREAD, 0 },
303 { "__typeof", RID_TYPEOF, 0 },
304 { "__typeof__", RID_TYPEOF, 0 },
305 { "__volatile", RID_VOLATILE, 0 },
306 { "__volatile__", RID_VOLATILE, 0 },
307 { "asm", RID_ASM, D_ASM },
308 { "auto", RID_AUTO, 0 },
309 { "bool", RID_BOOL, 0 },
310 { "break", RID_BREAK, 0 },
311 { "case", RID_CASE, 0 },
312 { "catch", RID_CATCH, 0 },
313 { "char", RID_CHAR, 0 },
314 { "class", RID_CLASS, 0 },
315 { "const", RID_CONST, 0 },
316 { "const_cast", RID_CONSTCAST, 0 },
317 { "continue", RID_CONTINUE, 0 },
318 { "default", RID_DEFAULT, 0 },
319 { "delete", RID_DELETE, 0 },
320 { "do", RID_DO, 0 },
321 { "double", RID_DOUBLE, 0 },
322 { "dynamic_cast", RID_DYNCAST, 0 },
323 { "else", RID_ELSE, 0 },
324 { "enum", RID_ENUM, 0 },
325 { "explicit", RID_EXPLICIT, 0 },
326 { "export", RID_EXPORT, 0 },
327 { "extern", RID_EXTERN, 0 },
328 { "false", RID_FALSE, 0 },
329 { "float", RID_FLOAT, 0 },
330 { "for", RID_FOR, 0 },
331 { "friend", RID_FRIEND, 0 },
332 { "goto", RID_GOTO, 0 },
333 { "if", RID_IF, 0 },
334 { "inline", RID_INLINE, 0 },
335 { "int", RID_INT, 0 },
336 { "long", RID_LONG, 0 },
337 { "mutable", RID_MUTABLE, 0 },
338 { "namespace", RID_NAMESPACE, 0 },
339 { "new", RID_NEW, 0 },
340 { "operator", RID_OPERATOR, 0 },
341 { "private", RID_PRIVATE, 0 },
342 { "protected", RID_PROTECTED, 0 },
343 { "public", RID_PUBLIC, 0 },
344 { "register", RID_REGISTER, 0 },
345 { "reinterpret_cast", RID_REINTCAST, 0 },
346 { "return", RID_RETURN, 0 },
347 { "short", RID_SHORT, 0 },
348 { "signed", RID_SIGNED, 0 },
349 { "sizeof", RID_SIZEOF, 0 },
350 { "static", RID_STATIC, 0 },
351 { "static_cast", RID_STATCAST, 0 },
352 { "struct", RID_STRUCT, 0 },
353 { "switch", RID_SWITCH, 0 },
354 { "template", RID_TEMPLATE, 0 },
355 { "this", RID_THIS, 0 },
356 { "throw", RID_THROW, 0 },
357 { "true", RID_TRUE, 0 },
358 { "try", RID_TRY, 0 },
359 { "typedef", RID_TYPEDEF, 0 },
360 { "typename", RID_TYPENAME, 0 },
361 { "typeid", RID_TYPEID, 0 },
362 { "typeof", RID_TYPEOF, D_ASM|D_EXT },
363 { "union", RID_UNION, 0 },
364 { "unsigned", RID_UNSIGNED, 0 },
365 { "using", RID_USING, 0 },
366 { "virtual", RID_VIRTUAL, 0 },
367 { "void", RID_VOID, 0 },
368 { "volatile", RID_VOLATILE, 0 },
369 { "wchar_t", RID_WCHAR, 0 },
370 { "while", RID_WHILE, 0 },
374 void
375 init_reswords ()
377 unsigned int i;
378 tree id;
379 int mask = ((flag_no_asm ? D_ASM : 0)
380 | (flag_no_gnu_keywords ? D_EXT : 0));
382 ridpointers = (tree *) ggc_calloc ((int) RID_MAX, sizeof (tree));
383 for (i = 0; i < ARRAY_SIZE (reswords); i++)
385 id = get_identifier (reswords[i].word);
386 C_RID_CODE (id) = reswords[i].rid;
387 ridpointers [(int) reswords[i].rid] = id;
388 if (! (reswords[i].disable & mask))
389 C_IS_RESERVED_WORD (id) = 1;
393 static void
394 init_cp_pragma ()
396 c_register_pragma (0, "vtable", handle_pragma_vtable);
397 c_register_pragma (0, "unit", handle_pragma_unit);
398 c_register_pragma (0, "interface", handle_pragma_interface);
399 c_register_pragma (0, "implementation", handle_pragma_implementation);
400 c_register_pragma ("GCC", "interface", handle_pragma_interface);
401 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
402 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
405 /* Initialize the C++ front end. This function is very sensitive to
406 the exact order that things are done here. It would be nice if the
407 initialization done by this routine were moved to its subroutines,
408 and the ordering dependencies clarified and reduced. */
409 bool
410 cxx_init (void)
412 input_filename = "<internal>";
414 init_reswords ();
415 init_tree ();
416 init_cp_semantics ();
417 init_operators ();
418 init_method ();
419 init_error ();
421 current_function_decl = NULL;
423 class_type_node = build_int_2 (class_type, 0);
424 TREE_TYPE (class_type_node) = class_type_node;
425 ridpointers[(int) RID_CLASS] = class_type_node;
427 record_type_node = build_int_2 (record_type, 0);
428 TREE_TYPE (record_type_node) = record_type_node;
429 ridpointers[(int) RID_STRUCT] = record_type_node;
431 union_type_node = build_int_2 (union_type, 0);
432 TREE_TYPE (union_type_node) = union_type_node;
433 ridpointers[(int) RID_UNION] = union_type_node;
435 enum_type_node = build_int_2 (enum_type, 0);
436 TREE_TYPE (enum_type_node) = enum_type_node;
437 ridpointers[(int) RID_ENUM] = enum_type_node;
439 cxx_init_decl_processing ();
441 /* Create the built-in __null node. */
442 null_node = build_int_2 (0, 0);
443 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
444 ridpointers[RID_NULL] = null_node;
446 interface_unknown = 1;
448 if (c_common_init () == false)
449 return false;
451 init_cp_pragma ();
453 init_repo (main_input_filename);
455 return true;
458 /* Helper function to load global variables with interface
459 information. */
461 void
462 extract_interface_info ()
464 struct c_fileinfo *finfo = 0;
466 if (flag_alt_external_templates)
468 tree til = tinst_for_decl ();
470 if (til)
471 finfo = get_fileinfo (TINST_FILE (til));
473 if (!finfo)
474 finfo = get_fileinfo (input_filename);
476 interface_only = finfo->interface_only;
477 interface_unknown = finfo->interface_unknown;
480 /* Return nonzero if S is not considered part of an
481 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
483 static int
484 interface_strcmp (s)
485 const char *s;
487 /* Set the interface/implementation bits for this scope. */
488 struct impl_files *ifiles;
489 const char *s1;
491 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
493 const char *t1 = ifiles->filename;
494 s1 = s;
496 if (*s1 != *t1 || *s1 == 0)
497 continue;
499 while (*s1 == *t1 && *s1 != 0)
500 s1++, t1++;
502 /* A match. */
503 if (*s1 == *t1)
504 return 0;
506 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
507 if (strchr (s1, '.') || strchr (t1, '.'))
508 continue;
510 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
511 continue;
513 /* A match. */
514 return 0;
517 /* No matches. */
518 return 1;
521 void
522 note_got_semicolon (type)
523 tree type;
525 if (!TYPE_P (type))
526 abort ();
527 if (CLASS_TYPE_P (type))
528 CLASSTYPE_GOT_SEMICOLON (type) = 1;
531 void
532 note_list_got_semicolon (declspecs)
533 tree declspecs;
535 tree link;
537 for (link = declspecs; link; link = TREE_CHAIN (link))
539 tree type = TREE_VALUE (link);
540 if (type && TYPE_P (type))
541 note_got_semicolon (type);
543 clear_anon_tags ();
547 /* Parse a #pragma whose sole argument is a string constant.
548 If OPT is true, the argument is optional. */
549 static tree
550 parse_strconst_pragma (name, opt)
551 const char *name;
552 int opt;
554 tree result, x;
555 enum cpp_ttype t;
557 t = c_lex (&x);
558 if (t == CPP_STRING)
560 result = x;
561 if (c_lex (&x) != CPP_EOF)
562 warning ("junk at end of #pragma %s", name);
563 return result;
566 if (t == CPP_EOF && opt)
567 return 0;
569 error ("invalid #pragma %s", name);
570 return (tree)-1;
573 static void
574 handle_pragma_vtable (dfile)
575 cpp_reader *dfile ATTRIBUTE_UNUSED;
577 parse_strconst_pragma ("vtable", 0);
578 sorry ("#pragma vtable no longer supported");
581 static void
582 handle_pragma_unit (dfile)
583 cpp_reader *dfile ATTRIBUTE_UNUSED;
585 /* Validate syntax, but don't do anything. */
586 parse_strconst_pragma ("unit", 0);
589 static void
590 handle_pragma_interface (dfile)
591 cpp_reader *dfile ATTRIBUTE_UNUSED;
593 tree fname = parse_strconst_pragma ("interface", 1);
594 struct c_fileinfo *finfo;
595 const char *main_filename;
597 if (fname == (tree)-1)
598 return;
599 else if (fname == 0)
600 main_filename = lbasename (input_filename);
601 else
602 main_filename = TREE_STRING_POINTER (fname);
604 finfo = get_fileinfo (input_filename);
606 if (impl_file_chain == 0)
608 /* If this is zero at this point, then we are
609 auto-implementing. */
610 if (main_input_filename == 0)
611 main_input_filename = input_filename;
614 interface_only = interface_strcmp (main_filename);
615 #ifdef MULTIPLE_SYMBOL_SPACES
616 if (! interface_only)
617 #endif
618 interface_unknown = 0;
620 finfo->interface_only = interface_only;
621 finfo->interface_unknown = interface_unknown;
624 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
625 We used to only allow this at toplevel, but that restriction was buggy
626 in older compilers and it seems reasonable to allow it in the headers
627 themselves, too. It only needs to precede the matching #p interface.
629 We don't touch interface_only or interface_unknown; the user must specify
630 a matching #p interface for this to have any effect. */
632 static void
633 handle_pragma_implementation (dfile)
634 cpp_reader *dfile ATTRIBUTE_UNUSED;
636 tree fname = parse_strconst_pragma ("implementation", 1);
637 const char *main_filename;
638 struct impl_files *ifiles = impl_file_chain;
640 if (fname == (tree)-1)
641 return;
643 if (fname == 0)
645 if (main_input_filename)
646 main_filename = main_input_filename;
647 else
648 main_filename = input_filename;
649 main_filename = lbasename (main_filename);
651 else
653 main_filename = TREE_STRING_POINTER (fname);
654 if (cpp_included (parse_in, main_filename))
655 warning ("#pragma implementation for %s appears after file is included",
656 main_filename);
659 for (; ifiles; ifiles = ifiles->next)
661 if (! strcmp (ifiles->filename, main_filename))
662 break;
664 if (ifiles == 0)
666 ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
667 ifiles->filename = main_filename;
668 ifiles->next = impl_file_chain;
669 impl_file_chain = ifiles;
673 /* Indicate that this file uses Java-personality exception handling. */
674 static void
675 handle_pragma_java_exceptions (dfile)
676 cpp_reader *dfile ATTRIBUTE_UNUSED;
678 tree x;
679 if (c_lex (&x) != CPP_EOF)
680 warning ("junk at end of #pragma GCC java_exceptions");
682 choose_personality_routine (lang_java);
685 /* Return true if d is in a global scope. */
687 static int
688 is_global (d)
689 tree d;
691 while (1)
692 switch (TREE_CODE (d))
694 case ERROR_MARK:
695 return 1;
697 case OVERLOAD: d = OVL_FUNCTION (d); continue;
698 case TREE_LIST: d = TREE_VALUE (d); continue;
699 default:
700 my_friendly_assert (DECL_P (d), 980629);
702 return DECL_NAMESPACE_SCOPE_P (d);
706 /* Issue an error message indicating that the lookup of NAME (an
707 IDENTIFIER_NODE) failed. */
709 void
710 unqualified_name_lookup_error (tree name)
712 if (IDENTIFIER_OPNAME_P (name))
714 if (name != ansi_opname (ERROR_MARK))
715 error ("`%D' not defined", name);
717 else if (current_function_decl == 0)
718 error ("`%D' was not declared in this scope", name);
719 else
721 if (IDENTIFIER_NAMESPACE_VALUE (name) != error_mark_node
722 || IDENTIFIER_ERROR_LOCUS (name) != current_function_decl)
724 static int undeclared_variable_notice;
726 error ("`%D' undeclared (first use this function)", name);
728 if (! undeclared_variable_notice)
730 error ("(Each undeclared identifier is reported only once for each function it appears in.)");
731 undeclared_variable_notice = 1;
734 /* Prevent repeated error messages. */
735 SET_IDENTIFIER_NAMESPACE_VALUE (name, error_mark_node);
736 SET_IDENTIFIER_ERROR_LOCUS (name, current_function_decl);
740 tree
741 do_identifier (token, args)
742 register tree token;
743 tree args;
745 register tree id;
747 timevar_push (TV_NAME_LOOKUP);
748 id = lookup_name (token, 0);
750 /* Do Koenig lookup if appropriate (inside templates we build lookup
751 expressions instead).
753 [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
754 finds the declaration of a class member function, the associated
755 namespaces and classes are not considered. */
757 if (args && !current_template_parms && (!id || is_global (id)))
758 id = lookup_arg_dependent (token, id, args);
760 if (id == error_mark_node)
762 /* lookup_name quietly returns error_mark_node if we're parsing,
763 as we don't want to complain about an identifier that ends up
764 being used as a declarator. So we call it again to get the error
765 message. */
766 id = lookup_name (token, 0);
767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
770 if (!id || (TREE_CODE (id) == FUNCTION_DECL
771 && DECL_ANTICIPATED (id)))
773 if (current_template_parms)
774 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
775 build_min_nt (LOOKUP_EXPR, token));
776 else if (IDENTIFIER_TYPENAME_P (token))
777 /* A templated conversion operator might exist. */
778 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, token);
779 else
781 unqualified_name_lookup_error (token);
782 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
786 id = check_for_out_of_scope_variable (id);
788 /* TREE_USED is set in `hack_identifier'. */
789 if (TREE_CODE (id) == CONST_DECL)
791 /* Check access. */
792 if (IDENTIFIER_CLASS_VALUE (token) == id)
793 enforce_access (CP_DECL_CONTEXT(id), id);
794 if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
795 id = DECL_INITIAL (id);
797 else
798 id = hack_identifier (id, token);
800 /* We must look up dependent names when the template is
801 instantiated, not while parsing it. For now, we don't
802 distinguish between dependent and independent names. So, for
803 example, we look up all overloaded functions at
804 instantiation-time, even though in some cases we should just use
805 the DECL we have here. We also use LOOKUP_EXPRs to find things
806 like local variables, rather than creating TEMPLATE_DECLs for the
807 local variables and then finding matching instantiations. */
808 if (current_template_parms
809 && (is_overloaded_fn (id)
810 || (TREE_CODE (id) == VAR_DECL
811 && CP_DECL_CONTEXT (id)
812 && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
813 || TREE_CODE (id) == PARM_DECL
814 || TREE_CODE (id) == RESULT_DECL
815 || TREE_CODE (id) == USING_DECL))
816 id = build_min_nt (LOOKUP_EXPR, token);
818 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
821 tree
822 do_scoped_id (token, id)
823 tree token;
824 tree id;
826 timevar_push (TV_NAME_LOOKUP);
827 if (!id || (TREE_CODE (id) == FUNCTION_DECL
828 && DECL_ANTICIPATED (id)))
830 if (processing_template_decl)
832 id = build_min_nt (LOOKUP_EXPR, token);
833 LOOKUP_EXPR_GLOBAL (id) = 1;
834 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
836 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
837 error ("`::%D' undeclared (first use here)", token);
838 id = error_mark_node;
839 /* Prevent repeated error messages. */
840 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
842 else
844 if (TREE_CODE (id) == ADDR_EXPR)
845 mark_used (TREE_OPERAND (id, 0));
846 else if (TREE_CODE (id) != OVERLOAD)
847 mark_used (id);
849 if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
851 /* XXX CHS - should we set TREE_USED of the constant? */
852 id = DECL_INITIAL (id);
853 /* This is to prevent an enum whose value is 0
854 from being considered a null pointer constant. */
855 id = build1 (NOP_EXPR, TREE_TYPE (id), id);
856 TREE_CONSTANT (id) = 1;
859 if (processing_template_decl)
861 if (is_overloaded_fn (id))
863 id = build_min_nt (LOOKUP_EXPR, token);
864 LOOKUP_EXPR_GLOBAL (id) = 1;
865 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
867 /* else just use the decl */
869 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, convert_from_reference (id));
872 tree
873 identifier_typedecl_value (node)
874 tree node;
876 tree t, type;
877 type = IDENTIFIER_TYPE_VALUE (node);
878 if (type == NULL_TREE)
879 return NULL_TREE;
881 if (IDENTIFIER_BINDING (node))
883 t = IDENTIFIER_VALUE (node);
884 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
885 return t;
887 if (IDENTIFIER_NAMESPACE_VALUE (node))
889 t = IDENTIFIER_NAMESPACE_VALUE (node);
890 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
891 return t;
894 /* Will this one ever happen? */
895 if (TYPE_MAIN_DECL (type))
896 return TYPE_MAIN_DECL (type);
898 /* We used to do an internal error of 62 here, but instead we will
899 handle the return of a null appropriately in the callers. */
900 return NULL_TREE;
903 #ifdef GATHER_STATISTICS
904 /* The original for tree_node_kind is in the toplevel tree.c; changes there
905 need to be brought into here, unless this were actually put into a header
906 instead. */
907 /* Statistics-gathering stuff. */
908 typedef enum
910 d_kind,
911 t_kind,
912 b_kind,
913 s_kind,
914 r_kind,
915 e_kind,
916 c_kind,
917 id_kind,
918 op_id_kind,
919 perm_list_kind,
920 temp_list_kind,
921 vec_kind,
922 x_kind,
923 lang_decl,
924 lang_type,
925 all_kinds
926 } tree_node_kind;
928 extern int tree_node_counts[];
929 extern int tree_node_sizes[];
930 #endif
932 tree
933 build_lang_decl (code, name, type)
934 enum tree_code code;
935 tree name;
936 tree type;
938 tree t;
940 t = build_decl (code, name, type);
941 retrofit_lang_decl (t);
943 return t;
946 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
947 and pushdecl (for functions generated by the backend). */
949 void
950 retrofit_lang_decl (t)
951 tree t;
953 struct lang_decl *ld;
954 size_t size;
956 if (CAN_HAVE_FULL_LANG_DECL_P (t))
957 size = sizeof (struct lang_decl);
958 else
959 size = sizeof (struct lang_decl_flags);
961 ld = (struct lang_decl *) ggc_alloc_cleared (size);
963 ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
964 ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
965 ld->decl_flags.u2sel = 0;
966 if (ld->decl_flags.can_be_full)
967 ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
969 DECL_LANG_SPECIFIC (t) = ld;
970 if (current_lang_name == lang_name_cplusplus)
971 SET_DECL_LANGUAGE (t, lang_cplusplus);
972 else if (current_lang_name == lang_name_c)
973 SET_DECL_LANGUAGE (t, lang_c);
974 else if (current_lang_name == lang_name_java)
975 SET_DECL_LANGUAGE (t, lang_java);
976 else abort ();
978 #ifdef GATHER_STATISTICS
979 tree_node_counts[(int)lang_decl] += 1;
980 tree_node_sizes[(int)lang_decl] += size;
981 #endif
984 void
985 cxx_dup_lang_specific_decl (node)
986 tree node;
988 int size;
989 struct lang_decl *ld;
991 if (! DECL_LANG_SPECIFIC (node))
992 return;
994 if (!CAN_HAVE_FULL_LANG_DECL_P (node))
995 size = sizeof (struct lang_decl_flags);
996 else
997 size = sizeof (struct lang_decl);
998 ld = (struct lang_decl *) ggc_alloc (size);
999 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1000 DECL_LANG_SPECIFIC (node) = ld;
1002 #ifdef GATHER_STATISTICS
1003 tree_node_counts[(int)lang_decl] += 1;
1004 tree_node_sizes[(int)lang_decl] += size;
1005 #endif
1008 /* Copy DECL, including any language-specific parts. */
1010 tree
1011 copy_decl (decl)
1012 tree decl;
1014 tree copy;
1016 copy = copy_node (decl);
1017 cxx_dup_lang_specific_decl (copy);
1018 return copy;
1021 /* Replace the shared language-specific parts of NODE with a new copy. */
1023 static void
1024 copy_lang_type (node)
1025 tree node;
1027 int size;
1028 struct lang_type *lt;
1030 if (! TYPE_LANG_SPECIFIC (node))
1031 return;
1033 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
1034 size = sizeof (struct lang_type);
1035 else
1036 size = sizeof (struct lang_type_ptrmem);
1037 lt = (struct lang_type *) ggc_alloc (size);
1038 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
1039 TYPE_LANG_SPECIFIC (node) = lt;
1041 #ifdef GATHER_STATISTICS
1042 tree_node_counts[(int)lang_type] += 1;
1043 tree_node_sizes[(int)lang_type] += size;
1044 #endif
1047 /* Copy TYPE, including any language-specific parts. */
1049 tree
1050 copy_type (type)
1051 tree type;
1053 tree copy;
1055 copy = copy_node (type);
1056 copy_lang_type (copy);
1057 return copy;
1060 tree
1061 cxx_make_type (code)
1062 enum tree_code code;
1064 register tree t = make_node (code);
1066 /* Create lang_type structure. */
1067 if (IS_AGGR_TYPE_CODE (code)
1068 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1070 struct lang_type *pi;
1072 pi = ((struct lang_type *)
1073 ggc_alloc_cleared (sizeof (struct lang_type)));
1075 TYPE_LANG_SPECIFIC (t) = pi;
1076 pi->u.c.h.is_lang_type_class = 1;
1078 #ifdef GATHER_STATISTICS
1079 tree_node_counts[(int)lang_type] += 1;
1080 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1081 #endif
1084 /* Set up some flags that give proper default behavior. */
1085 if (IS_AGGR_TYPE_CODE (code))
1087 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1088 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1090 /* Make sure this is laid out, for ease of use later. In the
1091 presence of parse errors, the normal was of assuring this
1092 might not ever get executed, so we lay it out *immediately*. */
1093 build_pointer_type (t);
1095 else
1096 /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But,
1097 TYPE_ALIAS_SET is initialized to -1 by default, so we must
1098 clear it here. */
1099 TYPE_ALIAS_SET (t) = 0;
1101 /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1102 since they can be virtual base types, and we then need a
1103 canonical binfo for them. Ideally, this would be done lazily for
1104 all types. */
1105 if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM
1106 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1107 || code == TYPENAME_TYPE)
1108 TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1110 return t;
1113 tree
1114 make_aggr_type (code)
1115 enum tree_code code;
1117 tree t = cxx_make_type (code);
1119 if (IS_AGGR_TYPE_CODE (code))
1120 SET_IS_AGGR_TYPE (t, 1);
1122 return t;
1125 /* Return the type-qualifier corresponding to the identifier given by
1126 RID. */
1129 cp_type_qual_from_rid (rid)
1130 tree rid;
1132 if (rid == ridpointers[(int) RID_CONST])
1133 return TYPE_QUAL_CONST;
1134 else if (rid == ridpointers[(int) RID_VOLATILE])
1135 return TYPE_QUAL_VOLATILE;
1136 else if (rid == ridpointers[(int) RID_RESTRICT])
1137 return TYPE_QUAL_RESTRICT;
1139 abort ();
1140 return TYPE_UNQUALIFIED;