Skip to content

Commit 0b675a0

Browse files
committed
Make mutability no longer a type constructor
1 parent f02f9cb commit 0b675a0

File tree

11 files changed

+105
-75
lines changed

11 files changed

+105
-75
lines changed

src/boot/fe/ast.ml

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type effect =
3535
| UNSAFE
3636
;;
3737

38+
type mutability =
39+
MUT_mutable
40+
| MUT_immutable
41+
;;
42+
3843
type name_base =
3944
BASE_ident of ident
4045
| BASE_temp of temp_id
@@ -187,21 +192,21 @@ and ty_obj = (effect * ((ident,ty_fn) Hashtbl.t))
187192

188193
and check_calls = (lval * (atom array)) array
189194

190-
and rec_input = (ident * atom)
195+
and rec_input = (ident * mutability * atom)
191196

192-
and tup_input = atom
197+
and tup_input = (mutability * atom)
193198

194199
and stmt' =
195200

196201
(* lval-assigning stmts. *)
197202
STMT_spawn of (lval * domain * lval * (atom array))
198203
| STMT_init_rec of (lval * (rec_input array) * lval option)
199204
| STMT_init_tup of (lval * (tup_input array))
200-
| STMT_init_vec of (lval * atom array)
205+
| STMT_init_vec of (lval * mutability * atom array)
201206
| STMT_init_str of (lval * string)
202207
| STMT_init_port of lval
203208
| STMT_init_chan of (lval * (lval option))
204-
| STMT_init_box of (lval * atom)
209+
| STMT_init_box of (lval * mutability * atom)
205210
| STMT_copy of (lval * expr)
206211
| STMT_copy_binop of (lval * binop * atom)
207212
| STMT_call of (lval * lval * (atom array))
@@ -1018,7 +1023,8 @@ and fmt_stmt_body (ff:Format.formatter) (s:stmt) : unit =
10181023
do
10191024
if i != 0
10201025
then fmt ff ", ";
1021-
let (ident, atom) = entries.(i) in
1026+
let (ident, mutability, atom) = entries.(i) in
1027+
if mutability = MUT_mutable then fmt ff "mutable ";
10221028
fmt_ident ff ident;
10231029
fmt ff " = ";
10241030
fmt_atom ff atom;
@@ -1032,9 +1038,11 @@ and fmt_stmt_body (ff:Format.formatter) (s:stmt) : unit =
10321038
end;
10331039
fmt ff ");"
10341040

1035-
| STMT_init_vec (dst, atoms) ->
1041+
| STMT_init_vec (dst, mutability, atoms) ->
10361042
fmt_lval ff dst;
1037-
fmt ff " = vec(";
1043+
fmt ff " = vec";
1044+
if mutability = MUT_mutable then fmt ff "[mutable]";
1045+
fmt ff "(";
10381046
for i = 0 to (Array.length atoms) - 1
10391047
do
10401048
if i != 0
@@ -1050,7 +1058,9 @@ and fmt_stmt_body (ff:Format.formatter) (s:stmt) : unit =
10501058
do
10511059
if i != 0
10521060
then fmt ff ", ";
1053-
fmt_atom ff entries.(i);
1061+
let (mutability, atom) = entries.(i) in
1062+
if mutability = MUT_mutable then fmt ff "mutable ";
1063+
fmt_atom ff atom;
10541064
done;
10551065
fmt ff ");";
10561066

@@ -1166,9 +1176,10 @@ and fmt_stmt_body (ff:Format.formatter) (s:stmt) : unit =
11661176
fmt_lval ff t;
11671177
fmt ff ";"
11681178

1169-
| STMT_init_box (lv, at) ->
1179+
| STMT_init_box (lv, mutability, at) ->
11701180
fmt_lval ff lv;
11711181
fmt ff " = @@";
1182+
if mutability = MUT_mutable then fmt ff " mutable ";
11721183
fmt_atom ff at;
11731184
fmt ff ";"
11741185

