mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:41:06 +01:00
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:
@@ -14,10 +14,6 @@
|
||||
#include "executable_semantics/interpreter/address.h"
|
||||
#include "executable_semantics/interpreter/dictionary.h"
|
||||
|
||||
namespace yy {
|
||||
class parser;
|
||||
}
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
struct Value;
|
||||
@@ -32,148 +28,72 @@ struct TypeCheckContext {
|
||||
Env values;
|
||||
};
|
||||
|
||||
// An existential AST declaration satisfying the Declaration concept.
|
||||
class Declaration {
|
||||
public: // ValueSemantic concept API.
|
||||
Declaration(const Declaration& other) = default;
|
||||
Declaration& operator=(const Declaration& other) = default;
|
||||
|
||||
// Constructs an instance equivalent to `d`, where `Model` satisfies the
|
||||
// Declaration concept.
|
||||
template <class Model>
|
||||
Declaration(Model d) : box(std::make_shared<Boxed<Model>>(d)) {}
|
||||
|
||||
public: // Declaration concept API, in addition to ValueSemantic.
|
||||
void Print() const { box->Print(); }
|
||||
auto Name() const -> std::string { return box->Name(); }
|
||||
|
||||
// Signals a type error if the declaration is not well typed,
|
||||
// otherwise returns this declaration with annotated types.
|
||||
//
|
||||
// - Parameter env: types of run-time names.
|
||||
// - Paraemter ct_env: values of compile-time names.
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration {
|
||||
return box->TypeChecked(env, ct_env);
|
||||
}
|
||||
// Add an entry in the runtime global symbol table for this declaration.
|
||||
void InitGlobals(Env& globals) const { return box->InitGlobals(globals); }
|
||||
// Add an entry in the compile time global symbol tables for this declaration.
|
||||
auto TopLevel(TypeCheckContext& e) const -> void { return box->TopLevel(e); }
|
||||
|
||||
private:
|
||||
// A base class that erases the type of a `Boxed<Content>`, where `Content`
|
||||
// satisfies the Declaration concept.
|
||||
struct Box {
|
||||
protected:
|
||||
Box() {}
|
||||
|
||||
public:
|
||||
Box(const Box& other) = delete;
|
||||
Box& operator=(const Box& other) = delete;
|
||||
|
||||
virtual ~Box() {}
|
||||
virtual auto Print() const -> void = 0;
|
||||
virtual auto Name() const -> std::string = 0;
|
||||
virtual auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration = 0;
|
||||
virtual auto InitGlobals(Env& globals) const -> void = 0;
|
||||
virtual auto TopLevel(TypeCheckContext&) const -> void = 0;
|
||||
};
|
||||
|
||||
// The derived class that holds an instance of `Content` satisfying the
|
||||
// Declaration concept.
|
||||
template <class Content>
|
||||
struct Boxed final : Box {
|
||||
const Content content;
|
||||
explicit Boxed(Content content) : Box(), content(content) {}
|
||||
|
||||
auto Print() const -> void override { return content.Print(); }
|
||||
auto Name() const -> std::string override { return content.Name(); }
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration override {
|
||||
return content.TypeChecked(env, ct_env);
|
||||
}
|
||||
auto InitGlobals(Env& globals) const -> void override {
|
||||
content.InitGlobals(globals);
|
||||
}
|
||||
auto TopLevel(TypeCheckContext& e) const -> void override {
|
||||
content.TopLevel(e);
|
||||
}
|
||||
};
|
||||
|
||||
// Constructs an instance in a "partially formed" state, which can only be
|
||||
// assigned to or destroyed.
|
||||
Declaration() = default;
|
||||
|
||||
// Give Bison access to the default constructor.
|
||||
friend class yy::parser;
|
||||
|
||||
// Note: the pointee is const as long as we have no mutating methods. When
|
||||
std::shared_ptr<const Box> box;
|
||||
enum class DeclarationKind {
|
||||
FunctionDeclaration,
|
||||
StructDeclaration,
|
||||
ChoiceDeclaration,
|
||||
VariableDeclaration,
|
||||
};
|
||||
|
||||
struct FunctionDeclaration {
|
||||
static constexpr DeclarationKind Kind = DeclarationKind::FunctionDeclaration;
|
||||
FunctionDefinition definition;
|
||||
explicit FunctionDeclaration(FunctionDefinition definition)
|
||||
: definition(std::move(definition)) {}
|
||||
|
||||
auto Print() const -> void;
|
||||
auto Name() const -> std::string;
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
|
||||
auto InitGlobals(Env& globals) const -> void;
|
||||
auto TopLevel(TypeCheckContext&) const -> void;
|
||||
};
|
||||
|
||||
struct StructDeclaration {
|
||||
static constexpr DeclarationKind Kind = DeclarationKind::StructDeclaration;
|
||||
StructDefinition definition;
|
||||
StructDeclaration(int line_num, std::string name, std::list<Member*>* members)
|
||||
: definition{line_num, new std::string(name), members} {}
|
||||
|
||||
void Print() const;
|
||||
auto Name() const -> std::string;
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
|
||||
void InitGlobals(Env& globals) const;
|
||||
auto TopLevel(TypeCheckContext&) const -> void;
|
||||
};
|
||||
|
||||
struct ChoiceDeclaration {
|
||||
static constexpr DeclarationKind Kind = DeclarationKind::ChoiceDeclaration;
|
||||
int line_num;
|
||||
std::string name;
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives;
|
||||
|
||||
ChoiceDeclaration(
|
||||
int line_num, std::string name,
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives)
|
||||
: line_num(line_num), name(name), alternatives(std::move(alternatives)) {}
|
||||
|
||||
void Print() const;
|
||||
auto Name() const -> std::string;
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
|
||||
void InitGlobals(Env& globals) const;
|
||||
auto TopLevel(TypeCheckContext&) const -> void;
|
||||
};
|
||||
|
||||
// Global variable definition implements the Declaration concept.
|
||||
class VariableDeclaration {
|
||||
public:
|
||||
VariableDeclaration(int source_location, std::string name,
|
||||
const Expression* type, const Expression* initializer)
|
||||
: source_location(source_location),
|
||||
name(name),
|
||||
type(type),
|
||||
initializer(initializer) {}
|
||||
|
||||
void Print() const;
|
||||
auto Name() const -> std::string;
|
||||
auto TypeChecked(TypeEnv env, Env ct_env) const -> Declaration;
|
||||
void InitGlobals(Env& globals) const;
|
||||
auto TopLevel(TypeCheckContext&) const -> void;
|
||||
|
||||
private:
|
||||
struct VariableDeclaration {
|
||||
static constexpr DeclarationKind Kind = DeclarationKind::VariableDeclaration;
|
||||
int source_location;
|
||||
std::string name;
|
||||
const Expression* type;
|
||||
const Expression* initializer;
|
||||
};
|
||||
|
||||
class Declaration {
|
||||
public:
|
||||
static auto MakeFunctionDeclaration(FunctionDefinition definition)
|
||||
-> const Declaration;
|
||||
static auto MakeStructDeclaration(int line_num, std::string name,
|
||||
std::list<Member*> members)
|
||||
-> const Declaration;
|
||||
static auto MakeChoiceDeclaration(
|
||||
int line_num, std::string name,
|
||||
std::list<std::pair<std::string, const Expression*>> alternatives)
|
||||
-> const Declaration;
|
||||
static auto MakeVariableDeclaration(int source_location, std::string name,
|
||||
const Expression* type,
|
||||
const Expression* initializer)
|
||||
-> const Declaration;
|
||||
|
||||
auto GetFunctionDeclaration() const -> const FunctionDeclaration&;
|
||||
auto GetStructDeclaration() const -> const StructDeclaration&;
|
||||
auto GetChoiceDeclaration() const -> const ChoiceDeclaration&;
|
||||
auto GetVariableDeclaration() const -> const VariableDeclaration&;
|
||||
|
||||
void Print() const;
|
||||
|
||||
inline auto tag() const -> DeclarationKind {
|
||||
return std::visit([](const auto& t) { return t.Kind; }, value);
|
||||
}
|
||||
|
||||
private:
|
||||
std::variant<FunctionDeclaration, StructDeclaration, ChoiceDeclaration,
|
||||
VariableDeclaration>
|
||||
value;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_DECLARATION_H_
|
||||
|
||||
Reference in New Issue
Block a user