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/source_file.h" |
10 #include "tools/gn/template.h" | 11 #include "tools/gn/template.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies | 15 // FLags set in the mode_flags_ of a scope. If a bit is set, it applies |
15 // recursively to all dependent scopes. | 16 // recursively to all dependent scopes. |
16 const unsigned kProcessingBuildConfigFlag = 1; | 17 const unsigned kProcessingBuildConfigFlag = 1; |
17 const unsigned kProcessingImportFlag = 2; | 18 const unsigned kProcessingImportFlag = 2; |
18 | 19 |
19 // Returns true if this variable name should be considered private. Private | 20 // Returns true if this variable name should be considered private. Private |
(...skipping 12 matching lines...) Expand all Loading... |
32 mark_dest_used(false) { | 33 mark_dest_used(false) { |
33 } | 34 } |
34 | 35 |
35 Scope::MergeOptions::~MergeOptions() { | 36 Scope::MergeOptions::~MergeOptions() { |
36 } | 37 } |
37 | 38 |
38 Scope::ProgrammaticProvider::~ProgrammaticProvider() { | 39 Scope::ProgrammaticProvider::~ProgrammaticProvider() { |
39 scope_->RemoveProvider(this); | 40 scope_->RemoveProvider(this); |
40 } | 41 } |
41 | 42 |
42 Scope::Scope(const Settings* settings) | 43 Scope::Scope(const Settings* settings, const InputFileSet& input_files) |
43 : const_containing_(nullptr), | 44 : const_containing_(nullptr), |
44 mutable_containing_(nullptr), | 45 mutable_containing_(nullptr), |
45 settings_(settings), | 46 settings_(settings), |
46 mode_flags_(0), | 47 mode_flags_(0), |
47 item_collector_(nullptr) { | 48 item_collector_(nullptr), |
48 } | 49 input_files_(input_files) {} |
49 | 50 |
50 Scope::Scope(Scope* parent) | 51 Scope::Scope(Scope* parent) |
51 : const_containing_(nullptr), | 52 : const_containing_(nullptr), |
52 mutable_containing_(parent), | 53 mutable_containing_(parent), |
53 settings_(parent->settings()), | 54 settings_(parent->settings()), |
54 mode_flags_(0), | 55 mode_flags_(0), |
55 item_collector_(nullptr) { | 56 item_collector_(nullptr), |
56 } | 57 input_files_(parent->input_files_) {} |
57 | 58 |
58 Scope::Scope(const Scope* parent) | 59 Scope::Scope(const Scope* parent) |
59 : const_containing_(parent), | 60 : const_containing_(parent), |
60 mutable_containing_(nullptr), | 61 mutable_containing_(nullptr), |
61 settings_(parent->settings()), | 62 settings_(parent->settings()), |
62 mode_flags_(0), | 63 mode_flags_(0), |
63 item_collector_(nullptr) { | 64 item_collector_(nullptr), |
64 } | 65 input_files_(parent->input_files_) {} |
65 | 66 |
66 Scope::~Scope() { | 67 Scope::~Scope() { |
67 } | 68 } |
68 | 69 |
69 void Scope::DetachFromContaining() { | 70 void Scope::DetachFromContaining() { |
70 const_containing_ = nullptr; | 71 const_containing_ = nullptr; |
71 mutable_containing_ = nullptr; | 72 mutable_containing_ = nullptr; |
72 } | 73 } |
73 | 74 |
74 bool Scope::HasValues(SearchNested search_nested) const { | 75 bool Scope::HasValues(SearchNested search_nested) const { |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 "same target type in your current scope. It's unfortunate that " | 333 "same target type in your current scope. It's unfortunate that " |
333 "I'm too stupid\nto tell you the location of where the target " | 334 "I'm too stupid\nto tell you the location of where the target " |
334 "defaults were set. Usually\nthis happens in the BUILDCONFIG.gn " | 335 "defaults were set. Usually\nthis happens in the BUILDCONFIG.gn " |
335 "file or in a related .gni file.\n"); | 336 "file or in a related .gni file.\n"); |
336 return false; | 337 return false; |
337 } | 338 } |
338 } | 339 } |
339 } | 340 } |
340 | 341 |
341 std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; | 342 std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; |
342 dest_scope = base::MakeUnique<Scope>(settings_); | 343 dest_scope = base::MakeUnique<Scope>(settings_, input_files_); |
343 pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, | 344 pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, |
344 "<SHOULDN'T HAPPEN>", err); | 345 "<SHOULDN'T HAPPEN>", err); |
345 } | 346 } |
346 | 347 |
347 // Sources assignment filter. | 348 // Sources assignment filter. |
348 if (sources_assignment_filter_) { | 349 if (sources_assignment_filter_) { |
349 if (!options.clobber_existing) { | 350 if (!options.clobber_existing) { |
350 if (dest->GetSourcesAssignmentFilter()) { | 351 if (dest->GetSourcesAssignmentFilter()) { |
351 // Sources assignment filter present in both the source and the dest. | 352 // Sources assignment filter present in both the source and the dest. |
352 std::string desc_string(desc_for_err); | 353 std::string desc_string(desc_for_err); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 "Executing " + desc_string + " should not conflict with anything " | 391 "Executing " + desc_string + " should not conflict with anything " |
391 "in the current\nscope.")); | 392 "in the current\nscope.")); |
392 return false; | 393 return false; |
393 } | 394 } |
394 } | 395 } |
395 | 396 |
396 // Be careful to delete any pointer we're about to clobber. | 397 // Be careful to delete any pointer we're about to clobber. |
397 dest->templates_[current_name] = pair.second; | 398 dest->templates_[current_name] = pair.second; |
398 } | 399 } |
399 | 400 |
| 401 // Input files. |
| 402 dest->input_files_.insert(input_files_.begin(), input_files_.end()); |
| 403 |
400 return true; | 404 return true; |
401 } | 405 } |
402 | 406 |
403 std::unique_ptr<Scope> Scope::MakeClosure() const { | 407 std::unique_ptr<Scope> Scope::MakeClosure() const { |
404 std::unique_ptr<Scope> result; | 408 std::unique_ptr<Scope> result; |
405 if (const_containing_) { | 409 if (const_containing_) { |
406 // We reached the top of the mutable scope stack. The result scope just | 410 // We reached the top of the mutable scope stack. The result scope just |
407 // references the const scope (which will never change). | 411 // references the const scope (which will never change). |
408 result.reset(new Scope(const_containing_)); | 412 result.reset(new Scope(const_containing_)); |
409 } else if (mutable_containing_) { | 413 } else if (mutable_containing_) { |
410 // There are more nested mutable scopes. Recursively go up the stack to | 414 // There are more nested mutable scopes. Recursively go up the stack to |
411 // get the closure. | 415 // get the closure. |
412 result = mutable_containing_->MakeClosure(); | 416 result = mutable_containing_->MakeClosure(); |
413 } else { | 417 } else { |
414 // This is a standalone scope, just copy it. | 418 // This is a standalone scope, just copy it. |
415 result.reset(new Scope(settings_)); | 419 result.reset(new Scope(settings_, input_files_)); |
416 } | 420 } |
417 | 421 |
418 // Want to clobber since we've flattened some nested scopes, and our parent | 422 // Want to clobber since we've flattened some nested scopes, and our parent |
419 // scope may have a duplicate value set. | 423 // scope may have a duplicate value set. |
420 MergeOptions options; | 424 MergeOptions options; |
421 options.clobber_existing = true; | 425 options.clobber_existing = true; |
422 | 426 |
423 // Add in our variables and we're done. | 427 // Add in our variables and we're done. |
424 Err err; | 428 Err err; |
425 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", | 429 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", |
426 &err); | 430 &err); |
427 DCHECK(!err.has_error()); | 431 DCHECK(!err.has_error()); |
428 return result; | 432 return result; |
429 } | 433 } |
430 | 434 |
431 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { | 435 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { |
432 std::unique_ptr<Scope>& dest = target_defaults_[target_type]; | 436 std::unique_ptr<Scope>& dest = target_defaults_[target_type]; |
433 dest = base::MakeUnique<Scope>(settings_); | 437 dest = base::MakeUnique<Scope>(settings_, input_files_); |
434 return dest.get(); | 438 return dest.get(); |
435 } | 439 } |
436 | 440 |
437 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { | 441 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { |
438 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); | 442 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); |
439 if (found != target_defaults_.end()) | 443 if (found != target_defaults_.end()) |
440 return found->second.get(); | 444 return found->second.get(); |
441 if (containing()) | 445 if (containing()) |
442 return containing()->GetTargetDefaults(target_type); | 446 return containing()->GetTargetDefaults(target_type); |
443 return nullptr; | 447 return nullptr; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 } | 492 } |
489 | 493 |
490 const SourceDir& Scope::GetSourceDir() const { | 494 const SourceDir& Scope::GetSourceDir() const { |
491 if (!source_dir_.is_null()) | 495 if (!source_dir_.is_null()) |
492 return source_dir_; | 496 return source_dir_; |
493 if (containing()) | 497 if (containing()) |
494 return containing()->GetSourceDir(); | 498 return containing()->GetSourceDir(); |
495 return source_dir_; | 499 return source_dir_; |
496 } | 500 } |
497 | 501 |
| 502 void Scope::AddInputFile(const InputFile* input_file) { |
| 503 input_files_.insert(input_file); |
| 504 } |
| 505 |
498 Scope::ItemVector* Scope::GetItemCollector() { | 506 Scope::ItemVector* Scope::GetItemCollector() { |
499 if (item_collector_) | 507 if (item_collector_) |
500 return item_collector_; | 508 return item_collector_; |
501 if (mutable_containing()) | 509 if (mutable_containing()) |
502 return mutable_containing()->GetItemCollector(); | 510 return mutable_containing()->GetItemCollector(); |
503 return nullptr; | 511 return nullptr; |
504 } | 512 } |
505 | 513 |
506 void Scope::SetProperty(const void* key, void* value) { | 514 void Scope::SetProperty(const void* key, void* value) { |
507 if (!value) { | 515 if (!value) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 return false; | 547 return false; |
540 for (const auto& pair : a) { | 548 for (const auto& pair : a) { |
541 const auto& found_b = b.find(pair.first); | 549 const auto& found_b = b.find(pair.first); |
542 if (found_b == b.end()) | 550 if (found_b == b.end()) |
543 return false; // Item in 'a' but not 'b'. | 551 return false; // Item in 'a' but not 'b'. |
544 if (pair.second.value != found_b->second.value) | 552 if (pair.second.value != found_b->second.value) |
545 return false; // Values for variable in 'a' and 'b' are different. | 553 return false; // Values for variable in 'a' and 'b' are different. |
546 } | 554 } |
547 return true; | 555 return true; |
548 } | 556 } |
OLD | NEW |