Branch data Line data Source code
1 : : /* Declarations for -*- C++ -*- name lookup routines.
2 : : Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 : : Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify
8 : : it under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful,
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
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #ifndef GCC_CP_NAME_LOOKUP_H
22 : : #define GCC_CP_NAME_LOOKUP_H
23 : :
24 : : #include "c-family/c-common.h"
25 : :
26 : :
27 : : /* The datatype used to implement C++ scope. */
28 : : struct cp_binding_level;
29 : :
30 : : /* Nonzero if this binding is for a local scope, as opposed to a class
31 : : or namespace scope. */
32 : : #define LOCAL_BINDING_P(NODE) ((NODE)->is_local)
33 : :
34 : : /* True if NODE->value is from a base class of the class which is
35 : : currently being defined. */
36 : : #define INHERITED_VALUE_BINDING_P(NODE) ((NODE)->value_is_inherited)
37 : :
38 : : /* The IMPLICIT_TYPEDEF is hidden from ordinary name lookup (it was
39 : : injected via a local class's friend decl). The typdef may be in the
40 : : VALUE or the TYPE slot. We do not get the situation where the
41 : : value and type slots are both filled and both hidden. */
42 : : #define HIDDEN_TYPE_BINDING_P(NODE) ((NODE)->type_is_hidden)
43 : :
44 : : /* Datatype that represents binding established by a declaration between
45 : : a name and a C++ entity. */
46 : : struct GTY(()) cxx_binding {
47 : : /* Link to chain together various bindings for this name. */
48 : : cxx_binding *previous;
49 : : /* The non-type entity this name is bound to. */
50 : : tree value;
51 : : /* The type entity this name is bound to. */
52 : : tree type;
53 : : /* The scope at which this binding was made. */
54 : : cp_binding_level *scope;
55 : :
56 : : bool value_is_inherited : 1;
57 : : bool is_local : 1;
58 : : bool type_is_hidden : 1;
59 : : };
60 : :
61 : : /* Datatype used to temporarily save C++ bindings (for implicit
62 : : instantiations purposes and like). Implemented in decl.cc. */
63 : : struct GTY(()) cxx_saved_binding {
64 : : /* The name of the current binding. */
65 : : tree identifier;
66 : : /* The binding we're saving. */
67 : : cxx_binding *binding;
68 : : tree real_type_value;
69 : : };
70 : :
71 : : /* To support lazy module loading, we squirrel away a section number
72 : : (and a couple of flags) in the binding slot of unloaded bindings.
73 : : We rely on pointers being aligned and setting the bottom bit to
74 : : mark a lazy value. GTY doesn't like an array of union, so we have
75 : : a containing struct. */
76 : :
77 : : struct GTY(()) binding_slot {
78 : : union GTY((desc ("%1.is_lazy ()"))) binding_slot_lazy {
79 : : tree GTY((tag ("false"))) binding;
80 : : } u;
81 : :
82 : 2492878 : operator tree & ()
83 : : {
84 : 0 : gcc_checking_assert (!is_lazy ());
85 : 2492878 : return u.binding;
86 : : }
87 : 3029211 : binding_slot &operator= (tree t)
88 : : {
89 : 3029211 : u.binding = t;
90 : 3029211 : return *this;
91 : : }
92 : 7892712 : bool is_lazy () const
93 : : {
94 : 7856131 : return bool (uintptr_t (u.binding) & 1);
95 : : }
96 : 1949659 : void set_lazy (unsigned snum)
97 : : {
98 : 1949659 : gcc_checking_assert (!u.binding);
99 : 1949659 : u.binding = tree (uintptr_t ((snum << 1) | 1));
100 : 1949659 : }
101 : : void or_lazy (unsigned snum)
102 : : {
103 : : gcc_checking_assert (is_lazy ());
104 : : u.binding = tree (uintptr_t (u.binding) | (snum << 1));
105 : : }
106 : 36581 : unsigned get_lazy () const
107 : : {
108 : 36581 : gcc_checking_assert (is_lazy ());
109 : 36581 : return unsigned (uintptr_t (u.binding) >> 1);
110 : : }
111 : : };
112 : :
113 : : /* Bindings for modules are held in a sparse array. There is always a
114 : : current TU slot, others are allocated as needed. By construction
115 : : of the importing mechanism we only ever need to append to the
116 : : array. Rather than have straight index/slot tuples, we bunch them
117 : : up for greater packing.
118 : :
119 : : The cluster representation packs well on a 64-bit system. */
120 : :
121 : : #define BINDING_VECTOR_SLOTS_PER_CLUSTER 2
122 : : struct binding_index {
123 : : unsigned short base;
124 : : unsigned short span;
125 : : };
126 : :
127 : : struct GTY(()) binding_cluster
128 : : {
129 : : binding_index GTY((skip)) indices[BINDING_VECTOR_SLOTS_PER_CLUSTER];
130 : : binding_slot slots[BINDING_VECTOR_SLOTS_PER_CLUSTER];
131 : : };
132 : :
133 : : /* These two fields overlay lang flags. So don't use those. */
134 : : #define BINDING_VECTOR_ALLOC_CLUSTERS(NODE) \
135 : : (BINDING_VECTOR_CHECK (NODE)->base.u.dependence_info.clique)
136 : : #define BINDING_VECTOR_NUM_CLUSTERS(NODE) \
137 : : (BINDING_VECTOR_CHECK (NODE)->base.u.dependence_info.base)
138 : : #define BINDING_VECTOR_CLUSTER_BASE(NODE) \
139 : : (((tree_binding_vec *)BINDING_VECTOR_CHECK (NODE))->vec)
140 : : #define BINDING_VECTOR_CLUSTER_LAST(NODE) \
141 : : (&BINDING_VECTOR_CLUSTER (NODE, BINDING_VECTOR_NUM_CLUSTERS (NODE) - 1))
142 : : #define BINDING_VECTOR_CLUSTER(NODE,IX) \
143 : : (((tree_binding_vec *)BINDING_VECTOR_CHECK (NODE))->vec[IX])
144 : :
145 : : struct GTY(()) tree_binding_vec {
146 : : struct tree_base base;
147 : : tree name;
148 : : binding_cluster GTY((length ("%h.base.u.dependence_info.base"))) vec[1];
149 : : };
150 : :
151 : : /* The name of a module vector. */
152 : : #define BINDING_VECTOR_NAME(NODE) \
153 : : (((tree_binding_vec *)BINDING_VECTOR_CHECK (NODE))->name)
154 : :
155 : : /* tree_binding_vec does uses base.u.dependence_info.base field for
156 : : length. It does not have lang_flag etc available! */
157 : :
158 : : /* These two flags note if a module-vector contains deduplicated
159 : : bindings (i.e. multiple declarations in different imports). */
160 : : /* This binding contains duplicate references to a global module
161 : : entity. */
162 : : #define BINDING_VECTOR_GLOBAL_DUPS_P(NODE) \
163 : : (BINDING_VECTOR_CHECK (NODE)->base.static_flag)
164 : : /* This binding contains duplicate references to a partioned module
165 : : entity. */
166 : : #define BINDING_VECTOR_PARTITION_DUPS_P(NODE) \
167 : : (BINDING_VECTOR_CHECK (NODE)->base.volatile_flag)
168 : :
169 : : /* These two flags indicate the provenence of the bindings on this
170 : : particular vector slot. We can of course determine this from slot
171 : : number, but that's a relatively expensive lookup. This avoids
172 : : that when iterating. */
173 : : /* This slot is part of the global module (a header unit). */
174 : : #define MODULE_BINDING_GLOBAL_P(NODE) \
175 : : (OVERLOAD_CHECK (NODE)->base.static_flag)
176 : : /* This slot is part of the current module (a partition or primary). */
177 : : #define MODULE_BINDING_PARTITION_P(NODE) \
178 : : (OVERLOAD_CHECK (NODE)->base.volatile_flag)
179 : :
180 : : extern void set_identifier_type_value (tree, tree);
181 : : extern void push_binding (tree, tree, cp_binding_level*);
182 : : extern void pop_local_binding (tree, tree);
183 : : extern void pop_bindings_and_leave_scope (void);
184 : : extern tree constructor_name (tree);
185 : : extern bool constructor_name_p (tree, tree);
186 : :
187 : : /* The kinds of scopes we recognize. */
188 : : enum scope_kind {
189 : : sk_block = 0, /* An ordinary block scope. This enumerator must
190 : : have the value zero because "cp_binding_level"
191 : : is initialized by using "memset" to set the
192 : : contents to zero, and the default scope kind
193 : : is "sk_block". */
194 : : sk_cleanup, /* A scope for (pseudo-)scope for cleanup. It is
195 : : pseudo in that it is transparent to name lookup
196 : : activities. */
197 : : sk_try, /* A try-block. */
198 : : sk_catch, /* A catch-block. */
199 : : sk_for, /* The scope of the variable declared in a
200 : : init-statement. */
201 : : sk_cond, /* The scope of the variable declared in the condition
202 : : of an if or switch statement. */
203 : : sk_stmt_expr, /* GNU statement expression block. */
204 : : sk_function_parms, /* The scope containing function parameters. */
205 : : sk_class, /* The scope containing the members of a class. */
206 : : sk_scoped_enum, /* The scope containing the enumerators of a C++11
207 : : scoped enumeration. */
208 : : sk_namespace, /* The scope containing the members of a
209 : : namespace, including the global scope. */
210 : : sk_template_parms, /* A scope for template parameters. */
211 : : sk_template_spec, /* Like sk_template_parms, but for an explicit
212 : : specialization. Since, by definition, an
213 : : explicit specialization is introduced by
214 : : "template <>", this scope is always empty. */
215 : : sk_transaction, /* A synchronized or atomic statement. */
216 : : sk_omp, /* An OpenMP structured block. */
217 : : sk_count /* Number of scope_kind enumerations. */
218 : : };
219 : :
220 : : struct GTY(()) cp_class_binding {
221 : : cxx_binding *base;
222 : : /* The bound name. */
223 : : tree identifier;
224 : : };
225 : :
226 : : /* For each binding contour we allocate a binding_level structure
227 : : which records the names defined in that contour.
228 : : Contours include:
229 : : 0) the global one
230 : : 1) one for each function definition,
231 : : where internal declarations of the parameters appear.
232 : : 2) one for each compound statement,
233 : : to record its declarations.
234 : :
235 : : The current meaning of a name can be found by searching the levels
236 : : from the current one out to the global one.
237 : :
238 : : Off to the side, may be the class_binding_level. This exists only
239 : : to catch class-local declarations. It is otherwise nonexistent.
240 : :
241 : : Also there may be binding levels that catch cleanups that must be
242 : : run when exceptions occur. Thus, to see whether a name is bound in
243 : : the current scope, it is not enough to look in the
244 : : CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
245 : : instead. */
246 : :
247 : : struct GTY(()) cp_binding_level {
248 : : /* A chain of _DECL nodes for all variables, constants, functions,
249 : : and typedef types. These are in the reverse of the order
250 : : supplied. There may be OVERLOADs on this list, too, but they
251 : : are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
252 : : tree names;
253 : :
254 : : /* Using directives. */
255 : : vec<tree, va_gc> *using_directives;
256 : :
257 : : /* For the binding level corresponding to a class, the entities
258 : : declared in the class or its base classes. */
259 : : vec<cp_class_binding, va_gc> *class_shadowed;
260 : :
261 : : /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
262 : : is used for all binding levels. The TREE_PURPOSE is the name of
263 : : the entity, the TREE_TYPE is the associated type. In addition
264 : : the TREE_VALUE is the IDENTIFIER_TYPE_VALUE before we entered
265 : : the class. */
266 : : tree type_shadowed;
267 : :
268 : : /* For each level (except not the global one),
269 : : a chain of BLOCK nodes for all the levels
270 : : that were entered and exited one level down. */
271 : : tree blocks;
272 : :
273 : : /* The entity (namespace, class, function) the scope of which this
274 : : binding contour corresponds to. Otherwise NULL. */
275 : : tree this_entity;
276 : :
277 : : /* The binding level which this one is contained in (inherits from). */
278 : : cp_binding_level *level_chain;
279 : :
280 : : /* STATEMENT_LIST for statements in this binding contour.
281 : : Only used at present for SK_CLEANUP temporary bindings. */
282 : : tree statement_list;
283 : :
284 : : /* Binding depth at which this level began. */
285 : : int binding_depth;
286 : :
287 : : /* The kind of scope that this object represents. However, a
288 : : SK_TEMPLATE_SPEC scope is represented with KIND set to
289 : : SK_TEMPLATE_PARMS and EXPLICIT_SPEC_P set to true. */
290 : : ENUM_BITFIELD (scope_kind) kind : 4;
291 : :
292 : : /* True if this scope is an SK_TEMPLATE_SPEC scope. This field is
293 : : only valid if KIND == SK_TEMPLATE_PARMS. */
294 : : BOOL_BITFIELD explicit_spec_p : 1;
295 : :
296 : : /* True means make a BLOCK for this level regardless of all else. */
297 : : unsigned keep : 1;
298 : :
299 : : /* Nonzero if this level can safely have additional
300 : : cleanup-needing variables added to it. */
301 : : unsigned more_cleanups_ok : 1;
302 : : unsigned have_cleanups : 1;
303 : :
304 : : /* Transient state set if this scope is of sk_class kind
305 : : and is in the process of defining 'this_entity'. Reset
306 : : on leaving the class definition to allow for the scope
307 : : to be subsequently re-used as a non-defining scope for
308 : : 'this_entity'. */
309 : : unsigned defining_class_p : 1;
310 : :
311 : : /* True for SK_FUNCTION_PARMS of a requires-expression. */
312 : : unsigned requires_expression : 1;
313 : :
314 : : /* True for artificial blocks which should be ignored when finding
315 : : parent scope. */
316 : : unsigned artificial : 1;
317 : :
318 : : /* 21 bits left to fill a 32-bit word. */
319 : : };
320 : :
321 : : /* The binding level currently in effect. */
322 : :
323 : : #define current_binding_level \
324 : : (*(cfun && cp_function_chain && cp_function_chain->bindings \
325 : : ? &cp_function_chain->bindings \
326 : : : &scope_chain->bindings))
327 : :
328 : : /* The binding level of the current class, if any. */
329 : :
330 : : #define class_binding_level scope_chain->class_bindings
331 : :
332 : : /* True if SCOPE designates the global scope binding contour. */
333 : : #define global_scope_p(SCOPE) \
334 : : ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
335 : :
336 : : extern cp_binding_level *leave_scope (void);
337 : : extern bool kept_level_p (void);
338 : : extern bool global_bindings_p (void);
339 : : extern bool toplevel_bindings_p (void);
340 : : extern bool namespace_bindings_p (void);
341 : : extern bool local_bindings_p (void);
342 : : extern bool template_parm_scope_p (void);
343 : : extern scope_kind innermost_scope_kind (void);
344 : : extern cp_binding_level *begin_scope (scope_kind, tree);
345 : : extern void print_binding_stack (void);
346 : : extern void pop_everything (void);
347 : : extern void keep_next_level (bool);
348 : : extern bool is_ancestor (tree ancestor, tree descendant);
349 : : extern bool is_nested_namespace (tree parent, tree descendant,
350 : : bool inline_only = false);
351 : : extern tree push_scope (tree);
352 : : extern void pop_scope (tree);
353 : : extern tree push_inner_scope (tree);
354 : : extern void pop_inner_scope (tree, tree);
355 : : extern void push_binding_level (cp_binding_level *);
356 : :
357 : : extern bool handle_namespace_attrs (tree, tree);
358 : : extern void pushlevel_class (void);
359 : : extern void poplevel_class (void);
360 : :
361 : : /* What kind of scopes name lookup looks in. An enum class so we
362 : : don't accidentally mix integers. */
363 : : enum class LOOK_where
364 : : {
365 : : BLOCK = 1 << 0, /* Consider block scopes. */
366 : : CLASS = 1 << 1, /* Consider class scopes. */
367 : : NAMESPACE = 1 << 2, /* Consider namespace scopes. */
368 : :
369 : : ALL = BLOCK | CLASS | NAMESPACE,
370 : : BLOCK_NAMESPACE = BLOCK | NAMESPACE,
371 : : CLASS_NAMESPACE = CLASS | NAMESPACE,
372 : : };
373 : : constexpr LOOK_where operator| (LOOK_where a, LOOK_where b)
374 : : {
375 : : return LOOK_where (unsigned (a) | unsigned (b));
376 : : }
377 : : constexpr LOOK_where operator& (LOOK_where a, LOOK_where b)
378 : : {
379 : : return LOOK_where (unsigned (a) & unsigned (b));
380 : : }
381 : :
382 : : enum class LOOK_want
383 : : {
384 : : NORMAL = 0, /* Normal lookup -- non-types can hide implicit types. */
385 : : TYPE = 1 << 1, /* We only want TYPE_DECLS. */
386 : : NAMESPACE = 1 << 2, /* We only want NAMESPACE_DECLS. */
387 : :
388 : : HIDDEN_FRIEND = 1 << 3, /* See hidden friends. */
389 : : HIDDEN_LAMBDA = 1 << 4, /* See lambda-ignored entities. */
390 : :
391 : : ANY_REACHABLE = 1 << 5, /* Include reachable module declarations not
392 : : normally visible to name lookup. */
393 : :
394 : : TYPE_NAMESPACE = TYPE | NAMESPACE, /* Either NAMESPACE or TYPE. */
395 : : };
396 : : constexpr LOOK_want operator| (LOOK_want a, LOOK_want b)
397 : : {
398 : : return LOOK_want (unsigned (a) | unsigned (b));
399 : : }
400 : : constexpr LOOK_want operator& (LOOK_want a, LOOK_want b)
401 : : {
402 : : return LOOK_want (unsigned (a) & unsigned (b));
403 : : }
404 : :
405 : : extern tree lookup_name (tree, LOOK_where, LOOK_want = LOOK_want::NORMAL);
406 : : /* Also declared in c-family/c-common.h. */
407 : : extern tree lookup_name (tree name);
408 : 3564936597 : inline tree lookup_name (tree name, LOOK_want want)
409 : : {
410 : 3564936597 : return lookup_name (name, LOOK_where::ALL, want);
411 : : }
412 : :
413 : : enum class TAG_how
414 : : {
415 : : CURRENT_ONLY = 0, // Look and insert only in current scope
416 : :
417 : : GLOBAL = 1, // Unqualified lookup, innermost-non-class insertion
418 : :
419 : : INNERMOST_NON_CLASS = 2, // Look and insert only into
420 : : // innermost-non-class
421 : :
422 : : HIDDEN_FRIEND = 3, // As INNERMOST_NON_CLASS, but hide it
423 : : };
424 : :
425 : : extern tree lookup_elaborated_type (tree, TAG_how);
426 : : extern tree get_namespace_binding (tree ns, tree id);
427 : : extern void set_global_binding (tree decl);
428 : 9112828 : inline tree get_global_binding (tree id)
429 : : {
430 : 9106176 : return get_namespace_binding (NULL_TREE, id);
431 : : }
432 : : extern tree lookup_qualified_name (tree scope, tree name,
433 : : LOOK_want = LOOK_want::NORMAL,
434 : : bool = true);
435 : : extern tree lookup_qualified_name (tree scope, const char *name,
436 : : LOOK_want = LOOK_want::NORMAL,
437 : : bool = true);
438 : : extern bool pushdecl_class_level (tree);
439 : : extern tree pushdecl_namespace_level (tree, bool hiding = false);
440 : : extern bool push_class_level_binding (tree, tree);
441 : : extern tree get_local_decls ();
442 : : extern int function_parm_depth (void);
443 : : extern tree cp_namespace_decls (tree);
444 : : extern void set_decl_namespace (tree, tree, bool);
445 : : extern void push_decl_namespace (tree);
446 : : extern void pop_decl_namespace (void);
447 : : extern void do_namespace_alias (tree, tree);
448 : : extern tree do_class_using_decl (tree, tree);
449 : : extern tree lookup_arg_dependent (tree, tree, vec<tree, va_gc> *);
450 : : extern tree search_anon_aggr (tree, tree, bool = false);
451 : : extern tree get_class_binding_direct (tree, tree, bool want_type = false);
452 : : extern tree get_class_binding (tree, tree, bool want_type = false);
453 : : extern tree *find_member_slot (tree klass, tree name);
454 : : extern tree *add_member_slot (tree klass, tree name);
455 : : extern void resort_type_member_vec (void *, void *,
456 : : gt_pointer_operator, void *);
457 : : extern vec<tree, va_gc> *set_class_bindings (tree, int extra = 0);
458 : : extern void insert_late_enum_def_bindings (tree, tree);
459 : : extern tree innermost_non_namespace_value (tree);
460 : : extern bool decl_in_scope_p (tree);
461 : : extern cxx_binding *outer_binding (tree, cxx_binding *, bool);
462 : : extern void cp_emit_debug_info_for_using (tree, tree);
463 : :
464 : : extern void finish_nonmember_using_decl (tree scope, tree name);
465 : : extern void finish_using_directive (tree target, tree attribs);
466 : : void push_local_extern_decl_alias (tree decl);
467 : : extern tree pushdecl (tree, bool hiding = false);
468 : : extern tree pushdecl_outermost_localscope (tree);
469 : : extern tree pushdecl_top_level (tree);
470 : : extern tree pushdecl_top_level_and_finish (tree, tree);
471 : : extern tree pushtag (tree, tree, TAG_how = TAG_how::CURRENT_ONLY);
472 : : extern int push_namespace (tree, bool make_inline = false);
473 : : extern void pop_namespace (void);
474 : : extern void push_nested_namespace (tree);
475 : : extern void pop_nested_namespace (tree);
476 : : extern void push_to_top_level (void);
477 : : extern void pop_from_top_level (void);
478 : : extern bool maybe_push_to_top_level (tree);
479 : : extern void maybe_pop_from_top_level (bool);
480 : : extern void push_using_decl_bindings (tree, tree);
481 : :
482 : : /* Lower level interface for modules. */
483 : : extern tree *mergeable_namespace_slots (tree ns, tree name, bool is_attached,
484 : : tree *mvec);
485 : : extern void add_mergeable_namespace_entity (tree *slot, tree decl);
486 : : extern tree lookup_class_binding (tree ctx, tree name);
487 : : extern bool import_module_binding (tree ctx, tree name, unsigned mod,
488 : : unsigned snum);
489 : : extern bool set_module_binding (tree ctx, tree name, unsigned mod,
490 : : bool global_p, bool partition_p,
491 : : tree value, tree type, tree visible);
492 : : extern void add_module_namespace_decl (tree ns, tree decl);
493 : :
494 : : enum WMB_Flags
495 : : {
496 : : WMB_None = 0,
497 : : WMB_Dups = 1 << 0,
498 : : WMB_Export = 1 << 1,
499 : : WMB_Using = 1 << 2,
500 : : WMB_Hidden = 1 << 3,
501 : : WMB_Purview = 1 << 4,
502 : : };
503 : :
504 : : extern unsigned walk_module_binding (tree binding, bitmap partitions,
505 : : bool (*)(tree decl, WMB_Flags, void *data),
506 : : void *data);
507 : : extern tree add_imported_namespace (tree ctx, tree name, location_t,
508 : : unsigned module,
509 : : bool inline_p, bool visible_p);
510 : : extern const char *get_cxx_dialect_name (enum cxx_dialect dialect);
511 : :
512 : : #endif /* GCC_CP_NAME_LOOKUP_H */
|