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