ASTReader.cpp revision c32647d111e516593b9ba242cad7b8ff4016c155
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines the ASTReader class, which reads AST files. 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Serialization/ASTReader.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Serialization/ASTDeserializationListener.h" 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ASTCommon.h" 17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "clang/Frontend/FrontendDiagnostic.h" 1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "clang/Frontend/Utils.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Sema/Sema.h" 202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Sema/Scope.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ASTConsumer.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ASTContext.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/DeclTemplate.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Expr.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/ExprCXX.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/Type.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/AST/TypeLocVisitor.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/MacroInfo.h" 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/PreprocessingRecord.h" 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/Preprocessor.h" 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Lex/HeaderSearch.h" 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/OnDiskHashTable.h" 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/SourceManager.h" 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/SourceManagerInternals.h" 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/FileManager.h" 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/FileSystemStatCache.h" 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/TargetInfo.h" 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/Version.h" 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/StringExtras.h" 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Bitcode/BitstreamReader.h" 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/Support/MemoryBuffer.h" 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h" 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/Path.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/system_error.h" 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <iterator> 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cstdio> 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/stat.h> 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace clang; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace clang::serialization; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PCH validator implementation 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ASTReaderListener::~ASTReaderListener() {} 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const LangOptions &PPLangOpts = PP.getLangOptions(); 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PARSE_LANGOPT_BENIGN(Option) 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \ 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (PPLangOpts.Option != LangOpts.Option) { \ 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Reader.Diag(DiagID) << LangOpts.Option << PPLangOpts.Option; \ 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; \ 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(Trigraphs); 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(BCPLComment); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(DollarIdents); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(AsmPreprocessor); 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(GNUKeywords, diag::warn_pch_gnu_keywords); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(ImplicitInt); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(Digraphs); 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(HexFloats); 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions); 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(MSCVersion); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus); 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(CXXOperatorName); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c); 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2); 85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi); 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI2, diag::warn_pch_nonfragile_abi2); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ObjCDefaultSynthProperties, 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_objc_auto_properties); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(NoConstantCFStrings, 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_no_constant_cfstrings); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(PascalStrings); 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(WritableStrings); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(LaxVectorConversions, 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_lax_vector_conversions); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(AltiVec, diag::warn_pch_altivec); 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions); 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(SjLjExceptions, diag::warn_pch_sjlj_exceptions); 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_thread_safe_statics); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(POSIXThreads, diag::warn_pch_posix_threads); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(EmitAllDecls); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior()); 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(HeinousExtensions, 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_heinous_extensions); 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // FIXME: Most of the options below are benign if the macro wasn't 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // used. Unfortunately, this means that a PCH compiled without 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // optimization can't be used with optimization turned on, even 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // though the only thing that changes is whether __OPTIMIZE__ was 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // defined... but if __OPTIMIZE__ never showed up in the header, it 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // doesn't matter. We could consider making this some special kind 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of check. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control); 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar); 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ShortEnums, diag::warn_pch_short_enums); 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) { 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Reader.Diag(diag::warn_pch_gc_mode) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) << LangOpts.getGCMode() << PPLangOpts.getGCMode(); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(getVisibilityMode()); 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(getStackProtectorMode(), 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) diag::warn_pch_stack_protector); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(InstantiationDepth); 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(CUDA, diag::warn_pch_cuda); 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(CatchUndefined); 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PARSE_LANGOPT_BENIGN(SpellChecking); 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef PARSE_LANGOPT_IMPORTANT 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef PARSE_LANGOPT_BENIGN 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) { 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Triple == PP.getTargetInfo().getTriple().str()) 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return false; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Reader.Diag(diag::warn_pch_target_triple) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) << Triple << PP.getTargetInfo().getTriple().str(); 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct EmptyStringRef { 1583240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch bool operator ()(llvm::StringRef r) const { return r.empty(); } 1593240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch }; 1603240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch struct EmptyBlock { 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();} 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L, 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PCHPredefinesBlocks R) { 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // First, sum up the lengths. 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned LL = 0, RL = 0; 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (unsigned I = 0, N = L.size(); I != N; ++I) { 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LL += L[I].size(); 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (unsigned I = 0, N = R.size(); I != N; ++I) { 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) RL += R[I].Data.size(); 1743240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch } 1753240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch if (LL != RL) 1763240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch return false; 1773240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch if (LL == 0 && RL == 0) 1783240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch return true; 1793240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Kick out empty parts, they confuse the algorithm below. 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Do it the hard way. At this point, both vectors must be non-empty. 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm::StringRef LR = L[0], RR = R[0].Data; 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (void) RN; 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (;;) { 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Compare the current pieces. 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (LR.size() == RR.size()) { 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If they're the same length, it's pretty easy. 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (LR != RR) 193 return false; 194 // Both pieces are done, advance. 195 ++LI; 196 ++RI; 197 // If either string is done, they're both done, since they're the same 198 // length. 199 if (LI == LN) { 200 assert(RI == RN && "Strings not the same length after all?"); 201 return true; 202 } 203 LR = L[LI]; 204 RR = R[RI].Data; 205 } else if (LR.size() < RR.size()) { 206 // Right piece is longer. 207 if (!RR.startswith(LR)) 208 return false; 209 ++LI; 210 assert(LI != LN && "Strings not the same length after all?"); 211 RR = RR.substr(LR.size()); 212 LR = L[LI]; 213 } else { 214 // Left piece is longer. 215 if (!LR.startswith(RR)) 216 return false; 217 ++RI; 218 assert(RI != RN && "Strings not the same length after all?"); 219 LR = LR.substr(RR.size()); 220 RR = R[RI].Data; 221 } 222 } 223} 224 225static std::pair<FileID, llvm::StringRef::size_type> 226FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) { 227 std::pair<FileID, llvm::StringRef::size_type> Res; 228 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { 229 Res.second = Buffers[I].Data.find(MacroDef); 230 if (Res.second != llvm::StringRef::npos) { 231 Res.first = Buffers[I].BufferID; 232 break; 233 } 234 } 235 return Res; 236} 237 238bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, 239 llvm::StringRef OriginalFileName, 240 std::string &SuggestedPredefines) { 241 // We are in the context of an implicit include, so the predefines buffer will 242 // have a #include entry for the PCH file itself (as normalized by the 243 // preprocessor initialization). Find it and skip over it in the checking 244 // below. 245 llvm::SmallString<256> PCHInclude; 246 PCHInclude += "#include \""; 247 PCHInclude += NormalizeDashIncludePath(OriginalFileName); 248 PCHInclude += "\"\n"; 249 std::pair<llvm::StringRef,llvm::StringRef> Split = 250 llvm::StringRef(PP.getPredefines()).split(PCHInclude.str()); 251 llvm::StringRef Left = Split.first, Right = Split.second; 252 if (Left == PP.getPredefines()) { 253 Error("Missing PCH include entry!"); 254 return true; 255 } 256 257 // If the concatenation of all the PCH buffers is equal to the adjusted 258 // command line, we're done. 259 llvm::SmallVector<llvm::StringRef, 2> CommandLine; 260 CommandLine.push_back(Left); 261 CommandLine.push_back(Right); 262 if (EqualConcatenations(CommandLine, Buffers)) 263 return false; 264 265 SourceManager &SourceMgr = PP.getSourceManager(); 266 267 // The predefines buffers are different. Determine what the differences are, 268 // and whether they require us to reject the PCH file. 269 llvm::SmallVector<llvm::StringRef, 8> PCHLines; 270 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) 271 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 272 273 llvm::SmallVector<llvm::StringRef, 8> CmdLineLines; 274 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 275 276 // Pick out implicit #includes after the PCH and don't consider them for 277 // validation; we will insert them into SuggestedPredefines so that the 278 // preprocessor includes them. 279 std::string IncludesAfterPCH; 280 llvm::SmallVector<llvm::StringRef, 8> AfterPCHLines; 281 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 282 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) { 283 if (AfterPCHLines[i].startswith("#include ")) { 284 IncludesAfterPCH += AfterPCHLines[i]; 285 IncludesAfterPCH += '\n'; 286 } else { 287 CmdLineLines.push_back(AfterPCHLines[i]); 288 } 289 } 290 291 // Make sure we add the includes last into SuggestedPredefines before we 292 // exit this function. 293 struct AddIncludesRAII { 294 std::string &SuggestedPredefines; 295 std::string &IncludesAfterPCH; 296 297 AddIncludesRAII(std::string &SuggestedPredefines, 298 std::string &IncludesAfterPCH) 299 : SuggestedPredefines(SuggestedPredefines), 300 IncludesAfterPCH(IncludesAfterPCH) { } 301 ~AddIncludesRAII() { 302 SuggestedPredefines += IncludesAfterPCH; 303 } 304 } AddIncludes(SuggestedPredefines, IncludesAfterPCH); 305 306 // Sort both sets of predefined buffer lines, since we allow some extra 307 // definitions and they may appear at any point in the output. 308 std::sort(CmdLineLines.begin(), CmdLineLines.end()); 309 std::sort(PCHLines.begin(), PCHLines.end()); 310 311 // Determine which predefines that were used to build the PCH file are missing 312 // from the command line. 313 std::vector<llvm::StringRef> MissingPredefines; 314 std::set_difference(PCHLines.begin(), PCHLines.end(), 315 CmdLineLines.begin(), CmdLineLines.end(), 316 std::back_inserter(MissingPredefines)); 317 318 bool MissingDefines = false; 319 bool ConflictingDefines = false; 320 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { 321 llvm::StringRef Missing = MissingPredefines[I]; 322 if (Missing.startswith("#include ")) { 323 // An -include was specified when generating the PCH; it is included in 324 // the PCH, just ignore it. 325 continue; 326 } 327 if (!Missing.startswith("#define ")) { 328 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 329 return true; 330 } 331 332 // This is a macro definition. Determine the name of the macro we're 333 // defining. 334 std::string::size_type StartOfMacroName = strlen("#define "); 335 std::string::size_type EndOfMacroName 336 = Missing.find_first_of("( \n\r", StartOfMacroName); 337 assert(EndOfMacroName != std::string::npos && 338 "Couldn't find the end of the macro name"); 339 llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); 340 341 // Determine whether this macro was given a different definition on the 342 // command line. 343 std::string MacroDefStart = "#define " + MacroName.str(); 344 std::string::size_type MacroDefLen = MacroDefStart.size(); 345 llvm::SmallVector<llvm::StringRef, 8>::iterator ConflictPos 346 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), 347 MacroDefStart); 348 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) { 349 if (!ConflictPos->startswith(MacroDefStart)) { 350 // Different macro; we're done. 351 ConflictPos = CmdLineLines.end(); 352 break; 353 } 354 355 assert(ConflictPos->size() > MacroDefLen && 356 "Invalid #define in predefines buffer?"); 357 if ((*ConflictPos)[MacroDefLen] != ' ' && 358 (*ConflictPos)[MacroDefLen] != '(') 359 continue; // Longer macro name; keep trying. 360 361 // We found a conflicting macro definition. 362 break; 363 } 364 365 if (ConflictPos != CmdLineLines.end()) { 366 Reader.Diag(diag::warn_cmdline_conflicting_macro_def) 367 << MacroName; 368 369 // Show the definition of this macro within the PCH file. 370 std::pair<FileID, llvm::StringRef::size_type> MacroLoc = 371 FindMacro(Buffers, Missing); 372 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); 373 SourceLocation PCHMissingLoc = 374 SourceMgr.getLocForStartOfFile(MacroLoc.first) 375 .getFileLocWithOffset(MacroLoc.second); 376 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; 377 378 ConflictingDefines = true; 379 continue; 380 } 381 382 // If the macro doesn't conflict, then we'll just pick up the macro 383 // definition from the PCH file. Warn the user that they made a mistake. 384 if (ConflictingDefines) 385 continue; // Don't complain if there are already conflicting defs 386 387 if (!MissingDefines) { 388 Reader.Diag(diag::warn_cmdline_missing_macro_defs); 389 MissingDefines = true; 390 } 391 392 // Show the definition of this macro within the PCH file. 393 std::pair<FileID, llvm::StringRef::size_type> MacroLoc = 394 FindMacro(Buffers, Missing); 395 assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); 396 SourceLocation PCHMissingLoc = 397 SourceMgr.getLocForStartOfFile(MacroLoc.first) 398 .getFileLocWithOffset(MacroLoc.second); 399 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); 400 } 401 402 if (ConflictingDefines) 403 return true; 404 405 // Determine what predefines were introduced based on command-line 406 // parameters that were not present when building the PCH 407 // file. Extra #defines are okay, so long as the identifiers being 408 // defined were not used within the precompiled header. 409 std::vector<llvm::StringRef> ExtraPredefines; 410 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), 411 PCHLines.begin(), PCHLines.end(), 412 std::back_inserter(ExtraPredefines)); 413 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { 414 llvm::StringRef &Extra = ExtraPredefines[I]; 415 if (!Extra.startswith("#define ")) { 416 Reader.Diag(diag::warn_pch_compiler_options_mismatch); 417 return true; 418 } 419 420 // This is an extra macro definition. Determine the name of the 421 // macro we're defining. 422 std::string::size_type StartOfMacroName = strlen("#define "); 423 std::string::size_type EndOfMacroName 424 = Extra.find_first_of("( \n\r", StartOfMacroName); 425 assert(EndOfMacroName != std::string::npos && 426 "Couldn't find the end of the macro name"); 427 llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); 428 429 // Check whether this name was used somewhere in the PCH file. If 430 // so, defining it as a macro could change behavior, so we reject 431 // the PCH file. 432 if (IdentifierInfo *II = Reader.get(MacroName)) { 433 Reader.Diag(diag::warn_macro_name_used_in_pch) << II; 434 return true; 435 } 436 437 // Add this definition to the suggested predefines buffer. 438 SuggestedPredefines += Extra; 439 SuggestedPredefines += '\n'; 440 } 441 442 // If we get here, it's because the predefines buffer had compatible 443 // contents. Accept the PCH file. 444 return false; 445} 446 447void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI, 448 unsigned ID) { 449 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID); 450 ++NumHeaderInfos; 451} 452 453void PCHValidator::ReadCounter(unsigned Value) { 454 PP.setCounterValue(Value); 455} 456 457//===----------------------------------------------------------------------===// 458// AST reader implementation 459//===----------------------------------------------------------------------===// 460 461void 462ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) { 463 DeserializationListener = Listener; 464} 465 466 467namespace { 468class ASTSelectorLookupTrait { 469 ASTReader &Reader; 470 471public: 472 struct data_type { 473 SelectorID ID; 474 ObjCMethodList Instance, Factory; 475 }; 476 477 typedef Selector external_key_type; 478 typedef external_key_type internal_key_type; 479 480 explicit ASTSelectorLookupTrait(ASTReader &Reader) : Reader(Reader) { } 481 482 static bool EqualKey(const internal_key_type& a, 483 const internal_key_type& b) { 484 return a == b; 485 } 486 487 static unsigned ComputeHash(Selector Sel) { 488 return serialization::ComputeHash(Sel); 489 } 490 491 // This hopefully will just get inlined and removed by the optimizer. 492 static const internal_key_type& 493 GetInternalKey(const external_key_type& x) { return x; } 494 495 static std::pair<unsigned, unsigned> 496 ReadKeyDataLength(const unsigned char*& d) { 497 using namespace clang::io; 498 unsigned KeyLen = ReadUnalignedLE16(d); 499 unsigned DataLen = ReadUnalignedLE16(d); 500 return std::make_pair(KeyLen, DataLen); 501 } 502 503 internal_key_type ReadKey(const unsigned char* d, unsigned) { 504 using namespace clang::io; 505 SelectorTable &SelTable = Reader.getContext()->Selectors; 506 unsigned N = ReadUnalignedLE16(d); 507 IdentifierInfo *FirstII 508 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); 509 if (N == 0) 510 return SelTable.getNullarySelector(FirstII); 511 else if (N == 1) 512 return SelTable.getUnarySelector(FirstII); 513 514 llvm::SmallVector<IdentifierInfo *, 16> Args; 515 Args.push_back(FirstII); 516 for (unsigned I = 1; I != N; ++I) 517 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); 518 519 return SelTable.getSelector(N, Args.data()); 520 } 521 522 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) { 523 using namespace clang::io; 524 525 data_type Result; 526 527 Result.ID = ReadUnalignedLE32(d); 528 unsigned NumInstanceMethods = ReadUnalignedLE16(d); 529 unsigned NumFactoryMethods = ReadUnalignedLE16(d); 530 531 // Load instance methods 532 ObjCMethodList *Prev = 0; 533 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 534 ObjCMethodDecl *Method 535 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); 536 if (!Result.Instance.Method) { 537 // This is the first method, which is the easy case. 538 Result.Instance.Method = Method; 539 Prev = &Result.Instance; 540 continue; 541 } 542 543 ObjCMethodList *Mem = 544 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); 545 Prev->Next = new (Mem) ObjCMethodList(Method, 0); 546 Prev = Prev->Next; 547 } 548 549 // Load factory methods 550 Prev = 0; 551 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 552 ObjCMethodDecl *Method 553 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d))); 554 if (!Result.Factory.Method) { 555 // This is the first method, which is the easy case. 556 Result.Factory.Method = Method; 557 Prev = &Result.Factory; 558 continue; 559 } 560 561 ObjCMethodList *Mem = 562 Reader.getSema()->BumpAlloc.Allocate<ObjCMethodList>(); 563 Prev->Next = new (Mem) ObjCMethodList(Method, 0); 564 Prev = Prev->Next; 565 } 566 567 return Result; 568 } 569}; 570 571} // end anonymous namespace 572 573/// \brief The on-disk hash table used for the global method pool. 574typedef OnDiskChainedHashTable<ASTSelectorLookupTrait> 575 ASTSelectorLookupTable; 576 577namespace clang { 578class ASTIdentifierLookupTrait { 579 ASTReader &Reader; 580 ASTReader::PerFileData &F; 581 582 // If we know the IdentifierInfo in advance, it is here and we will 583 // not build a new one. Used when deserializing information about an 584 // identifier that was constructed before the AST file was read. 585 IdentifierInfo *KnownII; 586 587public: 588 typedef IdentifierInfo * data_type; 589 590 typedef const std::pair<const char*, unsigned> external_key_type; 591 592 typedef external_key_type internal_key_type; 593 594 ASTIdentifierLookupTrait(ASTReader &Reader, ASTReader::PerFileData &F, 595 IdentifierInfo *II = 0) 596 : Reader(Reader), F(F), KnownII(II) { } 597 598 static bool EqualKey(const internal_key_type& a, 599 const internal_key_type& b) { 600 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0 601 : false; 602 } 603 604 static unsigned ComputeHash(const internal_key_type& a) { 605 return llvm::HashString(llvm::StringRef(a.first, a.second)); 606 } 607 608 // This hopefully will just get inlined and removed by the optimizer. 609 static const internal_key_type& 610 GetInternalKey(const external_key_type& x) { return x; } 611 612 // This hopefully will just get inlined and removed by the optimizer. 613 static const external_key_type& 614 GetExternalKey(const internal_key_type& x) { return x; } 615 616 static std::pair<unsigned, unsigned> 617 ReadKeyDataLength(const unsigned char*& d) { 618 using namespace clang::io; 619 unsigned DataLen = ReadUnalignedLE16(d); 620 unsigned KeyLen = ReadUnalignedLE16(d); 621 return std::make_pair(KeyLen, DataLen); 622 } 623 624 static std::pair<const char*, unsigned> 625 ReadKey(const unsigned char* d, unsigned n) { 626 assert(n >= 2 && d[n-1] == '\0'); 627 return std::make_pair((const char*) d, n-1); 628 } 629 630 IdentifierInfo *ReadData(const internal_key_type& k, 631 const unsigned char* d, 632 unsigned DataLen) { 633 using namespace clang::io; 634 IdentID ID = ReadUnalignedLE32(d); 635 bool IsInteresting = ID & 0x01; 636 637 // Wipe out the "is interesting" bit. 638 ID = ID >> 1; 639 640 if (!IsInteresting) { 641 // For uninteresting identifiers, just build the IdentifierInfo 642 // and associate it with the persistent ID. 643 IdentifierInfo *II = KnownII; 644 if (!II) 645 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second); 646 Reader.SetIdentifierInfo(ID, II); 647 II->setIsFromAST(); 648 return II; 649 } 650 651 unsigned Bits = ReadUnalignedLE16(d); 652 bool CPlusPlusOperatorKeyword = Bits & 0x01; 653 Bits >>= 1; 654 bool HasRevertedTokenIDToIdentifier = Bits & 0x01; 655 Bits >>= 1; 656 bool Poisoned = Bits & 0x01; 657 Bits >>= 1; 658 bool ExtensionToken = Bits & 0x01; 659 Bits >>= 1; 660 bool hasMacroDefinition = Bits & 0x01; 661 Bits >>= 1; 662 unsigned ObjCOrBuiltinID = Bits & 0x3FF; 663 Bits >>= 10; 664 665 assert(Bits == 0 && "Extra bits in the identifier?"); 666 DataLen -= 6; 667 668 // Build the IdentifierInfo itself and link the identifier ID with 669 // the new IdentifierInfo. 670 IdentifierInfo *II = KnownII; 671 if (!II) 672 II = &Reader.getIdentifierTable().getOwn(k.first, k.first + k.second); 673 Reader.SetIdentifierInfo(ID, II); 674 675 // Set or check the various bits in the IdentifierInfo structure. 676 // Token IDs are read-only. 677 if (HasRevertedTokenIDToIdentifier) 678 II->RevertTokenIDToIdentifier(); 679 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 680 assert(II->isExtensionToken() == ExtensionToken && 681 "Incorrect extension token flag"); 682 (void)ExtensionToken; 683 II->setIsPoisoned(Poisoned); 684 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 685 "Incorrect C++ operator keyword flag"); 686 (void)CPlusPlusOperatorKeyword; 687 688 // If this identifier is a macro, deserialize the macro 689 // definition. 690 if (hasMacroDefinition) { 691 uint32_t Offset = ReadUnalignedLE32(d); 692 Reader.SetIdentifierIsMacro(II, F, Offset); 693 DataLen -= 4; 694 } 695 696 // Read all of the declarations visible at global scope with this 697 // name. 698 if (Reader.getContext() == 0) return II; 699 if (DataLen > 0) { 700 llvm::SmallVector<uint32_t, 4> DeclIDs; 701 for (; DataLen > 0; DataLen -= 4) 702 DeclIDs.push_back(ReadUnalignedLE32(d)); 703 Reader.SetGloballyVisibleDecls(II, DeclIDs); 704 } 705 706 II->setIsFromAST(); 707 return II; 708 } 709}; 710 711} // end anonymous namespace 712 713/// \brief The on-disk hash table used to contain information about 714/// all of the identifiers in the program. 715typedef OnDiskChainedHashTable<ASTIdentifierLookupTrait> 716 ASTIdentifierLookupTable; 717 718namespace { 719class ASTDeclContextNameLookupTrait { 720 ASTReader &Reader; 721 722public: 723 /// \brief Pair of begin/end iterators for DeclIDs. 724 typedef std::pair<DeclID *, DeclID *> data_type; 725 726 /// \brief Special internal key for declaration names. 727 /// The hash table creates keys for comparison; we do not create 728 /// a DeclarationName for the internal key to avoid deserializing types. 729 struct DeclNameKey { 730 DeclarationName::NameKind Kind; 731 uint64_t Data; 732 DeclNameKey() : Kind((DeclarationName::NameKind)0), Data(0) { } 733 }; 734 735 typedef DeclarationName external_key_type; 736 typedef DeclNameKey internal_key_type; 737 738 explicit ASTDeclContextNameLookupTrait(ASTReader &Reader) : Reader(Reader) { } 739 740 static bool EqualKey(const internal_key_type& a, 741 const internal_key_type& b) { 742 return a.Kind == b.Kind && a.Data == b.Data; 743 } 744 745 unsigned ComputeHash(const DeclNameKey &Key) const { 746 llvm::FoldingSetNodeID ID; 747 ID.AddInteger(Key.Kind); 748 749 switch (Key.Kind) { 750 case DeclarationName::Identifier: 751 case DeclarationName::CXXLiteralOperatorName: 752 ID.AddString(((IdentifierInfo*)Key.Data)->getName()); 753 break; 754 case DeclarationName::ObjCZeroArgSelector: 755 case DeclarationName::ObjCOneArgSelector: 756 case DeclarationName::ObjCMultiArgSelector: 757 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data))); 758 break; 759 case DeclarationName::CXXConstructorName: 760 case DeclarationName::CXXDestructorName: 761 case DeclarationName::CXXConversionFunctionName: 762 ID.AddInteger((TypeID)Key.Data); 763 break; 764 case DeclarationName::CXXOperatorName: 765 ID.AddInteger((OverloadedOperatorKind)Key.Data); 766 break; 767 case DeclarationName::CXXUsingDirective: 768 break; 769 } 770 771 return ID.ComputeHash(); 772 } 773 774 internal_key_type GetInternalKey(const external_key_type& Name) const { 775 DeclNameKey Key; 776 Key.Kind = Name.getNameKind(); 777 switch (Name.getNameKind()) { 778 case DeclarationName::Identifier: 779 Key.Data = (uint64_t)Name.getAsIdentifierInfo(); 780 break; 781 case DeclarationName::ObjCZeroArgSelector: 782 case DeclarationName::ObjCOneArgSelector: 783 case DeclarationName::ObjCMultiArgSelector: 784 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 785 break; 786 case DeclarationName::CXXConstructorName: 787 case DeclarationName::CXXDestructorName: 788 case DeclarationName::CXXConversionFunctionName: 789 Key.Data = Reader.GetTypeID(Name.getCXXNameType()); 790 break; 791 case DeclarationName::CXXOperatorName: 792 Key.Data = Name.getCXXOverloadedOperator(); 793 break; 794 case DeclarationName::CXXLiteralOperatorName: 795 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier(); 796 break; 797 case DeclarationName::CXXUsingDirective: 798 break; 799 } 800 801 return Key; 802 } 803 804 external_key_type GetExternalKey(const internal_key_type& Key) const { 805 ASTContext *Context = Reader.getContext(); 806 switch (Key.Kind) { 807 case DeclarationName::Identifier: 808 return DeclarationName((IdentifierInfo*)Key.Data); 809 810 case DeclarationName::ObjCZeroArgSelector: 811 case DeclarationName::ObjCOneArgSelector: 812 case DeclarationName::ObjCMultiArgSelector: 813 return DeclarationName(Selector(Key.Data)); 814 815 case DeclarationName::CXXConstructorName: 816 return Context->DeclarationNames.getCXXConstructorName( 817 Context->getCanonicalType(Reader.GetType(Key.Data))); 818 819 case DeclarationName::CXXDestructorName: 820 return Context->DeclarationNames.getCXXDestructorName( 821 Context->getCanonicalType(Reader.GetType(Key.Data))); 822 823 case DeclarationName::CXXConversionFunctionName: 824 return Context->DeclarationNames.getCXXConversionFunctionName( 825 Context->getCanonicalType(Reader.GetType(Key.Data))); 826 827 case DeclarationName::CXXOperatorName: 828 return Context->DeclarationNames.getCXXOperatorName( 829 (OverloadedOperatorKind)Key.Data); 830 831 case DeclarationName::CXXLiteralOperatorName: 832 return Context->DeclarationNames.getCXXLiteralOperatorName( 833 (IdentifierInfo*)Key.Data); 834 835 case DeclarationName::CXXUsingDirective: 836 return DeclarationName::getUsingDirectiveName(); 837 } 838 839 llvm_unreachable("Invalid Name Kind ?"); 840 } 841 842 static std::pair<unsigned, unsigned> 843 ReadKeyDataLength(const unsigned char*& d) { 844 using namespace clang::io; 845 unsigned KeyLen = ReadUnalignedLE16(d); 846 unsigned DataLen = ReadUnalignedLE16(d); 847 return std::make_pair(KeyLen, DataLen); 848 } 849 850 internal_key_type ReadKey(const unsigned char* d, unsigned) { 851 using namespace clang::io; 852 853 DeclNameKey Key; 854 Key.Kind = (DeclarationName::NameKind)*d++; 855 switch (Key.Kind) { 856 case DeclarationName::Identifier: 857 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); 858 break; 859 case DeclarationName::ObjCZeroArgSelector: 860 case DeclarationName::ObjCOneArgSelector: 861 case DeclarationName::ObjCMultiArgSelector: 862 Key.Data = 863 (uint64_t)Reader.DecodeSelector(ReadUnalignedLE32(d)).getAsOpaquePtr(); 864 break; 865 case DeclarationName::CXXConstructorName: 866 case DeclarationName::CXXDestructorName: 867 case DeclarationName::CXXConversionFunctionName: 868 Key.Data = ReadUnalignedLE32(d); // TypeID 869 break; 870 case DeclarationName::CXXOperatorName: 871 Key.Data = *d++; // OverloadedOperatorKind 872 break; 873 case DeclarationName::CXXLiteralOperatorName: 874 Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); 875 break; 876 case DeclarationName::CXXUsingDirective: 877 break; 878 } 879 880 return Key; 881 } 882 883 data_type ReadData(internal_key_type, const unsigned char* d, 884 unsigned DataLen) { 885 using namespace clang::io; 886 unsigned NumDecls = ReadUnalignedLE16(d); 887 DeclID *Start = (DeclID *)d; 888 return std::make_pair(Start, Start + NumDecls); 889 } 890}; 891 892} // end anonymous namespace 893 894/// \brief The on-disk hash table used for the DeclContext's Name lookup table. 895typedef OnDiskChainedHashTable<ASTDeclContextNameLookupTrait> 896 ASTDeclContextNameLookupTable; 897 898bool ASTReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor, 899 const std::pair<uint64_t, uint64_t> &Offsets, 900 DeclContextInfo &Info) { 901 SavedStreamPosition SavedPosition(Cursor); 902 // First the lexical decls. 903 if (Offsets.first != 0) { 904 Cursor.JumpToBit(Offsets.first); 905 906 RecordData Record; 907 const char *Blob; 908 unsigned BlobLen; 909 unsigned Code = Cursor.ReadCode(); 910 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 911 if (RecCode != DECL_CONTEXT_LEXICAL) { 912 Error("Expected lexical block"); 913 return true; 914 } 915 916 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob); 917 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair); 918 } else { 919 Info.LexicalDecls = 0; 920 Info.NumLexicalDecls = 0; 921 } 922 923 // Now the lookup table. 924 if (Offsets.second != 0) { 925 Cursor.JumpToBit(Offsets.second); 926 927 RecordData Record; 928 const char *Blob; 929 unsigned BlobLen; 930 unsigned Code = Cursor.ReadCode(); 931 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen); 932 if (RecCode != DECL_CONTEXT_VISIBLE) { 933 Error("Expected visible lookup table block"); 934 return true; 935 } 936 Info.NameLookupTableData 937 = ASTDeclContextNameLookupTable::Create( 938 (const unsigned char *)Blob + Record[0], 939 (const unsigned char *)Blob, 940 ASTDeclContextNameLookupTrait(*this)); 941 } else { 942 Info.NameLookupTableData = 0; 943 } 944 945 return false; 946} 947 948void ASTReader::Error(const char *Msg) { 949 Diag(diag::err_fe_pch_malformed) << Msg; 950} 951 952/// \brief Tell the AST listener about the predefines buffers in the chain. 953bool ASTReader::CheckPredefinesBuffers() { 954 if (Listener) 955 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, 956 ActualOriginalFileName, 957 SuggestedPredefines); 958 return false; 959} 960 961//===----------------------------------------------------------------------===// 962// Source Manager Deserialization 963//===----------------------------------------------------------------------===// 964 965/// \brief Read the line table in the source manager block. 966/// \returns true if there was an error. 967bool ASTReader::ParseLineTable(PerFileData &F, 968 llvm::SmallVectorImpl<uint64_t> &Record) { 969 unsigned Idx = 0; 970 LineTableInfo &LineTable = SourceMgr.getLineTable(); 971 972 // Parse the file names 973 std::map<int, int> FileIDs; 974 for (int I = 0, N = Record[Idx++]; I != N; ++I) { 975 // Extract the file name 976 unsigned FilenameLen = Record[Idx++]; 977 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen); 978 Idx += FilenameLen; 979 MaybeAddSystemRootToFilename(Filename); 980 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(), 981 Filename.size()); 982 } 983 984 // Parse the line entries 985 std::vector<LineEntry> Entries; 986 while (Idx < Record.size()) { 987 int FID = Record[Idx++]; 988 989 // Extract the line entries 990 unsigned NumEntries = Record[Idx++]; 991 assert(NumEntries && "Numentries is 00000"); 992 Entries.clear(); 993 Entries.reserve(NumEntries); 994 for (unsigned I = 0; I != NumEntries; ++I) { 995 unsigned FileOffset = Record[Idx++]; 996 unsigned LineNo = Record[Idx++]; 997 int FilenameID = FileIDs[Record[Idx++]]; 998 SrcMgr::CharacteristicKind FileKind 999 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1000 unsigned IncludeOffset = Record[Idx++]; 1001 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1002 FileKind, IncludeOffset)); 1003 } 1004 LineTable.AddEntry(FID, Entries); 1005 } 1006 1007 return false; 1008} 1009 1010namespace { 1011 1012class ASTStatData { 1013public: 1014 const ino_t ino; 1015 const dev_t dev; 1016 const mode_t mode; 1017 const time_t mtime; 1018 const off_t size; 1019 1020 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s) 1021 : ino(i), dev(d), mode(mo), mtime(m), size(s) {} 1022}; 1023 1024class ASTStatLookupTrait { 1025 public: 1026 typedef const char *external_key_type; 1027 typedef const char *internal_key_type; 1028 1029 typedef ASTStatData data_type; 1030 1031 static unsigned ComputeHash(const char *path) { 1032 return llvm::HashString(path); 1033 } 1034 1035 static internal_key_type GetInternalKey(const char *path) { return path; } 1036 1037 static bool EqualKey(internal_key_type a, internal_key_type b) { 1038 return strcmp(a, b) == 0; 1039 } 1040 1041 static std::pair<unsigned, unsigned> 1042 ReadKeyDataLength(const unsigned char*& d) { 1043 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d); 1044 unsigned DataLen = (unsigned) *d++; 1045 return std::make_pair(KeyLen + 1, DataLen); 1046 } 1047 1048 static internal_key_type ReadKey(const unsigned char *d, unsigned) { 1049 return (const char *)d; 1050 } 1051 1052 static data_type ReadData(const internal_key_type, const unsigned char *d, 1053 unsigned /*DataLen*/) { 1054 using namespace clang::io; 1055 1056 ino_t ino = (ino_t) ReadUnalignedLE32(d); 1057 dev_t dev = (dev_t) ReadUnalignedLE32(d); 1058 mode_t mode = (mode_t) ReadUnalignedLE16(d); 1059 time_t mtime = (time_t) ReadUnalignedLE64(d); 1060 off_t size = (off_t) ReadUnalignedLE64(d); 1061 return data_type(ino, dev, mode, mtime, size); 1062 } 1063}; 1064 1065/// \brief stat() cache for precompiled headers. 1066/// 1067/// This cache is very similar to the stat cache used by pretokenized 1068/// headers. 1069class ASTStatCache : public FileSystemStatCache { 1070 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy; 1071 CacheTy *Cache; 1072 1073 unsigned &NumStatHits, &NumStatMisses; 1074public: 1075 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base, 1076 unsigned &NumStatHits, unsigned &NumStatMisses) 1077 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) { 1078 Cache = CacheTy::Create(Buckets, Base); 1079 } 1080 1081 ~ASTStatCache() { delete Cache; } 1082 1083 LookupResult getStat(const char *Path, struct stat &StatBuf, 1084 int *FileDescriptor) { 1085 // Do the lookup for the file's data in the AST file. 1086 CacheTy::iterator I = Cache->find(Path); 1087 1088 // If we don't get a hit in the AST file just forward to 'stat'. 1089 if (I == Cache->end()) { 1090 ++NumStatMisses; 1091 return statChained(Path, StatBuf, FileDescriptor); 1092 } 1093 1094 ++NumStatHits; 1095 ASTStatData Data = *I; 1096 1097 StatBuf.st_ino = Data.ino; 1098 StatBuf.st_dev = Data.dev; 1099 StatBuf.st_mtime = Data.mtime; 1100 StatBuf.st_mode = Data.mode; 1101 StatBuf.st_size = Data.size; 1102 return CacheExists; 1103 } 1104}; 1105} // end anonymous namespace 1106 1107 1108/// \brief Read a source manager block 1109ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(PerFileData &F) { 1110 using namespace SrcMgr; 1111 1112 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1113 1114 // Set the source-location entry cursor to the current position in 1115 // the stream. This cursor will be used to read the contents of the 1116 // source manager block initially, and then lazily read 1117 // source-location entries as needed. 1118 SLocEntryCursor = F.Stream; 1119 1120 // The stream itself is going to skip over the source manager block. 1121 if (F.Stream.SkipBlock()) { 1122 Error("malformed block record in AST file"); 1123 return Failure; 1124 } 1125 1126 // Enter the source manager block. 1127 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1128 Error("malformed source manager block record in AST file"); 1129 return Failure; 1130 } 1131 1132 RecordData Record; 1133 while (true) { 1134 unsigned Code = SLocEntryCursor.ReadCode(); 1135 if (Code == llvm::bitc::END_BLOCK) { 1136 if (SLocEntryCursor.ReadBlockEnd()) { 1137 Error("error at end of Source Manager block in AST file"); 1138 return Failure; 1139 } 1140 return Success; 1141 } 1142 1143 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1144 // No known subblocks, always skip them. 1145 SLocEntryCursor.ReadSubBlockID(); 1146 if (SLocEntryCursor.SkipBlock()) { 1147 Error("malformed block record in AST file"); 1148 return Failure; 1149 } 1150 continue; 1151 } 1152 1153 if (Code == llvm::bitc::DEFINE_ABBREV) { 1154 SLocEntryCursor.ReadAbbrevRecord(); 1155 continue; 1156 } 1157 1158 // Read a record. 1159 const char *BlobStart; 1160 unsigned BlobLen; 1161 Record.clear(); 1162 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1163 default: // Default behavior: ignore. 1164 break; 1165 1166 case SM_LINE_TABLE: 1167 if (ParseLineTable(F, Record)) 1168 return Failure; 1169 break; 1170 1171 case SM_SLOC_FILE_ENTRY: 1172 case SM_SLOC_BUFFER_ENTRY: 1173 case SM_SLOC_INSTANTIATION_ENTRY: 1174 // Once we hit one of the source location entries, we're done. 1175 return Success; 1176 } 1177 } 1178} 1179 1180/// \brief Get a cursor that's correctly positioned for reading the source 1181/// location entry with the given ID. 1182ASTReader::PerFileData *ASTReader::SLocCursorForID(unsigned ID) { 1183 assert(ID != 0 && ID <= TotalNumSLocEntries && 1184 "SLocCursorForID should only be called for real IDs."); 1185 1186 ID -= 1; 1187 PerFileData *F = 0; 1188 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 1189 F = Chain[N - I - 1]; 1190 if (ID < F->LocalNumSLocEntries) 1191 break; 1192 ID -= F->LocalNumSLocEntries; 1193 } 1194 assert(F && F->LocalNumSLocEntries > ID && "Chain corrupted"); 1195 1196 F->SLocEntryCursor.JumpToBit(F->SLocOffsets[ID]); 1197 return F; 1198} 1199 1200/// \brief Read in the source location entry with the given ID. 1201ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(unsigned ID) { 1202 if (ID == 0) 1203 return Success; 1204 1205 if (ID > TotalNumSLocEntries) { 1206 Error("source location entry ID out-of-range for AST file"); 1207 return Failure; 1208 } 1209 1210 PerFileData *F = SLocCursorForID(ID); 1211 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1212 1213 ++NumSLocEntriesRead; 1214 unsigned Code = SLocEntryCursor.ReadCode(); 1215 if (Code == llvm::bitc::END_BLOCK || 1216 Code == llvm::bitc::ENTER_SUBBLOCK || 1217 Code == llvm::bitc::DEFINE_ABBREV) { 1218 Error("incorrectly-formatted source location entry in AST file"); 1219 return Failure; 1220 } 1221 1222 RecordData Record; 1223 const char *BlobStart; 1224 unsigned BlobLen; 1225 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1226 default: 1227 Error("incorrectly-formatted source location entry in AST file"); 1228 return Failure; 1229 1230 case SM_SLOC_FILE_ENTRY: { 1231 std::string Filename(BlobStart, BlobStart + BlobLen); 1232 MaybeAddSystemRootToFilename(Filename); 1233 const FileEntry *File = FileMgr.getFile(Filename); 1234 if (File == 0) { 1235 std::string ErrorStr = "could not find file '"; 1236 ErrorStr += Filename; 1237 ErrorStr += "' referenced by AST file"; 1238 Error(ErrorStr.c_str()); 1239 return Failure; 1240 } 1241 1242 if (Record.size() < 10) { 1243 Error("source location entry is incorrect"); 1244 return Failure; 1245 } 1246 1247 if (!DisableValidation && 1248 ((off_t)Record[4] != File->getSize() 1249#if !defined(LLVM_ON_WIN32) 1250 // In our regression testing, the Windows file system seems to 1251 // have inconsistent modification times that sometimes 1252 // erroneously trigger this error-handling path. 1253 || (time_t)Record[5] != File->getModificationTime() 1254#endif 1255 )) { 1256 Diag(diag::err_fe_pch_file_modified) 1257 << Filename; 1258 return Failure; 1259 } 1260 1261 FileID FID = SourceMgr.createFileID(File, ReadSourceLocation(*F, Record[1]), 1262 (SrcMgr::CharacteristicKind)Record[2], 1263 ID, Record[0]); 1264 if (Record[3]) 1265 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()) 1266 .setHasLineDirectives(); 1267 1268 // Reconstruct header-search information for this file. 1269 HeaderFileInfo HFI; 1270 HFI.isImport = Record[6]; 1271 HFI.DirInfo = Record[7]; 1272 HFI.NumIncludes = Record[8]; 1273 HFI.ControllingMacroID = Record[9]; 1274 if (Listener) 1275 Listener->ReadHeaderFileInfo(HFI, File->getUID()); 1276 break; 1277 } 1278 1279 case SM_SLOC_BUFFER_ENTRY: { 1280 const char *Name = BlobStart; 1281 unsigned Offset = Record[0]; 1282 unsigned Code = SLocEntryCursor.ReadCode(); 1283 Record.clear(); 1284 unsigned RecCode 1285 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen); 1286 1287 if (RecCode != SM_SLOC_BUFFER_BLOB) { 1288 Error("AST record has invalid code"); 1289 return Failure; 1290 } 1291 1292 llvm::MemoryBuffer *Buffer 1293 = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(BlobStart, BlobLen - 1), 1294 Name); 1295 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); 1296 1297 if (strcmp(Name, "<built-in>") == 0) { 1298 PCHPredefinesBlock Block = { 1299 BufferID, 1300 llvm::StringRef(BlobStart, BlobLen - 1) 1301 }; 1302 PCHPredefinesBuffers.push_back(Block); 1303 } 1304 1305 break; 1306 } 1307 1308 case SM_SLOC_INSTANTIATION_ENTRY: { 1309 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1310 SourceMgr.createInstantiationLoc(SpellingLoc, 1311 ReadSourceLocation(*F, Record[2]), 1312 ReadSourceLocation(*F, Record[3]), 1313 Record[4], 1314 ID, 1315 Record[0]); 1316 break; 1317 } 1318 } 1319 1320 return Success; 1321} 1322 1323/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1324/// specified cursor. Read the abbreviations that are at the top of the block 1325/// and then leave the cursor pointing into the block. 1326bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor, 1327 unsigned BlockID) { 1328 if (Cursor.EnterSubBlock(BlockID)) { 1329 Error("malformed block record in AST file"); 1330 return Failure; 1331 } 1332 1333 while (true) { 1334 uint64_t Offset = Cursor.GetCurrentBitNo(); 1335 unsigned Code = Cursor.ReadCode(); 1336 1337 // We expect all abbrevs to be at the start of the block. 1338 if (Code != llvm::bitc::DEFINE_ABBREV) { 1339 Cursor.JumpToBit(Offset); 1340 return false; 1341 } 1342 Cursor.ReadAbbrevRecord(); 1343 } 1344} 1345 1346PreprocessedEntity *ASTReader::ReadMacroRecord(PerFileData &F, uint64_t Offset) { 1347 assert(PP && "Forgot to set Preprocessor ?"); 1348 llvm::BitstreamCursor &Stream = F.MacroCursor; 1349 1350 // Keep track of where we are in the stream, then jump back there 1351 // after reading this macro. 1352 SavedStreamPosition SavedPosition(Stream); 1353 1354 Stream.JumpToBit(Offset); 1355 RecordData Record; 1356 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs; 1357 MacroInfo *Macro = 0; 1358 1359 while (true) { 1360 unsigned Code = Stream.ReadCode(); 1361 switch (Code) { 1362 case llvm::bitc::END_BLOCK: 1363 return 0; 1364 1365 case llvm::bitc::ENTER_SUBBLOCK: 1366 // No known subblocks, always skip them. 1367 Stream.ReadSubBlockID(); 1368 if (Stream.SkipBlock()) { 1369 Error("malformed block record in AST file"); 1370 return 0; 1371 } 1372 continue; 1373 1374 case llvm::bitc::DEFINE_ABBREV: 1375 Stream.ReadAbbrevRecord(); 1376 continue; 1377 default: break; 1378 } 1379 1380 // Read a record. 1381 const char *BlobStart = 0; 1382 unsigned BlobLen = 0; 1383 Record.clear(); 1384 PreprocessorRecordTypes RecType = 1385 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart, 1386 BlobLen); 1387 switch (RecType) { 1388 case PP_MACRO_OBJECT_LIKE: 1389 case PP_MACRO_FUNCTION_LIKE: { 1390 // If we already have a macro, that means that we've hit the end 1391 // of the definition of the macro we were looking for. We're 1392 // done. 1393 if (Macro) 1394 return 0; 1395 1396 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); 1397 if (II == 0) { 1398 Error("macro must have a name in AST file"); 1399 return 0; 1400 } 1401 SourceLocation Loc = ReadSourceLocation(F, Record[1]); 1402 bool isUsed = Record[2]; 1403 1404 MacroInfo *MI = PP->AllocateMacroInfo(Loc); 1405 MI->setIsUsed(isUsed); 1406 MI->setIsFromAST(); 1407 1408 unsigned NextIndex = 3; 1409 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1410 // Decode function-like macro info. 1411 bool isC99VarArgs = Record[3]; 1412 bool isGNUVarArgs = Record[4]; 1413 MacroArgs.clear(); 1414 unsigned NumArgs = Record[5]; 1415 NextIndex = 6 + NumArgs; 1416 for (unsigned i = 0; i != NumArgs; ++i) 1417 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); 1418 1419 // Install function-like macro info. 1420 MI->setIsFunctionLike(); 1421 if (isC99VarArgs) MI->setIsC99Varargs(); 1422 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1423 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(), 1424 PP->getPreprocessorAllocator()); 1425 } 1426 1427 // Finally, install the macro. 1428 PP->setMacroInfo(II, MI); 1429 1430 // Remember that we saw this macro last so that we add the tokens that 1431 // form its body to it. 1432 Macro = MI; 1433 1434 if (NextIndex + 1 == Record.size() && PP->getPreprocessingRecord()) { 1435 // We have a macro definition. Load it now. 1436 PP->getPreprocessingRecord()->RegisterMacroDefinition(Macro, 1437 getMacroDefinition(Record[NextIndex])); 1438 } 1439 1440 ++NumMacrosRead; 1441 break; 1442 } 1443 1444 case PP_TOKEN: { 1445 // If we see a TOKEN before a PP_MACRO_*, then the file is 1446 // erroneous, just pretend we didn't see this. 1447 if (Macro == 0) break; 1448 1449 Token Tok; 1450 Tok.startToken(); 1451 Tok.setLocation(ReadSourceLocation(F, Record[0])); 1452 Tok.setLength(Record[1]); 1453 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) 1454 Tok.setIdentifierInfo(II); 1455 Tok.setKind((tok::TokenKind)Record[3]); 1456 Tok.setFlag((Token::TokenFlags)Record[4]); 1457 Macro->AddTokenToBody(Tok); 1458 break; 1459 } 1460 1461 case PP_MACRO_INSTANTIATION: { 1462 // If we already have a macro, that means that we've hit the end 1463 // of the definition of the macro we were looking for. We're 1464 // done. 1465 if (Macro) 1466 return 0; 1467 1468 if (!PP->getPreprocessingRecord()) { 1469 Error("missing preprocessing record in AST file"); 1470 return 0; 1471 } 1472 1473 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); 1474 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0])) 1475 return PE; 1476 1477 MacroInstantiation *MI 1478 = new (PPRec) MacroInstantiation(DecodeIdentifierInfo(Record[3]), 1479 SourceRange(ReadSourceLocation(F, Record[1]), 1480 ReadSourceLocation(F, Record[2])), 1481 getMacroDefinition(Record[4])); 1482 PPRec.SetPreallocatedEntity(Record[0], MI); 1483 return MI; 1484 } 1485 1486 case PP_MACRO_DEFINITION: { 1487 // If we already have a macro, that means that we've hit the end 1488 // of the definition of the macro we were looking for. We're 1489 // done. 1490 if (Macro) 1491 return 0; 1492 1493 if (!PP->getPreprocessingRecord()) { 1494 Error("missing preprocessing record in AST file"); 1495 return 0; 1496 } 1497 1498 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); 1499 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0])) 1500 return PE; 1501 1502 if (Record[1] > MacroDefinitionsLoaded.size()) { 1503 Error("out-of-bounds macro definition record"); 1504 return 0; 1505 } 1506 1507 // Decode the identifier info and then check again; if the macro is 1508 // still defined and associated with the identifier, 1509 IdentifierInfo *II = DecodeIdentifierInfo(Record[4]); 1510 if (!MacroDefinitionsLoaded[Record[1] - 1]) { 1511 MacroDefinition *MD 1512 = new (PPRec) MacroDefinition(II, 1513 ReadSourceLocation(F, Record[5]), 1514 SourceRange( 1515 ReadSourceLocation(F, Record[2]), 1516 ReadSourceLocation(F, Record[3]))); 1517 1518 PPRec.SetPreallocatedEntity(Record[0], MD); 1519 MacroDefinitionsLoaded[Record[1] - 1] = MD; 1520 1521 if (DeserializationListener) 1522 DeserializationListener->MacroDefinitionRead(Record[1], MD); 1523 } 1524 1525 return MacroDefinitionsLoaded[Record[1] - 1]; 1526 } 1527 1528 case PP_INCLUSION_DIRECTIVE: { 1529 // If we already have a macro, that means that we've hit the end 1530 // of the definition of the macro we were looking for. We're 1531 // done. 1532 if (Macro) 1533 return 0; 1534 1535 if (!PP->getPreprocessingRecord()) { 1536 Error("missing preprocessing record in AST file"); 1537 return 0; 1538 } 1539 1540 PreprocessingRecord &PPRec = *PP->getPreprocessingRecord(); 1541 if (PreprocessedEntity *PE = PPRec.getPreprocessedEntity(Record[0])) 1542 return PE; 1543 1544 const char *FullFileNameStart = BlobStart + Record[3]; 1545 const FileEntry *File 1546 = PP->getFileManager().getFile(llvm::StringRef(FullFileNameStart, 1547 BlobLen - Record[3])); 1548 1549 // FIXME: Stable encoding 1550 InclusionDirective::InclusionKind Kind 1551 = static_cast<InclusionDirective::InclusionKind>(Record[5]); 1552 InclusionDirective *ID 1553 = new (PPRec) InclusionDirective(PPRec, Kind, 1554 llvm::StringRef(BlobStart, Record[3]), 1555 Record[4], 1556 File, 1557 SourceRange(ReadSourceLocation(F, Record[1]), 1558 ReadSourceLocation(F, Record[2]))); 1559 PPRec.SetPreallocatedEntity(Record[0], ID); 1560 return ID; 1561 } 1562 } 1563 } 1564 1565 return 0; 1566} 1567 1568void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, PerFileData &F, 1569 uint64_t Offset) { 1570 // Note that this identifier has a macro definition. 1571 II->setHasMacroDefinition(true); 1572 1573 // Adjust the offset based on our position in the chain. 1574 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 1575 if (Chain[I] == &F) 1576 break; 1577 1578 Offset += Chain[I]->SizeInBits; 1579 } 1580 1581 UnreadMacroRecordOffsets[II] = Offset; 1582} 1583 1584void ASTReader::ReadDefinedMacros() { 1585 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 1586 PerFileData &F = *Chain[N - I - 1]; 1587 llvm::BitstreamCursor &MacroCursor = F.MacroCursor; 1588 1589 // If there was no preprocessor block, skip this file. 1590 if (!MacroCursor.getBitStreamReader()) 1591 continue; 1592 1593 llvm::BitstreamCursor Cursor = MacroCursor; 1594 Cursor.JumpToBit(F.MacroStartOffset); 1595 1596 RecordData Record; 1597 while (true) { 1598 uint64_t Offset = Cursor.GetCurrentBitNo(); 1599 unsigned Code = Cursor.ReadCode(); 1600 if (Code == llvm::bitc::END_BLOCK) 1601 break; 1602 1603 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1604 // No known subblocks, always skip them. 1605 Cursor.ReadSubBlockID(); 1606 if (Cursor.SkipBlock()) { 1607 Error("malformed block record in AST file"); 1608 return; 1609 } 1610 continue; 1611 } 1612 1613 if (Code == llvm::bitc::DEFINE_ABBREV) { 1614 Cursor.ReadAbbrevRecord(); 1615 continue; 1616 } 1617 1618 // Read a record. 1619 const char *BlobStart; 1620 unsigned BlobLen; 1621 Record.clear(); 1622 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) { 1623 default: // Default behavior: ignore. 1624 break; 1625 1626 case PP_MACRO_OBJECT_LIKE: 1627 case PP_MACRO_FUNCTION_LIKE: 1628 DecodeIdentifierInfo(Record[0]); 1629 break; 1630 1631 case PP_TOKEN: 1632 // Ignore tokens. 1633 break; 1634 1635 case PP_MACRO_INSTANTIATION: 1636 case PP_MACRO_DEFINITION: 1637 case PP_INCLUSION_DIRECTIVE: 1638 // Read the macro record. 1639 // FIXME: That's a stupid way to do this. We should reuse this cursor. 1640 ReadMacroRecord(F, Offset); 1641 break; 1642 } 1643 } 1644 } 1645 1646 // Drain the unread macro-record offsets map. 1647 while (!UnreadMacroRecordOffsets.empty()) 1648 LoadMacroDefinition(UnreadMacroRecordOffsets.begin()); 1649} 1650 1651void ASTReader::LoadMacroDefinition( 1652 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) { 1653 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition"); 1654 PerFileData *F = 0; 1655 uint64_t Offset = Pos->second; 1656 UnreadMacroRecordOffsets.erase(Pos); 1657 1658 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 1659 if (Offset < Chain[I]->SizeInBits) { 1660 F = Chain[I]; 1661 break; 1662 } 1663 1664 Offset -= Chain[I]->SizeInBits; 1665 } 1666 if (!F) { 1667 Error("Malformed macro record offset"); 1668 return; 1669 } 1670 1671 ReadMacroRecord(*F, Offset); 1672} 1673 1674void ASTReader::LoadMacroDefinition(IdentifierInfo *II) { 1675 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos 1676 = UnreadMacroRecordOffsets.find(II); 1677 LoadMacroDefinition(Pos); 1678} 1679 1680MacroDefinition *ASTReader::getMacroDefinition(MacroID ID) { 1681 if (ID == 0 || ID > MacroDefinitionsLoaded.size()) 1682 return 0; 1683 1684 if (!MacroDefinitionsLoaded[ID - 1]) { 1685 unsigned Index = ID - 1; 1686 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 1687 PerFileData &F = *Chain[N - I - 1]; 1688 if (Index < F.LocalNumMacroDefinitions) { 1689 ReadMacroRecord(F, F.MacroDefinitionOffsets[Index]); 1690 break; 1691 } 1692 Index -= F.LocalNumMacroDefinitions; 1693 } 1694 assert(MacroDefinitionsLoaded[ID - 1] && "Broken chain"); 1695 } 1696 1697 return MacroDefinitionsLoaded[ID - 1]; 1698} 1699 1700/// \brief If we are loading a relocatable PCH file, and the filename is 1701/// not an absolute path, add the system root to the beginning of the file 1702/// name. 1703void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) { 1704 // If this is not a relocatable PCH file, there's nothing to do. 1705 if (!RelocatablePCH) 1706 return; 1707 1708 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 1709 return; 1710 1711 if (isysroot == 0) { 1712 // If no system root was given, default to '/' 1713 Filename.insert(Filename.begin(), '/'); 1714 return; 1715 } 1716 1717 unsigned Length = strlen(isysroot); 1718 if (isysroot[Length - 1] != '/') 1719 Filename.insert(Filename.begin(), '/'); 1720 1721 Filename.insert(Filename.begin(), isysroot, isysroot + Length); 1722} 1723 1724ASTReader::ASTReadResult 1725ASTReader::ReadASTBlock(PerFileData &F) { 1726 llvm::BitstreamCursor &Stream = F.Stream; 1727 1728 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 1729 Error("malformed block record in AST file"); 1730 return Failure; 1731 } 1732 1733 // Read all of the records and blocks for the ASt file. 1734 RecordData Record; 1735 bool First = true; 1736 while (!Stream.AtEndOfStream()) { 1737 unsigned Code = Stream.ReadCode(); 1738 if (Code == llvm::bitc::END_BLOCK) { 1739 if (Stream.ReadBlockEnd()) { 1740 Error("error at end of module block in AST file"); 1741 return Failure; 1742 } 1743 1744 return Success; 1745 } 1746 1747 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 1748 switch (Stream.ReadSubBlockID()) { 1749 case DECLTYPES_BLOCK_ID: 1750 // We lazily load the decls block, but we want to set up the 1751 // DeclsCursor cursor to point into it. Clone our current bitcode 1752 // cursor to it, enter the block and read the abbrevs in that block. 1753 // With the main cursor, we just skip over it. 1754 F.DeclsCursor = Stream; 1755 if (Stream.SkipBlock() || // Skip with the main cursor. 1756 // Read the abbrevs. 1757 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 1758 Error("malformed block record in AST file"); 1759 return Failure; 1760 } 1761 break; 1762 1763 case DECL_UPDATES_BLOCK_ID: 1764 if (Stream.SkipBlock()) { 1765 Error("malformed block record in AST file"); 1766 return Failure; 1767 } 1768 break; 1769 1770 case PREPROCESSOR_BLOCK_ID: 1771 F.MacroCursor = Stream; 1772 if (PP) 1773 PP->setExternalSource(this); 1774 1775 if (Stream.SkipBlock() || 1776 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 1777 Error("malformed block record in AST file"); 1778 return Failure; 1779 } 1780 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 1781 break; 1782 1783 case SOURCE_MANAGER_BLOCK_ID: 1784 switch (ReadSourceManagerBlock(F)) { 1785 case Success: 1786 break; 1787 1788 case Failure: 1789 Error("malformed source manager block in AST file"); 1790 return Failure; 1791 1792 case IgnorePCH: 1793 return IgnorePCH; 1794 } 1795 break; 1796 } 1797 First = false; 1798 continue; 1799 } 1800 1801 if (Code == llvm::bitc::DEFINE_ABBREV) { 1802 Stream.ReadAbbrevRecord(); 1803 continue; 1804 } 1805 1806 // Read and process a record. 1807 Record.clear(); 1808 const char *BlobStart = 0; 1809 unsigned BlobLen = 0; 1810 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record, 1811 &BlobStart, &BlobLen)) { 1812 default: // Default behavior: ignore. 1813 break; 1814 1815 case METADATA: { 1816 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1817 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1818 : diag::warn_pch_version_too_new); 1819 return IgnorePCH; 1820 } 1821 1822 RelocatablePCH = Record[4]; 1823 if (Listener) { 1824 std::string TargetTriple(BlobStart, BlobLen); 1825 if (Listener->ReadTargetTriple(TargetTriple)) 1826 return IgnorePCH; 1827 } 1828 break; 1829 } 1830 1831 case CHAINED_METADATA: { 1832 if (!First) { 1833 Error("CHAINED_METADATA is not first record in block"); 1834 return Failure; 1835 } 1836 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 1837 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old 1838 : diag::warn_pch_version_too_new); 1839 return IgnorePCH; 1840 } 1841 1842 // Load the chained file, which is always a PCH file. 1843 switch(ReadASTCore(llvm::StringRef(BlobStart, BlobLen), PCH)) { 1844 case Failure: return Failure; 1845 // If we have to ignore the dependency, we'll have to ignore this too. 1846 case IgnorePCH: return IgnorePCH; 1847 case Success: break; 1848 } 1849 break; 1850 } 1851 1852 case TYPE_OFFSET: 1853 if (F.LocalNumTypes != 0) { 1854 Error("duplicate TYPE_OFFSET record in AST file"); 1855 return Failure; 1856 } 1857 F.TypeOffsets = (const uint32_t *)BlobStart; 1858 F.LocalNumTypes = Record[0]; 1859 break; 1860 1861 case DECL_OFFSET: 1862 if (F.LocalNumDecls != 0) { 1863 Error("duplicate DECL_OFFSET record in AST file"); 1864 return Failure; 1865 } 1866 F.DeclOffsets = (const uint32_t *)BlobStart; 1867 F.LocalNumDecls = Record[0]; 1868 break; 1869 1870 case TU_UPDATE_LEXICAL: { 1871 DeclContextInfo Info = { 1872 /* No visible information */ 0, 1873 reinterpret_cast<const KindDeclIDPair *>(BlobStart), 1874 BlobLen / sizeof(KindDeclIDPair) 1875 }; 1876 DeclContextOffsets[Context ? Context->getTranslationUnitDecl() : 0] 1877 .push_back(Info); 1878 break; 1879 } 1880 1881 case UPDATE_VISIBLE: { 1882 serialization::DeclID ID = Record[0]; 1883 void *Table = ASTDeclContextNameLookupTable::Create( 1884 (const unsigned char *)BlobStart + Record[1], 1885 (const unsigned char *)BlobStart, 1886 ASTDeclContextNameLookupTrait(*this)); 1887 if (ID == 1 && Context) { // Is it the TU? 1888 DeclContextInfo Info = { 1889 Table, /* No lexical inforamtion */ 0, 0 1890 }; 1891 DeclContextOffsets[Context->getTranslationUnitDecl()].push_back(Info); 1892 } else 1893 PendingVisibleUpdates[ID].push_back(Table); 1894 break; 1895 } 1896 1897 case REDECLS_UPDATE_LATEST: { 1898 assert(Record.size() % 2 == 0 && "Expected pairs of DeclIDs"); 1899 for (unsigned i = 0, e = Record.size(); i < e; i += 2) { 1900 DeclID First = Record[i], Latest = Record[i+1]; 1901 assert((FirstLatestDeclIDs.find(First) == FirstLatestDeclIDs.end() || 1902 Latest > FirstLatestDeclIDs[First]) && 1903 "The new latest is supposed to come after the previous latest"); 1904 FirstLatestDeclIDs[First] = Latest; 1905 } 1906 break; 1907 } 1908 1909 case LANGUAGE_OPTIONS: 1910 if (ParseLanguageOptions(Record) && !DisableValidation) 1911 return IgnorePCH; 1912 break; 1913 1914 case IDENTIFIER_TABLE: 1915 F.IdentifierTableData = BlobStart; 1916 if (Record[0]) { 1917 F.IdentifierLookupTable 1918 = ASTIdentifierLookupTable::Create( 1919 (const unsigned char *)F.IdentifierTableData + Record[0], 1920 (const unsigned char *)F.IdentifierTableData, 1921 ASTIdentifierLookupTrait(*this, F)); 1922 if (PP) 1923 PP->getIdentifierTable().setExternalIdentifierLookup(this); 1924 } 1925 break; 1926 1927 case IDENTIFIER_OFFSET: 1928 if (F.LocalNumIdentifiers != 0) { 1929 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 1930 return Failure; 1931 } 1932 F.IdentifierOffsets = (const uint32_t *)BlobStart; 1933 F.LocalNumIdentifiers = Record[0]; 1934 break; 1935 1936 case EXTERNAL_DEFINITIONS: 1937 // Optimization for the first block. 1938 if (ExternalDefinitions.empty()) 1939 ExternalDefinitions.swap(Record); 1940 else 1941 ExternalDefinitions.insert(ExternalDefinitions.end(), 1942 Record.begin(), Record.end()); 1943 break; 1944 1945 case SPECIAL_TYPES: 1946 // Optimization for the first block 1947 if (SpecialTypes.empty()) 1948 SpecialTypes.swap(Record); 1949 else 1950 SpecialTypes.insert(SpecialTypes.end(), Record.begin(), Record.end()); 1951 break; 1952 1953 case STATISTICS: 1954 TotalNumStatements += Record[0]; 1955 TotalNumMacros += Record[1]; 1956 TotalLexicalDeclContexts += Record[2]; 1957 TotalVisibleDeclContexts += Record[3]; 1958 break; 1959 1960 case TENTATIVE_DEFINITIONS: 1961 // Optimization for the first block. 1962 if (TentativeDefinitions.empty()) 1963 TentativeDefinitions.swap(Record); 1964 else 1965 TentativeDefinitions.insert(TentativeDefinitions.end(), 1966 Record.begin(), Record.end()); 1967 break; 1968 1969 case UNUSED_FILESCOPED_DECLS: 1970 // Optimization for the first block. 1971 if (UnusedFileScopedDecls.empty()) 1972 UnusedFileScopedDecls.swap(Record); 1973 else 1974 UnusedFileScopedDecls.insert(UnusedFileScopedDecls.end(), 1975 Record.begin(), Record.end()); 1976 break; 1977 1978 case WEAK_UNDECLARED_IDENTIFIERS: 1979 // Later blocks overwrite earlier ones. 1980 WeakUndeclaredIdentifiers.swap(Record); 1981 break; 1982 1983 case LOCALLY_SCOPED_EXTERNAL_DECLS: 1984 // Optimization for the first block. 1985 if (LocallyScopedExternalDecls.empty()) 1986 LocallyScopedExternalDecls.swap(Record); 1987 else 1988 LocallyScopedExternalDecls.insert(LocallyScopedExternalDecls.end(), 1989 Record.begin(), Record.end()); 1990 break; 1991 1992 case SELECTOR_OFFSETS: 1993 F.SelectorOffsets = (const uint32_t *)BlobStart; 1994 F.LocalNumSelectors = Record[0]; 1995 break; 1996 1997 case METHOD_POOL: 1998 F.SelectorLookupTableData = (const unsigned char *)BlobStart; 1999 if (Record[0]) 2000 F.SelectorLookupTable 2001 = ASTSelectorLookupTable::Create( 2002 F.SelectorLookupTableData + Record[0], 2003 F.SelectorLookupTableData, 2004 ASTSelectorLookupTrait(*this)); 2005 TotalNumMethodPoolEntries += Record[1]; 2006 break; 2007 2008 case REFERENCED_SELECTOR_POOL: 2009 F.ReferencedSelectorsData.swap(Record); 2010 break; 2011 2012 case PP_COUNTER_VALUE: 2013 if (!Record.empty() && Listener) 2014 Listener->ReadCounter(Record[0]); 2015 break; 2016 2017 case SOURCE_LOCATION_OFFSETS: 2018 F.SLocOffsets = (const uint32_t *)BlobStart; 2019 F.LocalNumSLocEntries = Record[0]; 2020 F.LocalSLocSize = Record[1]; 2021 break; 2022 2023 case SOURCE_LOCATION_PRELOADS: 2024 if (PreloadSLocEntries.empty()) 2025 PreloadSLocEntries.swap(Record); 2026 else 2027 PreloadSLocEntries.insert(PreloadSLocEntries.end(), 2028 Record.begin(), Record.end()); 2029 break; 2030 2031 case STAT_CACHE: { 2032 ASTStatCache *MyStatCache = 2033 new ASTStatCache((const unsigned char *)BlobStart + Record[0], 2034 (const unsigned char *)BlobStart, 2035 NumStatHits, NumStatMisses); 2036 FileMgr.addStatCache(MyStatCache); 2037 F.StatCache = MyStatCache; 2038 break; 2039 } 2040 2041 case EXT_VECTOR_DECLS: 2042 // Optimization for the first block. 2043 if (ExtVectorDecls.empty()) 2044 ExtVectorDecls.swap(Record); 2045 else 2046 ExtVectorDecls.insert(ExtVectorDecls.end(), 2047 Record.begin(), Record.end()); 2048 break; 2049 2050 case VTABLE_USES: 2051 // Later tables overwrite earlier ones. 2052 VTableUses.swap(Record); 2053 break; 2054 2055 case DYNAMIC_CLASSES: 2056 // Optimization for the first block. 2057 if (DynamicClasses.empty()) 2058 DynamicClasses.swap(Record); 2059 else 2060 DynamicClasses.insert(DynamicClasses.end(), 2061 Record.begin(), Record.end()); 2062 break; 2063 2064 case PENDING_IMPLICIT_INSTANTIATIONS: 2065 F.PendingInstantiations.swap(Record); 2066 break; 2067 2068 case SEMA_DECL_REFS: 2069 // Later tables overwrite earlier ones. 2070 SemaDeclRefs.swap(Record); 2071 break; 2072 2073 case ORIGINAL_FILE_NAME: 2074 // The primary AST will be the last to get here, so it will be the one 2075 // that's used. 2076 ActualOriginalFileName.assign(BlobStart, BlobLen); 2077 OriginalFileName = ActualOriginalFileName; 2078 MaybeAddSystemRootToFilename(OriginalFileName); 2079 break; 2080 2081 case VERSION_CONTROL_BRANCH_REVISION: { 2082 const std::string &CurBranch = getClangFullRepositoryVersion(); 2083 llvm::StringRef ASTBranch(BlobStart, BlobLen); 2084 if (llvm::StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2085 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch; 2086 return IgnorePCH; 2087 } 2088 break; 2089 } 2090 2091 case MACRO_DEFINITION_OFFSETS: 2092 F.MacroDefinitionOffsets = (const uint32_t *)BlobStart; 2093 F.NumPreallocatedPreprocessingEntities = Record[0]; 2094 F.LocalNumMacroDefinitions = Record[1]; 2095 break; 2096 2097 case DECL_UPDATE_OFFSETS: { 2098 if (Record.size() % 2 != 0) { 2099 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 2100 return Failure; 2101 } 2102 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2103 DeclUpdateOffsets[static_cast<DeclID>(Record[I])] 2104 .push_back(std::make_pair(&F, Record[I+1])); 2105 break; 2106 } 2107 2108 case DECL_REPLACEMENTS: { 2109 if (Record.size() % 2 != 0) { 2110 Error("invalid DECL_REPLACEMENTS block in AST file"); 2111 return Failure; 2112 } 2113 for (unsigned I = 0, N = Record.size(); I != N; I += 2) 2114 ReplacedDecls[static_cast<DeclID>(Record[I])] = 2115 std::make_pair(&F, Record[I+1]); 2116 break; 2117 } 2118 2119 case CXX_BASE_SPECIFIER_OFFSETS: { 2120 if (F.LocalNumCXXBaseSpecifiers != 0) { 2121 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file"); 2122 return Failure; 2123 } 2124 2125 F.LocalNumCXXBaseSpecifiers = Record[0]; 2126 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart; 2127 break; 2128 } 2129 2130 case DIAG_USER_MAPPINGS: 2131 if (Record.size() % 2 != 0) { 2132 Error("invalid DIAG_USER_MAPPINGS block in AST file"); 2133 return Failure; 2134 } 2135 if (UserDiagMappings.empty()) 2136 UserDiagMappings.swap(Record); 2137 else 2138 UserDiagMappings.insert(UserDiagMappings.end(), 2139 Record.begin(), Record.end()); 2140 break; 2141 } 2142 First = false; 2143 } 2144 Error("premature end of bitstream in AST file"); 2145 return Failure; 2146} 2147 2148ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName, 2149 ASTFileType Type) { 2150 switch(ReadASTCore(FileName, Type)) { 2151 case Failure: return Failure; 2152 case IgnorePCH: return IgnorePCH; 2153 case Success: break; 2154 } 2155 2156 // Here comes stuff that we only do once the entire chain is loaded. 2157 2158 // Allocate space for loaded slocentries, identifiers, decls and types. 2159 unsigned TotalNumIdentifiers = 0, TotalNumTypes = 0, TotalNumDecls = 0, 2160 TotalNumPreallocatedPreprocessingEntities = 0, TotalNumMacroDefs = 0, 2161 TotalNumSelectors = 0; 2162 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2163 TotalNumSLocEntries += Chain[I]->LocalNumSLocEntries; 2164 NextSLocOffset += Chain[I]->LocalSLocSize; 2165 TotalNumIdentifiers += Chain[I]->LocalNumIdentifiers; 2166 TotalNumTypes += Chain[I]->LocalNumTypes; 2167 TotalNumDecls += Chain[I]->LocalNumDecls; 2168 TotalNumPreallocatedPreprocessingEntities += 2169 Chain[I]->NumPreallocatedPreprocessingEntities; 2170 TotalNumMacroDefs += Chain[I]->LocalNumMacroDefinitions; 2171 TotalNumSelectors += Chain[I]->LocalNumSelectors; 2172 } 2173 SourceMgr.PreallocateSLocEntries(this, TotalNumSLocEntries, NextSLocOffset); 2174 IdentifiersLoaded.resize(TotalNumIdentifiers); 2175 TypesLoaded.resize(TotalNumTypes); 2176 DeclsLoaded.resize(TotalNumDecls); 2177 MacroDefinitionsLoaded.resize(TotalNumMacroDefs); 2178 if (PP) { 2179 if (TotalNumIdentifiers > 0) 2180 PP->getHeaderSearchInfo().SetExternalLookup(this); 2181 if (TotalNumPreallocatedPreprocessingEntities > 0) { 2182 if (!PP->getPreprocessingRecord()) 2183 PP->createPreprocessingRecord(); 2184 PP->getPreprocessingRecord()->SetExternalSource(*this, 2185 TotalNumPreallocatedPreprocessingEntities); 2186 } 2187 } 2188 SelectorsLoaded.resize(TotalNumSelectors); 2189 // Preload SLocEntries. 2190 for (unsigned I = 0, N = PreloadSLocEntries.size(); I != N; ++I) { 2191 ASTReadResult Result = ReadSLocEntryRecord(PreloadSLocEntries[I]); 2192 if (Result != Success) 2193 return Result; 2194 } 2195 2196 // Check the predefines buffers. 2197 if (!DisableValidation && CheckPredefinesBuffers()) 2198 return IgnorePCH; 2199 2200 if (PP) { 2201 // Initialization of keywords and pragmas occurs before the 2202 // AST file is read, so there may be some identifiers that were 2203 // loaded into the IdentifierTable before we intercepted the 2204 // creation of identifiers. Iterate through the list of known 2205 // identifiers and determine whether we have to establish 2206 // preprocessor definitions or top-level identifier declaration 2207 // chains for those identifiers. 2208 // 2209 // We copy the IdentifierInfo pointers to a small vector first, 2210 // since de-serializing declarations or macro definitions can add 2211 // new entries into the identifier table, invalidating the 2212 // iterators. 2213 llvm::SmallVector<IdentifierInfo *, 128> Identifiers; 2214 for (IdentifierTable::iterator Id = PP->getIdentifierTable().begin(), 2215 IdEnd = PP->getIdentifierTable().end(); 2216 Id != IdEnd; ++Id) 2217 Identifiers.push_back(Id->second); 2218 // We need to search the tables in all files. 2219 for (unsigned J = 0, M = Chain.size(); J != M; ++J) { 2220 ASTIdentifierLookupTable *IdTable 2221 = (ASTIdentifierLookupTable *)Chain[J]->IdentifierLookupTable; 2222 // Not all AST files necessarily have identifier tables, only the useful 2223 // ones. 2224 if (!IdTable) 2225 continue; 2226 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) { 2227 IdentifierInfo *II = Identifiers[I]; 2228 // Look in the on-disk hash tables for an entry for this identifier 2229 ASTIdentifierLookupTrait Info(*this, *Chain[J], II); 2230 std::pair<const char*,unsigned> Key(II->getNameStart(),II->getLength()); 2231 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info); 2232 if (Pos == IdTable->end()) 2233 continue; 2234 2235 // Dereferencing the iterator has the effect of populating the 2236 // IdentifierInfo node with the various declarations it needs. 2237 (void)*Pos; 2238 } 2239 } 2240 } 2241 2242 if (Context) 2243 InitializeContext(*Context); 2244 2245 if (DeserializationListener) 2246 DeserializationListener->ReaderInitialized(this); 2247 2248 // If this AST file is a precompiled preamble, then set the main file ID of 2249 // the source manager to the file source file from which the preamble was 2250 // built. This is the only valid way to use a precompiled preamble. 2251 if (Type == Preamble) { 2252 SourceLocation Loc 2253 = SourceMgr.getLocation(FileMgr.getFile(getOriginalSourceFile()), 1, 1); 2254 if (Loc.isValid()) { 2255 std::pair<FileID, unsigned> Decomposed = SourceMgr.getDecomposedLoc(Loc); 2256 SourceMgr.SetPreambleFileID(Decomposed.first); 2257 } 2258 } 2259 2260 return Success; 2261} 2262 2263ASTReader::ASTReadResult ASTReader::ReadASTCore(llvm::StringRef FileName, 2264 ASTFileType Type) { 2265 PerFileData *Prev = Chain.empty() ? 0 : Chain.back(); 2266 Chain.push_back(new PerFileData(Type)); 2267 PerFileData &F = *Chain.back(); 2268 if (Prev) 2269 Prev->NextInSource = &F; 2270 else 2271 FirstInSource = &F; 2272 F.Loaders.push_back(Prev); 2273 2274 // Set the AST file name. 2275 F.FileName = FileName; 2276 2277 // Open the AST file. 2278 // 2279 // FIXME: This shouldn't be here, we should just take a raw_ostream. 2280 std::string ErrStr; 2281 llvm::error_code ec; 2282 if (FileName == "-") { 2283 ec = llvm::MemoryBuffer::getSTDIN(F.Buffer); 2284 if (ec) 2285 ErrStr = ec.message(); 2286 } else 2287 F.Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrStr)); 2288 if (!F.Buffer) { 2289 Error(ErrStr.c_str()); 2290 return IgnorePCH; 2291 } 2292 2293 // Initialize the stream 2294 F.StreamFile.init((const unsigned char *)F.Buffer->getBufferStart(), 2295 (const unsigned char *)F.Buffer->getBufferEnd()); 2296 llvm::BitstreamCursor &Stream = F.Stream; 2297 Stream.init(F.StreamFile); 2298 F.SizeInBits = F.Buffer->getBufferSize() * 8; 2299 2300 // Sniff for the signature. 2301 if (Stream.Read(8) != 'C' || 2302 Stream.Read(8) != 'P' || 2303 Stream.Read(8) != 'C' || 2304 Stream.Read(8) != 'H') { 2305 Diag(diag::err_not_a_pch_file) << FileName; 2306 return Failure; 2307 } 2308 2309 while (!Stream.AtEndOfStream()) { 2310 unsigned Code = Stream.ReadCode(); 2311 2312 if (Code != llvm::bitc::ENTER_SUBBLOCK) { 2313 Error("invalid record at top-level of AST file"); 2314 return Failure; 2315 } 2316 2317 unsigned BlockID = Stream.ReadSubBlockID(); 2318 2319 // We only know the AST subblock ID. 2320 switch (BlockID) { 2321 case llvm::bitc::BLOCKINFO_BLOCK_ID: 2322 if (Stream.ReadBlockInfoBlock()) { 2323 Error("malformed BlockInfoBlock in AST file"); 2324 return Failure; 2325 } 2326 break; 2327 case AST_BLOCK_ID: 2328 switch (ReadASTBlock(F)) { 2329 case Success: 2330 break; 2331 2332 case Failure: 2333 return Failure; 2334 2335 case IgnorePCH: 2336 // FIXME: We could consider reading through to the end of this 2337 // AST block, skipping subblocks, to see if there are other 2338 // AST blocks elsewhere. 2339 2340 // Clear out any preallocated source location entries, so that 2341 // the source manager does not try to resolve them later. 2342 SourceMgr.ClearPreallocatedSLocEntries(); 2343 2344 // Remove the stat cache. 2345 if (F.StatCache) 2346 FileMgr.removeStatCache((ASTStatCache*)F.StatCache); 2347 2348 return IgnorePCH; 2349 } 2350 break; 2351 default: 2352 if (Stream.SkipBlock()) { 2353 Error("malformed block record in AST file"); 2354 return Failure; 2355 } 2356 break; 2357 } 2358 } 2359 2360 return Success; 2361} 2362 2363void ASTReader::setPreprocessor(Preprocessor &pp) { 2364 PP = &pp; 2365 2366 unsigned TotalNum = 0; 2367 for (unsigned I = 0, N = Chain.size(); I != N; ++I) 2368 TotalNum += Chain[I]->NumPreallocatedPreprocessingEntities; 2369 if (TotalNum) { 2370 if (!PP->getPreprocessingRecord()) 2371 PP->createPreprocessingRecord(); 2372 PP->getPreprocessingRecord()->SetExternalSource(*this, TotalNum); 2373 } 2374} 2375 2376void ASTReader::InitializeContext(ASTContext &Ctx) { 2377 Context = &Ctx; 2378 assert(Context && "Passed null context!"); 2379 2380 assert(PP && "Forgot to set Preprocessor ?"); 2381 PP->getIdentifierTable().setExternalIdentifierLookup(this); 2382 PP->getHeaderSearchInfo().SetExternalLookup(this); 2383 PP->setExternalSource(this); 2384 2385 // If we have an update block for the TU waiting, we have to add it before 2386 // deserializing the decl. 2387 DeclContextOffsetsMap::iterator DCU = DeclContextOffsets.find(0); 2388 if (DCU != DeclContextOffsets.end()) { 2389 // Insertion could invalidate map, so grab vector. 2390 DeclContextInfos T; 2391 T.swap(DCU->second); 2392 DeclContextOffsets.erase(DCU); 2393 DeclContextOffsets[Ctx.getTranslationUnitDecl()].swap(T); 2394 } 2395 2396 // Load the translation unit declaration 2397 GetTranslationUnitDecl(); 2398 2399 // Load the special types. 2400 Context->setBuiltinVaListType( 2401 GetType(SpecialTypes[SPECIAL_TYPE_BUILTIN_VA_LIST])); 2402 if (unsigned Id = SpecialTypes[SPECIAL_TYPE_OBJC_ID]) 2403 Context->setObjCIdType(GetType(Id)); 2404 if (unsigned Sel = SpecialTypes[SPECIAL_TYPE_OBJC_SELECTOR]) 2405 Context->setObjCSelType(GetType(Sel)); 2406 if (unsigned Proto = SpecialTypes[SPECIAL_TYPE_OBJC_PROTOCOL]) 2407 Context->setObjCProtoType(GetType(Proto)); 2408 if (unsigned Class = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS]) 2409 Context->setObjCClassType(GetType(Class)); 2410 2411 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) 2412 Context->setCFConstantStringType(GetType(String)); 2413 if (unsigned FastEnum 2414 = SpecialTypes[SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE]) 2415 Context->setObjCFastEnumerationStateType(GetType(FastEnum)); 2416 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 2417 QualType FileType = GetType(File); 2418 if (FileType.isNull()) { 2419 Error("FILE type is NULL"); 2420 return; 2421 } 2422 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 2423 Context->setFILEDecl(Typedef->getDecl()); 2424 else { 2425 const TagType *Tag = FileType->getAs<TagType>(); 2426 if (!Tag) { 2427 Error("Invalid FILE type in AST file"); 2428 return; 2429 } 2430 Context->setFILEDecl(Tag->getDecl()); 2431 } 2432 } 2433 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_jmp_buf]) { 2434 QualType Jmp_bufType = GetType(Jmp_buf); 2435 if (Jmp_bufType.isNull()) { 2436 Error("jmp_bug type is NULL"); 2437 return; 2438 } 2439 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 2440 Context->setjmp_bufDecl(Typedef->getDecl()); 2441 else { 2442 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 2443 if (!Tag) { 2444 Error("Invalid jmp_buf type in AST file"); 2445 return; 2446 } 2447 Context->setjmp_bufDecl(Tag->getDecl()); 2448 } 2449 } 2450 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_sigjmp_buf]) { 2451 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 2452 if (Sigjmp_bufType.isNull()) { 2453 Error("sigjmp_buf type is NULL"); 2454 return; 2455 } 2456 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 2457 Context->setsigjmp_bufDecl(Typedef->getDecl()); 2458 else { 2459 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 2460 assert(Tag && "Invalid sigjmp_buf type in AST file"); 2461 Context->setsigjmp_bufDecl(Tag->getDecl()); 2462 } 2463 } 2464 if (unsigned ObjCIdRedef 2465 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) 2466 Context->ObjCIdRedefinitionType = GetType(ObjCIdRedef); 2467 if (unsigned ObjCClassRedef 2468 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) 2469 Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef); 2470 if (unsigned String = SpecialTypes[SPECIAL_TYPE_BLOCK_DESCRIPTOR]) 2471 Context->setBlockDescriptorType(GetType(String)); 2472 if (unsigned String 2473 = SpecialTypes[SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR]) 2474 Context->setBlockDescriptorExtendedType(GetType(String)); 2475 if (unsigned ObjCSelRedef 2476 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) 2477 Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); 2478 if (unsigned String = SpecialTypes[SPECIAL_TYPE_NS_CONSTANT_STRING]) 2479 Context->setNSConstantStringType(GetType(String)); 2480 2481 if (SpecialTypes[SPECIAL_TYPE_INT128_INSTALLED]) 2482 Context->setInt128Installed(); 2483 2484 ReadUserDiagnosticMappings(Context->getDiagnostics()); 2485} 2486 2487/// \brief Retrieve the name of the original source file name 2488/// directly from the AST file, without actually loading the AST 2489/// file. 2490std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName, 2491 FileManager &FileMgr, 2492 Diagnostic &Diags) { 2493 // Open the AST file. 2494 std::string ErrStr; 2495 llvm::OwningPtr<llvm::MemoryBuffer> Buffer; 2496 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr)); 2497 if (!Buffer) { 2498 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ErrStr; 2499 return std::string(); 2500 } 2501 2502 // Initialize the stream 2503 llvm::BitstreamReader StreamFile; 2504 llvm::BitstreamCursor Stream; 2505 StreamFile.init((const unsigned char *)Buffer->getBufferStart(), 2506 (const unsigned char *)Buffer->getBufferEnd()); 2507 Stream.init(StreamFile); 2508 2509 // Sniff for the signature. 2510 if (Stream.Read(8) != 'C' || 2511 Stream.Read(8) != 'P' || 2512 Stream.Read(8) != 'C' || 2513 Stream.Read(8) != 'H') { 2514 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 2515 return std::string(); 2516 } 2517 2518 RecordData Record; 2519 while (!Stream.AtEndOfStream()) { 2520 unsigned Code = Stream.ReadCode(); 2521 2522 if (Code == llvm::bitc::ENTER_SUBBLOCK) { 2523 unsigned BlockID = Stream.ReadSubBlockID(); 2524 2525 // We only know the AST subblock ID. 2526 switch (BlockID) { 2527 case AST_BLOCK_ID: 2528 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2529 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2530 return std::string(); 2531 } 2532 break; 2533 2534 default: 2535 if (Stream.SkipBlock()) { 2536 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 2537 return std::string(); 2538 } 2539 break; 2540 } 2541 continue; 2542 } 2543 2544 if (Code == llvm::bitc::END_BLOCK) { 2545 if (Stream.ReadBlockEnd()) { 2546 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName; 2547 return std::string(); 2548 } 2549 continue; 2550 } 2551 2552 if (Code == llvm::bitc::DEFINE_ABBREV) { 2553 Stream.ReadAbbrevRecord(); 2554 continue; 2555 } 2556 2557 Record.clear(); 2558 const char *BlobStart = 0; 2559 unsigned BlobLen = 0; 2560 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) 2561 == ORIGINAL_FILE_NAME) 2562 return std::string(BlobStart, BlobLen); 2563 } 2564 2565 return std::string(); 2566} 2567 2568/// \brief Parse the record that corresponds to a LangOptions data 2569/// structure. 2570/// 2571/// This routine parses the language options from the AST file and then gives 2572/// them to the AST listener if one is set. 2573/// 2574/// \returns true if the listener deems the file unacceptable, false otherwise. 2575bool ASTReader::ParseLanguageOptions( 2576 const llvm::SmallVectorImpl<uint64_t> &Record) { 2577 if (Listener) { 2578 LangOptions LangOpts; 2579 2580 #define PARSE_LANGOPT(Option) \ 2581 LangOpts.Option = Record[Idx]; \ 2582 ++Idx 2583 2584 unsigned Idx = 0; 2585 PARSE_LANGOPT(Trigraphs); 2586 PARSE_LANGOPT(BCPLComment); 2587 PARSE_LANGOPT(DollarIdents); 2588 PARSE_LANGOPT(AsmPreprocessor); 2589 PARSE_LANGOPT(GNUMode); 2590 PARSE_LANGOPT(GNUKeywords); 2591 PARSE_LANGOPT(ImplicitInt); 2592 PARSE_LANGOPT(Digraphs); 2593 PARSE_LANGOPT(HexFloats); 2594 PARSE_LANGOPT(C99); 2595 PARSE_LANGOPT(Microsoft); 2596 PARSE_LANGOPT(CPlusPlus); 2597 PARSE_LANGOPT(CPlusPlus0x); 2598 PARSE_LANGOPT(CXXOperatorNames); 2599 PARSE_LANGOPT(ObjC1); 2600 PARSE_LANGOPT(ObjC2); 2601 PARSE_LANGOPT(ObjCNonFragileABI); 2602 PARSE_LANGOPT(ObjCNonFragileABI2); 2603 PARSE_LANGOPT(ObjCDefaultSynthProperties); 2604 PARSE_LANGOPT(NoConstantCFStrings); 2605 PARSE_LANGOPT(PascalStrings); 2606 PARSE_LANGOPT(WritableStrings); 2607 PARSE_LANGOPT(LaxVectorConversions); 2608 PARSE_LANGOPT(AltiVec); 2609 PARSE_LANGOPT(Exceptions); 2610 PARSE_LANGOPT(SjLjExceptions); 2611 PARSE_LANGOPT(NeXTRuntime); 2612 PARSE_LANGOPT(Freestanding); 2613 PARSE_LANGOPT(NoBuiltin); 2614 PARSE_LANGOPT(ThreadsafeStatics); 2615 PARSE_LANGOPT(POSIXThreads); 2616 PARSE_LANGOPT(Blocks); 2617 PARSE_LANGOPT(EmitAllDecls); 2618 PARSE_LANGOPT(MathErrno); 2619 LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) 2620 Record[Idx++]); 2621 PARSE_LANGOPT(HeinousExtensions); 2622 PARSE_LANGOPT(Optimize); 2623 PARSE_LANGOPT(OptimizeSize); 2624 PARSE_LANGOPT(Static); 2625 PARSE_LANGOPT(PICLevel); 2626 PARSE_LANGOPT(GNUInline); 2627 PARSE_LANGOPT(NoInline); 2628 PARSE_LANGOPT(AccessControl); 2629 PARSE_LANGOPT(CharIsSigned); 2630 PARSE_LANGOPT(ShortWChar); 2631 LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); 2632 LangOpts.setVisibilityMode((Visibility)Record[Idx++]); 2633 LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) 2634 Record[Idx++]); 2635 PARSE_LANGOPT(InstantiationDepth); 2636 PARSE_LANGOPT(OpenCL); 2637 PARSE_LANGOPT(CUDA); 2638 PARSE_LANGOPT(CatchUndefined); 2639 // FIXME: Missing ElideConstructors?! 2640 #undef PARSE_LANGOPT 2641 2642 return Listener->ReadLanguageOptions(LangOpts); 2643 } 2644 2645 return false; 2646} 2647 2648void ASTReader::ReadPreprocessedEntities() { 2649 ReadDefinedMacros(); 2650} 2651 2652PreprocessedEntity *ASTReader::ReadPreprocessedEntity(uint64_t Offset) { 2653 PerFileData *F = 0; 2654 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2655 if (Offset < Chain[I]->SizeInBits) { 2656 F = Chain[I]; 2657 break; 2658 } 2659 2660 Offset -= Chain[I]->SizeInBits; 2661 } 2662 2663 if (!F) { 2664 Error("Malformed preprocessed entity offset"); 2665 return 0; 2666 } 2667 2668 return ReadMacroRecord(*F, Offset); 2669} 2670 2671void ASTReader::ReadUserDiagnosticMappings(Diagnostic &Diag) { 2672 unsigned Idx = 0; 2673 while (Idx < UserDiagMappings.size()) { 2674 unsigned DiagID = UserDiagMappings[Idx++]; 2675 unsigned Map = UserDiagMappings[Idx++]; 2676 Diag.setDiagnosticMappingInternal(DiagID, Map, Diag.GetCurDiagState(), 2677 /*isUser=*/true); 2678 } 2679} 2680 2681/// \brief Get the correct cursor and offset for loading a type. 2682ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 2683 PerFileData *F = 0; 2684 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 2685 F = Chain[N - I - 1]; 2686 if (Index < F->LocalNumTypes) 2687 break; 2688 Index -= F->LocalNumTypes; 2689 } 2690 assert(F && F->LocalNumTypes > Index && "Broken chain"); 2691 return RecordLocation(F, F->TypeOffsets[Index]); 2692} 2693 2694/// \brief Read and return the type with the given index.. 2695/// 2696/// The index is the type ID, shifted and minus the number of predefs. This 2697/// routine actually reads the record corresponding to the type at the given 2698/// location. It is a helper routine for GetType, which deals with reading type 2699/// IDs. 2700QualType ASTReader::ReadTypeRecord(unsigned Index) { 2701 RecordLocation Loc = TypeCursorForIndex(Index); 2702 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 2703 2704 // Keep track of where we are in the stream, then jump back there 2705 // after reading this type. 2706 SavedStreamPosition SavedPosition(DeclsCursor); 2707 2708 ReadingKindTracker ReadingKind(Read_Type, *this); 2709 2710 // Note that we are loading a type record. 2711 Deserializing AType(this); 2712 2713 DeclsCursor.JumpToBit(Loc.Offset); 2714 RecordData Record; 2715 unsigned Code = DeclsCursor.ReadCode(); 2716 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) { 2717 case TYPE_EXT_QUAL: { 2718 if (Record.size() != 2) { 2719 Error("Incorrect encoding of extended qualifier type"); 2720 return QualType(); 2721 } 2722 QualType Base = GetType(Record[0]); 2723 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[1]); 2724 return Context->getQualifiedType(Base, Quals); 2725 } 2726 2727 case TYPE_COMPLEX: { 2728 if (Record.size() != 1) { 2729 Error("Incorrect encoding of complex type"); 2730 return QualType(); 2731 } 2732 QualType ElemType = GetType(Record[0]); 2733 return Context->getComplexType(ElemType); 2734 } 2735 2736 case TYPE_POINTER: { 2737 if (Record.size() != 1) { 2738 Error("Incorrect encoding of pointer type"); 2739 return QualType(); 2740 } 2741 QualType PointeeType = GetType(Record[0]); 2742 return Context->getPointerType(PointeeType); 2743 } 2744 2745 case TYPE_BLOCK_POINTER: { 2746 if (Record.size() != 1) { 2747 Error("Incorrect encoding of block pointer type"); 2748 return QualType(); 2749 } 2750 QualType PointeeType = GetType(Record[0]); 2751 return Context->getBlockPointerType(PointeeType); 2752 } 2753 2754 case TYPE_LVALUE_REFERENCE: { 2755 if (Record.size() != 1) { 2756 Error("Incorrect encoding of lvalue reference type"); 2757 return QualType(); 2758 } 2759 QualType PointeeType = GetType(Record[0]); 2760 return Context->getLValueReferenceType(PointeeType); 2761 } 2762 2763 case TYPE_RVALUE_REFERENCE: { 2764 if (Record.size() != 1) { 2765 Error("Incorrect encoding of rvalue reference type"); 2766 return QualType(); 2767 } 2768 QualType PointeeType = GetType(Record[0]); 2769 return Context->getRValueReferenceType(PointeeType); 2770 } 2771 2772 case TYPE_MEMBER_POINTER: { 2773 if (Record.size() != 2) { 2774 Error("Incorrect encoding of member pointer type"); 2775 return QualType(); 2776 } 2777 QualType PointeeType = GetType(Record[0]); 2778 QualType ClassType = GetType(Record[1]); 2779 if (PointeeType.isNull() || ClassType.isNull()) 2780 return QualType(); 2781 2782 return Context->getMemberPointerType(PointeeType, ClassType.getTypePtr()); 2783 } 2784 2785 case TYPE_CONSTANT_ARRAY: { 2786 QualType ElementType = GetType(Record[0]); 2787 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2788 unsigned IndexTypeQuals = Record[2]; 2789 unsigned Idx = 3; 2790 llvm::APInt Size = ReadAPInt(Record, Idx); 2791 return Context->getConstantArrayType(ElementType, Size, 2792 ASM, IndexTypeQuals); 2793 } 2794 2795 case TYPE_INCOMPLETE_ARRAY: { 2796 QualType ElementType = GetType(Record[0]); 2797 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2798 unsigned IndexTypeQuals = Record[2]; 2799 return Context->getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 2800 } 2801 2802 case TYPE_VARIABLE_ARRAY: { 2803 QualType ElementType = GetType(Record[0]); 2804 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 2805 unsigned IndexTypeQuals = Record[2]; 2806 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 2807 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 2808 return Context->getVariableArrayType(ElementType, ReadExpr(*Loc.F), 2809 ASM, IndexTypeQuals, 2810 SourceRange(LBLoc, RBLoc)); 2811 } 2812 2813 case TYPE_VECTOR: { 2814 if (Record.size() != 3) { 2815 Error("incorrect encoding of vector type in AST file"); 2816 return QualType(); 2817 } 2818 2819 QualType ElementType = GetType(Record[0]); 2820 unsigned NumElements = Record[1]; 2821 unsigned VecKind = Record[2]; 2822 return Context->getVectorType(ElementType, NumElements, 2823 (VectorType::VectorKind)VecKind); 2824 } 2825 2826 case TYPE_EXT_VECTOR: { 2827 if (Record.size() != 3) { 2828 Error("incorrect encoding of extended vector type in AST file"); 2829 return QualType(); 2830 } 2831 2832 QualType ElementType = GetType(Record[0]); 2833 unsigned NumElements = Record[1]; 2834 return Context->getExtVectorType(ElementType, NumElements); 2835 } 2836 2837 case TYPE_FUNCTION_NO_PROTO: { 2838 if (Record.size() != 4) { 2839 Error("incorrect encoding of no-proto function type"); 2840 return QualType(); 2841 } 2842 QualType ResultType = GetType(Record[0]); 2843 FunctionType::ExtInfo Info(Record[1], Record[2], (CallingConv)Record[3]); 2844 return Context->getFunctionNoProtoType(ResultType, Info); 2845 } 2846 2847 case TYPE_FUNCTION_PROTO: { 2848 QualType ResultType = GetType(Record[0]); 2849 2850 FunctionProtoType::ExtProtoInfo EPI; 2851 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 2852 /*regparm*/ Record[2], 2853 static_cast<CallingConv>(Record[3])); 2854 2855 unsigned Idx = 4; 2856 unsigned NumParams = Record[Idx++]; 2857 llvm::SmallVector<QualType, 16> ParamTypes; 2858 for (unsigned I = 0; I != NumParams; ++I) 2859 ParamTypes.push_back(GetType(Record[Idx++])); 2860 2861 EPI.Variadic = Record[Idx++]; 2862 EPI.TypeQuals = Record[Idx++]; 2863 EPI.HasExceptionSpec = Record[Idx++]; 2864 EPI.HasAnyExceptionSpec = Record[Idx++]; 2865 EPI.NumExceptions = Record[Idx++]; 2866 llvm::SmallVector<QualType, 2> Exceptions; 2867 for (unsigned I = 0; I != EPI.NumExceptions; ++I) 2868 Exceptions.push_back(GetType(Record[Idx++])); 2869 EPI.Exceptions = Exceptions.data(); 2870 return Context->getFunctionType(ResultType, ParamTypes.data(), NumParams, 2871 EPI); 2872 } 2873 2874 case TYPE_UNRESOLVED_USING: 2875 return Context->getTypeDeclType( 2876 cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); 2877 2878 case TYPE_TYPEDEF: { 2879 if (Record.size() != 2) { 2880 Error("incorrect encoding of typedef type"); 2881 return QualType(); 2882 } 2883 TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0])); 2884 QualType Canonical = GetType(Record[1]); 2885 if (!Canonical.isNull()) 2886 Canonical = Context->getCanonicalType(Canonical); 2887 return Context->getTypedefType(Decl, Canonical); 2888 } 2889 2890 case TYPE_TYPEOF_EXPR: 2891 return Context->getTypeOfExprType(ReadExpr(*Loc.F)); 2892 2893 case TYPE_TYPEOF: { 2894 if (Record.size() != 1) { 2895 Error("incorrect encoding of typeof(type) in AST file"); 2896 return QualType(); 2897 } 2898 QualType UnderlyingType = GetType(Record[0]); 2899 return Context->getTypeOfType(UnderlyingType); 2900 } 2901 2902 case TYPE_DECLTYPE: 2903 return Context->getDecltypeType(ReadExpr(*Loc.F)); 2904 2905 case TYPE_RECORD: { 2906 if (Record.size() != 2) { 2907 Error("incorrect encoding of record type"); 2908 return QualType(); 2909 } 2910 bool IsDependent = Record[0]; 2911 QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); 2912 T->setDependent(IsDependent); 2913 return T; 2914 } 2915 2916 case TYPE_ENUM: { 2917 if (Record.size() != 2) { 2918 Error("incorrect encoding of enum type"); 2919 return QualType(); 2920 } 2921 bool IsDependent = Record[0]; 2922 QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); 2923 T->setDependent(IsDependent); 2924 return T; 2925 } 2926 2927 case TYPE_PAREN: { 2928 if (Record.size() != 1) { 2929 Error("incorrect encoding of paren type"); 2930 return QualType(); 2931 } 2932 QualType InnerType = GetType(Record[0]); 2933 return Context->getParenType(InnerType); 2934 } 2935 2936 case TYPE_PACK_EXPANSION: { 2937 if (Record.size() != 1) { 2938 Error("incorrect encoding of pack expansion type"); 2939 return QualType(); 2940 } 2941 QualType Pattern = GetType(Record[0]); 2942 if (Pattern.isNull()) 2943 return QualType(); 2944 2945 return Context->getPackExpansionType(Pattern); 2946 } 2947 2948 case TYPE_ELABORATED: { 2949 unsigned Idx = 0; 2950 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 2951 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 2952 QualType NamedType = GetType(Record[Idx++]); 2953 return Context->getElaboratedType(Keyword, NNS, NamedType); 2954 } 2955 2956 case TYPE_OBJC_INTERFACE: { 2957 unsigned Idx = 0; 2958 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); 2959 return Context->getObjCInterfaceType(ItfD); 2960 } 2961 2962 case TYPE_OBJC_OBJECT: { 2963 unsigned Idx = 0; 2964 QualType Base = GetType(Record[Idx++]); 2965 unsigned NumProtos = Record[Idx++]; 2966 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos; 2967 for (unsigned I = 0; I != NumProtos; ++I) 2968 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++]))); 2969 return Context->getObjCObjectType(Base, Protos.data(), NumProtos); 2970 } 2971 2972 case TYPE_OBJC_OBJECT_POINTER: { 2973 unsigned Idx = 0; 2974 QualType Pointee = GetType(Record[Idx++]); 2975 return Context->getObjCObjectPointerType(Pointee); 2976 } 2977 2978 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 2979 unsigned Idx = 0; 2980 QualType Parm = GetType(Record[Idx++]); 2981 QualType Replacement = GetType(Record[Idx++]); 2982 return 2983 Context->getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm), 2984 Replacement); 2985 } 2986 2987 case TYPE_INJECTED_CLASS_NAME: { 2988 CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); 2989 QualType TST = GetType(Record[1]); // probably derivable 2990 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 2991 // for AST reading, too much interdependencies. 2992 return 2993 QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); 2994 } 2995 2996 case TYPE_TEMPLATE_TYPE_PARM: { 2997 unsigned Idx = 0; 2998 unsigned Depth = Record[Idx++]; 2999 unsigned Index = Record[Idx++]; 3000 bool Pack = Record[Idx++]; 3001 IdentifierInfo *Name = GetIdentifierInfo(Record, Idx); 3002 return Context->getTemplateTypeParmType(Depth, Index, Pack, Name); 3003 } 3004 3005 case TYPE_DEPENDENT_NAME: { 3006 unsigned Idx = 0; 3007 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3008 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3009 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 3010 QualType Canon = GetType(Record[Idx++]); 3011 if (!Canon.isNull()) 3012 Canon = Context->getCanonicalType(Canon); 3013 return Context->getDependentNameType(Keyword, NNS, Name, Canon); 3014 } 3015 3016 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 3017 unsigned Idx = 0; 3018 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 3019 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 3020 const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); 3021 unsigned NumArgs = Record[Idx++]; 3022 llvm::SmallVector<TemplateArgument, 8> Args; 3023 Args.reserve(NumArgs); 3024 while (NumArgs--) 3025 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 3026 return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, 3027 Args.size(), Args.data()); 3028 } 3029 3030 case TYPE_DEPENDENT_SIZED_ARRAY: { 3031 unsigned Idx = 0; 3032 3033 // ArrayType 3034 QualType ElementType = GetType(Record[Idx++]); 3035 ArrayType::ArraySizeModifier ASM 3036 = (ArrayType::ArraySizeModifier)Record[Idx++]; 3037 unsigned IndexTypeQuals = Record[Idx++]; 3038 3039 // DependentSizedArrayType 3040 Expr *NumElts = ReadExpr(*Loc.F); 3041 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 3042 3043 return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, 3044 IndexTypeQuals, Brackets); 3045 } 3046 3047 case TYPE_TEMPLATE_SPECIALIZATION: { 3048 unsigned Idx = 0; 3049 bool IsDependent = Record[Idx++]; 3050 TemplateName Name = ReadTemplateName(Record, Idx); 3051 llvm::SmallVector<TemplateArgument, 8> Args; 3052 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 3053 QualType Canon = GetType(Record[Idx++]); 3054 QualType T; 3055 if (Canon.isNull()) 3056 T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), 3057 Args.size()); 3058 else 3059 T = Context->getTemplateSpecializationType(Name, Args.data(), 3060 Args.size(), Canon); 3061 T->setDependent(IsDependent); 3062 return T; 3063 } 3064 } 3065 // Suppress a GCC warning 3066 return QualType(); 3067} 3068 3069class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 3070 ASTReader &Reader; 3071 ASTReader::PerFileData &F; 3072 llvm::BitstreamCursor &DeclsCursor; 3073 const ASTReader::RecordData &Record; 3074 unsigned &Idx; 3075 3076 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 3077 unsigned &I) { 3078 return Reader.ReadSourceLocation(F, R, I); 3079 } 3080 3081public: 3082 TypeLocReader(ASTReader &Reader, ASTReader::PerFileData &F, 3083 const ASTReader::RecordData &Record, unsigned &Idx) 3084 : Reader(Reader), F(F), DeclsCursor(F.DeclsCursor), Record(Record), Idx(Idx) 3085 { } 3086 3087 // We want compile-time assurance that we've enumerated all of 3088 // these, so unfortunately we have to declare them first, then 3089 // define them out-of-line. 3090#define ABSTRACT_TYPELOC(CLASS, PARENT) 3091#define TYPELOC(CLASS, PARENT) \ 3092 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 3093#include "clang/AST/TypeLocNodes.def" 3094 3095 void VisitFunctionTypeLoc(FunctionTypeLoc); 3096 void VisitArrayTypeLoc(ArrayTypeLoc); 3097}; 3098 3099void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 3100 // nothing to do 3101} 3102void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 3103 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 3104 if (TL.needsExtraLocalData()) { 3105 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 3106 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 3107 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 3108 TL.setModeAttr(Record[Idx++]); 3109 } 3110} 3111void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 3112 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3113} 3114void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 3115 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3116} 3117void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 3118 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 3119} 3120void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 3121 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 3122} 3123void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 3124 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 3125} 3126void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 3127 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3128} 3129void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 3130 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 3131 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 3132 if (Record[Idx++]) 3133 TL.setSizeExpr(Reader.ReadExpr(F)); 3134 else 3135 TL.setSizeExpr(0); 3136} 3137void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 3138 VisitArrayTypeLoc(TL); 3139} 3140void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 3141 VisitArrayTypeLoc(TL); 3142} 3143void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 3144 VisitArrayTypeLoc(TL); 3145} 3146void TypeLocReader::VisitDependentSizedArrayTypeLoc( 3147 DependentSizedArrayTypeLoc TL) { 3148 VisitArrayTypeLoc(TL); 3149} 3150void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 3151 DependentSizedExtVectorTypeLoc TL) { 3152 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3153} 3154void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 3155 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3156} 3157void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 3158 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3159} 3160void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 3161 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3162 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3163 TL.setTrailingReturn(Record[Idx++]); 3164 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) { 3165 TL.setArg(i, cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); 3166 } 3167} 3168void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 3169 VisitFunctionTypeLoc(TL); 3170} 3171void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 3172 VisitFunctionTypeLoc(TL); 3173} 3174void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 3175 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3176} 3177void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 3178 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3179} 3180void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 3181 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3182 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3183 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3184} 3185void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 3186 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 3187 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3188 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3189 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 3190} 3191void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 3192 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3193} 3194void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 3195 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3196} 3197void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 3198 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3199} 3200void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 3201 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3202} 3203void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 3204 SubstTemplateTypeParmTypeLoc TL) { 3205 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3206} 3207void TypeLocReader::VisitTemplateSpecializationTypeLoc( 3208 TemplateSpecializationTypeLoc TL) { 3209 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 3210 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3211 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3212 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 3213 TL.setArgLocInfo(i, 3214 Reader.GetTemplateArgumentLocInfo(F, 3215 TL.getTypePtr()->getArg(i).getKind(), 3216 Record, Idx)); 3217} 3218void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 3219 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 3220 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 3221} 3222void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 3223 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3224 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3225} 3226void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 3227 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3228} 3229void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 3230 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3231 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3232 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3233} 3234void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 3235 DependentTemplateSpecializationTypeLoc TL) { 3236 TL.setKeywordLoc(ReadSourceLocation(Record, Idx)); 3237 TL.setQualifierRange(Reader.ReadSourceRange(F, Record, Idx)); 3238 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3239 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3240 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3241 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 3242 TL.setArgLocInfo(I, 3243 Reader.GetTemplateArgumentLocInfo(F, 3244 TL.getTypePtr()->getArg(I).getKind(), 3245 Record, Idx)); 3246} 3247void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 3248 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 3249} 3250void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 3251 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 3252} 3253void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 3254 TL.setHasBaseTypeAsWritten(Record[Idx++]); 3255 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 3256 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 3257 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 3258 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 3259} 3260void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 3261 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 3262} 3263 3264TypeSourceInfo *ASTReader::GetTypeSourceInfo(PerFileData &F, 3265 const RecordData &Record, 3266 unsigned &Idx) { 3267 QualType InfoTy = GetType(Record[Idx++]); 3268 if (InfoTy.isNull()) 3269 return 0; 3270 3271 TypeSourceInfo *TInfo = getContext()->CreateTypeSourceInfo(InfoTy); 3272 TypeLocReader TLR(*this, F, Record, Idx); 3273 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 3274 TLR.Visit(TL); 3275 return TInfo; 3276} 3277 3278QualType ASTReader::GetType(TypeID ID) { 3279 unsigned FastQuals = ID & Qualifiers::FastMask; 3280 unsigned Index = ID >> Qualifiers::FastWidth; 3281 3282 if (Index < NUM_PREDEF_TYPE_IDS) { 3283 QualType T; 3284 switch ((PredefinedTypeIDs)Index) { 3285 case PREDEF_TYPE_NULL_ID: return QualType(); 3286 case PREDEF_TYPE_VOID_ID: T = Context->VoidTy; break; 3287 case PREDEF_TYPE_BOOL_ID: T = Context->BoolTy; break; 3288 3289 case PREDEF_TYPE_CHAR_U_ID: 3290 case PREDEF_TYPE_CHAR_S_ID: 3291 // FIXME: Check that the signedness of CharTy is correct! 3292 T = Context->CharTy; 3293 break; 3294 3295 case PREDEF_TYPE_UCHAR_ID: T = Context->UnsignedCharTy; break; 3296 case PREDEF_TYPE_USHORT_ID: T = Context->UnsignedShortTy; break; 3297 case PREDEF_TYPE_UINT_ID: T = Context->UnsignedIntTy; break; 3298 case PREDEF_TYPE_ULONG_ID: T = Context->UnsignedLongTy; break; 3299 case PREDEF_TYPE_ULONGLONG_ID: T = Context->UnsignedLongLongTy; break; 3300 case PREDEF_TYPE_UINT128_ID: T = Context->UnsignedInt128Ty; break; 3301 case PREDEF_TYPE_SCHAR_ID: T = Context->SignedCharTy; break; 3302 case PREDEF_TYPE_WCHAR_ID: T = Context->WCharTy; break; 3303 case PREDEF_TYPE_SHORT_ID: T = Context->ShortTy; break; 3304 case PREDEF_TYPE_INT_ID: T = Context->IntTy; break; 3305 case PREDEF_TYPE_LONG_ID: T = Context->LongTy; break; 3306 case PREDEF_TYPE_LONGLONG_ID: T = Context->LongLongTy; break; 3307 case PREDEF_TYPE_INT128_ID: T = Context->Int128Ty; break; 3308 case PREDEF_TYPE_FLOAT_ID: T = Context->FloatTy; break; 3309 case PREDEF_TYPE_DOUBLE_ID: T = Context->DoubleTy; break; 3310 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context->LongDoubleTy; break; 3311 case PREDEF_TYPE_OVERLOAD_ID: T = Context->OverloadTy; break; 3312 case PREDEF_TYPE_DEPENDENT_ID: T = Context->DependentTy; break; 3313 case PREDEF_TYPE_NULLPTR_ID: T = Context->NullPtrTy; break; 3314 case PREDEF_TYPE_CHAR16_ID: T = Context->Char16Ty; break; 3315 case PREDEF_TYPE_CHAR32_ID: T = Context->Char32Ty; break; 3316 case PREDEF_TYPE_OBJC_ID: T = Context->ObjCBuiltinIdTy; break; 3317 case PREDEF_TYPE_OBJC_CLASS: T = Context->ObjCBuiltinClassTy; break; 3318 case PREDEF_TYPE_OBJC_SEL: T = Context->ObjCBuiltinSelTy; break; 3319 } 3320 3321 assert(!T.isNull() && "Unknown predefined type"); 3322 return T.withFastQualifiers(FastQuals); 3323 } 3324 3325 Index -= NUM_PREDEF_TYPE_IDS; 3326 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 3327 if (TypesLoaded[Index].isNull()) { 3328 TypesLoaded[Index] = ReadTypeRecord(Index); 3329 if (TypesLoaded[Index].isNull()) 3330 return QualType(); 3331 3332 TypesLoaded[Index]->setFromAST(); 3333 TypeIdxs[TypesLoaded[Index]] = TypeIdx::fromTypeID(ID); 3334 if (DeserializationListener) 3335 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 3336 TypesLoaded[Index]); 3337 } 3338 3339 return TypesLoaded[Index].withFastQualifiers(FastQuals); 3340} 3341 3342TypeID ASTReader::GetTypeID(QualType T) const { 3343 return MakeTypeID(T, 3344 std::bind1st(std::mem_fun(&ASTReader::GetTypeIdx), this)); 3345} 3346 3347TypeIdx ASTReader::GetTypeIdx(QualType T) const { 3348 if (T.isNull()) 3349 return TypeIdx(); 3350 assert(!T.getLocalFastQualifiers()); 3351 3352 TypeIdxMap::const_iterator I = TypeIdxs.find(T); 3353 // GetTypeIdx is mostly used for computing the hash of DeclarationNames and 3354 // comparing keys of ASTDeclContextNameLookupTable. 3355 // If the type didn't come from the AST file use a specially marked index 3356 // so that any hash/key comparison fail since no such index is stored 3357 // in a AST file. 3358 if (I == TypeIdxs.end()) 3359 return TypeIdx(-1); 3360 return I->second; 3361} 3362 3363unsigned ASTReader::getTotalNumCXXBaseSpecifiers() const { 3364 unsigned Result = 0; 3365 for (unsigned I = 0, N = Chain.size(); I != N; ++I) 3366 Result += Chain[I]->LocalNumCXXBaseSpecifiers; 3367 3368 return Result; 3369} 3370 3371TemplateArgumentLocInfo 3372ASTReader::GetTemplateArgumentLocInfo(PerFileData &F, 3373 TemplateArgument::ArgKind Kind, 3374 const RecordData &Record, 3375 unsigned &Index) { 3376 switch (Kind) { 3377 case TemplateArgument::Expression: 3378 return ReadExpr(F); 3379 case TemplateArgument::Type: 3380 return GetTypeSourceInfo(F, Record, Index); 3381 case TemplateArgument::Template: { 3382 SourceRange QualifierRange = ReadSourceRange(F, Record, Index); 3383 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 3384 return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc); 3385 } 3386 case TemplateArgument::Null: 3387 case TemplateArgument::Integral: 3388 case TemplateArgument::Declaration: 3389 case TemplateArgument::Pack: 3390 return TemplateArgumentLocInfo(); 3391 } 3392 llvm_unreachable("unexpected template argument loc"); 3393 return TemplateArgumentLocInfo(); 3394} 3395 3396TemplateArgumentLoc 3397ASTReader::ReadTemplateArgumentLoc(PerFileData &F, 3398 const RecordData &Record, unsigned &Index) { 3399 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 3400 3401 if (Arg.getKind() == TemplateArgument::Expression) { 3402 if (Record[Index++]) // bool InfoHasSameExpr. 3403 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 3404 } 3405 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 3406 Record, Index)); 3407} 3408 3409Decl *ASTReader::GetExternalDecl(uint32_t ID) { 3410 return GetDecl(ID); 3411} 3412 3413uint64_t 3414ASTReader::GetCXXBaseSpecifiersOffset(serialization::CXXBaseSpecifiersID ID) { 3415 if (ID == 0) 3416 return 0; 3417 3418 --ID; 3419 uint64_t Offset = 0; 3420 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3421 if (ID < Chain[I]->LocalNumCXXBaseSpecifiers) 3422 return Offset + Chain[I]->CXXBaseSpecifiersOffsets[ID]; 3423 3424 ID -= Chain[I]->LocalNumCXXBaseSpecifiers; 3425 Offset += Chain[I]->SizeInBits; 3426 } 3427 3428 assert(false && "CXXBaseSpecifiers not found"); 3429 return 0; 3430} 3431 3432CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 3433 // Figure out which AST file contains this offset. 3434 PerFileData *F = 0; 3435 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3436 if (Offset < Chain[I]->SizeInBits) { 3437 F = Chain[I]; 3438 break; 3439 } 3440 3441 Offset -= Chain[I]->SizeInBits; 3442 } 3443 3444 if (!F) { 3445 Error("Malformed AST file: C++ base specifiers at impossible offset"); 3446 return 0; 3447 } 3448 3449 llvm::BitstreamCursor &Cursor = F->DeclsCursor; 3450 SavedStreamPosition SavedPosition(Cursor); 3451 Cursor.JumpToBit(Offset); 3452 ReadingKindTracker ReadingKind(Read_Decl, *this); 3453 RecordData Record; 3454 unsigned Code = Cursor.ReadCode(); 3455 unsigned RecCode = Cursor.ReadRecord(Code, Record); 3456 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 3457 Error("Malformed AST file: missing C++ base specifiers"); 3458 return 0; 3459 } 3460 3461 unsigned Idx = 0; 3462 unsigned NumBases = Record[Idx++]; 3463 void *Mem = Context->Allocate(sizeof(CXXBaseSpecifier) * NumBases); 3464 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 3465 for (unsigned I = 0; I != NumBases; ++I) 3466 Bases[I] = ReadCXXBaseSpecifier(*F, Record, Idx); 3467 return Bases; 3468} 3469 3470TranslationUnitDecl *ASTReader::GetTranslationUnitDecl() { 3471 if (!DeclsLoaded[0]) { 3472 ReadDeclRecord(0, 1); 3473 if (DeserializationListener) 3474 DeserializationListener->DeclRead(1, DeclsLoaded[0]); 3475 } 3476 3477 return cast<TranslationUnitDecl>(DeclsLoaded[0]); 3478} 3479 3480Decl *ASTReader::GetDecl(DeclID ID) { 3481 if (ID == 0) 3482 return 0; 3483 3484 if (ID > DeclsLoaded.size()) { 3485 Error("declaration ID out-of-range for AST file"); 3486 return 0; 3487 } 3488 3489 unsigned Index = ID - 1; 3490 if (!DeclsLoaded[Index]) { 3491 ReadDeclRecord(Index, ID); 3492 if (DeserializationListener) 3493 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 3494 } 3495 3496 return DeclsLoaded[Index]; 3497} 3498 3499/// \brief Resolve the offset of a statement into a statement. 3500/// 3501/// This operation will read a new statement from the external 3502/// source each time it is called, and is meant to be used via a 3503/// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 3504Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 3505 // Switch case IDs are per Decl. 3506 ClearSwitchCaseIDs(); 3507 3508 // Offset here is a global offset across the entire chain. 3509 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3510 PerFileData &F = *Chain[N - I - 1]; 3511 if (Offset < F.SizeInBits) { 3512 // Since we know that this statement is part of a decl, make sure to use 3513 // the decl cursor to read it. 3514 F.DeclsCursor.JumpToBit(Offset); 3515 return ReadStmtFromStream(F); 3516 } 3517 Offset -= F.SizeInBits; 3518 } 3519 llvm_unreachable("Broken chain"); 3520} 3521 3522bool ASTReader::FindExternalLexicalDecls(const DeclContext *DC, 3523 bool (*isKindWeWant)(Decl::Kind), 3524 llvm::SmallVectorImpl<Decl*> &Decls) { 3525 assert(DC->hasExternalLexicalStorage() && 3526 "DeclContext has no lexical decls in storage"); 3527 3528 // There might be lexical decls in multiple parts of the chain, for the TU 3529 // at least. 3530 // DeclContextOffsets might reallocate as we load additional decls below, 3531 // so make a copy of the vector. 3532 DeclContextInfos Infos = DeclContextOffsets[DC]; 3533 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3534 I != E; ++I) { 3535 // IDs can be 0 if this context doesn't contain declarations. 3536 if (!I->LexicalDecls) 3537 continue; 3538 3539 // Load all of the declaration IDs 3540 for (const KindDeclIDPair *ID = I->LexicalDecls, 3541 *IDE = ID + I->NumLexicalDecls; ID != IDE; ++ID) { 3542 if (isKindWeWant && !isKindWeWant((Decl::Kind)ID->first)) 3543 continue; 3544 3545 Decl *D = GetDecl(ID->second); 3546 assert(D && "Null decl in lexical decls"); 3547 Decls.push_back(D); 3548 } 3549 } 3550 3551 ++NumLexicalDeclContextsRead; 3552 return false; 3553} 3554 3555DeclContext::lookup_result 3556ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 3557 DeclarationName Name) { 3558 assert(DC->hasExternalVisibleStorage() && 3559 "DeclContext has no visible decls in storage"); 3560 if (!Name) 3561 return DeclContext::lookup_result(DeclContext::lookup_iterator(0), 3562 DeclContext::lookup_iterator(0)); 3563 3564 llvm::SmallVector<NamedDecl *, 64> Decls; 3565 // There might be visible decls in multiple parts of the chain, for the TU 3566 // and namespaces. For any given name, the last available results replace 3567 // all earlier ones. For this reason, we walk in reverse. 3568 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3569 for (DeclContextInfos::reverse_iterator I = Infos.rbegin(), E = Infos.rend(); 3570 I != E; ++I) { 3571 if (!I->NameLookupTableData) 3572 continue; 3573 3574 ASTDeclContextNameLookupTable *LookupTable = 3575 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3576 ASTDeclContextNameLookupTable::iterator Pos = LookupTable->find(Name); 3577 if (Pos == LookupTable->end()) 3578 continue; 3579 3580 ASTDeclContextNameLookupTrait::data_type Data = *Pos; 3581 for (; Data.first != Data.second; ++Data.first) 3582 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3583 break; 3584 } 3585 3586 ++NumVisibleDeclContextsRead; 3587 3588 SetExternalVisibleDeclsForName(DC, Name, Decls); 3589 return const_cast<DeclContext*>(DC)->lookup(Name); 3590} 3591 3592void ASTReader::MaterializeVisibleDecls(const DeclContext *DC) { 3593 assert(DC->hasExternalVisibleStorage() && 3594 "DeclContext has no visible decls in storage"); 3595 3596 llvm::SmallVector<NamedDecl *, 64> Decls; 3597 // There might be visible decls in multiple parts of the chain, for the TU 3598 // and namespaces. 3599 DeclContextInfos &Infos = DeclContextOffsets[DC]; 3600 for (DeclContextInfos::iterator I = Infos.begin(), E = Infos.end(); 3601 I != E; ++I) { 3602 if (!I->NameLookupTableData) 3603 continue; 3604 3605 ASTDeclContextNameLookupTable *LookupTable = 3606 (ASTDeclContextNameLookupTable*)I->NameLookupTableData; 3607 for (ASTDeclContextNameLookupTable::item_iterator 3608 ItemI = LookupTable->item_begin(), 3609 ItemEnd = LookupTable->item_end() ; ItemI != ItemEnd; ++ItemI) { 3610 ASTDeclContextNameLookupTable::item_iterator::value_type Val 3611 = *ItemI; 3612 ASTDeclContextNameLookupTrait::data_type Data = Val.second; 3613 Decls.clear(); 3614 for (; Data.first != Data.second; ++Data.first) 3615 Decls.push_back(cast<NamedDecl>(GetDecl(*Data.first))); 3616 MaterializeVisibleDeclsForName(DC, Val.first, Decls); 3617 } 3618 } 3619} 3620 3621void ASTReader::PassInterestingDeclsToConsumer() { 3622 assert(Consumer); 3623 while (!InterestingDecls.empty()) { 3624 DeclGroupRef DG(InterestingDecls.front()); 3625 InterestingDecls.pop_front(); 3626 Consumer->HandleInterestingDecl(DG); 3627 } 3628} 3629 3630void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 3631 this->Consumer = Consumer; 3632 3633 if (!Consumer) 3634 return; 3635 3636 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { 3637 // Force deserialization of this decl, which will cause it to be queued for 3638 // passing to the consumer. 3639 GetDecl(ExternalDefinitions[I]); 3640 } 3641 3642 PassInterestingDeclsToConsumer(); 3643} 3644 3645void ASTReader::PrintStats() { 3646 std::fprintf(stderr, "*** AST File Statistics:\n"); 3647 3648 unsigned NumTypesLoaded 3649 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 3650 QualType()); 3651 unsigned NumDeclsLoaded 3652 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 3653 (Decl *)0); 3654 unsigned NumIdentifiersLoaded 3655 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 3656 IdentifiersLoaded.end(), 3657 (IdentifierInfo *)0); 3658 unsigned NumSelectorsLoaded 3659 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 3660 SelectorsLoaded.end(), 3661 Selector()); 3662 3663 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits); 3664 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses); 3665 if (TotalNumSLocEntries) 3666 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 3667 NumSLocEntriesRead, TotalNumSLocEntries, 3668 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 3669 if (!TypesLoaded.empty()) 3670 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 3671 NumTypesLoaded, (unsigned)TypesLoaded.size(), 3672 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 3673 if (!DeclsLoaded.empty()) 3674 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 3675 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 3676 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 3677 if (!IdentifiersLoaded.empty()) 3678 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 3679 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 3680 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 3681 if (!SelectorsLoaded.empty()) 3682 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 3683 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 3684 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 3685 if (TotalNumStatements) 3686 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 3687 NumStatementsRead, TotalNumStatements, 3688 ((float)NumStatementsRead/TotalNumStatements * 100)); 3689 if (TotalNumMacros) 3690 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 3691 NumMacrosRead, TotalNumMacros, 3692 ((float)NumMacrosRead/TotalNumMacros * 100)); 3693 if (TotalLexicalDeclContexts) 3694 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 3695 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 3696 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 3697 * 100)); 3698 if (TotalVisibleDeclContexts) 3699 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 3700 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 3701 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 3702 * 100)); 3703 if (TotalNumMethodPoolEntries) { 3704 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 3705 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 3706 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 3707 * 100)); 3708 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses); 3709 } 3710 std::fprintf(stderr, "\n"); 3711} 3712 3713void ASTReader::InitializeSema(Sema &S) { 3714 SemaObj = &S; 3715 S.ExternalSource = this; 3716 3717 // Makes sure any declarations that were deserialized "too early" 3718 // still get added to the identifier's declaration chains. 3719 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) { 3720 if (SemaObj->TUScope) 3721 SemaObj->TUScope->AddDecl(PreloadedDecls[I]); 3722 3723 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]); 3724 } 3725 PreloadedDecls.clear(); 3726 3727 // If there were any tentative definitions, deserialize them and add 3728 // them to Sema's list of tentative definitions. 3729 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 3730 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I])); 3731 SemaObj->TentativeDefinitions.push_back(Var); 3732 } 3733 3734 // If there were any unused file scoped decls, deserialize them and add to 3735 // Sema's list of unused file scoped decls. 3736 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 3737 DeclaratorDecl *D = cast<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 3738 SemaObj->UnusedFileScopedDecls.push_back(D); 3739 } 3740 3741 // If there were any locally-scoped external declarations, 3742 // deserialize them and add them to Sema's table of locally-scoped 3743 // external declarations. 3744 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) { 3745 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I])); 3746 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D; 3747 } 3748 3749 // If there were any ext_vector type declarations, deserialize them 3750 // and add them to Sema's vector of such declarations. 3751 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) 3752 SemaObj->ExtVectorDecls.push_back( 3753 cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); 3754 3755 // FIXME: Do VTable uses and dynamic classes deserialize too much ? 3756 // Can we cut them down before writing them ? 3757 3758 // If there were any dynamic classes declarations, deserialize them 3759 // and add them to Sema's vector of such declarations. 3760 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) 3761 SemaObj->DynamicClasses.push_back( 3762 cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); 3763 3764 // Load the offsets of the declarations that Sema references. 3765 // They will be lazily deserialized when needed. 3766 if (!SemaDeclRefs.empty()) { 3767 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!"); 3768 SemaObj->StdNamespace = SemaDeclRefs[0]; 3769 SemaObj->StdBadAlloc = SemaDeclRefs[1]; 3770 } 3771 3772 for (PerFileData *F = FirstInSource; F; F = F->NextInSource) { 3773 3774 // If there are @selector references added them to its pool. This is for 3775 // implementation of -Wselector. 3776 if (!F->ReferencedSelectorsData.empty()) { 3777 unsigned int DataSize = F->ReferencedSelectorsData.size()-1; 3778 unsigned I = 0; 3779 while (I < DataSize) { 3780 Selector Sel = DecodeSelector(F->ReferencedSelectorsData[I++]); 3781 SourceLocation SelLoc = ReadSourceLocation( 3782 *F, F->ReferencedSelectorsData, I); 3783 SemaObj->ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); 3784 } 3785 } 3786 3787 // If there were any pending implicit instantiations, deserialize them 3788 // and add them to Sema's queue of such instantiations. 3789 assert(F->PendingInstantiations.size() % 2 == 0 && 3790 "Expected pairs of entries"); 3791 for (unsigned Idx = 0, N = F->PendingInstantiations.size(); Idx < N;) { 3792 ValueDecl *D=cast<ValueDecl>(GetDecl(F->PendingInstantiations[Idx++])); 3793 SourceLocation Loc = ReadSourceLocation(*F, F->PendingInstantiations,Idx); 3794 SemaObj->PendingInstantiations.push_back(std::make_pair(D, Loc)); 3795 } 3796 } 3797 3798 // The two special data sets below always come from the most recent PCH, 3799 // which is at the front of the chain. 3800 PerFileData &F = *Chain.front(); 3801 3802 // If there were any weak undeclared identifiers, deserialize them and add to 3803 // Sema's list of weak undeclared identifiers. 3804 if (!WeakUndeclaredIdentifiers.empty()) { 3805 unsigned Idx = 0; 3806 for (unsigned I = 0, N = WeakUndeclaredIdentifiers[Idx++]; I != N; ++I) { 3807 IdentifierInfo *WeakId = GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 3808 IdentifierInfo *AliasId= GetIdentifierInfo(WeakUndeclaredIdentifiers,Idx); 3809 SourceLocation Loc = ReadSourceLocation(F, WeakUndeclaredIdentifiers,Idx); 3810 bool Used = WeakUndeclaredIdentifiers[Idx++]; 3811 Sema::WeakInfo WI(AliasId, Loc); 3812 WI.setUsed(Used); 3813 SemaObj->WeakUndeclaredIdentifiers.insert(std::make_pair(WeakId, WI)); 3814 } 3815 } 3816 3817 // If there were any VTable uses, deserialize the information and add it 3818 // to Sema's vector and map of VTable uses. 3819 if (!VTableUses.empty()) { 3820 unsigned Idx = 0; 3821 for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { 3822 CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 3823 SourceLocation Loc = ReadSourceLocation(F, VTableUses, Idx); 3824 bool DefinitionRequired = VTableUses[Idx++]; 3825 SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); 3826 SemaObj->VTablesUsed[Class] = DefinitionRequired; 3827 } 3828 } 3829} 3830 3831IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) { 3832 // Try to find this name within our on-disk hash tables. We start with the 3833 // most recent one, since that one contains the most up-to-date info. 3834 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3835 ASTIdentifierLookupTable *IdTable 3836 = (ASTIdentifierLookupTable *)Chain[I]->IdentifierLookupTable; 3837 if (!IdTable) 3838 continue; 3839 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart); 3840 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key); 3841 if (Pos == IdTable->end()) 3842 continue; 3843 3844 // Dereferencing the iterator has the effect of building the 3845 // IdentifierInfo node and populating it with the various 3846 // declarations it needs. 3847 return *Pos; 3848 } 3849 return 0; 3850} 3851 3852namespace clang { 3853 /// \brief An identifier-lookup iterator that enumerates all of the 3854 /// identifiers stored within a set of AST files. 3855 class ASTIdentifierIterator : public IdentifierIterator { 3856 /// \brief The AST reader whose identifiers are being enumerated. 3857 const ASTReader &Reader; 3858 3859 /// \brief The current index into the chain of AST files stored in 3860 /// the AST reader. 3861 unsigned Index; 3862 3863 /// \brief The current position within the identifier lookup table 3864 /// of the current AST file. 3865 ASTIdentifierLookupTable::key_iterator Current; 3866 3867 /// \brief The end position within the identifier lookup table of 3868 /// the current AST file. 3869 ASTIdentifierLookupTable::key_iterator End; 3870 3871 public: 3872 explicit ASTIdentifierIterator(const ASTReader &Reader); 3873 3874 virtual llvm::StringRef Next(); 3875 }; 3876} 3877 3878ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader) 3879 : Reader(Reader), Index(Reader.Chain.size() - 1) { 3880 ASTIdentifierLookupTable *IdTable 3881 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable; 3882 Current = IdTable->key_begin(); 3883 End = IdTable->key_end(); 3884} 3885 3886llvm::StringRef ASTIdentifierIterator::Next() { 3887 while (Current == End) { 3888 // If we have exhausted all of our AST files, we're done. 3889 if (Index == 0) 3890 return llvm::StringRef(); 3891 3892 --Index; 3893 ASTIdentifierLookupTable *IdTable 3894 = (ASTIdentifierLookupTable *)Reader.Chain[Index]->IdentifierLookupTable; 3895 Current = IdTable->key_begin(); 3896 End = IdTable->key_end(); 3897 } 3898 3899 // We have any identifiers remaining in the current AST file; return 3900 // the next one. 3901 std::pair<const char*, unsigned> Key = *Current; 3902 ++Current; 3903 return llvm::StringRef(Key.first, Key.second); 3904} 3905 3906IdentifierIterator *ASTReader::getIdentifiers() const { 3907 return new ASTIdentifierIterator(*this); 3908} 3909 3910std::pair<ObjCMethodList, ObjCMethodList> 3911ASTReader::ReadMethodPool(Selector Sel) { 3912 // Find this selector in a hash table. We want to find the most recent entry. 3913 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 3914 PerFileData &F = *Chain[I]; 3915 if (!F.SelectorLookupTable) 3916 continue; 3917 3918 ASTSelectorLookupTable *PoolTable 3919 = (ASTSelectorLookupTable*)F.SelectorLookupTable; 3920 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 3921 if (Pos != PoolTable->end()) { 3922 ++NumSelectorsRead; 3923 // FIXME: Not quite happy with the statistics here. We probably should 3924 // disable this tracking when called via LoadSelector. 3925 // Also, should entries without methods count as misses? 3926 ++NumMethodPoolEntriesRead; 3927 ASTSelectorLookupTrait::data_type Data = *Pos; 3928 if (DeserializationListener) 3929 DeserializationListener->SelectorRead(Data.ID, Sel); 3930 return std::make_pair(Data.Instance, Data.Factory); 3931 } 3932 } 3933 3934 ++NumMethodPoolMisses; 3935 return std::pair<ObjCMethodList, ObjCMethodList>(); 3936} 3937 3938void ASTReader::LoadSelector(Selector Sel) { 3939 // It would be complicated to avoid reading the methods anyway. So don't. 3940 ReadMethodPool(Sel); 3941} 3942 3943void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { 3944 assert(ID && "Non-zero identifier ID required"); 3945 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 3946 IdentifiersLoaded[ID - 1] = II; 3947 if (DeserializationListener) 3948 DeserializationListener->IdentifierRead(ID, II); 3949} 3950 3951/// \brief Set the globally-visible declarations associated with the given 3952/// identifier. 3953/// 3954/// If the AST reader is currently in a state where the given declaration IDs 3955/// cannot safely be resolved, they are queued until it is safe to resolve 3956/// them. 3957/// 3958/// \param II an IdentifierInfo that refers to one or more globally-visible 3959/// declarations. 3960/// 3961/// \param DeclIDs the set of declaration IDs with the name @p II that are 3962/// visible at global scope. 3963/// 3964/// \param Nonrecursive should be true to indicate that the caller knows that 3965/// this call is non-recursive, and therefore the globally-visible declarations 3966/// will not be placed onto the pending queue. 3967void 3968ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 3969 const llvm::SmallVectorImpl<uint32_t> &DeclIDs, 3970 bool Nonrecursive) { 3971 if (NumCurrentElementsDeserializing && !Nonrecursive) { 3972 PendingIdentifierInfos.push_back(PendingIdentifierInfo()); 3973 PendingIdentifierInfo &PII = PendingIdentifierInfos.back(); 3974 PII.II = II; 3975 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end()); 3976 return; 3977 } 3978 3979 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 3980 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 3981 if (SemaObj) { 3982 if (SemaObj->TUScope) { 3983 // Introduce this declaration into the translation-unit scope 3984 // and add it to the declaration chain for this identifier, so 3985 // that (unqualified) name lookup will find it. 3986 SemaObj->TUScope->AddDecl(D); 3987 } 3988 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D); 3989 } else { 3990 // Queue this declaration so that it will be added to the 3991 // translation unit scope and identifier's declaration chain 3992 // once a Sema object is known. 3993 PreloadedDecls.push_back(D); 3994 } 3995 } 3996} 3997 3998IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) { 3999 if (ID == 0) 4000 return 0; 4001 4002 if (IdentifiersLoaded.empty()) { 4003 Error("no identifier table in AST file"); 4004 return 0; 4005 } 4006 4007 assert(PP && "Forgot to set Preprocessor ?"); 4008 ID -= 1; 4009 if (!IdentifiersLoaded[ID]) { 4010 unsigned Index = ID; 4011 const char *Str = 0; 4012 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4013 PerFileData *F = Chain[N - I - 1]; 4014 if (Index < F->LocalNumIdentifiers) { 4015 uint32_t Offset = F->IdentifierOffsets[Index]; 4016 Str = F->IdentifierTableData + Offset; 4017 break; 4018 } 4019 Index -= F->LocalNumIdentifiers; 4020 } 4021 assert(Str && "Broken Chain"); 4022 4023 // All of the strings in the AST file are preceded by a 16-bit length. 4024 // Extract that 16-bit length to avoid having to execute strlen(). 4025 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 4026 // unsigned integers. This is important to avoid integer overflow when 4027 // we cast them to 'unsigned'. 4028 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 4029 unsigned StrLen = (((unsigned) StrLenPtr[0]) 4030 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 4031 IdentifiersLoaded[ID] 4032 = &PP->getIdentifierTable().get(Str, StrLen); 4033 if (DeserializationListener) 4034 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]); 4035 } 4036 4037 return IdentifiersLoaded[ID]; 4038} 4039 4040void ASTReader::ReadSLocEntry(unsigned ID) { 4041 ReadSLocEntryRecord(ID); 4042} 4043 4044Selector ASTReader::DecodeSelector(unsigned ID) { 4045 if (ID == 0) 4046 return Selector(); 4047 4048 if (ID > SelectorsLoaded.size()) { 4049 Error("selector ID out of range in AST file"); 4050 return Selector(); 4051 } 4052 4053 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) { 4054 // Load this selector from the selector table. 4055 unsigned Idx = ID - 1; 4056 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 4057 PerFileData &F = *Chain[N - I - 1]; 4058 if (Idx < F.LocalNumSelectors) { 4059 ASTSelectorLookupTrait Trait(*this); 4060 SelectorsLoaded[ID - 1] = 4061 Trait.ReadKey(F.SelectorLookupTableData + F.SelectorOffsets[Idx], 0); 4062 if (DeserializationListener) 4063 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 4064 break; 4065 } 4066 Idx -= F.LocalNumSelectors; 4067 } 4068 } 4069 4070 return SelectorsLoaded[ID - 1]; 4071} 4072 4073Selector ASTReader::GetExternalSelector(uint32_t ID) { 4074 return DecodeSelector(ID); 4075} 4076 4077uint32_t ASTReader::GetNumExternalSelectors() { 4078 // ID 0 (the null selector) is considered an external selector. 4079 return getTotalNumSelectors() + 1; 4080} 4081 4082DeclarationName 4083ASTReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { 4084 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 4085 switch (Kind) { 4086 case DeclarationName::Identifier: 4087 return DeclarationName(GetIdentifierInfo(Record, Idx)); 4088 4089 case DeclarationName::ObjCZeroArgSelector: 4090 case DeclarationName::ObjCOneArgSelector: 4091 case DeclarationName::ObjCMultiArgSelector: 4092 return DeclarationName(GetSelector(Record, Idx)); 4093 4094 case DeclarationName::CXXConstructorName: 4095 return Context->DeclarationNames.getCXXConstructorName( 4096 Context->getCanonicalType(GetType(Record[Idx++]))); 4097 4098 case DeclarationName::CXXDestructorName: 4099 return Context->DeclarationNames.getCXXDestructorName( 4100 Context->getCanonicalType(GetType(Record[Idx++]))); 4101 4102 case DeclarationName::CXXConversionFunctionName: 4103 return Context->DeclarationNames.getCXXConversionFunctionName( 4104 Context->getCanonicalType(GetType(Record[Idx++]))); 4105 4106 case DeclarationName::CXXOperatorName: 4107 return Context->DeclarationNames.getCXXOperatorName( 4108 (OverloadedOperatorKind)Record[Idx++]); 4109 4110 case DeclarationName::CXXLiteralOperatorName: 4111 return Context->DeclarationNames.getCXXLiteralOperatorName( 4112 GetIdentifierInfo(Record, Idx)); 4113 4114 case DeclarationName::CXXUsingDirective: 4115 return DeclarationName::getUsingDirectiveName(); 4116 } 4117 4118 // Required to silence GCC warning 4119 return DeclarationName(); 4120} 4121 4122void ASTReader::ReadDeclarationNameLoc(PerFileData &F, 4123 DeclarationNameLoc &DNLoc, 4124 DeclarationName Name, 4125 const RecordData &Record, unsigned &Idx) { 4126 switch (Name.getNameKind()) { 4127 case DeclarationName::CXXConstructorName: 4128 case DeclarationName::CXXDestructorName: 4129 case DeclarationName::CXXConversionFunctionName: 4130 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 4131 break; 4132 4133 case DeclarationName::CXXOperatorName: 4134 DNLoc.CXXOperatorName.BeginOpNameLoc 4135 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4136 DNLoc.CXXOperatorName.EndOpNameLoc 4137 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4138 break; 4139 4140 case DeclarationName::CXXLiteralOperatorName: 4141 DNLoc.CXXLiteralOperatorName.OpNameLoc 4142 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 4143 break; 4144 4145 case DeclarationName::Identifier: 4146 case DeclarationName::ObjCZeroArgSelector: 4147 case DeclarationName::ObjCOneArgSelector: 4148 case DeclarationName::ObjCMultiArgSelector: 4149 case DeclarationName::CXXUsingDirective: 4150 break; 4151 } 4152} 4153 4154void ASTReader::ReadDeclarationNameInfo(PerFileData &F, 4155 DeclarationNameInfo &NameInfo, 4156 const RecordData &Record, unsigned &Idx) { 4157 NameInfo.setName(ReadDeclarationName(Record, Idx)); 4158 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 4159 DeclarationNameLoc DNLoc; 4160 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 4161 NameInfo.setInfo(DNLoc); 4162} 4163 4164void ASTReader::ReadQualifierInfo(PerFileData &F, QualifierInfo &Info, 4165 const RecordData &Record, unsigned &Idx) { 4166 Info.NNS = ReadNestedNameSpecifier(Record, Idx); 4167 Info.NNSRange = ReadSourceRange(F, Record, Idx); 4168 unsigned NumTPLists = Record[Idx++]; 4169 Info.NumTemplParamLists = NumTPLists; 4170 if (NumTPLists) { 4171 Info.TemplParamLists = new (*Context) TemplateParameterList*[NumTPLists]; 4172 for (unsigned i=0; i != NumTPLists; ++i) 4173 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 4174 } 4175} 4176 4177TemplateName 4178ASTReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { 4179 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 4180 switch (Kind) { 4181 case TemplateName::Template: 4182 return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); 4183 4184 case TemplateName::OverloadedTemplate: { 4185 unsigned size = Record[Idx++]; 4186 UnresolvedSet<8> Decls; 4187 while (size--) 4188 Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); 4189 4190 return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); 4191 } 4192 4193 case TemplateName::QualifiedTemplate: { 4194 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 4195 bool hasTemplKeyword = Record[Idx++]; 4196 TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); 4197 return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 4198 } 4199 4200 case TemplateName::DependentTemplate: { 4201 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); 4202 if (Record[Idx++]) // isIdentifier 4203 return Context->getDependentTemplateName(NNS, 4204 GetIdentifierInfo(Record, Idx)); 4205 return Context->getDependentTemplateName(NNS, 4206 (OverloadedOperatorKind)Record[Idx++]); 4207 } 4208 } 4209 4210 assert(0 && "Unhandled template name kind!"); 4211 return TemplateName(); 4212} 4213 4214TemplateArgument 4215ASTReader::ReadTemplateArgument(PerFileData &F, 4216 const RecordData &Record, unsigned &Idx) { 4217 switch ((TemplateArgument::ArgKind)Record[Idx++]) { 4218 case TemplateArgument::Null: 4219 return TemplateArgument(); 4220 case TemplateArgument::Type: 4221 return TemplateArgument(GetType(Record[Idx++])); 4222 case TemplateArgument::Declaration: 4223 return TemplateArgument(GetDecl(Record[Idx++])); 4224 case TemplateArgument::Integral: { 4225 llvm::APSInt Value = ReadAPSInt(Record, Idx); 4226 QualType T = GetType(Record[Idx++]); 4227 return TemplateArgument(Value, T); 4228 } 4229 case TemplateArgument::Template: 4230 return TemplateArgument(ReadTemplateName(Record, Idx)); 4231 case TemplateArgument::Expression: 4232 return TemplateArgument(ReadExpr(F)); 4233 case TemplateArgument::Pack: { 4234 unsigned NumArgs = Record[Idx++]; 4235 TemplateArgument *Args = new (*Context) TemplateArgument[NumArgs]; 4236 for (unsigned I = 0; I != NumArgs; ++I) 4237 Args[I] = ReadTemplateArgument(F, Record, Idx); 4238 return TemplateArgument(Args, NumArgs); 4239 } 4240 } 4241 4242 assert(0 && "Unhandled template argument kind!"); 4243 return TemplateArgument(); 4244} 4245 4246TemplateParameterList * 4247ASTReader::ReadTemplateParameterList(PerFileData &F, 4248 const RecordData &Record, unsigned &Idx) { 4249 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 4250 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 4251 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 4252 4253 unsigned NumParams = Record[Idx++]; 4254 llvm::SmallVector<NamedDecl *, 16> Params; 4255 Params.reserve(NumParams); 4256 while (NumParams--) 4257 Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); 4258 4259 TemplateParameterList* TemplateParams = 4260 TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, 4261 Params.data(), Params.size(), RAngleLoc); 4262 return TemplateParams; 4263} 4264 4265void 4266ASTReader:: 4267ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, 4268 PerFileData &F, const RecordData &Record, 4269 unsigned &Idx) { 4270 unsigned NumTemplateArgs = Record[Idx++]; 4271 TemplArgs.reserve(NumTemplateArgs); 4272 while (NumTemplateArgs--) 4273 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx)); 4274} 4275 4276/// \brief Read a UnresolvedSet structure. 4277void ASTReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, 4278 const RecordData &Record, unsigned &Idx) { 4279 unsigned NumDecls = Record[Idx++]; 4280 while (NumDecls--) { 4281 NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); 4282 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 4283 Set.addDecl(D, AS); 4284 } 4285} 4286 4287CXXBaseSpecifier 4288ASTReader::ReadCXXBaseSpecifier(PerFileData &F, 4289 const RecordData &Record, unsigned &Idx) { 4290 bool isVirtual = static_cast<bool>(Record[Idx++]); 4291 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 4292 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 4293 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 4294 SourceRange Range = ReadSourceRange(F, Record, Idx); 4295 return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, TInfo); 4296} 4297 4298std::pair<CXXBaseOrMemberInitializer **, unsigned> 4299ASTReader::ReadCXXBaseOrMemberInitializers(PerFileData &F, 4300 const RecordData &Record, 4301 unsigned &Idx) { 4302 CXXBaseOrMemberInitializer **BaseOrMemberInitializers = 0; 4303 unsigned NumInitializers = Record[Idx++]; 4304 if (NumInitializers) { 4305 ASTContext &C = *getContext(); 4306 4307 BaseOrMemberInitializers 4308 = new (C) CXXBaseOrMemberInitializer*[NumInitializers]; 4309 for (unsigned i=0; i != NumInitializers; ++i) { 4310 TypeSourceInfo *BaseClassInfo = 0; 4311 bool IsBaseVirtual = false; 4312 FieldDecl *Member = 0; 4313 IndirectFieldDecl *IndirectMember = 0; 4314 4315 bool IsBaseInitializer = Record[Idx++]; 4316 if (IsBaseInitializer) { 4317 BaseClassInfo = GetTypeSourceInfo(F, Record, Idx); 4318 IsBaseVirtual = Record[Idx++]; 4319 } else { 4320 bool IsIndirectMemberInitializer = Record[Idx++]; 4321 if (IsIndirectMemberInitializer) 4322 IndirectMember = cast<IndirectFieldDecl>(GetDecl(Record[Idx++])); 4323 else 4324 Member = cast<FieldDecl>(GetDecl(Record[Idx++])); 4325 } 4326 SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx); 4327 Expr *Init = ReadExpr(F); 4328 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 4329 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 4330 bool IsWritten = Record[Idx++]; 4331 unsigned SourceOrderOrNumArrayIndices; 4332 llvm::SmallVector<VarDecl *, 8> Indices; 4333 if (IsWritten) { 4334 SourceOrderOrNumArrayIndices = Record[Idx++]; 4335 } else { 4336 SourceOrderOrNumArrayIndices = Record[Idx++]; 4337 Indices.reserve(SourceOrderOrNumArrayIndices); 4338 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 4339 Indices.push_back(cast<VarDecl>(GetDecl(Record[Idx++]))); 4340 } 4341 4342 CXXBaseOrMemberInitializer *BOMInit; 4343 if (IsBaseInitializer) { 4344 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo, 4345 IsBaseVirtual, LParenLoc, 4346 Init, RParenLoc); 4347 } else if (IsWritten) { 4348 if (Member) 4349 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc, 4350 LParenLoc, Init, 4351 RParenLoc); 4352 else 4353 BOMInit = new (C) CXXBaseOrMemberInitializer(C, IndirectMember, 4354 MemberLoc, LParenLoc, 4355 Init, RParenLoc); 4356 } else { 4357 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc, 4358 LParenLoc, Init, RParenLoc, 4359 Indices.data(), 4360 Indices.size()); 4361 } 4362 4363 if (IsWritten) 4364 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 4365 BaseOrMemberInitializers[i] = BOMInit; 4366 } 4367 } 4368 4369 return std::make_pair(BaseOrMemberInitializers, NumInitializers); 4370} 4371 4372NestedNameSpecifier * 4373ASTReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { 4374 unsigned N = Record[Idx++]; 4375 NestedNameSpecifier *NNS = 0, *Prev = 0; 4376 for (unsigned I = 0; I != N; ++I) { 4377 NestedNameSpecifier::SpecifierKind Kind 4378 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 4379 switch (Kind) { 4380 case NestedNameSpecifier::Identifier: { 4381 IdentifierInfo *II = GetIdentifierInfo(Record, Idx); 4382 NNS = NestedNameSpecifier::Create(*Context, Prev, II); 4383 break; 4384 } 4385 4386 case NestedNameSpecifier::Namespace: { 4387 NamespaceDecl *NS = cast<NamespaceDecl>(GetDecl(Record[Idx++])); 4388 NNS = NestedNameSpecifier::Create(*Context, Prev, NS); 4389 break; 4390 } 4391 4392 case NestedNameSpecifier::TypeSpec: 4393 case NestedNameSpecifier::TypeSpecWithTemplate: { 4394 Type *T = GetType(Record[Idx++]).getTypePtrOrNull(); 4395 if (!T) 4396 return 0; 4397 4398 bool Template = Record[Idx++]; 4399 NNS = NestedNameSpecifier::Create(*Context, Prev, Template, T); 4400 break; 4401 } 4402 4403 case NestedNameSpecifier::Global: { 4404 NNS = NestedNameSpecifier::GlobalSpecifier(*Context); 4405 // No associated value, and there can't be a prefix. 4406 break; 4407 } 4408 } 4409 Prev = NNS; 4410 } 4411 return NNS; 4412} 4413 4414SourceRange 4415ASTReader::ReadSourceRange(PerFileData &F, const RecordData &Record, 4416 unsigned &Idx) { 4417 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 4418 SourceLocation end = ReadSourceLocation(F, Record, Idx); 4419 return SourceRange(beg, end); 4420} 4421 4422/// \brief Read an integral value 4423llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 4424 unsigned BitWidth = Record[Idx++]; 4425 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 4426 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 4427 Idx += NumWords; 4428 return Result; 4429} 4430 4431/// \brief Read a signed integral value 4432llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 4433 bool isUnsigned = Record[Idx++]; 4434 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 4435} 4436 4437/// \brief Read a floating-point value 4438llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) { 4439 return llvm::APFloat(ReadAPInt(Record, Idx)); 4440} 4441 4442// \brief Read a string 4443std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 4444 unsigned Len = Record[Idx++]; 4445 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 4446 Idx += Len; 4447 return Result; 4448} 4449 4450CXXTemporary *ASTReader::ReadCXXTemporary(const RecordData &Record, 4451 unsigned &Idx) { 4452 CXXDestructorDecl *Decl = cast<CXXDestructorDecl>(GetDecl(Record[Idx++])); 4453 return CXXTemporary::Create(*Context, Decl); 4454} 4455 4456DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 4457 return Diag(SourceLocation(), DiagID); 4458} 4459 4460DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 4461 return Diags.Report(Loc, DiagID); 4462} 4463 4464/// \brief Retrieve the identifier table associated with the 4465/// preprocessor. 4466IdentifierTable &ASTReader::getIdentifierTable() { 4467 assert(PP && "Forgot to set Preprocessor ?"); 4468 return PP->getIdentifierTable(); 4469} 4470 4471/// \brief Record that the given ID maps to the given switch-case 4472/// statement. 4473void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 4474 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID"); 4475 SwitchCaseStmts[ID] = SC; 4476} 4477 4478/// \brief Retrieve the switch-case statement with the given ID. 4479SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 4480 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID"); 4481 return SwitchCaseStmts[ID]; 4482} 4483 4484void ASTReader::ClearSwitchCaseIDs() { 4485 SwitchCaseStmts.clear(); 4486} 4487 4488/// \brief Record that the given label statement has been 4489/// deserialized and has the given ID. 4490void ASTReader::RecordLabelStmt(LabelStmt *S, unsigned ID) { 4491 assert(LabelStmts.find(ID) == LabelStmts.end() && 4492 "Deserialized label twice"); 4493 LabelStmts[ID] = S; 4494 4495 // If we've already seen any goto statements that point to this 4496 // label, resolve them now. 4497 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter; 4498 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID); 4499 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto) 4500 Goto->second->setLabel(S); 4501 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second); 4502 4503 // If we've already seen any address-label statements that point to 4504 // this label, resolve them now. 4505 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter; 4506 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels 4507 = UnresolvedAddrLabelExprs.equal_range(ID); 4508 for (AddrLabelIter AddrLabel = AddrLabels.first; 4509 AddrLabel != AddrLabels.second; ++AddrLabel) 4510 AddrLabel->second->setLabel(S); 4511 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second); 4512} 4513 4514/// \brief Set the label of the given statement to the label 4515/// identified by ID. 4516/// 4517/// Depending on the order in which the label and other statements 4518/// referencing that label occur, this operation may complete 4519/// immediately (updating the statement) or it may queue the 4520/// statement to be back-patched later. 4521void ASTReader::SetLabelOf(GotoStmt *S, unsigned ID) { 4522 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); 4523 if (Label != LabelStmts.end()) { 4524 // We've already seen this label, so set the label of the goto and 4525 // we're done. 4526 S->setLabel(Label->second); 4527 } else { 4528 // We haven't seen this label yet, so add this goto to the set of 4529 // unresolved goto statements. 4530 UnresolvedGotoStmts.insert(std::make_pair(ID, S)); 4531 } 4532} 4533 4534/// \brief Set the label of the given expression to the label 4535/// identified by ID. 4536/// 4537/// Depending on the order in which the label and other statements 4538/// referencing that label occur, this operation may complete 4539/// immediately (updating the statement) or it may queue the 4540/// statement to be back-patched later. 4541void ASTReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) { 4542 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID); 4543 if (Label != LabelStmts.end()) { 4544 // We've already seen this label, so set the label of the 4545 // label-address expression and we're done. 4546 S->setLabel(Label->second); 4547 } else { 4548 // We haven't seen this label yet, so add this label-address 4549 // expression to the set of unresolved label-address expressions. 4550 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S)); 4551 } 4552} 4553 4554void ASTReader::FinishedDeserializing() { 4555 assert(NumCurrentElementsDeserializing && 4556 "FinishedDeserializing not paired with StartedDeserializing"); 4557 if (NumCurrentElementsDeserializing == 1) { 4558 // If any identifiers with corresponding top-level declarations have 4559 // been loaded, load those declarations now. 4560 while (!PendingIdentifierInfos.empty()) { 4561 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II, 4562 PendingIdentifierInfos.front().DeclIDs, true); 4563 PendingIdentifierInfos.pop_front(); 4564 } 4565 4566 // We are not in recursive loading, so it's safe to pass the "interesting" 4567 // decls to the consumer. 4568 if (Consumer) 4569 PassInterestingDeclsToConsumer(); 4570 4571 assert(PendingForwardRefs.size() == 0 && 4572 "Some forward refs did not get linked to the definition!"); 4573 } 4574 --NumCurrentElementsDeserializing; 4575} 4576 4577ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context, 4578 const char *isysroot, bool DisableValidation) 4579 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), 4580 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 4581 Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), 4582 Consumer(0), isysroot(isysroot), DisableValidation(DisableValidation), 4583 NumStatHits(0), NumStatMisses(0), NumSLocEntriesRead(0), 4584 TotalNumSLocEntries(0), NextSLocOffset(0), NumStatementsRead(0), 4585 TotalNumStatements(0), NumMacrosRead(0), TotalNumMacros(0), 4586 NumSelectorsRead(0), NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), 4587 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), 4588 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), 4589 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { 4590 RelocatablePCH = false; 4591} 4592 4593ASTReader::ASTReader(SourceManager &SourceMgr, FileManager &FileMgr, 4594 Diagnostic &Diags, const char *isysroot, 4595 bool DisableValidation) 4596 : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), 4597 Diags(Diags), SemaObj(0), PP(0), Context(0), Consumer(0), 4598 isysroot(isysroot), DisableValidation(DisableValidation), NumStatHits(0), 4599 NumStatMisses(0), NumSLocEntriesRead(0), TotalNumSLocEntries(0), 4600 NextSLocOffset(0), NumStatementsRead(0), TotalNumStatements(0), 4601 NumMacrosRead(0), TotalNumMacros(0), NumSelectorsRead(0), 4602 NumMethodPoolEntriesRead(0), NumMethodPoolMisses(0), 4603 TotalNumMethodPoolEntries(0), NumLexicalDeclContextsRead(0), 4604 TotalLexicalDeclContexts(0), NumVisibleDeclContextsRead(0), 4605 TotalVisibleDeclContexts(0), NumCurrentElementsDeserializing(0) { 4606 RelocatablePCH = false; 4607} 4608 4609ASTReader::~ASTReader() { 4610 for (unsigned i = 0, e = Chain.size(); i != e; ++i) 4611 delete Chain[e - i - 1]; 4612 // Delete all visible decl lookup tables 4613 for (DeclContextOffsetsMap::iterator I = DeclContextOffsets.begin(), 4614 E = DeclContextOffsets.end(); 4615 I != E; ++I) { 4616 for (DeclContextInfos::iterator J = I->second.begin(), F = I->second.end(); 4617 J != F; ++J) { 4618 if (J->NameLookupTableData) 4619 delete static_cast<ASTDeclContextNameLookupTable*>( 4620 J->NameLookupTableData); 4621 } 4622 } 4623 for (DeclContextVisibleUpdatesPending::iterator 4624 I = PendingVisibleUpdates.begin(), 4625 E = PendingVisibleUpdates.end(); 4626 I != E; ++I) { 4627 for (DeclContextVisibleUpdates::iterator J = I->second.begin(), 4628 F = I->second.end(); 4629 J != F; ++J) 4630 delete static_cast<ASTDeclContextNameLookupTable*>(*J); 4631 } 4632} 4633 4634ASTReader::PerFileData::PerFileData(ASTFileType Ty) 4635 : Type(Ty), SizeInBits(0), LocalNumSLocEntries(0), SLocOffsets(0), LocalSLocSize(0), 4636 LocalNumIdentifiers(0), IdentifierOffsets(0), IdentifierTableData(0), 4637 IdentifierLookupTable(0), LocalNumMacroDefinitions(0), 4638 MacroDefinitionOffsets(0), LocalNumSelectors(0), SelectorOffsets(0), 4639 SelectorLookupTableData(0), SelectorLookupTable(0), LocalNumDecls(0), 4640 DeclOffsets(0), LocalNumCXXBaseSpecifiers(0), CXXBaseSpecifiersOffsets(0), 4641 LocalNumTypes(0), TypeOffsets(0), StatCache(0), 4642 NumPreallocatedPreprocessingEntities(0), NextInSource(0) 4643{} 4644 4645ASTReader::PerFileData::~PerFileData() { 4646 delete static_cast<ASTIdentifierLookupTable *>(IdentifierLookupTable); 4647 delete static_cast<ASTSelectorLookupTable *>(SelectorLookupTable); 4648} 4649 4650