OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef V8_CCTEST_SCOPE_TEST_HELPER_H_ |
| 6 #define V8_CCTEST_SCOPE_TEST_HELPER_H_ |
| 7 |
| 8 #include "src/ast/scopes.h" |
| 9 #include "src/ast/variables.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 |
| 14 class ScopeTestHelper { |
| 15 public: |
| 16 static bool MustAllocateInContext(Variable* var) { |
| 17 return var->scope()->MustAllocateInContext(var); |
| 18 } |
| 19 |
| 20 // True if the scope is and its entire subscope tree are hidden. |
| 21 static bool ScopeTreeIsHidden(Scope* scope) { |
| 22 if (!scope->is_hidden()) { |
| 23 return false; |
| 24 } |
| 25 for (Scope* inner = scope->inner_scope(); inner != nullptr; |
| 26 inner = inner->sibling()) { |
| 27 if (!ScopeTreeIsHidden(inner)) { |
| 28 return false; |
| 29 } |
| 30 } |
| 31 return true; |
| 32 } |
| 33 |
| 34 static void CompareScopeToData(Scope* scope, const PreParsedScopeData* data, |
| 35 size_t& index) { |
| 36 CHECK_EQ(data->backing_store_[index++], scope->scope_type()); |
| 37 CHECK_EQ(data->backing_store_[index++], scope->start_position()); |
| 38 CHECK_EQ(data->backing_store_[index++], scope->end_position()); |
| 39 |
| 40 int inner_scope_count = 0; |
| 41 for (Scope* inner = scope->inner_scope(); inner != nullptr; |
| 42 inner = inner->sibling()) { |
| 43 // FIXME(marja): This is probably not the right condition for knowing what |
| 44 // scopes are present in the preparse data. |
| 45 if (!ScopeTreeIsHidden(inner)) { |
| 46 ++inner_scope_count; |
| 47 } |
| 48 } |
| 49 CHECK_EQ(data->backing_store_[index++], inner_scope_count); |
| 50 |
| 51 int variable_count = 0; |
| 52 for (Variable* local : scope->locals_) { |
| 53 if (local->mode() == VAR || local->mode() == LET || |
| 54 local->mode() == CONST) { |
| 55 ++variable_count; |
| 56 } |
| 57 } |
| 58 |
| 59 CHECK_EQ(data->backing_store_[index++], variable_count); |
| 60 |
| 61 for (Variable* local : scope->locals_) { |
| 62 if (local->mode() == VAR || local->mode() == LET || |
| 63 local->mode() == CONST) { |
| 64 #ifdef DEBUG |
| 65 const AstRawString* local_name = local->raw_name(); |
| 66 int name_length = data->backing_store_[index++]; |
| 67 CHECK_EQ(name_length, local_name->length()); |
| 68 for (int i = 0; i < name_length; ++i) { |
| 69 CHECK_EQ(data->backing_store_[index++], local_name->raw_data()[i]); |
| 70 } |
| 71 #endif |
| 72 CHECK_EQ(data->backing_store_[index++], local->location()); |
| 73 CHECK_EQ(data->backing_store_[index++], local->maybe_assigned()); |
| 74 } |
| 75 } |
| 76 |
| 77 for (Scope* inner = scope->inner_scope(); inner != nullptr; |
| 78 inner = inner->sibling()) { |
| 79 if (!ScopeTreeIsHidden(inner)) { |
| 80 CompareScopeToData(inner, data, index); |
| 81 } |
| 82 } |
| 83 } |
| 84 }; |
| 85 } // namespace internal |
| 86 } // namespace v8 |
| 87 |
| 88 #endif // V8_CCTEST_SCOPE_TEST_HELPER_H_ |
OLD | NEW |