Fix crash when using out parameters in delegates, fixes bug 563705
[vala-lang.git] / vala / valascope.vala
blobd151113daf41ddaf19017532abb02a1eed29b713
1 /* valascope.vala
3 * Copyright (C) 2006-2008 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;
24 using Gee;
26 /**
27 * Represents a part of the symbol tree.
29 public class Vala.Scope {
30 /**
31 * The symbol that owns this scope.
33 public weak Symbol owner { get; set; }
35 /**
36 * The parent of this scope.
38 public weak Scope parent_scope { get; set; }
40 private Map<string,Symbol> symbol_table;
41 private Gee.List<Symbol> anonymous_members;
43 /**
44 * Creates a new scope.
46 * @return newly created scope
48 public Scope (Symbol? owner = null) {
49 this.owner = owner;
52 /**
53 * Adds the specified symbol with the specified name to the symbol table
54 * of this scope.
56 * @param name name for the specified symbol
57 * @param sym a symbol
59 public void add (string? name, Symbol sym) {
60 if (name != null) {
61 if (symbol_table == null) {
62 symbol_table = new HashMap<string,Symbol> (str_hash, str_equal);
63 } else if (lookup (name) != null) {
64 owner.error = true;
65 Report.error (sym.source_reference, "`%s' already contains a definition for `%s'".printf (owner.get_full_name (), name));
66 return;
69 symbol_table[(string) name] = sym;
70 } else {
71 if (anonymous_members == null) {
72 anonymous_members = new ArrayList<Symbol> ();
75 anonymous_members.add (sym);
77 sym.owner = this;
80 public void remove (string name) {
81 symbol_table.remove (name);
84 /**
85 * Returns the symbol stored in the symbol table with the specified
86 * name.
88 * @param name name of the symbol to be returned
89 * @return found symbol or null
91 public Symbol? lookup (string name) {
92 if (symbol_table == null) {
93 return null;
95 Symbol sym = symbol_table[name];
96 if (sym != null && !sym.active) {
97 sym = null;
99 return sym;
103 * Returns whether the specified scope is an ancestor of this scope.
105 * @param scope a scope or null for the root scope
106 * @return true if this scope is a subscope of the specified
107 * scope, false otherwise
109 public bool is_subscope_of (Scope? scope) {
110 if (scope == this) {
111 return true;
114 // null scope is the root scope
115 if (scope == null) {
116 return true;
119 if (parent_scope != null) {
120 return parent_scope.is_subscope_of (scope);
123 return false;