Line data Source code
1 : /* valaintegertype.vala
2 : *
3 : * Copyright (C) 2008-2009 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 : * An integer type.
27 : */
28 1429 : public class Vala.IntegerType : ValueType {
29 7612745 : string? literal_value;
30 7612745 : string? literal_type_name;
31 :
32 15225490 : public IntegerType (Struct type_symbol, string? literal_value = null, string? literal_type_name = null, SourceReference? source_reference = null) {
33 7612745 : base (type_symbol, source_reference);
34 15225490 : this.literal_value = literal_value;
35 15225490 : this.literal_type_name = literal_type_name;
36 : }
37 :
38 3483320 : public override DataType copy () {
39 3483320 : var result = new IntegerType ((Struct) type_symbol, literal_value, literal_type_name, source_reference);
40 3483320 : result.value_owned = value_owned;
41 3483320 : result.nullable = nullable;
42 3483320 : return result;
43 : }
44 :
45 680142 : public override bool compatible (DataType target_type) {
46 681658 : if (target_type.type_symbol is Struct && literal_type_name == "int") {
47 : // int literals are implicitly convertible to integer types
48 : // of a lower rank if the value of the literal is within
49 : // the range of the target type
50 337270 : var target_st = (Struct) target_type.type_symbol;
51 169393 : if (target_st.is_integer_type ()) {
52 167877 : var int_attr = target_st.get_attribute ("IntegerType");
53 167870 : if (int_attr != null && int_attr.has_argument ("min") && int_attr.has_argument ("max")) {
54 1549 : int val = int.parse (literal_value);
55 1549 : return (val >= int_attr.get_integer ("min") && val <= int_attr.get_integer ("max"));
56 : } else {
57 : // assume to be compatible if the target type doesn't specify limits
58 166328 : return true;
59 : }
60 : }
61 510749 : } else if (target_type.type_symbol is Enum && (literal_type_name == "int" || literal_type_name == "uint")) {
62 : // allow implicit conversion from 0 to enum and flags types
63 29862 : if (int.parse (literal_value) == 0) {
64 196173 : return true;
65 : }
66 : }
67 :
68 482420 : return base.compatible (target_type);
69 : }
70 : }
|