Skip to content

Commit 859f90a

Browse files
authored
Revert "[SyntaxParse] Parse associatedtype decl"
1 parent 56edd08 commit 859f90a

File tree

13 files changed

+217
-438
lines changed

13 files changed

+217
-438
lines changed

include/swift/Parse/ASTGen.h

-27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "swift/AST/ASTContext.h"
1717
#include "swift/AST/Decl.h"
1818
#include "swift/AST/Expr.h"
19-
#include "swift/AST/TypeRepr.h"
2019
#include "swift/Parse/PersistentParserState.h"
2120
#include "swift/Syntax/SyntaxNodes.h"
2221
#include "llvm/ADT/DenseMap.h"
@@ -44,30 +43,6 @@ class ASTGen {
4443

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

47-
SourceLoc generateIdentifierDeclName(const syntax::TokenSyntax &Tok,
48-
const SourceLoc, Identifier &Identifier);
49-
50-
public:
51-
//===--------------------------------------------------------------------===//
52-
// Decls.
53-
54-
Decl *generate(const syntax::DeclSyntax &Decl, const SourceLoc Loc);
55-
TypeDecl *generate(const syntax::AssociatedtypeDeclSyntax &Decl,
56-
const SourceLoc Loc);
57-
58-
TrailingWhereClause *generate(const syntax::GenericWhereClauseSyntax &syntax,
59-
const SourceLoc Loc);
60-
MutableArrayRef<TypeLoc>
61-
generate(const syntax::TypeInheritanceClauseSyntax &syntax,
62-
const SourceLoc Loc, bool allowClassRequirement);
63-
64-
private:
65-
DeclAttributes
66-
generateDeclAttributes(const syntax::DeclSyntax &D,
67-
const Optional<syntax::AttributeListSyntax> &attrs,
68-
const Optional<syntax::ModifierListSyntax> &modifiers,
69-
SourceLoc Loc, bool includeComments);
70-
7146
public:
7247
//===--------------------------------------------------------------------===//
7348
// Expressions.
@@ -122,8 +97,6 @@ class ASTGen {
12297
const SourceLoc Loc);
12398
TypeRepr *generate(const syntax::ImplicitlyUnwrappedOptionalTypeSyntax &Type,
12499
const SourceLoc Loc);
125-
TypeRepr *generate(const syntax::ClassRestrictionTypeSyntax &Type,
126-
const SourceLoc Loc);
127100
TypeRepr *generate(const syntax::CodeCompletionTypeSyntax &Type,
128101
const SourceLoc Loc);
129102
TypeRepr *generate(const syntax::UnknownTypeSyntax &Type,

include/swift/Parse/LibSyntaxGenerator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class LibSyntaxGenerator {
3838
assert(Node.isDeferredToken());
3939

4040
auto Kind = Node.getTokenKind();
41-
auto Range = Node.getDeferredTokenRange();
41+
auto Range = Node.getDeferredTokenRangeWithTrivia();
4242
auto LeadingTriviaPieces = Node.getDeferredLeadingTriviaPieces();
4343
auto TrailingTriviaPieces = Node.getDeferredTrailingTriviaPieces();
4444

include/swift/Parse/Parser.h

+4-15
Original file line numberDiff line numberDiff line change
@@ -1004,23 +1004,12 @@ class Parser {
10041004
bool delayParsingDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
10051005
IterableDeclContext *IDC);
10061006

1007-
ParsedSyntaxResult<ParsedTypeInheritanceClauseSyntax>
1008-
parseTypeInheritanceClauseSyntax(bool allowClassRequirement,
1009-
bool allowAnyObject);
1010-
1011-
ParsedSyntaxResult<ParsedDeclSyntax>
1012-
parseDeclAssociatedTypeSyntax(ParseDeclOptions flags,
1013-
Optional<ParsedAttributeListSyntax> attrs,
1014-
Optional<ParsedModifierListSyntax> modifiers);
1015-
10161007
ParserResult<TypeDecl> parseDeclTypeAlias(ParseDeclOptions Flags,
1017-
DeclAttributes &Attributes,
1018-
SourceLoc leadingLoc);
1008+
DeclAttributes &Attributes);
10191009

10201010
ParserResult<TypeDecl> parseDeclAssociatedType(ParseDeclOptions Flags,
1021-
DeclAttributes &Attributes,
1022-
SourceLoc leadingLoc);
1023-
1011+
DeclAttributes &Attributes);
1012+
10241013
/// Parse a #if ... #endif directive.
10251014
/// Delegate callback function to parse elements in the blocks.
10261015
ParserResult<IfConfigDecl> parseIfConfig(
@@ -1102,7 +1091,7 @@ class Parser {
11021091

11031092
ParserResult<ImportDecl> parseDeclImport(ParseDeclOptions Flags,
11041093
DeclAttributes &Attributes);
1105-
ParserStatus parseInheritance(MutableArrayRef<TypeLoc> &Inherited,
1094+
ParserStatus parseInheritance(SmallVectorImpl<TypeLoc> &Inherited,
11061095
bool allowClassRequirement,
11071096
bool allowAnyObject);
11081097
ParserStatus parseDeclItem(bool &PreviousHadSemi,

include/swift/Parse/SyntaxParsingContext.h

+10-21
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,17 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
279279
}
280280

281281
/// Returns the topmost Syntax node.
282-
template <typename SyntaxNode> SyntaxNode topNode();
282+
template <typename SyntaxNode> SyntaxNode topNode() {
283+
ParsedRawSyntaxNode &TopNode = getStorage().back();
284+
if (TopNode.isRecorded()) {
285+
OpaqueSyntaxNode OpaqueNode = TopNode.getOpaqueNode();
286+
return getSyntaxCreator().getLibSyntaxNodeFor<SyntaxNode>(OpaqueNode);
287+
}
288+
return getSyntaxCreator().createNode<SyntaxNode>(TopNode.copyDeferred());
289+
}
283290

284-
template <typename SyntaxNode> llvm::Optional<SyntaxNode> popIf() {
291+
template <typename SyntaxNode>
292+
llvm::Optional<SyntaxNode> popIf() {
285293
auto &Storage = getStorage();
286294
if (Storage.size() <= Offset)
287295
return llvm::None;
@@ -368,24 +376,5 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
368376
"Only meant for use in the debugger");
369377
};
370378

371-
template <typename SyntaxNode>
372-
inline SyntaxNode SyntaxParsingContext::topNode() {
373-
ParsedRawSyntaxNode &TopNode = getStorage().back();
374-
if (TopNode.isRecorded()) {
375-
OpaqueSyntaxNode OpaqueNode = TopNode.getOpaqueNode();
376-
return getSyntaxCreator().getLibSyntaxNodeFor<SyntaxNode>(OpaqueNode);
377-
}
378-
return getSyntaxCreator().createNode<SyntaxNode>(TopNode.copyDeferred());
379-
}
380-
381-
template <> inline TokenSyntax SyntaxParsingContext::topNode<TokenSyntax>() {
382-
ParsedRawSyntaxNode &TopNode = getStorage().back();
383-
if (TopNode.isRecorded()) {
384-
OpaqueSyntaxNode OpaqueNode = TopNode.getOpaqueNode();
385-
return getSyntaxCreator().getLibSyntaxNodeFor<TokenSyntax>(OpaqueNode);
386-
}
387-
return getSyntaxCreator().createToken(TopNode.copyDeferred());
388-
}
389-
390379
} // namespace swift
391380
#endif // SWIFT_SYNTAX_PARSING_CONTEXT_H

include/swift/Syntax/Syntax.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Syntax {
8383
SyntaxKind getKind() const;
8484

8585
/// Get the shared raw syntax.
86-
const RC<RawSyntax> &getRaw() const;
86+
RC<RawSyntax> getRaw() const;
8787

8888
/// Get an ID for this node that is stable across incremental parses
8989
SyntaxNodeId getId() const { return getRaw()->getId(); }

include/swift/Syntax/SyntaxData.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class SyntaxData final
186186
CursorIndex IndexInParent = 0);
187187

188188
/// Returns the raw syntax node for this syntax node.
189-
const RC<RawSyntax> &getRaw() const {
189+
const RC<RawSyntax> getRaw() const {
190190
return Raw;
191191
}
192192

lib/Parse/ASTGen.cpp

+6-137
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "swift/Parse/ASTGen.h"
1414

15-
#include "DebuggerContextChange.h"
1615
#include "swift/Basic/SourceManager.h"
1716
#include "swift/Parse/CodeCompletionCallbacks.h"
1817
#include "swift/Parse/Parser.h"
@@ -24,132 +23,6 @@ SourceLoc ASTGen::generate(const TokenSyntax &Tok, const SourceLoc Loc) {
2423
return advanceLocBegin(Loc, Tok);
2524
}
2625

27-
SourceLoc ASTGen::generateIdentifierDeclName(const syntax::TokenSyntax &Tok,
28-
const SourceLoc Loc,
29-
Identifier &Id) {
30-
StringRef text;
31-
if (Tok.getText() == "Any")
32-
// Special handle 'Any' because we don't want to accidantaly declare 'Any'
33-
// type in any way.
34-
text = "#Any";
35-
else
36-
text = Tok.getIdentifierText();
37-
38-
Id = Context.getIdentifier(text);
39-
return advanceLocBegin(Loc, Tok);
40-
}
41-
42-
Decl *ASTGen::generate(const DeclSyntax &D, const SourceLoc Loc) {
43-
Decl *DeclAST = nullptr;
44-
45-
if (auto associatedTypeDecl = D.getAs<AssociatedtypeDeclSyntax>()) {
46-
DeclAST = generate(*associatedTypeDecl, Loc);
47-
} else {
48-
llvm_unreachable("unsupported decl kind");
49-
}
50-
51-
return DeclAST;
52-
}
53-
54-
DeclAttributes
55-
ASTGen::generateDeclAttributes(const DeclSyntax &D,
56-
const Optional<AttributeListSyntax> &attrs,
57-
const Optional<ModifierListSyntax> &modifiers,
58-
SourceLoc Loc, bool includeComments) {
59-
SourceLoc attrsLoc;
60-
if (attrs) {
61-
attrsLoc = advanceLocBegin(Loc, *attrs->getFirstToken());
62-
} else if (modifiers) {
63-
attrsLoc = advanceLocBegin(Loc, *modifiers->getFirstToken());
64-
} else {
65-
// We might have comment attributes.
66-
attrsLoc = advanceLocBegin(Loc, *D.getFirstToken());
67-
}
68-
if (hasDeclAttributes(attrsLoc))
69-
return getDeclAttributes(attrsLoc);
70-
return DeclAttributes();
71-
}
72-
73-
MutableArrayRef<TypeLoc>
74-
ASTGen::generate(const TypeInheritanceClauseSyntax &clause, SourceLoc Loc,
75-
bool allowClassRequirement) {
76-
SmallVector<TypeLoc, 2> inherited;
77-
78-
bool hasClass = false;
79-
for (const auto elem : clause.getInheritedTypeCollection()) {
80-
const auto &tySyntax = elem.getTypeName();
81-
if (tySyntax.is<ClassRestrictionTypeSyntax>()) {
82-
// Accept 'class' only if it's allowed and it's the first one.
83-
if (!allowClassRequirement || hasClass)
84-
continue;
85-
hasClass = true;
86-
}
87-
if (auto ty = generate(tySyntax, Loc))
88-
inherited.emplace_back(ty);
89-
}
90-
91-
return Context.AllocateCopy(inherited);
92-
}
93-
94-
TypeDecl *ASTGen::generate(const AssociatedtypeDeclSyntax &D,
95-
const SourceLoc Loc) {
96-
if (!isa<ProtocolDecl>(P.CurDeclContext)) {
97-
// This is already diagnosed in Parser.
98-
return nullptr;
99-
}
100-
101-
auto idToken = D.getIdentifier();
102-
if (idToken.isMissing())
103-
return nullptr;
104-
105-
auto keywordLoc = advanceLocBegin(Loc, D.getAssociatedtypeKeyword());
106-
auto name = Context.getIdentifier(idToken.getIdentifierText());
107-
auto nameLoc = advanceLocBegin(Loc, idToken);
108-
109-
DeclAttributes attrs =
110-
generateDeclAttributes(D, D.getAttributes(), D.getModifiers(), Loc, true);
111-
112-
DebuggerContextChange DCC(P, name, DeclKind::AssociatedType);
113-
114-
ArrayRef<TypeLoc> inherited;
115-
if (const auto inheritanceClause = D.getInheritanceClause())
116-
inherited =
117-
generate(*inheritanceClause, Loc, /*allowClassRequirement=*/true);
118-
119-
TypeRepr *defaultTy = nullptr;
120-
if (const auto init = D.getInitializer())
121-
defaultTy = generate(init->getValue(), Loc);
122-
123-
TrailingWhereClause *trailingWhere = nullptr;
124-
if (auto whereClause = D.getGenericWhereClause())
125-
trailingWhere = generate(*whereClause, Loc);
126-
127-
auto assocType = new (Context)
128-
AssociatedTypeDecl(P.CurDeclContext, keywordLoc, name, nameLoc, defaultTy,
129-
trailingWhere);
130-
assocType->getAttrs() = attrs;
131-
if (!inherited.empty())
132-
assocType->setInherited(Context.AllocateCopy(inherited));
133-
addToScope(assocType);
134-
return assocType;
135-
}
136-
137-
TrailingWhereClause *ASTGen::generate(const GenericWhereClauseSyntax &syntax,
138-
const SourceLoc Loc) {
139-
SourceLoc whereLoc = advanceLocBegin(Loc, syntax.getWhereKeyword());
140-
141-
SmallVector<RequirementRepr, 4> requirements;
142-
requirements.reserve(syntax.getRequirementList().size());
143-
for (auto elem : syntax.getRequirementList()) {
144-
if (auto req = generate(elem, Loc))
145-
requirements.push_back(*req);
146-
}
147-
148-
if (requirements.empty())
149-
return nullptr;
150-
return TrailingWhereClause::create(Context, whereLoc, requirements);
151-
}
152-
15326
Expr *ASTGen::generate(const IntegerLiteralExprSyntax &Expr,
15427
const SourceLoc Loc) {
15528
auto Digits = Expr.getDigits();
@@ -251,8 +124,6 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
251124
TypeAST = generate(*Unwrapped, Loc);
252125
else if (auto Attributed = Type.getAs<AttributedTypeSyntax>())
253126
TypeAST = generate(*Attributed, Loc);
254-
else if (auto ClassRestriction = Type.getAs<ClassRestrictionTypeSyntax>())
255-
TypeAST = generate(*ClassRestriction, Loc);
256127
else if (auto CompletionTy = Type.getAs<CodeCompletionTypeSyntax>())
257128
TypeAST = generate(*CompletionTy, Loc);
258129
else if (auto Unknown = Type.getAs<UnknownTypeSyntax>())
@@ -513,6 +384,11 @@ TypeRepr *ASTGen::generate(const SimpleTypeIdentifierSyntax &Type,
513384
auto AnyLoc = advanceLocBegin(Loc, Type.getName());
514385
return CompositionTypeRepr::createEmptyComposition(Context, AnyLoc);
515386
}
387+
if (Type.getName().getText() == "class") {
388+
auto classLoc = advanceLocBegin(Loc, Type.getName());
389+
return new (Context)
390+
SimpleIdentTypeRepr(classLoc, Context.getIdentifier("AnyObject"));
391+
}
516392

517393
return generateSimpleOrMemberIdentifier(Type, Loc);
518394
}
@@ -572,13 +448,6 @@ TypeRepr *ASTGen::generate(const ImplicitlyUnwrappedOptionalTypeSyntax &Type,
572448
ImplicitlyUnwrappedOptionalTypeRepr(WrappedType, ExclamationLoc);
573449
}
574450

575-
TypeRepr *
576-
ASTGen::generate(const ClassRestrictionTypeSyntax &Type, const SourceLoc Loc) {
577-
auto classLoc = advanceLocBegin(Loc, Type);
578-
return new (Context)
579-
SimpleIdentTypeRepr(classLoc, Context.getIdentifier("AnyObject"));
580-
}
581-
582451
TypeRepr *ASTGen::generate(const CodeCompletionTypeSyntax &Type,
583452
const SourceLoc Loc) {
584453
auto base = Type.getBase();
@@ -720,7 +589,7 @@ GenericParamList *ASTGen::generate(const GenericParameterClauseSyntax &clause,
720589

721590
if (auto inherited = elem.getInheritedType()) {
722591
if (auto ty = generate(*inherited, Loc)) {
723-
SmallVector<TypeLoc, 1> constraints = {ty};
592+
SmallVector<TypeLoc, 1> constraints = {generate(*inherited, Loc)};
724593
param->setInherited(Context.AllocateCopy(constraints));
725594
}
726595
}

0 commit comments

Comments
 (0)