SourceManager.h revision bff5c512af8ca7ac92e974e04c06ff4f820e4ee1
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details.
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//  This file defines the SourceManager interface.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef LLVM_CLANG_SOURCEMANAGER_H
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LLVM_CLANG_SOURCEMANAGER_H
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/SourceLocation.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Bitcode/SerializationFwd.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Allocator.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/DataTypes.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/DenseMap.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <cassert>
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace llvm {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MemoryBuffer;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace clang {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SourceManager;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileManager;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileEntry;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class IdentifierTokenInfo;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class LineTableInfo;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// SrcMgr - Public enums and private classes that are part of the
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// SourceManager implementation.
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)///
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace SrcMgr {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// CharacteristicKind - This is used to represent whether a file or directory
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// holds normal user code, system code, or system code which is implicitly
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// 'extern "C"' in C++ mode.  Entire directories can be tagged with this
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// (this is maintained by DirectoryLookup and friends) as can specific
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// FileIDInfos when a #pragma system_header is seen or various other cases.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum CharacteristicKind {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    C_User, C_System, C_ExternCSystem
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// ContentCache - Once instance of this struct is kept for every file
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// loaded or used.  This object owns the MemoryBuffer object.
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class ContentCache {
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// Buffer - The actual buffer containing the characters from the input
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// file.  This is owned by the ContentCache object.
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mutable const llvm::MemoryBuffer *Buffer;
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Reference to the file entry.  This reference does not own
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// the FileEntry object.  It is possible for this to be NULL if
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// the ContentCache encapsulates an imaginary text buffer.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const FileEntry *Entry;
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// SourceLineCache - A bump pointer allocated array of offsets for each
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// source line.  This is lazily computed.  This is owned by the
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// SourceManager BumpPointerAllocator object.
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    unsigned *SourceLineCache;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// NumLines - The number of lines in this ContentCache.  This is only valid
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// if SourceLineCache is non-null.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    unsigned NumLines;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// getBuffer - Returns the memory buffer for the associated content.
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const llvm::MemoryBuffer *getBuffer() const;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// getSize - Returns the size of the content encapsulated by this
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  ContentCache. This can be the size of the source file or the size of an
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///  file this size is retrieved from the file's FileEntry.
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    unsigned getSize() const;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// getSizeBytesMapped - Returns the number of bytes actually mapped for
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///  instantiated.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    unsigned getSizeBytesMapped() const;
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    void setBuffer(const llvm::MemoryBuffer *B) {
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      assert(!Buffer && "MemoryBuffer already set.");
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Buffer = B;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ContentCache(const FileEntry *Ent = 0)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {}
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ~ContentCache();
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// The copy ctor does not allow copies where source object has either
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ///  is not transfered, so this is a logical error.
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) {
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Entry = RHS.Entry;
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              && "Passed ContentCache object cannot own a buffer.");
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      NumLines = RHS.NumLines;
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// Emit - Emit this ContentCache to Bitcode.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Emit(llvm::Serializer &S) const;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// ReadToSourceManager - Reconstitute a ContentCache from Bitcode
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   and store it in the specified SourceManager.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static void ReadToSourceManager(llvm::Deserializer &D, SourceManager &SM,
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    FileManager *FMgr, std::vector<char> &Buf);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  private:
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Disable assignments.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ContentCache &operator=(const ContentCache& RHS);
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  };
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// FileInfo - Information about a FileID, basically just the logical file
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// that it represents and include stack information.
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// Each FileInfo has include stack information, indicating where it came
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// from.  This information encodes the #include chain that a token was
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  /// instantiated from.  The main include file has an invalid IncludeLoc.
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// FileInfos contain a "ContentCache *", with the contents of the file.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ///
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class FileInfo {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// IncludeLoc - The location of the #include that brought in this file.
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /// This is an invalid SLOC for the main file (top of the #include chain).
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned IncludeLoc;  // Really a SourceLocation
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// Data - This contains the ContentCache* and the bits indicating the
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// characteristic of the file and whether it has #line info, all bitmangled
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// together.
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    uintptr_t Data;
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  public:
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// get - Return a FileInfo object.
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    static FileInfo get(SourceLocation IL, const ContentCache *Con,
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                        CharacteristicKind FileCharacter) {
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FileInfo X;
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      X.IncludeLoc = IL.getRawEncoding();
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      X.Data = (uintptr_t)Con;
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      assert((unsigned)FileCharacter < 4 && "invalid file character");
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      X.Data |= (unsigned)FileCharacter;
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return X;
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    SourceLocation getIncludeLoc() const {
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return SourceLocation::getFromRawEncoding(IncludeLoc);
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const ContentCache* getContentCache() const {
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return reinterpret_cast<const ContentCache*>(Data & ~7UL);
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// getCharacteristic - Return whether this is a system header or not.
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CharacteristicKind getFileCharacteristic() const {
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return (CharacteristicKind)(Data & 3);
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// hasLineDirectives - Return true if this FileID has #line directives in
1661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// it.
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    bool hasLineDirectives() const { return (Data & 4) != 0; }
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// setHasLineDirectives - Set the flag that indicates that this FileID has
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// line table entries associated with it.
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    void setHasLineDirectives() {
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      Data |= 4;
1731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
1741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  };
1751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
1771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// location - where the token was ultimately instantiated, and the
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// SpellingLoc - where the actual character data for the token came from.
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class InstantiationInfo {
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci     // Really these are all SourceLocations.
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// SpellingLoc - Where the spelling for the token can be found.
1831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    unsigned SpellingLoc;
1841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
1861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// indicate the start and end of the instantiation.  In object-line macros,
1871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// these will be the same.  In a function-like macro instantiation, the
1881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// start will be the identifier and the end will be the ')'.
1891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    unsigned InstantiationLocStart, InstantiationLocEnd;
1901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  public:
1911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    SourceLocation getSpellingLoc() const {
1921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      return SourceLocation::getFromRawEncoding(SpellingLoc);
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SourceLocation getInstantiationLocStart() const {
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return SourceLocation::getFromRawEncoding(InstantiationLocStart);
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SourceLocation getInstantiationLocEnd() const {
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return std::make_pair(getInstantiationLocStart(),
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            getInstantiationLocEnd());
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// get - Return a InstantiationInfo for an expansion.  IL specifies
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// the instantiation location (where the macro is expanded), and SL
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// specifies the spelling location (where the characters from the token
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// come from).  IL and PL can both refer to normal File SLocs or
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// instantiation locations.
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 SourceLocation SL) {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstantiationInfo X;
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      X.SpellingLoc = SL.getRawEncoding();
2150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      X.InstantiationLocStart = ILStart.getRawEncoding();
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      X.InstantiationLocEnd = ILEnd.getRawEncoding();
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return X;
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// SLocEntry - This is a discriminated union of FileInfo and
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// InstantiationInfo.  SourceManager keeps an array of these objects, and
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// they are uniquely identified by the FileID datatype.
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class SLocEntry {
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned Offset;   // low bit is set for instantiation info.
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    union {
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      FileInfo File;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      InstantiationInfo Instantiation;
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    };
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    unsigned getOffset() const { return Offset >> 1; }
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isInstantiation() const { return Offset & 1; }
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool isFile() const { return !isInstantiation(); }
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const FileInfo &getFile() const {
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      assert(isFile() && "Not a file SLocEntry!");
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return File;
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const InstantiationInfo &getInstantiation() const {
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(isInstantiation() && "Not an instantiation SLocEntry!");
243      return Instantiation;
244    }
245
246    static SLocEntry get(unsigned Offset, const FileInfo &FI) {
247      SLocEntry E;
248      E.Offset = Offset << 1;
249      E.File = FI;
250      return E;
251    }
252
253    static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
254      SLocEntry E;
255      E.Offset = (Offset << 1) | 1;
256      E.Instantiation = II;
257      return E;
258    }
259  };
260}  // end SrcMgr namespace.
261
262/// SourceManager - This file handles loading and caching of source files into
263/// memory.  This object owns the MemoryBuffer objects for all of the loaded
264/// files and assigns unique FileID's for each unique #include chain.
265///
266/// The SourceManager can be queried for information about SourceLocation
267/// objects, turning them into either spelling or instantiation locations.
268/// Spelling locations represent where the bytes corresponding to a token came
269/// from and instantiation locations represent where the location is in the
270/// user's view.  In the case of a macro expansion, for example, the spelling
271/// location indicates where the expanded token came from and the instantiation
272/// location specifies where it was expanded.
273class SourceManager {
274  mutable llvm::BumpPtrAllocator ContentCacheAlloc;
275
276  /// FileInfos - Memoized information about all of the files tracked by this
277  /// SourceManager.  This set allows us to merge ContentCache entries based
278  /// on their FileEntry*.  All ContentCache objects will thus have unique,
279  /// non-null, FileEntry pointers.
280  llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
281
282  /// MemBufferInfos - Information about various memory buffers that we have
283  /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
284  /// as they do not refer to a file.
285  std::vector<SrcMgr::ContentCache*> MemBufferInfos;
286
287  /// SLocEntryTable - This is an array of SLocEntry's that we have created.
288  /// FileID is an index into this vector.  This array is sorted by the offset.
289  std::vector<SrcMgr::SLocEntry> SLocEntryTable;
290  /// NextOffset - This is the next available offset that a new SLocEntry can
291  /// start at.  It is SLocEntryTable.back().getOffset()+size of back() entry.
292  unsigned NextOffset;
293
294  /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
295  /// LastFileIDLookup records the last FileID looked up or created, because it
296  /// is very common to look up many tokens from the same file.
297  mutable FileID LastFileIDLookup;
298
299  /// LineTable - This holds information for #line directives.  It is referenced
300  /// by indices from SLocEntryTable.
301  LineTableInfo *LineTable;
302
303  /// LastLineNo - These ivars serve as a cache used in the getLineNumber
304  /// method which is used to speedup getLineNumber calls to nearby locations.
305  mutable FileID LastLineNoFileIDQuery;
306  mutable SrcMgr::ContentCache *LastLineNoContentCache;
307  mutable unsigned LastLineNoFilePos;
308  mutable unsigned LastLineNoResult;
309
310  /// MainFileID - The file ID for the main source file of the translation unit.
311  FileID MainFileID;
312
313  // Statistics for -print-stats.
314  mutable unsigned NumLinearScans, NumBinaryProbes;
315
316  // SourceManager doesn't support copy construction.
317  explicit SourceManager(const SourceManager&);
318  void operator=(const SourceManager&);
319public:
320  SourceManager() : LineTable(0), NumLinearScans(0), NumBinaryProbes(0) {
321    clearIDTables();
322  }
323  ~SourceManager();
324
325  void clearIDTables();
326
327  //===--------------------------------------------------------------------===//
328  // MainFileID creation and querying methods.
329  //===--------------------------------------------------------------------===//
330
331  /// getMainFileID - Returns the FileID of the main source file.
332  FileID getMainFileID() const { return MainFileID; }
333
334  /// createMainFileID - Create the FileID for the main source file.
335  FileID createMainFileID(const FileEntry *SourceFile,
336                          SourceLocation IncludePos) {
337    assert(MainFileID.isInvalid() && "MainFileID already set!");
338    MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
339    return MainFileID;
340  }
341
342  //===--------------------------------------------------------------------===//
343  // Methods to create new FileID's and instantiations.
344  //===--------------------------------------------------------------------===//
345
346  /// createFileID - Create a new FileID that represents the specified file
347  /// being #included from the specified IncludePosition.  This returns 0 on
348  /// error and translates NULL into standard input.
349  FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
350                      SrcMgr::CharacteristicKind FileCharacter) {
351    const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
352    if (IR == 0) return FileID();    // Error opening file?
353    return createFileID(IR, IncludePos, FileCharacter);
354  }
355
356  /// createFileIDForMemBuffer - Create a new FileID that represents the
357  /// specified memory buffer.  This does no caching of the buffer and takes
358  /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
359  FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
360    return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
361                        SrcMgr::C_User);
362  }
363
364  /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
365  ///  that will represent the FileID for the main source.  One example
366  ///  of when this would be used is when the main source is read from STDIN.
367  FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
368    assert(MainFileID.isInvalid() && "MainFileID already set!");
369    MainFileID = createFileIDForMemBuffer(Buffer);
370    return MainFileID;
371  }
372
373  /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
374  /// that a token at Loc should actually be referenced from InstantiationLoc.
375  /// TokLength is the length of the token being instantiated.
376  SourceLocation createInstantiationLoc(SourceLocation Loc,
377                                        SourceLocation InstantiationLocStart,
378                                        SourceLocation InstantiationLocEnd,
379                                        unsigned TokLength);
380
381  //===--------------------------------------------------------------------===//
382  // FileID manipulation methods.
383  //===--------------------------------------------------------------------===//
384
385  /// getBuffer - Return the buffer for the specified FileID.
386  ///
387  const llvm::MemoryBuffer *getBuffer(FileID FID) const {
388    return getSLocEntry(FID).getFile().getContentCache()->getBuffer();
389  }
390
391  /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
392  const FileEntry *getFileEntryForID(FileID FID) const {
393    return getSLocEntry(FID).getFile().getContentCache()->Entry;
394  }
395
396  /// getBufferData - Return a pointer to the start and end of the source buffer
397  /// data for the specified FileID.
398  std::pair<const char*, const char*> getBufferData(FileID FID) const;
399
400
401  //===--------------------------------------------------------------------===//
402  // SourceLocation manipulation methods.
403  //===--------------------------------------------------------------------===//
404
405  /// getFileIDSlow - Return the FileID for a SourceLocation.  This is a very
406  /// hot method that is used for all SourceManager queries that start with a
407  /// SourceLocation object.  It is responsible for finding the entry in
408  /// SLocEntryTable which contains the specified location.
409  ///
410  FileID getFileID(SourceLocation SpellingLoc) const {
411    unsigned SLocOffset = SpellingLoc.getOffset();
412
413    // If our one-entry cache covers this offset, just return it.
414    if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
415      return LastFileIDLookup;
416
417    return getFileIDSlow(SLocOffset);
418  }
419
420  /// getLocForStartOfFile - Return the source location corresponding to the
421  /// first byte of the specified file.
422  SourceLocation getLocForStartOfFile(FileID FID) const {
423    assert(FID.ID < SLocEntryTable.size() && SLocEntryTable[FID.ID].isFile());
424    unsigned FileOffset = SLocEntryTable[FID.ID].getOffset();
425    return SourceLocation::getFileLoc(FileOffset);
426  }
427
428  /// getInstantiationLoc - Given a SourceLocation object, return the
429  /// instantiation location referenced by the ID.
430  SourceLocation getInstantiationLoc(SourceLocation Loc) const {
431    // Handle the non-mapped case inline, defer to out of line code to handle
432    // instantiations.
433    if (Loc.isFileID()) return Loc;
434    return getInstantiationLocSlowCase(Loc);
435  }
436
437  /// getImmediateInstantiationRange - Loc is required to be an instantiation
438  /// location.  Return the start/end of the instantiation information.
439  std::pair<SourceLocation,SourceLocation>
440  getImmediateInstantiationRange(SourceLocation Loc) const;
441
442  /// getInstantiationRange - Given a SourceLocation object, return the
443  /// range of tokens covered by the instantiation in the ultimate file.
444  std::pair<SourceLocation,SourceLocation>
445  getInstantiationRange(SourceLocation Loc) const;
446
447
448  /// getSpellingLoc - Given a SourceLocation object, return the spelling
449  /// location referenced by the ID.  This is the place where the characters
450  /// that make up the lexed token can be found.
451  SourceLocation getSpellingLoc(SourceLocation Loc) const {
452    // Handle the non-mapped case inline, defer to out of line code to handle
453    // instantiations.
454    if (Loc.isFileID()) return Loc;
455    return getSpellingLocSlowCase(Loc);
456  }
457
458  /// getImmediateSpellingLoc - Given a SourceLocation object, return the
459  /// spelling location referenced by the ID.  This is the first level down
460  /// towards the place where the characters that make up the lexed token can be
461  /// found.  This should not generally be used by clients.
462  SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
463
464  /// getDecomposedLoc - Decompose the specified location into a raw FileID +
465  /// Offset pair.  The first element is the FileID, the second is the
466  /// offset from the start of the buffer of the location.
467  std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
468    FileID FID = getFileID(Loc);
469    return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
470  }
471
472  /// getDecomposedInstantiationLoc - Decompose the specified location into a
473  /// raw FileID + Offset pair.  If the location is an instantiation record,
474  /// walk through it until we find the final location instantiated.
475  std::pair<FileID, unsigned>
476  getDecomposedInstantiationLoc(SourceLocation Loc) const {
477    FileID FID = getFileID(Loc);
478    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
479
480    unsigned Offset = Loc.getOffset()-E->getOffset();
481    if (Loc.isFileID())
482      return std::make_pair(FID, Offset);
483
484    return getDecomposedInstantiationLocSlowCase(E, Offset);
485  }
486
487  /// getDecomposedSpellingLoc - Decompose the specified location into a raw
488  /// FileID + Offset pair.  If the location is an instantiation record, walk
489  /// through it until we find its spelling record.
490  std::pair<FileID, unsigned>
491  getDecomposedSpellingLoc(SourceLocation Loc) const {
492    FileID FID = getFileID(Loc);
493    const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
494
495    unsigned Offset = Loc.getOffset()-E->getOffset();
496    if (Loc.isFileID())
497      return std::make_pair(FID, Offset);
498    return getDecomposedSpellingLocSlowCase(E, Offset);
499  }
500
501  /// getFileOffset - This method returns the offset from the start
502  /// of the file that the specified SourceLocation represents. This is not very
503  /// meaningful for a macro ID.
504  unsigned getFileOffset(SourceLocation SpellingLoc) const {
505    return getDecomposedLoc(SpellingLoc).second;
506  }
507
508
509  //===--------------------------------------------------------------------===//
510  // Queries about the code at a SourceLocation.
511  //===--------------------------------------------------------------------===//
512
513  /// getCharacterData - Return a pointer to the start of the specified location
514  /// in the appropriate spelling MemoryBuffer.
515  const char *getCharacterData(SourceLocation SL) const;
516
517  /// getColumnNumber - Return the column # for the specified file position.
518  /// This is significantly cheaper to compute than the line number.  This
519  /// returns zero if the column number isn't known.  This may only be called on
520  /// a file sloc, so you must choose a spelling or instantiation location
521  /// before calling this method.
522  unsigned getColumnNumber(FileID FID, unsigned FilePos) const;
523  unsigned getSpellingColumnNumber(SourceLocation Loc) const;
524  unsigned getInstantiationColumnNumber(SourceLocation Loc) const;
525
526
527  /// getLineNumber - Given a SourceLocation, return the spelling line number
528  /// for the position indicated.  This requires building and caching a table of
529  /// line offsets for the MemoryBuffer, so this is not cheap: use only when
530  /// about to emit a diagnostic.
531  unsigned getLineNumber(FileID FID, unsigned FilePos) const;
532
533  unsigned getInstantiationLineNumber(SourceLocation Loc) const;
534  unsigned getSpellingLineNumber(SourceLocation Loc) const;
535
536  /// Return the filename or buffer identifier of the buffer the location is in.
537  /// Note that this name does not respect #line directives.  Use getPresumedLoc
538  /// for normal clients.
539  const char *getBufferName(SourceLocation Loc) const;
540
541  /// getFileCharacteristic - return the file characteristic of the specified
542  /// source location, indicating whether this is a normal file, a system
543  /// header, or an "implicit extern C" system header.
544  ///
545  /// This state can be modified with flags on GNU linemarker directives like:
546  ///   # 4 "foo.h" 3
547  /// which changes all source locations in the current file after that to be
548  /// considered to be from a system header.
549  SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
550
551  /// getPresumedLoc - This method returns the "presumed" location of a
552  /// SourceLocation specifies.  A "presumed location" can be modified by #line
553  /// or GNU line marker directives.  This provides a view on the data that a
554  /// user should see in diagnostics, for example.
555  ///
556  /// Note that a presumed location is always given as the instantiation point
557  /// of an instantiation location, not at the spelling location.
558  PresumedLoc getPresumedLoc(SourceLocation Loc) const;
559
560  /// isFromSameFile - Returns true if both SourceLocations correspond to
561  ///  the same file.
562  bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
563    return getFileID(Loc1) == getFileID(Loc2);
564  }
565
566  /// isFromMainFile - Returns true if the file of provided SourceLocation is
567  ///   the main file.
568  bool isFromMainFile(SourceLocation Loc) const {
569    return getFileID(Loc) == getMainFileID();
570  }
571
572  /// isInSystemHeader - Returns if a SourceLocation is in a system header.
573  bool isInSystemHeader(SourceLocation Loc) const {
574    return getFileCharacteristic(Loc) != SrcMgr::C_User;
575  }
576
577  //===--------------------------------------------------------------------===//
578  // Line Table Manipulation Routines
579  //===--------------------------------------------------------------------===//
580
581  /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
582  ///
583  unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
584
585  /// AddLineNote - Add a line note to the line table for the FileID and offset
586  /// specified by Loc.  If FilenameID is -1, it is considered to be
587  /// unspecified.
588  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
589  void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
590                   bool IsFileEntry, bool IsFileExit,
591                   bool IsSystemHeader, bool IsExternCHeader);
592
593  //===--------------------------------------------------------------------===//
594  // Other miscellaneous methods.
595  //===--------------------------------------------------------------------===//
596
597  // Iterators over FileInfos.
598  typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
599      ::const_iterator fileinfo_iterator;
600  fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
601  fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
602
603  /// PrintStats - Print statistics to stderr.
604  ///
605  void PrintStats() const;
606
607  /// Emit - Emit this SourceManager to Bitcode.
608  void Emit(llvm::Serializer& S) const;
609
610  /// Read - Reconstitute a SourceManager from Bitcode.
611  static SourceManager* CreateAndRegister(llvm::Deserializer& S,
612                                          FileManager &FMgr);
613
614private:
615  friend class SrcMgr::ContentCache; // Used for deserialization.
616
617  /// isOffsetInFileID - Return true if the specified FileID contains the
618  /// specified SourceLocation offset.  This is a very hot method.
619  inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
620    const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
621    // If the entry is after the offset, it can't contain it.
622    if (SLocOffset < Entry.getOffset()) return false;
623
624    // If this is the last entry than it does.  Otherwise, the entry after it
625    // has to not include it.
626    if (FID.ID+1 == SLocEntryTable.size()) return true;
627    return SLocOffset < SLocEntryTable[FID.ID+1].getOffset();
628  }
629
630  /// createFileID - Create a new fileID for the specified ContentCache and
631  ///  include position.  This works regardless of whether the ContentCache
632  ///  corresponds to a file or some other input source.
633  FileID createFileID(const SrcMgr::ContentCache* File,
634                      SourceLocation IncludePos,
635                      SrcMgr::CharacteristicKind DirCharacter);
636
637  const SrcMgr::ContentCache *
638    getOrCreateContentCache(const FileEntry *SourceFile);
639
640  /// createMemBufferContentCache - Create a new ContentCache for the specified
641  ///  memory buffer.
642  const SrcMgr::ContentCache*
643  createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
644
645  const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
646    assert(FID.ID < SLocEntryTable.size() && "Invalid id");
647    return SLocEntryTable[FID.ID];
648  }
649
650  FileID getFileIDSlow(unsigned SLocOffset) const;
651
652  SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
653  SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
654
655  std::pair<FileID, unsigned>
656  getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
657                                        unsigned Offset) const;
658  std::pair<FileID, unsigned>
659  getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
660                                   unsigned Offset) const;
661};
662
663
664}  // end namespace clang
665
666#endif
667