Home | History | Annotate | Download | only in Expression
      1 //===-- Materializer.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 lldb_Materializer_h
     11 #define lldb_Materializer_h
     12 
     13 #include "lldb/lldb-private-types.h"
     14 #include "lldb/Core/Error.h"
     15 #include "lldb/Expression/IRMemoryMap.h"
     16 #include "lldb/Host/Mutex.h"
     17 #include "lldb/Symbol/SymbolContext.h"
     18 #include "lldb/Target/StackFrame.h"
     19 
     20 #include <vector>
     21 
     22 namespace lldb_private
     23 {
     24 
     25 class Materializer
     26 {
     27 public:
     28     Materializer ();
     29     ~Materializer ();
     30 
     31     class Dematerializer
     32     {
     33     public:
     34         Dematerializer () :
     35             m_materializer(NULL),
     36             m_map(NULL),
     37             m_process_address(LLDB_INVALID_ADDRESS)
     38         {
     39         }
     40 
     41         ~Dematerializer ()
     42         {
     43             Wipe ();
     44         }
     45 
     46         void Dematerialize (Error &err,
     47                             lldb::ClangExpressionVariableSP &result_sp,
     48                             lldb::addr_t frame_top,
     49                             lldb::addr_t frame_bottom);
     50 
     51         void Wipe ();
     52 
     53         bool IsValid ()
     54         {
     55             return m_materializer && m_map && (m_process_address != LLDB_INVALID_ADDRESS);
     56         }
     57     private:
     58         friend class Materializer;
     59 
     60         Dematerializer (Materializer &materializer,
     61                         lldb::StackFrameSP &frame_sp,
     62                         IRMemoryMap &map,
     63                         lldb::addr_t process_address) :
     64             m_materializer(&materializer),
     65             m_map(&map),
     66             m_process_address(process_address)
     67         {
     68             if (frame_sp)
     69             {
     70                 m_thread_wp = frame_sp->GetThread();
     71                 m_stack_id = frame_sp->GetStackID();
     72             }
     73         }
     74 
     75         Materializer       *m_materializer;
     76         lldb::ThreadWP      m_thread_wp;
     77         StackID             m_stack_id;
     78         IRMemoryMap        *m_map;
     79         lldb::addr_t        m_process_address;
     80     };
     81 
     82     typedef std::shared_ptr<Dematerializer> DematerializerSP;
     83     typedef std::weak_ptr<Dematerializer> DematerializerWP;
     84 
     85     DematerializerSP Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err);
     86 
     87     uint32_t AddPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp, Error &err);
     88     uint32_t AddVariable (lldb::VariableSP &variable_sp, Error &err);
     89     uint32_t AddResultVariable (const TypeFromUser &type, bool is_lvalue, bool keep_in_memory, Error &err);
     90     uint32_t AddSymbol (const Symbol &symbol_sp, Error &err);
     91     uint32_t AddRegister (const RegisterInfo &register_info, Error &err);
     92 
     93     uint32_t GetStructAlignment ()
     94     {
     95         return m_struct_alignment;
     96     }
     97 
     98     uint32_t GetStructByteSize ()
     99     {
    100         return m_current_offset;
    101     }
    102 
    103     uint32_t GetResultOffset ()
    104     {
    105         if (m_result_entity)
    106             return m_result_entity->GetOffset();
    107         else
    108             return UINT32_MAX;
    109     }
    110 
    111     class Entity
    112     {
    113     public:
    114         Entity () :
    115             m_alignment(1),
    116             m_size(0),
    117             m_offset(0)
    118         {
    119         }
    120 
    121         virtual ~Entity ()
    122         {
    123         }
    124 
    125         virtual void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err) = 0;
    126         virtual void Dematerialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address,
    127                                     lldb::addr_t frame_top, lldb::addr_t frame_bottom, Error &err) = 0;
    128         virtual void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log) = 0;
    129         virtual void Wipe (IRMemoryMap &map, lldb::addr_t process_address) = 0;
    130 
    131         uint32_t GetAlignment ()
    132         {
    133             return m_alignment;
    134         }
    135 
    136         uint32_t GetSize ()
    137         {
    138             return m_size;
    139         }
    140 
    141         uint32_t GetOffset ()
    142         {
    143             return m_offset;
    144         }
    145 
    146         void SetOffset (uint32_t offset)
    147         {
    148             m_offset = offset;
    149         }
    150     protected:
    151         void SetSizeAndAlignmentFromType (ClangASTType &type);
    152 
    153         uint32_t    m_alignment;
    154         uint32_t    m_size;
    155         uint32_t    m_offset;
    156     };
    157 
    158 private:
    159     uint32_t AddStructMember (Entity &entity);
    160 
    161     typedef std::unique_ptr<Entity>  EntityUP;
    162     typedef std::vector<EntityUP>   EntityVector;
    163 
    164     DematerializerWP                m_dematerializer_wp;
    165     EntityVector                    m_entities;
    166     Entity                         *m_result_entity;
    167     uint32_t                        m_current_offset;
    168     uint32_t                        m_struct_alignment;
    169 };
    170 
    171 }
    172 
    173 #endif
    174