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