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
20 * Jürg Billeter <j@bitron.ch>
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) {
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
;
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
= literal_value
.to_int ();
56 return (val
>= int_attr
.get_integer ("min") && val
<= int_attr
.get_integer ("max"));
58 // assume to be compatible if the target type doesn't specify limits
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 (literal_value
.to_int () == 0) {
69 return base.compatible (target_type
);