1//===- ASTDeserializationListener.h - Decl/Type PCH Read Events -*- 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 defines the ASTDeserializationListener class, which is notified
10// by the ASTReader whenever a type or declaration is deserialized.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SERIALIZATION_ASTDESERIALIZATIONLISTENER_H
15#define LLVM_CLANG_SERIALIZATION_ASTDESERIALIZATIONLISTENER_H
16
17#include "clang/Basic/IdentifierTable.h"
18#include "clang/Serialization/ASTBitCodes.h"
19
20namespace clang {
21
22class Decl;
23class ASTReader;
24class QualType;
25class MacroDefinitionRecord;
26class MacroInfo;
27class Module;
28class SourceLocation;
29
30// IMPORTANT: when you add a new interface to this class, please update the
31// DelegatingDeserializationListener below.
32class ASTDeserializationListener {
33public:
34 virtual ~ASTDeserializationListener();
35
36 /// The ASTReader was initialized.
37 virtual void ReaderInitialized(ASTReader *Reader) { }
38
39 /// An identifier was deserialized from the AST file.
40 virtual void IdentifierRead(serialization::IdentifierID ID,
41 IdentifierInfo *II) { }
42 /// A macro was read from the AST file.
43 virtual void MacroRead(serialization::MacroID ID, MacroInfo *MI) { }
44 /// A type was deserialized from the AST file. The ID here has the
45 /// qualifier bits already removed, and T is guaranteed to be locally
46 /// unqualified.
47 virtual void TypeRead(serialization::TypeIdx Idx, QualType T) { }
48 /// A decl was deserialized from the AST file.
49 //
50 // Note: Implementors should be cautious when introducing additional
51 // serialization (e.g., printing the qualified name of the declaration) within
52 // the callback. Doing so may lead to unintended and complex side effects, or
53 // even cause a crash.
54 virtual void DeclRead(GlobalDeclID ID, const Decl *D) {}
55 /// A predefined decl was built during the serialization.
56 virtual void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) {}
57 /// A selector was read from the AST file.
58 virtual void SelectorRead(serialization::SelectorID iD, Selector Sel) {}
59 /// A macro definition was read from the AST file.
60 virtual void MacroDefinitionRead(serialization::PreprocessedEntityID,
61 MacroDefinitionRecord *MD) {}
62 /// A module definition was read from the AST file.
63 virtual void ModuleRead(serialization::SubmoduleID ID, Module *Mod) {}
64 /// A module import was read from the AST file.
65 virtual void ModuleImportRead(serialization::SubmoduleID ID,
66 SourceLocation ImportLoc) {}
67};
68
69class DelegatingDeserializationListener : public ASTDeserializationListener {
70 ASTDeserializationListener *Previous;
71 bool DeletePrevious;
72
73public:
74 explicit DelegatingDeserializationListener(
75 ASTDeserializationListener *Previous, bool DeletePrevious)
76 : Previous(Previous), DeletePrevious(DeletePrevious) {}
77 ~DelegatingDeserializationListener() override {
78 if (DeletePrevious)
79 delete Previous;
80 }
81
82 DelegatingDeserializationListener(const DelegatingDeserializationListener &) =
83 delete;
84 DelegatingDeserializationListener &
85 operator=(const DelegatingDeserializationListener &) = delete;
86
87 void ReaderInitialized(ASTReader *Reader) override {
88 if (Previous)
89 Previous->ReaderInitialized(Reader);
90 }
91 void IdentifierRead(serialization::IdentifierID ID,
92 IdentifierInfo *II) override {
93 if (Previous)
94 Previous->IdentifierRead(ID, II);
95 }
96 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override {
97 if (Previous)
98 Previous->MacroRead(ID, MI);
99 }
100 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
101 if (Previous)
102 Previous->TypeRead(Idx, T);
103 }
104 void DeclRead(GlobalDeclID ID, const Decl *D) override {
105 if (Previous)
106 Previous->DeclRead(ID, D);
107 }
108 void PredefinedDeclBuilt(PredefinedDeclIDs ID, const Decl *D) override {
109 if (Previous)
110 Previous->PredefinedDeclBuilt(ID, D);
111 }
112 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
113 if (Previous)
114 Previous->SelectorRead(iD: ID, Sel);
115 }
116 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
117 MacroDefinitionRecord *MD) override {
118 if (Previous)
119 Previous->MacroDefinitionRead(PPID, MD);
120 }
121 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override {
122 if (Previous)
123 Previous->ModuleRead(ID, Mod);
124 }
125 void ModuleImportRead(serialization::SubmoduleID ID,
126 SourceLocation ImportLoc) override {
127 if (Previous)
128 Previous->ModuleImportRead(ID, ImportLoc);
129 }
130};
131
132} // namespace clang
133
134#endif
135