~kkubasik/vala/motu_package

« back to all changes in this revision

Viewing changes to vala-0.0.8/vala/valasymbol.vala

  • Committer: Kevin Kubasik
  • Date: 2007-04-23 16:46:12 UTC
  • Revision ID: kevin@kubasik.net-20070423164612-2nrkvw6m0lxixxft
inital import of vala 0.0.8 and its ubuntu packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* valasymbol.vala
 
2
 *
 
3
 * Copyright (C) 2006  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 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 node in the symbol tree.
 
27
 */
 
28
public class Vala.Symbol {
 
29
        /**
 
30
         * The code node that created this symbol, if applicable.
 
31
         */
 
32
        public weak CodeNode node { get; set; }
 
33
        
 
34
        /**
 
35
         * The parent of this symbol.
 
36
         */
 
37
        public weak Symbol parent_symbol { get; set; }
 
38
        
 
39
        /**
 
40
         * The symbol name.
 
41
         */
 
42
        public string name { get; set; }
 
43
 
 
44
        /**
 
45
         * Specifies whether this symbol is active.
 
46
         *
 
47
         * Symbols may become inactive when they only apply to a part of a
 
48
         * scope. This is used for local variables not declared at the beginning
 
49
         * of the block to determine which variables need to be freed before
 
50
         * jump statements.
 
51
         */
 
52
        public bool active { get; set; }
 
53
        
 
54
        private HashTable<string,Symbol> symbol_table = new HashTable.full (str_hash, str_equal, g_free, g_object_unref);
 
55
        
 
56
        /**
 
57
         * Creates a new symbol.
 
58
         *
 
59
         * @param node the corresponding code node
 
60
         * @return     newly created symbol
 
61
         */
 
62
        public Symbol (CodeNode _node = null)  {
 
63
                node = _node;
 
64
        }
 
65
        
 
66
        construct {
 
67
                active = true;
 
68
        }
 
69
        
 
70
        /**
 
71
         * Returns the fully expanded name of this symbol for use in
 
72
         * human-readable messages.
 
73
         *
 
74
         * @return full name
 
75
         */
 
76
        public ref string get_full_name () {
 
77
                if (parent_symbol == null) {
 
78
                        return name;
 
79
                }
 
80
                
 
81
                if (name == null) {
 
82
                        return parent_symbol.get_full_name ();
 
83
                }
 
84
 
 
85
                if (parent_symbol.get_full_name () == null) {
 
86
                        return name;
 
87
                }
 
88
                
 
89
                return "%s.%s".printf (parent_symbol.get_full_name (), name);
 
90
        }
 
91
        
 
92
        /**
 
93
         * Adds the specified symbol with the specified name to the symbol table
 
94
         * of this symbol.
 
95
         *
 
96
         * @param name name for the specified symbol
 
97
         * @param sym  a symbol
 
98
         */
 
99
        public void add (string! name, Symbol! sym) {
 
100
                symbol_table.insert (name, sym);
 
101
                sym.parent_symbol = this;
 
102
                sym.name = name;
 
103
        }
 
104
        
 
105
        /**
 
106
         * Returns the symbol stored in the symbol table with the specified
 
107
         * name.
 
108
         *
 
109
         * @param name name of the symbol to be returned
 
110
         * @return     found symbol or null
 
111
         */
 
112
        public Symbol lookup (string! name) {
 
113
                Symbol sym = symbol_table.lookup (name);
 
114
                if (sym != null && !sym.active) {
 
115
                        sym = null;
 
116
                }
 
117
                return sym;
 
118
        }
 
119
}