Line data Source code
1 : /* Separate lexical analyzer for GNU C++.
2 : Copyright (C) 1987-2023 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_MEMORY
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 : #include "langhooks.h"
36 :
37 : static int interface_strcmp (const char *);
38 : static void init_cp_pragma (void);
39 :
40 : static tree parse_strconst_pragma (const char *, int);
41 : static void handle_pragma_vtable (cpp_reader *);
42 : static void handle_pragma_unit (cpp_reader *);
43 : static void handle_pragma_interface (cpp_reader *);
44 : static void handle_pragma_implementation (cpp_reader *);
45 :
46 : static void init_operators (void);
47 : static void copy_lang_type (tree);
48 :
49 : /* A constraint that can be tested at compile time. */
50 : #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
51 :
52 : /* Functions and data structures for #pragma interface.
53 :
54 : `#pragma implementation' means that the main file being compiled
55 : is considered to implement (provide) the classes that appear in
56 : its main body. I.e., if this is file "foo.cc", and class `bar'
57 : is defined in "foo.cc", then we say that "foo.cc implements bar".
58 :
59 : All main input files "implement" themselves automagically.
60 :
61 : `#pragma interface' means that unless this file (of the form "foo.h"
62 : is not presently being included by file "foo.cc", the
63 : CLASSTYPE_INTERFACE_ONLY bit gets set. The effect is that none
64 : of the vtables nor any of the inline functions defined in foo.h
65 : will ever be output.
66 :
67 : There are cases when we want to link files such as "defs.h" and
68 : "main.cc". In this case, we give "defs.h" a `#pragma interface',
69 : and "main.cc" has `#pragma implementation "defs.h"'. */
70 :
71 : struct impl_files
72 : {
73 : const char *filename;
74 : struct impl_files *next;
75 : };
76 :
77 : static struct impl_files *impl_file_chain;
78 :
79 : void
80 89094 : cxx_finish (void)
81 : {
82 89094 : c_common_finish ();
83 89094 : }
84 :
85 : ovl_op_info_t ovl_op_info[2][OVL_OP_MAX] =
86 : {
87 : {
88 : {NULL_TREE, NULL, NULL, ERROR_MARK, OVL_OP_ERROR_MARK, 0},
89 : {NULL_TREE, NULL, NULL, NOP_EXPR, OVL_OP_NOP_EXPR, 0},
90 : #define DEF_OPERATOR(NAME, CODE, MANGLING, FLAGS) \
91 : {NULL_TREE, NAME, MANGLING, CODE, OVL_OP_##CODE, FLAGS},
92 : #define OPERATOR_TRANSITION }, { \
93 : {NULL_TREE, NULL, NULL, ERROR_MARK, OVL_OP_ERROR_MARK, 0},
94 : #include "operators.def"
95 : }
96 : };
97 : unsigned char ovl_op_mapping[MAX_TREE_CODES];
98 : unsigned char ovl_op_alternate[OVL_OP_MAX];
99 :
100 : /* Get the name of the kind of identifier T. */
101 :
102 : const char *
103 0 : get_identifier_kind_name (tree id)
104 : {
105 : /* Keep in sync with cp_id_kind enumeration. */
106 0 : static const char *const names[cik_max] = {
107 : "normal", "keyword", "constructor", "destructor",
108 : "simple-op", "assign-op", "conv-op", "<reserved>udlit-op"
109 : };
110 :
111 0 : unsigned kind = 0;
112 0 : kind |= IDENTIFIER_KIND_BIT_2 (id) << 2;
113 0 : kind |= IDENTIFIER_KIND_BIT_1 (id) << 1;
114 0 : kind |= IDENTIFIER_KIND_BIT_0 (id) << 0;
115 :
116 0 : return names[kind];
117 : }
118 :
119 : /* Set the identifier kind, which we expect to currently be zero. */
120 :
121 : void
122 20813710 : set_identifier_kind (tree id, cp_identifier_kind kind)
123 : {
124 20813710 : gcc_checking_assert (!IDENTIFIER_KIND_BIT_2 (id)
125 : & !IDENTIFIER_KIND_BIT_1 (id)
126 : & !IDENTIFIER_KIND_BIT_0 (id));
127 20813710 : IDENTIFIER_KIND_BIT_2 (id) |= (kind >> 2) & 1;
128 20813710 : IDENTIFIER_KIND_BIT_1 (id) |= (kind >> 1) & 1;
129 20813710 : IDENTIFIER_KIND_BIT_0 (id) |= (kind >> 0) & 1;
130 20813710 : }
131 :
132 : /* Create and tag the internal operator name for the overloaded
133 : operator PTR describes. */
134 :
135 : static tree
136 4998560 : set_operator_ident (ovl_op_info_t *ptr)
137 : {
138 4998560 : char buffer[32];
139 9997120 : size_t len = snprintf (buffer, sizeof (buffer), "operator%s%s",
140 4998560 : &" "[ptr->name[0] && ptr->name[0] != '_'
141 4998560 : && !ISALPHA (ptr->name[0])],
142 4998560 : ptr->name);
143 4998560 : gcc_checking_assert (len < sizeof (buffer));
144 :
145 4998560 : tree ident = get_identifier_with_length (buffer, len);
146 4998560 : ptr->identifier = ident;
147 :
148 4998560 : return ident;
149 : }
150 :
151 : /* Initialize data structures that keep track of operator names. */
152 :
153 : static void
154 89260 : init_operators (void)
155 : {
156 : /* We rely on both these being zero. */
157 89260 : gcc_checking_assert (!OVL_OP_ERROR_MARK && !ERROR_MARK);
158 :
159 : /* This loop iterates backwards because we need to move the
160 : assignment operators down to their correct slots. I.e. morally
161 : equivalent to an overlapping memmove where dest > src. Slot
162 : zero is for error_mark, so hae no operator. */
163 5177080 : for (unsigned ix = OVL_OP_MAX; --ix;)
164 : {
165 5087820 : ovl_op_info_t *op_ptr = &ovl_op_info[false][ix];
166 :
167 5087820 : if (op_ptr->name)
168 : {
169 4016700 : tree ident = set_operator_ident (op_ptr);
170 4016700 : if (unsigned index = IDENTIFIER_CP_INDEX (ident))
171 : {
172 535560 : ovl_op_info_t *bin_ptr = &ovl_op_info[false][index];
173 :
174 : /* They should only differ in unary/binary ness. */
175 535560 : gcc_checking_assert ((op_ptr->flags ^ bin_ptr->flags)
176 : == OVL_OP_FLAG_AMBIARY);
177 535560 : bin_ptr->flags |= op_ptr->flags;
178 535560 : ovl_op_alternate[index] = ix;
179 : }
180 : else
181 : {
182 3481140 : IDENTIFIER_CP_INDEX (ident) = ix;
183 3481140 : set_identifier_kind (ident, cik_simple_op);
184 : }
185 : }
186 5087820 : if (op_ptr->tree_code)
187 : {
188 5087820 : gcc_checking_assert (op_ptr->ovl_op_code == ix
189 : && !ovl_op_mapping[op_ptr->tree_code]);
190 5087820 : ovl_op_mapping[op_ptr->tree_code] = op_ptr->ovl_op_code;
191 : }
192 :
193 5087820 : ovl_op_info_t *as_ptr = &ovl_op_info[true][ix];
194 5087820 : if (as_ptr->name)
195 : {
196 : /* These will be placed at the start of the array, move to
197 : the correct slot and initialize. */
198 981860 : if (as_ptr->ovl_op_code != ix)
199 : {
200 892600 : ovl_op_info_t *dst_ptr = &ovl_op_info[true][as_ptr->ovl_op_code];
201 892600 : gcc_assert (as_ptr->ovl_op_code > ix && !dst_ptr->tree_code);
202 892600 : memcpy (dst_ptr, as_ptr, sizeof (*dst_ptr));
203 892600 : memset (as_ptr, 0, sizeof (*as_ptr));
204 892600 : as_ptr = dst_ptr;
205 : }
206 :
207 981860 : tree ident = set_operator_ident (as_ptr);
208 981860 : gcc_checking_assert (!IDENTIFIER_CP_INDEX (ident));
209 981860 : IDENTIFIER_CP_INDEX (ident) = as_ptr->ovl_op_code;
210 981860 : set_identifier_kind (ident, cik_assign_op);
211 :
212 981860 : gcc_checking_assert (!ovl_op_mapping[as_ptr->tree_code]
213 : || (ovl_op_mapping[as_ptr->tree_code]
214 : == as_ptr->ovl_op_code));
215 981860 : ovl_op_mapping[as_ptr->tree_code] = as_ptr->ovl_op_code;
216 : }
217 : }
218 89260 : }
219 :
220 : /* Initialize the reserved words. */
221 :
222 : void
223 89260 : init_reswords (void)
224 : {
225 89260 : unsigned int i;
226 89260 : tree id;
227 89260 : int mask = 0;
228 :
229 89260 : if (cxx_dialect < cxx11)
230 12669 : mask |= D_CXX11;
231 89260 : if (cxx_dialect < cxx20)
232 67402 : mask |= D_CXX20;
233 89260 : if (!flag_concepts)
234 67211 : mask |= D_CXX_CONCEPTS;
235 89260 : if (!flag_coroutines)
236 66508 : mask |= D_CXX_COROUTINES;
237 89260 : if (!flag_modules)
238 86378 : mask |= D_CXX_MODULES;
239 89260 : if (!flag_tm)
240 88880 : mask |= D_TRANSMEM;
241 89260 : if (!flag_char8_t)
242 67337 : mask |= D_CXX_CHAR8_T;
243 89260 : if (flag_no_asm)
244 4 : mask |= D_ASM | D_EXT | D_EXT11;
245 89260 : if (flag_no_gnu_keywords)
246 48782 : mask |= D_EXT | D_EXT11;
247 :
248 : /* The Objective-C keywords are all context-dependent. */
249 89260 : mask |= D_OBJC;
250 :
251 89260 : ridpointers = ggc_cleared_vec_alloc<tree> ((int) RID_MAX);
252 22225740 : for (i = 0; i < num_c_common_reswords; i++)
253 : {
254 22136480 : if (c_common_reswords[i].disable & D_CONLY)
255 2320760 : continue;
256 19815720 : id = get_identifier (c_common_reswords[i].word);
257 19815720 : C_SET_RID_CODE (id, c_common_reswords[i].rid);
258 19815720 : ridpointers [(int) c_common_reswords[i].rid] = id;
259 19815720 : if (! (c_common_reswords[i].disable & mask))
260 15454659 : set_identifier_kind (id, cik_keyword);
261 : }
262 :
263 178520 : for (i = 0; i < NUM_INT_N_ENTS; i++)
264 : {
265 89260 : char name[50];
266 89260 : sprintf (name, "__int%d", int_n_data[i].bitsize);
267 89260 : id = get_identifier (name);
268 89260 : C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
269 89260 : set_identifier_kind (id, cik_keyword);
270 :
271 89260 : sprintf (name, "__int%d__", int_n_data[i].bitsize);
272 89260 : id = get_identifier (name);
273 89260 : C_SET_RID_CODE (id, RID_FIRST_INT_N + i);
274 89260 : set_identifier_kind (id, cik_keyword);
275 : }
276 :
277 89260 : if (flag_openmp)
278 : {
279 3451 : id = get_identifier ("omp_all_memory");
280 3451 : C_SET_RID_CODE (id, RID_OMP_ALL_MEMORY);
281 3451 : set_identifier_kind (id, cik_keyword);
282 3451 : ridpointers [RID_OMP_ALL_MEMORY] = id;
283 : }
284 89260 : }
285 :
286 : static void
287 88525 : init_cp_pragma (void)
288 : {
289 88525 : c_register_pragma (0, "vtable", handle_pragma_vtable);
290 88525 : c_register_pragma (0, "unit", handle_pragma_unit);
291 88525 : c_register_pragma (0, "interface", handle_pragma_interface);
292 88525 : c_register_pragma (0, "implementation", handle_pragma_implementation);
293 88525 : c_register_pragma ("GCC", "interface", handle_pragma_interface);
294 88525 : c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
295 88525 : }
296 :
297 : /* TRUE if a code represents a statement. */
298 :
299 : bool statement_code_p[MAX_TREE_CODES];
300 :
301 : /* Initialize the C++ front end. This function is very sensitive to
302 : the exact order that things are done here. It would be nice if the
303 : initialization done by this routine were moved to its subroutines,
304 : and the ordering dependencies clarified and reduced. */
305 : bool
306 89260 : cxx_init (void)
307 : {
308 89260 : location_t saved_loc;
309 89260 : unsigned int i;
310 89260 : static const enum tree_code stmt_codes[] = {
311 : CTOR_INITIALIZER, TRY_BLOCK, HANDLER,
312 : EH_SPEC_BLOCK, USING_STMT, TAG_DEFN,
313 : IF_STMT, CLEANUP_STMT, FOR_STMT,
314 : RANGE_FOR_STMT, WHILE_STMT, DO_STMT,
315 : BREAK_STMT, CONTINUE_STMT, SWITCH_STMT,
316 : EXPR_STMT, OMP_DEPOBJ
317 : };
318 :
319 89260 : memset (&statement_code_p, 0, sizeof (statement_code_p));
320 1606680 : for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
321 1517420 : statement_code_p[stmt_codes[i]] = true;
322 :
323 89260 : saved_loc = input_location;
324 89260 : input_location = BUILTINS_LOCATION;
325 :
326 89260 : init_reswords ();
327 89260 : init_tree ();
328 89260 : init_cp_semantics ();
329 89260 : init_operators ();
330 89260 : init_method ();
331 :
332 89260 : current_function_decl = NULL;
333 :
334 89260 : class_type_node = ridpointers[(int) RID_CLASS];
335 :
336 89260 : cxx_init_decl_processing ();
337 :
338 89260 : if (c_common_init () == false)
339 : {
340 719 : input_location = saved_loc;
341 719 : return false;
342 : }
343 :
344 88525 : init_cp_pragma ();
345 :
346 88525 : input_location = saved_loc;
347 88525 : return true;
348 : }
349 :
350 : /* Return nonzero if S is not considered part of an
351 : INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
352 :
353 : static int
354 101 : interface_strcmp (const char* s)
355 : {
356 : /* Set the interface/implementation bits for this scope. */
357 101 : struct impl_files *ifiles;
358 101 : const char *s1;
359 :
360 117 : for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
361 : {
362 62 : const char *t1 = ifiles->filename;
363 62 : s1 = s;
364 :
365 62 : if (*s1 == 0 || filename_ncmp (s1, t1, 1) != 0)
366 8 : continue;
367 :
368 590 : while (*s1 != 0 && filename_ncmp (s1, t1, 1) == 0)
369 536 : s1++, t1++;
370 :
371 : /* A match. */
372 54 : if (*s1 == *t1)
373 : return 0;
374 :
375 : /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
376 24 : if (strchr (s1, '.') || strchr (t1, '.'))
377 8 : continue;
378 :
379 16 : if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
380 0 : continue;
381 :
382 : /* A match. */
383 : return 0;
384 : }
385 :
386 : /* No matches. */
387 : return 1;
388 : }
389 :
390 : /* We've just read a cpp-token, figure out our next state. Hey, this
391 : is a hand-coded co-routine! */
392 :
393 : struct module_token_filter
394 : {
395 : enum state
396 : {
397 : idle,
398 : module_first,
399 : module_cont,
400 : module_end,
401 : };
402 :
403 : enum state state : 8;
404 : bool is_import : 1;
405 : bool got_export : 1;
406 : bool got_colon : 1;
407 : bool want_dot : 1;
408 :
409 : location_t token_loc;
410 : cpp_reader *reader;
411 : module_state *module;
412 : module_state *import;
413 :
414 2882 : module_token_filter (cpp_reader *reader)
415 2882 : : state (idle), is_import (false),
416 2882 : got_export (false), got_colon (false), want_dot (false),
417 2882 : token_loc (UNKNOWN_LOCATION),
418 2882 : reader (reader), module (NULL), import (NULL)
419 : {
420 : };
421 :
422 : /* Process the next token. Note we cannot see CPP_EOF inside a
423 : pragma -- a CPP_PRAGMA_EOL always happens. */
424 28072232 : uintptr_t resume (int type, int keyword, tree value, location_t loc)
425 : {
426 28072232 : unsigned res = 0;
427 :
428 28072232 : switch (state)
429 : {
430 28062341 : case idle:
431 28062341 : if (type == CPP_KEYWORD)
432 4912761 : switch (keyword)
433 : {
434 : default:
435 : break;
436 :
437 1191 : case RID__EXPORT:
438 1191 : got_export = true;
439 1191 : res = lang_hooks::PT_begin_pragma;
440 1191 : break;
441 :
442 1593 : case RID__IMPORT:
443 1593 : is_import = true;
444 : /* FALLTHRU */
445 3078 : case RID__MODULE:
446 3078 : state = module_first;
447 3078 : want_dot = false;
448 3078 : got_colon = false;
449 3078 : token_loc = loc;
450 3078 : import = NULL;
451 3078 : if (!got_export)
452 3078 : res = lang_hooks::PT_begin_pragma;
453 : break;
454 : }
455 : break;
456 :
457 3093 : case module_first:
458 3093 : if (is_import && type == CPP_HEADER_NAME)
459 : {
460 : /* A header name. The preprocessor will have already
461 : done include searching and canonicalization. */
462 670 : state = module_end;
463 670 : goto header_unit;
464 : }
465 :
466 2423 : if (type == CPP_PADDING || type == CPP_COMMENT)
467 : break;
468 :
469 2408 : state = module_cont;
470 2408 : if (type == CPP_COLON && module)
471 : {
472 120 : got_colon = true;
473 120 : import = module;
474 120 : break;
475 : }
476 : /* FALLTHROUGH */
477 :
478 5150 : case module_cont:
479 5150 : switch (type)
480 : {
481 : case CPP_PADDING:
482 : case CPP_COMMENT:
483 : break;
484 :
485 2402 : default:
486 : /* If we ever need to pay attention to attributes for
487 : header modules, more logic will be needed. */
488 2402 : state = module_end;
489 2402 : break;
490 :
491 102 : case CPP_COLON:
492 102 : if (got_colon)
493 0 : state = module_end;
494 102 : got_colon = true;
495 : /* FALLTHROUGH */
496 269 : case CPP_DOT:
497 269 : if (!want_dot)
498 3 : state = module_end;
499 269 : want_dot = false;
500 269 : break;
501 :
502 3 : case CPP_PRAGMA_EOL:
503 3 : goto module_end;
504 :
505 2476 : case CPP_NAME:
506 2476 : if (want_dot)
507 : {
508 : /* Got name instead of [.:]. */
509 0 : state = module_end;
510 0 : break;
511 : }
512 2476 : header_unit:
513 3146 : import = get_module (value, import, got_colon);
514 3146 : want_dot = true;
515 3146 : break;
516 : }
517 : break;
518 :
519 3936 : case module_end:
520 3936 : if (type == CPP_PRAGMA_EOL)
521 : {
522 3075 : module_end:;
523 : /* End of the directive, handle the name. */
524 3078 : if (import && (is_import || !flag_header_unit))
525 5778 : if (module_state *m
526 2889 : = preprocess_module (import, token_loc, module != NULL,
527 2889 : is_import, got_export, reader))
528 1284 : if (!module)
529 1269 : module = m;
530 :
531 3078 : is_import = got_export = false;
532 3078 : state = idle;
533 : }
534 : break;
535 : }
536 :
537 28072232 : return res;
538 : }
539 : };
540 :
541 : /* Initialize or teardown. */
542 :
543 : uintptr_t
544 6465 : module_token_cdtor (cpp_reader *pfile, uintptr_t data_)
545 : {
546 6465 : if (module_token_filter *filter = reinterpret_cast<module_token_filter *> (data_))
547 : {
548 2882 : preprocessed_module (pfile);
549 2882 : delete filter;
550 2882 : data_ = 0;
551 : }
552 3583 : else if (modules_p ())
553 2882 : data_ = reinterpret_cast<uintptr_t > (new module_token_filter (pfile));
554 :
555 6465 : return data_;
556 : }
557 :
558 : uintptr_t
559 28072232 : module_token_lang (int type, int keyword, tree value, location_t loc,
560 : uintptr_t data_)
561 : {
562 28072232 : module_token_filter *filter = reinterpret_cast<module_token_filter *> (data_);
563 28071584 : return filter->resume (type, keyword, value, loc);
564 : }
565 :
566 : uintptr_t
567 1409 : module_token_pre (cpp_reader *pfile, const cpp_token *tok, uintptr_t data_)
568 : {
569 1409 : if (!tok)
570 761 : return module_token_cdtor (pfile, data_);
571 :
572 648 : int type = tok->type;
573 648 : int keyword = RID_MAX;
574 648 : tree value = NULL_TREE;
575 :
576 648 : if (tok->type == CPP_NAME)
577 : {
578 198 : value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node.node));
579 198 : if (IDENTIFIER_KEYWORD_P (value))
580 : {
581 84 : keyword = C_RID_CODE (value);
582 84 : type = CPP_KEYWORD;
583 : }
584 : }
585 450 : else if (tok->type == CPP_HEADER_NAME)
586 36 : value = build_string (tok->val.str.len, (const char *)tok->val.str.text);
587 :
588 648 : return module_token_lang (type, keyword, value, tok->src_loc, data_);
589 : }
590 :
591 : /* Parse a #pragma whose sole argument is a string constant.
592 : If OPT is true, the argument is optional. */
593 : static tree
594 155 : parse_strconst_pragma (const char* name, int opt)
595 : {
596 155 : tree result, x;
597 155 : enum cpp_ttype t;
598 :
599 155 : t = pragma_lex (&result);
600 155 : if (t == CPP_STRING)
601 : {
602 64 : if (pragma_lex (&x) != CPP_EOF)
603 0 : warning (0, "junk at end of %<#pragma %s%>", name);
604 64 : return result;
605 : }
606 :
607 91 : if (t == CPP_EOF && opt)
608 : return NULL_TREE;
609 :
610 0 : error ("invalid %<#pragma %s%>", name);
611 0 : return error_mark_node;
612 : }
613 :
614 : static void
615 0 : handle_pragma_vtable (cpp_reader* /*dfile*/)
616 : {
617 0 : parse_strconst_pragma ("vtable", 0);
618 0 : sorry ("%<#pragma vtable%> no longer supported");
619 0 : }
620 :
621 : static void
622 0 : handle_pragma_unit (cpp_reader* /*dfile*/)
623 : {
624 : /* Validate syntax, but don't do anything. */
625 0 : parse_strconst_pragma ("unit", 0);
626 0 : }
627 :
628 : static void
629 101 : handle_pragma_interface (cpp_reader* /*dfile*/)
630 : {
631 101 : tree fname = parse_strconst_pragma ("interface", 1);
632 101 : struct c_fileinfo *finfo;
633 101 : const char *filename;
634 :
635 101 : if (fname == error_mark_node)
636 : return;
637 101 : else if (fname == 0)
638 77 : filename = lbasename (LOCATION_FILE (input_location));
639 : else
640 24 : filename = TREE_STRING_POINTER (fname);
641 :
642 101 : finfo = get_fileinfo (LOCATION_FILE (input_location));
643 :
644 101 : if (impl_file_chain == 0)
645 : {
646 : /* If this is zero at this point, then we are
647 : auto-implementing. */
648 43 : if (main_input_filename == 0)
649 0 : main_input_filename = LOCATION_FILE (input_location);
650 : }
651 :
652 101 : finfo->interface_only = interface_strcmp (filename);
653 : /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
654 : a definition in another file. */
655 101 : if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
656 101 : finfo->interface_unknown = 0;
657 : }
658 :
659 : /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
660 : We used to only allow this at toplevel, but that restriction was buggy
661 : in older compilers and it seems reasonable to allow it in the headers
662 : themselves, too. It only needs to precede the matching #p interface.
663 :
664 : We don't touch finfo->interface_only or finfo->interface_unknown;
665 : the user must specify a matching #p interface for this to have
666 : any effect. */
667 :
668 : static void
669 54 : handle_pragma_implementation (cpp_reader* /*dfile*/)
670 : {
671 54 : tree fname = parse_strconst_pragma ("implementation", 1);
672 54 : const char *filename;
673 54 : struct impl_files *ifiles = impl_file_chain;
674 :
675 54 : if (fname == error_mark_node)
676 : return;
677 :
678 54 : if (fname == 0)
679 : {
680 14 : if (main_input_filename)
681 : filename = main_input_filename;
682 : else
683 0 : filename = LOCATION_FILE (input_location);
684 14 : filename = lbasename (filename);
685 : }
686 : else
687 : {
688 40 : filename = TREE_STRING_POINTER (fname);
689 40 : if (cpp_included_before (parse_in, filename, input_location))
690 4 : warning (0, "%<#pragma implementation%> for %qs appears after "
691 : "file is included", filename);
692 : }
693 :
694 58 : for (; ifiles; ifiles = ifiles->next)
695 : {
696 4 : if (! filename_cmp (ifiles->filename, filename))
697 : break;
698 : }
699 54 : if (ifiles == 0)
700 : {
701 54 : ifiles = XNEW (struct impl_files);
702 54 : ifiles->filename = xstrdup (filename);
703 54 : ifiles->next = impl_file_chain;
704 54 : impl_file_chain = ifiles;
705 : }
706 : }
707 :
708 : /* Issue an error message indicating that the lookup of NAME (an
709 : IDENTIFIER_NODE) failed. Returns the ERROR_MARK_NODE. */
710 :
711 : tree
712 1968 : unqualified_name_lookup_error (tree name, location_t loc)
713 : {
714 1968 : if (loc == UNKNOWN_LOCATION)
715 1426 : loc = cp_expr_loc_or_input_loc (name);
716 :
717 1968 : if (IDENTIFIER_ANY_OP_P (name))
718 12 : error_at (loc, "%qD not defined", name);
719 : else
720 : {
721 1956 : if (!objc_diagnose_private_ivar (name))
722 : {
723 1956 : auto_diagnostic_group d;
724 1956 : name_hint hint = suggest_alternatives_for (loc, name, true);
725 1956 : if (const char *suggestion = hint.suggestion ())
726 : {
727 292 : gcc_rich_location richloc (loc);
728 292 : richloc.add_fixit_replace (suggestion);
729 292 : error_at (&richloc,
730 : "%qD was not declared in this scope; did you mean %qs?",
731 : name, suggestion);
732 292 : }
733 : else
734 1664 : error_at (loc, "%qD was not declared in this scope", name);
735 1956 : }
736 : /* Prevent repeated error messages by creating a VAR_DECL with
737 : this NAME in the innermost block scope. */
738 1956 : if (local_bindings_p ())
739 : {
740 1600 : tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
741 1600 : TREE_USED (decl) = true;
742 1600 : pushdecl (decl);
743 : }
744 : }
745 :
746 1968 : return error_mark_node;
747 : }
748 :
749 : /* Like unqualified_name_lookup_error, but NAME_EXPR is an unqualified-id
750 : NAME, encapsulated with its location in a CP_EXPR, used as a function.
751 : Returns an appropriate expression for NAME. */
752 :
753 : tree
754 618 : unqualified_fn_lookup_error (cp_expr name_expr)
755 : {
756 618 : tree name = name_expr.get_value ();
757 618 : location_t loc = name_expr.get_location ();
758 618 : if (loc == UNKNOWN_LOCATION)
759 52 : loc = input_location;
760 :
761 618 : if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
762 1 : name = TREE_OPERAND (name, 0);
763 :
764 618 : if (processing_template_decl)
765 : {
766 : /* In a template, it is invalid to write "f()" or "f(3)" if no
767 : declaration of "f" is available. Historically, G++ and most
768 : other compilers accepted that usage since they deferred all name
769 : lookup until instantiation time rather than doing unqualified
770 : name lookup at template definition time; explain to the user what
771 : is going wrong.
772 :
773 : Note that we have the exact wording of the following message in
774 : the manual (trouble.texi, node "Name lookup"), so they need to
775 : be kept in synch. */
776 76 : permerror (loc, "there are no arguments to %qD that depend on a template "
777 : "parameter, so a declaration of %qD must be available",
778 : name, name);
779 :
780 76 : if (!flag_permissive)
781 : {
782 54 : static bool hint;
783 54 : if (!hint)
784 : {
785 44 : inform (loc, "(if you use %<-fpermissive%>, G++ will accept your "
786 : "code, but allowing the use of an undeclared name is "
787 : "deprecated)");
788 44 : hint = true;
789 : }
790 : }
791 76 : return name;
792 : }
793 :
794 542 : return unqualified_name_lookup_error (name, loc);
795 : }
796 :
797 :
798 : /* Hasher for the conversion operator name hash table. */
799 : struct conv_type_hasher : ggc_ptr_hash<tree_node>
800 : {
801 : /* Hash NODE, an identifier node in the table. TYPE_UID is
802 : suitable, as we're not concerned about matching canonicalness
803 : here. */
804 6315244 : static hashval_t hash (tree node)
805 : {
806 6315244 : return (hashval_t) TYPE_UID (TREE_TYPE (node));
807 : }
808 :
809 : /* Compare NODE, an identifier node in the table, against TYPE, an
810 : incoming TYPE being looked up. */
811 7238815 : static bool equal (tree node, tree type)
812 : {
813 7238815 : return TREE_TYPE (node) == type;
814 : }
815 : };
816 :
817 : /* This hash table maps TYPEs to the IDENTIFIER for a conversion
818 : operator to TYPE. The nodes are IDENTIFIERs whose TREE_TYPE is the
819 : TYPE. */
820 :
821 : static GTY (()) hash_table<conv_type_hasher> *conv_type_names;
822 :
823 : /* Return an identifier for a conversion operator to TYPE. We can get
824 : from the returned identifier to the type. We store TYPE, which is
825 : not necessarily the canonical type, which allows us to report the
826 : form the user used in error messages. All these identifiers are
827 : not in the identifier hash table, and have the same string name.
828 : These IDENTIFIERS are not in the identifier hash table, and all
829 : have the same IDENTIFIER_STRING. */
830 :
831 : tree
832 1244635 : make_conv_op_name (tree type)
833 : {
834 1244635 : if (type == error_mark_node)
835 : return error_mark_node;
836 :
837 1244631 : if (conv_type_names == NULL)
838 15256 : conv_type_names = hash_table<conv_type_hasher>::create_ggc (31);
839 :
840 1244631 : tree *slot = conv_type_names->find_slot_with_hash
841 1244631 : (type, (hashval_t) TYPE_UID (type), INSERT);
842 1244631 : tree identifier = *slot;
843 1244631 : if (!identifier)
844 : {
845 : /* Create a raw IDENTIFIER outside of the identifier hash
846 : table. */
847 500723 : identifier = copy_node (conv_op_identifier);
848 :
849 : /* Just in case something managed to bind. */
850 500723 : IDENTIFIER_BINDING (identifier) = NULL;
851 :
852 : /* Hang TYPE off the identifier so it can be found easily later
853 : when performing conversions. */
854 500723 : TREE_TYPE (identifier) = type;
855 :
856 500723 : *slot = identifier;
857 : }
858 :
859 : return identifier;
860 : }
861 :
862 : /* Wrapper around build_lang_decl_loc(). Should gradually move to
863 : build_lang_decl_loc() and then rename build_lang_decl_loc() back to
864 : build_lang_decl(). */
865 :
866 : tree
867 162865190 : build_lang_decl (enum tree_code code, tree name, tree type)
868 : {
869 325730380 : return build_lang_decl_loc (input_location, code, name, type);
870 : }
871 :
872 : /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
873 : DECL_LANG_SPECIFIC info to the result. */
874 :
875 : tree
876 302200897 : build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
877 : {
878 302200897 : tree t;
879 :
880 302200897 : t = build_decl (loc, code, name, type);
881 302200897 : retrofit_lang_decl (t);
882 :
883 302200897 : return t;
884 : }
885 :
886 : /* Maybe add a raw lang_decl to T, a decl. Return true if it needed
887 : one. */
888 :
889 : bool
890 690705533 : maybe_add_lang_decl_raw (tree t, bool decomp_p)
891 : {
892 690705533 : size_t size;
893 690705533 : lang_decl_selector sel;
894 :
895 690705533 : if (decomp_p)
896 : sel = lds_decomp, size = sizeof (struct lang_decl_decomp);
897 690665258 : else if (TREE_CODE (t) == FUNCTION_DECL)
898 : sel = lds_fn, size = sizeof (struct lang_decl_fn);
899 367616826 : else if (TREE_CODE (t) == NAMESPACE_DECL)
900 : sel = lds_ns, size = sizeof (struct lang_decl_ns);
901 366961618 : else if (TREE_CODE (t) == PARM_DECL)
902 : sel = lds_parm, size = sizeof (struct lang_decl_parm);
903 179012806 : else if (LANG_DECL_HAS_MIN (t))
904 : sel = lds_min, size = sizeof (struct lang_decl_min);
905 : else
906 : return false;
907 :
908 690705533 : struct lang_decl *ld
909 690705533 : = (struct lang_decl *) ggc_internal_cleared_alloc (size);
910 :
911 690705533 : ld->u.base.selector = sel;
912 690705533 : DECL_LANG_SPECIFIC (t) = ld;
913 :
914 690705533 : if (sel == lds_ns)
915 : /* Who'd create a namespace, only to put nothing in it? */
916 655208 : ld->u.ns.bindings = hash_table<named_decl_hash>::create_ggc (499);
917 :
918 : if (GATHER_STATISTICS)
919 : {
920 : tree_node_counts[(int)lang_decl] += 1;
921 : tree_node_sizes[(int)lang_decl] += size;
922 : }
923 : return true;
924 : }
925 :
926 : /* T has just had a decl_lang_specific added. Initialize its
927 : linkage. */
928 :
929 : static void
930 687995122 : set_decl_linkage (tree t)
931 : {
932 687995122 : if (current_lang_name == lang_name_cplusplus
933 687995122 : || decl_linkage (t) == lk_none)
934 442575233 : SET_DECL_LANGUAGE (t, lang_cplusplus);
935 245419889 : else if (current_lang_name == lang_name_c)
936 245419889 : SET_DECL_LANGUAGE (t, lang_c);
937 : else
938 0 : gcc_unreachable ();
939 687995122 : }
940 :
941 : /* T is a VAR_DECL node that needs to be a decomposition of BASE. */
942 :
943 : void
944 69282 : fit_decomposition_lang_decl (tree t, tree base)
945 : {
946 69282 : if (struct lang_decl *orig_ld = DECL_LANG_SPECIFIC (t))
947 : {
948 30222 : if (orig_ld->u.base.selector == lds_min)
949 : {
950 434 : maybe_add_lang_decl_raw (t, true);
951 434 : memcpy (DECL_LANG_SPECIFIC (t), orig_ld,
952 : sizeof (struct lang_decl_min));
953 : /* Reset selector, which will have been bashed by the
954 : memcpy. */
955 434 : DECL_LANG_SPECIFIC (t)->u.base.selector = lds_decomp;
956 : }
957 : else
958 29788 : gcc_checking_assert (orig_ld->u.base.selector == lds_decomp);
959 : }
960 : else
961 : {
962 39060 : maybe_add_lang_decl_raw (t, true);
963 39060 : set_decl_linkage (t);
964 : }
965 :
966 69282 : DECL_DECOMP_BASE (t) = base;
967 69282 : }
968 :
969 : /* Add DECL_LANG_SPECIFIC info to T, if it needs one. Generally
970 : every C++ decl needs one, but C builtins etc do not. */
971 :
972 : void
973 828431272 : retrofit_lang_decl (tree t)
974 : {
975 828431272 : if (DECL_LANG_SPECIFIC (t))
976 : return;
977 :
978 687956062 : if (maybe_add_lang_decl_raw (t, false))
979 687956062 : set_decl_linkage (t);
980 : }
981 :
982 : void
983 459157767 : cxx_dup_lang_specific_decl (tree node)
984 : {
985 459157767 : int size;
986 :
987 459157767 : if (! DECL_LANG_SPECIFIC (node))
988 : return;
989 :
990 351364407 : switch (DECL_LANG_SPECIFIC (node)->u.base.selector)
991 : {
992 : case lds_min:
993 : size = sizeof (struct lang_decl_min);
994 : break;
995 : case lds_fn:
996 : size = sizeof (struct lang_decl_fn);
997 : break;
998 : case lds_ns:
999 : size = sizeof (struct lang_decl_ns);
1000 : break;
1001 : case lds_parm:
1002 : size = sizeof (struct lang_decl_parm);
1003 : break;
1004 : case lds_decomp:
1005 : size = sizeof (struct lang_decl_decomp);
1006 : break;
1007 0 : default:
1008 0 : gcc_unreachable ();
1009 : }
1010 :
1011 351364407 : struct lang_decl *ld = (struct lang_decl *) ggc_internal_alloc (size);
1012 351364407 : memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1013 351364407 : DECL_LANG_SPECIFIC (node) = ld;
1014 :
1015 : /* Directly clear some flags that do not apply to the copy
1016 : (module_purview_p still does). */
1017 351364407 : ld->u.base.module_entity_p = false;
1018 351364407 : ld->u.base.module_import_p = false;
1019 351364407 : ld->u.base.module_keyed_decls_p = false;
1020 :
1021 351364407 : if (GATHER_STATISTICS)
1022 : {
1023 : tree_node_counts[(int)lang_decl] += 1;
1024 : tree_node_sizes[(int)lang_decl] += size;
1025 : }
1026 : }
1027 :
1028 : /* Copy DECL, including any language-specific parts. */
1029 :
1030 : tree
1031 286687565 : copy_decl (tree decl MEM_STAT_DECL)
1032 : {
1033 286687565 : tree copy;
1034 :
1035 286687565 : copy = copy_node (decl PASS_MEM_STAT);
1036 286687565 : cxx_dup_lang_specific_decl (copy);
1037 286687565 : return copy;
1038 : }
1039 :
1040 : /* Replace the shared language-specific parts of NODE with a new copy. */
1041 :
1042 : static void
1043 7975612 : copy_lang_type (tree node)
1044 : {
1045 7975612 : if (! TYPE_LANG_SPECIFIC (node))
1046 : return;
1047 :
1048 0 : auto *lt = (struct lang_type *) ggc_internal_alloc (sizeof (struct lang_type));
1049 :
1050 0 : memcpy (lt, TYPE_LANG_SPECIFIC (node), (sizeof (struct lang_type)));
1051 0 : TYPE_LANG_SPECIFIC (node) = lt;
1052 :
1053 0 : if (GATHER_STATISTICS)
1054 : {
1055 : tree_node_counts[(int)lang_type] += 1;
1056 : tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1057 : }
1058 : }
1059 :
1060 : /* Copy TYPE, including any language-specific parts. */
1061 :
1062 : tree
1063 7975612 : copy_type (tree type MEM_STAT_DECL)
1064 : {
1065 7975612 : tree copy;
1066 :
1067 7975612 : copy = copy_node (type PASS_MEM_STAT);
1068 7975612 : copy_lang_type (copy);
1069 7975612 : return copy;
1070 : }
1071 :
1072 : /* Add a raw lang_type to T, a type, should it need one. */
1073 :
1074 : bool
1075 447553998 : maybe_add_lang_type_raw (tree t)
1076 : {
1077 447553998 : if (!RECORD_OR_UNION_CODE_P (TREE_CODE (t)))
1078 : return false;
1079 :
1080 75552034 : auto *lt = (struct lang_type *) (ggc_internal_cleared_alloc
1081 75552034 : (sizeof (struct lang_type)));
1082 75552034 : TYPE_LANG_SPECIFIC (t) = lt;
1083 :
1084 75552034 : if (GATHER_STATISTICS)
1085 : {
1086 : tree_node_counts[(int)lang_type] += 1;
1087 : tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1088 : }
1089 :
1090 75552034 : return true;
1091 : }
1092 :
1093 : tree
1094 447343490 : cxx_make_type (enum tree_code code MEM_STAT_DECL)
1095 : {
1096 447343490 : tree t = make_node (code PASS_MEM_STAT);
1097 :
1098 447343490 : if (maybe_add_lang_type_raw (t))
1099 : {
1100 : /* Set up some flags that give proper default behavior. */
1101 75341526 : struct c_fileinfo *finfo =
1102 75341526 : get_fileinfo (LOCATION_FILE (input_location));
1103 75341526 : SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
1104 75341526 : CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
1105 : }
1106 :
1107 447343490 : if (code == RECORD_TYPE || code == UNION_TYPE)
1108 75341526 : TYPE_CXX_ODR_P (t) = 1;
1109 :
1110 447343490 : return t;
1111 : }
1112 :
1113 : /* A wrapper without the memory stats for LANG_HOOKS_MAKE_TYPE. */
1114 :
1115 : tree
1116 121192 : cxx_make_type_hook (enum tree_code code)
1117 : {
1118 121192 : return cxx_make_type (code);
1119 : }
1120 :
1121 : tree
1122 75220331 : make_class_type (enum tree_code code MEM_STAT_DECL)
1123 : {
1124 75220331 : tree t = cxx_make_type (code PASS_MEM_STAT);
1125 75220331 : SET_CLASS_TYPE_P (t, 1);
1126 75220331 : return t;
1127 : }
1128 :
1129 : /* Returns true if we are currently in the main source file, or in a
1130 : template instantiation started from the main source file. */
1131 :
1132 : bool
1133 152 : in_main_input_context (void)
1134 : {
1135 152 : struct tinst_level *tl = outermost_tinst_level();
1136 :
1137 152 : if (tl)
1138 9 : return filename_cmp (main_input_filename,
1139 18 : LOCATION_FILE (tl->locus)) == 0;
1140 : else
1141 143 : return filename_cmp (main_input_filename, LOCATION_FILE (input_location)) == 0;
1142 : }
1143 :
1144 : #include "gt-cp-lex.h"
|