Browse Source

[cpp] Add @:scalar metadata to allow custom @:coreType abstracts

Cauê Waneck 9 years ago
parent
commit
6d98320371
3 changed files with 21 additions and 5 deletions
  1. 19 5
      src/generators/gencpp.ml
  2. 1 0
      src/syntax/ast.ml
  3. 1 0
      src/typing/common.ml

+ 19 - 5
src/generators/gencpp.ml

@@ -926,6 +926,10 @@ let is_extern_class class_def =
        | _ -> false );
 ;;
 
+let is_scalar_abstract abstract_def =
+   Meta.has Meta.Scalar abstract_def.a_meta && Meta.has Meta.CoreType abstract_def.a_meta
+;;
+
 
 let real_non_native_interfaces =
 List.filter (function (t,pl) ->
@@ -1664,7 +1668,10 @@ let rec cpp_type_of ctx haxe_type =
 
    | TAbstract (a,params) ->
        cpp_type_from_path ctx a.a_path params (fun () ->
-            TCppScalar(join_class_path a.a_path "::") )
+            if is_scalar_abstract a then
+               TCppScalar(join_class_path a.a_path "::")
+            else
+               TCppDynamic)
 
    | TType (type_def,params) ->
        cpp_type_from_path ctx type_def.t_path params (fun () ->
@@ -3739,12 +3746,17 @@ let find_referenced_types_flags ctx obj super_deps constructor_deps header_only
    and add_type in_path =
       add_type_flag false in_path
    in
-   let add_extern_class klass =
-      let include_file = get_meta_string_path klass.cl_meta (if for_depends then Meta.Depend else Meta.Include) in
+   let add_extern_type decl =
+      let tinfo = t_infos decl in
+      let include_file = get_meta_string_path tinfo.mt_meta (if for_depends then Meta.Depend else Meta.Include) in
       if (include_file<>"") then
          add_type ( path_of_string include_file )
-      else if (not for_depends) && (has_meta_key klass.cl_meta Meta.Include) then
-         add_type klass.cl_path
+      else if (not for_depends) && (has_meta_key tinfo.mt_meta Meta.Include) then
+         add_type tinfo.mt_path
+   in
+
+   let add_extern_class klass =
+      add_extern_type (TClassDecl klass)
    in
    let add_native_gen_class klass =
       let include_file = get_meta_string_path klass.cl_meta (if for_depends then Meta.Depend else Meta.Include) in
@@ -3780,6 +3792,8 @@ let find_referenced_types_flags ctx obj super_deps constructor_deps header_only
             | _ when is_extern_class klass -> add_extern_class klass
             | _ -> (match klass.cl_kind with KTypeParameter _ -> () | _ -> add_type klass.cl_path);
             )
+         | TAbstract (a,params) when is_scalar_abstract a ->
+            add_extern_type (TAbstractDecl a)
          | TFun (args,haxe_type) -> visit_type haxe_type;
             List.iter (fun (_,_,t) -> visit_type t; ) args;
          | _ -> ()

+ 1 - 0
src/syntax/ast.ml

@@ -156,6 +156,7 @@ module Meta = struct
 		| Rtti
 		| Runtime
 		| RuntimeValue
+		| Scalar
 		| SelfCall
 		| Setter
 		| SkipCtor

+ 1 - 0
src/typing/common.ml

@@ -498,6 +498,7 @@ module MetaInfo = struct
 		| Rtti -> ":rtti",("Adds runtime type informations",[UsedOn TClass])
 		| Runtime -> ":runtime",("?",[])
 		| RuntimeValue -> ":runtimeValue",("Marks an abstract as being a runtime value",[UsedOn TAbstract])
+		| Scalar -> ":scalar",("Used by hxcpp to mark a custom coreType abstract",[UsedOn TAbstract; Platform Cpp])
 		| SelfCall -> ":selfCall",("Translates method calls into calling object directly",[UsedOn TClassField; Platform Js])
 		| Setter -> ":setter",("Generates a native setter function on the given field",[HasParam "Class field name";UsedOn TClassField;Platform Flash])
 		| StackOnly -> ":stackOnly",("Instances of this type can only appear on the stack",[Platform Cpp])