Skip to content

[ASTGen] Adjust geneate() function signatures #27291

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
Sep 23, 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
169 changes: 105 additions & 64 deletions include/swift/Parse/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,87 +31,130 @@ class ASTGen {

Parser &P;

// FIXME: remove when Syntax can represent all types and ASTGen can handle them
// 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, Parser &P)
: Context(Context), P(P) {}

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

Expr *generate(syntax::IntegerLiteralExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::FloatLiteralExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::NilLiteralExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::BooleanLiteralExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::PoundFileExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::PoundLineExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::PoundColumnExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::PoundFunctionExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::PoundDsohandleExprSyntax &Expr, SourceLoc &Loc);
Expr *generate(syntax::UnknownExprSyntax &Expr, SourceLoc &Loc);

TypeRepr *generate(syntax::TypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::SomeTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::CompositionTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::SimpleTypeIdentifierSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::MemberTypeIdentifierSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::DictionaryTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::ArrayTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::TupleTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::AttributedTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::FunctionTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::MetatypeTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::OptionalTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::ImplicitlyUnwrappedOptionalTypeSyntax Type, SourceLoc &Loc);
TypeRepr *generate(syntax::UnknownTypeSyntax Type, SourceLoc &Loc);

TypeRepr *generate(syntax::GenericArgumentSyntax Arg, SourceLoc &Loc);
llvm::SmallVector<TypeRepr *, 4>
generate(syntax::GenericArgumentListSyntax Args, SourceLoc &Loc);
ASTGen(ASTContext &Context, Parser &P) : Context(Context), P(P) {}

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);
SourceLoc generate(const syntax::TokenSyntax &Tok, const 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);
public:
//===--------------------------------------------------------------------===//
// Expressions.

Expr *generate(const syntax::IntegerLiteralExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::FloatLiteralExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::NilLiteralExprSyntax &Expr, const SourceLoc Loc);
Expr *generate(const syntax::BooleanLiteralExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::PoundFileExprSyntax &Expr, const SourceLoc Loc);
Expr *generate(const syntax::PoundLineExprSyntax &Expr, const SourceLoc Loc);
Expr *generate(const syntax::PoundColumnExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::PoundFunctionExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::PoundDsohandleExprSyntax &Expr,
const SourceLoc Loc);
Expr *generate(const syntax::UnknownExprSyntax &Expr, const SourceLoc Loc);

private:
Expr *generateMagicIdentifierLiteralExpression(syntax::TokenSyntax PoundToken,
SourceLoc &Loc);
Expr *generateMagicIdentifierLiteralExpression(
const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);

TupleTypeRepr *generateTuple(syntax::TokenSyntax LParen,
syntax::TupleTypeElementListSyntax Elements,
syntax::TokenSyntax RParen, SourceLoc &Loc,
bool IsFunction = false);
static MagicIdentifierLiteralExpr::Kind
getMagicIdentifierLiteralKind(tok Kind);

public:
//===--------------------------------------------------------------------===//
// Types.

TypeRepr *generate(const syntax::TypeSyntax &Type, const SourceLoc Loc);
TypeRepr *generate(const syntax::SomeTypeSyntax &Type, const SourceLoc Loc);
TypeRepr *generate(const syntax::CompositionTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::SimpleTypeIdentifierSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::MemberTypeIdentifierSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::DictionaryTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::ArrayTypeSyntax &Type, const SourceLoc Loc);
TypeRepr *generate(const syntax::TupleTypeSyntax &Type, const SourceLoc Loc);
TypeRepr *generate(const syntax::AttributedTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::FunctionTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::MetatypeTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::OptionalTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::ImplicitlyUnwrappedOptionalTypeSyntax &Type,
const SourceLoc Loc);
TypeRepr *generate(const syntax::UnknownTypeSyntax &Type,
const SourceLoc Loc);

private:
TupleTypeRepr *
generateTuple(const syntax::TokenSyntax &LParen,
const syntax::TupleTypeElementListSyntax &Elements,
const syntax::TokenSyntax &RParen, const SourceLoc Loc,
bool IsFunction = false);

void gatherTypeIdentifierComponents(
syntax::TypeSyntax Component, SourceLoc &Loc,
const syntax::TypeSyntax &Component, const SourceLoc Loc,
llvm::SmallVectorImpl<ComponentIdentTypeRepr *> &Components);

template <typename T>
TypeRepr *generateSimpleOrMemberIdentifier(T Type, SourceLoc &Loc);
TypeRepr *generateSimpleOrMemberIdentifier(const T &Type,
const SourceLoc Loc);

template <typename T>
ComponentIdentTypeRepr *generateIdentifier(T Type, SourceLoc &Loc);
ComponentIdentTypeRepr *generateIdentifier(const T &Type,
const SourceLoc Loc);

public:
//===--------------------------------------------------------------------===//
// Generics.

TypeRepr *generate(const syntax::GenericArgumentSyntax &Arg,
const SourceLoc Loc);
llvm::SmallVector<TypeRepr *, 4>
generate(const syntax::GenericArgumentListSyntax &Args, const SourceLoc Loc);

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

public:
//===--------------------------------------------------------------------===//
// Utilities.

/// 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);

private:
StringRef copyAndStripUnderscores(StringRef Orig);

/// Advance \p Loc to the first token of the \p Node.
/// \p Loc must be the leading trivia of the first token in the tree in which
/// \p Node resides.
static SourceLoc advanceLocBegin(const SourceLoc &Loc,
const syntax::Syntax &Node);

static MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind);

ValueDecl *lookupInScope(DeclName Name);

void addToScope(ValueDecl *D, bool diagnoseRedefinitions = true);
Expand All @@ -121,15 +164,13 @@ class ASTGen {
TypeRepr *lookupType(syntax::TypeSyntax Type);

public:
TypeRepr *addType(TypeRepr *Type, const SourceLoc &Loc);

bool hasType(const SourceLoc &Loc) const;

TypeRepr *getType(const SourceLoc &Loc) const;
void addType(TypeRepr *Type, const SourceLoc Loc);
bool hasType(const SourceLoc Loc) const;
TypeRepr *getType(const SourceLoc Loc) const;

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

Expand Down
4 changes: 2 additions & 2 deletions include/swift/Syntax/Syntax.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ class Syntax {

/// Returns the first non-missing token in this syntax. Returns None if there
/// is no non-missing token.
Optional<TokenSyntax> getFirstToken();
Optional<TokenSyntax> getFirstToken() const;

/// Returns the last non-missing token in this syntax. Returns None if there
/// is no non-missing token.
Optional<TokenSyntax> getLastToken();
Optional<TokenSyntax> getLastToken() const;

/// Print the syntax node with full fidelity to the given output stream.
void print(llvm::raw_ostream &OS, SyntaxPrintOptions Opts = SyntaxPrintOptions()) const;
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Syntax/SyntaxNodes.h.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public:
/// ${line}
% end
% if child.is_optional:
llvm::Optional<${child.type_name}> get${child.name}();
llvm::Optional<${child.type_name}> get${child.name}() const;
% else:
${child.type_name} get${child.name}();
${child.type_name} get${child.name}() const;
% end

% child_node = NODE_MAP.get(child.syntax_kind)
Expand Down
Loading