Debian Codegraph @

gcc/m2/pge-boot/GDynamicStrings.cc

raw
1 /* do not edit automatically generated by mc from DynamicStrings. */
2 /* DynamicStrings.mod provides a dynamic string type and procedures.
3
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <[email protected]>.
6
7 This file is part of GNU Modula-2.
8
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.
13
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.
18
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.
22
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/>. */
27
28 #include <stdbool.h>
29 # if !defined (PROC_D)
30 # define PROC_D
31 typedef void (*PROC_t) (void);
32 typedef struct { PROC_t proc; } PROC;
33 # endif
34
35 # if !defined (TRUE)
36 # define TRUE (1==1)
37 # endif
38
39 # if !defined (FALSE)
40 # define FALSE (1==0)
41 # endif
42
43 #include <stddef.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 # include "GStorage.h"
48 #include <unistd.h>
49 #if defined(__cplusplus)
50 # undef NULL
51 # define NULL 0
52 #endif
53 #define _DynamicStrings_C
54
55 #include "GDynamicStrings.h"
56 # include "Glibc.h"
57 # include "GStrLib.h"
58 # include "GStorage.h"
59 # include "GAssertion.h"
60 # include "GSYSTEM.h"
61 # include "GASCII.h"
62 # include "GM2RTS.h"
63
64 # define MaxBuf 127
65 # define PoisonOn false
66 # define DebugOn false
67 # define CheckOn false
68 # define TraceOn false
69 typedef struct DynamicStrings_Contents_r DynamicStrings_Contents;
70
71 typedef struct DynamicStrings_DebugInfo_r DynamicStrings_DebugInfo;
72
73 typedef struct DynamicStrings_stringRecord_r DynamicStrings_stringRecord;
74
75 typedef struct DynamicStrings_descriptor_r DynamicStrings_descriptor;
76
77 typedef DynamicStrings_descriptor *DynamicStrings_Descriptor;
78
79 typedef struct DynamicStrings_frameRec_r DynamicStrings_frameRec;
80
81 typedef DynamicStrings_frameRec *DynamicStrings_frame;
82
83 typedef struct DynamicStrings__T3_a DynamicStrings__T3;
84
85 typedef enum {DynamicStrings_inuse, DynamicStrings_marked, DynamicStrings_onlist, DynamicStrings_poisoned} DynamicStrings_desState;
86
87 typedef DynamicStrings_stringRecord *DynamicStrings_String__opaque;
88
89 struct DynamicStrings_DebugInfo_r {
90 DynamicStrings_String__opaque next;
91 void * file;
92 unsigned int line;
93 void * proc;
94 };
95
96 struct DynamicStrings_descriptor_r {
97 bool charStarUsed;
98 void * charStar;
99 unsigned int charStarSize;
100 bool charStarValid;
101 DynamicStrings_desState state;
102 DynamicStrings_String__opaque garbage;
103 };
104
105 struct DynamicStrings_frameRec_r {
106 DynamicStrings_String__opaque alloc;
107 DynamicStrings_String__opaque dealloc;
108 DynamicStrings_frame next;
109 };
110
111 struct DynamicStrings__T3_a { char array[(MaxBuf-1)+1]; };
112 struct DynamicStrings_Contents_r {
113 DynamicStrings__T3 buf;
114 unsigned int len;
115 DynamicStrings_String__opaque next;
116 };
117
118 struct DynamicStrings_stringRecord_r {
119 DynamicStrings_Contents contents;
120 DynamicStrings_Descriptor head;
121 DynamicStrings_DebugInfo debug;
122 };
123
124 static bool Initialized;
125 static DynamicStrings_frame frameHead;
126 static DynamicStrings_String__opaque captured;
127
128 /*
129 InitString - creates and returns a String type object.
130 Initial contents are, a.
131 */
132
133 extern "C" DynamicStrings_String DynamicStrings_InitString (const char *a_, unsigned int _a_high);
134
135 /*
136 KillString - frees String, s, and its contents.
137 NIL is returned.
138 */
139
140 extern "C" DynamicStrings_String DynamicStrings_KillString (DynamicStrings_String s);
141
142 /*
143 Fin - finishes with a string, it calls KillString with, s.
144 The purpose of the procedure is to provide a short cut
145 to calling KillString and then testing the return result.
146 */
147
148 extern "C" void DynamicStrings_Fin (DynamicStrings_String s);
149
150 /*
151 InitStringCharStar - initializes and returns a String to contain the C string.
152 */
153
154 extern "C" DynamicStrings_String DynamicStrings_InitStringCharStar (void * a);
155
156 /*
157 InitStringChar - initializes and returns a String to contain the single character, ch.
158 */
159
160 extern "C" DynamicStrings_String DynamicStrings_InitStringChar (char ch);
161
162 /*
163 Mark - marks String, s, ready for garbage collection.
164 */
165
166 extern "C" DynamicStrings_String DynamicStrings_Mark (DynamicStrings_String s);
167
168 /*
169 Length - returns the length of the String, s.
170 */
171
172 extern "C" unsigned int DynamicStrings_Length (DynamicStrings_String s);
173
174 /*
175 ConCat - returns String, a, after the contents of, b, have been appended.
176 */
177
178 extern "C" DynamicStrings_String DynamicStrings_ConCat (DynamicStrings_String a, DynamicStrings_String b);
179
180 /*
181 ConCatChar - returns String, a, after character, ch, has been appended.
182 */
183
184 extern "C" DynamicStrings_String DynamicStrings_ConCatChar (DynamicStrings_String a, char ch);
185
186 /*
187 Assign - assigns the contents of, b, into, a.
188 String, a, is returned.
189 */
190
191 extern "C" DynamicStrings_String DynamicStrings_Assign (DynamicStrings_String a, DynamicStrings_String b);
192
193 /*
194 ReplaceChar - returns string s after it has changed all occurances of from to to.
195 */
196
197 extern "C" DynamicStrings_String DynamicStrings_ReplaceChar (DynamicStrings_String s, char from, char to);
198
199 /*
200 Dup - duplicate a String, s, returning the copy of s.
201 */
202
203 extern "C" DynamicStrings_String DynamicStrings_Dup (DynamicStrings_String s);
204
205 /*
206 Add - returns a new String which contains the contents of a and b.
207 */
208
209 extern "C" DynamicStrings_String DynamicStrings_Add (DynamicStrings_String a, DynamicStrings_String b);
210
211 /*
212 Equal - returns TRUE if String, a, and, b, are equal.
213 */
214
215 extern "C" bool DynamicStrings_Equal (DynamicStrings_String a, DynamicStrings_String b);
216
217 /*
218 EqualCharStar - returns TRUE if contents of String, s, is the same as the
219 string, a.
220 */
221
222 extern "C" bool DynamicStrings_EqualCharStar (DynamicStrings_String s, void * a);
223
224 /*
225 EqualArray - returns TRUE if contents of String, s, is the same as the
226 string, a.
227 */
228
229 extern "C" bool DynamicStrings_EqualArray (DynamicStrings_String s, const char *a_, unsigned int _a_high);
230
231 /*
232 Mult - returns a new string which is n concatenations of String, s.
233 */
234
235 extern "C" DynamicStrings_String DynamicStrings_Mult (DynamicStrings_String s, unsigned int n);
236
237 /*
238 Slice - returns a new string which contains the elements
239 low..high-1
240
241 strings start at element 0
242 Slice(s, 0, 2) will return elements 0, 1 but not 2
243 Slice(s, 1, 3) will return elements 1, 2 but not 3
244 Slice(s, 2, 0) will return elements 2..max
245 Slice(s, 3, -1) will return elements 3..max-1
246 Slice(s, 4, -2) will return elements 4..max-2
247 */
248
249 extern "C" DynamicStrings_String DynamicStrings_Slice (DynamicStrings_String s, int low, int high);
250
251 /*
252 Index - returns the indice of the first occurance of, ch, in
253 String, s. -1 is returned if, ch, does not exist.
254 The search starts at position, o.
255 */
256
257 extern "C" int DynamicStrings_Index (DynamicStrings_String s, char ch, unsigned int o);
258
259 /*
260 RIndex - returns the indice of the last occurance of, ch,
261 in String, s. The search starts at position, o.
262 -1 is returned if, ch, is not found. The search
263 is performed left to right.
264 */
265
266 extern "C" int DynamicStrings_RIndex (DynamicStrings_String s, char ch, unsigned int o);
267
268 /*
269 ReverseIndex - returns the indice of the last occurance of ch
270 in String s. The search starts at position o
271 and searches from right to left. The start position
272 may be indexed negatively from the right (-1 is the
273 last index).
274 The return value if ch is found will always be positive.
275 -1 is returned if ch is not found.
276 */
277
278 extern "C" int DynamicStrings_ReverseIndex (DynamicStrings_String s, char ch, int o);
279
280 /*
281 RemoveComment - assuming that, comment, is a comment delimiter
282 which indicates anything to its right is a comment
283 then strip off the comment and also any white space
284 on the remaining right hand side.
285 It leaves any white space on the left hand side alone.
286 */
287
288 extern "C" DynamicStrings_String DynamicStrings_RemoveComment (DynamicStrings_String s, char comment);
289
290 /*
291 RemoveWhitePrefix - removes any leading white space from String, s.
292 A new string is returned.
293 */
294
295 extern "C" DynamicStrings_String DynamicStrings_RemoveWhitePrefix (DynamicStrings_String s);
296
297 /*
298 RemoveWhitePostfix - removes any leading white space from String, s.
299 A new string is returned.
300 */
301
302 extern "C" DynamicStrings_String DynamicStrings_RemoveWhitePostfix (DynamicStrings_String s);
303
304 /*
305 ToUpper - returns string, s, after it has had its lower case characters
306 replaced by upper case characters.
307 The string, s, is not duplicated.
308 */
309
310 extern "C" DynamicStrings_String DynamicStrings_ToUpper (DynamicStrings_String s);
311
312 /*
313 ToLower - returns string, s, after it has had its upper case characters
314 replaced by lower case characters.
315 The string, s, is not duplicated.
316 */
317
318 extern "C" DynamicStrings_String DynamicStrings_ToLower (DynamicStrings_String s);
319
320 /*
321 CopyOut - copies string, s, to a.
322 */
323
324 extern "C" void DynamicStrings_CopyOut (char *a, unsigned int _a_high, DynamicStrings_String s);
325
326 /*
327 char - returns the character, ch, at position, i, in String, s.
328 */
329
330 extern "C" char DynamicStrings_char (DynamicStrings_String s, int i);
331
332 /*
333 string - returns the C style char * of String, s.
334 */
335
336 extern "C" void * DynamicStrings_string (DynamicStrings_String s);
337
338 /*
339 InitStringDB - the debug version of InitString.
340 */
341
342 extern "C" DynamicStrings_String DynamicStrings_InitStringDB (const char *a_, unsigned int _a_high, const char *file_, unsigned int _file_high, unsigned int line);
343
344 /*
345 InitStringCharStarDB - the debug version of InitStringCharStar.
346 */
347
348 extern "C" DynamicStrings_String DynamicStrings_InitStringCharStarDB (void * a, const char *file_, unsigned int _file_high, unsigned int line);
349
350 /*
351 InitStringCharDB - the debug version of InitStringChar.
352 */
353
354 extern "C" DynamicStrings_String DynamicStrings_InitStringCharDB (char ch, const char *file_, unsigned int _file_high, unsigned int line);
355
356 /*
357 MultDB - the debug version of MultDB.
358 */
359
360 extern "C" DynamicStrings_String DynamicStrings_MultDB (DynamicStrings_String s, unsigned int n, const char *file_, unsigned int _file_high, unsigned int line);
361
362 /*
363 DupDB - the debug version of Dup.
364 */
365
366 extern "C" DynamicStrings_String DynamicStrings_DupDB (DynamicStrings_String s, const char *file_, unsigned int _file_high, unsigned int line);
367
368 /*
369 SliceDB - debug version of Slice.
370 */
371
372 extern "C" DynamicStrings_String DynamicStrings_SliceDB (DynamicStrings_String s, int low, int high, const char *file_, unsigned int _file_high, unsigned int line);
373
374 /*
375 PushAllocation - pushes the current allocation/deallocation lists.
376 */
377
378 extern "C" void DynamicStrings_PushAllocation (void);
379
380 /*
381 PopAllocation - test to see that all strings are deallocated since
382 the last push. Then it pops to the previous
383 allocation/deallocation lists.
384
385 If halt is true then the application terminates
386 with an exit code of 1.
387 */
388
389 extern "C" void DynamicStrings_PopAllocation (bool halt);
390
391 /*
392 PopAllocationExemption - test to see that all strings are deallocated, except
393 string e since the last push.
394 Post-condition: it pops to the previous allocation/deallocation
395 lists.
396
397 If halt is true then the application terminates
398 with an exit code of 1.
399 */
400
401 extern "C" DynamicStrings_String DynamicStrings_PopAllocationExemption (bool halt, DynamicStrings_String e);
402
403 /*
404 writeStringDesc write out debugging information about string, s. */
405
406 static void writeStringDesc (DynamicStrings_String__opaque s);
407
408 /*
409 writeNspace -
410 */
411
412 static void writeNspace (unsigned int n);
413
414 /*
415 DumpStringInfo -
416 */
417
418 static void DumpStringInfo (DynamicStrings_String__opaque s, unsigned int i);
419
420 /*
421 DumpStringInfo -
422 */
423
424 static void stop (void);
425
426 /*
427 doDSdbEnter -
428 */
429
430 static void doDSdbEnter (void);
431
432 /*
433 doDSdbExit -
434 */
435
436 static void doDSdbExit (DynamicStrings_String__opaque s);
437
438 /*
439 DSdbEnter -
440 */
441
442 static void DSdbEnter (void);
443
444 /*
445 DSdbExit -
446 */
447
448 static void DSdbExit (DynamicStrings_String__opaque s);
449 static unsigned int Capture (DynamicStrings_String__opaque s);
450
451 /*
452 Min -
453 */
454
455 static unsigned int Min (unsigned int a, unsigned int b);
456
457 /*
458 Max -
459 */
460
461 static unsigned int Max (unsigned int a, unsigned int b);
462
463 /*
464 writeString - writes a string to stdout.
465 */
466
467 static void writeString (const char *a_, unsigned int _a_high);
468
469 /*
470 writeCstring - writes a C string to stdout.
471 */
472
473 static void writeCstring (void * a);
474
475 /*
476 writeCard -
477 */
478
479 static void writeCard (unsigned int c);
480
481 /*
482 writeLongcard -
483 */
484
485 static void writeLongcard (long unsigned int l);
486
487 /*
488 writeAddress - writes out the address of a with a C style hex prefix.
489 */
490
491 static void writeAddress (void * a);
492
493 /*
494 writeLn - writes a newline.
495 */
496
497 static void writeLn (void);
498
499 /*
500 AssignDebug - assigns, file, and, line, information to string, s.
501 */
502
503 static DynamicStrings_String__opaque AssignDebug (DynamicStrings_String__opaque s, const char *file_, unsigned int _file_high, unsigned int line, const char *proc_, unsigned int _proc_high);
504
505 /*
506 IsOn - returns TRUE if, s, is on one of the debug lists.
507 */
508
509 static bool IsOn (DynamicStrings_String__opaque list, DynamicStrings_String__opaque s);
510
511 /*
512 AddTo - adds string, s, to, list.
513 */
514
515 static void AddTo (DynamicStrings_String__opaque *list, DynamicStrings_String__opaque s);
516
517 /*
518 SubFrom - removes string, s, from, list.
519 */
520
521 static void SubFrom (DynamicStrings_String__opaque *list, DynamicStrings_String__opaque s);
522
523 /*
524 AddAllocated - adds string, s, to the head of the allocated list.
525 */
526
527 static void AddAllocated (DynamicStrings_String__opaque s);
528
529 /*
530 AddDeallocated - adds string, s, to the head of the deallocated list.
531 */
532
533 static void AddDeallocated (DynamicStrings_String__opaque s);
534
535 /*
536 IsOnAllocated - returns TRUE if the string, s, has ever been allocated.
537 */
538
539 static bool IsOnAllocated (DynamicStrings_String__opaque s);
540
541 /*
542 IsOnDeallocated - returns TRUE if the string, s, has ever been deallocated.
543 */
544
545 static bool IsOnDeallocated (DynamicStrings_String__opaque s);
546
547 /*
548 SubAllocated - removes string, s, from the list of allocated strings.
549 */
550
551 static void SubAllocated (DynamicStrings_String__opaque s);
552
553 /*
554 SubDeallocated - removes string, s, from the list of deallocated strings.
555 */
556
557 static void SubDeallocated (DynamicStrings_String__opaque s);
558
559 /*
560 SubDebugInfo - removes string, s, from the list of allocated strings.
561 */
562
563 static void SubDebugInfo (DynamicStrings_String__opaque s);
564
565 /*
566 AddDebugInfo - adds string, s, to the list of allocated strings.
567 */
568
569 static void AddDebugInfo (DynamicStrings_String__opaque s);
570
571 /*
572 ConcatContents - add the contents of string, a, where, h, is the
573 total length of, a. The offset is in, o.
574 */
575
576 static void ConcatContents (DynamicStrings_Contents *c, const char *a_, unsigned int _a_high, unsigned int h, unsigned int o);
577
578 /*
579 DeallocateCharStar - deallocates any charStar.
580 */
581
582 static void DeallocateCharStar (DynamicStrings_String__opaque s);
583
584 /*
585 CheckPoisoned - checks for a poisoned string, s.
586 */
587
588 static DynamicStrings_String__opaque CheckPoisoned (DynamicStrings_String__opaque s);
589
590 /*
591 MarkInvalid - marks the char * version of String, s, as invalid.
592 */
593
594 static void MarkInvalid (DynamicStrings_String__opaque s);
595
596 /*
597 ConcatContentsAddress - concatenate the string, a, where, h, is the
598 total length of, a.
599 */
600
601 static void ConcatContentsAddress (DynamicStrings_Contents *c, void * a, unsigned int h);
602
603 /*
604 AddToGarbage - adds String, b, onto the garbage list of, a. Providing
605 the state of b is marked. The state is then altered to
606 onlist. String, a, is returned.
607 */
608
609 static DynamicStrings_String__opaque AddToGarbage (DynamicStrings_String__opaque a, DynamicStrings_String__opaque b);
610
611 /*
612 IsOnGarbage - returns TRUE if, s, is on string, e, garbage list.
613 */
614
615 static bool IsOnGarbage (DynamicStrings_String__opaque e, DynamicStrings_String__opaque s);
616
617 /*
618 IsWhite - returns TRUE if, ch, is a space or a tab.
619 */
620
621 static bool IsWhite (char ch);
622
623 /*
624 DumpState -
625 */
626
627 static void DumpState (DynamicStrings_String__opaque s);
628
629 /*
630 DumpStringSynopsis -
631 */
632
633 static void DumpStringSynopsis (DynamicStrings_String__opaque s);
634
635 /*
636 DumpString - displays the contents of string, s.
637 */
638
639 static void DumpString (DynamicStrings_String__opaque s);
640
641 /*
642 Init - initialize the module.
643 */
644
645 static void Init (void);
646
647
648 /*
649 writeStringDesc write out debugging information about string, s. */
650
651 static void writeStringDesc (DynamicStrings_String__opaque s)
652 {
653 writeCstring (s->debug.file);
654 writeString ((const char *) ":", 1);
655 writeCard (s->debug.line);
656 writeString ((const char *) ":", 1);
657 writeCstring (s->debug.proc);
658 writeString ((const char *) " ", 1);
659 writeAddress (reinterpret_cast<void *> (s));
660 writeString ((const char *) " ", 1);
661 switch (s->head->state)
662 {
663 case DynamicStrings_inuse:
664 writeString ((const char *) "still in use (", 14);
665 writeCard (s->contents.len);
666 writeString ((const char *) ") characters", 12);
667 break;
668
669 case DynamicStrings_marked:
670 writeString ((const char *) "marked", 6);
671 break;
672
673 case DynamicStrings_onlist:
674 writeString ((const char *) "on a (lost) garbage list", 24);
675 break;
676
677 case DynamicStrings_poisoned:
678 writeString ((const char *) "poisoned", 8);
679 break;
680
681
682 default:
683 writeString ((const char *) "unknown state", 13);
684 break;
685 }
686 }
687
688
689 /*
690 writeNspace -
691 */
692
693 static void writeNspace (unsigned int n)
694 {
695 while (n > 0)
696 {
697 writeString ((const char *) " ", 1);
698 n -= 1;
699 }
700 }
701
702
703 /*
704 DumpStringInfo -
705 */
706
707 static void DumpStringInfo (DynamicStrings_String__opaque s, unsigned int i)
708 {
709 if (s != NULL)
710 {
711 writeNspace (i);
712 writeStringDesc (s);
713 writeLn ();
714 if (s->head->garbage != NULL)
715 {
716 writeNspace (i);
717 writeString ((const char *) "garbage list:", 13);
718 writeLn ();
719 do {
720 s = s->head->garbage;
721 DumpStringInfo (s, i+1);
722 writeLn ();
723 } while (! (s == NULL));
724 }
725 }
726 }
727
728
729 /*
730 DumpStringInfo -
731 */
732
733 static void stop (void)
734 {
735 }
736
737
738 /*
739 doDSdbEnter -
740 */
741
742 static void doDSdbEnter (void)
743 {
744 if (CheckOn)
745 {
746 DynamicStrings_PushAllocation ();
747 }
748 }
749
750
751 /*
752 doDSdbExit -
753 */
754
755 static void doDSdbExit (DynamicStrings_String__opaque s)
756 {
757 if (CheckOn)
758 {
759 s = static_cast<DynamicStrings_String__opaque> (DynamicStrings_PopAllocationExemption (true, static_cast<DynamicStrings_String> (s)));
760 }
761 }
762
763
764 /*
765 DSdbEnter -
766 */
767
768 static void DSdbEnter (void)
769 {
770 }
771
772
773 /*
774 DSdbExit -
775 */
776
777 static void DSdbExit (DynamicStrings_String__opaque s)
778 {
779 }
780
781 static unsigned int Capture (DynamicStrings_String__opaque s)
782 {
783 /*
784 * #undef GM2_DEBUG_DYNAMICSTINGS
785 * #if defined(GM2_DEBUG_DYNAMICSTINGS)
786 * # define DSdbEnter doDSdbEnter
787 * # define DSdbExit doDSdbExit
788 * # define CheckOn TRUE
789 * # define TraceOn TRUE
790 * #endif
791 */
792 captured = s;
793 return 1;
794 /* static analysis guarentees a RETURN statement will be used before here. */
795 __builtin_unreachable ();
796 }
797
798
799 /*
800 Min -
801 */
802
803 static unsigned int Min (unsigned int a, unsigned int b)
804 {
805 if (a < b)
806 {
807 return a;
808 }
809 else
810 {
811 return b;
812 }
813 /* static analysis guarentees a RETURN statement will be used before here. */
814 __builtin_unreachable ();
815 }
816
817
818 /*
819 Max -
820 */
821
822 static unsigned int Max (unsigned int a, unsigned int b)
823 {
824 if (a > b)
825 {
826 return a;
827 }
828 else
829 {
830 return b;
831 }
832 /* static analysis guarentees a RETURN statement will be used before here. */
833 __builtin_unreachable ();
834 }
835
836
837 /*
838 writeString - writes a string to stdout.
839 */
840
841 static void writeString (const char *a_, unsigned int _a_high)
842 {
843 int i;
844 char a[_a_high+1];
845
846 /* make a local copy of each unbounded array. */
847 memcpy (a, a_, _a_high+1);
848
849 i = static_cast<int> (libc_write (1, const_cast<void*> (static_cast<const void*>(a)), static_cast<size_t> (StrLib_StrLen ((const char *) a, _a_high))));
850 }
851
852
853 /*
854 writeCstring - writes a C string to stdout.
855 */
856
857 static void writeCstring (void * a)
858 {
859 int i;
860
861 if (a == NULL)
862 {
863 writeString ((const char *) "(null)", 6);
864 }
865 else
866 {
867 i = static_cast<int> (libc_write (1, a, libc_strlen (a)));
868 }
869 }
870
871
872 /*
873 writeCard -
874 */
875
876 static void writeCard (unsigned int c)
877 {
878 char ch;
879 int i;
880
881 if (c > 9)
882 {
883 writeCard (c / 10);
884 writeCard (c % 10);
885 }
886 else
887 {
888 ch = ((char) ( ((unsigned int) ('0'))+c));
889 i = static_cast<int> (libc_write (1, &ch, static_cast<size_t> (1)));
890 }
891 }
892
893
894 /*
895 writeLongcard -
896 */
897
898 static void writeLongcard (long unsigned int l)
899 {
900 char ch;
901 int i;
902
903 if (l > 16)
904 {
905 writeLongcard (l / 16);
906 writeLongcard (l % 16);
907 }
908 else if (l < 10)
909 {
910 /* avoid dangling else. */
911 ch = ((char) ( ((unsigned int) ('0'))+((unsigned int ) (l))));
912 i = static_cast<int> (libc_write (1, &ch, static_cast<size_t> (1)));
913 }
914 else if (l < 16)
915 {
916 /* avoid dangling else. */
917 ch = ((char) (( ((unsigned int) ('a'))+((unsigned int ) (l)))-10));
918 i = static_cast<int> (libc_write (1, &ch, static_cast<size_t> (1)));
919 }
920 }
921
922
923 /*
924 writeAddress - writes out the address of a with a C style hex prefix.
925 */
926
927 static void writeAddress (void * a)
928 {
929 typedef struct writeAddress__T4_a writeAddress__T4;
930
931 struct writeAddress__T4_a { char array[30+1]; };
932 writeAddress__T4 buffer;
933
934 libc_snprintf (&buffer, static_cast<size_t> (sizeof (buffer)), (const char *) "0x%", 3, a);
935 writeString ((const char *) &buffer.array[0], 30);
936 }
937
938
939 /*
940 writeLn - writes a newline.
941 */
942
943 static void writeLn (void)
944 {
945 char ch;
946 int i;
947
948 ch = ASCII_lf;
949 i = static_cast<int> (libc_write (1, &ch, static_cast<size_t> (1)));
950 }
951
952
953 /*
954 AssignDebug - assigns, file, and, line, information to string, s.
955 */
956
957 static DynamicStrings_String__opaque AssignDebug (DynamicStrings_String__opaque s, const char *file_, unsigned int _file_high, unsigned int line, const char *proc_, unsigned int _proc_high)
958 {
959 void * f;
960 void * p;
961 char file[_file_high+1];
962 char proc[_proc_high+1];
963
964 /* make a local copy of each unbounded array. */
965 memcpy (file, file_, _file_high+1);
966 memcpy (proc, proc_, _proc_high+1);
967
968 f = const_cast<void*> (static_cast<const void*>(file));
969 p = const_cast<void*> (static_cast<const void*>(proc));
970 Storage_ALLOCATE (&s->debug.file, (StrLib_StrLen ((const char *) file, _file_high))+1);
971 if ((libc_strncpy (s->debug.file, f, (StrLib_StrLen ((const char *) file, _file_high))+1)) == NULL)
972 {} /* empty. */
973 s->debug.line = line;
974 Storage_ALLOCATE (&s->debug.proc, (StrLib_StrLen ((const char *) proc, _proc_high))+1);
975 if ((libc_strncpy (s->debug.proc, p, (StrLib_StrLen ((const char *) proc, _proc_high))+1)) == NULL)
976 {} /* empty. */
977 return s;
978 /* static analysis guarentees a RETURN statement will be used before here. */
979 __builtin_unreachable ();
980 }
981
982
983 /*
984 IsOn - returns TRUE if, s, is on one of the debug lists.
985 */
986
987 static bool IsOn (DynamicStrings_String__opaque list, DynamicStrings_String__opaque s)
988 {
989 while ((list != s) && (list != NULL))
990 {
991 list = list->debug.next;
992 }
993 return list == s;
994 /* static analysis guarentees a RETURN statement will be used before here. */
995 __builtin_unreachable ();
996 }
997
998
999 /*
1000 AddTo - adds string, s, to, list.
1001 */
1002
1003 static void AddTo (DynamicStrings_String__opaque *list, DynamicStrings_String__opaque s)
1004 {
1005 if ((*list) == NULL)
1006 {
1007 (*list) = s;
1008 s->debug.next = static_cast<DynamicStrings_String__opaque> (NULL);
1009 }
1010 else
1011 {
1012 s->debug.next = (*list);
1013 (*list) = s;
1014 }
1015 }
1016
1017
1018 /*
1019 SubFrom - removes string, s, from, list.
1020 */
1021
1022 static void SubFrom (DynamicStrings_String__opaque *list, DynamicStrings_String__opaque s)
1023 {
1024 DynamicStrings_String__opaque p;
1025
1026 if ((*list) == s)
1027 {
1028 (*list) = s->debug.next;
1029 }
1030 else
1031 {
1032 p = (*list);
1033 while ((p->debug.next != NULL) && (p->debug.next != s))
1034 {
1035 p = p->debug.next;
1036 }
1037 if (p->debug.next == s)
1038 {
1039 p->debug.next = s->debug.next;
1040 }
1041 else
1042 {
1043 /* not found, quit */
1044 return;
1045 }
1046 }
1047 s->debug.next = static_cast<DynamicStrings_String__opaque> (NULL);
1048 }
1049
1050
1051 /*
1052 AddAllocated - adds string, s, to the head of the allocated list.
1053 */
1054
1055 static void AddAllocated (DynamicStrings_String__opaque s)
1056 {
1057 Init ();
1058 AddTo (&frameHead->alloc, s);
1059 }
1060
1061
1062 /*
1063 AddDeallocated - adds string, s, to the head of the deallocated list.
1064 */
1065
1066 static void AddDeallocated (DynamicStrings_String__opaque s)
1067 {
1068 Init ();
1069 AddTo (&frameHead->dealloc, s);
1070 }
1071
1072
1073 /*
1074 IsOnAllocated - returns TRUE if the string, s, has ever been allocated.
1075 */
1076
1077 static bool IsOnAllocated (DynamicStrings_String__opaque s)
1078 {
1079 DynamicStrings_frame f;
1080
1081 Init ();
1082 f = frameHead;
1083 do {
1084 if (IsOn (f->alloc, s))
1085 {
1086 return true;
1087 }
1088 else
1089 {
1090 f = f->next;
1091 }
1092 } while (! (f == NULL));
1093 return false;
1094 /* static analysis guarentees a RETURN statement will be used before here. */
1095 __builtin_unreachable ();
1096 }
1097
1098
1099 /*
1100 IsOnDeallocated - returns TRUE if the string, s, has ever been deallocated.
1101 */
1102
1103 static bool IsOnDeallocated (DynamicStrings_String__opaque s)
1104 {
1105 DynamicStrings_frame f;
1106
1107 Init ();
1108 f = frameHead;
1109 do {
1110 if (IsOn (f->dealloc, s))
1111 {
1112 return true;
1113 }
1114 else
1115 {
1116 f = f->next;
1117 }
1118 } while (! (f == NULL));
1119 return false;
1120 /* static analysis guarentees a RETURN statement will be used before here. */
1121 __builtin_unreachable ();
1122 }
1123
1124
1125 /*
1126 SubAllocated - removes string, s, from the list of allocated strings.
1127 */
1128
1129 static void SubAllocated (DynamicStrings_String__opaque s)
1130 {
1131 DynamicStrings_frame f;
1132
1133 Init ();
1134 f = frameHead;
1135 do {
1136 if (IsOn (f->alloc, s))
1137 {
1138 SubFrom (&f->alloc, s);
1139 return;
1140 }
1141 else
1142 {
1143 f = f->next;
1144 }
1145 } while (! (f == NULL));
1146 }
1147
1148
1149 /*
1150 SubDeallocated - removes string, s, from the list of deallocated strings.
1151 */
1152
1153 static void SubDeallocated (DynamicStrings_String__opaque s)
1154 {
1155 DynamicStrings_frame f;
1156
1157 Init ();
1158 f = frameHead;
1159 do {
1160 if (IsOn (f->dealloc, s))
1161 {
1162 SubFrom (&f->dealloc, s);
1163 return;
1164 }
1165 else
1166 {
1167 f = f->next;
1168 }
1169 } while (! (f == NULL));
1170 }
1171
1172
1173 /*
1174 SubDebugInfo - removes string, s, from the list of allocated strings.
1175 */
1176
1177 static void SubDebugInfo (DynamicStrings_String__opaque s)
1178 {
1179 if (IsOnDeallocated (s))
1180 {
1181 Assertion_Assert (! DebugOn);
1182 /* string has already been deallocated */
1183 return;
1184 }
1185 if (IsOnAllocated (s))
1186 {
1187 SubAllocated (s);
1188 AddDeallocated (s);
1189 }
1190 else
1191 {
1192 /* string has not been allocated */
1193 Assertion_Assert (! DebugOn);
1194 }
1195 }
1196
1197
1198 /*
1199 AddDebugInfo - adds string, s, to the list of allocated strings.
1200 */
1201
1202 static void AddDebugInfo (DynamicStrings_String__opaque s)
1203 {
1204 s->debug.next = static_cast<DynamicStrings_String__opaque> (NULL);
1205 s->debug.file = NULL;
1206 s->debug.line = 0;
1207 s->debug.proc = NULL;
1208 if (CheckOn)
1209 {
1210 AddAllocated (s);
1211 }
1212 }
1213
1214
1215 /*
1216 ConcatContents - add the contents of string, a, where, h, is the
1217 total length of, a. The offset is in, o.
1218 */
1219
1220 static void ConcatContents (DynamicStrings_Contents *c, const char *a_, unsigned int _a_high, unsigned int h, unsigned int o)
1221 {
1222 unsigned int i;
1223 char a[_a_high+1];
1224
1225 /* make a local copy of each unbounded array. */
1226 memcpy (a, a_, _a_high+1);
1227
1228 i = (*c).len;
1229 while ((o < h) && (i < MaxBuf))
1230 {
1231 (*c).buf.array[i] = a[o];
1232 o += 1;
1233 i += 1;
1234 }
1235 if (o < h)
1236 {
1237 (*c).len = MaxBuf;
1238 Storage_ALLOCATE ((void **) &(*c).next, sizeof (DynamicStrings_stringRecord));
1239 (*c).next->head = NULL;
1240 (*c).next->contents.len = 0;
1241 (*c).next->contents.next = static_cast<DynamicStrings_String__opaque> (NULL);
1242 ConcatContents (&(*c).next->contents, (const char *) a, _a_high, h, o);
1243 AddDebugInfo ((*c).next);
1244 (*c).next = AssignDebug ((*c).next, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 722, (const char *) "ConcatContents", 14);
1245 }
1246 else
1247 {
1248 (*c).len = i;
1249 }
1250 }
1251
1252
1253 /*
1254 DeallocateCharStar - deallocates any charStar.
1255 */
1256
1257 static void DeallocateCharStar (DynamicStrings_String__opaque s)
1258 {
1259 if ((s != NULL) && (s->head != NULL))
1260 {
1261 if (s->head->charStarUsed && (s->head->charStar != NULL))
1262 {
1263 Storage_DEALLOCATE (&s->head->charStar, s->head->charStarSize);
1264 }
1265 s->head->charStarUsed = false;
1266 s->head->charStar = NULL;
1267 s->head->charStarSize = 0;
1268 s->head->charStarValid = false;
1269 }
1270 }
1271
1272
1273 /*
1274 CheckPoisoned - checks for a poisoned string, s.
1275 */
1276
1277 static DynamicStrings_String__opaque CheckPoisoned (DynamicStrings_String__opaque s)
1278 {
1279 if (((PoisonOn && (s != NULL)) && (s->head != NULL)) && (s->head->state == DynamicStrings_poisoned))
1280 {
1281 M2RTS_HALT (-1);
1282 __builtin_unreachable ();
1283 }
1284 return s;
1285 /* static analysis guarentees a RETURN statement will be used before here. */
1286 __builtin_unreachable ();
1287 }
1288
1289
1290 /*
1291 MarkInvalid - marks the char * version of String, s, as invalid.
1292 */
1293
1294 static void MarkInvalid (DynamicStrings_String__opaque s)
1295 {
1296 if (PoisonOn)
1297 {
1298 s = CheckPoisoned (s);
1299 }
1300 if (s->head != NULL)
1301 {
1302 s->head->charStarValid = false;
1303 }
1304 }
1305
1306
1307 /*
1308 ConcatContentsAddress - concatenate the string, a, where, h, is the
1309 total length of, a.
1310 */
1311
1312 static void ConcatContentsAddress (DynamicStrings_Contents *c, void * a, unsigned int h)
1313 {
1314 typedef char *ConcatContentsAddress__T1;
1315
1316 ConcatContentsAddress__T1 p;
1317 unsigned int i;
1318 unsigned int j;
1319
1320 j = 0;
1321 i = (*c).len;
1322 p = static_cast<ConcatContentsAddress__T1> (a);
1323 while ((j < h) && (i < MaxBuf))
1324 {
1325 (*c).buf.array[i] = (*p);
1326 i += 1;
1327 j += 1;
1328 p += 1;
1329 }
1330 if (j < h)
1331 {
1332 /* avoid dangling else. */
1333 (*c).len = MaxBuf;
1334 Storage_ALLOCATE ((void **) &(*c).next, sizeof (DynamicStrings_stringRecord));
1335 (*c).next->head = NULL;
1336 (*c).next->contents.len = 0;
1337 (*c).next->contents.next = static_cast<DynamicStrings_String__opaque> (NULL);
1338 ConcatContentsAddress (&(*c).next->contents, reinterpret_cast<void *> (p), h-j);
1339 AddDebugInfo ((*c).next);
1340 if (TraceOn)
1341 {
1342 (*c).next = AssignDebug ((*c).next, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 917, (const char *) "ConcatContentsAddress", 21);
1343 }
1344 }
1345 else
1346 {
1347 (*c).len = i;
1348 (*c).next = static_cast<DynamicStrings_String__opaque> (NULL);
1349 }
1350 }
1351
1352
1353 /*
1354 AddToGarbage - adds String, b, onto the garbage list of, a. Providing
1355 the state of b is marked. The state is then altered to
1356 onlist. String, a, is returned.
1357 */
1358
1359 static DynamicStrings_String__opaque AddToGarbage (DynamicStrings_String__opaque a, DynamicStrings_String__opaque b)
1360 {
1361 DynamicStrings_String__opaque c;
1362
1363 if (PoisonOn)
1364 {
1365 a = CheckPoisoned (a);
1366 b = CheckPoisoned (b);
1367 }
1368 /*
1369 IF (a#NIL) AND (a#b) AND (a^.head^.state=marked)
1370 THEN
1371 writeString('warning trying to add to a marked string') ; writeLn
1372 END ;
1373 */
1374 if (((((a != b) && (a != NULL)) && (b != NULL)) && (b->head->state == DynamicStrings_marked)) && (a->head->state == DynamicStrings_inuse))
1375 {
1376 c = a;
1377 while (c->head->garbage != NULL)
1378 {
1379 c = c->head->garbage;
1380 }
1381 c->head->garbage = b;
1382 b->head->state = DynamicStrings_onlist;
1383 if (CheckOn)
1384 {
1385 SubDebugInfo (b);
1386 }
1387 }
1388 return a;
1389 /* static analysis guarentees a RETURN statement will be used before here. */
1390 __builtin_unreachable ();
1391 }
1392
1393
1394 /*
1395 IsOnGarbage - returns TRUE if, s, is on string, e, garbage list.
1396 */
1397
1398 static bool IsOnGarbage (DynamicStrings_String__opaque e, DynamicStrings_String__opaque s)
1399 {
1400 if ((e != NULL) && (s != NULL))
1401 {
1402 while (e->head->garbage != NULL)
1403 {
1404 if (e->head->garbage == s)
1405 {
1406 return true;
1407 }
1408 else
1409 {
1410 e = e->head->garbage;
1411 }
1412 }
1413 }
1414 return false;
1415 /* static analysis guarentees a RETURN statement will be used before here. */
1416 __builtin_unreachable ();
1417 }
1418
1419
1420 /*
1421 IsWhite - returns TRUE if, ch, is a space or a tab.
1422 */
1423
1424 static bool IsWhite (char ch)
1425 {
1426 return (ch == ' ') || (ch == ASCII_tab);
1427 /* static analysis guarentees a RETURN statement will be used before here. */
1428 __builtin_unreachable ();
1429 }
1430
1431
1432 /*
1433 DumpState -
1434 */
1435
1436 static void DumpState (DynamicStrings_String__opaque s)
1437 {
1438 switch (s->head->state)
1439 {
1440 case DynamicStrings_inuse:
1441 writeString ((const char *) "still in use (", 14);
1442 writeCard (s->contents.len);
1443 writeString ((const char *) ") characters", 12);
1444 break;
1445
1446 case DynamicStrings_marked:
1447 writeString ((const char *) "marked", 6);
1448 break;
1449
1450 case DynamicStrings_onlist:
1451 writeString ((const char *) "on a garbage list", 17);
1452 break;
1453
1454 case DynamicStrings_poisoned:
1455 writeString ((const char *) "poisoned", 8);
1456 break;
1457
1458
1459 default:
1460 writeString ((const char *) "unknown state", 13);
1461 break;
1462 }
1463 }
1464
1465
1466 /*
1467 DumpStringSynopsis -
1468 */
1469
1470 static void DumpStringSynopsis (DynamicStrings_String__opaque s)
1471 {
1472 writeCstring (s->debug.file);
1473 writeString ((const char *) ":", 1);
1474 writeCard (s->debug.line);
1475 writeString ((const char *) ":", 1);
1476 writeCstring (s->debug.proc);
1477 writeString ((const char *) " string ", 8);
1478 writeAddress (reinterpret_cast<void *> (s));
1479 writeString ((const char *) " ", 1);
1480 DumpState (s);
1481 if (IsOnAllocated (s))
1482 {
1483 writeString ((const char *) " globally allocated", 19);
1484 }
1485 else if (IsOnDeallocated (s))
1486 {
1487 /* avoid dangling else. */
1488 writeString ((const char *) " globally deallocated", 21);
1489 }
1490 else
1491 {
1492 /* avoid dangling else. */
1493 writeString ((const char *) " globally unknown", 17);
1494 }
1495 writeLn ();
1496 }
1497
1498
1499 /*
1500 DumpString - displays the contents of string, s.
1501 */
1502
1503 static void DumpString (DynamicStrings_String__opaque s)
1504 {
1505 DynamicStrings_String__opaque t;
1506
1507 if (s != NULL)
1508 {
1509 DumpStringSynopsis (s);
1510 if ((s->head != NULL) && (s->head->garbage != NULL))
1511 {
1512 writeString ((const char *) "display chained strings on the garbage list", 43);
1513 writeLn ();
1514 t = s->head->garbage;
1515 while (t != NULL)
1516 {
1517 DumpStringSynopsis (t);
1518 t = t->head->garbage;
1519 }
1520 }
1521 }
1522 }
1523
1524
1525 /*
1526 Init - initialize the module.
1527 */
1528
1529 static void Init (void)
1530 {
1531 if (! Initialized)
1532 {
1533 Initialized = true;
1534 frameHead = NULL;
1535 DynamicStrings_PushAllocation ();
1536 }
1537 }
1538
1539
1540 /*
1541 InitString - creates and returns a String type object.
1542 Initial contents are, a.
1543 */
1544
1545 extern "C" DynamicStrings_String DynamicStrings_InitString (const char *a_, unsigned int _a_high)
1546 {
1547 DynamicStrings_String__opaque s;
1548 char a[_a_high+1];
1549
1550 /* make a local copy of each unbounded array. */
1551 memcpy (a, a_, _a_high+1);
1552
1553 Storage_ALLOCATE ((void **) &s, sizeof (DynamicStrings_stringRecord));
1554 s->contents.len = 0;
1555 s->contents.next = static_cast<DynamicStrings_String__opaque> (NULL);
1556 ConcatContents (&s->contents, (const char *) a, _a_high, StrLib_StrLen ((const char *) a, _a_high), 0);
1557 Storage_ALLOCATE ((void **) &s->head, sizeof (DynamicStrings_descriptor));
1558 s->head->charStarUsed = false;
1559 s->head->charStar = NULL;
1560 s->head->charStarSize = 0;
1561 s->head->charStarValid = false;
1562 s->head->garbage = static_cast<DynamicStrings_String__opaque> (NULL);
1563 s->head->state = DynamicStrings_inuse;
1564 AddDebugInfo (s);
1565 if (TraceOn)
1566 {
1567 s = AssignDebug (s, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 758, (const char *) "InitString", 10);
1568 }
1569 return static_cast<DynamicStrings_String> (s);
1570 /* static analysis guarentees a RETURN statement will be used before here. */
1571 __builtin_unreachable ();
1572 }
1573
1574
1575 /*
1576 KillString - frees String, s, and its contents.
1577 NIL is returned.
1578 */
1579
1580 extern "C" DynamicStrings_String DynamicStrings_KillString (DynamicStrings_String s)
1581 {
1582 DynamicStrings_String__opaque t;
1583
1584 if (PoisonOn)
1585 {
1586 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
1587 }
1588 if (s != NULL)
1589 {
1590 if (CheckOn)
1591 {
1592 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1593 if (IsOnAllocated (static_cast<DynamicStrings_String__opaque> (s)))
1594 {
1595 SubAllocated (static_cast<DynamicStrings_String__opaque> (s));
1596 }
1597 else if (IsOnDeallocated (static_cast<DynamicStrings_String__opaque> (s)))
1598 {
1599 /* avoid dangling else. */
1600 SubDeallocated (static_cast<DynamicStrings_String__opaque> (s));
1601 }
1602 }
1603 if (static_cast<DynamicStrings_String__opaque> (s)->head != NULL)
1604 {
1605 static_cast<DynamicStrings_String__opaque> (s)->head->state = DynamicStrings_poisoned;
1606 static_cast<DynamicStrings_String__opaque> (s)->head->garbage = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->head->garbage)));
1607 if (! PoisonOn)
1608 {
1609 DeallocateCharStar (static_cast<DynamicStrings_String__opaque> (s));
1610 }
1611 if (! PoisonOn)
1612 {
1613 Storage_DEALLOCATE ((void **) &static_cast<DynamicStrings_String__opaque> (s)->head, sizeof (DynamicStrings_descriptor));
1614 static_cast<DynamicStrings_String__opaque> (s)->head = NULL;
1615 }
1616 }
1617 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next)));
1618 if (! PoisonOn)
1619 {
1620 Storage_DEALLOCATE ((void **) &s, sizeof (DynamicStrings_stringRecord));
1621 }
1622 }
1623 return static_cast<DynamicStrings_String> (NULL);
1624 /* static analysis guarentees a RETURN statement will be used before here. */
1625 __builtin_unreachable ();
1626 }
1627
1628
1629 /*
1630 Fin - finishes with a string, it calls KillString with, s.
1631 The purpose of the procedure is to provide a short cut
1632 to calling KillString and then testing the return result.
1633 */
1634
1635 extern "C" void DynamicStrings_Fin (DynamicStrings_String s)
1636 {
1637 if ((DynamicStrings_KillString (s)) != NULL)
1638 {
1639 M2RTS_HALT (-1);
1640 __builtin_unreachable ();
1641 }
1642 }
1643
1644
1645 /*
1646 InitStringCharStar - initializes and returns a String to contain the C string.
1647 */
1648
1649 extern "C" DynamicStrings_String DynamicStrings_InitStringCharStar (void * a)
1650 {
1651 DynamicStrings_String__opaque s;
1652
1653 Storage_ALLOCATE ((void **) &s, sizeof (DynamicStrings_stringRecord));
1654 s->contents.len = 0;
1655 s->contents.next = static_cast<DynamicStrings_String__opaque> (NULL);
1656 if (a != NULL)
1657 {
1658 ConcatContentsAddress (&s->contents, a, static_cast<unsigned int> (libc_strlen (a)));
1659 }
1660 Storage_ALLOCATE ((void **) &s->head, sizeof (DynamicStrings_descriptor));
1661 s->head->charStarUsed = false;
1662 s->head->charStar = NULL;
1663 s->head->charStarSize = 0;
1664 s->head->charStarValid = false;
1665 s->head->garbage = static_cast<DynamicStrings_String__opaque> (NULL);
1666 s->head->state = DynamicStrings_inuse;
1667 AddDebugInfo (s);
1668 if (TraceOn)
1669 {
1670 s = AssignDebug (s, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 957, (const char *) "InitStringCharStar", 18);
1671 }
1672 return static_cast<DynamicStrings_String> (s);
1673 /* static analysis guarentees a RETURN statement will be used before here. */
1674 __builtin_unreachable ();
1675 }
1676
1677
1678 /*
1679 InitStringChar - initializes and returns a String to contain the single character, ch.
1680 */
1681
1682 extern "C" DynamicStrings_String DynamicStrings_InitStringChar (char ch)
1683 {
1684 typedef struct InitStringChar__T5_a InitStringChar__T5;
1685
1686 struct InitStringChar__T5_a { char array[1+1]; };
1687 InitStringChar__T5 a;
1688 DynamicStrings_String__opaque s;
1689
1690 a.array[0] = ch;
1691 a.array[1] = ASCII_nul;
1692 s = static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitString ((const char *) &a.array[0], 1));
1693 if (TraceOn)
1694 {
1695 s = AssignDebug (s, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 977, (const char *) "InitStringChar", 14);
1696 }
1697 return static_cast<DynamicStrings_String> (s);
1698 /* static analysis guarentees a RETURN statement will be used before here. */
1699 __builtin_unreachable ();
1700 }
1701
1702
1703 /*
1704 Mark - marks String, s, ready for garbage collection.
1705 */
1706
1707 extern "C" DynamicStrings_String DynamicStrings_Mark (DynamicStrings_String s)
1708 {
1709 if (PoisonOn)
1710 {
1711 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
1712 }
1713 if ((s != NULL) && (static_cast<DynamicStrings_String__opaque> (s)->head->state == DynamicStrings_inuse))
1714 {
1715 static_cast<DynamicStrings_String__opaque> (s)->head->state = DynamicStrings_marked;
1716 }
1717 return s;
1718 /* static analysis guarentees a RETURN statement will be used before here. */
1719 __builtin_unreachable ();
1720 }
1721
1722
1723 /*
1724 Length - returns the length of the String, s.
1725 */
1726
1727 extern "C" unsigned int DynamicStrings_Length (DynamicStrings_String s)
1728 {
1729 if (s == NULL)
1730 {
1731 return 0;
1732 }
1733 else
1734 {
1735 return static_cast<DynamicStrings_String__opaque> (s)->contents.len+(DynamicStrings_Length (static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next)));
1736 }
1737 /* static analysis guarentees a RETURN statement will be used before here. */
1738 __builtin_unreachable ();
1739 }
1740
1741
1742 /*
1743 ConCat - returns String, a, after the contents of, b, have been appended.
1744 */
1745
1746 extern "C" DynamicStrings_String DynamicStrings_ConCat (DynamicStrings_String a, DynamicStrings_String b)
1747 {
1748 DynamicStrings_String__opaque t;
1749
1750 if (PoisonOn)
1751 {
1752 a = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (a)));
1753 b = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (b)));
1754 }
1755 if (a == b)
1756 {
1757 return DynamicStrings_ConCat (a, DynamicStrings_Mark (DynamicStrings_Dup (b)));
1758 }
1759 else if (a != NULL)
1760 {
1761 /* avoid dangling else. */
1762 a = static_cast<DynamicStrings_String> (AddToGarbage (static_cast<DynamicStrings_String__opaque> (a), static_cast<DynamicStrings_String__opaque> (b)));
1763 MarkInvalid (static_cast<DynamicStrings_String__opaque> (a));
1764 t = static_cast<DynamicStrings_String__opaque> (a);
1765 while (b != NULL)
1766 {
1767 while ((t->contents.len == MaxBuf) && (t->contents.next != NULL))
1768 {
1769 t = t->contents.next;
1770 }
1771 ConcatContents (&t->contents, (const char *) &static_cast<DynamicStrings_String__opaque> (b)->contents.buf.array[0], (MaxBuf-1), static_cast<DynamicStrings_String__opaque> (b)->contents.len, 0);
1772 b = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (b)->contents.next);
1773 }
1774 }
1775 if ((a == NULL) && (b != NULL))
1776 {
1777 M2RTS_HALT (-1);
1778 __builtin_unreachable ();
1779 }
1780 return a;
1781 /* static analysis guarentees a RETURN statement will be used before here. */
1782 __builtin_unreachable ();
1783 }
1784
1785
1786 /*
1787 ConCatChar - returns String, a, after character, ch, has been appended.
1788 */
1789
1790 extern "C" DynamicStrings_String DynamicStrings_ConCatChar (DynamicStrings_String a, char ch)
1791 {
1792 typedef struct ConCatChar__T6_a ConCatChar__T6;
1793
1794 struct ConCatChar__T6_a { char array[1+1]; };
1795 ConCatChar__T6 b;
1796 DynamicStrings_String__opaque t;
1797
1798 if (PoisonOn)
1799 {
1800 a = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (a)));
1801 }
1802 b.array[0] = ch;
1803 b.array[1] = ASCII_nul;
1804 t = static_cast<DynamicStrings_String__opaque> (a);
1805 MarkInvalid (static_cast<DynamicStrings_String__opaque> (a));
1806 while ((t->contents.len == MaxBuf) && (t->contents.next != NULL))
1807 {
1808 t = t->contents.next;
1809 }
1810 ConcatContents (&t->contents, (const char *) &b.array[0], 1, 1, 0);
1811 return a;
1812 /* static analysis guarentees a RETURN statement will be used before here. */
1813 __builtin_unreachable ();
1814 }
1815
1816
1817 /*
1818 Assign - assigns the contents of, b, into, a.
1819 String, a, is returned.
1820 */
1821
1822 extern "C" DynamicStrings_String DynamicStrings_Assign (DynamicStrings_String a, DynamicStrings_String b)
1823 {
1824 if (PoisonOn)
1825 {
1826 a = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (a)));
1827 b = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (b)));
1828 }
1829 if ((a != NULL) && (b != NULL))
1830 {
1831 static_cast<DynamicStrings_String__opaque> (a)->contents.next = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (a)->contents.next)));
1832 static_cast<DynamicStrings_String__opaque> (a)->contents.len = 0;
1833 }
1834 return DynamicStrings_ConCat (a, b);
1835 /* static analysis guarentees a RETURN statement will be used before here. */
1836 __builtin_unreachable ();
1837 }
1838
1839
1840 /*
1841 ReplaceChar - returns string s after it has changed all occurances of from to to.
1842 */
1843
1844 extern "C" DynamicStrings_String DynamicStrings_ReplaceChar (DynamicStrings_String s, char from, char to)
1845 {
1846 DynamicStrings_String__opaque t;
1847 unsigned int i;
1848
1849 t = static_cast<DynamicStrings_String__opaque> (s);
1850 while (t != NULL)
1851 {
1852 i = 0;
1853 while (i < t->contents.len)
1854 {
1855 if (t->contents.buf.array[i] == from)
1856 {
1857 t->contents.buf.array[i] = to;
1858 }
1859 i += 1;
1860 }
1861 t = t->contents.next;
1862 }
1863 return s;
1864 /* static analysis guarentees a RETURN statement will be used before here. */
1865 __builtin_unreachable ();
1866 }
1867
1868
1869 /*
1870 Dup - duplicate a String, s, returning the copy of s.
1871 */
1872
1873 extern "C" DynamicStrings_String DynamicStrings_Dup (DynamicStrings_String s)
1874 {
1875 if (PoisonOn)
1876 {
1877 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
1878 }
1879 s = DynamicStrings_Assign (DynamicStrings_InitString ((const char *) "", 0), s);
1880 if (TraceOn)
1881 {
1882 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (s), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1198, (const char *) "Dup", 3));
1883 }
1884 return s;
1885 /* static analysis guarentees a RETURN statement will be used before here. */
1886 __builtin_unreachable ();
1887 }
1888
1889
1890 /*
1891 Add - returns a new String which contains the contents of a and b.
1892 */
1893
1894 extern "C" DynamicStrings_String DynamicStrings_Add (DynamicStrings_String a, DynamicStrings_String b)
1895 {
1896 if (PoisonOn)
1897 {
1898 a = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (a)));
1899 b = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (b)));
1900 }
1901 a = DynamicStrings_ConCat (DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "", 0), a), b);
1902 if (TraceOn)
1903 {
1904 a = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (a), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1218, (const char *) "Add", 3));
1905 }
1906 return a;
1907 /* static analysis guarentees a RETURN statement will be used before here. */
1908 __builtin_unreachable ();
1909 }
1910
1911
1912 /*
1913 Equal - returns TRUE if String, a, and, b, are equal.
1914 */
1915
1916 extern "C" bool DynamicStrings_Equal (DynamicStrings_String a, DynamicStrings_String b)
1917 {
1918 unsigned int i;
1919
1920 if (PoisonOn)
1921 {
1922 a = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (a)));
1923 b = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (b)));
1924 }
1925 if ((DynamicStrings_Length (a)) == (DynamicStrings_Length (b)))
1926 {
1927 while ((a != NULL) && (b != NULL))
1928 {
1929 i = 0;
1930 Assertion_Assert (static_cast<DynamicStrings_String__opaque> (a)->contents.len == static_cast<DynamicStrings_String__opaque> (b)->contents.len);
1931 while (i < static_cast<DynamicStrings_String__opaque> (a)->contents.len)
1932 {
1933 if (static_cast<DynamicStrings_String__opaque> (a)->contents.buf.array[i] != static_cast<DynamicStrings_String__opaque> (b)->contents.buf.array[i])
1934 {
1935 return false;
1936 }
1937 i += 1;
1938 }
1939 a = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (a)->contents.next);
1940 b = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (b)->contents.next);
1941 }
1942 return true;
1943 }
1944 else
1945 {
1946 return false;
1947 }
1948 /* static analysis guarentees a RETURN statement will be used before here. */
1949 __builtin_unreachable ();
1950 }
1951
1952
1953 /*
1954 EqualCharStar - returns TRUE if contents of String, s, is the same as the
1955 string, a.
1956 */
1957
1958 extern "C" bool DynamicStrings_EqualCharStar (DynamicStrings_String s, void * a)
1959 {
1960 DynamicStrings_String__opaque t;
1961
1962 if (PoisonOn)
1963 {
1964 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
1965 }
1966 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitStringCharStar (a));
1967 if (TraceOn)
1968 {
1969 t = AssignDebug (t, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1275, (const char *) "EqualCharStar", 13);
1970 }
1971 t = AddToGarbage (t, static_cast<DynamicStrings_String__opaque> (s));
1972 if (DynamicStrings_Equal (static_cast<DynamicStrings_String> (t), s))
1973 {
1974 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (t)));
1975 return true;
1976 }
1977 else
1978 {
1979 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (t)));
1980 return false;
1981 }
1982 /* static analysis guarentees a RETURN statement will be used before here. */
1983 __builtin_unreachable ();
1984 }
1985
1986
1987 /*
1988 EqualArray - returns TRUE if contents of String, s, is the same as the
1989 string, a.
1990 */
1991
1992 extern "C" bool DynamicStrings_EqualArray (DynamicStrings_String s, const char *a_, unsigned int _a_high)
1993 {
1994 DynamicStrings_String__opaque t;
1995 char a[_a_high+1];
1996
1997 /* make a local copy of each unbounded array. */
1998 memcpy (a, a_, _a_high+1);
1999
2000 if (PoisonOn)
2001 {
2002 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2003 }
2004 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitString ((const char *) a, _a_high));
2005 if (TraceOn)
2006 {
2007 t = AssignDebug (t, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1305, (const char *) "EqualArray", 10);
2008 }
2009 t = AddToGarbage (t, static_cast<DynamicStrings_String__opaque> (s));
2010 if (DynamicStrings_Equal (static_cast<DynamicStrings_String> (t), s))
2011 {
2012 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (t)));
2013 return true;
2014 }
2015 else
2016 {
2017 t = static_cast<DynamicStrings_String__opaque> (DynamicStrings_KillString (static_cast<DynamicStrings_String> (t)));
2018 return false;
2019 }
2020 /* static analysis guarentees a RETURN statement will be used before here. */
2021 __builtin_unreachable ();
2022 }
2023
2024
2025 /*
2026 Mult - returns a new string which is n concatenations of String, s.
2027 */
2028
2029 extern "C" DynamicStrings_String DynamicStrings_Mult (DynamicStrings_String s, unsigned int n)
2030 {
2031 if (PoisonOn)
2032 {
2033 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2034 }
2035 if (n <= 0)
2036 {
2037 s = static_cast<DynamicStrings_String> (AddToGarbage (static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitString ((const char *) "", 0)), static_cast<DynamicStrings_String__opaque> (s)));
2038 }
2039 else
2040 {
2041 s = DynamicStrings_ConCat (DynamicStrings_Mult (s, n-1), s);
2042 }
2043 if (TraceOn)
2044 {
2045 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (s), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1337, (const char *) "Mult", 4));
2046 }
2047 return s;
2048 /* static analysis guarentees a RETURN statement will be used before here. */
2049 __builtin_unreachable ();
2050 }
2051
2052
2053 /*
2054 Slice - returns a new string which contains the elements
2055 low..high-1
2056
2057 strings start at element 0
2058 Slice(s, 0, 2) will return elements 0, 1 but not 2
2059 Slice(s, 1, 3) will return elements 1, 2 but not 3
2060 Slice(s, 2, 0) will return elements 2..max
2061 Slice(s, 3, -1) will return elements 3..max-1
2062 Slice(s, 4, -2) will return elements 4..max-2
2063 */
2064
2065 extern "C" DynamicStrings_String DynamicStrings_Slice (DynamicStrings_String s, int low, int high)
2066 {
2067 DynamicStrings_String__opaque d;
2068 DynamicStrings_String__opaque t;
2069 int start;
2070 int stop;
2071 int o;
2072
2073 if (PoisonOn)
2074 {
2075 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2076 }
2077 if (low < 0)
2078 {
2079 low = ((int ) (DynamicStrings_Length (s)))+low;
2080 }
2081 if (high <= 0)
2082 {
2083 high = ((int ) (DynamicStrings_Length (s)))+high;
2084 }
2085 else
2086 {
2087 /* make sure high is <= Length (s) */
2088 high = Min (DynamicStrings_Length (s), static_cast<unsigned int> (high));
2089 }
2090 d = static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitString ((const char *) "", 0));
2091 d = AddToGarbage (d, static_cast<DynamicStrings_String__opaque> (s));
2092 o = 0;
2093 t = d;
2094 while (s != NULL)
2095 {
2096 if (low < (o+((int ) (static_cast<DynamicStrings_String__opaque> (s)->contents.len))))
2097 {
2098 if (o > high)
2099 {
2100 s = static_cast<DynamicStrings_String> (NULL);
2101 }
2102 else
2103 {
2104 /* found sliceable unit */
2105 if (low < o)
2106 {
2107 start = 0;
2108 }
2109 else
2110 {
2111 start = low-o;
2112 }
2113 stop = Max (Min (MaxBuf, static_cast<unsigned int> (high-o)), 0);
2114 while (t->contents.len == MaxBuf)
2115 {
2116 if (t->contents.next == NULL)
2117 {
2118 Storage_ALLOCATE ((void **) &t->contents.next, sizeof (DynamicStrings_stringRecord));
2119 t->contents.next->head = NULL;
2120 t->contents.next->contents.len = 0;
2121 AddDebugInfo (t->contents.next);
2122 if (TraceOn)
2123 {
2124 t->contents.next = AssignDebug (t->contents.next, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1405, (const char *) "Slice", 5);
2125 }
2126 }
2127 t = t->contents.next;
2128 }
2129 ConcatContentsAddress (&t->contents, &static_cast<DynamicStrings_String__opaque> (s)->contents.buf.array[start], static_cast<unsigned int> (stop-start));
2130 o += static_cast<DynamicStrings_String__opaque> (s)->contents.len;
2131 s = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next);
2132 }
2133 }
2134 else
2135 {
2136 o += static_cast<DynamicStrings_String__opaque> (s)->contents.len;
2137 s = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next);
2138 }
2139 }
2140 if (TraceOn)
2141 {
2142 d = AssignDebug (d, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1422, (const char *) "Slice", 5);
2143 }
2144 return static_cast<DynamicStrings_String> (d);
2145 /* static analysis guarentees a RETURN statement will be used before here. */
2146 __builtin_unreachable ();
2147 }
2148
2149
2150 /*
2151 Index - returns the indice of the first occurance of, ch, in
2152 String, s. -1 is returned if, ch, does not exist.
2153 The search starts at position, o.
2154 */
2155
2156 extern "C" int DynamicStrings_Index (DynamicStrings_String s, char ch, unsigned int o)
2157 {
2158 unsigned int i;
2159 unsigned int k;
2160
2161 if (PoisonOn)
2162 {
2163 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2164 }
2165 k = 0;
2166 while (s != NULL)
2167 {
2168 if ((k+static_cast<DynamicStrings_String__opaque> (s)->contents.len) < o)
2169 {
2170 k += static_cast<DynamicStrings_String__opaque> (s)->contents.len;
2171 }
2172 else
2173 {
2174 i = o-k;
2175 while (i < static_cast<DynamicStrings_String__opaque> (s)->contents.len)
2176 {
2177 if (static_cast<DynamicStrings_String__opaque> (s)->contents.buf.array[i] == ch)
2178 {
2179 return k+i;
2180 }
2181 i += 1;
2182 }
2183 k += i;
2184 o = k;
2185 }
2186 s = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next);
2187 }
2188 return -1;
2189 /* static analysis guarentees a RETURN statement will be used before here. */
2190 __builtin_unreachable ();
2191 }
2192
2193
2194 /*
2195 RIndex - returns the indice of the last occurance of, ch,
2196 in String, s. The search starts at position, o.
2197 -1 is returned if, ch, is not found. The search
2198 is performed left to right.
2199 */
2200
2201 extern "C" int DynamicStrings_RIndex (DynamicStrings_String s, char ch, unsigned int o)
2202 {
2203 unsigned int i;
2204 unsigned int k;
2205 int j;
2206
2207 if (PoisonOn)
2208 {
2209 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2210 }
2211 j = -1;
2212 k = 0;
2213 while (s != NULL)
2214 {
2215 if ((k+static_cast<DynamicStrings_String__opaque> (s)->contents.len) < o)
2216 {
2217 k += static_cast<DynamicStrings_String__opaque> (s)->contents.len;
2218 }
2219 else
2220 {
2221 if (o < k)
2222 {
2223 i = 0;
2224 }
2225 else
2226 {
2227 i = o-k;
2228 }
2229 while (i < static_cast<DynamicStrings_String__opaque> (s)->contents.len)
2230 {
2231 if (static_cast<DynamicStrings_String__opaque> (s)->contents.buf.array[i] == ch)
2232 {
2233 j = k;
2234 }
2235 k += 1;
2236 i += 1;
2237 }
2238 }
2239 s = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next);
2240 }
2241 return j;
2242 /* static analysis guarentees a RETURN statement will be used before here. */
2243 __builtin_unreachable ();
2244 }
2245
2246
2247 /*
2248 ReverseIndex - returns the indice of the last occurance of ch
2249 in String s. The search starts at position o
2250 and searches from right to left. The start position
2251 may be indexed negatively from the right (-1 is the
2252 last index).
2253 The return value if ch is found will always be positive.
2254 -1 is returned if ch is not found.
2255 */
2256
2257 extern "C" int DynamicStrings_ReverseIndex (DynamicStrings_String s, char ch, int o)
2258 {
2259 unsigned int c;
2260
2261 if (PoisonOn)
2262 {
2263 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2264 }
2265 if (o < 0)
2266 {
2267 o = ((int ) (DynamicStrings_Length (s)))+o;
2268 if (o < 0)
2269 {
2270 return -1;
2271 }
2272 }
2273 if (((unsigned int ) (o)) < (DynamicStrings_Length (s)))
2274 {
2275 while (o >= 0)
2276 {
2277 if ((DynamicStrings_char (s, o)) == ch)
2278 {
2279 return o;
2280 }
2281 else
2282 {
2283 o -= 1;
2284 }
2285 }
2286 }
2287 return -1;
2288 /* static analysis guarentees a RETURN statement will be used before here. */
2289 __builtin_unreachable ();
2290 }
2291
2292
2293 /*
2294 RemoveComment - assuming that, comment, is a comment delimiter
2295 which indicates anything to its right is a comment
2296 then strip off the comment and also any white space
2297 on the remaining right hand side.
2298 It leaves any white space on the left hand side alone.
2299 */
2300
2301 extern "C" DynamicStrings_String DynamicStrings_RemoveComment (DynamicStrings_String s, char comment)
2302 {
2303 int i;
2304
2305 i = DynamicStrings_Index (s, comment, 0);
2306 if (i == 0)
2307 {
2308 s = DynamicStrings_InitString ((const char *) "", 0);
2309 }
2310 else if (i > 0)
2311 {
2312 /* avoid dangling else. */
2313 s = DynamicStrings_RemoveWhitePostfix (DynamicStrings_Slice (DynamicStrings_Mark (s), 0, i));
2314 }
2315 if (TraceOn)
2316 {
2317 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (s), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1576, (const char *) "RemoveComment", 13));
2318 }
2319 return s;
2320 /* static analysis guarentees a RETURN statement will be used before here. */
2321 __builtin_unreachable ();
2322 }
2323
2324
2325 /*
2326 RemoveWhitePrefix - removes any leading white space from String, s.
2327 A new string is returned.
2328 */
2329
2330 extern "C" DynamicStrings_String DynamicStrings_RemoveWhitePrefix (DynamicStrings_String s)
2331 {
2332 unsigned int i;
2333
2334 i = 0;
2335 while (IsWhite (DynamicStrings_char (s, static_cast<int> (i))))
2336 {
2337 i += 1;
2338 }
2339 s = DynamicStrings_Slice (s, (int ) (i), 0);
2340 if (TraceOn)
2341 {
2342 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (s), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1688, (const char *) "RemoveWhitePrefix", 17));
2343 }
2344 return s;
2345 /* static analysis guarentees a RETURN statement will be used before here. */
2346 __builtin_unreachable ();
2347 }
2348
2349
2350 /*
2351 RemoveWhitePostfix - removes any leading white space from String, s.
2352 A new string is returned.
2353 */
2354
2355 extern "C" DynamicStrings_String DynamicStrings_RemoveWhitePostfix (DynamicStrings_String s)
2356 {
2357 int i;
2358
2359 i = ((int ) (DynamicStrings_Length (s)))-1;
2360 while ((i >= 0) && (IsWhite (DynamicStrings_char (s, i))))
2361 {
2362 i -= 1;
2363 }
2364 s = DynamicStrings_Slice (s, 0, i+1);
2365 if (TraceOn)
2366 {
2367 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (s), (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, 1710, (const char *) "RemoveWhitePostfix", 18));
2368 }
2369 return s;
2370 /* static analysis guarentees a RETURN statement will be used before here. */
2371 __builtin_unreachable ();
2372 }
2373
2374
2375 /*
2376 ToUpper - returns string, s, after it has had its lower case characters
2377 replaced by upper case characters.
2378 The string, s, is not duplicated.
2379 */
2380
2381 extern "C" DynamicStrings_String DynamicStrings_ToUpper (DynamicStrings_String s)
2382 {
2383 char ch;
2384 unsigned int i;
2385 DynamicStrings_String__opaque t;
2386
2387 if (s != NULL)
2388 {
2389 MarkInvalid (static_cast<DynamicStrings_String__opaque> (s));
2390 t = static_cast<DynamicStrings_String__opaque> (s);
2391 while (t != NULL)
2392 {
2393 i = 0;
2394 while (i < t->contents.len)
2395 {
2396 ch = t->contents.buf.array[i];
2397 if ((ch >= 'a') && (ch <= 'z'))
2398 {
2399 t->contents.buf.array[i] = ((char) (( ((unsigned int) (ch))- ((unsigned int) ('a')))+ ((unsigned int) ('A'))));
2400 }
2401 i += 1;
2402 }
2403 t = t->contents.next;
2404 }
2405 }
2406 return s;
2407 /* static analysis guarentees a RETURN statement will be used before here. */
2408 __builtin_unreachable ();
2409 }
2410
2411
2412 /*
2413 ToLower - returns string, s, after it has had its upper case characters
2414 replaced by lower case characters.
2415 The string, s, is not duplicated.
2416 */
2417
2418 extern "C" DynamicStrings_String DynamicStrings_ToLower (DynamicStrings_String s)
2419 {
2420 char ch;
2421 unsigned int i;
2422 DynamicStrings_String__opaque t;
2423
2424 if (s != NULL)
2425 {
2426 MarkInvalid (static_cast<DynamicStrings_String__opaque> (s));
2427 t = static_cast<DynamicStrings_String__opaque> (s);
2428 while (t != NULL)
2429 {
2430 i = 0;
2431 while (i < t->contents.len)
2432 {
2433 ch = t->contents.buf.array[i];
2434 if ((ch >= 'A') && (ch <= 'Z'))
2435 {
2436 t->contents.buf.array[i] = ((char) (( ((unsigned int) (ch))- ((unsigned int) ('A')))+ ((unsigned int) ('a'))));
2437 }
2438 i += 1;
2439 }
2440 t = t->contents.next;
2441 }
2442 }
2443 return s;
2444 /* static analysis guarentees a RETURN statement will be used before here. */
2445 __builtin_unreachable ();
2446 }
2447
2448
2449 /*
2450 CopyOut - copies string, s, to a.
2451 */
2452
2453 extern "C" void DynamicStrings_CopyOut (char *a, unsigned int _a_high, DynamicStrings_String s)
2454 {
2455 unsigned int i;
2456 unsigned int l;
2457
2458 l = Min (_a_high+1, DynamicStrings_Length (s));
2459 i = 0;
2460 while (i < l)
2461 {
2462 const_cast<char *>(a)[i] = DynamicStrings_char (s, static_cast<int> (i));
2463 i += 1;
2464 }
2465 if (i <= _a_high)
2466 {
2467 const_cast<char *>(a)[i] = ASCII_nul;
2468 }
2469 }
2470
2471
2472 /*
2473 char - returns the character, ch, at position, i, in String, s.
2474 */
2475
2476 extern "C" char DynamicStrings_char (DynamicStrings_String s, int i)
2477 {
2478 unsigned int c;
2479
2480 if (PoisonOn)
2481 {
2482 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2483 }
2484 if (i < 0)
2485 {
2486 c = (unsigned int ) (((int ) (DynamicStrings_Length (s)))+i);
2487 }
2488 else
2489 {
2490 c = i;
2491 }
2492 while ((s != NULL) && (c >= static_cast<DynamicStrings_String__opaque> (s)->contents.len))
2493 {
2494 c -= static_cast<DynamicStrings_String__opaque> (s)->contents.len;
2495 s = static_cast<DynamicStrings_String> (static_cast<DynamicStrings_String__opaque> (s)->contents.next);
2496 }
2497 if ((s == NULL) || (c >= static_cast<DynamicStrings_String__opaque> (s)->contents.len))
2498 {
2499 return ASCII_nul;
2500 }
2501 else
2502 {
2503 return static_cast<DynamicStrings_String__opaque> (s)->contents.buf.array[c];
2504 }
2505 /* static analysis guarentees a RETURN statement will be used before here. */
2506 __builtin_unreachable ();
2507 }
2508
2509
2510 /*
2511 string - returns the C style char * of String, s.
2512 */
2513
2514 extern "C" void * DynamicStrings_string (DynamicStrings_String s)
2515 {
2516 typedef char *string__T2;
2517
2518 DynamicStrings_String__opaque a;
2519 unsigned int l;
2520 unsigned int i;
2521 string__T2 p;
2522
2523 if (PoisonOn)
2524 {
2525 s = static_cast<DynamicStrings_String> (CheckPoisoned (static_cast<DynamicStrings_String__opaque> (s)));
2526 }
2527 if (s == NULL)
2528 {
2529 return NULL;
2530 }
2531 else
2532 {
2533 if (! static_cast<DynamicStrings_String__opaque> (s)->head->charStarValid)
2534 {
2535 l = DynamicStrings_Length (s);
2536 if (! (static_cast<DynamicStrings_String__opaque> (s)->head->charStarUsed && (static_cast<DynamicStrings_String__opaque> (s)->head->charStarSize > l)))
2537 {
2538 DeallocateCharStar (static_cast<DynamicStrings_String__opaque> (s));
2539 Storage_ALLOCATE (&static_cast<DynamicStrings_String__opaque> (s)->head->charStar, l+1);
2540 static_cast<DynamicStrings_String__opaque> (s)->head->charStarSize = l+1;
2541 static_cast<DynamicStrings_String__opaque> (s)->head->charStarUsed = true;
2542 }
2543 p = static_cast<string__T2> (static_cast<DynamicStrings_String__opaque> (s)->head->charStar);
2544 a = static_cast<DynamicStrings_String__opaque> (s);
2545 while (a != NULL)
2546 {
2547 i = 0;
2548 while (i < a->contents.len)
2549 {
2550 (*p) = a->contents.buf.array[i];
2551 i += 1;
2552 p += 1;
2553 }
2554 a = a->contents.next;
2555 }
2556 (*p) = ASCII_nul;
2557 static_cast<DynamicStrings_String__opaque> (s)->head->charStarValid = true;
2558 }
2559 return static_cast<DynamicStrings_String__opaque> (s)->head->charStar;
2560 }
2561 /* static analysis guarentees a RETURN statement will be used before here. */
2562 __builtin_unreachable ();
2563 }
2564
2565
2566 /*
2567 InitStringDB - the debug version of InitString.
2568 */
2569
2570 extern "C" DynamicStrings_String DynamicStrings_InitStringDB (const char *a_, unsigned int _a_high, const char *file_, unsigned int _file_high, unsigned int line)
2571 {
2572 char a[_a_high+1];
2573 char file[_file_high+1];
2574
2575 /* make a local copy of each unbounded array. */
2576 memcpy (a, a_, _a_high+1);
2577 memcpy (file, file_, _file_high+1);
2578
2579 return static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitString ((const char *) a, _a_high)), (const char *) file, _file_high, line, (const char *) "InitString", 10));
2580 /* static analysis guarentees a RETURN statement will be used before here. */
2581 __builtin_unreachable ();
2582 }
2583
2584
2585 /*
2586 InitStringCharStarDB - the debug version of InitStringCharStar.
2587 */
2588
2589 extern "C" DynamicStrings_String DynamicStrings_InitStringCharStarDB (void * a, const char *file_, unsigned int _file_high, unsigned int line)
2590 {
2591 char file[_file_high+1];
2592
2593 /* make a local copy of each unbounded array. */
2594 memcpy (file, file_, _file_high+1);
2595
2596 return static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitStringCharStar (a)), (const char *) file, _file_high, line, (const char *) "InitStringCharStar", 18));
2597 /* static analysis guarentees a RETURN statement will be used before here. */
2598 __builtin_unreachable ();
2599 }
2600
2601
2602 /*
2603 InitStringCharDB - the debug version of InitStringChar.
2604 */
2605
2606 extern "C" DynamicStrings_String DynamicStrings_InitStringCharDB (char ch, const char *file_, unsigned int _file_high, unsigned int line)
2607 {
2608 char file[_file_high+1];
2609
2610 /* make a local copy of each unbounded array. */
2611 memcpy (file, file_, _file_high+1);
2612
2613 return static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_InitStringChar (ch)), (const char *) file, _file_high, line, (const char *) "InitStringChar", 14));
2614 /* static analysis guarentees a RETURN statement will be used before here. */
2615 __builtin_unreachable ();
2616 }
2617
2618
2619 /*
2620 MultDB - the debug version of MultDB.
2621 */
2622
2623 extern "C" DynamicStrings_String DynamicStrings_MultDB (DynamicStrings_String s, unsigned int n, const char *file_, unsigned int _file_high, unsigned int line)
2624 {
2625 char file[_file_high+1];
2626
2627 /* make a local copy of each unbounded array. */
2628 memcpy (file, file_, _file_high+1);
2629
2630 return static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_Mult (s, n)), (const char *) file, _file_high, line, (const char *) "Mult", 4));
2631 /* static analysis guarentees a RETURN statement will be used before here. */
2632 __builtin_unreachable ();
2633 }
2634
2635
2636 /*
2637 DupDB - the debug version of Dup.
2638 */
2639
2640 extern "C" DynamicStrings_String DynamicStrings_DupDB (DynamicStrings_String s, const char *file_, unsigned int _file_high, unsigned int line)
2641 {
2642 char file[_file_high+1];
2643
2644 /* make a local copy of each unbounded array. */
2645 memcpy (file, file_, _file_high+1);
2646
2647 return static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_Dup (s)), (const char *) file, _file_high, line, (const char *) "Dup", 3));
2648 /* static analysis guarentees a RETURN statement will be used before here. */
2649 __builtin_unreachable ();
2650 }
2651
2652
2653 /*
2654 SliceDB - debug version of Slice.
2655 */
2656
2657 extern "C" DynamicStrings_String DynamicStrings_SliceDB (DynamicStrings_String s, int low, int high, const char *file_, unsigned int _file_high, unsigned int line)
2658 {
2659 char file[_file_high+1];
2660
2661 /* make a local copy of each unbounded array. */
2662 memcpy (file, file_, _file_high+1);
2663
2664 DSdbEnter ();
2665 s = static_cast<DynamicStrings_String> (AssignDebug (static_cast<DynamicStrings_String__opaque> (DynamicStrings_Slice (s, low, high)), (const char *) file, _file_high, line, (const char *) "Slice", 5));
2666 DSdbExit (static_cast<DynamicStrings_String__opaque> (s));
2667 return s;
2668 /* static analysis guarentees a RETURN statement will be used before here. */
2669 __builtin_unreachable ();
2670 }
2671
2672
2673 /*
2674 PushAllocation - pushes the current allocation/deallocation lists.
2675 */
2676
2677 extern "C" void DynamicStrings_PushAllocation (void)
2678 {
2679 DynamicStrings_frame f;
2680
2681 if (CheckOn)
2682 {
2683 Init ();
2684 Storage_ALLOCATE ((void **) &f, sizeof (DynamicStrings_frameRec));
2685 f->next = frameHead;
2686 f->alloc = static_cast<DynamicStrings_String__opaque> (NULL);
2687 f->dealloc = static_cast<DynamicStrings_String__opaque> (NULL);
2688 frameHead = f;
2689 }
2690 }
2691
2692
2693 /*
2694 PopAllocation - test to see that all strings are deallocated since
2695 the last push. Then it pops to the previous
2696 allocation/deallocation lists.
2697
2698 If halt is true then the application terminates
2699 with an exit code of 1.
2700 */
2701
2702 extern "C" void DynamicStrings_PopAllocation (bool halt)
2703 {
2704 if (CheckOn)
2705 {
2706 if ((DynamicStrings_PopAllocationExemption (halt, static_cast<DynamicStrings_String> (NULL))) == NULL)
2707 {} /* empty. */
2708 }
2709 }
2710
2711
2712 /*
2713 PopAllocationExemption - test to see that all strings are deallocated, except
2714 string e since the last push.
2715 Post-condition: it pops to the previous allocation/deallocation
2716 lists.
2717
2718 If halt is true then the application terminates
2719 with an exit code of 1.
2720 */
2721
2722 extern "C" DynamicStrings_String DynamicStrings_PopAllocationExemption (bool halt, DynamicStrings_String e)
2723 {
2724 DynamicStrings_String__opaque s;
2725 bool b;
2726
2727 Init ();
2728 if (CheckOn)
2729 {
2730 /* avoid gcc warning by using compound statement even if not strictly necessary. */
2731 if (frameHead == NULL)
2732 {
2733 stop ();
2734 M2RTS_Halt ((const char *) "mismatched number of PopAllocation's compared to PushAllocation's", 65, (const char *) "../../gcc/m2/gm2-libs/DynamicStrings.mod", 40, (const char *) "PopAllocationExemption", 22, 174);
2735 }
2736 else
2737 {
2738 /* writeString ("mismatched number of PopAllocation's compared to PushAllocation's") */
2739 if (frameHead->alloc != NULL)
2740 {
2741 b = false;
2742 s = frameHead->alloc;
2743 while (s != NULL)
2744 {
2745 if (! (((e == s) || (IsOnGarbage (static_cast<DynamicStrings_String__opaque> (e), s))) || (IsOnGarbage (s, static_cast<DynamicStrings_String__opaque> (e)))))
2746 {
2747 if (! b)
2748 {
2749 writeString ((const char *) "the following strings have been lost", 36);
2750 writeLn ();
2751 b = true;
2752 }
2753 DumpStringInfo (s, 0);
2754 }
2755 s = s->debug.next;
2756 }
2757 if (b && halt)
2758 {
2759 libc_exit (1);
2760 }
2761 }
2762 frameHead = frameHead->next;
2763 }
2764 }
2765 return e;
2766 /* static analysis guarentees a RETURN statement will be used before here. */
2767 __builtin_unreachable ();
2768 }
2769
2770 extern "C" void _M2_DynamicStrings_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
2771 {
2772 Initialized = false;
2773 Init ();
2774 }
2775
2776 extern "C" void _M2_DynamicStrings_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
2777 {
2778 }