Line data Source code
1 : /* valascope.vala
2 : *
3 : * Copyright (C) 2006-2010 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents a part of the symbol tree.
27 : */
28 198755625 : public class Vala.Scope {
29 : /**
30 : * The symbol that owns this scope.
31 : */
32 381258820 : public weak Symbol owner { get; set; }
33 :
34 : /**
35 : * The parent of this scope.
36 : */
37 93622566 : public weak Scope parent_scope { get; set; }
38 :
39 60047056 : private Map<string,Symbol> symbol_table;
40 60047056 : private List<Symbol> anonymous_members;
41 :
42 : /**
43 : * Creates a new scope.
44 : *
45 : * @return newly created scope
46 : */
47 120094132 : public Scope (Symbol? owner = null) {
48 60047066 : this.owner = owner;
49 : }
50 :
51 : /**
52 : * Adds the specified symbol with the specified name to the symbol table
53 : * of this scope.
54 : *
55 : * @param name name for the specified symbol
56 : * @param sym a symbol
57 : */
58 27174079 : public void add (string? name, Symbol sym) {
59 : // Ignore params-array parameters which can not be conflicted with
60 27174079 : if (sym is Parameter && ((Parameter) sym).params_array) {
61 : name = null;
62 : }
63 27170941 : if (name != null) {
64 26914864 : if (symbol_table == null) {
65 8189468 : symbol_table = new HashMap<string,Symbol> (str_hash, str_equal);
66 18725396 : } else if (lookup (name) != null) {
67 1 : owner.error = true;
68 1 : if (owner.name == null && owner.parent_symbol == null) {
69 1 : Report.error (sym.source_reference, "The root namespace already contains a definition for `%s'", name);
70 : } else {
71 0 : Report.error (sym.source_reference, "`%s' already contains a definition for `%s'", owner.get_full_name (), name);
72 : }
73 1 : Report.notice (lookup (name).source_reference, "previous definition of `%s' was here", name);
74 1 : return;
75 : }
76 :
77 26914863 : symbol_table[(string) name] = sym;
78 : } else {
79 259215 : if (anonymous_members == null) {
80 177371 : anonymous_members = new ArrayList<Symbol> ();
81 : }
82 :
83 259215 : anonymous_members.add (sym);
84 : }
85 27174078 : sym.owner = this;
86 : }
87 :
88 2 : public void remove (string name) {
89 2 : symbol_table.remove (name);
90 : }
91 :
92 : /**
93 : * Returns the symbol stored in the symbol table with the specified
94 : * name.
95 : *
96 : * @param name name of the symbol to be returned
97 : * @return found symbol or null
98 : */
99 89562786 : public Symbol? lookup (string name) {
100 89562786 : if (symbol_table == null) {
101 : return null;
102 : }
103 87351251 : Symbol sym = symbol_table[name];
104 87351251 : if (sym != null && !sym.active) {
105 0 : sym = null;
106 : }
107 : return sym;
108 : }
109 :
110 : /**
111 : * Returns whether the specified scope is an ancestor of this scope.
112 : *
113 : * @param scope a scope or null for the root scope
114 : * @return true if this scope is a subscope of the specified
115 : * scope, false otherwise
116 : */
117 560718 : public bool is_subscope_of (Scope? scope) {
118 560718 : if (scope == this) {
119 : return true;
120 : }
121 :
122 : // null scope is the root scope
123 555969 : if (scope == null) {
124 : return true;
125 : }
126 :
127 578 : if (parent_scope != null) {
128 : return parent_scope.is_subscope_of (scope);
129 : }
130 :
131 : return false;
132 : }
133 :
134 3 : public unowned Map<string,Symbol> get_symbol_table () {
135 3 : return symbol_table;
136 : }
137 : }
138 :
|