]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/lldb/include/lldb/Expression/ClangExpressionDeclMap.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / lldb / include / lldb / Expression / ClangExpressionDeclMap.h
1 //===-- ClangExpressionDeclMap.h --------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_ClangExpressionDeclMap_h_
11 #define liblldb_ClangExpressionDeclMap_h_
12
13 // C Includes
14 #include <signal.h>
15 #include <stdint.h>
16
17 // C++ Includes
18 #include <vector>
19
20 // Other libraries and framework includes
21 // Project includes
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "clang/AST/Decl.h"
25 #include "lldb/lldb-public.h"
26 #include "lldb/Core/ClangForward.h"
27 #include "lldb/Core/Value.h"
28 #include "lldb/Expression/ClangASTSource.h"
29 #include "lldb/Expression/ClangExpressionVariable.h"
30 #include "lldb/Expression/Materializer.h"
31 #include "lldb/Symbol/TaggedASTType.h"
32 #include "lldb/Symbol/SymbolContext.h"
33 #include "lldb/Target/ExecutionContext.h"
34
35 namespace lldb_private {
36
37 //----------------------------------------------------------------------
38 /// @class ClangExpressionDeclMap ClangExpressionDeclMap.h "lldb/Expression/ClangExpressionDeclMap.h"
39 /// @brief Manages named entities that are defined in LLDB's debug information.
40 ///
41 /// The Clang parser uses the ClangASTSource as an interface to request named
42 /// entities from outside an expression.  The ClangASTSource reports back, listing
43 /// all possible objects corresponding to a particular name.  But it in turn
44 /// relies on ClangExpressionDeclMap, which performs several important functions.
45 ///
46 /// First, it records what variables and functions were looked up and what Decls
47 /// were returned for them.
48 ///
49 /// Second, it constructs a struct on behalf of IRForTarget, recording which 
50 /// variables should be placed where and relaying this information back so that 
51 /// IRForTarget can generate context-independent code.
52 ///
53 /// Third, it "materializes" this struct on behalf of the expression command,
54 /// finding the current values of each variable and placing them into the
55 /// struct so that it can be passed to the JITted version of the IR.
56 ///
57 /// Fourth and finally, it "dematerializes" the struct after the JITted code has
58 /// has executed, placing the new values back where it found the old ones.
59 //----------------------------------------------------------------------
60 class ClangExpressionDeclMap : 
61     public ClangASTSource
62 {
63 public:
64     //------------------------------------------------------------------
65     /// Constructor
66     ///
67     /// Initializes class variables.
68     ///
69     /// @param[in] keep_result_in_memory
70     ///     If true, inhibits the normal deallocation of the memory for
71     ///     the result persistent variable, and instead marks the variable
72     ///     as persisting.
73     ///
74     /// @param[in] exe_ctx
75     ///     The execution context to use when parsing.
76     //------------------------------------------------------------------
77     ClangExpressionDeclMap (bool keep_result_in_memory,
78                             ExecutionContext &exe_ctx);
79     
80     //------------------------------------------------------------------
81     /// Destructor
82     //------------------------------------------------------------------
83     ~ClangExpressionDeclMap ();
84     
85     //------------------------------------------------------------------
86     /// Enable the state needed for parsing and IR transformation.
87     ///
88     /// @param[in] exe_ctx
89     ///     The execution context to use when finding types for variables.
90     ///     Also used to find a "scratch" AST context to store result types.
91     ///
92     /// @param[in] materializer
93     ///     If non-NULL, the materializer to populate with information about
94     ///     the variables to use
95     ///
96     /// @return
97     ///     True if parsing is possible; false if it is unsafe to continue.
98     //------------------------------------------------------------------
99     bool
100     WillParse (ExecutionContext &exe_ctx,
101                Materializer *materializer);
102     
103     //------------------------------------------------------------------
104     /// [Used by ClangExpressionParser] For each variable that had an unknown
105     ///     type at the beginning of parsing, determine its final type now.
106     ///
107     /// @return
108     ///     True on success; false otherwise.
109     //------------------------------------------------------------------
110     bool 
111     ResolveUnknownTypes();
112     
113     //------------------------------------------------------------------
114     /// Disable the state needed for parsing and IR transformation.
115     //------------------------------------------------------------------
116     void 
117     DidParse ();
118     
119     //------------------------------------------------------------------
120     /// [Used by IRForTarget] Add a variable to the list of persistent
121     ///     variables for the process.
122     ///
123     /// @param[in] decl
124     ///     The Clang declaration for the persistent variable, used for
125     ///     lookup during parsing.
126     ///
127     /// @param[in] name
128     ///     The name of the persistent variable, usually $something.
129     ///
130     /// @param[in] type
131     ///     The type of the variable, in the Clang parser's context.
132     ///
133     /// @return
134     ///     True on success; false otherwise.
135     //------------------------------------------------------------------
136     bool 
137     AddPersistentVariable (const clang::NamedDecl *decl,
138                            const ConstString &name, 
139                            TypeFromParser type,
140                            bool is_result,
141                            bool is_lvalue);
142     
143     //------------------------------------------------------------------
144     /// [Used by IRForTarget] Add a variable to the struct that needs to
145     ///     be materialized each time the expression runs.
146     ///
147     /// @param[in] decl
148     ///     The Clang declaration for the variable.
149     ///
150     /// @param[in] name
151     ///     The name of the variable.
152     ///
153     /// @param[in] value
154     ///     The LLVM IR value for this variable.
155     ///
156     /// @param[in] size
157     ///     The size of the variable in bytes.
158     ///
159     /// @param[in] alignment
160     ///     The required alignment of the variable in bytes.
161     ///
162     /// @return
163     ///     True on success; false otherwise.
164     //------------------------------------------------------------------
165     bool 
166     AddValueToStruct (const clang::NamedDecl *decl,
167                       const ConstString &name,
168                       llvm::Value *value,
169                       size_t size,
170                       off_t alignment);
171     
172     //------------------------------------------------------------------
173     /// [Used by IRForTarget] Finalize the struct, laying out the position 
174     /// of each object in it.
175     ///
176     /// @return
177     ///     True on success; false otherwise.
178     //------------------------------------------------------------------
179     bool 
180     DoStructLayout ();
181     
182     //------------------------------------------------------------------
183     /// [Used by IRForTarget] Get general information about the laid-out
184     /// struct after DoStructLayout() has been called.
185     ///
186     /// @param[out] num_elements
187     ///     The number of elements in the struct.
188     ///
189     /// @param[out] size
190     ///     The size of the struct, in bytes.
191     ///
192     /// @param[out] alignment
193     ///     The alignment of the struct, in bytes.
194     ///
195     /// @return
196     ///     True if the information could be retrieved; false otherwise.
197     //------------------------------------------------------------------
198     bool 
199     GetStructInfo (uint32_t &num_elements,
200                    size_t &size,
201                    off_t &alignment);
202     
203     //------------------------------------------------------------------
204     /// [Used by IRForTarget] Get specific information about one field
205     /// of the laid-out struct after DoStructLayout() has been called.
206     ///
207     /// @param[out] decl
208     ///     The parsed Decl for the field, as generated by ClangASTSource
209     ///     on ClangExpressionDeclMap's behalf.  In the case of the result
210     ///     value, this will have the name $__lldb_result even if the
211     ///     result value ends up having the name $1.  This is an
212     ///     implementation detail of IRForTarget.
213     ///
214     /// @param[out] value
215     ///     The IR value for the field (usually a GlobalVariable).  In
216     ///     the case of the result value, this will have the correct
217     ///     name ($1, for instance).  This is an implementation detail
218     ///     of IRForTarget.
219     ///
220     /// @param[out] offset
221     ///     The offset of the field from the beginning of the struct.
222     ///     As long as the struct is aligned according to its required
223     ///     alignment, this offset will align the field correctly.
224     ///
225     /// @param[out] name
226     ///     The name of the field as used in materialization.
227     ///
228     /// @param[in] index
229     ///     The index of the field about which information is requested.
230     ///
231     /// @return
232     ///     True if the information could be retrieved; false otherwise.
233     //------------------------------------------------------------------
234     bool 
235     GetStructElement (const clang::NamedDecl *&decl,
236                       llvm::Value *&value,
237                       off_t &offset,
238                       ConstString &name,
239                       uint32_t index);
240     
241     //------------------------------------------------------------------
242     /// [Used by IRForTarget] Get information about a function given its
243     /// Decl.
244     ///
245     /// @param[in] decl
246     ///     The parsed Decl for the Function, as generated by ClangASTSource
247     ///     on ClangExpressionDeclMap's behalf.
248     ///
249     /// @param[out] ptr
250     ///     The absolute address of the function in the target.
251     ///
252     /// @return
253     ///     True if the information could be retrieved; false otherwise.
254     //------------------------------------------------------------------
255     bool 
256     GetFunctionInfo (const clang::NamedDecl *decl, 
257                      uint64_t &ptr);
258     
259     //------------------------------------------------------------------
260     /// [Used by IRForTarget] Get the address of a function given nothing
261     /// but its name.  Some functions are needed but didn't get Decls made
262     /// during parsing -- specifically, sel_registerName is never called
263     /// in the generated IR but we need to call it nonetheless.
264     ///
265     /// @param[in] name
266     ///     The name of the function.  
267     ///
268     /// @param[out] ptr
269     ///     The absolute address of the function in the target.
270     ///
271     /// @return
272     ///     True if the address could be retrieved; false otherwise.
273     //------------------------------------------------------------------
274     bool 
275     GetFunctionAddress (const ConstString &name,
276                         uint64_t &ptr);
277     
278     //------------------------------------------------------------------
279     /// [Used by IRForTarget] Get the address of a symbol given nothing
280     /// but its name.
281     ///
282     /// @param[in] target
283     ///     The target to find the symbol in.  If not provided,
284     ///     then the current parsing context's Target.
285     ///
286     /// @param[in] process
287     ///     The process to use.  For Objective-C symbols, the process's
288     ///     Objective-C language runtime may be queried if the process
289     ///     is non-NULL.
290     ///
291     /// @param[in] name
292     ///     The name of the symbol.  
293     ///
294     /// @param[in] module
295     ///     The module to limit the search to. This can be NULL
296     ///
297     /// @return
298     ///     Valid load address for the symbol
299     //------------------------------------------------------------------
300     lldb::addr_t 
301     GetSymbolAddress (Target &target,
302                       Process *process,
303                       const ConstString &name,
304                       lldb::SymbolType symbol_type,
305                       Module *module = NULL);
306     
307     lldb::addr_t
308     GetSymbolAddress (const ConstString &name,
309                       lldb::SymbolType symbol_type);
310     
311     //------------------------------------------------------------------
312     /// [Used by IRInterpreter] Get basic target information.
313     ///
314     /// @param[out] byte_order
315     ///     The byte order of the target.
316     ///
317     /// @param[out] address_byte_size
318     ///     The size of a pointer in bytes.
319     ///
320     /// @return
321     ///     True if the information could be determined; false 
322     ///     otherwise.
323     //------------------------------------------------------------------
324     struct TargetInfo
325     {
326         lldb::ByteOrder byte_order;
327         size_t address_byte_size;
328         
329         TargetInfo() :
330             byte_order(lldb::eByteOrderInvalid),
331             address_byte_size(0)
332         {
333         }
334         
335         bool IsValid()
336         {
337             return (byte_order != lldb::eByteOrderInvalid &&
338                     address_byte_size != 0);
339         }
340     };
341     TargetInfo GetTargetInfo();
342     
343     //------------------------------------------------------------------
344     /// [Used by ClangASTSource] Find all entities matching a given name,
345     /// using a NameSearchContext to make Decls for them.
346     ///
347     /// @param[in] context
348     ///     The NameSearchContext that can construct Decls for this name.
349     ///
350     /// @return
351     ///     True on success; false otherwise.
352     //------------------------------------------------------------------
353     void 
354     FindExternalVisibleDecls (NameSearchContext &context);
355     
356     //------------------------------------------------------------------
357     /// Find all entities matching a given name in a given module/namespace,
358     /// using a NameSearchContext to make Decls for them.
359     ///
360     /// @param[in] context
361     ///     The NameSearchContext that can construct Decls for this name.
362     ///
363     /// @param[in] module
364     ///     If non-NULL, the module to query.
365     ///
366     /// @param[in] namespace_decl
367     ///     If valid and module is non-NULL, the parent namespace.
368     ///
369     /// @param[in] name
370     ///     The name as a plain C string.  The NameSearchContext contains 
371     ///     a DeclarationName for the name so at first the name may seem
372     ///     redundant, but ClangExpressionDeclMap operates in RTTI land so 
373     ///     it can't access DeclarationName.
374     ///
375     /// @param[in] current_id
376     ///     The ID for the current FindExternalVisibleDecls invocation,
377     ///     for logging purposes.
378     ///
379     /// @return
380     ///     True on success; false otherwise.
381     //------------------------------------------------------------------
382     void 
383     FindExternalVisibleDecls (NameSearchContext &context, 
384                               lldb::ModuleSP module,
385                               ClangNamespaceDecl &namespace_decl,
386                               unsigned int current_id);
387 private:
388     ClangExpressionVariableList    m_found_entities;           ///< All entities that were looked up for the parser.
389     ClangExpressionVariableList    m_struct_members;           ///< All entities that need to be placed in the struct.
390     bool                           m_keep_result_in_memory;    ///< True if result persistent variables generated by this expression should stay in memory.
391     
392     //----------------------------------------------------------------------
393     /// The following values should not live beyond parsing
394     //----------------------------------------------------------------------
395     class ParserVars 
396     {
397     public:
398         ParserVars(ClangExpressionDeclMap &decl_map) :
399             m_exe_ctx(),
400             m_sym_ctx(),
401             m_persistent_vars(NULL),
402             m_enable_lookups(false),
403             m_materializer(NULL),
404             m_decl_map(decl_map)
405         {
406         }
407         
408         Target *
409         GetTarget()
410         {
411             if (m_exe_ctx.GetTargetPtr())
412                 return m_exe_ctx.GetTargetPtr();
413             else if (m_sym_ctx.target_sp)
414                 m_sym_ctx.target_sp.get();
415             return NULL;
416         }
417         
418         ExecutionContext            m_exe_ctx;          ///< The execution context to use when parsing.
419         SymbolContext               m_sym_ctx;          ///< The symbol context to use in finding variables and types.
420         ClangPersistentVariables   *m_persistent_vars;  ///< The persistent variables for the process.
421         bool                        m_enable_lookups;   ///< Set to true during parsing if we have found the first "$__lldb" name.
422         TargetInfo                  m_target_info;      ///< Basic information about the target.
423         Materializer               *m_materializer;     ///< If non-NULL, the materializer to use when reporting used variables.
424     private:
425         ClangExpressionDeclMap     &m_decl_map;
426         DISALLOW_COPY_AND_ASSIGN (ParserVars);
427     };
428     
429     std::unique_ptr<ParserVars> m_parser_vars;
430     
431     //----------------------------------------------------------------------
432     /// Activate parser-specific variables
433     //----------------------------------------------------------------------
434     void 
435     EnableParserVars()
436     {
437         if (!m_parser_vars.get())
438             m_parser_vars.reset(new ParserVars(*this));
439     }
440     
441     //----------------------------------------------------------------------
442     /// Deallocate parser-specific variables
443     //----------------------------------------------------------------------
444     void 
445     DisableParserVars()
446     {
447         m_parser_vars.reset();
448     }
449     
450     //----------------------------------------------------------------------
451     /// The following values contain layout information for the materialized
452     /// struct, but are not specific to a single materialization
453     //----------------------------------------------------------------------
454     struct StructVars {
455         StructVars() :
456             m_struct_alignment(0),
457             m_struct_size(0),
458             m_struct_laid_out(false),
459             m_result_name(),
460             m_object_pointer_type(NULL, NULL)
461         {
462         }
463         
464         off_t                       m_struct_alignment;         ///< The alignment of the struct in bytes.
465         size_t                      m_struct_size;              ///< The size of the struct in bytes.
466         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).
467         ConstString                 m_result_name;              ///< The name of the result variable ($1, for example)
468         TypeFromUser                m_object_pointer_type;      ///< The type of the "this" variable, if one exists
469     };
470     
471     std::unique_ptr<StructVars> m_struct_vars;
472     
473     //----------------------------------------------------------------------
474     /// Activate struct variables
475     //----------------------------------------------------------------------
476     void 
477     EnableStructVars()
478     {
479         if (!m_struct_vars.get())
480             m_struct_vars.reset(new struct StructVars);
481     }
482     
483     //----------------------------------------------------------------------
484     /// Deallocate struct variables
485     //----------------------------------------------------------------------
486     void
487     DisableStructVars()
488     {
489         m_struct_vars.reset();
490     }
491     
492     //----------------------------------------------------------------------
493     /// Get this parser's ID for use in extracting parser- and JIT-specific
494     /// data from persistent variables.
495     //----------------------------------------------------------------------
496     uint64_t
497     GetParserID()
498     {
499         return (uint64_t)this;
500     }
501     
502     //------------------------------------------------------------------
503     /// Given a target, find a data symbol that has the given name.
504     ///
505     /// @param[in] target
506     ///     The target to use as the basis for the search.
507     ///
508     /// @param[in] name
509     ///     The name as a plain C string.
510     ///
511     /// @param[in] module
512     ///     The module to limit the search to. This can be NULL
513     ///
514     /// @return
515     ///     The LLDB Symbol found, or NULL if none was found.
516     //------------------------------------------------------------------
517     const Symbol *
518     FindGlobalDataSymbol (Target &target,
519                           const ConstString &name,
520                           Module *module = NULL);
521     
522     //------------------------------------------------------------------
523     /// Given a target, find a variable that matches the given name and 
524     /// type.
525     ///
526     /// @param[in] target
527     ///     The target to use as a basis for finding the variable.
528     ///
529     /// @param[in] module
530     ///     If non-NULL, the module to search.
531     ///
532     /// @param[in] name
533     ///     The name as a plain C string.
534     ///
535     /// @param[in] namespace_decl
536     ///     If non-NULL and module is non-NULL, the parent namespace.
537     ///
538     /// @param[in] type
539     ///     The required type for the variable.  This function may be called
540     ///     during parsing, in which case we don't know its type; hence the
541     ///     default.
542     ///
543     /// @return
544     ///     The LLDB Variable found, or NULL if none was found.
545     //------------------------------------------------------------------
546     lldb::VariableSP
547     FindGlobalVariable (Target &target,
548                         lldb::ModuleSP &module,
549                         const ConstString &name,
550                         ClangNamespaceDecl *namespace_decl,
551                         TypeFromUser *type = NULL);
552     
553     //------------------------------------------------------------------
554     /// Get the value of a variable in a given execution context and return
555     /// the associated Types if needed.
556     ///
557     /// @param[in] var
558     ///     The variable to evaluate.
559     ///
560     /// @param[out] var_location
561     ///     The variable location value to fill in
562     ///
563     /// @param[out] found_type
564     ///     The type of the found value, as it was found in the user process.
565     ///     This is only useful when the variable is being inspected on behalf
566     ///     of the parser, hence the default.
567     ///
568     /// @param[out] parser_type
569     ///     The type of the found value, as it was copied into the parser's
570     ///     AST context.  This is only useful when the variable is being
571     ///     inspected on behalf of the parser, hence the default.
572     ///
573     /// @param[in] decl
574     ///     The Decl to be looked up.
575     ///
576     /// @return
577     ///     Return true if the value was successfully filled in.
578     //------------------------------------------------------------------
579     bool
580     GetVariableValue (lldb::VariableSP &var,
581                       lldb_private::Value &var_location,
582                       TypeFromUser *found_type = NULL,
583                       TypeFromParser *parser_type = NULL);
584     
585     //------------------------------------------------------------------
586     /// Use the NameSearchContext to generate a Decl for the given LLDB
587     /// Variable, and put it in the Tuple list.
588     ///
589     /// @param[in] context
590     ///     The NameSearchContext to use when constructing the Decl.
591     ///
592     /// @param[in] var
593     ///     The LLDB Variable that needs a Decl.
594     ///
595     /// @param[in] valobj
596     ///     The LLDB ValueObject for that variable.
597     //------------------------------------------------------------------
598     void 
599     AddOneVariable (NameSearchContext &context, 
600                     lldb::VariableSP var,
601                     lldb::ValueObjectSP valobj,
602                     unsigned int current_id);
603     
604     //------------------------------------------------------------------
605     /// Use the NameSearchContext to generate a Decl for the given
606     /// persistent variable, and put it in the list of found entities.
607     ///
608     /// @param[in] context
609     ///     The NameSearchContext to use when constructing the Decl.
610     ///
611     /// @param[in] pvar
612     ///     The persistent variable that needs a Decl.
613     ///
614     /// @param[in] current_id
615     ///     The ID of the current invocation of FindExternalVisibleDecls
616     ///     for logging purposes.
617     //------------------------------------------------------------------
618     void 
619     AddOneVariable (NameSearchContext &context, 
620                     lldb::ClangExpressionVariableSP &pvar_sp,
621                     unsigned int current_id);
622     
623     //------------------------------------------------------------------
624     /// Use the NameSearchContext to generate a Decl for the given LLDB
625     /// symbol (treated as a variable), and put it in the list of found
626     /// entities.
627     ///
628     /// @param[in] context
629     ///     The NameSearchContext to use when constructing the Decl.
630     ///
631     /// @param[in] var
632     ///     The LLDB Variable that needs a Decl.
633     //------------------------------------------------------------------
634     void
635     AddOneGenericVariable (NameSearchContext &context,
636                            const Symbol &symbol,
637                            unsigned int current_id);
638     
639     //------------------------------------------------------------------
640     /// Use the NameSearchContext to generate a Decl for the given
641     /// function.  (Functions are not placed in the Tuple list.)  Can
642     /// handle both fully typed functions and generic functions.
643     ///
644     /// @param[in] context
645     ///     The NameSearchContext to use when constructing the Decl.
646     ///
647     /// @param[in] fun
648     ///     The Function that needs to be created.  If non-NULL, this is
649     ///     a fully-typed function.
650     ///
651     /// @param[in] sym
652     ///     The Symbol that corresponds to a function that needs to be 
653     ///     created with generic type (unitptr_t foo(...)).
654     //------------------------------------------------------------------
655     void
656     AddOneFunction (NameSearchContext &context, 
657                     Function *fun, 
658                     Symbol *sym,
659                     unsigned int current_id);
660     
661     //------------------------------------------------------------------
662     /// Use the NameSearchContext to generate a Decl for the given
663     /// register.
664     ///
665     /// @param[in] context
666     ///     The NameSearchContext to use when constructing the Decl.
667     ///
668     /// @param[in] reg_info
669     ///     The information corresponding to that register.
670     //------------------------------------------------------------------
671     void 
672     AddOneRegister (NameSearchContext &context, 
673                     const RegisterInfo *reg_info,
674                     unsigned int current_id);
675     
676     //------------------------------------------------------------------
677     /// Use the NameSearchContext to generate a Decl for the given
678     /// type.  (Types are not placed in the Tuple list.)
679     ///
680     /// @param[in] context
681     ///     The NameSearchContext to use when constructing the Decl.
682     ///
683     /// @param[in] type
684     ///     The type that needs to be created.
685     //------------------------------------------------------------------
686     void 
687     AddOneType (NameSearchContext &context, 
688                 TypeFromUser &type,
689                 unsigned int current_id);
690     
691     //------------------------------------------------------------------
692     /// Copy a C++ class type into the parser's AST context and add a
693     /// member function declaration to it for the expression.
694     ///
695     /// @param[in] type
696     ///     The type that needs to be created.
697     //------------------------------------------------------------------
698
699     TypeFromParser
700     CopyClassType(TypeFromUser &type,
701                   unsigned int current_id);
702 };
703     
704 } // namespace lldb_private
705
706 #endif  // liblldb_ClangExpressionDeclMap_h_