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