ClangExpressionDeclMap.h revision 73b520f4f60dca58e58e446c9504d45384ee677b
1//===-- ClangExpressionDeclMap.h --------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_ClangExpressionDeclMap_h_
11#define liblldb_ClangExpressionDeclMap_h_
12
13// C Includes
14#include <signal.h>
15#include <stdint.h>
16
17// C++ Includes
18#include <vector>
19
20// Other libraries and framework includes
21// Project includes
22#include "llvm/ADT/APInt.h"
23#include "llvm/ADT/DenseMap.h"
24#include "clang/AST/Decl.h"
25#include "lldb/lldb-public.h"
26#include "lldb/Core/ClangForward.h"
27#include "lldb/Core/Value.h"
28#include "lldb/Expression/ClangASTSource.h"
29#include "lldb/Expression/ClangExpressionVariable.h"
30#include "lldb/Symbol/ClangASTImporter.h"
31#include "lldb/Symbol/TaggedASTType.h"
32#include "lldb/Symbol/SymbolContext.h"
33#include "lldb/Target/ExecutionContext.h"
34
35namespace lldb_private {
36
37//----------------------------------------------------------------------
38/// @class ClangExpressionDeclMap ClangExpressionDeclMap.h "lldb/Expression/ClangExpressionDeclMap.h"
39/// @brief Manages named entities that are defined in LLDB's debug information.
40///
41/// The Clang parser uses the ClangASTSource as an interface to request named
42/// entities from outside an expression.  The ClangASTSource reports back, listing
43/// all possible objects corresponding to a particular name.  But it in turn
44/// relies on ClangExpressionDeclMap, which performs several important functions.
45///
46/// First, it records what variables and functions were looked up and what Decls
47/// were returned for them.
48///
49/// Second, it constructs a struct on behalf of IRForTarget, recording which
50/// variables should be placed where and relaying this information back so that
51/// IRForTarget can generate context-independent code.
52///
53/// Third, it "materializes" this struct on behalf of the expression command,
54/// finding the current values of each variable and placing them into the
55/// struct so that it can be passed to the JITted version of the IR.
56///
57/// Fourth and finally, it "dematerializes" the struct after the JITted code has
58/// has executed, placing the new values back where it found the old ones.
59//----------------------------------------------------------------------
60class ClangExpressionDeclMap :
61    public ClangASTSource
62{
63public:
64    //------------------------------------------------------------------
65    /// Constructor
66    ///
67    /// Initializes class variables.
68    ///
69    /// @param[in] keep_result_in_memory
70    ///     If true, inhibits the normal deallocation of the memory for
71    ///     the result persistent variable, and instead marks the variable
72    ///     as persisting.
73    //------------------------------------------------------------------
74    ClangExpressionDeclMap (bool keep_result_in_memory,
75                            ExecutionContext &exe_ctx);
76
77    //------------------------------------------------------------------
78    /// Destructor
79    //------------------------------------------------------------------
80    ~ClangExpressionDeclMap ();
81
82    //------------------------------------------------------------------
83    /// Enable the state needed for parsing and IR transformation.
84    ///
85    /// @param[in] exe_ctx
86    ///     The execution context to use when finding types for variables.
87    ///     Also used to find a "scratch" AST context to store result types.
88    ///
89    /// @return
90    ///     True if parsing is possible; false if it is unsafe to continue.
91    //------------------------------------------------------------------
92    bool
93    WillParse (ExecutionContext &exe_ctx);
94
95    //------------------------------------------------------------------
96    /// [Used by ClangExpressionParser] For each variable that had an unknown
97    ///     type at the beginning of parsing, determine its final type now.
98    ///
99    /// @return
100    ///     True on success; false otherwise.
101    //------------------------------------------------------------------
102    bool
103    ResolveUnknownTypes();
104
105    //------------------------------------------------------------------
106    /// Disable the state needed for parsing and IR transformation.
107    //------------------------------------------------------------------
108    void
109    DidParse ();
110
111    //------------------------------------------------------------------
112    /// [Used by IRForTarget] Get a new result variable name of the form
113    ///     $n, where n is a natural number starting with 0.
114    ///
115    /// @param[in] name
116    ///     The std::string to place the name into.
117    //------------------------------------------------------------------
118    const ConstString &
119    GetPersistentResultName ();
120
121    clang::NamespaceDecl *
122    AddNamespace (NameSearchContext &context,
123                  ClangASTImporter::NamespaceMapSP &namespace_decls);
124
125    //------------------------------------------------------------------
126    /// [Used by IRForTarget] Get a constant variable given a name,
127    ///     a type, and an llvm::APInt.
128    ///
129    /// @param[in] name
130    ///     The name of the variable
131    ///
132    /// @param[in] type
133    ///     The type of the variable, which will be imported into the
134    ///     target's AST context
135    ///
136    /// @param[in] value
137    ///     The value of the variable
138    ///
139    /// @return
140    ///     The created variable
141    //------------------------------------------------------------------
142    lldb::ClangExpressionVariableSP
143    BuildIntegerVariable (const ConstString &name,
144                          lldb_private::TypeFromParser type,
145                          const llvm::APInt& value);
146
147    //------------------------------------------------------------------
148    /// [Used by IRForTarget] Cast an existing variable given a Decl and
149    ///     a type.
150    ///
151    /// @param[in] name
152    ///     The name of the new variable
153    ///
154    /// @param[in] decl
155    ///     The Clang variable declaration for the original variable,
156    ///     which must be looked up in the map
157    ///
158    /// @param[in] type
159    ///     The desired type of the variable after casting
160    ///
161    /// @return
162    ///     The created variable
163    //------------------------------------------------------------------
164    lldb::ClangExpressionVariableSP
165    BuildCastVariable (const ConstString &name,
166                       clang::VarDecl *decl,
167                       lldb_private::TypeFromParser type);
168
169    //------------------------------------------------------------------
170    /// [Used by IRForTarget] Add a variable to the list of persistent
171    ///     variables for the process.
172    ///
173    /// @param[in] decl
174    ///     The Clang declaration for the persistent variable, used for
175    ///     lookup during parsing.
176    ///
177    /// @param[in] name
178    ///     The name of the persistent variable, usually $something.
179    ///
180    /// @param[in] type
181    ///     The type of the variable, in the Clang parser's context.
182    ///
183    /// @return
184    ///     True on success; false otherwise.
185    //------------------------------------------------------------------
186    bool
187    AddPersistentVariable (const clang::NamedDecl *decl,
188                           const ConstString &name,
189                           TypeFromParser type,
190                           bool is_result,
191                           bool is_lvalue);
192
193    //------------------------------------------------------------------
194    /// [Used by IRForTarget] Add a variable to the struct that needs to
195    ///     be materialized each time the expression runs.
196    ///
197    /// @param[in] decl
198    ///     The Clang declaration for the variable.
199    ///
200    /// @param[in] name
201    ///     The name of the variable.
202    ///
203    /// @param[in] value
204    ///     The LLVM IR value for this variable.
205    ///
206    /// @param[in] size
207    ///     The size of the variable in bytes.
208    ///
209    /// @param[in] alignment
210    ///     The required alignment of the variable in bytes.
211    ///
212    /// @return
213    ///     True on success; false otherwise.
214    //------------------------------------------------------------------
215    bool
216    AddValueToStruct (const clang::NamedDecl *decl,
217                      const ConstString &name,
218                      llvm::Value *value,
219                      size_t size,
220                      off_t alignment);
221
222    //------------------------------------------------------------------
223    /// [Used by IRForTarget] Finalize the struct, laying out the position
224    /// of each object in it.
225    ///
226    /// @return
227    ///     True on success; false otherwise.
228    //------------------------------------------------------------------
229    bool
230    DoStructLayout ();
231
232    //------------------------------------------------------------------
233    /// [Used by IRForTarget] Get general information about the laid-out
234    /// struct after DoStructLayout() has been called.
235    ///
236    /// @param[out] num_elements
237    ///     The number of elements in the struct.
238    ///
239    /// @param[out] size
240    ///     The size of the struct, in bytes.
241    ///
242    /// @param[out] alignment
243    ///     The alignment of the struct, in bytes.
244    ///
245    /// @return
246    ///     True if the information could be retrieved; false otherwise.
247    //------------------------------------------------------------------
248    bool
249    GetStructInfo (uint32_t &num_elements,
250                   size_t &size,
251                   off_t &alignment);
252
253    //------------------------------------------------------------------
254    /// [Used by IRForTarget] Get specific information about one field
255    /// of the laid-out struct after DoStructLayout() has been called.
256    ///
257    /// @param[out] decl
258    ///     The parsed Decl for the field, as generated by ClangASTSource
259    ///     on ClangExpressionDeclMap's behalf.  In the case of the result
260    ///     value, this will have the name $__lldb_result even if the
261    ///     result value ends up having the name $1.  This is an
262    ///     implementation detail of IRForTarget.
263    ///
264    /// @param[out] value
265    ///     The IR value for the field (usually a GlobalVariable).  In
266    ///     the case of the result value, this will have the correct
267    ///     name ($1, for instance).  This is an implementation detail
268    ///     of IRForTarget.
269    ///
270    /// @param[out] offset
271    ///     The offset of the field from the beginning of the struct.
272    ///     As long as the struct is aligned according to its required
273    ///     alignment, this offset will align the field correctly.
274    ///
275    /// @param[out] name
276    ///     The name of the field as used in materialization.
277    ///
278    /// @param[in] index
279    ///     The index of the field about which information is requested.
280    ///
281    /// @return
282    ///     True if the information could be retrieved; false otherwise.
283    //------------------------------------------------------------------
284    bool
285    GetStructElement (const clang::NamedDecl *&decl,
286                      llvm::Value *&value,
287                      off_t &offset,
288                      ConstString &name,
289                      uint32_t index);
290
291    //------------------------------------------------------------------
292    /// [Used by IRForTarget] Get information about a function given its
293    /// Decl.
294    ///
295    /// @param[in] decl
296    ///     The parsed Decl for the Function, as generated by ClangASTSource
297    ///     on ClangExpressionDeclMap's behalf.
298    ///
299    /// @param[out] ptr
300    ///     The absolute address of the function in the target.
301    ///
302    /// @return
303    ///     True if the information could be retrieved; false otherwise.
304    //------------------------------------------------------------------
305    bool
306    GetFunctionInfo (const clang::NamedDecl *decl,
307                     uint64_t &ptr);
308
309    //------------------------------------------------------------------
310    /// [Used by IRForTarget] Get the address of a function given nothing
311    /// but its name.  Some functions are needed but didn't get Decls made
312    /// during parsing -- specifically, sel_registerName is never called
313    /// in the generated IR but we need to call it nonetheless.
314    ///
315    /// @param[in] name
316    ///     The name of the function.
317    ///
318    /// @param[out] ptr
319    ///     The absolute address of the function in the target.
320    ///
321    /// @return
322    ///     True if the address could be retrieved; false otherwise.
323    //------------------------------------------------------------------
324    bool
325    GetFunctionAddress (const ConstString &name,
326                        uint64_t &ptr);
327
328    //------------------------------------------------------------------
329    /// [Used by IRForTarget] Get the address of a symbol given nothing
330    /// but its name.
331    ///
332    /// @param[in] target
333    ///     The target to find the symbol in.  If not provided,
334    ///     then the current parsing context's Target.
335    ///
336    /// @param[in] name
337    ///     The name of the symbol.
338    ///
339    /// @return
340    ///     Valid load address for the symbol
341    //------------------------------------------------------------------
342    lldb::addr_t
343    GetSymbolAddress (Target &target,
344                      const ConstString &name);
345
346    lldb::addr_t
347    GetSymbolAddress (const ConstString &name);
348
349    //------------------------------------------------------------------
350    /// [Used by IRInterpreter] Get basic target information.
351    ///
352    /// @param[out] byte_order
353    ///     The byte order of the target.
354    ///
355    /// @param[out] address_byte_size
356    ///     The size of a pointer in bytes.
357    ///
358    /// @return
359    ///     True if the information could be determined; false
360    ///     otherwise.
361    //------------------------------------------------------------------
362    struct TargetInfo
363    {
364        lldb::ByteOrder byte_order;
365        size_t address_byte_size;
366
367        TargetInfo() :
368            byte_order(lldb::eByteOrderInvalid),
369            address_byte_size(0)
370        {
371        }
372
373        bool IsValid()
374        {
375            return (byte_order != lldb::eByteOrderInvalid &&
376                    address_byte_size != 0);
377        }
378    };
379    TargetInfo GetTargetInfo();
380
381    //------------------------------------------------------------------
382    /// [Used by IRInterpreter] Promote an unknown address to a
383    ///     LoadAddress or FileAddress depending on the presence of a
384    ///     process.
385    ///
386    /// @param[in] addr
387    ///     The address to promote.
388    ///
389    /// @return
390    ///     The wrapped entity.
391    //------------------------------------------------------------------
392    lldb_private::Value WrapBareAddress (lldb::addr_t addr);
393
394    //------------------------------------------------------------------
395    /// [Used by IRInterpreter] Write to the target.
396    ///
397    /// @param[in] value
398    ///     The address to write to.
399    ///
400    /// @param[in] addr
401    ///     The address of the data buffer to read from.
402    ///
403    /// @param[in] length
404    ///     The amount of data to write, in bytes.
405    ///
406    /// @return
407    ///     True if the write could be performed; false otherwise.
408    //------------------------------------------------------------------
409    bool
410    WriteTarget (lldb_private::Value &value,
411                 const uint8_t *data,
412                 size_t length);
413
414    //------------------------------------------------------------------
415    /// [Used by IRInterpreter] Read from the target.
416    ///
417    /// @param[in] data
418    ///     The address of the data buffer to write to.
419    ///
420    /// @param[in] value
421    ///     The address to read from.
422    ///
423    /// @param[in] length
424    ///     The amount of data to read, in bytes.
425    ///
426    /// @return
427    ///     True if the read could be performed; false otherwise.
428    //------------------------------------------------------------------
429    bool
430    ReadTarget (uint8_t *data,
431                lldb_private::Value &value,
432                size_t length);
433
434    //------------------------------------------------------------------
435    /// [Used by IRInterpreter] Get the Value for a NamedDecl.
436    ///
437    /// @param[in] decl
438    ///     The Decl whose value is to be found.
439    ///
440    /// @return
441    ///     The value, or NULL.
442    //------------------------------------------------------------------
443    lldb_private::Value
444    LookupDecl (clang::NamedDecl *decl);
445
446    //------------------------------------------------------------------
447    /// [Used by IRInterpreter] Get the Value for "this", "self", or
448    ///   "_cmd".
449    ///
450    /// @param[in] name
451    ///     The name of the entity to be found.
452    ///
453    /// @return
454    ///     The value, or NULL.
455    //------------------------------------------------------------------
456    lldb_private::Value
457    GetSpecialValue (const ConstString &name);
458
459    //------------------------------------------------------------------
460    /// [Used by IRInterpreter] Returns true if the result is a
461    ///   reference to data in the target, meaning it must be
462    ///   dereferenced once more to get its data.
463    ///
464    /// @param[in] name
465    ///     The name of the result.
466    ///
467    /// @return
468    ///     True if the result is a reference; false otherwise (or on
469    ///     error).
470    //------------------------------------------------------------------
471    bool
472    ResultIsReference (const ConstString &name);
473
474    //------------------------------------------------------------------
475    /// [Used by IRInterpreter] Find the result persistent variable,
476    ///   propagate the given value to it, and return it.
477    ///
478    /// @param[out] valobj
479    ///     Set to the complete object.
480    ///
481    /// @param[in] value
482    ///     A value indicating the location of the value's contents.
483    ///
484    /// @param[in] name
485    ///     The name of the result.
486    ///
487    /// @param[in] type
488    ///     The type of the data.
489    ///
490    /// @param[in] transient
491    ///     True if the data should be treated as disappearing after the
492    ///     expression completes.  In that case, it gets no live data.
493    ///
494    /// @param[in] maybe_make_load
495    ///     True if the value is a file address but should be potentially
496    ///     upgraded to a load address if a target is presence.
497    ///
498    /// @return
499    ///     True on success; false otherwise.
500    //------------------------------------------------------------------
501    bool
502    CompleteResultVariable (lldb::ClangExpressionVariableSP &valobj,
503                            lldb_private::Value &value,
504                            const ConstString &name,
505                            lldb_private::TypeFromParser type,
506                            bool transient,
507                            bool maybe_make_load);
508
509    //------------------------------------------------------------------
510    /// [Used by CommandObjectExpression] Materialize the entire struct
511    /// at a given address, which should be aligned as specified by
512    /// GetStructInfo().
513    ///
514    /// @param[in] exe_ctx
515    ///     The execution context at which to dump the struct.
516    ///
517    /// @param[in] struct_address
518    ///     The address at which the struct should be written.
519    ///
520    /// @param[in] error
521    ///     An Error to populate with any messages related to
522    ///     materializing the struct.
523    ///
524    /// @return
525    ///     True on success; false otherwise.
526    //------------------------------------------------------------------
527    bool
528    Materialize (ExecutionContext &exe_ctx,
529                 lldb::addr_t &struct_address,
530                 Error &error);
531
532    //------------------------------------------------------------------
533    /// [Used by CommandObjectExpression] Get the "this" pointer
534    /// from a given execution context.
535    ///
536    /// @param[out] object_ptr
537    ///     The this pointer.
538    ///
539    /// @param[in] object_name
540    ///     The name of the object pointer -- "this," "self," or similar
541    ///     depending on language
542    ///
543    /// @param[in] exe_ctx
544    ///     The execution context at which to dump the struct.
545    ///
546    /// @param[in] error
547    ///     An Error to populate with any messages related to
548    ///     finding the "this" pointer.
549    ///
550    /// @param[in] suppress_type_check
551    ///     True if the type is not needed.
552    ///
553    /// @return
554    ///     True on success; false otherwise.
555    //------------------------------------------------------------------
556    bool
557    GetObjectPointer (lldb::addr_t &object_ptr,
558                      ConstString &object_name,
559                      ExecutionContext &exe_ctx,
560                      Error &error,
561                      bool suppress_type_check = false);
562
563    //------------------------------------------------------------------
564    /// [Used by CommandObjectExpression] Pretty-print a materialized
565    /// struct, which must have been materialized by Materialize(),
566    /// byte for byte on a given stream.
567    ///
568    /// @param[in] exe_ctx
569    ///     The execution context from which to read the struct.
570    ///
571    /// @param[in] s
572    ///     The stream on which to write the pretty-printed output.
573    ///
574    /// @param[in] error
575    ///     An Error to populate with any messages related to
576    ///     pretty-printing the struct.
577    ///
578    /// @return
579    ///     True on success; false otherwise.
580    //------------------------------------------------------------------
581    bool
582    DumpMaterializedStruct (ExecutionContext &exe_ctx,
583                            Stream &s,
584                            Error &error);
585
586    //------------------------------------------------------------------
587    /// [Used by CommandObjectExpression] Deaterialize the entire struct.
588    ///
589    /// @param[in] exe_ctx
590    ///     The execution context from which to read the struct.
591    ///
592    /// @param[out] result
593    ///     A ClangExpressionVariable containing the result of the
594    ///     expression, for potential re-use.
595    ///
596    /// @param[in] stack_frame_top, stack_frame_bottom
597    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
598    ///     in which the expression ran.  A result whose address falls
599    ///     inside this stack frame is dematerialized as a value
600    ///     requiring rematerialization.
601    ///
602    /// @param[in] error
603    ///     An Error to populate with any messages related to
604    ///     dematerializing the struct.
605    ///
606    /// @return
607    ///     True on success; false otherwise.
608    //------------------------------------------------------------------
609    bool
610    Dematerialize (ExecutionContext &exe_ctx,
611                   lldb::ClangExpressionVariableSP &result_sp,
612                   lldb::addr_t stack_frame_top,
613                   lldb::addr_t stack_frame_bottom,
614                   Error &error);
615
616    //------------------------------------------------------------------
617    /// [Used by ClangASTSource] Find all entities matching a given name,
618    /// using a NameSearchContext to make Decls for them.
619    ///
620    /// @param[in] context
621    ///     The NameSearchContext that can construct Decls for this name.
622    ///
623    /// @return
624    ///     True on success; false otherwise.
625    //------------------------------------------------------------------
626    void
627    FindExternalVisibleDecls (NameSearchContext &context);
628
629    //------------------------------------------------------------------
630    /// [Used by ClangASTSource] Find all Decls in a context that match
631    /// a given criterion.
632    ///
633    /// @param[in] decl_context
634    ///     The DeclContext to search.
635    ///
636    /// @param[in] predicate
637    ///     Returns True if a DeclKind is desired; False if not.
638    ///
639    /// @param[in] decls
640    ///     A list to add all found Decls that have a desired DeclKind
641    ///     into.
642    //------------------------------------------------------------------
643    clang::ExternalLoadResult
644    FindExternalLexicalDecls (const clang::DeclContext *decl_context,
645                              bool (*predicate)(clang::Decl::Kind),
646                              llvm::SmallVectorImpl<clang::Decl*> &decls);
647
648    //------------------------------------------------------------------
649    /// [Used by ClangASTSource] Complete the definition of a TagDecl.
650    ///
651    /// @param[in] tag_decl
652    ///     The TagDecl to be completed.
653    //------------------------------------------------------------------
654    void
655    CompleteType (clang::TagDecl *tag_decl);
656
657    //------------------------------------------------------------------
658    /// [Used by ClangASTSource] Complete the definition of an
659    /// ObjCInterfaceDecl.
660    ///
661    /// @param[in] tag_decl
662    ///     The ObjCInterfaceDecl to be completed.
663    //------------------------------------------------------------------
664    void
665    CompleteType (clang::ObjCInterfaceDecl *interface_decl);
666
667private:
668    ClangExpressionVariableList    m_found_entities;           ///< All entities that were looked up for the parser.
669    ClangExpressionVariableList    m_struct_members;           ///< All entities that need to be placed in the struct.
670    bool                           m_keep_result_in_memory;    ///< True if result persistent variables generated by this expression should stay in memory.
671
672    //----------------------------------------------------------------------
673    /// The following values should not live beyond parsing
674    //----------------------------------------------------------------------
675    class ParserVars
676    {
677    public:
678        ParserVars(ClangExpressionDeclMap &decl_map) :
679            m_decl_map(decl_map),
680            m_exe_ctx(NULL),
681            m_sym_ctx(),
682            m_persistent_vars(NULL),
683            m_enable_lookups(false),
684            m_ignore_lookups(false)
685        {
686        }
687
688        Target *
689        GetTarget()
690        {
691            if (m_exe_ctx && m_exe_ctx->GetTargetPtr())
692                return m_exe_ctx->GetTargetPtr();
693            else if (m_sym_ctx.target_sp)
694                m_sym_ctx.target_sp.get();
695            return NULL;
696        }
697
698        ExecutionContext           *m_exe_ctx;          ///< The execution context to use when parsing.
699        SymbolContext               m_sym_ctx;          ///< The symbol context to use in finding variables and types.
700        ClangPersistentVariables   *m_persistent_vars;  ///< The persistent variables for the process.
701        bool                        m_enable_lookups;   ///< Set to true during parsing if we have found the first "$__lldb" name.
702        bool                        m_ignore_lookups;   ///< True during an import when we should be ignoring type lookups.
703        std::auto_ptr<ClangASTImporter> m_ast_importer; ///< The importer used to import types on the parser's behalf.
704        TargetInfo                  m_target_info;      ///< Basic information about the target.
705    private:
706        ClangExpressionDeclMap     &m_decl_map;
707        DISALLOW_COPY_AND_ASSIGN (ParserVars);
708    };
709
710    std::auto_ptr<ParserVars> m_parser_vars;
711
712    //----------------------------------------------------------------------
713    /// Activate parser-specific variables
714    //----------------------------------------------------------------------
715    void
716    EnableParserVars()
717    {
718        if (!m_parser_vars.get())
719            m_parser_vars.reset(new ParserVars(*this));
720    }
721
722    //----------------------------------------------------------------------
723    /// Deallocate parser-specific variables
724    //----------------------------------------------------------------------
725    void
726    DisableParserVars()
727    {
728        m_parser_vars.reset();
729    }
730
731    //----------------------------------------------------------------------
732    /// The following values contain layout information for the materialized
733    /// struct, but are not specific to a single materialization
734    //----------------------------------------------------------------------
735    struct StructVars {
736        StructVars() :
737            m_struct_alignment(0),
738            m_struct_size(0),
739            m_struct_laid_out(false),
740            m_result_name(),
741            m_object_pointer_type(NULL, NULL)
742        {
743        }
744
745        off_t                       m_struct_alignment;         ///< The alignment of the struct in bytes.
746        size_t                      m_struct_size;              ///< The size of the struct in bytes.
747        bool                        m_struct_laid_out;          ///< True if the struct has been laid out and the layout is valid (that is, no new fields have been added since).
748        ConstString                 m_result_name;              ///< The name of the result variable ($1, for example)
749        TypeFromUser                m_object_pointer_type;      ///< The type of the "this" variable, if one exists
750    };
751
752    std::auto_ptr<StructVars> m_struct_vars;
753
754    //----------------------------------------------------------------------
755    /// Activate struct variables
756    //----------------------------------------------------------------------
757    void
758    EnableStructVars()
759    {
760        if (!m_struct_vars.get())
761            m_struct_vars.reset(new struct StructVars);
762    }
763
764    //----------------------------------------------------------------------
765    /// Deallocate struct variables
766    //----------------------------------------------------------------------
767    void
768    DisableStructVars()
769    {
770        m_struct_vars.reset();
771    }
772
773    //----------------------------------------------------------------------
774    /// The following values refer to a specific materialization of the
775    /// structure in a process
776    //----------------------------------------------------------------------
777    struct MaterialVars {
778        MaterialVars() :
779            m_allocated_area(0),
780            m_materialized_location(0)
781        {
782        }
783
784        Process                    *m_process;                  ///< The process that the struct is materialized into.
785        lldb::addr_t                m_allocated_area;           ///< The base of the memory allocated for the struct.  Starts on a potentially unaligned address and may therefore be larger than the struct.
786        lldb::addr_t                m_materialized_location;    ///< The address at which the struct is placed.  Falls inside the allocated area.
787    };
788
789    std::auto_ptr<MaterialVars> m_material_vars;
790
791    //----------------------------------------------------------------------
792    /// Activate materialization-specific variables
793    //----------------------------------------------------------------------
794    void
795    EnableMaterialVars()
796    {
797        if (!m_material_vars.get())
798            m_material_vars.reset(new struct MaterialVars);
799    }
800
801    //----------------------------------------------------------------------
802    /// Deallocate materialization-specific variables
803    //----------------------------------------------------------------------
804    void
805    DisableMaterialVars()
806    {
807        m_material_vars.reset();
808    }
809
810    //------------------------------------------------------------------
811    /// [Used by ClangASTSource] Find all entities matching a given name,
812    /// using a NameSearchContext to make Decls for them.
813    ///
814    /// @param[in] context
815    ///     The NameSearchContext that can construct Decls for this name.
816    ///
817    /// @param[in] module
818    ///     If non-NULL, the module to query.
819    ///
820    /// @param[in] namespace_decl
821    ///     If valid and module is non-NULL, the parent namespace.
822    ///
823    /// @param[in] name
824    ///     The name as a plain C string.  The NameSearchContext contains
825    ///     a DeclarationName for the name so at first the name may seem
826    ///     redundant, but ClangExpressionDeclMap operates in RTTI land so
827    ///     it can't access DeclarationName.
828    ///
829    /// @param[in] current_id
830    ///     The ID for the current FindExternalVisibleDecls invocation,
831    ///     for logging purposes.
832    ///
833    /// @return
834    ///     True on success; false otherwise.
835    //------------------------------------------------------------------
836    void
837    FindExternalVisibleDecls (NameSearchContext &context,
838                              lldb::ModuleSP module,
839                              ClangNamespaceDecl &namespace_decl,
840                              const ConstString &name,
841                              unsigned int current_id);
842
843    //------------------------------------------------------------------
844    /// Given a stack frame, find a variable that matches the given name and
845    /// type.  We need this for expression re-use; we may not always get the
846    /// same lldb::Variable back, and we want the expression to work wherever
847    /// it can.  Returns the variable defined in the tightest scope.
848    ///
849    /// @param[in] frame
850    ///     The stack frame to use as a basis for finding the variable.
851    ///
852    /// @param[in] name
853    ///     The name as a plain C string.
854    ///
855    /// @param[in] type
856    ///     The required type for the variable.  This function may be called
857    ///     during parsing, in which case we don't know its type; hence the
858    ///     default.
859    ///
860    /// @return
861    ///     The LLDB Variable found, or NULL if none was found.
862    //------------------------------------------------------------------
863    lldb::VariableSP
864    FindVariableInScope (StackFrame &frame,
865                         const ConstString &name,
866                         TypeFromUser *type = NULL);
867
868    //------------------------------------------------------------------
869    /// Given a target, find a data symbol that has the given name.
870    ///
871    /// @param[in] target
872    ///     The target to use as the basis for the search.
873    ///
874    /// @param[in] name
875    ///     The name as a plain C string.
876    ///
877    /// @return
878    ///     The LLDB Symbol found, or NULL if none was found.
879    //---------------------------------------------------------
880    Symbol *
881    FindGlobalDataSymbol (Target &target,
882                          const ConstString &name);
883
884    //------------------------------------------------------------------
885    /// Given a target, find a variable that matches the given name and
886    /// type.
887    ///
888    /// @param[in] target
889    ///     The target to use as a basis for finding the variable.
890    ///
891    /// @param[in] module
892    ///     If non-NULL, the module to search.
893    ///
894    /// @param[in] name
895    ///     The name as a plain C string.
896    ///
897    /// @param[in] namespace_decl
898    ///     If non-NULL and module is non-NULL, the parent namespace.
899    ///
900    /// @param[in] type
901    ///     The required type for the variable.  This function may be called
902    ///     during parsing, in which case we don't know its type; hence the
903    ///     default.
904    ///
905    /// @return
906    ///     The LLDB Variable found, or NULL if none was found.
907    //------------------------------------------------------------------
908    lldb::VariableSP
909    FindGlobalVariable (Target &target,
910                        lldb::ModuleSP &module,
911                        const ConstString &name,
912                        ClangNamespaceDecl *namespace_decl,
913                        TypeFromUser *type = NULL);
914
915    //------------------------------------------------------------------
916    /// Get the value of a variable in a given execution context and return
917    /// the associated Types if needed.
918    ///
919    /// @param[in] exe_ctx
920    ///     The execution context to look for the variable in.
921    ///
922    /// @param[in] var
923    ///     The variable to evaluate.
924    ///
925    /// @param[in] parser_ast_context
926    ///     The AST context of the parser, to store the found type in.
927    ///
928    /// @param[out] found_type
929    ///     The type of the found value, as it was found in the user process.
930    ///     This is only useful when the variable is being inspected on behalf
931    ///     of the parser, hence the default.
932    ///
933    /// @param[out] parser_type
934    ///     The type of the found value, as it was copied into the parser's
935    ///     AST context.  This is only useful when the variable is being
936    ///     inspected on behalf of the parser, hence the default.
937    ///
938    /// @param[in] decl
939    ///     The Decl to be looked up.
940    ///
941    /// @return
942    ///     The LLDB Value for the variable.
943    //------------------------------------------------------------------
944    Value *
945    GetVariableValue (ExecutionContext &exe_ctx,
946                      lldb::VariableSP &var,
947                      clang::ASTContext *parser_ast_context,
948                      TypeFromUser *found_type = NULL,
949                      TypeFromParser *parser_type = NULL);
950
951    //------------------------------------------------------------------
952    /// Use the NameSearchContext to generate a Decl for the given LLDB
953    /// Variable, and put it in the Tuple list.
954    ///
955    /// @param[in] context
956    ///     The NameSearchContext to use when constructing the Decl.
957    ///
958    /// @param[in] var
959    ///     The LLDB Variable that needs a Decl.
960    //------------------------------------------------------------------
961    void
962    AddOneVariable (NameSearchContext &context,
963                    lldb::VariableSP var,
964                    unsigned int current_id);
965
966    //------------------------------------------------------------------
967    /// Use the NameSearchContext to generate a Decl for the given
968    /// persistent variable, and put it in the list of found entities.
969    ///
970    /// @param[in] context
971    ///     The NameSearchContext to use when constructing the Decl.
972    ///
973    /// @param[in] pvar
974    ///     The persistent variable that needs a Decl.
975    ///
976    /// @param[in] current_id
977    ///     The ID of the current invocation of FindExternalVisibleDecls
978    ///     for logging purposes.
979    //------------------------------------------------------------------
980    void
981    AddOneVariable (NameSearchContext &context,
982                    lldb::ClangExpressionVariableSP &pvar_sp,
983                    unsigned int current_id);
984
985    //------------------------------------------------------------------
986    /// Use the NameSearchContext to generate a Decl for the given LLDB
987    /// symbol (treated as a variable), and put it in the list of found
988    /// entities.
989    ///
990    /// @param[in] context
991    ///     The NameSearchContext to use when constructing the Decl.
992    ///
993    /// @param[in] var
994    ///     The LLDB Variable that needs a Decl.
995    //------------------------------------------------------------------
996    void
997    AddOneGenericVariable (NameSearchContext &context,
998                           Symbol &symbol,
999                           unsigned int current_id);
1000
1001    //------------------------------------------------------------------
1002    /// Use the NameSearchContext to generate a Decl for the given
1003    /// function.  (Functions are not placed in the Tuple list.)  Can
1004    /// handle both fully typed functions and generic functions.
1005    ///
1006    /// @param[in] context
1007    ///     The NameSearchContext to use when constructing the Decl.
1008    ///
1009    /// @param[in] fun
1010    ///     The Function that needs to be created.  If non-NULL, this is
1011    ///     a fully-typed function.
1012    ///
1013    /// @param[in] sym
1014    ///     The Symbol that corresponds to a function that needs to be
1015    ///     created with generic type (unitptr_t foo(...)).
1016    //------------------------------------------------------------------
1017    void
1018    AddOneFunction (NameSearchContext &context,
1019                    Function *fun,
1020                    Symbol *sym,
1021                    unsigned int current_id);
1022
1023    //------------------------------------------------------------------
1024    /// Use the NameSearchContext to generate a Decl for the given
1025    /// register.
1026    ///
1027    /// @param[in] context
1028    ///     The NameSearchContext to use when constructing the Decl.
1029    ///
1030    /// @param[in] reg_info
1031    ///     The information corresponding to that register.
1032    //------------------------------------------------------------------
1033    void
1034    AddOneRegister (NameSearchContext &context,
1035                    const RegisterInfo *reg_info,
1036                    unsigned int current_id);
1037
1038    //------------------------------------------------------------------
1039    /// Use the NameSearchContext to generate a Decl for the given
1040    /// type.  (Types are not placed in the Tuple list.)
1041    ///
1042    /// @param[in] context
1043    ///     The NameSearchContext to use when constructing the Decl.
1044    ///
1045    /// @param[in] type
1046    ///     The type that needs to be created.
1047    ///
1048    /// @param[in] add_method
1049    ///     True if a method with signature void $__lldb_expr(void*)
1050    ///     should be added to the C++ class type passed in
1051    //------------------------------------------------------------------
1052    void
1053    AddOneType (NameSearchContext &context,
1054                TypeFromUser &type,
1055                unsigned int current_id,
1056                bool add_method);
1057
1058    //------------------------------------------------------------------
1059    /// Actually do the task of materializing or dematerializing the struct.
1060    /// Since both tasks are very similar, although ClangExpressionDeclMap
1061    /// exposes two functions to the outside, both call DoMaterialize.
1062    ///
1063    /// @param[in] dematerialize
1064    ///     True if the struct is to be dematerialized; false if it is to
1065    ///     be materialized.
1066    ///
1067    /// @param[in] exe_ctx
1068    ///     The execution context to use.
1069    ///
1070    /// @param[in] stack_frame_top, stack_frame_bottom
1071    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1072    ///     in which the expression ran.  A result whose address falls
1073    ///     inside this stack frame is dematerialized as a value
1074    ///     requiring rematerialization.
1075    ///
1076    /// @param[out] result
1077    ///     If the struct is being dematerialized, a pointer into which the
1078    ///     location of the result persistent variable is placed.  If not,
1079    ///     NULL.
1080    ///
1081    /// @param[in] err
1082    ///     An Error to populate with any messages related to
1083    ///     (de)materializing the struct.
1084    ///
1085    /// @return
1086    ///     True on success; false otherwise.
1087    //------------------------------------------------------------------
1088    bool
1089    DoMaterialize (bool dematerialize,
1090                   ExecutionContext &exe_ctx,
1091                   lldb::addr_t stack_frame_top,
1092                   lldb::addr_t stack_frame_bottom,
1093                   lldb::ClangExpressionVariableSP *result_sp_ptr,
1094                   Error &err);
1095
1096    //------------------------------------------------------------------
1097    /// Clean up the state required to dematerialize the variable.
1098    //------------------------------------------------------------------
1099    void
1100    DidDematerialize ();
1101
1102    //------------------------------------------------------------------
1103    /// Actually do the task of materializing or dematerializing a persistent
1104    /// variable.
1105    ///
1106    /// @param[in] dematerialize
1107    ///     True if the variable is to be dematerialized; false if it is to
1108    ///     be materialized.
1109    ///
1110    /// @param[in] exe_ctx
1111    ///     The execution context to use.
1112    ///
1113    /// @param[in] var_sp
1114    ///     The persistent variable to materialize
1115    ///
1116    /// @param[in] addr
1117    ///     The address at which to materialize the variable.
1118    ///
1119    /// @param[in] stack_frame_top, stack_frame_bottom
1120    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1121    ///     in which the expression ran.  A result whose address falls
1122    ///     inside this stack frame is dematerialized as a value
1123    ///     requiring rematerialization.
1124    ///
1125    /// @param[in] err
1126    ///     An Error to populate with any messages related to
1127    ///     (de)materializing the persistent variable.
1128    ///
1129    /// @return
1130    ///     True on success; false otherwise.
1131    //------------------------------------------------------------------
1132    bool
1133    DoMaterializeOnePersistentVariable (bool dematerialize,
1134                                        ExecutionContext &exe_ctx,
1135                                        lldb::ClangExpressionVariableSP &var_sp,
1136                                        lldb::addr_t addr,
1137                                        lldb::addr_t stack_frame_top,
1138                                        lldb::addr_t stack_frame_bottom,
1139                                        Error &err);
1140
1141    //------------------------------------------------------------------
1142    /// Actually do the task of materializing or dematerializing a
1143    /// variable.
1144    ///
1145    /// @param[in] dematerialize
1146    ///     True if the variable is to be dematerialized; false if it is to
1147    ///     be materialized.
1148    ///
1149    /// @param[in] exe_ctx
1150    ///     The execution context to use.
1151    ///
1152    /// @param[in] sym_ctx
1153    ///     The symbol context to use (for looking the variable up).
1154    ///
1155    /// @param[in] expr_var
1156    ///     The entity that the expression parser uses for the variable.
1157    ///     In case the variable needs to be copied into the target's
1158    ///     memory, this location is stored in the variable during
1159    ///     materialization and cleared when it is demateralized.
1160    ///
1161    /// @param[in] addr
1162    ///     The address at which to materialize the variable.
1163    ///
1164    /// @param[in] err
1165    ///     An Error to populate with any messages related to
1166    ///     (de)materializing the persistent variable.
1167    ///
1168    /// @return
1169    ///     True on success; false otherwise.
1170    //------------------------------------------------------------------
1171    bool
1172    DoMaterializeOneVariable (bool dematerialize,
1173                              ExecutionContext &exe_ctx,
1174                              const SymbolContext &sym_ctx,
1175                              lldb::ClangExpressionVariableSP &expr_var,
1176                              lldb::addr_t addr,
1177                              Error &err);
1178
1179    //------------------------------------------------------------------
1180    /// Actually do the task of materializing or dematerializing a
1181    /// register variable.
1182    ///
1183    /// @param[in] dematerialize
1184    ///     True if the variable is to be dematerialized; false if it is to
1185    ///     be materialized.
1186    ///
1187    /// @param[in] exe_ctx
1188    ///     The execution context to use.
1189    ///
1190    /// @param[in] reg_ctx
1191    ///     The register context to use.
1192    ///
1193    /// @param[in] reg_info
1194    ///     The information for the register to read/write.
1195    ///
1196    /// @param[in] addr
1197    ///     The address at which to materialize the variable.
1198    ///
1199    /// @param[in] err
1200    ///     An Error to populate with any messages related to
1201    ///     (de)materializing the persistent variable.
1202    ///
1203    /// @return
1204    ///     True on success; false otherwise.
1205    //------------------------------------------------------------------
1206    bool
1207    DoMaterializeOneRegister (bool dematerialize,
1208                              ExecutionContext &exe_ctx,
1209                              RegisterContext &reg_ctx,
1210                              const RegisterInfo &reg_info,
1211                              lldb::addr_t addr,
1212                              Error &err);
1213
1214    //------------------------------------------------------------------
1215    /// A wrapper for ClangASTContext::CopyType that sets a flag that
1216    /// indicates that we should not respond to queries during import.
1217    ///
1218    /// @param[in] dest_context
1219    ///     The target AST context, typically the parser's AST context.
1220    ///
1221    /// @param[in] source_context
1222    ///     The source AST context, typically the AST context of whatever
1223    ///     symbol file the type was found in.
1224    ///
1225    /// @param[in] clang_type
1226    ///     The source type.
1227    ///
1228    /// @return
1229    ///     The imported type.
1230    //------------------------------------------------------------------
1231    void *
1232    GuardedCopyType (clang::ASTContext *dest_context,
1233                     clang::ASTContext *source_context,
1234                     void *clang_type);
1235};
1236
1237} // namespace lldb_private
1238
1239#endif  // liblldb_ClangExpressionDeclMap_h_
1240