src/boot/fe/pexp.ml

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type pexp' =
2020
PEXP_call of (pexp * pexp array)
2121
| PEXP_spawn of (Ast.domain * pexp)
2222
| PEXP_bind of (pexp * pexp option array)
23-
| PEXP_rec of ((Ast.ident * pexp) array * pexp option)
24-
| PEXP_tup of (pexp array)
25-
| PEXP_vec of (pexp array)
23+
| PEXP_rec of ((Ast.ident * Ast.mutability * pexp) array * pexp option)
24+
| PEXP_tup of ((Ast.mutability * pexp) array)
25+
| PEXP_vec of Ast.mutability * (pexp array)
2626
| PEXP_port
2727
| PEXP_chan of (pexp option)
2828
| PEXP_binop of (Ast.binop * pexp * pexp)
@@ -32,8 +32,7 @@ type pexp' =
3232
| PEXP_lval of plval
3333
| PEXP_lit of Ast.lit
3434
| PEXP_str of string
35-
| PEXP_mutable of pexp
36-
| PEXP_box of pexp
35+
| PEXP_box of Ast.mutability * pexp
3736
| PEXP_custom of Ast.name * (pexp array) * (string option)
3837

3938
and plval =
@@ -177,6 +176,11 @@ and parse_effect (ps:pstate) : Ast.effect =
177176
| UNSAFE -> bump ps; Ast.UNSAFE
178177
| _ -> Ast.PURE
179178

179+
and parse_mutability (ps:pstate) : Ast.mutability =
180+
match peek ps with
181+
MUTABLE -> bump ps; Ast.MUT_mutable
182+
| _ -> Ast.MUT_immutable
183+
180184
and parse_ty_fn
181185
(effect:Ast.effect)
182186
(ps:pstate)
@@ -421,13 +425,14 @@ and parse_ty (ps:pstate) : Ast.ty =
421425
parse_constrained_ty ps
422426

423427

424-
and parse_rec_input (ps:pstate) : (Ast.ident * pexp) =
428+
and parse_rec_input (ps:pstate) : (Ast.ident * Ast.mutability * pexp) =
429+
let mutability = parse_mutability ps in
425430
let lab = (ctxt "rec input: label" parse_ident ps) in
426431
match peek ps with
427432
EQ ->
428433
bump ps;
429434
let pexp = ctxt "rec input: expr" parse_pexp ps in
430-
(lab, pexp)
435+
(lab, mutability, pexp)
431436
| _ -> raise (unexpected ps)
432437

433438

@@ -439,7 +444,7 @@ and parse_rec_body (ps:pstate) : pexp' = (*((Ast.ident * pexp) array) =*)
439444
| WITH -> raise (err "empty record extension" ps)
440445
| _ ->
441446
let inputs = one_or_more COMMA parse_rec_input ps in
442-
let labels = Array.map (fun (l, _) -> l) inputs in
447+
let labels = Array.map (fun (l, _, _) -> l) inputs in
443448
begin
444449
check_dup_rec_labels ps labels;
445450
match peek ps with
@@ -472,21 +477,18 @@ and parse_bottom_pexp (ps:pstate) : pexp =
472477
let apos = lexpos ps in
473478
match peek ps with
474479

475-
MUTABLE ->
476-
bump ps;
477-
let inner = parse_pexp ps in
478-
let bpos = lexpos ps in
479-
span ps apos bpos (PEXP_mutable inner)
480-
481-
| AT ->
480+
AT ->
482481
bump ps;
482+
let mutability = parse_mutability ps in
483483
let inner = parse_pexp ps in
484484
let bpos = lexpos ps in
485-
span ps apos bpos (PEXP_box inner)
485+
span ps apos bpos (PEXP_box (mutability, inner))
486486

487487
| TUP ->
488488
bump ps;
489-
let pexps = ctxt "paren pexps(s)" (rstr false parse_pexp_list) ps in
489+
let pexps =
490+
ctxt "paren pexps(s)" (rstr false parse_mutable_and_pexp_list) ps
491+
in
490492
let bpos = lexpos ps in
491493
span ps apos bpos (PEXP_tup pexps)
492494

