xref: /freebsd-src/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1fe6060f1SDimitry Andric //===--- IncrementalParser.h - Incremental Compilation ----------*- C++ -*-===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric //
9fe6060f1SDimitry Andric // This file implements the class which performs incremental code compilation.
10fe6060f1SDimitry Andric //
11fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #ifndef LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
14fe6060f1SDimitry Andric #define LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
15fe6060f1SDimitry Andric 
16*5f757f3fSDimitry Andric #include "clang/AST/GlobalDecl.h"
17fe6060f1SDimitry Andric #include "clang/Interpreter/PartialTranslationUnit.h"
18fe6060f1SDimitry Andric 
19fe6060f1SDimitry Andric #include "llvm/ADT/ArrayRef.h"
20fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h"
21fe6060f1SDimitry Andric #include "llvm/Support/Error.h"
22fe6060f1SDimitry Andric 
23fe6060f1SDimitry Andric #include <list>
24fe6060f1SDimitry Andric #include <memory>
25fe6060f1SDimitry Andric namespace llvm {
26fe6060f1SDimitry Andric class LLVMContext;
27*5f757f3fSDimitry Andric } // namespace llvm
28fe6060f1SDimitry Andric 
29fe6060f1SDimitry Andric namespace clang {
30fe6060f1SDimitry Andric class ASTConsumer;
3106c3fb27SDimitry Andric class CodeGenerator;
32fe6060f1SDimitry Andric class CompilerInstance;
33fe6060f1SDimitry Andric class IncrementalAction;
3406c3fb27SDimitry Andric class Interpreter;
35fe6060f1SDimitry Andric class Parser;
36fe6060f1SDimitry Andric /// Provides support for incremental compilation. Keeps track of the state
37fe6060f1SDimitry Andric /// changes between the subsequent incremental input.
38fe6060f1SDimitry Andric ///
39fe6060f1SDimitry Andric class IncrementalParser {
4006c3fb27SDimitry Andric protected:
41fe6060f1SDimitry Andric   /// Long-lived, incremental parsing action.
42fe6060f1SDimitry Andric   std::unique_ptr<IncrementalAction> Act;
43fe6060f1SDimitry Andric 
44fe6060f1SDimitry Andric   /// Compiler instance performing the incremental compilation.
45fe6060f1SDimitry Andric   std::unique_ptr<CompilerInstance> CI;
46fe6060f1SDimitry Andric 
47fe6060f1SDimitry Andric   /// Parser.
48fe6060f1SDimitry Andric   std::unique_ptr<Parser> P;
49fe6060f1SDimitry Andric 
50fe6060f1SDimitry Andric   /// Consumer to process the produced top level decls. Owned by Act.
51fe6060f1SDimitry Andric   ASTConsumer *Consumer = nullptr;
52fe6060f1SDimitry Andric 
53fe6060f1SDimitry Andric   /// Counts the number of direct user input lines that have been parsed.
54fe6060f1SDimitry Andric   unsigned InputCount = 0;
55fe6060f1SDimitry Andric 
56fe6060f1SDimitry Andric   /// List containing every information about every incrementally parsed piece
57fe6060f1SDimitry Andric   /// of code.
58fe6060f1SDimitry Andric   std::list<PartialTranslationUnit> PTUs;
59fe6060f1SDimitry Andric 
6006c3fb27SDimitry Andric   IncrementalParser();
61fe6060f1SDimitry Andric 
6206c3fb27SDimitry Andric public:
6306c3fb27SDimitry Andric   IncrementalParser(Interpreter &Interp,
6406c3fb27SDimitry Andric                     std::unique_ptr<CompilerInstance> Instance,
6506c3fb27SDimitry Andric                     llvm::LLVMContext &LLVMCtx, llvm::Error &Err);
6606c3fb27SDimitry Andric   virtual ~IncrementalParser();
6706c3fb27SDimitry Andric 
6806c3fb27SDimitry Andric   CompilerInstance *getCI() { return CI.get(); }
6906c3fb27SDimitry Andric   CodeGenerator *getCodeGen() const;
70fe6060f1SDimitry Andric 
71fe6060f1SDimitry Andric   /// Parses incremental input by creating an in-memory file.
72fe6060f1SDimitry Andric   ///\returns a \c PartialTranslationUnit which holds information about the
73fe6060f1SDimitry Andric   /// \c TranslationUnitDecl and \c llvm::Module corresponding to the input.
7406c3fb27SDimitry Andric   virtual llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Input);
75fe6060f1SDimitry Andric 
76349cc55cSDimitry Andric   /// Uses the CodeGenModule mangled name cache and avoids recomputing.
77349cc55cSDimitry Andric   ///\returns the mangled name of a \c GD.
78349cc55cSDimitry Andric   llvm::StringRef GetMangledName(GlobalDecl GD) const;
79349cc55cSDimitry Andric 
8081ad6265SDimitry Andric   void CleanUpPTU(PartialTranslationUnit &PTU);
8181ad6265SDimitry Andric 
8281ad6265SDimitry Andric   std::list<PartialTranslationUnit> &getPTUs() { return PTUs; }
8381ad6265SDimitry Andric 
8406c3fb27SDimitry Andric   std::unique_ptr<llvm::Module> GenModule();
8506c3fb27SDimitry Andric 
86fe6060f1SDimitry Andric private:
87fe6060f1SDimitry Andric   llvm::Expected<PartialTranslationUnit &> ParseOrWrapTopLevelDecl();
88fe6060f1SDimitry Andric };
89fe6060f1SDimitry Andric } // end namespace clang
90fe6060f1SDimitry Andric 
91fe6060f1SDimitry Andric #endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
92