Improve string tests
[vala-lang.git] / vala / valaerrordomain.vala
blob713e452ed69f0897150e48b92b310c91eb5b7a86
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;
152 * Returns the string to be prepended to the name of members of this
153 * error domain when used in C code.
155 * @return the prefix to be used in C code
157 public string get_cprefix () {
158 if (cprefix == null) {
159 cprefix = "%s_".printf (get_upper_case_cname (null));
161 return cprefix;
165 * Sets the string to be prepended to the name of members of this error
166 * domain when used in C code.
168 * @param cprefix the prefix to be used in C code
170 public void set_cprefix (string cprefix) {
171 this.cprefix = cprefix;
174 private void process_ccode_attribute (Attribute a) {
175 if (a.has_argument ("cname")) {
176 set_cname (a.get_string ("cname"));
178 if (a.has_argument ("cprefix")) {
179 set_cprefix (a.get_string ("cprefix"));
181 if (a.has_argument ("lower_case_csuffix")) {
182 lower_case_csuffix = a.get_string ("lower_case_csuffix");
184 if (a.has_argument ("cheader_filename")) {
185 var val = a.get_string ("cheader_filename");
186 foreach (string filename in val.split (",")) {
187 add_cheader_filename (filename);
193 * Process all associated attributes.
195 public void process_attributes () {
196 foreach (Attribute a in attributes) {
197 if (a.name == "CCode") {
198 process_ccode_attribute (a);
203 public override string? get_type_id () {
204 return "G_TYPE_POINTER";
207 public override string? get_marshaller_type_name () {
208 return "POINTER";
211 public override string? get_get_value_function () {
212 return "g_value_get_pointer";
215 public override string? get_set_value_function () {
216 return "g_value_set_pointer";
219 public override bool check (SemanticAnalyzer analyzer) {
220 if (checked) {
221 return !error;
224 checked = true;
226 process_attributes ();
228 foreach (ErrorCode ecode in codes) {
229 ecode.check (analyzer);
232 foreach (Method m in methods) {
233 m.check (analyzer);
236 return !error;