summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/desugar/libdesugar.hxx
blob: 191bd8dc4b050bad2a165c9428afdf10028d17b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#pragma once

/**
 ** \file desugar/libdesugar.hxx
 ** \brief Functions exported by the desugar module.
 */

#include <memory>

#include <ast/chunk-list.hh>
#include <ast/exp.hh>
#include <bind/libbind.hh>
#include <desugar/bounds-checking-visitor.hh>
#include <desugar/desugar-visitor.hh>
#include <desugar/libdesugar.hh>
#include <escapes/libescapes.hh>
#include <overload/liboverload.hh>
#include <type/libtype.hh>

namespace desugar
{
  /*----------.
  | Helpers.  |
  `----------*/

  template <typename A> void bind_and_types_check(A& tree)
  {
    misc::error e;
    // FIXME DONE: Some code was deleted here.
    e << bind::bind(&tree);
    e.ice_on_error_here();
    e << type::types_check(tree);
    e.ice_on_error_here();
  }

  // Explicit instantiation.
  template void bind_and_types_check<ast::ChunkList>(ast::ChunkList&);

  /*----------.
  | Desugar.  |
  `----------*/

  template <typename A>
  A* raw_desugar(const A& tree, bool desugar_for_p, bool desugar_string_cmp_p)
  {
    // Desugar.
    DesugarVisitor desugar(desugar_for_p, desugar_string_cmp_p);
    desugar(tree);
    return dynamic_cast<A*>(desugar.result_get());
  }

  template <typename A>
  A* desugar(const A& tree, bool desugar_for_p, bool desugar_string_cmp_p)
  {
    // Desugar.
    A* desugared = raw_desugar(tree, desugar_for_p, desugar_string_cmp_p);
    assertion(desugared);
    std::unique_ptr<A> desugared_ptr(desugared);
    // Recompute the bindings and the types.
    bind_and_types_check(*desugared_ptr);
    return desugared_ptr.release();
  }

  /// Explicit instantiations.
  template ast::ChunkList* raw_desugar(const ast::ChunkList&, bool, bool);
  template ast::ChunkList* desugar(const ast::ChunkList&, bool, bool);

  /*-----------------------.
  | Array bounds checking.  |
  `-----------------------*/

  template <typename A> A* raw_bounds_checks_add(const A& tree)
  {
    // Add array bounds checking code.
    BoundsCheckingVisitor add_bounds_checks;
    add_bounds_checks(tree);
    return dynamic_cast<A*>(add_bounds_checks.result_get());
  }

  template <typename A> A* bounds_checks_add(const A& tree)
  {
    // Add bounds checks.
    A* transformed = raw_bounds_checks_add(tree);
    assertion(transformed);
    std::unique_ptr<A> transformed_ptr(transformed);
    // Recompute the bindings and the types.
    bind_and_types_check(*transformed_ptr);
    return transformed_ptr.release();
  }

  /// Explicit instantiations.
  template ast::ChunkList* raw_bounds_checks_add(const ast::ChunkList&);
  template ast::ChunkList* bounds_checks_add(const ast::ChunkList&);

} // namespace desugar