]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add limited support for derived compact classes
authorJürg Billeter <j@bitron.ch>
Tue, 28 Jul 2009 16:50:25 +0000 (18:50 +0200)
committerJürg Billeter <j@bitron.ch>
Tue, 28 Jul 2009 17:25:46 +0000 (19:25 +0200)
Fixes bug 578603.

codegen/valaccodemethodcallmodule.vala
codegen/valaccodemethodmodule.vala
codegen/valagtypemodule.vala
vala/valaclass.vala

index 4cf641d4ee7d06cd30ad6d381f3217f3a46f9080..cd06e37446f8b30b771df0910b2244bee65546ca 100644 (file)
@@ -95,7 +95,9 @@ internal class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
 
                if (m is CreationMethod) {
                        if (context.profile == Profile.GOBJECT) {
-                               ccall.add_argument (new CCodeIdentifier ("object_type"));
+                               if (!((Class) m.parent_symbol).is_compact) {
+                                       ccall.add_argument (new CCodeIdentifier ("object_type"));
+                               }
                        } else {
                                ccall.add_argument (new CCodeIdentifier ("self"));
                        }
index d8342a6f113dab0b0d4639e45d5c6181dc1350de..ac71cae3bd40d0606d77f6e8d56ad8be917e0eff 100644 (file)
@@ -530,15 +530,24 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
                                                }
                                        } else if (current_type_symbol is Class) {
                                                var cl = (Class) m.parent_symbol;
-                                               var cdecl = new CCodeDeclaration (cl.get_cname () + "*");
-                                               var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
-                                               ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
-                                               cdecl.add_declarator (new CCodeVariableDeclarator ("self", ccall));
-                                               cinit.append (cdecl);
+                                               var cdeclaration = new CCodeDeclaration (cl.get_cname () + "*");
+                                               var cdecl = new CCodeVariableDeclarator ("self");
+                                               cdeclaration.add_declarator (cdecl);
+                                               cinit.append (cdeclaration);
 
-                                               var cinitcall = new CCodeFunctionCall (new CCodeIdentifier ("%s_instance_init".printf (cl.get_lower_case_cname (null))));
-                                               cinitcall.add_argument (new CCodeIdentifier ("self"));
-                                               cinit.append (new CCodeExpressionStatement (cinitcall));
+                                               if (!((CreationMethod) m).chain_up) {
+                                                       // TODO implicitly chain up to base class as in add_object_creation
+                                                       var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
+                                                       ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
+                                                       cdecl.initializer = ccall;
+                                               }
+
+                                               if (cl.base_class == null) {
+                                                       // derived compact classes do not have fields
+                                                       var cinitcall = new CCodeFunctionCall (new CCodeIdentifier ("%s_instance_init".printf (cl.get_lower_case_cname (null))));
+                                                       cinitcall.add_argument (new CCodeIdentifier ("self"));
+                                                       cinit.append (new CCodeExpressionStatement (cinitcall));
+                                               }
                                        } else {
                                                var st = (Struct) m.parent_symbol;
 
index 81573a6906cb0348803ef495b2990419a4a78bc0..18c44fd77c6a9ddc2cbb963c7ca4b327008cd73a 100644 (file)
@@ -87,7 +87,11 @@ internal class Vala.GTypeModule : GErrorModule {
                        decl_space.add_type_declaration (new CCodeNewline ());
                }
 
-               decl_space.add_type_declaration (new CCodeTypeDefinition ("struct _%s".printf (cl.get_cname ()), new CCodeVariableDeclarator (cl.get_cname ())));
+               if (cl.is_compact && cl.base_class != null) {
+                       decl_space.add_type_declaration (new CCodeTypeDefinition (cl.base_class.get_cname (), new CCodeVariableDeclarator (cl.get_cname ())));
+               } else {
+                       decl_space.add_type_declaration (new CCodeTypeDefinition ("struct _%s".printf (cl.get_cname ()), new CCodeVariableDeclarator (cl.get_cname ())));
+               }
 
                if (is_fundamental) {
                        var ref_fun = new CCodeFunction (cl.get_lower_case_cprefix () + "ref", "gpointer");
@@ -310,7 +314,10 @@ internal class Vala.GTypeModule : GErrorModule {
                if (cl.source_reference.comment != null) {
                        decl_space.add_type_definition (new CCodeComment (cl.source_reference.comment));
                }
-               decl_space.add_type_definition (instance_struct);
+               if (!cl.is_compact || cl.base_class == null) {
+                       // derived compact classes do not have a struct
+                       decl_space.add_type_definition (instance_struct);
+               }
 
                if (is_gtypeinstance) {
                        decl_space.add_type_definition (type_struct);
@@ -435,18 +442,20 @@ internal class Vala.GTypeModule : GErrorModule {
                        }
                        decl_space.add_type_member_declaration (prop_enum);
                } else {
-                       var function = new CCodeFunction (cl.get_lower_case_cprefix () + "free", "void");
-                       if (cl.access == SymbolAccessibility.PRIVATE) {
-                               function.modifiers = CCodeModifiers.STATIC;
-                       }
-
                        if (cl.has_private_fields) {
                                Report.error (cl.source_reference, "Private fields not supported in compact classes");
                        }
 
-                       function.add_parameter (new CCodeFormalParameter ("self", cl.get_cname () + "*"));
+                       if (cl.base_class == null) {
+                               var function = new CCodeFunction (cl.get_lower_case_cprefix () + "free", "void");
+                               if (cl.access == SymbolAccessibility.PRIVATE) {
+                                       function.modifiers = CCodeModifiers.STATIC;
+                               }
+
+                               function.add_parameter (new CCodeFormalParameter ("self", cl.get_cname () + "*"));
 
-                       decl_space.add_type_member_declaration (function);
+                               decl_space.add_type_member_declaration (function);
+                       }
                }
        }
 
@@ -612,31 +621,34 @@ internal class Vala.GTypeModule : GErrorModule {
                                source_type_member_definition.append (unref_fun);
                        }
                } else {
-                       add_instance_init_function (cl);
+                       if (cl.base_class == null) {
+                               // derived compact classes do not have fields
+                               add_instance_init_function (cl);
 
-                       var function = new CCodeFunction (cl.get_lower_case_cprefix () + "free", "void");
-                       if (cl.access == SymbolAccessibility.PRIVATE) {
-                               function.modifiers = CCodeModifiers.STATIC;
-                       }
+                               var function = new CCodeFunction (cl.get_lower_case_cprefix () + "free", "void");
+                               if (cl.access == SymbolAccessibility.PRIVATE) {
+                                       function.modifiers = CCodeModifiers.STATIC;
+                               }
 
-                       function.add_parameter (new CCodeFormalParameter ("self", cl.get_cname () + "*"));
+                               function.add_parameter (new CCodeFormalParameter ("self", cl.get_cname () + "*"));
 
-                       var cblock = new CCodeBlock ();
+                               var cblock = new CCodeBlock ();
 
-                       cblock.add_statement (instance_finalize_fragment);
+                               cblock.add_statement (instance_finalize_fragment);
 
-                       if (cl.destructor != null) {
-                               cblock.add_statement (cl.destructor.ccodenode);
-                       }
+                               if (cl.destructor != null) {
+                                       cblock.add_statement (cl.destructor.ccodenode);
+                               }
 
-                       var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
-                       ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
-                       ccall.add_argument (new CCodeIdentifier ("self"));
-                       cblock.add_statement (new CCodeExpressionStatement (ccall));
+                               var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
+                               ccall.add_argument (new CCodeIdentifier (cl.get_cname ()));
+                               ccall.add_argument (new CCodeIdentifier ("self"));
+                               cblock.add_statement (new CCodeExpressionStatement (ccall));
 
-                       function.block = cblock;
+                               function.block = cblock;
 
-                       source_type_member_definition.append (function);
+                               source_type_member_definition.append (function);
+                       }
                }
 
                current_symbol = old_symbol;
index 51efba31b7a2d82490a25c57930a52d95ef5efc7..d2bb81c840a1b5e0d063000d5ead0e59682d84f1 100644 (file)
@@ -825,6 +825,9 @@ public class Vala.Class : ObjectTypeSymbol {
 
        public override string? get_free_function () {
                if (free_function == null) {
+                       if (base_class != null) {
+                               return base_class.get_free_function ();
+                       }
                        free_function = get_default_free_function ();
                }
                return free_function;
@@ -1007,6 +1010,15 @@ public class Vala.Class : ObjectTypeSymbol {
                                        Report.error (source_reference, "compact classes `%s` may not implement interfaces".printf (get_full_name ()));
                                }
                        }
+
+                       if (base_class != null) {
+                               foreach (Field f in fields) {
+                                       if (f.binding == MemberBinding.INSTANCE) {
+                                               error = true;
+                                               Report.error (source_reference, "derived compact classes may not have instance fields");
+                                       }
+                               }
+                       }
                }
 
                /* gather all prerequisites */