xref: /freebsd-src/contrib/llvm-project/clang/lib/Interpreter/IncrementalParser.h (revision 3a0793336edfc21cb6d4c8c5c5d7f1665f3e6c5a)
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 
165f757f3fSDimitry 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*3a079333SDimitry Andric class Module;
285f757f3fSDimitry Andric } // namespace llvm
29fe6060f1SDimitry Andric 
30fe6060f1SDimitry Andric namespace clang {
31fe6060f1SDimitry Andric class ASTConsumer;
3206c3fb27SDimitry Andric class CodeGenerator;
33fe6060f1SDimitry Andric class CompilerInstance;
34fe6060f1SDimitry Andric class IncrementalAction;
3506c3fb27SDimitry Andric class Interpreter;
36fe6060f1SDimitry Andric class Parser;
37fe6060f1SDimitry Andric /// Provides support for incremental compilation. Keeps track of the state
38fe6060f1SDimitry Andric /// changes between the subsequent incremental input.
39fe6060f1SDimitry Andric ///
40fe6060f1SDimitry Andric class IncrementalParser {
4106c3fb27SDimitry Andric protected:
42fe6060f1SDimitry Andric   /// Long-lived, incremental parsing action.
43fe6060f1SDimitry Andric   std::unique_ptr<IncrementalAction> Act;
44fe6060f1SDimitry Andric 
45fe6060f1SDimitry Andric   /// Compiler instance performing the incremental compilation.
46fe6060f1SDimitry Andric   std::unique_ptr<CompilerInstance> CI;
47fe6060f1SDimitry Andric 
48fe6060f1SDimitry Andric   /// Parser.
49fe6060f1SDimitry Andric   std::unique_ptr<Parser> P;
50fe6060f1SDimitry Andric 
51fe6060f1SDimitry Andric   /// Consumer to process the produced top level decls. Owned by Act.
52fe6060f1SDimitry Andric   ASTConsumer *Consumer = nullptr;
53fe6060f1SDimitry Andric 
54fe6060f1SDimitry Andric   /// Counts the number of direct user input lines that have been parsed.
55fe6060f1SDimitry Andric   unsigned InputCount = 0;
56fe6060f1SDimitry Andric 
57fe6060f1SDimitry Andric   /// List containing every information about every incrementally parsed piece
58fe6060f1SDimitry Andric   /// of code.
59fe6060f1SDimitry Andric   std::list<PartialTranslationUnit> PTUs;
60fe6060f1SDimitry Andric 
61*3a079333SDimitry Andric   /// When CodeGen is created the first llvm::Module gets cached in many places
62*3a079333SDimitry Andric   /// and we must keep it alive.
63*3a079333SDimitry Andric   std::unique_ptr<llvm::Module> CachedInCodeGenModule;
64*3a079333SDimitry Andric 
6506c3fb27SDimitry Andric   IncrementalParser();
66fe6060f1SDimitry Andric 
6706c3fb27SDimitry Andric public:
6806c3fb27SDimitry Andric   IncrementalParser(Interpreter &Interp,
6906c3fb27SDimitry Andric                     std::unique_ptr<CompilerInstance> Instance,
7006c3fb27SDimitry Andric                     llvm::LLVMContext &LLVMCtx, llvm::Error &Err);
7106c3fb27SDimitry Andric   virtual ~IncrementalParser();
7206c3fb27SDimitry Andric 
getCI()7306c3fb27SDimitry Andric   CompilerInstance *getCI() { return CI.get(); }
7406c3fb27SDimitry Andric   CodeGenerator *getCodeGen() const;
75fe6060f1SDimitry Andric 
76fe6060f1SDimitry Andric   /// Parses incremental input by creating an in-memory file.
77fe6060f1SDimitry Andric   ///\returns a \c PartialTranslationUnit which holds information about the
78fe6060f1SDimitry Andric   /// \c TranslationUnitDecl and \c llvm::Module corresponding to the input.
7906c3fb27SDimitry Andric   virtual llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Input);
80fe6060f1SDimitry Andric 
81349cc55cSDimitry Andric   /// Uses the CodeGenModule mangled name cache and avoids recomputing.
82349cc55cSDimitry Andric   ///\returns the mangled name of a \c GD.
83349cc55cSDimitry Andric   llvm::StringRef GetMangledName(GlobalDecl GD) const;
84349cc55cSDimitry Andric 
8581ad6265SDimitry Andric   void CleanUpPTU(PartialTranslationUnit &PTU);
8681ad6265SDimitry Andric 
getPTUs()8781ad6265SDimitry Andric   std::list<PartialTranslationUnit> &getPTUs() { return PTUs; }
8881ad6265SDimitry Andric 
8906c3fb27SDimitry Andric   std::unique_ptr<llvm::Module> GenModule();
9006c3fb27SDimitry Andric 
91fe6060f1SDimitry Andric private:
92fe6060f1SDimitry Andric   llvm::Expected<PartialTranslationUnit &> ParseOrWrapTopLevelDecl();
93fe6060f1SDimitry Andric };
94fe6060f1SDimitry Andric } // end namespace clang
95fe6060f1SDimitry Andric 
96fe6060f1SDimitry Andric #endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
97