Line data Source code
1 : /* valanulltype.vala
2 : *
3 : * Copyright (C) 2007-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 : * The type of the null literal.
27 : */
28 1423 : public class Vala.NullType : ReferenceType {
29 819809 : public NullType (SourceReference? source_reference = null) {
30 819809 : base (null, source_reference);
31 819809 : this.nullable = true;
32 : }
33 :
34 612834 : public override bool compatible (DataType target_type) {
35 612834 : if (CodeContext.get ().experimental_non_null) {
36 5647 : return target_type.nullable;
37 : }
38 :
39 607187 : if (!(target_type is PointerType) && (target_type is NullType || (target_type.type_symbol == null && !(target_type is GenericType)))) {
40 4482 : return true;
41 : }
42 :
43 : /* null can be cast to any reference or array type or pointer type */
44 603540 : if (target_type is GenericType ||
45 595674 : target_type is PointerType ||
46 577418 : target_type.nullable ||
47 835 : target_type.type_symbol.has_attribute ("PointerType")) {
48 601870 : return true;
49 : }
50 :
51 835 : if (target_type.type_symbol.is_reference_type () ||
52 26 : target_type is ArrayType ||
53 26 : target_type is DelegateType) {
54 607187 : return true;
55 : }
56 :
57 : /* null is not compatible with any other type (i.e. value types) */
58 612834 : return false;
59 : }
60 :
61 51428 : public override DataType copy () {
62 51428 : return new NullType (source_reference);
63 : }
64 :
65 10723 : public override bool is_disposable () {
66 10723 : return false;
67 : }
68 :
69 2 : public override string to_qualified_string (Scope? scope = null) {
70 2 : return "null";
71 : }
72 : }
|