Home | History | Annotate | Download | only in Expression
      1 //===-- ASTStructExtractor.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_ASTStructExtractor_h_
     11 #define liblldb_ASTStructExtractor_h_
     12 
     13 #include "clang/Sema/SemaConsumer.h"
     14 #include "lldb/Core/ClangForward.h"
     15 #include "lldb/Expression/ClangExpressionVariable.h"
     16 #include "lldb/Expression/ClangFunction.h"
     17 
     18 namespace lldb_private {
     19 
     20 //----------------------------------------------------------------------
     21 /// @class ASTStructExtractor ASTStructExtractor.h "lldb/Expression/ASTStructExtractor.h"
     22 /// @brief Extracts and describes the argument structure for a wrapped function.
     23 ///
     24 /// This pass integrates with ClangFunction, which calls functions with custom
     25 /// sets of arguments.  To avoid having to implement the full calling convention
     26 /// for the target's architecture, ClangFunction writes a simple wrapper
     27 /// function that takes a pointer to an argument structure that contains room
     28 /// for the address of the function to be called, the values of all its
     29 /// arguments, and room for the function's return value.
     30 ///
     31 /// The definition of this struct is itself in the body of the wrapper function,
     32 /// so Clang does the structure layout itself.  ASTStructExtractor reads through
     33 /// the AST for the wrapper funtion and finds the struct.
     34 //----------------------------------------------------------------------
     35 class ASTStructExtractor : public clang::SemaConsumer
     36 {
     37 public:
     38     //----------------------------------------------------------------------
     39     /// Constructor
     40     ///
     41     /// @param[in] passthrough
     42     ///     Since the ASTs must typically go through to the Clang code generator
     43     ///     in order to produce LLVM IR, this SemaConsumer must allow them to
     44     ///     pass to the next step in the chain after processing.  Passthrough is
     45     ///     the next ASTConsumer, or NULL if none is required.
     46     ///
     47     /// @param[in] struct_name
     48     ///     The name of the structure to extract from the wrapper function.
     49     ///
     50     /// @param[in] function
     51     ///     The caller object whose members should be populated with information
     52     ///     about the argument struct.  ClangFunction friends ASTStructExtractor
     53     ///     for this purpose.
     54     //----------------------------------------------------------------------
     55     ASTStructExtractor(clang::ASTConsumer *passthrough,
     56                        const char *struct_name,
     57                        ClangFunction &function);
     58 
     59     //----------------------------------------------------------------------
     60     /// Destructor
     61     //----------------------------------------------------------------------
     62     virtual ~ASTStructExtractor();
     63 
     64     //----------------------------------------------------------------------
     65     /// Link this consumer with a particular AST context
     66     ///
     67     /// @param[in] Context
     68     ///     This AST context will be used for types and identifiers, and also
     69     ///     forwarded to the passthrough consumer, if one exists.
     70     //----------------------------------------------------------------------
     71     void Initialize(clang::ASTContext &Context);
     72 
     73     //----------------------------------------------------------------------
     74     /// Examine a list of Decls to find the function $__lldb_expr and
     75     /// transform its code
     76     ///
     77     /// @param[in] D
     78     ///     The list of Decls to search.  These may contain LinkageSpecDecls,
     79     ///     which need to be searched recursively.  That job falls to
     80     ///     TransformTopLevelDecl.
     81     //----------------------------------------------------------------------
     82     bool HandleTopLevelDecl(clang::DeclGroupRef D);
     83 
     84     //----------------------------------------------------------------------
     85     /// Passthrough stub
     86     //----------------------------------------------------------------------
     87     void HandleTranslationUnit(clang::ASTContext &Ctx);
     88 
     89     //----------------------------------------------------------------------
     90     /// Passthrough stub
     91     //----------------------------------------------------------------------
     92     void HandleTagDeclDefinition(clang::TagDecl *D);
     93 
     94     //----------------------------------------------------------------------
     95     /// Passthrough stub
     96     //----------------------------------------------------------------------
     97     void CompleteTentativeDefinition(clang::VarDecl *D);
     98 
     99     //----------------------------------------------------------------------
    100     /// Passthrough stub
    101     //----------------------------------------------------------------------
    102     void HandleVTable(clang::CXXRecordDecl *RD, bool DefinitionRequired);
    103 
    104     //----------------------------------------------------------------------
    105     /// Passthrough stub
    106     //----------------------------------------------------------------------
    107     void PrintStats();
    108 
    109     //----------------------------------------------------------------------
    110     /// Set the Sema object to use when performing transforms, and pass it on
    111     ///
    112     /// @param[in] S
    113     ///     The Sema to use.  Because Sema isn't externally visible, this class
    114     ///     casts it to an Action for actual use.
    115     //----------------------------------------------------------------------
    116     void InitializeSema(clang::Sema &S);
    117 
    118     //----------------------------------------------------------------------
    119     /// Reset the Sema to NULL now that transformations are done
    120     //----------------------------------------------------------------------
    121     void ForgetSema();
    122 private:
    123     //----------------------------------------------------------------------
    124     /// Hunt the given FunctionDecl for the argument struct and place
    125     /// information about it into m_function
    126     ///
    127     /// @param[in] F
    128     ///     The FunctionDecl to hunt.
    129     //----------------------------------------------------------------------
    130     void
    131     ExtractFromFunctionDecl(clang::FunctionDecl* F);
    132 
    133     //----------------------------------------------------------------------
    134     /// Hunt the given Decl for FunctionDecls named the same as the wrapper
    135     /// function name, recursing as necessary through LinkageSpecDecls, and
    136     /// calling ExtractFromFunctionDecl on anything that was found
    137     ///
    138     /// @param[in] D
    139     ///     The Decl to hunt.
    140     //----------------------------------------------------------------------
    141     void
    142     ExtractFromTopLevelDecl(clang::Decl* D);
    143 
    144     clang::ASTContext              *m_ast_context;          ///< The AST context to use for identifiers and types.
    145     clang::ASTConsumer             *m_passthrough;          ///< The ASTConsumer down the chain, for passthrough.  NULL if it's a SemaConsumer.
    146     clang::SemaConsumer            *m_passthrough_sema;     ///< The SemaConsumer down the chain, for passthrough.  NULL if it's an ASTConsumer.
    147     clang::Sema                    *m_sema;                 ///< The Sema to use.
    148     clang::Action                  *m_action;               ///< The Sema to use, cast to an Action so it's usable.
    149 
    150     ClangFunction                  &m_function;             ///< The function to populate with information about the argument structure.
    151     std::string                     m_struct_name;          ///< The name of the structure to extract.
    152 };
    153 
    154 }
    155 
    156 #endif
    157