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
|
//===--- SemaFixture.cpp - Helper for setting up Sema context --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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 "SemaFixture.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Import.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParseRequests.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Subsystems.h"
#include "llvm/ADT/DenseMap.h"
using namespace swift;
using namespace swift::unittest;
using namespace swift::constraints::inference;
SemaTest::SemaTest()
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SILOpts,
SearchPathOpts, ClangImporterOpts,
SymbolGraphOpts, CASOpts, SourceMgr, Diags)) {
INITIALIZE_LLVM();
registerParseRequestFunctions(Context.evaluator);
registerTypeCheckerRequestFunctions(Context.evaluator);
registerClangImporterRequestFunctions(Context.evaluator);
Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context));
Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true);
auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true);
assert(stdlib && "Failed to load standard library");
auto *module =
ModuleDecl::create(Context.getIdentifier("SemaTests"), Context);
MainFile = new (Context) SourceFile(*module, SourceFileKind::Main,
/*buffer=*/std::nullopt);
AttributedImport<ImportedModule> stdlibImport{{ImportPath::Access(), stdlib},
/*options=*/{}};
MainFile->setImports(stdlibImport);
module->addFile(*MainFile);
DC = module;
}
Type SemaTest::getStdlibType(StringRef name) const {
auto typeName = Context.getIdentifier(name);
auto *stdlib = Context.getStdlibModule();
llvm::SmallVector<ValueDecl *, 4> results;
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results);
if (results.size() != 1)
return Type();
if (auto *decl = dyn_cast<TypeDecl>(results.front())) {
if (auto *NTD = dyn_cast<NominalTypeDecl>(decl))
return NTD->getDeclaredType();
return decl->getDeclaredInterfaceType();
}
return Type();
}
NominalTypeDecl *SemaTest::getStdlibNominalTypeDecl(StringRef name) const {
auto typeName = Context.getIdentifier(name);
auto *stdlib = Context.getStdlibModule();
llvm::SmallVector<ValueDecl *, 4> results;
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results);
if (results.size() != 1)
return nullptr;
return dyn_cast<NominalTypeDecl>(results.front());
}
VarDecl *SemaTest::addExtensionVarMember(NominalTypeDecl *decl,
StringRef name, Type type) const {
auto *ext = ExtensionDecl::create(Context, SourceLoc(), nullptr, { }, DC,
nullptr);
decl->addExtension(ext);
ext->setExtendedNominal(decl);
auto *VD = new (Context) VarDecl(/*isStatic=*/ true, VarDecl::Introducer::Var,
/*nameLoc=*/ SourceLoc(),
Context.getIdentifier(name), ext);
ext->addMember(VD);
auto *pat = new (Context) NamedPattern(VD);
VD->setNamingPattern(pat);
pat->setType(type);
return VD;
}
ProtocolType *SemaTest::createProtocol(llvm::StringRef protocolName,
Type parent) {
auto *PD = new (Context)
ProtocolDecl(DC, SourceLoc(), SourceLoc(),
Context.getIdentifier(protocolName),
/*PrimaryAssociatedTypeNames=*/{},
/*Inherited=*/{},
/*trailingWhere=*/nullptr);
PD->setImplicit();
return ProtocolType::get(PD, parent, Context);
}
BindingSet SemaTest::inferBindings(ConstraintSystem &cs,
TypeVariableType *typeVar) {
llvm::SmallDenseMap<TypeVariableType *, BindingSet> cache;
for (auto *typeVar : cs.getTypeVariables()) {
if (!typeVar->getImpl().hasRepresentativeOrFixed())
cache.insert({typeVar, cs.getBindingsFor(typeVar, /*finalize=*/false)});
}
for (auto *typeVar : cs.getTypeVariables()) {
auto cachedBindings = cache.find(typeVar);
if (cachedBindings == cache.end())
continue;
auto &bindings = cachedBindings->getSecond();
bindings.inferTransitiveProtocolRequirements(cache);
bindings.finalize(cache);
}
auto result = cache.find(typeVar);
assert(result != cache.end());
return result->second;
}
|