forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeclContext.cpp
820 lines (703 loc) · 25.3 KB
/
DeclContext.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
//===--- DeclContext.cpp - DeclContext implementation ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/AST.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Types.h"
#include "swift/Basic/SourceManager.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
#define DEBUG_TYPE "Name lookup"
STATISTIC(NumLazyIterableDeclContexts,
"# of serialized iterable declaration contexts");
STATISTIC(NumUnloadedLazyIterableDeclContexts,
"# of serialized iterable declaration contexts never loaded");
// Only allow allocation of DeclContext using the allocator in ASTContext.
void *DeclContext::operator new(size_t Bytes, ASTContext &C,
unsigned Alignment) {
return C.Allocate(Bytes, Alignment);
}
ASTContext &DeclContext::getASTContext() const {
return getParentModule()->getASTContext();
}
NominalTypeDecl *
DeclContext::isNominalTypeOrNominalTypeExtensionContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return nullptr;
case DeclContextKind::ExtensionDecl: {
auto ED = cast<ExtensionDecl>(this);
auto type = ED->getExtendedType();
if (type.isNull() || type->is<ErrorType>())
return nullptr;
if (auto ND = type->getNominalOrBoundGenericNominal())
return ND;
if (auto unbound = dyn_cast<UnboundGenericType>(type.getPointer())) {
return unbound->getDecl();
}
return nullptr;
}
case DeclContextKind::NominalTypeDecl:
return const_cast<NominalTypeDecl *>(cast<NominalTypeDecl>(this));
}
}
ClassDecl *DeclContext::isClassOrClassExtensionContext() const {
return dyn_cast_or_null<ClassDecl>(
isNominalTypeOrNominalTypeExtensionContext());
}
EnumDecl *DeclContext::isEnumOrEnumExtensionContext() const {
return dyn_cast_or_null<EnumDecl>(
isNominalTypeOrNominalTypeExtensionContext());
}
ProtocolDecl *DeclContext::isProtocolOrProtocolExtensionContext() const {
return dyn_cast_or_null<ProtocolDecl>(
isNominalTypeOrNominalTypeExtensionContext());
}
ProtocolDecl *DeclContext::isProtocolExtensionContext() const {
if (getContextKind() != DeclContextKind::ExtensionDecl)
return nullptr;
return dyn_cast_or_null<ProtocolDecl>(
isNominalTypeOrNominalTypeExtensionContext());
}
GenericTypeParamDecl *DeclContext::getProtocolSelf() const {
assert(isProtocolOrProtocolExtensionContext() && "not a protocol");
return getGenericParamsOfContext()->getParams().front();
}
Type DeclContext::getDeclaredTypeOfContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return Type();
case DeclContextKind::ExtensionDecl: {
auto ED = cast<ExtensionDecl>(this);
auto type = ED->getExtendedType();
if (type.isNull() || type->is<ErrorType>()) {
return Type();
}
if (isa<UnboundGenericType>(type.getPointer())) {
getASTContext().getLazyResolver()->resolveExtension(
const_cast<ExtensionDecl *>(ED));
type = ED->getExtendedType();
}
if (auto ND = type->getNominalOrBoundGenericNominal())
return ND->getDeclaredType();
return Type();
}
case DeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this)->getDeclaredType();
}
llvm_unreachable("bad DeclContextKind");
}
Type DeclContext::getDeclaredTypeInContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return Type();
case DeclContextKind::ExtensionDecl: {
auto ED = cast<ExtensionDecl>(this);
auto type = ED->getExtendedType();
if (type.isNull() || type->is<ErrorType>()) {
return ErrorType::get(getASTContext());
}
if (isa<UnboundGenericType>(type.getPointer())) {
getASTContext().getLazyResolver()->resolveExtension(
const_cast<ExtensionDecl *>(ED));
type = ED->getExtendedType();
}
return type;
}
case DeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this)->getDeclaredTypeInContext();
}
llvm_unreachable("bad DeclContextKind");
}
Type DeclContext::getDeclaredInterfaceType() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return Type();
case DeclContextKind::ExtensionDecl:
// FIXME: Need a sugar-preserving getExtendedInterfaceType for extensions
if (auto nominal = getDeclaredTypeOfContext()->getAnyNominal())
return nominal->getDeclaredInterfaceType();
return Type();
case DeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this)->getDeclaredInterfaceType();
}
llvm_unreachable("bad DeclContextKind");
}
GenericParamList *DeclContext::getGenericParamsOfContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::TopLevelCodeDecl:
return nullptr;
case DeclContextKind::SerializedLocal:
case DeclContextKind::Initializer:
case DeclContextKind::AbstractClosureExpr:
// Closures and initializers can't themselves be generic, but they
// can occur in generic contexts.
return getParent()->getGenericParamsOfContext();
case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(this);
if (auto GP = AFD->getGenericParams())
return GP;
// If we're within a type context, pick up the generic signature
// of that context.
if (AFD->getDeclContext()->isTypeContext())
return AFD->getDeclContext()->getGenericParamsOfContext();
return nullptr;
}
case DeclContextKind::NominalTypeDecl: {
auto nominal = cast<NominalTypeDecl>(this);
if (auto gp = nominal->getGenericParams())
return gp;
if (nominal->getDeclContext()->isTypeContext())
return nominal->getDeclContext()->getGenericParamsOfContext();
return nullptr;
}
case DeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this)->getGenericParams();
}
llvm_unreachable("bad DeclContextKind");
}
GenericSignature *DeclContext::getGenericSignatureOfContext() const {
switch (getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return nullptr;
case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(this);
if (auto GFT = AFD->getInterfaceType()->getAs<GenericFunctionType>())
return GFT->getGenericSignature();
// If we're within a type context, pick up the generic signature
// of that context.
if (AFD->getDeclContext()->isTypeContext())
return AFD->getDeclContext()->getGenericSignatureOfContext();
return nullptr;
}
case DeclContextKind::NominalTypeDecl: {
auto nominal = cast<NominalTypeDecl>(this);
if (auto genericSig = nominal->getGenericSignature())
return genericSig;
// If we're within a type context, pick up the generic signature
// of that context.
if (nominal->getDeclContext()->isTypeContext())
return nominal->getDeclContext()->getGenericSignatureOfContext();
return nullptr;
}
case DeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this)->getGenericSignature();
}
llvm_unreachable("bad DeclContextKind");
}
DeclContext *DeclContext::getLocalContext() {
if (isLocalContext())
return this;
if (isModuleContext() || isExtensionContext())
return nullptr;
return getParent()->getLocalContext();
}
AbstractFunctionDecl *DeclContext::getInnermostMethodContext() {
DeclContext *result = this;
while (true) {
switch (result->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
// Look through closures, initial values.
result = result->getParent();
continue;
case DeclContextKind::AbstractFunctionDecl: {
// If this function is a method, we found our result.
auto func = dyn_cast<AbstractFunctionDecl>(result);
if (func->getDeclContext()->isTypeContext())
return func;
// This function isn't a method; look through it.
result = func->getDeclContext();
continue;
}
case DeclContextKind::ExtensionDecl:
case DeclContextKind::FileUnit:
case DeclContextKind::Module:
case DeclContextKind::NominalTypeDecl:
case DeclContextKind::TopLevelCodeDecl:
// Not in a method context.
return nullptr;
}
}
}
DeclContext *DeclContext::getInnermostTypeContext() {
DeclContext *Result = this;
while (true) {
switch (Result->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::Initializer:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SerializedLocal:
Result = Result->getParent();
continue;
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
return nullptr;
case DeclContextKind::ExtensionDecl:
case DeclContextKind::NominalTypeDecl:
return Result;
}
}
}
Decl *DeclContext::getInnermostDeclarationDeclContext() {
DeclContext *DC = this;
while (DC) {
switch (DC->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
break;
case DeclContextKind::TopLevelCodeDecl:
return cast<TopLevelCodeDecl>(DC);
case DeclContextKind::AbstractFunctionDecl:
return cast<AbstractFunctionDecl>(DC);
case DeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(DC);
case DeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(DC);
}
DC = DC->getParent();
}
return nullptr;
}
Module *DeclContext::getParentModule() const {
const DeclContext *DC = this;
while (!DC->isModuleContext())
DC = DC->getParent();
return const_cast<Module *>(cast<Module>(DC));
}
SourceFile *DeclContext::getParentSourceFile() const {
const DeclContext *DC = this;
while (!DC->isModuleScopeContext())
DC = DC->getParent();
return const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
}
DeclContext *DeclContext::getModuleScopeContext() const {
const DeclContext *DC = this;
while (true) {
switch (DC->getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
return const_cast<DeclContext*>(DC);
default:
break;
}
DC = DC->getParent();
}
}
/// Determine whether the given context is generic at any level.
bool DeclContext::isGenericContext() const {
for (const DeclContext *dc = this; ; dc = dc->getParent() ) {
switch (dc->getContextKind()) {
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
return false;
case DeclContextKind::Initializer:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::SerializedLocal:
// Check parent context.
continue;
case DeclContextKind::AbstractFunctionDecl:
if (cast<AbstractFunctionDecl>(dc)->getGenericParams())
return true;
continue;
case DeclContextKind::TopLevelCodeDecl:
// Check parent context.
continue;
case DeclContextKind::NominalTypeDecl:
if (cast<NominalTypeDecl>(dc)->getGenericParams())
return true;
continue;
case DeclContextKind::ExtensionDecl:
if (cast<ExtensionDecl>(dc)->getGenericParams())
return true;
continue;
}
llvm_unreachable("bad decl context kind");
}
llvm_unreachable("illegal declcontext hierarchy");
}
/// Determine whether the innermost context is generic.
bool DeclContext::isInnermostContextGeneric() const {
switch (getContextKind()) {
case DeclContextKind::AbstractFunctionDecl:
return (cast<AbstractFunctionDecl>(this)->getGenericParams() != nullptr);
case DeclContextKind::ExtensionDecl:
return (cast<ExtensionDecl>(this)->getGenericParams() != nullptr);
case DeclContextKind::NominalTypeDecl:
return (cast<NominalTypeDecl>(this)->getGenericParams() != nullptr);
default:
return false;
}
llvm_unreachable("bad DeclContextKind");
}
bool
DeclContext::isCascadingContextForLookup(bool functionsAreNonCascading) const {
// FIXME: This is explicitly checking for attributes in some cases because
// it can be called before accessibility is computed.
switch (getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
break;
case DeclContextKind::SerializedLocal:
llvm_unreachable("should not perform lookups in deserialized contexts");
case DeclContextKind::Initializer:
// Default arguments still require a type.
if (isa<DefaultArgumentInitializer>(this))
return false;
break;
case DeclContextKind::TopLevelCodeDecl:
// FIXME: Pattern initializers at top-level scope end up here.
return true;
case DeclContextKind::AbstractFunctionDecl: {
if (functionsAreNonCascading)
return false;
auto *AFD = cast<AbstractFunctionDecl>(this);
if (AFD->hasAccessibility())
return AFD->getFormalAccess() > Accessibility::Private;
break;
}
case DeclContextKind::Module:
case DeclContextKind::FileUnit:
return true;
case DeclContextKind::NominalTypeDecl: {
auto *nominal = cast<NominalTypeDecl>(this);
if (nominal->hasAccessibility())
return nominal->getFormalAccess() > Accessibility::Private;
break;
}
case DeclContextKind::ExtensionDecl: {
auto *extension = cast<ExtensionDecl>(this);
if (extension->hasDefaultAccessibility())
return extension->getDefaultAccessibility() > Accessibility::Private;
// FIXME: duplicated from computeDefaultAccessibility in TypeCheckDecl.cpp.
if (auto *AA = extension->getAttrs().getAttribute<AccessibilityAttr>())
return AA->getAccess() > Accessibility::Private;
if (Type extendedTy = extension->getExtendedType()) {
// Need to check if extendedTy is ErrorType
if (extendedTy->getAnyNominal())
return extendedTy->getAnyNominal()->isCascadingContextForLookup(true);
}
break;
}
}
return getParent()->isCascadingContextForLookup(true);
}
bool DeclContext::walkContext(ASTWalker &Walker) {
switch (getContextKind()) {
case DeclContextKind::Module:
return cast<Module>(this)->walk(Walker);
case DeclContextKind::FileUnit:
return cast<FileUnit>(this)->walk(Walker);
case DeclContextKind::AbstractClosureExpr:
return cast<AbstractClosureExpr>(this)->walk(Walker);
case DeclContextKind::NominalTypeDecl:
return cast<NominalTypeDecl>(this)->walk(Walker);
case DeclContextKind::ExtensionDecl:
return cast<ExtensionDecl>(this)->walk(Walker);
case DeclContextKind::TopLevelCodeDecl:
return cast<TopLevelCodeDecl>(this)->walk(Walker);
case DeclContextKind::AbstractFunctionDecl:
return cast<AbstractFunctionDecl>(this)->walk(Walker);
case DeclContextKind::SerializedLocal:
llvm_unreachable("walk is unimplemented for deserialized contexts");
case DeclContextKind::Initializer:
// Is there any point in trying to walk the expression?
return false;
}
llvm_unreachable("bad DeclContextKind");
}
void DeclContext::dumpContext() const {
printContext(llvm::outs());
}
template <typename DCType>
static unsigned getLineNumber(DCType *DC) {
SourceLoc loc = DC->getLoc();
if (loc.isInvalid())
return 0;
const ASTContext &ctx = static_cast<const DeclContext *>(DC)->getASTContext();
return ctx.SourceMgr.getLineAndColumn(loc).first;
}
bool DeclContext::classof(const Decl *D) {
switch (D->getKind()) { //
#define DECL(ID, PARENT) case DeclKind::ID: return false;
#define CONTEXT_DECL(ID, PARENT) case DeclKind::ID: return true;
#define CONTEXT_VALUE_DECL(ID, PARENT) case DeclKind::ID: return true;
#include "swift/AST/DeclNodes.def"
}
}
DeclContext *DeclContext::castDeclToDeclContext(const Decl *D) {
switch (D->getKind()) {
#define DECL(ID, PARENT) \
case DeclKind::ID: llvm_unreachable("not a decl context");
#define CONTEXT_DECL(ID, PARENT) \
case DeclKind::ID: \
return const_cast<DeclContext *>( \
static_cast<const DeclContext*>(cast<ID##Decl>(D)));
#define CONTEXT_VALUE_DECL(ID, PARENT) CONTEXT_DECL(ID, PARENT)
#include "swift/AST/DeclNodes.def"
}
}
unsigned DeclContext::printContext(raw_ostream &OS, unsigned indent) const {
unsigned Depth = 0;
if (auto *P = getParent())
Depth = P->printContext(OS, indent);
const char *Kind;
switch (getContextKind()) {
case DeclContextKind::Module: Kind = "Module"; break;
case DeclContextKind::FileUnit: Kind = "FileUnit"; break;
case DeclContextKind::SerializedLocal: Kind = "Serialized Local"; break;
case DeclContextKind::AbstractClosureExpr:
Kind = "AbstractClosureExpr";
break;
case DeclContextKind::NominalTypeDecl:
switch (cast<NominalTypeDecl>(this)->getKind()) {
#define NOMINAL_TYPE_DECL(ID, PARENT) \
case DeclKind::ID: Kind = #ID "Decl"; break;
#define DECL(ID, PARENT) \
case DeclKind::ID: llvm_unreachable("not a nominal type");
#include "swift/AST/DeclNodes.def"
}
break;
case DeclContextKind::ExtensionDecl: Kind = "ExtensionDecl"; break;
case DeclContextKind::TopLevelCodeDecl: Kind = "TopLevelCodeDecl"; break;
case DeclContextKind::Initializer: Kind = "Initializer"; break;
case DeclContextKind::AbstractFunctionDecl:
Kind = "AbstractFunctionDecl";
break;
}
OS.indent(Depth*2 + indent) << "0x" << (void*)this << " " << Kind;
switch (getContextKind()) {
case DeclContextKind::Module:
OS << " name=" << cast<Module>(this)->getName();
break;
case DeclContextKind::FileUnit:
switch (cast<FileUnit>(this)->getKind()) {
case FileUnitKind::Builtin:
OS << " Builtin";
break;
case FileUnitKind::Derived:
OS << " derived";
break;
case FileUnitKind::Source:
OS << " file=\"" << cast<SourceFile>(this)->getFilename() << "\"";
break;
case FileUnitKind::SerializedAST:
case FileUnitKind::ClangModule:
OS << " file=\"" << cast<LoadedFile>(this)->getFilename() << "\"";
break;
}
break;
case DeclContextKind::AbstractClosureExpr:
OS << " line=" << getLineNumber(cast<AbstractClosureExpr>(this));
OS << " : " << cast<AbstractClosureExpr>(this)->getType();
break;
case DeclContextKind::NominalTypeDecl:
OS << " name=" << cast<NominalTypeDecl>(this)->getName();
break;
case DeclContextKind::ExtensionDecl:
OS << " line=" << getLineNumber(cast<ExtensionDecl>(this));
OS << " base=" << cast<ExtensionDecl>(this)->getExtendedType();
break;
case DeclContextKind::TopLevelCodeDecl:
OS << " line=" << getLineNumber(cast<TopLevelCodeDecl>(this));
break;
case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(this);
OS << " name=" << AFD->getName();
if (AFD->hasType())
OS << " : " << AFD->getType();
else
OS << " : (no type set)";
break;
}
case DeclContextKind::Initializer:
switch (cast<Initializer>(this)->getInitializerKind()) {
case InitializerKind::PatternBinding: {
auto init = cast<PatternBindingInitializer>(this);
OS << " PatternBinding 0x" << (void*) init->getBinding();
break;
}
case InitializerKind::DefaultArgument: {
auto init = cast<DefaultArgumentInitializer>(this);
OS << " DefaultArgument index=" << init->getIndex();
break;
}
}
break;
case DeclContextKind::SerializedLocal: {
auto local = cast<SerializedLocalDeclContext>(this);
switch (local->getLocalDeclContextKind()) {
case LocalDeclContextKind::AbstractClosure: {
auto serializedClosure = cast<SerializedAbstractClosureExpr>(local);
OS << " closure : " << serializedClosure->getType();
break;
}
case LocalDeclContextKind::DefaultArgumentInitializer: {
auto init = cast<SerializedDefaultArgumentInitializer>(local);
OS << "DefaultArgument index=" << init->getIndex();
break;
}
case LocalDeclContextKind::PatternBindingInitializer: {
auto init = cast<SerializedPatternBindingInitializer>(local);
OS << " PatternBinding 0x" << (void*) init->getBinding();
break;
}
case LocalDeclContextKind::TopLevelCodeDecl:
OS << " TopLevelCode";
break;
}
}
}
OS << "\n";
return Depth + 1;
}
DeclRange IterableDeclContext::getMembers() const {
loadAllMembers();
return DeclRange(FirstDecl, nullptr);
}
/// Add a member to this context.
void IterableDeclContext::addMember(Decl *member, Decl *Hint) {
// Add the member to the list of declarations without notification.
addMemberSilently(member, Hint);
// Notify our parent declaration that we have added the member, which can
// be used to update the lookup tables.
// FIXME: If only we had the notion of a "searchable" declaration
// context...
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl: {
auto nominal = cast<NominalTypeDecl>(this);
nominal->addedMember(member);
assert(member->getDeclContext() == nominal &&
"Added member to the wrong context");
break;
}
case IterableDeclContextKind::ExtensionDecl: {
auto ext = cast<ExtensionDecl>(this);
ext->addedMember(member);
assert(member->getDeclContext() == ext &&
"Added member to the wrong context");
break;
}
}
}
void IterableDeclContext::addMemberSilently(Decl *member, Decl *hint) const {
assert(!member->NextDecl && "Already added to a container");
// If there is a hint decl that specifies where to add this, just
// link into the chain immediately following it.
if (hint) {
member->NextDecl = hint->NextDecl;
hint->NextDecl = member;
// If the hint was the last in the parent context's chain, update it.
if (LastDeclAndKind.getPointer() == hint)
LastDeclAndKind.setPointer(member);
return;
}
if (auto last = LastDeclAndKind.getPointer()) {
last->NextDecl = member;
} else {
FirstDecl = member;
}
LastDeclAndKind.setPointer(member);
}
void IterableDeclContext::setLoader(LazyMemberLoader *loader,
uint64_t contextData) {
LazyLoader = loader;
LazyLoaderContextData = contextData;
++NumLazyIterableDeclContexts;
++NumUnloadedLazyIterableDeclContexts;
}
void IterableDeclContext::loadAllMembers() const {
if (!isLazy())
return;
// Don't try to load all members re-entrant-ly.
auto resolver = getLoader();
auto contextData = getLoaderContextData();
LazyLoader = nullptr;
const Decl *container = nullptr;
switch (getIterableContextKind()) {
case IterableDeclContextKind::NominalTypeDecl:
container = cast<NominalTypeDecl>(this);
break;
case IterableDeclContextKind::ExtensionDecl:
container = cast<ExtensionDecl>(this);
break;
}
bool hasMissingRequiredMembers = false;
resolver->loadAllMembers(const_cast< Decl *>(container), contextData,
&hasMissingRequiredMembers);
if (hasMissingRequiredMembers)
if (auto proto = dyn_cast<ProtocolDecl>(this))
const_cast<ProtocolDecl *>(proto)->setHasMissingRequirements(true);
--NumUnloadedLazyIterableDeclContexts;
}
bool IterableDeclContext::classof(const Decl *D) {
switch (D->getKind()) {
#define DECL(ID, PARENT) case DeclKind::ID: return false;
#define NOMINAL_TYPE_DECL(ID, PARENT) case DeclKind::ID: return true;
#define EXTENSION_DECL(ID, PARENT) case DeclKind::ID: return true;
#include "swift/AST/DeclNodes.def"
}
}
IterableDeclContext *
IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
switch (D->getKind()) {
#define DECL(ID, PARENT) \
case DeclKind::ID: llvm_unreachable("not a decl context");
#define NOMINAL_TYPE_DECL(ID, PARENT) \
case DeclKind::ID: \
return const_cast<IterableDeclContext *>( \
static_cast<const IterableDeclContext*>(cast<ID##Decl>(D)));
#define EXTENSION_DECL(ID, PARENT) \
case DeclKind::ID: \
return const_cast<IterableDeclContext *>( \
static_cast<const IterableDeclContext*>(cast<ID##Decl>(D)));
#include "swift/AST/DeclNodes.def"
}
}