ClangExpressionDeclMap.h revision 557ccd6b47c5d4b3736e704e7f1e887a7fff6549
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] Promote an unknown address to a
385    ///     LoadAddress or FileAddress depending on the presence of a
386    ///     process.
387    ///
388    /// @param[in] addr
389    ///     The address to promote.
390    ///
391    /// @return
392    ///     The wrapped entity.
393    //------------------------------------------------------------------
394    lldb_private::Value WrapBareAddress (lldb::addr_t addr);
395
396    //------------------------------------------------------------------
397    /// [Used by IRInterpreter] Write to the target.
398    ///
399    /// @param[in] value
400    ///     The address to write to.
401    ///
402    /// @param[in] addr
403    ///     The address of the data buffer to read from.
404    ///
405    /// @param[in] length
406    ///     The amount of data to write, in bytes.
407    ///
408    /// @return
409    ///     True if the write could be performed; false otherwise.
410    //------------------------------------------------------------------
411    bool
412    WriteTarget (lldb_private::Value &value,
413                 const uint8_t *data,
414                 size_t length);
415
416    //------------------------------------------------------------------
417    /// [Used by IRInterpreter] Read from the target.
418    ///
419    /// @param[in] data
420    ///     The address of the data buffer to write to.
421    ///
422    /// @param[in] value
423    ///     The address to read from.
424    ///
425    /// @param[in] length
426    ///     The amount of data to read, in bytes.
427    ///
428    /// @return
429    ///     True if the read could be performed; false otherwise.
430    //------------------------------------------------------------------
431    bool
432    ReadTarget (uint8_t *data,
433                lldb_private::Value &value,
434                size_t length);
435
436    //------------------------------------------------------------------
437    /// [Used by IRInterpreter] Get the Value for a NamedDecl.
438    ///
439    /// @param[in] decl
440    ///     The Decl whose value is to be found.
441    ///
442    /// @return
443    ///     The value, or NULL.
444    //------------------------------------------------------------------
445    lldb_private::Value
446    LookupDecl (clang::NamedDecl *decl);
447
448    //------------------------------------------------------------------
449    /// [Used by IRInterpreter] Returns true if the result is a
450    ///   reference to data in the target, meaning it must be
451    ///   dereferenced once more to get its data.
452    ///
453    /// @param[in] name
454    ///     The name of the result.
455    ///
456    /// @return
457    ///     True if the result is a reference; false otherwise (or on
458    ///     error).
459    //------------------------------------------------------------------
460    bool
461    ResultIsReference (const ConstString &name);
462
463    //------------------------------------------------------------------
464    /// [Used by IRInterpreter] Find the result persistent variable,
465    ///   propagate the given value to it, and return it.
466    ///
467    /// @param[out] valobj
468    ///     Set to the complete object.
469    ///
470    /// @param[in] value
471    ///     A value indicating the location of the value's contents.
472    ///
473    /// @param[in] name
474    ///     The name of the result.
475    ///
476    /// @param[in] type
477    ///     The type of the data.
478    ///
479    /// @param[in] transient
480    ///     True if the data should be treated as disappearing after the
481    ///     expression completes.  In that case, it gets no live data.
482    ///
483    /// @param[in] maybe_make_load
484    ///     True if the value is a file address but should be potentially
485    ///     upgraded to a load address if a target is presence.
486    ///
487    /// @return
488    ///     True on success; false otherwise.
489    //------------------------------------------------------------------
490    bool
491    CompleteResultVariable (lldb::ClangExpressionVariableSP &valobj,
492                            lldb_private::Value &value,
493                            const ConstString &name,
494                            lldb_private::TypeFromParser type,
495                            bool transient,
496                            bool maybe_make_load);
497
498    //------------------------------------------------------------------
499    /// [Used by CommandObjectExpression] Materialize the entire struct
500    /// at a given address, which should be aligned as specified by
501    /// GetStructInfo().
502    ///
503    /// @param[in] exe_ctx
504    ///     The execution context at which to dump the struct.
505    ///
506    /// @param[in] struct_address
507    ///     The address at which the struct should be written.
508    ///
509    /// @param[in] error
510    ///     An Error to populate with any messages related to
511    ///     materializing the struct.
512    ///
513    /// @return
514    ///     True on success; false otherwise.
515    //------------------------------------------------------------------
516    bool
517    Materialize (ExecutionContext &exe_ctx,
518                 lldb::addr_t &struct_address,
519                 Error &error);
520
521    //------------------------------------------------------------------
522    /// [Used by CommandObjectExpression] Get the "this" pointer
523    /// from a given execution context.
524    ///
525    /// @param[out] object_ptr
526    ///     The this pointer.
527    ///
528    /// @param[in] object_name
529    ///     The name of the object pointer -- "this," "self," or similar
530    ///     depending on language
531    ///
532    /// @param[in] exe_ctx
533    ///     The execution context at which to dump the struct.
534    ///
535    /// @param[in] error
536    ///     An Error to populate with any messages related to
537    ///     finding the "this" pointer.
538    ///
539    /// @param[in] suppress_type_check
540    ///     True if the type is not needed.
541    ///
542    /// @return
543    ///     True on success; false otherwise.
544    //------------------------------------------------------------------
545    bool
546    GetObjectPointer (lldb::addr_t &object_ptr,
547                      ConstString &object_name,
548                      ExecutionContext &exe_ctx,
549                      Error &error,
550                      bool suppress_type_check = false);
551
552    //------------------------------------------------------------------
553    /// [Used by CommandObjectExpression] Pretty-print a materialized
554    /// struct, which must have been materialized by Materialize(),
555    /// byte for byte on a given stream.
556    ///
557    /// @param[in] exe_ctx
558    ///     The execution context from which to read the struct.
559    ///
560    /// @param[in] s
561    ///     The stream on which to write the pretty-printed output.
562    ///
563    /// @param[in] error
564    ///     An Error to populate with any messages related to
565    ///     pretty-printing the struct.
566    ///
567    /// @return
568    ///     True on success; false otherwise.
569    //------------------------------------------------------------------
570    bool
571    DumpMaterializedStruct (ExecutionContext &exe_ctx,
572                            Stream &s,
573                            Error &error);
574
575    //------------------------------------------------------------------
576    /// [Used by CommandObjectExpression] Deaterialize the entire struct.
577    ///
578    /// @param[in] exe_ctx
579    ///     The execution context from which to read the struct.
580    ///
581    /// @param[out] result
582    ///     A ClangExpressionVariable containing the result of the
583    ///     expression, for potential re-use.
584    ///
585    /// @param[in] stack_frame_top, stack_frame_bottom
586    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
587    ///     in which the expression ran.  A result whose address falls
588    ///     inside this stack frame is dematerialized as a value
589    ///     requiring rematerialization.
590    ///
591    /// @param[in] error
592    ///     An Error to populate with any messages related to
593    ///     dematerializing the struct.
594    ///
595    /// @return
596    ///     True on success; false otherwise.
597    //------------------------------------------------------------------
598    bool
599    Dematerialize (ExecutionContext &exe_ctx,
600                   lldb::ClangExpressionVariableSP &result_sp,
601                   lldb::addr_t stack_frame_top,
602                   lldb::addr_t stack_frame_bottom,
603                   Error &error);
604
605    //------------------------------------------------------------------
606    /// [Used by ClangASTSource] Find all entities matching a given name,
607    /// using a NameSearchContext to make Decls for them.
608    ///
609    /// @param[in] context
610    ///     The NameSearchContext that can construct Decls for this name.
611    ///
612    /// @param[in] name
613    ///     The name as a plain C string.  The NameSearchContext contains
614    ///     a DeclarationName for the name so at first the name may seem
615    ///     redundant, but ClangExpressionDeclMap operates in RTTI land so
616    ///     it can't access DeclarationName.
617    ///
618    /// @return
619    ///     True on success; false otherwise.
620    //------------------------------------------------------------------
621    void
622    FindExternalVisibleDecls (NameSearchContext &context,
623                              const ConstString &name);
624
625    //------------------------------------------------------------------
626    /// [Used by ClangASTSource] Find all Decls in a context that match
627    /// a given criterion.
628    ///
629    /// @param[in] decl_context
630    ///     The DeclContext to search.
631    ///
632    /// @param[in] predicate
633    ///     Returns True if a DeclKind is desired; False if not.
634    ///
635    /// @param[in] decls
636    ///     A list to add all found Decls that have a desired DeclKind
637    ///     into.
638    //------------------------------------------------------------------
639    clang::ExternalLoadResult
640    FindExternalLexicalDecls (const clang::DeclContext *decl_context,
641                              bool (*predicate)(clang::Decl::Kind),
642                              llvm::SmallVectorImpl<clang::Decl*> &decls);
643
644    //------------------------------------------------------------------
645    /// [Used by ClangASTSource] Complete the definition of a TagDecl.
646    ///
647    /// @param[in] tag_decl
648    ///     The TagDecl to be completed.
649    //------------------------------------------------------------------
650    void
651    CompleteTagDecl (clang::TagDecl *tag_decl);
652
653    //------------------------------------------------------------------
654    /// [Used by ClangASTSource] Complete the definition of an
655    /// ObjCInterfaceDecl.
656    ///
657    /// @param[in] tag_decl
658    ///     The ObjCInterfaceDecl to be completed.
659    //------------------------------------------------------------------
660    void
661    CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl);
662
663    //------------------------------------------------------------------
664    /// [Used by ClangASTSource] Report whether a $__lldb variable has
665    /// been searched for yet.  This is the trigger for beginning to
666    /// actually look for externally-defined names.  (Names that come
667    /// before this are typically the names of built-ins that don't need
668    /// to be looked up.)
669    ///
670    /// @return
671    ///     True if a $__lldb variable has been found.
672    //------------------------------------------------------------------
673    bool
674    GetLookupsEnabled () const
675    {
676        assert(m_parser_vars.get());
677        return m_parser_vars->m_enable_lookups;
678    }
679
680    bool
681    GetImportInProgress () const
682    {
683        if (m_parser_vars.get())
684            return m_parser_vars->m_ignore_lookups;
685        return false;
686    }
687
688    //------------------------------------------------------------------
689    /// [Used by ClangASTSource] Indicate that a $__lldb variable has
690    /// been found.
691    //------------------------------------------------------------------
692    void
693    SetLookupsEnabled ()
694    {
695        assert(m_parser_vars.get());
696        m_parser_vars->m_enable_lookups = true;
697    }
698
699private:
700    ClangExpressionVariableList    m_found_entities;           ///< All entities that were looked up for the parser.
701    ClangExpressionVariableList    m_struct_members;           ///< All entities that need to be placed in the struct.
702    bool                           m_keep_result_in_memory;    ///< True if result persistent variables generated by this expression should stay in memory.
703
704    //----------------------------------------------------------------------
705    /// The following values should not live beyond parsing
706    //----------------------------------------------------------------------
707    class ParserVars
708    {
709    public:
710        ParserVars() :
711            m_exe_ctx(NULL),
712            m_sym_ctx(),
713            m_persistent_vars(NULL),
714            m_enable_lookups(false),
715            m_ignore_lookups(false)
716        {
717        }
718
719        Target *
720        GetTarget()
721        {
722            if (m_exe_ctx && m_exe_ctx->GetTargetPtr())
723                return m_exe_ctx->GetTargetPtr();
724            else if (m_sym_ctx.target_sp)
725                m_sym_ctx.target_sp.get();
726            return NULL;
727        }
728
729        ClangASTImporter *GetASTImporter (clang::ASTContext *ast_context)
730        {
731            if (!m_ast_importer.get())
732                m_ast_importer.reset(new ClangASTImporter(ast_context));
733
734            if (m_ast_importer->TargetASTContext() != ast_context)
735                return NULL;
736
737            return m_ast_importer.get();
738        }
739
740        ExecutionContext           *m_exe_ctx;          ///< The execution context to use when parsing.
741        SymbolContext               m_sym_ctx;          ///< The symbol context to use in finding variables and types.
742        ClangPersistentVariables   *m_persistent_vars;  ///< The persistent variables for the process.
743        bool                        m_enable_lookups;   ///< Set to true during parsing if we have found the first "$__lldb" name.
744        bool                        m_ignore_lookups;   ///< True during an import when we should be ignoring type lookups.
745        std::auto_ptr<ClangASTImporter> m_ast_importer; ///< The importer used to import types on the parser's behalf.
746        TargetInfo                  m_target_info;      ///< Basic information about the target.
747    private:
748        DISALLOW_COPY_AND_ASSIGN (ParserVars);
749    };
750
751    std::auto_ptr<ParserVars> m_parser_vars;
752
753    //----------------------------------------------------------------------
754    /// Activate parser-specific variables
755    //----------------------------------------------------------------------
756    void
757    EnableParserVars()
758    {
759        if (!m_parser_vars.get())
760            m_parser_vars.reset(new ParserVars);
761    }
762
763    //----------------------------------------------------------------------
764    /// Deallocate parser-specific variables
765    //----------------------------------------------------------------------
766    void
767    DisableParserVars()
768    {
769        m_parser_vars.reset();
770    }
771
772    //----------------------------------------------------------------------
773    /// The following values contain layout information for the materialized
774    /// struct, but are not specific to a single materialization
775    //----------------------------------------------------------------------
776    struct StructVars {
777        StructVars() :
778            m_struct_alignment(0),
779            m_struct_size(0),
780            m_struct_laid_out(false),
781            m_result_name(),
782            m_object_pointer_type(NULL, NULL)
783        {
784        }
785
786        off_t                       m_struct_alignment;         ///< The alignment of the struct in bytes.
787        size_t                      m_struct_size;              ///< The size of the struct in bytes.
788        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).
789        ConstString                 m_result_name;              ///< The name of the result variable ($1, for example)
790        TypeFromUser                m_object_pointer_type;      ///< The type of the "this" variable, if one exists
791    };
792
793    std::auto_ptr<StructVars> m_struct_vars;
794
795    //----------------------------------------------------------------------
796    /// Activate struct variables
797    //----------------------------------------------------------------------
798    void
799    EnableStructVars()
800    {
801        if (!m_struct_vars.get())
802            m_struct_vars.reset(new struct StructVars);
803    }
804
805    //----------------------------------------------------------------------
806    /// Deallocate struct variables
807    //----------------------------------------------------------------------
808    void
809    DisableStructVars()
810    {
811        m_struct_vars.reset();
812    }
813
814    //----------------------------------------------------------------------
815    /// The following values refer to a specific materialization of the
816    /// structure in a process
817    //----------------------------------------------------------------------
818    struct MaterialVars {
819        MaterialVars() :
820            m_allocated_area(0),
821            m_materialized_location(0)
822        {
823        }
824
825        Process                    *m_process;                  ///< The process that the struct is materialized into.
826        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.
827        lldb::addr_t                m_materialized_location;    ///< The address at which the struct is placed.  Falls inside the allocated area.
828    };
829
830    std::auto_ptr<MaterialVars> m_material_vars;
831
832    //----------------------------------------------------------------------
833    /// Activate materialization-specific variables
834    //----------------------------------------------------------------------
835    void
836    EnableMaterialVars()
837    {
838        if (!m_material_vars.get())
839            m_material_vars.reset(new struct MaterialVars);
840    }
841
842    //----------------------------------------------------------------------
843    /// Deallocate materialization-specific variables
844    //----------------------------------------------------------------------
845    void
846    DisableMaterialVars()
847    {
848        m_material_vars.reset();
849    }
850
851    //------------------------------------------------------------------
852    /// [Used by ClangASTSource] Find all entities matching a given name,
853    /// using a NameSearchContext to make Decls for them.
854    ///
855    /// @param[in] context
856    ///     The NameSearchContext that can construct Decls for this name.
857    ///
858    /// @param[in] module
859    ///     If non-NULL, the module to query.
860    ///
861    /// @param[in] namespace_decl
862    ///     If valid and module is non-NULL, the parent namespace.
863    ///
864    /// @param[in] name
865    ///     The name as a plain C string.  The NameSearchContext contains
866    ///     a DeclarationName for the name so at first the name may seem
867    ///     redundant, but ClangExpressionDeclMap operates in RTTI land so
868    ///     it can't access DeclarationName.
869    ///
870    /// @param[in] current_id
871    ///     The ID for the current FindExternalVisibleDecls invocation,
872    ///     for logging purposes.
873    ///
874    /// @return
875    ///     True on success; false otherwise.
876    //------------------------------------------------------------------
877    void
878    FindExternalVisibleDecls (NameSearchContext &context,
879                              lldb::ModuleSP module,
880                              ClangNamespaceDecl &namespace_decl,
881                              const ConstString &name,
882                              unsigned int current_id);
883
884    //------------------------------------------------------------------
885    /// Given a stack frame, find a variable that matches the given name and
886    /// type.  We need this for expression re-use; we may not always get the
887    /// same lldb::Variable back, and we want the expression to work wherever
888    /// it can.  Returns the variable defined in the tightest scope.
889    ///
890    /// @param[in] frame
891    ///     The stack frame to use as a basis for finding the variable.
892    ///
893    /// @param[in] name
894    ///     The name as a plain C string.
895    ///
896    /// @param[in] type
897    ///     The required type for the variable.  This function may be called
898    ///     during parsing, in which case we don't know its type; hence the
899    ///     default.
900    ///
901    /// @return
902    ///     The LLDB Variable found, or NULL if none was found.
903    //------------------------------------------------------------------
904    lldb::VariableSP
905    FindVariableInScope (StackFrame &frame,
906                         const ConstString &name,
907                         TypeFromUser *type = NULL);
908
909    //------------------------------------------------------------------
910    /// Given a target, find a data symbol that has the given name.
911    ///
912    /// @param[in] target
913    ///     The target to use as the basis for the search.
914    ///
915    /// @param[in] name
916    ///     The name as a plain C string.
917    ///
918    /// @return
919    ///     The LLDB Symbol found, or NULL if none was found.
920    //---------------------------------------------------------
921    Symbol *
922    FindGlobalDataSymbol (Target &target,
923                          const ConstString &name);
924
925    //------------------------------------------------------------------
926    /// Given a target, find a variable that matches the given name and
927    /// type.
928    ///
929    /// @param[in] target
930    ///     The target to use as a basis for finding the variable.
931    ///
932    /// @param[in] module
933    ///     If non-NULL, the module to search.
934    ///
935    /// @param[in] name
936    ///     The name as a plain C string.
937    ///
938    /// @param[in] namespace_decl
939    ///     If non-NULL and module is non-NULL, the parent namespace.
940    ///
941    /// @param[in] type
942    ///     The required type for the variable.  This function may be called
943    ///     during parsing, in which case we don't know its type; hence the
944    ///     default.
945    ///
946    /// @return
947    ///     The LLDB Variable found, or NULL if none was found.
948    //------------------------------------------------------------------
949    lldb::VariableSP
950    FindGlobalVariable (Target &target,
951                        lldb::ModuleSP &module,
952                        const ConstString &name,
953                        ClangNamespaceDecl *namespace_decl,
954                        TypeFromUser *type = NULL);
955
956    //------------------------------------------------------------------
957    /// Get the value of a variable in a given execution context and return
958    /// the associated Types if needed.
959    ///
960    /// @param[in] exe_ctx
961    ///     The execution context to look for the variable in.
962    ///
963    /// @param[in] var
964    ///     The variable to evaluate.
965    ///
966    /// @param[in] parser_ast_context
967    ///     The AST context of the parser, to store the found type in.
968    ///
969    /// @param[out] found_type
970    ///     The type of the found value, as it was found in the user process.
971    ///     This is only useful when the variable is being inspected on behalf
972    ///     of the parser, hence the default.
973    ///
974    /// @param[out] parser_type
975    ///     The type of the found value, as it was copied into the parser's
976    ///     AST context.  This is only useful when the variable is being
977    ///     inspected on behalf of the parser, hence the default.
978    ///
979    /// @param[in] decl
980    ///     The Decl to be looked up.
981    ///
982    /// @return
983    ///     The LLDB Value for the variable.
984    //------------------------------------------------------------------
985    Value *
986    GetVariableValue (ExecutionContext &exe_ctx,
987                      lldb::VariableSP &var,
988                      clang::ASTContext *parser_ast_context,
989                      TypeFromUser *found_type = NULL,
990                      TypeFromParser *parser_type = NULL);
991
992    //------------------------------------------------------------------
993    /// Use the NameSearchContext to generate a Decl for the given LLDB
994    /// Variable, and put it in the Tuple list.
995    ///
996    /// @param[in] context
997    ///     The NameSearchContext to use when constructing the Decl.
998    ///
999    /// @param[in] var
1000    ///     The LLDB Variable that needs a Decl.
1001    //------------------------------------------------------------------
1002    void
1003    AddOneVariable (NameSearchContext &context,
1004                    lldb::VariableSP var,
1005                    unsigned int current_id);
1006
1007    //------------------------------------------------------------------
1008    /// Use the NameSearchContext to generate a Decl for the given
1009    /// persistent variable, and put it in the list of found entities.
1010    ///
1011    /// @param[in] context
1012    ///     The NameSearchContext to use when constructing the Decl.
1013    ///
1014    /// @param[in] pvar
1015    ///     The persistent variable that needs a Decl.
1016    ///
1017    /// @param[in] current_id
1018    ///     The ID of the current invocation of FindExternalVisibleDecls
1019    ///     for logging purposes.
1020    //------------------------------------------------------------------
1021    void
1022    AddOneVariable (NameSearchContext &context,
1023                    lldb::ClangExpressionVariableSP &pvar_sp,
1024                    unsigned int current_id);
1025
1026    //------------------------------------------------------------------
1027    /// Use the NameSearchContext to generate a Decl for the given LLDB
1028    /// symbol (treated as a variable), and put it in the list of found
1029    /// entities.
1030    ///
1031    /// @param[in] context
1032    ///     The NameSearchContext to use when constructing the Decl.
1033    ///
1034    /// @param[in] var
1035    ///     The LLDB Variable that needs a Decl.
1036    //------------------------------------------------------------------
1037    void
1038    AddOneGenericVariable (NameSearchContext &context,
1039                           Symbol &symbol,
1040                           unsigned int current_id);
1041
1042    //------------------------------------------------------------------
1043    /// Use the NameSearchContext to generate a Decl for the given
1044    /// function.  (Functions are not placed in the Tuple list.)  Can
1045    /// handle both fully typed functions and generic functions.
1046    ///
1047    /// @param[in] context
1048    ///     The NameSearchContext to use when constructing the Decl.
1049    ///
1050    /// @param[in] fun
1051    ///     The Function that needs to be created.  If non-NULL, this is
1052    ///     a fully-typed function.
1053    ///
1054    /// @param[in] sym
1055    ///     The Symbol that corresponds to a function that needs to be
1056    ///     created with generic type (unitptr_t foo(...)).
1057    //------------------------------------------------------------------
1058    void
1059    AddOneFunction (NameSearchContext &context,
1060                    Function *fun,
1061                    Symbol *sym,
1062                    unsigned int current_id);
1063
1064    //------------------------------------------------------------------
1065    /// Use the NameSearchContext to generate a Decl for the given
1066    /// register.
1067    ///
1068    /// @param[in] context
1069    ///     The NameSearchContext to use when constructing the Decl.
1070    ///
1071    /// @param[in] reg_info
1072    ///     The information corresponding to that register.
1073    //------------------------------------------------------------------
1074    void
1075    AddOneRegister (NameSearchContext &context,
1076                    const RegisterInfo *reg_info,
1077                    unsigned int current_id);
1078
1079    //------------------------------------------------------------------
1080    /// Use the NameSearchContext to generate a Decl for the given
1081    /// type.  (Types are not placed in the Tuple list.)
1082    ///
1083    /// @param[in] context
1084    ///     The NameSearchContext to use when constructing the Decl.
1085    ///
1086    /// @param[in] type
1087    ///     The type that needs to be created.
1088    ///
1089    /// @param[in] add_method
1090    ///     True if a method with signature void $__lldb_expr(void*)
1091    ///     should be added to the C++ class type passed in
1092    //------------------------------------------------------------------
1093    void
1094    AddOneType (NameSearchContext &context,
1095                TypeFromUser &type,
1096                unsigned int current_id,
1097                bool add_method);
1098
1099    //------------------------------------------------------------------
1100    /// Actually do the task of materializing or dematerializing the struct.
1101    /// Since both tasks are very similar, although ClangExpressionDeclMap
1102    /// exposes two functions to the outside, both call DoMaterialize.
1103    ///
1104    /// @param[in] dematerialize
1105    ///     True if the struct is to be dematerialized; false if it is to
1106    ///     be materialized.
1107    ///
1108    /// @param[in] exe_ctx
1109    ///     The execution context to use.
1110    ///
1111    /// @param[in] stack_frame_top, stack_frame_bottom
1112    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1113    ///     in which the expression ran.  A result whose address falls
1114    ///     inside this stack frame is dematerialized as a value
1115    ///     requiring rematerialization.
1116    ///
1117    /// @param[out] result
1118    ///     If the struct is being dematerialized, a pointer into which the
1119    ///     location of the result persistent variable is placed.  If not,
1120    ///     NULL.
1121    ///
1122    /// @param[in] err
1123    ///     An Error to populate with any messages related to
1124    ///     (de)materializing the struct.
1125    ///
1126    /// @return
1127    ///     True on success; false otherwise.
1128    //------------------------------------------------------------------
1129    bool
1130    DoMaterialize (bool dematerialize,
1131                   ExecutionContext &exe_ctx,
1132                   lldb::addr_t stack_frame_top,
1133                   lldb::addr_t stack_frame_bottom,
1134                   lldb::ClangExpressionVariableSP *result_sp_ptr,
1135                   Error &err);
1136
1137    //------------------------------------------------------------------
1138    /// Clean up the state required to dematerialize the variable.
1139    //------------------------------------------------------------------
1140    void
1141    DidDematerialize ();
1142
1143    //------------------------------------------------------------------
1144    /// Actually do the task of materializing or dematerializing a persistent
1145    /// variable.
1146    ///
1147    /// @param[in] dematerialize
1148    ///     True if the variable is to be dematerialized; false if it is to
1149    ///     be materialized.
1150    ///
1151    /// @param[in] exe_ctx
1152    ///     The execution context to use.
1153    ///
1154    /// @param[in] var_sp
1155    ///     The persistent variable to materialize
1156    ///
1157    /// @param[in] addr
1158    ///     The address at which to materialize the variable.
1159    ///
1160    /// @param[in] stack_frame_top, stack_frame_bottom
1161    ///     If not LLDB_INVALID_ADDRESS, the bounds for the stack frame
1162    ///     in which the expression ran.  A result whose address falls
1163    ///     inside this stack frame is dematerialized as a value
1164    ///     requiring rematerialization.
1165    ///
1166    /// @param[in] err
1167    ///     An Error to populate with any messages related to
1168    ///     (de)materializing the persistent variable.
1169    ///
1170    /// @return
1171    ///     True on success; false otherwise.
1172    //------------------------------------------------------------------
1173    bool
1174    DoMaterializeOnePersistentVariable (bool dematerialize,
1175                                        ExecutionContext &exe_ctx,
1176                                        lldb::ClangExpressionVariableSP &var_sp,
1177                                        lldb::addr_t addr,
1178                                        lldb::addr_t stack_frame_top,
1179                                        lldb::addr_t stack_frame_bottom,
1180                                        Error &err);
1181
1182    //------------------------------------------------------------------
1183    /// Actually do the task of materializing or dematerializing a
1184    /// variable.
1185    ///
1186    /// @param[in] dematerialize
1187    ///     True if the variable is to be dematerialized; false if it is to
1188    ///     be materialized.
1189    ///
1190    /// @param[in] exe_ctx
1191    ///     The execution context to use.
1192    ///
1193    /// @param[in] sym_ctx
1194    ///     The symbol context to use (for looking the variable up).
1195    ///
1196    /// @param[in] expr_var
1197    ///     The entity that the expression parser uses for the variable.
1198    ///     In case the variable needs to be copied into the target's
1199    ///     memory, this location is stored in the variable during
1200    ///     materialization and cleared when it is demateralized.
1201    ///
1202    /// @param[in] addr
1203    ///     The address at which to materialize the variable.
1204    ///
1205    /// @param[in] err
1206    ///     An Error to populate with any messages related to
1207    ///     (de)materializing the persistent variable.
1208    ///
1209    /// @return
1210    ///     True on success; false otherwise.
1211    //------------------------------------------------------------------
1212    bool
1213    DoMaterializeOneVariable (bool dematerialize,
1214                              ExecutionContext &exe_ctx,
1215                              const SymbolContext &sym_ctx,
1216                              lldb::ClangExpressionVariableSP &expr_var,
1217                              lldb::addr_t addr,
1218                              Error &err);
1219
1220    //------------------------------------------------------------------
1221    /// Actually do the task of materializing or dematerializing a
1222    /// register variable.
1223    ///
1224    /// @param[in] dematerialize
1225    ///     True if the variable is to be dematerialized; false if it is to
1226    ///     be materialized.
1227    ///
1228    /// @param[in] exe_ctx
1229    ///     The execution context to use.
1230    ///
1231    /// @param[in] reg_ctx
1232    ///     The register context to use.
1233    ///
1234    /// @param[in] reg_info
1235    ///     The information for the register to read/write.
1236    ///
1237    /// @param[in] addr
1238    ///     The address at which to materialize the variable.
1239    ///
1240    /// @param[in] err
1241    ///     An Error to populate with any messages related to
1242    ///     (de)materializing the persistent variable.
1243    ///
1244    /// @return
1245    ///     True on success; false otherwise.
1246    //------------------------------------------------------------------
1247    bool
1248    DoMaterializeOneRegister (bool dematerialize,
1249                              ExecutionContext &exe_ctx,
1250                              RegisterContext &reg_ctx,
1251                              const RegisterInfo &reg_info,
1252                              lldb::addr_t addr,
1253                              Error &err);
1254
1255    //------------------------------------------------------------------
1256    /// A wrapper for ClangASTContext::CopyType that sets a flag that
1257    /// indicates that we should not respond to queries during import.
1258    ///
1259    /// @param[in] dest_context
1260    ///     The target AST context, typically the parser's AST context.
1261    ///
1262    /// @param[in] source_context
1263    ///     The source AST context, typically the AST context of whatever
1264    ///     symbol file the type was found in.
1265    ///
1266    /// @param[in] clang_type
1267    ///     The source type.
1268    ///
1269    /// @return
1270    ///     The imported type.
1271    //------------------------------------------------------------------
1272    void *
1273    GuardedCopyType (clang::ASTContext *dest_context,
1274                     clang::ASTContext *source_context,
1275                     void *clang_type);
1276};
1277
1278} // namespace lldb_private
1279
1280#endif  // liblldb_ClangExpressionDeclMap_h_
1281