1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "flags.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "name-lookup.h"
30 #include "timevar.h"
31 #include "toplev.h"
32 #include "diagnostic.h"
33 #include "debug.h"
34 #include "c-pragma.h"
35
36 /* The bindings for a particular name in a particular scope. */
37
38 struct scope_binding {
39 tree value;
40 tree type;
41 };
42 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
43
44 static cxx_scope *innermost_nonclass_level (void);
45 static cxx_binding *binding_for_name (cxx_scope *, tree);
46 static tree lookup_name_innermost_nonclass_level (tree);
47 static tree push_overloaded_decl (tree, int, bool);
48 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
49 tree, int);
50 static bool qualified_lookup_using_namespace (tree, tree,
51 struct scope_binding *, int);
52 static tree lookup_type_current_level (tree);
53 static tree push_using_directive (tree);
54
55 /* The :: namespace. */
56
57 tree global_namespace;
58
59 /* The name of the anonymous namespace, throughout this translation
60 unit. */
61 static GTY(()) tree anonymous_namespace_name;
62
63 /* Initialise anonymous_namespace_name if necessary, and return it. */
64
65 static tree
get_anonymous_namespace_name(void)66 get_anonymous_namespace_name(void)
67 {
68 if (!anonymous_namespace_name)
69 {
70 /* The anonymous namespace has to have a unique name
71 if typeinfo objects are being compared by name. */
72 if (! flag_weak || ! SUPPORTS_ONE_ONLY)
73 anonymous_namespace_name = get_file_function_name ("N");
74 else
75 /* The demangler expects anonymous namespaces to be called
76 something starting with '_GLOBAL__N_'. */
77 anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
78 }
79 return anonymous_namespace_name;
80 }
81
82 /* Compute the chain index of a binding_entry given the HASH value of its
83 name and the total COUNT of chains. COUNT is assumed to be a power
84 of 2. */
85
86 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
87
88 /* A free list of "binding_entry"s awaiting for re-use. */
89
90 static GTY((deletable)) binding_entry free_binding_entry = NULL;
91
92 /* Create a binding_entry object for (NAME, TYPE). */
93
94 static inline binding_entry
binding_entry_make(tree name,tree type)95 binding_entry_make (tree name, tree type)
96 {
97 binding_entry entry;
98
99 if (free_binding_entry)
100 {
101 entry = free_binding_entry;
102 free_binding_entry = entry->chain;
103 }
104 else
105 entry = GGC_NEW (struct binding_entry_s);
106
107 entry->name = name;
108 entry->type = type;
109 entry->chain = NULL;
110
111 return entry;
112 }
113
114 /* Put ENTRY back on the free list. */
115 #if 0
116 static inline void
117 binding_entry_free (binding_entry entry)
118 {
119 entry->name = NULL;
120 entry->type = NULL;
121 entry->chain = free_binding_entry;
122 free_binding_entry = entry;
123 }
124 #endif
125
126 /* The datatype used to implement the mapping from names to types at
127 a given scope. */
128 struct binding_table_s GTY(())
129 {
130 /* Array of chains of "binding_entry"s */
131 binding_entry * GTY((length ("%h.chain_count"))) chain;
132
133 /* The number of chains in this table. This is the length of the
134 the member "chain" considered as an array. */
135 size_t chain_count;
136
137 /* Number of "binding_entry"s in this table. */
138 size_t entry_count;
139 };
140
141 /* Construct TABLE with an initial CHAIN_COUNT. */
142
143 static inline void
binding_table_construct(binding_table table,size_t chain_count)144 binding_table_construct (binding_table table, size_t chain_count)
145 {
146 table->chain_count = chain_count;
147 table->entry_count = 0;
148 table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
149 }
150
151 /* Make TABLE's entries ready for reuse. */
152 #if 0
153 static void
154 binding_table_free (binding_table table)
155 {
156 size_t i;
157 size_t count;
158
159 if (table == NULL)
160 return;
161
162 for (i = 0, count = table->chain_count; i < count; ++i)
163 {
164 binding_entry temp = table->chain[i];
165 while (temp != NULL)
166 {
167 binding_entry entry = temp;
168 temp = entry->chain;
169 binding_entry_free (entry);
170 }
171 table->chain[i] = NULL;
172 }
173 table->entry_count = 0;
174 }
175 #endif
176
177 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
178
179 static inline binding_table
binding_table_new(size_t chain_count)180 binding_table_new (size_t chain_count)
181 {
182 binding_table table = GGC_NEW (struct binding_table_s);
183 table->chain = NULL;
184 binding_table_construct (table, chain_count);
185 return table;
186 }
187
188 /* Expand TABLE to twice its current chain_count. */
189
190 static void
binding_table_expand(binding_table table)191 binding_table_expand (binding_table table)
192 {
193 const size_t old_chain_count = table->chain_count;
194 const size_t old_entry_count = table->entry_count;
195 const size_t new_chain_count = 2 * old_chain_count;
196 binding_entry *old_chains = table->chain;
197 size_t i;
198
199 binding_table_construct (table, new_chain_count);
200 for (i = 0; i < old_chain_count; ++i)
201 {
202 binding_entry entry = old_chains[i];
203 for (; entry != NULL; entry = old_chains[i])
204 {
205 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
206 const size_t j = ENTRY_INDEX (hash, new_chain_count);
207
208 old_chains[i] = entry->chain;
209 entry->chain = table->chain[j];
210 table->chain[j] = entry;
211 }
212 }
213 table->entry_count = old_entry_count;
214 }
215
216 /* Insert a binding for NAME to TYPE into TABLE. */
217
218 static void
binding_table_insert(binding_table table,tree name,tree type)219 binding_table_insert (binding_table table, tree name, tree type)
220 {
221 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
222 const size_t i = ENTRY_INDEX (hash, table->chain_count);
223 binding_entry entry = binding_entry_make (name, type);
224
225 entry->chain = table->chain[i];
226 table->chain[i] = entry;
227 ++table->entry_count;
228
229 if (3 * table->chain_count < 5 * table->entry_count)
230 binding_table_expand (table);
231 }
232
233 /* Return the binding_entry, if any, that maps NAME. */
234
235 binding_entry
binding_table_find(binding_table table,tree name)236 binding_table_find (binding_table table, tree name)
237 {
238 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
239 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
240
241 while (entry != NULL && entry->name != name)
242 entry = entry->chain;
243
244 return entry;
245 }
246
247 /* Apply PROC -- with DATA -- to all entries in TABLE. */
248
249 void
binding_table_foreach(binding_table table,bt_foreach_proc proc,void * data)250 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
251 {
252 const size_t chain_count = table->chain_count;
253 size_t i;
254
255 for (i = 0; i < chain_count; ++i)
256 {
257 binding_entry entry = table->chain[i];
258 for (; entry != NULL; entry = entry->chain)
259 proc (entry, data);
260 }
261 }
262
263 #ifndef ENABLE_SCOPE_CHECKING
264 # define ENABLE_SCOPE_CHECKING 0
265 #else
266 # define ENABLE_SCOPE_CHECKING 1
267 #endif
268
269 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
270
271 static GTY((deletable)) cxx_binding *free_bindings;
272
273 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
274 field to NULL. */
275
276 static inline void
cxx_binding_init(cxx_binding * binding,tree value,tree type)277 cxx_binding_init (cxx_binding *binding, tree value, tree type)
278 {
279 binding->value = value;
280 binding->type = type;
281 binding->previous = NULL;
282 }
283
284 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
285
286 static cxx_binding *
cxx_binding_make(tree value,tree type)287 cxx_binding_make (tree value, tree type)
288 {
289 cxx_binding *binding;
290 if (free_bindings)
291 {
292 binding = free_bindings;
293 free_bindings = binding->previous;
294 }
295 else
296 binding = GGC_NEW (cxx_binding);
297
298 cxx_binding_init (binding, value, type);
299
300 return binding;
301 }
302
303 /* Put BINDING back on the free list. */
304
305 static inline void
cxx_binding_free(cxx_binding * binding)306 cxx_binding_free (cxx_binding *binding)
307 {
308 binding->scope = NULL;
309 binding->previous = free_bindings;
310 free_bindings = binding;
311 }
312
313 /* Create a new binding for NAME (with the indicated VALUE and TYPE
314 bindings) in the class scope indicated by SCOPE. */
315
316 static cxx_binding *
new_class_binding(tree name,tree value,tree type,cxx_scope * scope)317 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
318 {
319 cp_class_binding *cb;
320 cxx_binding *binding;
321
322 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
323
324 cb->identifier = name;
325 cb->base = binding = cxx_binding_make (value, type);
326 binding->scope = scope;
327 return binding;
328 }
329
330 /* Make DECL the innermost binding for ID. The LEVEL is the binding
331 level at which this declaration is being bound. */
332
333 static void
push_binding(tree id,tree decl,cxx_scope * level)334 push_binding (tree id, tree decl, cxx_scope* level)
335 {
336 cxx_binding *binding;
337
338 if (level != class_binding_level)
339 {
340 binding = cxx_binding_make (decl, NULL_TREE);
341 binding->scope = level;
342 /* APPLE LOCAL blocks 6040305 (ch) */
343 binding->declared_in_block = cur_block != 0;
344 }
345 else
346 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
347
348 /* Now, fill in the binding information. */
349 binding->previous = IDENTIFIER_BINDING (id);
350 INHERITED_VALUE_BINDING_P (binding) = 0;
351 LOCAL_BINDING_P (binding) = (level != class_binding_level);
352
353 /* And put it on the front of the list of bindings for ID. */
354 IDENTIFIER_BINDING (id) = binding;
355 }
356
357 /* Remove the binding for DECL which should be the innermost binding
358 for ID. */
359
360 void
pop_binding(tree id,tree decl)361 pop_binding (tree id, tree decl)
362 {
363 cxx_binding *binding;
364
365 if (id == NULL_TREE)
366 /* It's easiest to write the loops that call this function without
367 checking whether or not the entities involved have names. We
368 get here for such an entity. */
369 return;
370
371 /* Get the innermost binding for ID. */
372 binding = IDENTIFIER_BINDING (id);
373
374 /* The name should be bound. */
375 gcc_assert (binding != NULL);
376
377 /* The DECL will be either the ordinary binding or the type
378 binding for this identifier. Remove that binding. */
379 if (binding->value == decl)
380 binding->value = NULL_TREE;
381 else
382 {
383 gcc_assert (binding->type == decl);
384 binding->type = NULL_TREE;
385 }
386
387 if (!binding->value && !binding->type)
388 {
389 /* We're completely done with the innermost binding for this
390 identifier. Unhook it from the list of bindings. */
391 IDENTIFIER_BINDING (id) = binding->previous;
392
393 /* Add it to the free list. */
394 cxx_binding_free (binding);
395 }
396 }
397
398 /* BINDING records an existing declaration for a name in the current scope.
399 But, DECL is another declaration for that same identifier in the
400 same scope. This is the `struct stat' hack whereby a non-typedef
401 class name or enum-name can be bound at the same level as some other
402 kind of entity.
403 3.3.7/1
404
405 A class name (9.1) or enumeration name (7.2) can be hidden by the
406 name of an object, function, or enumerator declared in the same scope.
407 If a class or enumeration name and an object, function, or enumerator
408 are declared in the same scope (in any order) with the same name, the
409 class or enumeration name is hidden wherever the object, function, or
410 enumerator name is visible.
411
412 It's the responsibility of the caller to check that
413 inserting this name is valid here. Returns nonzero if the new binding
414 was successful. */
415
416 static bool
supplement_binding(cxx_binding * binding,tree decl)417 supplement_binding (cxx_binding *binding, tree decl)
418 {
419 tree bval = binding->value;
420 bool ok = true;
421
422 timevar_push (TV_NAME_LOOKUP);
423 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
424 /* The new name is the type name. */
425 binding->type = decl;
426 else if (/* BVAL is null when push_class_level_binding moves an
427 inherited type-binding out of the way to make room for a
428 new value binding. */
429 !bval
430 /* BVAL is error_mark_node when DECL's name has been used
431 in a non-class scope prior declaration. In that case,
432 we should have already issued a diagnostic; for graceful
433 error recovery purpose, pretend this was the intended
434 declaration for that name. */
435 || bval == error_mark_node
436 /* If BVAL is anticipated but has not yet been declared,
437 pretend it is not there at all. */
438 || (TREE_CODE (bval) == FUNCTION_DECL
439 && DECL_ANTICIPATED (bval)
440 && !DECL_HIDDEN_FRIEND_P (bval)))
441 binding->value = decl;
442 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
443 {
444 /* The old binding was a type name. It was placed in
445 VALUE field because it was thought, at the point it was
446 declared, to be the only entity with such a name. Move the
447 type name into the type slot; it is now hidden by the new
448 binding. */
449 binding->type = bval;
450 binding->value = decl;
451 binding->value_is_inherited = false;
452 }
453 else if (TREE_CODE (bval) == TYPE_DECL
454 && TREE_CODE (decl) == TYPE_DECL
455 && DECL_NAME (decl) == DECL_NAME (bval)
456 && binding->scope->kind != sk_class
457 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
458 /* If either type involves template parameters, we must
459 wait until instantiation. */
460 || uses_template_parms (TREE_TYPE (decl))
461 || uses_template_parms (TREE_TYPE (bval))))
462 /* We have two typedef-names, both naming the same type to have
463 the same name. In general, this is OK because of:
464
465 [dcl.typedef]
466
467 In a given scope, a typedef specifier can be used to redefine
468 the name of any type declared in that scope to refer to the
469 type to which it already refers.
470
471 However, in class scopes, this rule does not apply due to the
472 stricter language in [class.mem] prohibiting redeclarations of
473 members. */
474 ok = false;
475 /* There can be two block-scope declarations of the same variable,
476 so long as they are `extern' declarations. However, there cannot
477 be two declarations of the same static data member:
478
479 [class.mem]
480
481 A member shall not be declared twice in the
482 member-specification. */
483 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
484 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
485 && !DECL_CLASS_SCOPE_P (decl))
486 {
487 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
488 ok = false;
489 }
490 else if (TREE_CODE (decl) == NAMESPACE_DECL
491 && TREE_CODE (bval) == NAMESPACE_DECL
492 && DECL_NAMESPACE_ALIAS (decl)
493 && DECL_NAMESPACE_ALIAS (bval)
494 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
495 /* [namespace.alias]
496
497 In a declarative region, a namespace-alias-definition can be
498 used to redefine a namespace-alias declared in that declarative
499 region to refer only to the namespace to which it already
500 refers. */
501 ok = false;
502 else
503 {
504 error ("declaration of %q#D", decl);
505 error ("conflicts with previous declaration %q+#D", bval);
506 ok = false;
507 }
508
509 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
510 }
511
512 /* Add DECL to the list of things declared in B. */
513
514 static void
add_decl_to_level(tree decl,cxx_scope * b)515 add_decl_to_level (tree decl, cxx_scope *b)
516 {
517 if (TREE_CODE (decl) == NAMESPACE_DECL
518 && !DECL_NAMESPACE_ALIAS (decl))
519 {
520 TREE_CHAIN (decl) = b->namespaces;
521 b->namespaces = decl;
522 }
523 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
524 {
525 TREE_CHAIN (decl) = b->vtables;
526 b->vtables = decl;
527 }
528 else
529 {
530 /* We build up the list in reverse order, and reverse it later if
531 necessary. */
532 TREE_CHAIN (decl) = b->names;
533 b->names = decl;
534 b->names_size++;
535
536 /* If appropriate, add decl to separate list of statics. We
537 include extern variables because they might turn out to be
538 static later. It's OK for this list to contain a few false
539 positives. */
540 if (b->kind == sk_namespace)
541 if ((TREE_CODE (decl) == VAR_DECL
542 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
543 || (TREE_CODE (decl) == FUNCTION_DECL
544 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
545 VEC_safe_push (tree, gc, b->static_decls, decl);
546 }
547 }
548
549 /* Record a decl-node X as belonging to the current lexical scope.
550 Check for errors (such as an incompatible declaration for the same
551 name already seen in the same scope). IS_FRIEND is true if X is
552 declared as a friend.
553
554 Returns either X or an old decl for the same name.
555 If an old decl is returned, it may have been smashed
556 to agree with what X says. */
557
558 tree
pushdecl_maybe_friend(tree x,bool is_friend)559 pushdecl_maybe_friend (tree x, bool is_friend)
560 {
561 tree t;
562 tree name;
563 int need_new_binding;
564
565 timevar_push (TV_NAME_LOOKUP);
566
567 if (x == error_mark_node)
568 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
569
570 need_new_binding = 1;
571
572 if (DECL_TEMPLATE_PARM_P (x))
573 /* Template parameters have no context; they are not X::T even
574 when declared within a class or namespace. */
575 ;
576 else
577 {
578 if (current_function_decl && x != current_function_decl
579 /* A local declaration for a function doesn't constitute
580 nesting. */
581 && TREE_CODE (x) != FUNCTION_DECL
582 /* A local declaration for an `extern' variable is in the
583 scope of the current namespace, not the current
584 function. */
585 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
586 && !DECL_CONTEXT (x))
587 DECL_CONTEXT (x) = current_function_decl;
588
589 /* If this is the declaration for a namespace-scope function,
590 but the declaration itself is in a local scope, mark the
591 declaration. */
592 if (TREE_CODE (x) == FUNCTION_DECL
593 && DECL_NAMESPACE_SCOPE_P (x)
594 && current_function_decl
595 && x != current_function_decl)
596 DECL_LOCAL_FUNCTION_P (x) = 1;
597 }
598
599 name = DECL_NAME (x);
600 if (name)
601 {
602 int different_binding_level = 0;
603
604 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
605 name = TREE_OPERAND (name, 0);
606
607 /* In case this decl was explicitly namespace-qualified, look it
608 up in its namespace context. */
609 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
610 t = namespace_binding (name, DECL_CONTEXT (x));
611 else
612 t = lookup_name_innermost_nonclass_level (name);
613
614 /* [basic.link] If there is a visible declaration of an entity
615 with linkage having the same name and type, ignoring entities
616 declared outside the innermost enclosing namespace scope, the
617 block scope declaration declares that same entity and
618 receives the linkage of the previous declaration. */
619 if (! t && current_function_decl && x != current_function_decl
620 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
621 && DECL_EXTERNAL (x))
622 {
623 /* Look in block scope. */
624 t = innermost_non_namespace_value (name);
625 /* Or in the innermost namespace. */
626 if (! t)
627 t = namespace_binding (name, DECL_CONTEXT (x));
628 /* Does it have linkage? Note that if this isn't a DECL, it's an
629 OVERLOAD, which is OK. */
630 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
631 t = NULL_TREE;
632 if (t)
633 different_binding_level = 1;
634 }
635
636 /* If we are declaring a function, and the result of name-lookup
637 was an OVERLOAD, look for an overloaded instance that is
638 actually the same as the function we are declaring. (If
639 there is one, we have to merge our declaration with the
640 previous declaration.) */
641 if (t && TREE_CODE (t) == OVERLOAD)
642 {
643 tree match;
644
645 if (TREE_CODE (x) == FUNCTION_DECL)
646 for (match = t; match; match = OVL_NEXT (match))
647 {
648 if (decls_match (OVL_CURRENT (match), x))
649 break;
650 }
651 else
652 /* Just choose one. */
653 match = t;
654
655 if (match)
656 t = OVL_CURRENT (match);
657 else
658 t = NULL_TREE;
659 }
660
661 if (t && t != error_mark_node)
662 {
663 if (different_binding_level)
664 {
665 if (decls_match (x, t))
666 /* The standard only says that the local extern
667 inherits linkage from the previous decl; in
668 particular, default args are not shared. Add
669 the decl into a hash table to make sure only
670 the previous decl in this case is seen by the
671 middle end. */
672 {
673 struct cxx_int_tree_map *h;
674 void **loc;
675
676 TREE_PUBLIC (x) = TREE_PUBLIC (t);
677
678 if (cp_function_chain->extern_decl_map == NULL)
679 cp_function_chain->extern_decl_map
680 = htab_create_ggc (20, cxx_int_tree_map_hash,
681 cxx_int_tree_map_eq, NULL);
682
683 h = GGC_NEW (struct cxx_int_tree_map);
684 h->uid = DECL_UID (x);
685 h->to = t;
686 loc = htab_find_slot_with_hash
687 (cp_function_chain->extern_decl_map, h,
688 h->uid, INSERT);
689 *(struct cxx_int_tree_map **) loc = h;
690 }
691 }
692 else if (TREE_CODE (t) == PARM_DECL)
693 {
694 gcc_assert (DECL_CONTEXT (t));
695
696 /* Check for duplicate params. */
697 if (duplicate_decls (x, t, is_friend))
698 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
699 }
700 else if ((DECL_EXTERN_C_FUNCTION_P (x)
701 || DECL_FUNCTION_TEMPLATE_P (x))
702 && is_overloaded_fn (t))
703 /* Don't do anything just yet. */;
704 else if (t == wchar_decl_node)
705 {
706 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
707 pedwarn ("redeclaration of %<wchar_t%> as %qT",
708 TREE_TYPE (x));
709
710 /* Throw away the redeclaration. */
711 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
712 }
713 else
714 {
715 tree olddecl = duplicate_decls (x, t, is_friend);
716
717 /* If the redeclaration failed, we can stop at this
718 point. */
719 if (olddecl == error_mark_node)
720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
721
722 if (olddecl)
723 {
724 if (TREE_CODE (t) == TYPE_DECL)
725 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
726
727 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
728 }
729 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
730 {
731 /* A redeclaration of main, but not a duplicate of the
732 previous one.
733
734 [basic.start.main]
735
736 This function shall not be overloaded. */
737 error ("invalid redeclaration of %q+D", t);
738 error ("as %qD", x);
739 /* We don't try to push this declaration since that
740 causes a crash. */
741 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
742 }
743 }
744 }
745
746 if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
747 check_default_args (x);
748
749 check_template_shadow (x);
750
751 /* If this is a function conjured up by the backend, massage it
752 so it looks friendly. */
753 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
754 {
755 retrofit_lang_decl (x);
756 SET_DECL_LANGUAGE (x, lang_c);
757 }
758
759 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
760 {
761 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
762 if (t != x)
763 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
764 if (!namespace_bindings_p ())
765 /* We do not need to create a binding for this name;
766 push_overloaded_decl will have already done so if
767 necessary. */
768 need_new_binding = 0;
769 }
770 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
771 {
772 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
773 if (t == x)
774 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
776 }
777
778 /* If declaring a type as a typedef, copy the type (unless we're
779 at line 0), and install this TYPE_DECL as the new type's typedef
780 name. See the extensive comment in ../c-decl.c (pushdecl). */
781 if (TREE_CODE (x) == TYPE_DECL)
782 {
783 tree type = TREE_TYPE (x);
784 if (DECL_IS_BUILTIN (x))
785 {
786 if (TYPE_NAME (type) == 0)
787 TYPE_NAME (type) = x;
788 }
789 else if (type != error_mark_node && TYPE_NAME (type) != x
790 /* We don't want to copy the type when all we're
791 doing is making a TYPE_DECL for the purposes of
792 inlining. */
793 && (!TYPE_NAME (type)
794 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
795 {
796 DECL_ORIGINAL_TYPE (x) = type;
797 type = build_variant_type_copy (type);
798 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
799 TYPE_NAME (type) = x;
800 TREE_TYPE (x) = type;
801 }
802
803 if (type != error_mark_node
804 && TYPE_NAME (type)
805 && TYPE_IDENTIFIER (type))
806 set_identifier_type_value (DECL_NAME (x), x);
807 }
808
809 /* Multiple external decls of the same identifier ought to match.
810
811 We get warnings about inline functions where they are defined.
812 We get warnings about other functions from push_overloaded_decl.
813
814 Avoid duplicate warnings where they are used. */
815 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
816 {
817 tree decl;
818
819 decl = IDENTIFIER_NAMESPACE_VALUE (name);
820 if (decl && TREE_CODE (decl) == OVERLOAD)
821 decl = OVL_FUNCTION (decl);
822
823 if (decl && decl != error_mark_node
824 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
825 /* If different sort of thing, we already gave an error. */
826 && TREE_CODE (decl) == TREE_CODE (x)
827 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
828 {
829 pedwarn ("type mismatch with previous external decl of %q#D", x);
830 pedwarn ("previous external decl of %q+#D", decl);
831 }
832 }
833
834 if (TREE_CODE (x) == FUNCTION_DECL
835 && is_friend
836 && !flag_friend_injection)
837 {
838 /* This is a new declaration of a friend function, so hide
839 it from ordinary function lookup. */
840 DECL_ANTICIPATED (x) = 1;
841 DECL_HIDDEN_FRIEND_P (x) = 1;
842 }
843
844 /* This name is new in its binding level.
845 Install the new declaration and return it. */
846 if (namespace_bindings_p ())
847 {
848 /* Install a global value. */
849
850 /* If the first global decl has external linkage,
851 warn if we later see static one. */
852 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
853 TREE_PUBLIC (name) = 1;
854
855 /* Bind the name for the entity. */
856 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
857 && t != NULL_TREE)
858 && (TREE_CODE (x) == TYPE_DECL
859 || TREE_CODE (x) == VAR_DECL
860 || TREE_CODE (x) == NAMESPACE_DECL
861 || TREE_CODE (x) == CONST_DECL
862 || TREE_CODE (x) == TEMPLATE_DECL))
863 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
864
865 /* If new decl is `static' and an `extern' was seen previously,
866 warn about it. */
867 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
868 warn_extern_redeclared_static (x, t);
869 }
870 else
871 {
872 /* Here to install a non-global value. */
873 tree oldlocal = innermost_non_namespace_value (name);
874 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
875
876 if (need_new_binding)
877 {
878 push_local_binding (name, x, 0);
879 /* Because push_local_binding will hook X on to the
880 current_binding_level's name list, we don't want to
881 do that again below. */
882 need_new_binding = 0;
883 }
884
885 /* If this is a TYPE_DECL, push it into the type value slot. */
886 if (TREE_CODE (x) == TYPE_DECL)
887 set_identifier_type_value (name, x);
888
889 /* Clear out any TYPE_DECL shadowed by a namespace so that
890 we won't think this is a type. The C struct hack doesn't
891 go through namespaces. */
892 if (TREE_CODE (x) == NAMESPACE_DECL)
893 set_identifier_type_value (name, NULL_TREE);
894
895 if (oldlocal)
896 {
897 tree d = oldlocal;
898
899 while (oldlocal
900 && TREE_CODE (oldlocal) == VAR_DECL
901 && DECL_DEAD_FOR_LOCAL (oldlocal))
902 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
903
904 if (oldlocal == NULL_TREE)
905 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
906 }
907
908 /* If this is an extern function declaration, see if we
909 have a global definition or declaration for the function. */
910 if (oldlocal == NULL_TREE
911 && DECL_EXTERNAL (x)
912 && oldglobal != NULL_TREE
913 && TREE_CODE (x) == FUNCTION_DECL
914 && TREE_CODE (oldglobal) == FUNCTION_DECL)
915 {
916 /* We have one. Their types must agree. */
917 if (decls_match (x, oldglobal))
918 /* OK */;
919 else
920 {
921 warning (0, "extern declaration of %q#D doesn't match", x);
922 warning (0, "global declaration %q+#D", oldglobal);
923 }
924 }
925 /* If we have a local external declaration,
926 and no file-scope declaration has yet been seen,
927 then if we later have a file-scope decl it must not be static. */
928 if (oldlocal == NULL_TREE
929 && oldglobal == NULL_TREE
930 && DECL_EXTERNAL (x)
931 && TREE_PUBLIC (x))
932 TREE_PUBLIC (name) = 1;
933
934 /* Warn if shadowing an argument at the top level of the body. */
935 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
936 /* Inline decls shadow nothing. */
937 && !DECL_FROM_INLINE (x)
938 && TREE_CODE (oldlocal) == PARM_DECL
939 /* Don't check the `this' parameter. */
940 && !DECL_ARTIFICIAL (oldlocal))
941 {
942 bool err = false;
943
944 /* Don't complain if it's from an enclosing function. */
945 if (DECL_CONTEXT (oldlocal) == current_function_decl
946 && TREE_CODE (x) != PARM_DECL)
947 {
948 /* Go to where the parms should be and see if we find
949 them there. */
950 struct cp_binding_level *b = current_binding_level->level_chain;
951
952 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
953 /* Skip the ctor/dtor cleanup level. */
954 b = b->level_chain;
955
956 /* ARM $8.3 */
957 if (b->kind == sk_function_parms)
958 {
959 error ("declaration of %q#D shadows a parameter", x);
960 err = true;
961 }
962 }
963
964 if (warn_shadow && !err)
965 {
966 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
967 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
968 }
969 }
970
971 /* Maybe warn if shadowing something else. */
972 else if (warn_shadow && !DECL_EXTERNAL (x)
973 /* No shadow warnings for internally generated vars. */
974 && ! DECL_ARTIFICIAL (x)
975 /* No shadow warnings for vars made for inlining. */
976 && ! DECL_FROM_INLINE (x))
977 {
978 tree member;
979
980 if (current_class_ptr)
981 member = lookup_member (current_class_type,
982 name,
983 /*protect=*/0,
984 /*want_type=*/false);
985 else
986 member = NULL_TREE;
987
988 if (member && !TREE_STATIC (member))
989 {
990 /* Location of previous decl is not useful in this case. */
991 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
992 x);
993 }
994 else if (oldlocal != NULL_TREE
995 && TREE_CODE (oldlocal) == VAR_DECL)
996 {
997 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
998 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
999 }
1000 else if (oldglobal != NULL_TREE
1001 && TREE_CODE (oldglobal) == VAR_DECL)
1002 /* XXX shadow warnings in outer-more namespaces */
1003 {
1004 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
1005 x);
1006 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
1007 }
1008 }
1009 }
1010
1011 if (TREE_CODE (x) == VAR_DECL)
1012 maybe_register_incomplete_var (x);
1013 }
1014
1015 if (need_new_binding)
1016 add_decl_to_level (x,
1017 DECL_NAMESPACE_SCOPE_P (x)
1018 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1019 : current_binding_level);
1020
1021 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1022 }
1023
1024 /* Record a decl-node X as belonging to the current lexical scope. */
1025
1026 tree
pushdecl(tree x)1027 pushdecl (tree x)
1028 {
1029 return pushdecl_maybe_friend (x, false);
1030 }
1031
1032 /* Enter DECL into the symbol table, if that's appropriate. Returns
1033 DECL, or a modified version thereof. */
1034
1035 tree
maybe_push_decl(tree decl)1036 maybe_push_decl (tree decl)
1037 {
1038 tree type = TREE_TYPE (decl);
1039
1040 /* Add this decl to the current binding level, but not if it comes
1041 from another scope, e.g. a static member variable. TEM may equal
1042 DECL or it may be a previous decl of the same name. */
1043 if (decl == error_mark_node
1044 || (TREE_CODE (decl) != PARM_DECL
1045 && DECL_CONTEXT (decl) != NULL_TREE
1046 /* Definitions of namespace members outside their namespace are
1047 possible. */
1048 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
1049 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1050 || TREE_CODE (type) == UNKNOWN_TYPE
1051 /* The declaration of a template specialization does not affect
1052 the functions available for overload resolution, so we do not
1053 call pushdecl. */
1054 || (TREE_CODE (decl) == FUNCTION_DECL
1055 && DECL_TEMPLATE_SPECIALIZATION (decl)))
1056 return decl;
1057 else
1058 return pushdecl (decl);
1059 }
1060
1061 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1062 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1063 doesn't really belong to this binding level, that it got here
1064 through a using-declaration. */
1065
1066 void
push_local_binding(tree id,tree decl,int flags)1067 push_local_binding (tree id, tree decl, int flags)
1068 {
1069 struct cp_binding_level *b;
1070
1071 /* Skip over any local classes. This makes sense if we call
1072 push_local_binding with a friend decl of a local class. */
1073 b = innermost_nonclass_level ();
1074
1075 if (lookup_name_innermost_nonclass_level (id))
1076 {
1077 /* Supplement the existing binding. */
1078 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1079 /* It didn't work. Something else must be bound at this
1080 level. Do not add DECL to the list of things to pop
1081 later. */
1082 return;
1083 }
1084 else
1085 /* Create a new binding. */
1086 push_binding (id, decl, b);
1087
1088 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1089 /* We must put the OVERLOAD into a TREE_LIST since the
1090 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1091 decls that got here through a using-declaration. */
1092 decl = build_tree_list (NULL_TREE, decl);
1093
1094 /* And put DECL on the list of things declared by the current
1095 binding level. */
1096 add_decl_to_level (decl, b);
1097 }
1098
1099 /* Check to see whether or not DECL is a variable that would have been
1100 in scope under the ARM, but is not in scope under the ANSI/ISO
1101 standard. If so, issue an error message. If name lookup would
1102 work in both cases, but return a different result, this function
1103 returns the result of ANSI/ISO lookup. Otherwise, it returns
1104 DECL. */
1105
1106 tree
check_for_out_of_scope_variable(tree decl)1107 check_for_out_of_scope_variable (tree decl)
1108 {
1109 tree shadowed;
1110
1111 /* We only care about out of scope variables. */
1112 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1113 return decl;
1114
1115 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1116 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1117 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1118 && DECL_DEAD_FOR_LOCAL (shadowed))
1119 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1120 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1121 if (!shadowed)
1122 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1123 if (shadowed)
1124 {
1125 if (!DECL_ERROR_REPORTED (decl))
1126 {
1127 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1128 warning (0, " matches this %q+D under ISO standard rules",
1129 shadowed);
1130 warning (0, " matches this %q+D under old rules", decl);
1131 DECL_ERROR_REPORTED (decl) = 1;
1132 }
1133 return shadowed;
1134 }
1135
1136 /* If we have already complained about this declaration, there's no
1137 need to do it again. */
1138 if (DECL_ERROR_REPORTED (decl))
1139 return decl;
1140
1141 DECL_ERROR_REPORTED (decl) = 1;
1142
1143 if (TREE_TYPE (decl) == error_mark_node)
1144 return decl;
1145
1146 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1147 {
1148 error ("name lookup of %qD changed for new ISO %<for%> scoping",
1149 DECL_NAME (decl));
1150 error (" cannot use obsolete binding at %q+D because "
1151 "it has a destructor", decl);
1152 return error_mark_node;
1153 }
1154 else
1155 {
1156 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
1157 DECL_NAME (decl));
1158 pedwarn (" using obsolete binding at %q+D", decl);
1159 }
1160
1161 return decl;
1162 }
1163
1164 /* true means unconditionally make a BLOCK for the next level pushed. */
1165
1166 static bool keep_next_level_flag;
1167
1168 static int binding_depth = 0;
1169 static int is_class_level = 0;
1170
1171 static void
indent(int depth)1172 indent (int depth)
1173 {
1174 int i;
1175
1176 for (i = 0; i < depth * 2; i++)
1177 putc (' ', stderr);
1178 }
1179
1180 /* Return a string describing the kind of SCOPE we have. */
1181 static const char *
cxx_scope_descriptor(cxx_scope * scope)1182 cxx_scope_descriptor (cxx_scope *scope)
1183 {
1184 /* The order of this table must match the "scope_kind"
1185 enumerators. */
1186 static const char* scope_kind_names[] = {
1187 "block-scope",
1188 "cleanup-scope",
1189 "try-scope",
1190 "catch-scope",
1191 "for-scope",
1192 "function-parameter-scope",
1193 "class-scope",
1194 "namespace-scope",
1195 "template-parameter-scope",
1196 "template-explicit-spec-scope"
1197 };
1198 const scope_kind kind = scope->explicit_spec_p
1199 ? sk_template_spec : scope->kind;
1200
1201 return scope_kind_names[kind];
1202 }
1203
1204 /* Output a debugging information about SCOPE when performing
1205 ACTION at LINE. */
1206 static void
cxx_scope_debug(cxx_scope * scope,int line,const char * action)1207 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
1208 {
1209 const char *desc = cxx_scope_descriptor (scope);
1210 if (scope->this_entity)
1211 verbatim ("%s %s(%E) %p %d\n", action, desc,
1212 scope->this_entity, (void *) scope, line);
1213 else
1214 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1215 }
1216
1217 /* Return the estimated initial size of the hashtable of a NAMESPACE
1218 scope. */
1219
1220 static inline size_t
namespace_scope_ht_size(tree ns)1221 namespace_scope_ht_size (tree ns)
1222 {
1223 tree name = DECL_NAME (ns);
1224
1225 return name == std_identifier
1226 ? NAMESPACE_STD_HT_SIZE
1227 : (name == global_scope_name
1228 ? GLOBAL_SCOPE_HT_SIZE
1229 : NAMESPACE_ORDINARY_HT_SIZE);
1230 }
1231
1232 /* A chain of binding_level structures awaiting reuse. */
1233
1234 static GTY((deletable)) struct cp_binding_level *free_binding_level;
1235
1236 /* Insert SCOPE as the innermost binding level. */
1237
1238 void
push_binding_level(struct cp_binding_level * scope)1239 push_binding_level (struct cp_binding_level *scope)
1240 {
1241 /* Add it to the front of currently active scopes stack. */
1242 scope->level_chain = current_binding_level;
1243 current_binding_level = scope;
1244 keep_next_level_flag = false;
1245
1246 if (ENABLE_SCOPE_CHECKING)
1247 {
1248 scope->binding_depth = binding_depth;
1249 indent (binding_depth);
1250 cxx_scope_debug (scope, input_line, "push");
1251 is_class_level = 0;
1252 binding_depth++;
1253 }
1254 }
1255
1256 /* Create a new KIND scope and make it the top of the active scopes stack.
1257 ENTITY is the scope of the associated C++ entity (namespace, class,
1258 function); it is NULL otherwise. */
1259
1260 cxx_scope *
begin_scope(scope_kind kind,tree entity)1261 begin_scope (scope_kind kind, tree entity)
1262 {
1263 cxx_scope *scope;
1264
1265 /* Reuse or create a struct for this binding level. */
1266 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1267 {
1268 scope = free_binding_level;
1269 free_binding_level = scope->level_chain;
1270 }
1271 else
1272 scope = GGC_NEW (cxx_scope);
1273 memset (scope, 0, sizeof (cxx_scope));
1274
1275 scope->this_entity = entity;
1276 scope->more_cleanups_ok = true;
1277 switch (kind)
1278 {
1279 case sk_cleanup:
1280 scope->keep = true;
1281 break;
1282
1283 case sk_template_spec:
1284 scope->explicit_spec_p = true;
1285 kind = sk_template_parms;
1286 /* Fall through. */
1287 case sk_template_parms:
1288 case sk_block:
1289 case sk_try:
1290 case sk_catch:
1291 case sk_for:
1292 case sk_class:
1293 case sk_function_parms:
1294 case sk_omp:
1295 scope->keep = keep_next_level_flag;
1296 break;
1297
1298 case sk_namespace:
1299 NAMESPACE_LEVEL (entity) = scope;
1300 scope->static_decls =
1301 VEC_alloc (tree, gc,
1302 DECL_NAME (entity) == std_identifier
1303 || DECL_NAME (entity) == global_scope_name
1304 ? 200 : 10);
1305 break;
1306
1307 default:
1308 /* Should not happen. */
1309 gcc_unreachable ();
1310 break;
1311 }
1312 scope->kind = kind;
1313
1314 push_binding_level (scope);
1315
1316 return scope;
1317 }
1318
1319 /* We're about to leave current scope. Pop the top of the stack of
1320 currently active scopes. Return the enclosing scope, now active. */
1321
1322 cxx_scope *
leave_scope(void)1323 leave_scope (void)
1324 {
1325 cxx_scope *scope = current_binding_level;
1326
1327 if (scope->kind == sk_namespace && class_binding_level)
1328 current_binding_level = class_binding_level;
1329
1330 /* We cannot leave a scope, if there are none left. */
1331 if (NAMESPACE_LEVEL (global_namespace))
1332 gcc_assert (!global_scope_p (scope));
1333
1334 if (ENABLE_SCOPE_CHECKING)
1335 {
1336 indent (--binding_depth);
1337 cxx_scope_debug (scope, input_line, "leave");
1338 if (is_class_level != (scope == class_binding_level))
1339 {
1340 indent (binding_depth);
1341 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
1342 }
1343 is_class_level = 0;
1344 }
1345
1346 #ifdef HANDLE_PRAGMA_VISIBILITY
1347 if (scope->has_visibility)
1348 pop_visibility ();
1349 #endif
1350
1351 /* Move one nesting level up. */
1352 current_binding_level = scope->level_chain;
1353
1354 /* Namespace-scopes are left most probably temporarily, not
1355 completely; they can be reopened later, e.g. in namespace-extension
1356 or any name binding activity that requires us to resume a
1357 namespace. For classes, we cache some binding levels. For other
1358 scopes, we just make the structure available for reuse. */
1359 if (scope->kind != sk_namespace
1360 && scope->kind != sk_class)
1361 {
1362 scope->level_chain = free_binding_level;
1363 gcc_assert (!ENABLE_SCOPE_CHECKING
1364 || scope->binding_depth == binding_depth);
1365 free_binding_level = scope;
1366 }
1367
1368 /* Find the innermost enclosing class scope, and reset
1369 CLASS_BINDING_LEVEL appropriately. */
1370 if (scope->kind == sk_class)
1371 {
1372 class_binding_level = NULL;
1373 for (scope = current_binding_level; scope; scope = scope->level_chain)
1374 if (scope->kind == sk_class)
1375 {
1376 class_binding_level = scope;
1377 break;
1378 }
1379 }
1380
1381 return current_binding_level;
1382 }
1383
1384 static void
resume_scope(struct cp_binding_level * b)1385 resume_scope (struct cp_binding_level* b)
1386 {
1387 /* Resuming binding levels is meant only for namespaces,
1388 and those cannot nest into classes. */
1389 gcc_assert (!class_binding_level);
1390 /* Also, resuming a non-directly nested namespace is a no-no. */
1391 gcc_assert (b->level_chain == current_binding_level);
1392 current_binding_level = b;
1393 if (ENABLE_SCOPE_CHECKING)
1394 {
1395 b->binding_depth = binding_depth;
1396 indent (binding_depth);
1397 cxx_scope_debug (b, input_line, "resume");
1398 is_class_level = 0;
1399 binding_depth++;
1400 }
1401 }
1402
1403 /* Return the innermost binding level that is not for a class scope. */
1404
1405 static cxx_scope *
innermost_nonclass_level(void)1406 innermost_nonclass_level (void)
1407 {
1408 cxx_scope *b;
1409
1410 b = current_binding_level;
1411 while (b->kind == sk_class)
1412 b = b->level_chain;
1413
1414 return b;
1415 }
1416
1417 /* We're defining an object of type TYPE. If it needs a cleanup, but
1418 we're not allowed to add any more objects with cleanups to the current
1419 scope, create a new binding level. */
1420
1421 void
maybe_push_cleanup_level(tree type)1422 maybe_push_cleanup_level (tree type)
1423 {
1424 if (type != error_mark_node
1425 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1426 && current_binding_level->more_cleanups_ok == 0)
1427 {
1428 begin_scope (sk_cleanup, NULL);
1429 current_binding_level->statement_list = push_stmt_list ();
1430 }
1431 }
1432
1433 /* Nonzero if we are currently in the global binding level. */
1434
1435 int
global_bindings_p(void)1436 global_bindings_p (void)
1437 {
1438 return global_scope_p (current_binding_level);
1439 }
1440
1441 /* True if we are currently in a toplevel binding level. This
1442 means either the global binding level or a namespace in a toplevel
1443 binding level. Since there are no non-toplevel namespace levels,
1444 this really means any namespace or template parameter level. We
1445 also include a class whose context is toplevel. */
1446
1447 bool
toplevel_bindings_p(void)1448 toplevel_bindings_p (void)
1449 {
1450 struct cp_binding_level *b = innermost_nonclass_level ();
1451
1452 return b->kind == sk_namespace || b->kind == sk_template_parms;
1453 }
1454
1455 /* True if this is a namespace scope, or if we are defining a class
1456 which is itself at namespace scope, or whose enclosing class is
1457 such a class, etc. */
1458
1459 bool
namespace_bindings_p(void)1460 namespace_bindings_p (void)
1461 {
1462 struct cp_binding_level *b = innermost_nonclass_level ();
1463
1464 return b->kind == sk_namespace;
1465 }
1466
1467 /* True if the current level needs to have a BLOCK made. */
1468
1469 bool
kept_level_p(void)1470 kept_level_p (void)
1471 {
1472 return (current_binding_level->blocks != NULL_TREE
1473 || current_binding_level->keep
1474 || current_binding_level->kind == sk_cleanup
1475 || current_binding_level->names != NULL_TREE);
1476 }
1477
1478 /* Returns the kind of the innermost scope. */
1479
1480 scope_kind
innermost_scope_kind(void)1481 innermost_scope_kind (void)
1482 {
1483 return current_binding_level->kind;
1484 }
1485
1486 /* Returns true if this scope was created to store template parameters. */
1487
1488 bool
template_parm_scope_p(void)1489 template_parm_scope_p (void)
1490 {
1491 return innermost_scope_kind () == sk_template_parms;
1492 }
1493
1494 /* If KEEP is true, make a BLOCK node for the next binding level,
1495 unconditionally. Otherwise, use the normal logic to decide whether
1496 or not to create a BLOCK. */
1497
1498 void
keep_next_level(bool keep)1499 keep_next_level (bool keep)
1500 {
1501 keep_next_level_flag = keep;
1502 }
1503
1504 /* Return the list of declarations of the current level.
1505 Note that this list is in reverse order unless/until
1506 you nreverse it; and when you do nreverse it, you must
1507 store the result back using `storedecls' or you will lose. */
1508
1509 tree
getdecls(void)1510 getdecls (void)
1511 {
1512 return current_binding_level->names;
1513 }
1514
1515 /* For debugging. */
1516 static int no_print_functions = 0;
1517 static int no_print_builtins = 0;
1518
1519 static void
print_binding_level(struct cp_binding_level * lvl)1520 print_binding_level (struct cp_binding_level* lvl)
1521 {
1522 tree t;
1523 int i = 0, len;
1524 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1525 if (lvl->more_cleanups_ok)
1526 fprintf (stderr, " more-cleanups-ok");
1527 if (lvl->have_cleanups)
1528 fprintf (stderr, " have-cleanups");
1529 fprintf (stderr, "\n");
1530 if (lvl->names)
1531 {
1532 fprintf (stderr, " names:\t");
1533 /* We can probably fit 3 names to a line? */
1534 for (t = lvl->names; t; t = TREE_CHAIN (t))
1535 {
1536 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1537 continue;
1538 if (no_print_builtins
1539 && (TREE_CODE (t) == TYPE_DECL)
1540 && DECL_IS_BUILTIN (t))
1541 continue;
1542
1543 /* Function decls tend to have longer names. */
1544 if (TREE_CODE (t) == FUNCTION_DECL)
1545 len = 3;
1546 else
1547 len = 2;
1548 i += len;
1549 if (i > 6)
1550 {
1551 fprintf (stderr, "\n\t");
1552 i = len;
1553 }
1554 print_node_brief (stderr, "", t, 0);
1555 if (t == error_mark_node)
1556 break;
1557 }
1558 if (i)
1559 fprintf (stderr, "\n");
1560 }
1561 if (VEC_length (cp_class_binding, lvl->class_shadowed))
1562 {
1563 size_t i;
1564 cp_class_binding *b;
1565 fprintf (stderr, " class-shadowed:");
1566 for (i = 0;
1567 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
1568 ++i)
1569 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1570 fprintf (stderr, "\n");
1571 }
1572 if (lvl->type_shadowed)
1573 {
1574 fprintf (stderr, " type-shadowed:");
1575 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1576 {
1577 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1578 }
1579 fprintf (stderr, "\n");
1580 }
1581 }
1582
1583 void
print_other_binding_stack(struct cp_binding_level * stack)1584 print_other_binding_stack (struct cp_binding_level *stack)
1585 {
1586 struct cp_binding_level *level;
1587 for (level = stack; !global_scope_p (level); level = level->level_chain)
1588 {
1589 fprintf (stderr, "binding level %p\n", (void *) level);
1590 print_binding_level (level);
1591 }
1592 }
1593
1594 void
print_binding_stack(void)1595 print_binding_stack (void)
1596 {
1597 struct cp_binding_level *b;
1598 fprintf (stderr, "current_binding_level=%p\n"
1599 "class_binding_level=%p\n"
1600 "NAMESPACE_LEVEL (global_namespace)=%p\n",
1601 (void *) current_binding_level, (void *) class_binding_level,
1602 (void *) NAMESPACE_LEVEL (global_namespace));
1603 if (class_binding_level)
1604 {
1605 for (b = class_binding_level; b; b = b->level_chain)
1606 if (b == current_binding_level)
1607 break;
1608 if (b)
1609 b = class_binding_level;
1610 else
1611 b = current_binding_level;
1612 }
1613 else
1614 b = current_binding_level;
1615 print_other_binding_stack (b);
1616 fprintf (stderr, "global:\n");
1617 print_binding_level (NAMESPACE_LEVEL (global_namespace));
1618 }
1619
1620 /* Return the type associated with id. */
1621
1622 tree
identifier_type_value(tree id)1623 identifier_type_value (tree id)
1624 {
1625 timevar_push (TV_NAME_LOOKUP);
1626 /* There is no type with that name, anywhere. */
1627 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1628 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1629 /* This is not the type marker, but the real thing. */
1630 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1631 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
1632 /* Have to search for it. It must be on the global level, now.
1633 Ask lookup_name not to return non-types. */
1634 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1635 if (id)
1636 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
1637 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1638 }
1639
1640 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1641 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
1642
1643 tree
identifier_global_value(tree t)1644 identifier_global_value (tree t)
1645 {
1646 return IDENTIFIER_GLOBAL_VALUE (t);
1647 }
1648
1649 /* Push a definition of struct, union or enum tag named ID. into
1650 binding_level B. DECL is a TYPE_DECL for the type. We assume that
1651 the tag ID is not already defined. */
1652
1653 static void
set_identifier_type_value_with_scope(tree id,tree decl,cxx_scope * b)1654 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
1655 {
1656 tree type;
1657
1658 if (b->kind != sk_namespace)
1659 {
1660 /* Shadow the marker, not the real thing, so that the marker
1661 gets restored later. */
1662 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1663 b->type_shadowed
1664 = tree_cons (id, old_type_value, b->type_shadowed);
1665 type = decl ? TREE_TYPE (decl) : NULL_TREE;
1666 TREE_TYPE (b->type_shadowed) = type;
1667 }
1668 else
1669 {
1670 cxx_binding *binding =
1671 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1672 gcc_assert (decl);
1673 if (binding->value)
1674 supplement_binding (binding, decl);
1675 else
1676 binding->value = decl;
1677
1678 /* Store marker instead of real type. */
1679 type = global_type_node;
1680 }
1681 SET_IDENTIFIER_TYPE_VALUE (id, type);
1682 }
1683
1684 /* As set_identifier_type_value_with_scope, but using
1685 current_binding_level. */
1686
1687 void
set_identifier_type_value(tree id,tree decl)1688 set_identifier_type_value (tree id, tree decl)
1689 {
1690 set_identifier_type_value_with_scope (id, decl, current_binding_level);
1691 }
1692
1693 /* Return the name for the constructor (or destructor) for the
1694 specified class TYPE. When given a template, this routine doesn't
1695 lose the specialization. */
1696
1697 static inline tree
constructor_name_full(tree type)1698 constructor_name_full (tree type)
1699 {
1700 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1701 }
1702
1703 /* Return the name for the constructor (or destructor) for the
1704 specified class. When given a template, return the plain
1705 unspecialized name. */
1706
1707 tree
constructor_name(tree type)1708 constructor_name (tree type)
1709 {
1710 tree name;
1711 name = constructor_name_full (type);
1712 if (IDENTIFIER_TEMPLATE (name))
1713 name = IDENTIFIER_TEMPLATE (name);
1714 return name;
1715 }
1716
1717 /* Returns TRUE if NAME is the name for the constructor for TYPE. */
1718
1719 bool
constructor_name_p(tree name,tree type)1720 constructor_name_p (tree name, tree type)
1721 {
1722 tree ctor_name;
1723
1724 if (!name)
1725 return false;
1726
1727 if (TREE_CODE (name) != IDENTIFIER_NODE)
1728 return false;
1729
1730 ctor_name = constructor_name_full (type);
1731 if (name == ctor_name)
1732 return true;
1733 if (IDENTIFIER_TEMPLATE (ctor_name)
1734 && name == IDENTIFIER_TEMPLATE (ctor_name))
1735 return true;
1736 return false;
1737 }
1738
1739 /* Counter used to create anonymous type names. */
1740
1741 static GTY(()) int anon_cnt;
1742
1743 /* Return an IDENTIFIER which can be used as a name for
1744 anonymous structs and unions. */
1745
1746 tree
make_anon_name(void)1747 make_anon_name (void)
1748 {
1749 char buf[32];
1750
1751 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1752 return get_identifier (buf);
1753 }
1754
1755 /* Return (from the stack of) the BINDING, if any, established at SCOPE. */
1756
1757 static inline cxx_binding *
find_binding(cxx_scope * scope,cxx_binding * binding)1758 find_binding (cxx_scope *scope, cxx_binding *binding)
1759 {
1760 timevar_push (TV_NAME_LOOKUP);
1761
1762 for (; binding != NULL; binding = binding->previous)
1763 if (binding->scope == scope)
1764 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
1765
1766 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
1767 }
1768
1769 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
1770
1771 static inline cxx_binding *
cxx_scope_find_binding_for_name(cxx_scope * scope,tree name)1772 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
1773 {
1774 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
1775 if (b)
1776 {
1777 /* Fold-in case where NAME is used only once. */
1778 if (scope == b->scope && b->previous == NULL)
1779 return b;
1780 return find_binding (scope, b);
1781 }
1782 return NULL;
1783 }
1784
1785 /* Always returns a binding for name in scope. If no binding is
1786 found, make a new one. */
1787
1788 static cxx_binding *
binding_for_name(cxx_scope * scope,tree name)1789 binding_for_name (cxx_scope *scope, tree name)
1790 {
1791 cxx_binding *result;
1792
1793 result = cxx_scope_find_binding_for_name (scope, name);
1794 if (result)
1795 return result;
1796 /* Not found, make a new one. */
1797 result = cxx_binding_make (NULL, NULL);
1798 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
1799 result->scope = scope;
1800 result->is_local = false;
1801 result->value_is_inherited = false;
1802 /* APPLE LOCAL blocks 6040305 (ch) */
1803 result->declared_in_block = 0;
1804 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
1805 return result;
1806 }
1807
1808 /* Insert another USING_DECL into the current binding level, returning
1809 this declaration. If this is a redeclaration, do nothing, and
1810 return NULL_TREE if this not in namespace scope (in namespace
1811 scope, a using decl might extend any previous bindings). */
1812
1813 static tree
push_using_decl(tree scope,tree name)1814 push_using_decl (tree scope, tree name)
1815 {
1816 tree decl;
1817
1818 timevar_push (TV_NAME_LOOKUP);
1819 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
1820 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1821 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
1822 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
1823 break;
1824 if (decl)
1825 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
1826 namespace_bindings_p () ? decl : NULL_TREE);
1827 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
1828 USING_DECL_SCOPE (decl) = scope;
1829 TREE_CHAIN (decl) = current_binding_level->usings;
1830 current_binding_level->usings = decl;
1831 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1832 }
1833
1834 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
1835 caller to set DECL_CONTEXT properly. */
1836
1837 tree
pushdecl_with_scope(tree x,cxx_scope * level,bool is_friend)1838 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
1839 {
1840 struct cp_binding_level *b;
1841 tree function_decl = current_function_decl;
1842
1843 timevar_push (TV_NAME_LOOKUP);
1844 current_function_decl = NULL_TREE;
1845 if (level->kind == sk_class)
1846 {
1847 b = class_binding_level;
1848 class_binding_level = level;
1849 pushdecl_class_level (x);
1850 class_binding_level = b;
1851 }
1852 else
1853 {
1854 b = current_binding_level;
1855 current_binding_level = level;
1856 x = pushdecl_maybe_friend (x, is_friend);
1857 current_binding_level = b;
1858 }
1859 current_function_decl = function_decl;
1860 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
1861 }
1862
1863 /* DECL is a FUNCTION_DECL for a non-member function, which may have
1864 other definitions already in place. We get around this by making
1865 the value of the identifier point to a list of all the things that
1866 want to be referenced by that name. It is then up to the users of
1867 that name to decide what to do with that list.
1868
1869 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
1870 DECL_TEMPLATE_RESULT. It is dealt with the same way.
1871
1872 FLAGS is a bitwise-or of the following values:
1873 PUSH_LOCAL: Bind DECL in the current scope, rather than at
1874 namespace scope.
1875 PUSH_USING: DECL is being pushed as the result of a using
1876 declaration.
1877
1878 IS_FRIEND is true if this is a friend declaration.
1879
1880 The value returned may be a previous declaration if we guessed wrong
1881 about what language DECL should belong to (C or C++). Otherwise,
1882 it's always DECL (and never something that's not a _DECL). */
1883
1884 static tree
push_overloaded_decl(tree decl,int flags,bool is_friend)1885 push_overloaded_decl (tree decl, int flags, bool is_friend)
1886 {
1887 tree name = DECL_NAME (decl);
1888 tree old;
1889 tree new_binding;
1890 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
1891
1892 timevar_push (TV_NAME_LOOKUP);
1893 if (doing_global)
1894 old = namespace_binding (name, DECL_CONTEXT (decl));
1895 else
1896 old = lookup_name_innermost_nonclass_level (name);
1897
1898 if (old)
1899 {
1900 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
1901 {
1902 tree t = TREE_TYPE (old);
1903 if (IS_AGGR_TYPE (t) && warn_shadow
1904 && (! DECL_IN_SYSTEM_HEADER (decl)
1905 || ! DECL_IN_SYSTEM_HEADER (old)))
1906 warning (0, "%q#D hides constructor for %q#T", decl, t);
1907 old = NULL_TREE;
1908 }
1909 else if (is_overloaded_fn (old))
1910 {
1911 tree tmp;
1912
1913 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
1914 {
1915 tree fn = OVL_CURRENT (tmp);
1916 tree dup;
1917
1918 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
1919 && !(flags & PUSH_USING)
1920 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1921 TYPE_ARG_TYPES (TREE_TYPE (decl)))
1922 && ! decls_match (fn, decl))
1923 error ("%q#D conflicts with previous using declaration %q#D",
1924 decl, fn);
1925
1926 dup = duplicate_decls (decl, fn, is_friend);
1927 /* If DECL was a redeclaration of FN -- even an invalid
1928 one -- pass that information along to our caller. */
1929 if (dup == fn || dup == error_mark_node)
1930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
1931 }
1932
1933 /* We don't overload implicit built-ins. duplicate_decls()
1934 may fail to merge the decls if the new decl is e.g. a
1935 template function. */
1936 if (TREE_CODE (old) == FUNCTION_DECL
1937 && DECL_ANTICIPATED (old)
1938 && !DECL_HIDDEN_FRIEND_P (old))
1939 old = NULL;
1940 }
1941 else if (old == error_mark_node)
1942 /* Ignore the undefined symbol marker. */
1943 old = NULL_TREE;
1944 else
1945 {
1946 error ("previous non-function declaration %q+#D", old);
1947 error ("conflicts with function declaration %q#D", decl);
1948 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
1949 }
1950 }
1951
1952 if (old || TREE_CODE (decl) == TEMPLATE_DECL
1953 /* If it's a using declaration, we always need to build an OVERLOAD,
1954 because it's the only way to remember that the declaration comes
1955 from 'using', and have the lookup behave correctly. */
1956 || (flags & PUSH_USING))
1957 {
1958 if (old && TREE_CODE (old) != OVERLOAD)
1959 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
1960 else
1961 new_binding = ovl_cons (decl, old);
1962 if (flags & PUSH_USING)
1963 OVL_USED (new_binding) = 1;
1964 }
1965 else
1966 /* NAME is not ambiguous. */
1967 new_binding = decl;
1968
1969 if (doing_global)
1970 set_namespace_binding (name, current_namespace, new_binding);
1971 else
1972 {
1973 /* We only create an OVERLOAD if there was a previous binding at
1974 this level, or if decl is a template. In the former case, we
1975 need to remove the old binding and replace it with the new
1976 binding. We must also run through the NAMES on the binding
1977 level where the name was bound to update the chain. */
1978
1979 if (TREE_CODE (new_binding) == OVERLOAD && old)
1980 {
1981 tree *d;
1982
1983 for (d = &IDENTIFIER_BINDING (name)->scope->names;
1984 *d;
1985 d = &TREE_CHAIN (*d))
1986 if (*d == old
1987 || (TREE_CODE (*d) == TREE_LIST
1988 && TREE_VALUE (*d) == old))
1989 {
1990 if (TREE_CODE (*d) == TREE_LIST)
1991 /* Just replace the old binding with the new. */
1992 TREE_VALUE (*d) = new_binding;
1993 else
1994 /* Build a TREE_LIST to wrap the OVERLOAD. */
1995 *d = tree_cons (NULL_TREE, new_binding,
1996 TREE_CHAIN (*d));
1997
1998 /* And update the cxx_binding node. */
1999 IDENTIFIER_BINDING (name)->value = new_binding;
2000 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2001 }
2002
2003 /* We should always find a previous binding in this case. */
2004 gcc_unreachable ();
2005 }
2006
2007 /* Install the new binding. */
2008 push_local_binding (name, new_binding, flags);
2009 }
2010
2011 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2012 }
2013
2014 /* Check a non-member using-declaration. Return the name and scope
2015 being used, and the USING_DECL, or NULL_TREE on failure. */
2016
2017 static tree
validate_nonmember_using_decl(tree decl,tree scope,tree name)2018 validate_nonmember_using_decl (tree decl, tree scope, tree name)
2019 {
2020 /* [namespace.udecl]
2021 A using-declaration for a class member shall be a
2022 member-declaration. */
2023 if (TYPE_P (scope))
2024 {
2025 error ("%qT is not a namespace", scope);
2026 return NULL_TREE;
2027 }
2028 else if (scope == error_mark_node)
2029 return NULL_TREE;
2030
2031 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2032 {
2033 /* 7.3.3/5
2034 A using-declaration shall not name a template-id. */
2035 error ("a using-declaration cannot specify a template-id. "
2036 "Try %<using %D%>", name);
2037 return NULL_TREE;
2038 }
2039
2040 if (TREE_CODE (decl) == NAMESPACE_DECL)
2041 {
2042 error ("namespace %qD not allowed in using-declaration", decl);
2043 return NULL_TREE;
2044 }
2045
2046 if (TREE_CODE (decl) == SCOPE_REF)
2047 {
2048 /* It's a nested name with template parameter dependent scope.
2049 This can only be using-declaration for class member. */
2050 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2051 return NULL_TREE;
2052 }
2053
2054 if (is_overloaded_fn (decl))
2055 decl = get_first_fn (decl);
2056
2057 gcc_assert (DECL_P (decl));
2058
2059 /* Make a USING_DECL. */
2060 return push_using_decl (scope, name);
2061 }
2062
2063 /* Process local and global using-declarations. */
2064
2065 static void
do_nonmember_using_decl(tree scope,tree name,tree oldval,tree oldtype,tree * newval,tree * newtype)2066 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2067 tree *newval, tree *newtype)
2068 {
2069 struct scope_binding decls = EMPTY_SCOPE_BINDING;
2070
2071 *newval = *newtype = NULL_TREE;
2072 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2073 /* Lookup error */
2074 return;
2075
2076 if (!decls.value && !decls.type)
2077 {
2078 error ("%qD not declared", name);
2079 return;
2080 }
2081
2082 /* LLVM LOCAL begin mainline */
2083 /* Shift the old and new bindings around so we're comparing class and
2084 enumeration names to each other. */
2085 if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2086 {
2087 oldtype = oldval;
2088 oldval = NULL_TREE;
2089 }
2090
2091 if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2092 {
2093 decls.type = decls.value;
2094 decls.value = NULL_TREE;
2095 }
2096 /* LLVM LOCAL end mainline */
2097
2098 /* It is impossible to overload a built-in function; any explicit
2099 declaration eliminates the built-in declaration. So, if OLDVAL
2100 is a built-in, then we can just pretend it isn't there. */
2101 if (oldval
2102 && TREE_CODE (oldval) == FUNCTION_DECL
2103 && DECL_ANTICIPATED (oldval)
2104 && !DECL_HIDDEN_FRIEND_P (oldval))
2105 oldval = NULL_TREE;
2106
2107 /* LLVM LOCAL begin mainline */
2108 if (decls.value)
2109 {
2110 /* Check for using functions. */
2111 if (is_overloaded_fn (decls.value))
2112 {
2113 tree tmp, tmp1;
2114
2115 if (oldval && !is_overloaded_fn (oldval))
2116 {
2117 error ("%qD is already declared in this scope", name);
2118 oldval = NULL_TREE;
2119 }
2120
2121 *newval = oldval;
2122 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2123 {
2124 tree new_fn = OVL_CURRENT (tmp);
2125
2126 /* [namespace.udecl]
2127
2128 If a function declaration in namespace scope or block
2129 scope has the same name and the same parameter types as a
2130 function introduced by a using declaration the program is
2131 ill-formed. */
2132 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2133 {
2134 tree old_fn = OVL_CURRENT (tmp1);
2135
2136 if (new_fn == old_fn)
2137 /* The function already exists in the current namespace. */
2138 break;
2139 else if (OVL_USED (tmp1))
2140 continue; /* this is a using decl */
2141 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2142 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2143 {
2144 gcc_assert (!DECL_ANTICIPATED (old_fn)
2145 || DECL_HIDDEN_FRIEND_P (old_fn));
2146
2147 /* There was already a non-using declaration in
2148 this scope with the same parameter types. If both
2149 are the same extern "C" functions, that's ok. */
2150 if (decls_match (new_fn, old_fn))
2151 break;
2152 else
2153 {
2154 error ("%qD is already declared in this scope", name);
2155 break;
2156 }
2157 }
2158 }
2159
2160 /* If we broke out of the loop, there's no reason to add
2161 this function to the using declarations for this
2162 scope. */
2163 if (tmp1)
2164 continue;
2165
2166 /* If we are adding to an existing OVERLOAD, then we no
2167 longer know the type of the set of functions. */
2168 if (*newval && TREE_CODE (*newval) == OVERLOAD)
2169 TREE_TYPE (*newval) = unknown_type_node;
2170 /* Add this new function to the set. */
2171 *newval = build_overload (OVL_CURRENT (tmp), *newval);
2172 /* If there is only one function, then we use its type. (A
2173 using-declaration naming a single function can be used in
2174 contexts where overload resolution cannot be
2175 performed.) */
2176 if (TREE_CODE (*newval) != OVERLOAD)
2177 {
2178 *newval = ovl_cons (*newval, NULL_TREE);
2179 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2180 }
2181 OVL_USED (*newval) = 1;
2182 }
2183 }
2184 else
2185 {
2186 *newval = decls.value;
2187 if (oldval && !decls_match (*newval, oldval))
2188 error ("%qD is already declared in this scope", name);
2189 }
2190 }
2191 else
2192 *newval = oldval;
2193
2194 if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2195 {
2196 error ("reference to %qD is ambiguous", name);
2197 print_candidates (decls.type);
2198 }
2199 else
2200 {
2201 *newtype = decls.type;
2202 if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2203 error ("%qD is already declared in this scope", name);
2204 }
2205
2206 /* If *newval is empty, shift any class or enumeration name down. */
2207 if (!*newval)
2208 {
2209 *newval = *newtype;
2210 *newtype = NULL_TREE;
2211 }
2212 /* LLVM LOCAL end mainline */
2213 }
2214
2215 /* Process a using-declaration at function scope. */
2216
2217 void
do_local_using_decl(tree decl,tree scope,tree name)2218 do_local_using_decl (tree decl, tree scope, tree name)
2219 {
2220 tree oldval, oldtype, newval, newtype;
2221 tree orig_decl = decl;
2222
2223 decl = validate_nonmember_using_decl (decl, scope, name);
2224 if (decl == NULL_TREE)
2225 return;
2226
2227 if (building_stmt_tree ()
2228 && at_function_scope_p ())
2229 add_decl_expr (decl);
2230
2231 oldval = lookup_name_innermost_nonclass_level (name);
2232 oldtype = lookup_type_current_level (name);
2233
2234 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2235
2236 if (newval)
2237 {
2238 if (is_overloaded_fn (newval))
2239 {
2240 tree fn, term;
2241
2242 /* We only need to push declarations for those functions
2243 that were not already bound in the current level.
2244 The old value might be NULL_TREE, it might be a single
2245 function, or an OVERLOAD. */
2246 if (oldval && TREE_CODE (oldval) == OVERLOAD)
2247 term = OVL_FUNCTION (oldval);
2248 else
2249 term = oldval;
2250 for (fn = newval; fn && OVL_CURRENT (fn) != term;
2251 fn = OVL_NEXT (fn))
2252 push_overloaded_decl (OVL_CURRENT (fn),
2253 PUSH_LOCAL | PUSH_USING,
2254 false);
2255 }
2256 else
2257 push_local_binding (name, newval, PUSH_USING);
2258 }
2259 if (newtype)
2260 {
2261 push_local_binding (name, newtype, PUSH_USING);
2262 set_identifier_type_value (name, newtype);
2263 }
2264
2265 /* Emit debug info. */
2266 if (!processing_template_decl)
2267 cp_emit_debug_info_for_using (orig_decl, current_scope());
2268 }
2269
2270 /* Returns true if ROOT (a namespace, class, or function) encloses
2271 CHILD. CHILD may be either a class type or a namespace. */
2272
2273 bool
is_ancestor(tree root,tree child)2274 is_ancestor (tree root, tree child)
2275 {
2276 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2277 || TREE_CODE (root) == FUNCTION_DECL
2278 || CLASS_TYPE_P (root)));
2279 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2280 || CLASS_TYPE_P (child)));
2281
2282 /* The global namespace encloses everything. */
2283 if (root == global_namespace)
2284 return true;
2285
2286 while (true)
2287 {
2288 /* If we've run out of scopes, stop. */
2289 if (!child)
2290 return false;
2291 /* If we've reached the ROOT, it encloses CHILD. */
2292 if (root == child)
2293 return true;
2294 /* Go out one level. */
2295 if (TYPE_P (child))
2296 child = TYPE_NAME (child);
2297 child = DECL_CONTEXT (child);
2298 }
2299 }
2300
2301 /* Enter the class or namespace scope indicated by T suitable for name
2302 lookup. T can be arbitrary scope, not necessary nested inside the
2303 current scope. Returns a non-null scope to pop iff pop_scope
2304 should be called later to exit this scope. */
2305
2306 tree
push_scope(tree t)2307 push_scope (tree t)
2308 {
2309 if (TREE_CODE (t) == NAMESPACE_DECL)
2310 push_decl_namespace (t);
2311 else if (CLASS_TYPE_P (t))
2312 {
2313 if (!at_class_scope_p ()
2314 || !same_type_p (current_class_type, t))
2315 push_nested_class (t);
2316 else
2317 /* T is the same as the current scope. There is therefore no
2318 need to re-enter the scope. Since we are not actually
2319 pushing a new scope, our caller should not call
2320 pop_scope. */
2321 t = NULL_TREE;
2322 }
2323
2324 return t;
2325 }
2326
2327 /* Leave scope pushed by push_scope. */
2328
2329 void
pop_scope(tree t)2330 pop_scope (tree t)
2331 {
2332 if (TREE_CODE (t) == NAMESPACE_DECL)
2333 pop_decl_namespace ();
2334 else if CLASS_TYPE_P (t)
2335 pop_nested_class ();
2336 }
2337
2338 /* Subroutine of push_inner_scope. */
2339
2340 static void
push_inner_scope_r(tree outer,tree inner)2341 push_inner_scope_r (tree outer, tree inner)
2342 {
2343 tree prev;
2344
2345 if (outer == inner
2346 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2347 return;
2348
2349 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2350 if (outer != prev)
2351 push_inner_scope_r (outer, prev);
2352 if (TREE_CODE (inner) == NAMESPACE_DECL)
2353 {
2354 struct cp_binding_level *save_template_parm = 0;
2355 /* Temporary take out template parameter scopes. They are saved
2356 in reversed order in save_template_parm. */
2357 while (current_binding_level->kind == sk_template_parms)
2358 {
2359 struct cp_binding_level *b = current_binding_level;
2360 current_binding_level = b->level_chain;
2361 b->level_chain = save_template_parm;
2362 save_template_parm = b;
2363 }
2364
2365 resume_scope (NAMESPACE_LEVEL (inner));
2366 current_namespace = inner;
2367
2368 /* Restore template parameter scopes. */
2369 while (save_template_parm)
2370 {
2371 struct cp_binding_level *b = save_template_parm;
2372 save_template_parm = b->level_chain;
2373 b->level_chain = current_binding_level;
2374 current_binding_level = b;
2375 }
2376 }
2377 else
2378 pushclass (inner);
2379 }
2380
2381 /* Enter the scope INNER from current scope. INNER must be a scope
2382 nested inside current scope. This works with both name lookup and
2383 pushing name into scope. In case a template parameter scope is present,
2384 namespace is pushed under the template parameter scope according to
2385 name lookup rule in 14.6.1/6.
2386
2387 Return the former current scope suitable for pop_inner_scope. */
2388
2389 tree
push_inner_scope(tree inner)2390 push_inner_scope (tree inner)
2391 {
2392 tree outer = current_scope ();
2393 if (!outer)
2394 outer = current_namespace;
2395
2396 push_inner_scope_r (outer, inner);
2397 return outer;
2398 }
2399
2400 /* Exit the current scope INNER back to scope OUTER. */
2401
2402 void
pop_inner_scope(tree outer,tree inner)2403 pop_inner_scope (tree outer, tree inner)
2404 {
2405 if (outer == inner
2406 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2407 return;
2408
2409 while (outer != inner)
2410 {
2411 if (TREE_CODE (inner) == NAMESPACE_DECL)
2412 {
2413 struct cp_binding_level *save_template_parm = 0;
2414 /* Temporary take out template parameter scopes. They are saved
2415 in reversed order in save_template_parm. */
2416 while (current_binding_level->kind == sk_template_parms)
2417 {
2418 struct cp_binding_level *b = current_binding_level;
2419 current_binding_level = b->level_chain;
2420 b->level_chain = save_template_parm;
2421 save_template_parm = b;
2422 }
2423
2424 pop_namespace ();
2425
2426 /* Restore template parameter scopes. */
2427 while (save_template_parm)
2428 {
2429 struct cp_binding_level *b = save_template_parm;
2430 save_template_parm = b->level_chain;
2431 b->level_chain = current_binding_level;
2432 current_binding_level = b;
2433 }
2434 }
2435 else
2436 popclass ();
2437
2438 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2439 }
2440 }
2441
2442 /* Do a pushlevel for class declarations. */
2443
2444 void
pushlevel_class(void)2445 pushlevel_class (void)
2446 {
2447 if (ENABLE_SCOPE_CHECKING)
2448 is_class_level = 1;
2449
2450 class_binding_level = begin_scope (sk_class, current_class_type);
2451 }
2452
2453 /* ...and a poplevel for class declarations. */
2454
2455 void
poplevel_class(void)2456 poplevel_class (void)
2457 {
2458 struct cp_binding_level *level = class_binding_level;
2459 cp_class_binding *cb;
2460 size_t i;
2461 tree shadowed;
2462
2463 timevar_push (TV_NAME_LOOKUP);
2464 gcc_assert (level != 0);
2465
2466 /* If we're leaving a toplevel class, cache its binding level. */
2467 if (current_class_depth == 1)
2468 previous_class_level = level;
2469 for (shadowed = level->type_shadowed;
2470 shadowed;
2471 shadowed = TREE_CHAIN (shadowed))
2472 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2473
2474 /* Remove the bindings for all of the class-level declarations. */
2475 if (level->class_shadowed)
2476 {
2477 for (i = 0;
2478 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
2479 ++i)
2480 {
2481 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2482 cxx_binding_free (cb->base);
2483 }
2484 ggc_free (level->class_shadowed);
2485 level->class_shadowed = NULL;
2486 }
2487
2488 /* Now, pop out of the binding level which we created up in the
2489 `pushlevel_class' routine. */
2490 if (ENABLE_SCOPE_CHECKING)
2491 is_class_level = 1;
2492
2493 leave_scope ();
2494 timevar_pop (TV_NAME_LOOKUP);
2495 }
2496
2497 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2498 appropriate. DECL is the value to which a name has just been
2499 bound. CLASS_TYPE is the class in which the lookup occurred. */
2500
2501 static void
set_inherited_value_binding_p(cxx_binding * binding,tree decl,tree class_type)2502 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2503 tree class_type)
2504 {
2505 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2506 {
2507 tree context;
2508
2509 if (TREE_CODE (decl) == OVERLOAD)
2510 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
2511 else
2512 {
2513 gcc_assert (DECL_P (decl));
2514 context = context_for_name_lookup (decl);
2515 }
2516
2517 if (is_properly_derived_from (class_type, context))
2518 INHERITED_VALUE_BINDING_P (binding) = 1;
2519 else
2520 INHERITED_VALUE_BINDING_P (binding) = 0;
2521 }
2522 else if (binding->value == decl)
2523 /* We only encounter a TREE_LIST when there is an ambiguity in the
2524 base classes. Such an ambiguity can be overridden by a
2525 definition in this class. */
2526 INHERITED_VALUE_BINDING_P (binding) = 1;
2527 else
2528 INHERITED_VALUE_BINDING_P (binding) = 0;
2529 }
2530
2531 /* Make the declaration of X appear in CLASS scope. */
2532
2533 bool
pushdecl_class_level(tree x)2534 pushdecl_class_level (tree x)
2535 {
2536 tree name;
2537 bool is_valid = true;
2538
2539 timevar_push (TV_NAME_LOOKUP);
2540 /* Get the name of X. */
2541 if (TREE_CODE (x) == OVERLOAD)
2542 name = DECL_NAME (get_first_fn (x));
2543 else
2544 name = DECL_NAME (x);
2545
2546 if (name)
2547 {
2548 is_valid = push_class_level_binding (name, x);
2549 if (TREE_CODE (x) == TYPE_DECL)
2550 set_identifier_type_value (name, x);
2551 }
2552 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2553 {
2554 /* If X is an anonymous aggregate, all of its members are
2555 treated as if they were members of the class containing the
2556 aggregate, for naming purposes. */
2557 tree f;
2558
2559 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
2560 {
2561 location_t save_location = input_location;
2562 input_location = DECL_SOURCE_LOCATION (f);
2563 if (!pushdecl_class_level (f))
2564 is_valid = false;
2565 input_location = save_location;
2566 }
2567 }
2568 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
2569 }
2570
2571 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
2572 scope. If the value returned is non-NULL, and the PREVIOUS field
2573 is not set, callers must set the PREVIOUS field explicitly. */
2574
2575 static cxx_binding *
get_class_binding(tree name,cxx_scope * scope)2576 get_class_binding (tree name, cxx_scope *scope)
2577 {
2578 tree class_type;
2579 tree type_binding;
2580 tree value_binding;
2581 cxx_binding *binding;
2582
2583 class_type = scope->this_entity;
2584
2585 /* Get the type binding. */
2586 type_binding = lookup_member (class_type, name,
2587 /*protect=*/2, /*want_type=*/true);
2588 /* Get the value binding. */
2589 value_binding = lookup_member (class_type, name,
2590 /*protect=*/2, /*want_type=*/false);
2591
2592 if (value_binding
2593 && (TREE_CODE (value_binding) == TYPE_DECL
2594 || DECL_CLASS_TEMPLATE_P (value_binding)
2595 || (TREE_CODE (value_binding) == TREE_LIST
2596 && TREE_TYPE (value_binding) == error_mark_node
2597 && (TREE_CODE (TREE_VALUE (value_binding))
2598 == TYPE_DECL))))
2599 /* We found a type binding, even when looking for a non-type
2600 binding. This means that we already processed this binding
2601 above. */
2602 ;
2603 else if (value_binding)
2604 {
2605 if (TREE_CODE (value_binding) == TREE_LIST
2606 && TREE_TYPE (value_binding) == error_mark_node)
2607 /* NAME is ambiguous. */
2608 ;
2609 else if (BASELINK_P (value_binding))
2610 /* NAME is some overloaded functions. */
2611 value_binding = BASELINK_FUNCTIONS (value_binding);
2612 }
2613
2614 /* If we found either a type binding or a value binding, create a
2615 new binding object. */
2616 if (type_binding || value_binding)
2617 {
2618 binding = new_class_binding (name,
2619 value_binding,
2620 type_binding,
2621 scope);
2622 /* This is a class-scope binding, not a block-scope binding. */
2623 LOCAL_BINDING_P (binding) = 0;
2624 set_inherited_value_binding_p (binding, value_binding, class_type);
2625 }
2626 else
2627 binding = NULL;
2628
2629 return binding;
2630 }
2631
2632 /* Make the declaration(s) of X appear in CLASS scope under the name
2633 NAME. Returns true if the binding is valid. */
2634
2635 bool
push_class_level_binding(tree name,tree x)2636 push_class_level_binding (tree name, tree x)
2637 {
2638 cxx_binding *binding;
2639 tree decl = x;
2640 bool ok;
2641
2642 timevar_push (TV_NAME_LOOKUP);
2643 /* The class_binding_level will be NULL if x is a template
2644 parameter name in a member template. */
2645 if (!class_binding_level)
2646 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2647
2648 if (name == error_mark_node)
2649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2650
2651 /* Check for invalid member names. */
2652 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
2653 /* We could have been passed a tree list if this is an ambiguous
2654 declaration. If so, pull the declaration out because
2655 check_template_shadow will not handle a TREE_LIST. */
2656 if (TREE_CODE (decl) == TREE_LIST
2657 && TREE_TYPE (decl) == error_mark_node)
2658 decl = TREE_VALUE (decl);
2659
2660 check_template_shadow (decl);
2661
2662 /* [class.mem]
2663
2664 If T is the name of a class, then each of the following shall
2665 have a name different from T:
2666
2667 -- every static data member of class T;
2668
2669 -- every member of class T that is itself a type;
2670
2671 -- every enumerator of every member of class T that is an
2672 enumerated type;
2673
2674 -- every member of every anonymous union that is a member of
2675 class T.
2676
2677 (Non-static data members were also forbidden to have the same
2678 name as T until TC1.) */
2679 if ((TREE_CODE (x) == VAR_DECL
2680 || TREE_CODE (x) == CONST_DECL
2681 || (TREE_CODE (x) == TYPE_DECL
2682 && !DECL_SELF_REFERENCE_P (x))
2683 /* A data member of an anonymous union. */
2684 || (TREE_CODE (x) == FIELD_DECL
2685 && DECL_CONTEXT (x) != current_class_type))
2686 && DECL_NAME (x) == constructor_name (current_class_type))
2687 {
2688 tree scope = context_for_name_lookup (x);
2689 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
2690 {
2691 error ("%qD has the same name as the class in which it is "
2692 "declared",
2693 x);
2694 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
2695 }
2696 }
2697
2698 /* Get the current binding for NAME in this class, if any. */
2699 binding = IDENTIFIER_BINDING (name);
2700 if (!binding || binding->scope != class_binding_level)
2701 {
2702 binding = get_class_binding (name, class_binding_level);
2703 /* If a new binding was created, put it at the front of the
2704 IDENTIFIER_BINDING list. */
2705 if (binding)
2706 {
2707 binding->previous = IDENTIFIER_BINDING (name);
2708 IDENTIFIER_BINDING (name) = binding;
2709 }
2710 }
2711
2712 /* If there is already a binding, then we may need to update the
2713 current value. */
2714 if (binding && binding->value)
2715 {
2716 tree bval = binding->value;
2717 tree old_decl = NULL_TREE;
2718
2719 if (INHERITED_VALUE_BINDING_P (binding))
2720 {
2721 /* If the old binding was from a base class, and was for a
2722 tag name, slide it over to make room for the new binding.
2723 The old binding is still visible if explicitly qualified
2724 with a class-key. */
2725 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
2726 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
2727 {
2728 old_decl = binding->type;
2729 binding->type = bval;
2730 binding->value = NULL_TREE;
2731 INHERITED_VALUE_BINDING_P (binding) = 0;
2732 }
2733 else
2734 {
2735 old_decl = bval;
2736 /* Any inherited type declaration is hidden by the type
2737 declaration in the derived class. */
2738 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
2739 binding->type = NULL_TREE;
2740 }
2741 }
2742 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
2743 old_decl = bval;
2744 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
2745 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2746 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
2747 old_decl = bval;
2748 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
2749 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2750
2751 if (old_decl && binding->scope == class_binding_level)
2752 {
2753 binding->value = x;
2754 /* It is always safe to clear INHERITED_VALUE_BINDING_P
2755 here. This function is only used to register bindings
2756 from with the class definition itself. */
2757 INHERITED_VALUE_BINDING_P (binding) = 0;
2758 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
2759 }
2760 }
2761
2762 /* Note that we declared this value so that we can issue an error if
2763 this is an invalid redeclaration of a name already used for some
2764 other purpose. */
2765 note_name_declared_in_class (name, decl);
2766
2767 /* If we didn't replace an existing binding, put the binding on the
2768 stack of bindings for the identifier, and update the shadowed
2769 list. */
2770 if (binding && binding->scope == class_binding_level)
2771 /* Supplement the existing binding. */
2772 ok = supplement_binding (binding, decl);
2773 else
2774 {
2775 /* Create a new binding. */
2776 push_binding (name, decl, class_binding_level);
2777 ok = true;
2778 }
2779
2780 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
2781 }
2782
2783 /* Process "using SCOPE::NAME" in a class scope. Return the
2784 USING_DECL created. */
2785
2786 tree
do_class_using_decl(tree scope,tree name)2787 do_class_using_decl (tree scope, tree name)
2788 {
2789 /* The USING_DECL returned by this function. */
2790 tree value;
2791 /* The declaration (or declarations) name by this using
2792 declaration. NULL if we are in a template and cannot figure out
2793 what has been named. */
2794 tree decl;
2795 /* True if SCOPE is a dependent type. */
2796 bool scope_dependent_p;
2797 /* True if SCOPE::NAME is dependent. */
2798 bool name_dependent_p;
2799 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
2800 bool bases_dependent_p;
2801 tree binfo;
2802 tree base_binfo;
2803 int i;
2804
2805 if (name == error_mark_node)
2806 return NULL_TREE;
2807
2808 if (!scope || !TYPE_P (scope))
2809 {
2810 error ("using-declaration for non-member at class scope");
2811 return NULL_TREE;
2812 }
2813
2814 /* Make sure the name is not invalid */
2815 if (TREE_CODE (name) == BIT_NOT_EXPR)
2816 {
2817 error ("%<%T::%D%> names destructor", scope, name);
2818 return NULL_TREE;
2819 }
2820 if (constructor_name_p (name, scope))
2821 {
2822 error ("%<%T::%D%> names constructor", scope, name);
2823 return NULL_TREE;
2824 }
2825 if (constructor_name_p (name, current_class_type))
2826 {
2827 error ("%<%T::%D%> names constructor in %qT",
2828 scope, name, current_class_type);
2829 return NULL_TREE;
2830 }
2831
2832 scope_dependent_p = dependent_type_p (scope);
2833 name_dependent_p = (scope_dependent_p
2834 || (IDENTIFIER_TYPENAME_P (name)
2835 && dependent_type_p (TREE_TYPE (name))));
2836
2837 bases_dependent_p = false;
2838 if (processing_template_decl)
2839 for (binfo = TYPE_BINFO (current_class_type), i = 0;
2840 BINFO_BASE_ITERATE (binfo, i, base_binfo);
2841 i++)
2842 if (dependent_type_p (TREE_TYPE (base_binfo)))
2843 {
2844 bases_dependent_p = true;
2845 break;
2846 }
2847
2848 decl = NULL_TREE;
2849
2850 /* From [namespace.udecl]:
2851
2852 A using-declaration used as a member-declaration shall refer to a
2853 member of a base class of the class being defined.
2854
2855 In general, we cannot check this constraint in a template because
2856 we do not know the entire set of base classes of the current
2857 class type. However, if all of the base classes are
2858 non-dependent, then we can avoid delaying the check until
2859 instantiation. */
2860 if (!scope_dependent_p)
2861 {
2862 base_kind b_kind;
2863 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
2864 if (b_kind < bk_proper_base)
2865 {
2866 if (!bases_dependent_p)
2867 {
2868 error_not_base_type (scope, current_class_type);
2869 return NULL_TREE;
2870 }
2871 }
2872 else if (!name_dependent_p)
2873 {
2874 decl = lookup_member (binfo, name, 0, false);
2875 if (!decl)
2876 {
2877 error ("no members matching %<%T::%D%> in %q#T", scope, name,
2878 scope);
2879 return NULL_TREE;
2880 }
2881 /* The binfo from which the functions came does not matter. */
2882 if (BASELINK_P (decl))
2883 decl = BASELINK_FUNCTIONS (decl);
2884 }
2885 }
2886
2887 value = build_lang_decl (USING_DECL, name, NULL_TREE);
2888 USING_DECL_DECLS (value) = decl;
2889 USING_DECL_SCOPE (value) = scope;
2890 DECL_DEPENDENT_P (value) = !decl;
2891
2892 return value;
2893 }
2894
2895
2896 /* Return the binding value for name in scope. */
2897
2898 tree
namespace_binding(tree name,tree scope)2899 namespace_binding (tree name, tree scope)
2900 {
2901 cxx_binding *binding;
2902
2903 if (scope == NULL)
2904 scope = global_namespace;
2905 else
2906 /* Unnecessary for the global namespace because it can't be an alias. */
2907 scope = ORIGINAL_NAMESPACE (scope);
2908
2909 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
2910
2911 return binding ? binding->value : NULL_TREE;
2912 }
2913
2914 /* Set the binding value for name in scope. */
2915
2916 void
set_namespace_binding(tree name,tree scope,tree val)2917 set_namespace_binding (tree name, tree scope, tree val)
2918 {
2919 cxx_binding *b;
2920
2921 timevar_push (TV_NAME_LOOKUP);
2922 if (scope == NULL_TREE)
2923 scope = global_namespace;
2924 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
2925 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
2926 b->value = val;
2927 else
2928 supplement_binding (b, val);
2929 timevar_pop (TV_NAME_LOOKUP);
2930 }
2931
2932 /* Set the context of a declaration to scope. Complain if we are not
2933 outside scope. */
2934
2935 void
set_decl_namespace(tree decl,tree scope,bool friendp)2936 set_decl_namespace (tree decl, tree scope, bool friendp)
2937 {
2938 tree old, fn;
2939
2940 /* Get rid of namespace aliases. */
2941 scope = ORIGINAL_NAMESPACE (scope);
2942
2943 /* It is ok for friends to be qualified in parallel space. */
2944 if (!friendp && !is_ancestor (current_namespace, scope))
2945 error ("declaration of %qD not in a namespace surrounding %qD",
2946 decl, scope);
2947 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
2948
2949 /* Writing "int N::i" to declare a variable within "N" is invalid. */
2950 if (scope == current_namespace)
2951 {
2952 if (at_namespace_scope_p ())
2953 error ("explicit qualification in declaration of %qD",
2954 decl);
2955 return;
2956 }
2957
2958 /* See whether this has been declared in the namespace. */
2959 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
2960 if (old == error_mark_node)
2961 /* No old declaration at all. */
2962 goto complain;
2963 if (!is_overloaded_fn (decl))
2964 /* Don't compare non-function decls with decls_match here, since
2965 it can't check for the correct constness at this
2966 point. pushdecl will find those errors later. */
2967 return;
2968 /* Since decl is a function, old should contain a function decl. */
2969 if (!is_overloaded_fn (old))
2970 goto complain;
2971 fn = OVL_CURRENT (old);
2972 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
2973 goto complain;
2974 /* A template can be explicitly specialized in any namespace. */
2975 if (processing_explicit_instantiation)
2976 return;
2977 if (processing_template_decl || processing_specialization)
2978 /* We have not yet called push_template_decl to turn a
2979 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
2980 match. But, we'll check later, when we construct the
2981 template. */
2982 return;
2983 /* Instantiations or specializations of templates may be declared as
2984 friends in any namespace. */
2985 if (friendp && DECL_USE_TEMPLATE (decl))
2986 return;
2987 if (is_overloaded_fn (old))
2988 {
2989 for (; old; old = OVL_NEXT (old))
2990 if (decls_match (decl, OVL_CURRENT (old)))
2991 return;
2992 }
2993 else if (decls_match (decl, old))
2994 return;
2995 complain:
2996 error ("%qD should have been declared inside %qD", decl, scope);
2997 }
2998
2999 /* Return the namespace where the current declaration is declared. */
3000
3001 static tree
current_decl_namespace(void)3002 current_decl_namespace (void)
3003 {
3004 tree result;
3005 /* If we have been pushed into a different namespace, use it. */
3006 if (decl_namespace_list)
3007 return TREE_PURPOSE (decl_namespace_list);
3008
3009 if (current_class_type)
3010 result = decl_namespace_context (current_class_type);
3011 else if (current_function_decl)
3012 result = decl_namespace_context (current_function_decl);
3013 else
3014 result = current_namespace;
3015 return result;
3016 }
3017
3018 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
3019 select a name that is unique to this compilation unit. */
3020
3021 void
push_namespace(tree name)3022 push_namespace (tree name)
3023 {
3024 push_namespace_with_attribs (name, NULL_TREE);
3025 }
3026
3027 /* Same, but specify attributes to apply to the namespace. The attributes
3028 only apply to the current namespace-body, not to any later extensions. */
3029
3030 void
push_namespace_with_attribs(tree name,tree attributes)3031 push_namespace_with_attribs (tree name, tree attributes)
3032 {
3033 tree d = NULL_TREE;
3034 int need_new = 1;
3035 int implicit_use = 0;
3036 bool anon = !name;
3037
3038 timevar_push (TV_NAME_LOOKUP);
3039
3040 /* We should not get here if the global_namespace is not yet constructed
3041 nor if NAME designates the global namespace: The global scope is
3042 constructed elsewhere. */
3043 gcc_assert (global_namespace != NULL && name != global_scope_name);
3044
3045 if (anon)
3046 {
3047 name = get_anonymous_namespace_name();
3048 d = IDENTIFIER_NAMESPACE_VALUE (name);
3049 if (d)
3050 /* Reopening anonymous namespace. */
3051 need_new = 0;
3052 implicit_use = 1;
3053 }
3054 else
3055 {
3056 /* Check whether this is an extended namespace definition. */
3057 d = IDENTIFIER_NAMESPACE_VALUE (name);
3058 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3059 {
3060 need_new = 0;
3061 if (DECL_NAMESPACE_ALIAS (d))
3062 {
3063 error ("namespace alias %qD not allowed here, assuming %qD",
3064 d, DECL_NAMESPACE_ALIAS (d));
3065 d = DECL_NAMESPACE_ALIAS (d);
3066 }
3067 }
3068 }
3069
3070 if (need_new)
3071 {
3072 /* Make a new namespace, binding the name to it. */
3073 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3074 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3075 /* The name of this namespace is not visible to other translation
3076 units if it is an anonymous namespace or member thereof. */
3077 if (anon || decl_anon_ns_mem_p (current_namespace))
3078 TREE_PUBLIC (d) = 0;
3079 else
3080 TREE_PUBLIC (d) = 1;
3081 pushdecl (d);
3082 if (anon)
3083 {
3084 /* Clear DECL_NAME for the benefit of debugging back ends. */
3085 SET_DECL_ASSEMBLER_NAME (d, name);
3086 DECL_NAME (d) = NULL_TREE;
3087 }
3088 begin_scope (sk_namespace, d);
3089 }
3090 else
3091 resume_scope (NAMESPACE_LEVEL (d));
3092
3093 if (implicit_use)
3094 do_using_directive (d);
3095 /* Enter the name space. */
3096 current_namespace = d;
3097
3098 #ifdef HANDLE_PRAGMA_VISIBILITY
3099 /* Clear has_visibility in case a previous namespace-definition had a
3100 visibility attribute and this one doesn't. */
3101 current_binding_level->has_visibility = 0;
3102 for (d = attributes; d; d = TREE_CHAIN (d))
3103 {
3104 tree name = TREE_PURPOSE (d);
3105 tree args = TREE_VALUE (d);
3106 tree x;
3107
3108 if (! is_attribute_p ("visibility", name))
3109 {
3110 warning (OPT_Wattributes, "%qs attribute directive ignored",
3111 IDENTIFIER_POINTER (name));
3112 continue;
3113 }
3114
3115 x = args ? TREE_VALUE (args) : NULL_TREE;
3116 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3117 {
3118 warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
3119 IDENTIFIER_POINTER (name));
3120 continue;
3121 }
3122
3123 current_binding_level->has_visibility = 1;
3124 push_visibility (TREE_STRING_POINTER (x));
3125 goto found;
3126 }
3127 found:
3128 #endif
3129
3130 timevar_pop (TV_NAME_LOOKUP);
3131 }
3132
3133 /* Pop from the scope of the current namespace. */
3134
3135 void
pop_namespace(void)3136 pop_namespace (void)
3137 {
3138 gcc_assert (current_namespace != global_namespace);
3139 current_namespace = CP_DECL_CONTEXT (current_namespace);
3140 /* The binding level is not popped, as it might be re-opened later. */
3141 leave_scope ();
3142 }
3143
3144 /* Push into the scope of the namespace NS, even if it is deeply
3145 nested within another namespace. */
3146
3147 void
push_nested_namespace(tree ns)3148 push_nested_namespace (tree ns)
3149 {
3150 if (ns == global_namespace)
3151 push_to_top_level ();
3152 else
3153 {
3154 push_nested_namespace (CP_DECL_CONTEXT (ns));
3155 push_namespace (DECL_NAME (ns));
3156 }
3157 }
3158
3159 /* Pop back from the scope of the namespace NS, which was previously
3160 entered with push_nested_namespace. */
3161
3162 void
pop_nested_namespace(tree ns)3163 pop_nested_namespace (tree ns)
3164 {
3165 timevar_push (TV_NAME_LOOKUP);
3166 while (ns != global_namespace)
3167 {
3168 pop_namespace ();
3169 ns = CP_DECL_CONTEXT (ns);
3170 }
3171
3172 pop_from_top_level ();
3173 timevar_pop (TV_NAME_LOOKUP);
3174 }
3175
3176 /* Temporarily set the namespace for the current declaration. */
3177
3178 void
push_decl_namespace(tree decl)3179 push_decl_namespace (tree decl)
3180 {
3181 if (TREE_CODE (decl) != NAMESPACE_DECL)
3182 decl = decl_namespace_context (decl);
3183 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
3184 NULL_TREE, decl_namespace_list);
3185 }
3186
3187 /* [namespace.memdef]/2 */
3188
3189 void
pop_decl_namespace(void)3190 pop_decl_namespace (void)
3191 {
3192 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
3193 }
3194
3195 /* Return the namespace that is the common ancestor
3196 of two given namespaces. */
3197
3198 static tree
namespace_ancestor(tree ns1,tree ns2)3199 namespace_ancestor (tree ns1, tree ns2)
3200 {
3201 timevar_push (TV_NAME_LOOKUP);
3202 if (is_ancestor (ns1, ns2))
3203 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
3204 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
3205 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
3206 }
3207
3208 /* Process a namespace-alias declaration. */
3209
3210 void
do_namespace_alias(tree alias,tree namespace)3211 do_namespace_alias (tree alias, tree namespace)
3212 {
3213 if (namespace == error_mark_node)
3214 return;
3215
3216 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3217
3218 namespace = ORIGINAL_NAMESPACE (namespace);
3219
3220 /* Build the alias. */
3221 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3222 DECL_NAMESPACE_ALIAS (alias) = namespace;
3223 DECL_EXTERNAL (alias) = 1;
3224 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3225 pushdecl (alias);
3226
3227 /* Emit debug info for namespace alias. */
3228 (*debug_hooks->global_decl) (alias);
3229 }
3230
3231 /* Like pushdecl, only it places X in the current namespace,
3232 if appropriate. */
3233
3234 tree
pushdecl_namespace_level(tree x,bool is_friend)3235 pushdecl_namespace_level (tree x, bool is_friend)
3236 {
3237 struct cp_binding_level *b = current_binding_level;
3238 tree t;
3239
3240 timevar_push (TV_NAME_LOOKUP);
3241 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3242
3243 /* Now, the type_shadowed stack may screw us. Munge it so it does
3244 what we want. */
3245 if (TREE_CODE (t) == TYPE_DECL)
3246 {
3247 tree name = DECL_NAME (t);
3248 tree newval;
3249 tree *ptr = (tree *)0;
3250 for (; !global_scope_p (b); b = b->level_chain)
3251 {
3252 tree shadowed = b->type_shadowed;
3253 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3254 if (TREE_PURPOSE (shadowed) == name)
3255 {
3256 ptr = &TREE_VALUE (shadowed);
3257 /* Can't break out of the loop here because sometimes
3258 a binding level will have duplicate bindings for
3259 PT names. It's gross, but I haven't time to fix it. */
3260 }
3261 }
3262 newval = TREE_TYPE (t);
3263 if (ptr == (tree *)0)
3264 {
3265 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3266 up here if this is changed to an assertion. --KR */
3267 SET_IDENTIFIER_TYPE_VALUE (name, t);
3268 }
3269 else
3270 {
3271 *ptr = newval;
3272 }
3273 }
3274 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3275 }
3276
3277 /* Insert USED into the using list of USER. Set INDIRECT_flag if this
3278 directive is not directly from the source. Also find the common
3279 ancestor and let our users know about the new namespace */
3280 static void
add_using_namespace(tree user,tree used,bool indirect)3281 add_using_namespace (tree user, tree used, bool indirect)
3282 {
3283 tree t;
3284 timevar_push (TV_NAME_LOOKUP);
3285 /* Using oneself is a no-op. */
3286 if (user == used)
3287 {
3288 timevar_pop (TV_NAME_LOOKUP);
3289 return;
3290 }
3291 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3292 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3293 /* Check if we already have this. */
3294 t = purpose_member (used, DECL_NAMESPACE_USING (user));
3295 if (t != NULL_TREE)
3296 {
3297 if (!indirect)
3298 /* Promote to direct usage. */
3299 TREE_INDIRECT_USING (t) = 0;
3300 timevar_pop (TV_NAME_LOOKUP);
3301 return;
3302 }
3303
3304 /* Add used to the user's using list. */
3305 DECL_NAMESPACE_USING (user)
3306 = tree_cons (used, namespace_ancestor (user, used),
3307 DECL_NAMESPACE_USING (user));
3308
3309 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3310
3311 /* Add user to the used's users list. */
3312 DECL_NAMESPACE_USERS (used)
3313 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3314
3315 /* Recursively add all namespaces used. */
3316 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3317 /* indirect usage */
3318 add_using_namespace (user, TREE_PURPOSE (t), 1);
3319
3320 /* Tell everyone using us about the new used namespaces. */
3321 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3322 add_using_namespace (TREE_PURPOSE (t), used, 1);
3323 timevar_pop (TV_NAME_LOOKUP);
3324 }
3325
3326 /* Process a using-declaration not appearing in class or local scope. */
3327
3328 void
do_toplevel_using_decl(tree decl,tree scope,tree name)3329 do_toplevel_using_decl (tree decl, tree scope, tree name)
3330 {
3331 tree oldval, oldtype, newval, newtype;
3332 tree orig_decl = decl;
3333 cxx_binding *binding;
3334
3335 decl = validate_nonmember_using_decl (decl, scope, name);
3336 if (decl == NULL_TREE)
3337 return;
3338
3339 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3340
3341 oldval = binding->value;
3342 oldtype = binding->type;
3343
3344 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3345
3346 /* Emit debug info. */
3347 if (!processing_template_decl)
3348 cp_emit_debug_info_for_using (orig_decl, current_namespace);
3349
3350 /* Copy declarations found. */
3351 if (newval)
3352 binding->value = newval;
3353 if (newtype)
3354 binding->type = newtype;
3355 }
3356
3357 /* Process a using-directive. */
3358
3359 void
do_using_directive(tree namespace)3360 do_using_directive (tree namespace)
3361 {
3362 tree context = NULL_TREE;
3363
3364 if (namespace == error_mark_node)
3365 return;
3366
3367 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
3368
3369 if (building_stmt_tree ())
3370 add_stmt (build_stmt (USING_STMT, namespace));
3371 namespace = ORIGINAL_NAMESPACE (namespace);
3372
3373 if (!toplevel_bindings_p ())
3374 {
3375 push_using_directive (namespace);
3376 context = current_scope ();
3377 }
3378 else
3379 {
3380 /* direct usage */
3381 add_using_namespace (current_namespace, namespace, 0);
3382 if (current_namespace != global_namespace)
3383 context = current_namespace;
3384 }
3385
3386 /* Emit debugging info. */
3387 if (!processing_template_decl)
3388 (*debug_hooks->imported_module_or_decl) (namespace, context);
3389 }
3390
3391 /* Deal with a using-directive seen by the parser. Currently we only
3392 handle attributes here, since they cannot appear inside a template. */
3393
3394 void
parse_using_directive(tree namespace,tree attribs)3395 parse_using_directive (tree namespace, tree attribs)
3396 {
3397 tree a;
3398
3399 do_using_directive (namespace);
3400
3401 for (a = attribs; a; a = TREE_CHAIN (a))
3402 {
3403 tree name = TREE_PURPOSE (a);
3404 if (is_attribute_p ("strong", name))
3405 {
3406 if (!toplevel_bindings_p ())
3407 error ("strong using only meaningful at namespace scope");
3408 else if (namespace != error_mark_node)
3409 {
3410 if (!is_ancestor (current_namespace, namespace))
3411 error ("current namespace %qD does not enclose strongly used namespace %qD",
3412 current_namespace, namespace);
3413 DECL_NAMESPACE_ASSOCIATIONS (namespace)
3414 = tree_cons (current_namespace, 0,
3415 DECL_NAMESPACE_ASSOCIATIONS (namespace));
3416 }
3417 }
3418 else
3419 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3420 }
3421 }
3422
3423 /* Like pushdecl, only it places X in the global scope if appropriate.
3424 Calls cp_finish_decl to register the variable, initializing it with
3425 *INIT, if INIT is non-NULL. */
3426
3427 static tree
pushdecl_top_level_1(tree x,tree * init,bool is_friend)3428 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3429 {
3430 timevar_push (TV_NAME_LOOKUP);
3431 push_to_top_level ();
3432 x = pushdecl_namespace_level (x, is_friend);
3433 if (init)
3434 finish_decl (x, *init, NULL_TREE);
3435 pop_from_top_level ();
3436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3437 }
3438
3439 /* Like pushdecl, only it places X in the global scope if appropriate. */
3440
3441 tree
pushdecl_top_level(tree x)3442 pushdecl_top_level (tree x)
3443 {
3444 return pushdecl_top_level_1 (x, NULL, false);
3445 }
3446
3447 /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */
3448
3449 tree
pushdecl_top_level_maybe_friend(tree x,bool is_friend)3450 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3451 {
3452 return pushdecl_top_level_1 (x, NULL, is_friend);
3453 }
3454
3455 /* Like pushdecl, only it places X in the global scope if
3456 appropriate. Calls cp_finish_decl to register the variable,
3457 initializing it with INIT. */
3458
3459 tree
pushdecl_top_level_and_finish(tree x,tree init)3460 pushdecl_top_level_and_finish (tree x, tree init)
3461 {
3462 return pushdecl_top_level_1 (x, &init, false);
3463 }
3464
3465 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3466 duplicates. The first list becomes the tail of the result.
3467
3468 The algorithm is O(n^2). We could get this down to O(n log n) by
3469 doing a sort on the addresses of the functions, if that becomes
3470 necessary. */
3471
3472 static tree
merge_functions(tree s1,tree s2)3473 merge_functions (tree s1, tree s2)
3474 {
3475 for (; s2; s2 = OVL_NEXT (s2))
3476 {
3477 tree fn2 = OVL_CURRENT (s2);
3478 tree fns1;
3479
3480 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3481 {
3482 tree fn1 = OVL_CURRENT (fns1);
3483
3484 /* If the function from S2 is already in S1, there is no
3485 need to add it again. For `extern "C"' functions, we
3486 might have two FUNCTION_DECLs for the same function, in
3487 different namespaces; again, we only need one of them. */
3488 if (fn1 == fn2
3489 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
3490 && DECL_NAME (fn1) == DECL_NAME (fn2)))
3491 break;
3492 }
3493
3494 /* If we exhausted all of the functions in S1, FN2 is new. */
3495 if (!fns1)
3496 s1 = build_overload (fn2, s1);
3497 }
3498 return s1;
3499 }
3500
3501 /* This should return an error not all definitions define functions.
3502 It is not an error if we find two functions with exactly the
3503 same signature, only if these are selected in overload resolution.
3504 old is the current set of bindings, new the freshly-found binding.
3505 XXX Do we want to give *all* candidates in case of ambiguity?
3506 XXX In what way should I treat extern declarations?
3507 XXX I don't want to repeat the entire duplicate_decls here */
3508
3509 /* LLVM LOCAL begin mainline */
3510 static void
ambiguous_decl(struct scope_binding * old,cxx_binding * new,int flags)3511 ambiguous_decl (struct scope_binding *old, cxx_binding *new, int flags)
3512 {
3513 tree val, type;
3514 gcc_assert (old != NULL);
3515
3516 /* Copy the type. */
3517 type = new->type;
3518 if (LOOKUP_NAMESPACES_ONLY (flags)
3519 || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
3520 type = NULL_TREE;
3521
3522 /* Copy the value. */
3523 val = new->value;
3524 if (val)
3525 {
3526 if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
3527 val = NULL_TREE;
3528 else
3529 switch (TREE_CODE (val))
3530 {
3531 case TEMPLATE_DECL:
3532 /* If we expect types or namespaces, and not templates,
3533 or this is not a template class. */
3534 if ((LOOKUP_QUALIFIERS_ONLY (flags)
3535 && !DECL_CLASS_TEMPLATE_P (val)))
3536 val = NULL_TREE;
3537 break;
3538 case TYPE_DECL:
3539 if (LOOKUP_NAMESPACES_ONLY (flags)
3540 || (type && (flags & LOOKUP_PREFER_TYPES)))
3541 val = NULL_TREE;
3542 break;
3543 case NAMESPACE_DECL:
3544 if (LOOKUP_TYPES_ONLY (flags))
3545 val = NULL_TREE;
3546 break;
3547 case FUNCTION_DECL:
3548 /* Ignore built-in functions that are still anticipated. */
3549 if (LOOKUP_QUALIFIERS_ONLY (flags))
3550 val = NULL_TREE;
3551 break;
3552 default:
3553 if (LOOKUP_QUALIFIERS_ONLY (flags))
3554 val = NULL_TREE;
3555 }
3556 }
3557
3558 /* If val is hidden, shift down any class or enumeration name. */
3559 if (!val)
3560 {
3561 val = type;
3562 type = NULL_TREE;
3563 }
3564
3565 /* LLVM LOCAL end mainline */
3566 if (!old->value)
3567 old->value = val;
3568 else if (val && val != old->value)
3569 {
3570 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
3571 old->value = merge_functions (old->value, val);
3572 else
3573 {
3574 old->value = tree_cons (NULL_TREE, old->value,
3575 build_tree_list (NULL_TREE, val));
3576 TREE_TYPE (old->value) = error_mark_node;
3577 }
3578 }
3579
3580 /* LLVM LOCAL begin mainline */
3581 if (!old->type)
3582 old->type = type;
3583 else if (type && old->type != type)
3584 {
3585 old->type = tree_cons (NULL_TREE, old->type,
3586 build_tree_list (NULL_TREE, type));
3587 TREE_TYPE (old->type) = error_mark_node;
3588 }
3589 /* LLVM LOCAL end mainline */
3590 }
3591
3592 /* Return the declarations that are members of the namespace NS. */
3593
3594 tree
cp_namespace_decls(tree ns)3595 cp_namespace_decls (tree ns)
3596 {
3597 return NAMESPACE_LEVEL (ns)->names;
3598 }
3599
3600 /* Combine prefer_type and namespaces_only into flags. */
3601
3602 static int
lookup_flags(int prefer_type,int namespaces_only)3603 lookup_flags (int prefer_type, int namespaces_only)
3604 {
3605 if (namespaces_only)
3606 return LOOKUP_PREFER_NAMESPACES;
3607 if (prefer_type > 1)
3608 return LOOKUP_PREFER_TYPES;
3609 if (prefer_type > 0)
3610 return LOOKUP_PREFER_BOTH;
3611 return 0;
3612 }
3613
3614 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
3615 ignore it or not. Subroutine of lookup_name_real and
3616 lookup_type_scope. */
3617
3618 static bool
qualify_lookup(tree val,int flags)3619 qualify_lookup (tree val, int flags)
3620 {
3621 if (val == NULL_TREE)
3622 return false;
3623 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
3624 return true;
3625 if ((flags & LOOKUP_PREFER_TYPES)
3626 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
3627 return true;
3628 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
3629 return false;
3630 return true;
3631 }
3632
3633 /* Given a lookup that returned VAL, decide if we want to ignore it or
3634 not based on DECL_ANTICIPATED. */
3635
3636 bool
hidden_name_p(tree val)3637 hidden_name_p (tree val)
3638 {
3639 if (DECL_P (val)
3640 && DECL_LANG_SPECIFIC (val)
3641 && DECL_ANTICIPATED (val))
3642 return true;
3643 return false;
3644 }
3645
3646 /* Remove any hidden friend functions from a possibly overloaded set
3647 of functions. */
3648
3649 tree
remove_hidden_names(tree fns)3650 remove_hidden_names (tree fns)
3651 {
3652 if (!fns)
3653 return fns;
3654
3655 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
3656 fns = NULL_TREE;
3657 else if (TREE_CODE (fns) == OVERLOAD)
3658 {
3659 tree o;
3660
3661 for (o = fns; o; o = OVL_NEXT (o))
3662 if (hidden_name_p (OVL_CURRENT (o)))
3663 break;
3664 if (o)
3665 {
3666 tree n = NULL_TREE;
3667
3668 for (o = fns; o; o = OVL_NEXT (o))
3669 if (!hidden_name_p (OVL_CURRENT (o)))
3670 n = build_overload (OVL_CURRENT (o), n);
3671 fns = n;
3672 }
3673 }
3674
3675 return fns;
3676 }
3677
3678 /* Unscoped lookup of a global: iterate over current namespaces,
3679 considering using-directives. */
3680
3681 static tree
unqualified_namespace_lookup(tree name,int flags)3682 unqualified_namespace_lookup (tree name, int flags)
3683 {
3684 tree initial = current_decl_namespace ();
3685 tree scope = initial;
3686 tree siter;
3687 struct cp_binding_level *level;
3688 tree val = NULL_TREE;
3689
3690 timevar_push (TV_NAME_LOOKUP);
3691
3692 for (; !val; scope = CP_DECL_CONTEXT (scope))
3693 {
3694 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3695 cxx_binding *b =
3696 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3697
3698 if (b)
3699 /* LLVM LOCAL mainline */
3700 ambiguous_decl (&binding, b, flags);
3701
3702 /* Add all _DECLs seen through local using-directives. */
3703 for (level = current_binding_level;
3704 level->kind != sk_namespace;
3705 level = level->level_chain)
3706 if (!lookup_using_namespace (name, &binding, level->using_directives,
3707 scope, flags))
3708 /* Give up because of error. */
3709 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3710
3711 /* Add all _DECLs seen through global using-directives. */
3712 /* XXX local and global using lists should work equally. */
3713 siter = initial;
3714 while (1)
3715 {
3716 if (!lookup_using_namespace (name, &binding,
3717 DECL_NAMESPACE_USING (siter),
3718 scope, flags))
3719 /* Give up because of error. */
3720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
3721 if (siter == scope) break;
3722 siter = CP_DECL_CONTEXT (siter);
3723 }
3724
3725 val = binding.value;
3726 if (scope == global_namespace)
3727 break;
3728 }
3729 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
3730 }
3731
3732 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
3733 or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type
3734 bindings.
3735
3736 Returns a DECL (or OVERLOAD, or BASELINK) representing the
3737 declaration found. If no suitable declaration can be found,
3738 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
3739 neither a class-type nor a namespace a diagnostic is issued. */
3740
3741 tree
lookup_qualified_name(tree scope,tree name,bool is_type_p,bool complain)3742 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
3743 {
3744 int flags = 0;
3745 tree t = NULL_TREE;
3746
3747 if (TREE_CODE (scope) == NAMESPACE_DECL)
3748 {
3749 struct scope_binding binding = EMPTY_SCOPE_BINDING;
3750
3751 flags |= LOOKUP_COMPLAIN;
3752 if (is_type_p)
3753 flags |= LOOKUP_PREFER_TYPES;
3754 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
3755 t = binding.value;
3756 }
3757 else if (is_aggr_type (scope, complain))
3758 t = lookup_member (scope, name, 2, is_type_p);
3759
3760 if (!t)
3761 return error_mark_node;
3762 return t;
3763 }
3764
3765 /* Subroutine of unqualified_namespace_lookup:
3766 Add the bindings of NAME in used namespaces to VAL.
3767 We are currently looking for names in namespace SCOPE, so we
3768 look through USINGS for using-directives of namespaces
3769 which have SCOPE as a common ancestor with the current scope.
3770 Returns false on errors. */
3771
3772 static bool
lookup_using_namespace(tree name,struct scope_binding * val,tree usings,tree scope,int flags)3773 lookup_using_namespace (tree name, struct scope_binding *val,
3774 tree usings, tree scope, int flags)
3775 {
3776 tree iter;
3777 timevar_push (TV_NAME_LOOKUP);
3778 /* Iterate over all used namespaces in current, searching for using
3779 directives of scope. */
3780 for (iter = usings; iter; iter = TREE_CHAIN (iter))
3781 if (TREE_VALUE (iter) == scope)
3782 {
3783 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
3784 cxx_binding *val1 =
3785 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
3786 /* Resolve ambiguities. */
3787 if (val1)
3788 /* LLVM LOCAL mainline */
3789 ambiguous_decl (val, val1, flags);
3790 }
3791 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
3792 }
3793
3794 /* [namespace.qual]
3795 Accepts the NAME to lookup and its qualifying SCOPE.
3796 Returns the name/type pair found into the cxx_binding *RESULT,
3797 or false on error. */
3798
3799 static bool
qualified_lookup_using_namespace(tree name,tree scope,struct scope_binding * result,int flags)3800 qualified_lookup_using_namespace (tree name, tree scope,
3801 struct scope_binding *result, int flags)
3802 {
3803 /* Maintain a list of namespaces visited... */
3804 tree seen = NULL_TREE;
3805 /* ... and a list of namespace yet to see. */
3806 tree todo = NULL_TREE;
3807 tree todo_maybe = NULL_TREE;
3808 tree usings;
3809 timevar_push (TV_NAME_LOOKUP);
3810 /* Look through namespace aliases. */
3811 scope = ORIGINAL_NAMESPACE (scope);
3812 while (scope && result->value != error_mark_node)
3813 {
3814 cxx_binding *binding =
3815 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3816 seen = tree_cons (scope, NULL_TREE, seen);
3817 if (binding)
3818 /* LLVM LOCAL mainline */
3819 ambiguous_decl (result, binding, flags);
3820
3821 /* Consider strong using directives always, and non-strong ones
3822 if we haven't found a binding yet. ??? Shouldn't we consider
3823 non-strong ones if the initial RESULT is non-NULL, but the
3824 binding in the given namespace is? */
3825 for (usings = DECL_NAMESPACE_USING (scope); usings;
3826 usings = TREE_CHAIN (usings))
3827 /* If this was a real directive, and we have not seen it. */
3828 if (!TREE_INDIRECT_USING (usings))
3829 {
3830 /* Try to avoid queuing the same namespace more than once,
3831 the exception being when a namespace was already
3832 enqueued for todo_maybe and then a strong using is
3833 found for it. We could try to remove it from
3834 todo_maybe, but it's probably not worth the effort. */
3835 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
3836 && !purpose_member (TREE_PURPOSE (usings), seen)
3837 && !purpose_member (TREE_PURPOSE (usings), todo))
3838 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
3839 else if ((!result->value && !result->type)
3840 && !purpose_member (TREE_PURPOSE (usings), seen)
3841 && !purpose_member (TREE_PURPOSE (usings), todo)
3842 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
3843 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
3844 todo_maybe);
3845 }
3846 if (todo)
3847 {
3848 scope = TREE_PURPOSE (todo);
3849 todo = TREE_CHAIN (todo);
3850 }
3851 else if (todo_maybe
3852 && (!result->value && !result->type))
3853 {
3854 scope = TREE_PURPOSE (todo_maybe);
3855 todo = TREE_CHAIN (todo_maybe);
3856 todo_maybe = NULL_TREE;
3857 }
3858 else
3859 scope = NULL_TREE; /* If there never was a todo list. */
3860 }
3861 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
3862 }
3863
3864 /* Return the innermost non-namespace binding for NAME from a scope
3865 containing BINDING, or, if BINDING is NULL, the current scope. If
3866 CLASS_P is false, then class bindings are ignored. */
3867
3868 cxx_binding *
outer_binding(tree name,cxx_binding * binding,bool class_p)3869 outer_binding (tree name,
3870 cxx_binding *binding,
3871 bool class_p)
3872 {
3873 cxx_binding *outer;
3874 cxx_scope *scope;
3875 cxx_scope *outer_scope;
3876
3877 if (binding)
3878 {
3879 scope = binding->scope->level_chain;
3880 outer = binding->previous;
3881 }
3882 else
3883 {
3884 scope = current_binding_level;
3885 outer = IDENTIFIER_BINDING (name);
3886 }
3887 outer_scope = outer ? outer->scope : NULL;
3888
3889 /* Because we create class bindings lazily, we might be missing a
3890 class binding for NAME. If there are any class binding levels
3891 between the LAST_BINDING_LEVEL and the scope in which OUTER was
3892 declared, we must lookup NAME in those class scopes. */
3893 if (class_p)
3894 while (scope && scope != outer_scope && scope->kind != sk_namespace)
3895 {
3896 if (scope->kind == sk_class)
3897 {
3898 cxx_binding *class_binding;
3899
3900 class_binding = get_class_binding (name, scope);
3901 if (class_binding)
3902 {
3903 /* Thread this new class-scope binding onto the
3904 IDENTIFIER_BINDING list so that future lookups
3905 find it quickly. */
3906 class_binding->previous = outer;
3907 if (binding)
3908 binding->previous = class_binding;
3909 else
3910 IDENTIFIER_BINDING (name) = class_binding;
3911 return class_binding;
3912 }
3913 }
3914 scope = scope->level_chain;
3915 }
3916
3917 return outer;
3918 }
3919
3920 /* Return the innermost block-scope or class-scope value binding for
3921 NAME, or NULL_TREE if there is no such binding. */
3922
3923 tree
innermost_non_namespace_value(tree name)3924 innermost_non_namespace_value (tree name)
3925 {
3926 cxx_binding *binding;
3927 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
3928 return binding ? binding->value : NULL_TREE;
3929 }
3930
3931 /* Look up NAME in the current binding level and its superiors in the
3932 namespace of variables, functions and typedefs. Return a ..._DECL
3933 node of some kind representing its definition if there is only one
3934 such declaration, or return a TREE_LIST with all the overloaded
3935 definitions if there are many, or return 0 if it is undefined.
3936 Hidden name, either friend declaration or built-in function, are
3937 not ignored.
3938
3939 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
3940 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
3941 Otherwise we prefer non-TYPE_DECLs.
3942
3943 If NONCLASS is nonzero, bindings in class scopes are ignored. If
3944 BLOCK_P is false, bindings in block scopes are ignored. */
3945
3946 tree
lookup_name_real(tree name,int prefer_type,int nonclass,bool block_p,int namespaces_only,int flags)3947 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
3948 int namespaces_only, int flags)
3949 {
3950 cxx_binding *iter;
3951 tree val = NULL_TREE;
3952
3953 timevar_push (TV_NAME_LOOKUP);
3954 /* Conversion operators are handled specially because ordinary
3955 unqualified name lookup will not find template conversion
3956 operators. */
3957 if (IDENTIFIER_TYPENAME_P (name))
3958 {
3959 struct cp_binding_level *level;
3960
3961 for (level = current_binding_level;
3962 level && level->kind != sk_namespace;
3963 level = level->level_chain)
3964 {
3965 tree class_type;
3966 tree operators;
3967
3968 /* A conversion operator can only be declared in a class
3969 scope. */
3970 if (level->kind != sk_class)
3971 continue;
3972
3973 /* Lookup the conversion operator in the class. */
3974 class_type = level->this_entity;
3975 operators = lookup_fnfields (class_type, name, /*protect=*/0);
3976 if (operators)
3977 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
3978 }
3979
3980 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
3981 }
3982
3983 flags |= lookup_flags (prefer_type, namespaces_only);
3984
3985 /* First, look in non-namespace scopes. */
3986
3987 if (current_class_type == NULL_TREE)
3988 nonclass = 1;
3989
3990 if (block_p || !nonclass)
3991 for (iter = outer_binding (name, NULL, !nonclass);
3992 iter;
3993 iter = outer_binding (name, iter, !nonclass))
3994 {
3995 tree binding;
3996
3997 /* Skip entities we don't want. */
3998 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
3999 continue;
4000
4001 /* If this is the kind of thing we're looking for, we're done. */
4002 if (qualify_lookup (iter->value, flags))
4003 binding = iter->value;
4004 else if ((flags & LOOKUP_PREFER_TYPES)
4005 && qualify_lookup (iter->type, flags))
4006 binding = iter->type;
4007 else
4008 binding = NULL_TREE;
4009
4010 if (binding)
4011 {
4012 if (hidden_name_p (binding))
4013 {
4014 /* A non namespace-scope binding can only be hidden if
4015 we are in a local class, due to friend declarations.
4016 In particular, consider:
4017
4018 void f() {
4019 struct A {
4020 friend struct B;
4021 void g() { B* b; } // error: B is hidden
4022 }
4023 struct B {};
4024 }
4025
4026 The standard says that "B" is a local class in "f"
4027 (but not nested within "A") -- but that name lookup
4028 for "B" does not find this declaration until it is
4029 declared directly with "f".
4030
4031 In particular:
4032
4033 [class.friend]
4034
4035 If a friend declaration appears in a local class and
4036 the name specified is an unqualified name, a prior
4037 declaration is looked up without considering scopes
4038 that are outside the innermost enclosing non-class
4039 scope. For a friend class declaration, if there is no
4040 prior declaration, the class that is specified
4041 belongs to the innermost enclosing non-class scope,
4042 but if it is subsequently referenced, its name is not
4043 found by name lookup until a matching declaration is
4044 provided in the innermost enclosing nonclass scope.
4045 */
4046 gcc_assert (current_class_type &&
4047 LOCAL_CLASS_P (current_class_type));
4048
4049 /* This binding comes from a friend declaration in the local
4050 class. The standard (11.4.8) states that the lookup can
4051 only succeed if there is a non-hidden declaration in the
4052 current scope, which is not the case here. */
4053 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4054 }
4055 val = binding;
4056 break;
4057 }
4058 }
4059
4060 /* Now lookup in namespace scopes. */
4061 if (!val)
4062 val = unqualified_namespace_lookup (name, flags);
4063
4064 /* If we have a single function from a using decl, pull it out. */
4065 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4066 val = OVL_FUNCTION (val);
4067
4068 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4069 }
4070
4071 tree
lookup_name_nonclass(tree name)4072 lookup_name_nonclass (tree name)
4073 {
4074 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4075 }
4076
4077 tree
lookup_function_nonclass(tree name,tree args,bool block_p)4078 lookup_function_nonclass (tree name, tree args, bool block_p)
4079 {
4080 return
4081 lookup_arg_dependent (name,
4082 lookup_name_real (name, 0, 1, block_p, 0,
4083 LOOKUP_COMPLAIN),
4084 args);
4085 }
4086
4087 tree
lookup_name(tree name)4088 lookup_name (tree name)
4089 {
4090 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4091 }
4092
4093 tree
lookup_name_prefer_type(tree name,int prefer_type)4094 lookup_name_prefer_type (tree name, int prefer_type)
4095 {
4096 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4097 0, LOOKUP_COMPLAIN);
4098 }
4099
4100 /* Look up NAME for type used in elaborated name specifier in
4101 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
4102 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
4103 name, more scopes are checked if cleanup or template parameter
4104 scope is encountered.
4105
4106 Unlike lookup_name_real, we make sure that NAME is actually
4107 declared in the desired scope, not from inheritance, nor using
4108 directive. For using declaration, there is DR138 still waiting
4109 to be resolved. Hidden name coming from an earlier friend
4110 declaration is also returned.
4111
4112 A TYPE_DECL best matching the NAME is returned. Catching error
4113 and issuing diagnostics are caller's responsibility. */
4114
4115 tree
lookup_type_scope(tree name,tag_scope scope)4116 lookup_type_scope (tree name, tag_scope scope)
4117 {
4118 cxx_binding *iter = NULL;
4119 tree val = NULL_TREE;
4120
4121 timevar_push (TV_NAME_LOOKUP);
4122
4123 /* Look in non-namespace scope first. */
4124 if (current_binding_level->kind != sk_namespace)
4125 iter = outer_binding (name, NULL, /*class_p=*/ true);
4126 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4127 {
4128 /* Check if this is the kind of thing we're looking for.
4129 If SCOPE is TS_CURRENT, also make sure it doesn't come from
4130 base class. For ITER->VALUE, we can simply use
4131 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
4132 our own check.
4133
4134 We check ITER->TYPE before ITER->VALUE in order to handle
4135 typedef struct C {} C;
4136 correctly. */
4137
4138 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4139 && (scope != ts_current
4140 || LOCAL_BINDING_P (iter)
4141 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4142 val = iter->type;
4143 else if ((scope != ts_current
4144 || !INHERITED_VALUE_BINDING_P (iter))
4145 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4146 val = iter->value;
4147
4148 if (val)
4149 break;
4150 }
4151
4152 /* Look in namespace scope. */
4153 if (!val)
4154 {
4155 iter = cxx_scope_find_binding_for_name
4156 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4157
4158 if (iter)
4159 {
4160 /* If this is the kind of thing we're looking for, we're done. */
4161 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4162 val = iter->type;
4163 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4164 val = iter->value;
4165 }
4166
4167 }
4168
4169 /* Type found, check if it is in the allowed scopes, ignoring cleanup
4170 and template parameter scopes. */
4171 if (val)
4172 {
4173 struct cp_binding_level *b = current_binding_level;
4174 while (b)
4175 {
4176 if (iter->scope == b)
4177 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
4178
4179 if (b->kind == sk_cleanup || b->kind == sk_template_parms)
4180 b = b->level_chain;
4181 else if (b->kind == sk_class
4182 && scope == ts_within_enclosing_non_class)
4183 b = b->level_chain;
4184 else
4185 break;
4186 }
4187 }
4188
4189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4190 }
4191
4192 /* Similar to `lookup_name' but look only in the innermost non-class
4193 binding level. */
4194
4195 static tree
lookup_name_innermost_nonclass_level(tree name)4196 lookup_name_innermost_nonclass_level (tree name)
4197 {
4198 struct cp_binding_level *b;
4199 tree t = NULL_TREE;
4200
4201 timevar_push (TV_NAME_LOOKUP);
4202 b = innermost_nonclass_level ();
4203
4204 if (b->kind == sk_namespace)
4205 {
4206 t = IDENTIFIER_NAMESPACE_VALUE (name);
4207
4208 /* extern "C" function() */
4209 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4210 t = TREE_VALUE (t);
4211 }
4212 else if (IDENTIFIER_BINDING (name)
4213 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4214 {
4215 cxx_binding *binding;
4216 binding = IDENTIFIER_BINDING (name);
4217 while (1)
4218 {
4219 if (binding->scope == b
4220 && !(TREE_CODE (binding->value) == VAR_DECL
4221 && DECL_DEAD_FOR_LOCAL (binding->value)))
4222 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
4223
4224 if (b->kind == sk_cleanup)
4225 b = b->level_chain;
4226 else
4227 break;
4228 }
4229 }
4230
4231 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4232 }
4233
4234 /* Like lookup_name_innermost_nonclass_level, but for types. */
4235
4236 static tree
lookup_type_current_level(tree name)4237 lookup_type_current_level (tree name)
4238 {
4239 tree t = NULL_TREE;
4240
4241 timevar_push (TV_NAME_LOOKUP);
4242 gcc_assert (current_binding_level->kind != sk_namespace);
4243
4244 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4245 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4246 {
4247 struct cp_binding_level *b = current_binding_level;
4248 while (1)
4249 {
4250 if (purpose_member (name, b->type_shadowed))
4251 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4252 REAL_IDENTIFIER_TYPE_VALUE (name));
4253 if (b->kind == sk_cleanup)
4254 b = b->level_chain;
4255 else
4256 break;
4257 }
4258 }
4259
4260 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4261 }
4262
4263 /* [basic.lookup.koenig] */
4264 /* A nonzero return value in the functions below indicates an error. */
4265
4266 struct arg_lookup
4267 {
4268 tree name;
4269 tree args;
4270 tree namespaces;
4271 tree classes;
4272 tree functions;
4273 };
4274
4275 static bool arg_assoc (struct arg_lookup*, tree);
4276 static bool arg_assoc_args (struct arg_lookup*, tree);
4277 static bool arg_assoc_type (struct arg_lookup*, tree);
4278 static bool add_function (struct arg_lookup *, tree);
4279 static bool arg_assoc_namespace (struct arg_lookup *, tree);
4280 static bool arg_assoc_class (struct arg_lookup *, tree);
4281 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4282
4283 /* Add a function to the lookup structure.
4284 Returns true on error. */
4285
4286 static bool
add_function(struct arg_lookup * k,tree fn)4287 add_function (struct arg_lookup *k, tree fn)
4288 {
4289 /* We used to check here to see if the function was already in the list,
4290 but that's O(n^2), which is just too expensive for function lookup.
4291 Now we deal with the occasional duplicate in joust. In doing this, we
4292 assume that the number of duplicates will be small compared to the
4293 total number of functions being compared, which should usually be the
4294 case. */
4295
4296 /* We must find only functions, or exactly one non-function. */
4297 if (!k->functions)
4298 k->functions = fn;
4299 else if (fn == k->functions)
4300 ;
4301 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4302 k->functions = build_overload (fn, k->functions);
4303 else
4304 {
4305 tree f1 = OVL_CURRENT (k->functions);
4306 tree f2 = fn;
4307 if (is_overloaded_fn (f1))
4308 {
4309 fn = f1; f1 = f2; f2 = fn;
4310 }
4311 error ("%q+D is not a function,", f1);
4312 error (" conflict with %q+D", f2);
4313 error (" in call to %qD", k->name);
4314 return true;
4315 }
4316
4317 return false;
4318 }
4319
4320 /* Returns true iff CURRENT has declared itself to be an associated
4321 namespace of SCOPE via a strong using-directive (or transitive chain
4322 thereof). Both are namespaces. */
4323
4324 bool
is_associated_namespace(tree current,tree scope)4325 is_associated_namespace (tree current, tree scope)
4326 {
4327 tree seen = NULL_TREE;
4328 tree todo = NULL_TREE;
4329 tree t;
4330 while (1)
4331 {
4332 if (scope == current)
4333 return true;
4334 seen = tree_cons (scope, NULL_TREE, seen);
4335 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
4336 if (!purpose_member (TREE_PURPOSE (t), seen))
4337 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
4338 if (todo)
4339 {
4340 scope = TREE_PURPOSE (todo);
4341 todo = TREE_CHAIN (todo);
4342 }
4343 else
4344 return false;
4345 }
4346 }
4347
4348 /* Return whether FN is a friend of an associated class of ARG. */
4349
4350 static bool
friend_of_associated_class_p(tree arg,tree fn)4351 friend_of_associated_class_p (tree arg, tree fn)
4352 {
4353 tree type;
4354
4355 if (TYPE_P (arg))
4356 type = arg;
4357 else if (type_unknown_p (arg))
4358 return false;
4359 else
4360 type = TREE_TYPE (arg);
4361
4362 /* If TYPE is a class, the class itself and all base classes are
4363 associated classes. */
4364 if (CLASS_TYPE_P (type))
4365 {
4366 if (is_friend (type, fn))
4367 return true;
4368
4369 if (TYPE_BINFO (type))
4370 {
4371 tree binfo, base_binfo;
4372 int i;
4373
4374 for (binfo = TYPE_BINFO (type), i = 0;
4375 BINFO_BASE_ITERATE (binfo, i, base_binfo);
4376 i++)
4377 if (is_friend (BINFO_TYPE (base_binfo), fn))
4378 return true;
4379 }
4380 }
4381
4382 /* If TYPE is a class member, the class of which it is a member is
4383 an associated class. */
4384 if ((CLASS_TYPE_P (type)
4385 || TREE_CODE (type) == UNION_TYPE
4386 || TREE_CODE (type) == ENUMERAL_TYPE)
4387 && TYPE_CONTEXT (type)
4388 && CLASS_TYPE_P (TYPE_CONTEXT (type))
4389 && is_friend (TYPE_CONTEXT (type), fn))
4390 return true;
4391
4392 return false;
4393 }
4394
4395 /* Add functions of a namespace to the lookup structure.
4396 Returns true on error. */
4397
4398 static bool
arg_assoc_namespace(struct arg_lookup * k,tree scope)4399 arg_assoc_namespace (struct arg_lookup *k, tree scope)
4400 {
4401 tree value;
4402
4403 if (purpose_member (scope, k->namespaces))
4404 return 0;
4405 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4406
4407 /* Check out our super-users. */
4408 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
4409 value = TREE_CHAIN (value))
4410 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
4411 return true;
4412
4413 value = namespace_binding (k->name, scope);
4414 if (!value)
4415 return false;
4416
4417 for (; value; value = OVL_NEXT (value))
4418 {
4419 /* We don't want to find arbitrary hidden functions via argument
4420 dependent lookup. We only want to find friends of associated
4421 classes. */
4422 if (hidden_name_p (OVL_CURRENT (value)))
4423 {
4424 tree args;
4425
4426 for (args = k->args; args; args = TREE_CHAIN (args))
4427 if (friend_of_associated_class_p (TREE_VALUE (args),
4428 OVL_CURRENT (value)))
4429 break;
4430 if (!args)
4431 continue;
4432 }
4433
4434 if (add_function (k, OVL_CURRENT (value)))
4435 return true;
4436 }
4437
4438 return false;
4439 }
4440
4441 /* Adds everything associated with a template argument to the lookup
4442 structure. Returns true on error. */
4443
4444 static bool
arg_assoc_template_arg(struct arg_lookup * k,tree arg)4445 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
4446 {
4447 /* [basic.lookup.koenig]
4448
4449 If T is a template-id, its associated namespaces and classes are
4450 ... the namespaces and classes associated with the types of the
4451 template arguments provided for template type parameters
4452 (excluding template template parameters); the namespaces in which
4453 any template template arguments are defined; and the classes in
4454 which any member templates used as template template arguments
4455 are defined. [Note: non-type template arguments do not
4456 contribute to the set of associated namespaces. ] */
4457
4458 /* Consider first template template arguments. */
4459 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4460 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4461 return false;
4462 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4463 {
4464 tree ctx = CP_DECL_CONTEXT (arg);
4465
4466 /* It's not a member template. */
4467 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4468 return arg_assoc_namespace (k, ctx);
4469 /* Otherwise, it must be member template. */
4470 else
4471 return arg_assoc_class (k, ctx);
4472 }
4473 /* It's not a template template argument, but it is a type template
4474 argument. */
4475 else if (TYPE_P (arg))
4476 return arg_assoc_type (k, arg);
4477 /* It's a non-type template argument. */
4478 else
4479 return false;
4480 }
4481
4482 /* Adds everything associated with class to the lookup structure.
4483 Returns true on error. */
4484
4485 static bool
arg_assoc_class(struct arg_lookup * k,tree type)4486 arg_assoc_class (struct arg_lookup *k, tree type)
4487 {
4488 tree list, friends, context;
4489 int i;
4490
4491 /* Backend build structures, such as __builtin_va_list, aren't
4492 affected by all this. */
4493 if (!CLASS_TYPE_P (type))
4494 return false;
4495
4496 if (purpose_member (type, k->classes))
4497 return false;
4498 k->classes = tree_cons (type, NULL_TREE, k->classes);
4499
4500 context = decl_namespace_context (type);
4501 if (arg_assoc_namespace (k, context))
4502 return true;
4503
4504 if (TYPE_BINFO (type))
4505 {
4506 /* Process baseclasses. */
4507 tree binfo, base_binfo;
4508
4509 for (binfo = TYPE_BINFO (type), i = 0;
4510 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
4511 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
4512 return true;
4513 }
4514
4515 /* Process friends. */
4516 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4517 list = TREE_CHAIN (list))
4518 if (k->name == FRIEND_NAME (list))
4519 for (friends = FRIEND_DECLS (list); friends;
4520 friends = TREE_CHAIN (friends))
4521 {
4522 tree fn = TREE_VALUE (friends);
4523
4524 /* Only interested in global functions with potentially hidden
4525 (i.e. unqualified) declarations. */
4526 if (CP_DECL_CONTEXT (fn) != context)
4527 continue;
4528 /* Template specializations are never found by name lookup.
4529 (Templates themselves can be found, but not template
4530 specializations.) */
4531 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
4532 continue;
4533 if (add_function (k, fn))
4534 return true;
4535 }
4536
4537 /* Process template arguments. */
4538 if (CLASSTYPE_TEMPLATE_INFO (type)
4539 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
4540 {
4541 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4542 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4543 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4544 }
4545
4546 return false;
4547 }
4548
4549 /* Adds everything associated with a given type.
4550 Returns 1 on error. */
4551
4552 static bool
arg_assoc_type(struct arg_lookup * k,tree type)4553 arg_assoc_type (struct arg_lookup *k, tree type)
4554 {
4555 /* As we do not get the type of non-type dependent expressions
4556 right, we can end up with such things without a type. */
4557 if (!type)
4558 return false;
4559
4560 if (TYPE_PTRMEM_P (type))
4561 {
4562 /* Pointer to member: associate class type and value type. */
4563 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
4564 return true;
4565 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
4566 }
4567 else switch (TREE_CODE (type))
4568 {
4569 case ERROR_MARK:
4570 return false;
4571 case VOID_TYPE:
4572 case INTEGER_TYPE:
4573 case REAL_TYPE:
4574 case COMPLEX_TYPE:
4575 case VECTOR_TYPE:
4576 case BOOLEAN_TYPE:
4577 return false;
4578 case RECORD_TYPE:
4579 if (TYPE_PTRMEMFUNC_P (type))
4580 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4581 return arg_assoc_class (k, type);
4582 case POINTER_TYPE:
4583 /* APPLE LOCAL blocks 6040305 */
4584 case BLOCK_POINTER_TYPE:
4585 case REFERENCE_TYPE:
4586 case ARRAY_TYPE:
4587 return arg_assoc_type (k, TREE_TYPE (type));
4588 case UNION_TYPE:
4589 case ENUMERAL_TYPE:
4590 return arg_assoc_namespace (k, decl_namespace_context (type));
4591 case METHOD_TYPE:
4592 /* The basetype is referenced in the first arg type, so just
4593 fall through. */
4594 case FUNCTION_TYPE:
4595 /* Associate the parameter types. */
4596 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4597 return true;
4598 /* Associate the return type. */
4599 return arg_assoc_type (k, TREE_TYPE (type));
4600 case TEMPLATE_TYPE_PARM:
4601 case BOUND_TEMPLATE_TEMPLATE_PARM:
4602 return false;
4603 case TYPENAME_TYPE:
4604 return false;
4605 case LANG_TYPE:
4606 gcc_assert (type == unknown_type_node);
4607 return false;
4608 default:
4609 gcc_unreachable ();
4610 }
4611 return false;
4612 }
4613
4614 /* Adds everything associated with arguments. Returns true on error. */
4615
4616 static bool
arg_assoc_args(struct arg_lookup * k,tree args)4617 arg_assoc_args (struct arg_lookup *k, tree args)
4618 {
4619 for (; args; args = TREE_CHAIN (args))
4620 if (arg_assoc (k, TREE_VALUE (args)))
4621 return true;
4622 return false;
4623 }
4624
4625 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4626
4627 static bool
arg_assoc(struct arg_lookup * k,tree n)4628 arg_assoc (struct arg_lookup *k, tree n)
4629 {
4630 if (n == error_mark_node)
4631 return false;
4632
4633 if (TYPE_P (n))
4634 return arg_assoc_type (k, n);
4635
4636 if (! type_unknown_p (n))
4637 return arg_assoc_type (k, TREE_TYPE (n));
4638
4639 if (TREE_CODE (n) == ADDR_EXPR)
4640 n = TREE_OPERAND (n, 0);
4641 if (TREE_CODE (n) == COMPONENT_REF)
4642 n = TREE_OPERAND (n, 1);
4643 if (TREE_CODE (n) == OFFSET_REF)
4644 n = TREE_OPERAND (n, 1);
4645 while (TREE_CODE (n) == TREE_LIST)
4646 n = TREE_VALUE (n);
4647 if (TREE_CODE (n) == BASELINK)
4648 n = BASELINK_FUNCTIONS (n);
4649
4650 if (TREE_CODE (n) == FUNCTION_DECL)
4651 return arg_assoc_type (k, TREE_TYPE (n));
4652 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4653 {
4654 /* [basic.lookup.koenig]
4655
4656 If T is a template-id, its associated namespaces and classes
4657 are the namespace in which the template is defined; for
4658 member templates, the member template's class... */
4659 tree template = TREE_OPERAND (n, 0);
4660 tree args = TREE_OPERAND (n, 1);
4661 tree ctx;
4662 int ix;
4663
4664 if (TREE_CODE (template) == COMPONENT_REF)
4665 template = TREE_OPERAND (template, 1);
4666
4667 /* First, the template. There may actually be more than one if
4668 this is an overloaded function template. But, in that case,
4669 we only need the first; all the functions will be in the same
4670 namespace. */
4671 template = OVL_CURRENT (template);
4672
4673 ctx = CP_DECL_CONTEXT (template);
4674
4675 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4676 {
4677 if (arg_assoc_namespace (k, ctx) == 1)
4678 return true;
4679 }
4680 /* It must be a member template. */
4681 else if (arg_assoc_class (k, ctx) == 1)
4682 return true;
4683
4684 /* Now the arguments. */
4685 if (args)
4686 for (ix = TREE_VEC_LENGTH (args); ix--;)
4687 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
4688 return true;
4689 }
4690 else if (TREE_CODE (n) == OVERLOAD)
4691 {
4692 for (; n; n = OVL_CHAIN (n))
4693 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4694 return true;
4695 }
4696
4697 return false;
4698 }
4699
4700 /* Performs Koenig lookup depending on arguments, where fns
4701 are the functions found in normal lookup. */
4702
4703 tree
lookup_arg_dependent(tree name,tree fns,tree args)4704 lookup_arg_dependent (tree name, tree fns, tree args)
4705 {
4706 struct arg_lookup k;
4707
4708 timevar_push (TV_NAME_LOOKUP);
4709
4710 /* Remove any hidden friend functions from the list of functions
4711 found so far. They will be added back by arg_assoc_class as
4712 appropriate. */
4713 fns = remove_hidden_names (fns);
4714
4715 k.name = name;
4716 k.args = args;
4717 k.functions = fns;
4718 k.classes = NULL_TREE;
4719
4720 /* We previously performed an optimization here by setting
4721 NAMESPACES to the current namespace when it was safe. However, DR
4722 164 says that namespaces that were already searched in the first
4723 stage of template processing are searched again (potentially
4724 picking up later definitions) in the second stage. */
4725 k.namespaces = NULL_TREE;
4726
4727 arg_assoc_args (&k, args);
4728
4729 fns = k.functions;
4730
4731 if (fns
4732 && TREE_CODE (fns) != VAR_DECL
4733 && !is_overloaded_fn (fns))
4734 {
4735 error ("argument dependent lookup finds %q+D", fns);
4736 error (" in call to %qD", name);
4737 fns = error_mark_node;
4738 }
4739
4740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
4741 }
4742
4743 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4744 changed (i.e. there was already a directive), or the fresh
4745 TREE_LIST otherwise. */
4746
4747 static tree
push_using_directive(tree used)4748 push_using_directive (tree used)
4749 {
4750 tree ud = current_binding_level->using_directives;
4751 tree iter, ancestor;
4752
4753 timevar_push (TV_NAME_LOOKUP);
4754 /* Check if we already have this. */
4755 if (purpose_member (used, ud) != NULL_TREE)
4756 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4757
4758 ancestor = namespace_ancestor (current_decl_namespace (), used);
4759 ud = current_binding_level->using_directives;
4760 ud = tree_cons (used, ancestor, ud);
4761 current_binding_level->using_directives = ud;
4762
4763 /* Recursively add all namespaces used. */
4764 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4765 push_using_directive (TREE_PURPOSE (iter));
4766
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4768 }
4769
4770 /* The type TYPE is being declared. If it is a class template, or a
4771 specialization of a class template, do any processing required and
4772 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
4773 being declared a friend. B is the binding level at which this TYPE
4774 should be bound.
4775
4776 Returns the TYPE_DECL for TYPE, which may have been altered by this
4777 processing. */
4778
4779 static tree
maybe_process_template_type_declaration(tree type,int is_friend,cxx_scope * b)4780 maybe_process_template_type_declaration (tree type, int is_friend,
4781 cxx_scope *b)
4782 {
4783 tree decl = TYPE_NAME (type);
4784
4785 if (processing_template_parmlist)
4786 /* You can't declare a new template type in a template parameter
4787 list. But, you can declare a non-template type:
4788
4789 template <class A*> struct S;
4790
4791 is a forward-declaration of `A'. */
4792 ;
4793 else if (b->kind == sk_namespace
4794 && current_binding_level->kind != sk_namespace)
4795 /* If this new type is being injected into a containing scope,
4796 then it's not a template type. */
4797 ;
4798 else
4799 {
4800 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
4801
4802 if (processing_template_decl)
4803 {
4804 /* This may change after the call to
4805 push_template_decl_real, but we want the original value. */
4806 tree name = DECL_NAME (decl);
4807
4808 decl = push_template_decl_real (decl, is_friend);
4809 /* If the current binding level is the binding level for the
4810 template parameters (see the comment in
4811 begin_template_parm_list) and the enclosing level is a class
4812 scope, and we're not looking at a friend, push the
4813 declaration of the member class into the class scope. In the
4814 friend case, push_template_decl will already have put the
4815 friend into global scope, if appropriate. */
4816 if (TREE_CODE (type) != ENUMERAL_TYPE
4817 && !is_friend && b->kind == sk_template_parms
4818 && b->level_chain->kind == sk_class)
4819 {
4820 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
4821
4822 if (!COMPLETE_TYPE_P (current_class_type))
4823 {
4824 maybe_add_class_template_decl_list (current_class_type,
4825 type, /*friend_p=*/0);
4826 /* Put this UTD in the table of UTDs for the class. */
4827 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4828 CLASSTYPE_NESTED_UTDS (current_class_type) =
4829 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4830
4831 binding_table_insert
4832 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4833 }
4834 }
4835 }
4836 }
4837
4838 return decl;
4839 }
4840
4841 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
4842 that the NAME is a class template, the tag is processed but not pushed.
4843
4844 The pushed scope depend on the SCOPE parameter:
4845 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
4846 scope.
4847 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
4848 non-template-parameter scope. This case is needed for forward
4849 declarations.
4850 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
4851 TS_GLOBAL case except that names within template-parameter scopes
4852 are not pushed at all.
4853
4854 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
4855
4856 tree
pushtag(tree name,tree type,tag_scope scope)4857 pushtag (tree name, tree type, tag_scope scope)
4858 {
4859 struct cp_binding_level *b;
4860 tree decl;
4861
4862 timevar_push (TV_NAME_LOOKUP);
4863 b = current_binding_level;
4864 while (/* Cleanup scopes are not scopes from the point of view of
4865 the language. */
4866 b->kind == sk_cleanup
4867 /* Neither are the scopes used to hold template parameters
4868 for an explicit specialization. For an ordinary template
4869 declaration, these scopes are not scopes from the point of
4870 view of the language. */
4871 || (b->kind == sk_template_parms
4872 && (b->explicit_spec_p || scope == ts_global))
4873 || (b->kind == sk_class
4874 && (scope != ts_current
4875 /* We may be defining a new type in the initializer
4876 of a static member variable. We allow this when
4877 not pedantic, and it is particularly useful for
4878 type punning via an anonymous union. */
4879 || COMPLETE_TYPE_P (b->this_entity))))
4880 b = b->level_chain;
4881
4882 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4883
4884 /* Do C++ gratuitous typedefing. */
4885 if (IDENTIFIER_TYPE_VALUE (name) != type)
4886 {
4887 tree tdef;
4888 int in_class = 0;
4889 tree context = TYPE_CONTEXT (type);
4890
4891 if (! context)
4892 {
4893 tree cs = current_scope ();
4894
4895 if (scope == ts_current)
4896 context = cs;
4897 else if (cs != NULL_TREE && TYPE_P (cs))
4898 /* When declaring a friend class of a local class, we want
4899 to inject the newly named class into the scope
4900 containing the local class, not the namespace
4901 scope. */
4902 context = decl_function_context (get_type_decl (cs));
4903 }
4904 if (!context)
4905 context = current_namespace;
4906
4907 if (b->kind == sk_class
4908 || (b->kind == sk_template_parms
4909 && b->level_chain->kind == sk_class))
4910 in_class = 1;
4911
4912 if (current_lang_name == lang_name_java)
4913 TYPE_FOR_JAVA (type) = 1;
4914
4915 tdef = create_implicit_typedef (name, type);
4916 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
4917 if (scope == ts_within_enclosing_non_class)
4918 {
4919 /* This is a friend. Make this TYPE_DECL node hidden from
4920 ordinary name lookup. Its corresponding TEMPLATE_DECL
4921 will be marked in push_template_decl_real. */
4922 retrofit_lang_decl (tdef);
4923 DECL_ANTICIPATED (tdef) = 1;
4924 DECL_FRIEND_P (tdef) = 1;
4925 }
4926
4927 decl = maybe_process_template_type_declaration
4928 (type, scope == ts_within_enclosing_non_class, b);
4929 if (decl == error_mark_node)
4930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4931
4932 if (! in_class)
4933 set_identifier_type_value_with_scope (name, tdef, b);
4934
4935 if (b->kind == sk_class)
4936 {
4937 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
4938 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
4939 class. But if it's a member template class, we want
4940 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
4941 later. */
4942 finish_member_declaration (decl);
4943 else
4944 pushdecl_class_level (decl);
4945 }
4946 else if (b->kind != sk_template_parms)
4947 {
4948 decl = pushdecl_with_scope (decl, b, /*is_friend=*/false);
4949 if (decl == error_mark_node)
4950 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4951 }
4952
4953 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
4954
4955 /* If this is a local class, keep track of it. We need this
4956 information for name-mangling, and so that it is possible to
4957 find all function definitions in a translation unit in a
4958 convenient way. (It's otherwise tricky to find a member
4959 function definition it's only pointed to from within a local
4960 class.) */
4961 if (TYPE_CONTEXT (type)
4962 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
4963 VEC_safe_push (tree, gc, local_classes, type);
4964 }
4965 if (b->kind == sk_class
4966 && !COMPLETE_TYPE_P (current_class_type))
4967 {
4968 maybe_add_class_template_decl_list (current_class_type,
4969 type, /*friend_p=*/0);
4970
4971 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
4972 CLASSTYPE_NESTED_UTDS (current_class_type)
4973 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
4974
4975 binding_table_insert
4976 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
4977 }
4978
4979 decl = TYPE_NAME (type);
4980 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
4981 TYPE_STUB_DECL (type) = decl;
4982
4983 /* Set type visibility now if this is a forward declaration. */
4984 TREE_PUBLIC (decl) = 1;
4985 determine_visibility (decl);
4986
4987 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
4988 }
4989
4990 /* Subroutines for reverting temporarily to top-level for instantiation
4991 of templates and such. We actually need to clear out the class- and
4992 local-value slots of all identifiers, so that only the global values
4993 are at all visible. Simply setting current_binding_level to the global
4994 scope isn't enough, because more binding levels may be pushed. */
4995 struct saved_scope *scope_chain;
4996
4997 /* If ID has not already been marked, add an appropriate binding to
4998 *OLD_BINDINGS. */
4999
5000 static void
store_binding(tree id,VEC (cxx_saved_binding,gc)** old_bindings)5001 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5002 {
5003 cxx_saved_binding *saved;
5004
5005 if (!id || !IDENTIFIER_BINDING (id))
5006 return;
5007
5008 if (IDENTIFIER_MARKED (id))
5009 return;
5010
5011 IDENTIFIER_MARKED (id) = 1;
5012
5013 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5014 saved->identifier = id;
5015 saved->binding = IDENTIFIER_BINDING (id);
5016 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5017 IDENTIFIER_BINDING (id) = NULL;
5018 }
5019
5020 static void
store_bindings(tree names,VEC (cxx_saved_binding,gc)** old_bindings)5021 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5022 {
5023 tree t;
5024
5025 timevar_push (TV_NAME_LOOKUP);
5026 for (t = names; t; t = TREE_CHAIN (t))
5027 {
5028 tree id;
5029
5030 if (TREE_CODE (t) == TREE_LIST)
5031 id = TREE_PURPOSE (t);
5032 else
5033 id = DECL_NAME (t);
5034
5035 store_binding (id, old_bindings);
5036 }
5037 timevar_pop (TV_NAME_LOOKUP);
5038 }
5039
5040 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5041 objects, rather than a TREE_LIST. */
5042
5043 static void
store_class_bindings(VEC (cp_class_binding,gc)* names,VEC (cxx_saved_binding,gc)** old_bindings)5044 store_class_bindings (VEC(cp_class_binding,gc) *names,
5045 VEC(cxx_saved_binding,gc) **old_bindings)
5046 {
5047 size_t i;
5048 cp_class_binding *cb;
5049
5050 timevar_push (TV_NAME_LOOKUP);
5051 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5052 store_binding (cb->identifier, old_bindings);
5053 timevar_pop (TV_NAME_LOOKUP);
5054 }
5055
5056 void
push_to_top_level(void)5057 push_to_top_level (void)
5058 {
5059 struct saved_scope *s;
5060 struct cp_binding_level *b;
5061 cxx_saved_binding *sb;
5062 size_t i;
5063 int need_pop;
5064
5065 timevar_push (TV_NAME_LOOKUP);
5066 s = GGC_CNEW (struct saved_scope);
5067
5068 b = scope_chain ? current_binding_level : 0;
5069
5070 /* If we're in the middle of some function, save our state. */
5071 if (cfun)
5072 {
5073 need_pop = 1;
5074 push_function_context_to (NULL_TREE);
5075 }
5076 else
5077 need_pop = 0;
5078
5079 if (scope_chain && previous_class_level)
5080 store_class_bindings (previous_class_level->class_shadowed,
5081 &s->old_bindings);
5082
5083 /* Have to include the global scope, because class-scope decls
5084 aren't listed anywhere useful. */
5085 for (; b; b = b->level_chain)
5086 {
5087 tree t;
5088
5089 /* Template IDs are inserted into the global level. If they were
5090 inserted into namespace level, finish_file wouldn't find them
5091 when doing pending instantiations. Therefore, don't stop at
5092 namespace level, but continue until :: . */
5093 if (global_scope_p (b))
5094 break;
5095
5096 store_bindings (b->names, &s->old_bindings);
5097 /* We also need to check class_shadowed to save class-level type
5098 bindings, since pushclass doesn't fill in b->names. */
5099 if (b->kind == sk_class)
5100 store_class_bindings (b->class_shadowed, &s->old_bindings);
5101
5102 /* Unwind type-value slots back to top level. */
5103 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5104 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5105 }
5106
5107 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
5108 IDENTIFIER_MARKED (sb->identifier) = 0;
5109
5110 s->prev = scope_chain;
5111 s->bindings = b;
5112 s->need_pop_function_context = need_pop;
5113 s->function_decl = current_function_decl;
5114 s->skip_evaluation = skip_evaluation;
5115
5116 scope_chain = s;
5117 current_function_decl = NULL_TREE;
5118 current_lang_base = VEC_alloc (tree, gc, 10);
5119 current_lang_name = lang_name_cplusplus;
5120 current_namespace = global_namespace;
5121 push_class_stack ();
5122 skip_evaluation = 0;
5123 timevar_pop (TV_NAME_LOOKUP);
5124 }
5125
5126 void
pop_from_top_level(void)5127 pop_from_top_level (void)
5128 {
5129 struct saved_scope *s = scope_chain;
5130 cxx_saved_binding *saved;
5131 size_t i;
5132
5133 timevar_push (TV_NAME_LOOKUP);
5134 /* Clear out class-level bindings cache. */
5135 if (previous_class_level)
5136 invalidate_class_lookup_cache ();
5137 pop_class_stack ();
5138
5139 current_lang_base = 0;
5140
5141 scope_chain = s->prev;
5142 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
5143 {
5144 tree id = saved->identifier;
5145
5146 IDENTIFIER_BINDING (id) = saved->binding;
5147 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5148 }
5149
5150 /* If we were in the middle of compiling a function, restore our
5151 state. */
5152 if (s->need_pop_function_context)
5153 pop_function_context_from (NULL_TREE);
5154 current_function_decl = s->function_decl;
5155 skip_evaluation = s->skip_evaluation;
5156 timevar_pop (TV_NAME_LOOKUP);
5157 }
5158
5159 /* Pop off extraneous binding levels left over due to syntax errors.
5160
5161 We don't pop past namespaces, as they might be valid. */
5162
5163 void
pop_everything(void)5164 pop_everything (void)
5165 {
5166 if (ENABLE_SCOPE_CHECKING)
5167 verbatim ("XXX entering pop_everything ()\n");
5168 while (!toplevel_bindings_p ())
5169 {
5170 if (current_binding_level->kind == sk_class)
5171 pop_nested_class ();
5172 else
5173 poplevel (0, 0, 0);
5174 }
5175 if (ENABLE_SCOPE_CHECKING)
5176 verbatim ("XXX leaving pop_everything ()\n");
5177 }
5178
5179 /* Emit debugging information for using declarations and directives.
5180 If input tree is overloaded fn then emit debug info for all
5181 candidates. */
5182
5183 void
cp_emit_debug_info_for_using(tree t,tree context)5184 cp_emit_debug_info_for_using (tree t, tree context)
5185 {
5186 /* Don't try to emit any debug information if we have errors. */
5187 if (sorrycount || errorcount)
5188 return;
5189
5190 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
5191 of a builtin function. */
5192 if (TREE_CODE (t) == FUNCTION_DECL
5193 && DECL_EXTERNAL (t)
5194 && DECL_BUILT_IN (t))
5195 return;
5196
5197 /* Do not supply context to imported_module_or_decl, if
5198 it is a global namespace. */
5199 if (context == global_namespace)
5200 context = NULL_TREE;
5201
5202 if (BASELINK_P (t))
5203 t = BASELINK_FUNCTIONS (t);
5204
5205 /* FIXME: Handle TEMPLATE_DECLs. */
5206 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
5207 if (TREE_CODE (t) != TEMPLATE_DECL)
5208 (*debug_hooks->imported_module_or_decl) (t, context);
5209 }
5210
5211 #include "gt-cp-name-lookup.h"
5212