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