mirror of
https://github.com/dart-lang/sdk
synced 2024-09-22 13:33:31 +00:00
Implement class hierarchy analysis in the VM.
Add test. Review URL: https://chromiumcodereview.appspot.com//10828399 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@11006 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
1e14772bb0
commit
fea5f83714
|
@ -1122,6 +1122,9 @@ void ClassFinalizer::FinalizeClass(const Class& cls, bool generating_snapshot) {
|
|||
name.ToCString());
|
||||
}
|
||||
cls.Finalize();
|
||||
// Signature classes extend Object. No need to add this class to the direct
|
||||
// subclasses of Object.
|
||||
ASSERT(super_type.IsNull() || super_type.IsObjectType());
|
||||
return;
|
||||
}
|
||||
// Finalize factory class, if any.
|
||||
|
@ -1168,6 +1171,12 @@ void ClassFinalizer::FinalizeClass(const Class& cls, bool generating_snapshot) {
|
|||
cls_name.ToCString(), lib_name.ToCString());
|
||||
}
|
||||
}
|
||||
// Add this class to the direct subclasses of the superclass, unless the
|
||||
// superclass is Object.
|
||||
if (!super_type.IsNull() && !super_type.IsObjectType()) {
|
||||
ASSERT(!super_class.IsNull());
|
||||
super_class.AddDirectSubclass(cls);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -96,4 +96,79 @@ void ClassTable::Print() {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// Returns true if the given array of cids contains the given cid.
|
||||
static bool ContainsCid(ZoneGrowableArray<intptr_t>* cids, intptr_t cid) {
|
||||
for (intptr_t i = 0; i < cids->length(); i++) {
|
||||
if ((*cids)[i] == cid) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Recursively collect direct and indirect subclass ids of cls.
|
||||
static void CollectSubclassIds(ZoneGrowableArray<intptr_t>* cids,
|
||||
const Class& cls) {
|
||||
const GrowableObjectArray& cls_direct_subclasses =
|
||||
GrowableObjectArray::Handle(cls.direct_subclasses());
|
||||
if (cls_direct_subclasses.IsNull()) {
|
||||
return;
|
||||
}
|
||||
Class& direct_subclass = Class::Handle();
|
||||
for (intptr_t i = 0; i < cls_direct_subclasses.Length(); i++) {
|
||||
direct_subclass ^= cls_direct_subclasses.At(i);
|
||||
intptr_t direct_subclass_id = direct_subclass.id();
|
||||
if (!ContainsCid(cids, direct_subclass_id)) {
|
||||
cids->Add(direct_subclass_id);
|
||||
CollectSubclassIds(cids, direct_subclass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ZoneGrowableArray<intptr_t>* ClassTable::GetSubclassIdsOf(intptr_t cid) const {
|
||||
const Class& cls = Class::Handle(At(cid));
|
||||
ASSERT(!cls.IsNull());
|
||||
// TODO(regis): Replace assert below with ASSERT(cid > kDartObjectCid).
|
||||
ASSERT(!cls.IsObjectClass());
|
||||
ZoneGrowableArray<intptr_t>* ids = new ZoneGrowableArray<intptr_t>();
|
||||
CollectSubclassIds(ids, cls);
|
||||
return ids;
|
||||
}
|
||||
|
||||
|
||||
ZoneGrowableArray<Function*>* ClassTable::GetNamedInstanceFunctionsOf(
|
||||
const ZoneGrowableArray<intptr_t>& cids,
|
||||
const String& function_name) const {
|
||||
ASSERT(!function_name.IsNull());
|
||||
ZoneGrowableArray<Function*>* functions = new ZoneGrowableArray<Function*>();
|
||||
Class& cls = Class::Handle();
|
||||
Function& cls_function = Function::Handle();
|
||||
for (intptr_t i = 0; i < cids.length(); i++) {
|
||||
const intptr_t cid = cids[i];
|
||||
cls = At(cid);
|
||||
// TODO(regis): Replace assert below with ASSERT(cid > kDartObjectCid).
|
||||
ASSERT(!cls.IsObjectClass());
|
||||
cls_function = cls.LookupDynamicFunction(function_name);
|
||||
if (!cls_function.IsNull()) {
|
||||
functions->Add(&Function::ZoneHandle(cls_function.raw()));
|
||||
}
|
||||
}
|
||||
return functions;
|
||||
}
|
||||
|
||||
|
||||
ZoneGrowableArray<Function*>* ClassTable::GetOverridesOf(
|
||||
const Function& function) const {
|
||||
ASSERT(!function.IsNull());
|
||||
ASSERT(function.IsDynamicFunction());
|
||||
const Class& function_owner = Class::Handle(function.Owner());
|
||||
const String& function_name = String::Handle(function.name());
|
||||
ZoneGrowableArray<intptr_t>* cids = new ZoneGrowableArray<intptr_t>();
|
||||
CollectSubclassIds(cids, function_owner);
|
||||
return GetNamedInstanceFunctionsOf(*cids, function_name);
|
||||
}
|
||||
|
||||
} // namespace dart
|
||||
|
|
|
@ -12,8 +12,11 @@
|
|||
namespace dart {
|
||||
|
||||
class Class;
|
||||
class Function;
|
||||
template <typename T> class ZoneGrowableArray;
|
||||
class ObjectPointerVisitor;
|
||||
class RawClass;
|
||||
class String;
|
||||
|
||||
class ClassTable {
|
||||
public:
|
||||
|
@ -35,6 +38,22 @@ class ClassTable {
|
|||
|
||||
void Print();
|
||||
|
||||
// Returns an array containing the cids of the direct and indirect subclasses
|
||||
// of the class given by its cid.
|
||||
// Must not be called for kDartObjectCid.
|
||||
ZoneGrowableArray<intptr_t>* GetSubclassIdsOf(intptr_t cid) const;
|
||||
|
||||
// Returns an array containing instance functions of the given name and
|
||||
// belonging to the classes given by their cids.
|
||||
// Cids must not contain kDartObjectCid.
|
||||
ZoneGrowableArray<Function*>* GetNamedInstanceFunctionsOf(
|
||||
const ZoneGrowableArray<intptr_t>& cids,
|
||||
const String& function_name) const;
|
||||
|
||||
// Returns an array of functions overriding the given function.
|
||||
// Must not be called for a function of class Object.
|
||||
ZoneGrowableArray<Function*>* GetOverridesOf(const Function& function) const;
|
||||
|
||||
static intptr_t table_offset() {
|
||||
return OFFSET_OF(ClassTable, table_);
|
||||
}
|
||||
|
|
126
runtime/vm/class_table_test.cc
Normal file
126
runtime/vm/class_table_test.cc
Normal file
|
@ -0,0 +1,126 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
#include "platform/assert.h"
|
||||
#include "vm/class_finalizer.h"
|
||||
#include "vm/class_table.h"
|
||||
#include "vm/dart_entry.h"
|
||||
#include "vm/globals.h"
|
||||
#include "vm/symbols.h"
|
||||
#include "vm/unit_test.h"
|
||||
|
||||
namespace dart {
|
||||
|
||||
TEST_CASE(ClassHierarchyAnalysis) {
|
||||
const char* kScriptChars =
|
||||
"class A {"
|
||||
" foo() { }"
|
||||
" bar() { }"
|
||||
"}\n"
|
||||
"class B extends A {"
|
||||
"}\n"
|
||||
"class C extends B {"
|
||||
" foo() { }"
|
||||
"}\n"
|
||||
"class D extends A {"
|
||||
" foo() { }"
|
||||
" bar() { }"
|
||||
"}\n";
|
||||
|
||||
TestCase::LoadTestScript(kScriptChars, NULL);
|
||||
EXPECT(ClassFinalizer::FinalizePendingClasses());
|
||||
const String& name = String::Handle(String::New(TestCase::url()));
|
||||
const Library& lib = Library::Handle(Library::LookupLibrary(name));
|
||||
EXPECT(!lib.IsNull());
|
||||
|
||||
const Class& class_a = Class::Handle(
|
||||
lib.LookupClass(String::Handle(Symbols::New("A"))));
|
||||
EXPECT(!class_a.IsNull());
|
||||
const intptr_t class_a_id = class_a.id();
|
||||
|
||||
const Class& class_b = Class::Handle(
|
||||
lib.LookupClass(String::Handle(Symbols::New("B"))));
|
||||
EXPECT(!class_b.IsNull());
|
||||
const intptr_t class_b_id = class_b.id();
|
||||
|
||||
const Class& class_c = Class::Handle(
|
||||
lib.LookupClass(String::Handle(Symbols::New("C"))));
|
||||
EXPECT(!class_c.IsNull());
|
||||
const intptr_t class_c_id = class_c.id();
|
||||
|
||||
const Class& class_d = Class::Handle(
|
||||
lib.LookupClass(String::Handle(Symbols::New("D"))));
|
||||
EXPECT(!class_d.IsNull());
|
||||
const intptr_t class_d_id = class_d.id();
|
||||
|
||||
const String& function_foo_name = String::Handle(String::New("foo"));
|
||||
const String& function_bar_name = String::Handle(String::New("bar"));
|
||||
|
||||
const Function& class_a_foo =
|
||||
Function::Handle(class_a.LookupDynamicFunction(function_foo_name));
|
||||
EXPECT(!class_a_foo.IsNull());
|
||||
|
||||
const Function& class_a_bar =
|
||||
Function::Handle(class_a.LookupDynamicFunction(function_bar_name));
|
||||
EXPECT(!class_a_bar.IsNull());
|
||||
|
||||
const Function& class_c_foo =
|
||||
Function::Handle(class_c.LookupDynamicFunction(function_foo_name));
|
||||
EXPECT(!class_c_foo.IsNull());
|
||||
|
||||
const Function& class_d_foo =
|
||||
Function::Handle(class_d.LookupDynamicFunction(function_foo_name));
|
||||
EXPECT(!class_d_foo.IsNull());
|
||||
|
||||
const Function& class_d_bar =
|
||||
Function::Handle(class_d.LookupDynamicFunction(function_bar_name));
|
||||
EXPECT(!class_d_bar.IsNull());
|
||||
|
||||
ClassTable* class_table = Isolate::Current()->class_table();
|
||||
EXPECT(class_table != NULL);
|
||||
|
||||
ZoneGrowableArray<intptr_t>* a_subclass_ids =
|
||||
class_table->GetSubclassIdsOf(class_a_id);
|
||||
EXPECT_EQ(3, a_subclass_ids->length());
|
||||
EXPECT_EQ(class_b_id, (*a_subclass_ids)[0]);
|
||||
EXPECT_EQ(class_c_id, (*a_subclass_ids)[1]);
|
||||
EXPECT_EQ(class_d_id, (*a_subclass_ids)[2]);
|
||||
ZoneGrowableArray<intptr_t>* b_subclass_ids =
|
||||
class_table->GetSubclassIdsOf(class_b_id);
|
||||
EXPECT_EQ(1, b_subclass_ids->length());
|
||||
EXPECT_EQ(class_c_id, (*b_subclass_ids)[0]);
|
||||
ZoneGrowableArray<intptr_t>* c_subclass_ids =
|
||||
class_table->GetSubclassIdsOf(class_c_id);
|
||||
EXPECT_EQ(0, c_subclass_ids->length());
|
||||
ZoneGrowableArray<intptr_t>* d_subclass_ids =
|
||||
class_table->GetSubclassIdsOf(class_d_id);
|
||||
EXPECT_EQ(0, d_subclass_ids->length());
|
||||
|
||||
ZoneGrowableArray<Function*>* foos =
|
||||
class_table->GetNamedInstanceFunctionsOf(*a_subclass_ids,
|
||||
function_foo_name);
|
||||
EXPECT_EQ(2, foos->length());
|
||||
EXPECT_EQ(class_c_foo.raw(), (*foos)[0]->raw());
|
||||
EXPECT_EQ(class_d_foo.raw(), (*foos)[1]->raw());
|
||||
|
||||
ZoneGrowableArray<Function*>* class_a_foo_overrides =
|
||||
class_table->GetOverridesOf(class_a_foo);
|
||||
EXPECT_EQ(2, class_a_foo_overrides->length());
|
||||
EXPECT_EQ(class_c_foo.raw(), (*class_a_foo_overrides)[0]->raw());
|
||||
EXPECT_EQ(class_d_foo.raw(), (*class_a_foo_overrides)[1]->raw());
|
||||
|
||||
ZoneGrowableArray<Function*>* bars =
|
||||
class_table->GetNamedInstanceFunctionsOf(*a_subclass_ids,
|
||||
function_bar_name);
|
||||
EXPECT_EQ(1, bars->length());
|
||||
EXPECT_EQ(class_d_bar.raw(), (*bars)[0]->raw());
|
||||
|
||||
ZoneGrowableArray<Function*>* class_a_bar_overrides =
|
||||
class_table->GetOverridesOf(class_a_bar);
|
||||
EXPECT_EQ(1, class_a_bar_overrides->length());
|
||||
EXPECT_EQ(class_d_bar.raw(), (*class_a_bar_overrides)[0]->raw());
|
||||
}
|
||||
|
||||
} // namespace dart
|
||||
|
|
@ -1857,6 +1857,28 @@ void Class::set_interfaces(const Array& value) const {
|
|||
}
|
||||
|
||||
|
||||
void Class::AddDirectSubclass(const Class& subclass) const {
|
||||
ASSERT(!subclass.IsNull());
|
||||
ASSERT(subclass.SuperClass() == raw());
|
||||
// Do not keep track of the direct subclasses of class Object.
|
||||
// TODO(regis): Replace assert below with ASSERT(id() != kDartObjectCid).
|
||||
ASSERT(!IsObjectClass());
|
||||
GrowableObjectArray& direct_subclasses =
|
||||
GrowableObjectArray::Handle(raw_ptr()->direct_subclasses_);
|
||||
if (direct_subclasses.IsNull()) {
|
||||
direct_subclasses = GrowableObjectArray::New(4, Heap::kOld);
|
||||
StorePointer(&raw_ptr()->direct_subclasses_, direct_subclasses.raw());
|
||||
}
|
||||
#if defined(DEBUG)
|
||||
// Verify that the same class is not added twice.
|
||||
for (intptr_t i = 0; i < direct_subclasses.Length(); i++) {
|
||||
ASSERT(direct_subclasses.At(i) != subclass.raw());
|
||||
}
|
||||
#endif
|
||||
direct_subclasses.Add(subclass);
|
||||
}
|
||||
|
||||
|
||||
RawArray* Class::constants() const {
|
||||
return raw_ptr()->constants_;
|
||||
}
|
||||
|
|
|
@ -553,6 +553,13 @@ class Class : public Object {
|
|||
return OFFSET_OF(RawClass, interfaces_);
|
||||
}
|
||||
|
||||
// Returns the list of classes having this class as direct superclass.
|
||||
RawGrowableObjectArray* direct_subclasses() const {
|
||||
return raw_ptr()->direct_subclasses_;
|
||||
}
|
||||
void AddDirectSubclass(const Class& subclass) const;
|
||||
// TODO(regis): Implement RemoveDirectSubclass for class unloading support.
|
||||
|
||||
// Check if this class represents the class of null.
|
||||
bool IsNullClass() const { return raw() == Object::null_class(); }
|
||||
|
||||
|
|
|
@ -408,6 +408,7 @@ class RawClass : public RawObject {
|
|||
RawArray* fields_;
|
||||
RawGrowableObjectArray* closure_functions_; // Local functions and literals.
|
||||
RawArray* interfaces_; // Array of AbstractType.
|
||||
RawGrowableObjectArray* direct_subclasses_; // Array of Class.
|
||||
RawScript* script_;
|
||||
RawLibrary* library_;
|
||||
RawTypeArguments* type_parameters_; // Array of TypeParameter.
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
'class_finalizer_test.cc',
|
||||
'class_table.cc',
|
||||
'class_table.h',
|
||||
'class_table_test.cc',
|
||||
'code_descriptors.cc',
|
||||
'code_descriptors.h',
|
||||
'code_descriptors_test.cc',
|
||||
|
|
Loading…
Reference in a new issue