SourceMgr.h revision 1e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39
1//===- SourceMgr.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 SourceMgr class.  This class is used as a simple
11// substrate for diagnostics, #include handling, and other low level things for
12// simple parsers.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef TGSOURCEMGR_H
17#define TGSOURCEMGR_H
18
19#include <string>
20#include <vector>
21#include <cassert>
22
23namespace llvm {
24  class MemoryBuffer;
25  class TGSourceMgr;
26
27class SMLoc {
28  const char *Ptr;
29public:
30  SMLoc() : Ptr(0) {}
31  SMLoc(const SMLoc &RHS) : Ptr(RHS.Ptr) {}
32
33  bool operator==(const SMLoc &RHS) const { return RHS.Ptr == Ptr; }
34  bool operator!=(const SMLoc &RHS) const { return RHS.Ptr != Ptr; }
35
36  const char *getPointer() const { return Ptr; }
37
38  static SMLoc getFromPointer(const char *Ptr) {
39    SMLoc L;
40    L.Ptr = Ptr;
41    return L;
42  }
43};
44
45/// TGSourceMgr - This owns the files read by tblgen, handles include stacks,
46/// and handles printing of diagnostics.
47class TGSourceMgr {
48  struct SrcBuffer {
49    /// Buffer - The memory buffer for the file.
50    MemoryBuffer *Buffer;
51
52    /// IncludeLoc - This is the location of the parent include, or null if at
53    /// the top level.
54    SMLoc IncludeLoc;
55  };
56
57  /// Buffers - This is all of the buffers that we are reading from.
58  std::vector<SrcBuffer> Buffers;
59
60  TGSourceMgr(const TGSourceMgr&);    // DO NOT IMPLEMENT
61  void operator=(const TGSourceMgr&); // DO NOT IMPLEMENT
62public:
63  TGSourceMgr() {}
64  ~TGSourceMgr();
65
66  const SrcBuffer &getBufferInfo(unsigned i) const {
67    assert(i < Buffers.size() && "Invalid Buffer ID!");
68    return Buffers[i];
69  }
70
71  const MemoryBuffer *getMemoryBuffer(unsigned i) const {
72    assert(i < Buffers.size() && "Invalid Buffer ID!");
73    return Buffers[i].Buffer;
74  }
75
76  SMLoc getParentIncludeLoc(unsigned i) const {
77    assert(i < Buffers.size() && "Invalid Buffer ID!");
78    return Buffers[i].IncludeLoc;
79  }
80
81  unsigned AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc) {
82    SrcBuffer NB;
83    NB.Buffer = F;
84    NB.IncludeLoc = IncludeLoc;
85    Buffers.push_back(NB);
86    return Buffers.size()-1;
87  }
88
89  /// FindBufferContainingLoc - Return the ID of the buffer containing the
90  /// specified location, returning -1 if not found.
91  int FindBufferContainingLoc(SMLoc Loc) const;
92
93  /// FindLineNumber - Find the line number for the specified location in the
94  /// specified file.  This is not a fast method.
95  unsigned FindLineNumber(SMLoc Loc, int BufferID = -1) const;
96
97
98  /// PrintError - Emit an error message about the specified location with the
99  /// specified string.
100  void PrintError(SMLoc ErrorLoc, const std::string &Msg) const;
101
102private:
103  void PrintIncludeStack(SMLoc IncludeLoc) const;
104};
105
106}  // end llvm namespace
107
108#endif
109