Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / ada / output.adb
blob5ac680176b838c3e91efc3a356fa516fd8b16fe0
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- O U T P U T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 package body Output is
34 Current_FD : File_Descriptor := Standout;
35 -- File descriptor for current output
37 Special_Output_Proc : Output_Proc := null;
38 -- Record argument to last call to Set_Special_Output. If this is
39 -- non-null, then we are in special output mode.
41 Indentation_Amount : constant Positive := 3;
42 -- Number of spaces to output for each indentation level
44 Indentation_Limit : constant Positive := 40;
45 -- Indentation beyond this number of spaces wraps around
47 pragma Assert (Indentation_Limit < Buffer_Max / 2);
48 -- Make sure this is substantially shorter than the line length
50 Cur_Indentation : Natural := 0;
51 -- Number of spaces to indent each line
53 -----------------------
54 -- Local_Subprograms --
55 -----------------------
57 procedure Flush_Buffer;
58 -- Flush buffer if non-empty and reset column counter
60 ---------------------------
61 -- Cancel_Special_Output --
62 ---------------------------
64 procedure Cancel_Special_Output is
65 begin
66 Special_Output_Proc := null;
67 end Cancel_Special_Output;
69 ------------
70 -- Column --
71 ------------
73 function Column return Pos is
74 begin
75 return Pos (Next_Col);
76 end Column;
78 ------------------
79 -- Flush_Buffer --
80 ------------------
82 procedure Flush_Buffer is
83 Write_Error : exception;
84 -- Raised if Write fails
86 ------------------
87 -- Write_Buffer --
88 ------------------
90 procedure Write_Buffer (Buf : String);
91 -- Write out Buf, either using Special_Output_Proc, or the normal way
92 -- using Write. Raise Write_Error if Write fails (presumably due to disk
93 -- full). Write_Error is not used in the case of Special_Output_Proc.
95 procedure Write_Buffer (Buf : String) is
96 begin
97 -- If Special_Output_Proc has been set, then use it
99 if Special_Output_Proc /= null then
100 Special_Output_Proc.all (Buf);
102 -- If output is not set, then output to either standard output
103 -- or standard error.
105 elsif Write (Current_FD, Buf'Address, Buf'Length) /= Buf'Length then
106 raise Write_Error;
108 end if;
109 end Write_Buffer;
111 Len : constant Natural := Next_Col - 1;
113 -- Start of processing for Flush_Buffer
115 begin
116 if Len /= 0 then
117 begin
118 -- If there's no indentation, or if the line is too long with
119 -- indentation, or if it's a blank line, just write the buffer.
121 if Cur_Indentation = 0
122 or else Cur_Indentation + Len > Buffer_Max
123 or else Buffer (1 .. Len) = (1 => ASCII.LF)
124 then
125 Write_Buffer (Buffer (1 .. Len));
127 -- Otherwise, construct a new buffer with preceding spaces, and
128 -- write that.
130 else
131 declare
132 Indented_Buffer : constant String :=
133 (1 .. Cur_Indentation => ' ') &
134 Buffer (1 .. Len);
135 begin
136 Write_Buffer (Indented_Buffer);
137 end;
138 end if;
140 exception
141 when Write_Error =>
143 -- If there are errors with standard error just quit. Otherwise
144 -- set the output to standard error before reporting a failure
145 -- and quitting.
147 if Current_FD /= Standerr then
148 Current_FD := Standerr;
149 Next_Col := 1;
150 Write_Line ("fatal error: disk full");
151 end if;
153 OS_Exit (2);
154 end;
156 -- Buffer is now empty
158 Next_Col := 1;
159 end if;
160 end Flush_Buffer;
162 -------------------
163 -- Ignore_Output --
164 -------------------
166 procedure Ignore_Output (S : String) is
167 begin
168 null;
169 end Ignore_Output;
171 ------------
172 -- Indent --
173 ------------
175 procedure Indent is
176 begin
177 -- The "mod" in the following assignment is to cause a wrap around in
178 -- the case where there is too much indentation.
180 Cur_Indentation :=
181 (Cur_Indentation + Indentation_Amount) mod Indentation_Limit;
182 end Indent;
184 -------------
185 -- Outdent --
186 -------------
188 procedure Outdent is
189 begin
190 -- The "mod" here undoes the wrap around from Indent above
192 Cur_Indentation :=
193 (Cur_Indentation - Indentation_Amount) mod Indentation_Limit;
194 end Outdent;
196 ---------------------------
197 -- Restore_Output_Buffer --
198 ---------------------------
200 procedure Restore_Output_Buffer (S : Saved_Output_Buffer) is
201 begin
202 Next_Col := S.Next_Col;
203 Cur_Indentation := S.Cur_Indentation;
204 Buffer (1 .. Next_Col - 1) := S.Buffer (1 .. Next_Col - 1);
205 end Restore_Output_Buffer;
207 ------------------------
208 -- Save_Output_Buffer --
209 ------------------------
211 function Save_Output_Buffer return Saved_Output_Buffer is
212 S : Saved_Output_Buffer;
213 begin
214 S.Buffer (1 .. Next_Col - 1) := Buffer (1 .. Next_Col - 1);
215 S.Next_Col := Next_Col;
216 S.Cur_Indentation := Cur_Indentation;
217 Next_Col := 1;
218 Cur_Indentation := 0;
219 return S;
220 end Save_Output_Buffer;
222 ------------------------
223 -- Set_Special_Output --
224 ------------------------
226 procedure Set_Special_Output (P : Output_Proc) is
227 begin
228 Special_Output_Proc := P;
229 end Set_Special_Output;
231 ----------------
232 -- Set_Output --
233 ----------------
235 procedure Set_Output (FD : File_Descriptor) is
236 begin
237 if Special_Output_Proc = null then
238 Flush_Buffer;
239 end if;
241 Current_FD := FD;
242 end Set_Output;
244 ------------------------
245 -- Set_Standard_Error --
246 ------------------------
248 procedure Set_Standard_Error is
249 begin
250 Set_Output (Standerr);
251 end Set_Standard_Error;
253 -------------------------
254 -- Set_Standard_Output --
255 -------------------------
257 procedure Set_Standard_Output is
258 begin
259 Set_Output (Standout);
260 end Set_Standard_Output;
262 -------
263 -- w --
264 -------
266 procedure w (C : Character) is
267 begin
268 Write_Char (''');
269 Write_Char (C);
270 Write_Char (''');
271 Write_Eol;
272 end w;
274 procedure w (S : String) is
275 begin
276 Write_Str (S);
277 Write_Eol;
278 end w;
280 procedure w (V : Int) is
281 begin
282 Write_Int (V);
283 Write_Eol;
284 end w;
286 procedure w (B : Boolean) is
287 begin
288 if B then
289 w ("True");
290 else
291 w ("False");
292 end if;
293 end w;
295 procedure w (L : String; C : Character) is
296 begin
297 Write_Str (L);
298 Write_Char (' ');
299 w (C);
300 end w;
302 procedure w (L : String; S : String) is
303 begin
304 Write_Str (L);
305 Write_Char (' ');
306 w (S);
307 end w;
309 procedure w (L : String; V : Int) is
310 begin
311 Write_Str (L);
312 Write_Char (' ');
313 w (V);
314 end w;
316 procedure w (L : String; B : Boolean) is
317 begin
318 Write_Str (L);
319 Write_Char (' ');
320 w (B);
321 end w;
323 ----------------
324 -- Write_Char --
325 ----------------
327 procedure Write_Char (C : Character) is
328 begin
329 if Next_Col = Buffer'Length then
330 Write_Eol;
331 end if;
333 if C = ASCII.LF then
334 Write_Eol;
335 else
336 Buffer (Next_Col) := C;
337 Next_Col := Next_Col + 1;
338 end if;
339 end Write_Char;
341 ---------------
342 -- Write_Eol --
343 ---------------
345 procedure Write_Eol is
346 begin
347 -- Remove any trailing space
349 while Next_Col > 1 and then Buffer (Next_Col - 1) = ' ' loop
350 Next_Col := Next_Col - 1;
351 end loop;
353 Buffer (Next_Col) := ASCII.LF;
354 Next_Col := Next_Col + 1;
355 Flush_Buffer;
356 end Write_Eol;
358 ---------------------------
359 -- Write_Eol_Keep_Blanks --
360 ---------------------------
362 procedure Write_Eol_Keep_Blanks is
363 begin
364 Buffer (Next_Col) := ASCII.LF;
365 Next_Col := Next_Col + 1;
366 Flush_Buffer;
367 end Write_Eol_Keep_Blanks;
369 ----------------------
370 -- Write_Erase_Char --
371 ----------------------
373 procedure Write_Erase_Char (C : Character) is
374 begin
375 if Next_Col /= 1 and then Buffer (Next_Col - 1) = C then
376 Next_Col := Next_Col - 1;
377 end if;
378 end Write_Erase_Char;
380 ---------------
381 -- Write_Int --
382 ---------------
384 procedure Write_Int (Val : Int) is
385 begin
386 if Val < 0 then
387 Write_Char ('-');
388 Write_Int (-Val);
390 else
391 if Val > 9 then
392 Write_Int (Val / 10);
393 end if;
395 Write_Char (Character'Val ((Val mod 10) + Character'Pos ('0')));
396 end if;
397 end Write_Int;
399 ----------------
400 -- Write_Line --
401 ----------------
403 procedure Write_Line (S : String) is
404 begin
405 Write_Str (S);
406 Write_Eol;
407 end Write_Line;
409 ------------------
410 -- Write_Spaces --
411 ------------------
413 procedure Write_Spaces (N : Nat) is
414 begin
415 for J in 1 .. N loop
416 Write_Char (' ');
417 end loop;
418 end Write_Spaces;
420 ---------------
421 -- Write_Str --
422 ---------------
424 procedure Write_Str (S : String) is
425 begin
426 for J in S'Range loop
427 Write_Char (S (J));
428 end loop;
429 end Write_Str;
431 end Output;