]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add explicit copy method for arrays
authorLuca Bruno <lucabru@src.gnome.org>
Sat, 30 Jul 2011 14:41:34 +0000 (16:41 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 20 Oct 2016 10:43:48 +0000 (12:43 +0200)
https://bugzilla.gnome.org/show_bug.cgi?id=650663

codegen/valaccodememberaccessmodule.vala
codegen/valaccodemethodcallmodule.vala
tests/basic-types/arrays.vala
vala/Makefile.am
vala/valaarraycopymethod.vala [new file with mode: 0644]
vala/valaarraytype.vala

index af6fc65aab892349e5dfb0facdf648d4ebc01856..cfbd9d35bc7010966f1dabc6f51acd9773c31df1 100644 (file)
@@ -36,7 +36,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                if (expr.symbol_reference is Method) {
                        var m = (Method) expr.symbol_reference;
 
-                       if (!(m is DynamicMethod || m is ArrayMoveMethod || m is ArrayResizeMethod)) {
+                       if (!(m is DynamicMethod || m is ArrayMoveMethod || m is ArrayResizeMethod || m is ArrayCopyMethod)) {
                                generate_method_declaration (m, cfile);
 
                                if (!m.external && m.external_package) {
index e323bed75da263bf0baad1ea616d2b31b0549e49..2cd12b69f23afdf18499d67eb1ba17522411d1a9 100644 (file)
@@ -188,6 +188,9 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        in_arg_map.set (get_param_pos (0), new CCodeIdentifier (get_ccode_name (array_type.element_type)));
                } else if (m is ArrayMoveMethod) {
                        requires_array_move = true;
+               } else if (m is ArrayCopyMethod) {
+                       expr.target_value = copy_value (ma.inner.target_value, expr);
+                       return;
                }
 
                CCodeExpression instance = null;
index fece74999a97d2811dacd0bb2fd30054dbf6b106..7685304a6410c2e52ccbf85aaadfa1c384f0c385 100644 (file)
@@ -191,6 +191,13 @@ void test_void_array () {
        assert ((void*) null in a);
 }
 
+void test_explicit_copying () {
+       int[] a0 = { 1, 2, 3};
+       var a1 = a0.copy ();
+       assert (a1.length == 3);
+       assert (a0[1] == a1[1]);
+}
+
 void main () {
        test_integer_array ();
        test_string_array ();
@@ -202,4 +209,5 @@ void main () {
        test_delegate_array ();
        test_generics_array ();
        test_void_array ();
+       test_explicit_copying ();
 }
index 134d732aa9cd42ca749844af778fee77c9414c61..397594cdcb178fafe0b9cca714edff88f1f38fb1 100644 (file)
@@ -18,6 +18,7 @@ noinst_LTLIBRARIES = \
 
 libvalacore_la_VALASOURCES = \
        valaaddressofexpression.vala \
+       valaarraycopymethod.vala \
        valaarraycreationexpression.vala \
        valaarraylengthfield.vala \
        valaarraymovemethod.vala \
diff --git a/vala/valaarraycopymethod.vala b/vala/valaarraycopymethod.vala
new file mode 100644 (file)
index 0000000..97086cd
--- /dev/null
@@ -0,0 +1,38 @@
+/* valaarraycopymethod.vala
+ *
+ * Copyright (C) 2011  Luca Bruno
+ *
+ * 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:
+ *     Luca Bruno <lucabru@src.gnome.org>
+ */
+
+using GLib;
+
+/**
+ * Represents the Array.copy method.
+ */
+public class Vala.ArrayCopyMethod : Method {
+       /**
+        * Creates a new array copy method.
+        *
+        * @return newly created method
+        */
+       public ArrayCopyMethod (SourceReference source_reference) {
+               base ("copy", new InvalidType (), source_reference);
+               external = true;
+       }
+}
index f6d01329241ec2d83e6feffaea81b1ee6d207f74..0acd0dfbf52df3e46523c79209f2a75673cb8de2 100644 (file)
@@ -67,6 +67,7 @@ public class Vala.ArrayType : ReferenceType {
        private ArrayLengthField length_field;
        private ArrayResizeMethod resize_method;
        private ArrayMoveMethod move_method;
+       private ArrayCopyMethod copy_method;
 
        public ArrayType (DataType element_type, int rank, SourceReference? source_reference) {
                this.element_type = element_type;
@@ -84,6 +85,8 @@ public class Vala.ArrayType : ReferenceType {
                                return null;
                        }
                        return get_resize_method ();
+               } else if (member_name == "copy") {
+                       return get_copy_method ();
                }
                return null;
        }
@@ -145,6 +148,19 @@ public class Vala.ArrayType : ReferenceType {
                return move_method;
        }
 
+       private ArrayCopyMethod get_copy_method () {
+               if (copy_method == null) {
+                       copy_method = new ArrayCopyMethod (source_reference);
+
+                       copy_method.return_type = this.copy ();
+                       copy_method.return_type.value_owned = true;
+                       copy_method.access = SymbolAccessibility.PUBLIC;
+
+                       copy_method.set_attribute_string ("CCode", "cname", "_vala_array_copy");
+               }
+               return copy_method;
+       }
+
        public override DataType copy () {
                var result = new ArrayType (element_type.copy (), rank, source_reference);
                result.value_owned = value_owned;