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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 "same target type in your current scope. It's unfortunate that " | 343 "same target type in your current scope. It's unfortunate that " |
343 "I'm too stupid\nto tell you the location of where the target " | 344 "I'm too stupid\nto tell you the location of where the target " |
344 "defaults were set. Usually\nthis happens in the BUILDCONFIG.gn " | 345 "defaults were set. Usually\nthis happens in the BUILDCONFIG.gn " |
345 "file or in a related .gni file.\n"); | 346 "file or in a related .gni file.\n"); |
346 return false; | 347 return false; |
347 } | 348 } |
348 } | 349 } |
349 } | 350 } |
350 | 351 |
351 std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; | 352 std::unique_ptr<Scope>& dest_scope = dest->target_defaults_[current_name]; |
352 dest_scope = base::MakeUnique<Scope>(settings_); | 353 dest_scope = base::MakeUnique<Scope>(settings_, input_files_); |
353 pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, | 354 pair.second->NonRecursiveMergeTo(dest_scope.get(), options, node_for_err, |
354 "<SHOULDN'T HAPPEN>", err); | 355 "<SHOULDN'T HAPPEN>", err); |
355 } | 356 } |
356 | 357 |
357 // Sources assignment filter. | 358 // Sources assignment filter. |
358 if (sources_assignment_filter_) { | 359 if (sources_assignment_filter_) { |
359 if (!options.clobber_existing) { | 360 if (!options.clobber_existing) { |
360 if (dest->GetSourcesAssignmentFilter()) { | 361 if (dest->GetSourcesAssignmentFilter()) { |
361 // Sources assignment filter present in both the source and the dest. | 362 // Sources assignment filter present in both the source and the dest. |
362 std::string desc_string(desc_for_err); | 363 std::string desc_string(desc_for_err); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 "Executing " + desc_string + " should not conflict with anything " | 401 "Executing " + desc_string + " should not conflict with anything " |
401 "in the current\nscope.")); | 402 "in the current\nscope.")); |
402 return false; | 403 return false; |
403 } | 404 } |
404 } | 405 } |
405 | 406 |
406 // Be careful to delete any pointer we're about to clobber. | 407 // Be careful to delete any pointer we're about to clobber. |
407 dest->templates_[current_name] = pair.second; | 408 dest->templates_[current_name] = pair.second; |
408 } | 409 } |
409 | 410 |
| 411 // Input files. |
| 412 dest->input_files_.insert(input_files_.begin(), input_files_.end()); |
| 413 |
410 return true; | 414 return true; |
411 } | 415 } |
412 | 416 |
413 std::unique_ptr<Scope> Scope::MakeClosure() const { | 417 std::unique_ptr<Scope> Scope::MakeClosure() const { |
414 std::unique_ptr<Scope> result; | 418 std::unique_ptr<Scope> result; |
415 if (const_containing_) { | 419 if (const_containing_) { |
416 // We reached the top of the mutable scope stack. The result scope just | 420 // We reached the top of the mutable scope stack. The result scope just |
417 // references the const scope (which will never change). | 421 // references the const scope (which will never change). |
418 result.reset(new Scope(const_containing_)); | 422 result.reset(new Scope(const_containing_)); |
419 } else if (mutable_containing_) { | 423 } else if (mutable_containing_) { |
420 // There are more nested mutable scopes. Recursively go up the stack to | 424 // There are more nested mutable scopes. Recursively go up the stack to |
421 // get the closure. | 425 // get the closure. |
422 result = mutable_containing_->MakeClosure(); | 426 result = mutable_containing_->MakeClosure(); |
423 } else { | 427 } else { |
424 // This is a standalone scope, just copy it. | 428 // This is a standalone scope, just copy it. |
425 result.reset(new Scope(settings_)); | 429 result.reset(new Scope(settings_, input_files_)); |
426 } | 430 } |
427 | 431 |
428 // Want to clobber since we've flattened some nested scopes, and our parent | 432 // Want to clobber since we've flattened some nested scopes, and our parent |
429 // scope may have a duplicate value set. | 433 // scope may have a duplicate value set. |
430 MergeOptions options; | 434 MergeOptions options; |
431 options.clobber_existing = true; | 435 options.clobber_existing = true; |
432 | 436 |
433 // Add in our variables and we're done. | 437 // Add in our variables and we're done. |
434 Err err; | 438 Err err; |
435 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", | 439 NonRecursiveMergeTo(result.get(), options, nullptr, "<SHOULDN'T HAPPEN>", |
436 &err); | 440 &err); |
437 DCHECK(!err.has_error()); | 441 DCHECK(!err.has_error()); |
438 return result; | 442 return result; |
439 } | 443 } |
440 | 444 |
441 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { | 445 Scope* Scope::MakeTargetDefaults(const std::string& target_type) { |
442 std::unique_ptr<Scope>& dest = target_defaults_[target_type]; | 446 std::unique_ptr<Scope>& dest = target_defaults_[target_type]; |
443 dest = base::MakeUnique<Scope>(settings_); | 447 dest = base::MakeUnique<Scope>(settings_, input_files_); |
444 return dest.get(); | 448 return dest.get(); |
445 } | 449 } |
446 | 450 |
447 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { | 451 const Scope* Scope::GetTargetDefaults(const std::string& target_type) const { |
448 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); | 452 NamedScopeMap::const_iterator found = target_defaults_.find(target_type); |
449 if (found != target_defaults_.end()) | 453 if (found != target_defaults_.end()) |
450 return found->second.get(); | 454 return found->second.get(); |
451 if (containing()) | 455 if (containing()) |
452 return containing()->GetTargetDefaults(target_type); | 456 return containing()->GetTargetDefaults(target_type); |
453 return nullptr; | 457 return nullptr; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 } | 502 } |
499 | 503 |
500 const SourceDir& Scope::GetSourceDir() const { | 504 const SourceDir& Scope::GetSourceDir() const { |
501 if (!source_dir_.is_null()) | 505 if (!source_dir_.is_null()) |
502 return source_dir_; | 506 return source_dir_; |
503 if (containing()) | 507 if (containing()) |
504 return containing()->GetSourceDir(); | 508 return containing()->GetSourceDir(); |
505 return source_dir_; | 509 return source_dir_; |
506 } | 510 } |
507 | 511 |
| 512 void Scope::AddInputFile(const InputFile* input_file) { |
| 513 input_files_.insert(input_file); |
| 514 } |
| 515 |
508 Scope::ItemVector* Scope::GetItemCollector() { | 516 Scope::ItemVector* Scope::GetItemCollector() { |
509 if (item_collector_) | 517 if (item_collector_) |
510 return item_collector_; | 518 return item_collector_; |
511 if (mutable_containing()) | 519 if (mutable_containing()) |
512 return mutable_containing()->GetItemCollector(); | 520 return mutable_containing()->GetItemCollector(); |
513 return nullptr; | 521 return nullptr; |
514 } | 522 } |
515 | 523 |
516 void Scope::SetProperty(const void* key, void* value) { | 524 void Scope::SetProperty(const void* key, void* value) { |
517 if (!value) { | 525 if (!value) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 return false; | 557 return false; |
550 for (const auto& pair : a) { | 558 for (const auto& pair : a) { |
551 const auto& found_b = b.find(pair.first); | 559 const auto& found_b = b.find(pair.first); |
552 if (found_b == b.end()) | 560 if (found_b == b.end()) |
553 return false; // Item in 'a' but not 'b'. | 561 return false; // Item in 'a' but not 'b'. |
554 if (pair.second.value != found_b->second.value) | 562 if (pair.second.value != found_b->second.value) |
555 return false; // Values for variable in 'a' and 'b' are different. | 563 return false; // Values for variable in 'a' and 'b' are different. |
556 } | 564 } |
557 return true; | 565 return true; |
558 } | 566 } |
OLD | NEW |