mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:54:47 +01:00
Change Alternative to a class (#853)
Revives BisonWrap because this seems a reasonable use of it (avoiding the need to have an std::optional or pointer for Alternative, both of which I thought could be unclear about the intent).
This commit is contained in:
@@ -30,8 +30,8 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
case Kind::ChoiceDeclaration: {
|
||||
const auto& choice = cast<ChoiceDeclaration>(*this);
|
||||
out << "choice " << choice.Name() << " {\n";
|
||||
for (const auto& [name, signature] : choice.Alternatives()) {
|
||||
out << "alt " << name << " " << *signature << ";\n";
|
||||
for (const auto& alt : choice.Alternatives()) {
|
||||
out << "alt " << alt.name() << " " << alt.signature() << ";\n";
|
||||
}
|
||||
out << "}\n";
|
||||
break;
|
||||
|
||||
@@ -95,10 +95,21 @@ class ClassDeclaration : public Declaration {
|
||||
|
||||
class ChoiceDeclaration : public Declaration {
|
||||
public:
|
||||
ChoiceDeclaration(
|
||||
SourceLocation loc, std::string name,
|
||||
std::vector<std::pair<std::string, Nonnull<const Expression*>>>
|
||||
alternatives)
|
||||
class Alternative {
|
||||
public:
|
||||
Alternative(std::string name, Nonnull<const Expression*> signature)
|
||||
: name_(name), signature_(signature) {}
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto signature() const -> const Expression& { return *signature_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
Nonnull<const Expression*> signature_;
|
||||
};
|
||||
|
||||
ChoiceDeclaration(SourceLocation loc, std::string name,
|
||||
std::vector<Alternative> alternatives)
|
||||
: Declaration(Kind::ChoiceDeclaration, loc),
|
||||
name(std::move(name)),
|
||||
alternatives(std::move(alternatives)) {}
|
||||
@@ -108,14 +119,13 @@ class ChoiceDeclaration : public Declaration {
|
||||
}
|
||||
|
||||
auto Name() const -> const std::string& { return name; }
|
||||
auto Alternatives() const -> const
|
||||
std::vector<std::pair<std::string, Nonnull<const Expression*>>>& {
|
||||
auto Alternatives() const -> const std::vector<Alternative>& {
|
||||
return alternatives;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<std::pair<std::string, Nonnull<const Expression*>>> alternatives;
|
||||
std::vector<Alternative> alternatives;
|
||||
};
|
||||
|
||||
// Global variable definition implements the Declaration concept.
|
||||
|
||||
Reference in New Issue
Block a user