DirectoryLookup.h revision 8f5d7d1d1f990f174c7f2682271a83acf64dd93d
153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//===--- DirectoryLookup.h - Info for searching for headers -----*- C++ -*-===//
253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//
353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//
553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// License. See LICENSE.TXT for details.
753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//
853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//===----------------------------------------------------------------------===//
953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//
1053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// This file defines the DirectoryLookup interface.
1153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//
1253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)//===----------------------------------------------------------------------===//
1353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
1453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#ifndef LLVM_CLANG_LEX_DIRECTORYLOOKUP_H
1553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#define LLVM_CLANG_LEX_DIRECTORYLOOKUP_H
1653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
1753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "clang/Basic/LLVM.h"
1853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "clang/Basic/SourceManager.h"
1953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "clang/Lex/ModuleMap.h"
2053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
2153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)namespace clang {
2253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class HeaderMap;
2353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class DirectoryEntry;
2453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class FileEntry;
2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class HeaderSearch;
2653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class Module;
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
28bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)/// DirectoryLookup - This class represents one entry in the search list that
29197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch/// specifies the search order for directories in \#include directives.  It
30197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch/// represents either a directory, a framework, or a headermap.
3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)///
32c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)class DirectoryLookup {
3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)public:
3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  enum LookupType_t {
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    LT_NormalDir,
3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    LT_Framework,
3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    LT_HeaderMap
3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  };
3953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)private:
4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  union {  // This union is discriminated by isHeaderMap.
4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    /// Dir - This is the actual directory that we're referring to for a normal
4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    /// directory or a framework.
43a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    const DirectoryEntry *Dir;
4407a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    /// Map - This is the HeaderMap if this is a headermap lookup.
4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    ///
4753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    const HeaderMap *Map;
4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  } u;
49d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
5051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)  /// DirCharacteristic - The type of directory this is: this is an instance of
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// SrcMgr::CharacteristicKind.
5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  unsigned DirCharacteristic : 2;
537242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// LookupType - This indicates whether this DirectoryLookup object is a
5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// normal directory, a framework, or a headermap.
5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  unsigned LookupType : 2;
57197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
58197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch  /// \brief Whether this is a header map used when building a framework.
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  unsigned IsIndexHeaderMap : 1;
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \brief Whether we've performed an exhaustive search for module maps
6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// within the subdirectories of this directory.
6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  unsigned SearchedAllModuleMaps : 1;
6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)public:
6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// DirectoryLookup ctor - Note that this ctor *does not take ownership* of
6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// 'dir'.
6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  DirectoryLookup(const DirectoryEntry *dir, SrcMgr::CharacteristicKind DT,
6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                  bool isFramework)
70a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    : DirCharacteristic(DT),
7153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      LookupType(isFramework ? LT_Framework : LT_NormalDir),
7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      IsIndexHeaderMap(false), SearchedAllModuleMaps(false) {
7307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    u.Dir = dir;
7453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
7553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
7653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// DirectoryLookup ctor - Note that this ctor *does not take ownership* of
77d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)  /// 'map'.
7851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)  DirectoryLookup(const HeaderMap *map, SrcMgr::CharacteristicKind DT,
7953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                  bool isIndexHeaderMap)
8053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    : DirCharacteristic(DT), LookupType(LT_HeaderMap),
817242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci      IsIndexHeaderMap(isIndexHeaderMap), SearchedAllModuleMaps(false) {
8253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    u.Map = map;
8353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
8453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
85197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch  /// getLookupType - Return the kind of directory lookup that this is: either a
86197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch  /// normal directory, a framework path, or a HeaderMap.
8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)  LookupType_t getLookupType() const { return (LookupType_t)LookupType; }
8853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
8953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// getName - Return the directory or filename corresponding to this lookup
9053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// object.
9153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const char *getName() const;
9253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
9353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// getDir - Return the directory that this entry refers to.
9453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
9553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const DirectoryEntry *getDir() const { return isNormalDir() ? u.Dir : 0; }
9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// getFrameworkDir - Return the directory that this framework refers to.
9853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const DirectoryEntry *getFrameworkDir() const {
10053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return isFramework() ? u.Dir : 0;
10153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
10253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// getHeaderMap - Return the directory that this entry refers to.
10453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
10553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const HeaderMap *getHeaderMap() const { return isHeaderMap() ? u.Map : 0; }
10653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
10753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// isNormalDir - Return true if this is a normal directory, not a header map.
10853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool isNormalDir() const { return getLookupType() == LT_NormalDir; }
10953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
11053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// isFramework - True if this is a framework directory.
11153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
11253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool isFramework() const { return getLookupType() == LT_Framework; }
11353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
11453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// isHeaderMap - Return true if this is a header map, not a normal directory.
11553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool isHeaderMap() const { return getLookupType() == LT_HeaderMap; }
11653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
11753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \brief Determine whether we have already searched this entire
11853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// directory for module maps.
11953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool haveSearchedAllModuleMaps() const { return SearchedAllModuleMaps; }
12053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
12153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \brief Specify whether we have already searched all of the subdirectories
12253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// for module maps.
12353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  void setSearchedAllModuleMaps(bool SAMM) {
12453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    SearchedAllModuleMaps = SAMM;
12553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
12653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
12753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// DirCharacteristic - The type of directory this is, one of the DirType enum
12853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// values.
12953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  SrcMgr::CharacteristicKind getDirCharacteristic() const {
13053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return (SrcMgr::CharacteristicKind)DirCharacteristic;
13153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
13253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
13353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \brief Whether this describes a system header directory.
13453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool isSystemHeaderDirectory() const {
13553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return getDirCharacteristic() != SrcMgr::C_User;
13653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
13753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
13853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \brief Whether this header map is building a framework or not.
13953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  bool isIndexHeaderMap() const {
14053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)    return isHeaderMap() && IsIndexHeaderMap;
14153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  }
14253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
14353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// LookupFile - Lookup the specified file in this search path, returning it
14453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// if it exists or returning null if not.
14553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
14653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \param Filename The file to look up relative to the search paths.
14753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
14853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \param HS The header search instance to search with.
14953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
150a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)  /// \param SearchPath If not NULL, will be set to the search path relative
15153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// to which the file was found.
15253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
15353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \param RelativePath If not NULL, will be set to the path relative to
15453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// SearchPath at which the file was found. This only differs from the
155a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)  /// Filename for framework includes.
15653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
15753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \param SuggestedModule If non-null, and the file found is semantically
15853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// part of a known module, this will be set to the module that should
15953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// be imported instead of preprocessing/parsing the file found.
16053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  ///
16153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// \param [out] InUserSpecifiedSystemFramework If the file is found,
16253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// set to true if the file is located in a framework that has been
16353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  /// user-specified to be treated as a system framework.
16453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const FileEntry *LookupFile(StringRef Filename, HeaderSearch &HS,
165a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                              SmallVectorImpl<char> *SearchPath,
16653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                              SmallVectorImpl<char> *RelativePath,
16753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                              ModuleMap::KnownHeader *SuggestedModule,
16853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)                              bool &InUserSpecifiedSystemFramework) const;
16953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
170a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)private:
17153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)  const FileEntry *DoFrameworkLookup(
17253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      StringRef Filename, HeaderSearch &HS,
17353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      SmallVectorImpl<char> *SearchPath,
17453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      SmallVectorImpl<char> *RelativePath,
175a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)      ModuleMap::KnownHeader *SuggestedModule,
17653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)      bool &InUserSpecifiedSystemHeader) const;
17753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
17853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)};
17953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
180a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}  // end namespace clang
18153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
18253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#endif
18353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)