Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce callables. #23517

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ DECL_ATTR(available, Available,
AllowMultipleAttributes | LongAttribute,
1)
CONTEXTUAL_SIMPLE_DECL_ATTR(final, Final,
OnClass | OnFunc | OnAccessor | OnVar | OnSubscript |
OnClass | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
DeclModifier,
2)
DECL_ATTR(objc, ObjC,
Expand All @@ -129,7 +129,7 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(required, Required,
DeclModifier,
4)
CONTEXTUAL_SIMPLE_DECL_ATTR(optional, Optional,
OnConstructor | OnFunc | OnAccessor | OnVar | OnSubscript |
OnConstructor | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
DeclModifier,
5)
SIMPLE_DECL_ATTR(dynamicCallable, DynamicCallable,
Expand Down Expand Up @@ -183,7 +183,7 @@ DECL_ATTR(_semantics, Semantics,
AllowMultipleAttributes | UserInaccessible,
21)
CONTEXTUAL_SIMPLE_DECL_ATTR(dynamic, Dynamic,
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor |
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
DeclModifier,
22)
CONTEXTUAL_SIMPLE_DECL_ATTR(infix, Infix,
Expand All @@ -199,13 +199,14 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(postfix, Postfix,
DeclModifier,
25)
SIMPLE_DECL_ATTR(_transparent, Transparent,
OnFunc | OnAccessor | OnConstructor | OnVar | UserInaccessible,
OnFunc | OnAccessor | OnCall | OnConstructor | OnVar | UserInaccessible,
26)
SIMPLE_DECL_ATTR(requires_stored_property_inits, RequiresStoredPropertyInits,
OnClass,
27)
SIMPLE_DECL_ATTR(nonobjc, NonObjC,
OnExtension | OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor,
OnExtension | OnFunc | OnAccessor | OnCall | OnVar | OnSubscript |
OnConstructor,
30)
SIMPLE_DECL_ATTR(_fixed_layout, FixedLayout,
OnVar | OnClass | OnStruct |
Expand All @@ -215,40 +216,41 @@ SIMPLE_DECL_ATTR(inlinable, Inlinable,
OnVar | OnSubscript | OnAbstractFunction,
32)
DECL_ATTR(_specialize, Specialize,
OnConstructor | OnFunc | OnAccessor |
OnConstructor | OnFunc | OnAccessor | OnCall |
AllowMultipleAttributes | LongAttribute | UserInaccessible,
33)
SIMPLE_DECL_ATTR(objcMembers, ObjCMembers,
OnClass,
34)
CONTEXTUAL_SIMPLE_DECL_ATTR(__consuming, Consuming,
OnFunc | OnAccessor |
OnFunc | OnAccessor | OnCall |
DeclModifier |
UserInaccessible |
NotSerialized, 40)
CONTEXTUAL_SIMPLE_DECL_ATTR(mutating, Mutating,
OnFunc | OnAccessor |
OnFunc | OnAccessor | OnCall |
DeclModifier |
NotSerialized, 41)
CONTEXTUAL_SIMPLE_DECL_ATTR(nonmutating, NonMutating,
OnFunc | OnAccessor |
OnFunc | OnAccessor | OnCall |
DeclModifier |
NotSerialized, 42)
CONTEXTUAL_SIMPLE_DECL_ATTR(convenience, Convenience,
OnConstructor |
DeclModifier |
NotSerialized, 43)
CONTEXTUAL_SIMPLE_DECL_ATTR(override, Override,
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor | OnAssociatedType |
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
OnAssociatedType |
DeclModifier |
NotSerialized, 44)
SIMPLE_DECL_ATTR(_hasStorage, HasStorage,
OnVar |
UserInaccessible |
NotSerialized, 45)
DECL_ATTR(private, AccessControl,
OnFunc | OnAccessor | OnExtension | OnGenericType | OnVar | OnSubscript |
OnConstructor |
OnFunc | OnAccessor | OnCall | OnExtension | OnGenericType | OnVar |
OnSubscript | OnConstructor |
DeclModifier |
NotSerialized, 46)
DECL_ATTR_ALIAS(fileprivate, AccessControl)
Expand Down Expand Up @@ -296,7 +298,7 @@ DECL_ATTR(_alignment, Alignment,
UserInaccessible,
56)
SIMPLE_DECL_ATTR(rethrows, Rethrows,
OnFunc | OnAccessor | OnConstructor |
OnFunc | OnAccessor | OnCall | OnConstructor |
RejectByParser,
57)
DECL_ATTR(_swift_native_objc_runtime_base, SwiftNativeObjCRuntimeBase,
Expand All @@ -307,30 +309,30 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(indirect, Indirect, DeclModifier |
OnEnum | OnEnumElement,
60)
SIMPLE_DECL_ATTR(warn_unqualified_access, WarnUnqualifiedAccess,
OnFunc | OnAccessor /*| OnVar*/ |
OnFunc | OnAccessor | /*OnVar |*/
LongAttribute,
61)
SIMPLE_DECL_ATTR(_show_in_interface, ShowInInterface,
OnProtocol |
UserInaccessible,
62)
DECL_ATTR(_cdecl, CDecl,
OnFunc | OnAccessor |
OnFunc | OnAccessor | OnCall |
LongAttribute | UserInaccessible,
63)
SIMPLE_DECL_ATTR(usableFromInline, UsableFromInline,
OnAbstractFunction | OnVar | OnSubscript | OnNominalType | OnTypeAlias |
LongAttribute,
64)
SIMPLE_DECL_ATTR(discardableResult, DiscardableResult,
OnFunc | OnAccessor | OnConstructor |
OnFunc | OnAccessor | OnCall | OnConstructor |
LongAttribute,
65)
SIMPLE_DECL_ATTR(GKInspectable, GKInspectable,
OnVar,
66)
DECL_ATTR(_implements, Implements,
OnFunc | OnAccessor | OnVar | OnSubscript | OnTypeAlias |
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnTypeAlias |
UserInaccessible |
NotSerialized, 67)
DECL_ATTR(_objcRuntimeName, ObjCRuntimeName,
Expand All @@ -346,7 +348,7 @@ DECL_ATTR(_restatedObjCConformance, RestatedObjCConformance,
NotSerialized, 70)
// NOTE: 71 is unused
SIMPLE_DECL_ATTR(_implicitly_unwrapped_optional, ImplicitlyUnwrappedOptional,
OnFunc | OnAccessor | OnVar | OnParam | OnSubscript | OnConstructor |
OnFunc | OnAccessor | OnCall | OnVar | OnParam | OnSubscript | OnConstructor |
RejectByParser,
72)
DECL_ATTR(_optimize, Optimize,
Expand All @@ -358,7 +360,7 @@ DECL_ATTR(_clangImporterSynthesizedType, ClangImporterSynthesizedType,
LongAttribute | RejectByParser | UserInaccessible |
NotSerialized, 74)
SIMPLE_DECL_ATTR(_weakLinked, WeakLinked,
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnVar |
OnNominalType | OnAssociatedType | OnFunc | OnAccessor | OnCall | OnVar |
OnSubscript | OnConstructor | OnEnumElement | OnExtension | UserInaccessible,
75)
SIMPLE_DECL_ATTR(_frozen, Frozen,
Expand All @@ -374,7 +376,8 @@ SIMPLE_DECL_ATTR(_hasInitialValue, HasInitialValue,
UserInaccessible,
78)
SIMPLE_DECL_ATTR(_nonoverride, NonOverride,
OnFunc | OnAccessor | OnVar | OnSubscript | OnConstructor | OnAssociatedType |
OnFunc | OnAccessor | OnCall | OnVar | OnSubscript | OnConstructor |
OnAssociatedType |
UserInaccessible | NotSerialized,
79)
DECL_ATTR(_dynamicReplacement, DynamicReplacement,
Expand Down
65 changes: 58 additions & 7 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace swift {
struct ASTNode;
class ASTPrinter;
class ASTWalker;
class CallDecl;
class ConstructorDecl;
class DestructorDecl;
class DiagnosticEngine;
Expand Down Expand Up @@ -137,6 +138,7 @@ enum class DescriptiveDeclKind : uint8_t {
GenericStruct,
GenericClass,
GenericType,
Call,
Subscript,
Constructor,
Destructor,
Expand Down Expand Up @@ -5758,7 +5760,8 @@ class FuncDecl : public AbstractFunctionDecl {

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::Func ||
D->getKind() == DeclKind::Accessor;
D->getKind() == DeclKind::Accessor ||
D->getKind() == DeclKind::Call;
}
static bool classof(const AbstractFunctionDecl *D) {
return classof(static_cast<const Decl*>(D));
Expand Down Expand Up @@ -5911,7 +5914,53 @@ AbstractStorageDecl::AccessorRecord::getAccessor(AccessorKind kind) const {
}
return nullptr;
}


/// CallDecl - Declares a callable method for a type. For example:
///
/// \code
/// struct Adder {
/// var base: Int
/// call(_ x: Int) -> Int {
/// return base + x
/// }
/// }
/// \endcode
class CallDecl final : public FuncDecl {
CallDecl(DeclName fullName, SourceLoc declLoc, bool throws,
SourceLoc throwsLoc, unsigned numParameterLists,
GenericParamList *genericParams, DeclContext *parent)
: FuncDecl(DeclKind::Call,
/*staticLoc*/ SourceLoc(), StaticSpellingKind::None,
/*func loc*/ declLoc, /*name*/ fullName, /*name loc*/ declLoc,
throws, throwsLoc, numParameterLists, genericParams, parent) {}

static CallDecl *createImpl(ASTContext &ctx, DeclName fullName,
SourceLoc declLoc, bool throws,
SourceLoc throwsLoc,
GenericParamList *genericParams,
DeclContext *parent, ClangNode clangNode);

public:
static CallDecl *create(ASTContext &ctx, DeclName fullName, SourceLoc declLoc,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
ParameterList *parameterList,
TypeLoc fnRetType, DeclContext *parent,
ClangNode clangNode = ClangNode());

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::Call;
}
static bool classof(const AbstractFunctionDecl *D) {
return classof(static_cast<const Decl*>(D));
}
static bool classof(const DeclContext *DC) {
if (auto D = DC->getAsDecl())
return classof(D);
return false;
}
};

/// This represents a 'case' declaration in an 'enum', which may declare
/// one or more individual comma-separated EnumElementDecls.
class EnumCaseDecl final : public Decl,
Expand Down Expand Up @@ -6834,6 +6883,7 @@ inline ParamDecl **AbstractFunctionDecl::getImplicitSelfDeclStorage() {
return cast<DestructorDecl>(this)->getImplicitSelfDeclStorage();
case DeclKind::Func:
case DeclKind::Accessor:
case DeclKind::Call:
return cast<FuncDecl>(this)->getImplicitSelfDeclStorage();
}
}
Expand All @@ -6842,11 +6892,12 @@ inline ParamDecl **FuncDecl::getImplicitSelfDeclStorage() {
if (!hasImplicitSelfDecl())
return nullptr;

if (!isa<AccessorDecl>(this)) {
assert(getKind() == DeclKind::Func && "no new kinds of functions");
return reinterpret_cast<ParamDecl **>(this+1);
}
return reinterpret_cast<ParamDecl **>(static_cast<AccessorDecl*>(this)+1);
if (isa<AccessorDecl>(this))
return reinterpret_cast<ParamDecl **>(static_cast<AccessorDecl *>(this)+1);
else if (isa<CallDecl>(this))
return reinterpret_cast<ParamDecl **>(static_cast<CallDecl *>(this)+1);
assert(getKind() == DeclKind::Func && "no new kinds of functions");
return reinterpret_cast<ParamDecl **>(this+1);
}

inline DeclIterator &DeclIterator::operator++() {
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/DeclNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ ABSTRACT_DECL(Value, Decl)
ABSTRACT_FUNCTION_DECL(Destructor, AbstractFunctionDecl)
ABSTRACT_FUNCTION_DECL(Func, AbstractFunctionDecl)
ABSTRACT_FUNCTION_DECL(Accessor, FuncDecl)
DECL_RANGE(AbstractFunction, Constructor, Accessor)
ABSTRACT_FUNCTION_DECL(Call, FuncDecl)
DECL_RANGE(AbstractFunction, Constructor, Call)
CONTEXT_VALUE_DECL(EnumElement, ValueDecl)
DECL_RANGE(Value, Enum, EnumElement)

Expand Down
10 changes: 10 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ ERROR(subscript_without_get,none,
ERROR(subscript_static,none,
"subscript cannot be marked %0", (StaticSpellingKind))

// `call` member
ERROR(call_decl_static,none,
"'call' member cannot be marked %0", (StaticSpellingKind))
ERROR(call_decl_wrong_scope,none,
"'call' members may only be declared within a type", ())
ERROR(expected_lparen_call,PointsToFirstBadToken,
"expected '(' for 'call' member parameters", ())
ERROR(call_decl_cannot_have_name,PointsToFirstBadToken,
"'call' member cannot have a name", ())

// initializer
ERROR(invalid_nested_init,none,
"missing '%select{super.|self.}0' at initializer invocation", (bool))
Expand Down
21 changes: 13 additions & 8 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,9 @@ NOTE(archetype_declared_in_type,none,
NOTE(unbound_generic_parameter_explicit_fix,none,
"explicitly specify the generic arguments to fix this issue", ())

ERROR(missing_call_decl,none,
"%0 does not define 'call' declaration of type", (Type))

ERROR(invalid_dynamic_callable_type,none,
"@dynamicCallable attribute requires %0 to have either a valid "
"'dynamicallyCall(withArguments:)' method or "
Expand Down Expand Up @@ -1687,15 +1690,15 @@ ERROR(witness_requires_class_implementation,none,
(DeclName, Type))
ERROR(witness_not_accessible_proto,none,
"%select{initializer %1|method %1|%select{|setter for }2property %1"
"|subscript%select{| setter}2}0 must be declared "
"|subscript%select{| setter}2|'call' member}0 must be declared "
"%select{%error|fileprivate|internal|public|%error}3 because it matches a "
"requirement in %select{private|fileprivate|internal|public|%error}4 protocol "
"%5",
(RequirementKind, DeclName, bool, AccessLevel, AccessLevel, DeclName))
ERROR(witness_not_accessible_type,none,
"%select{initializer %1|method %1|%select{|setter for }2property %1"
"|subscript%select{| setter}2}0 must be as accessible as its enclosing "
"type because it matches a requirement in protocol %5",
"|subscript%select{| setter}2|'call' member}0 must be as accessible as "
"its enclosing type because it matches a requirement in protocol %5",
(RequirementKind, DeclName, bool, AccessLevel, AccessLevel, DeclName))
ERROR(type_witness_not_accessible_proto,none,
"%0 %1 must be declared %select{%error|fileprivate|internal|public|%error}2 "
Expand Down Expand Up @@ -1763,19 +1766,21 @@ NOTE(inherited_protocol_does_not_conform,none,
"type %0 does not conform to inherited protocol %1", (Type, Type))
NOTE(no_witnesses,none,
"protocol requires "
"%select{initializer %1|function %1|property %1|subscript}0 with type %2"
"%select{|; do you want to add a stub?}3",
"%select{initializer %1|function %1|property %1|subscript|'call' member}0 "
"with type %2%select{|; do you want to add a stub?}3",
(RequirementKind, DeclName, Type, bool))
NOTE(missing_witnesses_general,none, "do you want to add protocol stubs?",
())
NOTE(ambiguous_witnesses,none,
"multiple matching "
"%select{initializers named %1|functions named %1|properties named %1|"
"subscript operators}0 with type %2", (RequirementKind, DeclName, Type))
"subscript operators|'call' member}0 with type %2",
(RequirementKind, DeclName, Type))
NOTE(ambiguous_witnesses_wrong_name,none,
"multiple matching "
"%select{initializers named %1|functions named %1|properties named %1|"
"subscript operators}0 with type %2", (RequirementKind, DeclName, Type))
"subscript operators|'call' member}0 with type %2",
(RequirementKind, DeclName, Type))
NOTE(no_witnesses_type,none,
"protocol requires nested type %0; do you want to add it?", (Identifier))
NOTE(default_associated_type_req_fail,none,
Expand Down Expand Up @@ -1839,7 +1844,7 @@ NOTE(protocol_witness_renamed,none,
"rename to %0 to satisfy this requirement%1", (DeclName, StringRef))
NOTE(protocol_witness_kind_conflict,none,
"candidate is not %select{an initializer|a function|a variable|"
"a subscript}0", (RequirementKind))
"a subscript|a 'call' member}0", (RequirementKind))
NOTE(protocol_witness_type_conflict,none,
"candidate has non-matching type %0%1", (Type, StringRef))
NOTE(protocol_witness_missing_requirement,none,
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/DiagnosticsSema.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace swift {
Constructor,
Func,
Var,
Subscript
Subscript,
Call
};

// Declare common diagnostics objects with their appropriate types.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ class DeclBaseName {
};

private:
/// In a special DeclName represenenting a subscript, this opaque pointer
/// is used as the data of the base name identifier.
/// In a special DeclName representing a subscript, this opaque pointer is
/// used as the data of the base name identifier.
/// This is an implementation detail that should never leak outside of
/// DeclName.
static void *SubscriptIdentifierData;
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ NODE(BoundGenericOtherNominalType)
NODE(BoundGenericTypeAlias)
NODE(BoundGenericFunction)
NODE(BuiltinTypeName)
CONTEXT_NODE(CallDeclaration)
NODE(CFunctionPointer)
CONTEXT_NODE(Class)
NODE(ClassMetadataBaseOffset)
Expand Down
1 change: 1 addition & 0 deletions include/swift/IDE/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ enum class CodeCompletionDeclKind {
Constructor,
Destructor,
Subscript,
Call,
StaticMethod,
InstanceMethod,
PrefixOperatorFunction,
Expand Down
Loading