Skip to content

Commit

Permalink
Track actual path position for path.Path<Params> (HaxeFoundation#11405
Browse files Browse the repository at this point in the history
)

* turn placed_type_path into a record and add pos_path

* fix Rudy
  • Loading branch information
Simn authored Nov 22, 2023
1 parent b588672 commit 2fbb4bf
Show file tree
Hide file tree
Showing 37 changed files with 373 additions and 296 deletions.
21 changes: 11 additions & 10 deletions src/codegen/dotnet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,21 @@ let mk_type_path ctx path params =
let nested = List.map (netcl_to_hx) nested in
ns, Some (String.concat "_" nested ^ "_" ^ netcl_to_hx cl), nhd
in
CTPath {
make_ptp_ct_null {
tpackage = fst (netpath_to_hx ctx.nstd (pack,[],""));
Ast.tname = name;
tparams = params;
tsub = sub;
}

let raw_type_path ctx path params =
{
let tp = {
tpackage = fst path;
Ast.tname = snd path;
tparams = params;
tsub = None;
}
} in
make_ptp tp null_pos

let rec convert_signature ctx p = function
| LVoid ->
Expand Down Expand Up @@ -577,7 +578,7 @@ let convert_ilprop ctx p prop is_explicit_impl =
cff_kind = kind;
}

let get_type_path ctx ct = match ct with | CTPath p -> p | _ -> die "" __LOC__
let get_type_path ctx ct = match ct with | CTPath ptp -> ptp | _ -> die "" __LOC__

let is_explicit ctx ilcls i =
let s = match i with
Expand Down Expand Up @@ -754,12 +755,12 @@ let convert_ilclass ctx p ?(delegate=false) ilcls = match ilcls.csuper with
(match ilcls.csuper with
| Some { snorm = LClass ( (["System"],[],"Object"), [] ) } -> ()
| Some ({ snorm = LClass ( (["System"],[],"ValueType"), [] ) } as s) ->
flags := HExtends (get_type_path ctx (convert_signature ctx p s.snorm),null_pos) :: !flags;
flags := HExtends (get_type_path ctx (convert_signature ctx p s.snorm)) :: !flags;
meta := (Meta.Struct,[],p) :: !meta
| Some { snorm = LClass ( (["haxe";"lang"],[],"HxObject"), [] ) } ->
meta := (Meta.HxGen,[],p) :: !meta
| Some s ->
flags := HExtends (get_type_path ctx (convert_signature ctx p s.snorm),null_pos) :: !flags
flags := HExtends (get_type_path ctx (convert_signature ctx p s.snorm)) :: !flags
| _ -> ());

let has_explicit_ifaces = ref false in
Expand All @@ -771,9 +772,9 @@ let convert_ilclass ctx p ?(delegate=false) ilcls = match ilcls.csuper with
| i ->
if is_explicit ctx ilcls i then has_explicit_ifaces := true;
flags := if !is_interface then
HExtends (get_type_path ctx (convert_signature ctx p i),null_pos) :: !flags
HExtends (get_type_path ctx (convert_signature ctx p i)) :: !flags
else
HImplements (get_type_path ctx (convert_signature ctx p i),null_pos) :: !flags
HImplements (get_type_path ctx (convert_signature ctx p i)) :: !flags
) ilcls.cimplements;
(* this is needed because of explicit interfaces. see http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx *)
(* explicit interfaces can't be mapped into Haxe in any way - since their fields can't be accessed directly, but they still implement that interface *)
Expand All @@ -784,9 +785,9 @@ let convert_ilclass ctx p ?(delegate=false) ilcls = match ilcls.csuper with
ignore (List.exists (function
| { psig = { snorm = LMethod(_,ret,[v]) } } ->
flags := if !is_interface then
(HExtends( raw_type_path ctx ([],"ArrayAccess") [ TPType (convert_signature ctx p ret,null_pos) ],null_pos) :: !flags)
(HExtends( raw_type_path ctx ([],"ArrayAccess") [ TPType (convert_signature ctx p ret,null_pos) ]) :: !flags)
else
(HImplements( raw_type_path ctx ([],"ArrayAccess") [ TPType (convert_signature ctx p ret,null_pos) ],null_pos) :: !flags);
(HImplements( raw_type_path ctx ([],"ArrayAccess") [ TPType (convert_signature ctx p ret,null_pos) ]) :: !flags);
true
| _ -> false) ilcls.cprops);

