174 lines · c
1//===-- ASTResultSynthesizer.h ----------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_ASTRESULTSYNTHESIZER_H10#define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_ASTRESULTSYNTHESIZER_H11 12#include "lldb/Target/Target.h"13#include "clang/Sema/SemaConsumer.h"14 15namespace clang {16class CompoundStmt;17class DeclContext;18class NamedDecl;19class ObjCMethodDecl;20class TypeDecl;21} // namespace clang22 23namespace lldb_private {24 25/// \class ASTResultSynthesizer ASTResultSynthesizer.h26/// "lldb/Expression/ASTResultSynthesizer.h" Adds a result variable27/// declaration to the ASTs for an expression.28///29/// Users expect the expression "i + 3" to return a result, even if a result30/// variable wasn't specifically declared. To fulfil this requirement, LLDB31/// adds a result variable to the expression, transforming it to "int32/// $__lldb_expr_result = i + 3." The IR transformers ensure that the33/// resulting variable is mapped to the right piece of memory.34/// ASTResultSynthesizer's job is to add the variable and its initialization35/// to the ASTs for the expression, and it does so by acting as a SemaConsumer36/// for Clang.37class ASTResultSynthesizer : public clang::SemaConsumer {38public:39 /// Constructor40 ///41 /// \param[in] passthrough42 /// Since the ASTs must typically go through to the Clang code generator43 /// in order to produce LLVM IR, this SemaConsumer must allow them to44 /// pass to the next step in the chain after processing. Passthrough is45 /// the next ASTConsumer, or NULL if none is required.46 ///47 /// \param[in] top_level48 /// If true, register all top-level Decls and don't try to handle the49 /// main function.50 ///51 /// \param[in] target52 /// The target, which contains the persistent variable store and the53 /// AST importer.54 ASTResultSynthesizer(clang::ASTConsumer *passthrough, bool top_level,55 Target &target);56 57 /// Destructor58 ~ASTResultSynthesizer() override;59 60 /// Link this consumer with a particular AST context61 ///62 /// \param[in] Context63 /// This AST context will be used for types and identifiers, and also64 /// forwarded to the passthrough consumer, if one exists.65 void Initialize(clang::ASTContext &Context) override;66 67 /// Examine a list of Decls to find the function $__lldb_expr and transform68 /// its code69 ///70 /// \param[in] D71 /// The list of Decls to search. These may contain LinkageSpecDecls,72 /// which need to be searched recursively. That job falls to73 /// TransformTopLevelDecl.74 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;75 76 /// Passthrough stub77 void HandleTranslationUnit(clang::ASTContext &Ctx) override;78 79 /// Passthrough stub80 void HandleTagDeclDefinition(clang::TagDecl *D) override;81 82 /// Passthrough stub83 void CompleteTentativeDefinition(clang::VarDecl *D) override;84 85 /// Passthrough stub86 void HandleVTable(clang::CXXRecordDecl *RD) override;87 88 /// Passthrough stub89 void PrintStats() override;90 91 /// Set the Sema object to use when performing transforms, and pass it on92 ///93 /// \param[in] S94 /// The Sema to use. Because Sema isn't externally visible, this class95 /// casts it to an Action for actual use.96 void InitializeSema(clang::Sema &S) override;97 98 /// Reset the Sema to NULL now that transformations are done99 void ForgetSema() override;100 101 /// The parse has succeeded, so record its persistent decls102 void CommitPersistentDecls();103 104private:105 /// Hunt the given Decl for FunctionDecls named $__lldb_expr, recursing as106 /// necessary through LinkageSpecDecls, and calling SynthesizeResult on107 /// anything that was found108 ///109 /// \param[in] D110 /// The Decl to hunt.111 void TransformTopLevelDecl(clang::Decl *D);112 113 /// Process an Objective-C method and produce the result variable and114 /// initialization115 ///116 /// \param[in] MethodDecl117 /// The method to process.118 bool SynthesizeObjCMethodResult(clang::ObjCMethodDecl *MethodDecl);119 120 /// Process a function and produce the result variable and initialization121 ///122 /// \param[in] FunDecl123 /// The function to process.124 bool SynthesizeFunctionResult(clang::FunctionDecl *FunDecl);125 126 /// Process a function body and produce the result variable and127 /// initialization128 ///129 /// \param[in] Body130 /// The body of the function.131 ///132 /// \param[in] DC133 /// The DeclContext of the function, into which the result variable134 /// is inserted.135 bool SynthesizeBodyResult(clang::CompoundStmt *Body, clang::DeclContext *DC);136 137 /// Given a DeclContext for a function or method, find all types declared in138 /// the context and record any persistent types found.139 ///140 /// \param[in] FunDeclCtx141 /// The context for the function to process.142 void RecordPersistentTypes(clang::DeclContext *FunDeclCtx);143 144 /// Given a TypeDecl, if it declares a type whose name starts with a dollar145 /// sign, register it as a pointer type in the target's scratch AST context.146 void MaybeRecordPersistentType(clang::TypeDecl *D);147 148 /// Given a NamedDecl, register it as a pointer type in the target's scratch149 /// AST context.150 void RecordPersistentDecl(clang::NamedDecl *D);151 152 clang::ASTContext153 *m_ast_context; ///< The AST context to use for identifiers and types.154 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for155 ///passthrough. NULL if it's a156 ///SemaConsumer.157 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,158 ///for passthrough. NULL if it's an159 ///ASTConsumer.160 161 std::vector<clang::NamedDecl *> m_decls; ///< Persistent declarations to162 ///register assuming the expression163 ///succeeds.164 165 Target &m_target; ///< The target, which contains the persistent variable166 ///store and the167 clang::Sema *m_sema; ///< The Sema to use.168 bool m_top_level;169};170 171} // namespace lldb_private172 173#endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_ASTRESULTSYNTHESIZER_H174