Commit bb562fba authored by Raffaele Sandrini's avatar Raffaele Sandrini Committed by Raffaele Sandrini

add support for default values of types use default values for

2007-03-22  Raffaele Sandrini  <rasa@gmx.ch>
	* vala/valaflags.vala, vala/valaenum.vala, vala/valadatatype.vala,
	  vala/valastruct.vala: add support for default values of types
	* vala/valacodegenerator.vala: use default values for initialization
	* vapi/glib-2.0.vala: add default values for common types

svn path=/trunk/; revision=265
parent e9c9eaed
2007-03-22 Raffaele Sandrini <rasa@gmx.ch>
* vala/valaflags.vala, vala/valaenum.vala, vala/valadatatype.vala,
vala/valastruct.vala: add support for default values of types
* vala/valacodegenerator.vala: use default values for initialization
* vapi/glib-2.0.vala: add default values for common types
2007-03-22 Jürg Billeter <j@bitron.ch>
* configure.ac: Post-release version bump
......
......@@ -906,17 +906,17 @@ public class Vala.CodeGenerator : CodeVisitor {
cspec.call = new CCodeIdentifier ("g_param_spec_uint");
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("G_MAXUINT"));
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("0U"));
} else if (prop.type_reference.data_type == long_type.data_type) {
cspec.call = new CCodeIdentifier ("g_param_spec_long");
cspec.add_argument (new CCodeConstant ("G_MINLONG"));
cspec.add_argument (new CCodeConstant ("G_MAXLONG"));
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("0L"));
} else if (prop.type_reference.data_type == ulong_type.data_type) {
cspec.call = new CCodeIdentifier ("g_param_spec_ulong");
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("G_MAXULONG"));
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("0UL"));
} else if (prop.type_reference.data_type == bool_type.data_type) {
cspec.call = new CCodeIdentifier ("g_param_spec_boolean");
cspec.add_argument (new CCodeConstant ("FALSE"));
......@@ -924,12 +924,12 @@ public class Vala.CodeGenerator : CodeVisitor {
cspec.call = new CCodeIdentifier ("g_param_spec_float");
cspec.add_argument (new CCodeConstant ("-G_MAXFLOAT"));
cspec.add_argument (new CCodeConstant ("G_MAXFLOAT"));
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("0.0F"));
} else if (prop.type_reference.data_type == double_type.data_type) {
cspec.call = new CCodeIdentifier ("g_param_spec_double");
cspec.add_argument (new CCodeConstant ("-G_MAXDOUBLE"));
cspec.add_argument (new CCodeConstant ("G_MAXDOUBLE"));
cspec.add_argument (new CCodeConstant ("0"));
cspec.add_argument (new CCodeConstant ("0.0"));
} else {
cspec.call = new CCodeIdentifier ("g_param_spec_pointer");
}
......@@ -1213,22 +1213,8 @@ public class Vala.CodeGenerator : CodeVisitor {
if (ret_type.is_reference_type ()) {
ccheck.add_argument (new CCodeConstant ("NULL"));
} else if (ret_type == bool_type.data_type) {
ccheck.add_argument (new CCodeConstant ("FALSE"));
} else if (ret_type == char_type.data_type ||
ret_type == unichar_type.data_type ||
ret_type == short_type.data_type ||
ret_type == ushort_type.data_type ||
ret_type == int_type.data_type ||
ret_type == uint_type.data_type ||
ret_type == long_type.data_type ||
ret_type == ulong_type.data_type ||
ret_type == int64_type.data_type ||
ret_type == uint64_type.data_type ||
ret_type == double_type.data_type ||
ret_type == float_type.data_type ||
ret_type is Enum || ret_type is Flags) {
ccheck.add_argument (new CCodeConstant ("0"));
} else if (ret_type.get_default_value () != null) {
ccheck.add_argument (new CCodeConstant (ret_type.get_default_value ()));
} else {
Report.warning (method_node.source_reference, "not supported return type for runtime type checks");
return new CCodeExpressionStatement (new CCodeConstant ("0"));
......@@ -2021,6 +2007,16 @@ public class Vala.CodeGenerator : CodeVisitor {
public override void visit_empty_statement (EmptyStatement! stmt) {
stmt.ccodenode = new CCodeEmptyStatement ();
}
private bool struct_has_instance_fields (Struct! st) {
foreach (Field f in st.get_fields ()) {
if (f.instance) {
return true;
}
}
return false;
}
public override void visit_declaration_statement (DeclarationStatement! stmt) {
/* split declaration statement as var declarators
......@@ -2035,18 +2031,27 @@ public class Vala.CodeGenerator : CodeVisitor {
cfrag.append (cdecl);
/* try to initialize uninitialized variables */
if (decl.initializer == null && decl.type_reference.data_type is Struct) {
var st = (Struct) decl.type_reference.data_type;
if (!st.is_reference_type () && st.get_fields ().length () > 0) {
if (decl.type_reference.data_type.is_reference_type ()) {
((CCodeVariableDeclarator) decl.ccodenode).initializer = new CCodeConstant ("NULL");
} else if (decl.type_reference.data_type.get_default_value () != null) {
((CCodeVariableDeclarator) decl.ccodenode).initializer = new CCodeConstant (decl.type_reference.data_type.get_default_value ());
} else if (decl.type_reference.data_type is Struct &&
struct_has_instance_fields ((Struct) decl.type_reference.data_type)) {
var st = (Struct) decl.type_reference.data_type;
/* memset needs string.h */
string_h_needed = true;
var czero = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
czero.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (decl.name)));
czero.add_argument (new CCodeConstant ("0"));
czero.add_argument (new CCodeIdentifier ("sizeof (%s)".printf (decl.type_reference.get_cname ())));
cfrag.append (new CCodeExpressionStatement (czero));
} else {
Report.warning (decl.source_reference, "unable to initialize a variable of type `%s'".printf (decl.type_reference.data_type.symbol.get_full_name ()));
}
}
}
......
......@@ -201,7 +201,17 @@ public abstract class Vala.DataType : CodeNode {
public virtual ref string get_lower_case_cprefix () {
return null;
}
/**
* Returns the default value for the given type. Returning null means
* there is no default value (i.e. not that the default name is NULL).
*
* @return the name of the default value
*/
public virtual string get_default_value () {
return null;
}
/**
* Returns a list of C header filenames users of this data type must
* include.
......
......@@ -144,5 +144,9 @@ public class Vala.Enum : DataType {
public override string get_set_value_function () {
return "g_value_set_enum";
}
public override string get_default_value () {
return "0";
}
}
......@@ -86,4 +86,8 @@ public class Vala.Flags : DataType {
public override string get_set_value_function () {
return "g_value_set_flags";
}
public override string get_default_value () {
return "0";
}
}
......@@ -47,6 +47,7 @@ public class Vala.Struct : DataType {
private string marshaller_type_name;
private string get_value_function;
private string set_value_function;
private string default_value = null;
/**
* Specifies the default construction method.
......@@ -260,6 +261,9 @@ public class Vala.Struct : DataType {
if (a.has_argument ("set_value_function")) {
set_set_value_function (a.get_string ("set_value_function"));
}
if (a.has_argument ("default_value")) {
set_default_value (a.get_string ("default_value"));
}
}
private void process_ref_type_attribute (Attribute! a) {
......@@ -391,6 +395,14 @@ public class Vala.Struct : DataType {
set_value_function = function;
}
public override string get_default_value () {
return default_value;
}
private void set_default_value (string! value) {
default_value = value;
}
/**
* Adds the specified struct to the list of base types of this struct.
*
......
......@@ -21,19 +21,19 @@
* Raffaele Sandrini <rasa@gmx.ch>
*/
[CCode (cname = "gboolean", cheader_filename = "glib.h", type_id = "G_TYPE_BOOLEAN", marshaller_type_name = "BOOLEAN", get_value_function = "g_value_get_boolean", set_value_function = "g_value_set_boolean")]
[CCode (cname = "gboolean", cheader_filename = "glib.h", type_id = "G_TYPE_BOOLEAN", marshaller_type_name = "BOOLEAN", get_value_function = "g_value_get_boolean", set_value_function = "g_value_set_boolean", default_value = "FALSE")]
public struct bool {
}
[CCode (cname = "gpointer", cheader_filename = "glib.h", type_id = "G_TYPE_POINTER", marshaller_type_name = "POINTER", get_value_function = "g_value_get_pointer", set_value_function = "g_value_set_pointer")]
[CCode (cname = "gpointer", cheader_filename = "glib.h", type_id = "G_TYPE_POINTER", marshaller_type_name = "POINTER", get_value_function = "g_value_get_pointer", set_value_function = "g_value_set_pointer", default_value = "NULL")]
public struct pointer {
}
[CCode (cname = "gconstpointer", cheader_filename = "glib.h", type_id = "G_TYPE_POINTER", marshaller_type_name = "POINTER", get_value_function = "g_value_get_pointer", set_value_function = "g_value_set_pointer")]
[CCode (cname = "gconstpointer", cheader_filename = "glib.h", type_id = "G_TYPE_POINTER", marshaller_type_name = "POINTER", get_value_function = "g_value_get_pointer", set_value_function = "g_value_set_pointer", default_value = "NULL")]
public struct constpointer {
}
[CCode (cname = "gchar", 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")]
[CCode (cname = "gchar", 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)]
public struct char {
[InstanceLast ()]
......@@ -41,7 +41,7 @@ public struct char {
public ref string! to_string (string! format = "%hhi");
}
[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")]
[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 = 2)]
public struct uchar {
[InstanceLast ()]
......@@ -49,7 +49,7 @@ public struct uchar {
public ref string! to_string (string! format = "%hhu");
}
[CCode (cname = "gint", cheader_filename = "glib.h", type_id = "G_TYPE_INT", marshaller_type_name = "INT", get_value_function = "g_value_get_int", set_value_function = "g_value_set_int")]
[CCode (cname = "gint", cheader_filename = "glib.h", type_id = "G_TYPE_INT", marshaller_type_name = "INT", get_value_function = "g_value_get_int", set_value_function = "g_value_set_int", default_value = "0")]
[IntegerType (rank = 9)]
public struct int {
[CCode (cname = "G_MININT")]
......@@ -62,7 +62,7 @@ public struct int {
public ref string! to_string (string! format = "%i");
}
[CCode (cname = "guint", cheader_filename = "glib.h", type_id = "G_TYPE_UINT", marshaller_type_name = "UINT", get_value_function = "g_value_get_uint", set_value_function = "g_value_set_uint")]
[CCode (cname = "guint", cheader_filename = "glib.h", type_id = "G_TYPE_UINT", marshaller_type_name = "UINT", get_value_function = "g_value_get_uint", set_value_function = "g_value_set_uint", default_value = "0U")]
[IntegerType (rank = 10)]
public struct uint {
[CCode (cname = "0")]
......@@ -75,7 +75,7 @@ public struct uint {
public ref string! to_string (string! format = "%u");
}
[CCode (cname = "gshort", cheader_filename = "glib.h")]
[CCode (cname = "gshort", cheader_filename = "glib.h", default_value = "0")]
[IntegerType (rank = 5)]
public struct short {
[CCode (cname = "G_MINSHORT")]
......@@ -88,7 +88,7 @@ public struct short {
public ref string! to_string (string! format = "%hi");
}
[CCode (cname = "gushort", cheader_filename = "glib.h")]
[CCode (cname = "gushort", cheader_filename = "glib.h", default_value = "0U")]
[IntegerType (rank = 6)]
public struct ushort {
[CCode (cname = "0U")]
......@@ -101,7 +101,7 @@ public struct ushort {
public ref string! to_string (string! format = "%hu");
}
[CCode (cname = "glong", cheader_filename = "glib.h", type_id = "G_TYPE_LONG", marshaller_type_name = "LONG", get_value_function = "g_value_get_long", set_value_function = "g_value_set_long")]
[CCode (cname = "glong", cheader_filename = "glib.h", type_id = "G_TYPE_LONG", marshaller_type_name = "LONG", get_value_function = "g_value_get_long", set_value_function = "g_value_set_long", default_value = "0L")]
[IntegerType (rank = 14)]
public struct long {
[CCode (cname = "G_MINLONG")]
......@@ -114,7 +114,7 @@ public struct long {
public ref string! to_string (string! format = "%li");
}
[CCode (cname = "gulong", cheader_filename = "glib.h", type_id = "G_TYPE_ULONG", marshaller_type_name = "ULONG", get_value_function = "g_value_get_ulong", set_value_function = "g_value_set_ulong")]
[CCode (cname = "gulong", cheader_filename = "glib.h", type_id = "G_TYPE_ULONG", marshaller_type_name = "ULONG", get_value_function = "g_value_get_ulong", set_value_function = "g_value_set_ulong", default_value = "0UL")]
[IntegerType (rank = 15)]
public struct ulong {
[CCode (cname = "0UL")]
......@@ -127,7 +127,7 @@ public struct ulong {
public ref string! to_string (string! format = "%lu");
}
[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")]
[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 = 3)]
public struct int8 {
[CCode (cname = "G_MININT8")]
......@@ -139,7 +139,7 @@ public struct int8 {
public string! to_string (string! format = "%hhi");
}
[CCode (cname = "guint8", cheader_filename = "glib.h")]
[CCode (cname = "guint8", cheader_filename = "glib.h", default_value = "0U")]
[IntegerType (rank = 4)]
public struct uint8 {
[CCode (cname = "0U")]
......@@ -151,7 +151,7 @@ public struct uint8 {
public ref string! to_string (string! format = "%hhu");
}
[CCode (cname = "gint16", cheader_filename = "glib.h")]
[CCode (cname = "gint16", cheader_filename = "glib.h", default_value = "0")]
[IntegerType (rank = 7)]
public struct int16 {
[CCode (cname = "G_MININT16")]
......@@ -163,7 +163,7 @@ public struct int16 {
public ref string! to_string (string! format = "%hi");
}
[CCode (cname = "guint16", cheader_filename = "glib.h")]
[CCode (cname = "guint16", cheader_filename = "glib.h", default_value = "0U")]
[IntegerType (rank = 8)]
public struct uint16 {
[CCode (cname = "0U")]
......@@ -175,7 +175,7 @@ public struct uint16 {
public ref string! to_string (string! format = "%hu");
}
[CCode (cname = "gint32", cheader_filename = "glib.h")]
[CCode (cname = "gint32", cheader_filename = "glib.h", default_value = "0")]
[IntegerType (rank = 11)]
public struct int32 {
[CCode (cname = "G_MININT32")]
......@@ -187,7 +187,7 @@ public struct int32 {
public ref string! to_string (string! format = "%i");
}
[CCode (cname = "guint32", cheader_filename = "glib.h")]
[CCode (cname = "guint32", cheader_filename = "glib.h", default_value = "0U")]
[IntegerType (rank = 12)]
public struct uint32 {
[CCode (cname = "0U")]
......@@ -200,7 +200,7 @@ public struct uint32 {
public ref string! to_string (string! format = "%u");
}
[CCode (cname = "gint64", cheader_filename = "glib.h", type_id = "G_TYPE_INT64", marshaller_type_name = "INT64", get_value_function = "g_value_get_int64", set_value_function = "g_value_set_int64")]
[CCode (cname = "gint64", cheader_filename = "glib.h", type_id = "G_TYPE_INT64", marshaller_type_name = "INT64", get_value_function = "g_value_get_int64", set_value_function = "g_value_set_int64", default_value = "0LL")]
[IntegerType (rank = 16)]
public struct int64 {
[CCode (cname = "G_MININT64")]
......@@ -213,7 +213,7 @@ public struct int64 {
public ref string! to_string (string! format = "%lli");
}
[CCode (cname = "guint64", cheader_filename = "glib.h", type_id = "G_TYPE_UINT64", marshaller_type_name = "UINT64", get_value_function = "g_value_get_uint64", set_value_function = "g_value_set_uint64")]
[CCode (cname = "guint64", cheader_filename = "glib.h", type_id = "G_TYPE_UINT64", marshaller_type_name = "UINT64", get_value_function = "g_value_get_uint64", set_value_function = "g_value_set_uint64", default_value = "0ULL")]
[IntegerType (rank = 17)]
public struct uint64 {
[CCode (cname = "0ULL")]
......@@ -226,7 +226,7 @@ public struct uint64 {
public ref string! to_string (string! format = "%llu");
}
[CCode (cname = "float", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_FLOAT", marshaller_type_name = "FLOAT", get_value_function = "g_value_get_float", set_value_function = "g_value_set_float")]
[CCode (cname = "float", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_FLOAT", marshaller_type_name = "FLOAT", get_value_function = "g_value_get_float", set_value_function = "g_value_set_float", default_value = "0.0F")]
[FloatingType (rank = 1)]
public struct float {
[CCode (cname = "FLT_MANT_DIG")]
......@@ -269,7 +269,7 @@ public struct float {
public ref string! to_string (string! format = "%g");
}
[CCode (cname = "double", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_DOUBLE", marshaller_type_name = "DOUBLE", get_value_function = "g_value_get_double", set_value_function = "g_value_set_double")]
[CCode (cname = "double", cheader_filename = "glib.h,float.h,math.h", type_id = "G_TYPE_DOUBLE", marshaller_type_name = "DOUBLE", get_value_function = "g_value_get_double", set_value_function = "g_value_set_double", default_value = "0.0")]
[FloatingType (rank = 2)]
public struct double {
[CCode (cname = "DBL_MANT_DIG")]
......@@ -312,7 +312,7 @@ public struct double {
public ref string! to_string (string! format = "%g");
}
[CCode (cname = "gunichar", cheader_filename = "glib.h", get_value_function = "g_value_get_int", set_value_function = "g_value_set_int")]
[CCode (cname = "gunichar", cheader_filename = "glib.h", get_value_function = "g_value_get_int", set_value_function = "g_value_set_int", default_value = "0U")]
[IntegerType (rank = 13)]
public struct unichar {
[CCode (cname = "g_unichar_isalnum")]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment