Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scopes_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/scopes.h"
6#include "platform/assert.h"
7#include "vm/unit_test.h"
8
9namespace dart {
10
12 // Allocate a couple of local variables first.
13 const Type& dynamic_type = Type::ZoneHandle(Type::DynamicType());
14 const String& a = String::ZoneHandle(Symbols::New(thread, "a"));
15 LocalVariable* var_a = new LocalVariable(
16 TokenPosition::kNoSource, TokenPosition::kNoSource, a, dynamic_type);
17 LocalVariable* inner_var_a = new LocalVariable(
18 TokenPosition::kNoSource, TokenPosition::kNoSource, a, dynamic_type);
19 const String& b = String::ZoneHandle(Symbols::New(thread, "b"));
20 LocalVariable* var_b = new LocalVariable(
21 TokenPosition::kNoSource, TokenPosition::kNoSource, b, dynamic_type);
22 const String& c = String::ZoneHandle(Symbols::New(thread, "c"));
23 LocalVariable* var_c = new LocalVariable(
24 TokenPosition::kNoSource, TokenPosition::kNoSource, c, dynamic_type);
25
26 LocalScope* outer_scope = new LocalScope(nullptr, 0, 0);
27 LocalScope* inner_scope1 = new LocalScope(outer_scope, 0, 0);
28 LocalScope* inner_scope2 = new LocalScope(outer_scope, 0, 0);
29
30 EXPECT(outer_scope->parent() == nullptr);
31 EXPECT_EQ(outer_scope, inner_scope1->parent());
32 EXPECT_EQ(outer_scope, inner_scope2->parent());
33 EXPECT_EQ(inner_scope2, outer_scope->child());
34 EXPECT_EQ(inner_scope1, inner_scope2->sibling());
35 EXPECT(inner_scope1->child() == nullptr);
36 EXPECT(inner_scope2->child() == nullptr);
37
38 // Populate the local scopes as follows:
39 // { // outer_scope
40 // var a;
41 // { // inner_scope1
42 // var b;
43 // }
44 // L: { // inner_scope2
45 // var c;
46 // }
47 // }
48 EXPECT(outer_scope->AddVariable(var_a));
49 EXPECT(inner_scope1->AddVariable(var_b));
50 EXPECT(inner_scope2->AddVariable(var_c));
51 EXPECT(!outer_scope->AddVariable(var_a));
52
53 // Check the simple layout above.
54 EXPECT_EQ(var_a, outer_scope->LocalLookupVariable(
56 EXPECT_EQ(var_a, inner_scope1->LookupVariable(
59 nullptr);
61 nullptr);
62
63 // Modify the local scopes to contain shadowing:
64 // { // outer_scope
65 // var a;
66 // { // inner_scope1
67 // var b;
68 // var a; // inner_var_a
69 // }
70 // { // inner_scope2
71 // var c;
72 // L: ...
73 // }
74 // }
75 EXPECT(inner_scope1->AddVariable(inner_var_a));
76 EXPECT_EQ(inner_var_a, inner_scope1->LookupVariable(
79 true) != var_a);
80
81 // Modify the local scopes with access of an outer scope variable:
82 // { // outer_scope
83 // var a;
84 // { // inner_scope1
85 // var b;
86 // var a; // inner_var_a
87 // }
88 // { // inner_scope2
89 // var c = a;
90 // L: ...
91 // }
92 // }
94 nullptr);
95 EXPECT(inner_scope2->AddVariable(var_a));
96 EXPECT_EQ(var_a, inner_scope2->LocalLookupVariable(
98
99 EXPECT_EQ(1, outer_scope->num_variables());
100 EXPECT_EQ(2, inner_scope1->num_variables());
101 EXPECT_EQ(2, inner_scope2->num_variables());
102
103 // Cannot depend on the order, but we should find the variables.
104 EXPECT(outer_scope->VariableAt(0) == var_a);
105 EXPECT((inner_scope1->VariableAt(0) == inner_var_a) ||
106 (inner_scope1->VariableAt(1) == inner_var_a));
107 EXPECT((inner_scope1->VariableAt(0) == var_b) ||
108 (inner_scope1->VariableAt(1) == var_b));
109 EXPECT((inner_scope2->VariableAt(0) == var_a) ||
110 (inner_scope2->VariableAt(1) == var_a) ||
111 (inner_scope2->VariableAt(2) == var_a));
112 EXPECT((inner_scope2->VariableAt(0) == var_c) ||
113 (inner_scope2->VariableAt(1) == var_c) ||
114 (inner_scope2->VariableAt(2) == var_c));
115}
116
117} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
LocalScope * parent() const
Definition scopes.h:319
LocalScope * sibling() const
Definition scopes.h:321
LocalVariable * VariableAt(intptr_t index) const
Definition scopes.h:398
intptr_t num_variables() const
Definition scopes.h:397
LocalVariable * LookupVariable(const String &name, intptr_t kernel_offset, bool test_only)
Definition scopes.cc:350
bool AddVariable(LocalVariable *variable)
Definition scopes.cc:57
LocalScope * child() const
Definition scopes.h:320
LocalVariable * LocalLookupVariable(const String &name, intptr_t kernel_offset) const
Definition scopes.cc:336
static constexpr intptr_t kNoKernelOffset
Definition scopes.h:77
static Object & ZoneHandle()
Definition object.h:419
static StringPtr New(Thread *thread, const char *cstr)
Definition symbols.h:722
static TypePtr DynamicType()
Definition object.cc:21866
static bool b
struct MyStruct a[10]
#define ISOLATE_UNIT_TEST_CASE(name)
Definition unit_test.h:64