138b71d6bSMehdi Amini //===- AST.cpp - Helper for printing out the Toy AST ----------------------===//
238b71d6bSMehdi Amini //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
638b71d6bSMehdi Amini //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
838b71d6bSMehdi Amini //
938b71d6bSMehdi Amini // This file implements the AST dump for the Toy language.
1038b71d6bSMehdi Amini //
1138b71d6bSMehdi Amini //===----------------------------------------------------------------------===//
1238b71d6bSMehdi Amini
1338b71d6bSMehdi Amini #include "toy/AST.h"
1438b71d6bSMehdi Amini
15*904c5b48SMehdi Amini #include "llvm/ADT/STLExtras.h"
1638b71d6bSMehdi Amini #include "llvm/ADT/Twine.h"
17ebf190fcSRiver Riddle #include "llvm/ADT/TypeSwitch.h"
18*904c5b48SMehdi Amini #include "llvm/Support/Casting.h"
1938b71d6bSMehdi Amini #include "llvm/Support/raw_ostream.h"
20*904c5b48SMehdi Amini #include <string>
2138b71d6bSMehdi Amini
2238b71d6bSMehdi Amini using namespace toy;
2338b71d6bSMehdi Amini
2438b71d6bSMehdi Amini namespace {
2538b71d6bSMehdi Amini
2638b71d6bSMehdi Amini // RAII helper to manage increasing/decreasing the indentation as we traverse
2738b71d6bSMehdi Amini // the AST
2838b71d6bSMehdi Amini struct Indent {
Indent__anonf230983e0111::Indent2938b71d6bSMehdi Amini Indent(int &level) : level(level) { ++level; }
~Indent__anonf230983e0111::Indent3038b71d6bSMehdi Amini ~Indent() { --level; }
3138b71d6bSMehdi Amini int &level;
3238b71d6bSMehdi Amini };
3338b71d6bSMehdi Amini
3438b71d6bSMehdi Amini /// Helper class that implement the AST tree traversal and print the nodes along
3538b71d6bSMehdi Amini /// the way. The only data member is the current indentation level.
3638b71d6bSMehdi Amini class ASTDumper {
3738b71d6bSMehdi Amini public:
3822cfff70SRiver Riddle void dump(ModuleAST *node);
3938b71d6bSMehdi Amini
4038b71d6bSMehdi Amini private:
4122cfff70SRiver Riddle void dump(const VarType &type);
4238b71d6bSMehdi Amini void dump(VarDeclExprAST *varDecl);
4338b71d6bSMehdi Amini void dump(ExprAST *expr);
4438b71d6bSMehdi Amini void dump(ExprASTList *exprList);
4538b71d6bSMehdi Amini void dump(NumberExprAST *num);
4622cfff70SRiver Riddle void dump(LiteralExprAST *node);
4722cfff70SRiver Riddle void dump(VariableExprAST *node);
4822cfff70SRiver Riddle void dump(ReturnExprAST *node);
4922cfff70SRiver Riddle void dump(BinaryExprAST *node);
5022cfff70SRiver Riddle void dump(CallExprAST *node);
5122cfff70SRiver Riddle void dump(PrintExprAST *node);
5222cfff70SRiver Riddle void dump(PrototypeAST *node);
5322cfff70SRiver Riddle void dump(FunctionAST *node);
5438b71d6bSMehdi Amini
5538b71d6bSMehdi Amini // Actually print spaces matching the current indentation level
indent()5638b71d6bSMehdi Amini void indent() {
5738b71d6bSMehdi Amini for (int i = 0; i < curIndent; i++)
5838b71d6bSMehdi Amini llvm::errs() << " ";
5938b71d6bSMehdi Amini }
6038b71d6bSMehdi Amini int curIndent = 0;
6138b71d6bSMehdi Amini };
6238b71d6bSMehdi Amini
6338b71d6bSMehdi Amini } // namespace
6438b71d6bSMehdi Amini
6538b71d6bSMehdi Amini /// Return a formatted string for the location of any node
66b7f93c28SJeff Niu template <typename T>
loc(T * node)67b7f93c28SJeff Niu static std::string loc(T *node) {
6822cfff70SRiver Riddle const auto &loc = node->loc();
6938b71d6bSMehdi Amini return (llvm::Twine("@") + *loc.file + ":" + llvm::Twine(loc.line) + ":" +
7038b71d6bSMehdi Amini llvm::Twine(loc.col))
7138b71d6bSMehdi Amini .str();
7238b71d6bSMehdi Amini }
7338b71d6bSMehdi Amini
7438b71d6bSMehdi Amini // Helper Macro to bump the indentation level and print the leading spaces for
7538b71d6bSMehdi Amini // the current indentations
7638b71d6bSMehdi Amini #define INDENT() \
7738b71d6bSMehdi Amini Indent level_(curIndent); \
7838b71d6bSMehdi Amini indent();
7938b71d6bSMehdi Amini
8038b71d6bSMehdi Amini /// Dispatch to a generic expressions to the appropriate subclass using RTTI
dump(ExprAST * expr)8138b71d6bSMehdi Amini void ASTDumper::dump(ExprAST *expr) {
82ebf190fcSRiver Riddle llvm::TypeSwitch<ExprAST *>(expr)
8374278dd0SRiver Riddle .Case<BinaryExprAST, CallExprAST, LiteralExprAST, NumberExprAST,
8474278dd0SRiver Riddle PrintExprAST, ReturnExprAST, VarDeclExprAST, VariableExprAST>(
855a0d4803SRiver Riddle [&](auto *node) { this->dump(node); })
8674278dd0SRiver Riddle .Default([&](ExprAST *) {
8738b71d6bSMehdi Amini // No match, fallback to a generic message
8838b71d6bSMehdi Amini INDENT();
8938b71d6bSMehdi Amini llvm::errs() << "<unknown Expr, kind " << expr->getKind() << ">\n";
9074278dd0SRiver Riddle });
9138b71d6bSMehdi Amini }
9238b71d6bSMehdi Amini
9338b71d6bSMehdi Amini /// A variable declaration is printing the variable name, the type, and then
9438b71d6bSMehdi Amini /// recurse in the initializer value.
dump(VarDeclExprAST * varDecl)9538b71d6bSMehdi Amini void ASTDumper::dump(VarDeclExprAST *varDecl) {
9638b71d6bSMehdi Amini INDENT();
9738b71d6bSMehdi Amini llvm::errs() << "VarDecl " << varDecl->getName();
9838b71d6bSMehdi Amini dump(varDecl->getType());
9938b71d6bSMehdi Amini llvm::errs() << " " << loc(varDecl) << "\n";
10038b71d6bSMehdi Amini dump(varDecl->getInitVal());
10138b71d6bSMehdi Amini }
10238b71d6bSMehdi Amini
10338b71d6bSMehdi Amini /// A "block", or a list of expression
dump(ExprASTList * exprList)10438b71d6bSMehdi Amini void ASTDumper::dump(ExprASTList *exprList) {
10538b71d6bSMehdi Amini INDENT();
10638b71d6bSMehdi Amini llvm::errs() << "Block {\n";
10738b71d6bSMehdi Amini for (auto &expr : *exprList)
10838b71d6bSMehdi Amini dump(expr.get());
10938b71d6bSMehdi Amini indent();
11038b71d6bSMehdi Amini llvm::errs() << "} // Block\n";
11138b71d6bSMehdi Amini }
11238b71d6bSMehdi Amini
11338b71d6bSMehdi Amini /// A literal number, just print the value.
dump(NumberExprAST * num)11438b71d6bSMehdi Amini void ASTDumper::dump(NumberExprAST *num) {
11538b71d6bSMehdi Amini INDENT();
11638b71d6bSMehdi Amini llvm::errs() << num->getValue() << " " << loc(num) << "\n";
11738b71d6bSMehdi Amini }
11838b71d6bSMehdi Amini
119f28c5acaSKazuaki Ishizaki /// Helper to print recursively a literal. This handles nested array like:
12038b71d6bSMehdi Amini /// [ [ 1, 2 ], [ 3, 4 ] ]
12138b71d6bSMehdi Amini /// We print out such array with the dimensions spelled out at every level:
12238b71d6bSMehdi Amini /// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ]
printLitHelper(ExprAST * litOrNum)12322cfff70SRiver Riddle void printLitHelper(ExprAST *litOrNum) {
12438b71d6bSMehdi Amini // Inside a literal expression we can have either a number or another literal
12502b6fb21SMehdi Amini if (auto *num = llvm::dyn_cast<NumberExprAST>(litOrNum)) {
12638b71d6bSMehdi Amini llvm::errs() << num->getValue();
12738b71d6bSMehdi Amini return;
12838b71d6bSMehdi Amini }
12922cfff70SRiver Riddle auto *literal = llvm::cast<LiteralExprAST>(litOrNum);
13038b71d6bSMehdi Amini
13138b71d6bSMehdi Amini // Print the dimension for this literal first
13238b71d6bSMehdi Amini llvm::errs() << "<";
1332f21a579SRiver Riddle llvm::interleaveComma(literal->getDims(), llvm::errs());
13438b71d6bSMehdi Amini llvm::errs() << ">";
13538b71d6bSMehdi Amini
13638b71d6bSMehdi Amini // Now print the content, recursing on every element of the list
13738b71d6bSMehdi Amini llvm::errs() << "[ ";
1382f21a579SRiver Riddle llvm::interleaveComma(literal->getValues(), llvm::errs(),
13922cfff70SRiver Riddle [&](auto &elt) { printLitHelper(elt.get()); });
14038b71d6bSMehdi Amini llvm::errs() << "]";
14138b71d6bSMehdi Amini }
14238b71d6bSMehdi Amini
14338b71d6bSMehdi Amini /// Print a literal, see the recursive helper above for the implementation.
dump(LiteralExprAST * node)14422cfff70SRiver Riddle void ASTDumper::dump(LiteralExprAST *node) {
14538b71d6bSMehdi Amini INDENT();
14638b71d6bSMehdi Amini llvm::errs() << "Literal: ";
14722cfff70SRiver Riddle printLitHelper(node);
14822cfff70SRiver Riddle llvm::errs() << " " << loc(node) << "\n";
14938b71d6bSMehdi Amini }
15038b71d6bSMehdi Amini
15138b71d6bSMehdi Amini /// Print a variable reference (just a name).
dump(VariableExprAST * node)15222cfff70SRiver Riddle void ASTDumper::dump(VariableExprAST *node) {
15338b71d6bSMehdi Amini INDENT();
15422cfff70SRiver Riddle llvm::errs() << "var: " << node->getName() << " " << loc(node) << "\n";
15538b71d6bSMehdi Amini }
15638b71d6bSMehdi Amini
15738b71d6bSMehdi Amini /// Return statement print the return and its (optional) argument.
dump(ReturnExprAST * node)15822cfff70SRiver Riddle void ASTDumper::dump(ReturnExprAST *node) {
15938b71d6bSMehdi Amini INDENT();
16038b71d6bSMehdi Amini llvm::errs() << "Return\n";
1617430894aSFangrui Song if (node->getExpr().has_value())
16222cfff70SRiver Riddle return dump(*node->getExpr());
16338b71d6bSMehdi Amini {
16438b71d6bSMehdi Amini INDENT();
16538b71d6bSMehdi Amini llvm::errs() << "(void)\n";
16638b71d6bSMehdi Amini }
16738b71d6bSMehdi Amini }
16838b71d6bSMehdi Amini
16938b71d6bSMehdi Amini /// Print a binary operation, first the operator, then recurse into LHS and RHS.
dump(BinaryExprAST * node)17022cfff70SRiver Riddle void ASTDumper::dump(BinaryExprAST *node) {
17138b71d6bSMehdi Amini INDENT();
17222cfff70SRiver Riddle llvm::errs() << "BinOp: " << node->getOp() << " " << loc(node) << "\n";
17322cfff70SRiver Riddle dump(node->getLHS());
17422cfff70SRiver Riddle dump(node->getRHS());
17538b71d6bSMehdi Amini }
17638b71d6bSMehdi Amini
17738b71d6bSMehdi Amini /// Print a call expression, first the callee name and the list of args by
17838b71d6bSMehdi Amini /// recursing into each individual argument.
dump(CallExprAST * node)17922cfff70SRiver Riddle void ASTDumper::dump(CallExprAST *node) {
18038b71d6bSMehdi Amini INDENT();
18122cfff70SRiver Riddle llvm::errs() << "Call '" << node->getCallee() << "' [ " << loc(node) << "\n";
18222cfff70SRiver Riddle for (auto &arg : node->getArgs())
18338b71d6bSMehdi Amini dump(arg.get());
18438b71d6bSMehdi Amini indent();
18538b71d6bSMehdi Amini llvm::errs() << "]\n";
18638b71d6bSMehdi Amini }
18738b71d6bSMehdi Amini
18838b71d6bSMehdi Amini /// Print a builtin print call, first the builtin name and then the argument.
dump(PrintExprAST * node)18922cfff70SRiver Riddle void ASTDumper::dump(PrintExprAST *node) {
19038b71d6bSMehdi Amini INDENT();
19122cfff70SRiver Riddle llvm::errs() << "Print [ " << loc(node) << "\n";
19222cfff70SRiver Riddle dump(node->getArg());
19338b71d6bSMehdi Amini indent();
19438b71d6bSMehdi Amini llvm::errs() << "]\n";
19538b71d6bSMehdi Amini }
19638b71d6bSMehdi Amini
19738b71d6bSMehdi Amini /// Print type: only the shape is printed in between '<' and '>'
dump(const VarType & type)19822cfff70SRiver Riddle void ASTDumper::dump(const VarType &type) {
19938b71d6bSMehdi Amini llvm::errs() << "<";
2002f21a579SRiver Riddle llvm::interleaveComma(type.shape, llvm::errs());
20138b71d6bSMehdi Amini llvm::errs() << ">";
20238b71d6bSMehdi Amini }
20338b71d6bSMehdi Amini
20438b71d6bSMehdi Amini /// Print a function prototype, first the function name, and then the list of
20538b71d6bSMehdi Amini /// parameters names.
dump(PrototypeAST * node)20622cfff70SRiver Riddle void ASTDumper::dump(PrototypeAST *node) {
20738b71d6bSMehdi Amini INDENT();
2085633813bSRahul Joshi llvm::errs() << "Proto '" << node->getName() << "' " << loc(node) << "\n";
20938b71d6bSMehdi Amini indent();
21038b71d6bSMehdi Amini llvm::errs() << "Params: [";
2112f21a579SRiver Riddle llvm::interleaveComma(node->getArgs(), llvm::errs(),
21222cfff70SRiver Riddle [](auto &arg) { llvm::errs() << arg->getName(); });
21338b71d6bSMehdi Amini llvm::errs() << "]\n";
21438b71d6bSMehdi Amini }
21538b71d6bSMehdi Amini
21638b71d6bSMehdi Amini /// Print a function, first the prototype and then the body.
dump(FunctionAST * node)21722cfff70SRiver Riddle void ASTDumper::dump(FunctionAST *node) {
21838b71d6bSMehdi Amini INDENT();
21938b71d6bSMehdi Amini llvm::errs() << "Function \n";
22022cfff70SRiver Riddle dump(node->getProto());
22122cfff70SRiver Riddle dump(node->getBody());
22238b71d6bSMehdi Amini }
22338b71d6bSMehdi Amini
22438b71d6bSMehdi Amini /// Print a module, actually loop over the functions and print them in sequence.
dump(ModuleAST * node)22522cfff70SRiver Riddle void ASTDumper::dump(ModuleAST *node) {
22638b71d6bSMehdi Amini INDENT();
22738b71d6bSMehdi Amini llvm::errs() << "Module:\n";
22822cfff70SRiver Riddle for (auto &f : *node)
22922cfff70SRiver Riddle dump(&f);
23038b71d6bSMehdi Amini }
23138b71d6bSMehdi Amini
23238b71d6bSMehdi Amini namespace toy {
23338b71d6bSMehdi Amini
23438b71d6bSMehdi Amini // Public API
dump(ModuleAST & module)23538b71d6bSMehdi Amini void dump(ModuleAST &module) { ASTDumper().dump(&module); }
23638b71d6bSMehdi Amini
23738b71d6bSMehdi Amini } // namespace toy
238