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)
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. */
24 #include "coretypes.h"
28 #include "name-lookup.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. */
36 cxx_binding_make (tree value
, tree type
)
41 binding
= free_bindings
;
42 free_bindings
= binding
->previous
;
45 binding
= ggc_alloc (sizeof (cxx_binding
));
47 binding
->value
= value
;
53 /* Put BINDING back on the free list. */
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. */
77 cxx_scope_find_binding_for_name (cxx_scope
*scope
, tree name
)
79 cxx_binding
*b
= IDENTIFIER_NAMESPACE_BINDINGS (name
);
82 /* Fold-in case where NAME is used only once. */
83 if (scope
== BINDING_SCOPE (b
) && b
->previous
== NULL
)
85 return find_binding (scope
, b
);
90 /* Always returns a binding for name in scope. If no binding is
91 found, make a new one. */
94 binding_for_name (cxx_scope
*scope
, tree name
)
98 result
= cxx_scope_find_binding_for_name (scope
, name
);
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
;
111 /* Namespace-scope manipulation routines. */
113 /* Return the binding value for name in scope. */
116 namespace_binding (tree name
, tree scope
)
118 cxx_binding
*binding
;
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. */
131 set_namespace_binding (tree name
, tree scope
, tree val
)
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
);