Skip to content

Commit 55f979c

Browse files
authored
Merge pull request #5948 from hughbe/ast-msvc
Support building swift/AST with MSVC on Windows
2 parents ab0e944 + 7a5ef4b commit 55f979c

32 files changed

+191
-9
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ class AnyFunctionRef {
149149
}
150150
llvm_unreachable("unexpected AnyFunctionRef representation");
151151
}
152-
152+
153+
// Disable "only for use within the debugger" warning.
154+
#if defined(_MSC_VER)
155+
#pragma warning(push)
156+
#pragma warning(disable: 4996)
157+
#endif
153158
LLVM_ATTRIBUTE_DEPRECATED(void dump() const LLVM_ATTRIBUTE_USED,
154159
"only for use within the debugger") {
155160
if (auto afd = TheFunction.dyn_cast<AbstractFunctionDecl *>()) {
@@ -171,6 +176,9 @@ class AnyFunctionRef {
171176
llvm_unreachable("unexpected AnyFunctionRef representation");
172177
}
173178
};
179+
#if defined(_MSC_VER)
180+
#pragma warning(pop)
181+
#endif
174182

175183
} // namespace swift
176184

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,11 @@ class alignas(1 << DeclAlignInBits) Decl {
863863

864864
// Make vanilla new/delete illegal for Decls.
865865
void *operator new(size_t Bytes) = delete;
866+
867+
// Work around MSVC error: attempting to reference a deleted function.
868+
#if !defined(_MSC_VER) || defined(__clang__)
866869
void operator delete(void *Data) = delete;
870+
#endif
867871

868872
// Only allow allocation of Decls using the allocator in ASTContext
869873
// or by doing a placement new.

include/swift/AST/Expr.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,13 +662,27 @@ class TrailingCallArguments
662662
return *static_cast<const Derived *>(this);
663663
}
664664

665+
// Work around MSVC bug: can't infer llvm::trailing_objects_internal,
666+
// even though we granted friend access to it.
665667
size_t numTrailingObjects(
668+
#if defined(_MSC_VER) && !defined(__clang__)
669+
llvm::trailing_objects_internal::TrailingObjectsBase::OverloadToken<
670+
Identifier>) const {
671+
#else
666672
typename TrailingObjects::template OverloadToken<Identifier>) const {
673+
#endif
667674
return asDerived().getNumArguments();
668675
}
669676

677+
// Work around MSVC bug: can't infer llvm::trailing_objects_internal,
678+
// even though we granted friend access to it.
670679
size_t numTrailingObjects(
680+
#if defined(_MSC_VER) && !defined(__clang__)
681+
llvm::trailing_objects_internal::TrailingObjectsBase::OverloadToken<
682+
SourceLoc>) const {
683+
#else
671684
typename TrailingObjects::template OverloadToken<SourceLoc>) const {
685+
#endif
672686
return asDerived().hasArgumentLabelLocs()
673687
? asDerived().getNumArguments()
674688
: 0;
@@ -4447,6 +4461,8 @@ class ObjCSelectorExpr : public Expr {
44474461
case ObjCSelectorKind::Setter:
44484462
return true;
44494463
}
4464+
4465+
llvm_unreachable("Unhandled ObjcSelectorKind in switch.");
44504466
}
44514467

44524468
/// Whether this selector references a method.

include/swift/AST/GenericEnvironment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ class alignas(1 << DeclAlignInBits) GenericEnvironment final
183183

184184
/// Make vanilla new/delete illegal.
185185
void *operator new(size_t Bytes) = delete;
186+
#if !defined(_MSC_VER) || defined(__clang__)
186187
void operator delete(void *Data) = delete;
188+
#endif
187189

188190
/// Only allow placement new.
189191
void *operator new(size_t Bytes, void *Mem) {

include/swift/AST/Module.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,11 @@ class ModuleDecl : public TypeDecl, public DeclContext {
506506
private:
507507
// Make placement new and vanilla new/delete illegal for Modules.
508508
void *operator new(size_t Bytes) throw() = delete;
509+
510+
// Work around MSVC error: attempting to reference a deleted function.
511+
#if !defined(_MSC_VER) && !defined(__clang__)
509512
void operator delete(void *Data) throw() = delete;
513+
#endif
510514
void *operator new(size_t Bytes, void *Mem) throw() = delete;
511515
public:
512516
// Only allow allocation of Modules using the allocator in ASTContext
@@ -515,6 +519,8 @@ class ModuleDecl : public TypeDecl, public DeclContext {
515519
unsigned Alignment = alignof(ModuleDecl));
516520
};
517521

522+
static inline unsigned alignOfFileUnit();
523+
518524
/// A container for module-scope declarations that itself provides a scope; the
519525
/// smallest unit of code organization.
520526
///
@@ -741,8 +747,12 @@ class FileUnit : public DeclContext {
741747
// Only allow allocation of FileUnits using the allocator in ASTContext
742748
// or by doing a placement new.
743749
void *operator new(size_t Bytes, ASTContext &C,
744-
unsigned Alignment = alignof(FileUnit));
750+
unsigned Alignment = alignOfFileUnit());
745751
};
752+
753+
static inline unsigned alignOfFileUnit() {
754+
return alignof(FileUnit&);
755+
}
746756

