Commit 0d3fd910 authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[torque] Refactor global class list

This stores a type alias in the global class list, so we can get
to the declarable and figure out namespace / file.

R=szuend@chromium.org

Bug: v8:7793
Change-Id: I2164b77e559d28fa18da12192a34e0a7264b858e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1613989
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarSimon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61566}
parent 0552d1d3
...@@ -51,13 +51,14 @@ class GlobalContext : public ContextualClass<GlobalContext> { ...@@ -51,13 +51,14 @@ class GlobalContext : public ContextualClass<GlobalContext> {
return result; return result;
} }
static void RegisterClass(const std::string& name, ClassType* new_class) { static void RegisterClass(const TypeAlias* alias) {
Get().classes_[name] = new_class; DCHECK(alias->ParentScope()->IsNamespace());
Get().classes_.push_back(alias);
} }
static const std::map<std::string, ClassType*>& GetClasses() { using GlobalClassList = std::vector<const TypeAlias*>;
return Get().classes_;
} static const GlobalClassList& GetClasses() { return Get().classes_; }
static void AddCppInclude(std::string include_path) { static void AddCppInclude(std::string include_path) {
Get().cpp_includes_.push_back(std::move(include_path)); Get().cpp_includes_.push_back(std::move(include_path));
...@@ -87,7 +88,7 @@ class GlobalContext : public ContextualClass<GlobalContext> { ...@@ -87,7 +88,7 @@ class GlobalContext : public ContextualClass<GlobalContext> {
Ast ast_; Ast ast_;
std::vector<std::unique_ptr<Declarable>> declarables_; std::vector<std::unique_ptr<Declarable>> declarables_;
std::vector<std::string> cpp_includes_; std::vector<std::string> cpp_includes_;
std::map<std::string, ClassType*> classes_; GlobalClassList classes_;
friend class LanguageServerData; friend class LanguageServerData;
}; };
......
...@@ -3098,8 +3098,8 @@ void ImplementationVisitor::GenerateClassDefinitions( ...@@ -3098,8 +3098,8 @@ void ImplementationVisitor::GenerateClassDefinitions(
NamespaceScope implementation_namespaces(implementation, NamespaceScope implementation_namespaces(implementation,
{"v8", "internal"}); {"v8", "internal"});
for (auto i : GlobalContext::GetClasses()) { for (const TypeAlias* alias : GlobalContext::GetClasses()) {
ClassType* type = i.second; const ClassType* type = ClassType::DynamicCast(alias->type());
if (!type->GenerateCppClassDefinitions()) continue; if (!type->GenerateCppClassDefinitions()) continue;
CppClassGenerator g(type, header, inline_header, implementation); CppClassGenerator g(type, header, inline_header, implementation);
g.GenerateClass(); g.GenerateClass();
...@@ -3117,8 +3117,8 @@ void ImplementationVisitor::GenerateClassFieldOffsets( ...@@ -3117,8 +3117,8 @@ void ImplementationVisitor::GenerateClassFieldOffsets(
{ {
IncludeGuardScope include_guard(new_contents_stream, file_name); IncludeGuardScope include_guard(new_contents_stream, file_name);
for (auto i : GlobalContext::GetClasses()) { for (const TypeAlias* alias : GlobalContext::GetClasses()) {
ClassType* type = i.second; const ClassType* type = ClassType::DynamicCast(alias->type());
if (!type->IsExtern()) continue; if (!type->IsExtern()) continue;
// TODO(danno): Ideally (and we've got several core V8 dev's feedback // TODO(danno): Ideally (and we've got several core V8 dev's feedback
...@@ -3128,7 +3128,7 @@ void ImplementationVisitor::GenerateClassFieldOffsets( ...@@ -3128,7 +3128,7 @@ void ImplementationVisitor::GenerateClassFieldOffsets(
// values come from. // values come from.
new_contents_stream << "#define "; new_contents_stream << "#define ";
new_contents_stream << "TORQUE_GENERATED_" new_contents_stream << "TORQUE_GENERATED_"
<< CapifyStringWithUnderscores(i.first) << CapifyStringWithUnderscores(type->name())
<< "_FIELDS(V) \\\n"; << "_FIELDS(V) \\\n";
std::vector<Field> fields = type->fields(); std::vector<Field> fields = type->fields();
FieldSectionType section = FieldSectionType::kNoSection; FieldSectionType section = FieldSectionType::kNoSection;
...@@ -3217,8 +3217,8 @@ void ImplementationVisitor::GeneratePrintDefinitions( ...@@ -3217,8 +3217,8 @@ void ImplementationVisitor::GeneratePrintDefinitions(
NamespaceScope impl_namespaces(impl, {"v8", "internal"}); NamespaceScope impl_namespaces(impl, {"v8", "internal"});
for (auto i : GlobalContext::GetClasses()) { for (const TypeAlias* alias : GlobalContext::GetClasses()) {
ClassType* type = i.second; const ClassType* type = ClassType::DynamicCast(alias->type());
if (!type->ShouldGeneratePrint()) continue; if (!type->ShouldGeneratePrint()) continue;
if (type->IsExtern() && type->GenerateCppClassDefinitions()) { if (type->IsExtern() && type->GenerateCppClassDefinitions()) {
...@@ -3337,8 +3337,8 @@ void ImplementationVisitor::GenerateClassVerifiers( ...@@ -3337,8 +3337,8 @@ void ImplementationVisitor::GenerateClassVerifiers(
// Generate forward declarations to avoid including any headers. // Generate forward declarations to avoid including any headers.
h_contents << "class Isolate;\n"; h_contents << "class Isolate;\n";
for (auto i : GlobalContext::GetClasses()) { for (const TypeAlias* alias : GlobalContext::GetClasses()) {
ClassType* type = i.second; const ClassType* type = ClassType::DynamicCast(alias->type());
if (!type->IsExtern() || !type->ShouldGenerateVerify()) continue; if (!type->IsExtern() || !type->ShouldGenerateVerify()) continue;
h_contents << "class " << type->name() << ";\n"; h_contents << "class " << type->name() << ";\n";
} }
...@@ -3348,17 +3348,18 @@ void ImplementationVisitor::GenerateClassVerifiers( ...@@ -3348,17 +3348,18 @@ void ImplementationVisitor::GenerateClassVerifiers(
h_contents << "class " << verifier_class << "{\n"; h_contents << "class " << verifier_class << "{\n";
h_contents << " public:\n"; h_contents << " public:\n";
for (auto i : GlobalContext::GetClasses()) { for (const TypeAlias* alias : GlobalContext::GetClasses()) {
ClassType* type = i.second; const ClassType* type = ClassType::DynamicCast(alias->type());
std::string name = type->name();
if (!type->IsExtern() || !type->ShouldGenerateVerify()) continue; if (!type->IsExtern() || !type->ShouldGenerateVerify()) continue;
std::string method_name = i.first + "Verify"; std::string method_name = name + "Verify";
h_contents << " static void " << method_name << "(" << i.first h_contents << " static void " << method_name << "(" << name
<< " o, Isolate* isolate);\n"; << " o, Isolate* isolate);\n";
cc_contents << "void " << verifier_class << "::" << method_name << "(" cc_contents << "void " << verifier_class << "::" << method_name << "("
<< i.first << " o, Isolate* isolate) {\n"; << name << " o, Isolate* isolate) {\n";
// First, do any verification for the super class. Not all classes have // First, do any verification for the super class. Not all classes have
// verifiers, so skip to the nearest super class that has one. // verifiers, so skip to the nearest super class that has one.
...@@ -3385,13 +3386,12 @@ void ImplementationVisitor::GenerateClassVerifiers( ...@@ -3385,13 +3386,12 @@ void ImplementationVisitor::GenerateClassVerifiers(
cc_contents << " // Instance type check skipped because\n"; cc_contents << " // Instance type check skipped because\n";
cc_contents << " // it is an instantiated abstract class.\n"; cc_contents << " // it is an instantiated abstract class.\n";
} else { } else {
cc_contents << " CHECK(o.Is" << i.first << "());\n"; cc_contents << " CHECK(o.Is" << name << "());\n";
} }
// Third, verify its properties. // Third, verify its properties.
for (auto f : type->fields()) { for (auto f : type->fields()) {
GenerateClassFieldVerifier(i.first, *i.second, f, h_contents, GenerateClassFieldVerifier(name, *type, f, h_contents, cc_contents);
cc_contents);
} }
cc_contents << "}\n"; cc_contents << "}\n";
......
...@@ -129,6 +129,7 @@ const ClassType* TypeVisitor::ComputeType(ClassDeclaration* decl) { ...@@ -129,6 +129,7 @@ const ClassType* TypeVisitor::ComputeType(ClassDeclaration* decl) {
// TODO(sigurds): Remove this hack by introducing a declarable for classes. // TODO(sigurds): Remove this hack by introducing a declarable for classes.
const TypeAlias* alias = const TypeAlias* alias =
Declarations::LookupTypeAlias(QualifiedName(decl->name->value)); Declarations::LookupTypeAlias(QualifiedName(decl->name->value));
GlobalContext::RegisterClass(alias);
DCHECK_EQ(*alias->delayed_, decl); DCHECK_EQ(*alias->delayed_, decl);
if (decl->flags & ClassFlag::kExtern) { if (decl->flags & ClassFlag::kExtern) {
if (!decl->super) { if (!decl->super) {
...@@ -163,7 +164,6 @@ const ClassType* TypeVisitor::ComputeType(ClassDeclaration* decl) { ...@@ -163,7 +164,6 @@ const ClassType* TypeVisitor::ComputeType(ClassDeclaration* decl) {
TypeOracle::GetClassType(TypeOracle::GetTaggedType(), decl->name->value, TypeOracle::GetClassType(TypeOracle::GetTaggedType(), decl->name->value,
decl->flags, "FixedArray", decl, alias); decl->flags, "FixedArray", decl, alias);
} }
GlobalContext::RegisterClass(decl->name->value, new_class);
return new_class; return new_class;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment