1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package contains low level output routines used by the compiler for
33 -- writing error messages and informational output. It is also used by the
34 -- debug source file output routines (see Sprint.Print_Debug_Line).
36 -- This unit is used by gnatcoll
37 pragma Warnings (Off, "*is an internal GNAT unit");
38 pragma Warnings (Off, "*use * instead");
40 with Hostparm; use Hostparm;
41 with Types; use Types;
43 with System.OS_Lib; use System.OS_Lib;
46 pragma Elaborate_Body;
48 type Output_Proc is access procedure (S : String);
49 -- This type is used for the Set_Special_Output procedure. If Output_Proc
50 -- is called, then instead of lines being written to standard error or
51 -- standard output, a call is made to the given procedure for each line,
52 -- passing the line with an end of line character (which is a single
53 -- ASCII.LF character, even in systems which normally use CR/LF or some
54 -- other sequence for line end).
60 procedure Set_Special_Output (P : Output_Proc);
61 -- Sets subsequent output to call procedure P. If P is null, then the call
62 -- cancels the effect of a previous call, reverting the output to standard
63 -- error or standard output depending on the mode at the time of previous
64 -- call. Any exception generated by by calls to P is simply propagated to
65 -- the caller of the routine causing the write operation.
67 procedure Cancel_Special_Output;
68 -- Cancels the effect of a call to Set_Special_Output, if any. The output
69 -- is then directed to standard error or standard output depending on the
70 -- last call to Set_Standard_Error or Set_Standard_Output. It is never an
71 -- error to call Cancel_Special_Output. It has the same effect as calling
72 -- Set_Special_Output (null).
74 procedure Ignore_Output (S : String);
75 -- Does nothing. To disable output, pass Ignore_Output'Access to
76 -- Set_Special_Output.
78 procedure Set_Standard_Error;
79 -- Sets subsequent output to appear on the standard error file (whatever
80 -- that might mean for the host operating system, if anything) when
81 -- no special output is in effect. When a special output is in effect,
82 -- the output will appear on standard error only after special output
83 -- has been cancelled.
85 procedure Set_Standard_Output;
86 -- Sets subsequent output to appear on the standard output file (whatever
87 -- that might mean for the host operating system, if anything) when no
88 -- special output is in effect. When a special output is in effect, the
89 -- output will appear on standard output only after special output has been
90 -- cancelled. Output to standard output is the default mode before any call
91 -- to either of the Set procedures.
93 procedure Set_Output (FD : File_Descriptor);
94 -- Sets subsequent output to appear on the given file descriptor when no
95 -- special output is in effect. When a special output is in effect, the
96 -- output will appear on the given file descriptor only after special
97 -- output has been cancelled.
100 -- Increases the current indentation level. Whenever a line is written
101 -- (triggered by Eol), an appropriate amount of whitespace is added to the
102 -- beginning of the line, wrapping around if it gets too long.
105 -- Decreases the current indentation level
107 procedure Write_Char (C : Character);
108 -- Write one character to the standard output file. If the character is LF,
109 -- this is equivalent to Write_Eol.
111 procedure Write_Erase_Char (C : Character);
112 -- If last character in buffer matches C, erase it, otherwise no effect
115 -- Write an end of line (whatever is required by the system in use, e.g.
116 -- CR/LF for DOS, or LF for Unix) to the standard output file. This routine
117 -- also empties the line buffer, actually writing it to the file. Note that
118 -- Write_Eol is the only routine that causes any actual output to be
119 -- written. Trailing spaces are removed.
121 procedure Write_Eol_Keep_Blanks;
122 -- Similar as Write_Eol, except that trailing spaces are not removed
124 procedure Write_Int (Val : Int);
125 -- Write an integer value with no leading blanks or zeroes. Negative values
126 -- are preceded by a minus sign).
128 procedure Write_Spaces (N : Nat);
131 procedure Write_Str (S : String);
132 -- Write a string of characters to the standard output file. Note that
133 -- end of line is normally handled separately using WRITE_EOL, but it is
134 -- allowable for the string to contain LF (but not CR) characters, which
135 -- are properly interpreted as end of line characters. The string may also
136 -- contain horizontal tab characters.
138 procedure Write_Line (S : String);
139 -- Equivalent to Write_Str (S) followed by Write_Eol;
141 function Column return Pos;
142 pragma Inline (Column);
143 -- Returns the number of the column about to be written (e.g. a value of 1
144 -- means the current line is empty).
146 -------------------------
147 -- Buffer Save/Restore --
148 -------------------------
150 -- This facility allows the current line buffer to be saved and restored
152 type Saved_Output_Buffer is private;
153 -- Type used for Save/Restore_Buffer
155 Buffer_Max : constant := Hostparm.Max_Line_Length;
156 -- Maximal size of a buffered output line
158 function Save_Output_Buffer return Saved_Output_Buffer;
159 -- Save current line buffer and reset line buffer to empty
161 procedure Restore_Output_Buffer (S : Saved_Output_Buffer);
162 -- Restore previously saved output buffer. The value in S is not affected
163 -- so it is legitimate to restore a buffer more than once.
165 --------------------------
166 -- Debugging Procedures --
167 --------------------------
169 -- The following procedures are intended only for debugging purposes,
170 -- for temporary insertion into the text in environments where a debugger
171 -- is not available. They all have non-standard very short lower case
172 -- names, precisely to make sure that they are only used for debugging!
174 procedure w (C : Character);
175 -- Dump quote, character, quote, followed by line return
177 procedure w (S : String);
178 -- Dump string followed by line return
180 procedure w (V : Int);
181 -- Dump integer followed by line return
183 procedure w (B : Boolean);
184 -- Dump Boolean followed by line return
186 procedure w (L : String; C : Character);
187 -- Dump contents of string followed by blank, quote, character, quote
189 procedure w (L : String; S : String);
190 -- Dump two strings separated by blanks, followed by line return
192 procedure w (L : String; V : Int);
193 -- Dump contents of string followed by blank, integer, line return
195 procedure w (L : String; B : Boolean);
196 -- Dump contents of string followed by blank, Boolean, line return
199 -- Note: the following buffer and column position are maintained by the
200 -- subprograms defined in this package, and cannot be directly modified or
201 -- accessed by a client.
203 Buffer : String (1 .. Buffer_Max + 1) := (others => '*');
204 for Buffer'Alignment use 4;
205 -- Buffer used to build output line. We do line buffering because it
206 -- is needed for the support of the debug-generated-code option (-gnatD).
207 -- Historically it was first added because on VMS, line buffering is
208 -- needed with certain file formats. So in any case line buffering must
209 -- be retained for this purpose, even if other reasons disappear. Note
210 -- any attempt to write more output to a line than can fit in the buffer
211 -- will be silently ignored. The alignment clause improves the efficiency
212 -- of the save/restore procedures.
214 Next_Col : Positive range 1 .. Buffer'Length + 1 := 1;
215 -- Column about to be written
217 type Saved_Output_Buffer is record
218 Buffer : String (1 .. Buffer_Max + 1);
220 Cur_Indentation : Natural;