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";

0 commit comments

Comments
 (0)