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