]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/lex.c
Factor unrelated declarations out of tree.h.
[thirdparty/gcc.git] / gcc / cp / lex.c
CommitLineData
471086d6 1/* Separate lexical analyzer for GNU C++.
711789cc 2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
471086d6 3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
6f0d25a6 5This file is part of GCC.
471086d6 6
6f0d25a6 7GCC is free software; you can redistribute it and/or modify
471086d6 8it under the terms of the GNU General Public License as published by
aa139c3f 9the Free Software Foundation; either version 3, or (at your option)
471086d6 10any later version.
11
6f0d25a6 12GCC is distributed in the hope that it will be useful,
471086d6 13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
aa139c3f 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
471086d6 20
21
22/* This file is the lexical analyzer for GNU C++. */
23
39c8ac16 24#include "config.h"
b3ef7553 25#include "system.h"
805e22b2 26#include "coretypes.h"
27#include "tm.h"
471086d6 28#include "input.h"
29#include "tree.h"
9ed99284 30#include "stringpool.h"
471086d6 31#include "cp-tree.h"
518796ad 32#include "cpplib.h"
471086d6 33#include "flags.h"
7bedc3a0 34#include "c-family/c-pragma.h"
79408174 35#include "c-family/c-objc.h"
d8c9779c 36#include "tm_p.h"
74d2af64 37#include "timevar.h"
471086d6 38
42c35e28 39static int interface_strcmp (const char *);
40static void init_cp_pragma (void);
518796ad 41
42c35e28 42static tree parse_strconst_pragma (const char *, int);
43static void handle_pragma_vtable (cpp_reader *);
44static void handle_pragma_unit (cpp_reader *);
45static void handle_pragma_interface (cpp_reader *);
46static void handle_pragma_implementation (cpp_reader *);
47static void handle_pragma_java_exceptions (cpp_reader *);
518796ad 48
42c35e28 49static void init_operators (void);
50static void copy_lang_type (tree);
471086d6 51
518796ad 52/* A constraint that can be tested at compile time. */
518796ad 53#define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
46e5e818 54
23f83a9a 55/* Functions and data structures for #pragma interface.
56
57 `#pragma implementation' means that the main file being compiled
58 is considered to implement (provide) the classes that appear in
59 its main body. I.e., if this is file "foo.cc", and class `bar'
60 is defined in "foo.cc", then we say that "foo.cc implements bar".
61
62 All main input files "implement" themselves automagically.
63
64 `#pragma interface' means that unless this file (of the form "foo.h"
65 is not presently being included by file "foo.cc", the
66 CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
67 of the vtables nor any of the inline functions defined in foo.h
68 will ever be output.
69
70 There are cases when we want to link files such as "defs.h" and
71 "main.cc". In this case, we give "defs.h" a `#pragma interface',
72 and "main.cc" has `#pragma implementation "defs.h"'. */
73
74struct impl_files
75{
44acf429 76 const char *filename;
23f83a9a 77 struct impl_files *next;
78};
79
80static struct impl_files *impl_file_chain;
81
bde8fea7 82/* True if we saw "#pragma GCC java_exceptions". */
83bool pragma_java_exceptions;
471086d6 84\f
ca58dfe1 85void
42c35e28 86cxx_finish (void)
471086d6 87{
cdc9fa3e 88 c_common_finish ();
471086d6 89}
90
97cc4539 91/* A mapping from tree codes to operator name information. */
e014fc6a 92operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
97cc4539 93/* Similar, but for assignment operators. */
e014fc6a 94operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
3c3beda6 95
97cc4539 96/* Initialize data structures that keep track of operator names. */
97
c5144efa 98#define DEF_OPERATOR(NAME, C, M, AR, AP) \
518796ad 99 CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
100#include "operators.def"
101#undef DEF_OPERATOR
102
97cc4539 103static void
42c35e28 104init_operators (void)
97cc4539 105{
106 tree identifier;
107 char buffer[256];
108 struct operator_name_info_t *oni;
3c3beda6 109
c5144efa 110#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
0169d787 111 sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
97cc4539 112 identifier = get_identifier (buffer); \
113 IDENTIFIER_OPNAME_P (identifier) = 1; \
114 \
115 oni = (ASSN_P \
116 ? &assignment_operator_name_info[(int) CODE] \
117 : &operator_name_info[(int) CODE]); \
118 oni->identifier = identifier; \
119 oni->name = NAME; \
653e5405 120 oni->mangled_name = MANGLING; \
94302392 121 oni->arity = ARITY;
97cc4539 122
123#include "operators.def"
124#undef DEF_OPERATOR
125
3c3beda6 126 operator_name_info[(int) ERROR_MARK].identifier
97cc4539 127 = get_identifier ("<invalid operator>");
128
129 /* Handle some special cases. These operators are not defined in
130 the language, but can be produced internally. We may need them
131 for error-reporting. (Eventually, we should ensure that this
132 does not happen. Error messages involving these operators will
133 be confusing to users.) */
3c3beda6 134
135 operator_name_info [(int) INIT_EXPR].name
97cc4539 136 = operator_name_info [(int) MODIFY_EXPR].name;
137 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
138 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
139 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
140 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
141 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
142 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
143 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
144 operator_name_info [(int) ABS_EXPR].name = "abs";
97cc4539 145 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
146 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
97cc4539 147 operator_name_info [(int) RANGE_EXPR].name = "...";
97d541d5 148 operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
97cc4539 149
3c3beda6 150 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
97cc4539 151 = "(exact /=)";
3c3beda6 152 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
97cc4539 153 = "(ceiling /=)";
3c3beda6 154 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
97cc4539 155 = "(floor /=)";
3c3beda6 156 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
97cc4539 157 = "(round /=)";
3c3beda6 158 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
97cc4539 159 = "(ceiling %=)";
3c3beda6 160 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
97cc4539 161 = "(floor %=)";
3c3beda6 162 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
97cc4539 163 = "(round %=)";
164}
165
5c6e5756 166/* Initialize the reserved words. */
518796ad 167
9ceb1c29 168void
42c35e28 169init_reswords (void)
518796ad 170{
171 unsigned int i;
172 tree id;
5c6e5756 173 int mask = 0;
174
60777f69 175 if (cxx_dialect < cxx11)
5c6e5756 176 mask |= D_CXX0X;
177 if (flag_no_asm)
178 mask |= D_ASM | D_EXT;
179 if (flag_no_gnu_keywords)
180 mask |= D_EXT;
b4d6f57b 181
182 /* The Objective-C keywords are all context-dependent. */
183 mask |= D_OBJC;
518796ad 184
ba72912a 185 ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
5c6e5756 186 for (i = 0; i < num_c_common_reswords; i++)
17fd67d5 187 {
69201cc5 188 if (c_common_reswords[i].disable & D_CONLY)
189 continue;
5c6e5756 190 id = get_identifier (c_common_reswords[i].word);
191 C_SET_RID_CODE (id, c_common_reswords[i].rid);
192 ridpointers [(int) c_common_reswords[i].rid] = id;
193 if (! (c_common_reswords[i].disable & mask))
518796ad 194 C_IS_RESERVED_WORD (id) = 1;
17fd67d5 195 }
518796ad 196}
17fd67d5 197
518796ad 198static void
42c35e28 199init_cp_pragma (void)
518796ad 200{
eb180587 201 c_register_pragma (0, "vtable", handle_pragma_vtable);
202 c_register_pragma (0, "unit", handle_pragma_unit);
203 c_register_pragma (0, "interface", handle_pragma_interface);
204 c_register_pragma (0, "implementation", handle_pragma_implementation);
205 c_register_pragma ("GCC", "interface", handle_pragma_interface);
206 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
207 c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
518796ad 208}
bac62436 209\f
820440d6 210/* TRUE if a code represents a statement. */
211
212bool statement_code_p[MAX_TREE_CODES];
213
9ceb1c29 214/* Initialize the C++ front end. This function is very sensitive to
215 the exact order that things are done here. It would be nice if the
216 initialization done by this routine were moved to its subroutines,
217 and the ordering dependencies clarified and reduced. */
03bde601 218bool
219cxx_init (void)
518796ad 220{
26cb3d1c 221 location_t saved_loc;
820440d6 222 unsigned int i;
bac62436 223 static const enum tree_code stmt_codes[] = {
820440d6 224 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
225 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
226 IF_STMT, CLEANUP_STMT, FOR_STMT,
9dd72ec4 227 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
228 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
229 EXPR_STMT
bac62436 230 };
231
820440d6 232 memset (&statement_code_p, 0, sizeof (statement_code_p));
233 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
234 statement_code_p[stmt_codes[i]] = true;
bac62436 235
26cb3d1c 236 saved_loc = input_location;
237 input_location = BUILTINS_LOCATION;
518796ad 238
239 init_reswords ();
23f83a9a 240 init_tree ();
41fb2c18 241 init_cp_semantics ();
97cc4539 242 init_operators ();
442fd60a 243 init_method ();
471086d6 244 init_error ();
cfa70726 245
471086d6 246 current_function_decl = NULL;
247
38253b86 248 class_type_node = ridpointers[(int) RID_CLASS];
471086d6 249
9ceb1c29 250 cxx_init_decl_processing ();
251
03bde601 252 if (c_common_init () == false)
0c6109e0 253 {
26cb3d1c 254 input_location = saved_loc;
0c6109e0 255 return false;
256 }
9ceb1c29 257
258 init_cp_pragma ();
259
caa6fdce 260 init_repo ();
9ceb1c29 261
26cb3d1c 262 input_location = saved_loc;
03bde601 263 return true;
471086d6 264}
471086d6 265\f
ac9386a0 266/* Return nonzero if S is not considered part of an
471086d6 267 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
96624a9e 268
471086d6 269static int
42c35e28 270interface_strcmp (const char* s)
471086d6 271{
272 /* Set the interface/implementation bits for this scope. */
273 struct impl_files *ifiles;
e1721763 274 const char *s1;
471086d6 275
471086d6 276 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
277 {
e1721763 278 const char *t1 = ifiles->filename;
471086d6 279 s1 = s;
280
82715bcd 281 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
471086d6 282 continue;
283
82715bcd 284 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
471086d6 285 s1++, t1++;
286
287 /* A match. */
288 if (*s1 == *t1)
289 return 0;
290
291 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
78dbff7c 292 if (strchr (s1, '.') || strchr (t1, '.'))
471086d6 293 continue;
294
295 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
296 continue;
297
298 /* A match. */
299 return 0;
300 }
301
302 /* No matches. */
303 return 1;
304}
305
518796ad 306\f
307
308/* Parse a #pragma whose sole argument is a string constant.
309 If OPT is true, the argument is optional. */
310static tree
42c35e28 311parse_strconst_pragma (const char* name, int opt)
518796ad 312{
313 tree result, x;
314 enum cpp_ttype t;
315
b0b1c358 316 t = pragma_lex (&result);
518796ad 317 if (t == CPP_STRING)
318 {
b5d533bb 319 if (pragma_lex (&x) != CPP_EOF)
c3ceba8e 320 warning (0, "junk at end of #pragma %s", name);
518796ad 321 return result;
322 }
323
324 if (t == CPP_EOF && opt)
b0b1c358 325 return NULL_TREE;
518796ad 326
327 error ("invalid #pragma %s", name);
b0b1c358 328 return error_mark_node;
518796ad 329}
3c3beda6 330
518796ad 331static void
a49c5913 332handle_pragma_vtable (cpp_reader* /*dfile*/)
518796ad 333{
b53db2b0 334 parse_strconst_pragma ("vtable", 0);
335 sorry ("#pragma vtable no longer supported");
518796ad 336}
337
495d2af2 338static void
a49c5913 339handle_pragma_unit (cpp_reader* /*dfile*/)
495d2af2 340{
518796ad 341 /* Validate syntax, but don't do anything. */
342 parse_strconst_pragma ("unit", 0);
343}
344
345static void
a49c5913 346handle_pragma_interface (cpp_reader* /*dfile*/)
518796ad 347{
348 tree fname = parse_strconst_pragma ("interface", 1);
349 struct c_fileinfo *finfo;
00d26680 350 const char *filename;
518796ad 351
b0b1c358 352 if (fname == error_mark_node)
518796ad 353 return;
354 else if (fname == 0)
00d26680 355 filename = lbasename (input_filename);
518796ad 356 else
b25f004c 357 filename = TREE_STRING_POINTER (fname);
518796ad 358
4507a637 359 finfo = get_fileinfo (input_filename);
495d2af2 360
361 if (impl_file_chain == 0)
362 {
363 /* If this is zero at this point, then we are
364 auto-implementing. */
365 if (main_input_filename == 0)
366 main_input_filename = input_filename;
495d2af2 367 }
368
00d26680 369 finfo->interface_only = interface_strcmp (filename);
197e583a 370 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
371 a definition in another file. */
62bf98ad 372 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
373 finfo->interface_unknown = 0;
495d2af2 374}
375
e060fd61 376/* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
377 We used to only allow this at toplevel, but that restriction was buggy
378 in older compilers and it seems reasonable to allow it in the headers
379 themselves, too. It only needs to precede the matching #p interface.
380
62bf98ad 381 We don't touch finfo->interface_only or finfo->interface_unknown;
382 the user must specify a matching #p interface for this to have
383 any effect. */
e060fd61 384
495d2af2 385static void
a49c5913 386handle_pragma_implementation (cpp_reader* /*dfile*/)
495d2af2 387{
518796ad 388 tree fname = parse_strconst_pragma ("implementation", 1);
00d26680 389 const char *filename;
e060fd61 390 struct impl_files *ifiles = impl_file_chain;
518796ad 391
b0b1c358 392 if (fname == error_mark_node)
518796ad 393 return;
394
395 if (fname == 0)
396 {
397 if (main_input_filename)
00d26680 398 filename = main_input_filename;
518796ad 399 else
00d26680 400 filename = input_filename;
401 filename = lbasename (filename);
518796ad 402 }
403 else
404 {
b25f004c 405 filename = TREE_STRING_POINTER (fname);
67015c73 406 if (cpp_included_before (parse_in, filename, input_location))
c3ceba8e 407 warning (0, "#pragma implementation for %qs appears after "
00d26680 408 "file is included", filename);
518796ad 409 }
410
e060fd61 411 for (; ifiles; ifiles = ifiles->next)
495d2af2 412 {
82715bcd 413 if (! filename_cmp (ifiles->filename, filename))
e060fd61 414 break;
495d2af2 415 }
e060fd61 416 if (ifiles == 0)
495d2af2 417 {
56e60747 418 ifiles = XNEW (struct impl_files);
b25f004c 419 ifiles->filename = xstrdup (filename);
e060fd61 420 ifiles->next = impl_file_chain;
421 impl_file_chain = ifiles;
495d2af2 422 }
495d2af2 423}
53137e6a 424
6df8a9dd 425/* Indicate that this file uses Java-personality exception handling. */
426static void
a49c5913 427handle_pragma_java_exceptions (cpp_reader* /*dfile*/)
6df8a9dd 428{
429 tree x;
b5d533bb 430 if (pragma_lex (&x) != CPP_EOF)
c3ceba8e 431 warning (0, "junk at end of #pragma GCC java_exceptions");
6df8a9dd 432
433 choose_personality_routine (lang_java);
bde8fea7 434 pragma_java_exceptions = true;
6df8a9dd 435}
436
7d172fa8 437/* Issue an error message indicating that the lookup of NAME (an
0886adbc 438 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
7d172fa8 439
0886adbc 440tree
7d172fa8 441unqualified_name_lookup_error (tree name)
442{
443 if (IDENTIFIER_OPNAME_P (name))
444 {
445 if (name != ansi_opname (ERROR_MARK))
a2c5b975 446 error ("%qD not defined", name);
7d172fa8 447 }
7d172fa8 448 else
449 {
79408174 450 if (!objc_diagnose_private_ivar (name))
f91726b4 451 {
452 error ("%qD was not declared in this scope", name);
d3c44c27 453 suggest_alternatives_for (location_of (name), name);
f91726b4 454 }
28bbd27a 455 /* Prevent repeated error messages by creating a VAR_DECL with
456 this NAME in the innermost block scope. */
fa8ed26b 457 if (local_bindings_p ())
7d172fa8 458 {
28bbd27a 459 tree decl;
e60a6f7b 460 decl = build_decl (input_location,
461 VAR_DECL, name, error_mark_node);
28bbd27a 462 DECL_CONTEXT (decl) = current_function_decl;
463 push_local_binding (name, decl, 0);
d1a64350 464 /* Mark the variable as used so that we do not get warnings
465 about it being unused later. */
466 TREE_USED (decl) = 1;
7d172fa8 467 }
7d172fa8 468 }
c324ed63 469
0886adbc 470 return error_mark_node;
e857e9c7 471}
472
0886adbc 473/* Like unqualified_name_lookup_error, but NAME is an unqualified-id
474 used as a function. Returns an appropriate expression for
475 NAME. */
476
e857e9c7 477tree
0886adbc 478unqualified_fn_lookup_error (tree name)
e857e9c7 479{
3cc0b4b9 480 if (processing_template_decl)
e857e9c7 481 {
0886adbc 482 /* In a template, it is invalid to write "f()" or "f(3)" if no
483 declaration of "f" is available. Historically, G++ and most
8ddbdfab 484 other compilers accepted that usage since they deferred all name
485 lookup until instantiation time rather than doing unqualified
9031d10b 486 name lookup at template definition time; explain to the user what
8ddbdfab 487 is going wrong.
488
489 Note that we have the exact wording of the following message in
490 the manual (trouble.texi, node "Name lookup"), so they need to
491 be kept in synch. */
2b9e3597 492 permerror (input_location, "there are no arguments to %qD that depend on a template "
07317e69 493 "parameter, so a declaration of %qD must be available",
494 name, name);
9031d10b 495
0886adbc 496 if (!flag_permissive)
e857e9c7 497 {
0886adbc 498 static bool hint;
499 if (!hint)
500 {
5bcc316e 501 inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
7668b48e 502 "code, but allowing the use of an undeclared name is "
0886adbc 503 "deprecated)");
504 hint = true;
505 }
e857e9c7 506 }
c08d51be 507 return name;
e857e9c7 508 }
471086d6 509
0886adbc 510 return unqualified_name_lookup_error (name);
471086d6 511}
512
cbdababb 513/* Wrapper around build_lang_decl_loc(). Should gradually move to
514 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
515 build_lang_decl(). */
516
471086d6 517tree
42c35e28 518build_lang_decl (enum tree_code code, tree name, tree type)
cbdababb 519{
520 return build_lang_decl_loc (input_location, code, name, type);
521}
522
523/* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
524 DECL_LANG_SPECIFIC info to the result. */
525
526tree
527build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
471086d6 528{
c4802aa6 529 tree t;
530
cbdababb 531 t = build_decl (loc, code, name, type);
4ba5f733 532 retrofit_lang_decl (t);
c4802aa6 533
4ba5f733 534 return t;
535}
536
537/* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
a17c2a3a 538 and pushdecl (for functions generated by the back end). */
4ba5f733 539
540void
42c35e28 541retrofit_lang_decl (tree t)
4ba5f733 542{
70a658bd 543 struct lang_decl *ld;
c4802aa6 544 size_t size;
39e70cbf 545 int sel;
546
547 if (TREE_CODE (t) == FUNCTION_DECL)
548 sel = 1, size = sizeof (struct lang_decl_fn);
549 else if (TREE_CODE (t) == NAMESPACE_DECL)
550 sel = 2, size = sizeof (struct lang_decl_ns);
32d008d9 551 else if (TREE_CODE (t) == PARM_DECL)
552 sel = 3, size = sizeof (struct lang_decl_parm);
39e70cbf 553 else if (LANG_DECL_HAS_MIN (t))
554 sel = 0, size = sizeof (struct lang_decl_min);
c4802aa6 555 else
39e70cbf 556 gcc_unreachable ();
c6138f2d 557
ba72912a 558 ld = ggc_alloc_cleared_lang_decl (size);
471086d6 559
39e70cbf 560 ld->u.base.selector = sel;
1f3233d1 561
70a658bd 562 DECL_LANG_SPECIFIC (t) = ld;
a8f49272 563 if (current_lang_name == lang_name_cplusplus
564 || decl_linkage (t) == lk_none)
4b1984f5 565 SET_DECL_LANGUAGE (t, lang_cplusplus);
471086d6 566 else if (current_lang_name == lang_name_c)
4b1984f5 567 SET_DECL_LANGUAGE (t, lang_c);
f468434d 568 else if (current_lang_name == lang_name_java)
4b1984f5 569 SET_DECL_LANGUAGE (t, lang_java);
092b1d6f 570 else
571 gcc_unreachable ();
471086d6 572
ecd52ea9 573 if (GATHER_STATISTICS)
574 {
575 tree_node_counts[(int)lang_decl] += 1;
576 tree_node_sizes[(int)lang_decl] += size;
577 }
471086d6 578}
579
471086d6 580void
42c35e28 581cxx_dup_lang_specific_decl (tree node)
471086d6 582{
583 int size;
a1b0969c 584 struct lang_decl *ld;
471086d6 585
e857e9c7 586 if (! DECL_LANG_SPECIFIC (node))
587 return;
588
39e70cbf 589 if (TREE_CODE (node) == FUNCTION_DECL)
590 size = sizeof (struct lang_decl_fn);
591 else if (TREE_CODE (node) == NAMESPACE_DECL)
592 size = sizeof (struct lang_decl_ns);
32d008d9 593 else if (TREE_CODE (node) == PARM_DECL)
594 size = sizeof (struct lang_decl_parm);
39e70cbf 595 else if (LANG_DECL_HAS_MIN (node))
596 size = sizeof (struct lang_decl_min);
471086d6 597 else
39e70cbf 598 gcc_unreachable ();
599
ba72912a 600 ld = ggc_alloc_lang_decl (size);
b1b63592 601 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
a1b0969c 602 DECL_LANG_SPECIFIC (node) = ld;
1d36b416 603
ecd52ea9 604 if (GATHER_STATISTICS)
605 {
606 tree_node_counts[(int)lang_decl] += 1;
607 tree_node_sizes[(int)lang_decl] += size;
608 }
471086d6 609}
610
c771ec7a 611/* Copy DECL, including any language-specific parts. */
612
613tree
42c35e28 614copy_decl (tree decl)
c771ec7a 615{
616 tree copy;
617
618 copy = copy_node (decl);
dbc42b78 619 cxx_dup_lang_specific_decl (copy);
c771ec7a 620 return copy;
621}
622
1d36b416 623/* Replace the shared language-specific parts of NODE with a new copy. */
624
f6020ad0 625static void
42c35e28 626copy_lang_type (tree node)
1d36b416 627{
628 int size;
629 struct lang_type *lt;
630
631 if (! TYPE_LANG_SPECIFIC (node))
632 return;
633
1f3233d1 634 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
635 size = sizeof (struct lang_type);
636 else
637 size = sizeof (struct lang_type_ptrmem);
ba72912a 638 lt = ggc_alloc_lang_type (size);
1d36b416 639 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
640 TYPE_LANG_SPECIFIC (node) = lt;
641
ecd52ea9 642 if (GATHER_STATISTICS)
643 {
644 tree_node_counts[(int)lang_type] += 1;
645 tree_node_sizes[(int)lang_type] += size;
646 }
1d36b416 647}
648
649/* Copy TYPE, including any language-specific parts. */
650
651tree
42c35e28 652copy_type (tree type)
1d36b416 653{
654 tree copy;
655
656 copy = copy_node (type);
657 copy_lang_type (copy);
658 return copy;
659}
660
471086d6 661tree
42c35e28 662cxx_make_type (enum tree_code code)
471086d6 663{
cd16867a 664 tree t = make_node (code);
471086d6 665
1d36b416 666 /* Create lang_type structure. */
95397ff9 667 if (RECORD_OR_UNION_CODE_P (code)
1d36b416 668 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
e4e283ec 669 {
ba72912a 670 struct lang_type *pi
671 = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
471086d6 672
dc125c61 673 TYPE_LANG_SPECIFIC (t) = pi;
1f3233d1 674 pi->u.c.h.is_lang_type_class = 1;
1d36b416 675
ecd52ea9 676 if (GATHER_STATISTICS)
677 {
678 tree_node_counts[(int)lang_type] += 1;
679 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
680 }
1d36b416 681 }
682
683 /* Set up some flags that give proper default behavior. */
95397ff9 684 if (RECORD_OR_UNION_CODE_P (code))
1d36b416 685 {
4507a637 686 struct c_fileinfo *finfo = get_fileinfo (input_filename);
62bf98ad 687 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
688 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
e4e283ec 689 }
471086d6 690
691 return t;
692}
693
9972f65e 694tree
95397ff9 695make_class_type (enum tree_code code)
9972f65e 696{
a1f71e15 697 tree t = cxx_make_type (code);
95397ff9 698 SET_CLASS_TYPE_P (t, 1);
9972f65e 699 return t;
700}
96cc8e6a 701
702/* Returns true if we are currently in the main source file, or in a
703 template instantiation started from the main source file. */
704
705bool
706in_main_input_context (void)
707{
c5dd8e06 708 struct tinst_level *tl = outermost_tinst_level();
96cc8e6a 709
710 if (tl)
82715bcd 711 return filename_cmp (main_input_filename,
712 LOCATION_FILE (tl->locus)) == 0;
96cc8e6a 713 else
82715bcd 714 return filename_cmp (main_input_filename, input_filename) == 0;
96cc8e6a 715}