@@ -54,6 +54,7 @@ namespace swift {
5454 struct ASTNode ;
5555 class ASTPrinter ;
5656 class ASTWalker ;
57+ class CallDecl ;
5758 class ConstructorDecl ;
5859 class DestructorDecl ;
5960 class DiagnosticEngine ;
@@ -137,6 +138,7 @@ enum class DescriptiveDeclKind : uint8_t {
137138 GenericStruct,
138139 GenericClass,
139140 GenericType,
141+ Call,
140142 Subscript,
141143 Constructor,
142144 Destructor,
@@ -3296,6 +3298,21 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
32963298 ToStoredPropertyOrMissingMemberPlaceholder ());
32973299 }
32983300
3301+ private:
3302+ // / Predicate used to filter CallDeclRange.
3303+ struct ToCallDecl {
3304+ ToCallDecl () {}
3305+ Optional<CallDecl *> operator ()(Decl *decl) const ;
3306+ };
3307+
3308+ public:
3309+ // / A range for iterating the call declarations of a nominal type.
3310+ using CallDeclRange = OptionalTransformRange<DeclRange,
3311+ ToCallDecl>;
3312+
3313+ // / Return a collection of the call declarations of this nominal type.
3314+ CallDeclRange getCallDeclarations () const ;
3315+
32993316 // Implement isa/cast/dyncast/etc.
33003317 static bool classof (const Decl *D) {
33013318 return D->getKind () >= DeclKind::First_NominalTypeDecl &&
@@ -5732,7 +5749,8 @@ class FuncDecl : public AbstractFunctionDecl {
57325749
57335750 static bool classof (const Decl *D) {
57345751 return D->getKind () == DeclKind::Func ||
5735- D->getKind () == DeclKind::Accessor;
5752+ D->getKind () == DeclKind::Accessor ||
5753+ D->getKind () == DeclKind::Call;
57365754 }
57375755 static bool classof (const AbstractFunctionDecl *D) {
57385756 return classof (static_cast <const Decl*>(D));
@@ -5885,7 +5903,53 @@ AbstractStorageDecl::AccessorRecord::getAccessor(AccessorKind kind) const {
58855903 }
58865904 return nullptr ;
58875905}
5888-
5906+
5907+ // / CallDecl - Declares a callable method for a type. For example:
5908+ // /
5909+ // / \code
5910+ // / struct Adder {
5911+ // / var base: Int
5912+ // / call(_ x: Int) -> Int {
5913+ // / return base + x
5914+ // / }
5915+ // / }
5916+ // / \endcode
5917+ class CallDecl final : public FuncDecl {
5918+ CallDecl (DeclName fullName, SourceLoc declLoc, bool throws,
5919+ SourceLoc throwsLoc, unsigned numParameterLists,
5920+ GenericParamList *genericParams, DeclContext *parent)
5921+ : FuncDecl(DeclKind::Call,
5922+ /* staticLoc*/ SourceLoc(), StaticSpellingKind::None,
5923+ /* func loc*/ declLoc, /* name*/ fullName, /* name loc*/ declLoc,
5924+ throws, throwsLoc, numParameterLists, genericParams, parent) {}
5925+
5926+ static CallDecl *createImpl (ASTContext &ctx, DeclName fullName,
5927+ SourceLoc declLoc, bool throws,
5928+ SourceLoc throwsLoc,
5929+ GenericParamList *genericParams,
5930+ DeclContext *parent, ClangNode clangNode);
5931+
5932+ public:
5933+ static CallDecl *create (ASTContext &ctx, DeclName fullName, SourceLoc declLoc,
5934+ bool throws, SourceLoc throwsLoc,
5935+ GenericParamList *genericParams,
5936+ ParameterList *parameterList,
5937+ TypeLoc fnRetType, DeclContext *parent,
5938+ ClangNode clangNode = ClangNode());
5939+
5940+ static bool classof (const Decl *D) {
5941+ return D->getKind () == DeclKind::Call;
5942+ }
5943+ static bool classof (const AbstractFunctionDecl *D) {
5944+ return classof (static_cast <const Decl*>(D));
5945+ }
5946+ static bool classof (const DeclContext *DC) {
5947+ if (auto D = DC->getAsDecl ())
5948+ return classof (D);
5949+ return false ;
5950+ }
5951+ };
5952+
58895953// / This represents a 'case' declaration in an 'enum', which may declare
58905954// / one or more individual comma-separated EnumElementDecls.
58915955class EnumCaseDecl final : public Decl,
@@ -6775,6 +6839,13 @@ ::operator()(Decl *decl) const {
67756839 return None;
67766840}
67776841
6842+ inline Optional<CallDecl *>
6843+ NominalTypeDecl::ToCallDecl::operator ()(Decl *decl) const {
6844+ if (auto callDecl = dyn_cast<CallDecl>(decl))
6845+ return callDecl;
6846+ return None;
6847+ }
6848+
67786849inline void
67796850AbstractStorageDecl::overwriteSetterAccess (AccessLevel accessLevel) {
67806851 Accessors.setInt (accessLevel);
@@ -6808,6 +6879,7 @@ inline ParamDecl **AbstractFunctionDecl::getImplicitSelfDeclStorage() {
68086879 return cast<DestructorDecl>(this )->getImplicitSelfDeclStorage ();
68096880 case DeclKind::Func:
68106881 case DeclKind::Accessor:
6882+ case DeclKind::Call:
68116883 return cast<FuncDecl>(this )->getImplicitSelfDeclStorage ();
68126884 }
68136885}
@@ -6816,11 +6888,12 @@ inline ParamDecl **FuncDecl::getImplicitSelfDeclStorage() {
68166888 if (!hasImplicitSelfDecl ())
68176889 return nullptr ;
68186890
6819- if (!isa<AccessorDecl>(this )) {
6820- assert (getKind () == DeclKind::Func && " no new kinds of functions" );
6821- return reinterpret_cast <ParamDecl **>(this +1 );
6822- }
6823- return reinterpret_cast <ParamDecl **>(static_cast <AccessorDecl*>(this )+1 );
6891+ if (isa<AccessorDecl>(this ))
6892+ return reinterpret_cast <ParamDecl **>(static_cast <AccessorDecl *>(this )+1 );
6893+ else if (isa<CallDecl>(this ))
6894+ return reinterpret_cast <ParamDecl **>(static_cast <CallDecl *>(this )+1 );
6895+ assert (getKind () == DeclKind::Func && " no new kinds of functions" );
6896+ return reinterpret_cast <ParamDecl **>(this +1 );
68246897}
68256898
68266899inline DeclIterator &DeclIterator::operator ++() {
0 commit comments