1 //===--- Module.h - Describe a module ---------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Module class, which describes a module in the source
11 // code.
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_BASIC_MODULE_H
15 #define LLVM_CLANG_BASIC_MODULE_H
16 
17 #include "clang/Basic/SourceLocation.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include <string>
23 #include <utility>
24 
25 namespace llvm {
26   class raw_ostream;
27 }
28 
29 namespace clang {
30 
31 class FileEntry;
32 
33 /// \brief Describes the name of a module.
34 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
35   ModuleId;
36 
37 /// \brief Describes a module or submodule.
38 class Module {
39 public:
40   /// \brief The name of this module.
41   std::string Name;
42 
43   /// \brief The location of the module definition.
44   SourceLocation DefinitionLoc;
45 
46   /// \brief The parent of this module. This will be NULL for the top-level
47   /// module.
48   Module *Parent;
49 
50   /// \brief The umbrella header, if any.
51   ///
52   /// Only the top-level module can have an umbrella header.
53   const FileEntry *UmbrellaHeader;
54 
55   /// \brief The submodules of this module, indexed by name.
56   llvm::StringMap<Module *> SubModules;
57 
58   /// \brief The headers that are part of this module.
59   llvm::SmallVector<const FileEntry *, 2> Headers;
60 
61   /// \brief Whether this is a framework module.
62   bool IsFramework;
63 
64   /// \brief Whether this is an explicit submodule.
65   bool IsExplicit;
66 
67   /// \brief Describes the visibility of the various names within a
68   /// particular module.
69   enum NameVisibilityKind {
70     /// \brief All of the names in this module are hidden.
71     ///
72     Hidden,
73     /// \brief Only the macro names in this module are visible.
74     MacrosVisible,
75     /// \brief All of the names in this module are visible.
76     AllVisible
77   };
78 
79   ///\ brief The visibility of names within this particular module.
80   NameVisibilityKind NameVisibility;
81 
82   /// \brief The set of modules imported by this module, and on which this
83   /// module depends.
84   llvm::SmallVector<Module *, 2> Imports;
85 
86   /// \brief Describes an exported module.
87   ///
88   /// The pointer is the module being re-exported, while the bit will be true
89   /// to indicate that this is a wildcard export.
90   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
91 
92   /// \brief The set of export declarations.
93   llvm::SmallVector<ExportDecl, 2> Exports;
94 
95   /// \brief Describes an exported module that has not yet been resolved
96   /// (perhaps because tASThe module it refers to has not yet been loaded).
97   struct UnresolvedExportDecl {
98     /// \brief The location of the 'export' keyword in the module map file.
99     SourceLocation ExportLoc;
100 
101     /// \brief The name of the module.
102     ModuleId Id;
103 
104     /// \brief Whether this export declaration ends in a wildcard, indicating
105     /// that all of its submodules should be exported (rather than the named
106     /// module itself).
107     bool Wildcard;
108   };
109 
110   /// \brief The set of export declarations that have yet to be resolved.
111   llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
112 
113   /// \brief Construct a top-level module.
114   explicit Module(StringRef Name, SourceLocation DefinitionLoc,
115                   bool IsFramework)
116     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0),
117       IsFramework(IsFramework), IsExplicit(false), NameVisibility(Hidden) { }
118 
119   /// \brief Construct  a new module or submodule.
120   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
121          bool IsFramework, bool IsExplicit)
122     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
123       UmbrellaHeader(0), IsFramework(IsFramework), IsExplicit(IsExplicit),
124       NameVisibility(Hidden) { }
125 
126   ~Module();
127 
128   /// \brief Determine whether this module is a submodule.
129   bool isSubModule() const { return Parent != 0; }
130 
131   /// \brief Determine whether this module is a submodule of the given other
132   /// module.
133   bool isSubModuleOf(Module *Other) const;
134 
135   /// \brief Determine whether this module is a part of a framework,
136   /// either because it is a framework module or because it is a submodule
137   /// of a framework module.
138   bool isPartOfFramework() const {
139     for (const Module *Mod = this; Mod; Mod = Mod->Parent)
140       if (Mod->IsFramework)
141         return true;
142 
143     return false;
144   }
145 
146   /// \brief Retrieve the full name of this module, including the path from
147   /// its top-level module.
148   std::string getFullModuleName() const;
149 
150   /// \brief Retrieve the name of the top-level module.
151   ///
152   StringRef getTopLevelModuleName() const;
153 
154   /// \brief Print the module map for this module to the given stream.
155   ///
156   void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
157 
158   /// \brief Dump the contents of this module to the given output stream.
159   void dump() const;
160 };
161 
162 } // end namespace clang
163 
164 
165 #endif // LLVM_CLANG_BASIC_MODULE_H
166