3 * Copyright (C) 2006-2010 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>
26 * Represents a part of the parsed source code.
28 * Code nodes get created by the parser and are used throughout the whole
29 * compilation process.
31 public abstract class Vala
.CodeNode
{
33 * Parent of this code node.
35 public weak CodeNode? parent_node
{ get; set; }
38 * References the location in the source file where this code node has
41 public SourceReference? source_reference
{ get; set; }
43 public bool unreachable
{ get; set; }
46 * Contains all attributes that have been specified for this code node.
48 public GLib
.List
<Attribute
> attributes
;
50 public string type_name
{
51 get { return Type
.from_instance (this
).name (); }
54 public bool checked
{ get; set; }
57 * Specifies whether a fatal error has been detected in this code node.
59 public bool error
{ get; set; }
62 * Specifies that this node or a child node may throw an exception.
64 public bool tree_can_fail
{
65 get { return _error_types
!= null && _error_types
.size
> 0; }
68 private List
<DataType
> _error_types
;
69 private static List
<DataType
> _empty_type_list
;
71 static int last_temp_nr
= 0;
74 * Specifies the exceptions that can be thrown by this node or a child node
76 public List
<DataType
> get_error_types () {
77 if (_error_types
!= null) {
80 if (_empty_type_list
== null) {
81 _empty_type_list
= new ArrayList
<DataType
> ();
83 return _empty_type_list
;
87 * Adds an error type to the exceptions that can be thrown by this node
90 public void add_error_type (DataType error_type
) {
91 if (_error_types
== null) {
92 _error_types
= new ArrayList
<DataType
> ();
94 _error_types
.add (error_type
);
95 error_type
.parent_node
= this
;
99 * Adds a collection of error types to the exceptions that can be thrown by this node
102 public void add_error_types (List
<DataType
> error_types
) {
103 foreach (DataType error_type
in error_types
) {
104 add_error_type (error_type
);
109 * Visits this code node with the specified CodeVisitor.
111 * @param visitor the visitor to be called while traversing
113 public virtual void accept (CodeVisitor visitor
) {
117 * Visits all children of this code node with the specified CodeVisitor.
119 * @param visitor the visitor to be called while traversing
121 public virtual void accept_children (CodeVisitor visitor
) {
124 public virtual bool check (CodeContext context
) {
128 public virtual void emit (CodeGenerator codegen
) {
131 public virtual void replace_type (DataType old_type
, DataType new_type
) {
134 public virtual void replace_expression (Expression old_node
, Expression new_node
) {
138 * Returns the specified attribute.
140 * @param name attribute name
143 public Attribute?
get_attribute (string name
) {
144 // FIXME: use hash table
145 foreach (Attribute a
in attributes
) {
146 if (a
.name
== name
) {
155 * Returns a string that represents this code node.
157 * @return a string representation
159 public virtual string to_string () {
160 var str
= new
StringBuilder ();
164 if (source_reference
!= null) {
165 str
.append ("@").append (source_reference
.to_string ());
168 return str
.append (" */").str
;
171 public virtual void get_defined_variables (Collection
<LocalVariable
> collection
) {
174 public virtual void get_used_variables (Collection
<LocalVariable
> collection
) {
177 public static string get_temp_name () {
178 return "." + (++last_temp_nr
).to_string ();