gstreamer-pbutils-0.10: Ownership transfer fixes
[vala-lang.git] / vala / valaerrordomain.vala
blobeecd97c8ecbdd16bbacf3165887fde97ea4f2ed7
1 /* valaerrordomain.vala
3 * Copyright (C) 2008-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 an error domain declaration in the source code.
29 public class Vala.ErrorDomain : TypeSymbol {
30 private Gee.List<ErrorCode> codes = new ArrayList<ErrorCode> ();
31 private Gee.List<Method> methods = new ArrayList<Method> ();
32 private string cname;
33 private string cprefix;
34 private string lower_case_cprefix;
35 private string lower_case_csuffix;
37 /**
38 * Creates a new error domain.
40 * @param name type name
41 * @param source_reference reference to source code
42 * @return newly created error domain
44 public ErrorDomain (string name, SourceReference? source_reference = null) {
45 base (name, source_reference);
48 /**
49 * Appends the specified code to the list of error codes.
51 * @param ecode an error code
53 public void add_code (ErrorCode ecode) {
54 codes.add (ecode);
55 scope.add (ecode.name, ecode);
58 /**
59 * Adds the specified method as a member to this error domain.
61 * @param m a method
63 public void add_method (Method m) {
64 if (m is CreationMethod) {
65 Report.error (m.source_reference, "construction methods may only be declared within classes and structs");
67 m.error = true;
68 return;
70 if (m.binding == MemberBinding.INSTANCE) {
71 m.this_parameter = new FormalParameter ("this", new ErrorType (this, null));
72 m.scope.add (m.this_parameter.name, m.this_parameter);
75 methods.add (m);
76 scope.add (m.name, m);
79 /**
80 * Returns a copy of the list of error codes.
82 * @return list of error codes
84 public Gee.List<ErrorCode> get_codes () {
85 return new ReadOnlyList<ErrorCode> (codes);
88 /**
89 * Returns a copy of the list of methods.
91 * @return list of methods
93 public Gee.List<Method> get_methods () {
94 return new ReadOnlyList<Method> (methods);
97 public override void accept (CodeVisitor visitor) {
98 visitor.visit_error_domain (this);
101 public override void accept_children (CodeVisitor visitor) {
102 foreach (ErrorCode ecode in codes) {
103 ecode.accept (visitor);
106 foreach (Method m in methods) {
107 m.accept (visitor);
111 public override string get_cname (bool const_type = false) {
112 if (cname == null) {
113 cname = "%s%s".printf (parent_symbol.get_cprefix (), name);
115 return cname;
118 public override string get_lower_case_cprefix () {
119 if (lower_case_cprefix == null) {
120 lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
122 return lower_case_cprefix;
125 private string get_lower_case_csuffix () {
126 if (lower_case_csuffix == null) {
127 lower_case_csuffix = camel_case_to_lower_case (name);
129 return lower_case_csuffix;
132 public override string? get_lower_case_cname (string? infix) {
133 if (infix == null) {
134 infix = "";
136 return "%s%s%s".printf (parent_symbol.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
139 public override string? get_upper_case_cname (string? infix) {
140 return get_lower_case_cname (infix).up ();
143 public override bool is_reference_type () {
144 return false;
147 private void set_cname (string cname) {
148 this.cname = cname;
151 public override string get_cprefix () {
152 if (cprefix == null) {
153 cprefix = "%s_".printf (get_upper_case_cname (null));
155 return cprefix;
159 * Sets the string to be prepended to the name of members of this error
160 * domain when used in C code.
162 * @param cprefix the prefix to be used in C code
164 public void set_cprefix (string cprefix) {
165 this.cprefix = cprefix;
168 private void process_ccode_attribute (Attribute a) {
169 if (a.has_argument ("cname")) {
170 set_cname (a.get_string ("cname"));
172 if (a.has_argument ("cprefix")) {
173 set_cprefix (a.get_string ("cprefix"));
175 if (a.has_argument ("lower_case_csuffix")) {
176 lower_case_csuffix = a.get_string ("lower_case_csuffix");
178 if (a.has_argument ("cheader_filename")) {
179 var val = a.get_string ("cheader_filename");
180 foreach (string filename in val.split (",")) {
181 add_cheader_filename (filename);
187 * Process all associated attributes.
189 public void process_attributes () {
190 foreach (Attribute a in attributes) {
191 if (a.name == "CCode") {
192 process_ccode_attribute (a);
197 public override string? get_type_id () {
198 return "G_TYPE_POINTER";
201 public override string? get_marshaller_type_name () {
202 return "POINTER";
205 public override string? get_get_value_function () {
206 return "g_value_get_pointer";
209 public override string? get_set_value_function () {
210 return "g_value_set_pointer";
213 public override bool check (SemanticAnalyzer analyzer) {
214 if (checked) {
215 return !error;
218 checked = true;
220 process_attributes ();
222 foreach (ErrorCode ecode in codes) {
223 ecode.check (analyzer);
226 foreach (Method m in methods) {
227 m.check (analyzer);
230 return !error;