| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 : const_containing_(parent), | 59 : const_containing_(parent), |
| 60 mutable_containing_(nullptr), | 60 mutable_containing_(nullptr), |
| 61 settings_(parent->settings()), | 61 settings_(parent->settings()), |
| 62 mode_flags_(0), | 62 mode_flags_(0), |
| 63 item_collector_(nullptr) { | 63 item_collector_(nullptr) { |
| 64 } | 64 } |
| 65 | 65 |
| 66 Scope::~Scope() { | 66 Scope::~Scope() { |
| 67 } | 67 } |
| 68 | 68 |
| 69 void Scope::DetachFromContaining() { |
| 70 const_containing_ = nullptr; |
| 71 mutable_containing_ = nullptr; |
| 72 } |
| 73 |
| 69 const Value* Scope::GetValue(const base::StringPiece& ident, | 74 const Value* Scope::GetValue(const base::StringPiece& ident, |
| 70 bool counts_as_used) { | 75 bool counts_as_used) { |
| 71 // First check for programmatically-provided values. | 76 // First check for programmatically-provided values. |
| 72 for (auto* provider : programmatic_providers_) { | 77 for (auto* provider : programmatic_providers_) { |
| 73 const Value* v = provider->GetProgrammaticValue(ident); | 78 const Value* v = provider->GetProgrammaticValue(ident); |
| 74 if (v) | 79 if (v) |
| 75 return v; | 80 return v; |
| 76 } | 81 } |
| 77 | 82 |
| 78 RecordMap::iterator found = values_.find(ident); | 83 RecordMap::iterator found = values_.find(ident); |
| 79 if (found != values_.end()) { | 84 if (found != values_.end()) { |
| 80 if (counts_as_used) | 85 if (counts_as_used) |
| 81 found->second.used = true; | 86 found->second.used = true; |
| 82 return &found->second.value; | 87 return &found->second.value; |
| 83 } | 88 } |
| 84 | 89 |
| 85 // Search in the parent scope. | 90 // Search in the parent scope. |
| 86 if (const_containing_) | 91 if (const_containing_) |
| 87 return const_containing_->GetValue(ident); | 92 return const_containing_->GetValue(ident); |
| 88 if (mutable_containing_) | 93 if (mutable_containing_) |
| 89 return mutable_containing_->GetValue(ident, counts_as_used); | 94 return mutable_containing_->GetValue(ident, counts_as_used); |
| 90 return nullptr; | 95 return nullptr; |
| 91 } | 96 } |
| 92 | 97 |
| 93 Value* Scope::GetMutableValue(const base::StringPiece& ident, | 98 Value* Scope::GetMutableValue(const base::StringPiece& ident, |
| 99 SearchNested search_mode, |
| 94 bool counts_as_used) { | 100 bool counts_as_used) { |
| 95 // Don't do programmatic values, which are not mutable. | 101 // Don't do programmatic values, which are not mutable. |
| 96 RecordMap::iterator found = values_.find(ident); | 102 RecordMap::iterator found = values_.find(ident); |
| 97 if (found != values_.end()) { | 103 if (found != values_.end()) { |
| 98 if (counts_as_used) | 104 if (counts_as_used) |
| 99 found->second.used = true; | 105 found->second.used = true; |
| 100 return &found->second.value; | 106 return &found->second.value; |
| 101 } | 107 } |
| 102 | 108 |
| 103 // Search in the parent mutable scope, but not const one. | 109 // Search in the parent mutable scope if requested, but not const one. |
| 104 if (mutable_containing_) | 110 if (search_mode == SEARCH_NESTED && mutable_containing_) { |
| 105 return mutable_containing_->GetMutableValue(ident, counts_as_used); | 111 return mutable_containing_->GetMutableValue( |
| 106 return nullptr; | 112 ident, Scope::SEARCH_NESTED, counts_as_used); |
| 107 } | |
| 108 | |
| 109 Value* Scope::GetValueForcedToCurrentScope(const base::StringPiece& ident, | |
| 110 const ParseNode* set_node) { | |
| 111 RecordMap::iterator found = values_.find(ident); | |
| 112 if (found != values_.end()) | |
| 113 return &found->second.value; // Already have in the current scope. | |
| 114 | |
| 115 // Search in the parent scope. | |
| 116 if (containing()) { | |
| 117 const Value* in_containing = containing()->GetValue(ident); | |
| 118 if (in_containing) { | |
| 119 // Promote to current scope. | |
| 120 return SetValue(ident, *in_containing, set_node); | |
| 121 } | |
| 122 } | 113 } |
| 123 return nullptr; | 114 return nullptr; |
| 124 } | 115 } |
| 125 | 116 |
| 126 base::StringPiece Scope::GetStorageKey(const base::StringPiece& ident) const { | 117 base::StringPiece Scope::GetStorageKey(const base::StringPiece& ident) const { |
| 127 RecordMap::const_iterator found = values_.find(ident); | 118 RecordMap::const_iterator found = values_.find(ident); |
| 128 if (found != values_.end()) | 119 if (found != values_.end()) |
| 129 return found->first; | 120 return found->first; |
| 130 | 121 |
| 131 // Search in parent scope. | 122 // Search in parent scope. |
| 132 if (containing()) | 123 if (containing()) |
| 133 return containing()->GetStorageKey(ident); | 124 return containing()->GetStorageKey(ident); |
| 134 return base::StringPiece(); | 125 return base::StringPiece(); |
| 135 } | 126 } |
| 136 | 127 |
| 137 const Value* Scope::GetValue(const base::StringPiece& ident) const { | 128 const Value* Scope::GetValue(const base::StringPiece& ident) const { |
| 138 RecordMap::const_iterator found = values_.find(ident); | 129 RecordMap::const_iterator found = values_.find(ident); |
| 139 if (found != values_.end()) | 130 if (found != values_.end()) |
| 140 return &found->second.value; | 131 return &found->second.value; |
| 141 if (containing()) | 132 if (containing()) |
| 142 return containing()->GetValue(ident); | 133 return containing()->GetValue(ident); |
| 143 return nullptr; | 134 return nullptr; |
| 144 } | 135 } |
| 145 | 136 |
| 146 Value* Scope::SetValue(const base::StringPiece& ident, | 137 Value* Scope::SetValue(const base::StringPiece& ident, |
| 147 const Value& v, | 138 Value v, |
| 148 const ParseNode* set_node) { | 139 const ParseNode* set_node) { |
| 149 Record& r = values_[ident]; // Clears any existing value. | 140 Record& r = values_[ident]; // Clears any existing value. |
| 150 r.value = v; | 141 r.value = std::move(v); |
| 151 r.value.set_origin(set_node); | 142 r.value.set_origin(set_node); |
| 152 return &r.value; | 143 return &r.value; |
| 153 } | 144 } |
| 154 | 145 |
| 155 void Scope::RemoveIdentifier(const base::StringPiece& ident) { | 146 void Scope::RemoveIdentifier(const base::StringPiece& ident) { |
| 156 RecordMap::iterator found = values_.find(ident); | 147 RecordMap::iterator found = values_.find(ident); |
| 157 if (found != values_.end()) | 148 if (found != values_.end()) |
| 158 values_.erase(found); | 149 values_.erase(found); |
| 159 } | 150 } |
| 160 | 151 |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 return false; | 514 return false; |
| 524 for (const auto& pair : a) { | 515 for (const auto& pair : a) { |
| 525 const auto& found_b = b.find(pair.first); | 516 const auto& found_b = b.find(pair.first); |
| 526 if (found_b == b.end()) | 517 if (found_b == b.end()) |
| 527 return false; // Item in 'a' but not 'b'. | 518 return false; // Item in 'a' but not 'b'. |
| 528 if (pair.second.value != found_b->second.value) | 519 if (pair.second.value != found_b->second.value) |
| 529 return false; // Values for variable in 'a' and 'b' are different. | 520 return false; // Values for variable in 'a' and 'b' are different. |
| 530 } | 521 } |
| 531 return true; | 522 return true; |
| 532 } | 523 } |
| OLD | NEW |