Skip to content

Commit 39993e8

Browse files
committed
Private getters/setters
1 parent 5497f50 commit 39993e8

31 files changed

+269
-55
lines changed

src/codegen/codegen.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ open Extlib_leftovers
2828
let rec has_properties c =
2929
List.exists (fun f ->
3030
match f.cf_kind with
31-
| Var { v_read = AccCall } -> true
32-
| Var { v_write = AccCall } -> true
31+
| Var { v_read = AccCall | AccPrivateCall } -> true
32+
| Var { v_write = AccCall | AccPrivateCall } -> true
3333
| _ when Meta.has Meta.Accessor f.cf_meta -> true
3434
| _ -> false
3535
) c.cl_ordered_fields || (match c.cl_super with Some (c,_) -> has_properties c | _ -> false)
@@ -40,10 +40,10 @@ let get_properties fields =
4040
(f.cf_name, f.cf_name) :: acc
4141
else
4242
let acc = (match f.cf_kind with
43-
| Var { v_read = AccCall } -> ("get_" ^ f.cf_name , "get_" ^ f.cf_name) :: acc
43+
| Var { v_read = AccCall | AccPrivateCall } -> ("get_" ^ f.cf_name , "get_" ^ f.cf_name) :: acc
4444
| _ -> acc) in
4545
match f.cf_kind with
46-
| Var { v_write = AccCall } -> ("set_" ^ f.cf_name , "set_" ^ f.cf_name) :: acc
46+
| Var { v_write = AccCall | AccPrivateCall } -> ("set_" ^ f.cf_name , "set_" ^ f.cf_name) :: acc
4747
| _ -> acc
4848
) [] fields
4949

src/codegen/genxml.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ and gen_field att f =
135135
match acc with
136136
| AccNormal | AccRequire _ | AccCtor -> att
137137
| AccNo | AccNever -> (name, "null") :: att
138-
| AccCall -> (name,"accessor") :: att
138+
| AccCall | AccPrivateCall -> (name,"accessor") :: att
139139
| AccInline -> (name,"inline") :: att
140140
in
141141
let att = (match f.cf_expr with None -> att | Some e -> ("line",string_of_int (Lexer.get_error_line e.epos)) :: att) in

src/compiler/hxb/hxbReader.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ class hxb_reader
993993
let s = self#read_string in
994994
let so = self#read_option (fun () -> self#read_string) in
995995
AccRequire(s,so)
996+
| 7 -> AccPrivateCall
996997
| i ->
997998
error (Printf.sprintf "Bad accessor kind: %i" i)
998999
in

src/compiler/hxb/hxbWriter.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ module HxbWriter = struct
17101710
Chunk.write_u8 writer.chunk 6;
17111711
Chunk.write_string writer.chunk s;
17121712
Chunk.write_option writer.chunk so (Chunk.write_string writer.chunk)
1713+
| AccPrivateCall -> Chunk.write_u8 writer.chunk 7
17131714
in
17141715
f r;
17151716
f w

src/context/display/findReferences.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let rec collect_reference_positions com (name,pos,kind) =
3939
| Var vk ->
4040
let host = FieldAccess.get_host c cf in
4141
let check r mode = match r with
42-
| AccCall ->
42+
| AccCall | AccPrivateCall ->
4343
begin match FieldAccess.find_accessor_for_field host cf cf.cf_type mode with
4444
| AccessorFound (cf_acc,new_host) ->
4545
let c_host = FieldAccess.get_host_class_raise new_host in

src/context/typecore.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,12 @@ let needs_inline ctx c cf =
517517
cf.cf_kind = Method MethInline && ctx.allow_inline && (ctx.com.doinline || is_forced_inline c cf)
518518

519519
(** checks if we can access to a given class field using current context *)
520-
let can_access ctx c cf stat =
521-
if (has_class_field_flag cf CfPublic) then
520+
let can_access ctx c cf ?(check_prop=false) ?(is_setter=false) stat =
521+
let is_not_private_prop = not check_prop || match cf.cf_kind with
522+
| Var { v_read = AccPrivateCall } when not is_setter -> false
523+
| Var { v_write = AccPrivateCall } when is_setter -> false
524+
| _ -> true in
525+
if (is_not_private_prop && has_class_field_flag cf CfPublic) then
522526
true
523527
else if c == ctx.c.curclass then
524528
true

