Skip to content

Commit 6382ed6

Browse files
authored
Merge pull request swiftlang#6035 from jckarter/use-sil-box-field-accessors
2 parents 1d03e05 + 277608a commit 6382ed6

File tree

134 files changed

+2809
-2186
lines changed

Some content is hidden

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

134 files changed

+2809
-2186
lines changed

include/swift/AST/ASTContext.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class ASTContext {
168168
struct Implementation;
169169
Implementation &Impl;
170170

171-
friend class ConstraintCheckerArenaRAII;
171+
friend ConstraintCheckerArenaRAII;
172172
public:
173173
ASTContext(LangOptions &langOpts, SearchPathOptions &SearchPathOpts,
174174
SourceManager &SourceMgr, DiagnosticEngine &Diags);
@@ -787,25 +787,43 @@ class ASTContext {
787787
bool isSwiftVersion3() const { return LangOpts.isSwiftVersion3(); }
788788

789789
private:
790-
friend class Decl;
790+
friend Decl;
791791
Optional<RawComment> getRawComment(const Decl *D);
792792
void setRawComment(const Decl *D, RawComment RC);
793793

794794
Optional<StringRef> getBriefComment(const Decl *D);
795795
void setBriefComment(const Decl *D, StringRef Comment);
796796

797-
friend class TypeBase;
797+
friend TypeBase;
798798

799799
/// \brief Set the substitutions for the given bound generic type.
800800
void setSubstitutions(TypeBase *type,
801801
DeclContext *gpContext,
802802
ArrayRef<Substitution> Subs) const;
803803

804-
/// Provide context-level uniquing for SIL lowered type layouts.
804+
/// Retrieve the archetype builder and potential archetype
805+
/// corresponding to the given archetype type.
806+
///
807+
/// This facility is only used by the archetype builder when forming
808+
/// archetypes.a
809+
std::pair<ArchetypeBuilder *, ArchetypeBuilder::PotentialArchetype *>
810+
getLazyArchetype(const ArchetypeType *archetype);
811+
812+
/// Register information for a lazily-constructed archetype.
813+
void registerLazyArchetype(
814+
const ArchetypeType *archetype,
815+
ArchetypeBuilder &builder,
816+
ArchetypeBuilder::PotentialArchetype *potentialArchetype);
817+
818+
/// Unregister information about the given lazily-constructed archetype.
819+
void unregisterLazyArchetype(const ArchetypeType *archetype);
820+
821+
friend ArchetypeType;
822+
friend ArchetypeBuilder::PotentialArchetype;
823+
824+
/// Provide context-level uniquing for SIL lowered type layouts and boxes.
805825
friend SILLayout;
806-
llvm::FoldingSet<SILLayout> *&getSILLayouts();
807826
friend SILBoxType;
808-
llvm::DenseMap<CanType, SILBoxType *> &getSILBoxTypes();
809827
};
810828

811829
/// Retrieve information about the given Objective-C method for

include/swift/AST/DiagnosticsParse.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@ WARNING(deprecated_protocol_composition_single,none,
681681
WARNING(deprecated_any_composition,none,
682682
"'protocol<>' syntax is deprecated; use 'Any' instead", ())
683683

684+
// SIL box Types
685+
ERROR(sil_box_expected_var_or_let,none,
686+
"expected 'var' or 'let' to introduce SIL box field type", ())
687+
ERROR(sil_box_expected_r_brace,none,
688+
"expected '}' to complete SIL box field type list", ())
689+
ERROR(sil_box_expected_r_angle,none,
690+
"expected '>' to complete SIL box generic argument list", ())
691+
684692
//------------------------------------------------------------------------------
685693
// Pattern parsing diagnostics
686694
//------------------------------------------------------------------------------

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2840,7 +2840,8 @@ ERROR(unsupported_sil_convention,none,
28402840
"convention '%0' not supported in SIL", (StringRef))
28412841
ERROR(illegal_sil_type,none,
28422842
"type %0 is not a legal SIL value type", (Type))
2843-
2843+
ERROR(sil_box_arg_mismatch,none,
2844+
"SIL box type has wrong number of generic arguments for layout", ())
28442845
// SIL Metatypes
28452846
ERROR(sil_metatype_without_repr,none,
28462847
"metatypes in SIL must have @thin, @thick, or @objc_metatype attribute",

include/swift/AST/GenericSignature.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ class alignas(1 << TypeAlignInBits) GenericSignature final
129129
SubstitutionMap &subMap) const;
130130

131131
using LookupConformanceFn =
132-
llvm::function_ref<ProtocolConformanceRef(CanType, Type, ProtocolType *)>;
132+
llvm::function_ref<ProtocolConformanceRef(CanType dependentType,
133+
Type conformingReplacementType,
134+
ProtocolType *conformedProtocol)>;
133135

134136
/// Build an array of substitutions from an interface type substitution map,
135137
/// using the given function to look up conformances.

include/swift/SIL/SILLayout.h renamed to include/swift/AST/SILLayout.h

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
#include "llvm/ADT/PointerIntPair.h"
3232
#include "swift/AST/GenericSignature.h"
3333
#include "swift/AST/Identifier.h"
34-
#include "swift/AST/Types.h"
35-
#include "swift/SIL/SILAllocated.h"
36-
#include "swift/SIL/SILType.h"
34+
#include "swift/AST/Type.h"
3735

3836
namespace swift {
3937

38+
class SILType;
39+
4040
/// A field of a SIL aggregate layout.
4141
class SILField final {
4242
enum : unsigned {
@@ -68,12 +68,8 @@ class SILField final {
6868
/// in this aggregate should be lowered.
6969
CanType getLoweredType() const { return LoweredTypeAndFlags.getPointer(); }
7070

71-
SILType getAddressType() const {
72-
return SILType::getPrimitiveAddressType(getLoweredType());
73-
}
74-
SILType getObjectType() const {
75-
return SILType::getPrimitiveObjectType(getLoweredType());
76-
}
71+
SILType getAddressType() const; // In SILType.h
72+
SILType getObjectType() const; // In SILType.h
7773

7874
/// True if this field is mutable inside its aggregate.
7975
///
@@ -149,17 +145,6 @@ class SILLayout final : public llvm::FoldingSetNode,
149145
}
150146
};
151147

152-
inline SILType SILBoxType::getFieldType(unsigned index) const {
153-
auto fieldTy = getLayout()->getFields()[index].getLoweredType();
154-
// Apply generic arguments if the layout is generic.
155-
if (!getGenericArgs().empty()) {
156-
auto substMap =
157-
getLayout()->getGenericSignature()->getSubstitutionMap(getGenericArgs());
158-
fieldTy = fieldTy.subst(substMap)->getCanonicalType();
159-
}
160-
return SILType::getPrimitiveAddressType(fieldTy);
161-
}
162-
163148
} // end namespace swift
164149

165150
#endif // SWIFT_SIL_LAYOUT_H

include/swift/AST/TypeRepr.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,86 @@ class FixedTypeRepr : public TypeRepr {
859859
friend class TypeRepr;
860860
};
861861

862+
/// SIL-only TypeRepr for box types.
863+
///
864+
/// Boxes are either concrete: { var Int, let String }
865+
/// or generic: <T: Runcible> { var T, let String } <Int>
866+
class SILBoxTypeRepr : public TypeRepr {
867+
GenericParamList *GenericParams;
868+
GenericEnvironment *GenericEnv = nullptr;
869+
870+
SourceLoc LBraceLoc, RBraceLoc;
871+
SourceLoc ArgLAngleLoc, ArgRAngleLoc;
872+
873+
public:
874+
struct Field {
875+
SourceLoc VarOrLetLoc;
876+
bool Mutable;
877+
TypeRepr *FieldType;
878+
};
879+
880+
private:
881+
ArrayRef<Field> Fields;
882+
ArrayRef<TypeRepr *> GenericArgs;
883+
884+
public:
885+
SILBoxTypeRepr(GenericParamList *GenericParams,
886+
SourceLoc LBraceLoc, ArrayRef<Field> Fields,
887+
SourceLoc RBraceLoc,
888+
SourceLoc ArgLAngleLoc, ArrayRef<TypeRepr *> GenericArgs,
889+
SourceLoc ArgRAngleLoc)
890+
: TypeRepr(TypeReprKind::SILBox),
891+
GenericParams(GenericParams), LBraceLoc(LBraceLoc), RBraceLoc(RBraceLoc),
892+
ArgLAngleLoc(ArgLAngleLoc), ArgRAngleLoc(ArgRAngleLoc),
893+
Fields(Fields), GenericArgs(GenericArgs)
894+
{}
895+
896+
static SILBoxTypeRepr *create(ASTContext &C,
897+
GenericParamList *GenericParams,
898+
SourceLoc LBraceLoc, ArrayRef<Field> Fields,
899+
SourceLoc RBraceLoc,
900+
SourceLoc ArgLAngleLoc, ArrayRef<TypeRepr *> GenericArgs,
901+
SourceLoc ArgRAngleLoc);
902+
903+
void setGenericEnvironment(GenericEnvironment *Env) {
904+
assert(!GenericEnv);
905+
GenericEnv = Env;
906+
}
907+
908+
ArrayRef<Field> getFields() const {
909+
return Fields;
910+
}
911+
ArrayRef<TypeRepr *> getGenericArguments() const {
912+
return GenericArgs;
913+
}
914+
915+
GenericParamList *getGenericParams() const {
916+
return GenericParams;
917+
}
918+
GenericSignature *getGenericSignature() const {
919+
return GenericEnv->getGenericSignature();
920+
}
921+
GenericEnvironment *getGenericEnvironment() const {
922+
return GenericEnv;
923+
}
924+
925+
SourceLoc getLBraceLoc() const { return LBraceLoc; }
926+
SourceLoc getRBraceLoc() const { return RBraceLoc; }
927+
SourceLoc getArgumentLAngleLoc() const { return ArgLAngleLoc; }
928+
SourceLoc getArgumentRAngleLoc() const { return ArgRAngleLoc; }
929+
930+
static bool classof(const TypeRepr *T) {
931+
return T->getKind() == TypeReprKind::SILBox;
932+
}
933+
static bool classof(const SILBoxTypeRepr *T) { return true; }
934+
935+
private:
936+
SourceLoc getStartLocImpl() const;
937+
SourceLoc getEndLocImpl() const;
938+
SourceLoc getLocImpl() const;
939+
void printImpl(ASTPrinter &Printer, const PrintOptions &Opts) const;
940+
friend TypeRepr;
941+
};
862942

863943
inline bool TypeRepr::isSimple() const {
864944
switch (getKind()) {
@@ -879,6 +959,7 @@ inline bool TypeRepr::isSimple() const {
879959
case TypeReprKind::Tuple:
880960
case TypeReprKind::Fixed:
881961
case TypeReprKind::Array:
962+
case TypeReprKind::SILBox:
882963
return true;
883964
}
884965
llvm_unreachable("bad TypeRepr kind");

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ TYPEREPR(Metatype, TypeRepr)
5151
TYPEREPR(Protocol, TypeRepr)
5252
TYPEREPR(InOut, TypeRepr)
5353
TYPEREPR(Fixed, TypeRepr)
54+
TYPEREPR(SILBox, TypeRepr)
5455

5556
#undef ABSTRACT_TYPEREPR
5657
#undef TYPEREPR

include/swift/AST/Types.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/GenericParamKey.h"
2222
#include "swift/AST/Ownership.h"
2323
#include "swift/AST/Requirement.h"
24+
#include "swift/AST/SILLayout.h"
2425
#include "swift/AST/Substitution.h"
2526
#include "swift/AST/Type.h"
2627
#include "swift/AST/TypeAlignments.h"
@@ -3332,10 +3333,12 @@ class SILBoxType;
33323333
class SILLayout; // From SIL
33333334
typedef CanTypeWrapper<SILBoxType> CanSILBoxType;
33343335

3335-
/// The SIL-only type @box T, which represents a reference to a (non-class)
3336+
/// The SIL-only type for boxes, which represent a reference to a (non-class)
33363337
/// refcounted value referencing an aggregate with a given lowered layout.
33373338
class SILBoxType final : public TypeBase,
3338-
private llvm::TrailingObjects<SILBoxType,Substitution>{
3339+
public llvm::FoldingSetNode,
3340+
private llvm::TrailingObjects<SILBoxType,Substitution>
3341+
{
33393342
friend TrailingObjects;
33403343

33413344
SILLayout *Layout;
@@ -3348,26 +3351,48 @@ class SILBoxType final : public TypeBase,
33483351
SILLayout *Layout, ArrayRef<Substitution> Args);
33493352

33503353
public:
3354+
static CanSILBoxType get(ASTContext &C,
3355+
SILLayout *Layout,
3356+
ArrayRef<Substitution> Args);
3357+
33513358
SILLayout *getLayout() const { return Layout; }
33523359
ArrayRef<Substitution> getGenericArgs() const {
33533360
return llvm::makeArrayRef(getTrailingObjects<Substitution>(),
33543361
NumGenericArgs);
33553362
}
3363+
CanType getFieldLoweredType(unsigned index) const {
3364+
auto fieldTy = getLayout()->getFields()[index].getLoweredType();
3365+
// Apply generic arguments if the layout is generic.
3366+
if (!getGenericArgs().empty()) {
3367+
auto substMap =
3368+
getLayout()->getGenericSignature()->getSubstitutionMap(getGenericArgs());
3369+
fieldTy = fieldTy.subst(substMap)->getCanonicalType();
3370+
}
3371+
return fieldTy;
3372+
}
3373+
SILType getFieldType(unsigned index) const; // In SILType.h
33563374

33573375
// TODO: SILBoxTypes should be explicitly constructed in terms of specific
33583376
// layouts. As a staging mechanism, we expose the old single-boxed-type
33593377
// interface.
3360-
// These functions are implemented in the SIL library instead of the AST.
33613378

33623379
static CanSILBoxType get(CanType BoxedType);
3363-
CanType getBoxedType() const;
3364-
// In SILType.h
3365-
SILType getBoxedAddressType() const;
3366-
SILType getFieldType(unsigned index) const;
3380+
CanType getBoxedType() const; // In SILType.h
3381+
SILType getBoxedAddressType() const; // In SILType.h
33673382

33683383
static bool classof(const TypeBase *T) {
33693384
return T->getKind() == TypeKind::SILBox;
33703385
}
3386+
3387+
/// Produce a profile of this box, for use in a folding set.
3388+
static void Profile(llvm::FoldingSetNodeID &id,
3389+
SILLayout *Layout,
3390+
ArrayRef<Substitution> Args);
3391+
3392+
/// \brief Produce a profile of this box, for use in a folding set.
3393+
void Profile(llvm::FoldingSetNodeID &id) {
3394+
Profile(id, getLayout(), getGenericArgs());
3395+
}
33713396
};
33723397
DEFINE_EMPTY_CAN_TYPE_WRAPPER(SILBoxType, Type)
33733398

include/swift/Parse/Parser.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@ class Parser {
833833

834834
ParserResult<TypeRepr> parseType();
835835
ParserResult<TypeRepr> parseType(Diag<> MessageID,
836-
bool HandleCodeCompletion = true);
836+
bool HandleCodeCompletion = true,
837+
bool IsSILFuncDecl = false);
837838

838839
/// \brief Parse any type, but diagnose all types except type-identifier or
839840
/// type-composition with non-type-identifier.
@@ -864,7 +865,10 @@ class Parser {
864865
ParserResult<TypeRepr> parseTypeIdentifier();
865866
ParserResult<TypeRepr> parseOldStyleProtocolComposition();
866867
ParserResult<CompositionTypeRepr> parseAnyType();
867-
868+
ParserResult<TypeRepr> parseSILBoxType(GenericParamList *generics,
869+
const TypeAttributes &attrs,
870+
Optional<Scope> &GenericsScope);
871+
868872
ParserResult<TupleTypeRepr> parseTypeTupleBody();
869873
ParserResult<TypeRepr> parseTypeArray(TypeRepr *Base);
870874

include/swift/SIL/SILModule.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/AST/Builtins.h"
2222
#include "swift/AST/Module.h"
2323
#include "swift/AST/SILOptions.h"
24+
#include "swift/AST/SILLayout.h"
2425
#include "swift/Basic/LangOptions.h"
2526
#include "swift/Basic/Range.h"
2627
#include "swift/SIL/SILCoverageMap.h"
@@ -29,7 +30,6 @@
2930
#include "swift/SIL/SILFunction.h"
3031
#include "swift/SIL/SILGlobalVariable.h"
3132
#include "swift/SIL/Notifications.h"
32-
#include "swift/SIL/SILLayout.h"
3333
#include "swift/SIL/SILType.h"
3434
#include "swift/SIL/SILVTable.h"
3535
#include "swift/SIL/SILWitnessTable.h"

include/swift/SIL/SILType.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,18 @@ inline SILType SILBoxType::getBoxedAddressType() const {
596596
static inline llvm::hash_code hash_value(SILType V) {
597597
return llvm::hash_value(V.getOpaqueValue());
598598
}
599-
599+
600+
inline SILType SILBoxType::getFieldType(unsigned index) const {
601+
return SILType::getPrimitiveAddressType(getFieldLoweredType(index));
602+
}
603+
604+
inline SILType SILField::getAddressType() const {
605+
return SILType::getPrimitiveAddressType(getLoweredType());
606+
}
607+
inline SILType SILField::getObjectType() const {
608+
return SILType::getPrimitiveObjectType(getLoweredType());
609+
}
610+
600611
} // end swift namespace
601612

602613
namespace llvm {

0 commit comments

Comments
 (0)