2002-03-31 Segher Boessenkool <segher@koffie.nl>
[official-gcc.git] / gcc / cp / name-lookup.c
blob3eb010a8d1fd56c3f35eed3d5a1441cc6128924b
1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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 2, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "name-lookup.h"
29 #include "timevar.h"
31 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
32 static GTY((deletable (""))) cxx_binding *free_bindings;
34 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
35 cxx_binding *
36 cxx_binding_make (tree value, tree type)
38 cxx_binding *binding;
39 if (free_bindings)
41 binding = free_bindings;
42 free_bindings = binding->previous;
44 else
45 binding = ggc_alloc (sizeof (cxx_binding));
47 binding->value = value;
48 binding->type = type;
50 return binding;
53 /* Put BINDING back on the free list. */
54 void
55 cxx_binding_free (cxx_binding *binding)
57 binding->previous = free_bindings;
58 free_bindings = binding;
61 /* Return (from the stack of) the BINDING, if any, establihsed at SCOPE. */
63 static inline cxx_binding *
64 find_binding (cxx_scope *scope, cxx_binding *binding)
66 timevar_push (TV_NAME_LOOKUP);
68 for (; binding != NULL; binding = binding->previous)
69 if (BINDING_SCOPE (binding) == scope)
70 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
72 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL);
75 /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
76 cxx_binding *
77 cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
79 cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
80 if (b)
82 /* Fold-in case where NAME is used only once. */
83 if (scope == BINDING_SCOPE (b) && b->previous == NULL)
84 return b;
85 return find_binding (scope, b);
87 return NULL;
90 /* Always returns a binding for name in scope. If no binding is
91 found, make a new one. */
93 cxx_binding *
94 binding_for_name (cxx_scope *scope, tree name)
96 cxx_binding *result;
98 result = cxx_scope_find_binding_for_name (scope, name);
99 if (result)
100 return result;
101 /* Not found, make a new one. */
102 result = cxx_binding_make (NULL, NULL);
103 result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
104 BINDING_SCOPE (result) = scope;
105 result->is_local = false;
106 result->value_is_inherited = false;
107 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
108 return result;
111 /* Namespace-scope manipulation routines. */
113 /* Return the binding value for name in scope. */
115 tree
116 namespace_binding (tree name, tree scope)
118 cxx_binding *binding;
120 if (scope == NULL)
121 scope = global_namespace;
122 scope = ORIGINAL_NAMESPACE (scope);
123 binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
125 return binding ? binding->value : NULL_TREE;
128 /* Set the binding value for name in scope. */
130 void
131 set_namespace_binding (tree name, tree scope, tree val)
133 cxx_binding *b;
135 timevar_push (TV_NAME_LOOKUP);
136 if (scope == NULL_TREE)
137 scope = global_namespace;
138 b = binding_for_name (NAMESPACE_LEVEL (scope), name);
139 BINDING_VALUE (b) = val;
140 timevar_pop (TV_NAME_LOOKUP);