Skip to content

Commit

Permalink
Merge branch 'development' into run_some_filters_in_diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jul 15, 2023
2 parents 143227a + 27053ae commit 1a8e9bc
Show file tree
Hide file tree
Showing 41 changed files with 478 additions and 244 deletions.
6 changes: 3 additions & 3 deletions src/codegen/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ let add_property_field com c =
c.cl_statics <- PMap.add cf.cf_name cf c.cl_statics;
c.cl_ordered_statics <- cf :: c.cl_ordered_statics

let escape_res_name name allow_dirs =
let escape_res_name name allowed =
ExtString.String.replace_chars (fun chr ->
if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' || chr = '.' then
Char.escaped chr
else if chr = '/' && allow_dirs then
"/"
else if List.mem chr allowed then
Char.escaped chr
else
"-x" ^ (string_of_int (Char.code chr))) name

Expand Down
3 changes: 1 addition & 2 deletions src/context/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,7 @@ exception Abort of Error.error

let ignore_error com =
let b = com.display.dms_error_policy = EPIgnore in
if b then
if b then com.has_error <- true;
if b then com.has_error <- true;
b

(* Defines *)
Expand Down
2 changes: 1 addition & 1 deletion src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ let map_expr_type f ft fv e =
| TFunction fu ->
let fu = {
tf_expr = f fu.tf_expr;
tf_args = List.map (fun (v,o) -> fv v, o) fu.tf_args;
tf_args = List.map (fun (v,o) -> fv v, (Option.map f o)) fu.tf_args;
tf_type = ft fu.tf_type;
} in
{ e with eexpr = TFunction fu; etype = ft e.etype }
Expand Down
6 changes: 4 additions & 2 deletions src/filters/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,11 @@ let catch_native ctx catches t p =
)
(* Haxe-specific wildcard catches should go to if-fest because they need additional handling *)
| (v,_) :: _ when is_haxe_wildcard_catch ctx v.v_type ->
(match handle_as_value_exception with
| [] ->
(match handle_as_value_exception, value_exception_catch with
| [], None ->
catches_to_ifs ctx catches t p
| [], Some catch ->
catches_to_ifs ctx [catch] t p
| _ ->
catches_as_value_exception ctx handle_as_value_exception None t p
:: catches_to_ifs ctx catches t p
Expand Down
7 changes: 5 additions & 2 deletions src/filters/renameVars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ let declare_var rc scope v =
let will_be_reserved rc v =
rc.rc_no_shadowing || (has_var_flag v VCaptured && rc.rc_hoisting)

let unbound_variable v =
raise (Failure (Printf.sprintf "Unbound variable: %s<%i>" v.v_name v.v_id))

(**
Invoked for each `TLocal v` texr_expr
*)
Expand All @@ -234,7 +237,7 @@ let rec determine_overlaps rc scope v =
Overlaps.add v scope.foreign_vars;
(match scope.parent with
| Some parent -> determine_overlaps rc parent v
| None -> raise (Failure "Failed to locate variable declaration")
| None -> unbound_variable v
)
| (d, _) :: _ when d == v ->
()
Expand All @@ -261,7 +264,7 @@ let use_var rc scope v =
| Some parent ->
loop parent
| None ->
raise (Failure "Failed to locate variable declaration")
unbound_variable v
end
in
loop scope
Expand Down
7 changes: 6 additions & 1 deletion src/generators/gencpp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ let keyword_remap name =
| "HX_" | "HXLINE" | "HXDLIN"
| "NO" | "YES"
| "abstract" | "decltype" | "finally" | "nullptr" | "static_assert"
| "struct" -> "_hx_" ^ name
| "struct" | "_Atomic"
| "constexpr" | "consteval" | "constinit"
| "co_await" | "co_return" | "co_yield"
| "alignas" | "alignof"
| "_Alignas" | "_Alignof"
| "requires" -> "_hx_" ^ name
| x -> x
;;

Expand Down
2 changes: 1 addition & 1 deletion src/generators/gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3421,7 +3421,7 @@ let generate con =
gen.gcon.file ^ "/src/Resources"
in
Hashtbl.iter (fun name v ->
let name = Codegen.escape_res_name name true in
let name = Codegen.escape_res_name name ['/'] in
let full_path = src ^ "/" ^ name in
Path.mkdir_from_path full_path;

Expand Down
9 changes: 7 additions & 2 deletions src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3329,15 +3329,20 @@ let generate_static ctx c f =
let gen_content() =
op ctx (OThrow (make_string ctx ("Requires compiling with -D hl-ver=" ^ ver ^ ".0 or higher") null_pos));
in
ignore(make_fun ctx ~gen_content (s_type_path c.cl_path,f.cf_name) (alloc_fid ctx c f) (match f.cf_expr with Some { eexpr = TFunction f } -> f | _ -> abort "Missing function body" f.cf_pos) None None)
(match f.cf_expr with
| Some { eexpr = TFunction fn } -> ignore(make_fun ctx ~gen_content (s_type_path c.cl_path,f.cf_name) (alloc_fid ctx c f) fn None None)
| _ -> if not (Meta.has Meta.NoExpr f.cf_meta) then abort "Missing function body" f.cf_pos)

else
add_native "std" f.cf_name
| (Meta.HlNative,[] ,_ ) :: _ ->
add_native "std" f.cf_name
| (Meta.HlNative,_ ,p) :: _ ->
abort "Invalid @:hlNative decl" p
| [] ->
ignore(make_fun ctx (s_type_path c.cl_path,f.cf_name) (alloc_fid ctx c f) (match f.cf_expr with Some { eexpr = TFunction f } -> f | _ -> abort "Missing function body" f.cf_pos) None None)
(match f.cf_expr with
| Some { eexpr = TFunction fn } -> ignore(make_fun ctx (s_type_path c.cl_path,f.cf_name) (alloc_fid ctx c f) fn None None)
| _ -> if not (Meta.has Meta.NoExpr f.cf_meta) then abort "Missing function body" f.cf_pos)
| _ :: l ->
loop l
in
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2661,7 +2661,7 @@ let generate con =
let res = ref [] in
Hashtbl.iter (fun name v ->
res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = null_pos } :: !res;
let name = Codegen.escape_res_name name true in
let name = Codegen.escape_res_name name ['/'] in
let full_path = gen.gcon.file ^ "/src/" ^ name in
Path.mkdir_from_path full_path;

Expand Down
11 changes: 9 additions & 2 deletions src/generators/genjvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ open JvmSignature
open JvmMethod
open JvmBuilder
open Genshared
open Tanon_identification

(* Note: This module is the bridge between Haxe structures and JVM structures. No module in generators/jvm should reference any
Haxe-specific type. *)
Expand Down Expand Up @@ -2581,7 +2582,13 @@ class tclass_to_jvm gctx c = object(self)
let field mtype cf = match cf.cf_kind with
| Method (MethNormal | MethInline) ->
List.iter (fun cf ->
if not (has_class_field_flag cf CfExtern) then self#generate_method gctx jc c mtype cf
let is_weird_abstract_field_without_expression = match cf.cf_expr,c.cl_kind with
| None,KAbstractImpl _ ->
true
| _ ->
false
in
if not (has_class_field_flag cf CfExtern) && not (is_weird_abstract_field_without_expression) then self#generate_method gctx jc c mtype cf
) (cf :: List.filter (fun cf -> has_class_field_flag cf CfOverload) cf.cf_overloads)
| _ ->
if not (has_class_flag c CInterface) && is_physical_field cf then self#generate_field gctx jc c mtype cf
Expand Down Expand Up @@ -3067,7 +3074,7 @@ let generate jvm_flag com =
end
) com.native_libs.java_libs in
Hashtbl.iter (fun name v ->
let filename = Codegen.escape_res_name name true in
let filename = Codegen.escape_res_name name ['/';'-'] in
gctx.out#add_entry v filename;
) com.resources;
let generate_real_types () =
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let write_resource dir name data =
let rdir = dir ^ "/res" in
if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
if not (Sys.file_exists rdir) then Unix.mkdir rdir 0o755;
let name = Codegen.escape_res_name name false in
let name = Codegen.escape_res_name name [] in
let ch = open_out_bin (rdir ^ "/" ^ name) in
output_string ch data;
close_out ch
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2270,7 +2270,7 @@ module Generator = struct
end else
","
in
let k_enc = Codegen.escape_res_name k false in
let k_enc = Codegen.escape_res_name k [] in
print ctx "%s\"%s\": open('%%s.%%s'%%(_file,'%s'),'rb').read()" prefix (StringHelper.s_escape k) k_enc;

let f = open_out_bin (ctx.com.file ^ "." ^ k_enc) in
Expand Down
164 changes: 2 additions & 162 deletions src/generators/genshared.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,167 +15,6 @@ let is_extern_abstract a = match a.a_impl with
| ([],("Void" | "Float" | "Int" | "Single" | "Bool" | "Null")) -> true
| _ -> false

open OverloadResolution

type 'a path_field_mapping = {
pfm_path : path;
pfm_params : type_params;
pfm_fields : (string,tclass_field) PMap.t;
mutable pfm_converted : (string * 'a) list option;
pfm_arity : int;
}

let count_fields pm =
PMap.fold (fun _ i -> i + 1) pm 0

let pfm_of_typedef td = match follow td.t_type with
| TAnon an -> {
pfm_path = td.t_path;
pfm_params = td.t_params;
pfm_fields = an.a_fields;
pfm_converted = None;
pfm_arity = count_fields an.a_fields;
}
| _ ->
die "" __LOC__

class ['a] tanon_identification (empty_path : string list * string) =
let is_normal_anon an = match !(an.a_status) with
| Closed | Const -> true
| _ -> false
in
object(self)

val pfms = Hashtbl.create 0
val pfm_by_arity = DynArray.create ()
val mutable num = 0

method get_pfms = pfms

method add_pfm (path : path) (pfm : 'a path_field_mapping) =
while DynArray.length pfm_by_arity <= pfm.pfm_arity do
DynArray.add pfm_by_arity (DynArray.create ())
done;
DynArray.add (DynArray.get pfm_by_arity pfm.pfm_arity) pfm;
Hashtbl.replace pfms path pfm

method unify (tc : Type.t) (pfm : 'a path_field_mapping) =
let check () =
let pair_up fields =
PMap.fold (fun cf acc ->
let cf' = PMap.find cf.cf_name fields in
(cf,cf') :: acc
) pfm.pfm_fields []
in
let monos = match follow tc with
| TInst(c,tl) ->
let pairs = pair_up c.cl_fields in
let monos = List.map (fun _ -> mk_mono()) pfm.pfm_params in
let map = apply_params pfm.pfm_params monos in
List.iter (fun (cf,cf') ->
if not (unify_kind cf'.cf_kind cf.cf_kind) then raise (Unify_error [Unify_custom "kind mismatch"]);
Type.unify (apply_params c.cl_params tl (monomorphs cf'.cf_params cf'.cf_type)) (map (monomorphs cf.cf_params cf.cf_type))
) pairs;
monos
| TAnon an1 ->
let fields = ref an1.a_fields in
let pairs = pair_up an1.a_fields in
let monos = List.map (fun _ -> mk_mono()) pfm.pfm_params in
let map = apply_params pfm.pfm_params monos in
List.iter (fun (cf,cf') ->
if not (unify_kind cf'.cf_kind cf.cf_kind) then raise (Unify_error [Unify_custom "kind mismatch"]);
fields := PMap.remove cf.cf_name !fields;
Type.type_eq EqDoNotFollowNull cf'.cf_type (map (monomorphs cf.cf_params cf.cf_type))
) pairs;
if not (PMap.is_empty !fields) then raise (Unify_error [Unify_custom "not enough fields"]);
monos
| _ ->
raise (Unify_error [Unify_custom "bad type"])
in
(* Check if we applied Void to a return type parameter... (#3463) *)
List.iter (fun t -> match follow t with
| TMono r ->
Monomorph.bind r t_dynamic
| t ->
if Type.ExtType.is_void t then raise(Unify_error [Unify_custom "return mono"])
) monos
in
try
check()
with Not_found ->
raise (Unify_error [])

method find_compatible (arity : int) (tc : Type.t) =
if arity >= DynArray.length pfm_by_arity then
raise Not_found;
let d = DynArray.get pfm_by_arity arity in
let l = DynArray.length d in
let rec loop i =
if i >= l then
raise Not_found;
let pfm = DynArray.unsafe_get d i in
try
self#unify tc pfm;
pfm
with Unify_error _ ->
loop (i + 1)
in
loop 0

method identify_typedef (td : tdef) =
let rec loop t = match t with
| TAnon an when is_normal_anon an && not (PMap.is_empty an.a_fields) ->
self#add_pfm td.t_path (pfm_of_typedef td)
| TMono {tm_type = Some t} ->
loop t
| TLazy f ->
loop (lazy_type f)
| t ->
()
in
loop td.t_type

method identify (accept_anons : bool) (t : Type.t) =
match t with
| TType(td,tl) ->
begin try
Some (Hashtbl.find pfms td.t_path)
with Not_found ->
self#identify accept_anons (apply_typedef td tl)
end
| TMono {tm_type = Some t} ->
self#identify accept_anons t
| TAbstract(a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
self#identify accept_anons (Abstract.get_underlying_type a tl)
| TAbstract({a_path=([],"Null")},[t]) ->
self#identify accept_anons t
| TLazy f ->
self#identify accept_anons (lazy_type f)
| TAnon an when accept_anons && not (PMap.is_empty an.a_fields) ->
let arity = PMap.fold (fun cf i ->
Gencommon.replace_mono cf.cf_type;
i + 1
) an.a_fields 0 in
begin try
Some (self#find_compatible arity t)
with Not_found ->
let id = num in
num <- num + 1;
let path = (["haxe";"generated"],Printf.sprintf "Anon%i" id) in
let pfm = {
pfm_path = path;
pfm_params = [];
pfm_fields = an.a_fields;
pfm_converted = None;
pfm_arity = count_fields an.a_fields;
} in
self#add_pfm path pfm;
Some pfm
end;
| _ ->
None
end

type field_generation_info = {
mutable has_this_before_super : bool;
(* This is an ordered list of fields that are targets of super() calls which is determined during
Expand Down Expand Up @@ -214,7 +53,8 @@ module Info = struct
end

open Info

open OverloadResolution
open Tanon_identification

class ['a] preprocessor (basic : basic_types) (convert : Type.t -> 'a) =
let make_native cf =
Expand Down
13 changes: 11 additions & 2 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1664,10 +1664,19 @@ let decode_type_def v =
EClass (mk flags fields)
| 3, [t] ->
ETypedef (mk (if isExtern then [EExtern] else []) (decode_ctype t))
| 4, [tthis;tfrom;tto] ->
let flags = match opt decode_array tfrom with None -> [] | Some ta -> List.map (fun t -> AbFrom (decode_ctype t)) ta in
| 4, [tthis;tflags;tfrom;tto] ->
let flags = match opt decode_array tflags with
| None -> []
| Some ta -> List.map (fun f -> match decode_enum f with
| 0, [] -> AbEnum
| 1, [ct] -> AbFrom (decode_ctype ct)
| 2, [ct] -> AbTo (decode_ctype ct)
| _ -> raise Invalid_expr
) ta in
let flags = match opt decode_array tfrom with None -> flags | Some ta -> List.map (fun t -> AbFrom (decode_ctype t)) ta @ flags in
let flags = match opt decode_array tto with None -> flags | Some ta -> (List.map (fun t -> AbTo (decode_ctype t)) ta) @ flags in
let flags = match opt decode_ctype tthis with None -> flags | Some t -> (AbOver t) :: flags in
let flags = if isExtern then AbExtern :: flags else flags in
EAbstract(mk flags fields)
| 5, [fk;al] ->
let fk = decode_class_field_kind fk in
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/grammar.mly
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ and parse_class_content doc meta flags n p1 s =
let tl = parse_constraint_params s in
let rec loop had_display p0 acc =
let check_display p1 =
if not had_display && !in_display_file && display_position#enclosed_in p1 then
if not had_display && !in_display_file && !display_mode = DMDefault && display_position#enclosed_in p1 then
syntax_completion (if List.mem HInterface n then SCInterfaceRelation else SCClassRelation) None (display_position#with_pos p1)
in
match s with parser
Expand Down
Loading

0 comments on commit 1a8e9bc

Please sign in to comment.