Switch the asmprinter (.ll) and all the stuff it requires over to
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream --------------------------------===//
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 defines the raw_ostream class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
15 #define LLVM_SUPPORT_RAW_OSTREAM_H
16
17 #include "llvm/ADT/StringExtras.h"
18 #include <cassert>
19 #include <cstring>
20 #include <string>
21 #include <iosfwd>
22
23 namespace llvm {
24   class format_object_base;
25   
26 /// raw_ostream - This class implements an extremely fast bulk output stream
27 /// that can *only* output to a stream.  It does not support seeking, reopening,
28 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
29 /// a chunk at a time.
30 class raw_ostream {
31 protected:
32   char *OutBufStart, *OutBufEnd, *OutBufCur;
33 public:
34   raw_ostream() {
35     // Start out ready to flush.
36     OutBufStart = OutBufEnd = OutBufCur = 0;
37   }
38   virtual ~raw_ostream() {}
39   
40   //===--------------------------------------------------------------------===//
41   // Configuration Interface
42   //===--------------------------------------------------------------------===//
43   
44   /// SetBufferSize - Set the internal buffer size to the specified amount
45   /// instead of the default.
46   void SetBufferSize(unsigned Size) {
47     assert(Size >= 64 &&
48            "Buffer size must be somewhat large for invariants to hold");
49     flush();
50     
51     delete [] OutBufStart;
52     OutBufStart = new char[Size];
53     OutBufEnd = OutBufStart+Size;
54     OutBufCur = OutBufStart;
55   }
56   
57   //===--------------------------------------------------------------------===//
58   // Data Output Interface
59   //===--------------------------------------------------------------------===//
60   
61   void flush() {
62     if (OutBufCur != OutBufStart)
63       flush_impl();
64   }
65   
66   raw_ostream &operator<<(char C) {
67     if (OutBufCur >= OutBufEnd)
68       flush_impl();
69     *OutBufCur++ = C;
70     return *this;
71   }
72   
73   raw_ostream &operator<<(unsigned char C) {
74     if (OutBufCur >= OutBufEnd)
75       flush_impl();
76     *OutBufCur++ = C;
77     return *this;
78   }
79   
80   raw_ostream &operator<<(signed char C) {
81     if (OutBufCur >= OutBufEnd)
82       flush_impl();
83     *OutBufCur++ = C;
84     return *this;
85   }
86   
87   raw_ostream &operator<<(const char *Str) {
88     return write(Str, strlen(Str));
89   }
90   
91   raw_ostream &operator<<(const std::string& Str) {
92     return write(Str.data(), Str.length());
93   }
94   
95   raw_ostream &operator<<(unsigned long N);
96   raw_ostream &operator<<(long N);
97   raw_ostream &operator<<(unsigned long long N);
98   raw_ostream &operator<<(long long N);
99   raw_ostream &operator<<(const void *P);
100   raw_ostream &operator<<(unsigned int N) {
101     return this->operator<<(static_cast<unsigned long>(N));
102   }
103   
104   raw_ostream &operator<<(int N) {
105     return this->operator<<(static_cast<long>(N));
106   }
107
108   raw_ostream &operator<<(double N) {
109     return this->operator<<(ftostr(N));
110   }
111   
112   raw_ostream &write(const char *Ptr, unsigned Size);
113   
114   // Formatted output, see the format() function in Support/Format.h.
115   raw_ostream &operator<<(const format_object_base &Fmt);
116   
117   //===--------------------------------------------------------------------===//
118   // Subclass Interface
119   //===--------------------------------------------------------------------===//
120
121 protected:
122   
123   /// flush_impl - The is the piece of the class that is implemented by
124   /// subclasses.  This outputs the currently buffered data and resets the
125   /// buffer to empty.
126   virtual void flush_impl() = 0;
127   
128   /// HandleFlush - A stream's implementation of flush should call this after
129   /// emitting the bytes to the data sink.
130   void HandleFlush() {
131     if (OutBufStart == 0)
132       SetBufferSize(4096);
133     OutBufCur = OutBufStart;
134   }
135 private:
136   // An out of line virtual method to provide a home for the class vtable.
137   virtual void handle();
138 };
139   
140 //===----------------------------------------------------------------------===//
141 // File Output Streams
142 //===----------------------------------------------------------------------===//
143   
144 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
145 ///
146 class raw_fd_ostream : public raw_ostream {
147   int FD;
148   bool ShouldClose;
149 public:
150   /// raw_fd_ostream - Open the specified file for writing.  If an error occurs,
151   /// information about the error is put into ErrorInfo, and the stream should
152   /// be immediately destroyed.
153   raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
154   
155   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
156   /// ShouldClose is true, this closes the file when 
157   raw_fd_ostream(int fd, bool shouldClose) : FD(fd), ShouldClose(shouldClose) {}
158   
159   ~raw_fd_ostream();
160     
161   /// flush_impl - The is the piece of the class that is implemented by
162   /// subclasses.  This outputs the currently buffered data and resets the
163   /// buffer to empty.
164   virtual void flush_impl();
165 };
166   
167 /// raw_stdout_ostream - This is a stream that always prints to stdout.
168 ///
169 class raw_stdout_ostream : public raw_fd_ostream {
170   // An out of line virtual method to provide a home for the class vtable.
171   virtual void handle();
172 public:
173   raw_stdout_ostream();
174 };
175
176 /// raw_stderr_ostream - This is a stream that always prints to stderr.
177 ///
178 class raw_stderr_ostream : public raw_fd_ostream {
179   // An out of line virtual method to provide a home for the class vtable.
180   virtual void handle();
181 public:
182   raw_stderr_ostream();
183 };
184   
185 /// outs() - This returns a reference to a raw_ostream for standard output.
186 /// Use it like: outs() << "foo" << "bar";
187 raw_ostream &outs();
188
189 /// errs() - This returns a reference to a raw_ostream for standard error.
190 /// Use it like: errs() << "foo" << "bar";
191 raw_ostream &errs();
192   
193   
194 //===----------------------------------------------------------------------===//
195 // Bridge Output Streams
196 //===----------------------------------------------------------------------===//
197   
198 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
199 /// simple adaptor class.
200 class raw_os_ostream : public raw_ostream {
201   std::ostream &OS;
202 public:
203   raw_os_ostream(std::ostream &O) : OS(O) {}
204   ~raw_os_ostream();
205
206   /// flush_impl - The is the piece of the class that is implemented by
207   /// subclasses.  This outputs the currently buffered data and resets the
208   /// buffer to empty.
209   virtual void flush_impl();
210 };
211   
212 } // end llvm namespace
213
214 #endif