girparser: Skip priv fields
[vala-lang.git] / vala / valascope.vala
bloba2a946b09968a513166283e9739627c1fb5557a6
1 /* valascope.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents a part of the symbol tree.
28 public class Vala.Scope {
29 /**
30 * The symbol that owns this scope.
32 public weak Symbol owner { get; set; }
34 /**
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;
42 /**
43 * Creates a new scope.
45 * @return newly created scope
47 public Scope (Symbol? owner = null) {
48 this.owner = owner;
51 /**
52 * Adds the specified symbol with the specified name to the symbol table
53 * of this scope.
55 * @param name name for the specified symbol
56 * @param sym a symbol
58 public void add (string? name, Symbol sym) {
59 if (name != null) {
60 if (symbol_table == null) {
61 symbol_table = new HashMap<string,Symbol> (str_hash, str_equal);
62 } else if (lookup (name) != null) {
63 owner.error = true;
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));
66 } else {
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));
70 return;
73 symbol_table[(string) name] = sym;
74 } else {
75 if (anonymous_members == null) {
76 anonymous_members = new ArrayList<Symbol> ();
79 anonymous_members.add (sym);
81 sym.owner = this;
84 public void remove (string name) {
85 symbol_table.remove (name);
88 /**
89 * Returns the symbol stored in the symbol table with the specified
90 * name.
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) {
97 return null;
99 Symbol sym = symbol_table[name];
100 if (sym != null && !sym.active) {
101 sym = null;
103 return sym;
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) {
114 if (scope == this) {
115 return true;
118 // null scope is the root scope
119 if (scope == null) {
120 return true;
123 if (parent_scope != null) {
124 return parent_scope.is_subscope_of (scope);
127 return false;
130 public Map<string,Symbol> get_symbol_table () {
131 return symbol_table;