OSDN Git Service

45523059af34f28cbaa55509374ae4cbcd0e1a50
[pf3gnuchains/gcc-fork.git] / gcc / cp / lex.c
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, 2004, 2005, 2007, 2008
4    Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23
24 /* This file is the lexical analyzer for GNU C++.  */
25
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 "flags.h"
35 #include "c-pragma.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "tm_p.h"
39 #include "timevar.h"
40
41 static int interface_strcmp (const char *);
42 static void init_cp_pragma (void);
43
44 static tree parse_strconst_pragma (const char *, int);
45 static void handle_pragma_vtable (cpp_reader *);
46 static void handle_pragma_unit (cpp_reader *);
47 static void handle_pragma_interface (cpp_reader *);
48 static void handle_pragma_implementation (cpp_reader *);
49 static void handle_pragma_java_exceptions (cpp_reader *);
50
51 static void init_operators (void);
52 static void copy_lang_type (tree);
53
54 /* A constraint that can be tested at compile time.  */
55 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
56
57 /* Functions and data structures for #pragma interface.
58
59    `#pragma implementation' means that the main file being compiled
60    is considered to implement (provide) the classes that appear in
61    its main body.  I.e., if this is file "foo.cc", and class `bar'
62    is defined in "foo.cc", then we say that "foo.cc implements bar".
63
64    All main input files "implement" themselves automagically.
65
66    `#pragma interface' means that unless this file (of the form "foo.h"
67    is not presently being included by file "foo.cc", the
68    CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
69    of the vtables nor any of the inline functions defined in foo.h
70    will ever be output.
71
72    There are cases when we want to link files such as "defs.h" and
73    "main.cc".  In this case, we give "defs.h" a `#pragma interface',
74    and "main.cc" has `#pragma implementation "defs.h"'.  */
75
76 struct impl_files
77 {
78   const char *filename;
79   struct impl_files *next;
80 };
81
82 static struct impl_files *impl_file_chain;
83
84 \f
85 void
86 cxx_finish (void)
87 {
88   c_common_finish ();
89 }
90
91 /* A mapping from tree codes to operator name information.  */
92 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
93 /* Similar, but for assignment operators.  */
94 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
95
96 /* Initialize data structures that keep track of operator names.  */
97
98 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
99  CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
100 #include "operators.def"
101 #undef DEF_OPERATOR
102
103 static void
104 init_operators (void)
105 {
106   tree identifier;
107   char buffer[256];
108   struct operator_name_info_t *oni;
109
110 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)                   \
111   sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
112   identifier = get_identifier (buffer);                                     \
113   IDENTIFIER_OPNAME_P (identifier) = 1;                                     \
114                                                                             \
115   oni = (ASSN_P                                                             \
116          ? &assignment_operator_name_info[(int) CODE]                       \
117          : &operator_name_info[(int) CODE]);                                \
118   oni->identifier = identifier;                                             \
119   oni->name = NAME;                                                         \
120   oni->mangled_name = MANGLING;                                             \
121   oni->arity = ARITY;
122
123 #include "operators.def"
124 #undef DEF_OPERATOR
125
126   operator_name_info[(int) ERROR_MARK].identifier
127     = get_identifier ("<invalid operator>");
128
129   /* Handle some special cases.  These operators are not defined in
130      the language, but can be produced internally.  We may need them
131      for error-reporting.  (Eventually, we should ensure that this
132      does not happen.  Error messages involving these operators will
133      be confusing to users.)  */
134
135   operator_name_info [(int) INIT_EXPR].name
136     = operator_name_info [(int) MODIFY_EXPR].name;
137   operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
138   operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
139   operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
140   operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
141   operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
142   operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
143   operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
144   operator_name_info [(int) ABS_EXPR].name = "abs";
145   operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
146   operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
147   operator_name_info [(int) RANGE_EXPR].name = "...";
148   operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
149
150   assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
151     = "(exact /=)";
152   assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
153     = "(ceiling /=)";
154   assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
155     = "(floor /=)";
156   assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
157     = "(round /=)";
158   assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
159     = "(ceiling %=)";
160   assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
161     = "(floor %=)";
162   assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
163     = "(round %=)";
164 }
165
166 /* The reserved keyword table.  */
167 struct resword
168 {
169   const char *const word;
170   ENUM_BITFIELD(rid) const rid : 16;
171   const unsigned int disable   : 16;
172 };
173
174 /* Disable mask.  Keywords are disabled if (reswords[i].disable & mask) is
175    _true_.  */
176 #define D_EXT           0x01    /* GCC extension */
177 #define D_ASM           0x02    /* in C99, but has a switch to turn it off */
178 #define D_OBJC          0x04    /* Objective C++ only */
179 #define D_CXX0X         0x08    /* C++0x only */
180
181 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
182
183 static const struct resword reswords[] =
184 {
185   { "_Complex",         RID_COMPLEX,    0 },
186   { "__FUNCTION__",     RID_FUNCTION_NAME, 0 },
187   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
188   { "__alignof",        RID_ALIGNOF,    0 },
189   { "__alignof__",      RID_ALIGNOF,    0 },
190   { "__asm",            RID_ASM,        0 },
191   { "__asm__",          RID_ASM,        0 },
192   { "__attribute",      RID_ATTRIBUTE,  0 },
193   { "__attribute__",    RID_ATTRIBUTE,  0 },
194   { "__builtin_offsetof", RID_OFFSETOF, 0 },
195   { "__builtin_va_arg", RID_VA_ARG,     0 },
196   { "__complex",        RID_COMPLEX,    0 },
197   { "__complex__",      RID_COMPLEX,    0 },
198   { "__const",          RID_CONST,      0 },
199   { "__const__",        RID_CONST,      0 },
200   { "__decltype",       RID_DECLTYPE,   0 },
201   { "__extension__",    RID_EXTENSION,  0 },
202   { "__func__",         RID_C99_FUNCTION_NAME,  0 },
203   { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, 0 },
204   { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, 0 },
205   { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, 0 },
206   { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, 0 },
207   { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, 0 },
208   { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, 0 },
209   { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, 0 },
210   { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, 0 },
211   { "__is_abstract",    RID_IS_ABSTRACT, 0 },
212   { "__is_base_of",     RID_IS_BASE_OF, 0 },
213   { "__is_class",       RID_IS_CLASS,   0 },
214   { "__is_convertible_to", RID_IS_CONVERTIBLE_TO, 0 },
215   { "__is_empty",       RID_IS_EMPTY,   0 },
216   { "__is_enum",        RID_IS_ENUM,    0 },
217   { "__is_pod",         RID_IS_POD,     0 },
218   { "__is_polymorphic", RID_IS_POLYMORPHIC, 0 },
219   { "__is_union",       RID_IS_UNION,   0 },
220   { "__imag",           RID_IMAGPART,   0 },
221   { "__imag__",         RID_IMAGPART,   0 },
222   { "__inline",         RID_INLINE,     0 },
223   { "__inline__",       RID_INLINE,     0 },
224   { "__label__",        RID_LABEL,      0 },
225   { "__null",           RID_NULL,       0 },
226   { "__real",           RID_REALPART,   0 },
227   { "__real__",         RID_REALPART,   0 },
228   { "__restrict",       RID_RESTRICT,   0 },
229   { "__restrict__",     RID_RESTRICT,   0 },
230   { "__signed",         RID_SIGNED,     0 },
231   { "__signed__",       RID_SIGNED,     0 },
232   { "__thread",         RID_THREAD,     0 },
233   { "__typeof",         RID_TYPEOF,     0 },
234   { "__typeof__",       RID_TYPEOF,     0 },
235   { "__volatile",       RID_VOLATILE,   0 },
236   { "__volatile__",     RID_VOLATILE,   0 },
237   { "asm",              RID_ASM,        D_ASM },
238   { "auto",             RID_AUTO,       0 },
239   { "bool",             RID_BOOL,       0 },
240   { "break",            RID_BREAK,      0 },
241   { "case",             RID_CASE,       0 },
242   { "catch",            RID_CATCH,      0 },
243   { "char",             RID_CHAR,       0 },
244   { "class",            RID_CLASS,      0 },
245   { "const",            RID_CONST,      0 },
246   { "const_cast",       RID_CONSTCAST,  0 },
247   { "continue",         RID_CONTINUE,   0 },
248   { "decltype",         RID_DECLTYPE,   D_CXX0X },
249   { "default",          RID_DEFAULT,    0 },
250   { "delete",           RID_DELETE,     0 },
251   { "do",               RID_DO,         0 },
252   { "double",           RID_DOUBLE,     0 },
253   { "dynamic_cast",     RID_DYNCAST,    0 },
254   { "else",             RID_ELSE,       0 },
255   { "enum",             RID_ENUM,       0 },
256   { "explicit",         RID_EXPLICIT,   0 },
257   { "export",           RID_EXPORT,     0 },
258   { "extern",           RID_EXTERN,     0 },
259   { "false",            RID_FALSE,      0 },
260   { "float",            RID_FLOAT,      0 },
261   { "for",              RID_FOR,        0 },
262   { "friend",           RID_FRIEND,     0 },
263   { "goto",             RID_GOTO,       0 },
264   { "if",               RID_IF,         0 },
265   { "inline",           RID_INLINE,     0 },
266   { "int",              RID_INT,        0 },
267   { "long",             RID_LONG,       0 },
268   { "mutable",          RID_MUTABLE,    0 },
269   { "namespace",        RID_NAMESPACE,  0 },
270   { "new",              RID_NEW,        0 },
271   { "operator",         RID_OPERATOR,   0 },
272   { "private",          RID_PRIVATE,    0 },
273   { "protected",        RID_PROTECTED,  0 },
274   { "public",           RID_PUBLIC,     0 },
275   { "register",         RID_REGISTER,   0 },
276   { "reinterpret_cast", RID_REINTCAST,  0 },
277   { "return",           RID_RETURN,     0 },
278   { "short",            RID_SHORT,      0 },
279   { "signed",           RID_SIGNED,     0 },
280   { "sizeof",           RID_SIZEOF,     0 },
281   { "static",           RID_STATIC,     0 },
282   { "static_assert",    RID_STATIC_ASSERT, D_CXX0X },
283   { "static_cast",      RID_STATCAST,   0 },
284   { "struct",           RID_STRUCT,     0 },
285   { "switch",           RID_SWITCH,     0 },
286   { "template",         RID_TEMPLATE,   0 },
287   { "this",             RID_THIS,       0 },
288   { "throw",            RID_THROW,      0 },
289   { "true",             RID_TRUE,       0 },
290   { "try",              RID_TRY,        0 },
291   { "typedef",          RID_TYPEDEF,    0 },
292   { "typename",         RID_TYPENAME,   0 },
293   { "typeid",           RID_TYPEID,     0 },
294   { "typeof",           RID_TYPEOF,     D_ASM|D_EXT },
295   { "union",            RID_UNION,      0 },
296   { "unsigned",         RID_UNSIGNED,   0 },
297   { "using",            RID_USING,      0 },
298   { "virtual",          RID_VIRTUAL,    0 },
299   { "void",             RID_VOID,       0 },
300   { "volatile",         RID_VOLATILE,   0 },
301   { "wchar_t",          RID_WCHAR,      0 },
302   { "while",            RID_WHILE,      0 },
303
304   /* The remaining keywords are specific to Objective-C++.  NB:
305      All of them will remain _disabled_, since they are context-
306      sensitive.  */
307
308   /* These ObjC keywords are recognized only immediately after
309      an '@'.  NB: The following C++ keywords double as
310      ObjC keywords in this context: RID_CLASS, RID_PRIVATE,
311      RID_PROTECTED, RID_PUBLIC, RID_THROW, RID_TRY and RID_CATCH.  */
312   { "compatibility_alias", RID_AT_ALIAS,        D_OBJC },
313   { "defs",             RID_AT_DEFS,            D_OBJC },
314   { "encode",           RID_AT_ENCODE,          D_OBJC },
315   { "end",              RID_AT_END,             D_OBJC },
316   { "implementation",   RID_AT_IMPLEMENTATION,  D_OBJC },
317   { "interface",        RID_AT_INTERFACE,       D_OBJC },
318   { "protocol",         RID_AT_PROTOCOL,        D_OBJC },
319   { "selector",         RID_AT_SELECTOR,        D_OBJC },
320   { "finally",          RID_AT_FINALLY,         D_OBJC },
321   { "synchronized",     RID_AT_SYNCHRONIZED,    D_OBJC },
322   /* These are recognized only in protocol-qualifier context.  */
323   { "bycopy",           RID_BYCOPY,             D_OBJC },
324   { "byref",            RID_BYREF,              D_OBJC },
325   { "in",               RID_IN,                 D_OBJC },
326   { "inout",            RID_INOUT,              D_OBJC },
327   { "oneway",           RID_ONEWAY,             D_OBJC },
328   { "out",              RID_OUT,                D_OBJC },
329 };
330
331 void
332 init_reswords (void)
333 {
334   unsigned int i;
335   tree id;
336   int mask = ((flag_no_asm ? D_ASM : 0)
337               | D_OBJC
338               | (flag_no_gnu_keywords ? D_EXT : 0)
339               | ((cxx_dialect == cxx0x) ? 0 : D_CXX0X));
340
341   ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
342   for (i = 0; i < ARRAY_SIZE (reswords); i++)
343     {
344       id = get_identifier (reswords[i].word);
345       C_RID_CODE (id) = reswords[i].rid;
346       ridpointers [(int) reswords[i].rid] = id;
347       if (! (reswords[i].disable & mask))
348         C_IS_RESERVED_WORD (id) = 1;
349     }
350 }
351
352 static void
353 init_cp_pragma (void)
354 {
355   c_register_pragma (0, "vtable", handle_pragma_vtable);
356   c_register_pragma (0, "unit", handle_pragma_unit);
357   c_register_pragma (0, "interface", handle_pragma_interface);
358   c_register_pragma (0, "implementation", handle_pragma_implementation);
359   c_register_pragma ("GCC", "interface", handle_pragma_interface);
360   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
361   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
362 }
363 \f
364 /* TRUE if a code represents a statement.  */
365
366 bool statement_code_p[MAX_TREE_CODES];
367
368 /* Initialize the C++ front end.  This function is very sensitive to
369    the exact order that things are done here.  It would be nice if the
370    initialization done by this routine were moved to its subroutines,
371    and the ordering dependencies clarified and reduced.  */
372 bool
373 cxx_init (void)
374 {
375   location_t saved_loc;
376   unsigned int i;
377   static const enum tree_code stmt_codes[] = {
378    CTOR_INITIALIZER,    TRY_BLOCK,      HANDLER,
379    EH_SPEC_BLOCK,       USING_STMT,     TAG_DEFN,
380    IF_STMT,             CLEANUP_STMT,   FOR_STMT,
381    WHILE_STMT,          DO_STMT,        BREAK_STMT,
382    CONTINUE_STMT,       SWITCH_STMT,    EXPR_STMT
383   };
384
385   memset (&statement_code_p, 0, sizeof (statement_code_p));
386   for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
387     statement_code_p[stmt_codes[i]] = true;
388
389   saved_loc = input_location;
390   input_location = BUILTINS_LOCATION;
391
392   init_reswords ();
393   init_tree ();
394   init_cp_semantics ();
395   init_operators ();
396   init_method ();
397   init_error ();
398
399   current_function_decl = NULL;
400
401   class_type_node = ridpointers[(int) RID_CLASS];
402
403   cxx_init_decl_processing ();
404
405   /* The fact that G++ uses COMDAT for many entities (inline
406      functions, template instantiations, virtual tables, etc.) mean
407      that it is fundamentally unreliable to try to make decisions
408      about whether or not to output a particular entity until the end
409      of the compilation.  However, the inliner requires that functions
410      be provided to the back end if they are to be inlined.
411      Therefore, we always use unit-at-a-time mode; in that mode, we
412      can provide entities to the back end and it will decide what to
413      emit based on what is actually needed.  */
414   flag_unit_at_a_time = 1;
415
416   if (c_common_init () == false)
417     {
418       input_location = saved_loc;
419       return false;
420     }
421
422   init_cp_pragma ();
423
424   init_repo ();
425
426   input_location = saved_loc;
427   return true;
428 }
429 \f
430 /* Return nonzero if S is not considered part of an
431    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
432
433 static int
434 interface_strcmp (const char* s)
435 {
436   /* Set the interface/implementation bits for this scope.  */
437   struct impl_files *ifiles;
438   const char *s1;
439
440   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
441     {
442       const char *t1 = ifiles->filename;
443       s1 = s;
444
445       if (*s1 != *t1 || *s1 == 0)
446         continue;
447
448       while (*s1 == *t1 && *s1 != 0)
449         s1++, t1++;
450
451       /* A match.  */
452       if (*s1 == *t1)
453         return 0;
454
455       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
456       if (strchr (s1, '.') || strchr (t1, '.'))
457         continue;
458
459       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
460         continue;
461
462       /* A match.  */
463       return 0;
464     }
465
466   /* No matches.  */
467   return 1;
468 }
469
470 \f
471
472 /* Parse a #pragma whose sole argument is a string constant.
473    If OPT is true, the argument is optional.  */
474 static tree
475 parse_strconst_pragma (const char* name, int opt)
476 {
477   tree result, x;
478   enum cpp_ttype t;
479
480   t = pragma_lex (&result);
481   if (t == CPP_STRING)
482     {
483       if (pragma_lex (&x) != CPP_EOF)
484         warning (0, "junk at end of #pragma %s", name);
485       return result;
486     }
487
488   if (t == CPP_EOF && opt)
489     return NULL_TREE;
490
491   error ("invalid #pragma %s", name);
492   return error_mark_node;
493 }
494
495 static void
496 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
497 {
498   parse_strconst_pragma ("vtable", 0);
499   sorry ("#pragma vtable no longer supported");
500 }
501
502 static void
503 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
504 {
505   /* Validate syntax, but don't do anything.  */
506   parse_strconst_pragma ("unit", 0);
507 }
508
509 static void
510 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
511 {
512   tree fname = parse_strconst_pragma ("interface", 1);
513   struct c_fileinfo *finfo;
514   const char *filename;
515
516   if (fname == error_mark_node)
517     return;
518   else if (fname == 0)
519     filename = lbasename (input_filename);
520   else
521     filename = TREE_STRING_POINTER (fname);
522
523   finfo = get_fileinfo (input_filename);
524
525   if (impl_file_chain == 0)
526     {
527       /* If this is zero at this point, then we are
528          auto-implementing.  */
529       if (main_input_filename == 0)
530         main_input_filename = input_filename;
531     }
532
533   finfo->interface_only = interface_strcmp (filename);
534   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
535      a definition in another file.  */
536   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
537     finfo->interface_unknown = 0;
538 }
539
540 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
541    We used to only allow this at toplevel, but that restriction was buggy
542    in older compilers and it seems reasonable to allow it in the headers
543    themselves, too.  It only needs to precede the matching #p interface.
544
545    We don't touch finfo->interface_only or finfo->interface_unknown;
546    the user must specify a matching #p interface for this to have
547    any effect.  */
548
549 static void
550 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
551 {
552   tree fname = parse_strconst_pragma ("implementation", 1);
553   const char *filename;
554   struct impl_files *ifiles = impl_file_chain;
555
556   if (fname == error_mark_node)
557     return;
558
559   if (fname == 0)
560     {
561       if (main_input_filename)
562         filename = main_input_filename;
563       else
564         filename = input_filename;
565       filename = lbasename (filename);
566     }
567   else
568     {
569       filename = TREE_STRING_POINTER (fname);
570       if (cpp_included_before (parse_in, filename, input_location))
571         warning (0, "#pragma implementation for %qs appears after "
572                  "file is included", filename);
573     }
574
575   for (; ifiles; ifiles = ifiles->next)
576     {
577       if (! strcmp (ifiles->filename, filename))
578         break;
579     }
580   if (ifiles == 0)
581     {
582       ifiles = XNEW (struct impl_files);
583       ifiles->filename = xstrdup (filename);
584       ifiles->next = impl_file_chain;
585       impl_file_chain = ifiles;
586     }
587 }
588
589 /* Indicate that this file uses Java-personality exception handling.  */
590 static void
591 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
592 {
593   tree x;
594   if (pragma_lex (&x) != CPP_EOF)
595     warning (0, "junk at end of #pragma GCC java_exceptions");
596
597   choose_personality_routine (lang_java);
598 }
599
600 /* Issue an error message indicating that the lookup of NAME (an
601    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
602
603 tree
604 unqualified_name_lookup_error (tree name)
605 {
606   if (IDENTIFIER_OPNAME_P (name))
607     {
608       if (name != ansi_opname (ERROR_MARK))
609         error ("%qD not defined", name);
610     }
611   else
612     {
613       error ("%qD was not declared in this scope", name);
614       /* Prevent repeated error messages by creating a VAR_DECL with
615          this NAME in the innermost block scope.  */
616       if (current_function_decl)
617         {
618           tree decl;
619           decl = build_decl (VAR_DECL, name, error_mark_node);
620           DECL_CONTEXT (decl) = current_function_decl;
621           push_local_binding (name, decl, 0);
622           /* Mark the variable as used so that we do not get warnings
623              about it being unused later.  */
624           TREE_USED (decl) = 1;
625         }
626     }
627
628   return error_mark_node;
629 }
630
631 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
632    used as a function.  Returns an appropriate expression for
633    NAME.  */
634
635 tree
636 unqualified_fn_lookup_error (tree name)
637 {
638   if (processing_template_decl)
639     {
640       /* In a template, it is invalid to write "f()" or "f(3)" if no
641          declaration of "f" is available.  Historically, G++ and most
642          other compilers accepted that usage since they deferred all name
643          lookup until instantiation time rather than doing unqualified
644          name lookup at template definition time; explain to the user what
645          is going wrong.
646
647          Note that we have the exact wording of the following message in
648          the manual (trouble.texi, node "Name lookup"), so they need to
649          be kept in synch.  */
650       permerror ("there are no arguments to %qD that depend on a template "
651                  "parameter, so a declaration of %qD must be available",
652                  name, name);
653
654       if (!flag_permissive)
655         {
656           static bool hint;
657           if (!hint)
658             {
659               inform ("(if you use %<-fpermissive%>, G++ will accept your "
660                      "code, but allowing the use of an undeclared name is "
661                      "deprecated)");
662               hint = true;
663             }
664         }
665       return name;
666     }
667
668   return unqualified_name_lookup_error (name);
669 }
670
671 tree
672 build_lang_decl (enum tree_code code, tree name, tree type)
673 {
674   tree t;
675
676   t = build_decl (code, name, type);
677   retrofit_lang_decl (t);
678
679   /* All nesting of C++ functions is lexical; there is never a "static
680      chain" in the sense of GNU C nested functions.  */
681   if (code == FUNCTION_DECL)
682     DECL_NO_STATIC_CHAIN (t) = 1;
683
684   return t;
685 }
686
687 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
688    and pushdecl (for functions generated by the back end).  */
689
690 void
691 retrofit_lang_decl (tree t)
692 {
693   struct lang_decl *ld;
694   size_t size;
695
696   if (CAN_HAVE_FULL_LANG_DECL_P (t))
697     size = sizeof (struct lang_decl);
698   else
699     size = sizeof (struct lang_decl_flags);
700
701   ld = GGC_CNEWVAR (struct lang_decl, size);
702
703   ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
704   ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
705   ld->decl_flags.u2sel = 0;
706   if (ld->decl_flags.can_be_full)
707     ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
708
709   DECL_LANG_SPECIFIC (t) = ld;
710   if (current_lang_name == lang_name_cplusplus
711       || decl_linkage (t) == lk_none)
712     SET_DECL_LANGUAGE (t, lang_cplusplus);
713   else if (current_lang_name == lang_name_c)
714     SET_DECL_LANGUAGE (t, lang_c);
715   else if (current_lang_name == lang_name_java)
716     SET_DECL_LANGUAGE (t, lang_java);
717   else
718     gcc_unreachable ();
719
720 #ifdef GATHER_STATISTICS
721   tree_node_counts[(int)lang_decl] += 1;
722   tree_node_sizes[(int)lang_decl] += size;
723 #endif
724 }
725
726 void
727 cxx_dup_lang_specific_decl (tree node)
728 {
729   int size;
730   struct lang_decl *ld;
731
732   if (! DECL_LANG_SPECIFIC (node))
733     return;
734
735   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
736     size = sizeof (struct lang_decl_flags);
737   else
738     size = sizeof (struct lang_decl);
739   ld = GGC_NEWVAR (struct lang_decl, size);
740   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
741   DECL_LANG_SPECIFIC (node) = ld;
742
743 #ifdef GATHER_STATISTICS
744   tree_node_counts[(int)lang_decl] += 1;
745   tree_node_sizes[(int)lang_decl] += size;
746 #endif
747 }
748
749 /* Copy DECL, including any language-specific parts.  */
750
751 tree
752 copy_decl (tree decl)
753 {
754   tree copy;
755
756   copy = copy_node (decl);
757   cxx_dup_lang_specific_decl (copy);
758   return copy;
759 }
760
761 /* Replace the shared language-specific parts of NODE with a new copy.  */
762
763 static void
764 copy_lang_type (tree node)
765 {
766   int size;
767   struct lang_type *lt;
768
769   if (! TYPE_LANG_SPECIFIC (node))
770     return;
771
772   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
773     size = sizeof (struct lang_type);
774   else
775     size = sizeof (struct lang_type_ptrmem);
776   lt = GGC_NEWVAR (struct lang_type, size);
777   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
778   TYPE_LANG_SPECIFIC (node) = lt;
779
780 #ifdef GATHER_STATISTICS
781   tree_node_counts[(int)lang_type] += 1;
782   tree_node_sizes[(int)lang_type] += size;
783 #endif
784 }
785
786 /* Copy TYPE, including any language-specific parts.  */
787
788 tree
789 copy_type (tree type)
790 {
791   tree copy;
792
793   copy = copy_node (type);
794   copy_lang_type (copy);
795   return copy;
796 }
797
798 tree
799 cxx_make_type (enum tree_code code)
800 {
801   tree t = make_node (code);
802
803   /* Create lang_type structure.  */
804   if (RECORD_OR_UNION_CODE_P (code)
805       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
806     {
807       struct lang_type *pi = GGC_CNEW (struct lang_type);
808
809       TYPE_LANG_SPECIFIC (t) = pi;
810       pi->u.c.h.is_lang_type_class = 1;
811
812 #ifdef GATHER_STATISTICS
813       tree_node_counts[(int)lang_type] += 1;
814       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
815 #endif
816     }
817
818   /* Set up some flags that give proper default behavior.  */
819   if (RECORD_OR_UNION_CODE_P (code))
820     {
821       struct c_fileinfo *finfo = get_fileinfo (input_filename);
822       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
823       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
824     }
825
826   return t;
827 }
828
829 tree
830 make_class_type (enum tree_code code)
831 {
832   tree t = cxx_make_type (code);
833   SET_CLASS_TYPE_P (t, 1);
834   return t;
835 }
836
837 /* Returns true if we are currently in the main source file, or in a
838    template instantiation started from the main source file.  */
839
840 bool
841 in_main_input_context (void)
842 {
843   struct tinst_level *tl = outermost_tinst_level();
844
845   if (tl)
846     return strcmp (main_input_filename,
847                   LOCATION_FILE (tl->locus)) == 0;
848   else
849     return strcmp (main_input_filename, input_filename) == 0;
850 }