Sfoglia il codice sorgente

[cpp] add more specific pointer types, including function pointer. Add some NativeString access

hughsando 11 anni fa
parent
commit
01ea152d00
7 ha cambiato i file con 167 aggiunte e 49 eliminazioni
  1. 99 32
      gencpp.ml
  2. 19 0
      std/cpp/BasePointer.hx
  3. 3 0
      std/cpp/Char.hx
  4. 15 0
      std/cpp/ConstPointer.hx
  5. 10 0
      std/cpp/Function.hx
  6. 13 0
      std/cpp/NativeString.hx
  7. 8 17
      std/cpp/Pointer.hx

+ 99 - 32
gencpp.ml

@@ -63,9 +63,11 @@ let join_class_path path separator =
 let is_internal_class = function
    |  ([],"Int") | ([],"Void") |  ([],"String") | ([], "Null") | ([], "Float")
    |  ([],"Array") | ([], "Class") | ([], "Enum") | ([], "Bool")
-   |  ([], "Dynamic") | ([], "ArrayAccess") | (["cpp"], "FastIterator") | (["cpp"],"Pointer") -> true
+   |  ([], "Dynamic") | ([], "ArrayAccess") | (["cpp"], "FastIterator")
+   |  (["cpp"],"Pointer") | (["cpp"],"ConstPointer")
+   |  (["cpp"],"BasePointer") | (["cpp"],"Function") -> true
    |  ([],"Math") | (["haxe";"io"], "Unsigned_char__") -> true
-   |  (["cpp"],"Int8") | (["cpp"],"UInt8")
+   |  (["cpp"],"Int8") | (["cpp"],"UInt8") | (["cpp"],"Char")
    |  (["cpp"],"Int16") | (["cpp"],"UInt16")
    |  (["cpp"],"Int32") | (["cpp"],"UInt32")
    |  (["cpp"],"Int64") | (["cpp"],"UInt64")
@@ -416,7 +418,7 @@ let gen_close_namespace output class_path =
 (* The basic types can have default values and are passesby value *)
 let is_numeric = function
    | "Int" | "Bool" | "Float" |  "::haxe::io::Unsigned_char__" | "unsigned char" -> true
-   | "::cpp::UInt8" | "::cpp::Int8"
+   | "::cpp::UInt8" | "::cpp::Int8" | "::cpp::Char"
    | "::cpp::UInt16" | "::cpp::Int16"
    | "::cpp::UInt32" | "::cpp::Int32"
    | "::cpp::UInt64" | "::cpp::Int64"
@@ -425,6 +427,15 @@ let is_numeric = function
    | _ -> false
 
 
+let rec remove_parens expression =
+   match expression.eexpr with
+   | TParenthesis e -> remove_parens e
+   | TMeta(_,e) -> remove_parens e
+   | TCast ( e,None) -> remove_parens e
+   | _ -> expression
+;;
+
+
 let cant_be_null type_string =
    is_numeric type_string
 ;;
@@ -440,15 +451,47 @@ let is_interface_type t =
 ;;
 
 
+
+let is_cpp_function_instance haxe_type =
+   match follow haxe_type with
+   | TInst (klass,params) ->
+      (match klass.cl_path with
+      | ["cpp"] , "Function" -> true
+      | _ -> false )
+   | _ -> false
+   ;;
+
+
+let is_cpp_function_class haxe_type =
+   match follow haxe_type with
+   | TType (klass,params) ->
+      (match klass.t_path with
+      | ["cpp"] , "Function" -> true
+      | _ -> false )
+   | _ -> false
+   ;;
+
+let is_fromStaticFunction_call func =
+   match (remove_parens func).eexpr with
+   | TField (_,FStatic ({cl_path=["cpp"],"Function"},{cf_name="fromStaticFunction"} ) ) -> true
+   | _ -> false
+;;
+
 let is_pointer haxe_type =
    match follow haxe_type with
    | TInst (klass,params) ->
       (match klass.cl_path with
-      | ["cpp"] , "Pointer" -> true
+      | ["cpp"] , "Pointer"
+      | ["cpp"] , "ConstPointer"
+      | ["cpp"] , "BasePointer"
+      | ["cpp"] , "Function" -> true
       | _ -> false )
    | TType (type_def,params) ->
       (match type_def.t_path with
-      | ["cpp"] , "Pointer" -> true
+      | ["cpp"] , "Pointer"
+      | ["cpp"] , "ConstPointer"
+      | ["cpp"] , "BasePointer"
+      | ["cpp"] , "Function" -> true
       | _ -> false )
    | _ -> false
    ;;
