summaryrefslogtreecommitdiff
path: root/tiger-compiler/src/ast/dumper-dot.hxx
blob: 30a7b33213d221b7abc3d260cae48d8b5a8c27e4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 ** \file ast/dumper-dot.hxx
 ** \brief Implementation of ast::DumperDot.
 */

#pragma once

#include <cstdint>
#include <ast/dumper-dot.hh>
#include <misc/indent.hh>

namespace ast
{

  template <typename Container>
    requires misc::ConstIterable<Container>
  inline void DumperDot::dump_list(const std::string& field, const Container& l)
  {
    const std::string* old_parent_field = parent_field;
    auto it = l.begin();
    unsigned n = 0;
    while (it != l.end())
      {
        std::ostringstream o;
        o << field;
        if (std::next(it) != l.end() || n > 0)
          o << n++;
        const std::string field_name = o.str();
        parent_field = &field_name;
        (*it++)->accept(*this);
      }
    parent_field = old_parent_field;
  }

  template <typename T> inline void DumperDot::dump_def(const T& e) const
  {
    const ast::Ast* d = nullptr;
    // FIXME: Some code was deleted here (set d using definition of e).
    (void)e;
    if (!d)
      return;
    ostr_ << parent_id << ":def:s -> " << reinterpret_cast<std::uintptr_t>(d)
          << ":nodename [constraint=false, style=dashed, color=\"dimgray\"]"
          << misc::iendl;
  }

  inline void DumperDot::display_link(unsigned long old_parent_id) const
  {
    if (parent_field)
      ostr_ << old_parent_id << ":" << *parent_field << ":s"
            << " -> " << parent_id << ":nodename:n" << misc::iendl;
  }

  inline void DumperDot::footer_and_link(unsigned long old_parent_id) const
  {
    node_html_footer();
    display_link(old_parent_id);
  }

  template <typename E>
  inline void DumperDot::dump_chunk(const E& e, const std::string& name)
  {
    unsigned long old_parent_id = parent_id;
    parent_id = reinterpret_cast<std::uintptr_t>(&e);
    ostr_ << parent_id << " [label=<" << misc::incendl
          << "<table cellborder='0' cellspacing='0'>" << misc::incendl << "<tr>"
          << misc::incendl;
    inner_fields = 0;
    node_html_port_list(name, e, true);
    ostr_ << misc::decendl << "</tr>" << misc::decendl << "</table>"
          << misc::decendl << ">]" << misc::iendl;
    display_link(old_parent_id);
    dump_list("nodename", e);
    parent_id = old_parent_id;
  }

  namespace
  {
    inline void node_html_begin_inner(std::ostream& ostr, bool list = false)
    {
      ostr << "<td cellpadding='0'>" << misc::incendl
           << "<table border='0' cellborder='" << (list ? 0 : 1) << "'"
           << " cellspacing='0' cellpadding='" << (list ? 0 : 2) << "'>"
           << misc::incendl << "<tr>" << misc::incendl;
    }

    inline void node_html_end_inner(std::ostream& ostr)
    {
      ostr << misc::decendl << "</tr>" << misc::decendl << "</table>"
           << misc::decendl << "</td>";
    }

    inline void node_html_separator(std::ostream& ostr)
    {
      node_html_end_inner(ostr);
      ostr << misc::decendl << "</tr>" << misc::iendl << "<tr>"
           << misc::incendl;
      node_html_begin_inner(ostr);
    }
    inline void node_html_tr(std::ostream& ostr,
                             const std::string& port,
                             const std::string content)
    {
      ostr << "<td port='" << port << "'>" << content << "</td>";
    }
    inline bool ends_with(const std::string& value, const std::string& ending)
    {
      if (ending.size() > value.size())
        return false;
      return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
    }
    inline std::string node_html_color(const std::string& type)
    {
      if (ends_with(type, "Dec"))
        return "red1";
      else if (ends_with(type, "Var"))
        return "orange1";
      else if (ends_with(type, "Ty"))
        return "green3";
      else if (ends_with(type, "Exp"))
        return "blue2";
      return "black";
    }

    template <typename T> std::string html_escape(const T& input)
    {
      std::ostringstream i;
      i << input;
      const std::string& str = i.str();
      std::ostringstream o;
      const std::string& specials = "&<>";
      for (const auto& p : str)
        if (p == '\\')
          o << '\\' << '\\';
        else if (specials.find(p) != std::string::npos)
          o << "&#" << static_cast<int>(static_cast<unsigned char>(p)) << ";";
        else
          o << p;
      return o.str();
    }
  } // namespace

  template <typename T>
  inline unsigned long DumperDot::node_html_header(const T& e,
                                                   const std::string& type)
  {
    unsigned long old_parent_id = parent_id;
    parent_id = reinterpret_cast<std::uintptr_t>(&e);
    ostr_ << parent_id << " [label=<" << misc::incendl
          << "<table border='0' cellborder='0' cellspacing='0' cellpadding='0'"
          << " color='" << node_html_color(type) << "'>" << misc::incendl
          << "<tr>" << misc::incendl;
    node_html_begin_inner(ostr_);
    node_html_tr(ostr_, "nodename", type);
    node_html_separator(ostr_);
    inner_fields = 0;
    return old_parent_id;
  }
  template <typename T>
  inline void DumperDot::node_html_field(const std::string& name,
                                         const T& content,
                                         const std::string& sep)
  {
    std::ostringstream o;
    o << name << ":&nbsp;" << sep << html_escape(content) << sep;
    if (inner_fields++)
      ostr_ << misc::iendl;
    node_html_tr(ostr_, name, o.str());
  }
  inline void DumperDot::node_html_one_port(const std::string& p)
  {
    if (inner_fields++)
      ostr_ << misc::iendl;
    node_html_tr(ostr_, p, p);
  }
  inline void DumperDot::node_html_ports(const std::vector<std::string>& ports)
  {
    if (inner_fields)
      node_html_separator(ostr_);
    inner_fields = 0;
    for (auto p : ports)
      node_html_one_port(p);
  }
  template <typename T>
  inline void DumperDot::node_html_port_list(const std::string& name,
                                             const T& list,
                                             bool chunk)
  {
    if (inner_fields++)
      ostr_ << misc::iendl;
    const std::string ref = chunk ? "nodename" : name;
    node_html_begin_inner(ostr_, true);
    long int size = std::distance(list.begin(), list.end());
    ostr_ << "<td port='" << ref << "' colspan='" << (size ? size : 1) << "'>"
          << name << "</td>";
    if (size > 1)
      {
        ostr_ << misc::decendl << "</tr>" << misc::iendl << "<tr>"
              << misc::incindent;
        for (long int n = 0; n < size; n++)
          ostr_ << misc::iendl << "<td port='" << ref << n << "'>" << n
                << "</td>";
      }
    node_html_end_inner(ostr_);
  }
  inline void DumperDot::node_html_footer() const
  {
    if (!inner_fields)
      ostr_ << "<td></td>";
    node_html_end_inner(ostr_);
    ostr_ << misc::decendl << "</tr>" << misc::decendl << "</table>"
          << misc::decendl << ">]" << misc::iendl;
  }
} // namespace ast