1 /* Separate lexical analyzer for GNU C++.
2    Copyright (C) 1987-2018 Free Software Foundation, Inc.
3    Hacked by Michael Tiemann (tiemann@cygnus.com)
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11 
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 
22 /* This file is the lexical analyzer for GNU C++.  */
23 
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "cp-tree.h"
28 #include "stringpool.h"
29 #include "c-family/c-pragma.h"
30 #include "c-family/c-objc.h"
31 
32 static int interface_strcmp (const char *);
33 static void init_cp_pragma (void);
34 
35 static tree parse_strconst_pragma (const char *, int);
36 static void handle_pragma_vtable (cpp_reader *);
37 static void handle_pragma_unit (cpp_reader *);
38 static void handle_pragma_interface (cpp_reader *);
39 static void handle_pragma_implementation (cpp_reader *);
40 
41 static void init_operators (void);
42 static void copy_lang_type (tree);
43 
44 /* A constraint that can be tested at compile time.  */
45 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
46 
47 /* Functions and data structures for #pragma interface.
48 
49    `#pragma implementation' means that the main file being compiled
50    is considered to implement (provide) the classes that appear in
51    its main body.  I.e., if this is file "foo.cc", and class `bar'
52    is defined in "foo.cc", then we say that "foo.cc implements bar".
53 
54    All main input files "implement" themselves automagically.
55 
56    `#pragma interface' means that unless this file (of the form "foo.h"
57    is not presently being included by file "foo.cc", the
58    CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
59    of the vtables nor any of the inline functions defined in foo.h
60    will ever be output.
61 
62    There are cases when we want to link files such as "defs.h" and
63    "main.cc".  In this case, we give "defs.h" a `#pragma interface',
64    and "main.cc" has `#pragma implementation "defs.h"'.  */
65 
66 struct impl_files
67 {
68   const char *filename;
69   struct impl_files *next;
70 };
71 
72 static struct impl_files *impl_file_chain;
73 
74 void
cxx_finish(void)75 cxx_finish (void)
76 {
77   c_common_finish ();
78 }
79 
80 ovl_op_info_t ovl_op_info[2][OVL_OP_MAX] =
81   {
82     {
83       {NULL_TREE, NULL, NULL, ERROR_MARK, OVL_OP_ERROR_MARK, 0},
84       {NULL_TREE, NULL, NULL, NOP_EXPR, OVL_OP_NOP_EXPR, 0},
85 #define DEF_OPERATOR(NAME, CODE, MANGLING, FLAGS) \
86       {NULL_TREE, NAME, MANGLING, CODE, OVL_OP_##CODE, FLAGS},
87 #define OPERATOR_TRANSITION }, {			\
88       {NULL_TREE, NULL, NULL, ERROR_MARK, OVL_OP_ERROR_MARK, 0},
89 #include "operators.def"
90     }
91   };
92 unsigned char ovl_op_mapping[MAX_TREE_CODES];
93 unsigned char ovl_op_alternate[OVL_OP_MAX];
94 
95 /* Get the name of the kind of identifier T.  */
96 
97 const char *
get_identifier_kind_name(tree id)98 get_identifier_kind_name (tree id)
99 {
100   /* Keep in sync with cp_id_kind enumeration.  */
101   static const char *const names[cik_max] = {
102     "normal", "keyword", "constructor", "destructor",
103     "simple-op", "assign-op", "conv-op", "<reserved>udlit-op"
104   };
105 
106   unsigned kind = 0;
107   kind |= IDENTIFIER_KIND_BIT_2 (id) << 2;
108   kind |= IDENTIFIER_KIND_BIT_1 (id) << 1;
109   kind |= IDENTIFIER_KIND_BIT_0 (id) << 0;
110 
111   return names[kind];
112 }
113 
114 /* Set the identifier kind, which we expect to currently be zero.  */
115 
116 void
set_identifier_kind(tree id,cp_identifier_kind kind)117 set_identifier_kind (tree id, cp_identifier_kind kind)
118 {
119   gcc_checking_assert (!IDENTIFIER_KIND_BIT_2 (id)
120 		       & !IDENTIFIER_KIND_BIT_1 (id)
121 		       & !IDENTIFIER_KIND_BIT_0 (id));
122   IDENTIFIER_KIND_BIT_2 (id) |= (kind >> 2) & 1;
123   IDENTIFIER_KIND_BIT_1 (id) |= (kind >> 1) & 1;
124   IDENTIFIER_KIND_BIT_0 (id) |= (kind >> 0) & 1;
125 }
126 
127 /* Create and tag the internal operator name for the overloaded
128    operator PTR describes.  */
129 
130 static tree
set_operator_ident(ovl_op_info_t * ptr)131 set_operator_ident (ovl_op_info_t *ptr)
132 {
133   char buffer[32];
134   size_t len = snprintf (buffer, sizeof (buffer), "operator%s%s",
135 			 &" "[ptr->name[0] && ptr->name[0] != '_'
136 			      && !ISALPHA (ptr->name[0])],
137 			 ptr->name);
138   gcc_checking_assert (len < sizeof (buffer));
139 
140   tree ident = get_identifier_with_length (buffer, len);
141   ptr->identifier = ident;
142 
143   return ident;
144 }
145 
146 /* Initialize data structures that keep track of operator names.  */
147 
148 static void
init_operators(void)149 init_operators (void)
150 {
151   /* We rely on both these being zero.  */
152   gcc_checking_assert (!OVL_OP_ERROR_MARK && !ERROR_MARK);
153 
154   /* This loop iterates backwards because we need to move the
155      assignment operators down to their correct slots.  I.e. morally
156      equivalent to an overlapping memmove where dest > src.  Slot
157      zero is for error_mark, so hae no operator. */
158   for (unsigned ix = OVL_OP_MAX; --ix;)
159     {
160       ovl_op_info_t *op_ptr = &ovl_op_info[false][ix];
161 
162       if (op_ptr->name)
163 	{
164 	  /* Make sure it fits in lang_decl_fn::operator_code. */
165 	  gcc_checking_assert (op_ptr->ovl_op_code < (1 << 6));
166 	  tree ident = set_operator_ident (op_ptr);
167 	  if (unsigned index = IDENTIFIER_CP_INDEX (ident))
168 	    {
169 	      ovl_op_info_t *bin_ptr = &ovl_op_info[false][index];
170 
171 	      /* They should only differ in unary/binary ness.  */
172 	      gcc_checking_assert ((op_ptr->flags ^ bin_ptr->flags)
173 				   == OVL_OP_FLAG_AMBIARY);
174 	      bin_ptr->flags |= op_ptr->flags;
175 	      ovl_op_alternate[index] = ix;
176 	    }
177 	  else
178 	    {
179 	      IDENTIFIER_CP_INDEX (ident) = ix;
180 	      set_identifier_kind (ident, cik_simple_op);
181 	    }
182 	}
183       if (op_ptr->tree_code)
184 	{
185 	  gcc_checking_assert (op_ptr->ovl_op_code == ix
186 			       && !ovl_op_mapping[op_ptr->tree_code]);
187 	  ovl_op_mapping[op_ptr->tree_code] = op_ptr->ovl_op_code;
188 	}
189 
190       ovl_op_info_t *as_ptr = &ovl_op_info[true][ix];
191       if (as_ptr->name)
192 	{
193 	  /* These will be placed at the start of the array, move to
194 	     the correct slot and initialize.  */
195 	  if (as_ptr->ovl_op_code != ix)
196 	    {
197 	      ovl_op_info_t *dst_ptr = &ovl_op_info[true][as_ptr->ovl_op_code];
198 	      gcc_assert (as_ptr->ovl_op_code > ix && !dst_ptr->tree_code);
199 	      memcpy (dst_ptr, as_ptr, sizeof (*dst_ptr));
200 	      memset (as_ptr, 0, sizeof (*as_ptr));
201 	      as_ptr = dst_ptr;
202 	    }
203 
204 	  tree ident = set_operator_ident (as_ptr);
205 	  gcc_checking_assert (!IDENTIFIER_CP_INDEX (ident));
206 	  IDENTIFIER_CP_INDEX (ident) = as_ptr->ovl_op_code;
207 	  set_identifier_kind (ident, cik_assign_op);
208 
209 	  gcc_checking_assert (!ovl_op_mapping[as_ptr->tree_code]
210 			       || (ovl_op_mapping[as_ptr->tree_code]
211 				   == as_ptr->ovl_op_code));
212 	  ovl_op_mapping[as_ptr->tree_code] = as_ptr->ovl_op_code;
213 	}
214     }
215 }
216 
217 /* Initialize the reserved words.  */
218 
219 void
init_reswords(void)220 init_reswords (void)
221 {
222   unsigned int i;
223   tree id;
224   int mask = 0;
225 
226   if (cxx_dialect < cxx11)
227     mask |= D_CXX11;
228   if (!flag_concepts)
229     mask |= D_CXX_CONCEPTS;
230   if (!flag_tm)
231     mask |= D_TRANSMEM;
232   if (flag_no_asm)
233     mask |= D_ASM | D_EXT;
234   if (flag_no_gnu_keywords)
235     mask |= D_EXT;
236 
237   /* The Objective-C keywords are all context-dependent.  */
238   mask |= D_OBJC;
239 
240   ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
241   for (i = 0; i < num_c_common_reswords; i++)
242     {
243       if (c_common_reswords[i].disable & D_CONLY)
244 	continue;
245       id = get_identifier (c_common_reswords[i].word);
246       C_SET_RID_CODE (id, c_common_reswords[i].rid);
247       ridpointers [(int) c_common_reswords[i].rid] = id;
248       if (! (c_common_reswords[i].disable & mask))
249 	set_identifier_kind (id, cik_keyword);
250     }
251 
252   for (i = 0; i < NUM_INT_N_ENTS; i++)
253     {
254       char name[50];
255       sprintf (name, "__int%d", int_n_data[i].bitsize);
256       id = get_identifier (name);
257       C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
258       set_identifier_kind (id, cik_keyword);
259     }
260 }
261 
262 static void
init_cp_pragma(void)263 init_cp_pragma (void)
264 {
265   c_register_pragma (0, "vtable", handle_pragma_vtable);
266   c_register_pragma (0, "unit", handle_pragma_unit);
267   c_register_pragma (0, "interface", handle_pragma_interface);
268   c_register_pragma (0, "implementation", handle_pragma_implementation);
269   c_register_pragma ("GCC", "interface", handle_pragma_interface);
270   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
271 }
272 
273 /* TRUE if a code represents a statement.  */
274 
275 bool statement_code_p[MAX_TREE_CODES];
276 
277 /* Initialize the C++ front end.  This function is very sensitive to
278    the exact order that things are done here.  It would be nice if the
279    initialization done by this routine were moved to its subroutines,
280    and the ordering dependencies clarified and reduced.  */
281 bool
cxx_init(void)282 cxx_init (void)
283 {
284   location_t saved_loc;
285   unsigned int i;
286   static const enum tree_code stmt_codes[] = {
287    CTOR_INITIALIZER,	TRY_BLOCK,	HANDLER,
288    EH_SPEC_BLOCK,	USING_STMT,	TAG_DEFN,
289    IF_STMT,		CLEANUP_STMT,	FOR_STMT,
290    RANGE_FOR_STMT,	WHILE_STMT,	DO_STMT,
291    BREAK_STMT,		CONTINUE_STMT,	SWITCH_STMT,
292    EXPR_STMT
293   };
294 
295   memset (&statement_code_p, 0, sizeof (statement_code_p));
296   for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
297     statement_code_p[stmt_codes[i]] = true;
298 
299   saved_loc = input_location;
300   input_location = BUILTINS_LOCATION;
301 
302   init_reswords ();
303   init_tree ();
304   init_cp_semantics ();
305   init_operators ();
306   init_method ();
307 
308   current_function_decl = NULL;
309 
310   class_type_node = ridpointers[(int) RID_CLASS];
311 
312   cxx_init_decl_processing ();
313 
314   if (c_common_init () == false)
315     {
316       input_location = saved_loc;
317       return false;
318     }
319 
320   init_cp_pragma ();
321 
322   init_repo ();
323 
324   input_location = saved_loc;
325   return true;
326 }
327 
328 /* Return nonzero if S is not considered part of an
329    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
330 
331 static int
interface_strcmp(const char * s)332 interface_strcmp (const char* s)
333 {
334   /* Set the interface/implementation bits for this scope.  */
335   struct impl_files *ifiles;
336   const char *s1;
337 
338   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
339     {
340       const char *t1 = ifiles->filename;
341       s1 = s;
342 
343       if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
344 	continue;
345 
346       while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
347 	s1++, t1++;
348 
349       /* A match.  */
350       if (*s1 == *t1)
351 	return 0;
352 
353       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
354       if (strchr (s1, '.') || strchr (t1, '.'))
355 	continue;
356 
357       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
358 	continue;
359 
360       /* A match.  */
361       return 0;
362     }
363 
364   /* No matches.  */
365   return 1;
366 }
367 
368 
369 
370 /* Parse a #pragma whose sole argument is a string constant.
371    If OPT is true, the argument is optional.  */
372 static tree
parse_strconst_pragma(const char * name,int opt)373 parse_strconst_pragma (const char* name, int opt)
374 {
375   tree result, x;
376   enum cpp_ttype t;
377 
378   t = pragma_lex (&result);
379   if (t == CPP_STRING)
380     {
381       if (pragma_lex (&x) != CPP_EOF)
382 	warning (0, "junk at end of #pragma %s", name);
383       return result;
384     }
385 
386   if (t == CPP_EOF && opt)
387     return NULL_TREE;
388 
389   error ("invalid #pragma %s", name);
390   return error_mark_node;
391 }
392 
393 static void
handle_pragma_vtable(cpp_reader *)394 handle_pragma_vtable (cpp_reader* /*dfile*/)
395 {
396   parse_strconst_pragma ("vtable", 0);
397   sorry ("#pragma vtable no longer supported");
398 }
399 
400 static void
handle_pragma_unit(cpp_reader *)401 handle_pragma_unit (cpp_reader* /*dfile*/)
402 {
403   /* Validate syntax, but don't do anything.  */
404   parse_strconst_pragma ("unit", 0);
405 }
406 
407 static void
handle_pragma_interface(cpp_reader *)408 handle_pragma_interface (cpp_reader* /*dfile*/)
409 {
410   tree fname = parse_strconst_pragma ("interface", 1);
411   struct c_fileinfo *finfo;
412   const char *filename;
413 
414   if (fname == error_mark_node)
415     return;
416   else if (fname == 0)
417     filename = lbasename (LOCATION_FILE (input_location));
418   else
419     filename = TREE_STRING_POINTER (fname);
420 
421   finfo = get_fileinfo (LOCATION_FILE (input_location));
422 
423   if (impl_file_chain == 0)
424     {
425       /* If this is zero at this point, then we are
426 	 auto-implementing.  */
427       if (main_input_filename == 0)
428 	main_input_filename = LOCATION_FILE (input_location);
429     }
430 
431   finfo->interface_only = interface_strcmp (filename);
432   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
433      a definition in another file.  */
434   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
435     finfo->interface_unknown = 0;
436 }
437 
438 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
439    We used to only allow this at toplevel, but that restriction was buggy
440    in older compilers and it seems reasonable to allow it in the headers
441    themselves, too.  It only needs to precede the matching #p interface.
442 
443    We don't touch finfo->interface_only or finfo->interface_unknown;
444    the user must specify a matching #p interface for this to have
445    any effect.  */
446 
447 static void
handle_pragma_implementation(cpp_reader *)448 handle_pragma_implementation (cpp_reader* /*dfile*/)
449 {
450   tree fname = parse_strconst_pragma ("implementation", 1);
451   const char *filename;
452   struct impl_files *ifiles = impl_file_chain;
453 
454   if (fname == error_mark_node)
455     return;
456 
457   if (fname == 0)
458     {
459       if (main_input_filename)
460 	filename = main_input_filename;
461       else
462 	filename = LOCATION_FILE (input_location);
463       filename = lbasename (filename);
464     }
465   else
466     {
467       filename = TREE_STRING_POINTER (fname);
468       if (cpp_included_before (parse_in, filename, input_location))
469 	warning (0, "#pragma implementation for %qs appears after "
470 		 "file is included", filename);
471     }
472 
473   for (; ifiles; ifiles = ifiles->next)
474     {
475       if (! filename_cmp (ifiles->filename, filename))
476 	break;
477     }
478   if (ifiles == 0)
479     {
480       ifiles = XNEW (struct impl_files);
481       ifiles->filename = xstrdup (filename);
482       ifiles->next = impl_file_chain;
483       impl_file_chain = ifiles;
484     }
485 }
486 
487 /* Issue an error message indicating that the lookup of NAME (an
488    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
489 
490 tree
unqualified_name_lookup_error(tree name,location_t loc)491 unqualified_name_lookup_error (tree name, location_t loc)
492 {
493   if (loc == UNKNOWN_LOCATION)
494     loc = EXPR_LOC_OR_LOC (name, input_location);
495 
496   if (IDENTIFIER_ANY_OP_P (name))
497     error_at (loc, "%qD not defined", name);
498   else
499     {
500       if (!objc_diagnose_private_ivar (name))
501 	{
502 	  error_at (loc, "%qD was not declared in this scope", name);
503 	  suggest_alternatives_for (loc, name, true);
504 	}
505       /* Prevent repeated error messages by creating a VAR_DECL with
506 	 this NAME in the innermost block scope.  */
507       if (local_bindings_p ())
508 	{
509 	  tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
510 	  TREE_USED (decl) = true;
511 	  pushdecl (decl);
512 	}
513     }
514 
515   return error_mark_node;
516 }
517 
518 /* Like unqualified_name_lookup_error, but NAME_EXPR is an unqualified-id
519    NAME, encapsulated with its location in a CP_EXPR, used as a function.
520    Returns an appropriate expression for NAME.  */
521 
522 tree
unqualified_fn_lookup_error(cp_expr name_expr)523 unqualified_fn_lookup_error (cp_expr name_expr)
524 {
525   tree name = name_expr.get_value ();
526   location_t loc = name_expr.get_location ();
527   if (loc == UNKNOWN_LOCATION)
528     loc = input_location;
529 
530   if (processing_template_decl)
531     {
532       /* In a template, it is invalid to write "f()" or "f(3)" if no
533 	 declaration of "f" is available.  Historically, G++ and most
534 	 other compilers accepted that usage since they deferred all name
535 	 lookup until instantiation time rather than doing unqualified
536 	 name lookup at template definition time; explain to the user what
537 	 is going wrong.
538 
539 	 Note that we have the exact wording of the following message in
540 	 the manual (trouble.texi, node "Name lookup"), so they need to
541 	 be kept in synch.  */
542       permerror (loc, "there are no arguments to %qD that depend on a template "
543 		 "parameter, so a declaration of %qD must be available",
544 		 name, name);
545 
546       if (!flag_permissive)
547 	{
548 	  static bool hint;
549 	  if (!hint)
550 	    {
551 	      inform (loc, "(if you use %<-fpermissive%>, G++ will accept your "
552 		     "code, but allowing the use of an undeclared name is "
553 		     "deprecated)");
554 	      hint = true;
555 	    }
556 	}
557       return name;
558     }
559 
560   return unqualified_name_lookup_error (name, loc);
561 }
562 
563 
564 /* Hasher for the conversion operator name hash table.  */
565 struct conv_type_hasher : ggc_ptr_hash<tree_node>
566 {
567   /* Hash NODE, an identifier node in the table.  TYPE_UID is
568      suitable, as we're not concerned about matching canonicalness
569      here.  */
hashconv_type_hasher570   static hashval_t hash (tree node)
571   {
572     return (hashval_t) TYPE_UID (TREE_TYPE (node));
573   }
574 
575   /* Compare NODE, an identifier node in the table, against TYPE, an
576      incoming TYPE being looked up.  */
equalconv_type_hasher577   static bool equal (tree node, tree type)
578   {
579     return TREE_TYPE (node) == type;
580   }
581 };
582 
583 /* This hash table maps TYPEs to the IDENTIFIER for a conversion
584    operator to TYPE.  The nodes are IDENTIFIERs whose TREE_TYPE is the
585    TYPE.  */
586 
587 static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
588 
589 /* Return an identifier for a conversion operator to TYPE.  We can get
590    from the returned identifier to the type.  We store TYPE, which is
591    not necessarily the canonical type,  which allows us to report the
592    form the user used in error messages.  All these identifiers are
593    not in the identifier hash table, and have the same string name.
594    These IDENTIFIERS are not in the identifier hash table, and all
595    have the same IDENTIFIER_STRING.  */
596 
597 tree
make_conv_op_name(tree type)598 make_conv_op_name (tree type)
599 {
600   if (type == error_mark_node)
601     return error_mark_node;
602 
603   if (conv_type_names == NULL)
604     conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
605 
606   tree *slot = conv_type_names->find_slot_with_hash
607     (type, (hashval_t) TYPE_UID (type), INSERT);
608   tree identifier = *slot;
609   if (!identifier)
610     {
611       /* Create a raw IDENTIFIER outside of the identifier hash
612 	 table.  */
613       identifier = copy_node (conv_op_identifier);
614 
615       /* Just in case something managed to bind.  */
616       IDENTIFIER_BINDING (identifier) = NULL;
617 
618       /* Hang TYPE off the identifier so it can be found easily later
619 	 when performing conversions.  */
620       TREE_TYPE (identifier) = type;
621 
622       *slot = identifier;
623     }
624 
625   return identifier;
626 }
627 
628 /* Wrapper around build_lang_decl_loc(). Should gradually move to
629    build_lang_decl_loc() and then rename build_lang_decl_loc() back to
630    build_lang_decl().  */
631 
632 tree
build_lang_decl(enum tree_code code,tree name,tree type)633 build_lang_decl (enum tree_code code, tree name, tree type)
634 {
635   return build_lang_decl_loc (input_location, code, name, type);
636 }
637 
638 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
639    DECL_LANG_SPECIFIC info to the result.  */
640 
641 tree
build_lang_decl_loc(location_t loc,enum tree_code code,tree name,tree type)642 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
643 {
644   tree t;
645 
646   t = build_decl (loc, code, name, type);
647   retrofit_lang_decl (t);
648 
649   return t;
650 }
651 
652 /* Maybe add a raw lang_decl to T, a decl.  Return true if it needed
653    one.  */
654 
655 static bool
maybe_add_lang_decl_raw(tree t,bool decomp_p)656 maybe_add_lang_decl_raw (tree t, bool decomp_p)
657 {
658   size_t size;
659   lang_decl_selector sel;
660 
661   if (decomp_p)
662     sel = lds_decomp, size = sizeof (struct lang_decl_decomp);
663   else if (TREE_CODE (t) == FUNCTION_DECL)
664     sel = lds_fn, size = sizeof (struct lang_decl_fn);
665   else if (TREE_CODE (t) == NAMESPACE_DECL)
666     sel = lds_ns, size = sizeof (struct lang_decl_ns);
667   else if (TREE_CODE (t) == PARM_DECL)
668     sel = lds_parm, size = sizeof (struct lang_decl_parm);
669   else if (LANG_DECL_HAS_MIN (t))
670     sel = lds_min, size = sizeof (struct lang_decl_min);
671   else
672     return false;
673 
674   struct lang_decl *ld
675     = (struct lang_decl *) ggc_internal_cleared_alloc (size);
676 
677   ld->u.base.selector = sel;
678   DECL_LANG_SPECIFIC (t) = ld;
679 
680   if (sel == lds_ns)
681     /* Who'd create a namespace, only to put nothing in it?  */
682     ld->u.ns.bindings = hash_table<named_decl_hash>::create_ggc (499);
683 
684   if (GATHER_STATISTICS)
685     {
686       tree_node_counts[(int)lang_decl] += 1;
687       tree_node_sizes[(int)lang_decl] += size;
688     }
689   return true;
690 }
691 
692 /* T has just had a decl_lang_specific added.  Initialize its
693    linkage.  */
694 
695 static void
set_decl_linkage(tree t)696 set_decl_linkage (tree t)
697 {
698   if (current_lang_name == lang_name_cplusplus
699       || decl_linkage (t) == lk_none)
700     SET_DECL_LANGUAGE (t, lang_cplusplus);
701   else if (current_lang_name == lang_name_c)
702     SET_DECL_LANGUAGE (t, lang_c);
703   else
704     gcc_unreachable ();
705 }
706 
707 /* T is a VAR_DECL node that needs to be a decomposition of BASE.  */
708 
709 void
fit_decomposition_lang_decl(tree t,tree base)710 fit_decomposition_lang_decl (tree t, tree base)
711 {
712   if (struct lang_decl *orig_ld = DECL_LANG_SPECIFIC (t))
713     {
714       if (orig_ld->u.base.selector == lds_min)
715 	{
716 	  maybe_add_lang_decl_raw (t, true);
717 	  memcpy (DECL_LANG_SPECIFIC (t), orig_ld,
718 		  sizeof (struct lang_decl_min));
719 	  /* Reset selector, which will have been bashed by the
720 	     memcpy.  */
721 	  DECL_LANG_SPECIFIC (t)->u.base.selector = lds_decomp;
722 	}
723       else
724 	gcc_checking_assert (orig_ld->u.base.selector == lds_decomp);
725     }
726   else
727     {
728       maybe_add_lang_decl_raw (t, true);
729       set_decl_linkage (t);
730     }
731 
732   DECL_DECOMP_BASE (t) = base;
733 }
734 
735 /* Add DECL_LANG_SPECIFIC info to T, if it needs one.  Generally
736    every C++ decl needs one, but C builtins etc do not.   */
737 
738 void
retrofit_lang_decl(tree t)739 retrofit_lang_decl (tree t)
740 {
741   if (DECL_LANG_SPECIFIC (t))
742     return;
743 
744   if (maybe_add_lang_decl_raw (t, false))
745     set_decl_linkage (t);
746 }
747 
748 void
cxx_dup_lang_specific_decl(tree node)749 cxx_dup_lang_specific_decl (tree node)
750 {
751   int size;
752 
753   if (! DECL_LANG_SPECIFIC (node))
754     return;
755 
756   switch (DECL_LANG_SPECIFIC (node)->u.base.selector)
757     {
758     case lds_min:
759       size = sizeof (struct lang_decl_min);
760       break;
761     case lds_fn:
762       size = sizeof (struct lang_decl_fn);
763       break;
764     case lds_ns:
765       size = sizeof (struct lang_decl_ns);
766       break;
767     case lds_parm:
768       size = sizeof (struct lang_decl_parm);
769       break;
770     case lds_decomp:
771       size = sizeof (struct lang_decl_decomp);
772       break;
773     default:
774       gcc_unreachable ();
775     }
776 
777   struct lang_decl *ld = (struct lang_decl *) ggc_internal_alloc (size);
778   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
779   DECL_LANG_SPECIFIC (node) = ld;
780 
781   if (GATHER_STATISTICS)
782     {
783       tree_node_counts[(int)lang_decl] += 1;
784       tree_node_sizes[(int)lang_decl] += size;
785     }
786 }
787 
788 /* Copy DECL, including any language-specific parts.  */
789 
790 tree
copy_decl(tree decl MEM_STAT_DECL)791 copy_decl (tree decl MEM_STAT_DECL)
792 {
793   tree copy;
794 
795   copy = copy_node (decl PASS_MEM_STAT);
796   cxx_dup_lang_specific_decl (copy);
797   return copy;
798 }
799 
800 /* Replace the shared language-specific parts of NODE with a new copy.  */
801 
802 static void
copy_lang_type(tree node)803 copy_lang_type (tree node)
804 {
805   if (! TYPE_LANG_SPECIFIC (node))
806     return;
807 
808   struct lang_type *lt
809     = (struct lang_type *) ggc_internal_alloc (sizeof (struct lang_type));
810 
811   memcpy (lt, TYPE_LANG_SPECIFIC (node), (sizeof (struct lang_type)));
812   TYPE_LANG_SPECIFIC (node) = lt;
813 
814   if (GATHER_STATISTICS)
815     {
816       tree_node_counts[(int)lang_type] += 1;
817       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
818     }
819 }
820 
821 /* Copy TYPE, including any language-specific parts.  */
822 
823 tree
copy_type(tree type MEM_STAT_DECL)824 copy_type (tree type MEM_STAT_DECL)
825 {
826   tree copy;
827 
828   copy = copy_node (type PASS_MEM_STAT);
829   copy_lang_type (copy);
830   return copy;
831 }
832 
833 /* Add a raw lang_type to T, a type, should it need one.  */
834 
835 static bool
maybe_add_lang_type_raw(tree t)836 maybe_add_lang_type_raw (tree t)
837 {
838   if (!RECORD_OR_UNION_CODE_P (TREE_CODE (t)))
839     return false;
840 
841   TYPE_LANG_SPECIFIC (t)
842     = (struct lang_type *) (ggc_internal_cleared_alloc
843 			    (sizeof (struct lang_type)));
844 
845   if (GATHER_STATISTICS)
846     {
847       tree_node_counts[(int)lang_type] += 1;
848       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
849     }
850 
851   return true;
852 }
853 
854 tree
cxx_make_type(enum tree_code code)855 cxx_make_type (enum tree_code code)
856 {
857   tree t = make_node (code);
858 
859   if (maybe_add_lang_type_raw (t))
860     {
861       /* Set up some flags that give proper default behavior.  */
862       struct c_fileinfo *finfo =
863 	get_fileinfo (LOCATION_FILE (input_location));
864       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
865       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
866     }
867 
868   return t;
869 }
870 
871 tree
make_class_type(enum tree_code code)872 make_class_type (enum tree_code code)
873 {
874   tree t = cxx_make_type (code);
875   SET_CLASS_TYPE_P (t, 1);
876   return t;
877 }
878 
879 /* Returns true if we are currently in the main source file, or in a
880    template instantiation started from the main source file.  */
881 
882 bool
in_main_input_context(void)883 in_main_input_context (void)
884 {
885   struct tinst_level *tl = outermost_tinst_level();
886 
887   if (tl)
888     return filename_cmp (main_input_filename,
889 			 LOCATION_FILE (tl->locus)) == 0;
890   else
891     return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;
892 }
893 
894 #include "gt-cp-lex.h"
895