Skip to content

Commit aa7c420

Browse files
authored
Merge pull request #3967 from swiftwasm/main
2 parents e2a4e53 + a46a3c5 commit aa7c420

File tree

138 files changed

+4683
-921
lines changed

Some content is hidden

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

138 files changed

+4683
-921
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ set(SWIFT_DARWIN_PLATFORMS "IOS" "IOS_SIMULATOR" "TVOS" "TVOS_SIMULATOR" "WATCHO
674674
set(SWIFT_APPLE_PLATFORMS ${SWIFT_DARWIN_PLATFORMS})
675675
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple")
676676
list(APPEND SWIFT_APPLE_PLATFORMS "FREESTANDING")
677+
if(SWIFT_FREESTANDING_IS_DARWIN)
678+
list(APPEND SWIFT_DARWIN_PLATFORMS "FREESTANDING")
679+
endif()
677680
endif()
678681

679682
# Configuration flags passed to all of our invocations of gyb. Try to

include/swift/AST/Decl.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/GenericParamKey.h"
2929
#include "swift/AST/IfConfigClause.h"
3030
#include "swift/AST/LayoutConstraint.h"
31+
#include "swift/AST/ReferenceCounting.h"
3132
#include "swift/AST/StorageImpl.h"
3233
#include "swift/AST/TypeAlignments.h"
3334
#include "swift/AST/TypeWalker.h"
@@ -37,10 +38,10 @@
3738
#include "swift/Basic/Compiler.h"
3839
#include "swift/Basic/Debug.h"
3940
#include "swift/Basic/InlineBitfield.h"
41+
#include "swift/Basic/Located.h"
4042
#include "swift/Basic/NullablePtr.h"
4143
#include "swift/Basic/OptionalEnum.h"
4244
#include "swift/Basic/Range.h"
43-
#include "swift/Basic/Located.h"
4445
#include "llvm/ADT/DenseSet.h"
4546
#include "llvm/Support/TrailingObjects.h"
4647
#include <type_traits>
@@ -3906,7 +3907,8 @@ class ClassDecl final : public NominalTypeDecl {
39063907
///
39073908
/// \see getForeignClassKind
39083909
bool isForeign() const {
3909-
return getForeignClassKind() != ForeignKind::Normal;
3910+
return getForeignClassKind() != ForeignKind::Normal ||
3911+
const_cast<ClassDecl *>(this)->isForeignReferenceType();
39103912
}
39113913

39123914
/// Whether the class is (known to be) a default actor.
@@ -3941,9 +3943,22 @@ class ClassDecl final : public NominalTypeDecl {
39413943
bool isNativeNSObjectSubclass() const;
39423944

39433945
/// Whether the class uses the ObjC object model (reference counting,
3944-
/// allocation, etc.) instead of the Swift model.
3945-
bool usesObjCObjectModel() const {
3946-
return checkAncestry(AncestryFlags::ObjCObjectModel);
3946+
/// allocation, etc.), the Swift model, or has no reference counting at all.
3947+
ReferenceCounting getObjectModel() {
3948+
if (isForeignReferenceType())
3949+
return ReferenceCounting::None;
3950+
3951+
if (checkAncestry(AncestryFlags::ObjCObjectModel))
3952+
return ReferenceCounting::ObjC;
3953+
3954+
return ReferenceCounting::Native;
3955+
}
3956+
3957+
LayoutConstraintKind getLayoutConstraintKind() {
3958+
if (getObjectModel() == ReferenceCounting::ObjC)
3959+
return LayoutConstraintKind::Class;
3960+
3961+
return LayoutConstraintKind::NativeClass;
39473962
}
39483963

39493964
/// Returns true if the class has designated initializers that are not listed
@@ -4065,6 +4080,11 @@ class ClassDecl final : public NominalTypeDecl {
40654080
bool hasKnownSwiftImplementation() const {
40664081
return !hasClangNode();
40674082
}
4083+
4084+
/// Used to determine if this class decl is a foriegn reference type. I.e., a
4085+
/// non-reference-counted swift reference type that was imported from a C++
4086+
/// record.
4087+
bool isForeignReferenceType();
40684088
};
40694089

40704090
/// A convenience wrapper around the \c SelfReferencePosition::Kind enum.

include/swift/AST/DiagnosticsIRGen.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ ERROR(temporary_allocation_alignment_not_positive,none,
5959
ERROR(temporary_allocation_alignment_not_power_of_2,none,
6060
"alignment value must be a power of two", ())
6161

62+
ERROR(foreign_reference_types_unsupported,none,
63+
"attempt to use a foreign reference type in a generic context. "
64+
"Foreign reference types are currently not supported. Using foreign "
65+
"reference types in a generic context is not yet implemented.", ())
66+
6267
#define UNDEFINE_DIAGNOSTIC_MACROS
6368
#include "DefineDiagnosticMacros.h"

include/swift/AST/Module.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,19 @@ class ModuleEntity {
882882
ModuleEntity(const ModuleDecl *Mod) : Mod(Mod) {}
883883
ModuleEntity(const clang::Module *Mod) : Mod(static_cast<const void *>(Mod)){}
884884

885-
StringRef getName() const;
886-
std::string getFullName() const;
885+
/// @param useRealNameIfAliased Whether to use the module's real name in case
886+
/// module aliasing is used. For example, if a file
887+
/// has `import Foo` and `-module-alias Foo=Bar` is
888+
/// passed, treat Foo as an alias and Bar as the real
889+
/// module name as its dependency. This only applies
890+
/// to Swift modules.
891+
/// @return The module name; for Swift modules, the real module name could be
892+
/// different from the name if module aliasing is used.
893+
StringRef getName(bool useRealNameIfAliased = false) const;
894+
895+
/// For Swift modules, it returns the same result as \c ModuleEntity::getName(bool).
896+
/// For Clang modules, it returns the result of \c clang::Module::getFullModuleName.
897+
std::string getFullName(bool useRealNameIfAliased = false) const;
887898

888899
bool isSystemModule() const;
889900
bool isBuiltinModule() const;

include/swift/AST/ReferenceCounting.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ enum class ReferenceCounting : uint8_t {
2929
/// Blocks are always ObjC reference counting compatible.
3030
ObjC,
3131

32+
/// The object has no reference counting. This is used by foreign reference
33+
/// types.
34+
None,
35+
3236
/// The object uses _Block_copy/_Block_release reference counting.
3337
///
3438
/// This is a strict subset of ObjC; all blocks are also ObjC reference

include/swift/AST/Type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ class CanType : public Type {
488488
NominalTypeDecl *getAnyNominal() const;
489489
GenericTypeDecl *getAnyGeneric() const;
490490

491+
bool isForeignReferenceType(); // in Types.h
492+
491493
CanType getOptionalObjectType() const {
492494
return getOptionalObjectTypeImpl(*this);
493495
}

include/swift/AST/Types.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -811,16 +811,19 @@ class alignas(1 << TypeAlignInBits) TypeBase
811811

812812
/// If this is a class type or a bound generic class type, returns the
813813
/// (possibly generic) class.
814-
ClassDecl *getClassOrBoundGenericClass();
815-
814+
ClassDecl *getClassOrBoundGenericClass() const;
815+
816816
/// If this is a struct type or a bound generic struct type, returns
817817
/// the (possibly generic) class.
818818
StructDecl *getStructOrBoundGenericStruct();
819819

820820
/// If this is an enum or a bound generic enum type, returns the
821821
/// (possibly generic) enum.
822822
EnumDecl *getEnumOrBoundGenericEnum();
823-
823+
824+
/// If this is a class, check if this class is a foreign reference type.
825+
bool isForeignReferenceType();
826+
824827
/// Determine whether this type may have a superclass, which holds for
825828
/// classes, bound generic classes, and archetypes that are only instantiable
826829
/// with a class type.
@@ -6198,7 +6201,7 @@ inline bool TypeBase::canDynamicallyBeOptionalType(bool includeExistential) {
61986201
return isArchetypeOrExistential && !T.isAnyClassReferenceType();
61996202
}
62006203

6201-
inline ClassDecl *TypeBase::getClassOrBoundGenericClass() {
6204+
inline ClassDecl *TypeBase::getClassOrBoundGenericClass() const {
62026205
return getCanonicalType().getClassOrBoundGenericClass();
62036206
}
62046207

@@ -6262,8 +6265,6 @@ inline GenericTypeDecl *TypeBase::getAnyGeneric() {
62626265
return getCanonicalType().getAnyGeneric();
62636266
}
62646267

6265-
6266-
62676268
inline bool TypeBase::isBuiltinIntegerType(unsigned n) {
62686269
if (auto intTy = dyn_cast<BuiltinIntegerType>(getCanonicalType()))
62696270
return intTy->getWidth().isFixedWidth()

include/swift/AST/USRGeneration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
4747
bool printValueDeclUSR(const ValueDecl *D, raw_ostream &OS);
4848

4949
/// Prints out the USR for the given ModuleEntity.
50+
/// In case module aliasing is used, it prints the real module name. For example,
51+
/// if a file has `import Foo` and `-module-alias Foo=Bar` is passed, treat Foo as
52+
/// an alias and Bar as the real module name as its dependency. Note that the
53+
/// aliasing only applies to Swift modules.
5054
/// \returns true if it failed, false on success.
5155
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS);
5256

include/swift/Basic/BlotSetVector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class BlotSetVector {
113113
return {index, true};
114114
}
115115

116+
bool count(const ValueT &value) { return map.count(value); }
117+
116118
/// Replace \p value1 with \p value2 placing \p value2 into the position in
117119
/// the array where value1 used to be. If \p value2 is already in the set,
118120
/// this just erases \p value1.

include/swift/ClangImporter/ClangImporterRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ class CXXNamespaceMemberLookup
132132

133133
/// The input type for a record member lookup request.
134134
struct ClangRecordMemberLookupDescriptor final {
135-
StructDecl *recordDecl;
135+
NominalTypeDecl *recordDecl;
136136
DeclName name;
137137

138-
ClangRecordMemberLookupDescriptor(StructDecl *recordDecl, DeclName name)
138+
ClangRecordMemberLookupDescriptor(NominalTypeDecl *recordDecl, DeclName name)
139139
: recordDecl(recordDecl), name(name) {
140140
assert(isa<clang::RecordDecl>(recordDecl->getClangDecl()));
141141
}

include/swift/Reflection/MetadataSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class MetadataSource {
178178
}
179179

180180
void dump() const;
181-
void dump(FILE *file, unsigned Indent = 0) const;
181+
void dump(std::ostream &stream, unsigned Indent = 0) const;
182182
template <typename Allocator>
183183
static const MetadataSource *decode(Allocator &A, const std::string &str) {
184184
auto begin = str.begin();

include/swift/Reflection/TypeLowering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class TypeInfo {
145145
bool isBitwiseTakable() const { return BitwiseTakable; }
146146

147147
void dump() const;
148-
void dump(FILE *file, unsigned Indent = 0) const;
148+
void dump(std::ostream &stream, unsigned Indent = 0) const;
149149

150150
// Using the provided reader, inspect our value.
151151
// Return false if we can't inspect value.

include/swift/Reflection/TypeRef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "swift/ABI/MetadataValues.h"
2626
#include "swift/Remote/MetadataReader.h"
2727
#include "swift/Basic/Unreachable.h"
28-
28+
#include <ostream>
2929
namespace swift {
3030
namespace reflection {
3131

@@ -177,7 +177,7 @@ class alignas(8) TypeRef {
177177
}
178178

179179
void dump() const;
180-
void dump(FILE *file, unsigned Indent = 0) const;
180+
void dump(std::ostream &stream, unsigned Indent = 0) const;
181181

182182
/// Build a demangle tree from this TypeRef.
183183
Demangle::NodePointer getDemangling(Demangle::Demangler &Dem) const;

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
#include "swift/Reflection/TypeLowering.h"
2525
#include "swift/Reflection/TypeRef.h"
2626
#include "llvm/ADT/Optional.h"
27-
#include <vector>
27+
#include <ostream>
2828
#include <unordered_map>
29+
#include <vector>
2930

3031
namespace swift {
3132
namespace reflection {
@@ -209,7 +210,7 @@ struct ClosureContextInfo {
209210
unsigned NumBindings = 0;
210211

211212
void dump() const;
212-
void dump(FILE *file) const;
213+
void dump(std::ostream &stream) const;
213214
};
214215

215216
struct FieldTypeInfo {
@@ -643,6 +644,10 @@ class TypeRefBuilder {
643644

644645
private:
645646
std::vector<ReflectionInfo> ReflectionInfos;
647+
648+
/// Index of the next Reflection Info that should be processed.
649+
/// This assumes that Reflection Infos are never removed from the vector.
650+
size_t FirstUnprocessedReflectionInfoIndex = 0;
646651

647652
llvm::Optional<std::string> normalizeReflectionName(RemoteRef<char> name);
648653
bool reflectionNameMatches(RemoteRef<char> reflectionName,
@@ -728,12 +733,12 @@ class TypeRefBuilder {
728733
///
729734

730735
void dumpTypeRef(RemoteRef<char> MangledName,
731-
FILE *file, bool printTypeName = false);
732-
void dumpFieldSection(FILE *file);
733-
void dumpAssociatedTypeSection(FILE *file);
734-
void dumpBuiltinTypeSection(FILE *file);
735-
void dumpCaptureSection(FILE *file);
736-
void dumpAllSections(FILE *file);
736+
std::ostream &stream, bool printTypeName = false);
737+
void dumpFieldSection(std::ostream &stream);
738+
void dumpAssociatedTypeSection(std::ostream &stream);
739+
void dumpBuiltinTypeSection(std::ostream &stream);
740+
void dumpCaptureSection(std::ostream &stream);
741+
void dumpAllSections(std::ostream &stream);
737742
};
738743

739744

include/swift/SIL/OwnershipUtils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SILModule;
3131
class SILValue;
3232
class DeadEndBlocks;
3333
class PrunedLiveness;
34+
struct BorrowedValue;
3435

3536
/// Returns true if v is an address or trivial.
3637
bool isValueAddressOrTrivial(SILValue v);
@@ -103,6 +104,15 @@ inline bool isForwardingConsume(SILValue value) {
103104
bool findInnerTransitiveGuaranteedUses(
104105
SILValue guaranteedValue, SmallVectorImpl<Operand *> *usePoints = nullptr);
105106

107+
/// Like findInnerTransitiveGuaranteedUses except that rather than it being a
108+
/// precondition that the provided value not be a BorrowedValue, it is a [type-
109+
/// system-enforced] precondition that the provided value be a BorrowedValue.
110+
///
111+
/// TODO: Merge with findInnerTransitiveGuaranteedUses.
112+
bool findInnerTransitiveGuaranteedUsesOfBorrowedValue(
113+
BorrowedValue borrowedValue,
114+
SmallVectorImpl<Operand *> *usePoints = nullptr);
115+
106116
/// Find leaf "use points" of a guaranteed value within its enclosing borrow
107117
/// scope (without looking through reborrows). To find the use points of the
108118
/// extended borrow scope, after looking through reborrows, use
@@ -1196,6 +1206,14 @@ void visitTransitiveEndBorrows(
11961206
BorrowedValue beginBorrow,
11971207
function_ref<void(EndBorrowInst *)> visitEndBorrow);
11981208

1209+
/// Whether the specified lexical begin_borrow instruction is nested.
1210+
///
1211+
/// A begin_borrow [lexical] is nested if the borrowed value's lifetime is
1212+
/// guaranteed by another lexical scope. That happens if:
1213+
/// - the value is a guaranteed argument to the function
1214+
/// - the value is itself a begin_borrow [lexical]
1215+
bool isNestedLexicalBeginBorrow(BeginBorrowInst *bbi);
1216+
11991217
} // namespace swift
12001218

12011219
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
630630
/// Can this instruction abort the program in some manner?
631631
bool mayTrap() const;
632632

633+
/// Involves a synchronization point like a memory barrier, lock or syscall.
634+
///
635+
/// TODO: We need side-effect analysis and library annotation for this to be
636+
/// a reasonable API. For now, this is just a placeholder.
637+
bool maySynchronize() const;
638+
633639
/// Returns true if the given instruction is completely identical to RHS.
634640
bool isIdenticalTo(const SILInstruction *RHS) const {
635641
return isIdenticalTo(RHS,

include/swift/SIL/SILType.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ class SILType {
236236
NominalTypeDecl *getNominalOrBoundGenericNominal() const {
237237
return getASTType().getNominalOrBoundGenericNominal();
238238
}
239-
239+
240+
/// If this type maps to a Swift class, check if that class is a foreign
241+
/// reference type.
242+
bool isForeignReferenceType() const {
243+
return getASTType().isForeignReferenceType();
244+
}
245+
240246
/// True if the type is an address type.
241247
bool isAddress() const { return getCategory() == SILValueCategory::Address; }
242248

include/swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
/// deleted, which in turn allows canonicalization of the outer owned values
1717
/// (via CanonicalizeOSSALifetime).
1818
///
19-
/// This does not shrink borrow scopes; it does not rewrite end_borrows.
20-
///
21-
/// TODO: A separate utility to shrink borrow scopes should eventually run
22-
/// before this utility. It should hoist end_borrow up to the latest "destroy
23-
/// barrier" whenever the scope does not contain a PointerEscape.
19+
/// This does not shrink borrow scopes; it does not rewrite end_borrows. For
20+
/// that, see ShrinkBorrowScope.
2421
///
2522
//===----------------------------------------------------------------------===//
2623

@@ -150,6 +147,8 @@ class CanonicalizeBorrowScope {
150147
bool consolidateBorrowScope();
151148
};
152149

150+
bool shrinkBorrowScope(BeginBorrowInst *bbi, InstructionDeleter &deleter);
151+
153152
} // namespace swift
154153

155154
#endif // SWIFT_SILOPTIMIZER_UTILS_CANONICALIZEBORROWSCOPES_H

lib/AST/ClangTypeConverter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,8 @@ ClangTypeConverter::visitBoundGenericType(BoundGenericType *type) {
545545
auto args = type->getGenericArgs();
546546
assert((args.size() == 1) && "Optional should have 1 generic argument.");
547547
clang::QualType innerTy = convert(args[0]);
548-
if (swift::canImportAsOptional(innerTy.getTypePtrOrNull()))
548+
if (swift::canImportAsOptional(innerTy.getTypePtrOrNull()) ||
549+
args[0]->isForeignReferenceType())
549550
return innerTy;
550551
return clang::QualType();
551552
}

0 commit comments

Comments
 (0)