@@ -498,11 +500,18 @@ and parse_bottom_pexp (ps:pstate) : pexp =
498500

499501
| VEC ->
500502
bump ps;
501-
begin
502-
let pexps = ctxt "vec pexp: exprs" parse_pexp_list ps in
503-
let bpos = lexpos ps in
504-
span ps apos bpos (PEXP_vec pexps)
505-
end
503+
let mutability =
504+
match peek ps with
505+
LBRACKET ->
506+
bump ps;
507+
expect ps MUTABLE;
508+
expect ps RBRACKET;
509+
Ast.MUT_mutable
510+
| _ -> Ast.MUT_immutable
511+
in
512+
let pexps = ctxt "vec pexp: exprs" parse_pexp_list ps in
513+
let bpos = lexpos ps in
514+
span ps apos bpos (PEXP_vec (mutability, pexps))
506515

507516

508517
| LIT_STR s ->
@@ -947,6 +956,9 @@ and parse_as_pexp (ps:pstate) : pexp =
947956
and parse_pexp (ps:pstate) : pexp =
948957
parse_as_pexp ps
949958

959+
and parse_mutable_and_pexp (ps:pstate) : (Ast.mutability * pexp) =
960+
let mutability = parse_mutability ps in
961+
(mutability, parse_as_pexp ps)
950962

951963
and parse_pexp_list (ps:pstate) : pexp array =
952964
match peek ps with
@@ -955,6 +967,13 @@ and parse_pexp_list (ps:pstate) : pexp array =
955967
(ctxt "pexp list" parse_pexp) ps
956968
| _ -> raise (unexpected ps)
957969

970+
and parse_mutable_and_pexp_list (ps:pstate) : (Ast.mutability * pexp) array =
971+
match peek ps with
972+
LPAREN ->
973+
bracketed_zero_or_more LPAREN RPAREN (Some COMMA)
974+
(ctxt "mutable-and-pexp list" parse_mutable_and_pexp) ps
975+
| _ -> raise (unexpected ps)
976+
958977
;;
959978

