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/stl_util.h" | 8 #include "base/stl_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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 item_collector_(nullptr) { | 54 item_collector_(nullptr) { |
55 } | 55 } |
56 | 56 |
57 Scope::~Scope() { | 57 Scope::~Scope() { |
58 STLDeleteContainerPairSecondPointers(target_defaults_.begin(), | 58 STLDeleteContainerPairSecondPointers(target_defaults_.begin(), |
59 target_defaults_.end()); | 59 target_defaults_.end()); |
60 } | 60 } |
61 | 61 |
62 const Value* Scope::GetValue(const base::StringPiece& ident, | 62 const Value* Scope::GetValue(const base::StringPiece& ident, |
63 bool counts_as_used) { | 63 bool counts_as_used) { |
64 // First check for programatically-provided values. | 64 // First check for programmatically-provided values. |
65 for (const auto& provider : programmatic_providers_) { | 65 for (const auto& provider : programmatic_providers_) { |
66 const Value* v = provider->GetProgrammaticValue(ident); | 66 const Value* v = provider->GetProgrammaticValue(ident); |
67 if (v) | 67 if (v) |
68 return v; | 68 return v; |
69 } | 69 } |
70 | 70 |
71 RecordMap::iterator found = values_.find(ident); | 71 RecordMap::iterator found = values_.find(ident); |
72 if (found != values_.end()) { | 72 if (found != values_.end()) { |
73 if (counts_as_used) | 73 if (counts_as_used) |
74 found->second.used = true; | 74 found->second.used = true; |
75 return &found->second.value; | 75 return &found->second.value; |
76 } | 76 } |
77 | 77 |
78 // Search in the parent scope. | 78 // Search in the parent scope. |
79 if (const_containing_) | 79 if (const_containing_) |
80 return const_containing_->GetValue(ident); | 80 return const_containing_->GetValue(ident); |
81 if (mutable_containing_) | 81 if (mutable_containing_) |
82 return mutable_containing_->GetValue(ident, counts_as_used); | 82 return mutable_containing_->GetValue(ident, counts_as_used); |
83 return nullptr; | 83 return nullptr; |
84 } | 84 } |
85 | 85 |
86 Value* Scope::GetMutableValue(const base::StringPiece& ident, | 86 Value* Scope::GetMutableValue(const base::StringPiece& ident, |
87 bool counts_as_used) { | 87 bool counts_as_used) { |
88 // Don't do programatic values, which are not mutable. | 88 // Don't do programmatic values, which are not mutable. |
89 RecordMap::iterator found = values_.find(ident); | 89 RecordMap::iterator found = values_.find(ident); |
90 if (found != values_.end()) { | 90 if (found != values_.end()) { |
91 if (counts_as_used) | 91 if (counts_as_used) |
92 found->second.used = true; | 92 found->second.used = true; |
93 return &found->second.value; | 93 return &found->second.value; |
94 } | 94 } |
95 | 95 |
96 // Search in the parent mutable scope, but not const one. | 96 // Search in the parent mutable scope, but not const one. |
97 if (mutable_containing_) | 97 if (mutable_containing_) |
98 return mutable_containing_->GetMutableValue(ident, counts_as_used); | 98 return mutable_containing_->GetMutableValue(ident, counts_as_used); |
(...skipping 10 matching lines...) Expand all Loading... |
109 if (containing()) { | 109 if (containing()) { |
110 const Value* in_containing = containing()->GetValue(ident); | 110 const Value* in_containing = containing()->GetValue(ident); |
111 if (in_containing) { | 111 if (in_containing) { |
112 // Promote to current scope. | 112 // Promote to current scope. |
113 return SetValue(ident, *in_containing, set_node); | 113 return SetValue(ident, *in_containing, set_node); |
114 } | 114 } |
115 } | 115 } |
116 return nullptr; | 116 return nullptr; |
117 } | 117 } |
118 | 118 |
| 119 base::StringPiece Scope::GetStorageKey(const base::StringPiece& ident) const { |
| 120 RecordMap::const_iterator found = values_.find(ident); |
| 121 if (found != values_.end()) |
| 122 return found->first; |
| 123 |
| 124 // Search in parent scope. |
| 125 if (containing()) |
| 126 return containing()->GetStorageKey(ident); |
| 127 return base::StringPiece(); |
| 128 } |
| 129 |
119 const Value* Scope::GetValue(const base::StringPiece& ident) const { | 130 const Value* Scope::GetValue(const base::StringPiece& ident) const { |
120 RecordMap::const_iterator found = values_.find(ident); | 131 RecordMap::const_iterator found = values_.find(ident); |
121 if (found != values_.end()) | 132 if (found != values_.end()) |
122 return &found->second.value; | 133 return &found->second.value; |
123 if (containing()) | 134 if (containing()) |
124 return containing()->GetValue(ident); | 135 return containing()->GetValue(ident); |
125 return nullptr; | 136 return nullptr; |
126 } | 137 } |
127 | 138 |
128 Value* Scope::SetValue(const base::StringPiece& ident, | 139 Value* Scope::SetValue(const base::StringPiece& ident, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 184 |
174 void Scope::MarkUsed(const base::StringPiece& ident) { | 185 void Scope::MarkUsed(const base::StringPiece& ident) { |
175 RecordMap::iterator found = values_.find(ident); | 186 RecordMap::iterator found = values_.find(ident); |
176 if (found == values_.end()) { | 187 if (found == values_.end()) { |
177 NOTREACHED(); | 188 NOTREACHED(); |
178 return; | 189 return; |
179 } | 190 } |
180 found->second.used = true; | 191 found->second.used = true; |
181 } | 192 } |
182 | 193 |
| 194 void Scope::MarkAllUsed() { |
| 195 for (auto& cur : values_) |
| 196 cur.second.used = true; |
| 197 } |
| 198 |
183 void Scope::MarkUnused(const base::StringPiece& ident) { | 199 void Scope::MarkUnused(const base::StringPiece& ident) { |
184 RecordMap::iterator found = values_.find(ident); | 200 RecordMap::iterator found = values_.find(ident); |
185 if (found == values_.end()) { | 201 if (found == values_.end()) { |
186 NOTREACHED(); | 202 NOTREACHED(); |
187 return; | 203 return; |
188 } | 204 } |
189 found->second.used = false; | 205 found->second.used = false; |
190 } | 206 } |
191 | 207 |
192 bool Scope::IsSetButUnused(const base::StringPiece& ident) const { | 208 bool Scope::IsSetButUnused(const base::StringPiece& ident) const { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 err->AppendSubErr(Err(pair.second.value, "defined here.", | 264 err->AppendSubErr(Err(pair.second.value, "defined here.", |
249 "Which would clobber the one in your current scope")); | 265 "Which would clobber the one in your current scope")); |
250 err->AppendSubErr(Err(*existing_value, "defined here.", | 266 err->AppendSubErr(Err(*existing_value, "defined here.", |
251 "Executing " + desc_string + " should not conflict with anything " | 267 "Executing " + desc_string + " should not conflict with anything " |
252 "in the current\nscope unless the values are identical.")); | 268 "in the current\nscope unless the values are identical.")); |
253 return false; | 269 return false; |
254 } | 270 } |
255 } | 271 } |
256 dest->values_[pair.first] = pair.second; | 272 dest->values_[pair.first] = pair.second; |
257 | 273 |
258 if (options.mark_used) | 274 if (options.mark_dest_used) |
259 dest->MarkUsed(pair.first); | 275 dest->MarkUsed(pair.first); |
260 } | 276 } |
261 | 277 |
262 // Target defaults are owning pointers. | 278 // Target defaults are owning pointers. |
263 for (const auto& pair : target_defaults_) { | 279 for (const auto& pair : target_defaults_) { |
264 if (!options.clobber_existing) { | 280 if (!options.clobber_existing) { |
265 if (dest->GetTargetDefaults(pair.first)) { | 281 if (dest->GetTargetDefaults(pair.first)) { |
266 // TODO(brettw) it would be nice to know the origin of a | 282 // TODO(brettw) it would be nice to know the origin of a |
267 // set_target_defaults so we can give locations for the colliding target | 283 // set_target_defaults so we can give locations for the colliding target |
268 // defaults. | 284 // defaults. |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 } | 484 } |
469 | 485 |
470 void Scope::AddProvider(ProgrammaticProvider* p) { | 486 void Scope::AddProvider(ProgrammaticProvider* p) { |
471 programmatic_providers_.insert(p); | 487 programmatic_providers_.insert(p); |
472 } | 488 } |
473 | 489 |
474 void Scope::RemoveProvider(ProgrammaticProvider* p) { | 490 void Scope::RemoveProvider(ProgrammaticProvider* p) { |
475 DCHECK(programmatic_providers_.find(p) != programmatic_providers_.end()); | 491 DCHECK(programmatic_providers_.find(p) != programmatic_providers_.end()); |
476 programmatic_providers_.erase(p); | 492 programmatic_providers_.erase(p); |
477 } | 493 } |
OLD | NEW |