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 "llvm/ADT/StringRef.h"
17#include "llvm/Support/Error.h"
18
19#include <list>
20#include <memory>
21
22namespace llvm {
23class Module;
24}
25
26namespace clang {
27class ASTConsumer;
28class CompilerInstance;
29class Parser;
30class Sema;
31class TranslationUnitDecl;
32class IncrementalAction;
33struct PartialTranslationUnit;
34
35/// Provides support for incremental compilation. Keeps track of the state
36/// changes between the subsequent incremental input.
37///
38class IncrementalParser {
39protected:
40 /// The Sema performing the incremental compilation.
41 Sema &S;
42
43 /// Parser.
44 std::unique_ptr<Parser> P;
45
46 /// Consumer to process the produced top level decls. Owned by Act.
47 ASTConsumer *Consumer = nullptr;
48
49 /// Counts the number of direct user input lines that have been parsed.
50 unsigned InputCount = 0;
51
52 /// The FrontendAction used during incremental parsing.
53 IncrementalAction *Act = nullptr;
54
55 std::list<PartialTranslationUnit> &PTUs;
56
57public:
58 IncrementalParser(CompilerInstance &Instance, IncrementalAction *Act,
59 llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
60 virtual ~IncrementalParser();
61
62 /// Parses incremental input by creating an in-memory file.
63 ///\returns a \c PartialTranslationUnit which holds information about the
64 /// \c TranslationUnitDecl.
65 virtual llvm::Expected<TranslationUnitDecl *> Parse(llvm::StringRef Input);
66
67 void CleanUpPTU(TranslationUnitDecl *MostRecentTU);
68
69 /// Register a PTU produced by Parse.
70 PartialTranslationUnit &RegisterPTU(TranslationUnitDecl *TU,
71 std::unique_ptr<llvm::Module> M = {});
72
73private:
74 llvm::Expected<TranslationUnitDecl *> ParseOrWrapTopLevelDecl();
75};
76} // end namespace clang
77
78#endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALPARSER_H
79