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