libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc-boot / GPushBackInput.cc
blob9bc2a83abd4522978afa953cf6bcb813444138e4
1 /* do not edit automatically generated by mc from PushBackInput. */
2 /* PushBackInput.mod provides a method for pushing back and consuming input.
4 Copyright (C) 2001-2024 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
37 # if !defined (FALSE)
38 # define FALSE (1==0)
39 # endif
41 #if defined(__cplusplus)
42 # undef NULL
43 # define NULL 0
44 #endif
45 #define _PushBackInput_C
47 #include "GPushBackInput.h"
48 # include "GFIO.h"
49 # include "GDynamicStrings.h"
50 # include "GASCII.h"
51 # include "GDebug.h"
52 # include "GStrLib.h"
53 # include "GNumberIO.h"
54 # include "GStrIO.h"
55 # include "GStdIO.h"
56 # include "Glibc.h"
58 # define MaxPushBackStack 8192
59 # define MaxFileName 4096
60 typedef struct PushBackInput__T2_a PushBackInput__T2;
62 typedef struct PushBackInput__T3_a PushBackInput__T3;
64 struct PushBackInput__T2_a { char array[MaxFileName+1]; };
65 struct PushBackInput__T3_a { char array[MaxPushBackStack+1]; };
66 static PushBackInput__T2 FileName;
67 static PushBackInput__T3 CharStack;
68 static unsigned int ExitStatus;
69 static unsigned int Column;
70 static unsigned int StackPtr;
71 static unsigned int LineNo;
72 static bool Debugging;
75 Open - opens a file for reading.
78 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high);
81 GetCh - gets a character from either the push back stack or
82 from file, f.
85 extern "C" char PushBackInput_GetCh (FIO_File f);
88 PutCh - pushes a character onto the push back stack, it also
89 returns the character which has been pushed.
92 extern "C" char PushBackInput_PutCh (char ch);
95 PutString - pushes a string onto the push back stack.
98 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high);
101 PutStr - pushes a dynamic string onto the push back stack.
102 The string, s, is not deallocated.
105 extern "C" void PushBackInput_PutStr (DynamicStrings_String s);
108 Error - emits an error message with the appropriate file, line combination.
111 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high);
114 WarnError - emits an error message with the appropriate file, line combination.
115 It does not terminate but when the program finishes an exit status of
116 1 will be issued.
119 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high);
122 WarnString - emits an error message with the appropriate file, line combination.
123 It does not terminate but when the program finishes an exit status of
124 1 will be issued.
127 extern "C" void PushBackInput_WarnString (DynamicStrings_String s);
130 Close - closes the opened file.
133 extern "C" void PushBackInput_Close (FIO_File f);
136 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
139 extern "C" unsigned int PushBackInput_GetExitStatus (void);
142 SetDebug - sets the debug flag on or off.
145 extern "C" void PushBackInput_SetDebug (bool d);
148 GetColumnPosition - returns the column position of the current character.
151 extern "C" unsigned int PushBackInput_GetColumnPosition (void);
154 GetCurrentLine - returns the current line number.
157 extern "C" unsigned int PushBackInput_GetCurrentLine (void);
160 ErrChar - writes a char, ch, to stderr.
163 static void ErrChar (char ch);
166 Init - initialize global variables.
169 static void Init (void);
173 ErrChar - writes a char, ch, to stderr.
176 static void ErrChar (char ch)
178 FIO_WriteChar (FIO_StdErr, ch);
183 Init - initialize global variables.
186 static void Init (void)
188 ExitStatus = 0;
189 StackPtr = 0;
190 LineNo = 1;
191 Column = 0;
196 Open - opens a file for reading.
199 extern "C" FIO_File PushBackInput_Open (const char *a_, unsigned int _a_high)
201 char a[_a_high+1];
203 /* make a local copy of each unbounded array. */
204 memcpy (a, a_, _a_high+1);
206 Init ();
207 StrLib_StrCopy ((const char *) a, _a_high, (char *) &FileName.array[0], MaxFileName);
208 return FIO_OpenToRead ((const char *) a, _a_high);
209 /* static analysis guarentees a RETURN statement will be used before here. */
210 __builtin_unreachable ();
215 GetCh - gets a character from either the push back stack or
216 from file, f.
219 extern "C" char PushBackInput_GetCh (FIO_File f)
221 char ch;
223 if (StackPtr > 0)
225 StackPtr -= 1;
226 if (Debugging)
228 StdIO_Write (CharStack.array[StackPtr]);
230 return CharStack.array[StackPtr];
232 else
234 if ((FIO_EOF (f)) || (! (FIO_IsNoError (f))))
236 ch = ASCII_nul;
238 else
240 do {
241 ch = FIO_ReadChar (f);
242 } while (! (((ch != ASCII_cr) || (FIO_EOF (f))) || (! (FIO_IsNoError (f)))));
243 if (ch == ASCII_lf)
245 Column = 0;
246 LineNo += 1;
248 else
250 Column += 1;
253 if (Debugging)
255 StdIO_Write (ch);
257 return ch;
259 /* static analysis guarentees a RETURN statement will be used before here. */
260 __builtin_unreachable ();
265 PutCh - pushes a character onto the push back stack, it also
266 returns the character which has been pushed.
269 extern "C" char PushBackInput_PutCh (char ch)
271 if (StackPtr < MaxPushBackStack)
273 CharStack.array[StackPtr] = ch;
274 StackPtr += 1;
276 else
278 Debug_Halt ((const char *) "max push back stack exceeded, increase MaxPushBackStack", 55, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutCh", 5, 151);
280 return ch;
281 /* static analysis guarentees a RETURN statement will be used before here. */
282 __builtin_unreachable ();
287 PutString - pushes a string onto the push back stack.
290 extern "C" void PushBackInput_PutString (const char *a_, unsigned int _a_high)
292 unsigned int l;
293 char a[_a_high+1];
295 /* make a local copy of each unbounded array. */
296 memcpy (a, a_, _a_high+1);
298 l = StrLib_StrLen ((const char *) a, _a_high);
299 while (l > 0)
301 l -= 1;
302 if ((PushBackInput_PutCh (a[l])) != a[l])
304 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutString", 9, 132);
311 PutStr - pushes a dynamic string onto the push back stack.
312 The string, s, is not deallocated.
315 extern "C" void PushBackInput_PutStr (DynamicStrings_String s)
317 unsigned int i;
319 i = DynamicStrings_Length (s);
320 while (i > 0)
322 i -= 1;
323 if ((PushBackInput_PutCh (DynamicStrings_char (s, static_cast<int> (i)))) != (DynamicStrings_char (s, static_cast<int> (i))))
325 Debug_Halt ((const char *) "assert failed", 13, (const char *) "../../gcc/m2/gm2-libs/PushBackInput.mod", 39, (const char *) "PutStr", 6, 113);
332 Error - emits an error message with the appropriate file, line combination.
335 extern "C" void PushBackInput_Error (const char *a_, unsigned int _a_high)
337 char a[_a_high+1];
339 /* make a local copy of each unbounded array. */
340 memcpy (a, a_, _a_high+1);
342 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
343 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
344 StdIO_Write (':');
345 NumberIO_WriteCard (LineNo, 0);
346 StdIO_Write (':');
347 StrIO_WriteString ((const char *) a, _a_high);
348 StrIO_WriteLn ();
349 StdIO_PopOutput ();
350 FIO_Close (FIO_StdErr);
351 libc_exit (1);
356 WarnError - emits an error message with the appropriate file, line combination.
357 It does not terminate but when the program finishes an exit status of
358 1 will be issued.
361 extern "C" void PushBackInput_WarnError (const char *a_, unsigned int _a_high)
363 char a[_a_high+1];
365 /* make a local copy of each unbounded array. */
366 memcpy (a, a_, _a_high+1);
368 StdIO_PushOutput ((StdIO_ProcWrite) {(StdIO_ProcWrite_t) ErrChar});
369 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
370 StdIO_Write (':');
371 NumberIO_WriteCard (LineNo, 0);
372 StdIO_Write (':');
373 StrIO_WriteString ((const char *) a, _a_high);
374 StrIO_WriteLn ();
375 StdIO_PopOutput ();
376 ExitStatus = 1;
381 WarnString - emits an error message with the appropriate file, line combination.
382 It does not terminate but when the program finishes an exit status of
383 1 will be issued.
386 extern "C" void PushBackInput_WarnString (DynamicStrings_String s)
388 typedef char *WarnString__T1;
390 WarnString__T1 p;
392 p = static_cast<WarnString__T1> (DynamicStrings_string (s));
393 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
394 StdIO_Write (':');
395 NumberIO_WriteCard (LineNo, 0);
396 StdIO_Write (':');
397 do {
398 if (p != NULL)
400 if ((*p) == ASCII_lf)
402 StrIO_WriteLn ();
403 StrIO_WriteString ((const char *) &FileName.array[0], MaxFileName);
404 StdIO_Write (':');
405 NumberIO_WriteCard (LineNo, 0);
406 StdIO_Write (':');
408 else
410 StdIO_Write ((*p));
412 p += 1;
414 } while (! ((p == NULL) || ((*p) == ASCII_nul)));
415 ExitStatus = 1;
420 Close - closes the opened file.
423 extern "C" void PushBackInput_Close (FIO_File f)
425 FIO_Close (f);
430 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
433 extern "C" unsigned int PushBackInput_GetExitStatus (void)
435 return ExitStatus;
436 /* static analysis guarentees a RETURN statement will be used before here. */
437 __builtin_unreachable ();
442 SetDebug - sets the debug flag on or off.
445 extern "C" void PushBackInput_SetDebug (bool d)
447 Debugging = d;
452 GetColumnPosition - returns the column position of the current character.
455 extern "C" unsigned int PushBackInput_GetColumnPosition (void)
457 if (StackPtr > Column)
459 return 0;
461 else
463 return Column-StackPtr;
465 /* static analysis guarentees a RETURN statement will be used before here. */
466 __builtin_unreachable ();
471 GetCurrentLine - returns the current line number.
474 extern "C" unsigned int PushBackInput_GetCurrentLine (void)
476 return LineNo;
477 /* static analysis guarentees a RETURN statement will be used before here. */
478 __builtin_unreachable ();
481 extern "C" void _M2_PushBackInput_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
483 PushBackInput_SetDebug (false);
484 Init ();
487 extern "C" void _M2_PushBackInput_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])