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