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
20 * Jürg Billeter <j@bitron.ch>
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
> ();
32 private string cprefix
;
33 private string lower_case_cprefix
;
34 private string lower_case_csuffix
;
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
);
48 * Appends the specified code to the list of error codes.
50 * @param ecode an error code
52 public void add_code (ErrorCode ecode
) {
54 scope
.add (ecode
.name
, ecode
);
58 * Adds the specified method as a member to this error domain.
62 public 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");
69 if (m
.binding
== MemberBinding
.INSTANCE
) {
70 m
.this_parameter
= new
FormalParameter ("this", new
ErrorType (this
, null));
71 m
.scope
.add (m
.this_parameter
.name
, m
.this_parameter
);
75 scope
.add (m
.name
, m
);
79 * Returns a copy of the list of error codes.
81 * @return list of error codes
83 public List
<ErrorCode
> get_codes () {
84 return new ReadOnlyList
<ErrorCode
> (codes
);
88 * Returns a copy of the list of methods.
90 * @return list of methods
92 public List
<Method
> get_methods () {
93 return new ReadOnlyList
<Method
> (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
) {
110 public override string get_cname (bool const_type
= false) {
112 cname
= "%s%s".printf (parent_symbol
.get_cprefix (), name
);
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
) {
135 return "%s%s%s".printf (parent_symbol
.get_lower_case_cprefix (), infix
, get_lower_case_csuffix ());
138 public override string?
get_upper_case_cname (string? infix
) {
139 return get_lower_case_cname (infix
).up ();
142 public override bool is_reference_type () {
146 private void set_cname (string cname
) {
150 public override string get_cprefix () {
151 if (cprefix
== null) {
152 cprefix
= "%s_".printf (get_upper_case_cname (null));
158 * Sets the string to be prepended to the name of members of this error
159 * domain when used in C code.
161 * @param cprefix the prefix to be used in C code
163 public void set_cprefix (string cprefix
) {
164 this
.cprefix
= cprefix
;
167 private void process_ccode_attribute (Attribute a
) {
168 if (a
.has_argument ("cname")) {
169 set_cname (a
.get_string ("cname"));
171 if (a
.has_argument ("cprefix")) {
172 set_cprefix (a
.get_string ("cprefix"));
174 if (a
.has_argument ("lower_case_csuffix")) {
175 lower_case_csuffix
= a
.get_string ("lower_case_csuffix");
177 if (a
.has_argument ("cheader_filename")) {
178 var val
= a
.get_string ("cheader_filename");
179 foreach (string filename
in val
.split (",")) {
180 add_cheader_filename (filename
);
186 * Process all associated attributes.
188 public void process_attributes () {
189 foreach (Attribute a
in attributes
) {
190 if (a
.name
== "CCode") {
191 process_ccode_attribute (a
);
196 public override string?
get_type_id () {
197 return "G_TYPE_POINTER";
200 public override string?
get_marshaller_type_name () {
204 public override string?
get_get_value_function () {
205 return "g_value_get_pointer";
208 public override string?
get_set_value_function () {
209 return "g_value_set_pointer";
212 public override bool check (SemanticAnalyzer analyzer
) {
219 process_attributes ();
221 foreach (ErrorCode ecode
in codes
) {
222 ecode
.check (analyzer
);
225 foreach (Method m
in methods
) {