1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
//===-- SwiftASTManipulator.h -----------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef liblldb_SwiftASTManipulator_h
#define liblldb_SwiftASTManipulator_h
#include "lldb/Expression/Expression.h"
#include "lldb/Symbol/CompilerType.h"
#include "lldb/Utility/Status.h"
#include "lldb/Utility/Stream.h"
#include "swift/AST/Decl.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include <optional>
namespace swift {
class CaseStmt;
class DoCatchStmt;
class ExtensionDecl;
class FuncDecl;
class DoStmt;
class ReturnStmt;
class SourceFile;
class VarDecl;
} // namespace swift
namespace lldb_private {
class SwiftASTContextForExpressions;
class SwiftASTManipulatorBase {
public:
class VariableMetadata {
public:
VariableMetadata() = default;
virtual ~VariableMetadata() = default;
virtual unsigned GetType() const = 0;
};
class VariableMetadataResult
: public SwiftASTManipulatorBase::VariableMetadata {
public:
virtual ~VariableMetadataResult();
constexpr static unsigned Type() { return 'Resu'; }
unsigned GetType() const override { return Type(); }
static bool classof(const VariableMetadata *VM) {
return VM->GetType() == Type();
}
};
class VariableMetadataError
: public SwiftASTManipulatorBase::VariableMetadata {
public:
virtual ~VariableMetadataError();
constexpr static unsigned Type() { return 'Erro'; }
unsigned GetType() const override { return Type(); }
static bool classof(const VariableMetadata *VM) {
return VM->GetType() == Type();
}
};
class VariableMetadataPersistent
: public SwiftASTManipulatorBase::VariableMetadata {
public:
VariableMetadataPersistent(
lldb::ExpressionVariableSP &persistent_variable_sp)
: m_persistent_variable_sp(persistent_variable_sp) {}
static constexpr unsigned Type() { return 'Pers'; }
unsigned GetType() const override { return Type(); }
static bool classof(const VariableMetadata *VM) {
return VM->GetType() == Type();
}
lldb::ExpressionVariableSP m_persistent_variable_sp;
};
class VariableMetadataVariable
: public SwiftASTManipulatorBase::VariableMetadata {
public:
VariableMetadataVariable(lldb::VariableSP &variable_sp)
: m_variable_sp(variable_sp) {}
static constexpr unsigned Type() { return 'Vari'; }
unsigned GetType() const override { return Type(); }
static bool classof(const VariableMetadata *VM) {
return VM->GetType() == Type();
}
lldb::VariableSP m_variable_sp;
};
typedef std::shared_ptr<VariableMetadata> VariableMetadataSP;
struct VariableInfo {
CompilerType GetType() const { return m_type; }
swift::Identifier GetName() const { return m_name; }
VariableMetadata *GetMetadata() const { return m_metadata.get(); }
void TakeMetadata(VariableMetadata *vmd) { m_metadata.reset(vmd); }
swift::VarDecl *GetDecl() const { return m_decl; }
swift::VarDecl::Introducer GetVarIntroducer() const;
bool IsCaptureList() const;
bool IsMetadataPointer() const { return m_name.str().startswith("$τ"); }
bool IsOutermostMetadataPointer() const {
return m_name.str().startswith("$τ_0_");
}
bool IsSelf() const {
return m_name.str().equals("$__lldb_injected_self");
}
bool IsPackCount() const {
return m_name.str().startswith("$pack_count_");
}
bool IsUnboundPack() const { return m_is_unbound_pack; }
VariableInfo() : m_lookup_error(llvm::Error::success()) {}
VariableInfo(CompilerType type, swift::Identifier name,
VariableMetadataSP metadata,
swift::VarDecl::Introducer introducer,
bool is_capture_list = false, bool is_unbound_pack = false)
: m_type(type), m_name(name), m_metadata(metadata),
m_var_introducer(introducer), m_lookup_error(llvm::Error::success()),
m_is_capture_list(is_capture_list),
m_is_unbound_pack(is_unbound_pack) {}
VariableInfo(CompilerType type, swift::Identifier name,
swift::VarDecl *decl)
: m_type(type), m_name(name), m_decl(decl),
m_lookup_error(llvm::Error::success()) {}
void Print(Stream &stream) const;
void SetType(CompilerType new_type) { m_type = new_type; }
void SetLookupError(llvm::Error &&error) {
m_lookup_error = std::move(error);
}
llvm::Error TakeLookupError() { return m_lookup_error.ToError(); }
friend class SwiftASTManipulator;
protected:
CompilerType m_type;
swift::Identifier m_name;
VariableMetadataSP m_metadata;
swift::VarDecl *m_decl = nullptr;
swift::VarDecl::Introducer m_var_introducer =
swift::VarDecl::Introducer::Var;
Status m_lookup_error;
bool m_is_capture_list = false;
bool m_is_unbound_pack = false;
};
SwiftASTManipulatorBase(swift::SourceFile &source_file,
const SymbolContext &sc, bool repl,
lldb::BindGenericTypes bind_generic_types)
: m_source_file(source_file), m_sc(sc), m_repl(repl),
m_bind_generic_types(bind_generic_types) {
DoInitialization();
}
llvm::MutableArrayRef<VariableInfo> GetVariableInfo() { return m_variables; }
bool IsValid() {
return m_repl || (m_function_decl &&
(m_entrypoint_decl || (!m_extension_decl)) && m_do_stmt);
}
swift::BraceStmt *GetUserBody();
private:
void DoInitialization();
protected:
swift::SourceFile &m_source_file;
llvm::SmallVector<VariableInfo, 1> m_variables;
const SymbolContext &m_sc;
bool m_repl = false;
lldb::BindGenericTypes m_bind_generic_types = lldb::eBindAuto;
/// The function containing the expression's code.
swift::FuncDecl *m_function_decl = nullptr;
/// The entrypoint function. Null if evaluating an expression outside a
/// method, $__lldb_expr otherswise.
swift::FuncDecl *m_entrypoint_decl = nullptr;
/// If evaluating in a generic context, the trampoline function that calls the
/// method with the user's expression, null otherwise.
swift::FuncDecl *m_trampoline_decl = nullptr;
/// If evaluating in a generic context, the sink function the entrypoint calls
/// in the AST, null otherwise.
swift::FuncDecl *m_sink_decl = nullptr;
/// The extension m_function_decl lives in, if it's a method.
swift::ExtensionDecl *m_extension_decl = nullptr;
/// The do{}catch(){} statement whose body is the main body.
swift::DoCatchStmt *m_do_stmt = nullptr;
/// The body of the catch - we patch the assignment there to capture
/// any error thrown.
swift::CaseStmt *m_catch_stmt = nullptr;
};
class SwiftASTManipulator : public SwiftASTManipulatorBase {
public:
SwiftASTManipulator(SwiftASTContextForExpressions &swift_ast_ctx,
swift::SourceFile &source_file, const SymbolContext &sc,
bool repl, lldb::BindGenericTypes bind_generic_types);
SwiftASTContextForExpressions &GetScratchContext() { return m_swift_ast_ctx; }
void FindSpecialNames(llvm::SmallVectorImpl<swift::Identifier> &names,
llvm::StringRef prefix);
llvm::Expected<swift::VarDecl *>
AddExternalVariable(swift::Identifier name, CompilerType &type,
VariableMetadataSP &metadata_sp);
swift::FuncDecl *GetFunctionToInjectVariableInto(
const SwiftASTManipulator::VariableInfo &variable) const;
llvm::Expected<swift::VarDecl *> GetVarDeclForVariableInFunction(
const SwiftASTManipulator::VariableInfo &variable,
swift::FuncDecl *containing_function);
llvm::Expected<swift::Type> GetSwiftTypeForVariable(
const SwiftASTManipulator::VariableInfo &variable) const;
bool AddExternalVariables(llvm::MutableArrayRef<VariableInfo> variables);
bool RewriteResult();
void MakeDeclarationsPublic();
void
FindVariableDeclarations(llvm::SmallVectorImpl<size_t> &found_declarations,
bool repl);
void FindNonVariableDeclarations(
llvm::SmallVectorImpl<swift::ValueDecl *> &non_variables);
bool FixCaptures();
/// Makes a typealias binding name to type in the scope of the decl_ctx. If
/// decl_ctx is a nullptr this is a global typealias.
llvm::Expected<swift::TypeAliasDecl *>
MakeTypealias(swift::Identifier name, CompilerType &type,
bool make_private = true,
swift::DeclContext *decl_ctx = nullptr);
/// Returns true if $__lldb_result is a non copyable type.
/// Currently LLDB cannot deal with expressions whose result is a non copyable
/// type, this function exists so LLDB can diagnose this situation and provide
/// a proper error message.
bool IsExpressionResultNonCopyable();
llvm::Error FixupResultAfterTypeChecking();
static const char *GetArgumentName() { return "$__lldb_arg"; }
static const char *GetResultName() { return "$__lldb_result"; }
static const char *GetErrorName() { return "$__lldb_error_result"; }
static bool
SaveExpressionTextToTempFile(llvm::StringRef text,
const EvaluateExpressionOptions &options,
std::string &expr_source_path);
swift::FuncDecl *GetEntrypointDecl() const {
return m_entrypoint_decl;
}
swift::FuncDecl *GetFuncDecl() const {
return m_function_decl;
}
swift::FuncDecl *GetTrampolineDecl() const {
return m_trampoline_decl;
}
swift::FuncDecl *GetSinkDecl() const {
return m_sink_decl;
}
private:
uint32_t m_tmpname_idx = 0;
typedef llvm::SmallVectorImpl<swift::ASTNode> Body;
swift::Stmt *ConvertExpressionToTmpReturnVarAccess(
swift::Expr *expr, const swift::SourceLoc &source_loc, bool in_return,
swift::DeclContext *decl_context);
struct ResultLocationInfo {
/// This points to the first stage tmp result decl.
swift::VarDecl *tmp_var_decl = nullptr;
/// This is the DoStmt statement that we make up.
swift::DoStmt *wrapper_stmt = nullptr;
/// This is the expression returned by this block.
swift::PatternBindingDecl *binding_decl = nullptr;
/// This is the original expression that we resolved to this type.
swift::Expr *orig_expr = nullptr;
/// If this block does a return, this is the return statement.
swift::ReturnStmt *return_stmt = nullptr;
/// This is the source location of this return in the overall
/// expression.
const swift::SourceLoc source_loc;
ResultLocationInfo(const swift::SourceLoc &in_source_loc)
: source_loc(in_source_loc) {}
};
void InsertResult(swift::VarDecl *result_var, swift::Type &result_type,
ResultLocationInfo &result_info);
void InsertError(swift::VarDecl *error_var, swift::Type &error_type);
std::vector<ResultLocationInfo> m_result_info;
llvm::StringMap<swift::TypeBase *> m_type_aliases;
SwiftASTContextForExpressions &m_swift_ast_ctx;
};
}
#endif
|