Expand Down
70 changes: 36 additions & 34 deletions src/codegen/java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ let lookup_jclass com path =
| Some p -> Some p
) com.native_libs.java_libs None

let mk_type_path ctx path params =
let mk_type_path ctx path params p =
let name, sub = try
let p, _ = String.split (snd path) "$" in
jname_to_hx p, Some (jname_to_hx (snd path))
Expand All @@ -104,31 +104,31 @@ let mk_type_path ctx path params =
| _ ->
pack, sub, name
in
CTPath {
make_ptp_ct {
tpackage = pack;
tname = name;
tparams = params;
tsub = sub;
}
} p

let has_tparam name params = List.exists(fun (n,_,_) -> n = name) params

let rec convert_arg ctx p arg =
match arg with
| TAny | TType (WSuper, _) -> TPType (mk_type_path ctx ([], "Dynamic") [],null_pos)
| TAny | TType (WSuper, _) -> TPType (mk_type_path ctx ([], "Dynamic") [] p,null_pos)
| TType (_, jsig) -> TPType (convert_signature ctx p jsig,null_pos)

and convert_signature ctx p jsig =
match jsig with
| TByte -> mk_type_path ctx (["java"; "types"], "Int8") []
| TChar -> mk_type_path ctx (["java"; "types"], "Char16") []
| TDouble -> mk_type_path ctx ([], "Float") []
| TFloat -> mk_type_path ctx ([], "Single") []
| TInt -> mk_type_path ctx ([], "Int") []
| TLong -> mk_type_path ctx (["haxe"], "Int64") []
| TShort -> mk_type_path ctx (["java"; "types"], "Int16") []
| TBool -> mk_type_path ctx ([], "Bool") []
| TObject ( (["haxe";"root"], name), args ) -> mk_type_path ctx ([], name) (List.map (convert_arg ctx p) args)
| TByte -> mk_type_path ctx (["java"; "types"], "Int8") [] p
| TChar -> mk_type_path ctx (["java"; "types"], "Char16") [] p
| TDouble -> mk_type_path ctx ([], "Float") [] p
| TFloat -> mk_type_path ctx ([], "Single") [] p
| TInt -> mk_type_path ctx ([], "Int") [] p
| TLong -> mk_type_path ctx (["haxe"], "Int64") [] p
| TShort -> mk_type_path ctx (["java"; "types"], "Int16") [] p
| TBool -> mk_type_path ctx ([], "Bool") [] p
| TObject ( (["haxe";"root"], name), args ) -> mk_type_path ctx ([], name) (List.map (convert_arg ctx p) args) p
(** nullable types *)
(* replaced from Null<Type> to the actual abstract type to fix #2738 *)
(* | TObject ( (["java";"lang"], "Integer"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx ([], "Int") []) ] *)
Expand All @@ -140,34 +140,34 @@ and convert_signature ctx p jsig =
(* | TObject ( (["java";"lang"], "Short"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["java";"types"], "Int16") []) ] *)
(* | TObject ( (["java";"lang"], "Long"), [] ) -> mk_type_path ctx ([], "Null") [ TPType (mk_type_path ctx (["haxe"], "Int64") []) ] *)
(** other std types *)
| TObject ( (["java";"lang"], "Object"), [] ) -> mk_type_path ctx ([], "Dynamic") []
| TObject ( (["java";"lang"], "String"), [] ) -> mk_type_path ctx ([], "String") []
| TObject ( (["java";"lang"], "Enum"), [_] ) -> mk_type_path ctx ([], "EnumValue") []
| TObject ( (["java";"lang"], "Object"), [] ) -> mk_type_path ctx ([], "Dynamic") [] p
| TObject ( (["java";"lang"], "String"), [] ) -> mk_type_path ctx ([], "String") [] p
| TObject ( (["java";"lang"], "Enum"), [_] ) -> mk_type_path ctx ([], "EnumValue") [] p
(** other types *)
| TObject ( path, [] ) ->
(match lookup_jclass ctx.jcom path with
| Some (jcl, _, _) -> mk_type_path ctx path (List.map (fun _ -> convert_arg ctx p TAny) jcl.ctypes)
| None -> mk_type_path ctx path [])
| TObject ( path, args ) -> mk_type_path ctx path (List.map (convert_arg ctx p) args)
| Some (jcl, _, _) -> mk_type_path ctx path (List.map (fun _ -> convert_arg ctx p TAny) jcl.ctypes) p
| None -> mk_type_path ctx path [] p)
| TObject ( path, args ) -> mk_type_path ctx path (List.map (convert_arg ctx p) args) p
| TObjectInner (pack, (name, params) :: inners) ->
let actual_param = match List.rev inners with
| (_, p) :: _ -> p
| _ -> die "" __LOC__ in
mk_type_path ctx (pack, name ^ "$" ^ String.concat "$" (List.map fst inners)) (List.map (fun param -> convert_arg ctx p param) actual_param)
mk_type_path ctx (pack, name ^ "$" ^ String.concat "$" (List.map fst inners)) (List.map (fun param -> convert_arg ctx p param) actual_param) p
| TObjectInner (pack, inners) -> die "" __LOC__
| TArray (jsig, _) -> mk_type_path ctx (["java"], "NativeArray") [ TPType (convert_signature ctx p jsig,null_pos) ]
| TArray (jsig, _) -> mk_type_path ctx (["java"], "NativeArray") [ TPType (convert_signature ctx p jsig,null_pos) ] p
| TMethod _ -> JReader.error "TMethod cannot be converted directly into Complex Type"
| TTypeParameter s -> (match ctx.jtparams with
| cur :: others ->
if has_tparam s cur then
mk_type_path ctx ([], s) []
mk_type_path ctx ([], s) [] p
else begin
if ctx.jcom.verbose && not(List.exists (has_tparam s) others) then print_endline ("Type parameter " ^ s ^ " was not found while building type!");
mk_type_path ctx ([], "Dynamic") []
mk_type_path ctx ([], "Dynamic") [] p
end
| _ ->
if ctx.jcom.verbose then print_endline ("Empty type parameter stack!");
mk_type_path ctx ([], "Dynamic") [])
mk_type_path ctx ([], "Dynamic") [] p)

