--- /dev/null
+/* valaintegertype.vala
+ *
+ * Copyright (C) 2008 Jürg Billeter
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Author:
+ * Jürg Billeter <j@bitron.ch>
+ */
+
+using GLib;
+
+/**
+ * An integer type.
+ */
+public class Vala.IntegerType : ValueType {
+ public IntegerLiteral literal { get; set; }
+
+ public IntegerType (construct Typesymbol! type_symbol) {
+ data_type = type_symbol;
+ }
+
+ public override bool compatible (DataType! target_type) {
+ if (target_type.data_type is Struct && literal.get_type_name () == "int") {
+ // int literals are implicitly convertible to integer types
+ // of a lower rank if the value of the literal is within
+ // the range of the target type
+ var target_st = (Struct) target_type.data_type;
+ if (target_st.is_integer_type ()) {
+ var int_attr = target_st.get_attribute ("IntegerType");
+ if (int_attr.has_argument ("min") && int_attr.has_argument ("max")) {
+ int val = literal.value.to_int ();
+ return (val >= int_attr.get_integer ("min") && val <= int_attr.get_integer ("max"));
+ }
+ }
+ }
+
+ return base.compatible (target_type);
+ }
+}
[SimpleType]
[CCode (cname = "gchar", cprefix = "g_ascii_", cheader_filename = "glib.h", type_id = "G_TYPE_CHAR", marshaller_type_name = "CHAR", get_value_function = "g_value_get_char", set_value_function = "g_value_set_char", default_value = "\'\\0\'")]
-[IntegerType (rank = 2)]
+[IntegerType (rank = 2, min = 0, max = 127)]
public struct char {
[InstanceLast ()]
[CCode (cname = "g_strdup_printf")]
[SimpleType]
[CCode (cname = "guchar", cheader_filename = "glib.h", type_id = "G_TYPE_UCHAR", marshaller_type_name = "UCHAR", get_value_function = "g_value_get_uchar", set_value_function = "g_value_set_uchar", default_value = "\'\\0\'")]
-[IntegerType (rank = 3)]
+[IntegerType (rank = 3, min = 0, max = 255)]
public struct uchar {
[InstanceLast ()]
[CCode (cname = "g_strdup_printf")]
[SimpleType]
[CCode (cname = "gshort", cheader_filename = "glib.h", default_value = "0")]
-[IntegerType (rank = 4)]
+[IntegerType (rank = 4, min = -32768, max = 32767)]
public struct short {
[CCode (cname = "G_MINSHORT")]
public static short MIN;
[SimpleType]
[CCode (cname = "gushort", cheader_filename = "glib.h", default_value = "0U")]
-[IntegerType (rank = 5)]
+[IntegerType (rank = 5, min = 0, max = 65535)]
public struct ushort {
[CCode (cname = "0U")]
public static ushort MIN;
[SimpleType]
[CCode (cname = "gint8", cheader_filename = "glib.h", type_id = "G_TYPE_CHAR", marshaller_type_name = "CHAR", get_value_function = "g_value_get_char", set_value_function = "g_value_set_char", default_value = "0")]
-[IntegerType (rank = 1)]
+[IntegerType (rank = 1, min = -128, max = 127)]
public struct int8 {
[CCode (cname = "G_MININT8")]
public static int8 MIN;
[SimpleType]
[CCode (cname = "guint8", cheader_filename = "glib.h", type_id = "G_TYPE_UCHAR", marshaller_type_name = "UCHAR", get_value_function = "g_value_get_uchar", set_value_function = "g_value_set_uchar", default_value = "0U")]
-[IntegerType (rank = 3)]
+[IntegerType (rank = 3, min = 0, max = 255)]
public struct uint8 {
[CCode (cname = "0U")]
public static uint8 MIN;
[SimpleType]
[CCode (cname = "gint16", cheader_filename = "glib.h", default_value = "0")]
-[IntegerType (rank = 4)]
+[IntegerType (rank = 4, min = -32768, max = 32767)]
public struct int16 {
[CCode (cname = "G_MININT16")]
public static int16 MIN;
[SimpleType]
[CCode (cname = "guint16", cheader_filename = "glib.h", default_value = "0U")]
-[IntegerType (rank = 5)]
+[IntegerType (rank = 5, min = 0, max = 65535)]
public struct uint16 {
[CCode (cname = "0U")]
public static uint16 MIN;