1/* valaerrorcode.vala
2 *
3 * Copyright (C) 2008  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
23using GLib;
24
25/**
26 * Represents an error value member in the source code.
27 */
28public class Vala.ErrorCode : TypeSymbol {
29	/**
30	 * Specifies the numerical representation of this enum value.
31	 */
32	public Expression? value {
33		get { return _value; }
34		set {
35			_value = value;
36			if (_value != null) {
37				_value.parent_node = this;
38			}
39		}
40	}
41
42	/**
43	 * Refers to the enum value of this error code for direct access.
44	 */
45	public Constant code {
46		get {
47			return _code;
48		}
49		private set {
50			_code = value;
51			if (_code != null) {
52				_code.owner = owner;
53			}
54		}
55	}
56
57	private Expression _value;
58	private Constant _code;
59
60	/**
61	 * Creates a new enum value.
62	 *
63	 * @param name enum value name
64	 * @return     newly created enum value
65	 */
66	public ErrorCode (string name, SourceReference? source_reference = null, Comment? comment = null) {
67		base (name, source_reference, comment);
68	}
69
70	/**
71	 * Creates a new enum value with the specified numerical representation.
72	 *
73	 * @param name  enum value name
74	 * @param value numerical representation
75	 * @return      newly created enum value
76	 */
77	public ErrorCode.with_value (string name, Expression value, SourceReference? source_reference = null) {
78		this (name, source_reference);
79		this.value = value;
80	}
81
82	public override void accept (CodeVisitor visitor) {
83		visitor.visit_error_code (this);
84	}
85
86	public override void accept_children (CodeVisitor visitor) {
87		if (value != null) {
88			value.accept (visitor);
89		}
90	}
91
92	public override bool check (CodeContext context) {
93		if (checked) {
94			return !error;
95		}
96
97		checked = true;
98
99		if (value != null) {
100			value.check (context);
101		}
102
103		code = new Constant (name, context.analyzer.int_type.copy (), null, source_reference, comment);
104		code.external = true;
105		code.check (context);
106
107		return !error;
108	}
109}
110