Skip to content

Commit a3d31ba

Browse files
authored
Merge pull request #18 from lefessan/z-2021-06-15-solidity-for-freeton
solidity for freeton
2 parents e4cf67d + 1ba9f06 commit a3d31ba

File tree

7 files changed

+249
-79
lines changed

7 files changed

+249
-79
lines changed

src/solidity-common/solidity_common.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ module Ident = struct
8787
let to_string id = id
8888
let of_string id = id
8989
let printf fmt id = Format.fprintf fmt "%s" id
90-
let constructor = "#"
91-
let onBounce = "!"
92-
let receive = "@"
93-
let fallback = "*"
90+
let constructor = ":constructor"
91+
let onBounce = ":onBounce"
92+
let receive = ":receive"
93+
let fallback = ":fallback"
9494
end
9595

9696
module LongIdent = struct

src/solidity-typechecker/solidity_checker_TYPES.ml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ and variable_desc = {
102102
mutable variable_getter : function_desc option; (* when the variable has a getter*)
103103
variable_is_primitive : bool;
104104
variable_def : Solidity_ast.state_variable_definition option; (* module/contract*)
105+
mutable variable_ops : ( function_desc * variable_operation ) list ;
105106
}
106107

107108
and function_desc = {
@@ -116,8 +117,21 @@ and function_desc = {
116117
function_is_method : bool;
117118
function_is_primitive : bool;
118119
function_def : Solidity_ast.function_definition option; (* Primitives have no definition *)
120+
mutable function_ops : ( variable_desc * variable_operation ) list ;
121+
mutable function_purity : function_purity ;
119122
}
120123

124+
and function_purity = (* whether it modifies its contract *)
125+
| PurityUnknown
126+
| PurityPure
127+
| PurityView
128+
| PurityMute
129+
130+
and variable_operation =
131+
| OpAssign
132+
| OpAccess
133+
| OpCall of function_desc
134+
121135
and modifier_desc = {
122136
modifier_abs_name : absolute LongIdent.t;
123137
mutable modifier_params : (type_ * Ident.t option) list;
@@ -241,7 +255,7 @@ type options = {
241255
call_args: args option; (* could just have an in_lvalue flag *)
242256
fun_returns : type_ list;
243257
in_loop: bool;
244-
in_function: bool;
258+
in_function: function_desc option;
245259
in_modifier: bool;
246260
current_hierarchy: absolute LongIdent.t list;
247261
current_contract: contract_desc option;

src/solidity-typechecker/solidity_primitives.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,29 @@ open Solidity_exceptions
1717

1818
let error = type_error
1919

20+
module UTILS = struct
21+
2022
let register id p f_desc =
2123
Solidity_common.add_primitive id p;
2224
Solidity_tenv.add_primitive_desc id f_desc
2325

26+
let primitive_fun_named ?(returns_lvalue=false)
27+
?(purity=PurityPure)
28+
arg_types ret_types function_mutability =
29+
Function { function_abs_name = LongIdent.empty;
30+
function_params = arg_types;
31+
function_returns = List.map (fun t -> (t, None)) ret_types;
32+
function_returns_lvalue = returns_lvalue;
33+
function_visibility = VPublic;
34+
function_mutability;
35+
function_override = None;
36+
function_selector = None;
37+
function_is_method = false; (* can be true *)
38+
function_is_primitive = true;
39+
function_def = None;
40+
function_ops = [];
41+
function_purity = purity;
42+
}
2443

2544
let make_fun = Solidity_type_builder.primitive_fun
2645

@@ -47,6 +66,8 @@ let preprocess_arg_1 pos t atl_opt =
4766
error pos "Need at least 1 argument for function \
4867
call, but provided only 0"
4968

69+
end
70+
open UTILS
5071

5172
let register_primitives () =
5273

src/solidity-typechecker/solidity_primitives.mli

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,39 @@
1111
(**************************************************************************)
1212

1313
val init : unit -> unit
14+
15+
module UTILS : sig
16+
val register :
17+
int ->
18+
Solidity_common.primitive ->
19+
(Solidity_common.pos ->
20+
Solidity_checker_TYPES.options ->
21+
Solidity_checker_TYPES.type_ option ->
22+
Solidity_checker_TYPES.ident_desc option) ->
23+
unit
24+
val primitive_fun_named :
25+
?returns_lvalue:bool ->
26+
?purity:Solidity_checker_TYPES.function_purity ->
27+
(Solidity_checker_TYPES.type_ *
28+
Solidity_common.IdentSet.elt option)
29+
list ->
30+
Solidity_checker_TYPES.type_ list ->
31+
Solidity_ast.fun_mutability -> Solidity_checker_TYPES.ident_desc
32+
33+
val make_var :
34+
Solidity_checker_TYPES.type_ -> Solidity_checker_TYPES.ident_desc
35+
val make_fun :
36+
?returns_lvalue:bool ->
37+
?purity:Solidity_checker_TYPES.function_purity ->
38+
Solidity_checker_TYPES.type_ list ->
39+
Solidity_checker_TYPES.type_ list ->
40+
Solidity_ast.fun_mutability -> Solidity_checker_TYPES.ident_desc
41+
val make_prim_args :
42+
Solidity_common.pos ->
43+
Solidity_checker_TYPES.options ->
44+
Solidity_checker_TYPES.type_ list option
45+
val preprocess_arg_0 : 'a -> 'b list option -> 'b list
46+
val preprocess_arg_1 :
47+
Solidity_common.pos -> 'a -> 'a list option -> 'a list
48+
49+
end

src/solidity-typechecker/solidity_tenv.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ let find_constructor pos { contract_abs_name; contract_env; _ } =
194194
function_selector = None;
195195
function_is_method = true;
196196
function_is_primitive = false;
197-
function_def = None; }
197+
function_def = None;
198+
function_ops = [];
199+
function_purity = PurityUnknown;
200+
}
198201

199202
let has_abstract_function cd =
200203
let exception Found of Ident.t in

src/solidity-typechecker/solidity_type_builder.ml

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ and function_type_to_desc pos env ft =
257257
function_selector = None;
258258
function_is_method = false;
259259
function_is_primitive = false;
260-
function_def = None; }
260+
function_def = None;
261+
function_ops = [];
262+
function_purity = PurityUnknown;
263+
}
261264

