codegen: Fix floating reference regression with Variants
[vala-gnome.git] / vala / valaintegertype.vala
blob53845455a478b280e99f7546cbf2872672ed7ab2
1 /* valaintegertype.vala
3 * Copyright (C) 2008-2009 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>
23 using GLib;
25 /**
26 * An integer type.
28 public class Vala.IntegerType : ValueType {
29 string? literal_value;
30 string? literal_type_name;
32 public IntegerType (Struct type_symbol, string? literal_value = null, string? literal_type_name = null) {
33 base (type_symbol);
34 this.literal_value = literal_value;
35 this.literal_type_name = literal_type_name;
38 public override DataType copy () {
39 var result = new IntegerType ((Struct) type_symbol, literal_value, literal_type_name);
40 result.source_reference = source_reference;
41 result.value_owned = value_owned;
42 result.nullable = nullable;
43 return result;
46 public override bool compatible (DataType target_type) {
47 if (target_type.data_type is Struct && literal_type_name == "int") {
48 // int literals are implicitly convertible to integer types
49 // of a lower rank if the value of the literal is within
50 // the range of the target type
51 var target_st = (Struct) target_type.data_type;
52 if (target_st.is_integer_type ()) {
53 var int_attr = target_st.get_attribute ("IntegerType");
54 if (int_attr != null && int_attr.has_argument ("min") && int_attr.has_argument ("max")) {
55 int val = int.parse (literal_value);
56 return (val >= int_attr.get_integer ("min") && val <= int_attr.get_integer ("max"));
57 } else {
58 // assume to be compatible if the target type doesn't specify limits
59 return true;
62 } else if (target_type.data_type is Enum && literal_type_name == "int") {
63 // allow implicit conversion from 0 to enum and flags types
64 if (int.parse (literal_value) == 0) {
65 return true;
69 return base.compatible (target_type);