]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/name-lookup.h
c++: missing SFINAE with pointer subtraction [PR78173]
[thirdparty/gcc.git] / gcc / cp / name-lookup.h
CommitLineData
6db76e48 1/* Declarations for -*- C++ -*- name lookup routines.
8d9254fc 2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
aed81407
GDR
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
ed3cf953 5This file is part of GCC.
aed81407 6
ed3cf953 7GCC is free software; you can redistribute it and/or modify
aed81407 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
aed81407
GDR
10any later version.
11
ed3cf953 12GCC is distributed in the hope that it will be useful,
aed81407
GDR
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
aed81407
GDR
20
21#ifndef GCC_CP_NAME_LOOKUP_H
22#define GCC_CP_NAME_LOOKUP_H
23
39dabefd 24#include "c-family/c-common.h"
aed81407 25
5e0c54e5 26\f
ed3cf953 27/* The datatype used to implement C++ scope. */
c80ee302 28struct cp_binding_level;
ed3cf953 29
aed81407
GDR
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
147135cc 34/* True if NODE->value is from a base class of the class which is
aed81407
GDR
35 currently being defined. */
36#define INHERITED_VALUE_BINDING_P(NODE) ((NODE)->value_is_inherited)
37
7cbfe089
NS
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
c80ee302
NS
44/* Datatype that represents binding established by a declaration between
45 a name and a C++ entity. */
d1b38208 46struct GTY(()) cxx_binding {
aed81407
GDR
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;
ed3cf953 53 /* The scope at which this binding was made. */
2c140474 54 cp_binding_level *scope;
c80ee302
NS
55
56 bool value_is_inherited : 1;
57 bool is_local : 1;
7cbfe089 58 bool type_is_hidden : 1;
aed81407
GDR
59};
60
f44b0c8e
MM
61/* Datatype used to temporarily save C++ bindings (for implicit
62 instantiations purposes and like). Implemented in decl.c. */
a79683d5 63struct GTY(()) cxx_saved_binding {
f44b0c8e
MM
64 /* The name of the current binding. */
65 tree identifier;
66 /* The binding we're saving. */
67 cxx_binding *binding;
f44b0c8e 68 tree real_type_value;
a79683d5 69};
f44b0c8e 70
05f7a2af
NS
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
77struct GTY(()) binding_slot {
78 union GTY((desc ("%1.is_lazy ()"))) binding_slot_lazy {
79 tree GTY((tag ("false"))) binding;
80 } u;
81
82 operator tree & ()
83 {
84 gcc_checking_assert (!is_lazy ());
85 return u.binding;
86 }
87 binding_slot &operator= (tree t)
88 {
89 u.binding = t;
90 return *this;
91 }
92 bool is_lazy () const
93 {
94 return bool (uintptr_t (u.binding) & 1);
95 }
96 void set_lazy (unsigned snum)
97 {
98 gcc_checking_assert (!u.binding);
99 u.binding = tree (uintptr_t ((snum << 1) | 1));
100 }
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 unsigned get_lazy () const
107 {
108 gcc_checking_assert (is_lazy ());
109 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
122struct binding_index {
123 unsigned short base;
124 unsigned short span;
125};
126
127struct 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
145struct 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/* There are specializations of a template keyed to this binding. */
181#define BINDING_VECTOR_PENDING_SPECIALIZATIONS_P(NODE) \
182 (BINDING_VECTOR_CHECK (NODE)->base.public_flag)
183/* The key is in a header unit (not a named module partition or
184 primary). */
185#define BINDING_VECTOR_PENDING_IS_HEADER_P(NODE) \
186 (BINDING_VECTOR_CHECK (NODE)->base.protected_flag)
187/* The key is in a named module (primary or partition). */
188#define BINDING_VECTOR_PENDING_IS_PARTITION_P(NODE) \
189 (BINDING_VECTOR_CHECK (NODE)->base.private_flag)
f44b0c8e 190
00e8de68
GDR
191extern tree identifier_type_value (tree);
192extern void set_identifier_type_value (tree, tree);
55fec44d 193extern void push_binding (tree, tree, cp_binding_level*);
9c82d7b6 194extern void pop_local_binding (tree, tree);
9485d254 195extern void pop_bindings_and_leave_scope (void);
5a167978
GDR
196extern tree constructor_name (tree);
197extern bool constructor_name_p (tree, tree);
aed81407 198\f
1ec57cf0 199/* The kinds of scopes we recognize. */
a79683d5 200enum scope_kind {
1ec57cf0
GDR
201 sk_block = 0, /* An ordinary block scope. This enumerator must
202 have the value zero because "cp_binding_level"
203 is initialized by using "memset" to set the
204 contents to zero, and the default scope kind
205 is "sk_block". */
0cbd7506 206 sk_cleanup, /* A scope for (pseudo-)scope for cleanup. It is
1799e5d5 207 pseudo in that it is transparent to name lookup
0cbd7506 208 activities. */
1ec57cf0 209 sk_try, /* A try-block. */
0cbd7506
MS
210 sk_catch, /* A catch-block. */
211 sk_for, /* The scope of the variable declared in a
17a9e380 212 init-statement. */
1eb2a14d
JJ
213 sk_cond, /* The scope of the variable declared in the condition
214 of an if or switch statement. */
1ec57cf0 215 sk_function_parms, /* The scope containing function parameters. */
0cbd7506 216 sk_class, /* The scope containing the members of a class. */
b4cf2e42 217 sk_scoped_enum, /* The scope containing the enumerators of a C++11
adf2edec 218 scoped enumeration. */
0cbd7506 219 sk_namespace, /* The scope containing the members of a
1ec57cf0
GDR
220 namespace, including the global scope. */
221 sk_template_parms, /* A scope for template parameters. */
1799e5d5 222 sk_template_spec, /* Like sk_template_parms, but for an explicit
1ec57cf0
GDR
223 specialization. Since, by definition, an
224 explicit specialization is introduced by
225 "template <>", this scope is always empty. */
b8fd7909 226 sk_transaction, /* A synchronized or atomic statement. */
3db45ab5 227 sk_omp /* An OpenMP structured block. */
a79683d5 228};
1ec57cf0 229
a79683d5 230struct GTY(()) cp_class_binding {
f256f612 231 cxx_binding *base;
89b578be
MM
232 /* The bound name. */
233 tree identifier;
a79683d5 234};
89b578be 235
1ec57cf0
GDR
236/* For each binding contour we allocate a binding_level structure
237 which records the names defined in that contour.
238 Contours include:
239 0) the global one
240 1) one for each function definition,
241 where internal declarations of the parameters appear.
242 2) one for each compound statement,
243 to record its declarations.
244
245 The current meaning of a name can be found by searching the levels
246 from the current one out to the global one.
247
248 Off to the side, may be the class_binding_level. This exists only
249 to catch class-local declarations. It is otherwise nonexistent.
250
251 Also there may be binding levels that catch cleanups that must be
252 run when exceptions occur. Thus, to see whether a name is bound in
253 the current scope, it is not enough to look in the
254 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
255 instead. */
256
d1b38208 257struct GTY(()) cp_binding_level {
2c140474
DN
258 /* A chain of _DECL nodes for all variables, constants, functions,
259 and typedef types. These are in the reverse of the order
260 supplied. There may be OVERLOADs on this list, too, but they
261 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
262 tree names;
263
3c9feefc
NS
264 /* Using directives. */
265 vec<tree, va_gc> *using_directives;
2c140474
DN
266
267 /* For the binding level corresponding to a class, the entities
268 declared in the class or its base classes. */
9771b263 269 vec<cp_class_binding, va_gc> *class_shadowed;
2c140474
DN
270
271 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
272 is used for all binding levels. The TREE_PURPOSE is the name of
273 the entity, the TREE_TYPE is the associated type. In addition
274 the TREE_VALUE is the IDENTIFIER_TYPE_VALUE before we entered
275 the class. */
276 tree type_shadowed;
277
2c140474
DN
278 /* For each level (except not the global one),
279 a chain of BLOCK nodes for all the levels
280 that were entered and exited one level down. */
281 tree blocks;
282
283 /* The entity (namespace, class, function) the scope of which this
284 binding contour corresponds to. Otherwise NULL. */
285 tree this_entity;
286
287 /* The binding level which this one is contained in (inherits from). */
288 cp_binding_level *level_chain;
289
2c140474
DN
290 /* STATEMENT_LIST for statements in this binding contour.
291 Only used at present for SK_CLEANUP temporary bindings. */
292 tree statement_list;
293
294 /* Binding depth at which this level began. */
295 int binding_depth;
296
297 /* The kind of scope that this object represents. However, a
298 SK_TEMPLATE_SPEC scope is represented with KIND set to
299 SK_TEMPLATE_PARMS and EXPLICIT_SPEC_P set to true. */
300 ENUM_BITFIELD (scope_kind) kind : 4;
301
302 /* True if this scope is an SK_TEMPLATE_SPEC scope. This field is
303 only valid if KIND == SK_TEMPLATE_PARMS. */
304 BOOL_BITFIELD explicit_spec_p : 1;
305
306 /* true means make a BLOCK for this level regardless of all else. */
307 unsigned keep : 1;
308
309 /* Nonzero if this level can safely have additional
310 cleanup-needing variables added to it. */
311 unsigned more_cleanups_ok : 1;
312 unsigned have_cleanups : 1;
313
5294e4c3
AB
314 /* Transient state set if this scope is of sk_class kind
315 and is in the process of defining 'this_entity'. Reset
316 on leaving the class definition to allow for the scope
317 to be subsequently re-used as a non-defining scope for
318 'this_entity'. */
319 unsigned defining_class_p : 1;
320
f968ef9b
JJ
321 /* true for SK_FUNCTION_PARMS of immediate functions. */
322 unsigned immediate_fn_ctx_p : 1;
323
324 /* 22 bits left to fill a 32-bit word. */
2c140474 325};
1ec57cf0
GDR
326
327/* The binding level currently in effect. */
328
329#define current_binding_level \
da7d88bf 330 (*(cfun && cp_function_chain && cp_function_chain->bindings \
1ec57cf0
GDR
331 ? &cp_function_chain->bindings \
332 : &scope_chain->bindings))
333
334/* The binding level of the current class, if any. */
335
336#define class_binding_level scope_chain->class_bindings
337
5f52c0e0
GDR
338/* True if SCOPE designates the global scope binding contour. */
339#define global_scope_p(SCOPE) \
340 ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
aed81407 341
2c140474 342extern cp_binding_level *leave_scope (void);
00e8de68 343extern bool kept_level_p (void);
c99c0026 344extern bool global_bindings_p (void);
056a17ee 345extern bool toplevel_bindings_p (void);
00e8de68 346extern bool namespace_bindings_p (void);
fdf03377 347extern bool local_bindings_p (void);
00e8de68
GDR
348extern bool template_parm_scope_p (void);
349extern scope_kind innermost_scope_kind (void);
2c140474 350extern cp_binding_level *begin_scope (scope_kind, tree);
00e8de68 351extern void print_binding_stack (void);
00e8de68
GDR
352extern void pop_everything (void);
353extern void keep_next_level (bool);
056a17ee 354extern bool is_ancestor (tree ancestor, tree descendant);
322763f5
NS
355extern bool is_nested_namespace (tree parent, tree descendant,
356 bool inline_only = false);
4514aa8c 357extern tree push_scope (tree);
5a167978 358extern void pop_scope (tree);
87c465f5
KL
359extern tree push_inner_scope (tree);
360extern void pop_inner_scope (tree, tree);
2c140474 361extern void push_binding_level (cp_binding_level *);
00e8de68 362\f
65567efa 363extern bool handle_namespace_attrs (tree, tree);
00e8de68
GDR
364extern void pushlevel_class (void);
365extern void poplevel_class (void);
0c5f6bbf
NS
366
367/* What kind of scopes name lookup looks in. An enum class so we
368 don't accidentally mix integers. */
369enum class LOOK_where
370{
371 BLOCK = 1 << 0, /* Consider block scopes. */
372 CLASS = 1 << 1, /* Consider class scopes. */
373 NAMESPACE = 1 << 2, /* Consider namespace scopes. */
374
375 ALL = BLOCK | CLASS | NAMESPACE,
376 BLOCK_NAMESPACE = BLOCK | NAMESPACE,
377 CLASS_NAMESPACE = CLASS | NAMESPACE,
378};
379constexpr LOOK_where operator| (LOOK_where a, LOOK_where b)
380{
381 return LOOK_where (unsigned (a) | unsigned (b));
382}
4c58a32f 383constexpr LOOK_where operator& (LOOK_where a, LOOK_where b)
0c5f6bbf 384{
4c58a32f 385 return LOOK_where (unsigned (a) & unsigned (b));
0c5f6bbf
NS
386}
387
4c58a32f
NS
388enum class LOOK_want
389{
390 NORMAL = 0, /* Normal lookup -- non-types can hide implicit types. */
391 TYPE = 1 << 1, /* We only want TYPE_DECLS. */
392 NAMESPACE = 1 << 2, /* We only want NAMESPACE_DECLS. */
0c5f6bbf 393
db1c2a89
NS
394 HIDDEN_FRIEND = 1 << 3, /* See hidden friends. */
395 HIDDEN_LAMBDA = 1 << 4, /* See lambda-ignored entities. */
396
4c58a32f
NS
397 TYPE_NAMESPACE = TYPE | NAMESPACE, /* Either NAMESPACE or TYPE. */
398};
399constexpr LOOK_want operator| (LOOK_want a, LOOK_want b)
400{
401 return LOOK_want (unsigned (a) | unsigned (b));
402}
403constexpr LOOK_want operator& (LOOK_want a, LOOK_want b)
404{
405 return LOOK_want (unsigned (a) & unsigned (b));
406}
0c5f6bbf 407
f00008b4
NS
408extern tree lookup_name (tree, LOOK_where, LOOK_want = LOOK_want::NORMAL);
409/* Also declared in c-family/c-common.h. */
410extern tree lookup_name (tree name);
411inline tree lookup_name (tree name, LOOK_want want)
412{
413 return lookup_name (name, LOOK_where::ALL, want);
414}
415
00aaae03
NS
416enum class TAG_how
417{
418 CURRENT_ONLY = 0, // Look and insert only in current scope
419
420 GLOBAL = 1, // Unqualified lookup, innermost-non-class insertion
421
422 INNERMOST_NON_CLASS = 2, // Look and insert only into
423 // innermost-non-class
424
425 HIDDEN_FRIEND = 3, // As INNERMOST_NON_CLASS, but hide it
426};
427
428extern tree lookup_elaborated_type (tree, TAG_how);
06aa5490 429extern tree get_namespace_binding (tree ns, tree id);
87e3d7cf
NS
430extern void set_global_binding (tree decl);
431inline tree get_global_binding (tree id)
432{
433 return get_namespace_binding (NULL_TREE, id);
434}
4c58a32f
NS
435extern tree lookup_qualified_name (tree scope, tree name,
436 LOOK_want = LOOK_want::NORMAL,
db1c2a89 437 bool = true);
4c58a32f
NS
438extern tree lookup_qualified_name (tree scope, const char *name,
439 LOOK_want = LOOK_want::NORMAL,
db1c2a89 440 bool = true);
00e8de68 441extern bool pushdecl_class_level (tree);
c74e6f7c 442extern tree pushdecl_namespace_level (tree, bool hiding = false);
00e8de68 443extern bool push_class_level_binding (tree, tree);
9c82d7b6 444extern tree get_local_decls ();
67e18edb 445extern int function_parm_depth (void);
00e8de68 446extern tree cp_namespace_decls (tree);
5a167978 447extern void set_decl_namespace (tree, tree, bool);
5a167978
GDR
448extern void push_decl_namespace (tree);
449extern void pop_decl_namespace (void);
450extern void do_namespace_alias (tree, tree);
1d786913 451extern tree do_class_using_decl (tree, tree);
f35a733d 452extern tree lookup_arg_dependent (tree, tree, vec<tree, va_gc> *);
35129fd3 453extern tree search_anon_aggr (tree, tree, bool = false);
9a54a0d9
NS
454extern tree get_class_binding_direct (tree, tree, bool want_type = false);
455extern tree get_class_binding (tree, tree, bool want_type = false);
10b5c982
NS
456extern tree *find_member_slot (tree klass, tree name);
457extern tree *add_member_slot (tree klass, tree name);
783dc739 458extern void resort_type_member_vec (void *, void *,
fe920c2d 459 gt_pointer_operator, void *);
79c1b9fb 460extern vec<tree, va_gc> *set_class_bindings (tree, int extra = 0);
41970ff1 461extern void insert_late_enum_def_bindings (tree, tree);
90ea9897
MM
462extern tree innermost_non_namespace_value (tree);
463extern cxx_binding *outer_binding (tree, cxx_binding *, bool);
98ed9dae 464extern void cp_emit_debug_info_for_using (tree, tree);
c003e212 465
692af872
NS
466extern void finish_nonmember_using_decl (tree scope, tree name);
467extern void finish_using_directive (tree target, tree attribs);
c74e6f7c 468extern tree pushdecl (tree, bool hiding = false);
5256a7f5 469extern tree pushdecl_outermost_localscope (tree);
c74e6f7c 470extern tree pushdecl_top_level (tree);
2e3757e7 471extern tree pushdecl_top_level_and_finish (tree, tree);
00aaae03 472extern tree pushtag (tree, tree, TAG_how = TAG_how::CURRENT_ONLY);
3a77e7cc 473extern int push_namespace (tree, bool make_inline = false);
056a17ee
NS
474extern void pop_namespace (void);
475extern void push_nested_namespace (tree);
476extern void pop_nested_namespace (tree);
477extern void push_to_top_level (void);
478extern void pop_from_top_level (void);
bddee796
JM
479extern void maybe_save_operator_binding (tree);
480extern void push_operator_bindings (void);
481extern void discard_operator_bindings (tree);
641da50a 482
c0979d8f
NS
483/* Lower level interface for modules. */
484extern void add_mergeable_namespace_entity (tree *slot, tree decl);
485
aed81407 486#endif /* GCC_CP_NAME_LOOKUP_H */