xref: /netbsd-src/external/apache2/llvm/dist/clang/include/clang/Interpreter/Interpreter.h (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
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/Transaction.h"
18 
19 #include "llvm/Support/Error.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 namespace llvm {
25 namespace orc {
26 class ThreadSafeContext;
27 }
28 class Module;
29 } // namespace llvm
30 
31 namespace clang {
32 
33 class CompilerInstance;
34 class DeclGroupRef;
35 class IncrementalExecutor;
36 class IncrementalParser;
37 
38 /// Create a pre-configured \c CompilerInstance for incremental processing.
39 class IncrementalCompilerBuilder {
40 public:
41   static llvm::Expected<std::unique_ptr<CompilerInstance>>
42   create(std::vector<const char *> &ClangArgv);
43 };
44 
45 /// Provides top-level interfaces for incremental compilation and execution.
46 class Interpreter {
47   std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;
48   std::unique_ptr<IncrementalParser> IncrParser;
49   std::unique_ptr<IncrementalExecutor> IncrExecutor;
50 
51   Interpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &Err);
52 
53 public:
54   ~Interpreter();
55   static llvm::Expected<std::unique_ptr<Interpreter>>
56   create(std::unique_ptr<CompilerInstance> CI);
57   const CompilerInstance *getCompilerInstance() const;
58   llvm::Expected<Transaction &> Parse(llvm::StringRef Code);
59   llvm::Error Execute(Transaction &T);
ParseAndExecute(llvm::StringRef Code)60   llvm::Error ParseAndExecute(llvm::StringRef Code) {
61     auto ErrOrTransaction = Parse(Code);
62     if (auto Err = ErrOrTransaction.takeError())
63       return Err;
64     if (ErrOrTransaction->TheModule)
65       return Execute(*ErrOrTransaction);
66     return llvm::Error::success();
67   }
68 };
69 } // namespace clang
70 
71 #endif // LLVM_CLANG_INTERPRETER_INTERPRETER_H
72