|
| 1 | +//===--- TypeCheckRequests.cpp - Type Checking Requests ------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#include "GenericTypeResolver.h" |
| 13 | +#include "TypeChecker.h" |
| 14 | +#include "swift/AST/TypeCheckRequests.h" |
| 15 | +#include "swift/AST/Decl.h" |
| 16 | +#include "swift/AST/ExistentialLayout.h" |
| 17 | +#include "swift/AST/TypeLoc.h" |
| 18 | +#include "swift/AST/Types.h" |
| 19 | +#include "swift/Subsystems.h" |
| 20 | + |
| 21 | +using namespace swift; |
| 22 | + |
| 23 | +Type InheritedTypeRequest::evaluate( |
| 24 | + Evaluator &evaluator, |
| 25 | + llvm::PointerUnion<TypeDecl *, ExtensionDecl *> decl, |
| 26 | + unsigned index) const { |
| 27 | + // Figure out how to resolve types. |
| 28 | + TypeResolutionOptions options; |
| 29 | + DeclContext *dc; |
| 30 | + if (auto typeDecl = decl.dyn_cast<TypeDecl *>()) { |
| 31 | + if (auto nominal = dyn_cast<NominalTypeDecl>(typeDecl)) { |
| 32 | + dc = nominal; |
| 33 | + options |= TypeResolutionFlags::GenericSignature; |
| 34 | + options |= TypeResolutionFlags::InheritanceClause; |
| 35 | + options |= TypeResolutionFlags::AllowUnavailableProtocol; |
| 36 | + } else { |
| 37 | + dc = typeDecl->getDeclContext(); |
| 38 | + |
| 39 | + if (isa<GenericTypeParamDecl>(typeDecl)) { |
| 40 | + // For generic parameters, we want name lookup to look at just the |
| 41 | + // signature of the enclosing entity. |
| 42 | + if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) { |
| 43 | + dc = nominal; |
| 44 | + options |= TypeResolutionFlags::GenericSignature; |
| 45 | + } else if (auto ext = dyn_cast<ExtensionDecl>(dc)) { |
| 46 | + dc = ext; |
| 47 | + options |= TypeResolutionFlags::GenericSignature; |
| 48 | + } else if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) { |
| 49 | + dc = func; |
| 50 | + options |= TypeResolutionFlags::GenericSignature; |
| 51 | + } else if (!dc->isModuleScopeContext()) { |
| 52 | + // Skip the generic parameter's context entirely. |
| 53 | + dc = dc->getParent(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } else { |
| 58 | + auto ext = decl.get<ExtensionDecl *>(); |
| 59 | + dc = ext; |
| 60 | + options |= TypeResolutionFlags::GenericSignature; |
| 61 | + options |= TypeResolutionFlags::InheritanceClause; |
| 62 | + options |= TypeResolutionFlags::AllowUnavailableProtocol; |
| 63 | + } |
| 64 | + |
| 65 | + ProtocolRequirementTypeResolver protoResolver; |
| 66 | + GenericTypeToArchetypeResolver archetypeResolver(dc); |
| 67 | + GenericTypeResolver *resolver; |
| 68 | + if (isa<ProtocolDecl>(dc)) { |
| 69 | + resolver = &protoResolver; |
| 70 | + } else { |
| 71 | + resolver = &archetypeResolver; |
| 72 | + } |
| 73 | + |
| 74 | + // FIXME: Hack for calls through here when we have no type checker. |
| 75 | + auto lazyResolver = dc->getASTContext().getLazyResolver(); |
| 76 | + if (!lazyResolver) return ErrorType::get(dc->getASTContext()); |
| 77 | + |
| 78 | + TypeChecker &tc = *static_cast<TypeChecker *>(lazyResolver); |
| 79 | + TypeLoc &typeLoc = getTypeLoc(decl, index); |
| 80 | + |
| 81 | + Type inheritedType = |
| 82 | + tc.resolveType(typeLoc.getTypeRepr(), dc, options, resolver); |
| 83 | + if (inheritedType && !isa<ProtocolDecl>(dc)) |
| 84 | + inheritedType = inheritedType->mapTypeOutOfContext(); |
| 85 | + return inheritedType ? inheritedType : ErrorType::get(tc.Context); |
| 86 | +} |
| 87 | + |
| 88 | +Type SuperclassTypeRequest::evaluate(Evaluator &evaluator, |
| 89 | + NominalTypeDecl *nominalDecl) const { |
| 90 | + assert(isa<ClassDecl>(nominalDecl) || isa<ProtocolDecl>(nominalDecl)); |
| 91 | + |
| 92 | + for (unsigned int idx : indices(nominalDecl->getInherited())) { |
| 93 | + Type inheritedType = evaluator(InheritedTypeRequest{nominalDecl, idx}); |
| 94 | + if (!inheritedType) continue; |
| 95 | + |
| 96 | + // If we found a class, return it. |
| 97 | + if (inheritedType->getClassOrBoundGenericClass()) { |
| 98 | + if (inheritedType->hasArchetype()) |
| 99 | + return inheritedType->mapTypeOutOfContext(); |
| 100 | + |
| 101 | + return inheritedType; |
| 102 | + } |
| 103 | + |
| 104 | + // If we found an existential with a superclass bound, return it. |
| 105 | + if (inheritedType->isExistentialType()) { |
| 106 | + if (auto superclassType = |
| 107 | + inheritedType->getExistentialLayout().superclass) { |
| 108 | + if (superclassType->getClassOrBoundGenericClass()) { |
| 109 | + if (superclassType->hasArchetype()) |
| 110 | + return superclassType->mapTypeOutOfContext(); |
| 111 | + |
| 112 | + return superclassType; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // No superclass. |
| 119 | + return Type(); |
| 120 | +} |
| 121 | + |
| 122 | +Type EnumRawTypeRequest::evaluate(Evaluator &evaluator, |
| 123 | + EnumDecl *enumDecl) const { |
| 124 | + for (unsigned int idx : indices(enumDecl->getInherited())) { |
| 125 | + Type inheritedType = evaluator(InheritedTypeRequest{enumDecl, idx}); |
| 126 | + if (!inheritedType) continue; |
| 127 | + |
| 128 | + // Skip existential types. |
| 129 | + if (inheritedType->isExistentialType()) continue; |
| 130 | + |
| 131 | + // We found a raw type; return it. |
| 132 | + if (inheritedType->hasArchetype()) |
| 133 | + return inheritedType->mapTypeOutOfContext(); |
| 134 | + |
| 135 | + return inheritedType; |
| 136 | + } |
| 137 | + |
| 138 | + // No raw type. |
| 139 | + return Type(); |
| 140 | +} |
| 141 | + |
| 142 | +// Define request evaluation functions for each of the type checker requests. |
| 143 | +static AbstractRequestFunction *typeCheckerRequestFunctions[] = { |
| 144 | +#define SWIFT_TYPEID(Name) \ |
| 145 | + reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest), |
| 146 | +#include "swift/AST/TypeCheckerTypeIDZone.def" |
| 147 | +#undef SWIFT_TYPEID |
| 148 | +}; |
| 149 | + |
| 150 | +void swift::registerTypeCheckerRequestFunctions(Evaluator &evaluator) { |
| 151 | + evaluator.registerRequestFunctions(SWIFT_TYPE_CHECKER_REQUESTS_TYPEID_ZONE, |
| 152 | + typeCheckerRequestFunctions); |
| 153 | +} |
| 154 | + |
0 commit comments