src/core/json/genjson.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ and generate_class_field' ctx cfs cf =
497497
| AccNever -> "AccNever",None
498498
| AccCtor -> "AccCtor",None
499499
| AccCall -> "AccCall",None
500+
| AccPrivateCall -> "AccPrivateCall",None
500501
| AccInline -> "AccInline",None
501502
| AccRequire(s,so) -> "AccRequire",Some (jobject ["require",jstring s;"message",jopt jstring so])
502503
in
@@ -736,4 +737,4 @@ let generate timer_ctx types file =
736737
let ch = open_out_bin file in
737738
Json.write_json (output_string ch) json;
738739
close_out ch;
739-
) ()
740+
) ()

src/core/tOther.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module TExprToExpr = struct
5858
| AccNormal | AccCtor | AccInline | AccRequire _ -> "default"
5959
| AccNo -> "null"
6060
| AccNever -> "never"
61-
| AccCall -> get_or_set
61+
| AccCall | AccPrivateCall -> get_or_set
6262
in
6363
let read = (var_access_to_string v.v_read "get",null_pos) in
6464
let write = (var_access_to_string v.v_write "set",null_pos) in

src/core/tPrinting.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ let s_access is_read = function
164164
| AccNo -> "null"
165165
| AccNever -> "never"
166166
| AccCall -> if is_read then "get" else "set"
167+
| AccPrivateCall -> if is_read then "private get" else "private set"
167168
| AccInline -> "inline"
168169
| AccRequire (n,_) -> "require " ^ n
169170
| AccCtor -> "ctor"

