lex.c revision 169690
1218887Sdim/* Separate lexical analyzer for GNU C++.
2218887Sdim   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3218887Sdim   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4218887Sdim   Hacked by Michael Tiemann (tiemann@cygnus.com)
5218887Sdim
6218887SdimThis file is part of GCC.
7218887Sdim
8218887SdimGCC is free software; you can redistribute it and/or modify
9218887Sdimit under the terms of the GNU General Public License as published by
10251662Sdimthe Free Software Foundation; either version 2, or (at your option)
11218887Sdimany later version.
12218887Sdim
13218887SdimGCC is distributed in the hope that it will be useful,
14218887Sdimbut WITHOUT ANY WARRANTY; without even the implied warranty of
15249423SdimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16249423SdimGNU General Public License for more details.
17249423Sdim
18221345SdimYou should have received a copy of the GNU General Public License
19296417Sdimalong with GCC; see the file COPYING.  If not, write to
20296417Sdimthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
21218887SdimBoston, MA 02110-1301, USA.  */
22296417Sdim
23261991Sdim
24261991Sdim/* This file is the lexical analyzer for GNU C++.  */
25234353Sdim
26218887Sdim#include "config.h"
27218887Sdim#include "system.h"
28218887Sdim#include "coretypes.h"
29218887Sdim#include "tm.h"
30218887Sdim#include "input.h"
31234353Sdim#include "tree.h"
32234353Sdim#include "cp-tree.h"
33234353Sdim#include "cpplib.h"
34234353Sdim#include "flags.h"
35234353Sdim#include "c-pragma.h"
36234353Sdim#include "toplev.h"
37234353Sdim#include "output.h"
38234353Sdim#include "tm_p.h"
39234353Sdim#include "timevar.h"
40234353Sdim
41234353Sdimstatic int interface_strcmp (const char *);
42234353Sdimstatic void init_cp_pragma (void);
43234353Sdim
44234353Sdimstatic tree parse_strconst_pragma (const char *, int);
45234353Sdimstatic void handle_pragma_vtable (cpp_reader *);
46234353Sdimstatic void handle_pragma_unit (cpp_reader *);
47234353Sdimstatic void handle_pragma_interface (cpp_reader *);
48234353Sdimstatic void handle_pragma_implementation (cpp_reader *);
49234353Sdimstatic void handle_pragma_java_exceptions (cpp_reader *);
50234353Sdim
51234353Sdimstatic void init_operators (void);
52234353Sdimstatic void copy_lang_type (tree);
53218887Sdim
54218887Sdim/* A constraint that can be tested at compile time.  */
55218887Sdim#define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
56218887Sdim
57221345Sdim/* Functions and data structures for #pragma interface.
58218887Sdim
59218887Sdim   `#pragma implementation' means that the main file being compiled
60218887Sdim   is considered to implement (provide) the classes that appear in
61226633Sdim   its main body.  I.e., if this is file "foo.cc", and class `bar'
62218887Sdim   is defined in "foo.cc", then we say that "foo.cc implements bar".
63218887Sdim
64218887Sdim   All main input files "implement" themselves automagically.
65218887Sdim
66218887Sdim   `#pragma interface' means that unless this file (of the form "foo.h"
67218887Sdim   is not presently being included by file "foo.cc", the
68218887Sdim   CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
69218887Sdim   of the vtables nor any of the inline functions defined in foo.h
70218887Sdim   will ever be output.
71218887Sdim
72218887Sdim   There are cases when we want to link files such as "defs.h" and
73218887Sdim   "main.cc".  In this case, we give "defs.h" a `#pragma interface',
74218887Sdim   and "main.cc" has `#pragma implementation "defs.h"'.  */
75218887Sdim
76218887Sdimstruct impl_files
77221345Sdim{
78218887Sdim  const char *filename;
79218887Sdim  struct impl_files *next;
80218887Sdim};
81218887Sdim
82234353Sdimstatic struct impl_files *impl_file_chain;
83218887Sdim
84218887Sdim
85218887Sdimvoid
86218887Sdimcxx_finish (void)
87218887Sdim{
88218887Sdim  c_common_finish ();
89218887Sdim}
90218887Sdim
91218887Sdim/* A mapping from tree codes to operator name information.  */
92218887Sdimoperator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
93218887Sdim/* Similar, but for assignment operators.  */
94218887Sdimoperator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
95218887Sdim
96218887Sdim/* Initialize data structures that keep track of operator names.  */
97221345Sdim
98218887Sdim#define DEF_OPERATOR(NAME, C, M, AR, AP) \
99218887Sdim CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
100218887Sdim#include "operators.def"
101276479Sdim#undef DEF_OPERATOR
102276479Sdim
103276479Sdimstatic void
104276479Sdiminit_operators (void)
105276479Sdim{
106218887Sdim  tree identifier;
107234353Sdim  char buffer[256];
108234353Sdim  struct operator_name_info_t *oni;
109218887Sdim
110218887Sdim#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)		    \
111218887Sdim  sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
112218887Sdim  identifier = get_identifier (buffer);					    \
113218887Sdim  IDENTIFIER_OPNAME_P (identifier) = 1;					    \
114218887Sdim									    \
115218887Sdim  oni = (ASSN_P								    \
116218887Sdim	 ? &assignment_operator_name_info[(int) CODE]			    \
117234353Sdim	 : &operator_name_info[(int) CODE]);				    \
118234353Sdim  oni->identifier = identifier;						    \
119234353Sdim  oni->name = NAME;							    \
120234353Sdim  oni->mangled_name = MANGLING;						    \
121234353Sdim  oni->arity = ARITY;
122234353Sdim
123234353Sdim#include "operators.def"
124234353Sdim#undef DEF_OPERATOR
125234353Sdim
126234353Sdim  operator_name_info[(int) ERROR_MARK].identifier
127234353Sdim    = get_identifier ("<invalid operator>");
128234353Sdim
129234353Sdim  /* Handle some special cases.  These operators are not defined in
130234353Sdim     the language, but can be produced internally.  We may need them
131234353Sdim     for error-reporting.  (Eventually, we should ensure that this
132234353Sdim     does not happen.  Error messages involving these operators will
133234353Sdim     be confusing to users.)  */
134234353Sdim
135234353Sdim  operator_name_info [(int) INIT_EXPR].name
136234353Sdim    = operator_name_info [(int) MODIFY_EXPR].name;
137234353Sdim  operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
138234353Sdim  operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
139234353Sdim  operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
140234353Sdim  operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
141234353Sdim  operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
142234353Sdim  operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
143234353Sdim  operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
144234353Sdim  operator_name_info [(int) ABS_EXPR].name = "abs";
145234353Sdim  operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
146234353Sdim  operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
147234353Sdim  operator_name_info [(int) RANGE_EXPR].name = "...";
148234353Sdim  operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
149234353Sdim
150234353Sdim  assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
151234353Sdim    = "(exact /=)";
152234353Sdim  assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
153234353Sdim    = "(ceiling /=)";
154234353Sdim  assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
155234353Sdim    = "(floor /=)";
156234353Sdim  assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
157243830Sdim    = "(round /=)";
158243830Sdim  assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
159243830Sdim    = "(ceiling %=)";
160243830Sdim  assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
161243830Sdim    = "(floor %=)";
162243830Sdim  assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
163243830Sdim    = "(round %=)";
164243830Sdim}
165261991Sdim
166261991Sdim/* The reserved keyword table.  */
167261991Sdimstruct resword
168261991Sdim{
169261991Sdim  const char *const word;
170261991Sdim  ENUM_BITFIELD(rid) const rid : 16;
171261991Sdim  const unsigned int disable   : 16;
172243830Sdim};
173243830Sdim
174243830Sdim/* Disable mask.  Keywords are disabled if (reswords[i].disable & mask) is
175243830Sdim   _true_.  */
176261991Sdim#define D_EXT		0x01	/* GCC extension */
177243830Sdim#define D_ASM		0x02	/* in C99, but has a switch to turn it off */
178261991Sdim#define D_OBJC		0x04	/* Objective C++ only */
179261991Sdim
180261991SdimCONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
181261991Sdim
182261991Sdimstatic const struct resword reswords[] =
183243830Sdim{
184243830Sdim  { "_Complex",		RID_COMPLEX,	0 },
185261991Sdim  { "__FUNCTION__",	RID_FUNCTION_NAME, 0 },
186261991Sdim  { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
187261991Sdim  { "__alignof",	RID_ALIGNOF,	0 },
188243830Sdim  { "__alignof__",	RID_ALIGNOF,	0 },
189243830Sdim  { "__asm",		RID_ASM,	0 },
190243830Sdim  { "__asm__",		RID_ASM,	0 },
191243830Sdim  { "__attribute",	RID_ATTRIBUTE,	0 },
192243830Sdim  { "__attribute__",	RID_ATTRIBUTE,	0 },
193243830Sdim  { "__builtin_offsetof", RID_OFFSETOF, 0 },
194243830Sdim  { "__builtin_va_arg",	RID_VA_ARG,	0 },
195243830Sdim  { "__complex",	RID_COMPLEX,	0 },
196261991Sdim  { "__complex__",	RID_COMPLEX,	0 },
197261991Sdim  { "__const",		RID_CONST,	0 },
198261991Sdim  { "__const__",	RID_CONST,	0 },
199261991Sdim  { "__extension__",	RID_EXTENSION,	0 },
200261991Sdim  { "__func__",		RID_C99_FUNCTION_NAME,	0 },
201261991Sdim  { "__imag",		RID_IMAGPART,	0 },
202261991Sdim  { "__imag__",		RID_IMAGPART,	0 },
203261991Sdim  { "__inline",		RID_INLINE,	0 },
204261991Sdim  { "__inline__",	RID_INLINE,	0 },
205261991Sdim  { "__label__",	RID_LABEL,	0 },
206261991Sdim  { "__null",		RID_NULL,	0 },
207261991Sdim  { "__real",		RID_REALPART,	0 },
208261991Sdim  { "__real__",		RID_REALPART,	0 },
209261991Sdim  { "__restrict",	RID_RESTRICT,	0 },
210261991Sdim  { "__restrict__",	RID_RESTRICT,	0 },
211261991Sdim  { "__signed",		RID_SIGNED,	0 },
212261991Sdim  { "__signed__",	RID_SIGNED,	0 },
213261991Sdim  { "__thread",		RID_THREAD,	0 },
214261991Sdim  { "__typeof",		RID_TYPEOF,	0 },
215296417Sdim  { "__typeof__",	RID_TYPEOF,	0 },
216296417Sdim  { "__volatile",	RID_VOLATILE,	0 },
217296417Sdim  { "__volatile__",	RID_VOLATILE,	0 },
218296417Sdim  { "asm",		RID_ASM,	D_ASM },
219296417Sdim  { "auto",		RID_AUTO,	0 },
220296417Sdim  { "bool",		RID_BOOL,	0 },
221296417Sdim  { "break",		RID_BREAK,	0 },
222296417Sdim  { "case",		RID_CASE,	0 },
223296417Sdim  { "catch",		RID_CATCH,	0 },
224296417Sdim  { "char",		RID_CHAR,	0 },
225296417Sdim  { "class",		RID_CLASS,	0 },
226296417Sdim  { "const",		RID_CONST,	0 },
227296417Sdim  { "const_cast",	RID_CONSTCAST,	0 },
228296417Sdim  { "continue",		RID_CONTINUE,	0 },
229296417Sdim  { "default",		RID_DEFAULT,	0 },
230296417Sdim  { "delete",		RID_DELETE,	0 },
231296417Sdim  { "do",		RID_DO,		0 },
232296417Sdim  { "double",		RID_DOUBLE,	0 },
233296417Sdim  { "dynamic_cast",	RID_DYNCAST,	0 },
234296417Sdim  { "else",		RID_ELSE,	0 },
235296417Sdim  { "enum",		RID_ENUM,	0 },
236296417Sdim  { "explicit",		RID_EXPLICIT,	0 },
237296417Sdim  { "export",		RID_EXPORT,	0 },
238296417Sdim  { "extern",		RID_EXTERN,	0 },
239296417Sdim  { "false",		RID_FALSE,	0 },
240296417Sdim  { "float",		RID_FLOAT,	0 },
241296417Sdim  { "for",		RID_FOR,	0 },
242296417Sdim  { "friend",		RID_FRIEND,	0 },
243296417Sdim  { "goto",		RID_GOTO,	0 },
244296417Sdim  { "if",		RID_IF,		0 },
245296417Sdim  { "inline",		RID_INLINE,	0 },
246296417Sdim  { "int",		RID_INT,	0 },
247296417Sdim  { "long",		RID_LONG,	0 },
248  { "mutable",		RID_MUTABLE,	0 },
249  { "namespace",	RID_NAMESPACE,	0 },
250  { "new",		RID_NEW,	0 },
251  { "operator",		RID_OPERATOR,	0 },
252  { "private",		RID_PRIVATE,	0 },
253  { "protected",	RID_PROTECTED,	0 },
254  { "public",		RID_PUBLIC,	0 },
255  { "register",		RID_REGISTER,	0 },
256  { "reinterpret_cast",	RID_REINTCAST,	0 },
257  { "return",		RID_RETURN,	0 },
258  { "short",		RID_SHORT,	0 },
259  { "signed",		RID_SIGNED,	0 },
260  { "sizeof",		RID_SIZEOF,	0 },
261  { "static",		RID_STATIC,	0 },
262  { "static_cast",	RID_STATCAST,	0 },
263  { "struct",		RID_STRUCT,	0 },
264  { "switch",		RID_SWITCH,	0 },
265  { "template",		RID_TEMPLATE,	0 },
266  { "this",		RID_THIS,	0 },
267  { "throw",		RID_THROW,	0 },
268  { "true",		RID_TRUE,	0 },
269  { "try",		RID_TRY,	0 },
270  { "typedef",		RID_TYPEDEF,	0 },
271  { "typename",		RID_TYPENAME,	0 },
272  { "typeid",		RID_TYPEID,	0 },
273  { "typeof",		RID_TYPEOF,	D_ASM|D_EXT },
274  { "union",		RID_UNION,	0 },
275  { "unsigned",		RID_UNSIGNED,	0 },
276  { "using",		RID_USING,	0 },
277  { "virtual",		RID_VIRTUAL,	0 },
278  { "void",		RID_VOID,	0 },
279  { "volatile",		RID_VOLATILE,	0 },
280  { "wchar_t",		RID_WCHAR,	0 },
281  { "while",		RID_WHILE,	0 },
282
283  /* The remaining keywords are specific to Objective-C++.  NB:
284     All of them will remain _disabled_, since they are context-
285     sensitive.  */
286
287  /* These ObjC keywords are recognized only immediately after
288     an '@'.  NB: The following C++ keywords double as
289     ObjC keywords in this context: RID_CLASS, RID_PRIVATE,
290     RID_PROTECTED, RID_PUBLIC, RID_THROW, RID_TRY and RID_CATCH.  */
291  { "compatibility_alias", RID_AT_ALIAS,	D_OBJC },
292  { "defs",		RID_AT_DEFS,		D_OBJC },
293  { "encode",		RID_AT_ENCODE,		D_OBJC },
294  { "end",		RID_AT_END,		D_OBJC },
295  { "implementation",	RID_AT_IMPLEMENTATION,	D_OBJC },
296  { "interface",	RID_AT_INTERFACE,	D_OBJC },
297  { "protocol",		RID_AT_PROTOCOL,	D_OBJC },
298  { "selector",		RID_AT_SELECTOR,	D_OBJC },
299  { "finally",		RID_AT_FINALLY,		D_OBJC },
300  { "synchronized",	RID_AT_SYNCHRONIZED,	D_OBJC },
301  /* These are recognized only in protocol-qualifier context.  */
302  { "bycopy",		RID_BYCOPY,		D_OBJC },
303  { "byref",		RID_BYREF,		D_OBJC },
304  { "in",		RID_IN,			D_OBJC },
305  { "inout",		RID_INOUT,		D_OBJC },
306  { "oneway",		RID_ONEWAY,		D_OBJC },
307  { "out",		RID_OUT,		D_OBJC },
308};
309
310void
311init_reswords (void)
312{
313  unsigned int i;
314  tree id;
315  int mask = ((flag_no_asm ? D_ASM : 0)
316	      | D_OBJC
317	      | (flag_no_gnu_keywords ? D_EXT : 0));
318
319  ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
320  for (i = 0; i < ARRAY_SIZE (reswords); i++)
321    {
322      id = get_identifier (reswords[i].word);
323      C_RID_CODE (id) = reswords[i].rid;
324      ridpointers [(int) reswords[i].rid] = id;
325      if (! (reswords[i].disable & mask))
326	C_IS_RESERVED_WORD (id) = 1;
327    }
328}
329
330static void
331init_cp_pragma (void)
332{
333  c_register_pragma (0, "vtable", handle_pragma_vtable);
334  c_register_pragma (0, "unit", handle_pragma_unit);
335  c_register_pragma (0, "interface", handle_pragma_interface);
336  c_register_pragma (0, "implementation", handle_pragma_implementation);
337  c_register_pragma ("GCC", "interface", handle_pragma_interface);
338  c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
339  c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
340}
341
342/* TRUE if a code represents a statement.  */
343
344bool statement_code_p[MAX_TREE_CODES];
345
346/* Initialize the C++ front end.  This function is very sensitive to
347   the exact order that things are done here.  It would be nice if the
348   initialization done by this routine were moved to its subroutines,
349   and the ordering dependencies clarified and reduced.  */
350bool
351cxx_init (void)
352{
353  unsigned int i;
354  static const enum tree_code stmt_codes[] = {
355   CTOR_INITIALIZER,	TRY_BLOCK,	HANDLER,
356   EH_SPEC_BLOCK,	USING_STMT,	TAG_DEFN,
357   IF_STMT,		CLEANUP_STMT,	FOR_STMT,
358   WHILE_STMT,		DO_STMT,	BREAK_STMT,
359   CONTINUE_STMT,	SWITCH_STMT,	EXPR_STMT
360  };
361
362  memset (&statement_code_p, 0, sizeof (statement_code_p));
363  for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
364    statement_code_p[stmt_codes[i]] = true;
365
366  /* We cannot just assign to input_filename because it has already
367     been initialized and will be used later as an N_BINCL for stabs+
368     debugging.  */
369#ifdef USE_MAPPED_LOCATION
370  push_srcloc (BUILTINS_LOCATION);
371#else
372  push_srcloc ("<built-in>", 0);
373#endif
374
375  init_reswords ();
376  init_tree ();
377  init_cp_semantics ();
378  init_operators ();
379  init_method ();
380  init_error ();
381
382  current_function_decl = NULL;
383
384  class_type_node = ridpointers[(int) RID_CLASS];
385
386  cxx_init_decl_processing ();
387
388  /* The fact that G++ uses COMDAT for many entities (inline
389     functions, template instantiations, virtual tables, etc.) mean
390     that it is fundamentally unreliable to try to make decisions
391     about whether or not to output a particular entity until the end
392     of the compilation.  However, the inliner requires that functions
393     be provided to the back end if they are to be inlined.
394     Therefore, we always use unit-at-a-time mode; in that mode, we
395     can provide entities to the back end and it will decide what to
396     emit based on what is actually needed.  */
397  flag_unit_at_a_time = 1;
398
399  if (c_common_init () == false)
400    {
401      pop_srcloc();
402      return false;
403    }
404
405  init_cp_pragma ();
406
407  init_repo ();
408
409  pop_srcloc();
410  return true;
411}
412
413/* Return nonzero if S is not considered part of an
414   INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
415
416static int
417interface_strcmp (const char* s)
418{
419  /* Set the interface/implementation bits for this scope.  */
420  struct impl_files *ifiles;
421  const char *s1;
422
423  for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
424    {
425      const char *t1 = ifiles->filename;
426      s1 = s;
427
428      if (*s1 != *t1 || *s1 == 0)
429	continue;
430
431      while (*s1 == *t1 && *s1 != 0)
432	s1++, t1++;
433
434      /* A match.  */
435      if (*s1 == *t1)
436	return 0;
437
438      /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
439      if (strchr (s1, '.') || strchr (t1, '.'))
440	continue;
441
442      if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
443	continue;
444
445      /* A match.  */
446      return 0;
447    }
448
449  /* No matches.  */
450  return 1;
451}
452
453
454
455/* Parse a #pragma whose sole argument is a string constant.
456   If OPT is true, the argument is optional.  */
457static tree
458parse_strconst_pragma (const char* name, int opt)
459{
460  tree result, x;
461  enum cpp_ttype t;
462
463  t = pragma_lex (&result);
464  if (t == CPP_STRING)
465    {
466      if (pragma_lex (&x) != CPP_EOF)
467	warning (0, "junk at end of #pragma %s", name);
468      return result;
469    }
470
471  if (t == CPP_EOF && opt)
472    return NULL_TREE;
473
474  error ("invalid #pragma %s", name);
475  return error_mark_node;
476}
477
478static void
479handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
480{
481  parse_strconst_pragma ("vtable", 0);
482  sorry ("#pragma vtable no longer supported");
483}
484
485static void
486handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
487{
488  /* Validate syntax, but don't do anything.  */
489  parse_strconst_pragma ("unit", 0);
490}
491
492static void
493handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
494{
495  tree fname = parse_strconst_pragma ("interface", 1);
496  struct c_fileinfo *finfo;
497  const char *filename;
498
499  if (fname == error_mark_node)
500    return;
501  else if (fname == 0)
502    filename = lbasename (input_filename);
503  else
504    filename = ggc_strdup (TREE_STRING_POINTER (fname));
505
506  finfo = get_fileinfo (input_filename);
507
508  if (impl_file_chain == 0)
509    {
510      /* If this is zero at this point, then we are
511	 auto-implementing.  */
512      if (main_input_filename == 0)
513	main_input_filename = input_filename;
514    }
515
516  finfo->interface_only = interface_strcmp (filename);
517  /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
518     a definition in another file.  */
519  if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
520    finfo->interface_unknown = 0;
521}
522
523/* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
524   We used to only allow this at toplevel, but that restriction was buggy
525   in older compilers and it seems reasonable to allow it in the headers
526   themselves, too.  It only needs to precede the matching #p interface.
527
528   We don't touch finfo->interface_only or finfo->interface_unknown;
529   the user must specify a matching #p interface for this to have
530   any effect.  */
531
532static void
533handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
534{
535  tree fname = parse_strconst_pragma ("implementation", 1);
536  const char *filename;
537  struct impl_files *ifiles = impl_file_chain;
538
539  if (fname == error_mark_node)
540    return;
541
542  if (fname == 0)
543    {
544      if (main_input_filename)
545	filename = main_input_filename;
546      else
547	filename = input_filename;
548      filename = lbasename (filename);
549    }
550  else
551    {
552      filename = ggc_strdup (TREE_STRING_POINTER (fname));
553#if 0
554      /* We currently cannot give this diagnostic, as we reach this point
555	 only after cpplib has scanned the entire translation unit, so
556	 cpp_included always returns true.  A plausible fix is to compare
557	 the current source-location cookie with the first source-location
558	 cookie (if any) of the filename, but this requires completing the
559	 --enable-mapped-location project first.  See PR 17577.  */
560      if (cpp_included (parse_in, filename))
561	warning (0, "#pragma implementation for %qs appears after "
562		 "file is included", filename);
563#endif
564    }
565
566  for (; ifiles; ifiles = ifiles->next)
567    {
568      if (! strcmp (ifiles->filename, filename))
569	break;
570    }
571  if (ifiles == 0)
572    {
573      ifiles = XNEW (struct impl_files);
574      ifiles->filename = filename;
575      ifiles->next = impl_file_chain;
576      impl_file_chain = ifiles;
577    }
578}
579
580/* Indicate that this file uses Java-personality exception handling.  */
581static void
582handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
583{
584  tree x;
585  if (pragma_lex (&x) != CPP_EOF)
586    warning (0, "junk at end of #pragma GCC java_exceptions");
587
588  choose_personality_routine (lang_java);
589}
590
591/* Issue an error message indicating that the lookup of NAME (an
592   IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
593
594tree
595unqualified_name_lookup_error (tree name)
596{
597  if (IDENTIFIER_OPNAME_P (name))
598    {
599      if (name != ansi_opname (ERROR_MARK))
600	error ("%qD not defined", name);
601    }
602  else
603    {
604      error ("%qD was not declared in this scope", name);
605      /* Prevent repeated error messages by creating a VAR_DECL with
606	 this NAME in the innermost block scope.  */
607      if (current_function_decl)
608	{
609	  tree decl;
610	  decl = build_decl (VAR_DECL, name, error_mark_node);
611	  DECL_CONTEXT (decl) = current_function_decl;
612	  push_local_binding (name, decl, 0);
613	  /* Mark the variable as used so that we do not get warnings
614	     about it being unused later.  */
615	  TREE_USED (decl) = 1;
616	}
617    }
618
619  return error_mark_node;
620}
621
622/* Like unqualified_name_lookup_error, but NAME is an unqualified-id
623   used as a function.  Returns an appropriate expression for
624   NAME.  */
625
626tree
627unqualified_fn_lookup_error (tree name)
628{
629  if (processing_template_decl)
630    {
631      /* In a template, it is invalid to write "f()" or "f(3)" if no
632	 declaration of "f" is available.  Historically, G++ and most
633	 other compilers accepted that usage since they deferred all name
634	 lookup until instantiation time rather than doing unqualified
635	 name lookup at template definition time; explain to the user what
636	 is going wrong.
637
638	 Note that we have the exact wording of the following message in
639	 the manual (trouble.texi, node "Name lookup"), so they need to
640	 be kept in synch.  */
641      pedwarn ("there are no arguments to %qD that depend on a template "
642	       "parameter, so a declaration of %qD must be available",
643	       name, name);
644
645      if (!flag_permissive)
646	{
647	  static bool hint;
648	  if (!hint)
649	    {
650	      error ("(if you use %<-fpermissive%>, G++ will accept your "
651		     "code, but allowing the use of an undeclared name is "
652		     "deprecated)");
653	      hint = true;
654	    }
655	}
656      return name;
657    }
658
659  return unqualified_name_lookup_error (name);
660}
661
662tree
663build_lang_decl (enum tree_code code, tree name, tree type)
664{
665  tree t;
666
667  t = build_decl (code, name, type);
668  retrofit_lang_decl (t);
669
670  /* All nesting of C++ functions is lexical; there is never a "static
671     chain" in the sense of GNU C nested functions.  */
672  if (code == FUNCTION_DECL)
673    DECL_NO_STATIC_CHAIN (t) = 1;
674
675  return t;
676}
677
678/* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
679   and pushdecl (for functions generated by the backend).  */
680
681void
682retrofit_lang_decl (tree t)
683{
684  struct lang_decl *ld;
685  size_t size;
686
687  if (CAN_HAVE_FULL_LANG_DECL_P (t))
688    size = sizeof (struct lang_decl);
689  else
690    size = sizeof (struct lang_decl_flags);
691
692  ld = GGC_CNEWVAR (struct lang_decl, size);
693
694  ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
695  ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
696  ld->decl_flags.u2sel = 0;
697  if (ld->decl_flags.can_be_full)
698    ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
699
700  DECL_LANG_SPECIFIC (t) = ld;
701  if (current_lang_name == lang_name_cplusplus
702      || decl_linkage (t) == lk_none)
703    SET_DECL_LANGUAGE (t, lang_cplusplus);
704  else if (current_lang_name == lang_name_c)
705    SET_DECL_LANGUAGE (t, lang_c);
706  else if (current_lang_name == lang_name_java)
707    SET_DECL_LANGUAGE (t, lang_java);
708  else
709    gcc_unreachable ();
710
711#ifdef GATHER_STATISTICS
712  tree_node_counts[(int)lang_decl] += 1;
713  tree_node_sizes[(int)lang_decl] += size;
714#endif
715}
716
717void
718cxx_dup_lang_specific_decl (tree node)
719{
720  int size;
721  struct lang_decl *ld;
722
723  if (! DECL_LANG_SPECIFIC (node))
724    return;
725
726  if (!CAN_HAVE_FULL_LANG_DECL_P (node))
727    size = sizeof (struct lang_decl_flags);
728  else
729    size = sizeof (struct lang_decl);
730  ld = GGC_NEWVAR (struct lang_decl, size);
731  memcpy (ld, DECL_LANG_SPECIFIC (node), size);
732  DECL_LANG_SPECIFIC (node) = ld;
733
734#ifdef GATHER_STATISTICS
735  tree_node_counts[(int)lang_decl] += 1;
736  tree_node_sizes[(int)lang_decl] += size;
737#endif
738}
739
740/* Copy DECL, including any language-specific parts.  */
741
742tree
743copy_decl (tree decl)
744{
745  tree copy;
746
747  copy = copy_node (decl);
748  cxx_dup_lang_specific_decl (copy);
749  return copy;
750}
751
752/* Replace the shared language-specific parts of NODE with a new copy.  */
753
754static void
755copy_lang_type (tree node)
756{
757  int size;
758  struct lang_type *lt;
759
760  if (! TYPE_LANG_SPECIFIC (node))
761    return;
762
763  if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
764    size = sizeof (struct lang_type);
765  else
766    size = sizeof (struct lang_type_ptrmem);
767  lt = GGC_NEWVAR (struct lang_type, size);
768  memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
769  TYPE_LANG_SPECIFIC (node) = lt;
770
771#ifdef GATHER_STATISTICS
772  tree_node_counts[(int)lang_type] += 1;
773  tree_node_sizes[(int)lang_type] += size;
774#endif
775}
776
777/* Copy TYPE, including any language-specific parts.  */
778
779tree
780copy_type (tree type)
781{
782  tree copy;
783
784  copy = copy_node (type);
785  copy_lang_type (copy);
786  return copy;
787}
788
789tree
790cxx_make_type (enum tree_code code)
791{
792  tree t = make_node (code);
793
794  /* Create lang_type structure.  */
795  if (IS_AGGR_TYPE_CODE (code)
796      || code == BOUND_TEMPLATE_TEMPLATE_PARM)
797    {
798      struct lang_type *pi = GGC_CNEW (struct lang_type);
799
800      TYPE_LANG_SPECIFIC (t) = pi;
801      pi->u.c.h.is_lang_type_class = 1;
802
803#ifdef GATHER_STATISTICS
804      tree_node_counts[(int)lang_type] += 1;
805      tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
806#endif
807    }
808
809  /* Set up some flags that give proper default behavior.  */
810  if (IS_AGGR_TYPE_CODE (code))
811    {
812      struct c_fileinfo *finfo = get_fileinfo (input_filename);
813      SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
814      CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
815    }
816
817  return t;
818}
819
820tree
821make_aggr_type (enum tree_code code)
822{
823  tree t = cxx_make_type (code);
824
825  if (IS_AGGR_TYPE_CODE (code))
826    SET_IS_AGGR_TYPE (t, 1);
827
828  return t;
829}
830