@@ -3962,9 +3962,11 @@ enum class AccessorKind {
3962
3962
#include " swift/AST/AccessorKinds.def"
3963
3963
};
3964
3964
3965
+ const unsigned NumAccessorKinds = unsigned (AccessorKind::Last) + 1 ;
3966
+
3965
3967
static inline IntRange<AccessorKind> allAccessorKinds () {
3966
3968
return IntRange<AccessorKind>(AccessorKind (0 ),
3967
- AccessorKind (unsigned (AccessorKind::Last) + 1 ));
3969
+ AccessorKind (NumAccessorKinds ));
3968
3970
}
3969
3971
3970
3972
// / The safety semantics of this addressor.
@@ -4121,66 +4123,66 @@ class AbstractStorageDecl : public ValueDecl {
4121
4123
// / return the address at which the object is stored.
4122
4124
ComputedWithMutableAddress,
4123
4125
};
4126
+
4127
+ static const size_t MaxNumAccessors = 255 ;
4124
4128
private:
4125
4129
AbstractStorageDecl *OverriddenDecl;
4126
4130
4127
- struct GetSetRecord ;
4128
-
4129
- // / This is stored immediately before the GetSetRecord.
4130
- struct alignas (1 << 3 ) AddressorRecord {
4131
- AccessorDecl *Address = nullptr ; // User-defined address accessor
4132
- AccessorDecl *MutableAddress = nullptr ; // User-defined mutableAddress accessor
4133
-
4134
- GetSetRecord *getGetSet () {
4135
- // Relies on not-strictly-portable ABI layout assumptions.
4136
- return reinterpret_cast <GetSetRecord*>(this +1 );
4137
- }
4138
- };
4139
- void configureAddressorRecord (AddressorRecord *record,
4140
- AccessorDecl *addressor, AccessorDecl *mutableAddressor);
4131
+ // / A record of the accessors for the declaration.
4132
+ class alignas (1 << 3 ) AccessorRecord final :
4133
+ private llvm::TrailingObjects<AccessorRecord, AccessorDecl*> {
4134
+ friend TrailingObjects;
4135
+
4136
+ using AccessorIndex = uint8_t ;
4137
+ static const AccessorIndex InvalidIndex = 0 ;
4141
4138
4142
- struct alignas (1 << 3 ) GetSetRecord {
4143
4139
SourceRange Braces;
4144
- AccessorDecl *Get = nullptr ; // User-defined getter
4145
- AccessorDecl *Set = nullptr ; // User-defined setter
4146
- AccessorDecl *MaterializeForSet = nullptr ; // optional materializeForSet accessor
4140
+ AccessorIndex NumAccessors;
4141
+
4142
+ // / The storage capacity of this record for accessors. Always includes
4143
+ // / enough space for adding opaque accessors to the record, which are the
4144
+ // / only accessors that should ever be added retroactively; hence this
4145
+ // / field is only here for the purposes of safety checks.
4146
+ AccessorIndex AccessorsCapacity;
4147
+
4148
+ // / Either 0, meaning there is no registered accessor of the given kind,
4149
+ // / or the index+1 of the accessor in the accessors array.
4150
+ AccessorIndex AccessorIndices[NumAccessorKinds];
4151
+
4152
+ AccessorRecord (SourceRange braces, ArrayRef<AccessorDecl*> accessors,
4153
+ AccessorIndex accessorsCapacity);
4154
+ public:
4155
+ static AccessorRecord *create (ASTContext &ctx, SourceRange braces,
4156
+ ArrayRef<AccessorDecl*> accessors);
4157
+
4158
+ SourceRange getBracesRange () const { return Braces; }
4159
+
4160
+ inline AccessorDecl *getAccessor (AccessorKind kind) const ;
4147
4161
4148
- AddressorRecord *getAddressors () {
4149
- // Relies on not-strictly-portable ABI layout assumptions.
4150
- return reinterpret_cast <AddressorRecord*>(this ) - 1 ;
4162
+ ArrayRef<AccessorDecl *> getAllAccessors () const {
4163
+ return { getTrailingObjects<AccessorDecl*>(), NumAccessors };
4151
4164
}
4165
+
4166
+ void addOpaqueAccessor (AccessorDecl *accessor);
4167
+
4168
+ private:
4169
+ MutableArrayRef<AccessorDecl *> getAccessorsBuffer () {
4170
+ return { getTrailingObjects<AccessorDecl*>(), NumAccessors };
4171
+ }
4172
+
4173
+ bool registerAccessor (AccessorDecl *accessor, AccessorIndex index);
4152
4174
};
4153
- void configureGetSetRecord (GetSetRecord *getSetRecord,
4154
- AccessorDecl *getter, AccessorDecl *setter,
4155
- AccessorDecl *materializeForSet);
4156
- void configureSetRecord (GetSetRecord *getSetInfo,
4157
- AccessorDecl *setter,
4158
- AccessorDecl *materializeForSet);
4159
-
4160
- struct ObservingRecord : GetSetRecord {
4161
- AccessorDecl *WillSet = nullptr ; // willSet(value):
4162
- AccessorDecl *DidSet = nullptr ; // didSet:
4163
- };
4164
- void configureObservingRecord (ObservingRecord *record,
4165
- AccessorDecl *willSet, AccessorDecl *didSet);
4166
4175
4167
- llvm::PointerIntPair<GetSetRecord *, 3 , OptionalEnum<AccessLevel>> GetSetInfo ;
4176
+ llvm::PointerIntPair<AccessorRecord *, 3 , OptionalEnum<AccessLevel>> Accessors ;
4168
4177
llvm::PointerIntPair<BehaviorRecord*, 3 , OptionalEnum<AccessLevel>>
4169
4178
BehaviorInfo;
4170
4179
4171
- ObservingRecord &getDidSetInfo () const {
4172
- assert (hasObservers ());
4173
- return *static_cast <ObservingRecord*>(GetSetInfo.getPointer ());
4174
- }
4175
- AddressorRecord &getAddressorInfo () const {
4176
- assert (hasAddressors ());
4177
- return *GetSetInfo.getPointer ()->getAddressors ();
4178
- }
4179
-
4180
4180
void setStorageKind (StorageKindTy K) {
4181
4181
Bits.AbstractStorageDecl .StorageKind = unsigned (K);
4182
4182
}
4183
4183
4184
+ void configureAccessor (AccessorDecl *accessor);
4185
+
4184
4186
protected:
4185
4187
AbstractStorageDecl (DeclKind Kind, DeclContext *DC, DeclName Name,
4186
4188
SourceLoc NameLoc)
@@ -4308,55 +4310,26 @@ class AbstractStorageDecl : public ValueDecl {
4308
4310
Bits.AbstractStorageDecl .IsSetterMutating = isMutating;
4309
4311
}
4310
4312
4311
- AccessorDecl *getAccessorFunction (AccessorKind accessor ) const ;
4312
-
4313
- // / \brief Push all of the accessor functions associated with this VarDecl
4314
- // / onto `decls`.
4315
- void getAllAccessorFunctions (SmallVectorImpl<Decl *> &decls) const ;
4313
+ AccessorDecl *getAccessorFunction (AccessorKind kind ) const {
4314
+ if ( auto info = Accessors. getPointer ())
4315
+ return info-> getAccessor (kind);
4316
+ return nullptr ;
4317
+ }
4316
4318
4317
- // / \brief Turn this into a computed variable, providing a getter and setter.
4318
- void makeComputed (SourceLoc LBraceLoc, AccessorDecl *Get, AccessorDecl *Set,
4319
- AccessorDecl *MaterializeForSet, SourceLoc RBraceLoc);
4319
+ ArrayRef<AccessorDecl*> getAllAccessorFunctions () const {
4320
+ if (const auto *info = Accessors.getPointer ())
4321
+ return info->getAllAccessors ();
4322
+ return {};
4323
+ }
4320
4324
4321
- // / \brief Turn this into a computed object, providing a getter and a mutable
4322
- // / addressor.
4323
- void makeComputedWithMutableAddress (SourceLoc lbraceLoc,
4324
- AccessorDecl *getter,
4325
- AccessorDecl *setter,
4326
- AccessorDecl *materializeForSet,
4327
- AccessorDecl *mutableAddressor,
4328
- SourceLoc rbraceLoc);
4325
+ void setAccessors (StorageKindTy storageKind,
4326
+ SourceLoc lbraceLoc, ArrayRef<AccessorDecl*> accessors,
4327
+ SourceLoc rbraceLoc);
4329
4328
4330
4329
// / \brief Add trivial accessors to this Stored or Addressed object.
4331
4330
void addTrivialAccessors (AccessorDecl *Get, AccessorDecl *Set,
4332
4331
AccessorDecl *MaterializeForSet);
4333
4332
4334
- // / \brief Turn this into a stored-with-observers var, providing the
4335
- // / didSet/willSet specifiers.
4336
- void makeStoredWithObservers (SourceLoc LBraceLoc, AccessorDecl *WillSet,
4337
- AccessorDecl *DidSet, SourceLoc RBraceLoc);
4338
-
4339
- // / \brief Turn this into an inherited-with-observers var, providing
4340
- // / the didSet/willSet specifiers.
4341
- void makeInheritedWithObservers (SourceLoc LBraceLoc, AccessorDecl *WillSet,
4342
- AccessorDecl *DidSet, SourceLoc RBraceLoc);
4343
-
4344
- // / \brief Turn this into an addressed var.
4345
- void makeAddressed (SourceLoc LBraceLoc, AccessorDecl *Addressor,
4346
- AccessorDecl *MutableAddressor,
4347
- SourceLoc RBraceLoc);
4348
-
4349
- // / \brief Turn this into an addressed var with observing accessors.
4350
- void makeAddressedWithObservers (SourceLoc LBraceLoc, AccessorDecl *Addressor,
4351
- AccessorDecl *MutableAddressor,
4352
- AccessorDecl *WillSet, AccessorDecl *DidSet,
4353
- SourceLoc RBraceLoc);
4354
-
4355
- // / \brief Specify the synthesized get/set functions for a
4356
- // / StoredWithObservers or AddressedWithObservers var. This is used by Sema.
4357
- void setObservingAccessors (AccessorDecl *Get, AccessorDecl *Set,
4358
- AccessorDecl *MaterializeForSet);
4359
-
4360
4333
// / \brief Add a setter to an existing Computed var.
4361
4334
// /
4362
4335
// / This should only be used by the ClangImporter.
@@ -4370,39 +4343,30 @@ class AbstractStorageDecl : public ValueDecl {
4370
4343
// / This should only be used by Sema.
4371
4344
void setMaterializeForSetFunc (AccessorDecl *materializeForSet);
4372
4345
4373
- // / \brief Specify the braces range without adding accessors.
4374
- // /
4375
- // / This is used to record the braces range if the accessors were rejected.
4376
- void setInvalidBracesRange (SourceRange BracesRange);
4377
-
4378
4346
SourceRange getBracesRange () const {
4379
- if (auto info = GetSetInfo .getPointer ())
4380
- return info->Braces ;
4347
+ if (auto info = Accessors .getPointer ())
4348
+ return info->getBracesRange () ;
4381
4349
return SourceRange ();
4382
4350
}
4383
4351
4384
4352
// / \brief Retrieve the getter used to access the value of this variable.
4385
4353
AccessorDecl *getGetter () const {
4386
- if (auto info = GetSetInfo.getPointer ())
4387
- return info->Get ;
4388
- return nullptr ;
4354
+ return getAccessorFunction (AccessorKind::Get);
4389
4355
}
4390
4356
4391
4357
// / \brief Retrieve the setter used to mutate the value of this variable.
4392
4358
AccessorDecl *getSetter () const {
4393
- if (auto info = GetSetInfo.getPointer ())
4394
- return info->Set ;
4395
- return nullptr ;
4359
+ return getAccessorFunction (AccessorKind::Set);
4396
4360
}
4397
4361
4398
4362
AccessLevel getSetterFormalAccess () const {
4399
4363
assert (hasAccess ());
4400
- assert (GetSetInfo .getInt ().hasValue ());
4401
- return GetSetInfo .getInt ().getValue ();
4364
+ assert (Accessors .getInt ().hasValue ());
4365
+ return Accessors .getInt ().getValue ();
4402
4366
}
4403
4367
4404
4368
void setSetterAccess (AccessLevel accessLevel) {
4405
- assert (!GetSetInfo .getInt ().hasValue ());
4369
+ assert (!Accessors .getInt ().hasValue ());
4406
4370
overwriteSetterAccess (accessLevel);
4407
4371
}
4408
4372
@@ -4411,19 +4375,18 @@ class AbstractStorageDecl : public ValueDecl {
4411
4375
// / \brief Retrieve the materializeForSet function, if this
4412
4376
// / declaration has one.
4413
4377
AccessorDecl *getMaterializeForSetFunc () const {
4414
- if (auto info = GetSetInfo.getPointer ())
4415
- return info->MaterializeForSet ;
4416
- return nullptr ;
4378
+ return getAccessorFunction (AccessorKind::MaterializeForSet);
4417
4379
}
4418
4380
4419
- // / \brief Return the decl for the 'address' accessor if it
4420
- // / exists; this is only valid on a declaration with addressors.
4421
- AccessorDecl *getAddressor () const { return getAddressorInfo ().Address ; }
4381
+ // / \brief Return the decl for the immutable addressor if it exists.
4382
+ AccessorDecl *getAddressor () const {
4383
+ return getAccessorFunction (AccessorKind::Address);
4384
+ }
4422
4385
4423
4386
// / \brief Return the decl for the 'mutableAddress' accessors if
4424
4387
// / it exists; this is only valid on a declaration with addressors.
4425
4388
AccessorDecl *getMutableAddressor () const {
4426
- return getAddressorInfo (). MutableAddress ;
4389
+ return getAccessorFunction (AccessorKind:: MutableAddress) ;
4427
4390
}
4428
4391
4429
4392
// / \brief Return the appropriate addressor for the given access kind.
@@ -4435,11 +4398,15 @@ class AbstractStorageDecl : public ValueDecl {
4435
4398
4436
4399
// / \brief Return the decl for the willSet specifier if it exists, this is
4437
4400
// / only valid on a declaration with Observing storage.
4438
- AccessorDecl *getWillSetFunc () const { return getDidSetInfo ().WillSet ; }
4401
+ AccessorDecl *getWillSetFunc () const {
4402
+ return getAccessorFunction (AccessorKind::WillSet);
4403
+ }
4439
4404
4440
4405
// / \brief Return the decl for the didSet specifier if it exists, this is
4441
4406
// / only valid on a declaration with Observing storage.
4442
- AccessorDecl *getDidSetFunc () const { return getDidSetInfo ().DidSet ; }
4407
+ AccessorDecl *getDidSetFunc () const {
4408
+ return getAccessorFunction (AccessorKind::DidSet);
4409
+ }
4443
4410
4444
4411
// / Given that this is an Objective-C property or subscript declaration,
4445
4412
// / produce its getter selector.
@@ -5777,6 +5744,16 @@ inline ParameterList **FuncDecl::getParameterListBuffer() {
5777
5744
}
5778
5745
return reinterpret_cast <ParameterList**>(static_cast <AccessorDecl*>(this )+1 );
5779
5746
}
5747
+
5748
+ inline AccessorDecl *
5749
+ AbstractStorageDecl::AccessorRecord::getAccessor (AccessorKind kind) const {
5750
+ if (auto optIndex = AccessorIndices[unsigned (kind)]) {
5751
+ auto accessor = getAllAccessors ()[optIndex - 1 ];
5752
+ assert (accessor && accessor->getAccessorKind () == kind);
5753
+ return accessor;
5754
+ }
5755
+ return nullptr ;
5756
+ }
5780
5757
5781
5758
// / \brief This represents a 'case' declaration in an 'enum', which may declare
5782
5759
// / one or more individual comma-separated EnumElementDecls.
@@ -6652,7 +6629,7 @@ ::operator()(Decl *decl) const {
6652
6629
6653
6630
inline void
6654
6631
AbstractStorageDecl::overwriteSetterAccess (AccessLevel accessLevel) {
6655
- GetSetInfo .setInt (accessLevel);
6632
+ Accessors .setInt (accessLevel);
6656
6633
if (auto setter = getSetter ())
6657
6634
setter->overwriteAccess (accessLevel);
6658
6635
if (auto materializeForSet = getMaterializeForSetFunc ())
0 commit comments