Line data Source code
1 : /* valaerrordomain.vala
2 : *
3 : * Copyright (C) 2008-2010 Jürg Billeter
4 : *
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.
9 :
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.
14 :
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
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : using GLib;
24 :
25 : /**
26 : * Represents an error domain declaration in the source code.
27 : */
28 63194 : public class Vala.ErrorDomain : TypeSymbol {
29 61664 : private List<ErrorCode> codes = new ArrayList<ErrorCode> ();
30 61664 : private List<Method> methods = new ArrayList<Method> ();
31 :
32 : /**
33 : * Creates a new error domain.
34 : *
35 : * @param name type name
36 : * @param source_reference reference to source code
37 : * @return newly created error domain
38 : */
39 92496 : public ErrorDomain (string name, SourceReference? source_reference = null, Comment? comment = null) {
40 30832 : base (name, source_reference, comment);
41 : }
42 :
43 : /**
44 : * Appends the specified code to the list of error codes.
45 : *
46 : * @param ecode an error code
47 : */
48 445542 : public void add_code (ErrorCode ecode) {
49 445542 : codes.add (ecode);
50 445542 : scope.add (ecode.name, ecode);
51 : }
52 :
53 : /**
54 : * Adds the specified method as a member to this error domain.
55 : *
56 : * @param m a method
57 : */
58 53281 : public override void add_method (Method m) {
59 53281 : if (m is CreationMethod) {
60 1 : Report.error (m.source_reference, "construction methods may only be declared within classes and structs");
61 :
62 1 : m.error = true;
63 1 : return;
64 : }
65 53280 : if (m.binding == MemberBinding.INSTANCE) {
66 1 : m.this_parameter = new Parameter ("this", new ErrorType (this, null), m.source_reference);
67 1 : m.scope.add (m.this_parameter.name, m.this_parameter);
68 : }
69 :
70 53280 : methods.add (m);
71 53280 : scope.add (m.name, m);
72 : }
73 :
74 : /**
75 : * Returns the list of error codes.
76 : *
77 : * @return list of error codes
78 : */
79 147 : public unowned List<ErrorCode> get_codes () {
80 147 : return codes;
81 : }
82 :
83 : /**
84 : * Returns the list of methods.
85 : *
86 : * @return list of methods
87 : */
88 36 : public unowned List<Method> get_methods () {
89 36 : return methods;
90 : }
91 :
92 68933 : public override void accept (CodeVisitor visitor) {
93 68933 : visitor.visit_error_domain (this);
94 : }
95 :
96 67355 : public override void accept_children (CodeVisitor visitor) {
97 2008099 : foreach (ErrorCode ecode in codes) {
98 970372 : ecode.accept (visitor);
99 : }
100 :
101 299147 : foreach (Method m in methods) {
102 115896 : m.accept (visitor);
103 : }
104 : }
105 :
106 41 : public override bool is_reference_type () {
107 41 : return false;
108 : }
109 :
110 357462 : public override bool check (CodeContext context) {
111 357462 : if (checked) {
112 328912 : return !error;
113 : }
114 :
115 28550 : checked = true;
116 :
117 28550 : if (codes.size <= 0) {
118 1 : Report.error (source_reference, "Error domain `%s' requires at least one code", get_full_name ());
119 1 : error = true;
120 1 : return false;
121 : }
122 :
123 853053 : foreach (ErrorCode ecode in codes) {
124 412252 : ecode.check (context);
125 : }
126 :
127 127129 : foreach (Method m in methods) {
128 49290 : if (m.binding == MemberBinding.INSTANCE) {
129 1 : if (external_package) {
130 0 : Report.warning (m.source_reference, "Instance methods are not supported in error domains yet");
131 : } else {
132 1 : Report.error (m.source_reference, "Instance methods are not supported in error domains yet");
133 : }
134 1 : error = true;
135 : }
136 49290 : m.check (context);
137 : }
138 :
139 28549 : return !error;
140 : }
141 : }
|