mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Define a base class for all AST nodes. (#947)
Also implement code-generation to manage the resulting boilerplate.
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user