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