clang  10.0.0git
ASTRecordReader.h
Go to the documentation of this file.
1 //===- ASTRecordReader.h - Helper classes for reading AST -------*- 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 classes that are useful in the implementation of
10 // the ASTReader.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
16 
18 #include "clang/Lex/Token.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/APInt.h"
22 #include "llvm/ADT/APSInt.h"
23 
24 namespace clang {
25 
26 /// An object for streaming information from a record.
28  : public serialization::DataStreamBasicReader<ASTRecordReader> {
30 
31  ASTReader *Reader;
32  ModuleFile *F;
33  unsigned Idx = 0;
34  ASTReader::RecordData Record;
35 
38 
39 public:
40  /// Construct an ASTRecordReader that uses the default encoding scheme.
42  : DataStreamBasicReader(Reader.getContext()), Reader(&Reader), F(&F) {}
43 
44  /// Reads a record with id AbbrevID from Cursor, resetting the
45  /// internal state.
46  Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor,
47  unsigned AbbrevID);
48 
49  /// Is this a module file for a module (rather than a PCH or similar).
50  bool isModule() const { return F->isModule(); }
51 
52  /// Retrieve the AST context that this AST reader supplements.
53  ASTContext &getContext() { return Reader->getContext(); }
54 
55  /// The current position in this record.
56  unsigned getIdx() const { return Idx; }
57 
58  /// The length of this record.
59  size_t size() const { return Record.size(); }
60 
61  /// An arbitrary index in this record.
62  const uint64_t &operator[](size_t N) { return Record[N]; }
63 
64  /// Returns the last value in this record.
65  uint64_t back() { return Record.back(); }
66 
67  /// Returns the current value in this record, and advances to the
68  /// next value.
69  uint64_t readInt() { return Record[Idx++]; }
70 
72  auto Array = llvm::makeArrayRef(Record).slice(Idx, Len);
73  Idx += Len;
74  return Array;
75  }
76 
77  /// Returns the current value in this record, without advancing.
78  uint64_t peekInt() { return Record[Idx]; }
79 
80  /// Skips the specified number of values.
81  void skipInts(unsigned N) { Idx += N; }
82 
83  /// Retrieve the global submodule ID its local ID number.
85  getGlobalSubmoduleID(unsigned LocalID) {
86  return Reader->getGlobalSubmoduleID(*F, LocalID);
87  }
88 
89  /// Retrieve the submodule that corresponds to a global submodule ID.
91  return Reader->getSubmodule(GlobalID);
92  }
93 
94  /// Read the record that describes the lexical contents of a DC.
96  return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
97  DC);
98  }
99 
100  /// Read the record that describes the visible contents of a DC.
103  return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset,
104  ID);
105  }
106 
108  uint64_t Kind = readInt();
109  bool HasExpr = Kind & 0x1;
110  Kind = Kind >> 1;
111  return ExplicitSpecifier(HasExpr ? readExpr() : nullptr,
112  static_cast<ExplicitSpecKind>(Kind));
113  }
114 
115  /// Read information about an exception specification (inherited).
116  //FunctionProtoType::ExceptionSpecInfo
117  //readExceptionSpecInfo(SmallVectorImpl<QualType> &ExceptionStorage);
118 
119  /// Get the global offset corresponding to a local offset.
120  uint64_t getGlobalBitOffset(uint32_t LocalOffset) {
121  return Reader->getGlobalBitOffset(*F, LocalOffset);
122  }
123 
124  /// Reads a statement.
125  Stmt *readStmt() { return Reader->ReadStmt(*F); }
126  Stmt *readStmtRef() { return readStmt(); /* FIXME: readSubStmt? */ }
127 
128  /// Reads an expression.
129  Expr *readExpr() { return Reader->ReadExpr(*F); }
130 
131  /// Reads a sub-statement operand during statement reading.
132  Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
133 
134  /// Reads a sub-expression operand during statement reading.
135  Expr *readSubExpr() { return Reader->ReadSubExpr(); }
136 
137  /// Reads a declaration with the given local ID in the given module.
138  ///
139  /// \returns The requested declaration, casted to the given return type.
140  template<typename T>
141  T *GetLocalDeclAs(uint32_t LocalID) {
142  return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
143  }
144 
145  /// Reads a TemplateArgumentLocInfo appropriate for the
146  /// given TemplateArgument kind, advancing Idx.
149 
150  /// Reads a TemplateArgumentLoc, advancing Idx.
152 
155 
156  /// Reads a declarator info from the given record, advancing Idx.
158 
159  /// Reads the location information for a type.
160  void readTypeLoc(TypeLoc TL);
161 
162 
163  /// Map a local type ID within a given AST file to a global type ID.
164  serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
165  return Reader->getGlobalTypeID(*F, LocalID);
166  }
167 
170  }
171 
172  /// Read a type from the current position in the record.
174  return Reader->readType(*F, Record, Idx);
175  }
177  return readType();
178  }
179 
180  /// Reads a declaration ID from the given position in this record.
181  ///
182  /// \returns The declaration ID read from the record, adjusted to a global ID.
184  return Reader->ReadDeclID(*F, Record, Idx);
185  }
186 
187  /// Reads a declaration from the given position in a record in the
188  /// given module, advancing Idx.
190  return Reader->ReadDecl(*F, Record, Idx);
191  }
193  return readDecl();
194  }
195 
196  /// Reads a declaration from the given position in the record,
197  /// advancing Idx.
198  ///
199  /// \returns The declaration read from this location, casted to the given
200  /// result type.
201  template<typename T>
202  T *readDeclAs() {
203  return Reader->ReadDeclAs<T>(*F, Record, Idx);
204  }
205 
207  return Reader->readIdentifier(*F, Record, Idx);
208  }
209 
210  /// Read a selector from the Record, advancing Idx.
212  return Reader->ReadSelector(*F, Record, Idx);
213  }
214 
215  /// Read a declaration name, advancing Idx.
216  // DeclarationName readDeclarationName(); (inherited)
219 
220  void readQualifierInfo(QualifierInfo &Info);
221 
222  /// Return a nested name specifier, advancing Idx.
223  // NestedNameSpecifier *readNestedNameSpecifier(); (inherited)
224 
226 
227  /// Read a template name, advancing Idx.
228  // TemplateName readTemplateName(); (inherited)
229 
230  /// Read a template argument, advancing Idx. (inherited)
231  // TemplateArgument readTemplateArgument();
232  using DataStreamBasicReader::readTemplateArgument;
235  if (Canonicalize) {
237  }
238  return Arg;
239  }
240 
241  /// Read a template parameter list, advancing Idx.
243 
244  /// Read a template argument array, advancing Idx.
246  bool Canonicalize = false);
247 
248  /// Read a UnresolvedSet structure, advancing Idx.
250 
251  /// Read a C++ base specifier, advancing Idx.
253 
254  /// Read a CXXCtorInitializer array, advancing Idx.
256 
258  return Reader->ReadCXXTemporary(*F, Record, Idx);
259  }
260 
261  /// Read an OpenMP clause, advancing Idx.
263 
264  /// Read a source location, advancing Idx.
266  return Reader->ReadSourceLocation(*F, Record, Idx);
267  }
268 
269  /// Read a source range, advancing Idx.
271  return Reader->ReadSourceRange(*F, Record, Idx);
272  }
273 
274  /// Read an arbitrary constant value, advancing Idx.
276 
277  /// Read an integral value, advancing Idx.
278  // llvm::APInt readAPInt(); (inherited)
279 
280  /// Read a signed integral value, advancing Idx.
281  // llvm::APSInt readAPSInt(); (inherited)
282 
283  /// Read a floating-point value, advancing Idx.
284  llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem);
285 
286  /// Read a boolean value, advancing Idx.
287  bool readBool() { return readInt() != 0; }
288 
289  /// Read a 32-bit unsigned value; required to satisfy BasicReader.
290  uint32_t readUInt32() {
291  return uint32_t(readInt());
292  }
293 
294  /// Read a 64-bit unsigned value; required to satisfy BasicReader.
295  uint64_t readUInt64() {
296  return readInt();
297  }
298 
299  /// Read a string, advancing Idx.
300  std::string readString() {
301  return Reader->ReadString(Record, Idx);
302  }
303 
304  /// Read a path, advancing Idx.
305  std::string readPath() {
306  return Reader->ReadPath(*F, Record, Idx);
307  }
308 
309  /// Read a version tuple, advancing Idx.
310  VersionTuple readVersionTuple() {
311  return ASTReader::ReadVersionTuple(Record, Idx);
312  }
313 
314  /// Reads one attribute from the current stream position, advancing Idx.
315  Attr *readAttr();
316 
317  /// Reads attributes from the current stream position, advancing Idx.
318  void readAttributes(AttrVec &Attrs);
319 
320  /// Reads a token out of a record, advancing Idx.
322  return Reader->ReadToken(*F, Record, Idx);
323  }
324 
325  void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
326  Reader->RecordSwitchCaseID(SC, ID);
327  }
328 
329  /// Retrieve the switch-case statement with the given ID.
331  return Reader->getSwitchCaseWithID(ID);
332  }
333 };
334 
335 /// Helper class that saves the current stream position and
336 /// then restores it when destroyed.
338  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
339  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {}
340 
342  if (llvm::Error Err = Cursor.JumpToBit(Offset))
343  llvm::report_fatal_error(
344  "Cursor should always be able to go back, failed: " +
345  toString(std::move(Err)));
346  }
347 
348 private:
349  llvm::BitstreamCursor &Cursor;
350  uint64_t Offset;
351 };
352 
353 inline void PCHValidator::Error(const char *Msg) {
354  Reader.Error(Msg);
355 }
356 
357 } // namespace clang
358 
359 #endif
Decl * GetLocalDecl(ModuleFile &F, uint32_t LocalID)
Reads a declaration with the given local ID in the given module.
Definition: ASTReader.h:1813
SourceLocation readSourceLocation()
Read a source location, advancing Idx.
Smart pointer class that efficiently represents Objective-C method names.
void readQualifierInfo(QualifierInfo &Info)
Definition: ASTReader.cpp:8617
A (possibly-)qualified type.
Definition: Type.h:654
CXXBaseSpecifier readCXXBaseSpecifier()
Read a C++ base specifier, advancing Idx.
Definition: ASTReader.cpp:8670
ASTRecordReader(ASTReader &Reader, ModuleFile &F)
Construct an ASTRecordReader that uses the default encoding scheme.
Stmt - This represents one statement.
Definition: Stmt.h:66
Module * getSubmodule(serialization::SubmoduleID GlobalID)
Retrieve the submodule that corresponds to a global submodule ID.
Definition: ASTReader.cpp:8444
SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const
Read a source location from raw form.
Definition: ASTReader.h:2107
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
void readUnresolvedSet(LazyASTUnresolvedSet &Set)
Read a UnresolvedSet structure, advancing Idx.
Definition: ASTReader.cpp:8659
A container of type source information.
Definition: Type.h:6227
Store information needed for an explicit specifier.
Definition: DeclCXX.h:1787
DeclarationNameInfo readDeclarationNameInfo()
Definition: ASTReader.cpp:8609
TemplateParameterList * readTemplateParameterList()
Read a template parameter list, advancing Idx.
Definition: ASTReader.cpp:8630
static Qualifiers fromOpaqueValue(unsigned opaque)
Definition: Type.h:249
void recordSwitchCaseID(SwitchCase *SC, unsigned ID)
const ASTTemplateArgumentListInfo * readASTTemplateArgumentListInfo()
Definition: ASTReader.cpp:7051
QualType readType()
Read a type from the current position in the record.
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:603
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:69
T * ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I)
Reads a declaration from the given position in a record in the given module.
Definition: ASTReader.h:1853
Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.h:2088
Module * getSubmodule(serialization::SubmoduleID GlobalID)
Retrieve the submodule that corresponds to a global submodule ID.
The collection of all-type qualifiers we support.
Definition: Type.h:143
An UnresolvedSet-like class that might not have been loaded from the external AST source yet...
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:58
serialization::TypeID getGlobalTypeID(unsigned LocalID) const
Map a local type ID within a given AST file to a global type ID.
TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) const
Retrieve the "canonical" template argument.
One of these records is kept for each identifier that is lexed.
T * readDeclAs()
Reads a declaration from the given position in the record, advancing Idx.
uint64_t readUInt64()
Read a 64-bit unsigned value; required to satisfy BasicReader.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
A C++ nested-name-specifier augmented with source location information.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
Expr * readExpr()
Reads an expression.
Helper class that saves the current stream position and then restores it when destroyed.
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
Stmt * ReadSubStmt()
Reads a sub-statement operand during statement reading.
Definition: ASTReader.h:2169
DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name)
Read a declaration name, advancing Idx.
Definition: ASTReader.cpp:8577
Describes a module or submodule.
Definition: Module.h:64
size_t size() const
The length of this record.
static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx)
Read a version tuple.
Definition: ASTReader.cpp:8906
uint32_t readUInt32()
Read a 32-bit unsigned value; required to satisfy BasicReader.
TemplateArgument readTemplateArgument(bool Canonicalize)
std::string ReadPath(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.cpp:8891
serialization::SubmoduleID getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID)
Retrieve the global submodule ID given a module and its local ID number.
Definition: ASTReader.cpp:8429
CXXTemporary * ReadCXXTemporary(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.cpp:8918
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
Definition: ASTReader.h:2214
SourceRange readSourceRange()
Read a source range, advancing Idx.
CXXCtorInitializer ** readCXXCtorInitializers()
Read a CXXCtorInitializer array, advancing Idx.
Definition: ASTReader.cpp:8685
static std::string ReadString(const RecordData &Record, unsigned &Idx)
Definition: ASTReader.cpp:8884
uint64_t back()
Returns the last value in this record.
DataStreamBasicReader provides convenience implementations for many BasicReader methods based on the ...
TypeSourceInfo * readTypeSourceInfo()
Reads a declarator info from the given record, advancing Idx.
Definition: ASTReader.cpp:6720
APValue readAPValue()
Read an arbitrary constant value, advancing Idx.
Definition: ASTReader.cpp:8838
Decl * ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I)
Reads a declaration from the given position in a record in the given module.
Definition: ASTReader.h:1843
std::string readPath()
Read a path, advancing Idx.
unsigned Offset
Definition: Format.cpp:1827
ArrayRef< uint64_t > readIntArray(unsigned Len)
serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const
Map a local type ID within a given AST file into a global type ID.
Definition: ASTReader.cpp:6990
This represents one expression.
Definition: Expr.h:108
SwitchCase * getSwitchCaseWithID(unsigned ID)
Retrieve the switch-case statement with the given ID.
Definition: ASTReader.cpp:8948
QualType readType(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Read a type from the current position in the given record, which was read from the given AST file...
Definition: ASTReader.h:1777
Stmt * ReadStmt(ModuleFile &F)
Reads a statement.
Stmt * readStmt()
Reads a statement.
Expected< unsigned > readRecord(llvm::BitstreamCursor &Cursor, unsigned AbbrevID)
Reads a record with id AbbrevID from Cursor, resetting the internal state.
ArgKind
The kind of template argument we&#39;re storing.
Definition: TemplateBase.h:53
SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Read a source range.
Definition: ASTReader.cpp:8812
Decl * readDecl()
Reads a declaration from the given position in a record in the given module, advancing Idx...
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:107
OMPClause * readOMPClause()
Read an OpenMP clause, advancing Idx.
SavedStreamPosition(llvm::BitstreamCursor &Cursor)
std::string readString()
Read a string, advancing Idx.
void skipInts(unsigned N)
Skips the specified number of values.
const uint64_t & operator[](size_t N)
An arbitrary index in this record.
bool isModule() const
Is this a module file for a module (rather than a PCH or similar).
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
Definition: ASTBitCodes.h:171
A struct with extended info about a syntactic name qualifier, to be used for the case of out-of-line ...
Definition: Decl.h:644
Kind
CXXTemporary * readCXXTemporary()
Encodes a location in the source.
Stmt * readSubStmt()
Reads a sub-statement operand during statement reading.
Represents a C++ temporary.
Definition: ExprCXX.h:1341
IdentifierInfo * readIdentifier(ModuleFile &M, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.h:2018
bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC)
Read the record that describes the lexical contents of a DC.
void readAttributes(AttrVec &Attrs)
Reads attributes from the current stream position, advancing Idx.
bool isModule() const
Is this a module file for a module (rather than a PCH or similar).
Definition: ModuleFile.h:479
Attr * readAttr()
Reads one attribute from the current stream position, advancing Idx.
Expr * readSubExpr()
Reads a sub-expression operand during statement reading.
T * GetLocalDeclAs(uint32_t LocalID)
Reads a declaration with the given local ID in the given module.
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:51
unsigned getIdx() const
The current position in this record.
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
uint32_t TypeID
An ID number that refers to a type in an AST file.
Definition: ASTBitCodes.h:85
serialization::SubmoduleID getGlobalSubmoduleID(unsigned LocalID)
Retrieve the global submodule ID its local ID number.
serialization::DeclID readDeclID()
Reads a declaration ID from the given position in this record.
DeclarationNameLoc - Additional source/type location info for a declaration name. ...
Represents a template argument.
Definition: TemplateBase.h:50
Dataflow Directional Tag Classes.
llvm::BitstreamCursor DeclsCursor
DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block.
Definition: ModuleFile.h:408
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:340
Selector readSelector()
Read a selector from the Record, advancing Idx.
Expr * ReadExpr(ModuleFile &F)
Reads an expression.
IdentifierInfo * readIdentifier()
uint64_t peekInt()
Returns the current value in this record, without advancing.
The name of a declaration.
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspnd...
void readTypeLoc(TypeLoc TL)
Reads the location information for a type.
Definition: ASTReader.cpp:6714
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: ASTBitCodes.h:68
VersionTuple readVersionTuple()
Read a version tuple, advancing Idx.
uint64_t readInt()
Returns the current value in this record, and advances to the next value.
TemplateArgumentLocInfo readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind)
Reads a TemplateArgumentLocInfo appropriate for the given TemplateArgument kind, advancing Idx...
Definition: ASTReader.cpp:7009
SmallVector< uint64_t, 64 > RecordData
Definition: ASTReader.h:360
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:449
TemplateArgumentLoc readTemplateArgumentLoc()
Reads a TemplateArgumentLoc, advancing Idx.
Definition: ASTReader.cpp:7040
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2155
uint64_t getGlobalBitOffset(uint32_t LocalOffset)
Read information about an exception specification (inherited).
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:115
Represents a base class of a C++ class.
Definition: DeclCXX.h:145
SmallVectorImpl< uint64_t > RecordDataImpl
Definition: ASTReader.h:361
void readTemplateArgumentList(SmallVectorImpl< TemplateArgument > &TemplArgs, bool Canonicalize=false)
Read a template argument array, advancing Idx.
Definition: ASTReader.cpp:8649
serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Reads a declaration ID from the given position in a record in the given module.
Definition: ASTReader.cpp:7361
llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem)
Read an integral value, advancing Idx.
Definition: ASTReader.cpp:8879
Expr * ReadSubExpr()
Reads a sub-expression operand during statement reading.
Token ReadToken(ModuleFile &M, const RecordDataImpl &Record, unsigned &Idx)
Reads a token out of a record.
Definition: ASTReader.cpp:1662
Location information for a TemplateArgument.
Definition: TemplateBase.h:392
NestedNameSpecifierLoc readNestedNameSpecifierLoc()
Return a nested name specifier, advancing Idx.
Definition: ASTReader.cpp:8750
ExplicitSpecifier readExplicitSpec()
An object for streaming information from a record.
bool readVisibleDeclContextStorage(uint64_t Offset, serialization::DeclID ID)
Read the record that describes the visible contents of a DC.
bool readBool()
Read a boolean value, advancing Idx.
Token readToken()
Reads a token out of a record, advancing Idx.
A trivial tuple used to represent a source range.
SwitchCase * getSwitchCaseWithID(unsigned ID)
Retrieve the switch-case statement with the given ID.
Attr - This represents one attribute.
Definition: Attr.h:45
void RecordSwitchCaseID(SwitchCase *SC, unsigned ID)
Record that the given ID maps to the given switch-case statement.
Definition: ASTReader.cpp:8941