Skip to content

Commit 4eeb85e

Browse files
committed
[Src] refactoring
1 parent 86440cc commit 4eeb85e

File tree

6 files changed

+75
-75
lines changed

6 files changed

+75
-75
lines changed

src/ast.ml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ module AST = struct
147147
| Skip -> false
148148
| Stmt -> false
149149

150-
let modify_import import v =
151-
{ import; variable = v.variable; field = v.field; summary = v.summary }
152-
153150
let rec last_code p = match p with Seq (_, s) -> last_code s | _ -> p
154151

155152
let rec modify_last_assign p =
@@ -517,6 +514,22 @@ module AST = struct
517514

518515
and is_cn = function ClassName _ -> true | _ -> false
519516

517+
let array_var_code typ v =
518+
match get_array_typ typ with
519+
| Int -> "int" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
520+
| Long -> "long" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
521+
| Short -> "short" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
522+
| Byte -> "byte" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
523+
| Float -> "float" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
524+
| Double -> "double" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
525+
| Char -> "char" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
526+
| String -> "String" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
527+
| Object name ->
528+
(get_short_class_name name |> Utils.replace_nested_symbol)
529+
^ array_code (get_array_dim typ) ""
530+
^ " " ^ snd v
531+
| _ -> ""
532+
520533
let var_code v =
521534
let v =
522535
match v.variable with
@@ -536,21 +549,7 @@ module AST = struct
536549
| String -> "String " ^ snd v
537550
| Object name ->
538551
(get_short_class_name name |> Utils.replace_nested_symbol) ^ " " ^ snd v
539-
| Array typ -> (
540-
match get_array_typ typ with
541-
| Int -> "int" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
542-
| Long -> "long" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
543-
| Short -> "short" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
544-
| Byte -> "byte" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
545-
| Float -> "float" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
546-
| Double -> "double" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
547-
| Char -> "char" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
548-
| String -> "String" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
549-
| Object name ->
550-
(get_short_class_name name |> Utils.replace_nested_symbol)
551-
^ array_code (get_array_dim typ) ""
552-
^ " " ^ snd v
553-
| _ -> "")
552+
| Array typ -> array_var_code typ v
554553
| _ -> ""
555554

556555
let recv_name_code recv func =

src/dug.ml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,6 @@ module DUG = struct
217217

218218
and count_param = function Arg a -> List.length a | Param p -> List.length p
219219

220-
let modify_import import v =
221-
{ import; variable = v.variable; field = v.field; summary = v.summary }
222-
223220
let is_array_init f = Utils.is_array_init (get_func f).method_name
224221

225222
let is_array_set f = Utils.is_array_set (get_func f).method_name
@@ -800,6 +797,22 @@ module DUG = struct
800797

801798
and is_cn = function ClassName _ -> true | _ -> false
802799

800+
let array_var_code typ v =
801+
match get_array_typ typ with
802+
| Int -> "int" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
803+
| Long -> "long" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
804+
| Short -> "short" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
805+
| Byte -> "byte" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
806+
| Float -> "float" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
807+
| Double -> "double" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
808+
| Char -> "char" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
809+
| String -> "String" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
810+
| Object name ->
811+
(get_short_class_name name |> Utils.replace_nested_symbol)
812+
^ array_code (get_array_dim typ) ""
813+
^ " " ^ snd v
814+
| _ -> ""
815+
803816
let var_code v =
804817
let v =
805818
match v.variable with
@@ -819,21 +832,7 @@ module DUG = struct
819832
| String -> "String " ^ snd v
820833
| Object name ->
821834
(get_short_class_name name |> Utils.replace_nested_symbol) ^ " " ^ snd v
822-
| Array typ -> (
823-
match get_array_typ typ with
824-
| Int -> "int" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
825-
| Long -> "long" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
826-
| Short -> "short" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
827-
| Byte -> "byte" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
828-
| Float -> "float" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
829-
| Double -> "double" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
830-
| Char -> "char" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
831-
| String -> "String" ^ array_code (get_array_dim typ) "" ^ " " ^ snd v
832-
| Object name ->
833-
(get_short_class_name name |> Utils.replace_nested_symbol)
834-
^ array_code (get_array_dim typ) ""
835-
^ " " ^ snd v
836-
| _ -> "")
835+
| Array typ -> array_var_code typ v
837836
| _ -> ""
838837

