Răsfoiți Sursa

Add Reference typedef to hint to hxcpp that result should not be cast

Hugh 9 ani în urmă
părinte
comite
9e16487fe9
4 a modificat fișierele cu 48 adăugiri și 6 ștergeri
  1. 15 1
      src/generators/gencpp.ml
  2. 1 1
      std/cpp/ConstPointer.hx
  3. 4 4
      std/cpp/Pointer.hx
  4. 28 0
      std/cpp/Reference.hx

+ 15 - 1
src/generators/gencpp.ml

@@ -1313,6 +1313,7 @@ type tcpp =
    | TCppPointer of string * tcpp
    | TCppRawPointer of string * tcpp
    | TCppFunction of tcpp list * tcpp * string
+   | TCppReference
    | TCppVoidStar
    | TCppDynamicArray
    | TCppObjectArray of tcpp
@@ -1536,6 +1537,7 @@ let rec s_tcpp = function
 and tcpp_to_string_suffix suffix tcpp = match tcpp with
    | TCppDynamic -> " ::Dynamic"
    | TCppObject -> " ::Dynamic"
+   | TCppReference -> " Reference<>"
    | TCppVoid -> "void"
    | TCppVoidStar -> "void *"
    | TCppVariant -> "::cpp::Variant"
@@ -1636,6 +1638,12 @@ let rec const_string_of expr =
 ;;
 
 
+let cpp_is_struct_access t =
+   match t with
+   | TCppInst (class_def) -> (has_meta_key class_def.cl_meta Meta.StructAccess)
+   | _ -> false
+;;
+
 
 let cpp_is_dynamic_type = function
    | TCppDynamic | TCppObject | TCppVariant | TCppWrapped _ | TCppGlobal | TCppNull
@@ -1720,6 +1728,8 @@ let rec cpp_type_of ctx haxe_type =
             cpp_function_type_of ctx function_type abi;
       | (["cpp"],"Callable"), [function_type] ->
             cpp_function_type_of_string ctx function_type "";
+      | (["cpp"],"Reference"), [_] ->
+            TCppReference
 
       | ([],"Array"), [p] ->
          let arrayOf = cpp_type_of ctx p in
@@ -1729,6 +1739,7 @@ let rec cpp_type_of ctx haxe_type =
               TCppDynamicArray
 
             | TCppObject
+            | TCppReference
             | TCppEnum _
             | TCppInst _
             | TCppInterface _
@@ -1870,6 +1881,7 @@ let cpp_class_name klass =
 let cpp_variant_type_of t = match t with
    | TCppDynamic
    | TCppObject
+   | TCppReference
    | TCppVoid
    | TCppFastIterator _
    | TCppDynamicArray
@@ -2254,7 +2266,7 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
                         CppDynamicField(retypedObj, member.cf_name), TCppVariant
 
                      | _ ->
-                        let operator = if is_struct_access obj.etype || retypedObj.cpptype=TCppString then "." else "->" in
+                        let operator = if cpp_is_struct_access retypedObj.cpptype || retypedObj.cpptype=TCppString then "." else "->" in
                         CppVar(VarInstance(retypedObj,member,tcpp_to_string clazzType, operator) ), exprType
                      )
                end else if (clazz.cl_interface && not is_objc (* Use instance call for objc interfaces *)) then
@@ -2702,6 +2714,8 @@ let retype_expression ctx request_type function_args expression_tree forInjectio
       (* Auto cast rules... *)
       if return_type=TCppVoid then
          mk_cppexpr retypedExpr TCppVoid
+      else if (cppExpr.cpptype=TCppReference) then
+         mk_cppexpr retypedExpr return_type
       else if (cppExpr.cpptype=TCppVariant || cppExpr.cpptype=TCppDynamic) then begin
          match return_type with
          | TCppObjectArray _

+ 1 - 1
std/cpp/ConstPointer.hx

@@ -26,7 +26,7 @@ extern class ConstPointer<T>
 {
    // ptr actually returns the pointer - not strictly a 'T' - for pointers to smart pointers
    // Use value or ref to get dereferenced value
-   public var ptr:T;
+   public var ptr:Reference<T>;
 
    @:analyzer(no_simplification)
    public var value(get,never):T;

+ 4 - 4
std/cpp/Pointer.hx

@@ -25,12 +25,12 @@
 extern class Pointer<T> extends ConstPointer<T> implements ArrayAccess<T>
 {
    @:analyzer(no_simplification)
-   public var ref(get,set):T;
+   public var ref(get,set):Reference<T>;
 
    @:analyzer(no_simplification)
-   public function get_ref() : T;
+   public function get_ref() : Reference<T>;
    @:analyzer(no_simplification)
-   public function set_ref(t:T) : T;
+   public function set_ref(t:T) : Reference<T>;
 
    @:analyzer(no_simplification)
    public function setAt(inIndex:Int, value:T):Void;
@@ -66,7 +66,7 @@ extern class Pointer<T> extends ConstPointer<T> implements ArrayAccess<T>
    override public function add(inT:Int):Pointer<T>;
 
    @:analyzer(no_simplification)
-   public function postIncRef():T;
+   public function postIncRef():Reference<T>;
 
    public function destroy():Void;
    public function destroyArray():Void;

+ 28 - 0
std/cpp/Reference.hx

@@ -0,0 +1,28 @@
+/*
+ * Copyright (C)2005-2016 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package cpp;
+
+// Allows haxe to type result correctly, and hxcpp can recognise this and prevent
+//  unwanted casting
+typedef Reference<T> = T;
+
+