mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Merge remote-tracking branch 'origin/main' into rebranch
This commit is contained in:
@@ -361,6 +361,12 @@ class ModelASTWalker : public ASTWalker {
|
||||
friend class InactiveClauseRAII;
|
||||
bool inInactiveClause = false;
|
||||
|
||||
/// A mapping of argument expressions to their full argument info.
|
||||
llvm::DenseMap<Expr *, Argument> ArgumentInfo;
|
||||
|
||||
/// The number of ArgumentList parents in the walk.
|
||||
unsigned ArgumentListDepth = 0;
|
||||
|
||||
public:
|
||||
SyntaxModelWalker &Walker;
|
||||
ArrayRef<SyntaxNode> TokenNodes;
|
||||
@@ -378,6 +384,10 @@ public:
|
||||
|
||||
void visitSourceFile(SourceFile &SrcFile, ArrayRef<SyntaxNode> Tokens);
|
||||
|
||||
std::pair<bool, ArgumentList *>
|
||||
walkToArgumentListPre(ArgumentList *ArgList) override;
|
||||
ArgumentList *walkToArgumentListPost(ArgumentList *ArgList) override;
|
||||
|
||||
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;
|
||||
Expr *walkToExprPost(Expr *E) override;
|
||||
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override;
|
||||
@@ -422,7 +432,6 @@ private:
|
||||
bool pushStructureNode(const SyntaxStructureNode &Node,
|
||||
const ASTNodeType& ASTNode);
|
||||
bool popStructureNode();
|
||||
bool isCurrentCallArgExpr(const Expr *E);
|
||||
|
||||
bool processComment(CharSourceRange Range);
|
||||
bool searchForURL(CharSourceRange Range);
|
||||
@@ -484,36 +493,6 @@ CharSourceRange innerCharSourceRangeFromSourceRange(const SourceManager &SM,
|
||||
return CharSourceRange(SM, SRS, (SR.End != SR.Start) ? SR.End : SRS);
|
||||
}
|
||||
|
||||
CharSourceRange parameterNameRangeOfCallArg(const Expr *ArgListExpr,
|
||||
const Expr *Arg) {
|
||||
if (isa<ParenExpr>(ArgListExpr))
|
||||
return CharSourceRange();
|
||||
|
||||
auto *TE = cast<TupleExpr>(ArgListExpr);
|
||||
if (!TE->hasElementNameLocs() || !TE->hasElementNames())
|
||||
return CharSourceRange();
|
||||
|
||||
// Loop over the elements to find the index representing Arg.
|
||||
// This is somewhat inefficient but the only way to find the corresponding
|
||||
// name without the index, and the number of parameters in a call is normally
|
||||
// very low. If this becomes a performance problem, we could perhaps have
|
||||
// ASTWalker visit the element name as well.
|
||||
unsigned i = 0;
|
||||
for (auto E : TE->getElements()) {
|
||||
if (E == Arg) {
|
||||
SourceLoc NL = TE->getElementNameLoc(i);
|
||||
Identifier Name = TE->getElementName(i);
|
||||
if (NL.isValid() && !Name.empty())
|
||||
return CharSourceRange(NL, Name.getLength());
|
||||
|
||||
return CharSourceRange();
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
return CharSourceRange();
|
||||
}
|
||||
|
||||
static void setDecl(SyntaxStructureNode &N, Decl *D) {
|
||||
N.Dcl = D;
|
||||
N.Attrs = D->getAttrs();
|
||||
@@ -547,15 +526,41 @@ static bool shouldTreatAsSingleToken(const SyntaxStructureNode &Node,
|
||||
SM.getLineAndColumnInBuffer(Node.Range.getEnd()).first;
|
||||
}
|
||||
|
||||
std::pair<bool, ArgumentList *>
|
||||
ModelASTWalker::walkToArgumentListPre(ArgumentList *ArgList) {
|
||||
for (auto Arg : *ArgList) {
|
||||
auto res = ArgumentInfo.insert({Arg.getExpr(), Arg});
|
||||
assert(res.second && "Duplicate arguments?");
|
||||
(void)res;
|
||||
}
|
||||
ArgumentListDepth += 1;
|
||||
return {true, ArgList};
|
||||
}
|
||||
|
||||
ArgumentList *ModelASTWalker::walkToArgumentListPost(ArgumentList *ArgList) {
|
||||
// If there are no more argument lists above us, we can clear out the argument
|
||||
// mapping to save memory.
|
||||
ArgumentListDepth -= 1;
|
||||
if (ArgumentListDepth == 0)
|
||||
ArgumentInfo.clear();
|
||||
return ArgList;
|
||||
}
|
||||
|
||||
std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
|
||||
if (isVisitedBefore(E))
|
||||
return {false, E};
|
||||
|
||||
auto addCallArgExpr = [&](Expr *Elem, Expr *ArgListExpr) {
|
||||
if (isa<DefaultArgumentExpr>(Elem) || !isCurrentCallArgExpr(ArgListExpr))
|
||||
auto addCallArgExpr = [&](const Argument &Arg) {
|
||||
auto *Elem = Arg.getExpr();
|
||||
if (isa<DefaultArgumentExpr>(Elem))
|
||||
return;
|
||||
|
||||
CharSourceRange NR = parameterNameRangeOfCallArg(ArgListExpr, Elem);
|
||||
CharSourceRange NR;
|
||||
auto NL = Arg.getLabelLoc();
|
||||
auto Name = Arg.getLabel();
|
||||
if (NL.isValid() && !Name.empty())
|
||||
NR = CharSourceRange(NL, Name.getLength());
|
||||
|
||||
SyntaxStructureNode SN;
|
||||
SN.Kind = SyntaxStructureKind::Argument;
|
||||
SN.NameRange = NR;
|
||||
@@ -571,10 +576,9 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
|
||||
pushStructureNode(SN, Elem);
|
||||
};
|
||||
|
||||
if (auto *ParentExpr = Parent.getAsExpr()) {
|
||||
if (isa<TupleExpr>(ParentExpr) || isa<ParenExpr>(ParentExpr))
|
||||
addCallArgExpr(E, ParentExpr);
|
||||
}
|
||||
auto Arg = ArgumentInfo.find(E);
|
||||
if (Arg != ArgumentInfo.end())
|
||||
addCallArgExpr(Arg->second);
|
||||
|
||||
if (E->isImplicit())
|
||||
return { true, E };
|
||||
@@ -596,9 +600,9 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
|
||||
if (CE->getFn() && CE->getFn()->getSourceRange().isValid())
|
||||
SN.NameRange = charSourceRangeFromSourceRange(SM,
|
||||
CE->getFn()->getSourceRange());
|
||||
if (CE->getArg() && CE->getArg()->getSourceRange().isValid())
|
||||
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM,
|
||||
CE->getArg()->getSourceRange());
|
||||
if (CE->getArgs()->getSourceRange().isValid())
|
||||
SN.BodyRange = innerCharSourceRangeFromSourceRange(
|
||||
SM, CE->getArgs()->getSourceRange());
|
||||
pushStructureNode(SN, CE);
|
||||
|
||||
} else if (auto *ObjectE = dyn_cast<ObjectLiteralExpr>(E)) {
|
||||
@@ -640,20 +644,15 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
|
||||
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM, E->getSourceRange());
|
||||
pushStructureNode(SN, E);
|
||||
} else if (auto *Tup = dyn_cast<TupleExpr>(E)) {
|
||||
auto *ParentE = Parent.getAsExpr();
|
||||
if (!isCurrentCallArgExpr(Tup) && (!ParentE || !isa<InterpolatedStringLiteralExpr>(ParentE))) {
|
||||
SyntaxStructureNode SN;
|
||||
SN.Kind = SyntaxStructureKind::TupleExpression;
|
||||
SN.Range = charSourceRangeFromSourceRange(SM, Tup->getSourceRange());
|
||||
SN.BodyRange = innerCharSourceRangeFromSourceRange(SM,
|
||||
Tup->getSourceRange());
|
||||
|
||||
for (auto *Elem : Tup->getElements()) {
|
||||
addExprElem(Elem, SN);
|
||||
}
|
||||
|
||||
pushStructureNode(SN, Tup);
|
||||
SyntaxStructureNode SN;
|
||||
SN.Kind = SyntaxStructureKind::TupleExpression;
|
||||
SN.Range = charSourceRangeFromSourceRange(SM, Tup->getSourceRange());
|
||||
SN.BodyRange =
|
||||
innerCharSourceRangeFromSourceRange(SM, Tup->getSourceRange());
|
||||
for (auto *Elem : Tup->getElements()) {
|
||||
addExprElem(Elem, SN);
|
||||
}
|
||||
pushStructureNode(SN, Tup);
|
||||
} else if (auto *Closure = dyn_cast<ClosureExpr>(E)) {
|
||||
SyntaxStructureNode SN;
|
||||
SN.Kind = SyntaxStructureKind::ClosureExpression;
|
||||
@@ -684,8 +683,8 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
|
||||
llvm::SaveAndRestore<ASTWalker::ParentTy> SetParent(Parent, E);
|
||||
ISL->forEachSegment(Ctx, [&](bool isInterpolation, CallExpr *CE) {
|
||||
if (isInterpolation) {
|
||||
if (auto *Arg = CE->getArg())
|
||||
Arg->walk(*this);
|
||||
for (auto arg : *CE->getArgs())
|
||||
arg.getExpr()->walk(*this);
|
||||
}
|
||||
});
|
||||
return { false, walkToExprPost(E) };
|
||||
@@ -1186,8 +1185,8 @@ bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
|
||||
if (!Repr->walk(*this))
|
||||
return false;
|
||||
}
|
||||
if (auto *Arg = CA->getArg()) {
|
||||
if (!Arg->walk(*this))
|
||||
if (auto *Args = CA->getArgs()) {
|
||||
if (!Args->walk(*this))
|
||||
return false;
|
||||
}
|
||||
} else if (!TokenNodes.empty()) {
|
||||
@@ -1434,20 +1433,6 @@ bool ModelASTWalker::popStructureNode() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ModelASTWalker::isCurrentCallArgExpr(const Expr *E) {
|
||||
if (SubStructureStack.empty())
|
||||
return false;
|
||||
auto Current = SubStructureStack.back();
|
||||
|
||||
if (Current.StructureNode.Kind ==
|
||||
SyntaxStructureKind::ObjectLiteralExpression &&
|
||||
cast<ObjectLiteralExpr>(Current.ASTNode.getAsExpr())->getArg() == E)
|
||||
return true;
|
||||
|
||||
return Current.StructureNode.Kind == SyntaxStructureKind::CallExpression &&
|
||||
cast<CallExpr>(Current.ASTNode.getAsExpr())->getArg() == E;
|
||||
}
|
||||
|
||||
bool ModelASTWalker::processComment(CharSourceRange Range) {
|
||||
StringRef Text = SM.extractText(Range, BufferID);
|
||||
SourceLoc Loc = Range.getStart();
|
||||
|
||||
Reference in New Issue
Block a user