1d44252ecSDouglas Gregor //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2d44252ecSDouglas Gregor //
3d44252ecSDouglas Gregor //                     The LLVM Compiler Infrastructure
4d44252ecSDouglas Gregor //
5d44252ecSDouglas Gregor // This file is distributed under the University of Illinois Open Source
6d44252ecSDouglas Gregor // License. See LICENSE.TXT for details.
7d44252ecSDouglas Gregor //
8d44252ecSDouglas Gregor //===----------------------------------------------------------------------===//
9d44252ecSDouglas Gregor //
10d44252ecSDouglas Gregor //  This file defines the ModuleManager class, which manages a set of loaded
11d44252ecSDouglas Gregor //  modules for the ASTReader.
12d44252ecSDouglas Gregor //
13d44252ecSDouglas Gregor //===----------------------------------------------------------------------===//
14d44252ecSDouglas Gregor #include "clang/Serialization/ModuleManager.h"
15d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h"
16d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.h"
17d44252ecSDouglas Gregor #include "llvm/Support/system_error.h"
18d44252ecSDouglas Gregor 
199d7c1a2aSDouglas Gregor #ifndef NDEBUG
209d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h"
219d7c1a2aSDouglas Gregor #endif
229d7c1a2aSDouglas Gregor 
23d44252ecSDouglas Gregor using namespace clang;
24d44252ecSDouglas Gregor using namespace serialization;
25d44252ecSDouglas Gregor 
26de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) {
27d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name);
28d44252ecSDouglas Gregor   return Modules[Entry];
29d44252ecSDouglas Gregor }
30d44252ecSDouglas Gregor 
31d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
32d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name);
33d44252ecSDouglas Gregor   return InMemoryBuffers[Entry];
34d44252ecSDouglas Gregor }
35d44252ecSDouglas Gregor 
36de3ef502SDouglas Gregor std::pair<ModuleFile *, bool>
37d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type,
386fb03aeaSDouglas Gregor                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
396fb03aeaSDouglas Gregor                          unsigned Generation, std::string &ErrorStr) {
40d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(FileName);
41d44252ecSDouglas Gregor   if (!Entry && FileName != "-") {
42d44252ecSDouglas Gregor     ErrorStr = "file not found";
43de3ef502SDouglas Gregor     return std::make_pair(static_cast<ModuleFile*>(0), false);
44d44252ecSDouglas Gregor   }
45d44252ecSDouglas Gregor 
46d44252ecSDouglas Gregor   // Check whether we already loaded this module, before
47de3ef502SDouglas Gregor   ModuleFile *&ModuleEntry = Modules[Entry];
48d44252ecSDouglas Gregor   bool NewModule = false;
49d44252ecSDouglas Gregor   if (!ModuleEntry) {
50d44252ecSDouglas Gregor     // Allocate a new module.
514fc9f3e8SDouglas Gregor     ModuleFile *New = new ModuleFile(Type, Generation);
52bdb259d2SDouglas Gregor     New->Index = Chain.size();
53d44252ecSDouglas Gregor     New->FileName = FileName.str();
54aedf7144SArgyrios Kyrtzidis     New->File = Entry;
556fb03aeaSDouglas Gregor     New->ImportLoc = ImportLoc;
56d44252ecSDouglas Gregor     Chain.push_back(New);
57d44252ecSDouglas Gregor     NewModule = true;
58d44252ecSDouglas Gregor     ModuleEntry = New;
59d44252ecSDouglas Gregor 
60d44252ecSDouglas Gregor     // Load the contents of the module
61d44252ecSDouglas Gregor     if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
62d44252ecSDouglas Gregor       // The buffer was already provided for us.
63d44252ecSDouglas Gregor       assert(Buffer && "Passed null buffer");
64d44252ecSDouglas Gregor       New->Buffer.reset(Buffer);
65d44252ecSDouglas Gregor     } else {
66d44252ecSDouglas Gregor       // Open the AST file.
67d44252ecSDouglas Gregor       llvm::error_code ec;
68d44252ecSDouglas Gregor       if (FileName == "-") {
69d44252ecSDouglas Gregor         ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
70d44252ecSDouglas Gregor         if (ec)
71d44252ecSDouglas Gregor           ErrorStr = ec.message();
72d44252ecSDouglas Gregor       } else
73d44252ecSDouglas Gregor         New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
74d44252ecSDouglas Gregor 
75d44252ecSDouglas Gregor       if (!New->Buffer)
76de3ef502SDouglas Gregor         return std::make_pair(static_cast<ModuleFile*>(0), false);
77d44252ecSDouglas Gregor     }
78d44252ecSDouglas Gregor 
79d44252ecSDouglas Gregor     // Initialize the stream
80d44252ecSDouglas Gregor     New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
81d44252ecSDouglas Gregor                          (const unsigned char *)New->Buffer->getBufferEnd());     }
82d44252ecSDouglas Gregor 
83d44252ecSDouglas Gregor   if (ImportedBy) {
84d44252ecSDouglas Gregor     ModuleEntry->ImportedBy.insert(ImportedBy);
85d44252ecSDouglas Gregor     ImportedBy->Imports.insert(ModuleEntry);
86d44252ecSDouglas Gregor   } else {
876fb03aeaSDouglas Gregor     if (!ModuleEntry->DirectlyImported)
886fb03aeaSDouglas Gregor       ModuleEntry->ImportLoc = ImportLoc;
896fb03aeaSDouglas Gregor 
90d44252ecSDouglas Gregor     ModuleEntry->DirectlyImported = true;
91d44252ecSDouglas Gregor   }
92d44252ecSDouglas Gregor 
93d44252ecSDouglas Gregor   return std::make_pair(ModuleEntry, NewModule);
94d44252ecSDouglas Gregor }
95d44252ecSDouglas Gregor 
96188dbef2SDouglas Gregor namespace {
97188dbef2SDouglas Gregor   /// \brief Predicate that checks whether a module file occurs within
98188dbef2SDouglas Gregor   /// the given set.
99188dbef2SDouglas Gregor   class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
100188dbef2SDouglas Gregor     llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
101188dbef2SDouglas Gregor 
102188dbef2SDouglas Gregor   public:
103188dbef2SDouglas Gregor     IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
104188dbef2SDouglas Gregor     : Removed(Removed) { }
105188dbef2SDouglas Gregor 
106188dbef2SDouglas Gregor     bool operator()(ModuleFile *MF) const {
107188dbef2SDouglas Gregor       return Removed.count(MF);
108188dbef2SDouglas Gregor     }
109188dbef2SDouglas Gregor   };
110188dbef2SDouglas Gregor }
111188dbef2SDouglas Gregor 
112188dbef2SDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
113188dbef2SDouglas Gregor   if (first == last)
114188dbef2SDouglas Gregor     return;
115188dbef2SDouglas Gregor 
116188dbef2SDouglas Gregor   // Collect the set of module file pointers that we'll be removing.
117188dbef2SDouglas Gregor   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
118188dbef2SDouglas Gregor 
119188dbef2SDouglas Gregor   // Remove any references to the now-destroyed modules.
120188dbef2SDouglas Gregor   IsInModuleFileSet checkInSet(victimSet);
121188dbef2SDouglas Gregor   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
122188dbef2SDouglas Gregor     Chain[i]->ImportedBy.remove_if(checkInSet);
123188dbef2SDouglas Gregor   }
124188dbef2SDouglas Gregor 
125188dbef2SDouglas Gregor   // Delete the modules and erase them from the various structures.
126188dbef2SDouglas Gregor   for (ModuleIterator victim = first; victim != last; ++victim) {
127188dbef2SDouglas Gregor     Modules.erase((*victim)->File);
128188dbef2SDouglas Gregor     delete *victim;
129188dbef2SDouglas Gregor   }
130188dbef2SDouglas Gregor 
131188dbef2SDouglas Gregor   // Remove the modules from the chain.
132188dbef2SDouglas Gregor   Chain.erase(first, last);
133188dbef2SDouglas Gregor }
134188dbef2SDouglas Gregor 
135d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName,
136d44252ecSDouglas Gregor                                       llvm::MemoryBuffer *Buffer) {
137d44252ecSDouglas Gregor 
138d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
139d44252ecSDouglas Gregor                                                   Buffer->getBufferSize(), 0);
140d44252ecSDouglas Gregor   InMemoryBuffers[Entry] = Buffer;
141d44252ecSDouglas Gregor }
142d44252ecSDouglas Gregor 
143aedf7144SArgyrios Kyrtzidis ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { }
144d44252ecSDouglas Gregor 
145d44252ecSDouglas Gregor ModuleManager::~ModuleManager() {
146d44252ecSDouglas Gregor   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
147d44252ecSDouglas Gregor     delete Chain[e - i - 1];
148d44252ecSDouglas Gregor }
149d44252ecSDouglas Gregor 
150de3ef502SDouglas Gregor void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
151d44252ecSDouglas Gregor                           void *UserData) {
152*e41d7feaSDouglas Gregor   // If the visitation number array is the wrong size, resize it and recompute
153*e41d7feaSDouglas Gregor   // an order.
154*e41d7feaSDouglas Gregor   if (VisitOrder.size() != Chain.size()) {
155d44252ecSDouglas Gregor     unsigned N = size();
156*e41d7feaSDouglas Gregor     VisitOrder.clear();
157*e41d7feaSDouglas Gregor     VisitOrder.reserve(N);
158d44252ecSDouglas Gregor 
159d44252ecSDouglas Gregor     // Record the number of incoming edges for each module. When we
160d44252ecSDouglas Gregor     // encounter a module with no incoming edges, push it into the queue
161d44252ecSDouglas Gregor     // to seed the queue.
162de3ef502SDouglas Gregor     SmallVector<ModuleFile *, 4> Queue;
163d44252ecSDouglas Gregor     Queue.reserve(N);
164bdb259d2SDouglas Gregor     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
165bdb259d2SDouglas Gregor     UnusedIncomingEdges.reserve(size());
166d44252ecSDouglas Gregor     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
167d44252ecSDouglas Gregor       if (unsigned Size = (*M)->ImportedBy.size())
168bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(Size);
169bdb259d2SDouglas Gregor       else {
170bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(0);
171d44252ecSDouglas Gregor         Queue.push_back(*M);
172d44252ecSDouglas Gregor       }
173bdb259d2SDouglas Gregor     }
174d44252ecSDouglas Gregor 
175*e41d7feaSDouglas Gregor     // Traverse the graph, making sure to visit a module before visiting any
176*e41d7feaSDouglas Gregor     // of its dependencies.
177d44252ecSDouglas Gregor     unsigned QueueStart = 0;
178d44252ecSDouglas Gregor     while (QueueStart < Queue.size()) {
179de3ef502SDouglas Gregor       ModuleFile *CurrentModule = Queue[QueueStart++];
180*e41d7feaSDouglas Gregor       VisitOrder.push_back(CurrentModule);
181d44252ecSDouglas Gregor 
182d44252ecSDouglas Gregor       // For any module that this module depends on, push it on the
183d44252ecSDouglas Gregor       // stack (if it hasn't already been marked as visited).
184bdb259d2SDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
185bdb259d2SDouglas Gregor              M = CurrentModule->Imports.begin(),
186d44252ecSDouglas Gregor              MEnd = CurrentModule->Imports.end();
187d44252ecSDouglas Gregor            M != MEnd; ++M) {
188d44252ecSDouglas Gregor         // Remove our current module as an impediment to visiting the
189d44252ecSDouglas Gregor         // module we depend on. If we were the last unvisited module
190d44252ecSDouglas Gregor         // that depends on this particular module, push it into the
191d44252ecSDouglas Gregor         // queue to be visited.
192bdb259d2SDouglas Gregor         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
193d44252ecSDouglas Gregor         if (NumUnusedEdges && (--NumUnusedEdges == 0))
194d44252ecSDouglas Gregor           Queue.push_back(*M);
195d44252ecSDouglas Gregor       }
196d44252ecSDouglas Gregor     }
197*e41d7feaSDouglas Gregor 
198*e41d7feaSDouglas Gregor     assert(VisitOrder.size() == N && "Visitation order is wrong?");
199*e41d7feaSDouglas Gregor   }
200*e41d7feaSDouglas Gregor 
201*e41d7feaSDouglas Gregor   SmallVector<ModuleFile *, 4> Stack;
202*e41d7feaSDouglas Gregor   SmallVector<bool, 4> Visited(size(), false);
203*e41d7feaSDouglas Gregor 
204*e41d7feaSDouglas Gregor   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
205*e41d7feaSDouglas Gregor     ModuleFile *CurrentModule = VisitOrder[I];
206*e41d7feaSDouglas Gregor     // Should we skip this module file?
207*e41d7feaSDouglas Gregor     if (Visited[CurrentModule->Index])
208*e41d7feaSDouglas Gregor       continue;
209*e41d7feaSDouglas Gregor 
210*e41d7feaSDouglas Gregor     // Visit the module.
211*e41d7feaSDouglas Gregor     Visited[CurrentModule->Index] = true;
212*e41d7feaSDouglas Gregor     if (!Visitor(*CurrentModule, UserData))
213*e41d7feaSDouglas Gregor       continue;
214*e41d7feaSDouglas Gregor 
215*e41d7feaSDouglas Gregor     // The visitor has requested that cut off visitation of any
216*e41d7feaSDouglas Gregor     // module that the current module depends on. To indicate this
217*e41d7feaSDouglas Gregor     // behavior, we mark all of the reachable modules as having been visited.
218*e41d7feaSDouglas Gregor     ModuleFile *NextModule = CurrentModule;
219*e41d7feaSDouglas Gregor     Stack.reserve(size());
220*e41d7feaSDouglas Gregor     do {
221*e41d7feaSDouglas Gregor       // For any module that this module depends on, push it on the
222*e41d7feaSDouglas Gregor       // stack (if it hasn't already been marked as visited).
223*e41d7feaSDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
224*e41d7feaSDouglas Gregor              M = NextModule->Imports.begin(),
225*e41d7feaSDouglas Gregor              MEnd = NextModule->Imports.end();
226*e41d7feaSDouglas Gregor            M != MEnd; ++M) {
227*e41d7feaSDouglas Gregor         if (!Visited[(*M)->Index]) {
228*e41d7feaSDouglas Gregor           Stack.push_back(*M);
229*e41d7feaSDouglas Gregor           Visited[(*M)->Index] = true;
230*e41d7feaSDouglas Gregor         }
231*e41d7feaSDouglas Gregor       }
232*e41d7feaSDouglas Gregor 
233*e41d7feaSDouglas Gregor       if (Stack.empty())
234*e41d7feaSDouglas Gregor         break;
235*e41d7feaSDouglas Gregor 
236*e41d7feaSDouglas Gregor       // Pop the next module off the stack.
237*e41d7feaSDouglas Gregor       NextModule = Stack.back();
238*e41d7feaSDouglas Gregor       Stack.pop_back();
239*e41d7feaSDouglas Gregor     } while (true);
240*e41d7feaSDouglas Gregor   }
241d44252ecSDouglas Gregor }
242d44252ecSDouglas Gregor 
243d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module.
244de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M,
245de3ef502SDouglas Gregor                             bool (*Visitor)(ModuleFile &M, bool Preorder,
246d44252ecSDouglas Gregor                                             void *UserData),
247d44252ecSDouglas Gregor                             void *UserData,
248bdb259d2SDouglas Gregor                             SmallVectorImpl<bool> &Visited) {
249d44252ecSDouglas Gregor   // Preorder visitation
250d44252ecSDouglas Gregor   if (Visitor(M, /*Preorder=*/true, UserData))
251d44252ecSDouglas Gregor     return true;
252d44252ecSDouglas Gregor 
253d44252ecSDouglas Gregor   // Visit children
254de3ef502SDouglas Gregor   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
255d44252ecSDouglas Gregor                                             IMEnd = M.Imports.end();
256d44252ecSDouglas Gregor        IM != IMEnd; ++IM) {
257bdb259d2SDouglas Gregor     if (Visited[(*IM)->Index])
258d44252ecSDouglas Gregor       continue;
259bdb259d2SDouglas Gregor     Visited[(*IM)->Index] = true;
260d44252ecSDouglas Gregor 
261d44252ecSDouglas Gregor     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
262d44252ecSDouglas Gregor       return true;
263d44252ecSDouglas Gregor   }
264d44252ecSDouglas Gregor 
265d44252ecSDouglas Gregor   // Postorder visitation
266d44252ecSDouglas Gregor   return Visitor(M, /*Preorder=*/false, UserData);
267d44252ecSDouglas Gregor }
268d44252ecSDouglas Gregor 
269de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
270d44252ecSDouglas Gregor                                                     void *UserData),
271d44252ecSDouglas Gregor                                     void *UserData) {
272bdb259d2SDouglas Gregor   SmallVector<bool, 16> Visited(size(), false);
273d44252ecSDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
274bdb259d2SDouglas Gregor     if (Visited[Chain[I]->Index])
275d44252ecSDouglas Gregor       continue;
276bdb259d2SDouglas Gregor     Visited[Chain[I]->Index] = true;
277d44252ecSDouglas Gregor 
278d44252ecSDouglas Gregor     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
279d44252ecSDouglas Gregor       return;
280d44252ecSDouglas Gregor   }
281d44252ecSDouglas Gregor }
2829d7c1a2aSDouglas Gregor 
2839d7c1a2aSDouglas Gregor #ifndef NDEBUG
2849d7c1a2aSDouglas Gregor namespace llvm {
2859d7c1a2aSDouglas Gregor   template<>
2869d7c1a2aSDouglas Gregor   struct GraphTraits<ModuleManager> {
287de3ef502SDouglas Gregor     typedef ModuleFile NodeType;
288de3ef502SDouglas Gregor     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
2899d7c1a2aSDouglas Gregor     typedef ModuleManager::ModuleConstIterator nodes_iterator;
2909d7c1a2aSDouglas Gregor 
2919d7c1a2aSDouglas Gregor     static ChildIteratorType child_begin(NodeType *Node) {
2929d7c1a2aSDouglas Gregor       return Node->Imports.begin();
2939d7c1a2aSDouglas Gregor     }
2949d7c1a2aSDouglas Gregor 
2959d7c1a2aSDouglas Gregor     static ChildIteratorType child_end(NodeType *Node) {
2969d7c1a2aSDouglas Gregor       return Node->Imports.end();
2979d7c1a2aSDouglas Gregor     }
2989d7c1a2aSDouglas Gregor 
2999d7c1a2aSDouglas Gregor     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
3009d7c1a2aSDouglas Gregor       return Manager.begin();
3019d7c1a2aSDouglas Gregor     }
3029d7c1a2aSDouglas Gregor 
3039d7c1a2aSDouglas Gregor     static nodes_iterator nodes_end(const ModuleManager &Manager) {
3049d7c1a2aSDouglas Gregor       return Manager.end();
3059d7c1a2aSDouglas Gregor     }
3069d7c1a2aSDouglas Gregor   };
3079d7c1a2aSDouglas Gregor 
3089d7c1a2aSDouglas Gregor   template<>
3099d7c1a2aSDouglas Gregor   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
3109d7c1a2aSDouglas Gregor     explicit DOTGraphTraits(bool IsSimple = false)
3119d7c1a2aSDouglas Gregor       : DefaultDOTGraphTraits(IsSimple) { }
3129d7c1a2aSDouglas Gregor 
3139d7c1a2aSDouglas Gregor     static bool renderGraphFromBottomUp() {
3149d7c1a2aSDouglas Gregor       return true;
3159d7c1a2aSDouglas Gregor     }
3169d7c1a2aSDouglas Gregor 
317de3ef502SDouglas Gregor     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
3189d7c1a2aSDouglas Gregor       return llvm::sys::path::stem(M->FileName);
3199d7c1a2aSDouglas Gregor     }
3209d7c1a2aSDouglas Gregor   };
3219d7c1a2aSDouglas Gregor }
3229d7c1a2aSDouglas Gregor 
3239d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() {
3249d7c1a2aSDouglas Gregor   llvm::ViewGraph(*this, "Modules");
3259d7c1a2aSDouglas Gregor }
3269d7c1a2aSDouglas Gregor #endif
327