262265
and process_fun_params pos env ~ext params =
263266
List.map (fun (t, loc_opt, name_opt) ->
@@ -321,7 +324,10 @@ let variable_desc_to_function_desc pos vid variable_abs_name vt :
321324
function_selector;
322325
function_is_method = true;
323326
function_is_primitive = false;
324-
function_def = None; }
327+
function_def = None;
328+
function_ops = [];
329+
function_purity = PurityUnknown;
330+
}
325331

326332
(* Build the function corresponding to an event *)
327333
let event_desc_to_function_desc (ed : event_desc) : function_desc =
@@ -335,7 +341,10 @@ let event_desc_to_function_desc (ed : event_desc) : function_desc =
335341
function_selector = None;
336342
function_is_method = false;
337343
function_is_primitive = false;
338-
function_def = None; }
344+
function_def = None;
345+
function_ops = [];
346+
function_purity = PurityUnknown;
347+
}
339348

340349
(* Make a ident description for a local variable *)
341350
let local_variable_desc variable_type : variable_desc =
@@ -347,7 +356,9 @@ let local_variable_desc variable_type : variable_desc =
347356
variable_override = None;
348357
variable_getter = None;
349358
variable_is_primitive = false;
350-
variable_def = None; }
359+
variable_def = None;
360+
variable_ops = [] ;
361+
}
351362

352363

353364

@@ -395,7 +406,9 @@ let make_variable_desc vlid vd =
395406
variable_override = None;
396407
variable_getter = None;
397408
variable_is_primitive = false;
398-
variable_def = Some (vd); }
409+
variable_def = Some (vd);
410+
variable_ops = [] ;
411+
}
399412

400413
let update_variable_desc pos env vd kind_opt =
401414
let vd' =
@@ -428,7 +441,10 @@ let make_function_desc flid fd method_ =
428441
function_selector = None;
429442
function_is_method = method_;
430443
function_is_primitive = false;
431-
function_def = Some (fd); }
444+
function_def = Some (fd);
445+
function_ops = [];
446+
function_purity = PurityUnknown;
447+
}
432448

433449
let update_function_desc pos env fd kind_opt =
434450
let fd' =
@@ -468,6 +484,7 @@ let update_struct_fields sd fields =
468484
(* Functions to build primitive types/desc *)
469485

470486
let primitive_fun_desc ?(returns_lvalue=false)
487+
?(purity=PurityPure)
471488
arg_types ret_types function_mutability =
472489
{ function_abs_name = LongIdent.empty;
473490
function_params = List.map (fun t -> (t, None)) arg_types;
@@ -479,17 +496,20 @@ let primitive_fun_desc ?(returns_lvalue=false)
479496
function_selector = None;
480497
function_is_method = false; (* can be true *)
481498
function_is_primitive = true;
482-
function_def = None; }
499+
function_def = None;
500+
function_ops = [];
501+
function_purity = purity;
502+
}
483503

484504
let primitive_fun_type ?(kind=KOther) ?(returns_lvalue=false)
485505
arg_types ret_types function_mutability =
486506
let fd = primitive_fun_desc ~returns_lvalue
487507
arg_types ret_types function_mutability in
488508
TFunction (fd, { new_fun_options with kind })
489509

490-
let primitive_fun ?(returns_lvalue=false)
510+
let primitive_fun ?(returns_lvalue=false) ?purity
491511
arg_types ret_types function_mutability =
492-
let fd = primitive_fun_desc ~returns_lvalue
512+
let fd = primitive_fun_desc ~returns_lvalue ?purity
493513
arg_types ret_types function_mutability in
494514
Function (fd)
495515

@@ -502,7 +522,9 @@ let primitive_var_desc (*?(is_lvalue=false)*) variable_type =
502522
variable_override = None;
503523
variable_getter = None;
504524
variable_is_primitive = true;
505-
variable_def = None; }
525+
variable_def = None;
526+
variable_ops = [] ;
527+
}
506528

507529
let primitive_var (*?(is_lvalue=false)*) variable_type =
508530
let vd = primitive_var_desc variable_type in

0 commit comments

Comments
 (0)