+2006-06-29 Jürg Billeter <j@bitron.ch>
+
+ * 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 <j@bitron.ch>
* vala/valacodenode.vala, vala/valaexpression.vala: add interface
return "g_object_ref";
}
- public override string get_free_function () {
+ public override string get_unref_function () {
return "g_object_unref";
}
}
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 ();
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"));
}
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 ();
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<string> 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<string> get_cheader_filenames () {
if (cheader_filenames == null) {
/* default to header filenames of the namespace */
}
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<string> cheader_filenames;
}
}
List<Field> fields;
List<Method> 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) {
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") {
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 () {
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")]
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<G> {
[ReturnsModifiedPointer ()]
public void append (ref G data);
[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<K,V> {
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);