1132718Skan/* Declarations for C++ name lookup routines.
2169689Skan   Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
3132718Skan   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4132718Skan
5132718SkanThis file is part of GCC.
6132718Skan
7132718SkanGCC is free software; you can redistribute it and/or modify
8132718Skanit under the terms of the GNU General Public License as published by
9132718Skanthe Free Software Foundation; either version 2, or (at your option)
10132718Skanany later version.
11132718Skan
12132718SkanGCC is distributed in the hope that it will be useful,
13132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
14132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132718SkanGNU General Public License for more details.
16132718Skan
17132718SkanYou should have received a copy of the GNU General Public License
18132718Skanalong with GCC; see the file COPYING.  If not, write to
19169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
20169689SkanBoston, MA 02110-1301, USA.  */
21132718Skan
22132718Skan#ifndef GCC_CP_NAME_LOOKUP_H
23132718Skan#define GCC_CP_NAME_LOOKUP_H
24132718Skan
25132718Skan#include "c-common.h"
26132718Skan
27132718Skan/* The type of dictionary used to map names to types declared at
28132718Skan   a given scope.  */
29132718Skantypedef struct binding_table_s *binding_table;
30132718Skantypedef struct binding_entry_s *binding_entry;
31132718Skan
32132718Skan/* The type of a routine repeatedly called by binding_table_foreach.  */
33132718Skantypedef void (*bt_foreach_proc) (binding_entry, void *);
34132718Skan
35132718Skanstruct binding_entry_s GTY(())
36132718Skan{
37132718Skan  binding_entry chain;
38132718Skan  tree name;
39132718Skan  tree type;
40132718Skan};
41132718Skan
42132718Skan/* These macros indicate the initial chains count for binding_table.  */
43169689Skan#define SCOPE_DEFAULT_HT_SIZE		(1 << 3)
44169689Skan#define CLASS_SCOPE_HT_SIZE		(1 << 3)
45169689Skan#define NAMESPACE_ORDINARY_HT_SIZE	(1 << 5)
46169689Skan#define NAMESPACE_STD_HT_SIZE		(1 << 8)
47169689Skan#define GLOBAL_SCOPE_HT_SIZE		(1 << 8)
48132718Skan
49132718Skanextern void binding_table_foreach (binding_table, bt_foreach_proc, void *);
50132718Skanextern binding_entry binding_table_find (binding_table, tree);
51132718Skan
52132718Skan/* Datatype that represents binding established by a declaration between
53132718Skan   a name and a C++ entity.  */
54132718Skantypedef struct cxx_binding cxx_binding;
55132718Skan
56132718Skan/* The datatype used to implement C++ scope.  */
57132718Skantypedef struct cp_binding_level cxx_scope;
58132718Skan
59132718Skan/* Nonzero if this binding is for a local scope, as opposed to a class
60132718Skan   or namespace scope.  */
61132718Skan#define LOCAL_BINDING_P(NODE) ((NODE)->is_local)
62132718Skan
63132718Skan/* True if NODE->value is from a base class of the class which is
64132718Skan   currently being defined.  */
65132718Skan#define INHERITED_VALUE_BINDING_P(NODE) ((NODE)->value_is_inherited)
66132718Skan
67132718Skanstruct cxx_binding GTY(())
68132718Skan{
69132718Skan  /* Link to chain together various bindings for this name.  */
70132718Skan  cxx_binding *previous;
71132718Skan  /* The non-type entity this name is bound to.  */
72132718Skan  tree value;
73132718Skan  /* The type entity this name is bound to.  */
74132718Skan  tree type;
75132718Skan  /* The scope at which this binding was made.  */
76132718Skan  cxx_scope *scope;
77132718Skan  unsigned value_is_inherited : 1;
78132718Skan  unsigned is_local : 1;
79260311Spfg  /* APPLE LOCAL blocks 6040305 (ch) */
80260311Spfg  unsigned declared_in_block : 1;
81132718Skan};
82132718Skan
83169689Skan/* Datatype used to temporarily save C++ bindings (for implicit
84169689Skan   instantiations purposes and like).  Implemented in decl.c.  */
85169689Skantypedef struct cxx_saved_binding GTY(())
86169689Skan{
87169689Skan  /* The name of the current binding.  */
88169689Skan  tree identifier;
89169689Skan  /* The binding we're saving.  */
90169689Skan  cxx_binding *binding;
91169689Skan  tree real_type_value;
92169689Skan} cxx_saved_binding;
93169689Skan
94169689SkanDEF_VEC_O(cxx_saved_binding);
95169689SkanDEF_VEC_ALLOC_O(cxx_saved_binding,gc);
96169689Skan
97132718Skanextern tree identifier_type_value (tree);
98132718Skanextern void set_identifier_type_value (tree, tree);
99132718Skanextern void pop_binding (tree, tree);
100132718Skanextern tree constructor_name (tree);
101132718Skanextern bool constructor_name_p (tree, tree);
102132718Skan
103132718Skan/* The kinds of scopes we recognize.  */
104132718Skantypedef enum scope_kind {
105132718Skan  sk_block = 0,      /* An ordinary block scope.  This enumerator must
106132718Skan			have the value zero because "cp_binding_level"
107132718Skan			is initialized by using "memset" to set the
108132718Skan			contents to zero, and the default scope kind
109132718Skan			is "sk_block".  */
110169689Skan  sk_cleanup,	     /* A scope for (pseudo-)scope for cleanup.  It is
111169689Skan			pseudo in that it is transparent to name lookup
112169689Skan			activities.  */
113132718Skan  sk_try,	     /* A try-block.  */
114169689Skan  sk_catch,	     /* A catch-block.  */
115169689Skan  sk_for,	     /* The scope of the variable declared in a
116132718Skan			for-init-statement.  */
117132718Skan  sk_function_parms, /* The scope containing function parameters.  */
118169689Skan  sk_class,	     /* The scope containing the members of a class.  */
119169689Skan  sk_namespace,	     /* The scope containing the members of a
120132718Skan			namespace, including the global scope.  */
121132718Skan  sk_template_parms, /* A scope for template parameters.  */
122169689Skan  sk_template_spec,  /* Like sk_template_parms, but for an explicit
123132718Skan			specialization.  Since, by definition, an
124132718Skan			explicit specialization is introduced by
125132718Skan			"template <>", this scope is always empty.  */
126169689Skan  sk_omp	     /* An OpenMP structured block.  */
127132718Skan} scope_kind;
128132718Skan
129169689Skan/* The scope where the class/struct/union/enum tag applies.  */
130169689Skantypedef enum tag_scope {
131169689Skan  ts_current = 0,	/* Current scope only.  This is for the
132169689Skan			     class-key identifier;
133169689Skan			   case mentioned in [basic.lookup.elab]/2,
134169689Skan			   or the class/enum definition
135169689Skan			     class-key identifier { ... };  */
136169689Skan  ts_global = 1,	/* All scopes.  This is the 3.4.1
137169689Skan			   [basic.lookup.unqual] lookup mentioned
138169689Skan			   in [basic.lookup.elab]/2.  */
139169689Skan  ts_within_enclosing_non_class = 2	/* Search within enclosing non-class
140169689Skan					   only, for friend class lookup
141169689Skan					   according to [namespace.memdef]/3
142169689Skan					   and [class.friend]/9.  */
143169689Skan} tag_scope;
144169689Skan
145169689Skantypedef struct cp_class_binding GTY(())
146169689Skan{
147275477Sdim  cxx_binding *base;
148169689Skan  /* The bound name.  */
149169689Skan  tree identifier;
150169689Skan} cp_class_binding;
151169689Skan
152169689SkanDEF_VEC_O(cp_class_binding);
153169689SkanDEF_VEC_ALLOC_O(cp_class_binding,gc);
154169689Skan
155132718Skan/* For each binding contour we allocate a binding_level structure
156132718Skan   which records the names defined in that contour.
157132718Skan   Contours include:
158132718Skan    0) the global one
159132718Skan    1) one for each function definition,
160132718Skan       where internal declarations of the parameters appear.
161132718Skan    2) one for each compound statement,
162132718Skan       to record its declarations.
163132718Skan
164132718Skan   The current meaning of a name can be found by searching the levels
165132718Skan   from the current one out to the global one.
166132718Skan
167132718Skan   Off to the side, may be the class_binding_level.  This exists only
168132718Skan   to catch class-local declarations.  It is otherwise nonexistent.
169132718Skan
170132718Skan   Also there may be binding levels that catch cleanups that must be
171132718Skan   run when exceptions occur.  Thus, to see whether a name is bound in
172132718Skan   the current scope, it is not enough to look in the
173132718Skan   CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
174132718Skan   instead.  */
175132718Skan
176132718Skan/* Note that the information in the `names' component of the global contour
177132718Skan   is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
178132718Skan
179132718Skanstruct cp_binding_level GTY(())
180132718Skan  {
181132718Skan    /* A chain of _DECL nodes for all variables, constants, functions,
182132718Skan       and typedef types.  These are in the reverse of the order
183132718Skan       supplied.  There may be OVERLOADs on this list, too, but they
184132718Skan       are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
185132718Skan    tree names;
186132718Skan
187132718Skan    /* Count of elements in names chain.  */
188132718Skan    size_t names_size;
189132718Skan
190132718Skan    /* A chain of NAMESPACE_DECL nodes.  */
191132718Skan    tree namespaces;
192132718Skan
193132718Skan    /* An array of static functions and variables (for namespaces only) */
194169689Skan    VEC(tree,gc) *static_decls;
195132718Skan
196132718Skan    /* A chain of VTABLE_DECL nodes.  */
197169689Skan    tree vtables;
198132718Skan
199132718Skan    /* A list of USING_DECL nodes.  */
200132718Skan    tree usings;
201132718Skan
202132718Skan    /* A list of used namespaces. PURPOSE is the namespace,
203132718Skan       VALUE the common ancestor with this binding_level's namespace.  */
204132718Skan    tree using_directives;
205132718Skan
206169689Skan    /* For the binding level corresponding to a class, the entities
207169689Skan       declared in the class or its base classes.  */
208169689Skan    VEC(cp_class_binding,gc) *class_shadowed;
209132718Skan
210132718Skan    /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
211169689Skan       is used for all binding levels. The TREE_PURPOSE is the name of
212169689Skan       the entity, the TREE_TYPE is the associated type.  In addition
213169689Skan       the TREE_VALUE is the IDENTIFIER_TYPE_VALUE before we entered
214169689Skan       the class.  */
215132718Skan    tree type_shadowed;
216132718Skan
217132718Skan    /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
218132718Skan       label in this scope.  The TREE_PURPOSE is the previous value of
219132718Skan       the IDENTIFIER_LABEL VALUE.  */
220132718Skan    tree shadowed_labels;
221132718Skan
222132718Skan    /* For each level (except not the global one),
223132718Skan       a chain of BLOCK nodes for all the levels
224132718Skan       that were entered and exited one level down.  */
225132718Skan    tree blocks;
226132718Skan
227132718Skan    /* The entity (namespace, class, function) the scope of which this
228132718Skan       binding contour corresponds to.  Otherwise NULL.  */
229132718Skan    tree this_entity;
230132718Skan
231132718Skan    /* The binding level which this one is contained in (inherits from).  */
232132718Skan    struct cp_binding_level *level_chain;
233132718Skan
234132718Skan    /* List of VAR_DECLS saved from a previous for statement.
235132718Skan       These would be dead in ISO-conforming code, but might
236132718Skan       be referenced in ARM-era code.  These are stored in a
237132718Skan       TREE_LIST; the TREE_VALUE is the actual declaration.  */
238132718Skan    tree dead_vars_from_for;
239132718Skan
240169689Skan    /* STATEMENT_LIST for statements in this binding contour.
241169689Skan       Only used at present for SK_CLEANUP temporary bindings.  */
242169689Skan    tree statement_list;
243169689Skan
244132718Skan    /* Binding depth at which this level began.  */
245132718Skan    int binding_depth;
246132718Skan
247132718Skan    /* The kind of scope that this object represents.  However, a
248132718Skan       SK_TEMPLATE_SPEC scope is represented with KIND set to
249169689Skan       SK_TEMPLATE_PARMS and EXPLICIT_SPEC_P set to true.  */
250132718Skan    ENUM_BITFIELD (scope_kind) kind : 4;
251132718Skan
252132718Skan    /* True if this scope is an SK_TEMPLATE_SPEC scope.  This field is
253132718Skan       only valid if KIND == SK_TEMPLATE_PARMS.  */
254132718Skan    BOOL_BITFIELD explicit_spec_p : 1;
255132718Skan
256132718Skan    /* true means make a BLOCK for this level regardless of all else.  */
257132718Skan    unsigned keep : 1;
258132718Skan
259132718Skan    /* Nonzero if this level can safely have additional
260132718Skan       cleanup-needing variables added to it.  */
261132718Skan    unsigned more_cleanups_ok : 1;
262132718Skan    unsigned have_cleanups : 1;
263132718Skan
264169689Skan    /* Nonzero if this level has associated visibility which we should pop
265169689Skan       when leaving the scope. */
266169689Skan    unsigned has_visibility : 1;
267169689Skan
268169689Skan    /* 23 bits left to fill a 32-bit word.  */
269132718Skan  };
270132718Skan
271132718Skan/* The binding level currently in effect.  */
272132718Skan
273132718Skan#define current_binding_level			\
274132718Skan  (*(cfun && cp_function_chain->bindings	\
275132718Skan   ? &cp_function_chain->bindings		\
276132718Skan   : &scope_chain->bindings))
277132718Skan
278132718Skan/* The binding level of the current class, if any.  */
279132718Skan
280132718Skan#define class_binding_level scope_chain->class_bindings
281132718Skan
282132718Skan/* The tree node representing the global scope.  */
283132718Skanextern GTY(()) tree global_namespace;
284132718Skanextern GTY(()) tree global_scope_name;
285132718Skan
286132718Skan/* Indicates that there is a type value in some namespace, although
287132718Skan   that is not necessarily in scope at the moment.  */
288132718Skan
289132718Skanextern GTY(()) tree global_type_node;
290132718Skan
291132718Skan/* True if SCOPE designates the global scope binding contour.  */
292132718Skan#define global_scope_p(SCOPE) \
293132718Skan  ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
294132718Skan
295132718Skanextern cxx_scope *leave_scope (void);
296132718Skanextern bool kept_level_p (void);
297132718Skanextern int global_bindings_p (void);
298132718Skanextern bool toplevel_bindings_p	(void);
299132718Skanextern bool namespace_bindings_p (void);
300132718Skanextern bool template_parm_scope_p (void);
301132718Skanextern scope_kind innermost_scope_kind (void);
302132718Skanextern cxx_scope *begin_scope (scope_kind, tree);
303132718Skanextern void print_binding_stack	(void);
304132718Skanextern void push_to_top_level (void);
305132718Skanextern void pop_from_top_level (void);
306132718Skanextern void pop_everything (void);
307132718Skanextern void keep_next_level (bool);
308132718Skanextern bool is_ancestor (tree, tree);
309169689Skanextern tree push_scope (tree);
310132718Skanextern void pop_scope (tree);
311169689Skanextern tree push_inner_scope (tree);
312169689Skanextern void pop_inner_scope (tree, tree);
313169689Skanextern void push_binding_level (struct cp_binding_level *);
314132718Skan
315132718Skanextern void push_namespace (tree);
316169689Skanextern void push_namespace_with_attribs (tree, tree);
317132718Skanextern void pop_namespace (void);
318132718Skanextern void push_nested_namespace (tree);
319132718Skanextern void pop_nested_namespace (tree);
320132718Skanextern void pushlevel_class (void);
321132718Skanextern void poplevel_class (void);
322169689Skanextern tree pushdecl_with_scope (tree, cxx_scope *, bool);
323169689Skanextern tree lookup_name_prefer_type (tree, int);
324169689Skanextern tree lookup_name_real (tree, int, int, bool, int, int);
325169689Skanextern tree lookup_type_scope (tree, tag_scope);
326132718Skanextern tree namespace_binding (tree, tree);
327132718Skanextern void set_namespace_binding (tree, tree, tree);
328169689Skanextern bool hidden_name_p (tree);
329169689Skanextern tree remove_hidden_names (tree);
330132718Skanextern tree lookup_qualified_name (tree, tree, bool, bool);
331132718Skanextern tree lookup_name_nonclass (tree);
332169689Skanextern tree lookup_function_nonclass (tree, tree, bool);
333161651Skanextern void push_local_binding (tree, tree, int);
334132718Skanextern bool pushdecl_class_level (tree);
335169689Skanextern tree pushdecl_namespace_level (tree, bool);
336132718Skanextern bool push_class_level_binding (tree, tree);
337132718Skanextern tree getdecls (void);
338132718Skanextern tree cp_namespace_decls (tree);
339132718Skanextern void set_decl_namespace (tree, tree, bool);
340132718Skanextern void push_decl_namespace (tree);
341132718Skanextern void pop_decl_namespace (void);
342132718Skanextern void do_namespace_alias (tree, tree);
343132718Skanextern void do_toplevel_using_decl (tree, tree, tree);
344132718Skanextern void do_local_using_decl (tree, tree, tree);
345169689Skanextern tree do_class_using_decl (tree, tree);
346132718Skanextern void do_using_directive (tree);
347132718Skanextern tree lookup_arg_dependent (tree, tree, tree);
348132718Skanextern bool is_associated_namespace (tree, tree);
349132718Skanextern void parse_using_directive (tree, tree);
350169689Skanextern tree innermost_non_namespace_value (tree);
351169689Skanextern cxx_binding *outer_binding (tree, cxx_binding *, bool);
352169689Skanextern void cp_emit_debug_info_for_using (tree, tree);
353132718Skan
354132718Skan/* Set *DECL to the (non-hidden) declaration for ID at global scope,
355132718Skan   if present and return true; otherwise return false.  */
356132718Skan
357132718Skanstatic inline bool
358132718Skanget_global_value_if_present (tree id, tree *decl)
359132718Skan{
360132718Skan  tree global_value = namespace_binding (id, global_namespace);
361132718Skan  if (global_value)
362132718Skan    *decl = global_value;
363132718Skan  return global_value != NULL;
364132718Skan}
365132718Skan
366132718Skan/* True is the binding of IDENTIFIER at global scope names a type.  */
367132718Skan
368132718Skanstatic inline bool
369132718Skanis_typename_at_global_scope (tree id)
370132718Skan{
371132718Skan  tree global_value = namespace_binding (id, global_namespace);
372132718Skan
373132718Skan  return global_value && TREE_CODE (global_value) == TYPE_DECL;
374132718Skan}
375132718Skan
376132718Skan#endif /* GCC_CP_NAME_LOOKUP_H */
377