| 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 | |
| 9 | namespace dart { |
| 10 | |
| 11 | ISOLATE_UNIT_TEST_CASE(LocalScope) { |
| 12 | // Allocate a couple of local variables first. |
| 13 | const Type& dynamic_type = Type::ZoneHandle(ptr: Type::DynamicType()); |
| 14 | const String& a = String::ZoneHandle(ptr: Symbols::New(thread, cstr: "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(ptr: Symbols::New(thread, cstr: "b" )); |
| 20 | LocalVariable* var_b = new LocalVariable( |
| 21 | TokenPosition::kNoSource, TokenPosition::kNoSource, b, dynamic_type); |
| 22 | const String& c = String::ZoneHandle(ptr: Symbols::New(thread, cstr: "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( |
| 55 | a, LocalVariable::kNoKernelOffset)); |
| 56 | EXPECT_EQ(var_a, inner_scope1->LookupVariable( |
| 57 | a, LocalVariable::kNoKernelOffset, true)); |
| 58 | EXPECT(outer_scope->LocalLookupVariable(b, LocalVariable::kNoKernelOffset) == |
| 59 | nullptr); |
| 60 | EXPECT(inner_scope1->LocalLookupVariable(c, LocalVariable::kNoKernelOffset) == |
| 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( |
| 77 | a, LocalVariable::kNoKernelOffset, true)); |
| 78 | EXPECT(inner_scope1->LookupVariable(a, LocalVariable::kNoKernelOffset, |
| 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 | // } |
| 93 | EXPECT(inner_scope2->LocalLookupVariable(a, LocalVariable::kNoKernelOffset) == |
| 94 | nullptr); |
| 95 | EXPECT(inner_scope2->AddVariable(var_a)); |
| 96 | EXPECT_EQ(var_a, inner_scope2->LocalLookupVariable( |
| 97 | a, LocalVariable::kNoKernelOffset)); |
| 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 |
| 118 | |