AST.cpp
8.03 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//===- AST.cpp - Helper for printing out the Toy AST ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the AST dump for the Toy language.
//
//===----------------------------------------------------------------------===//
#include "toy/AST.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/raw_ostream.h"
using namespace toy;
namespace {
// RAII helper to manage increasing/decreasing the indentation as we traverse
// the AST
struct Indent {
Indent(int &level) : level(level) { ++level; }
~Indent() { --level; }
int &level;
};
/// Helper class that implement the AST tree traversal and print the nodes along
/// the way. The only data member is the current indentation level.
class ASTDumper {
public:
void dump(ModuleAST *node);
private:
void dump(const VarType &type);
void dump(VarDeclExprAST *varDecl);
void dump(ExprAST *expr);
void dump(ExprASTList *exprList);
void dump(NumberExprAST *num);
void dump(LiteralExprAST *node);
void dump(StructLiteralExprAST *node);
void dump(VariableExprAST *node);
void dump(ReturnExprAST *node);
void dump(BinaryExprAST *node);
void dump(CallExprAST *node);
void dump(PrintExprAST *node);
void dump(PrototypeAST *node);
void dump(FunctionAST *node);
void dump(StructAST *node);
// Actually print spaces matching the current indentation level
void indent() {
for (int i = 0; i < curIndent; i++)
llvm::errs() << " ";
}
int curIndent = 0;
};
} // namespace
/// Return a formatted string for the location of any node
template <typename T> static std::string loc(T *node) {
const auto &loc = node->loc();
return (llvm::Twine("@") + *loc.file + ":" + llvm::Twine(loc.line) + ":" +
llvm::Twine(loc.col))
.str();
}
// Helper Macro to bump the indentation level and print the leading spaces for
// the current indentations
#define INDENT() \
Indent level_(curIndent); \
indent();
/// Dispatch to a generic expressions to the appropriate subclass using RTTI
void ASTDumper::dump(ExprAST *expr) {
llvm::TypeSwitch<ExprAST *>(expr)
.Case<BinaryExprAST, CallExprAST, LiteralExprAST, NumberExprAST,
PrintExprAST, ReturnExprAST, StructLiteralExprAST, VarDeclExprAST,
VariableExprAST>([&](auto *node) { this->dump(node); })
.Default([&](ExprAST *) {
// No match, fallback to a generic message
INDENT();
llvm::errs() << "<unknown Expr, kind " << expr->getKind() << ">\n";
});
}
/// A variable declaration is printing the variable name, the type, and then
/// recurse in the initializer value.
void ASTDumper::dump(VarDeclExprAST *varDecl) {
INDENT();
llvm::errs() << "VarDecl " << varDecl->getName();
dump(varDecl->getType());
llvm::errs() << " " << loc(varDecl) << "\n";
if (auto *initVal = varDecl->getInitVal())
dump(initVal);
}
/// A "block", or a list of expression
void ASTDumper::dump(ExprASTList *exprList) {
INDENT();
llvm::errs() << "Block {\n";
for (auto &expr : *exprList)
dump(expr.get());
indent();
llvm::errs() << "} // Block\n";
}
/// A literal number, just print the value.
void ASTDumper::dump(NumberExprAST *num) {
INDENT();
llvm::errs() << num->getValue() << " " << loc(num) << "\n";
}
/// Helper to print recursively a literal. This handles nested array like:
/// [ [ 1, 2 ], [ 3, 4 ] ]
/// We print out such array with the dimensions spelled out at every level:
/// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ]
void printLitHelper(ExprAST *litOrNum) {
// Inside a literal expression we can have either a number or another literal
if (auto num = llvm::dyn_cast<NumberExprAST>(litOrNum)) {
llvm::errs() << num->getValue();
return;
}
auto *literal = llvm::cast<LiteralExprAST>(litOrNum);
// Print the dimension for this literal first
llvm::errs() << "<";
llvm::interleaveComma(literal->getDims(), llvm::errs());
llvm::errs() << ">";
// Now print the content, recursing on every element of the list
llvm::errs() << "[ ";
llvm::interleaveComma(literal->getValues(), llvm::errs(),
[&](auto &elt) { printLitHelper(elt.get()); });
llvm::errs() << "]";
}
/// Print a literal, see the recursive helper above for the implementation.
void ASTDumper::dump(LiteralExprAST *node) {
INDENT();
llvm::errs() << "Literal: ";
printLitHelper(node);
llvm::errs() << " " << loc(node) << "\n";
}
/// Print a struct literal.
void ASTDumper::dump(StructLiteralExprAST *node) {
INDENT();
llvm::errs() << "Struct Literal: ";
for (auto &value : node->getValues())
dump(value.get());
indent();
llvm::errs() << " " << loc(node) << "\n";
}
/// Print a variable reference (just a name).
void ASTDumper::dump(VariableExprAST *node) {
INDENT();
llvm::errs() << "var: " << node->getName() << " " << loc(node) << "\n";
}
/// Return statement print the return and its (optional) argument.
void ASTDumper::dump(ReturnExprAST *node) {
INDENT();
llvm::errs() << "Return\n";
if (node->getExpr().hasValue())
return dump(*node->getExpr());
{
INDENT();
llvm::errs() << "(void)\n";
}
}
/// Print a binary operation, first the operator, then recurse into LHS and RHS.
void ASTDumper::dump(BinaryExprAST *node) {
INDENT();
llvm::errs() << "BinOp: " << node->getOp() << " " << loc(node) << "\n";
dump(node->getLHS());
dump(node->getRHS());
}
/// Print a call expression, first the callee name and the list of args by
/// recursing into each individual argument.
void ASTDumper::dump(CallExprAST *node) {
INDENT();
llvm::errs() << "Call '" << node->getCallee() << "' [ " << loc(node) << "\n";
for (auto &arg : node->getArgs())
dump(arg.get());
indent();
llvm::errs() << "]\n";
}
/// Print a builtin print call, first the builtin name and then the argument.
void ASTDumper::dump(PrintExprAST *node) {
INDENT();
llvm::errs() << "Print [ " << loc(node) << "\n";
dump(node->getArg());
indent();
llvm::errs() << "]\n";
}
/// Print type: only the shape is printed in between '<' and '>'
void ASTDumper::dump(const VarType &type) {
llvm::errs() << "<";
if (!type.name.empty())
llvm::errs() << type.name;
else
llvm::interleaveComma(type.shape, llvm::errs());
llvm::errs() << ">";
}
/// Print a function prototype, first the function name, and then the list of
/// parameters names.
void ASTDumper::dump(PrototypeAST *node) {
INDENT();
llvm::errs() << "Proto '" << node->getName() << "' " << loc(node) << "\n";
indent();
llvm::errs() << "Params: [";
llvm::interleaveComma(node->getArgs(), llvm::errs(),
[](auto &arg) { llvm::errs() << arg->getName(); });
llvm::errs() << "]\n";
}
/// Print a function, first the prototype and then the body.
void ASTDumper::dump(FunctionAST *node) {
INDENT();
llvm::errs() << "Function \n";
dump(node->getProto());
dump(node->getBody());
}
/// Print a struct.
void ASTDumper::dump(StructAST *node) {
INDENT();
llvm::errs() << "Struct: " << node->getName() << " " << loc(node) << "\n";
{
INDENT();
llvm::errs() << "Variables: [\n";
for (auto &variable : node->getVariables())
dump(variable.get());
indent();
llvm::errs() << "]\n";
}
}
/// Print a module, actually loop over the functions and print them in sequence.
void ASTDumper::dump(ModuleAST *node) {
INDENT();
llvm::errs() << "Module:\n";
for (auto &record : *node) {
if (FunctionAST *function = llvm::dyn_cast<FunctionAST>(record.get()))
dump(function);
else if (StructAST *str = llvm::dyn_cast<StructAST>(record.get()))
dump(str);
else
llvm::errs() << "<unknown Record, kind " << record->getKind() << ">\n";
}
}
namespace toy {
// Public API
void dump(ModuleAST &module) { ASTDumper().dump(&module); }
} // namespace toy