Skip to content

[pull] swiftwasm from main #5198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,10 @@ class CustomAttr final : public DeclAttribute {
bool isArgUnsafe() const;
void setArgIsUnsafe(bool unsafe) { isArgUnsafeBit = unsafe; }

/// Whether this custom attribute is a macro attached to the given
/// declaration.
bool isAttachedMacro(const Decl *decl) const;

Expr *getSemanticInit() const { return semanticInit; }
void setSemanticInit(Expr *expr) { semanticInit = expr; }

Expand Down
16 changes: 16 additions & 0 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "swift/AST/IndexSubset.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeRepr.h"
Expand Down Expand Up @@ -2336,6 +2337,21 @@ bool CustomAttr::isArgUnsafe() const {
return isArgUnsafeBit;
}

bool CustomAttr::isAttachedMacro(const Decl *decl) const {
auto &ctx = decl->getASTContext();
auto *dc = decl->getInnermostDeclContext();

auto attrDecl = evaluateOrDefault(
ctx.evaluator,
CustomAttrDeclRequest{const_cast<CustomAttr *>(this), dc},
nullptr);

if (!attrDecl)
return false;

return attrDecl.dyn_cast<MacroDecl *>();
}

DeclarationAttr::DeclarationAttr(SourceLoc atLoc, SourceRange range,
MacroRole role,
ArrayRef<MacroIntroducedDeclName> peerNames,
Expand Down
21 changes: 20 additions & 1 deletion lib/PrintAsClang/ClangSyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

#include "ClangSyntaxPrinter.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/AST/SwiftNameTranslation.h"
#include "swift/AST/TypeCheckRequests.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/NestedNameSpecifier.h"
Expand Down Expand Up @@ -140,18 +142,21 @@ void ClangSyntaxPrinter::printModuleNamespaceStart(
os << "namespace ";
printBaseName(&moduleContext);
os << " __attribute__((swift_private))";
printSymbolUSRAttribute(&moduleContext);
os << " {\n";
}

/// Print a C++ namespace declaration with the give name and body.
void ClangSyntaxPrinter::printNamespace(
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia) const {
NamespaceTrivia trivia, const ModuleDecl *moduleContext) const {
os << "namespace ";
namePrinter(os);
if (trivia == NamespaceTrivia::AttributeSwiftPrivate)
os << " __attribute__((swift_private))";
if (moduleContext)
printSymbolUSRAttribute(moduleContext);
os << " {\n\n";
bodyPrinter(os);
os << "\n} // namespace ";
Expand Down Expand Up @@ -405,3 +410,17 @@ void ClangSyntaxPrinter::printIgnoredCxx17ExtensionDiagnosticBlock(
llvm::function_ref<void()> bodyPrinter) {
printIgnoredDiagnosticBlock("c++17-extensions", bodyPrinter);
}

void ClangSyntaxPrinter::printSymbolUSRAttribute(const ValueDecl *D) const {
if (isa<ModuleDecl>(D)) {
os << " SWIFT_SYMBOL_MODULE(\"";
printBaseName(D);
os << "\")";
return;
}
auto result = evaluateOrDefault(D->getASTContext().evaluator,
USRGenerationRequest{D}, std::string());
if (result.empty())
return;
os << " SWIFT_SYMBOL(\"" << result << "\")";
}
7 changes: 6 additions & 1 deletion lib/PrintAsClang/ClangSyntaxPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class ClangSyntaxPrinter {
/// Print a C++ namespace declaration with the give name and body.
void printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
NamespaceTrivia trivia = NamespaceTrivia::None) const;
NamespaceTrivia trivia = NamespaceTrivia::None,
const ModuleDecl *moduleContext = nullptr) const;

void printNamespace(StringRef name,
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter,
Expand Down Expand Up @@ -220,6 +221,10 @@ class ClangSyntaxPrinter {
void printIgnoredCxx17ExtensionDiagnosticBlock(
llvm::function_ref<void()> bodyPrinter);

/// Print the macro that applies Clang's `external_source_symbol` attribute
/// on the generated declaration.
void printSymbolUSRAttribute(const ValueDecl *D) const;

protected:
raw_ostream &os;
};
Expand Down
8 changes: 5 additions & 3 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,8 @@ class DeclAndTypePrinter::Implementation
}
os << " } ";
syntaxPrinter.printIdentifier(caseName);
if (elementDecl)
syntaxPrinter.printSymbolUSRAttribute(elementDecl);
os << ";\n";
};

