Skip to content

Commit 3b8360d

Browse files
Merge pull request #5198 from swiftwasm/main
[pull] swiftwasm from main
2 parents 0bb04ac + f2302b9 commit 3b8360d

File tree

70 files changed

+745
-515
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+745
-515
lines changed

include/swift/AST/Attr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,10 @@ class CustomAttr final : public DeclAttribute {
17021702
bool isArgUnsafe() const;
17031703
void setArgIsUnsafe(bool unsafe) { isArgUnsafeBit = unsafe; }
17041704

1705+
/// Whether this custom attribute is a macro attached to the given
1706+
/// declaration.
1707+
bool isAttachedMacro(const Decl *decl) const;
1708+
17051709
Expr *getSemanticInit() const { return semanticInit; }
17061710
void setSemanticInit(Expr *expr) { semanticInit = expr; }
17071711

lib/AST/Attr.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/IndexSubset.h"
2424
#include "swift/AST/LazyResolver.h"
2525
#include "swift/AST/Module.h"
26+
#include "swift/AST/NameLookupRequests.h"
2627
#include "swift/AST/ParameterList.h"
2728
#include "swift/AST/TypeCheckRequests.h"
2829
#include "swift/AST/TypeRepr.h"
@@ -2336,6 +2337,21 @@ bool CustomAttr::isArgUnsafe() const {
23362337
return isArgUnsafeBit;
23372338
}
23382339

2340+
bool CustomAttr::isAttachedMacro(const Decl *decl) const {
2341+
auto &ctx = decl->getASTContext();
2342+
auto *dc = decl->getInnermostDeclContext();
2343+
2344+
auto attrDecl = evaluateOrDefault(
2345+
ctx.evaluator,
2346+
CustomAttrDeclRequest{const_cast<CustomAttr *>(this), dc},
2347+
nullptr);
2348+
2349+
if (!attrDecl)
2350+
return false;
2351+
2352+
return attrDecl.dyn_cast<MacroDecl *>();
2353+
}
2354+
23392355
DeclarationAttr::DeclarationAttr(SourceLoc atLoc, SourceRange range,
23402356
MacroRole role,
23412357
ArrayRef<MacroIntroducedDeclName> peerNames,

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
#include "ClangSyntaxPrinter.h"
1414
#include "swift/ABI/MetadataValues.h"
15+
#include "swift/AST/ASTContext.h"
1516
#include "swift/AST/Decl.h"
1617
#include "swift/AST/Module.h"
1718
#include "swift/AST/SwiftNameTranslation.h"
19+
#include "swift/AST/TypeCheckRequests.h"
1820
#include "clang/AST/ASTContext.h"
1921
#include "clang/AST/DeclTemplate.h"
2022
#include "clang/AST/NestedNameSpecifier.h"
@@ -140,18 +142,21 @@ void ClangSyntaxPrinter::printModuleNamespaceStart(
140142
os << "namespace ";
141143
printBaseName(&moduleContext);
142144
os << " __attribute__((swift_private))";
145+
printSymbolUSRAttribute(&moduleContext);
143146
os << " {\n";
144147
}
145148

146149
/// Print a C++ namespace declaration with the give name and body.
147150
void ClangSyntaxPrinter::printNamespace(
148151
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
149152
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
150-
NamespaceTrivia trivia) const {
153+
NamespaceTrivia trivia, const ModuleDecl *moduleContext) const {
151154
os << "namespace ";
152155
namePrinter(os);
153156
if (trivia == NamespaceTrivia::AttributeSwiftPrivate)
154157
os << " __attribute__((swift_private))";
158+
if (moduleContext)
159+
printSymbolUSRAttribute(moduleContext);
155160
os << " {\n\n";
156161
bodyPrinter(os);
157162
os << "\n} // namespace ";
@@ -405,3 +410,17 @@ void ClangSyntaxPrinter::printIgnoredCxx17ExtensionDiagnosticBlock(
405410
llvm::function_ref<void()> bodyPrinter) {
406411
printIgnoredDiagnosticBlock("c++17-extensions", bodyPrinter);
407412
}
413+
414+
void ClangSyntaxPrinter::printSymbolUSRAttribute(const ValueDecl *D) const {
415+
if (isa<ModuleDecl>(D)) {
416+
os << " SWIFT_SYMBOL_MODULE(\"";
417+
printBaseName(D);
418+
os << "\")";
419+
return;
420+
}
421+
auto result = evaluateOrDefault(D->getASTContext().evaluator,
422+
USRGenerationRequest{D}, std::string());
423+
if (result.empty())
424+
return;
425+
os << " SWIFT_SYMBOL(\"" << result << "\")";
426+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class ClangSyntaxPrinter {
125125
/// Print a C++ namespace declaration with the give name and body.
126126
void printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,
127127
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
128-
NamespaceTrivia trivia = NamespaceTrivia::None) const;
128+
NamespaceTrivia trivia = NamespaceTrivia::None,
129+
const ModuleDecl *moduleContext = nullptr) const;
129130

130131
void printNamespace(StringRef name,
131132
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
@@ -220,6 +221,10 @@ class ClangSyntaxPrinter {
220221
void printIgnoredCxx17ExtensionDiagnosticBlock(
221222
llvm::function_ref<void()> bodyPrinter);
222223

224+
/// Print the macro that applies Clang's `external_source_symbol` attribute
225+
/// on the generated declaration.
226+
void printSymbolUSRAttribute(const ValueDecl *D) const;
227+
223228
protected:
224229
raw_ostream &os;
225230
};

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ class DeclAndTypePrinter::Implementation
731731
}
732732
os << " } ";
733733
syntaxPrinter.printIdentifier(caseName);
734+
if (elementDecl)
735+
syntaxPrinter.printSymbolUSRAttribute(elementDecl);
734736
os << ";\n";
735737
};
736738

@@ -742,6 +744,7 @@ class DeclAndTypePrinter::Implementation
742744
[&](const auto &pair) {
743745
os << "\n ";
744746
syntaxPrinter.printIdentifier(pair.first->getNameStr());
747+
syntaxPrinter.printSymbolUSRAttribute(pair.first);
745748
},
746749
",");
747750
// TODO: allow custom name for this special case
@@ -1413,16 +1416,15 @@ class DeclAndTypePrinter::Implementation
14131416
owningPrinter.interopContext, owningPrinter);
14141417
DeclAndTypeClangFunctionPrinter::FunctionSignatureModifiers modifiers;
14151418
modifiers.isInline = true;
1419+
// FIXME: Support throwing exceptions for Swift errors.
1420+
modifiers.isNoexcept = !funcTy->isThrowing();
14161421
auto result = funcPrinter.printFunctionSignature(
14171422
FD, funcABI.getSignature(), cxx_translation::getNameForCxx(FD),
14181423
resultTy,
14191424
DeclAndTypeClangFunctionPrinter::FunctionSignatureKind::CxxInlineThunk,
14201425
modifiers);
14211426
assert(
14221427
!result.isUnsupported()); // The C signature should be unsupported too.
1423-
// FIXME: Support throwing exceptions for Swift errors.
1424-
if (!funcTy->isThrowing())
1425-
os << " noexcept";
14261428
printFunctionClangAttributes(FD, funcTy);
14271429
printAvailability(FD);
14281430
os << " {\n";

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,14 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
788788
llvm::raw_string_ostream prologueOS{modulePrologueBuf};
789789
EmittedClangHeaderDependencyInfo info;
790790

