Skip to content

Commit 222c704

Browse files
Merge pull request #5515 from swiftwasm/main
[pull] swiftwasm from main
2 parents c209503 + 2db4a03 commit 222c704

Some content is hidden

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

43 files changed

+554
-136
lines changed

include/swift/AST/Decl.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
659659
HasAnyUnavailableValues : 1
660660
);
661661

662-
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
662+
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
663663
/// If the module is compiled as static library.
664664
StaticLibrary : 1,
665665

@@ -709,7 +709,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
709709

710710
/// If the map from @objc provided name to top level swift::Decl in this
711711
/// module is populated
712-
ObjCNameLookupCachePopulated : 1
712+
ObjCNameLookupCachePopulated : 1,
713+
714+
/// Whether this module has been built with C++ interoperability enabled.
715+
HasCxxInteroperability : 1
713716
);
714717

715718
SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,
@@ -5293,10 +5296,10 @@ class AbstractStorageDecl : public ValueDecl {
52935296

52945297
/// Overwrite the registered implementation-info. This should be
52955298
/// used carefully.
5296-
void setImplInfo(StorageImplInfo implInfo) {
5297-
LazySemanticInfo.ImplInfoComputed = 1;
5298-
ImplInfo = implInfo;
5299-
}
5299+
void setImplInfo(StorageImplInfo implInfo);
5300+
5301+
/// Cache the implementation-info, for use by the request-evaluator.
5302+
void cacheImplInfo(StorageImplInfo implInfo);
53005303

53015304
ReadImplKind getReadImpl() const {
53025305
return getImplInfo().getReadImpl();
@@ -5311,9 +5314,7 @@ class AbstractStorageDecl : public ValueDecl {
53115314

53125315
/// Return true if this is a VarDecl that has storage associated with
53135316
/// it.
5314-
bool hasStorage() const {
5315-
return getImplInfo().hasStorage();
5316-
}
5317+
bool hasStorage() const;
53175318

53185319
/// Return true if this storage has the basic accessors/capability
53195320
/// to be mutated. This is generally constant after the accessors are

include/swift/AST/DiagnosticsSema.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,12 @@ ERROR(need_hermetic_seal_to_import_module,none,
874874
"module %0 was built with -experimental-hermetic-seal-at-link, but "
875875
"current compilation does not have -experimental-hermetic-seal-at-link",
876876
(Identifier))
877+
ERROR(need_cxx_interop_to_import_module,none,
878+
"module %0 was built with C++ interoperability enabled, but "
879+
"current compilation does not enable C++ interoperability",
880+
(Identifier))
881+
NOTE(enable_cxx_interop_docs,none,
882+
"visit https://www.swift.org/documentation/cxx-interop/project-build-setup to learn how to enable C++ interoperability", ())
877883

878884
ERROR(modularization_issue_decl_moved,Fatal,
879885
"reference to %select{top-level declaration|type}0 %1 broken by a context change; "
@@ -7181,6 +7187,13 @@ ERROR(invalid_macro_role_for_macro_syntax,none,
71817187
(unsigned))
71827188
ERROR(macro_cannot_introduce_names,none,
71837189
"'%0' macros are not allowed to introduce names", (StringRef))
7190+
ERROR(macro_accessor_missing_from_expansion,none,
7191+
"expansion of macro %0 did not produce a %select{non-|}1observing "
7192+
"accessor",
7193+
(DeclName, bool))
7194+
ERROR(macro_init_accessor_not_documented,none,
7195+
"expansion of macro %0 produced an unexpected 'init' accessor",
7196+
(DeclName))
71847197

71857198
ERROR(macro_resolve_circular_reference, none,
71867199
"circular reference resolving %select{freestanding|attached}0 macro %1",

include/swift/AST/Evaluator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ class Evaluator {
316316
cache.insert<Request>(request, std::move(output));
317317
}
318318

319+
template<typename Request,
320+
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>
321+
bool hasCachedResult(const Request &request) {
322+
return cache.find_as(request) != cache.end<Request>();
323+
}
324+
319325
/// Do not introduce new callers of this function.
320326
template<typename Request,
321327
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>

include/swift/AST/Module.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,14 @@ class ModuleDecl
654654
Bits.ModuleDecl.HasHermeticSealAtLink = enabled;
655655
}
656656

657+
/// Returns true if this module was built with C++ interoperability enabled.
658+
bool hasCxxInteroperability() const {
659+
return Bits.ModuleDecl.HasCxxInteroperability;
660+
}
661+
void setHasCxxInteroperability(bool enabled = true) {
662+
Bits.ModuleDecl.HasCxxInteroperability = enabled;
663+
}
664+
657665
/// \returns true if this module is a system module; note that the StdLib is
658666
/// considered a system module.
659667
bool isSystemModule() const {

include/swift/AST/NameLookup.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,13 @@ void forEachPotentialResolvedMacro(
556556
llvm::function_ref<void(MacroDecl *, const MacroRoleAttr *)> body
557557
);
558558

559+
/// For each macro with the given role that might be attached to the given
560+
/// declaration, call the body.
561+
void forEachPotentialAttachedMacro(
562+
Decl *decl, MacroRole role,
563+
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
564+
);
565+
559566
} // end namespace namelookup
560567

561568
/// Describes an inherited nominal entry.

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,26 @@ class InitAccessorPropertiesRequest :
16861686
ArrayRef<VarDecl *>
16871687
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
16881688

1689+
// Evaluation.
1690+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1691+
1692+
public:
1693+
bool isCached() const { return true; }
1694+
};
1695+
1696+
class HasStorageRequest :
1697+
public SimpleRequest<HasStorageRequest,
1698+
bool(AbstractStorageDecl *),
1699+
RequestFlags::Cached> {
1700+
public:
1701+
using SimpleRequest::SimpleRequest;
1702+
1703+
private:
1704+
friend SimpleRequest;
1705+
1706+
// Evaluation.
1707+
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;
1708+
16891709
public:
16901710
bool isCached() const { return true; }
16911711
};

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ SWIFT_REQUEST(TypeChecker, SelfAccessKindRequest, SelfAccessKind(FuncDecl *),
302302
SWIFT_REQUEST(TypeChecker, StorageImplInfoRequest,
303303
StorageImplInfo(AbstractStorageDecl *), SeparatelyCached,
304304
NoLocationInfo)
305+
SWIFT_REQUEST(TypeChecker, HasStorageRequest,
306+
bool(AbstractStorageDecl *), Cached,
307+
NoLocationInfo)
305308
SWIFT_REQUEST(TypeChecker, StoredPropertiesAndMissingMembersRequest,
306309
ArrayRef<Decl *>(NominalTypeDecl *), Cached, NoLocationInfo)
307310
SWIFT_REQUEST(TypeChecker, StoredPropertiesRequest,

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ namespace swift {
313313
/// Imports getters and setters as computed properties.
314314
bool CxxInteropGettersSettersAsProperties = false;
315315

316+
/// Should the compiler require C++ interoperability to be enabled
317+
/// when importing Swift modules that enable C++ interoperability.
318+
bool RequireCxxInteropToImportCxxInteropModule = true;
319+
316320
/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
317321
/// classes instead of the stable ABI's bit. This is needed when
318322
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,11 @@ def cxx_interop_getters_setters_as_properties :
865865
HelpText<"Import getters and setters as computed properties in Swift">,
866866
Flags<[FrontendOption, HelpHidden]>;
867867

868+
def cxx_interop_disable_requirement_at_import :
869+
Flag<["-"], "disable-cxx-interop-requirement-at-import">,
870+
HelpText<"Do not require C++ interoperability to be enabled when importing a Swift module that enables C++ interoperability">,
871+
Flags<[FrontendOption, HelpHidden]>;
872+
868873
def use_malloc : Flag<["-"], "use-malloc">,
869874
HelpText<"Allocate internal data structures using malloc "
870875
"(for memory debugging)">;

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ class SILModule {
395395
/// Action to be executed for serializing the SILModule.
396396
ActionCallback SerializeSILAction;
397397

398-
#ifndef NDEBUG
399398
BasicBlockNameMapType basicBlockNames;
400-
#endif
401399

402400
SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
403401
Lowering::TypeConverter &TC, const SILOptions &Options,

include/swift/Serialization/Validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ExtendedValidationInfo {
131131
unsigned IsBuiltFromInterface : 1;
132132
unsigned IsAllowModuleWithCompilerErrorsEnabled : 1;
133133
unsigned IsConcurrencyChecked : 1;
134+
unsigned HasCxxInteroperability : 1;
134135
} Bits;
135136
public:
136137
ExtendedValidationInfo() : Bits() {}
@@ -232,6 +233,10 @@ class ExtendedValidationInfo {
232233
void setIsConcurrencyChecked(bool val = true) {
233234
Bits.IsConcurrencyChecked = val;
234235
}
236+
bool hasCxxInteroperability() const { return Bits.HasCxxInteroperability; }
237+
void setHasCxxInteroperability(bool val) {
238+
Bits.HasCxxInteroperability = val;
239+
}
235240
};
236241

237242
struct SearchPath {

lib/AST/Decl.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,7 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
25252525
ResilienceExpansion expansion) const {
25262526
switch (semantics) {
25272527
case AccessSemantics::DirectToStorage:
2528-
assert(hasStorage());
2528+
assert(hasStorage() || getASTContext().Diags.hadAnyError());
25292529
return AccessStrategy::getStorage();
25302530

25312531
case AccessSemantics::DistributedThunk:
@@ -6393,13 +6393,40 @@ bool ProtocolDecl::hasCircularInheritedProtocols() const {
63936393
ctx.evaluator, HasCircularInheritedProtocolsRequest{mutableThis}, true);
63946394
}
63956395

6396+
bool AbstractStorageDecl::hasStorage() const {
6397+
ASTContext &ctx = getASTContext();
6398+
return evaluateOrDefault(ctx.evaluator,
6399+
HasStorageRequest{const_cast<AbstractStorageDecl *>(this)},
6400+
false);
6401+
}
6402+
63966403
StorageImplInfo AbstractStorageDecl::getImplInfo() const {
63976404
ASTContext &ctx = getASTContext();
63986405
return evaluateOrDefault(ctx.evaluator,
63996406
StorageImplInfoRequest{const_cast<AbstractStorageDecl *>(this)},
64006407
StorageImplInfo::getSimpleStored(StorageIsMutable));
64016408
}
64026409

6410+
void AbstractStorageDecl::cacheImplInfo(StorageImplInfo implInfo) {
6411+
LazySemanticInfo.ImplInfoComputed = 1;
6412+
ImplInfo = implInfo;
6413+
}
6414+
6415+
void AbstractStorageDecl::setImplInfo(StorageImplInfo implInfo) {
6416+
cacheImplInfo(implInfo);
6417+
6418+
if (isImplicit()) {
6419+
auto &evaluator = getASTContext().evaluator;
6420+
HasStorageRequest request{this};
6421+
if (!evaluator.hasCachedResult(request))
6422+
evaluator.cacheOutput(request, implInfo.hasStorage());
6423+
else {
6424+
assert(
6425+
evaluateOrDefault(evaluator, request, false) == implInfo.hasStorage());
6426+
}
6427+
}
6428+
}
6429+
64036430
bool AbstractStorageDecl::hasPrivateAccessor() const {
64046431
for (auto accessor : getAllAccessors()) {
64056432
if (hasPrivateOrFilePrivateFormalAccess(accessor))

lib/AST/Module.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
646646
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
647647
Bits.ModuleDecl.IsConcurrencyChecked = 0;
648648
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
649+
Bits.ModuleDecl.HasCxxInteroperability = 0;
649650
}
650651

651652
void ModuleDecl::setIsSystemModule(bool flag) {

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ void namelookup::forEachPotentialResolvedMacro(
16271627

16281628
/// For each macro with the given role that might be attached to the given
16291629
/// declaration, call the body.
1630-
static void forEachPotentialAttachedMacro(
1630+
void namelookup::forEachPotentialAttachedMacro(
16311631
Decl *decl, MacroRole role,
16321632
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
16331633
) {

lib/AST/TypeCheckRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ StorageImplInfoRequest::getCachedResult() const {
687687

688688
void StorageImplInfoRequest::cacheResult(StorageImplInfo value) const {
689689
auto *storage = std::get<0>(getStorage());
690-
storage->setImplInfo(value);
690+
storage->cacheImplInfo(value);
691691
}
692692

693693
//----------------------------------------------------------------------------//

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "swift/AST/NameLookupRequests.h"
3535
#include "swift/AST/PrettyStackTrace.h"
3636
#include "swift/AST/SourceFile.h"
37+
#include "swift/AST/TypeCheckRequests.h"
3738
#include "swift/AST/Types.h"
3839
#include "swift/Basic/Defer.h"
3940
#include "swift/Basic/Platform.h"
@@ -5056,6 +5057,7 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
50565057
out->setIsObjC(var->isObjC());
50575058
out->setIsDynamic(var->isDynamic());
50585059
out->copyFormalAccessFrom(var);
5060+
out->getASTContext().evaluator.cacheOutput(HasStorageRequest{out}, false);
50595061
out->setAccessors(SourceLoc(),
50605062
makeBaseClassMemberAccessors(newContext, out, var),
50615063
SourceLoc());

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void ClangImporter::Implementation::makeComputed(AbstractStorageDecl *storage,
126126
AccessorDecl *getter,
127127
AccessorDecl *setter) {
128128
assert(getter);
129+
storage->getASTContext().evaluator.cacheOutput(HasStorageRequest{storage}, false);
129130
if (setter) {
130131
storage->setImplInfo(StorageImplInfo::getMutableComputed());
131132
storage->setAccessors(SourceLoc(), {getter, setter}, SourceLoc());

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10431043
Args.hasArg(OPT_experimental_c_foreign_reference_types);
10441044

10451045
Opts.CxxInteropGettersSettersAsProperties = Args.hasArg(OPT_cxx_interop_getters_setters_as_properties);
1046+
Opts.RequireCxxInteropToImportCxxInteropModule =
1047+
!Args.hasArg(OPT_cxx_interop_disable_requirement_at_import);
10461048

10471049
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
10481050

lib/Frontend/Frontend.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,9 @@ ModuleDecl *CompilerInstance::getMainModule() const {
13191319
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
13201320
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))
13211321
MainModule->setIsConcurrencyChecked(true);
1322+
if (Invocation.getLangOptions().EnableCXXInterop &&
1323+
Invocation.getLangOptions().RequireCxxInteropToImportCxxInteropModule)
1324+
MainModule->setHasCxxInteroperability();
13221325

13231326
// Register the main module with the AST context.
13241327
Context->addLoadedModule(MainModule);

lib/IRGen/IRGenSIL.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,10 +2942,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {
29422942

29432943
void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29442944
SILGlobalVariable *var = i->getReferencedGlobal();
2945-
SILType loweredTy = var->getLoweredTypeInContext(getExpansionContext());
2946-
assert(loweredTy == i->getType().getObjectType());
2945+
SILType loweredTy = var->getLoweredType();
29472946
auto &ti = getTypeInfo(loweredTy);
2948-
2947+
29492948
auto expansion = IGM.getResilienceExpansionForLayout(var);
29502949

29512950
// If the variable is empty in all resilience domains that can see it,
@@ -2968,7 +2967,15 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
29682967
// Otherwise, the static storage for the global consists of a fixed-size
29692968
// buffer; project it.
29702969
addr = emitProjectValueInBuffer(*this, loweredTy, addr);
2971-
2970+
2971+
2972+
// Get the address of the type in context.
2973+
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
2974+
auto &tiInContext = getTypeInfo(loweredTyInContext);
2975+
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
2976+
tiInContext.getStorageType()->getPointerTo());
2977+
addr = Address(ptr, tiInContext.getStorageType(),
2978+
tiInContext.getBestKnownAlignment());
29722979
setLoweredAddress(i, addr);
29732980
}
29742981

0 commit comments

Comments
 (0)