Skip to content

Commit cac51bd

Browse files
committed
Implement function-parameter-list and friends in lib/Syntax
Implements the following grammar productions: - function-parameter-list - function-parameter This is mostly reusable for other flavors of function declarations, such as initializers and whatnot, but those will have separate top-level syntax nodes. https://bugs.swift.org/browse/SR-4067
1 parent f4e478e commit cac51bd

File tree

8 files changed

+882
-183
lines changed

8 files changed

+882
-183
lines changed

include/swift/Syntax/DeclSyntax.h

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
namespace swift {
3333
namespace syntax {
3434

35+
class ExprSyntax;
36+
class ExprSyntaxData;
37+
3538
#pragma mark declaration Data
3639

3740
class DeclSyntaxData : public SyntaxData {
@@ -380,6 +383,245 @@ class TypeAliasDeclSyntaxBuilder final {
380383
TypeAliasDeclSyntax build() const;
381384
};
382385

386+
#pragma mark - function-parameter Data
387+
388+
class FunctionParameterSyntaxData final : public SyntaxData {
389+
390+
friend struct SyntaxFactory;
391+
friend class Syntax;
392+
friend class SyntaxData;
393+
friend class FunctionParameterSyntax;
394+
395+
RC<TypeSyntaxData> CachedTypeSyntax;
396+
RC<ExprSyntaxData> CachedDefaultValue;
397+
398+
FunctionParameterSyntaxData(RC<RawSyntax> Raw,
399+
const SyntaxData *Parent = nullptr,
400+
CursorIndex IndexInParent = 0);
401+
static RC<FunctionParameterSyntaxData>
402+
make(RC<RawSyntax> Raw, const SyntaxData *Parent = nullptr,
403+
CursorIndex IndexInParent = 0);
404+
static RC<FunctionParameterSyntaxData> makeBlank();
405+
406+
public:
407+
static bool classof(const SyntaxData *SD) {
408+
return SD->getKind() == SyntaxKind::FunctionParameter;
409+
}
410+
};
411+
412+
#pragma mark - function-parameter API
413+
414+
/// parameter ->
415+
/// external-parameter-name? local-parameter-name ':'
416+
/// type '...'? '='? expression? ','?
417+
class FunctionParameterSyntax final : public Syntax {
418+
friend struct SyntaxFactory;
419+
friend class Syntax;
420+
friend class SyntaxData;
421+
friend class FunctionParameterSyntaxData;
422+
423+
using DataType = FunctionParameterSyntaxData;
424+
425+
enum class Cursor : CursorIndex {
426+
ExternalName,
427+
LocalName,
428+
Colon,
429+
Type,
430+
Ellipsis,
431+
DefaultEqual,
432+
DefaultExpression,
433+
TrailingComma,
434+
};
435+
436+
FunctionParameterSyntax(const RC<SyntaxData> Root,
437+
const DataType *Data);
438+
439+
public:
440+
/// Get the external name of the parameter, if there is one.
441+
RC<TokenSyntax> getExternalName() const;
442+
443+
/// Return a FunctionParameterSyntax with the given external name.
444+
FunctionParameterSyntax
445+
withExternalName(RC<TokenSyntax> NewExternalName) const;
446+
447+
/// Return the local name of the parameter.
448+
RC<TokenSyntax> getLocalName() const;
449+
450+
/// Return a FunctionParameterSyntax with the given local name.
451+
FunctionParameterSyntax
452+
withLocalName(RC<TokenSyntax> NewLocalName) const;
453+
454+
/// Return the colon ':' token between the local name and type of the
455+
/// parameter.
456+
RC<TokenSyntax> getColonToken() const;
457+
458+
/// Return a FunctionParameterSyntax with the given colon token between
459+
/// the local name and type.
460+
FunctionParameterSyntax
461+
withColonToken(RC<TokenSyntax> NewColonToken) const;
462+
463+
/// Return the syntax for the type of this parameter.
464+
llvm::Optional<TypeSyntax> getTypeSyntax() const;
465+
466+
/// Return a FunctionParameterSyntax with the given parameter type syntax.
467+
FunctionParameterSyntax
468+
withTypeSyntax(llvm::Optional<TypeSyntax> NewType) const;
469+
470+
/// Return the equal '=' token in between the parameter type and the default
471+
/// value, if there is one.
472+
RC<TokenSyntax> getEqualToken() const;
473+
474+
/// Return a FunctionParameterSyntax with the given equal '=' token in
475+
/// between the parameter type and the default value.
476+
FunctionParameterSyntax withEqualToken(RC<TokenSyntax> NewEqualToken) const;
477+
478+
/// Return the expresion for the default value of the parameter, if there
479+
/// is one.
480+
llvm::Optional<ExprSyntax> getDefaultValue() const;
481+
482+
/// Return a FunctionParameterSyntax with the given default value. To remove
483+
/// the default value, pass llvm::None.
484+
FunctionParameterSyntax
485+
withDefaultValue(llvm::Optional<ExprSyntax> NewDefaultValue) const;
486+
487+
/// Return the trailing comma on the parameter, if there is one.
488+
RC<TokenSyntax> getTrailingComma() const;
489+
490+
/// Return a FunctionParameterSyntax with the given trailing comma.
491+
FunctionParameterSyntax
492+
withTrailingComma(RC<TokenSyntax> NewTrailingComma) const;
493+
494+
static bool classof(const Syntax *S) {
495+
return S->getKind() == SyntaxKind::FunctionParameter;
496+
}
497+
};
498+
499+
#pragma mark - function-parameter-list Data
500+
501+
class FunctionParameterListSyntaxData final : public SyntaxData {
502+
503+
std::vector<RC<FunctionParameterSyntaxData>> CachedParameters;
504+
505+
public:
506+
static bool classof(const SyntaxData *SD) {
507+
return SD->getKind() == SyntaxKind::FunctionParameterList;
508+
}
509+
};
510+
511+
#pragma mark - function-parameter-list API
512+
513+
/// parameter-list -> parameteter | parameter ',' parameter-list
514+
class FunctionParameterListSyntax final : public Syntax {
515+
friend struct SyntaxBuilder;
516+
friend class Syntax;
517+
friend class SyntaxData;
518+
friend class FunctionParameterListSyntaxData;
519+
520+
using DataType = FunctionParameterListSyntaxData;
521+
522+
public:
523+
static bool classof(const Syntax *S) {
524+
return S->getKind() == SyntaxKind::FunctionParameterList;
525+
}
526+
};
527+
528+
#pragma mark - function-signature Data
529+
530+
class FunctionSignatureSyntaxData final : public SyntaxData {
531+
532+
RC<FunctionParameterListSyntaxData> CachedParameterList;
533+
RC<TypeAttributesSyntaxData> CachedReturnTypeAttributes;
534+
RC<TypeSyntaxData> CachedReturnTypeSyntax;
535+
536+
public:
537+
static bool classof(const SyntaxData *SD) {
538+
return SD->getKind() == SyntaxKind::FunctionSignature;
539+
}
540+
};
541+
542+
#pragma mark - function-signature API
543+
544+
/// function-signature ->
545+
/// '(' parameter-list? ')' (throws | rethrows)? '->' attributes? type
546+
class FunctionSignatureSyntax final : public Syntax {
547+
friend struct SyntaxBuilder;
548+
friend class Syntax;
549+
friend class SyntaxData;
550+
friend class FunctionSignatureSyntaxData;
551+
552+
using DataType = FunctionSignatureSyntaxData;
553+
554+
enum class Cursor : CursorIndex {
555+
LeftParen,
556+
ParameterList,
557+
RightParen,
558+
ThrowsOrRethrows,
559+
Arrow,
560+
ReturnTypeAttributes,
561+
ReturnType,
562+
};
563+
564+
public:
565+
/// Return the left parenthesis '(' token enclosing the parameter list.
566+
RC<TokenSyntax> getLeftParenToken() const;
567+
568+
/// Return a FunctionSignatureSyntax with the given left parentesis '(' token
569+
/// enclosing the parameter list.
570+
FunctionSignatureSyntax
571+
withLeftParenToken(RC<TokenSyntax> NewLeftParen) const;
572+
573+
/// Return the parameter list for this signature.
574+
FunctionParameterListSyntax getParameterList() const;
575+
576+
/// Return the parameter list for this signature.
577+
FunctionSignatureSyntax
578+
withParameterList(FunctionParameterListSyntax NewParameterList) const;
579+
580+
/// Return the 'throws' token in this signature if it exists.
581+
RC<TokenSyntax> getThrowsToken() const;
582+
583+
/// Return a FunctionSignatureSyntax with the given 'throws' token.
584+
FunctionSignatureSyntax withThrowsToken(RC<TokenSyntax> NewThrowsToken) const;
585+
586+
/// Return the 'rethrows' token in this signature if it exists;
587+
RC<TokenSyntax> getRethrowsToken() const;
588+
589+
/// Return a FunctionSignatureSyntax with the given 'rethrows' token.
590+
FunctionSignatureSyntax
591+
withRethrowsToken(RC<TokenSyntax> NewRethrowsToken) const;
592+
593+
/// Return the arrow '->' token for the signature.
594+
RC<TokenSyntax> getArrowToken() const;
595+
596+
/// Return a FunctionSignatureSyntax with the given arrow token
597+
FunctionSignatureSyntax withArrowToken(RC<TokenSyntax> NewArrowToken) const;
598+
599+
/// Return the return type attributes for the signature.
600+
TypeAttributesSyntax getReturnTypeAttributes() const;
601+
602+
/// Return a FunctionSignatureSyntax with the given return type attributes.
603+
FunctionSignatureSyntax
604+
withReturnTypeAttributes(TypeAttributesSyntax NewReturnTypeAttributes) const;
605+
606+
/// Return the syntax for the return type of the signature.
607+
TypeSyntax getReturnTypeSyntax() const;
608+
609+
/// Return a FunctionSignatureSyntax with the given return type.
610+
FunctionSignatureSyntax withReturnTypeSyntax(TypeSyntax NewReturnType) const;
611+
612+
/// Return the right parenthesis ')' token enclosing the parameter list.
613+
RC<TokenSyntax> getRightParenToken() const;
614+
615+
/// Return a FunctionSignatureSyntax with the given right parentesis ')' token
616+
/// enclosing the parameter list.
617+
FunctionSignatureSyntax
618+
withRightParenToken(RC<TokenSyntax> NewLeftParen) const;
619+
620+
static bool classof(const Syntax *S) {
621+
return S->getKind() == SyntaxKind::FunctionSignature;
622+
}
623+
};
624+
383625
} // end namespace syntax
384626
} // end namespace swift
385627

include/swift/Syntax/ExprSyntax.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ExprSyntaxData : public SyntaxData {
5151
};
5252

5353
class ExprSyntax : public Syntax {
54+
friend class FunctionParameterSyntax;
5455
public:
5556
using DataType = ExprSyntaxData;
5657

0 commit comments

Comments
 (0)