From 788853b555b589c1283664171732c3c624a0e5b9 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 26 Jun 2019 16:04:06 -0700 Subject: [PATCH] WIP/SwiftExpressionParser: preserve default argument thunks --- .../Swift/SwiftASTManipulator.cpp | 38 +++++++++++++++++++ .../Swift/SwiftASTManipulator.h | 3 ++ .../Swift/SwiftExpressionParser.cpp | 14 +++++++ 3 files changed, 55 insertions(+) diff --git a/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp b/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp index c453af0a3b75..5bb4b06053ea 100644 --- a/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp +++ b/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp @@ -788,6 +788,44 @@ void SwiftASTManipulator::FindNonVariableDeclarations( } } +void SwiftASTManipulator::EnumerateDefaultValuedArguments(llvm::SmallVectorImpl>> &FDAI) { + class DefaultArgumentThunkFinder : public swift::ASTWalker { + swift::SourceFile *SF; + llvm::SmallVectorImpl>> &FDAI; + + public: + explicit DefaultArgumentThunkFinder(llvm::SmallVectorImpl>> &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(D)) { + if (swift::ParameterList *PL = FD->getParameters()) { + std::vector 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) { diff --git a/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h b/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h index 4213550fb221..fd30f1c0fd3e 100644 --- a/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h +++ b/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h @@ -164,6 +164,9 @@ class SwiftASTManipulator : public SwiftASTManipulatorBase { void FindNonVariableDeclarations( llvm::SmallVectorImpl &non_variables); + void EnumerateDefaultValuedArguments( + llvm::SmallVectorImpl>> &DefaultValuedArgumentIndices); + bool FixCaptures(); swift::ValueDecl *MakeGlobalTypealias(swift::Identifier name, diff --git a/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp b/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp index 09db606ef243..9cce4391bbb8 100644 --- a/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp +++ b/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp @@ -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" @@ -1661,9 +1662,22 @@ unsigned SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager, variable_map[name] = *var_info; } + llvm::SmallVector>, 4> FDAIs; + parsed_expr->code_manipulator->EnumerateDefaultValuedArguments(FDAIs); + std::unique_ptr sil_module(swift::performSILGeneration( parsed_expr->source_file, swift_ast_ctx->GetSILOptions())); + for (const auto &FDAI : FDAIs) { + swift::FuncDecl *FD = FDAI.first; + std::vector DAI = FDAI.second; + for (int AI : DAI) { + auto DAG = swift::SILDeclRef::getDefaultArgGenerator(FD, AI); + swift::SILFunction *F = sil_module->lookUpFunction(DAG); + F->dump(); + } + } + if (log) { std::string s; llvm::raw_string_ostream ss(s);