src/core/tType.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and var_access =
1616
| AccNever (* can't be accessed, even in subclasses *)
1717
| AccCtor (* can only be accessed from the constructor *)
1818
| AccCall (* perform a method call when accessed *)
19+
| AccPrivateCall (* perform a method call when accessed, but private like AccNo *)
1920
| AccInline (* similar to Normal but inline when accessed *)
2021
| AccRequire of string * string option (* set when @:require(cond) fails *)
2122

src/core/tUnification.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,12 @@ let unify_access a1 a2 =
511511
a1 = a2 || match a1, a2 with
512512
| _, AccNo | _, AccNever -> true
513513
| AccInline, AccNormal -> true
514+
| AccCall, AccPrivateCall -> true
514515
| _ -> false
515516

516517
let direct_access = function
517518
| AccNo | AccNever | AccNormal | AccInline | AccRequire _ | AccCtor -> true
518-
| AccCall -> false
519+
| AccCall | AccPrivateCall -> false
519520

520521
let unify_kind ~(strict:bool) k1 k2 =
521522
k1 = k2 || match k1, k2 with
@@ -908,8 +909,8 @@ let rec unify (uctx : unification_context) a b =
908909
with Not_found ->
909910
()
910911
in
911-
(match vk.v_read with AccCall -> check ("get_" ^ f1.cf_name) | _ -> ());
912-
(match vk.v_write with AccCall -> check ("set_" ^ f1.cf_name) | _ -> ());
912+
(match vk.v_read with AccCall | AccPrivateCall -> check ("get_" ^ f1.cf_name) | _ -> ());
913+
(match vk.v_write with AccCall | AccPrivateCall -> check ("set_" ^ f1.cf_name) | _ -> ());
913914
| _ -> ()
914915
end;
915916
(match f1.cf_kind with

src/generators/cpp/cppRetyper.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ let get_id path ids =
15051505
| None ->
15061506
let new_id = make_id 0 in
15071507
(new_id, ObjectIds.add path new_id ids)
1508-
1508+
15091509
let native_field_name_remap field =
15101510
match get_meta_string field.cf_meta Meta.Native with
15111511
| Some nativeImpl ->
@@ -1533,7 +1533,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
15331533
tcv_type = field.cf_type;
15341534
tcv_default = None;
15351535

1536-
tcv_has_getter = (match field.cf_kind with | Var { v_read = AccCall } -> true | _ -> false);
1536+
tcv_has_getter = (match field.cf_kind with | Var { v_read = AccCall | AccPrivateCall } -> true | _ -> false);
15371537
tcv_is_stackonly = has_meta Meta.StackOnly field.cf_meta;
15381538
tcv_is_reflective = reflective class_def field;
15391539
tcv_is_gc_element = cpp_type_of field.cf_type |> is_gc_element ctx;
@@ -1567,14 +1567,14 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
15671567
(* We can't implement abstract functions as pure virtual due to cppia needing to construct the class *)
15681568
let map_arg (name, _, t) =
15691569
( (alloc_var VGenerated name t null_pos), (get_default_value name) ) in
1570-
let expr =
1570+
let expr =
15711571
match follow ret with
15721572
| TAbstract ({ a_path = ([], "Void") }, _) ->
15731573
{ eexpr = TReturn None; etype = ret; epos = null_pos }
15741574
| _ ->
15751575
let zero_val = Some { eexpr = TConst (TInt Int32.zero); etype = ret; epos = null_pos } in
15761576
{ eexpr = TReturn zero_val; etype = ret; epos = null_pos } in
1577-
1577+
15781578
{
15791579
tf_args = args |> List.map map_arg;
15801580
tf_type = ret;
@@ -1611,7 +1611,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
16111611
| Var _, _ ->
16121612
Some (create_variable field)
16131613
(* Dynamic methods are implemented as a physical field holding a closure *)
1614-
| Method MethDynamic, Some { eexpr = TFunction func } ->
1614+
| Method MethDynamic, Some { eexpr = TFunction func } ->
16151615
Some (create_variable { field with cf_expr = None; cf_kind = Var ({ v_read = AccNormal; v_write = AccNormal }) })
16161616
(* Below should cause abstracts which have functions with no implementation to be generated as a field *)
16171617
(* See Int32.hx as an example *)
@@ -1631,7 +1631,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
16311631
None in
16321632

16331633
let id, ids = get_id class_def.cl_path ids in
1634-
1634+
16351635
let static_functions =
16361636
class_def.cl_ordered_statics
16371637
|> List.filter_map (filter_functions true) in
@@ -1650,7 +1650,7 @@ let rec tcpp_class_from_tclass ctx ids slots class_def class_params =
16501650
|> List.filter (fun field -> field.cf_name <> "__meta__" && field.cf_name <> "__rtti")
16511651
|> List.filter_map filter_properties in
16521652

1653-
let functions =
1653+
let functions =
16541654
class_def.cl_ordered_fields
16551655
|> List.filter_map (filter_functions true) in
16561656

@@ -1822,4 +1822,4 @@ and tcpp_enum_from_tenum ctx ids enum_def =
18221822
in
18231823
let enum = { te_enum = enum_def; te_id = self_id; te_constructors = constructors } in
18241824

1825-
(ids, enum)
1825+
(ids, enum)

src/generators/cpp/gen/cppCppia.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,7 @@ let generate_script_class common_ctx script class_def =
17991799
| AccNormal | AccCtor -> IaAccessNormal
18001800
| AccNo -> IaAccessNot
18011801
| AccNever -> IaAccessNot
1802-
| AccCall ->
1802+
| AccCall | AccPrivateCall ->
18031803
if
18041804
Meta.has Meta.NativeProperty class_def.cl_meta
18051805
|| Meta.has Meta.NativeProperty field.cf_meta

src/generators/cpp/gen/cppGenClassImplementation.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let gen_function ctx class_def class_name is_static func =
4747
(gen_cpp_function_body ctx class_def is_static func.tcf_field.cf_name func.tcf_func code tail_code);
4848

4949
output "\n\n";
50-
50+
5151
(* generate dynamic version too ... *)
5252
if (not func.tcf_is_virtual || not func.tcf_is_overriding) && func.tcf_is_reflective then
5353
let tcpp_args = List.map (fun (v, _) -> cpp_type_of v.v_type) func.tcf_func.tf_args in
@@ -85,7 +85,7 @@ let gen_function ctx class_def class_name is_static func =
8585
Printf.sprintf "(::cpp::Struct< %s >) a%i" (tcpp_to_string arg) idx
8686
| _ ->
8787
Printf.sprintf "a%i" idx in
88-
88+
8989
tcpp_args
9090
|> ExtList.List.mapi cast_prefix
9191
|> String.concat ", "
@@ -120,7 +120,7 @@ let gen_dynamic_function ctx class_def class_name is_static is_for_static_var (f
120120
let ret = if is_void then "(void)" else "return " in
121121

122122
ctx.ctx_real_this_ptr <- false;
123-
Printf.sprintf "HX_BEGIN_DEFAULT_FUNC(%s, %s)\n" func_name class_name |> output;
123+
Printf.sprintf "HX_BEGIN_DEFAULT_FUNC(%s, %s)\n" func_name class_name |> output;
124124
Printf.sprintf "%s _hx_run(%s)" return_type_str (print_arg_list func.tcf_func.tf_args "__o_") |> output;
125125

126126
gen_cpp_function_body ctx class_def is_static func_name func.tcf_func "" "" no_debug;
@@ -273,7 +273,7 @@ let generate_native_class base_ctx tcpp_class =
273273
output_cpp "\n";
274274

275275
gen_dynamic_function_allocator ctx output_cpp tcpp_class;
276-
276+
277277
generate_native_constructor ctx output_cpp class_def false;
278278
gen_boot_field ctx output_cpp tcpp_class;
279279

@@ -394,13 +394,13 @@ let generate_managed_class base_ctx tcpp_class =
394394
let impl_name = cpp_class_name class_def in
395395

396396
let fold_interface (glued, acc) interface =
397-
397+
398398
let rec gen_interface_funcs interface =
399399

400400
let fold_field (glued, acc) func =
401401
let cast = cpp_tfun_signature false func.iff_args func.iff_return in
402402
let real_name = cpp_member_name_of func.iff_field in
403-
403+
404404
(* C++ can't work out which function it needs to take the addrss of
405405
when the implementation is overloaded - currently the map-set functions.
406406
Change the castKey to force a glue function in this case (could double-cast the pointer, but it is ugly)
@@ -452,7 +452,7 @@ let generate_managed_class base_ctx tcpp_class =
452452
let call = Printf.sprintf "static %s %s_%s = {\n%s\n};\n" (cpp_class_name interface.if_class) cname interface_name combined in
453453
(glued, call :: acc)
454454
in
455-
455+
456456
let glued, calls =
457457
List.fold_left
458458
fold_interface
@@ -615,7 +615,7 @@ let generate_managed_class base_ctx tcpp_class =
615615
else
616616
acc
617617
in
618-
618+
619619
let castable f =
620620
match cpp_type_of f.cf_type with
621621
| TCppInst (t, _) as inst when Meta.has Meta.StructAccess t.cl_meta ->
@@ -671,7 +671,7 @@ let generate_managed_class base_ctx tcpp_class =
671671
Printf.sprintf "%s = inValue.Cast< %s >(); return inValue;" var.tcv_name casted in
672672

673673
match var.tcv_field.cf_kind with
674-
| Var { v_write = AccCall } ->
674+
| Var { v_write = AccCall | AccPrivateCall } ->
675675
let prop_call = checkPropCall var.tcv_field in
676676
let setter = Printf.sprintf "set_%s" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
677677
let call = Printf.sprintf "if (%s) { return ::hx::Val( %s(inValue.Cast< %s >()) ); } else { %s }" prop_call setter casted default in
@@ -690,7 +690,7 @@ let generate_managed_class base_ctx tcpp_class =
690690
let casted = castable var.tcv_field in
691691

692692
match var.tcv_field.cf_kind with
693-
| Var { v_write = AccCall } ->
693+
| Var { v_write = AccCall | AccPrivateCall } ->
694694
let prop_call = checkPropCall var.tcv_field in
695695
let setter = Printf.sprintf "set_%s" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
696696
let call = Printf.sprintf "if (%s) { return ::hx::Val( %s(inValue.Cast< %s >()) ); }" prop_call setter casted in
@@ -717,7 +717,7 @@ let generate_managed_class base_ctx tcpp_class =
717717
let casted = castable var.tcv_field in
718718

719719
match var.tcv_field.cf_kind with
720-
| Var { v_write = AccCall } ->
720+
| Var { v_write = AccCall | AccPrivateCall } ->
721721
let prop_call = checkPropCall var.tcv_field in
722722
let setter = Printf.sprintf "set_%s" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
723723
let call = Printf.sprintf "if (%s) { ioValue = %s(ioValue.Cast< %s >()); } else { %s = ioValue.Cast< %s >(); } return true;" prop_call setter casted var.tcv_name casted in
@@ -734,7 +734,7 @@ let generate_managed_class base_ctx tcpp_class =
734734
let fold_property (var:tcpp_class_variable) acc =
735735
if var.tcv_is_reflective && not (is_abstract_impl class_def) then
736736
match var.tcv_field.cf_kind with
737-
| Var { v_write = AccCall } ->
737+
| Var { v_write = AccCall | AccPrivateCall } ->
738738
let prop_call = checkPropCall var.tcv_field in
739739
let setter = Printf.sprintf "set_%s" var.tcv_field.cf_name |> get_wrapper var.tcv_field in
740740
let casted = castable var.tcv_field in
@@ -965,7 +965,7 @@ let generate_managed_class base_ctx tcpp_class =
965965
| _ -> [] in
966966
current_virtual_functions_rev cls initial
967967
in
968-
968+
969969
flatten_tcpp_class_functions_rec tcpp_class |> List.rev
970970
in
971971

src/generators/flashProps.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ let find_property_for_accessor ~isget cl tl accessor_name =
2929
match Type.class_field cl tl prop_name with
3030
| Some (prop_cl, prop_tl), _, prop_cf ->
3131
(match prop_cf.cf_kind with
32-
| Var { v_read = AccCall; v_write = AccCall | AccNever } when isget && is_flash_property prop_cf -> Some (prop_cl, prop_tl, prop_cf)
33-
| Var { v_read = AccCall | AccNever; v_write = AccCall } when not isget && is_flash_property prop_cf -> Some (prop_cl, prop_tl, prop_cf)
32+
| Var { v_read = AccCall | AccPrivateCall; v_write = AccCall | AccPrivateCall | AccNever } when isget && is_flash_property prop_cf -> Some (prop_cl, prop_tl, prop_cf)
33+
| Var { v_read = AccCall | AccPrivateCall | AccNever; v_write = AccCall | AccPrivateCall } when not isget && is_flash_property prop_cf -> Some (prop_cl, prop_tl, prop_cf)
3434
| _ -> None)
3535
| _ -> None
3636
with Not_found ->
@@ -47,8 +47,8 @@ let find_static_property_for_accessor ~isget cl accessor_name =
4747
try
4848
let prop_cf = PMap.find prop_name cl.cl_statics in
4949
(match prop_cf.cf_kind with
50-
| Var { v_read = AccCall; v_write = AccCall | AccNever } when isget && is_flash_property prop_cf -> Some prop_cf
51-
| Var { v_read = AccCall | AccNever; v_write = AccCall } when not isget && is_flash_property prop_cf -> Some prop_cf
50+
| Var { v_read = AccCall | AccPrivateCall; v_write = AccCall | AccPrivateCall | AccNever } when isget && is_flash_property prop_cf -> Some prop_cf
51+
| Var { v_read = AccCall | AccPrivateCall | AccNever; v_write = AccCall | AccPrivateCall } when not isget && is_flash_property prop_cf -> Some prop_cf
5252
| _ -> None)
5353
with Not_found ->
5454
None

src/generators/genphp7.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,8 @@ class class_builder ctx (cls:tclass) =
38913891
| Var { v_read = read; v_write = write } ->
38923892
if read = AccCall then getters := field.cf_name :: !getters;
38933893
if write = AccCall then setters := field.cf_name :: !setters;
3894+
if read = AccPrivateCall then getters := field.cf_name :: !getters;
3895+
if write = AccPrivateCall then setters := field.cf_name :: !setters;
38943896
| _ -> ()
38953897
in
38963898
List.iter collect cls.cl_ordered_fields;

src/generators/genpy.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,7 +1718,7 @@ module Generator = struct
17181718
match cf.cf_kind with
17191719
| Var _ when not (is_physical_field cf) ->
17201720
()
1721-
| Var({v_read = AccCall}) ->
1721+
| Var({v_read = AccCall | AccPrivateCall}) ->
17221722
if Meta.has Meta.IsVar cf.cf_meta then
17231723
DynArray.add fields cf.cf_name
17241724
else
@@ -1922,7 +1922,7 @@ module Generator = struct
19221922
print ctx " @staticmethod\n def _hx_empty_init(_hx_o):";
19231923
let found_fields = ref false in
19241924
List.iter (fun cf -> match cf.cf_kind with
1925-
| Var ({v_read = AccCall}) ->
1925+
| Var ({v_read = AccCall | AccPrivateCall}) ->
19261926
()
19271927
| Var _ ->
19281928
found_fields := true;

src/macro/macroApi.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ and encode_var_access a =
11261126
| AccInline -> 5, []
11271127
| AccRequire (s,msg) -> 6, [encode_string s; null encode_string msg]
11281128
| AccCtor -> 7, []
1129+
| AccPrivateCall -> 8, []
11291130
) in
11301131
encode_enum IVarAccess tag pl
11311132

@@ -1452,6 +1453,7 @@ let decode_var_access v =
14521453
| 5, [] -> AccInline
14531454
| 6, [s1;s2] -> AccRequire(decode_string s1, opt decode_string s2)
14541455
| 7, [] -> AccCtor
1456+
| 8, [] -> AccPrivateCall
14551457
| _ -> raise Invalid_expr
14561458

14571459
let decode_method_kind v =

0 commit comments

Comments
 (0)