Migrate declaration parsing to value semantics (#614)

* Use Expression by value in FunctionDefinition
* Migrate choice declarations to value semantics
* Migrate Declaration parsing to value semantics
This commit is contained in:
Geoff Romer
2021-06-30 13:09:12 -07:00
committed by GitHub
parent 7e70a0335e
commit fa5b9cd553
9 changed files with 90 additions and 81 deletions
+19 -10
View File
@@ -13,6 +13,10 @@
#include "executable_semantics/ast/struct_definition.h"
#include "executable_semantics/interpreter/dictionary.h"
namespace yy {
class parser;
}
namespace Carbon {
struct Value;
@@ -56,7 +60,7 @@ class Declaration {
// Add an entry in the compile time global symbol tables for this declaration.
auto TopLevel(TypeCheckContext& e) const -> void { return box->TopLevel(e); }
private: // types
private:
// A base class that erases the type of a `Boxed<Content>`, where `Content`
// satisfies the Declaration concept.
struct Box {
@@ -95,15 +99,21 @@ class Declaration {
}
};
private: // data members
// 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;
};
struct FunctionDeclaration {
const FunctionDefinition* definition;
explicit FunctionDeclaration(const FunctionDefinition* definition)
: definition(definition) {}
FunctionDefinition definition;
explicit FunctionDeclaration(FunctionDefinition definition)
: definition(std::move(definition)) {}
auto Print() const -> void;
auto Name() const -> std::string;
@@ -127,12 +137,11 @@ struct StructDeclaration {
struct ChoiceDeclaration {
int line_num;
std::string name;
std::list<std::pair<std::string, const Expression*>> alternatives;
std::list<std::pair<std::string, 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(alternatives) {}
ChoiceDeclaration(int line_num, std::string name,
std::list<std::pair<std::string, Expression>> alternatives)
: line_num(line_num), name(name), alternatives(std::move(alternatives)) {}
void Print() const;
auto Name() const -> std::string;