@@ -4976,6 +4976,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
4976
4976
4977
4977
using BodySynthesizer = void (*)(AbstractFunctionDecl *);
4978
4978
4979
+ private:
4980
+ ParameterList *Params;
4981
+
4979
4982
protected:
4980
4983
// If a function has a body at all, we have either a parsed body AST node or
4981
4984
// we have saved the end location of the unparsed body.
@@ -5192,48 +5195,32 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
5192
5195
// / depending on the function context.
5193
5196
bool argumentNameIsAPIByDefault () const ;
5194
5197
5195
- // / \brief Returns the "natural" number of argument clauses taken by this
5196
- // / function. This value is one for free-standing functions, and two for
5197
- // / methods.
5198
- unsigned getNumParameterLists () const {
5199
- return Bits.AbstractFunctionDecl .HasImplicitSelfDecl ? 2 : 1 ;
5200
- }
5201
-
5202
- // / \brief Returns the parameter pattern(s) for the function definition that
5203
- // / determine the parameter names bound in the function body.
5204
- // /
5205
- // / The number of "top-level" elements in this pattern will match the number
5206
- // / of argument names in the compound name of the function or constructor.
5207
- MutableArrayRef<ParameterList *> getParameterLists ();
5208
- ArrayRef<const ParameterList *> getParameterLists () const {
5209
- auto paramLists =
5210
- const_cast <AbstractFunctionDecl *>(this )->getParameterLists ();
5211
- return ArrayRef<const ParameterList *>(paramLists.data (),paramLists.size ());
5212
- }
5213
- ParameterList *getParameterList (unsigned i) {
5214
- return getParameterLists ()[i];
5215
- }
5216
- const ParameterList *getParameterList (unsigned i) const {
5217
- return getParameterLists ()[i];
5218
- }
5198
+ // / Retrieve the function's parameter list, not including 'self' if present.
5219
5199
ParameterList *getParameters () {
5220
- return getParameterLists (). back () ;
5200
+ return Params ;
5221
5201
}
5222
5202
const ParameterList *getParameters () const {
5223
- return getParameterLists (). back () ;
5203
+ return Params ;
5224
5204
}
5225
5205
5226
5206
void setParameters (ParamDecl *SelfDecl,
5227
5207
ParameterList *Params);
5228
5208
5229
- // / \brief This method returns the implicit 'self' decl.
5230
- // /
5231
- // / Note that some functions don't have an implicit 'self' decl, for example,
5232
- // / free functions. In this case nullptr is returned.
5209
+ bool hasImplicitSelfDecl () const {
5210
+ return Bits.AbstractFunctionDecl .HasImplicitSelfDecl ;
5211
+ }
5212
+
5213
+ ParamDecl **getImplicitSelfDeclStorage ();
5214
+
5215
+ // / Retrieve the implicit 'self' parameter for methods, or nullptr for free
5216
+ // / functions.
5233
5217
const ParamDecl *getImplicitSelfDecl () const {
5234
5218
return const_cast <AbstractFunctionDecl*>(this )->getImplicitSelfDecl ();
5235
5219
}
5236
- ParamDecl *getImplicitSelfDecl ();
5220
+ ParamDecl *getImplicitSelfDecl () {
5221
+ auto **selfDecl = getImplicitSelfDeclStorage ();
5222
+ return (selfDecl == nullptr ? nullptr : *selfDecl);
5223
+ }
5237
5224
5238
5225
// / Retrieve the declaration that this method overrides, if any.
5239
5226
AbstractFunctionDecl *getOverriddenDecl () const {
@@ -5330,11 +5317,6 @@ class FuncDecl : public AbstractFunctionDecl {
5330
5317
OperatorDecl *Operator = nullptr ;
5331
5318
5332
5319
protected:
5333
- ParameterList **getParameterListBuffer (); // defined inline below
5334
- ParameterList * const *getParameterListBuffer () const {
5335
- return const_cast <FuncDecl*>(this )->getParameterListBuffer ();
5336
- }
5337
-
5338
5320
FuncDecl (DeclKind Kind,
5339
5321
SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
5340
5322
SourceLoc FuncLoc,
@@ -5426,30 +5408,7 @@ class FuncDecl : public AbstractFunctionDecl {
5426
5408
void setSelfAccessKind (SelfAccessKind mod) {
5427
5409
Bits.FuncDecl .SelfAccess = static_cast <unsigned >(mod);
5428
5410
}
5429
-
5430
- // / \brief Returns the parameter lists(s) for the function definition.
5431
- // /
5432
- // / The number of "top-level" elements will match the number of argument names
5433
- // / in the compound name of the function or constructor.
5434
- MutableArrayRef<ParameterList *> getParameterLists () {
5435
- return {getParameterListBuffer (), getNumParameterLists ()};
5436
- }
5437
- ArrayRef<const ParameterList *> getParameterLists () const {
5438
- return {getParameterListBuffer (), getNumParameterLists ()};
5439
- }
5440
- ParameterList *getParameterList (unsigned i) {
5441
- return getParameterLists ()[i];
5442
- }
5443
- const ParameterList *getParameterList (unsigned i) const {
5444
- return getParameterLists ()[i];
5445
- }
5446
- ParameterList *getParameters () {
5447
- return getParameterLists ().back ();
5448
- }
5449
- const ParameterList *getParameters () const {
5450
- return getParameterLists ().back ();
5451
- }
5452
-
5411
+
5453
5412
// / \returns true if this is non-mutating due to applying a 'mutating'
5454
5413
// / attribute. For example a "mutating set" accessor.
5455
5414
bool isExplicitNonMutating () const ;
@@ -5504,7 +5463,9 @@ class FuncDecl : public AbstractFunctionDecl {
5504
5463
void getLocalCaptures (SmallVectorImpl<CapturedValue> &Result) const {
5505
5464
return getCaptureInfo ().getLocalCaptures (Result);
5506
5465
}
5507
-
5466
+
5467
+ ParamDecl **getImplicitSelfDeclStorage ();
5468
+
5508
5469
// / Get the supertype method this method overrides, if any.
5509
5470
FuncDecl *getOverriddenDecl () const {
5510
5471
return cast_or_null<FuncDecl>(AbstractFunctionDecl::getOverriddenDecl ());
@@ -5672,14 +5633,6 @@ class AccessorDecl final : public FuncDecl {
5672
5633
}
5673
5634
};
5674
5635
5675
- inline ParameterList **FuncDecl::getParameterListBuffer () {
5676
- if (!isa<AccessorDecl>(this )) {
5677
- assert (getKind () == DeclKind::Func && " no new kinds of functions" );
5678
- return reinterpret_cast <ParameterList**>(this +1 );
5679
- }
5680
- return reinterpret_cast <ParameterList**>(static_cast <AccessorDecl*>(this )+1 );
5681
- }
5682
-
5683
5636
inline AccessorDecl *
5684
5637
AbstractStorageDecl::AccessorRecord::getAccessor (AccessorKind kind) const {
5685
5638
if (auto optIndex = AccessorIndices[unsigned (kind)]) {
@@ -5903,7 +5856,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
5903
5856
// / The location of the '!' or '?' for a failable initializer.
5904
5857
SourceLoc FailabilityLoc;
5905
5858
5906
- ParameterList *ParameterLists[ 2 ] ;
5859
+ ParamDecl *SelfDecl ;
5907
5860
5908
5861
// / The interface type of the initializing constructor.
5909
5862
Type InitializerInterfaceType;
@@ -5939,25 +5892,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
5939
5892
Expr *getSuperInitCall () { return CallToSuperInit; }
5940
5893
void setSuperInitCall (Expr *CallExpr) { CallToSuperInit = CallExpr; }
5941
5894
5942
- MutableArrayRef<ParameterList *> getParameterLists () {
5943
- return { ParameterLists, 2 };
5944
- }
5945
- ArrayRef<const ParameterList *> getParameterLists () const {
5946
- return AbstractFunctionDecl::getParameterLists ();
5947
- }
5948
- ParameterList *getParameterList (unsigned i) {
5949
- return getParameterLists ()[i];
5950
- }
5951
- const ParameterList *getParameterList (unsigned i) const {
5952
- return getParameterLists ()[i];
5953
- }
5954
-
5955
- // / Returns the normal parameters to the initializer, not including self.
5956
- ParameterList *getParameters () { return ParameterLists[1 ]; }
5957
-
5958
- // / Returns the normal parameters to the initializer, not including self.
5959
- const ParameterList *getParameters () const { return ParameterLists[1 ]; }
5960
-
5895
+ ParamDecl **getImplicitSelfDeclStorage () { return &SelfDecl; }
5961
5896
5962
5897
// / Specifies the kind of initialization call performed within the body
5963
5898
// / of the constructor, e.g., self.init or super.init.
@@ -6103,19 +6038,14 @@ class ConstructorDecl : public AbstractFunctionDecl {
6103
6038
// / }
6104
6039
// / \endcode
6105
6040
class DestructorDecl : public AbstractFunctionDecl {
6106
- ParameterList *ParameterLists[ 2 ] ;
6041
+ ParamDecl *SelfDecl ;
6107
6042
6108
6043
public:
6109
6044
DestructorDecl (SourceLoc DestructorLoc, ParamDecl *selfDecl,
6110
6045
DeclContext *Parent);
6111
6046
6112
- MutableArrayRef<ParameterList *> getParameterLists () {
6113
- return { ParameterLists, 2 };
6114
- }
6115
- ArrayRef<const ParameterList *> getParameterLists () const {
6116
- return { ParameterLists, 2 };
6117
- }
6118
-
6047
+ ParamDecl **getImplicitSelfDeclStorage () { return &SelfDecl; }
6048
+
6119
6049
SourceLoc getDestructorLoc () const { return getNameLoc (); }
6120
6050
SourceLoc getStartLoc () const { return getDestructorLoc (); }
6121
6051
SourceRange getSourceRange () const ;
@@ -6569,18 +6499,32 @@ inline bool AbstractStorageDecl::isStatic() const {
6569
6499
return false ;
6570
6500
}
6571
6501
6572
- inline MutableArrayRef<ParameterList *>
6573
- AbstractFunctionDecl::getParameterLists () {
6502
+ // / Constructors and destructors always have a 'self' parameter,
6503
+ // / which is stored in an instance member. Functions only have a
6504
+ // / 'self' if they are declared inside of a nominal type or extension,
6505
+ // / in which case we tail-allocate storage for it.
6506
+ inline ParamDecl **AbstractFunctionDecl::getImplicitSelfDeclStorage () {
6574
6507
switch (getKind ()) {
6575
6508
default : llvm_unreachable (" Unknown AbstractFunctionDecl!" );
6576
6509
case DeclKind::Constructor:
6577
- return cast<ConstructorDecl>(this )->getParameterLists ();
6510
+ return cast<ConstructorDecl>(this )->getImplicitSelfDeclStorage ();
6578
6511
case DeclKind::Destructor:
6579
- return cast<DestructorDecl>(this )->getParameterLists ();
6512
+ return cast<DestructorDecl>(this )->getImplicitSelfDeclStorage ();
6580
6513
case DeclKind::Func:
6581
6514
case DeclKind::Accessor:
6582
- return cast<FuncDecl>(this )->getParameterLists ();
6515
+ return cast<FuncDecl>(this )->getImplicitSelfDeclStorage ();
6516
+ }
6517
+ }
6518
+
6519
+ inline ParamDecl **FuncDecl::getImplicitSelfDeclStorage () {
6520
+ if (!hasImplicitSelfDecl ())
6521
+ return nullptr ;
6522
+
6523
+ if (!isa<AccessorDecl>(this )) {
6524
+ assert (getKind () == DeclKind::Func && " no new kinds of functions" );
6525
+ return reinterpret_cast <ParamDecl **>(this +1 );
6583
6526
}
6527
+ return reinterpret_cast <ParamDecl **>(static_cast <AccessorDecl*>(this )+1 );
6584
6528
}
6585
6529
6586
6530
inline DeclIterator &DeclIterator::operator ++() {
0 commit comments