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 "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/Support/Error.h" 21 22 #include <list> 23 #include <memory> 24 namespace llvm { 25 class LLVMContext; 26 } 27 28 namespace clang { 29 class ASTConsumer; 30 class CompilerInstance; 31 class CodeGenerator; 32 class DeclGroupRef; 33 class FrontendAction; 34 class IncrementalAction; 35 class Parser; 36 37 /// Provides support for incremental compilation. Keeps track of the state 38 /// changes between the subsequent incremental input. 39 /// 40 class IncrementalParser { 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 public: 61 IncrementalParser(std::unique_ptr<CompilerInstance> Instance, 62 llvm::LLVMContext &LLVMCtx, llvm::Error &Err); 63 ~IncrementalParser(); 64 65 const CompilerInstance *getCI() const { return CI.get(); } 66 67 /// Parses incremental input by creating an in-memory file. 68 ///\returns a \c PartialTranslationUnit which holds information about the 69 /// \c TranslationUnitDecl and \c llvm::Module corresponding to the input. 70 llvm::Expected<PartialTranslationUnit &> Parse(llvm::StringRef Input); 71 72 private: 73 llvm::Expected<PartialTranslationUnit &> ParseOrWrapTopLevelDecl(); 74 }; 75 } // end namespace clang 76 77 #endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H 78