-
Notifications
You must be signed in to change notification settings - Fork 55
/
GenC.fu
3949 lines (3772 loc) · 102 KB
/
GenC.fu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// GenC.fu - C code generator
//
// Copyright (C) 2011-2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
//
// Fusion Transpiler is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Fusion Transpiler is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Fusion Transpiler. If not, see http://www.gnu.org/licenses/
public class GenC : GenCCpp
{
SortedSet<FuId>() IntFunctions;
SortedSet<FuId>() LongFunctions;
bool IntTryParse;
bool LongTryParse;
bool DoubleTryParse;
bool StringAssign;
bool StringSubstring;
bool StringAppend;
bool StringIndexOf;
bool StringLastIndexOf;
bool StringEndsWith;
bool StringReplace;
bool StringFormat;
bool MatchFind;
bool MatchPos;
bool PtrConstruct;
bool SharedMake;
bool SharedAddRef;
bool SharedRelease;
bool SharedAssign;
SortedSet<FuId>() ListFrees;
bool TreeCompareInteger;
bool TreeCompareString;
SortedSet<FuId>() Compares;
SortedSet<FuId>() Contains;
List<FuVar>() VarsToDestruct;
bool ConditionVarInScope;
protected FuClass CurrentClass;
protected override string GetTargetName() => "C";
protected override void WriteSelfDoc!(FuMethod method)
{
if (method.CallType == FuCallType.Static)
return;
Write(" * @param self This <code>");
WriteName(method.Parent);
WriteLine("</code>.");
}
protected override void WriteReturnDoc!(FuMethod method)
{
if (method.Throws.Count == 0)
return;
Write(" * @return <code>");
WriteThrowReturnValue(method.Type, false);
WriteLine("</code> on error.");
}
protected override void WriteThrowsDoc!(FuThrowsDeclaration decl)
{
}
protected override void IncludeStdInt!()
{
Include("stdint.h");
}
protected override void IncludeStdDef!()
{
Include("stddef.h");
}
protected override void IncludeAssert!()
{
Include("assert.h");
}
protected override void IncludeMath!()
{
Include("math.h");
}
protected virtual void IncludeStdBool!()
{
Include("stdbool.h");
}
internal override void VisitLiteralNull!()
{
Write("NULL");
}
protected virtual void WritePrintfLongPrefix!()
{
Write("ll");
}
protected override void WritePrintfWidth!(FuInterpolatedPart part)
{
base.WritePrintfWidth(part);
if (IsStringSubstring(part.Argument) != null) {
assert part.Precision < 0;
Write(".*");
}
if (part.Argument.Type.Id == FuId.NIntType)
WriteChar('t');
else if (part.Argument.Type.Id == FuId.LongType)
WritePrintfLongPrefix();
}
protected virtual void WriteInterpolatedStringArgBase!(FuExpr expr)
{
if (expr.Type.Id == FuId.LongType) {
Write("(long long) ");
expr.Accept(this, FuPriority.Primary);
}
else
expr.Accept(this, FuPriority.Argument);
}
void WriteStringPtrAdd!(FuCallExpr call, bool cast)
{
if (IsUTF8GetString(call)) {
if (cast)
Write("(const char *) ");
WriteArrayPtrAdd(call.Arguments[0], call.Arguments[1]);
}
else
WriteArrayPtrAdd(call.Method.Left, call.Arguments[0]);
}
static bool IsDictionaryClassStgIndexing(FuExpr expr)
{
return expr is FuBinaryExpr indexing
&& indexing.Op == FuToken.LeftBracket
&& indexing.Left.Type is FuClassType dict
&& dict.Class.TypeParameterCount == 2
&& dict.GetValueType() is FuStorageType;
}
protected override void StartTemporaryVar!(FuType type)
{
// overridden just to avoid the unnecessary space
StartDefinition(type, true, true);
}
void WriteUpcast!(FuClass resultClass, FuSymbol klass)
{
for (; klass != resultClass; klass = klass.Parent)
Write(".base");
}
void WriteClassPtr!(FuClass resultClass, FuExpr expr, FuPriority parent)
{
switch (expr.Type) {
case FuStorageType storage when storage.Class.Id == FuId.None && !IsDictionaryClassStgIndexing(expr):
WriteChar('&');
expr.Accept(this, FuPriority.Primary);
WriteUpcast(resultClass, storage.Class);
break;
case FuClassType ptr when ptr.Class != resultClass:
WriteChar('&');
WritePostfix(expr, "->base");
WriteUpcast(resultClass, ptr.Class.Parent);
break;
default:
expr.Accept(this, parent);
break;
}
}
protected override void WriteInterpolatedStringArg!(FuExpr expr)
{
FuCallExpr? call = IsStringSubstring(expr);
if (call != null) {
GetStringSubstringLength(call).Accept(this, FuPriority.Argument);
Write(", ");
WriteStringPtrAdd(call, true);
}
else if (expr.Type is FuClassType klass && klass.Class.Id != FuId.StringClass) {
// TODO: abstract, virtual, override
Write(this.Namespace);
Write(klass.Class.Name);
Write("_ToString(");
WriteClassPtr(klass.Class, expr, FuPriority.Argument);
WriteChar(')');
}
else
WriteInterpolatedStringArgBase(expr);
}
internal override void VisitInterpolatedString!(FuInterpolatedString expr, FuPriority parent)
{
if (TryWriteTemporary(expr))
return;
Include("stdarg.h");
Include("stdio.h");
this.StringFormat = true;
Write("FuString_Format(");
WritePrintf(expr, false);
}
protected virtual void WriteCamelCaseNotKeyword!(string name)
{
switch (name) {
case "this":
Write("self");
break;
case "Asm":
case "Assert":
case "Auto":
case "Bool":
case "Break":
case "Byte":
case "Case":
case "Char":
case "Class":
case "Const":
case "Continue":
case "Default":
case "Do":
case "Double":
case "Else":
case "Enum":
case "Extern":
case "False":
case "Float":
case "For":
case "Foreach":
case "Goto":
case "If":
case "Inline":
case "Int":
case "Long":
case "Register":
case "Restrict":
case "Return":
case "Short":
case "Signed":
case "Sizeof":
case "Static":
case "Struct":
case "Switch":
case "True":
case "Typedef":
case "Typeof": // gcc extension
case "Union":
case "Unsigned":
case "Void":
case "Volatile":
case "While":
case "asm":
case "auto":
case "char":
case "extern":
case "goto":
case "inline":
case "register":
case "restrict":
case "signed":
case "sizeof":
case "struct":
case "typedef":
case "typeof": // gcc extension
case "union":
case "unsigned":
case "volatile":
WriteCamelCase(name);
WriteChar('_');
break;
default:
WriteCamelCase(name);
break;
}
}
protected override void WriteName!(FuSymbol symbol)
{
switch (symbol) {
case FuContainerType:
Write(this.Namespace);
Write(symbol.Name);
break;
case FuMethod:
Write(this.Namespace);
Write(symbol.Parent.Name);
WriteChar('_');
Write(symbol.Name);
break;
case FuConst:
if (symbol.Parent is FuContainerType) {
Write(this.Namespace);
Write(symbol.Parent.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
break;
default:
WriteCamelCaseNotKeyword(symbol.Name);
break;
}
}
void WriteForeachArrayIndexing!(FuForeach forEach, FuSymbol symbol)
{
if (forEach.Collection.Type.Id == FuId.MainArgsType)
Write("argv");
else
forEach.Collection.Accept(this, FuPriority.Primary);
WriteChar('[');
WriteCamelCaseNotKeyword(symbol.Name);
WriteChar(']');
}
void WriteSelfForField!(FuSymbol fieldClass)
{
assert fieldClass is FuClass;
Write("self->");
for (FuSymbol klass = this.CurrentClass; klass != fieldClass; klass = klass.Parent)
Write("base.");
}
protected override void WriteLocalName!(FuSymbol symbol, FuPriority parent)
{
if (symbol.Parent is FuForeach forEach) {
assert forEach.Collection.Type is FuClassType klass;
switch (klass.Class.Id) {
case FuId.StringClass:
case FuId.ListClass when !(klass.GetElementType() is FuStorageType):
if (parent == FuPriority.Primary)
WriteChar('(');
WriteChar('*');
WriteCamelCaseNotKeyword(symbol.Name);
if (parent == FuPriority.Primary)
WriteChar(')');
return;
case FuId.ArrayStorageClass:
if (klass.GetElementType() is FuStorageType) {
if (parent > FuPriority.Add)
WriteChar('(');
forEach.Collection.Accept(this, FuPriority.Add);
Write(" + ");
WriteCamelCaseNotKeyword(symbol.Name);
if (parent > FuPriority.Add)
WriteChar(')');
}
else
WriteForeachArrayIndexing(forEach, symbol);
return;
default:
break;
}
}
if (symbol is FuField)
WriteSelfForField(symbol.Parent);
WriteName(symbol);
}
void WriteMatchProperty!(FuSymbolReference expr, int which)
{
this.MatchPos = true;
Write("FuMatch_GetPos(");
expr.Left.Accept(this, FuPriority.Argument);
Write(", ");
VisitLiteralLong(which);
WriteChar(')');
}
internal override void VisitSymbolReference!(FuSymbolReference expr, FuPriority parent)
{
switch (expr.Symbol.Id) {
case FuId.StringLength:
WriteStringLength(expr.Left);
break;
case FuId.ConsoleError:
Include("stdio.h");
Write("stderr");
break;
case FuId.ListCount:
case FuId.StackCount:
WritePostfix(expr.Left, "->len");
break;
case FuId.QueueCount:
expr.Left.Accept(this, FuPriority.Primary);
if (expr.Left.Type is FuStorageType)
WriteChar('.');
else
Write("->");
Write("length");
break;
case FuId.HashSetCount:
case FuId.DictionaryCount:
WriteCall("g_hash_table_size", expr.Left);
break;
case FuId.SortedSetCount:
case FuId.SortedDictionaryCount:
WriteCall("g_tree_nnodes", expr.Left);
break;
case FuId.MatchStart:
WriteMatchProperty(expr, 0);
break;
case FuId.MatchEnd:
WriteMatchProperty(expr, 1);
break;
case FuId.MatchLength:
WriteMatchProperty(expr, 2);
break;
case FuId.MatchValue:
if (!TryWriteTemporary(expr)) {
Write("g_match_info_fetch(");
expr.Left.Accept(this, FuPriority.Argument);
Write(", 0)");
}
break;
default:
if (expr.Left == null || expr.Symbol is FuConst)
WriteLocalName(expr.Symbol, parent);
else if (IsDictionaryClassStgIndexing(expr.Left)) {
WritePostfix(expr.Left, "->");
WriteName(expr.Symbol);
}
else if (expr.Left is FuSymbolReference symbol && symbol.Symbol.Parent is FuForeach forEach && forEach.Collection.Type is FuArrayStorageType array) {
// not essential, but a[i].foo looks better than (a + i)->foo
WriteForeachArrayIndexing(forEach, symbol.Symbol);
WriteMemberAccess(array.GetElementType(), expr.Symbol.Parent);
WriteName(expr.Symbol);
}
else
base.VisitSymbolReference(expr, parent);
break;
}
}
void WriteGlib!(string s)
{
Include("glib.h");
Write(s);
}
protected virtual void WriteStringPtrType!()
{
Write("const char *");
}
protected virtual void WriteClassType!(FuClassType klass, bool space)
{
switch (klass.Class.Id) {
case FuId.None:
if (!(klass is FuReadWriteClassType))
Write("const ");
WriteName(klass.Class);
if (!(klass is FuStorageType))
Write(" *");
else if (space)
WriteChar(' ');
break;
case FuId.StringClass:
if (klass.Id == FuId.StringStorageType)
Write("char *");
else
WriteStringPtrType();
break;
case FuId.ListClass:
case FuId.StackClass:
WriteGlib("GArray *");
break;
case FuId.QueueClass:
WriteGlib("GQueue ");
if (!(klass is FuStorageType))
WriteChar('*');
break;
case FuId.HashSetClass:
case FuId.DictionaryClass:
WriteGlib("GHashTable *");
break;
case FuId.SortedSetClass:
case FuId.SortedDictionaryClass:
WriteGlib("GTree *");
break;
case FuId.TextWriterClass:
Include("stdio.h");
Write("FILE *");
break;
case FuId.StringWriterClass:
WriteGlib("GString *");
break;
case FuId.RegexClass:
if (!(klass is FuReadWriteClassType))
Write("const ");
WriteGlib("GRegex *");
break;
case FuId.MatchClass:
if (!(klass is FuReadWriteClassType))
Write("const ");
WriteGlib("GMatchInfo *");
break;
case FuId.LockClass:
NotYet(klass, "Lock");
Include("threads.h");
Write("mtx_t ");
break;
default:
NotSupported(klass, klass.Class.Name);
break;
}
}
void WriteArrayPrefix!(FuType type)
{
if (type is FuClassType array && array.IsArray()) {
WriteArrayPrefix(array.GetElementType());
if (!(type is FuArrayStorageType)) {
if (array.GetElementType() is FuArrayStorageType)
WriteChar('(');
if (!(type is FuReadWriteClassType))
Write("const ");
WriteChar('*');
}
}
}
void StartDefinition!(FuType type, bool promote, bool space)
{
FuType baseType = type.GetBaseType();
switch (baseType) {
case FuIntegerType:
WriteNumericType(GetTypeId(baseType, promote && type == baseType));
if (space)
WriteChar(' ');
break;
case FuEnum:
if (baseType.Id == FuId.BoolType) {
IncludeStdBool();
Write("bool");
}
else
WriteName(baseType);
if (space)
WriteChar(' ');
break;
case FuClassType klass:
WriteClassType(klass, space);
break;
default:
Write(baseType.Name);
if (space)
WriteChar(' ');
break;
}
WriteArrayPrefix(type);
}
void EndDefinition!(FuType type)
{
while (type.IsArray()) {
FuType elementType = type.AsClassType().GetElementType();
if (type is FuArrayStorageType arrayStorage) {
WriteChar('[');
VisitLiteralLong(arrayStorage.Length);
WriteChar(']');
}
else if (elementType is FuArrayStorageType)
WriteChar(')');
type = elementType;
}
}
void WriteReturnType!(FuMethod method)
{
if (method.Type.Id == FuId.VoidType && method.Throws.Count > 0) {
IncludeStdBool();
Write("bool ");
}
else
StartDefinition(method.Type, true, true);
}
protected override void WriteType!(FuType type, bool promote)
{
StartDefinition(type, promote, type is FuClassType arrayPtr && arrayPtr.Class.Id == FuId.ArrayPtrClass);
EndDefinition(type);
}
protected override void WriteTypeAndName!(FuNamedValue value)
{
StartDefinition(value.Type, true, true);
WriteName(value);
EndDefinition(value.Type);
}
void WriteDynamicArrayCast!(FuType elementType)
{
WriteChar('(');
StartDefinition(elementType, false, true);
Write(elementType.IsArray() ? "(*)" : "*");
EndDefinition(elementType);
Write(") ");
}
void WriteXstructorPtr!(bool need, FuClass klass, string name)
{
if (need) {
Write("(FuMethodPtr) ");
WriteName(klass);
WriteChar('_');
Write(name);
}
else
Write("NULL");
}
static bool IsHeapAllocated(FuType type) => type.Id == FuId.StringStorageType || type is FuDynamicPtrType || (type is FuStorageType storage && storage.Class.Id == FuId.MatchClass);
static bool NeedToDestructType(FuType type)
{
if (IsHeapAllocated(type))
return true;
if (type is FuStorageType storage) {
switch (storage.Class.Id) {
case FuId.ListClass:
case FuId.QueueClass:
case FuId.StackClass:
case FuId.HashSetClass:
case FuId.SortedSetClass:
case FuId.DictionaryClass:
case FuId.SortedDictionaryClass:
case FuId.StringWriterClass:
case FuId.MatchClass:
case FuId.LockClass:
return true;
default:
return NeedsDestructor(storage.Class);
}
}
return false;
}
static bool NeedToDestruct(FuSymbol symbol)
{
FuType type = symbol.Type;
while (type is FuArrayStorageType array)
type = array.GetElementType();
return NeedToDestructType(type);
}
static bool NeedsDestructor(FuClass klass)
{
for (FuSymbol? symbol = klass.First; symbol != null; symbol = symbol.Next) {
if (symbol is FuField field && NeedToDestruct(field))
return true;
}
return klass.Parent is FuClass baseClass && NeedsDestructor(baseClass);
}
void WriteXstructorPtrs!(FuClass klass)
{
WriteXstructorPtr(NeedsConstructor(klass), klass, "Construct");
Write(", ");
WriteXstructorPtr(NeedsDestructor(klass), klass, "Destruct");
}
void WriteListFreeName!(FuId id)
{
Write("FuList_Free");
switch (id) {
case FuId.None:
Write("Shared");
break;
case FuId.StringClass:
Write("String");
break;
case FuId.ListClass:
Write("List");
break;
case FuId.QueueClass:
Write("Queue");
break;
case FuId.DictionaryClass:
Write("HashTable");
break;
case FuId.SortedDictionaryClass:
Write("Tree");
break;
case FuId.RegexClass:
Write("Regex");
break;
case FuId.MatchClass:
Write("Match");
break;
default:
assert false;
}
}
void AddListFree!(FuId id)
{
this.ListFrees.Add(id);
WriteListFreeName(id);
}
void WriteListFree!(FuClassType elementType)
{
switch (elementType.Class.Id) {
case FuId.None:
case FuId.ArrayPtrClass:
if (elementType is FuDynamicPtrType) {
this.SharedRelease = true;
AddListFree(FuId.None);
}
else {
Write("(GDestroyNotify) ");
WriteName(elementType.Class);
Write("_Destruct");
}
break;
case FuId.StringClass:
AddListFree(FuId.StringClass);
break;
case FuId.ListClass:
case FuId.StackClass:
AddListFree(FuId.ListClass);
break;
case FuId.QueueClass:
AddListFree(FuId.QueueClass);
break;
case FuId.HashSetClass:
case FuId.DictionaryClass:
AddListFree(FuId.DictionaryClass);
break;
case FuId.SortedSetClass:
case FuId.SortedDictionaryClass:
AddListFree(FuId.SortedDictionaryClass);
break;
case FuId.StringWriterClass:
AddListFree(FuId.StringWriterClass);
break;
case FuId.RegexClass:
AddListFree(FuId.RegexClass);
break;
case FuId.MatchClass:
AddListFree(FuId.MatchClass);
break;
default:
assert false;
}
}
protected override void WriteNewArray!(FuType elementType, FuExpr lengthExpr, FuPriority parent)
{
this.SharedMake = true;
if (parent > FuPriority.Mul)
WriteChar('(');
WriteDynamicArrayCast(elementType);
Write("FuShared_Make(");
lengthExpr.Accept(this, FuPriority.Argument);
Write(", sizeof(");
WriteType(elementType, false);
Write("), ");
switch (elementType) {
case FuStringStorageType:
this.PtrConstruct = true;
Write("(FuMethodPtr) FuPtr_Construct, ");
AddListFree(FuId.StringClass);
break;
case FuStorageType storage:
WriteXstructorPtrs(storage.Class);
break;
case FuDynamicPtrType dynamic:
this.PtrConstruct = true;
Write("(FuMethodPtr) FuPtr_Construct, ");
WriteListFree(dynamic);
break;
default:
Write("NULL, NULL");
break;
}
WriteChar(')');
if (parent > FuPriority.Mul)
WriteChar(')');
}
static FuExpr GetStringSubstringLength(FuCallExpr call) => call.Arguments[IsUTF8GetString(call) ? 2 : 1];
void WriteStringStorageValue!(FuExpr expr)
{
if (expr.IsNewString(false))
expr.Accept(this, FuPriority.Argument);
else {
Include("string.h");
WriteCall("strdup", expr);
}
}
protected override void WriteArrayStorageInit!(FuArrayStorageType array, FuExpr value)
{
switch (value) {
case null:
if (IsHeapAllocated(array.GetStorageType()))
Write(" = { NULL }");
break;
case FuLiteral literal when literal.IsDefaultValue():
Write(" = { ");
literal.Accept(this, FuPriority.Argument);
Write(" }");
break;
default:
assert false;
}
}
static bool IsUnique(FuDynamicPtrType dynamic) => dynamic.Unique && dynamic.Class.Id == FuId.ArrayPtrClass && !HasDictionaryDestroy(dynamic.GetElementType());
bool WriteDestructMethodName!(FuClassType klass)
{
switch (klass.Class.Id) {
case FuId.None:
case FuId.ArrayPtrClass:
case FuId.JsonElementClass:
if (klass is FuDynamicPtrType dynamic) {
if (IsUnique(dynamic))
Write("free");
else {
this.SharedRelease = true;
Write("FuShared_Release");
}
return false;
}
WriteName(klass.Class);
Write("_Destruct");
return true;
case FuId.StringClass:
Write("free");
return false;
case FuId.ListClass:
case FuId.StackClass:
Write("g_array_unref");
return false;
case FuId.QueueClass:
Write(HasDictionaryDestroy(klass.GetElementType()) ? "g_queue_clear_full" : "g_queue_clear");
return true;
case FuId.HashSetClass:
case FuId.DictionaryClass:
Write("g_hash_table_unref");
return false;
case FuId.SortedSetClass:
case FuId.SortedDictionaryClass:
Write("g_tree_unref");
return false;
case FuId.StringWriterClass:
Write("g_string_free");
return false;
case FuId.RegexClass:
Write("g_regex_unref");
return false;
case FuId.MatchClass:
Write("g_match_info_unref");
return false;
case FuId.LockClass:
Write("mtx_destroy");
return true;
default:
assert false;
}
}
void StartDestructCall!(FuClassType klass)
{
bool addressOf = WriteDestructMethodName(klass);
WriteChar('(');
if (addressOf)
WriteChar('&');
}
static bool HasDictionaryDestroy(FuType? type) => type is FuOwningType || type is FuStringStorageType;
void WriteDictionaryDestroy!(FuType? type)
{
switch (type) {
case null: // for Java fut
Write("NULL");
break;
case FuStringStorageType:
case FuArrayStorageType:
Write("free");
break;
case FuOwningType owning:
if (owning.Class.Id == FuId.None) {
if (type is FuStorageType) {
if (NeedsDestructor(owning.Class)) {
Write("(GDestroyNotify) ");
WriteName(owning.Class);
Write("_Delete"); // TODO: emit
}
else
Write("free");
break;
}
}
else
Write("(GDestroyNotify) ");
WriteDestructMethodName(owning);
break;
default:
Write("NULL");
break;
}
}
void WriteHashEqual!(FuType keyType)
{
Write(keyType is FuStringType ? "g_str_hash, g_str_equal" : "NULL, NULL");
}
void WriteNewHashTable!(FuType keyType, FuType? valueType)
{
Write("g_hash_table_new");
if (HasDictionaryDestroy(keyType) || HasDictionaryDestroy(valueType)) {
Write("_full(");
WriteHashEqual(keyType);
Write(", ");
WriteDictionaryDestroy(keyType);
Write(", ");
WriteDictionaryDestroy(valueType);
}
else {
WriteChar('(');
WriteHashEqual(keyType);
}
WriteChar(')');
}
void WriteNewTree!(FuType keyType, FuType? valueType)
{
if (keyType.Id == FuId.StringPtrType && !HasDictionaryDestroy(valueType))
Write("g_tree_new((GCompareFunc) strcmp");
else {
Write("g_tree_new_full(FuTree_Compare");
switch (keyType) {
case FuIntegerType:
this.TreeCompareInteger = true;
Write("Integer");
break;
case FuStringType:
this.TreeCompareString = true;
Write("String");
break;
default:
NotSupported(keyType, keyType.ToString());
break;
}
Write(", NULL, ");
WriteDictionaryDestroy(keyType);
Write(", ");
WriteDictionaryDestroy(valueType);
}
WriteChar(')');
}
protected override void WriteNew!(FuReadWriteClassType klass, FuPriority parent)
{
switch (klass.Class.Id) {
case FuId.ListClass:
case FuId.StackClass:
Write("g_array_new(FALSE, FALSE, sizeof(");
WriteType(klass.GetElementType(), false);
Write("))");
break;
case FuId.QueueClass:
Write("G_QUEUE_INIT");
break;
case FuId.HashSetClass:
WriteNewHashTable(klass.GetElementType(), null);
break;
case FuId.SortedSetClass:
WriteNewTree(klass.GetElementType(), null);
break;
case FuId.DictionaryClass:
WriteNewHashTable(klass.GetKeyType(), klass.GetValueType());
break;
case FuId.SortedDictionaryClass:
WriteNewTree(klass.GetKeyType(), klass.GetValueType());
break;
case FuId.StringWriterClass:
Write("g_string_new(NULL)");
break;
default:
this.SharedMake = true;