libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / mc-boot / GmcComment.cc
blob4c7650283ec229b257c354dbd243ba122e4f7cf3
1 /* do not edit automatically generated by mc from mcComment. */
2 /* mcComment.mod provides a module to remember the comments.
4 Copyright (C) 2015-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 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include <stdbool.h>
26 # if !defined (PROC_D)
27 # define PROC_D
28 typedef void (*PROC_t) (void);
29 typedef struct { PROC_t proc; } PROC;
30 # endif
32 # if !defined (TRUE)
33 # define TRUE (1==1)
34 # endif
36 # if !defined (FALSE)
37 # define FALSE (1==0)
38 # endif
40 # include "GStorage.h"
41 # include "Gmcrts.h"
42 #if defined(__cplusplus)
43 # undef NULL
44 # define NULL 0
45 #endif
46 #define _mcComment_C
48 #include "GmcComment.h"
49 # include "GDynamicStrings.h"
50 # include "GStorage.h"
51 # include "GnameKey.h"
52 # include "GmcDebug.h"
53 # include "GASCII.h"
54 # include "Glibc.h"
56 typedef struct mcComment__T1_r mcComment__T1;
58 typedef enum {mcComment_unknown, mcComment_procedureHeading, mcComment_inBody, mcComment_afterStatement} mcComment_commentType;
60 typedef mcComment__T1 *mcComment_commentDesc__opaque;
62 struct mcComment__T1_r {
63 mcComment_commentType type;
64 DynamicStrings_String content;
65 nameKey_Name procName;
66 bool used;
71 initComment - the start of a new comment has been seen by the lexical analyser.
72 A new comment block is created and all addText contents are placed
73 in this block. onlySpaces indicates whether we have only seen
74 spaces on this line.
77 extern "C" mcComment_commentDesc mcComment_initComment (bool onlySpaces);
80 addText - cs is a C string (null terminated) which contains comment text.
81 This is appended to the comment, cd.
84 extern "C" void mcComment_addText (mcComment_commentDesc cd, void * cs);
87 getContent - returns the content of comment, cd.
90 extern "C" DynamicStrings_String mcComment_getContent (mcComment_commentDesc cd);
93 getCommentCharStar - returns the C string content of comment, cd.
96 extern "C" void * mcComment_getCommentCharStar (mcComment_commentDesc cd);
99 setProcedureComment - changes the type of comment, cd, to a
100 procedure heading comment,
101 providing it has the procname as the first word.
104 extern "C" void mcComment_setProcedureComment (mcComment_commentDesc cd, nameKey_Name procname);
107 getProcedureComment - returns the current procedure comment if available.
110 extern "C" DynamicStrings_String mcComment_getProcedureComment (mcComment_commentDesc cd);
113 getAfterStatementComment - returns the current statement after comment if available.
116 extern "C" DynamicStrings_String mcComment_getAfterStatementComment (mcComment_commentDesc cd);
119 getInbodyStatementComment - returns the current statement after comment if available.
122 extern "C" DynamicStrings_String mcComment_getInbodyStatementComment (mcComment_commentDesc cd);
125 isProcedureComment - returns TRUE if, cd, is a procedure comment.
128 extern "C" bool mcComment_isProcedureComment (mcComment_commentDesc cd);
131 isBodyComment - returns TRUE if, cd, is a body comment.
134 extern "C" bool mcComment_isBodyComment (mcComment_commentDesc cd);
137 isAfterComment - returns TRUE if, cd, is an after comment.
140 extern "C" bool mcComment_isAfterComment (mcComment_commentDesc cd);
143 Min - returns the lower of, a, and, b.
146 static unsigned int Min (unsigned int a, unsigned int b);
149 RemoveNewlines -
152 static DynamicStrings_String RemoveNewlines (DynamicStrings_String s);
155 seenProcedure - returns TRUE if the name, procName, appears as the first word
156 in the comment.
159 static bool seenProcedure (mcComment_commentDesc__opaque cd, nameKey_Name procName);
162 dumpComment -
165 static void dumpComment (mcComment_commentDesc__opaque cd);
169 Min - returns the lower of, a, and, b.
172 static unsigned int Min (unsigned int a, unsigned int b)
174 if (a < b)
176 return a;
178 else
180 return b;
182 /* static analysis guarentees a RETURN statement will be used before here. */
183 __builtin_unreachable ();
188 RemoveNewlines -
191 static DynamicStrings_String RemoveNewlines (DynamicStrings_String s)
193 while ((DynamicStrings_Length (s)) > 0)
195 if ((DynamicStrings_char (s, 0)) == ASCII_nl)
197 s = DynamicStrings_RemoveWhitePrefix (DynamicStrings_Slice (s, 1, 0));
199 else
201 return DynamicStrings_RemoveWhitePrefix (s);
204 return s;
205 /* static analysis guarentees a RETURN statement will be used before here. */
206 __builtin_unreachable ();
211 seenProcedure - returns TRUE if the name, procName, appears as the first word
212 in the comment.
215 static bool seenProcedure (mcComment_commentDesc__opaque cd, nameKey_Name procName)
217 DynamicStrings_String s;
218 void * a;
219 unsigned int i;
220 unsigned int h;
221 bool res;
223 a = nameKey_keyToCharStar (procName);
224 s = RemoveNewlines (cd->content);
225 s = DynamicStrings_Slice (DynamicStrings_Mark (s), 0, static_cast<int> (Min (DynamicStrings_Length (s), nameKey_lengthKey (procName))));
226 res = DynamicStrings_EqualCharStar (s, a);
227 s = DynamicStrings_KillString (s);
228 return res;
229 /* static analysis guarentees a RETURN statement will be used before here. */
230 __builtin_unreachable ();
235 dumpComment -
238 static void dumpComment (mcComment_commentDesc__opaque cd)
240 libc_printf ((const char *) "comment : ", 10);
241 switch (cd->type)
243 case mcComment_unknown:
244 libc_printf ((const char *) "unknown", 7);
245 break;
247 case mcComment_procedureHeading:
248 libc_printf ((const char *) "procedureheading", 16);
249 break;
251 case mcComment_inBody:
252 libc_printf ((const char *) "inbody", 6);
253 break;
255 case mcComment_afterStatement:
256 libc_printf ((const char *) "afterstatement", 14);
257 break;
260 default:
261 CaseException ("../../gcc/m2/mc/mcComment.def", 20, 1);
262 __builtin_unreachable ();
264 if (cd->used)
266 libc_printf ((const char *) " used", 5);
268 else
270 libc_printf ((const char *) " unused", 7);
272 libc_printf ((const char *) " contents = %s\\n", 16, DynamicStrings_string (cd->content));
277 initComment - the start of a new comment has been seen by the lexical analyser.
278 A new comment block is created and all addText contents are placed
279 in this block. onlySpaces indicates whether we have only seen
280 spaces on this line.
283 extern "C" mcComment_commentDesc mcComment_initComment (bool onlySpaces)
285 mcComment_commentDesc__opaque cd;
287 Storage_ALLOCATE ((void **) &cd, sizeof (mcComment__T1));
288 mcDebug_assert (cd != NULL);
289 if (onlySpaces)
291 cd->type = mcComment_inBody;
293 else
295 cd->type = mcComment_afterStatement;
297 cd->content = DynamicStrings_InitString ((const char *) "", 0);
298 cd->procName = nameKey_NulName;
299 cd->used = false;
300 return static_cast<mcComment_commentDesc> (cd);
301 /* static analysis guarentees a RETURN statement will be used before here. */
302 __builtin_unreachable ();
307 addText - cs is a C string (null terminated) which contains comment text.
308 This is appended to the comment, cd.
311 extern "C" void mcComment_addText (mcComment_commentDesc cd, void * cs)
313 if (cd != NULL)
315 static_cast<mcComment_commentDesc__opaque> (cd)->content = DynamicStrings_ConCat (static_cast<mcComment_commentDesc__opaque> (cd)->content, DynamicStrings_InitStringCharStar (cs));
321 getContent - returns the content of comment, cd.
324 extern "C" DynamicStrings_String mcComment_getContent (mcComment_commentDesc cd)
326 if (cd != NULL)
328 return static_cast<mcComment_commentDesc__opaque> (cd)->content;
330 return static_cast<DynamicStrings_String> (NULL);
331 /* static analysis guarentees a RETURN statement will be used before here. */
332 __builtin_unreachable ();
337 getCommentCharStar - returns the C string content of comment, cd.
340 extern "C" void * mcComment_getCommentCharStar (mcComment_commentDesc cd)
342 DynamicStrings_String s;
344 s = mcComment_getContent (cd);
345 if (s == NULL)
347 return NULL;
349 else
351 return DynamicStrings_string (s);
353 /* static analysis guarentees a RETURN statement will be used before here. */
354 __builtin_unreachable ();
359 setProcedureComment - changes the type of comment, cd, to a
360 procedure heading comment,
361 providing it has the procname as the first word.
364 extern "C" void mcComment_setProcedureComment (mcComment_commentDesc cd, nameKey_Name procname)
366 if (cd != NULL)
368 if (seenProcedure (static_cast<mcComment_commentDesc__opaque> (cd), procname))
370 static_cast<mcComment_commentDesc__opaque> (cd)->type = mcComment_procedureHeading;
371 static_cast<mcComment_commentDesc__opaque> (cd)->procName = procname;
378 getProcedureComment - returns the current procedure comment if available.
381 extern "C" DynamicStrings_String mcComment_getProcedureComment (mcComment_commentDesc cd)
383 if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_procedureHeading) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used)
385 static_cast<mcComment_commentDesc__opaque> (cd)->used = true;
386 return static_cast<mcComment_commentDesc__opaque> (cd)->content;
388 return static_cast<DynamicStrings_String> (NULL);
389 /* static analysis guarentees a RETURN statement will be used before here. */
390 __builtin_unreachable ();
395 getAfterStatementComment - returns the current statement after comment if available.
398 extern "C" DynamicStrings_String mcComment_getAfterStatementComment (mcComment_commentDesc cd)
400 if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_afterStatement) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used)
402 static_cast<mcComment_commentDesc__opaque> (cd)->used = true;
403 return static_cast<mcComment_commentDesc__opaque> (cd)->content;
405 return static_cast<DynamicStrings_String> (NULL);
406 /* static analysis guarentees a RETURN statement will be used before here. */
407 __builtin_unreachable ();
412 getInbodyStatementComment - returns the current statement after comment if available.
415 extern "C" DynamicStrings_String mcComment_getInbodyStatementComment (mcComment_commentDesc cd)
417 if ((static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_inBody) && ! static_cast<mcComment_commentDesc__opaque> (cd)->used)
419 static_cast<mcComment_commentDesc__opaque> (cd)->used = true;
420 return static_cast<mcComment_commentDesc__opaque> (cd)->content;
422 return static_cast<DynamicStrings_String> (NULL);
423 /* static analysis guarentees a RETURN statement will be used before here. */
424 __builtin_unreachable ();
429 isProcedureComment - returns TRUE if, cd, is a procedure comment.
432 extern "C" bool mcComment_isProcedureComment (mcComment_commentDesc cd)
434 return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_procedureHeading);
435 /* static analysis guarentees a RETURN statement will be used before here. */
436 __builtin_unreachable ();
441 isBodyComment - returns TRUE if, cd, is a body comment.
444 extern "C" bool mcComment_isBodyComment (mcComment_commentDesc cd)
446 return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_inBody);
447 /* static analysis guarentees a RETURN statement will be used before here. */
448 __builtin_unreachable ();
453 isAfterComment - returns TRUE if, cd, is an after comment.
456 extern "C" bool mcComment_isAfterComment (mcComment_commentDesc cd)
458 return (cd != NULL) && (static_cast<mcComment_commentDesc__opaque> (cd)->type == mcComment_afterStatement);
459 /* static analysis guarentees a RETURN statement will be used before here. */
460 __builtin_unreachable ();
463 extern "C" void _M2_mcComment_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
467 extern "C" void _M2_mcComment_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])