17template<
class T> std::string
escape(
const T& val) {
18 std::ostringstream oss;
28 Dot(std::ostream& ostream,
bool types,
const Def* root =
nullptr)
34 (tab_++).
println(os_,
"digraph {{");
35 tab_.println(os_,
"ordering=out;");
36 tab_.println(os_,
"splines=false;");
37 tab_.println(os_,
"node [shape=box,style=filled];");
40 void epilogue() { (tab_--).
println(os_,
"}}"); }
42 void run(
const Def* root, uint32_t max) {
48 void recurse(
const Def* def, uint32_t max) {
49 if (max == 0 || !done_.emplace(def).second)
return;
50 tab_.print(os_,
"_{}[", def->gid());
53 os_ <<
"style=\"filled,diagonals,bold\"";
55 os_ <<
"style=\"filled,diagonals\"";
56 else if (def == root_)
57 os_ <<
"style=\"filled,bold\"";
60 if (def->free_vars().empty()) os_ <<
"rank=min,";
61 tooltip(def) <<
"];\n";
63 if (!def->is_set())
return;
65 for (
size_t i = 0, e = def->num_ops(); i != e; ++i) {
68 tab_.print(os_,
"_{} -> _{}[taillabel=\"{}\",", def->gid(),
op->
gid(), i);
69 if (
op->isa<Lit>() ||
op->isa<
Axiom>() || def->isa<
Var>() || def->isa<Nat>() || def->isa<Idx>())
70 print(os_,
"fontcolor=\"#00000000\",color=\"#00000000\",constraint=false];\n");
75 if (
auto t = def->type().def(); t && types_) {
77 tab_.println(os_,
"_{} -> _{}[color=\"#00000000\",constraint=false,style=dashed];", def->gid(),
t->gid());
81 std::ostream& label(
const Def* def) {
82 print(os_,
"label=<{}<br/>", def->unique_name());
83 if (
auto lit = def->isa<Lit>())
86 os_ << def->node_name();
90 std::ostream& color(
const Def* def) {
94 std::ostream& tooltip(
const Def* def) {
95 static constexpr auto NL =
" ";
96 auto loc =
escape(def->loc());
97 auto type =
escape(def->type().def());
99 print(os_,
"tooltip=\"");
100 print(os_,
"<b>expr:</b> {}{}", def, NL);
101 print(os_,
"<b>type:</b> {}{}", type, NL);
102 print(os_,
"<b>name:</b> {}{}", def->sym(), NL);
103 print(os_,
"<b>gid:</b> {}{}", def->gid(), NL);
104 print(os_,
"<b>flags:</b> 0x{x}{}", def->flags(), NL);
105 print(os_,
"<b>free_vars:</b> {{{, }}}{}", def->free_vars(), NL);
106 print(os_,
"<b>local_vars:</b> {{{, }}}{}", def->local_vars(), NL);
107 print(os_,
"<b>local_muts:</b> {{{, }}}{}", def->local_muts(), NL);
108 if (
auto mut = def->isa_mut())
print(os_,
"<b>fv_consumers:</b> {{{, }}}{}", mut->fv_consumers(), NL);
109 print(os_,
"<b>loc:</b> {}", loc);
110 return print(os_,
"\"");
123void Def::dot(std::ostream& ostream, uint32_t max,
bool types)
const { Dot(ostream, types,
this).run(
this, max); }
125void Def::dot(
const char* file, uint32_t max,
bool types)
const {
127 dot(std::cout, max, types);
129 auto of = std::ofstream(file);
134void World::dot(
const char* file,
bool annexes,
bool types)
const {
138 auto of = std::ofstream(file);
143void World::dot(std::ostream& os,
bool anx,
bool types)
const {
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
const Def * def() const
Retrieve wrapped Def without Infer::refering.
const auto & annexes() const
const auto & externals() const
Def * external(Sym name)
Lookup by name.
void dot(std::ostream &os, bool annexes=false, bool types=false) const
const Def * annex()
Get Axiom from a plugin.
Ref op(trait o, Ref type)
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
std::ostream & print(std::ostream &os, const char *s)
Base case.
void find_and_replace(std::string &str, std::string_view what, std::string_view repl)
Replaces all occurrences of what with repl.
GIDSet< const Def * > DefSet
std::ostream & println(std::ostream &os, const char *fmt, Args &&... args)
As above but end with std::endl.