Skip to content

Commit b2c31c3

Browse files
committed
[unittests/Sema] Add an ability to retrieve stdlib types by name
1 parent 0b22d91 commit b2c31c3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

unittests/Sema/SemaFixture.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "SemaFixture.h"
14+
#include "swift/AST/Decl.h"
1415
#include "swift/AST/Module.h"
1516
#include "swift/AST/ParseRequests.h"
1617
#include "swift/AST/SourceFile.h"
18+
#include "swift/AST/Type.h"
19+
#include "swift/AST/Types.h"
1720
#include "swift/Basic/LLVMInitialize.h"
1821
#include "swift/ClangImporter/ClangImporter.h"
1922
#include "swift/Serialization/SerializedModuleLoader.h"
@@ -52,3 +55,23 @@ SemaTest::SemaTest()
5255

5356
DC = module;
5457
}
58+
59+
Type SemaTest::getStdlibType(StringRef name) const {
60+
auto typeName = Context.getIdentifier(name);
61+
62+
auto *stdlib = Context.getStdlibModule();
63+
64+
llvm::SmallVector<ValueDecl *, 4> results;
65+
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results);
66+
67+
if (results.size() != 1)
68+
return Type();
69+
70+
if (auto *decl = dyn_cast<TypeDecl>(results.front())) {
71+
if (auto *NTD = dyn_cast<NominalTypeDecl>(decl))
72+
return NTD->getDeclaredType();
73+
return decl->getDeclaredInterfaceType();
74+
}
75+
76+
return Type();
77+
}

unittests/Sema/SemaFixture.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "swift/AST/DiagnosticEngine.h"
1515
#include "swift/AST/Module.h"
1616
#include "swift/AST/SourceFile.h"
17+
#include "swift/AST/Type.h"
1718
#include "swift/Basic/LangOptions.h"
1819
#include "swift/Basic/Platform.h"
1920
#include "swift/Basic/SourceManager.h"
@@ -58,6 +59,9 @@ class SemaTest : public SemaTestBase {
5859
DeclContext *DC;
5960

6061
SemaTest();
62+
63+
protected:
64+
Type getStdlibType(StringRef name) const;
6165
};
6266

6367
} // end namespace unittest

0 commit comments

Comments
 (0)