747757
/// A file containing Swift source code.
748758
///

include/swift/AST/ProtocolConformance.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
265265

266266
// Make vanilla new/delete illegal for protocol conformances.
267267
void *operator new(size_t bytes) = delete;
268+
269+
// Work around MSVC error: attempting to reference a deleted function.
270+
#if !defined(_MSC_VER) || defined(__clang__)
268271
void operator delete(void *data) = delete;
272+
#endif
269273

270274
// Only allow allocation of protocol conformances using the allocator in
271275
// ASTContext or by doing a placement new.

include/swift/AST/TypeMatcher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class TypeMatcher {
6969
cast<CLASS##Type>(secondType.getPointer()));
7070
#include "swift/AST/TypeNodes.def"
7171
}
72+
73+
llvm_unreachable("Unhandled TypeKind in switch.");
7274
}
7375

7476
/// Honeypot to catch cases where we should redispatch the second type, but

include/swift/AST/TypeRepr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ enum class TypeReprKind : uint8_t {
4242

4343
/// \brief Representation of a type as written in source.
4444
class alignas(8) TypeRepr {
45+
// Fix MSVC error: attempting to reference a deleted function.
46+
#if !defined(_MSC_VER) || defined(__clang__)
4547
TypeRepr(const TypeRepr&) = delete;
48+
#endif
4649
void operator=(const TypeRepr&) = delete;
4750

4851
class TypeReprBitfields {

include/swift/AST/Types.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ class ParameterTypeFlags {
13811381
NumBits = 3
13821382
};
13831383
OptionSet<ParameterFlags> value;
1384-
static_assert(NumBits < 8*sizeof(value), "overflowed");
1384+
static_assert(NumBits < 8*sizeof(OptionSet<ParameterFlags>), "overflowed");
13851385

13861386
ParameterTypeFlags(OptionSet<ParameterFlags, uint8_t> val) : value(val) {}
13871387

@@ -2255,6 +2255,8 @@ inline bool canBeCalledIndirectly(SILFunctionTypeRepresentation rep) {
22552255
case SILFunctionTypeRepresentation::WitnessMethod:
22562256
return true;
22572257
}
2258+
2259+
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
22582260
}
22592261

22602262
/// Map a SIL function representation to the base language calling convention
@@ -2273,6 +2275,8 @@ getSILFunctionLanguage(SILFunctionTypeRepresentation rep) {
22732275
case SILFunctionTypeRepresentation::Closure:
22742276
return SILFunctionLanguage::Swift;
22752277
}
2278+
2279+
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
22762280
}
22772281

22782282
/// AnyFunctionType - A function type has a single input and result, but
@@ -2357,6 +2361,8 @@ class AnyFunctionType : public TypeBase {
23572361
case SILFunctionTypeRepresentation::WitnessMethod:
23582362
return true;
23592363
}
2364+
2365+
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
23602366
}
23612367

23622368
/// True if the function representation carries context.
@@ -2373,6 +2379,8 @@ class AnyFunctionType : public TypeBase {
23732379
case SILFunctionTypeRepresentation::Closure:
23742380
return false;
23752381
}
2382+
2383+
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
23762384
}
23772385

23782386
// Note that we don't have setters. That is by design, use
@@ -3043,6 +3051,8 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30433051
case Representation::WitnessMethod:
30443052
return true;
30453053
}
3054+
3055+
llvm_unreachable("Unhandled Representation in switch.");
30463056
}
30473057

30483058
bool hasGuaranteedSelfParam() const {
@@ -3058,6 +3068,8 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30583068
case Representation::WitnessMethod:
30593069
return true;
30603070
}
3071+
3072+
llvm_unreachable("Unhandled Representation in switch.");
30613073
}
30623074

30633075
/// True if the function representation carries context.
@@ -3074,6 +3086,8 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30743086
case Representation::Closure:
30753087
return false;
30763088
}
3089+
3090+
llvm_unreachable("Unhandled Representation in switch.");
30773091
}
30783092

30793093
// Note that we don't have setters. That is by design, use

