ClangExpressionDeclMap.h revision 03f64ca626d4dc0488e6acfe7023e4e376b4df30
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] decl
844    ///     If non-NULL 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    /// @return
853    ///     True on success; false otherwise.
854    //------------------------------------------------------------------
855    void
856    FindExternalVisibleDecls (NameSearchContext &context,
857                              lldb::ModuleSP module,
858                              ClangNamespaceDecl &namespace_decl,
859                              const ConstString &name);
860
861    //------------------------------------------------------------------
862    /// Given a stack frame, find a variable that matches the given name and
863    /// type.  We need this for expression re-use; we may not always get the
864    /// same lldb::Variable back, and we want the expression to work wherever
865    /// it can.  Returns the variable defined in the tightest scope.
866    ///
867    /// @param[in] frame
868    ///     The stack frame to use as a basis for finding the variable.
869    ///
870    /// @param[in] name
871    ///     The name as a plain C string.
872    ///
873    /// @param[in] type
874    ///     The required type for the variable.  This function may be called
875    ///     during parsing, in which case we don't know its type; hence the
876    ///     default.
877    ///
878    /// @return
879    ///     The LLDB Variable found, or NULL if none was found.
880    //------------------------------------------------------------------
881    lldb::VariableSP
882    FindVariableInScope (StackFrame &frame,
883                         const ConstString &name,
884                         TypeFromUser *type = NULL);
885
886    //------------------------------------------------------------------
887    /// Given a target, find a data symbol that has the given name.
888    ///
889    /// @param[in] target
890    ///     The target to use as the basis for the search.
891    ///
892    /// @param[in] name
893    ///     The name as a plain C string.
894    ///
895    /// @return
896    ///     The LLDB Symbol found, or NULL if none was found.
897    //---------------------------------------------------------
898    Symbol *
899    FindGlobalDataSymbol (Target &target,
900                          const ConstString &name);
901
902    //------------------------------------------------------------------
903    /// Given a target, find a variable that matches the given name and
904    /// type.
905    ///
906    /// @param[in] target
907    ///     The target to use as a basis for finding the variable.
908    ///
909    /// @param[in] name
910    ///     The name as a plain C string.
911    ///
912    /// @param[in] type
913    ///     The required type for the variable.  This function may be called
914    ///     during parsing, in which case we don't know its type; hence the
915    ///     default.
916    ///
917    /// @return
918    ///     The LLDB Variable found, or NULL if none was found.
919    //------------------------------------------------------------------
920    lldb::VariableSP
921    FindGlobalVariable (Target &target,
922                        const char *name,
923                        TypeFromUser *type = NULL);
924
925    //------------------------------------------------------------------
926    /// Get the value of a variable in a given execution context and return
927    /// the associated Types if needed.
928    ///
929    /// @param[in] exe_ctx
930    ///     The execution context to look for the variable in.
931    ///
932    /// @param[in] var
933    ///     The variable to evaluate.
934    ///
935    /// @param[in] parser_ast_context
936    ///     The AST context of the parser, to store the found type in.
937    ///
938    /// @param[out] found_type
939    ///     The type of the found value, as it was found in the user process.
940    ///     This is only useful when the variable is being inspected on behalf
941    ///     of the parser, hence the default.
942    ///
943    /// @param[out] parser_type
944    ///     The type of the found value, as it was copied into the parser's
945    ///     AST context.  This is only useful when the variable is being
946    ///     inspected on behalf of the parser, hence the default.
947    ///
948    /// @param[in] decl
949    ///     The Decl to be looked up.
950    ///
951    /// @return
952    ///     The LLDB Value for the variable.
953    //------------------------------------------------------------------
954    Value *
955    GetVariableValue (ExecutionContext &exe_ctx,
956                      lldb::VariableSP var,
957                      clang::ASTContext *parser_ast_context,
958                      TypeFromUser *found_type = NULL,
959                      TypeFromParser *parser_type = NULL);
960
961    //------------------------------------------------------------------
962    /// Use the NameSearchContext to generate a Decl for the given LLDB
963    /// Variable, and put it in the Tuple list.
964    ///
965    /// @param[in] context
966    ///     The NameSearchContext to use when constructing the Decl.
967    ///
968    /// @param[in] var
969    ///     The LLDB Variable that needs a Decl.
970    //------------------------------------------------------------------
971    void
972    AddOneVariable (NameSearchContext &context,
973                    lldb::VariableSP var);
974
975    //------------------------------------------------------------------
976    /// Use the NameSearchContext to generate a Decl for the given
977    /// persistent variable, and put it in the list of found entities.
978    ///
979    /// @param[in] context
980    ///     The NameSearchContext to use when constructing the Decl.
981    ///
982    /// @param[in] pvar
983    ///     The persistent variable that needs a Decl.
984    //------------------------------------------------------------------
985    void
986    AddOneVariable (NameSearchContext &context,
987                    lldb::ClangExpressionVariableSP &pvar_sp);
988
989    //------------------------------------------------------------------
990    /// Use the NameSearchContext to generate a Decl for the given LLDB
991    /// symbol (treated as a variable), and put it in the list of found
992    /// entities.
993    ///
994    /// @param[in] context
995    ///     The NameSearchContext to use when constructing the Decl.
996    ///
997    /// @param[in] var
998    ///     The LLDB Variable that needs a Decl.
999    //------------------------------------------------------------------
1000    void
1001    AddOneGenericVariable (NameSearchContext &context,
1002                           Symbol &symbol);
1003
1004    //------------------------------------------------------------------
1005    /// Use the NameSearchContext to generate a Decl for the given
1006    /// function.  (Functions are not placed in the Tuple list.)  Can
1007    /// handle both fully typed functions and generic functions.
1008    ///
1009    /// @param[in] context
1010    ///     The NameSearchContext to use when constructing the Decl.
1011    ///
1012    /// @param[in] fun
1013    ///     The Function that needs to be created.  If non-NULL, this is
1014    ///     a fully-typed function.
1015    ///
1016    /// @param[in] sym
1017    ///     The Symbol that corresponds to a function that needs to be
1018    ///     created with generic type (unitptr_t foo(...)).
1019    //------------------------------------------------------------------
1020    void
1021    AddOneFunction (NameSearchContext &context,
1022                    Function *fun,
1023                    Symbol *sym);
1024
1025    //------------------------------------------------------------------
1026    /// Use the NameSearchContext to generate a Decl for the given
1027    /// register.
1028    ///
1029    /// @param[in] context
1030    ///     The NameSearchContext to use when constructing the Decl.
1031    ///
1032    /// @param[in] reg_info
1033    ///     The information corresponding to that register.
1034    //------------------------------------------------------------------
1035    void
1036    AddOneRegister (NameSearchContext &context,
1037                    const RegisterInfo *reg_info);
1038
1039    //------------------------------------------------------------------
1040    /// Use the NameSearchContext to generate a Decl for the given
1041    /// type.  (Types are not placed in the Tuple list.)
1042    ///
1043    /// @param[in] context
1044    ///     The NameSearchContext to use when constructing the Decl.
1045    ///
1046    /// @param[in] type
1047    ///     The type that needs to be created.
1048    ///
1049    /// @param[in] add_method
1050    ///     True if a method with signature void $__lldb_expr(void*)
1051    ///     should be added to the C++ class type passed in
1052    //------------------------------------------------------------------
1053    void
1054    AddOneType (NameSearchContext &context,
1055                TypeFromUser &type,
1056                bool add_method = false);
1057
1058    //------------------------------------------------------------------
1059    /// Actually do the task of materializing or dematerializing the struct.
1060    /// Since both tasks are very similar, although ClangExpressionDeclMap
1061    /// exposes two functions to the outside, both call DoMaterialize.
1062    ///
1063    /// @param[in] dematerialize
1064    ///     True if the struct is to be dematerialized; false if it is to
1065    ///     be materialized.
1066    ///
1067    /// @param[in] exe_ctx
1068    ///     The execution context to use.
1069    ///
1070    /// @param[in] stack_frame_top, stack_frame_bottom
1071    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1072    ///     in which the expression ran.  A result whose address falls
1073    ///     inside this stack frame is dematerialized as a value
1074    ///     requiring rematerialization.
1075    ///
1076    /// @param[out] result
1077    ///     If the struct is being dematerialized, a pointer into which the
1078    ///     location of the result persistent variable is placed.  If not,
1079    ///     NULL.
1080    ///
1081    /// @param[in] err
1082    ///     An Error to populate with any messages related to
1083    ///     (de)materializing the struct.
1084    ///
1085    /// @return
1086    ///     True on success; false otherwise.
1087    //------------------------------------------------------------------
1088    bool
1089    DoMaterialize (bool dematerialize,
1090                   ExecutionContext &exe_ctx,
1091                   lldb::addr_t stack_frame_top,
1092                   lldb::addr_t stack_frame_bottom,
1093                   lldb::ClangExpressionVariableSP *result_sp_ptr,
1094                   Error &err);
1095
1096    //------------------------------------------------------------------
1097    /// Clean up the state required to dematerialize the variable.
1098    //------------------------------------------------------------------
1099    void
1100    DidDematerialize ();
1101
1102    //------------------------------------------------------------------
1103    /// Actually do the task of materializing or dematerializing a persistent
1104    /// variable.
1105    ///
1106    /// @param[in] dematerialize
1107    ///     True if the variable is to be dematerialized; false if it is to
1108    ///     be materialized.
1109    ///
1110    /// @param[in] exe_ctx
1111    ///     The execution context to use.
1112    ///
1113    /// @param[in] var_sp
1114    ///     The persistent variable to materialize
1115    ///
1116    /// @param[in] addr
1117    ///     The address at which to materialize the variable.
1118    ///
1119    /// @param[in] stack_frame_top, stack_frame_bottom
1120    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1121    ///     in which the expression ran.  A result whose address falls
1122    ///     inside this stack frame is dematerialized as a value
1123    ///     requiring rematerialization.
1124    ///
1125    /// @param[in] err
1126    ///     An Error to populate with any messages related to
1127    ///     (de)materializing the persistent variable.
1128    ///
1129    /// @return
1130    ///     True on success; false otherwise.
1131    //------------------------------------------------------------------
1132    bool
1133    DoMaterializeOnePersistentVariable (bool dematerialize,
1134                                        ExecutionContext &exe_ctx,
1135                                        lldb::ClangExpressionVariableSP &var_sp,
1136                                        lldb::addr_t addr,
1137                                        lldb::addr_t stack_frame_top,
1138                                        lldb::addr_t stack_frame_bottom,
1139                                        Error &err);
1140
1141    //------------------------------------------------------------------
1142    /// Actually do the task of materializing or dematerializing a
1143    /// variable.
1144    ///
1145    /// @param[in] dematerialize
1146    ///     True if the variable is to be dematerialized; false if it is to
1147    ///     be materialized.
1148    ///
1149    /// @param[in] exe_ctx
1150    ///     The execution context to use.
1151    ///
1152    /// @param[in] sym_ctx
1153    ///     The symbol context to use (for looking the variable up).
1154    ///
1155    /// @param[in] expr_var
1156    ///     The entity that the expression parser uses for the variable.
1157    ///     In case the variable needs to be copied into the target's
1158    ///     memory, this location is stored in the variable during
1159    ///     materialization and cleared when it is demateralized.
1160    ///
1161    /// @param[in] addr
1162    ///     The address at which to materialize the variable.
1163    ///
1164    /// @param[in] err
1165    ///     An Error to populate with any messages related to
1166    ///     (de)materializing the persistent variable.
1167    ///
1168    /// @return
1169    ///     True on success; false otherwise.
1170    //------------------------------------------------------------------
1171    bool
1172    DoMaterializeOneVariable (bool dematerialize,
1173                              ExecutionContext &exe_ctx,
1174                              const SymbolContext &sym_ctx,
1175                              lldb::ClangExpressionVariableSP &expr_var,
1176                              lldb::addr_t addr,
1177                              Error &err);
1178
1179    //------------------------------------------------------------------
1180    /// Actually do the task of materializing or dematerializing a
1181    /// register variable.
1182    ///
1183    /// @param[in] dematerialize
1184    ///     True if the variable is to be dematerialized; false if it is to
1185    ///     be materialized.
1186    ///
1187    /// @param[in] exe_ctx
1188    ///     The execution context to use.
1189    ///
1190    /// @param[in] reg_ctx
1191    ///     The register context to use.
1192    ///
1193    /// @param[in] reg_info
1194    ///     The information for the register to read/write.
1195    ///
1196    /// @param[in] addr
1197    ///     The address at which to materialize the variable.
1198    ///
1199    /// @param[in] err
1200    ///     An Error to populate with any messages related to
1201    ///     (de)materializing the persistent variable.
1202    ///
1203    /// @return
1204    ///     True on success; false otherwise.
1205    //------------------------------------------------------------------
1206    bool
1207    DoMaterializeOneRegister (bool dematerialize,
1208                              ExecutionContext &exe_ctx,
1209                              RegisterContext &reg_ctx,
1210                              const RegisterInfo &reg_info,
1211                              lldb::addr_t addr,
1212                              Error &err);
1213
1214    //------------------------------------------------------------------
1215    /// A wrapper for ClangASTContext::CopyType that sets a flag that
1216    /// indicates that we should not respond to queries during import.
1217    ///
1218    /// @param[in] dest_context
1219    ///     The target AST context, typically the parser's AST context.
1220    ///
1221    /// @param[in] source_context
1222    ///     The source AST context, typically the AST context of whatever
1223    ///     symbol file the type was found in.
1224    ///
1225    /// @param[in] clang_type
1226    ///     The source type.
1227    ///
1228    /// @return
1229    ///     The imported type.
1230    //------------------------------------------------------------------
1231    void *
1232    GuardedCopyType (clang::ASTContext *dest_context,
1233                     clang::ASTContext *source_context,
1234                     void *clang_type);
1235};
1236
1237} // namespace lldb_private
1238
1239#endif  // liblldb_ClangExpressionDeclMap_h_
1240