3 * Copyright (C) 2006-2009 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
20 * Jürg Billeter <j@bitron.ch>
27 * Represents a part of the parsed source code.
29 * Code nodes get created by the parser and are used throughout the whole
30 * compilation process.
32 public abstract class Vala
.CodeNode
{
34 * Parent of this code node.
36 public weak CodeNode? parent_node
{ get; set; }
39 * References the location in the source file where this code node has
42 public SourceReference? source_reference
{ get; set; }
45 * Contains all attributes that have been specified for this code node.
47 public GLib
.List
<Attribute
> attributes
;
49 public string type_name
{
50 get { return Type
.from_instance (this
).name (); }
54 * Generated CCodeNode that corresponds to this code node.
56 public CCodeNode? ccodenode
{
61 if (value
!= null && source_reference
!= null) {
62 value
.line
= new
CCodeLineDirective (
63 Path
.get_basename (source_reference
.file
.filename
),
64 source_reference
.first_line
);
71 public bool checked
{ get; set; }
74 * Specifies whether a fatal error has been detected in this code node.
76 public bool error
{ get; set; }
79 * Specifies that this node or a child node may throw an exception.
81 public bool tree_can_fail
{
82 get { return _error_types
!= null && _error_types
.size
> 0; }
85 private Gee
.List
<DataType
> _error_types
;
86 private static Gee
.List
<DataType
> _empty_type_list
;
88 private CCodeNode? _ccodenode
;
90 static int last_temp_nr
= 0;
93 * Specifies the exceptions that can be thrown by this node or a child node
95 public Gee
.List
<DataType
> get_error_types () {
96 if (_error_types
!= null) {
99 if (_empty_type_list
== null) {
100 _empty_type_list
= new ReadOnlyList
<DataType
> (new ArrayList
<DataType
> ());
102 return _empty_type_list
;
106 * Adds an error type to the exceptions that can be thrown by this node
109 public void add_error_type (DataType error_type
) {
110 if (_error_types
== null) {
111 _error_types
= new ArrayList
<DataType
> ();
113 _error_types
.add (error_type
);
114 error_type
.parent_node
= this
;
118 * Adds a collection of error types to the exceptions that can be thrown by this node
121 public void add_error_types (Gee
.List
<DataType
> error_types
) {
122 foreach (DataType error_type
in error_types
) {
123 add_error_type (error_type
);
128 * Visits this code node with the specified CodeVisitor.
130 * @param visitor the visitor to be called while traversing
132 public virtual void accept (CodeVisitor visitor
) {
136 * Visits all children of this code node with the specified CodeVisitor.
138 * @param visitor the visitor to be called while traversing
140 public virtual void accept_children (CodeVisitor visitor
) {
143 public virtual bool check (SemanticAnalyzer analyzer
) {
147 public virtual void replace_type (DataType old_type
, DataType new_type
) {
150 public virtual void replace_expression (Expression old_node
, Expression new_node
) {
154 * Returns the specified attribute.
156 * @param name attribute name
159 public Attribute?
get_attribute (string name
) {
160 // FIXME: use hash table
161 foreach (Attribute a
in attributes
) {
162 if (a
.name
== name
) {
171 * Returns a string that represents this code node.
173 * @return a string representation
175 public virtual string to_string () {
176 var str
= new
StringBuilder ();
180 if (source_reference
!= null) {
181 str
.append ("@").append (source_reference
.to_string ());
184 return str
.append (" */").str
;
187 public virtual void get_defined_variables (Collection
<LocalVariable
> collection
) {
190 public virtual void get_used_variables (Collection
<LocalVariable
> collection
) {
193 public string get_temp_name () {
194 return "." + (++last_temp_nr
).to_string ();