Skip to content

Commit e53ad15

Browse files
committed
Revert "Merge pull request swiftlang#27508 from rintaro/syntaxparse-expridentifier"
This reverts commit dfe962f, reversing changes made to 04ce7bf.
1 parent 550865c commit e53ad15

File tree

7 files changed

+201
-386
lines changed

7 files changed

+201
-386
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ class ASTGen {
8181
//===--------------------------------------------------------------------===//
8282
// Expressions.
8383

84-
Expr *generate(const syntax::ExprSyntax &Expr, const SourceLoc Loc);
85-
Expr *generate(const syntax::IdentifierExprSyntax &Expr, const SourceLoc Loc);
86-
Expr *generate(const syntax::EditorPlaceholderExprSyntax &Expr,
87-
const SourceLoc Loc);
88-
Expr *generate(const syntax::SpecializeExprSyntax &Expr, const SourceLoc Loc);
8984
Expr *generate(const syntax::IntegerLiteralExprSyntax &Expr,
9085
const SourceLoc Loc);
9186
Expr *generate(const syntax::FloatLiteralExprSyntax &Expr,
@@ -103,13 +98,7 @@ class ASTGen {
10398
const SourceLoc Loc);
10499
Expr *generate(const syntax::UnknownExprSyntax &Expr, const SourceLoc Loc);
105100

106-
std::pair<DeclName, DeclNameLoc> generateUnqualifiedDeclName(
107-
const syntax::TokenSyntax &idTok,
108-
const Optional<syntax::DeclNameArgumentsSyntax> &args,
109-
const SourceLoc Loc);
110-
111101
private:
112-
113102
Expr *generateMagicIdentifierLiteralExpression(
114103
const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);
115104

@@ -181,9 +170,10 @@ class ASTGen {
181170
//===--------------------------------------------------------------------===//
182171
// Generics.
183172

184-
void generate(const syntax::GenericArgumentClauseSyntax &Arg,
185-
const SourceLoc Loc, SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
186-
SmallVectorImpl<TypeRepr *> &args);
173+
TypeRepr *generate(const syntax::GenericArgumentSyntax &Arg,
174+
const SourceLoc Loc);
175+
llvm::SmallVector<TypeRepr *, 4>
176+
generate(const syntax::GenericArgumentListSyntax &Args, const SourceLoc Loc);
187177

188178
GenericParamList *
189179
generate(const syntax::GenericParameterClauseListSyntax &clause,

include/swift/Parse/Parser.h

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,6 @@ class Parser {
14811481
/// _)
14821482
/// \param loc The location of the label (empty if it doesn't exist)
14831483
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc);
1484-
bool parseOptionalArgumentLabelSyntax(Optional<ParsedTokenSyntax> &name,
1485-
Optional<ParsedTokenSyntax> &colon);
14861484

14871485
/// Parse an unqualified-decl-name.
14881486
///
@@ -1501,20 +1499,10 @@ class Parser {
15011499
bool allowOperators=false,
15021500
bool allowZeroArgCompoundNames=false,
15031501
bool allowDeinitAndSubscript=false);
1504-
ParserStatus
1505-
parseUnqualifiedDeclNameSyntax(Optional<ParsedTokenSyntax> &identTok,
1506-
Optional<ParsedDeclNameArgumentsSyntax> &declNameArg,
1507-
bool afterDot, const Diagnostic &diag,
1508-
bool allowOperators=false,
1509-
bool allowZeroArgCompoundNames=false,
1510-
bool allowDeinitAndSubscript=false);
1511-
1512-
ParsedSyntaxResult<ParsedExprSyntax> parseExprIdentifierSyntax();
1513-
ParsedSyntaxResult<ParsedExprSyntax>
1514-
parseExprSpecializeSyntax(ParsedExprSyntax &&);
15151502

15161503
Expr *parseExprIdentifier();
1517-
Expr *parseExprEditorPlaceholder(SourceLoc loc, StringRef text);
1504+
Expr *parseExprEditorPlaceholder(Token PlaceholderTok,
1505+
Identifier PlaceholderId);
15181506

15191507
/// Parse a closure expression after the opening brace.
15201508
///

lib/Parse/ASTGen.cpp

Lines changed: 25 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -238,166 +238,6 @@ TrailingWhereClause *ASTGen::generate(const GenericWhereClauseSyntax &syntax,
238238
return TrailingWhereClause::create(Context, whereLoc, requirements);
239239
}
240240

241-
Expr *ASTGen::generate(const ExprSyntax &E, const SourceLoc Loc) {
242-
Expr *result = nullptr;
243-
244-
if (auto identifierExpr = E.getAs<IdentifierExprSyntax>())
245-
result = generate(*identifierExpr, Loc);
246-
else if (auto specializeExpr = E.getAs<SpecializeExprSyntax>())
247-
result = generate(*specializeExpr, Loc);
248-
else if (auto editorPlaceHolderExpr = E.getAs<EditorPlaceholderExprSyntax>())
249-
result = generate(*editorPlaceHolderExpr, Loc);
250-
else if (auto integerLiteralExpr = E.getAs<IntegerLiteralExprSyntax>())
251-
result = generate(*integerLiteralExpr, Loc);
252-
else if (auto floatLiteralExpr = E.getAs<FloatLiteralExprSyntax>())
253-
result = generate(*floatLiteralExpr, Loc);
254-
else if (auto nilLiteral = E.getAs<NilLiteralExprSyntax>())
255-
result = generate(*nilLiteral, Loc);
256-
else if (auto boolLiteral = E.getAs<BooleanLiteralExprSyntax>())
257-
result = generate(*boolLiteral, Loc);
258-
else if (auto poundFileExpr = E.getAs<PoundFileExprSyntax>())
259-
result = generate(*poundFileExpr, Loc);
260-
else if (auto poundLineExpr = E.getAs<PoundLineExprSyntax>())
261-
result = generate(*poundLineExpr, Loc);
262-
else if (auto poundColumnExpr = E.getAs<PoundColumnExprSyntax>())
263-
result = generate(*poundColumnExpr, Loc);
264-
else if (auto poundFunctionExpr = E.getAs<PoundFunctionExprSyntax>())
265-
result = generate(*poundFunctionExpr, Loc);
266-
else if (auto poundDsohandleExpr = E.getAs<PoundDsohandleExprSyntax>())
267-
result = generate(*poundDsohandleExpr, Loc);
268-
else
269-
llvm_unreachable("unsupported expression");
270-
271-
return result;
272-
}
273-
274-
std::pair<DeclName, DeclNameLoc> ASTGen::generateUnqualifiedDeclName(
275-
const TokenSyntax &idTok, const Optional<DeclNameArgumentsSyntax> &args,
276-
const SourceLoc Loc) {
277-
SourceLoc baseNameLoc = advanceLocBegin(Loc, idTok);
278-
279-
DeclBaseName baseName;
280-
if (idTok.getTokenKind() == tok::kw_init)
281-
baseName = DeclBaseName::createConstructor();
282-
else if (idTok.getTokenKind() == tok::kw_deinit)
283-
baseName = DeclBaseName::createDestructor();
284-
else if (idTok.getTokenKind() == tok::kw_subscript)
285-
baseName = DeclBaseName::createSubscript();
286-
else
287-
baseName = Context.getIdentifier(idTok.getIdentifierText());
288-
289-
if (!args)
290-
return {DeclName(baseName), DeclNameLoc(baseNameLoc)};
291-
292-
// FIXME: Remove this block and use 'Loc'.
293-
// This is needed for the case 'idTok' and 'args' are not in the same tree.
294-
// i.e. Call from parseUnqualifiedDeclName().
295-
SourceLoc argsLeadingLoc = Loc;
296-
if (!args->getParent()) {
297-
argsLeadingLoc = Loc.getAdvancedLoc(idTok.getTextLength());
298-
} else {
299-
assert(idTok.getData().getParent() == args->getData().getParent() &&
300-
idTok.getIndexInParent() + 1 == args->getIndexInParent() &&
301-
"'idTok' must be immediately followed by 'args'");
302-
}
303-
304-
SmallVector<Identifier, 2> argumentLabels;
305-
SmallVector<SourceLoc, 2> argumentLabelLocs;
306-
for (auto arg : args->getArguments()) {
307-
Identifier label;
308-
if (!arg.getName().isMissing() &&
309-
arg.getName().getTokenKind() != tok::kw__) {
310-
label = Context.getIdentifier(arg.getName().getIdentifierText());
311-
}
312-
argumentLabels.push_back(label);
313-
argumentLabelLocs.push_back(advanceLocBegin(argsLeadingLoc,
314-
*arg.getFirstToken()));
315-
}
316-
SourceLoc lParenLoc = advanceLocBegin(argsLeadingLoc, args->getLeftParen());
317-
SourceLoc rParenLoc = advanceLocBegin(argsLeadingLoc, args->getRightParen());
318-
319-
DeclName name(Context, baseName, argumentLabels);
320-
DeclNameLoc nameLoc;
321-
if (argumentLabelLocs.empty())
322-
nameLoc = DeclNameLoc(baseNameLoc);
323-
else
324-
nameLoc = DeclNameLoc(Context, baseNameLoc, lParenLoc, argumentLabelLocs,
325-
rParenLoc);
326-
return {name, nameLoc};
327-
}
328-
329-
Expr *ASTGen::generate(const IdentifierExprSyntax &E, const SourceLoc Loc) {
330-
auto idTok = E.getIdentifier();
331-
DeclName name;
332-
DeclNameLoc nameLoc;
333-
std::tie(name, nameLoc) = generateUnqualifiedDeclName(
334-
E.getIdentifier(), E.getDeclNameArguments(), Loc);
335-
336-
ValueDecl *D = nullptr;
337-
if (!P.InPoundIfEnvironment) {
338-
D = lookupInScope(name);
339-
// FIXME: We want this to work: "var x = { x() }", but for now it's better
340-
// to disallow it than to crash.
341-
if (D) {
342-
for (auto activeVar : P.DisabledVars) {
343-
if (activeVar != D)
344-
continue;
345-
P.diagnose(nameLoc.getBaseNameLoc(), P.DisabledVarReason);
346-
return new (Context) ErrorExpr(nameLoc.getSourceRange());
347-
}
348-
} else {
349-
for (auto activeVar : P.DisabledVars) {
350-
if (activeVar->getFullName() != name)
351-
continue;
352-
P.diagnose(nameLoc.getBaseNameLoc(), P.DisabledVarReason);
353-
return new (Context) ErrorExpr(nameLoc.getSourceRange());
354-
}
355-
}
356-
}
357-
358-
if (!D) {
359-
return new (Context)
360-
UnresolvedDeclRefExpr(name, DeclRefKind::Ordinary, nameLoc);
361-
}
362-
363-
if (auto TD = dyn_cast<TypeDecl>(D)) {
364-
// When parsing default argument expressions for generic functions,
365-
// we haven't built a FuncDecl or re-parented the GenericTypeParamDecls
366-
// to the FuncDecl yet. Other than that, we should only ever find
367-
// global or local declarations here.
368-
assert(!TD->getDeclContext()->isTypeContext() ||
369-
isa<GenericTypeParamDecl>(TD));
370-
return TypeExpr::createForDecl(nameLoc.getBaseNameLoc(), TD,
371-
/*DeclContext=*/nullptr,
372-
/*inplicit=*/false);
373-
}
374-
375-
return new (Context) DeclRefExpr(D, nameLoc, /*implicit=*/false);
376-
}
377-
378-
Expr *ASTGen::generate(const EditorPlaceholderExprSyntax &E, const SourceLoc Loc) {
379-
assert(!E.getIdentifier().isMissing());
380-
381-
auto text = E.getIdentifier().getText();
382-
auto tokLoc = advanceLocBegin(Loc, E.getIdentifier());
383-
return P.parseExprEditorPlaceholder(tokLoc, text);
384-
}
385-
386-
Expr *ASTGen::generate(const SpecializeExprSyntax &E, const SourceLoc Loc) {
387-
auto base = generate(E.getExpression(), Loc);
388-
389-
SourceLoc lAngleLoc, rAngleLoc;
390-
SmallVector<TypeRepr *, 4> argTyRs;
391-
generate(E.getGenericArgumentClause(), Loc, lAngleLoc, rAngleLoc, argTyRs);
392-
if (argTyRs.empty())
393-
return base;
394-
395-
SmallVector<TypeLoc, 4> args;
396-
args.assign(argTyRs.begin(), argTyRs.end());
397-
return UnresolvedSpecializeExpr::create(Context, base, lAngleLoc, args,
398-
rAngleLoc);
399-
}
400-
401241
Expr *ASTGen::generate(const IntegerLiteralExprSyntax &Expr,
402242
const SourceLoc Loc) {
403243
auto Digits = Expr.getDigits();
@@ -830,12 +670,15 @@ ComponentIdentTypeRepr *ASTGen::generateIdentifier(const T &Type,
830670
auto IdentifierLoc = advanceLocBegin(Loc, Type.getName());
831671
auto Identifier = Context.getIdentifier(Type.getName().getIdentifierText());
832672
if (auto Clause = Type.getGenericArgumentClause()) {
833-
SourceLoc lAngleLoc, rAngleLoc;
834-
SmallVector<TypeRepr *, 4> args;
835-
generate(*Clause, Loc, lAngleLoc, rAngleLoc, args);
836-
if (!args.empty())
673+
auto Args = Clause->getArguments();
674+
if (!Args.empty()) {
675+
auto LAngleLoc = advanceLocBegin(Loc, Clause->getLeftAngleBracket());
676+
auto RAngleLoc = advanceLocBegin(Loc, Clause->getRightAngleBracket());
677+
SourceRange Range{LAngleLoc, RAngleLoc};
678+
auto ArgsAST = generate(Args, Loc);
837679
return GenericIdentTypeRepr::create(Context, IdentifierLoc, Identifier,
838-
args, {lAngleLoc, rAngleLoc});
680+
ArgsAST, Range);
681+
}
839682
}
840683
return new (Context) SimpleIdentTypeRepr(IdentifierLoc, Identifier);
841684
}
@@ -964,11 +807,13 @@ TypeRepr *ASTGen::generate(const SILBoxTypeSyntax &Type, const SourceLoc Loc,
964807
auto RBraceLoc = advanceLocBegin(Loc, Type.getRightBrace());
965808

966809
SourceLoc LAngleLoc, RAngleLoc;
967-
SmallVector<TypeRepr *, 4> Args;
810+
SmallVector<TypeRepr*, 4> Args;
968811
if (auto genericArgs = Type.getGenericArgumentClause()) {
969812
if (genericArgs->getRightAngleBracket().isMissing())
970813
return nullptr;
971-
generate(*genericArgs, Loc, LAngleLoc, RAngleLoc, Args);
814+
LAngleLoc = advanceLocBegin(Loc, genericArgs->getLeftAngleBracket());
815+
RAngleLoc = advanceLocBegin(Loc, genericArgs->getRightAngleBracket());
816+
Args = generate(genericArgs->getArguments(), Loc);
972817
}
973818

974819
auto SILType = SILBoxTypeRepr::create(Context, generics, LBraceLoc, Fields,
@@ -1084,20 +929,22 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
1084929
return nullptr;
1085930
}
1086931

1087-
void
1088-
ASTGen::generate(const GenericArgumentClauseSyntax &clause, const SourceLoc Loc,
1089-
SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
1090-
SmallVectorImpl<TypeRepr *> &args) {
1091-
lAngleLoc = advanceLocBegin(Loc, clause.getLeftAngleBracket());
1092-
rAngleLoc = advanceLocBegin(Loc, clause.getRightAngleBracket());
1093-
1094-
assert(args.empty());
1095-
for (auto Arg : clause.getArguments()) {
1096-
auto tyR = generate(Arg.getArgumentType(), Loc);
932+
SmallVector<TypeRepr *, 4>
933+
ASTGen::generate(const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
934+
SmallVector<TypeRepr *, 4> Types;
935+
for (auto Arg : Args) {
936+
auto tyR = generate(Arg, Loc);
1097937
if (!tyR)
1098938
tyR = new (Context) ErrorTypeRepr(advanceLocBegin(Loc, Arg));
1099-
args.push_back(tyR);
939+
Types.push_back(tyR);
1100940
}
941+
942+
return Types;
943+
}
944+
945+
TypeRepr *ASTGen::generate(const GenericArgumentSyntax &Arg,
946+
const SourceLoc Loc) {
947+
return generate(Arg.getArgumentType(), Loc);
1101948
}
1102949

1103950
StringRef ASTGen::copyAndStripUnderscores(StringRef Orig, ASTContext &Context) {

0 commit comments

Comments
 (0)