Warn when using result variable with incompatible type to prepare possible
[vala-lang.git] / vala / valaerrortype.vala
blob8d19cb3c03468b199169eba84e23f8075eaed3ef
1 /* valaerrortype.vala
3 * Copyright (C) 2008 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>
21 * Raffaele Sandrini <raffaele@sandrini.ch>
24 using GLib;
26 /**
27 * A class type.
29 public class Vala.ErrorType : ReferenceType {
30 /**
31 * The error domain or null for generic error.
33 public weak ErrorDomain? error_domain { get; set; }
35 /**
36 * The error code or null for generic error.
38 public weak ErrorCode? error_code { get; set; }
40 public ErrorType (ErrorDomain? error_domain, ErrorCode? error_code, SourceReference? source_reference = null) {
41 this.error_domain = error_domain;
42 this.data_type = error_domain;
43 this.error_code = error_code;
44 this.source_reference = source_reference;
47 public override bool compatible (DataType target_type) {
48 /* temporarily ignore type parameters */
49 if (target_type.type_parameter != null) {
50 return true;
53 var et = target_type as ErrorType;
55 /* error types are only compatible to error types */
56 if (et == null) {
57 return false;
60 /* every error type is compatible to the base error type */
61 if (et.error_domain == null) {
62 return true;
65 /* otherwhise the error_domain has to be equal */
66 if (et.error_domain != error_domain) {
67 return false;
70 if (et.error_code == null) {
71 return true;
74 return et.error_code == error_code;
77 public override string to_qualified_string (Scope? scope) {
78 if (error_domain == null) {
79 return "GLib.Error";
80 } else {
81 return error_domain.get_full_name ();
85 public override DataType copy () {
86 var result = new ErrorType (error_domain, error_code, source_reference);
87 result.value_owned = value_owned;
88 result.nullable = nullable;
90 return result;
93 public override string? get_cname () {
94 return "GError*";
97 public override string? get_lower_case_cname (string? infix = null) {
98 if (error_domain == null) {
99 if (infix == null) {
100 return "g_error";
101 } else {
102 return "g_%s_error".printf (infix);
104 } else {
105 return error_domain.get_lower_case_cname (infix);
109 public override bool equals (DataType type2) {
110 var et = type2 as ErrorType;
112 if (et == null) {
113 return false;
116 return error_domain == et.error_domain;
119 public override Symbol? get_member (string member_name) {
120 var root_symbol = source_reference.file.context.root;
121 var gerror_symbol = root_symbol.scope.lookup ("GLib").scope.lookup ("Error");
122 return gerror_symbol.scope.lookup (member_name);
125 public override string? get_type_id () {
126 return "G_TYPE_POINTER";
129 public override bool is_reference_type_or_type_parameter () {
130 return true;
133 public override bool check (SemanticAnalyzer analyzer) {
134 if (error_domain != null) {
135 return error_domain.check (analyzer);
137 return true;