1414#ifndef LLVM_CLANG_INTERPRETER_INTERPRETER_H
1515#define LLVM_CLANG_INTERPRETER_INTERPRETER_H
1616
17- #include " clang/AST/Decl.h"
1817#include " clang/AST/GlobalDecl.h"
1918#include " clang/Interpreter/PartialTranslationUnit.h"
2019#include " clang/Interpreter/Value.h"
21- #include " clang/Sema/Ownership.h"
2220
2321#include " llvm/ADT/DenseMap.h"
2422#include " llvm/ExecutionEngine/JITSymbol.h"
@@ -38,6 +36,9 @@ class ThreadSafeContext;
3836namespace clang {
3937
4038class CompilerInstance ;
39+ class CodeGenerator ;
40+ class CXXRecordDecl ;
41+ class Decl ;
4142class IncrementalExecutor ;
4243class IncrementalParser ;
4344
@@ -77,42 +78,45 @@ class IncrementalCompilerBuilder {
7778 llvm::StringRef CudaSDKPath;
7879};
7980
80- // / Generate glue code between the Interpreter's built-in runtime and user code.
81- class RuntimeInterfaceBuilder {
82- public:
83- virtual ~RuntimeInterfaceBuilder () = default ;
84-
85- using TransformExprFunction = ExprResult(RuntimeInterfaceBuilder *Builder,
86- Expr *, ArrayRef<Expr *>);
87- virtual TransformExprFunction *getPrintValueTransformer () = 0;
88- };
81+ class IncrementalAction ;
82+ class InProcessPrintingASTConsumer ;
8983
9084// / Provides top-level interfaces for incremental compilation and execution.
9185class Interpreter {
86+ friend class Value ;
87+ friend InProcessPrintingASTConsumer;
88+
9289 std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
90+ // / Long-lived, incremental parsing action.
91+ std::unique_ptr<IncrementalAction> Act;
9392 std::unique_ptr<IncrementalParser> IncrParser;
9493 std::unique_ptr<IncrementalExecutor> IncrExecutor;
95- std::unique_ptr<RuntimeInterfaceBuilder> RuntimeIB;
9694
9795 // An optional parser for CUDA offloading
9896 std::unique_ptr<IncrementalParser> DeviceParser;
9997
98+ // / List containing information about each incrementally parsed piece of code.
99+ std::list<PartialTranslationUnit> PTUs;
100+
100101 unsigned InitPTUSize = 0 ;
101102
102103 // This member holds the last result of the value printing. It's a class
103104 // member because we might want to access it after more inputs. If no value
104105 // printing happens, it's in an invalid state.
105106 Value LastValue;
106107
107- // Add a call to an Expr to report its result. We query the function from
108- // RuntimeInterfaceBuilder once and store it as a function pointer to avoid
109- // frequent virtual function calls.
110- RuntimeInterfaceBuilder::TransformExprFunction *AddPrintValueCall = nullptr ;
108+ // / When CodeGen is created the first llvm::Module gets cached in many places
109+ // / and we must keep it alive.
110+ std::unique_ptr<llvm::Module> CachedInCodeGenModule;
111+
112+ // / Compiler instance performing the incremental compilation.
113+ std::unique_ptr<CompilerInstance> CI;
111114
112115protected:
113116 // Derived classes can use an extended interface of the Interpreter.
114- Interpreter (std::unique_ptr<CompilerInstance> CI, llvm::Error &Err,
115- std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr );
117+ Interpreter (std::unique_ptr<CompilerInstance> Instance, llvm::Error &Err,
118+ std::unique_ptr<llvm::orc::LLJITBuilder> JITBuilder = nullptr ,
119+ std::unique_ptr<clang::ASTConsumer> Consumer = nullptr );
116120
117121 // Create the internal IncrementalExecutor, or re-create it after calling
118122 // ResetExecutor().
@@ -122,15 +126,8 @@ class Interpreter {
122126 // JIT engine. In particular, it doesn't run cleanup or destructors.
123127 void ResetExecutor ();
124128
125- // Lazily construct the RuntimeInterfaceBuilder. The provided instance will be
126- // used for the entire lifetime of the interpreter. The default implementation
127- // targets the in-process __clang_Interpreter runtime. Override this to use a
128- // custom runtime.
129- virtual std::unique_ptr<RuntimeInterfaceBuilder> FindRuntimeInterface ();
130-
131129public:
132130 virtual ~Interpreter ();
133-
134131 static llvm::Expected<std::unique_ptr<Interpreter>>
135132 create (std::unique_ptr<CompilerInstance> CI);
136133 static llvm::Expected<std::unique_ptr<Interpreter>>
@@ -145,7 +142,6 @@ class Interpreter {
145142 llvm::Expected<PartialTranslationUnit &> Parse (llvm::StringRef Code);
146143 llvm::Error Execute (PartialTranslationUnit &T);
147144 llvm::Error ParseAndExecute (llvm::StringRef Code, Value *V = nullptr );
148- llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall (CXXRecordDecl *CXXRD);
149145
150146 // / Undo N previous incremental inputs.
151147 llvm::Error Undo (unsigned N = 1 );
@@ -167,8 +163,6 @@ class Interpreter {
167163 llvm::Expected<llvm::orc::ExecutorAddr>
168164 getSymbolAddressFromLinkerName (llvm::StringRef LinkerName) const ;
169165
170- enum InterfaceKind { NoAlloc, WithAlloc, CopyArray, NewTag };
171-
172166 const llvm::SmallVectorImpl<Expr *> &getValuePrintingInfo () const {
173167 return ValuePrintingInfo;
174168 }
@@ -178,7 +172,15 @@ class Interpreter {
178172private:
179173 size_t getEffectivePTUSize () const ;
180174 void markUserCodeStart ();
175+ llvm::Expected<Expr *> ExtractValueFromExpr (Expr *E);
176+ llvm::Expected<llvm::orc::ExecutorAddr> CompileDtorCall (CXXRecordDecl *CXXRD);
177+
178+ CodeGenerator *getCodeGen () const ;
179+ std::unique_ptr<llvm::Module> GenModule ();
180+ PartialTranslationUnit &RegisterPTU (TranslationUnitDecl *TU);
181181
182+ // A cache for the compiled destructors used to for de-allocation of managed
183+ // clang::Values.
182184 llvm::DenseMap<CXXRecordDecl *, llvm::orc::ExecutorAddr> Dtors;
183185
184186 llvm::SmallVector<Expr *, 4 > ValuePrintingInfo;
0 commit comments