OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/scope.h" | 5 #include "tools/gn/scope.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "tools/gn/parse_tree.h" | 9 #include "tools/gn/parse_tree.h" |
10 #include "tools/gn/template.h" | 10 #include "tools/gn/template.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 mutable_containing_ = nullptr; | 71 mutable_containing_ = nullptr; |
72 } | 72 } |
73 | 73 |
74 bool Scope::HasValues(SearchNested search_nested) const { | 74 bool Scope::HasValues(SearchNested search_nested) const { |
75 DCHECK(search_nested == SEARCH_CURRENT); | 75 DCHECK(search_nested == SEARCH_CURRENT); |
76 return !values_.empty(); | 76 return !values_.empty(); |
77 } | 77 } |
78 | 78 |
79 const Value* Scope::GetValue(const base::StringPiece& ident, | 79 const Value* Scope::GetValue(const base::StringPiece& ident, |
80 bool counts_as_used) { | 80 bool counts_as_used) { |
81 const Scope* found_in_scope = nullptr; | |
82 return GetValueWithScope(ident, counts_as_used, &found_in_scope); | |
83 } | |
84 | |
85 const Value* Scope::GetValueWithScope(const base::StringPiece& ident, | |
86 bool counts_as_used, | |
87 const Scope** found_in_scope) { | |
88 // First check for programmatically-provided values. | 81 // First check for programmatically-provided values. |
89 for (auto* provider : programmatic_providers_) { | 82 for (auto* provider : programmatic_providers_) { |
90 const Value* v = provider->GetProgrammaticValue(ident); | 83 const Value* v = provider->GetProgrammaticValue(ident); |
91 if (v) { | 84 if (v) |
92 *found_in_scope = nullptr; | |
93 return v; | 85 return v; |
94 } | |
95 } | 86 } |
96 | 87 |
97 RecordMap::iterator found = values_.find(ident); | 88 RecordMap::iterator found = values_.find(ident); |
98 if (found != values_.end()) { | 89 if (found != values_.end()) { |
99 if (counts_as_used) | 90 if (counts_as_used) |
100 found->second.used = true; | 91 found->second.used = true; |
101 *found_in_scope = this; | |
102 return &found->second.value; | 92 return &found->second.value; |
103 } | 93 } |
104 | 94 |
105 // Search in the parent scope. | 95 // Search in the parent scope. |
106 if (const_containing_) | 96 if (const_containing_) |
107 return const_containing_->GetValueWithScope(ident, found_in_scope); | 97 return const_containing_->GetValue(ident); |
108 if (mutable_containing_) { | 98 if (mutable_containing_) |
109 return mutable_containing_->GetValueWithScope(ident, counts_as_used, | 99 return mutable_containing_->GetValue(ident, counts_as_used); |
110 found_in_scope); | |
111 } | |
112 return nullptr; | 100 return nullptr; |
113 } | 101 } |
114 | 102 |
115 Value* Scope::GetMutableValue(const base::StringPiece& ident, | 103 Value* Scope::GetMutableValue(const base::StringPiece& ident, |
116 SearchNested search_mode, | 104 SearchNested search_mode, |
117 bool counts_as_used) { | 105 bool counts_as_used) { |
118 // Don't do programmatic values, which are not mutable. | 106 // Don't do programmatic values, which are not mutable. |
119 RecordMap::iterator found = values_.find(ident); | 107 RecordMap::iterator found = values_.find(ident); |
120 if (found != values_.end()) { | 108 if (found != values_.end()) { |
121 if (counts_as_used) | 109 if (counts_as_used) |
(...skipping 14 matching lines...) Expand all Loading... |
136 if (found != values_.end()) | 124 if (found != values_.end()) |
137 return found->first; | 125 return found->first; |
138 | 126 |
139 // Search in parent scope. | 127 // Search in parent scope. |
140 if (containing()) | 128 if (containing()) |
141 return containing()->GetStorageKey(ident); | 129 return containing()->GetStorageKey(ident); |
142 return base::StringPiece(); | 130 return base::StringPiece(); |
143 } | 131 } |
144 | 132 |
145 const Value* Scope::GetValue(const base::StringPiece& ident) const { | 133 const Value* Scope::GetValue(const base::StringPiece& ident) const { |
146 const Scope *found_in_scope = nullptr; | |
147 return GetValueWithScope(ident, &found_in_scope); | |
148 } | |
149 | |
150 const Value* Scope::GetValueWithScope(const base::StringPiece& ident, | |
151 const Scope** found_in_scope) const { | |
152 RecordMap::const_iterator found = values_.find(ident); | 134 RecordMap::const_iterator found = values_.find(ident); |
153 if (found != values_.end()) { | 135 if (found != values_.end()) |
154 *found_in_scope = this; | |
155 return &found->second.value; | 136 return &found->second.value; |
156 } | |
157 if (containing()) | 137 if (containing()) |
158 return containing()->GetValueWithScope(ident, found_in_scope); | 138 return containing()->GetValue(ident); |
159 return nullptr; | 139 return nullptr; |
160 } | 140 } |
161 | 141 |
162 Value* Scope::SetValue(const base::StringPiece& ident, | 142 Value* Scope::SetValue(const base::StringPiece& ident, |
163 Value v, | 143 Value v, |
164 const ParseNode* set_node) { | 144 const ParseNode* set_node) { |
165 Record& r = values_[ident]; // Clears any existing value. | 145 Record& r = values_[ident]; // Clears any existing value. |
166 r.value = std::move(v); | 146 r.value = std::move(v); |
167 r.value.set_origin(set_node); | 147 r.value.set_origin(set_node); |
168 return &r.value; | 148 return &r.value; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 return false; | 519 return false; |
540 for (const auto& pair : a) { | 520 for (const auto& pair : a) { |
541 const auto& found_b = b.find(pair.first); | 521 const auto& found_b = b.find(pair.first); |
542 if (found_b == b.end()) | 522 if (found_b == b.end()) |
543 return false; // Item in 'a' but not 'b'. | 523 return false; // Item in 'a' but not 'b'. |
544 if (pair.second.value != found_b->second.value) | 524 if (pair.second.value != found_b->second.value) |
545 return false; // Values for variable in 'a' and 'b' are different. | 525 return false; // Values for variable in 'a' and 'b' are different. |
546 } | 526 } |
547 return true; | 527 return true; |
548 } | 528 } |
OLD | NEW |