791+
// Define the `SWIFT_SYMBOL` macro.
792+
os << "#ifdef SWIFT_SYMBOL\n";
793+
os << "#undef SWIFT_SYMBOL\n";
794+
os << "#endif\n";
795+
os << "#define SWIFT_SYMBOL(usrValue) SWIFT_SYMBOL_MODULE_USR(\"";
796+
ClangSyntaxPrinter(os).printBaseName(&M);
797+
os << "\", usrValue)\n";
798+
791799
// FIXME: Use getRequiredAccess once @expose is supported.
792800
ModuleWriter writer(moduleOS, prologueOS, info.imports, M, interopContext,
793801
AccessLevel::Public, requiresExposedAttribute,
@@ -824,6 +832,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
824832
os << "namespace ";
825833
M.ValueDecl::getName().print(os);
826834
os << " __attribute__((swift_private))";
835+
ClangSyntaxPrinter(os).printSymbolUSRAttribute(&M);
827836
os << " {\n";
828837
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
829838
os << "extern \"C\" {\n";
@@ -842,10 +851,11 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
842851
ClangSyntaxPrinter(os).printNamespace(
843852
[&](raw_ostream &os) { M.ValueDecl::getName().print(os); },
844853
[&](raw_ostream &os) { os << moduleOS.str(); },
845-
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate);
854+
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);
846855