lib/AST/ASTContext.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,16 @@ struct ASTContext::Implementation {
275275
~Arena() {
276276
for (auto &conformance : SpecializedConformances)
277277
conformance.~SpecializedProtocolConformance();
278+
// Work around MSVC warning: local variable is initialized but
279+
// not referenced.
280+
#if defined(_MSC_VER)
281+
#pragma warning (disable: 4189)
282+
#endif
278283
for (auto &conformance : InheritedConformances)
279284
conformance.~InheritedProtocolConformance();
285+
#if defined(_MSC_VER)
286+
#pragma warning (default: 4189)
287+
#endif
280288

281289
// Call the normal conformance destructors last since they could be
282290
// referenced by the other conformance types.
@@ -536,6 +544,8 @@ EnumDecl *ASTContext::getOptionalDecl(OptionalTypeKind kind) const {
536544
case OTK_Optional:
537545
return getOptionalDecl();
538546
}
547+
548+
llvm_unreachable("Unhandled OptionalTypeKind in switch.");
539549
}
540550

541551
static EnumElementDecl *findEnumElement(EnumDecl *e, Identifier name) {
@@ -1714,6 +1724,8 @@ std::pair<unsigned, DeclName> swift::getObjCMethodDiagInfo(
17141724
// Normal method.
17151725
return { 4, func->getFullName() };
17161726
}
1727+
1728+
llvm_unreachable("Unhandled AccessorKind in switch.");
17171729
}
17181730

17191731
bool swift::fixDeclarationName(InFlightDiagnostic &diag, ValueDecl *decl,
@@ -3760,6 +3772,8 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
37603772
case ForeignLanguage::ObjectiveC:
37613773
return entry;
37623774
}
3775+
3776+
llvm_unreachable("Unhandled ForeignLanguage in switch.");
37633777
}
37643778

37653779
bool ASTContext::isTypeBridgedInExternalModule(

lib/AST/ASTMangler.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
#include "llvm/Support/CommandLine.h"
3737

3838
using namespace swift;
39-
using namespace NewMangling;
39+
using namespace swift::NewMangling;
4040

4141
std::string NewMangling::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
4242
if (useNewMangling()) {
43-
ASTMangler::ASTMangler NewMangler(/* DWARF */ true);
43+
ASTMangler NewMangler(/* DWARF */ true);
4444
return NewMangler.mangleTypeForDebugger(Ty, DC);
4545
}
4646
Mangle::Mangler OldMangler(/* DWARF */ true);
@@ -50,7 +50,7 @@ std::string NewMangling::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
5050

5151
std::string NewMangling::mangleTypeAsUSR(Type Ty) {
5252
if (useNewMangling()) {
53-
ASTMangler::ASTMangler NewMangler;
53+
ASTMangler NewMangler;
5454
return NewMangler.mangleTypeAsUSR(Ty);
5555
}
5656
Mangle::Mangler OldMangler;
@@ -369,7 +369,7 @@ void ASTMangler::appendDeclName(const ValueDecl *decl) {
369369
assert(!discriminator.empty());
370370
assert(!isNonAscii(discriminator.str()) &&
371371
"discriminator contains non-ASCII characters");
372-
(void)isNonAscii;
372+
(void)&isNonAscii;
373373
assert(!clang::isDigit(discriminator.str().front()) &&
374374
"not a valid identifier");
375375

@@ -387,6 +387,8 @@ static const char *getMetatypeRepresentationOp(MetatypeRepresentation Rep) {
387387
case MetatypeRepresentation::ObjC:
388388
return "o";
389389
}
390+
391+
llvm_unreachable("Unhandled MetatypeRepresentation in switch.");
390392
}
391393

392394
static bool isStdlibType(const NominalTypeDecl *decl) {

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ std::string ASTPrinter::sanitizeUtf8(StringRef Text) {
477477
Builder.reserve(Text.size());
478478
const UTF8* Data = reinterpret_cast<const UTF8*>(Text.begin());
479479
const UTF8* End = reinterpret_cast<const UTF8*>(Text.end());
480-
StringRef Replacement = "\ufffd";
480+
StringRef Replacement = u8"\ufffd";
481481
while (Data < End) {
482482
auto Step = getNumBytesForUTF8(*Data);
483483
if (Data + Step > End) {
@@ -612,6 +612,8 @@ static bool escapeKeywordInContext(StringRef keyword, PrintNameContext context){
612612
case PrintNameContext::TupleElement:
613613
return !canBeArgumentLabel(keyword);
614614
}
615+
616+
llvm_unreachable("Unhandled PrintNameContext in switch.");
615617
}
616618

617619
void ASTPrinter::printName(Identifier Name, PrintNameContext Context) {

0 commit comments

Comments
 (0)