Merge ClassDefinition and ClassDeclaration (#940)

This commit is contained in:
Jon Meow
2021-11-09 15:24:22 -08:00
committed by GitHub
parent 73eea8e47a
commit e8d9b7675b
8 changed files with 34 additions and 84 deletions
-12
View File
@@ -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",
@@ -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 <string>
#include <vector>
#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<Nonnull<Member*>> 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<Nonnull<Member*>> { 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<Nonnull<Member*>> members_;
StaticScope static_scope_;
};
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_AST_CLASS_DEFINITION_H_
+3 -4
View File
@@ -17,10 +17,9 @@ void Declaration::Print(llvm::raw_ostream& out) const {
break;
case Kind::ClassDeclaration: {
const ClassDefinition& class_def =
cast<ClassDeclaration>(*this).definition();
out << "class " << class_def.name() << " {\n";
for (Nonnull<Member*> m : class_def.members()) {
const auto& class_decl = cast<ClassDeclaration>(*this);
out << "class " << class_decl.name() << " {\n";
for (Nonnull<Member*> m : class_decl.members()) {
out << *m;
}
out << "}\n";
+11 -5
View File
@@ -10,7 +10,6 @@
#include <vector>
#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<Nonnull<Member*>> 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<Nonnull<Member*>> { 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<Nonnull<Member*>> members_;
StaticScope static_scope_;
};
class ChoiceDeclaration : public Declaration {