blob: 69fb74ca20c6f4365b375027a7e3cdde80ddacf7 [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
Argyrios Kyrtzidis1d876b72009-03-13 08:12:13 +000017#include <string>
Chris Lattner7b9ffe42009-03-13 16:09:24 +000018#include <vector>
19#include <cassert>
Chris Lattneraa739d22009-03-13 07:05:43 +000020
21namespace llvm {
22 class MemoryBuffer;
Chris Lattner1c8ae592009-03-13 16:01:53 +000023 class TGSourceMgr;
Chris Lattneraa739d22009-03-13 07:05:43 +000024
Chris Lattner1c8ae592009-03-13 16:01:53 +000025class TGLoc {
26 const char *Ptr;
27public:
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 Lattneraa739d22009-03-13 07:05:43 +000042
43/// TGSourceMgr - This owns the files read by tblgen, handles include stacks,
44/// and handles printing of diagnostics.
45class 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 Lattner1c8ae592009-03-13 16:01:53 +000052 TGLoc IncludeLoc;
Chris Lattneraa739d22009-03-13 07:05:43 +000053 };
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
60public:
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 Lattner1c8ae592009-03-13 16:01:53 +000074 TGLoc getParentIncludeLoc(unsigned i) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000075 assert(i < Buffers.size() && "Invalid Buffer ID!");
76 return Buffers[i].IncludeLoc;
77 }
78
Chris Lattner1c8ae592009-03-13 16:01:53 +000079 unsigned AddNewSourceBuffer(MemoryBuffer *F, TGLoc IncludeLoc) {
Chris Lattneraa739d22009-03-13 07:05:43 +000080 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 Lattner1c8ae592009-03-13 16:01:53 +000089 int FindBufferContainingLoc(TGLoc Loc) const;
Chris Lattneraa739d22009-03-13 07:05:43 +000090
91 /// FindLineNumber - Find the line number for the specified location in the
92 /// specified file. This is not a fast method.
Chris Lattner1c8ae592009-03-13 16:01:53 +000093 unsigned FindLineNumber(TGLoc Loc, int BufferID = -1) const;
Chris Lattneraa739d22009-03-13 07:05:43 +000094
95
96 /// PrintError - Emit an error message about the specified location with the
97 /// specified string.
Chris Lattner1c8ae592009-03-13 16:01:53 +000098 void PrintError(TGLoc ErrorLoc, const std::string &Msg) const;
Chris Lattneraa739d22009-03-13 07:05:43 +000099
100private:
Chris Lattner1c8ae592009-03-13 16:01:53 +0000101 void PrintIncludeStack(TGLoc IncludeLoc) const;
Chris Lattneraa739d22009-03-13 07:05:43 +0000102};
103
104} // end llvm namespace
105
106#endif