|
| 1 | +//===--- SemaFixture.cpp - Helper for setting up Sema context --------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 | + |
| 13 | +#include "SemaFixture.h" |
| 14 | +#include "swift/AST/Decl.h" |
| 15 | +#include "swift/AST/Import.h" |
| 16 | +#include "swift/AST/Module.h" |
| 17 | +#include "swift/AST/ParseRequests.h" |
| 18 | +#include "swift/AST/SourceFile.h" |
| 19 | +#include "swift/AST/Type.h" |
| 20 | +#include "swift/AST/Types.h" |
| 21 | +#include "swift/Basic/LLVMInitialize.h" |
| 22 | +#include "swift/ClangImporter/ClangImporter.h" |
| 23 | +#include "swift/Serialization/SerializedModuleLoader.h" |
| 24 | +#include "swift/Subsystems.h" |
| 25 | + |
| 26 | +using namespace swift; |
| 27 | +using namespace swift::unittest; |
| 28 | + |
| 29 | +SemaTest::SemaTest() |
| 30 | + : Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts, |
| 31 | + ClangImporterOpts, SourceMgr, Diags)) { |
| 32 | + INITIALIZE_LLVM(); |
| 33 | + |
| 34 | + registerParseRequestFunctions(Context.evaluator); |
| 35 | + registerTypeCheckerRequestFunctions(Context.evaluator); |
| 36 | + |
| 37 | + Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context)); |
| 38 | + Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true); |
| 39 | + |
| 40 | + auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true); |
| 41 | + assert(stdlib && "Failed to load standard library"); |
| 42 | + |
| 43 | + auto *module = |
| 44 | + ModuleDecl::create(Context.getIdentifier("SemaTests"), Context); |
| 45 | + |
| 46 | + MainFile = new (Context) SourceFile(*module, SourceFileKind::Main, |
| 47 | + /*buffer=*/None); |
| 48 | + |
| 49 | + AttributedImport<ImportedModule> stdlibImport{{ImportPath::Access(), stdlib}, |
| 50 | + /*options=*/{}}; |
| 51 | + |
| 52 | + MainFile->setImports(stdlibImport); |
| 53 | + module->addFile(*MainFile); |
| 54 | + |
| 55 | + DC = module; |
| 56 | +} |
| 57 | + |
| 58 | +Type SemaTest::getStdlibType(StringRef name) const { |
| 59 | + auto typeName = Context.getIdentifier(name); |
| 60 | + |
| 61 | + auto *stdlib = Context.getStdlibModule(); |
| 62 | + |
| 63 | + llvm::SmallVector<ValueDecl *, 4> results; |
| 64 | + stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results); |
| 65 | + |
| 66 | + if (results.size() != 1) |
| 67 | + return Type(); |
| 68 | + |
| 69 | + if (auto *decl = dyn_cast<TypeDecl>(results.front())) { |
| 70 | + if (auto *NTD = dyn_cast<NominalTypeDecl>(decl)) |
| 71 | + return NTD->getDeclaredType(); |
| 72 | + return decl->getDeclaredInterfaceType(); |
| 73 | + } |
| 74 | + |
| 75 | + return Type(); |
| 76 | +} |
0 commit comments