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