Skip to content

Commit faaa3a8

Browse files
authored
Merge pull request swiftlang#26883 from rintaro/revert-revert-26478-gsoc-2019-parser-types
Re-apply "[Parser] Decouple the parser from AST creation (part 2)"
2 parents 62f947d + 5103789 commit faaa3a8

31 files changed

+1968
-770
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,108 @@
1414
#define SWIFT_PARSE_ASTGEN_H
1515

1616
#include "swift/AST/ASTContext.h"
17+
#include "swift/AST/Decl.h"
1718
#include "swift/AST/Expr.h"
19+
#include "swift/Parse/PersistentParserState.h"
1820
#include "swift/Syntax/SyntaxNodes.h"
1921
#include "llvm/ADT/DenseMap.h"
2022

2123
namespace swift {
2224
/// Generates AST nodes from Syntax nodes.
2325
class ASTGen {
2426
ASTContext &Context;
25-
// A stack of source locations of syntax constructs. Allows us to get the
26-
// SourceLoc necessary to create AST nodes for nodes in not-yet-complete
27-
// Syntax tree. The topmost item should always correspond to the token/node
28-
// that has been parsed/transformed most recently.
29-
// todo [gsoc]: remove when possible
30-
llvm::SmallVector<SourceLoc, 16> LocStack;
27+
28+
/// Type cache to prevent multiple transformations of the same syntax node.
29+
llvm::DenseMap<syntax::SyntaxNodeId, TypeRepr *> TypeCache;
30+
31+
PersistentParserState **ParserState;
32+
33+
// FIXME: remove when Syntax can represent all types and ASTGen can handle them
34+
/// Types that cannot be represented by Syntax or generated by ASTGen.
35+
llvm::DenseMap<SourceLoc, TypeRepr *> Types;
3136

3237
public:
33-
explicit ASTGen(ASTContext &Context) : Context(Context) {}
34-
35-
IntegerLiteralExpr *generate(syntax::IntegerLiteralExprSyntax &Expr);
36-
FloatLiteralExpr *generate(syntax::FloatLiteralExprSyntax &Expr);
37-
NilLiteralExpr *generate(syntax::NilLiteralExprSyntax &Expr);
38-
BooleanLiteralExpr *generate(syntax::BooleanLiteralExprSyntax &Expr);
39-
MagicIdentifierLiteralExpr *generate(syntax::PoundFileExprSyntax &Expr);
40-
MagicIdentifierLiteralExpr *generate(syntax::PoundLineExprSyntax &Expr);
41-
MagicIdentifierLiteralExpr *generate(syntax::PoundColumnExprSyntax &Expr);
42-
MagicIdentifierLiteralExpr *generate(syntax::PoundFunctionExprSyntax &Expr);
43-
MagicIdentifierLiteralExpr *generate(syntax::PoundDsohandleExprSyntax &Expr);
44-
Expr *generate(syntax::UnknownExprSyntax &Expr);
45-
46-
/// Stores source location necessary for AST creation.
47-
void pushLoc(SourceLoc Loc);
38+
ASTGen(ASTContext &Context, PersistentParserState **ParserState)
39+
: Context(Context), ParserState(ParserState) {}
40+
41+
SourceLoc generate(syntax::TokenSyntax Tok, SourceLoc &Loc);
42+
43+
Expr *generate(syntax::IntegerLiteralExprSyntax &Expr, SourceLoc &Loc);
44+
Expr *generate(syntax::FloatLiteralExprSyntax &Expr, SourceLoc &Loc);
45+
Expr *generate(syntax::NilLiteralExprSyntax &Expr, SourceLoc &Loc);
46+
Expr *generate(syntax::BooleanLiteralExprSyntax &Expr, SourceLoc &Loc);
47+
Expr *generate(syntax::PoundFileExprSyntax &Expr, SourceLoc &Loc);
48+
Expr *generate(syntax::PoundLineExprSyntax &Expr, SourceLoc &Loc);
49+
Expr *generate(syntax::PoundColumnExprSyntax &Expr, SourceLoc &Loc);
50+
Expr *generate(syntax::PoundFunctionExprSyntax &Expr, SourceLoc &Loc);
51+
Expr *generate(syntax::PoundDsohandleExprSyntax &Expr, SourceLoc &Loc);
52+
Expr *generate(syntax::UnknownExprSyntax &Expr, SourceLoc &Loc);
53+
54+
TypeRepr *generate(syntax::TypeSyntax Type, SourceLoc &Loc);
55+
TypeRepr *generate(syntax::SomeTypeSyntax Type, SourceLoc &Loc);
56+
TypeRepr *generate(syntax::CompositionTypeSyntax Type, SourceLoc &Loc);
57+
TypeRepr *generate(syntax::SimpleTypeIdentifierSyntax Type, SourceLoc &Loc);
58+
TypeRepr *generate(syntax::MemberTypeIdentifierSyntax Type, SourceLoc &Loc);
59+
TypeRepr *generate(syntax::DictionaryTypeSyntax Type, SourceLoc &Loc);
60+
TypeRepr *generate(syntax::ArrayTypeSyntax Type, SourceLoc &Loc);
61+
TypeRepr *generate(syntax::TupleTypeSyntax Type, SourceLoc &Loc);
62+
TypeRepr *generate(syntax::AttributedTypeSyntax Type, SourceLoc &Loc);
63+
TypeRepr *generate(syntax::FunctionTypeSyntax Type, SourceLoc &Loc);
64+
TypeRepr *generate(syntax::MetatypeTypeSyntax Type, SourceLoc &Loc);
65+
TypeRepr *generate(syntax::OptionalTypeSyntax Type, SourceLoc &Loc);
66+
TypeRepr *generate(syntax::ImplicitlyUnwrappedOptionalTypeSyntax Type, SourceLoc &Loc);
67+
TypeRepr *generate(syntax::UnknownTypeSyntax Type, SourceLoc &Loc);
68+
69+
TypeRepr *generate(syntax::GenericArgumentSyntax Arg, SourceLoc &Loc);
70+
llvm::SmallVector<TypeRepr *, 4>
71+
generate(syntax::GenericArgumentListSyntax Args, SourceLoc &Loc);
4872

4973
/// Copy a numeric literal value into AST-owned memory, stripping underscores
5074
/// so the semantic part of the value can be parsed by APInt/APFloat parsers.
5175
static StringRef copyAndStripUnderscores(StringRef Orig, ASTContext &Context);
5276

5377
private:
78+
Expr *generateMagicIdentifierLiteralExpression(syntax::TokenSyntax PoundToken,
79+
SourceLoc &Loc);
80+
81+
TupleTypeRepr *generateTuple(syntax::TokenSyntax LParen,
82+
syntax::TupleTypeElementListSyntax Elements,
83+
syntax::TokenSyntax RParen, SourceLoc &Loc,
84+
bool IsFunction = false);
85+
86+
void gatherTypeIdentifierComponents(
87+
syntax::TypeSyntax Component, SourceLoc &Loc,
88+
llvm::SmallVectorImpl<ComponentIdentTypeRepr *> &Components);
89+
90+
template <typename T>
91+
TypeRepr *generateSimpleOrMemberIdentifier(T Type, SourceLoc &Loc);
92+
93+
template <typename T>
94+
ComponentIdentTypeRepr *generateIdentifier(T Type, SourceLoc &Loc);
95+
5496
StringRef copyAndStripUnderscores(StringRef Orig);
5597

56-
SourceLoc topLoc();
98+
static SourceLoc advanceLocBegin(const SourceLoc &Loc,
99+
const syntax::Syntax &Node);
100+
static SourceLoc advanceLocEnd(const SourceLoc &Loc,
101+
const syntax::TokenSyntax &Token);
102+
static SourceLoc advanceLocAfter(const SourceLoc &Loc,
103+
const syntax::Syntax &Node);
104+
105+
static MagicIdentifierLiteralExpr::Kind getMagicIdentifierLiteralKind(tok Kind);
106+
107+
ValueDecl *lookupInScope(DeclName Name);
108+
109+
TypeRepr *cacheType(syntax::TypeSyntax Type, TypeRepr *TypeAST);
110+
111+
TypeRepr *lookupType(syntax::TypeSyntax Type);
112+
113+
public:
114+
TypeRepr *addType(TypeRepr *Type, const SourceLoc &Loc);
57115

58-
MagicIdentifierLiteralExpr *
59-
generateMagicIdentifierLiteralExpr(const syntax::TokenSyntax &PoundToken);
116+
bool hasType(const SourceLoc &Loc) const;
60117

61-
/// Map magic literal tokens such as #file to their MagicIdentifierLiteralExpr
62-
/// kind.
63-
static MagicIdentifierLiteralExpr::Kind
64-
getMagicIdentifierLiteralKind(tok Kind);
118+
TypeRepr *getType(const SourceLoc &Loc) const;
65119
};
66120
} // namespace swift
67121

