Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
cha_test.cc
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/compiler/cha.h"
6#include "platform/assert.h"
8#include "vm/globals.h"
9#include "vm/resolver.h"
10#include "vm/symbols.h"
11#include "vm/unit_test.h"
12
13namespace dart {
14
15#define Z (thread->zone())
16
17TEST_CASE(ClassHierarchyAnalysis) {
18 const char* kScriptChars =
19 "class A {"
20 " foo() { }"
21 " bar() { }"
22 "}\n"
23 "class B extends A {"
24 "}\n"
25 "class C extends B {"
26 " foo() { }"
27 "}\n"
28 "class D extends A {"
29 " foo() { }"
30 " bar() { }"
31 "}\n";
32
33 TestCase::LoadTestScript(kScriptChars, nullptr);
34
35 TransitionNativeToVM transition(thread);
39 EXPECT(!lib.IsNull());
40
41 const Class& class_a =
43 EXPECT(!class_a.IsNull());
44 EXPECT(class_a.EnsureIsFinalized(thread) == Error::null());
45
46 const Class& class_b =
48 EXPECT(!class_b.IsNull());
49
50 const Class& class_c =
52 EXPECT(!class_c.IsNull());
53 EXPECT(class_c.EnsureIsFinalized(thread) == Error::null());
54
55 const Class& class_d =
57 EXPECT(!class_d.IsNull());
58 EXPECT(class_d.EnsureIsFinalized(thread) == Error::null());
59
60 const String& function_foo_name = String::Handle(String::New("foo"));
61 const String& function_bar_name = String::Handle(String::New("bar"));
62
63 const Function& class_a_foo = Function::Handle(
64 Resolver::ResolveDynamicFunction(Z, class_a, function_foo_name));
65 EXPECT(!class_a_foo.IsNull());
66
67 const Function& class_a_bar = Function::Handle(
68 Resolver::ResolveDynamicFunction(Z, class_a, function_bar_name));
69 EXPECT(!class_a_bar.IsNull());
70
71 const Function& class_c_foo = Function::Handle(
72 Resolver::ResolveDynamicFunction(Z, class_c, function_foo_name));
73 EXPECT(!class_c_foo.IsNull());
74
75 const Function& class_d_foo = Function::Handle(
76 Resolver::ResolveDynamicFunction(Z, class_d, function_foo_name));
77 EXPECT(!class_d_foo.IsNull());
78
79 const Function& class_d_bar = Function::Handle(
80 Resolver::ResolveDynamicFunction(Z, class_d, function_bar_name));
81 EXPECT(!class_d_bar.IsNull());
82
83 CHA cha(thread);
84
85 EXPECT(cha.HasSubclasses(kInstanceCid));
86 EXPECT(!cha.HasSubclasses(kSmiCid));
88
89 EXPECT(CHA::HasSubclasses(class_a));
90 EXPECT(CHA::HasSubclasses(class_b));
91 EXPECT(!CHA::HasSubclasses(class_c));
92 cha.AddToGuardedClassesForSubclassCount(class_c, /*subclass_count=*/0);
93 EXPECT(!CHA::HasSubclasses(class_d));
94 cha.AddToGuardedClassesForSubclassCount(class_d, /*subclass_count=*/0);
95
96 EXPECT(!cha.IsGuardedClass(class_a.id()));
97 EXPECT(!cha.IsGuardedClass(class_b.id()));
98 EXPECT(cha.IsGuardedClass(class_c.id()));
99 EXPECT(cha.IsGuardedClass(class_d.id()));
100
101 const Class& closure_class =
102 Class::Handle(IsolateGroup::Current()->object_store()->closure_class());
103 EXPECT(!cha.HasSubclasses(closure_class.id()));
104}
105
106} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
#define Z
bool IsGuardedClass(intptr_t cid) const
Definition cha.cc:57
static bool HasSubclasses(const Class &cls)
Definition cha.cc:64
void AddToGuardedClassesForSubclassCount(const Class &cls, intptr_t subclass_count)
Definition cha.cc:40
static bool ProcessPendingClasses()
intptr_t id() const
Definition object.h:1235
ErrorPtr EnsureIsFinalized(Thread *thread) const
Definition object.cc:4979
static IsolateGroup * Current()
Definition isolate.h:534
ClassPtr LookupClass(const String &name) const
Definition object.cc:14152
static LibraryPtr LookupLibrary(Thread *thread, const String &url)
Definition object.cc:14646
static ObjectPtr null()
Definition object.h:433
bool IsNull() const
Definition object.h:363
static Object & Handle()
Definition object.h:407
static FunctionPtr ResolveDynamicFunction(Zone *zone, const Class &receiver_class, const String &function_name)
Definition resolver.cc:189
static StringPtr New(const char *cstr, Heap::Space space=Heap::kNew)
Definition object.cc:23777
static StringPtr New(Thread *thread, const char *cstr)
Definition symbols.h:722
static Dart_Handle LoadTestScript(const char *script, Dart_NativeEntryResolver resolver, const char *lib_uri=RESOLVED_USER_TEST_URI, bool finalize=true, bool allow_compile_errors=false)
Definition unit_test.cc:422
static const char * url()
Definition unit_test.cc:184
const char *const name
@ kNullCid
Definition class_id.h:252
#define TEST_CASE(name)
Definition unit_test.h:85