]>
Commit | Line | Data |
---|---|---|
aed81407 | 1 | /* Definitions for C++ name lookup routines. |
cbe34bb5 | 2 | Copyright (C) 2003-2017 Free Software Foundation, Inc. |
aed81407 GDR |
3 | Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> |
4 | ||
ed3cf953 | 5 | This file is part of GCC. |
aed81407 | 6 | |
ed3cf953 | 7 | GCC is free software; you can redistribute it and/or modify |
aed81407 | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
aed81407 GDR |
10 | any later version. |
11 | ||
ed3cf953 | 12 | GCC is distributed in the hope that it will be useful, |
aed81407 GDR |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
aed81407 GDR |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
2adfab87 AM |
24 | #include "cp-tree.h" |
25 | #include "timevar.h" | |
d8a2d370 DN |
26 | #include "stringpool.h" |
27 | #include "print-tree.h" | |
28 | #include "attribs.h" | |
6097b0c3 | 29 | #include "debug.h" |
39dabefd | 30 | #include "c-family/c-pragma.h" |
501c95ff | 31 | #include "params.h" |
52ed68f7 DM |
32 | #include "gcc-rich-location.h" |
33 | #include "spellcheck-tree.h" | |
34 | #include "parser.h" | |
00e8de68 | 35 | |
b655c310 NS |
36 | static cxx_binding *cxx_binding_make (tree value, tree type); |
37 | static cp_binding_level *innermost_nonclass_level (void); | |
38 | static void set_identifier_type_value_with_scope (tree id, tree decl, | |
39 | cp_binding_level *b); | |
4b4b2e58 NS |
40 | static void set_namespace_binding (tree name, tree scope, tree val); |
41 | ||
15f8ac7f GK |
42 | /* The bindings for a particular name in a particular scope. */ |
43 | ||
44 | struct scope_binding { | |
45 | tree value; | |
46 | tree type; | |
47 | }; | |
48 | #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE } | |
49 | ||
2c140474 | 50 | static cxx_binding *binding_for_name (cp_binding_level *, tree); |
d63d5d0c | 51 | static tree push_overloaded_decl (tree, int, bool); |
15f8ac7f | 52 | static bool lookup_using_namespace (tree, struct scope_binding *, tree, |
0cbd7506 | 53 | tree, int); |
15f8ac7f GK |
54 | static bool qualified_lookup_using_namespace (tree, tree, |
55 | struct scope_binding *, int); | |
7d5dbb22 DM |
56 | static void consider_binding_level (tree name, |
57 | best_match <tree, const char *> &bm, | |
ebed7175 DM |
58 | cp_binding_level *lvl, |
59 | bool look_within_fields, | |
60 | enum lookup_name_fuzzy_kind kind); | |
a5e6b29b GDR |
61 | static tree lookup_type_current_level (tree); |
62 | static tree push_using_directive (tree); | |
3a636414 | 63 | static tree lookup_extern_c_fun_in_all_ns (tree); |
557831a9 | 64 | static void diagnose_name_conflict (tree, tree); |
5a167978 | 65 | |
b655c310 | 66 | /* Add DECL to the list of things declared in B. */ |
5880f14f | 67 | |
b655c310 NS |
68 | static void |
69 | add_decl_to_level (tree decl, cp_binding_level *b) | |
5880f14f | 70 | { |
b655c310 NS |
71 | /* We used to record virtual tables as if they were ordinary |
72 | variables, but no longer do so. */ | |
73 | gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl))); | |
74 | ||
75 | if (TREE_CODE (decl) == NAMESPACE_DECL | |
76 | && !DECL_NAMESPACE_ALIAS (decl)) | |
5880f14f | 77 | { |
b655c310 NS |
78 | DECL_CHAIN (decl) = b->namespaces; |
79 | b->namespaces = decl; | |
5880f14f | 80 | } |
b655c310 NS |
81 | else |
82 | { | |
83 | /* We build up the list in reverse order, and reverse it later if | |
84 | necessary. */ | |
85 | TREE_CHAIN (decl) = b->names; | |
86 | b->names = decl; | |
5e0c54e5 | 87 | |
b655c310 NS |
88 | /* If appropriate, add decl to separate list of statics. We |
89 | include extern variables because they might turn out to be | |
90 | static later. It's OK for this list to contain a few false | |
91 | positives. */ | |
92 | if (b->kind == sk_namespace) | |
93 | if ((VAR_P (decl) | |
94 | && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) | |
95 | || (TREE_CODE (decl) == FUNCTION_DECL | |
96 | && (!TREE_PUBLIC (decl) | |
97 | || decl_anon_ns_mem_p (decl) | |
98 | || DECL_DECLARED_INLINE_P (decl)))) | |
30318029 | 99 | vec_safe_push (static_decls, decl); |
b655c310 NS |
100 | } |
101 | } | |
daafa301 | 102 | |
b655c310 NS |
103 | /* [basic.lookup.koenig] */ |
104 | /* A nonzero return value in the functions below indicates an error. */ | |
5e0c54e5 | 105 | |
b655c310 NS |
106 | struct arg_lookup |
107 | { | |
108 | tree name; | |
109 | vec<tree, va_gc> *args; | |
110 | vec<tree, va_gc> *namespaces; | |
111 | vec<tree, va_gc> *classes; | |
112 | tree functions; | |
113 | hash_set<tree> *fn_set; | |
114 | }; | |
8db29d88 | 115 | |
b655c310 NS |
116 | static bool arg_assoc (struct arg_lookup*, tree); |
117 | static bool arg_assoc_args (struct arg_lookup*, tree); | |
118 | static bool arg_assoc_args_vec (struct arg_lookup*, vec<tree, va_gc> *); | |
119 | static bool arg_assoc_type (struct arg_lookup*, tree); | |
120 | static bool add_function (struct arg_lookup *, tree); | |
121 | static bool arg_assoc_namespace (struct arg_lookup *, tree); | |
122 | static bool arg_assoc_class_only (struct arg_lookup *, tree); | |
123 | static bool arg_assoc_bases (struct arg_lookup *, tree); | |
124 | static bool arg_assoc_class (struct arg_lookup *, tree); | |
125 | static bool arg_assoc_template_arg (struct arg_lookup*, tree); | |
8db29d88 | 126 | |
b655c310 NS |
127 | /* Add a function to the lookup structure. |
128 | Returns true on error. */ | |
8db29d88 | 129 | |
b655c310 NS |
130 | static bool |
131 | add_function (struct arg_lookup *k, tree fn) | |
8db29d88 | 132 | { |
b655c310 NS |
133 | if (!is_overloaded_fn (fn)) |
134 | /* All names except those of (possibly overloaded) functions and | |
135 | function templates are ignored. */; | |
136 | else if (k->fn_set && k->fn_set->add (fn)) | |
137 | /* It's already in the list. */; | |
138 | else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL) | |
139 | k->functions = fn; | |
140 | else if (fn == k->functions) | |
141 | ; | |
142 | else | |
143 | { | |
144 | k->functions = build_overload (fn, k->functions); | |
145 | if (TREE_CODE (k->functions) == OVERLOAD) | |
146 | OVL_ARG_DEPENDENT (k->functions) = true; | |
147 | } | |
8db29d88 | 148 | |
b655c310 | 149 | return false; |
8db29d88 AO |
150 | } |
151 | ||
b655c310 NS |
152 | /* Returns true iff CURRENT has declared itself to be an associated |
153 | namespace of SCOPE via a strong using-directive (or transitive chain | |
154 | thereof). Both are namespaces. */ | |
daafa301 | 155 | |
b655c310 NS |
156 | bool |
157 | is_associated_namespace (tree current, tree scope) | |
5e0c54e5 | 158 | { |
b655c310 NS |
159 | vec<tree, va_gc> *seen = make_tree_vector (); |
160 | vec<tree, va_gc> *todo = make_tree_vector (); | |
161 | tree t; | |
162 | bool ret; | |
5e0c54e5 | 163 | |
b655c310 | 164 | while (1) |
5e0c54e5 | 165 | { |
b655c310 NS |
166 | if (scope == current) |
167 | { | |
168 | ret = true; | |
169 | break; | |
170 | } | |
171 | vec_safe_push (seen, scope); | |
172 | for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t)) | |
173 | if (!vec_member (TREE_PURPOSE (t), seen)) | |
174 | vec_safe_push (todo, TREE_PURPOSE (t)); | |
175 | if (!todo->is_empty ()) | |
176 | { | |
177 | scope = todo->last (); | |
178 | todo->pop (); | |
179 | } | |
180 | else | |
181 | { | |
182 | ret = false; | |
183 | break; | |
184 | } | |
5e0c54e5 | 185 | } |
5e0c54e5 | 186 | |
b655c310 NS |
187 | release_tree_vector (seen); |
188 | release_tree_vector (todo); | |
5e0c54e5 | 189 | |
b655c310 | 190 | return ret; |
5e0c54e5 | 191 | } |
5e0c54e5 | 192 | |
b655c310 NS |
193 | /* Add functions of a namespace to the lookup structure. |
194 | Returns true on error. */ | |
5e0c54e5 | 195 | |
b655c310 NS |
196 | static bool |
197 | arg_assoc_namespace (struct arg_lookup *k, tree scope) | |
198 | { | |
199 | tree value; | |
5e0c54e5 | 200 | |
b655c310 NS |
201 | if (vec_member (scope, k->namespaces)) |
202 | return false; | |
203 | vec_safe_push (k->namespaces, scope); | |
daafa301 | 204 | |
b655c310 NS |
205 | /* Check out our super-users. */ |
206 | for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value; | |
207 | value = TREE_CHAIN (value)) | |
208 | if (arg_assoc_namespace (k, TREE_PURPOSE (value))) | |
209 | return true; | |
5e0c54e5 | 210 | |
b655c310 NS |
211 | /* Also look down into inline namespaces. */ |
212 | for (value = DECL_NAMESPACE_USING (scope); value; | |
213 | value = TREE_CHAIN (value)) | |
214 | if (is_associated_namespace (scope, TREE_PURPOSE (value))) | |
215 | if (arg_assoc_namespace (k, TREE_PURPOSE (value))) | |
216 | return true; | |
daafa301 | 217 | |
06aa5490 | 218 | value = get_namespace_binding (scope, k->name); |
b655c310 NS |
219 | if (!value) |
220 | return false; | |
5e0c54e5 | 221 | |
b655c310 | 222 | for (; value; value = OVL_NEXT (value)) |
5e0c54e5 | 223 | { |
b655c310 NS |
224 | /* We don't want to find arbitrary hidden functions via argument |
225 | dependent lookup. We only want to find friends of associated | |
226 | classes, which we'll do via arg_assoc_class. */ | |
227 | if (hidden_name_p (OVL_CURRENT (value))) | |
228 | continue; | |
5e0c54e5 | 229 | |
b655c310 NS |
230 | if (add_function (k, OVL_CURRENT (value))) |
231 | return true; | |
232 | } | |
daafa301 | 233 | |
b655c310 | 234 | return false; |
5e0c54e5 GDR |
235 | } |
236 | ||
b655c310 NS |
237 | /* Adds everything associated with a template argument to the lookup |
238 | structure. Returns true on error. */ | |
daafa301 | 239 | |
b655c310 NS |
240 | static bool |
241 | arg_assoc_template_arg (struct arg_lookup *k, tree arg) | |
5e0c54e5 | 242 | { |
b655c310 | 243 | /* [basic.lookup.koenig] |
5e0c54e5 | 244 | |
b655c310 NS |
245 | If T is a template-id, its associated namespaces and classes are |
246 | ... the namespaces and classes associated with the types of the | |
247 | template arguments provided for template type parameters | |
248 | (excluding template template parameters); the namespaces in which | |
249 | any template template arguments are defined; and the classes in | |
250 | which any member templates used as template template arguments | |
251 | are defined. [Note: non-type template arguments do not | |
252 | contribute to the set of associated namespaces. ] */ | |
253 | ||
254 | /* Consider first template template arguments. */ | |
255 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
256 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) | |
257 | return false; | |
258 | else if (TREE_CODE (arg) == TEMPLATE_DECL) | |
5e0c54e5 | 259 | { |
b655c310 | 260 | tree ctx = CP_DECL_CONTEXT (arg); |
0cbd7506 | 261 | |
b655c310 NS |
262 | /* It's not a member template. */ |
263 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
264 | return arg_assoc_namespace (k, ctx); | |
265 | /* Otherwise, it must be member template. */ | |
266 | else | |
267 | return arg_assoc_class_only (k, ctx); | |
5e0c54e5 | 268 | } |
b655c310 NS |
269 | /* It's an argument pack; handle it recursively. */ |
270 | else if (ARGUMENT_PACK_P (arg)) | |
271 | { | |
272 | tree args = ARGUMENT_PACK_ARGS (arg); | |
273 | int i, len = TREE_VEC_LENGTH (args); | |
274 | for (i = 0; i < len; ++i) | |
275 | if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i))) | |
276 | return true; | |
277 | ||
278 | return false; | |
279 | } | |
280 | /* It's not a template template argument, but it is a type template | |
281 | argument. */ | |
282 | else if (TYPE_P (arg)) | |
283 | return arg_assoc_type (k, arg); | |
284 | /* It's a non-type template argument. */ | |
285 | else | |
286 | return false; | |
5e0c54e5 GDR |
287 | } |
288 | ||
b655c310 NS |
289 | /* Adds the class and its friends to the lookup structure. |
290 | Returns true on error. */ | |
daafa301 | 291 | |
b655c310 NS |
292 | static bool |
293 | arg_assoc_class_only (struct arg_lookup *k, tree type) | |
5e0c54e5 | 294 | { |
b655c310 | 295 | tree list, friends, context; |
5e0c54e5 | 296 | |
b655c310 NS |
297 | /* Backend-built structures, such as __builtin_va_list, aren't |
298 | affected by all this. */ | |
299 | if (!CLASS_TYPE_P (type)) | |
300 | return false; | |
5e0c54e5 | 301 | |
b655c310 NS |
302 | context = decl_namespace_context (type); |
303 | if (arg_assoc_namespace (k, context)) | |
304 | return true; | |
5e0c54e5 | 305 | |
b655c310 | 306 | complete_type (type); |
daafa301 | 307 | |
b655c310 NS |
308 | /* Process friends. */ |
309 | for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; | |
310 | list = TREE_CHAIN (list)) | |
311 | if (k->name == FRIEND_NAME (list)) | |
312 | for (friends = FRIEND_DECLS (list); friends; | |
313 | friends = TREE_CHAIN (friends)) | |
314 | { | |
315 | tree fn = TREE_VALUE (friends); | |
5e0c54e5 | 316 | |
b655c310 NS |
317 | /* Only interested in global functions with potentially hidden |
318 | (i.e. unqualified) declarations. */ | |
319 | if (CP_DECL_CONTEXT (fn) != context) | |
320 | continue; | |
321 | /* Template specializations are never found by name lookup. | |
322 | (Templates themselves can be found, but not template | |
323 | specializations.) */ | |
324 | if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) | |
325 | continue; | |
326 | if (add_function (k, fn)) | |
327 | return true; | |
328 | } | |
5e0c54e5 | 329 | |
b655c310 | 330 | return false; |
5e0c54e5 GDR |
331 | } |
332 | ||
b655c310 NS |
333 | /* Adds the class and its bases to the lookup structure. |
334 | Returns true on error. */ | |
daafa301 | 335 | |
b655c310 NS |
336 | static bool |
337 | arg_assoc_bases (struct arg_lookup *k, tree type) | |
5e0c54e5 | 338 | { |
b655c310 NS |
339 | if (arg_assoc_class_only (k, type)) |
340 | return true; | |
82f2836c | 341 | |
b655c310 | 342 | if (TYPE_BINFO (type)) |
5e0c54e5 | 343 | { |
b655c310 NS |
344 | /* Process baseclasses. */ |
345 | tree binfo, base_binfo; | |
346 | int i; | |
aed81407 | 347 | |
b655c310 NS |
348 | for (binfo = TYPE_BINFO (type), i = 0; |
349 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | |
350 | if (arg_assoc_bases (k, BINFO_TYPE (base_binfo))) | |
351 | return true; | |
352 | } | |
89b578be | 353 | |
b655c310 | 354 | return false; |
89b578be MM |
355 | } |
356 | ||
b655c310 NS |
357 | /* Adds everything associated with a class argument type to the lookup |
358 | structure. Returns true on error. | |
daafa301 | 359 | |
b655c310 NS |
360 | If T is a class type (including unions), its associated classes are: the |
361 | class itself; the class of which it is a member, if any; and its direct | |
362 | and indirect base classes. Its associated namespaces are the namespaces | |
363 | of which its associated classes are members. Furthermore, if T is a | |
364 | class template specialization, its associated namespaces and classes | |
365 | also include: the namespaces and classes associated with the types of | |
366 | the template arguments provided for template type parameters (excluding | |
367 | template template parameters); the namespaces of which any template | |
368 | template arguments are members; and the classes of which any member | |
369 | templates used as template template arguments are members. [ Note: | |
370 | non-type template arguments do not contribute to the set of associated | |
371 | namespaces. --end note] */ | |
372 | ||
373 | static bool | |
374 | arg_assoc_class (struct arg_lookup *k, tree type) | |
aed81407 | 375 | { |
b655c310 NS |
376 | tree list; |
377 | int i; | |
aed81407 | 378 | |
b655c310 NS |
379 | /* Backend build structures, such as __builtin_va_list, aren't |
380 | affected by all this. */ | |
381 | if (!CLASS_TYPE_P (type)) | |
382 | return false; | |
aed81407 | 383 | |
b655c310 NS |
384 | if (vec_member (type, k->classes)) |
385 | return false; | |
386 | vec_safe_push (k->classes, type); | |
aed81407 | 387 | |
b655c310 NS |
388 | if (TYPE_CLASS_SCOPE_P (type) |
389 | && arg_assoc_class_only (k, TYPE_CONTEXT (type))) | |
390 | return true; | |
daafa301 | 391 | |
b655c310 NS |
392 | if (arg_assoc_bases (k, type)) |
393 | return true; | |
c87ceb13 | 394 | |
b655c310 NS |
395 | /* Process template arguments. */ |
396 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
397 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) | |
398 | { | |
399 | list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
400 | for (i = 0; i < TREE_VEC_LENGTH (list); ++i) | |
401 | if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i))) | |
402 | return true; | |
403 | } | |
00e8de68 | 404 | |
b655c310 | 405 | return false; |
90ea9897 MM |
406 | } |
407 | ||
b655c310 NS |
408 | /* Adds everything associated with a given type. |
409 | Returns 1 on error. */ | |
90ea9897 | 410 | |
b655c310 NS |
411 | static bool |
412 | arg_assoc_type (struct arg_lookup *k, tree type) | |
90ea9897 | 413 | { |
b655c310 NS |
414 | /* As we do not get the type of non-type dependent expressions |
415 | right, we can end up with such things without a type. */ | |
416 | if (!type) | |
417 | return false; | |
7de5bccc | 418 | |
b655c310 | 419 | if (TYPE_PTRDATAMEM_P (type)) |
90ea9897 | 420 | { |
b655c310 NS |
421 | /* Pointer to member: associate class type and value type. */ |
422 | if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type))) | |
423 | return true; | |
424 | return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type)); | |
89b578be | 425 | } |
b655c310 NS |
426 | else switch (TREE_CODE (type)) |
427 | { | |
428 | case ERROR_MARK: | |
429 | return false; | |
430 | case VOID_TYPE: | |
431 | case INTEGER_TYPE: | |
432 | case REAL_TYPE: | |
433 | case COMPLEX_TYPE: | |
434 | case VECTOR_TYPE: | |
435 | case BOOLEAN_TYPE: | |
436 | case FIXED_POINT_TYPE: | |
437 | case DECLTYPE_TYPE: | |
438 | case NULLPTR_TYPE: | |
439 | return false; | |
440 | case RECORD_TYPE: | |
441 | if (TYPE_PTRMEMFUNC_P (type)) | |
442 | return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
443 | /* FALLTHRU */ | |
444 | case UNION_TYPE: | |
445 | return arg_assoc_class (k, type); | |
446 | case POINTER_TYPE: | |
447 | case REFERENCE_TYPE: | |
448 | case ARRAY_TYPE: | |
449 | return arg_assoc_type (k, TREE_TYPE (type)); | |
450 | case ENUMERAL_TYPE: | |
451 | if (TYPE_CLASS_SCOPE_P (type) | |
452 | && arg_assoc_class_only (k, TYPE_CONTEXT (type))) | |
453 | return true; | |
454 | return arg_assoc_namespace (k, decl_namespace_context (type)); | |
455 | case METHOD_TYPE: | |
456 | /* The basetype is referenced in the first arg type, so just | |
457 | fall through. */ | |
458 | case FUNCTION_TYPE: | |
459 | /* Associate the parameter types. */ | |
460 | if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) | |
461 | return true; | |
462 | /* Associate the return type. */ | |
463 | return arg_assoc_type (k, TREE_TYPE (type)); | |
464 | case TEMPLATE_TYPE_PARM: | |
465 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
466 | return false; | |
467 | case TYPENAME_TYPE: | |
468 | return false; | |
469 | case LANG_TYPE: | |
470 | gcc_assert (type == unknown_type_node | |
471 | || type == init_list_type_node); | |
472 | return false; | |
473 | case TYPE_PACK_EXPANSION: | |
474 | return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type)); | |
c8094d83 | 475 | |
b655c310 NS |
476 | default: |
477 | gcc_unreachable (); | |
478 | } | |
479 | return false; | |
480 | } | |
00e8de68 | 481 | |
b655c310 NS |
482 | /* Adds everything associated with arguments. Returns true on error. */ |
483 | ||
484 | static bool | |
485 | arg_assoc_args (struct arg_lookup *k, tree args) | |
486 | { | |
487 | for (; args; args = TREE_CHAIN (args)) | |
488 | if (arg_assoc (k, TREE_VALUE (args))) | |
489 | return true; | |
490 | return false; | |
00e8de68 GDR |
491 | } |
492 | ||
b655c310 NS |
493 | /* Adds everything associated with an argument vector. Returns true |
494 | on error. */ | |
00e8de68 | 495 | |
b655c310 NS |
496 | static bool |
497 | arg_assoc_args_vec (struct arg_lookup *k, vec<tree, va_gc> *args) | |
00e8de68 | 498 | { |
b655c310 NS |
499 | unsigned int ix; |
500 | tree arg; | |
00e8de68 | 501 | |
b655c310 NS |
502 | FOR_EACH_VEC_SAFE_ELT (args, ix, arg) |
503 | if (arg_assoc (k, arg)) | |
504 | return true; | |
505 | return false; | |
506 | } | |
00e8de68 | 507 | |
b655c310 | 508 | /* Adds everything associated with a given tree_node. Returns 1 on error. */ |
00e8de68 | 509 | |
b655c310 NS |
510 | static bool |
511 | arg_assoc (struct arg_lookup *k, tree n) | |
512 | { | |
513 | if (n == error_mark_node) | |
514 | return false; | |
00e8de68 | 515 | |
b655c310 NS |
516 | if (TYPE_P (n)) |
517 | return arg_assoc_type (k, n); | |
00e8de68 | 518 | |
b655c310 NS |
519 | if (! type_unknown_p (n)) |
520 | return arg_assoc_type (k, TREE_TYPE (n)); | |
521 | ||
522 | if (TREE_CODE (n) == ADDR_EXPR) | |
523 | n = TREE_OPERAND (n, 0); | |
524 | if (TREE_CODE (n) == COMPONENT_REF) | |
525 | n = TREE_OPERAND (n, 1); | |
526 | if (TREE_CODE (n) == OFFSET_REF) | |
527 | n = TREE_OPERAND (n, 1); | |
528 | while (TREE_CODE (n) == TREE_LIST) | |
529 | n = TREE_VALUE (n); | |
530 | if (BASELINK_P (n)) | |
531 | n = BASELINK_FUNCTIONS (n); | |
532 | ||
533 | if (TREE_CODE (n) == FUNCTION_DECL) | |
534 | return arg_assoc_type (k, TREE_TYPE (n)); | |
535 | if (TREE_CODE (n) == TEMPLATE_ID_EXPR) | |
00e8de68 | 536 | { |
b655c310 NS |
537 | /* The working paper doesn't currently say how to handle template-id |
538 | arguments. The sensible thing would seem to be to handle the list | |
539 | of template candidates like a normal overload set, and handle the | |
540 | template arguments like we do for class template | |
541 | specializations. */ | |
542 | tree templ = TREE_OPERAND (n, 0); | |
543 | tree args = TREE_OPERAND (n, 1); | |
544 | int ix; | |
00e8de68 | 545 | |
b655c310 NS |
546 | /* First the templates. */ |
547 | if (arg_assoc (k, templ)) | |
548 | return true; | |
549 | ||
550 | /* Now the arguments. */ | |
551 | if (args) | |
552 | for (ix = TREE_VEC_LENGTH (args); ix--;) | |
553 | if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1) | |
554 | return true; | |
00e8de68 | 555 | } |
b655c310 NS |
556 | else if (TREE_CODE (n) == OVERLOAD) |
557 | { | |
558 | for (; n; n = OVL_NEXT (n)) | |
559 | if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n)))) | |
560 | return true; | |
561 | } | |
562 | ||
563 | return false; | |
00e8de68 GDR |
564 | } |
565 | ||
b655c310 NS |
566 | /* Performs Koenig lookup depending on arguments, where fns |
567 | are the functions found in normal lookup. */ | |
9485d254 | 568 | |
b655c310 NS |
569 | static cp_expr |
570 | lookup_arg_dependent_1 (tree name, tree fns, vec<tree, va_gc> *args) | |
9485d254 | 571 | { |
b655c310 | 572 | struct arg_lookup k; |
9485d254 | 573 | |
b655c310 NS |
574 | /* Remove any hidden friend functions from the list of functions |
575 | found so far. They will be added back by arg_assoc_class as | |
576 | appropriate. */ | |
577 | fns = remove_hidden_names (fns); | |
557831a9 | 578 | |
b655c310 NS |
579 | k.name = name; |
580 | k.args = args; | |
581 | k.functions = fns; | |
582 | k.classes = make_tree_vector (); | |
af79925b | 583 | |
b655c310 NS |
584 | /* We previously performed an optimization here by setting |
585 | NAMESPACES to the current namespace when it was safe. However, DR | |
586 | 164 says that namespaces that were already searched in the first | |
587 | stage of template processing are searched again (potentially | |
588 | picking up later definitions) in the second stage. */ | |
589 | k.namespaces = make_tree_vector (); | |
7f82286e | 590 | |
b655c310 NS |
591 | /* We used to allow duplicates and let joust discard them, but |
592 | since the above change for DR 164 we end up with duplicates of | |
593 | all the functions found by unqualified lookup. So keep track | |
594 | of which ones we've seen. */ | |
595 | if (fns) | |
7f82286e | 596 | { |
b655c310 NS |
597 | tree ovl; |
598 | /* We shouldn't be here if lookup found something other than | |
599 | namespace-scope functions. */ | |
600 | gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns))); | |
601 | k.fn_set = new hash_set<tree>; | |
602 | for (ovl = fns; ovl; ovl = OVL_NEXT (ovl)) | |
603 | k.fn_set->add (OVL_CURRENT (ovl)); | |
7f82286e | 604 | } |
b655c310 NS |
605 | else |
606 | k.fn_set = NULL; | |
7f82286e | 607 | |
b655c310 | 608 | arg_assoc_args_vec (&k, args); |
557831a9 | 609 | |
b655c310 NS |
610 | fns = k.functions; |
611 | ||
612 | if (fns | |
613 | && !VAR_P (fns) | |
614 | && !is_overloaded_fn (fns)) | |
615 | { | |
616 | error ("argument dependent lookup finds %q+D", fns); | |
617 | error (" in call to %qD", name); | |
618 | fns = error_mark_node; | |
619 | } | |
c87ceb13 | 620 | |
b655c310 NS |
621 | release_tree_vector (k.classes); |
622 | release_tree_vector (k.namespaces); | |
623 | delete k.fn_set; | |
624 | ||
625 | return fns; | |
626 | } | |
c87ceb13 | 627 | |
b655c310 | 628 | /* Wrapper for lookup_arg_dependent_1. */ |
c87ceb13 | 629 | |
b655c310 NS |
630 | cp_expr |
631 | lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args) | |
c87ceb13 | 632 | { |
b655c310 NS |
633 | cp_expr ret; |
634 | bool subtime; | |
635 | subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
636 | ret = lookup_arg_dependent_1 (name, fns, args); | |
637 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
638 | return ret; | |
639 | } | |
557831a9 | 640 | |
b655c310 NS |
641 | /* Compute the chain index of a binding_entry given the HASH value of its |
642 | name and the total COUNT of chains. COUNT is assumed to be a power | |
643 | of 2. */ | |
9306cccb | 644 | |
b655c310 | 645 | #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) |
c87ceb13 | 646 | |
b655c310 | 647 | /* A free list of "binding_entry"s awaiting for re-use. */ |
c87ceb13 | 648 | |
b655c310 | 649 | static GTY((deletable)) binding_entry free_binding_entry = NULL; |
c8094d83 | 650 | |
b655c310 | 651 | /* The binding oracle; see cp-tree.h. */ |
c87ceb13 | 652 | |
b655c310 | 653 | cp_binding_oracle_function *cp_binding_oracle; |
575bfb00 | 654 | |
b655c310 NS |
655 | /* If we have a binding oracle, ask it for all namespace-scoped |
656 | definitions of NAME. */ | |
557831a9 | 657 | |
b655c310 NS |
658 | static inline void |
659 | query_oracle (tree name) | |
557831a9 | 660 | { |
b655c310 NS |
661 | if (!cp_binding_oracle) |
662 | return; | |
557831a9 | 663 | |
b655c310 NS |
664 | /* LOOKED_UP holds the set of identifiers that we have already |
665 | looked up with the oracle. */ | |
666 | static hash_set<tree> looked_up; | |
667 | if (looked_up.add (name)) | |
668 | return; | |
575bfb00 | 669 | |
b655c310 | 670 | cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); |
c87ceb13 | 671 | } |
00e8de68 | 672 | |
b655c310 | 673 | /* Create a binding_entry object for (NAME, TYPE). */ |
00e8de68 | 674 | |
b655c310 NS |
675 | static inline binding_entry |
676 | binding_entry_make (tree name, tree type) | |
00e8de68 | 677 | { |
b655c310 | 678 | binding_entry entry; |
89bd2c03 | 679 | |
b655c310 | 680 | if (free_binding_entry) |
00e8de68 | 681 | { |
b655c310 NS |
682 | entry = free_binding_entry; |
683 | free_binding_entry = entry->chain; | |
00e8de68 | 684 | } |
c8094d83 | 685 | else |
b655c310 | 686 | entry = ggc_alloc<binding_entry_s> (); |
00e8de68 | 687 | |
b655c310 NS |
688 | entry->name = name; |
689 | entry->type = type; | |
690 | entry->chain = NULL; | |
a5e6b29b | 691 | |
b655c310 NS |
692 | return entry; |
693 | } | |
a5e6b29b | 694 | |
b655c310 NS |
695 | /* Put ENTRY back on the free list. */ |
696 | #if 0 | |
697 | static inline void | |
698 | binding_entry_free (binding_entry entry) | |
a5e6b29b | 699 | { |
b655c310 NS |
700 | entry->name = NULL; |
701 | entry->type = NULL; | |
702 | entry->chain = free_binding_entry; | |
703 | free_binding_entry = entry; | |
704 | } | |
705 | #endif | |
a5e6b29b | 706 | |
b655c310 NS |
707 | /* The datatype used to implement the mapping from names to types at |
708 | a given scope. */ | |
709 | struct GTY(()) binding_table_s { | |
710 | /* Array of chains of "binding_entry"s */ | |
711 | binding_entry * GTY((length ("%h.chain_count"))) chain; | |
2a50edcd | 712 | |
b655c310 NS |
713 | /* The number of chains in this table. This is the length of the |
714 | member "chain" considered as an array. */ | |
715 | size_t chain_count; | |
a5e6b29b | 716 | |
b655c310 NS |
717 | /* Number of "binding_entry"s in this table. */ |
718 | size_t entry_count; | |
719 | }; | |
a5e6b29b | 720 | |
b655c310 | 721 | /* Construct TABLE with an initial CHAIN_COUNT. */ |
a5e6b29b | 722 | |
b655c310 NS |
723 | static inline void |
724 | binding_table_construct (binding_table table, size_t chain_count) | |
725 | { | |
726 | table->chain_count = chain_count; | |
727 | table->entry_count = 0; | |
728 | table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count); | |
729 | } | |
a5e6b29b | 730 | |
b655c310 NS |
731 | /* Make TABLE's entries ready for reuse. */ |
732 | #if 0 | |
733 | static void | |
734 | binding_table_free (binding_table table) | |
735 | { | |
736 | size_t i; | |
737 | size_t count; | |
a5e6b29b | 738 | |
b655c310 NS |
739 | if (table == NULL) |
740 | return; | |
a5e6b29b | 741 | |
b655c310 NS |
742 | for (i = 0, count = table->chain_count; i < count; ++i) |
743 | { | |
744 | binding_entry temp = table->chain[i]; | |
745 | while (temp != NULL) | |
a5e6b29b | 746 | { |
b655c310 NS |
747 | binding_entry entry = temp; |
748 | temp = entry->chain; | |
749 | binding_entry_free (entry); | |
a5e6b29b | 750 | } |
b655c310 NS |
751 | table->chain[i] = NULL; |
752 | } | |
753 | table->entry_count = 0; | |
754 | } | |
755 | #endif | |
a5e6b29b | 756 | |
b655c310 | 757 | /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ |
a5e6b29b | 758 | |
b655c310 NS |
759 | static inline binding_table |
760 | binding_table_new (size_t chain_count) | |
761 | { | |
762 | binding_table table = ggc_alloc<binding_table_s> (); | |
763 | table->chain = NULL; | |
764 | binding_table_construct (table, chain_count); | |
765 | return table; | |
766 | } | |
a5e6b29b | 767 | |
b655c310 | 768 | /* Expand TABLE to twice its current chain_count. */ |
a5e6b29b | 769 | |
b655c310 NS |
770 | static void |
771 | binding_table_expand (binding_table table) | |
772 | { | |
773 | const size_t old_chain_count = table->chain_count; | |
774 | const size_t old_entry_count = table->entry_count; | |
775 | const size_t new_chain_count = 2 * old_chain_count; | |
776 | binding_entry *old_chains = table->chain; | |
777 | size_t i; | |
778 | ||
779 | binding_table_construct (table, new_chain_count); | |
780 | for (i = 0; i < old_chain_count; ++i) | |
781 | { | |
782 | binding_entry entry = old_chains[i]; | |
783 | for (; entry != NULL; entry = old_chains[i]) | |
a5e6b29b | 784 | { |
b655c310 NS |
785 | const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); |
786 | const size_t j = ENTRY_INDEX (hash, new_chain_count); | |
10827cd8 | 787 | |
b655c310 NS |
788 | old_chains[i] = entry->chain; |
789 | entry->chain = table->chain[j]; | |
790 | table->chain[j] = entry; | |
791 | } | |
792 | } | |
793 | table->entry_count = old_entry_count; | |
794 | } | |
10827cd8 | 795 | |
b655c310 | 796 | /* Insert a binding for NAME to TYPE into TABLE. */ |
10827cd8 | 797 | |
b655c310 NS |
798 | static void |
799 | binding_table_insert (binding_table table, tree name, tree type) | |
800 | { | |
801 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
802 | const size_t i = ENTRY_INDEX (hash, table->chain_count); | |
803 | binding_entry entry = binding_entry_make (name, type); | |
c8094d83 | 804 | |
b655c310 NS |
805 | entry->chain = table->chain[i]; |
806 | table->chain[i] = entry; | |
807 | ++table->entry_count; | |
b1a19c7c | 808 | |
b655c310 NS |
809 | if (3 * table->chain_count < 5 * table->entry_count) |
810 | binding_table_expand (table); | |
811 | } | |
a5e6b29b | 812 | |
b655c310 | 813 | /* Return the binding_entry, if any, that maps NAME. */ |
c8094d83 | 814 | |
b655c310 NS |
815 | binding_entry |
816 | binding_table_find (binding_table table, tree name) | |
817 | { | |
818 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
819 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
c8094d83 | 820 | |
b655c310 NS |
821 | while (entry != NULL && entry->name != name) |
822 | entry = entry->chain; | |
a5e6b29b | 823 | |
b655c310 NS |
824 | return entry; |
825 | } | |
ecba6c56 | 826 | |
b655c310 | 827 | /* Apply PROC -- with DATA -- to all entries in TABLE. */ |
a5e6b29b | 828 | |
b655c310 NS |
829 | void |
830 | binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) | |
831 | { | |
832 | size_t chain_count; | |
833 | size_t i; | |
a5e6b29b | 834 | |
b655c310 NS |
835 | if (!table) |
836 | return; | |
a5e6b29b | 837 | |
b655c310 NS |
838 | chain_count = table->chain_count; |
839 | for (i = 0; i < chain_count; ++i) | |
840 | { | |
841 | binding_entry entry = table->chain[i]; | |
842 | for (; entry != NULL; entry = entry->chain) | |
843 | proc (entry, data); | |
844 | } | |
845 | } | |
846 | \f | |
847 | #ifndef ENABLE_SCOPE_CHECKING | |
848 | # define ENABLE_SCOPE_CHECKING 0 | |
849 | #else | |
850 | # define ENABLE_SCOPE_CHECKING 1 | |
851 | #endif | |
199b7a35 | 852 | |
b655c310 | 853 | /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ |
f7cbd40e | 854 | |
b655c310 | 855 | static GTY((deletable)) cxx_binding *free_bindings; |
f7cbd40e | 856 | |
b655c310 NS |
857 | /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS |
858 | field to NULL. */ | |
d0940d56 | 859 | |
b655c310 NS |
860 | static inline void |
861 | cxx_binding_init (cxx_binding *binding, tree value, tree type) | |
862 | { | |
863 | binding->value = value; | |
864 | binding->type = type; | |
865 | binding->previous = NULL; | |
866 | } | |
a5e6b29b | 867 | |
b655c310 | 868 | /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ |
3797cb21 | 869 | |
b655c310 NS |
870 | static cxx_binding * |
871 | cxx_binding_make (tree value, tree type) | |
872 | { | |
873 | cxx_binding *binding; | |
874 | if (free_bindings) | |
875 | { | |
876 | binding = free_bindings; | |
877 | free_bindings = binding->previous; | |
878 | } | |
879 | else | |
880 | binding = ggc_alloc<cxx_binding> (); | |
a5e6b29b | 881 | |
b655c310 | 882 | cxx_binding_init (binding, value, type); |
a5e6b29b | 883 | |
b655c310 NS |
884 | return binding; |
885 | } | |
a5e6b29b | 886 | |
b655c310 | 887 | /* Put BINDING back on the free list. */ |
a5e6b29b | 888 | |
b655c310 NS |
889 | static inline void |
890 | cxx_binding_free (cxx_binding *binding) | |
891 | { | |
892 | binding->scope = NULL; | |
893 | binding->previous = free_bindings; | |
894 | free_bindings = binding; | |
895 | } | |
a5e6b29b | 896 | |
b655c310 NS |
897 | /* Create a new binding for NAME (with the indicated VALUE and TYPE |
898 | bindings) in the class scope indicated by SCOPE. */ | |
a5e6b29b | 899 | |
b655c310 NS |
900 | static cxx_binding * |
901 | new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) | |
902 | { | |
903 | cp_class_binding cb = {cxx_binding_make (value, type), name}; | |
904 | cxx_binding *binding = cb.base; | |
905 | vec_safe_push (scope->class_shadowed, cb); | |
906 | binding->scope = scope; | |
907 | return binding; | |
908 | } | |
a5e6b29b | 909 | |
b655c310 NS |
910 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
911 | level at which this declaration is being bound. */ | |
a5e6b29b | 912 | |
b655c310 NS |
913 | void |
914 | push_binding (tree id, tree decl, cp_binding_level* level) | |
915 | { | |
916 | cxx_binding *binding; | |
a5e6b29b | 917 | |
b655c310 NS |
918 | if (level != class_binding_level) |
919 | { | |
920 | binding = cxx_binding_make (decl, NULL_TREE); | |
921 | binding->scope = level; | |
922 | } | |
923 | else | |
924 | binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); | |
a5e6b29b | 925 | |
b655c310 NS |
926 | /* Now, fill in the binding information. */ |
927 | binding->previous = IDENTIFIER_BINDING (id); | |
928 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
929 | LOCAL_BINDING_P (binding) = (level != class_binding_level); | |
a5e6b29b | 930 | |
b655c310 NS |
931 | /* And put it on the front of the list of bindings for ID. */ |
932 | IDENTIFIER_BINDING (id) = binding; | |
575bfb00 LC |
933 | } |
934 | ||
b655c310 NS |
935 | /* Remove the binding for DECL which should be the innermost binding |
936 | for ID. */ | |
575bfb00 | 937 | |
b655c310 | 938 | void |
9c82d7b6 | 939 | pop_local_binding (tree id, tree decl) |
575bfb00 | 940 | { |
b655c310 | 941 | cxx_binding *binding; |
d63d5d0c | 942 | |
b655c310 NS |
943 | if (id == NULL_TREE) |
944 | /* It's easiest to write the loops that call this function without | |
945 | checking whether or not the entities involved have names. We | |
946 | get here for such an entity. */ | |
947 | return; | |
d63d5d0c | 948 | |
b655c310 NS |
949 | /* Get the innermost binding for ID. */ |
950 | binding = IDENTIFIER_BINDING (id); | |
a5e6b29b | 951 | |
b655c310 NS |
952 | /* The name should be bound. */ |
953 | gcc_assert (binding != NULL); | |
a5e6b29b | 954 | |
b655c310 NS |
955 | /* The DECL will be either the ordinary binding or the type |
956 | binding for this identifier. Remove that binding. */ | |
957 | if (binding->value == decl) | |
958 | binding->value = NULL_TREE; | |
a5e6b29b | 959 | else |
b655c310 NS |
960 | { |
961 | gcc_assert (binding->type == decl); | |
962 | binding->type = NULL_TREE; | |
963 | } | |
00e8de68 | 964 | |
b655c310 | 965 | if (!binding->value && !binding->type) |
00e8de68 | 966 | { |
b655c310 NS |
967 | /* We're completely done with the innermost binding for this |
968 | identifier. Unhook it from the list of bindings. */ | |
969 | IDENTIFIER_BINDING (id) = binding->previous; | |
970 | ||
971 | /* Add it to the free list. */ | |
972 | cxx_binding_free (binding); | |
00e8de68 | 973 | } |
b655c310 | 974 | } |
00e8de68 | 975 | |
b655c310 NS |
976 | /* Remove the bindings for the decls of the current level and leave |
977 | the current scope. */ | |
00e8de68 | 978 | |
b655c310 NS |
979 | void |
980 | pop_bindings_and_leave_scope (void) | |
981 | { | |
9c82d7b6 NS |
982 | for (tree t = get_local_decls (); t; t = DECL_CHAIN (t)) |
983 | pop_local_binding (DECL_NAME (t), t); | |
b655c310 | 984 | leave_scope (); |
00e8de68 | 985 | } |
a5e6b29b | 986 | |
b655c310 NS |
987 | /* Strip non dependent using declarations. If DECL is dependent, |
988 | surreptitiously create a typename_type and return it. */ | |
a5e6b29b GDR |
989 | |
990 | tree | |
b655c310 | 991 | strip_using_decl (tree decl) |
a5e6b29b | 992 | { |
b655c310 NS |
993 | if (decl == NULL_TREE) |
994 | return NULL_TREE; | |
a5e6b29b | 995 | |
b655c310 NS |
996 | while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) |
997 | decl = USING_DECL_DECLS (decl); | |
a5e6b29b | 998 | |
b655c310 NS |
999 | if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) |
1000 | && USING_DECL_TYPENAME_P (decl)) | |
a5e6b29b | 1001 | { |
b655c310 NS |
1002 | /* We have found a type introduced by a using |
1003 | declaration at class scope that refers to a dependent | |
1004 | type. | |
1005 | ||
1006 | using typename :: [opt] nested-name-specifier unqualified-id ; | |
1007 | */ | |
1008 | decl = make_typename_type (TREE_TYPE (decl), | |
1009 | DECL_NAME (decl), | |
1010 | typename_type, tf_error); | |
1011 | if (decl != error_mark_node) | |
1012 | decl = TYPE_NAME (decl); | |
a5e6b29b GDR |
1013 | } |
1014 | ||
b655c310 NS |
1015 | return decl; |
1016 | } | |
a5e6b29b | 1017 | |
b655c310 NS |
1018 | /* BINDING records an existing declaration for a name in the current scope. |
1019 | But, DECL is another declaration for that same identifier in the | |
1020 | same scope. This is the `struct stat' hack whereby a non-typedef | |
1021 | class name or enum-name can be bound at the same level as some other | |
1022 | kind of entity. | |
1023 | 3.3.7/1 | |
ae5cbc33 | 1024 | |
b655c310 NS |
1025 | A class name (9.1) or enumeration name (7.2) can be hidden by the |
1026 | name of an object, function, or enumerator declared in the same scope. | |
1027 | If a class or enumeration name and an object, function, or enumerator | |
1028 | are declared in the same scope (in any order) with the same name, the | |
1029 | class or enumeration name is hidden wherever the object, function, or | |
1030 | enumerator name is visible. | |
ae5cbc33 | 1031 | |
b655c310 NS |
1032 | It's the responsibility of the caller to check that |
1033 | inserting this name is valid here. Returns nonzero if the new binding | |
1034 | was successful. */ | |
1035 | ||
1036 | static bool | |
1037 | supplement_binding_1 (cxx_binding *binding, tree decl) | |
1038 | { | |
1039 | tree bval = binding->value; | |
1040 | bool ok = true; | |
1041 | tree target_bval = strip_using_decl (bval); | |
1042 | tree target_decl = strip_using_decl (decl); | |
1043 | ||
1044 | if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) | |
1045 | && target_decl != target_bval | |
1046 | && (TREE_CODE (target_bval) != TYPE_DECL | |
1047 | /* We allow pushing an enum multiple times in a class | |
1048 | template in order to handle late matching of underlying | |
1049 | type on an opaque-enum-declaration followed by an | |
1050 | enum-specifier. */ | |
1051 | || (processing_template_decl | |
1052 | && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE | |
1053 | && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE | |
1054 | && (dependent_type_p (ENUM_UNDERLYING_TYPE | |
1055 | (TREE_TYPE (target_decl))) | |
1056 | || dependent_type_p (ENUM_UNDERLYING_TYPE | |
1057 | (TREE_TYPE (target_bval))))))) | |
1058 | /* The new name is the type name. */ | |
1059 | binding->type = decl; | |
1060 | else if (/* TARGET_BVAL is null when push_class_level_binding moves | |
1061 | an inherited type-binding out of the way to make room | |
1062 | for a new value binding. */ | |
1063 | !target_bval | |
1064 | /* TARGET_BVAL is error_mark_node when TARGET_DECL's name | |
1065 | has been used in a non-class scope prior declaration. | |
1066 | In that case, we should have already issued a | |
1067 | diagnostic; for graceful error recovery purpose, pretend | |
1068 | this was the intended declaration for that name. */ | |
1069 | || target_bval == error_mark_node | |
1070 | /* If TARGET_BVAL is anticipated but has not yet been | |
1071 | declared, pretend it is not there at all. */ | |
1072 | || (TREE_CODE (target_bval) == FUNCTION_DECL | |
1073 | && DECL_ANTICIPATED (target_bval) | |
1074 | && !DECL_HIDDEN_FRIEND_P (target_bval))) | |
1075 | binding->value = decl; | |
1076 | else if (TREE_CODE (target_bval) == TYPE_DECL | |
1077 | && DECL_ARTIFICIAL (target_bval) | |
1078 | && target_decl != target_bval | |
1079 | && (TREE_CODE (target_decl) != TYPE_DECL | |
1080 | || same_type_p (TREE_TYPE (target_decl), | |
1081 | TREE_TYPE (target_bval)))) | |
a5e6b29b | 1082 | { |
b655c310 NS |
1083 | /* The old binding was a type name. It was placed in |
1084 | VALUE field because it was thought, at the point it was | |
1085 | declared, to be the only entity with such a name. Move the | |
1086 | type name into the type slot; it is now hidden by the new | |
1087 | binding. */ | |
1088 | binding->type = bval; | |
1089 | binding->value = decl; | |
1090 | binding->value_is_inherited = false; | |
a5e6b29b | 1091 | } |
b655c310 NS |
1092 | else if (TREE_CODE (target_bval) == TYPE_DECL |
1093 | && TREE_CODE (target_decl) == TYPE_DECL | |
1094 | && DECL_NAME (target_decl) == DECL_NAME (target_bval) | |
1095 | && binding->scope->kind != sk_class | |
1096 | && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) | |
1097 | /* If either type involves template parameters, we must | |
1098 | wait until instantiation. */ | |
1099 | || uses_template_parms (TREE_TYPE (target_decl)) | |
1100 | || uses_template_parms (TREE_TYPE (target_bval)))) | |
1101 | /* We have two typedef-names, both naming the same type to have | |
1102 | the same name. In general, this is OK because of: | |
a5e6b29b | 1103 | |
b655c310 | 1104 | [dcl.typedef] |
00e8de68 | 1105 | |
b655c310 NS |
1106 | In a given scope, a typedef specifier can be used to redefine |
1107 | the name of any type declared in that scope to refer to the | |
1108 | type to which it already refers. | |
00e8de68 | 1109 | |
b655c310 NS |
1110 | However, in class scopes, this rule does not apply due to the |
1111 | stricter language in [class.mem] prohibiting redeclarations of | |
1112 | members. */ | |
1113 | ok = false; | |
1114 | /* There can be two block-scope declarations of the same variable, | |
1115 | so long as they are `extern' declarations. However, there cannot | |
1116 | be two declarations of the same static data member: | |
00e8de68 | 1117 | |
b655c310 | 1118 | [class.mem] |
00e8de68 | 1119 | |
b655c310 NS |
1120 | A member shall not be declared twice in the |
1121 | member-specification. */ | |
1122 | else if (VAR_P (target_decl) | |
1123 | && VAR_P (target_bval) | |
1124 | && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) | |
1125 | && !DECL_CLASS_SCOPE_P (target_decl)) | |
1126 | { | |
1127 | duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); | |
1128 | ok = false; | |
1129 | } | |
1130 | else if (TREE_CODE (decl) == NAMESPACE_DECL | |
1131 | && TREE_CODE (bval) == NAMESPACE_DECL | |
1132 | && DECL_NAMESPACE_ALIAS (decl) | |
1133 | && DECL_NAMESPACE_ALIAS (bval) | |
1134 | && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) | |
1135 | /* [namespace.alias] | |
00e8de68 | 1136 | |
b655c310 NS |
1137 | In a declarative region, a namespace-alias-definition can be |
1138 | used to redefine a namespace-alias declared in that declarative | |
1139 | region to refer only to the namespace to which it already | |
1140 | refers. */ | |
1141 | ok = false; | |
1142 | else if (maybe_remove_implicit_alias (bval)) | |
1143 | { | |
1144 | /* There was a mangling compatibility alias using this mangled name, | |
1145 | but now we have a real decl that wants to use it instead. */ | |
1146 | binding->value = decl; | |
1147 | } | |
1148 | else | |
1149 | { | |
1150 | if (!error_operand_p (bval)) | |
1151 | diagnose_name_conflict (decl, bval); | |
1152 | ok = false; | |
1153 | } | |
00e8de68 | 1154 | |
b655c310 | 1155 | return ok; |
00e8de68 GDR |
1156 | } |
1157 | ||
b655c310 NS |
1158 | /* Diagnose a name conflict between DECL and BVAL. */ |
1159 | ||
00e8de68 | 1160 | static void |
b655c310 NS |
1161 | diagnose_name_conflict (tree decl, tree bval) |
1162 | { | |
1163 | if (TREE_CODE (decl) == TREE_CODE (bval) | |
1164 | && (TREE_CODE (decl) != TYPE_DECL | |
1165 | || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval)) | |
1166 | || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval))) | |
1167 | && !is_overloaded_fn (decl)) | |
1168 | error ("redeclaration of %q#D", decl); | |
00e8de68 | 1169 | else |
b655c310 NS |
1170 | error ("%q#D conflicts with a previous declaration", decl); |
1171 | ||
1172 | inform (location_of (bval), "previous declaration %q#D", bval); | |
00e8de68 GDR |
1173 | } |
1174 | ||
b655c310 | 1175 | /* Wrapper for supplement_binding_1. */ |
00e8de68 | 1176 | |
b655c310 NS |
1177 | static bool |
1178 | supplement_binding (cxx_binding *binding, tree decl) | |
00e8de68 | 1179 | { |
b655c310 NS |
1180 | bool ret; |
1181 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
1182 | ret = supplement_binding_1 (binding, decl); | |
1183 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
1184 | return ret; | |
00e8de68 GDR |
1185 | } |
1186 | ||
b655c310 NS |
1187 | /* Record a decl-node X as belonging to the current lexical scope. |
1188 | Check for errors (such as an incompatible declaration for the same | |
1189 | name already seen in the same scope). IS_FRIEND is true if X is | |
1190 | declared as a friend. | |
00e8de68 | 1191 | |
b655c310 NS |
1192 | Returns either X or an old decl for the same name. |
1193 | If an old decl is returned, it may have been smashed | |
1194 | to agree with what X says. */ | |
89b578be | 1195 | |
b655c310 NS |
1196 | static tree |
1197 | pushdecl_maybe_friend_1 (tree x, bool is_friend) | |
89b578be | 1198 | { |
b655c310 NS |
1199 | tree t; |
1200 | tree name; | |
1201 | int need_new_binding; | |
89b578be | 1202 | |
b655c310 NS |
1203 | if (x == error_mark_node) |
1204 | return error_mark_node; | |
00e8de68 | 1205 | |
b655c310 | 1206 | need_new_binding = 1; |
c8094d83 | 1207 | |
b655c310 NS |
1208 | if (DECL_TEMPLATE_PARM_P (x)) |
1209 | /* Template parameters have no context; they are not X::T even | |
1210 | when declared within a class or namespace. */ | |
1211 | ; | |
1212 | else | |
00e8de68 | 1213 | { |
b655c310 NS |
1214 | if (current_function_decl && x != current_function_decl |
1215 | /* A local declaration for a function doesn't constitute | |
1216 | nesting. */ | |
1217 | && TREE_CODE (x) != FUNCTION_DECL | |
1218 | /* A local declaration for an `extern' variable is in the | |
1219 | scope of the current namespace, not the current | |
1220 | function. */ | |
1221 | && !(VAR_P (x) && DECL_EXTERNAL (x)) | |
1222 | /* When parsing the parameter list of a function declarator, | |
1223 | don't set DECL_CONTEXT to an enclosing function. When we | |
1224 | push the PARM_DECLs in order to process the function body, | |
1225 | current_binding_level->this_entity will be set. */ | |
1226 | && !(TREE_CODE (x) == PARM_DECL | |
1227 | && current_binding_level->kind == sk_function_parms | |
1228 | && current_binding_level->this_entity == NULL) | |
1229 | && !DECL_CONTEXT (x)) | |
1230 | DECL_CONTEXT (x) = current_function_decl; | |
1231 | ||
1232 | /* If this is the declaration for a namespace-scope function, | |
1233 | but the declaration itself is in a local scope, mark the | |
1234 | declaration. */ | |
1235 | if (TREE_CODE (x) == FUNCTION_DECL | |
1236 | && DECL_NAMESPACE_SCOPE_P (x) | |
1237 | && current_function_decl | |
1238 | && x != current_function_decl) | |
1239 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
00e8de68 | 1240 | } |
00e8de68 | 1241 | |
b655c310 NS |
1242 | name = DECL_NAME (x); |
1243 | if (name) | |
00e8de68 | 1244 | { |
b655c310 | 1245 | int different_binding_level = 0; |
c8094d83 | 1246 | |
b655c310 NS |
1247 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
1248 | name = TREE_OPERAND (name, 0); | |
00e8de68 | 1249 | |
b655c310 NS |
1250 | /* In case this decl was explicitly namespace-qualified, look it |
1251 | up in its namespace context. */ | |
1252 | if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ()) | |
06aa5490 | 1253 | t = get_namespace_binding (DECL_CONTEXT (x), name); |
b655c310 NS |
1254 | else |
1255 | t = lookup_name_innermost_nonclass_level (name); | |
00e8de68 | 1256 | |
b655c310 NS |
1257 | /* [basic.link] If there is a visible declaration of an entity |
1258 | with linkage having the same name and type, ignoring entities | |
1259 | declared outside the innermost enclosing namespace scope, the | |
1260 | block scope declaration declares that same entity and | |
1261 | receives the linkage of the previous declaration. */ | |
1262 | if (! t && current_function_decl && x != current_function_decl | |
1263 | && VAR_OR_FUNCTION_DECL_P (x) | |
1264 | && DECL_EXTERNAL (x)) | |
1265 | { | |
1266 | /* Look in block scope. */ | |
1267 | t = innermost_non_namespace_value (name); | |
1268 | /* Or in the innermost namespace. */ | |
1269 | if (! t) | |
06aa5490 | 1270 | t = get_namespace_binding (DECL_CONTEXT (x), name); |
b655c310 NS |
1271 | /* Does it have linkage? Note that if this isn't a DECL, it's an |
1272 | OVERLOAD, which is OK. */ | |
1273 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
1274 | t = NULL_TREE; | |
1275 | if (t) | |
1276 | different_binding_level = 1; | |
1277 | } | |
00e8de68 | 1278 | |
b655c310 NS |
1279 | /* If we are declaring a function, and the result of name-lookup |
1280 | was an OVERLOAD, look for an overloaded instance that is | |
1281 | actually the same as the function we are declaring. (If | |
1282 | there is one, we have to merge our declaration with the | |
1283 | previous declaration.) */ | |
1284 | if (t && TREE_CODE (t) == OVERLOAD) | |
1285 | { | |
1286 | tree match; | |
00e8de68 | 1287 | |
b655c310 NS |
1288 | if (TREE_CODE (x) == FUNCTION_DECL) |
1289 | for (match = t; match; match = OVL_NEXT (match)) | |
1290 | { | |
1291 | if (decls_match (OVL_CURRENT (match), x)) | |
1292 | break; | |
1293 | } | |
1294 | else | |
1295 | /* Just choose one. */ | |
1296 | match = t; | |
1297 | ||
1298 | if (match) | |
1299 | t = OVL_CURRENT (match); | |
1300 | else | |
1301 | t = NULL_TREE; | |
1302 | } | |
1303 | ||
1304 | if (t && t != error_mark_node) | |
1305 | { | |
1306 | if (different_binding_level) | |
1307 | { | |
1308 | if (decls_match (x, t)) | |
1309 | /* The standard only says that the local extern | |
1310 | inherits linkage from the previous decl; in | |
1311 | particular, default args are not shared. Add | |
1312 | the decl into a hash table to make sure only | |
1313 | the previous decl in this case is seen by the | |
1314 | middle end. */ | |
1315 | { | |
1316 | struct cxx_int_tree_map *h; | |
1317 | ||
1318 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
1319 | ||
1320 | if (cp_function_chain->extern_decl_map == NULL) | |
1321 | cp_function_chain->extern_decl_map | |
1322 | = hash_table<cxx_int_tree_map_hasher>::create_ggc (20); | |
1323 | ||
1324 | h = ggc_alloc<cxx_int_tree_map> (); | |
1325 | h->uid = DECL_UID (x); | |
1326 | h->to = t; | |
1327 | cxx_int_tree_map **loc = cp_function_chain->extern_decl_map | |
1328 | ->find_slot (h, INSERT); | |
1329 | *loc = h; | |
1330 | } | |
1331 | } | |
1332 | else if (TREE_CODE (t) == PARM_DECL) | |
1333 | { | |
1334 | /* Check for duplicate params. */ | |
1335 | tree d = duplicate_decls (x, t, is_friend); | |
1336 | if (d) | |
1337 | return d; | |
1338 | } | |
1339 | else if ((DECL_EXTERN_C_FUNCTION_P (x) | |
1340 | || DECL_FUNCTION_TEMPLATE_P (x)) | |
1341 | && is_overloaded_fn (t)) | |
1342 | /* Don't do anything just yet. */; | |
1343 | else if (t == wchar_decl_node) | |
1344 | { | |
1345 | if (! DECL_IN_SYSTEM_HEADER (x)) | |
1346 | pedwarn (input_location, OPT_Wpedantic, "redeclaration of %<wchar_t%> as %qT", | |
1347 | TREE_TYPE (x)); | |
1348 | ||
1349 | /* Throw away the redeclaration. */ | |
1350 | return t; | |
1351 | } | |
1352 | else | |
1353 | { | |
1354 | tree olddecl = duplicate_decls (x, t, is_friend); | |
1355 | ||
1356 | /* If the redeclaration failed, we can stop at this | |
1357 | point. */ | |
1358 | if (olddecl == error_mark_node) | |
1359 | return error_mark_node; | |
00e8de68 | 1360 | |
b655c310 NS |
1361 | if (olddecl) |
1362 | { | |
1363 | if (TREE_CODE (t) == TYPE_DECL) | |
1364 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
00e8de68 | 1365 | |
b655c310 NS |
1366 | return t; |
1367 | } | |
1368 | else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL) | |
1369 | { | |
1370 | /* A redeclaration of main, but not a duplicate of the | |
1371 | previous one. | |
00e8de68 | 1372 | |
b655c310 | 1373 | [basic.start.main] |
00e8de68 | 1374 | |
b655c310 NS |
1375 | This function shall not be overloaded. */ |
1376 | error ("invalid redeclaration of %q+D", t); | |
1377 | error ("as %qD", x); | |
1378 | /* We don't try to push this declaration since that | |
1379 | causes a crash. */ | |
1380 | return x; | |
1381 | } | |
1382 | } | |
1383 | } | |
c8094d83 | 1384 | |
b655c310 NS |
1385 | /* If x has C linkage-specification, (extern "C"), |
1386 | lookup its binding, in case it's already bound to an object. | |
1387 | The lookup is done in all namespaces. | |
1388 | If we find an existing binding, make sure it has the same | |
1389 | exception specification as x, otherwise, bail in error [7.5, 7.6]. */ | |
1390 | if ((TREE_CODE (x) == FUNCTION_DECL) | |
1391 | && DECL_EXTERN_C_P (x) | |
1392 | /* We should ignore declarations happening in system headers. */ | |
1393 | && !DECL_ARTIFICIAL (x) | |
1394 | && !DECL_IN_SYSTEM_HEADER (x)) | |
1395 | { | |
1396 | tree previous = lookup_extern_c_fun_in_all_ns (x); | |
1397 | if (previous | |
1398 | && !DECL_ARTIFICIAL (previous) | |
1399 | && !DECL_IN_SYSTEM_HEADER (previous) | |
1400 | && DECL_CONTEXT (previous) != DECL_CONTEXT (x)) | |
1401 | { | |
1402 | /* In case either x or previous is declared to throw an exception, | |
1403 | make sure both exception specifications are equal. */ | |
1404 | if (decls_match (x, previous)) | |
1405 | { | |
1406 | tree x_exception_spec = NULL_TREE; | |
1407 | tree previous_exception_spec = NULL_TREE; | |
00e8de68 | 1408 | |
b655c310 NS |
1409 | x_exception_spec = |
1410 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x)); | |
1411 | previous_exception_spec = | |
1412 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous)); | |
1413 | if (!comp_except_specs (previous_exception_spec, | |
1414 | x_exception_spec, | |
1415 | ce_normal)) | |
1416 | { | |
1417 | pedwarn (input_location, 0, | |
1418 | "declaration of %q#D with C language linkage", | |
1419 | x); | |
1420 | pedwarn (DECL_SOURCE_LOCATION (previous), 0, | |
1421 | "conflicts with previous declaration %q#D", | |
1422 | previous); | |
1423 | pedwarn (input_location, 0, | |
1424 | "due to different exception specifications"); | |
1425 | return error_mark_node; | |
1426 | } | |
1427 | if (DECL_ASSEMBLER_NAME_SET_P (previous)) | |
1428 | SET_DECL_ASSEMBLER_NAME (x, | |
1429 | DECL_ASSEMBLER_NAME (previous)); | |
1430 | } | |
1431 | else | |
1432 | { | |
1433 | pedwarn (input_location, 0, | |
1434 | "declaration of %q#D with C language linkage", x); | |
1435 | pedwarn (DECL_SOURCE_LOCATION (previous), 0, | |
1436 | "conflicts with previous declaration %q#D", | |
1437 | previous); | |
1438 | } | |
1439 | } | |
1440 | } | |
00e8de68 | 1441 | |
b655c310 | 1442 | check_template_shadow (x); |
00e8de68 | 1443 | |
b655c310 NS |
1444 | /* If this is a function conjured up by the back end, massage it |
1445 | so it looks friendly. */ | |
1446 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) | |
1447 | { | |
1448 | retrofit_lang_decl (x); | |
1449 | SET_DECL_LANGUAGE (x, lang_c); | |
1450 | } | |
5294e4c3 | 1451 | |
b655c310 NS |
1452 | t = x; |
1453 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) | |
1454 | { | |
1455 | t = push_overloaded_decl (x, PUSH_LOCAL, is_friend); | |
1456 | if (!namespace_bindings_p ()) | |
1457 | /* We do not need to create a binding for this name; | |
1458 | push_overloaded_decl will have already done so if | |
1459 | necessary. */ | |
1460 | need_new_binding = 0; | |
1461 | } | |
1462 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) | |
1463 | { | |
1464 | t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend); | |
1465 | if (t == x) | |
1466 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
1467 | } | |
00e8de68 | 1468 | |
b655c310 NS |
1469 | if (DECL_DECLARES_FUNCTION_P (t)) |
1470 | { | |
1471 | check_default_args (t); | |
00e8de68 | 1472 | |
b655c310 NS |
1473 | if (is_friend && t == x && !flag_friend_injection) |
1474 | { | |
1475 | /* This is a new friend declaration of a function or a | |
1476 | function template, so hide it from ordinary function | |
1477 | lookup. */ | |
1478 | DECL_ANTICIPATED (t) = 1; | |
1479 | DECL_HIDDEN_FRIEND_P (t) = 1; | |
1480 | } | |
1481 | } | |
00e8de68 | 1482 | |
b655c310 NS |
1483 | if (t != x || DECL_FUNCTION_TEMPLATE_P (t)) |
1484 | return t; | |
00e8de68 | 1485 | |
b655c310 NS |
1486 | /* If declaring a type as a typedef, copy the type (unless we're |
1487 | at line 0), and install this TYPE_DECL as the new type's typedef | |
1488 | name. See the extensive comment of set_underlying_type (). */ | |
1489 | if (TREE_CODE (x) == TYPE_DECL) | |
1490 | { | |
1491 | tree type = TREE_TYPE (x); | |
00e8de68 | 1492 | |
b655c310 NS |
1493 | if (DECL_IS_BUILTIN (x) |
1494 | || (TREE_TYPE (x) != error_mark_node | |
1495 | && TYPE_NAME (type) != x | |
1496 | /* We don't want to copy the type when all we're | |
1497 | doing is making a TYPE_DECL for the purposes of | |
1498 | inlining. */ | |
1499 | && (!TYPE_NAME (type) | |
1500 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))) | |
1501 | set_underlying_type (x); | |
00e8de68 | 1502 | |
b655c310 NS |
1503 | if (type != error_mark_node |
1504 | && TYPE_IDENTIFIER (type)) | |
1505 | set_identifier_type_value (DECL_NAME (x), x); | |
00e8de68 | 1506 | |
b655c310 NS |
1507 | /* If this is a locally defined typedef in a function that |
1508 | is not a template instantation, record it to implement | |
1509 | -Wunused-local-typedefs. */ | |
1510 | if (!instantiating_current_function_p ()) | |
1511 | record_locally_defined_typedef (x); | |
1512 | } | |
00e8de68 | 1513 | |
b655c310 | 1514 | /* Multiple external decls of the same identifier ought to match. |
00e8de68 | 1515 | |
b655c310 NS |
1516 | We get warnings about inline functions where they are defined. |
1517 | We get warnings about other functions from push_overloaded_decl. | |
00e8de68 | 1518 | |
b655c310 NS |
1519 | Avoid duplicate warnings where they are used. */ |
1520 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) | |
1521 | { | |
1522 | tree decl; | |
00e8de68 | 1523 | |
06aa5490 | 1524 | decl = get_namespace_binding (current_namespace, name); |
b655c310 NS |
1525 | if (decl && TREE_CODE (decl) == OVERLOAD) |
1526 | decl = OVL_FUNCTION (decl); | |
00e8de68 | 1527 | |
b655c310 NS |
1528 | if (decl && decl != error_mark_node |
1529 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
1530 | /* If different sort of thing, we already gave an error. */ | |
1531 | && TREE_CODE (decl) == TREE_CODE (x) | |
1532 | && !comptypes (TREE_TYPE (x), TREE_TYPE (decl), | |
1533 | COMPARE_REDECLARATION)) | |
1534 | { | |
1535 | if (permerror (input_location, "type mismatch with previous " | |
1536 | "external decl of %q#D", x)) | |
1537 | inform (DECL_SOURCE_LOCATION (decl), | |
1538 | "previous external decl of %q#D", decl); | |
1539 | } | |
1540 | } | |
00e8de68 | 1541 | |
b655c310 NS |
1542 | /* This name is new in its binding level. |
1543 | Install the new declaration and return it. */ | |
1544 | if (namespace_bindings_p ()) | |
1545 | { | |
1546 | /* Install a global value. */ | |
00e8de68 | 1547 | |
b655c310 NS |
1548 | /* If the first global decl has external linkage, |
1549 | warn if we later see static one. */ | |
1550 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) | |
1551 | TREE_PUBLIC (name) = 1; | |
00e8de68 | 1552 | |
b655c310 NS |
1553 | /* Bind the name for the entity. */ |
1554 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
1555 | && t != NULL_TREE) | |
1556 | && (TREE_CODE (x) == TYPE_DECL | |
1557 | || VAR_P (x) | |
1558 | || TREE_CODE (x) == NAMESPACE_DECL | |
1559 | || TREE_CODE (x) == CONST_DECL | |
1560 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
4b4b2e58 | 1561 | set_namespace_binding (name, current_namespace, x); |
00e8de68 | 1562 | |
b655c310 NS |
1563 | /* If new decl is `static' and an `extern' was seen previously, |
1564 | warn about it. */ | |
1565 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) | |
1566 | warn_extern_redeclared_static (x, t); | |
1567 | } | |
1568 | else | |
1569 | { | |
1570 | /* Here to install a non-global value. */ | |
06aa5490 | 1571 | tree oldglobal = get_namespace_binding (current_namespace, name); |
b655c310 NS |
1572 | tree oldlocal = NULL_TREE; |
1573 | cp_binding_level *oldscope = NULL; | |
1574 | cxx_binding *oldbinding = outer_binding (name, NULL, true); | |
1575 | if (oldbinding) | |
1576 | { | |
1577 | oldlocal = oldbinding->value; | |
1578 | oldscope = oldbinding->scope; | |
1579 | } | |
00e8de68 | 1580 | |
b655c310 NS |
1581 | if (need_new_binding) |
1582 | { | |
1583 | push_local_binding (name, x, 0); | |
1584 | /* Because push_local_binding will hook X on to the | |
1585 | current_binding_level's name list, we don't want to | |
1586 | do that again below. */ | |
1587 | need_new_binding = 0; | |
1588 | } | |
fdf03377 | 1589 | |
b655c310 NS |
1590 | /* If this is a TYPE_DECL, push it into the type value slot. */ |
1591 | if (TREE_CODE (x) == TYPE_DECL) | |
1592 | set_identifier_type_value (name, x); | |
fdf03377 | 1593 | |
b655c310 NS |
1594 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
1595 | we won't think this is a type. The C struct hack doesn't | |
1596 | go through namespaces. */ | |
1597 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
1598 | set_identifier_type_value (name, NULL_TREE); | |
00e8de68 | 1599 | |
b655c310 NS |
1600 | if (oldlocal) |
1601 | { | |
1602 | tree d = oldlocal; | |
00e8de68 | 1603 | |
b655c310 NS |
1604 | while (oldlocal |
1605 | && VAR_P (oldlocal) | |
1606 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
1607 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); | |
00e8de68 | 1608 | |
b655c310 | 1609 | if (oldlocal == NULL_TREE) |
4b4b2e58 | 1610 | oldlocal |
06aa5490 | 1611 | = get_namespace_binding (current_namespace, DECL_NAME (d)); |
b655c310 | 1612 | } |
00e8de68 | 1613 | |
b655c310 NS |
1614 | /* If this is an extern function declaration, see if we |
1615 | have a global definition or declaration for the function. */ | |
1616 | if (oldlocal == NULL_TREE | |
1617 | && DECL_EXTERNAL (x) | |
1618 | && oldglobal != NULL_TREE | |
1619 | && TREE_CODE (x) == FUNCTION_DECL | |
1620 | && TREE_CODE (oldglobal) == FUNCTION_DECL) | |
1621 | { | |
1622 | /* We have one. Their types must agree. */ | |
1623 | if (decls_match (x, oldglobal)) | |
1624 | /* OK */; | |
1625 | else | |
1626 | { | |
1627 | warning (0, "extern declaration of %q#D doesn%'t match", x); | |
1628 | warning_at (DECL_SOURCE_LOCATION (oldglobal), 0, | |
1629 | "global declaration %q#D", oldglobal); | |
1630 | } | |
1631 | } | |
1632 | /* If we have a local external declaration, | |
1633 | and no file-scope declaration has yet been seen, | |
1634 | then if we later have a file-scope decl it must not be static. */ | |
1635 | if (oldlocal == NULL_TREE | |
1636 | && oldglobal == NULL_TREE | |
1637 | && DECL_EXTERNAL (x) | |
1638 | && TREE_PUBLIC (x)) | |
1639 | TREE_PUBLIC (name) = 1; | |
00e8de68 | 1640 | |
b655c310 NS |
1641 | /* Don't complain about the parms we push and then pop |
1642 | while tentatively parsing a function declarator. */ | |
1643 | if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE) | |
1644 | /* Ignore. */; | |
00e8de68 | 1645 | |
b655c310 NS |
1646 | /* Warn if shadowing an argument at the top level of the body. */ |
1647 | else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) | |
1648 | /* Inline decls shadow nothing. */ | |
1649 | && !DECL_FROM_INLINE (x) | |
1650 | && (TREE_CODE (oldlocal) == PARM_DECL | |
1651 | || VAR_P (oldlocal) | |
1652 | /* If the old decl is a type decl, only warn if the | |
1653 | old decl is an explicit typedef or if both the old | |
1654 | and new decls are type decls. */ | |
1655 | || (TREE_CODE (oldlocal) == TYPE_DECL | |
1656 | && (!DECL_ARTIFICIAL (oldlocal) | |
1657 | || TREE_CODE (x) == TYPE_DECL))) | |
1658 | /* Don't check for internally generated vars unless | |
1659 | it's an implicit typedef (see create_implicit_typedef | |
1660 | in decl.c) or anonymous union variable. */ | |
1661 | && (!DECL_ARTIFICIAL (x) | |
1662 | || DECL_IMPLICIT_TYPEDEF_P (x) | |
1663 | || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x)))) | |
1664 | { | |
1665 | bool nowarn = false; | |
00e8de68 | 1666 | |
b655c310 NS |
1667 | /* Don't complain if it's from an enclosing function. */ |
1668 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
1669 | && TREE_CODE (x) != PARM_DECL | |
1670 | && TREE_CODE (oldlocal) == PARM_DECL) | |
1671 | { | |
1672 | /* Go to where the parms should be and see if we find | |
1673 | them there. */ | |
1674 | cp_binding_level *b = current_binding_level->level_chain; | |
00e8de68 | 1675 | |
b655c310 NS |
1676 | if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) |
1677 | /* Skip the ctor/dtor cleanup level. */ | |
1678 | b = b->level_chain; | |
00e8de68 | 1679 | |
b655c310 NS |
1680 | /* ARM $8.3 */ |
1681 | if (b->kind == sk_function_parms) | |
1682 | { | |
1683 | error ("declaration of %q#D shadows a parameter", x); | |
1684 | nowarn = true; | |
1685 | } | |
1686 | } | |
00e8de68 | 1687 | |
b655c310 NS |
1688 | /* The local structure or class can't use parameters of |
1689 | the containing function anyway. */ | |
1690 | if (DECL_CONTEXT (oldlocal) != current_function_decl) | |
1691 | { | |
1692 | cp_binding_level *scope = current_binding_level; | |
1693 | tree context = DECL_CONTEXT (oldlocal); | |
1694 | for (; scope; scope = scope->level_chain) | |
1695 | { | |
1696 | if (scope->kind == sk_function_parms | |
1697 | && scope->this_entity == context) | |
1698 | break; | |
1699 | if (scope->kind == sk_class | |
1700 | && !LAMBDA_TYPE_P (scope->this_entity)) | |
1701 | { | |
1702 | nowarn = true; | |
1703 | break; | |
1704 | } | |
1705 | } | |
1706 | } | |
1707 | /* Error if redeclaring a local declared in a | |
1708 | init-statement or in the condition of an if or | |
1709 | switch statement when the new declaration is in the | |
1710 | outermost block of the controlled statement. | |
1711 | Redeclaring a variable from a for or while condition is | |
1712 | detected elsewhere. */ | |
1713 | else if (VAR_P (oldlocal) | |
1714 | && oldscope == current_binding_level->level_chain | |
1715 | && (oldscope->kind == sk_cond | |
1716 | || oldscope->kind == sk_for)) | |
1717 | { | |
1718 | error ("redeclaration of %q#D", x); | |
1719 | inform (DECL_SOURCE_LOCATION (oldlocal), | |
1720 | "%q#D previously declared here", oldlocal); | |
1721 | nowarn = true; | |
1722 | } | |
1723 | /* C++11: | |
1724 | 3.3.3/3: The name declared in an exception-declaration (...) | |
1725 | shall not be redeclared in the outermost block of the handler. | |
1726 | 3.3.3/2: A parameter name shall not be redeclared (...) in | |
1727 | the outermost block of any handler associated with a | |
1728 | function-try-block. | |
1729 | 3.4.1/15: The function parameter names shall not be redeclared | |
1730 | in the exception-declaration nor in the outermost block of a | |
1731 | handler for the function-try-block. */ | |
1732 | else if ((VAR_P (oldlocal) | |
1733 | && oldscope == current_binding_level->level_chain | |
1734 | && oldscope->kind == sk_catch) | |
1735 | || (TREE_CODE (oldlocal) == PARM_DECL | |
1736 | && (current_binding_level->kind == sk_catch | |
1737 | || (current_binding_level->level_chain->kind | |
1738 | == sk_catch)) | |
1739 | && in_function_try_handler)) | |
1740 | { | |
1741 | if (permerror (input_location, "redeclaration of %q#D", x)) | |
1742 | inform (DECL_SOURCE_LOCATION (oldlocal), | |
1743 | "%q#D previously declared here", oldlocal); | |
1744 | nowarn = true; | |
1745 | } | |
67e18edb | 1746 | |
b655c310 NS |
1747 | if ((warn_shadow |
1748 | || warn_shadow_local | |
1749 | || warn_shadow_compatible_local) | |
1750 | && !nowarn) | |
1751 | { | |
1752 | bool warned; | |
1753 | enum opt_code warning_code; | |
1754 | /* If '-Wshadow=compatible-local' is specified without other | |
1755 | -Wshadow= flags, we will warn only when the type of the | |
1756 | shadowing variable (i.e. x) can be converted to that of | |
1757 | the shadowed parameter (oldlocal). The reason why we only | |
1758 | check if x's type can be converted to oldlocal's type | |
1759 | (but not the other way around) is because when users | |
1760 | accidentally shadow a parameter, more than often they | |
1761 | would use the variable thinking (mistakenly) it's still | |
1762 | the parameter. It would be rare that users would use the | |
1763 | variable in the place that expects the parameter but | |
1764 | thinking it's a new decl. */ | |
1765 | if (warn_shadow) | |
1766 | warning_code = OPT_Wshadow; | |
1767 | else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x), | |
1768 | tf_none)) | |
1769 | warning_code = OPT_Wshadow_compatible_local; | |
1770 | else | |
1771 | warning_code = OPT_Wshadow_local; | |
67e18edb | 1772 | |
b655c310 NS |
1773 | if (TREE_CODE (oldlocal) == PARM_DECL) |
1774 | warned = warning_at (input_location, warning_code, | |
1775 | "declaration of %q#D shadows a parameter", x); | |
1776 | else if (is_capture_proxy (oldlocal)) | |
1777 | warned = warning_at (input_location, warning_code, | |
1778 | "declaration of %qD shadows a lambda capture", | |
1779 | x); | |
1780 | else | |
1781 | warned = warning_at (input_location, warning_code, | |
1782 | "declaration of %qD shadows a previous local", | |
1783 | x); | |
67e18edb | 1784 | |
b655c310 NS |
1785 | if (warned) |
1786 | inform (DECL_SOURCE_LOCATION (oldlocal), | |
1787 | "shadowed declaration is here"); | |
1788 | } | |
1789 | } | |
67e18edb | 1790 | |
b655c310 NS |
1791 | /* Maybe warn if shadowing something else. */ |
1792 | else if (warn_shadow && !DECL_EXTERNAL (x) | |
1793 | /* No shadow warnings for internally generated vars unless | |
1794 | it's an implicit typedef (see create_implicit_typedef | |
1795 | in decl.c). */ | |
1796 | && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)) | |
1797 | /* No shadow warnings for vars made for inlining. */ | |
1798 | && ! DECL_FROM_INLINE (x)) | |
1799 | { | |
1800 | tree member; | |
00e8de68 | 1801 | |
b655c310 NS |
1802 | if (nonlambda_method_basetype ()) |
1803 | member = lookup_member (current_nonlambda_class_type (), | |
1804 | name, | |
1805 | /*protect=*/0, | |
1806 | /*want_type=*/false, | |
1807 | tf_warning_or_error); | |
1808 | else | |
1809 | member = NULL_TREE; | |
00e8de68 | 1810 | |
b655c310 NS |
1811 | if (member && !TREE_STATIC (member)) |
1812 | { | |
1813 | if (BASELINK_P (member)) | |
1814 | member = BASELINK_FUNCTIONS (member); | |
1815 | member = OVL_CURRENT (member); | |
1816 | ||
1817 | /* Do not warn if a variable shadows a function, unless | |
1818 | the variable is a function or a pointer-to-function. */ | |
1819 | if (TREE_CODE (member) != FUNCTION_DECL | |
1820 | || TREE_CODE (x) == FUNCTION_DECL | |
1821 | || TYPE_PTRFN_P (TREE_TYPE (x)) | |
1822 | || TYPE_PTRMEMFUNC_P (TREE_TYPE (x))) | |
1823 | { | |
1824 | if (warning_at (input_location, OPT_Wshadow, | |
1825 | "declaration of %qD shadows a member of %qT", | |
1826 | x, current_nonlambda_class_type ()) | |
1827 | && DECL_P (member)) | |
1828 | inform (DECL_SOURCE_LOCATION (member), | |
1829 | "shadowed declaration is here"); | |
1830 | } | |
1831 | } | |
1832 | else if (oldglobal != NULL_TREE | |
1833 | && (VAR_P (oldglobal) | |
1834 | /* If the old decl is a type decl, only warn if the | |
1835 | old decl is an explicit typedef or if both the | |
1836 | old and new decls are type decls. */ | |
1837 | || (TREE_CODE (oldglobal) == TYPE_DECL | |
1838 | && (!DECL_ARTIFICIAL (oldglobal) | |
1839 | || TREE_CODE (x) == TYPE_DECL))) | |
1840 | && !instantiating_current_function_p ()) | |
1841 | /* XXX shadow warnings in outer-more namespaces */ | |
1842 | { | |
1843 | if (warning_at (input_location, OPT_Wshadow, | |
1844 | "declaration of %qD shadows a " | |
1845 | "global declaration", x)) | |
1846 | inform (DECL_SOURCE_LOCATION (oldglobal), | |
1847 | "shadowed declaration is here"); | |
1848 | } | |
00e8de68 | 1849 | } |
0cbd7506 | 1850 | } |
7b3b6ae4 | 1851 | |
b655c310 NS |
1852 | if (VAR_P (x)) |
1853 | maybe_register_incomplete_var (x); | |
00e8de68 | 1854 | } |
00e8de68 | 1855 | |
b655c310 NS |
1856 | if (need_new_binding) |
1857 | add_decl_to_level (x, | |
1858 | DECL_NAMESPACE_SCOPE_P (x) | |
1859 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
1860 | : current_binding_level); | |
00e8de68 | 1861 | |
b655c310 | 1862 | return x; |
00e8de68 GDR |
1863 | } |
1864 | ||
b655c310 | 1865 | /* Wrapper for pushdecl_maybe_friend_1. */ |
575bfb00 LC |
1866 | |
1867 | tree | |
b655c310 | 1868 | pushdecl_maybe_friend (tree x, bool is_friend) |
575bfb00 LC |
1869 | { |
1870 | tree ret; | |
b655c310 NS |
1871 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
1872 | ret = pushdecl_maybe_friend_1 (x, is_friend); | |
1873 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
575bfb00 LC |
1874 | return ret; |
1875 | } | |
1876 | ||
b655c310 | 1877 | /* Record a decl-node X as belonging to the current lexical scope. */ |
00e8de68 GDR |
1878 | |
1879 | tree | |
b655c310 | 1880 | pushdecl (tree x) |
00e8de68 | 1881 | { |
b655c310 | 1882 | return pushdecl_maybe_friend (x, false); |
00e8de68 GDR |
1883 | } |
1884 | ||
b655c310 NS |
1885 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
1886 | DECL, or a modified version thereof. */ | |
00e8de68 | 1887 | |
b655c310 NS |
1888 | tree |
1889 | maybe_push_decl (tree decl) | |
00e8de68 | 1890 | { |
b655c310 | 1891 | tree type = TREE_TYPE (decl); |
00e8de68 | 1892 | |
b655c310 NS |
1893 | /* Add this decl to the current binding level, but not if it comes |
1894 | from another scope, e.g. a static member variable. TEM may equal | |
1895 | DECL or it may be a previous decl of the same name. */ | |
1896 | if (decl == error_mark_node | |
1897 | || (TREE_CODE (decl) != PARM_DECL | |
1898 | && DECL_CONTEXT (decl) != NULL_TREE | |
1899 | /* Definitions of namespace members outside their namespace are | |
1900 | possible. */ | |
1901 | && !DECL_NAMESPACE_SCOPE_P (decl)) | |
1902 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) | |
1903 | || type == unknown_type_node | |
1904 | /* The declaration of a template specialization does not affect | |
1905 | the functions available for overload resolution, so we do not | |
1906 | call pushdecl. */ | |
1907 | || (TREE_CODE (decl) == FUNCTION_DECL | |
1908 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
1909 | return decl; | |
00e8de68 | 1910 | else |
b655c310 | 1911 | return pushdecl (decl); |
00e8de68 GDR |
1912 | } |
1913 | ||
b655c310 NS |
1914 | /* Bind DECL to ID in the current_binding_level, assumed to be a local |
1915 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1916 | doesn't really belong to this binding level, that it got here | |
1917 | through a using-declaration. */ | |
00e8de68 GDR |
1918 | |
1919 | void | |
b655c310 | 1920 | push_local_binding (tree id, tree decl, int flags) |
00e8de68 | 1921 | { |
b655c310 | 1922 | cp_binding_level *b; |
00e8de68 | 1923 | |
b655c310 NS |
1924 | /* Skip over any local classes. This makes sense if we call |
1925 | push_local_binding with a friend decl of a local class. */ | |
1926 | b = innermost_nonclass_level (); | |
5a167978 | 1927 | |
b655c310 NS |
1928 | if (lookup_name_innermost_nonclass_level (id)) |
1929 | { | |
1930 | /* Supplement the existing binding. */ | |
1931 | if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) | |
1932 | /* It didn't work. Something else must be bound at this | |
1933 | level. Do not add DECL to the list of things to pop | |
1934 | later. */ | |
1935 | return; | |
1936 | } | |
1937 | else | |
1938 | /* Create a new binding. */ | |
1939 | push_binding (id, decl, b); | |
5a167978 | 1940 | |
b655c310 NS |
1941 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
1942 | /* We must put the OVERLOAD into a TREE_LIST since the | |
1943 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1944 | decls that got here through a using-declaration. */ | |
1945 | decl = build_tree_list (NULL_TREE, decl); | |
5a167978 | 1946 | |
b655c310 NS |
1947 | /* And put DECL on the list of things declared by the current |
1948 | binding level. */ | |
1949 | add_decl_to_level (decl, b); | |
5a167978 GDR |
1950 | } |
1951 | ||
b655c310 NS |
1952 | /* Check to see whether or not DECL is a variable that would have been |
1953 | in scope under the ARM, but is not in scope under the ANSI/ISO | |
1954 | standard. If so, issue an error message. If name lookup would | |
1955 | work in both cases, but return a different result, this function | |
1956 | returns the result of ANSI/ISO lookup. Otherwise, it returns | |
1957 | DECL. */ | |
5a167978 | 1958 | |
b655c310 NS |
1959 | tree |
1960 | check_for_out_of_scope_variable (tree decl) | |
5a167978 | 1961 | { |
b655c310 | 1962 | tree shadowed; |
c8094d83 | 1963 | |
b655c310 NS |
1964 | /* We only care about out of scope variables. */ |
1965 | if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl))) | |
1966 | return decl; | |
420bf978 | 1967 | |
b655c310 NS |
1968 | shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl) |
1969 | ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ; | |
1970 | while (shadowed != NULL_TREE && VAR_P (shadowed) | |
1971 | && DECL_DEAD_FOR_LOCAL (shadowed)) | |
1972 | shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed) | |
1973 | ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE; | |
1974 | if (!shadowed) | |
06aa5490 | 1975 | shadowed = get_namespace_binding (current_namespace, DECL_NAME (decl)); |
b655c310 NS |
1976 | if (shadowed) |
1977 | { | |
1978 | if (!DECL_ERROR_REPORTED (decl)) | |
1979 | { | |
1980 | warning (0, "name lookup of %qD changed", DECL_NAME (decl)); | |
1981 | warning_at (DECL_SOURCE_LOCATION (shadowed), 0, | |
1982 | " matches this %qD under ISO standard rules", | |
1983 | shadowed); | |
1984 | warning_at (DECL_SOURCE_LOCATION (decl), 0, | |
1985 | " matches this %qD under old rules", decl); | |
1986 | DECL_ERROR_REPORTED (decl) = 1; | |
1987 | } | |
1988 | return shadowed; | |
1989 | } | |
5a167978 | 1990 | |
b655c310 NS |
1991 | /* If we have already complained about this declaration, there's no |
1992 | need to do it again. */ | |
1993 | if (DECL_ERROR_REPORTED (decl)) | |
1994 | return decl; | |
a5e6b29b | 1995 | |
b655c310 | 1996 | DECL_ERROR_REPORTED (decl) = 1; |
a5e6b29b | 1997 | |
b655c310 NS |
1998 | if (TREE_TYPE (decl) == error_mark_node) |
1999 | return decl; | |
a5e6b29b | 2000 | |
b655c310 NS |
2001 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
2002 | { | |
2003 | error ("name lookup of %qD changed for ISO %<for%> scoping", | |
2004 | DECL_NAME (decl)); | |
2005 | error (" cannot use obsolete binding at %q+D because " | |
2006 | "it has a destructor", decl); | |
2007 | return error_mark_node; | |
2008 | } | |
2009 | else | |
2010 | { | |
2011 | permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping", | |
2012 | DECL_NAME (decl)); | |
2013 | if (flag_permissive) | |
2014 | permerror (DECL_SOURCE_LOCATION (decl), | |
2015 | " using obsolete binding at %qD", decl); | |
2016 | else | |
2017 | { | |
2018 | static bool hint; | |
2019 | if (!hint) | |
2020 | { | |
2021 | inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)"); | |
2022 | hint = true; | |
2023 | } | |
2024 | } | |
2025 | } | |
a5e6b29b | 2026 | |
b655c310 | 2027 | return decl; |
a5e6b29b | 2028 | } |
b655c310 NS |
2029 | \f |
2030 | /* true means unconditionally make a BLOCK for the next level pushed. */ | |
a5e6b29b | 2031 | |
b655c310 | 2032 | static bool keep_next_level_flag; |
d5f4eddd | 2033 | |
b655c310 | 2034 | static int binding_depth = 0; |
d5f4eddd | 2035 | |
b655c310 NS |
2036 | static void |
2037 | indent (int depth) | |
d5f4eddd | 2038 | { |
b655c310 | 2039 | int i; |
d5f4eddd | 2040 | |
b655c310 NS |
2041 | for (i = 0; i < depth * 2; i++) |
2042 | putc (' ', stderr); | |
d5f4eddd JM |
2043 | } |
2044 | ||
b655c310 NS |
2045 | /* Return a string describing the kind of SCOPE we have. */ |
2046 | static const char * | |
2047 | cp_binding_level_descriptor (cp_binding_level *scope) | |
ed3cf953 | 2048 | { |
b655c310 NS |
2049 | /* The order of this table must match the "scope_kind" |
2050 | enumerators. */ | |
2051 | static const char* scope_kind_names[] = { | |
2052 | "block-scope", | |
2053 | "cleanup-scope", | |
2054 | "try-scope", | |
2055 | "catch-scope", | |
2056 | "for-scope", | |
2057 | "function-parameter-scope", | |
2058 | "class-scope", | |
2059 | "namespace-scope", | |
2060 | "template-parameter-scope", | |
2061 | "template-explicit-spec-scope" | |
2062 | }; | |
2063 | const scope_kind kind = scope->explicit_spec_p | |
2064 | ? sk_template_spec : scope->kind; | |
ed3cf953 | 2065 | |
b655c310 | 2066 | return scope_kind_names[kind]; |
ed3cf953 GDR |
2067 | } |
2068 | ||
b655c310 NS |
2069 | /* Output a debugging information about SCOPE when performing |
2070 | ACTION at LINE. */ | |
2071 | static void | |
2072 | cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) | |
ed3cf953 | 2073 | { |
b655c310 NS |
2074 | const char *desc = cp_binding_level_descriptor (scope); |
2075 | if (scope->this_entity) | |
0f2c4a8f | 2076 | verbatim ("%s %<%s(%E)%> %p %d\n", action, desc, |
b655c310 NS |
2077 | scope->this_entity, (void *) scope, line); |
2078 | else | |
2079 | verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); | |
ed3cf953 GDR |
2080 | } |
2081 | ||
b655c310 NS |
2082 | /* Return the estimated initial size of the hashtable of a NAMESPACE |
2083 | scope. */ | |
ed3cf953 | 2084 | |
b655c310 NS |
2085 | static inline size_t |
2086 | namespace_scope_ht_size (tree ns) | |
ed3cf953 | 2087 | { |
b655c310 | 2088 | tree name = DECL_NAME (ns); |
ed3cf953 | 2089 | |
b655c310 NS |
2090 | return name == std_identifier |
2091 | ? NAMESPACE_STD_HT_SIZE | |
ad9870f2 | 2092 | : (name == global_identifier |
b655c310 NS |
2093 | ? GLOBAL_SCOPE_HT_SIZE |
2094 | : NAMESPACE_ORDINARY_HT_SIZE); | |
ed3cf953 | 2095 | } |
ed3cf953 | 2096 | |
b655c310 | 2097 | /* A chain of binding_level structures awaiting reuse. */ |
ecba6c56 | 2098 | |
b655c310 | 2099 | static GTY((deletable)) cp_binding_level *free_binding_level; |
ecba6c56 | 2100 | |
b655c310 | 2101 | /* Insert SCOPE as the innermost binding level. */ |
ecba6c56 | 2102 | |
b655c310 NS |
2103 | void |
2104 | push_binding_level (cp_binding_level *scope) | |
2105 | { | |
2106 | /* Add it to the front of currently active scopes stack. */ | |
2107 | scope->level_chain = current_binding_level; | |
2108 | current_binding_level = scope; | |
2109 | keep_next_level_flag = false; | |
2110 | ||
2111 | if (ENABLE_SCOPE_CHECKING) | |
ecba6c56 | 2112 | { |
b655c310 NS |
2113 | scope->binding_depth = binding_depth; |
2114 | indent (binding_depth); | |
2115 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
2116 | "push"); | |
2117 | binding_depth++; | |
ecba6c56 | 2118 | } |
ecba6c56 DS |
2119 | } |
2120 | ||
b655c310 NS |
2121 | /* Create a new KIND scope and make it the top of the active scopes stack. |
2122 | ENTITY is the scope of the associated C++ entity (namespace, class, | |
2123 | function, C++0x enumeration); it is NULL otherwise. */ | |
3a636414 | 2124 | |
b655c310 NS |
2125 | cp_binding_level * |
2126 | begin_scope (scope_kind kind, tree entity) | |
3a636414 | 2127 | { |
b655c310 | 2128 | cp_binding_level *scope; |
3a636414 | 2129 | |
b655c310 NS |
2130 | /* Reuse or create a struct for this binding level. */ |
2131 | if (!ENABLE_SCOPE_CHECKING && free_binding_level) | |
3a636414 | 2132 | { |
b655c310 NS |
2133 | scope = free_binding_level; |
2134 | free_binding_level = scope->level_chain; | |
2135 | memset (scope, 0, sizeof (cp_binding_level)); | |
3a636414 | 2136 | } |
b655c310 NS |
2137 | else |
2138 | scope = ggc_cleared_alloc<cp_binding_level> (); | |
3a636414 | 2139 | |
b655c310 NS |
2140 | scope->this_entity = entity; |
2141 | scope->more_cleanups_ok = true; | |
2142 | switch (kind) | |
2143 | { | |
2144 | case sk_cleanup: | |
2145 | scope->keep = true; | |
2146 | break; | |
a5e6b29b | 2147 | |
b655c310 NS |
2148 | case sk_template_spec: |
2149 | scope->explicit_spec_p = true; | |
2150 | kind = sk_template_parms; | |
2151 | /* Fall through. */ | |
2152 | case sk_template_parms: | |
2153 | case sk_block: | |
2154 | case sk_try: | |
2155 | case sk_catch: | |
2156 | case sk_for: | |
2157 | case sk_cond: | |
2158 | case sk_class: | |
2159 | case sk_scoped_enum: | |
2160 | case sk_function_parms: | |
2161 | case sk_transaction: | |
2162 | case sk_omp: | |
2163 | scope->keep = keep_next_level_flag; | |
2164 | break; | |
a5e6b29b | 2165 | |
b655c310 NS |
2166 | case sk_namespace: |
2167 | NAMESPACE_LEVEL (entity) = scope; | |
a5e6b29b | 2168 | break; |
b655c310 NS |
2169 | |
2170 | default: | |
2171 | /* Should not happen. */ | |
2172 | gcc_unreachable (); | |
2173 | break; | |
2174 | } | |
2175 | scope->kind = kind; | |
2176 | ||
2177 | push_binding_level (scope); | |
2178 | ||
2179 | return scope; | |
575bfb00 LC |
2180 | } |
2181 | ||
b655c310 NS |
2182 | /* We're about to leave current scope. Pop the top of the stack of |
2183 | currently active scopes. Return the enclosing scope, now active. */ | |
575bfb00 | 2184 | |
b655c310 NS |
2185 | cp_binding_level * |
2186 | leave_scope (void) | |
575bfb00 | 2187 | { |
b655c310 | 2188 | cp_binding_level *scope = current_binding_level; |
a5e6b29b | 2189 | |
b655c310 NS |
2190 | if (scope->kind == sk_namespace && class_binding_level) |
2191 | current_binding_level = class_binding_level; | |
2b8dfc07 | 2192 | |
b655c310 NS |
2193 | /* We cannot leave a scope, if there are none left. */ |
2194 | if (NAMESPACE_LEVEL (global_namespace)) | |
2195 | gcc_assert (!global_scope_p (scope)); | |
ed3cf953 | 2196 | |
b655c310 NS |
2197 | if (ENABLE_SCOPE_CHECKING) |
2198 | { | |
2199 | indent (--binding_depth); | |
2200 | cp_binding_level_debug (scope, LOCATION_LINE (input_location), | |
2201 | "leave"); | |
2202 | } | |
ed3cf953 | 2203 | |
b655c310 NS |
2204 | /* Move one nesting level up. */ |
2205 | current_binding_level = scope->level_chain; | |
2206 | ||
2207 | /* Namespace-scopes are left most probably temporarily, not | |
2208 | completely; they can be reopened later, e.g. in namespace-extension | |
2209 | or any name binding activity that requires us to resume a | |
2210 | namespace. For classes, we cache some binding levels. For other | |
2211 | scopes, we just make the structure available for reuse. */ | |
2212 | if (scope->kind != sk_namespace | |
2213 | && scope->kind != sk_class) | |
00e8de68 | 2214 | { |
b655c310 NS |
2215 | scope->level_chain = free_binding_level; |
2216 | gcc_assert (!ENABLE_SCOPE_CHECKING | |
2217 | || scope->binding_depth == binding_depth); | |
2218 | free_binding_level = scope; | |
00e8de68 | 2219 | } |
b655c310 NS |
2220 | |
2221 | if (scope->kind == sk_class) | |
00e8de68 | 2222 | { |
b655c310 NS |
2223 | /* Reset DEFINING_CLASS_P to allow for reuse of a |
2224 | class-defining scope in a non-defining context. */ | |
2225 | scope->defining_class_p = 0; | |
2226 | ||
2227 | /* Find the innermost enclosing class scope, and reset | |
2228 | CLASS_BINDING_LEVEL appropriately. */ | |
2229 | class_binding_level = NULL; | |
2230 | for (scope = current_binding_level; scope; scope = scope->level_chain) | |
2231 | if (scope->kind == sk_class) | |
2232 | { | |
2233 | class_binding_level = scope; | |
2234 | break; | |
2235 | } | |
00e8de68 | 2236 | } |
b655c310 NS |
2237 | |
2238 | return current_binding_level; | |
00e8de68 | 2239 | } |
575bfb00 | 2240 | |
b655c310 NS |
2241 | static void |
2242 | resume_scope (cp_binding_level* b) | |
575bfb00 | 2243 | { |
b655c310 NS |
2244 | /* Resuming binding levels is meant only for namespaces, |
2245 | and those cannot nest into classes. */ | |
2246 | gcc_assert (!class_binding_level); | |
2247 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
2248 | gcc_assert (b->level_chain == current_binding_level); | |
2249 | current_binding_level = b; | |
2250 | if (ENABLE_SCOPE_CHECKING) | |
2251 | { | |
2252 | b->binding_depth = binding_depth; | |
2253 | indent (binding_depth); | |
2254 | cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); | |
2255 | binding_depth++; | |
2256 | } | |
575bfb00 LC |
2257 | } |
2258 | ||
b655c310 | 2259 | /* Return the innermost binding level that is not for a class scope. */ |
fde6f97e | 2260 | |
b655c310 NS |
2261 | static cp_binding_level * |
2262 | innermost_nonclass_level (void) | |
fde6f97e | 2263 | { |
b655c310 | 2264 | cp_binding_level *b; |
fde6f97e | 2265 | |
b655c310 NS |
2266 | b = current_binding_level; |
2267 | while (b->kind == sk_class) | |
2268 | b = b->level_chain; | |
fde6f97e | 2269 | |
b655c310 | 2270 | return b; |
fde6f97e | 2271 | } |
00e8de68 | 2272 | |
b655c310 NS |
2273 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
2274 | we're not allowed to add any more objects with cleanups to the current | |
2275 | scope, create a new binding level. */ | |
a5e6b29b | 2276 | |
b655c310 NS |
2277 | void |
2278 | maybe_push_cleanup_level (tree type) | |
a5e6b29b | 2279 | { |
b655c310 NS |
2280 | if (type != error_mark_node |
2281 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
2282 | && current_binding_level->more_cleanups_ok == 0) | |
a5e6b29b | 2283 | { |
b655c310 NS |
2284 | begin_scope (sk_cleanup, NULL); |
2285 | current_binding_level->statement_list = push_stmt_list (); | |
2286 | } | |
2287 | } | |
a5e6b29b | 2288 | |
b655c310 | 2289 | /* Return true if we are in the global binding level. */ |
a5e6b29b | 2290 | |
b655c310 NS |
2291 | bool |
2292 | global_bindings_p (void) | |
2293 | { | |
2294 | return global_scope_p (current_binding_level); | |
2295 | } | |
a5e6b29b | 2296 | |
b655c310 NS |
2297 | /* True if we are currently in a toplevel binding level. This |
2298 | means either the global binding level or a namespace in a toplevel | |
2299 | binding level. Since there are no non-toplevel namespace levels, | |
2300 | this really means any namespace or template parameter level. We | |
2301 | also include a class whose context is toplevel. */ | |
1a32490a | 2302 | |
b655c310 NS |
2303 | bool |
2304 | toplevel_bindings_p (void) | |
2305 | { | |
2306 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 2307 | |
b655c310 NS |
2308 | return b->kind == sk_namespace || b->kind == sk_template_parms; |
2309 | } | |
a5e6b29b | 2310 | |
b655c310 NS |
2311 | /* True if this is a namespace scope, or if we are defining a class |
2312 | which is itself at namespace scope, or whose enclosing class is | |
2313 | such a class, etc. */ | |
a5e6b29b | 2314 | |
b655c310 NS |
2315 | bool |
2316 | namespace_bindings_p (void) | |
2317 | { | |
2318 | cp_binding_level *b = innermost_nonclass_level (); | |
a5e6b29b | 2319 | |
b655c310 NS |
2320 | return b->kind == sk_namespace; |
2321 | } | |
a5e6b29b | 2322 | |
b655c310 | 2323 | /* True if the innermost non-class scope is a block scope. */ |
a5e6b29b | 2324 | |
b655c310 NS |
2325 | bool |
2326 | local_bindings_p (void) | |
2327 | { | |
2328 | cp_binding_level *b = innermost_nonclass_level (); | |
2329 | return b->kind < sk_function_parms || b->kind == sk_omp; | |
2330 | } | |
a5e6b29b | 2331 | |
b655c310 | 2332 | /* True if the current level needs to have a BLOCK made. */ |
a5e6b29b | 2333 | |
b655c310 NS |
2334 | bool |
2335 | kept_level_p (void) | |
2336 | { | |
2337 | return (current_binding_level->blocks != NULL_TREE | |
2338 | || current_binding_level->keep | |
2339 | || current_binding_level->kind == sk_cleanup | |
2340 | || current_binding_level->names != NULL_TREE | |
2341 | || current_binding_level->using_directives); | |
575bfb00 LC |
2342 | } |
2343 | ||
b655c310 | 2344 | /* Returns the kind of the innermost scope. */ |
575bfb00 | 2345 | |
b655c310 NS |
2346 | scope_kind |
2347 | innermost_scope_kind (void) | |
575bfb00 | 2348 | { |
b655c310 | 2349 | return current_binding_level->kind; |
a5e6b29b GDR |
2350 | } |
2351 | ||
b655c310 | 2352 | /* Returns true if this scope was created to store template parameters. */ |
5a167978 | 2353 | |
b655c310 NS |
2354 | bool |
2355 | template_parm_scope_p (void) | |
5a167978 | 2356 | { |
b655c310 NS |
2357 | return innermost_scope_kind () == sk_template_parms; |
2358 | } | |
7756db03 | 2359 | |
b655c310 NS |
2360 | /* If KEEP is true, make a BLOCK node for the next binding level, |
2361 | unconditionally. Otherwise, use the normal logic to decide whether | |
2362 | or not to create a BLOCK. */ | |
5a167978 | 2363 | |
b655c310 NS |
2364 | void |
2365 | keep_next_level (bool keep) | |
2366 | { | |
2367 | keep_next_level_flag = keep; | |
2368 | } | |
5a167978 | 2369 | |
9c82d7b6 | 2370 | /* Return the list of declarations of the current local scope. */ |
5a167978 | 2371 | |
b655c310 | 2372 | tree |
9c82d7b6 | 2373 | get_local_decls (void) |
b655c310 | 2374 | { |
9c82d7b6 NS |
2375 | gcc_assert (current_binding_level->kind != sk_namespace |
2376 | && current_binding_level->kind != sk_class); | |
b655c310 NS |
2377 | return current_binding_level->names; |
2378 | } | |
5a167978 | 2379 | |
b655c310 | 2380 | /* Return how many function prototypes we are currently nested inside. */ |
5a167978 | 2381 | |
b655c310 NS |
2382 | int |
2383 | function_parm_depth (void) | |
2384 | { | |
2385 | int level = 0; | |
2386 | cp_binding_level *b; | |
8597cab1 | 2387 | |
b655c310 NS |
2388 | for (b = current_binding_level; |
2389 | b->kind == sk_function_parms; | |
2390 | b = b->level_chain) | |
2391 | ++level; | |
2392 | ||
2393 | return level; | |
5a167978 GDR |
2394 | } |
2395 | ||
b655c310 NS |
2396 | /* For debugging. */ |
2397 | static int no_print_functions = 0; | |
2398 | static int no_print_builtins = 0; | |
5a167978 GDR |
2399 | |
2400 | static void | |
b655c310 | 2401 | print_binding_level (cp_binding_level* lvl) |
5a167978 | 2402 | { |
b655c310 NS |
2403 | tree t; |
2404 | int i = 0, len; | |
2405 | fprintf (stderr, " blocks=%p", (void *) lvl->blocks); | |
2406 | if (lvl->more_cleanups_ok) | |
2407 | fprintf (stderr, " more-cleanups-ok"); | |
2408 | if (lvl->have_cleanups) | |
2409 | fprintf (stderr, " have-cleanups"); | |
2410 | fprintf (stderr, "\n"); | |
2411 | if (lvl->names) | |
2412 | { | |
2413 | fprintf (stderr, " names:\t"); | |
2414 | /* We can probably fit 3 names to a line? */ | |
2415 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
2416 | { | |
2417 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) | |
2418 | continue; | |
2419 | if (no_print_builtins | |
2420 | && (TREE_CODE (t) == TYPE_DECL) | |
2421 | && DECL_IS_BUILTIN (t)) | |
2422 | continue; | |
5a167978 | 2423 | |
b655c310 NS |
2424 | /* Function decls tend to have longer names. */ |
2425 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2426 | len = 3; | |
2427 | else | |
2428 | len = 2; | |
2429 | i += len; | |
2430 | if (i > 6) | |
2431 | { | |
2432 | fprintf (stderr, "\n\t"); | |
2433 | i = len; | |
2434 | } | |
2435 | print_node_brief (stderr, "", t, 0); | |
2436 | if (t == error_mark_node) | |
2437 | break; | |
2438 | } | |
2439 | if (i) | |
2440 | fprintf (stderr, "\n"); | |
2441 | } | |
2442 | if (vec_safe_length (lvl->class_shadowed)) | |
5a167978 | 2443 | { |
b655c310 NS |
2444 | size_t i; |
2445 | cp_class_binding *b; | |
2446 | fprintf (stderr, " class-shadowed:"); | |
2447 | FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) | |
2448 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); | |
2449 | fprintf (stderr, "\n"); | |
5a167978 | 2450 | } |
b655c310 | 2451 | if (lvl->type_shadowed) |
44fd0e80 | 2452 | { |
b655c310 NS |
2453 | fprintf (stderr, " type-shadowed:"); |
2454 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
2455 | { | |
2456 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
2457 | } | |
2458 | fprintf (stderr, "\n"); | |
44fd0e80 | 2459 | } |
b655c310 | 2460 | } |
44fd0e80 | 2461 | |
b655c310 NS |
2462 | DEBUG_FUNCTION void |
2463 | debug (cp_binding_level &ref) | |
2464 | { | |
2465 | print_binding_level (&ref); | |
2466 | } | |
2467 | ||
2468 | DEBUG_FUNCTION void | |
2469 | debug (cp_binding_level *ptr) | |
2470 | { | |
2471 | if (ptr) | |
2472 | debug (*ptr); | |
2473 | else | |
2474 | fprintf (stderr, "<nil>\n"); | |
2475 | } | |
2476 | ||
2477 | ||
2478 | void | |
2479 | print_other_binding_stack (cp_binding_level *stack) | |
2480 | { | |
2481 | cp_binding_level *level; | |
2482 | for (level = stack; !global_scope_p (level); level = level->level_chain) | |
44fd0e80 | 2483 | { |
b655c310 NS |
2484 | fprintf (stderr, "binding level %p\n", (void *) level); |
2485 | print_binding_level (level); | |
44fd0e80 | 2486 | } |
b655c310 | 2487 | } |
44fd0e80 | 2488 | |
b655c310 NS |
2489 | void |
2490 | print_binding_stack (void) | |
2491 | { | |
2492 | cp_binding_level *b; | |
2493 | fprintf (stderr, "current_binding_level=%p\n" | |
2494 | "class_binding_level=%p\n" | |
2495 | "NAMESPACE_LEVEL (global_namespace)=%p\n", | |
2496 | (void *) current_binding_level, (void *) class_binding_level, | |
2497 | (void *) NAMESPACE_LEVEL (global_namespace)); | |
2498 | if (class_binding_level) | |
5a167978 | 2499 | { |
b655c310 NS |
2500 | for (b = class_binding_level; b; b = b->level_chain) |
2501 | if (b == current_binding_level) | |
2502 | break; | |
2503 | if (b) | |
2504 | b = class_binding_level; | |
2505 | else | |
2506 | b = current_binding_level; | |
2507 | } | |
2508 | else | |
2509 | b = current_binding_level; | |
2510 | print_other_binding_stack (b); | |
2511 | fprintf (stderr, "global:\n"); | |
2512 | print_binding_level (NAMESPACE_LEVEL (global_namespace)); | |
2513 | } | |
2514 | \f | |
2515 | /* Return the type associated with ID. */ | |
5a167978 | 2516 | |
b655c310 NS |
2517 | static tree |
2518 | identifier_type_value_1 (tree id) | |
2519 | { | |
2520 | /* There is no type with that name, anywhere. */ | |
2521 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
2522 | return NULL_TREE; | |
2523 | /* This is not the type marker, but the real thing. */ | |
2524 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
2525 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
2526 | /* Have to search for it. It must be on the global level, now. | |
2527 | Ask lookup_name not to return non-types. */ | |
2528 | id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); | |
2529 | if (id) | |
2530 | return TREE_TYPE (id); | |
2531 | return NULL_TREE; | |
2532 | } | |
44fd0e80 | 2533 | |
b655c310 | 2534 | /* Wrapper for identifier_type_value_1. */ |
1374a761 | 2535 | |
b655c310 NS |
2536 | tree |
2537 | identifier_type_value (tree id) | |
2538 | { | |
2539 | tree ret; | |
2540 | timevar_start (TV_NAME_LOOKUP); | |
2541 | ret = identifier_type_value_1 (id); | |
2542 | timevar_stop (TV_NAME_LOOKUP); | |
2543 | return ret; | |
2544 | } | |
44fd0e80 | 2545 | |
e3016344 | 2546 | |
b655c310 NS |
2547 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since |
2548 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
5a167978 | 2549 | |
b655c310 NS |
2550 | tree |
2551 | identifier_global_value (tree t) | |
2552 | { | |
2553 | return IDENTIFIER_GLOBAL_VALUE (t); | |
2554 | } | |
44fd0e80 | 2555 | |
b655c310 NS |
2556 | /* Push a definition of struct, union or enum tag named ID. into |
2557 | binding_level B. DECL is a TYPE_DECL for the type. We assume that | |
2558 | the tag ID is not already defined. */ | |
1374a761 | 2559 | |
b655c310 NS |
2560 | static void |
2561 | set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) | |
2562 | { | |
2563 | tree type; | |
5a167978 | 2564 | |
b655c310 | 2565 | if (b->kind != sk_namespace) |
5a167978 | 2566 | { |
b655c310 NS |
2567 | /* Shadow the marker, not the real thing, so that the marker |
2568 | gets restored later. */ | |
2569 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
2570 | b->type_shadowed | |
2571 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2572 | type = decl ? TREE_TYPE (decl) : NULL_TREE; | |
2573 | TREE_TYPE (b->type_shadowed) = type; | |
19831e2b OW |
2574 | } |
2575 | else | |
2576 | { | |
b655c310 NS |
2577 | cxx_binding *binding = |
2578 | binding_for_name (NAMESPACE_LEVEL (current_namespace), id); | |
2579 | gcc_assert (decl); | |
2580 | if (binding->value) | |
2581 | supplement_binding (binding, decl); | |
2582 | else | |
2583 | binding->value = decl; | |
44fd0e80 | 2584 | |
b655c310 NS |
2585 | /* Store marker instead of real type. */ |
2586 | type = global_type_node; | |
2587 | } | |
2588 | SET_IDENTIFIER_TYPE_VALUE (id, type); | |
5a167978 GDR |
2589 | } |
2590 | ||
b655c310 NS |
2591 | /* As set_identifier_type_value_with_scope, but using |
2592 | current_binding_level. */ | |
5a167978 GDR |
2593 | |
2594 | void | |
b655c310 | 2595 | set_identifier_type_value (tree id, tree decl) |
5a167978 | 2596 | { |
b655c310 NS |
2597 | set_identifier_type_value_with_scope (id, decl, current_binding_level); |
2598 | } | |
5a167978 | 2599 | |
b655c310 NS |
2600 | /* Return the name for the constructor (or destructor) for the |
2601 | specified class TYPE. When given a template, this routine doesn't | |
2602 | lose the specialization. */ | |
5a167978 | 2603 | |
b655c310 NS |
2604 | static inline tree |
2605 | constructor_name_full (tree type) | |
2606 | { | |
2607 | return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); | |
2608 | } | |
5a167978 | 2609 | |
b655c310 NS |
2610 | /* Return the name for the constructor (or destructor) for the |
2611 | specified class. When given a template, return the plain | |
2612 | unspecialized name. */ | |
5a167978 | 2613 | |
b655c310 NS |
2614 | tree |
2615 | constructor_name (tree type) | |
2616 | { | |
2617 | tree name; | |
2618 | name = constructor_name_full (type); | |
2619 | if (IDENTIFIER_TEMPLATE (name)) | |
2620 | name = IDENTIFIER_TEMPLATE (name); | |
2621 | return name; | |
2622 | } | |
5a167978 | 2623 | |
b655c310 NS |
2624 | /* Returns TRUE if NAME is the name for the constructor for TYPE, |
2625 | which must be a class type. */ | |
5a167978 | 2626 | |
b655c310 NS |
2627 | bool |
2628 | constructor_name_p (tree name, tree type) | |
2629 | { | |
2630 | tree ctor_name; | |
6097b0c3 | 2631 | |
b655c310 NS |
2632 | gcc_assert (MAYBE_CLASS_TYPE_P (type)); |
2633 | ||
2634 | if (!name) | |
2635 | return false; | |
2636 | ||
2637 | if (!identifier_p (name)) | |
2638 | return false; | |
2639 | ||
2640 | /* These don't have names. */ | |
2641 | if (TREE_CODE (type) == DECLTYPE_TYPE | |
2642 | || TREE_CODE (type) == TYPEOF_TYPE) | |
2643 | return false; | |
2644 | ||
2645 | ctor_name = constructor_name_full (type); | |
2646 | if (name == ctor_name) | |
2647 | return true; | |
2648 | if (IDENTIFIER_TEMPLATE (ctor_name) | |
2649 | && name == IDENTIFIER_TEMPLATE (ctor_name)) | |
2650 | return true; | |
2651 | return false; | |
5a167978 GDR |
2652 | } |
2653 | ||
b655c310 | 2654 | /* Counter used to create anonymous type names. */ |
5a167978 | 2655 | |
b655c310 NS |
2656 | static GTY(()) int anon_cnt; |
2657 | ||
2658 | /* Return an IDENTIFIER which can be used as a name for | |
2659 | unnamed structs and unions. */ | |
2660 | ||
2661 | tree | |
2662 | make_anon_name (void) | |
5a167978 | 2663 | { |
b655c310 NS |
2664 | char buf[32]; |
2665 | ||
2666 | sprintf (buf, anon_aggrname_format (), anon_cnt++); | |
2667 | return get_identifier (buf); | |
2668 | } | |
2669 | ||
2670 | /* This code is practically identical to that for creating | |
2671 | anonymous names, but is just used for lambdas instead. This isn't really | |
2672 | necessary, but it's convenient to avoid treating lambdas like other | |
2673 | unnamed types. */ | |
2674 | ||
2675 | static GTY(()) int lambda_cnt = 0; | |
2676 | ||
2677 | tree | |
2678 | make_lambda_name (void) | |
2679 | { | |
2680 | char buf[32]; | |
2681 | ||
2682 | sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); | |
2683 | return get_identifier (buf); | |
2684 | } | |
c8094d83 | 2685 | |
b655c310 | 2686 | /* Return (from the stack of) the BINDING, if any, established at SCOPE. */ |
5a167978 | 2687 | |
b655c310 NS |
2688 | static inline cxx_binding * |
2689 | find_binding (cp_binding_level *scope, cxx_binding *binding) | |
2690 | { | |
2691 | for (; binding != NULL; binding = binding->previous) | |
2692 | if (binding->scope == scope) | |
2693 | return binding; | |
2694 | ||
2695 | return (cxx_binding *)0; | |
5a167978 GDR |
2696 | } |
2697 | ||
b655c310 | 2698 | /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ |
5a167978 | 2699 | |
b655c310 NS |
2700 | static inline cxx_binding * |
2701 | cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name) | |
5a167978 | 2702 | { |
b655c310 NS |
2703 | cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); |
2704 | if (b) | |
91b004e5 | 2705 | { |
b655c310 NS |
2706 | /* Fold-in case where NAME is used only once. */ |
2707 | if (scope == b->scope && b->previous == NULL) | |
2708 | return b; | |
2709 | return find_binding (scope, b); | |
91b004e5 | 2710 | } |
b655c310 | 2711 | return NULL; |
5a167978 GDR |
2712 | } |
2713 | ||
b655c310 NS |
2714 | /* Always returns a binding for name in scope. If no binding is |
2715 | found, make a new one. */ | |
5a167978 | 2716 | |
b655c310 NS |
2717 | static cxx_binding * |
2718 | binding_for_name (cp_binding_level *scope, tree name) | |
5a167978 | 2719 | { |
b655c310 | 2720 | cxx_binding *result; |
87c465f5 | 2721 | |
b655c310 NS |
2722 | result = cp_binding_level_find_binding_for_name (scope, name); |
2723 | if (result) | |
2724 | return result; | |
2725 | /* Not found, make a new one. */ | |
2726 | result = cxx_binding_make (NULL, NULL); | |
2727 | result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2728 | result->scope = scope; | |
2729 | result->is_local = false; | |
2730 | result->value_is_inherited = false; | |
2731 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
2732 | return result; | |
2733 | } | |
87c465f5 | 2734 | |
b655c310 NS |
2735 | /* Walk through the bindings associated to the name of FUNCTION, |
2736 | and return the first declaration of a function with a | |
2737 | "C" linkage specification, a.k.a 'extern "C"'. | |
2738 | This function looks for the binding, regardless of which scope it | |
2739 | has been defined in. It basically looks in all the known scopes. | |
2740 | Note that this function does not lookup for bindings of builtin functions | |
2741 | or for functions declared in system headers. */ | |
2742 | static tree | |
2743 | lookup_extern_c_fun_in_all_ns (tree function) | |
87c465f5 | 2744 | { |
b655c310 NS |
2745 | tree name; |
2746 | cxx_binding *iter; | |
87c465f5 | 2747 | |
b655c310 | 2748 | gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL); |
87c465f5 | 2749 | |
b655c310 NS |
2750 | name = DECL_NAME (function); |
2751 | gcc_assert (name && identifier_p (name)); | |
2752 | ||
2753 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2754 | iter; | |
2755 | iter = iter->previous) | |
87c465f5 | 2756 | { |
b655c310 NS |
2757 | tree ovl; |
2758 | for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) | |
87c465f5 | 2759 | { |
b655c310 NS |
2760 | tree decl = OVL_CURRENT (ovl); |
2761 | if (decl | |
2762 | && TREE_CODE (decl) == FUNCTION_DECL | |
2763 | && DECL_EXTERN_C_P (decl) | |
2764 | && !DECL_ARTIFICIAL (decl)) | |
2765 | { | |
2766 | return decl; | |
2767 | } | |
87c465f5 | 2768 | } |
b655c310 NS |
2769 | } |
2770 | return NULL; | |
2771 | } | |
87c465f5 | 2772 | |
b655c310 | 2773 | /* Returns a list of C-linkage decls with the name NAME. */ |
87c465f5 | 2774 | |
b655c310 NS |
2775 | tree |
2776 | c_linkage_bindings (tree name) | |
2777 | { | |
2778 | tree decls = NULL_TREE; | |
2779 | cxx_binding *iter; | |
2780 | ||
2781 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2782 | iter; | |
2783 | iter = iter->previous) | |
2784 | { | |
2785 | tree ovl; | |
2786 | for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) | |
87c465f5 | 2787 | { |
b655c310 NS |
2788 | tree decl = OVL_CURRENT (ovl); |
2789 | if (decl | |
2790 | && DECL_EXTERN_C_P (decl) | |
2791 | && !DECL_ARTIFICIAL (decl)) | |
2792 | { | |
2793 | if (decls == NULL_TREE) | |
2794 | decls = decl; | |
2795 | else | |
2796 | decls = tree_cons (NULL_TREE, decl, decls); | |
2797 | } | |
87c465f5 KL |
2798 | } |
2799 | } | |
b655c310 | 2800 | return decls; |
87c465f5 KL |
2801 | } |
2802 | ||
b655c310 NS |
2803 | /* Insert another USING_DECL into the current binding level, returning |
2804 | this declaration. If this is a redeclaration, do nothing, and | |
2805 | return NULL_TREE if this not in namespace scope (in namespace | |
2806 | scope, a using decl might extend any previous bindings). */ | |
87c465f5 | 2807 | |
b655c310 NS |
2808 | static tree |
2809 | push_using_decl_1 (tree scope, tree name) | |
87c465f5 | 2810 | { |
b655c310 | 2811 | tree decl; |
87c465f5 | 2812 | |
b655c310 NS |
2813 | gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); |
2814 | gcc_assert (identifier_p (name)); | |
2815 | for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) | |
2816 | if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) | |
2817 | break; | |
2818 | if (decl) | |
2819 | return namespace_bindings_p () ? decl : NULL_TREE; | |
2820 | decl = build_lang_decl (USING_DECL, name, NULL_TREE); | |
2821 | USING_DECL_SCOPE (decl) = scope; | |
2822 | DECL_CHAIN (decl) = current_binding_level->usings; | |
2823 | current_binding_level->usings = decl; | |
2824 | return decl; | |
87c465f5 KL |
2825 | } |
2826 | ||
b655c310 | 2827 | /* Wrapper for push_using_decl_1. */ |
87c465f5 | 2828 | |
b655c310 NS |
2829 | static tree |
2830 | push_using_decl (tree scope, tree name) | |
87c465f5 | 2831 | { |
b655c310 NS |
2832 | tree ret; |
2833 | timevar_start (TV_NAME_LOOKUP); | |
2834 | ret = push_using_decl_1 (scope, name); | |
2835 | timevar_stop (TV_NAME_LOOKUP); | |
2836 | return ret; | |
2837 | } | |
87c465f5 | 2838 | |
b655c310 NS |
2839 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
2840 | caller to set DECL_CONTEXT properly. | |
87c465f5 | 2841 | |
b655c310 NS |
2842 | Note that this must only be used when X will be the new innermost |
2843 | binding for its name, as we tack it onto the front of IDENTIFIER_BINDING | |
2844 | without checking to see if the current IDENTIFIER_BINDING comes from a | |
2845 | closer binding level than LEVEL. */ | |
87c465f5 | 2846 | |
b655c310 NS |
2847 | static tree |
2848 | pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend) | |
2849 | { | |
2850 | cp_binding_level *b; | |
2851 | tree function_decl = current_function_decl; | |
87c465f5 | 2852 | |
b655c310 NS |
2853 | current_function_decl = NULL_TREE; |
2854 | if (level->kind == sk_class) | |
2855 | { | |
2856 | b = class_binding_level; | |
2857 | class_binding_level = level; | |
2858 | pushdecl_class_level (x); | |
2859 | class_binding_level = b; | |
2860 | } | |
2861 | else | |
2862 | { | |
2863 | b = current_binding_level; | |
2864 | current_binding_level = level; | |
2865 | x = pushdecl_maybe_friend (x, is_friend); | |
2866 | current_binding_level = b; | |
87c465f5 | 2867 | } |
b655c310 NS |
2868 | current_function_decl = function_decl; |
2869 | return x; | |
87c465f5 | 2870 | } |
b655c310 | 2871 | |
d16d5eac | 2872 | /* Inject X into the local scope just before the function parms. */ |
00e8de68 | 2873 | |
b655c310 | 2874 | tree |
d16d5eac | 2875 | pushdecl_outermost_localscope (tree x) |
00e8de68 | 2876 | { |
b655c310 | 2877 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
d16d5eac NS |
2878 | cp_binding_level *b = NULL, *n = current_binding_level; |
2879 | ||
2880 | if (n->kind == sk_function_parms) | |
2881 | return error_mark_node; | |
2882 | do | |
2883 | { | |
2884 | b = n; | |
2885 | n = b->level_chain; | |
2886 | } | |
2887 | while (n->kind != sk_function_parms); | |
2888 | tree ret = pushdecl_with_scope_1 (x, b, false); | |
b655c310 NS |
2889 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
2890 | return ret; | |
00e8de68 GDR |
2891 | } |
2892 | ||
b655c310 NS |
2893 | /* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl. |
2894 | Compares the parameter-type-lists of DECL1 and DECL2 and returns false | |
2895 | if they are different. If the DECLs are template functions, the return | |
2896 | types and the template parameter lists are compared too (DR 565). */ | |
00e8de68 | 2897 | |
b655c310 NS |
2898 | static bool |
2899 | compparms_for_decl_and_using_decl (tree decl1, tree decl2) | |
00e8de68 | 2900 | { |
b655c310 NS |
2901 | if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)), |
2902 | TYPE_ARG_TYPES (TREE_TYPE (decl2)))) | |
2903 | return false; | |
00e8de68 | 2904 | |
b655c310 NS |
2905 | if (! DECL_FUNCTION_TEMPLATE_P (decl1) |
2906 | || ! DECL_FUNCTION_TEMPLATE_P (decl2)) | |
2907 | return true; | |
00e8de68 | 2908 | |
b655c310 NS |
2909 | return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1), |
2910 | DECL_TEMPLATE_PARMS (decl2)) | |
2911 | && same_type_p (TREE_TYPE (TREE_TYPE (decl1)), | |
2912 | TREE_TYPE (TREE_TYPE (decl2)))); | |
00e8de68 GDR |
2913 | } |
2914 | ||
b655c310 NS |
2915 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
2916 | other definitions already in place. We get around this by making | |
2917 | the value of the identifier point to a list of all the things that | |
2918 | want to be referenced by that name. It is then up to the users of | |
2919 | that name to decide what to do with that list. | |
00e8de68 | 2920 | |
b655c310 NS |
2921 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its |
2922 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
90ea9897 | 2923 | |
b655c310 NS |
2924 | FLAGS is a bitwise-or of the following values: |
2925 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
2926 | namespace scope. | |
2927 | PUSH_USING: DECL is being pushed as the result of a using | |
2928 | declaration. | |
00e8de68 | 2929 | |
b655c310 | 2930 | IS_FRIEND is true if this is a friend declaration. |
00e8de68 | 2931 | |
b655c310 NS |
2932 | The value returned may be a previous declaration if we guessed wrong |
2933 | about what language DECL should belong to (C or C++). Otherwise, | |
2934 | it's always DECL (and never something that's not a _DECL). */ | |
00e8de68 | 2935 | |
b655c310 NS |
2936 | static tree |
2937 | push_overloaded_decl_1 (tree decl, int flags, bool is_friend) | |
00e8de68 | 2938 | { |
b655c310 NS |
2939 | tree name = DECL_NAME (decl); |
2940 | tree old; | |
2941 | tree new_binding; | |
2942 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); | |
d5f4eddd | 2943 | |
b655c310 | 2944 | if (doing_global) |
06aa5490 | 2945 | old = get_namespace_binding (DECL_CONTEXT (decl), name); |
00e8de68 | 2946 | else |
b655c310 | 2947 | old = lookup_name_innermost_nonclass_level (name); |
00e8de68 | 2948 | |
b655c310 | 2949 | if (old) |
00e8de68 | 2950 | { |
b655c310 | 2951 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
00e8de68 | 2952 | { |
b655c310 NS |
2953 | tree t = TREE_TYPE (old); |
2954 | if (MAYBE_CLASS_TYPE_P (t) && warn_shadow | |
2955 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
2956 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
2957 | warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t); | |
2958 | old = NULL_TREE; | |
00e8de68 | 2959 | } |
b655c310 NS |
2960 | else if (is_overloaded_fn (old)) |
2961 | { | |
2962 | tree tmp; | |
90ea9897 | 2963 | |
b655c310 NS |
2964 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
2965 | { | |
2966 | tree fn = OVL_CURRENT (tmp); | |
2967 | tree dup; | |
90ea9897 | 2968 | |
b655c310 NS |
2969 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) |
2970 | && !(flags & PUSH_USING) | |
2971 | && compparms_for_decl_and_using_decl (fn, decl) | |
2972 | && ! decls_match (fn, decl)) | |
2973 | diagnose_name_conflict (decl, fn); | |
90ea9897 | 2974 | |
b655c310 NS |
2975 | dup = duplicate_decls (decl, fn, is_friend); |
2976 | /* If DECL was a redeclaration of FN -- even an invalid | |
2977 | one -- pass that information along to our caller. */ | |
2978 | if (dup == fn || dup == error_mark_node) | |
2979 | return dup; | |
2980 | } | |
90ea9897 | 2981 | |
b655c310 NS |
2982 | /* We don't overload implicit built-ins. duplicate_decls() |
2983 | may fail to merge the decls if the new decl is e.g. a | |
2984 | template function. */ | |
2985 | if (TREE_CODE (old) == FUNCTION_DECL | |
2986 | && DECL_ANTICIPATED (old) | |
2987 | && !DECL_HIDDEN_FRIEND_P (old)) | |
2988 | old = NULL; | |
2989 | } | |
2990 | else if (old == error_mark_node) | |
2991 | /* Ignore the undefined symbol marker. */ | |
2992 | old = NULL_TREE; | |
2993 | else | |
2994 | { | |
2995 | error ("previous non-function declaration %q+#D", old); | |
2996 | error ("conflicts with function declaration %q#D", decl); | |
2997 | return decl; | |
2998 | } | |
90ea9897 MM |
2999 | } |
3000 | ||
b655c310 NS |
3001 | if (old || TREE_CODE (decl) == TEMPLATE_DECL |
3002 | /* If it's a using declaration, we always need to build an OVERLOAD, | |
3003 | because it's the only way to remember that the declaration comes | |
3004 | from 'using', and have the lookup behave correctly. */ | |
3005 | || (flags & PUSH_USING)) | |
90ea9897 | 3006 | { |
b655c310 NS |
3007 | if (old && TREE_CODE (old) != OVERLOAD) |
3008 | /* Wrap the existing single decl in an overload. */ | |
3009 | new_binding = ovl_cons (old, NULL_TREE); | |
3010 | else | |
3011 | new_binding = old; | |
3012 | new_binding = ovl_cons (decl, new_binding); | |
3013 | if (flags & PUSH_USING) | |
3014 | OVL_USED (new_binding) = 1; | |
90ea9897 MM |
3015 | } |
3016 | else | |
b655c310 NS |
3017 | /* NAME is not ambiguous. */ |
3018 | new_binding = decl; | |
0fdc23b9 | 3019 | |
b655c310 NS |
3020 | if (doing_global) |
3021 | set_namespace_binding (name, current_namespace, new_binding); | |
3022 | else | |
279d3eb8 | 3023 | { |
b655c310 NS |
3024 | /* We only create an OVERLOAD if there was a previous binding at |
3025 | this level, or if decl is a template. In the former case, we | |
3026 | need to remove the old binding and replace it with the new | |
3027 | binding. We must also run through the NAMES on the binding | |
3028 | level where the name was bound to update the chain. */ | |
279d3eb8 | 3029 | |
b655c310 NS |
3030 | if (TREE_CODE (new_binding) == OVERLOAD && old) |
3031 | { | |
3032 | tree *d; | |
c67a1c46 | 3033 | |
b655c310 NS |
3034 | for (d = &IDENTIFIER_BINDING (name)->scope->names; |
3035 | *d; | |
3036 | d = &TREE_CHAIN (*d)) | |
3037 | if (*d == old | |
3038 | || (TREE_CODE (*d) == TREE_LIST | |
3039 | && TREE_VALUE (*d) == old)) | |
3040 | { | |
3041 | if (TREE_CODE (*d) == TREE_LIST) | |
3042 | /* Just replace the old binding with the new. */ | |
3043 | TREE_VALUE (*d) = new_binding; | |
3044 | else | |
3045 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
3046 | *d = tree_cons (NULL_TREE, new_binding, | |
3047 | TREE_CHAIN (*d)); | |
ece95d90 | 3048 | |
b655c310 NS |
3049 | /* And update the cxx_binding node. */ |
3050 | IDENTIFIER_BINDING (name)->value = new_binding; | |
3051 | return decl; | |
3052 | } | |
00e8de68 | 3053 | |
b655c310 NS |
3054 | /* We should always find a previous binding in this case. */ |
3055 | gcc_unreachable (); | |
3056 | } | |
d2f2c87b | 3057 | |
b655c310 NS |
3058 | /* Install the new binding. */ |
3059 | push_local_binding (name, new_binding, flags); | |
3060 | } | |
d2f2c87b | 3061 | |
b655c310 NS |
3062 | return decl; |
3063 | } | |
d2f2c87b | 3064 | |
b655c310 | 3065 | /* Wrapper for push_overloaded_decl_1. */ |
d2f2c87b | 3066 | |
b655c310 NS |
3067 | static tree |
3068 | push_overloaded_decl (tree decl, int flags, bool is_friend) | |
3069 | { | |
3070 | tree ret; | |
3071 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
3072 | ret = push_overloaded_decl_1 (decl, flags, is_friend); | |
3073 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
3074 | return ret; | |
3075 | } | |
d2f2c87b | 3076 | |
b655c310 NS |
3077 | /* Check a non-member using-declaration. Return the name and scope |
3078 | being used, and the USING_DECL, or NULL_TREE on failure. */ | |
d2f2c87b | 3079 | |
b655c310 NS |
3080 | static tree |
3081 | validate_nonmember_using_decl (tree decl, tree scope, tree name) | |
3082 | { | |
3083 | /* [namespace.udecl] | |
3084 | A using-declaration for a class member shall be a | |
3085 | member-declaration. */ | |
3086 | if (TYPE_P (scope)) | |
90ea9897 | 3087 | { |
b655c310 NS |
3088 | error ("%qT is not a namespace or unscoped enum", scope); |
3089 | return NULL_TREE; | |
d2f2c87b | 3090 | } |
b655c310 NS |
3091 | else if (scope == error_mark_node) |
3092 | return NULL_TREE; | |
d2f2c87b | 3093 | |
b655c310 | 3094 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) |
90ea9897 | 3095 | { |
b655c310 NS |
3096 | /* 7.3.3/5 |
3097 | A using-declaration shall not name a template-id. */ | |
3098 | error ("a using-declaration cannot specify a template-id. " | |
3099 | "Try %<using %D%>", name); | |
3100 | return NULL_TREE; | |
90ea9897 MM |
3101 | } |
3102 | ||
b655c310 | 3103 | if (TREE_CODE (decl) == NAMESPACE_DECL) |
00e8de68 | 3104 | { |
b655c310 NS |
3105 | error ("namespace %qD not allowed in using-declaration", decl); |
3106 | return NULL_TREE; | |
00e8de68 GDR |
3107 | } |
3108 | ||
b655c310 | 3109 | if (TREE_CODE (decl) == SCOPE_REF) |
90ea9897 | 3110 | { |
b655c310 NS |
3111 | /* It's a nested name with template parameter dependent scope. |
3112 | This can only be using-declaration for class member. */ | |
3113 | error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); | |
3114 | return NULL_TREE; | |
90ea9897 | 3115 | } |
00e8de68 | 3116 | |
b655c310 NS |
3117 | if (is_overloaded_fn (decl)) |
3118 | decl = get_first_fn (decl); | |
3119 | ||
3120 | gcc_assert (DECL_P (decl)); | |
575bfb00 | 3121 | |
b655c310 NS |
3122 | /* Make a USING_DECL. */ |
3123 | tree using_decl = push_using_decl (scope, name); | |
575bfb00 | 3124 | |
b655c310 NS |
3125 | if (using_decl == NULL_TREE |
3126 | && at_function_scope_p () | |
3127 | && VAR_P (decl)) | |
3128 | /* C++11 7.3.3/10. */ | |
3129 | error ("%qD is already declared in this scope", name); | |
3130 | ||
3131 | return using_decl; | |
00e8de68 GDR |
3132 | } |
3133 | ||
b655c310 | 3134 | /* Process local and global using-declarations. */ |
1d786913 | 3135 | |
b655c310 NS |
3136 | static void |
3137 | do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, | |
3138 | tree *newval, tree *newtype) | |
5a167978 | 3139 | { |
b655c310 | 3140 | struct scope_binding decls = EMPTY_SCOPE_BINDING; |
c8094d83 | 3141 | |
b655c310 NS |
3142 | *newval = *newtype = NULL_TREE; |
3143 | if (!qualified_lookup_using_namespace (name, scope, &decls, 0)) | |
3144 | /* Lookup error */ | |
3145 | return; | |
0fdc23b9 | 3146 | |
b655c310 | 3147 | if (!decls.value && !decls.type) |
5a167978 | 3148 | { |
b655c310 NS |
3149 | error ("%qD not declared", name); |
3150 | return; | |
5a167978 | 3151 | } |
98ed9dae | 3152 | |
b655c310 NS |
3153 | /* Shift the old and new bindings around so we're comparing class and |
3154 | enumeration names to each other. */ | |
3155 | if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval)) | |
5a167978 | 3156 | { |
b655c310 NS |
3157 | oldtype = oldval; |
3158 | oldval = NULL_TREE; | |
98ed9dae | 3159 | } |
b655c310 NS |
3160 | |
3161 | if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value)) | |
140bec21 | 3162 | { |
b655c310 NS |
3163 | decls.type = decls.value; |
3164 | decls.value = NULL_TREE; | |
140bec21 | 3165 | } |
b655c310 NS |
3166 | |
3167 | if (decls.value) | |
98ed9dae | 3168 | { |
b655c310 NS |
3169 | /* Check for using functions. */ |
3170 | if (is_overloaded_fn (decls.value)) | |
3171 | { | |
3172 | tree tmp, tmp1; | |
5a167978 | 3173 | |
b655c310 NS |
3174 | if (oldval && !is_overloaded_fn (oldval)) |
3175 | { | |
3176 | error ("%qD is already declared in this scope", name); | |
3177 | oldval = NULL_TREE; | |
3178 | } | |
6097b0c3 | 3179 | |
b655c310 NS |
3180 | *newval = oldval; |
3181 | for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp)) | |
3182 | { | |
3183 | tree new_fn = OVL_CURRENT (tmp); | |
577b02d8 | 3184 | |
b655c310 NS |
3185 | /* Don't import functions that haven't been declared. */ |
3186 | if (DECL_ANTICIPATED (new_fn)) | |
3187 | continue; | |
577b02d8 | 3188 | |
b655c310 | 3189 | /* [namespace.udecl] |
c8094d83 | 3190 | |
b655c310 NS |
3191 | If a function declaration in namespace scope or block |
3192 | scope has the same name and the same parameter types as a | |
3193 | function introduced by a using declaration the program is | |
3194 | ill-formed. */ | |
3195 | for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) | |
3196 | { | |
3197 | tree old_fn = OVL_CURRENT (tmp1); | |
3db45ab5 | 3198 | |
b655c310 NS |
3199 | if (new_fn == old_fn) |
3200 | /* The function already exists in the current namespace. */ | |
3201 | break; | |
3202 | else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1)) | |
3203 | continue; /* this is a using decl */ | |
3204 | else if (compparms_for_decl_and_using_decl (new_fn, old_fn)) | |
3205 | { | |
3206 | /* There was already a non-using declaration in | |
3207 | this scope with the same parameter types. If both | |
3208 | are the same extern "C" functions, that's ok. */ | |
3209 | if (DECL_ANTICIPATED (old_fn) | |
3210 | && !DECL_HIDDEN_FRIEND_P (old_fn)) | |
3211 | /* Ignore anticipated built-ins. */; | |
3212 | else if (decls_match (new_fn, old_fn)) | |
3213 | break; | |
3214 | else | |
3215 | { | |
3216 | diagnose_name_conflict (new_fn, old_fn); | |
3217 | break; | |
3218 | } | |
3219 | } | |
3220 | } | |
1e9f5818 | 3221 | |
b655c310 NS |
3222 | /* If we broke out of the loop, there's no reason to add |
3223 | this function to the using declarations for this | |
3224 | scope. */ | |
3225 | if (tmp1) | |
3226 | continue; | |
3227 | ||
3228 | /* If we are adding to an existing OVERLOAD, then we no | |
3229 | longer know the type of the set of functions. */ | |
3230 | if (*newval && TREE_CODE (*newval) == OVERLOAD) | |
3231 | TREE_TYPE (*newval) = unknown_type_node; | |
3232 | /* Add this new function to the set. */ | |
3233 | *newval = build_overload (OVL_CURRENT (tmp), *newval); | |
3234 | /* If there is only one function, then we use its type. (A | |
3235 | using-declaration naming a single function can be used in | |
3236 | contexts where overload resolution cannot be | |
3237 | performed.) */ | |
3238 | if (TREE_CODE (*newval) != OVERLOAD) | |
3239 | { | |
3240 | *newval = ovl_cons (*newval, NULL_TREE); | |
3241 | TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp)); | |
3242 | } | |
3243 | OVL_USED (*newval) = 1; | |
b01e6d2b | 3244 | } |
98ed9dae | 3245 | } |
b655c310 | 3246 | else |
31f7f784 | 3247 | { |
b655c310 NS |
3248 | /* If we're declaring a non-function and OLDVAL is an anticipated |
3249 | built-in, just pretend it isn't there. */ | |
3250 | if (oldval | |
3251 | && TREE_CODE (oldval) == FUNCTION_DECL | |
3252 | && DECL_ANTICIPATED (oldval) | |
3253 | && !DECL_HIDDEN_FRIEND_P (oldval)) | |
3254 | oldval = NULL_TREE; | |
3255 | ||
3256 | *newval = decls.value; | |
3257 | if (oldval && !decls_match (*newval, oldval)) | |
3258 | error ("%qD is already declared in this scope", name); | |
31f7f784 | 3259 | } |
b655c310 NS |
3260 | } |
3261 | else | |
3262 | *newval = oldval; | |
3263 | ||
3264 | if (decls.type && TREE_CODE (decls.type) == TREE_LIST) | |
3265 | { | |
3266 | error ("reference to %qD is ambiguous", name); | |
3267 | print_candidates (decls.type); | |
3268 | } | |
3269 | else | |
3270 | { | |
3271 | *newtype = decls.type; | |
3272 | if (oldtype && *newtype && !decls_match (oldtype, *newtype)) | |
3273 | error ("%qD is already declared in this scope", name); | |
3274 | } | |
3275 | ||
3276 | /* If *newval is empty, shift any class or enumeration name down. */ | |
3277 | if (!*newval) | |
3278 | { | |
3279 | *newval = *newtype; | |
3280 | *newtype = NULL_TREE; | |
3281 | } | |
3282 | } | |
3283 | ||
3284 | /* Process a using-declaration at function scope. */ | |
3285 | ||
3286 | void | |
3287 | do_local_using_decl (tree decl, tree scope, tree name) | |
3288 | { | |
3289 | tree oldval, oldtype, newval, newtype; | |
3290 | tree orig_decl = decl; | |
3291 | ||
3292 | decl = validate_nonmember_using_decl (decl, scope, name); | |
3293 | if (decl == NULL_TREE) | |
3294 | return; | |
3295 | ||
3296 | if (building_stmt_list_p () | |
3297 | && at_function_scope_p ()) | |
3298 | add_decl_expr (decl); | |
3299 | ||
3300 | oldval = lookup_name_innermost_nonclass_level (name); | |
3301 | oldtype = lookup_type_current_level (name); | |
3302 | ||
3303 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); | |
3304 | ||
3305 | if (newval) | |
3306 | { | |
3307 | if (is_overloaded_fn (newval)) | |
577b02d8 | 3308 | { |
b655c310 NS |
3309 | tree fn, term; |
3310 | ||
3311 | /* We only need to push declarations for those functions | |
3312 | that were not already bound in the current level. | |
3313 | The old value might be NULL_TREE, it might be a single | |
3314 | function, or an OVERLOAD. */ | |
3315 | if (oldval && TREE_CODE (oldval) == OVERLOAD) | |
3316 | term = OVL_FUNCTION (oldval); | |
3317 | else | |
3318 | term = oldval; | |
3319 | for (fn = newval; fn && OVL_CURRENT (fn) != term; | |
3320 | fn = OVL_NEXT (fn)) | |
3321 | push_overloaded_decl (OVL_CURRENT (fn), | |
3322 | PUSH_LOCAL | PUSH_USING, | |
3323 | false); | |
577b02d8 | 3324 | } |
b655c310 NS |
3325 | else |
3326 | push_local_binding (name, newval, PUSH_USING); | |
3327 | } | |
3328 | if (newtype) | |
3329 | { | |
3330 | push_local_binding (name, newtype, PUSH_USING); | |
3331 | set_identifier_type_value (name, newtype); | |
1e9f5818 | 3332 | } |
98ed9dae | 3333 | |
b655c310 NS |
3334 | /* Emit debug info. */ |
3335 | if (!processing_template_decl) | |
3336 | cp_emit_debug_info_for_using (orig_decl, current_scope()); | |
5a167978 GDR |
3337 | } |
3338 | ||
b655c310 NS |
3339 | /* Returns true if ROOT (a namespace, class, or function) encloses |
3340 | CHILD. CHILD may be either a class type or a namespace. */ | |
575bfb00 | 3341 | |
b655c310 NS |
3342 | bool |
3343 | is_ancestor (tree root, tree child) | |
00e8de68 | 3344 | { |
b655c310 NS |
3345 | gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL |
3346 | || TREE_CODE (root) == FUNCTION_DECL | |
3347 | || CLASS_TYPE_P (root))); | |
3348 | gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL | |
3349 | || CLASS_TYPE_P (child))); | |
00e8de68 | 3350 | |
b655c310 NS |
3351 | /* The global namespace encloses everything. */ |
3352 | if (root == global_namespace) | |
3353 | return true; | |
00e8de68 | 3354 | |
b655c310 NS |
3355 | while (true) |
3356 | { | |
3357 | /* If we've run out of scopes, stop. */ | |
3358 | if (!child) | |
3359 | return false; | |
3360 | /* If we've reached the ROOT, it encloses CHILD. */ | |
3361 | if (root == child) | |
3362 | return true; | |
3363 | /* Go out one level. */ | |
3364 | if (TYPE_P (child)) | |
3365 | child = TYPE_NAME (child); | |
3366 | child = DECL_CONTEXT (child); | |
3367 | } | |
575bfb00 LC |
3368 | } |
3369 | ||
b655c310 NS |
3370 | /* Enter the class or namespace scope indicated by T suitable for name |
3371 | lookup. T can be arbitrary scope, not necessary nested inside the | |
3372 | current scope. Returns a non-null scope to pop iff pop_scope | |
3373 | should be called later to exit this scope. */ | |
ed3cf953 | 3374 | |
b655c310 NS |
3375 | tree |
3376 | push_scope (tree t) | |
ed3cf953 | 3377 | { |
b655c310 NS |
3378 | if (TREE_CODE (t) == NAMESPACE_DECL) |
3379 | push_decl_namespace (t); | |
3380 | else if (CLASS_TYPE_P (t)) | |
3381 | { | |
3382 | if (!at_class_scope_p () | |
3383 | || !same_type_p (current_class_type, t)) | |
3384 | push_nested_class (t); | |
3385 | else | |
3386 | /* T is the same as the current scope. There is therefore no | |
3387 | need to re-enter the scope. Since we are not actually | |
3388 | pushing a new scope, our caller should not call | |
3389 | pop_scope. */ | |
3390 | t = NULL_TREE; | |
3391 | } | |
ed3cf953 | 3392 | |
b655c310 | 3393 | return t; |
575bfb00 LC |
3394 | } |
3395 | ||
b655c310 | 3396 | /* Leave scope pushed by push_scope. */ |
575bfb00 LC |
3397 | |
3398 | void | |
b655c310 | 3399 | pop_scope (tree t) |
575bfb00 | 3400 | { |
b655c310 NS |
3401 | if (t == NULL_TREE) |
3402 | return; | |
3403 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
3404 | pop_decl_namespace (); | |
3405 | else if CLASS_TYPE_P (t) | |
3406 | pop_nested_class (); | |
ed3cf953 GDR |
3407 | } |
3408 | ||
b655c310 | 3409 | /* Subroutine of push_inner_scope. */ |
5a167978 | 3410 | |
b655c310 NS |
3411 | static void |
3412 | push_inner_scope_r (tree outer, tree inner) | |
5a167978 | 3413 | { |
b655c310 | 3414 | tree prev; |
5ae9ba3e | 3415 | |
b655c310 NS |
3416 | if (outer == inner |
3417 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
abc088aa | 3418 | return; |
b655c310 NS |
3419 | |
3420 | prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
3421 | if (outer != prev) | |
3422 | push_inner_scope_r (outer, prev); | |
3423 | if (TREE_CODE (inner) == NAMESPACE_DECL) | |
5ae9ba3e | 3424 | { |
b655c310 NS |
3425 | cp_binding_level *save_template_parm = 0; |
3426 | /* Temporary take out template parameter scopes. They are saved | |
3427 | in reversed order in save_template_parm. */ | |
3428 | while (current_binding_level->kind == sk_template_parms) | |
160594b0 | 3429 | { |
b655c310 NS |
3430 | cp_binding_level *b = current_binding_level; |
3431 | current_binding_level = b->level_chain; | |
3432 | b->level_chain = save_template_parm; | |
3433 | save_template_parm = b; | |
160594b0 | 3434 | } |
b655c310 NS |
3435 | |
3436 | resume_scope (NAMESPACE_LEVEL (inner)); | |
3437 | current_namespace = inner; | |
3438 | ||
3439 | /* Restore template parameter scopes. */ | |
3440 | while (save_template_parm) | |
160594b0 | 3441 | { |
b655c310 NS |
3442 | cp_binding_level *b = save_template_parm; |
3443 | save_template_parm = b->level_chain; | |
3444 | b->level_chain = current_binding_level; | |
3445 | current_binding_level = b; | |
160594b0 | 3446 | } |
5ae9ba3e | 3447 | } |
b655c310 NS |
3448 | else |
3449 | pushclass (inner); | |
c8094d83 | 3450 | } |
5a167978 | 3451 | |
b655c310 NS |
3452 | /* Enter the scope INNER from current scope. INNER must be a scope |
3453 | nested inside current scope. This works with both name lookup and | |
3454 | pushing name into scope. In case a template parameter scope is present, | |
3455 | namespace is pushed under the template parameter scope according to | |
3456 | name lookup rule in 14.6.1/6. | |
3457 | ||
3458 | Return the former current scope suitable for pop_inner_scope. */ | |
5a167978 | 3459 | |
ae099258 | 3460 | tree |
b655c310 | 3461 | push_inner_scope (tree inner) |
5a167978 | 3462 | { |
b655c310 NS |
3463 | tree outer = current_scope (); |
3464 | if (!outer) | |
3465 | outer = current_namespace; | |
5a167978 | 3466 | |
b655c310 NS |
3467 | push_inner_scope_r (outer, inner); |
3468 | return outer; | |
5a167978 GDR |
3469 | } |
3470 | ||
b655c310 | 3471 | /* Exit the current scope INNER back to scope OUTER. */ |
00e8de68 | 3472 | |
b655c310 NS |
3473 | void |
3474 | pop_inner_scope (tree outer, tree inner) | |
0ed5edac | 3475 | { |
b655c310 NS |
3476 | if (outer == inner |
3477 | || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) | |
3478 | return; | |
0ed5edac | 3479 | |
b655c310 | 3480 | while (outer != inner) |
65567efa | 3481 | { |
b655c310 | 3482 | if (TREE_CODE (inner) == NAMESPACE_DECL) |
65567efa | 3483 | { |
b655c310 NS |
3484 | cp_binding_level *save_template_parm = 0; |
3485 | /* Temporary take out template parameter scopes. They are saved | |
3486 | in reversed order in save_template_parm. */ | |
3487 | while (current_binding_level->kind == sk_template_parms) | |
65567efa | 3488 | { |
b655c310 NS |
3489 | cp_binding_level *b = current_binding_level; |
3490 | current_binding_level = b->level_chain; | |
3491 | b->level_chain = save_template_parm; | |
3492 | save_template_parm = b; | |
65567efa JM |
3493 | } |
3494 | ||
b655c310 | 3495 | pop_namespace (); |
65567efa | 3496 | |
b655c310 NS |
3497 | /* Restore template parameter scopes. */ |
3498 | while (save_template_parm) | |
7cb73573 | 3499 | { |
b655c310 NS |
3500 | cp_binding_level *b = save_template_parm; |
3501 | save_template_parm = b->level_chain; | |
3502 | b->level_chain = current_binding_level; | |
3503 | current_binding_level = b; | |
7cb73573 | 3504 | } |
e3501bab | 3505 | } |
65567efa | 3506 | else |
b655c310 NS |
3507 | popclass (); |
3508 | ||
3509 | inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); | |
65567efa | 3510 | } |
b655c310 NS |
3511 | } |
3512 | \f | |
3513 | /* Do a pushlevel for class declarations. */ | |
65567efa | 3514 | |
b655c310 NS |
3515 | void |
3516 | pushlevel_class (void) | |
3517 | { | |
3518 | class_binding_level = begin_scope (sk_class, current_class_type); | |
65567efa | 3519 | } |
0ed5edac | 3520 | |
b655c310 NS |
3521 | /* ...and a poplevel for class declarations. */ |
3522 | ||
3523 | void | |
3524 | poplevel_class (void) | |
00e8de68 | 3525 | { |
b655c310 NS |
3526 | cp_binding_level *level = class_binding_level; |
3527 | cp_class_binding *cb; | |
3528 | size_t i; | |
3529 | tree shadowed; | |
00e8de68 | 3530 | |
575bfb00 | 3531 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 3532 | gcc_assert (level != 0); |
c8094d83 | 3533 | |
b655c310 NS |
3534 | /* If we're leaving a toplevel class, cache its binding level. */ |
3535 | if (current_class_depth == 1) | |
3536 | previous_class_level = level; | |
3537 | for (shadowed = level->type_shadowed; | |
3538 | shadowed; | |
3539 | shadowed = TREE_CHAIN (shadowed)) | |
3540 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); | |
00e8de68 | 3541 | |
b655c310 NS |
3542 | /* Remove the bindings for all of the class-level declarations. */ |
3543 | if (level->class_shadowed) | |
00e8de68 | 3544 | { |
b655c310 | 3545 | FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) |
0cbd7506 | 3546 | { |
b655c310 NS |
3547 | IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; |
3548 | cxx_binding_free (cb->base); | |
0cbd7506 | 3549 | } |
b655c310 NS |
3550 | ggc_free (level->class_shadowed); |
3551 | level->class_shadowed = NULL; | |
00e8de68 GDR |
3552 | } |
3553 | ||
b655c310 NS |
3554 | /* Now, pop out of the binding level which we created up in the |
3555 | `pushlevel_class' routine. */ | |
3556 | gcc_assert (current_binding_level == level); | |
3557 | leave_scope (); | |
3558 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
3559 | } | |
3560 | ||
3561 | /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as | |
3562 | appropriate. DECL is the value to which a name has just been | |
3563 | bound. CLASS_TYPE is the class in which the lookup occurred. */ | |
3564 | ||
3565 | static void | |
3566 | set_inherited_value_binding_p (cxx_binding *binding, tree decl, | |
3567 | tree class_type) | |
3568 | { | |
3569 | if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) | |
00e8de68 | 3570 | { |
b655c310 NS |
3571 | tree context; |
3572 | ||
3573 | if (TREE_CODE (decl) == OVERLOAD) | |
3574 | context = ovl_scope (decl); | |
b9e75696 | 3575 | else |
ed36980c | 3576 | { |
b655c310 NS |
3577 | gcc_assert (DECL_P (decl)); |
3578 | context = context_for_name_lookup (decl); | |
ed36980c | 3579 | } |
00e8de68 | 3580 | |
b655c310 NS |
3581 | if (is_properly_derived_from (class_type, context)) |
3582 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
3583 | else | |
3584 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
3585 | } | |
3586 | else if (binding->value == decl) | |
3587 | /* We only encounter a TREE_LIST when there is an ambiguity in the | |
3588 | base classes. Such an ambiguity can be overridden by a | |
3589 | definition in this class. */ | |
3590 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
3591 | else | |
3592 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
00e8de68 GDR |
3593 | } |
3594 | ||
b655c310 | 3595 | /* Make the declaration of X appear in CLASS scope. */ |
00e8de68 | 3596 | |
b655c310 NS |
3597 | bool |
3598 | pushdecl_class_level (tree x) | |
00e8de68 | 3599 | { |
b655c310 NS |
3600 | tree name; |
3601 | bool is_valid = true; | |
3602 | bool subtime; | |
00e8de68 | 3603 | |
b655c310 NS |
3604 | /* Do nothing if we're adding to an outer lambda closure type, |
3605 | outer_binding will add it later if it's needed. */ | |
3606 | if (current_class_type != class_binding_level->this_entity) | |
3607 | return true; | |
00e8de68 | 3608 | |
b655c310 NS |
3609 | subtime = timevar_cond_start (TV_NAME_LOOKUP); |
3610 | /* Get the name of X. */ | |
3611 | if (TREE_CODE (x) == OVERLOAD) | |
3612 | name = DECL_NAME (get_first_fn (x)); | |
00e8de68 | 3613 | else |
b655c310 | 3614 | name = DECL_NAME (x); |
00e8de68 | 3615 | |
b655c310 | 3616 | if (name) |
00e8de68 | 3617 | { |
b655c310 NS |
3618 | is_valid = push_class_level_binding (name, x); |
3619 | if (TREE_CODE (x) == TYPE_DECL) | |
3620 | set_identifier_type_value (name, x); | |
00e8de68 | 3621 | } |
b655c310 NS |
3622 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
3623 | { | |
3624 | /* If X is an anonymous aggregate, all of its members are | |
3625 | treated as if they were members of the class containing the | |
3626 | aggregate, for naming purposes. */ | |
3627 | tree f; | |
00e8de68 | 3628 | |
b655c310 NS |
3629 | for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f)) |
3630 | { | |
3631 | location_t save_location = input_location; | |
3632 | input_location = DECL_SOURCE_LOCATION (f); | |
3633 | if (!pushdecl_class_level (f)) | |
3634 | is_valid = false; | |
3635 | input_location = save_location; | |
3636 | } | |
3637 | } | |
575bfb00 | 3638 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 3639 | return is_valid; |
00e8de68 GDR |
3640 | } |
3641 | ||
b655c310 NS |
3642 | /* Return the BINDING (if any) for NAME in SCOPE, which is a class |
3643 | scope. If the value returned is non-NULL, and the PREVIOUS field | |
3644 | is not set, callers must set the PREVIOUS field explicitly. */ | |
5a167978 | 3645 | |
b655c310 NS |
3646 | static cxx_binding * |
3647 | get_class_binding (tree name, cp_binding_level *scope) | |
5a167978 | 3648 | { |
b655c310 NS |
3649 | tree class_type; |
3650 | tree type_binding; | |
3651 | tree value_binding; | |
3652 | cxx_binding *binding; | |
5a167978 | 3653 | |
b655c310 | 3654 | class_type = scope->this_entity; |
5a167978 | 3655 | |
b655c310 NS |
3656 | /* Get the type binding. */ |
3657 | type_binding = lookup_member (class_type, name, | |
3658 | /*protect=*/2, /*want_type=*/true, | |
3659 | tf_warning_or_error); | |
3660 | /* Get the value binding. */ | |
3661 | value_binding = lookup_member (class_type, name, | |
3662 | /*protect=*/2, /*want_type=*/false, | |
3663 | tf_warning_or_error); | |
5a167978 | 3664 | |
b655c310 NS |
3665 | if (value_binding |
3666 | && (TREE_CODE (value_binding) == TYPE_DECL | |
3667 | || DECL_CLASS_TEMPLATE_P (value_binding) | |
3668 | || (TREE_CODE (value_binding) == TREE_LIST | |
3669 | && TREE_TYPE (value_binding) == error_mark_node | |
3670 | && (TREE_CODE (TREE_VALUE (value_binding)) | |
3671 | == TYPE_DECL)))) | |
3672 | /* We found a type binding, even when looking for a non-type | |
3673 | binding. This means that we already processed this binding | |
3674 | above. */ | |
3675 | ; | |
3676 | else if (value_binding) | |
3677 | { | |
3678 | if (TREE_CODE (value_binding) == TREE_LIST | |
3679 | && TREE_TYPE (value_binding) == error_mark_node) | |
3680 | /* NAME is ambiguous. */ | |
3681 | ; | |
3682 | else if (BASELINK_P (value_binding)) | |
3683 | /* NAME is some overloaded functions. */ | |
3684 | value_binding = BASELINK_FUNCTIONS (value_binding); | |
3685 | } | |
5a167978 | 3686 | |
b655c310 NS |
3687 | /* If we found either a type binding or a value binding, create a |
3688 | new binding object. */ | |
3689 | if (type_binding || value_binding) | |
3690 | { | |
3691 | binding = new_class_binding (name, | |
3692 | value_binding, | |
3693 | type_binding, | |
3694 | scope); | |
3695 | /* This is a class-scope binding, not a block-scope binding. */ | |
3696 | LOCAL_BINDING_P (binding) = 0; | |
3697 | set_inherited_value_binding_p (binding, value_binding, class_type); | |
3698 | } | |
575bfb00 | 3699 | else |
b655c310 NS |
3700 | binding = NULL; |
3701 | ||
3702 | return binding; | |
575bfb00 LC |
3703 | } |
3704 | ||
b655c310 NS |
3705 | /* Make the declaration(s) of X appear in CLASS scope under the name |
3706 | NAME. Returns true if the binding is valid. */ | |
575bfb00 | 3707 | |
b655c310 NS |
3708 | static bool |
3709 | push_class_level_binding_1 (tree name, tree x) | |
575bfb00 | 3710 | { |
b655c310 NS |
3711 | cxx_binding *binding; |
3712 | tree decl = x; | |
3713 | bool ok; | |
5a167978 | 3714 | |
b655c310 NS |
3715 | /* The class_binding_level will be NULL if x is a template |
3716 | parameter name in a member template. */ | |
3717 | if (!class_binding_level) | |
3718 | return true; | |
5a167978 | 3719 | |
b655c310 NS |
3720 | if (name == error_mark_node) |
3721 | return false; | |
166206ce | 3722 | |
b655c310 NS |
3723 | /* Can happen for an erroneous declaration (c++/60384). */ |
3724 | if (!identifier_p (name)) | |
3725 | { | |
3726 | gcc_assert (errorcount || sorrycount); | |
3727 | return false; | |
3728 | } | |
5a167978 | 3729 | |
b655c310 NS |
3730 | /* Check for invalid member names. But don't worry about a default |
3731 | argument-scope lambda being pushed after the class is complete. */ | |
3732 | gcc_assert (TYPE_BEING_DEFINED (current_class_type) | |
3733 | || LAMBDA_TYPE_P (TREE_TYPE (decl))); | |
3734 | /* Check that we're pushing into the right binding level. */ | |
3735 | gcc_assert (current_class_type == class_binding_level->this_entity); | |
5a167978 | 3736 | |
b655c310 NS |
3737 | /* We could have been passed a tree list if this is an ambiguous |
3738 | declaration. If so, pull the declaration out because | |
3739 | check_template_shadow will not handle a TREE_LIST. */ | |
3740 | if (TREE_CODE (decl) == TREE_LIST | |
3741 | && TREE_TYPE (decl) == error_mark_node) | |
3742 | decl = TREE_VALUE (decl); | |
6097b0c3 | 3743 | |
b655c310 NS |
3744 | if (!check_template_shadow (decl)) |
3745 | return false; | |
5a167978 | 3746 | |
b655c310 | 3747 | /* [class.mem] |
00e8de68 | 3748 | |
b655c310 NS |
3749 | If T is the name of a class, then each of the following shall |
3750 | have a name different from T: | |
00e8de68 | 3751 | |
b655c310 | 3752 | -- every static data member of class T; |
00e8de68 | 3753 | |
b655c310 NS |
3754 | -- every member of class T that is itself a type; |
3755 | ||
3756 | -- every enumerator of every member of class T that is an | |
3757 | enumerated type; | |
3758 | ||
3759 | -- every member of every anonymous union that is a member of | |
3760 | class T. | |
3761 | ||
3762 | (Non-static data members were also forbidden to have the same | |
3763 | name as T until TC1.) */ | |
3764 | if ((VAR_P (x) | |
3765 | || TREE_CODE (x) == CONST_DECL | |
3766 | || (TREE_CODE (x) == TYPE_DECL | |
3767 | && !DECL_SELF_REFERENCE_P (x)) | |
3768 | /* A data member of an anonymous union. */ | |
3769 | || (TREE_CODE (x) == FIELD_DECL | |
3770 | && DECL_CONTEXT (x) != current_class_type)) | |
3771 | && DECL_NAME (x) == constructor_name (current_class_type)) | |
3772 | { | |
3773 | tree scope = context_for_name_lookup (x); | |
3774 | if (TYPE_P (scope) && same_type_p (scope, current_class_type)) | |
3775 | { | |
3776 | error ("%qD has the same name as the class in which it is " | |
3777 | "declared", | |
3778 | x); | |
3779 | return false; | |
3780 | } | |
3781 | } | |
3782 | ||
3783 | /* Get the current binding for NAME in this class, if any. */ | |
3784 | binding = IDENTIFIER_BINDING (name); | |
3785 | if (!binding || binding->scope != class_binding_level) | |
00e8de68 | 3786 | { |
b655c310 NS |
3787 | binding = get_class_binding (name, class_binding_level); |
3788 | /* If a new binding was created, put it at the front of the | |
3789 | IDENTIFIER_BINDING list. */ | |
3790 | if (binding) | |
0cbd7506 | 3791 | { |
b655c310 NS |
3792 | binding->previous = IDENTIFIER_BINDING (name); |
3793 | IDENTIFIER_BINDING (name) = binding; | |
0cbd7506 | 3794 | } |
b655c310 NS |
3795 | } |
3796 | ||
3797 | /* If there is already a binding, then we may need to update the | |
3798 | current value. */ | |
3799 | if (binding && binding->value) | |
3800 | { | |
3801 | tree bval = binding->value; | |
3802 | tree old_decl = NULL_TREE; | |
3803 | tree target_decl = strip_using_decl (decl); | |
3804 | tree target_bval = strip_using_decl (bval); | |
3805 | ||
3806 | if (INHERITED_VALUE_BINDING_P (binding)) | |
0cbd7506 | 3807 | { |
b655c310 NS |
3808 | /* If the old binding was from a base class, and was for a |
3809 | tag name, slide it over to make room for the new binding. | |
3810 | The old binding is still visible if explicitly qualified | |
3811 | with a class-key. */ | |
3812 | if (TREE_CODE (target_bval) == TYPE_DECL | |
3813 | && DECL_ARTIFICIAL (target_bval) | |
3814 | && !(TREE_CODE (target_decl) == TYPE_DECL | |
3815 | && DECL_ARTIFICIAL (target_decl))) | |
3816 | { | |
3817 | old_decl = binding->type; | |
3818 | binding->type = bval; | |
3819 | binding->value = NULL_TREE; | |
3820 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
3821 | } | |
3822 | else | |
3823 | { | |
3824 | old_decl = bval; | |
3825 | /* Any inherited type declaration is hidden by the type | |
3826 | declaration in the derived class. */ | |
3827 | if (TREE_CODE (target_decl) == TYPE_DECL | |
3828 | && DECL_ARTIFICIAL (target_decl)) | |
3829 | binding->type = NULL_TREE; | |
3830 | } | |
00e8de68 | 3831 | } |
b655c310 NS |
3832 | else if (TREE_CODE (target_decl) == OVERLOAD |
3833 | && is_overloaded_fn (target_bval)) | |
3834 | old_decl = bval; | |
3835 | else if (TREE_CODE (decl) == USING_DECL | |
3836 | && TREE_CODE (bval) == USING_DECL | |
3837 | && same_type_p (USING_DECL_SCOPE (decl), | |
3838 | USING_DECL_SCOPE (bval))) | |
3839 | /* This is a using redeclaration that will be diagnosed later | |
3840 | in supplement_binding */ | |
3841 | ; | |
3842 | else if (TREE_CODE (decl) == USING_DECL | |
3843 | && TREE_CODE (bval) == USING_DECL | |
3844 | && DECL_DEPENDENT_P (decl) | |
3845 | && DECL_DEPENDENT_P (bval)) | |
3846 | return true; | |
3847 | else if (TREE_CODE (decl) == USING_DECL | |
3848 | && is_overloaded_fn (target_bval)) | |
3849 | old_decl = bval; | |
3850 | else if (TREE_CODE (bval) == USING_DECL | |
3851 | && is_overloaded_fn (target_decl)) | |
3852 | return true; | |
3853 | ||
3854 | if (old_decl && binding->scope == class_binding_level) | |
0cbd7506 | 3855 | { |
b655c310 NS |
3856 | binding->value = x; |
3857 | /* It is always safe to clear INHERITED_VALUE_BINDING_P | |
3858 | here. This function is only used to register bindings | |
3859 | from with the class definition itself. */ | |
3860 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
3861 | return true; | |
0cbd7506 | 3862 | } |
00e8de68 | 3863 | } |
00e8de68 | 3864 | |
b655c310 NS |
3865 | /* Note that we declared this value so that we can issue an error if |
3866 | this is an invalid redeclaration of a name already used for some | |
3867 | other purpose. */ | |
3868 | note_name_declared_in_class (name, decl); | |
575bfb00 | 3869 | |
b655c310 NS |
3870 | /* If we didn't replace an existing binding, put the binding on the |
3871 | stack of bindings for the identifier, and update the shadowed | |
3872 | list. */ | |
3873 | if (binding && binding->scope == class_binding_level) | |
3874 | /* Supplement the existing binding. */ | |
3875 | ok = supplement_binding (binding, decl); | |
3876 | else | |
5a167978 | 3877 | { |
b655c310 NS |
3878 | /* Create a new binding. */ |
3879 | push_binding (name, decl, class_binding_level); | |
3880 | ok = true; | |
5a167978 GDR |
3881 | } |
3882 | ||
b655c310 | 3883 | return ok; |
575bfb00 LC |
3884 | } |
3885 | ||
b655c310 | 3886 | /* Wrapper for push_class_level_binding_1. */ |
575bfb00 | 3887 | |
b655c310 NS |
3888 | bool |
3889 | push_class_level_binding (tree name, tree x) | |
575bfb00 | 3890 | { |
b655c310 | 3891 | bool ret; |
575bfb00 | 3892 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 | 3893 | ret = push_class_level_binding_1 (name, x); |
575bfb00 | 3894 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 | 3895 | return ret; |
5a167978 GDR |
3896 | } |
3897 | ||
b655c310 NS |
3898 | /* Process "using SCOPE::NAME" in a class scope. Return the |
3899 | USING_DECL created. */ | |
5a167978 | 3900 | |
b655c310 NS |
3901 | tree |
3902 | do_class_using_decl (tree scope, tree name) | |
5a167978 | 3903 | { |
b655c310 NS |
3904 | /* The USING_DECL returned by this function. */ |
3905 | tree value; | |
3906 | /* The declaration (or declarations) name by this using | |
3907 | declaration. NULL if we are in a template and cannot figure out | |
3908 | what has been named. */ | |
3909 | tree decl; | |
3910 | /* True if SCOPE is a dependent type. */ | |
3911 | bool scope_dependent_p; | |
3912 | /* True if SCOPE::NAME is dependent. */ | |
3913 | bool name_dependent_p; | |
3914 | /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */ | |
3915 | bool bases_dependent_p; | |
3916 | tree binfo; | |
166206ce | 3917 | |
b655c310 NS |
3918 | if (name == error_mark_node) |
3919 | return NULL_TREE; | |
166206ce | 3920 | |
b655c310 NS |
3921 | if (!scope || !TYPE_P (scope)) |
3922 | { | |
3923 | error ("using-declaration for non-member at class scope"); | |
3924 | return NULL_TREE; | |
3925 | } | |
166206ce | 3926 | |
b655c310 NS |
3927 | /* Make sure the name is not invalid */ |
3928 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6097b0c3 | 3929 | { |
b655c310 NS |
3930 | error ("%<%T::%D%> names destructor", scope, name); |
3931 | return NULL_TREE; | |
6097b0c3 | 3932 | } |
b655c310 NS |
3933 | /* Using T::T declares inheriting ctors, even if T is a typedef. */ |
3934 | if (MAYBE_CLASS_TYPE_P (scope) | |
3935 | && (name == TYPE_IDENTIFIER (scope) | |
3936 | || constructor_name_p (name, scope))) | |
6097b0c3 | 3937 | { |
b655c310 NS |
3938 | maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); |
3939 | name = ctor_identifier; | |
3940 | CLASSTYPE_NON_AGGREGATE (current_class_type) = true; | |
d19c0f4b | 3941 | } |
b655c310 NS |
3942 | if (constructor_name_p (name, current_class_type)) |
3943 | { | |
3944 | error ("%<%T::%D%> names constructor in %qT", | |
3945 | scope, name, current_class_type); | |
3946 | return NULL_TREE; | |
3947 | } | |
3948 | ||
3949 | scope_dependent_p = dependent_scope_p (scope); | |
3950 | name_dependent_p = (scope_dependent_p | |
3951 | || (IDENTIFIER_TYPENAME_P (name) | |
3952 | && dependent_type_p (TREE_TYPE (name)))); | |
5a167978 | 3953 | |
b655c310 | 3954 | bases_dependent_p = any_dependent_bases_p (); |
86098eb8 | 3955 | |
b655c310 | 3956 | decl = NULL_TREE; |
86098eb8 | 3957 | |
b655c310 | 3958 | /* From [namespace.udecl]: |
1b255e8f | 3959 | |
b655c310 NS |
3960 | A using-declaration used as a member-declaration shall refer to a |
3961 | member of a base class of the class being defined. | |
3962 | ||
3963 | In general, we cannot check this constraint in a template because | |
3964 | we do not know the entire set of base classes of the current | |
3965 | class type. Morover, if SCOPE is dependent, it might match a | |
3966 | non-dependent base. */ | |
3967 | ||
3968 | if (!scope_dependent_p) | |
86098eb8 | 3969 | { |
b655c310 NS |
3970 | base_kind b_kind; |
3971 | binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, | |
3972 | tf_warning_or_error); | |
3973 | if (b_kind < bk_proper_base) | |
86098eb8 | 3974 | { |
b655c310 | 3975 | if (!bases_dependent_p || b_kind == bk_same_type) |
9deb204a | 3976 | { |
b655c310 NS |
3977 | error_not_base_type (scope, current_class_type); |
3978 | return NULL_TREE; | |
9deb204a | 3979 | } |
86098eb8 | 3980 | } |
b655c310 NS |
3981 | else if (name == ctor_identifier |
3982 | && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo))) | |
3983 | { | |
3984 | error ("cannot inherit constructors from indirect base %qT", scope); | |
3985 | return NULL_TREE; | |
3986 | } | |
3987 | else if (!name_dependent_p) | |
3988 | { | |
3989 | decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); | |
3990 | if (!decl) | |
3991 | { | |
3992 | error ("no members matching %<%T::%D%> in %q#T", scope, name, | |
3993 | scope); | |
3994 | return NULL_TREE; | |
3995 | } | |
3996 | /* The binfo from which the functions came does not matter. */ | |
3997 | if (BASELINK_P (decl)) | |
3998 | decl = BASELINK_FUNCTIONS (decl); | |
3999 | } | |
86098eb8 | 4000 | } |
86098eb8 | 4001 | |
b655c310 NS |
4002 | value = build_lang_decl (USING_DECL, name, NULL_TREE); |
4003 | USING_DECL_DECLS (value) = decl; | |
4004 | USING_DECL_SCOPE (value) = scope; | |
4005 | DECL_DEPENDENT_P (value) = !decl; | |
a5e6b29b | 4006 | |
b655c310 | 4007 | return value; |
a5e6b29b GDR |
4008 | } |
4009 | ||
b655c310 NS |
4010 | \f |
4011 | /* Return the binding value for name in scope. */ | |
a5e6b29b | 4012 | |
b655c310 NS |
4013 | |
4014 | static tree | |
4015 | namespace_binding_1 (tree name, tree scope) | |
a5e6b29b | 4016 | { |
b655c310 | 4017 | cxx_binding *binding; |
d63d5d0c | 4018 | |
b655c310 NS |
4019 | if (SCOPE_FILE_SCOPE_P (scope)) |
4020 | scope = global_namespace; | |
4021 | else | |
4022 | /* Unnecessary for the global namespace because it can't be an alias. */ | |
4023 | scope = ORIGINAL_NAMESPACE (scope); | |
d63d5d0c | 4024 | |
b655c310 | 4025 | binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); |
a5e6b29b | 4026 | |
b655c310 NS |
4027 | return binding ? binding->value : NULL_TREE; |
4028 | } | |
a5e6b29b | 4029 | |
4b4b2e58 NS |
4030 | /* Return the binding for NAME in NS. If NS is NULL, look in |
4031 | global_namespace. */ | |
4032 | ||
a5e6b29b | 4033 | tree |
06aa5490 | 4034 | get_namespace_binding (tree ns, tree name) |
a5e6b29b | 4035 | { |
b655c310 | 4036 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 NS |
4037 | if (!ns) |
4038 | ns = global_namespace; | |
4039 | tree ret = namespace_binding_1 (name, ns); | |
b655c310 NS |
4040 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
4041 | return ret; | |
a5e6b29b GDR |
4042 | } |
4043 | ||
b655c310 | 4044 | static void |
4b4b2e58 | 4045 | set_namespace_binding (tree name, tree scope, tree val) |
5a167978 | 4046 | { |
b655c310 | 4047 | cxx_binding *b; |
c8094d83 | 4048 | |
b655c310 NS |
4049 | if (scope == NULL_TREE) |
4050 | scope = global_namespace; | |
4051 | b = binding_for_name (NAMESPACE_LEVEL (scope), name); | |
4052 | if (!b->value | |
4053 | /* For templates and using we create a single element OVERLOAD. | |
4054 | Look for the chain to know whether this is really augmenting | |
4055 | an existing overload. */ | |
4056 | || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val)) | |
4057 | || val == error_mark_node) | |
4058 | b->value = val; | |
4059 | else | |
4060 | supplement_binding (b, val); | |
5a167978 GDR |
4061 | } |
4062 | ||
06aa5490 NS |
4063 | /* Set value binding og NAME in the global namespace to VAL. Does not |
4064 | add it to the list of things in the namespace. */ | |
1e003829 | 4065 | |
b655c310 | 4066 | void |
06aa5490 | 4067 | set_global_binding (tree name, tree val) |
1e003829 | 4068 | { |
b655c310 | 4069 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4b4b2e58 NS |
4070 | |
4071 | set_namespace_binding (name, global_namespace, val); | |
4072 | ||
b655c310 | 4073 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
1e003829 JM |
4074 | } |
4075 | ||
b655c310 NS |
4076 | /* Set the context of a declaration to scope. Complain if we are not |
4077 | outside scope. */ | |
5a167978 | 4078 | |
b655c310 NS |
4079 | void |
4080 | set_decl_namespace (tree decl, tree scope, bool friendp) | |
5a167978 | 4081 | { |
b655c310 | 4082 | tree old; |
af92ab36 | 4083 | |
b655c310 NS |
4084 | /* Get rid of namespace aliases. */ |
4085 | scope = ORIGINAL_NAMESPACE (scope); | |
af92ab36 | 4086 | |
b655c310 NS |
4087 | /* It is ok for friends to be qualified in parallel space. */ |
4088 | if (!friendp && !is_ancestor (current_namespace, scope)) | |
4089 | error ("declaration of %qD not in a namespace surrounding %qD", | |
4090 | decl, scope); | |
4091 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
af92ab36 | 4092 | |
b655c310 NS |
4093 | /* Writing "int N::i" to declare a variable within "N" is invalid. */ |
4094 | if (scope == current_namespace) | |
af92ab36 | 4095 | { |
b655c310 NS |
4096 | if (at_namespace_scope_p ()) |
4097 | error ("explicit qualification in declaration of %qD", | |
4098 | decl); | |
4099 | return; | |
af92ab36 | 4100 | } |
c8094d83 | 4101 | |
b655c310 NS |
4102 | /* See whether this has been declared in the namespace. */ |
4103 | old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false, | |
4104 | /*complain*/true, /*hidden*/true); | |
4105 | if (old == error_mark_node) | |
4106 | /* No old declaration at all. */ | |
4107 | goto complain; | |
4108 | /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ | |
4109 | if (TREE_CODE (old) == TREE_LIST) | |
5a167978 | 4110 | { |
b655c310 NS |
4111 | error ("reference to %qD is ambiguous", decl); |
4112 | print_candidates (old); | |
4113 | return; | |
4114 | } | |
4115 | if (!is_overloaded_fn (decl)) | |
4116 | { | |
4117 | /* We might have found OLD in an inline namespace inside SCOPE. */ | |
4118 | if (TREE_CODE (decl) == TREE_CODE (old)) | |
4119 | DECL_CONTEXT (decl) = DECL_CONTEXT (old); | |
4120 | /* Don't compare non-function decls with decls_match here, since | |
4121 | it can't check for the correct constness at this | |
4122 | point. pushdecl will find those errors later. */ | |
4123 | return; | |
4124 | } | |
4125 | /* Since decl is a function, old should contain a function decl. */ | |
4126 | if (!is_overloaded_fn (old)) | |
4127 | goto complain; | |
4128 | /* We handle these in check_explicit_instantiation_namespace. */ | |
4129 | if (processing_explicit_instantiation) | |
4130 | return; | |
4131 | if (processing_template_decl || processing_specialization) | |
4132 | /* We have not yet called push_template_decl to turn a | |
4133 | FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't | |
4134 | match. But, we'll check later, when we construct the | |
4135 | template. */ | |
4136 | return; | |
4137 | /* Instantiations or specializations of templates may be declared as | |
4138 | friends in any namespace. */ | |
4139 | if (friendp && DECL_USE_TEMPLATE (decl)) | |
4140 | return; | |
4141 | if (is_overloaded_fn (old)) | |
4142 | { | |
4143 | tree found = NULL_TREE; | |
4144 | tree elt = old; | |
4145 | for (; elt; elt = OVL_NEXT (elt)) | |
5a167978 | 4146 | { |
b655c310 NS |
4147 | tree ofn = OVL_CURRENT (elt); |
4148 | /* Adjust DECL_CONTEXT first so decls_match will return true | |
4149 | if DECL will match a declaration in an inline namespace. */ | |
4150 | DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); | |
4151 | if (decls_match (decl, ofn)) | |
4152 | { | |
4153 | if (found && !decls_match (found, ofn)) | |
4154 | { | |
4155 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
4156 | error ("reference to %qD is ambiguous", decl); | |
4157 | print_candidates (old); | |
4158 | return; | |
4159 | } | |
4160 | found = ofn; | |
4161 | } | |
4162 | } | |
4163 | if (found) | |
4164 | { | |
4165 | if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found))) | |
4166 | goto complain; | |
4167 | if (DECL_HIDDEN_FRIEND_P (found)) | |
4168 | { | |
4169 | pedwarn (DECL_SOURCE_LOCATION (decl), 0, | |
0f2c4a8f MS |
4170 | "%qD has not been declared within %qD", decl, scope); |
4171 | inform (DECL_SOURCE_LOCATION (found), | |
4172 | "only here as a %<friend%>"); | |
b655c310 NS |
4173 | } |
4174 | DECL_CONTEXT (decl) = DECL_CONTEXT (found); | |
4175 | return; | |
5a167978 GDR |
4176 | } |
4177 | } | |
b655c310 | 4178 | else |
5a167978 | 4179 | { |
b655c310 NS |
4180 | DECL_CONTEXT (decl) = DECL_CONTEXT (old); |
4181 | if (decls_match (decl, old)) | |
4182 | return; | |
5a167978 | 4183 | } |
b655c310 NS |
4184 | |
4185 | /* It didn't work, go back to the explicit scope. */ | |
4186 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
4187 | complain: | |
4188 | error ("%qD should have been declared inside %qD", decl, scope); | |
5a167978 GDR |
4189 | } |
4190 | ||
b655c310 | 4191 | /* Return the namespace where the current declaration is declared. */ |
00e8de68 GDR |
4192 | |
4193 | tree | |
b655c310 | 4194 | current_decl_namespace (void) |
00e8de68 | 4195 | { |
b655c310 NS |
4196 | tree result; |
4197 | /* If we have been pushed into a different namespace, use it. */ | |
4198 | if (!vec_safe_is_empty (decl_namespace_list)) | |
4199 | return decl_namespace_list->last (); | |
00e8de68 | 4200 | |
b655c310 NS |
4201 | if (current_class_type) |
4202 | result = decl_namespace_context (current_class_type); | |
4203 | else if (current_function_decl) | |
4204 | result = decl_namespace_context (current_function_decl); | |
4205 | else | |
4206 | result = current_namespace; | |
4207 | return result; | |
00e8de68 GDR |
4208 | } |
4209 | ||
b655c310 NS |
4210 | /* Process any ATTRIBUTES on a namespace definition. Returns true if |
4211 | attribute visibility is seen. */ | |
00e8de68 | 4212 | |
b655c310 NS |
4213 | bool |
4214 | handle_namespace_attrs (tree ns, tree attributes) | |
00e8de68 | 4215 | { |
b655c310 NS |
4216 | tree d; |
4217 | bool saw_vis = false; | |
4218 | ||
4219 | for (d = attributes; d; d = TREE_CHAIN (d)) | |
af79925b | 4220 | { |
b655c310 NS |
4221 | tree name = get_attribute_name (d); |
4222 | tree args = TREE_VALUE (d); | |
4223 | ||
4224 | if (is_attribute_p ("visibility", name)) | |
4225 | { | |
4226 | /* attribute visibility is a property of the syntactic block | |
4227 | rather than the namespace as a whole, so we don't touch the | |
4228 | NAMESPACE_DECL at all. */ | |
4229 | tree x = args ? TREE_VALUE (args) : NULL_TREE; | |
4230 | if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) | |
4231 | { | |
4232 | warning (OPT_Wattributes, | |
4233 | "%qD attribute requires a single NTBS argument", | |
4234 | name); | |
4235 | continue; | |
4236 | } | |
4237 | ||
4238 | if (!TREE_PUBLIC (ns)) | |
4239 | warning (OPT_Wattributes, | |
4240 | "%qD attribute is meaningless since members of the " | |
4241 | "anonymous namespace get local symbols", name); | |
4242 | ||
4243 | push_visibility (TREE_STRING_POINTER (x), 1); | |
4244 | saw_vis = true; | |
4245 | } | |
4246 | else if (is_attribute_p ("abi_tag", name)) | |
4247 | { | |
4248 | if (!DECL_NAMESPACE_ASSOCIATIONS (ns)) | |
4249 | { | |
4250 | warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " | |
4251 | "namespace", name); | |
4252 | continue; | |
4253 | } | |
4254 | if (!DECL_NAME (ns)) | |
4255 | { | |
4256 | warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " | |
4257 | "namespace", name); | |
4258 | continue; | |
4259 | } | |
4260 | if (!args) | |
4261 | { | |
4262 | tree dn = DECL_NAME (ns); | |
4263 | args = build_string (IDENTIFIER_LENGTH (dn) + 1, | |
4264 | IDENTIFIER_POINTER (dn)); | |
4265 | TREE_TYPE (args) = char_array_type_node; | |
4266 | args = fix_string_type (args); | |
4267 | args = build_tree_list (NULL_TREE, args); | |
4268 | } | |
4269 | if (check_abi_tag_args (args, name)) | |
4270 | DECL_ATTRIBUTES (ns) = tree_cons (name, args, | |
4271 | DECL_ATTRIBUTES (ns)); | |
4272 | } | |
4273 | else | |
4274 | { | |
4275 | warning (OPT_Wattributes, "%qD attribute directive ignored", | |
4276 | name); | |
4277 | continue; | |
4278 | } | |
af79925b | 4279 | } |
bd3d082e | 4280 | |
b655c310 NS |
4281 | return saw_vis; |
4282 | } | |
4283 | /* Temporarily set the namespace for the current declaration. */ | |
bd3d082e | 4284 | |
b655c310 NS |
4285 | void |
4286 | push_decl_namespace (tree decl) | |
bd3d082e | 4287 | { |
b655c310 NS |
4288 | if (TREE_CODE (decl) != NAMESPACE_DECL) |
4289 | decl = decl_namespace_context (decl); | |
4290 | vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); | |
00e8de68 GDR |
4291 | } |
4292 | ||
b655c310 | 4293 | /* [namespace.memdef]/2 */ |
d63d5d0c | 4294 | |
b655c310 NS |
4295 | void |
4296 | pop_decl_namespace (void) | |
d63d5d0c | 4297 | { |
b655c310 NS |
4298 | decl_namespace_list->pop (); |
4299 | } | |
d63d5d0c | 4300 | |
b655c310 NS |
4301 | /* Return the namespace that is the common ancestor |
4302 | of two given namespaces. */ | |
d63d5d0c | 4303 | |
b655c310 NS |
4304 | static tree |
4305 | namespace_ancestor_1 (tree ns1, tree ns2) | |
4306 | { | |
4307 | tree nsr; | |
4308 | if (is_ancestor (ns1, ns2)) | |
4309 | nsr = ns1; | |
4310 | else | |
4311 | nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2); | |
4312 | return nsr; | |
4313 | } | |
d63d5d0c | 4314 | |
b655c310 | 4315 | /* Wrapper for namespace_ancestor_1. */ |
d63d5d0c | 4316 | |
b655c310 NS |
4317 | static tree |
4318 | namespace_ancestor (tree ns1, tree ns2) | |
4319 | { | |
4320 | tree nsr; | |
4321 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
4322 | nsr = namespace_ancestor_1 (ns1, ns2); | |
4323 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
4324 | return nsr; | |
d63d5d0c ILT |
4325 | } |
4326 | ||
b655c310 | 4327 | /* Process a namespace-alias declaration. */ |
501c95ff NF |
4328 | |
4329 | void | |
b655c310 | 4330 | do_namespace_alias (tree alias, tree name_space) |
501c95ff | 4331 | { |
b655c310 NS |
4332 | if (name_space == error_mark_node) |
4333 | return; | |
501c95ff | 4334 | |
b655c310 | 4335 | gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); |
501c95ff | 4336 | |
b655c310 | 4337 | name_space = ORIGINAL_NAMESPACE (name_space); |
501c95ff | 4338 | |
b655c310 NS |
4339 | /* Build the alias. */ |
4340 | alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); | |
4341 | DECL_NAMESPACE_ALIAS (alias) = name_space; | |
4342 | DECL_EXTERNAL (alias) = 1; | |
4343 | DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); | |
4344 | pushdecl (alias); | |
501c95ff | 4345 | |
b655c310 NS |
4346 | /* Emit debug info for namespace alias. */ |
4347 | if (!building_stmt_list_p ()) | |
4348 | (*debug_hooks->early_global_decl) (alias); | |
4349 | } | |
501c95ff | 4350 | |
b655c310 NS |
4351 | /* Like pushdecl, only it places X in the current namespace, |
4352 | if appropriate. */ | |
501c95ff | 4353 | |
b655c310 NS |
4354 | tree |
4355 | pushdecl_namespace_level (tree x, bool is_friend) | |
4356 | { | |
4357 | cp_binding_level *b = current_binding_level; | |
4358 | tree t; | |
501c95ff | 4359 | |
b655c310 | 4360 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
d16d5eac NS |
4361 | t = pushdecl_with_scope_1 |
4362 | (x, NAMESPACE_LEVEL (current_namespace), is_friend); | |
501c95ff | 4363 | |
b655c310 NS |
4364 | /* Now, the type_shadowed stack may screw us. Munge it so it does |
4365 | what we want. */ | |
4366 | if (TREE_CODE (t) == TYPE_DECL) | |
52ed68f7 | 4367 | { |
b655c310 NS |
4368 | tree name = DECL_NAME (t); |
4369 | tree newval; | |
4370 | tree *ptr = (tree *)0; | |
4371 | for (; !global_scope_p (b); b = b->level_chain) | |
52ed68f7 | 4372 | { |
b655c310 NS |
4373 | tree shadowed = b->type_shadowed; |
4374 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4375 | if (TREE_PURPOSE (shadowed) == name) | |
4376 | { | |
4377 | ptr = &TREE_VALUE (shadowed); | |
4378 | /* Can't break out of the loop here because sometimes | |
4379 | a binding level will have duplicate bindings for | |
4380 | PT names. It's gross, but I haven't time to fix it. */ | |
4381 | } | |
4382 | } | |
4383 | newval = TREE_TYPE (t); | |
4384 | if (ptr == (tree *)0) | |
4385 | { | |
4386 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4387 | up here if this is changed to an assertion. --KR */ | |
4388 | SET_IDENTIFIER_TYPE_VALUE (name, t); | |
4389 | } | |
4390 | else | |
4391 | { | |
4392 | *ptr = newval; | |
52ed68f7 | 4393 | } |
52ed68f7 | 4394 | } |
b655c310 NS |
4395 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
4396 | return t; | |
501c95ff NF |
4397 | } |
4398 | ||
b655c310 NS |
4399 | /* Insert USED into the using list of USER. Set INDIRECT_flag if this |
4400 | directive is not directly from the source. Also find the common | |
4401 | ancestor and let our users know about the new namespace */ | |
5ca28c1d | 4402 | |
b655c310 NS |
4403 | static void |
4404 | add_using_namespace_1 (tree user, tree used, bool indirect) | |
5ca28c1d | 4405 | { |
b655c310 NS |
4406 | tree t; |
4407 | /* Using oneself is a no-op. */ | |
4408 | if (user == used) | |
4409 | return; | |
4410 | gcc_assert (TREE_CODE (user) == NAMESPACE_DECL); | |
4411 | gcc_assert (TREE_CODE (used) == NAMESPACE_DECL); | |
4412 | /* Check if we already have this. */ | |
4413 | t = purpose_member (used, DECL_NAMESPACE_USING (user)); | |
4414 | if (t != NULL_TREE) | |
5ca28c1d | 4415 | { |
b655c310 NS |
4416 | if (!indirect) |
4417 | /* Promote to direct usage. */ | |
4418 | TREE_INDIRECT_USING (t) = 0; | |
4419 | return; | |
5ca28c1d | 4420 | } |
b655c310 NS |
4421 | |
4422 | /* Add used to the user's using list. */ | |
4423 | DECL_NAMESPACE_USING (user) | |
4424 | = tree_cons (used, namespace_ancestor (user, used), | |
4425 | DECL_NAMESPACE_USING (user)); | |
4426 | ||
4427 | TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; | |
4428 | ||
4429 | /* Add user to the used's users list. */ | |
4430 | DECL_NAMESPACE_USERS (used) | |
4431 | = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); | |
4432 | ||
4433 | /* Recursively add all namespaces used. */ | |
4434 | for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) | |
4435 | /* indirect usage */ | |
4436 | add_using_namespace_1 (user, TREE_PURPOSE (t), 1); | |
4437 | ||
4438 | /* Tell everyone using us about the new used namespaces. */ | |
4439 | for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) | |
4440 | add_using_namespace_1 (TREE_PURPOSE (t), used, 1); | |
5ca28c1d DM |
4441 | } |
4442 | ||
b655c310 | 4443 | /* Wrapper for add_using_namespace_1. */ |
5ca28c1d DM |
4444 | |
4445 | static void | |
b655c310 | 4446 | add_using_namespace (tree user, tree used, bool indirect) |
5ca28c1d | 4447 | { |
b655c310 NS |
4448 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
4449 | add_using_namespace_1 (user, used, indirect); | |
4450 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
5ca28c1d DM |
4451 | } |
4452 | ||
b655c310 | 4453 | /* Process a using-declaration not appearing in class or local scope. */ |
ebed7175 | 4454 | |
b655c310 NS |
4455 | void |
4456 | do_toplevel_using_decl (tree decl, tree scope, tree name) | |
ebed7175 | 4457 | { |
b655c310 NS |
4458 | tree oldval, oldtype, newval, newtype; |
4459 | tree orig_decl = decl; | |
4460 | cxx_binding *binding; | |
fcb2cdfc | 4461 | |
b655c310 NS |
4462 | decl = validate_nonmember_using_decl (decl, scope, name); |
4463 | if (decl == NULL_TREE) | |
4464 | return; | |
ebed7175 | 4465 | |
b655c310 | 4466 | binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name); |
ebed7175 | 4467 | |
b655c310 NS |
4468 | oldval = binding->value; |
4469 | oldtype = binding->type; | |
ebed7175 | 4470 | |
b655c310 NS |
4471 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); |
4472 | ||
4473 | /* Emit debug info. */ | |
4474 | if (!processing_template_decl) | |
4475 | cp_emit_debug_info_for_using (orig_decl, current_namespace); | |
4476 | ||
4477 | /* Copy declarations found. */ | |
4478 | if (newval) | |
4479 | binding->value = newval; | |
4480 | if (newtype) | |
4481 | binding->type = newtype; | |
ebed7175 DM |
4482 | } |
4483 | ||
b655c310 | 4484 | /* Process a using-directive. */ |
00e8de68 | 4485 | |
b655c310 NS |
4486 | void |
4487 | do_using_directive (tree name_space) | |
00e8de68 | 4488 | { |
b655c310 | 4489 | tree context = NULL_TREE; |
00e8de68 | 4490 | |
b655c310 NS |
4491 | if (name_space == error_mark_node) |
4492 | return; | |
00e8de68 | 4493 | |
b655c310 | 4494 | gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); |
00e8de68 | 4495 | |
b655c310 NS |
4496 | if (building_stmt_list_p ()) |
4497 | add_stmt (build_stmt (input_location, USING_STMT, name_space)); | |
4498 | name_space = ORIGINAL_NAMESPACE (name_space); | |
00e8de68 | 4499 | |
b655c310 NS |
4500 | if (!toplevel_bindings_p ()) |
4501 | { | |
4502 | push_using_directive (name_space); | |
4503 | } | |
4504 | else | |
4505 | { | |
4506 | /* direct usage */ | |
4507 | add_using_namespace (current_namespace, name_space, 0); | |
4508 | if (current_namespace != global_namespace) | |
4509 | context = current_namespace; | |
00e8de68 | 4510 | |
b655c310 NS |
4511 | /* Emit debugging info. */ |
4512 | if (!processing_template_decl) | |
4513 | (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE, | |
4514 | context, false); | |
00e8de68 | 4515 | } |
575bfb00 LC |
4516 | } |
4517 | ||
b655c310 NS |
4518 | /* Deal with a using-directive seen by the parser. Currently we only |
4519 | handle attributes here, since they cannot appear inside a template. */ | |
575bfb00 | 4520 | |
b655c310 NS |
4521 | void |
4522 | parse_using_directive (tree name_space, tree attribs) | |
575bfb00 | 4523 | { |
b655c310 | 4524 | do_using_directive (name_space); |
00e8de68 | 4525 | |
b655c310 NS |
4526 | if (attribs == error_mark_node) |
4527 | return; | |
00e8de68 | 4528 | |
b655c310 | 4529 | for (tree a = attribs; a; a = TREE_CHAIN (a)) |
00e8de68 | 4530 | { |
b655c310 NS |
4531 | tree name = get_attribute_name (a); |
4532 | if (is_attribute_p ("strong", name)) | |
4533 | { | |
4534 | warning (OPT_Wdeprecated, "strong using is deprecated; use inline " | |
4535 | "namespaces instead"); | |
4536 | if (!toplevel_bindings_p ()) | |
4537 | error ("strong using only meaningful at namespace scope"); | |
4538 | else if (name_space != error_mark_node) | |
4539 | { | |
4540 | if (!is_ancestor (current_namespace, name_space)) | |
4541 | error ("current namespace %qD does not enclose strongly used namespace %qD", | |
4542 | current_namespace, name_space); | |
4543 | DECL_NAMESPACE_ASSOCIATIONS (name_space) | |
4544 | = tree_cons (current_namespace, 0, | |
4545 | DECL_NAMESPACE_ASSOCIATIONS (name_space)); | |
4546 | } | |
4547 | } | |
4548 | else | |
4549 | warning (OPT_Wattributes, "%qD attribute directive ignored", name); | |
00e8de68 | 4550 | } |
00e8de68 GDR |
4551 | } |
4552 | ||
b655c310 NS |
4553 | /* Like pushdecl, only it places X in the global scope if appropriate. |
4554 | Calls cp_finish_decl to register the variable, initializing it with | |
4555 | *INIT, if INIT is non-NULL. */ | |
5a167978 | 4556 | |
b655c310 NS |
4557 | static tree |
4558 | pushdecl_top_level_1 (tree x, tree *init, bool is_friend) | |
5a167978 | 4559 | { |
575bfb00 | 4560 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
b655c310 NS |
4561 | push_to_top_level (); |
4562 | x = pushdecl_namespace_level (x, is_friend); | |
4563 | if (init) | |
4564 | cp_finish_decl (x, *init, false, NULL_TREE, 0); | |
4565 | pop_from_top_level (); | |
575bfb00 | 4566 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
b655c310 NS |
4567 | return x; |
4568 | } | |
4569 | ||
4570 | /* Like pushdecl, only it places X in the global scope if appropriate. */ | |
4571 | ||
4572 | tree | |
4573 | pushdecl_top_level (tree x) | |
4574 | { | |
4575 | return pushdecl_top_level_1 (x, NULL, false); | |
5a167978 GDR |
4576 | } |
4577 | ||
b655c310 | 4578 | /* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */ |
160594b0 | 4579 | |
b655c310 NS |
4580 | tree |
4581 | pushdecl_top_level_maybe_friend (tree x, bool is_friend) | |
160594b0 | 4582 | { |
b655c310 | 4583 | return pushdecl_top_level_1 (x, NULL, is_friend); |
160594b0 JM |
4584 | } |
4585 | ||
b655c310 NS |
4586 | /* Like pushdecl, only it places X in the global scope if |
4587 | appropriate. Calls cp_finish_decl to register the variable, | |
4588 | initializing it with INIT. */ | |
5a167978 | 4589 | |
b655c310 | 4590 | tree |
056a17ee | 4591 | pushdecl_top_level_with_init (tree x, tree init) |
5a167978 | 4592 | { |
b655c310 NS |
4593 | return pushdecl_top_level_1 (x, &init, false); |
4594 | } | |
160594b0 | 4595 | |
b655c310 NS |
4596 | /* Combines two sets of overloaded functions into an OVERLOAD chain, removing |
4597 | duplicates. The first list becomes the tail of the result. | |
8db29d88 | 4598 | |
b655c310 NS |
4599 | The algorithm is O(n^2). We could get this down to O(n log n) by |
4600 | doing a sort on the addresses of the functions, if that becomes | |
4601 | necessary. */ | |
160594b0 | 4602 | |
b655c310 NS |
4603 | static tree |
4604 | merge_functions (tree s1, tree s2) | |
4605 | { | |
4606 | for (; s2; s2 = OVL_NEXT (s2)) | |
5a167978 | 4607 | { |
b655c310 NS |
4608 | tree fn2 = OVL_CURRENT (s2); |
4609 | tree fns1; | |
160594b0 | 4610 | |
b655c310 | 4611 | for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) |
dc55c941 | 4612 | { |
b655c310 | 4613 | tree fn1 = OVL_CURRENT (fns1); |
160594b0 | 4614 | |
b655c310 NS |
4615 | /* If the function from S2 is already in S1, there is no |
4616 | need to add it again. For `extern "C"' functions, we | |
4617 | might have two FUNCTION_DECLs for the same function, in | |
4618 | different namespaces, but let's leave them in case | |
4619 | they have different default arguments. */ | |
4620 | if (fn1 == fn2) | |
4621 | break; | |
dc55c941 | 4622 | } |
160594b0 | 4623 | |
b655c310 NS |
4624 | /* If we exhausted all of the functions in S1, FN2 is new. */ |
4625 | if (!fns1) | |
4626 | s1 = build_overload (fn2, s1); | |
9771b263 | 4627 | } |
b655c310 | 4628 | return s1; |
5a167978 GDR |
4629 | } |
4630 | ||
b655c310 | 4631 | /* Returns TRUE iff OLD and NEW are the same entity. |
28ca05f0 | 4632 | |
b655c310 NS |
4633 | 3 [basic]/3: An entity is a value, object, reference, function, |
4634 | enumerator, type, class member, template, template specialization, | |
4635 | namespace, parameter pack, or this. | |
52ed68f7 | 4636 | |
b655c310 NS |
4637 | 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name |
4638 | in two different namespaces, and the declarations do not declare the | |
4639 | same entity and do not declare functions, the use of the name is | |
4640 | ill-formed. */ | |
52ed68f7 | 4641 | |
b655c310 NS |
4642 | static bool |
4643 | same_entity_p (tree one, tree two) | |
4644 | { | |
4645 | if (one == two) | |
4646 | return true; | |
4647 | if (!one || !two) | |
4648 | return false; | |
4649 | if (TREE_CODE (one) == TYPE_DECL | |
4650 | && TREE_CODE (two) == TYPE_DECL | |
4651 | && same_type_p (TREE_TYPE (one), TREE_TYPE (two))) | |
4652 | return true; | |
4653 | return false; | |
52ed68f7 DM |
4654 | } |
4655 | ||
b655c310 NS |
4656 | /* This should return an error not all definitions define functions. |
4657 | It is not an error if we find two functions with exactly the | |
4658 | same signature, only if these are selected in overload resolution. | |
4659 | old is the current set of bindings, new_binding the freshly-found binding. | |
4660 | XXX Do we want to give *all* candidates in case of ambiguity? | |
4661 | XXX In what way should I treat extern declarations? | |
4662 | XXX I don't want to repeat the entire duplicate_decls here */ | |
52ed68f7 | 4663 | |
b655c310 NS |
4664 | static void |
4665 | ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags) | |
52ed68f7 | 4666 | { |
b655c310 NS |
4667 | tree val, type; |
4668 | gcc_assert (old != NULL); | |
52ed68f7 | 4669 | |
b655c310 NS |
4670 | /* Copy the type. */ |
4671 | type = new_binding->type; | |
4672 | if (LOOKUP_NAMESPACES_ONLY (flags) | |
4673 | || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN))) | |
4674 | type = NULL_TREE; | |
52ed68f7 | 4675 | |
b655c310 NS |
4676 | /* Copy the value. */ |
4677 | val = new_binding->value; | |
4678 | if (val) | |
4679 | { | |
4680 | if (!(flags & LOOKUP_HIDDEN)) | |
4681 | val = remove_hidden_names (val); | |
4682 | if (val) | |
4683 | switch (TREE_CODE (val)) | |
4684 | { | |
4685 | case TEMPLATE_DECL: | |
4686 | /* If we expect types or namespaces, and not templates, | |
4687 | or this is not a template class. */ | |
4688 | if ((LOOKUP_QUALIFIERS_ONLY (flags) | |
4689 | && !DECL_TYPE_TEMPLATE_P (val))) | |
4690 | val = NULL_TREE; | |
4691 | break; | |
4692 | case TYPE_DECL: | |
4693 | if (LOOKUP_NAMESPACES_ONLY (flags) | |
4694 | || (type && (flags & LOOKUP_PREFER_TYPES))) | |
4695 | val = NULL_TREE; | |
4696 | break; | |
4697 | case NAMESPACE_DECL: | |
4698 | if (LOOKUP_TYPES_ONLY (flags)) | |
4699 | val = NULL_TREE; | |
4700 | break; | |
4701 | case FUNCTION_DECL: | |
4702 | /* Ignore built-in functions that are still anticipated. */ | |
4703 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
4704 | val = NULL_TREE; | |
4705 | break; | |
4706 | default: | |
4707 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
4708 | val = NULL_TREE; | |
4709 | } | |
4710 | } | |
52ed68f7 | 4711 | |
b655c310 NS |
4712 | /* If val is hidden, shift down any class or enumeration name. */ |
4713 | if (!val) | |
52ed68f7 | 4714 | { |
b655c310 NS |
4715 | val = type; |
4716 | type = NULL_TREE; | |
4717 | } | |
8bf3cdff | 4718 | |
b655c310 NS |
4719 | if (!old->value) |
4720 | old->value = val; | |
4721 | else if (val && !same_entity_p (val, old->value)) | |
4722 | { | |
4723 | if (is_overloaded_fn (old->value) && is_overloaded_fn (val)) | |
4724 | old->value = merge_functions (old->value, val); | |
4725 | else | |
4726 | { | |
4727 | old->value = tree_cons (NULL_TREE, old->value, | |
4728 | build_tree_list (NULL_TREE, val)); | |
4729 | TREE_TYPE (old->value) = error_mark_node; | |
4730 | } | |
4731 | } | |
8bf3cdff | 4732 | |
b655c310 NS |
4733 | if (!old->type) |
4734 | old->type = type; | |
4735 | else if (type && old->type != type) | |
4736 | { | |
4737 | old->type = tree_cons (NULL_TREE, old->type, | |
4738 | build_tree_list (NULL_TREE, type)); | |
4739 | TREE_TYPE (old->type) = error_mark_node; | |
52ed68f7 | 4740 | } |
b655c310 | 4741 | } |
52ed68f7 | 4742 | |
b655c310 NS |
4743 | /* Return the declarations that are members of the namespace NS. */ |
4744 | ||
4745 | tree | |
4746 | cp_namespace_decls (tree ns) | |
4747 | { | |
4748 | return NAMESPACE_LEVEL (ns)->names; | |
52ed68f7 DM |
4749 | } |
4750 | ||
b655c310 | 4751 | /* Combine prefer_type and namespaces_only into flags. */ |
9ca6556e | 4752 | |
b655c310 NS |
4753 | static int |
4754 | lookup_flags (int prefer_type, int namespaces_only) | |
4755 | { | |
4756 | if (namespaces_only) | |
4757 | return LOOKUP_PREFER_NAMESPACES; | |
4758 | if (prefer_type > 1) | |
4759 | return LOOKUP_PREFER_TYPES; | |
4760 | if (prefer_type > 0) | |
4761 | return LOOKUP_PREFER_BOTH; | |
4762 | return 0; | |
4763 | } | |
9ca6556e | 4764 | |
b655c310 NS |
4765 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to |
4766 | ignore it or not. Subroutine of lookup_name_real and | |
4767 | lookup_type_scope. */ | |
172a4594 DS |
4768 | |
4769 | static bool | |
b655c310 | 4770 | qualify_lookup (tree val, int flags) |
172a4594 | 4771 | { |
b655c310 | 4772 | if (val == NULL_TREE) |
172a4594 | 4773 | return false; |
b655c310 NS |
4774 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
4775 | return true; | |
4776 | if (flags & LOOKUP_PREFER_TYPES) | |
4777 | { | |
4778 | tree target_val = strip_using_decl (val); | |
4779 | if (TREE_CODE (target_val) == TYPE_DECL | |
4780 | || TREE_CODE (target_val) == TEMPLATE_DECL) | |
4781 | return true; | |
4782 | } | |
4783 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
74788b80 | 4784 | return false; |
b655c310 NS |
4785 | /* Look through lambda things that we shouldn't be able to see. */ |
4786 | if (is_lambda_ignored_entity (val)) | |
4787 | return false; | |
4788 | return true; | |
4789 | } | |
74788b80 | 4790 | |
b655c310 NS |
4791 | /* Given a lookup that returned VAL, decide if we want to ignore it or |
4792 | not based on DECL_ANTICIPATED. */ | |
74788b80 | 4793 | |
b655c310 NS |
4794 | bool |
4795 | hidden_name_p (tree val) | |
4796 | { | |
4797 | if (DECL_P (val) | |
4798 | && DECL_LANG_SPECIFIC (val) | |
4799 | && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val) | |
4800 | && DECL_ANTICIPATED (val)) | |
4801 | return true; | |
4802 | if (TREE_CODE (val) == OVERLOAD) | |
4803 | { | |
4804 | for (tree o = val; o; o = OVL_CHAIN (o)) | |
4805 | if (!hidden_name_p (OVL_FUNCTION (o))) | |
4806 | return false; | |
4807 | return true; | |
4808 | } | |
4809 | return false; | |
172a4594 DS |
4810 | } |
4811 | ||
b655c310 NS |
4812 | /* Remove any hidden declarations from a possibly overloaded set |
4813 | of functions. */ | |
90ea9897 | 4814 | |
b655c310 NS |
4815 | tree |
4816 | remove_hidden_names (tree fns) | |
90ea9897 | 4817 | { |
b655c310 NS |
4818 | if (!fns) |
4819 | return fns; | |
90ea9897 | 4820 | |
b655c310 NS |
4821 | if (DECL_P (fns) && hidden_name_p (fns)) |
4822 | fns = NULL_TREE; | |
4823 | else if (TREE_CODE (fns) == OVERLOAD) | |
90ea9897 | 4824 | { |
b655c310 | 4825 | tree o; |
c8094d83 | 4826 | |
b655c310 NS |
4827 | for (o = fns; o; o = OVL_NEXT (o)) |
4828 | if (hidden_name_p (OVL_CURRENT (o))) | |
4829 | break; | |
4830 | if (o) | |
4831 | { | |
4832 | tree n = NULL_TREE; | |
172a4594 | 4833 | |
b655c310 NS |
4834 | for (o = fns; o; o = OVL_NEXT (o)) |
4835 | if (!hidden_name_p (OVL_CURRENT (o))) | |
4836 | n = build_overload (OVL_CURRENT (o), n); | |
4837 | fns = n; | |
4838 | } | |
4839 | } | |
90ea9897 | 4840 | |
b655c310 | 4841 | return fns; |
90ea9897 MM |
4842 | } |
4843 | ||
b655c310 NS |
4844 | /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name |
4845 | lookup failed. Search through all available namespaces and print out | |
4846 | possible candidates. If no exact matches are found, and | |
4847 | SUGGEST_MISSPELLINGS is true, then also look for near-matches and | |
4848 | suggest the best near-match, if there is one. */ | |
90ea9897 | 4849 | |
b655c310 NS |
4850 | void |
4851 | suggest_alternatives_for (location_t location, tree name, | |
4852 | bool suggest_misspellings) | |
90ea9897 | 4853 | { |
b655c310 NS |
4854 | vec<tree> candidates = vNULL; |
4855 | vec<tree> namespaces_to_search = vNULL; | |
4856 | int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); | |
4857 | int n_searched = 0; | |
4858 | tree t; | |
4859 | unsigned ix; | |
4860 | ||
4861 | namespaces_to_search.safe_push (global_namespace); | |
4862 | ||
4863 | while (!namespaces_to_search.is_empty () | |
4864 | && n_searched < max_to_search) | |
4865 | { | |
4866 | tree scope = namespaces_to_search.pop (); | |
4867 | struct scope_binding binding = EMPTY_SCOPE_BINDING; | |
4868 | cp_binding_level *level = NAMESPACE_LEVEL (scope); | |
90ea9897 | 4869 | |
b655c310 NS |
4870 | /* Look in this namespace. */ |
4871 | qualified_lookup_using_namespace (name, scope, &binding, 0); | |
00e8de68 | 4872 | |
b655c310 | 4873 | n_searched++; |
00e8de68 | 4874 | |
b655c310 NS |
4875 | if (binding.value) |
4876 | candidates.safe_push (binding.value); | |
00e8de68 | 4877 | |
b655c310 NS |
4878 | /* Add child namespaces. */ |
4879 | for (t = level->namespaces; t; t = DECL_CHAIN (t)) | |
4880 | namespaces_to_search.safe_push (t); | |
4881 | } | |
00e8de68 | 4882 | |
b655c310 NS |
4883 | /* If we stopped before we could examine all namespaces, inform the |
4884 | user. Do this even if we don't have any candidates, since there | |
4885 | might be more candidates further down that we weren't able to | |
4886 | find. */ | |
4887 | if (n_searched >= max_to_search | |
4888 | && !namespaces_to_search.is_empty ()) | |
4889 | inform (location, | |
4890 | "maximum limit of %d namespaces searched for %qE", | |
4891 | max_to_search, name); | |
8db29d88 | 4892 | |
b655c310 | 4893 | namespaces_to_search.release (); |
00e8de68 | 4894 | |
b655c310 NS |
4895 | /* Nothing useful to report for NAME. Report on likely misspellings, |
4896 | or do nothing. */ | |
4897 | if (candidates.is_empty ()) | |
4898 | { | |
4899 | if (suggest_misspellings) | |
00e8de68 | 4900 | { |
b655c310 NS |
4901 | const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME); |
4902 | if (fuzzy_name) | |
4903 | { | |
4904 | gcc_rich_location richloc (location); | |
4905 | richloc.add_fixit_replace (fuzzy_name); | |
4906 | inform_at_rich_loc (&richloc, "suggested alternative: %qs", | |
4907 | fuzzy_name); | |
4908 | } | |
4909 | } | |
4910 | return; | |
4911 | } | |
c8094d83 | 4912 | |
b655c310 NS |
4913 | inform_n (location, candidates.length (), |
4914 | "suggested alternative:", | |
4915 | "suggested alternatives:"); | |
c8094d83 | 4916 | |
b655c310 NS |
4917 | FOR_EACH_VEC_ELT (candidates, ix, t) |
4918 | inform (location_of (t), " %qE", t); | |
00e8de68 | 4919 | |
b655c310 NS |
4920 | candidates.release (); |
4921 | } | |
00e8de68 | 4922 | |
b655c310 NS |
4923 | /* Subroutine of maybe_suggest_missing_header for handling unrecognized names |
4924 | for some of the most common names within "std::". | |
4925 | Given non-NULL NAME, a name for lookup within "std::", return the header | |
4926 | name defining it within the C++ Standard Library (without '<' and '>'), | |
4927 | or NULL. */ | |
00e8de68 | 4928 | |
b655c310 NS |
4929 | static const char * |
4930 | get_std_name_hint (const char *name) | |
4931 | { | |
4932 | struct std_name_hint | |
4933 | { | |
4934 | const char *name; | |
4935 | const char *header; | |
4936 | }; | |
4937 | static const std_name_hint hints[] = { | |
4938 | /* <array>. */ | |
4939 | {"array", "array"}, // C++11 | |
4940 | /* <deque>. */ | |
4941 | {"deque", "deque"}, | |
4942 | /* <forward_list>. */ | |
4943 | {"forward_list", "forward_list"}, // C++11 | |
4944 | /* <fstream>. */ | |
4945 | {"basic_filebuf", "fstream"}, | |
4946 | {"basic_ifstream", "fstream"}, | |
4947 | {"basic_ofstream", "fstream"}, | |
4948 | {"basic_fstream", "fstream"}, | |
4949 | /* <iostream>. */ | |
4950 | {"cin", "iostream"}, | |
4951 | {"cout", "iostream"}, | |
4952 | {"cerr", "iostream"}, | |
4953 | {"clog", "iostream"}, | |
4954 | {"wcin", "iostream"}, | |
4955 | {"wcout", "iostream"}, | |
4956 | {"wclog", "iostream"}, | |
4957 | /* <list>. */ | |
4958 | {"list", "list"}, | |
4959 | /* <map>. */ | |
4960 | {"map", "map"}, | |
4961 | {"multimap", "map"}, | |
4962 | /* <queue>. */ | |
4963 | {"queue", "queue"}, | |
4964 | {"priority_queue", "queue"}, | |
4965 | /* <ostream>. */ | |
4966 | {"ostream", "ostream"}, | |
4967 | {"wostream", "ostream"}, | |
4968 | {"ends", "ostream"}, | |
4969 | {"flush", "ostream"}, | |
4970 | {"endl", "ostream"}, | |
4971 | /* <set>. */ | |
4972 | {"set", "set"}, | |
4973 | {"multiset", "set"}, | |
4974 | /* <sstream>. */ | |
4975 | {"basic_stringbuf", "sstream"}, | |
4976 | {"basic_istringstream", "sstream"}, | |
4977 | {"basic_ostringstream", "sstream"}, | |
4978 | {"basic_stringstream", "sstream"}, | |
4979 | /* <stack>. */ | |
4980 | {"stack", "stack"}, | |
4981 | /* <string>. */ | |
4982 | {"string", "string"}, | |
4983 | {"wstring", "string"}, | |
4984 | {"u16string", "string"}, | |
4985 | {"u32string", "string"}, | |
4986 | /* <unordered_map>. */ | |
4987 | {"unordered_map", "unordered_map"}, // C++11 | |
4988 | {"unordered_multimap", "unordered_map"}, // C++11 | |
4989 | /* <unordered_set>. */ | |
4990 | {"unordered_set", "unordered_set"}, // C++11 | |
4991 | {"unordered_multiset", "unordered_set"}, // C++11 | |
4992 | /* <vector>. */ | |
4993 | {"vector", "vector"}, | |
4994 | }; | |
4995 | const size_t num_hints = sizeof (hints) / sizeof (hints[0]); | |
4996 | for (size_t i = 0; i < num_hints; i++) | |
4997 | { | |
4998 | if (0 == strcmp (name, hints[i].name)) | |
4999 | return hints[i].header; | |
5000 | } | |
5001 | return NULL; | |
5002 | } | |
00e8de68 | 5003 | |
b655c310 NS |
5004 | /* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no |
5005 | suggestions to offer. | |
5006 | If SCOPE is the "std" namespace, then suggest pertinent header | |
5007 | files for NAME. */ | |
00e8de68 | 5008 | |
b655c310 NS |
5009 | static void |
5010 | maybe_suggest_missing_header (location_t location, tree name, tree scope) | |
5011 | { | |
5012 | if (scope == NULL_TREE) | |
5013 | return; | |
5014 | if (TREE_CODE (scope) != NAMESPACE_DECL) | |
5015 | return; | |
5016 | /* We only offer suggestions for the "std" namespace. */ | |
5017 | if (scope != std_node) | |
5018 | return; | |
5019 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); | |
c8094d83 | 5020 | |
b655c310 NS |
5021 | const char *name_str = IDENTIFIER_POINTER (name); |
5022 | const char *header_hint = get_std_name_hint (name_str); | |
5023 | if (header_hint) | |
5024 | inform (location, | |
5025 | "%<std::%s%> is defined in header %<<%s>%>;" | |
5026 | " did you forget to %<#include <%s>%>?", | |
5027 | name_str, header_hint, header_hint); | |
5028 | } | |
c8094d83 | 5029 | |
b655c310 NS |
5030 | /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name |
5031 | lookup failed within the explicitly provided SCOPE. Suggest the | |
5032 | the best meaningful candidates (if any) as a fix-it hint. | |
5033 | Return true iff a suggestion was provided. */ | |
c8094d83 | 5034 | |
b655c310 NS |
5035 | bool |
5036 | suggest_alternative_in_explicit_scope (location_t location, tree name, | |
5037 | tree scope) | |
5038 | { | |
5039 | /* Resolve any namespace aliases. */ | |
5040 | scope = ORIGINAL_NAMESPACE (scope); | |
b9f673eb | 5041 | |
b655c310 | 5042 | cp_binding_level *level = NAMESPACE_LEVEL (scope); |
d4d8c232 | 5043 | |
b655c310 NS |
5044 | best_match <tree, const char *> bm (name); |
5045 | consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); | |
d4d8c232 | 5046 | |
b655c310 NS |
5047 | /* See if we have a good suggesion for the user. */ |
5048 | const char *fuzzy_name = bm.get_best_meaningful_candidate (); | |
5049 | if (fuzzy_name) | |
5050 | { | |
5051 | gcc_rich_location richloc (location); | |
5052 | richloc.add_fixit_replace (fuzzy_name); | |
5053 | inform_at_rich_loc (&richloc, "suggested alternative: %qs", | |
5054 | fuzzy_name); | |
5055 | return true; | |
5056 | } | |
5057 | else | |
5058 | maybe_suggest_missing_header (location, name, scope); | |
d4d8c232 | 5059 | |
b655c310 NS |
5060 | return false; |
5061 | } | |
d4d8c232 | 5062 | |
b655c310 NS |
5063 | /* Unscoped lookup of a global: iterate over current namespaces, |
5064 | considering using-directives. */ | |
d4d8c232 | 5065 | |
b655c310 NS |
5066 | static tree |
5067 | unqualified_namespace_lookup_1 (tree name, int flags) | |
5068 | { | |
5069 | tree initial = current_decl_namespace (); | |
5070 | tree scope = initial; | |
5071 | tree siter; | |
5072 | cp_binding_level *level; | |
5073 | tree val = NULL_TREE; | |
b9f673eb | 5074 | |
b655c310 NS |
5075 | for (; !val; scope = CP_DECL_CONTEXT (scope)) |
5076 | { | |
5077 | struct scope_binding binding = EMPTY_SCOPE_BINDING; | |
5078 | cxx_binding *b = | |
5079 | cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
00e8de68 | 5080 | |
b655c310 NS |
5081 | if (b) |
5082 | ambiguous_decl (&binding, b, flags); | |
00e8de68 | 5083 | |
b655c310 NS |
5084 | /* Add all _DECLs seen through local using-directives. */ |
5085 | for (level = current_binding_level; | |
5086 | level->kind != sk_namespace; | |
5087 | level = level->level_chain) | |
5088 | if (!lookup_using_namespace (name, &binding, level->using_directives, | |
5089 | scope, flags)) | |
5090 | /* Give up because of error. */ | |
5091 | return error_mark_node; | |
89908c8f | 5092 | |
b655c310 NS |
5093 | /* Add all _DECLs seen through global using-directives. */ |
5094 | /* XXX local and global using lists should work equally. */ | |
5095 | siter = initial; | |
5096 | while (1) | |
5097 | { | |
5098 | if (!lookup_using_namespace (name, &binding, | |
5099 | DECL_NAMESPACE_USING (siter), | |
5100 | scope, flags)) | |
5101 | /* Give up because of error. */ | |
5102 | return error_mark_node; | |
5103 | if (siter == scope) break; | |
5104 | siter = CP_DECL_CONTEXT (siter); | |
5105 | } | |
00e8de68 | 5106 | |
b655c310 NS |
5107 | val = binding.value; |
5108 | if (scope == global_namespace) | |
5109 | break; | |
5110 | } | |
575bfb00 LC |
5111 | return val; |
5112 | } | |
5113 | ||
b655c310 | 5114 | /* Wrapper for unqualified_namespace_lookup_1. */ |
575bfb00 | 5115 | |
b655c310 NS |
5116 | static tree |
5117 | unqualified_namespace_lookup (tree name, int flags) | |
575bfb00 LC |
5118 | { |
5119 | tree ret; | |
5120 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
b655c310 | 5121 | ret = unqualified_namespace_lookup_1 (name, flags); |
575bfb00 LC |
5122 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
5123 | return ret; | |
00e8de68 GDR |
5124 | } |
5125 | ||
b655c310 NS |
5126 | /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL |
5127 | or a class TYPE). | |
00e8de68 | 5128 | |
b655c310 NS |
5129 | If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. |
5130 | If PREFER_TYPE is > 1, we only return TYPE_DECLs. | |
00e8de68 | 5131 | |
b655c310 NS |
5132 | Returns a DECL (or OVERLOAD, or BASELINK) representing the |
5133 | declaration found. If no suitable declaration can be found, | |
5134 | ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is | |
5135 | neither a class-type nor a namespace a diagnostic is issued. */ | |
00e8de68 | 5136 | |
98803730 | 5137 | tree |
b655c310 NS |
5138 | lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, |
5139 | bool find_hidden) | |
98803730 | 5140 | { |
b655c310 NS |
5141 | tree t = NULL_TREE; |
5142 | ||
5143 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
5144 | { | |
5145 | struct scope_binding binding = EMPTY_SCOPE_BINDING; | |
5146 | ||
5147 | int flags = lookup_flags (prefer_type, /*namespaces_only*/false); | |
5148 | if (find_hidden) | |
5149 | flags |= LOOKUP_HIDDEN; | |
5150 | if (qualified_lookup_using_namespace (name, scope, &binding, flags)) | |
5151 | t = binding.value; | |
5152 | } | |
5153 | else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) | |
5154 | t = lookup_enumerator (scope, name); | |
5155 | else if (is_class_type (scope, complain)) | |
5156 | t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); | |
5157 | ||
5158 | if (!t) | |
5159 | return error_mark_node; | |
5160 | return t; | |
98803730 MS |
5161 | } |
5162 | ||
b655c310 NS |
5163 | /* Subroutine of unqualified_namespace_lookup: |
5164 | Add the bindings of NAME in used namespaces to VAL. | |
5165 | We are currently looking for names in namespace SCOPE, so we | |
5166 | look through USINGS for using-directives of namespaces | |
5167 | which have SCOPE as a common ancestor with the current scope. | |
5168 | Returns false on errors. */ | |
29ef83de | 5169 | |
b655c310 NS |
5170 | static bool |
5171 | lookup_using_namespace (tree name, struct scope_binding *val, | |
5172 | tree usings, tree scope, int flags) | |
5173 | { | |
5174 | tree iter; | |
5175 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5176 | /* Iterate over all used namespaces in current, searching for using | |
5177 | directives of scope. */ | |
5178 | for (iter = usings; iter; iter = TREE_CHAIN (iter)) | |
5179 | if (TREE_VALUE (iter) == scope) | |
5180 | { | |
5181 | tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter)); | |
5182 | cxx_binding *val1 = | |
5183 | cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name); | |
5184 | /* Resolve ambiguities. */ | |
5185 | if (val1) | |
5186 | ambiguous_decl (val, val1, flags); | |
5187 | } | |
5188 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
5189 | return val->value != error_mark_node; | |
5190 | } | |
87c465f5 | 5191 | |
b655c310 | 5192 | /* Returns true iff VEC contains TARGET. */ |
461c6fce | 5193 | |
b655c310 NS |
5194 | static bool |
5195 | tree_vec_contains (vec<tree, va_gc> *vec, tree target) | |
461c6fce | 5196 | { |
b655c310 NS |
5197 | unsigned int i; |
5198 | tree elt; | |
5199 | FOR_EACH_VEC_SAFE_ELT (vec,i,elt) | |
5200 | if (elt == target) | |
5201 | return true; | |
5202 | return false; | |
5203 | } | |
461c6fce | 5204 | |
b655c310 NS |
5205 | /* [namespace.qual] |
5206 | Accepts the NAME to lookup and its qualifying SCOPE. | |
5207 | Returns the name/type pair found into the cxx_binding *RESULT, | |
5208 | or false on error. */ | |
461c6fce | 5209 | |
b655c310 NS |
5210 | static bool |
5211 | qualified_lookup_using_namespace (tree name, tree scope, | |
5212 | struct scope_binding *result, int flags) | |
5213 | { | |
5214 | /* Maintain a list of namespaces visited... */ | |
5215 | vec<tree, va_gc> *seen = NULL; | |
5216 | vec<tree, va_gc> *seen_inline = NULL; | |
5217 | /* ... and a list of namespace yet to see. */ | |
5218 | vec<tree, va_gc> *todo = NULL; | |
5219 | vec<tree, va_gc> *todo_maybe = NULL; | |
5220 | vec<tree, va_gc> *todo_inline = NULL; | |
5221 | tree usings; | |
5222 | timevar_start (TV_NAME_LOOKUP); | |
5223 | /* Look through namespace aliases. */ | |
5224 | scope = ORIGINAL_NAMESPACE (scope); | |
461c6fce | 5225 | |
b655c310 | 5226 | query_oracle (name); |
461c6fce | 5227 | |
b655c310 NS |
5228 | /* Algorithm: Starting with SCOPE, walk through the set of used |
5229 | namespaces. For each used namespace, look through its inline | |
5230 | namespace set for any bindings and usings. If no bindings are | |
5231 | found, add any usings seen to the set of used namespaces. */ | |
5232 | vec_safe_push (todo, scope); | |
461c6fce | 5233 | |
b655c310 | 5234 | while (todo->length ()) |
461c6fce | 5235 | { |
b655c310 NS |
5236 | bool found_here; |
5237 | scope = todo->pop (); | |
5238 | if (tree_vec_contains (seen, scope)) | |
5239 | continue; | |
5240 | vec_safe_push (seen, scope); | |
5241 | vec_safe_push (todo_inline, scope); | |
461c6fce | 5242 | |
b655c310 NS |
5243 | found_here = false; |
5244 | while (todo_inline->length ()) | |
461c6fce | 5245 | { |
b655c310 | 5246 | cxx_binding *binding; |
c8094d83 | 5247 | |
b655c310 NS |
5248 | scope = todo_inline->pop (); |
5249 | if (tree_vec_contains (seen_inline, scope)) | |
5250 | continue; | |
5251 | vec_safe_push (seen_inline, scope); | |
461c6fce | 5252 | |
b655c310 NS |
5253 | binding = |
5254 | cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
5255 | if (binding) | |
5256 | { | |
5257 | ambiguous_decl (result, binding, flags); | |
5258 | if (result->type || result->value) | |
5259 | found_here = true; | |
5260 | } | |
461c6fce | 5261 | |
b655c310 NS |
5262 | for (usings = DECL_NAMESPACE_USING (scope); usings; |
5263 | usings = TREE_CHAIN (usings)) | |
5264 | if (!TREE_INDIRECT_USING (usings)) | |
5265 | { | |
5266 | if (is_associated_namespace (scope, TREE_PURPOSE (usings))) | |
5267 | vec_safe_push (todo_inline, TREE_PURPOSE (usings)); | |
5268 | else | |
5269 | vec_safe_push (todo_maybe, TREE_PURPOSE (usings)); | |
5270 | } | |
461c6fce | 5271 | } |
575bfb00 | 5272 | |
b655c310 NS |
5273 | if (found_here) |
5274 | vec_safe_truncate (todo_maybe, 0); | |
5275 | else | |
5276 | while (vec_safe_length (todo_maybe)) | |
5277 | vec_safe_push (todo, todo_maybe->pop ()); | |
5278 | } | |
5279 | vec_free (todo); | |
5280 | vec_free (todo_maybe); | |
5281 | vec_free (todo_inline); | |
5282 | vec_free (seen); | |
5283 | vec_free (seen_inline); | |
5284 | timevar_stop (TV_NAME_LOOKUP); | |
5285 | return result->value != error_mark_node; | |
575bfb00 LC |
5286 | } |
5287 | ||
b655c310 NS |
5288 | /* Helper function for lookup_name_fuzzy. |
5289 | Traverse binding level LVL, looking for good name matches for NAME | |
5290 | (and BM). */ | |
5291 | static void | |
5292 | consider_binding_level (tree name, best_match <tree, const char *> &bm, | |
5293 | cp_binding_level *lvl, bool look_within_fields, | |
5294 | enum lookup_name_fuzzy_kind kind) | |
00e8de68 | 5295 | { |
b655c310 NS |
5296 | if (look_within_fields) |
5297 | if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) | |
5298 | { | |
5299 | tree type = lvl->this_entity; | |
5300 | bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); | |
5301 | tree best_matching_field | |
5302 | = lookup_member_fuzzy (type, name, want_type_p); | |
5303 | if (best_matching_field) | |
5304 | bm.consider (IDENTIFIER_POINTER (best_matching_field)); | |
5305 | } | |
00e8de68 | 5306 | |
b655c310 | 5307 | for (tree t = lvl->names; t; t = TREE_CHAIN (t)) |
00e8de68 | 5308 | { |
b655c310 | 5309 | tree d = t; |
00e8de68 | 5310 | |
b655c310 NS |
5311 | /* OVERLOADs or decls from using declaration are wrapped into |
5312 | TREE_LIST. */ | |
5313 | if (TREE_CODE (d) == TREE_LIST) | |
00e8de68 | 5314 | { |
b655c310 NS |
5315 | d = TREE_VALUE (d); |
5316 | d = OVL_CURRENT (d); | |
00e8de68 | 5317 | } |
00e8de68 | 5318 | |
b655c310 NS |
5319 | /* Don't use bindings from implicitly declared functions, |
5320 | as they were likely misspellings themselves. */ | |
5321 | if (TREE_TYPE (d) == error_mark_node) | |
5322 | continue; | |
00e8de68 | 5323 | |
b655c310 NS |
5324 | /* Skip anticipated decls of builtin functions. */ |
5325 | if (TREE_CODE (d) == FUNCTION_DECL | |
5326 | && DECL_BUILT_IN (d) | |
5327 | && DECL_ANTICIPATED (d)) | |
5328 | continue; | |
575bfb00 | 5329 | |
b655c310 NS |
5330 | if (tree name = DECL_NAME (d)) |
5331 | /* Ignore internal names with spaces in them. */ | |
5332 | if (!strchr (IDENTIFIER_POINTER (name), ' ')) | |
5333 | bm.consider (IDENTIFIER_POINTER (name)); | |
5334 | } | |
575bfb00 LC |
5335 | } |
5336 | ||
b655c310 NS |
5337 | /* Search for near-matches for NAME within the current bindings, and within |
5338 | macro names, returning the best match as a const char *, or NULL if | |
5339 | no reasonable match is found. */ | |
575bfb00 | 5340 | |
b655c310 NS |
5341 | const char * |
5342 | lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) | |
e8f43da6 | 5343 | { |
b655c310 | 5344 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
e8f43da6 | 5345 | |
b655c310 | 5346 | best_match <tree, const char *> bm (name); |
e8f43da6 | 5347 | |
b655c310 NS |
5348 | cp_binding_level *lvl; |
5349 | for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) | |
5350 | consider_binding_level (name, bm, lvl, true, kind); | |
e8f43da6 | 5351 | |
b655c310 NS |
5352 | for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) |
5353 | consider_binding_level (name, bm, lvl, false, kind); | |
e8f43da6 | 5354 | |
b655c310 NS |
5355 | /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" |
5356 | as: | |
5357 | x = SOME_OTHER_MACRO (y); | |
5358 | then "SOME_OTHER_MACRO" will survive to the frontend and show up | |
5359 | as a misspelled identifier. | |
e8f43da6 | 5360 | |
b655c310 NS |
5361 | Use the best distance so far so that a candidate is only set if |
5362 | a macro is better than anything so far. This allows early rejection | |
5363 | (without calculating the edit distance) of macro names that must have | |
5364 | distance >= bm.get_best_distance (), and means that we only get a | |
5365 | non-NULL result for best_macro_match if it's better than any of | |
5366 | the identifiers already checked. */ | |
5367 | best_macro_match bmm (name, bm.get_best_distance (), parse_in); | |
5368 | cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); | |
5369 | /* If a macro is the closest so far to NAME, consider it. */ | |
5370 | if (best_macro) | |
5371 | bm.consider ((const char *)best_macro->ident.str); | |
00e8de68 | 5372 | |
b655c310 NS |
5373 | /* Try the "starts_decl_specifier_p" keywords to detect |
5374 | "singed" vs "signed" typos. */ | |
5375 | for (unsigned i = 0; i < num_c_common_reswords; i++) | |
5376 | { | |
5377 | const c_common_resword *resword = &c_common_reswords[i]; | |
00e8de68 | 5378 | |
b655c310 NS |
5379 | if (kind == FUZZY_LOOKUP_TYPENAME) |
5380 | if (!cp_keyword_starts_decl_specifier_p (resword->rid)) | |
5381 | continue; | |
00e8de68 | 5382 | |
b655c310 NS |
5383 | tree resword_identifier = ridpointers [resword->rid]; |
5384 | if (!resword_identifier) | |
5385 | continue; | |
5386 | gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); | |
00e8de68 | 5387 | |
b655c310 NS |
5388 | /* Only consider reserved words that survived the |
5389 | filtering in init_reswords (e.g. for -std). */ | |
5390 | if (!C_IS_RESERVED_WORD (resword_identifier)) | |
5391 | continue; | |
00e8de68 | 5392 | |
b655c310 NS |
5393 | bm.consider (IDENTIFIER_POINTER (resword_identifier)); |
5394 | } | |
5395 | ||
5396 | return bm.get_best_meaningful_candidate (); | |
5397 | } | |
5a167978 | 5398 | |
b655c310 | 5399 | /* Subroutine of outer_binding. |
5a167978 | 5400 | |
b655c310 NS |
5401 | Returns TRUE if BINDING is a binding to a template parameter of |
5402 | SCOPE. In that case SCOPE is the scope of a primary template | |
5403 | parameter -- in the sense of G++, i.e, a template that has its own | |
5404 | template header. | |
5a167978 | 5405 | |
b655c310 | 5406 | Returns FALSE otherwise. */ |
5a167978 GDR |
5407 | |
5408 | static bool | |
b655c310 NS |
5409 | binding_to_template_parms_of_scope_p (cxx_binding *binding, |
5410 | cp_binding_level *scope) | |
5a167978 | 5411 | { |
b655c310 NS |
5412 | tree binding_value, tmpl, tinfo; |
5413 | int level; | |
5a167978 | 5414 | |
b655c310 NS |
5415 | if (!binding || !scope || !scope->this_entity) |
5416 | return false; | |
5417 | ||
5418 | binding_value = binding->value ? binding->value : binding->type; | |
5419 | tinfo = get_template_info (scope->this_entity); | |
5420 | ||
5421 | /* BINDING_VALUE must be a template parm. */ | |
5422 | if (binding_value == NULL_TREE | |
5423 | || (!DECL_P (binding_value) | |
5424 | || !DECL_TEMPLATE_PARM_P (binding_value))) | |
5425 | return false; | |
5426 | ||
5427 | /* The level of BINDING_VALUE. */ | |
5428 | level = | |
5429 | template_type_parameter_p (binding_value) | |
5430 | ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX | |
5431 | (TREE_TYPE (binding_value))) | |
5432 | : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); | |
5433 | ||
5434 | /* The template of the current scope, iff said scope is a primary | |
5435 | template. */ | |
5436 | tmpl = (tinfo | |
5437 | && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) | |
5438 | ? TI_TEMPLATE (tinfo) | |
5439 | : NULL_TREE); | |
5440 | ||
5441 | /* If the level of the parm BINDING_VALUE equals the depth of TMPL, | |
5442 | then BINDING_VALUE is a parameter of TMPL. */ | |
5443 | return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
5a167978 GDR |
5444 | } |
5445 | ||
b655c310 NS |
5446 | /* Return the innermost non-namespace binding for NAME from a scope |
5447 | containing BINDING, or, if BINDING is NULL, the current scope. | |
5448 | Please note that for a given template, the template parameters are | |
5449 | considered to be in the scope containing the current scope. | |
5450 | If CLASS_P is false, then class bindings are ignored. */ | |
86098eb8 | 5451 | |
b655c310 NS |
5452 | cxx_binding * |
5453 | outer_binding (tree name, | |
5454 | cxx_binding *binding, | |
5455 | bool class_p) | |
86098eb8 | 5456 | { |
b655c310 NS |
5457 | cxx_binding *outer; |
5458 | cp_binding_level *scope; | |
5459 | cp_binding_level *outer_scope; | |
d4ccba66 | 5460 | |
b655c310 | 5461 | if (binding) |
86098eb8 | 5462 | { |
b655c310 NS |
5463 | scope = binding->scope->level_chain; |
5464 | outer = binding->previous; | |
5465 | } | |
5466 | else | |
5467 | { | |
5468 | scope = current_binding_level; | |
5469 | outer = IDENTIFIER_BINDING (name); | |
86098eb8 | 5470 | } |
b655c310 | 5471 | outer_scope = outer ? outer->scope : NULL; |
d4ccba66 | 5472 | |
b655c310 NS |
5473 | /* Because we create class bindings lazily, we might be missing a |
5474 | class binding for NAME. If there are any class binding levels | |
5475 | between the LAST_BINDING_LEVEL and the scope in which OUTER was | |
5476 | declared, we must lookup NAME in those class scopes. */ | |
5477 | if (class_p) | |
5478 | while (scope && scope != outer_scope && scope->kind != sk_namespace) | |
5479 | { | |
5480 | if (scope->kind == sk_class) | |
5481 | { | |
5482 | cxx_binding *class_binding; | |
d4ccba66 | 5483 | |
b655c310 NS |
5484 | class_binding = get_class_binding (name, scope); |
5485 | if (class_binding) | |
5486 | { | |
5487 | /* Thread this new class-scope binding onto the | |
5488 | IDENTIFIER_BINDING list so that future lookups | |
5489 | find it quickly. */ | |
5490 | class_binding->previous = outer; | |
5491 | if (binding) | |
5492 | binding->previous = class_binding; | |
5493 | else | |
5494 | IDENTIFIER_BINDING (name) = class_binding; | |
5495 | return class_binding; | |
5496 | } | |
5497 | } | |
5498 | /* If we are in a member template, the template parms of the member | |
5499 | template are considered to be inside the scope of the containing | |
5500 | class, but within G++ the class bindings are all pushed between the | |
5501 | template parms and the function body. So if the outer binding is | |
5502 | a template parm for the current scope, return it now rather than | |
5503 | look for a class binding. */ | |
5504 | if (outer_scope && outer_scope->kind == sk_template_parms | |
5505 | && binding_to_template_parms_of_scope_p (outer, scope)) | |
5506 | return outer; | |
5507 | ||
5508 | scope = scope->level_chain; | |
5509 | } | |
5510 | ||
5511 | return outer; | |
86098eb8 JM |
5512 | } |
5513 | ||
b655c310 NS |
5514 | /* Return the innermost block-scope or class-scope value binding for |
5515 | NAME, or NULL_TREE if there is no such binding. */ | |
5a167978 | 5516 | |
b655c310 NS |
5517 | tree |
5518 | innermost_non_namespace_value (tree name) | |
5a167978 | 5519 | { |
b655c310 NS |
5520 | cxx_binding *binding; |
5521 | binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); | |
5522 | return binding ? binding->value : NULL_TREE; | |
5523 | } | |
5a167978 | 5524 | |
b655c310 NS |
5525 | /* Look up NAME in the current binding level and its superiors in the |
5526 | namespace of variables, functions and typedefs. Return a ..._DECL | |
5527 | node of some kind representing its definition if there is only one | |
5528 | such declaration, or return a TREE_LIST with all the overloaded | |
5529 | definitions if there are many, or return 0 if it is undefined. | |
5530 | Hidden name, either friend declaration or built-in function, are | |
5531 | not ignored. | |
86098eb8 | 5532 | |
b655c310 NS |
5533 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
5534 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
5535 | Otherwise we prefer non-TYPE_DECLs. | |
c8094d83 | 5536 | |
b655c310 NS |
5537 | If NONCLASS is nonzero, bindings in class scopes are ignored. If |
5538 | BLOCK_P is false, bindings in block scopes are ignored. */ | |
4cfaec1c | 5539 | |
b655c310 NS |
5540 | static tree |
5541 | lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, | |
5542 | int namespaces_only, int flags) | |
5543 | { | |
5544 | cxx_binding *iter; | |
5545 | tree val = NULL_TREE; | |
5a167978 | 5546 | |
b655c310 NS |
5547 | query_oracle (name); |
5548 | ||
5549 | /* Conversion operators are handled specially because ordinary | |
5550 | unqualified name lookup will not find template conversion | |
5551 | operators. */ | |
5552 | if (IDENTIFIER_TYPENAME_P (name)) | |
d63d5d0c | 5553 | { |
b655c310 | 5554 | cp_binding_level *level; |
d63d5d0c | 5555 | |
b655c310 NS |
5556 | for (level = current_binding_level; |
5557 | level && level->kind != sk_namespace; | |
5558 | level = level->level_chain) | |
5559 | { | |
5560 | tree class_type; | |
5561 | tree operators; | |
5562 | ||
5563 | /* A conversion operator can only be declared in a class | |
5564 | scope. */ | |
5565 | if (level->kind != sk_class) | |
5566 | continue; | |
5567 | ||
5568 | /* Lookup the conversion operator in the class. */ | |
5569 | class_type = level->this_entity; | |
5570 | operators = lookup_fnfields (class_type, name, /*protect=*/0); | |
5571 | if (operators) | |
5572 | return operators; | |
5573 | } | |
5574 | ||
5575 | return NULL_TREE; | |
d63d5d0c | 5576 | } |
c8094d83 | 5577 | |
b655c310 NS |
5578 | flags |= lookup_flags (prefer_type, namespaces_only); |
5579 | ||
5580 | /* First, look in non-namespace scopes. */ | |
5581 | ||
5582 | if (current_class_type == NULL_TREE) | |
5583 | nonclass = 1; | |
5a167978 | 5584 | |
b655c310 NS |
5585 | if (block_p || !nonclass) |
5586 | for (iter = outer_binding (name, NULL, !nonclass); | |
5587 | iter; | |
5588 | iter = outer_binding (name, iter, !nonclass)) | |
5589 | { | |
5590 | tree binding; | |
5a167978 | 5591 | |
b655c310 NS |
5592 | /* Skip entities we don't want. */ |
5593 | if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) | |
5594 | continue; | |
5a167978 | 5595 | |
b655c310 NS |
5596 | /* If this is the kind of thing we're looking for, we're done. */ |
5597 | if (qualify_lookup (iter->value, flags)) | |
5598 | binding = iter->value; | |
5599 | else if ((flags & LOOKUP_PREFER_TYPES) | |
5600 | && qualify_lookup (iter->type, flags)) | |
5601 | binding = iter->type; | |
5602 | else | |
5603 | binding = NULL_TREE; | |
5a167978 | 5604 | |
b655c310 NS |
5605 | if (binding) |
5606 | { | |
5607 | if (hidden_name_p (binding)) | |
5608 | { | |
5609 | /* A non namespace-scope binding can only be hidden in the | |
5610 | presence of a local class, due to friend declarations. | |
5a167978 | 5611 | |
b655c310 | 5612 | In particular, consider: |
ba796308 | 5613 | |
b655c310 NS |
5614 | struct C; |
5615 | void f() { | |
5616 | struct A { | |
5617 | friend struct B; | |
5618 | friend struct C; | |
5619 | void g() { | |
5620 | B* b; // error: B is hidden | |
5621 | C* c; // OK, finds ::C | |
5622 | } | |
5623 | }; | |
5624 | B *b; // error: B is hidden | |
5625 | C *c; // OK, finds ::C | |
5626 | struct B {}; | |
5627 | B *bb; // OK | |
5628 | } | |
5a167978 | 5629 | |
b655c310 NS |
5630 | The standard says that "B" is a local class in "f" |
5631 | (but not nested within "A") -- but that name lookup | |
5632 | for "B" does not find this declaration until it is | |
5633 | declared directly with "f". | |
5a167978 | 5634 | |
b655c310 | 5635 | In particular: |
c8094d83 | 5636 | |
b655c310 | 5637 | [class.friend] |
5a167978 | 5638 | |
b655c310 NS |
5639 | If a friend declaration appears in a local class and |
5640 | the name specified is an unqualified name, a prior | |
5641 | declaration is looked up without considering scopes | |
5642 | that are outside the innermost enclosing non-class | |
5643 | scope. For a friend function declaration, if there is | |
5644 | no prior declaration, the program is ill-formed. For a | |
5645 | friend class declaration, if there is no prior | |
5646 | declaration, the class that is specified belongs to the | |
5647 | innermost enclosing non-class scope, but if it is | |
5648 | subsequently referenced, its name is not found by name | |
5649 | lookup until a matching declaration is provided in the | |
5650 | innermost enclosing nonclass scope. | |
ccb14335 | 5651 | |
b655c310 NS |
5652 | So just keep looking for a non-hidden binding. |
5653 | */ | |
5654 | gcc_assert (TREE_CODE (binding) == TYPE_DECL); | |
5655 | continue; | |
5656 | } | |
5657 | val = binding; | |
5658 | break; | |
5659 | } | |
5660 | } | |
2395cd2e | 5661 | |
b655c310 NS |
5662 | /* Now lookup in namespace scopes. */ |
5663 | if (!val) | |
5664 | val = unqualified_namespace_lookup (name, flags); | |
c8b2e872 | 5665 | |
b655c310 NS |
5666 | /* Anticipated built-ins and friends aren't found by normal lookup. */ |
5667 | if (val && !(flags & LOOKUP_HIDDEN)) | |
5668 | val = remove_hidden_names (val); | |
5a167978 | 5669 | |
b655c310 NS |
5670 | /* If we have a single function from a using decl, pull it out. */ |
5671 | if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) | |
5672 | val = OVL_FUNCTION (val); | |
5673 | ||
5674 | return val; | |
db10df3d JM |
5675 | } |
5676 | ||
b655c310 | 5677 | /* Wrapper for lookup_name_real_1. */ |
db10df3d | 5678 | |
b655c310 NS |
5679 | tree |
5680 | lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, | |
5681 | int namespaces_only, int flags) | |
db10df3d | 5682 | { |
b655c310 NS |
5683 | tree ret; |
5684 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5685 | ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, | |
5686 | namespaces_only, flags); | |
5687 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
5688 | return ret; | |
5689 | } | |
db10df3d | 5690 | |
b655c310 NS |
5691 | tree |
5692 | lookup_name_nonclass (tree name) | |
5693 | { | |
5694 | return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); | |
db10df3d JM |
5695 | } |
5696 | ||
b655c310 NS |
5697 | tree |
5698 | lookup_function_nonclass (tree name, vec<tree, va_gc> *args, bool block_p) | |
5699 | { | |
5700 | return | |
5701 | lookup_arg_dependent (name, | |
5702 | lookup_name_real (name, 0, 1, block_p, 0, 0), | |
5703 | args); | |
5704 | } | |
db10df3d | 5705 | |
b655c310 NS |
5706 | tree |
5707 | lookup_name (tree name) | |
5708 | { | |
5709 | return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); | |
5710 | } | |
db10df3d | 5711 | |
b655c310 NS |
5712 | tree |
5713 | lookup_name_prefer_type (tree name, int prefer_type) | |
db10df3d | 5714 | { |
b655c310 NS |
5715 | return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); |
5716 | } | |
db10df3d | 5717 | |
b655c310 NS |
5718 | /* Look up NAME for type used in elaborated name specifier in |
5719 | the scopes given by SCOPE. SCOPE can be either TS_CURRENT or | |
5720 | TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the | |
5721 | name, more scopes are checked if cleanup or template parameter | |
5722 | scope is encountered. | |
db10df3d | 5723 | |
b655c310 NS |
5724 | Unlike lookup_name_real, we make sure that NAME is actually |
5725 | declared in the desired scope, not from inheritance, nor using | |
5726 | directive. For using declaration, there is DR138 still waiting | |
5727 | to be resolved. Hidden name coming from an earlier friend | |
5728 | declaration is also returned. | |
db10df3d | 5729 | |
b655c310 NS |
5730 | A TYPE_DECL best matching the NAME is returned. Catching error |
5731 | and issuing diagnostics are caller's responsibility. */ | |
db10df3d | 5732 | |
b655c310 NS |
5733 | static tree |
5734 | lookup_type_scope_1 (tree name, tag_scope scope) | |
5735 | { | |
5736 | cxx_binding *iter = NULL; | |
5737 | tree val = NULL_TREE; | |
db10df3d | 5738 | |
b655c310 NS |
5739 | /* Look in non-namespace scope first. */ |
5740 | if (current_binding_level->kind != sk_namespace) | |
5741 | iter = outer_binding (name, NULL, /*class_p=*/ true); | |
5742 | for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) | |
5a167978 | 5743 | { |
b655c310 NS |
5744 | /* Check if this is the kind of thing we're looking for. |
5745 | If SCOPE is TS_CURRENT, also make sure it doesn't come from | |
5746 | base class. For ITER->VALUE, we can simply use | |
5747 | INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use | |
5748 | our own check. | |
5a167978 | 5749 | |
b655c310 NS |
5750 | We check ITER->TYPE before ITER->VALUE in order to handle |
5751 | typedef struct C {} C; | |
5752 | correctly. */ | |
5a167978 | 5753 | |
b655c310 NS |
5754 | if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) |
5755 | && (scope != ts_current | |
5756 | || LOCAL_BINDING_P (iter) | |
5757 | || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) | |
5758 | val = iter->type; | |
5759 | else if ((scope != ts_current | |
5760 | || !INHERITED_VALUE_BINDING_P (iter)) | |
5761 | && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) | |
5762 | val = iter->value; | |
5a167978 | 5763 | |
b655c310 NS |
5764 | if (val) |
5765 | break; | |
5766 | } | |
5a167978 | 5767 | |
b655c310 NS |
5768 | /* Look in namespace scope. */ |
5769 | if (!val) | |
5a167978 | 5770 | { |
b655c310 NS |
5771 | iter = cp_binding_level_find_binding_for_name |
5772 | (NAMESPACE_LEVEL (current_decl_namespace ()), name); | |
5773 | ||
5774 | if (iter) | |
5775 | { | |
5776 | /* If this is the kind of thing we're looking for, we're done. */ | |
5777 | if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)) | |
5778 | val = iter->type; | |
5779 | else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) | |
5780 | val = iter->value; | |
5781 | } | |
5782 | ||
5a167978 | 5783 | } |
b655c310 NS |
5784 | |
5785 | /* Type found, check if it is in the allowed scopes, ignoring cleanup | |
5786 | and template parameter scopes. */ | |
5787 | if (val) | |
5a167978 | 5788 | { |
b655c310 NS |
5789 | cp_binding_level *b = current_binding_level; |
5790 | while (b) | |
5791 | { | |
5792 | if (iter->scope == b) | |
5793 | return val; | |
5d80a306 | 5794 | |
b655c310 NS |
5795 | if (b->kind == sk_cleanup || b->kind == sk_template_parms |
5796 | || b->kind == sk_function_parms) | |
5797 | b = b->level_chain; | |
5798 | else if (b->kind == sk_class | |
5799 | && scope == ts_within_enclosing_non_class) | |
5800 | b = b->level_chain; | |
5801 | else | |
5802 | break; | |
5803 | } | |
5a167978 | 5804 | } |
5a167978 | 5805 | |
b655c310 | 5806 | return NULL_TREE; |
5a167978 | 5807 | } |
b655c310 NS |
5808 | |
5809 | /* Wrapper for lookup_type_scope_1. */ | |
5a167978 | 5810 | |
b655c310 NS |
5811 | tree |
5812 | lookup_type_scope (tree name, tag_scope scope) | |
c166b898 | 5813 | { |
b655c310 NS |
5814 | tree ret; |
5815 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5816 | ret = lookup_type_scope_1 (name, scope); | |
5817 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
5818 | return ret; | |
c166b898 ILT |
5819 | } |
5820 | ||
5a167978 | 5821 | |
b655c310 NS |
5822 | /* Similar to `lookup_name' but look only in the innermost non-class |
5823 | binding level. */ | |
5a167978 | 5824 | |
b655c310 NS |
5825 | static tree |
5826 | lookup_name_innermost_nonclass_level_1 (tree name) | |
5827 | { | |
5828 | cp_binding_level *b; | |
5829 | tree t = NULL_TREE; | |
5a167978 | 5830 | |
b655c310 | 5831 | b = innermost_nonclass_level (); |
5a167978 | 5832 | |
b655c310 | 5833 | if (b->kind == sk_namespace) |
5a167978 | 5834 | { |
06aa5490 | 5835 | t = get_namespace_binding (current_namespace, name); |
5a167978 | 5836 | |
b655c310 NS |
5837 | /* extern "C" function() */ |
5838 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
5839 | t = TREE_VALUE (t); | |
5a167978 | 5840 | } |
b655c310 NS |
5841 | else if (IDENTIFIER_BINDING (name) |
5842 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) | |
5a167978 | 5843 | { |
b655c310 NS |
5844 | cxx_binding *binding; |
5845 | binding = IDENTIFIER_BINDING (name); | |
5846 | while (1) | |
5847 | { | |
5848 | if (binding->scope == b | |
5849 | && !(VAR_P (binding->value) | |
5850 | && DECL_DEAD_FOR_LOCAL (binding->value))) | |
5851 | return binding->value; | |
5852 | ||
5853 | if (b->kind == sk_cleanup) | |
5854 | b = b->level_chain; | |
5855 | else | |
5856 | break; | |
5857 | } | |
5a167978 GDR |
5858 | } |
5859 | ||
b655c310 NS |
5860 | return t; |
5861 | } | |
5862 | ||
5863 | /* Wrapper for lookup_name_innermost_nonclass_level_1. */ | |
5864 | ||
5865 | tree | |
5866 | lookup_name_innermost_nonclass_level (tree name) | |
5867 | { | |
5868 | tree ret; | |
5869 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
5870 | ret = lookup_name_innermost_nonclass_level_1 (name); | |
5871 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
5872 | return ret; | |
5a167978 GDR |
5873 | } |
5874 | ||
5a167978 | 5875 | |
b655c310 NS |
5876 | /* Returns true iff DECL is a block-scope extern declaration of a function |
5877 | or variable. */ | |
5878 | ||
5879 | bool | |
5880 | is_local_extern (tree decl) | |
5a167978 | 5881 | { |
b655c310 | 5882 | cxx_binding *binding; |
5a167978 | 5883 | |
b655c310 NS |
5884 | /* For functions, this is easy. */ |
5885 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
5886 | return DECL_LOCAL_FUNCTION_P (decl); | |
d63d5d0c | 5887 | |
b655c310 NS |
5888 | if (!VAR_P (decl)) |
5889 | return false; | |
5890 | if (!current_function_decl) | |
5891 | return false; | |
5a167978 | 5892 | |
b655c310 NS |
5893 | /* For variables, this is not easy. We need to look at the binding stack |
5894 | for the identifier to see whether the decl we have is a local. */ | |
5895 | for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); | |
5896 | binding && binding->scope->kind != sk_namespace; | |
5897 | binding = binding->previous) | |
5898 | if (binding->value == decl) | |
5899 | return LOCAL_BINDING_P (binding); | |
5a167978 | 5900 | |
b655c310 NS |
5901 | return false; |
5902 | } | |
ca1085f0 | 5903 | |
b655c310 | 5904 | /* Like lookup_name_innermost_nonclass_level, but for types. */ |
4860b874 | 5905 | |
b655c310 NS |
5906 | static tree |
5907 | lookup_type_current_level (tree name) | |
5908 | { | |
5909 | tree t = NULL_TREE; | |
bfdb7b70 | 5910 | |
b655c310 NS |
5911 | timevar_start (TV_NAME_LOOKUP); |
5912 | gcc_assert (current_binding_level->kind != sk_namespace); | |
5a167978 | 5913 | |
b655c310 NS |
5914 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE |
5915 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
5916 | { | |
5917 | cp_binding_level *b = current_binding_level; | |
5918 | while (1) | |
5919 | { | |
5920 | if (purpose_member (name, b->type_shadowed)) | |
5921 | { | |
5922 | t = REAL_IDENTIFIER_TYPE_VALUE (name); | |
5923 | break; | |
5924 | } | |
5925 | if (b->kind == sk_cleanup) | |
5926 | b = b->level_chain; | |
5927 | else | |
5928 | break; | |
5929 | } | |
5930 | } | |
575bfb00 | 5931 | |
b655c310 NS |
5932 | timevar_stop (TV_NAME_LOOKUP); |
5933 | return t; | |
575bfb00 LC |
5934 | } |
5935 | ||
00e8de68 GDR |
5936 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
5937 | changed (i.e. there was already a directive), or the fresh | |
5938 | TREE_LIST otherwise. */ | |
5939 | ||
a5e6b29b | 5940 | static tree |
575bfb00 | 5941 | push_using_directive_1 (tree used) |
00e8de68 GDR |
5942 | { |
5943 | tree ud = current_binding_level->using_directives; | |
5944 | tree iter, ancestor; | |
5945 | ||
00e8de68 GDR |
5946 | /* Check if we already have this. */ |
5947 | if (purpose_member (used, ud) != NULL_TREE) | |
575bfb00 | 5948 | return NULL_TREE; |
00e8de68 GDR |
5949 | |
5950 | ancestor = namespace_ancestor (current_decl_namespace (), used); | |
5951 | ud = current_binding_level->using_directives; | |
5952 | ud = tree_cons (used, ancestor, ud); | |
5953 | current_binding_level->using_directives = ud; | |
5954 | ||
5955 | /* Recursively add all namespaces used. */ | |
5956 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
5957 | push_using_directive (TREE_PURPOSE (iter)); | |
5958 | ||
575bfb00 LC |
5959 | return ud; |
5960 | } | |
5961 | ||
5962 | /* Wrapper for push_using_directive_1. */ | |
5963 | ||
5964 | static tree | |
5965 | push_using_directive (tree used) | |
5966 | { | |
5967 | tree ret; | |
f9e2a506 | 5968 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
575bfb00 | 5969 | ret = push_using_directive_1 (used); |
f9e2a506 | 5970 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
575bfb00 | 5971 | return ret; |
00e8de68 GDR |
5972 | } |
5973 | ||
5974 | /* The type TYPE is being declared. If it is a class template, or a | |
5975 | specialization of a class template, do any processing required and | |
5976 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is | |
5977 | being declared a friend. B is the binding level at which this TYPE | |
5978 | should be bound. | |
5979 | ||
5980 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
5981 | processing. */ | |
5982 | ||
5983 | static tree | |
bd3d082e | 5984 | maybe_process_template_type_declaration (tree type, int is_friend, |
2c140474 | 5985 | cp_binding_level *b) |
00e8de68 GDR |
5986 | { |
5987 | tree decl = TYPE_NAME (type); | |
5988 | ||
5989 | if (processing_template_parmlist) | |
5990 | /* You can't declare a new template type in a template parameter | |
5991 | list. But, you can declare a non-template type: | |
5992 | ||
0cbd7506 | 5993 | template <class A*> struct S; |
00e8de68 GDR |
5994 | |
5995 | is a forward-declaration of `A'. */ | |
5996 | ; | |
c43e95f8 MM |
5997 | else if (b->kind == sk_namespace |
5998 | && current_binding_level->kind != sk_namespace) | |
5999 | /* If this new type is being injected into a containing scope, | |
6000 | then it's not a template type. */ | |
6001 | ; | |
00e8de68 GDR |
6002 | else |
6003 | { | |
9e1e64ec PC |
6004 | gcc_assert (MAYBE_CLASS_TYPE_P (type) |
6005 | || TREE_CODE (type) == ENUMERAL_TYPE); | |
00e8de68 GDR |
6006 | |
6007 | if (processing_template_decl) | |
6008 | { | |
6009 | /* This may change after the call to | |
6010 | push_template_decl_real, but we want the original value. */ | |
6011 | tree name = DECL_NAME (decl); | |
6012 | ||
bd3d082e | 6013 | decl = push_template_decl_real (decl, is_friend); |
79faac54 PC |
6014 | if (decl == error_mark_node) |
6015 | return error_mark_node; | |
6016 | ||
00e8de68 GDR |
6017 | /* If the current binding level is the binding level for the |
6018 | template parameters (see the comment in | |
6019 | begin_template_parm_list) and the enclosing level is a class | |
6020 | scope, and we're not looking at a friend, push the | |
6021 | declaration of the member class into the class scope. In the | |
6022 | friend case, push_template_decl will already have put the | |
6023 | friend into global scope, if appropriate. */ | |
6024 | if (TREE_CODE (type) != ENUMERAL_TYPE | |
bd3d082e | 6025 | && !is_friend && b->kind == sk_template_parms |
00e8de68 GDR |
6026 | && b->level_chain->kind == sk_class) |
6027 | { | |
6028 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); | |
e57df6fe | 6029 | |
00e8de68 GDR |
6030 | if (!COMPLETE_TYPE_P (current_class_type)) |
6031 | { | |
6032 | maybe_add_class_template_decl_list (current_class_type, | |
6033 | type, /*friend_p=*/0); | |
c72a1a86 | 6034 | /* Put this UTD in the table of UTDs for the class. */ |
e57df6fe KL |
6035 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6036 | CLASSTYPE_NESTED_UTDS (current_class_type) = | |
6037 | binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
6038 | ||
6039 | binding_table_insert | |
6040 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 GDR |
6041 | } |
6042 | } | |
6043 | } | |
6044 | } | |
6045 | ||
6046 | return decl; | |
6047 | } | |
6048 | ||
5a24482e KL |
6049 | /* Push a tag name NAME for struct/class/union/enum type TYPE. In case |
6050 | that the NAME is a class template, the tag is processed but not pushed. | |
6051 | ||
6052 | The pushed scope depend on the SCOPE parameter: | |
6053 | - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup | |
6054 | scope. | |
6055 | - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and | |
6056 | non-template-parameter scope. This case is needed for forward | |
6057 | declarations. | |
6058 | - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to | |
6059 | TS_GLOBAL case except that names within template-parameter scopes | |
6060 | are not pushed at all. | |
6061 | ||
c6f9f83b | 6062 | Returns TYPE upon success and ERROR_MARK_NODE otherwise. */ |
00e8de68 | 6063 | |
575bfb00 LC |
6064 | static tree |
6065 | pushtag_1 (tree name, tree type, tag_scope scope) | |
00e8de68 | 6066 | { |
2c140474 | 6067 | cp_binding_level *b; |
6a000704 | 6068 | tree decl; |
00e8de68 | 6069 | |
00e8de68 | 6070 | b = current_binding_level; |
7c82a41e MM |
6071 | while (/* Cleanup scopes are not scopes from the point of view of |
6072 | the language. */ | |
6073 | b->kind == sk_cleanup | |
b344d949 JM |
6074 | /* Neither are function parameter scopes. */ |
6075 | || b->kind == sk_function_parms | |
7c82a41e MM |
6076 | /* Neither are the scopes used to hold template parameters |
6077 | for an explicit specialization. For an ordinary template | |
6078 | declaration, these scopes are not scopes from the point of | |
5a24482e KL |
6079 | view of the language. */ |
6080 | || (b->kind == sk_template_parms | |
6081 | && (b->explicit_spec_p || scope == ts_global)) | |
00e8de68 | 6082 | || (b->kind == sk_class |
bd3d082e | 6083 | && (scope != ts_current |
00e8de68 GDR |
6084 | /* We may be defining a new type in the initializer |
6085 | of a static member variable. We allow this when | |
6086 | not pedantic, and it is particularly useful for | |
6087 | type punning via an anonymous union. */ | |
6088 | || COMPLETE_TYPE_P (b->this_entity)))) | |
6089 | b = b->level_chain; | |
6090 | ||
9dc6f476 | 6091 | gcc_assert (identifier_p (name)); |
3db45ab5 | 6092 | |
6a000704 | 6093 | /* Do C++ gratuitous typedefing. */ |
575bfb00 | 6094 | if (identifier_type_value_1 (name) != type) |
00e8de68 | 6095 | { |
6a000704 NS |
6096 | tree tdef; |
6097 | int in_class = 0; | |
6098 | tree context = TYPE_CONTEXT (type); | |
00e8de68 | 6099 | |
6a000704 NS |
6100 | if (! context) |
6101 | { | |
6102 | tree cs = current_scope (); | |
3db45ab5 | 6103 | |
d5f4eddd JM |
6104 | if (scope == ts_current |
6105 | || (cs && TREE_CODE (cs) == FUNCTION_DECL)) | |
6a000704 NS |
6106 | context = cs; |
6107 | else if (cs != NULL_TREE && TYPE_P (cs)) | |
6108 | /* When declaring a friend class of a local class, we want | |
6109 | to inject the newly named class into the scope | |
6110 | containing the local class, not the namespace | |
6111 | scope. */ | |
6112 | context = decl_function_context (get_type_decl (cs)); | |
6113 | } | |
6114 | if (!context) | |
6115 | context = current_namespace; | |
bd3d082e | 6116 | |
6a000704 NS |
6117 | if (b->kind == sk_class |
6118 | || (b->kind == sk_template_parms | |
6119 | && b->level_chain->kind == sk_class)) | |
6120 | in_class = 1; | |
00e8de68 | 6121 | |
6a000704 NS |
6122 | tdef = create_implicit_typedef (name, type); |
6123 | DECL_CONTEXT (tdef) = FROB_CONTEXT (context); | |
6124 | if (scope == ts_within_enclosing_non_class) | |
00e8de68 | 6125 | { |
6a000704 NS |
6126 | /* This is a friend. Make this TYPE_DECL node hidden from |
6127 | ordinary name lookup. Its corresponding TEMPLATE_DECL | |
6128 | will be marked in push_template_decl_real. */ | |
6129 | retrofit_lang_decl (tdef); | |
6130 | DECL_ANTICIPATED (tdef) = 1; | |
6131 | DECL_FRIEND_P (tdef) = 1; | |
6132 | } | |
e57df6fe | 6133 | |
6a000704 NS |
6134 | decl = maybe_process_template_type_declaration |
6135 | (type, scope == ts_within_enclosing_non_class, b); | |
6136 | if (decl == error_mark_node) | |
575bfb00 | 6137 | return decl; |
3db45ab5 | 6138 | |
6a000704 NS |
6139 | if (b->kind == sk_class) |
6140 | { | |
0efc4442 | 6141 | if (!TYPE_BEING_DEFINED (current_class_type)) |
575bfb00 | 6142 | return error_mark_node; |
0efc4442 | 6143 | |
6a000704 NS |
6144 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
6145 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the | |
6146 | class. But if it's a member template class, we want | |
6147 | the TEMPLATE_DECL, not the TYPE_DECL, so this is done | |
6148 | later. */ | |
6149 | finish_member_declaration (decl); | |
6150 | else | |
6151 | pushdecl_class_level (decl); | |
00e8de68 | 6152 | } |
6a000704 | 6153 | else if (b->kind != sk_template_parms) |
c5f8391c | 6154 | { |
575bfb00 | 6155 | decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false); |
c5f8391c | 6156 | if (decl == error_mark_node) |
575bfb00 | 6157 | return decl; |
d3e19c2c PC |
6158 | |
6159 | if (DECL_CONTEXT (decl) == std_node | |
ad9870f2 | 6160 | && init_list_identifier == DECL_NAME (TYPE_NAME (type)) |
d3e19c2c PC |
6161 | && !CLASSTYPE_TEMPLATE_INFO (type)) |
6162 | { | |
6163 | error ("declaration of std::initializer_list does not match " | |
6164 | "#include <initializer_list>, isn't a template"); | |
6165 | return error_mark_node; | |
6166 | } | |
c5f8391c | 6167 | } |
6a000704 | 6168 | |
dc3ca06f SM |
6169 | if (! in_class) |
6170 | set_identifier_type_value_with_scope (name, tdef, b); | |
6171 | ||
6a000704 NS |
6172 | TYPE_CONTEXT (type) = DECL_CONTEXT (decl); |
6173 | ||
6174 | /* If this is a local class, keep track of it. We need this | |
6175 | information for name-mangling, and so that it is possible to | |
6176 | find all function definitions in a translation unit in a | |
6177 | convenient way. (It's otherwise tricky to find a member | |
6178 | function definition it's only pointed to from within a local | |
6179 | class.) */ | |
9ededfc5 | 6180 | if (TYPE_FUNCTION_SCOPE_P (type)) |
fdaf2f48 JM |
6181 | { |
6182 | if (processing_template_decl) | |
6183 | { | |
6184 | /* Push a DECL_EXPR so we call pushtag at the right time in | |
6185 | template instantiation rather than in some nested context. */ | |
6186 | add_decl_expr (decl); | |
6187 | } | |
6188 | else | |
9771b263 | 6189 | vec_safe_push (local_classes, type); |
fdaf2f48 | 6190 | } |
00e8de68 | 6191 | } |
6a000704 NS |
6192 | if (b->kind == sk_class |
6193 | && !COMPLETE_TYPE_P (current_class_type)) | |
00e8de68 | 6194 | { |
6a000704 NS |
6195 | maybe_add_class_template_decl_list (current_class_type, |
6196 | type, /*friend_p=*/0); | |
3db45ab5 | 6197 | |
6a000704 NS |
6198 | if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) |
6199 | CLASSTYPE_NESTED_UTDS (current_class_type) | |
6200 | = binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
3db45ab5 | 6201 | |
6a000704 NS |
6202 | binding_table_insert |
6203 | (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); | |
00e8de68 | 6204 | } |
6a000704 NS |
6205 | |
6206 | decl = TYPE_NAME (type); | |
6207 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); | |
6a000704 | 6208 | |
b9e75696 JM |
6209 | /* Set type visibility now if this is a forward declaration. */ |
6210 | TREE_PUBLIC (decl) = 1; | |
6211 | determine_visibility (decl); | |
6212 | ||
575bfb00 LC |
6213 | return type; |
6214 | } | |
6215 | ||
6216 | /* Wrapper for pushtag_1. */ | |
6217 | ||
6218 | tree | |
6219 | pushtag (tree name, tree type, tag_scope scope) | |
6220 | { | |
6221 | tree ret; | |
6222 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6223 | ret = pushtag_1 (name, type, scope); | |
6224 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6225 | return ret; | |
00e8de68 | 6226 | } |
8db29d88 | 6227 | |
00e8de68 | 6228 | \f |
00e8de68 GDR |
6229 | /* Subroutines for reverting temporarily to top-level for instantiation |
6230 | of templates and such. We actually need to clear out the class- and | |
6231 | local-value slots of all identifiers, so that only the global values | |
6232 | are at all visible. Simply setting current_binding_level to the global | |
6233 | scope isn't enough, because more binding levels may be pushed. */ | |
6234 | struct saved_scope *scope_chain; | |
6235 | ||
71f15f31 RG |
6236 | /* Return true if ID has not already been marked. */ |
6237 | ||
6238 | static inline bool | |
6239 | store_binding_p (tree id) | |
6240 | { | |
6241 | if (!id || !IDENTIFIER_BINDING (id)) | |
6242 | return false; | |
6243 | ||
6244 | if (IDENTIFIER_MARKED (id)) | |
6245 | return false; | |
6246 | ||
6247 | return true; | |
6248 | } | |
6249 | ||
6250 | /* Add an appropriate binding to *OLD_BINDINGS which needs to already | |
6251 | have enough space reserved. */ | |
89b578be | 6252 | |
f44b0c8e | 6253 | static void |
9771b263 | 6254 | store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings) |
89b578be | 6255 | { |
f32682ca | 6256 | cxx_saved_binding saved; |
89b578be | 6257 | |
71f15f31 | 6258 | gcc_checking_assert (store_binding_p (id)); |
c8094d83 | 6259 | |
f44b0c8e | 6260 | IDENTIFIER_MARKED (id) = 1; |
89b578be | 6261 | |
f32682ca DN |
6262 | saved.identifier = id; |
6263 | saved.binding = IDENTIFIER_BINDING (id); | |
6264 | saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
9771b263 | 6265 | (*old_bindings)->quick_push (saved); |
89b578be | 6266 | IDENTIFIER_BINDING (id) = NULL; |
89b578be MM |
6267 | } |
6268 | ||
f44b0c8e | 6269 | static void |
9771b263 | 6270 | store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings) |
00e8de68 | 6271 | { |
199d1d48 | 6272 | static vec<tree> bindings_need_stored; |
71f15f31 RG |
6273 | tree t, id; |
6274 | size_t i; | |
00e8de68 | 6275 | |
575bfb00 | 6276 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
00e8de68 GDR |
6277 | for (t = names; t; t = TREE_CHAIN (t)) |
6278 | { | |
00e8de68 GDR |
6279 | if (TREE_CODE (t) == TREE_LIST) |
6280 | id = TREE_PURPOSE (t); | |
6281 | else | |
6282 | id = DECL_NAME (t); | |
6283 | ||
71f15f31 | 6284 | if (store_binding_p (id)) |
9771b263 | 6285 | bindings_need_stored.safe_push (id); |
71f15f31 | 6286 | } |
9771b263 | 6287 | if (!bindings_need_stored.is_empty ()) |
71f15f31 | 6288 | { |
9771b263 DN |
6289 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
6290 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 6291 | { |
5764ee3c | 6292 | /* We can apparently have duplicates in NAMES. */ |
71f15f31 RG |
6293 | if (store_binding_p (id)) |
6294 | store_binding (id, old_bindings); | |
6295 | } | |
9771b263 | 6296 | bindings_need_stored.truncate (0); |
00e8de68 | 6297 | } |
575bfb00 | 6298 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
00e8de68 GDR |
6299 | } |
6300 | ||
89b578be MM |
6301 | /* Like store_bindings, but NAMES is a vector of cp_class_binding |
6302 | objects, rather than a TREE_LIST. */ | |
6303 | ||
f44b0c8e | 6304 | static void |
9771b263 DN |
6305 | store_class_bindings (vec<cp_class_binding, va_gc> *names, |
6306 | vec<cxx_saved_binding, va_gc> **old_bindings) | |
89b578be | 6307 | { |
199d1d48 | 6308 | static vec<tree> bindings_need_stored; |
89b578be MM |
6309 | size_t i; |
6310 | cp_class_binding *cb; | |
89b578be | 6311 | |
575bfb00 | 6312 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
9771b263 | 6313 | for (i = 0; vec_safe_iterate (names, i, &cb); ++i) |
71f15f31 | 6314 | if (store_binding_p (cb->identifier)) |
9771b263 DN |
6315 | bindings_need_stored.safe_push (cb->identifier); |
6316 | if (!bindings_need_stored.is_empty ()) | |
71f15f31 RG |
6317 | { |
6318 | tree id; | |
9771b263 DN |
6319 | vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); |
6320 | for (i = 0; bindings_need_stored.iterate (i, &id); ++i) | |
71f15f31 | 6321 | store_binding (id, old_bindings); |
9771b263 | 6322 | bindings_need_stored.truncate (0); |
71f15f31 | 6323 | } |
575bfb00 | 6324 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
89b578be MM |
6325 | } |
6326 | ||
5c712250 PP |
6327 | /* A chain of saved_scope structures awaiting reuse. */ |
6328 | ||
6329 | static GTY((deletable)) struct saved_scope *free_saved_scope; | |
6330 | ||
00e8de68 | 6331 | void |
c353b8e3 | 6332 | push_to_top_level (void) |
00e8de68 GDR |
6333 | { |
6334 | struct saved_scope *s; | |
2c140474 | 6335 | cp_binding_level *b; |
f44b0c8e MM |
6336 | cxx_saved_binding *sb; |
6337 | size_t i; | |
30bcc028 | 6338 | bool need_pop; |
00e8de68 | 6339 | |
575bfb00 | 6340 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); |
5c712250 PP |
6341 | |
6342 | /* Reuse or create a new structure for this saved scope. */ | |
6343 | if (free_saved_scope != NULL) | |
6344 | { | |
6345 | s = free_saved_scope; | |
6346 | free_saved_scope = s->prev; | |
6347 | ||
6348 | vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings; | |
6349 | memset (s, 0, sizeof (*s)); | |
6350 | /* Also reuse the structure's old_bindings vector. */ | |
6351 | vec_safe_truncate (old_bindings, 0); | |
6352 | s->old_bindings = old_bindings; | |
6353 | } | |
6354 | else | |
6355 | s = ggc_cleared_alloc<saved_scope> (); | |
00e8de68 GDR |
6356 | |
6357 | b = scope_chain ? current_binding_level : 0; | |
6358 | ||
6359 | /* If we're in the middle of some function, save our state. */ | |
6360 | if (cfun) | |
6361 | { | |
30bcc028 | 6362 | need_pop = true; |
d2784db4 | 6363 | push_function_context (); |
00e8de68 GDR |
6364 | } |
6365 | else | |
30bcc028 | 6366 | need_pop = false; |
00e8de68 | 6367 | |
89b578be | 6368 | if (scope_chain && previous_class_level) |
f44b0c8e MM |
6369 | store_class_bindings (previous_class_level->class_shadowed, |
6370 | &s->old_bindings); | |
00e8de68 GDR |
6371 | |
6372 | /* Have to include the global scope, because class-scope decls | |
6373 | aren't listed anywhere useful. */ | |
6374 | for (; b; b = b->level_chain) | |
6375 | { | |
6376 | tree t; | |
6377 | ||
6378 | /* Template IDs are inserted into the global level. If they were | |
6379 | inserted into namespace level, finish_file wouldn't find them | |
6380 | when doing pending instantiations. Therefore, don't stop at | |
6381 | namespace level, but continue until :: . */ | |
c353b8e3 | 6382 | if (global_scope_p (b)) |
00e8de68 GDR |
6383 | break; |
6384 | ||
f44b0c8e | 6385 | store_bindings (b->names, &s->old_bindings); |
00e8de68 GDR |
6386 | /* We also need to check class_shadowed to save class-level type |
6387 | bindings, since pushclass doesn't fill in b->names. */ | |
6388 | if (b->kind == sk_class) | |
f44b0c8e | 6389 | store_class_bindings (b->class_shadowed, &s->old_bindings); |
00e8de68 GDR |
6390 | |
6391 | /* Unwind type-value slots back to top level. */ | |
6392 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
6393 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
6394 | } | |
f44b0c8e | 6395 | |
9771b263 | 6396 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb) |
f44b0c8e MM |
6397 | IDENTIFIER_MARKED (sb->identifier) = 0; |
6398 | ||
00e8de68 | 6399 | s->prev = scope_chain; |
00e8de68 GDR |
6400 | s->bindings = b; |
6401 | s->need_pop_function_context = need_pop; | |
6402 | s->function_decl = current_function_decl; | |
7d882b83 ILT |
6403 | s->unevaluated_operand = cp_unevaluated_operand; |
6404 | s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; | |
04f7a48e | 6405 | s->x_stmt_tree.stmts_are_full_exprs_p = true; |
00e8de68 GDR |
6406 | |
6407 | scope_chain = s; | |
6408 | current_function_decl = NULL_TREE; | |
9771b263 | 6409 | vec_alloc (current_lang_base, 10); |
00e8de68 GDR |
6410 | current_lang_name = lang_name_cplusplus; |
6411 | current_namespace = global_namespace; | |
c888c93b | 6412 | push_class_stack (); |
7d882b83 ILT |
6413 | cp_unevaluated_operand = 0; |
6414 | c_inhibit_evaluation_warnings = 0; | |
575bfb00 | 6415 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); |
00e8de68 GDR |
6416 | } |
6417 | ||
575bfb00 LC |
6418 | static void |
6419 | pop_from_top_level_1 (void) | |
00e8de68 GDR |
6420 | { |
6421 | struct saved_scope *s = scope_chain; | |
6422 | cxx_saved_binding *saved; | |
f44b0c8e | 6423 | size_t i; |
00e8de68 | 6424 | |
00e8de68 | 6425 | /* Clear out class-level bindings cache. */ |
89b578be | 6426 | if (previous_class_level) |
00e8de68 | 6427 | invalidate_class_lookup_cache (); |
c888c93b | 6428 | pop_class_stack (); |
00e8de68 GDR |
6429 | |
6430 | current_lang_base = 0; | |
6431 | ||
6432 | scope_chain = s->prev; | |
9771b263 | 6433 | FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved) |
00e8de68 GDR |
6434 | { |
6435 | tree id = saved->identifier; | |
6436 | ||
6437 | IDENTIFIER_BINDING (id) = saved->binding; | |
00e8de68 GDR |
6438 | SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); |
6439 | } | |
6440 | ||
6441 | /* If we were in the middle of compiling a function, restore our | |
6442 | state. */ | |
6443 | if (s->need_pop_function_context) | |
d2784db4 | 6444 | pop_function_context (); |
00e8de68 | 6445 | current_function_decl = s->function_decl; |
7d882b83 ILT |
6446 | cp_unevaluated_operand = s->unevaluated_operand; |
6447 | c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings; | |
5c712250 PP |
6448 | |
6449 | /* Make this saved_scope structure available for reuse by | |
6450 | push_to_top_level. */ | |
6451 | s->prev = free_saved_scope; | |
6452 | free_saved_scope = s; | |
00e8de68 GDR |
6453 | } |
6454 | ||
575bfb00 LC |
6455 | /* Wrapper for pop_from_top_level_1. */ |
6456 | ||
6457 | void | |
6458 | pop_from_top_level (void) | |
6459 | { | |
6460 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6461 | pop_from_top_level_1 (); | |
6462 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6463 | } | |
6464 | ||
b655c310 NS |
6465 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
6466 | select a name that is unique to this compilation unit. Returns FALSE if | |
6467 | pushdecl fails, TRUE otherwise. */ | |
6468 | ||
6469 | bool | |
6470 | push_namespace (tree name) | |
6471 | { | |
6472 | tree d = NULL_TREE; | |
6473 | bool need_new = true; | |
6474 | bool implicit_use = false; | |
6475 | bool anon = !name; | |
6476 | ||
6477 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6478 | ||
6479 | /* We should not get here if the global_namespace is not yet constructed | |
6480 | nor if NAME designates the global namespace: The global scope is | |
6481 | constructed elsewhere. */ | |
ad9870f2 | 6482 | gcc_assert (global_namespace != NULL && name != global_identifier); |
b655c310 NS |
6483 | |
6484 | if (anon) | |
6485 | { | |
ad9870f2 | 6486 | name = anon_identifier; |
06aa5490 | 6487 | d = get_namespace_binding (current_namespace, name); |
b655c310 NS |
6488 | if (d) |
6489 | /* Reopening anonymous namespace. */ | |
6490 | need_new = false; | |
6491 | implicit_use = true; | |
6492 | } | |
6493 | else | |
6494 | { | |
6495 | /* Check whether this is an extended namespace definition. */ | |
06aa5490 | 6496 | d = get_namespace_binding (current_namespace, name); |
b655c310 NS |
6497 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) |
6498 | { | |
6499 | tree dna = DECL_NAMESPACE_ALIAS (d); | |
6500 | if (dna) | |
6501 | { | |
6502 | /* We do some error recovery for, eg, the redeclaration | |
6503 | of M here: | |
6504 | ||
6505 | namespace N {} | |
6506 | namespace M = N; | |
6507 | namespace M {} | |
6508 | ||
6509 | However, in nasty cases like: | |
6510 | ||
6511 | namespace N | |
6512 | { | |
6513 | namespace M = N; | |
6514 | namespace M {} | |
6515 | } | |
6516 | ||
6517 | we just error out below, in duplicate_decls. */ | |
6518 | if (NAMESPACE_LEVEL (dna)->level_chain | |
6519 | == current_binding_level) | |
6520 | { | |
6521 | error ("namespace alias %qD not allowed here, " | |
6522 | "assuming %qD", d, dna); | |
6523 | d = dna; | |
6524 | need_new = false; | |
6525 | } | |
6526 | } | |
6527 | else | |
6528 | need_new = false; | |
6529 | } | |
6530 | } | |
6531 | ||
6532 | if (need_new) | |
6533 | { | |
6534 | /* Make a new namespace, binding the name to it. */ | |
6535 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); | |
6536 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); | |
6537 | /* The name of this namespace is not visible to other translation | |
6538 | units if it is an anonymous namespace or member thereof. */ | |
6539 | if (anon || decl_anon_ns_mem_p (current_namespace)) | |
6540 | TREE_PUBLIC (d) = 0; | |
6541 | else | |
6542 | TREE_PUBLIC (d) = 1; | |
6543 | if (pushdecl (d) == error_mark_node) | |
6544 | { | |
6545 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6546 | return false; | |
6547 | } | |
6548 | if (anon) | |
6549 | { | |
6550 | /* Clear DECL_NAME for the benefit of debugging back ends. */ | |
6551 | SET_DECL_ASSEMBLER_NAME (d, name); | |
6552 | DECL_NAME (d) = NULL_TREE; | |
6553 | } | |
6554 | begin_scope (sk_namespace, d); | |
6555 | } | |
6556 | else | |
6557 | resume_scope (NAMESPACE_LEVEL (d)); | |
6558 | ||
6559 | if (implicit_use) | |
6560 | do_using_directive (d); | |
6561 | /* Enter the name space. */ | |
6562 | current_namespace = d; | |
6563 | ||
6564 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6565 | return true; | |
6566 | } | |
6567 | ||
6568 | /* Pop from the scope of the current namespace. */ | |
6569 | ||
6570 | void | |
6571 | pop_namespace (void) | |
6572 | { | |
6573 | gcc_assert (current_namespace != global_namespace); | |
6574 | current_namespace = CP_DECL_CONTEXT (current_namespace); | |
6575 | /* The binding level is not popped, as it might be re-opened later. */ | |
6576 | leave_scope (); | |
6577 | } | |
6578 | ||
6579 | /* Push into the scope of the namespace NS, even if it is deeply | |
6580 | nested within another namespace. */ | |
6581 | ||
6582 | void | |
6583 | push_nested_namespace (tree ns) | |
6584 | { | |
6585 | if (ns == global_namespace) | |
6586 | push_to_top_level (); | |
6587 | else | |
6588 | { | |
6589 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
6590 | push_namespace (DECL_NAME (ns)); | |
6591 | } | |
6592 | } | |
6593 | ||
6594 | /* Pop back from the scope of the namespace NS, which was previously | |
6595 | entered with push_nested_namespace. */ | |
6596 | ||
6597 | void | |
6598 | pop_nested_namespace (tree ns) | |
6599 | { | |
6600 | bool subtime = timevar_cond_start (TV_NAME_LOOKUP); | |
6601 | gcc_assert (current_namespace == ns); | |
6602 | while (ns != global_namespace) | |
6603 | { | |
6604 | pop_namespace (); | |
6605 | ns = CP_DECL_CONTEXT (ns); | |
6606 | } | |
6607 | ||
6608 | pop_from_top_level (); | |
6609 | timevar_cond_stop (TV_NAME_LOOKUP, subtime); | |
6610 | } | |
575bfb00 | 6611 | |
00e8de68 GDR |
6612 | /* Pop off extraneous binding levels left over due to syntax errors. |
6613 | ||
6614 | We don't pop past namespaces, as they might be valid. */ | |
6615 | ||
6616 | void | |
6617 | pop_everything (void) | |
6618 | { | |
6619 | if (ENABLE_SCOPE_CHECKING) | |
6620 | verbatim ("XXX entering pop_everything ()\n"); | |
056a17ee | 6621 | while (!namespace_bindings_p ()) |
00e8de68 GDR |
6622 | { |
6623 | if (current_binding_level->kind == sk_class) | |
6624 | pop_nested_class (); | |
6625 | else | |
6626 | poplevel (0, 0, 0); | |
6627 | } | |
6628 | if (ENABLE_SCOPE_CHECKING) | |
6629 | verbatim ("XXX leaving pop_everything ()\n"); | |
6630 | } | |
6631 | ||
6097b0c3 | 6632 | /* Emit debugging information for using declarations and directives. |
c8094d83 | 6633 | If input tree is overloaded fn then emit debug info for all |
6097b0c3 DP |
6634 | candidates. */ |
6635 | ||
98ed9dae | 6636 | void |
6097b0c3 DP |
6637 | cp_emit_debug_info_for_using (tree t, tree context) |
6638 | { | |
099f36ab | 6639 | /* Don't try to emit any debug information if we have errors. */ |
1da2ed5f | 6640 | if (seen_error ()) |
099f36ab JM |
6641 | return; |
6642 | ||
c8094d83 | 6643 | /* Ignore this FUNCTION_DECL if it refers to a builtin declaration |
6097b0c3 | 6644 | of a builtin function. */ |
c8094d83 | 6645 | if (TREE_CODE (t) == FUNCTION_DECL |
6097b0c3 DP |
6646 | && DECL_EXTERNAL (t) |
6647 | && DECL_BUILT_IN (t)) | |
6648 | return; | |
6649 | ||
6650 | /* Do not supply context to imported_module_or_decl, if | |
6651 | it is a global namespace. */ | |
6652 | if (context == global_namespace) | |
6653 | context = NULL_TREE; | |
c8094d83 | 6654 | |
6097b0c3 DP |
6655 | if (BASELINK_P (t)) |
6656 | t = BASELINK_FUNCTIONS (t); | |
c8094d83 | 6657 | |
6097b0c3 DP |
6658 | /* FIXME: Handle TEMPLATE_DECLs. */ |
6659 | for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t)) | |
6660 | if (TREE_CODE (t) != TEMPLATE_DECL) | |
98381eb4 | 6661 | { |
38e01f9e | 6662 | if (building_stmt_list_p ()) |
c2255bc4 | 6663 | add_stmt (build_stmt (input_location, USING_STMT, t)); |
98381eb4 JJ |
6664 | else |
6665 | (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false); | |
6666 | } | |
98ed9dae | 6667 | } |
6097b0c3 | 6668 | |
28ea4c88 | 6669 | #include "gt-cp-name-lookup.h" |