forked from bmx-ng/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctranslator.bmx
5804 lines (4818 loc) · 166 KB
/
ctranslator.bmx
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
' Copyright (c) 2013-2016 Bruce A Henderson
'
' Based on the public domain Monkey "trans" by Mark Sibly
'
' This software is provided 'as-is', without any express or implied
' warranty. In no event will the authors be held liable for any damages
' arising from the use of this software.
'
' Permission is granted to anyone to use this software for any purpose,
' including commercial applications, and to alter it and redistribute it
' freely, subject to the following restrictions:
'
' 1. The origin of this software must not be misrepresented; you must not
' claim that you wrote the original software. If you use this software
' in a product, an acknowledgment in the product documentation would be
' appreciated but is not required.
'
' 2. Altered source versions must be plainly marked as such, and must not be
' misrepresented as being the original software.
'
' 3. This notice may not be removed or altered from any source
' distribution.
'
SuperStrict
Import "parser.bmx"
Type TCTranslator Extends TTranslator
'Field stringConstCount:Int
Field prefix:String
Field reserved_methods:String = ",New,Delete,ToString,Compare,SendMessage,_reserved1_,_reserved2_,_reserved3_,".ToLower()
Method New()
_trans = Self
End Method
Method TransSPointer$( ty:TType, withVar:Int = False )
Local p:String
If ty
If withVar And (ty._flags & TType.T_VAR) Then
p:+ "*"
End If
If ty._flags & TType.T_PTR Then
p:+ "*"
Else If ty._flags & TType.T_PTRPTR Then
p:+ "**"
Else If ty._flags & TType.T_PTRPTRPTR Then
p:+ "***"
End If
End If
Return p
End Method
Method TransArrayType$( ty:TType)
Local p:String = TransSPointer(ty)
If TBoolType( ty ) Return "~q" + p + "i~q"
If TByteType( ty ) Return "~q" + p + "b~q"
If TShortType( ty ) Return "~q" + p + "s~q"
If TIntType( ty ) Return "~q" + p + "i~q"
If TUIntType( ty ) Return "~q" + p + "u~q"
If TFloatType( ty ) Return "~q" + p + "f~q"
If TDoubleType( ty ) Return "~q" + p + "d~q"
If TLongType( ty ) Return "~q" + p + "l~q"
If TULongType( ty ) Return "~q" + p + "y~q"
If TSizeTType( ty ) Return "~q" + p + "z~q"
If TWParamType( ty ) Return "~q" + p + "w~q"
If TLParamType( ty ) Return "~q" + p + "x~q"
If TStringType( ty ) Return "~q$~q"
If TInt128Type( ty ) Return "~q" + p + "j~q"
If TFloat128Type( ty ) Return "~q" + p + "k~q"
If TDouble128Type( ty ) Return "~q" + p + "m~q"
If TFloat64Type( ty ) Return "~q" + p + "h~q"
If TArrayType( ty ) Then
Local s:String = "["
For Local i:Int = 0 Until TArrayType( ty ).dims - 1
s:+ ","
Next
s:+ "]"
s:+ TransArrayType(TArrayType( ty ).elemType)
Return Enquote(s.Replace("~q", ""))
End If
If TObjectType( ty ) Then
If TObjectType( ty ).classdecl.IsStruct()
Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
Else
If Not TObjectType( ty ).classdecl.IsExtern()
Return "~q:" + TObjectType(ty).classDecl.ident + "~q"
Else
If TObjectType( ty ).classdecl.IsInterface() Then
Return "~q" + p + "*#" + TObjectType(ty).classDecl.ident + "~q"
' ElseIf TObjectType( ty ).classdecl.IsStruct()
' Return "~q" + p + "@" + TObjectType(ty).classDecl.ident + "~q"
Else
Return "~q" + p + "#" + TObjectType(ty).classDecl.ident + "~q"
End If
End If
End If
End If
If TFunctionPtrType( ty ) Return "~q(~q"
End Method
Method TransDefDataType$( ty:TType)
If TByteType( ty ) Return "~qb~q"
If TShortType( ty ) Return "~qs~q"
If TIntType( ty ) Return "~qi~q"
If TUIntType( ty ) Return "~qu~q"
If TFloatType( ty ) Return "~qf~q"
If TDoubleType( ty ) Return "~qd~q"
If TLongType( ty ) Return "~ql~q"
If TULongType( ty ) Return "~qy~q"
If TSizeTType( ty ) Return "~qz~q"
If TStringType( ty ) Return "~q$~q"
If TWParamType( ty ) Return "~qw~q"
If TLParamType( ty ) Return "~qx~q"
End Method
Method TransDefDataConversion$(ty:TType)
If TByteType( ty ) Return "bbConvertToInt"
If TShortType( ty ) Return "bbConvertToInt"
If TIntType( ty ) Return "bbConvertToInt"
If TUIntType( ty ) Return "bbConvertToUInt"
If TFloatType( ty ) Return "bbConvertToFloat"
If TDoubleType( ty ) Return "bbConvertToDouble"
If TLongType( ty ) Return "bbConvertToLong"
If TULongType( ty ) Return "bbConvertToULong"
If TSizeTType( ty ) Return "bbConvertToSizet"
If TStringType( ty ) Return "bbConvertToString"
End Method
Method TransDefDataUnionType$(ty:TType)
If TByteType( ty ) Return "b"
If TShortType( ty ) Return "s"
If TIntType( ty ) Return "i"
If TUIntType( ty ) Return "u"
If TFloatType( ty ) Return "f"
If TDoubleType( ty ) Return "d"
If TLongType( ty ) Return "l"
If TULongType( ty ) Return "y"
If TSizeTType( ty ) Return "z"
If TWParamType( ty ) Return "w"
If TLParamType( ty ) Return "x"
If TStringType( ty ) Return "t"
End Method
Method TransDebugScopeType$(ty:TType)
Local p:String = TransSPointer(ty)
If TByteType( ty ) Return p + "b"
If TShortType( ty ) Return p + "s"
If TIntType( ty ) Return p + "i"
If TUIntType( ty ) Return p + "u"
If TFloatType( ty ) Return p + "f"
If TDoubleType( ty ) Return p + "d"
If TLongType( ty ) Return p + "l"
If TULongType( ty ) Return p + "y"
If TSizeTType( ty ) Return p + "t"
If TWParamType( ty ) Return p + "W"
If TLParamType( ty ) Return p + "X"
If TInt128Type( ty ) Return p + "j"
If TFloat128Type( ty ) Return p + "k"
If TDouble128Type( ty ) Return p + "m"
If TFloat64Type( ty ) Return p + "h"
If TStringType( ty ) Return "$"
If TArrayType( ty ) Then
Local s:String = "["
For Local i:Int = 0 Until TArrayType( ty ).dims - 1
s:+ ","
Next
s:+ "]"
Return s + TransDebugScopeType(TArrayType( ty ).elemType)
End If
If TObjectType( ty ) Then
If TObjectType( ty ).classdecl.IsStruct() Then
Return p + "@" + TObjectType(ty).classDecl.ident
Else If Not TObjectType( ty ).classdecl.IsExtern()
Return ":" + TObjectType( ty ).classDecl.ident
Else
If TObjectType( ty ).classdecl.IsInterface() Then
Return p + "*#" + TObjectType(ty).classDecl.ident
Else
Return p + "#" + TObjectType(ty).classDecl.ident
End If
End If
End If
If TFunctionPtrType( ty ) Then
Local func:TFuncDecl = TFunctionPtrType( ty ).func
Local s:String = "("
For Local i:Int = 0 Until func.argDecls.length
If i Then
s :+ ","
End If
s :+ TransDebugScopeType(func.argDecls[i].ty)
Next
Return s + ")" + TransDebugScopeType(func.retType)
End If
End Method
Method TransType$( ty:TType, ident:String, fpReturnTypeFunctionArgs:String = Null, fpReturnTypeClassFunc:Int = False)
Local p:String = TransSPointer(ty, True)
If TVoidType( ty ) Or Not ty Then
Return "void"
End If
If TBoolType( ty ) Return "BBINT" + p
If TByteType( ty ) Return "BBBYTE" + p
If TShortType( ty ) Return "BBSHORT" + p
If TIntType( ty ) Return "BBINT" + p
If TUIntType( ty ) Return "BBUINT" + p
If TFloatType( ty ) Return "BBFLOAT" + p
If TDoubleType( ty ) Return "BBDOUBLE" + p
If TLongType( ty ) Return "BBLONG" + p
If TULongType( ty ) Return "BBULONG" + p
If TSizeTType( ty ) Return "BBSIZET" + p
If TWParamType( ty ) Return "WPARAM" + p
If TLParamType( ty ) Return "LPARAM" + p
If TInt128Type( ty ) Return "BBINT128" + p
If TFloat128Type( ty ) Return "BBFLOAT128" + p
If TDouble128Type( ty ) Return "BBDOUBLE128" + p
If TFloat64Type( ty ) Return "BBFLOAT64" + p
If TStringType( ty ) Then
If ty._flags & TType.T_CHAR_PTR Then
Return "BBBYTE *"
Else If ty._flags & TType.T_SHORT_PTR Then
Return "BBSHORT *"
End If
Return "BBSTRING" + p
End If
If TArrayType( ty ) Return "BBARRAY" + p
If TObjectType( ty ) Then
Return TransObject(TObjectType(ty).classdecl) + p
End If
If TFunctionPtrType( ty ) Then
TFunctionPtrType(ty).func.Semant
Local api:String
If TFunctionPtrType(ty).func.attrs & DECL_API_WIN32 Then
api = " __stdcall "
End If
Local args:String
For Local arg:TArgDecl = EachIn TFunctionPtrType(ty).func.argDecls
arg.Semant()
If args Then
args :+ ","
End If
args :+ TransType(arg.ty, "")
Next
Local ret:String = ""
If fpReturnTypeFunctionArgs Then
ret = Bra(fpReturnTypeFunctionArgs)
End If
If fpReturnTypeClassFunc Then
' typedef for function pointer return type
Return ident + "x" + Bra(api + p +"* " + ident) + Bra(args)
Else
' if a function F returns another function (let's call it G),
' then C syntax requires the declaration of F to be nested into that of the type of G
' e.g. "Function F:RetG(ArgG)(ArgF)" in BlitzMax becomes "RetG(* F(ArgF) )(ArgG)" in C
' solution: use "* F(ArgF)" as an ident to generate a declaration for G
' the result will be the declaration for F
Local callable:String = Bra(api + p +"* " + ident + ret)
If TFunctionPtrType(TFunctionPtrType(ty).func.retType) Then
If Not args Then args = " " ' make sure the parentheses aren't ommited even if the parameter list is empty
Return TransType(TFunctionPtrType(ty).func.retType, callable, args)
Else
Local retTypeStr:String = TransType(TFunctionPtrType(ty).func.retType, "")
Return retTypeStr + callable + Bra(args)
End If
End If
End If
If TExternObjectType( ty ) Return "struct " + TExternObjectType( ty ).classDecl.munged + p
InternalErr
End Method
Method TransIfcType$( ty:TType, isSuperStrict:Int = False )
Local p:String = TransSPointer(ty)
If ty And (ty._flags & TType.T_VAR) Then
p :+ " Var"
End If
If Not ty Then
If opt_issuperstrict Or isSuperStrict Then
Return p
Else
Return "%" + p
End If
End If
If TVoidType( ty ) Then
Return p
End If
If TByteType( ty ) Return "@" + p
If TShortType( ty ) Return "@@" + p
If TIntType( ty ) Return "%" + p
If TUIntType( ty ) Return "|" + p
If TFloatType( ty ) Return "#" + p
If TDoubleType( ty ) Return "!" + p
If TLongType( ty ) Return "%%" + p
If TULongType( ty ) Return "||" + p
If TSizeTType( ty ) Return "%z" + p
If TWParamType( ty ) Return "%w" + p
If TLParamType( ty ) Return "%x" + p
If TInt128Type( ty ) Return "%j" + p
If TFloat128Type( ty ) Return "!k" + p
If TDouble128Type( ty ) Return "!m" + p
If TFloat64Type( ty ) Return "!h" + p
If TStringType( ty ) Then
If ty._flags & TType.T_CHAR_PTR Then
Return "$z"
Else If ty._flags & TType.T_SHORT_PTR Then
Return "$w"
End If
Return "$" + p
End If
If TArrayType( ty ) Then
Local s:String = TransIfcType(TArrayType( ty ).elemType) + "&["
For Local i:Int = 0 Until TArrayType( ty ).dims - 1
s:+ ","
Next
Return s + "]" + p
End If
If TObjectType( ty ) Then
Local t:String = ":"
If TObjectType(ty).classDecl.IsExtern() Then
If TObjectType(ty).classDecl.IsInterface() Then
t = "??"
ElseIf TObjectType(ty).classDecl.IsStruct() Then
t = "~~"
Else
t = "?"
End If
End If
Local cdecl:TClassDecl = TObjectType(ty).classDecl
' find first type in hierarchy that isn't private
While cdecl.IsPrivate() And cdecl.superClass <> Null
cdecl = cdecl.superClass
Wend
Return t + cdecl.ident + p
End If
If TFunctionPtrType( ty ) Return TransIfcType(TFunctionPtrType(ty).func.retType, TFunctionPtrType(ty).func.ModuleScope().IsSuperStrict()) + TransIfcArgs(TFunctionPtrType(ty).func)
If TExternObjectType( ty ) Return ":" + TExternObjectType(ty).classDecl.ident + p
InternalErr
End Method
Method TransRefType$( ty:TType, ident:String )
Return TransType( ty, ident )
End Method
Method TransValue$( ty:TType,value$ )
If value
If IsPointerType(ty, 0, TType.T_POINTER) Return value
If TBoolType( ty ) Return "1"
If TShortType( ty ) Return value
If TIntType( ty ) Return value
If TUIntType( ty ) Return value+"U"
If TLongType( ty ) Return value+"LL"
If TULongType( ty ) Return value+"ULL"
If TSizeTType( ty ) Return value
If TWParamType( ty ) Return value
If TLParamType( ty ) Return value
If TInt128Type( ty ) Return value
If TFloatType( ty ) Then
If value = "nan" Or value = "1.#IND0000" Then
Return "bbPOSNANf"
Else If value="-nan" Or value = "-1.#IND0000" Then
Return "bbNEGNANf"
Else If value = "inf" Or value = "1.#INF0000" Then
Return "bbPOSINFf"
Else If value = "-inf" Or value = "-1.#INF0000" Then
Return "bbNEGINFf"
Else
If value.ToLower().Find("e")>=0 Then
Return value
End If
If value.Find(".") < 0 Then
value :+ ".0"
End If
Return value+"f"
End If
End If
If TDoubleType( ty ) Or TFloat128Type(ty) Or TDouble128Type(ty) Or TFloat64Type(ty) Then
If value = "nan" Or value = "1.#IND0000" Then
Return "bbPOSNANd"
Else If value="-nan" Or value = "-1.#IND0000" Then
Return "bbNEGNANd"
Else If value = "inf" Or value = "1.#INF0000" Then
Return "bbPOSINFd"
Else If value = "-inf" Or value = "-1.#INF0000" Then
Return "bbNEGINFd"
Else
If value.ToLower().Find("e") >=0 Then
Return value
End If
If value.Find(".") < 0 Then
value :+ ".0"
End If
Return value
End If
End If
If TStringType( ty ) Return TransStringConst(value )
If TByteType( ty ) Return value
Else
If TBoolType( ty ) Return "0"
If TIntrinsicType( ty) Then
If IsPointerType(ty, 0, TType.T_POINTER) Then
Return "0"
Else
Return "{}"
End If
End If
If TNumericType( ty ) Return "0" ' numeric and pointers
If TStringType( ty ) Return "&bbEmptyString"
If TArrayType( ty ) Return "&bbEmptyArray"
If TObjectType( ty ) Then
If TObjectType( ty ).classDecl.IsExtern() Or TObjectType( ty ).classDecl.IsStruct() Then
If TObjectType( ty ).classDecl.IsInterface() Or IsPointerType(ty) Or (Not TObjectType( ty ).classDecl.IsStruct()) Then
Return "0"
Else
Return "{}"
End If
Else
Return "&bbNullObject"
End If
End If
If TFunctionPtrType( ty) Return "&brl_blitz_NullFunctionError" ' todo ??
EndIf
InternalErr
End Method
Method TransArgs$( args:TExpr[],decl:TFuncDecl, objParam:String = Null )
'If decl.ident="AddS" DebugStop
Local t$
If objParam And (decl.IsMethod() Or decl.isCtor()) And ((Not decl.IsExtern()) Or (decl.IsExtern() And TClassDecl(decl.scope) And Not TClassDecl(decl.scope).IsStruct())) Then
t:+ objParam
End If
For Local i:Int=0 Until decl.argDecls.Length
Local ty:TType = TArgDecl(decl.argDecls[i].actual).ty
If t t:+","
If i < args.length
Local arg:TExpr = args[i]
If TNullExpr(arg) Then
t :+ TransValue(ty, Null)
Continue
Else If TIndexExpr(arg) And (ty._flags & TType.T_VAR) Then
t:+ "&"
Else If TStringType(ty) And (ty._flags & TType.T_VAR) Then
If TCastExpr(arg) And TStringType(TCastExpr(arg).expr.exprType) Then
t:+ "&"
End If
Else If TArrayType(ty) And (ty._flags & TType.T_VAR) Then
If (TVarExpr(arg) And TArrayType(TVarExpr(arg).exprType) Or (TMemberVarExpr(arg) And TArrayType(TMemberVarExpr(arg).exprType))) And Not (arg.exprType._flags & TType.T_VAR) Then
t:+ "&"
End If
Else If TObjectType(ty) And (ty._flags & TType.T_VAR) Then
If (TVarExpr(arg) Or TMemberVarExpr(arg)) And TObjectType(arg.exprType) And Not (arg.exprType._flags & TType.T_VAR) Then
t:+ "&"
End If
Else If TFunctionPtrType(ty) Or IsPointerType(ty, TType.T_BYTE) Then
If TFunctionPtrType(ty) And (ty._flags & TType.T_VAR) Then
t:+ "&"
End If
If TInvokeExpr(arg) And Not TInvokeExpr(arg).decl.IsMethod() Then
If IsPointerType(ty, TType.T_BYTE) Then
t:+ TInvokeExpr(arg).Trans()
Else
' need to test scopes to see if we need to use the current instance's function or not
' use the "actual", not the copy we made for the function pointer.
Local fdecl:TFuncDecl = TFuncDecl(TInvokeExpr(arg).decl.actual)
If Not fdecl.munged Then
MungDecl fdecl
TInvokeExpr(arg).decl.munged = fdecl.munged
End If
If TClassDecl(fdecl.scope) Then
' current scope is related to function scope?
If _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() Then
If _env.ClassScope().ExtendsClass(TClassDecl(fdecl.scope)) Then
Local scope:TScopeDecl = _env.scope
Local obj:String = Bra("struct " + scope.munged + "_obj*")
Local class:String = "o->clas"
t:+ class + "->f_" + fdecl.ident + MangleMethod(fdecl)
Else
t:+ fdecl.munged
End If
Else
t:+ fdecl.munged
End If
Else
t:+ fdecl.munged
End If
End If
Continue
End If
' some cases where we are passing a function pointer via a void* parameter.
If TCastExpr(arg) And TInvokeExpr(TCastExpr(arg).expr) And Not TInvokeExpr(TCastExpr(arg).expr).invokedWithBraces Then
If Not TInvokeExpr(TCastExpr(arg).expr).decl.munged Then
t:+ TInvokeExpr(TCastExpr(arg).expr).decl.actual.munged
Else
t:+ TInvokeExpr(TCastExpr(arg).expr).decl.munged
End If
Continue
End If
' Object -> Byte Ptr
If IsPointerType(ty, TType.T_BYTE) And TObjectType(arg.exprType) Then
t:+ Bra(Bra("(BBBYTE*)" + Bra(arg.Trans())) + "+" + Bra("sizeof(void*)"))
Continue
End If
Else If IsNumericType(ty) Then
If TObjectType(arg.exprType) 'And TObjectType(args[i].exprType).classDecl = TClassDecl.nullObjectClass Then
err "NULL"
t:+ "0"
Continue
End If
End If
If decl.argDecls[i].castTo Then
t:+ Bra(decl.argDecls[i].castTo) + arg.Trans()
Else
Local tc:String = TransTemplateCast( ty,arg.exprType,arg.Trans() )
' *sigh*
' if var is going to var, remove any leading dereference character.
' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
If arg.exprType.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And (arg.exprType._flags & TType.T_VAR) Then
If tc.startswith("*") Then
tc = tc[1..]
End If
End If
t:+ tc
't:+TransTemplateCast( ty,args[i].exprType,args[i].Trans() )
End If
Else
decl.argDecls[i].Semant()
' default values
Local init:TExpr = decl.argDecls[i].init
If init Then
If TConstExpr(init) Then
If TObjectType(TConstExpr(init).exprType) Then
t:+"NULLNULLNULL"
' And TNullDecl(TObjectType(TConstExpr(init).exprType).classDecl)) Or (TConstExpr(init).value = "bbNullObject") Then
If TStringType(decl.argDecls[i].ty) Then
t :+ "&bbEmptyString"
Else If TArrayType(decl.argDecls[i].ty) Then
t :+ "&bbEmptyArray"
Else
t :+ "&bbNullObject"
End If
Else
t:+ decl.argDecls[i].init.Trans()
End If
Else If TFunctionPtrType(ty) Then
If TInvokeExpr(init) Then
t:+ TInvokeExpr(init).decl.munged
End If
Else
t:+ decl.argDecls[i].init.Trans()
End If
End If
End If
Next
Return Bra(t)
End Method
Method TransArgsTypes$( args:TExpr[],declArgTypes:TType[])
Local t$
For Local i:Int=0 Until args.Length
If t t:+","
t:+TransTemplateCast( declArgTypes[i],args[i].exprType,args[i].Trans() )
Next
Return Bra(t)
End Method
Method TransPtrCast$( ty:TType,src:TType,expr$,cast$ )
If IsPointerType(ty, 0, TType.T_POINTER | TType.T_VARPTR | TType.T_VAR) Or TFunctionPtrType(ty) Then
' TODO : pointer stuff
If TNullType(src) Return TransValue(ty, Null)
Return expr
End If
'If expr = "NULL" DebugStop
' If TIntType(ty) And TStringType(src) Then
'DebugStop
' Return "bbObjectDowncast" + Bra(expr + ",&" + TStringType(src).cDecl.munged)
' End If
If TNullType(src)
Return TransValue(ty, Null)
End If
If TStringType(ty) And TObjectType(src) Then
If Not TStringType(ty).cDecl Then
ty.Semant()
End If
'If TNullDecl(TObjectType(src).classDecl) Then
' Return "&bbEmptyString"
'End If
Return Bra("(BBString *)bbObjectDowncast" + Bra(expr + ",&" + TStringType(ty).cDecl.munged))
End If
'If TArrayType(ty) And TObjectType(src) Then
' If TNullDecl(TObjectType(src).classDecl) Then
' Return "&bbEmptyArray"
' End If
'End If
If TVarPtrType(src) And TNumericType(ty) Then
Return "*" + expr
End If
If TIntType(ty) And TStringType(src) Then
Return Bra(expr + " != &bbEmptyString")
End If
' If TIntType(ty) And TObjectType(src) Then
' Return Bra(expr + " != &bbNullObject")
' End If
If TObjectType(ty) And TStringType(src) Then
Return expr
End If
If Not TObjectType(ty) Or Not TObjectType(src) Then
DebugStop
InternalErr
End If
Local t$=TransType(ty, "TODO: TransPtrCast")
If src.GetClass().IsInterface() Or ty.GetClass().IsInterface() cast="dynamic"
If src.GetClass().IsInterface() And Not ty.GetClass().IsInterface() Then
Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
End If
'upcast?
If src.GetClass().ExtendsClass( ty.GetClass() ) Return expr
If TObjectType(ty) Then
Return Bra(Bra(TransObject(TObjectType(ty).classDecl)) + "bbObjectDowncast" + Bra(expr + ",&" + TObjectType(ty).classDecl.munged))
End If
Return cast+"_cast<"+TransType(ty, "TODO: TransPtrCast")+">"+Bra( expr )
End Method
'***** Utility *****
Method TransLocalDecl$( decl:TLocalDecl,init:TExpr, declare:Int = False, outputInit:Int = True )
Local initTrans:String
If outputInit Then
If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
initTrans = "=" + TInvokeExpr(init).decl.munged
Else
initTrans = "=" + init.Trans()
End If
End If
If Not declare And opt_debug Then
Local ty:TType = decl.ty
If Not TObjectType( ty ) Or (TObjectType( ty ) And Not TObjectType( ty ).classDecl.IsStruct()) Then
If TIntrinsicType(ty) Then
If Not TConstExpr(init) Then
Return decl.munged + initTrans
End If
Else
Return decl.munged + initTrans
End If
Else If TObjectType( ty ) And TObjectType( ty ).classDecl.IsStruct() Then
If Not TConstExpr(init) Then
Return decl.munged + initTrans
End If
End If
Else
If TFunctionPtrType(decl.ty) Then
If TInvokeExpr(init) And Not TInvokeExpr(init).invokedWithBraces Then
Return TransType( decl.ty, decl.munged ) + " = " + TInvokeExpr(init).decl.munged
Else
Return TransType( decl.ty, decl.munged ) + initTrans
End If
Else
Local ty:TType = decl.ty
If TVoidType( ty ) Or Not ty Then
ty = init.exprType
End If
If TObjectType(ty) Then
If TObjectType(ty).classdecl.IsExtern() Then
If TObjectType(ty).classdecl.IsInterface() Then
Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
Else
Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
End If
Else
If TObjectType(ty).classdecl.IsStruct() Then
Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
Else
If decl.volatile Then
Return TransType( ty, decl.munged )+" volatile "+decl.munged + initTrans
Else
Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
End If
End If
End If
Else
Return TransType( ty, decl.munged )+" "+decl.munged + initTrans
End If
End If
End If
End Method
Method TransLocalDeclNoInit$( decl:TVarDecl )
If TFunctionPtrType(decl.ty) Then
Return TransType( decl.ty, decl.munged ) + "=" + TransValue(decl.ty, "")
Else
If TObjectType(decl.ty) Then
If TObjectType(decl.ty).classdecl.IsExtern() Then
If Not TObjectType(decl.ty).classdecl.IsStruct() Then
Return TransType( decl.ty, decl.munged )+" "+decl.munged+"=" + TransValue(decl.ty, "")
Else
Return TransType( decl.ty, decl.munged )+" "+decl.munged
End If
Else
If Not TObjectType(decl.ty).classdecl.IsStruct() Then
If TLocalDecl(decl) And TLocalDecl(decl).volatile Then
Return TransType( decl.ty, decl.munged )+" volatile "+decl.munged + "=" + TransValue(decl.ty, "")
Else
Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
End If
Else
Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
End If
End If
Else
Return TransType( decl.ty, decl.munged )+" "+decl.munged + "=" + TransValue(decl.ty, "")
End If
End If
End Method
Method TransGlobalDecl$( gdecl:TGlobalDecl )
Local glob:String
If Not gdecl.funcGlobal Then
If Not (gdecl.attrs & DECL_INITONLY) Then
glob :+"static " + TransType( gdecl.init.exprType, gdecl.munged )+" "
End If
glob :+ gdecl.munged+"="
If (TNewObjectExpr(gdecl.init) Or TNewArrayExpr(gdecl.init)) And Not (gdecl.attrs & DECL_INITONLY) Then
glob :+ "0;~n"
glob :+ indent + "if (" + gdecl.munged + "==0) {~n"
glob :+ indent + "~t" + gdecl.munged + "=" + gdecl.init.Trans() + ";~n"
glob :+ indent + "}"
Else If TArrayExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
glob :+ "0;~n"
Emit glob
Emit "if (" + gdecl.munged + "==0) {"
glob = gdecl.munged + "=" + gdecl.init.Trans() + ";"
Emit glob
Emit "}"
glob = ""
Else
If gdecl.init Then
If TFunctionPtrType(gdecl.ty) Then
If TInvokeExpr(gdecl.init) And Not TInvokeExpr(gdecl.init).invokedWithBraces Then
glob :+ TInvokeExpr(gdecl.init).decl.munged
Else
glob :+ gdecl.init.Trans()
End If
Else If Not TConstExpr(gdecl.init) And Not (gdecl.attrs & DECL_INITONLY) Then
' for non const, we need to add an initialiser
glob :+ TransValue(gdecl.ty, "") + ";~n"
glob :+ indent +"static int _" + gdecl.munged + "_inited = 0;~n"
glob :+ indent + "if (!_" + gdecl.munged + "_inited) {~n"
glob :+ indent + "~t_" + gdecl.munged + "_inited = 1;~n"
glob :+ indent + "~t" + gdecl.munged + " = " + gdecl.init.Trans() + ";~n"
glob :+ indent + "}"
Else
glob :+ gdecl.init.Trans()
End If
Else
If TFunctionPtrType(gdecl.ty) Then
glob :+ "&brl_blitz_NullFunctionError"
Else
glob :+ "0"
End If
End If
End If
Else
glob :+ "static int _" + gdecl.munged + "_inited = 0;~n"
glob :+ indent + "if (!_" + gdecl.munged + "_inited) {~n"
glob :+ indent + "~t_" + gdecl.munged + "_inited = 1;~n"
glob :+ indent + "~t" + gdecl.munged + " = "
If gdecl.init Then
glob :+ gdecl.init.Trans()
Else
glob :+ TransValue(gdecl.ty, "")
End If
glob :+ ";~n"
glob :+ indent + "}"
End If
Return glob
End Method
Method CreateLocal2$( ty:TType, t$ )
Local tmp:TLocalDecl=New TLocalDecl.Create( "", ty,Null, True )
MungDecl tmp
If TShortType(ty) Then
Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToWString" + Bra(t)+ ";"
Else
Emit TransType(ty, "") + " " + tmp.munged + " = bbStringToCString" + Bra(t)+ ";"
End If
customVarStack.Push(tmp.munged)
Return tmp.munged
End Method
Method EmitPushErr()
Emit "pushErr();"
End Method
Method EmitSetErr( info$ )
Emit "errInfo=~q"+info.Replace( "\","/" )+"~q;"
End Method
Method EmitPopErr()
Emit "popErr();"
End Method
'***** Declarations *****
Method TransStatic$( decl:TDecl )
If decl.IsExtern() Then
If Not decl.munged
Return decl.ident
End If
Return decl.munged
Else If _env And decl.scope And decl.scope=_env.ClassScope()
' calling a class function from a method?
If TFuncDecl(decl) And _env.ClassScope() And _env.FuncScope() And _env.FuncScope().IsMethod() And Not (decl.attrs & FUNC_PTR) Then
Local scope:TScopeDecl = _env.ClassScope()
Local obj:String = Bra("struct " + scope.munged + "_obj*")
Local class:String = "o->clas"
Return class + "->f_" + decl.ident + MangleMethod(TFuncDecl(decl))
Else
Return decl.munged
End If
Else If TClassDecl( decl.scope )
'Return decl.scope.munged+"::"+decl.munged
Return decl.munged
Else If TModuleDecl( decl.scope )
Return decl.munged
Else If TFuncDecl(decl.scope)
Return decl.munged
Else If TGlobalDecl(decl)
Return decl.munged
Else If TBlockDecl(decl.scope)
Return decl.munged
EndIf
InternalErr
End Method
Method TransTemplateCast$( ty:TType,src:TType,expr$ )
' *sigh*
' if var is going to var, remove any leading dereference character.
' rather hacky. Would be better to cast variable to varptr during semanting (well done if you can work out where!)
'If src.EqualsType( ty.ActualType() ) And (ty._flags & TType.T_VAR) And (src._flags & TType.T_VAR) Then
' If expr.startswith("*") Then
' expr = expr[1..]
' End If
'End If
If ty=src Return expr
ty=ty.ActualType()
'src=src.ActualType()
If src.EqualsType( ty ) Return expr
Return TransPtrCast( ty,src,expr,"static" )
End Method
Method TransGlobal$( decl:TGlobalDecl )
Return TransStatic( decl )
End Method
Method TransField$( decl:TFieldDecl,lhs:TExpr )
If lhs Then
Return TransFieldRef(decl, TransSubExpr( lhs ), lhs.exprType)
Else
Return TransFieldRef(decl, "o", Null)
End If
' Local swiz$
' If TObjectType( decl.ty )
' If TObjectType( decl.ty ).classDecl.IsInterface() swiz=".p"
' EndIf
' If lhs Return TransSubExpr( lhs )+"->"+decl.munged+swiz
' Return decl.munged+swiz
End Method
Method TransFunc$( decl:TFuncDecl,args:TExpr[],lhs:TExpr, sup:Int = False, scope:TScopeDecl = Null )
'If decl.ident = "eventfilter" DebugStop
' for calling the super class method instead
Local tSuper:String
If sup Then
tSuper = "->super"
End If
If Not decl.munged
MungDecl decl
End If
'If decl.IsMethod()
If lhs And Not TSelfExpr(lhs) Then
If TStringType(lhs.exprType) Then
Return decl.munged + TransArgs(args, decl, TransSubExpr( lhs ))
End If
If TStmtExpr(lhs) Then
lhs.Trans()
lhs = TStmtExpr(lhs).expr
End If
If TVarExpr(lhs) Then
Local cdecl:TClassDecl
If TObjectType(TVarExpr(lhs).decl.ty) Then
cdecl = TObjectType(TVarExpr(lhs).decl.ty).classDecl
Else If TArrayType(TVarExpr(lhs).decl.ty) Then
Return decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
If decl.attrs & FUNC_PTR Then
'Return "(" + obj + TransSubExpr( lhs ) + ")->" + decl.munged+TransArgs( args,decl, Null)
Local op:String
If cdecl.IsStruct() Then op = "." Else op = "->"
Return TransSubExpr( lhs ) + op + decl.munged+TransArgs( args,decl, Null)
Else
'Local lvar:String = CreateLocal(lhs, False)
'Local lvarInit:String = Bra(lvar + " = " + lhs.Trans())
If decl.scope.IsExtern()
If Not cdecl.IsStruct() Then
'Return decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
Return Bra(TransSubExpr( lhs )) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, TransSubExpr( lhs ) ))
'Return Bra(lvarInit) + "->vtbl->" + decl.munged + Bra(TransArgs( args,decl, lvar ))
End If
Err "TODO extern types not allowed methods"
Else
If cdecl.IsInterface() And Not equalsBuiltInFunc(cdecl, decl) Then
Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + TransSubExpr( lhs ) + ", " + "&" + cdecl.munged + "_ifc)"))
Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
' Local ifc:String = Bra("(struct " + cdecl.munged + "_methods*)" + Bra("bbObjectInterface(" + lvarInit + ", " + "&" + cdecl.munged + "_ifc)"))
' Return ifc + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
Else
If cdecl.IsStruct() Then
If Not isPointerType(lhs.exprType) Then
Return "_" + decl.munged+TransArgs( args,decl, "&" + TransSubExpr( lhs ) )
Else
Return "_" + decl.munged+TransArgs( args,decl, TransSubExpr( lhs ) )
End If
Else
Local class:String = Bra(TransSubExpr( lhs )) + "->clas" + tSuper
Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, TransSubExpr( lhs ) )
' Local class:String = Bra(lvarInit) + "->clas" + tSuper
' Return class + "->" + TransFuncPrefix(cdecl, decl) + FuncDeclMangleIdent(decl)+TransArgs( args,decl, lvar )
End If
End If
End If
End If
Else If TNewObjectExpr(lhs) Then
Local cdecl:TClassDecl = TNewObjectExpr(lhs).classDecl
If cdecl.IsStruct() Then
' create a local variable of the inner invocation