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