00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "config.h"
00027 #include "system.h"
00028 #include "coretypes.h"
00029 #include "tm.h"
00030 #include "flags.h"
00031 #include "tree.h"
00032 #include "cp-tree.h"
00033 #include "name-lookup.h"
00034 #include "timevar.h"
00035 #include "toplev.h"
00036 #include "diagnostic.h"
00037 #include "debug.h"
00038 #include "c-pragma.h"
00039
00040 #ifdef KEY
00041 extern void gspin_gxx_emits_decl (tree);
00042 #endif
00043
00044
00045
00046 struct scope_binding {
00047 tree value;
00048 tree type;
00049 };
00050 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
00051
00052 static cxx_scope *innermost_nonclass_level (void);
00053 static tree select_decl (const struct scope_binding *, int);
00054 static cxx_binding *binding_for_name (cxx_scope *, tree);
00055 static tree lookup_name_innermost_nonclass_level (tree);
00056 static tree push_overloaded_decl (tree, int, bool);
00057 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
00058 tree, int);
00059 static bool qualified_lookup_using_namespace (tree, tree,
00060 struct scope_binding *, int);
00061 static tree lookup_type_current_level (tree);
00062 static tree push_using_directive (tree);
00063
00064
00065
00066 tree global_namespace;
00067
00068
00069
00070 static GTY(()) tree anonymous_namespace_name;
00071
00072
00073
00074
00075
00076
00077 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
00078
00079
00080
00081 static GTY((deletable)) binding_entry free_binding_entry = NULL;
00082
00083
00084
00085 static inline binding_entry
00086 binding_entry_make (tree name, tree type)
00087 {
00088 binding_entry entry;
00089
00090 if (free_binding_entry)
00091 {
00092 entry = free_binding_entry;
00093 free_binding_entry = entry->chain;
00094 }
00095 else
00096 entry = GGC_NEW (struct binding_entry_s);
00097
00098 entry->name = name;
00099 entry->type = type;
00100 entry->chain = NULL;
00101
00102 return entry;
00103 }
00104
00105
00106 #if 0
00107 static inline void
00108 binding_entry_free (binding_entry entry)
00109 {
00110 entry->name = NULL;
00111 entry->type = NULL;
00112 entry->chain = free_binding_entry;
00113 free_binding_entry = entry;
00114 }
00115 #endif
00116
00117
00118
00119 struct binding_table_s GTY(())
00120 {
00121
00122 binding_entry * GTY((length ("%h.chain_count"))) chain;
00123
00124
00125
00126 size_t chain_count;
00127
00128
00129 size_t entry_count;
00130 };
00131
00132
00133
00134 static inline void
00135 binding_table_construct (binding_table table, size_t chain_count)
00136 {
00137 table->chain_count = chain_count;
00138 table->entry_count = 0;
00139 table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
00140 }
00141
00142
00143 #if 0
00144 static void
00145 binding_table_free (binding_table table)
00146 {
00147 size_t i;
00148 size_t count;
00149
00150 if (table == NULL)
00151 return;
00152
00153 for (i = 0, count = table->chain_count; i < count; ++i)
00154 {
00155 binding_entry temp = table->chain[i];
00156 while (temp != NULL)
00157 {
00158 binding_entry entry = temp;
00159 temp = entry->chain;
00160 binding_entry_free (entry);
00161 }
00162 table->chain[i] = NULL;
00163 }
00164 table->entry_count = 0;
00165 }
00166 #endif
00167
00168
00169
00170 static inline binding_table
00171 binding_table_new (size_t chain_count)
00172 {
00173 binding_table table = GGC_NEW (struct binding_table_s);
00174 table->chain = NULL;
00175 binding_table_construct (table, chain_count);
00176 return table;
00177 }
00178
00179
00180
00181 static void
00182 binding_table_expand (binding_table table)
00183 {
00184 const size_t old_chain_count = table->chain_count;
00185 const size_t old_entry_count = table->entry_count;
00186 const size_t new_chain_count = 2 * old_chain_count;
00187 binding_entry *old_chains = table->chain;
00188 size_t i;
00189
00190 binding_table_construct (table, new_chain_count);
00191 for (i = 0; i < old_chain_count; ++i)
00192 {
00193 binding_entry entry = old_chains[i];
00194 for (; entry != NULL; entry = old_chains[i])
00195 {
00196 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
00197 const size_t j = ENTRY_INDEX (hash, new_chain_count);
00198
00199 old_chains[i] = entry->chain;
00200 entry->chain = table->chain[j];
00201 table->chain[j] = entry;
00202 }
00203 }
00204 table->entry_count = old_entry_count;
00205 }
00206
00207
00208
00209 static void
00210 binding_table_insert (binding_table table, tree name, tree type)
00211 {
00212 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
00213 const size_t i = ENTRY_INDEX (hash, table->chain_count);
00214 binding_entry entry = binding_entry_make (name, type);
00215
00216 entry->chain = table->chain[i];
00217 table->chain[i] = entry;
00218 ++table->entry_count;
00219
00220 if (3 * table->chain_count < 5 * table->entry_count)
00221 binding_table_expand (table);
00222 }
00223
00224
00225
00226 binding_entry
00227 binding_table_find (binding_table table, tree name)
00228 {
00229 const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
00230 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
00231
00232 while (entry != NULL && entry->name != name)
00233 entry = entry->chain;
00234
00235 return entry;
00236 }
00237
00238
00239
00240 void
00241 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
00242 {
00243 const size_t chain_count = table->chain_count;
00244 size_t i;
00245
00246 for (i = 0; i < chain_count; ++i)
00247 {
00248 binding_entry entry = table->chain[i];
00249 for (; entry != NULL; entry = entry->chain)
00250 proc (entry, data);
00251 }
00252 }
00253
00254 #ifndef ENABLE_SCOPE_CHECKING
00255 # define ENABLE_SCOPE_CHECKING 0
00256 #else
00257 # define ENABLE_SCOPE_CHECKING 1
00258 #endif
00259
00260
00261
00262 static GTY((deletable)) cxx_binding *free_bindings;
00263
00264
00265
00266
00267 static inline void
00268 cxx_binding_init (cxx_binding *binding, tree value, tree type)
00269 {
00270 binding->value = value;
00271 binding->type = type;
00272 binding->previous = NULL;
00273 }
00274
00275
00276
00277 static cxx_binding *
00278 cxx_binding_make (tree value, tree type)
00279 {
00280 cxx_binding *binding;
00281 if (free_bindings)
00282 {
00283 binding = free_bindings;
00284 free_bindings = binding->previous;
00285 }
00286 else
00287 binding = GGC_NEW (cxx_binding);
00288
00289 cxx_binding_init (binding, value, type);
00290
00291 return binding;
00292 }
00293
00294
00295
00296 static inline void
00297 cxx_binding_free (cxx_binding *binding)
00298 {
00299 binding->scope = NULL;
00300 binding->previous = free_bindings;
00301 free_bindings = binding;
00302 }
00303
00304
00305
00306
00307 static cxx_binding *
00308 new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
00309 {
00310 cp_class_binding *cb;
00311 cxx_binding *binding;
00312
00313 if (VEC_length (cp_class_binding, scope->class_shadowed))
00314 {
00315 cp_class_binding *old_base;
00316 old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
00317 if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
00318 {
00319
00320 size_t i;
00321
00322 for (i = 0;
00323 VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
00324 i++)
00325 {
00326 cxx_binding **b;
00327 b = &IDENTIFIER_BINDING (cb->identifier);
00328 while (*b != &old_base[i].base)
00329 b = &((*b)->previous);
00330 *b = &cb->base;
00331 }
00332 }
00333 cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
00334 }
00335 else
00336 cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
00337
00338 cb->identifier = name;
00339 binding = &cb->base;
00340 binding->scope = scope;
00341 cxx_binding_init (binding, value, type);
00342 return binding;
00343 }
00344
00345
00346
00347
00348 static void
00349 push_binding (tree id, tree decl, cxx_scope* level)
00350 {
00351 cxx_binding *binding;
00352
00353 if (level != class_binding_level)
00354 {
00355 binding = cxx_binding_make (decl, NULL_TREE);
00356 binding->scope = level;
00357 }
00358 else
00359 binding = new_class_binding (id, decl, NULL_TREE, level);
00360
00361
00362 binding->previous = IDENTIFIER_BINDING (id);
00363 INHERITED_VALUE_BINDING_P (binding) = 0;
00364 LOCAL_BINDING_P (binding) = (level != class_binding_level);
00365
00366
00367 IDENTIFIER_BINDING (id) = binding;
00368 }
00369
00370
00371
00372
00373 void
00374 pop_binding (tree id, tree decl)
00375 {
00376 cxx_binding *binding;
00377
00378 if (id == NULL_TREE)
00379
00380
00381
00382 return;
00383
00384
00385 binding = IDENTIFIER_BINDING (id);
00386
00387
00388 gcc_assert (binding != NULL);
00389
00390
00391
00392 if (binding->value == decl)
00393 binding->value = NULL_TREE;
00394 else
00395 {
00396 gcc_assert (binding->type == decl);
00397 binding->type = NULL_TREE;
00398 }
00399
00400 if (!binding->value && !binding->type)
00401 {
00402
00403
00404 IDENTIFIER_BINDING (id) = binding->previous;
00405
00406
00407 cxx_binding_free (binding);
00408 }
00409 }
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 static bool
00430 supplement_binding (cxx_binding *binding, tree decl)
00431 {
00432 tree bval = binding->value;
00433 bool ok = true;
00434
00435 timevar_push (TV_NAME_LOOKUP);
00436 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
00437
00438 binding->type = decl;
00439 else if (
00440
00441
00442 !bval
00443
00444
00445
00446
00447
00448 || bval == error_mark_node
00449
00450
00451 || (TREE_CODE (bval) == FUNCTION_DECL
00452 && DECL_ANTICIPATED (bval)
00453 && !DECL_HIDDEN_FRIEND_P (bval)))
00454 binding->value = decl;
00455 else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
00456 {
00457
00458
00459
00460
00461
00462 binding->type = bval;
00463 binding->value = decl;
00464 binding->value_is_inherited = false;
00465 }
00466 else if (TREE_CODE (bval) == TYPE_DECL
00467 && TREE_CODE (decl) == TYPE_DECL
00468 && DECL_NAME (decl) == DECL_NAME (bval)
00469 && binding->scope->kind != sk_class
00470 && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
00471
00472
00473 || uses_template_parms (TREE_TYPE (decl))
00474 || uses_template_parms (TREE_TYPE (bval))))
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 ok = false;
00488
00489
00490
00491
00492
00493
00494
00495
00496 else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
00497 && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
00498 && !DECL_CLASS_SCOPE_P (decl))
00499 {
00500 duplicate_decls (decl, binding->value, false);
00501 ok = false;
00502 }
00503 else if (TREE_CODE (decl) == NAMESPACE_DECL
00504 && TREE_CODE (bval) == NAMESPACE_DECL
00505 && DECL_NAMESPACE_ALIAS (decl)
00506 && DECL_NAMESPACE_ALIAS (bval)
00507 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
00508
00509
00510
00511
00512
00513
00514 ok = false;
00515 else
00516 {
00517 error ("declaration of %q#D", decl);
00518 error ("conflicts with previous declaration %q+#D", bval);
00519 ok = false;
00520 }
00521
00522 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
00523 }
00524
00525
00526
00527 static void
00528 add_decl_to_level (tree decl, cxx_scope *b)
00529 {
00530 if (TREE_CODE (decl) == NAMESPACE_DECL
00531 && !DECL_NAMESPACE_ALIAS (decl))
00532 {
00533 TREE_CHAIN (decl) = b->namespaces;
00534 b->namespaces = decl;
00535 }
00536 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
00537 {
00538 TREE_CHAIN (decl) = b->vtables;
00539 b->vtables = decl;
00540 }
00541 else
00542 {
00543
00544
00545 TREE_CHAIN (decl) = b->names;
00546 b->names = decl;
00547 b->names_size++;
00548
00549
00550
00551
00552
00553 if (b->kind == sk_namespace)
00554 if ((TREE_CODE (decl) == VAR_DECL
00555 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
00556 || (TREE_CODE (decl) == FUNCTION_DECL
00557 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
00558 VEC_safe_push (tree, gc, b->static_decls, decl);
00559 }
00560 }
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 tree
00572 pushdecl_maybe_friend (tree x, bool is_friend)
00573 {
00574 tree t;
00575 tree name;
00576 int need_new_binding;
00577
00578 timevar_push (TV_NAME_LOOKUP);
00579
00580 if (x == error_mark_node)
00581 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
00582
00583 need_new_binding = 1;
00584
00585 if (DECL_TEMPLATE_PARM_P (x))
00586
00587
00588 ;
00589 else
00590 {
00591 if (current_function_decl && x != current_function_decl
00592
00593
00594 && TREE_CODE (x) != FUNCTION_DECL
00595
00596
00597
00598 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
00599 && !DECL_CONTEXT (x))
00600 DECL_CONTEXT (x) = current_function_decl;
00601
00602
00603
00604
00605 if (TREE_CODE (x) == FUNCTION_DECL
00606 && DECL_NAMESPACE_SCOPE_P (x)
00607 && current_function_decl
00608 && x != current_function_decl)
00609 DECL_LOCAL_FUNCTION_P (x) = 1;
00610 }
00611
00612 name = DECL_NAME (x);
00613 if (name)
00614 {
00615 int different_binding_level = 0;
00616
00617 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
00618 name = TREE_OPERAND (name, 0);
00619
00620
00621
00622 if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
00623 t = namespace_binding (name, DECL_CONTEXT (x));
00624 else
00625 t = lookup_name_innermost_nonclass_level (name);
00626
00627
00628
00629
00630
00631
00632 if (! t && current_function_decl && x != current_function_decl
00633 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
00634 && DECL_EXTERNAL (x))
00635 {
00636
00637 t = innermost_non_namespace_value (name);
00638
00639 if (! t)
00640 t = namespace_binding (name, DECL_CONTEXT (x));
00641
00642
00643 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
00644 t = NULL_TREE;
00645 if (t)
00646 different_binding_level = 1;
00647 }
00648
00649
00650
00651
00652
00653
00654 if (t && TREE_CODE (t) == OVERLOAD)
00655 {
00656 tree match;
00657
00658 if (TREE_CODE (x) == FUNCTION_DECL)
00659 for (match = t; match; match = OVL_NEXT (match))
00660 {
00661 if (decls_match (OVL_CURRENT (match), x))
00662 break;
00663 }
00664 else
00665
00666 match = t;
00667
00668 if (match)
00669 t = OVL_CURRENT (match);
00670 else
00671 t = NULL_TREE;
00672 }
00673
00674 if (t && t != error_mark_node)
00675 {
00676 if (different_binding_level)
00677 {
00678 if (decls_match (x, t))
00679
00680
00681
00682
00683
00684
00685 {
00686 struct cxx_int_tree_map *h;
00687 void **loc;
00688
00689 TREE_PUBLIC (x) = TREE_PUBLIC (t);
00690
00691 if (cp_function_chain->extern_decl_map == NULL)
00692 cp_function_chain->extern_decl_map
00693 = htab_create_ggc (20, cxx_int_tree_map_hash,
00694 cxx_int_tree_map_eq, NULL);
00695
00696 h = GGC_NEW (struct cxx_int_tree_map);
00697 h->uid = DECL_UID (x);
00698 h->to = t;
00699 loc = htab_find_slot_with_hash
00700 (cp_function_chain->extern_decl_map, h,
00701 h->uid, INSERT);
00702 *(struct cxx_int_tree_map **) loc = h;
00703 }
00704 }
00705 else if (TREE_CODE (t) == PARM_DECL)
00706 {
00707 gcc_assert (DECL_CONTEXT (t));
00708
00709
00710 if (duplicate_decls (x, t, is_friend))
00711 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
00712 }
00713 else if ((DECL_EXTERN_C_FUNCTION_P (x)
00714 || DECL_FUNCTION_TEMPLATE_P (x))
00715 && is_overloaded_fn (t))
00716 ;
00717 else if (t == wchar_decl_node)
00718 {
00719 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
00720 pedwarn ("redeclaration of %<wchar_t%> as %qT",
00721 TREE_TYPE (x));
00722
00723
00724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
00725 }
00726 else
00727 {
00728 tree olddecl = duplicate_decls (x, t, is_friend);
00729
00730
00731
00732 if (olddecl == error_mark_node)
00733 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
00734
00735 if (olddecl)
00736 {
00737 if (TREE_CODE (t) == TYPE_DECL)
00738 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
00739
00740 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
00741 }
00742 else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
00743 {
00744
00745
00746
00747
00748
00749
00750 error ("invalid redeclaration of %q+D", t);
00751 error ("as %qD", x);
00752
00753
00754 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
00755 }
00756 }
00757 }
00758
00759 if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
00760 check_default_args (x);
00761
00762 check_template_shadow (x);
00763
00764
00765
00766 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
00767 {
00768 retrofit_lang_decl (x);
00769 SET_DECL_LANGUAGE (x, lang_c);
00770 }
00771
00772 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
00773 {
00774 t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
00775 if (t != x)
00776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
00777 if (!namespace_bindings_p ())
00778
00779
00780
00781 need_new_binding = 0;
00782 }
00783 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
00784 {
00785 t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
00786 if (t == x)
00787 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
00788 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
00789 }
00790
00791
00792
00793
00794 if (TREE_CODE (x) == TYPE_DECL)
00795 {
00796 tree type = TREE_TYPE (x);
00797 if (DECL_IS_BUILTIN (x))
00798 {
00799 if (TYPE_NAME (type) == 0)
00800 TYPE_NAME (type) = x;
00801 }
00802 else if (type != error_mark_node && TYPE_NAME (type) != x
00803
00804
00805
00806 && (!TYPE_NAME (type)
00807 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
00808 {
00809 DECL_ORIGINAL_TYPE (x) = type;
00810 type = build_variant_type_copy (type);
00811 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
00812 TYPE_NAME (type) = x;
00813 TREE_TYPE (x) = type;
00814 }
00815
00816 if (type != error_mark_node
00817 && TYPE_NAME (type)
00818 && TYPE_IDENTIFIER (type))
00819 set_identifier_type_value (DECL_NAME (x), x);
00820 }
00821
00822
00823
00824
00825
00826
00827
00828 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
00829 {
00830 tree decl;
00831
00832 decl = IDENTIFIER_NAMESPACE_VALUE (name);
00833 if (decl && TREE_CODE (decl) == OVERLOAD)
00834 decl = OVL_FUNCTION (decl);
00835
00836 if (decl && decl != error_mark_node
00837 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
00838
00839 && TREE_CODE (decl) == TREE_CODE (x)
00840 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
00841 {
00842 pedwarn ("type mismatch with previous external decl of %q#D", x);
00843 pedwarn ("previous external decl of %q+#D", decl);
00844 }
00845 }
00846
00847 if (TREE_CODE (x) == FUNCTION_DECL
00848 && is_friend
00849 && !flag_friend_injection)
00850 {
00851
00852
00853 DECL_ANTICIPATED (x) = 1;
00854 DECL_HIDDEN_FRIEND_P (x) = 1;
00855 }
00856
00857
00858
00859 if (namespace_bindings_p ())
00860 {
00861
00862
00863
00864
00865 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
00866 TREE_PUBLIC (name) = 1;
00867
00868
00869 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
00870 && t != NULL_TREE)
00871 && (TREE_CODE (x) == TYPE_DECL
00872 || TREE_CODE (x) == VAR_DECL
00873 || TREE_CODE (x) == NAMESPACE_DECL
00874 || TREE_CODE (x) == CONST_DECL
00875 || TREE_CODE (x) == TEMPLATE_DECL))
00876 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
00877
00878
00879
00880 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
00881 warn_extern_redeclared_static (x, t);
00882 }
00883 else
00884 {
00885
00886 tree oldlocal = innermost_non_namespace_value (name);
00887 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
00888
00889 if (need_new_binding)
00890 {
00891 push_local_binding (name, x, 0);
00892
00893
00894
00895 need_new_binding = 0;
00896 }
00897
00898
00899 if (TREE_CODE (x) == TYPE_DECL)
00900 set_identifier_type_value (name, x);
00901
00902
00903
00904
00905 if (TREE_CODE (x) == NAMESPACE_DECL)
00906 set_identifier_type_value (name, NULL_TREE);
00907
00908 if (oldlocal)
00909 {
00910 tree d = oldlocal;
00911
00912 while (oldlocal
00913 && TREE_CODE (oldlocal) == VAR_DECL
00914 && DECL_DEAD_FOR_LOCAL (oldlocal))
00915 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
00916
00917 if (oldlocal == NULL_TREE)
00918 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
00919 }
00920
00921
00922
00923 if (oldlocal == NULL_TREE
00924 && DECL_EXTERNAL (x)
00925 && oldglobal != NULL_TREE
00926 && TREE_CODE (x) == FUNCTION_DECL
00927 && TREE_CODE (oldglobal) == FUNCTION_DECL)
00928 {
00929
00930 if (decls_match (x, oldglobal))
00931 ;
00932 else
00933 {
00934 warning (0, "extern declaration of %q#D doesn't match", x);
00935 warning (0, "global declaration %q+#D", oldglobal);
00936 }
00937 }
00938
00939
00940
00941 if (oldlocal == NULL_TREE
00942 && oldglobal == NULL_TREE
00943 && DECL_EXTERNAL (x)
00944 && TREE_PUBLIC (x))
00945 TREE_PUBLIC (name) = 1;
00946
00947
00948 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
00949
00950 && !DECL_FROM_INLINE (x)
00951 && TREE_CODE (oldlocal) == PARM_DECL
00952
00953 && !DECL_ARTIFICIAL (oldlocal))
00954 {
00955 bool err = false;
00956
00957
00958 if (DECL_CONTEXT (oldlocal) == current_function_decl
00959 && TREE_CODE (x) != PARM_DECL)
00960 {
00961
00962
00963 struct cp_binding_level *b = current_binding_level->level_chain;
00964
00965 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
00966
00967 b = b->level_chain;
00968
00969
00970 if (b->kind == sk_function_parms)
00971 {
00972 error ("declaration of %q#D shadows a parameter", x);
00973 err = true;
00974 }
00975 }
00976
00977 if (warn_shadow && !err)
00978 {
00979 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
00980 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
00981 }
00982 }
00983
00984
00985 else if (warn_shadow && !DECL_EXTERNAL (x)
00986
00987 && ! DECL_ARTIFICIAL (x)
00988
00989 && ! DECL_FROM_INLINE (x))
00990 {
00991 tree member;
00992
00993 if (current_class_ptr)
00994 member = lookup_member (current_class_type,
00995 name,
00996 0,
00997 false);
00998 else
00999 member = NULL_TREE;
01000
01001 if (member && !TREE_STATIC (member))
01002 {
01003
01004 warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
01005 x);
01006 }
01007 else if (oldlocal != NULL_TREE
01008 && TREE_CODE (oldlocal) == VAR_DECL)
01009 {
01010 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
01011 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
01012 }
01013 else if (oldglobal != NULL_TREE
01014 && TREE_CODE (oldglobal) == VAR_DECL)
01015
01016 {
01017 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
01018 x);
01019 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
01020 }
01021 }
01022 }
01023
01024 if (TREE_CODE (x) == VAR_DECL)
01025 maybe_register_incomplete_var (x);
01026 }
01027
01028 if (need_new_binding)
01029 add_decl_to_level (x,
01030 DECL_NAMESPACE_SCOPE_P (x)
01031 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
01032 : current_binding_level);
01033
01034 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
01035 }
01036
01037
01038
01039 tree
01040 pushdecl (tree x)
01041 {
01042 return pushdecl_maybe_friend (x, false);
01043 }
01044
01045
01046
01047
01048 tree
01049 maybe_push_decl (tree decl)
01050 {
01051 tree type = TREE_TYPE (decl);
01052
01053
01054
01055
01056 if (decl == error_mark_node
01057 || (TREE_CODE (decl) != PARM_DECL
01058 && DECL_CONTEXT (decl) != NULL_TREE
01059
01060
01061 && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
01062 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
01063 || TREE_CODE (type) == UNKNOWN_TYPE
01064
01065
01066
01067 || (TREE_CODE (decl) == FUNCTION_DECL
01068 && DECL_TEMPLATE_SPECIALIZATION (decl)))
01069 return decl;
01070 else
01071 return pushdecl (decl);
01072 }
01073
01074
01075
01076
01077
01078
01079 void
01080 push_local_binding (tree id, tree decl, int flags)
01081 {
01082 struct cp_binding_level *b;
01083
01084
01085
01086 b = innermost_nonclass_level ();
01087
01088 if (lookup_name_innermost_nonclass_level (id))
01089 {
01090
01091 if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
01092
01093
01094
01095 return;
01096 }
01097 else
01098
01099 push_binding (id, decl, b);
01100
01101 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
01102
01103
01104
01105 decl = build_tree_list (NULL_TREE, decl);
01106
01107
01108
01109 add_decl_to_level (decl, b);
01110 }
01111
01112
01113
01114
01115
01116
01117
01118
01119 tree
01120 check_for_out_of_scope_variable (tree decl)
01121 {
01122 tree shadowed;
01123
01124
01125 if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
01126 return decl;
01127
01128 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
01129 ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
01130 while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
01131 && DECL_DEAD_FOR_LOCAL (shadowed))
01132 shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
01133 ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
01134 if (!shadowed)
01135 shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
01136 if (shadowed)
01137 {
01138 if (!DECL_ERROR_REPORTED (decl))
01139 {
01140 warning (0, "name lookup of %qD changed", DECL_NAME (decl));
01141 warning (0, " matches this %q+D under ISO standard rules",
01142 shadowed);
01143 warning (0, " matches this %q+D under old rules", decl);
01144 DECL_ERROR_REPORTED (decl) = 1;
01145 }
01146 return shadowed;
01147 }
01148
01149
01150
01151 if (DECL_ERROR_REPORTED (decl))
01152 return decl;
01153
01154 DECL_ERROR_REPORTED (decl) = 1;
01155
01156 if (TREE_TYPE (decl) == error_mark_node)
01157 return decl;
01158
01159 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
01160 {
01161 error ("name lookup of %qD changed for new ISO %<for%> scoping",
01162 DECL_NAME (decl));
01163 error (" cannot use obsolete binding at %q+D because "
01164 "it has a destructor", decl);
01165 return error_mark_node;
01166 }
01167 else
01168 {
01169 pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
01170 DECL_NAME (decl));
01171 pedwarn (" using obsolete binding at %q+D", decl);
01172 }
01173
01174 return decl;
01175 }
01176
01177
01178
01179 static bool keep_next_level_flag;
01180
01181 static int binding_depth = 0;
01182 static int is_class_level = 0;
01183
01184 static void
01185 indent (int depth)
01186 {
01187 int i;
01188
01189 for (i = 0; i < depth * 2; i++)
01190 putc (' ', stderr);
01191 }
01192
01193
01194 static const char *
01195 cxx_scope_descriptor (cxx_scope *scope)
01196 {
01197
01198
01199 static const char* scope_kind_names[] = {
01200 "block-scope",
01201 "cleanup-scope",
01202 "try-scope",
01203 "catch-scope",
01204 "for-scope",
01205 "function-parameter-scope",
01206 "class-scope",
01207 "namespace-scope",
01208 "template-parameter-scope",
01209 "template-explicit-spec-scope"
01210 };
01211 const scope_kind kind = scope->explicit_spec_p
01212 ? sk_template_spec : scope->kind;
01213
01214 return scope_kind_names[kind];
01215 }
01216
01217
01218
01219 static void
01220 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
01221 {
01222 const char *desc = cxx_scope_descriptor (scope);
01223 if (scope->this_entity)
01224 verbatim ("%s %s(%E) %p %d\n", action, desc,
01225 scope->this_entity, (void *) scope, line);
01226 else
01227 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
01228 }
01229
01230
01231
01232
01233 static inline size_t
01234 namespace_scope_ht_size (tree ns)
01235 {
01236 tree name = DECL_NAME (ns);
01237
01238 return name == std_identifier
01239 ? NAMESPACE_STD_HT_SIZE
01240 : (name == global_scope_name
01241 ? GLOBAL_SCOPE_HT_SIZE
01242 : NAMESPACE_ORDINARY_HT_SIZE);
01243 }
01244
01245
01246
01247 static GTY((deletable)) struct cp_binding_level *free_binding_level;
01248
01249
01250
01251 void
01252 push_binding_level (struct cp_binding_level *scope)
01253 {
01254
01255 scope->level_chain = current_binding_level;
01256 current_binding_level = scope;
01257 keep_next_level_flag = false;
01258
01259 if (ENABLE_SCOPE_CHECKING)
01260 {
01261 scope->binding_depth = binding_depth;
01262 indent (binding_depth);
01263 cxx_scope_debug (scope, input_line, "push");
01264 is_class_level = 0;
01265 binding_depth++;
01266 }
01267 }
01268
01269
01270
01271
01272
01273 cxx_scope *
01274 begin_scope (scope_kind kind, tree entity)
01275 {
01276 cxx_scope *scope;
01277
01278
01279 if (!ENABLE_SCOPE_CHECKING && free_binding_level)
01280 {
01281 scope = free_binding_level;
01282 free_binding_level = scope->level_chain;
01283 }
01284 else
01285 scope = GGC_NEW (cxx_scope);
01286 memset (scope, 0, sizeof (cxx_scope));
01287
01288 scope->this_entity = entity;
01289 scope->more_cleanups_ok = true;
01290 switch (kind)
01291 {
01292 case sk_cleanup:
01293 scope->keep = true;
01294 break;
01295
01296 case sk_template_spec:
01297 scope->explicit_spec_p = true;
01298 kind = sk_template_parms;
01299
01300 case sk_template_parms:
01301 case sk_block:
01302 case sk_try:
01303 case sk_catch:
01304 case sk_for:
01305 case sk_class:
01306 case sk_function_parms:
01307 case sk_omp:
01308 scope->keep = keep_next_level_flag;
01309 break;
01310
01311 case sk_namespace:
01312 NAMESPACE_LEVEL (entity) = scope;
01313 scope->static_decls =
01314 VEC_alloc (tree, gc,
01315 DECL_NAME (entity) == std_identifier
01316 || DECL_NAME (entity) == global_scope_name
01317 ? 200 : 10);
01318 break;
01319
01320 default:
01321
01322 gcc_unreachable ();
01323 break;
01324 }
01325 scope->kind = kind;
01326
01327 push_binding_level (scope);
01328
01329 return scope;
01330 }
01331
01332
01333
01334
01335 cxx_scope *
01336 leave_scope (void)
01337 {
01338 cxx_scope *scope = current_binding_level;
01339
01340 if (scope->kind == sk_namespace && class_binding_level)
01341 current_binding_level = class_binding_level;
01342
01343
01344 if (NAMESPACE_LEVEL (global_namespace))
01345 gcc_assert (!global_scope_p (scope));
01346
01347 if (ENABLE_SCOPE_CHECKING)
01348 {
01349 indent (--binding_depth);
01350 cxx_scope_debug (scope, input_line, "leave");
01351 if (is_class_level != (scope == class_binding_level))
01352 {
01353 indent (binding_depth);
01354 verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
01355 }
01356 is_class_level = 0;
01357 }
01358
01359 #ifdef HANDLE_PRAGMA_VISIBILITY
01360 if (scope->has_visibility)
01361 pop_visibility ();
01362 #endif
01363
01364
01365 current_binding_level = scope->level_chain;
01366
01367
01368
01369
01370
01371
01372 if (scope->kind != sk_namespace
01373 && scope->kind != sk_class)
01374 {
01375 scope->level_chain = free_binding_level;
01376 gcc_assert (!ENABLE_SCOPE_CHECKING
01377 || scope->binding_depth == binding_depth);
01378 free_binding_level = scope;
01379 }
01380
01381
01382
01383 if (scope->kind == sk_class)
01384 {
01385 class_binding_level = NULL;
01386 for (scope = current_binding_level; scope; scope = scope->level_chain)
01387 if (scope->kind == sk_class)
01388 {
01389 class_binding_level = scope;
01390 break;
01391 }
01392 }
01393
01394 return current_binding_level;
01395 }
01396
01397 static void
01398 resume_scope (struct cp_binding_level* b)
01399 {
01400
01401
01402 gcc_assert (!class_binding_level);
01403
01404 gcc_assert (b->level_chain == current_binding_level);
01405 current_binding_level = b;
01406 if (ENABLE_SCOPE_CHECKING)
01407 {
01408 b->binding_depth = binding_depth;
01409 indent (binding_depth);
01410 cxx_scope_debug (b, input_line, "resume");
01411 is_class_level = 0;
01412 binding_depth++;
01413 }
01414 }
01415
01416
01417
01418 static cxx_scope *
01419 innermost_nonclass_level (void)
01420 {
01421 cxx_scope *b;
01422
01423 b = current_binding_level;
01424 while (b->kind == sk_class)
01425 b = b->level_chain;
01426
01427 return b;
01428 }
01429
01430
01431
01432
01433
01434 void
01435 maybe_push_cleanup_level (tree type)
01436 {
01437 if (type != error_mark_node
01438 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
01439 && current_binding_level->more_cleanups_ok == 0)
01440 {
01441 begin_scope (sk_cleanup, NULL);
01442 current_binding_level->statement_list = push_stmt_list ();
01443 }
01444 }
01445
01446
01447
01448 int
01449 global_bindings_p (void)
01450 {
01451 return global_scope_p (current_binding_level);
01452 }
01453
01454
01455
01456
01457
01458
01459
01460 bool
01461 toplevel_bindings_p (void)
01462 {
01463 struct cp_binding_level *b = innermost_nonclass_level ();
01464
01465 return b->kind == sk_namespace || b->kind == sk_template_parms;
01466 }
01467
01468
01469
01470
01471
01472 bool
01473 namespace_bindings_p (void)
01474 {
01475 struct cp_binding_level *b = innermost_nonclass_level ();
01476
01477 return b->kind == sk_namespace;
01478 }
01479
01480
01481
01482 bool
01483 kept_level_p (void)
01484 {
01485 return (current_binding_level->blocks != NULL_TREE
01486 || current_binding_level->keep
01487 || current_binding_level->kind == sk_cleanup
01488 || current_binding_level->names != NULL_TREE);
01489 }
01490
01491
01492
01493 scope_kind
01494 innermost_scope_kind (void)
01495 {
01496 return current_binding_level->kind;
01497 }
01498
01499
01500
01501 bool
01502 template_parm_scope_p (void)
01503 {
01504 return innermost_scope_kind () == sk_template_parms;
01505 }
01506
01507
01508
01509
01510
01511 void
01512 keep_next_level (bool keep)
01513 {
01514 keep_next_level_flag = keep;
01515 }
01516
01517
01518
01519
01520
01521
01522 tree
01523 getdecls (void)
01524 {
01525 return current_binding_level->names;
01526 }
01527
01528
01529 static int no_print_functions = 0;
01530 static int no_print_builtins = 0;
01531
01532 static void
01533 print_binding_level (struct cp_binding_level* lvl)
01534 {
01535 tree t;
01536 int i = 0, len;
01537 fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
01538 if (lvl->more_cleanups_ok)
01539 fprintf (stderr, " more-cleanups-ok");
01540 if (lvl->have_cleanups)
01541 fprintf (stderr, " have-cleanups");
01542 fprintf (stderr, "\n");
01543 if (lvl->names)
01544 {
01545 fprintf (stderr, " names:\t");
01546
01547 for (t = lvl->names; t; t = TREE_CHAIN (t))
01548 {
01549 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
01550 continue;
01551 if (no_print_builtins
01552 && (TREE_CODE (t) == TYPE_DECL)
01553 && DECL_IS_BUILTIN (t))
01554 continue;
01555
01556
01557 if (TREE_CODE (t) == FUNCTION_DECL)
01558 len = 3;
01559 else
01560 len = 2;
01561 i += len;
01562 if (i > 6)
01563 {
01564 fprintf (stderr, "\n\t");
01565 i = len;
01566 }
01567 print_node_brief (stderr, "", t, 0);
01568 if (t == error_mark_node)
01569 break;
01570 }
01571 if (i)
01572 fprintf (stderr, "\n");
01573 }
01574 if (VEC_length (cp_class_binding, lvl->class_shadowed))
01575 {
01576 size_t i;
01577 cp_class_binding *b;
01578 fprintf (stderr, " class-shadowed:");
01579 for (i = 0;
01580 VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
01581 ++i)
01582 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
01583 fprintf (stderr, "\n");
01584 }
01585 if (lvl->type_shadowed)
01586 {
01587 fprintf (stderr, " type-shadowed:");
01588 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
01589 {
01590 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
01591 }
01592 fprintf (stderr, "\n");
01593 }
01594 }
01595
01596 void
01597 print_other_binding_stack (struct cp_binding_level *stack)
01598 {
01599 struct cp_binding_level *level;
01600 for (level = stack; !global_scope_p (level); level = level->level_chain)
01601 {
01602 fprintf (stderr, "binding level %p\n", (void *) level);
01603 print_binding_level (level);
01604 }
01605 }
01606
01607 void
01608 print_binding_stack (void)
01609 {
01610 struct cp_binding_level *b;
01611 fprintf (stderr, "current_binding_level=%p\n"
01612 "class_binding_level=%p\n"
01613 "NAMESPACE_LEVEL (global_namespace)=%p\n",
01614 (void *) current_binding_level, (void *) class_binding_level,
01615 (void *) NAMESPACE_LEVEL (global_namespace));
01616 if (class_binding_level)
01617 {
01618 for (b = class_binding_level; b; b = b->level_chain)
01619 if (b == current_binding_level)
01620 break;
01621 if (b)
01622 b = class_binding_level;
01623 else
01624 b = current_binding_level;
01625 }
01626 else
01627 b = current_binding_level;
01628 print_other_binding_stack (b);
01629 fprintf (stderr, "global:\n");
01630 print_binding_level (NAMESPACE_LEVEL (global_namespace));
01631 }
01632
01633
01634
01635 tree
01636 identifier_type_value (tree id)
01637 {
01638 timevar_push (TV_NAME_LOOKUP);
01639
01640 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
01641 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
01642
01643 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
01644 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
01645
01646
01647 id = lookup_name_real (id, 2, 1, true, 0, LOOKUP_COMPLAIN);
01648 if (id)
01649 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
01650 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
01651 }
01652
01653
01654
01655
01656 tree
01657 identifier_global_value (tree t)
01658 {
01659 return IDENTIFIER_GLOBAL_VALUE (t);
01660 }
01661
01662
01663
01664
01665
01666 static void
01667 set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
01668 {
01669 tree type;
01670
01671 if (b->kind != sk_namespace)
01672 {
01673
01674
01675 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
01676 b->type_shadowed
01677 = tree_cons (id, old_type_value, b->type_shadowed);
01678 type = decl ? TREE_TYPE (decl) : NULL_TREE;
01679 TREE_TYPE (b->type_shadowed) = type;
01680 }
01681 else
01682 {
01683 cxx_binding *binding =
01684 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
01685 gcc_assert (decl);
01686 if (binding->value)
01687 supplement_binding (binding, decl);
01688 else
01689 binding->value = decl;
01690
01691
01692 type = global_type_node;
01693 }
01694 SET_IDENTIFIER_TYPE_VALUE (id, type);
01695 }
01696
01697
01698
01699
01700 void
01701 set_identifier_type_value (tree id, tree decl)
01702 {
01703 set_identifier_type_value_with_scope (id, decl, current_binding_level);
01704 }
01705
01706
01707
01708
01709
01710 static inline tree
01711 constructor_name_full (tree type)
01712 {
01713 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
01714 }
01715
01716
01717
01718
01719
01720 tree
01721 constructor_name (tree type)
01722 {
01723 tree name;
01724 name = constructor_name_full (type);
01725 if (IDENTIFIER_TEMPLATE (name))
01726 name = IDENTIFIER_TEMPLATE (name);
01727 return name;
01728 }
01729
01730
01731
01732 bool
01733 constructor_name_p (tree name, tree type)
01734 {
01735 tree ctor_name;
01736
01737 if (!name)
01738 return false;
01739
01740 if (TREE_CODE (name) != IDENTIFIER_NODE)
01741 return false;
01742
01743 ctor_name = constructor_name_full (type);
01744 if (name == ctor_name)
01745 return true;
01746 if (IDENTIFIER_TEMPLATE (ctor_name)
01747 && name == IDENTIFIER_TEMPLATE (ctor_name))
01748 return true;
01749 return false;
01750 }
01751
01752
01753
01754 static GTY(()) int anon_cnt;
01755
01756
01757
01758
01759 tree
01760 make_anon_name (void)
01761 {
01762 char buf[32];
01763
01764 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
01765 return get_identifier (buf);
01766 }
01767
01768
01769
01770 static inline cxx_binding *
01771 find_binding (cxx_scope *scope, cxx_binding *binding)
01772 {
01773 timevar_push (TV_NAME_LOOKUP);
01774
01775 for (; binding != NULL; binding = binding->previous)
01776 if (binding->scope == scope)
01777 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
01778
01779 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
01780 }
01781
01782
01783
01784 static inline cxx_binding *
01785 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
01786 {
01787 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
01788 if (b)
01789 {
01790
01791 if (scope == b->scope && b->previous == NULL)
01792 return b;
01793 return find_binding (scope, b);
01794 }
01795 return NULL;
01796 }
01797
01798
01799
01800
01801 static cxx_binding *
01802 binding_for_name (cxx_scope *scope, tree name)
01803 {
01804 cxx_binding *result;
01805
01806 result = cxx_scope_find_binding_for_name (scope, name);
01807 if (result)
01808 return result;
01809
01810 result = cxx_binding_make (NULL, NULL);
01811 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
01812 result->scope = scope;
01813 result->is_local = false;
01814 result->value_is_inherited = false;
01815 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
01816 return result;
01817 }
01818
01819
01820
01821
01822
01823
01824 static tree
01825 push_using_decl (tree scope, tree name)
01826 {
01827 tree decl;
01828
01829 timevar_push (TV_NAME_LOOKUP);
01830 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
01831 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
01832 for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
01833 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
01834 break;
01835 if (decl)
01836 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
01837 namespace_bindings_p () ? decl : NULL_TREE);
01838 decl = build_lang_decl (USING_DECL, name, NULL_TREE);
01839 USING_DECL_SCOPE (decl) = scope;
01840 TREE_CHAIN (decl) = current_binding_level->usings;
01841 current_binding_level->usings = decl;
01842 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
01843 }
01844
01845
01846
01847
01848 tree
01849 pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
01850 {
01851 struct cp_binding_level *b;
01852 tree function_decl = current_function_decl;
01853
01854 timevar_push (TV_NAME_LOOKUP);
01855 current_function_decl = NULL_TREE;
01856 if (level->kind == sk_class)
01857 {
01858 b = class_binding_level;
01859 class_binding_level = level;
01860 pushdecl_class_level (x);
01861 class_binding_level = b;
01862 }
01863 else
01864 {
01865 b = current_binding_level;
01866 current_binding_level = level;
01867 x = pushdecl_maybe_friend (x, is_friend);
01868 current_binding_level = b;
01869 }
01870 current_function_decl = function_decl;
01871 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
01872 }
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895 static tree
01896 push_overloaded_decl (tree decl, int flags, bool is_friend)
01897 {
01898 tree name = DECL_NAME (decl);
01899 tree old;
01900 tree new_binding;
01901 int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
01902
01903 timevar_push (TV_NAME_LOOKUP);
01904 if (doing_global)
01905 old = namespace_binding (name, DECL_CONTEXT (decl));
01906 else
01907 old = lookup_name_innermost_nonclass_level (name);
01908
01909 if (old)
01910 {
01911 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
01912 {
01913 tree t = TREE_TYPE (old);
01914 if (IS_AGGR_TYPE (t) && warn_shadow
01915 && (! DECL_IN_SYSTEM_HEADER (decl)
01916 || ! DECL_IN_SYSTEM_HEADER (old)))
01917 warning (0, "%q#D hides constructor for %q#T", decl, t);
01918 old = NULL_TREE;
01919 }
01920 else if (is_overloaded_fn (old))
01921 {
01922 tree tmp;
01923
01924 for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
01925 {
01926 tree fn = OVL_CURRENT (tmp);
01927 tree dup;
01928
01929 if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
01930 && !(flags & PUSH_USING)
01931 && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
01932 TYPE_ARG_TYPES (TREE_TYPE (decl)))
01933 && ! decls_match (fn, decl))
01934 error ("%q#D conflicts with previous using declaration %q#D",
01935 decl, fn);
01936
01937 dup = duplicate_decls (decl, fn, is_friend);
01938
01939
01940 if (dup == fn || dup == error_mark_node)
01941 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup);
01942 }
01943
01944
01945
01946
01947 if (TREE_CODE (old) == FUNCTION_DECL
01948 && DECL_ANTICIPATED (old)
01949 && !DECL_HIDDEN_FRIEND_P (old))
01950 old = NULL;
01951 }
01952 else if (old == error_mark_node)
01953
01954 old = NULL_TREE;
01955 else
01956 {
01957 error ("previous non-function declaration %q+#D", old);
01958 error ("conflicts with function declaration %q#D", decl);
01959 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
01960 }
01961 }
01962
01963 if (old || TREE_CODE (decl) == TEMPLATE_DECL
01964
01965
01966
01967 || (flags & PUSH_USING))
01968 {
01969 if (old && TREE_CODE (old) != OVERLOAD)
01970 new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
01971 else
01972 new_binding = ovl_cons (decl, old);
01973 if (flags & PUSH_USING)
01974 OVL_USED (new_binding) = 1;
01975 }
01976 else
01977
01978 new_binding = decl;
01979
01980 if (doing_global)
01981 set_namespace_binding (name, current_namespace, new_binding);
01982 else
01983 {
01984
01985
01986
01987
01988
01989
01990 if (TREE_CODE (new_binding) == OVERLOAD && old)
01991 {
01992 tree *d;
01993
01994 for (d = &IDENTIFIER_BINDING (name)->scope->names;
01995 *d;
01996 d = &TREE_CHAIN (*d))
01997 if (*d == old
01998 || (TREE_CODE (*d) == TREE_LIST
01999 && TREE_VALUE (*d) == old))
02000 {
02001 if (TREE_CODE (*d) == TREE_LIST)
02002
02003 TREE_VALUE (*d) = new_binding;
02004 else
02005
02006 *d = tree_cons (NULL_TREE, new_binding,
02007 TREE_CHAIN (*d));
02008
02009
02010 IDENTIFIER_BINDING (name)->value = new_binding;
02011 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
02012 }
02013
02014
02015 gcc_unreachable ();
02016 }
02017
02018
02019 push_local_binding (name, new_binding, flags);
02020 }
02021
02022 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
02023 }
02024
02025
02026
02027
02028 static tree
02029 validate_nonmember_using_decl (tree decl, tree scope, tree name)
02030 {
02031
02032
02033
02034 if (TYPE_P (scope))
02035 {
02036 error ("%qT is not a namespace", scope);
02037 return NULL_TREE;
02038 }
02039 else if (scope == error_mark_node)
02040 return NULL_TREE;
02041
02042 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
02043 {
02044
02045
02046 error ("a using-declaration cannot specify a template-id. "
02047 "Try %<using %D%>", name);
02048 return NULL_TREE;
02049 }
02050
02051 if (TREE_CODE (decl) == NAMESPACE_DECL)
02052 {
02053 error ("namespace %qD not allowed in using-declaration", decl);
02054 return NULL_TREE;
02055 }
02056
02057 if (TREE_CODE (decl) == SCOPE_REF)
02058 {
02059
02060
02061 error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
02062 return NULL_TREE;
02063 }
02064
02065 if (is_overloaded_fn (decl))
02066 decl = get_first_fn (decl);
02067
02068 gcc_assert (DECL_P (decl));
02069
02070
02071 return push_using_decl (scope, name);
02072 }
02073
02074
02075
02076 static void
02077 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
02078 tree *newval, tree *newtype)
02079 {
02080 struct scope_binding decls = EMPTY_SCOPE_BINDING;
02081
02082 *newval = *newtype = NULL_TREE;
02083 if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
02084
02085 return;
02086
02087 if (!decls.value && !decls.type)
02088 {
02089 error ("%qD not declared", name);
02090 return;
02091 }
02092
02093
02094
02095
02096 if (oldval
02097 && TREE_CODE (oldval) == FUNCTION_DECL
02098 && DECL_ANTICIPATED (oldval)
02099 && !DECL_HIDDEN_FRIEND_P (oldval))
02100 oldval = NULL_TREE;
02101
02102
02103 if (decls.value && is_overloaded_fn (decls.value))
02104 {
02105 tree tmp, tmp1;
02106
02107 if (oldval && !is_overloaded_fn (oldval))
02108 {
02109 if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
02110 error ("%qD is already declared in this scope", name);
02111 oldval = NULL_TREE;
02112 }
02113
02114 *newval = oldval;
02115 for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
02116 {
02117 tree new_fn = OVL_CURRENT (tmp);
02118
02119
02120
02121
02122
02123
02124
02125 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
02126 {
02127 tree old_fn = OVL_CURRENT (tmp1);
02128
02129 if (new_fn == old_fn)
02130
02131 break;
02132 else if (OVL_USED (tmp1))
02133 continue;
02134 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
02135 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
02136 {
02137 gcc_assert (!DECL_ANTICIPATED (old_fn)
02138 || DECL_HIDDEN_FRIEND_P (old_fn));
02139
02140
02141
02142
02143 if (decls_match (new_fn, old_fn))
02144 break;
02145 else
02146 {
02147 error ("%qD is already declared in this scope", name);
02148 break;
02149 }
02150 }
02151 }
02152
02153
02154
02155
02156 if (tmp1)
02157 continue;
02158
02159
02160
02161 if (*newval && TREE_CODE (*newval) == OVERLOAD)
02162 TREE_TYPE (*newval) = unknown_type_node;
02163
02164 *newval = build_overload (OVL_CURRENT (tmp), *newval);
02165
02166
02167
02168
02169 if (TREE_CODE (*newval) != OVERLOAD)
02170 {
02171 *newval = ovl_cons (*newval, NULL_TREE);
02172 TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
02173 }
02174 OVL_USED (*newval) = 1;
02175 }
02176 }
02177 else
02178 {
02179 *newval = decls.value;
02180 if (oldval && !decls_match (*newval, oldval))
02181 error ("%qD is already declared in this scope", name);
02182 }
02183
02184 *newtype = decls.type;
02185 if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
02186 {
02187 error ("using declaration %qD introduced ambiguous type %qT",
02188 name, oldtype);
02189 return;
02190 }
02191 }
02192
02193
02194
02195 void
02196 do_local_using_decl (tree decl, tree scope, tree name)
02197 {
02198 tree oldval, oldtype, newval, newtype;
02199 tree orig_decl = decl;
02200
02201 decl = validate_nonmember_using_decl (decl, scope, name);
02202 if (decl == NULL_TREE)
02203 return;
02204
02205 if (building_stmt_tree ()
02206 && at_function_scope_p ())
02207 add_decl_expr (decl);
02208
02209 oldval = lookup_name_innermost_nonclass_level (name);
02210 oldtype = lookup_type_current_level (name);
02211
02212 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
02213
02214 if (newval)
02215 {
02216 if (is_overloaded_fn (newval))
02217 {
02218 tree fn, term;
02219
02220
02221
02222
02223
02224 if (oldval && TREE_CODE (oldval) == OVERLOAD)
02225 term = OVL_FUNCTION (oldval);
02226 else
02227 term = oldval;
02228 for (fn = newval; fn && OVL_CURRENT (fn) != term;
02229 fn = OVL_NEXT (fn))
02230 push_overloaded_decl (OVL_CURRENT (fn),
02231 PUSH_LOCAL | PUSH_USING,
02232 false);
02233 }
02234 else
02235 push_local_binding (name, newval, PUSH_USING);
02236 }
02237 if (newtype)
02238 {
02239 push_local_binding (name, newtype, PUSH_USING);
02240 set_identifier_type_value (name, newtype);
02241 }
02242
02243
02244 if (!processing_template_decl)
02245 cp_emit_debug_info_for_using (orig_decl, current_scope());
02246 }
02247
02248
02249
02250
02251 bool
02252 is_ancestor (tree root, tree child)
02253 {
02254 gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
02255 || TREE_CODE (root) == FUNCTION_DECL
02256 || CLASS_TYPE_P (root)));
02257 gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
02258 || CLASS_TYPE_P (child)));
02259
02260
02261 if (root == global_namespace)
02262 return true;
02263
02264 while (true)
02265 {
02266
02267 if (!child)
02268 return false;
02269
02270 if (root == child)
02271 return true;
02272
02273 if (TYPE_P (child))
02274 child = TYPE_NAME (child);
02275 child = DECL_CONTEXT (child);
02276 }
02277 }
02278
02279
02280
02281
02282
02283
02284 tree
02285 push_scope (tree t)
02286 {
02287 if (TREE_CODE (t) == NAMESPACE_DECL)
02288 push_decl_namespace (t);
02289 else if (CLASS_TYPE_P (t))
02290 {
02291 if (!at_class_scope_p ()
02292 || !same_type_p (current_class_type, t))
02293 push_nested_class (t);
02294 else
02295
02296
02297
02298
02299 t = NULL_TREE;
02300 }
02301
02302 return t;
02303 }
02304
02305
02306
02307 void
02308 pop_scope (tree t)
02309 {
02310 if (TREE_CODE (t) == NAMESPACE_DECL)
02311 pop_decl_namespace ();
02312 else if CLASS_TYPE_P (t)
02313 pop_nested_class ();
02314 }
02315
02316
02317
02318 static void
02319 push_inner_scope_r (tree outer, tree inner)
02320 {
02321 tree prev;
02322
02323 if (outer == inner
02324 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
02325 return;
02326
02327 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
02328 if (outer != prev)
02329 push_inner_scope_r (outer, prev);
02330 if (TREE_CODE (inner) == NAMESPACE_DECL)
02331 {
02332 struct cp_binding_level *save_template_parm = 0;
02333
02334
02335 while (current_binding_level->kind == sk_template_parms)
02336 {
02337 struct cp_binding_level *b = current_binding_level;
02338 current_binding_level = b->level_chain;
02339 b->level_chain = save_template_parm;
02340 save_template_parm = b;
02341 }
02342
02343 resume_scope (NAMESPACE_LEVEL (inner));
02344 current_namespace = inner;
02345
02346
02347 while (save_template_parm)
02348 {
02349 struct cp_binding_level *b = save_template_parm;
02350 save_template_parm = b->level_chain;
02351 b->level_chain = current_binding_level;
02352 current_binding_level = b;
02353 }
02354 }
02355 else
02356 pushclass (inner);
02357 }
02358
02359
02360
02361
02362
02363
02364
02365
02366
02367 tree
02368 push_inner_scope (tree inner)
02369 {
02370 tree outer = current_scope ();
02371 if (!outer)
02372 outer = current_namespace;
02373
02374 push_inner_scope_r (outer, inner);
02375 return outer;
02376 }
02377
02378
02379
02380 void
02381 pop_inner_scope (tree outer, tree inner)
02382 {
02383 if (outer == inner
02384 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
02385 return;
02386
02387 while (outer != inner)
02388 {
02389 if (TREE_CODE (inner) == NAMESPACE_DECL)
02390 {
02391 struct cp_binding_level *save_template_parm = 0;
02392
02393
02394 while (current_binding_level->kind == sk_template_parms)
02395 {
02396 struct cp_binding_level *b = current_binding_level;
02397 current_binding_level = b->level_chain;
02398 b->level_chain = save_template_parm;
02399 save_template_parm = b;
02400 }
02401
02402 pop_namespace ();
02403
02404
02405 while (save_template_parm)
02406 {
02407 struct cp_binding_level *b = save_template_parm;
02408 save_template_parm = b->level_chain;
02409 b->level_chain = current_binding_level;
02410 current_binding_level = b;
02411 }
02412 }
02413 else
02414 popclass ();
02415
02416 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
02417 }
02418 }
02419
02420
02421
02422 void
02423 pushlevel_class (void)
02424 {
02425 if (ENABLE_SCOPE_CHECKING)
02426 is_class_level = 1;
02427
02428 class_binding_level = begin_scope (sk_class, current_class_type);
02429 }
02430
02431
02432
02433 void
02434 poplevel_class (void)
02435 {
02436 struct cp_binding_level *level = class_binding_level;
02437 cp_class_binding *cb;
02438 size_t i;
02439 tree shadowed;
02440
02441 timevar_push (TV_NAME_LOOKUP);
02442 gcc_assert (level != 0);
02443
02444
02445 if (current_class_depth == 1)
02446 previous_class_level = level;
02447 for (shadowed = level->type_shadowed;
02448 shadowed;
02449 shadowed = TREE_CHAIN (shadowed))
02450 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
02451
02452
02453 if (level->class_shadowed)
02454 {
02455 for (i = 0;
02456 VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
02457 ++i)
02458 IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
02459 ggc_free (level->class_shadowed);
02460 level->class_shadowed = NULL;
02461 }
02462
02463
02464
02465 if (ENABLE_SCOPE_CHECKING)
02466 is_class_level = 1;
02467
02468 leave_scope ();
02469 timevar_pop (TV_NAME_LOOKUP);
02470 }
02471
02472
02473
02474
02475
02476 static void
02477 set_inherited_value_binding_p (cxx_binding *binding, tree decl,
02478 tree class_type)
02479 {
02480 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
02481 {
02482 tree context;
02483
02484 if (TREE_CODE (decl) == OVERLOAD)
02485 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
02486 else
02487 {
02488 gcc_assert (DECL_P (decl));
02489 context = context_for_name_lookup (decl);
02490 }
02491
02492 if (is_properly_derived_from (class_type, context))
02493 INHERITED_VALUE_BINDING_P (binding) = 1;
02494 else
02495 INHERITED_VALUE_BINDING_P (binding) = 0;
02496 }
02497 else if (binding->value == decl)
02498
02499
02500
02501 INHERITED_VALUE_BINDING_P (binding) = 1;
02502 else
02503 INHERITED_VALUE_BINDING_P (binding) = 0;
02504 }
02505
02506
02507
02508 bool
02509 pushdecl_class_level (tree x)
02510 {
02511 tree name;
02512 bool is_valid = true;
02513
02514 timevar_push (TV_NAME_LOOKUP);
02515
02516 if (TREE_CODE (x) == OVERLOAD)
02517 name = DECL_NAME (get_first_fn (x));
02518 else
02519 name = DECL_NAME (x);
02520
02521 if (name)
02522 {
02523 is_valid = push_class_level_binding (name, x);
02524 if (TREE_CODE (x) == TYPE_DECL)
02525 set_identifier_type_value (name, x);
02526 }
02527 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
02528 {
02529
02530
02531
02532 tree f;
02533
02534 for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
02535 {
02536 location_t save_location = input_location;
02537 input_location = DECL_SOURCE_LOCATION (f);
02538 if (!pushdecl_class_level (f))
02539 is_valid = false;
02540 input_location = save_location;
02541 }
02542 }
02543 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
02544 }
02545
02546
02547
02548
02549
02550 static cxx_binding *
02551 get_class_binding (tree name, cxx_scope *scope)
02552 {
02553 tree class_type;
02554 tree type_binding;
02555 tree value_binding;
02556 cxx_binding *binding;
02557
02558 class_type = scope->this_entity;
02559
02560
02561 type_binding = lookup_member (class_type, name,
02562 2, true);
02563
02564 value_binding = lookup_member (class_type, name,
02565 2, false);
02566
02567 if (value_binding
02568 && (TREE_CODE (value_binding) == TYPE_DECL
02569 || DECL_CLASS_TEMPLATE_P (value_binding)
02570 || (TREE_CODE (value_binding) == TREE_LIST
02571 && TREE_TYPE (value_binding) == error_mark_node
02572 && (TREE_CODE (TREE_VALUE (value_binding))
02573 == TYPE_DECL))))
02574
02575
02576
02577 ;
02578 else if (value_binding)
02579 {
02580 if (TREE_CODE (value_binding) == TREE_LIST
02581 && TREE_TYPE (value_binding) == error_mark_node)
02582
02583 ;
02584 else if (BASELINK_P (value_binding))
02585
02586 value_binding = BASELINK_FUNCTIONS (value_binding);
02587 }
02588
02589
02590
02591 if (type_binding || value_binding)
02592 {
02593 binding = new_class_binding (name,
02594 value_binding,
02595 type_binding,
02596 scope);
02597
02598 LOCAL_BINDING_P (binding) = 0;
02599 set_inherited_value_binding_p (binding, value_binding, class_type);
02600 }
02601 else
02602 binding = NULL;
02603
02604 return binding;
02605 }
02606
02607
02608
02609
02610 bool
02611 push_class_level_binding (tree name, tree x)
02612 {
02613 cxx_binding *binding;
02614 tree decl = x;
02615 bool ok;
02616
02617 timevar_push (TV_NAME_LOOKUP);
02618
02619
02620 if (!class_binding_level)
02621 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
02622
02623 if (name == error_mark_node)
02624 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
02625
02626
02627 gcc_assert (TYPE_BEING_DEFINED (current_class_type));
02628
02629
02630
02631 if (TREE_CODE (decl) == TREE_LIST
02632 && TREE_TYPE (decl) == error_mark_node)
02633 decl = TREE_VALUE (decl);
02634
02635 check_template_shadow (decl);
02636
02637
02638
02639
02640
02641
02642
02643
02644
02645
02646
02647
02648
02649
02650
02651
02652
02653
02654 if ((TREE_CODE (x) == VAR_DECL
02655 || TREE_CODE (x) == CONST_DECL
02656 || (TREE_CODE (x) == TYPE_DECL
02657 && !DECL_SELF_REFERENCE_P (x))
02658
02659 || (TREE_CODE (x) == FIELD_DECL
02660 && DECL_CONTEXT (x) != current_class_type))
02661 && DECL_NAME (x) == constructor_name (current_class_type))
02662 {
02663 tree scope = context_for_name_lookup (x);
02664 if (TYPE_P (scope) && same_type_p (scope, current_class_type))
02665 {
02666 error ("%qD has the same name as the class in which it is "
02667 "declared",
02668 x);
02669 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
02670 }
02671 }
02672
02673
02674 binding = IDENTIFIER_BINDING (name);
02675 if (!binding || binding->scope != class_binding_level)
02676 {
02677 binding = get_class_binding (name, class_binding_level);
02678
02679
02680 if (binding)
02681 {
02682 binding->previous = IDENTIFIER_BINDING (name);
02683 IDENTIFIER_BINDING (name) = binding;
02684 }
02685 }
02686
02687
02688
02689 if (binding && binding->value)
02690 {
02691 tree bval = binding->value;
02692 tree old_decl = NULL_TREE;
02693
02694 if (INHERITED_VALUE_BINDING_P (binding))
02695 {
02696
02697
02698
02699
02700 if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
02701 && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
02702 {
02703 old_decl = binding->type;
02704 binding->type = bval;
02705 binding->value = NULL_TREE;
02706 INHERITED_VALUE_BINDING_P (binding) = 0;
02707 }
02708 else
02709 {
02710 old_decl = bval;
02711
02712
02713 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
02714 binding->type = NULL_TREE;
02715 }
02716 }
02717 else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
02718 old_decl = bval;
02719 else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
02720 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
02721 else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
02722 old_decl = bval;
02723 else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
02724 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
02725
02726 if (old_decl && binding->scope == class_binding_level)
02727 {
02728 binding->value = x;
02729
02730
02731
02732 INHERITED_VALUE_BINDING_P (binding) = 0;
02733 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
02734 }
02735 }
02736
02737
02738
02739
02740 note_name_declared_in_class (name, decl);
02741
02742
02743
02744
02745 if (binding && binding->scope == class_binding_level)
02746
02747 ok = supplement_binding (binding, decl);
02748 else
02749 {
02750
02751 push_binding (name, decl, class_binding_level);
02752 ok = true;
02753 }
02754
02755 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
02756 }
02757
02758
02759
02760
02761 tree
02762 do_class_using_decl (tree scope, tree name)
02763 {
02764
02765 tree value;
02766
02767
02768
02769 tree decl;
02770
02771 bool scope_dependent_p;
02772
02773 bool name_dependent_p;
02774
02775 bool bases_dependent_p;
02776 tree binfo;
02777 tree base_binfo;
02778 int i;
02779
02780 if (name == error_mark_node)
02781 return NULL_TREE;
02782
02783 if (!scope || !TYPE_P (scope))
02784 {
02785 error ("using-declaration for non-member at class scope");
02786 return NULL_TREE;
02787 }
02788
02789
02790 if (TREE_CODE (name) == BIT_NOT_EXPR)
02791 {
02792 error ("%<%T::%D%> names destructor", scope, name);
02793 return NULL_TREE;
02794 }
02795 if (constructor_name_p (name, scope))
02796 {
02797 error ("%<%T::%D%> names constructor", scope, name);
02798 return NULL_TREE;
02799 }
02800 if (constructor_name_p (name, current_class_type))
02801 {
02802 error ("%<%T::%D%> names constructor in %qT",
02803 scope, name, current_class_type);
02804 return NULL_TREE;
02805 }
02806
02807 scope_dependent_p = dependent_type_p (scope);
02808 name_dependent_p = (scope_dependent_p
02809 || (IDENTIFIER_TYPENAME_P (name)
02810 && dependent_type_p (TREE_TYPE (name))));
02811
02812 bases_dependent_p = false;
02813 if (processing_template_decl)
02814 for (binfo = TYPE_BINFO (current_class_type), i = 0;
02815 BINFO_BASE_ITERATE (binfo, i, base_binfo);
02816 i++)
02817 if (dependent_type_p (TREE_TYPE (base_binfo)))
02818 {
02819 bases_dependent_p = true;
02820 break;
02821 }
02822
02823 decl = NULL_TREE;
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835 if (!scope_dependent_p)
02836 {
02837 base_kind b_kind;
02838 binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
02839 if (b_kind < bk_proper_base)
02840 {
02841 if (!bases_dependent_p)
02842 {
02843 error_not_base_type (scope, current_class_type);
02844 return NULL_TREE;
02845 }
02846 }
02847 else if (!name_dependent_p)
02848 {
02849 decl = lookup_member (binfo, name, 0, false);
02850 if (!decl)
02851 {
02852 error ("no members matching %<%T::%D%> in %q#T", scope, name,
02853 scope);
02854 return NULL_TREE;
02855 }
02856
02857 if (BASELINK_P (decl))
02858 decl = BASELINK_FUNCTIONS (decl);
02859 }
02860 }
02861
02862 value = build_lang_decl (USING_DECL, name, NULL_TREE);
02863 USING_DECL_DECLS (value) = decl;
02864 USING_DECL_SCOPE (value) = scope;
02865 DECL_DEPENDENT_P (value) = !decl;
02866
02867 return value;
02868 }
02869
02870
02871
02872 #ifdef KEY
02873
02874 tree (*p_namespace_binding) (tree, tree) = namespace_binding;
02875 #endif
02876
02877
02878
02879 tree
02880 namespace_binding (tree name, tree scope)
02881 {
02882 cxx_binding *binding;
02883
02884 if (scope == NULL)
02885 scope = global_namespace;
02886 else
02887
02888 scope = ORIGINAL_NAMESPACE (scope);
02889
02890 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
02891
02892 return binding ? binding->value : NULL_TREE;
02893 }
02894
02895
02896
02897 void
02898 set_namespace_binding (tree name, tree scope, tree val)
02899 {
02900 cxx_binding *b;
02901
02902 timevar_push (TV_NAME_LOOKUP);
02903 if (scope == NULL_TREE)
02904 scope = global_namespace;
02905 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
02906 if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
02907 b->value = val;
02908 else
02909 supplement_binding (b, val);
02910 timevar_pop (TV_NAME_LOOKUP);
02911 }
02912
02913
02914
02915
02916 void
02917 set_decl_namespace (tree decl, tree scope, bool friendp)
02918 {
02919 tree old, fn;
02920
02921
02922 scope = ORIGINAL_NAMESPACE (scope);
02923
02924
02925 if (!friendp && !is_ancestor (current_namespace, scope))
02926 error ("declaration of %qD not in a namespace surrounding %qD",
02927 decl, scope);
02928 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
02929
02930
02931 if (scope == current_namespace)
02932 {
02933 if (at_namespace_scope_p ())
02934 error ("explicit qualification in declaration of %qD",
02935 decl);
02936 return;
02937 }
02938
02939
02940 old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
02941 if (!old)
02942
02943 goto complain;
02944 if (!is_overloaded_fn (decl))
02945
02946
02947
02948 return;
02949
02950 if (!is_overloaded_fn (old))
02951 goto complain;
02952 fn = OVL_CURRENT (old);
02953 if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
02954 goto complain;
02955
02956 if (processing_explicit_instantiation)
02957 return;
02958 if (processing_template_decl || processing_specialization)
02959
02960
02961
02962
02963 return;
02964
02965
02966 if (friendp && DECL_USE_TEMPLATE (decl))
02967 return;
02968 if (is_overloaded_fn (old))
02969 {
02970 for (; old; old = OVL_NEXT (old))
02971 if (decls_match (decl, OVL_CURRENT (old)))
02972 return;
02973 }
02974 else if (decls_match (decl, old))
02975 return;
02976 complain:
02977 error ("%qD should have been declared inside %qD", decl, scope);
02978 }
02979
02980
02981
02982 static tree
02983 current_decl_namespace (void)
02984 {
02985 tree result;
02986
02987 if (decl_namespace_list)
02988 return TREE_PURPOSE (decl_namespace_list);
02989
02990 if (current_class_type)
02991 result = decl_namespace_context (current_class_type);
02992 else if (current_function_decl)
02993 result = decl_namespace_context (current_function_decl);
02994 else
02995 result = current_namespace;
02996 return result;
02997 }
02998
02999
03000
03001
03002 void
03003 push_namespace (tree name)
03004 {
03005 push_namespace_with_attribs (name, NULL_TREE);
03006 }
03007
03008
03009
03010
03011 void
03012 push_namespace_with_attribs (tree name, tree attributes)
03013 {
03014 tree d = NULL_TREE;
03015 int need_new = 1;
03016 int implicit_use = 0;
03017 bool anon = !name;
03018
03019 timevar_push (TV_NAME_LOOKUP);
03020
03021
03022
03023
03024 gcc_assert (global_namespace != NULL && name != global_scope_name);
03025
03026 if (anon)
03027 {
03028
03029
03030 if (!anonymous_namespace_name)
03031 anonymous_namespace_name = get_file_function_name ('N');
03032 name = anonymous_namespace_name;
03033 d = IDENTIFIER_NAMESPACE_VALUE (name);
03034 if (d)
03035
03036 need_new = 0;
03037 implicit_use = 1;
03038 }
03039 else
03040 {
03041
03042 d = IDENTIFIER_NAMESPACE_VALUE (name);
03043 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
03044 {
03045 need_new = 0;
03046 if (DECL_NAMESPACE_ALIAS (d))
03047 {
03048 error ("namespace alias %qD not allowed here, assuming %qD",
03049 d, DECL_NAMESPACE_ALIAS (d));
03050 d = DECL_NAMESPACE_ALIAS (d);
03051 }
03052 }
03053 }
03054
03055 if (need_new)
03056 {
03057
03058 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
03059 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
03060
03061
03062 if (anon || decl_anon_ns_mem_p (current_namespace))
03063 TREE_PUBLIC (d) = 0;
03064 else
03065 TREE_PUBLIC (d) = 1;
03066 pushdecl (d);
03067 if (anon)
03068 {
03069
03070 SET_DECL_ASSEMBLER_NAME (d, name);
03071 DECL_NAME (d) = NULL_TREE;
03072 }
03073 begin_scope (sk_namespace, d);
03074
03075 #ifdef KEY
03076 if (flag_spin_file)
03077 gspin_gxx_emits_decl (d);
03078 #endif
03079 }
03080 else
03081 resume_scope (NAMESPACE_LEVEL (d));
03082
03083 if (implicit_use)
03084 do_using_directive (d);
03085
03086 current_namespace = d;
03087
03088 #ifdef HANDLE_PRAGMA_VISIBILITY
03089
03090
03091 current_binding_level->has_visibility = 0;
03092 for (d = attributes; d; d = TREE_CHAIN (d))
03093 {
03094 tree name = TREE_PURPOSE (d);
03095 tree args = TREE_VALUE (d);
03096 tree x;
03097
03098 if (! is_attribute_p ("visibility", name))
03099 {
03100 warning (OPT_Wattributes, "%qs attribute directive ignored",
03101 IDENTIFIER_POINTER (name));
03102 continue;
03103 }
03104
03105 x = args ? TREE_VALUE (args) : NULL_TREE;
03106 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
03107 {
03108 warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
03109 IDENTIFIER_POINTER (name));
03110 continue;
03111 }
03112
03113 current_binding_level->has_visibility = 1;
03114 push_visibility (TREE_STRING_POINTER (x));
03115 goto found;
03116 }
03117 found:
03118 #endif
03119
03120 timevar_pop (TV_NAME_LOOKUP);
03121 }
03122
03123
03124
03125 void
03126 pop_namespace (void)
03127 {
03128 gcc_assert (current_namespace != global_namespace);
03129 current_namespace = CP_DECL_CONTEXT (current_namespace);
03130
03131 leave_scope ();
03132 }
03133
03134
03135
03136
03137 void
03138 push_nested_namespace (tree ns)
03139 {
03140 if (ns == global_namespace)
03141 push_to_top_level ();
03142 else
03143 {
03144 push_nested_namespace (CP_DECL_CONTEXT (ns));
03145 push_namespace (DECL_NAME (ns));
03146 }
03147 }
03148
03149
03150
03151
03152 void
03153 pop_nested_namespace (tree ns)
03154 {
03155 timevar_push (TV_NAME_LOOKUP);
03156 while (ns != global_namespace)
03157 {
03158 pop_namespace ();
03159 ns = CP_DECL_CONTEXT (ns);
03160 }
03161
03162 pop_from_top_level ();
03163 timevar_pop (TV_NAME_LOOKUP);
03164 }
03165
03166
03167
03168 void
03169 push_decl_namespace (tree decl)
03170 {
03171 if (TREE_CODE (decl) != NAMESPACE_DECL)
03172 decl = decl_namespace_context (decl);
03173 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
03174 NULL_TREE, decl_namespace_list);
03175 }
03176
03177
03178
03179 void
03180 pop_decl_namespace (void)
03181 {
03182 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
03183 }
03184
03185
03186
03187
03188 static tree
03189 namespace_ancestor (tree ns1, tree ns2)
03190 {
03191 timevar_push (TV_NAME_LOOKUP);
03192 if (is_ancestor (ns1, ns2))
03193 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
03194 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
03195 namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
03196 }
03197
03198
03199
03200 void
03201 do_namespace_alias (tree alias, tree namespace)
03202 {
03203 if (namespace == error_mark_node)
03204 return;
03205
03206 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
03207
03208 namespace = ORIGINAL_NAMESPACE (namespace);
03209
03210
03211 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
03212 DECL_NAMESPACE_ALIAS (alias) = namespace;
03213 DECL_EXTERNAL (alias) = 1;
03214 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
03215 pushdecl (alias);
03216
03217
03218 (*debug_hooks->global_decl) (alias);
03219 }
03220
03221
03222
03223
03224 tree
03225 pushdecl_namespace_level (tree x, bool is_friend)
03226 {
03227 struct cp_binding_level *b = current_binding_level;
03228 tree t;
03229
03230 timevar_push (TV_NAME_LOOKUP);
03231 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
03232
03233
03234
03235 if (TREE_CODE (t) == TYPE_DECL)
03236 {
03237 tree name = DECL_NAME (t);
03238 tree newval;
03239 tree *ptr = (tree *)0;
03240 for (; !global_scope_p (b); b = b->level_chain)
03241 {
03242 tree shadowed = b->type_shadowed;
03243 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
03244 if (TREE_PURPOSE (shadowed) == name)
03245 {
03246 ptr = &TREE_VALUE (shadowed);
03247
03248
03249
03250 }
03251 }
03252 newval = TREE_TYPE (t);
03253 if (ptr == (tree *)0)
03254 {
03255
03256
03257 SET_IDENTIFIER_TYPE_VALUE (name, t);
03258 }
03259 else
03260 {
03261 *ptr = newval;
03262 }
03263 }
03264 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
03265 }
03266
03267
03268
03269
03270 static void
03271 add_using_namespace (tree user, tree used, bool indirect)
03272 {
03273 tree t;
03274 timevar_push (TV_NAME_LOOKUP);
03275
03276 if (user == used)
03277 {
03278 timevar_pop (TV_NAME_LOOKUP);
03279 return;
03280 }
03281 gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
03282 gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
03283
03284 t = purpose_member (used, DECL_NAMESPACE_USING (user));
03285 if (t != NULL_TREE)
03286 {
03287 if (!indirect)
03288
03289 TREE_INDIRECT_USING (t) = 0;
03290 timevar_pop (TV_NAME_LOOKUP);
03291 return;
03292 }
03293
03294
03295 DECL_NAMESPACE_USING (user)
03296 = tree_cons (used, namespace_ancestor (user, used),
03297 DECL_NAMESPACE_USING (user));
03298
03299 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
03300
03301
03302 DECL_NAMESPACE_USERS (used)
03303 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
03304
03305
03306 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
03307
03308 add_using_namespace (user, TREE_PURPOSE (t), 1);
03309
03310
03311 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
03312 add_using_namespace (TREE_PURPOSE (t), used, 1);
03313 timevar_pop (TV_NAME_LOOKUP);
03314 }
03315
03316
03317
03318 void
03319 do_toplevel_using_decl (tree decl, tree scope, tree name)
03320 {
03321 tree oldval, oldtype, newval, newtype;
03322 tree orig_decl = decl;
03323 cxx_binding *binding;
03324
03325 decl = validate_nonmember_using_decl (decl, scope, name);
03326 if (decl == NULL_TREE)
03327 return;
03328
03329 binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
03330
03331 oldval = binding->value;
03332 oldtype = binding->type;
03333
03334 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
03335
03336
03337 if (!processing_template_decl)
03338 cp_emit_debug_info_for_using (orig_decl, current_namespace);
03339
03340
03341 if (newval)
03342 binding->value = newval;
03343 if (newtype)
03344 binding->type = newtype;
03345 }
03346
03347
03348
03349 void
03350 do_using_directive (tree namespace)
03351 {
03352 tree context = NULL_TREE;
03353
03354 if (namespace == error_mark_node)
03355 return;
03356
03357 gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
03358
03359 if (building_stmt_tree ())
03360 add_stmt (build_stmt (USING_STMT, namespace));
03361 namespace = ORIGINAL_NAMESPACE (namespace);
03362
03363 if (!toplevel_bindings_p ())
03364 {
03365 push_using_directive (namespace);
03366 context = current_scope ();
03367 }
03368 else
03369 {
03370
03371 add_using_namespace (current_namespace, namespace, 0);
03372 if (current_namespace != global_namespace)
03373 context = current_namespace;
03374 }
03375
03376
03377 if (!processing_template_decl)
03378 (*debug_hooks->imported_module_or_decl) (namespace, context);
03379 }
03380
03381
03382
03383
03384 void
03385 parse_using_directive (tree namespace, tree attribs)
03386 {
03387 tree a;
03388
03389 do_using_directive (namespace);
03390
03391 for (a = attribs; a; a = TREE_CHAIN (a))
03392 {
03393 tree name = TREE_PURPOSE (a);
03394 if (is_attribute_p ("strong", name))
03395 {
03396 if (!toplevel_bindings_p ())
03397 error ("strong using only meaningful at namespace scope");
03398 else if (namespace != error_mark_node)
03399 {
03400 if (!is_ancestor (current_namespace, namespace))
03401 error ("current namespace %qD does not enclose strongly used namespace %qD",
03402 current_namespace, namespace);
03403 DECL_NAMESPACE_ASSOCIATIONS (namespace)
03404 = tree_cons (current_namespace, 0,
03405 DECL_NAMESPACE_ASSOCIATIONS (namespace));
03406 }
03407 }
03408 else
03409 warning (OPT_Wattributes, "%qD attribute directive ignored", name);
03410 }
03411 }
03412
03413
03414
03415
03416
03417 static tree
03418 pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
03419 {
03420 timevar_push (TV_NAME_LOOKUP);
03421 push_to_top_level ();
03422 x = pushdecl_namespace_level (x, is_friend);
03423 if (init)
03424 finish_decl (x, *init, NULL_TREE);
03425 pop_from_top_level ();
03426 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
03427 }
03428
03429
03430
03431 tree
03432 pushdecl_top_level (tree x)
03433 {
03434 return pushdecl_top_level_1 (x, NULL, false);
03435 }
03436
03437
03438
03439 tree
03440 pushdecl_top_level_maybe_friend (tree x, bool is_friend)
03441 {
03442 return pushdecl_top_level_1 (x, NULL, is_friend);
03443 }
03444
03445
03446
03447
03448
03449 tree
03450 pushdecl_top_level_and_finish (tree x, tree init)
03451 {
03452 return pushdecl_top_level_1 (x, &init, false);
03453 }
03454
03455
03456
03457
03458
03459
03460
03461
03462 static tree
03463 merge_functions (tree s1, tree s2)
03464 {
03465 for (; s2; s2 = OVL_NEXT (s2))
03466 {
03467 tree fn2 = OVL_CURRENT (s2);
03468 tree fns1;
03469
03470 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
03471 {
03472 tree fn1 = OVL_CURRENT (fns1);
03473
03474
03475
03476
03477
03478 if (fn1 == fn2
03479 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
03480 && DECL_NAME (fn1) == DECL_NAME (fn2)))
03481 break;
03482 }
03483
03484
03485 if (!fns1)
03486 s1 = build_overload (fn2, s1);
03487 }
03488 return s1;
03489 }
03490
03491
03492
03493
03494
03495
03496
03497
03498
03499 static void
03500 ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
03501 int flags)
03502 {
03503 tree val, type;
03504 gcc_assert (old != NULL);
03505
03506 val = new->value;
03507 if (val)
03508 switch (TREE_CODE (val))
03509 {
03510 case TEMPLATE_DECL:
03511
03512
03513 if ((LOOKUP_QUALIFIERS_ONLY (flags)
03514 && !DECL_CLASS_TEMPLATE_P (val))
03515 || hidden_name_p (val))
03516 val = NULL_TREE;
03517 break;
03518 case TYPE_DECL:
03519 if (LOOKUP_NAMESPACES_ONLY (flags) || hidden_name_p (val))
03520 val = NULL_TREE;
03521 break;
03522 case NAMESPACE_DECL:
03523 if (LOOKUP_TYPES_ONLY (flags))
03524 val = NULL_TREE;
03525 break;
03526 case FUNCTION_DECL:
03527
03528 if (LOOKUP_QUALIFIERS_ONLY (flags) || hidden_name_p (val))
03529 val = NULL_TREE;
03530 break;
03531 default:
03532 if (LOOKUP_QUALIFIERS_ONLY (flags))
03533 val = NULL_TREE;
03534 }
03535
03536 if (!old->value)
03537 old->value = val;
03538 else if (val && val != old->value)
03539 {
03540 if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
03541 old->value = merge_functions (old->value, val);
03542 else
03543 {
03544 old->value = tree_cons (NULL_TREE, old->value,
03545 build_tree_list (NULL_TREE, new->value));
03546 TREE_TYPE (old->value) = error_mark_node;
03547 }
03548 }
03549
03550 type = new->type;
03551 if (LOOKUP_NAMESPACES_ONLY (flags))
03552 type = NULL_TREE;
03553 if (!old->type)
03554 old->type = type;
03555 else if (type && old->type != type)
03556 {
03557 if (flags & LOOKUP_COMPLAIN)
03558 {
03559 error ("%qD denotes an ambiguous type",name);
03560 error ("%J first type here", TYPE_MAIN_DECL (old->type));
03561 error ("%J other type here", TYPE_MAIN_DECL (type));
03562 }
03563 }
03564 }
03565
03566
03567
03568 tree
03569 cp_namespace_decls (tree ns)
03570 {
03571 return NAMESPACE_LEVEL (ns)->names;
03572 }
03573
03574
03575
03576 static int
03577 lookup_flags (int prefer_type, int namespaces_only)
03578 {
03579 if (namespaces_only)
03580 return LOOKUP_PREFER_NAMESPACES;
03581 if (prefer_type > 1)
03582 return LOOKUP_PREFER_TYPES;
03583 if (prefer_type > 0)
03584 return LOOKUP_PREFER_BOTH;
03585 return 0;
03586 }
03587
03588
03589
03590
03591
03592 static bool
03593 qualify_lookup (tree val, int flags)
03594 {
03595 if (val == NULL_TREE)
03596 return false;
03597 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
03598 return true;
03599 if ((flags & LOOKUP_PREFER_TYPES)
03600 && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
03601 return true;
03602 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
03603 return false;
03604 return true;
03605 }
03606
03607
03608
03609
03610 bool
03611 hidden_name_p (tree val)
03612 {
03613 if (DECL_P (val)
03614 && DECL_LANG_SPECIFIC (val)
03615 && DECL_ANTICIPATED (val))
03616 return true;
03617 return false;
03618 }
03619
03620
03621
03622
03623 tree
03624 remove_hidden_names (tree fns)
03625 {
03626 if (!fns)
03627 return fns;
03628
03629 if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
03630 fns = NULL_TREE;
03631 else if (TREE_CODE (fns) == OVERLOAD)
03632 {
03633 tree o;
03634
03635 for (o = fns; o; o = OVL_NEXT (o))
03636 if (hidden_name_p (OVL_CURRENT (o)))
03637 break;
03638 if (o)
03639 {
03640 tree n = NULL_TREE;
03641
03642 for (o = fns; o; o = OVL_NEXT (o))
03643 if (!hidden_name_p (OVL_CURRENT (o)))
03644 n = build_overload (OVL_CURRENT (o), n);
03645 fns = n;
03646 }
03647 }
03648
03649 return fns;
03650 }
03651
03652
03653
03654 static tree
03655 select_decl (const struct scope_binding *binding, int flags)
03656 {
03657 tree val;
03658 val = binding->value;
03659
03660 timevar_push (TV_NAME_LOOKUP);
03661 if (LOOKUP_NAMESPACES_ONLY (flags))
03662 {
03663
03664 if (val && (TREE_CODE (val) == NAMESPACE_DECL
03665 || TREE_CODE (val) == TREE_LIST))
03666 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
03667 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
03668 }
03669
03670
03671
03672 if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
03673 val = binding->type;
03674
03675 else if (val && LOOKUP_TYPES_ONLY (flags)
03676 && ! DECL_DECLARES_TYPE_P (val))
03677 val = NULL_TREE;
03678
03679 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
03680 }
03681
03682
03683
03684
03685 static tree
03686 unqualified_namespace_lookup (tree name, int flags)
03687 {
03688 tree initial = current_decl_namespace ();
03689 tree scope = initial;
03690 tree siter;
03691 struct cp_binding_level *level;
03692 tree val = NULL_TREE;
03693 struct scope_binding binding = EMPTY_SCOPE_BINDING;
03694
03695 timevar_push (TV_NAME_LOOKUP);
03696
03697 for (; !val; scope = CP_DECL_CONTEXT (scope))
03698 {
03699 cxx_binding *b =
03700 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
03701
03702 if (b)
03703 {
03704 if (b->value
03705 && ((flags & LOOKUP_HIDDEN) || !hidden_name_p (b->value)))
03706 binding.value = b->value;
03707 binding.type = b->type;
03708 }
03709
03710
03711 for (level = current_binding_level;
03712 level->kind != sk_namespace;
03713 level = level->level_chain)
03714 if (!lookup_using_namespace (name, &binding, level->using_directives,
03715 scope, flags))
03716
03717 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
03718
03719
03720
03721 siter = initial;
03722 while (1)
03723 {
03724 if (!lookup_using_namespace (name, &binding,
03725 DECL_NAMESPACE_USING (siter),
03726 scope, flags))
03727
03728 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
03729 if (siter == scope) break;
03730 siter = CP_DECL_CONTEXT (siter);
03731 }
03732
03733 val = select_decl (&binding, flags);
03734 if (scope == global_namespace)
03735 break;
03736 }
03737 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
03738 }
03739
03740
03741
03742
03743
03744
03745
03746
03747
03748
03749 tree
03750 lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
03751 {
03752 int flags = 0;
03753 tree t = NULL_TREE;
03754
03755 if (TREE_CODE (scope) == NAMESPACE_DECL)
03756 {
03757 struct scope_binding binding = EMPTY_SCOPE_BINDING;
03758
03759 flags |= LOOKUP_COMPLAIN;
03760 if (is_type_p)
03761 flags |= LOOKUP_PREFER_TYPES;
03762 if (qualified_lookup_using_namespace (name, scope, &binding, flags))
03763 t = select_decl (&binding, flags);
03764 }
03765 else if (is_aggr_type (scope, complain))
03766 t = lookup_member (scope, name, 2, is_type_p);
03767
03768 if (!t)
03769 return error_mark_node;
03770 return t;
03771 }
03772
03773
03774
03775
03776
03777
03778
03779
03780 static bool
03781 lookup_using_namespace (tree name, struct scope_binding *val,
03782 tree usings, tree scope, int flags)
03783 {
03784 tree iter;
03785 timevar_push (TV_NAME_LOOKUP);
03786
03787
03788 for (iter = usings; iter; iter = TREE_CHAIN (iter))
03789 if (TREE_VALUE (iter) == scope)
03790 {
03791 tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
03792 cxx_binding *val1 =
03793 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
03794
03795 if (val1)
03796 ambiguous_decl (name, val, val1, flags);
03797 }
03798 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
03799 }
03800
03801
03802
03803
03804
03805
03806 static bool
03807 qualified_lookup_using_namespace (tree name, tree scope,
03808 struct scope_binding *result, int flags)
03809 {
03810
03811 tree seen = NULL_TREE;
03812
03813 tree todo = NULL_TREE;
03814 tree todo_maybe = NULL_TREE;
03815 tree usings;
03816 timevar_push (TV_NAME_LOOKUP);
03817
03818 scope = ORIGINAL_NAMESPACE (scope);
03819 while (scope && result->value != error_mark_node)
03820 {
03821 cxx_binding *binding =
03822 cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
03823 seen = tree_cons (scope, NULL_TREE, seen);
03824 if (binding)
03825 ambiguous_decl (name, result, binding, flags);
03826
03827
03828
03829
03830
03831 for (usings = DECL_NAMESPACE_USING (scope); usings;
03832 usings = TREE_CHAIN (usings))
03833
03834 if (!TREE_INDIRECT_USING (usings))
03835 {
03836
03837
03838
03839
03840
03841 if (is_associated_namespace (scope, TREE_PURPOSE (usings))
03842 && !purpose_member (TREE_PURPOSE (usings), seen)
03843 && !purpose_member (TREE_PURPOSE (usings), todo))
03844 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
03845 else if ((!result->value && !result->type)
03846 && !purpose_member (TREE_PURPOSE (usings), seen)
03847 && !purpose_member (TREE_PURPOSE (usings), todo)
03848 && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
03849 todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
03850 todo_maybe);
03851 }
03852 if (todo)
03853 {
03854 scope = TREE_PURPOSE (todo);
03855 todo = TREE_CHAIN (todo);
03856 }
03857 else if (todo_maybe
03858 && (!result->value && !result->type))
03859 {
03860 scope = TREE_PURPOSE (todo_maybe);
03861 todo = TREE_CHAIN (todo_maybe);
03862 todo_maybe = NULL_TREE;
03863 }
03864 else
03865 scope = NULL_TREE;
03866 }
03867 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
03868 }
03869
03870
03871
03872
03873
03874 cxx_binding *
03875 outer_binding (tree name,
03876 cxx_binding *binding,
03877 bool class_p)
03878 {
03879 cxx_binding *outer;
03880 cxx_scope *scope;
03881 cxx_scope *outer_scope;
03882
03883 if (binding)
03884 {
03885 scope = binding->scope->level_chain;
03886 outer = binding->previous;
03887 }
03888 else
03889 {
03890 scope = current_binding_level;
03891 outer = IDENTIFIER_BINDING (name);
03892 }
03893 outer_scope = outer ? outer->scope : NULL;
03894
03895
03896
03897
03898
03899 if (class_p)
03900 while (scope && scope != outer_scope && scope->kind != sk_namespace)
03901 {
03902 if (scope->kind == sk_class)
03903 {
03904 cxx_binding *class_binding;
03905
03906 class_binding = get_class_binding (name, scope);
03907 if (class_binding)
03908 {
03909
03910
03911
03912 class_binding->previous = outer;
03913 if (binding)
03914 binding->previous = class_binding;
03915 else
03916 IDENTIFIER_BINDING (name) = class_binding;
03917 return class_binding;
03918 }
03919 }
03920 scope = scope->level_chain;
03921 }
03922
03923 return outer;
03924 }
03925
03926
03927
03928
03929 tree
03930 innermost_non_namespace_value (tree name)
03931 {
03932 cxx_binding *binding;
03933 binding = outer_binding (name, NULL, true);
03934 return binding ? binding->value : NULL_TREE;
03935 }
03936
03937
03938
03939
03940
03941
03942
03943
03944
03945
03946
03947
03948
03949
03950
03951
03952 tree
03953 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
03954 int namespaces_only, int flags)
03955 {
03956 cxx_binding *iter;
03957 tree val = NULL_TREE;
03958
03959 timevar_push (TV_NAME_LOOKUP);
03960
03961
03962
03963 if (IDENTIFIER_TYPENAME_P (name))
03964 {
03965 struct cp_binding_level *level;
03966
03967 for (level = current_binding_level;
03968 level && level->kind != sk_namespace;
03969 level = level->level_chain)
03970 {
03971 tree class_type;
03972 tree operators;
03973
03974
03975
03976 if (level->kind != sk_class)
03977 continue;
03978
03979
03980 class_type = level->this_entity;
03981 operators = lookup_fnfields (class_type, name, 0);
03982 if (operators)
03983 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
03984 }
03985
03986 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
03987 }
03988
03989 flags |= lookup_flags (prefer_type, namespaces_only);
03990
03991
03992
03993 if (current_class_type == NULL_TREE)
03994 nonclass = 1;
03995
03996 if (block_p || !nonclass)
03997 for (iter = outer_binding (name, NULL, !nonclass);
03998 iter;
03999 iter = outer_binding (name, iter, !nonclass))
04000 {
04001 tree binding;
04002
04003
04004 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
04005 continue;
04006
04007
04008 if (qualify_lookup (iter->value, flags))
04009 binding = iter->value;
04010 else if ((flags & LOOKUP_PREFER_TYPES)
04011 && qualify_lookup (iter->type, flags))
04012 binding = iter->type;
04013 else
04014 binding = NULL_TREE;
04015
04016 if (binding)
04017 {
04018
04019 gcc_assert (!hidden_name_p (binding));
04020 val = binding;
04021 break;
04022 }
04023 }
04024
04025
04026 if (!val)
04027 val = unqualified_namespace_lookup (name, flags);
04028
04029
04030 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
04031 val = OVL_FUNCTION (val);
04032
04033 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
04034 }
04035
04036 tree
04037 lookup_name_nonclass (tree name)
04038 {
04039 return lookup_name_real (name, 0, 1, true, 0, LOOKUP_COMPLAIN);
04040 }
04041
04042 tree
04043 lookup_function_nonclass (tree name, tree args, bool block_p)
04044 {
04045 return
04046 lookup_arg_dependent (name,
04047 lookup_name_real (name, 0, 1, block_p, 0,
04048 LOOKUP_COMPLAIN),
04049 args);
04050 }
04051
04052 tree
04053 lookup_name (tree name)
04054 {
04055 return lookup_name_real (name, 0, 0, true, 0, LOOKUP_COMPLAIN);
04056 }
04057
04058 tree
04059 lookup_name_prefer_type (tree name, int prefer_type)
04060 {
04061 return lookup_name_real (name, prefer_type, 0, true,
04062 0, LOOKUP_COMPLAIN);
04063 }
04064
04065
04066
04067
04068
04069
04070
04071
04072
04073
04074
04075
04076
04077
04078
04079
04080 tree
04081 lookup_type_scope (tree name, tag_scope scope)
04082 {
04083 cxx_binding *iter = NULL;
04084 tree val = NULL_TREE;
04085
04086 timevar_push (TV_NAME_LOOKUP);
04087
04088
04089 if (current_binding_level->kind != sk_namespace)
04090 iter = outer_binding (name, NULL, true);
04091 for (; iter; iter = outer_binding (name, iter, true))
04092 {
04093
04094
04095
04096
04097
04098
04099
04100
04101
04102
04103 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
04104 && (scope != ts_current
04105 || LOCAL_BINDING_P (iter)
04106 || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
04107 val = iter->type;
04108 else if ((scope != ts_current
04109 || !INHERITED_VALUE_BINDING_P (iter))
04110 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
04111 val = iter->value;
04112
04113 if (val)
04114 break;
04115 }
04116
04117
04118 if (!val)
04119 {
04120 iter = cxx_scope_find_binding_for_name
04121 (NAMESPACE_LEVEL (current_decl_namespace ()), name);
04122
04123 if (iter)
04124 {
04125
04126 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
04127 val = iter->type;
04128 else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
04129 val = iter->value;
04130 }
04131
04132 }
04133
04134
04135
04136 if (val)
04137 {
04138 struct cp_binding_level *b = current_binding_level;
04139 while (b)
04140 {
04141 if (iter->scope == b)
04142 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
04143
04144 if (b->kind == sk_cleanup || b->kind == sk_template_parms)
04145 b = b->level_chain;
04146 else if (b->kind == sk_class
04147 && scope == ts_within_enclosing_non_class)
04148 b = b->level_chain;
04149 else
04150 break;
04151 }
04152 }
04153
04154 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
04155 }
04156
04157
04158
04159
04160 static tree
04161 lookup_name_innermost_nonclass_level (tree name)
04162 {
04163 struct cp_binding_level *b;
04164 tree t = NULL_TREE;
04165
04166 timevar_push (TV_NAME_LOOKUP);
04167 b = innermost_nonclass_level ();
04168
04169 if (b->kind == sk_namespace)
04170 {
04171 t = IDENTIFIER_NAMESPACE_VALUE (name);
04172
04173
04174 if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
04175 t = TREE_VALUE (t);
04176 }
04177 else if (IDENTIFIER_BINDING (name)
04178 && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
04179 {
04180 cxx_binding *binding;
04181 binding = IDENTIFIER_BINDING (name);
04182 while (1)
04183 {
04184 if (binding->scope == b
04185 && !(TREE_CODE (binding->value) == VAR_DECL
04186 && DECL_DEAD_FOR_LOCAL (binding->value)))
04187 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
04188
04189 if (b->kind == sk_cleanup)
04190 b = b->level_chain;
04191 else
04192 break;
04193 }
04194 }
04195
04196 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
04197 }
04198
04199
04200
04201 static tree
04202 lookup_type_current_level (tree name)
04203 {
04204 tree t = NULL_TREE;
04205
04206 timevar_push (TV_NAME_LOOKUP);
04207 gcc_assert (current_binding_level->kind != sk_namespace);
04208
04209 if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
04210 && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
04211 {
04212 struct cp_binding_level *b = current_binding_level;
04213 while (1)
04214 {
04215 if (purpose_member (name, b->type_shadowed))
04216 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
04217 REAL_IDENTIFIER_TYPE_VALUE (name));
04218 if (b->kind == sk_cleanup)
04219 b = b->level_chain;
04220 else
04221 break;
04222 }
04223 }
04224
04225 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
04226 }
04227
04228
04229
04230
04231 struct arg_lookup
04232 {
04233 tree name;
04234 tree args;
04235 tree namespaces;
04236 tree classes;
04237 tree functions;
04238 };
04239
04240 static bool arg_assoc (struct arg_lookup*, tree);
04241 static bool arg_assoc_args (struct arg_lookup*, tree);
04242 static bool arg_assoc_type (struct arg_lookup*, tree);
04243 static bool add_function (struct arg_lookup *, tree);
04244 static bool arg_assoc_namespace (struct arg_lookup *, tree);
04245 static bool arg_assoc_class (struct arg_lookup *, tree);
04246 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
04247
04248
04249
04250
04251 static bool
04252 add_function (struct arg_lookup *k, tree fn)
04253 {
04254
04255
04256
04257
04258
04259
04260
04261
04262 if (!k->functions)
04263 k->functions = fn;
04264 else if (fn == k->functions)
04265 ;
04266 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
04267 k->functions = build_overload (fn, k->functions);
04268 else
04269 {
04270 tree f1 = OVL_CURRENT (k->functions);
04271 tree f2 = fn;
04272 if (is_overloaded_fn (f1))
04273 {
04274 fn = f1; f1 = f2; f2 = fn;
04275 }
04276 error ("%q+D is not a function,", f1);
04277 error (" conflict with %q+D", f2);
04278 error (" in call to %qD", k->name);
04279 return true;
04280 }
04281
04282 return false;
04283 }
04284
04285
04286
04287
04288
04289 bool
04290 is_associated_namespace (tree current, tree scope)
04291 {
04292 tree seen = NULL_TREE;
04293 tree todo = NULL_TREE;
04294 tree t;
04295 while (1)
04296 {
04297 if (scope == current)
04298 return true;
04299 seen = tree_cons (scope, NULL_TREE, seen);
04300 for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
04301 if (!purpose_member (TREE_PURPOSE (t), seen))
04302 todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
04303 if (todo)
04304 {
04305 scope = TREE_PURPOSE (todo);
04306 todo = TREE_CHAIN (todo);
04307 }
04308 else
04309 return false;
04310 }
04311 }
04312
04313
04314
04315 static bool
04316 friend_of_associated_class_p (tree arg, tree fn)
04317 {
04318 tree type;
04319
04320 if (TYPE_P (arg))
04321 type = arg;
04322 else if (type_unknown_p (arg))
04323 return false;
04324 else
04325 type = TREE_TYPE (arg);
04326
04327
04328
04329 if (CLASS_TYPE_P (type))
04330 {
04331 if (is_friend (type, fn))
04332 return true;
04333
04334 if (TYPE_BINFO (type))
04335 {
04336 tree binfo, base_binfo;
04337 int i;
04338
04339 for (binfo = TYPE_BINFO (type), i = 0;
04340 BINFO_BASE_ITERATE (binfo, i, base_binfo);
04341 i++)
04342 if (is_friend (BINFO_TYPE (base_binfo), fn))
04343 return true;
04344 }
04345 }
04346
04347
04348
04349 if ((CLASS_TYPE_P (type)
04350 || TREE_CODE (type) == UNION_TYPE
04351 || TREE_CODE (type) == ENUMERAL_TYPE)
04352 && TYPE_CONTEXT (type)
04353 && CLASS_TYPE_P (TYPE_CONTEXT (type))
04354 && is_friend (TYPE_CONTEXT (type), fn))
04355 return true;
04356
04357 return false;
04358 }
04359
04360
04361
04362
04363 static bool
04364 arg_assoc_namespace (struct arg_lookup *k, tree scope)
04365 {
04366 tree value;
04367
04368 if (purpose_member (scope, k->namespaces))
04369 return 0;
04370 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
04371
04372
04373 for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
04374 value = TREE_CHAIN (value))
04375 if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
04376 return true;
04377
04378 value = namespace_binding (k->name, scope);
04379 if (!value)
04380 return false;
04381
04382 for (; value; value = OVL_NEXT (value))
04383 {
04384
04385
04386
04387 if (hidden_name_p (OVL_CURRENT (value)))
04388 {
04389 tree args;
04390
04391 for (args = k->args; args; args = TREE_CHAIN (args))
04392 if (friend_of_associated_class_p (TREE_VALUE (args),
04393 OVL_CURRENT (value)))
04394 break;
04395 if (!args)
04396 continue;
04397 }
04398
04399 if (add_function (k, OVL_CURRENT (value)))
04400 return true;
04401 }
04402
04403 return false;
04404 }
04405
04406
04407
04408
04409 static bool
04410 arg_assoc_template_arg (struct arg_lookup *k, tree arg)
04411 {
04412
04413
04414
04415
04416
04417
04418
04419
04420
04421
04422
04423
04424 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
04425 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
04426 return false;
04427 else if (TREE_CODE (arg) == TEMPLATE_DECL)
04428 {
04429 tree ctx = CP_DECL_CONTEXT (arg);
04430
04431
04432 if (TREE_CODE (ctx) == NAMESPACE_DECL)
04433 return arg_assoc_namespace (k, ctx);
04434
04435 else
04436 return arg_assoc_class (k, ctx);
04437 }
04438
04439
04440 else if (TYPE_P (arg))
04441 return arg_assoc_type (k, arg);
04442
04443 else
04444 return false;
04445 }
04446
04447
04448
04449
04450 static bool
04451 arg_assoc_class (struct arg_lookup *k, tree type)
04452 {
04453 tree list, friends, context;
04454 int i;
04455
04456
04457
04458 if (!CLASS_TYPE_P (type))
04459 return false;
04460
04461 if (purpose_member (type, k->classes))
04462 return false;
04463 k->classes = tree_cons (type, NULL_TREE, k->classes);
04464
04465 context = decl_namespace_context (type);
04466 if (arg_assoc_namespace (k, context))
04467 return true;
04468
04469 if (TYPE_BINFO (type))
04470 {
04471
04472 tree binfo, base_binfo;
04473
04474 for (binfo = TYPE_BINFO (type), i = 0;
04475 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
04476 if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
04477 return true;
04478 }
04479
04480
04481 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
04482 list = TREE_CHAIN (list))
04483 if (k->name == FRIEND_NAME (list))
04484 for (friends = FRIEND_DECLS (list); friends;
04485 friends = TREE_CHAIN (friends))
04486 {
04487 tree fn = TREE_VALUE (friends);
04488
04489
04490
04491 if (CP_DECL_CONTEXT (fn) != context)
04492 continue;
04493
04494
04495
04496 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
04497 continue;
04498 if (add_function (k, fn))
04499 return true;
04500 }
04501
04502
04503 if (CLASSTYPE_TEMPLATE_INFO (type)
04504 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
04505 {
04506 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
04507 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
04508 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
04509 }
04510
04511 return false;
04512 }
04513
04514
04515
04516
04517 static bool
04518 arg_assoc_type (struct arg_lookup *k, tree type)
04519 {
04520
04521
04522 if (!type)
04523 return false;
04524
04525 if (TYPE_PTRMEM_P (type))
04526 {
04527
04528 if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
04529 return true;
04530 return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
04531 }
04532 else switch (TREE_CODE (type))
04533 {
04534 case ERROR_MARK:
04535 return false;
04536 case VOID_TYPE:
04537 case INTEGER_TYPE:
04538 case REAL_TYPE:
04539 case COMPLEX_TYPE:
04540 case VECTOR_TYPE:
04541 case BOOLEAN_TYPE:
04542 return false;
04543 case RECORD_TYPE:
04544 if (TYPE_PTRMEMFUNC_P (type))
04545 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
04546 return arg_assoc_class (k, type);
04547 case POINTER_TYPE:
04548 case REFERENCE_TYPE:
04549 case ARRAY_TYPE:
04550 return arg_assoc_type (k, TREE_TYPE (type));
04551 case UNION_TYPE:
04552 case ENUMERAL_TYPE:
04553 return arg_assoc_namespace (k, decl_namespace_context (type));
04554 case METHOD_TYPE:
04555
04556
04557 case FUNCTION_TYPE:
04558
04559 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
04560 return true;
04561
04562 return arg_assoc_type (k, TREE_TYPE (type));
04563 case TEMPLATE_TYPE_PARM:
04564 case BOUND_TEMPLATE_TEMPLATE_PARM:
04565 return false;
04566 case TYPENAME_TYPE:
04567 return false;
04568 case LANG_TYPE:
04569 gcc_assert (type == unknown_type_node);
04570 return false;
04571 default:
04572 gcc_unreachable ();
04573 }
04574 return false;
04575 }
04576
04577
04578
04579 static bool
04580 arg_assoc_args (struct arg_lookup *k, tree args)
04581 {
04582 for (; args; args = TREE_CHAIN (args))
04583 if (arg_assoc (k, TREE_VALUE (args)))
04584 return true;
04585 return false;
04586 }
04587
04588
04589
04590 static bool
04591 arg_assoc (struct arg_lookup *k, tree n)
04592 {
04593 if (n == error_mark_node)
04594 return false;
04595
04596 if (TYPE_P (n))
04597 return arg_assoc_type (k, n);
04598
04599 if (! type_unknown_p (n))
04600 return arg_assoc_type (k, TREE_TYPE (n));
04601
04602 if (TREE_CODE (n) == ADDR_EXPR)
04603 n = TREE_OPERAND (n, 0);
04604 if (TREE_CODE (n) == COMPONENT_REF)
04605 n = TREE_OPERAND (n, 1);
04606 if (TREE_CODE (n) == OFFSET_REF)
04607 n = TREE_OPERAND (n, 1);
04608 while (TREE_CODE (n) == TREE_LIST)
04609 n = TREE_VALUE (n);
04610 if (TREE_CODE (n) == BASELINK)
04611 n = BASELINK_FUNCTIONS (n);
04612
04613 if (TREE_CODE (n) == FUNCTION_DECL)
04614 return arg_assoc_type (k, TREE_TYPE (n));
04615 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
04616 {
04617
04618
04619
04620
04621
04622 tree template = TREE_OPERAND (n, 0);
04623 tree args = TREE_OPERAND (n, 1);
04624 tree ctx;
04625 int ix;
04626
04627 if (TREE_CODE (template) == COMPONENT_REF)
04628 template = TREE_OPERAND (template, 1);
04629
04630
04631
04632
04633
04634 template = OVL_CURRENT (template);
04635
04636 ctx = CP_DECL_CONTEXT (template);
04637
04638 if (TREE_CODE (ctx) == NAMESPACE_DECL)
04639 {
04640 if (arg_assoc_namespace (k, ctx) == 1)
04641 return true;
04642 }
04643
04644 else if (arg_assoc_class (k, ctx) == 1)
04645 return true;
04646
04647
04648 if (args)
04649 for (ix = TREE_VEC_LENGTH (args); ix--;)
04650 if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
04651 return true;
04652 }
04653 else if (TREE_CODE (n) == OVERLOAD)
04654 {
04655 for (; n; n = OVL_CHAIN (n))
04656 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
04657 return true;
04658 }
04659
04660 return false;
04661 }
04662
04663
04664
04665
04666 tree
04667 lookup_arg_dependent (tree name, tree fns, tree args)
04668 {
04669 struct arg_lookup k;
04670
04671 timevar_push (TV_NAME_LOOKUP);
04672
04673
04674
04675
04676 fns = remove_hidden_names (fns);
04677
04678 k.name = name;
04679 k.args = args;
04680 k.functions = fns;
04681 k.classes = NULL_TREE;
04682
04683
04684
04685
04686
04687
04688 k.namespaces = NULL_TREE;
04689
04690 arg_assoc_args (&k, args);
04691
04692 fns = k.functions;
04693
04694 if (fns
04695 && TREE_CODE (fns) != VAR_DECL
04696 && !is_overloaded_fn (fns))
04697 {
04698 error ("argument dependent lookup finds %q+D", fns);
04699 error (" in call to %qD", name);
04700 fns = error_mark_node;
04701 }
04702
04703 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
04704 }
04705
04706
04707
04708
04709
04710 static tree
04711 push_using_directive (tree used)
04712 {
04713 tree ud = current_binding_level->using_directives;
04714 tree iter, ancestor;
04715
04716 timevar_push (TV_NAME_LOOKUP);
04717
04718 if (purpose_member (used, ud) != NULL_TREE)
04719 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
04720
04721 ancestor = namespace_ancestor (current_decl_namespace (), used);
04722 ud = current_binding_level->using_directives;
04723 ud = tree_cons (used, ancestor, ud);
04724 current_binding_level->using_directives = ud;
04725
04726
04727 for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
04728 push_using_directive (TREE_PURPOSE (iter));
04729
04730 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
04731 }
04732
04733
04734
04735
04736
04737
04738
04739
04740
04741
04742 static tree
04743 maybe_process_template_type_declaration (tree type, int is_friend,
04744 cxx_scope *b)
04745 {
04746 tree decl = TYPE_NAME (type);
04747
04748 if (processing_template_parmlist)
04749
04750
04751
04752
04753
04754
04755 ;
04756 else if (b->kind == sk_namespace
04757 && current_binding_level->kind != sk_namespace)
04758
04759
04760 ;
04761 else
04762 {
04763 gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
04764
04765 if (processing_template_decl)
04766 {
04767
04768
04769 tree name = DECL_NAME (decl);
04770
04771 decl = push_template_decl_real (decl, is_friend);
04772
04773
04774
04775
04776
04777
04778
04779 if (TREE_CODE (type) != ENUMERAL_TYPE
04780 && !is_friend && b->kind == sk_template_parms
04781 && b->level_chain->kind == sk_class)
04782 {
04783 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
04784
04785 if (!COMPLETE_TYPE_P (current_class_type))
04786 {
04787 maybe_add_class_template_decl_list (current_class_type,
04788 type, 0);
04789
04790 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
04791 CLASSTYPE_NESTED_UTDS (current_class_type) =
04792 binding_table_new (SCOPE_DEFAULT_HT_SIZE);
04793
04794 binding_table_insert
04795 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
04796 }
04797 }
04798 }
04799 }
04800
04801 return decl;
04802 }
04803
04804
04805
04806
04807
04808
04809
04810
04811
04812
04813
04814
04815
04816
04817
04818
04819 tree
04820 pushtag (tree name, tree type, tag_scope scope)
04821 {
04822 struct cp_binding_level *b;
04823 tree decl;
04824
04825 timevar_push (TV_NAME_LOOKUP);
04826 b = current_binding_level;
04827 while (
04828
04829 b->kind == sk_cleanup
04830
04831
04832
04833
04834 || (b->kind == sk_template_parms
04835 && (b->explicit_spec_p || scope == ts_global))
04836 || (b->kind == sk_class
04837 && (scope != ts_current
04838
04839
04840
04841
04842 || COMPLETE_TYPE_P (b->this_entity))))
04843 b = b->level_chain;
04844
04845 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
04846
04847
04848 if (IDENTIFIER_TYPE_VALUE (name) != type)
04849 {
04850 tree tdef;
04851 int in_class = 0;
04852 tree context = TYPE_CONTEXT (type);
04853
04854 if (! context)
04855 {
04856 tree cs = current_scope ();
04857
04858 if (scope == ts_current)
04859 context = cs;
04860 else if (cs != NULL_TREE && TYPE_P (cs))
04861
04862
04863
04864
04865 context = decl_function_context (get_type_decl (cs));
04866 }
04867 if (!context)
04868 context = current_namespace;
04869
04870 if (b->kind == sk_class
04871 || (b->kind == sk_template_parms
04872 && b->level_chain->kind == sk_class))
04873 in_class = 1;
04874
04875 if (current_lang_name == lang_name_java)
04876 TYPE_FOR_JAVA (type) = 1;
04877
04878 tdef = create_implicit_typedef (name, type);
04879 DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
04880 if (scope == ts_within_enclosing_non_class)
04881 {
04882
04883
04884
04885 retrofit_lang_decl (tdef);
04886 DECL_ANTICIPATED (tdef) = 1;
04887 DECL_FRIEND_P (tdef) = 1;
04888 }
04889
04890 decl = maybe_process_template_type_declaration
04891 (type, scope == ts_within_enclosing_non_class, b);
04892 if (decl == error_mark_node)
04893 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
04894
04895 if (! in_class)
04896 set_identifier_type_value_with_scope (name, tdef, b);
04897
04898 if (b->kind == sk_class)
04899 {
04900 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
04901
04902
04903
04904
04905 finish_member_declaration (decl);
04906 else
04907 pushdecl_class_level (decl);
04908 }
04909 else if (b->kind != sk_template_parms)
04910 {
04911 decl = pushdecl_with_scope (decl, b, false);
04912 if (decl == error_mark_node)
04913 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
04914 }
04915
04916 TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
04917
04918
04919
04920
04921
04922
04923
04924 if (TYPE_CONTEXT (type)
04925 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
04926 VEC_safe_push (tree, gc, local_classes, type);
04927 }
04928 if (b->kind == sk_class
04929 && !COMPLETE_TYPE_P (current_class_type))
04930 {
04931 maybe_add_class_template_decl_list (current_class_type,
04932 type, 0);
04933
04934 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
04935 CLASSTYPE_NESTED_UTDS (current_class_type)
04936 = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
04937
04938 binding_table_insert
04939 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
04940 }
04941
04942 decl = TYPE_NAME (type);
04943 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
04944 TYPE_STUB_DECL (type) = decl;
04945
04946
04947 TREE_PUBLIC (decl) = 1;
04948 determine_visibility (decl);
04949
04950 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
04951 }
04952
04953
04954
04955
04956
04957
04958 struct saved_scope *scope_chain;
04959
04960
04961
04962
04963 static void
04964 store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
04965 {
04966 cxx_saved_binding *saved;
04967
04968 if (!id || !IDENTIFIER_BINDING (id))
04969 return;
04970
04971 if (IDENTIFIER_MARKED (id))
04972 return;
04973
04974 IDENTIFIER_MARKED (id) = 1;
04975
04976 saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
04977 saved->identifier = id;
04978 saved->binding = IDENTIFIER_BINDING (id);
04979 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
04980 IDENTIFIER_BINDING (id) = NULL;
04981 }
04982
04983 static void
04984 store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
04985 {
04986 tree t;
04987
04988 timevar_push (TV_NAME_LOOKUP);
04989 for (t = names; t; t = TREE_CHAIN (t))
04990 {
04991 tree id;
04992
04993 if (TREE_CODE (t) == TREE_LIST)
04994 id = TREE_PURPOSE (t);
04995 else
04996 id = DECL_NAME (t);
04997
04998 store_binding (id, old_bindings);
04999 }
05000 timevar_pop (TV_NAME_LOOKUP);
05001 }
05002
05003
05004
05005
05006 static void
05007 store_class_bindings (VEC(cp_class_binding,gc) *names,
05008 VEC(cxx_saved_binding,gc) **old_bindings)
05009 {
05010 size_t i;
05011 cp_class_binding *cb;
05012
05013 timevar_push (TV_NAME_LOOKUP);
05014 for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
05015 store_binding (cb->identifier, old_bindings);
05016 timevar_pop (TV_NAME_LOOKUP);
05017 }
05018
05019 void
05020 push_to_top_level (void)
05021 {
05022 struct saved_scope *s;
05023 struct cp_binding_level *b;
05024 cxx_saved_binding *sb;
05025 size_t i;
05026 int need_pop;
05027
05028 timevar_push (TV_NAME_LOOKUP);
05029 s = GGC_CNEW (struct saved_scope);
05030
05031 b = scope_chain ? current_binding_level : 0;
05032
05033
05034 if (cfun)
05035 {
05036 need_pop = 1;
05037 push_function_context_to (NULL_TREE);
05038 }
05039 else
05040 need_pop = 0;
05041
05042 if (scope_chain && previous_class_level)
05043 store_class_bindings (previous_class_level->class_shadowed,
05044 &s->old_bindings);
05045
05046
05047
05048 for (; b; b = b->level_chain)
05049 {
05050 tree t;
05051
05052
05053
05054
05055
05056 if (global_scope_p (b))
05057 break;
05058
05059 store_bindings (b->names, &s->old_bindings);
05060
05061
05062 if (b->kind == sk_class)
05063 store_class_bindings (b->class_shadowed, &s->old_bindings);
05064
05065
05066 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
05067 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
05068 }
05069
05070 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
05071 IDENTIFIER_MARKED (sb->identifier) = 0;
05072
05073 s->prev = scope_chain;
05074 s->bindings = b;
05075 s->need_pop_function_context = need_pop;
05076 s->function_decl = current_function_decl;
05077 s->skip_evaluation = skip_evaluation;
05078
05079 scope_chain = s;
05080 current_function_decl = NULL_TREE;
05081 current_lang_base = VEC_alloc (tree, gc, 10);
05082 current_lang_name = lang_name_cplusplus;
05083 current_namespace = global_namespace;
05084 push_class_stack ();
05085 skip_evaluation = 0;
05086 timevar_pop (TV_NAME_LOOKUP);
05087 }
05088
05089 void
05090 pop_from_top_level (void)
05091 {
05092 struct saved_scope *s = scope_chain;
05093 cxx_saved_binding *saved;
05094 size_t i;
05095
05096 timevar_push (TV_NAME_LOOKUP);
05097
05098 if (previous_class_level)
05099 invalidate_class_lookup_cache ();
05100 pop_class_stack ();
05101
05102 current_lang_base = 0;
05103
05104 scope_chain = s->prev;
05105 for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
05106 {
05107 tree id = saved->identifier;
05108
05109 IDENTIFIER_BINDING (id) = saved->binding;
05110 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
05111 }
05112
05113
05114
05115 if (s->need_pop_function_context)
05116 pop_function_context_from (NULL_TREE);
05117 current_function_decl = s->function_decl;
05118 skip_evaluation = s->skip_evaluation;
05119 timevar_pop (TV_NAME_LOOKUP);
05120 }
05121
05122
05123
05124
05125
05126 void
05127 pop_everything (void)
05128 {
05129 if (ENABLE_SCOPE_CHECKING)
05130 verbatim ("XXX entering pop_everything ()\n");
05131 while (!toplevel_bindings_p ())
05132 {
05133 if (current_binding_level->kind == sk_class)
05134 pop_nested_class ();
05135 else
05136 poplevel (0, 0, 0);
05137 }
05138 if (ENABLE_SCOPE_CHECKING)
05139 verbatim ("XXX leaving pop_everything ()\n");
05140 }
05141
05142
05143
05144
05145
05146 void
05147 cp_emit_debug_info_for_using (tree t, tree context)
05148 {
05149
05150 if (sorrycount || errorcount)
05151 return;
05152
05153
05154
05155 if (TREE_CODE (t) == FUNCTION_DECL
05156 && DECL_EXTERNAL (t)
05157 && DECL_BUILT_IN (t))
05158 return;
05159
05160
05161
05162 if (context == global_namespace)
05163 context = NULL_TREE;
05164
05165 if (BASELINK_P (t))
05166 t = BASELINK_FUNCTIONS (t);
05167
05168
05169 for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
05170 if (TREE_CODE (t) != TEMPLATE_DECL)
05171 (*debug_hooks->imported_module_or_decl) (t, context);
05172 }
05173
05174 #include "gt-cp-name-lookup.h"