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