Debian Codegraph @

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