summaryrefslogtreecommitdiff
path: root/vala/valacodenode.vala
blob: d92232e1c12579f052db70a3668189e5d2864a5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* valacodenode.vala
 *
 * Copyright (C) 2006-2008  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;
using Gee;

/**
 * Represents a part of the parsed source code.
 *
 * Code nodes get created by the parser and are used throughout the whole
 * compilation process.
 */
public abstract class Vala.CodeNode : Object {
	/**
	 * Parent of this code node.
	 */
	public weak CodeNode? parent_node { get; set; }

	/**
	 * References the location in the source file where this code node has
	 * been written.
	 */
	public SourceReference? source_reference { get; set; }
	
	/**
	 * Contains all attributes that have been specified for this code node.
	 */
	public GLib.List<Attribute> attributes;
	
	/**
	 * Generated CCodeNode that corresponds to this code node.
	 */
	public CCodeNode? ccodenode {
		get {
			return _ccodenode;
		}
		set {
			if (value != null && source_reference != null) {
				value.line = new CCodeLineDirective (
					Path.get_basename (source_reference.file.filename),
					source_reference.first_line);
			}

			_ccodenode = value;
		}
	}

	/**
	 * Specifies whether a fatal error has been detected in this code node.
	 */
	public bool error { get; set; }

	/**
	 * Specifies that this node or a child node may throw an exception.
	 */
	public bool tree_can_fail { 
		get { return _error_types.size > 0; }
	}

	/**
	 * Specifies the exceptions that can be thrown by this node or a child node
	 */
	public Gee.List<DataType> get_error_types () { 
		return _error_types;
	}

	private Gee.List<DataType> _error_types = new ArrayList<DataType> ();

	/**
	 * Adds an error type to the exceptions that can be thrown by this node
	 * or a child node 
	 */
	public void add_error_type (DataType error_type) {
		_error_types.add (error_type);
		error_type.parent_node = this;
	}

	/**
	 * Adds a collection of error types to the exceptions that can be thrown by this node
	 * or a child node 
	 */
	public void add_error_types (Gee.Collection<DataType> error_types) {
		foreach (DataType error_type in error_types) {
			_error_types.add (error_type);
			error_type.parent_node = this;
		}
	}

	/**
	 * Visits this code node with the specified CodeVisitor.
	 *
	 * @param visitor the visitor to be called while traversing
	 */
	public virtual void accept (CodeVisitor visitor) {
	}

	/**
	 * Visits all children of this code node with the specified CodeVisitor.
	 *
	 * @param visitor the visitor to be called while traversing
	 */
	public virtual void accept_children (CodeVisitor visitor) {
	}

	public virtual void replace_type (DataType old_type, DataType new_type) {
	}

	public virtual void replace_expression (Expression old_node, Expression new_node) {
	}

	/**
	 * Returns the specified attribute.
	 *
	 * @param name attribute name
	 * @return     attribute
	 */
	public Attribute? get_attribute (string name) {
		// FIXME: use hash table
		foreach (Attribute a in attributes) {
			if (a.name == name) {
				return a;
			}
		}
		
		return null;
	}

	private CodeBinding? code_binding;
	private CCodeNode? _ccodenode;

	/**
	 * Returns a string that represents this code node.
	 *
	 * @return a string representation
	 */
	public virtual string to_string () {
		var str = new StringBuilder ();

		str.append ("/* ").append (get_type ().name ());

		if (source_reference != null) {
			str.append ("@").append (source_reference.to_string ());
		}

		return str.append (" */").str;
	}

	/**
	 * Returns the binding to the generated code.
	 *
	 * @return code binding
	 */
	public CodeBinding? get_code_binding (CodeGenerator codegen) {
		if (code_binding == null) {
			code_binding = create_code_binding (codegen);
		}
		return code_binding;
	}

	/**
	 * Creates the binding to the generated code.
	 */
	public virtual CodeBinding? create_code_binding (CodeGenerator codegen) {
		return null;
	}
}