comparison gcc/cp/lex.c @ 111:04ced10e8804

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