Fix gtk_text_iter_forward_find_char binding, patch by Nicolas Joseph,
[vala-lang.git] / vala / valacodenode.vala
blob015f227dab83c5a017204158bd0869d509a4652d
1 /* valacodenode.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
24 using Gee;
26 /**
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 {
33 /**
34 * Parent of this code node.
36 public weak CodeNode? parent_node { get; set; }
38 /**
39 * References the location in the source file where this code node has
40 * been written.
42 public SourceReference? source_reference { get; set; }
44 /**
45 * Contains all attributes that have been specified for this code node.
47 public GLib.List<Attribute> attributes;
49 /**
50 * Generated CCodeNode that corresponds to this code node.
52 public CCodeNode? ccodenode {
53 get {
54 return _ccodenode;
56 set {
57 if (value != null && source_reference != null) {
58 value.line = new CCodeLineDirective (
59 Path.get_basename (source_reference.file.filename),
60 source_reference.first_line);
63 _ccodenode = value;
67 public bool checked { get; set; }
69 /**
70 * Specifies whether a fatal error has been detected in this code node.
72 public bool error { get; set; }
74 /**
75 * Specifies that this node or a child node may throw an exception.
77 public bool tree_can_fail {
78 get { return _error_types != null && _error_types.size > 0; }
81 private Gee.List<DataType> _error_types;
82 private static Gee.List<DataType> _empty_type_list;
84 private CCodeNode? _ccodenode;
86 static int last_temp_nr = 0;
88 /**
89 * Specifies the exceptions that can be thrown by this node or a child node
91 public Gee.List<DataType> get_error_types () {
92 if (_error_types != null) {
93 return _error_types;
95 if (_empty_type_list == null) {
96 _empty_type_list = new ReadOnlyList<DataType> (new ArrayList<DataType> ());
98 return _empty_type_list;
102 * Adds an error type to the exceptions that can be thrown by this node
103 * or a child node
105 public void add_error_type (DataType error_type) {
106 if (_error_types == null) {
107 _error_types = new ArrayList<DataType> ();
109 _error_types.add (error_type);
110 error_type.parent_node = this;
114 * Adds a collection of error types to the exceptions that can be thrown by this node
115 * or a child node
117 public void add_error_types (Gee.List<DataType> error_types) {
118 foreach (DataType error_type in error_types) {
119 add_error_type (error_type);
124 * Visits this code node with the specified CodeVisitor.
126 * @param visitor the visitor to be called while traversing
128 public virtual void accept (CodeVisitor visitor) {
132 * Visits all children of this code node with the specified CodeVisitor.
134 * @param visitor the visitor to be called while traversing
136 public virtual void accept_children (CodeVisitor visitor) {
139 public virtual bool check (SemanticAnalyzer analyzer) {
140 return true;
143 public virtual void replace_type (DataType old_type, DataType new_type) {
146 public virtual void replace_expression (Expression old_node, Expression new_node) {
150 * Returns the specified attribute.
152 * @param name attribute name
153 * @return attribute
155 public Attribute? get_attribute (string name) {
156 // FIXME: use hash table
157 foreach (Attribute a in attributes) {
158 if (a.name == name) {
159 return a;
163 return null;
167 * Returns a string that represents this code node.
169 * @return a string representation
171 public virtual string to_string () {
172 var str = new StringBuilder ();
174 str.append ("/* ");
176 if (source_reference != null) {
177 str.append ("@").append (source_reference.to_string ());
180 return str.append (" */").str;
183 public virtual void get_defined_variables (Collection<LocalVariable> collection) {
186 public virtual void get_used_variables (Collection<LocalVariable> collection) {
189 public string get_temp_name () {
190 return "." + (++last_temp_nr).to_string ();
193 public weak string get_type_name () {
194 return Type.from_instance (this).name ();