2
0

unnecessaryCastsRemoval.ml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. (*
  2. The Haxe Compiler
  3. Copyright (C) 2005-2017 Haxe Foundation
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *)
  16. open Gencommon
  17. open Type
  18. (*
  19. This module will take care of simplifying unnecessary casts, specially those made by the compiler
  20. when inlining. Right now, it will only take care of casts used as a statement, which are always useless;
  21. TODO: Take care of more cases, e.g. when the to and from types are the same
  22. dependencies:
  23. This must run after CastDetection, but before ExpressionUnwrap
  24. *)
  25. let rec take_off_cast run e =
  26. match e.eexpr with
  27. | TCast (c, _) -> take_off_cast run c
  28. | _ -> run e
  29. let rec traverse e =
  30. match e.eexpr with
  31. | TBlock bl ->
  32. let bl = List.map (take_off_cast traverse) bl in
  33. { e with eexpr = TBlock bl }
  34. | TTry (block, catches) ->
  35. { e with eexpr = TTry(traverse (mk_block block), List.map (fun (v,block) -> (v, traverse (mk_block block))) catches) }
  36. | TSwitch (cond,el_e_l, default) ->
  37. { e with eexpr = TSwitch(cond, List.map (fun (el,e) -> (el, traverse (mk_block e))) el_e_l, Option.map (fun e -> traverse (mk_block e)) default) }
  38. | TWhile (cond,block,flag) ->
  39. {e with eexpr = TWhile(cond,traverse (mk_block block), flag) }
  40. | TIf (cond, eif, eelse) ->
  41. { e with eexpr = TIf(cond, traverse (mk_block eif), Option.map (fun e -> traverse (mk_block e)) eelse) }
  42. | TFor (v,it,block) ->
  43. { e with eexpr = TFor(v,it, traverse (mk_block block)) }
  44. | TFunction (tfunc) ->
  45. { e with eexpr = TFunction({ tfunc with tf_expr = traverse (mk_block tfunc.tf_expr) }) }
  46. | _ ->
  47. e (* if expression doesn't have a block, we will exit *)
  48. let name = "casts_removal"
  49. let priority = solve_deps name [DAfter CastDetect.priority; DBefore ExpressionUnwrap.priority]
  50. let configure gen =
  51. gen.gsyntax_filters#add name (PCustom priority) traverse