Expand All @@ -742,6 +744,7 @@ class DeclAndTypePrinter::Implementation
[&](const auto &pair) {
os << "\n ";
syntaxPrinter.printIdentifier(pair.first->getNameStr());
syntaxPrinter.printSymbolUSRAttribute(pair.first);
},
",");
// TODO: allow custom name for this special case
Expand Down Expand Up @@ -1413,16 +1416,15 @@ class DeclAndTypePrinter::Implementation
owningPrinter.interopContext, owningPrinter);
DeclAndTypeClangFunctionPrinter::FunctionSignatureModifiers modifiers;
modifiers.isInline = true;
// FIXME: Support throwing exceptions for Swift errors.
modifiers.isNoexcept = !funcTy->isThrowing();
auto result = funcPrinter.printFunctionSignature(
FD, funcABI.getSignature(), cxx_translation::getNameForCxx(FD),
resultTy,
DeclAndTypeClangFunctionPrinter::FunctionSignatureKind::CxxInlineThunk,
modifiers);
assert(
!result.isUnsupported()); // The C signature should be unsupported too.
// FIXME: Support throwing exceptions for Swift errors.
if (!funcTy->isThrowing())
os << " noexcept";
printFunctionClangAttributes(FD, funcTy);
printAvailability(FD);
os << " {\n";
Expand Down
12 changes: 11 additions & 1 deletion lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
llvm::raw_string_ostream prologueOS{modulePrologueBuf};
EmittedClangHeaderDependencyInfo info;

// Define the `SWIFT_SYMBOL` macro.
os << "#ifdef SWIFT_SYMBOL\n";
os << "#undef SWIFT_SYMBOL\n";
os << "#endif\n";
os << "#define SWIFT_SYMBOL(usrValue) SWIFT_SYMBOL_MODULE_USR(\"";
ClangSyntaxPrinter(os).printBaseName(&M);
os << "\", usrValue)\n";

// FIXME: Use getRequiredAccess once @expose is supported.
ModuleWriter writer(moduleOS, prologueOS, info.imports, M, interopContext,
AccessLevel::Public, requiresExposedAttribute,
Expand Down Expand Up @@ -824,6 +832,7 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
os << "namespace ";
M.ValueDecl::getName().print(os);
os << " __attribute__((swift_private))";
ClangSyntaxPrinter(os).printSymbolUSRAttribute(&M);
os << " {\n";
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
os << "extern \"C\" {\n";
Expand All @@ -842,10 +851,11 @@ EmittedClangHeaderDependencyInfo swift::printModuleContentsAsCxx(
ClangSyntaxPrinter(os).printNamespace(
[&](raw_ostream &os) { M.ValueDecl::getName().print(os); },
[&](raw_ostream &os) { os << moduleOS.str(); },
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate);
ClangSyntaxPrinter::NamespaceTrivia::AttributeSwiftPrivate, &M);

if (M.isStdlibModule()) {
os << "#pragma clang diagnostic pop\n";
}
os << "#undef SWIFT_SYMBOL\n";
return info;
}
4 changes: 3 additions & 1 deletion lib/PrintAsClang/PrintClangClassType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ void ClangClassTypePrinter::printClassTypeDecl(
baseClassQualifiedName = "swift::_impl::RefCountedClass";
}

os << "class ";
os << "class";
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
os << ' ';
printer.printBaseName(typeDecl);
if (typeDecl->isFinal())
os << " final";
Expand Down
8 changes: 8 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,11 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
os << ')';
if (modifiers.isConst)
os << " const";
if (modifiers.isNoexcept)
os << " noexcept";
if (modifiers.hasSymbolUSR)
ClangSyntaxPrinter(os).printSymbolUSRAttribute(
modifiers.symbolUSROverride ? modifiers.symbolUSROverride : FD);
return resultingRepresentation;
}

