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