Skip to content

Commit c2bfff0

Browse files
committed
AST: Add support for building 'throws' and 'async' built-in functions
1 parent 1a89813 commit c2bfff0

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

lib/AST/Builtins.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,24 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType) {
178178
return FD;
179179
}
180180

181+
namespace {
182+
183+
enum class BuiltinThrowsKind : uint8_t {
184+
None,
185+
Throws,
186+
Rethrows
187+
};
188+
189+
}
190+
181191
/// Build a builtin function declaration.
182192
static FuncDecl *
183193
getBuiltinGenericFunction(Identifier Id,
184194
ArrayRef<AnyFunctionType::Param> ArgParamTypes,
185195
Type ResType,
186196
GenericParamList *GenericParams,
187197
GenericSignature Sig,
188-
bool Rethrows = false) {
198+
bool Async, BuiltinThrowsKind Throws) {
189199
assert(GenericParams && "Missing generic parameters");
190200
auto &Context = ResType->getASTContext();
191201

@@ -211,13 +221,15 @@ getBuiltinGenericFunction(Identifier Id,
211221

212222
DeclName Name(Context, Id, paramList);
213223
auto *const func = FuncDecl::createImplicit(
214-
Context, StaticSpellingKind::None, Name, /*NameLoc=*/SourceLoc(),
215-
/*Async=*/false,
216-
/*Throws=*/Rethrows, GenericParams, paramList, ResType, DC);
224+
Context, StaticSpellingKind::None, Name,
225+
/*NameLoc=*/SourceLoc(),
226+
Async,
227+
Throws != BuiltinThrowsKind::None,
228+
GenericParams, paramList, ResType, DC);
217229

218230
func->setAccess(AccessLevel::Public);
219231
func->setGenericSignature(Sig);
220-
if (Rethrows)
232+
if (Throws == BuiltinThrowsKind::Rethrows)
221233
func->getAttrs().add(new (Context) RethrowsAttr(/*ThrowsLoc*/ SourceLoc()));
222234

223235
return func;
@@ -445,7 +457,8 @@ namespace {
445457
GenericParamList *TheGenericParamList;
446458
SmallVector<AnyFunctionType::Param, 4> InterfaceParams;
447459
Type InterfaceResult;
448-
bool Rethrows = false;
460+
bool Async = false;
461+
BuiltinThrowsKind Throws = BuiltinThrowsKind::None;
449462

450463
// Accumulate params and requirements here, so that we can make the
451464
// appropriate `AbstractGenericSignatureRequest` when `build()` is called.
@@ -490,8 +503,16 @@ namespace {
490503
addedRequirements.push_back(req);
491504
}
492505

493-
void setRethrows(bool rethrows = true) {
494-
Rethrows = rethrows;
506+
void setAsync() {
507+
Async = true;
508+
}
509+
510+
void setThrows() {
511+
Throws = BuiltinThrowsKind::Throws;
512+
}
513+
514+
void setRethrows() {
515+
Throws = BuiltinThrowsKind::Rethrows;
495516
}
496517

497518
FuncDecl *build(Identifier name) {
@@ -502,7 +523,8 @@ namespace {
502523
nullptr);
503524
return getBuiltinGenericFunction(name, InterfaceParams,
504525
InterfaceResult,
505-
TheGenericParamList, GenericSig);
526+
TheGenericParamList, GenericSig,
527+
Async, Throws);
506528
}
507529

508530
// Don't use these generator classes directly; call the make{...}

0 commit comments

Comments
 (0)