Skip to content

Commit 597538c

Browse files
committed
[SyntaxParse] Parse IdentifierExpr syntax
Along with UnqualifiedDeclName (e.g. 'foo(x:y:)').
1 parent a4fcd26 commit 597538c

File tree

7 files changed

+386
-201
lines changed

7 files changed

+386
-201
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class ASTGen {
7474
//===--------------------------------------------------------------------===//
7575
// Expressions.
7676

77+
Expr *generate(const syntax::ExprSyntax &Expr, const SourceLoc Loc);
78+
Expr *generate(const syntax::IdentifierExprSyntax &Expr, const SourceLoc Loc);
79+
Expr *generate(const syntax::EditorPlaceholderExprSyntax &Expr,
80+
const SourceLoc Loc);
81+
Expr *generate(const syntax::SpecializeExprSyntax &Expr, const SourceLoc Loc);
7782
Expr *generate(const syntax::IntegerLiteralExprSyntax &Expr,
7883
const SourceLoc Loc);
7984
Expr *generate(const syntax::FloatLiteralExprSyntax &Expr,
@@ -91,7 +96,13 @@ class ASTGen {
9196
const SourceLoc Loc);
9297
Expr *generate(const syntax::UnknownExprSyntax &Expr, const SourceLoc Loc);
9398

99+
std::pair<DeclName, DeclNameLoc> generateUnqualifiedDeclName(
100+
const syntax::TokenSyntax &idTok,
101+
const Optional<syntax::DeclNameArgumentsSyntax> &args,
102+
const SourceLoc Loc);
103+
94104
private:
105+
95106
Expr *generateMagicIdentifierLiteralExpression(
96107
const syntax::TokenSyntax &PoundToken, const SourceLoc Loc);
97108

@@ -163,10 +174,9 @@ class ASTGen {
163174
//===--------------------------------------------------------------------===//
164175
// Generics.
165176

166-
TypeRepr *generate(const syntax::GenericArgumentSyntax &Arg,
167-
const SourceLoc Loc);
168-
llvm::SmallVector<TypeRepr *, 4>
169-
generate(const syntax::GenericArgumentListSyntax &Args, const SourceLoc Loc);
177+
void generate(const syntax::GenericArgumentClauseSyntax &Arg,
178+
const SourceLoc Loc, SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
179+
SmallVectorImpl<TypeRepr *> &args);
170180

171181
GenericParamList *
172182
generate(const syntax::GenericParameterClauseListSyntax &clause,

include/swift/Parse/Parser.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,8 @@ class Parser {
14761476
/// _)
14771477
/// \param loc The location of the label (empty if it doesn't exist)
14781478
void parseOptionalArgumentLabel(Identifier &name, SourceLoc &loc);
1479+
bool parseOptionalArgumentLabelSyntax(Optional<ParsedTokenSyntax> &name,
1480+
Optional<ParsedTokenSyntax> &colon);
14791481

14801482
/// Parse an unqualified-decl-name.
14811483
///
@@ -1494,10 +1496,20 @@ class Parser {
14941496
bool allowOperators=false,
14951497
bool allowZeroArgCompoundNames=false,
14961498
bool allowDeinitAndSubscript=false);
1499+
ParserStatus
1500+
parseUnqualifiedDeclNameSyntax(Optional<ParsedTokenSyntax> &identTok,
1501+
Optional<ParsedDeclNameArgumentsSyntax> &declNameArg,
1502+
bool afterDot, const Diagnostic &diag,
1503+
bool allowOperators=false,
1504+
bool allowZeroArgCompoundNames=false,
1505+
bool allowDeinitAndSubscript=false);
1506+
1507+
ParsedSyntaxResult<ParsedExprSyntax> parseExprIdentifierSyntax();
1508+
ParsedSyntaxResult<ParsedExprSyntax>
1509+
parseExprSpecializeSyntax(ParsedExprSyntax &&);
14971510

14981511
Expr *parseExprIdentifier();
1499-
Expr *parseExprEditorPlaceholder(Token PlaceholderTok,
1500-
Identifier PlaceholderId);
1512+
Expr *parseExprEditorPlaceholder(SourceLoc loc, StringRef text);
15011513

15021514
/// Parse a closure expression after the opening brace.
15031515
///

lib/Parse/ASTGen.cpp

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

155+
Expr *ASTGen::generate(const ExprSyntax &E, const SourceLoc Loc) {
156+
Expr *result = nullptr;
157+
158+
if (auto identifierExpr = E.getAs<IdentifierExprSyntax>())
159+
result = generate(*identifierExpr, Loc);
160+
else if (auto specializeExpr = E.getAs<SpecializeExprSyntax>())
161+
result = generate(*specializeExpr, Loc);
162+
else if (auto editorPlaceHolderExpr = E.getAs<EditorPlaceholderExprSyntax>())
163+
result = generate(*editorPlaceHolderExpr, Loc);
164+
else if (auto integerLiteralExpr = E.getAs<IntegerLiteralExprSyntax>())
165+
result = generate(*integerLiteralExpr, Loc);
166+
else if (auto floatLiteralExpr = E.getAs<FloatLiteralExprSyntax>())
167+
result = generate(*floatLiteralExpr, Loc);
168+
else if (auto nilLiteral = E.getAs<NilLiteralExprSyntax>())
169+
result = generate(*nilLiteral, Loc);
170+
else if (auto boolLiteral = E.getAs<BooleanLiteralExprSyntax>())
171+
result = generate(*boolLiteral, Loc);
172+
else if (auto poundFileExpr = E.getAs<PoundFileExprSyntax>())
173+
result = generate(*poundFileExpr, Loc);
174+
else if (auto poundLineExpr = E.getAs<PoundLineExprSyntax>())
175+
result = generate(*poundLineExpr, Loc);
176+
else if (auto poundColumnExpr = E.getAs<PoundColumnExprSyntax>())
177+
result = generate(*poundColumnExpr, Loc);
178+
else if (auto poundFunctionExpr = E.getAs<PoundFunctionExprSyntax>())
179+
result = generate(*poundFunctionExpr, Loc);
180+
else if (auto poundDsohandleExpr = E.getAs<PoundDsohandleExprSyntax>())
181+
result = generate(*poundDsohandleExpr, Loc);
182+
else
183+
llvm_unreachable("unsupported expression");
184+
185+
return result;
186+
}
187+
188+
std::pair<DeclName, DeclNameLoc> ASTGen::generateUnqualifiedDeclName(
189+
const TokenSyntax &idTok, const Optional<DeclNameArgumentsSyntax> &args,
190+
const SourceLoc Loc) {
191+
SourceLoc baseNameLoc = advanceLocBegin(Loc, idTok);
192+
193+
DeclBaseName baseName;
194+
if (idTok.getTokenKind() == tok::kw_init)
195+
baseName = DeclBaseName::createConstructor();
196+
else if (idTok.getTokenKind() == tok::kw_deinit)
197+
baseName = DeclBaseName::createDestructor();
198+
else if (idTok.getTokenKind() == tok::kw_subscript)
199+
baseName = DeclBaseName::createSubscript();
200+
else
201+
baseName = Context.getIdentifier(idTok.getIdentifierText());
202+
203+
if (!args)
204+
return {DeclName(baseName), DeclNameLoc(baseNameLoc)};
205+
206+
// FIXME: Remove this block and use 'Loc'.
207+
// This is needed for the case 'idTok' and 'args' are not in the same tree.
208+
// i.e. Call from parseUnqualifiedDeclName().
209+
SourceLoc argsLeadingLoc = Loc;
210+
if (!args->getParent()) {
211+
argsLeadingLoc = Loc.getAdvancedLoc(idTok.getTextLength());
212+
} else {
213+
assert(idTok.getData().getParent() == args->getData().getParent() &&
214+
idTok.getIndexInParent() + 1 == args->getIndexInParent() &&
215+
"'idTok' must be immediately followed by 'args'");
216+
}
217+
218+
SmallVector<Identifier, 2> argumentLabels;
219+
SmallVector<SourceLoc, 2> argumentLabelLocs;
220+
for (auto arg : args->getArguments()) {
221+
Identifier label;
222+
if (!arg.getName().isMissing() &&
223+
arg.getName().getTokenKind() != tok::kw__) {
224+
label = Context.getIdentifier(arg.getName().getIdentifierText());
225+
}
226+
argumentLabels.push_back(label);
227+
argumentLabelLocs.push_back(advanceLocBegin(argsLeadingLoc,
228+
*arg.getFirstToken()));
229+
}
230+
SourceLoc lParenLoc = advanceLocBegin(argsLeadingLoc, args->getLeftParen());
231+
SourceLoc rParenLoc = advanceLocBegin(argsLeadingLoc, args->getRightParen());
232+
233+
DeclName name(Context, baseName, argumentLabels);
234+
DeclNameLoc nameLoc;
235+
if (argumentLabelLocs.empty())
236+
nameLoc = DeclNameLoc(baseNameLoc);
237+
else
238+
nameLoc = DeclNameLoc(Context, baseNameLoc, lParenLoc, argumentLabelLocs,
239+
rParenLoc);
240+
return {name, nameLoc};
241+
}
242+
243+
Expr *ASTGen::generate(const IdentifierExprSyntax &E, const SourceLoc Loc) {
244+
auto idTok = E.getIdentifier();
245+
DeclName name;
246+
DeclNameLoc nameLoc;
247+
std::tie(name, nameLoc) = generateUnqualifiedDeclName(
248+
E.getIdentifier(), E.getDeclNameArguments(), Loc);
249+
250+
ValueDecl *D = nullptr;
251+
if (!P.InPoundIfEnvironment) {
252+
D = lookupInScope(name);
253+
// FIXME: We want this to work: "var x = { x() }", but for now it's better
254+
// to disallow it than to crash.
255+
if (D) {
256+
for (auto activeVar : P.DisabledVars) {
257+
if (activeVar != D)
258+
continue;
259+
P.diagnose(nameLoc.getBaseNameLoc(), P.DisabledVarReason);
260+
return new (Context) ErrorExpr(nameLoc.getSourceRange());
261+
}
262+
} else {
263+
for (auto activeVar : P.DisabledVars) {
264+
if (activeVar->getFullName() != name)
265+
continue;
266+
P.diagnose(nameLoc.getBaseNameLoc(), P.DisabledVarReason);
267+
return new (Context) ErrorExpr(nameLoc.getSourceRange());
268+
}
269+
}
270+
}
271+
272+
if (!D) {
273+
return new (Context)
274+
UnresolvedDeclRefExpr(name, DeclRefKind::Ordinary, nameLoc);
275+
}
276+
277+
if (auto TD = dyn_cast<TypeDecl>(D)) {
278+
// When parsing default argument expressions for generic functions,
279+
// we haven't built a FuncDecl or re-parented the GenericTypeParamDecls
280+
// to the FuncDecl yet. Other than that, we should only ever find
281+
// global or local declarations here.
282+
assert(!TD->getDeclContext()->isTypeContext() ||
283+
isa<GenericTypeParamDecl>(TD));
284+
return TypeExpr::createForDecl(nameLoc.getBaseNameLoc(), TD,
285+
/*DeclContext=*/nullptr,
286+
/*inplicit=*/false);
287+
}
288+
289+
return new (Context) DeclRefExpr(D, nameLoc, /*implicit=*/false);
290+
}
291+
292+
Expr *ASTGen::generate(const EditorPlaceholderExprSyntax &E, const SourceLoc Loc) {
293+
assert(!E.getIdentifier().isMissing());
294+
295+
auto text = E.getIdentifier().getText();
296+
auto tokLoc = advanceLocBegin(Loc, E.getIdentifier());
297+
return P.parseExprEditorPlaceholder(tokLoc, text);
298+
}
299+
300+
Expr *ASTGen::generate(const SpecializeExprSyntax &E, const SourceLoc Loc) {
301+
auto base = generate(E.getExpression(), Loc);
302+
303+
SourceLoc lAngleLoc, rAngleLoc;
304+
SmallVector<TypeRepr *, 4> argTyRs;
305+
generate(E.getGenericArgumentClause(), Loc, lAngleLoc, rAngleLoc, argTyRs);
306+
if (argTyRs.empty())
307+
return base;
308+
309+
SmallVector<TypeLoc, 4> args;
310+
args.assign(argTyRs.begin(), argTyRs.end());
311+
return UnresolvedSpecializeExpr::create(Context, base, lAngleLoc, args,
312+
rAngleLoc);
313+
}
314+
155315
Expr *ASTGen::generate(const IntegerLiteralExprSyntax &Expr,
156316
const SourceLoc Loc) {
157317
auto Digits = Expr.getDigits();
@@ -584,15 +744,12 @@ ComponentIdentTypeRepr *ASTGen::generateIdentifier(const T &Type,
584744
auto IdentifierLoc = advanceLocBegin(Loc, Type.getName());
585745
auto Identifier = Context.getIdentifier(Type.getName().getIdentifierText());
586746
if (auto Clause = Type.getGenericArgumentClause()) {
587-
auto Args = Clause->getArguments();
588-
if (!Args.empty()) {
589-
auto LAngleLoc = advanceLocBegin(Loc, Clause->getLeftAngleBracket());
590-
auto RAngleLoc = advanceLocBegin(Loc, Clause->getRightAngleBracket());
591-
SourceRange Range{LAngleLoc, RAngleLoc};
592-
auto ArgsAST = generate(Args, Loc);
747+
SourceLoc lAngleLoc, rAngleLoc;
748+
SmallVector<TypeRepr *, 4> args;
749+
generate(*Clause, Loc, lAngleLoc, rAngleLoc, args);
750+
if (!args.empty())
593751
return GenericIdentTypeRepr::create(Context, IdentifierLoc, Identifier,
594-
ArgsAST, Range);
595-
}
752+
args, {lAngleLoc, rAngleLoc});
596753
}
597754
return new (Context) SimpleIdentTypeRepr(IdentifierLoc, Identifier);
598755
}
@@ -721,13 +878,11 @@ TypeRepr *ASTGen::generate(const SILBoxTypeSyntax &Type, const SourceLoc Loc,
721878
auto RBraceLoc = advanceLocBegin(Loc, Type.getRightBrace());
722879

723880
SourceLoc LAngleLoc, RAngleLoc;
724-
SmallVector<TypeRepr*, 4> Args;
881+
SmallVector<TypeRepr *, 4> Args;
725882
if (auto genericArgs = Type.getGenericArgumentClause()) {
726883
if (genericArgs->getRightAngleBracket().isMissing())
727884
return nullptr;
728-
LAngleLoc = advanceLocBegin(Loc, genericArgs->getLeftAngleBracket());
729-
RAngleLoc = advanceLocBegin(Loc, genericArgs->getRightAngleBracket());
730-
Args = generate(genericArgs->getArguments(), Loc);
885+
generate(*genericArgs, Loc, LAngleLoc, RAngleLoc, Args);
731886
}
732887

733888
auto SILType = SILBoxTypeRepr::create(Context, generics, LBraceLoc, Fields,
@@ -843,22 +998,20 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
843998
return nullptr;
844999
}
8451000

846-
SmallVector<TypeRepr *, 4>
847-
ASTGen::generate(const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
848-
SmallVector<TypeRepr *, 4> Types;
849-
for (auto Arg : Args) {
850-
auto tyR = generate(Arg, Loc);
1001+
void
1002+
ASTGen::generate(const GenericArgumentClauseSyntax &clause, const SourceLoc Loc,
1003+
SourceLoc &lAngleLoc, SourceLoc &rAngleLoc,
1004+
SmallVectorImpl<TypeRepr *> &args) {
1005+
lAngleLoc = advanceLocBegin(Loc, clause.getLeftAngleBracket());
1006+
rAngleLoc = advanceLocBegin(Loc, clause.getRightAngleBracket());
1007+
1008+
assert(args.empty());
1009+
for (auto Arg : clause.getArguments()) {
1010+
auto tyR = generate(Arg.getArgumentType(), Loc);
8511011
if (!tyR)
8521012
tyR = new (Context) ErrorTypeRepr(advanceLocBegin(Loc, Arg));
853-
Types.push_back(tyR);
1013+
args.push_back(tyR);
8541014
}
855-
856-
return Types;
857-
}
858-
859-
TypeRepr *ASTGen::generate(const GenericArgumentSyntax &Arg,
860-
const SourceLoc Loc) {
861-
return generate(Arg.getArgumentType(), Loc);
8621015
}
8631016

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

0 commit comments

Comments
 (0)