mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Implement function-signature in lib/Syntax
Part of: https://bugs.swift.org/browse/SR-4043
This commit is contained in:
@@ -540,3 +540,195 @@ withTrailingComma(RC<TokenSyntax> NewTrailingComma) const {
|
||||
return Data->replaceChild<FunctionParameterSyntax>(NewTrailingComma,
|
||||
Cursor::TrailingComma);
|
||||
}
|
||||
|
||||
#pragma mark - function-signature Data
|
||||
|
||||
FunctionSignatureSyntaxData::
|
||||
FunctionSignatureSyntaxData(const RC<RawSyntax> Raw,
|
||||
const SyntaxData *Parent,
|
||||
const CursorIndex IndexInParent)
|
||||
: SyntaxData(Raw, Parent, IndexInParent) {
|
||||
assert(Raw->Layout.size() == 7);
|
||||
syntax_assert_child_token_text(Raw,
|
||||
FunctionSignatureSyntax::Cursor::LeftParen,
|
||||
tok::l_paren, "(");
|
||||
|
||||
assert(Raw->getChild(FunctionSignatureSyntax::Cursor::ParameterList)->Kind ==
|
||||
SyntaxKind::FunctionParameterList);
|
||||
|
||||
syntax_assert_child_token_text(Raw,
|
||||
FunctionSignatureSyntax::Cursor::RightParen,
|
||||
tok::r_paren, ")");
|
||||
#ifndef NDEBUG
|
||||
auto ThrowsRethrows = cast<TokenSyntax>(
|
||||
Raw->getChild(FunctionSignatureSyntax::Cursor::ThrowsOrRethrows));
|
||||
assert(cast<TokenSyntax>(ThrowsRethrows)->getTokenKind() == tok::kw_throws ||
|
||||
cast<TokenSyntax>(ThrowsRethrows)->getTokenKind() == tok::kw_rethrows);
|
||||
#endif
|
||||
syntax_assert_child_token_text(Raw, FunctionSignatureSyntax::Cursor::Arrow,
|
||||
tok::arrow, "->");
|
||||
syntax_assert_child_kind(Raw,
|
||||
FunctionSignatureSyntax::Cursor::ReturnTypeAttributes,
|
||||
SyntaxKind::TypeAttributes);
|
||||
assert(Raw->getChild(FunctionSignatureSyntax::Cursor::ReturnType)->isType());
|
||||
}
|
||||
|
||||
RC<FunctionSignatureSyntaxData>
|
||||
FunctionSignatureSyntaxData::make(RC<RawSyntax> Raw, const SyntaxData *Parent,
|
||||
CursorIndex IndexInParent) {
|
||||
return RC<FunctionSignatureSyntaxData> {
|
||||
new FunctionSignatureSyntaxData {
|
||||
Raw, Parent, IndexInParent
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
RC<FunctionSignatureSyntaxData> FunctionSignatureSyntaxData::makeBlank() {
|
||||
auto Raw = RawSyntax::make(SyntaxKind::FunctionSignature,
|
||||
{
|
||||
TokenSyntax::missingToken(tok::l_paren, "("),
|
||||
RawSyntax::missing(SyntaxKind::FunctionParameterList),
|
||||
TokenSyntax::missingToken(tok::r_paren, ")"),
|
||||
TokenSyntax::missingToken(tok::kw_throws, "throws"),
|
||||
TokenSyntax::missingToken(tok::arrow, "->"),
|
||||
RawSyntax::missing(SyntaxKind::TypeAttributes),
|
||||
RawSyntax::missing(SyntaxKind::MissingType),
|
||||
},
|
||||
SourcePresence::Present);
|
||||
return make(Raw);
|
||||
}
|
||||
|
||||
#pragma mark - function-signature API
|
||||
|
||||
RC<TokenSyntax> FunctionSignatureSyntax::getLeftParenToken() const {
|
||||
return cast<TokenSyntax>(getRaw()->getChild(Cursor::LeftParen));
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withLeftParenToken(RC<TokenSyntax> NewLeftParen) const {
|
||||
syntax_assert_token_is(NewLeftParen, tok::l_paren, "(");
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewLeftParen,
|
||||
Cursor::LeftParen);
|
||||
}
|
||||
|
||||
FunctionParameterListSyntax FunctionSignatureSyntax::getParameterList() const {
|
||||
auto RawList = getRaw()->getChild(Cursor::ParameterList);
|
||||
|
||||
auto *MyData = getUnsafeData<FunctionSignatureSyntax>();
|
||||
|
||||
auto &ChildPtr = *reinterpret_cast<std::atomic<uintptr_t>*>(
|
||||
&MyData->CachedParameterList);
|
||||
|
||||
SyntaxData::realizeSyntaxNode<FunctionParameterListSyntax>(ChildPtr, RawList,
|
||||
MyData, cursorIndex(Cursor::ParameterList));
|
||||
|
||||
return FunctionParameterListSyntax {
|
||||
Root,
|
||||
MyData->CachedParameterList.get()
|
||||
};
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withParameterList(FunctionParameterListSyntax NewParameterList) const {
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewParameterList.getRaw(),
|
||||
Cursor::ParameterList);
|
||||
}
|
||||
|
||||
RC<TokenSyntax> FunctionSignatureSyntax::getRightParenToken() const {
|
||||
return cast<TokenSyntax>(getRaw()->getChild(Cursor::RightParen));
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withRightParenToken(RC<TokenSyntax> NewRightParen) const {
|
||||
syntax_assert_token_is(NewRightParen, tok::r_paren, ")");
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewRightParen,
|
||||
Cursor::RightParen);
|
||||
}
|
||||
|
||||
RC<TokenSyntax> FunctionSignatureSyntax::getThrowsToken() const {
|
||||
auto Throw = cast<TokenSyntax>(getRaw()->getChild(Cursor::ThrowsOrRethrows));
|
||||
if (Throw->getTokenKind() != tok::kw_throws) {
|
||||
return TokenSyntax::missingToken(tok::kw_throws, "throws");
|
||||
}
|
||||
return Throw;
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withThrowsToken(RC<TokenSyntax> NewThrowsToken) const {
|
||||
syntax_assert_token_is(NewThrowsToken, tok::kw_throws, "throws");
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewThrowsToken,
|
||||
Cursor::ThrowsOrRethrows);
|
||||
}
|
||||
|
||||
RC<TokenSyntax> FunctionSignatureSyntax::getRethrowsToken() const {
|
||||
auto Rethrow = cast<TokenSyntax>(
|
||||
getRaw()->getChild(Cursor::ThrowsOrRethrows));
|
||||
if (Rethrow->getTokenKind() != tok::kw_rethrows) {
|
||||
return TokenSyntax::missingToken(tok::kw_rethrows, "rethrows");
|
||||
}
|
||||
return Rethrow;
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withRethrowsToken(RC<TokenSyntax> NewRethrowsToken) const {
|
||||
syntax_assert_token_is(NewRethrowsToken, tok::kw_rethrows, "rethrows");
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewRethrowsToken,
|
||||
Cursor::ThrowsOrRethrows);
|
||||
}
|
||||
|
||||
RC<TokenSyntax> FunctionSignatureSyntax::getArrowToken() const {
|
||||
return cast<TokenSyntax>(getRaw()->getChild(Cursor::Arrow));
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withArrowToken(RC<TokenSyntax> NewArrowToken) const {
|
||||
syntax_assert_token_is(NewArrowToken, tok::arrow, "->");
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewArrowToken,
|
||||
Cursor::Arrow);
|
||||
}
|
||||
|
||||
TypeAttributesSyntax FunctionSignatureSyntax::getReturnTypeAttributes() const {
|
||||
auto RawAttrs = getRaw()->getChild(Cursor::ReturnTypeAttributes);
|
||||
|
||||
auto *MyData = getUnsafeData<FunctionSignatureSyntax>();
|
||||
|
||||
auto &ChildPtr = *reinterpret_cast<std::atomic<uintptr_t>*>(
|
||||
&MyData->CachedReturnTypeAttributes);
|
||||
|
||||
SyntaxData::realizeSyntaxNode<TypeAttributesSyntax>(ChildPtr, RawAttrs,
|
||||
MyData, cursorIndex(Cursor::ReturnTypeAttributes));
|
||||
|
||||
return TypeAttributesSyntax {
|
||||
Root,
|
||||
MyData->CachedReturnTypeAttributes.get()
|
||||
};
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withReturnTypeAttributes(TypeAttributesSyntax NewAttributes) const {
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(NewAttributes.getRaw(),
|
||||
Cursor::ReturnTypeAttributes);
|
||||
}
|
||||
|
||||
TypeSyntax FunctionSignatureSyntax::getReturnTypeSyntax() const {
|
||||
auto RawType = getRaw()->getChild(Cursor::ReturnType);
|
||||
|
||||
auto *MyData = getUnsafeData<FunctionSignatureSyntax>();
|
||||
|
||||
auto &ChildPtr = *reinterpret_cast<std::atomic<uintptr_t>*>(
|
||||
&MyData->CachedReturnTypeSyntax);
|
||||
|
||||
SyntaxData::realizeSyntaxNode<TypeSyntax>(ChildPtr, RawType,
|
||||
MyData, cursorIndex(Cursor::ReturnType));
|
||||
|
||||
return TypeSyntax {
|
||||
Root,
|
||||
MyData->CachedReturnTypeSyntax.get()
|
||||
};
|
||||
}
|
||||
|
||||
FunctionSignatureSyntax FunctionSignatureSyntax::
|
||||
withReturnTypeSyntax(TypeSyntax NewReturnTypeSyntax) const {
|
||||
return Data->replaceChild<FunctionSignatureSyntax>(
|
||||
NewReturnTypeSyntax.getRaw(), Cursor::ReturnType);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user