Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:06

0001 //===- ASTRecordReader.h - Helper classes for reading AST -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines classes that are useful in the implementation of
0010 //  the ASTReader.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
0015 #define LLVM_CLANG_SERIALIZATION_ASTRECORDREADER_H
0016 
0017 #include "clang/AST/ASTContext.h"
0018 #include "clang/AST/AbstractBasicReader.h"
0019 #include "clang/Lex/Token.h"
0020 #include "clang/Serialization/ASTReader.h"
0021 #include "clang/Serialization/SourceLocationEncoding.h"
0022 #include "llvm/ADT/APFloat.h"
0023 #include "llvm/ADT/APInt.h"
0024 #include "llvm/ADT/APSInt.h"
0025 
0026 namespace clang {
0027 class OpenACCClause;
0028 class OMPTraitInfo;
0029 class OMPChildren;
0030 
0031 /// An object for streaming information from a record.
0032 class ASTRecordReader
0033     : public serialization::DataStreamBasicReader<ASTRecordReader> {
0034   using ModuleFile = serialization::ModuleFile;
0035   using LocSeq = SourceLocationSequence;
0036 
0037   ASTReader *Reader;
0038   ModuleFile *F;
0039   unsigned Idx = 0;
0040   ASTReader::RecordData Record;
0041 
0042   using RecordData = ASTReader::RecordData;
0043   using RecordDataImpl = ASTReader::RecordDataImpl;
0044 
0045 public:
0046   /// Construct an ASTRecordReader that uses the default encoding scheme.
0047   ASTRecordReader(ASTReader &Reader, ModuleFile &F)
0048     : DataStreamBasicReader(Reader.getContext()), Reader(&Reader), F(&F) {}
0049 
0050   /// Reads a record with id AbbrevID from Cursor, resetting the
0051   /// internal state.
0052   Expected<unsigned> readRecord(llvm::BitstreamCursor &Cursor,
0053                                 unsigned AbbrevID);
0054 
0055   /// Is this a module file for a module (rather than a PCH or similar).
0056   bool isModule() const { return F->isModule(); }
0057 
0058   /// Retrieve the AST context that this AST reader supplements.
0059   ASTContext &getContext() { return Reader->getContext(); }
0060 
0061   /// The current position in this record.
0062   unsigned getIdx() const { return Idx; }
0063 
0064   /// The length of this record.
0065   size_t size() const { return Record.size(); }
0066 
0067   /// An arbitrary index in this record.
0068   const uint64_t &operator[](size_t N) { return Record[N]; }
0069 
0070   /// Returns the last value in this record.
0071   uint64_t back() { return Record.back(); }
0072 
0073   /// Returns the current value in this record, and advances to the
0074   /// next value.
0075   uint64_t readInt() { return Record[Idx++]; }
0076 
0077   ArrayRef<uint64_t> readIntArray(unsigned Len) {
0078     auto Array = llvm::ArrayRef(Record).slice(Idx, Len);
0079     Idx += Len;
0080     return Array;
0081   }
0082 
0083   /// Returns the current value in this record, without advancing.
0084   uint64_t peekInt() { return Record[Idx]; }
0085 
0086   /// Skips the specified number of values.
0087   void skipInts(unsigned N) { Idx += N; }
0088 
0089   /// Retrieve the global submodule ID its local ID number.
0090   serialization::SubmoduleID
0091   getGlobalSubmoduleID(unsigned LocalID) {
0092     return Reader->getGlobalSubmoduleID(*F, LocalID);
0093   }
0094 
0095   /// Retrieve the submodule that corresponds to a global submodule ID.
0096   Module *getSubmodule(serialization::SubmoduleID GlobalID) {
0097     return Reader->getSubmodule(GlobalID);
0098   }
0099 
0100   /// Read the record that describes the lexical contents of a DC.
0101   bool readLexicalDeclContextStorage(uint64_t Offset, DeclContext *DC) {
0102     return Reader->ReadLexicalDeclContextStorage(*F, F->DeclsCursor, Offset,
0103                                                  DC);
0104   }
0105 
0106   ExplicitSpecifier readExplicitSpec() {
0107     uint64_t Kind = readInt();
0108     bool HasExpr = Kind & 0x1;
0109     Kind = Kind >> 1;
0110     return ExplicitSpecifier(HasExpr ? readExpr() : nullptr,
0111                              static_cast<ExplicitSpecKind>(Kind));
0112   }
0113 
0114   /// Read information about an exception specification (inherited).
0115   //FunctionProtoType::ExceptionSpecInfo
0116   //readExceptionSpecInfo(SmallVectorImpl<QualType> &ExceptionStorage);
0117 
0118   /// Get the global offset corresponding to a local offset.
0119   uint64_t getGlobalBitOffset(uint64_t LocalOffset) {
0120     return Reader->getGlobalBitOffset(*F, LocalOffset);
0121   }
0122 
0123   /// Reads a statement.
0124   Stmt *readStmt() { return Reader->ReadStmt(*F); }
0125   Stmt *readStmtRef() { return readStmt(); /* FIXME: readSubStmt? */ }
0126 
0127   /// Reads an expression.
0128   Expr *readExpr() { return Reader->ReadExpr(*F); }
0129 
0130   /// Reads a sub-statement operand during statement reading.
0131   Stmt *readSubStmt() { return Reader->ReadSubStmt(); }
0132 
0133   /// Reads a sub-expression operand during statement reading.
0134   Expr *readSubExpr() { return Reader->ReadSubExpr(); }
0135 
0136   /// Reads a declaration with the given local ID in the given module.
0137   ///
0138   /// \returns The requested declaration, casted to the given return type.
0139   template <typename T> T *GetLocalDeclAs(LocalDeclID LocalID) {
0140     return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
0141   }
0142 
0143   /// Reads a TemplateArgumentLocInfo appropriate for the
0144   /// given TemplateArgument kind, advancing Idx.
0145   TemplateArgumentLocInfo
0146   readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind);
0147 
0148   /// Reads a TemplateArgumentLoc, advancing Idx.
0149   TemplateArgumentLoc readTemplateArgumentLoc();
0150 
0151   void readTemplateArgumentListInfo(TemplateArgumentListInfo &Result);
0152 
0153   const ASTTemplateArgumentListInfo*
0154   readASTTemplateArgumentListInfo();
0155 
0156   // Reads a concept reference from the given record.
0157   ConceptReference *readConceptReference();
0158 
0159   /// Reads a declarator info from the given record, advancing Idx.
0160   TypeSourceInfo *readTypeSourceInfo();
0161 
0162   /// Reads the location information for a type.
0163   void readTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
0164 
0165   /// Map a local type ID within a given AST file to a global type ID.
0166   serialization::TypeID getGlobalTypeID(serialization::TypeID LocalID) const {
0167     return Reader->getGlobalTypeID(*F, LocalID);
0168   }
0169 
0170   Qualifiers readQualifiers() {
0171     return Qualifiers::fromOpaqueValue(readInt());
0172   }
0173 
0174   /// Read a type from the current position in the record.
0175   QualType readType() {
0176     return Reader->readType(*F, Record, Idx);
0177   }
0178   QualType readQualType() {
0179     return readType();
0180   }
0181 
0182   /// Reads a declaration ID from the given position in this record.
0183   ///
0184   /// \returns The declaration ID read from the record, adjusted to a global ID.
0185   GlobalDeclID readDeclID() { return Reader->ReadDeclID(*F, Record, Idx); }
0186 
0187   /// Reads a declaration from the given position in a record in the
0188   /// given module, advancing Idx.
0189   Decl *readDecl() {
0190     return Reader->ReadDecl(*F, Record, Idx);
0191   }
0192   Decl *readDeclRef() {
0193     return readDecl();
0194   }
0195 
0196   /// Reads a declaration from the given position in the record,
0197   /// advancing Idx.
0198   ///
0199   /// \returns The declaration read from this location, casted to the given
0200   /// result type.
0201   template<typename T>
0202   T *readDeclAs() {
0203     return Reader->ReadDeclAs<T>(*F, Record, Idx);
0204   }
0205 
0206   IdentifierInfo *readIdentifier() {
0207     return Reader->readIdentifier(*F, Record, Idx);
0208   }
0209 
0210   /// Read a selector from the Record, advancing Idx.
0211   Selector readSelector() {
0212     return Reader->ReadSelector(*F, Record, Idx);
0213   }
0214 
0215   TypeCoupledDeclRefInfo readTypeCoupledDeclRefInfo();
0216 
0217   /// Read a declaration name, advancing Idx.
0218   // DeclarationName readDeclarationName(); (inherited)
0219   DeclarationNameLoc readDeclarationNameLoc(DeclarationName Name);
0220   DeclarationNameInfo readDeclarationNameInfo();
0221 
0222   void readQualifierInfo(QualifierInfo &Info);
0223 
0224   /// Return a nested name specifier, advancing Idx.
0225   // NestedNameSpecifier *readNestedNameSpecifier(); (inherited)
0226 
0227   NestedNameSpecifierLoc readNestedNameSpecifierLoc();
0228 
0229   /// Read a template name, advancing Idx.
0230   // TemplateName readTemplateName(); (inherited)
0231 
0232   /// Read a template argument, advancing Idx. (inherited)
0233   // TemplateArgument readTemplateArgument();
0234   using DataStreamBasicReader::readTemplateArgument;
0235   TemplateArgument readTemplateArgument(bool Canonicalize) {
0236     TemplateArgument Arg = readTemplateArgument();
0237     if (Canonicalize) {
0238       Arg = getContext().getCanonicalTemplateArgument(Arg);
0239     }
0240     return Arg;
0241   }
0242 
0243   /// Read a template parameter list, advancing Idx.
0244   TemplateParameterList *readTemplateParameterList();
0245 
0246   /// Read a template argument array, advancing Idx.
0247   void readTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
0248                                 bool Canonicalize = false);
0249 
0250   /// Read a UnresolvedSet structure, advancing Idx.
0251   void readUnresolvedSet(LazyASTUnresolvedSet &Set);
0252 
0253   /// Read a C++ base specifier, advancing Idx.
0254   CXXBaseSpecifier readCXXBaseSpecifier();
0255 
0256   /// Read a CXXCtorInitializer array, advancing Idx.
0257   CXXCtorInitializer **readCXXCtorInitializers();
0258 
0259   CXXTemporary *readCXXTemporary() {
0260     return Reader->ReadCXXTemporary(*F, Record, Idx);
0261   }
0262 
0263   /// Read an OMPTraitInfo object, advancing Idx.
0264   OMPTraitInfo *readOMPTraitInfo();
0265 
0266   /// Read an OpenMP clause, advancing Idx.
0267   OMPClause *readOMPClause();
0268 
0269   /// Read an OpenMP children, advancing Idx.
0270   void readOMPChildren(OMPChildren *Data);
0271 
0272   /// Read a list of Exprs used for a var-list.
0273   llvm::SmallVector<Expr *> readOpenACCVarList();
0274 
0275   /// Read a list of Exprs used for a int-expr-list.
0276   llvm::SmallVector<Expr *> readOpenACCIntExprList();
0277 
0278   /// Read an OpenACC clause, advancing Idx.
0279   OpenACCClause *readOpenACCClause();
0280 
0281   /// Read a list of OpenACC clauses into the passed SmallVector.
0282   void readOpenACCClauseList(MutableArrayRef<const OpenACCClause *> Clauses);
0283 
0284   /// Read a source location, advancing Idx.
0285   SourceLocation readSourceLocation(LocSeq *Seq = nullptr) {
0286     return Reader->ReadSourceLocation(*F, Record, Idx, Seq);
0287   }
0288 
0289   /// Read a source range, advancing Idx.
0290   SourceRange readSourceRange(LocSeq *Seq = nullptr) {
0291     return Reader->ReadSourceRange(*F, Record, Idx, Seq);
0292   }
0293 
0294   /// Read an arbitrary constant value, advancing Idx.
0295   // APValue readAPValue(); (inherited)
0296 
0297   /// Read an integral value, advancing Idx.
0298   // llvm::APInt readAPInt(); (inherited)
0299 
0300   /// Read a signed integral value, advancing Idx.
0301   // llvm::APSInt readAPSInt(); (inherited)
0302 
0303   /// Read a floating-point value, advancing Idx.
0304   llvm::APFloat readAPFloat(const llvm::fltSemantics &Sem);
0305 
0306   /// Read a boolean value, advancing Idx.
0307   bool readBool() { return readInt() != 0; }
0308 
0309   /// Read a 32-bit unsigned value; required to satisfy BasicReader.
0310   uint32_t readUInt32() {
0311     return uint32_t(readInt());
0312   }
0313 
0314   /// Read a 64-bit unsigned value; required to satisfy BasicReader.
0315   uint64_t readUInt64() {
0316     return readInt();
0317   }
0318 
0319   /// Read a string, advancing Idx.
0320   std::string readString() {
0321     return Reader->ReadString(Record, Idx);
0322   }
0323 
0324   /// Read a path, advancing Idx.
0325   std::string readPath() {
0326     return Reader->ReadPath(*F, Record, Idx);
0327   }
0328 
0329   /// Read a version tuple, advancing Idx.
0330   VersionTuple readVersionTuple() {
0331     return ASTReader::ReadVersionTuple(Record, Idx);
0332   }
0333 
0334   /// Reads one attribute from the current stream position, advancing Idx.
0335   Attr *readAttr();
0336 
0337   /// Reads attributes from the current stream position, advancing Idx.
0338   void readAttributes(AttrVec &Attrs);
0339 
0340   /// Read an BTFTypeTagAttr object.
0341   BTFTypeTagAttr *readBTFTypeTagAttr() {
0342     return cast<BTFTypeTagAttr>(readAttr());
0343   }
0344 
0345   /// Reads a token out of a record, advancing Idx.
0346   Token readToken() {
0347     return Reader->ReadToken(*F, Record, Idx);
0348   }
0349 
0350   void recordSwitchCaseID(SwitchCase *SC, unsigned ID) {
0351     Reader->RecordSwitchCaseID(SC, ID);
0352   }
0353 
0354   /// Retrieve the switch-case statement with the given ID.
0355   SwitchCase *getSwitchCaseWithID(unsigned ID) {
0356     return Reader->getSwitchCaseWithID(ID);
0357   }
0358 };
0359 
0360 /// Helper class that saves the current stream position and
0361 /// then restores it when destroyed.
0362 struct SavedStreamPosition {
0363   explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
0364       : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) {}
0365 
0366   ~SavedStreamPosition() {
0367     if (llvm::Error Err = Cursor.JumpToBit(Offset))
0368       llvm::report_fatal_error(
0369           llvm::Twine("Cursor should always be able to go back, failed: ") +
0370           toString(std::move(Err)));
0371   }
0372 
0373 private:
0374   llvm::BitstreamCursor &Cursor;
0375   uint64_t Offset;
0376 };
0377 
0378 } // namespace clang
0379 
0380 #endif