Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 1 | //===- TGSourceMgr.h - Manager for Source Buffers & Diagnostics -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the TGSourceMgr class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef TGSOURCEMGR_H |
| 15 | #define TGSOURCEMGR_H |
| 16 | |
Argyrios Kyrtzidis | 1d876b7 | 2009-03-13 08:12:13 +0000 | [diff] [blame] | 17 | #include <string> |
Chris Lattner | 7b9ffe4 | 2009-03-13 16:09:24 +0000 | [diff] [blame] | 18 | #include <vector> |
| 19 | #include <cassert> |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
| 22 | class MemoryBuffer; |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 23 | class TGSourceMgr; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 25 | class TGLoc { |
| 26 | const char *Ptr; |
| 27 | public: |
| 28 | TGLoc() : Ptr(0) {} |
| 29 | TGLoc(const TGLoc &RHS) : Ptr(RHS.Ptr) {} |
| 30 | |
| 31 | bool operator==(const TGLoc &RHS) const { return RHS.Ptr == Ptr; } |
| 32 | bool operator!=(const TGLoc &RHS) const { return RHS.Ptr != Ptr; } |
| 33 | |
| 34 | const char *getPointer() const { return Ptr; } |
| 35 | |
| 36 | static TGLoc getFromPointer(const char *Ptr) { |
| 37 | TGLoc L; |
| 38 | L.Ptr = Ptr; |
| 39 | return L; |
| 40 | } |
| 41 | }; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 42 | |
| 43 | /// TGSourceMgr - This owns the files read by tblgen, handles include stacks, |
| 44 | /// and handles printing of diagnostics. |
| 45 | class TGSourceMgr { |
| 46 | struct SrcBuffer { |
| 47 | /// Buffer - The memory buffer for the file. |
| 48 | MemoryBuffer *Buffer; |
| 49 | |
| 50 | /// IncludeLoc - This is the location of the parent include, or null if at |
| 51 | /// the top level. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 52 | TGLoc IncludeLoc; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /// Buffers - This is all of the buffers that we are reading from. |
| 56 | std::vector<SrcBuffer> Buffers; |
| 57 | |
| 58 | TGSourceMgr(const TGSourceMgr&); // DO NOT IMPLEMENT |
| 59 | void operator=(const TGSourceMgr&); // DO NOT IMPLEMENT |
| 60 | public: |
| 61 | TGSourceMgr() {} |
| 62 | ~TGSourceMgr(); |
| 63 | |
| 64 | const SrcBuffer &getBufferInfo(unsigned i) const { |
| 65 | assert(i < Buffers.size() && "Invalid Buffer ID!"); |
| 66 | return Buffers[i]; |
| 67 | } |
| 68 | |
| 69 | const MemoryBuffer *getMemoryBuffer(unsigned i) const { |
| 70 | assert(i < Buffers.size() && "Invalid Buffer ID!"); |
| 71 | return Buffers[i].Buffer; |
| 72 | } |
| 73 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 74 | TGLoc getParentIncludeLoc(unsigned i) const { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 75 | assert(i < Buffers.size() && "Invalid Buffer ID!"); |
| 76 | return Buffers[i].IncludeLoc; |
| 77 | } |
| 78 | |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 79 | unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLoc IncludeLoc) { |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 80 | SrcBuffer NB; |
| 81 | NB.Buffer = F; |
| 82 | NB.IncludeLoc = IncludeLoc; |
| 83 | Buffers.push_back(NB); |
| 84 | return Buffers.size()-1; |
| 85 | } |
| 86 | |
| 87 | /// FindBufferContainingLoc - Return the ID of the buffer containing the |
| 88 | /// specified location, returning -1 if not found. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 89 | int FindBufferContainingLoc(TGLoc Loc) const; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 90 | |
| 91 | /// FindLineNumber - Find the line number for the specified location in the |
| 92 | /// specified file. This is not a fast method. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 93 | unsigned FindLineNumber(TGLoc Loc, int BufferID = -1) const; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 94 | |
| 95 | |
| 96 | /// PrintError - Emit an error message about the specified location with the |
| 97 | /// specified string. |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 98 | void PrintError(TGLoc ErrorLoc, const std::string &Msg) const; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 99 | |
| 100 | private: |
Chris Lattner | 1c8ae59 | 2009-03-13 16:01:53 +0000 | [diff] [blame] | 101 | void PrintIncludeStack(TGLoc IncludeLoc) const; |
Chris Lattner | aa739d2 | 2009-03-13 07:05:43 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // end llvm namespace |
| 105 | |
| 106 | #endif |