Path: blob/main/contrib/llvm-project/clang/lib/Serialization/ASTReaderInternals.h
35233 views
//===- ASTReaderInternals.h - AST Reader Internals --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file provides internal definitions used in the AST reader.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H13#define LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H1415#include "MultiOnDiskHashTable.h"16#include "clang/AST/DeclarationName.h"17#include "clang/Basic/LLVM.h"18#include "clang/Serialization/ASTBitCodes.h"19#include "llvm/ADT/DenseSet.h"20#include "llvm/ADT/SmallVector.h"21#include "llvm/ADT/StringRef.h"22#include "llvm/Support/OnDiskHashTable.h"23#include <ctime>24#include <utility>2526namespace clang {2728class ASTReader;29class FileEntry;30struct HeaderFileInfo;31class HeaderSearch;32class ObjCMethodDecl;3334namespace serialization {3536class ModuleFile;3738namespace reader {3940/// Class that performs name lookup into a DeclContext stored41/// in an AST file.42class ASTDeclContextNameLookupTrait {43ASTReader &Reader;44ModuleFile &F;4546public:47// Maximum number of lookup tables we allow before condensing the tables.48static const int MaxTables = 4;4950/// The lookup result is a list of global declaration IDs.51using data_type = SmallVector<GlobalDeclID, 4>;5253struct data_type_builder {54data_type &Data;55llvm::DenseSet<GlobalDeclID> Found;5657data_type_builder(data_type &D) : Data(D) {}5859void insert(GlobalDeclID ID) {60// Just use a linear scan unless we have more than a few IDs.61if (Found.empty() && !Data.empty()) {62if (Data.size() <= 4) {63for (auto I : Found)64if (I == ID)65return;66Data.push_back(ID);67return;68}6970// Switch to tracking found IDs in the set.71Found.insert(Data.begin(), Data.end());72}7374if (Found.insert(ID).second)75Data.push_back(ID);76}77};78using hash_value_type = unsigned;79using offset_type = unsigned;80using file_type = ModuleFile *;8182using external_key_type = DeclarationName;83using internal_key_type = DeclarationNameKey;8485explicit ASTDeclContextNameLookupTrait(ASTReader &Reader, ModuleFile &F)86: Reader(Reader), F(F) {}8788static bool EqualKey(const internal_key_type &a, const internal_key_type &b) {89return a == b;90}9192static hash_value_type ComputeHash(const internal_key_type &Key) {93return Key.getHash();94}9596static internal_key_type GetInternalKey(const external_key_type &Name) {97return Name;98}99100static std::pair<unsigned, unsigned>101ReadKeyDataLength(const unsigned char *&d);102103internal_key_type ReadKey(const unsigned char *d, unsigned);104105void ReadDataInto(internal_key_type, const unsigned char *d,106unsigned DataLen, data_type_builder &Val);107108static void MergeDataInto(const data_type &From, data_type_builder &To) {109To.Data.reserve(To.Data.size() + From.size());110for (GlobalDeclID ID : From)111To.insert(ID);112}113114file_type ReadFileRef(const unsigned char *&d);115};116117struct DeclContextLookupTable {118MultiOnDiskHashTable<ASTDeclContextNameLookupTrait> Table;119};120121/// Base class for the trait describing the on-disk hash table for the122/// identifiers in an AST file.123///124/// This class is not useful by itself; rather, it provides common125/// functionality for accessing the on-disk hash table of identifiers126/// in an AST file. Different subclasses customize that functionality127/// based on what information they are interested in. Those subclasses128/// must provide the \c data_type type and the ReadData operation, only.129class ASTIdentifierLookupTraitBase {130public:131using external_key_type = StringRef;132using internal_key_type = StringRef;133using hash_value_type = unsigned;134using offset_type = unsigned;135136static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {137return a == b;138}139140static hash_value_type ComputeHash(const internal_key_type& a);141142static std::pair<unsigned, unsigned>143ReadKeyDataLength(const unsigned char*& d);144145// This hopefully will just get inlined and removed by the optimizer.146static const internal_key_type&147GetInternalKey(const external_key_type& x) { return x; }148149// This hopefully will just get inlined and removed by the optimizer.150static const external_key_type&151GetExternalKey(const internal_key_type& x) { return x; }152153static internal_key_type ReadKey(const unsigned char* d, unsigned n);154};155156/// Class that performs lookup for an identifier stored in an AST file.157class ASTIdentifierLookupTrait : public ASTIdentifierLookupTraitBase {158ASTReader &Reader;159ModuleFile &F;160161// If we know the IdentifierInfo in advance, it is here and we will162// not build a new one. Used when deserializing information about an163// identifier that was constructed before the AST file was read.164IdentifierInfo *KnownII;165166public:167using data_type = IdentifierInfo *;168169ASTIdentifierLookupTrait(ASTReader &Reader, ModuleFile &F,170IdentifierInfo *II = nullptr)171: Reader(Reader), F(F), KnownII(II) {}172173data_type ReadData(const internal_key_type& k,174const unsigned char* d,175unsigned DataLen);176177IdentifierID ReadIdentifierID(const unsigned char *d);178179ASTReader &getReader() const { return Reader; }180};181182/// The on-disk hash table used to contain information about183/// all of the identifiers in the program.184using ASTIdentifierLookupTable =185llvm::OnDiskIterableChainedHashTable<ASTIdentifierLookupTrait>;186187/// Class that performs lookup for a selector's entries in the global188/// method pool stored in an AST file.189class ASTSelectorLookupTrait {190ASTReader &Reader;191ModuleFile &F;192193public:194struct data_type {195SelectorID ID;196unsigned InstanceBits;197unsigned FactoryBits;198bool InstanceHasMoreThanOneDecl;199bool FactoryHasMoreThanOneDecl;200SmallVector<ObjCMethodDecl *, 2> Instance;201SmallVector<ObjCMethodDecl *, 2> Factory;202};203204using external_key_type = Selector;205using internal_key_type = external_key_type;206using hash_value_type = unsigned;207using offset_type = unsigned;208209ASTSelectorLookupTrait(ASTReader &Reader, ModuleFile &F)210: Reader(Reader), F(F) {}211212static bool EqualKey(const internal_key_type& a,213const internal_key_type& b) {214return a == b;215}216217static hash_value_type ComputeHash(Selector Sel);218219static const internal_key_type&220GetInternalKey(const external_key_type& x) { return x; }221222static std::pair<unsigned, unsigned>223ReadKeyDataLength(const unsigned char*& d);224225internal_key_type ReadKey(const unsigned char* d, unsigned);226data_type ReadData(Selector, const unsigned char* d, unsigned DataLen);227};228229/// The on-disk hash table used for the global method pool.230using ASTSelectorLookupTable =231llvm::OnDiskChainedHashTable<ASTSelectorLookupTrait>;232233/// Trait class used to search the on-disk hash table containing all of234/// the header search information.235///236/// The on-disk hash table contains a mapping from each header path to237/// information about that header (how many times it has been included, its238/// controlling macro, etc.). Note that we actually hash based on the size239/// and mtime, and support "deep" comparisons of file names based on current240/// inode numbers, so that the search can cope with non-normalized path names241/// and symlinks.242class HeaderFileInfoTrait {243ASTReader &Reader;244ModuleFile &M;245HeaderSearch *HS;246const char *FrameworkStrings;247248public:249using external_key_type = FileEntryRef;250251struct internal_key_type {252off_t Size;253time_t ModTime;254StringRef Filename;255bool Imported;256};257258using internal_key_ref = const internal_key_type &;259260using data_type = HeaderFileInfo;261using hash_value_type = unsigned;262using offset_type = unsigned;263264HeaderFileInfoTrait(ASTReader &Reader, ModuleFile &M, HeaderSearch *HS,265const char *FrameworkStrings)266: Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings) {}267268static hash_value_type ComputeHash(internal_key_ref ikey);269internal_key_type GetInternalKey(external_key_type ekey);270bool EqualKey(internal_key_ref a, internal_key_ref b);271272static std::pair<unsigned, unsigned>273ReadKeyDataLength(const unsigned char*& d);274275static internal_key_type ReadKey(const unsigned char *d, unsigned);276277data_type ReadData(internal_key_ref,const unsigned char *d, unsigned DataLen);278279private:280const FileEntry *getFile(const internal_key_type &Key);281};282283/// The on-disk hash table used for known header files.284using HeaderFileInfoLookupTable =285llvm::OnDiskChainedHashTable<HeaderFileInfoTrait>;286287} // namespace reader288289} // namespace serialization290291} // namespace clang292293#endif // LLVM_CLANG_LIB_SERIALIZATION_ASTREADERINTERNALS_H294295296