]>
Commit | Line | Data |
---|---|---|
aed81407 | 1 | /* Definitions for C++ name lookup routines. |
a5544970 | 2 | Copyright (C) 2003-2019 Free Software Foundation, Inc. |
aed81407 GDR |
3 | Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> |
4 | ||
ed3cf953 | 5 | This file is part of GCC. |
aed81407 | 6 | |
ed3cf953 | 7 | GCC is free software; you can redistribute it and/or modify |
aed81407 | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
aed81407 GDR |
10 | any later version. |
11 | ||
ed3cf953 | 12 | GCC is distributed in the hope that it will be useful, |
aed81407 GDR |
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 | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
aed81407 GDR |
20 | |
21 | #include "config.h" | |
6c7a259b | 22 | #define INCLUDE_UNIQUE_PTR |
aed81407 GDR |
23 | #include "system.h" |
24 | #include "coretypes.h" | |
2adfab87 AM |
25 | #include "cp-tree.h" |
26 | #include "timevar.h" | |
d8a2d370 DN |
27 | #include "stringpool.h" |
28 | #include "print-tree.h" | |
29 | #include "attribs.h" | |
6097b0c3 | 30 | #include "debug.h" |
39dabefd | 31 | #include "c-family/c-pragma.h" |
501c95ff | 32 | #include "params.h" |
52ed68f7 DM |
33 | #include "gcc-rich-location.h" |
34 | #include "spellcheck-tree.h" | |
35 | #include "parser.h" | |
6c7a259b | 36 | #include "c-family/name-hint.h" |
26edace6 | 37 | #include "c-family/known-headers.h" |
613bc14f | 38 | #include "c-family/c-spellcheck.h" |
00e8de68 | 39 | |
b655c310 NS |
40 | static cxx_binding *cxx_binding_make (tree value, tree type); |
41 | static cp_binding_level *innermost_nonclass_level (void); | |
42 | static void set_identifier_type_value_with_scope (tree id, tree decl, | |
43 | cp_binding_level *b); | |
7e2de6df DM |
44 | static name_hint maybe_suggest_missing_std_header (location_t location, |
45 | tree name); | |
46 | static name_hint suggest_alternatives_for_1 (location_t location, tree name, | |
47 | bool suggest_misspellings); | |
15f8ac7f | 48 | |
3c9cca88 NS |
49 | /* Create an overload suitable for recording an artificial TYPE_DECL |
50 | and another decl. We use this machanism to implement the struct | |
51 | stat hack within a namespace. It'd be nice to use it everywhere. */ | |
52 | ||
53 | #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N)) | |
54 | #define STAT_TYPE(N) TREE_TYPE (N) | |
55 | #define STAT_DECL(N) OVL_FUNCTION (N) | |
56 | #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N) | |
57 | #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE) | |
58 | ||
3d7ff728 NS |
59 | /* Create a STAT_HACK node with DECL as the value binding and TYPE as |
60 | the type binding. */ | |
61 | ||
62 | static tree | |
63 | stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE) | |
3c9cca88 NS |
64 | { |
65 | tree result = make_node (OVERLOAD); | |
66 | ||
67 | /* Mark this as a lookup, so we can tell this is a stat hack. */ | |
68 | OVL_LOOKUP_P (result) = true; | |
69 | STAT_DECL (result) = decl; | |
70 | STAT_TYPE (result) = type; | |
71 | return result; | |
72 | } | |
73 | ||
3a9cc685 NS |
74 | /* Create a local binding level for NAME. */ |
75 | ||
76 | static cxx_binding * | |
77 | create_local_binding (cp_binding_level *level, tree name) | |
78 | { | |
79 | cxx_binding *binding = cxx_binding_make (NULL, NULL); | |
80 | ||
81 | INHERITED_VALUE_BINDING_P (binding) = false; | |
82 | LOCAL_BINDING_P (binding) = true; | |
83 | binding->scope = level; | |
84 | binding->previous = IDENTIFIER_BINDING (name); | |
85 | ||
86 | IDENTIFIER_BINDING (name) = binding; | |
87 | ||
88 | return binding; | |
89 | } | |
90 | ||
aa7bda5f NS |
91 | /* Find the binding for NAME in namespace NS. If CREATE_P is true, |
92 | make an empty binding if there wasn't one. */ | |
93 | ||
3c9cca88 NS |
94 | static tree * |
95 | find_namespace_slot (tree ns, tree name, bool create_p = false) | |
aa7bda5f | 96 | { |
e833f686 NS |
97 | tree *slot = DECL_NAMESPACE_BINDINGS (ns) |
98 | ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0, | |
99 | create_p ? INSERT : NO_INSERT); | |
98c28dd4 | 100 | return slot; |
3c9cca88 NS |
101 | } |
102 | ||
103 | static tree | |
104 | find_namespace_value (tree ns, tree name) | |
105 | { | |
106 | tree *b = find_namespace_slot (ns, name); | |
107 | ||
108 | return b ? MAYBE_STAT_DECL (*b) : NULL_TREE; | |
aa7bda5f NS |
109 | } |
110 | ||
b655c310 | 111 | /* Add DECL to the list of things declared in B. */ |
5880f14f | 112 | |
b655c310 | 113 | static void |
9d029ddf | 114 | add_decl_to_level (cp_binding_level *b, tree decl) |
5880f14f | 115 | { |
5256a7f5 | 116 | gcc_assert (b->kind != sk_class); |
b655c310 | 117 | |
c957e9c0 NS |
118 | /* Make sure we don't create a circular list. xref_tag can end |
119 | up pushing the same artificial decl more than once. We | |
120 | should have already detected that in update_binding. */ | |
121 | gcc_assert (b->names != decl); | |
122 | ||
123 | /* We build up the list in reverse order, and reverse it later if | |
124 | necessary. */ | |
125 | TREE_CHAIN (decl) = b->names; | |
126 | b->names = decl; | |
127 | ||
128 | /* If appropriate, add decl to separate list of statics. We | |
129 | include extern variables because they might turn out to be | |
130 | static later. It's OK for this list to contain a few false | |
131 | positives. */ | |
132 | if (b->kind == sk_namespace | |
133 | && ((VAR_P (decl) | |
134 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) | |
135 | || (TREE_CODE (decl) == FUNCTION_DECL | |
136 | && (!TREE_PUBLIC (decl) | |
137 | || decl_anon_ns_mem_p (decl) | |
138 | || DECL_DECLARED_INLINE_P (decl))))) | |
139 | vec_safe_push (static_decls, decl); | |
b655c310 | 140 | } |
daafa301 | 141 | |
59a4ede9 NS |
142 | /* Find the binding for NAME in the local binding level B. */ |
143 | ||
144 | static cxx_binding * | |
145 | find_local_binding (cp_binding_level *b, tree name) | |
146 | { | |
147 | if (cxx_binding *binding = IDENTIFIER_BINDING (name)) | |
148 | for (;; b = b->level_chain) | |
149 | { | |
ee336e84 | 150 | if (binding->scope == b) |
59a4ede9 NS |
151 | return binding; |
152 | ||
153 | /* Cleanup contours are transparent to the language. */ | |
154 | if (b->kind != sk_cleanup) | |
155 | break; | |
156 | } | |
157 | return NULL; | |
158 | } | |
159 | ||
f35a733d | 160 | struct name_lookup |
b655c310 | 161 | { |
932f48ac NS |
162 | public: |
163 | typedef std::pair<tree, tree> using_pair; | |
164 | typedef vec<using_pair, va_heap, vl_embed> using_queue; | |
165 | ||
f35a733d NS |
166 | public: |
167 | tree name; /* The identifier being looked for. */ | |
168 | tree value; /* A (possibly ambiguous) set of things found. */ | |
169 | tree type; /* A type that has been found. */ | |
9dda0ace | 170 | int flags; /* Lookup flags. */ |
3d7ff728 NS |
171 | bool deduping; /* Full deduping is needed because using declarations |
172 | are in play. */ | |
f35a733d NS |
173 | vec<tree, va_heap, vl_embed> *scopes; |
174 | name_lookup *previous; /* Previously active lookup. */ | |
f35a733d NS |
175 | |
176 | protected: | |
177 | /* Marked scope stack for outermost name lookup. */ | |
178 | static vec<tree, va_heap, vl_embed> *shared_scopes; | |
179 | /* Currently active lookup. */ | |
180 | static name_lookup *active; | |
181 | ||
182 | public: | |
9dda0ace NS |
183 | name_lookup (tree n, int f = 0) |
184 | : name (n), value (NULL_TREE), type (NULL_TREE), flags (f), | |
3d7ff728 | 185 | deduping (false), scopes (NULL), previous (NULL) |
f35a733d NS |
186 | { |
187 | preserve_state (); | |
188 | } | |
189 | ~name_lookup () | |
190 | { | |
f35a733d NS |
191 | restore_state (); |
192 | } | |
193 | ||
194 | private: /* Uncopyable, unmovable, unassignable. I am a rock. */ | |
195 | name_lookup (const name_lookup &); | |
196 | name_lookup &operator= (const name_lookup &); | |
197 | ||
198 | protected: | |
199 | static bool seen_p (tree scope) | |
200 | { | |
201 | return LOOKUP_SEEN_P (scope); | |
202 | } | |
203 | static bool found_p (tree scope) | |
204 | { | |
205 | return LOOKUP_FOUND_P (scope); | |
206 | } | |
207 | ||
208 | void mark_seen (tree scope); /* Mark and add to scope vector. */ | |
209 | static void mark_found (tree scope) | |
210 | { | |
211 | gcc_checking_assert (seen_p (scope)); | |
212 | LOOKUP_FOUND_P (scope) = true; | |
213 | } | |
214 | bool see_and_mark (tree scope) | |
215 | { | |
216 | bool ret = seen_p (scope); | |
217 | if (!ret) | |
218 | mark_seen (scope); | |
219 | return ret; | |
220 | } | |
221 | bool find_and_mark (tree scope); | |
222 | ||
223 | private: | |
224 | void preserve_state (); | |
225 | void restore_state (); | |
226 | ||
9dda0ace NS |
227 | private: |
228 | static tree ambiguous (tree thing, tree current); | |
3d7ff728 | 229 | void add_overload (tree fns); |
9dda0ace NS |
230 | void add_value (tree new_val); |
231 | void add_type (tree new_type); | |
232 | bool process_binding (tree val_bind, tree type_bind); | |
233 | ||
234 | /* Look in only namespace. */ | |
235 | bool search_namespace_only (tree scope); | |
236 | /* Look in namespace and its (recursive) inlines. Ignore using | |
237 | directives. Return true if something found (inc dups). */ | |
238 | bool search_namespace (tree scope); | |
239 | /* Look in the using directives of namespace + inlines using | |
240 | qualified lookup rules. */ | |
241 | bool search_usings (tree scope); | |
242 | ||
932f48ac NS |
243 | private: |
244 | using_queue *queue_namespace (using_queue *queue, int depth, tree scope); | |
3c9feefc NS |
245 | using_queue *do_queue_usings (using_queue *queue, int depth, |
246 | vec<tree, va_gc> *usings); | |
247 | using_queue *queue_usings (using_queue *queue, int depth, | |
248 | vec<tree, va_gc> *usings) | |
932f48ac NS |
249 | { |
250 | if (usings) | |
251 | queue = do_queue_usings (queue, depth, usings); | |
252 | return queue; | |
253 | } | |
254 | ||
9dda0ace | 255 | private: |
f35a733d NS |
256 | void add_fns (tree); |
257 | ||
258 | void adl_expr (tree); | |
259 | void adl_type (tree); | |
260 | void adl_template_arg (tree); | |
261 | void adl_class (tree); | |
262 | void adl_bases (tree); | |
263 | void adl_class_only (tree); | |
264 | void adl_namespace (tree); | |
265 | void adl_namespace_only (tree); | |
266 | ||
267 | public: | |
9dda0ace NS |
268 | /* Search namespace + inlines + maybe usings as qualified lookup. */ |
269 | bool search_qualified (tree scope, bool usings = true); | |
270 | ||
932f48ac NS |
271 | /* Search namespace + inlines + usings as unqualified lookup. */ |
272 | bool search_unqualified (tree scope, cp_binding_level *); | |
273 | ||
9dda0ace | 274 | /* ADL lookup of ARGS. */ |
f35a733d | 275 | tree search_adl (tree fns, vec<tree, va_gc> *args); |
b655c310 | 276 | }; |
8db29d88 | 277 | |
f35a733d NS |
278 | /* Scope stack shared by all outermost lookups. This avoids us |
279 | allocating and freeing on every single lookup. */ | |
280 | vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes; | |
8db29d88 | 281 | |
f35a733d NS |
282 | /* Currently active lookup. */ |
283 | name_lookup *name_lookup::active; | |
284 | ||
285 | /* Name lookup is recursive, becase ADL can cause template | |
286 | instatiation. This is of course a rare event, so we optimize for | |
287 | it not happening. When we discover an active name-lookup, which | |
288 | must be an ADL lookup, we need to unmark the marked scopes and also | |
289 | unmark the lookup we might have been accumulating. */ | |
290 | ||
291 | void | |
292 | name_lookup::preserve_state () | |
8db29d88 | 293 | { |
f35a733d NS |
294 | previous = active; |
295 | if (previous) | |
296 | { | |
297 | unsigned length = vec_safe_length (previous->scopes); | |
298 | vec_safe_reserve (previous->scopes, length * 2); | |
299 | for (unsigned ix = length; ix--;) | |
300 | { | |
301 | tree decl = (*previous->scopes)[ix]; | |
302 | ||
303 | gcc_checking_assert (LOOKUP_SEEN_P (decl)); | |
304 | LOOKUP_SEEN_P (decl) = false; | |
305 | ||
306 | /* Preserve the FOUND_P state on the interrupted lookup's | |
307 | stack. */ | |
308 | if (LOOKUP_FOUND_P (decl)) | |
309 | { | |
310 | LOOKUP_FOUND_P (decl) = false; | |
311 | previous->scopes->quick_push (decl); | |
312 | } | |
313 | } | |
32196b87 NS |
314 | |
315 | /* Unmark the outer partial lookup. */ | |
3d7ff728 NS |
316 | if (previous->deduping) |
317 | lookup_mark (previous->value, false); | |
f35a733d | 318 | } |
b655c310 | 319 | else |
f35a733d NS |
320 | scopes = shared_scopes; |
321 | active = this; | |
8db29d88 AO |
322 | } |
323 | ||
f35a733d | 324 | /* Restore the marking state of a lookup we interrupted. */ |
daafa301 | 325 | |
f35a733d NS |
326 | void |
327 | name_lookup::restore_state () | |
5e0c54e5 | 328 | { |
3d7ff728 NS |
329 | if (deduping) |
330 | lookup_mark (value, false); | |
331 | ||
f35a733d NS |
332 | /* Unmark and empty this lookup's scope stack. */ |
333 | for (unsigned ix = vec_safe_length (scopes); ix--;) | |
334 | { | |
335 | tree decl = scopes->pop (); | |
336 | gcc_checking_assert (LOOKUP_SEEN_P (decl)); | |
337 | LOOKUP_SEEN_P (decl) = false; | |
338 | LOOKUP_FOUND_P (decl) = false; | |
339 | } | |
5e0c54e5 | 340 | |
f35a733d NS |
341 | active = previous; |
342 | if (previous) | |
5e0c54e5 | 343 | { |
32196b87 NS |
344 | free (scopes); |
345 | ||
f35a733d NS |
346 | unsigned length = vec_safe_length (previous->scopes); |
347 | for (unsigned ix = 0; ix != length; ix++) | |
b655c310 | 348 | { |
f35a733d NS |
349 | tree decl = (*previous->scopes)[ix]; |
350 | if (LOOKUP_SEEN_P (decl)) | |
351 | { | |
352 | /* The remainder of the scope stack must be recording | |
353 | FOUND_P decls, which we want to pop off. */ | |
354 | do | |
355 | { | |
356 | tree decl = previous->scopes->pop (); | |
357 | gcc_checking_assert (LOOKUP_SEEN_P (decl) | |
358 | && !LOOKUP_FOUND_P (decl)); | |
359 | LOOKUP_FOUND_P (decl) = true; | |
360 | } | |
361 | while (++ix != length); | |
362 | break; | |
363 | } | |
364 | ||
365 | gcc_checking_assert (!LOOKUP_FOUND_P (decl)); | |
366 | LOOKUP_SEEN_P (decl) = true; | |
b655c310 | 367 | } |
5e0c54e5 | 368 | |
32196b87 | 369 | /* Remark the outer partial lookup. */ |
3d7ff728 NS |
370 | if (previous->deduping) |
371 | lookup_mark (previous->value, true); | |
f35a733d NS |
372 | } |
373 | else | |
374 | shared_scopes = scopes; | |
5e0c54e5 | 375 | } |
5e0c54e5 | 376 | |
f35a733d NS |
377 | void |
378 | name_lookup::mark_seen (tree scope) | |
b655c310 | 379 | { |
f35a733d NS |
380 | gcc_checking_assert (!seen_p (scope)); |
381 | LOOKUP_SEEN_P (scope) = true; | |
382 | vec_safe_push (scopes, scope); | |
383 | } | |
daafa301 | 384 | |
f35a733d NS |
385 | bool |
386 | name_lookup::find_and_mark (tree scope) | |
387 | { | |
388 | bool result = LOOKUP_FOUND_P (scope); | |
389 | if (!result) | |
390 | { | |
391 | LOOKUP_FOUND_P (scope) = true; | |
392 | if (!LOOKUP_SEEN_P (scope)) | |
393 | vec_safe_push (scopes, scope); | |
394 | } | |
5e0c54e5 | 395 | |
f35a733d NS |
396 | return result; |
397 | } | |
daafa301 | 398 | |
9dda0ace NS |
399 | /* THING and CURRENT are ambiguous, concatenate them. */ |
400 | ||
401 | tree | |
402 | name_lookup::ambiguous (tree thing, tree current) | |
403 | { | |
404 | if (TREE_CODE (current) != TREE_LIST) | |
405 | { | |
406 | current = build_tree_list (NULL_TREE, current); | |
407 | TREE_TYPE (current) = error_mark_node; | |
408 | } | |
409 | current = tree_cons (NULL_TREE, thing, current); | |
410 | TREE_TYPE (current) = error_mark_node; | |
411 | ||
412 | return current; | |
413 | } | |
414 | ||
3d7ff728 NS |
415 | /* FNS is a new overload set to add to the exising set. */ |
416 | ||
417 | void | |
418 | name_lookup::add_overload (tree fns) | |
419 | { | |
420 | if (!deduping && TREE_CODE (fns) == OVERLOAD) | |
421 | { | |
422 | tree probe = fns; | |
423 | if (flags & LOOKUP_HIDDEN) | |
424 | probe = ovl_skip_hidden (probe); | |
8e82c473 NS |
425 | if (probe && TREE_CODE (probe) == OVERLOAD |
426 | && OVL_DEDUP_P (probe)) | |
3d7ff728 NS |
427 | { |
428 | /* We're about to add something found by a using | |
429 | declaration, so need to engage deduping mode. */ | |
430 | lookup_mark (value, true); | |
431 | deduping = true; | |
432 | } | |
433 | } | |
434 | ||
435 | value = lookup_maybe_add (fns, value, deduping); | |
436 | } | |
437 | ||
9dda0ace NS |
438 | /* Add a NEW_VAL, a found value binding into the current value binding. */ |
439 | ||
440 | void | |
441 | name_lookup::add_value (tree new_val) | |
442 | { | |
3d7ff728 NS |
443 | if (OVL_P (new_val) && (!value || OVL_P (value))) |
444 | add_overload (new_val); | |
445 | else if (!value) | |
9dda0ace NS |
446 | value = new_val; |
447 | else if (value == new_val) | |
448 | ; | |
449 | else if ((TREE_CODE (value) == TYPE_DECL | |
450 | && TREE_CODE (new_val) == TYPE_DECL | |
451 | && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val)))) | |
71bbbd13 NS |
452 | /* Typedefs to the same type. */; |
453 | else if (TREE_CODE (value) == NAMESPACE_DECL | |
454 | && TREE_CODE (new_val) == NAMESPACE_DECL | |
455 | && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val)) | |
456 | /* Namespace (possibly aliased) to the same namespace. Locate | |
457 | the namespace*/ | |
458 | value = ORIGINAL_NAMESPACE (value); | |
9dda0ace | 459 | else |
3d7ff728 NS |
460 | { |
461 | if (deduping) | |
462 | { | |
463 | /* Disengage deduping mode. */ | |
464 | lookup_mark (value, false); | |
465 | deduping = false; | |
466 | } | |
467 | value = ambiguous (new_val, value); | |
468 | } | |
9dda0ace NS |
469 | } |
470 | ||
471 | /* Add a NEW_TYPE, a found type binding into the current type binding. */ | |
472 | ||
473 | void | |
474 | name_lookup::add_type (tree new_type) | |
475 | { | |
476 | if (!type) | |
477 | type = new_type; | |
478 | else if (TREE_CODE (type) == TREE_LIST | |
479 | || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type))) | |
480 | type = ambiguous (new_type, type); | |
481 | } | |
482 | ||
483 | /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns | |
484 | true if we actually found something noteworthy. */ | |
485 | ||
486 | bool | |
487 | name_lookup::process_binding (tree new_val, tree new_type) | |
488 | { | |
489 | /* Did we really see a type? */ | |
490 | if (new_type | |
491 | && (LOOKUP_NAMESPACES_ONLY (flags) | |
492 | || (!(flags & LOOKUP_HIDDEN) | |
493 | && DECL_LANG_SPECIFIC (new_type) | |
494 | && DECL_ANTICIPATED (new_type)))) | |
495 | new_type = NULL_TREE; | |
496 | ||
497 | if (new_val && !(flags & LOOKUP_HIDDEN)) | |
498 | new_val = ovl_skip_hidden (new_val); | |
499 | ||
500 | /* Do we really see a value? */ | |
501 | if (new_val) | |
502 | switch (TREE_CODE (new_val)) | |
503 | { | |
504 | case TEMPLATE_DECL: | |
505 | /* If we expect types or namespaces, and not templates, | |
506 | or this is not a template class. */ | |
507 | if ((LOOKUP_QUALIFIERS_ONLY (flags) | |
508 | && !DECL_TYPE_TEMPLATE_P (new_val))) | |
509 | new_val = NULL_TREE; | |
510 | break; | |
511 | case TYPE_DECL: | |
512 | if (LOOKUP_NAMESPACES_ONLY (flags) | |
513 | || (new_type && (flags & LOOKUP_PREFER_TYPES))) | |
514 | new_val = NULL_TREE; | |
515 | break; | |
516 | case NAMESPACE_DECL: | |
517 | if (LOOKUP_TYPES_ONLY (flags)) | |
518 | new_val = NULL_TREE; | |
519 | break; | |
520 | default: | |
521 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
522 | new_val = NULL_TREE; | |
523 | } | |
524 | ||
525 | if (!new_val) | |
526 | { | |
527 | new_val = new_type; | |
528 | new_type = NULL_TREE; | |
529 | } | |
530 | ||
531 | /* Merge into the lookup */ | |
532 | if (new_val) | |
533 | add_value (new_val); | |
534 | if (new_type) | |
535 | add_type (new_type); | |
536 | ||
537 | return new_val != NULL_TREE; | |
538 | } | |
539 | ||
540 | /* Look in exactly namespace SCOPE. */ | |
541 | ||
542 | bool | |
543 | name_lookup::search_namespace_only (tree scope) | |
544 | { | |
545 | bool found = false; | |
546 | ||
3c9cca88 NS |
547 | if (tree *binding = find_namespace_slot (scope, name)) |
548 | found |= process_binding (MAYBE_STAT_DECL (*binding), | |
549 | MAYBE_STAT_TYPE (*binding)); | |
9dda0ace NS |
550 | |
551 | return found; | |
552 | } | |
553 | ||
554 | /* Conditionally look in namespace SCOPE and inline children. */ | |
555 | ||
556 | bool | |
557 | name_lookup::search_namespace (tree scope) | |
558 | { | |
559 | if (see_and_mark (scope)) | |
560 | /* We've visited this scope before. Return what we found then. */ | |
561 | return found_p (scope); | |
562 | ||
563 | /* Look in exactly namespace. */ | |
564 | bool found = search_namespace_only (scope); | |
7a4e1f7d NS |
565 | |
566 | /* Don't look into inline children, if we're looking for an | |
567 | anonymous name -- it must be in the current scope, if anywhere. */ | |
568 | if (name) | |
569 | /* Recursively look in its inline children. */ | |
570 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) | |
571 | for (unsigned ix = inlinees->length (); ix--;) | |
572 | found |= search_namespace ((*inlinees)[ix]); | |
9dda0ace NS |
573 | |
574 | if (found) | |
575 | mark_found (scope); | |
576 | ||
577 | return found; | |
578 | } | |
579 | ||
580 | /* Recursively follow using directives of SCOPE & its inline children. | |
581 | Such following is essentially a flood-fill algorithm. */ | |
582 | ||
583 | bool | |
584 | name_lookup::search_usings (tree scope) | |
585 | { | |
586 | /* We do not check seen_p here, as that was already set during the | |
587 | namespace_only walk. */ | |
588 | if (found_p (scope)) | |
589 | return true; | |
590 | ||
591 | bool found = false; | |
3c9feefc NS |
592 | if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope)) |
593 | for (unsigned ix = usings->length (); ix--;) | |
594 | found |= search_qualified ((*usings)[ix], true); | |
9dda0ace NS |
595 | |
596 | /* Look in its inline children. */ | |
3c9feefc NS |
597 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
598 | for (unsigned ix = inlinees->length (); ix--;) | |
599 | found |= search_usings ((*inlinees)[ix]); | |
9dda0ace NS |
600 | |
601 | if (found) | |
602 | mark_found (scope); | |
603 | ||
604 | return found; | |
605 | } | |
606 | ||
607 | /* Qualified namespace lookup in SCOPE. | |
608 | 1) Look in SCOPE (+inlines). If found, we're done. | |
609 | 2) Otherwise, if USINGS is true, | |
610 | recurse for every using directive of SCOPE (+inlines). | |
611 | ||
612 | Trickiness is (a) loops and (b) multiple paths to same namespace. | |
613 | In both cases we want to not repeat any lookups, and know whether | |
614 | to stop the caller's step #2. Do this via the FOUND_P marker. */ | |
615 | ||
616 | bool | |
617 | name_lookup::search_qualified (tree scope, bool usings) | |
618 | { | |
619 | bool found = false; | |
620 | ||
621 | if (seen_p (scope)) | |
622 | found = found_p (scope); | |
623 | else | |
624 | { | |
625 | found = search_namespace (scope); | |
626 | if (!found && usings) | |
627 | found = search_usings (scope); | |
628 | } | |
629 | ||
630 | return found; | |
631 | } | |
632 | ||
932f48ac NS |
633 | /* Add SCOPE to the unqualified search queue, recursively add its |
634 | inlines and those via using directives. */ | |
635 | ||
636 | name_lookup::using_queue * | |
637 | name_lookup::queue_namespace (using_queue *queue, int depth, tree scope) | |
638 | { | |
639 | if (see_and_mark (scope)) | |
640 | return queue; | |
641 | ||
642 | /* Record it. */ | |
643 | tree common = scope; | |
644 | while (SCOPE_DEPTH (common) > depth) | |
645 | common = CP_DECL_CONTEXT (common); | |
646 | vec_safe_push (queue, using_pair (common, scope)); | |
647 | ||
648 | /* Queue its inline children. */ | |
3c9feefc NS |
649 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
650 | for (unsigned ix = inlinees->length (); ix--;) | |
651 | queue = queue_namespace (queue, depth, (*inlinees)[ix]); | |
932f48ac NS |
652 | |
653 | /* Queue its using targets. */ | |
654 | queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope)); | |
655 | ||
656 | return queue; | |
657 | } | |
658 | ||
659 | /* Add the namespaces in USINGS to the unqualified search queue. */ | |
660 | ||
661 | name_lookup::using_queue * | |
3c9feefc NS |
662 | name_lookup::do_queue_usings (using_queue *queue, int depth, |
663 | vec<tree, va_gc> *usings) | |
932f48ac | 664 | { |
3c9feefc NS |
665 | for (unsigned ix = usings->length (); ix--;) |
666 | queue = queue_namespace (queue, depth, (*usings)[ix]); | |
932f48ac NS |
667 | |
668 | return queue; | |
669 | } | |
670 | ||
671 | /* Unqualified namespace lookup in SCOPE. | |
672 | 1) add scope+inlins to worklist. | |
673 | 2) recursively add target of every using directive | |
674 | 3) for each worklist item where SCOPE is common ancestor, search it | |
675 | 4) if nothing find, scope=parent, goto 1. */ | |
676 | ||
677 | bool | |
678 | name_lookup::search_unqualified (tree scope, cp_binding_level *level) | |
679 | { | |
680 | /* Make static to avoid continual reallocation. We're not | |
681 | recursive. */ | |
682 | static using_queue *queue = NULL; | |
683 | bool found = false; | |
684 | int length = vec_safe_length (queue); | |
685 | ||
686 | /* Queue local using-directives. */ | |
687 | for (; level->kind != sk_namespace; level = level->level_chain) | |
688 | queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives); | |
689 | ||
690 | for (; !found; scope = CP_DECL_CONTEXT (scope)) | |
691 | { | |
692 | gcc_assert (!DECL_NAMESPACE_ALIAS (scope)); | |
693 | int depth = SCOPE_DEPTH (scope); | |
694 | ||
695 | /* Queue namespaces reachable from SCOPE. */ | |
696 | queue = queue_namespace (queue, depth, scope); | |
697 | ||
698 | /* Search every queued namespace where SCOPE is the common | |
699 | ancestor. Adjust the others. */ | |
700 | unsigned ix = length; | |
701 | do | |
702 | { | |
703 | using_pair &pair = (*queue)[ix]; | |
704 | while (pair.first == scope) | |
705 | { | |
706 | found |= search_namespace_only (pair.second); | |
707 | pair = queue->pop (); | |
708 | if (ix == queue->length ()) | |
709 | goto done; | |
710 | } | |
711 | /* The depth is the same as SCOPE, find the parent scope. */ | |
712 | if (SCOPE_DEPTH (pair.first) == depth) | |
713 | pair.first = CP_DECL_CONTEXT (pair.first); | |
714 | ix++; | |
715 | } | |
716 | while (ix < queue->length ()); | |
717 | done:; | |
718 | if (scope == global_namespace) | |
719 | break; | |
02c7dd78 NS |
720 | |
721 | /* If looking for hidden names, we only look in the innermost | |
722 | namespace scope. [namespace.memdef]/3 If a friend | |
723 | declaration in a non-local class first declares a class, | |
724 | function, class template or function template the friend is a | |
725 | member of the innermost enclosing namespace. See also | |
726 | [basic.lookup.unqual]/7 */ | |
727 | if (flags & LOOKUP_HIDDEN) | |
728 | break; | |
932f48ac NS |
729 | } |
730 | ||
731 | vec_safe_truncate (queue, length); | |
732 | ||
733 | return found; | |
734 | } | |
735 | ||
f35a733d NS |
736 | /* FNS is a value binding. If it is a (set of overloaded) functions, |
737 | add them into the current value. */ | |
5e0c54e5 | 738 | |
f35a733d NS |
739 | void |
740 | name_lookup::add_fns (tree fns) | |
741 | { | |
742 | if (!fns) | |
743 | return; | |
744 | else if (TREE_CODE (fns) == OVERLOAD) | |
5e0c54e5 | 745 | { |
f35a733d NS |
746 | if (TREE_TYPE (fns) != unknown_type_node) |
747 | fns = OVL_FUNCTION (fns); | |
b655c310 | 748 | } |
f35a733d NS |
749 | else if (!DECL_DECLARES_FUNCTION_P (fns)) |
750 | return; | |
daafa301 | 751 | |
3d7ff728 | 752 | add_overload (fns); |
5e0c54e5 GDR |
753 | } |
754 | ||
f35a733d | 755 | /* Add functions of a namespace to the lookup structure. */ |
daafa301 | 756 | |
f35a733d NS |
757 | void |
758 | name_lookup::adl_namespace_only (tree scope) | |
5e0c54e5 | 759 | { |
f35a733d | 760 | mark_seen (scope); |
5e0c54e5 | 761 | |
f35a733d | 762 | /* Look down into inline namespaces. */ |
3c9feefc NS |
763 | if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) |
764 | for (unsigned ix = inlinees->length (); ix--;) | |
765 | adl_namespace_only ((*inlinees)[ix]); | |
b655c310 | 766 | |
3c9cca88 NS |
767 | if (tree fns = find_namespace_value (scope, name)) |
768 | add_fns (ovl_skip_hidden (fns)); | |
f35a733d | 769 | } |
0cbd7506 | 770 | |
f35a733d NS |
771 | /* Find the containing non-inlined namespace, add it and all its |
772 | inlinees. */ | |
b655c310 | 773 | |
f35a733d NS |
774 | void |
775 | name_lookup::adl_namespace (tree scope) | |
776 | { | |
777 | if (seen_p (scope)) | |
778 | return; | |
779 | ||
780 | /* Find the containing non-inline namespace. */ | |
781 | while (DECL_NAMESPACE_INLINE_P (scope)) | |
782 | scope = CP_DECL_CONTEXT (scope); | |
783 | ||
784 | adl_namespace_only (scope); | |
5e0c54e5 GDR |
785 | } |
786 | ||
f35a733d | 787 | /* Adds the class and its friends to the lookup structure. */ |
daafa301 | 788 | |
f35a733d NS |
789 | void |
790 | name_lookup::adl_class_only (tree type) | |
5e0c54e5 | 791 | { |
b655c310 NS |
792 | /* Backend-built structures, such as __builtin_va_list, aren't |
793 | affected by all this. */ | |
794 | if (!CLASS_TYPE_P (type)) | |
f35a733d NS |
795 | return; |
796 | ||
797 | type = TYPE_MAIN_VARIANT (type); | |
5e0c54e5 | 798 | |
f35a733d NS |
799 | if (see_and_mark (type)) |
800 | return; | |
801 | ||
802 | tree context = decl_namespace_context (type); | |
803 | adl_namespace (context); | |
5e0c54e5 | 804 | |
b655c310 | 805 | complete_type (type); |
daafa301 | 806 | |
f35a733d NS |
807 | /* Add friends. */ |
808 | for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; | |
b655c310 | 809 | list = TREE_CHAIN (list)) |
f35a733d NS |
810 | if (name == FRIEND_NAME (list)) |
811 | for (tree friends = FRIEND_DECLS (list); friends; | |
b655c310 NS |
812 | friends = TREE_CHAIN (friends)) |
813 | { | |
814 | tree fn = TREE_VALUE (friends); | |
5e0c54e5 | 815 | |
b655c310 NS |
816 | /* Only interested in global functions with potentially hidden |
817 | (i.e. unqualified) declarations. */ | |
818 | if (CP_DECL_CONTEXT (fn) != context) | |
819 | continue; | |
f35a733d | 820 | |
25d446fd NS |
821 | /* Only interested in anticipated friends. (Non-anticipated |
822 | ones will have been inserted during the namespace | |
823 | adl.) */ | |
824 | if (!DECL_ANTICIPATED (fn)) | |
825 | continue; | |
826 | ||
b655c310 NS |
827 | /* Template specializations are never found by name lookup. |
828 | (Templates themselves can be found, but not template | |
829 | specializations.) */ | |
830 | if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) | |
831 | continue; | |
5e0c54e5 | 832 | |
f35a733d NS |
833 | add_fns (fn); |
834 | } | |
5e0c54e5 GDR |
835 | } |
836 | ||
b655c310 NS |
837 | /* Adds the class and its bases to the lookup structure. |
838 | Returns true on error. */ | |
daafa301 | 839 | |
f35a733d NS |
840 | void |
841 | name_lookup::adl_bases (tree type) | |
5e0c54e5 | 842 | { |
f35a733d | 843 | adl_class_only (type); |
82f2836c | 844 | |
f35a733d NS |
845 | /* Process baseclasses. */ |
846 | if (tree binfo = TYPE_BINFO (type)) | |
5e0c54e5 | 847 | { |
f35a733d | 848 | tree base_binfo; |
b655c310 | 849 | int i; |
aed81407 | 850 | |
f35a733d NS |
851 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
852 | adl_bases (BINFO_TYPE (base_binfo)); | |
b655c310 | 853 | } |
89b578be MM |
854 | } |
855 | ||
b655c310 NS |
856 | /* Adds everything associated with a class argument type to the lookup |
857 | structure. Returns true on error. | |
daafa301 | 858 | |
b655c310 NS |
859 | If T is a class type (including unions), its associated classes are: the |
860 | class itself; the class of which it is a member, if any; and its direct | |
861 | and indirect base classes. Its associated namespaces are the namespaces | |
862 | of which its associated classes are members. Furthermore, if T is a | |
863 | class template specialization, its associated namespaces and classes | |
864 | also include: the namespaces and classes associated with the types of | |
865 | the template arguments provided for template type parameters (excluding | |
866 | template template parameters); the namespaces of which any template | |
867 | template arguments are members; and the classes of which any member | |
868 | templates used as template template arguments are members. [ Note: | |
869 | non-type template arguments do not contribute to the set of associated | |
870 | namespaces. --end note] */ | |
871 | ||
f35a733d NS |
872 | void |
873 | name_lookup::adl_class (tree type) | |
aed81407 | 874 | { |
b655c310 NS |
875 | /* Backend build structures, such as __builtin_va_list, aren't |
876 | affected by all this. */ | |
877 | if (!CLASS_TYPE_P (type)) | |
f35a733d | 878 | return; |
aed81407 | 879 | |
f35a733d NS |
880 | type = TYPE_MAIN_VARIANT (type); |
881 | /* We don't set found here because we have to have set seen first, | |
882 | which is done in the adl_bases walk. */ | |
883 | if (found_p (type)) | |
884 | return; | |
aed81407 | 885 | |
f35a733d NS |
886 | adl_bases (type); |
887 | mark_found (type); | |
daafa301 | 888 | |
f35a733d NS |
889 | if (TYPE_CLASS_SCOPE_P (type)) |
890 | adl_class_only (TYPE_CONTEXT (type)); | |
c87ceb13 | 891 | |
b655c310 NS |
892 | /* Process template arguments. */ |
893 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
894 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) | |
895 | { | |
f35a733d NS |
896 | tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
897 | for (int i = 0; i < TREE_VEC_LENGTH (list); ++i) | |
898 | adl_template_arg (TREE_VEC_ELT (list, i)); | |
b655c310 | 899 | } |
90ea9897 MM |
900 | } |
901 | ||
f35a733d NS |
902 | void |
903 | name_lookup::adl_expr (tree expr) | |
904 | { | |
905 | if (!expr) | |
906 | return; | |
90ea9897 | 907 | |
f35a733d NS |
908 | gcc_assert (!TYPE_P (expr)); |
909 | ||
910 | if (TREE_TYPE (expr) != unknown_type_node) | |
911 | { | |
912 | adl_type (TREE_TYPE (expr)); | |
913 | return; | |
914 | } | |
915 | ||
916 | if (TREE_CODE (expr) == ADDR_EXPR) | |
917 | expr = TREE_OPERAND (expr, 0); | |
918 | if (TREE_CODE (expr) == COMPONENT_REF | |
919 | || TREE_CODE (expr) == OFFSET_REF) | |
920 | expr = TREE_OPERAND (expr, 1); | |
921 | expr = MAYBE_BASELINK_FUNCTIONS (expr); | |
922 | ||
923 | if (OVL_P (expr)) | |
924 | for (lkp_iterator iter (expr); iter; ++iter) | |
925 | adl_type (TREE_TYPE (*iter)); | |
926 | else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) | |
927 | { | |
928 | /* The working paper doesn't currently say how to handle | |
929 | template-id arguments. The sensible thing would seem to be | |
930 | to handle the list of template candidates like a normal | |
931 | overload set, and handle the template arguments like we do | |
932 | for class template specializations. */ | |
933 | ||
934 | /* First the templates. */ | |
935 | adl_expr (TREE_OPERAND (expr, 0)); | |
936 | ||
937 | /* Now the arguments. */ | |
938 | if (tree args = TREE_OPERAND (expr, 1)) | |
939 | for (int ix = TREE_VEC_LENGTH (args); ix--;) | |
940 | adl_template_arg (TREE_VEC_ELT (args, ix)); | |
941 | } | |
942 | } | |
943 | ||
944 | void | |
945 | name_lookup::adl_type (tree type) | |
90ea9897 | 946 | { |
b655c310 | 947 | if (!type) |
f35a733d | 948 | return; |
7de5bccc | 949 | |
b655c310 | 950 | if (TYPE_PTRDATAMEM_P (type)) |
90ea9897 | 951 | { |
b655c310 | 952 | /* Pointer to member: associate class type and value type. */ |
f35a733d NS |
953 | adl_type (TYPE_PTRMEM_CLASS_TYPE (type)); |
954 | adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type)); | |
955 | return; | |
89b578be | 956 | } |
f35a733d NS |
957 | |
958 | switch (TREE_CODE (type)) | |
b655c310 | 959 | { |
b655c310 NS |
960 | case RECORD_TYPE: |
961 | if (TYPE_PTRMEMFUNC_P (type)) | |
f35a733d NS |
962 | { |
963 | adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
964 | return; | |
965 | } | |
b655c310 NS |
966 | /* FALLTHRU */ |
967 | case UNION_TYPE: | |
f35a733d NS |
968 | adl_class (type); |
969 | return; | |
970 | ||
b655c310 NS |
971 | case METHOD_TYPE: |
972 | /* The basetype is referenced in the first arg type, so just | |
973 | fall through. */ | |
974 | case FUNCTION_TYPE: | |
975 | /* Associate the parameter types. */ | |
f35a733d NS |
976 | for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) |
977 | adl_type (TREE_VALUE (args)); | |
978 | /* FALLTHROUGH */ | |
979 | ||
980 | case POINTER_TYPE: | |
981 | case REFERENCE_TYPE: | |
982 | case ARRAY_TYPE: | |
983 | adl_type (TREE_TYPE (type)); | |
984 | return; | |
985 | ||
986 | case ENUMERAL_TYPE: | |
987 | if (TYPE_CLASS_SCOPE_P (type)) | |
988 | adl_class_only (TYPE_CONTEXT (type)); | |
989 | adl_namespace (decl_namespace_context (type)); | |
990 | return; | |
991 | ||
b655c310 NS |
992 | case LANG_TYPE: |
993 | gcc_assert (type == unknown_type_node | |
994 | || type == init_list_type_node); | |
f35a733d NS |
995 | return; |
996 | ||
b655c310 | 997 | case TYPE_PACK_EXPANSION: |
f35a733d NS |
998 | adl_type (PACK_EXPANSION_PATTERN (type)); |
999 | return; | |
c8094d83 | 1000 | |
b655c310 | 1001 | default: |
f35a733d | 1002 | break; |
b655c310 | 1003 | } |
00e8de68 GDR |
1004 | } |
1005 | ||
f35a733d NS |
1006 | /* Adds everything associated with a template argument to the lookup |
1007 | structure. */ | |
00e8de68 | 1008 | |
f35a733d NS |
1009 | void |
1010 | name_lookup::adl_template_arg (tree arg) | |
00e8de68 | 1011 | { |
f35a733d | 1012 | /* [basic.lookup.koenig] |
00e8de68 | 1013 | |
f35a733d NS |
1014 | If T is a template-id, its associated namespaces and classes are |
1015 | ... the namespaces and classes associated with the types of the | |
1016 | template arguments provided for template type parameters | |
1017 | (excluding template template parameters); the namespaces in which | |
1018 | any template template arguments are defined; and the classes in | |
1019 | which any member templates used as template template arguments | |
1020 | are defined. [Note: non-type template arguments do not | |
1021 | contribute to the set of associated namespaces. ] */ | |
00e8de68 | 1022 | |
f35a733d NS |
1023 | /* Consider first template template arguments. */ |
1024 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
1025 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) | |
1026 | ; | |
1027 | else if (TREE_CODE (arg) == TEMPLATE_DECL) | |
00e8de68 | 1028 | { |
f35a733d | 1029 | tree ctx = CP_DECL_CONTEXT (arg); |
b655c310 | 1030 | |
f35a733d NS |
1031 | /* It's not a member template. */ |
1032 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
1033 | adl_namespace (ctx); | |
1034 | /* Otherwise, it must be member template. */ | |
1035 | else | |
1036 | adl_class_only (ctx); | |
00e8de68 | 1037 | } |
f35a733d NS |
1038 | /* It's an argument pack; handle it recursively. */ |
1039 | else if (ARGUMENT_PACK_P (arg)) | |
b655c310 | 1040 | { |
f35a733d NS |
1041 | tree args = ARGUMENT_PACK_ARGS (arg); |
1042 | int i, len = TREE_VEC_LENGTH (args); | |
1043 | for (i = 0; i < len; ++i) | |
1044 | adl_template_arg (TREE_VEC_ELT (args, i)); | |
b655c310 | 1045 | } |
f35a733d NS |
1046 | /* It's not a template template argument, but it is a type template |
1047 | argument. */ | |
1048 | else if (TYPE_P (arg)) | |
1049 | adl_type (arg); | |
00e8de68 GDR |
1050 | } |
1051 | ||
f35a733d NS |
1052 | /* Perform ADL lookup. FNS is the existing lookup result and ARGS are |
1053 | the call arguments. */ | |
9485d254 | 1054 | |
f35a733d NS |
1055 | tree |
1056 | name_lookup::search_adl (tree fns, vec<tree, va_gc> *args) | |
9485d254 | 1057 | { |
3d7ff728 NS |
1058 | if (fns) |
1059 | { | |
1060 | deduping = true; | |
1061 | lookup_mark (fns, true); | |
1062 | } | |
f35a733d | 1063 | value = fns; |
7f82286e | 1064 | |
f35a733d NS |
1065 | unsigned ix; |
1066 | tree arg; | |
1067 | ||
1068 | FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg) | |
b67b23f0 NS |
1069 | /* OMP reduction operators put an ADL-significant type as the |
1070 | first arg. */ | |
1071 | if (TYPE_P (arg)) | |
1072 | adl_type (arg); | |
1073 | else | |
f35a733d NS |
1074 | adl_expr (arg); |
1075 | ||
f35a733d | 1076 | fns = value; |
c87ceb13 | 1077 | |
b655c310 NS |
1078 | return fns; |
1079 | } | |
c87ceb13 | 1080 | |
9dda0ace NS |
1081 | static bool qualified_namespace_lookup (tree, name_lookup *); |
1082 | static void consider_binding_level (tree name, | |
1083 | best_match <tree, const char *> &bm, | |
1084 | cp_binding_level *lvl, | |
1085 | bool look_within_fields, | |
1086 | enum lookup_name_fuzzy_kind kind); | |
9dda0ace NS |
1087 | static void diagnose_name_conflict (tree, tree); |
1088 | ||
f35a733d NS |
1089 | /* ADL lookup of NAME. FNS is the result of regular lookup, and we |
1090 | don't add duplicates to it. ARGS is the vector of call | |
1091 | arguments (which will not be empty). */ | |
c87ceb13 | 1092 | |
f35a733d | 1093 | tree |
b655c310 | 1094 | lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args) |
c87ceb13 | 1095 | { |
f35a733d NS |
1096 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
1097 | name_lookup lookup (name); | |
1098 | fns = lookup.search_adl (fns, args); | |
b655c310 | 1099 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
f35a733d NS |
1100 | return fns; |
1101 | } | |
1102 | ||
bff8b385 NS |
1103 | /* FNS is an overload set of conversion functions. Return the |
1104 | overloads converting to TYPE. */ | |
9e931c2a NS |
1105 | |
1106 | static tree | |
bff8b385 | 1107 | extract_conversion_operator (tree fns, tree type) |
9e931c2a | 1108 | { |
2e12a855 | 1109 | tree convs = NULL_TREE; |
bff8b385 | 1110 | tree tpls = NULL_TREE; |
9e931c2a | 1111 | |
bff8b385 | 1112 | for (ovl_iterator iter (fns); iter; ++iter) |
9e931c2a | 1113 | { |
bff8b385 NS |
1114 | if (same_type_p (DECL_CONV_FN_TYPE (*iter), type)) |
1115 | convs = lookup_add (*iter, convs); | |
2e12a855 | 1116 | |
bff8b385 NS |
1117 | if (TREE_CODE (*iter) == TEMPLATE_DECL) |
1118 | tpls = lookup_add (*iter, tpls); | |
9e931c2a NS |
1119 | } |
1120 | ||
bff8b385 NS |
1121 | if (!convs) |
1122 | convs = tpls; | |
1123 | ||
2e12a855 | 1124 | return convs; |
9e931c2a NS |
1125 | } |
1126 | ||
783dc739 | 1127 | /* Binary search of (ordered) MEMBER_VEC for NAME. */ |
9e931c2a | 1128 | |
b991151b | 1129 | static tree |
783dc739 | 1130 | member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name) |
9e931c2a | 1131 | { |
783dc739 | 1132 | for (unsigned lo = 0, hi = member_vec->length (); lo < hi;) |
9e931c2a | 1133 | { |
b991151b | 1134 | unsigned mid = (lo + hi) / 2; |
783dc739 | 1135 | tree binding = (*member_vec)[mid]; |
b991151b NS |
1136 | tree binding_name = OVL_NAME (binding); |
1137 | ||
1138 | if (binding_name > name) | |
1139 | hi = mid; | |
1140 | else if (binding_name < name) | |
1141 | lo = mid + 1; | |
1142 | else | |
1143 | return binding; | |
9e931c2a | 1144 | } |
9e931c2a | 1145 | |
b991151b NS |
1146 | return NULL_TREE; |
1147 | } | |
bff8b385 | 1148 | |
783dc739 | 1149 | /* Linear search of (unordered) MEMBER_VEC for NAME. */ |
b991151b NS |
1150 | |
1151 | static tree | |
783dc739 | 1152 | member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name) |
b991151b | 1153 | { |
783dc739 | 1154 | for (int ix = member_vec->length (); ix--;) |
783dc739 | 1155 | if (tree binding = (*member_vec)[ix]) |
10b5c982 NS |
1156 | if (OVL_NAME (binding) == name) |
1157 | return binding; | |
b991151b NS |
1158 | |
1159 | return NULL_TREE; | |
9e931c2a NS |
1160 | } |
1161 | ||
45e3a33d | 1162 | /* Linear search of (partially ordered) fields of KLASS for NAME. */ |
9e931c2a | 1163 | |
b991151b | 1164 | static tree |
45e3a33d | 1165 | fields_linear_search (tree klass, tree name, bool want_type) |
9e931c2a | 1166 | { |
45e3a33d | 1167 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) |
9e931c2a | 1168 | { |
45e3a33d | 1169 | tree decl = fields; |
9e931c2a | 1170 | |
35129fd3 | 1171 | if (TREE_CODE (decl) == FIELD_DECL |
45e3a33d | 1172 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) |
9e931c2a | 1173 | { |
35129fd3 | 1174 | if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type)) |
d68ddd2b | 1175 | return temp; |
9e931c2a | 1176 | } |
9e931c2a | 1177 | |
45e3a33d | 1178 | if (DECL_NAME (decl) != name) |
9e931c2a | 1179 | continue; |
45e3a33d NS |
1180 | |
1181 | if (TREE_CODE (decl) == USING_DECL) | |
9e931c2a NS |
1182 | { |
1183 | decl = strip_using_decl (decl); | |
1184 | if (is_overloaded_fn (decl)) | |
1185 | continue; | |
1186 | } | |
1187 | ||
45e3a33d NS |
1188 | if (DECL_DECLARES_FUNCTION_P (decl)) |
1189 | /* Functions are found separately. */ | |
1190 | continue; | |
1191 | ||
1192 | if (!want_type || DECL_DECLARES_TYPE_P (decl)) | |
9e931c2a NS |
1193 | return decl; |
1194 | } | |
1195 | ||
9e931c2a NS |
1196 | return NULL_TREE; |
1197 | } | |
1198 | ||
35129fd3 NS |
1199 | /* Look for NAME member inside of anonymous aggregate ANON. Although |
1200 | such things should only contain FIELD_DECLs, we check that too | |
1201 | late, and would give very confusing errors if we weren't | |
1202 | permissive here. */ | |
d68ddd2b JJ |
1203 | |
1204 | tree | |
35129fd3 | 1205 | search_anon_aggr (tree anon, tree name, bool want_type) |
d68ddd2b JJ |
1206 | { |
1207 | gcc_assert (COMPLETE_TYPE_P (anon)); | |
35129fd3 NS |
1208 | tree ret = get_class_binding_direct (anon, name, want_type); |
1209 | return ret; | |
d68ddd2b JJ |
1210 | } |
1211 | ||
b991151b NS |
1212 | /* Look for NAME as an immediate member of KLASS (including |
1213 | anon-members or unscoped enum member). TYPE_OR_FNS is zero for | |
1214 | regular search. >0 to get a type binding (if there is one) and <0 | |
1215 | if you want (just) the member function binding. | |
1216 | ||
1217 | Use this if you do not want lazy member creation. */ | |
1218 | ||
1219 | tree | |
9a54a0d9 | 1220 | get_class_binding_direct (tree klass, tree name, bool want_type) |
b991151b NS |
1221 | { |
1222 | gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass)); | |
1223 | ||
1224 | /* Conversion operators can only be found by the marker conversion | |
1225 | operator name. */ | |
1226 | bool conv_op = IDENTIFIER_CONV_OP_P (name); | |
1227 | tree lookup = conv_op ? conv_op_identifier : name; | |
1228 | tree val = NULL_TREE; | |
783dc739 | 1229 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
b991151b | 1230 | |
783dc739 | 1231 | if (COMPLETE_TYPE_P (klass) && member_vec) |
b991151b | 1232 | { |
783dc739 | 1233 | val = member_vec_binary_search (member_vec, lookup); |
45e3a33d NS |
1234 | if (!val) |
1235 | ; | |
45e3a33d | 1236 | else if (STAT_HACK_P (val)) |
9a54a0d9 NS |
1237 | val = want_type ? STAT_TYPE (val) : STAT_DECL (val); |
1238 | else if (want_type && !DECL_DECLARES_TYPE_P (val)) | |
1239 | val = NULL_TREE; | |
b991151b | 1240 | } |
45e3a33d NS |
1241 | else |
1242 | { | |
9a54a0d9 | 1243 | if (member_vec && !want_type) |
783dc739 | 1244 | val = member_vec_linear_search (member_vec, lookup); |
b991151b | 1245 | |
9a54a0d9 | 1246 | if (!val || (TREE_CODE (val) == OVERLOAD && OVL_DEDUP_P (val))) |
45e3a33d NS |
1247 | /* Dependent using declarations are a 'field', make sure we |
1248 | return that even if we saw an overload already. */ | |
9a54a0d9 NS |
1249 | if (tree field_val = fields_linear_search (klass, lookup, want_type)) |
1250 | { | |
1251 | if (!val) | |
1252 | val = field_val; | |
1253 | else if (TREE_CODE (field_val) == USING_DECL) | |
1254 | val = ovl_make (field_val, val); | |
1255 | } | |
45e3a33d | 1256 | } |
b991151b NS |
1257 | |
1258 | /* Extract the conversion operators asked for, unless the general | |
1259 | conversion operator was requested. */ | |
1260 | if (val && conv_op) | |
1261 | { | |
1262 | gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker); | |
1263 | val = OVL_CHAIN (val); | |
1264 | if (tree type = TREE_TYPE (name)) | |
1265 | val = extract_conversion_operator (val, type); | |
1266 | } | |
1267 | ||
1268 | return val; | |
1269 | } | |
1270 | ||
1271 | /* Look for NAME's binding in exactly KLASS. See | |
1272 | get_class_binding_direct for argument description. Does lazy | |
1273 | special function creation as necessary. */ | |
9e931c2a NS |
1274 | |
1275 | tree | |
9a54a0d9 | 1276 | get_class_binding (tree klass, tree name, bool want_type) |
9e931c2a | 1277 | { |
20614c86 | 1278 | klass = complete_type (klass); |
9e931c2a | 1279 | |
20614c86 | 1280 | if (COMPLETE_TYPE_P (klass)) |
9e931c2a | 1281 | { |
20614c86 | 1282 | /* Lazily declare functions, if we're going to search these. */ |
9e931c2a NS |
1283 | if (IDENTIFIER_CTOR_P (name)) |
1284 | { | |
20614c86 NS |
1285 | if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass)) |
1286 | lazily_declare_fn (sfk_constructor, klass); | |
1287 | if (CLASSTYPE_LAZY_COPY_CTOR (klass)) | |
1288 | lazily_declare_fn (sfk_copy_constructor, klass); | |
1289 | if (CLASSTYPE_LAZY_MOVE_CTOR (klass)) | |
1290 | lazily_declare_fn (sfk_move_constructor, klass); | |
9e931c2a | 1291 | } |
20614c86 | 1292 | else if (IDENTIFIER_DTOR_P (name)) |
9e931c2a | 1293 | { |
20614c86 NS |
1294 | if (CLASSTYPE_LAZY_DESTRUCTOR (klass)) |
1295 | lazily_declare_fn (sfk_destructor, klass); | |
9e931c2a | 1296 | } |
88a819be | 1297 | else if (name == assign_op_identifier) |
9e931c2a | 1298 | { |
20614c86 NS |
1299 | if (CLASSTYPE_LAZY_COPY_ASSIGN (klass)) |
1300 | lazily_declare_fn (sfk_copy_assignment, klass); | |
1301 | if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass)) | |
1302 | lazily_declare_fn (sfk_move_assignment, klass); | |
9e931c2a NS |
1303 | } |
1304 | } | |
1305 | ||
9a54a0d9 | 1306 | return get_class_binding_direct (klass, name, want_type); |
9e931c2a NS |
1307 | } |
1308 | ||
fcaf3065 | 1309 | /* Find the slot containing overloads called 'NAME'. If there is no |
10b5c982 NS |
1310 | such slot and the class is complete, create an empty one, at the |
1311 | correct point in the sorted member vector. Otherwise return NULL. | |
1312 | Deals with conv_op marker handling. */ | |
fcaf3065 NS |
1313 | |
1314 | tree * | |
10b5c982 | 1315 | find_member_slot (tree klass, tree name) |
fcaf3065 NS |
1316 | { |
1317 | bool complete_p = COMPLETE_TYPE_P (klass); | |
10b5c982 | 1318 | |
783dc739 NS |
1319 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
1320 | if (!member_vec) | |
fcaf3065 | 1321 | { |
783dc739 NS |
1322 | vec_alloc (member_vec, 8); |
1323 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; | |
45e3a33d NS |
1324 | if (complete_p) |
1325 | { | |
783dc739 | 1326 | /* If the class is complete but had no member_vec, we need |
45e3a33d NS |
1327 | to add the TYPE_FIELDS into it. We're also most likely |
1328 | to be adding ctors & dtors, so ask for 6 spare slots (the | |
1329 | abstract cdtors and their clones). */ | |
1330 | set_class_bindings (klass, 6); | |
783dc739 | 1331 | member_vec = CLASSTYPE_MEMBER_VEC (klass); |
45e3a33d | 1332 | } |
fcaf3065 NS |
1333 | } |
1334 | ||
1335 | if (IDENTIFIER_CONV_OP_P (name)) | |
1336 | name = conv_op_identifier; | |
1337 | ||
783dc739 | 1338 | unsigned ix, length = member_vec->length (); |
fcaf3065 NS |
1339 | for (ix = 0; ix < length; ix++) |
1340 | { | |
783dc739 | 1341 | tree *slot = &(*member_vec)[ix]; |
fcaf3065 NS |
1342 | tree fn_name = OVL_NAME (*slot); |
1343 | ||
1344 | if (fn_name == name) | |
1345 | { | |
45e3a33d NS |
1346 | /* If we found an existing slot, it must be a function set. |
1347 | Even with insertion after completion, because those only | |
1348 | happen with artificial fns that have unspellable names. | |
1349 | This means we do not have to deal with the stat hack | |
1350 | either. */ | |
1351 | gcc_checking_assert (OVL_P (*slot)); | |
fcaf3065 NS |
1352 | if (name == conv_op_identifier) |
1353 | { | |
1354 | gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker); | |
1355 | /* Skip the conv-op marker. */ | |
1356 | slot = &OVL_CHAIN (*slot); | |
1357 | } | |
1358 | return slot; | |
1359 | } | |
1360 | ||
1361 | if (complete_p && fn_name > name) | |
1362 | break; | |
1363 | } | |
1364 | ||
10b5c982 | 1365 | /* No slot found, add one if the class is complete. */ |
fcaf3065 NS |
1366 | if (complete_p) |
1367 | { | |
10b5c982 NS |
1368 | /* Do exact allocation, as we don't expect to add many. */ |
1369 | gcc_assert (name != conv_op_identifier); | |
783dc739 | 1370 | vec_safe_reserve_exact (member_vec, 1); |
10b5c982 | 1371 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; |
783dc739 | 1372 | member_vec->quick_insert (ix, NULL_TREE); |
10b5c982 | 1373 | return &(*member_vec)[ix]; |
fcaf3065 | 1374 | } |
10b5c982 NS |
1375 | |
1376 | return NULL; | |
1377 | } | |
1378 | ||
1379 | /* KLASS is an incomplete class to which we're adding a method NAME. | |
1380 | Add a slot and deal with conv_op marker handling. */ | |
1381 | ||
1382 | tree * | |
1383 | add_member_slot (tree klass, tree name) | |
1384 | { | |
1385 | gcc_assert (!COMPLETE_TYPE_P (klass)); | |
1386 | ||
1387 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); | |
1388 | vec_safe_push (member_vec, NULL_TREE); | |
783dc739 | 1389 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; |
fcaf3065 | 1390 | |
10b5c982 NS |
1391 | tree *slot = &member_vec->last (); |
1392 | if (IDENTIFIER_CONV_OP_P (name)) | |
fcaf3065 NS |
1393 | { |
1394 | /* Install the marker prefix. */ | |
1395 | *slot = ovl_make (conv_op_marker, NULL_TREE); | |
1396 | slot = &OVL_CHAIN (*slot); | |
1397 | } | |
1398 | ||
1399 | return slot; | |
1400 | } | |
1401 | ||
783dc739 NS |
1402 | /* Comparison function to compare two MEMBER_VEC entries by name. |
1403 | Because we can have duplicates during insertion of TYPE_FIELDS, we | |
1404 | do extra checking so deduping doesn't have to deal with so many | |
1405 | cases. */ | |
20614c86 NS |
1406 | |
1407 | static int | |
783dc739 | 1408 | member_name_cmp (const void *a_p, const void *b_p) |
20614c86 NS |
1409 | { |
1410 | tree a = *(const tree *)a_p; | |
1411 | tree b = *(const tree *)b_p; | |
1412 | tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a); | |
1413 | tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b); | |
1414 | ||
45e3a33d NS |
1415 | gcc_checking_assert (name_a && name_b); |
1416 | if (name_a != name_b) | |
1417 | return name_a < name_b ? -1 : +1; | |
1418 | ||
1419 | if (name_a == conv_op_identifier) | |
1420 | { | |
1421 | /* Strip the conv-op markers. */ | |
1422 | gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker | |
1423 | && OVL_FUNCTION (b) == conv_op_marker); | |
1424 | a = OVL_CHAIN (a); | |
1425 | b = OVL_CHAIN (b); | |
1426 | } | |
1427 | ||
1428 | if (TREE_CODE (a) == OVERLOAD) | |
1429 | a = OVL_FUNCTION (a); | |
1430 | if (TREE_CODE (b) == OVERLOAD) | |
1431 | b = OVL_FUNCTION (b); | |
1432 | ||
1433 | /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */ | |
12f71313 NS |
1434 | if (TREE_CODE (a) != TREE_CODE (b)) |
1435 | { | |
1436 | /* If one of them is a TYPE_DECL, it loses. */ | |
1437 | if (TREE_CODE (a) == TYPE_DECL) | |
1438 | return +1; | |
1439 | else if (TREE_CODE (b) == TYPE_DECL) | |
1440 | return -1; | |
1441 | ||
1442 | /* If one of them is a USING_DECL, it loses. */ | |
1443 | if (TREE_CODE (a) == USING_DECL) | |
1444 | return +1; | |
1445 | else if (TREE_CODE (b) == USING_DECL) | |
1446 | return -1; | |
1447 | ||
1448 | /* There are no other cases with different kinds of decls, as | |
1449 | duplicate detection should have kicked in earlier. However, | |
1450 | some erroneous cases get though. */ | |
1451 | gcc_assert (errorcount); | |
1452 | } | |
1453 | ||
1454 | /* Using source location would be the best thing here, but we can | |
1455 | get identically-located decls in the following circumstances: | |
1456 | ||
1457 | 1) duplicate artificial type-decls for the same type. | |
1458 | ||
1459 | 2) pack expansions of using-decls. | |
1460 | ||
1461 | We should not be doing #1, but in either case it doesn't matter | |
1462 | how we order these. Use UID as a proxy for source ordering, so | |
1463 | that identically-located decls still have a well-defined stable | |
1464 | ordering. */ | |
6c19e703 JJ |
1465 | if (DECL_UID (a) != DECL_UID (b)) |
1466 | return DECL_UID (a) < DECL_UID (b) ? -1 : +1; | |
1467 | gcc_assert (a == b); | |
1468 | return 0; | |
20614c86 NS |
1469 | } |
1470 | ||
fe920c2d NS |
1471 | static struct { |
1472 | gt_pointer_operator new_value; | |
1473 | void *cookie; | |
1474 | } resort_data; | |
1475 | ||
783dc739 | 1476 | /* This routine compares two fields like member_name_cmp but using the |
20614c86 NS |
1477 | pointer operator in resort_field_decl_data. We don't have to deal |
1478 | with duplicates here. */ | |
fe920c2d NS |
1479 | |
1480 | static int | |
783dc739 | 1481 | resort_member_name_cmp (const void *a_p, const void *b_p) |
fe920c2d | 1482 | { |
20614c86 NS |
1483 | tree a = *(const tree *)a_p; |
1484 | tree b = *(const tree *)b_p; | |
1485 | tree name_a = OVL_NAME (a); | |
1486 | tree name_b = OVL_NAME (b); | |
fe920c2d | 1487 | |
20614c86 NS |
1488 | resort_data.new_value (&name_a, resort_data.cookie); |
1489 | resort_data.new_value (&name_b, resort_data.cookie); | |
fe920c2d | 1490 | |
20614c86 | 1491 | gcc_checking_assert (name_a != name_b); |
fe920c2d | 1492 | |
20614c86 | 1493 | return name_a < name_b ? -1 : +1; |
fe920c2d NS |
1494 | } |
1495 | ||
783dc739 | 1496 | /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */ |
fe920c2d NS |
1497 | |
1498 | void | |
783dc739 | 1499 | resort_type_member_vec (void *obj, void */*orig_obj*/, |
20614c86 | 1500 | gt_pointer_operator new_value, void* cookie) |
fe920c2d | 1501 | { |
783dc739 | 1502 | if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj) |
fe920c2d NS |
1503 | { |
1504 | resort_data.new_value = new_value; | |
1505 | resort_data.cookie = cookie; | |
774ae645 | 1506 | member_vec->qsort (resort_member_name_cmp); |
fe920c2d NS |
1507 | } |
1508 | } | |
1509 | ||
1887fb46 NS |
1510 | /* Recursively count the number of fields in KLASS, including anonymous |
1511 | union members. */ | |
41970ff1 | 1512 | |
1887fb46 NS |
1513 | static unsigned |
1514 | count_class_fields (tree klass) | |
41970ff1 | 1515 | { |
1887fb46 NS |
1516 | unsigned n_fields = 0; |
1517 | ||
1518 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) | |
1519 | if (DECL_DECLARES_FUNCTION_P (fields)) | |
1520 | /* Functions are dealt with separately. */; | |
1521 | else if (TREE_CODE (fields) == FIELD_DECL | |
1522 | && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) | |
1523 | n_fields += count_class_fields (TREE_TYPE (fields)); | |
1524 | else if (DECL_NAME (fields)) | |
1525 | n_fields += 1; | |
1526 | ||
18a01e85 | 1527 | return n_fields; |
41970ff1 NS |
1528 | } |
1529 | ||
783dc739 NS |
1530 | /* Append all the nonfunction members fields of KLASS to MEMBER_VEC. |
1531 | Recurse for anonymous members. MEMBER_VEC must have space. */ | |
18a01e85 | 1532 | |
45e3a33d | 1533 | static void |
783dc739 | 1534 | member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass) |
18a01e85 | 1535 | { |
1887fb46 NS |
1536 | for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) |
1537 | if (DECL_DECLARES_FUNCTION_P (fields)) | |
1538 | /* Functions are handled separately. */; | |
1539 | else if (TREE_CODE (fields) == FIELD_DECL | |
1540 | && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) | |
783dc739 | 1541 | member_vec_append_class_fields (member_vec, TREE_TYPE (fields)); |
1887fb46 | 1542 | else if (DECL_NAME (fields)) |
45e3a33d NS |
1543 | { |
1544 | tree field = fields; | |
1545 | /* Mark a conv-op USING_DECL with the conv-op-marker. */ | |
1546 | if (TREE_CODE (field) == USING_DECL | |
1547 | && IDENTIFIER_CONV_OP_P (DECL_NAME (field))) | |
1548 | field = ovl_make (conv_op_marker, field); | |
783dc739 | 1549 | member_vec->quick_push (field); |
45e3a33d | 1550 | } |
18a01e85 NS |
1551 | } |
1552 | ||
783dc739 NS |
1553 | /* Append all of the enum values of ENUMTYPE to MEMBER_VEC. |
1554 | MEMBER_VEC must have space. */ | |
18a01e85 | 1555 | |
45e3a33d | 1556 | static void |
783dc739 | 1557 | member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype) |
18a01e85 | 1558 | { |
1887fb46 NS |
1559 | for (tree values = TYPE_VALUES (enumtype); |
1560 | values; values = TREE_CHAIN (values)) | |
783dc739 | 1561 | member_vec->quick_push (TREE_VALUE (values)); |
45e3a33d NS |
1562 | } |
1563 | ||
783dc739 | 1564 | /* MEMBER_VEC has just had new DECLs added to it, but is sorted. |
45e3a33d NS |
1565 | DeDup adjacent DECLS of the same name. We already dealt with |
1566 | conflict resolution when adding the fields or methods themselves. | |
1567 | There are three cases (which could all be combined): | |
1568 | 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate. | |
1569 | 2) a USING_DECL and an overload. If the USING_DECL is dependent, | |
1570 | it wins. Otherwise the OVERLOAD does. | |
1571 | 3) two USING_DECLS. ... | |
1572 | ||
783dc739 | 1573 | member_name_cmp will have ordered duplicates as |
45e3a33d NS |
1574 | <fns><using><type> */ |
1575 | ||
1576 | static void | |
783dc739 | 1577 | member_vec_dedup (vec<tree, va_gc> *member_vec) |
45e3a33d | 1578 | { |
783dc739 | 1579 | unsigned len = member_vec->length (); |
45e3a33d NS |
1580 | unsigned store = 0; |
1581 | ||
774ae645 JJ |
1582 | if (!len) |
1583 | return; | |
1584 | ||
303f4850 NS |
1585 | tree name = OVL_NAME ((*member_vec)[0]); |
1586 | for (unsigned jx, ix = 0; ix < len; ix = jx) | |
45e3a33d | 1587 | { |
303f4850 | 1588 | tree current = NULL_TREE; |
45e3a33d NS |
1589 | tree to_type = NULL_TREE; |
1590 | tree to_using = NULL_TREE; | |
1591 | tree marker = NULL_TREE; | |
45e3a33d | 1592 | |
303f4850 | 1593 | for (jx = ix; jx < len; jx++) |
45e3a33d | 1594 | { |
303f4850 NS |
1595 | tree next = (*member_vec)[jx]; |
1596 | if (jx != ix) | |
45e3a33d | 1597 | { |
303f4850 NS |
1598 | tree next_name = OVL_NAME (next); |
1599 | if (next_name != name) | |
1600 | { | |
1601 | name = next_name; | |
1602 | break; | |
1603 | } | |
45e3a33d | 1604 | } |
45e3a33d | 1605 | |
303f4850 | 1606 | if (IDENTIFIER_CONV_OP_P (name)) |
45e3a33d | 1607 | { |
303f4850 | 1608 | marker = next; |
45e3a33d NS |
1609 | next = OVL_CHAIN (next); |
1610 | } | |
1611 | ||
1612 | if (TREE_CODE (next) == USING_DECL) | |
1613 | { | |
303f4850 NS |
1614 | if (IDENTIFIER_CTOR_P (name)) |
1615 | /* Dependent inherited ctor. */ | |
1616 | continue; | |
1617 | ||
45e3a33d | 1618 | next = strip_using_decl (next); |
303f4850 | 1619 | if (TREE_CODE (next) == USING_DECL) |
45e3a33d NS |
1620 | { |
1621 | to_using = next; | |
303f4850 | 1622 | continue; |
45e3a33d | 1623 | } |
303f4850 NS |
1624 | |
1625 | if (is_overloaded_fn (next)) | |
1626 | continue; | |
45e3a33d NS |
1627 | } |
1628 | ||
303f4850 NS |
1629 | if (DECL_DECLARES_TYPE_P (next)) |
1630 | { | |
1631 | to_type = next; | |
1632 | continue; | |
1633 | } | |
1634 | ||
1635 | if (!current) | |
1636 | current = next; | |
45e3a33d NS |
1637 | } |
1638 | ||
1639 | if (to_using) | |
1640 | { | |
1641 | if (!current) | |
1642 | current = to_using; | |
1643 | else | |
1644 | current = ovl_make (to_using, current); | |
1645 | } | |
1646 | ||
1647 | if (to_type) | |
1648 | { | |
1649 | if (!current) | |
1650 | current = to_type; | |
1651 | else | |
1652 | current = stat_hack (current, to_type); | |
1653 | } | |
1654 | ||
303f4850 | 1655 | if (current) |
45e3a33d | 1656 | { |
303f4850 NS |
1657 | if (marker) |
1658 | { | |
1659 | OVL_CHAIN (marker) = current; | |
1660 | current = marker; | |
1661 | } | |
1662 | (*member_vec)[store++] = current; | |
45e3a33d | 1663 | } |
45e3a33d NS |
1664 | } |
1665 | ||
1666 | while (store++ < len) | |
783dc739 | 1667 | member_vec->pop (); |
18a01e85 NS |
1668 | } |
1669 | ||
783dc739 NS |
1670 | /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is |
1671 | no existing MEMBER_VEC and fewer than 8 fields, do nothing. We | |
45e3a33d NS |
1672 | know there must be at least 1 field -- the self-reference |
1673 | TYPE_DECL, except for anon aggregates, which will have at least | |
1674 | one field. */ | |
41970ff1 NS |
1675 | |
1676 | void | |
45e3a33d | 1677 | set_class_bindings (tree klass, unsigned extra) |
41970ff1 | 1678 | { |
45e3a33d | 1679 | unsigned n_fields = count_class_fields (klass); |
783dc739 | 1680 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
fe920c2d | 1681 | |
783dc739 | 1682 | if (member_vec || n_fields >= 8) |
18a01e85 | 1683 | { |
45e3a33d | 1684 | /* Append the new fields. */ |
783dc739 NS |
1685 | vec_safe_reserve_exact (member_vec, extra + n_fields); |
1686 | member_vec_append_class_fields (member_vec, klass); | |
45e3a33d NS |
1687 | } |
1688 | ||
783dc739 | 1689 | if (member_vec) |
45e3a33d | 1690 | { |
783dc739 | 1691 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; |
774ae645 | 1692 | member_vec->qsort (member_name_cmp); |
783dc739 | 1693 | member_vec_dedup (member_vec); |
18a01e85 | 1694 | } |
41970ff1 NS |
1695 | } |
1696 | ||
18a01e85 | 1697 | /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */ |
41970ff1 NS |
1698 | |
1699 | void | |
d876eb05 | 1700 | insert_late_enum_def_bindings (tree klass, tree enumtype) |
41970ff1 | 1701 | { |
45e3a33d | 1702 | int n_fields; |
783dc739 | 1703 | vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); |
1887fb46 | 1704 | |
45e3a33d | 1705 | /* The enum bindings will already be on the TYPE_FIELDS, so don't |
1887fb46 | 1706 | count them twice. */ |
783dc739 | 1707 | if (!member_vec) |
1887fb46 | 1708 | n_fields = count_class_fields (klass); |
45e3a33d NS |
1709 | else |
1710 | n_fields = list_length (TYPE_VALUES (enumtype)); | |
1887fb46 | 1711 | |
783dc739 | 1712 | if (member_vec || n_fields >= 8) |
18a01e85 | 1713 | { |
783dc739 NS |
1714 | vec_safe_reserve_exact (member_vec, n_fields); |
1715 | if (CLASSTYPE_MEMBER_VEC (klass)) | |
1716 | member_vec_append_enum_values (member_vec, enumtype); | |
1887fb46 | 1717 | else |
783dc739 NS |
1718 | member_vec_append_class_fields (member_vec, klass); |
1719 | CLASSTYPE_MEMBER_VEC (klass) = member_vec; | |
774ae645 | 1720 | member_vec->qsort (member_name_cmp); |
783dc739 | 1721 | member_vec_dedup (member_vec); |
18a01e85 | 1722 | } |
41970ff1 NS |
1723 | } |
1724 | ||
b655c310 NS |
1725 | /* Compute the chain index of a binding_entry given the HASH value of its |
1726 | name and the total COUNT of chains. COUNT is assumed to be a power | |
1727 | of 2. */ | |
9306cccb | 1728 | |
b655c310 | 1729 | #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) |
c87ceb13 | 1730 | |
b655c310 | 1731 | /* A free list of "binding_entry"s awaiting for re-use. */ |
c87ceb13 | 1732 | |
b655c310 | 1733 | static GTY((deletable)) binding_entry free_binding_entry = NULL; |
c8094d83 | 1734 | |
b655c310 | 1735 | /* The binding oracle; see cp-tree.h. */ |
c87ceb13 | 1736 | |
b655c310 | 1737 | cp_binding_oracle_function *cp_binding_oracle; |
575bfb00 | 1738 | |
b655c310 NS |
1739 | /* If we have a binding oracle, ask it for all namespace-scoped |
1740 | definitions of NAME. */ | |
557831a9 | 1741 | |
b655c310 NS |
1742 | static inline void |
1743 | query_oracle (tree name) | |
557831a9 | 1744 | { |
b655c310 NS |
1745 | if (!cp_binding_oracle) |
1746 | return; | |
557831a9 | 1747 | |
b655c310 NS |
1748 | /* LOOKED_UP holds the set of identifiers that we have already |
1749 | looked up with the oracle. */ | |
1750 | static hash_set<tree> looked_up; | |
1751 | if (looked_up.add (name)) | |
1752 | return; | |
575bfb00 | 1753 | |
b655c310 | 1754 | cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); |
c87ceb13 | 1755 | } |
00e8de68 | 1756 | |
b655c310 | 1757 | /* Create a binding_entry object for (NAME, TYPE). */ |
00e8de68 | 1758 | |
b655c310 NS |
1759 | static inline binding_entry |
1760 | binding_entry_make (tree name, tree type) | |
00e8de68 | 1761 | { |
b655c310 | 1762 | binding_entry entry; |
89bd2c03 | 1763 | |
b655c310 | 1764 | if (free_binding_entry) |
00e8de68 | 1765 | { |
b655c310 NS |
1766 | entry = free_binding_entry; |
1767 | free_binding_entry = entry->chain; | |
00e8de68 | 1768 | } |
c8094d83 | 1769 | else |
b655c310 | 1770 | entry = ggc_alloc<binding_entry_s> (); |
00e8de68 | 1771 | |
b655c310 NS |
1772 | entry->name = name; |
1773 | entry->type = type; | |
1774 | entry->chain = NULL; | |
a5e6b29b | 1775 | |
b655c310 NS |
1776 | return entry; |
1777 | } | |
a5e6b29b | 1778 | |
b655c310 NS |
1779 | /* Put ENTRY back on the free list. */ |
1780 | #if 0 | |
1781 | static inline void | |
1782 | binding_entry_free (binding_entry entry) | |
a5e6b29b | 1783 | { |
b655c310 NS |
1784 | entry->name = NULL; |
1785 | entry->type = NULL; | |
1786 | entry->chain = free_binding_entry; | |
1787 | free_binding_entry = entry; | |
1788 | } | |
1789 | #endif | |
a5e6b29b | 1790 | |
b655c310 NS |
1791 | /* The datatype used to implement the mapping from names to types at |
1792 | a given scope. */ | |
1793 | struct GTY(()) binding_table_s { | |
1794 | /* Array of chains of "binding_entry"s */ | |
1795 | binding_entry * GTY((length ("%h.chain_count"))) chain; | |
2a50edcd | 1796 | |
b655c310 NS |
1797 | /* The number of chains in this table. This is the length of the |
1798 | member "chain" considered as an array. */ | |
1799 | size_t chain_count; | |
a5e6b29b | 1800 | |
b655c310 NS |
1801 | /* Number of "binding_entry"s in this table. */ |
1802 | size_t entry_count; | |
1803 | }; | |
a5e6b29b | 1804 | |
b655c310 | 1805 | /* Construct TABLE with an initial CHAIN_COUNT. */ |
a5e6b29b | 1806 | |
b655c310 NS |
1807 | static inline void |
1808 | binding_table_construct (binding_table table, size_t chain_count) | |
1809 | { | |
1810 | table->chain_count = chain_count; | |
1811 | table->entry_count = 0; | |
1812 | table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count); | |
1813 | } | |
a5e6b29b | 1814 | |
b655c310 NS |
1815 | /* Make TABLE's entries ready for reuse. */ |
1816 | #if 0 | |
1817 | static void | |
1818 | binding_table_free (binding_table table) | |
1819 | { | |
1820 | size_t i; | |
1821 | size_t count; | |
a5e6b29b | 1822 | |
b655c310 NS |
1823 | if (table == NULL) |
1824 | return; | |
a5e6b29b | 1825 | |
b655c310 NS |
1826 | for (i = 0, count = table->chain_count; i < count; ++i) |
1827 | { | |
1828 | binding_entry temp = table->chain[i]; | |
1829 | while (temp != NULL) | |
a5e6b29b | 1830 | { |
b655c310 NS |
1831 | binding_entry entry = temp; |
1832 | temp = entry->chain; | |
1833 | binding_entry_free (entry); | |
a5e6b29b | 1834 | } |
b655c310 NS |
1835 | table->chain[i] = NULL; |
1836 | } | |
1837 | table->entry_count = 0; | |
1838 | } | |
1839 | #endif | |
a5e6b29b | 1840 | |
b655c310 | 1841 | /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ |
a5e6b29b | 1842 | |
b655c310 NS |
1843 | static inline binding_table |
1844 | binding_table_new (size_t chain_count) | |
1845 | { | |
1846 | binding_table table = ggc_alloc<binding_table_s> (); | |
1847 | table->chain = NULL; | |
1848 | binding_table_construct (table, chain_count); | |
1849 | return table; | |
1850 | } | |
a5e6b29b | 1851 | |
b655c310 | 1852 | /* Expand TABLE to twice its current chain_count. */ |
a5e6b29b | 1853 | |
b655c310 NS |
1854 | static void |
1855 | binding_table_expand (binding_table table) | |
1856 | { | |
1857 | const size_t old_chain_count = table->chain_count; | |
1858 | const size_t old_entry_count = table->entry_count; | |
1859 | const size_t new_chain_count = 2 * old_chain_count; | |
1860 | binding_entry *old_chains = table->chain; | |
1861 | size_t i; | |
1862 | ||
1863 | binding_table_construct (table, new_chain_count); | |
1864 | for (i = 0; i < old_chain_count; ++i) | |
1865 | { | |
1866 | binding_entry entry = old_chains[i]; | |
1867 | for (; entry != NULL; entry = old_chains[i]) | |
a5e6b29b | 1868 | { |
b655c310 NS |
1869 | const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); |
1870 | const size_t j = ENTRY_INDEX (hash, new_chain_count); | |
10827cd8 | 1871 | |
b655c310 NS |
1872 | old_chains[i] = entry->chain; |
1873 | entry->chain = table->chain[j]; | |
1874 | table->chain[j] = entry; | |
1875 | } | |
1876 | } | |
1877 | table->entry_count = old_entry_count; | |
1878 | } | |
10827cd8 | 1879 | |
b655c310 | 1880 | /* Insert a binding for NAME to TYPE into TABLE. */ |
10827cd8 | 1881 | |
b655c310 NS |
1882 | static void |
1883 | binding_table_insert (binding_table table, tree name, tree type) | |
1884 | { | |
1885 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
1886 | const size_t i = ENTRY_INDEX (hash, table->chain_count); | |
1887 | binding_entry entry = binding_entry_make (name, type); | |
c8094d83 | 1888 | |
b655c310 NS |
1889 | entry->chain = table->chain[i]; |
1890 | table->chain[i] = entry; | |
1891 | ++table->entry_count; | |
b1a19c7c | 1892 | |
b655c310 NS |
1893 | if (3 * table->chain_count < 5 * table->entry_count) |
1894 | binding_table_expand (table); | |
1895 | } | |
a5e6b29b | 1896 | |
b655c310 | 1897 | /* Return the binding_entry, if any, that maps NAME. */ |
c8094d83 | 1898 | |
b655c310 NS |
1899 | binding_entry |
1900 | binding_table_find (binding_table table, tree name) | |
1901 | { | |
1902 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
1903 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
c8094d83 | 1904 | |
b655c310 NS |
1905 | while (entry != NULL && entry->name != name) |
1906 | entry = entry->chain; | |
a5e6b29b | 1907 | |
b655c310 NS |
1908 | return entry; |
1909 | } | |
ecba6c56 | 1910 | |
b655c310 | 1911 | /* Apply PROC -- with DATA -- to all entries in TABLE. */ |
a5e6b29b | 1912 | |
b655c310 NS |
1913 | void |
1914 | binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) | |
1915 | { | |
1916 | size_t chain_count; | |
1917 | size_t i; | |
a5e6b29b | 1918 | |
b655c310 NS |
1919 | if (!table) |
1920 | return; | |
a5e6b29b | 1921 | |
b655c310 NS |
1922 | chain_count = table->chain_count; |
1923 | for (i = 0; i < chain_count; ++i) | |
1924 | { | |
1925 | binding_entry entry = table->chain[i]; | |
1926 | for (; entry != NULL; entry = entry->chain) | |
1927 | proc (entry, data); | |
1928 | } | |
1929 | } | |
1930 | \f | |
1931 | #ifndef ENABLE_SCOPE_CHECKING | |
1932 | # define ENABLE_SCOPE_CHECKING 0 | |
1933 | #else | |
1934 | # define ENABLE_SCOPE_CHECKING 1 | |
1935 | #endif | |
199b7a35 | 1936 | |
b655c310 | 1937 | /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ |
f7cbd40e | 1938 | |
b655c310 | 1939 | static GTY((deletable)) cxx_binding *free_bindings; |
f7cbd40e | 1940 | |
b655c310 NS |
1941 | /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS |
1942 | field to NULL. */ | |
d0940d56 | 1943 | |
b655c310 NS |
1944 | static inline void |
1945 | cxx_binding_init (cxx_binding *binding, tree value, tree type) | |
1946 | { | |
1947 | binding->value = value; | |
1948 | binding->type = type; | |
1949 | binding->previous = NULL; | |
1950 | } | |
a5e6b29b | 1951 | |
b655c310 | 1952 | /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ |
3797cb21 | 1953 | |
b655c310 NS |
1954 | static cxx_binding * |
1955 | cxx_binding_make (tree value, tree type) | |
1956 | { | |
1957 | cxx_binding *binding; | |
1958 | if (free_bindings) | |
1959 | { | |
1960 | binding = free_bindings; | |
1961 | free_bindings = binding->previous; | |
1962 | } | |
1963 | else | |
1964 | binding = ggc_alloc<cxx_binding> (); | |
a5e6b29b | 1965 | |
b655c310 | 1966 | cxx_binding_init (binding, value, type); |
a5e6b29b | 1967 | |
b655c310 NS |
1968 | return binding; |
1969 | } | |
a5e6b29b | 1970 | |
b655c310 | 1971 | /* Put BINDING back on the free list. */ |
a5e6b29b | 1972 | |
b655c310 NS |
1973 | static inline void |
1974 | cxx_binding_free (cxx_binding *binding) | |
1975 | { | |
1976 | binding->scope = NULL; | |
1977 | binding->previous = free_bindings; | |
1978 | free_bindings = binding; | |
1979 | } | |
a5e6b29b | 1980 | |
b655c310 NS |
1981 | /* Create a new binding for NAME (with the indicated VALUE and TYPE |
1982 | bindings) in the class scope indicated by SCOPE. */ | |
a5e6b29b | 1983 | |
b655c310 NS |
1984 | static cxx_binding * |
1985 | new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) | |
1986 | { | |
1987 | cp_class_binding cb = {cxx_binding_make (value, type), name}; | |
1988 | cxx_binding *binding = cb.base; | |
1989 | vec_safe_push (scope->class_shadowed, cb); | |
1990 | binding->scope = scope; | |
1991 | return binding; | |
1992 | } | |
a5e6b29b | 1993 | |
b655c310 NS |
1994 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
1995 | level at which this declaration is being bound. */ | |
a5e6b29b | 1996 | |
b655c310 NS |
1997 | void |
1998 | push_binding (tree id, tree decl, cp_binding_level* level) | |
1999 | { | |
2000 | cxx_binding *binding; | |
a5e6b29b | 2001 | |
b655c310 NS |
2002 | if (level != class_binding_level) |
2003 | { | |
2004 | binding = cxx_binding_make (decl, NULL_TREE); | |
2005 | binding->scope = level; | |
2006 | } | |
2007 | else | |
2008 | binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); | |
a5e6b29b | 2009 | |
b655c310 NS |
2010 | /* Now, fill in the binding information. */ |
2011 | binding->previous = IDENTIFIER_BINDING (id); | |
2012 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
2013 | LOCAL_BINDING_P (binding) = (level != class_binding_level); | |
a5e6b29b | 2014 | |
b655c310 NS |
2015 | /* And put it on the front of the list of bindings for ID. */ |
2016 | IDENTIFIER_BINDING (id) = binding; | |
575bfb00 LC |
2017 | } |
2018 | ||
b655c310 NS |
2019 | /* Remove the binding for DECL which should be the innermost binding |
2020 | for ID. */ | |
575bfb00 | 2021 | |
b655c310 | 2022 | void |
9c82d7b6 | 2023 | pop_local_binding (tree id, tree decl) |
575bfb00 | 2024 | { |
b655c310 | 2025 | cxx_binding *binding; |
d63d5d0c | 2026 | |
b655c310 NS |
2027 | if (id == NULL_TREE) |
2028 | /* It's easiest to write the loops that call this function without | |
2029 | checking whether or not the entities involved have names. We | |
2030 | get here for such an entity. */ | |
2031 | return; | |
d63d5d0c | 2032 | |
b655c310 NS |
2033 | /* Get the innermost binding for ID. */ |
2034 | binding = IDENTIFIER_BINDING (id); | |
a5e6b29b | 2035 | |
b655c310 NS |
2036 | /* The name should be bound. */ |
2037 | gcc_assert (binding != NULL); | |
a5e6b29b | 2038 | |
b655c310 NS |
2039 | /* The DECL will be either the ordinary binding or the type |
2040 | binding for this identifier. Remove that binding. */ | |
2041 | if (binding->value == decl) | |
2042 | binding->value = NULL_TREE; | |
a5e6b29b | 2043 | else |
b655c310 NS |
2044 | { |
2045 | gcc_assert (binding->type == decl); | |
2046 | binding->type = NULL_TREE; | |
2047 | } | |
00e8de68 | 2048 | |
b655c310 | 2049 | if (!binding->value && !binding->type) |
00e8de68 | 2050 | { |
b655c310 NS |
2051 | /* We're completely done with the innermost binding for this |
2052 | identifier. Unhook it from the list of bindings. */ | |
2053 | IDENTIFIER_BINDING (id) = binding->previous; | |
2054 | ||
2055 | /* Add it to the free list. */ | |
2056 | cxx_binding_free (binding); | |
00e8de68 | 2057 | } |
b655c310 | 2058 | } |
00e8de68 | 2059 | |
b655c310 NS |
2060 | /* Remove the bindings for the decls of the current level and leave |
2061 | the current scope. */ | |
00e8de68 | 2062 | |
b655c310 NS |
2063 | void |
2064 | pop_bindings_and_leave_scope (void) | |
2065 | { | |
9c82d7b6 | 2066 | for (tree t = get_local_decls (); t; t = DECL_CHAIN (t)) |
9d029ddf NS |
2067 | { |
2068 | tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t; | |
2069 | tree name = OVL_NAME (decl); | |
2070 | ||
2071 | pop_local_binding (name, decl); | |
2072 | } | |
2073 | ||
b655c310 | 2074 | leave_scope (); |
00e8de68 | 2075 | } |
a5e6b29b | 2076 | |
b655c310 NS |
2077 | /* Strip non dependent using declarations. If DECL is dependent, |
2078 | surreptitiously create a typename_type and return it. */ | |
a5e6b29b GDR |
2079 | |
2080 | tree | |
b655c310 | 2081 | strip_using_decl (tree decl) |
a5e6b29b | 2082 | { |
b655c310 NS |
2083 | if (decl == NULL_TREE) |
2084 | return NULL_TREE; | |
a5e6b29b | 2085 | |
b655c310 NS |
2086 | while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) |
2087 | decl = USING_DECL_DECLS (decl); | |
a5e6b29b | 2088 | |
b655c310 NS |
2089 | if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) |
2090 | && USING_DECL_TYPENAME_P (decl)) | |
a5e6b29b | 2091 | { |
b655c310 NS |
2092 | /* We have found a type introduced by a using |
2093 | declaration at class scope that refers to a dependent | |
2094 | type. | |
2095 | ||
2096 | using typename :: [opt] nested-name-specifier unqualified-id ; | |
2097 | */ | |
10839133 | 2098 | decl = make_typename_type (USING_DECL_SCOPE (decl), |
b655c310 NS |
2099 | DECL_NAME (decl), |
2100 | typename_type, tf_error); | |
2101 | if (decl != error_mark_node) | |
2102 | decl = TYPE_NAME (decl); | |
a5e6b29b GDR |
2103 | } |
2104 | ||
b655c310 NS |
2105 | return decl; |
2106 | } | |
a5e6b29b | 2107 | |
ef4c5e78 NS |
2108 | /* Return true if OVL is an overload for an anticipated builtin. */ |
2109 | ||
2110 | static bool | |
2111 | anticipated_builtin_p (tree ovl) | |
2112 | { | |
2113 | if (TREE_CODE (ovl) != OVERLOAD) | |
2114 | return false; | |
2115 | ||
2116 | if (!OVL_HIDDEN_P (ovl)) | |
2117 | return false; | |
2118 | ||
2119 | tree fn = OVL_FUNCTION (ovl); | |
2120 | gcc_checking_assert (DECL_ANTICIPATED (fn)); | |
2121 | ||
2122 | if (DECL_HIDDEN_FRIEND_P (fn)) | |
2123 | return false; | |
2124 | ||
2125 | return true; | |
2126 | } | |
2127 | ||
b655c310 NS |
2128 | /* BINDING records an existing declaration for a name in the current scope. |
2129 | But, DECL is another declaration for that same identifier in the | |
2130 | same scope. This is the `struct stat' hack whereby a non-typedef | |
2131 | class name or enum-name can be bound at the same level as some other | |
2132 | kind of entity. | |
2133 | 3.3.7/1 | |
ae5cbc33 | 2134 | |
b655c310 NS |
2135 | A class name (9.1) or enumeration name (7.2) can be hidden by the |
2136 | name of an object, function, or enumerator declared in the same scope. | |
2137 | If a class or enumeration name and an object, function, or enumerator | |
2138 | are declared in the same scope (in any order) with the same name, the | |
2139 | class or enumeration name is hidden wherever the object, function, or | |
2140 | enumerator name is visible. | |
ae5cbc33 | 2141 | |
b655c310 NS |
2142 | It's the responsibility of the caller to check that |
2143 | inserting this name is valid here. Returns nonzero if the new binding | |
2144 | was successful. */ | |
2145 | ||
2146 | static bool | |
2147 | supplement_binding_1 (cxx_binding *binding, tree decl) | |
2148 | { | |
2149 | tree bval = binding->value; | |
2150 | bool ok = true; | |
2151 | tree target_bval = strip_using_decl (bval); | |
2152 | tree target_decl = strip_using_decl (decl); | |
2153 | ||
2154 | if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) | |
2155 | && target_decl != target_bval | |
2156 | && (TREE_CODE (target_bval) != TYPE_DECL | |
2157 | /* We allow pushing an enum multiple times in a class | |
2158 | template in order to handle late matching of underlying | |
2159 | type on an opaque-enum-declaration followed by an | |
2160 | enum-specifier. */ | |
2161 | || (processing_template_decl | |
2162 | && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE | |
2163 | && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE | |
2164 | && (dependent_type_p (ENUM_UNDERLYING_TYPE | |
2165 | (TREE_TYPE (target_decl))) | |
2166 | || dependent_type_p (ENUM_UNDERLYING_TYPE | |
2167 | (TREE_TYPE (target_bval))))))) | |
2168 | /* The new name is the type name. */ | |
2169 | binding->type = decl; | |
2170 | else if (/* TARGET_BVAL is null when push_class_level_binding moves | |
2171 | an inherited type-binding out of the way to make room | |
2172 | for a new value binding. */ | |
2173 | !target_bval | |
2174 | /* TARGET_BVAL is error_mark_node when TARGET_DECL's name | |
2175 | has been used in a non-class scope prior declaration. | |
2176 | In that case, we should have already issued a | |
2177 | diagnostic; for graceful error recovery purpose, pretend | |
2178 | this was the intended declaration for that name. */ | |
2179 | || target_bval == error_mark_node | |
2180 | /* If TARGET_BVAL is anticipated but has not yet been | |
2181 | declared, pretend it is not there at all. */ | |
ef4c5e78 | 2182 | || anticipated_builtin_p (target_bval)) |
b655c310 NS |
2183 | binding->value = decl; |
2184 | else if (TREE_CODE (target_bval) == TYPE_DECL | |
2185 | && DECL_ARTIFICIAL (target_bval) | |
2186 | && target_decl != target_bval | |
2187 | && (TREE_CODE (target_decl) != TYPE_DECL | |
2188 | || same_type_p (TREE_TYPE (target_decl), | |
2189 | TREE_TYPE (target_bval)))) | |
a5e6b29b | 2190 | { |
b655c310 NS |
2191 | /* The old binding was a type name. It was placed in |
2192 | VALUE field because it was thought, at the point it was | |
2193 | declared, to be the only entity with such a name. Move the | |
2194 | type name into the type slot; it is now hidden by the new | |
2195 | binding. */ | |
2196 | binding->type = bval; | |
2197 | binding->value = decl; | |
2198 | binding->value_is_inherited = false; | |
a5e6b29b | 2199 | } |
b655c310 NS |
2200 | else if (TREE_CODE (target_bval) == TYPE_DECL |
2201 | && TREE_CODE (target_decl) == TYPE_DECL | |
2202 | && DECL_NAME (target_decl) == DECL_NAME (target_bval) | |
2203 | && binding->scope->kind != sk_class | |
2204 | && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) | |
2205 | /* If either type involves template parameters, we must | |
2206 | wait until instantiation. */ | |
2207 | || uses_template_parms (TREE_TYPE (target_decl)) | |
2208 | || uses_template_parms (TREE_TYPE (target_bval)))) | |
2209 | /* We have two typedef-names, both naming the same type to have | |
2210 | the same name. In general, this is OK because of: | |
a5e6b29b | 2211 | |
b655c310 | 2212 | [dcl.typedef] |
00e8de68 | 2213 | |
b655c310 NS |
2214 | In a given scope, a typedef specifier can be used to redefine |
2215 | the name of any type declared in that scope to refer to the | |
2216 | type to which it already refers. | |
00e8de68 | 2217 | |
b655c310 NS |
2218 | However, in class scopes, this rule does not apply due to the |
2219 | stricter language in [class.mem] prohibiting redeclarations of | |
2220 | members. */ | |
2221 | ok = false; | |
2222 | /* There can be two block-scope declarations of the same variable, | |
2223 | so long as they are `extern' declarations. However, there cannot | |
2224 | be two declarations of the same static data member: | |
00e8de68 | 2225 | |
b655c310 | 2226 | [class.mem] |
00e8de68 | 2227 | |
b655c310 NS |
2228 | A member shall not be declared twice in the |
2229 | member-specification. */ | |
2230 | else if (VAR_P (target_decl) | |
2231 | && VAR_P (target_bval) | |
2232 | && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) | |
2233 | && !DECL_CLASS_SCOPE_P (target_decl)) | |
2234 | { | |
2235 | duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); | |
2236 | ok = false; | |
2237 | } | |
2238 | else if (TREE_CODE (decl) == NAMESPACE_DECL | |
2239 | && TREE_CODE (bval) == NAMESPACE_DECL | |
2240 | && DECL_NAMESPACE_ALIAS (decl) | |
2241 | && DECL_NAMESPACE_ALIAS (bval) | |
2242 | && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) | |
2243 | /* [namespace.alias] | |
00e8de68 | 2244 | |
b655c310 NS |
2245 | In a declarative region, a namespace-alias-definition can be |
2246 | used to redefine a namespace-alias declared in that declarative | |
2247 | region to refer only to the namespace to which it already | |
2248 | refers. */ | |
2249 | ok = false; | |
b655c310 NS |
2250 | else |
2251 | { | |
2252 | if (!error_operand_p (bval)) | |
2253 | diagnose_name_conflict (decl, bval); | |
2254 | ok = false; | |
2255 | } | |
00e8de68 | 2256 | |
b655c310 | 2257 | return ok; |
00e8de68 GDR |
2258 | } |
2259 | ||
b655c310 NS |
2260 | /* Diagnose a name conflict between DECL and BVAL. */ |
2261 | ||
00e8de68 | 2262 | static void |
b655c310 NS |
2263 | diagnose_name_conflict (tree decl, tree bval) |
2264 | { | |
2265 | if (TREE_CODE (decl) == TREE_CODE (bval) | |
71bbbd13 | 2266 | && TREE_CODE (decl) != NAMESPACE_DECL |
9d029ddf | 2267 | && !DECL_DECLARES_FUNCTION_P (decl) |
71bbbd13 NS |
2268 | && (TREE_CODE (decl) != TYPE_DECL |
2269 | || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval)) | |
9d029ddf | 2270 | && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval)) |
b655c310 | 2271 | error ("redeclaration of %q#D", decl); |
00e8de68 | 2272 | else |
b655c310 NS |
2273 | error ("%q#D conflicts with a previous declaration", decl); |
2274 | ||
2275 | inform (location_of (bval), "previous declaration %q#D", bval); | |
00e8de68 GDR |
2276 | } |
2277 | ||
b655c310 | 2278 | /* Wrapper for supplement_binding_1. */ |
00e8de68 | 2279 | |
b655c310 NS |
2280 | static bool |
2281 | supplement_binding (cxx_binding *binding, tree decl) | |
00e8de68 | 2282 | { |
b655c310 NS |
2283 | bool ret; |
2284 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
2285 | ret = supplement_binding_1 (binding, decl); | |
2286 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
2287 | return ret; | |
00e8de68 GDR |
2288 | } |
2289 | ||
9d029ddf NS |
2290 | /* Replace BINDING's current value on its scope's name list with |
2291 | NEWVAL. */ | |
2292 | ||
2293 | static void | |
2294 | update_local_overload (cxx_binding *binding, tree newval) | |
2295 | { | |
2296 | tree *d; | |
2297 | ||
2298 | for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d)) | |
2299 | if (*d == binding->value) | |
2300 | { | |
2301 | /* Stitch new list node in. */ | |
2302 | *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d)); | |
2303 | break; | |
2304 | } | |
2305 | else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value) | |
2306 | break; | |
2307 | ||
2308 | TREE_VALUE (*d) = newval; | |
2309 | } | |
2310 | ||
2311 | /* Compares the parameter-type-lists of ONE and TWO and | |
2312 | returns false if they are different. If the DECLs are template | |
2313 | functions, the return types and the template parameter lists are | |
2314 | compared too (DR 565). */ | |
2315 | ||
2316 | static bool | |
2317 | matching_fn_p (tree one, tree two) | |
2318 | { | |
2319 | if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)), | |
2320 | TYPE_ARG_TYPES (TREE_TYPE (two)))) | |
2321 | return false; | |
2322 | ||
2323 | if (TREE_CODE (one) == TEMPLATE_DECL | |
2324 | && TREE_CODE (two) == TEMPLATE_DECL) | |
2325 | { | |
2326 | /* Compare template parms. */ | |
2327 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (one), | |
2328 | DECL_TEMPLATE_PARMS (two))) | |
2329 | return false; | |
2330 | ||
2331 | /* And return type. */ | |
2332 | if (!same_type_p (TREE_TYPE (TREE_TYPE (one)), | |
2333 | TREE_TYPE (TREE_TYPE (two)))) | |
2334 | return false; | |
2335 | } | |
2336 | ||
2337 | return true; | |
2338 | } | |
2339 | ||
3c9cca88 | 2340 | /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current |
3a9cc685 NS |
2341 | binding value (possibly with anticipated builtins stripped). |
2342 | Diagnose conflicts and return updated decl. */ | |
2343 | ||
2344 | static tree | |
3c9cca88 | 2345 | update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot, |
3a9cc685 NS |
2346 | tree old, tree decl, bool is_friend) |
2347 | { | |
2348 | tree to_val = decl; | |
e2f35333 NS |
2349 | tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type; |
2350 | tree to_type = old_type; | |
3a9cc685 | 2351 | |
3c9cca88 NS |
2352 | gcc_assert (level->kind == sk_namespace ? !binding |
2353 | : level->kind != sk_class && !slot); | |
3a9cc685 NS |
2354 | if (old == error_mark_node) |
2355 | old = NULL_TREE; | |
2356 | ||
e2f35333 NS |
2357 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
2358 | { | |
2359 | tree other = to_type; | |
2360 | ||
2361 | if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) | |
2362 | other = old; | |
2363 | ||
2364 | /* Pushing an artificial typedef. See if this matches either | |
2365 | the type slot or the old value slot. */ | |
2366 | if (!other) | |
2367 | ; | |
2368 | else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl))) | |
2369 | /* Two artificial decls to same type. Do nothing. */ | |
2370 | return other; | |
2371 | else | |
2372 | goto conflict; | |
2373 | ||
2374 | if (old) | |
2375 | { | |
2376 | /* Slide decl into the type slot, keep old unaltered */ | |
2377 | to_type = decl; | |
2378 | to_val = old; | |
2379 | goto done; | |
2380 | } | |
2381 | } | |
2382 | ||
3a9cc685 NS |
2383 | if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
2384 | { | |
e2f35333 | 2385 | /* Slide old into the type slot. */ |
3a9cc685 NS |
2386 | to_type = old; |
2387 | old = NULL_TREE; | |
2388 | } | |
2389 | ||
2390 | if (DECL_DECLARES_FUNCTION_P (decl)) | |
2391 | { | |
2392 | if (!old) | |
2393 | ; | |
2394 | else if (OVL_P (old)) | |
2395 | { | |
2396 | for (ovl_iterator iter (old); iter; ++iter) | |
2397 | { | |
2398 | tree fn = *iter; | |
2399 | ||
2400 | if (iter.using_p () && matching_fn_p (fn, decl)) | |
2401 | { | |
2402 | /* If a function declaration in namespace scope or | |
2403 | block scope has the same name and the same | |
2404 | parameter-type- list (8.3.5) as a function | |
2405 | introduced by a using-declaration, and the | |
2406 | declarations do not declare the same function, | |
2407 | the program is ill-formed. [namespace.udecl]/14 */ | |
2408 | if (tree match = duplicate_decls (decl, fn, is_friend)) | |
2409 | return match; | |
2410 | else | |
2411 | /* FIXME: To preserve existing error behavior, we | |
2412 | still push the decl. This might change. */ | |
2413 | diagnose_name_conflict (decl, fn); | |
2414 | } | |
2415 | } | |
2416 | } | |
2417 | else | |
2418 | goto conflict; | |
2419 | ||
c14c0b15 NS |
2420 | if (to_type != old_type |
2421 | && warn_shadow | |
2422 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type)) | |
2423 | && !(DECL_IN_SYSTEM_HEADER (decl) | |
2424 | && DECL_IN_SYSTEM_HEADER (to_type))) | |
2425 | warning (OPT_Wshadow, "%q#D hides constructor for %q#D", | |
2426 | decl, to_type); | |
2427 | ||
3a9cc685 NS |
2428 | to_val = ovl_insert (decl, old); |
2429 | } | |
3a9cc685 NS |
2430 | else if (!old) |
2431 | ; | |
3a9cc685 NS |
2432 | else if (TREE_CODE (old) != TREE_CODE (decl)) |
2433 | /* Different kinds of decls conflict. */ | |
2434 | goto conflict; | |
2435 | else if (TREE_CODE (old) == TYPE_DECL) | |
2436 | { | |
e2f35333 | 2437 | if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))) |
3a9cc685 NS |
2438 | /* Two type decls to the same type. Do nothing. */ |
2439 | return old; | |
2440 | else | |
2441 | goto conflict; | |
2442 | } | |
2443 | else if (TREE_CODE (old) == NAMESPACE_DECL) | |
2444 | { | |
71bbbd13 NS |
2445 | /* Two maybe-aliased namespaces. If they're to the same target |
2446 | namespace, that's ok. */ | |
2447 | if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl)) | |
3a9cc685 | 2448 | goto conflict; |
71bbbd13 NS |
2449 | |
2450 | /* The new one must be an alias at this point. */ | |
2451 | gcc_assert (DECL_NAMESPACE_ALIAS (decl)); | |
2452 | return old; | |
3a9cc685 NS |
2453 | } |
2454 | else if (TREE_CODE (old) == VAR_DECL) | |
2455 | { | |
2456 | /* There can be two block-scope declarations of the same | |
2457 | variable, so long as they are `extern' declarations. */ | |
2458 | if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl)) | |
2459 | goto conflict; | |
2460 | else if (tree match = duplicate_decls (decl, old, false)) | |
2461 | return match; | |
2462 | else | |
2463 | goto conflict; | |
2464 | } | |
2465 | else | |
2466 | { | |
2467 | conflict: | |
2468 | diagnose_name_conflict (decl, old); | |
2469 | to_val = NULL_TREE; | |
2470 | } | |
2471 | ||
e2f35333 | 2472 | done: |
3a9cc685 NS |
2473 | if (to_val) |
2474 | { | |
7cd9cf2f NS |
2475 | if (level->kind == sk_namespace || to_type == decl || to_val == decl) |
2476 | add_decl_to_level (level, decl); | |
3a9cc685 NS |
2477 | else |
2478 | { | |
7cd9cf2f NS |
2479 | gcc_checking_assert (binding->value && OVL_P (binding->value)); |
2480 | update_local_overload (binding, to_val); | |
3a9cc685 NS |
2481 | } |
2482 | ||
3c9cca88 NS |
2483 | if (slot) |
2484 | { | |
2485 | if (STAT_HACK_P (*slot)) | |
2486 | { | |
e2f35333 | 2487 | STAT_TYPE (*slot) = to_type; |
3c9cca88 NS |
2488 | STAT_DECL (*slot) = to_val; |
2489 | } | |
2490 | else if (to_type) | |
2491 | *slot = stat_hack (to_val, to_type); | |
2492 | else | |
2493 | *slot = to_val; | |
2494 | } | |
2495 | else | |
2496 | { | |
e2f35333 | 2497 | binding->type = to_type; |
3c9cca88 NS |
2498 | binding->value = to_val; |
2499 | } | |
3a9cc685 NS |
2500 | } |
2501 | ||
2502 | return decl; | |
2503 | } | |
2504 | ||
7cd6ea64 | 2505 | /* Table of identifiers to extern C declarations (or LISTS thereof). */ |
539f481a | 2506 | |
7cd6ea64 | 2507 | static GTY(()) hash_table<named_decl_hash> *extern_c_decls; |
539f481a | 2508 | |
7cd6ea64 NS |
2509 | /* DECL has C linkage. If we have an existing instance, make sure the |
2510 | new one is compatible. Make sure it has the same exception | |
2511 | specification [7.5, 7.6]. Add DECL to the map. */ | |
539f481a NS |
2512 | |
2513 | static void | |
2514 | check_extern_c_conflict (tree decl) | |
2515 | { | |
2516 | /* Ignore artificial or system header decls. */ | |
2517 | if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl)) | |
2518 | return; | |
2519 | ||
38dff921 JM |
2520 | /* This only applies to decls at namespace scope. */ |
2521 | if (!DECL_NAMESPACE_SCOPE_P (decl)) | |
2522 | return; | |
2523 | ||
7cd6ea64 NS |
2524 | if (!extern_c_decls) |
2525 | extern_c_decls = hash_table<named_decl_hash>::create_ggc (127); | |
539f481a | 2526 | |
7cd6ea64 | 2527 | tree *slot = extern_c_decls |
9bc3f420 NS |
2528 | ->find_slot_with_hash (DECL_NAME (decl), |
2529 | IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT); | |
2530 | if (tree old = *slot) | |
539f481a | 2531 | { |
9bc3f420 NS |
2532 | if (TREE_CODE (old) == OVERLOAD) |
2533 | old = OVL_FUNCTION (old); | |
539f481a NS |
2534 | |
2535 | int mismatch = 0; | |
2536 | if (DECL_CONTEXT (old) == DECL_CONTEXT (decl)) | |
2537 | ; /* If they're in the same context, we'll have already complained | |
2538 | about a (possible) mismatch, when inserting the decl. */ | |
2539 | else if (!decls_match (decl, old)) | |
2540 | mismatch = 1; | |
7cd6ea64 NS |
2541 | else if (TREE_CODE (decl) == FUNCTION_DECL |
2542 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)), | |
2543 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)), | |
2544 | ce_normal)) | |
539f481a NS |
2545 | mismatch = -1; |
2546 | else if (DECL_ASSEMBLER_NAME_SET_P (old)) | |
2547 | SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old)); | |
2548 | ||
2549 | if (mismatch) | |
2550 | { | |
097f82ec | 2551 | auto_diagnostic_group d; |
539f481a | 2552 | pedwarn (input_location, 0, |
7cd6ea64 NS |
2553 | "conflicting C language linkage declaration %q#D", decl); |
2554 | inform (DECL_SOURCE_LOCATION (old), | |
2555 | "previous declaration %q#D", old); | |
539f481a | 2556 | if (mismatch < 0) |
7cd6ea64 NS |
2557 | inform (input_location, |
2558 | "due to different exception specifications"); | |
539f481a NS |
2559 | } |
2560 | else | |
9bc3f420 NS |
2561 | { |
2562 | if (old == *slot) | |
2563 | /* The hash table expects OVERLOADS, so construct one with | |
2564 | OLD as both the function and the chain. This allocate | |
2565 | an excess OVERLOAD node, but it's rare to have multiple | |
2566 | extern "C" decls of the same name. And we save | |
2567 | complicating the hash table logic (which is used | |
2568 | elsewhere). */ | |
2569 | *slot = ovl_make (old, old); | |
2570 | ||
2571 | slot = &OVL_CHAIN (*slot); | |
2572 | ||
2573 | /* Chain it on for c_linkage_binding's use. */ | |
2574 | *slot = tree_cons (NULL_TREE, decl, *slot); | |
2575 | } | |
539f481a | 2576 | } |
9bc3f420 NS |
2577 | else |
2578 | *slot = decl; | |
539f481a NS |
2579 | } |
2580 | ||
2581 | /* Returns a list of C-linkage decls with the name NAME. Used in | |
2582 | c-family/c-pragma.c to implement redefine_extname pragma. */ | |
2583 | ||
2584 | tree | |
2585 | c_linkage_bindings (tree name) | |
2586 | { | |
7cd6ea64 NS |
2587 | if (extern_c_decls) |
2588 | if (tree *slot = extern_c_decls | |
9bc3f420 NS |
2589 | ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT)) |
2590 | { | |
2591 | tree result = *slot; | |
2592 | if (TREE_CODE (result) == OVERLOAD) | |
2593 | result = OVL_CHAIN (result); | |
2594 | return result; | |
2595 | } | |
2596 | ||
539f481a NS |
2597 | return NULL_TREE; |
2598 | } | |
2599 | ||
097f82ec DM |
2600 | /* Subroutine of check_local_shadow. */ |
2601 | ||
2602 | static void | |
2603 | inform_shadowed (tree shadowed) | |
2604 | { | |
2605 | inform (DECL_SOURCE_LOCATION (shadowed), | |
2606 | "shadowed declaration is here"); | |
2607 | } | |
2608 | ||
c0c24822 NS |
2609 | /* DECL is being declared at a local scope. Emit suitable shadow |
2610 | warnings. */ | |
2611 | ||
2612 | static void | |
2613 | check_local_shadow (tree decl) | |
2614 | { | |
2615 | /* Don't complain about the parms we push and then pop | |
2616 | while tentatively parsing a function declarator. */ | |
2617 | if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl)) | |
2618 | return; | |
2619 | ||
c0c24822 NS |
2620 | /* External decls are something else. */ |
2621 | if (DECL_EXTERNAL (decl)) | |
2622 | return; | |
2623 | ||
2624 | tree old = NULL_TREE; | |
2625 | cp_binding_level *old_scope = NULL; | |
2626 | if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true)) | |
2627 | { | |
2628 | old = binding->value; | |
2629 | old_scope = binding->scope; | |
2630 | } | |
c0c24822 | 2631 | |
c0c24822 NS |
2632 | if (old |
2633 | && (TREE_CODE (old) == PARM_DECL | |
2634 | || VAR_P (old) | |
2635 | || (TREE_CODE (old) == TYPE_DECL | |
2636 | && (!DECL_ARTIFICIAL (old) | |
2637 | || TREE_CODE (decl) == TYPE_DECL))) | |
006fbbe9 | 2638 | && DECL_FUNCTION_SCOPE_P (old) |
c0c24822 | 2639 | && (!DECL_ARTIFICIAL (decl) |
63b48839 | 2640 | || is_capture_proxy (decl) |
c0c24822 NS |
2641 | || DECL_IMPLICIT_TYPEDEF_P (decl) |
2642 | || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl)))) | |
2643 | { | |
2644 | /* DECL shadows a local thing possibly of interest. */ | |
2645 | ||
63b48839 VV |
2646 | /* DR 2211: check that captures and parameters |
2647 | do not have the same name. */ | |
2648 | if (is_capture_proxy (decl)) | |
2649 | { | |
2650 | if (current_lambda_expr () | |
2651 | && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ()) | |
2652 | && TREE_CODE (old) == PARM_DECL | |
2653 | && DECL_NAME (decl) != this_identifier) | |
2654 | { | |
2655 | error_at (DECL_SOURCE_LOCATION (old), | |
2656 | "lambda parameter %qD " | |
2657 | "previously declared as a capture", old); | |
2658 | } | |
2659 | return; | |
2660 | } | |
c0c24822 | 2661 | /* Don't complain if it's from an enclosing function. */ |
63b48839 | 2662 | else if (DECL_CONTEXT (old) == current_function_decl |
c0c24822 NS |
2663 | && TREE_CODE (decl) != PARM_DECL |
2664 | && TREE_CODE (old) == PARM_DECL) | |
2665 | { | |
2666 | /* Go to where the parms should be and see if we find | |
2667 | them there. */ | |
2668 | cp_binding_level *b = current_binding_level->level_chain; | |
2669 | ||
2670 | if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) | |
2671 | /* Skip the ctor/dtor cleanup level. */ | |
2672 | b = b->level_chain; | |
2673 | ||
2674 | /* ARM $8.3 */ | |
2675 | if (b->kind == sk_function_parms) | |
2676 | { | |
2677 | error ("declaration of %q#D shadows a parameter", decl); | |
2678 | return; | |
2679 | } | |
2680 | } | |
2681 | ||
2682 | /* The local structure or class can't use parameters of | |
2683 | the containing function anyway. */ | |
2684 | if (DECL_CONTEXT (old) != current_function_decl) | |
2685 | { | |
2686 | for (cp_binding_level *scope = current_binding_level; | |
2687 | scope != old_scope; scope = scope->level_chain) | |
2688 | if (scope->kind == sk_class | |
2689 | && !LAMBDA_TYPE_P (scope->this_entity)) | |
2690 | return; | |
2691 | } | |
2692 | /* Error if redeclaring a local declared in a | |
2693 | init-statement or in the condition of an if or | |
2694 | switch statement when the new declaration is in the | |
2695 | outermost block of the controlled statement. | |
2696 | Redeclaring a variable from a for or while condition is | |
2697 | detected elsewhere. */ | |
2698 | else if (VAR_P (old) | |
2699 | && old_scope == current_binding_level->level_chain | |
2700 | && (old_scope->kind == sk_cond || old_scope->kind == sk_for)) | |
2701 | { | |
097f82ec | 2702 | auto_diagnostic_group d; |
c0c24822 NS |
2703 | error ("redeclaration of %q#D", decl); |
2704 | inform (DECL_SOURCE_LOCATION (old), | |
2705 | "%q#D previously declared here", old); | |
2706 | return; | |
2707 | } | |
2708 | /* C++11: | |
2709 | 3.3.3/3: The name declared in an exception-declaration (...) | |
2710 | shall not be redeclared in the outermost block of the handler. | |
2711 | 3.3.3/2: A parameter name shall not be redeclared (...) in | |
2712 | the outermost block of any handler associated with a | |
2713 | function-try-block. | |
2714 | 3.4.1/15: The function parameter names shall not be redeclared | |
2715 | in the exception-declaration nor in the outermost block of a | |
2716 | handler for the function-try-block. */ | |
2717 | else if ((TREE_CODE (old) == VAR_DECL | |
2718 | && old_scope == current_binding_level->level_chain | |
2719 | && old_scope->kind == sk_catch) | |
2720 | || (TREE_CODE (old) == PARM_DECL | |
2721 | && (current_binding_level->kind == sk_catch | |
2722 | || current_binding_level->level_chain->kind == sk_catch) | |
2723 | && in_function_try_handler)) | |
2724 | { | |
097f82ec | 2725 | auto_diagnostic_group d; |
c0c24822 NS |
2726 | if (permerror (input_location, "redeclaration of %q#D", decl)) |
2727 | inform (DECL_SOURCE_LOCATION (old), | |
2728 | "%q#D previously declared here", old); | |
2729 | return; | |
2730 | } | |
2731 | ||
2732 | /* If '-Wshadow=compatible-local' is specified without other | |
2733 | -Wshadow= flags, we will warn only when the type of the | |
2734 | shadowing variable (DECL) can be converted to that of the | |
2735 | shadowed parameter (OLD_LOCAL). The reason why we only check | |
2736 | if DECL's type can be converted to OLD_LOCAL's type (but not the | |
2737 | other way around) is because when users accidentally shadow a | |
2738 | parameter, more than often they would use the variable | |
2739 | thinking (mistakenly) it's still the parameter. It would be | |
2740 | rare that users would use the variable in the place that | |
2741 | expects the parameter but thinking it's a new decl. */ | |
2742 | ||
2743 | enum opt_code warning_code; | |
2744 | if (warn_shadow) | |
2745 | warning_code = OPT_Wshadow; | |
2746 | else if (warn_shadow_local) | |
2747 | warning_code = OPT_Wshadow_local; | |
2748 | else if (warn_shadow_compatible_local | |
9db84ece NS |
2749 | && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)) |
2750 | || (!dependent_type_p (TREE_TYPE (decl)) | |
2751 | && !dependent_type_p (TREE_TYPE (old)) | |
3897f134 NS |
2752 | /* If the new decl uses auto, we don't yet know |
2753 | its type (the old type cannot be using auto | |
2754 | at this point, without also being | |
2755 | dependent). This is an indication we're | |
2756 | (now) doing the shadow checking too | |
2757 | early. */ | |
2758 | && !type_uses_auto (TREE_TYPE (decl)) | |
9db84ece NS |
2759 | && can_convert (TREE_TYPE (old), TREE_TYPE (decl), |
2760 | tf_none)))) | |
c0c24822 NS |
2761 | warning_code = OPT_Wshadow_compatible_local; |
2762 | else | |
2763 | return; | |
2764 | ||
2765 | const char *msg; | |
2766 | if (TREE_CODE (old) == PARM_DECL) | |
2767 | msg = "declaration of %q#D shadows a parameter"; | |
2768 | else if (is_capture_proxy (old)) | |
2769 | msg = "declaration of %qD shadows a lambda capture"; | |
2770 | else | |
2771 | msg = "declaration of %qD shadows a previous local"; | |
2772 | ||
097f82ec | 2773 | auto_diagnostic_group d; |
c0c24822 | 2774 | if (warning_at (input_location, warning_code, msg, decl)) |
097f82ec | 2775 | inform_shadowed (old); |
c0c24822 NS |
2776 | return; |
2777 | } | |
2778 | ||
2779 | if (!warn_shadow) | |
2780 | return; | |
2781 | ||
2782 | /* Don't warn for artificial things that are not implicit typedefs. */ | |
2783 | if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl)) | |
2784 | return; | |
2785 | ||
2786 | if (nonlambda_method_basetype ()) | |
2787 | if (tree member = lookup_member (current_nonlambda_class_type (), | |
2788 | DECL_NAME (decl), /*protect=*/0, | |
2789 | /*want_type=*/false, tf_warning_or_error)) | |
2790 | { | |
2791 | member = MAYBE_BASELINK_FUNCTIONS (member); | |
2792 | ||
2793 | /* Warn if a variable shadows a non-function, or the variable | |
2794 | is a function or a pointer-to-function. */ | |
5256a7f5 | 2795 | if (!OVL_P (member) |
c0c24822 NS |
2796 | || TREE_CODE (decl) == FUNCTION_DECL |
2797 | || TYPE_PTRFN_P (TREE_TYPE (decl)) | |
2798 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))) | |
2799 | { | |
097f82ec | 2800 | auto_diagnostic_group d; |
c0c24822 NS |
2801 | if (warning_at (input_location, OPT_Wshadow, |
2802 | "declaration of %qD shadows a member of %qT", | |
2803 | decl, current_nonlambda_class_type ()) | |
2804 | && DECL_P (member)) | |
097f82ec | 2805 | inform_shadowed (member); |
c0c24822 NS |
2806 | } |
2807 | return; | |
2808 | } | |
2809 | ||
2810 | /* Now look for a namespace shadow. */ | |
3c9cca88 | 2811 | old = find_namespace_value (current_namespace, DECL_NAME (decl)); |
c0c24822 NS |
2812 | if (old |
2813 | && (VAR_P (old) | |
2814 | || (TREE_CODE (old) == TYPE_DECL | |
2815 | && (!DECL_ARTIFICIAL (old) | |
2816 | || TREE_CODE (decl) == TYPE_DECL))) | |
2817 | && !instantiating_current_function_p ()) | |
2818 | /* XXX shadow warnings in outer-more namespaces */ | |
2819 | { | |
097f82ec | 2820 | auto_diagnostic_group d; |
c0c24822 NS |
2821 | if (warning_at (input_location, OPT_Wshadow, |
2822 | "declaration of %qD shadows a global declaration", | |
2823 | decl)) | |
097f82ec | 2824 | inform_shadowed (old); |
c0c24822 NS |
2825 | return; |
2826 | } | |
2827 | ||
2828 | return; | |
c0c24822 NS |
2829 | } |
2830 | ||
e4ea7a4c NS |
2831 | /* DECL is being pushed inside function CTX. Set its context, if |
2832 | needed. */ | |
2833 | ||
2834 | static void | |
2835 | set_decl_context_in_fn (tree ctx, tree decl) | |
2836 | { | |
2837 | if (!DECL_CONTEXT (decl) | |
2838 | /* A local declaration for a function doesn't constitute | |
2839 | nesting. */ | |
2840 | && TREE_CODE (decl) != FUNCTION_DECL | |
2841 | /* A local declaration for an `extern' variable is in the | |
2842 | scope of the current namespace, not the current | |
2843 | function. */ | |
2844 | && !(VAR_P (decl) && DECL_EXTERNAL (decl)) | |
2845 | /* When parsing the parameter list of a function declarator, | |
2846 | don't set DECL_CONTEXT to an enclosing function. When we | |
2847 | push the PARM_DECLs in order to process the function body, | |
2848 | current_binding_level->this_entity will be set. */ | |
2849 | && !(TREE_CODE (decl) == PARM_DECL | |
2850 | && current_binding_level->kind == sk_function_parms | |
2851 | && current_binding_level->this_entity == NULL)) | |
2852 | DECL_CONTEXT (decl) = ctx; | |
2853 | ||
2854 | /* If this is the declaration for a namespace-scope function, | |
2855 | but the declaration itself is in a local scope, mark the | |
2856 | declaration. */ | |
2857 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl)) | |
2858 | DECL_LOCAL_FUNCTION_P (decl) = 1; | |
2859 | } | |
2860 | ||
2861 | /* DECL is a local-scope decl with linkage. SHADOWED is true if the | |
2862 | name is already bound at the current level. | |
2863 | ||
2864 | [basic.link] If there is a visible declaration of an entity with | |
2865 | linkage having the same name and type, ignoring entities declared | |
2866 | outside the innermost enclosing namespace scope, the block scope | |
2867 | declaration declares that same entity and receives the linkage of | |
2868 | the previous declaration. | |
2869 | ||
2870 | Also, make sure that this decl matches any existing external decl | |
2871 | in the enclosing namespace. */ | |
2872 | ||
2873 | static void | |
2874 | set_local_extern_decl_linkage (tree decl, bool shadowed) | |
2875 | { | |
2876 | tree ns_value = decl; /* Unique marker. */ | |
2877 | ||
2878 | if (!shadowed) | |
2879 | { | |
2880 | tree loc_value = innermost_non_namespace_value (DECL_NAME (decl)); | |
2881 | if (!loc_value) | |
2882 | { | |
2883 | ns_value | |
3c9cca88 | 2884 | = find_namespace_value (current_namespace, DECL_NAME (decl)); |
e4ea7a4c NS |
2885 | loc_value = ns_value; |
2886 | } | |
8f3284a4 NS |
2887 | if (loc_value == error_mark_node |
2888 | /* An ambiguous lookup. */ | |
2889 | || (loc_value && TREE_CODE (loc_value) == TREE_LIST)) | |
e4ea7a4c NS |
2890 | loc_value = NULL_TREE; |
2891 | ||
2892 | for (ovl_iterator iter (loc_value); iter; ++iter) | |
ef4c5e78 | 2893 | if (!iter.hidden_p () |
e4ea7a4c NS |
2894 | && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter)) |
2895 | && decls_match (*iter, decl)) | |
2896 | { | |
2897 | /* The standard only says that the local extern inherits | |
2898 | linkage from the previous decl; in particular, default | |
2899 | args are not shared. Add the decl into a hash table to | |
2900 | make sure only the previous decl in this case is seen | |
2901 | by the middle end. */ | |
2902 | struct cxx_int_tree_map *h; | |
2903 | ||
2904 | /* We inherit the outer decl's linkage. But we're a | |
2905 | different decl. */ | |
2906 | TREE_PUBLIC (decl) = TREE_PUBLIC (*iter); | |
2907 | ||
2908 | if (cp_function_chain->extern_decl_map == NULL) | |
2909 | cp_function_chain->extern_decl_map | |
2910 | = hash_table<cxx_int_tree_map_hasher>::create_ggc (20); | |
2911 | ||
2912 | h = ggc_alloc<cxx_int_tree_map> (); | |
2913 | h->uid = DECL_UID (decl); | |
2914 | h->to = *iter; | |
2915 | cxx_int_tree_map **loc = cp_function_chain->extern_decl_map | |
2916 | ->find_slot (h, INSERT); | |
2917 | *loc = h; | |
2918 | break; | |
2919 | } | |
2920 | } | |
2921 | ||
2922 | if (TREE_PUBLIC (decl)) | |
2923 | { | |
2924 | /* DECL is externally visible. Make sure it matches a matching | |
3c9cca88 | 2925 | decl in the namespace scope. We only really need to check |
e4ea7a4c NS |
2926 | this when inserting the decl, not when we find an existing |
2927 | match in the current scope. However, in practice we're | |
2928 | going to be inserting a new decl in the majority of cases -- | |
2929 | who writes multiple extern decls for the same thing in the | |
2930 | same local scope? Doing it here often avoids a duplicate | |
2931 | namespace lookup. */ | |
2932 | ||
2933 | /* Avoid repeating a lookup. */ | |
2934 | if (ns_value == decl) | |
3c9cca88 | 2935 | ns_value = find_namespace_value (current_namespace, DECL_NAME (decl)); |
e4ea7a4c | 2936 | |
8f3284a4 NS |
2937 | if (ns_value == error_mark_node |
2938 | || (ns_value && TREE_CODE (ns_value) == TREE_LIST)) | |
e4ea7a4c NS |
2939 | ns_value = NULL_TREE; |
2940 | ||
2941 | for (ovl_iterator iter (ns_value); iter; ++iter) | |
2942 | { | |
2943 | tree other = *iter; | |
2944 | ||
2945 | if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other))) | |
2946 | ; /* Not externally visible. */ | |
2947 | else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other)) | |
2948 | ; /* Both are extern "C", we'll check via that mechanism. */ | |
2949 | else if (TREE_CODE (other) != TREE_CODE (decl) | |
2950 | || ((VAR_P (decl) || matching_fn_p (other, decl)) | |
2951 | && !comptypes (TREE_TYPE (decl), TREE_TYPE (other), | |
2952 | COMPARE_REDECLARATION))) | |
2953 | { | |
097f82ec | 2954 | auto_diagnostic_group d; |
e4ea7a4c NS |
2955 | if (permerror (DECL_SOURCE_LOCATION (decl), |
2956 | "local external declaration %q#D", decl)) | |
2957 | inform (DECL_SOURCE_LOCATION (other), | |
2958 | "does not match previous declaration %q#D", other); | |
2959 | break; | |
2960 | } | |
2961 | } | |
2962 | } | |
2963 | } | |
2964 | ||
3a9cc685 NS |
2965 | /* Record DECL as belonging to the current lexical scope. Check for |
2966 | errors (such as an incompatible declaration for the same name | |
2967 | already seen in the same scope). IS_FRIEND is true if DECL is | |
b655c310 | 2968 | declared as a friend. |
00e8de68 | 2969 | |
3a9cc685 NS |
2970 | Returns either DECL or an old decl for the same name. If an old |
2971 | decl is returned, it may have been smashed to agree with what DECL | |
2972 | says. */ | |
89b578be | 2973 | |
b655c310 | 2974 | static tree |
3a9cc685 | 2975 | do_pushdecl (tree decl, bool is_friend) |
89b578be | 2976 | { |
3a9cc685 | 2977 | if (decl == error_mark_node) |
b655c310 | 2978 | return error_mark_node; |
00e8de68 | 2979 | |
3a9cc685 NS |
2980 | if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl) |
2981 | set_decl_context_in_fn (current_function_decl, decl); | |
c8094d83 | 2982 | |
3a9cc685 NS |
2983 | /* The binding level we will be pushing into. During local class |
2984 | pushing, we want to push to the containing scope. */ | |
2985 | cp_binding_level *level = current_binding_level; | |
2986 | while (level->kind == sk_class) | |
2987 | level = level->level_chain; | |
00e8de68 | 2988 | |
e833f686 NS |
2989 | /* An anonymous namespace has a NULL DECL_NAME, but we still want to |
2990 | insert it. Other NULL-named decls, not so much. */ | |
2991 | tree name = DECL_NAME (decl); | |
2992 | if (name || TREE_CODE (decl) == NAMESPACE_DECL) | |
00e8de68 | 2993 | { |
3c9cca88 | 2994 | cxx_binding *binding = NULL; /* Local scope binding. */ |
3a9cc685 | 2995 | tree ns = NULL_TREE; /* Searched namespace. */ |
3c9cca88 | 2996 | tree *slot = NULL; /* Binding slot in namespace. */ |
3a9cc685 | 2997 | tree old = NULL_TREE; |
00e8de68 | 2998 | |
3a9cc685 | 2999 | if (level->kind == sk_namespace) |
b655c310 | 3000 | { |
3a9cc685 NS |
3001 | /* We look in the decl's namespace for an existing |
3002 | declaration, even though we push into the current | |
3003 | namespace. */ | |
3004 | ns = (DECL_NAMESPACE_SCOPE_P (decl) | |
3005 | ? CP_DECL_CONTEXT (decl) : current_namespace); | |
3006 | /* Create the binding, if this is current namespace, because | |
3007 | that's where we'll be pushing anyway. */ | |
3c9cca88 NS |
3008 | slot = find_namespace_slot (ns, name, ns == current_namespace); |
3009 | if (slot) | |
3010 | old = MAYBE_STAT_DECL (*slot); | |
b655c310 | 3011 | } |
3a9cc685 | 3012 | else |
3c9cca88 NS |
3013 | { |
3014 | binding = find_local_binding (level, name); | |
3015 | if (binding) | |
3016 | old = binding->value; | |
3017 | } | |
00e8de68 | 3018 | |
3a9cc685 NS |
3019 | if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl) |
3020 | && DECL_EXTERNAL (decl)) | |
3021 | set_local_extern_decl_linkage (decl, old != NULL_TREE); | |
00e8de68 | 3022 | |
3a9cc685 NS |
3023 | if (old == error_mark_node) |
3024 | old = NULL_TREE; | |
00e8de68 | 3025 | |
3a9cc685 NS |
3026 | for (ovl_iterator iter (old); iter; ++iter) |
3027 | if (iter.using_p ()) | |
3028 | ; /* Ignore using decls here. */ | |
3029 | else if (tree match = duplicate_decls (decl, *iter, is_friend)) | |
ef4c5e78 | 3030 | { |
274c1516 NS |
3031 | if (match == error_mark_node) |
3032 | ; | |
3033 | else if (TREE_CODE (match) == TYPE_DECL) | |
3034 | /* The IDENTIFIER will have the type referring to the | |
3035 | now-smashed TYPE_DECL, because ...? Reset it. */ | |
3036 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match)); | |
3037 | else if (iter.hidden_p () && !DECL_HIDDEN_P (match)) | |
ef4c5e78 NS |
3038 | { |
3039 | /* Unhiding a previously hidden decl. */ | |
3040 | tree head = iter.reveal_node (old); | |
3041 | if (head != old) | |
3042 | { | |
3043 | if (!ns) | |
3c9cca88 NS |
3044 | { |
3045 | update_local_overload (binding, head); | |
3046 | binding->value = head; | |
3047 | } | |
3048 | else if (STAT_HACK_P (*slot)) | |
3049 | STAT_DECL (*slot) = head; | |
3050 | else | |
3051 | *slot = head; | |
ef4c5e78 | 3052 | } |
7cd6ea64 NS |
3053 | if (DECL_EXTERN_C_P (match)) |
3054 | /* We need to check and register the decl now. */ | |
ef4c5e78 NS |
3055 | check_extern_c_conflict (match); |
3056 | } | |
3057 | return match; | |
3058 | } | |
c8094d83 | 3059 | |
3a9cc685 | 3060 | /* We are pushing a new decl. */ |
00e8de68 | 3061 | |
ef4c5e78 NS |
3062 | /* Skip a hidden builtin we failed to match already. There can |
3063 | only be one. */ | |
3064 | if (old && anticipated_builtin_p (old)) | |
3065 | old = OVL_CHAIN (old); | |
5294e4c3 | 3066 | |
3a9cc685 | 3067 | check_template_shadow (decl); |
00e8de68 | 3068 | |
3a9cc685 | 3069 | if (DECL_DECLARES_FUNCTION_P (decl)) |
b655c310 | 3070 | { |
3a9cc685 | 3071 | check_default_args (decl); |
00e8de68 | 3072 | |
3a9cc685 | 3073 | if (is_friend) |
b655c310 | 3074 | { |
3a9cc685 | 3075 | if (level->kind != sk_namespace) |
b1a7e33d NS |
3076 | { |
3077 | /* In a local class, a friend function declaration must | |
3078 | find a matching decl in the innermost non-class scope. | |
3079 | [class.friend/11] */ | |
3080 | error ("friend declaration %qD in local class without " | |
3081 | "prior local declaration", decl); | |
3082 | /* Don't attempt to push it. */ | |
3083 | return error_mark_node; | |
3084 | } | |
6c072e21 NS |
3085 | /* Hide it from ordinary lookup. */ |
3086 | DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true; | |
b655c310 NS |
3087 | } |
3088 | } | |
00e8de68 | 3089 | |
3a9cc685 | 3090 | if (level->kind != sk_namespace) |
b655c310 | 3091 | { |
3a9cc685 | 3092 | check_local_shadow (decl); |
00e8de68 | 3093 | |
3a9cc685 NS |
3094 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
3095 | /* A local namespace alias. */ | |
3096 | set_identifier_type_value (name, NULL_TREE); | |
00e8de68 | 3097 | |
3a9cc685 NS |
3098 | if (!binding) |
3099 | binding = create_local_binding (level, name); | |
b655c310 | 3100 | } |
3c9cca88 | 3101 | else if (!slot) |
b655c310 | 3102 | { |
3a9cc685 | 3103 | ns = current_namespace; |
3c9cca88 | 3104 | slot = find_namespace_slot (ns, name, true); |
be3b7dcf NS |
3105 | /* Update OLD to reflect the namespace we're going to be |
3106 | pushing into. */ | |
3107 | old = MAYBE_STAT_DECL (*slot); | |
b655c310 | 3108 | } |
fdf03377 | 3109 | |
3c9cca88 | 3110 | old = update_binding (level, binding, slot, old, decl, is_friend); |
fdf03377 | 3111 | |
3a9cc685 NS |
3112 | if (old != decl) |
3113 | /* An existing decl matched, use it. */ | |
3114 | decl = old; | |
3115 | else if (TREE_CODE (decl) == TYPE_DECL) | |
3116 | { | |
3117 | tree type = TREE_TYPE (decl); | |
00e8de68 | 3118 | |
3a9cc685 | 3119 | if (type != error_mark_node) |
b655c310 | 3120 | { |
3a9cc685 NS |
3121 | if (TYPE_NAME (type) != decl) |
3122 | set_underlying_type (decl); | |
00e8de68 | 3123 | |
3a9cc685 NS |
3124 | if (!ns) |
3125 | set_identifier_type_value_with_scope (name, decl, level); | |
b655c310 | 3126 | else |
3a9cc685 | 3127 | SET_IDENTIFIER_TYPE_VALUE (name, global_type_node); |
b655c310 | 3128 | } |
7b3b6ae4 | 3129 | |
3a9cc685 NS |
3130 | /* If this is a locally defined typedef in a function that |
3131 | is not a template instantation, record it to implement | |
3132 | -Wunused-local-typedefs. */ | |
3133 | if (!instantiating_current_function_p ()) | |
3134 | record_locally_defined_typedef (decl); | |
3135 | } | |
3136 | else if (VAR_P (decl)) | |
3137 | maybe_register_incomplete_var (decl); | |
7cd6ea64 NS |
3138 | |
3139 | if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL) | |
3140 | && DECL_EXTERN_C_P (decl)) | |
3a9cc685 | 3141 | check_extern_c_conflict (decl); |
00e8de68 | 3142 | } |
3a9cc685 NS |
3143 | else |
3144 | add_decl_to_level (level, decl); | |
00e8de68 | 3145 | |
3a9cc685 | 3146 | return decl; |
00e8de68 GDR |
3147 | } |
3148 | ||
4f15a5da | 3149 | /* Record a decl-node X as belonging to the current lexical scope. |
32196b87 NS |
3150 | It's a friend if IS_FRIEND is true -- which affects exactly where |
3151 | we push it. */ | |
575bfb00 LC |
3152 | |
3153 | tree | |
4f15a5da | 3154 | pushdecl (tree x, bool is_friend) |
575bfb00 | 3155 | { |
b655c310 | 3156 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
32196b87 | 3157 | tree ret = do_pushdecl (x, is_friend); |
b655c310 | 3158 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
575bfb00 LC |
3159 | return ret; |
3160 | } | |
3161 | ||
b655c310 NS |
3162 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
3163 | DECL, or a modified version thereof. */ | |
00e8de68 | 3164 | |
b655c310 NS |
3165 | tree |
3166 | maybe_push_decl (tree decl) | |
00e8de68 | 3167 | { |
b655c310 | 3168 | tree type = TREE_TYPE (decl); |
00e8de68 | 3169 | |
b655c310 NS |
3170 | /* Add this decl to the current binding level, but not if it comes |
3171 | from another scope, e.g. a static member variable. TEM may equal | |
3172 | DECL or it may be a previous decl of the same name. */ | |
3173 | if (decl == error_mark_node | |
3174 | || (TREE_CODE (decl) != PARM_DECL | |
3175 | && DECL_CONTEXT (decl) != NULL_TREE | |
3176 | /* Definitions of namespace members outside their namespace are | |
3177 | possible. */ | |
3178 | && !DECL_NAMESPACE_SCOPE_P (decl)) | |
3179 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) | |
3180 | || type == unknown_type_node | |
3181 | /* The declaration of a template specialization does not affect | |
3182 | the functions available for overload resolution, so we do not | |
3183 | call pushdecl. */ | |
3184 | || (TREE_CODE (decl) == FUNCTION_DECL | |
3185 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
3186 | return decl; | |
00e8de68 | 3187 | else |
b655c310 | 3188 | return pushdecl (decl); |
00e8de68 GDR |
3189 | } |
3190 | ||
b655c310 | 3191 | /* Bind DECL to ID in the current_binding_level, assumed to be a local |
9d029ddf NS |
3192 | binding level. If IS_USING is true, DECL got here through a |
3193 | using-declaration. */ | |
00e8de68 | 3194 | |
9d029ddf NS |
3195 | static void |
3196 | push_local_binding (tree id, tree decl, bool is_using) | |
00e8de68 | 3197 | { |
b655c310 NS |
3198 | /* Skip over any local classes. This makes sense if we call |
3199 | push_local_binding with a friend decl of a local class. */ | |
3c9cca88 | 3200 | cp_binding_level *b = innermost_nonclass_level (); |
5ad4f1c8 | 3201 | |
3c9cca88 NS |
3202 | gcc_assert (b->kind != sk_namespace); |
3203 | if (find_local_binding (b, id)) | |
b655c310 NS |
3204 | { |
3205 | /* Supplement the existing binding. */ | |
3206 | if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) | |
3207 | /* It didn't work. Something else must be bound at this | |
3208 | level. Do not add DECL to the list of things to pop | |
3209 | later. */ | |
3210 | return; | |
3211 | } | |
3212 | else | |
3213 | /* Create a new binding. */ | |
3214 | push_binding (id, decl, b); | |
5a167978 | 3215 | |
9d029ddf NS |
3216 | if (TREE_CODE (decl) == OVERLOAD || is_using) |
3217 | /* We must put the OVERLOAD or using into a TREE_LIST since we | |
3218 | cannot use the decl's chain itself. */ | |
b655c310 | 3219 | decl = build_tree_list (NULL_TREE, decl); |
5a167978 | 3220 | |
b655c310 NS |
3221 | /* And put DECL on the list of things declared by the current |
3222 | binding level. */ | |
9d029ddf | 3223 | add_decl_to_level (b, decl); |
5a167978 GDR |
3224 | } |
3225 | ||
b655c310 NS |
3226 | \f |
3227 | /* true means unconditionally make a BLOCK for the next level pushed. */ | |
a5e6b29b | 3228 | |
b655c310 | 3229 | static bool keep_next_level_flag; |
d5f4eddd | 3230 | |
b655c310 | 3231 | static int binding_depth = 0; |
d5f4eddd | 3232 | |
b655c310 NS |
3233 | static void |
3234 | indent (int depth) | |
d5f4eddd | 3235 | { |
b655c310 | 3236 | int i; |
d5f4eddd | 3237 | |
b655c310 NS |
3238 | for (i = 0; i < depth * 2; i++) |
3239 | putc (' ', stderr); | |
d5f4eddd JM |
3240 | } |
3241 | ||
b655c310 NS |
3242 | /* Return a string describing the kind of SCOPE we have. */ |
3243 | static const char * | |
3244 | cp_binding_level_descriptor (cp_binding_level *scope) | |
ed3cf953 | 3245 | { |
b655c310 NS |
3246 | /* The order of this table must match the "scope_kind" |
3247 | enumerators. */ | |
3248 | static const char* scope_kind_names[] = { | |
3249 | "block-scope", | |
3250 | "cleanup-scope", | |
3251 | "try-scope", | |
3252 | "catch-scope", | |
3253 | "for-scope", | |
3254 | "function-parameter-scope", | |
3255 | "class-scope", | |
3256 | "namespace-scope", | |
3257 | "template-parameter-scope", | |
3258 | "template-explicit-spec-scope" | |
3259 | }; | |
3260 | const scope_kind kind = scope->explicit_spec_p | |
3261 | ? sk_template_spec : scope->kind; | |
ed3cf953 | 3262 | |
b655c310 | 3263 | return scope_kind_names[kind]; |
ed3cf953 GDR |
3264 | } |
3265 | ||
b655c310 NS |
3266 | /* Output a debugging information about SCOPE when performing |
3267 | ACTION at LINE. */ | |
3268 | static void | |
3269 | cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) | |
ed3cf953 | 3270 | { |
b655c310 NS |
3271 | const char *desc = cp_binding_level_descriptor (scope); |
3272 | if (scope->this_entity) | |
a9c697b8 | 3273 | verbatim ("%s %<%s(%E)%> %p %d", action, desc, |
b655c310 NS |
3274 | scope->this_entity, (void *) scope, line); |
3275 | else | |
a9c697b8 | 3276 | verbatim ("%s %s %p %d", action, desc, (void *) scope, line); |
ed3cf953 GDR |
3277 | } |
3278 | ||
b655c310 | 3279 | /* A chain of binding_level structures awaiting reuse. */ |
ecba6c56 | 3280 | |
b655c310 | 3281 | static GTY((deletable)) cp_binding_level *free_binding_level; |
ecba6c56 | 3282 | |
b655c310 | 3283 | /* Insert SCOPE as the innermost binding level. */ |
ecba6c56 | 3284 | |
b655c310 NS |
3285 | void |
3286 | push_binding_level (cp_binding_level *scope) | |
3287 | { | |
3288 | /* Add it to the front of currently active scopes stack. */ | |
3289 | scope->level_chain = current_binding_level; | |
3290 | current_binding_level = scope; | |
3291 | keep_next_level_flag = false; | |
3292 | ||
3293 | if (ENABLE_SCOPE_CHECKING) | |
ecba6c56 | 3294 | { |
b655c310 NS |
3295 | scope->binding_depth = binding_depth; |
3296 | indent (binding_depth); | |
3297 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
3298 | "push"); | |
3299 | binding_depth++; | |
ecba6c56 | 3300 | } |
ecba6c56 DS |
3301 | } |
3302 | ||
b655c310 NS |
3303 | /* Create a new KIND scope and make it the top of the active scopes stack. |
3304 | ENTITY is the scope of the associated C++ entity (namespace, class, | |
3305 | function, C++0x enumeration); it is NULL otherwise. */ | |
3a636414 | 3306 | |
b655c310 NS |
3307 | cp_binding_level * |
3308 | begin_scope (scope_kind kind, tree entity) | |
3a636414 | 3309 | { |
b655c310 | 3310 | cp_binding_level *scope; |
3a636414 | 3311 | |
b655c310 NS |
3312 | /* Reuse or create a struct for this binding level. */ |
3313 | if (!ENABLE_SCOPE_CHECKING && free_binding_level) | |
3a636414 | 3314 | { |
b655c310 NS |
3315 | scope = free_binding_level; |
3316 | free_binding_level = scope->level_chain; | |
3317 | memset (scope, 0, sizeof (cp_binding_level)); | |
3a636414 | 3318 | } |
b655c310 NS |
3319 | else |
3320 | scope = ggc_cleared_alloc<cp_binding_level> (); | |
3a636414 | 3321 | |
b655c310 NS |
3322 | scope->this_entity = entity; |
3323 | scope->more_cleanups_ok = true; | |
3324 | switch (kind) | |
3325 | { | |
3326 | case sk_cleanup: | |
3327 | scope->keep = true; | |
3328 | break; | |
a5e6b29b | 3329 | |
b655c310 NS |
3330 | case sk_template_spec: |
3331 | scope->explicit_spec_p = true; | |
3332 | kind = sk_template_parms; | |
3333 | /* Fall through. */ | |
3334 | case sk_template_parms: | |
3335 | case sk_block: | |
3336 | case sk_try: | |
3337 | case sk_catch: | |
3338 | case sk_for: | |
3339 | case sk_cond: | |
3340 | case sk_class: | |
3341 | case sk_scoped_enum: | |
3342 | case sk_function_parms: | |
3343 | case sk_transaction: | |
3344 | case sk_omp: | |
3345 | scope->keep = keep_next_level_flag; | |
3346 | break; | |
a5e6b29b | 3347 | |
b655c310 NS |
3348 | case sk_namespace: |
3349 | NAMESPACE_LEVEL (entity) = scope; | |
a5e6b29b | 3350 | break; |
b655c310 NS |
3351 | |
3352 | default: | |
3353 | /* Should not happen. */ | |
3354 | gcc_unreachable (); | |
3355 | break; | |
3356 | } | |
3357 | scope->kind = kind; | |
3358 | ||
3359 | push_binding_level (scope); | |
3360 | ||
3361 | return scope; | |
575bfb00 LC |
3362 | } |
3363 | ||
b655c310 NS |
3364 | /* We're about to leave current scope. Pop the top of the stack of |
3365 | currently active scopes. Return the enclosing scope, now active. */ | |
575bfb00 | 3366 | |
b655c310 NS |
3367 | cp_binding_level * |
3368 | leave_scope (void) | |
575bfb00 | 3369 | { |
b655c310 | 3370 | cp_binding_level *scope = current_binding_level; |
a5e6b29b | 3371 | |
b655c310 NS |
3372 | if (scope->kind == sk_namespace && class_binding_level) |
3373 | current_binding_level = class_binding_level; | |
2b8dfc07 | 3374 | |
b655c310 NS |
3375 | /* We cannot leave a scope, if there are none left. */ |
3376 | if (NAMESPACE_LEVEL (global_namespace)) | |
3377 | gcc_assert (!global_scope_p (scope)); | |
ed3cf953 | 3378 | |
b655c310 NS |
3379 | if (ENABLE_SCOPE_CHECKING) |
3380 | { | |
3381 | indent (--binding_depth); | |
3382 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
3383 | "leave"); | |
3384 | } | |
ed3cf953 | 3385 | |
b655c310 NS |
3386 | /* Move one nesting level up. */ |
3387 | current_binding_level = scope->level_chain; | |
3388 | ||
3389 | /* Namespace-scopes are left most probably temporarily, not | |
3390 | completely; they can be reopened later, e.g. in namespace-extension | |
3391 | or any name binding activity that requires us to resume a | |
3392 | namespace. For classes, we cache some binding levels. For other | |
3393 | scopes, we just make the structure available for reuse. */ | |
3394 | if (scope->kind != sk_namespace | |
3395 | && scope->kind != sk_class) | |
00e8de68 | 3396 | { |
b655c310 NS |
3397 | scope->level_chain = free_binding_level; |
3398 | gcc_assert (!ENABLE_SCOPE_CHECKING | |
3399 | || scope->binding_depth == binding_depth); | |
3400 | free_binding_level = scope; | |
00e8de68 | 3401 | } |
b655c310 NS |
3402 | |
3403 | if (scope->kind == sk_class) | |
00e8de68 | 3404 | { |
b655c310 NS |
3405 | /* Reset DEFINING_CLASS_P to allow for reuse of a |
3406 | class-defining scope in a non-defining context. */ | |
3407 | scope->defining_class_p = 0; | |
3408 | ||
3409 | /* Find the innermost enclosing class scope, and reset | |
3410 | CLASS_BINDING_LEVEL appropriately. */ | |
3411 | class_binding_level = NULL; | |
3412 | for (scope = current_binding_level; scope; scope = scope->level_chain) | |
3413 | if (scope->kind == sk_class) | |
3414 | { | |
3415 | class_binding_level = scope; | |
3416 | break; | |
3417 | } | |
00e8de68 | 3418 | } |
b655c310 NS |
3419 | |
3420 | return current_binding_level; | |
00e8de68 | 3421 | } |
575bfb00 | 3422 | |
b655c310 NS |
3423 | static void |
3424 | resume_scope (cp_binding_level* b) | |
575bfb00 | 3425 | { |
b655c310 NS |
3426 | /* Resuming binding levels is meant only for namespaces, |
3427 | and those cannot nest into classes. */ | |
3428 | gcc_assert (!class_binding_level); | |
3429 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
3430 | gcc_assert (b->level_chain == current_binding_level); | |
3431 | current_binding_level = b; | |
3432 | if (ENABLE_SCOPE_CHECKING) | |
3433 | { | |
3434 | b->binding_depth = binding_depth; | |
3435 | indent (binding_depth); | |
3436 | cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); | |
3437 | binding_depth++; | |
3438 | } | |
575bfb00 LC |
3439 | } |
3440 | ||
b655c310 | 3441 | /* Return the innermost binding level that is not for a class scope. */ |
fde6f97e | 3442 | |
b655c310 NS |
3443 | static cp_binding_level * |
3444 | innermost_nonclass_level (void) | |
fde6f97e | 3445 | { |
b655c310 | 3446 | cp_binding_level *b; |
fde6f97e | 3447 | |
b655c310 NS |
3448 | b = current_binding_level; |
3449 | while (b->kind == sk_class) | |
3450 | b = b->level_chain; | |
fde6f97e | 3451 | |
b655c310 | 3452 | return b; |
fde6f97e | 3453 | } |
00e8de68 | 3454 | |
b655c310 NS |
3455 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
3456 | we're not allowed to add any more objects with cleanups to the current | |
3457 | scope, create a new binding level. */ | |
a5e6b29b | 3458 | |
b655c310 NS |
3459 | void |
3460 | maybe_push_cleanup_level (tree type) | |
a5e6b29b | 3461 | { |
b655c310 NS |
3462 | if (type != error_mark_node |
3463 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
3464 | && current_binding_level->more_cleanups_ok == 0) | |
a5e6b29b | 3465 | { |
b655c310 NS |
3466 | begin_scope (sk_cleanup, NULL); |
3467 | current_binding_level->statement_list = push_stmt_list (); | |
3468 | } | |
3469 | } | |
a5e6b29b | 3470 | |
b655c310 | 3471 | /* Return true if we are in the global binding level. */ |
a5e6b29b | 3472 | |
b655c310 NS |
3473 | bool |
3474 | global_bindings_p (void) | |
3475 | { | |
3476 | return global_scope_p (current_binding_level); | |
3477 | } | |
a5e6b29b | 3478 | |
b655c310 NS |
3479 | /* True if we are currently in a toplevel binding level. This |
3480 | means either the global binding level or a namespace in a toplevel | |
3481 | binding level. Since there are no non-toplevel namespace levels, | |
3482 | this really means any namespace or template parameter level. We | |
3483 | also include a class whose context is toplevel. */ | |
1a32490a | 3484 | |
b655c310 NS |
3485 | bool |
3486 | toplevel_bindings_p (void) | |
3487 | { | |
3488 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 3489 | |
b655c310 NS |
3490 | return b->kind == sk_namespace || b->kind == sk_template_parms; |
3491 | } | |
a5e6b29b | 3492 | |
b655c310 NS |
3493 | /* True if this is a namespace scope, or if we are defining a class |
3494 | which is itself at namespace scope, or whose enclosing class is | |
3495 | such a class, etc. */ | |
a5e6b29b | 3496 | |
b655c310 NS |
3497 | bool |
3498 | namespace_bindings_p (void) | |
3499 | { | |
3500 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 3501 | |
b655c310 NS |
3502 | return b->kind == sk_namespace; |
3503 | } | |
a5e6b29b | 3504 | |
b655c310 | 3505 | /* True if the innermost non-class scope is a block scope. */ |
a5e6b29b | 3506 | |
b655c310 NS |
3507 | bool |
3508 | local_bindings_p (void) | |
3509 | { | |
3510 | cp_binding_level *b = innermost_nonclass_level (); | |
3511 | return b->kind < sk_function_parms || b->kind == sk_omp; | |
3512 | } | |
a5e6b29b | 3513 | |
b655c310 | 3514 | /* True if the current level needs to have a BLOCK made. */ |
a5e6b29b | 3515 | |
b655c310 NS |
3516 | bool |
3517 | kept_level_p (void) | |
3518 | { | |
3519 | return (current_binding_level->blocks != NULL_TREE | |
3520 | || current_binding_level->keep | |
3521 | || current_binding_level->kind == sk_cleanup | |
3522 | || current_binding_level->names != NULL_TREE | |
3523 | || current_binding_level->using_directives); | |
575bfb00 LC |
3524 | } |
3525 | ||
b655c310 | 3526 | /* Returns the kind of the innermost scope. */ |
575bfb00 | 3527 | |
b655c310 NS |
3528 | scope_kind |
3529 | innermost_scope_kind (void) | |
575bfb00 | 3530 | { |
b655c310 | 3531 | return current_binding_level->kind; |
a5e6b29b GDR |
3532 | } |
3533 | ||
b655c310 | 3534 | /* Returns true if this scope was created to store template parameters. */ |
5a167978 | 3535 | |
b655c310 NS |
3536 | bool |
3537 | template_parm_scope_p (void) | |
5a167978 | 3538 | { |
b655c310 NS |
3539 | return innermost_scope_kind () == sk_template_parms; |
3540 | } | |
7756db03 | 3541 | |
b655c310 NS |
3542 | /* If KEEP is true, make a BLOCK node for the next binding level, |
3543 | unconditionally. Otherwise, use the normal logic to decide whether | |
3544 | or not to create a BLOCK. */ | |
5a167978 | 3545 | |
b655c310 NS |
3546 | void |
3547 | keep_next_level (bool keep) | |
3548 | { | |
3549 | keep_next_level_flag = keep; | |
3550 | } | |
5a167978 | 3551 | |
9c82d7b6 | 3552 | /* Return the list of declarations of the current local scope. */ |
5a167978 | 3553 | |
b655c310 | 3554 | tree |
9c82d7b6 | 3555 | get_local_decls (void) |
b655c310 | 3556 | { |
9c82d7b6 NS |
3557 | gcc_assert (current_binding_level->kind != sk_namespace |
3558 | && current_binding_level->kind != sk_class); | |
b655c310 NS |
3559 | return current_binding_level->names; |
3560 | } | |
5a167978 | 3561 | |
b655c310 | 3562 | /* Return how many function prototypes we are currently nested inside. */ |
5a167978 | 3563 | |
b655c310 NS |
3564 | int |
3565 | function_parm_depth (void) | |
3566 | { | |
3567 | int level = 0; | |
3568 | cp_binding_level *b; | |
8597cab1 | 3569 | |
b655c310 NS |
3570 | for (b = current_binding_level; |
3571 | b->kind == sk_function_parms; | |
3572 | b = b->level_chain) | |
3573 | ++level; | |
3574 | ||
3575 | return level; | |
5a167978 GDR |
3576 | } |
3577 | ||
b655c310 NS |
3578 | /* For debugging. */ |
3579 | static int no_print_functions = 0; | |
3580 | static int no_print_builtins = 0; | |
5a167978 GDR |
3581 | |
3582 | static void | |
b655c310 | 3583 | print_binding_level (cp_binding_level* lvl) |
5a167978 | 3584 | { |
b655c310 NS |
3585 | tree t; |
3586 | int i = 0, len; | |
fbfef506 JM |
3587 | if (lvl->this_entity) |
3588 | print_node_brief (stderr, "entity=", lvl->this_entity, 1); | |
b655c310 NS |
3589 | fprintf (stderr, " blocks=%p", (void *) lvl->blocks); |
3590 | if (lvl->more_cleanups_ok) | |
3591 | fprintf (stderr, " more-cleanups-ok"); | |
3592 | if (lvl->have_cleanups) | |
3593 | fprintf (stderr, " have-cleanups"); | |
3594 | fprintf (stderr, "\n"); | |
3595 | if (lvl->names) | |
3596 | { | |
3597 | fprintf (stderr, " names:\t"); | |
3598 | /* We can probably fit 3 names to a line? */ | |
3599 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
3600 | { | |
3601 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) | |
3602 | continue; | |
3603 | if (no_print_builtins | |
3604 | && (TREE_CODE (t) == TYPE_DECL) | |
3605 | && DECL_IS_BUILTIN (t)) | |
3606 | continue; | |
5a167978 | 3607 | |
b655c310 NS |
3608 | /* Function decls tend to have longer names. */ |
3609 | if (TREE_CODE (t) == FUNCTION_DECL) | |
3610 | len = 3; | |
3611 | else | |
3612 | len = 2; | |
3613 | i += len; | |
3614 | if (i > 6) | |
3615 | { | |
3616 | fprintf (stderr, "\n\t"); | |
3617 | i = len; | |
3618 | } | |
3619 | print_node_brief (stderr, "", t, 0); | |
3620 | if (t == error_mark_node) | |
3621 | break; | |
3622 | } | |
3623 | if (i) | |
3624 | fprintf (stderr, "\n"); | |
3625 | } | |
3626 | if (vec_safe_length (lvl->class_shadowed)) | |
5a167978 | 3627 | { |
b655c310 NS |
3628 | size_t i; |
3629 | cp_class_binding *b; | |
3630 | fprintf (stderr, " class-shadowed:"); | |
3631 | FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) | |
3632 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); | |
3633 | fprintf (stderr, "\n"); | |
5a167978 | 3634 | } |
b655c310 | 3635 | if (lvl->type_shadowed) |
44fd0e80 | 3636 | { |
b655c310 NS |
3637 | fprintf (stderr, " type-shadowed:"); |
3638 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
3639 | { | |
3640 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
3641 | } | |
3642 | fprintf (stderr, "\n"); | |
44fd0e80 | 3643 | } |
b655c310 | 3644 | } |
44fd0e80 | 3645 | |
b655c310 NS |
3646 | DEBUG_FUNCTION void |
3647 | debug (cp_binding_level &ref) | |
3648 | { | |
3649 | print_binding_level (&ref); | |
3650 | } | |
3651 | ||
3652 | DEBUG_FUNCTION void | |
3653 | debug (cp_binding_level *ptr) | |
3654 | { | |
3655 | if (ptr) | |
3656 | debug (*ptr); | |
3657 | else | |
3658 | fprintf (stderr, "<nil>\n"); | |
3659 | } | |
3660 | ||
3661 | ||
d4e15523 | 3662 | static void |
b655c310 NS |
3663 | print_other_binding_stack (cp_binding_level *stack) |
3664 | { | |
3665 | cp_binding_level *level; | |
3666 | for (level = stack; !global_scope_p (level); level = level->level_chain) | |
44fd0e80 | 3667 | { |
b655c310 NS |
3668 | fprintf (stderr, "binding level %p\n", (void *) level); |
3669 | print_binding_level (level); | |
44fd0e80 | 3670 | } |
b655c310 | 3671 | } |
44fd0e80 | 3672 | |
b655c310 NS |
3673 | void |
3674 | print_binding_stack (void) | |
3675 | { | |
3676 | cp_binding_level *b; | |
3677 | fprintf (stderr, "current_binding_level=%p\n" | |
3678 | "class_binding_level=%p\n" | |
3679 | "NAMESPACE_LEVEL (global_namespace)=%p\n", | |
3680 | (void *) current_binding_level, (void *) class_binding_level, | |
3681 | (void *) NAMESPACE_LEVEL (global_namespace)); | |
3682 | if (class_binding_level) | |
5a167978 | 3683 | { |
b655c310 NS |
3684 | for (b = class_binding_level; b; b = b->level_chain) |
3685 | if (b == current_binding_level) | |
3686 | break; | |
3687 | if (b) | |
3688 | b = class_binding_level; | |
3689 | else | |
3690 | b = current_binding_level; | |
3691 | } | |
3692 | else | |
3693 | b = current_binding_level; | |
3694 | print_other_binding_stack (b); | |
3695 | fprintf (stderr, "global:\n"); | |
3696 | print_binding_level (NAMESPACE_LEVEL (global_namespace)); | |
3697 | } | |
3698 | \f | |
3699 | /* Return the type associated with ID. */ | |
5a167978 | 3700 | |
b655c310 NS |
3701 | static tree |
3702 | identifier_type_value_1 (tree id) | |
3703 | { | |
3704 | /* There is no type with that name, anywhere. */ | |
3705 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
3706 | return NULL_TREE; | |
3707 | /* This is not the type marker, but the real thing. */ | |
3708 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
3709 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
3710 | /* Have to search for it. It must be on the global level, now. | |
3711 | Ask lookup_name not to return non-types. */ | |
3712 | id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); | |
3713 | if (id) | |
3714 | return TREE_TYPE (id); | |
3715 | return NULL_TREE; | |
3716 | } | |
44fd0e80 | 3717 | |
b655c310 | 3718 | /* Wrapper for identifier_type_value_1. */ |
1374a761 | 3719 | |
b655c310 NS |
3720 | tree |
3721 | identifier_type_value (tree id) | |
3722 | { | |
3723 | tree ret; | |
3724 | timevar_start (TV_NAME_LOOKUP); | |
3725 | ret = identifier_type_value_1 (id); | |
3726 | timevar_stop (TV_NAME_LOOKUP); | |
3727 | return ret; | |
3728 | } | |
44fd0e80 | 3729 | |
b655c310 NS |
3730 | /* Push a definition of struct, union or enum tag named ID. into |
3731 | binding_level B. DECL is a TYPE_DECL for the type. We assume that | |
3732 | the tag ID is not already defined. */ | |
1374a761 | 3733 | |
b655c310 NS |
3734 | static void |
3735 | set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) | |
3736 | { | |
3737 | tree type; | |
5a167978 | 3738 | |
b655c310 | 3739 | if (b->kind != sk_namespace) |
5a167978 | 3740 | { |
b655c310 NS |
3741 | /* Shadow the marker, not the real thing, so that the marker |
3742 | gets restored later. */ | |
3743 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
3744 | b->type_shadowed | |
3745 | = tree_cons (id, old_type_value, b->type_shadowed); | |
3746 | type = decl ? TREE_TYPE (decl) : NULL_TREE; | |
3747 | TREE_TYPE (b->type_shadowed) = type; | |
19831e2b OW |
3748 | } |
3749 | else | |
3750 | { | |
3c9cca88 NS |
3751 | tree *slot = find_namespace_slot (current_namespace, id, true); |
3752 | gcc_assert (decl); | |
3753 | update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false); | |
44fd0e80 | 3754 | |
b655c310 NS |
3755 | /* Store marker instead of real type. */ |
3756 | type = global_type_node; | |
3757 | } | |
3758 | SET_IDENTIFIER_TYPE_VALUE (id, type); | |
5a167978 GDR |
3759 | } |
3760 | ||
b655c310 NS |
3761 | /* As set_identifier_type_value_with_scope, but using |
3762 | current_binding_level. */ | |
5a167978 GDR |
3763 | |
3764 | void | |
b655c310 | 3765 | set_identifier_type_value (tree id, tree decl) |
5a167978 | 3766 | { |
b655c310 NS |
3767 | set_identifier_type_value_with_scope (id, decl, current_binding_level); |
3768 | } | |
5a167978 | 3769 | |
b655c310 | 3770 | /* Return the name for the constructor (or destructor) for the |
5fee5eca | 3771 | specified class. */ |
5a167978 | 3772 | |
b655c310 NS |
3773 | tree |
3774 | constructor_name (tree type) | |
3775 | { | |
56b2a94b NS |
3776 | tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type)); |
3777 | ||
3778 | return decl ? DECL_NAME (decl) : NULL_TREE; | |
b655c310 | 3779 | } |
5a167978 | 3780 | |
b655c310 NS |
3781 | /* Returns TRUE if NAME is the name for the constructor for TYPE, |
3782 | which must be a class type. */ | |
5a167978 | 3783 | |
b655c310 NS |
3784 | bool |
3785 | constructor_name_p (tree name, tree type) | |
3786 | { | |
b655c310 NS |
3787 | gcc_assert (MAYBE_CLASS_TYPE_P (type)); |
3788 | ||
b655c310 NS |
3789 | /* These don't have names. */ |
3790 | if (TREE_CODE (type) == DECLTYPE_TYPE | |
3791 | || TREE_CODE (type) == TYPEOF_TYPE) | |
3792 | return false; | |
3793 | ||
56b2a94b | 3794 | if (name && name == constructor_name (type)) |
b655c310 | 3795 | return true; |
5fee5eca | 3796 | |
b655c310 | 3797 | return false; |
5a167978 GDR |
3798 | } |
3799 | ||
b655c310 | 3800 | /* Counter used to create anonymous type names. */ |
5a167978 | 3801 | |
b655c310 NS |
3802 | static GTY(()) int anon_cnt; |
3803 | ||
3804 | /* Return an IDENTIFIER which can be used as a name for | |
3805 | unnamed structs and unions. */ | |
3806 | ||
3807 | tree | |
3808 | make_anon_name (void) | |
5a167978 | 3809 | { |
b655c310 NS |
3810 | char buf[32]; |
3811 | ||
3812 | sprintf (buf, anon_aggrname_format (), anon_cnt++); | |
3813 | return get_identifier (buf); | |
3814 | } | |
3815 | ||
3816 | /* This code is practically identical to that for creating | |
3817 | anonymous names, but is just used for lambdas instead. This isn't really | |
3818 | necessary, but it's convenient to avoid treating lambdas like other | |
3819 | unnamed types. */ | |
3820 | ||
3821 | static GTY(()) int lambda_cnt = 0; | |
3822 | ||
3823 | tree | |
3824 | make_lambda_name (void) | |
3825 | { | |
3826 | char buf[32]; | |
3827 | ||
3828 | sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); | |
3829 | return get_identifier (buf); | |
3830 | } | |
c8094d83 | 3831 | |
b655c310 NS |
3832 | /* Insert another USING_DECL into the current binding level, returning |
3833 | this declaration. If this is a redeclaration, do nothing, and | |
3834 | return NULL_TREE if this not in namespace scope (in namespace | |
3835 | scope, a using decl might extend any previous bindings). */ | |
87c465f5 | 3836 | |
b655c310 NS |
3837 | static tree |
3838 | push_using_decl_1 (tree scope, tree name) | |
87c465f5 | 3839 | { |
b655c310 | 3840 | tree decl; |
87c465f5 | 3841 | |
b655c310 NS |
3842 | gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); |
3843 | gcc_assert (identifier_p (name)); | |
3844 | for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) | |
3845 | if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) | |
3846 | break; | |
3847 | if (decl) | |
3848 | return namespace_bindings_p () ? decl : NULL_TREE; | |
3849 | decl = build_lang_decl (USING_DECL, name, NULL_TREE); | |
3850 | USING_DECL_SCOPE (decl) = scope; | |
3851 | DECL_CHAIN (decl) = current_binding_level->usings; | |
3852 | current_binding_level->usings = decl; | |
3853 | return decl; | |
87c465f5 KL |
3854 | } |
3855 | ||
b655c310 | 3856 | /* Wrapper for push_using_decl_1. */ |
87c465f5 | 3857 | |
b655c310 NS |
3858 | static tree |
3859 | push_using_decl (tree scope, tree name) | |
87c465f5 | 3860 | { |
b655c310 NS |
3861 | tree ret; |
3862 | timevar_start (TV_NAME_LOOKUP); | |
3863 | ret = push_using_decl_1 (scope, name); | |
3864 | timevar_stop (TV_NAME_LOOKUP); | |
3865 | return ret; | |
3866 | } | |
87c465f5 | 3867 | |
b655c310 NS |
3868 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
3869 | caller to set DECL_CONTEXT properly. | |
87c465f5 | 3870 | |
b655c310 NS |
3871 | Note that this must only be used when X will be the new innermost |
3872 | binding for its name, as we tack it onto the front of IDENTIFIER_BINDING | |
3873 | without checking to see if the current IDENTIFIER_BINDING comes from a | |
3874 | closer binding level than LEVEL. */ | |
87c465f5 | 3875 | |
b655c310 | 3876 | static tree |
5256a7f5 | 3877 | do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) |
b655c310 NS |
3878 | { |
3879 | cp_binding_level *b; | |
87c465f5 | 3880 | |
b655c310 NS |
3881 | if (level->kind == sk_class) |
3882 | { | |
3883 | b = class_binding_level; | |
3884 | class_binding_level = level; | |
3885 | pushdecl_class_level (x); | |
3886 | class_binding_level = b; | |
3887 | } | |
3888 | else | |
3889 | { | |
d04e6ed5 NS |
3890 | tree function_decl = current_function_decl; |
3891 | if (level->kind == sk_namespace) | |
3892 | current_function_decl = NULL_TREE; | |
b655c310 NS |
3893 | b = current_binding_level; |
3894 | current_binding_level = level; | |
4f15a5da | 3895 | x = pushdecl (x, is_friend); |
b655c310 | 3896 | current_binding_level = b; |
d04e6ed5 | 3897 | current_function_decl = function_decl; |
87c465f5 | 3898 | } |
b655c310 | 3899 | return x; |
87c465f5 | 3900 | } |
9e931c2a | 3901 | |
d16d5eac | 3902 | /* Inject X into the local scope just before the function parms. */ |
00e8de68 | 3903 | |
b655c310 | 3904 | tree |
d16d5eac | 3905 | pushdecl_outermost_localscope (tree x) |
00e8de68 | 3906 | { |
c8634a1a | 3907 | cp_binding_level *b = NULL; |
b655c310 | 3908 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
d16d5eac | 3909 | |
c8634a1a NS |
3910 | /* Find the scope just inside the function parms. */ |
3911 | for (cp_binding_level *n = current_binding_level; | |
3912 | n->kind != sk_function_parms; n = b->level_chain) | |
3913 | b = n; | |
3914 | ||
5256a7f5 | 3915 | tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node; |
b655c310 | 3916 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
c8634a1a | 3917 | |
b655c310 | 3918 | return ret; |
00e8de68 GDR |
3919 | } |
3920 | ||
b655c310 NS |
3921 | /* Check a non-member using-declaration. Return the name and scope |
3922 | being used, and the USING_DECL, or NULL_TREE on failure. */ | |
d2f2c87b | 3923 | |
b655c310 NS |
3924 | static tree |
3925 | validate_nonmember_using_decl (tree decl, tree scope, tree name) | |
3926 | { | |
3927 | /* [namespace.udecl] | |
3928 | A using-declaration for a class member shall be a | |
3929 | member-declaration. */ | |
3930 | if (TYPE_P (scope)) | |
90ea9897 | 3931 | { |
b655c310 NS |
3932 | error ("%qT is not a namespace or unscoped enum", scope); |
3933 | return NULL_TREE; | |
d2f2c87b | 3934 | } |
b655c310 NS |
3935 | else if (scope == error_mark_node) |
3936 | return NULL_TREE; | |
d2f2c87b | 3937 | |
b655c310 | 3938 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) |
90ea9897 | 3939 | { |
b655c310 NS |
3940 | /* 7.3.3/5 |
3941 | A using-declaration shall not name a template-id. */ | |
3942 | error ("a using-declaration cannot specify a template-id. " | |
3943 | "Try %<using %D%>", name); | |
3944 | return NULL_TREE; | |
90ea9897 MM |
3945 | } |
3946 | ||
b655c310 | 3947 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
00e8de68 | 3948 | { |
b655c310 NS |
3949 | error ("namespace %qD not allowed in using-declaration", decl); |
3950 | return NULL_TREE; | |
00e8de68 GDR |
3951 | } |
3952 | ||
b655c310 | 3953 | if (TREE_CODE (decl) == SCOPE_REF) |
90ea9897 | 3954 | { |
b655c310 NS |
3955 | /* It's a nested name with template parameter dependent scope. |
3956 | This can only be using-declaration for class member. */ | |
3957 | error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); | |
3958 | return NULL_TREE; | |
90ea9897 | 3959 | } |
00e8de68 | 3960 | |
9d029ddf | 3961 | decl = OVL_FIRST (decl); |
575bfb00 | 3962 | |
b655c310 NS |
3963 | /* Make a USING_DECL. */ |
3964 | tree using_decl = push_using_decl (scope, name); | |
575bfb00 | 3965 | |
b655c310 NS |
3966 | if (using_decl == NULL_TREE |
3967 | && at_function_scope_p () | |
3968 | && VAR_P (decl)) | |
3969 | /* C++11 7.3.3/10. */ | |
3970 | error ("%qD is already declared in this scope", name); | |
3971 | ||
3972 | return using_decl; | |
00e8de68 GDR |
3973 | } |
3974 | ||
9d029ddf NS |
3975 | /* Process a local-scope or namespace-scope using declaration. SCOPE |
3976 | is the nominated scope to search for NAME. VALUE_P and TYPE_P | |
3977 | point to the binding for NAME in the current scope and are | |
3978 | updated. */ | |
1d786913 | 3979 | |
b655c310 | 3980 | static void |
9d029ddf | 3981 | do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p) |
5a167978 | 3982 | { |
9dda0ace | 3983 | name_lookup lookup (name, 0); |
c8094d83 | 3984 | |
9dda0ace | 3985 | if (!qualified_namespace_lookup (scope, &lookup)) |
5a167978 | 3986 | { |
b655c310 NS |
3987 | error ("%qD not declared", name); |
3988 | return; | |
5a167978 | 3989 | } |
9d029ddf NS |
3990 | else if (TREE_CODE (lookup.value) == TREE_LIST) |
3991 | { | |
3992 | error ("reference to %qD is ambiguous", name); | |
3993 | print_candidates (lookup.value); | |
3994 | lookup.value = NULL_TREE; | |
3995 | } | |
3996 | ||
3997 | if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST) | |
3998 | { | |
3999 | error ("reference to %qD is ambiguous", name); | |
4000 | print_candidates (lookup.type); | |
4001 | lookup.type = NULL_TREE; | |
4002 | } | |
4003 | ||
4004 | tree value = *value_p; | |
4005 | tree type = *type_p; | |
98ed9dae | 4006 | |
b655c310 NS |
4007 | /* Shift the old and new bindings around so we're comparing class and |
4008 | enumeration names to each other. */ | |
9d029ddf | 4009 | if (value && DECL_IMPLICIT_TYPEDEF_P (value)) |
5a167978 | 4010 | { |
9d029ddf NS |
4011 | type = value; |
4012 | value = NULL_TREE; | |
98ed9dae | 4013 | } |
b655c310 | 4014 | |
9d029ddf | 4015 | if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)) |
140bec21 | 4016 | { |
9d029ddf NS |
4017 | lookup.type = lookup.value; |
4018 | lookup.value = NULL_TREE; | |
140bec21 | 4019 | } |
b655c310 | 4020 | |
9d029ddf | 4021 | if (lookup.value && lookup.value != value) |
98ed9dae | 4022 | { |
b655c310 | 4023 | /* Check for using functions. */ |
9d029ddf | 4024 | if (OVL_P (lookup.value) && (!value || OVL_P (value))) |
b655c310 | 4025 | { |
9d029ddf | 4026 | for (lkp_iterator usings (lookup.value); usings; ++usings) |
b655c310 | 4027 | { |
9d029ddf | 4028 | tree new_fn = *usings; |
577b02d8 | 4029 | |
b655c310 | 4030 | /* [namespace.udecl] |
c8094d83 | 4031 | |
b655c310 NS |
4032 | If a function declaration in namespace scope or block |
4033 | scope has the same name and the same parameter types as a | |
4034 | function introduced by a using declaration the program is | |
4035 | ill-formed. */ | |
9d029ddf NS |
4036 | bool found = false; |
4037 | for (ovl_iterator old (value); !found && old; ++old) | |
b655c310 | 4038 | { |
9d029ddf | 4039 | tree old_fn = *old; |
3db45ab5 | 4040 | |
b655c310 | 4041 | if (new_fn == old_fn) |
9d029ddf NS |
4042 | /* The function already exists in the current |
4043 | namespace. */ | |
4044 | found = true; | |
4045 | else if (old.using_p ()) | |
4046 | continue; /* This is a using decl. */ | |
ef4c5e78 | 4047 | else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn)) |
9d029ddf NS |
4048 | continue; /* This is an anticipated builtin. */ |
4049 | else if (!matching_fn_p (new_fn, old_fn)) | |
4050 | continue; /* Parameters do not match. */ | |
4051 | else if (decls_match (new_fn, old_fn)) | |
4052 | found = true; | |
4053 | else | |
b655c310 | 4054 | { |
9d029ddf NS |
4055 | diagnose_name_conflict (new_fn, old_fn); |
4056 | found = true; | |
b655c310 NS |
4057 | } |
4058 | } | |
1e9f5818 | 4059 | |
9d029ddf NS |
4060 | if (!found) |
4061 | /* Unlike the overload case we don't drop anticipated | |
4062 | builtins here. They don't cause a problem, and | |
4063 | we'd like to match them with a future | |
4064 | declaration. */ | |
4065 | value = ovl_insert (new_fn, value, true); | |
b01e6d2b | 4066 | } |
98ed9dae | 4067 | } |
9d029ddf NS |
4068 | else if (value |
4069 | /* Ignore anticipated builtins. */ | |
ef4c5e78 | 4070 | && !anticipated_builtin_p (value) |
9d029ddf NS |
4071 | && !decls_match (lookup.value, value)) |
4072 | diagnose_name_conflict (lookup.value, value); | |
b655c310 | 4073 | else |
9d029ddf | 4074 | value = lookup.value; |
b655c310 NS |
4075 | } |
4076 | ||
9d029ddf | 4077 | if (lookup.type && lookup.type != type) |
b655c310 | 4078 | { |
9d029ddf NS |
4079 | if (type && !decls_match (lookup.type, type)) |
4080 | diagnose_name_conflict (lookup.type, type); | |
b655c310 | 4081 | else |
9d029ddf | 4082 | type = lookup.type; |
b655c310 | 4083 | } |
9d029ddf NS |
4084 | |
4085 | /* If bind->value is empty, shift any class or enumeration name back. */ | |
4086 | if (!value) | |
b655c310 | 4087 | { |
9d029ddf NS |
4088 | value = type; |
4089 | type = NULL_TREE; | |
1e9f5818 | 4090 | } |
98ed9dae | 4091 | |
9d029ddf NS |
4092 | *value_p = value; |
4093 | *type_p = type; | |
5a167978 GDR |
4094 | } |
4095 | ||
322763f5 NS |
4096 | /* Returns true if ANCESTOR encloses DESCENDANT, including matching. |
4097 | Both are namespaces. */ | |
4098 | ||
4099 | bool | |
4100 | is_nested_namespace (tree ancestor, tree descendant, bool inline_only) | |
4101 | { | |
4102 | int depth = SCOPE_DEPTH (ancestor); | |
4103 | ||
4104 | if (!depth && !inline_only) | |
4105 | /* The global namespace encloses everything. */ | |
4106 | return true; | |
4107 | ||
4108 | while (SCOPE_DEPTH (descendant) > depth | |
4109 | && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant))) | |
4110 | descendant = CP_DECL_CONTEXT (descendant); | |
4111 | ||
4112 | return ancestor == descendant; | |
4113 | } | |
4114 | ||
b655c310 NS |
4115 | /* Returns true if ROOT (a namespace, class, or function) encloses |
4116 | CHILD. CHILD may be either a class type or a namespace. */ | |
575bfb00 | 4117 | |
b655c310 NS |
4118 | bool |
4119 | is_ancestor (tree root, tree child) | |
00e8de68 | 4120 | { |
b655c310 NS |
4121 | gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL |
4122 | || TREE_CODE (root) == FUNCTION_DECL | |
4123 | || CLASS_TYPE_P (root))); | |
4124 | gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL | |
4125 | || CLASS_TYPE_P (child))); | |
00e8de68 | 4126 | |
b655c310 NS |
4127 | /* The global namespace encloses everything. */ |
4128 | if (root == global_namespace) | |
4129 | return true; | |
00e8de68 | 4130 | |
322763f5 NS |
4131 | /* Search until we reach namespace scope. */ |
4132 | while (TREE_CODE (child) != NAMESPACE_DECL) | |
b655c310 | 4133 | { |
b655c310 NS |
4134 | /* If we've reached the ROOT, it encloses CHILD. */ |
4135 | if (root == child) | |
4136 | return true; | |
4137 | /* Go out one level. */ | |
4138 | if (TYPE_P (child)) | |
4139 | child = TYPE_NAME (child); | |
322763f5 | 4140 | child = CP_DECL_CONTEXT (child); |
b655c310 | 4141 | } |
322763f5 NS |
4142 | |
4143 | if (TREE_CODE (root) == NAMESPACE_DECL) | |
4144 | return is_nested_namespace (root, child); | |
4145 | ||
4146 | return false; | |
575bfb00 LC |
4147 | } |
4148 | ||
b655c310 NS |
4149 | /* Enter the class or namespace scope indicated by T suitable for name |
4150 | lookup. T can be arbitrary scope, not necessary nested inside the | |
4151 | current scope. Returns a non-null scope to pop iff pop_scope | |
4152 | should be called later to exit this scope. */ | |
ed3cf953 | 4153 | |
b655c310 NS |
4154 | tree |
4155 | push_scope (tree t) | |
ed3cf953 | 4156 | { |
b655c310 NS |
4157 | if (TREE_CODE (t) == NAMESPACE_DECL) |
4158 | push_decl_namespace (t); | |
4159 | else if (CLASS_TYPE_P (t)) | |
4160 | { | |
4161 | if (!at_class_scope_p () | |
4162 | || !same_type_p (current_class_type, t)) | |
4163 | push_nested_class (t); | |
4164 | else | |
4165 | /* T is the same as the current scope. There is therefore no | |
4166 | need to re-enter the scope. Since we are not actually | |
4167 | pushing a new scope, our caller should not call | |
4168 | pop_scope. */ | |
4169 | t = NULL_TREE; | |
4170 | } | |
ed3cf953 | 4171 | |
b655c310 | 4172 | return t; |
575bfb00 LC |
4173 | } |
4174 | ||
b655c310 | 4175 | /* Leave scope pushed by push_scope. */ |
575bfb00 LC |
4176 | |
4177 | void | |
b655c310 | 4178 | pop_scope (tree t) |
575bfb00 | 4179 | { |
b655c310 NS |
4180 | if (t == NULL_TREE) |
4181 | return; | |
4182 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
4183 | pop_decl_namespace (); | |
4184 | else if CLASS_TYPE_P (t) | |
4185 | pop_nested_class (); | |
ed3cf953 GDR |
4186 | } |
4187 | ||
b655c310 | 4188 | /* Subroutine of push_inner_scope. */ |
5a167978 | 4189 | |
b655c310 NS |
4190 | static void |
4191 | push_inner_scope_r (tree outer, tree inner) | |
5a167978 | 4192 | { |
b655c310 | 4193 | tree prev; |
5ae9ba3e | 4194 | |
b655c310 NS |
4195 | if (outer == inner |
4196 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
abc088aa | 4197 | return; |
b655c310 NS |
4198 | |
4199 | prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
4200 | if (outer != prev) | |
4201 | push_inner_scope_r (outer, prev); | |
4202 | if (TREE_CODE (inner) == NAMESPACE_DECL) | |
5ae9ba3e | 4203 | { |
b655c310 NS |
4204 | cp_binding_level *save_template_parm = 0; |
4205 | /* Temporary take out template parameter scopes. They are saved | |
4206 | in reversed order in save_template_parm. */ | |
4207 | while (current_binding_level->kind == sk_template_parms) | |
160594b0 | 4208 | { |
b655c310 NS |
4209 | cp_binding_level *b = current_binding_level; |
4210 | current_binding_level = b->level_chain; | |
4211 | b->level_chain = save_template_parm; | |
4212 | save_template_parm = b; | |
160594b0 | 4213 | } |
b655c310 NS |
4214 | |
4215 | resume_scope (NAMESPACE_LEVEL (inner)); | |
4216 | current_namespace = inner; | |
4217 | ||
4218 | /* Restore template parameter scopes. */ | |
4219 | while (save_template_parm) | |
160594b0 | 4220 | { |
b655c310 NS |
4221 | cp_binding_level *b = save_template_parm; |
4222 | save_template_parm = b->level_chain; | |
4223 | b->level_chain = current_binding_level; | |
4224 | current_binding_level = b; | |
160594b0 | 4225 | } |
5ae9ba3e | 4226 | } |
b655c310 NS |
4227 | else |
4228 | pushclass (inner); | |
c8094d83 | 4229 | } |
5a167978 | 4230 | |
b655c310 NS |
4231 | /* Enter the scope INNER from current scope. INNER must be a scope |
4232 | nested inside current scope. This works with both name lookup and | |
4233 | pushing name into scope. In case a template parameter scope is present, | |
4234 | namespace is pushed under the template parameter scope according to | |
4235 | name lookup rule in 14.6.1/6. | |
4236 | ||
4237 | Return the former current scope suitable for pop_inner_scope. */ | |
5a167978 | 4238 | |
ae099258 | 4239 | tree |
b655c310 | 4240 | push_inner_scope (tree inner) |
5a167978 | 4241 | { |
b655c310 NS |
4242 | tree outer = current_scope (); |
4243 | if (!outer) | |
4244 | outer = current_namespace; | |
5a167978 | 4245 | |
b655c310 NS |
4246 | push_inner_scope_r (outer, inner); |
4247 | return outer; | |
5a167978 GDR |
4248 | } |
4249 | ||
b655c310 | 4250 | /* Exit the current scope INNER back to scope OUTER. */ |
00e8de68 | 4251 | |
b655c310 NS |
4252 | void |
4253 | pop_inner_scope (tree outer, tree inner) | |
0ed5edac | 4254 | { |
b655c310 NS |
4255 | if (outer == inner |
4256 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
4257 | return; | |
0ed5edac | 4258 | |
b655c310 | 4259 | while (outer != inner) |
65567efa | 4260 | { |
b655c310 | 4261 | if (TREE_CODE (inner) == NAMESPACE_DECL) |
65567efa | 4262 | { |
b655c310 NS |
4263 | cp_binding_level *save_template_parm = 0; |
4264 | /* Temporary take out template parameter scopes. They are saved | |
4265 | in reversed order in save_template_parm. */ | |
4266 | while (current_binding_level->kind == sk_template_parms) | |
65567efa | 4267 | { |
b655c310 NS |
4268 | cp_binding_level *b = current_binding_level; |
4269 | current_binding_level = b->level_chain; | |
4270 | b->level_chain = save_template_parm; | |
4271 | save_template_parm = b; | |
65567efa JM |
4272 | } |
4273 | ||
b655c310 | 4274 | pop_namespace (); |
65567efa | 4275 | |
b655c310 NS |
4276 | /* Restore template parameter scopes. */ |
4277 | while (save_template_parm) | |
7cb73573 | 4278 | { |
b655c310 NS |
4279 | cp_binding_level *b = save_template_parm; |
4280 | save_template_parm = b->level_chain; | |
4281 | b->level_chain = current_binding_level; | |
4282 | current_binding_level = b; | |
7cb73573 | 4283 | } |
e3501bab | 4284 | } |
65567efa | 4285 | else |
b655c310 NS |
4286 | popclass (); |
4287 | ||
4288 | inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
65567efa | 4289 | } |
b655c310 NS |
4290 | } |
4291 | \f | |
4292 | /* Do a pushlevel for class declarations. */ | |
65567efa | 4293 | |
b655c310 NS |
4294 | void |
4295 | pushlevel_class (void) | |
4296 | { | |
4297 | class_binding_level = begin_scope (sk_class, current_class_type); | |
65567efa | 4298 | } |
0ed5edac | 4299 | |
b655c310 NS |
4300 | /* ...and a poplevel for class declarations. */ |
4301 | ||
4302 | void | |
4303 | poplevel_class (void) | |
00e8de68 | 4304 | { |
b655c310 NS |
4305 | cp_binding_level *level = class_binding_level; |
4306 | cp_class_binding *cb; | |
4307 | size_t i; | |
4308 | tree shadowed; | |
00e8de68 | 4309 | |
575bfb00 | 4310 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 4311 | gcc_assert (level != 0); |
c8094d83 | 4312 | |
b655c310 NS |
4313 | /* If we're leaving a toplevel class, cache its binding level. */ |
4314 | if (current_class_depth == 1) | |
4315 | previous_class_level = level; | |
4316 | for (shadowed = level->type_shadowed; | |
4317 | shadowed; | |
4318 | shadowed = TREE_CHAIN (shadowed)) | |
4319 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); | |
00e8de68 | 4320 | |
b655c310 NS |
4321 | /* Remove the bindings for all of the class-level declarations. */ |
4322 | if (level->class_shadowed) | |
00e8de68 | 4323 | { |
b655c310 | 4324 | FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) |
0cbd7506 | 4325 | { |
b655c310 NS |
4326 | IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; |
4327 | cxx_binding_free (cb->base); | |
0cbd7506 | 4328 | } |
b655c310 NS |
4329 | ggc_free (level->class_shadowed); |
4330 | level->class_shadowed = NULL; | |
00e8de68 GDR |
4331 | } |
4332 | ||
b655c310 NS |
4333 | /* Now, pop out of the binding level which we created up in the |
4334 | `pushlevel_class' routine. */ | |
4335 | gcc_assert (current_binding_level == level); | |
4336 | leave_scope (); | |
4337 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
4338 | } | |
4339 | ||
4340 | /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as | |
4341 | appropriate. DECL is the value to which a name has just been | |
4342 | bound. CLASS_TYPE is the class in which the lookup occurred. */ | |
4343 | ||
4344 | static void | |
4345 | set_inherited_value_binding_p (cxx_binding *binding, tree decl, | |
4346 | tree class_type) | |
4347 | { | |
4348 | if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) | |
00e8de68 | 4349 | { |
b655c310 NS |
4350 | tree context; |
4351 | ||
4352 | if (TREE_CODE (decl) == OVERLOAD) | |
4353 | context = ovl_scope (decl); | |
b9e75696 | 4354 | else |
ed36980c | 4355 | { |
b655c310 NS |
4356 | gcc_assert (DECL_P (decl)); |
4357 | context = context_for_name_lookup (decl); | |
ed36980c | 4358 | } |
00e8de68 | 4359 | |
b655c310 NS |
4360 | if (is_properly_derived_from (class_type, context)) |
4361 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
4362 | else | |
4363 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4364 | } | |
4365 | else if (binding->value == decl) | |
4366 | /* We only encounter a TREE_LIST when there is an ambiguity in the | |
4367 | base classes. Such an ambiguity can be overridden by a | |
4368 | definition in this class. */ | |
4369 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
4370 | else | |
4371 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
00e8de68 GDR |
4372 | } |
4373 | ||
b655c310 | 4374 | /* Make the declaration of X appear in CLASS scope. */ |
00e8de68 | 4375 | |
b655c310 NS |
4376 | bool |
4377 | pushdecl_class_level (tree x) | |
00e8de68 | 4378 | { |
b655c310 NS |
4379 | bool is_valid = true; |
4380 | bool subtime; | |
00e8de68 | 4381 | |
b655c310 NS |
4382 | /* Do nothing if we're adding to an outer lambda closure type, |
4383 | outer_binding will add it later if it's needed. */ | |
4384 | if (current_class_type != class_binding_level->this_entity) | |
4385 | return true; | |
00e8de68 | 4386 | |
b655c310 NS |
4387 | subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4388 | /* Get the name of X. */ | |
848bf88d | 4389 | tree name = OVL_NAME (x); |
00e8de68 | 4390 | |
b655c310 | 4391 | if (name) |
00e8de68 | 4392 | { |
b655c310 NS |
4393 | is_valid = push_class_level_binding (name, x); |
4394 | if (TREE_CODE (x) == TYPE_DECL) | |
4395 | set_identifier_type_value (name, x); | |
00e8de68 | 4396 | } |
b655c310 NS |
4397 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
4398 | { | |
4399 | /* If X is an anonymous aggregate, all of its members are | |
4400 | treated as if they were members of the class containing the | |
4401 | aggregate, for naming purposes. */ | |
6f87580f NS |
4402 | location_t save_location = input_location; |
4403 | tree anon = TREE_TYPE (x); | |
4404 | if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon)) | |
4405 | for (unsigned ix = member_vec->length (); ix--;) | |
4406 | { | |
4407 | tree binding = (*member_vec)[ix]; | |
4408 | if (STAT_HACK_P (binding)) | |
4409 | { | |
4410 | if (!pushdecl_class_level (STAT_TYPE (binding))) | |
4411 | is_valid = false; | |
4412 | binding = STAT_DECL (binding); | |
4413 | } | |
4414 | if (!pushdecl_class_level (binding)) | |
4415 | is_valid = false; | |
b655c310 | 4416 | } |
6f87580f NS |
4417 | else |
4418 | for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f)) | |
4419 | if (TREE_CODE (f) == FIELD_DECL) | |
4420 | { | |
4421 | input_location = DECL_SOURCE_LOCATION (f); | |
4422 | if (!pushdecl_class_level (f)) | |
4423 | is_valid = false; | |
4424 | } | |
4425 | input_location = save_location; | |
b655c310 | 4426 | } |
575bfb00 | 4427 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 4428 | return is_valid; |
00e8de68 GDR |
4429 | } |
4430 | ||
b655c310 NS |
4431 | /* Return the BINDING (if any) for NAME in SCOPE, which is a class |
4432 | scope. If the value returned is non-NULL, and the PREVIOUS field | |
4433 | is not set, callers must set the PREVIOUS field explicitly. */ | |
5a167978 | 4434 | |
b655c310 NS |
4435 | static cxx_binding * |
4436 | get_class_binding (tree name, cp_binding_level *scope) | |
5a167978 | 4437 | { |
b655c310 NS |
4438 | tree class_type; |
4439 | tree type_binding; | |
4440 | tree value_binding; | |
4441 | cxx_binding *binding; | |
5a167978 | 4442 | |
b655c310 | 4443 | class_type = scope->this_entity; |
5a167978 | 4444 | |
b655c310 NS |
4445 | /* Get the type binding. */ |
4446 | type_binding = lookup_member (class_type, name, | |
4447 | /*protect=*/2, /*want_type=*/true, | |
4448 | tf_warning_or_error); | |
4449 | /* Get the value binding. */ | |
4450 | value_binding = lookup_member (class_type, name, | |
4451 | /*protect=*/2, /*want_type=*/false, | |
4452 | tf_warning_or_error); | |
5a167978 | 4453 | |
b655c310 NS |
4454 | if (value_binding |
4455 | && (TREE_CODE (value_binding) == TYPE_DECL | |
4456 | || DECL_CLASS_TEMPLATE_P (value_binding) | |
4457 | || (TREE_CODE (value_binding) == TREE_LIST | |
4458 | && TREE_TYPE (value_binding) == error_mark_node | |
4459 | && (TREE_CODE (TREE_VALUE (value_binding)) | |
4460 | == TYPE_DECL)))) | |
4461 | /* We found a type binding, even when looking for a non-type | |
4462 | binding. This means that we already processed this binding | |
4463 | above. */ | |
4464 | ; | |
4465 | else if (value_binding) | |
4466 | { | |
4467 | if (TREE_CODE (value_binding) == TREE_LIST | |
4468 | && TREE_TYPE (value_binding) == error_mark_node) | |
4469 | /* NAME is ambiguous. */ | |
4470 | ; | |
4471 | else if (BASELINK_P (value_binding)) | |
4472 | /* NAME is some overloaded functions. */ | |
4473 | value_binding = BASELINK_FUNCTIONS (value_binding); | |
4474 | } | |
5a167978 | 4475 | |
b655c310 NS |
4476 | /* If we found either a type binding or a value binding, create a |
4477 | new binding object. */ | |
4478 | if (type_binding || value_binding) | |
4479 | { | |
4480 | binding = new_class_binding (name, | |
4481 | value_binding, | |
4482 | type_binding, | |
4483 | scope); | |
4484 | /* This is a class-scope binding, not a block-scope binding. */ | |
4485 | LOCAL_BINDING_P (binding) = 0; | |
4486 | set_inherited_value_binding_p (binding, value_binding, class_type); | |
4487 | } | |
575bfb00 | 4488 | else |
b655c310 NS |
4489 | binding = NULL; |
4490 | ||
4491 | return binding; | |
575bfb00 LC |
4492 | } |
4493 | ||
b655c310 NS |
4494 | /* Make the declaration(s) of X appear in CLASS scope under the name |
4495 | NAME. Returns true if the binding is valid. */ | |
575bfb00 | 4496 | |
b655c310 NS |
4497 | static bool |
4498 | push_class_level_binding_1 (tree name, tree x) | |
575bfb00 | 4499 | { |
b655c310 NS |
4500 | cxx_binding *binding; |
4501 | tree decl = x; | |
4502 | bool ok; | |
5a167978 | 4503 | |
b655c310 NS |
4504 | /* The class_binding_level will be NULL if x is a template |
4505 | parameter name in a member template. */ | |
4506 | if (!class_binding_level) | |
4507 | return true; | |
5a167978 | 4508 | |
b655c310 NS |
4509 | if (name == error_mark_node) |
4510 | return false; | |
166206ce | 4511 | |
b655c310 NS |
4512 | /* Can happen for an erroneous declaration (c++/60384). */ |
4513 | if (!identifier_p (name)) | |
4514 | { | |
4515 | gcc_assert (errorcount || sorrycount); | |
4516 | return false; | |
4517 | } | |
5a167978 | 4518 | |
b655c310 NS |
4519 | /* Check for invalid member names. But don't worry about a default |
4520 | argument-scope lambda being pushed after the class is complete. */ | |
4521 | gcc_assert (TYPE_BEING_DEFINED (current_class_type) | |
4522 | || LAMBDA_TYPE_P (TREE_TYPE (decl))); | |
4523 | /* Check that we're pushing into the right binding level. */ | |
4524 | gcc_assert (current_class_type == class_binding_level->this_entity); | |
5a167978 | 4525 | |
b655c310 NS |
4526 | /* We could have been passed a tree list if this is an ambiguous |
4527 | declaration. If so, pull the declaration out because | |
4528 | check_template_shadow will not handle a TREE_LIST. */ | |
4529 | if (TREE_CODE (decl) == TREE_LIST | |
4530 | && TREE_TYPE (decl) == error_mark_node) | |
4531 | decl = TREE_VALUE (decl); | |
6097b0c3 | 4532 | |
b655c310 NS |
4533 | if (!check_template_shadow (decl)) |
4534 | return false; | |
5a167978 | 4535 | |
b655c310 | 4536 | /* [class.mem] |
00e8de68 | 4537 | |
b655c310 NS |
4538 | If T is the name of a class, then each of the following shall |
4539 | have a name different from T: | |
00e8de68 | 4540 | |
b655c310 | 4541 | -- every static data member of class T; |
00e8de68 | 4542 | |
b655c310 NS |
4543 | -- every member of class T that is itself a type; |
4544 | ||
4545 | -- every enumerator of every member of class T that is an | |
4546 | enumerated type; | |
4547 | ||
4548 | -- every member of every anonymous union that is a member of | |
4549 | class T. | |
4550 | ||
4551 | (Non-static data members were also forbidden to have the same | |
4552 | name as T until TC1.) */ | |
4553 | if ((VAR_P (x) | |
4554 | || TREE_CODE (x) == CONST_DECL | |
4555 | || (TREE_CODE (x) == TYPE_DECL | |
4556 | && !DECL_SELF_REFERENCE_P (x)) | |
4557 | /* A data member of an anonymous union. */ | |
4558 | || (TREE_CODE (x) == FIELD_DECL | |
4559 | && DECL_CONTEXT (x) != current_class_type)) | |
56b2a94b | 4560 | && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type))) |
b655c310 NS |
4561 | { |
4562 | tree scope = context_for_name_lookup (x); | |
4563 | if (TYPE_P (scope) && same_type_p (scope, current_class_type)) | |
4564 | { | |
4565 | error ("%qD has the same name as the class in which it is " | |
4566 | "declared", | |
4567 | x); | |
4568 | return false; | |
4569 | } | |
4570 | } | |
4571 | ||
4572 | /* Get the current binding for NAME in this class, if any. */ | |
4573 | binding = IDENTIFIER_BINDING (name); | |
4574 | if (!binding || binding->scope != class_binding_level) | |
00e8de68 | 4575 | { |
b655c310 NS |
4576 | binding = get_class_binding (name, class_binding_level); |
4577 | /* If a new binding was created, put it at the front of the | |
4578 | IDENTIFIER_BINDING list. */ | |
4579 | if (binding) | |
0cbd7506 | 4580 | { |
b655c310 NS |
4581 | binding->previous = IDENTIFIER_BINDING (name); |
4582 | IDENTIFIER_BINDING (name) = binding; | |
0cbd7506 | 4583 | } |
b655c310 NS |
4584 | } |
4585 | ||
4586 | /* If there is already a binding, then we may need to update the | |
4587 | current value. */ | |
4588 | if (binding && binding->value) | |
4589 | { | |
4590 | tree bval = binding->value; | |
4591 | tree old_decl = NULL_TREE; | |
4592 | tree target_decl = strip_using_decl (decl); | |
4593 | tree target_bval = strip_using_decl (bval); | |
4594 | ||
4595 | if (INHERITED_VALUE_BINDING_P (binding)) | |
0cbd7506 | 4596 | { |
b655c310 NS |
4597 | /* If the old binding was from a base class, and was for a |
4598 | tag name, slide it over to make room for the new binding. | |
4599 | The old binding is still visible if explicitly qualified | |
4600 | with a class-key. */ | |
4601 | if (TREE_CODE (target_bval) == TYPE_DECL | |
4602 | && DECL_ARTIFICIAL (target_bval) | |
4603 | && !(TREE_CODE (target_decl) == TYPE_DECL | |
4604 | && DECL_ARTIFICIAL (target_decl))) | |
4605 | { | |
4606 | old_decl = binding->type; | |
4607 | binding->type = bval; | |
4608 | binding->value = NULL_TREE; | |
4609 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4610 | } | |
4611 | else | |
4612 | { | |
4613 | old_decl = bval; | |
4614 | /* Any inherited type declaration is hidden by the type | |
4615 | declaration in the derived class. */ | |
4616 | if (TREE_CODE (target_decl) == TYPE_DECL | |
4617 | && DECL_ARTIFICIAL (target_decl)) | |
4618 | binding->type = NULL_TREE; | |
4619 | } | |
00e8de68 | 4620 | } |
b655c310 | 4621 | else if (TREE_CODE (target_decl) == OVERLOAD |
5256a7f5 | 4622 | && OVL_P (target_bval)) |
b655c310 NS |
4623 | old_decl = bval; |
4624 | else if (TREE_CODE (decl) == USING_DECL | |
4625 | && TREE_CODE (bval) == USING_DECL | |
4626 | && same_type_p (USING_DECL_SCOPE (decl), | |
4627 | USING_DECL_SCOPE (bval))) | |
4628 | /* This is a using redeclaration that will be diagnosed later | |
4629 | in supplement_binding */ | |
4630 | ; | |
4631 | else if (TREE_CODE (decl) == USING_DECL | |
4632 | && TREE_CODE (bval) == USING_DECL | |
4633 | && DECL_DEPENDENT_P (decl) | |
4634 | && DECL_DEPENDENT_P (bval)) | |
4635 | return true; | |
4636 | else if (TREE_CODE (decl) == USING_DECL | |
5256a7f5 | 4637 | && OVL_P (target_bval)) |
b655c310 NS |
4638 | old_decl = bval; |
4639 | else if (TREE_CODE (bval) == USING_DECL | |
5256a7f5 | 4640 | && OVL_P (target_decl)) |
b655c310 NS |
4641 | return true; |
4642 | ||
4643 | if (old_decl && binding->scope == class_binding_level) | |
0cbd7506 | 4644 | { |
b655c310 NS |
4645 | binding->value = x; |
4646 | /* It is always safe to clear INHERITED_VALUE_BINDING_P | |
4647 | here. This function is only used to register bindings | |
4648 | from with the class definition itself. */ | |
4649 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4650 | return true; | |
0cbd7506 | 4651 | } |
00e8de68 | 4652 | } |
00e8de68 | 4653 | |
b655c310 NS |
4654 | /* Note that we declared this value so that we can issue an error if |
4655 | this is an invalid redeclaration of a name already used for some | |
4656 | other purpose. */ | |
4657 | note_name_declared_in_class (name, decl); | |
575bfb00 | 4658 | |
b655c310 NS |
4659 | /* If we didn't replace an existing binding, put the binding on the |
4660 | stack of bindings for the identifier, and update the shadowed | |
4661 | list. */ | |
4662 | if (binding && binding->scope == class_binding_level) | |
4663 | /* Supplement the existing binding. */ | |
4664 | ok = supplement_binding (binding, decl); | |
4665 | else | |
5a167978 | 4666 | { |
b655c310 NS |
4667 | /* Create a new binding. */ |
4668 | push_binding (name, decl, class_binding_level); | |
4669 | ok = true; | |
5a167978 GDR |
4670 | } |
4671 | ||
b655c310 | 4672 | return ok; |
575bfb00 LC |
4673 | } |
4674 | ||
b655c310 | 4675 | /* Wrapper for push_class_level_binding_1. */ |
575bfb00 | 4676 | |
b655c310 NS |
4677 | bool |
4678 | push_class_level_binding (tree name, tree x) | |
575bfb00 | 4679 | { |
b655c310 | 4680 | bool ret; |
575bfb00 | 4681 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 4682 | ret = push_class_level_binding_1 (name, x); |
575bfb00 | 4683 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 4684 | return ret; |
5a167978 GDR |
4685 | } |
4686 | ||
b655c310 NS |
4687 | /* Process "using SCOPE::NAME" in a class scope. Return the |
4688 | USING_DECL created. */ | |
5a167978 | 4689 | |
b655c310 NS |
4690 | tree |
4691 | do_class_using_decl (tree scope, tree name) | |
5a167978 | 4692 | { |
b655c310 NS |
4693 | if (name == error_mark_node) |
4694 | return NULL_TREE; | |
166206ce | 4695 | |
b655c310 NS |
4696 | if (!scope || !TYPE_P (scope)) |
4697 | { | |
4698 | error ("using-declaration for non-member at class scope"); | |
4699 | return NULL_TREE; | |
4700 | } | |
166206ce | 4701 | |
b655c310 NS |
4702 | /* Make sure the name is not invalid */ |
4703 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6097b0c3 | 4704 | { |
b655c310 NS |
4705 | error ("%<%T::%D%> names destructor", scope, name); |
4706 | return NULL_TREE; | |
6097b0c3 | 4707 | } |
0c29f2a2 | 4708 | |
b655c310 NS |
4709 | /* Using T::T declares inheriting ctors, even if T is a typedef. */ |
4710 | if (MAYBE_CLASS_TYPE_P (scope) | |
4711 | && (name == TYPE_IDENTIFIER (scope) | |
4712 | || constructor_name_p (name, scope))) | |
6097b0c3 | 4713 | { |
b655c310 NS |
4714 | maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); |
4715 | name = ctor_identifier; | |
4716 | CLASSTYPE_NON_AGGREGATE (current_class_type) = true; | |
d19c0f4b | 4717 | } |
0c29f2a2 NS |
4718 | |
4719 | /* Cannot introduce a constructor name. */ | |
b655c310 NS |
4720 | if (constructor_name_p (name, current_class_type)) |
4721 | { | |
4722 | error ("%<%T::%D%> names constructor in %qT", | |
4723 | scope, name, current_class_type); | |
4724 | return NULL_TREE; | |
4725 | } | |
4726 | ||
b655c310 | 4727 | /* From [namespace.udecl]: |
1b255e8f | 4728 | |
b655c310 NS |
4729 | A using-declaration used as a member-declaration shall refer to a |
4730 | member of a base class of the class being defined. | |
4731 | ||
4732 | In general, we cannot check this constraint in a template because | |
4733 | we do not know the entire set of base classes of the current | |
4734 | class type. Morover, if SCOPE is dependent, it might match a | |
4735 | non-dependent base. */ | |
4736 | ||
0c29f2a2 NS |
4737 | tree decl = NULL_TREE; |
4738 | if (!dependent_scope_p (scope)) | |
86098eb8 | 4739 | { |
b655c310 | 4740 | base_kind b_kind; |
0c29f2a2 NS |
4741 | tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, |
4742 | tf_warning_or_error); | |
b655c310 | 4743 | if (b_kind < bk_proper_base) |
86098eb8 | 4744 | { |
0c29f2a2 NS |
4745 | /* If there are dependent bases, scope might resolve at |
4746 | instantiation time, even if it isn't exactly one of the | |
4747 | dependent bases. */ | |
4748 | if (b_kind == bk_same_type || !any_dependent_bases_p ()) | |
9deb204a | 4749 | { |
b655c310 NS |
4750 | error_not_base_type (scope, current_class_type); |
4751 | return NULL_TREE; | |
9deb204a | 4752 | } |
86098eb8 | 4753 | } |
84eb0f1a | 4754 | else if (name == ctor_identifier && !binfo_direct_p (binfo)) |
b655c310 NS |
4755 | { |
4756 | error ("cannot inherit constructors from indirect base %qT", scope); | |
4757 | return NULL_TREE; | |
4758 | } | |
0c29f2a2 NS |
4759 | else if (!IDENTIFIER_CONV_OP_P (name) |
4760 | || !dependent_type_p (TREE_TYPE (name))) | |
b655c310 NS |
4761 | { |
4762 | decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); | |
4763 | if (!decl) | |
4764 | { | |
4765 | error ("no members matching %<%T::%D%> in %q#T", scope, name, | |
4766 | scope); | |
4767 | return NULL_TREE; | |
4768 | } | |
0c29f2a2 | 4769 | |
b655c310 NS |
4770 | /* The binfo from which the functions came does not matter. */ |
4771 | if (BASELINK_P (decl)) | |
4772 | decl = BASELINK_FUNCTIONS (decl); | |
4773 | } | |
86098eb8 | 4774 | } |
86098eb8 | 4775 | |
0c29f2a2 | 4776 | tree value = build_lang_decl (USING_DECL, name, NULL_TREE); |
b655c310 NS |
4777 | USING_DECL_DECLS (value) = decl; |
4778 | USING_DECL_SCOPE (value) = scope; | |
4779 | DECL_DEPENDENT_P (value) = !decl; | |
a5e6b29b | 4780 | |
b655c310 | 4781 | return value; |
a5e6b29b GDR |
4782 | } |
4783 | ||
b655c310 | 4784 | \f |
4b4b2e58 NS |
4785 | /* Return the binding for NAME in NS. If NS is NULL, look in |
4786 | global_namespace. */ | |
4787 | ||
a5e6b29b | 4788 | tree |
06aa5490 | 4789 | get_namespace_binding (tree ns, tree name) |
a5e6b29b | 4790 | { |
b655c310 | 4791 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 NS |
4792 | if (!ns) |
4793 | ns = global_namespace; | |
3c9cca88 NS |
4794 | gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns)); |
4795 | tree ret = find_namespace_value (ns, name); | |
b655c310 | 4796 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
3c9cca88 | 4797 | return ret; |
5a167978 GDR |
4798 | } |
4799 | ||
87e3d7cf NS |
4800 | /* Push internal DECL into the global namespace. Does not do the |
4801 | full overload fn handling and does not add it to the list of things | |
4802 | in the namespace. */ | |
1e003829 | 4803 | |
b655c310 | 4804 | void |
87e3d7cf | 4805 | set_global_binding (tree decl) |
1e003829 | 4806 | { |
b655c310 | 4807 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 | 4808 | |
87e3d7cf | 4809 | tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true); |
3c9cca88 | 4810 | |
a6a5091a NS |
4811 | if (*slot) |
4812 | /* The user's placed something in the implementor's namespace. */ | |
4813 | diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot)); | |
4814 | ||
4815 | /* Force the binding, so compiler internals continue to work. */ | |
4816 | *slot = decl; | |
4b4b2e58 | 4817 | |
b655c310 | 4818 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
1e003829 JM |
4819 | } |
4820 | ||
b655c310 NS |
4821 | /* Set the context of a declaration to scope. Complain if we are not |
4822 | outside scope. */ | |
5a167978 | 4823 | |
b655c310 NS |
4824 | void |
4825 | set_decl_namespace (tree decl, tree scope, bool friendp) | |
5a167978 | 4826 | { |
b655c310 NS |
4827 | /* Get rid of namespace aliases. */ |
4828 | scope = ORIGINAL_NAMESPACE (scope); | |
af92ab36 | 4829 | |
b655c310 | 4830 | /* It is ok for friends to be qualified in parallel space. */ |
322763f5 | 4831 | if (!friendp && !is_nested_namespace (current_namespace, scope)) |
b655c310 NS |
4832 | error ("declaration of %qD not in a namespace surrounding %qD", |
4833 | decl, scope); | |
4834 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
af92ab36 | 4835 | |
5ec046c0 NS |
4836 | /* See whether this has been declared in the namespace or inline |
4837 | children. */ | |
4838 | tree old = NULL_TREE; | |
4839 | { | |
4840 | name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN); | |
4841 | if (!lookup.search_qualified (scope, /*usings=*/false)) | |
4842 | /* No old declaration at all. */ | |
4843 | goto not_found; | |
4844 | old = lookup.value; | |
4845 | } | |
c8094d83 | 4846 | |
b655c310 NS |
4847 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ |
4848 | if (TREE_CODE (old) == TREE_LIST) | |
5a167978 | 4849 | { |
5ec046c0 NS |
4850 | ambiguous: |
4851 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
b655c310 NS |
4852 | error ("reference to %qD is ambiguous", decl); |
4853 | print_candidates (old); | |
4854 | return; | |
4855 | } | |
5ec046c0 NS |
4856 | |
4857 | if (!DECL_DECLARES_FUNCTION_P (decl)) | |
b655c310 | 4858 | { |
b655c310 NS |
4859 | /* Don't compare non-function decls with decls_match here, since |
4860 | it can't check for the correct constness at this | |
5ec046c0 NS |
4861 | point. pushdecl will find those errors later. */ |
4862 | ||
4863 | /* We might have found it in an inline namespace child of SCOPE. */ | |
4864 | if (TREE_CODE (decl) == TREE_CODE (old)) | |
4865 | DECL_CONTEXT (decl) = DECL_CONTEXT (old); | |
4866 | ||
4867 | found: | |
4868 | /* Writing "N::i" to declare something directly in "N" is invalid. */ | |
4869 | if (CP_DECL_CONTEXT (decl) == current_namespace | |
4870 | && at_namespace_scope_p ()) | |
4871 | error ("explicit qualification in declaration of %qD", decl); | |
b655c310 NS |
4872 | return; |
4873 | } | |
5ec046c0 | 4874 | |
b655c310 | 4875 | /* Since decl is a function, old should contain a function decl. */ |
e1cad930 | 4876 | if (!OVL_P (old)) |
5ec046c0 NS |
4877 | goto not_found; |
4878 | ||
b655c310 NS |
4879 | /* We handle these in check_explicit_instantiation_namespace. */ |
4880 | if (processing_explicit_instantiation) | |
4881 | return; | |
4882 | if (processing_template_decl || processing_specialization) | |
4883 | /* We have not yet called push_template_decl to turn a | |
4884 | FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't | |
4885 | match. But, we'll check later, when we construct the | |
4886 | template. */ | |
4887 | return; | |
4888 | /* Instantiations or specializations of templates may be declared as | |
4889 | friends in any namespace. */ | |
4890 | if (friendp && DECL_USE_TEMPLATE (decl)) | |
4891 | return; | |
5ec046c0 NS |
4892 | |
4893 | tree found; | |
4894 | found = NULL_TREE; | |
4895 | ||
4896 | for (lkp_iterator iter (old); iter; ++iter) | |
b655c310 | 4897 | { |
5ec046c0 NS |
4898 | if (iter.using_p ()) |
4899 | continue; | |
87c976ae | 4900 | |
5ec046c0 NS |
4901 | tree ofn = *iter; |
4902 | ||
4903 | /* Adjust DECL_CONTEXT first so decls_match will return true | |
4904 | if DECL will match a declaration in an inline namespace. */ | |
4905 | DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); | |
4906 | if (decls_match (decl, ofn)) | |
b655c310 | 4907 | { |
5ec046c0 | 4908 | if (found) |
b655c310 | 4909 | { |
5ec046c0 NS |
4910 | /* We found more than one matching declaration. */ |
4911 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
4912 | goto ambiguous; | |
b655c310 | 4913 | } |
5ec046c0 | 4914 | found = ofn; |
5a167978 GDR |
4915 | } |
4916 | } | |
5ec046c0 NS |
4917 | |
4918 | if (found) | |
5a167978 | 4919 | { |
5ec046c0 NS |
4920 | if (DECL_HIDDEN_FRIEND_P (found)) |
4921 | { | |
4922 | pedwarn (DECL_SOURCE_LOCATION (decl), 0, | |
4923 | "%qD has not been declared within %qD", decl, scope); | |
4924 | inform (DECL_SOURCE_LOCATION (found), | |
4925 | "only here as a %<friend%>"); | |
4926 | } | |
4927 | DECL_CONTEXT (decl) = DECL_CONTEXT (found); | |
4928 | goto found; | |
5a167978 | 4929 | } |
b655c310 | 4930 | |
5ec046c0 | 4931 | not_found: |
b655c310 NS |
4932 | /* It didn't work, go back to the explicit scope. */ |
4933 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
b655c310 | 4934 | error ("%qD should have been declared inside %qD", decl, scope); |
5a167978 GDR |
4935 | } |
4936 | ||
b655c310 | 4937 | /* Return the namespace where the current declaration is declared. */ |
00e8de68 GDR |
4938 | |
4939 | tree | |
b655c310 | 4940 | current_decl_namespace (void) |
00e8de68 | 4941 | { |
b655c310 NS |
4942 | tree result; |
4943 | /* If we have been pushed into a different namespace, use it. */ | |
4944 | if (!vec_safe_is_empty (decl_namespace_list)) | |
4945 | return decl_namespace_list->last (); | |
00e8de68 | 4946 | |
b655c310 NS |
4947 | if (current_class_type) |
4948 | result = decl_namespace_context (current_class_type); | |
4949 | else if (current_function_decl) | |
4950 | result = decl_namespace_context (current_function_decl); | |
4951 | else | |
4952 | result = current_namespace; | |
4953 | return result; | |
00e8de68 GDR |
4954 | } |
4955 | ||
b655c310 NS |
4956 | /* Process any ATTRIBUTES on a namespace definition. Returns true if |
4957 | attribute visibility is seen. */ | |
00e8de68 | 4958 | |
b655c310 NS |
4959 | bool |
4960 | handle_namespace_attrs (tree ns, tree attributes) | |
00e8de68 | 4961 | { |
b655c310 NS |
4962 | tree d; |
4963 | bool saw_vis = false; | |
4964 | ||
93e544c1 JJ |
4965 | if (attributes == error_mark_node) |
4966 | return false; | |
4967 | ||
b655c310 | 4968 | for (d = attributes; d; d = TREE_CHAIN (d)) |
af79925b | 4969 | { |
b655c310 NS |
4970 | tree name = get_attribute_name (d); |
4971 | tree args = TREE_VALUE (d); | |
4972 | ||
4973 | if (is_attribute_p ("visibility", name)) | |
4974 | { | |
4975 | /* attribute visibility is a property of the syntactic block | |
4976 | rather than the namespace as a whole, so we don't touch the | |
4977 | NAMESPACE_DECL at all. */ | |
4978 | tree x = args ? TREE_VALUE (args) : NULL_TREE; | |
4979 | if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) | |
4980 | { | |
4981 | warning (OPT_Wattributes, | |
4982 | "%qD attribute requires a single NTBS argument", | |
4983 | name); | |
4984 | continue; | |
4985 | } | |
4986 | ||
4987 | if (!TREE_PUBLIC (ns)) | |
4988 | warning (OPT_Wattributes, | |
4989 | "%qD attribute is meaningless since members of the " | |
4990 | "anonymous namespace get local symbols", name); | |
4991 | ||
4992 | push_visibility (TREE_STRING_POINTER (x), 1); | |
4993 | saw_vis = true; | |
4994 | } | |
4995 | else if (is_attribute_p ("abi_tag", name)) | |
4996 | { | |
44e00a7a | 4997 | if (!DECL_NAME (ns)) |
b655c310 | 4998 | { |
44e00a7a | 4999 | warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " |
b655c310 NS |
5000 | "namespace", name); |
5001 | continue; | |
5002 | } | |
44e00a7a | 5003 | if (!DECL_NAMESPACE_INLINE_P (ns)) |
b655c310 | 5004 | { |
44e00a7a | 5005 | warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " |
b655c310 NS |
5006 | "namespace", name); |
5007 | continue; | |
5008 | } | |
5009 | if (!args) | |
5010 | { | |
5011 | tree dn = DECL_NAME (ns); | |
5012 | args = build_string (IDENTIFIER_LENGTH (dn) + 1, | |
5013 | IDENTIFIER_POINTER (dn)); | |
5014 | TREE_TYPE (args) = char_array_type_node; | |
5015 | args = fix_string_type (args); | |
5016 | args = build_tree_list (NULL_TREE, args); | |
5017 | } | |
5018 | if (check_abi_tag_args (args, name)) | |
5019 | DECL_ATTRIBUTES (ns) = tree_cons (name, args, | |
5020 | DECL_ATTRIBUTES (ns)); | |
5021 | } | |
5022 | else | |
5023 | { | |
5024 | warning (OPT_Wattributes, "%qD attribute directive ignored", | |
5025 | name); | |
5026 | continue; | |
5027 | } | |
af79925b | 5028 | } |
bd3d082e | 5029 | |
b655c310 NS |
5030 | return saw_vis; |
5031 | } | |
e1cad930 | 5032 | |
b655c310 | 5033 | /* Temporarily set the namespace for the current declaration. */ |
bd3d082e | 5034 | |
b655c310 NS |
5035 | void |
5036 | push_decl_namespace (tree decl) | |
bd3d082e | 5037 | { |
b655c310 NS |
5038 | if (TREE_CODE (decl) != NAMESPACE_DECL) |
5039 | decl = decl_namespace_context (decl); | |
5040 | vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); | |
00e8de68 GDR |
5041 | } |
5042 | ||
b655c310 | 5043 | /* [namespace.memdef]/2 */ |
d63d5d0c | 5044 | |
b655c310 NS |
5045 | void |
5046 | pop_decl_namespace (void) | |
d63d5d0c | 5047 | { |
b655c310 NS |
5048 | decl_namespace_list->pop (); |
5049 | } | |
d63d5d0c | 5050 | |
b655c310 | 5051 | /* Process a namespace-alias declaration. */ |
501c95ff NF |
5052 | |
5053 | void | |
b655c310 | 5054 | do_namespace_alias (tree alias, tree name_space) |
501c95ff | 5055 | { |
b655c310 NS |
5056 | if (name_space == error_mark_node) |
5057 | return; | |
501c95ff | 5058 | |
b655c310 | 5059 | gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); |
501c95ff | 5060 | |
b655c310 | 5061 | name_space = ORIGINAL_NAMESPACE (name_space); |
501c95ff | 5062 | |
b655c310 NS |
5063 | /* Build the alias. */ |
5064 | alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); | |
5065 | DECL_NAMESPACE_ALIAS (alias) = name_space; | |
5066 | DECL_EXTERNAL (alias) = 1; | |
5067 | DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); | |
5068 | pushdecl (alias); | |
501c95ff | 5069 | |
b655c310 NS |
5070 | /* Emit debug info for namespace alias. */ |
5071 | if (!building_stmt_list_p ()) | |
5072 | (*debug_hooks->early_global_decl) (alias); | |
5073 | } | |
501c95ff | 5074 | |
b655c310 NS |
5075 | /* Like pushdecl, only it places X in the current namespace, |
5076 | if appropriate. */ | |
501c95ff | 5077 | |
b655c310 NS |
5078 | tree |
5079 | pushdecl_namespace_level (tree x, bool is_friend) | |
5080 | { | |
5081 | cp_binding_level *b = current_binding_level; | |
5082 | tree t; | |
501c95ff | 5083 | |
b655c310 | 5084 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
5256a7f5 | 5085 | t = do_pushdecl_with_scope |
d16d5eac | 5086 | (x, NAMESPACE_LEVEL (current_namespace), is_friend); |
501c95ff | 5087 | |
b655c310 NS |
5088 | /* Now, the type_shadowed stack may screw us. Munge it so it does |
5089 | what we want. */ | |
5090 | if (TREE_CODE (t) == TYPE_DECL) | |
52ed68f7 | 5091 | { |
b655c310 NS |
5092 | tree name = DECL_NAME (t); |
5093 | tree newval; | |
5094 | tree *ptr = (tree *)0; | |
5095 | for (; !global_scope_p (b); b = b->level_chain) | |
52ed68f7 | 5096 | { |
b655c310 NS |
5097 | tree shadowed = b->type_shadowed; |
5098 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
5099 | if (TREE_PURPOSE (shadowed) == name) | |
5100 | { | |
5101 | ptr = &TREE_VALUE (shadowed); | |
5102 | /* Can't break out of the loop here because sometimes | |
5103 | a binding level will have duplicate bindings for | |
5104 | PT names. It's gross, but I haven't time to fix it. */ | |
5105 | } | |
5106 | } | |
5107 | newval = TREE_TYPE (t); | |
5108 | if (ptr == (tree *)0) | |
5109 | { | |
5110 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
5111 | up here if this is changed to an assertion. --KR */ | |
5112 | SET_IDENTIFIER_TYPE_VALUE (name, t); | |
5113 | } | |
5114 | else | |
5115 | { | |
5116 | *ptr = newval; | |
52ed68f7 | 5117 | } |
52ed68f7 | 5118 | } |
b655c310 NS |
5119 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
5120 | return t; | |
501c95ff NF |
5121 | } |
5122 | ||
9d029ddf | 5123 | /* Process a using-declaration appearing in namespace scope. */ |
ebed7175 | 5124 | |
b655c310 | 5125 | void |
9d029ddf | 5126 | finish_namespace_using_decl (tree decl, tree scope, tree name) |
ebed7175 | 5127 | { |
b655c310 | 5128 | tree orig_decl = decl; |
fcb2cdfc | 5129 | |
3c9cca88 NS |
5130 | gcc_checking_assert (current_binding_level->kind == sk_namespace |
5131 | && !processing_template_decl); | |
b655c310 NS |
5132 | decl = validate_nonmember_using_decl (decl, scope, name); |
5133 | if (decl == NULL_TREE) | |
5134 | return; | |
ebed7175 | 5135 | |
3c9cca88 NS |
5136 | tree *slot = find_namespace_slot (current_namespace, name, true); |
5137 | tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE; | |
5138 | tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE; | |
5139 | do_nonmember_using_decl (scope, name, &val, &type); | |
5140 | if (STAT_HACK_P (*slot)) | |
5141 | { | |
5142 | STAT_DECL (*slot) = val; | |
5143 | STAT_TYPE (*slot) = type; | |
5144 | } | |
5145 | else if (type) | |
5146 | *slot = stat_hack (val, type); | |
5147 | else | |
5148 | *slot = val; | |
b655c310 NS |
5149 | |
5150 | /* Emit debug info. */ | |
3c9cca88 | 5151 | cp_emit_debug_info_for_using (orig_decl, current_namespace); |
9d029ddf | 5152 | } |
b655c310 | 5153 | |
3c9cca88 | 5154 | /* Process a using-declaration at function scope. */ |
9d029ddf NS |
5155 | |
5156 | void | |
5157 | finish_local_using_decl (tree decl, tree scope, tree name) | |
5158 | { | |
5159 | tree orig_decl = decl; | |
5160 | ||
5161 | gcc_checking_assert (current_binding_level->kind != sk_class | |
5162 | && current_binding_level->kind != sk_namespace); | |
5163 | decl = validate_nonmember_using_decl (decl, scope, name); | |
5164 | if (decl == NULL_TREE) | |
5165 | return; | |
5166 | ||
e1cad930 | 5167 | add_decl_expr (decl); |
9d029ddf NS |
5168 | |
5169 | cxx_binding *binding = find_local_binding (current_binding_level, name); | |
5170 | tree value = binding ? binding->value : NULL_TREE; | |
5171 | tree type = binding ? binding->type : NULL_TREE; | |
5172 | ||
5173 | do_nonmember_using_decl (scope, name, &value, &type); | |
5174 | ||
5175 | if (!value) | |
5176 | ; | |
5177 | else if (binding && value == binding->value) | |
5178 | ; | |
5179 | else if (binding && binding->value && TREE_CODE (value) == OVERLOAD) | |
5180 | { | |
5181 | update_local_overload (IDENTIFIER_BINDING (name), value); | |
5182 | IDENTIFIER_BINDING (name)->value = value; | |
5183 | } | |
5184 | else | |
5185 | /* Install the new binding. */ | |
5186 | push_local_binding (name, value, true); | |
5187 | ||
5188 | if (!type) | |
5189 | ; | |
5190 | else if (binding && type == binding->type) | |
5191 | ; | |
5192 | else | |
5193 | { | |
5194 | push_local_binding (name, type, true); | |
5195 | set_identifier_type_value (name, type); | |
5196 | } | |
5197 | ||
5198 | /* Emit debug info. */ | |
5199 | if (!processing_template_decl) | |
5200 | cp_emit_debug_info_for_using (orig_decl, current_scope ()); | |
ebed7175 DM |
5201 | } |
5202 | ||
b655c310 NS |
5203 | /* Return the declarations that are members of the namespace NS. */ |
5204 | ||
5205 | tree | |
5206 | cp_namespace_decls (tree ns) | |
5207 | { | |
5208 | return NAMESPACE_LEVEL (ns)->names; | |
52ed68f7 DM |
5209 | } |
5210 | ||
b655c310 | 5211 | /* Combine prefer_type and namespaces_only into flags. */ |
9ca6556e | 5212 | |
b655c310 NS |
5213 | static int |
5214 | lookup_flags (int prefer_type, int namespaces_only) | |
5215 | { | |
5216 | if (namespaces_only) | |
5217 | return LOOKUP_PREFER_NAMESPACES; | |
5218 | if (prefer_type > 1) | |
5219 | return LOOKUP_PREFER_TYPES; | |
5220 | if (prefer_type > 0) | |
5221 | return LOOKUP_PREFER_BOTH; | |
5222 | return 0; | |
5223 | } | |
9ca6556e | 5224 | |
b655c310 NS |
5225 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to |
5226 | ignore it or not. Subroutine of lookup_name_real and | |
5227 | lookup_type_scope. */ | |
172a4594 DS |
5228 | |
5229 | static bool | |
b655c310 | 5230 | qualify_lookup (tree val, int flags) |
172a4594 | 5231 | { |
b655c310 | 5232 | if (val == NULL_TREE) |
172a4594 | 5233 | return false; |
b655c310 NS |
5234 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5235 | return true; | |
5236 | if (flags & LOOKUP_PREFER_TYPES) | |
5237 | { | |
5238 | tree target_val = strip_using_decl (val); | |
5239 | if (TREE_CODE (target_val) == TYPE_DECL | |
5240 | || TREE_CODE (target_val) == TEMPLATE_DECL) | |
5241 | return true; | |
5242 | } | |
5243 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
74788b80 | 5244 | return false; |
b655c310 | 5245 | /* Look through lambda things that we shouldn't be able to see. */ |
5c263e84 | 5246 | if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val)) |
b655c310 NS |
5247 | return false; |
5248 | return true; | |
5249 | } | |
74788b80 | 5250 | |
cb731872 DM |
5251 | /* Is there a "using namespace std;" directive within USINGS? */ |
5252 | ||
5253 | static bool | |
5254 | using_directives_contain_std_p (vec<tree, va_gc> *usings) | |
5255 | { | |
5256 | if (!usings) | |
5257 | return false; | |
5258 | ||
5259 | for (unsigned ix = usings->length (); ix--;) | |
5260 | if ((*usings)[ix] == std_node) | |
5261 | return true; | |
5262 | ||
5263 | return false; | |
5264 | } | |
5265 | ||
5266 | /* Is there a "using namespace std;" directive within the current | |
5267 | namespace (or its ancestors)? | |
5268 | Compare with name_lookup::search_unqualified. */ | |
5269 | ||
5270 | static bool | |
5271 | has_using_namespace_std_directive_p () | |
5272 | { | |
5273 | /* Look at local using-directives. */ | |
5274 | for (cp_binding_level *level = current_binding_level; | |
5275 | level->kind != sk_namespace; | |
5276 | level = level->level_chain) | |
5277 | if (using_directives_contain_std_p (level->using_directives)) | |
5278 | return true; | |
5279 | ||
5280 | /* Look at this namespace and its ancestors. */ | |
5281 | for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope)) | |
5282 | { | |
5283 | if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope))) | |
5284 | return true; | |
5285 | ||
5286 | if (scope == global_namespace) | |
5287 | break; | |
5288 | } | |
5289 | ||
5290 | return false; | |
5291 | } | |
5292 | ||
7e2de6df DM |
5293 | /* Subclass of deferred_diagnostic, for issuing a note when |
5294 | --param cxx-max-namespaces-for-diagnostic-help is reached. | |
90ea9897 | 5295 | |
7e2de6df DM |
5296 | The note should be issued after the error, but before any other |
5297 | deferred diagnostics. This is handled by decorating a wrapped | |
5298 | deferred_diagnostic, and emitting a note before that wrapped note is | |
5299 | deleted. */ | |
5300 | ||
5301 | class namespace_limit_reached : public deferred_diagnostic | |
5302 | { | |
5303 | public: | |
5304 | namespace_limit_reached (location_t loc, unsigned limit, tree name, | |
5305 | gnu::unique_ptr<deferred_diagnostic> wrapped) | |
5306 | : deferred_diagnostic (loc), | |
5307 | m_limit (limit), m_name (name), | |
5308 | m_wrapped (move (wrapped)) | |
5309 | { | |
5310 | } | |
5311 | ||
5312 | ~namespace_limit_reached () | |
5313 | { | |
5314 | /* Unconditionally warn that the search was truncated. */ | |
5315 | inform (get_location (), | |
5316 | "maximum limit of %d namespaces searched for %qE", | |
5317 | m_limit, m_name); | |
5318 | /* m_wrapped will be implicitly deleted after this, emitting any followup | |
5319 | diagnostic after the above note. */ | |
5320 | } | |
5321 | ||
5322 | private: | |
5323 | unsigned m_limit; | |
5324 | tree m_name; | |
5325 | gnu::unique_ptr<deferred_diagnostic> m_wrapped; | |
5326 | }; | |
5327 | ||
5328 | /* Subclass of deferred_diagnostic, for use when issuing a single suggestion. | |
5329 | Emit a note showing the location of the declaration of the suggestion. */ | |
5330 | ||
5331 | class show_candidate_location : public deferred_diagnostic | |
5332 | { | |
5333 | public: | |
5334 | show_candidate_location (location_t loc, tree candidate) | |
5335 | : deferred_diagnostic (loc), | |
5336 | m_candidate (candidate) | |
5337 | { | |
5338 | } | |
5339 | ||
5340 | ~show_candidate_location () | |
5341 | { | |
5342 | inform (location_of (m_candidate), "%qE declared here", m_candidate); | |
5343 | } | |
5344 | ||
5345 | private: | |
5346 | tree m_candidate; | |
5347 | }; | |
5348 | ||
5349 | /* Subclass of deferred_diagnostic, for use when there are multiple candidates | |
5350 | to be suggested by suggest_alternatives_for. | |
5351 | ||
5352 | Emit a series of notes showing the various suggestions. */ | |
5353 | ||
5354 | class suggest_alternatives : public deferred_diagnostic | |
90ea9897 | 5355 | { |
7e2de6df DM |
5356 | public: |
5357 | suggest_alternatives (location_t loc, vec<tree> candidates) | |
5358 | : deferred_diagnostic (loc), | |
5359 | m_candidates (candidates) | |
5360 | { | |
5361 | } | |
5362 | ||
5363 | ~suggest_alternatives () | |
5364 | { | |
5365 | if (m_candidates.length ()) | |
5366 | { | |
5367 | inform_n (get_location (), m_candidates.length (), | |
5368 | "suggested alternative:", | |
5369 | "suggested alternatives:"); | |
5370 | for (unsigned ix = 0; ix != m_candidates.length (); ix++) | |
5371 | { | |
5372 | tree val = m_candidates[ix]; | |
5373 | ||
5374 | inform (location_of (val), " %qE", val); | |
5375 | } | |
5376 | } | |
5377 | m_candidates.release (); | |
5378 | } | |
5379 | ||
5380 | private: | |
5381 | vec<tree> m_candidates; | |
5382 | }; | |
5383 | ||
5384 | /* A class for encapsulating the result of a search across | |
3ded6ffd DM |
5385 | multiple namespaces (and scoped enums within them) for an |
5386 | unrecognized name seen at a given source location. */ | |
7e2de6df DM |
5387 | |
5388 | class namespace_hints | |
5389 | { | |
5390 | public: | |
5391 | namespace_hints (location_t loc, tree name); | |
5392 | ||
5393 | name_hint convert_candidates_to_name_hint (); | |
5394 | name_hint maybe_decorate_with_limit (name_hint); | |
5395 | ||
5396 | private: | |
3ded6ffd DM |
5397 | void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name); |
5398 | ||
7e2de6df DM |
5399 | location_t m_loc; |
5400 | tree m_name; | |
5401 | vec<tree> m_candidates; | |
5402 | ||
5403 | /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */ | |
5404 | unsigned m_limit; | |
5405 | ||
5406 | /* Was the limit reached? */ | |
5407 | bool m_limited; | |
5408 | }; | |
5409 | ||
3ded6ffd DM |
5410 | /* Constructor for namespace_hints. Search namespaces and scoped enums, |
5411 | looking for an exact match for unrecognized NAME seen at LOC. */ | |
7e2de6df DM |
5412 | |
5413 | namespace_hints::namespace_hints (location_t loc, tree name) | |
5414 | : m_loc(loc), m_name (name) | |
5415 | { | |
5416 | auto_vec<tree> worklist; | |
5417 | ||
5418 | m_candidates = vNULL; | |
5419 | m_limited = false; | |
5420 | m_limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); | |
c957e9c0 NS |
5421 | |
5422 | /* Breadth-first search of namespaces. Up to limit namespaces | |
5423 | searched (limit zero == unlimited). */ | |
5424 | worklist.safe_push (global_namespace); | |
5425 | for (unsigned ix = 0; ix != worklist.length (); ix++) | |
b655c310 | 5426 | { |
c957e9c0 | 5427 | tree ns = worklist[ix]; |
25396db9 | 5428 | name_lookup lookup (name); |
90ea9897 | 5429 | |
25396db9 | 5430 | if (lookup.search_qualified (ns, false)) |
7e2de6df | 5431 | m_candidates.safe_push (lookup.value); |
00e8de68 | 5432 | |
7e2de6df | 5433 | if (!m_limited) |
00e8de68 | 5434 | { |
c957e9c0 NS |
5435 | /* Look for child namespaces. We have to do this |
5436 | indirectly because they are chained in reverse order, | |
5437 | which is confusing to the user. */ | |
7e2de6df | 5438 | auto_vec<tree> children; |
c957e9c0 NS |
5439 | |
5440 | for (tree decl = NAMESPACE_LEVEL (ns)->names; | |
5441 | decl; decl = TREE_CHAIN (decl)) | |
3ded6ffd DM |
5442 | { |
5443 | if (TREE_CODE (decl) == NAMESPACE_DECL | |
5444 | && !DECL_NAMESPACE_ALIAS (decl) | |
5445 | && !DECL_NAMESPACE_INLINE_P (decl)) | |
5446 | children.safe_push (decl); | |
5447 | ||
5448 | /* Look for exact matches for NAME within scoped enums. | |
5449 | These aren't added to the worklist, and so don't count | |
5450 | against the search limit. */ | |
5451 | if (TREE_CODE (decl) == TYPE_DECL) | |
5452 | { | |
5453 | tree type = TREE_TYPE (decl); | |
5454 | if (SCOPED_ENUM_P (type)) | |
5455 | maybe_add_candidate_for_scoped_enum (type, name); | |
5456 | } | |
5457 | } | |
c957e9c0 | 5458 | |
7e2de6df | 5459 | while (!m_limited && !children.is_empty ()) |
b655c310 | 5460 | { |
7e2de6df DM |
5461 | if (worklist.length () == m_limit) |
5462 | m_limited = true; | |
c957e9c0 NS |
5463 | else |
5464 | worklist.safe_push (children.pop ()); | |
b655c310 NS |
5465 | } |
5466 | } | |
b655c310 | 5467 | } |
7e2de6df | 5468 | } |
c8094d83 | 5469 | |
7e2de6df DM |
5470 | /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc |
5471 | for m_name, an IDENTIFIER_NODE for which name lookup failed. | |
c8094d83 | 5472 | |
7e2de6df DM |
5473 | If m_candidates is non-empty, use it to generate a suggestion and/or |
5474 | a deferred diagnostic that lists the possible candidate(s). | |
5475 | */ | |
5476 | ||
5477 | name_hint | |
5478 | namespace_hints::convert_candidates_to_name_hint () | |
5479 | { | |
5480 | /* How many candidates do we have? */ | |
5481 | ||
5482 | /* If we have just one candidate, issue a name_hint with it as a suggestion | |
5483 | (so that consumers are able to suggest it within the error message and emit | |
5484 | it as a fix-it hint), and with a note showing the candidate's location. */ | |
5485 | if (m_candidates.length () == 1) | |
5486 | { | |
5487 | tree candidate = m_candidates[0]; | |
5488 | /* Clean up CANDIDATES. */ | |
5489 | m_candidates.release (); | |
5490 | return name_hint (expr_to_string (candidate), | |
5491 | new show_candidate_location (m_loc, candidate)); | |
c957e9c0 | 5492 | } |
7e2de6df DM |
5493 | else if (m_candidates.length () > 1) |
5494 | /* If we have more than one candidate, issue a name_hint without a single | |
5495 | "suggestion", but with a deferred diagnostic that lists the | |
5496 | various candidates. This takes ownership of m_candidates. */ | |
5497 | return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates)); | |
5498 | ||
5499 | /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */ | |
5500 | gcc_assert (m_candidates.length () == 0); | |
5501 | gcc_assert (m_candidates == vNULL); | |
5502 | ||
5503 | return name_hint (); | |
5504 | } | |
5505 | ||
5506 | /* If --param cxx-max-namespaces-for-diagnostic-help was reached, | |
5507 | then we want to emit a note about after the error, but before | |
5508 | any other deferred diagnostics. | |
5509 | ||
5510 | Handle this by figuring out what hint is needed, then optionally | |
5511 | decorating HINT with a namespace_limit_reached wrapper. */ | |
5512 | ||
5513 | name_hint | |
5514 | namespace_hints::maybe_decorate_with_limit (name_hint hint) | |
5515 | { | |
5516 | if (m_limited) | |
5517 | return name_hint (hint.suggestion (), | |
5518 | new namespace_limit_reached (m_loc, m_limit, | |
5519 | m_name, | |
5520 | hint.take_deferred ())); | |
5521 | else | |
5522 | return hint; | |
5523 | } | |
5524 | ||
3ded6ffd DM |
5525 | /* Look inside SCOPED_ENUM for exact matches for NAME. |
5526 | If one is found, add its CONST_DECL to m_candidates. */ | |
5527 | ||
5528 | void | |
5529 | namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum, | |
5530 | tree name) | |
5531 | { | |
5532 | gcc_assert (SCOPED_ENUM_P (scoped_enum)); | |
5533 | ||
5534 | for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter)) | |
5535 | { | |
5536 | tree id = TREE_PURPOSE (iter); | |
5537 | if (id == name) | |
5538 | { | |
5539 | m_candidates.safe_push (TREE_VALUE (iter)); | |
5540 | return; | |
5541 | } | |
5542 | } | |
5543 | } | |
5544 | ||
7e2de6df DM |
5545 | /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which |
5546 | name lookup failed. | |
5547 | ||
3ded6ffd DM |
5548 | Search through all available namespaces and any scoped enums within them |
5549 | and generate a suggestion and/or a deferred diagnostic that lists possible | |
5550 | candidate(s). | |
7e2de6df DM |
5551 | |
5552 | If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also | |
5553 | look for near-matches and suggest the best near-match, if there is one. | |
cb731872 | 5554 | |
7e2de6df DM |
5555 | If nothing is found, then an empty name_hint is returned. */ |
5556 | ||
5557 | name_hint | |
5558 | suggest_alternatives_for (location_t location, tree name, | |
5559 | bool suggest_misspellings) | |
5560 | { | |
5561 | /* First, search for exact matches in other namespaces. */ | |
5562 | namespace_hints ns_hints (location, name); | |
5563 | name_hint result = ns_hints.convert_candidates_to_name_hint (); | |
5564 | ||
5565 | /* Otherwise, try other approaches. */ | |
5566 | if (!result) | |
5567 | result = suggest_alternatives_for_1 (location, name, suggest_misspellings); | |
5568 | ||
5569 | return ns_hints.maybe_decorate_with_limit (gnu::move (result)); | |
5570 | } | |
5571 | ||
5572 | /* The second half of suggest_alternatives_for, for when no exact matches | |
5573 | were found in other namespaces. */ | |
5574 | ||
5575 | static name_hint | |
5576 | suggest_alternatives_for_1 (location_t location, tree name, | |
5577 | bool suggest_misspellings) | |
5578 | { | |
cb731872 DM |
5579 | /* No candidates were found in the available namespaces. */ |
5580 | ||
5581 | /* If there's a "using namespace std;" active, and this | |
5582 | is one of the most common "std::" names, then it's probably a | |
5583 | missing #include. */ | |
5584 | if (has_using_namespace_std_directive_p ()) | |
7e2de6df DM |
5585 | { |
5586 | name_hint hint = maybe_suggest_missing_std_header (location, name); | |
5587 | if (hint) | |
5588 | return hint; | |
5589 | } | |
cb731872 DM |
5590 | |
5591 | /* Otherwise, consider misspellings. */ | |
5592 | if (!suggest_misspellings) | |
7e2de6df | 5593 | return name_hint (); |
00e8de68 | 5594 | |
7e2de6df DM |
5595 | return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location); |
5596 | } | |
5597 | ||
5598 | /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which | |
5599 | name lookup failed. | |
5600 | ||
5601 | Search through all available namespaces and generate a suggestion and/or | |
5602 | a deferred diagnostic that lists possible candidate(s). | |
5603 | ||
5604 | This is similiar to suggest_alternatives_for, but doesn't fallback to | |
5605 | the other approaches used by that function. */ | |
5606 | ||
5607 | name_hint | |
5608 | suggest_alternatives_in_other_namespaces (location_t location, tree name) | |
5609 | { | |
5610 | namespace_hints ns_hints (location, name); | |
5611 | ||
5612 | name_hint result = ns_hints.convert_candidates_to_name_hint (); | |
5613 | ||
5614 | return ns_hints.maybe_decorate_with_limit (gnu::move (result)); | |
b655c310 | 5615 | } |
00e8de68 | 5616 | |
e1c7971b DM |
5617 | /* A well-known name within the C++ standard library, returned by |
5618 | get_std_name_hint. */ | |
5619 | ||
5620 | struct std_name_hint | |
5621 | { | |
5622 | /* A name within "std::". */ | |
5623 | const char *name; | |
5624 | ||
5625 | /* The header name defining it within the C++ Standard Library | |
5626 | (with '<' and '>'). */ | |
5627 | const char *header; | |
5628 | ||
5629 | /* The dialect of C++ in which this was added. */ | |
5630 | enum cxx_dialect min_dialect; | |
5631 | }; | |
5632 | ||
b655c310 NS |
5633 | /* Subroutine of maybe_suggest_missing_header for handling unrecognized names |
5634 | for some of the most common names within "std::". | |
e1c7971b | 5635 | Given non-NULL NAME, return the std_name_hint for it, or NULL. */ |
00e8de68 | 5636 | |
e1c7971b | 5637 | static const std_name_hint * |
b655c310 NS |
5638 | get_std_name_hint (const char *name) |
5639 | { | |
b655c310 | 5640 | static const std_name_hint hints[] = { |
e1c7971b DM |
5641 | /* <any>. */ |
5642 | {"any", "<any>", cxx17}, | |
5643 | {"any_cast", "<any>", cxx17}, | |
5644 | {"make_any", "<any>", cxx17}, | |
b655c310 | 5645 | /* <array>. */ |
e1c7971b DM |
5646 | {"array", "<array>", cxx11}, |
5647 | /* <atomic>. */ | |
5648 | {"atomic", "<atomic>", cxx11}, | |
5649 | {"atomic_flag", "<atomic>", cxx11}, | |
5650 | /* <bitset>. */ | |
5651 | {"bitset", "<bitset>", cxx11}, | |
b3e862e0 | 5652 | /* <complex>. */ |
e1c7971b DM |
5653 | {"complex", "<complex>", cxx98}, |
5654 | {"complex_literals", "<complex>", cxx98}, | |
5655 | /* <condition_variable>. */ | |
5656 | {"condition_variable", "<condition_variable>", cxx11}, | |
5657 | {"condition_variable_any", "<condition_variable>", cxx11}, | |
b655c310 | 5658 | /* <deque>. */ |
e1c7971b | 5659 | {"deque", "<deque>", cxx98}, |
b655c310 | 5660 | /* <forward_list>. */ |
e1c7971b | 5661 | {"forward_list", "<forward_list>", cxx11}, |
b655c310 | 5662 | /* <fstream>. */ |
e1c7971b DM |
5663 | {"basic_filebuf", "<fstream>", cxx98}, |
5664 | {"basic_ifstream", "<fstream>", cxx98}, | |
5665 | {"basic_ofstream", "<fstream>", cxx98}, | |
5666 | {"basic_fstream", "<fstream>", cxx98}, | |
5667 | {"fstream", "<fstream>", cxx98}, | |
5668 | {"ifstream", "<fstream>", cxx98}, | |
5669 | {"ofstream", "<fstream>", cxx98}, | |
5670 | /* <functional>. */ | |
5671 | {"bind", "<functional>", cxx11}, | |
5672 | {"function", "<functional>", cxx11}, | |
5673 | {"hash", "<functional>", cxx11}, | |
5674 | {"mem_fn", "<functional>", cxx11}, | |
5675 | /* <future>. */ | |
5676 | {"async", "<future>", cxx11}, | |
5677 | {"future", "<future>", cxx11}, | |
5678 | {"packaged_task", "<future>", cxx11}, | |
5679 | {"promise", "<future>", cxx11}, | |
b655c310 | 5680 | /* <iostream>. */ |
e1c7971b DM |
5681 | {"cin", "<iostream>", cxx98}, |
5682 | {"cout", "<iostream>", cxx98}, | |
5683 | {"cerr", "<iostream>", cxx98}, | |
5684 | {"clog", "<iostream>", cxx98}, | |
5685 | {"wcin", "<iostream>", cxx98}, | |
5686 | {"wcout", "<iostream>", cxx98}, | |
5687 | {"wclog", "<iostream>", cxx98}, | |
5688 | /* <istream>. */ | |
5689 | {"istream", "<istream>", cxx98}, | |
5690 | /* <iterator>. */ | |
5691 | {"advance", "<iterator>", cxx98}, | |
5692 | {"back_inserter", "<iterator>", cxx98}, | |
5693 | {"begin", "<iterator>", cxx11}, | |
5694 | {"distance", "<iterator>", cxx98}, | |
5695 | {"end", "<iterator>", cxx11}, | |
5696 | {"front_inserter", "<iterator>", cxx98}, | |
5697 | {"inserter", "<iterator>", cxx98}, | |
5698 | {"istream_iterator", "<iterator>", cxx98}, | |
5699 | {"istreambuf_iterator", "<iterator>", cxx98}, | |
5700 | {"iterator_traits", "<iterator>", cxx98}, | |
5701 | {"move_iterator", "<iterator>", cxx11}, | |
5702 | {"next", "<iterator>", cxx11}, | |
5703 | {"ostream_iterator", "<iterator>", cxx98}, | |
5704 | {"ostreambuf_iterator", "<iterator>", cxx98}, | |
5705 | {"prev", "<iterator>", cxx11}, | |
5706 | {"reverse_iterator", "<iterator>", cxx98}, | |
5707 | /* <ostream>. */ | |
5708 | {"ostream", "<ostream>", cxx98}, | |
b655c310 | 5709 | /* <list>. */ |
e1c7971b | 5710 | {"list", "<list>", cxx98}, |
b655c310 | 5711 | /* <map>. */ |
e1c7971b DM |
5712 | {"map", "<map>", cxx98}, |
5713 | {"multimap", "<map>", cxx98}, | |
62e98ef1 | 5714 | /* <memory>. */ |
e1c7971b DM |
5715 | {"make_shared", "<memory>", cxx11}, |
5716 | {"make_unique", "<memory>", cxx11}, | |
5717 | {"shared_ptr", "<memory>", cxx11}, | |
5718 | {"unique_ptr", "<memory>", cxx11}, | |
5719 | {"weak_ptr", "<memory>", cxx11}, | |
5720 | /* <mutex>. */ | |
5721 | {"mutex", "<mutex>", cxx11}, | |
5722 | {"timed_mutex", "<mutex>", cxx11}, | |
5723 | {"recursive_mutex", "<mutex>", cxx11}, | |
5724 | {"recursive_timed_mutex", "<mutex>", cxx11}, | |
5725 | {"once_flag", "<mutex>", cxx11}, | |
5726 | {"call_once,", "<mutex>", cxx11}, | |
5727 | {"lock", "<mutex>", cxx11}, | |
5728 | {"scoped_lock", "<mutex>", cxx17}, | |
5729 | {"try_lock", "<mutex>", cxx11}, | |
5730 | {"lock_guard", "<mutex>", cxx11}, | |
5731 | {"unique_lock", "<mutex>", cxx11}, | |
5732 | /* <optional>. */ | |
5733 | {"optional", "<optional>", cxx17}, | |
5734 | {"make_optional", "<optional>", cxx17}, | |
b655c310 | 5735 | /* <ostream>. */ |
e1c7971b DM |
5736 | {"ostream", "<ostream>", cxx98}, |
5737 | {"wostream", "<ostream>", cxx98}, | |
5738 | {"ends", "<ostream>", cxx98}, | |
5739 | {"flush", "<ostream>", cxx98}, | |
5740 | {"endl", "<ostream>", cxx98}, | |
5741 | /* <queue>. */ | |
5742 | {"queue", "<queue>", cxx98}, | |
5743 | {"priority_queue", "<queue>", cxx98}, | |
b655c310 | 5744 | /* <set>. */ |
e1c7971b DM |
5745 | {"set", "<set>", cxx98}, |
5746 | {"multiset", "<set>", cxx98}, | |
5747 | /* <shared_mutex>. */ | |
5748 | {"shared_lock", "<shared_mutex>", cxx14}, | |
5749 | {"shared_mutex", "<shared_mutex>", cxx17}, | |
5750 | {"shared_timed_mutex", "<shared_mutex>", cxx14}, | |
b655c310 | 5751 | /* <sstream>. */ |
e1c7971b DM |
5752 | {"basic_stringbuf", "<sstream>", cxx98}, |
5753 | {"basic_istringstream", "<sstream>", cxx98}, | |
5754 | {"basic_ostringstream", "<sstream>", cxx98}, | |
5755 | {"basic_stringstream", "<sstream>", cxx98}, | |
5756 | {"istringstream", "<sstream>", cxx98}, | |
5757 | {"ostringstream", "<sstream>", cxx98}, | |
5758 | {"stringstream", "<sstream>", cxx98}, | |
b655c310 | 5759 | /* <stack>. */ |
e1c7971b | 5760 | {"stack", "<stack>", cxx98}, |
b655c310 | 5761 | /* <string>. */ |
e1c7971b DM |
5762 | {"basic_string", "<string>", cxx98}, |
5763 | {"string", "<string>", cxx98}, | |
5764 | {"wstring", "<string>", cxx98}, | |
c124af93 | 5765 | {"u8string", "<string>", cxx2a}, |
e1c7971b DM |
5766 | {"u16string", "<string>", cxx11}, |
5767 | {"u32string", "<string>", cxx11}, | |
5768 | /* <string_view>. */ | |
5769 | {"string_view", "<string_view>", cxx17}, | |
5770 | /* <thread>. */ | |
5771 | {"thread", "<thread>", cxx11}, | |
5772 | /* <tuple>. */ | |
5773 | {"make_tuple", "<tuple>", cxx11}, | |
5774 | {"tuple", "<tuple>", cxx11}, | |
5775 | {"tuple_element", "<tuple>", cxx11}, | |
5776 | {"tuple_size", "<tuple>", cxx11}, | |
b655c310 | 5777 | /* <unordered_map>. */ |
e1c7971b DM |
5778 | {"unordered_map", "<unordered_map>", cxx11}, |
5779 | {"unordered_multimap", "<unordered_map>", cxx11}, | |
b655c310 | 5780 | /* <unordered_set>. */ |
e1c7971b DM |
5781 | {"unordered_set", "<unordered_set>", cxx11}, |
5782 | {"unordered_multiset", "<unordered_set>", cxx11}, | |
62e98ef1 | 5783 | /* <utility>. */ |
e1c7971b DM |
5784 | {"declval", "<utility>", cxx11}, |
5785 | {"forward", "<utility>", cxx11}, | |
5786 | {"make_pair", "<utility>", cxx98}, | |
5787 | {"move", "<utility>", cxx11}, | |
5788 | {"pair", "<utility>", cxx98}, | |
5789 | /* <variant>. */ | |
5790 | {"variant", "<variant>", cxx17}, | |
5791 | {"visit", "<variant>", cxx17}, | |
b655c310 | 5792 | /* <vector>. */ |
e1c7971b | 5793 | {"vector", "<vector>", cxx98}, |
b655c310 NS |
5794 | }; |
5795 | const size_t num_hints = sizeof (hints) / sizeof (hints[0]); | |
5796 | for (size_t i = 0; i < num_hints; i++) | |
5797 | { | |
01512446 | 5798 | if (strcmp (name, hints[i].name) == 0) |
e1c7971b | 5799 | return &hints[i]; |
b655c310 NS |
5800 | } |
5801 | return NULL; | |
5802 | } | |
00e8de68 | 5803 | |
e1c7971b DM |
5804 | /* Describe DIALECT. */ |
5805 | ||
5806 | static const char * | |
5807 | get_cxx_dialect_name (enum cxx_dialect dialect) | |
5808 | { | |
5809 | switch (dialect) | |
5810 | { | |
5811 | default: | |
5812 | gcc_unreachable (); | |
5813 | case cxx98: | |
5814 | return "C++98"; | |
5815 | case cxx11: | |
5816 | return "C++11"; | |
5817 | case cxx14: | |
5818 | return "C++14"; | |
5819 | case cxx17: | |
5820 | return "C++17"; | |
5821 | case cxx2a: | |
5822 | return "C++2a"; | |
5823 | } | |
5824 | } | |
5825 | ||
7e2de6df DM |
5826 | /* Subclass of deferred_diagnostic for use for names in the "std" namespace |
5827 | that weren't recognized, but for which we know which header it ought to be | |
5828 | in. | |
00e8de68 | 5829 | |
7e2de6df DM |
5830 | Emit a note either suggesting the header to be included, or noting that |
5831 | the current dialect is too early for the given name. */ | |
5832 | ||
5833 | class missing_std_header : public deferred_diagnostic | |
5834 | { | |
5835 | public: | |
5836 | missing_std_header (location_t loc, | |
5837 | const char *name_str, | |
5838 | const std_name_hint *header_hint) | |
5839 | : deferred_diagnostic (loc), | |
5840 | m_name_str (name_str), | |
5841 | m_header_hint (header_hint) | |
5842 | {} | |
5843 | ~missing_std_header () | |
5844 | { | |
5845 | gcc_rich_location richloc (get_location ()); | |
5846 | if (cxx_dialect >= m_header_hint->min_dialect) | |
5847 | { | |
5848 | const char *header = m_header_hint->header; | |
5849 | maybe_add_include_fixit (&richloc, header, true); | |
5850 | inform (&richloc, | |
5851 | "%<std::%s%> is defined in header %qs;" | |
5852 | " did you forget to %<#include %s%>?", | |
5853 | m_name_str, header, header); | |
5854 | } | |
5855 | else | |
5856 | inform (&richloc, | |
5857 | "%<std::%s%> is only available from %s onwards", | |
5858 | m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect)); | |
5859 | } | |
5860 | ||
5861 | private: | |
5862 | const char *m_name_str; | |
5863 | const std_name_hint *m_header_hint; | |
5864 | }; | |
5865 | ||
5866 | /* Attempt to generate a name_hint that suggests pertinent header files | |
5867 | for NAME at LOCATION, for common names within the "std" namespace, | |
5868 | or an empty name_hint if this isn't applicable. */ | |
5869 | ||
5870 | static name_hint | |
cb731872 | 5871 | maybe_suggest_missing_std_header (location_t location, tree name) |
b655c310 | 5872 | { |
b655c310 | 5873 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
c8094d83 | 5874 | |
b655c310 | 5875 | const char *name_str = IDENTIFIER_POINTER (name); |
e1c7971b | 5876 | const std_name_hint *header_hint = get_std_name_hint (name_str); |
f661e57e | 5877 | if (!header_hint) |
7e2de6df | 5878 | return name_hint (); |
f661e57e | 5879 | |
7e2de6df DM |
5880 | return name_hint (NULL, new missing_std_header (location, name_str, |
5881 | header_hint)); | |
b655c310 | 5882 | } |
c8094d83 | 5883 | |
7e2de6df DM |
5884 | /* Attempt to generate a name_hint that suggests a missing header file |
5885 | for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't | |
5886 | applicable. */ | |
cb731872 | 5887 | |
7e2de6df | 5888 | static name_hint |
cb731872 DM |
5889 | maybe_suggest_missing_header (location_t location, tree name, tree scope) |
5890 | { | |
5891 | if (scope == NULL_TREE) | |
7e2de6df | 5892 | return name_hint (); |
cb731872 | 5893 | if (TREE_CODE (scope) != NAMESPACE_DECL) |
7e2de6df | 5894 | return name_hint (); |
cb731872 DM |
5895 | /* We only offer suggestions for the "std" namespace. */ |
5896 | if (scope != std_node) | |
7e2de6df | 5897 | return name_hint (); |
cb731872 DM |
5898 | return maybe_suggest_missing_std_header (location, name); |
5899 | } | |
5900 | ||
7e2de6df DM |
5901 | /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name |
5902 | lookup failed within the explicitly provided SCOPE. | |
c8094d83 | 5903 | |
7e2de6df DM |
5904 | Suggest the the best meaningful candidates (if any), otherwise |
5905 | an empty name_hint is returned. */ | |
5906 | ||
5907 | name_hint | |
b655c310 NS |
5908 | suggest_alternative_in_explicit_scope (location_t location, tree name, |
5909 | tree scope) | |
5910 | { | |
7518398d MP |
5911 | /* Something went very wrong; don't suggest anything. */ |
5912 | if (name == error_mark_node) | |
7e2de6df | 5913 | return name_hint (); |
7518398d | 5914 | |
b655c310 NS |
5915 | /* Resolve any namespace aliases. */ |
5916 | scope = ORIGINAL_NAMESPACE (scope); | |
b9f673eb | 5917 | |
7e2de6df DM |
5918 | name_hint hint = maybe_suggest_missing_header (location, name, scope); |
5919 | if (hint) | |
5920 | return hint; | |
f661e57e | 5921 | |
b655c310 | 5922 | cp_binding_level *level = NAMESPACE_LEVEL (scope); |
d4d8c232 | 5923 | |
b655c310 NS |
5924 | best_match <tree, const char *> bm (name); |
5925 | consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); | |
d4d8c232 | 5926 | |
b655c310 NS |
5927 | /* See if we have a good suggesion for the user. */ |
5928 | const char *fuzzy_name = bm.get_best_meaningful_candidate (); | |
5929 | if (fuzzy_name) | |
7e2de6df | 5930 | return name_hint (fuzzy_name, NULL); |
d4d8c232 | 5931 | |
7e2de6df | 5932 | return name_hint (); |
b655c310 | 5933 | } |
d4d8c232 | 5934 | |
3ded6ffd DM |
5935 | /* Given NAME, look within SCOPED_ENUM for possible spell-correction |
5936 | candidates. */ | |
5937 | ||
5938 | name_hint | |
5939 | suggest_alternative_in_scoped_enum (tree name, tree scoped_enum) | |
5940 | { | |
5941 | gcc_assert (SCOPED_ENUM_P (scoped_enum)); | |
5942 | ||
5943 | best_match <tree, const char *> bm (name); | |
5944 | for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter)) | |
5945 | { | |
5946 | tree id = TREE_PURPOSE (iter); | |
5947 | bm.consider (IDENTIFIER_POINTER (id)); | |
5948 | } | |
5949 | return name_hint (bm.get_best_meaningful_candidate (), NULL); | |
5950 | } | |
5951 | ||
b655c310 NS |
5952 | /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL |
5953 | or a class TYPE). | |
00e8de68 | 5954 | |
b655c310 NS |
5955 | If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. |
5956 | If PREFER_TYPE is > 1, we only return TYPE_DECLs. | |
00e8de68 | 5957 | |
b655c310 NS |
5958 | Returns a DECL (or OVERLOAD, or BASELINK) representing the |
5959 | declaration found. If no suitable declaration can be found, | |
5960 | ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is | |
5961 | neither a class-type nor a namespace a diagnostic is issued. */ | |
00e8de68 | 5962 | |
98803730 | 5963 | tree |
b655c310 NS |
5964 | lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, |
5965 | bool find_hidden) | |
98803730 | 5966 | { |
b655c310 NS |
5967 | tree t = NULL_TREE; |
5968 | ||
5969 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
5970 | { | |
b655c310 NS |
5971 | int flags = lookup_flags (prefer_type, /*namespaces_only*/false); |
5972 | if (find_hidden) | |
5973 | flags |= LOOKUP_HIDDEN; | |
9dda0ace NS |
5974 | name_lookup lookup (name, flags); |
5975 | ||
5976 | if (qualified_namespace_lookup (scope, &lookup)) | |
5977 | t = lookup.value; | |
b655c310 NS |
5978 | } |
5979 | else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) | |
5980 | t = lookup_enumerator (scope, name); | |
5981 | else if (is_class_type (scope, complain)) | |
5982 | t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); | |
5983 | ||
5984 | if (!t) | |
5985 | return error_mark_node; | |
5986 | return t; | |
98803730 MS |
5987 | } |
5988 | ||
b655c310 NS |
5989 | /* [namespace.qual] |
5990 | Accepts the NAME to lookup and its qualifying SCOPE. | |
5991 | Returns the name/type pair found into the cxx_binding *RESULT, | |
5992 | or false on error. */ | |
461c6fce | 5993 | |
b655c310 | 5994 | static bool |
9dda0ace NS |
5995 | qualified_namespace_lookup (tree scope, name_lookup *lookup) |
5996 | { | |
b655c310 | 5997 | timevar_start (TV_NAME_LOOKUP); |
9dda0ace NS |
5998 | query_oracle (lookup->name); |
5999 | bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope)); | |
b655c310 | 6000 | timevar_stop (TV_NAME_LOOKUP); |
9dda0ace | 6001 | return found; |
575bfb00 LC |
6002 | } |
6003 | ||
b655c310 NS |
6004 | /* Helper function for lookup_name_fuzzy. |
6005 | Traverse binding level LVL, looking for good name matches for NAME | |
6006 | (and BM). */ | |
6007 | static void | |
6008 | consider_binding_level (tree name, best_match <tree, const char *> &bm, | |
6009 | cp_binding_level *lvl, bool look_within_fields, | |
6010 | enum lookup_name_fuzzy_kind kind) | |
00e8de68 | 6011 | { |
b655c310 NS |
6012 | if (look_within_fields) |
6013 | if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) | |
6014 | { | |
6015 | tree type = lvl->this_entity; | |
6016 | bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); | |
6017 | tree best_matching_field | |
6018 | = lookup_member_fuzzy (type, name, want_type_p); | |
6019 | if (best_matching_field) | |
6020 | bm.consider (IDENTIFIER_POINTER (best_matching_field)); | |
6021 | } | |
00e8de68 | 6022 | |
c79144f8 DM |
6023 | /* Only suggest names reserved for the implementation if NAME begins |
6024 | with an underscore. */ | |
6025 | bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_'); | |
6026 | ||
b655c310 | 6027 | for (tree t = lvl->names; t; t = TREE_CHAIN (t)) |
00e8de68 | 6028 | { |
b655c310 | 6029 | tree d = t; |
00e8de68 | 6030 | |
b655c310 NS |
6031 | /* OVERLOADs or decls from using declaration are wrapped into |
6032 | TREE_LIST. */ | |
6033 | if (TREE_CODE (d) == TREE_LIST) | |
87c976ae | 6034 | d = OVL_FIRST (TREE_VALUE (d)); |
00e8de68 | 6035 | |
b655c310 NS |
6036 | /* Don't use bindings from implicitly declared functions, |
6037 | as they were likely misspellings themselves. */ | |
6038 | if (TREE_TYPE (d) == error_mark_node) | |
6039 | continue; | |
00e8de68 | 6040 | |
b655c310 NS |
6041 | /* Skip anticipated decls of builtin functions. */ |
6042 | if (TREE_CODE (d) == FUNCTION_DECL | |
3d78e008 | 6043 | && fndecl_built_in_p (d) |
b655c310 NS |
6044 | && DECL_ANTICIPATED (d)) |
6045 | continue; | |
575bfb00 | 6046 | |
66bd3086 DM |
6047 | /* Skip compiler-generated variables (e.g. __for_begin/__for_end |
6048 | within range for). */ | |
6049 | if (TREE_CODE (d) == VAR_DECL | |
6050 | && DECL_ARTIFICIAL (d)) | |
6051 | continue; | |
6052 | ||
c79144f8 DM |
6053 | tree suggestion = DECL_NAME (d); |
6054 | if (!suggestion) | |
6055 | continue; | |
6056 | ||
5da1234b DM |
6057 | /* Don't suggest names that are for anonymous aggregate types, as |
6058 | they are an implementation detail generated by the compiler. */ | |
6059 | if (anon_aggrname_p (suggestion)) | |
6060 | continue; | |
6061 | ||
c79144f8 DM |
6062 | const char *suggestion_str = IDENTIFIER_POINTER (suggestion); |
6063 | ||
6064 | /* Ignore internal names with spaces in them. */ | |
6065 | if (strchr (suggestion_str, ' ')) | |
6066 | continue; | |
6067 | ||
6068 | /* Don't suggest names that are reserved for use by the | |
6069 | implementation, unless NAME began with an underscore. */ | |
6070 | if (name_reserved_for_implementation_p (suggestion_str) | |
6071 | && !consider_implementation_names) | |
6072 | continue; | |
6073 | ||
6074 | bm.consider (suggestion_str); | |
b655c310 | 6075 | } |
575bfb00 LC |
6076 | } |
6077 | ||
01ada121 DM |
6078 | /* Subclass of deferred_diagnostic. Notify the user that the |
6079 | given macro was used before it was defined. | |
6080 | This can be done in the C++ frontend since tokenization happens | |
6081 | upfront. */ | |
6082 | ||
6083 | class macro_use_before_def : public deferred_diagnostic | |
6084 | { | |
6085 | public: | |
b0cc1e53 DM |
6086 | /* Factory function. Return a new macro_use_before_def instance if |
6087 | appropriate, or return NULL. */ | |
6088 | static macro_use_before_def * | |
6089 | maybe_make (location_t use_loc, cpp_hashnode *macro) | |
6090 | { | |
620e594b | 6091 | location_t def_loc = cpp_macro_definition_location (macro); |
b0cc1e53 DM |
6092 | if (def_loc == UNKNOWN_LOCATION) |
6093 | return NULL; | |
6094 | ||
6095 | /* We only want to issue a note if the macro was used *before* it was | |
6096 | defined. | |
6097 | We don't want to issue a note for cases where a macro was incorrectly | |
6098 | used, leaving it unexpanded (e.g. by using the wrong argument | |
6099 | count). */ | |
6100 | if (!linemap_location_before_p (line_table, use_loc, def_loc)) | |
6101 | return NULL; | |
6102 | ||
6103 | return new macro_use_before_def (use_loc, macro); | |
6104 | } | |
6105 | ||
6106 | private: | |
01ada121 DM |
6107 | /* Ctor. LOC is the location of the usage. MACRO is the |
6108 | macro that was used. */ | |
6109 | macro_use_before_def (location_t loc, cpp_hashnode *macro) | |
6110 | : deferred_diagnostic (loc), m_macro (macro) | |
6111 | { | |
6112 | gcc_assert (macro); | |
6113 | } | |
6114 | ||
6115 | ~macro_use_before_def () | |
6116 | { | |
6117 | if (is_suppressed_p ()) | |
6118 | return; | |
6119 | ||
b0cc1e53 DM |
6120 | inform (get_location (), "the macro %qs had not yet been defined", |
6121 | (const char *)m_macro->ident.str); | |
6122 | inform (cpp_macro_definition_location (m_macro), | |
6123 | "it was later defined here"); | |
01ada121 DM |
6124 | } |
6125 | ||
6126 | private: | |
6127 | cpp_hashnode *m_macro; | |
6128 | }; | |
6129 | ||
0d7d8e66 DM |
6130 | /* Determine if it can ever make sense to offer RID as a suggestion for |
6131 | a misspelling. | |
6132 | ||
6133 | Subroutine of lookup_name_fuzzy. */ | |
6134 | ||
6135 | static bool | |
6136 | suggest_rid_p (enum rid rid) | |
6137 | { | |
6138 | switch (rid) | |
6139 | { | |
6140 | /* Support suggesting function-like keywords. */ | |
6141 | case RID_STATIC_ASSERT: | |
6142 | return true; | |
6143 | ||
6144 | default: | |
6145 | /* Support suggesting the various decl-specifier words, to handle | |
6146 | e.g. "singed" vs "signed" typos. */ | |
6147 | if (cp_keyword_starts_decl_specifier_p (rid)) | |
6148 | return true; | |
6149 | ||
6150 | /* Otherwise, don't offer it. This avoids suggesting e.g. "if" | |
6151 | and "do" for short misspellings, which are likely to lead to | |
6152 | nonsensical results. */ | |
6153 | return false; | |
6154 | } | |
6155 | } | |
01ada121 | 6156 | |
b655c310 NS |
6157 | /* Search for near-matches for NAME within the current bindings, and within |
6158 | macro names, returning the best match as a const char *, or NULL if | |
01ada121 DM |
6159 | no reasonable match is found. |
6160 | ||
6161 | Use LOC for any deferred diagnostics. */ | |
575bfb00 | 6162 | |
6c7a259b | 6163 | name_hint |
01ada121 | 6164 | lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) |
e8f43da6 | 6165 | { |
b655c310 | 6166 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
e8f43da6 | 6167 | |
26edace6 DM |
6168 | /* First, try some well-known names in the C++ standard library, in case |
6169 | the user forgot a #include. */ | |
6170 | const char *header_hint | |
6171 | = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name)); | |
6172 | if (header_hint) | |
6173 | return name_hint (NULL, | |
6174 | new suggest_missing_header (loc, | |
6175 | IDENTIFIER_POINTER (name), | |
6176 | header_hint)); | |
6177 | ||
b655c310 | 6178 | best_match <tree, const char *> bm (name); |
e8f43da6 | 6179 | |
b655c310 NS |
6180 | cp_binding_level *lvl; |
6181 | for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) | |
6182 | consider_binding_level (name, bm, lvl, true, kind); | |
e8f43da6 | 6183 | |
b655c310 NS |
6184 | for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) |
6185 | consider_binding_level (name, bm, lvl, false, kind); | |
e8f43da6 | 6186 | |
b655c310 NS |
6187 | /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" |
6188 | as: | |
6189 | x = SOME_OTHER_MACRO (y); | |
6190 | then "SOME_OTHER_MACRO" will survive to the frontend and show up | |
6191 | as a misspelled identifier. | |
e8f43da6 | 6192 | |
b655c310 NS |
6193 | Use the best distance so far so that a candidate is only set if |
6194 | a macro is better than anything so far. This allows early rejection | |
6195 | (without calculating the edit distance) of macro names that must have | |
6196 | distance >= bm.get_best_distance (), and means that we only get a | |
6197 | non-NULL result for best_macro_match if it's better than any of | |
6198 | the identifiers already checked. */ | |
6199 | best_macro_match bmm (name, bm.get_best_distance (), parse_in); | |
6200 | cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); | |
6201 | /* If a macro is the closest so far to NAME, consider it. */ | |
6202 | if (best_macro) | |
6203 | bm.consider ((const char *)best_macro->ident.str); | |
01ada121 DM |
6204 | else if (bmm.get_best_distance () == 0) |
6205 | { | |
b0cc1e53 DM |
6206 | /* If we have an exact match for a macro name, then either the |
6207 | macro was used with the wrong argument count, or the macro | |
6208 | has been used before it was defined. */ | |
3f6677f4 NS |
6209 | if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ()) |
6210 | if (cpp_user_macro_p (macro)) | |
6211 | return name_hint (NULL, | |
6212 | macro_use_before_def::maybe_make (loc, macro)); | |
01ada121 | 6213 | } |
00e8de68 | 6214 | |
b655c310 NS |
6215 | /* Try the "starts_decl_specifier_p" keywords to detect |
6216 | "singed" vs "signed" typos. */ | |
6217 | for (unsigned i = 0; i < num_c_common_reswords; i++) | |
6218 | { | |
6219 | const c_common_resword *resword = &c_common_reswords[i]; | |
00e8de68 | 6220 | |
0d7d8e66 DM |
6221 | if (!suggest_rid_p (resword->rid)) |
6222 | continue; | |
00e8de68 | 6223 | |
b655c310 NS |
6224 | tree resword_identifier = ridpointers [resword->rid]; |
6225 | if (!resword_identifier) | |
6226 | continue; | |
6227 | gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); | |
00e8de68 | 6228 | |
b655c310 NS |
6229 | /* Only consider reserved words that survived the |
6230 | filtering in init_reswords (e.g. for -std). */ | |
84c0088f | 6231 | if (!IDENTIFIER_KEYWORD_P (resword_identifier)) |
b655c310 | 6232 | continue; |
00e8de68 | 6233 | |
b655c310 NS |
6234 | bm.consider (IDENTIFIER_POINTER (resword_identifier)); |
6235 | } | |
6236 | ||
6c7a259b | 6237 | return name_hint (bm.get_best_meaningful_candidate (), NULL); |
b655c310 | 6238 | } |
5a167978 | 6239 | |
b655c310 | 6240 | /* Subroutine of outer_binding. |
5a167978 | 6241 | |
b655c310 NS |
6242 | Returns TRUE if BINDING is a binding to a template parameter of |
6243 | SCOPE. In that case SCOPE is the scope of a primary template | |
6244 | parameter -- in the sense of G++, i.e, a template that has its own | |
6245 | template header. | |
5a167978 | 6246 | |
b655c310 | 6247 | Returns FALSE otherwise. */ |
5a167978 GDR |
6248 | |
6249 | static bool | |
b655c310 NS |
6250 | binding_to_template_parms_of_scope_p (cxx_binding *binding, |
6251 | cp_binding_level *scope) | |
5a167978 | 6252 | { |
b655c310 NS |
6253 | tree binding_value, tmpl, tinfo; |
6254 | int level; | |
5a167978 | 6255 | |
b655c310 NS |
6256 | if (!binding || !scope || !scope->this_entity) |
6257 | return false; | |
6258 | ||
6259 | binding_value = binding->value ? binding->value : binding->type; | |
6260 | tinfo = get_template_info (scope->this_entity); | |
6261 | ||
6262 | /* BINDING_VALUE must be a template parm. */ | |
6263 | if (binding_value == NULL_TREE | |
6264 | || (!DECL_P (binding_value) | |
6265 | || !DECL_TEMPLATE_PARM_P (binding_value))) | |
6266 | return false; | |
6267 | ||
6268 | /* The level of BINDING_VALUE. */ | |
6269 | level = | |
6270 | template_type_parameter_p (binding_value) | |
6271 | ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX | |
6272 | (TREE_TYPE (binding_value))) | |
6273 | : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); | |
6274 | ||
6275 | /* The template of the current scope, iff said scope is a primary | |
6276 | template. */ | |
6277 | tmpl = (tinfo | |
6278 | && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
6279 | ? TI_TEMPLATE (tinfo) | |
6280 | : NULL_TREE); | |
6281 | ||
6282 | /* If the level of the parm BINDING_VALUE equals the depth of TMPL, | |
6283 | then BINDING_VALUE is a parameter of TMPL. */ | |
6284 | return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
5a167978 GDR |
6285 | } |
6286 | ||
b655c310 NS |
6287 | /* Return the innermost non-namespace binding for NAME from a scope |
6288 | containing BINDING, or, if BINDING is NULL, the current scope. | |
6289 | Please note that for a given template, the template parameters are | |
6290 | considered to be in the scope containing the current scope. | |
6291 | If CLASS_P is false, then class bindings are ignored. */ | |
86098eb8 | 6292 | |
b655c310 NS |
6293 | cxx_binding * |
6294 | outer_binding (tree name, | |
6295 | cxx_binding *binding, | |
6296 | bool class_p) | |
86098eb8 | 6297 | { |
b655c310 NS |
6298 | cxx_binding *outer; |
6299 | cp_binding_level *scope; | |
6300 | cp_binding_level *outer_scope; | |
d4ccba66 | 6301 | |
b655c310 | 6302 | if (binding) |
86098eb8 | 6303 | { |
b655c310 NS |
6304 | scope = binding->scope->level_chain; |
6305 | outer = binding->previous; | |
6306 | } | |
6307 | else | |
6308 | { | |
6309 | scope = current_binding_level; | |
6310 | outer = IDENTIFIER_BINDING (name); | |
86098eb8 | 6311 | } |
b655c310 | 6312 | outer_scope = outer ? outer->scope : NULL; |
d4ccba66 | 6313 | |
b655c310 NS |
6314 | /* Because we create class bindings lazily, we might be missing a |
6315 | class binding for NAME. If there are any class binding levels | |
6316 | between the LAST_BINDING_LEVEL and the scope in which OUTER was | |
6317 | declared, we must lookup NAME in those class scopes. */ | |
6318 | if (class_p) | |
6319 | while (scope && scope != outer_scope && scope->kind != sk_namespace) | |
6320 | { | |
6321 | if (scope->kind == sk_class) | |
6322 | { | |
6323 | cxx_binding *class_binding; | |
d4ccba66 | 6324 | |
b655c310 NS |
6325 | class_binding = get_class_binding (name, scope); |
6326 | if (class_binding) | |
6327 | { | |
6328 | /* Thread this new class-scope binding onto the | |
6329 | IDENTIFIER_BINDING list so that future lookups | |
6330 | find it quickly. */ | |
6331 | class_binding->previous = outer; | |
6332 | if (binding) | |
6333 | binding->previous = class_binding; | |
6334 | else | |
6335 | IDENTIFIER_BINDING (name) = class_binding; | |
6336 | return class_binding; | |
6337 | } | |
6338 | } | |
6339 | /* If we are in a member template, the template parms of the member | |
6340 | template are considered to be inside the scope of the containing | |
6341 | class, but within G++ the class bindings are all pushed between the | |
6342 | template parms and the function body. So if the outer binding is | |
6343 | a template parm for the current scope, return it now rather than | |
6344 | look for a class binding. */ | |
6345 | if (outer_scope && outer_scope->kind == sk_template_parms | |
6346 | && binding_to_template_parms_of_scope_p (outer, scope)) | |
6347 | return outer; | |
6348 | ||
6349 | scope = scope->level_chain; | |
6350 | } | |
6351 | ||
6352 | return outer; | |
86098eb8 JM |
6353 | } |
6354 | ||
b655c310 NS |
6355 | /* Return the innermost block-scope or class-scope value binding for |
6356 | NAME, or NULL_TREE if there is no such binding. */ | |
5a167978 | 6357 | |
b655c310 NS |
6358 | tree |
6359 | innermost_non_namespace_value (tree name) | |
5a167978 | 6360 | { |
b655c310 NS |
6361 | cxx_binding *binding; |
6362 | binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); | |
6363 | return binding ? binding->value : NULL_TREE; | |
6364 | } | |
5a167978 | 6365 | |
b655c310 NS |
6366 | /* Look up NAME in the current binding level and its superiors in the |
6367 | namespace of variables, functions and typedefs. Return a ..._DECL | |
6368 | node of some kind representing its definition if there is only one | |
6369 | such declaration, or return a TREE_LIST with all the overloaded | |
6370 | definitions if there are many, or return 0 if it is undefined. | |
6371 | Hidden name, either friend declaration or built-in function, are | |
6372 | not ignored. | |
86098eb8 | 6373 | |
b655c310 NS |
6374 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
6375 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
6376 | Otherwise we prefer non-TYPE_DECLs. | |
c8094d83 | 6377 | |
b655c310 NS |
6378 | If NONCLASS is nonzero, bindings in class scopes are ignored. If |
6379 | BLOCK_P is false, bindings in block scopes are ignored. */ | |
4cfaec1c | 6380 | |
b655c310 NS |
6381 | static tree |
6382 | lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, | |
6383 | int namespaces_only, int flags) | |
6384 | { | |
6385 | cxx_binding *iter; | |
6386 | tree val = NULL_TREE; | |
5a167978 | 6387 | |
b655c310 NS |
6388 | query_oracle (name); |
6389 | ||
6390 | /* Conversion operators are handled specially because ordinary | |
6391 | unqualified name lookup will not find template conversion | |
6392 | operators. */ | |
84c0088f | 6393 | if (IDENTIFIER_CONV_OP_P (name)) |
d63d5d0c | 6394 | { |
b655c310 | 6395 | cp_binding_level *level; |
d63d5d0c | 6396 | |
b655c310 NS |
6397 | for (level = current_binding_level; |
6398 | level && level->kind != sk_namespace; | |
6399 | level = level->level_chain) | |
6400 | { | |
6401 | tree class_type; | |
6402 | tree operators; | |
6403 | ||
6404 | /* A conversion operator can only be declared in a class | |
6405 | scope. */ | |
6406 | if (level->kind != sk_class) | |
6407 | continue; | |
6408 | ||
6409 | /* Lookup the conversion operator in the class. */ | |
6410 | class_type = level->this_entity; | |
6411 | operators = lookup_fnfields (class_type, name, /*protect=*/0); | |
6412 | if (operators) | |
6413 | return operators; | |
6414 | } | |
6415 | ||
6416 | return NULL_TREE; | |
d63d5d0c | 6417 | } |
c8094d83 | 6418 | |
b655c310 NS |
6419 | flags |= lookup_flags (prefer_type, namespaces_only); |
6420 | ||
6421 | /* First, look in non-namespace scopes. */ | |
6422 | ||
6423 | if (current_class_type == NULL_TREE) | |
6424 | nonclass = 1; | |
5a167978 | 6425 | |
b655c310 NS |
6426 | if (block_p || !nonclass) |
6427 | for (iter = outer_binding (name, NULL, !nonclass); | |
6428 | iter; | |
6429 | iter = outer_binding (name, iter, !nonclass)) | |
6430 | { | |
6431 | tree binding; | |
5a167978 | 6432 | |
b655c310 NS |
6433 | /* Skip entities we don't want. */ |
6434 | if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) | |
6435 | continue; | |
5a167978 | 6436 | |
b655c310 NS |
6437 | /* If this is the kind of thing we're looking for, we're done. */ |
6438 | if (qualify_lookup (iter->value, flags)) | |
6439 | binding = iter->value; | |
6440 | else if ((flags & LOOKUP_PREFER_TYPES) | |
6441 | && qualify_lookup (iter->type, flags)) | |
6442 | binding = iter->type; | |
6443 | else | |
6444 | binding = NULL_TREE; | |
5a167978 | 6445 | |
b655c310 NS |
6446 | if (binding) |
6447 | { | |
ef4c5e78 | 6448 | if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding)) |
b655c310 NS |
6449 | { |
6450 | /* A non namespace-scope binding can only be hidden in the | |
6451 | presence of a local class, due to friend declarations. | |
5a167978 | 6452 | |
b655c310 | 6453 | In particular, consider: |
ba796308 | 6454 | |
b655c310 NS |
6455 | struct C; |
6456 | void f() { | |
6457 | struct A { | |
6458 | friend struct B; | |
6459 | friend struct C; | |
6460 | void g() { | |
6461 | B* b; // error: B is hidden | |
6462 | C* c; // OK, finds ::C | |
6463 | } | |
6464 | }; | |
6465 | B *b; // error: B is hidden | |
6466 | C *c; // OK, finds ::C | |
6467 | struct B {}; | |
6468 | B *bb; // OK | |
6469 | } | |
5a167978 | 6470 | |
b655c310 NS |
6471 | The standard says that "B" is a local class in "f" |
6472 | (but not nested within "A") -- but that name lookup | |
6473 | for "B" does not find this declaration until it is | |
6474 | declared directly with "f". | |
5a167978 | 6475 | |
b655c310 | 6476 | In particular: |
c8094d83 | 6477 | |
b655c310 | 6478 | [class.friend] |
5a167978 | 6479 | |
b655c310 NS |
6480 | If a friend declaration appears in a local class and |
6481 | the name specified is an unqualified name, a prior | |
6482 | declaration is looked up without considering scopes | |
6483 | that are outside the innermost enclosing non-class | |
6484 | scope. For a friend function declaration, if there is | |
6485 | no prior declaration, the program is ill-formed. For a | |
6486 | friend class declaration, if there is no prior | |
6487 | declaration, the class that is specified belongs to the | |
6488 | innermost enclosing non-class scope, but if it is | |
6489 | subsequently referenced, its name is not found by name | |
6490 | lookup until a matching declaration is provided in the | |
6491 | innermost enclosing nonclass scope. | |
ccb14335 | 6492 | |
b655c310 NS |
6493 | So just keep looking for a non-hidden binding. |
6494 | */ | |
6495 | gcc_assert (TREE_CODE (binding) == TYPE_DECL); | |
6496 | continue; | |
6497 | } | |
6498 | val = binding; | |
6499 | break; | |
6500 | } | |
6501 | } | |
2395cd2e | 6502 | |
b655c310 NS |
6503 | /* Now lookup in namespace scopes. */ |
6504 | if (!val) | |
932f48ac NS |
6505 | { |
6506 | name_lookup lookup (name, flags); | |
6507 | if (lookup.search_unqualified | |
6508 | (current_decl_namespace (), current_binding_level)) | |
6509 | val = lookup.value; | |
6510 | } | |
c8b2e872 | 6511 | |
b655c310 NS |
6512 | /* If we have a single function from a using decl, pull it out. */ |
6513 | if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) | |
6514 | val = OVL_FUNCTION (val); | |
6515 | ||
6516 | return val; | |
db10df3d JM |
6517 | } |
6518 | ||
b655c310 | 6519 | /* Wrapper for lookup_name_real_1. */ |
db10df3d | 6520 | |
b655c310 NS |
6521 | tree |
6522 | lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, | |
6523 | int namespaces_only, int flags) | |
db10df3d | 6524 | { |
b655c310 NS |
6525 | tree ret; |
6526 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6527 | ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, | |
6528 | namespaces_only, flags); | |
6529 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6530 | return ret; | |
6531 | } | |
db10df3d | 6532 | |
b655c310 NS |
6533 | tree |
6534 | lookup_name_nonclass (tree name) | |
6535 | { | |
6536 | return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); | |
db10df3d JM |
6537 | } |
6538 | ||
b655c310 NS |
6539 | tree |
6540 | lookup_name (tree name) | |
6541 | { | |
6542 | return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); | |
6543 | } | |
db10df3d | 6544 | |
b655c310 NS |
6545 | tree |
6546 | lookup_name_prefer_type (tree name, int prefer_type) | |
db10df3d | 6547 | { |
b655c310 NS |
6548 | return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); |
6549 | } | |
db10df3d | 6550 | |
b655c310 NS |
6551 | /* Look up NAME for type used in elaborated name specifier in |
6552 | the scopes given by SCOPE. SCOPE can be either TS_CURRENT or | |
6553 | TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the | |
6554 | name, more scopes are checked if cleanup or template parameter | |
6555 | scope is encountered. | |
db10df3d | 6556 | |
b655c310 NS |
6557 | Unlike lookup_name_real, we make sure that NAME is actually |
6558 | declared in the desired scope, not from inheritance, nor using | |
6559 | directive. For using declaration, there is DR138 still waiting | |
6560 | to be resolved. Hidden name coming from an earlier friend | |
6561 | declaration is also returned. | |
db10df3d | 6562 | |
b655c310 NS |
6563 | A TYPE_DECL best matching the NAME is returned. Catching error |
6564 | and issuing diagnostics are caller's responsibility. */ | |
db10df3d | 6565 | |
b655c310 NS |
6566 | static tree |
6567 | lookup_type_scope_1 (tree name, tag_scope scope) | |
6568 | { | |
6569 | cxx_binding *iter = NULL; | |
6570 | tree val = NULL_TREE; | |
3c9cca88 | 6571 | cp_binding_level *level = NULL; |
db10df3d | 6572 | |
b655c310 NS |
6573 | /* Look in non-namespace scope first. */ |
6574 | if (current_binding_level->kind != sk_namespace) | |
6575 | iter = outer_binding (name, NULL, /*class_p=*/ true); | |
6576 | for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) | |
5a167978 | 6577 | { |
b655c310 NS |
6578 | /* Check if this is the kind of thing we're looking for. |
6579 | If SCOPE is TS_CURRENT, also make sure it doesn't come from | |
6580 | base class. For ITER->VALUE, we can simply use | |
6581 | INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use | |
6582 | our own check. | |
5a167978 | 6583 | |
b655c310 NS |
6584 | We check ITER->TYPE before ITER->VALUE in order to handle |
6585 | typedef struct C {} C; | |
6586 | correctly. */ | |
5a167978 | 6587 | |
b655c310 NS |
6588 | if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) |
6589 | && (scope != ts_current | |
6590 | || LOCAL_BINDING_P (iter) | |
6591 | || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) | |
6592 | val = iter->type; | |
6593 | else if ((scope != ts_current | |
6594 | || !INHERITED_VALUE_BINDING_P (iter)) | |
6595 | && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) | |
6596 | val = iter->value; | |
5a167978 | 6597 | |
b655c310 NS |
6598 | if (val) |
6599 | break; | |
6600 | } | |
5a167978 | 6601 | |
b655c310 | 6602 | /* Look in namespace scope. */ |
3c9cca88 NS |
6603 | if (val) |
6604 | level = iter->scope; | |
6605 | else | |
5a167978 | 6606 | { |
3c9cca88 | 6607 | tree ns = current_decl_namespace (); |
b655c310 | 6608 | |
3c9cca88 | 6609 | if (tree *slot = find_namespace_slot (ns, name)) |
b655c310 NS |
6610 | { |
6611 | /* If this is the kind of thing we're looking for, we're done. */ | |
3c9cca88 NS |
6612 | if (tree type = MAYBE_STAT_TYPE (*slot)) |
6613 | if (qualify_lookup (type, LOOKUP_PREFER_TYPES)) | |
6614 | val = type; | |
6615 | if (!val) | |
6616 | { | |
6617 | if (tree decl = MAYBE_STAT_DECL (*slot)) | |
6618 | if (qualify_lookup (decl, LOOKUP_PREFER_TYPES)) | |
6619 | val = decl; | |
6620 | } | |
6621 | level = NAMESPACE_LEVEL (ns); | |
b655c310 | 6622 | } |
5a167978 | 6623 | } |
b655c310 NS |
6624 | |
6625 | /* Type found, check if it is in the allowed scopes, ignoring cleanup | |
6626 | and template parameter scopes. */ | |
6627 | if (val) | |
5a167978 | 6628 | { |
b655c310 NS |
6629 | cp_binding_level *b = current_binding_level; |
6630 | while (b) | |
6631 | { | |
3c9cca88 | 6632 | if (level == b) |
b655c310 | 6633 | return val; |
5d80a306 | 6634 | |
b655c310 NS |
6635 | if (b->kind == sk_cleanup || b->kind == sk_template_parms |
6636 | || b->kind == sk_function_parms) | |
6637 | b = b->level_chain; | |
6638 | else if (b->kind == sk_class | |
6639 | && scope == ts_within_enclosing_non_class) | |
6640 | b = b->level_chain; | |
6641 | else | |
6642 | break; | |
6643 | } | |
5a167978 | 6644 | } |
5a167978 | 6645 | |
b655c310 | 6646 | return NULL_TREE; |
5a167978 | 6647 | } |
b655c310 NS |
6648 | |
6649 | /* Wrapper for lookup_type_scope_1. */ | |
5a167978 | 6650 | |
b655c310 NS |
6651 | tree |
6652 | lookup_type_scope (tree name, tag_scope scope) | |
c166b898 | 6653 | { |
b655c310 NS |
6654 | tree ret; |
6655 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6656 | ret = lookup_type_scope_1 (name, scope); | |
6657 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6658 | return ret; | |
c166b898 ILT |
6659 | } |
6660 | ||
b655c310 NS |
6661 | /* Returns true iff DECL is a block-scope extern declaration of a function |
6662 | or variable. */ | |
6663 | ||
6664 | bool | |
6665 | is_local_extern (tree decl) | |
5a167978 | 6666 | { |
b655c310 | 6667 | cxx_binding *binding; |
5a167978 | 6668 | |
b655c310 NS |
6669 | /* For functions, this is easy. */ |
6670 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
6671 | return DECL_LOCAL_FUNCTION_P (decl); | |
d63d5d0c | 6672 | |
b655c310 NS |
6673 | if (!VAR_P (decl)) |
6674 | return false; | |
6675 | if (!current_function_decl) | |
6676 | return false; | |
5a167978 | 6677 | |
b655c310 NS |
6678 | /* For variables, this is not easy. We need to look at the binding stack |
6679 | for the identifier to see whether the decl we have is a local. */ | |
6680 | for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); | |
6681 | binding && binding->scope->kind != sk_namespace; | |
6682 | binding = binding->previous) | |
6683 | if (binding->value == decl) | |
6684 | return LOCAL_BINDING_P (binding); | |
5a167978 | 6685 | |
b655c310 NS |
6686 | return false; |
6687 | } | |
ca1085f0 | 6688 | |
00e8de68 GDR |
6689 | /* The type TYPE is being declared. If it is a class template, or a |
6690 | specialization of a class template, do any processing required and | |
6691 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is | |
6692 | being declared a friend. B is the binding level at which this TYPE | |
6693 | should be bound. | |
6694 | ||
6695 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
6696 | processing. */ | |
6697 | ||
6698 | static tree | |
bd3d082e | 6699 | maybe_process_template_type_declaration (tree type, int is_friend, |
2c140474 | 6700 | cp_binding_level *b) |
00e8de68 GDR |
6701 | { |
6702 | tree decl = TYPE_NAME (type); | |
6703 | ||
6704 | if (processing_template_parmlist) | |
6705 | /* You can't declare a new template type in a template parameter | |
6706 | list. But, you can declare a non-template type: | |
6707 | ||
0cbd7506 | 6708 | template <class A*> struct S; |
00e8de68 GDR |
6709 | |
6710 | is a forward-declaration of `A'. */ | |
6711 | ; | |
c43e95f8 MM |
6712 | else if (b->kind == sk_namespace |
6713 | && current_binding_level->kind != sk_namespace) | |
6714 | /* If this new type is being injected into a containing scope, | |
6715 | then it's not a template type. */ | |
6716 | ; | |
00e8de68 GDR |
6717 | else |
6718 | { | |
9e1e64ec PC |
6719 | gcc_assert (MAYBE_CLASS_TYPE_P (type) |
6720 | || TREE_CODE (type) == ENUMERAL_TYPE); | |
00e8de68 GDR |
6721 | |
6722 | if (processing_template_decl) | |
6723 | { | |
6724 | /* This may change after the call to | |
6725 | push_template_decl_real, but we want the original value. */ | |
6726 | tree name = DECL_NAME (decl); | |
6727 | ||
bd3d082e | 6728 | decl = push_template_decl_real (decl, is_friend); |
79faac54 PC |
6729 | if (decl == error_mark_node) |
6730 | return error_mark_node; | |
6731 | ||
00e8de68 GDR |
6732 | /* If the current binding level is the binding level for the |
6733 | template parameters (see the comment in | |
6734 | begin_template_parm_list) and the enclosing level is a class | |
6735 | scope, and we're not looking at a friend, push the | |
6736 | declaration of the member class into the class scope. In the | |
6737 | friend case, push_template_decl will already have put the | |
6738 | friend into global scope, if appropriate. */ | |
6739 | if (TREE_CODE (type) != ENUMERAL_TYPE | |
bd3d082e | 6740 | && !is_friend && b->kind == sk_template_parms |
00e8de68 GDR |
6741 | && b->level_chain->kind == sk_class) |
6742 | { | |
6743 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); | |
e57df6fe | 6744 | |
00e8de68 GDR |
6745 | if (!COMPLETE_TYPE_P (current_class_type)) |
6746 | { | |
6747 | maybe_add_class_template_decl_list (current_class_type, | |
6748 | type, /*friend_p=*/0); | |
c72a1a86 | 6749 | /* Put this UTD in the table of UTDs for the class. */ |
e57df6fe KL |
6750 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6751 | CLASSTYPE_NESTED_UTDS (current_class_type) = | |
6752 | binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
6753 | ||
6754 | binding_table_insert | |
6755 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 GDR |
6756 | } |
6757 | } | |
6758 | } | |
6759 | } | |
6760 | ||
6761 | return decl; | |
6762 | } | |
6763 | ||
5a24482e KL |
6764 | /* Push a tag name NAME for struct/class/union/enum type TYPE. In case |
6765 | that the NAME is a class template, the tag is processed but not pushed. | |
6766 | ||
6767 | The pushed scope depend on the SCOPE parameter: | |
6768 | - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup | |
6769 | scope. | |
6770 | - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and | |
6771 | non-template-parameter scope. This case is needed for forward | |
6772 | declarations. | |
6773 | - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to | |
6774 | TS_GLOBAL case except that names within template-parameter scopes | |
6775 | are not pushed at all. | |
6776 | ||
c6f9f83b | 6777 | Returns TYPE upon success and ERROR_MARK_NODE otherwise. */ |
00e8de68 | 6778 | |
575bfb00 | 6779 | static tree |
5256a7f5 | 6780 | do_pushtag (tree name, tree type, tag_scope scope) |
00e8de68 | 6781 | { |
6a000704 | 6782 | tree decl; |
00e8de68 | 6783 | |
5256a7f5 | 6784 | cp_binding_level *b = current_binding_level; |
8945521a JM |
6785 | while (true) |
6786 | { | |
6787 | if (/* Cleanup scopes are not scopes from the point of view of | |
6788 | the language. */ | |
6789 | b->kind == sk_cleanup | |
6790 | /* Neither are function parameter scopes. */ | |
6791 | || b->kind == sk_function_parms | |
6792 | /* Neither are the scopes used to hold template parameters | |
6793 | for an explicit specialization. For an ordinary template | |
6794 | declaration, these scopes are not scopes from the point of | |
6795 | view of the language. */ | |
6796 | || (b->kind == sk_template_parms | |
6797 | && (b->explicit_spec_p || scope == ts_global))) | |
6798 | b = b->level_chain; | |
6799 | else if (b->kind == sk_class | |
6800 | && scope != ts_current) | |
6801 | { | |
6802 | b = b->level_chain; | |
6803 | if (b->kind == sk_template_parms) | |
6804 | b = b->level_chain; | |
6805 | } | |
6806 | else | |
6807 | break; | |
6808 | } | |
00e8de68 | 6809 | |
9dc6f476 | 6810 | gcc_assert (identifier_p (name)); |
3db45ab5 | 6811 | |
6a000704 | 6812 | /* Do C++ gratuitous typedefing. */ |
575bfb00 | 6813 | if (identifier_type_value_1 (name) != type) |
00e8de68 | 6814 | { |
6a000704 NS |
6815 | tree tdef; |
6816 | int in_class = 0; | |
6817 | tree context = TYPE_CONTEXT (type); | |
00e8de68 | 6818 | |
6a000704 NS |
6819 | if (! context) |
6820 | { | |
fe7a23a6 JM |
6821 | cp_binding_level *cb = b; |
6822 | while (cb->kind != sk_namespace | |
6823 | && cb->kind != sk_class | |
6824 | && (cb->kind != sk_function_parms | |
6825 | || !cb->this_entity)) | |
6826 | cb = cb->level_chain; | |
6827 | tree cs = cb->this_entity; | |
6828 | ||
6829 | gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL | |
6830 | ? cs == current_function_decl | |
6831 | : TYPE_P (cs) ? cs == current_class_type | |
6832 | : cs == current_namespace); | |
582f844c | 6833 | |
d5f4eddd JM |
6834 | if (scope == ts_current |
6835 | || (cs && TREE_CODE (cs) == FUNCTION_DECL)) | |
6a000704 | 6836 | context = cs; |
c443f3d5 | 6837 | else if (cs && TYPE_P (cs)) |
6a000704 NS |
6838 | /* When declaring a friend class of a local class, we want |
6839 | to inject the newly named class into the scope | |
6840 | containing the local class, not the namespace | |
6841 | scope. */ | |
6842 | context = decl_function_context (get_type_decl (cs)); | |
6843 | } | |
6844 | if (!context) | |
6845 | context = current_namespace; | |
bd3d082e | 6846 | |
6a000704 NS |
6847 | if (b->kind == sk_class |
6848 | || (b->kind == sk_template_parms | |
6849 | && b->level_chain->kind == sk_class)) | |
6850 | in_class = 1; | |
00e8de68 | 6851 | |
6a000704 NS |
6852 | tdef = create_implicit_typedef (name, type); |
6853 | DECL_CONTEXT (tdef) = FROB_CONTEXT (context); | |
6854 | if (scope == ts_within_enclosing_non_class) | |
00e8de68 | 6855 | { |
6a000704 NS |
6856 | /* This is a friend. Make this TYPE_DECL node hidden from |
6857 | ordinary name lookup. Its corresponding TEMPLATE_DECL | |
6858 | will be marked in push_template_decl_real. */ | |
6859 | retrofit_lang_decl (tdef); | |
6860 | DECL_ANTICIPATED (tdef) = 1; | |
6861 | DECL_FRIEND_P (tdef) = 1; | |
6862 | } | |
e57df6fe | 6863 | |
6a000704 NS |
6864 | decl = maybe_process_template_type_declaration |
6865 | (type, scope == ts_within_enclosing_non_class, b); | |
6866 | if (decl == error_mark_node) | |
575bfb00 | 6867 | return decl; |
3db45ab5 | 6868 | |
6a000704 NS |
6869 | if (b->kind == sk_class) |
6870 | { | |
fe7a23a6 JM |
6871 | if (!TYPE_BEING_DEFINED (current_class_type)) |
6872 | /* Don't push anywhere if the class is complete; a lambda in an | |
6873 | NSDMI is not a member of the class. */ | |
6874 | ; | |
6875 | else if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
6a000704 NS |
6876 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
6877 | class. But if it's a member template class, we want | |
6878 | the TEMPLATE_DECL, not the TYPE_DECL, so this is done | |
6879 | later. */ | |
6880 | finish_member_declaration (decl); | |
6881 | else | |
6882 | pushdecl_class_level (decl); | |
00e8de68 | 6883 | } |
6a000704 | 6884 | else if (b->kind != sk_template_parms) |
c5f8391c | 6885 | { |
5256a7f5 | 6886 | decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false); |
c5f8391c | 6887 | if (decl == error_mark_node) |
575bfb00 | 6888 | return decl; |
d3e19c2c PC |
6889 | |
6890 | if (DECL_CONTEXT (decl) == std_node | |
ad9870f2 | 6891 | && init_list_identifier == DECL_NAME (TYPE_NAME (type)) |
d3e19c2c PC |
6892 | && !CLASSTYPE_TEMPLATE_INFO (type)) |
6893 | { | |
61f84e25 | 6894 | error ("declaration of %<std::initializer_list%> does not match " |
93964ebd | 6895 | "%<#include <initializer_list>%>, isn%'t a template"); |
d3e19c2c PC |
6896 | return error_mark_node; |
6897 | } | |
c5f8391c | 6898 | } |
6a000704 | 6899 | |
dc3ca06f SM |
6900 | if (! in_class) |
6901 | set_identifier_type_value_with_scope (name, tdef, b); | |
6902 | ||
6a000704 NS |
6903 | TYPE_CONTEXT (type) = DECL_CONTEXT (decl); |
6904 | ||
6905 | /* If this is a local class, keep track of it. We need this | |
6906 | information for name-mangling, and so that it is possible to | |
6907 | find all function definitions in a translation unit in a | |
6908 | convenient way. (It's otherwise tricky to find a member | |
6909 | function definition it's only pointed to from within a local | |
6910 | class.) */ | |
9ededfc5 | 6911 | if (TYPE_FUNCTION_SCOPE_P (type)) |
fdaf2f48 JM |
6912 | { |
6913 | if (processing_template_decl) | |
6914 | { | |
6915 | /* Push a DECL_EXPR so we call pushtag at the right time in | |
6916 | template instantiation rather than in some nested context. */ | |
6917 | add_decl_expr (decl); | |
6918 | } | |
373d1f5f JM |
6919 | /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */ |
6920 | else if (!LAMBDA_TYPE_P (type)) | |
41f927f4 | 6921 | determine_local_discriminator (TYPE_NAME (type)); |
fdaf2f48 | 6922 | } |
00e8de68 | 6923 | } |
c443f3d5 | 6924 | |
6a000704 NS |
6925 | if (b->kind == sk_class |
6926 | && !COMPLETE_TYPE_P (current_class_type)) | |
00e8de68 | 6927 | { |
6a000704 NS |
6928 | maybe_add_class_template_decl_list (current_class_type, |
6929 | type, /*friend_p=*/0); | |
3db45ab5 | 6930 | |
6a000704 NS |
6931 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6932 | CLASSTYPE_NESTED_UTDS (current_class_type) | |
6933 | = binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
3db45ab5 | 6934 | |
6a000704 NS |
6935 | binding_table_insert |
6936 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 | 6937 | } |
6a000704 NS |
6938 | |
6939 | decl = TYPE_NAME (type); | |
6940 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); | |
6a000704 | 6941 | |
b9e75696 JM |
6942 | /* Set type visibility now if this is a forward declaration. */ |
6943 | TREE_PUBLIC (decl) = 1; | |
6944 | determine_visibility (decl); | |
6945 | ||
575bfb00 LC |
6946 | return type; |
6947 | } | |
6948 | ||
5256a7f5 | 6949 | /* Wrapper for do_pushtag. */ |
575bfb00 LC |
6950 | |
6951 | tree | |
6952 | pushtag (tree name, tree type, tag_scope scope) | |
6953 | { | |
6954 | tree ret; | |
6955 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5256a7f5 | 6956 | ret = do_pushtag (name, type, scope); |
575bfb00 LC |
6957 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
6958 | return ret; | |
00e8de68 | 6959 | } |
8db29d88 | 6960 | |
00e8de68 | 6961 | \f |
00e8de68 GDR |
6962 | /* Subroutines for reverting temporarily to top-level for instantiation |
6963 | of templates and such. We actually need to clear out the class- and | |
6964 | local-value slots of all identifiers, so that only the global values | |
6965 | are at all visible. Simply setting current_binding_level to the global | |
6966 | scope isn't enough, because more binding levels may be pushed. */ | |
6967 | struct saved_scope *scope_chain; | |
6968 | ||
71f15f31 RG |
6969 | /* Return true if ID has not already been marked. */ |
6970 | ||
6971 | static inline bool | |
6972 | store_binding_p (tree id) | |
6973 | { | |
6974 | if (!id || !IDENTIFIER_BINDING (id)) | |
6975 | return false; | |
6976 | ||
6977 | if (IDENTIFIER_MARKED (id)) | |
6978 | return false; | |
6979 | ||
6980 | return true; | |
6981 | } | |
6982 | ||
6983 | /* Add an appropriate binding to *OLD_BINDINGS which needs to already | |
6984 | have enough space reserved. */ | |
89b578be | 6985 | |
f44b0c8e | 6986 | static void |
9771b263 | 6987 | store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings) |
89b578be | 6988 | { |
f32682ca | 6989 | cxx_saved_binding saved; |
89b578be | 6990 | |
71f15f31 | 6991 | gcc_checking_assert (store_binding_p (id)); |
c8094d83 | 6992 | |
f44b0c8e | 6993 | IDENTIFIER_MARKED (id) = 1; |
89b578be | 6994 | |
f32682ca DN |
6995 | saved.identifier = id; |
6996 | saved.binding = IDENTIFIER_BINDING (id); | |
6997 | saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
9771b263 | 6998 | (*old_bindings)->quick_push (saved); |
89b578be | 6999 | IDENTIFIER_BINDING (id) = NULL; |
89b578be MM |
7000 | } |
7001 | ||
f44b0c8e | 7002 | static void |
9771b263 | 7003 | store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings) |
00e8de68 | 7004 | { |
199d1d48 | 7005 | static vec<tree> bindings_need_stored; |
71f15f31 RG |
7006 | tree t, id; |
7007 | size_t i; | |
00e8de68 | 7008 | |
575bfb00 | 7009 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
00e8de68 GDR |
7010 | for (t = names; t; t = TREE_CHAIN (t)) |
7011 | { | |
00e8de68 GDR |
7012 | if (TREE_CODE (t) == TREE_LIST) |
7013 | id = TREE_PURPOSE (t); | |
7014 | else | |
7015 | id = DECL_NAME (t); | |
7016 | ||
71f15f31 | 7017 | if (store_binding_p (id)) |
9771b263 | 7018 | bindings_need_stored.safe_push (id); |
71f15f31 | 7019 | } |
9771b263 | 7020 | if (!bindings_need_stored.is_empty ()) |
71f15f31 | 7021 | { |
9771b263 DN |
7022 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
7023 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 7024 | { |
5764ee3c | 7025 | /* We can apparently have duplicates in NAMES. */ |
71f15f31 RG |
7026 | if (store_binding_p (id)) |
7027 | store_binding (id, old_bindings); | |
7028 | } | |
9771b263 | 7029 | bindings_need_stored.truncate (0); |
00e8de68 | 7030 | } |
575bfb00 | 7031 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
00e8de68 GDR |
7032 | } |
7033 | ||
89b578be MM |
7034 | /* Like store_bindings, but NAMES is a vector of cp_class_binding |
7035 | objects, rather than a TREE_LIST. */ | |
7036 | ||
f44b0c8e | 7037 | static void |
9771b263 DN |
7038 | store_class_bindings (vec<cp_class_binding, va_gc> *names, |
7039 | vec<cxx_saved_binding, va_gc> **old_bindings) | |
89b578be | 7040 | { |
199d1d48 | 7041 | static vec<tree> bindings_need_stored; |
89b578be MM |
7042 | size_t i; |
7043 | cp_class_binding *cb; | |
89b578be | 7044 | |
9771b263 | 7045 | for (i = 0; vec_safe_iterate (names, i, &cb); ++i) |
71f15f31 | 7046 | if (store_binding_p (cb->identifier)) |
9771b263 DN |
7047 | bindings_need_stored.safe_push (cb->identifier); |
7048 | if (!bindings_need_stored.is_empty ()) | |
71f15f31 RG |
7049 | { |
7050 | tree id; | |
9771b263 DN |
7051 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
7052 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 7053 | store_binding (id, old_bindings); |
9771b263 | 7054 | bindings_need_stored.truncate (0); |
71f15f31 | 7055 | } |
89b578be MM |
7056 | } |
7057 | ||
5c712250 PP |
7058 | /* A chain of saved_scope structures awaiting reuse. */ |
7059 | ||
7060 | static GTY((deletable)) struct saved_scope *free_saved_scope; | |
7061 | ||
c405923d NS |
7062 | static void |
7063 | do_push_to_top_level (void) | |
00e8de68 GDR |
7064 | { |
7065 | struct saved_scope *s; | |
2c140474 | 7066 | cp_binding_level *b; |
f44b0c8e MM |
7067 | cxx_saved_binding *sb; |
7068 | size_t i; | |
30bcc028 | 7069 | bool need_pop; |
00e8de68 | 7070 | |
5c712250 PP |
7071 | /* Reuse or create a new structure for this saved scope. */ |
7072 | if (free_saved_scope != NULL) | |
7073 | { | |
7074 | s = free_saved_scope; | |
7075 | free_saved_scope = s->prev; | |
7076 | ||
7077 | vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings; | |
7078 | memset (s, 0, sizeof (*s)); | |
7079 | /* Also reuse the structure's old_bindings vector. */ | |
7080 | vec_safe_truncate (old_bindings, 0); | |
7081 | s->old_bindings = old_bindings; | |
7082 | } | |
7083 | else | |
7084 | s = ggc_cleared_alloc<saved_scope> (); | |
00e8de68 GDR |
7085 | |
7086 | b = scope_chain ? current_binding_level : 0; | |
7087 | ||
7088 | /* If we're in the middle of some function, save our state. */ | |
7089 | if (cfun) | |
7090 | { | |
30bcc028 | 7091 | need_pop = true; |
d2784db4 | 7092 | push_function_context (); |
00e8de68 GDR |
7093 | } |
7094 | else | |
30bcc028 | 7095 | need_pop = false; |
00e8de68 | 7096 | |
89b578be | 7097 | if (scope_chain && previous_class_level) |
f44b0c8e MM |
7098 | store_class_bindings (previous_class_level->class_shadowed, |
7099 | &s->old_bindings); | |
00e8de68 GDR |
7100 | |
7101 | /* Have to include the global scope, because class-scope decls | |
7102 | aren't listed anywhere useful. */ | |
7103 | for (; b; b = b->level_chain) | |
7104 | { | |
7105 | tree t; | |
7106 | ||
7107 | /* Template IDs are inserted into the global level. If they were | |
7108 | inserted into namespace level, finish_file wouldn't find them | |
7109 | when doing pending instantiations. Therefore, don't stop at | |
7110 | namespace level, but continue until :: . */ | |
c353b8e3 | 7111 | if (global_scope_p (b)) |
00e8de68 GDR |
7112 | break; |
7113 | ||
f44b0c8e | 7114 | store_bindings (b->names, &s->old_bindings); |
00e8de68 GDR |
7115 | /* We also need to check class_shadowed to save class-level type |
7116 | bindings, since pushclass doesn't fill in b->names. */ | |
7117 | if (b->kind == sk_class) | |
f44b0c8e | 7118 | store_class_bindings (b->class_shadowed, &s->old_bindings); |
00e8de68 GDR |
7119 | |
7120 | /* Unwind type-value slots back to top level. */ | |
7121 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
7122 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
7123 | } | |
f44b0c8e | 7124 | |
9771b263 | 7125 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb) |
f44b0c8e MM |
7126 | IDENTIFIER_MARKED (sb->identifier) = 0; |
7127 | ||
00e8de68 | 7128 | s->prev = scope_chain; |
00e8de68 GDR |
7129 | s->bindings = b; |
7130 | s->need_pop_function_context = need_pop; | |
7131 | s->function_decl = current_function_decl; | |
7d882b83 ILT |
7132 | s->unevaluated_operand = cp_unevaluated_operand; |
7133 | s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
b00e9be0 | 7134 | s->suppress_location_wrappers = suppress_location_wrappers; |
04f7a48e | 7135 | s->x_stmt_tree.stmts_are_full_exprs_p = true; |
00e8de68 GDR |
7136 | |
7137 | scope_chain = s; | |
7138 | current_function_decl = NULL_TREE; | |
50200c65 | 7139 | current_lang_base = NULL; |
00e8de68 GDR |
7140 | current_lang_name = lang_name_cplusplus; |
7141 | current_namespace = global_namespace; | |
c888c93b | 7142 | push_class_stack (); |
7d882b83 ILT |
7143 | cp_unevaluated_operand = 0; |
7144 | c_inhibit_evaluation_warnings = 0; | |
b00e9be0 | 7145 | suppress_location_wrappers = 0; |
00e8de68 GDR |
7146 | } |
7147 | ||
575bfb00 | 7148 | static void |
c405923d | 7149 | do_pop_from_top_level (void) |
00e8de68 GDR |
7150 | { |
7151 | struct saved_scope *s = scope_chain; | |
7152 | cxx_saved_binding *saved; | |
f44b0c8e | 7153 | size_t i; |
00e8de68 | 7154 | |
00e8de68 | 7155 | /* Clear out class-level bindings cache. */ |
89b578be | 7156 | if (previous_class_level) |
00e8de68 | 7157 | invalidate_class_lookup_cache (); |
c888c93b | 7158 | pop_class_stack (); |
00e8de68 | 7159 | |
50200c65 | 7160 | release_tree_vector (current_lang_base); |
00e8de68 GDR |
7161 | |
7162 | scope_chain = s->prev; | |
9771b263 | 7163 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved) |
00e8de68 GDR |
7164 | { |
7165 | tree id = saved->identifier; | |
7166 | ||
7167 | IDENTIFIER_BINDING (id) = saved->binding; | |
00e8de68 GDR |
7168 | SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); |
7169 | } | |
7170 | ||
7171 | /* If we were in the middle of compiling a function, restore our | |
7172 | state. */ | |
7173 | if (s->need_pop_function_context) | |
d2784db4 | 7174 | pop_function_context (); |
00e8de68 | 7175 | current_function_decl = s->function_decl; |
7d882b83 ILT |
7176 | cp_unevaluated_operand = s->unevaluated_operand; |
7177 | c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings; | |
b00e9be0 | 7178 | suppress_location_wrappers = s->suppress_location_wrappers; |
5c712250 PP |
7179 | |
7180 | /* Make this saved_scope structure available for reuse by | |
7181 | push_to_top_level. */ | |
7182 | s->prev = free_saved_scope; | |
7183 | free_saved_scope = s; | |
00e8de68 GDR |
7184 | } |
7185 | ||
c405923d NS |
7186 | /* Push into the scope of the namespace NS, even if it is deeply |
7187 | nested within another namespace. */ | |
575bfb00 | 7188 | |
c405923d NS |
7189 | static void |
7190 | do_push_nested_namespace (tree ns) | |
7191 | { | |
7192 | if (ns == global_namespace) | |
7193 | do_push_to_top_level (); | |
7194 | else | |
7195 | { | |
7196 | do_push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
7197 | gcc_checking_assert | |
e833f686 | 7198 | (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns); |
c405923d NS |
7199 | resume_scope (NAMESPACE_LEVEL (ns)); |
7200 | current_namespace = ns; | |
7201 | } | |
7202 | } | |
7203 | ||
7204 | /* Pop back from the scope of the namespace NS, which was previously | |
7205 | entered with push_nested_namespace. */ | |
7206 | ||
7207 | static void | |
7208 | do_pop_nested_namespace (tree ns) | |
7209 | { | |
7210 | while (ns != global_namespace) | |
7211 | { | |
7212 | ns = CP_DECL_CONTEXT (ns); | |
7213 | current_namespace = ns; | |
7214 | leave_scope (); | |
7215 | } | |
7216 | ||
7217 | do_pop_from_top_level (); | |
7218 | } | |
7219 | ||
44e00a7a NS |
7220 | /* Add TARGET to USINGS, if it does not already exist there. |
7221 | We used to build the complete graph of usings at this point, from | |
7222 | the POV of the source namespaces. Now we build that as we perform | |
7223 | the unqualified search. */ | |
65cc1407 NS |
7224 | |
7225 | static void | |
3c9feefc | 7226 | add_using_namespace (vec<tree, va_gc> *&usings, tree target) |
65cc1407 | 7227 | { |
3c9feefc NS |
7228 | if (usings) |
7229 | for (unsigned ix = usings->length (); ix--;) | |
7230 | if ((*usings)[ix] == target) | |
7231 | return; | |
65cc1407 | 7232 | |
3c9feefc | 7233 | vec_safe_push (usings, target); |
65cc1407 NS |
7234 | } |
7235 | ||
44e00a7a | 7236 | /* Tell the debug system of a using directive. */ |
65cc1407 NS |
7237 | |
7238 | static void | |
e071b767 | 7239 | emit_debug_info_using_namespace (tree from, tree target, bool implicit) |
65cc1407 | 7240 | { |
44e00a7a NS |
7241 | /* Emit debugging info. */ |
7242 | tree context = from != global_namespace ? from : NULL_TREE; | |
e071b767 JJ |
7243 | debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false, |
7244 | implicit); | |
65cc1407 NS |
7245 | } |
7246 | ||
7247 | /* Process a namespace-scope using directive. */ | |
7248 | ||
7249 | void | |
7250 | finish_namespace_using_directive (tree target, tree attribs) | |
7251 | { | |
7252 | gcc_checking_assert (namespace_bindings_p ()); | |
7253 | if (target == error_mark_node) | |
7254 | return; | |
7255 | ||
44e00a7a | 7256 | add_using_namespace (DECL_NAMESPACE_USING (current_namespace), |
65cc1407 | 7257 | ORIGINAL_NAMESPACE (target)); |
44e00a7a | 7258 | emit_debug_info_using_namespace (current_namespace, |
e071b767 | 7259 | ORIGINAL_NAMESPACE (target), false); |
65cc1407 NS |
7260 | |
7261 | if (attribs == error_mark_node) | |
7262 | return; | |
7263 | ||
7264 | for (tree a = attribs; a; a = TREE_CHAIN (a)) | |
7265 | { | |
7266 | tree name = get_attribute_name (a); | |
7267 | if (is_attribute_p ("strong", name)) | |
7268 | { | |
7269 | warning (0, "strong using directive no longer supported"); | |
7270 | if (CP_DECL_CONTEXT (target) == current_namespace) | |
7271 | inform (DECL_SOURCE_LOCATION (target), | |
7272 | "you may use an inline namespace instead"); | |
7273 | } | |
7274 | else | |
7275 | warning (OPT_Wattributes, "%qD attribute directive ignored", name); | |
7276 | } | |
7277 | } | |
7278 | ||
7279 | /* Process a function-scope using-directive. */ | |
7280 | ||
7281 | void | |
7282 | finish_local_using_directive (tree target, tree attribs) | |
7283 | { | |
7284 | gcc_checking_assert (local_bindings_p ()); | |
7285 | if (target == error_mark_node) | |
7286 | return; | |
7287 | ||
7288 | if (attribs) | |
7289 | warning (OPT_Wattributes, "attributes ignored on local using directive"); | |
7290 | ||
7291 | add_stmt (build_stmt (input_location, USING_STMT, target)); | |
7292 | ||
44e00a7a NS |
7293 | add_using_namespace (current_binding_level->using_directives, |
7294 | ORIGINAL_NAMESPACE (target)); | |
65cc1407 NS |
7295 | } |
7296 | ||
c405923d NS |
7297 | /* Pushes X into the global namespace. */ |
7298 | ||
7299 | tree | |
7300 | pushdecl_top_level (tree x, bool is_friend) | |
575bfb00 LC |
7301 | { |
7302 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
c405923d NS |
7303 | do_push_to_top_level (); |
7304 | x = pushdecl_namespace_level (x, is_friend); | |
7305 | do_pop_from_top_level (); | |
575bfb00 | 7306 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
c405923d NS |
7307 | return x; |
7308 | } | |
7309 | ||
7310 | /* Pushes X into the global namespace and calls cp_finish_decl to | |
7311 | register the variable, initializing it with INIT. */ | |
7312 | ||
7313 | tree | |
7314 | pushdecl_top_level_and_finish (tree x, tree init) | |
7315 | { | |
7316 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
7317 | do_push_to_top_level (); | |
7318 | x = pushdecl_namespace_level (x, false); | |
7319 | cp_finish_decl (x, init, false, NULL_TREE, 0); | |
7320 | do_pop_from_top_level (); | |
7321 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
7322 | return x; | |
575bfb00 LC |
7323 | } |
7324 | ||
945bf9e1 NS |
7325 | /* Enter the namespaces from current_namerspace to NS. */ |
7326 | ||
7327 | static int | |
7328 | push_inline_namespaces (tree ns) | |
7329 | { | |
7330 | int count = 0; | |
7331 | if (ns != current_namespace) | |
7332 | { | |
7333 | gcc_assert (ns != global_namespace); | |
7334 | count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); | |
7335 | resume_scope (NAMESPACE_LEVEL (ns)); | |
7336 | current_namespace = ns; | |
7337 | count++; | |
7338 | } | |
7339 | return count; | |
7340 | } | |
7341 | ||
3a77e7cc NS |
7342 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, |
7343 | then we enter an anonymous namespace. If MAKE_INLINE is true, then | |
7344 | we create an inline namespace (it is up to the caller to check upon | |
7345 | redefinition). Return the number of namespaces entered. */ | |
b655c310 | 7346 | |
3a77e7cc NS |
7347 | int |
7348 | push_namespace (tree name, bool make_inline) | |
b655c310 | 7349 | { |
b655c310 | 7350 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
3a77e7cc | 7351 | int count = 0; |
b655c310 NS |
7352 | |
7353 | /* We should not get here if the global_namespace is not yet constructed | |
7354 | nor if NAME designates the global namespace: The global scope is | |
7355 | constructed elsewhere. */ | |
e833f686 | 7356 | gcc_checking_assert (global_namespace != NULL && name != global_identifier); |
3a77e7cc | 7357 | |
945bf9e1 NS |
7358 | tree ns = NULL_TREE; |
7359 | { | |
7360 | name_lookup lookup (name, 0); | |
7361 | if (!lookup.search_qualified (current_namespace, /*usings=*/false)) | |
7362 | ; | |
7363 | else if (TREE_CODE (lookup.value) != NAMESPACE_DECL) | |
7364 | ; | |
7365 | else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value)) | |
7366 | { | |
7367 | /* A namespace alias is not allowed here, but if the alias | |
7368 | is for a namespace also inside the current scope, | |
7369 | accept it with a diagnostic. That's better than dying | |
7370 | horribly. */ | |
7371 | if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna))) | |
7372 | { | |
7373 | error ("namespace alias %qD not allowed here, " | |
7374 | "assuming %qD", lookup.value, dna); | |
7375 | ns = dna; | |
7376 | } | |
7377 | } | |
7378 | else | |
7379 | ns = lookup.value; | |
7380 | } | |
b655c310 | 7381 | |
3a77e7cc | 7382 | bool new_ns = false; |
945bf9e1 NS |
7383 | if (ns) |
7384 | /* DR2061. NS might be a member of an inline namespace. We | |
7385 | need to push into those namespaces. */ | |
7386 | count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); | |
7387 | else | |
b655c310 | 7388 | { |
3a77e7cc | 7389 | ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
322763f5 NS |
7390 | SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1; |
7391 | if (!SCOPE_DEPTH (ns)) | |
7392 | /* We only allow depth 255. */ | |
7393 | sorry ("cannot nest more than %d namespaces", | |
7394 | SCOPE_DEPTH (current_namespace)); | |
3a77e7cc NS |
7395 | DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace); |
7396 | new_ns = true; | |
7397 | ||
7398 | if (pushdecl (ns) == error_mark_node) | |
7399 | ns = NULL_TREE; | |
b655c310 | 7400 | else |
b655c310 | 7401 | { |
e833f686 | 7402 | if (!name) |
3a77e7cc | 7403 | { |
e833f686 | 7404 | SET_DECL_ASSEMBLER_NAME (ns, anon_identifier); |
3a77e7cc NS |
7405 | |
7406 | if (!make_inline) | |
44e00a7a NS |
7407 | add_using_namespace (DECL_NAMESPACE_USING (current_namespace), |
7408 | ns); | |
3a77e7cc NS |
7409 | } |
7410 | else if (TREE_PUBLIC (current_namespace)) | |
7411 | TREE_PUBLIC (ns) = 1; | |
7412 | ||
7413 | if (make_inline) | |
3c9feefc NS |
7414 | { |
7415 | DECL_NAMESPACE_INLINE_P (ns) = true; | |
7416 | vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns); | |
7417 | } | |
e071b767 | 7418 | |
e833f686 | 7419 | if (!name || make_inline) |
e071b767 | 7420 | emit_debug_info_using_namespace (current_namespace, ns, true); |
b655c310 | 7421 | } |
3a77e7cc NS |
7422 | } |
7423 | ||
7424 | if (ns) | |
7425 | { | |
7426 | if (make_inline && !DECL_NAMESPACE_INLINE_P (ns)) | |
b655c310 | 7427 | { |
3a77e7cc NS |
7428 | error ("inline namespace must be specified at initial definition"); |
7429 | inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns); | |
b655c310 | 7430 | } |
3a77e7cc NS |
7431 | if (new_ns) |
7432 | begin_scope (sk_namespace, ns); | |
7433 | else | |
7434 | resume_scope (NAMESPACE_LEVEL (ns)); | |
7435 | current_namespace = ns; | |
7436 | count++; | |
b655c310 | 7437 | } |
b655c310 NS |
7438 | |
7439 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
3a77e7cc | 7440 | return count; |
b655c310 NS |
7441 | } |
7442 | ||
7443 | /* Pop from the scope of the current namespace. */ | |
7444 | ||
7445 | void | |
7446 | pop_namespace (void) | |
7447 | { | |
3c9feefc NS |
7448 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
7449 | ||
b655c310 NS |
7450 | gcc_assert (current_namespace != global_namespace); |
7451 | current_namespace = CP_DECL_CONTEXT (current_namespace); | |
7452 | /* The binding level is not popped, as it might be re-opened later. */ | |
7453 | leave_scope (); | |
3c9feefc NS |
7454 | |
7455 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
b655c310 NS |
7456 | } |
7457 | ||
c405923d | 7458 | /* External entry points for do_{push_to/pop_from}_top_level. */ |
b655c310 NS |
7459 | |
7460 | void | |
c405923d | 7461 | push_to_top_level (void) |
b655c310 | 7462 | { |
c405923d NS |
7463 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
7464 | do_push_to_top_level (); | |
7465 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
b655c310 NS |
7466 | } |
7467 | ||
c405923d NS |
7468 | void |
7469 | pop_from_top_level (void) | |
7470 | { | |
7471 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
7472 | do_pop_from_top_level (); | |
7473 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
7474 | } | |
7475 | ||
7476 | /* External entry points for do_{push,pop}_nested_namespace. */ | |
7477 | ||
7478 | void | |
7479 | push_nested_namespace (tree ns) | |
7480 | { | |
7481 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
7482 | do_push_nested_namespace (ns); | |
7483 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
7484 | } | |
b655c310 NS |
7485 | |
7486 | void | |
7487 | pop_nested_namespace (tree ns) | |
7488 | { | |
7489 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
7490 | gcc_assert (current_namespace == ns); | |
c405923d | 7491 | do_pop_nested_namespace (ns); |
b655c310 NS |
7492 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
7493 | } | |
575bfb00 | 7494 | |
00e8de68 | 7495 | /* Pop off extraneous binding levels left over due to syntax errors. |
00e8de68 GDR |
7496 | We don't pop past namespaces, as they might be valid. */ |
7497 | ||
7498 | void | |
7499 | pop_everything (void) | |
7500 | { | |
7501 | if (ENABLE_SCOPE_CHECKING) | |
a9c697b8 | 7502 | verbatim ("XXX entering %<pop_everything ()%>"); |
056a17ee | 7503 | while (!namespace_bindings_p ()) |
00e8de68 GDR |
7504 | { |
7505 | if (current_binding_level->kind == sk_class) | |
7506 | pop_nested_class (); | |
7507 | else | |
7508 | poplevel (0, 0, 0); | |
7509 | } | |
7510 | if (ENABLE_SCOPE_CHECKING) | |
a9c697b8 | 7511 | verbatim ("XXX leaving %<pop_everything ()%>"); |
00e8de68 GDR |
7512 | } |
7513 | ||
6097b0c3 | 7514 | /* Emit debugging information for using declarations and directives. |
c8094d83 | 7515 | If input tree is overloaded fn then emit debug info for all |
6097b0c3 DP |
7516 | candidates. */ |
7517 | ||
98ed9dae | 7518 | void |
6097b0c3 DP |
7519 | cp_emit_debug_info_for_using (tree t, tree context) |
7520 | { | |
099f36ab | 7521 | /* Don't try to emit any debug information if we have errors. */ |
1da2ed5f | 7522 | if (seen_error ()) |
099f36ab JM |
7523 | return; |
7524 | ||
c8094d83 | 7525 | /* Ignore this FUNCTION_DECL if it refers to a builtin declaration |
6097b0c3 | 7526 | of a builtin function. */ |
c8094d83 | 7527 | if (TREE_CODE (t) == FUNCTION_DECL |
6097b0c3 | 7528 | && DECL_EXTERNAL (t) |
3d78e008 | 7529 | && fndecl_built_in_p (t)) |
6097b0c3 DP |
7530 | return; |
7531 | ||
7532 | /* Do not supply context to imported_module_or_decl, if | |
7533 | it is a global namespace. */ | |
7534 | if (context == global_namespace) | |
7535 | context = NULL_TREE; | |
c8094d83 | 7536 | |
e1cad930 | 7537 | t = MAYBE_BASELINK_FUNCTIONS (t); |
c8094d83 | 7538 | |
6097b0c3 | 7539 | /* FIXME: Handle TEMPLATE_DECLs. */ |
87c976ae NS |
7540 | for (lkp_iterator iter (t); iter; ++iter) |
7541 | { | |
7542 | tree fn = *iter; | |
7543 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7544 | { | |
7545 | if (building_stmt_list_p ()) | |
7546 | add_stmt (build_stmt (input_location, USING_STMT, fn)); | |
7547 | else | |
e071b767 JJ |
7548 | debug_hooks->imported_module_or_decl (fn, NULL_TREE, context, |
7549 | false, false); | |
87c976ae NS |
7550 | } |
7551 | } | |
98ed9dae | 7552 | } |
6097b0c3 | 7553 | |
bddee796 JM |
7554 | /* Return the result of unqualified lookup for the overloaded operator |
7555 | designated by CODE, if we are in a template and the binding we find is | |
7556 | not. */ | |
7557 | ||
7558 | static tree | |
7559 | op_unqualified_lookup (tree fnname) | |
7560 | { | |
7561 | if (cxx_binding *binding = IDENTIFIER_BINDING (fnname)) | |
7562 | { | |
7563 | cp_binding_level *l = binding->scope; | |
7564 | while (l && !l->this_entity) | |
7565 | l = l->level_chain; | |
7566 | if (l && uses_template_parms (l->this_entity)) | |
7567 | /* Don't preserve decls from an uninstantiated template, | |
7568 | wait until that template is instantiated. */ | |
7569 | return NULL_TREE; | |
7570 | } | |
7571 | tree fns = lookup_name (fnname); | |
7572 | if (fns && fns == get_global_binding (fnname)) | |
7573 | /* The instantiation can find these. */ | |
7574 | return NULL_TREE; | |
7575 | return fns; | |
7576 | } | |
7577 | ||
7578 | /* E is an expression representing an operation with dependent type, so we | |
7579 | don't know yet whether it will use the built-in meaning of the operator or a | |
7580 | function. Remember declarations of that operator in scope. */ | |
7581 | ||
7582 | const char *const op_bind_attrname = "operator bindings"; | |
7583 | ||
7584 | void | |
7585 | maybe_save_operator_binding (tree e) | |
7586 | { | |
7587 | /* This is only useful in a generic lambda. */ | |
7588 | if (!processing_template_decl) | |
7589 | return; | |
7590 | tree cfn = current_function_decl; | |
7591 | if (!cfn) | |
7592 | return; | |
7593 | ||
7594 | /* Let's only do this for generic lambdas for now, we could do it for all | |
7595 | function templates if we wanted to. */ | |
7596 | if (!current_lambda_expr()) | |
7597 | return; | |
7598 | ||
7599 | tree fnname = ovl_op_identifier (false, TREE_CODE (e)); | |
7600 | if (!fnname) | |
7601 | return; | |
7602 | ||
7603 | tree attributes = DECL_ATTRIBUTES (cfn); | |
7604 | tree attr = lookup_attribute (op_bind_attrname, attributes); | |
7605 | tree bindings = NULL_TREE; | |
7606 | tree fns = NULL_TREE; | |
7607 | if (attr) | |
7608 | { | |
7609 | bindings = TREE_VALUE (attr); | |
7610 | if (tree elt = purpose_member (fnname, bindings)) | |
7611 | fns = TREE_VALUE (elt); | |
7612 | } | |
7613 | ||
7614 | if (!fns && (fns = op_unqualified_lookup (fnname))) | |
7615 | { | |
7616 | bindings = tree_cons (fnname, fns, bindings); | |
7617 | if (attr) | |
7618 | TREE_VALUE (attr) = bindings; | |
7619 | else | |
7620 | DECL_ATTRIBUTES (cfn) | |
7621 | = tree_cons (get_identifier (op_bind_attrname), | |
7622 | bindings, | |
7623 | attributes); | |
7624 | } | |
7625 | } | |
7626 | ||
7627 | /* Called from cp_free_lang_data so we don't put this into LTO. */ | |
7628 | ||
7629 | void | |
7630 | discard_operator_bindings (tree decl) | |
7631 | { | |
7632 | DECL_ATTRIBUTES (decl) = remove_attribute (op_bind_attrname, | |
7633 | DECL_ATTRIBUTES (decl)); | |
7634 | } | |
7635 | ||
7636 | /* Subroutine of start_preparsed_function: push the bindings we saved away in | |
7637 | maybe_save_op_lookup into the function parameter binding level. */ | |
7638 | ||
7639 | void | |
7640 | push_operator_bindings () | |
7641 | { | |
7642 | tree decl1 = current_function_decl; | |
7643 | if (tree attr = lookup_attribute (op_bind_attrname, | |
7644 | DECL_ATTRIBUTES (decl1))) | |
7645 | for (tree binds = TREE_VALUE (attr); binds; binds = TREE_CHAIN (binds)) | |
7646 | { | |
7647 | tree name = TREE_PURPOSE (binds); | |
7648 | tree val = TREE_VALUE (binds); | |
7649 | push_local_binding (name, val, /*using*/true); | |
7650 | } | |
7651 | } | |
7652 | ||
28ea4c88 | 7653 | #include "gt-cp-name-lookup.h" |