@@ -471,8 +514,12 @@ let rec class_string klass suffix params =
    (* FastIterator class *)
    |  (["cpp"],"FastIterator") -> "::cpp::FastIterator" ^ suffix ^ "< " ^ (String.concat ","
                (List.map type_string  params) ) ^ " >"
-   |  (["cpp"],"Pointer") -> "::cpp::Pointer< " ^ (String.concat ","
-               (List.map type_string params) ) ^ " >"
+   |  (["cpp"],"Pointer")
+   |  (["cpp"],"ConstPointer")
+   |  (["cpp"],"BasePointer") ->
+        "::cpp::Pointer< " ^ (String.concat "," (List.map type_string params) ) ^ " >"
+   |  (["cpp"],"Function") ->
+        "::cpp::Pointer< " ^ (cpp_function_signature_params params) ^ " >"
    | _ when is_dynamic_type_param klass.cl_kind -> "Dynamic"
    |  ([],"#Int") -> "/* # */int"
    |  (["haxe";"io"],"Unsigned_char__") -> "unsigned char"
@@ -531,10 +578,13 @@ and type_string_suff suffix haxe_type =
          (match params with
          | [t] -> "::cpp::FastIterator< " ^ (type_string (follow t) ) ^ " >"
          | _ -> assert false)
-      | ["cpp"] , "Pointer" ->
+      | ["cpp"] , "Pointer"
+      | ["cpp"] , "ConstPointer"
+      | ["cpp"] , "BasePointer" ->
          (match params with
          | [t] -> "::cpp::Pointer< " ^ (type_string (follow t) ) ^ " >"
          | _ -> assert false)
+      | ["cpp"] , "Function" -> "::cpp::Pointer< " ^ (cpp_function_signature_params params) ^ " >"
       | _ ->  type_string_suff suffix (apply_params type_def.t_types params type_def.t_type)
       )
    | TFun (args,haxe_type) -> "Dynamic" ^ suffix
@@ -566,11 +616,29 @@ and is_dynamic_array_param haxe_type =
    else (match follow haxe_type with
    | TInst (klass,params) ->
          (match klass.cl_path with
-         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator") | (["cpp"],"Pointer") -> false
+         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator") | (["cpp"],"Pointer") |(["cpp"],"ConstPointer") -> false
          | _ -> (match klass.cl_kind with KTypeParameter _ -> true | _ -> false)
          )
    | _ -> false
    )
+and cpp_function_signature tfun =
+   match follow tfun with
+   | TFun(args,ret) -> (type_string ret) ^ "(" ^ (gen_tfun_interface_arg_list args) ^ ")"
+   | _ -> "void *"
+ 
+and cpp_function_signature_params params = match params with
+   | [t] -> cpp_function_signature t
+   | _ ->  assert false;
+
+and gen_interface_arg_type_name name opt typ =
+   let type_str = (type_string typ) in
+   (if (opt && (cant_be_null type_str) ) then
+      "hx::Null< " ^ type_str ^ " > "
+   else
+      type_str )
+   ^ " " ^ (keyword_remap name)
+and gen_tfun_interface_arg_list args =
+   String.concat "," (List.map (fun (name,opt,typ) -> gen_interface_arg_type_name name opt typ) args)
 ;;
 
 
