3 * Copyright (C) 2006-2010 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
26 * Represents a part of the symbol tree.
28 public class Vala
.Scope
{
30 * The symbol that owns this scope.
32 public weak Symbol owner
{ get; set; }
35 * The parent of this scope.
37 public weak Scope parent_scope
{ get; set; }
39 private Map
<string,Symbol
> symbol_table
;
40 private List
<Symbol
> anonymous_members
;
43 * Creates a new scope.
45 * @return newly created scope
47 public Scope (Symbol? owner
= null) {
52 * Adds the specified symbol with the specified name to the symbol table
55 * @param name name for the specified symbol
58 public void add (string? name
, Symbol sym
) {
60 if (symbol_table
== null) {
61 symbol_table
= new HashMap
<string,Symbol
> (str_hash
, str_equal
);
62 } else if (lookup (name
) != null) {
64 if (owner
.name
== null && owner
.parent_symbol
== null) {
65 Report
.error (sym
.source_reference
, "The root namespace already contains a definition for `%s'".printf (name
));
67 Report
.error (sym
.source_reference
, "`%s' already contains a definition for `%s'".printf (owner
.get_full_name (), name
));
69 Report
.notice (lookup (name
).source_reference
, "previous definition of `%s' was here".printf (name
));
73 symbol_table
[(string) name
] = sym
;
75 if (anonymous_members
== null) {
76 anonymous_members
= new ArrayList
<Symbol
> ();
79 anonymous_members
.add (sym
);
84 public void remove (string name
) {
85 symbol_table
.remove (name
);
89 * Returns the symbol stored in the symbol table with the specified
92 * @param name name of the symbol to be returned
93 * @return found symbol or null
95 public Symbol?
lookup (string name
) {
96 if (symbol_table
== null) {
99 Symbol sym
= symbol_table
[name
];
100 if (sym
!= null && !sym
.active
) {
107 * Returns whether the specified scope is an ancestor of this scope.
109 * @param scope a scope or null for the root scope
110 * @return true if this scope is a subscope of the specified
111 * scope, false otherwise
113 public bool is_subscope_of (Scope? scope
) {
118 // null scope is the root scope
123 if (parent_scope
!= null) {
124 return parent_scope
.is_subscope_of (scope
);
130 public Map
<string,Symbol
> get_symbol_table () {