-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Declaration to newer property style, class-ify ClassDefinition #859
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 tag() const -> Kind { return tag_; } | ||
|
||
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 tag, SourceLocation source_loc) | ||
: tag_(tag), source_loc_(source_loc) {} | ||
|
||
private: | ||
const Kind tag; | ||
SourceLocation loc; | ||
const Kind tag_; | ||
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->tag() == 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->tag() == 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->tag() == 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_; | ||
Comment on lines
+124
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have a mutable overload? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My preference is to do a mutable overload on an as-needed basis. I expect one to be added, it just didn't need one as of yet, I assume because code wasn't implemented to need it. |
||
} | ||
|
||
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->tag() == 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call this
kind
instead? That's what I wanted to call these methods originally, if not for the name collision with theKind
type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, #860 covers this but may as well reduce merge conflicts here.