let convert_constant ctx p const =
Option.map_default (function
Expand Down Expand Up @@ -196,7 +196,7 @@ let convert_param ctx p parent param =
tp_meta = [];
}

let get_type_path ctx ct = match ct with | CTPath p -> p | _ -> die "" __LOC__
let get_type_path ctx ct = match ct with | CTPath ptp -> ptp | _ -> die "" __LOC__

let is_override field =
List.exists (function | AttrVisibleAnnotations [{ ann_type = TObject( (["java";"lang"], "Override"), _ ) }] -> true | _ -> false) field.jf_attributes
Expand Down Expand Up @@ -308,6 +308,7 @@ let convert_java_enum ctx p pe =
List.iter (fun jsig ->
match convert_signature ctx p jsig with
| CTPath path ->
let path = path.path in
cff_meta := (Meta.Throws, [Ast.EConst (Ast.String (s_type_path (path.tpackage,path.tname),SDoubleQuotes)), p],p) :: !cff_meta
| _ -> ()
) field.jf_throws;
Expand Down Expand Up @@ -360,13 +361,13 @@ let convert_java_enum ctx p pe =
let hx_sig =
match s with
| TArray (s1,_) when !is_varargs && !i = args_count && is_eligible_for_haxe_rest_args s1 ->
mk_type_path ctx (["haxe"], "Rest") [TPType (convert_signature ctx p s1,null_pos)]
mk_type_path ctx (["haxe"], "Rest") [TPType (convert_signature ctx p s1,null_pos)] p
| _ ->
convert_signature ctx null_pos s
in
(local_names !i,null_pos), false, [], Some(hx_sig,null_pos), None
) args in
let t = Option.map_default (convert_signature ctx p) (mk_type_path ctx ([], "Void") []) ret in
let t = Option.map_default (convert_signature ctx p) (mk_type_path ctx ([], "Void") [] p) ret in
cff_access := (AOverload,p) :: !cff_access;
let types = List.map (function
| (name, Some ext, impl) ->
Expand Down Expand Up @@ -485,17 +486,17 @@ let convert_java_enum ctx p pe =
(match jc.csuper with
| TObject( (["java";"lang"], "Object"), _ ) -> ()
| TObject( (["haxe";"lang"], "HxObject"), _ ) -> meta := (Meta.HxGen,[],p) :: !meta
| _ -> flags := HExtends (get_type_path ctx (convert_signature ctx p jc.csuper),p) :: !flags
| _ -> flags := HExtends (get_type_path ctx (convert_signature ctx p jc.csuper)) :: !flags
);

List.iter (fun i ->
match i with
| TObject ( (["haxe";"lang"], "IHxObject"), _ ) -> meta := (Meta.HxGen,[],p) :: !meta
| _ -> flags :=
if !is_interface then
HExtends (get_type_path ctx (convert_signature ctx p i),p) :: !flags
HExtends (get_type_path ctx (convert_signature ctx p i)) :: !flags
else
HImplements (get_type_path ctx (convert_signature ctx p i),p) :: !flags
HImplements (get_type_path ctx (convert_signature ctx p i)) :: !flags
) jc.cinterfaces;

let fields = ref [] in
Expand All @@ -520,6 +521,7 @@ let convert_java_enum ctx p pe =
match convert_signature ctx p jsig with
| CTPath path ->
let pos = { p with pfile = p.pfile ^ " (" ^ f.jf_name ^" @:throws)" } in
let path = path.path in
EImport( List.map (fun s -> s,pos) (path.tpackage @ [path.tname]), INormal )
| _ -> die "" __LOC__
) f.jf_throws
Expand Down Expand Up @@ -1069,19 +1071,19 @@ class virtual java_library com name file_path = object(self)
d_params = c.d_params;
d_meta = [];
d_flags = [];
d_data = CTPath {
d_data = make_ptp_th_null {
tpackage = pack;
tname = snd path;
tparams = List.map (fun tp ->
TPType (CTPath {
TPType (make_ptp_th_null {
tpackage = [];
tname = fst tp.tp_name;
tparams = [];
tsub = None;
},null_pos)
})
) c.d_params;
tsub = Some(fst c.d_name);
},null_pos;
};
} in
inner_alias := SS.add alias_name !inner_alias;
alias_list := (alias_def, pos) :: !alias_list;
Expand Down
62 changes: 32 additions & 30 deletions src/codegen/javaModern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,46 +617,48 @@ type java_lib_ctx = {
module SignatureConverter = struct
open PathConverter

let mk_type_path path params =
let mk_type_path path params p =
let pack,(mname,name) = jpath_to_hx path in
match mname with
let path = match mname with
| None ->
CTPath {
{
tpackage = pack;
tname = name;
tparams = params;
tsub = None;
}
| Some mname ->
CTPath {
{
tpackage = pack;
tname = mname;
tparams = params;
tsub = Some name;
}
in
make_ptp_ct path p

let ct_type_param name = CTPath {
let ct_type_param name = make_ptp_ct_null {
tpackage = [];
tname = name;
tparams = [];
tsub = None
}

let ct_void = CTPath {
let ct_void = make_ptp_ct_null {
tpackage = [];
tname = "Void";
tparams = [];
tsub = None;
}

let ct_dynamic = CTPath {
let ct_dynamic = make_ptp_ct_null {
tpackage = [];
tname = "Dynamic";
tparams = [];
tsub = None;
}

let ct_string = CTPath {
let ct_string = make_ptp_ct_null {
tpackage = [];
tname = "String";
tparams = [];
Expand All @@ -665,33 +667,33 @@ module SignatureConverter = struct

let rec convert_arg ctx p arg =
match arg with
| TAny | TType (WSuper, _) -> TPType (mk_type_path ([], "Dynamic") [],p)
| TAny | TType (WSuper, _) -> TPType (mk_type_path ([], "Dynamic") [] p,p)
| TType (_, jsig) -> TPType (convert_signature ctx p jsig,p)

and convert_signature ctx p jsig =
match jsig with
| TByte -> mk_type_path (["java"; "types"], "Int8") []
| TChar -> mk_type_path (["java"; "types"], "Char16") []
| TDouble -> mk_type_path ([], "Float") []
| TFloat -> mk_type_path ([], "Single") []
| TInt -> mk_type_path ([], "Int") []
| TLong -> mk_type_path (["haxe"], "Int64") []
| TShort -> mk_type_path (["java"; "types"], "Int16") []
| TBool -> mk_type_path ([], "Bool") []
| TObject ( (["haxe";"root"], name), args ) -> mk_type_path ([], name) (List.map (convert_arg ctx p) args)
| TObject ( (["java";"lang"], "Object"), [] ) -> mk_type_path ([], "Dynamic") []
| TObject ( (["java";"lang"], "String"), [] ) -> mk_type_path ([], "String") []
| TObject ( (["java";"lang"], "Enum"), [_] ) -> mk_type_path ([], "EnumValue") []
| TByte -> mk_type_path (["java"; "types"], "Int8") [] p
| TChar -> mk_type_path (["java"; "types"], "Char16") [] p
| TDouble -> mk_type_path ([], "Float") [] p
| TFloat -> mk_type_path ([], "Single") [] p
| TInt -> mk_type_path ([], "Int") [] p
| TLong -> mk_type_path (["haxe"], "Int64") [] p
| TShort -> mk_type_path (["java"; "types"], "Int16") [] p
| TBool -> mk_type_path ([], "Bool") [] p
| TObject ( (["haxe";"root"], name), args ) -> mk_type_path ([], name) (List.map (convert_arg ctx p) args) p
| TObject ( (["java";"lang"], "Object"), [] ) -> mk_type_path ([], "Dynamic") [] p
| TObject ( (["java";"lang"], "String"), [] ) -> mk_type_path ([], "String") [] p
| TObject ( (["java";"lang"], "Enum"), [_] ) -> mk_type_path ([], "EnumValue") [] p
| TObject ( path, [] ) ->
mk_type_path path []
| TObject ( path, args ) -> mk_type_path path (List.map (convert_arg ctx p) args)
mk_type_path path [] p
| TObject ( path, args ) -> mk_type_path path (List.map (convert_arg ctx p) args) p
| TObjectInner (pack, (name, params) :: inners) ->
let actual_param = match List.rev inners with
| (_, p) :: _ -> p
| _ -> die "" __LOC__ in
mk_type_path (pack, name ^ "$" ^ String.concat "$" (List.map fst inners)) (List.map (fun param -> convert_arg ctx p param) actual_param)
mk_type_path (pack, name ^ "$" ^ String.concat "$" (List.map fst inners)) (List.map (fun param -> convert_arg ctx p param) actual_param) p
| TObjectInner (pack, inners) -> die "" __LOC__
| TArray (jsig, _) -> mk_type_path (["java"], "NativeArray") [ TPType (convert_signature ctx p jsig,p) ]
| TArray (jsig, _) -> mk_type_path (["java"], "NativeArray") [ TPType (convert_signature ctx p jsig,p) ] p
| TMethod _ -> failwith "TMethod cannot be converted directly into Complex Type"
| TTypeParameter s ->
try
Expand All @@ -700,7 +702,7 @@ module SignatureConverter = struct
ct_dynamic
end

let get_type_path ct = match ct with | CTPath p -> p | _ -> die "" __LOC__
let get_type_path ct = match ct with | CTPath ptp -> ptp | _ -> die "" __LOC__

module Converter = struct

Expand Down Expand Up @@ -890,7 +892,7 @@ module Converter = struct
let hx_sig =
match jsig with
| TArray (jsig1,_) when is_varargs && i + 1 = args_count && is_eligible_for_haxe_rest_args jsig1 ->
mk_type_path (["haxe"], "Rest") [TPType (convert_signature ctx p jsig1,p)]
mk_type_path (["haxe"], "Rest") [TPType (convert_signature ctx p jsig1,p)] p
| _ ->
convert_signature ctx p jsig
in
Expand Down Expand Up @@ -936,12 +938,12 @@ module Converter = struct
| TObject(([],""),_)
| TObject((["java";"lang"],"Object"),_) ->
if is_annotation then
add_flag (HExtends ({tpackage = ["java";"lang";"annotation"]; tname = "Annotation"; tsub = None; tparams = []},null_pos));
add_flag (HExtends (make_ptp {tpackage = ["java";"lang";"annotation"]; tname = "Annotation"; tsub = None; tparams = []} p))
| jsig ->
add_flag (HExtends (get_type_path (convert_signature ctx p jsig),p))
add_flag (HExtends (get_type_path (convert_signature ctx p jsig)))
end;
List.iter (fun jsig ->
let path = (get_type_path (convert_signature ctx p jsig),p) in
let path = get_type_path (convert_signature ctx p jsig) in
if is_interface then
add_flag (HExtends path)
else
Expand Down
Loading

0 comments on commit 2fbb4bf

Please sign in to comment.