1 //===--- Interpreter.h - Incremental Compilation and Execution---*- 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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the component which performs incremental code
10 // compilation and execution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_INTERPRETER_INTERPRETER_H
15 #define LLVM_CLANG_INTERPRETER_INTERPRETER_H
16 
17 #include "clang/Interpreter/PartialTranslationUnit.h"
18 
19 #include "clang/AST/GlobalDecl.h"
20 
21 #include "llvm/ExecutionEngine/JITSymbol.h"
22 #include "llvm/Support/Error.h"
23 
24 #include <memory>
25 #include <vector>
26 
27 namespace llvm {
28 namespace orc {
29 class LLJIT;
30 class ThreadSafeContext;
31 }
32 } // namespace llvm
33 
34 namespace clang {
35 
36 class CompilerInstance;
37 class IncrementalExecutor;
38 class IncrementalParser;
39 
40 /// Create a pre-configured \c CompilerInstance for incremental processing.
41 class IncrementalCompilerBuilder {
42 public:
43   static llvm::Expected<std::unique_ptr<CompilerInstance>>
44   create(std::vector<const char *> &ClangArgv);
45 };
46 
47 /// Provides top-level interfaces for incremental compilation and execution.
48 class Interpreter {
49   std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
50   std::unique_ptr<IncrementalParser> IncrParser;
51   std::unique_ptr<IncrementalExecutor> IncrExecutor;
52 
53   Interpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err);
54 
55 public:
56   ~Interpreter();
57   static llvm::Expected<std::unique_ptr<Interpreter>>
58   create(std::unique_ptr<CompilerInstance> CI);
59   const CompilerInstance *getCompilerInstance() const;
60   const llvm::orc::LLJIT *getExecutionEngine() const;
61   llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Code);
62   llvm::Error Execute(PartialTranslationUnit &T);
63   llvm::Error ParseAndExecute(llvm::StringRef Code) {
64     auto PTU = Parse(Code);
65     if (!PTU)
66       return PTU.takeError();
67     if (PTU->TheModule)
68       return Execute(*PTU);
69     return llvm::Error::success();
70   }
71 
72   /// \returns the \c JITTargetAddress of a \c GlobalDecl. This interface uses
73   /// the CodeGenModule's internal mangling cache to avoid recomputing the
74   /// mangled name.
75   llvm::Expected<llvm::JITTargetAddress> getSymbolAddress(GlobalDecl GD) const;
76 
77   /// \returns the \c JITTargetAddress of a given name as written in the IR.
78   llvm::Expected<llvm::JITTargetAddress>
79   getSymbolAddress(llvm::StringRef IRName) const;
80 
81   /// \returns the \c JITTargetAddress of a given name as written in the object
82   /// file.
83   llvm::Expected<llvm::JITTargetAddress>
84   getSymbolAddressFromLinkerName(llvm::StringRef LinkerName) const;
85 };
86 } // namespace clang
87 
88 #endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
89