@@ -609,14 +677,6 @@ let is_numeric_field field =
 ;;
 
 
-let rec remove_parens expression =
-   match expression.eexpr with
-   | TParenthesis e -> remove_parens e
-   | TMeta(_,e) -> remove_parens e
-   | TCast ( e,None) -> remove_parens e
-   | _ -> expression
-;;
-
 
 let is_static_access obj =
    match (remove_parens obj).eexpr with
@@ -632,6 +692,16 @@ let is_native_with_space func =
 ;;
 
 
+let rec is_cpp_function_member func =
+   match (remove_parens func).eexpr with
+   | TField(obj,field) when is_cpp_function_instance obj.etype -> true
+   | TCall(obj,_) -> is_cpp_function_member obj
+   | _ -> false
+;;
+
+
+
+
 (* Get the type and output it to the stream *)
 let gen_type ctx haxe_type =
    ctx.ctx_output (type_string haxe_type)
@@ -699,19 +769,6 @@ let gen_arg_type_name name default_val arg_type prefix =
    | Some constant  -> (type_str,prefix ^ remap_name)
    | _ -> (type_str,remap_name);;
 
-let gen_interface_arg_type_name name opt typ =
-   let type_str = (type_string typ) in
-   (if (opt && (cant_be_null type_str) ) then
-      "hx::Null< " ^ type_str ^ " > "
-   else
-      type_str )
-   ^ " " ^ (keyword_remap name)
-;;
-
-let gen_tfun_interface_arg_list args =
-   String.concat "," (List.map (fun (name,opt,typ) -> gen_interface_arg_type_name name opt typ) args)
-;;
-
 (* Generate prototype text, including allowing default values to be null *)
 let gen_arg name default_val arg_type prefix =
    let pair = gen_arg_type_name name default_val arg_type prefix in
@@ -1807,6 +1864,15 @@ and gen_expression ctx retval expression =
          gen_expression ctx true func;
       end;
       output ("(" ^ !arg_string ^ ");\n");
+   | TCall (func, arg_list) when is_fromStaticFunction_call func ->
+      (match arg_list with
+         | [ {eexpr = TField( _, FStatic(klass,field)) } ] ->
+            let signature = cpp_function_signature field.cf_type in
+            let name = keyword_remap field.cf_name in
+            output ("::cpp::Pointer<" ^ signature ^">( &::" ^(join_class_path klass.cl_path "::")^ "_obj::" ^ name ^ ")");
+         | _ -> error "fromStaticFunction must take a static function" expression.epos;
+      )
+
    | TCall (func, arg_list) ->
       let rec is_variable e = match e.eexpr with
       | TField _ | TEnumParameter _ -> false
@@ -1868,7 +1934,8 @@ and gen_expression ctx retval expression =
             output ")";
       end;
       if (cast_result) then output (")");
-      if ( (is_variable func) && (expr_type<>"Dynamic") && (not is_super) && (not is_block_call)) then
+      if ( (is_variable func) && (not (is_cpp_function_member func) ) &&
+           (expr_type<>"Dynamic") && (not is_super) && (not is_block_call)) then
          ctx.ctx_output (".Cast< " ^ expr_type ^ " >()" );
 
       let rec cast_array_output func =
@@ -2557,7 +2624,7 @@ let find_referenced_types ctx obj super_deps constructor_deps header_only for_de
          for the Array, Class, FastIterator or Pointer classes, for which we do a fully typed object *)
       | TInst (klass,params) ->
          (match klass.cl_path with
-         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator") | (["cpp"],"Pointer")-> List.iter visit_type params
+         | ([],"Array") | ([],"Class") | (["cpp"],"FastIterator") | (["cpp"],"Pointer") | (["cpp"],"ConstPointer") -> List.iter visit_type params
          | _ when is_extern_class klass -> add_extern_class klass
          | _ -> (match klass.cl_kind with KTypeParameter _ -> () | _ -> add_type klass.cl_path);
          )

