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