1/* Declarations for C++ name lookup routines.
2   Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
3   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
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
18along with GCC; see the file COPYING.  If not, write to
19the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA.  */
21
22#ifndef GCC_CP_NAME_LOOKUP_H
23#define GCC_CP_NAME_LOOKUP_H
24
25#include "c-common.h"
26
27/* The type of dictionary used to map names to types declared at
28   a given scope.  */
29typedef struct binding_table_s *binding_table;
30typedef struct binding_entry_s *binding_entry;
31
32/* The type of a routine repeatedly called by binding_table_foreach.  */
33typedef void (*bt_foreach_proc) (binding_entry, void *);
34
35struct binding_entry_s GTY(())
36{
37  binding_entry chain;
38  tree name;
39  tree type;
40};
41
42/* These macros indicate the initial chains count for binding_table.  */
43#define SCOPE_DEFAULT_HT_SIZE		(1 << 3)
44#define CLASS_SCOPE_HT_SIZE		(1 << 3)
45#define NAMESPACE_ORDINARY_HT_SIZE	(1 << 5)
46#define NAMESPACE_STD_HT_SIZE		(1 << 8)
47#define GLOBAL_SCOPE_HT_SIZE		(1 << 8)
48
49extern void binding_table_foreach (binding_table, bt_foreach_proc, void *);
50extern binding_entry binding_table_find (binding_table, tree);
51
52/* Datatype that represents binding established by a declaration between
53   a name and a C++ entity.  */
54typedef struct cxx_binding cxx_binding;
55
56/* The datatype used to implement C++ scope.  */
57typedef struct cp_binding_level cxx_scope;
58
59/* Nonzero if this binding is for a local scope, as opposed to a class
60   or namespace scope.  */
61#define LOCAL_BINDING_P(NODE) ((NODE)->is_local)
62
63/* True if NODE->value is from a base class of the class which is
64   currently being defined.  */
65#define INHERITED_VALUE_BINDING_P(NODE) ((NODE)->value_is_inherited)
66
67struct cxx_binding GTY(())
68{
69  /* Link to chain together various bindings for this name.  */
70  cxx_binding *previous;
71  /* The non-type entity this name is bound to.  */
72  tree value;
73  /* The type entity this name is bound to.  */
74  tree type;
75  /* The scope at which this binding was made.  */
76  cxx_scope *scope;
77  unsigned value_is_inherited : 1;
78  unsigned is_local : 1;
79  /* APPLE LOCAL blocks 6040305 (ch) */
80  unsigned declared_in_block : 1;
81};
82
83/* Datatype used to temporarily save C++ bindings (for implicit
84   instantiations purposes and like).  Implemented in decl.c.  */
85typedef struct cxx_saved_binding GTY(())
86{
87  /* The name of the current binding.  */
88  tree identifier;
89  /* The binding we're saving.  */
90  cxx_binding *binding;
91  tree real_type_value;
92} cxx_saved_binding;
93
94DEF_VEC_O(cxx_saved_binding);
95DEF_VEC_ALLOC_O(cxx_saved_binding,gc);
96
97extern tree identifier_type_value (tree);
98extern void set_identifier_type_value (tree, tree);
99extern void pop_binding (tree, tree);
100extern tree constructor_name (tree);
101extern bool constructor_name_p (tree, tree);
102
103/* The kinds of scopes we recognize.  */
104typedef enum scope_kind {
105  sk_block = 0,      /* An ordinary block scope.  This enumerator must
106			have the value zero because "cp_binding_level"
107			is initialized by using "memset" to set the
108			contents to zero, and the default scope kind
109			is "sk_block".  */
110  sk_cleanup,	     /* A scope for (pseudo-)scope for cleanup.  It is
111			pseudo in that it is transparent to name lookup
112			activities.  */
113  sk_try,	     /* A try-block.  */
114  sk_catch,	     /* A catch-block.  */
115  sk_for,	     /* The scope of the variable declared in a
116			for-init-statement.  */
117  sk_function_parms, /* The scope containing function parameters.  */
118  sk_class,	     /* The scope containing the members of a class.  */
119  sk_namespace,	     /* The scope containing the members of a
120			namespace, including the global scope.  */
121  sk_template_parms, /* A scope for template parameters.  */
122  sk_template_spec,  /* Like sk_template_parms, but for an explicit
123			specialization.  Since, by definition, an
124			explicit specialization is introduced by
125			"template <>", this scope is always empty.  */
126  sk_omp	     /* An OpenMP structured block.  */
127} scope_kind;
128
129/* The scope where the class/struct/union/enum tag applies.  */
130typedef enum tag_scope {
131  ts_current = 0,	/* Current scope only.  This is for the
132			     class-key identifier;
133			   case mentioned in [basic.lookup.elab]/2,
134			   or the class/enum definition
135			     class-key identifier { ... };  */
136  ts_global = 1,	/* All scopes.  This is the 3.4.1
137			   [basic.lookup.unqual] lookup mentioned
138			   in [basic.lookup.elab]/2.  */
139  ts_within_enclosing_non_class = 2	/* Search within enclosing non-class
140					   only, for friend class lookup
141					   according to [namespace.memdef]/3
142					   and [class.friend]/9.  */
143} tag_scope;
144
145typedef struct cp_class_binding GTY(())
146{
147  cxx_binding *base;
148  /* The bound name.  */
149  tree identifier;
150} cp_class_binding;
151
152DEF_VEC_O(cp_class_binding);
153DEF_VEC_ALLOC_O(cp_class_binding,gc);
154
155/* For each binding contour we allocate a binding_level structure
156   which records the names defined in that contour.
157   Contours include:
158    0) the global one
159    1) one for each function definition,
160       where internal declarations of the parameters appear.
161    2) one for each compound statement,
162       to record its declarations.
163
164   The current meaning of a name can be found by searching the levels
165   from the current one out to the global one.
166
167   Off to the side, may be the class_binding_level.  This exists only
168   to catch class-local declarations.  It is otherwise nonexistent.
169
170   Also there may be binding levels that catch cleanups that must be
171   run when exceptions occur.  Thus, to see whether a name is bound in
172   the current scope, it is not enough to look in the
173   CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
174   instead.  */
175
176/* Note that the information in the `names' component of the global contour
177   is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
178
179struct cp_binding_level GTY(())
180  {
181    /* A chain of _DECL nodes for all variables, constants, functions,
182       and typedef types.  These are in the reverse of the order
183       supplied.  There may be OVERLOADs on this list, too, but they
184       are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
185    tree names;
186
187    /* Count of elements in names chain.  */
188    size_t names_size;
189
190    /* A chain of NAMESPACE_DECL nodes.  */
191    tree namespaces;
192
193    /* An array of static functions and variables (for namespaces only) */
194    VEC(tree,gc) *static_decls;
195
196    /* A chain of VTABLE_DECL nodes.  */
197    tree vtables;
198
199    /* A list of USING_DECL nodes.  */
200    tree usings;
201
202    /* A list of used namespaces. PURPOSE is the namespace,
203       VALUE the common ancestor with this binding_level's namespace.  */
204    tree using_directives;
205
206    /* For the binding level corresponding to a class, the entities
207       declared in the class or its base classes.  */
208    VEC(cp_class_binding,gc) *class_shadowed;
209
210    /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
211       is used for all binding levels. The TREE_PURPOSE is the name of
212       the entity, the TREE_TYPE is the associated type.  In addition
213       the TREE_VALUE is the IDENTIFIER_TYPE_VALUE before we entered
214       the class.  */
215    tree type_shadowed;
216
217    /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
218       label in this scope.  The TREE_PURPOSE is the previous value of
219       the IDENTIFIER_LABEL VALUE.  */
220    tree shadowed_labels;
221
222    /* For each level (except not the global one),
223       a chain of BLOCK nodes for all the levels
224       that were entered and exited one level down.  */
225    tree blocks;
226
227    /* The entity (namespace, class, function) the scope of which this
228       binding contour corresponds to.  Otherwise NULL.  */
229    tree this_entity;
230
231    /* The binding level which this one is contained in (inherits from).  */
232    struct cp_binding_level *level_chain;
233
234    /* List of VAR_DECLS saved from a previous for statement.
235       These would be dead in ISO-conforming code, but might
236       be referenced in ARM-era code.  These are stored in a
237       TREE_LIST; the TREE_VALUE is the actual declaration.  */
238    tree dead_vars_from_for;
239
240    /* STATEMENT_LIST for statements in this binding contour.
241       Only used at present for SK_CLEANUP temporary bindings.  */
242    tree statement_list;
243
244    /* Binding depth at which this level began.  */
245    int binding_depth;
246
247    /* The kind of scope that this object represents.  However, a
248       SK_TEMPLATE_SPEC scope is represented with KIND set to
249       SK_TEMPLATE_PARMS and EXPLICIT_SPEC_P set to true.  */
250    ENUM_BITFIELD (scope_kind) kind : 4;
251
252    /* True if this scope is an SK_TEMPLATE_SPEC scope.  This field is
253       only valid if KIND == SK_TEMPLATE_PARMS.  */
254    BOOL_BITFIELD explicit_spec_p : 1;
255
256    /* true means make a BLOCK for this level regardless of all else.  */
257    unsigned keep : 1;
258
259    /* Nonzero if this level can safely have additional
260       cleanup-needing variables added to it.  */
261    unsigned more_cleanups_ok : 1;
262    unsigned have_cleanups : 1;
263
264    /* Nonzero if this level has associated visibility which we should pop
265       when leaving the scope. */
266    unsigned has_visibility : 1;
267
268    /* 23 bits left to fill a 32-bit word.  */
269  };
270
271/* The binding level currently in effect.  */
272
273#define current_binding_level			\
274  (*(cfun && cp_function_chain->bindings	\
275   ? &cp_function_chain->bindings		\
276   : &scope_chain->bindings))
277
278/* The binding level of the current class, if any.  */
279
280#define class_binding_level scope_chain->class_bindings
281
282/* The tree node representing the global scope.  */
283extern GTY(()) tree global_namespace;
284extern GTY(()) tree global_scope_name;
285
286/* Indicates that there is a type value in some namespace, although
287   that is not necessarily in scope at the moment.  */
288
289extern GTY(()) tree global_type_node;
290
291/* True if SCOPE designates the global scope binding contour.  */
292#define global_scope_p(SCOPE) \
293  ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
294
295extern cxx_scope *leave_scope (void);
296extern bool kept_level_p (void);
297extern int global_bindings_p (void);
298extern bool toplevel_bindings_p	(void);
299extern bool namespace_bindings_p (void);
300extern bool template_parm_scope_p (void);
301extern scope_kind innermost_scope_kind (void);
302extern cxx_scope *begin_scope (scope_kind, tree);
303extern void print_binding_stack	(void);
304extern void push_to_top_level (void);
305extern void pop_from_top_level (void);
306extern void pop_everything (void);
307extern void keep_next_level (bool);
308extern bool is_ancestor (tree, tree);
309extern tree push_scope (tree);
310extern void pop_scope (tree);
311extern tree push_inner_scope (tree);
312extern void pop_inner_scope (tree, tree);
313extern void push_binding_level (struct cp_binding_level *);
314
315extern void push_namespace (tree);
316extern void push_namespace_with_attribs (tree, tree);
317extern void pop_namespace (void);
318extern void push_nested_namespace (tree);
319extern void pop_nested_namespace (tree);
320extern void pushlevel_class (void);
321extern void poplevel_class (void);
322extern tree pushdecl_with_scope (tree, cxx_scope *, bool);
323extern tree lookup_name_prefer_type (tree, int);
324extern tree lookup_name_real (tree, int, int, bool, int, int);
325extern tree lookup_type_scope (tree, tag_scope);
326extern tree namespace_binding (tree, tree);
327extern void set_namespace_binding (tree, tree, tree);
328extern bool hidden_name_p (tree);
329extern tree remove_hidden_names (tree);
330extern tree lookup_qualified_name (tree, tree, bool, bool);
331extern tree lookup_name_nonclass (tree);
332extern tree lookup_function_nonclass (tree, tree, bool);
333extern void push_local_binding (tree, tree, int);
334extern bool pushdecl_class_level (tree);
335extern tree pushdecl_namespace_level (tree, bool);
336extern bool push_class_level_binding (tree, tree);
337extern tree getdecls (void);
338extern tree cp_namespace_decls (tree);
339extern void set_decl_namespace (tree, tree, bool);
340extern void push_decl_namespace (tree);
341extern void pop_decl_namespace (void);
342extern void do_namespace_alias (tree, tree);
343extern void do_toplevel_using_decl (tree, tree, tree);
344extern void do_local_using_decl (tree, tree, tree);
345extern tree do_class_using_decl (tree, tree);
346extern void do_using_directive (tree);
347extern tree lookup_arg_dependent (tree, tree, tree);
348extern bool is_associated_namespace (tree, tree);
349extern void parse_using_directive (tree, tree);
350extern tree innermost_non_namespace_value (tree);
351extern cxx_binding *outer_binding (tree, cxx_binding *, bool);
352extern void cp_emit_debug_info_for_using (tree, tree);
353
354/* Set *DECL to the (non-hidden) declaration for ID at global scope,
355   if present and return true; otherwise return false.  */
356
357static inline bool
358get_global_value_if_present (tree id, tree *decl)
359{
360  tree global_value = namespace_binding (id, global_namespace);
361  if (global_value)
362    *decl = global_value;
363  return global_value != NULL;
364}
365
366/* True is the binding of IDENTIFIER at global scope names a type.  */
367
368static inline bool
369is_typename_at_global_scope (tree id)
370{
371  tree global_value = namespace_binding (id, global_namespace);
372
373  return global_value && TREE_CODE (global_value) == TYPE_DECL;
374}
375
376#endif /* GCC_CP_NAME_LOOKUP_H */
377