960979
(*
@@ -1099,8 +1118,7 @@ and desugar_expr_atom
10991118
| PEXP_bind _
11001119
| PEXP_spawn _
11011120
| PEXP_custom _
1102-
| PEXP_box _
1103-
| PEXP_mutable _ ->
1121+
| PEXP_box _ ->
11041122
let (_, tmp, decl_stmt) = build_tmp ps slot_auto apos bpos in
11051123
let stmts = desugar_expr_init ps tmp pexp in
11061124
(Array.append [| decl_stmt |] stmts,
@@ -1233,11 +1251,11 @@ and desugar_expr_init
12331251
begin
12341252
Array.map
12351253
begin
1236-
fun (ident, pexp) ->
1254+
fun (ident, mutability, pexp) ->
12371255
let (stmts, atom) =
12381256
desugar_expr_atom ps pexp
12391257
in
1240-
(stmts, (ident, atom))
1258+
(stmts, (ident, mutability, atom))
12411259
end
12421260
args
12431261
end
@@ -1259,19 +1277,24 @@ and desugar_expr_init
12591277
end
12601278

12611279
| PEXP_tup args ->
1280+
let muts = Array.to_list (Array.map fst args) in
12621281
let (arg_stmts, arg_atoms) =
1263-
desugar_expr_atoms ps args
1282+
desugar_expr_atoms ps (Array.map snd args)
12641283
in
1265-
let stmt = ss (Ast.STMT_init_tup (dst_lval, arg_atoms)) in
1284+
let arg_atoms = Array.to_list arg_atoms in
1285+
let tup_args = Array.of_list (List.combine muts arg_atoms) in
1286+
let stmt = ss (Ast.STMT_init_tup (dst_lval, tup_args)) in
12661287
aa arg_stmts [| stmt |]
12671288

12681289
| PEXP_str s ->
12691290
let stmt = ss (Ast.STMT_init_str (dst_lval, s)) in
12701291
[| stmt |]
12711292

1272-
| PEXP_vec args ->
1293+
| PEXP_vec (mutability, args) ->
12731294
let (arg_stmts, arg_atoms) = desugar_expr_atoms ps args in
1274-
let stmt = ss (Ast.STMT_init_vec (dst_lval, arg_atoms)) in
1295+
let stmt =
1296+
ss (Ast.STMT_init_vec (dst_lval, mutability, arg_atoms))
1297+
in
12751298
aa arg_stmts [| stmt |]
12761299

12771300
| PEXP_port ->
@@ -1296,20 +1319,15 @@ and desugar_expr_init
12961319
in
12971320
aa port_stmts [| chan_stmt |]
12981321

1299-
| PEXP_box arg ->
1322+
| PEXP_box (mutability, arg) ->
13001323
let (arg_stmts, arg_mode_atom) =
13011324
desugar_expr_atom ps arg
13021325
in
1303-
let stmt = ss (Ast.STMT_init_box (dst_lval, arg_mode_atom)) in
1326+
let stmt =
1327+
ss (Ast.STMT_init_box (dst_lval, mutability, arg_mode_atom))
1328+
in
13041329
aa arg_stmts [| stmt |]
13051330

1306-
| PEXP_mutable arg ->
1307-
(* Initializing a local from a "mutable" atom is the same as
1308-
* initializing it from an immutable one; all locals are mutable
1309-
* anyways. So this is just a fall-through.
1310-
*)
1311-
desugar_expr_init ps dst_lval arg
1312-
13131331
| PEXP_custom (n, a, b) ->
13141332
let (arg_stmts, args) = desugar_expr_atoms ps a in
13151333
let stmts =

src/boot/llvm/lltrans.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,18 @@ let trans_crate
761761
let trans_tail () = trans_tail_with_builder llbuilder in
762762

763763
match head.node with
764-
Ast.STMT_init_tup (dest, atoms) ->
764+
Ast.STMT_init_tup (dest, elems) ->
765765
let zero = const_i32 0 in
766766
let lldest = trans_lval dest in
767-
let trans_tup_atom idx atom =
767+
let trans_tup_elem idx (_, atom) =
768768
let indices = [| zero; const_i32 idx |] in
769769
let gep_id = anon_llid "init_tup_gep" in
770770
let ptr =
771771
Llvm.build_gep lldest indices gep_id llbuilder
772772
in
773773
ignore (Llvm.build_store (trans_atom atom) ptr llbuilder)
774774
in
775-
Array.iteri trans_tup_atom atoms;
775+
Array.iteri trans_tup_elem elems;
776776
trans_tail ()
777777

778778
| Ast.STMT_copy (dest, src) ->

src/boot/me/alias.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let alias_analysis_visitor
6767
| Ast.STMT_recv (dst, _) -> alias dst
6868
| Ast.STMT_init_port (dst) -> alias dst
6969
| Ast.STMT_init_chan (dst, _) -> alias dst
70-
| Ast.STMT_init_vec (dst, _) -> alias dst
70+
| Ast.STMT_init_vec (dst, _, _) -> alias dst
7171
| Ast.STMT_init_str (dst, _) -> alias dst
7272
| Ast.STMT_for_each sfe ->
7373
let (slot, _) = sfe.Ast.for_each_slot in

src/boot/me/semant.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,13 @@ let atoms_slots (cx:ctxt) (az:Ast.atom array) : node_id array =
583583
;;
584584

585585
let tup_inputs_slots (cx:ctxt) (az:Ast.tup_input array) : node_id array =
586-
Array.concat (List.map (atom_slots cx) (Array.to_list az))
586+
Array.concat (List.map (atom_slots cx) (Array.to_list (Array.map snd az)))
587587
;;
588588

589589
let rec_inputs_slots (cx:ctxt)
590590
(inputs:Ast.rec_input array) : node_id array =
591591
Array.concat (List.map
592-
(fun (_, atom) -> atom_slots cx atom)
592+
(fun (_, _, atom) -> atom_slots cx atom)
593593
(Array.to_list inputs))
594594
;;
595595

0 commit comments

Comments
 (0)