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