From: Jürg Billeter Date: Thu, 29 Jun 2006 21:22:17 +0000 (+0000) Subject: distinguish between ref_function/unref_function and X-Git-Tag: VALA_0_0_1~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=027deaebab1124c9e5bfec6705b900205647b5cf;p=thirdparty%2Fvala.git distinguish between ref_function/unref_function and 2006-06-29 Jürg Billeter * vala/valacodegenerator.vala, vala/valaclass.vala, vala/valadatatype.vala, vala/valastruct.vala: distinguish between ref_function/unref_function and dup_function/free_function * vala/valadatatype.vala: add interface documentation * vapi/glib-2.0.vala: use dup_function attribute, keep ref_function for compatibility svn path=/trunk/; revision=57 --- diff --git a/vala/ChangeLog b/vala/ChangeLog index e54900534..951cf3c57 100644 --- a/vala/ChangeLog +++ b/vala/ChangeLog @@ -1,3 +1,12 @@ +2006-06-29 Jürg Billeter + + * vala/valacodegenerator.vala, vala/valaclass.vala, + vala/valadatatype.vala, vala/valastruct.vala: distinguish between + ref_function/unref_function and dup_function/free_function + * vala/valadatatype.vala: add interface documentation + * vapi/glib-2.0.vala: use dup_function attribute, keep ref_function for + compatibility + 2006-06-29 Jürg Billeter * vala/valacodenode.vala, vala/valaexpression.vala: add interface diff --git a/vala/vala/valaclass.vala b/vala/vala/valaclass.vala index e9267d96d..f02f0e0ac 100644 --- a/vala/vala/valaclass.vala +++ b/vala/vala/valaclass.vala @@ -221,7 +221,7 @@ namespace Vala { return "g_object_ref"; } - public override string get_free_function () { + public override string get_unref_function () { return "g_object_unref"; } } diff --git a/vala/vala/valacodegenerator.vala b/vala/vala/valacodegenerator.vala index bd3eb34b5..05acccaa8 100644 --- a/vala/vala/valacodegenerator.vala +++ b/vala/vala/valacodegenerator.vala @@ -1039,17 +1039,23 @@ namespace Vala { var cisnull = new CCodeBinaryExpression (operator = CCodeBinaryOperator.EQUALITY, left = cvar, right = new CCodeConstant (name = "NULL")); - var free_func = type.type.get_free_function (); + string unref_function; + if (type.type.is_reference_counting ()) { + unref_function = type.type.get_unref_function (); + } else { + unref_function = type.type.get_free_function (); + } + if (type.array && type.type.name == "string") { - free_func = "g_strfreev"; + unref_function = "g_strfreev"; } - var ccall = new CCodeFunctionCall (call = new CCodeIdentifier (name = free_func)); + var ccall = new CCodeFunctionCall (call = new CCodeIdentifier (name = unref_function)); ccall.add_argument (cvar); /* set freed references to NULL to prevent further use */ var ccomma = new CCodeCommaExpression (); - if (free_func == "g_list_free") { + if (unref_function == "g_list_free") { bool is_ref = false; bool is_class = false; var type_args = type.get_type_arguments (); @@ -1069,7 +1075,7 @@ namespace Vala { cunrefcall.add_argument (new CCodeConstant (name = "NULL")); ccomma.inner.append (cunrefcall); } - } else if (free_func == "g_string_free") { + } else if (unref_function == "g_string_free") { ccall.add_argument (new CCodeConstant (name = "TRUE")); } @@ -1630,8 +1636,15 @@ namespace Vala { var ctemp = new CCodeIdentifier (name = decl.name); var cisnull = new CCodeBinaryExpression (operator = CCodeBinaryOperator.EQUALITY, left = ctemp, right = new CCodeConstant (name = "NULL")); + + string ref_function; + if (expr.static_type.type.is_reference_counting ()) { + ref_function = expr.static_type.type.get_ref_function (); + } else { + ref_function = expr.static_type.type.get_dup_function (); + } - var ccall = new CCodeFunctionCall (call = new CCodeIdentifier (name = expr.static_type.type.get_ref_function ())); + var ccall = new CCodeFunctionCall (call = new CCodeIdentifier (name = ref_function)); ccall.add_argument (ctemp); var ccomma = new CCodeCommaExpression (); diff --git a/vala/vala/valadatatype.vala b/vala/vala/valadatatype.vala index 9c5b1a482..47b822e14 100644 --- a/vala/vala/valadatatype.vala +++ b/vala/vala/valadatatype.vala @@ -23,22 +23,153 @@ using GLib; namespace Vala { + /** + * Represents a runtime data type. This data type may be defined in Vala + * source code or imported from an external library with a Vala API + * file. + */ public abstract class DataType : CodeNode { + /** + * The symbol name of this data type. + */ public string! name { get; set construct; } - public weak Namespace @namespace; + + /** + * Specifies the accessibility of the class. Public + * accessibility doesn't limit access. Default accessibility + * limits access to this program or library. Protected and + * private accessibility is not supported for types. + */ public MemberAccessibility access; + + /** + * The namespace containing this data type. + */ + public weak Namespace @namespace; + /** + * Returns the name of this data type as it is used in C code. + * + * @return the name to be used in C code + */ public abstract string get_cname (); - public abstract bool is_reference_type (); - public abstract bool is_reference_counting (); - public abstract string get_ref_function (); - public abstract string get_free_function (); - public abstract string get_type_id (); - public abstract ref string get_upper_case_cname (string infix); - public abstract ref string get_lower_case_cname (string infix); + /** + * Checks whether this data type has value or reference type + * semantics. + * + * @return true if this data type has reference type semantics + */ + public virtual bool is_reference_type () { + return false; + } - private List cheader_filenames; + /** + * Returns the C function name that duplicates instances of this + * data type. The specified C function must accept one argument + * referencing the instance of this data type and return a + * reference to the duplicate. + * + * @return the name of the C function if supported or null + * otherwise + */ + public virtual string get_dup_function () { + return null; + } + + /** + * Returns the C function name that frees instances of this + * data type. This is only valid for data types with reference + * type semantics that do not support reference counting. The + * specified C function must accept one argument pointing to the + * instance to be freed. + * + * @return the name of the C function or null if this data type + * is not a reference type or if it supports reference + * counting + */ + public virtual string get_free_function () { + return null; + } + + /** + * Checks whether this data type supports reference counting. + * This is only valid for reference types. + * + * @return true if this data type supports reference counting + */ + public virtual bool is_reference_counting () { + return false; + } + + /** + * Returns the C function name that increments the reference + * count of instances of this data type. This is only valid for + * data types supporting reference counting. The specified C + * function must accept one argument referencing the instance of + * this data type and return the reference. + * + * @return the name of the C function or null if this data type + * does not support reference counting + */ + public virtual string get_ref_function () { + return null; + } + + /** + * Returns the C function name that decrements the reference + * count of instances of this data type. This is only valid for + * data types supporting reference counting. The specified C + * function must accept one argument referencing the instance of + * this data type. + * + * @return the name of the C function or null if this data type + * does not support reference counting + */ + public virtual string get_unref_function () { + return null; + } + + /** + * Returns the C symbol representing the runtime type id for + * this data type. The specified symbol must express a + * registered GType. + * + * @return the name of the GType name in C code or null if this + * data type is not registered with GType + */ + public virtual string get_type_id () { + return null; + } + + /** + * Returns the C name of this data type in upper case. Words are + * separated by underscores. The upper case C name of the + * namespace is prefix of the result. + * + * @param infix a string to be placed between namespace and + * data type name or null + * @return the upper case name to be used in C code + */ + public abstract ref string! get_upper_case_cname (string infix); + + /** + * Returns the C name of this data type in lower case. Words are + * separated by underscores. The lower case C name of the + * namespace is prefix of the result. + * + * @param infix a string to be placed between namespace and + * data type name or null + * @return the lower case name to be used in C code + */ + public abstract ref string! get_lower_case_cname (string infix); + + /** + * Returns a list of C header filenames users of this data type + * must include. + * + * @return list of C header filenames for this data type + */ public ref List get_cheader_filenames () { if (cheader_filenames == null) { /* default to header filenames of the namespace */ @@ -48,9 +179,17 @@ namespace Vala { } return cheader_filenames.copy (); } - + + /** + * Adds a filename to the list of C header filenames users of + * this data type must include. + * + * @param filename a C header filename + */ public void add_cheader_filename (string! filename) { cheader_filenames.append (filename); } + + private List cheader_filenames; } } diff --git a/vala/vala/valastruct.vala b/vala/vala/valastruct.vala index 4d21ba0bc..ef8d40ffd 100644 --- a/vala/vala/valastruct.vala +++ b/vala/vala/valastruct.vala @@ -29,11 +29,11 @@ namespace Vala { List fields; List methods; - public string cname; - public string ref_function; - public string free_function; - public string type_id; - public string lower_case_csuffix; + string cname; + string dup_function; + string free_function; + string type_id; + string lower_case_csuffix; bool reference_type; public static ref Struct new (string! name, SourceReference source) { @@ -154,12 +154,12 @@ namespace Vala { private void process_ref_type_attribute (Attribute! a) { reference_type = true; foreach (NamedArgument arg in a.args) { - if (arg.name == "ref_function") { + if (arg.name == "dup_function") { /* this will already be checked during semantic analysis */ if (arg.argument is LiteralExpression) { var lit = ((LiteralExpression) arg.argument).literal; if (lit is StringLiteral) { - set_ref_function (((StringLiteral) lit).eval ()); + set_dup_function (((StringLiteral) lit).eval ()); } } } else if (arg.name == "free_function") { @@ -196,15 +196,15 @@ namespace Vala { return false; } - public override string get_ref_function () { - if (ref_function == null) { + public override string get_dup_function () { + if (dup_function == null) { Report.error (source_reference, "The type `%s` doesn't contain a copy function".printf (symbol.get_full_name ())); } - return ref_function; + return dup_function; } - public void set_ref_function (string! name) { - this.ref_function = name; + public void set_dup_function (string! name) { + this.dup_function = name; } public override string get_free_function () { diff --git a/vala/vapi/glib-2.0.vala b/vala/vapi/glib-2.0.vala index 6ac45abf8..9bf68abcb 100644 --- a/vala/vapi/glib-2.0.vala +++ b/vala/vapi/glib-2.0.vala @@ -112,7 +112,7 @@ public struct unichar { public unichar tolower (); } -[ReferenceType (ref_function = "g_strdup", free_function = "g_free", type_id = "G_TYPE_STRING")] +[ReferenceType (dup_function = "g_strdup", free_function = "g_free", type_id = "G_TYPE_STRING", ref_function = "g_strdup")] [CCode (cname = "char", cheader_filename = "string.h,glib.h")] public struct string { [CCode (cname = "g_str_has_suffix")] @@ -295,7 +295,7 @@ namespace GLib { string arg_description; } - [ReferenceType (ref_function = "g_list_copy", free_function = "g_list_free")] + [ReferenceType (dup_function = "g_list_copy", free_function = "g_list_free", ref_function = "g_list_copy")] public struct List { [ReturnsModifiedPointer ()] public void append (ref G data); @@ -343,7 +343,7 @@ namespace GLib { [CCode (cname = "strcmp")] public static GLib.CompareFunc strcmp; - [ReferenceType (ref_function = "g_hash_table_ref", free_function = "g_hash_table_unref")] + [ReferenceType (dup_function = "g_hash_table_ref", free_function = "g_hash_table_unref", ref_function = "g_hash_table_ref")] public struct HashTable { public static ref HashTable new (HashFunc hash_func, EqualFunc key_equal_func); public static ref HashTable new_full (HashFunc hash_func, EqualFunc key_equal_func, DestroyNotify key_destroy_func, DestroyNotify value_destroy_func);