1 //===--- IncrementalParser.h - Incremental Compilation ----------*- 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 implements the class which performs incremental code compilation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
14 #define LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
15 
16 #include "clang/Interpreter/PartialTranslationUnit.h"
17 
18 #include "clang/AST/GlobalDecl.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Error.h"
22 
23 #include <list>
24 #include <memory>
25 namespace llvm {
26 class LLVMContext;
27 }
28 
29 namespace clang {
30 class ASTConsumer;
31 class CodeGenerator;
32 class CompilerInstance;
33 class IncrementalAction;
34 class Interpreter;
35 class Parser;
36 /// Provides support for incremental compilation. Keeps track of the state
37 /// changes between the subsequent incremental input.
38 ///
39 class IncrementalParser {
40 protected:
41   /// Long-lived, incremental parsing action.
42   std::unique_ptr<IncrementalAction> Act;
43 
44   /// Compiler instance performing the incremental compilation.
45   std::unique_ptr<CompilerInstance> CI;
46 
47   /// Parser.
48   std::unique_ptr<Parser> P;
49 
50   /// Consumer to process the produced top level decls. Owned by Act.
51   ASTConsumer *Consumer = nullptr;
52 
53   /// Counts the number of direct user input lines that have been parsed.
54   unsigned InputCount = 0;
55 
56   /// List containing every information about every incrementally parsed piece
57   /// of code.
58   std::list<PartialTranslationUnit> PTUs;
59 
60   IncrementalParser();
61 
62 public:
63   IncrementalParser(Interpreter &Interp,
64                     std::unique_ptr<CompilerInstance> Instance,
65                     llvm::LLVMContext &LLVMCtx, llvm::Error &Err);
66   virtual ~IncrementalParser();
67 
68   CompilerInstance *getCI() { return CI.get(); }
69   CodeGenerator *getCodeGen() const;
70 
71   /// Parses incremental input by creating an in-memory file.
72   ///\returns a \c PartialTranslationUnit which holds information about the
73   /// \c TranslationUnitDecl and \c llvm::Module corresponding to the input.
74   virtual llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Input);
75 
76   /// Uses the CodeGenModule mangled name cache and avoids recomputing.
77   ///\returns the mangled name of a \c GD.
78   llvm::StringRef GetMangledName(GlobalDecl GD) const;
79 
80   void CleanUpPTU(PartialTranslationUnit &PTU);
81 
82   std::list<PartialTranslationUnit> &getPTUs() { return PTUs; }
83 
84   std::unique_ptr<llvm::Module> GenModule();
85 
86 private:
87   llvm::Expected<PartialTranslationUnit &> ParseOrWrapTopLevelDecl();
88 };
89 } // end namespace clang
90 
91 #endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
92