839838
let recv_name_code recv func =

src/generator.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,11 @@ let is_abstract m_name class_name_list m_info c_info =
13531353
else check)
13541354
false class_name_list
13551355

1356+
let is_need_outer_variable m_name class_name c_info =
1357+
is_nested_class class_name
1358+
&& is_static_class class_name c_info |> not
1359+
&& Utils.is_init_method m_name
1360+
13561361
let match_constructor_name class_name method_name =
13571362
let class_name = Str.global_replace Regexp.dollar "\\$" class_name in
13581363
Str.string_match (class_name ^ "\\.<init>" |> Str.regexp) method_name 0
@@ -1378,6 +1383,11 @@ let is_usable_method m_name m_info c_info =
13781383
|| is_usable_default_class (Utils.get_class_name m_name) c_info)
13791384
&& is_available_method m_name m_info
13801385

1386+
let is_not_filtered_method m_name m_info =
1387+
(not (IgnoredMethods.mem m_name !ignored_methods))
1388+
&& (not (!Cmdline.debug && List.mem m_name !Cmdline.ignore))
1389+
&& is_available_method m_name m_info
1390+
13811391
let is_usable_constructor c_name m_name c_info ig =
13821392
is_abstract_class c_name (c_info, ig) |> not
13831393
&& match_constructor_name c_name m_name

src/generatorAST.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,7 @@ let get_void_func id ?(ee = "") ?(es = empty_summary)
546546
if class_name = "" || class_name = "String" then []
547547
else
548548
get_setters class_name setter_map
549-
|> List.filter (fun (s, _) ->
550-
(not (IgnoredMethods.mem s !ignored_methods))
551-
&& (not (!Cmdline.debug && List.mem s !Cmdline.ignore))
552-
&& is_available_method s m_info)
549+
|> List.filter (fun (s, _) -> is_not_filtered_method s m_info)
553550
|> get_setter_list summary
554551
|> mk_void_func var id class_name m_info
555552

@@ -692,7 +689,7 @@ let get_arg_seq (args : ASTIR.id list) =
692689

