clang  12.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 
17 #include "clang/AST/ASTContext.h"
19 #include "clang/Lex/Token.h"
21 #include "llvm/ADT/APFloat.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/APSInt.h"
24 
25 namespace clang {
26 class OMPTraitInfo;
27 
28 /// An object for streaming information from a record.
30  : public serialization::DataStreamBasicReader<ASTRecordReader> {
32 
33  ASTReader *Reader;
34  ModuleFile *F;
35  unsigned Idx = 0;
36  ASTReader::RecordData Record;
37 
40 
41 public:
42  /// Construct an ASTRecordReader that uses the default encoding scheme.
44  : DataStreamBasicReader(Reader.getContext()), Reader(&Reader), F(&F) {}
45 
46  /// Reads a record with id AbbrevID from Cursor, resetting the
47  /// internal state.
48  Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor,
49  unsigned AbbrevID);
50 
51  /// Is this a module file for a module (rather than a PCH or similar).
52  bool isModule() const { return F->isModule(); }
53 
54  /// Retrieve the AST context that this AST reader supplements.
55  ASTContext &getContext() { return Reader->getContext(); }
56 
57  /// The current position in this record.
58  unsigned getIdx() const { return Idx; }
59 
60  /// The length of this record.
61  size_t size() const { return Record.size(); }
62 
63  /// An arbitrary index in this record.
64  const uint64_t &operator[](size_t N) { return Record[N]; }
65 
66  /// Returns the last value in this record.
67  uint64_t back() { return Record.back(); }
68 
69  /// Returns the current value in this record, and advances to the
70  /// next value.
71  uint64_t readInt() { return Record[Idx++]; }
72 
74  auto Array = llvm::makeArrayRef(Record).slice(Idx, Len);
75  Idx += Len;
76  return Array;
77  }
78 
79  /// Returns the current value in this record, without advancing.
80  uint64_t peekInt() { return Record[Idx]; }
81 
82  /// Skips the specified number of values.
83  void skipInts(unsigned N) { Idx += N; }
84 
85  /// Retrieve the global submodule ID its local ID number.
87  getGlobalSubmoduleID(unsigned LocalID) {
88  return Reader->getGlobalSubmoduleID(*F, LocalID);
89  }
90 
91  /// Retrieve the submodule that corresponds to a global submodule ID.
93  return Reader->getSubmodule(GlobalID);
94  }
95 
96  /// Read the record that describes the lexical contents of a DC.
98  return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
99  DC);
100  }
101 
102  /// Read the record that describes the visible contents of a DC.
105  return Reader->ReadVisibleDeclContextStorage(*F, F->DeclsCursor, Offset,
106  ID);
107  }
108 
110  uint64_t Kind = readInt();
111  bool HasExpr = Kind & 0x1;
112  Kind = Kind >> 1;
113  return ExplicitSpecifier(HasExpr ? readExpr() : nullptr,
114  static_cast<ExplicitSpecKind>(Kind));
115  }
116 
117  /// Read information about an exception specification (inherited).
118  //FunctionProtoType::ExceptionSpecInfo
119  //readExceptionSpecInfo(SmallVectorImpl<QualType> &ExceptionStorage);
120 
121  /// Get the global offset corresponding to a local offset.
122  uint64_t getGlobalBitOffset(uint64_t LocalOffset) {
123  return Reader->getGlobalBitOffset(*F, LocalOffset);
124  }
125 
126  /// Reads a statement.
127  Stmt *readStmt() { return Reader->ReadStmt(*F); }
128  Stmt *readStmtRef() { return readStmt(); /* FIXME: readSubStmt? */ }
129 
130  /// Reads an expression.
131  Expr *readExpr() { return Reader->ReadExpr(*F); }
132 
133  /// Reads a sub-statement operand during statement reading.
134  Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
135 
136  /// Reads a sub-expression operand during statement reading.
137  Expr *readSubExpr() { return Reader->ReadSubExpr(); }
138 
139  /// Reads a declaration with the given local ID in the given module.
140  ///
141  /// \returns The requested declaration, casted to the given return type.
142  template<typename T>
143  T *GetLocalDeclAs(uint32_t LocalID) {
144  return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
145  }
146 
147  /// Reads a TemplateArgumentLocInfo appropriate for the
148  /// given TemplateArgument kind, advancing Idx.
151 
152  /// Reads a TemplateArgumentLoc, advancing Idx.
154 
157 
158  /// Reads a declarator info from the given record, advancing Idx.
160 
161  /// Reads the location information for a type.
162  void readTypeLoc(TypeLoc TL);
163 
164 
165  /// Map a local type ID within a given AST file to a global type ID.
166  serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
167  return Reader->getGlobalTypeID(*F, LocalID);
168  }
169 
172  }
173 
174  /// Read a type from the current position in the record.
176  return Reader->readType(*F, Record, Idx);
177  }
179  return readType();
180  }
181 
182  /// Reads a declaration ID from the given position in this record.
183  ///
184  /// \returns The declaration ID read from the record, adjusted to a global ID.
186  return Reader->ReadDeclID(*F, Record, Idx);
187  }
188 
189  /// Reads a declaration from the given position in a record in the
190  /// given module, advancing Idx.
192  return Reader->ReadDecl(*F, Record, Idx);
193  }
195  return readDecl();
196  }
197 
198  /// Reads a declaration from the given position in the record,
199  /// advancing Idx.
200  ///
201  /// \returns The declaration read from this location, casted to the given
202  /// result type.
203  template<typename T>
204  T *readDeclAs() {
205  return Reader->ReadDeclAs<T>(*F, Record, Idx);
206  }
207 
209  return Reader->readIdentifier(*F, Record, Idx);
210  }
211 
212  /// Read a selector from the Record, advancing Idx.
214  return Reader->ReadSelector(*F, Record, Idx);
215  }
216 
217  /// Read a declaration name, advancing Idx.
218  // DeclarationName readDeclarationName(); (inherited)
221 
222  void readQualifierInfo(QualifierInfo &Info);
223 
224  /// Return a nested name specifier, advancing Idx.
225  // NestedNameSpecifier *readNestedNameSpecifier(); (inherited)
226 
228 
229  /// Read a template name, advancing Idx.
230  // TemplateName readTemplateName(); (inherited)
231 
232  /// Read a template argument, advancing Idx. (inherited)
233  // TemplateArgument readTemplateArgument();
234  using DataStreamBasicReader::readTemplateArgument;
237  if (Canonicalize) {
239  }
240  return Arg;
241  }
242 
243  /// Read a template parameter list, advancing Idx.
245 
246  /// Read a template argument array, advancing Idx.
248  bool Canonicalize = false);
249 
250  /// Read a UnresolvedSet structure, advancing Idx.
252 
253  /// Read a C++ base specifier, advancing Idx.
255 
256  /// Read a CXXCtorInitializer array, advancing Idx.
258 
260  return Reader->ReadCXXTemporary(*F, Record, Idx);
261  }
262 
263  /// Read an OMPTraitInfo object, advancing Idx.
265 
266  /// Read an OpenMP clause, advancing Idx.
268 
269  /// Read a source location, advancing Idx.
271  return Reader->ReadSourceLocation(*F, Record, Idx);
272  }
273 
274  /// Read a source range, advancing Idx.
276  return Reader->ReadSourceRange(*F, Record, Idx);
277  }
278 
279  /// Read an arbitrary constant value, advancing Idx.
281 
282  /// Read an integral value, advancing Idx.
283  // llvm::APInt readAPInt(); (inherited)
284 
285  /// Read a signed integral value, advancing Idx.
286  // llvm::APSInt readAPSInt(); (inherited)
287 
288  /// Read a floating-point value, advancing Idx.
289  llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem);
290 
291  /// Read a boolean value, advancing Idx.
292  bool readBool() { return readInt() != 0; }
293 
294  /// Read a 32-bit unsigned value; required to satisfy BasicReader.
295  uint32_t readUInt32() {
296  return uint32_t(readInt());
297  }
298 
299  /// Read a 64-bit unsigned value; required to satisfy BasicReader.
300  uint64_t readUInt64() {
301  return readInt();
302  }
303 
304  /// Read a string, advancing Idx.
305  std::string readString() {
306  return Reader->ReadString(Record, Idx);
307  }
308 
309  /// Read a path, advancing Idx.
310  std::string readPath() {
311  return Reader->ReadPath(*F, Record, Idx);
312  }
313 
314  /// Read a version tuple, advancing Idx.
315  VersionTuple readVersionTuple() {
316  return ASTReader::ReadVersionTuple(Record, Idx);
317  }
318 
319  /// Reads one attribute from the current stream position, advancing Idx.
320  Attr *readAttr();
321 
322  /// Reads attributes from the current stream position, advancing Idx.
323  void readAttributes(AttrVec &Attrs);
324 
325  /// Reads a token out of a record, advancing Idx.
327  return Reader->ReadToken(*F, Record, Idx);
328  }
329 
330  void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
331  Reader->RecordSwitchCaseID(SC, ID);
332  }
333 
334  /// Retrieve the switch-case statement with the given ID.
336  return Reader->getSwitchCaseWithID(ID);
337  }
338 };
339 
340 /// Helper class that saves the current stream position and
341 /// then restores it when destroyed.
343  explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
344  : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {}
345 
347  if (llvm::Error Err = Cursor.JumpToBit(Offset))
348  llvm::report_fatal_error(
349  "Cursor should always be able to go back, failed: " +
350  toString(std::move(Err)));
351  }
352 
353 private:
354  llvm::BitstreamCursor &Cursor;
355  uint64_t Offset;
356 };
357 
358 inline void PCHValidator::Error(const char *Msg) {
359  Reader.Error(Msg);
360 }
361 
362 } // namespace clang
363 
364 #endif
Defines the clang::ASTContext interface.
Decl * GetLocalDecl(ModuleFile &F, uint32_t LocalID)
Reads a declaration with the given local ID in the given module.
Definition: ASTReader.h:1832
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:8748
A (possibly-)qualified type.
Definition: Type.h:655
CXXBaseSpecifier readCXXBaseSpecifier()
Read a C++ base specifier, advancing Idx.
Definition: ASTReader.cpp:8801
ASTRecordReader(ASTReader &Reader, ModuleFile &F)
Construct an ASTRecordReader that uses the default encoding scheme.
Stmt - This represents one statement.
Definition: Stmt.h:68
Module * getSubmodule(serialization::SubmoduleID GlobalID)
Retrieve the submodule that corresponds to a global submodule ID.
Definition: ASTReader.cpp:8575
SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const
Read a source location from raw form.
Definition: ASTReader.h:2130
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
void readUnresolvedSet(LazyASTUnresolvedSet &Set)
Read a UnresolvedSet structure, advancing Idx.
Definition: ASTReader.cpp:8790
A container of type source information.
Definition: Type.h:6373
Store information needed for an explicit specifier.
Definition: DeclCXX.h:1816
DeclarationNameInfo readDeclarationNameInfo()
Definition: ASTReader.cpp:8740
TemplateParameterList * readTemplateParameterList()
Read a template parameter list, advancing Idx.
Definition: ASTReader.cpp:8761
static Qualifiers fromOpaqueValue(unsigned opaque)
Definition: Type.h:250
void recordSwitchCaseID(SwitchCase *SC, unsigned ID)
const ASTTemplateArgumentListInfo * readASTTemplateArgumentListInfo()
Definition: ASTReader.cpp:7135
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:605
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:1872
Selector ReadSelector(ModuleFile &M, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.h:2111
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:144
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:174
A C++ nested-name-specifier augmented with source location information.
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:2192
DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name)
Read a declaration name, advancing Idx.
Definition: ASTReader.cpp:8708
Describes a module or submodule.
Definition: Module.h:87
size_t size() const
The length of this record.
static VersionTuple ReadVersionTuple(const RecordData &Record, unsigned &Idx)
Read a version tuple.
Definition: ASTReader.cpp:9037
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:9022
serialization::SubmoduleID getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID)
Retrieve the global submodule ID given a module and its local ID number.
Definition: ASTReader.cpp:8560
CXXTemporary * ReadCXXTemporary(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.cpp:9049
ASTContext & getContext()
Retrieve the AST context that this AST reader supplements.
Definition: ASTReader.h:2237
SourceRange readSourceRange()
Read a source range, advancing Idx.
CXXCtorInitializer ** readCXXCtorInitializers()
Read a CXXCtorInitializer array, advancing Idx.
Definition: ASTReader.cpp:8816
static std::string ReadString(const RecordData &Record, unsigned &Idx)
Definition: ASTReader.cpp:9015
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:6792
APValue readAPValue()
Read an arbitrary constant value, advancing Idx.
Definition: ASTReader.cpp:8969
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:1862
std::string readPath()
Read a path, advancing Idx.
unsigned Offset
Definition: Format.cpp:2020
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:7074
This represents one expression.
Definition: Expr.h:110
SwitchCase * getSwitchCaseWithID(unsigned ID)
Retrieve the switch-case statement with the given ID.
Definition: ASTReader.cpp:9079
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:1796
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:54
SourceRange ReadSourceRange(ModuleFile &F, const RecordData &Record, unsigned &Idx)
Read a source range.
Definition: ASTReader.cpp:8943
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:106
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).
OMPTraitInfo * readOMPTraitInfo()
Read an OMPTraitInfo object, advancing Idx.
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:1346
IdentifierInfo * readIdentifier(ModuleFile &M, const RecordData &Record, unsigned &Idx)
Definition: ASTReader.h:2041
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:499
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:52
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:51
Dataflow Directional Tag Classes.
llvm::BitstreamCursor DeclsCursor
DeclsCursor - This is a cursor to the start of the DECLTYPES_BLOCK block.
Definition: ModuleFile.h:425
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1303
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:6786
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:7093
std::string toString(const til::SExpr *E)
SmallVector< uint64_t, 64 > RecordData
Definition: ASTReader.h:360
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:451
TemplateArgumentLoc readTemplateArgumentLoc()
Reads a TemplateArgumentLoc, advancing Idx.
Definition: ASTReader.cpp:7124
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2185
Helper data structure representing the traits in a match clause of an declare variant or metadirectiv...
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:146
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:8780
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:7448
llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem)
Read an integral value, advancing Idx.
Definition: ASTReader.cpp:9010
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:1670
Location information for a TemplateArgument.
Definition: TemplateBase.h:394
NestedNameSpecifierLoc readNestedNameSpecifierLoc()
Return a nested name specifier, advancing Idx.
Definition: ASTReader.cpp:8881
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.
uint64_t getGlobalBitOffset(uint64_t LocalOffset)
Read information about an exception specification (inherited).
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:46
void RecordSwitchCaseID(SwitchCase *SC, unsigned ID)
Record that the given ID maps to the given switch-case statement.
Definition: ASTReader.cpp:9072