mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:21:06 +01:00
Migrate Declaration to newer property style, class-ify ClassDefinition (#859)
This commit is contained in:
@@ -13,10 +13,22 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct ClassDefinition {
|
||||
SourceLocation loc;
|
||||
std::string name;
|
||||
std::vector<Nonnull<Member*>> members;
|
||||
class ClassDefinition {
|
||||
public:
|
||||
ClassDefinition(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<Member*>> members)
|
||||
: source_loc_(source_loc),
|
||||
name_(std::move(name)),
|
||||
members_(std::move(members)) {}
|
||||
|
||||
auto source_loc() const -> SourceLocation { return source_loc_; }
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto members() const -> llvm::ArrayRef<Nonnull<Member*>> { return members_; }
|
||||
|
||||
private:
|
||||
SourceLocation source_loc_;
|
||||
std::string name_;
|
||||
std::vector<Nonnull<Member*>> members_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace Carbon {
|
||||
using llvm::cast;
|
||||
|
||||
void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
switch (Tag()) {
|
||||
switch (kind()) {
|
||||
case Kind::FunctionDeclaration:
|
||||
out << cast<FunctionDeclaration>(*this).Definition();
|
||||
out << cast<FunctionDeclaration>(*this).definition();
|
||||
break;
|
||||
|
||||
case Kind::ClassDeclaration: {
|
||||
const ClassDefinition& class_def =
|
||||
cast<ClassDeclaration>(*this).Definition();
|
||||
out << "class " << class_def.name << " {\n";
|
||||
for (Nonnull<Member*> m : class_def.members) {
|
||||
cast<ClassDeclaration>(*this).definition();
|
||||
out << "class " << class_def.name() << " {\n";
|
||||
for (Nonnull<Member*> m : class_def.members()) {
|
||||
out << *m;
|
||||
}
|
||||
out << "}\n";
|
||||
@@ -29,8 +29,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& alt : choice.Alternatives()) {
|
||||
out << "choice " << choice.name() << " {\n";
|
||||
for (const auto& alt : choice.alternatives()) {
|
||||
out << "alt " << alt.name() << " " << alt.signature() << ";\n";
|
||||
}
|
||||
out << "}\n";
|
||||
@@ -39,7 +39,7 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
|
||||
case Kind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(*this);
|
||||
out << "var " << *var.Binding() << " = " << *var.Initializer() << "\n";
|
||||
out << "var " << var.binding() << " = " << var.initializer() << "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,60 +40,59 @@ class Declaration {
|
||||
Declaration(const Member&) = delete;
|
||||
Declaration& operator=(const Member&) = delete;
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
auto kind() const -> Kind { return kind_; }
|
||||
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
auto source_loc() const -> SourceLocation { return source_loc_; }
|
||||
|
||||
protected:
|
||||
// Constructs a Declaration representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Declaration(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
Declaration(Kind kind, SourceLocation source_loc)
|
||||
: kind_(kind), source_loc_(source_loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
SourceLocation loc;
|
||||
const Kind kind_;
|
||||
SourceLocation source_loc_;
|
||||
};
|
||||
|
||||
class FunctionDeclaration : public Declaration {
|
||||
public:
|
||||
FunctionDeclaration(Nonnull<FunctionDefinition*> definition)
|
||||
: Declaration(Kind::FunctionDeclaration, definition->source_loc()),
|
||||
definition(definition) {}
|
||||
definition_(definition) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
return decl->Tag() == Kind::FunctionDeclaration;
|
||||
return decl->kind() == Kind::FunctionDeclaration;
|
||||
}
|
||||
|
||||
auto Definition() const -> const FunctionDefinition& { return *definition; }
|
||||
auto Definition() -> FunctionDefinition& { return *definition; }
|
||||
auto definition() const -> const FunctionDefinition& { return *definition_; }
|
||||
auto definition() -> FunctionDefinition& { return *definition_; }
|
||||
|
||||
private:
|
||||
Nonnull<FunctionDefinition*> definition;
|
||||
Nonnull<FunctionDefinition*> definition_;
|
||||
};
|
||||
|
||||
class ClassDeclaration : public Declaration {
|
||||
public:
|
||||
ClassDeclaration(SourceLocation loc, std::string name,
|
||||
ClassDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Nonnull<Member*>> members)
|
||||
: Declaration(Kind::ClassDeclaration, loc),
|
||||
definition({.loc = loc,
|
||||
.name = std::move(name),
|
||||
.members = std::move(members)}) {}
|
||||
: Declaration(Kind::ClassDeclaration, source_loc),
|
||||
definition_(source_loc, std::move(name), std::move(members)) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
return decl->Tag() == Kind::ClassDeclaration;
|
||||
return decl->kind() == Kind::ClassDeclaration;
|
||||
}
|
||||
|
||||
auto Definition() const -> const ClassDefinition& { return definition; }
|
||||
auto Definition() -> ClassDefinition& { return definition; }
|
||||
auto definition() const -> const ClassDefinition& { return definition_; }
|
||||
auto definition() -> ClassDefinition& { return definition_; }
|
||||
|
||||
private:
|
||||
ClassDefinition definition;
|
||||
ClassDefinition definition_;
|
||||
};
|
||||
|
||||
class ChoiceDeclaration : public Declaration {
|
||||
@@ -111,50 +110,51 @@ class ChoiceDeclaration : public Declaration {
|
||||
Nonnull<Expression*> signature_;
|
||||
};
|
||||
|
||||
ChoiceDeclaration(SourceLocation loc, std::string name,
|
||||
ChoiceDeclaration(SourceLocation source_loc, std::string name,
|
||||
std::vector<Alternative> alternatives)
|
||||
: Declaration(Kind::ChoiceDeclaration, loc),
|
||||
name(std::move(name)),
|
||||
alternatives(std::move(alternatives)) {}
|
||||
: Declaration(Kind::ChoiceDeclaration, source_loc),
|
||||
name_(std::move(name)),
|
||||
alternatives_(std::move(alternatives)) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
return decl->Tag() == Kind::ChoiceDeclaration;
|
||||
return decl->kind() == Kind::ChoiceDeclaration;
|
||||
}
|
||||
|
||||
auto Name() const -> const std::string& { return name; }
|
||||
auto Alternatives() const -> const std::vector<Alternative>& {
|
||||
return alternatives;
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
auto alternatives() const -> llvm::ArrayRef<Alternative> {
|
||||
return alternatives_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Alternative> alternatives;
|
||||
std::string name_;
|
||||
std::vector<Alternative> alternatives_;
|
||||
};
|
||||
|
||||
// Global variable definition implements the Declaration concept.
|
||||
class VariableDeclaration : public Declaration {
|
||||
public:
|
||||
VariableDeclaration(SourceLocation loc, Nonnull<BindingPattern*> binding,
|
||||
VariableDeclaration(SourceLocation source_loc,
|
||||
Nonnull<BindingPattern*> binding,
|
||||
Nonnull<Expression*> initializer)
|
||||
: Declaration(Kind::VariableDeclaration, loc),
|
||||
binding(binding),
|
||||
initializer(initializer) {}
|
||||
: Declaration(Kind::VariableDeclaration, source_loc),
|
||||
binding_(binding),
|
||||
initializer_(initializer) {}
|
||||
|
||||
static auto classof(const Declaration* decl) -> bool {
|
||||
return decl->Tag() == Kind::VariableDeclaration;
|
||||
return decl->kind() == Kind::VariableDeclaration;
|
||||
}
|
||||
|
||||
auto Binding() const -> Nonnull<const BindingPattern*> { return binding; }
|
||||
auto Binding() -> Nonnull<BindingPattern*> { return binding; }
|
||||
auto Initializer() const -> Nonnull<const Expression*> { return initializer; }
|
||||
auto Initializer() -> Nonnull<Expression*> { return initializer; }
|
||||
auto binding() const -> const BindingPattern& { return *binding_; }
|
||||
auto binding() -> BindingPattern& { return *binding_; }
|
||||
auto initializer() const -> const Expression& { return *initializer_; }
|
||||
auto initializer() -> Expression& { return *initializer_; }
|
||||
|
||||
private:
|
||||
// TODO: split this into a non-optional name and a type, initialized by
|
||||
// a constructor that takes a BindingPattern and handles errors like a
|
||||
// missing name.
|
||||
Nonnull<BindingPattern*> binding;
|
||||
Nonnull<Expression*> initializer;
|
||||
Nonnull<BindingPattern*> binding_;
|
||||
Nonnull<Expression*> initializer_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -42,7 +42,7 @@ class Expression {
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
auto Tag() const -> Kind { return kind; }
|
||||
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
@@ -53,10 +53,10 @@ class Expression {
|
||||
// Constructs an Expression representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Expression(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
Expression(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
const Kind kind;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class Member {
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
auto Tag() const -> Kind { return kind; }
|
||||
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
@@ -42,10 +42,10 @@ class Member {
|
||||
// Constructs a Member representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Member(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
Member(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
const Kind kind;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ class Pattern {
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
auto Tag() const -> Kind { return kind; }
|
||||
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
@@ -50,10 +50,10 @@ class Pattern {
|
||||
// Constructs a Pattern representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Pattern(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
Pattern(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
const Kind kind;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ class Statement {
|
||||
|
||||
// Returns the enumerator corresponding to the most-derived type of this
|
||||
// object.
|
||||
auto Tag() const -> Kind { return tag; }
|
||||
auto Tag() const -> Kind { return kind; }
|
||||
|
||||
auto SourceLoc() const -> SourceLocation { return loc; }
|
||||
|
||||
@@ -50,10 +50,10 @@ class Statement {
|
||||
// Constructs an Statement representing syntax at the given line number.
|
||||
// `tag` must be the enumerator corresponding to the most-derived type being
|
||||
// constructed.
|
||||
Statement(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
|
||||
Statement(Kind kind, SourceLocation loc) : kind(kind), loc(loc) {}
|
||||
|
||||
private:
|
||||
const Kind tag;
|
||||
const Kind kind;
|
||||
SourceLocation loc;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user