Migrate Declaration to variant (#644)

Note this makes some structural choices that I'm not sure how popular they'll be... Most notably, I could remove `DeclarationKind` because I'm using `std::visit` and `Declaration::Visitor` in reality. I could go to `switch(tag())` instead, but I'm wondering if this approach will be well received for the separation of ownership. Alternatively, I could move `Declaration` around so that it actually implements the things like `TopLevel` -- I did feel weird with the old structure of typecheck.cpp implementing members of declaration.h, though.
This commit is contained in:
Jon Meow
2021-07-15 16:22:56 -07:00
committed by GitHub
parent ada2be2d16
commit acb33932cb
9 changed files with 305 additions and 287 deletions
+91 -23
View File
@@ -8,33 +8,101 @@
namespace Carbon {
void FunctionDeclaration::Print() const { definition.Print(); }
void StructDeclaration::Print() const {
std::cout << "struct " << *definition.name << " {" << std::endl;
for (auto& member : *definition.members) {
member->Print();
}
std::cout << "}" << std::endl;
auto Declaration::MakeFunctionDeclaration(FunctionDefinition definition)
-> const Declaration {
Declaration d;
d.value = FunctionDeclaration({.definition = definition});
return d;
}
void ChoiceDeclaration::Print() const {
std::cout << "choice " << name << " {" << std::endl;
for (const auto& [name, signature] : alternatives) {
std::cout << "alt " << name << " ";
PrintExp(signature);
std::cout << ";" << std::endl;
}
std::cout << "}" << std::endl;
auto Declaration::MakeStructDeclaration(int line_num, std::string name,
std::list<Member*> members)
-> const Declaration {
Declaration d;
d.value = StructDeclaration(
{.definition = StructDefinition({.line_num = line_num,
.name = std::move(name),
.members = std::move(members)})});
return d;
}
// Print a global variable declaration to standard out.
void VariableDeclaration::Print() const {
std::cout << "var ";
PrintExp(type);
std::cout << " : " << name << " = ";
PrintExp(initializer);
std::cout << std::endl;
auto Declaration::MakeChoiceDeclaration(
int line_num, std::string name,
std::list<std::pair<std::string, const Expression*>> alternatives)
-> const Declaration {
Declaration d;
d.value = ChoiceDeclaration({.line_num = line_num,
.name = std::move(name),
.alternatives = std::move(alternatives)});
return d;
}
auto Declaration::MakeVariableDeclaration(int source_location, std::string name,
const Expression* type,
const Expression* initializer)
-> const Declaration {
Declaration d;
d.value = VariableDeclaration({.source_location = source_location,
.name = std::move(name),
.type = type,
.initializer = initializer});
return d;
}
auto Declaration::GetFunctionDeclaration() const -> const FunctionDeclaration& {
return std::get<FunctionDeclaration>(value);
}
auto Declaration::GetStructDeclaration() const -> const StructDeclaration& {
return std::get<StructDeclaration>(value);
}
auto Declaration::GetChoiceDeclaration() const -> const ChoiceDeclaration& {
return std::get<ChoiceDeclaration>(value);
}
auto Declaration::GetVariableDeclaration() const -> const VariableDeclaration& {
return std::get<VariableDeclaration>(value);
}
void Declaration::Print() const {
switch (tag()) {
case DeclarationKind::FunctionDeclaration:
GetFunctionDeclaration().definition.Print();
break;
case DeclarationKind::StructDeclaration: {
const StructDefinition& struct_def = GetStructDeclaration().definition;
std::cout << "struct " << struct_def.name << " {" << std::endl;
for (Member* m : struct_def.members) {
m->Print();
}
std::cout << "}" << std::endl;
break;
}
case DeclarationKind::ChoiceDeclaration: {
const auto& choice = GetChoiceDeclaration();
std::cout << "choice " << choice.name << " {" << std::endl;
for (const auto& [name, signature] : choice.alternatives) {
std::cout << "alt " << name << " ";
PrintExp(signature);
std::cout << ";" << std::endl;
}
std::cout << "}" << std::endl;
break;
}
case DeclarationKind::VariableDeclaration: {
const auto& var = GetVariableDeclaration();
std::cout << "var ";
PrintExp(var.type);
std::cout << " : " << var.name << " = ";
PrintExp(var.initializer);
std::cout << std::endl;
break;
}
}
}
} // namespace Carbon