-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from hacspec/question-mark-as-early-returns
Question mark as early returns
- Loading branch information
Showing
24 changed files
with
933 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
module Direct_and_mut = Phase_direct_and_mut.Make | ||
module And_mut_defsite = Phase_and_mut_defsite.Make | ||
module Drop_references = Phase_drop_references.Make | ||
module Drop_blocks = Phase_drop_blocks.Make | ||
module Reconstruct_for_loops = Phase_reconstruct_for_loops.Make | ||
module Reconstruct_while_loops = Phase_reconstruct_while_loops.Make | ||
module Reconstruct_question_marks = Phase_reconstruct_question_marks.Make | ||
module Trivialize_assign_lhs = Phase_trivialize_assign_lhs.Make | ||
module Cf_into_monads = Phase_cf_into_monads.Make | ||
module Functionalize_loops = Phase_functionalize_loops.Make | ||
[%%phases_index ()] | ||
|
||
module Reject = Phase_reject | ||
module Local_mutation = Phase_local_mutation.Make | ||
module Traits_specs = Phase_traits_specs.Make | ||
module Drop_sized_trait = Phase_drop_sized_trait.Make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
open! Prelude | ||
|
||
module Make (F : Features.T) = | ||
Phase_utils.MakeMonomorphicPhase | ||
(F) | ||
(struct | ||
let phase_id = Diagnostics.Phase.DropNeedlessReturns | ||
|
||
open Ast.Make (F) | ||
module U = Ast_utils.Make (F) | ||
module Visitors = Ast_visitors.Make (F) | ||
|
||
module Error = Phase_utils.MakeError (struct | ||
let ctx = Diagnostics.Context.Phase phase_id | ||
end) | ||
|
||
let visitor = | ||
object (self) | ||
inherit [_] Visitors.map as _super | ||
|
||
method! visit_expr () e = | ||
match e with | ||
| { e = Return { e; _ }; _ } -> e | ||
(* we know [e] is on an exit position: the return is | ||
thus useless, we can skip it *) | ||
| { e = Let { monadic = None; lhs; rhs; body }; _ } -> | ||
let body = self#visit_expr () body in | ||
{ e with e = Let { monadic = None; lhs; rhs; body } } | ||
(* If a let expression is an exit node, then it's body | ||
is as well *) | ||
| { e = Match { scrutinee; arms }; _ } -> | ||
let arms = List.map ~f:(self#visit_arm ()) arms in | ||
{ e with e = Match { scrutinee; arms } } | ||
| { e = If { cond; then_; else_ }; _ } -> | ||
let then_ = self#visit_expr () then_ in | ||
let else_ = Option.map ~f:(self#visit_expr ()) else_ in | ||
{ e with e = If { cond; then_; else_ } } | ||
| _ -> e | ||
(** The invariant here is that [visit_expr] is called only | ||
on expressions that are on exit positions. [visit_expr] | ||
is first called on root expressions, which are (by | ||
definition) exit nodes. Then, [visit_expr] itself makes | ||
recursive calls to sub expressions that are themselves | ||
in exit nodes. **) | ||
end | ||
|
||
let ditems = List.map ~f:(visitor#visit_item ()) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(** This phase transforms `return e` expressions into `e` when `return | ||
e` is on an exit position. *) | ||
|
||
module Make : Phase_utils.UNCONSTRAINTED_MONOMORPHIC_PHASE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
open! Prelude | ||
|
||
module Make (F : Features.T) = | ||
Phase_utils.MakeMonomorphicPhase | ||
(F) | ||
(struct | ||
let phase_id = Diagnostics.Phase.SimplifyHoisting | ||
|
||
open Ast.Make (F) | ||
module U = Ast_utils.Make (F) | ||
module Visitors = Ast_visitors.Make (F) | ||
|
||
module Error = Phase_utils.MakeError (struct | ||
let ctx = Diagnostics.Context.Phase phase_id | ||
end) | ||
|
||
let inline_matches = | ||
object (self) | ||
inherit [_] Visitors.map as super | ||
|
||
method! visit_expr () e = | ||
match e with | ||
| { | ||
e = | ||
Let | ||
{ | ||
monadic = None; | ||
lhs = | ||
{ | ||
p = | ||
PBinding | ||
{ | ||
mut = Immutable; | ||
mode = ByValue; | ||
var; | ||
subpat = None; | ||
_; | ||
}; | ||
_; | ||
}; | ||
rhs; | ||
body; | ||
}; | ||
_; | ||
} | ||
when Local_ident.is_side_effect_hoist_var var -> | ||
let body, count = | ||
(object | ||
inherit [_] Visitors.mapreduce as super | ||
method zero = 0 | ||
method plus = ( + ) | ||
|
||
method! visit_expr () e = | ||
match e.e with | ||
| LocalVar v when [%eq: Local_ident.t] v var -> (rhs, 1) | ||
| _ -> super#visit_expr () e | ||
end) | ||
#visit_expr | ||
() body | ||
in | ||
if [%eq: int] count 1 then self#visit_expr () body | ||
else super#visit_expr () e | ||
| _ -> super#visit_expr () e | ||
end | ||
|
||
let ditems = List.map ~f:(inline_matches#visit_item ()) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(** This phase rewrites `let pat = match ... { ... => ..., ... => return ... }; e` | ||
into `match ... { ... => let pat = ...; e}`. *) | ||
|
||
module Make : Phase_utils.UNCONSTRAINTED_MONOMORPHIC_PHASE |
Oops, something went wrong.