Expand Down Expand Up @@ -1309,6 +1314,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxMethod(
isa<FuncDecl>(FD) ? cast<FuncDecl>(FD)->isMutating() : false;
modifiers.isConst =
!isa<ClassDecl>(typeDeclContext) && !isMutating && !isConstructor;
modifiers.hasSymbolUSR = !isDefinition;
auto result = printFunctionSignature(
FD, signature,
isConstructor ? getConstructorName(FD)
Expand Down Expand Up @@ -1373,6 +1379,8 @@ void DeclAndTypeClangFunctionPrinter::printCxxPropertyAccessorMethod(
modifiers.isInline = true;
modifiers.isConst =
!isStatic && accessor->isGetter() && !isa<ClassDecl>(typeDeclContext);
modifiers.hasSymbolUSR = !isDefinition;
modifiers.symbolUSROverride = accessor->getStorage();
auto result = printFunctionSignature(
accessor, signature, remapPropertyName(accessor, resultTy), resultTy,
FunctionSignatureKind::CxxInlineThunk, modifiers);
Expand Down
5 changes: 5 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class DeclAndTypeClangFunctionPrinter {
bool isStatic = false;
bool isInline = false;
bool isConst = false;
bool isNoexcept = false;
bool hasSymbolUSR = true;
/// Specific declaration that should be used to emit the symbol's
/// USR instead of the original function declaration.
const ValueDecl *symbolUSROverride = nullptr;

FunctionSignatureModifiers() {}
};
Expand Down
8 changes: 6 additions & 2 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ void ClangValueTypePrinter::forwardDeclType(raw_ostream &os,
typeDecl->getGenericSignature().getCanonicalSignature();
ClangSyntaxPrinter(os).printGenericSignature(genericSignature);
}
os << "class ";
os << "class";
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
os << ' ';
ClangSyntaxPrinter(os).printBaseName(typeDecl);
os << ";\n";
printTypePrecedingGenericTraits(os, typeDecl, typeDecl->getModuleContext());
Expand Down Expand Up @@ -259,7 +261,9 @@ void ClangValueTypePrinter::printValueTypeDecl(

// Print out the C++ class itself.
printGenericSignature(os);
os << "class ";
os << "class";
ClangSyntaxPrinter(os).printSymbolUSRAttribute(typeDecl);
os << ' ';
ClangSyntaxPrinter(os).printBaseName(typeDecl);
os << " final {\n";
os << "public:\n";
Expand Down
23 changes: 23 additions & 0 deletions lib/PrintAsClang/_SwiftCxxInteroperability.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
// FIXME: Use always_inline, artificial.
#define SWIFT_INLINE_THUNK inline

/// The `SWIFT_SYMBOL_MODULE` and `SWIFT_SYMBOL_MODULE_USR` macros apply
/// `external_source_symbol` Clang attributes to C++ declarations that represent
/// Swift declarations. This allows Clang to index them as external
/// declarations, using the specified Swift USR values.
#if __has_attribute(external_source_symbol)
#define SWIFT_SYMBOL_MODULE(moduleValue) \
__attribute__((external_source_symbol( \
language = "Swift", defined_in = moduleValue, generated_declaration)))
#if __has_attribute(external_source_symbol_with_usr)
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue) \
__attribute__(( \
external_source_symbol(language = "Swift", defined_in = moduleValue, \
generated_declaration, USR = usrValue)))
#else
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue) \
__attribute__((external_source_symbol( \
language = "Swift", defined_in = moduleValue, generated_declaration)))
#endif
#else
#define SWIFT_SYMBOL_MODULE_USR(moduleValue, usrValue)
#define SWIFT_SYMBOL_MODULE(moduleValue)
#endif

namespace swift {
namespace _impl {

Expand Down
25 changes: 21 additions & 4 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2855,6 +2855,11 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
case DAK_Custom: {
auto abbrCode = S.DeclTypeAbbrCodes[CustomDeclAttrLayout::Code];
auto theAttr = cast<CustomAttr>(DA);

// Macro attributes are not serialized.
if (theAttr->isAttachedMacro(D))
return;

auto typeID = S.addTypeRef(theAttr->getType());
if (!typeID && !S.allowCompilerErrors()) {
llvm::PrettyStackTraceString message("CustomAttr has no type");
Expand Down Expand Up @@ -3368,15 +3373,27 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
///
/// This should be kept conservative. Compiler crashes are still better than
/// miscompiles.
static bool overriddenDeclAffectsABI(const ValueDecl *overridden) {
static bool overriddenDeclAffectsABI(const ValueDecl *override,
const ValueDecl *overridden) {
if (!overridden)
return false;
// There's one case where we know a declaration doesn't affect the ABI of
// There's a few cases where we know a declaration doesn't affect the ABI of
// its overrides after they've been compiled: if the declaration is '@objc'
// and 'dynamic'. In that case, all accesses to the method or property will
// go through the Objective-C method tables anyway.
if (overridden->hasClangNode() || overridden->shouldUseObjCDispatch())
return false;

// In a public-override-internal case, the override doesn't have ABI
// implications.
auto isPublic = [](const ValueDecl *VD) {
return VD->getFormalAccessScope(VD->getDeclContext(),
/*treatUsableFromInlineAsPublic*/true)
.isPublic();
};
if (isPublic(override) && !isPublic(overridden))
return false;

return true;
}

Expand Down Expand Up @@ -4067,7 +4084,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
fn->isImplicitlyUnwrappedOptional(),
S.addDeclRef(fn->getOperatorDecl()),
S.addDeclRef(fn->getOverriddenDecl()),
overriddenDeclAffectsABI(fn->getOverriddenDecl()),
overriddenDeclAffectsABI(fn, fn->getOverriddenDecl()),
fn->getName().getArgumentNames().size() +
fn->getName().isCompoundName(),
rawAccessLevel,
Expand Down Expand Up @@ -4159,7 +4176,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
uint8_t(getStableAccessorKind(fn->getAccessorKind()));

bool overriddenAffectsABI =
overriddenDeclAffectsABI(fn->getOverriddenDecl());
overriddenDeclAffectsABI(fn, fn->getOverriddenDecl());

Type ty = fn->getInterfaceType();
SmallVector<IdentifierID, 4> dependencies;
Expand Down
9 changes: 9 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
# standard library from the Swift compiler build.
project(swift-stdlib LANGUAGES C CXX)

# CMake passes `-external:I` to clang-cl which results in the search order being
# altered, and this impacts the definitions of the intrinsics. When building
# with a MSVC toolset 19.29.30036.3 or newer, this will prevent the runtime from
# being built on Windows. Since we know that we only support `clang-cl` as the
# compiler for the runtime due to the use of the Swift calling convention, we
# simply override the CMake behaviour unconditionally.
set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-I")
set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-I")

# Add path for custom CMake modules.
list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
Expand Down
Loading