Migrate Declaration to newer property style, class-ify ClassDefinition (#859)

This commit is contained in:
Jon Meow
2021-09-29 14:38:18 -07:00
committed by GitHub
parent 25dce9fbcf
commit d157d96338
11 changed files with 127 additions and 115 deletions
+3 -3
View File
@@ -53,7 +53,7 @@ class Action {
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto Tag() const -> Kind { return tag; }
auto Tag() const -> Kind { return kind; }
static void PrintList(const Stack<Nonnull<Action*>>& ls,
llvm::raw_ostream& out);
@@ -64,13 +64,13 @@ class Action {
protected:
// Constructs an Action. `tag` must be the enumerator corresponding to the
// most-derived type being constructed.
explicit Action(Kind tag) : tag(tag) {}
explicit Action(Kind kind) : kind(kind) {}
private:
int pos = 0;
std::vector<Nonnull<const Value*>> results;
const Kind tag;
const Kind kind;
};
class LValAction : public Action {
@@ -107,10 +107,10 @@ auto Interpreter::EvalPrim(Operator op,
}
void Interpreter::InitEnv(const Declaration& d, Env* env) {
switch (d.Tag()) {
switch (d.kind()) {
case Declaration::Kind::FunctionDeclaration: {
const FunctionDefinition& func_def =
cast<FunctionDeclaration>(d).Definition();
cast<FunctionDeclaration>(d).definition();
Env new_env = *env;
// Bring the deduced parameters into scope.
for (const auto& deduced : func_def.deduced_parameters()) {
@@ -125,10 +125,10 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
}
case Declaration::Kind::ClassDeclaration: {
const ClassDefinition& class_def = cast<ClassDeclaration>(d).Definition();
const ClassDefinition& class_def = cast<ClassDeclaration>(d).definition();
VarValues fields;
VarValues methods;
for (Nonnull<const Member*> m : class_def.members) {
for (Nonnull<const Member*> m : class_def.members()) {
switch (m->Tag()) {
case Member::Kind::FieldMember: {
Nonnull<const BindingPattern*> binding =
@@ -141,23 +141,23 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
}
}
}
auto st = arena->New<ClassType>(class_def.name, std::move(fields),
auto st = arena->New<ClassType>(class_def.name(), std::move(fields),
std::move(methods));
auto a = heap.AllocateValue(st);
env->Set(class_def.name, a);
env->Set(class_def.name(), a);
break;
}
case Declaration::Kind::ChoiceDeclaration: {
const auto& choice = cast<ChoiceDeclaration>(d);
VarValues alts;
for (const auto& alternative : choice.Alternatives()) {
for (const auto& alternative : choice.alternatives()) {
auto t = InterpExp(Env(arena), &alternative.signature());
alts.push_back(make_pair(alternative.name(), t));
}
auto ct = arena->New<ChoiceType>(choice.Name(), std::move(alts));
auto ct = arena->New<ChoiceType>(choice.name(), std::move(alts));
auto a = heap.AllocateValue(ct);
env->Set(choice.Name(), a);
env->Set(choice.name(), a);
break;
}
@@ -165,9 +165,9 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
const auto& var = cast<VariableDeclaration>(d);
// Adds an entry in `globals` mapping the variable's name to the
// result of evaluating the initializer.
auto v = InterpExp(*env, var.Initializer());
auto v = InterpExp(*env, &var.initializer());
Address a = heap.AllocateValue(v);
env->Set(*var.Binding()->Name(), a);
env->Set(*var.binding().Name(), a);
break;
}
}
@@ -931,7 +931,7 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
Env ct_top) -> Nonnull<const Value*> {
VarValues fields;
VarValues methods;
for (Nonnull<const Member*> m : sd->members) {
for (Nonnull<const Member*> m : sd->members()) {
switch (m->Tag()) {
case Member::Kind::FieldMember: {
Nonnull<const BindingPattern*> binding =
@@ -951,41 +951,41 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
}
}
}
return arena->New<ClassType>(sd->name, std::move(fields), std::move(methods));
return arena->New<ClassType>(sd->name(), std::move(fields),
std::move(methods));
}
static auto GetName(const Declaration& d) -> const std::string& {
switch (d.Tag()) {
switch (d.kind()) {
case Declaration::Kind::FunctionDeclaration:
return cast<FunctionDeclaration>(d).Definition().name();
return cast<FunctionDeclaration>(d).definition().name();
case Declaration::Kind::ClassDeclaration:
return cast<ClassDeclaration>(d).Definition().name;
return cast<ClassDeclaration>(d).definition().name();
case Declaration::Kind::ChoiceDeclaration:
return cast<ChoiceDeclaration>(d).Name();
return cast<ChoiceDeclaration>(d).name();
case Declaration::Kind::VariableDeclaration: {
Nonnull<const BindingPattern*> binding =
cast<VariableDeclaration>(d).Binding();
if (!binding->Name().has_value()) {
FATAL_COMPILATION_ERROR(binding->SourceLoc())
const BindingPattern& binding = cast<VariableDeclaration>(d).binding();
if (!binding.Name().has_value()) {
FATAL_COMPILATION_ERROR(binding.SourceLoc())
<< "Top-level variable declarations must have names";
}
return *binding->Name();
return *binding.Name();
}
}
}
auto TypeChecker::MakeTypeChecked(Nonnull<Declaration*> d, const TypeEnv& types,
const Env& values) -> Nonnull<Declaration*> {
switch (d->Tag()) {
switch (d->kind()) {
case Declaration::Kind::FunctionDeclaration:
return arena->New<FunctionDeclaration>(TypeCheckFunDef(
&cast<FunctionDeclaration>(*d).Definition(), types, values));
&cast<FunctionDeclaration>(*d).definition(), types, values));
case Declaration::Kind::ClassDeclaration: {
const ClassDefinition& class_def =
cast<ClassDeclaration>(*d).Definition();
cast<ClassDeclaration>(*d).definition();
std::vector<Nonnull<Member*>> fields;
for (Nonnull<Member*> m : class_def.members) {
for (Nonnull<Member*> m : class_def.members()) {
switch (m->Tag()) {
case Member::Kind::FieldMember:
// TODO: Interpret the type expression and store the result.
@@ -993,8 +993,8 @@ auto TypeChecker::MakeTypeChecked(Nonnull<Declaration*> d, const TypeEnv& types,
break;
}
}
return arena->New<ClassDeclaration>(class_def.loc, class_def.name,
std::move(fields));
return arena->New<ClassDeclaration>(class_def.source_loc(),
class_def.name(), std::move(fields));
}
case Declaration::Kind::ChoiceDeclaration:
@@ -1007,17 +1007,17 @@ auto TypeChecker::MakeTypeChecked(Nonnull<Declaration*> d, const TypeEnv& types,
// the declared type of the variable, otherwise returns this
// declaration with annotated types.
TCExpression type_checked_initializer =
TypeCheckExp(var.Initializer(), types, values);
TypeCheckExp(&var.initializer(), types, values);
const auto* binding_type =
dyn_cast<ExpressionPattern>(var.Binding()->Type());
dyn_cast<ExpressionPattern>(var.binding().Type());
if (binding_type == nullptr) {
// TODO: consider adding support for `auto`
FATAL_COMPILATION_ERROR(var.SourceLoc())
FATAL_COMPILATION_ERROR(var.source_loc())
<< "Type of a top-level variable must be an expression.";
}
Nonnull<const Value*> declared_type =
interpreter.InterpExp(values, binding_type->Expression());
ExpectType(var.SourceLoc(), "initializer of variable", declared_type,
ExpectType(var.source_loc(), "initializer of variable", declared_type,
type_checked_initializer.type);
return d;
}
@@ -1025,9 +1025,9 @@ auto TypeChecker::MakeTypeChecked(Nonnull<Declaration*> d, const TypeEnv& types,
}
void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
switch (d->Tag()) {
switch (d->kind()) {
case Declaration::Kind::FunctionDeclaration: {
FunctionDefinition& func_def = cast<FunctionDeclaration>(*d).Definition();
FunctionDefinition& func_def = cast<FunctionDeclaration>(*d).definition();
auto t = TypeOfFunDef(tops->types, tops->values, &func_def);
tops->types.Set(func_def.name(), t);
interpreter.InitEnv(*d, &tops->values);
@@ -1036,10 +1036,10 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
case Declaration::Kind::ClassDeclaration: {
const ClassDefinition& class_def =
cast<ClassDeclaration>(*d).Definition();
cast<ClassDeclaration>(*d).definition();
auto st = TypeOfClassDef(&class_def, tops->types, tops->values);
Address a = interpreter.AllocateValue(st);
tops->values.Set(class_def.name, a); // Is this obsolete?
tops->values.Set(class_def.name(), a); // Is this obsolete?
std::vector<TupleElement> field_types;
for (const auto& [field_name, field_value] :
cast<ClassType>(*st).Fields()) {
@@ -1048,21 +1048,21 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
auto fun_ty = arena->New<FunctionType>(
std::vector<GenericBinding>(),
arena->New<TupleValue>(std::move(field_types)), st);
tops->types.Set(class_def.name, fun_ty);
tops->types.Set(class_def.name(), fun_ty);
break;
}
case Declaration::Kind::ChoiceDeclaration: {
const auto& choice = cast<ChoiceDeclaration>(*d);
VarValues alts;
for (const auto& alternative : choice.Alternatives()) {
for (const auto& alternative : choice.alternatives()) {
auto t = interpreter.InterpExp(tops->values, &alternative.signature());
alts.push_back(std::make_pair(alternative.name(), t));
}
auto ct = arena->New<ChoiceType>(choice.Name(), std::move(alts));
auto ct = arena->New<ChoiceType>(choice.name(), std::move(alts));
Address a = interpreter.AllocateValue(ct);
tops->values.Set(choice.Name(), a); // Is this obsolete?
tops->types.Set(choice.Name(), ct);
tops->values.Set(choice.name(), a); // Is this obsolete?
tops->types.Set(choice.name(), ct);
break;
}
@@ -1071,10 +1071,10 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
// Associate the variable name with it's declared type in the
// compile-time symbol table.
Nonnull<Expression*> type =
cast<ExpressionPattern>(*var.Binding()->Type()).Expression();
cast<ExpressionPattern>(*var.binding().Type()).Expression();
Nonnull<const Value*> declared_type =
interpreter.InterpExp(tops->values, type);
tops->types.Set(*var.Binding()->Name(), declared_type);
tops->types.Set(*var.binding().Name(), declared_type);
break;
}
}
+3 -3
View File
@@ -61,7 +61,7 @@ class Value {
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto Tag() const -> Kind { return tag; }
auto Tag() const -> Kind { return kind; }
void Print(llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
@@ -80,10 +80,10 @@ class Value {
protected:
// Constructs a Value. `tag` must be the enumerator corresponding to the
// most-derived type being constructed.
explicit Value(Kind tag) : tag(tag) {}
explicit Value(Kind kind) : kind(kind) {}
private:
const Kind tag;
const Kind kind;
};
using VarValues = std::vector<std::pair<std::string, Nonnull<const Value*>>>;