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   unsigned IsFramework : 1;
63 
64   /// \brief Whether this is an explicit submodule.
65   unsigned IsExplicit : 1;
66 
67   /// \brief Whether we should infer submodules for this module based on
68   /// the headers.
69   ///
70   /// Submodules can only be inferred for modules with an umbrella header.
71   unsigned InferSubmodules : 1;
72 
73   /// \brief Whether, when inferring submodules, the inferred submodules
74   /// should be explicit.
75   unsigned InferExplicitSubmodules : 1;
76 
77   /// \brief Whether, when inferring submodules, the inferr submodules should
78   /// export all modules they import (e.g., the equivalent of "export *").
79   unsigned InferExportWildcard : 1;
80 
81   /// \brief Describes the visibility of the various names within a
82   /// particular module.
83   enum NameVisibilityKind {
84     /// \brief All of the names in this module are hidden.
85     ///
86     Hidden,
87     /// \brief Only the macro names in this module are visible.
88     MacrosVisible,
89     /// \brief All of the names in this module are visible.
90     AllVisible
91   };
92 
93   ///\ brief The visibility of names within this particular module.
94   NameVisibilityKind NameVisibility;
95 
96   /// \brief The location of the inferred submodule.
97   SourceLocation InferredSubmoduleLoc;
98 
99   /// \brief The set of modules imported by this module, and on which this
100   /// module depends.
101   llvm::SmallVector<Module *, 2> Imports;
102 
103   /// \brief Describes an exported module.
104   ///
105   /// The pointer is the module being re-exported, while the bit will be true
106   /// to indicate that this is a wildcard export.
107   typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
108 
109   /// \brief The set of export declarations.
110   llvm::SmallVector<ExportDecl, 2> Exports;
111 
112   /// \brief Describes an exported module that has not yet been resolved
113   /// (perhaps because tASThe module it refers to has not yet been loaded).
114   struct UnresolvedExportDecl {
115     /// \brief The location of the 'export' keyword in the module map file.
116     SourceLocation ExportLoc;
117 
118     /// \brief The name of the module.
119     ModuleId Id;
120 
121     /// \brief Whether this export declaration ends in a wildcard, indicating
122     /// that all of its submodules should be exported (rather than the named
123     /// module itself).
124     bool Wildcard;
125   };
126 
127   /// \brief The set of export declarations that have yet to be resolved.
128   llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
129 
130   /// \brief Construct a top-level module.
131   explicit Module(StringRef Name, SourceLocation DefinitionLoc,
132                   bool IsFramework)
133     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), UmbrellaHeader(0),
134       IsFramework(IsFramework), IsExplicit(false), InferSubmodules(false),
135       InferExplicitSubmodules(false), InferExportWildcard(false),
136       NameVisibility(Hidden) { }
137 
138   /// \brief Construct  a new module or submodule.
139   Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
140          bool IsFramework, bool IsExplicit)
141     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
142       UmbrellaHeader(0), IsFramework(IsFramework), IsExplicit(IsExplicit),
143       InferSubmodules(false), InferExplicitSubmodules(false),
144       InferExportWildcard(false),NameVisibility(Hidden) { }
145 
146   ~Module();
147 
148   /// \brief Determine whether this module is a submodule.
149   bool isSubModule() const { return Parent != 0; }
150 
151   /// \brief Determine whether this module is a submodule of the given other
152   /// module.
153   bool isSubModuleOf(Module *Other) const;
154 
155   /// \brief Determine whether this module is a part of a framework,
156   /// either because it is a framework module or because it is a submodule
157   /// of a framework module.
158   bool isPartOfFramework() const {
159     for (const Module *Mod = this; Mod; Mod = Mod->Parent)
160       if (Mod->IsFramework)
161         return true;
162 
163     return false;
164   }
165 
166   /// \brief Retrieve the full name of this module, including the path from
167   /// its top-level module.
168   std::string getFullModuleName() const;
169 
170   /// \brief Retrieve the top-level module for this (sub)module, which may
171   /// be this module.
172   Module *getTopLevelModule() {
173     return const_cast<Module *>(
174              const_cast<const Module *>(this)->getTopLevelModule());
175   }
176 
177   /// \brief Retrieve the top-level module for this (sub)module, which may
178   /// be this module.
179   const Module *getTopLevelModule() const;
180 
181   /// \brief Retrieve the name of the top-level module.
182   ///
183   StringRef getTopLevelModuleName() const {
184     return getTopLevelModule()->Name;
185   }
186 
187   /// \brief Print the module map for this module to the given stream.
188   ///
189   void print(llvm::raw_ostream &OS, unsigned Indent = 0) const;
190 
191   /// \brief Dump the contents of this module to the given output stream.
192   void dump() const;
193 };
194 
195 } // end namespace clang
196 
197 
198 #endif // LLVM_CLANG_BASIC_MODULE_H
199