blob: 33d3c1091527acbf624d5f4a5ba27461c7b420b0 [file] [log] [blame]
Chris Lattneraa739d22009-03-13 07:05:43 +00001//===- 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
Evan Cheng79a5cef2009-03-13 07:41:30 +000017#include <cassert>
Chris Lattneraa739d22009-03-13 07:05:43 +000018#include <vector>
19
20namespace llvm {
21 class MemoryBuffer;
22
23/// FIXME: Make this a struct that is opaque.
24typedef const char *TGLocTy;
25
26/// TGSourceMgr - This owns the files read by tblgen, handles include stacks,
27/// and handles printing of diagnostics.
28class TGSourceMgr {
29 struct SrcBuffer {
30 /// Buffer - The memory buffer for the file.
31 MemoryBuffer *Buffer;
32
33 /// IncludeLoc - This is the location of the parent include, or null if at
34 /// the top level.
35 TGLocTy IncludeLoc;
36 };
37
38 /// Buffers - This is all of the buffers that we are reading from.
39 std::vector<SrcBuffer> Buffers;
40
41 TGSourceMgr(const TGSourceMgr&); // DO NOT IMPLEMENT
42 void operator=(const TGSourceMgr&); // DO NOT IMPLEMENT
43public:
44 TGSourceMgr() {}
45 ~TGSourceMgr();
46
47 const SrcBuffer &getBufferInfo(unsigned i) const {
48 assert(i < Buffers.size() && "Invalid Buffer ID!");
49 return Buffers[i];
50 }
51
52 const MemoryBuffer *getMemoryBuffer(unsigned i) const {
53 assert(i < Buffers.size() && "Invalid Buffer ID!");
54 return Buffers[i].Buffer;
55 }
56
57 TGLocTy getParentIncludeLoc(unsigned i) const {
58 assert(i < Buffers.size() && "Invalid Buffer ID!");
59 return Buffers[i].IncludeLoc;
60 }
61
62 unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLocTy IncludeLoc) {
63 SrcBuffer NB;
64 NB.Buffer = F;
65 NB.IncludeLoc = IncludeLoc;
66 Buffers.push_back(NB);
67 return Buffers.size()-1;
68 }
69
70 /// FindBufferContainingLoc - Return the ID of the buffer containing the
71 /// specified location, returning -1 if not found.
72 int FindBufferContainingLoc(TGLocTy Loc) const;
73
74 /// FindLineNumber - Find the line number for the specified location in the
75 /// specified file. This is not a fast method.
76 unsigned FindLineNumber(TGLocTy Loc, int BufferID = -1) const;
77
78
79 /// PrintError - Emit an error message about the specified location with the
80 /// specified string.
81 void PrintError(TGLocTy ErrorLoc, const std::string &Msg) const;
82
83private:
84 void PrintIncludeStack(TGLocTy IncludeLoc) const;
85};
86
87} // end llvm namespace
88
89#endif