+ 19 - 0
std/cpp/BasePointer.hx

@@ -0,0 +1,19 @@
+package cpp;
+
+@:coreType @:include("cpp/Pointer.h")
+extern class BasePointer<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 value(get,never):T;
+
+	public function lt(inOther:BasePointer<T>):Bool;
+	public function leq(inOther:BasePointer<T>):Bool;
+	public function gt(inOther:BasePointer<T>):Bool;
+	public function geq(inOther:BasePointer<T>):Bool;
+
+}
+
+

+ 3 - 0
std/cpp/Char.hx

@@ -0,0 +1,3 @@
+package cpp;
+
+@:coreType @:notNull @:runtimeValue abstract Char from Int to Int {}

+ 15 - 0
std/cpp/ConstPointer.hx

@@ -0,0 +1,15 @@
+package cpp;
+
+@:coreType @:include("cpp/Pointer.h")
+extern class ConstPointer<T> extends BasePointer<T>
+{
+	public function at(inIndex:Int):T;
+
+	public function inc():ConstPointer<T>;
+	public function dec():ConstPointer<T>;
+	public function postIncBy(inT:Int):ConstPointer<T>;
+	public function incBy(inT:Int):ConstPointer<T>;
+	public function add(inT:Int):ConstPointer<T>;
+
+}
+

+ 10 - 0
std/cpp/Function.hx

@@ -0,0 +1,10 @@
+package cpp;
+
+@:coreType @:include("cpp/Pointer.h")
+extern class Function<T> extends BasePointer<T>
+{
+   public static function getProcAddress<T>(inModule:String, inFunction:String) : Function<T>;
+   public static function fromStaticFunction<T>(inStaticFunction:T) : Function<T>;
+}
+
+

+ 13 - 0
std/cpp/NativeString.hx

@@ -0,0 +1,13 @@
+package cpp;
+
+extern class NativeString {
+
+	public static inline function c_str( inString:String ) : ConstPointer<Char> {
+		var result:ConstPointer<Char> =  untyped inString.__s;
+		return result;
+   }
+	public static inline function fromPointer(inPtr:ConstPointer<Char> ) : String {
+      return untyped __global__.String(inPtr.ptr);
+   }
+}
+

+ 8 - 17
std/cpp/Pointer.hx

@@ -1,28 +1,19 @@
 package cpp;
 
-@:unreflective @:coreType
-extern class Pointer<T> implements ArrayAccess<T>
+@:coreType
+extern class Pointer<T> extends ConstPointer<T> implements ArrayAccess<T>
 {
-   // ptr actually returns the pointer - not strictly a 'T' - for pointers to smaert pointers
-   // Use value or [0] for a reference to the item
-	public var ptr:T;
-	public var value(get,set):T;
+	public var ref(get,set):T;
 
 	public static function arrayElem<T>(array:Array<T>, inElem:Int):Pointer<T>;
 
-	public function inc():Pointer<T>;
-	public function dec():Void;
-	public function postIncBy(inT:Int):Pointer<T>;
-	public function incBy(inT:Int):Pointer<T>;
-	public function add(inT:Int):Pointer<T>;
+	override public function inc():Pointer<T>;
+	override public function dec():Pointer<T>;
+	override public function postIncBy(inT:Int):Pointer<T>;
+	override public function incBy(inT:Int):Pointer<T>;
+	override public function add(inT:Int):Pointer<T>;
 
 	public function destroy():Void;
 	public function destroyArray():Void;
-
-	public function lt(inOther:Pointer<T>):Bool;
-	public function leq(inOther:Pointer<T>):Bool;
-	public function gt(inOther:Pointer<T>):Bool;
-	public function geq(inOther:Pointer<T>):Bool;
-
 }