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