Line data Source code
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 2050 : static bool MustAllocateInContext(Variable* var) {
17 2050 : return var->scope()->MustAllocateInContext(var);
18 : }
19 :
20 : static void AllocateWithoutVariableResolution(Scope* scope) {
21 16440 : scope->AllocateVariablesRecursively();
22 : }
23 :
24 147060 : static void CompareScopes(Scope* baseline, Scope* scope,
25 : bool precise_maybe_assigned) {
26 75048 : CHECK_EQ(baseline->scope_type(), scope->scope_type());
27 75048 : CHECK_IMPLIES(baseline->is_declaration_scope(),
28 : baseline->AsDeclarationScope()->function_kind() ==
29 : scope->AsDeclarationScope()->function_kind());
30 :
31 75048 : if (!ProducedPreParsedScopeData::ScopeNeedsData(baseline)) {
32 : return;
33 : }
34 :
35 110814 : if (scope->is_declaration_scope() &&
36 49440 : scope->AsDeclarationScope()->is_skipped_function()) {
37 : return;
38 : }
39 :
40 36006 : if (baseline->scope_type() == ScopeType::FUNCTION_SCOPE) {
41 22902 : Variable* function = baseline->AsDeclarationScope()->function_var();
42 22902 : if (function != nullptr) {
43 : CompareVariables(function, scope->AsDeclarationScope()->function_var(),
44 2118 : precise_maybe_assigned);
45 : } else {
46 20784 : CHECK_NULL(scope->AsDeclarationScope()->function_var());
47 : }
48 : }
49 :
50 226218 : for (auto baseline_local = baseline->locals()->begin(),
51 : scope_local = scope->locals()->begin();
52 190212 : baseline_local != baseline->locals()->end();
53 : ++baseline_local, ++scope_local) {
54 308412 : if (scope_local->mode() == VAR || scope_local->mode() == LET ||
55 : scope_local->mode() == CONST) {
56 203100 : CompareVariables(*baseline_local, *scope_local, precise_maybe_assigned);
57 : }
58 : }
59 :
60 94614 : for (Scope *baseline_inner = baseline->inner_scope(),
61 58608 : *scope_inner = scope->inner_scope();
62 : scope_inner != nullptr; scope_inner = scope_inner->sibling(),
63 : baseline_inner = baseline_inner->sibling()) {
64 58608 : CompareScopes(baseline_inner, scope_inner, precise_maybe_assigned);
65 : }
66 : }
67 :
68 414672 : static void CompareVariables(Variable* baseline_local, Variable* scope_local,
69 : bool precise_maybe_assigned) {
70 : // Sanity check the variable name. If this fails, the variable order
71 : // is not deterministic.
72 103668 : CHECK_EQ(scope_local->raw_name()->length(),
73 : baseline_local->raw_name()->length());
74 1551120 : for (int i = 0; i < scope_local->raw_name()->length(); ++i) {
75 1447452 : CHECK_EQ(scope_local->raw_name()->raw_data()[i],
76 : baseline_local->raw_name()->raw_data()[i]);
77 : }
78 :
79 103668 : CHECK_EQ(scope_local->location(), baseline_local->location());
80 103668 : if (precise_maybe_assigned) {
81 100770 : CHECK_EQ(scope_local->maybe_assigned(), baseline_local->maybe_assigned());
82 : } else {
83 : STATIC_ASSERT(kMaybeAssigned > kNotAssigned);
84 2898 : CHECK_GE(scope_local->maybe_assigned(), baseline_local->maybe_assigned());
85 : }
86 103668 : }
87 :
88 : // Finds a scope given a start point and directions to it (which inner scope
89 : // to pick).
90 173220 : static Scope* FindScope(Scope* scope, const std::vector<unsigned>& location) {
91 224736 : for (auto n : location) {
92 : scope = scope->inner_scope();
93 107508 : CHECK_NOT_NULL(scope);
94 114606 : while (n-- > 0) {
95 : scope = scope->sibling();
96 7098 : CHECK_NOT_NULL(scope);
97 : }
98 : }
99 58614 : return scope;
100 : }
101 :
102 75804 : static void MarkInnerFunctionsAsSkipped(Scope* scope) {
103 135168 : for (Scope* inner = scope->inner_scope(); inner != nullptr;
104 : inner = inner->sibling()) {
105 93084 : if (inner->scope_type() == ScopeType::FUNCTION_SCOPE &&
106 33720 : !inner->AsDeclarationScope()->is_arrow_scope()) {
107 27258 : inner->AsDeclarationScope()->set_is_skipped_function(true);
108 : }
109 59364 : MarkInnerFunctionsAsSkipped(inner);
110 : }
111 75804 : }
112 : };
113 : } // namespace internal
114 : } // namespace v8
115 :
116 : #endif // V8_CCTEST_SCOPE_TEST_HELPER_H_
|