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