• 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 #include "gn/builder_record.h"
6 
7 #include "gn/item.h"
8 
BuilderRecord(ItemType type,const Label & label,const ParseNode * originally_referenced_from)9 BuilderRecord::BuilderRecord(ItemType type,
10                              const Label& label,
11                              const ParseNode* originally_referenced_from)
12     : type_(type),
13       label_(label),
14       originally_referenced_from_(originally_referenced_from) {}
15 
16 // static
GetNameForType(ItemType type)17 const char* BuilderRecord::GetNameForType(ItemType type) {
18   switch (type) {
19     case ITEM_TARGET:
20       return "target";
21     case ITEM_CONFIG:
22       return "config";
23     case ITEM_TOOLCHAIN:
24       return "toolchain";
25     case ITEM_POOL:
26       return "pool";
27     case ITEM_UNKNOWN:
28     default:
29       return "unknown";
30   }
31 }
32 
33 // static
IsItemOfType(const Item * item,ItemType type)34 bool BuilderRecord::IsItemOfType(const Item* item, ItemType type) {
35   switch (type) {
36     case ITEM_TARGET:
37       return !!item->AsTarget();
38     case ITEM_CONFIG:
39       return !!item->AsConfig();
40     case ITEM_TOOLCHAIN:
41       return !!item->AsToolchain();
42     case ITEM_POOL:
43       return !!item->AsPool();
44     case ITEM_UNKNOWN:
45     default:
46       return false;
47   }
48 }
49 
50 // static
TypeOfItem(const Item * item)51 BuilderRecord::ItemType BuilderRecord::TypeOfItem(const Item* item) {
52   if (item->AsTarget())
53     return ITEM_TARGET;
54   if (item->AsConfig())
55     return ITEM_CONFIG;
56   if (item->AsToolchain())
57     return ITEM_TOOLCHAIN;
58   if (item->AsPool())
59     return ITEM_POOL;
60 
61   NOTREACHED();
62   return ITEM_UNKNOWN;
63 }
64 
AddGenDep(BuilderRecord * record)65 void BuilderRecord::AddGenDep(BuilderRecord* record) {
66   // Records don't have to wait on resolution of their gen deps, since all they
67   // need to do is propagate should_generate to them.
68   all_deps_.insert(record);
69 }
70 
OnResolvedDep(const BuilderRecord * dep)71 bool BuilderRecord::OnResolvedDep(const BuilderRecord* dep) {
72   DCHECK(all_deps_.contains(const_cast<BuilderRecord*>(dep)));
73   DCHECK(unresolved_count_ > 0);
74   return --unresolved_count_ == 0;
75 }
76 
GetSortedUnresolvedDeps() const77 std::vector<const BuilderRecord*> BuilderRecord::GetSortedUnresolvedDeps()
78     const {
79   std::vector<const BuilderRecord*> result;
80   for (auto it = all_deps_.begin(); it.valid(); ++it) {
81     BuilderRecord* dep = *it;
82     if (dep->waiting_on_resolution_.contains(const_cast<BuilderRecord*>(this)))
83       result.push_back(dep);
84   }
85   std::sort(result.begin(), result.end(), LabelCompare);
86   return result;
87 }
88 
AddDep(BuilderRecord * record)89 void BuilderRecord::AddDep(BuilderRecord* record) {
90   if (all_deps_.add(record) && !record->resolved()) {
91     unresolved_count_ += 1;
92     record->waiting_on_resolution_.add(this);
93   }
94 }
95