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
3 // found in the LICENSE file.
5 #include "tools/gn/scope.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/template.h"
14 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies
15 // recursively to all dependent scopes.
16 const unsigned kProcessingBuildConfigFlag
= 1;
17 const unsigned kProcessingImportFlag
= 2;
19 // Returns true if this variable name should be considered private. Private
20 // values start with an underscore, and are not imported from "gni" files
21 // when processing an import.
22 bool IsPrivateVar(const base::StringPiece
& name
) {
23 return name
.empty() || name
[0] == '_';
29 Scope::ProgrammaticProvider::~ProgrammaticProvider() {
30 scope_
->RemoveProvider(this);
33 Scope::Scope(const Settings
* settings
)
34 : const_containing_(nullptr),
35 mutable_containing_(nullptr),
38 item_collector_(nullptr) {
41 Scope::Scope(Scope
* parent
)
42 : const_containing_(nullptr),
43 mutable_containing_(parent
),
44 settings_(parent
->settings()),
46 item_collector_(nullptr) {
49 Scope::Scope(const Scope
* parent
)
50 : const_containing_(parent
),
51 mutable_containing_(nullptr),
52 settings_(parent
->settings()),
54 item_collector_(nullptr) {
58 STLDeleteContainerPairSecondPointers(target_defaults_
.begin(),
59 target_defaults_
.end());
62 const Value
* Scope::GetValue(const base::StringPiece
& ident
,
63 bool counts_as_used
) {
64 // First check for programatically-provided values.
65 for (const auto& provider
: programmatic_providers_
) {
66 const Value
* v
= provider
->GetProgrammaticValue(ident
);
71 RecordMap::iterator found
= values_
.find(ident
);
72 if (found
!= values_
.end()) {
74 found
->second
.used
= true;
75 return &found
->second
.value
;
78 // Search in the parent scope.
79 if (const_containing_
)
80 return const_containing_
->GetValue(ident
);
81 if (mutable_containing_
)
82 return mutable_containing_
->GetValue(ident
, counts_as_used
);
86 Value
* Scope::GetMutableValue(const base::StringPiece
& ident
,
87 bool counts_as_used
) {
88 // Don't do programatic values, which are not mutable.
89 RecordMap::iterator found
= values_
.find(ident
);
90 if (found
!= values_
.end()) {
92 found
->second
.used
= true;
93 return &found
->second
.value
;
96 // Search in the parent mutable scope, but not const one.
97 if (mutable_containing_
)
98 return mutable_containing_
->GetMutableValue(ident
, counts_as_used
);
102 Value
* Scope::GetValueForcedToCurrentScope(const base::StringPiece
& ident
,
103 const ParseNode
* set_node
) {
104 RecordMap::iterator found
= values_
.find(ident
);
105 if (found
!= values_
.end())
106 return &found
->second
.value
; // Already have in the current scope.
108 // Search in the parent scope.
110 const Value
* in_containing
= containing()->GetValue(ident
);
112 // Promote to current scope.
113 return SetValue(ident
, *in_containing
, set_node
);
119 const Value
* Scope::GetValue(const base::StringPiece
& ident
) const {
120 RecordMap::const_iterator found
= values_
.find(ident
);
121 if (found
!= values_
.end())
122 return &found
->second
.value
;
124 return containing()->GetValue(ident
);
128 Value
* Scope::SetValue(const base::StringPiece
& ident
,
130 const ParseNode
* set_node
) {
131 Record
& r
= values_
[ident
]; // Clears any existing value.
133 r
.value
.set_origin(set_node
);
137 void Scope::RemoveIdentifier(const base::StringPiece
& ident
) {
138 RecordMap::iterator found
= values_
.find(ident
);
139 if (found
!= values_
.end())
140 values_
.erase(found
);
143 void Scope::RemovePrivateIdentifiers() {
144 // Do it in two phases to avoid mutating while iterating. Our hash map is
145 // currently backed by several different vendor-specific implementations and
146 // I'm not sure if all of them support mutating while iterating. Since this
147 // is not perf-critical, do the safe thing.
148 std::vector
<base::StringPiece
> to_remove
;
149 for (const auto& cur
: values_
) {
150 if (IsPrivateVar(cur
.first
))
151 to_remove
.push_back(cur
.first
);
154 for (const auto& cur
: to_remove
)
158 bool Scope::AddTemplate(const std::string
& name
, const Template
* templ
) {
159 if (GetTemplate(name
))
161 templates_
[name
] = templ
;
165 const Template
* Scope::GetTemplate(const std::string
& name
) const {
166 TemplateMap::const_iterator found
= templates_
.find(name
);
167 if (found
!= templates_
.end())
168 return found
->second
.get();
170 return containing()->GetTemplate(name
);
174 void Scope::MarkUsed(const base::StringPiece
& ident
) {
175 RecordMap::iterator found
= values_
.find(ident
);
176 if (found
== values_
.end()) {
180 found
->second
.used
= true;
183 void Scope::MarkUnused(const base::StringPiece
& ident
) {
184 RecordMap::iterator found
= values_
.find(ident
);
185 if (found
== values_
.end()) {
189 found
->second
.used
= false;
192 bool Scope::IsSetButUnused(const base::StringPiece
& ident
) const {
193 RecordMap::const_iterator found
= values_
.find(ident
);
194 if (found
!= values_
.end()) {
195 if (!found
->second
.used
) {
202 bool Scope::CheckForUnusedVars(Err
* err
) const {
203 for (const auto& pair
: values_
) {
204 if (!pair
.second
.used
) {
205 std::string help
= "You set the variable \"" + pair
.first
.as_string() +
206 "\" here and it was unused before it went\nout of scope.";
208 const BinaryOpNode
* binary
= pair
.second
.value
.origin()->AsBinaryOp();
209 if (binary
&& binary
->op().type() == Token::EQUAL
) {
210 // Make a nicer error message for normal var sets.
211 *err
= Err(binary
->left()->GetRange(), "Assignment had no effect.",
214 // This will happen for internally-generated variables.
215 *err
= Err(pair
.second
.value
.origin(), "Assignment had no effect.",
224 void Scope::GetCurrentScopeValues(KeyValueMap
* output
) const {
225 for (const auto& pair
: values_
)
226 (*output
)[pair
.first
] = pair
.second
.value
;
229 bool Scope::NonRecursiveMergeTo(Scope
* dest
,
230 const MergeOptions
& options
,
231 const ParseNode
* node_for_err
,
232 const char* desc_for_err
,
235 for (const auto& pair
: values_
) {
236 if (options
.skip_private_vars
&& IsPrivateVar(pair
.first
))
237 continue; // Skip this private var.
239 const Value
& new_value
= pair
.second
.value
;
240 if (!options
.clobber_existing
) {
241 const Value
* existing_value
= dest
->GetValue(pair
.first
);
242 if (existing_value
&& new_value
!= *existing_value
) {
243 // Value present in both the source and the dest.
244 std::string
desc_string(desc_for_err
);
245 *err
= Err(node_for_err
, "Value collision.",
246 "This " + desc_string
+ " contains \"" + pair
.first
.as_string() +
248 err
->AppendSubErr(Err(pair
.second
.value
, "defined here.",
249 "Which would clobber the one in your current scope"));
250 err
->AppendSubErr(Err(*existing_value
, "defined here.",
251 "Executing " + desc_string
+ " should not conflict with anything "
252 "in the current\nscope unless the values are identical."));
256 dest
->values_
[pair
.first
] = pair
.second
;
258 if (options
.mark_used
)
259 dest
->MarkUsed(pair
.first
);
262 // Target defaults are owning pointers.
263 for (const auto& pair
: target_defaults_
) {
264 if (!options
.clobber_existing
) {
265 if (dest
->GetTargetDefaults(pair
.first
)) {
266 // 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
269 std::string
desc_string(desc_for_err
);
270 *err
= Err(node_for_err
, "Target defaults collision.",
271 "This " + desc_string
+ " contains target defaults for\n"
272 "\"" + pair
.first
+ "\" which would clobber one for the\n"
273 "same target type in your current scope. It's unfortunate that I'm "
274 "too stupid\nto tell you the location of where the target defaults "
275 "were set. Usually\nthis happens in the BUILDCONFIG.gn file.");
280 // Be careful to delete any pointer we're about to clobber.
281 Scope
** dest_scope
= &dest
->target_defaults_
[pair
.first
];
284 *dest_scope
= new Scope(settings_
);
285 pair
.second
->NonRecursiveMergeTo(*dest_scope
, options
, node_for_err
,
286 "<SHOULDN'T HAPPEN>", err
);
289 // Sources assignment filter.
290 if (sources_assignment_filter_
) {
291 if (!options
.clobber_existing
) {
292 if (dest
->GetSourcesAssignmentFilter()) {
293 // Sources assignment filter present in both the source and the dest.
294 std::string
desc_string(desc_for_err
);
295 *err
= Err(node_for_err
, "Assignment filter collision.",
296 "The " + desc_string
+ " contains a sources_assignment_filter "
297 "which\nwould clobber the one in your current scope.");
301 dest
->sources_assignment_filter_
.reset(
302 new PatternList(*sources_assignment_filter_
));
306 for (const auto& pair
: templates_
) {
307 if (options
.skip_private_vars
&& IsPrivateVar(pair
.first
))
308 continue; // Skip this private template.
310 if (!options
.clobber_existing
) {
311 const Template
* existing_template
= dest
->GetTemplate(pair
.first
);
312 // Since templates are refcounted, we can check if it's the same one by
313 // comparing pointers.
314 if (existing_template
&& pair
.second
.get() != existing_template
) {
315 // Rule present in both the source and the dest, and they're not the
317 std::string
desc_string(desc_for_err
);
318 *err
= Err(node_for_err
, "Template collision.",
319 "This " + desc_string
+ " contains a template \"" +
321 err
->AppendSubErr(Err(pair
.second
->GetDefinitionRange(),
323 "Which would clobber the one in your current scope"));
324 err
->AppendSubErr(Err(existing_template
->GetDefinitionRange(),
326 "Executing " + desc_string
+ " should not conflict with anything "
327 "in the current\nscope."));
332 // Be careful to delete any pointer we're about to clobber.
333 dest
->templates_
[pair
.first
] = pair
.second
;
339 scoped_ptr
<Scope
> Scope::MakeClosure() const {
340 scoped_ptr
<Scope
> result
;
341 if (const_containing_
) {
342 // We reached the top of the mutable scope stack. The result scope just
343 // references the const scope (which will never change).
344 result
.reset(new Scope(const_containing_
));
345 } else if (mutable_containing_
) {
346 // There are more nested mutable scopes. Recursively go up the stack to
348 result
= mutable_containing_
->MakeClosure();
350 // This is a standalone scope, just copy it.
351 result
.reset(new Scope(settings_
));
354 // Want to clobber since we've flattened some nested scopes, and our parent
355 // scope may have a duplicate value set.
356 MergeOptions options
;
357 options
.clobber_existing
= true;
359 // Add in our variables and we're done.
361 NonRecursiveMergeTo(result
.get(), options
, nullptr, "<SHOULDN'T HAPPEN>",
363 DCHECK(!err
.has_error());
364 return result
.Pass();
367 Scope
* Scope::MakeTargetDefaults(const std::string
& target_type
) {
368 if (GetTargetDefaults(target_type
))
371 Scope
** dest
= &target_defaults_
[target_type
];
373 NOTREACHED(); // Already set.
376 *dest
= new Scope(settings_
);
380 const Scope
* Scope::GetTargetDefaults(const std::string
& target_type
) const {
381 NamedScopeMap::const_iterator found
= target_defaults_
.find(target_type
);
382 if (found
!= target_defaults_
.end())
383 return found
->second
;
385 return containing()->GetTargetDefaults(target_type
);
389 const PatternList
* Scope::GetSourcesAssignmentFilter() const {
390 if (sources_assignment_filter_
)
391 return sources_assignment_filter_
.get();
393 return containing()->GetSourcesAssignmentFilter();
397 void Scope::SetProcessingBuildConfig() {
398 DCHECK((mode_flags_
& kProcessingBuildConfigFlag
) == 0);
399 mode_flags_
|= kProcessingBuildConfigFlag
;
402 void Scope::ClearProcessingBuildConfig() {
403 DCHECK(mode_flags_
& kProcessingBuildConfigFlag
);
404 mode_flags_
&= ~(kProcessingBuildConfigFlag
);
407 bool Scope::IsProcessingBuildConfig() const {
408 if (mode_flags_
& kProcessingBuildConfigFlag
)
411 return containing()->IsProcessingBuildConfig();
415 void Scope::SetProcessingImport() {
416 DCHECK((mode_flags_
& kProcessingImportFlag
) == 0);
417 mode_flags_
|= kProcessingImportFlag
;
420 void Scope::ClearProcessingImport() {
421 DCHECK(mode_flags_
& kProcessingImportFlag
);
422 mode_flags_
&= ~(kProcessingImportFlag
);
425 bool Scope::IsProcessingImport() const {
426 if (mode_flags_
& kProcessingImportFlag
)
429 return containing()->IsProcessingImport();
433 const SourceDir
& Scope::GetSourceDir() const {
434 if (!source_dir_
.is_null())
437 return containing()->GetSourceDir();
441 Scope::ItemVector
* Scope::GetItemCollector() {
443 return item_collector_
;
444 if (mutable_containing())
445 return mutable_containing()->GetItemCollector();
449 void Scope::SetProperty(const void* key
, void* value
) {
451 DCHECK(properties_
.find(key
) != properties_
.end());
452 properties_
.erase(key
);
454 properties_
[key
] = value
;
458 void* Scope::GetProperty(const void* key
, const Scope
** found_on_scope
) const {
459 PropertyMap::const_iterator found
= properties_
.find(key
);
460 if (found
!= properties_
.end()) {
462 *found_on_scope
= this;
463 return found
->second
;
466 return containing()->GetProperty(key
, found_on_scope
);
470 void Scope::AddProvider(ProgrammaticProvider
* p
) {
471 programmatic_providers_
.insert(p
);
474 void Scope::RemoveProvider(ProgrammaticProvider
* p
) {
475 DCHECK(programmatic_providers_
.find(p
) != programmatic_providers_
.end());
476 programmatic_providers_
.erase(p
);