Skip to content

libSyntax: parse generic where clause. #13261

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

Merged
merged 1 commit into from
Dec 5, 2017
Merged
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
1 change: 1 addition & 0 deletions include/swift/Syntax/SyntaxParsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum class SyntaxContextKind {
Expr,
Type,
Pattern,
Syntax,
};

/// Indicates what action should be performed on the destruction of
Expand Down
13 changes: 11 additions & 2 deletions lib/Parse/ParseGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/Syntax/SyntaxNodes.h"
#include "swift/Syntax/SyntaxParsingContext.h"
using namespace swift;
using namespace swift::syntax;

/// parseGenericParameters - Parse a sequence of generic parameters, e.g.,
/// < T : Comparable, U : Container> along with an optional requires clause.
Expand Down Expand Up @@ -245,11 +246,17 @@ ParserStatus Parser::parseGenericWhereClause(
SmallVectorImpl<RequirementRepr> &Requirements,
bool &FirstTypeInComplete,
bool AllowLayoutConstraints) {
SyntaxParsingContext ClauseContext(SyntaxContext,
SyntaxKind::GenericWhereClause);
ParserStatus Status;
// Parse the 'where'.
WhereLoc = consumeToken(tok::kw_where);
FirstTypeInComplete = false;
SyntaxParsingContext ReqListContext(SyntaxContext,
SyntaxKind::GenericRequirementList);
bool HasNextReq;
do {
SyntaxParsingContext ReqContext(SyntaxContext, SyntaxContextKind::Syntax);
// Parse the leading type-identifier.
auto FirstTypeResult = parseTypeIdentifier();
if (FirstTypeResult.hasSyntax())
Expand All @@ -269,7 +276,7 @@ ParserStatus Parser::parseGenericWhereClause(
if (Tok.is(tok::colon)) {
// A conformance-requirement.
SourceLoc ColonLoc = consumeToken();

ReqContext.setCreateSyntax(SyntaxKind::ConformanceRequirement);
if (Tok.is(tok::identifier) &&
getLayoutConstraint(Context.getIdentifier(Tok.getText()), Context)
->isKnownLayout()) {
Expand Down Expand Up @@ -309,6 +316,7 @@ ParserStatus Parser::parseGenericWhereClause(
}
} else if ((Tok.isAnyOperator() && Tok.getText() == "==") ||
Tok.is(tok::equal)) {
ReqContext.setCreateSyntax(SyntaxKind::SameTypeRequirement);
// A same-type-requirement
if (Tok.is(tok::equal)) {
diagnose(Tok, diag::requires_single_equal)
Expand All @@ -334,8 +342,9 @@ ParserStatus Parser::parseGenericWhereClause(
Status.setIsParseError();
break;
}
HasNextReq = consumeIf(tok::comma);
// If there's a comma, keep parsing the list.
} while (consumeIf(tok::comma));
} while (HasNextReq);

if (Requirements.empty())
WhereLoc = SourceLoc();
Expand Down
6 changes: 6 additions & 0 deletions lib/Syntax/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ RC<RawSyntax> bridgeAs(SyntaxContextKind Kind, ArrayRef<RC<RawSyntax>> Parts) {
if (!RawNode->isPattern())
return makeUnknownSyntax(SyntaxKind::UnknownPattern, Parts);
break;
case SyntaxContextKind::Syntax:
// We don't need to coerce in this case.
break;
}
return RawNode;
} else {
Expand All @@ -178,6 +181,9 @@ RC<RawSyntax> bridgeAs(SyntaxContextKind Kind, ArrayRef<RC<RawSyntax>> Parts) {
case SyntaxContextKind::Pattern:
UnknownKind = SyntaxKind::UnknownPattern;
break;
case SyntaxContextKind::Syntax:
UnknownKind = SyntaxKind::Unknown;
break;
}
return makeUnknownSyntax(UnknownKind, Parts);
}
Expand Down
2 changes: 2 additions & 0 deletions test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ struct foo <MemberDeclBlock>{<Attribute>
private </DeclModifier><DeclModifier>static </DeclModifier>func foo() <CodeBlock>{}</CodeBlock>
}
}</MemberDeclBlock>

struct S<A, B, C, D> <GenericWhereClause>where <ConformanceRequirement><SimpleTypeIdentifier>A</SimpleTypeIdentifier>:<SimpleTypeIdentifier>B</SimpleTypeIdentifier>, </ConformanceRequirement><SameTypeRequirement><SimpleTypeIdentifier>B</SimpleTypeIdentifier>==<SimpleTypeIdentifier>C</SimpleTypeIdentifier>, </SameTypeRequirement><ConformanceRequirement><SimpleTypeIdentifier>A </SimpleTypeIdentifier>: <SimpleTypeIdentifier>C</SimpleTypeIdentifier>, </ConformanceRequirement><SameTypeRequirement><MemberTypeIdentifier><SimpleTypeIdentifier>B</SimpleTypeIdentifier>.C </MemberTypeIdentifier>== <MemberTypeIdentifier><SimpleTypeIdentifier>D</SimpleTypeIdentifier>.A</MemberTypeIdentifier>, </SameTypeRequirement><ConformanceRequirement><MemberTypeIdentifier><SimpleTypeIdentifier>A</SimpleTypeIdentifier>.B</MemberTypeIdentifier>: <MemberTypeIdentifier><SimpleTypeIdentifier>C</SimpleTypeIdentifier>.D </MemberTypeIdentifier></ConformanceRequirement></GenericWhereClause><MemberDeclBlock>{}</MemberDeclBlock>
2 changes: 2 additions & 0 deletions test/Syntax/round_trip_parse_gen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ struct foo {
private static func foo() {}
}
}

struct S<A, B, C, D> where A:B, B==C, A : C, B.C == D.A, A.B: C.D {}