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