Reapply my previous asmprinter changes now with more testing and two
[oota-llvm.git] / include / llvm / Support / FormattedStream.h
1 //===-- llvm/CodeGen/FormattedStream.h - Formatted streams ------*- 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 contains raw_ostream implementations for streams to do
11 // things like pretty-print comments.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_FORMATTEDSTREAM_H
16 #define LLVM_SUPPORT_FORMATTEDSTREAM_H
17
18 #include "llvm/Support/raw_ostream.h"
19
20 namespace llvm 
21 {
22   /// formatted_raw_ostream - Formatted raw_fd_ostream to handle
23   /// asm-specific constructs.
24   ///
25   class formatted_raw_ostream : public raw_ostream {
26   private:
27     /// TheStream - The real stream we output to.
28     ///
29     raw_ostream &TheStream;
30
31     /// Column - The current output column of the stream.  The column
32     /// scheme is zero-based.
33     ///
34     unsigned Column;
35
36     virtual void write_impl(const char *Ptr, unsigned Size) {
37       ComputeColumn(Ptr, Size);
38       TheStream.write(Ptr, Size);
39     }
40
41     /// current_pos - Return the current position within the stream,
42     /// not counting the bytes currently in the buffer.
43     virtual uint64_t current_pos() { 
44       // This has the same effect as calling TheStream.current_pos(),
45       // but that interface is private.
46       return TheStream.tell() - TheStream.GetNumBytesInBuffer();
47     }
48
49     /// ComputeColumn - Examine the current output and figure out
50     /// which column we end up in after output.
51     ///
52     void ComputeColumn(const char *Ptr, unsigned Size);
53
54   public:
55     /// formatted_raw_ostream - Open the specified file for
56     /// writing. If an error occurs, information about the error is
57     /// put into ErrorInfo, and the stream should be immediately
58     /// destroyed; the string will be empty if no error occurred.
59     ///
60     /// \param Filename - The file to open. If this is "-" then the
61     /// stream will use stdout instead.
62     /// \param Binary - The file should be opened in binary mode on
63     /// platforms that support this distinction.
64     formatted_raw_ostream(raw_ostream &Stream) 
65         : raw_ostream(), TheStream(Stream), Column(0) {}
66
67     /// PadToColumn - Align the output to some column number.
68     ///
69     /// \param NewCol - The column to move to.
70     /// \param MinPad - The minimum space to give after the most
71     /// recent I/O, even if the current column + minpad > newcol.
72     ///
73     void PadToColumn(unsigned NewCol, unsigned MinPad = 0);
74   };
75 }
76
77 #endif