10b57cec5SDimitry Andric //===-- ClangExpressionDeclMap.h --------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONDECLMAP_H 105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONDECLMAP_H 110b57cec5SDimitry Andric 12fe6060f1SDimitry Andric #include <csignal> 13fe6060f1SDimitry Andric #include <cstdint> 140b57cec5SDimitry Andric 15bdd1243dSDimitry Andric #include <memory> 160b57cec5SDimitry Andric #include <vector> 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #include "ClangASTSource.h" 190b57cec5SDimitry Andric #include "ClangExpressionVariable.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric #include "lldb/Core/Value.h" 220b57cec5SDimitry Andric #include "lldb/Expression/Materializer.h" 230b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h" 240b57cec5SDimitry Andric #include "lldb/Symbol/TaggedASTType.h" 250b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h" 260b57cec5SDimitry Andric #include "lldb/lldb-public.h" 270b57cec5SDimitry Andric #include "clang/AST/Decl.h" 280b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric namespace lldb_private { 310b57cec5SDimitry Andric 325ffd83dbSDimitry Andric class ClangPersistentVariables; 335ffd83dbSDimitry Andric 340b57cec5SDimitry Andric /// \class ClangExpressionDeclMap ClangExpressionDeclMap.h 350b57cec5SDimitry Andric /// "lldb/Expression/ClangExpressionDeclMap.h" Manages named entities that are 360b57cec5SDimitry Andric /// defined in LLDB's debug information. 370b57cec5SDimitry Andric /// 380b57cec5SDimitry Andric /// The Clang parser uses the ClangASTSource as an interface to request named 390b57cec5SDimitry Andric /// entities from outside an expression. The ClangASTSource reports back, 400b57cec5SDimitry Andric /// listing all possible objects corresponding to a particular name. But it 410b57cec5SDimitry Andric /// in turn relies on ClangExpressionDeclMap, which performs several important 420b57cec5SDimitry Andric /// functions. 430b57cec5SDimitry Andric /// 440b57cec5SDimitry Andric /// First, it records what variables and functions were looked up and what 450b57cec5SDimitry Andric /// Decls were returned for them. 460b57cec5SDimitry Andric /// 470b57cec5SDimitry Andric /// Second, it constructs a struct on behalf of IRForTarget, recording which 480b57cec5SDimitry Andric /// variables should be placed where and relaying this information back so 490b57cec5SDimitry Andric /// that IRForTarget can generate context-independent code. 500b57cec5SDimitry Andric /// 510b57cec5SDimitry Andric /// Third, it "materializes" this struct on behalf of the expression command, 520b57cec5SDimitry Andric /// finding the current values of each variable and placing them into the 530b57cec5SDimitry Andric /// struct so that it can be passed to the JITted version of the IR. 540b57cec5SDimitry Andric /// 550b57cec5SDimitry Andric /// Fourth and finally, it "dematerializes" the struct after the JITted code 56*5f757f3fSDimitry Andric /// has executed, placing the new values back where it found the old ones. 570b57cec5SDimitry Andric class ClangExpressionDeclMap : public ClangASTSource { 580b57cec5SDimitry Andric public: 590b57cec5SDimitry Andric /// Constructor 600b57cec5SDimitry Andric /// 610b57cec5SDimitry Andric /// Initializes class variables. 620b57cec5SDimitry Andric /// 630b57cec5SDimitry Andric /// \param[in] keep_result_in_memory 640b57cec5SDimitry Andric /// If true, inhibits the normal deallocation of the memory for 650b57cec5SDimitry Andric /// the result persistent variable, and instead marks the variable 660b57cec5SDimitry Andric /// as persisting. 670b57cec5SDimitry Andric /// 68480093f4SDimitry Andric /// \param[in] result_delegate 690b57cec5SDimitry Andric /// If non-NULL, use this delegate to report result values. This 700b57cec5SDimitry Andric /// allows the client ClangUserExpression to report a result. 710b57cec5SDimitry Andric /// 72480093f4SDimitry Andric /// \param[in] target 73480093f4SDimitry Andric /// The target to use when parsing. 74480093f4SDimitry Andric /// 75480093f4SDimitry Andric /// \param[in] importer 76480093f4SDimitry Andric /// The ClangASTImporter to use when parsing. 770b57cec5SDimitry Andric /// 780b57cec5SDimitry Andric /// \param[in] ctx_obj 790b57cec5SDimitry Andric /// If not empty, then expression is evaluated in context of this object. 800b57cec5SDimitry Andric /// See the comment to `UserExpression::Evaluate` for details. 810b57cec5SDimitry Andric ClangExpressionDeclMap( 820b57cec5SDimitry Andric bool keep_result_in_memory, 830b57cec5SDimitry Andric Materializer::PersistentVariableDelegate *result_delegate, 845ffd83dbSDimitry Andric const lldb::TargetSP &target, 855ffd83dbSDimitry Andric const std::shared_ptr<ClangASTImporter> &importer, ValueObject *ctx_obj); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric /// Destructor 880b57cec5SDimitry Andric ~ClangExpressionDeclMap() override; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric /// Enable the state needed for parsing and IR transformation. 910b57cec5SDimitry Andric /// 920b57cec5SDimitry Andric /// \param[in] exe_ctx 930b57cec5SDimitry Andric /// The execution context to use when finding types for variables. 940b57cec5SDimitry Andric /// Also used to find a "scratch" AST context to store result types. 950b57cec5SDimitry Andric /// 960b57cec5SDimitry Andric /// \param[in] materializer 970b57cec5SDimitry Andric /// If non-NULL, the materializer to populate with information about 980b57cec5SDimitry Andric /// the variables to use 990b57cec5SDimitry Andric /// 1000b57cec5SDimitry Andric /// \return 1010b57cec5SDimitry Andric /// True if parsing is possible; false if it is unsafe to continue. 1020b57cec5SDimitry Andric bool WillParse(ExecutionContext &exe_ctx, Materializer *materializer); 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric void InstallCodeGenerator(clang::ASTConsumer *code_gen); 1050b57cec5SDimitry Andric 106e8d8bef9SDimitry Andric void InstallDiagnosticManager(DiagnosticManager &diag_manager); 107e8d8bef9SDimitry Andric 1080b57cec5SDimitry Andric /// Disable the state needed for parsing and IR transformation. 1090b57cec5SDimitry Andric void DidParse(); 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric /// [Used by IRForTarget] Add a variable to the list of persistent 1120b57cec5SDimitry Andric /// variables for the process. 1130b57cec5SDimitry Andric /// 1140b57cec5SDimitry Andric /// \param[in] decl 1150b57cec5SDimitry Andric /// The Clang declaration for the persistent variable, used for 1160b57cec5SDimitry Andric /// lookup during parsing. 1170b57cec5SDimitry Andric /// 1180b57cec5SDimitry Andric /// \param[in] name 1190b57cec5SDimitry Andric /// The name of the persistent variable, usually $something. 1200b57cec5SDimitry Andric /// 1210b57cec5SDimitry Andric /// \param[in] type 1220b57cec5SDimitry Andric /// The type of the variable, in the Clang parser's context. 1230b57cec5SDimitry Andric /// 1240b57cec5SDimitry Andric /// \return 1250b57cec5SDimitry Andric /// True on success; false otherwise. 1260b57cec5SDimitry Andric bool AddPersistentVariable(const clang::NamedDecl *decl, 1270b57cec5SDimitry Andric ConstString name, TypeFromParser type, 1280b57cec5SDimitry Andric bool is_result, bool is_lvalue); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric /// [Used by IRForTarget] Add a variable to the struct that needs to 1310b57cec5SDimitry Andric /// be materialized each time the expression runs. 1320b57cec5SDimitry Andric /// 1330b57cec5SDimitry Andric /// \param[in] decl 1340b57cec5SDimitry Andric /// The Clang declaration for the variable. 1350b57cec5SDimitry Andric /// 1360b57cec5SDimitry Andric /// \param[in] name 1370b57cec5SDimitry Andric /// The name of the variable. 1380b57cec5SDimitry Andric /// 1390b57cec5SDimitry Andric /// \param[in] value 1400b57cec5SDimitry Andric /// The LLVM IR value for this variable. 1410b57cec5SDimitry Andric /// 1420b57cec5SDimitry Andric /// \param[in] size 1430b57cec5SDimitry Andric /// The size of the variable in bytes. 1440b57cec5SDimitry Andric /// 1450b57cec5SDimitry Andric /// \param[in] alignment 1460b57cec5SDimitry Andric /// The required alignment of the variable in bytes. 1470b57cec5SDimitry Andric /// 1480b57cec5SDimitry Andric /// \return 1490b57cec5SDimitry Andric /// True on success; false otherwise. 1500b57cec5SDimitry Andric bool AddValueToStruct(const clang::NamedDecl *decl, ConstString name, 1510b57cec5SDimitry Andric llvm::Value *value, size_t size, 1520b57cec5SDimitry Andric lldb::offset_t alignment); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric /// [Used by IRForTarget] Finalize the struct, laying out the position of 1550b57cec5SDimitry Andric /// each object in it. 1560b57cec5SDimitry Andric /// 1570b57cec5SDimitry Andric /// \return 1580b57cec5SDimitry Andric /// True on success; false otherwise. 1590b57cec5SDimitry Andric bool DoStructLayout(); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric /// [Used by IRForTarget] Get general information about the laid-out struct 1620b57cec5SDimitry Andric /// after DoStructLayout() has been called. 1630b57cec5SDimitry Andric /// 1640b57cec5SDimitry Andric /// \param[out] num_elements 1650b57cec5SDimitry Andric /// The number of elements in the struct. 1660b57cec5SDimitry Andric /// 1670b57cec5SDimitry Andric /// \param[out] size 1680b57cec5SDimitry Andric /// The size of the struct, in bytes. 1690b57cec5SDimitry Andric /// 1700b57cec5SDimitry Andric /// \param[out] alignment 1710b57cec5SDimitry Andric /// The alignment of the struct, in bytes. 1720b57cec5SDimitry Andric /// 1730b57cec5SDimitry Andric /// \return 1740b57cec5SDimitry Andric /// True if the information could be retrieved; false otherwise. 1750b57cec5SDimitry Andric bool GetStructInfo(uint32_t &num_elements, size_t &size, 1760b57cec5SDimitry Andric lldb::offset_t &alignment); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric /// [Used by IRForTarget] Get specific information about one field of the 1790b57cec5SDimitry Andric /// laid-out struct after DoStructLayout() has been called. 1800b57cec5SDimitry Andric /// 1810b57cec5SDimitry Andric /// \param[out] decl 1820b57cec5SDimitry Andric /// The parsed Decl for the field, as generated by ClangASTSource 1830b57cec5SDimitry Andric /// on ClangExpressionDeclMap's behalf. In the case of the result 1840b57cec5SDimitry Andric /// value, this will have the name $__lldb_result even if the 1850b57cec5SDimitry Andric /// result value ends up having the name $1. This is an 1860b57cec5SDimitry Andric /// implementation detail of IRForTarget. 1870b57cec5SDimitry Andric /// 1880b57cec5SDimitry Andric /// \param[out] value 1890b57cec5SDimitry Andric /// The IR value for the field (usually a GlobalVariable). In 1900b57cec5SDimitry Andric /// the case of the result value, this will have the correct 1910b57cec5SDimitry Andric /// name ($1, for instance). This is an implementation detail 1920b57cec5SDimitry Andric /// of IRForTarget. 1930b57cec5SDimitry Andric /// 1940b57cec5SDimitry Andric /// \param[out] offset 1950b57cec5SDimitry Andric /// The offset of the field from the beginning of the struct. 1960b57cec5SDimitry Andric /// As long as the struct is aligned according to its required 1970b57cec5SDimitry Andric /// alignment, this offset will align the field correctly. 1980b57cec5SDimitry Andric /// 1990b57cec5SDimitry Andric /// \param[out] name 2000b57cec5SDimitry Andric /// The name of the field as used in materialization. 2010b57cec5SDimitry Andric /// 2020b57cec5SDimitry Andric /// \param[in] index 2030b57cec5SDimitry Andric /// The index of the field about which information is requested. 2040b57cec5SDimitry Andric /// 2050b57cec5SDimitry Andric /// \return 2060b57cec5SDimitry Andric /// True if the information could be retrieved; false otherwise. 2070b57cec5SDimitry Andric bool GetStructElement(const clang::NamedDecl *&decl, llvm::Value *&value, 2080b57cec5SDimitry Andric lldb::offset_t &offset, ConstString &name, 2090b57cec5SDimitry Andric uint32_t index); 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric /// [Used by IRForTarget] Get information about a function given its Decl. 2120b57cec5SDimitry Andric /// 2130b57cec5SDimitry Andric /// \param[in] decl 2140b57cec5SDimitry Andric /// The parsed Decl for the Function, as generated by ClangASTSource 2150b57cec5SDimitry Andric /// on ClangExpressionDeclMap's behalf. 2160b57cec5SDimitry Andric /// 2170b57cec5SDimitry Andric /// \param[out] ptr 2180b57cec5SDimitry Andric /// The absolute address of the function in the target. 2190b57cec5SDimitry Andric /// 2200b57cec5SDimitry Andric /// \return 2210b57cec5SDimitry Andric /// True if the information could be retrieved; false otherwise. 2220b57cec5SDimitry Andric bool GetFunctionInfo(const clang::NamedDecl *decl, uint64_t &ptr); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric /// [Used by IRForTarget] Get the address of a symbol given nothing but its 2250b57cec5SDimitry Andric /// name. 2260b57cec5SDimitry Andric /// 2270b57cec5SDimitry Andric /// \param[in] target 2280b57cec5SDimitry Andric /// The target to find the symbol in. If not provided, 2290b57cec5SDimitry Andric /// then the current parsing context's Target. 2300b57cec5SDimitry Andric /// 2310b57cec5SDimitry Andric /// \param[in] process 2320b57cec5SDimitry Andric /// The process to use. For Objective-C symbols, the process's 2330b57cec5SDimitry Andric /// Objective-C language runtime may be queried if the process 2340b57cec5SDimitry Andric /// is non-NULL. 2350b57cec5SDimitry Andric /// 2360b57cec5SDimitry Andric /// \param[in] name 2370b57cec5SDimitry Andric /// The name of the symbol. 2380b57cec5SDimitry Andric /// 2390b57cec5SDimitry Andric /// \param[in] module 2400b57cec5SDimitry Andric /// The module to limit the search to. This can be NULL 2410b57cec5SDimitry Andric /// 2420b57cec5SDimitry Andric /// \return 2430b57cec5SDimitry Andric /// Valid load address for the symbol 2440b57cec5SDimitry Andric lldb::addr_t GetSymbolAddress(Target &target, Process *process, 2450b57cec5SDimitry Andric ConstString name, lldb::SymbolType symbol_type, 2460b57cec5SDimitry Andric Module *module = nullptr); 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric lldb::addr_t GetSymbolAddress(ConstString name, 2490b57cec5SDimitry Andric lldb::SymbolType symbol_type); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric struct TargetInfo { 252fe6060f1SDimitry Andric lldb::ByteOrder byte_order = lldb::eByteOrderInvalid; 253fe6060f1SDimitry Andric size_t address_byte_size = 0; 2540b57cec5SDimitry Andric 255fe6060f1SDimitry Andric TargetInfo() = default; 2560b57cec5SDimitry Andric IsValidTargetInfo2570b57cec5SDimitry Andric bool IsValid() { 2580b57cec5SDimitry Andric return (byte_order != lldb::eByteOrderInvalid && address_byte_size != 0); 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric }; 2610b57cec5SDimitry Andric TargetInfo GetTargetInfo(); 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric /// [Used by ClangASTSource] Find all entities matching a given name, using 2640b57cec5SDimitry Andric /// a NameSearchContext to make Decls for them. 2650b57cec5SDimitry Andric /// 2660b57cec5SDimitry Andric /// \param[in] context 2670b57cec5SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 2680b57cec5SDimitry Andric void FindExternalVisibleDecls(NameSearchContext &context) override; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric /// Find all entities matching a given name in a given module/namespace, 2710b57cec5SDimitry Andric /// using a NameSearchContext to make Decls for them. 2720b57cec5SDimitry Andric /// 2730b57cec5SDimitry Andric /// \param[in] context 2740b57cec5SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 2750b57cec5SDimitry Andric /// 2760b57cec5SDimitry Andric /// \param[in] module 2770b57cec5SDimitry Andric /// If non-NULL, the module to query. 2780b57cec5SDimitry Andric /// 2790b57cec5SDimitry Andric /// \param[in] namespace_decl 2800b57cec5SDimitry Andric /// If valid and module is non-NULL, the parent namespace. 2810b57cec5SDimitry Andric void FindExternalVisibleDecls(NameSearchContext &context, 2820b57cec5SDimitry Andric lldb::ModuleSP module, 2835ffd83dbSDimitry Andric const CompilerDeclContext &namespace_decl); 2840b57cec5SDimitry Andric 285480093f4SDimitry Andric protected: 286480093f4SDimitry Andric /// Retrieves the declaration with the given name from the storage of 287480093f4SDimitry Andric /// persistent declarations. 288480093f4SDimitry Andric /// 289480093f4SDimitry Andric /// \return 290480093f4SDimitry Andric /// A persistent decl with the given name or a nullptr. 291480093f4SDimitry Andric virtual clang::NamedDecl *GetPersistentDecl(ConstString name); 292480093f4SDimitry Andric 2930b57cec5SDimitry Andric private: 2940b57cec5SDimitry Andric ExpressionVariableList 2950b57cec5SDimitry Andric m_found_entities; ///< All entities that were looked up for the parser. 2960b57cec5SDimitry Andric ExpressionVariableList 2970b57cec5SDimitry Andric m_struct_members; ///< All entities that need to be placed in the struct. 2980b57cec5SDimitry Andric bool m_keep_result_in_memory; ///< True if result persistent variables 2990b57cec5SDimitry Andric ///generated by this expression should stay in 3000b57cec5SDimitry Andric ///memory. 3010b57cec5SDimitry Andric Materializer::PersistentVariableDelegate 3020b57cec5SDimitry Andric *m_result_delegate; ///< If non-NULL, used to report expression results to 3030b57cec5SDimitry Andric ///ClangUserExpression. 3040b57cec5SDimitry Andric ValueObject *m_ctx_obj; ///< If not empty, then expression is 3050b57cec5SDimitry Andric ///evaluated in context of this object. 3060b57cec5SDimitry Andric ///For details see the comment to 3070b57cec5SDimitry Andric ///`UserExpression::Evaluate`. 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric /// The following values should not live beyond parsing 3100b57cec5SDimitry Andric class ParserVars { 3110b57cec5SDimitry Andric public: 312fe6060f1SDimitry Andric ParserVars() = default; 3130b57cec5SDimitry Andric GetTarget()3140b57cec5SDimitry Andric Target *GetTarget() { 3150b57cec5SDimitry Andric if (m_exe_ctx.GetTargetPtr()) 3160b57cec5SDimitry Andric return m_exe_ctx.GetTargetPtr(); 3170b57cec5SDimitry Andric else if (m_sym_ctx.target_sp) 318480093f4SDimitry Andric return m_sym_ctx.target_sp.get(); 3190b57cec5SDimitry Andric return nullptr; 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric ExecutionContext m_exe_ctx; ///< The execution context to use when parsing. 3230b57cec5SDimitry Andric SymbolContext m_sym_ctx; ///< The symbol context to use in finding variables 3240b57cec5SDimitry Andric ///and types. 3250b57cec5SDimitry Andric ClangPersistentVariables *m_persistent_vars = 3260b57cec5SDimitry Andric nullptr; ///< The persistent variables for the process. 3270b57cec5SDimitry Andric bool m_enable_lookups = false; ///< Set to true during parsing if we have 3280b57cec5SDimitry Andric ///found the first "$__lldb" name. 3290b57cec5SDimitry Andric TargetInfo m_target_info; ///< Basic information about the target. 3300b57cec5SDimitry Andric Materializer *m_materializer = nullptr; ///< If non-NULL, the materializer 3310b57cec5SDimitry Andric ///to use when reporting used 3320b57cec5SDimitry Andric ///variables. 3330b57cec5SDimitry Andric clang::ASTConsumer *m_code_gen = nullptr; ///< If non-NULL, a code generator 3340b57cec5SDimitry Andric ///that receives new top-level 3350b57cec5SDimitry Andric ///functions. 336e8d8bef9SDimitry Andric DiagnosticManager *m_diagnostics = nullptr; 337e8d8bef9SDimitry Andric 3380b57cec5SDimitry Andric private: 3395ffd83dbSDimitry Andric ParserVars(const ParserVars &) = delete; 3405ffd83dbSDimitry Andric const ParserVars &operator=(const ParserVars &) = delete; 3410b57cec5SDimitry Andric }; 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric std::unique_ptr<ParserVars> m_parser_vars; 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric /// Activate parser-specific variables EnableParserVars()3460b57cec5SDimitry Andric void EnableParserVars() { 3470b57cec5SDimitry Andric if (!m_parser_vars.get()) 3489dba64beSDimitry Andric m_parser_vars = std::make_unique<ParserVars>(); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric /// Deallocate parser-specific variables DisableParserVars()3520b57cec5SDimitry Andric void DisableParserVars() { m_parser_vars.reset(); } 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric /// The following values contain layout information for the materialized 3550b57cec5SDimitry Andric /// struct, but are not specific to a single materialization 3560b57cec5SDimitry Andric struct StructVars { 357753f127fSDimitry Andric StructVars() = default; 3580b57cec5SDimitry Andric 359fe6060f1SDimitry Andric lldb::offset_t m_struct_alignment = 360fe6060f1SDimitry Andric 0; ///< The alignment of the struct in bytes. 361fe6060f1SDimitry Andric size_t m_struct_size = 0; ///< The size of the struct in bytes. 362fe6060f1SDimitry Andric bool m_struct_laid_out = 363fe6060f1SDimitry Andric false; ///< True if the struct has been laid out and the 3640b57cec5SDimitry Andric /// layout is valid (that is, no new fields have been 3650b57cec5SDimitry Andric /// added since). 3660b57cec5SDimitry Andric ConstString 3670b57cec5SDimitry Andric m_result_name; ///< The name of the result variable ($1, for example) 3680b57cec5SDimitry Andric }; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric std::unique_ptr<StructVars> m_struct_vars; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric /// Activate struct variables EnableStructVars()3730b57cec5SDimitry Andric void EnableStructVars() { 3740b57cec5SDimitry Andric if (!m_struct_vars.get()) 3750b57cec5SDimitry Andric m_struct_vars.reset(new struct StructVars); 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric /// Deallocate struct variables DisableStructVars()3790b57cec5SDimitry Andric void DisableStructVars() { m_struct_vars.reset(); } 3800b57cec5SDimitry Andric GetScratchContext(Target & target)381bdd1243dSDimitry Andric lldb::TypeSystemClangSP GetScratchContext(Target &target) { 382e8d8bef9SDimitry Andric return ScratchTypeSystemClang::GetForTarget(target, 383e8d8bef9SDimitry Andric m_ast_context->getLangOpts()); 384e8d8bef9SDimitry Andric } 385e8d8bef9SDimitry Andric 3860b57cec5SDimitry Andric /// Get this parser's ID for use in extracting parser- and JIT-specific data 3870b57cec5SDimitry Andric /// from persistent variables. GetParserID()3880b57cec5SDimitry Andric uint64_t GetParserID() { return (uint64_t) this; } 3890b57cec5SDimitry Andric 390480093f4SDimitry Andric /// Should be called on all copied functions. 391480093f4SDimitry Andric void MaybeRegisterFunctionBody(clang::FunctionDecl *copied_function_decl); 392480093f4SDimitry Andric 393480093f4SDimitry Andric /// Searches the persistent decls of the target for entities with the 394480093f4SDimitry Andric /// given name. 395480093f4SDimitry Andric /// 396480093f4SDimitry Andric /// \param[in] context 397480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 398480093f4SDimitry Andric /// 399480093f4SDimitry Andric /// \param[in] name 400480093f4SDimitry Andric /// The name of the entities that need to be found. 4015ffd83dbSDimitry Andric void SearchPersistenDecls(NameSearchContext &context, const ConstString name); 402480093f4SDimitry Andric 403480093f4SDimitry Andric /// Handles looking up $__lldb_class which requires special treatment. 404480093f4SDimitry Andric /// 405480093f4SDimitry Andric /// \param[in] context 406480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 4075ffd83dbSDimitry Andric void LookUpLldbClass(NameSearchContext &context); 408480093f4SDimitry Andric 409480093f4SDimitry Andric /// Handles looking up $__lldb_objc_class which requires special treatment. 410480093f4SDimitry Andric /// 411480093f4SDimitry Andric /// \param[in] context 412480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 4135ffd83dbSDimitry Andric void LookUpLldbObjCClass(NameSearchContext &context); 414480093f4SDimitry Andric 415480093f4SDimitry Andric /// Handles looking up the synthetic namespace that contains our local 416480093f4SDimitry Andric /// variables for the current frame. 417480093f4SDimitry Andric /// 418480093f4SDimitry Andric /// \param[in] sym_ctx 419480093f4SDimitry Andric /// The current SymbolContext of this frame. 420480093f4SDimitry Andric /// 421480093f4SDimitry Andric /// \param[in] name_context 422480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 423480093f4SDimitry Andric void LookupLocalVarNamespace(SymbolContext &sym_ctx, 424480093f4SDimitry Andric NameSearchContext &name_context); 425480093f4SDimitry Andric 426480093f4SDimitry Andric /// Lookup entities in the ClangModulesDeclVendor. 427480093f4SDimitry Andric /// \param[in] context 428480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 429480093f4SDimitry Andric /// 430480093f4SDimitry Andric /// \param[in] name 431480093f4SDimitry Andric /// The name of the entities that need to be found. 4325ffd83dbSDimitry Andric void LookupInModulesDeclVendor(NameSearchContext &context, ConstString name); 433480093f4SDimitry Andric 434480093f4SDimitry Andric /// Looks up a local variable. 435480093f4SDimitry Andric /// 436480093f4SDimitry Andric /// \param[in] context 437480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 438480093f4SDimitry Andric /// 439480093f4SDimitry Andric /// \param[in] name 440480093f4SDimitry Andric /// The name of the entities that need to be found. 441480093f4SDimitry Andric /// 442480093f4SDimitry Andric /// \param[in] sym_ctx 443480093f4SDimitry Andric /// The current SymbolContext of this frame. 444480093f4SDimitry Andric /// 445480093f4SDimitry Andric /// \param[in] namespace_decl 446480093f4SDimitry Andric /// The parent namespace if there is one. 447480093f4SDimitry Andric /// 448480093f4SDimitry Andric /// \return 449480093f4SDimitry Andric /// True iff a local variable was found. 450480093f4SDimitry Andric bool LookupLocalVariable(NameSearchContext &context, ConstString name, 4515ffd83dbSDimitry Andric SymbolContext &sym_ctx, 4525ffd83dbSDimitry Andric const CompilerDeclContext &namespace_decl); 453480093f4SDimitry Andric 454480093f4SDimitry Andric /// Searches for functions in the given SymbolContextList. 455480093f4SDimitry Andric /// 456480093f4SDimitry Andric /// \param[in] sc_list 457480093f4SDimitry Andric /// The SymbolContextList to search. 458480093f4SDimitry Andric /// 459480093f4SDimitry Andric /// \param[in] frame_decl_context 460480093f4SDimitry Andric /// The current DeclContext of the current frame. 461480093f4SDimitry Andric /// 462480093f4SDimitry Andric /// \return 463480093f4SDimitry Andric /// A SymbolContextList with any found functions in the front and 464480093f4SDimitry Andric /// any unknown SymbolContexts which are not functions in the back. 465480093f4SDimitry Andric /// The SymbolContexts for the functions are ordered by how close they are 466480093f4SDimitry Andric /// to the DeclContext for the given frame DeclContext. 467480093f4SDimitry Andric SymbolContextList SearchFunctionsInSymbolContexts( 468480093f4SDimitry Andric const SymbolContextList &sc_list, 469480093f4SDimitry Andric const CompilerDeclContext &frame_decl_context); 470480093f4SDimitry Andric 471480093f4SDimitry Andric /// Looks up a function. 472480093f4SDimitry Andric /// 473480093f4SDimitry Andric /// \param[in] context 474480093f4SDimitry Andric /// The NameSearchContext that can construct Decls for this name. 475480093f4SDimitry Andric /// 476480093f4SDimitry Andric /// \param[in] module_sp 477480093f4SDimitry Andric /// If non-NULL, the module to query. 478480093f4SDimitry Andric /// 479480093f4SDimitry Andric /// \param[in] name 480480093f4SDimitry Andric /// The name of the function that should be find. 481480093f4SDimitry Andric /// 482480093f4SDimitry Andric /// \param[in] namespace_decl 483480093f4SDimitry Andric /// If valid and module is non-NULL, the parent namespace. 484480093f4SDimitry Andric void LookupFunction(NameSearchContext &context, lldb::ModuleSP module_sp, 4855ffd83dbSDimitry Andric ConstString name, 4865ffd83dbSDimitry Andric const CompilerDeclContext &namespace_decl); 487480093f4SDimitry Andric 4880b57cec5SDimitry Andric /// Given a target, find a variable that matches the given name and type. 4890b57cec5SDimitry Andric /// 4900b57cec5SDimitry Andric /// \param[in] target 4910b57cec5SDimitry Andric /// The target to use as a basis for finding the variable. 4920b57cec5SDimitry Andric /// 4930b57cec5SDimitry Andric /// \param[in] module 4940b57cec5SDimitry Andric /// If non-NULL, the module to search. 4950b57cec5SDimitry Andric /// 4960b57cec5SDimitry Andric /// \param[in] name 4970b57cec5SDimitry Andric /// The name as a plain C string. 4980b57cec5SDimitry Andric /// 4990b57cec5SDimitry Andric /// \param[in] namespace_decl 5000b57cec5SDimitry Andric /// If non-NULL and module is non-NULL, the parent namespace. 5010b57cec5SDimitry Andric /// 5020b57cec5SDimitry Andric /// \return 5030b57cec5SDimitry Andric /// The LLDB Variable found, or NULL if none was found. 5045ffd83dbSDimitry Andric lldb::VariableSP 5055ffd83dbSDimitry Andric FindGlobalVariable(Target &target, lldb::ModuleSP &module, ConstString name, 5065ffd83dbSDimitry Andric const CompilerDeclContext &namespace_decl); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric /// Get the value of a variable in a given execution context and return the 5090b57cec5SDimitry Andric /// associated Types if needed. 5100b57cec5SDimitry Andric /// 5110b57cec5SDimitry Andric /// \param[in] var 5120b57cec5SDimitry Andric /// The variable to evaluate. 5130b57cec5SDimitry Andric /// 5140b57cec5SDimitry Andric /// \param[out] var_location 5150b57cec5SDimitry Andric /// The variable location value to fill in 5160b57cec5SDimitry Andric /// 5170b57cec5SDimitry Andric /// \param[out] found_type 5180b57cec5SDimitry Andric /// The type of the found value, as it was found in the user process. 5190b57cec5SDimitry Andric /// This is only useful when the variable is being inspected on behalf 5200b57cec5SDimitry Andric /// of the parser, hence the default. 5210b57cec5SDimitry Andric /// 5220b57cec5SDimitry Andric /// \param[out] parser_type 5230b57cec5SDimitry Andric /// The type of the found value, as it was copied into the parser's 5240b57cec5SDimitry Andric /// AST context. This is only useful when the variable is being 5250b57cec5SDimitry Andric /// inspected on behalf of the parser, hence the default. 5260b57cec5SDimitry Andric /// 5270b57cec5SDimitry Andric /// \return 5280b57cec5SDimitry Andric /// Return true if the value was successfully filled in. 5290b57cec5SDimitry Andric bool GetVariableValue(lldb::VariableSP &var, 5300b57cec5SDimitry Andric lldb_private::Value &var_location, 5310b57cec5SDimitry Andric TypeFromUser *found_type = nullptr, 5320b57cec5SDimitry Andric TypeFromParser *parser_type = nullptr); 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given LLDB 535fcaf7f86SDimitry Andric /// ValueObject, and put it in the list of found entities. 536fcaf7f86SDimitry Andric /// 537fcaf7f86SDimitry Andric /// Helper function used by the other AddOneVariable APIs. 538fcaf7f86SDimitry Andric /// 539fcaf7f86SDimitry Andric /// \param[in,out] context 540fcaf7f86SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 541fcaf7f86SDimitry Andric /// 542fcaf7f86SDimitry Andric /// \param[in] pt 543fcaf7f86SDimitry Andric /// The CompilerType of the variable we're adding a Decl for. 544fcaf7f86SDimitry Andric /// 545fcaf7f86SDimitry Andric /// \param[in] var 546fcaf7f86SDimitry Andric /// The LLDB ValueObject that needs a Decl. 547fcaf7f86SDimitry Andric ClangExpressionVariable::ParserVars * 548fcaf7f86SDimitry Andric AddExpressionVariable(NameSearchContext &context, TypeFromParser const &pt, 549fcaf7f86SDimitry Andric lldb::ValueObjectSP valobj); 550fcaf7f86SDimitry Andric 551fcaf7f86SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given LLDB 5520b57cec5SDimitry Andric /// Variable, and put it in the Tuple list. 5530b57cec5SDimitry Andric /// 5540b57cec5SDimitry Andric /// \param[in] context 5550b57cec5SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 5560b57cec5SDimitry Andric /// 5570b57cec5SDimitry Andric /// \param[in] var 5580b57cec5SDimitry Andric /// The LLDB Variable that needs a Decl. 5590b57cec5SDimitry Andric /// 5600b57cec5SDimitry Andric /// \param[in] valobj 5610b57cec5SDimitry Andric /// The LLDB ValueObject for that variable. 5620b57cec5SDimitry Andric void AddOneVariable(NameSearchContext &context, lldb::VariableSP var, 5635ffd83dbSDimitry Andric lldb::ValueObjectSP valobj); 5640b57cec5SDimitry Andric 565fcaf7f86SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given ValueObject 566fcaf7f86SDimitry Andric /// and put it in the list of found entities. 567fcaf7f86SDimitry Andric /// 568fcaf7f86SDimitry Andric /// \param[in,out] context 569fcaf7f86SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 570fcaf7f86SDimitry Andric /// 571fcaf7f86SDimitry Andric /// \param[in] valobj 572fcaf7f86SDimitry Andric /// The ValueObject that needs a Decl. 573fcaf7f86SDimitry Andric /// 574fcaf7f86SDimitry Andric /// \param[in] valobj_provider Callback that fetches a ValueObjectSP 575fcaf7f86SDimitry Andric /// from the specified frame 576fcaf7f86SDimitry Andric void AddOneVariable(NameSearchContext &context, lldb::ValueObjectSP valobj, 577fcaf7f86SDimitry Andric ValueObjectProviderTy valobj_provider); 578fcaf7f86SDimitry Andric 5790b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given persistent 5800b57cec5SDimitry Andric /// variable, and put it in the list of found entities. 5810b57cec5SDimitry Andric /// 5820b57cec5SDimitry Andric /// \param[in] context 5830b57cec5SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 5840b57cec5SDimitry Andric /// 585480093f4SDimitry Andric /// \param[in] pvar_sp 5860b57cec5SDimitry Andric /// The persistent variable that needs a Decl. 5870b57cec5SDimitry Andric void AddOneVariable(NameSearchContext &context, 5885ffd83dbSDimitry Andric lldb::ExpressionVariableSP &pvar_sp); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given LLDB symbol 5910b57cec5SDimitry Andric /// (treated as a variable), and put it in the list of found entities. 5925ffd83dbSDimitry Andric void AddOneGenericVariable(NameSearchContext &context, const Symbol &symbol); 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given function. 5950b57cec5SDimitry Andric /// (Functions are not placed in the Tuple list.) Can handle both fully 5960b57cec5SDimitry Andric /// typed functions and generic functions. 5970b57cec5SDimitry Andric /// 5980b57cec5SDimitry Andric /// \param[in] context 5990b57cec5SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 6000b57cec5SDimitry Andric /// 6010b57cec5SDimitry Andric /// \param[in] fun 6020b57cec5SDimitry Andric /// The Function that needs to be created. If non-NULL, this is 6030b57cec5SDimitry Andric /// a fully-typed function. 6040b57cec5SDimitry Andric /// 6050b57cec5SDimitry Andric /// \param[in] sym 6060b57cec5SDimitry Andric /// The Symbol that corresponds to a function that needs to be 6070b57cec5SDimitry Andric /// created with generic type (unitptr_t foo(...)). 6085ffd83dbSDimitry Andric void AddOneFunction(NameSearchContext &context, Function *fun, Symbol *sym); 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given register. 6110b57cec5SDimitry Andric /// 6120b57cec5SDimitry Andric /// \param[in] context 6130b57cec5SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 6140b57cec5SDimitry Andric /// 6150b57cec5SDimitry Andric /// \param[in] reg_info 6160b57cec5SDimitry Andric /// The information corresponding to that register. 6175ffd83dbSDimitry Andric void AddOneRegister(NameSearchContext &context, const RegisterInfo *reg_info); 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric /// Use the NameSearchContext to generate a Decl for the given type. (Types 6200b57cec5SDimitry Andric /// are not placed in the Tuple list.) 6210b57cec5SDimitry Andric /// 6220b57cec5SDimitry Andric /// \param[in] context 6230b57cec5SDimitry Andric /// The NameSearchContext to use when constructing the Decl. 6240b57cec5SDimitry Andric /// 6250b57cec5SDimitry Andric /// \param[in] type 6260b57cec5SDimitry Andric /// The type that needs to be created. 6275ffd83dbSDimitry Andric void AddOneType(NameSearchContext &context, const TypeFromUser &type); 6280b57cec5SDimitry Andric 6295ffd83dbSDimitry Andric /// Adds the class in which the expression is evaluated to the lookup and 6305ffd83dbSDimitry Andric /// prepares the class to be used as a context for expression evaluation (for 6315ffd83dbSDimitry Andric /// example, it creates a fake member function that will contain the 6325ffd83dbSDimitry Andric /// expression LLDB is trying to evaluate). 6330b57cec5SDimitry Andric /// 6340b57cec5SDimitry Andric /// \param[in] context 6355ffd83dbSDimitry Andric /// The NameSearchContext to which the class should be added as a lookup 6365ffd83dbSDimitry Andric /// result. 6370b57cec5SDimitry Andric /// 6380b57cec5SDimitry Andric /// \param[in] type 6395ffd83dbSDimitry Andric /// The type of the class that serves as the evaluation context. 6405ffd83dbSDimitry Andric void AddContextClassType(NameSearchContext &context, 6415ffd83dbSDimitry Andric const TypeFromUser &type); 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric /// Move a type out of the current ASTContext into another, but make sure to 6440b57cec5SDimitry Andric /// export all components of the type also. 6450b57cec5SDimitry Andric /// 6460b57cec5SDimitry Andric /// \param[in] target 6475ffd83dbSDimitry Andric /// The TypeSystemClang to move to. 6480b57cec5SDimitry Andric /// \param[in] source 6495ffd83dbSDimitry Andric /// The TypeSystemClang to move from. This is assumed to be going away. 6500b57cec5SDimitry Andric /// \param[in] parser_type 6510b57cec5SDimitry Andric /// The type as it appears in the source context. 6520b57cec5SDimitry Andric /// 6530b57cec5SDimitry Andric /// \return 6540b57cec5SDimitry Andric /// Returns the moved type, or an empty type if there was a problem. 6555ffd83dbSDimitry Andric TypeFromUser DeportType(TypeSystemClang &target, TypeSystemClang &source, 6560b57cec5SDimitry Andric TypeFromParser parser_type); 6570b57cec5SDimitry Andric 6585ffd83dbSDimitry Andric TypeSystemClang *GetTypeSystemClang(); 6590b57cec5SDimitry Andric }; 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric } // namespace lldb_private 6620b57cec5SDimitry Andric 6635ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONDECLMAP_H 664