-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTestContext.cpp
52 lines (45 loc) · 2.17 KB
/
TestContext.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
//===--- TestContext.cpp - Helper for setting up ASTContexts --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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 "TestContext.h"
#include "swift/AST/Module.h"
#include "swift/Strings.h"
using namespace swift;
using namespace swift::unittest;
static void declareOptionalType(ASTContext &ctx, SourceFile *fileForLookups,
Identifier name) {
auto wrapped = new (ctx) GenericTypeParamDecl(fileForLookups,
ctx.getIdentifier("Wrapped"),
SourceLoc(), /*depth*/0,
/*index*/0);
auto params = GenericParamList::create(ctx, SourceLoc(), wrapped,
SourceLoc());
auto decl = new (ctx) EnumDecl(SourceLoc(), name, SourceLoc(),
/*inherited*/{}, params, fileForLookups);
wrapped->setDeclContext(decl);
fileForLookups->Decls.push_back(decl);
}
TestContext::TestContext(ShouldDeclareOptionalTypes optionals)
: Ctx(LangOpts, SearchPathOpts, SourceMgr, Diags) {
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
auto *module = ModuleDecl::create(stdlibID, Ctx);
Ctx.LoadedModules[stdlibID] = module;
using ImplicitModuleImportKind = SourceFile::ImplicitModuleImportKind;
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
/*buffer*/None,
ImplicitModuleImportKind::None);
module->addFile(*FileForLookups);
if (optionals == DeclareOptionalTypes) {
declareOptionalType(Ctx, FileForLookups, Ctx.getIdentifier("Optional"));
declareOptionalType(Ctx, FileForLookups,
Ctx.getIdentifier("ImplicitlyUnwrappedOptional"));
}
}