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