-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTypeCheckRequestFunctions.cpp
153 lines (132 loc) · 5.42 KB
/
TypeCheckRequestFunctions.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
//===--- TypeCheckRequests.cpp - Type Checking Requests ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "GenericTypeResolver.h"
#include "TypeChecker.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/TypeLoc.h"
#include "swift/AST/Types.h"
#include "swift/Subsystems.h"
using namespace swift;
Type InheritedTypeRequest::evaluate(
Evaluator &evaluator,
llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl,
unsigned index) const {
// Figure out how to resolve types.
TypeResolutionOptions options;
DeclContext *dc;
if (auto typeDecl = decl.dyn_cast<TypeDecl *>()) {
if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) {
dc = nominal;
options |= TypeResolutionFlags::GenericSignature;
options |= TypeResolutionFlags::InheritanceClause;
options |= TypeResolutionFlags::AllowUnavailableProtocol;
} else {
dc = typeDecl->getDeclContext();
if (isa<GenericTypeParamDecl>(typeDecl)) {
// For generic parameters, we want name lookup to look at just the
// signature of the enclosing entity.
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {
dc = nominal;
options |= TypeResolutionFlags::GenericSignature;
} else if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
dc = ext;
options |= TypeResolutionFlags::GenericSignature;
} else if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
dc = func;
options |= TypeResolutionFlags::GenericSignature;
} else if (!dc->isModuleScopeContext()) {
// Skip the generic parameter's context entirely.
dc = dc->getParent();
}
}
}
} else {
auto ext = decl.get<ExtensionDecl *>();
dc = ext;
options |= TypeResolutionFlags::GenericSignature;
options |= TypeResolutionFlags::InheritanceClause;
options |= TypeResolutionFlags::AllowUnavailableProtocol;
}
ProtocolRequirementTypeResolver protoResolver;
GenericTypeToArchetypeResolver archetypeResolver(dc);
GenericTypeResolver *resolver;
if (isa<ProtocolDecl>(dc)) {
resolver = &protoResolver;
} else {
resolver = &archetypeResolver;
}
// FIXME: Hack for calls through here when we have no type checker.
auto lazyResolver = dc->getASTContext().getLazyResolver();
if (!lazyResolver) return ErrorType::get(dc->getASTContext());
TypeChecker &tc = *static_cast<TypeChecker *>(lazyResolver);
TypeLoc &typeLoc = getTypeLoc(decl, index);
Type inheritedType =
tc.resolveType(typeLoc.getTypeRepr(), dc, options, resolver);
if (inheritedType && !isa<ProtocolDecl>(dc))
inheritedType = inheritedType->mapTypeOutOfContext();
return inheritedType ? inheritedType : ErrorType::get(tc.Context);
}
Type SuperclassTypeRequest::evaluate(Evaluator &evaluator,
NominalTypeDecl *nominalDecl) const {
assert(isa<ClassDecl>(nominalDecl) || isa<ProtocolDecl>(nominalDecl));
for (unsigned int idx : indices(nominalDecl->getInherited())) {
Type inheritedType = evaluator(InheritedTypeRequest{nominalDecl, idx});
if (!inheritedType) continue;
// If we found a class, return it.
if (inheritedType->getClassOrBoundGenericClass()) {
if (inheritedType->hasArchetype())
return inheritedType->mapTypeOutOfContext();
return inheritedType;
}
// If we found an existential with a superclass bound, return it.
if (inheritedType->isExistentialType()) {
if (auto superclassType =
inheritedType->getExistentialLayout().explicitSuperclass) {
if (superclassType->getClassOrBoundGenericClass()) {
if (superclassType->hasArchetype())
return superclassType->mapTypeOutOfContext();
return superclassType;
}
}
}
}
// No superclass.
return Type();
}
Type EnumRawTypeRequest::evaluate(Evaluator &evaluator,
EnumDecl *enumDecl) const {
for (unsigned int idx : indices(enumDecl->getInherited())) {
Type inheritedType = evaluator(InheritedTypeRequest{enumDecl, idx});
if (!inheritedType) continue;
// Skip existential types.
if (inheritedType->isExistentialType()) continue;
// We found a raw type; return it.
if (inheritedType->hasArchetype())
return inheritedType->mapTypeOutOfContext();
return inheritedType;
}
// No raw type.
return Type();
}
// Define request evaluation functions for each of the type checker requests.
static AbstractRequestFunction *typeCheckerRequestFunctions[] = {
#define SWIFT_TYPEID(Name) \
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
#include "swift/AST/TypeCheckerTypeIDZone.def"
#undef SWIFT_TYPEID
};
void swift::registerTypeCheckerRequestFunctions(Evaluator &evaluator) {
evaluator.registerRequestFunctions(SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE,
typeCheckerRequestFunctions);
}