Add llvm_start_multithreaded(), which starts up the LLVM internals in thread-safe...
[oota-llvm.git] / include / llvm / Support / raw_ostream.h
1 //===--- raw_ostream.h - Raw output stream ----------------------*- 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 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   template <typename T>
26   class SmallVectorImpl;
27
28 /// raw_ostream - This class implements an extremely fast bulk output stream
29 /// that can *only* output to a stream.  It does not support seeking, reopening,
30 /// rewinding, line buffered disciplines etc. It is a simple buffer that outputs
31 /// a chunk at a time.
32 class raw_ostream {
33 private:
34   /// The buffer is handled in such a way that the buffer is
35   /// uninitialized, unbuffered, or out of space when OutBufCur >=
36   /// OutBufEnd. Thus a single comparison suffices to determine if we
37   /// need to take the slow path to write a single character.
38   ///
39   /// The buffer is in one of three states:
40   ///  1. Unbuffered (Unbuffered == true)
41   ///  1. Uninitialized (Unbuffered == false && OutBufStart == 0).
42   ///  2. Buffered (Unbuffered == false && OutBufStart != 0 &&
43   ///               OutBufEnd - OutBufStart >= 64).
44   char *OutBufStart, *OutBufEnd, *OutBufCur;
45   bool Unbuffered;
46
47 public:
48   explicit raw_ostream(bool unbuffered=false) : Unbuffered(unbuffered) {
49     // Start out ready to flush.
50     OutBufStart = OutBufEnd = OutBufCur = 0;
51   }
52
53   virtual ~raw_ostream() {
54     delete [] OutBufStart;
55   }
56
57   /// tell - Return the current offset with the file.
58   uint64_t tell() { return current_pos() + GetNumBytesInBuffer(); }
59
60   //===--------------------------------------------------------------------===//
61   // Configuration Interface
62   //===--------------------------------------------------------------------===//
63
64   /// SetBufferSize - Set the internal buffer size to the specified amount
65   /// instead of the default.
66   void SetBufferSize(unsigned Size=4096) {
67     assert(Size >= 64 &&
68            "Buffer size must be somewhat large for invariants to hold");
69     flush();
70
71     delete [] OutBufStart;
72     OutBufStart = new char[Size];
73     OutBufEnd = OutBufStart+Size;
74     OutBufCur = OutBufStart;
75     Unbuffered = false;
76   }
77
78   /// SetUnbuffered - Set the streams buffering status. When
79   /// unbuffered the stream will flush after every write. This routine
80   /// will also flush the buffer immediately when the stream is being
81   /// set to unbuffered.
82   void SetUnbuffered() {
83     flush();
84     
85     delete [] OutBufStart;
86     OutBufStart = OutBufEnd = OutBufCur = 0;
87     Unbuffered = true;
88   }
89
90   unsigned GetNumBytesInBuffer() const {
91     return OutBufCur - OutBufStart;
92   }
93
94   //===--------------------------------------------------------------------===//
95   // Data Output Interface
96   //===--------------------------------------------------------------------===//
97
98   void flush() {
99     if (OutBufCur != OutBufStart)
100       flush_nonempty();
101   }
102
103   raw_ostream &operator<<(char C) {
104     if (OutBufCur >= OutBufEnd)
105       return write(C);
106     *OutBufCur++ = C;
107     return *this;
108   }
109
110   raw_ostream &operator<<(unsigned char C) {
111     if (OutBufCur >= OutBufEnd)
112       return write(C);
113     *OutBufCur++ = C;
114     return *this;
115   }
116
117   raw_ostream &operator<<(signed char C) {
118     if (OutBufCur >= OutBufEnd)
119       return write(C);
120     *OutBufCur++ = C;
121     return *this;
122   }
123
124   raw_ostream &operator<<(const char *Str) {
125     // Inline fast path, particulary for constant strings where a
126     // sufficiently smart compiler will simplify strlen.
127
128     unsigned Size = strlen(Str);
129
130     // Make sure we can use the fast path.
131     if (OutBufCur+Size > OutBufEnd)
132       return write(Str, Size);
133
134     memcpy(OutBufCur, Str, Size);
135     OutBufCur += Size;
136     return *this;
137   }
138
139   raw_ostream &operator<<(const std::string& Str) {
140     write(Str.data(), Str.length());
141     return *this;
142   }
143
144   raw_ostream &operator<<(unsigned long N);
145   raw_ostream &operator<<(long N);
146   raw_ostream &operator<<(unsigned long long N);
147   raw_ostream &operator<<(long long N);
148   raw_ostream &operator<<(const void *P);
149   raw_ostream &operator<<(unsigned int N) {
150     this->operator<<(static_cast<unsigned long>(N));
151     return *this;
152   }
153
154   raw_ostream &operator<<(int N) {
155     this->operator<<(static_cast<long>(N));
156     return *this;
157   }
158
159   raw_ostream &operator<<(double N) {
160     this->operator<<(ftostr(N));
161     return *this;
162   }
163
164   raw_ostream &write(unsigned char C);
165   raw_ostream &write(const char *Ptr, unsigned Size);
166
167   // Formatted output, see the format() function in Support/Format.h.
168   raw_ostream &operator<<(const format_object_base &Fmt);
169
170   //===--------------------------------------------------------------------===//
171   // Subclass Interface
172   //===--------------------------------------------------------------------===//
173
174 private:
175   /// write_impl - The is the piece of the class that is implemented
176   /// by subclasses.  This writes the \args Size bytes starting at
177   /// \arg Ptr to the underlying stream.
178   /// 
179   /// \invariant { Size > 0 }
180   virtual void write_impl(const char *Ptr, unsigned Size) = 0;
181
182   // An out of line virtual method to provide a home for the class vtable.
183   virtual void handle();
184
185   /// current_pos - Return the current position within the stream, not
186   /// counting the bytes currently in the buffer.
187   virtual uint64_t current_pos() = 0;
188
189   //===--------------------------------------------------------------------===//
190   // Private Interface
191   //===--------------------------------------------------------------------===//
192 private:
193   /// flush_nonempty - Flush the current buffer, which is known to be
194   /// non-empty. This outputs the currently buffered data and resets
195   /// the buffer to empty.
196   void flush_nonempty();
197 };
198
199 //===----------------------------------------------------------------------===//
200 // File Output Streams
201 //===----------------------------------------------------------------------===//
202
203 /// raw_fd_ostream - A raw_ostream that writes to a file descriptor.
204 ///
205 class raw_fd_ostream : public raw_ostream {
206   int FD;
207   bool ShouldClose;
208   uint64_t pos;
209
210   /// write_impl - See raw_ostream::write_impl.
211   virtual void write_impl(const char *Ptr, unsigned Size);
212
213   /// current_pos - Return the current position within the stream, not
214   /// counting the bytes currently in the buffer.
215   virtual uint64_t current_pos() { return pos; }
216
217 public:
218   /// raw_fd_ostream - Open the specified file for writing. If an
219   /// error occurs, information about the error is put into ErrorInfo,
220   /// and the stream should be immediately destroyed; the string will
221   /// be empty if no error occurred.
222   ///
223   /// \param Filename - The file to open. If this is "-" then the
224   /// stream will use stdout instead.
225   /// \param Binary - The file should be opened in binary mode on
226   /// platforms that support this distinction.
227   raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
228
229   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
230   /// ShouldClose is true, this closes the file when the stream is destroyed.
231   raw_fd_ostream(int fd, bool shouldClose, 
232                  bool unbuffered=false) : raw_ostream(unbuffered), FD(fd), 
233                                           ShouldClose(shouldClose) {}
234   
235   ~raw_fd_ostream();
236
237   /// close - Manually flush the stream and close the file.
238   void close();
239
240   /// tell - Return the current offset with the file.
241   uint64_t tell() { return pos + GetNumBytesInBuffer(); }
242
243   /// seek - Flushes the stream and repositions the underlying file descriptor
244   ///  positition to the offset specified from the beginning of the file.
245   uint64_t seek(uint64_t off);
246 };
247
248 /// raw_stdout_ostream - This is a stream that always prints to stdout.
249 ///
250 class raw_stdout_ostream : public raw_fd_ostream {
251   // An out of line virtual method to provide a home for the class vtable.
252   virtual void handle();
253 public:
254   raw_stdout_ostream();
255 };
256
257 /// raw_stderr_ostream - This is a stream that always prints to stderr.
258 ///
259 class raw_stderr_ostream : public raw_fd_ostream {
260   // An out of line virtual method to provide a home for the class vtable.
261   virtual void handle();
262 public:
263   raw_stderr_ostream();
264 };
265
266 /// outs() - This returns a reference to a raw_ostream for standard output.
267 /// Use it like: outs() << "foo" << "bar";
268 raw_ostream &outs();
269
270 /// errs() - This returns a reference to a raw_ostream for standard error.
271 /// Use it like: errs() << "foo" << "bar";
272 raw_ostream &errs();
273
274
275 //===----------------------------------------------------------------------===//
276 // Output Stream Adaptors
277 //===----------------------------------------------------------------------===//
278
279 /// raw_os_ostream - A raw_ostream that writes to an std::ostream.  This is a
280 /// simple adaptor class.
281 class raw_os_ostream : public raw_ostream {
282   std::ostream &OS;
283
284   /// write_impl - See raw_ostream::write_impl.
285   virtual void write_impl(const char *Ptr, unsigned Size);
286
287   /// current_pos - Return the current position within the stream, not
288   /// counting the bytes currently in the buffer.
289   virtual uint64_t current_pos();
290
291 public:
292   raw_os_ostream(std::ostream &O) : OS(O) {}
293   ~raw_os_ostream();
294
295   /// tell - Return the current offset with the stream.
296   uint64_t tell();
297 };
298
299 /// raw_string_ostream - A raw_ostream that writes to an std::string.  This is a
300 /// simple adaptor class.
301 class raw_string_ostream : public raw_ostream {
302   std::string &OS;
303
304   /// write_impl - See raw_ostream::write_impl.
305   virtual void write_impl(const char *Ptr, unsigned Size);
306
307   /// current_pos - Return the current position within the stream, not
308   /// counting the bytes currently in the buffer.
309   virtual uint64_t current_pos() { return OS.size(); }
310 public:
311   raw_string_ostream(std::string &O) : OS(O) {}
312   ~raw_string_ostream();
313
314   /// tell - Return the current offset with the stream.
315   uint64_t tell() { return OS.size() + GetNumBytesInBuffer(); }
316
317   /// str - Flushes the stream contents to the target string and returns
318   ///  the string's reference.
319   std::string& str() {
320     flush();
321     return OS;
322   }
323 };
324
325 /// raw_svector_ostream - A raw_ostream that writes to an SmallVector or
326 /// SmallString.  This is a simple adaptor class.
327 class raw_svector_ostream : public raw_ostream {
328   SmallVectorImpl<char> &OS;
329
330   /// write_impl - See raw_ostream::write_impl.
331   virtual void write_impl(const char *Ptr, unsigned Size);
332
333   /// current_pos - Return the current position within the stream, not
334   /// counting the bytes currently in the buffer.
335   virtual uint64_t current_pos();
336 public:
337   raw_svector_ostream(SmallVectorImpl<char> &O) : OS(O) {}
338   ~raw_svector_ostream();
339
340   /// tell - Return the current offset with the stream.
341   uint64_t tell();
342 };
343
344 } // end llvm namespace
345
346 #endif