girparser: Provide default constructor for classes.
[vala-lang.git] / vala / valaerrordomain.vala
blob40dd9b1ff89d052fc4a63c8d02f085d0a891c7cd
1 /* valaerrordomain.vala
3 * Copyright (C) 2008-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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 using GLib;
25 /**
26 * Represents an error domain declaration in the source code.
28 public class Vala.ErrorDomain : TypeSymbol {
29 private List<ErrorCode> codes = new ArrayList<ErrorCode> ();
30 private List<Method> methods = new ArrayList<Method> ();
31 private string cname;
32 private string cprefix;
33 private string lower_case_cprefix;
34 private string lower_case_csuffix;
36 /**
37 * Creates a new error domain.
39 * @param name type name
40 * @param source_reference reference to source code
41 * @return newly created error domain
43 public ErrorDomain (string name, SourceReference? source_reference = null, Comment? comment = null) {
44 base (name, source_reference, comment);
47 /**
48 * Appends the specified code to the list of error codes.
50 * @param ecode an error code
52 public void add_code (ErrorCode ecode) {
53 codes.add (ecode);
54 scope.add (ecode.name, ecode);
57 /**
58 * Adds the specified method as a member to this error domain.
60 * @param m a method
62 public override void add_method (Method m) {
63 if (m is CreationMethod) {
64 Report.error (m.source_reference, "construction methods may only be declared within classes and structs");
66 m.error = true;
67 return;
69 if (m.binding == MemberBinding.INSTANCE) {
70 m.this_parameter = new Parameter ("this", new ErrorType (this, null));
71 m.scope.add (m.this_parameter.name, m.this_parameter);
74 methods.add (m);
75 scope.add (m.name, m);
78 /**
79 * Returns a copy of the list of error codes.
81 * @return list of error codes
83 public List<ErrorCode> get_codes () {
84 return codes;
87 /**
88 * Returns a copy of the list of methods.
90 * @return list of methods
92 public List<Method> get_methods () {
93 return methods;
96 public override void accept (CodeVisitor visitor) {
97 visitor.visit_error_domain (this);
100 public override void accept_children (CodeVisitor visitor) {
101 foreach (ErrorCode ecode in codes) {
102 ecode.accept (visitor);
105 foreach (Method m in methods) {
106 m.accept (visitor);
110 public override string get_cname (bool const_type = false) {
111 if (cname == null) {
112 cname = "%s%s".printf (parent_symbol.get_cprefix (), name);
114 return cname;
117 public override string get_lower_case_cprefix () {
118 if (lower_case_cprefix == null) {
119 lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
121 return lower_case_cprefix;
124 private string get_lower_case_csuffix () {
125 if (lower_case_csuffix == null) {
126 lower_case_csuffix = camel_case_to_lower_case (name);
128 return lower_case_csuffix;
131 public override string? get_lower_case_cname (string? infix) {
132 if (infix == null) {
133 infix = "";
136 string cprefix = "";
137 if (parent_symbol != null) {
138 cprefix = parent_symbol.get_lower_case_cprefix ();
141 return "%s%s%s".printf (cprefix, infix, get_lower_case_csuffix ());
144 public override string? get_upper_case_cname (string? infix) {
145 return get_lower_case_cname (infix).up ();
148 public override bool is_reference_type () {
149 return false;
152 public void set_cname (string cname) {
153 this.cname = cname;
156 public override string get_cprefix () {
157 if (cprefix == null) {
158 cprefix = "%s_".printf (get_upper_case_cname (null));
160 return cprefix;
164 * Sets the string to be prepended to the name of members of this error
165 * domain when used in C code.
167 * @param cprefix the prefix to be used in C code
169 public void set_cprefix (string cprefix) {
170 this.cprefix = cprefix;
173 private void process_ccode_attribute (Attribute a) {
174 if (a.has_argument ("cname")) {
175 set_cname (a.get_string ("cname"));
177 if (a.has_argument ("cprefix")) {
178 set_cprefix (a.get_string ("cprefix"));
180 if (a.has_argument ("lower_case_csuffix")) {
181 lower_case_csuffix = a.get_string ("lower_case_csuffix");
183 if (a.has_argument ("cheader_filename")) {
184 var val = a.get_string ("cheader_filename");
185 foreach (string filename in val.split (",")) {
186 add_cheader_filename (filename);
192 * Process all associated attributes.
194 public void process_attributes () {
195 foreach (Attribute a in attributes) {
196 if (a.name == "CCode") {
197 process_ccode_attribute (a);
198 } else if (a.name == "Deprecated") {
199 process_deprecated_attribute (a);
200 } else if (a.name == "Experimental") {
201 process_experimental_attribute (a);
206 public override string? get_type_id () {
207 return "G_TYPE_POINTER";
210 public override string? get_marshaller_type_name () {
211 return "POINTER";
214 public override string? get_get_value_function () {
215 return "g_value_get_pointer";
218 public override string? get_set_value_function () {
219 return "g_value_set_pointer";
222 public override bool check (CodeContext context) {
223 if (checked) {
224 return !error;
227 checked = true;
229 process_attributes ();
231 foreach (ErrorCode ecode in codes) {
232 ecode.check (context);
235 foreach (Method m in methods) {
236 m.check (context);
239 return !error;