FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/ast/ast.cpp at master · Xsankor/daScript · GitHub
Xsankor
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
src
/
ast
/
ast.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
2999 lines (2646 loc) · 98.1 KB
Breadcrumbs
daScript
/
src
/
ast
/
ast.cpp
Copy path
File metadata and controls
2999 lines (2646 loc) · 98.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/ast/ast_visitor.h
"
namespace
das
{
//
local or global
bool
isLocalOrGlobal
(
const
ExpressionPtr & expr ) {
if
( expr->
rtti_isVar
() ) {
auto
ev = static_pointer_cast<ExprVar>(expr);
return
ev->
local
|| !(ev->
argument
|| ev->
block
);
}
else
if
( expr->
rtti_isAt
() ) {
auto
ea = static_pointer_cast<ExprAt>(expr);
if
( ea->
subexpr
&& ea->
subexpr
->
type
&& ea->
subexpr
->
type
->
dim
.
size
() ) {
return
isLocalOrGlobal
(ea->
subexpr
);
}
}
else
if
( expr->
rtti_isField
() ) {
auto
ef = static_pointer_cast<ExprField>(expr);
if
( ef->
value
&& ef->
value
->
type
&& (ef->
value
->
type
->
baseType
!=Type::tHandle || ef->
value
->
type
->
isLocal
()) ) {
return
isLocalOrGlobal
(ef->
value
);
}
}
else
if
( expr->
rtti_isSwizzle
() ) {
auto
sw = static_pointer_cast<ExprSwizzle>(expr);
if
( sw->
value
) {
return
isLocalOrGlobal
(sw->
value
);
}
}
return
false
;
}
//
AOT
AotListBase * AotListBase::head =
nullptr
;
AotListBase::AotListBase
( RegisterAotFunctions prfn ) {
tail = head;
head =
this
;
regFn = prfn;
}
void
AotListBase::registerAot
( AotLibrary & lib ) {
auto
it = head;
while
( it ) {
(*it->
regFn
)(lib);
it = it->
tail
;
}
}
//
aot library
DAS_THREAD_LOCAL
unique_ptr<AotLibrary> g_AOT_lib;
AotLibrary &
getGlobalAotLibrary
() {
if
( !g_AOT_lib ) {
g_AOT_lib = make_unique<AotLibrary>();
AotListBase::registerAot
(*g_AOT_lib);
}
return
*g_AOT_lib;
}
void
clearGlobalAotLibrary
() {
g_AOT_lib.
reset
();
}
//
annotations
string
Annotation::getMangledName
()
const
{
return
module
?
module
->
name
+
"
::
"
+ name : name;
}
string
FunctionAnnotation::aotName
( ExprCallFunc * call ) {
return
call->
func
->
getAotBasicName
();
}
string
AnnotationDeclaration::getMangledName
()
const
{
TextWriter tw;
tw <<
"
[
"
<< annotation->
getMangledName
();
if
(arguments.
size
()) {
tw <<
"
(
"
;
for
(
auto
& arg : arguments) {
if
(&arg != &arguments.
front
()) {
tw <<
"
,
"
;
}
tw << arg.
name
<<
"
:
"
<<
das_to_string
(arg.
type
) <<
"
=
"
;
switch
(arg.
type
) {
case
Type::tBool: tw << (arg.
bValue
?
"
true
"
:
"
false
"
);
break
;
case
Type::tInt: tw << arg.
iValue
;
break
;
case
Type::tFloat: tw << arg.
fValue
;
break
;
case
Type::tString: tw <<
"
\"
"
<< arg.
sValue
<<
"
\"
"
;
break
;
default
: tw <<
"
error
"
;
break
;
}
}
tw <<
"
)
"
;
}
tw <<
"
]
"
;
return
tw.
str
();
}
//
enumeration
string
Enumeration::getMangledName
()
const
{
return
(
module
?
module
->
name
+
"
::
"
+name : name);
//
+ "#" + das_to_string(baseType);
}
TypeDeclPtr
Enumeration::makeBaseType
()
const
{
return
make_smart<TypeDecl>(baseType);
}
Type
Enumeration::getEnumType
()
const
{
switch
(baseType) {
case
Type::tInt8:
case
Type::tUInt8:
return
Type::tEnumeration8;
case
Type::tInt16:
case
Type::tUInt16:
return
Type::tEnumeration16;
case
Type::tInt:
case
Type::tUInt:
return
Type::tEnumeration;
default
:
DAS_ASSERTF
(
0
,
"
we should not be here. unsupported enumeration base type.
"
);
return
Type::none;
}
}
TypeDeclPtr
Enumeration::makeEnumType
()
const
{
TypeDeclPtr res;
switch
(baseType) {
case
Type::tInt8:
case
Type::tUInt8:
res = make_smart<TypeDecl>(Type::tEnumeration8);
break
;
case
Type::tInt16:
case
Type::tUInt16:
res = make_smart<TypeDecl>(Type::tEnumeration16);
break
;
case
Type::tInt:
case
Type::tUInt:
res = make_smart<TypeDecl>(Type::tEnumeration);
break
;
default
:
DAS_ASSERTF
(
0
,
"
we should not be here. unsupported enumeration base type.
"
);
return
nullptr
;
}
res->
enumType
=
const_cast
<Enumeration*>(
this
);
return
res;
}
pair<ExpressionPtr,
bool
>
Enumeration::find
(
const
string & na )
const
{
auto
it =
find_if
(list.
begin
(), list.
end
(), [&](
const
EnumEntry & arg){
return
arg.
name
== na;
});
return
it!=list.
end
() ?
pair<ExpressionPtr,
bool
>(it->
value
,
true
) :
pair<ExpressionPtr,
bool
>(
nullptr
,
false
);
}
int64_t
Enumeration::find
(
const
string & na,
int64_t
def )
const
{
auto
it =
find_if
(list.
begin
(), list.
end
(), [&](
const
EnumEntry & arg){
return
arg.
name
== na;
});
return
it!=list.
end
() ?
getConstExprIntOrUInt
(it->
value
) : def;
}
string
Enumeration::find
(
int64_t
va,
const
string & def )
const
{
for
(
const
auto
& it : list ) {
if
(
getConstExprIntOrUInt
(it.
value
)==va ) {
return
it.
name
;
}
}
return
def;
}
bool
Enumeration::add
(
const
string & f,
const
LineInfo & att ) {
return
add
(f,
nullptr
, att);
}
bool
Enumeration::addI
(
const
string & f,
int64_t
value,
const
LineInfo & att ) {
return
add
(f, make_smart<ExprConstInt64>(value),att);
}
bool
Enumeration::addIEx
(
const
string & f,
const
string & fcpp,
int64_t
value,
const
LineInfo & att ) {
return
addEx
(f, fcpp, make_smart<ExprConstInt64>(value),att);
}
bool
Enumeration::addEx
(
const
string & na,
const
string & naCpp,
const
ExpressionPtr & expr,
const
LineInfo & att ) {
auto
it =
find_if
(list.
begin
(), list.
end
(), [&](
const
EnumEntry & arg){
return
arg.
name
== na;
});
if
( it == list.
end
() ) {
EnumEntry ena;
ena.
name
= na;
ena.
cppName
= naCpp.
empty
() ? na : naCpp;
ena.
value
= expr;
ena.
at
= att;
list.
push_back
(ena);
return
true
;
}
else
{
return
false
;
}
}
bool
Enumeration::add
(
const
string & na,
const
ExpressionPtr & expr,
const
LineInfo & att ) {
return
addEx
(na,
"
"
,expr,att);
}
//
structure
StructurePtr
Structure::clone
()
const
{
auto
cs = make_smart<Structure>(name);
cs->
fields
.
reserve
(fields.
size
());
for
(
auto
& fd : fields ) {
cs->
fields
.
emplace_back
(fd.
name
, fd.
type
, fd.
init
, fd.
annotation
, fd.
moveSemantics
, fd.
at
);
cs->
fields
.
back
().
flags
= fd.
flags
;
}
cs->
at
= at;
cs->
module
=
module
;
cs->
flags
= flags;
cs->
annotations
=
cloneAnnotationList
(annotations);
return
cs;
}
bool
Structure::isCompatibleCast
(
const
Structure & castS )
const
{
if
( castS.
fields
.
size
() < fields.
size
() ) {
return
false
;
}
for
(
size_t
i=
0
; i!=fields.
size
(); ++i ) {
auto
& fd = fields[i];
auto
& cfd = castS.
fields
[i];
if
( fd.
name
!= cfd.
name
) {
return
false
;
}
if
( !fd.
type
->
isSameType
(*cfd.
type
, RefMatters::yes, ConstMatters::yes, TemporaryMatters::yes) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::hasAnyInitializers
()
const
{
for
(
const
auto
& fd : fields ) {
if
( fd.
init
)
return
true
;
}
return
false
;
}
bool
Structure::canCopy
(
bool
tempMatters)
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
canCopy
(tempMatters) )
return
false
;
}
return
true
;
}
bool
Structure::canMove
()
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
canMove
() )
return
false
;
}
return
true
;
}
bool
Structure::canClone
()
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
canClone
() )
return
false
;
}
return
true
;
}
bool
Structure::canAot
( das_set<Structure *> & recAot )
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
canAot
(recAot) )
return
false
;
}
return
true
;
}
bool
Structure::canAot
()
const
{
das_set<Structure *> recAot;
return
canAot
(recAot);
}
bool
Structure::isNoHeapType
()
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
isNoHeapType
() )
return
false
;
}
return
true
;
}
bool
Structure::isPod
()
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
isPod
() )
return
false
;
}
return
true
;
}
bool
Structure::isRawPod
()
const
{
for
(
const
auto
& fd : fields ) {
if
( !fd.
type
->
isRawPod
() )
return
false
;
}
return
true
;
}
int
Structure::getSizeOf
()
const
{
int
size =
0
;
const
Structure * cppLayoutParent =
nullptr
;
for
(
const
auto
& fd : fields ) {
int
fieldAlignemnt = fd.
type
->
getAlignOf
();
int
al = fieldAlignemnt -
1
;
if
( cppLayout ) {
auto
fp =
findFieldParent
(fd.
name
);
if
( fp!=cppLayoutParent ) {
if
(
DAS_NON_POD_PADDING
|| !cppLayoutNotPod) {
size = cppLayoutParent ? cppLayoutParent->
getSizeOf
() :
0
;
}
cppLayoutParent = fp;
}
}
size = (size + al) & ~al;
size += fd.
type
->
getSizeOf
();
}
int
al =
getAlignOf
() -
1
;
size = (size + al) & ~al;
return
size;
}
int
Structure::getAlignOf
()
const
{
int
align =
1
;
for
(
const
auto
& fd : fields ) {
align =
das::max
( fd.
type
->
getAlignOf
(), align );
}
return
align;
}
const
Structure::FieldDeclaration *
Structure::findField
(
const
string & na )
const
{
if
( fieldLookup.
size
()==fields.
size
() ) {
auto
it = fieldLookup.
find
(na);
if
( it == fieldLookup.
end
() )
return
nullptr
;
return
&fields[it->
second
];
}
else
{
for
(
const
auto
& fd : fields ) {
if
( fd.
name
==na ) {
return
&fd;
}
}
}
return
nullptr
;
}
const
Structure *
Structure::findFieldParent
(
const
string & na )
const
{
if
( parent ) {
if
( parent->
findField
(na) ) {
return
parent;
}
}
if
(
findField
(na) ) {
return
this
;
}
return
nullptr
;
}
string
Structure::getMangledName
()
const
{
return
module
?
module
->
name
+
"
::
"
+name : name;
}
bool
Structure::canBePlacedInContainer
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
canBePlacedInContainer
(dep) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::hasNonTrivialCtor
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
hasNonTrivialCtor
(dep) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::hasNonTrivialDtor
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
hasNonTrivialDtor
(dep) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::hasNonTrivialCopy
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
hasNonTrivialCopy
(dep) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::isExprTypeAnywhere
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& fd.
type
->
isExprTypeAnywhere
(dep) ) {
return
true
;
}
}
return
false
;
}
bool
Structure::isLocal
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
isLocal
(dep) ) {
return
false
;
}
}
return
true
;
}
bool
Structure::hasClasses
(das_set<Structure *> & dep)
const
{
//
||
if
( isClass )
return
true
;
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& fd.
type
->
hasClasses
(dep) ) {
return
true
;
}
}
return
false
;
}
bool
Structure::isTemp
(das_set<Structure *> & dep)
const
{
//
||
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& fd.
type
->
isTemp
(
true
,
true
, dep) ) {
return
true
;
}
}
return
false
;
}
bool
Structure::isShareable
(das_set<Structure *> & dep)
const
{
//
&&
for
(
const
auto
& fd : fields ) {
if
( fd.
type
&& !fd.
type
->
isShareable
(dep) ) {
return
false
;
}
}
return
true
;
}
//
variable
VariablePtr
Variable::clone
()
const
{
auto
pVar = make_smart<Variable>();
pVar->
name
= name;
pVar->
aka
= aka;
pVar->
type
= make_smart<TypeDecl>(*type);
if
( init )
pVar->
init
= init->
clone
();
if
( source )
pVar->
source
= source->
clone
();
pVar->
at
= at;
pVar->
flags
= flags;
pVar->
initStackSize
= initStackSize;
pVar->
annotation
= annotation;
return
pVar;
}
string
Variable::getMangledName
()
const
{
string mn =
module
?
module
->
name
+
"
::
"
+name : name;
return
mn +
"
"
+ type->
getMangledName
();
}
uint64_t
Variable::getMangledNameHash
()
const
{
auto
mangledName =
getMangledName
();
return
hash_blockz64
((
uint8_t
*)mangledName.
c_str
());
}
bool
Variable::isAccessUnused
()
const
{
return
!(access_get || access_init || access_pass || access_ref);
}
bool
Variable::isCtorInitialized
()
const
{
if
( !init ) {
return
false
;
}
if
( !type->
hasNonTrivialCtor
() ) {
return
false
;
}
if
( init->
rtti_isCallFunc
() ) {
auto
cfun = static_pointer_cast<ExprCallFunc>(init);
if
( cfun->
func
&& cfun->
func
->
isTypeConstructor
) {
return
true
;
}
}
return
false
;
}
//
function
AnnotationList
cloneAnnotationList
(
const
AnnotationList & list ) {
AnnotationList clist;
for
(
auto
& ann : list ) {
auto
decl = make_smart<AnnotationDeclaration>();
decl->
annotation
= ann->
annotation
;
decl->
arguments
= ann->
arguments
;
decl->
at
= ann->
at
;
clist.
push_back
(decl);
}
return
clist;
}
FunctionPtr
Function::clone
()
const
{
auto
cfun = make_smart<Function>();
cfun->
name
= name;
for
(
const
auto
& arg : arguments ) {
cfun->
arguments
.
push_back
(arg->
clone
());
}
cfun->
annotations
=
cloneAnnotationList
(annotations);
cfun->
result
= make_smart<TypeDecl>(*result);
cfun->
body
= body->
clone
();
cfun->
index
= -
1
;
cfun->
totalStackSize
=
0
;
cfun->
totalGenLabel
= totalGenLabel;
cfun->
at
= at;
cfun->
atDecl
= atDecl;
cfun->
module
=
nullptr
;
cfun->
flags
= flags;
cfun->
moreFlags
= moreFlags;
cfun->
sideEffectFlags
= sideEffectFlags;
cfun->
inferStack
= inferStack;
cfun->
classParent
= classParent;
return
cfun;
}
LineInfo
Function::getConceptLocation
(
const
LineInfo & atL)
const
{
return
inferStack.
size
() ? inferStack[
0
].
at
: atL;
}
string
Function::getLocationExtra
()
const
{
if
( !inferStack.
size
() ) {
return
"
"
;
}
TextWriter ss;
ss <<
"
\n
while compiling
"
<<
describe
() <<
"
\n
"
;
for
(
const
auto
& ih : inferStack ) {
ss <<
"
instanced from
"
<< ih.
func
->
describe
() <<
"
at
"
<< ih.
at
.
describe
() <<
"
\n
"
;
}
return
ss.
str
();
}
string
Function::describeName
(DescribeModule moduleName)
const
{
TextWriter ss;
if
( moduleName==DescribeModule::yes &&
module
&& !
module
->
name
.
empty
() ) {
ss <<
module
->
name
<<
"
::
"
;
}
if
( !
isalpha
(name[
0
]) && name[
0
]!=
'
_
'
&& name[
0
]!=
'
`
'
) {
ss <<
"
operator
"
;
}
ss << name;
return
ss.
str
();
}
string
Function::describe
(DescribeModule moduleName, DescribeExtra extra)
const
{
TextWriter ss;
if
( moduleName==DescribeModule::yes &&
module
&& !
module
->
name
.
empty
() ) {
ss <<
module
->
name
<<
"
::
"
;
}
if
( !
isalpha
(name[
0
]) && name[
0
]!=
'
_
'
&& name[
0
]!=
'
`
'
) {
ss <<
"
operator
"
;
}
ss << name;
if
( arguments.
size
() ) {
ss <<
"
(
"
;
for
(
auto
& arg : arguments ) {
ss << arg->
name
<<
"
:
"
<< *arg->
type
;
if
( extra==DescribeExtra::yes && arg->
init
) {
ss <<
"
=
"
<< *arg->
init
;
}
if
( arg != arguments.
back
() ) ss <<
"
;
"
;
}
ss <<
"
)
"
;
}
ss <<
"
:
"
<< result->
describe
();
return
ss.
str
();
}
string
Function::getMangledName
()
const
{
FixedBufferTextWriter ss;
getMangledName
(ss);
return
ss.
str
();
}
void
Function::getMangledName
(FixedBufferTextWriter & ss)
const
{
if
(
module
&& !
module
->
name
.
empty
() ) {
ss <<
"
@
"
<<
module
->
name
<<
"
::
"
;
}
ss << name;
for
(
auto
& arg : arguments ) {
ss <<
"
"
;
arg->
type
->
getMangledName
(ss);
}
}
uint64_t
Function::getMangledNameHash
()
const
{
auto
mangledName =
getMangledName
();
return
hash_blockz64
((
uint8_t
*)mangledName.
c_str
());
}
VariablePtr
Function::findArgument
(
const
string & na) {
for
(
auto
& arg : arguments ) {
if
( arg->
name
==na ) {
return
arg;
}
}
return
nullptr
;
}
FunctionPtr
Function::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
for
(
auto
& arg : arguments ) {
vis.
preVisitArgument
(
this
, arg, arg==arguments.
back
() );
if
( arg->
type
) {
vis.
preVisit
(arg->
type
.
get
());
arg->
type
= arg->
type
->
visit
(vis);
arg->
type
= vis.
visit
(arg->
type
.
get
());
}
if
( arg->
init
&& vis.
canVisitArgumentInit
(
this
,arg,arg->
init
.
get
()) ) {
vis.
preVisitArgumentInit
(
this
, arg, arg->
init
.
get
());
arg->
init
= arg->
init
->
visit
(vis);
if
( arg->
init
) {
arg->
init
= vis.
visitArgumentInit
(
this
, arg, arg->
init
.
get
());
}
}
arg = vis.
visitArgument
(
this
, arg, arg==arguments.
back
() );
}
if
( result ) {
vis.
preVisit
(result.
get
());
result = result->
visit
(vis);
result = vis.
visit
(result.
get
());
}
if
( body ) {
vis.
preVisitFunctionBody
(
this
, body.
get
());
if
( body ) body = body->
visit
(vis);
if
( body ) body = vis.
visitFunctionBody
(
this
, body.
get
());
}
return
vis.
visit
(
this
);
}
bool
Function::isGeneric
()
const
{
for
(
const
auto
& ann : annotations ) {
if
(ann->
annotation
) {
auto
fna = static_pointer_cast<FunctionAnnotation>(ann->
annotation
);
if
(fna->
isGeneric
()) {
return
true
;
}
}
}
for
(
auto
& arg : arguments ) {
if
( arg->
type
->
isAuto
() && !arg->
init
) {
return
true
;
}
}
return
false
;
}
string
Function::getAotArgumentPrefix
(ExprCallFunc * call,
int
argIndex)
const
{
for
(
auto
& ann : annotations ) {
if
( ann->
annotation
->
rtti_isFunctionAnnotation
() ) {
auto
pAnn = static_pointer_cast<FunctionAnnotation>(ann->
annotation
);
return
pAnn->
aotArgumentPrefix
(call, argIndex);
}
}
return
"
"
;
}
string
Function::getAotArgumentSuffix
(ExprCallFunc * call,
int
argIndex)
const
{
for
(
auto
& ann : annotations ) {
if
( ann->
annotation
->
rtti_isFunctionAnnotation
() ) {
auto
pAnn = static_pointer_cast<FunctionAnnotation>(ann->
annotation
);
return
pAnn->
aotArgumentSuffix
(call, argIndex);
}
}
return
"
"
;
}
string
Function::getAotName
(ExprCallFunc * call)
const
{
for
(
auto
& ann : annotations ) {
if
( ann->
annotation
->
rtti_isFunctionAnnotation
() ) {
auto
pAnn = static_pointer_cast<FunctionAnnotation>(ann->
annotation
);
return
pAnn->
aotName
(call);
}
}
return
call->
func
->
getAotBasicName
();
}
FunctionPtr
Function::setSideEffects
( SideEffects seFlags ) {
sideEffectFlags =
uint32_t
(seFlags) &
~uint32_t
(SideEffects::unsafe);
if
(
uint32_t
(seFlags) &
uint32_t
(SideEffects::unsafe) ) {
unsafeOperation =
true
;
}
return
this
;
}
FunctionPtr
Function::setAotTemplate
() {
aotTemplate =
true
;
return
this
;
}
FunctionPtr
Function::setAnyTemplate
() {
anyTemplate =
true
;
return
this
;
}
Function *
Function::getOrigin
()
const
{
if
( fromGeneric ) {
auto
origin = fromGeneric;
while
( origin->
fromGeneric
) {
origin = origin->
fromGeneric
;
}
return
origin;
}
else
{
return
nullptr
;
}
}
//
built-in function
BuiltInFunction::BuiltInFunction
(
const
char
* fn,
const
char
* fnCpp ) {
builtIn =
true
;
name = fn;
cppName = fnCpp ? fnCpp :
"
"
;
}
void
BuiltInFunction::construct
(
const
vector<TypeDeclPtr> & args ) {
this
->
totalStackSize
=
sizeof
(Prologue);
for
(
size_t
argi=
1
; argi != args.
size
(); ++argi ) {
auto
arg = make_smart<Variable>();
arg->
name
=
"
arg
"
+
to_string
(argi-
1
);
arg->
type
= args[argi];
if
( arg->
type
->
baseType
==Type::fakeContext ) {
arg->
init
= make_smart<ExprFakeContext>(at);
arg->
init
->
generated
=
true
;
}
else
if
( arg->
type
->
baseType
==Type::fakeLineInfo ) {
arg->
init
= make_smart<ExprFakeLineInfo>(at);
arg->
init
->
generated
=
true
;
}
if
( arg->
type
->
isTempType
() ) {
arg->
type
->
implicit
=
true
;
}
this
->
arguments
.
push_back
(arg);
}
result = args[
0
];
if
( result->
isRefType
() && !result->
ref
) {
if
( result->
canCopy
() ) {
copyOnReturn =
true
;
moveOnReturn =
false
;
}
else
if
( result->
canMove
() ) {
copyOnReturn =
false
;
moveOnReturn =
true
;
}
else
if
( result->
ref
) {
//
its ref, so its fine
}
else
if
( result->
hasNonTrivialCtor
() ) {
//
we can initialize it locally
}
else
{
DAS_FATAL_ERROR
(
"
BuiltInFn %s can't be bound. It returns values which can't be copied or moved
\n
"
, name.
c_str
());
}
}
else
{
copyOnReturn =
false
;
moveOnReturn =
false
;
}
}
void
BuiltInFunction::constructExternal
(
const
vector<TypeDeclPtr> & args ) {
this
->
totalStackSize
=
sizeof
(Prologue);
for
(
size_t
argi=
1
; argi != args.
size
(); ++argi ) {
auto
arg = make_smart<Variable>();
arg->
name
=
"
arg
"
+
to_string
(argi-
1
);
arg->
type
= args[argi];
if
( arg->
type
->
baseType
==Type::fakeContext ) {
arg->
init
= make_smart<ExprFakeContext>(at);
arg->
init
->
generated
=
true
;
}
else
if
( arg->
type
->
baseType
==Type::fakeLineInfo ) {
arg->
init
= make_smart<ExprFakeLineInfo>(at);
arg->
init
->
generated
=
true
;
}
this
->
arguments
.
push_back
(arg);
}
result = args[
0
];
if
( result->
isRefType
() && !result->
ref
) {
if
( result->
canCopy
() ) {
copyOnReturn =
true
;
moveOnReturn =
false
;
}
else
if
( result->
canMove
() ) {
copyOnReturn =
false
;
moveOnReturn =
true
;
}
else
if
( !result->
ref
) {
DAS_FATAL_ERROR
(
"
ExternalFn %s can't be bound. It returns values which can't be copied or moved
\n
"
, name.
c_str
());
}
}
else
{
copyOnReturn =
false
;
moveOnReturn =
false
;
}
}
void
BuiltInFunction::constructInterop
(
const
vector<TypeDeclPtr> & args ) {
this
->
totalStackSize
=
sizeof
(Prologue);
for
(
size_t
argi=
1
; argi!=args.
size
(); ++argi ) {
auto
arg = make_smart<Variable>();
arg->
name
=
"
arg
"
+
to_string
(argi-
1
);
arg->
type
= args[argi];
if
( arg->
type
->
baseType
==Type::fakeContext ) {
arg->
init
= make_smart<ExprFakeContext>(at);
arg->
init
->
generated
=
true
;
}
else
if
( arg->
type
->
baseType
==Type::fakeLineInfo ) {
arg->
init
= make_smart<ExprFakeLineInfo>(at);
arg->
init
->
generated
=
true
;
}
this
->
arguments
.
push_back
(arg);
}
result = args[
0
];
if
( result->
isRefType
() && !result->
ref
) {
if
( result->
canCopy
() ) {
copyOnReturn =
true
;
moveOnReturn =
false
;
}
else
if
( result->
canMove
() ) {
copyOnReturn =
false
;
moveOnReturn =
true
;
}
else
if
( !result->
ref
) {
DAS_FATAL_ERROR
(
"
InteropFn %s can't be bound. It returns values which can't be copied or moved
\n
"
, name.
c_str
());
}
}
else
{
copyOnReturn =
false
;
moveOnReturn =
false
;
}
}
//
add extern func
void
addExternFunc
(Module& mod,
const
FunctionPtr & fnX,
bool
isCmres, SideEffects seFlags) {
if
(!isCmres) {
if
(fnX->
result
->
isRefType
() && !fnX->
result
->
ref
) {
DAS_FATAL_ERROR
(
"
addExtern(%s)::failed in module %s
\n
"
"
this function should be bound with addExtern<DAS_BIND_FUNC(%s), SimNode_ExtFuncCallAndCopyOrMove>
\n
"
"
likely cast<> is implemented for the return type, and it should not
\n
"
,
fnX->
name
.
c_str
(), mod.
name
.
c_str
(), fnX->
name
.
c_str
());
}
}
fnX->
setSideEffects
(seFlags);
if
(!mod.
addFunction
(fnX)) {
DAS_FATAL_ERROR
(
"
addExtern(%s) failed in module %s
\n
"
, fnX->
name
.
c_str
(), mod.
name
.
c_str
());
}
}
//
expression
ExpressionPtr
Expression::clone
(
const
ExpressionPtr & expr )
const
{
if
( !expr ) {
DAS_ASSERTF
(
0
,
"
its not ok to clone Expression as is.
"
"
if we are here, this means that ::clone function is not written correctly.
"
"
incorrect clone function can be found on the stack above this.
"
);
return
nullptr
;
}
expr->
at
= at;
expr->
type
= type ? make_smart<TypeDecl>(*type) :
nullptr
;
expr->
genFlags
= genFlags;
return
expr;
}
ExpressionPtr
Expression::autoDereference
(
const
ExpressionPtr & expr ) {
if
( expr->
type
&& !expr->
type
->
isAutoOrAlias
() && expr->
type
->
isRef
() && !expr->
type
->
isRefType
() ) {
auto
ar2l = make_smart<ExprRef2Value>();
ar2l->
subexpr
= expr;
ar2l->
at
= expr->
at
;
ar2l->
type
= make_smart<TypeDecl>(*expr->
type
);
ar2l->
type
->
ref
=
false
;
return
ar2l;
}
else
{
return
expr;
}
}
//
Reader
ExpressionPtr
ExprReader::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprReader::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprReader>(expr);
Expression::clone
(cexpr);
cexpr->
macro
= macro;
cexpr->
sequence
= sequence;
return
cexpr;
}
//
Label
ExpressionPtr
ExprLabel::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprLabel::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprLabel>(expr);
Expression::clone
(cexpr);
cexpr->
label
= label;
cexpr->
comment
= comment;
return
cexpr;
}
//
Goto
ExpressionPtr
ExprGoto::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
if
( subexpr ) {
subexpr = subexpr->
visit
(vis);
}
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprGoto::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprGoto>(expr);
Expression::clone
(cexpr);
cexpr->
label
= label;
if
( subexpr ) {
cexpr->
subexpr
= subexpr->
clone
();
}
return
cexpr;
}
//
ExprRef2Value
ExpressionPtr
ExprRef2Value::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
subexpr = subexpr->
visit
(vis);
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprRef2Value::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprRef2Value>(expr);
Expression::clone
(cexpr);
cexpr->
subexpr
= subexpr->
clone
();
return
cexpr;
}
//
ExprRef2Ptr
ExpressionPtr
ExprRef2Ptr::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
subexpr = subexpr->
visit
(vis);
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprRef2Ptr::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprRef2Ptr>(expr);
Expression::clone
(cexpr);
cexpr->
subexpr
= subexpr->
clone
();
return
cexpr;
}
//
ExprPtr2Ref
ExpressionPtr
ExprPtr2Ref::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
subexpr = subexpr->
visit
(vis);
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprPtr2Ref::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprPtr2Ref>(expr);
Expression::clone
(cexpr);
cexpr->
subexpr
= subexpr->
clone
();
return
cexpr;
}
//
ExprAddr
ExpressionPtr
ExprAddr::visit
(Visitor & vis) {
vis.
preVisit
(
this
);
if
( funcType ) {
vis.
preVisit
(funcType.
get
());
funcType = funcType->
visit
(vis);
funcType = vis.
visit
(funcType.
get
());
}
return
vis.
visit
(
this
);
}
ExpressionPtr
ExprAddr::clone
(
const
ExpressionPtr & expr )
const
{
auto
cexpr = clonePtr<ExprAddr>(expr);
Expression::clone
(cexpr);
cexpr->
target
= target;
cexpr->
func
= func;
if
(funcType) cexpr->
funcType
= make_smart<TypeDecl>(*funcType);
return
cexpr;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL