]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/lex.c
gcc/ada/ChangeLog:
[thirdparty/gcc.git] / gcc / cp / lex.c
CommitLineData
471086d6 1/* Separate lexical analyzer for GNU C++.
fbd26352 2 Copyright (C) 1987-2019 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"
d42760aa 25/* For use with name_hint. */
26#define INCLUDE_UNIQUE_PTR
b3ef7553 27#include "system.h"
805e22b2 28#include "coretypes.h"
471086d6 29#include "cp-tree.h"
4cba6f60 30#include "stringpool.h"
7bedc3a0 31#include "c-family/c-pragma.h"
79408174 32#include "c-family/c-objc.h"
d42760aa 33#include "gcc-rich-location.h"
34#include "cp-name-hint.h"
471086d6 35
42c35e28 36static int interface_strcmp (const char *);
37static void init_cp_pragma (void);
518796ad 38
42c35e28 39static tree parse_strconst_pragma (const char *, int);
40static void handle_pragma_vtable (cpp_reader *);
41static void handle_pragma_unit (cpp_reader *);
42static void handle_pragma_interface (cpp_reader *);
43static void handle_pragma_implementation (cpp_reader *);
518796ad 44
42c35e28 45static void init_operators (void);
46static void copy_lang_type (tree);
471086d6 47
518796ad 48/* A constraint that can be tested at compile time. */
518796ad 49#define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
46e5e818 50
23f83a9a 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
70struct impl_files
71{
44acf429 72 const char *filename;
23f83a9a 73 struct impl_files *next;
74};
75
76static struct impl_files *impl_file_chain;
471086d6 77\f
ca58dfe1 78void
42c35e28 79cxx_finish (void)
471086d6 80{
cdc9fa3e 81 c_common_finish ();
471086d6 82}
83
a543234d 84ovl_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 };
96unsigned char ovl_op_mapping[MAX_TREE_CODES];
97unsigned char ovl_op_alternate[OVL_OP_MAX];
3c3beda6 98
991449b2 99/* Get the name of the kind of identifier T. */
100
101const char *
102get_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",
816659f9 107 "simple-op", "assign-op", "conv-op", "<reserved>udlit-op"
108 };
991449b2 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
120void
121set_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
02938df3 131/* Create and tag the internal operator name for the overloaded
132 operator PTR describes. */
133
134static tree
61fae5c4 135set_operator_ident (ovl_op_info_t *ptr)
02938df3 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
a543234d 150/* Initialize data structures that keep track of operator names. */
151
97cc4539 152static void
42c35e28 153init_operators (void)
97cc4539 154{
a543234d 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;
816659f9 184 set_identifier_kind (ident, cik_simple_op);
a543234d 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 }
97cc4539 219}
220
5c6e5756 221/* Initialize the reserved words. */
518796ad 222
9ceb1c29 223void
42c35e28 224init_reswords (void)
518796ad 225{
226 unsigned int i;
227 tree id;
5c6e5756 228 int mask = 0;
229
60777f69 230 if (cxx_dialect < cxx11)
fa769cc5 231 mask |= D_CXX11;
56c12fd4 232 if (!flag_concepts)
233 mask |= D_CXX_CONCEPTS;
6d02e6b2 234 if (!flag_tm)
235 mask |= D_TRANSMEM;
82362779 236 if (!flag_char8_t)
237 mask |= D_CXX_CHAR8_T;
5c6e5756 238 if (flag_no_asm)
239 mask |= D_ASM | D_EXT;
240 if (flag_no_gnu_keywords)
241 mask |= D_EXT;
b4d6f57b 242
243 /* The Objective-C keywords are all context-dependent. */
244 mask |= D_OBJC;
518796ad 245
25a27413 246 ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
5c6e5756 247 for (i = 0; i < num_c_common_reswords; i++)
17fd67d5 248 {
69201cc5 249 if (c_common_reswords[i].disable & D_CONLY)
250 continue;
5c6e5756 251 id = get_identifier (c_common_reswords[i].word);
252 C_SET_RID_CODE (id, c_common_reswords[i].rid);
253 ridpointers [(int) c_common_reswords[i].rid] = id;
254 if (! (c_common_reswords[i].disable & mask))
991449b2 255 set_identifier_kind (id, cik_keyword);
9f75f026 256 }
257
258 for (i = 0; i < NUM_INT_N_ENTS; i++)
259 {
260 char name[50];
261 sprintf (name, "__int%d", int_n_data[i].bitsize);
262 id = get_identifier (name);
263 C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
991449b2 264 set_identifier_kind (id, cik_keyword);
17fd67d5 265 }
518796ad 266}
17fd67d5 267
518796ad 268static void
42c35e28 269init_cp_pragma (void)
518796ad 270{
eb180587 271 c_register_pragma (0, "vtable", handle_pragma_vtable);
272 c_register_pragma (0, "unit", handle_pragma_unit);
273 c_register_pragma (0, "interface", handle_pragma_interface);
274 c_register_pragma (0, "implementation", handle_pragma_implementation);
275 c_register_pragma ("GCC", "interface", handle_pragma_interface);
276 c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
518796ad 277}
bac62436 278\f
820440d6 279/* TRUE if a code represents a statement. */
280
281bool statement_code_p[MAX_TREE_CODES];
282
9ceb1c29 283/* Initialize the C++ front end. This function is very sensitive to
284 the exact order that things are done here. It would be nice if the
285 initialization done by this routine were moved to its subroutines,
286 and the ordering dependencies clarified and reduced. */
03bde601 287bool
288cxx_init (void)
518796ad 289{
26cb3d1c 290 location_t saved_loc;
820440d6 291 unsigned int i;
bac62436 292 static const enum tree_code stmt_codes[] = {
820440d6 293 CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
294 EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
295 IF_STMT, CLEANUP_STMT, FOR_STMT,
9dd72ec4 296 RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
297 BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
7e5a76c8 298 EXPR_STMT, OMP_DEPOBJ
bac62436 299 };
300
820440d6 301 memset (&statement_code_p, 0, sizeof (statement_code_p));
302 for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
303 statement_code_p[stmt_codes[i]] = true;
bac62436 304
26cb3d1c 305 saved_loc = input_location;
306 input_location = BUILTINS_LOCATION;
518796ad 307
308 init_reswords ();
23f83a9a 309 init_tree ();
41fb2c18 310 init_cp_semantics ();
97cc4539 311 init_operators ();
442fd60a 312 init_method ();
cfa70726 313
471086d6 314 current_function_decl = NULL;
315
38253b86 316 class_type_node = ridpointers[(int) RID_CLASS];
471086d6 317
9ceb1c29 318 cxx_init_decl_processing ();
319
03bde601 320 if (c_common_init () == false)
0c6109e0 321 {
26cb3d1c 322 input_location = saved_loc;
0c6109e0 323 return false;
324 }
9ceb1c29 325
326 init_cp_pragma ();
327
caa6fdce 328 init_repo ();
9ceb1c29 329
26cb3d1c 330 input_location = saved_loc;
03bde601 331 return true;
471086d6 332}
471086d6 333\f
ac9386a0 334/* Return nonzero if S is not considered part of an
471086d6 335 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
96624a9e 336
471086d6 337static int
42c35e28 338interface_strcmp (const char* s)
471086d6 339{
340 /* Set the interface/implementation bits for this scope. */
341 struct impl_files *ifiles;
e1721763 342 const char *s1;
471086d6 343
471086d6 344 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
345 {
e1721763 346 const char *t1 = ifiles->filename;
471086d6 347 s1 = s;
348
82715bcd 349 if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
471086d6 350 continue;
351
82715bcd 352 while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
471086d6 353 s1++, t1++;
354
355 /* A match. */
356 if (*s1 == *t1)
357 return 0;
358
359 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
78dbff7c 360 if (strchr (s1, '.') || strchr (t1, '.'))
471086d6 361 continue;
362
363 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
364 continue;
365
366 /* A match. */
367 return 0;
368 }
369
370 /* No matches. */
371 return 1;
372}
373
518796ad 374\f
375
376/* Parse a #pragma whose sole argument is a string constant.
377 If OPT is true, the argument is optional. */
378static tree
42c35e28 379parse_strconst_pragma (const char* name, int opt)
518796ad 380{
381 tree result, x;
382 enum cpp_ttype t;
383
b0b1c358 384 t = pragma_lex (&result);
518796ad 385 if (t == CPP_STRING)
386 {
b5d533bb 387 if (pragma_lex (&x) != CPP_EOF)
85b9be9b 388 warning (0, "junk at end of %<#pragma %s%>", name);
518796ad 389 return result;
390 }
391
392 if (t == CPP_EOF && opt)
b0b1c358 393 return NULL_TREE;
518796ad 394
85b9be9b 395 error ("invalid %<#pragma %s%>", name);
b0b1c358 396 return error_mark_node;
518796ad 397}
3c3beda6 398
518796ad 399static void
a49c5913 400handle_pragma_vtable (cpp_reader* /*dfile*/)
518796ad 401{
b53db2b0 402 parse_strconst_pragma ("vtable", 0);
85b9be9b 403 sorry ("%<#pragma vtable%> no longer supported");
518796ad 404}
405
495d2af2 406static void
a49c5913 407handle_pragma_unit (cpp_reader* /*dfile*/)
495d2af2 408{
518796ad 409 /* Validate syntax, but don't do anything. */
410 parse_strconst_pragma ("unit", 0);
411}
412
413static void
a49c5913 414handle_pragma_interface (cpp_reader* /*dfile*/)
518796ad 415{
416 tree fname = parse_strconst_pragma ("interface", 1);
417 struct c_fileinfo *finfo;
00d26680 418 const char *filename;
518796ad 419
b0b1c358 420 if (fname == error_mark_node)
518796ad 421 return;
422 else if (fname == 0)
3df42822 423 filename = lbasename (LOCATION_FILE (input_location));
518796ad 424 else
b25f004c 425 filename = TREE_STRING_POINTER (fname);
518796ad 426
3df42822 427 finfo = get_fileinfo (LOCATION_FILE (input_location));
495d2af2 428
429 if (impl_file_chain == 0)
430 {
431 /* If this is zero at this point, then we are
432 auto-implementing. */
433 if (main_input_filename == 0)
3df42822 434 main_input_filename = LOCATION_FILE (input_location);
495d2af2 435 }
436
00d26680 437 finfo->interface_only = interface_strcmp (filename);
197e583a 438 /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
439 a definition in another file. */
62bf98ad 440 if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
441 finfo->interface_unknown = 0;
495d2af2 442}
443
e060fd61 444/* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
445 We used to only allow this at toplevel, but that restriction was buggy
446 in older compilers and it seems reasonable to allow it in the headers
447 themselves, too. It only needs to precede the matching #p interface.
448
62bf98ad 449 We don't touch finfo->interface_only or finfo->interface_unknown;
450 the user must specify a matching #p interface for this to have
451 any effect. */
e060fd61 452
495d2af2 453static void
a49c5913 454handle_pragma_implementation (cpp_reader* /*dfile*/)
495d2af2 455{
518796ad 456 tree fname = parse_strconst_pragma ("implementation", 1);
00d26680 457 const char *filename;
e060fd61 458 struct impl_files *ifiles = impl_file_chain;
518796ad 459
b0b1c358 460 if (fname == error_mark_node)
518796ad 461 return;
462
463 if (fname == 0)
464 {
465 if (main_input_filename)
00d26680 466 filename = main_input_filename;
518796ad 467 else
3df42822 468 filename = LOCATION_FILE (input_location);
00d26680 469 filename = lbasename (filename);
518796ad 470 }
471 else
472 {
b25f004c 473 filename = TREE_STRING_POINTER (fname);
67015c73 474 if (cpp_included_before (parse_in, filename, input_location))
85b9be9b 475 warning (0, "%<#pragma implementation%> for %qs appears after "
00d26680 476 "file is included", filename);
518796ad 477 }
478
e060fd61 479 for (; ifiles; ifiles = ifiles->next)
495d2af2 480 {
82715bcd 481 if (! filename_cmp (ifiles->filename, filename))
e060fd61 482 break;
495d2af2 483 }
e060fd61 484 if (ifiles == 0)
495d2af2 485 {
56e60747 486 ifiles = XNEW (struct impl_files);
b25f004c 487 ifiles->filename = xstrdup (filename);
e060fd61 488 ifiles->next = impl_file_chain;
489 impl_file_chain = ifiles;
495d2af2 490 }
495d2af2 491}
53137e6a 492
7d172fa8 493/* Issue an error message indicating that the lookup of NAME (an
0886adbc 494 IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
7d172fa8 495
0886adbc 496tree
f6203294 497unqualified_name_lookup_error (tree name, location_t loc)
7d172fa8 498{
f6203294 499 if (loc == UNKNOWN_LOCATION)
d3a3cfb8 500 loc = cp_expr_loc_or_loc (name, input_location);
f6203294 501
991449b2 502 if (IDENTIFIER_ANY_OP_P (name))
e2665e8b 503 error_at (loc, "%qD not defined", name);
7d172fa8 504 else
505 {
79408174 506 if (!objc_diagnose_private_ivar (name))
f91726b4 507 {
bc35ef65 508 auto_diagnostic_group d;
d42760aa 509 name_hint hint = suggest_alternatives_for (loc, name, true);
510 if (const char *suggestion = hint.suggestion ())
511 {
512 gcc_rich_location richloc (loc);
513 richloc.add_fixit_replace (suggestion);
514 error_at (&richloc,
515 "%qD was not declared in this scope; did you mean %qs?",
516 name, suggestion);
517 }
518 else
519 error_at (loc, "%qD was not declared in this scope", name);
f91726b4 520 }
28bbd27a 521 /* Prevent repeated error messages by creating a VAR_DECL with
522 this NAME in the innermost block scope. */
fa8ed26b 523 if (local_bindings_p ())
7d172fa8 524 {
3e584e1f 525 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
526 TREE_USED (decl) = true;
527 pushdecl (decl);
7d172fa8 528 }
7d172fa8 529 }
c324ed63 530
0886adbc 531 return error_mark_node;
e857e9c7 532}
533
f6203294 534/* Like unqualified_name_lookup_error, but NAME_EXPR is an unqualified-id
535 NAME, encapsulated with its location in a CP_EXPR, used as a function.
536 Returns an appropriate expression for NAME. */
0886adbc 537
e857e9c7 538tree
f6203294 539unqualified_fn_lookup_error (cp_expr name_expr)
e857e9c7 540{
f6203294 541 tree name = name_expr.get_value ();
542 location_t loc = name_expr.get_location ();
543 if (loc == UNKNOWN_LOCATION)
544 loc = input_location;
545
8110e96e 546 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
547 name = TREE_OPERAND (name, 0);
548
3cc0b4b9 549 if (processing_template_decl)
e857e9c7 550 {
0886adbc 551 /* In a template, it is invalid to write "f()" or "f(3)" if no
552 declaration of "f" is available. Historically, G++ and most
8ddbdfab 553 other compilers accepted that usage since they deferred all name
554 lookup until instantiation time rather than doing unqualified
9031d10b 555 name lookup at template definition time; explain to the user what
8ddbdfab 556 is going wrong.
557
558 Note that we have the exact wording of the following message in
559 the manual (trouble.texi, node "Name lookup"), so they need to
560 be kept in synch. */
f6203294 561 permerror (loc, "there are no arguments to %qD that depend on a template "
07317e69 562 "parameter, so a declaration of %qD must be available",
563 name, name);
9031d10b 564
0886adbc 565 if (!flag_permissive)
e857e9c7 566 {
0886adbc 567 static bool hint;
568 if (!hint)
569 {
f6203294 570 inform (loc, "(if you use %<-fpermissive%>, G++ will accept your "
85b9be9b 571 "code, but allowing the use of an undeclared name is "
572 "deprecated)");
0886adbc 573 hint = true;
574 }
e857e9c7 575 }
c08d51be 576 return name;
e857e9c7 577 }
471086d6 578
f6203294 579 return unqualified_name_lookup_error (name, loc);
471086d6 580}
581
ae257606 582
583/* Hasher for the conversion operator name hash table. */
b423f98b 584struct conv_type_hasher : ggc_ptr_hash<tree_node>
585{
ae257606 586 /* Hash NODE, an identifier node in the table. TYPE_UID is
587 suitable, as we're not concerned about matching canonicalness
588 here. */
589 static hashval_t hash (tree node)
590 {
591 return (hashval_t) TYPE_UID (TREE_TYPE (node));
592 }
593
594 /* Compare NODE, an identifier node in the table, against TYPE, an
595 incoming TYPE being looked up. */
596 static bool equal (tree node, tree type)
597 {
598 return TREE_TYPE (node) == type;
599 }
b423f98b 600};
601
602/* This hash table maps TYPEs to the IDENTIFIER for a conversion
603 operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
604 TYPE. */
605
606static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
607
ae257606 608/* Return an identifier for a conversion operator to TYPE. We can get
609 from the returned identifier to the type. We store TYPE, which is
610 not necessarily the canonical type, which allows us to report the
611 form the user used in error messages. All these identifiers are
612 not in the identifier hash table, and have the same string name.
613 These IDENTIFIERS are not in the identifier hash table, and all
614 have the same IDENTIFIER_STRING. */
b423f98b 615
616tree
617make_conv_op_name (tree type)
618{
b423f98b 619 if (type == error_mark_node)
620 return error_mark_node;
621
622 if (conv_type_names == NULL)
623 conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
624
ae257606 625 tree *slot = conv_type_names->find_slot_with_hash
626 (type, (hashval_t) TYPE_UID (type), INSERT);
627 tree identifier = *slot;
b423f98b 628 if (!identifier)
629 {
ae257606 630 /* Create a raw IDENTIFIER outside of the identifier hash
631 table. */
632 identifier = copy_node (conv_op_identifier);
b423f98b 633
ae257606 634 /* Just in case something managed to bind. */
635 IDENTIFIER_BINDING (identifier) = NULL;
b423f98b 636
637 /* Hang TYPE off the identifier so it can be found easily later
638 when performing conversions. */
639 TREE_TYPE (identifier) = type;
640
ae257606 641 *slot = identifier;
b423f98b 642 }
643
644 return identifier;
645}
646
cbdababb 647/* Wrapper around build_lang_decl_loc(). Should gradually move to
648 build_lang_decl_loc() and then rename build_lang_decl_loc() back to
649 build_lang_decl(). */
650
471086d6 651tree
42c35e28 652build_lang_decl (enum tree_code code, tree name, tree type)
cbdababb 653{
654 return build_lang_decl_loc (input_location, code, name, type);
655}
656
657/* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
658 DECL_LANG_SPECIFIC info to the result. */
659
660tree
661build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
471086d6 662{
c4802aa6 663 tree t;
664
cbdababb 665 t = build_decl (loc, code, name, type);
4ba5f733 666 retrofit_lang_decl (t);
c4802aa6 667
4ba5f733 668 return t;
669}
670
be2b7add 671/* Maybe add a raw lang_decl to T, a decl. Return true if it needed
672 one. */
4ba5f733 673
be2b7add 674static bool
675maybe_add_lang_decl_raw (tree t, bool decomp_p)
4ba5f733 676{
c4802aa6 677 size_t size;
be2b7add 678 lang_decl_selector sel;
800478e6 679
be2b7add 680 if (decomp_p)
681 sel = lds_decomp, size = sizeof (struct lang_decl_decomp);
3f430531 682 else if (TREE_CODE (t) == FUNCTION_DECL)
be2b7add 683 sel = lds_fn, size = sizeof (struct lang_decl_fn);
39e70cbf 684 else if (TREE_CODE (t) == NAMESPACE_DECL)
be2b7add 685 sel = lds_ns, size = sizeof (struct lang_decl_ns);
32d008d9 686 else if (TREE_CODE (t) == PARM_DECL)
be2b7add 687 sel = lds_parm, size = sizeof (struct lang_decl_parm);
39e70cbf 688 else if (LANG_DECL_HAS_MIN (t))
be2b7add 689 sel = lds_min, size = sizeof (struct lang_decl_min);
c4802aa6 690 else
be2b7add 691 return false;
c6138f2d 692
be2b7add 693 struct lang_decl *ld
694 = (struct lang_decl *) ggc_internal_cleared_alloc (size);
471086d6 695
39e70cbf 696 ld->u.base.selector = sel;
70a658bd 697 DECL_LANG_SPECIFIC (t) = ld;
1458d1aa 698
be2b7add 699 if (sel == lds_ns)
1458d1aa 700 /* Who'd create a namespace, only to put nothing in it? */
1cc0785d 701 ld->u.ns.bindings = hash_table<named_decl_hash>::create_ggc (499);
1458d1aa 702
be2b7add 703 if (GATHER_STATISTICS)
704 {
705 tree_node_counts[(int)lang_decl] += 1;
706 tree_node_sizes[(int)lang_decl] += size;
707 }
708 return true;
709}
710
711/* T has just had a decl_lang_specific added. Initialize its
712 linkage. */
713
714static void
715set_decl_linkage (tree t)
716{
a8f49272 717 if (current_lang_name == lang_name_cplusplus
718 || decl_linkage (t) == lk_none)
4b1984f5 719 SET_DECL_LANGUAGE (t, lang_cplusplus);
471086d6 720 else if (current_lang_name == lang_name_c)
4b1984f5 721 SET_DECL_LANGUAGE (t, lang_c);
092b1d6f 722 else
723 gcc_unreachable ();
be2b7add 724}
471086d6 725
be2b7add 726/* T is a VAR_DECL node that needs to be a decomposition of BASE. */
727
728void
729fit_decomposition_lang_decl (tree t, tree base)
730{
731 if (struct lang_decl *orig_ld = DECL_LANG_SPECIFIC (t))
ecd52ea9 732 {
be2b7add 733 if (orig_ld->u.base.selector == lds_min)
734 {
735 maybe_add_lang_decl_raw (t, true);
736 memcpy (DECL_LANG_SPECIFIC (t), orig_ld,
737 sizeof (struct lang_decl_min));
738 /* Reset selector, which will have been bashed by the
739 memcpy. */
740 DECL_LANG_SPECIFIC (t)->u.base.selector = lds_decomp;
741 }
742 else
743 gcc_checking_assert (orig_ld->u.base.selector == lds_decomp);
744 }
745 else
746 {
747 maybe_add_lang_decl_raw (t, true);
748 set_decl_linkage (t);
ecd52ea9 749 }
be2b7add 750
751 DECL_DECOMP_BASE (t) = base;
752}
753
754/* Add DECL_LANG_SPECIFIC info to T, if it needs one. Generally
755 every C++ decl needs one, but C builtins etc do not. */
756
757void
758retrofit_lang_decl (tree t)
759{
760 if (DECL_LANG_SPECIFIC (t))
761 return;
762
763 if (maybe_add_lang_decl_raw (t, false))
764 set_decl_linkage (t);
471086d6 765}
766
471086d6 767void
42c35e28 768cxx_dup_lang_specific_decl (tree node)
471086d6 769{
770 int size;
471086d6 771
e857e9c7 772 if (! DECL_LANG_SPECIFIC (node))
773 return;
774
be2b7add 775 switch (DECL_LANG_SPECIFIC (node)->u.base.selector)
776 {
777 case lds_min:
778 size = sizeof (struct lang_decl_min);
779 break;
780 case lds_fn:
781 size = sizeof (struct lang_decl_fn);
782 break;
783 case lds_ns:
784 size = sizeof (struct lang_decl_ns);
785 break;
786 case lds_parm:
787 size = sizeof (struct lang_decl_parm);
788 break;
789 case lds_decomp:
790 size = sizeof (struct lang_decl_decomp);
791 break;
792 default:
793 gcc_unreachable ();
794 }
39e70cbf 795
be2b7add 796 struct lang_decl *ld = (struct lang_decl *) ggc_internal_alloc (size);
b1b63592 797 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
a1b0969c 798 DECL_LANG_SPECIFIC (node) = ld;
1d36b416 799
ecd52ea9 800 if (GATHER_STATISTICS)
801 {
802 tree_node_counts[(int)lang_decl] += 1;
803 tree_node_sizes[(int)lang_decl] += size;
804 }
471086d6 805}
806
c771ec7a 807/* Copy DECL, including any language-specific parts. */
808
809tree
0b97e551 810copy_decl (tree decl MEM_STAT_DECL)
c771ec7a 811{
812 tree copy;
813
8a50b528 814 copy = copy_node (decl PASS_MEM_STAT);
dbc42b78 815 cxx_dup_lang_specific_decl (copy);
c771ec7a 816 return copy;
817}
818
1d36b416 819/* Replace the shared language-specific parts of NODE with a new copy. */
820
f6020ad0 821static void
42c35e28 822copy_lang_type (tree node)
1d36b416 823{
1d36b416 824 if (! TYPE_LANG_SPECIFIC (node))
825 return;
826
049d5ecc 827 struct lang_type *lt
828 = (struct lang_type *) ggc_internal_alloc (sizeof (struct lang_type));
829
830 memcpy (lt, TYPE_LANG_SPECIFIC (node), (sizeof (struct lang_type)));
1d36b416 831 TYPE_LANG_SPECIFIC (node) = lt;
832
ecd52ea9 833 if (GATHER_STATISTICS)
834 {
835 tree_node_counts[(int)lang_type] += 1;
049d5ecc 836 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
ecd52ea9 837 }
1d36b416 838}
839
840/* Copy TYPE, including any language-specific parts. */
841
842tree
0b97e551 843copy_type (tree type MEM_STAT_DECL)
1d36b416 844{
845 tree copy;
846
8a50b528 847 copy = copy_node (type PASS_MEM_STAT);
1d36b416 848 copy_lang_type (copy);
849 return copy;
850}
851
be2b7add 852/* Add a raw lang_type to T, a type, should it need one. */
471086d6 853
be2b7add 854static bool
855maybe_add_lang_type_raw (tree t)
856{
49a669e2 857 if (!RECORD_OR_UNION_CODE_P (TREE_CODE (t)))
6f2dbf5d 858 return false;
859
860 TYPE_LANG_SPECIFIC (t)
861 = (struct lang_type *) (ggc_internal_cleared_alloc
862 (sizeof (struct lang_type)));
1d36b416 863
6f2dbf5d 864 if (GATHER_STATISTICS)
865 {
866 tree_node_counts[(int)lang_type] += 1;
867 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1d36b416 868 }
6f2dbf5d 869
870 return true;
be2b7add 871}
872
873tree
5b63c39b 874cxx_make_type (enum tree_code code MEM_STAT_DECL)
be2b7add 875{
5b63c39b 876 tree t = make_node (code PASS_MEM_STAT);
be2b7add 877
49a669e2 878 if (maybe_add_lang_type_raw (t))
1d36b416 879 {
49a669e2 880 /* Set up some flags that give proper default behavior. */
881 struct c_fileinfo *finfo =
3df42822 882 get_fileinfo (LOCATION_FILE (input_location));
62bf98ad 883 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
884 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
e4e283ec 885 }
471086d6 886
887 return t;
888}
889
5b63c39b 890/* A wrapper without the memory stats for LANG_HOOKS_MAKE_TYPE. */
891
892tree
893cxx_make_type_hook (enum tree_code code)
894{
895 return cxx_make_type (code);
896}
897
9972f65e 898tree
5b63c39b 899make_class_type (enum tree_code code MEM_STAT_DECL)
9972f65e 900{
5b63c39b 901 tree t = cxx_make_type (code PASS_MEM_STAT);
95397ff9 902 SET_CLASS_TYPE_P (t, 1);
9972f65e 903 return t;
904}
96cc8e6a 905
906/* Returns true if we are currently in the main source file, or in a
907 template instantiation started from the main source file. */
908
909bool
910in_main_input_context (void)
911{
c5dd8e06 912 struct tinst_level *tl = outermost_tinst_level();
96cc8e6a 913
914 if (tl)
82715bcd 915 return filename_cmp (main_input_filename,
916 LOCATION_FILE (tl->locus)) == 0;
96cc8e6a 917 else
3df42822 918 return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;
96cc8e6a 919}
b423f98b 920
921#include "gt-cp-lex.h"