1 //===- ModuleLoader.h - Module Loader Interface -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ModuleLoader interface, which is responsible for
10 //  loading named modules.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LEX_MODULELOADER_H
15 #define LLVM_CLANG_LEX_MODULELOADER_H
16 
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/Module.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/StringRef.h"
23 #include <utility>
24 
25 namespace clang {
26 
27 class GlobalModuleIndex;
28 class IdentifierInfo;
29 
30 /// A sequence of identifier/location pairs used to describe a particular
31 /// module or submodule, e.g., std.vector.
32 using ModuleIdPath = ArrayRef<std::pair<IdentifierInfo *, SourceLocation>>;
33 
34 /// Describes the result of attempting to load a module.
35 class ModuleLoadResult {
36 public:
37   enum LoadResultKind {
38     // We either succeeded or failed to load the named module.
39     Normal,
40 
41     // The module exists, but does not actually contain the named submodule.
42     // This should only happen if the named submodule was inferred from an
43     // umbrella directory, but not actually part of the umbrella header.
44     MissingExpected,
45 
46     // The module exists but cannot be imported due to a configuration mismatch.
47     ConfigMismatch,
48 
49     // We failed to load the module, but we shouldn't cache the failure.
50     OtherUncachedFailure,
51   };
52   llvm::PointerIntPair<Module *, 2, LoadResultKind> Storage;
53 
54   ModuleLoadResult() = default;
55   ModuleLoadResult(Module *M) : Storage(M, Normal) {}
56   ModuleLoadResult(LoadResultKind Kind) : Storage(nullptr, Kind) {}
57 
58   operator Module *() const { return Storage.getPointer(); }
59 
60   /// Determines whether this is a normal return, whether or not loading the
61   /// module was successful.
62   bool isNormal() const { return Storage.getInt() == Normal; }
63 
64   /// Determines whether the module, which failed to load, was
65   /// actually a submodule that we expected to see (based on implying the
66   /// submodule from header structure), but didn't materialize in the actual
67   /// module.
68   bool isMissingExpected() const { return Storage.getInt() == MissingExpected; }
69 
70   /// Determines whether the module failed to load due to a configuration
71   /// mismatch with an explicitly-named .pcm file from the command line.
72   bool isConfigMismatch() const { return Storage.getInt() == ConfigMismatch; }
73 };
74 
75 /// Abstract interface for a module loader.
76 ///
77 /// This abstract interface describes a module loader, which is responsible
78 /// for resolving a module name (e.g., "std") to an actual module file, and
79 /// then loading that module.
80 class ModuleLoader {
81   // Building a module if true.
82   bool BuildingModule;
83 
84 public:
85   explicit ModuleLoader(bool BuildingModule = false)
86       : BuildingModule(BuildingModule) {}
87 
88   virtual ~ModuleLoader();
89 
90   /// Returns true if this instance is building a module.
91   bool buildingModule() const {
92     return BuildingModule;
93   }
94 
95   /// Flag indicating whether this instance is building a module.
96   void setBuildingModule(bool BuildingModuleFlag) {
97     BuildingModule = BuildingModuleFlag;
98   }
99 
100   /// Attempt to load the given module.
101   ///
102   /// This routine attempts to load the module described by the given
103   /// parameters.  If there is a module cache, this may implicitly compile the
104   /// module before loading it.
105   ///
106   /// \param ImportLoc The location of the 'import' keyword.
107   ///
108   /// \param Path The identifiers (and their locations) of the module
109   /// "path", e.g., "std.vector" would be split into "std" and "vector".
110   ///
111   /// \param Visibility The visibility provided for the names in the loaded
112   /// module.
113   ///
114   /// \param IsInclusionDirective Indicates that this module is being loaded
115   /// implicitly, due to the presence of an inclusion directive. Otherwise,
116   /// it is being loaded due to an import declaration.
117   ///
118   /// \returns If successful, returns the loaded module. Otherwise, returns
119   /// NULL to indicate that the module could not be loaded.
120   virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
121                                       ModuleIdPath Path,
122                                       Module::NameVisibilityKind Visibility,
123                                       bool IsInclusionDirective) = 0;
124 
125   /// Attempt to create the given module from the specified source buffer.
126   /// Does not load the module or make any submodule visible; for that, use
127   /// loadModule and makeModuleVisible.
128   ///
129   /// \param Loc The location at which to create the module.
130   /// \param ModuleName The name of the module to create.
131   /// \param Source The source of the module: a (preprocessed) module map.
132   virtual void createModuleFromSource(SourceLocation Loc, StringRef ModuleName,
133                                       StringRef Source) = 0;
134 
135   /// Make the given module visible.
136   virtual void makeModuleVisible(Module *Mod,
137                                  Module::NameVisibilityKind Visibility,
138                                  SourceLocation ImportLoc) = 0;
139 
140   /// Load, create, or return global module.
141   /// This function returns an existing global module index, if one
142   /// had already been loaded or created, or loads one if it
143   /// exists, or creates one if it doesn't exist.
144   /// Also, importantly, if the index doesn't cover all the modules
145   /// in the module map, it will be update to do so here, because
146   /// of its use in searching for needed module imports and
147   /// associated fixit messages.
148   /// \param TriggerLoc The location for what triggered the load.
149   /// \returns Returns null if load failed.
150   virtual GlobalModuleIndex *loadGlobalModuleIndex(
151                                                 SourceLocation TriggerLoc) = 0;
152 
153   /// Check global module index for missing imports.
154   /// \param Name The symbol name to look for.
155   /// \param TriggerLoc The location for what triggered the load.
156   /// \returns Returns true if any modules with that symbol found.
157   virtual bool lookupMissingImports(StringRef Name,
158                                     SourceLocation TriggerLoc) = 0;
159 
160   bool HadFatalFailure = false;
161 };
162 
163 /// A module loader that doesn't know how to create or load modules.
164 class TrivialModuleLoader : public ModuleLoader {
165 public:
166   ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
167                               Module::NameVisibilityKind Visibility,
168                               bool IsInclusionDirective) override {
169     return {};
170   }
171 
172   void createModuleFromSource(SourceLocation ImportLoc, StringRef ModuleName,
173                               StringRef Source) override {}
174 
175   void makeModuleVisible(Module *Mod, Module::NameVisibilityKind Visibility,
176                          SourceLocation ImportLoc) override {}
177 
178   GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override {
179     return nullptr;
180   }
181 
182   bool lookupMissingImports(StringRef Name,
183                             SourceLocation TriggerLoc) override {
184     return false;
185   }
186 };
187 
188 } // namespace clang
189 
190 #endif // LLVM_CLANG_LEX_MODULELOADER_H
191