1f4a2713aSLionel Sambuc //===--- DirectoryLookup.h - Info for searching for headers -----*- C++ -*-===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc // 10f4a2713aSLionel Sambuc // This file defines the DirectoryLookup interface. 11f4a2713aSLionel Sambuc // 12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13f4a2713aSLionel Sambuc 14f4a2713aSLionel Sambuc #ifndef LLVM_CLANG_LEX_DIRECTORYLOOKUP_H 15f4a2713aSLionel Sambuc #define LLVM_CLANG_LEX_DIRECTORYLOOKUP_H 16f4a2713aSLionel Sambuc 17f4a2713aSLionel Sambuc #include "clang/Basic/LLVM.h" 18f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h" 19f4a2713aSLionel Sambuc #include "clang/Lex/ModuleMap.h" 20f4a2713aSLionel Sambuc 21f4a2713aSLionel Sambuc namespace clang { 22f4a2713aSLionel Sambuc class HeaderMap; 23f4a2713aSLionel Sambuc class DirectoryEntry; 24f4a2713aSLionel Sambuc class FileEntry; 25f4a2713aSLionel Sambuc class HeaderSearch; 26f4a2713aSLionel Sambuc class Module; 27f4a2713aSLionel Sambuc 28f4a2713aSLionel Sambuc /// DirectoryLookup - This class represents one entry in the search list that 29f4a2713aSLionel Sambuc /// specifies the search order for directories in \#include directives. It 30f4a2713aSLionel Sambuc /// represents either a directory, a framework, or a headermap. 31f4a2713aSLionel Sambuc /// 32f4a2713aSLionel Sambuc class DirectoryLookup { 33f4a2713aSLionel Sambuc public: 34f4a2713aSLionel Sambuc enum LookupType_t { 35f4a2713aSLionel Sambuc LT_NormalDir, 36f4a2713aSLionel Sambuc LT_Framework, 37f4a2713aSLionel Sambuc LT_HeaderMap 38f4a2713aSLionel Sambuc }; 39f4a2713aSLionel Sambuc private: 40f4a2713aSLionel Sambuc union { // This union is discriminated by isHeaderMap. 41f4a2713aSLionel Sambuc /// Dir - This is the actual directory that we're referring to for a normal 42f4a2713aSLionel Sambuc /// directory or a framework. 43f4a2713aSLionel Sambuc const DirectoryEntry *Dir; 44f4a2713aSLionel Sambuc 45f4a2713aSLionel Sambuc /// Map - This is the HeaderMap if this is a headermap lookup. 46f4a2713aSLionel Sambuc /// 47f4a2713aSLionel Sambuc const HeaderMap *Map; 48f4a2713aSLionel Sambuc } u; 49f4a2713aSLionel Sambuc 50f4a2713aSLionel Sambuc /// DirCharacteristic - The type of directory this is: this is an instance of 51f4a2713aSLionel Sambuc /// SrcMgr::CharacteristicKind. 52f4a2713aSLionel Sambuc unsigned DirCharacteristic : 2; 53f4a2713aSLionel Sambuc 54f4a2713aSLionel Sambuc /// LookupType - This indicates whether this DirectoryLookup object is a 55f4a2713aSLionel Sambuc /// normal directory, a framework, or a headermap. 56f4a2713aSLionel Sambuc unsigned LookupType : 2; 57f4a2713aSLionel Sambuc 58f4a2713aSLionel Sambuc /// \brief Whether this is a header map used when building a framework. 59f4a2713aSLionel Sambuc unsigned IsIndexHeaderMap : 1; 60f4a2713aSLionel Sambuc 61f4a2713aSLionel Sambuc /// \brief Whether we've performed an exhaustive search for module maps 62f4a2713aSLionel Sambuc /// within the subdirectories of this directory. 63f4a2713aSLionel Sambuc unsigned SearchedAllModuleMaps : 1; 64f4a2713aSLionel Sambuc 65f4a2713aSLionel Sambuc public: 66f4a2713aSLionel Sambuc /// DirectoryLookup ctor - Note that this ctor *does not take ownership* of 67f4a2713aSLionel Sambuc /// 'dir'. DirectoryLookup(const DirectoryEntry * dir,SrcMgr::CharacteristicKind DT,bool isFramework)68f4a2713aSLionel Sambuc DirectoryLookup(const DirectoryEntry *dir, SrcMgr::CharacteristicKind DT, 69f4a2713aSLionel Sambuc bool isFramework) 70f4a2713aSLionel Sambuc : DirCharacteristic(DT), 71f4a2713aSLionel Sambuc LookupType(isFramework ? LT_Framework : LT_NormalDir), 72f4a2713aSLionel Sambuc IsIndexHeaderMap(false), SearchedAllModuleMaps(false) { 73f4a2713aSLionel Sambuc u.Dir = dir; 74f4a2713aSLionel Sambuc } 75f4a2713aSLionel Sambuc 76f4a2713aSLionel Sambuc /// DirectoryLookup ctor - Note that this ctor *does not take ownership* of 77f4a2713aSLionel Sambuc /// 'map'. DirectoryLookup(const HeaderMap * map,SrcMgr::CharacteristicKind DT,bool isIndexHeaderMap)78f4a2713aSLionel Sambuc DirectoryLookup(const HeaderMap *map, SrcMgr::CharacteristicKind DT, 79f4a2713aSLionel Sambuc bool isIndexHeaderMap) 80f4a2713aSLionel Sambuc : DirCharacteristic(DT), LookupType(LT_HeaderMap), 81f4a2713aSLionel Sambuc IsIndexHeaderMap(isIndexHeaderMap), SearchedAllModuleMaps(false) { 82f4a2713aSLionel Sambuc u.Map = map; 83f4a2713aSLionel Sambuc } 84f4a2713aSLionel Sambuc 85f4a2713aSLionel Sambuc /// getLookupType - Return the kind of directory lookup that this is: either a 86f4a2713aSLionel Sambuc /// normal directory, a framework path, or a HeaderMap. getLookupType()87f4a2713aSLionel Sambuc LookupType_t getLookupType() const { return (LookupType_t)LookupType; } 88f4a2713aSLionel Sambuc 89f4a2713aSLionel Sambuc /// getName - Return the directory or filename corresponding to this lookup 90f4a2713aSLionel Sambuc /// object. 91f4a2713aSLionel Sambuc const char *getName() const; 92f4a2713aSLionel Sambuc 93f4a2713aSLionel Sambuc /// getDir - Return the directory that this entry refers to. 94f4a2713aSLionel Sambuc /// getDir()95*0a6a1f1dSLionel Sambuc const DirectoryEntry *getDir() const { 96*0a6a1f1dSLionel Sambuc return isNormalDir() ? u.Dir : nullptr; 97*0a6a1f1dSLionel Sambuc } 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc /// getFrameworkDir - Return the directory that this framework refers to. 100f4a2713aSLionel Sambuc /// getFrameworkDir()101f4a2713aSLionel Sambuc const DirectoryEntry *getFrameworkDir() const { 102*0a6a1f1dSLionel Sambuc return isFramework() ? u.Dir : nullptr; 103f4a2713aSLionel Sambuc } 104f4a2713aSLionel Sambuc 105f4a2713aSLionel Sambuc /// getHeaderMap - Return the directory that this entry refers to. 106f4a2713aSLionel Sambuc /// getHeaderMap()107*0a6a1f1dSLionel Sambuc const HeaderMap *getHeaderMap() const { 108*0a6a1f1dSLionel Sambuc return isHeaderMap() ? u.Map : nullptr; 109*0a6a1f1dSLionel Sambuc } 110f4a2713aSLionel Sambuc 111f4a2713aSLionel Sambuc /// isNormalDir - Return true if this is a normal directory, not a header map. isNormalDir()112f4a2713aSLionel Sambuc bool isNormalDir() const { return getLookupType() == LT_NormalDir; } 113f4a2713aSLionel Sambuc 114f4a2713aSLionel Sambuc /// isFramework - True if this is a framework directory. 115f4a2713aSLionel Sambuc /// isFramework()116f4a2713aSLionel Sambuc bool isFramework() const { return getLookupType() == LT_Framework; } 117f4a2713aSLionel Sambuc 118f4a2713aSLionel Sambuc /// isHeaderMap - Return true if this is a header map, not a normal directory. isHeaderMap()119f4a2713aSLionel Sambuc bool isHeaderMap() const { return getLookupType() == LT_HeaderMap; } 120f4a2713aSLionel Sambuc 121f4a2713aSLionel Sambuc /// \brief Determine whether we have already searched this entire 122f4a2713aSLionel Sambuc /// directory for module maps. haveSearchedAllModuleMaps()123f4a2713aSLionel Sambuc bool haveSearchedAllModuleMaps() const { return SearchedAllModuleMaps; } 124f4a2713aSLionel Sambuc 125f4a2713aSLionel Sambuc /// \brief Specify whether we have already searched all of the subdirectories 126f4a2713aSLionel Sambuc /// for module maps. setSearchedAllModuleMaps(bool SAMM)127f4a2713aSLionel Sambuc void setSearchedAllModuleMaps(bool SAMM) { 128f4a2713aSLionel Sambuc SearchedAllModuleMaps = SAMM; 129f4a2713aSLionel Sambuc } 130f4a2713aSLionel Sambuc 131f4a2713aSLionel Sambuc /// DirCharacteristic - The type of directory this is, one of the DirType enum 132f4a2713aSLionel Sambuc /// values. getDirCharacteristic()133f4a2713aSLionel Sambuc SrcMgr::CharacteristicKind getDirCharacteristic() const { 134f4a2713aSLionel Sambuc return (SrcMgr::CharacteristicKind)DirCharacteristic; 135f4a2713aSLionel Sambuc } 136f4a2713aSLionel Sambuc 137f4a2713aSLionel Sambuc /// \brief Whether this describes a system header directory. isSystemHeaderDirectory()138f4a2713aSLionel Sambuc bool isSystemHeaderDirectory() const { 139f4a2713aSLionel Sambuc return getDirCharacteristic() != SrcMgr::C_User; 140f4a2713aSLionel Sambuc } 141f4a2713aSLionel Sambuc 142f4a2713aSLionel Sambuc /// \brief Whether this header map is building a framework or not. isIndexHeaderMap()143f4a2713aSLionel Sambuc bool isIndexHeaderMap() const { 144f4a2713aSLionel Sambuc return isHeaderMap() && IsIndexHeaderMap; 145f4a2713aSLionel Sambuc } 146f4a2713aSLionel Sambuc 147f4a2713aSLionel Sambuc /// LookupFile - Lookup the specified file in this search path, returning it 148f4a2713aSLionel Sambuc /// if it exists or returning null if not. 149f4a2713aSLionel Sambuc /// 150f4a2713aSLionel Sambuc /// \param Filename The file to look up relative to the search paths. 151f4a2713aSLionel Sambuc /// 152f4a2713aSLionel Sambuc /// \param HS The header search instance to search with. 153f4a2713aSLionel Sambuc /// 154f4a2713aSLionel Sambuc /// \param SearchPath If not NULL, will be set to the search path relative 155f4a2713aSLionel Sambuc /// to which the file was found. 156f4a2713aSLionel Sambuc /// 157f4a2713aSLionel Sambuc /// \param RelativePath If not NULL, will be set to the path relative to 158f4a2713aSLionel Sambuc /// SearchPath at which the file was found. This only differs from the 159f4a2713aSLionel Sambuc /// Filename for framework includes. 160f4a2713aSLionel Sambuc /// 161f4a2713aSLionel Sambuc /// \param SuggestedModule If non-null, and the file found is semantically 162f4a2713aSLionel Sambuc /// part of a known module, this will be set to the module that should 163f4a2713aSLionel Sambuc /// be imported instead of preprocessing/parsing the file found. 164f4a2713aSLionel Sambuc /// 165f4a2713aSLionel Sambuc /// \param [out] InUserSpecifiedSystemFramework If the file is found, 166f4a2713aSLionel Sambuc /// set to true if the file is located in a framework that has been 167f4a2713aSLionel Sambuc /// user-specified to be treated as a system framework. 168*0a6a1f1dSLionel Sambuc /// 169*0a6a1f1dSLionel Sambuc /// \param [out] MappedName if this is a headermap which maps the filename to 170*0a6a1f1dSLionel Sambuc /// a framework include ("Foo.h" -> "Foo/Foo.h"), set the new name to this 171*0a6a1f1dSLionel Sambuc /// vector and point Filename to it. 172*0a6a1f1dSLionel Sambuc const FileEntry *LookupFile(StringRef &Filename, HeaderSearch &HS, 173f4a2713aSLionel Sambuc SmallVectorImpl<char> *SearchPath, 174f4a2713aSLionel Sambuc SmallVectorImpl<char> *RelativePath, 175f4a2713aSLionel Sambuc ModuleMap::KnownHeader *SuggestedModule, 176*0a6a1f1dSLionel Sambuc bool &InUserSpecifiedSystemFramework, 177*0a6a1f1dSLionel Sambuc bool &HasBeenMapped, 178*0a6a1f1dSLionel Sambuc SmallVectorImpl<char> &MappedName) const; 179f4a2713aSLionel Sambuc 180f4a2713aSLionel Sambuc private: 181f4a2713aSLionel Sambuc const FileEntry *DoFrameworkLookup( 182f4a2713aSLionel Sambuc StringRef Filename, HeaderSearch &HS, 183f4a2713aSLionel Sambuc SmallVectorImpl<char> *SearchPath, 184f4a2713aSLionel Sambuc SmallVectorImpl<char> *RelativePath, 185f4a2713aSLionel Sambuc ModuleMap::KnownHeader *SuggestedModule, 186f4a2713aSLionel Sambuc bool &InUserSpecifiedSystemHeader) const; 187f4a2713aSLionel Sambuc 188f4a2713aSLionel Sambuc }; 189f4a2713aSLionel Sambuc 190f4a2713aSLionel Sambuc } // end namespace clang 191f4a2713aSLionel Sambuc 192f4a2713aSLionel Sambuc #endif 193