Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

WIP/SwiftExpressionParser: preserve default argument thunks #1744

Draft
wants to merge 1 commit into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,44 @@ void SwiftASTManipulator::FindNonVariableDeclarations(
}
}

void SwiftASTManipulator::EnumerateDefaultValuedArguments(llvm::SmallVectorImpl<std::pair<swift::FuncDecl *, std::vector<int>>> &FDAI) {
class DefaultArgumentThunkFinder : public swift::ASTWalker {
swift::SourceFile *SF;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use variable names more in keeping with lldb's conventions. Particularly here, you have FD DAI, AI, DAI... Playing the "guess the acronym" game makes the code much harder to read.

llvm::SmallVectorImpl<std::pair<swift::FuncDecl *, std::vector<int>>> &FDAI;

public:
explicit DefaultArgumentThunkFinder(llvm::SmallVectorImpl<std::pair<swift::FuncDecl *, std::vector<int>>> &FDAI) : SF(nullptr), FDAI(FDAI) {}

void scan(swift::SourceFile &SF) {
this->SF = &SF;
for (swift::Decl *D : SF.Decls)
D->walk(*this);
this->SF = nullptr;
}

private:
bool walkToDeclPre(swift::Decl *D) override {
if (auto *FD = llvm::dyn_cast<swift::FuncDecl>(D)) {
if (swift::ParameterList *PL = FD->getParameters()) {
std::vector<int> DAI;
for (unsigned AI = 0, AE = PL->size(); AI < AE; ++AI) {
if (PL->get(AI)->getDefaultArgumentKind() ==
swift::DefaultArgumentKind::None)
continue;
DAI.push_back(AI);
}
if (!DAI.empty())
FDAI.push_back(std::make_pair(FD, DAI));
}
}
return true;
}
};

DefaultArgumentThunkFinder DATF(FDAI);
DATF.scan(m_source_file);
}

void SwiftASTManipulator::InsertResult(
swift::VarDecl *result_var, swift::Type &result_type,
SwiftASTManipulator::ResultLocationInfo &result_info) {
Expand Down
3 changes: 3 additions & 0 deletions source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class SwiftASTManipulator : public SwiftASTManipulatorBase {
void FindNonVariableDeclarations(
llvm::SmallVectorImpl<swift::ValueDecl *> &non_variables);

void EnumerateDefaultValuedArguments(
llvm::SmallVectorImpl<std::pair<swift::FuncDecl *, std::vector<int>>> &DefaultValuedArgumentIndices);

bool FixCaptures();

swift::ValueDecl *MakeGlobalTypealias(swift::Identifier name,
Expand Down
14 changes: 14 additions & 0 deletions source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "swift/Frontend/Frontend.h"
#include "swift/Parse/LocalContext.h"
#include "swift/Parse/PersistentParserState.h"
#include "swift/SIL/SILDeclRef.h"
#include "swift/SIL/SILDebuggerClient.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILModule.h"
Expand Down Expand Up @@ -1661,9 +1662,22 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
variable_map[name] = *var_info;
}

llvm::SmallVector<std::pair<swift::FuncDecl *, std::vector<int>>, 4> FDAIs;
parsed_expr->code_manipulator->EnumerateDefaultValuedArguments(FDAIs);

std::unique_ptr<swift::SILModule> sil_module(swift::performSILGeneration(
parsed_expr->source_file, swift_ast_ctx->GetSILOptions()));

for (const auto &FDAI : FDAIs) {
swift::FuncDecl *FD = FDAI.first;
std::vector<int> DAI = FDAI.second;
for (int AI : DAI) {
auto DAG = swift::SILDeclRef::getDefaultArgGenerator(FD, AI);
swift::SILFunction *F = sil_module->lookUpFunction(DAG);
F->dump();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this has any effect (besides spewing garbage to stderr). What's actually going on here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with the spewing garbage bit at least - it was very useful to me when I was trying to determine if I was catching everything and the correct thing :-). Yes, this is absolutely debugging cruft that is not meant to be merged.

}
}

if (log) {
std::string s;
llvm::raw_string_ostream ss(s);
Expand Down