693690
let mk_arg_seq arg class_name =
694691
let modified_x x =
695-
if is_primitive_from_v x then AST.modify_import class_name x else x
692+
if is_primitive_from_v x then { x with import = class_name } else x
696693
in
697694
get_arg_seq
698695
(List.fold_left
@@ -905,9 +902,8 @@ let recv_in_assign_unroll (prec, (p : ASTIR.t), loop_ids, obj_types)
905902
match p with
906903
| Assign (_, _, f, arg) when AST.recv_in_assign p ->
907904
if
908-
is_nested_class (AST.get_func f).import
909-
&& is_static_class (AST.get_func f).import c_info |> not
910-
&& Utils.is_init_method (AST.get_func f).method_name
905+
is_need_outer_variable (AST.get_func f).method_name
906+
(AST.get_func f).import c_info
911907
then (
912908
let recv, f, arg = get_inner_func f arg in
913909
if (AST.get_v recv).import = "" then

src/generatorDUG.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,10 +577,7 @@ let get_void_func id ?(ee = "") ?(es = empty_summary)
577577
if class_name = "" || class_name = "String" then []
578578
else
579579
get_setters class_name setter_map
580-
|> List.filter (fun (s, _) ->
581-
(not (IgnoredMethods.mem s !ignored_methods))
582-
&& (not (!Cmdline.debug && List.mem s !Cmdline.ignore))
583-
&& is_available_method s m_info)
580+
|> List.filter (fun (s, _) -> is_not_filtered_method s m_info)
584581
|> get_setter_list summary
585582
|> mk_void_func var id class_name m_info
586583

@@ -779,7 +776,7 @@ let get_arg_seq prev_num stmt tc (args : DUGIR.id list) =
779776

780777
let mk_arg_seq arg prev_num class_name s tc =
781778
let modified_x x =
782-
if is_primitive_from_v x then DUG.modify_import class_name x else x
779+
if is_primitive_from_v x then { x with import = class_name } else x
783780
in
784781
get_arg_seq prev_num s tc
785782
(List.fold_left
@@ -988,9 +985,8 @@ let recv_in_assign_unroll (prec, ((s : DUGIR.t), tc), loop_ids, obj_types)
988985
match s with
989986
| Assign (_, _, (x0, _, f, arg)) when DUG.recv_in_assign s ->
990987
if
991-
is_nested_class (DUG.get_func f).import
992-
&& is_static_class (DUG.get_func f).import c_info |> not
993-
&& Utils.is_init_method (DUG.get_func f).method_name
988+
is_need_outer_variable (DUG.get_func f).method_name
989+
(DUG.get_func f).import c_info
994990
then (
995991
let recv, f, arg = get_inner_func f arg in
996992
if (DUG.get_v recv).import = "" then

src/synthesizerDUG.ml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ let insert_loops loop_id_map =
2020
(fun id _ (code, count) -> (code ^ insert_loop id, count + 1))
2121
loop_id_map ("", 0)
2222

23+
let found_id id line =
24+
match Str.search_forward (Str.regexp ("[( ]" ^ id ^ "[^0-9]")) line 0 with
25+
| exception Not_found -> false
26+
| _ -> true
27+
28+
let found_id_pair typ id_code exps prev_ids acc =
29+
List.fold_left
30+
(fun ids (o_typ, o_id_code, o_exps) ->
31+
if o_typ = typ && o_exps = exps then (id_code, o_id_code) :: ids else ids)
32+
acc prev_ids
33+
34+
let get_duplicated_ids loop_id_map =
35+
LoopIdMap.fold
36+
(fun id exps (ids, prev_ids) ->
37+
let typ, id_code = DUG.loop_id_lval_for_check id in
38+
if typ = NonType || id_code = "" then (ids, prev_ids)
39+
else
40+
let ids = found_id_pair typ id_code exps prev_ids ids in
41+
(ids, (typ, id_code, exps) :: prev_ids))
42+
loop_id_map ([], [])
43+
|> fst
44+
2345
let check_same_loop (_, tc) loop_id_map =
2446
let tc_list = String.split_on_char '\n' tc in
25-
let found_id id line =
26-
match Str.search_forward (Str.regexp ("[( ]" ^ id ^ "[^0-9]")) line 0 with
27-
| exception Not_found -> false
28-
| _ -> true
29-
in
30-
let found_id_pair typ id_code exps prev_ids acc =
31-
List.fold_left
32-
(fun ids (o_typ, o_id_code, o_exps) ->
33-
if o_typ = typ && o_exps = exps then (id_code, o_id_code) :: ids
34-
else ids)
35-
acc prev_ids
36-
in
37-
let duplicated_list =
38-
LoopIdMap.fold
39-
(fun id exps (ids, prev_ids) ->
40-
let typ, id_code = DUG.loop_id_lval_for_check id in
41-
if typ = NonType || id_code = "" then (ids, prev_ids)
42-
else
43-
let ids = found_id_pair typ id_code exps prev_ids ids in
44-
(ids, (typ, id_code, exps) :: prev_ids))
45-
loop_id_map ([], [])
46-
|> fst
47-
in
47+
let duplicated_list = get_duplicated_ids loop_id_map in
4848
if duplicated_list = [] then false
4949
else
5050
List.fold_left

0 commit comments

Comments
 (0)