Define a base class for all AST nodes. (#947)

Also implement code-generation to manage the resulting boilerplate.
This commit is contained in:
Geoff Romer
2021-11-16 11:54:47 -08:00
committed by GitHub
parent d854fb93cb
commit 7a5b8434c8
24 changed files with 965 additions and 543 deletions
+7 -6
View File
@@ -10,13 +10,15 @@ namespace Carbon {
using llvm::cast;
Declaration::~Declaration() = default;
void Declaration::Print(llvm::raw_ostream& out) const {
switch (kind()) {
case Kind::FunctionDeclaration:
case DeclarationKind::FunctionDeclaration:
cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
break;
case Kind::ClassDeclaration: {
case DeclarationKind::ClassDeclaration: {
const auto& class_decl = cast<ClassDeclaration>(*this);
out << "class " << class_decl.name() << " {\n";
for (Nonnull<Member*> m : class_decl.members()) {
@@ -26,18 +28,17 @@ void Declaration::Print(llvm::raw_ostream& out) const {
break;
}
case Kind::ChoiceDeclaration: {
case DeclarationKind::ChoiceDeclaration: {
const auto& choice = cast<ChoiceDeclaration>(*this);
out << "choice " << choice.name() << " {\n";
for (Nonnull<const ChoiceDeclaration::Alternative*> alt :
choice.alternatives()) {
for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
out << "alt " << alt->name() << " " << alt->signature() << ";\n";
}
out << "}\n";
break;
}
case Kind::VariableDeclaration: {
case DeclarationKind::VariableDeclaration: {
const auto& var = cast<VariableDeclaration>(*this);
out << "var " << var.binding() << " = " << var.initializer() << "\n";
break;