1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- ASTRecordReader.h - Helper classes for reading AST -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines classes that are useful in the implementation of
// the ASTReader.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
#define LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
#include "clang/AST/ASTContext.h"
#include "clang/AST/AbstractBasicReader.h"
#include "clang/Lex/Token.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/SourceLocationEncoding.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/APSInt.h"
namespace clang {
class OMPTraitInfo;
class OMPChildren;
/// An object for streaming information from a record.
class ASTRecordReader
: public serialization::DataStreamBasicReader<ASTRecordReader> {
using ModuleFile = serialization::ModuleFile;
using LocSeq = SourceLocationSequence;
ASTReader *Reader;
ModuleFile *F;
unsigned Idx = 0;
ASTReader::RecordData Record;
using RecordData = ASTReader::RecordData;
using RecordDataImpl = ASTReader::RecordDataImpl;
public:
/// Construct an ASTRecordReader that uses the default encoding scheme.
ASTRecordReader(ASTReader &Reader, ModuleFile &F)
: DataStreamBasicReader(Reader.getContext()), Reader(&Reader), F(&F) {}
/// Reads a record with id AbbrevID from Cursor, resetting the
/// internal state.
Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor,
unsigned AbbrevID);
/// Is this a module file for a module (rather than a PCH or similar).
bool isModule() const { return F->isModule(); }
/// Retrieve the AST context that this AST reader supplements.
ASTContext &getContext() { return Reader->getContext(); }
/// The current position in this record.
unsigned getIdx() const { return Idx; }
/// The length of this record.
size_t size() const { return Record.size(); }
/// An arbitrary index in this record.
const uint64_t &operator[](size_t N) { return Record[N]; }
/// Returns the last value in this record.
uint64_t back() { return Record.back(); }
/// Returns the current value in this record, and advances to the
/// next value.
uint64_t readInt() { return Record[Idx++]; }
ArrayRef<uint64_t> readIntArray(unsigned Len) {
auto Array = llvm::ArrayRef(Record).slice(Idx, Len);
Idx += Len;
return Array;
}
/// Returns the current value in this record, without advancing.
uint64_t peekInt() { return Record[Idx]; }
/// Skips the specified number of values.
void skipInts(unsigned N) { Idx += N; }
/// Retrieve the global submodule ID its local ID number.
serialization::SubmoduleID
getGlobalSubmoduleID(unsigned LocalID) {
return Reader->getGlobalSubmoduleID(*F, LocalID);
}
/// Retrieve the submodule that corresponds to a global submodule ID.
Module *getSubmodule(serialization::SubmoduleID GlobalID) {
return Reader->getSubmodule(GlobalID);
}
/// Read the record that describes the lexical contents of a DC.
bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC) {
return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
DC);
}
/// Read the record that describes the visible contents of a DC.
bool readVisibleDeclContextStorage(uint64_t Offset,
serialization::DeclID ID) {
return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset,
ID);
}
ExplicitSpecifier readExplicitSpec() {
uint64_t Kind = readInt();
bool HasExpr = Kind & 0x1;
Kind = Kind >> 1;
return ExplicitSpecifier(HasExpr ? readExpr() : nullptr,
static_cast<ExplicitSpecKind>(Kind));
}
/// Read information about an exception specification (inherited).
//FunctionProtoType::ExceptionSpecInfo
//readExceptionSpecInfo(SmallVectorImpl<QualType> &ExceptionStorage);
/// Get the global offset corresponding to a local offset.
uint64_t getGlobalBitOffset(uint64_t LocalOffset) {
return Reader->getGlobalBitOffset(*F, LocalOffset);
}
/// Reads a statement.
Stmt *readStmt() { return Reader->ReadStmt(*F); }
Stmt *readStmtRef() { return readStmt(); /* FIXME: readSubStmt? */ }
/// Reads an expression.
Expr *readExpr() { return Reader->ReadExpr(*F); }
/// Reads a sub-statement operand during statement reading.
Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
/// Reads a sub-expression operand during statement reading.
Expr *readSubExpr() { return Reader->ReadSubExpr(); }
/// Reads a declaration with the given local ID in the given module.
///
/// \returns The requested declaration, casted to the given return type.
template<typename T>
T *GetLocalDeclAs(uint32_t LocalID) {
return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
}
/// Reads a TemplateArgumentLocInfo appropriate for the
/// given TemplateArgument kind, advancing Idx.
TemplateArgumentLocInfo
readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind);
/// Reads a TemplateArgumentLoc, advancing Idx.
TemplateArgumentLoc readTemplateArgumentLoc();
const ASTTemplateArgumentListInfo*
readASTTemplateArgumentListInfo();
/// Reads a declarator info from the given record, advancing Idx.
TypeSourceInfo *readTypeSourceInfo();
/// Reads the location information for a type.
void readTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
/// Map a local type ID within a given AST file to a global type ID.
serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
return Reader->getGlobalTypeID(*F, LocalID);
}
Qualifiers readQualifiers() {
return Qualifiers::fromOpaqueValue(readInt());
}
/// Read a type from the current position in the record.
QualType readType() {
return Reader->readType(*F, Record, Idx);
}
QualType readQualType() {
return readType();
}
/// Reads a declaration ID from the given position in this record.
///
/// \returns The declaration ID read from the record, adjusted to a global ID.
serialization::DeclID readDeclID() {
return Reader->ReadDeclID(*F, Record, Idx);
}
/// Reads a declaration from the given position in a record in the
/// given module, advancing Idx.
Decl *readDecl() {
return Reader->ReadDecl(*F, Record, Idx);
}
Decl *readDeclRef() {
return readDecl();
}
/// Reads a declaration from the given position in the record,
/// advancing Idx.
///
/// \returns The declaration read from this location, casted to the given
/// result type.
template<typename T>
T *readDeclAs() {
return Reader->ReadDeclAs<T>(*F, Record, Idx);
}
IdentifierInfo *readIdentifier() {
return Reader->readIdentifier(*F, Record, Idx);
}
/// Read a selector from the Record, advancing Idx.
Selector readSelector() {
return Reader->ReadSelector(*F, Record, Idx);
}
/// Read a declaration name, advancing Idx.
// DeclarationName readDeclarationName(); (inherited)
DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name);
DeclarationNameInfo readDeclarationNameInfo();
void readQualifierInfo(QualifierInfo &Info);
/// Return a nested name specifier, advancing Idx.
// NestedNameSpecifier *readNestedNameSpecifier(); (inherited)
NestedNameSpecifierLoc readNestedNameSpecifierLoc();
/// Read a template name, advancing Idx.
// TemplateName readTemplateName(); (inherited)
/// Read a template argument, advancing Idx. (inherited)
// TemplateArgument readTemplateArgument();
using DataStreamBasicReader::readTemplateArgument;
TemplateArgument readTemplateArgument(bool Canonicalize) {
TemplateArgument Arg = readTemplateArgument();
if (Canonicalize) {
Arg = getContext().getCanonicalTemplateArgument(Arg);
}
return Arg;
}
/// Read a template parameter list, advancing Idx.
TemplateParameterList *readTemplateParameterList();
/// Read a template argument array, advancing Idx.
void readTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
bool Canonicalize = false);
/// Read a UnresolvedSet structure, advancing Idx.
void readUnresolvedSet(LazyASTUnresolvedSet &Set);
/// Read a C++ base specifier, advancing Idx.
CXXBaseSpecifier readCXXBaseSpecifier();
/// Read a CXXCtorInitializer array, advancing Idx.
CXXCtorInitializer **readCXXCtorInitializers();
CXXTemporary *readCXXTemporary() {
return Reader->ReadCXXTemporary(*F, Record, Idx);
}
/// Read an OMPTraitInfo object, advancing Idx.
OMPTraitInfo *readOMPTraitInfo();
/// Read an OpenMP clause, advancing Idx.
OMPClause *readOMPClause();
/// Read an OpenMP children, advancing Idx.
void readOMPChildren(OMPChildren *Data);
/// Read a source location, advancing Idx.
SourceLocation readSourceLocation(LocSeq *Seq = nullptr) {
return Reader->ReadSourceLocation(*F, Record, Idx, Seq);
}
/// Read a source range, advancing Idx.
SourceRange readSourceRange(LocSeq *Seq = nullptr) {
return Reader->ReadSourceRange(*F, Record, Idx, Seq);
}
/// Read an arbitrary constant value, advancing Idx.
// APValue readAPValue(); (inherited)
/// Read an integral value, advancing Idx.
// llvm::APInt readAPInt(); (inherited)
/// Read a signed integral value, advancing Idx.
// llvm::APSInt readAPSInt(); (inherited)
/// Read a floating-point value, advancing Idx.
llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem);
/// Read a boolean value, advancing Idx.
bool readBool() { return readInt() != 0; }
/// Read a 32-bit unsigned value; required to satisfy BasicReader.
uint32_t readUInt32() {
return uint32_t(readInt());
}
/// Read a 64-bit unsigned value; required to satisfy BasicReader.
uint64_t readUInt64() {
return readInt();
}
/// Read a string, advancing Idx.
std::string readString() {
return Reader->ReadString(Record, Idx);
}
/// Read a path, advancing Idx.
std::string readPath() {
return Reader->ReadPath(*F, Record, Idx);
}
/// Read a version tuple, advancing Idx.
VersionTuple readVersionTuple() {
return ASTReader::ReadVersionTuple(Record, Idx);
}
/// Reads one attribute from the current stream position, advancing Idx.
Attr *readAttr();
/// Reads attributes from the current stream position, advancing Idx.
void readAttributes(AttrVec &Attrs);
/// Read an BTFTypeTagAttr object.
BTFTypeTagAttr *readBTFTypeTagAttr() {
return cast<BTFTypeTagAttr>(readAttr());
}
/// Reads a token out of a record, advancing Idx.
Token readToken() {
return Reader->ReadToken(*F, Record, Idx);
}
void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Reader->RecordSwitchCaseID(SC, ID);
}
/// Retrieve the switch-case statement with the given ID.
SwitchCase *getSwitchCaseWithID(unsigned ID) {
return Reader->getSwitchCaseWithID(ID);
}
};
/// Helper class that saves the current stream position and
/// then restores it when destroyed.
struct SavedStreamPosition {
explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
: Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {}
~SavedStreamPosition() {
if (llvm::Error Err = Cursor.JumpToBit(Offset))
llvm::report_fatal_error(
llvm::Twine("Cursor should always be able to go back, failed: ") +
toString(std::move(Err)));
}
private:
llvm::BitstreamCursor &Cursor;
uint64_t Offset;
};
inline void PCHValidator::Error(const char *Msg) {
Reader.Error(Msg);
}
} // namespace clang
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|