mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Merge ClassDefinition and ClassDeclaration (#940)
This commit is contained in:
@@ -127,10 +127,10 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
}
|
||||
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
const ClassDefinition& class_def = cast<ClassDeclaration>(d).definition();
|
||||
const auto& class_decl = cast<ClassDeclaration>(d);
|
||||
std::vector<NamedValue> fields;
|
||||
std::vector<NamedValue> methods;
|
||||
for (Nonnull<const Member*> m : class_def.members()) {
|
||||
for (Nonnull<const Member*> m : class_decl.members()) {
|
||||
switch (m->kind()) {
|
||||
case Member::Kind::FieldMember: {
|
||||
const BindingPattern& binding = cast<FieldMember>(*m).binding();
|
||||
@@ -143,9 +143,9 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
}
|
||||
}
|
||||
auto st = arena_->New<NominalClassType>(
|
||||
class_def.name(), std::move(fields), std::move(methods));
|
||||
class_decl.name(), std::move(fields), std::move(methods));
|
||||
AllocationId a = heap_.AllocateValue(st);
|
||||
env->Set(class_def.name(), a);
|
||||
env->Set(class_decl.name(), a);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -141,10 +141,10 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration,
|
||||
break;
|
||||
}
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
auto& class_def = cast<ClassDeclaration>(declaration).definition();
|
||||
static_scope.Add(class_def.name(), &declaration);
|
||||
for (auto* member : class_def.members()) {
|
||||
PopulateNamesInMember(arena, *member, class_def.static_scope());
|
||||
auto& class_decl = cast<ClassDeclaration>(declaration);
|
||||
static_scope.Add(class_decl.name(), &declaration);
|
||||
for (auto* member : class_decl.members()) {
|
||||
PopulateNamesInMember(arena, *member, class_decl.static_scope());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1088,11 +1088,12 @@ auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values,
|
||||
ret);
|
||||
}
|
||||
|
||||
auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
|
||||
Env ct_top) -> Nonnull<const Value*> {
|
||||
auto TypeChecker::TypeOfClassDecl(const ClassDeclaration& class_decl,
|
||||
TypeEnv /*types*/, Env ct_top)
|
||||
-> Nonnull<const Value*> {
|
||||
std::vector<NamedValue> fields;
|
||||
std::vector<NamedValue> methods;
|
||||
for (Nonnull<const Member*> m : sd->members()) {
|
||||
for (Nonnull<const Member*> m : class_decl.members()) {
|
||||
switch (m->kind()) {
|
||||
case Member::Kind::FieldMember: {
|
||||
const BindingPattern& binding = cast<FieldMember>(*m).binding();
|
||||
@@ -1111,7 +1112,7 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/,
|
||||
}
|
||||
}
|
||||
}
|
||||
return arena_->New<NominalClassType>(sd->name(), std::move(fields),
|
||||
return arena_->New<NominalClassType>(class_decl.name(), std::move(fields),
|
||||
std::move(methods));
|
||||
}
|
||||
|
||||
@@ -1120,7 +1121,7 @@ static auto GetName(const Declaration& d) -> const std::string& {
|
||||
case Declaration::Kind::FunctionDeclaration:
|
||||
return cast<FunctionDeclaration>(d).name();
|
||||
case Declaration::Kind::ClassDeclaration:
|
||||
return cast<ClassDeclaration>(d).definition().name();
|
||||
return cast<ClassDeclaration>(d).name();
|
||||
case Declaration::Kind::ChoiceDeclaration:
|
||||
return cast<ChoiceDeclaration>(d).name();
|
||||
case Declaration::Kind::VariableDeclaration: {
|
||||
@@ -1192,11 +1193,11 @@ void TypeChecker::TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops) {
|
||||
}
|
||||
|
||||
case Declaration::Kind::ClassDeclaration: {
|
||||
const auto& class_def = cast<ClassDeclaration>(*d).definition();
|
||||
auto st = TypeOfClassDef(&class_def, tops->types, tops->values);
|
||||
const auto& class_decl = cast<ClassDeclaration>(*d);
|
||||
auto st = TypeOfClassDecl(class_decl, tops->types, tops->values);
|
||||
AllocationId a = interpreter_.AllocateValue(st);
|
||||
tops->values.Set(class_def.name(), a); // Is this obsolete?
|
||||
tops->types.Set(class_def.name(), st);
|
||||
tops->values.Set(class_decl.name(), a); // Is this obsolete?
|
||||
tops->types.Set(class_decl.name(), st);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -132,8 +132,8 @@ class TypeChecker {
|
||||
|
||||
auto TypeOfFunDef(TypeEnv types, Env values, FunctionDeclaration* fun_def)
|
||||
-> Nonnull<const Value*>;
|
||||
auto TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, Env ct_top)
|
||||
-> Nonnull<const Value*>;
|
||||
auto TypeOfClassDecl(const ClassDeclaration& class_decl, TypeEnv /*types*/,
|
||||
Env ct_top) -> Nonnull<const Value*>;
|
||||
|
||||
auto TopLevel(std::vector<Nonnull<Declaration*>>* fs) -> TypeCheckContext;
|
||||
void TopLevel(Nonnull<Declaration*> d, TypeCheckContext* tops);
|
||||
|
||||
Reference in New Issue
Block a user