make "locations" a class instead of a typedef.
[oota-llvm.git] / utils / TableGen / TGSourceMgr.h
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
17 #include <cassert>
18 #include <vector>
19 #include <string>
20
21 namespace llvm {
22   class MemoryBuffer;
23   class TGSourceMgr;
24   
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 };
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.
52     TGLoc IncludeLoc;
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   
74   TGLoc getParentIncludeLoc(unsigned i) const {
75     assert(i < Buffers.size() && "Invalid Buffer ID!");
76     return Buffers[i].IncludeLoc;
77   }
78   
79   unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLoc IncludeLoc) {
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.
89   int FindBufferContainingLoc(TGLoc Loc) const;
90   
91   /// FindLineNumber - Find the line number for the specified location in the
92   /// specified file.  This is not a fast method.
93   unsigned FindLineNumber(TGLoc Loc, int BufferID = -1) const;
94   
95   
96   /// PrintError - Emit an error message about the specified location with the
97   /// specified string.
98   void PrintError(TGLoc ErrorLoc, const std::string &Msg) const;
99   
100 private:
101   void PrintIncludeStack(TGLoc IncludeLoc) const;
102 };
103   
104 }  // end llvm namespace
105
106 #endif