[executable semantics] class-ify Declaration (#307)

* [executable semantics] class-ify Declaration

NFC (no functional change).

Proof of concept that we can simplify code by replacing unions with safer, more
regular types.  Hand-rolled existentials (type-erasing CoW wrappers) are a follow-on
step that will further simplify usage.

Began adding `const` where possible, and replacing `std::string*` with
`std::string`.  Most `const`s can disappear as we replace reference semantics
with value semantics, but in the meantime it's an important step in the right
direction.
This commit is contained in:
Dave Abrahams
2021-02-27 13:32:03 -08:00
committed by GitHub
parent cd57024042
commit 2205fd52ce
14 changed files with 191 additions and 210 deletions
+16 -49
View File
@@ -8,57 +8,24 @@
namespace Carbon {
auto MakeFunDecl(FunctionDefinition* f) -> Declaration* {
auto* d = new Declaration();
d->tag = DeclarationKind::FunctionDeclaration;
d->u.fun_def = f;
return d;
}
void FunctionDeclaration::Print() const { PrintFunDef(definition); }
auto MakeStructDecl(int line_num, std::string name, std::list<Member*>* members)
-> Declaration* {
auto* d = new Declaration();
d->tag = DeclarationKind::StructDeclaration;
d->u.struct_def = new StructDefinition();
d->u.struct_def->line_num = line_num;
d->u.struct_def->name = new std::string(std::move(name));
d->u.struct_def->members = members;
return d;
}
auto MakeChoiceDecl(int line_num, std::string name,
std::list<std::pair<std::string, Expression*>>* alts)
-> Declaration* {
auto* d = new Declaration();
d->tag = DeclarationKind::ChoiceDeclaration;
d->u.choice_def.line_num = line_num;
d->u.choice_def.name = new std::string(std::move(name));
d->u.choice_def.alternatives = alts;
return d;
}
void PrintDecl(Declaration* d) {
switch (d->tag) {
case DeclarationKind::FunctionDeclaration:
PrintFunDef(d->u.fun_def);
break;
case DeclarationKind::StructDeclaration:
std::cout << "struct " << *d->u.struct_def->name << " {" << std::endl;
for (auto& member : *d->u.struct_def->members) {
PrintMember(member);
}
std::cout << "}" << std::endl;
break;
case DeclarationKind::ChoiceDeclaration:
std::cout << "choice " << *d->u.choice_def.name << " {" << std::endl;
for (auto& alternative : *d->u.choice_def.alternatives) {
std::cout << "alt " << alternative.first << " ";
PrintExp(alternative.second);
std::cout << ";" << std::endl;
}
std::cout << "}" << std::endl;
break;
void StructDeclaration::Print() const {
std::cout << "struct " << *definition.name << " {" << std::endl;
for (auto& member : *definition.members) {
PrintMember(member);
}
std::cout << "}" << std::endl;
}
void ChoiceDeclaration::Print() const {
std::cout << "choice " << name << " {" << std::endl;
for (auto& alternative : alternatives) {
std::cout << "alt " << alternative.first << " ";
PrintExp(alternative.second);
std::cout << ";" << std::endl;
}
std::cout << "}" << std::endl;
}
} // namespace Carbon
+53 -25
View File
@@ -14,36 +14,64 @@
namespace Carbon {
enum class DeclarationKind {
FunctionDeclaration,
StructDeclaration,
ChoiceDeclaration
};
struct Value;
template <class K, class V>
struct AssocList;
using Address = unsigned int;
using TypeEnv = AssocList<std::string, Value*>;
using Env = AssocList<std::string, Address>;
/// TODO:explain this. Also name it if necessary. Consult with jsiek.
using ExecutionEnvironment = std::pair<TypeEnv*, Env*>;
struct Declaration {
DeclarationKind tag;
union {
struct FunctionDefinition* fun_def;
struct StructDefinition* struct_def;
struct {
int line_num;
std::string* name;
std::list<std::pair<std::string, Expression*>>* alternatives;
} choice_def;
} u;
virtual void Print() const = 0;
virtual auto Name() const -> std::string = 0;
virtual auto TypeChecked(TypeEnv* env, Env* ct_env) const
-> const Declaration* = 0;
virtual void InitGlobals(Env*& globals) const = 0;
virtual auto TopLevel(ExecutionEnvironment&) const -> void = 0;
};
auto MakeFunDecl(struct FunctionDefinition* f) -> Declaration*;
auto MakeStructDecl(int line_num, std::string name, std::list<Member*>* members)
-> Declaration*;
auto MakeChoiceDecl(int line_num, std::string name,
std::list<std::pair<std::string, Expression*>>* alts)
-> Declaration*;
struct FunctionDeclaration : Declaration {
const FunctionDefinition* definition;
explicit FunctionDeclaration(const FunctionDefinition* definition)
: definition(definition) {}
void PrintDecl(Declaration* d);
void Print() const;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> const Declaration*;
void InitGlobals(Env*& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
struct StructDeclaration : Declaration {
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 -> const Declaration*;
void InitGlobals(Env*& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
struct ChoiceDeclaration : Declaration {
int line_num;
std::string name;
std::list<std::pair<std::string, Expression*>> alternatives;
ChoiceDeclaration(int line_num, std::string name,
std::list<std::pair<std::string, Expression*>> alternatives)
: line_num(line_num), name(name), alternatives(alternatives) {}
void Print() const;
auto Name() const -> std::string;
auto TypeChecked(TypeEnv* env, Env* ct_env) const -> const Declaration*;
void InitGlobals(Env*& globals) const;
auto TopLevel(ExecutionEnvironment&) const -> void;
};
} // namespace Carbon
+1 -1
View File
@@ -197,7 +197,7 @@ static void PrintFields(
}
}
void PrintExp(Expression* e) {
void PrintExp(const Expression* e) {
switch (e->tag) {
case ExpressionKind::Index:
PrintExp(e->u.index.aggregate);
+1 -1
View File
@@ -109,7 +109,7 @@ auto MakeFunType(int line_num, Expression* param, Expression* ret)
-> Expression*;
auto MakeAutoType(int line_num) -> Expression*;
void PrintExp(Expression* exp);
void PrintExp(const Expression* exp);
} // namespace Carbon
@@ -20,7 +20,7 @@ auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
return f;
}
void PrintFunDefDepth(struct FunctionDefinition* f, int depth) {
void PrintFunDefDepth(const FunctionDefinition* f, int depth) {
std::cout << "fn " << f->name << " ";
PrintExp(f->param_pattern);
std::cout << " -> ";
@@ -34,6 +34,6 @@ void PrintFunDefDepth(struct FunctionDefinition* f, int depth) {
}
}
void PrintFunDef(struct FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
void PrintFunDef(const FunctionDefinition* f) { PrintFunDefDepth(f, -1); }
} // namespace Carbon
@@ -19,10 +19,9 @@ struct FunctionDefinition {
};
auto MakeFunDef(int line_num, std::string name, Expression* ret_type,
Expression* param, Statement* body)
-> struct FunctionDefinition*;
void PrintFunDef(struct FunctionDefinition*);
void PrintFunDefDepth(struct FunctionDefinition*, int);
Expression* param, Statement* body) -> FunctionDefinition*;
void PrintFunDef(const FunctionDefinition*);
void PrintFunDefDepth(const FunctionDefinition*, int);
} // namespace Carbon