include/swift/Parse/ParsedRawSyntaxNode.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,20 @@ class ParsedRawSyntaxNode {
136136
/// Primary used for a deferred missing token.
137137
bool isMissing() const { return IsMissing; }
138138

139+
CharSourceRange getDeferredRange() const {
140+
switch (DK) {
141+
case DataKind::DeferredLayout:
142+
return getDeferredLayoutRange();
143+
case DataKind::DeferredToken:
144+
return getDeferredTokenRangeWithoutBackticks();
145+
default:
146+
llvm_unreachable("node not deferred");
147+
}
148+
}
149+
139150
// Recorded Data ===========================================================//
140151

141-
CharSourceRange getRange() const {
152+
CharSourceRange getRecordedRange() const {
142153
assert(isRecorded());
143154
return RecordedData.Range;
144155
}
@@ -149,6 +160,20 @@ class ParsedRawSyntaxNode {
149160

150161
// Deferred Layout Data ====================================================//
151162

163+
CharSourceRange getDeferredLayoutRange() const {
164+
assert(DK == DataKind::DeferredLayout);
165+
assert(!DeferredLayout.Children.empty());
166+
auto getLastNonNullChild = [this]() {
167+
for (auto &&Child : llvm::reverse(getDeferredChildren()))
168+
if (!Child.isNull())
169+
return Child;
170+
llvm_unreachable("layout node without non-null children");
171+
};
172+
auto firstRange = DeferredLayout.Children.front().getDeferredRange();
173+
auto lastRange = getLastNonNullChild().getDeferredRange();
174+
firstRange.widen(lastRange);
175+
return firstRange;
176+
}
152177
ArrayRef<ParsedRawSyntaxNode> getDeferredChildren() const {
153178
assert(DK == DataKind::DeferredLayout);
154179
return DeferredLayout.Children;

0 commit comments

Comments
 (0)