Skip to content
Draft
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
4 changes: 4 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ class ASTContext final {
/// Retrieve a specific, known protocol.
ProtocolDecl *getProtocol(KnownProtocolKind kind) const;

/// Retrieves a Type of the standard library given his name.
Type getTypeByString(StringRef type);

/// Determine whether the given nominal type is one of the standard
/// library or Cocoa framework types that is known to be bridged by another
/// module's overlay, for layering or implementation detail reasons.
Expand Down Expand Up @@ -709,6 +712,7 @@ class ASTContext final {

// Builtin type and simple types that are used frequently.
const CanType TheErrorType; /// This is the ErrorType singleton.
const CanType TheNeverType; /// This is the Never singleton.
const CanType TheUnresolvedType; /// This is the UnresolvedType singleton.
const CanType TheEmptyTupleType; /// This is '()', aka Void
const CanType TheAnyType; /// This is 'Any', the empty protocol composition
Expand Down
36 changes: 16 additions & 20 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5766,6 +5766,7 @@ class ImportAsMemberStatus {
/// Base class for function-like declarations.
class AbstractFunctionDecl : public GenericContext, public ValueDecl {
friend class NeedsNewVTableEntryRequest;
friend class ThrowsTypeRequest;

public:
enum class BodyKind {
Expand Down Expand Up @@ -5862,7 +5863,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
SourceLoc ThrowsLoc;

/// Location of the type of the 'throws'.
TypeRepr *ThrowsType;
TypeLoc ThrowsType;

struct {
unsigned NeedsNewVTableEntryComputed : 1;
Expand All @@ -5871,12 +5872,12 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {

AbstractFunctionDecl(DeclKind Kind, DeclContext *Parent, DeclName Name,
SourceLoc NameLoc, bool Async, SourceLoc AsyncLoc,
bool Throws, SourceLoc ThrowsLoc, TypeRepr *ThrowsType,
bool Throws, SourceLoc ThrowsLoc, TypeLoc ThrowsType,
bool HasImplicitSelfDecl,
GenericParamList *GenericParams)
: GenericContext(DeclContextKind::AbstractFunctionDecl, Parent, GenericParams),
ValueDecl(Kind, Parent, Name, NameLoc),
Body(nullptr), AsyncLoc(AsyncLoc), ThrowsLoc(ThrowsLoc) {
ValueDecl(Kind, Parent, Name, NameLoc), Body(nullptr),
AsyncLoc(AsyncLoc), ThrowsLoc(ThrowsLoc), ThrowsType(ThrowsType) {
setBodyKind(BodyKind::None);
Bits.AbstractFunctionDecl.HasImplicitSelfDecl = HasImplicitSelfDecl;
Bits.AbstractFunctionDecl.Overridden = false;
Expand Down Expand Up @@ -5962,11 +5963,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {

/// Returns true if the function body throws.
bool hasThrows() const { return Bits.AbstractFunctionDecl.Throws; }

/// Returns true if the function has a typed throw.
bool hasTypedThrows() const { return (Bits.AbstractFunctionDecl.Throws && ThrowsType != nullptr); }

TypeRepr *typedThrow() const { return ThrowsType; }
Type getThrowsType() const { return ThrowsType.getType(); }

Type getThrowsInterfaceType() const;

// FIXME: Hack that provides names with keyword arguments for accessors.
DeclName getEffectiveFullName() const;
Expand Down Expand Up @@ -6234,7 +6234,7 @@ class FuncDecl : public AbstractFunctionDecl {
SourceLoc FuncLoc,
DeclName Name, SourceLoc NameLoc,
bool Async, SourceLoc AsyncLoc,
bool Throws, SourceLoc ThrowsLoc, TypeRepr *ThrowsType,
bool Throws, SourceLoc ThrowsLoc, TypeLoc ThrowsType,
bool HasImplicitSelfDecl,
GenericParamList *GenericParams, DeclContext *Parent)
: AbstractFunctionDecl(Kind, Parent,
Expand All @@ -6244,7 +6244,6 @@ class FuncDecl : public AbstractFunctionDecl {
HasImplicitSelfDecl, GenericParams),
StaticLoc(StaticLoc), FuncLoc(FuncLoc) {
assert(!Name.getBaseName().isSpecial());

Bits.FuncDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);

Bits.FuncDecl.ForcedStaticDispatch = false;
Expand All @@ -6256,6 +6255,7 @@ class FuncDecl : public AbstractFunctionDecl {
Bits.FuncDecl.HasTopLevelLocalContextCaptures = false;
}

void setThrowsInterfaceType(Type type);
void setResultInterfaceType(Type type);

private:
Expand All @@ -6264,7 +6264,7 @@ class FuncDecl : public AbstractFunctionDecl {
SourceLoc FuncLoc,
DeclName Name, SourceLoc NameLoc,
bool Async, SourceLoc AsyncLoc,
bool Throws, SourceLoc ThrowsLoc, TypeRepr *ThrowsType,
bool Throws, SourceLoc ThrowsLoc, Type ThrowsType,
GenericParamList *GenericParams,
DeclContext *Parent,
ClangNode ClangN);
Expand All @@ -6287,28 +6287,28 @@ class FuncDecl : public AbstractFunctionDecl {
/// Factory function only for use by deserialization.
static FuncDecl *createDeserialized(ASTContext &Context,
StaticSpellingKind StaticSpelling,
DeclName Name, bool Async, bool Throws, TypeRepr *ThrowsType,
DeclName Name, bool Async, bool Throws, Type ThrowsType,
GenericParamList *GenericParams,
Type FnRetType, DeclContext *Parent);

static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling, SourceLoc FuncLoc,
DeclName Name, SourceLoc NameLoc, bool Async,
SourceLoc AsyncLoc, bool Throws, SourceLoc ThrowsLoc, TypeRepr *ThrowsType,
SourceLoc AsyncLoc, bool Throws, SourceLoc ThrowsLoc, Type ThrowsType,
GenericParamList *GenericParams,
ParameterList *BodyParams, TypeRepr *ResultTyR,
DeclContext *Parent);

static FuncDecl *createImplicit(ASTContext &Context,
StaticSpellingKind StaticSpelling,
DeclName Name, SourceLoc NameLoc, bool Async,
bool Throws, TypeRepr *ThrowsType, GenericParamList *GenericParams,
bool Throws, Type ThrowsType, GenericParamList *GenericParams,
ParameterList *BodyParams, Type FnRetType,
DeclContext *Parent);

static FuncDecl *createImported(ASTContext &Context, SourceLoc FuncLoc,
DeclName Name, SourceLoc NameLoc,
bool Async, bool Throws, TypeRepr *ThrowsType,
bool Async, bool Throws, Type ThrowsType,
ParameterList *BodyParams, Type FnRetType,
DeclContext *Parent, ClangNode ClangN);

Expand Down Expand Up @@ -6443,13 +6443,12 @@ class AccessorDecl final : public FuncDecl {
AccessorDecl(SourceLoc declLoc, SourceLoc accessorKeywordLoc,
AccessorKind accessorKind, AbstractStorageDecl *storage,
SourceLoc staticLoc, StaticSpellingKind staticSpelling,
bool throws, SourceLoc throwsLoc, TypeRepr *throwsType,
bool hasImplicitSelfDecl, GenericParamList *genericParams,
DeclContext *parent)
: FuncDecl(DeclKind::Accessor,
staticLoc, staticSpelling, /*func loc*/ declLoc,
/*name*/ Identifier(), /*name loc*/ declLoc,
/*Async=*/false, SourceLoc(), throws, throwsLoc, throwsType,
/*Async=*/false, SourceLoc(), /*Throws=*/false, SourceLoc(), TypeLoc(),
hasImplicitSelfDecl, genericParams, parent),
AccessorKeywordLoc(accessorKeywordLoc),
Storage(storage) {
Expand All @@ -6463,7 +6462,6 @@ class AccessorDecl final : public FuncDecl {
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool throws, SourceLoc throwsLoc, TypeRepr *throwsType,
GenericParamList *genericParams,
DeclContext *parent,
ClangNode clangNode);
Expand All @@ -6481,7 +6479,6 @@ class AccessorDecl final : public FuncDecl {
AccessorKind accessorKind,
AbstractStorageDecl *storage,
StaticSpellingKind staticSpelling,
bool throws,
GenericParamList *genericParams,
Type fnRetType, DeclContext *parent);

Expand All @@ -6491,7 +6488,6 @@ class AccessorDecl final : public FuncDecl {
AbstractStorageDecl *storage,
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool throws, SourceLoc throwsLoc, TypeRepr *throwsType,
GenericParamList *genericParams,
ParameterList *parameterList,
Type fnRetType, DeclContext *parent,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ ERROR(cannot_convert_sequence_element_protocol,none,
ERROR(throws_functiontype_mismatch,none,
"invalid conversion from throwing function of type %0 to "
"non-throwing function type %1", (Type, Type))
ERROR(throws_type_doesnt_conform_to_error,none,
"throws type %0 must conform to 'Error'", (Type))

// Key-path expressions.
ERROR(expr_keypath_no_objc_runtime,none,
Expand Down
21 changes: 21 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,27 @@ class ParamSpecifierRequest
void cacheResult(ParamSpecifier value) const;
};

/// Determines the throws type of a function.
class ThrowsTypeRequest
: public SimpleRequest<ThrowsTypeRequest,
Type(AbstractFunctionDecl *),
RequestFlags::SeparatelyCached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
Type evaluate(Evaluator &evaluator, AbstractFunctionDecl *decl) const;

public:
// Separate caching.
bool isCached() const { return true; }
Optional<Type> getCachedResult() const;
void cacheResult(Type value) const;
};

/// Determines the result type of a function or element type of a subscript.
class ResultTypeRequest
: public SimpleRequest<ResultTypeRequest,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ SWIFT_REQUEST(TypeChecker, ParamSpecifierRequest,
ParamDecl::Specifier(ParamDecl *), SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ResultTypeRequest,
Type(ValueDecl *), SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ThrowsTypeRequest,
Type(AbstractFunctionDecl *), SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, AreAllStoredPropertiesDefaultInitableRequest,
bool(NominalTypeDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasUserDefinedDesignatedInitRequest,
Expand Down
29 changes: 18 additions & 11 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,7 @@ END_CAN_TYPE_WRAPPER(DynamicSelfType, Type)
/// be 'thin', indicating that a function value has no capture context and can be
/// represented at the binary level as a single function pointer.
class AnyFunctionType : public TypeBase {
const Type ThrowsType;
const Type Output;

public:
Expand Down Expand Up @@ -2873,9 +2874,9 @@ class AnyFunctionType : public TypeBase {
/// Subclasses are responsible for storing and retrieving the
/// ClangTypeInfo value if one is present.
AnyFunctionType(TypeKind Kind, const ASTContext *CanTypeContext,
Type Output, RecursiveTypeProperties properties,
Type Output, Type ThrowsType, RecursiveTypeProperties properties,
unsigned NumParams, ExtInfo Info)
: TypeBase(Kind, CanTypeContext, properties), Output(Output) {
: TypeBase(Kind, CanTypeContext, properties), ThrowsType(ThrowsType), Output(Output) {
Bits.AnyFunctionType.ExtInfoBits = Info.getBits();
Bits.AnyFunctionType.HasClangTypeInfo = !Info.getClangTypeInfo().empty();
Bits.AnyFunctionType.NumParams = NumParams;
Expand Down Expand Up @@ -2916,6 +2917,7 @@ class AnyFunctionType : public TypeBase {
ArrayRef<Identifier> labels);

Type getResult() const { return Output; }
Type getThrowsType() const { return ThrowsType; }
ArrayRef<Param> getParams() const;
unsigned getNumParams() const { return Bits.AnyFunctionType.NumParams; }

Expand Down Expand Up @@ -3118,7 +3120,7 @@ BEGIN_CAN_TYPE_WRAPPER(AnyFunctionType, Type)

static CanAnyFunctionType get(CanGenericSignature signature,
CanParamArrayRef params,
CanType result,
CanType result, CanType throwsType,
ExtInfo info = ExtInfo());

CanGenericSignature getOptGenericSignature() const;
Expand All @@ -3128,6 +3130,7 @@ BEGIN_CAN_TYPE_WRAPPER(AnyFunctionType, Type)
}

PROXY_CAN_TYPE_SIMPLE_GETTER(getResult)
PROXY_CAN_TYPE_SIMPLE_GETTER(getThrowsType)

CanAnyFunctionType withExtInfo(ExtInfo info) const {
return CanAnyFunctionType(getPointer()->withExtInfo(info));
Expand Down Expand Up @@ -3159,7 +3162,8 @@ class FunctionType final

public:
/// 'Constructor' Factory Function
static FunctionType *get(ArrayRef<Param> params, Type result,
static FunctionType *get(ArrayRef<Param> params,
Type result, Type throwsType,
ExtInfo info = ExtInfo());

// Retrieve the input parameters of this function type.
Expand Down Expand Up @@ -3190,13 +3194,13 @@ class FunctionType final
}

private:
FunctionType(ArrayRef<Param> params, Type result, ExtInfo info,
FunctionType(ArrayRef<Param> params, Type result, Type throwsType, ExtInfo info,
const ASTContext *ctx, RecursiveTypeProperties properties);
};
BEGIN_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
static CanFunctionType get(CanParamArrayRef params, CanType result,
static CanFunctionType get(CanParamArrayRef params, CanType result, CanType throwsType,
ExtInfo info = ExtInfo()) {
auto fnType = FunctionType::get(params.getOriginalArray(), result, info);
auto fnType = FunctionType::get(params.getOriginalArray(), result, throwsType, info);
return cast<FunctionType>(fnType->getCanonicalType());
}

Expand Down Expand Up @@ -3255,6 +3259,7 @@ class GenericFunctionType final : public AnyFunctionType,
GenericFunctionType(GenericSignature sig,
ArrayRef<Param> params,
Type result,
Type throwsType,
ExtInfo info,
const ASTContext *ctx,
RecursiveTypeProperties properties);
Expand All @@ -3264,6 +3269,7 @@ class GenericFunctionType final : public AnyFunctionType,
static GenericFunctionType *get(GenericSignature sig,
ArrayRef<Param> params,
Type result,
Type throwsType,
ExtInfo info = ExtInfo());

// Retrieve the input parameters of this function type.
Expand Down Expand Up @@ -3308,11 +3314,12 @@ BEGIN_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
static CanGenericFunctionType get(CanGenericSignature sig,
CanParamArrayRef params,
CanType result,
CanType throwsType,
ExtInfo info = ExtInfo()) {
// Knowing that the argument types are independently canonical is
// not sufficient to guarantee that the function type will be canonical.
auto fnType = GenericFunctionType::get(sig, params.getOriginalArray(),
result, info);
result, throwsType, info);
return cast<GenericFunctionType>(fnType->getCanonicalType());
}

Expand All @@ -3334,11 +3341,11 @@ END_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)

inline CanAnyFunctionType
CanAnyFunctionType::get(CanGenericSignature signature, CanParamArrayRef params,
CanType result, ExtInfo extInfo) {
CanType result, CanType throwsType, ExtInfo extInfo) {
if (signature) {
return CanGenericFunctionType::get(signature, params, result, extInfo);
return CanGenericFunctionType::get(signature, params, result, throwsType, extInfo);
} else {
return CanFunctionType::get(params, result, extInfo);
return CanFunctionType::get(params, result, throwsType, extInfo);
}
}

Expand Down
Loading