diff --git a/executable_semantics/ast/BUILD b/executable_semantics/ast/BUILD index ed85e312b78f..996196024d9a 100644 --- a/executable_semantics/ast/BUILD +++ b/executable_semantics/ast/BUILD @@ -14,17 +14,6 @@ cc_library( ], ) -cc_library( - name = "class_definition", - hdrs = ["class_definition.h"], - deps = [ - ":member", - ":source_location", - "//common:ostream", - "@llvm-project//llvm:Support", - ], -) - cc_library( name = "declaration", srcs = ["declaration.cpp"], @@ -32,7 +21,6 @@ cc_library( "declaration.h", ], deps = [ - ":class_definition", ":member", ":pattern", ":source_location", diff --git a/executable_semantics/ast/class_definition.h b/executable_semantics/ast/class_definition.h deleted file mode 100644 index bc9a013357d3..000000000000 --- a/executable_semantics/ast/class_definition.h +++ /dev/null @@ -1,44 +0,0 @@ -// Part of the Carbon Language project, under the Apache License v2.0 with LLVM -// Exceptions. See /LICENSE for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#ifndef EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_ -#define EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_ - -#include -#include - -#include "executable_semantics/ast/member.h" -#include "executable_semantics/ast/source_location.h" -#include "executable_semantics/ast/static_scope.h" - -namespace Carbon { - -class StaticScope; - -class ClassDefinition { - public: - ClassDefinition(SourceLocation source_loc, std::string name, - std::vector> members) - : source_loc_(source_loc), - name_(std::move(name)), - members_(std::move(members)) {} - - auto source_loc() const -> SourceLocation { return source_loc_; } - auto name() const -> const std::string& { return name_; } - auto members() const -> llvm::ArrayRef> { return members_; } - - // Contains class members. - auto static_scope() const -> const StaticScope& { return static_scope_; } - auto static_scope() -> StaticScope& { return static_scope_; } - - private: - SourceLocation source_loc_; - std::string name_; - std::vector> members_; - StaticScope static_scope_; -}; - -} // namespace Carbon - -#endif // EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_ diff --git a/executable_semantics/ast/declaration.cpp b/executable_semantics/ast/declaration.cpp index 46d75d7349a6..2960796e1500 100644 --- a/executable_semantics/ast/declaration.cpp +++ b/executable_semantics/ast/declaration.cpp @@ -17,10 +17,9 @@ void Declaration::Print(llvm::raw_ostream& out) const { break; case Kind::ClassDeclaration: { - const ClassDefinition& class_def = - cast(*this).definition(); - out << "class " << class_def.name() << " {\n"; - for (Nonnull m : class_def.members()) { + const auto& class_decl = cast(*this); + out << "class " << class_decl.name() << " {\n"; + for (Nonnull m : class_decl.members()) { out << *m; } out << "}\n"; diff --git a/executable_semantics/ast/declaration.h b/executable_semantics/ast/declaration.h index 0a623e98b167..ae3fcb48bf42 100644 --- a/executable_semantics/ast/declaration.h +++ b/executable_semantics/ast/declaration.h @@ -10,7 +10,6 @@ #include #include "common/ostream.h" -#include "executable_semantics/ast/class_definition.h" #include "executable_semantics/ast/member.h" #include "executable_semantics/ast/pattern.h" #include "executable_semantics/ast/source_location.h" @@ -160,17 +159,24 @@ class ClassDeclaration : public Declaration { ClassDeclaration(SourceLocation source_loc, std::string name, std::vector> members) : Declaration(Kind::ClassDeclaration, source_loc), - definition_(source_loc, std::move(name), std::move(members)) {} + name_(std::move(name)), + members_(std::move(members)) {} static auto classof(const Declaration* decl) -> bool { return decl->kind() == Kind::ClassDeclaration; } - auto definition() const -> const ClassDefinition& { return definition_; } - auto definition() -> ClassDefinition& { return definition_; } + auto name() const -> const std::string& { return name_; } + auto members() const -> llvm::ArrayRef> { return members_; } + + // Contains class members. Scoped variables are in the body. + auto static_scope() const -> const StaticScope& { return static_scope_; } + auto static_scope() -> StaticScope& { return static_scope_; } private: - ClassDefinition definition_; + std::string name_; + std::vector> members_; + StaticScope static_scope_; }; class ChoiceDeclaration : public Declaration { diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 981a64d34885..249331028ef8 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -127,10 +127,10 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) { } case Declaration::Kind::ClassDeclaration: { - const ClassDefinition& class_def = cast(d).definition(); + const auto& class_decl = cast(d); std::vector fields; std::vector methods; - for (Nonnull m : class_def.members()) { + for (Nonnull m : class_decl.members()) { switch (m->kind()) { case Member::Kind::FieldMember: { const BindingPattern& binding = cast(*m).binding(); @@ -143,9 +143,9 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) { } } auto st = arena_->New( - 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; } diff --git a/executable_semantics/interpreter/resolve_names.cpp b/executable_semantics/interpreter/resolve_names.cpp index f5c59b69f11c..b847447fc749 100644 --- a/executable_semantics/interpreter/resolve_names.cpp +++ b/executable_semantics/interpreter/resolve_names.cpp @@ -141,10 +141,10 @@ void PopulateNamesInDeclaration(Arena* arena, Declaration& declaration, break; } case Declaration::Kind::ClassDeclaration: { - auto& class_def = cast(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(declaration); + static_scope.Add(class_decl.name(), &declaration); + for (auto* member : class_decl.members()) { + PopulateNamesInMember(arena, *member, class_decl.static_scope()); } break; } diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 7e70c36ce26a..135a99cf38b3 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -1088,11 +1088,12 @@ auto TypeChecker::TypeOfFunDef(TypeEnv types, Env values, ret); } -auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, - Env ct_top) -> Nonnull { +auto TypeChecker::TypeOfClassDecl(const ClassDeclaration& class_decl, + TypeEnv /*types*/, Env ct_top) + -> Nonnull { std::vector fields; std::vector methods; - for (Nonnull m : sd->members()) { + for (Nonnull m : class_decl.members()) { switch (m->kind()) { case Member::Kind::FieldMember: { const BindingPattern& binding = cast(*m).binding(); @@ -1111,7 +1112,7 @@ auto TypeChecker::TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, } } } - return arena_->New(sd->name(), std::move(fields), + return arena_->New(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(d).name(); case Declaration::Kind::ClassDeclaration: - return cast(d).definition().name(); + return cast(d).name(); case Declaration::Kind::ChoiceDeclaration: return cast(d).name(); case Declaration::Kind::VariableDeclaration: { @@ -1192,11 +1193,11 @@ void TypeChecker::TopLevel(Nonnull d, TypeCheckContext* tops) { } case Declaration::Kind::ClassDeclaration: { - const auto& class_def = cast(*d).definition(); - auto st = TypeOfClassDef(&class_def, tops->types, tops->values); + const auto& class_decl = cast(*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; } diff --git a/executable_semantics/interpreter/type_checker.h b/executable_semantics/interpreter/type_checker.h index 467c430f66f2..7874b77f806f 100644 --- a/executable_semantics/interpreter/type_checker.h +++ b/executable_semantics/interpreter/type_checker.h @@ -132,8 +132,8 @@ class TypeChecker { auto TypeOfFunDef(TypeEnv types, Env values, FunctionDeclaration* fun_def) -> Nonnull; - auto TypeOfClassDef(const ClassDefinition* sd, TypeEnv /*types*/, Env ct_top) - -> Nonnull; + auto TypeOfClassDecl(const ClassDeclaration& class_decl, TypeEnv /*types*/, + Env ct_top) -> Nonnull; auto TopLevel(std::vector>* fs) -> TypeCheckContext; void TopLevel(Nonnull d, TypeCheckContext* tops);