Skip to content

[SyntaxParse] Reapply Parse generic parameter clause and generic where clause #27281

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 2 commits into from
Sep 21, 2019
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
2 changes: 2 additions & 0 deletions cmake/modules/SwiftHandleGybSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ function(handle_gyb_sources dependency_out_var_name sources_var_name arch)
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/DeclNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/ExprNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/GenericNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/NodeSerializationCodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/PatternNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/SILOnlyNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/StmtNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/TypeNodes.py"
"${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/Token.py"
Expand Down
24 changes: 21 additions & 3 deletions include/swift/Parse/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@

namespace swift {
/// Generates AST nodes from Syntax nodes.
class Parser;
class ASTGen {
ASTContext &Context;

/// Type cache to prevent multiple transformations of the same syntax node.
llvm::DenseMap<syntax::SyntaxNodeId, TypeRepr *> TypeCache;

PersistentParserState **ParserState;
Parser &P;

// FIXME: remove when Syntax can represent all types and ASTGen can handle them
/// Types that cannot be represented by Syntax or generated by ASTGen.
llvm::DenseMap<SourceLoc, TypeRepr *> Types;

llvm::DenseMap<SourceLoc, DeclAttributes> ParsedDeclAttrs;

public:
ASTGen(ASTContext &Context, PersistentParserState **ParserState)
: Context(Context), ParserState(ParserState) {}
ASTGen(ASTContext &Context, Parser &P)
: Context(Context), P(P) {}

SourceLoc generate(syntax::TokenSyntax Tok, SourceLoc &Loc);

Expand Down Expand Up @@ -70,6 +73,15 @@ class ASTGen {
llvm::SmallVector<TypeRepr *, 4>
generate(syntax::GenericArgumentListSyntax Args, SourceLoc &Loc);

GenericParamList *
generate(syntax::GenericParameterClauseListSyntax clause, SourceLoc &Loc);
GenericParamList *
generate(syntax::GenericParameterClauseSyntax clause, SourceLoc &Loc);
Optional<RequirementRepr>
generate(syntax::GenericRequirementSyntax req, SourceLoc &Loc);
LayoutConstraint
generate(syntax::LayoutConstraintSyntax req, SourceLoc &Loc);

/// Copy a numeric literal value into AST-owned memory, stripping underscores
/// so the semantic part of the value can be parsed by APInt/APFloat parsers.
static StringRef copyAndStripUnderscores(StringRef Orig, ASTContext &Context);
Expand Down Expand Up @@ -102,6 +114,8 @@ class ASTGen {

ValueDecl *lookupInScope(DeclName Name);

void addToScope(ValueDecl *D, bool diagnoseRedefinitions = true);

TypeRepr *cacheType(syntax::TypeSyntax Type, TypeRepr *TypeAST);

TypeRepr *lookupType(syntax::TypeSyntax Type);
Expand All @@ -112,6 +126,10 @@ class ASTGen {
bool hasType(const SourceLoc &Loc) const;

TypeRepr *getType(const SourceLoc &Loc) const;

void addDeclAttributes(DeclAttributes attrs, SourceLoc Loc);
bool hasDeclAttributes(SourceLoc Loc) const;
DeclAttributes getDeclAttributes(SourceLoc Loc) const;
};
} // namespace swift

Expand Down
4 changes: 2 additions & 2 deletions include/swift/Parse/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ class Lexer {
ParsedTrivia &TrailingTriviaResult) {
Result = NextToken;
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
LeadingTriviaResult = {LeadingTrivia};
TrailingTriviaResult = {TrailingTrivia};
std::swap(LeadingTriviaResult, LeadingTrivia);
std::swap(TrailingTriviaResult, TrailingTrivia);
}
if (Result.isNot(tok::eof))
lexImpl();
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/ParsedSyntaxBuilders.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace swift {
class ParsedRawSyntaxRecorder;
class SyntaxParsingContext;

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.is_buildable():
% child_count = len(node.children)
class Parsed${node.name}Builder {
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Parse/ParsedSyntaxNodes.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ namespace swift {
% # Emit the non-collection classes first, then emit the collection classes
% # that reference these classes.

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if not node.is_syntax_collection():
class Parsed${node.name};
% end
% end

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.is_syntax_collection():
using Parsed${node.name} =
ParsedSyntaxCollection<syntax::SyntaxKind::${node.syntax_kind}>;
% end
% end

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if not node.is_syntax_collection():
% qualifier = "" if node.is_base() else "final"
% for line in dedented_lines(node.description):
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Parse/ParsedSyntaxRecorder.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SyntaxParsingContext;

struct ParsedSyntaxRecorder {

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.children:
% child_params = []
% for child in node.children:
Expand Down
24 changes: 22 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,19 @@ class Parser {
/// plain Tok.is(T1) check).
bool skipUntilTokenOrEndOfLine(tok T1);

//-------------------------------------------------------------------------//
// Ignore token APIs.
// This is used when we skip gabage text in the source text.

/// Ignore the current single token.
void ignoreToken();
void ignoreToken(tok Kind) {
/// Ignore the current single token asserting its kind.
assert(Tok.is(Kind));
ignoreToken();
}
/// Conditionally ignore the current single token if it matches with the \p
/// Kind.
bool ignoreIf(tok Kind) {
if (!Tok.is(Kind))
return false;
Expand Down Expand Up @@ -1173,7 +1181,8 @@ class Parser {
using TypeASTResult = ParserResult<TypeRepr>;
using TypeResult = ParsedSyntaxResult<ParsedTypeSyntax>;

LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);
ParsedSyntaxResult<ParsedLayoutConstraintSyntax>
parseLayoutConstraintSyntax();

TypeResult parseTypeSyntax();
TypeResult parseTypeSyntax(Diag<> MessageID, bool HandleCodeCompletion = true,
Expand Down Expand Up @@ -1597,8 +1606,19 @@ class Parser {
//===--------------------------------------------------------------------===//
// Generics Parsing

ParserResult<GenericParamList> parseSILGenericParams();

ParserStatus parseSILGenericParamsSyntax(
Optional<ParsedGenericParameterClauseListSyntax> &result);

ParsedSyntaxResult<ParsedGenericParameterClauseSyntax>
parseGenericParameterClauseSyntax();

ParsedSyntaxResult<ParsedGenericWhereClauseSyntax>
parseGenericWhereClauseSyntax(bool &FirstTypeInComplete,
bool AllowLayoutConstraints = false);

ParserResult<GenericParamList> parseGenericParameters();
ParserResult<GenericParamList> parseGenericParameters(SourceLoc LAngleLoc);
ParserStatus parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
SmallVectorImpl<GenericTypeParamDecl *> &GenericParams);
ParserResult<GenericParamList> maybeParseGenericParams();
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Syntax/SyntaxBuilders.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace syntax {

class SyntaxArena;

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.is_buildable():
% child_count = len(node.children)
class ${node.name}Builder {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Syntax/SyntaxFactory.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct SyntaxFactory {
static Syntax
makeBlankCollectionSyntax(SyntaxKind Kind);

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.children:
% child_params = []
% for child in node.children:
Expand Down
14 changes: 10 additions & 4 deletions include/swift/Syntax/SyntaxKind.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from gyb_syntax_support import *
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS
grouped_nodes = { kind: [] for kind in SYNTAX_BASE_KINDS }
for node in SYNTAX_NODES:
for node in SYNTAX_NODES + SILONLY_NODES:
grouped_nodes[node.base_kind].append(node)
# -*- mode: C++ -*-
# Ignore the following admonition; it applies to the resulting .h file only
Expand Down Expand Up @@ -89,12 +89,14 @@ struct WrapperTypeTraits<syntax::SyntaxKind> {
return 0;
case syntax::SyntaxKind::Unknown:
return 1;
% for name, nodes in grouped_nodes.items():
% for node in nodes:
% for node in SYNTAX_NODES:
case syntax::SyntaxKind::${node.syntax_kind}:
return ${SYNTAX_NODE_SERIALIZATION_CODES[node.syntax_kind]};
% end
% end
% for node in SILONLY_NODES:
case syntax::SyntaxKind::${node.syntax_kind}:
% end
llvm_unreachable("unserializable syntax kind");
}
llvm_unreachable("unhandled kind");
}
Expand Down Expand Up @@ -122,6 +124,10 @@ struct ScalarReferenceTraits<syntax::SyntaxKind> {
case syntax::SyntaxKind::${node.syntax_kind}:
return "\"${node.syntax_kind}\"";
% end
% for node in SILONLY_NODES:
case syntax::SyntaxKind::${node.syntax_kind}:
% end
llvm_unreachable("unserializable syntax kind");
}
llvm_unreachable("unhandled kind");
}
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Syntax/SyntaxNodes.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ namespace syntax {
% # Emit the non-collection classes first, then emit the collection classes
% # that reference these classes.

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if not node.is_syntax_collection():
class ${node.name};
% end
% end

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if node.is_syntax_collection():
using ${node.name} =
SyntaxCollection<SyntaxKind::${node.syntax_kind},
${node.collection_element_type}>;
% end
% end

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if not node.is_syntax_collection():
% qualifier = "" if node.is_base() else "final"
% for line in dedented_lines(node.description):
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Syntax/SyntaxVisitor.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace syntax {
struct SyntaxVisitor {
virtual ~SyntaxVisitor() {}

% for node in SYNTAX_NODES:
% for node in SYNTAX_NODES + SILONLY_NODES:
% if is_visitable(node):
virtual void visit(${node.name} node);
% end
Expand Down
Loading