forked from ldc-developers/ldc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.d
2384 lines (2090 loc) · 71.3 KB
/
func.d
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
/**
* Defines a function declaration.
*
* Includes:
* - function/delegate literals
* - function aliases
* - (static/shared) constructors/destructors/post-blits
* - `invariant`
* - `unittest`
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/func.d, _func.d)
* Documentation: https://dlang.org/phobos/dmd_func.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/func.d
*/
module dmd.func;
import core.stdc.stdio;
import core.stdc.string;
import dmd.aggregate;
import dmd.arraytypes;
import dmd.astenums;
import dmd.blockexit;
import dmd.gluelayer;
import dmd.dcast;
import dmd.dclass;
import dmd.declaration;
import dmd.delegatize;
import dmd.dinterpret;
import dmd.dmodule;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dtemplate;
import dmd.errors;
import dmd.escape;
import dmd.expression;
import dmd.globals;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.init;
import dmd.location;
import dmd.mtype;
import dmd.objc;
import dmd.root.aav;
import dmd.common.outbuffer;
import dmd.rootobject;
import dmd.root.string;
import dmd.root.stringtable;
import dmd.semantic2;
import dmd.semantic3;
import dmd.statement_rewrite_walker;
import dmd.statement;
import dmd.tokens;
import dmd.typesem;
import dmd.visitor;
version (IN_GCC) {}
else version (IN_LLVM) {}
else version = MARS;
/// Inline Status
enum ILS : ubyte
{
uninitialized, /// not computed yet
no, /// cannot inline
yes, /// can inline
}
enum BUILTIN : ubyte
{
unknown = 255, /// not known if this is a builtin
unimp = 0, /// this is not a builtin
gcc, /// this is a GCC builtin
llvm, /// this is an LLVM builtin
sin,
cos,
tan,
sqrt,
fabs,
ldexp,
log,
log2,
log10,
exp,
expm1,
exp2,
round,
floor,
ceil,
trunc,
copysign,
pow,
fmin,
fmax,
fma,
isnan,
isinfinity,
isfinite,
bsf,
bsr,
bswap,
popcnt,
yl2x,
yl2xp1,
toPrecFloat,
toPrecDouble,
toPrecReal,
ctfeWrite,
// IN_LLVM:
llvm_sin,
llvm_cos,
llvm_sqrt,
llvm_exp,
llvm_exp2,
llvm_log,
llvm_log2,
llvm_log10,
llvm_fabs,
llvm_minnum,
llvm_maxnum,
llvm_floor,
llvm_ceil,
llvm_trunc,
llvm_rint,
llvm_nearbyint,
llvm_round,
llvm_fma,
llvm_copysign,
llvm_bswap,
llvm_cttz,
llvm_ctlz,
llvm_ctpop,
llvm_expect,
}
private struct FUNCFLAG
{
bool purityInprocess; /// working on determining purity
bool safetyInprocess; /// working on determining safety
bool nothrowInprocess; /// working on determining nothrow
bool nogcInprocess; /// working on determining @nogc
bool returnInprocess; /// working on inferring 'return' for parameters
bool inlineScanned; /// function has been scanned for inline possibilities
bool inferScope; /// infer 'scope' for parameters
bool hasCatches; /// function has try-catch statements
bool skipCodegen; /// do not generate code for this function.
bool printf; /// is a printf-like function
bool scanf; /// is a scanf-like function
bool noreturn; /// the function does not return
bool isNRVO = true; /// Support for named return value optimization
bool isNaked; /// The function is 'naked' (see inline ASM)
bool isGenerated; /// The function is compiler generated (e.g. `opCmp`)
bool isIntroducing; /// If this function introduces the overload set
bool hasSemantic3Errors; /// If errors in semantic3 this function's frame ptr
bool hasNoEH; /// No exception unwinding is needed
bool inferRetType; /// Return type is to be inferred
bool hasDualContext; /// has a dual-context 'this' parameter
bool hasAlwaysInlines; /// Contains references to functions that must be inlined
bool isCrtCtor; /// Has attribute pragma(crt_constructor)
bool isCrtDtor; /// Has attribute pragma(crt_destructor)
bool hasEscapingSiblings;/// Has sibling functions that escape
bool computedEscapingSiblings; /// `hasEscapingSiblings` has been computed
bool dllImport; /// __declspec(dllimport)
bool dllExport; /// __declspec(dllexport)
}
/***********************************************************
* Tuple of result identifier (possibly null) and statement.
* This is used to store out contracts: out(id){ ensure }
*/
extern (C++) struct Ensure
{
Identifier id;
Statement ensure;
Ensure syntaxCopy()
{
return Ensure(id, ensure.syntaxCopy());
}
/*****************************************
* Do syntax copy of an array of Ensure's.
*/
static Ensures* arraySyntaxCopy(Ensures* a)
{
Ensures* b = null;
if (a)
{
b = a.copy();
foreach (i, e; *a)
{
(*b)[i] = e.syntaxCopy();
}
}
return b;
}
}
/***********************************************************
* Most functions don't have contracts, so save memory by grouping
* this information into a separate struct
*/
private struct ContractInfo
{
Statements* frequires; /// in contracts
Ensures* fensures; /// out contracts
Statement frequire; /// lowered in contract
Statement fensure; /// lowered out contract
FuncDeclaration fdrequire; /// function that does the in contract
FuncDeclaration fdensure; /// function that does the out contract
Expressions* fdrequireParams; /// argument list for __require
Expressions* fdensureParams; /// argument list for __ensure
}
/***********************************************************
*/
extern (C++) class FuncDeclaration : Declaration
{
Statement fbody; /// function body
FuncDeclarations foverrides; /// functions this function overrides
private ContractInfo* contracts; /// contract information
const(char)* mangleString; /// mangled symbol created from mangleExact()
version (IN_LLVM)
{
uint priority;
// true if overridden with the pragma(LDC_allow_inline); statement
bool allowInlining = false;
// true if set with the pragma(LDC_never_inline); statement
bool neverInline = false;
// Whether to emit instrumentation code if -fprofile-instr-generate is specified,
// the value is set with pragma(LDC_profile_instr, true|false)
bool emitInstrumentation = true;
}
VarDeclaration vresult; /// result variable for out contracts
LabelDsymbol returnLabel; /// where the return goes
bool[size_t] isTypeIsolatedCache; /// cache for the potentially very expensive isTypeIsolated check
// used to prevent symbols in different
// scopes from having the same name
DsymbolTable localsymtab;
VarDeclaration vthis; /// 'this' parameter (member and nested)
VarDeclaration v_arguments; /// '_arguments' parameter
VarDeclaration v_argptr; /// '_argptr' variable
VarDeclarations* parameters; /// Array of VarDeclaration's for parameters
DsymbolTable labtab; /// statement label symbol table
Dsymbol overnext; /// next in overload list
FuncDeclaration overnext0; /// next in overload list (only used during IFTI)
Loc endloc; /// location of closing curly bracket
int vtblIndex = -1; /// for member functions, index into vtbl[]
ILS inlineStatusStmt = ILS.uninitialized;
ILS inlineStatusExp = ILS.uninitialized;
PINLINE inlining = PINLINE.default_;
int inlineNest; /// !=0 if nested inline
ForeachStatement fes; /// if foreach body, this is the foreach
BaseClass* interfaceVirtual; /// if virtual, but only appears in base interface vtbl[]
/** if !=NULL, then this is the type
of the 'introducing' function
this one is overriding
*/
Type tintro;
StorageClass storage_class2; /// storage class for template onemember's
// Things that should really go into Scope
/// 1 if there's a return exp; statement
/// 2 if there's a throw statement
/// 4 if there's an assert(0)
/// 8 if there's inline asm
/// 16 if there are multiple return statements
int hasReturnExp;
VarDeclaration nrvo_var; /// variable to replace with shidden
version (IN_LLVM) {} else
{
Symbol* shidden; /// hidden pointer passed to function
}
ReturnStatements* returns;
GotoStatements* gotos; /// Gotos with forward references
version (MARS)
{
VarDeclarations* alignSectionVars; /// local variables with alignment needs larger than stackAlign
Symbol* salignSection; /// pointer to aligned section, if any
}
/// set if this is a known, builtin function we can evaluate at compile time
BUILTIN builtin = BUILTIN.unknown;
/// set if someone took the address of this function
int tookAddressOf;
bool requiresClosure; // this function needs a closure
/** local variables in this function which are referenced by nested functions
* (They'll get put into the "closure" for this function.)
*/
VarDeclarations closureVars;
/** Outer variables which are referenced by this nested function
* (the inverse of closureVars)
*/
VarDeclarations outerVars;
/// Sibling nested functions which called this one
FuncDeclarations siblingCallers;
FuncDeclarations *inlinedNestedCallees;
/// In case of failed `@safe` inference, store the error that made the function `@system` for
/// better diagnostics
AttributeViolation* safetyViolation;
AttributeViolation* nogcViolation;
AttributeViolation* pureViolation;
AttributeViolation* nothrowViolation;
/// See the `FUNCFLAG` struct
import dmd.common.bitfields;
mixin(generateBitFields!(FUNCFLAG, uint));
/**
* Data for a function declaration that is needed for the Objective-C
* integration.
*/
ObjcFuncDeclaration objc;
extern (D) this(const ref Loc loc, const ref Loc endloc, Identifier ident, StorageClass storage_class, Type type, bool noreturn = false)
{
super(loc, ident);
//.printf("FuncDeclaration(id = '%s', type = %s)\n", ident.toChars(), type.toChars());
//.printf("storage_class = x%llx\n", storage_class);
this.storage_class = storage_class;
this.type = type;
if (type)
{
// Normalize storage_class, because function-type related attributes
// are already set in the 'type' in parsing phase.
this.storage_class &= ~(STC.TYPECTOR | STC.FUNCATTR);
}
this.endloc = endloc;
if (noreturn)
this.noreturn = true;
/* The type given for "infer the return type" is a TypeFunction with
* NULL for the return type.
*/
if (type && type.nextOf() is null)
this.inferRetType = true;
}
static FuncDeclaration create(const ref Loc loc, const ref Loc endloc, Identifier id, StorageClass storage_class, Type type, bool noreturn = false)
{
return new FuncDeclaration(loc, endloc, id, storage_class, type, noreturn);
}
final nothrow pure @safe
{
private ref ContractInfo getContracts()
{
if (!contracts)
contracts = new ContractInfo();
return *contracts;
}
// getters
inout(Statements*) frequires() inout { return contracts ? contracts.frequires : null; }
inout(Ensures*) fensures() inout { return contracts ? contracts.fensures : null; }
inout(Statement) frequire() inout { return contracts ? contracts.frequire: null; }
inout(Statement) fensure() inout { return contracts ? contracts.fensure : null; }
inout(FuncDeclaration) fdrequire() inout { return contracts ? contracts.fdrequire : null; }
inout(FuncDeclaration) fdensure() inout { return contracts ? contracts.fdensure: null; }
inout(Expressions*) fdrequireParams() inout { return contracts ? contracts.fdrequireParams: null; }
inout(Expressions*) fdensureParams() inout { return contracts ? contracts.fdensureParams: null; }
extern (D) private static string generateContractSetter(string field, string type)
{
return type ~ " " ~ field ~ "(" ~ type ~ " param)" ~
"{
if (!param && !contracts) return null;
return getContracts()." ~ field ~ " = param;
}";
}
mixin(generateContractSetter("frequires", "Statements*"));
mixin(generateContractSetter("fensures", "Ensures*"));
mixin(generateContractSetter("frequire", "Statement"));
mixin(generateContractSetter("fensure", "Statement"));
mixin(generateContractSetter("fdrequire", "FuncDeclaration"));
mixin(generateContractSetter("fdensure", "FuncDeclaration"));
mixin(generateContractSetter("fdrequireParams", "Expressions*"));
mixin(generateContractSetter("fdensureParams", "Expressions*"));
}
override FuncDeclaration syntaxCopy(Dsymbol s)
{
//printf("FuncDeclaration::syntaxCopy('%s')\n", toChars());
FuncDeclaration f = s ? cast(FuncDeclaration)s
: new FuncDeclaration(loc, endloc, ident, storage_class, type.syntaxCopy(), this.noreturn != 0);
f.frequires = frequires ? Statement.arraySyntaxCopy(frequires) : null;
f.fensures = fensures ? Ensure.arraySyntaxCopy(fensures) : null;
f.fbody = fbody ? fbody.syntaxCopy() : null;
version (IN_LLVM)
{
f.mangleOverride = mangleOverride;
}
return f;
}
override final bool equals(const RootObject o) const
{
if (this == o)
return true;
if (auto s = isDsymbol(o))
{
auto fd1 = this;
auto fd2 = s.isFuncDeclaration();
if (!fd2)
return false;
auto fa1 = fd1.isFuncAliasDeclaration();
auto faf1 = fa1 ? fa1.toAliasFunc() : fd1;
auto fa2 = fd2.isFuncAliasDeclaration();
auto faf2 = fa2 ? fa2.toAliasFunc() : fd2;
if (fa1 && fa2)
{
return faf1.equals(faf2) && fa1.hasOverloads == fa2.hasOverloads;
}
bool b1 = fa1 !is null;
if (b1 && faf1.isUnique() && !fa1.hasOverloads)
b1 = false;
bool b2 = fa2 !is null;
if (b2 && faf2.isUnique() && !fa2.hasOverloads)
b2 = false;
if (b1 != b2)
return false;
return faf1.toParent().equals(faf2.toParent()) &&
faf1.ident.equals(faf2.ident) &&
faf1.type.equals(faf2.type);
}
return false;
}
/****************************************************
* Overload this FuncDeclaration with the new one f.
* Return true if successful; i.e. no conflict.
*/
override bool overloadInsert(Dsymbol s)
{
//printf("FuncDeclaration::overloadInsert(s = %s) this = %s\n", s.toChars(), toChars());
assert(s != this);
AliasDeclaration ad = s.isAliasDeclaration();
if (ad)
{
if (overnext)
return overnext.overloadInsert(ad);
if (!ad.aliassym && ad.type.ty != Tident && ad.type.ty != Tinstance && ad.type.ty != Ttypeof)
{
//printf("\tad = '%s'\n", ad.type.toChars());
return false;
}
overnext = ad;
//printf("\ttrue: no conflict\n");
return true;
}
TemplateDeclaration td = s.isTemplateDeclaration();
if (td)
{
if (!td.funcroot)
td.funcroot = this;
if (overnext)
return overnext.overloadInsert(td);
overnext = td;
return true;
}
FuncDeclaration fd = s.isFuncDeclaration();
if (!fd)
return false;
version (none)
{
/* Disable this check because:
* const void foo();
* semantic() isn't run yet on foo(), so the const hasn't been
* applied yet.
*/
if (type)
{
printf("type = %s\n", type.toChars());
printf("fd.type = %s\n", fd.type.toChars());
}
// fd.type can be NULL for overloaded constructors
if (type && fd.type && fd.type.covariant(type) && fd.type.mod == type.mod && !isFuncAliasDeclaration())
{
//printf("\tfalse: conflict %s\n", kind());
return false;
}
}
if (overnext)
{
td = overnext.isTemplateDeclaration();
if (td)
fd.overloadInsert(td);
else
return overnext.overloadInsert(fd);
}
overnext = fd;
//printf("\ttrue: no conflict\n");
return true;
}
/********************************************
* find function template root in overload list
*/
extern (D) final TemplateDeclaration findTemplateDeclRoot()
{
FuncDeclaration f = this;
while (f && f.overnext)
{
//printf("f.overnext = %p %s\n", f.overnext, f.overnext.toChars());
TemplateDeclaration td = f.overnext.isTemplateDeclaration();
if (td)
return td;
f = f.overnext.isFuncDeclaration();
}
return null;
}
/********************************************
* Returns true if function was declared
* directly or indirectly in a unittest block
*/
final bool inUnittest()
{
Dsymbol f = this;
do
{
if (f.isUnitTestDeclaration())
return true;
f = f.toParent();
}
while (f);
return false;
}
/********************************
* Searches for a label with the given identifier. This function will insert a new
* `LabelDsymbol` into `labtab` if it does not contain a mapping for `ident`.
*
* Params:
* ident = identifier of the requested label
* loc = location used when creating a new `LabelDsymbol`
*
* Returns: the `LabelDsymbol` for `ident`
*/
final LabelDsymbol searchLabel(Identifier ident, const ref Loc loc)
{
Dsymbol s;
if (!labtab)
labtab = new DsymbolTable(); // guess we need one
s = labtab.lookup(ident);
if (!s)
{
s = new LabelDsymbol(ident, loc);
labtab.insert(s);
}
return cast(LabelDsymbol)s;
}
/*****************************************
* Determine lexical level difference from `this` to nested function `fd`.
* Params:
* fd = target of call
* intypeof = !=0 if inside typeof
* Returns:
* 0 same level
* >0 decrease nesting by number
* -1 increase nesting by 1 (`fd` is nested within `this`)
* LevelError error, `this` cannot call `fd`
*/
extern (D) final int getLevel(FuncDeclaration fd, int intypeof)
{
//printf("FuncDeclaration::getLevel(fd = '%s')\n", fd.toChars());
Dsymbol fdparent = fd.toParent2();
if (fdparent == this)
return -1;
Dsymbol s = this;
int level = 0;
while (fd != s && fdparent != s.toParent2())
{
//printf("\ts = %s, '%s'\n", s.kind(), s.toChars());
if (auto thisfd = s.isFuncDeclaration())
{
if (!thisfd.isNested() && !thisfd.vthis && !intypeof)
return LevelError;
}
else
{
if (auto thiscd = s.isAggregateDeclaration())
{
/* AggregateDeclaration::isNested returns true only when
* it has a hidden pointer.
* But, calling the function belongs unrelated lexical scope
* is still allowed inside typeof.
*
* struct Map(alias fun) {
* typeof({ return fun(); }) RetType;
* // No member function makes Map struct 'not nested'.
* }
*/
if (!thiscd.isNested() && !intypeof)
return LevelError;
}
else
return LevelError;
}
s = s.toParentP(fd);
assert(s);
level++;
}
return level;
}
enum LevelError = -2;
override const(char)* toPrettyChars(bool QualifyTypes = false)
{
if (isMain())
return "D main";
else
return Dsymbol.toPrettyChars(QualifyTypes);
}
/** for diagnostics, e.g. 'int foo(int x, int y) pure' */
final const(char)* toFullSignature()
{
OutBuffer buf;
functionToBufferWithIdent(type.toTypeFunction(), buf, toChars(), isStatic);
return buf.extractChars();
}
final bool isMain() const
{
return ident == Id.main && resolvedLinkage() != LINK.c && !isMember() && !isNested();
}
final bool isCMain() const
{
return ident == Id.main && resolvedLinkage() == LINK.c && !isMember() && !isNested();
}
final bool isWinMain() const
{
//printf("FuncDeclaration::isWinMain() %s\n", toChars());
version (none)
{
bool x = ident == Id.WinMain && resolvedLinkage() != LINK.c && !isMember();
printf("%s\n", x ? "yes" : "no");
return x;
}
else
{
return ident == Id.WinMain && resolvedLinkage() != LINK.c && !isMember();
}
}
final bool isDllMain() const
{
return ident == Id.DllMain && resolvedLinkage() != LINK.c && !isMember();
}
final bool isRtInit() const
{
return ident == Id.rt_init && resolvedLinkage() == LINK.c && !isMember() && !isNested();
}
override final bool isExport() const
{
return visibility.kind == Visibility.Kind.export_ || dllExport;
}
override final bool isImportedSymbol() const
{
//printf("isImportedSymbol()\n");
//printf("protection = %d\n", visibility);
return (visibility.kind == Visibility.Kind.export_ || dllImport) && !fbody;
}
override final bool isCodeseg() const pure nothrow @nogc @safe
{
return true; // functions are always in the code segment
}
override final bool isOverloadable() const
{
return true; // functions can be overloaded
}
/***********************************
* Override so it can work even if semantic() hasn't yet
* been run.
*/
override final bool isAbstract()
{
if (storage_class & STC.abstract_)
return true;
if (semanticRun >= PASS.semanticdone)
return false;
if (_scope)
{
if (_scope.stc & STC.abstract_)
return true;
parent = _scope.parent;
Dsymbol parent = toParent();
if (parent.isInterfaceDeclaration())
return true;
}
return false;
}
/*****************************************
* Initialize for inferring the attributes of this function.
*/
final void initInferAttributes()
{
//printf("initInferAttributes() for %s (%s)\n", toPrettyChars(), ident.toChars());
TypeFunction tf = type.toTypeFunction();
if (tf.purity == PURE.impure) // purity not specified
purityInprocess = true;
if (tf.trust == TRUST.default_)
safetyInprocess = true;
if (!tf.isnothrow)
nothrowInprocess = true;
if (!tf.isnogc)
nogcInprocess = true;
if (!isVirtual() || this.isIntroducing())
returnInprocess = true;
// Initialize for inferring STC.scope_
inferScope = true;
}
extern (D) final uint flags()
{
return bitFields;
}
extern (D) final uint flags(uint f)
{
bitFields = f;
return bitFields;
}
final bool isSafe()
{
if (safetyInprocess)
setUnsafe();
return type.toTypeFunction().trust == TRUST.safe;
}
extern (D) final bool isSafeBypassingInference()
{
return !(safetyInprocess) && isSafe();
}
final bool isTrusted()
{
if (safetyInprocess)
setUnsafe();
return type.toTypeFunction().trust == TRUST.trusted;
}
/**************************************
* The function is doing something unsafe, so mark it as unsafe.
*
* Params:
* gag = surpress error message (used in escape.d)
* loc = location of error
* fmt = printf-style format string
* arg0 = (optional) argument for first %s format specifier
* arg1 = (optional) argument for second %s format specifier
* arg2 = (optional) argument for third %s format specifier
* Returns: whether there's a safe error
*/
extern (D) final bool setUnsafe(
bool gag = false, Loc loc = Loc.init, const(char)* fmt = null,
RootObject arg0 = null, RootObject arg1 = null, RootObject arg2 = null)
{
if (safetyInprocess)
{
safetyInprocess = false;
type.toTypeFunction().trust = TRUST.system;
if (fmt || arg0)
safetyViolation = new AttributeViolation(loc, fmt, arg0, arg1, arg2);
if (fes)
fes.func.setUnsafe();
}
else if (isSafe())
{
if (!gag && fmt)
.error(loc, fmt, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
return true;
}
return false;
}
/**************************************
* The function is calling `@system` function `f`, so mark it as unsafe.
*
* Params:
* f = function being called (needed for diagnostic of inferred functions)
* Returns: whether there's a safe error
*/
extern (D) final bool setUnsafeCall(FuncDeclaration f)
{
return setUnsafe(false, f.loc, null, f, null);
}
final bool isNogc()
{
//printf("isNogc() %s, inprocess: %d\n", toChars(), !!(flags & FUNCFLAG.nogcInprocess));
if (nogcInprocess)
setGC(loc, null);
return type.toTypeFunction().isnogc;
}
extern (D) final bool isNogcBypassingInference()
{
return !nogcInprocess && isNogc();
}
/**************************************
* The function is doing something that may allocate with the GC,
* so mark it as not nogc (not no-how).
*
* Params:
* loc = location of impure action
* fmt = format string for error message. Must include "%s `%s`" for the function kind and name.
* arg0 = (optional) argument to format string
*
* Returns:
* true if function is marked as @nogc, meaning a user error occurred
*/
extern (D) final bool setGC(Loc loc, const(char)* fmt, RootObject arg0 = null)
{
//printf("setGC() %s\n", toChars());
if (nogcInprocess && semanticRun < PASS.semantic3 && _scope)
{
this.semantic2(_scope);
this.semantic3(_scope);
}
if (nogcInprocess)
{
nogcInprocess = false;
if (fmt)
nogcViolation = new AttributeViolation(loc, fmt, this, arg0); // action that requires GC
else if (arg0)
nogcViolation = new AttributeViolation(loc, fmt, arg0); // call to non-@nogc function
type.toTypeFunction().isnogc = false;
if (fes)
fes.func.setGC(Loc.init, null, null);
}
else if (isNogc())
return true;
return false;
}
/**************************************
* The function calls non-`@nogc` function f, mark it as not nogc.
* Params:
* f = function being called
* Returns:
* true if function is marked as @nogc, meaning a user error occurred
*/
extern (D) final bool setGCCall(FuncDeclaration f)
{
return setGC(loc, null, f);
}
/**************************************
* The function is doing something that may throw an exception, register that in case nothrow is being inferred
*
* Params:
* loc = location of action
* fmt = format string for error message
* arg0 = (optional) argument to format string
*/
extern (D) final void setThrow(Loc loc, const(char)* fmt, RootObject arg0 = null)
{
if (nothrowInprocess && !nothrowViolation)
{
nothrowViolation = new AttributeViolation(loc, fmt, arg0); // action that requires GC
}
}
/**************************************
* The function calls non-`nothrow` function f, register that in case nothrow is being inferred
* Params:
* loc = location of call
* f = function being called
*/
extern (D) final void setThrowCall(Loc loc, FuncDeclaration f)
{
return setThrow(loc, null, f);
}
extern (D) final void printGCUsage(const ref Loc loc, const(char)* warn)
{
if (!global.params.v.gc)
return;
Module m = getModule();
if (m && m.isRoot() && !inUnittest())
{
message(loc, "vgc: %s", warn);
}
}
/****************************************
* Determine if function needs a static frame pointer.
* Returns:
* `true` if function is really nested within other function.
* Contracts:
* If isNested() returns true, isThis() should return false,
* unless the function needs a dual-context pointer.
*/
bool isNested() const
{
auto f = toAliasFunc();
//printf("\ttoParent2() = '%s'\n", f.toParent2().toChars());
return ((f.storage_class & STC.static_) == 0) &&
(f._linkage == LINK.d) &&
(f.toParent2().isFuncDeclaration() !is null ||
f.toParent2() !is f.toParentLocal());
}
/****************************************
* Determine if function is a non-static member function
* that has an implicit 'this' expression.
* Returns:
* The aggregate it is a member of, or null.
* Contracts:
* Both isThis() and isNested() should return true if function needs a dual-context pointer,
* otherwise if isThis() returns true, isNested() should return false.
*/
override inout(AggregateDeclaration) isThis() inout
{
//printf("+FuncDeclaration::isThis() '%s'\n", toChars());
auto ad = (storage_class & STC.static_) ? .objc.isThis(this) : isMemberLocal();
//printf("-FuncDeclaration::isThis() %p\n", ad);
return ad;
}
override final bool needThis()
{
//printf("FuncDeclaration::needThis() '%s'\n", toChars());
return toAliasFunc().isThis() !is null;