Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: tools/gn/scope.cc

Issue 2509333003: Change GN to disallow reading args defined in the same declare_args() call. (Closed)
Patch Set: update w/ review feedback Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« tools/gn/functions.cc ('K') | « tools/gn/scope.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
81 // First check for programmatically-provided values. 88 // First check for programmatically-provided values.
82 for (auto* provider : programmatic_providers_) { 89 for (auto* provider : programmatic_providers_) {
83 const Value* v = provider->GetProgrammaticValue(ident); 90 const Value* v = provider->GetProgrammaticValue(ident);
84 if (v) 91 if (v) {
92 *found_in_scope = nullptr;
85 return v; 93 return v;
94 }
86 } 95 }
87 96
88 RecordMap::iterator found = values_.find(ident); 97 RecordMap::iterator found = values_.find(ident);
89 if (found != values_.end()) { 98 if (found != values_.end()) {
90 if (counts_as_used) 99 if (counts_as_used)
91 found->second.used = true; 100 found->second.used = true;
101 *found_in_scope = this;
92 return &found->second.value; 102 return &found->second.value;
93 } 103 }
94 104
95 // Search in the parent scope. 105 // Search in the parent scope.
96 if (const_containing_) 106 if (const_containing_)
97 return const_containing_->GetValue(ident); 107 return const_containing_->GetValueWithScope(ident, found_in_scope);
98 if (mutable_containing_) 108 if (mutable_containing_) {
99 return mutable_containing_->GetValue(ident, counts_as_used); 109 return mutable_containing_->GetValueWithScope(ident, counts_as_used,
110 found_in_scope);
111 }
100 return nullptr; 112 return nullptr;
101 } 113 }
102 114
103 Value* Scope::GetMutableValue(const base::StringPiece& ident, 115 Value* Scope::GetMutableValue(const base::StringPiece& ident,
104 SearchNested search_mode, 116 SearchNested search_mode,
105 bool counts_as_used) { 117 bool counts_as_used) {
106 // Don't do programmatic values, which are not mutable. 118 // Don't do programmatic values, which are not mutable.
107 RecordMap::iterator found = values_.find(ident); 119 RecordMap::iterator found = values_.find(ident);
108 if (found != values_.end()) { 120 if (found != values_.end()) {
109 if (counts_as_used) 121 if (counts_as_used)
(...skipping 14 matching lines...) Expand all
124 if (found != values_.end()) 136 if (found != values_.end())
125 return found->first; 137 return found->first;
126 138
127 // Search in parent scope. 139 // Search in parent scope.
128 if (containing()) 140 if (containing())
129 return containing()->GetStorageKey(ident); 141 return containing()->GetStorageKey(ident);
130 return base::StringPiece(); 142 return base::StringPiece();
131 } 143 }
132 144
133 const Value* Scope::GetValue(const base::StringPiece& ident) const { 145 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 {
134 RecordMap::const_iterator found = values_.find(ident); 152 RecordMap::const_iterator found = values_.find(ident);
135 if (found != values_.end()) 153 if (found != values_.end()) {
154 *found_in_scope = this;
136 return &found->second.value; 155 return &found->second.value;
156 }
137 if (containing()) 157 if (containing())
138 return containing()->GetValue(ident); 158 return containing()->GetValueWithScope(ident, found_in_scope);
139 return nullptr; 159 return nullptr;
140 } 160 }
141 161
142 Value* Scope::SetValue(const base::StringPiece& ident, 162 Value* Scope::SetValue(const base::StringPiece& ident,
143 Value v, 163 Value v,
144 const ParseNode* set_node) { 164 const ParseNode* set_node) {
145 Record& r = values_[ident]; // Clears any existing value. 165 Record& r = values_[ident]; // Clears any existing value.
146 r.value = std::move(v); 166 r.value = std::move(v);
147 r.value.set_origin(set_node); 167 r.value.set_origin(set_node);
148 return &r.value; 168 return &r.value;
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 return false; 539 return false;
520 for (const auto& pair : a) { 540 for (const auto& pair : a) {
521 const auto& found_b = b.find(pair.first); 541 const auto& found_b = b.find(pair.first);
522 if (found_b == b.end()) 542 if (found_b == b.end())
523 return false; // Item in 'a' but not 'b'. 543 return false; // Item in 'a' but not 'b'.
524 if (pair.second.value != found_b->second.value) 544 if (pair.second.value != found_b->second.value)
525 return false; // Values for variable in 'a' and 'b' are different. 545 return false; // Values for variable in 'a' and 'b' are different.
526 } 546 }
527 return true; 547 return true;
528 } 548 }
OLDNEW
« tools/gn/functions.cc ('K') | « tools/gn/scope.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698