847856
if (M.isStdlibModule()) {
848857
os << "#pragma clang diagnostic pop\n";
849858
}
859+
os << "#undef SWIFT_SYMBOL\n";
850860
return info;
851861
}

lib/PrintAsClang/PrintClangClassType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void ClangClassTypePrinter::printClassTypeDecl(
5858
baseClassQualifiedName = "swift::_impl::RefCountedClass";
5959
}
6060

61-
os << "class ";
61+
os << "class";
62+
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
63+
os << ' ';
6264
printer.printBaseName(typeDecl);
6365
if (typeDecl->isFinal())
6466
os << " final";

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,11 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
903903
os << ')';
904904
if (modifiers.isConst)
905905
os << " const";
906+
if (modifiers.isNoexcept)
907+
os << " noexcept";
908+
if (modifiers.hasSymbolUSR)
909+
ClangSyntaxPrinter(os).printSymbolUSRAttribute(
910+
modifiers.symbolUSROverride ? modifiers.symbolUSROverride : FD);
906911
return resultingRepresentation;
907912
}
908913

@@ -1309,6 +1314,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxMethod(
13091314
isa<FuncDecl>(FD) ? cast<FuncDecl>(FD)->isMutating() : false;
13101315
modifiers.isConst =
13111316
!isa<ClassDecl>(typeDeclContext) && !isMutating && !isConstructor;
1317+
modifiers.hasSymbolUSR = !isDefinition;
13121318
auto result = printFunctionSignature(
13131319
FD, signature,
13141320
isConstructor ? getConstructorName(FD)
@@ -1373,6 +1379,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
13731379
modifiers.isInline = true;
13741380
modifiers.isConst =
13751381
!isStatic && accessor->isGetter() && !isa<ClassDecl>(typeDeclContext);
1382+
modifiers.hasSymbolUSR = !isDefinition;
1383+
modifiers.symbolUSROverride = accessor->getStorage();
13761384
auto result = printFunctionSignature(
13771385
accessor, signature, remapPropertyName(accessor, resultTy), resultTy,
13781386
FunctionSignatureKind::CxxInlineThunk, modifiers);

lib/PrintAsClang/PrintClangFunction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class DeclAndTypeClangFunctionPrinter {
8686
bool isStatic = false;
8787
bool isInline = false;
8888
bool isConst = false;
89+
bool isNoexcept = false;
90+
bool hasSymbolUSR = true;
91+
/// Specific declaration that should be used to emit the symbol's
92+
/// USR instead of the original function declaration.
93+
const ValueDecl *symbolUSROverride = nullptr;
8994

9095
FunctionSignatureModifiers() {}
9196
};

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ void ClangValueTypePrinter::forwardDeclType(raw_ostream &os,
8888
typeDecl->getGenericSignature().getCanonicalSignature();
8989
ClangSyntaxPrinter(os).printGenericSignature(genericSignature);
9090
}
91-
os << "class ";
91+
os << "class";
92+
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
93+
os << ' ';
9294
ClangSyntaxPrinter(os).printBaseName(typeDecl);
9395
os << ";\n";
9496
printTypePrecedingGenericTraits(os, typeDecl, typeDecl->getModuleContext());
@@ -259,7 +261,9 @@ void ClangValueTypePrinter::printValueTypeDecl(
259261

260262
// Print out the C++ class itself.
261263
printGenericSignature(os);
262-
os << "class ";
264+
os << "class";
265+
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
266+
os << ' ';
263267
ClangSyntaxPrinter(os).printBaseName(typeDecl);
264268
os << " final {\n";
265269
os << "public:\n";

lib/PrintAsClang/_SwiftCxxInteroperability.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
// FIXME: Use always_inline, artificial.
3232
#define SWIFT_INLINE_THUNK inline
3333

34+
/// The `SWIFT_SYMBOL_MODULE` and `SWIFT_SYMBOL_MODULE_USR` macros apply
35+
/// `external_source_symbol` Clang attributes to C++ declarations that represent
36+
/// Swift declarations. This allows Clang to index them as external
37+
/// declarations, using the specified Swift USR values.
38+
#if __has_attribute(external_source_symbol)
39+
#define SWIFT_SYMBOL_MODULE(moduleValue) \
40+
__attribute__((external_source_symbol( \
41+
language = "Swift", defined_in = moduleValue, generated_declaration)))
42+
#if __has_attribute(external_source_symbol_with_usr)
43+
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue) \
44+
__attribute__(( \
45+
external_source_symbol(language = "Swift", defined_in = moduleValue, \
46+
generated_declaration, USR = usrValue)))
47+
#else
48+
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue) \
49+
__attribute__((external_source_symbol( \
50+
language = "Swift", defined_in = moduleValue, generated_declaration)))
51+
#endif
52+
#else
53+
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue)
54+
#define SWIFT_SYMBOL_MODULE(moduleValue)
55+
#endif
56+
3457
namespace swift {
3558
namespace _impl {
3659

lib/Serialization/Serialization.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,11 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
28552855
case DAK_Custom: {
28562856
auto abbrCode = S.DeclTypeAbbrCodes[CustomDeclAttrLayout::Code];
28572857
auto theAttr = cast<CustomAttr>(DA);
2858+
2859+
// Macro attributes are not serialized.
2860+
if (theAttr->isAttachedMacro(D))
2861+
return;
2862+
28582863
auto typeID = S.addTypeRef(theAttr->getType());
28592864
if (!typeID && !S.allowCompilerErrors()) {
28602865
llvm::PrettyStackTraceString message("CustomAttr has no type");
@@ -3368,15 +3373,27 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
33683373
///
33693374
/// This should be kept conservative. Compiler crashes are still better than
33703375
/// miscompiles.
3371-
static bool overriddenDeclAffectsABI(const ValueDecl *overridden) {
3376+
static bool overriddenDeclAffectsABI(const ValueDecl *override,
3377+
const ValueDecl *overridden) {
33723378
if (!overridden)
33733379
return false;
3374-
// There's one case where we know a declaration doesn't affect the ABI of
3380+
// There's a few cases where we know a declaration doesn't affect the ABI of
33753381
// its overrides after they've been compiled: if the declaration is '@objc'
33763382
// and 'dynamic'. In that case, all accesses to the method or property will
33773383
// go through the Objective-C method tables anyway.
33783384
if (overridden->hasClangNode() || overridden->shouldUseObjCDispatch())
33793385
return false;
3386+
3387+
// In a public-override-internal case, the override doesn't have ABI
3388+
// implications.
3389+
auto isPublic = [](const ValueDecl *VD) {
3390+
return VD->getFormalAccessScope(VD->getDeclContext(),
3391+
/*treatUsableFromInlineAsPublic*/true)
3392+
.isPublic();
3393+
};
3394+
if (isPublic(override) && !isPublic(overridden))
3395+
return false;
3396+
33803397
return true;
33813398
}
33823399

@@ -4067,7 +4084,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
40674084
fn->isImplicitlyUnwrappedOptional(),
40684085
S.addDeclRef(fn->getOperatorDecl()),
40694086
S.addDeclRef(fn->getOverriddenDecl()),
4070-
overriddenDeclAffectsABI(fn->getOverriddenDecl()),
4087+
overriddenDeclAffectsABI(fn, fn->getOverriddenDecl()),
40714088
fn->getName().getArgumentNames().size() +
40724089
fn->getName().isCompoundName(),
40734090
rawAccessLevel,
@@ -4159,7 +4176,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
41594176
uint8_t(getStableAccessorKind(fn->getAccessorKind()));
41604177

41614178
bool overriddenAffectsABI =
4162-
overriddenDeclAffectsABI(fn->getOverriddenDecl());
4179+
overriddenDeclAffectsABI(fn, fn->getOverriddenDecl());
41634180

41644181
Type ty = fn->getInterfaceType();
41654182
SmallVector<IdentifierID, 4> dependencies;

stdlib/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
# standard library from the Swift compiler build.
66
project(swift-stdlib LANGUAGES C CXX)
77

8+
# CMake passes `-external:I` to clang-cl which results in the search order being
9+
# altered, and this impacts the definitions of the intrinsics. When building
10+
# with a MSVC toolset 19.29.30036.3 or newer, this will prevent the runtime from
11+
# being built on Windows. Since we know that we only support `clang-cl` as the
12+
# compiler for the runtime due to the use of the Swift calling convention, we
13+
# simply override the CMake behaviour unconditionally.
14+
set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-I")
15+
set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-I")
16+
817
# Add path for custom CMake modules.
918
list(APPEND CMAKE_MODULE_PATH
1019
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

0 commit comments

Comments
 (0)