1de3ef502SDouglas Gregor //===--- Module.h - Describe a module ---------------------------*- C++ -*-===//
2de3ef502SDouglas Gregor //
3de3ef502SDouglas Gregor //                     The LLVM Compiler Infrastructure
4de3ef502SDouglas Gregor //
5de3ef502SDouglas Gregor // This file is distributed under the University of Illinois Open Source
6de3ef502SDouglas Gregor // License. See LICENSE.TXT for details.
7de3ef502SDouglas Gregor //
8de3ef502SDouglas Gregor //===----------------------------------------------------------------------===//
9de3ef502SDouglas Gregor //
10de3ef502SDouglas Gregor // This file defines the Module class, which describes a module in the source
11de3ef502SDouglas Gregor // code.
12de3ef502SDouglas Gregor //
13de3ef502SDouglas Gregor //===----------------------------------------------------------------------===//
14de3ef502SDouglas Gregor #include "clang/Basic/Module.h"
15de3ef502SDouglas Gregor #include "clang/Basic/FileManager.h"
16de3ef502SDouglas Gregor #include "llvm/Support/raw_ostream.h"
17de3ef502SDouglas Gregor using namespace clang;
18de3ef502SDouglas Gregor 
19de3ef502SDouglas Gregor Module::~Module() {
20de3ef502SDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = SubModules.begin(),
21de3ef502SDouglas Gregor                                         IEnd = SubModules.end();
22de3ef502SDouglas Gregor        I != IEnd; ++I) {
23de3ef502SDouglas Gregor     delete I->getValue();
24de3ef502SDouglas Gregor   }
25de3ef502SDouglas Gregor 
26de3ef502SDouglas Gregor }
27de3ef502SDouglas Gregor 
28*f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const {
29*f5eedd05SDouglas Gregor   const Module *This = this;
30*f5eedd05SDouglas Gregor   do {
31*f5eedd05SDouglas Gregor     if (This == Other)
32*f5eedd05SDouglas Gregor       return true;
33*f5eedd05SDouglas Gregor 
34*f5eedd05SDouglas Gregor     This = This->Parent;
35*f5eedd05SDouglas Gregor   } while (This);
36*f5eedd05SDouglas Gregor 
37*f5eedd05SDouglas Gregor   return false;
38*f5eedd05SDouglas Gregor }
39*f5eedd05SDouglas Gregor 
40de3ef502SDouglas Gregor std::string Module::getFullModuleName() const {
41de3ef502SDouglas Gregor   llvm::SmallVector<StringRef, 2> Names;
42de3ef502SDouglas Gregor 
43de3ef502SDouglas Gregor   // Build up the set of module names (from innermost to outermost).
44de3ef502SDouglas Gregor   for (const Module *M = this; M; M = M->Parent)
45de3ef502SDouglas Gregor     Names.push_back(M->Name);
46de3ef502SDouglas Gregor 
47de3ef502SDouglas Gregor   std::string Result;
48de3ef502SDouglas Gregor   for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
49de3ef502SDouglas Gregor                                                       IEnd = Names.rend();
50de3ef502SDouglas Gregor        I != IEnd; ++I) {
51de3ef502SDouglas Gregor     if (!Result.empty())
52de3ef502SDouglas Gregor       Result += '.';
53de3ef502SDouglas Gregor 
54de3ef502SDouglas Gregor     Result += *I;
55de3ef502SDouglas Gregor   }
56de3ef502SDouglas Gregor 
57de3ef502SDouglas Gregor   return Result;
58de3ef502SDouglas Gregor }
59de3ef502SDouglas Gregor 
60de3ef502SDouglas Gregor StringRef Module::getTopLevelModuleName() const {
61de3ef502SDouglas Gregor   const Module *Top = this;
62de3ef502SDouglas Gregor   while (Top->Parent)
63de3ef502SDouglas Gregor     Top = Top->Parent;
64de3ef502SDouglas Gregor 
65de3ef502SDouglas Gregor   return Top->Name;
66de3ef502SDouglas Gregor }
67de3ef502SDouglas Gregor 
6824bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
6924bb923aSDouglas Gregor   for (unsigned I = 0, N = Id.size(); I != N; ++I) {
7024bb923aSDouglas Gregor     if (I)
7124bb923aSDouglas Gregor       OS << ".";
7224bb923aSDouglas Gregor     OS << Id[I].first;
7324bb923aSDouglas Gregor   }
7424bb923aSDouglas Gregor }
7524bb923aSDouglas Gregor 
76de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
77de3ef502SDouglas Gregor   OS.indent(Indent);
78de3ef502SDouglas Gregor   if (IsFramework)
79de3ef502SDouglas Gregor     OS << "framework ";
80de3ef502SDouglas Gregor   if (IsExplicit)
81de3ef502SDouglas Gregor     OS << "explicit ";
82de3ef502SDouglas Gregor   OS << "module " << Name << " {\n";
83de3ef502SDouglas Gregor 
84de3ef502SDouglas Gregor   if (UmbrellaHeader) {
85de3ef502SDouglas Gregor     OS.indent(Indent + 2);
86de3ef502SDouglas Gregor     OS << "umbrella \"";
87de3ef502SDouglas Gregor     OS.write_escaped(UmbrellaHeader->getName());
88de3ef502SDouglas Gregor     OS << "\"\n";
89de3ef502SDouglas Gregor   }
90de3ef502SDouglas Gregor 
91de3ef502SDouglas Gregor   for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
92de3ef502SDouglas Gregor     OS.indent(Indent + 2);
93de3ef502SDouglas Gregor     OS << "header \"";
94de3ef502SDouglas Gregor     OS.write_escaped(Headers[I]->getName());
95de3ef502SDouglas Gregor     OS << "\"\n";
96de3ef502SDouglas Gregor   }
97de3ef502SDouglas Gregor 
98de3ef502SDouglas Gregor   for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(),
99de3ef502SDouglas Gregor        MIEnd = SubModules.end();
100de3ef502SDouglas Gregor        MI != MIEnd; ++MI)
101de3ef502SDouglas Gregor     MI->getValue()->print(OS, Indent + 2);
102de3ef502SDouglas Gregor 
10324bb923aSDouglas Gregor   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
10424bb923aSDouglas Gregor     OS.indent(Indent + 2);
10524bb923aSDouglas Gregor     OS << "export " << Exports[I].getPointer()->getFullModuleName();
10624bb923aSDouglas Gregor     if (Exports[I].getInt())
10724bb923aSDouglas Gregor       OS << ".*";
10824bb923aSDouglas Gregor     OS << "\n";
10924bb923aSDouglas Gregor   }
11024bb923aSDouglas Gregor 
11124bb923aSDouglas Gregor   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
11224bb923aSDouglas Gregor     OS.indent(Indent + 2);
11324bb923aSDouglas Gregor     OS << "export ";
11424bb923aSDouglas Gregor     printModuleId(OS, UnresolvedExports[I].Id);
11524bb923aSDouglas Gregor     if (UnresolvedExports[I].Wildcard)
11624bb923aSDouglas Gregor       OS << ".*";
11724bb923aSDouglas Gregor     OS << "\n";
11824bb923aSDouglas Gregor   }
11924bb923aSDouglas Gregor 
120de3ef502SDouglas Gregor   OS.indent(Indent);
121de3ef502SDouglas Gregor   OS << "}\n";
122de3ef502SDouglas Gregor }
123de3ef502SDouglas Gregor 
124de3ef502SDouglas Gregor void Module::dump() const {
125de3ef502SDouglas Gregor   print(llvm::errs());
126de3ef502SDouglas Gregor }
127de3ef502SDouglas Gregor 
128de3ef502SDouglas Gregor 
129