Skip to content

Commit 56dfb08

Browse files
committed
Port swift/basic to Windows
1 parent af6dc69 commit 56dfb08

File tree

6 files changed

+96
-15
lines changed

6 files changed

+96
-15
lines changed

include/swift/Basic/EncodedSequence.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,20 @@ class EncodedSequence : public EncodedSequenceBase {
337337
/// see the documentation there for information about how to use this
338338
/// data structure.
339339
template <class ValueType> class Map {
340+
// Hack: MSVC isn't able to resolve the InlineKeyCapacity part of the
341+
// template of PrefixMap, so we have to split it up and pass it manually.
342+
#if defined(_MSC_VER)
343+
static const size_t Size = (sizeof(void*) - 1) / sizeof(Chunk);
344+
static const size_t ActualSize = max<size_t>(Size, 1);
345+
346+
using MapBase = PrefixMap<Chunk, ValueType, ActualSize>;
347+
#else
340348
using MapBase = PrefixMap<Chunk, ValueType>;
349+
#endif
341350
MapBase TheMap;
342351

343352
public:
344-
using SequenceIterator = EncodedSequence::iterator;
353+
using SequenceIterator = typename EncodedSequence::iterator;
345354
using KeyType = typename MapBase::KeyType;
346355
using Handle = typename MapBase::Handle;
347356

include/swift/Basic/ImmutablePointerSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ template <typename T> class ImmutablePointerSet : public llvm::FoldingSetNode {
112112
return *LowerBound == Ptr;
113113
}
114114

115-
using iterator = typename decltype(Data)::iterator;
115+
using iterator = typename ArrayRef<PtrTy>::iterator;
116116
iterator begin() const { return Data.begin(); }
117117
iterator end() const { return Data.end(); }
118118

include/swift/Basic/type_traits.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace swift {
2929
/// is not intended to be specialized.
3030
template<typename T>
3131
struct IsTriviallyCopyable {
32-
#if _LIBCPP_VERSION
32+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
3333
// libc++ implements it.
3434
static const bool value = std::is_trivially_copyable<T>::value;
3535
#elif __has_feature(is_trivially_copyable)
@@ -41,7 +41,7 @@ struct IsTriviallyCopyable {
4141

4242
template<typename T>
4343
struct IsTriviallyConstructible {
44-
#if _LIBCPP_VERSION
44+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
4545
// libc++ implements it.
4646
static const bool value = std::is_trivially_constructible<T>::value;
4747
#elif __has_feature(has_trivial_constructor)
@@ -53,7 +53,7 @@ struct IsTriviallyConstructible {
5353

5454
template<typename T>
5555
struct IsTriviallyDestructible {
56-
#if _LIBCPP_VERSION
56+
#if _LIBCPP_VERSION || (defined(_MSC_VER) && !defined(__clang__))
5757
// libc++ implements it.
5858
static const bool value = std::is_trivially_destructible<T>::value;
5959
#elif __has_feature(has_trivial_destructor)

lib/Basic/SourceLoc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void SourceManager::verifyAllBuffers() const {
2424
};
2525

2626
// FIXME: This depends on the buffer IDs chosen by llvm::SourceMgr.
27-
__attribute__((used)) static char arbitraryTotal = 0;
27+
LLVM_ATTRIBUTE_USED static char arbitraryTotal = 0;
2828
for (unsigned i = 1, e = LLVMSourceMgr.getNumBuffers(); i <= e; ++i) {
2929
auto *buffer = LLVMSourceMgr.getMemoryBuffer(i);
3030
if (buffer->getBufferSize() == 0)

lib/Basic/UUID.cpp

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,105 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18-
#include <uuid/uuid.h>
1918
#include "swift/Basic/UUID.h"
2019

20+
// WIN32 doesn't natively support <uuid/uuid.h>. Instead, we use Win32 APIs.
21+
#if defined(_WIN32)
22+
#define WIN32_LEAN_AND_MEAN
23+
#include <rpc.h>
24+
#include <string>
25+
#else
26+
#include <uuid/uuid.h>
27+
#endif
28+
2129
using namespace swift;
2230

23-
UUID::UUID(FromRandom_t) {
31+
swift::UUID::UUID(FromRandom_t) {
32+
#if defined(_WIN32)
33+
::UUID uuid;
34+
UuidCreate(&uuid);
35+
36+
memcpy(Value, &uuid, Size);
37+
#else
2438
uuid_generate_random(Value);
39+
#endif
2540
}
2641

27-
UUID::UUID(FromTime_t) {
42+
swift::UUID::UUID(FromTime_t) {
43+
#if defined(_WIN32)
44+
::UUID uuid;
45+
UuidCreate(&uuid);
46+
47+
memcpy(Value, &uuid, Size);
48+
#else
2849
uuid_generate_time(Value);
50+
#endif
2951
}
3052

31-
UUID::UUID() {
53+
swift::UUID::UUID() {
54+
#if defined(_WIN32)
55+
::UUID uuid = *((::UUID *)&Value);
56+
UuidCreateNil(&uuid);
57+
58+
memcpy(Value, &uuid, Size);
59+
#else
3260
uuid_clear(Value);
61+
#endif
3362
}
3463

35-
Optional<UUID> UUID::fromString(const char *s) {
36-
UUID result;
64+
Optional<swift::UUID> swift::UUID::fromString(const char *s) {
65+
#if defined(_WIN32)
66+
RPC_CSTR t = const_cast<RPC_CSTR>(reinterpret_cast<const unsigned char*>(s));
67+
68+
::UUID uuid;
69+
RPC_STATUS status = UuidFromStringA(t, &uuid);
70+
if (status == RPC_S_INVALID_STRING_UUID) {
71+
return None;
72+
}
73+
74+
swift::UUID result = UUID();
75+
memcpy(result.Value, &uuid, Size);
76+
return result;
77+
#else
78+
swift::UUID result;
3779
if (uuid_parse(s, result.Value))
3880
return None;
3981
return result;
82+
#endif
4083
}
4184

42-
void UUID::toString(llvm::SmallVectorImpl<char> &out) const {
85+
void swift::UUID::toString(llvm::SmallVectorImpl<char> &out) const {
4386
out.resize(UUID::StringBufferSize);
87+
#if defined(_WIN32)
88+
::UUID uuid;
89+
memcpy(&uuid, Value, Size);
90+
91+
RPC_CSTR str;
92+
UuidToStringA(&uuid, &str);
93+
94+
char* signedStr = reinterpret_cast<char*>(str);
95+
memcpy(out.data(), signedStr, StringBufferSize);
96+
#else
4497
uuid_unparse_upper(Value, out.data());
98+
#endif
4599
// Pop off the null terminator.
46100
assert(out.back() == '\0' && "did not null-terminate?!");
47101
out.pop_back();
48102
}
49103

50-
int UUID::compare(UUID y) const {
104+
int swift::UUID::compare(UUID y) const {
105+
#if defined(_WIN32)
106+
RPC_STATUS s;
107+
::UUID uuid1;
108+
memcpy(&uuid1, Value, Size);
109+
110+
::UUID uuid2;
111+
memcpy(&uuid2, y.Value, Size);
112+
113+
return UuidCompare(&uuid1, &uuid2, &s);
114+
#else
51115
return uuid_compare(Value, y.Value);
116+
#endif
52117
}
53118

54119
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os, UUID uuid) {

lib/Basic/Version.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
SWIFT_MAKE_VERSION_STRING(SWIFT_VERSION_MAJOR, SWIFT_VERSION_MINOR)
4444
#endif
4545

46+
// MSVC doesn't support __has_include
47+
#if defined(_MSC_VER)
48+
# include "LLVMRevision.inc"
49+
# include "ClangRevision.inc"
50+
# include "SwiftRevision.inc"
51+
#else
4652
#if __has_include("LLVMRevision.inc")
4753
# include "LLVMRevision.inc"
4854
#endif
@@ -52,6 +58,7 @@
5258
#if __has_include("SwiftRevision.inc")
5359
# include "SwiftRevision.inc"
5460
#endif
61+
#endif
5562

5663
namespace swift {
5764
namespace version {
@@ -401,7 +408,7 @@ std::string getSwiftFullVersion(Version effectiveVersion) {
401408
#endif
402409

403410
// Suppress unused function warning
404-
(void) printFullRevisionString;
411+
(void)&printFullRevisionString;
405412

406413
return OS.str();
407414
}

0 commit comments

Comments
 (0)