-
Notifications
You must be signed in to change notification settings - Fork 424
/
FnSymbol.cpp
1748 lines (1401 loc) · 48 KB
/
FnSymbol.cpp
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 2020-2024 Hewlett Packard Enterprise Development LP
* Copyright 2004-2019 Cray Inc.
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "FnSymbol.h"
#include "AstToText.h"
#include "astutil.h"
#include "bb.h"
#include "CollapseBlocks.h"
#include "driver.h"
#include "expandVarArgs.h"
#include "iterator.h"
#include "PartialCopyData.h"
#include "passes.h"
#include "stmt.h"
#include "stringutil.h"
#include "visibleFunctions.h"
#include "global-ast-vecs.h"
FnSymbol* chpl_gen_main = NULL;
FnSymbol* initStringLiterals = NULL;
FnSymbol* gAddModuleFn = NULL;
FnSymbol* gGenericTupleTypeCtor = NULL;
FnSymbol* gGenericTupleDestroy = NULL;
std::map<FnSymbol*, int> ftableMap;
std::vector<FnSymbol*> ftableVec;
FnSymbol::FnSymbol(const char* initName)
: Symbol(E_FnSymbol, initName), userInstantiationPointLoc(0, NULL) {
retType = dtUnknown;
where = NULL;
lifetimeConstraints= NULL;
retExprType = NULL;
body = new BlockStmt();
thisTag = INTENT_BLANK;
retTag = RET_VALUE;
iteratorInfo = NULL;
iteratorGroup = NULL;
cacheInfo = NULL;
interfaceInfo = NULL;
_this = NULL;
instantiatedFrom = NULL;
_instantiationPoint = NULL;
_backupInstantiationPoint = NULL;
basicBlocks = NULL;
calledBy = NULL;
userString = NULL;
valueFunction = NULL;
codegenUniqueNum = 1;
doc = NULL;
retSymbol = NULL;
llvmDISubprogram = NULL;
mIsNormalized = false;
_throwsError = false;
mIsGeneric = false;
mIsGenericIsValid = false;
substitutions.clear();
gFnSymbols.add(this);
formals.parent = this;
}
FnSymbol::~FnSymbol() {
cleanupIteratorInfo(this);
cleanupIteratorGroup(this);
cleanupCacheInfo(this);
BasicBlock::clear(this);
delete calledBy;
}
void FnSymbol::verify() {
Symbol::verify();
if (astTag != E_FnSymbol) {
INT_FATAL(this, "Bad FnSymbol::astTag");
}
if (_this && _this->defPoint->parentSymbol != this)
INT_FATAL(this, "Each method must contain a 'this' declaration.");
if (!this->hasFlag(FLAG_NO_FN_BODY) && normalized) {
CallExpr* last = toCallExpr(body->body.last());
if (last == NULL || last->isPrimitive(PRIM_RETURN) == false) {
INT_FATAL(this, "Last statement in normalized function is not a return");
}
}
if (formals.parent != this) {
INT_FATAL(this, "Bad AList::parent in FnSymbol");
}
if (where && where->parentSymbol != this) {
INT_FATAL(this, "Bad FnSymbol::where::parentSymbol");
}
if (lifetimeConstraints && lifetimeConstraints->parentSymbol != this) {
INT_FATAL(this, "Bad FnSymbol::lifetimeConstraints::parentSymbol");
}
if (InterfaceInfo* ifcInfo = interfaceInfo) {
// constrainedTypes: AList of DefExpr of ConstrainedType
INT_ASSERT(ifcInfo->constrainedTypes.parent == this);
for_alist(ctExpr, ifcInfo->constrainedTypes) {
Symbol* ctSym = toDefExpr(ctExpr)->sym;
ConstrainedType* ctType = toConstrainedType((toTypeSymbol(ctSym)->type));
INT_ASSERT(ctType->ctUse == CT_CGFUN_FORMAL);
}
// interfaceConstraints: AList of IfcConstraint
INT_ASSERT(ifcInfo->interfaceConstraints.parent == this);
for_alist(ic, ifcInfo->interfaceConstraints)
INT_ASSERT(isIfcConstraint(ic));
// ifcInfo->ifcReps is created during resolution
// and disappears together with its parent function at the end of
// resolution, so we never see it here.
// CG functions are generic and should be pruned at end of resolution
INT_ASSERT(!resolved);
}
if (retExprType && retExprType->parentSymbol != this) {
INT_FATAL(this, "Bad FnSymbol::retExprType::parentSymbol");
}
if (body && body->parentSymbol != this) {
INT_FATAL(this, "Bad FnSymbol::body::parentSymbol");
}
for_alist(fExpr, formals) {
DefExpr* argDef = toDefExpr(fExpr);
INT_ASSERT(argDef);
INT_ASSERT(isArgSymbol(argDef->sym));
}
if (this->instantiatedFrom && !this->instantiatedFrom->inTree())
INT_FATAL(this, "instantiatedFrom not in tree");
// check substitutions
form_Map(SymbolMapElem, e, this->substitutions) {
if (e->key && !e->key->inTree())
INT_FATAL(this, "Substitution key not in tree");
if (e->value && !e->value->inTree())
INT_FATAL(this, "Substitution value not in tree");
}
// check substitutionsPostResolve
{
size_t n = this->substitutionsPostResolve.size();
for (size_t i = 0; i < n; i++) {
const NameAndSymbol& ns = this->substitutionsPostResolve[i];
if (ns.value && !ns.value->inTree())
INT_FATAL(this, "Substitution value not in tree");
}
}
verifyNotOnList(where);
verifyNotOnList(lifetimeConstraints);
verifyNotOnList(retExprType);
verifyNotOnList(body);
verifyInTree(retType, "FnSymbol::retType");
verifyInTree(_this, "FnSymbol::_this");
verifyInTree(instantiatedFrom, "FnSymbol::instantiatedFrom");
verifyInTree(_instantiationPoint, "FnSymbol::instantiationPoint");
verifyInTree(_backupInstantiationPoint, "FnSymbol::backupInstantiationPoint");
verifyInTree(valueFunction, "FnSymbol::valueFunction");
verifyInTree(retSymbol, "FnSymbol::retSymbol");
verifyIteratorGroup(this);
}
FnSymbol* FnSymbol::copyInner(SymbolMap* map) {
// Copy members that are common to innerCopy and partialCopy.
FnSymbol* copy = this->copyInnerCore(map);
// Copy members that weren't set by copyInnerCore.
copy->where = COPY_INT(this->where);
copy->lifetimeConstraints = COPY_INT(this->lifetimeConstraints);
copy->body = COPY_INT(this->body);
copy->retExprType = COPY_INT(this->retExprType);
copy->_this = this->_this;
size_t n = this->substitutionsPostResolve.size();
for (size_t i = 0; i < n; i++) {
const NameAndSymbol& ns = this->substitutionsPostResolve[i];
copy->substitutionsPostResolve.push_back(ns);
}
if (InterfaceInfo* ifcInfoOld = this->interfaceInfo) {
InterfaceInfo* ifcInfoCopy = new InterfaceInfo(copy);
for_alist(ct, ifcInfoOld->constrainedTypes)
ifcInfoCopy->addConstrainedType(toDefExpr(COPY_INT(ct)));
for_alist(icon, ifcInfoOld->interfaceConstraints)
ifcInfoCopy->addInterfaceConstraint(toIfcConstraint(COPY_INT(icon)));
}
return copy;
}
/** Copy over members common to both copyInner and partialCopy.
*
* \param map Map from symbols in the old function to symbols in the new one
*/
FnSymbol* FnSymbol::copyInnerCore(SymbolMap* map) {
FnSymbol* newFn = new FnSymbol(this->name);
/* Copy the flags.
*
* TODO: See if it is necessary to copy flags both here and in the copy
* method.
*/
newFn->copyFlags(this);
newFn->deprecationMsg = this->deprecationMsg;
newFn->unstableMsg = this->unstableMsg;
newFn->parenfulDeprecationMsg = this->parenfulDeprecationMsg;
if (this->throwsError() == true) {
newFn->throwsErrorInit();
}
for_formals(formal, this) {
newFn->insertFormalAtTail(COPY_INT(formal->defPoint));
}
// Copy members that are needed by both copyInner and partialCopy.
newFn->astloc = this->astloc;
newFn->retType = this->retType;
newFn->thisTag = this->thisTag;
newFn->cname = this->cname;
newFn->retTag = this->retTag;
newFn->mIsGeneric = this->mIsGeneric;
newFn->mIsGenericIsValid = this->mIsGenericIsValid;
newFn->instantiatedFrom = this->instantiatedFrom;
newFn->_instantiationPoint = this->_instantiationPoint;
newFn->_backupInstantiationPoint = this->_backupInstantiationPoint;
return newFn;
}
/** Copy just enough of the AST to get through filter candidate and
* disambiguate-by-match.
*
* This function selectively copies portions of the function's AST
* representation. The goal here is to copy exactly as many nodes as are
* necessary to determine if a function is the best candidate for resolving a
* call site and no more. Special handling is necessary for the _this, where,
* and retExprType members. In addition, the return symbol needs to be made
* available despite the fact that we have skipped copying the body.
*
* \param map Map from symbols in the old function to symbols in the new one
*/
FnSymbol* FnSymbol::partialCopy(SymbolMap* map) {
FnSymbol* newFn = this->copyInnerCore(map);
// Indicate that we need to instantiate its body later.
PartialCopyData& pci = addPartialCopyData(newFn);
pci.partialCopySource = this;
if (this->hasFlag(FLAG_RESOLVED))
// Ensure 'newFn' is pruned if finalizeCopy() is never invoked.
newFn->removeFlag(FLAG_RESOLVED);
if (this->_this == NULL) {
// Case 1: No _this pointer.
newFn->_this = NULL;
} else if (Symbol* replacementThis = map->get(this->_this)) {
// Case 2: _this symbol is defined as one of the formal arguments.
newFn->_this = replacementThis;
} else {
/*
* Case 3: _this symbol is defined in the function's body.
* A new symbol is created. This symbol will have to be used
* to replace some of the symbols generated from copying the
* function's body during finalizeCopy.
*/
DefExpr* defPoint = this->_this->defPoint;
newFn->_this = this->_this->copy(map);
newFn->_this->defPoint = new DefExpr(newFn->_this,
COPY_INT(defPoint->init),
COPY_INT(defPoint->exprType));
}
// Copy and insert the where clause if it is present.
if (this->where != NULL) {
newFn->where = COPY_INT(this->where);
insert_help(newFn->where, NULL, newFn);
}
// Copy and insert the lifetimeConstraints clause if it is present.
if (this->lifetimeConstraints != NULL) {
newFn->lifetimeConstraints = COPY_INT(this->lifetimeConstraints);
insert_help(newFn->lifetimeConstraints, NULL, newFn);
}
// Copy and insert the retExprType if it is present.
if (this->retExprType != NULL) {
newFn->retExprType = COPY_INT(this->retExprType);
insert_help(newFn->retExprType, NULL, newFn);
}
/*
* Because we are not copying the function's body we need to make the return
* symbol available through other means. To do this we first have to find
* where the symbol is defined. It may either be nothing, the _this symbol, a
* formal symbol, or a symbol defined in the function's body. In the last
* case a new symbol and definition point have to be generated; the
* finalizeCopy method will replace their corresponding nodes from the body
* appropriately.
*/
auto sym = this->getReturnSymbol();
if (sym == nullptr) {
// Case 0: Function has no body, and thus no RVV.
newFn->retSymbol = nullptr;
} else if (sym == gVoid) {
// Case 1: Function returns void.
newFn->retSymbol = gVoid;
} else if (sym == this->_this) {
// Case 2: Function returns _this.
newFn->retSymbol = newFn->_this;
} else if (Symbol* replacementRet = map->get(sym)) {
// Case 3: Function returns a formal argument.
newFn->retSymbol = replacementRet;
} else {
// Case 4: Function returns a symbol defined in the body.
DefExpr* defPoint = sym->defPoint;
newFn->retSymbol = COPY_INT(sym);
newFn->retSymbol->defPoint = new DefExpr(newFn->retSymbol,
COPY_INT(defPoint->init),
COPY_INT(defPoint->exprType));
update_symbols(newFn->retSymbol, map);
}
// Add a map entry from this FnSymbol to the newly generated one.
map->put(this, newFn);
// Update symbols in the sub-AST as is appropriate.
update_symbols(newFn, map);
// Copy over the partialCopyMap, to be used later in finalizeCopy.
pci.partialCopyMap.copy(*map);
return newFn;
}
/** Finish copying the function's AST after a partial copy.
*
* This function finishes the work started by partialCopy. This involves
* copying the setter and body, and repairing some inconsistencies in the
* copied body.
*
* \param map Map from symbols in the old function to symbols in the new one
*/
void FnSymbol::finalizeCopy() {
if (PartialCopyData* pci = getPartialCopyData(this)) {
FnSymbol* const partialCopySource = pci->partialCopySource;
// Make sure that the source has been finalized.
partialCopySource->finalizeCopy();
if (partialCopySource->hasFlag(FLAG_RESOLVED))
this->addFlag(FLAG_RESOLVED);
SET_LINENO(this);
// Retrieve our old/new symbol map from the partial copy process.
SymbolMap* map = &(pci->partialCopyMap);
/*
* When we reach this point we will be in one of three scenarios:
* 1) The function's body is empty and needs to be copied over from the
* copy source.
* 2) The function's body has been replaced and we don't need to do
* anything else.
* 3) The function has had varargs expanded and we need to copy over the
* added statements from the old block to a new copy of the body from
* the source.
*/
if (this->hasFlag(FLAG_EXPANDED_VARARGS)) {
// Alias the old body and make a new copy of the body from the source.
BlockStmt* varArgNodes = this->body;
this->body->replace( COPY_INT(partialCopySource->body) );
/*
* Iterate over the statements that have been added to the function body
* and add them to the new body.
*/
for_alist_backward(node, varArgNodes->body) {
this->body->insertAtHead(node->remove());
}
} else if (this->body->body.length == 0) {
this->body->replace( COPY_INT(partialCopySource->body) );
}
Symbol* replacementThis = map->get(partialCopySource->_this);
/*
* Two cases may arise here. The first is when the _this symbol is defined
* in the formal arguments. In this case no additional work needs to be
* done. In the second case the function's _this symbol is defined in the
* function's body. In this case we need to repair the new/old symbol map
* and replace the definition point in the body with our existing def point.
*/
if (replacementThis != this->_this) {
/*
* In Case 2:
* partialCopySource->_this := A
* this->_this := B
*
* map[A] := C
*/
// Set map[A] := B
map->put(partialCopySource->_this, this->_this);
// Set map[C] := B
map->put(replacementThis, this->_this);
// Replace the definition of _this in the body: def(C) -> def(B)
replacementThis->defPoint->replace(this->_this->defPoint);
}
/*
* Cases where the return symbol is gVoid or this->_this don't require
* any additional actions.
*/
if (this->retSymbol != gVoid && this->retSymbol != this->_this) {
Symbol* replacementRet = map->get(partialCopySource->getReturnSymbol());
if (replacementRet != this->retSymbol) {
/*
* We now know that retSymbol is defined in function's body. We must
* now replace the duplicate symbol and its definition point with the
* ones generated in partialCopy. This is the exact same process as
* was done above for the _this symbol.
*/
replacementRet->defPoint->replace(this->retSymbol->defPoint);
map->put(partialCopySource->getReturnSymbol(), this->retSymbol);
map->put(replacementRet, this->retSymbol);
}
}
/*
* Null out the return symbol so that future changes to the return symbol
* will be reflected in calls to getReturnSymbol().
*/
this->retSymbol = NULL;
// Repair broken up-pointers.
insert_help(this, this->defPoint, this->defPoint->parentSymbol);
/*
* Update all old symbols left in the function's AST with their appropriate
* replacements.
*/
update_symbols(this, map);
// Replace vararg formal if appropriate.
if (pci->varargOldFormal) {
substituteVarargTupleRefs(this, pci);
}
// For CG fns calling to other CG fns.
if (InterfaceInfo* ifcInfo = partialCopySource->interfaceInfo)
handleCallsToOtherCGfuns(partialCopySource, ifcInfo, *map, this);
// Clean up book keeping information.
clearPartialCopyData(this);
}
}
void FnSymbol::replaceChild(BaseAST* oldAst, BaseAST* newAst) {
if (oldAst == body) {
body = toBlockStmt(newAst);
} else if (oldAst == where) {
where = toBlockStmt(newAst);
} else if (oldAst == lifetimeConstraints) {
lifetimeConstraints = toBlockStmt(newAst);
} else if (oldAst == retExprType) {
retExprType = toBlockStmt(newAst);
} else {
INT_FATAL(this, "Unexpected case in FnSymbol::replaceChild");
}
}
void FnSymbol::insertAtHead(Expr* ast) {
body->insertAtHead(ast);
}
void FnSymbol::insertAtTail(Expr* ast) {
body->insertAtTail(ast);
}
void FnSymbol::insertAtHead(const char* format, ...) {
va_list args;
va_start(args, format);
insertAtHead(new_Expr(format, args));
va_end(args);
}
void FnSymbol::insertAtTail(const char* format, ...) {
va_list args;
va_start(args, format);
insertAtTail(new_Expr(format, args));
va_end(args);
}
Symbol* FnSymbol::getReturnSymbol() {
Symbol* retval = this->retSymbol;
if (this->hasFlag(FLAG_NO_FN_BODY)) {
INT_ASSERT(retval == nullptr);
return nullptr;
}
if (retval == NULL) {
CallExpr* ret = toCallExpr(body->body.last());
if (ret != NULL && ret->isPrimitive(PRIM_RETURN) == true) {
if (SymExpr* sym = toSymExpr(ret->get(1))) {
retval = sym->symbol();
} else {
INT_FATAL(this, "function is not normal");
}
}
}
if (retval == NULL) {
INT_FATAL(this, "function is not normal");
}
return retval;
}
FunctionType* FnSymbol::computeAndSetType() {
auto ret = FunctionType::get(this);
this->type = ret;
return ret;
}
// Removes all statements from body and adds all statements from block.
void FnSymbol::replaceBodyStmtsWithStmts(BlockStmt* block) {
for_alist(stmt, this->body->body) {
stmt->remove();
}
for_alist(stmt, block->body) {
this->body->insertAtTail(stmt->remove());
}
}
// Removes all statements from body and adds expr
void FnSymbol::replaceBodyStmtsWithStmt(Expr* addStmt) {
for_alist(stmt, this->body->body) {
stmt->remove();
}
this->body->insertAtTail(addStmt);
}
void FnSymbol::setInstantiationPoint(Expr* expr) {
if (expr == NULL) {
this->_instantiationPoint = NULL;
this->_backupInstantiationPoint = NULL;
} else {
BlockStmt* block = toBlockStmt(expr);
if (block == NULL || block->blockTag == BLOCK_SCOPELESS)
block = getInstantiationPoint(expr);
this->_instantiationPoint = block;
this->_backupInstantiationPoint = block->getFunction();
}
//if (expr != NULL)
// userInstantiationPointLoc = getUserInstantiationPoint(this);
}
BlockStmt* FnSymbol::instantiationPoint() const {
if (this->_instantiationPoint && this->_instantiationPoint->parentSymbol)
return this->_instantiationPoint;
else if (this->_backupInstantiationPoint)
return this->_backupInstantiationPoint->body;
else
return NULL;
}
void FnSymbol::insertBeforeEpilogue(Expr* ast) {
LabelSymbol* label = getEpilogueLabel();
if (label) {
DefExpr* def = label->defPoint;
def->insertBefore(ast);
} else {
// if an epilogue is later added, this will be excluded
CallExpr* ret = toCallExpr(body->body.last());
ret->insertBefore(ast);
}
}
void FnSymbol::insertIntoEpilogue(Expr* ast) {
getOrCreateEpilogueLabel(); // always inserting into an epilogue
CallExpr* ret = toCallExpr(body->body.last());
ret->insertBefore(ast);
}
LabelSymbol* FnSymbol::getEpilogueLabel() {
CallExpr* ret = toCallExpr(body->body.last());
LabelSymbol* retval = NULL;
if (ret != NULL && ret->isPrimitive(PRIM_RETURN) == true) {
for (Expr* last = ret; last != NULL && retval == NULL; last = last->prev) {
if (DefExpr* def = toDefExpr(last->prev)) {
if (LabelSymbol* label = toLabelSymbol(def->sym)) {
if (label->hasFlag(FLAG_EPILOGUE_LABEL) == true) {
retval = label;
}
}
}
}
} else {
INT_FATAL(this, "function is not normal");
}
return retval;
}
LabelSymbol* FnSymbol::getOrCreateEpilogueLabel() {
LabelSymbol* label = getEpilogueLabel();
if (label == NULL) {
label = new LabelSymbol(astr("_end", name));
label->addFlag(FLAG_EPILOGUE_LABEL);
CallExpr* ret = toCallExpr(body->body.last());
ret->insertBefore(new DefExpr(label));
}
return label;
}
void FnSymbol::insertFormalAtHead(BaseAST* ast) {
Expr* toInsert = NULL;
if (ArgSymbol* arg = toArgSymbol(ast)) {
toInsert = new DefExpr(arg);
} else if (DefExpr* def = toDefExpr(ast)) {
toInsert = def;
} else {
INT_FATAL(ast, "Bad argument to FnSymbol::insertFormalAtHead");
}
formals.insertAtHead(toInsert);
parent_insert_help(this, toInsert);
}
void FnSymbol::insertFormalAtTail(BaseAST* ast) {
Expr* toInsert = NULL;
if (ArgSymbol* arg = toArgSymbol(ast)) {
toInsert = new DefExpr(arg);
} else if (DefExpr* def = toDefExpr(ast)) {
toInsert = def;
} else {
INT_FATAL(ast, "Bad argument to FnSymbol::insertFormalAtTail");
}
formals.insertAtTail(toInsert);
parent_insert_help(this, toInsert);
}
int FnSymbol::numFormals() const {
return formals.length;
}
ArgSymbol* FnSymbol::getFormal(int i) const {
return toArgSymbol(toDefExpr(formals.get(i))->sym);
}
void FnSymbol::collapseBlocks() {
CollapseBlocks visitor;
body->accept(&visitor);
}
//
// If 'this' has a single use as the callee of a CallExpr,
// return that CallExpr. Otherwise return NULL.
//
CallExpr* FnSymbol::singleInvocation() const {
SymExpr* se = firstSymExpr();
CallExpr* retval = NULL;
if (se == NULL) {
// no uses at all
retval = NULL;
} else if (se != lastSymExpr()) {
// more than one use
retval = NULL;
// Got exactly one use. Check how it is used.
} else if (CallExpr* parent = toCallExpr(se->parentExpr)) {
if (se == parent->baseExpr) {
retval = parent;
}
else if (parent->isPrimitive(PRIM_GPU_KERNEL_LAUNCH)) {
if (se == parent->get(1)) {
retval = parent;
}
}
}
// The use is not as the callee, ex. as a FCF.
return retval;
}
const char* FnSymbol::getParenfulDeprecationMsg() const {
if (parenfulDeprecationMsg[0] == '\0') {
const char* msg = astr("the parenful form of function ", name, " is deprecated");
return msg;
}
else {
return parenfulDeprecationMsg.c_str();
}
}
//
// Support for constrained generics.
//
InterfaceInfo::InterfaceInfo(FnSymbol* parentFn) {
parentFn->interfaceInfo = this;
constrainedTypes.parent = parentFn;
interfaceConstraints.parent = parentFn;
}
void InterfaceInfo::addConstrainedType(DefExpr* def) {
constrainedTypes.insertAtTail(def);
}
void InterfaceInfo::addInterfaceConstraint(IfcConstraint* icon) {
interfaceConstraints.insertAtTail(icon);
}
//
// Labels this function as generic or non-generic.
// Returns:
// * TGR_NEWLY_TAGGED - if this function has not been labeled before,
// * TGR_ALREADY_TAGGED - otherwise,
// * TGR_TAGGING_ABORTED - if this invocation is recursive and so is aborted.
//
TagGenericResult FnSymbol::tagIfGeneric(SymbolMap* map, bool abortOK) {
if (isGenericIsValid()) {
// generic-ness has already been established
return TGR_ALREADY_TAGGED;
} else if (isConstrainedGeneric()) {
setGeneric(true);
return TGR_NEWLY_TAGGED;
} else {
// avoid recursing for the function.
static std::set<Symbol*> seen;
if (seen.count(this)) {
INT_ASSERT(abortOK);
return TGR_TAGGING_ABORTED;
}
seen.insert(this);
// compute and set this function's genericity
bool generic = hasGenericFormals(map);
setGeneric(generic);
seen.erase(this);
return TGR_NEWLY_TAGGED;
}
}
static void checkFormalType(const FnSymbol* enclosingFn, ArgSymbol* formal) {
if (! formal->typeExprFromDefaultExpr) {
BaseAST* typeExp = formal->typeExpr->body.tail;
if (SymExpr* se = toSymExpr(typeExp)) {
Symbol* sym = se->symbol();
if (!isTypeSymbol(sym) && !sym->hasFlag(FLAG_TYPE_VARIABLE)) {
if (formal == enclosingFn->_this) {
USR_FATAL_CONT(formal, "Method defined on non-type '%s'",
sym->name);
} else {
Immediate* imm = nullptr;
if (VarSymbol* var = toVarSymbol(sym)) imm = var->immediate;
USR_FATAL_CONT(typeExp, "The declared type of the formal "
"%s is non-type '%s'", formal->name,
imm == nullptr ? sym->name : imm->to_string().c_str());
}
}
} else if (CallExpr* call = toCallExpr(typeExp)) {
if (FnSymbol* target = call->resolvedFunction())
if (target->retTag != RET_TYPE)
USR_FATAL_CONT(typeExp, "The declared type of the formal "
"%s is given by non-type function '%s'", formal->name, target->name);
}
if (formal->type->symbol->hasFlag(FLAG_GENERIC)) {
if (enclosingFn->hasFlag(FLAG_COMPILER_GENERATED)) {
// We shouldn't complain to the user about routines we generate
} else {
warnIfGenericFormalMissingQ(formal, formal->type, nullptr);
}
}
}
}
//
// Scan the formals and return true if there are any
// generic formals.
//
// 'map' is expected to be non-NULL if this function has been instantiated.
//
bool FnSymbol::hasGenericFormals(SymbolMap* map) const {
bool anyGeneric = false;
std::vector<unsigned char> formalIsGeneric;
int count = 0;
for_formals(formal, this) {
bool isGeneric = false;
if (formal->type == dtUnknown && formal->typeExpr != NULL) {
// If the type expression does not depend on a previous generic formal,
// we can resolve it now, even if there was a previous generic formal.
bool dependsOnPreviousGeneric = false;
int i = 0;
if (anyGeneric) {
for_formals(prevFormal, this) {
if (i >= count)
break;
int cursz = formalIsGeneric.size();
if (i < cursz && formalIsGeneric[i] == 1) {
// check to see if prevFormal is used in the typeExpr
// for the current formal.
if (findSymExprFor(formal->typeExpr, prevFormal) != NULL) {
dependsOnPreviousGeneric = true;
break;
}
}
i++;
}
if (dependsOnPreviousGeneric) {
// Stop resolving formals.
INT_ASSERT(anyGeneric == true);
break;
}
}
resolveBlockStmt(formal->typeExpr);
formal->type = formal->typeExpr->body.tail->getValType();
checkFormalType(this, formal);
}
if (formal->originalIntent == INTENT_OUT) {
// out intent formals never make a function generic
// (type is inferred from the function body)
} else if (formal->intent == INTENT_PARAM) {
isGeneric = true;
} else if (isConstrainedType(formal->type)) {
// A CG function is known to be generic, so we should not be
// querying hasGenericFormals().
INT_ASSERT(! isConstrainedGeneric());
// It can be:
// - a required fn in an 'interface' declaration
// - a generic implementation instantiated with a standin type
// - an interim instantiation of a CG function
} else if (formal->type->symbol->hasFlag(FLAG_GENERIC) == true) {
bool formalInstantiated = false;
if (map != NULL && formal->hasFlag(FLAG_TYPE_VARIABLE)) {
form_Map(SymbolMapElem, e, *map) {
if (e->key->name == formal->name) {
formalInstantiated = true;
break;
}
}
}
if (!formalInstantiated) {
bool typeHasGenericDefaults = false;
if (AggregateType* at = toAggregateType(formal->type))
typeHasGenericDefaults = at->isGenericWithDefaults();
if (typeHasGenericDefaults == false ||
formal->hasFlag(FLAG_MARKED_GENERIC) == true ||
formal == _this) {
if (!(formal == _this && (isInitializer() || isCopyInit()))) {
isGeneric = true;
}
}
}
}
// init= on generic types need to be considered generic so that 'this.type'
// stuff will resolve.
if (map == NULL && formal == _this && isCopyInit() && _this->type->symbol->hasFlag(FLAG_GENERIC)) {
isGeneric = true;
}
if (isGeneric == true) {
if (hasFlag(FLAG_EXPORT)) {
USR_FATAL_CONT(this,
"exported function `%s` can't be generic", name);
USR_PRINT(this,
" formal argument '%s' causes it to be", formal->name);
}
}
anyGeneric = anyGeneric || isGeneric;
if (isGeneric) {
while ((int)formalIsGeneric.size() < count) {
formalIsGeneric.push_back(0);
}
formalIsGeneric.push_back(1);