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. 4 5 #ifndef TOOLS_GN_BUILDER_RECORD_H_ 6 #define TOOLS_GN_BUILDER_RECORD_H_ 7 8 #include <memory> 9 #include <set> 10 #include <utility> 11 12 #include "base/macros.h" 13 #include "gn/item.h" 14 #include "gn/location.h" 15 16 class ParseNode; 17 18 // This class is used by the builder to manage the loading of the dependency 19 // tree. It holds a reference to an item and links to other records that the 20 // item depends on, both resolved ones, and unresolved ones. 21 // 22 // If a target depends on another one that hasn't been defined yet, we'll make 23 // a placeholder BuilderRecord with no item, and try to load the buildfile 24 // associated with the new item. The item will get filled in when we encounter 25 // the declaration for the item (or when we're done and realize there are 26 // undefined items). 27 // 28 // You can also have null item pointers when the target is not required for 29 // the current build (should_generate is false). 30 class BuilderRecord { 31 public: 32 using BuilderRecordSet = std::set<BuilderRecord*>; 33 34 enum ItemType { 35 ITEM_UNKNOWN, 36 ITEM_TARGET, 37 ITEM_CONFIG, 38 ITEM_TOOLCHAIN, 39 ITEM_POOL 40 }; 41 42 BuilderRecord(ItemType type, const Label& label); 43 type()44 ItemType type() const { return type_; } label()45 const Label& label() const { return label_; } 46 47 // Returns a user-ready name for the given type. e.g. "target". 48 static const char* GetNameForType(ItemType type); 49 50 // Returns true if the given item is of the given type. 51 static bool IsItemOfType(const Item* item, ItemType type); 52 53 // Returns the type enum for the given item. 54 static ItemType TypeOfItem(const Item* item); 55 item()56 Item* item() { return item_.get(); } item()57 const Item* item() const { return item_.get(); } set_item(std::unique_ptr<Item> item)58 void set_item(std::unique_ptr<Item> item) { item_ = std::move(item); } 59 60 // Indicates from where this item was originally referenced from that caused 61 // it to be loaded. For targets for which we encountered the declaration 62 // before a reference, this will be the empty range. originally_referenced_from()63 const ParseNode* originally_referenced_from() const { 64 return originally_referenced_from_; 65 } set_originally_referenced_from(const ParseNode * pn)66 void set_originally_referenced_from(const ParseNode* pn) { 67 originally_referenced_from_ = pn; 68 } 69 should_generate()70 bool should_generate() const { return should_generate_; } set_should_generate(bool sg)71 void set_should_generate(bool sg) { should_generate_ = sg; } 72 resolved()73 bool resolved() const { return resolved_; } set_resolved(bool r)74 void set_resolved(bool r) { resolved_ = r; } 75 can_resolve()76 bool can_resolve() const { return item_ && unresolved_deps_.empty(); } 77 78 // All records this one is depending on. all_deps()79 BuilderRecordSet& all_deps() { return all_deps_; } all_deps()80 const BuilderRecordSet& all_deps() const { return all_deps_; } 81 82 // Unresolved records this one is depending on. A subset of all... above. unresolved_deps()83 BuilderRecordSet& unresolved_deps() { return unresolved_deps_; } unresolved_deps()84 const BuilderRecordSet& unresolved_deps() const { return unresolved_deps_; } 85 86 // Records that are waiting on this one to be resolved. This is the other 87 // end of the "unresolved deps" arrow. waiting_on_resolution()88 BuilderRecordSet& waiting_on_resolution() { return waiting_on_resolution_; } waiting_on_resolution()89 const BuilderRecordSet& waiting_on_resolution() const { 90 return waiting_on_resolution_; 91 } 92 93 void AddDep(BuilderRecord* record); 94 95 private: 96 ItemType type_; 97 Label label_; 98 std::unique_ptr<Item> item_; 99 const ParseNode* originally_referenced_from_ = nullptr; 100 bool should_generate_ = false; 101 bool resolved_ = false; 102 103 BuilderRecordSet all_deps_; 104 BuilderRecordSet unresolved_deps_; 105 BuilderRecordSet waiting_on_resolution_; 106 107 DISALLOW_COPY_AND_ASSIGN(BuilderRecord); 108 }; 109 110 #endif // TOOLS_GN_BUILDER_RECORD_H_ 111