blob: 5a1e882067408991560c1cf45454b0aeb018923d [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000032#include "llvm/Bitcode/BitstreamReader.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include <algorithm>
36#include <cstdio>
37
38using namespace clang;
39
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000040namespace {
41 /// \brief Helper class that saves the current stream position and
42 /// then restores it when destroyed.
43 struct VISIBILITY_HIDDEN SavedStreamPosition {
44 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
45 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
46
47 ~SavedStreamPosition() {
48 Stream.JumpToBit(Offset);
49 }
50
51 private:
52 llvm::BitstreamReader &Stream;
53 uint64_t Offset;
54 };
55}
56
Douglas Gregorc34897d2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000063 PCHReader &Reader;
64 const PCHReader::RecordData &Record;
65 unsigned &Idx;
66
67 public:
68 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
69 unsigned &Idx)
70 : Reader(Reader), Record(Record), Idx(Idx) { }
71
72 void VisitDecl(Decl *D);
73 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
74 void VisitNamedDecl(NamedDecl *ND);
75 void VisitTypeDecl(TypeDecl *TD);
76 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000085 void VisitParmVarDecl(ParmVarDecl *PD);
86 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000087 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
88 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000089 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000090 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *D);
92 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
93 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000094 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
95 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
96 void VisitObjCClassDecl(ObjCClassDecl *D);
97 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
98 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
99 void VisitObjCImplDecl(ObjCImplDecl *D);
100 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
101 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
102 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
103 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
104 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000105 };
106}
107
108void PCHDeclReader::VisitDecl(Decl *D) {
109 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
110 D->setLexicalDeclContext(
111 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
112 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
113 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000114 if (Record[Idx++])
115 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000116 D->setImplicit(Record[Idx++]);
117 D->setAccess((AccessSpecifier)Record[Idx++]);
118}
119
120void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
121 VisitDecl(TU);
122}
123
124void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
125 VisitDecl(ND);
126 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
127}
128
129void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
130 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000131 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
132}
133
134void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000135 // Note that we cannot use VisitTypeDecl here, because we need to
136 // set the underlying type of the typedef *before* we try to read
137 // the type associated with the TypedefDecl.
138 VisitNamedDecl(TD);
139 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
140 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
141 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000142}
143
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000144void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
145 VisitTypeDecl(TD);
146 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
147 TD->setDefinition(Record[Idx++]);
148 TD->setTypedefForAnonDecl(
149 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
150}
151
152void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
153 VisitTagDecl(ED);
154 ED->setIntegerType(Reader.GetType(Record[Idx++]));
155}
156
Douglas Gregor982365e2009-04-13 21:20:57 +0000157void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
158 VisitTagDecl(RD);
159 RD->setHasFlexibleArrayMember(Record[Idx++]);
160 RD->setAnonymousStructOrUnion(Record[Idx++]);
161}
162
Douglas Gregorc34897d2009-04-09 22:27:44 +0000163void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
164 VisitNamedDecl(VD);
165 VD->setType(Reader.GetType(Record[Idx++]));
166}
167
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000168void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
169 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000170 if (Record[Idx++])
171 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000172 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
173}
174
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000175void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
176 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000177 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000178 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179 FD->setPreviousDeclaration(
180 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
181 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
182 FD->setInline(Record[Idx++]);
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000183 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000184 FD->setVirtual(Record[Idx++]);
185 FD->setPure(Record[Idx++]);
186 FD->setInheritedPrototype(Record[Idx++]);
187 FD->setHasPrototype(Record[Idx++]);
188 FD->setDeleted(Record[Idx++]);
189 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
190 unsigned NumParams = Record[Idx++];
191 llvm::SmallVector<ParmVarDecl *, 16> Params;
192 Params.reserve(NumParams);
193 for (unsigned I = 0; I != NumParams; ++I)
194 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
195 FD->setParams(Reader.getContext(), &Params[0], NumParams);
196}
197
Steve Naroff79ea0e02009-04-20 15:06:07 +0000198void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
199 VisitNamedDecl(MD);
200 if (Record[Idx++]) {
201 // In practice, this won't be executed (since method definitions
202 // don't occur in header files).
203 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
204 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
205 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
206 }
207 MD->setInstanceMethod(Record[Idx++]);
208 MD->setVariadic(Record[Idx++]);
209 MD->setSynthesized(Record[Idx++]);
210 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
211 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
212 MD->setResultType(Reader.GetType(Record[Idx++]));
213 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
214 unsigned NumParams = Record[Idx++];
215 llvm::SmallVector<ParmVarDecl *, 16> Params;
216 Params.reserve(NumParams);
217 for (unsigned I = 0; I != NumParams; ++I)
218 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
219 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
220}
221
Steve Naroff7333b492009-04-20 20:09:33 +0000222void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
223 VisitNamedDecl(CD);
224 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
225}
226
227void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
228 VisitObjCContainerDecl(ID);
229 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000230 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
231 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000232 unsigned NumProtocols = Record[Idx++];
233 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
234 Protocols.reserve(NumProtocols);
235 for (unsigned I = 0; I != NumProtocols; ++I)
236 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000237 unsigned NumIvars = Record[Idx++];
238 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
239 IVars.reserve(NumIvars);
240 for (unsigned I = 0; I != NumIvars; ++I)
241 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
242 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000243 ID->setCategoryList(
244 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000245 ID->setForwardDecl(Record[Idx++]);
246 ID->setImplicitInterfaceDecl(Record[Idx++]);
247 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
248 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000249 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000250}
251
252void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
253 VisitFieldDecl(IVD);
254 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
255}
256
Steve Naroff97b53bd2009-04-21 15:12:33 +0000257void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
258 VisitObjCContainerDecl(PD);
259 PD->setForwardDecl(Record[Idx++]);
260 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
261 unsigned NumProtoRefs = Record[Idx++];
262 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
263 ProtoRefs.reserve(NumProtoRefs);
264 for (unsigned I = 0; I != NumProtoRefs; ++I)
265 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
266 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
267}
268
269void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
270 VisitFieldDecl(FD);
271}
272
273void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
274 VisitDecl(CD);
275 unsigned NumClassRefs = Record[Idx++];
276 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
277 ClassRefs.reserve(NumClassRefs);
278 for (unsigned I = 0; I != NumClassRefs; ++I)
279 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
280 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
281}
282
283void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
284 VisitDecl(FPD);
285 unsigned NumProtoRefs = Record[Idx++];
286 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
287 ProtoRefs.reserve(NumProtoRefs);
288 for (unsigned I = 0; I != NumProtoRefs; ++I)
289 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
290 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
291}
292
293void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
294 VisitObjCContainerDecl(CD);
295 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
296 unsigned NumProtoRefs = Record[Idx++];
297 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
298 ProtoRefs.reserve(NumProtoRefs);
299 for (unsigned I = 0; I != NumProtoRefs; ++I)
300 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
301 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000302 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000303 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
304}
305
306void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
307 VisitNamedDecl(CAD);
308 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
309}
310
311void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
312 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000313 D->setType(Reader.GetType(Record[Idx++]));
314 // FIXME: stable encoding
315 D->setPropertyAttributes(
316 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
317 // FIXME: stable encoding
318 D->setPropertyImplementation(
319 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
320 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
321 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setGetterMethodDecl(
323 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
324 D->setSetterMethodDecl(
325 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
326 D->setPropertyIvarDecl(
327 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000328}
329
330void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000331 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000332 D->setClassInterface(
333 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
334 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000335}
336
337void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
338 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000339 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000340}
341
342void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
343 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000344 D->setSuperClass(
345 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000346}
347
348
349void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
350 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000351 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 D->setPropertyDecl(
353 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
354 D->setPropertyIvarDecl(
355 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000356}
357
Douglas Gregor982365e2009-04-13 21:20:57 +0000358void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
359 VisitValueDecl(FD);
360 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000361 if (Record[Idx++])
362 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000363}
364
Douglas Gregorc34897d2009-04-09 22:27:44 +0000365void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
366 VisitValueDecl(VD);
367 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
368 VD->setThreadSpecified(Record[Idx++]);
369 VD->setCXXDirectInitializer(Record[Idx++]);
370 VD->setDeclaredInCondition(Record[Idx++]);
371 VD->setPreviousDeclaration(
372 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
373 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000374 if (Record[Idx++])
375 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000376}
377
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000378void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
379 VisitVarDecl(PD);
380 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000381 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000382}
383
384void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
385 VisitParmVarDecl(PD);
386 PD->setOriginalType(Reader.GetType(Record[Idx++]));
387}
388
Douglas Gregor2a491792009-04-13 22:49:25 +0000389void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
390 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000391 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000392}
393
394void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
395 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000396 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000397 unsigned NumParams = Record[Idx++];
398 llvm::SmallVector<ParmVarDecl *, 16> Params;
399 Params.reserve(NumParams);
400 for (unsigned I = 0; I != NumParams; ++I)
401 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
402 BD->setParams(Reader.getContext(), &Params[0], NumParams);
403}
404
Douglas Gregorc34897d2009-04-09 22:27:44 +0000405std::pair<uint64_t, uint64_t>
406PCHDeclReader::VisitDeclContext(DeclContext *DC) {
407 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000408 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000409 return std::make_pair(LexicalOffset, VisibleOffset);
410}
411
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000412//===----------------------------------------------------------------------===//
413// Statement/expression deserialization
414//===----------------------------------------------------------------------===//
415namespace {
416 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000417 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000418 PCHReader &Reader;
419 const PCHReader::RecordData &Record;
420 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000421 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000422
423 public:
424 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000425 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
426 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000427
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000428 /// \brief The number of record fields required for the Stmt class
429 /// itself.
430 static const unsigned NumStmtFields = 0;
431
Douglas Gregor596e0932009-04-15 16:35:07 +0000432 /// \brief The number of record fields required for the Expr class
433 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000434 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000435
Douglas Gregora151ba42009-04-14 23:32:43 +0000436 // Each of the Visit* functions reads in part of the expression
437 // from the given record and the current expression stack, then
438 // return the total number of operands that it read from the
439 // expression stack.
440
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000441 unsigned VisitStmt(Stmt *S);
442 unsigned VisitNullStmt(NullStmt *S);
443 unsigned VisitCompoundStmt(CompoundStmt *S);
444 unsigned VisitSwitchCase(SwitchCase *S);
445 unsigned VisitCaseStmt(CaseStmt *S);
446 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000447 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000448 unsigned VisitIfStmt(IfStmt *S);
449 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000450 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000451 unsigned VisitDoStmt(DoStmt *S);
452 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000453 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000454 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000455 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000456 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000457 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000458 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000459 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000460 unsigned VisitExpr(Expr *E);
461 unsigned VisitPredefinedExpr(PredefinedExpr *E);
462 unsigned VisitDeclRefExpr(DeclRefExpr *E);
463 unsigned VisitIntegerLiteral(IntegerLiteral *E);
464 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000465 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000466 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000467 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000468 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000469 unsigned VisitUnaryOperator(UnaryOperator *E);
470 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000471 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000472 unsigned VisitCallExpr(CallExpr *E);
473 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000474 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000475 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000476 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
477 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000478 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000479 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
480 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000481 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000482 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000483 unsigned VisitInitListExpr(InitListExpr *E);
484 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
485 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000486 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000487 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000488 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000489 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
490 unsigned VisitChooseExpr(ChooseExpr *E);
491 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000492 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000493 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000494 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000495 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000496 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000497 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
498 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000499 };
500}
501
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000502unsigned PCHStmtReader::VisitStmt(Stmt *S) {
503 assert(Idx == NumStmtFields && "Incorrect statement field count");
504 return 0;
505}
506
507unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
508 VisitStmt(S);
509 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
510 return 0;
511}
512
513unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
514 VisitStmt(S);
515 unsigned NumStmts = Record[Idx++];
516 S->setStmts(Reader.getContext(),
517 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
518 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
519 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
520 return NumStmts;
521}
522
523unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
524 VisitStmt(S);
525 Reader.RecordSwitchCaseID(S, Record[Idx++]);
526 return 0;
527}
528
529unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
530 VisitSwitchCase(S);
531 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
532 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
533 S->setSubStmt(StmtStack.back());
534 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
535 return 3;
536}
537
538unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
539 VisitSwitchCase(S);
540 S->setSubStmt(StmtStack.back());
541 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 return 1;
543}
544
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000545unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
546 VisitStmt(S);
547 S->setID(Reader.GetIdentifierInfo(Record, Idx));
548 S->setSubStmt(StmtStack.back());
549 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
550 Reader.RecordLabelStmt(S, Record[Idx++]);
551 return 1;
552}
553
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000554unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
555 VisitStmt(S);
556 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
557 S->setThen(StmtStack[StmtStack.size() - 2]);
558 S->setElse(StmtStack[StmtStack.size() - 1]);
559 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
560 return 3;
561}
562
563unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
564 VisitStmt(S);
565 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
566 S->setBody(StmtStack.back());
567 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
568 SwitchCase *PrevSC = 0;
569 for (unsigned N = Record.size(); Idx != N; ++Idx) {
570 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
571 if (PrevSC)
572 PrevSC->setNextSwitchCase(SC);
573 else
574 S->setSwitchCaseList(SC);
575 PrevSC = SC;
576 }
577 return 2;
578}
579
Douglas Gregora6b503f2009-04-17 00:16:09 +0000580unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
581 VisitStmt(S);
582 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
583 S->setBody(StmtStack.back());
584 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
585 return 2;
586}
587
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000588unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
589 VisitStmt(S);
590 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
591 S->setBody(StmtStack.back());
592 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
593 return 2;
594}
595
596unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
597 VisitStmt(S);
598 S->setInit(StmtStack[StmtStack.size() - 4]);
599 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
600 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
601 S->setBody(StmtStack.back());
602 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 return 4;
604}
605
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000606unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
607 VisitStmt(S);
608 Reader.SetLabelOf(S, Record[Idx++]);
609 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
610 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 return 0;
612}
613
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000614unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
615 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000616 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000617 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
618 return 1;
619}
620
Douglas Gregora6b503f2009-04-17 00:16:09 +0000621unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
622 VisitStmt(S);
623 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
624 return 0;
625}
626
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000627unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
628 VisitStmt(S);
629 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630 return 0;
631}
632
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000633unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
634 VisitStmt(S);
635 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
636 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 1;
638}
639
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000640unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
641 VisitStmt(S);
642 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
644
645 if (Idx + 1 == Record.size()) {
646 // Single declaration
647 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
648 } else {
649 llvm::SmallVector<Decl *, 16> Decls;
650 Decls.reserve(Record.size() - Idx);
651 for (unsigned N = Record.size(); Idx != N; ++Idx)
652 Decls.push_back(Reader.GetDecl(Record[Idx]));
653 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
654 &Decls[0], Decls.size())));
655 }
656 return 0;
657}
658
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000659unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
660 VisitStmt(S);
661 unsigned NumOutputs = Record[Idx++];
662 unsigned NumInputs = Record[Idx++];
663 unsigned NumClobbers = Record[Idx++];
664 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
665 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
666 S->setVolatile(Record[Idx++]);
667 S->setSimple(Record[Idx++]);
668
669 unsigned StackIdx
670 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
671 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
672
673 // Outputs and inputs
674 llvm::SmallVector<std::string, 16> Names;
675 llvm::SmallVector<StringLiteral*, 16> Constraints;
676 llvm::SmallVector<Stmt*, 16> Exprs;
677 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
678 Names.push_back(Reader.ReadString(Record, Idx));
679 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
680 Exprs.push_back(StmtStack[StackIdx++]);
681 }
682 S->setOutputsAndInputs(NumOutputs, NumInputs,
683 &Names[0], &Constraints[0], &Exprs[0]);
684
685 // Constraints
686 llvm::SmallVector<StringLiteral*, 16> Clobbers;
687 for (unsigned I = 0; I != NumClobbers; ++I)
688 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
689 S->setClobbers(&Clobbers[0], NumClobbers);
690
691 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
692 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
693}
694
Douglas Gregora151ba42009-04-14 23:32:43 +0000695unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000696 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000697 E->setType(Reader.GetType(Record[Idx++]));
698 E->setTypeDependent(Record[Idx++]);
699 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000700 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000701 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000702}
703
Douglas Gregora151ba42009-04-14 23:32:43 +0000704unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000705 VisitExpr(E);
706 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
707 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000708 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000709}
710
Douglas Gregora151ba42009-04-14 23:32:43 +0000711unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000712 VisitExpr(E);
713 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
714 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000715 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000716}
717
Douglas Gregora151ba42009-04-14 23:32:43 +0000718unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000719 VisitExpr(E);
720 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
721 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000722 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000723}
724
Douglas Gregora151ba42009-04-14 23:32:43 +0000725unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000726 VisitExpr(E);
727 E->setValue(Reader.ReadAPFloat(Record, Idx));
728 E->setExact(Record[Idx++]);
729 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000730 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000731}
732
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000733unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
734 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000735 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000736 return 1;
737}
738
Douglas Gregor596e0932009-04-15 16:35:07 +0000739unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
740 VisitExpr(E);
741 unsigned Len = Record[Idx++];
742 assert(Record[Idx] == E->getNumConcatenated() &&
743 "Wrong number of concatenated tokens!");
744 ++Idx;
745 E->setWide(Record[Idx++]);
746
747 // Read string data
748 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
749 E->setStrData(Reader.getContext(), &Str[0], Len);
750 Idx += Len;
751
752 // Read source locations
753 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
754 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
755
756 return 0;
757}
758
Douglas Gregora151ba42009-04-14 23:32:43 +0000759unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000760 VisitExpr(E);
761 E->setValue(Record[Idx++]);
762 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
763 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000764 return 0;
765}
766
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000767unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
768 VisitExpr(E);
769 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
770 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000771 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000772 return 1;
773}
774
Douglas Gregor12d74052009-04-15 15:58:59 +0000775unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
776 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000777 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000778 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
779 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
780 return 1;
781}
782
783unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
784 VisitExpr(E);
785 E->setSizeof(Record[Idx++]);
786 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000787 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000788 ++Idx;
789 } else {
790 E->setArgument(Reader.GetType(Record[Idx++]));
791 }
792 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 return E->isArgumentType()? 0 : 1;
795}
796
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000797unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
798 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000799 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
800 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000801 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
802 return 2;
803}
804
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000805unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
806 VisitExpr(E);
807 E->setNumArgs(Reader.getContext(), Record[Idx++]);
808 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000809 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000810 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000811 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000812 return E->getNumArgs() + 1;
813}
814
815unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
816 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000817 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000818 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
819 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
820 E->setArrow(Record[Idx++]);
821 return 1;
822}
823
Douglas Gregora151ba42009-04-14 23:32:43 +0000824unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
825 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000826 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000827 return 1;
828}
829
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000830unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
831 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000832 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
833 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000834 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
835 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
836 return 2;
837}
838
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000839unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
840 VisitBinaryOperator(E);
841 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
842 E->setComputationResultType(Reader.GetType(Record[Idx++]));
843 return 2;
844}
845
846unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
847 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000848 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
849 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
850 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000851 return 3;
852}
853
Douglas Gregora151ba42009-04-14 23:32:43 +0000854unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
855 VisitCastExpr(E);
856 E->setLvalueCast(Record[Idx++]);
857 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000858}
859
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000860unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
861 VisitCastExpr(E);
862 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
863 return 1;
864}
865
866unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
867 VisitExplicitCastExpr(E);
868 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
869 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 return 1;
871}
872
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000873unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
874 VisitExpr(E);
875 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000876 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000877 E->setFileScope(Record[Idx++]);
878 return 1;
879}
880
Douglas Gregorec0b8292009-04-15 23:02:49 +0000881unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
882 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000883 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000884 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
885 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
886 return 1;
887}
888
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000889unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
890 VisitExpr(E);
891 unsigned NumInits = Record[Idx++];
892 E->reserveInits(NumInits);
893 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000894 E->updateInit(I,
895 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
896 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000897 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
898 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 E->setInitializedFieldInUnion(
900 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
901 E->sawArrayRangeDesignator(Record[Idx++]);
902 return NumInits + 1;
903}
904
905unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
906 typedef DesignatedInitExpr::Designator Designator;
907
908 VisitExpr(E);
909 unsigned NumSubExprs = Record[Idx++];
910 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
911 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000912 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000913 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
914 E->setGNUSyntax(Record[Idx++]);
915
916 llvm::SmallVector<Designator, 4> Designators;
917 while (Idx < Record.size()) {
918 switch ((pch::DesignatorTypes)Record[Idx++]) {
919 case pch::DESIG_FIELD_DECL: {
920 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
921 SourceLocation DotLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 SourceLocation FieldLoc
924 = SourceLocation::getFromRawEncoding(Record[Idx++]);
925 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
926 FieldLoc));
927 Designators.back().setField(Field);
928 break;
929 }
930
931 case pch::DESIG_FIELD_NAME: {
932 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
933 SourceLocation DotLoc
934 = SourceLocation::getFromRawEncoding(Record[Idx++]);
935 SourceLocation FieldLoc
936 = SourceLocation::getFromRawEncoding(Record[Idx++]);
937 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
938 break;
939 }
940
941 case pch::DESIG_ARRAY: {
942 unsigned Index = Record[Idx++];
943 SourceLocation LBracketLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 SourceLocation RBracketLoc
946 = SourceLocation::getFromRawEncoding(Record[Idx++]);
947 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
948 break;
949 }
950
951 case pch::DESIG_ARRAY_RANGE: {
952 unsigned Index = Record[Idx++];
953 SourceLocation LBracketLoc
954 = SourceLocation::getFromRawEncoding(Record[Idx++]);
955 SourceLocation EllipsisLoc
956 = SourceLocation::getFromRawEncoding(Record[Idx++]);
957 SourceLocation RBracketLoc
958 = SourceLocation::getFromRawEncoding(Record[Idx++]);
959 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
960 RBracketLoc));
961 break;
962 }
963 }
964 }
965 E->setDesignators(&Designators[0], Designators.size());
966
967 return NumSubExprs;
968}
969
970unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
971 VisitExpr(E);
972 return 0;
973}
974
Douglas Gregorec0b8292009-04-15 23:02:49 +0000975unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
976 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000978 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 return 1;
981}
982
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000983unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
984 VisitExpr(E);
985 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 Reader.SetLabelOf(E, Record[Idx++]);
988 return 0;
989}
990
Douglas Gregoreca12f62009-04-17 19:05:30 +0000991unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
992 VisitExpr(E);
993 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
994 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
996 return 1;
997}
998
Douglas Gregor209d4622009-04-15 23:33:31 +0000999unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1000 VisitExpr(E);
1001 E->setArgType1(Reader.GetType(Record[Idx++]));
1002 E->setArgType2(Reader.GetType(Record[Idx++]));
1003 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1005 return 0;
1006}
1007
1008unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1009 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001010 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1011 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1012 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001013 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1014 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 return 3;
1016}
1017
1018unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1019 VisitExpr(E);
1020 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1021 return 0;
1022}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001023
Douglas Gregor725e94b2009-04-16 00:01:45 +00001024unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1025 VisitExpr(E);
1026 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001027 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001028 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1029 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1030 return NumExprs;
1031}
1032
Douglas Gregore246b742009-04-17 19:21:43 +00001033unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1034 VisitExpr(E);
1035 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1036 E->setHasBlockDeclRefExprs(Record[Idx++]);
1037 return 0;
1038}
1039
Douglas Gregor725e94b2009-04-16 00:01:45 +00001040unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1041 VisitExpr(E);
1042 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1043 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1044 E->setByRef(Record[Idx++]);
1045 return 0;
1046}
1047
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001048//===----------------------------------------------------------------------===//
1049// Objective-C Expressions and Statements
1050
1051unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1052 VisitExpr(E);
1053 E->setString(cast<StringLiteral>(StmtStack.back()));
1054 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1055 return 1;
1056}
1057
Chris Lattner80f83c62009-04-22 05:57:30 +00001058unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1059 VisitExpr(E);
1060 E->setEncodedType(Reader.GetType(Record[Idx++]));
1061 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1062 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1063 return 0;
1064}
1065
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001066unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1067 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001068 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001069 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1070 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1071 return 0;
1072}
1073
1074unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1075 VisitExpr(E);
1076 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1077 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1078 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1079 return 0;
1080}
1081
Chris Lattner80f83c62009-04-22 05:57:30 +00001082
Douglas Gregorc713da92009-04-21 22:25:48 +00001083//===----------------------------------------------------------------------===//
1084// PCH reader implementation
1085//===----------------------------------------------------------------------===//
1086
1087namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001088class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1089 PCHReader &Reader;
1090
1091public:
1092 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1093
1094 typedef Selector external_key_type;
1095 typedef external_key_type internal_key_type;
1096
1097 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1098
1099 static bool EqualKey(const internal_key_type& a,
1100 const internal_key_type& b) {
1101 return a == b;
1102 }
1103
1104 static unsigned ComputeHash(Selector Sel) {
1105 unsigned N = Sel.getNumArgs();
1106 if (N == 0)
1107 ++N;
1108 unsigned R = 5381;
1109 for (unsigned I = 0; I != N; ++I)
1110 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1111 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1112 return R;
1113 }
1114
1115 // This hopefully will just get inlined and removed by the optimizer.
1116 static const internal_key_type&
1117 GetInternalKey(const external_key_type& x) { return x; }
1118
1119 static std::pair<unsigned, unsigned>
1120 ReadKeyDataLength(const unsigned char*& d) {
1121 using namespace clang::io;
1122 unsigned KeyLen = ReadUnalignedLE16(d);
1123 unsigned DataLen = ReadUnalignedLE16(d);
1124 return std::make_pair(KeyLen, DataLen);
1125 }
1126
1127 internal_key_type ReadKey(const unsigned char* d, unsigned n) {
1128 using namespace clang::io;
1129 SelectorTable &SelTable = Reader.getContext().Selectors;
1130 unsigned N = ReadUnalignedLE16(d);
1131 IdentifierInfo *FirstII
1132 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1133 if (N == 0)
1134 return SelTable.getNullarySelector(FirstII);
1135 else if (N == 1)
1136 return SelTable.getUnarySelector(FirstII);
1137
1138 llvm::SmallVector<IdentifierInfo *, 16> Args;
1139 Args.push_back(FirstII);
1140 for (unsigned I = 1; I != N; ++I)
1141 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1142
1143 return SelTable.getSelector(N, &Args[0]);
1144 }
1145
1146 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1147 using namespace clang::io;
1148 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1149 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1150
1151 data_type Result;
1152
1153 // Load instance methods
1154 ObjCMethodList *Prev = 0;
1155 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1156 ObjCMethodDecl *Method
1157 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1158 if (!Result.first.Method) {
1159 // This is the first method, which is the easy case.
1160 Result.first.Method = Method;
1161 Prev = &Result.first;
1162 continue;
1163 }
1164
1165 Prev->Next = new ObjCMethodList(Method, 0);
1166 Prev = Prev->Next;
1167 }
1168
1169 // Load factory methods
1170 Prev = 0;
1171 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1172 ObjCMethodDecl *Method
1173 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1174 if (!Result.second.Method) {
1175 // This is the first method, which is the easy case.
1176 Result.second.Method = Method;
1177 Prev = &Result.second;
1178 continue;
1179 }
1180
1181 Prev->Next = new ObjCMethodList(Method, 0);
1182 Prev = Prev->Next;
1183 }
1184
1185 return Result;
1186 }
1187};
1188
1189} // end anonymous namespace
1190
1191/// \brief The on-disk hash table used for the global method pool.
1192typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1193 PCHMethodPoolLookupTable;
1194
1195namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001196class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1197 PCHReader &Reader;
1198
1199 // If we know the IdentifierInfo in advance, it is here and we will
1200 // not build a new one. Used when deserializing information about an
1201 // identifier that was constructed before the PCH file was read.
1202 IdentifierInfo *KnownII;
1203
1204public:
1205 typedef IdentifierInfo * data_type;
1206
1207 typedef const std::pair<const char*, unsigned> external_key_type;
1208
1209 typedef external_key_type internal_key_type;
1210
1211 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1212 : Reader(Reader), KnownII(II) { }
1213
1214 static bool EqualKey(const internal_key_type& a,
1215 const internal_key_type& b) {
1216 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1217 : false;
1218 }
1219
1220 static unsigned ComputeHash(const internal_key_type& a) {
1221 return BernsteinHash(a.first, a.second);
1222 }
1223
1224 // This hopefully will just get inlined and removed by the optimizer.
1225 static const internal_key_type&
1226 GetInternalKey(const external_key_type& x) { return x; }
1227
1228 static std::pair<unsigned, unsigned>
1229 ReadKeyDataLength(const unsigned char*& d) {
1230 using namespace clang::io;
1231 unsigned KeyLen = ReadUnalignedLE16(d);
1232 unsigned DataLen = ReadUnalignedLE16(d);
1233 return std::make_pair(KeyLen, DataLen);
1234 }
1235
1236 static std::pair<const char*, unsigned>
1237 ReadKey(const unsigned char* d, unsigned n) {
1238 assert(n >= 2 && d[n-1] == '\0');
1239 return std::make_pair((const char*) d, n-1);
1240 }
1241
1242 IdentifierInfo *ReadData(const internal_key_type& k,
1243 const unsigned char* d,
1244 unsigned DataLen) {
1245 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001246 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001247 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1248 Bits >>= 1;
1249 bool Poisoned = Bits & 0x01;
1250 Bits >>= 1;
1251 bool ExtensionToken = Bits & 0x01;
1252 Bits >>= 1;
1253 bool hasMacroDefinition = Bits & 0x01;
1254 Bits >>= 1;
1255 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1256 Bits >>= 10;
1257 unsigned TokenID = Bits & 0xFF;
1258 Bits >>= 8;
1259
Douglas Gregorc713da92009-04-21 22:25:48 +00001260 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001261 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001262 DataLen -= 8;
1263
1264 // Build the IdentifierInfo itself and link the identifier ID with
1265 // the new IdentifierInfo.
1266 IdentifierInfo *II = KnownII;
1267 if (!II)
1268 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1269 k.first, k.first + k.second);
1270 Reader.SetIdentifierInfo(ID, II);
1271
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001272 // Set or check the various bits in the IdentifierInfo structure.
1273 // FIXME: Load token IDs lazily, too?
1274 assert((unsigned)II->getTokenID() == TokenID &&
1275 "Incorrect token ID loaded");
1276 (void)TokenID;
1277 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1278 assert(II->isExtensionToken() == ExtensionToken &&
1279 "Incorrect extension token flag");
1280 (void)ExtensionToken;
1281 II->setIsPoisoned(Poisoned);
1282 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1283 "Incorrect C++ operator keyword flag");
1284 (void)CPlusPlusOperatorKeyword;
1285
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001286 // If this identifier is a macro, deserialize the macro
1287 // definition.
1288 if (hasMacroDefinition) {
1289 uint32_t Offset = ReadUnalignedLE64(d);
1290 Reader.ReadMacroRecord(Offset);
1291 DataLen -= 8;
1292 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001293
1294 // Read all of the declarations visible at global scope with this
1295 // name.
1296 Sema *SemaObj = Reader.getSema();
1297 while (DataLen > 0) {
1298 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001299 if (SemaObj) {
1300 // Introduce this declaration into the translation-unit scope
1301 // and add it to the declaration chain for this identifier, so
1302 // that (unqualified) name lookup will find it.
1303 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1304 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1305 } else {
1306 // Queue this declaration so that it will be added to the
1307 // translation unit scope and identifier's declaration chain
1308 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001309 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001310 }
1311
1312 DataLen -= 4;
1313 }
1314 return II;
1315 }
1316};
1317
1318} // end anonymous namespace
1319
1320/// \brief The on-disk hash table used to contain information about
1321/// all of the identifiers in the program.
1322typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1323 PCHIdentifierLookupTable;
1324
Douglas Gregorc34897d2009-04-09 22:27:44 +00001325// FIXME: use the diagnostics machinery
1326static bool Error(const char *Str) {
1327 std::fprintf(stderr, "%s\n", Str);
1328 return true;
1329}
1330
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001331/// \brief Check the contents of the predefines buffer against the
1332/// contents of the predefines buffer used to build the PCH file.
1333///
1334/// The contents of the two predefines buffers should be the same. If
1335/// not, then some command-line option changed the preprocessor state
1336/// and we must reject the PCH file.
1337///
1338/// \param PCHPredef The start of the predefines buffer in the PCH
1339/// file.
1340///
1341/// \param PCHPredefLen The length of the predefines buffer in the PCH
1342/// file.
1343///
1344/// \param PCHBufferID The FileID for the PCH predefines buffer.
1345///
1346/// \returns true if there was a mismatch (in which case the PCH file
1347/// should be ignored), or false otherwise.
1348bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1349 unsigned PCHPredefLen,
1350 FileID PCHBufferID) {
1351 const char *Predef = PP.getPredefines().c_str();
1352 unsigned PredefLen = PP.getPredefines().size();
1353
1354 // If the two predefines buffers compare equal, we're done!.
1355 if (PredefLen == PCHPredefLen &&
1356 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1357 return false;
1358
1359 // The predefines buffers are different. Produce a reasonable
1360 // diagnostic showing where they are different.
1361
1362 // The source locations (potentially in the two different predefines
1363 // buffers)
1364 SourceLocation Loc1, Loc2;
1365 SourceManager &SourceMgr = PP.getSourceManager();
1366
1367 // Create a source buffer for our predefines string, so
1368 // that we can build a diagnostic that points into that
1369 // source buffer.
1370 FileID BufferID;
1371 if (Predef && Predef[0]) {
1372 llvm::MemoryBuffer *Buffer
1373 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1374 "<built-in>");
1375 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1376 }
1377
1378 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1379 std::pair<const char *, const char *> Locations
1380 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1381
1382 if (Locations.first != Predef + MinLen) {
1383 // We found the location in the two buffers where there is a
1384 // difference. Form source locations to point there (in both
1385 // buffers).
1386 unsigned Offset = Locations.first - Predef;
1387 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1388 .getFileLocWithOffset(Offset);
1389 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1390 .getFileLocWithOffset(Offset);
1391 } else if (PredefLen > PCHPredefLen) {
1392 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1393 .getFileLocWithOffset(MinLen);
1394 } else {
1395 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1396 .getFileLocWithOffset(MinLen);
1397 }
1398
1399 Diag(Loc1, diag::warn_pch_preprocessor);
1400 if (Loc2.isValid())
1401 Diag(Loc2, diag::note_predef_in_pch);
1402 Diag(diag::note_ignoring_pch) << FileName;
1403 return true;
1404}
1405
Douglas Gregor635f97f2009-04-13 16:31:14 +00001406/// \brief Read the line table in the source manager block.
1407/// \returns true if ther was an error.
1408static bool ParseLineTable(SourceManager &SourceMgr,
1409 llvm::SmallVectorImpl<uint64_t> &Record) {
1410 unsigned Idx = 0;
1411 LineTableInfo &LineTable = SourceMgr.getLineTable();
1412
1413 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001414 std::map<int, int> FileIDs;
1415 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001416 // Extract the file name
1417 unsigned FilenameLen = Record[Idx++];
1418 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1419 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001420 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1421 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001422 }
1423
1424 // Parse the line entries
1425 std::vector<LineEntry> Entries;
1426 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001427 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001428
1429 // Extract the line entries
1430 unsigned NumEntries = Record[Idx++];
1431 Entries.clear();
1432 Entries.reserve(NumEntries);
1433 for (unsigned I = 0; I != NumEntries; ++I) {
1434 unsigned FileOffset = Record[Idx++];
1435 unsigned LineNo = Record[Idx++];
1436 int FilenameID = Record[Idx++];
1437 SrcMgr::CharacteristicKind FileKind
1438 = (SrcMgr::CharacteristicKind)Record[Idx++];
1439 unsigned IncludeOffset = Record[Idx++];
1440 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1441 FileKind, IncludeOffset));
1442 }
1443 LineTable.AddEntry(FID, Entries);
1444 }
1445
1446 return false;
1447}
1448
Douglas Gregorab1cef72009-04-10 03:52:48 +00001449/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001450PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001451 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001452 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1453 Error("Malformed source manager block record");
1454 return Failure;
1455 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001456
1457 SourceManager &SourceMgr = Context.getSourceManager();
1458 RecordData Record;
1459 while (true) {
1460 unsigned Code = Stream.ReadCode();
1461 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001462 if (Stream.ReadBlockEnd()) {
1463 Error("Error at end of Source Manager block");
1464 return Failure;
1465 }
1466
1467 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001468 }
1469
1470 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1471 // No known subblocks, always skip them.
1472 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001473 if (Stream.SkipBlock()) {
1474 Error("Malformed block record");
1475 return Failure;
1476 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001477 continue;
1478 }
1479
1480 if (Code == llvm::bitc::DEFINE_ABBREV) {
1481 Stream.ReadAbbrevRecord();
1482 continue;
1483 }
1484
1485 // Read a record.
1486 const char *BlobStart;
1487 unsigned BlobLen;
1488 Record.clear();
1489 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1490 default: // Default behavior: ignore.
1491 break;
1492
1493 case pch::SM_SLOC_FILE_ENTRY: {
1494 // FIXME: We would really like to delay the creation of this
1495 // FileEntry until it is actually required, e.g., when producing
1496 // a diagnostic with a source location in this file.
1497 const FileEntry *File
1498 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1499 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001500 FileID ID = SourceMgr.createFileID(File,
1501 SourceLocation::getFromRawEncoding(Record[1]),
1502 (CharacteristicKind)Record[2]);
1503 if (Record[3])
1504 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1505 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001506 break;
1507 }
1508
1509 case pch::SM_SLOC_BUFFER_ENTRY: {
1510 const char *Name = BlobStart;
1511 unsigned Code = Stream.ReadCode();
1512 Record.clear();
1513 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1514 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001515 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001516 llvm::MemoryBuffer *Buffer
1517 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1518 BlobStart + BlobLen - 1,
1519 Name);
1520 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1521
1522 if (strcmp(Name, "<built-in>") == 0
1523 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1524 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001525 break;
1526 }
1527
1528 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1529 SourceLocation SpellingLoc
1530 = SourceLocation::getFromRawEncoding(Record[1]);
1531 SourceMgr.createInstantiationLoc(
1532 SpellingLoc,
1533 SourceLocation::getFromRawEncoding(Record[2]),
1534 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001535 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001536 break;
1537 }
1538
Chris Lattnere1be6022009-04-14 23:22:57 +00001539 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001540 if (ParseLineTable(SourceMgr, Record))
1541 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001542 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001543 }
1544 }
1545}
1546
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001547void PCHReader::ReadMacroRecord(uint64_t Offset) {
1548 // Keep track of where we are in the stream, then jump back there
1549 // after reading this macro.
1550 SavedStreamPosition SavedPosition(Stream);
1551
1552 Stream.JumpToBit(Offset);
1553 RecordData Record;
1554 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1555 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001556
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001557 while (true) {
1558 unsigned Code = Stream.ReadCode();
1559 switch (Code) {
1560 case llvm::bitc::END_BLOCK:
1561 return;
1562
1563 case llvm::bitc::ENTER_SUBBLOCK:
1564 // No known subblocks, always skip them.
1565 Stream.ReadSubBlockID();
1566 if (Stream.SkipBlock()) {
1567 Error("Malformed block record");
1568 return;
1569 }
1570 continue;
1571
1572 case llvm::bitc::DEFINE_ABBREV:
1573 Stream.ReadAbbrevRecord();
1574 continue;
1575 default: break;
1576 }
1577
1578 // Read a record.
1579 Record.clear();
1580 pch::PreprocessorRecordTypes RecType =
1581 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1582 switch (RecType) {
1583 case pch::PP_COUNTER_VALUE:
1584 // Skip this record.
1585 break;
1586
1587 case pch::PP_MACRO_OBJECT_LIKE:
1588 case pch::PP_MACRO_FUNCTION_LIKE: {
1589 // If we already have a macro, that means that we've hit the end
1590 // of the definition of the macro we were looking for. We're
1591 // done.
1592 if (Macro)
1593 return;
1594
1595 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1596 if (II == 0) {
1597 Error("Macro must have a name");
1598 return;
1599 }
1600 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1601 bool isUsed = Record[2];
1602
1603 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1604 MI->setIsUsed(isUsed);
1605
1606 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1607 // Decode function-like macro info.
1608 bool isC99VarArgs = Record[3];
1609 bool isGNUVarArgs = Record[4];
1610 MacroArgs.clear();
1611 unsigned NumArgs = Record[5];
1612 for (unsigned i = 0; i != NumArgs; ++i)
1613 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1614
1615 // Install function-like macro info.
1616 MI->setIsFunctionLike();
1617 if (isC99VarArgs) MI->setIsC99Varargs();
1618 if (isGNUVarArgs) MI->setIsGNUVarargs();
1619 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1620 PP.getPreprocessorAllocator());
1621 }
1622
1623 // Finally, install the macro.
1624 PP.setMacroInfo(II, MI);
1625
1626 // Remember that we saw this macro last so that we add the tokens that
1627 // form its body to it.
1628 Macro = MI;
1629 ++NumMacrosRead;
1630 break;
1631 }
1632
1633 case pch::PP_TOKEN: {
1634 // If we see a TOKEN before a PP_MACRO_*, then the file is
1635 // erroneous, just pretend we didn't see this.
1636 if (Macro == 0) break;
1637
1638 Token Tok;
1639 Tok.startToken();
1640 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1641 Tok.setLength(Record[1]);
1642 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1643 Tok.setIdentifierInfo(II);
1644 Tok.setKind((tok::TokenKind)Record[3]);
1645 Tok.setFlag((Token::TokenFlags)Record[4]);
1646 Macro->AddTokenToBody(Tok);
1647 break;
1648 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001649 case pch::PP_HEADER_FILE_INFO:
1650 break; // Already processed by ReadPreprocessorBlock().
1651 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001652 }
1653}
1654
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001655bool PCHReader::ReadPreprocessorBlock() {
1656 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1657 return Error("Malformed preprocessor block record");
1658
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001659 RecordData Record;
Steve Naroffcda68f22009-04-24 20:03:17 +00001660 unsigned NumHeaderInfos = 0;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001661 while (true) {
1662 unsigned Code = Stream.ReadCode();
1663 switch (Code) {
1664 case llvm::bitc::END_BLOCK:
1665 if (Stream.ReadBlockEnd())
1666 return Error("Error at end of preprocessor block");
1667 return false;
1668
1669 case llvm::bitc::ENTER_SUBBLOCK:
1670 // No known subblocks, always skip them.
1671 Stream.ReadSubBlockID();
1672 if (Stream.SkipBlock())
1673 return Error("Malformed block record");
1674 continue;
1675
1676 case llvm::bitc::DEFINE_ABBREV:
1677 Stream.ReadAbbrevRecord();
1678 continue;
1679 default: break;
1680 }
1681
1682 // Read a record.
1683 Record.clear();
1684 pch::PreprocessorRecordTypes RecType =
1685 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1686 switch (RecType) {
1687 default: // Default behavior: ignore unknown records.
1688 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001689 case pch::PP_COUNTER_VALUE:
1690 if (!Record.empty())
1691 PP.setCounterValue(Record[0]);
1692 break;
1693
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001694 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001695 case pch::PP_MACRO_FUNCTION_LIKE:
1696 case pch::PP_TOKEN:
Steve Naroffcda68f22009-04-24 20:03:17 +00001697 break;
1698 case pch::PP_HEADER_FILE_INFO: {
1699 HeaderFileInfo HFI;
1700 HFI.isImport = Record[0];
1701 HFI.DirInfo = Record[1];
1702 HFI.NumIncludes = Record[2];
1703 HFI.ControllingMacro = DecodeIdentifierInfo(Record[3]);
1704 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1705 break;
1706 }
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001707 }
1708 }
1709}
1710
Steve Naroff9e84d782009-04-23 10:39:46 +00001711bool PCHReader::ReadSelectorBlock() {
1712 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1713 return Error("Malformed selector block record");
1714
1715 RecordData Record;
1716 while (true) {
1717 unsigned Code = Stream.ReadCode();
1718 switch (Code) {
1719 case llvm::bitc::END_BLOCK:
1720 if (Stream.ReadBlockEnd())
1721 return Error("Error at end of preprocessor block");
1722 return false;
1723
1724 case llvm::bitc::ENTER_SUBBLOCK:
1725 // No known subblocks, always skip them.
1726 Stream.ReadSubBlockID();
1727 if (Stream.SkipBlock())
1728 return Error("Malformed block record");
1729 continue;
1730
1731 case llvm::bitc::DEFINE_ABBREV:
1732 Stream.ReadAbbrevRecord();
1733 continue;
1734 default: break;
1735 }
1736
1737 // Read a record.
1738 Record.clear();
1739 pch::PCHRecordTypes RecType =
1740 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1741 switch (RecType) {
1742 default: // Default behavior: ignore unknown records.
1743 break;
1744 case pch::SELECTOR_TABLE:
1745 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1746 unsigned NumSels = Record[Idx++];
1747
1748 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1749 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1750 unsigned NumArgs = Record[Idx++];
1751 KeyIdents.clear();
1752 if (NumArgs <= 1) {
1753 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1754 assert(II && "DecodeIdentifierInfo returned 0");
1755 KeyIdents.push_back(II);
1756 } else {
1757 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1758 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1759 assert(II && "DecodeIdentifierInfo returned 0");
1760 KeyIdents.push_back(II);
1761 }
1762 }
1763 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1764 SelectorData.push_back(Sel);
1765 }
1766 }
1767 }
1768 return false;
1769}
1770
Douglas Gregorc713da92009-04-21 22:25:48 +00001771PCHReader::PCHReadResult
Steve Naroff9e84d782009-04-23 10:39:46 +00001772PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1773 uint64_t &SelectorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001774 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1775 Error("Malformed block record");
1776 return Failure;
1777 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001778
1779 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001780 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781 while (!Stream.AtEndOfStream()) {
1782 unsigned Code = Stream.ReadCode();
1783 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001784 if (Stream.ReadBlockEnd()) {
1785 Error("Error at end of module block");
1786 return Failure;
1787 }
Chris Lattner29241862009-04-11 21:15:38 +00001788
Douglas Gregor179cfb12009-04-10 20:39:37 +00001789 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001790 }
1791
1792 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1793 switch (Stream.ReadSubBlockID()) {
1794 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1795 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1796 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001797 if (Stream.SkipBlock()) {
1798 Error("Malformed block record");
1799 return Failure;
1800 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001801 break;
1802
Chris Lattner29241862009-04-11 21:15:38 +00001803 case pch::PREPROCESSOR_BLOCK_ID:
1804 // Skip the preprocessor block for now, but remember where it is. We
1805 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001806 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001807 Error("Multiple preprocessor blocks found.");
1808 return Failure;
1809 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001810 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001811 if (Stream.SkipBlock()) {
1812 Error("Malformed block record");
1813 return Failure;
1814 }
1815 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001816
1817 case pch::SELECTOR_BLOCK_ID:
1818 // Skip the selector block for now, but remember where it is. We
1819 // want to read it in after the identifier table.
1820 if (SelectorBlockOffset) {
1821 Error("Multiple selector blocks found.");
1822 return Failure;
1823 }
1824 SelectorBlockOffset = Stream.GetCurrentBitNo();
1825 if (Stream.SkipBlock()) {
1826 Error("Malformed block record");
1827 return Failure;
1828 }
1829 break;
Chris Lattner29241862009-04-11 21:15:38 +00001830
Douglas Gregorab1cef72009-04-10 03:52:48 +00001831 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001832 switch (ReadSourceManagerBlock()) {
1833 case Success:
1834 break;
1835
1836 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001837 Error("Malformed source manager block");
1838 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001839
1840 case IgnorePCH:
1841 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001842 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001843 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001844 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001845 continue;
1846 }
1847
1848 if (Code == llvm::bitc::DEFINE_ABBREV) {
1849 Stream.ReadAbbrevRecord();
1850 continue;
1851 }
1852
1853 // Read and process a record.
1854 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001855 const char *BlobStart = 0;
1856 unsigned BlobLen = 0;
1857 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1858 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001859 default: // Default behavior: ignore.
1860 break;
1861
1862 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001863 if (!TypeOffsets.empty()) {
1864 Error("Duplicate TYPE_OFFSET record in PCH file");
1865 return Failure;
1866 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001867 TypeOffsets.swap(Record);
1868 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1869 break;
1870
1871 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001872 if (!DeclOffsets.empty()) {
1873 Error("Duplicate DECL_OFFSET record in PCH file");
1874 return Failure;
1875 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001876 DeclOffsets.swap(Record);
1877 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1878 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001879
1880 case pch::LANGUAGE_OPTIONS:
1881 if (ParseLanguageOptions(Record))
1882 return IgnorePCH;
1883 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001884
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001885 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001886 std::string TargetTriple(BlobStart, BlobLen);
1887 if (TargetTriple != Context.Target.getTargetTriple()) {
1888 Diag(diag::warn_pch_target_triple)
1889 << TargetTriple << Context.Target.getTargetTriple();
1890 Diag(diag::note_ignoring_pch) << FileName;
1891 return IgnorePCH;
1892 }
1893 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001894 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001895
1896 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001897 IdentifierTableData = BlobStart;
1898 IdentifierLookupTable
1899 = PCHIdentifierLookupTable::Create(
1900 (const unsigned char *)IdentifierTableData + Record[0],
1901 (const unsigned char *)IdentifierTableData,
1902 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001903 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001904 break;
1905
1906 case pch::IDENTIFIER_OFFSET:
1907 if (!IdentifierData.empty()) {
1908 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1909 return Failure;
1910 }
1911 IdentifierData.swap(Record);
1912#ifndef NDEBUG
1913 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1914 if ((IdentifierData[I] & 0x01) == 0) {
1915 Error("Malformed identifier table in the precompiled header");
1916 return Failure;
1917 }
1918 }
1919#endif
1920 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001921
1922 case pch::EXTERNAL_DEFINITIONS:
1923 if (!ExternalDefinitions.empty()) {
1924 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1925 return Failure;
1926 }
1927 ExternalDefinitions.swap(Record);
1928 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001929
Douglas Gregore01ad442009-04-18 05:55:16 +00001930 case pch::SPECIAL_TYPES:
1931 SpecialTypes.swap(Record);
1932 break;
1933
Douglas Gregor456e0952009-04-17 22:13:46 +00001934 case pch::STATISTICS:
1935 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001936 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001937 TotalLexicalDeclContexts = Record[2];
1938 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001939 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001940 case pch::TENTATIVE_DEFINITIONS:
1941 if (!TentativeDefinitions.empty()) {
1942 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1943 return Failure;
1944 }
1945 TentativeDefinitions.swap(Record);
1946 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001947
1948 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1949 if (!LocallyScopedExternalDecls.empty()) {
1950 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1951 return Failure;
1952 }
1953 LocallyScopedExternalDecls.swap(Record);
1954 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001955
1956 case pch::METHOD_POOL:
1957 MethodPoolLookupTable
1958 = PCHMethodPoolLookupTable::Create(
1959 (const unsigned char *)BlobStart + Record[0],
1960 (const unsigned char *)BlobStart,
1961 PCHMethodPoolLookupTrait(*this));
1962 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001963 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001964 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001965 Error("Premature end of bitstream");
1966 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001967}
1968
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001969PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001970 // Set the PCH file name.
1971 this->FileName = FileName;
1972
Douglas Gregorc34897d2009-04-09 22:27:44 +00001973 // Open the PCH file.
1974 std::string ErrStr;
1975 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001976 if (!Buffer) {
1977 Error(ErrStr.c_str());
1978 return IgnorePCH;
1979 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001980
1981 // Initialize the stream
1982 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1983 (const unsigned char *)Buffer->getBufferEnd());
1984
1985 // Sniff for the signature.
1986 if (Stream.Read(8) != 'C' ||
1987 Stream.Read(8) != 'P' ||
1988 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001989 Stream.Read(8) != 'H') {
1990 Error("Not a PCH file");
1991 return IgnorePCH;
1992 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001993
1994 // We expect a number of well-defined blocks, though we don't necessarily
1995 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001996 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff9e84d782009-04-23 10:39:46 +00001997 uint64_t SelectorBlockOffset = 0;
1998
Douglas Gregorc34897d2009-04-09 22:27:44 +00001999 while (!Stream.AtEndOfStream()) {
2000 unsigned Code = Stream.ReadCode();
2001
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002002 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2003 Error("Invalid record at top-level");
2004 return Failure;
2005 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002006
2007 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00002008
Douglas Gregorc34897d2009-04-09 22:27:44 +00002009 // We only know the PCH subblock ID.
2010 switch (BlockID) {
2011 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002012 if (Stream.ReadBlockInfoBlock()) {
2013 Error("Malformed BlockInfoBlock");
2014 return Failure;
2015 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002016 break;
2017 case pch::PCH_BLOCK_ID:
Steve Naroff9e84d782009-04-23 10:39:46 +00002018 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002019 case Success:
2020 break;
2021
2022 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002023 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002024
2025 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002026 // FIXME: We could consider reading through to the end of this
2027 // PCH block, skipping subblocks, to see if there are other
2028 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002029 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002030 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002031 break;
2032 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002033 if (Stream.SkipBlock()) {
2034 Error("Malformed block record");
2035 return Failure;
2036 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002037 break;
2038 }
2039 }
2040
2041 // Load the translation unit declaration
2042 ReadDeclRecord(DeclOffsets[0], 0);
2043
Douglas Gregorc713da92009-04-21 22:25:48 +00002044 // Initialization of builtins and library builtins occurs before the
2045 // PCH file is read, so there may be some identifiers that were
2046 // loaded into the IdentifierTable before we intercepted the
2047 // creation of identifiers. Iterate through the list of known
2048 // identifiers and determine whether we have to establish
2049 // preprocessor definitions or top-level identifier declaration
2050 // chains for those identifiers.
2051 //
2052 // We copy the IdentifierInfo pointers to a small vector first,
2053 // since de-serializing declarations or macro definitions can add
2054 // new entries into the identifier table, invalidating the
2055 // iterators.
2056 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2057 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2058 IdEnd = PP.getIdentifierTable().end();
2059 Id != IdEnd; ++Id)
2060 Identifiers.push_back(Id->second);
2061 PCHIdentifierLookupTable *IdTable
2062 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2063 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2064 IdentifierInfo *II = Identifiers[I];
2065 // Look in the on-disk hash table for an entry for
2066 PCHIdentifierLookupTrait Info(*this, II);
2067 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2068 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2069 if (Pos == IdTable->end())
2070 continue;
2071
2072 // Dereferencing the iterator has the effect of populating the
2073 // IdentifierInfo node with the various declarations it needs.
2074 (void)*Pos;
2075 }
2076
Douglas Gregore01ad442009-04-18 05:55:16 +00002077 // Load the special types.
2078 Context.setBuiltinVaListType(
2079 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002080 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2081 Context.setObjCIdType(GetType(Id));
2082 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2083 Context.setObjCSelType(GetType(Sel));
2084 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2085 Context.setObjCProtoType(GetType(Proto));
2086 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2087 Context.setObjCClassType(GetType(Class));
2088 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2089 Context.setCFConstantStringType(GetType(String));
2090 if (unsigned FastEnum
2091 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2092 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc713da92009-04-21 22:25:48 +00002093 // If we saw the preprocessor block, read it now.
2094 if (PreprocessorBlockOffset) {
2095 SavedStreamPosition SavedPos(Stream);
2096 Stream.JumpToBit(PreprocessorBlockOffset);
2097 if (ReadPreprocessorBlock()) {
2098 Error("Malformed preprocessor block");
2099 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002100 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002101 }
Steve Naroff9e84d782009-04-23 10:39:46 +00002102 if (SelectorBlockOffset) {
2103 SavedStreamPosition SavedPos(Stream);
2104 Stream.JumpToBit(SelectorBlockOffset);
2105 if (ReadSelectorBlock()) {
2106 Error("Malformed preprocessor block");
2107 return Failure;
2108 }
2109 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002110
Douglas Gregorc713da92009-04-21 22:25:48 +00002111 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002112}
2113
Douglas Gregor179cfb12009-04-10 20:39:37 +00002114/// \brief Parse the record that corresponds to a LangOptions data
2115/// structure.
2116///
2117/// This routine compares the language options used to generate the
2118/// PCH file against the language options set for the current
2119/// compilation. For each option, we classify differences between the
2120/// two compiler states as either "benign" or "important". Benign
2121/// differences don't matter, and we accept them without complaint
2122/// (and without modifying the language options). Differences between
2123/// the states for important options cause the PCH file to be
2124/// unusable, so we emit a warning and return true to indicate that
2125/// there was an error.
2126///
2127/// \returns true if the PCH file is unacceptable, false otherwise.
2128bool PCHReader::ParseLanguageOptions(
2129 const llvm::SmallVectorImpl<uint64_t> &Record) {
2130 const LangOptions &LangOpts = Context.getLangOptions();
2131#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2132#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2133 if (Record[Idx] != LangOpts.Option) { \
2134 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2135 Diag(diag::note_ignoring_pch) << FileName; \
2136 return true; \
2137 } \
2138 ++Idx
2139
2140 unsigned Idx = 0;
2141 PARSE_LANGOPT_BENIGN(Trigraphs);
2142 PARSE_LANGOPT_BENIGN(BCPLComment);
2143 PARSE_LANGOPT_BENIGN(DollarIdents);
2144 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2145 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2146 PARSE_LANGOPT_BENIGN(ImplicitInt);
2147 PARSE_LANGOPT_BENIGN(Digraphs);
2148 PARSE_LANGOPT_BENIGN(HexFloats);
2149 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2150 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2151 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2152 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2153 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2154 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2155 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2156 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2157 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2158 PARSE_LANGOPT_BENIGN(PascalStrings);
2159 PARSE_LANGOPT_BENIGN(Boolean);
2160 PARSE_LANGOPT_BENIGN(WritableStrings);
2161 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2162 diag::warn_pch_lax_vector_conversions);
2163 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2164 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2165 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2166 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2167 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2168 diag::warn_pch_thread_safe_statics);
2169 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2170 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2171 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2172 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2173 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2174 diag::warn_pch_heinous_extensions);
2175 // FIXME: Most of the options below are benign if the macro wasn't
2176 // used. Unfortunately, this means that a PCH compiled without
2177 // optimization can't be used with optimization turned on, even
2178 // though the only thing that changes is whether __OPTIMIZE__ was
2179 // defined... but if __OPTIMIZE__ never showed up in the header, it
2180 // doesn't matter. We could consider making this some special kind
2181 // of check.
2182 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2183 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2184 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2185 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2186 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2187 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2188 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2189 Diag(diag::warn_pch_gc_mode)
2190 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2191 Diag(diag::note_ignoring_pch) << FileName;
2192 return true;
2193 }
2194 ++Idx;
2195 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2196 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2197#undef PARSE_LANGOPT_IRRELEVANT
2198#undef PARSE_LANGOPT_BENIGN
2199
2200 return false;
2201}
2202
Douglas Gregorc34897d2009-04-09 22:27:44 +00002203/// \brief Read and return the type at the given offset.
2204///
2205/// This routine actually reads the record corresponding to the type
2206/// at the given offset in the bitstream. It is a helper routine for
2207/// GetType, which deals with reading type IDs.
2208QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002209 // Keep track of where we are in the stream, then jump back there
2210 // after reading this type.
2211 SavedStreamPosition SavedPosition(Stream);
2212
Douglas Gregorc34897d2009-04-09 22:27:44 +00002213 Stream.JumpToBit(Offset);
2214 RecordData Record;
2215 unsigned Code = Stream.ReadCode();
2216 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002217 case pch::TYPE_EXT_QUAL: {
2218 assert(Record.size() == 3 &&
2219 "Incorrect encoding of extended qualifier type");
2220 QualType Base = GetType(Record[0]);
2221 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2222 unsigned AddressSpace = Record[2];
2223
2224 QualType T = Base;
2225 if (GCAttr != QualType::GCNone)
2226 T = Context.getObjCGCQualType(T, GCAttr);
2227 if (AddressSpace)
2228 T = Context.getAddrSpaceQualType(T, AddressSpace);
2229 return T;
2230 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002231
Douglas Gregorc34897d2009-04-09 22:27:44 +00002232 case pch::TYPE_FIXED_WIDTH_INT: {
2233 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2234 return Context.getFixedWidthIntType(Record[0], Record[1]);
2235 }
2236
2237 case pch::TYPE_COMPLEX: {
2238 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2239 QualType ElemType = GetType(Record[0]);
2240 return Context.getComplexType(ElemType);
2241 }
2242
2243 case pch::TYPE_POINTER: {
2244 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2245 QualType PointeeType = GetType(Record[0]);
2246 return Context.getPointerType(PointeeType);
2247 }
2248
2249 case pch::TYPE_BLOCK_POINTER: {
2250 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2251 QualType PointeeType = GetType(Record[0]);
2252 return Context.getBlockPointerType(PointeeType);
2253 }
2254
2255 case pch::TYPE_LVALUE_REFERENCE: {
2256 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2257 QualType PointeeType = GetType(Record[0]);
2258 return Context.getLValueReferenceType(PointeeType);
2259 }
2260
2261 case pch::TYPE_RVALUE_REFERENCE: {
2262 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2263 QualType PointeeType = GetType(Record[0]);
2264 return Context.getRValueReferenceType(PointeeType);
2265 }
2266
2267 case pch::TYPE_MEMBER_POINTER: {
2268 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2269 QualType PointeeType = GetType(Record[0]);
2270 QualType ClassType = GetType(Record[1]);
2271 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2272 }
2273
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002274 case pch::TYPE_CONSTANT_ARRAY: {
2275 QualType ElementType = GetType(Record[0]);
2276 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2277 unsigned IndexTypeQuals = Record[2];
2278 unsigned Idx = 3;
2279 llvm::APInt Size = ReadAPInt(Record, Idx);
2280 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2281 }
2282
2283 case pch::TYPE_INCOMPLETE_ARRAY: {
2284 QualType ElementType = GetType(Record[0]);
2285 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2286 unsigned IndexTypeQuals = Record[2];
2287 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2288 }
2289
2290 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002291 QualType ElementType = GetType(Record[0]);
2292 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2293 unsigned IndexTypeQuals = Record[2];
2294 return Context.getVariableArrayType(ElementType, ReadExpr(),
2295 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002296 }
2297
2298 case pch::TYPE_VECTOR: {
2299 if (Record.size() != 2) {
2300 Error("Incorrect encoding of vector type in PCH file");
2301 return QualType();
2302 }
2303
2304 QualType ElementType = GetType(Record[0]);
2305 unsigned NumElements = Record[1];
2306 return Context.getVectorType(ElementType, NumElements);
2307 }
2308
2309 case pch::TYPE_EXT_VECTOR: {
2310 if (Record.size() != 2) {
2311 Error("Incorrect encoding of extended vector type in PCH file");
2312 return QualType();
2313 }
2314
2315 QualType ElementType = GetType(Record[0]);
2316 unsigned NumElements = Record[1];
2317 return Context.getExtVectorType(ElementType, NumElements);
2318 }
2319
2320 case pch::TYPE_FUNCTION_NO_PROTO: {
2321 if (Record.size() != 1) {
2322 Error("Incorrect encoding of no-proto function type");
2323 return QualType();
2324 }
2325 QualType ResultType = GetType(Record[0]);
2326 return Context.getFunctionNoProtoType(ResultType);
2327 }
2328
2329 case pch::TYPE_FUNCTION_PROTO: {
2330 QualType ResultType = GetType(Record[0]);
2331 unsigned Idx = 1;
2332 unsigned NumParams = Record[Idx++];
2333 llvm::SmallVector<QualType, 16> ParamTypes;
2334 for (unsigned I = 0; I != NumParams; ++I)
2335 ParamTypes.push_back(GetType(Record[Idx++]));
2336 bool isVariadic = Record[Idx++];
2337 unsigned Quals = Record[Idx++];
2338 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2339 isVariadic, Quals);
2340 }
2341
2342 case pch::TYPE_TYPEDEF:
2343 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2344 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2345
2346 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002347 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002348
2349 case pch::TYPE_TYPEOF: {
2350 if (Record.size() != 1) {
2351 Error("Incorrect encoding of typeof(type) in PCH file");
2352 return QualType();
2353 }
2354 QualType UnderlyingType = GetType(Record[0]);
2355 return Context.getTypeOfType(UnderlyingType);
2356 }
2357
2358 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002359 assert(Record.size() == 1 && "Incorrect encoding of record type");
2360 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002361
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002362 case pch::TYPE_ENUM:
2363 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2364 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2365
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002366 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002367 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2368 return Context.getObjCInterfaceType(
2369 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002370
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002371 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2372 unsigned Idx = 0;
2373 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2374 unsigned NumProtos = Record[Idx++];
2375 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2376 for (unsigned I = 0; I != NumProtos; ++I)
2377 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2378 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2379 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002380
Chris Lattner9b9f2352009-04-22 06:40:03 +00002381 case pch::TYPE_OBJC_QUALIFIED_ID: {
2382 unsigned Idx = 0;
2383 unsigned NumProtos = Record[Idx++];
2384 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2385 for (unsigned I = 0; I != NumProtos; ++I)
2386 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2387 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2388 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002389 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002390 // Suppress a GCC warning
2391 return QualType();
2392}
2393
2394/// \brief Note that we have loaded the declaration with the given
2395/// Index.
2396///
2397/// This routine notes that this declaration has already been loaded,
2398/// so that future GetDecl calls will return this declaration rather
2399/// than trying to load a new declaration.
2400inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2401 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2402 DeclAlreadyLoaded[Index] = true;
2403 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2404}
2405
2406/// \brief Read the declaration at the given offset from the PCH file.
2407Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002408 // Keep track of where we are in the stream, then jump back there
2409 // after reading this declaration.
2410 SavedStreamPosition SavedPosition(Stream);
2411
Douglas Gregorc34897d2009-04-09 22:27:44 +00002412 Decl *D = 0;
2413 Stream.JumpToBit(Offset);
2414 RecordData Record;
2415 unsigned Code = Stream.ReadCode();
2416 unsigned Idx = 0;
2417 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002418
Douglas Gregorc34897d2009-04-09 22:27:44 +00002419 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002420 case pch::DECL_ATTR:
2421 case pch::DECL_CONTEXT_LEXICAL:
2422 case pch::DECL_CONTEXT_VISIBLE:
2423 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2424 break;
2425
Douglas Gregorc34897d2009-04-09 22:27:44 +00002426 case pch::DECL_TRANSLATION_UNIT:
2427 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002428 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002429 break;
2430
2431 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002432 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002433 break;
2434 }
2435
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002436 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002437 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002438 break;
2439 }
2440
Douglas Gregor982365e2009-04-13 21:20:57 +00002441 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002442 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2443 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002444 break;
2445 }
2446
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002447 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002448 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2449 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002450 break;
2451 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002452
2453 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002454 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2455 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002456 break;
2457 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002458
Steve Naroff79ea0e02009-04-20 15:06:07 +00002459 case pch::DECL_OBJC_METHOD: {
2460 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2461 Selector(), QualType(), 0);
2462 break;
2463 }
2464
Steve Naroff97b53bd2009-04-21 15:12:33 +00002465 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002466 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2467 break;
2468 }
2469
Steve Naroff97b53bd2009-04-21 15:12:33 +00002470 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002471 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2472 ObjCIvarDecl::None);
2473 break;
2474 }
2475
Steve Naroff97b53bd2009-04-21 15:12:33 +00002476 case pch::DECL_OBJC_PROTOCOL: {
2477 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2478 break;
2479 }
2480
2481 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2482 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2483 QualType(), 0);
2484 break;
2485 }
2486
2487 case pch::DECL_OBJC_CLASS: {
2488 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2489 break;
2490 }
2491
2492 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2493 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2494 break;
2495 }
2496
2497 case pch::DECL_OBJC_CATEGORY: {
2498 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2499 break;
2500 }
2501
2502 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002503 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002504 break;
2505 }
2506
2507 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002508 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002509 break;
2510 }
2511
2512 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002513 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002514 break;
2515 }
2516
2517 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002518 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002519 break;
2520 }
2521
2522 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002523 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2524 SourceLocation(), 0,
2525 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002526 break;
2527 }
2528
Douglas Gregor982365e2009-04-13 21:20:57 +00002529 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002530 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2531 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002532 break;
2533 }
2534
Douglas Gregorc34897d2009-04-09 22:27:44 +00002535 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002536 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2537 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002538 break;
2539 }
2540
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002541 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002542 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2543 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002544 break;
2545 }
2546
2547 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002548 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002549 QualType(), QualType(), VarDecl::None,
2550 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002551 break;
2552 }
2553
Douglas Gregor2a491792009-04-13 22:49:25 +00002554 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002555 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002556 break;
2557 }
2558
2559 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002560 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002561 break;
2562 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002563 }
2564
Douglas Gregorc713da92009-04-21 22:25:48 +00002565 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002566 if (D) {
2567 LoadedDecl(Index, D);
2568 Reader.Visit(D);
2569 }
2570
Douglas Gregorc34897d2009-04-09 22:27:44 +00002571 // If this declaration is also a declaration context, get the
2572 // offsets for its tables of lexical and visible declarations.
2573 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2574 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2575 if (Offsets.first || Offsets.second) {
2576 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2577 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2578 DeclContextOffsets[DC] = Offsets;
2579 }
2580 }
2581 assert(Idx == Record.size());
2582
Douglas Gregor405b6432009-04-22 19:09:20 +00002583 if (Consumer) {
2584 // If we have deserialized a declaration that has a definition the
2585 // AST consumer might need to know about, notify the consumer
2586 // about that definition now.
2587 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2588 if (Var->isFileVarDecl() && Var->getInit()) {
2589 DeclGroupRef DG(Var);
2590 Consumer->HandleTopLevelDecl(DG);
2591 }
2592 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2593 if (Func->isThisDeclarationADefinition()) {
2594 DeclGroupRef DG(Func);
2595 Consumer->HandleTopLevelDecl(DG);
2596 }
2597 }
2598 }
2599
Douglas Gregorc34897d2009-04-09 22:27:44 +00002600 return D;
2601}
2602
Douglas Gregorac8f2802009-04-10 17:25:41 +00002603QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002604 unsigned Quals = ID & 0x07;
2605 unsigned Index = ID >> 3;
2606
2607 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2608 QualType T;
2609 switch ((pch::PredefinedTypeIDs)Index) {
2610 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2611 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2612 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2613
2614 case pch::PREDEF_TYPE_CHAR_U_ID:
2615 case pch::PREDEF_TYPE_CHAR_S_ID:
2616 // FIXME: Check that the signedness of CharTy is correct!
2617 T = Context.CharTy;
2618 break;
2619
2620 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2621 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2622 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2623 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2624 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2625 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2626 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2627 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2628 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2629 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2630 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2631 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2632 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2633 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2634 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2635 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2636 }
2637
2638 assert(!T.isNull() && "Unknown predefined type");
2639 return T.getQualifiedType(Quals);
2640 }
2641
2642 Index -= pch::NUM_PREDEF_TYPE_IDS;
2643 if (!TypeAlreadyLoaded[Index]) {
2644 // Load the type from the PCH file.
2645 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2646 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2647 TypeAlreadyLoaded[Index] = true;
2648 }
2649
2650 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2651}
2652
Douglas Gregorac8f2802009-04-10 17:25:41 +00002653Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002654 if (ID == 0)
2655 return 0;
2656
2657 unsigned Index = ID - 1;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002658 assert(Index < DeclAlreadyLoaded.size() && "Declaration ID out of range");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002659 if (DeclAlreadyLoaded[Index])
2660 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2661
2662 // Load the declaration from the PCH file.
2663 return ReadDeclRecord(DeclOffsets[Index], Index);
2664}
2665
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002666Stmt *PCHReader::GetStmt(uint64_t Offset) {
2667 // Keep track of where we are in the stream, then jump back there
2668 // after reading this declaration.
2669 SavedStreamPosition SavedPosition(Stream);
2670
2671 Stream.JumpToBit(Offset);
2672 return ReadStmt();
2673}
2674
Douglas Gregorc34897d2009-04-09 22:27:44 +00002675bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002676 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002677 assert(DC->hasExternalLexicalStorage() &&
2678 "DeclContext has no lexical decls in storage");
2679 uint64_t Offset = DeclContextOffsets[DC].first;
2680 assert(Offset && "DeclContext has no lexical decls in storage");
2681
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002682 // Keep track of where we are in the stream, then jump back there
2683 // after reading this context.
2684 SavedStreamPosition SavedPosition(Stream);
2685
Douglas Gregorc34897d2009-04-09 22:27:44 +00002686 // Load the record containing all of the declarations lexically in
2687 // this context.
2688 Stream.JumpToBit(Offset);
2689 RecordData Record;
2690 unsigned Code = Stream.ReadCode();
2691 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002692 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002693 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2694
2695 // Load all of the declaration IDs
2696 Decls.clear();
2697 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002698 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002699 return false;
2700}
2701
2702bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2703 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2704 assert(DC->hasExternalVisibleStorage() &&
2705 "DeclContext has no visible decls in storage");
2706 uint64_t Offset = DeclContextOffsets[DC].second;
2707 assert(Offset && "DeclContext has no visible decls in storage");
2708
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002709 // Keep track of where we are in the stream, then jump back there
2710 // after reading this context.
2711 SavedStreamPosition SavedPosition(Stream);
2712
Douglas Gregorc34897d2009-04-09 22:27:44 +00002713 // Load the record containing all of the declarations visible in
2714 // this context.
2715 Stream.JumpToBit(Offset);
2716 RecordData Record;
2717 unsigned Code = Stream.ReadCode();
2718 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002719 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002720 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2721 if (Record.size() == 0)
2722 return false;
2723
2724 Decls.clear();
2725
2726 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002727 while (Idx < Record.size()) {
2728 Decls.push_back(VisibleDeclaration());
2729 Decls.back().Name = ReadDeclarationName(Record, Idx);
2730
Douglas Gregorc34897d2009-04-09 22:27:44 +00002731 unsigned Size = Record[Idx++];
2732 llvm::SmallVector<unsigned, 4> & LoadedDecls
2733 = Decls.back().Declarations;
2734 LoadedDecls.reserve(Size);
2735 for (unsigned I = 0; I < Size; ++I)
2736 LoadedDecls.push_back(Record[Idx++]);
2737 }
2738
Douglas Gregoraf136d92009-04-22 22:34:57 +00002739 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002740 return false;
2741}
2742
Douglas Gregor631f6c62009-04-14 00:24:19 +00002743void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002744 this->Consumer = Consumer;
2745
Douglas Gregor631f6c62009-04-14 00:24:19 +00002746 if (!Consumer)
2747 return;
2748
2749 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2750 Decl *D = GetDecl(ExternalDefinitions[I]);
2751 DeclGroupRef DG(D);
2752 Consumer->HandleTopLevelDecl(DG);
2753 }
2754}
2755
Douglas Gregorc34897d2009-04-09 22:27:44 +00002756void PCHReader::PrintStats() {
2757 std::fprintf(stderr, "*** PCH Statistics:\n");
2758
2759 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2760 TypeAlreadyLoaded.end(),
2761 true);
2762 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2763 DeclAlreadyLoaded.end(),
2764 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002765 unsigned NumIdentifiersLoaded = 0;
2766 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2767 if ((IdentifierData[I] & 0x01) == 0)
2768 ++NumIdentifiersLoaded;
2769 }
2770
Douglas Gregorc34897d2009-04-09 22:27:44 +00002771 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2772 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002773 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002774 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2775 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002776 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2777 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2778 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2779 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002780 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2781 NumStatementsRead, TotalNumStatements,
2782 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002783 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2784 NumMacrosRead, TotalNumMacros,
2785 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002786 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2787 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2788 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2789 * 100));
2790 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2791 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2792 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2793 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002794 std::fprintf(stderr, "\n");
2795}
2796
Douglas Gregorc713da92009-04-21 22:25:48 +00002797void PCHReader::InitializeSema(Sema &S) {
2798 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002799 S.ExternalSource = this;
2800
Douglas Gregor2554cf22009-04-22 21:15:06 +00002801 // Makes sure any declarations that were deserialized "too early"
2802 // still get added to the identifier's declaration chains.
2803 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2804 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2805 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002806 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002807 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002808
2809 // If there were any tentative definitions, deserialize them and add
2810 // them to Sema's table of tentative definitions.
2811 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2812 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2813 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2814 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002815
2816 // If there were any locally-scoped external declarations,
2817 // deserialize them and add them to Sema's table of locally-scoped
2818 // external declarations.
2819 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2820 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2821 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2822 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002823}
2824
2825IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2826 // Try to find this name within our on-disk hash table
2827 PCHIdentifierLookupTable *IdTable
2828 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2829 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2830 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2831 if (Pos == IdTable->end())
2832 return 0;
2833
2834 // Dereferencing the iterator has the effect of building the
2835 // IdentifierInfo node and populating it with the various
2836 // declarations it needs.
2837 return *Pos;
2838}
2839
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002840std::pair<ObjCMethodList, ObjCMethodList>
2841PCHReader::ReadMethodPool(Selector Sel) {
2842 if (!MethodPoolLookupTable)
2843 return std::pair<ObjCMethodList, ObjCMethodList>();
2844
2845 // Try to find this selector within our on-disk hash table.
2846 PCHMethodPoolLookupTable *PoolTable
2847 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2848 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
2849 if (Pos == PoolTable->end())
2850 return std::pair<ObjCMethodList, ObjCMethodList>();;
2851
2852 return *Pos;
2853}
2854
Douglas Gregorc713da92009-04-21 22:25:48 +00002855void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2856 assert(ID && "Non-zero identifier ID required");
2857 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2858}
2859
Chris Lattner29241862009-04-11 21:15:38 +00002860IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002861 if (ID == 0)
2862 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002863
Douglas Gregorc713da92009-04-21 22:25:48 +00002864 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002865 Error("No identifier table in PCH file");
2866 return 0;
2867 }
Chris Lattner29241862009-04-11 21:15:38 +00002868
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002869 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002870 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002871 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002872 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002873 }
Chris Lattner29241862009-04-11 21:15:38 +00002874
2875 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002876}
2877
Steve Naroff9e84d782009-04-23 10:39:46 +00002878Selector PCHReader::DecodeSelector(unsigned ID) {
2879 if (ID == 0)
2880 return Selector();
2881
2882 if (SelectorData.empty()) {
2883 Error("No selector table in PCH file");
2884 return Selector();
2885 }
2886
2887 if (ID > SelectorData.size()) {
2888 Error("Selector ID out of range");
2889 return Selector();
2890 }
2891 return SelectorData[ID-1];
2892}
2893
Douglas Gregorc34897d2009-04-09 22:27:44 +00002894DeclarationName
2895PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2896 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2897 switch (Kind) {
2898 case DeclarationName::Identifier:
2899 return DeclarationName(GetIdentifierInfo(Record, Idx));
2900
2901 case DeclarationName::ObjCZeroArgSelector:
2902 case DeclarationName::ObjCOneArgSelector:
2903 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002904 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002905
2906 case DeclarationName::CXXConstructorName:
2907 return Context.DeclarationNames.getCXXConstructorName(
2908 GetType(Record[Idx++]));
2909
2910 case DeclarationName::CXXDestructorName:
2911 return Context.DeclarationNames.getCXXDestructorName(
2912 GetType(Record[Idx++]));
2913
2914 case DeclarationName::CXXConversionFunctionName:
2915 return Context.DeclarationNames.getCXXConversionFunctionName(
2916 GetType(Record[Idx++]));
2917
2918 case DeclarationName::CXXOperatorName:
2919 return Context.DeclarationNames.getCXXOperatorName(
2920 (OverloadedOperatorKind)Record[Idx++]);
2921
2922 case DeclarationName::CXXUsingDirective:
2923 return DeclarationName::getUsingDirectiveName();
2924 }
2925
2926 // Required to silence GCC warning
2927 return DeclarationName();
2928}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002929
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002930/// \brief Read an integral value
2931llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2932 unsigned BitWidth = Record[Idx++];
2933 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2934 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2935 Idx += NumWords;
2936 return Result;
2937}
2938
2939/// \brief Read a signed integral value
2940llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2941 bool isUnsigned = Record[Idx++];
2942 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2943}
2944
Douglas Gregore2f37202009-04-14 21:55:33 +00002945/// \brief Read a floating-point value
2946llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002947 return llvm::APFloat(ReadAPInt(Record, Idx));
2948}
2949
Douglas Gregor1c507882009-04-15 21:30:51 +00002950// \brief Read a string
2951std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2952 unsigned Len = Record[Idx++];
2953 std::string Result(&Record[Idx], &Record[Idx] + Len);
2954 Idx += Len;
2955 return Result;
2956}
2957
2958/// \brief Reads attributes from the current stream position.
2959Attr *PCHReader::ReadAttributes() {
2960 unsigned Code = Stream.ReadCode();
2961 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2962 "Expected unabbreviated record"); (void)Code;
2963
2964 RecordData Record;
2965 unsigned Idx = 0;
2966 unsigned RecCode = Stream.ReadRecord(Code, Record);
2967 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2968 (void)RecCode;
2969
2970#define SIMPLE_ATTR(Name) \
2971 case Attr::Name: \
2972 New = ::new (Context) Name##Attr(); \
2973 break
2974
2975#define STRING_ATTR(Name) \
2976 case Attr::Name: \
2977 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2978 break
2979
2980#define UNSIGNED_ATTR(Name) \
2981 case Attr::Name: \
2982 New = ::new (Context) Name##Attr(Record[Idx++]); \
2983 break
2984
2985 Attr *Attrs = 0;
2986 while (Idx < Record.size()) {
2987 Attr *New = 0;
2988 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2989 bool IsInherited = Record[Idx++];
2990
2991 switch (Kind) {
2992 STRING_ATTR(Alias);
2993 UNSIGNED_ATTR(Aligned);
2994 SIMPLE_ATTR(AlwaysInline);
2995 SIMPLE_ATTR(AnalyzerNoReturn);
2996 STRING_ATTR(Annotate);
2997 STRING_ATTR(AsmLabel);
2998
2999 case Attr::Blocks:
3000 New = ::new (Context) BlocksAttr(
3001 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3002 break;
3003
3004 case Attr::Cleanup:
3005 New = ::new (Context) CleanupAttr(
3006 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3007 break;
3008
3009 SIMPLE_ATTR(Const);
3010 UNSIGNED_ATTR(Constructor);
3011 SIMPLE_ATTR(DLLExport);
3012 SIMPLE_ATTR(DLLImport);
3013 SIMPLE_ATTR(Deprecated);
3014 UNSIGNED_ATTR(Destructor);
3015 SIMPLE_ATTR(FastCall);
3016
3017 case Attr::Format: {
3018 std::string Type = ReadString(Record, Idx);
3019 unsigned FormatIdx = Record[Idx++];
3020 unsigned FirstArg = Record[Idx++];
3021 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3022 break;
3023 }
3024
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003025 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003026
3027 case Attr::IBOutletKind:
3028 New = ::new (Context) IBOutletAttr();
3029 break;
3030
3031 SIMPLE_ATTR(NoReturn);
3032 SIMPLE_ATTR(NoThrow);
3033 SIMPLE_ATTR(Nodebug);
3034 SIMPLE_ATTR(Noinline);
3035
3036 case Attr::NonNull: {
3037 unsigned Size = Record[Idx++];
3038 llvm::SmallVector<unsigned, 16> ArgNums;
3039 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3040 Idx += Size;
3041 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3042 break;
3043 }
3044
3045 SIMPLE_ATTR(ObjCException);
3046 SIMPLE_ATTR(ObjCNSObject);
3047 SIMPLE_ATTR(Overloadable);
3048 UNSIGNED_ATTR(Packed);
3049 SIMPLE_ATTR(Pure);
3050 UNSIGNED_ATTR(Regparm);
3051 STRING_ATTR(Section);
3052 SIMPLE_ATTR(StdCall);
3053 SIMPLE_ATTR(TransparentUnion);
3054 SIMPLE_ATTR(Unavailable);
3055 SIMPLE_ATTR(Unused);
3056 SIMPLE_ATTR(Used);
3057
3058 case Attr::Visibility:
3059 New = ::new (Context) VisibilityAttr(
3060 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3061 break;
3062
3063 SIMPLE_ATTR(WarnUnusedResult);
3064 SIMPLE_ATTR(Weak);
3065 SIMPLE_ATTR(WeakImport);
3066 }
3067
3068 assert(New && "Unable to decode attribute?");
3069 New->setInherited(IsInherited);
3070 New->setNext(Attrs);
3071 Attrs = New;
3072 }
3073#undef UNSIGNED_ATTR
3074#undef STRING_ATTR
3075#undef SIMPLE_ATTR
3076
3077 // The list of attributes was built backwards. Reverse the list
3078 // before returning it.
3079 Attr *PrevAttr = 0, *NextAttr = 0;
3080 while (Attrs) {
3081 NextAttr = Attrs->getNext();
3082 Attrs->setNext(PrevAttr);
3083 PrevAttr = Attrs;
3084 Attrs = NextAttr;
3085 }
3086
3087 return PrevAttr;
3088}
3089
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003090Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003091 // Within the bitstream, expressions are stored in Reverse Polish
3092 // Notation, with each of the subexpressions preceding the
3093 // expression they are stored in. To evaluate expressions, we
3094 // continue reading expressions and placing them on the stack, with
3095 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003096 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003097 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003098 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003099 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003100 llvm::SmallVector<Stmt *, 16> StmtStack;
3101 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003102 Stmt::EmptyShell Empty;
3103
Douglas Gregora151ba42009-04-14 23:32:43 +00003104 while (true) {
3105 unsigned Code = Stream.ReadCode();
3106 if (Code == llvm::bitc::END_BLOCK) {
3107 if (Stream.ReadBlockEnd()) {
3108 Error("Error at end of Source Manager block");
3109 return 0;
3110 }
3111 break;
3112 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003113
Douglas Gregora151ba42009-04-14 23:32:43 +00003114 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3115 // No known subblocks, always skip them.
3116 Stream.ReadSubBlockID();
3117 if (Stream.SkipBlock()) {
3118 Error("Malformed block record");
3119 return 0;
3120 }
3121 continue;
3122 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003123
Douglas Gregora151ba42009-04-14 23:32:43 +00003124 if (Code == llvm::bitc::DEFINE_ABBREV) {
3125 Stream.ReadAbbrevRecord();
3126 continue;
3127 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003128
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003129 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003130 Idx = 0;
3131 Record.clear();
3132 bool Finished = false;
3133 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003134 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003135 Finished = true;
3136 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003137
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003138 case pch::STMT_NULL_PTR:
3139 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003140 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003141
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003142 case pch::STMT_NULL:
3143 S = new (Context) NullStmt(Empty);
3144 break;
3145
3146 case pch::STMT_COMPOUND:
3147 S = new (Context) CompoundStmt(Empty);
3148 break;
3149
3150 case pch::STMT_CASE:
3151 S = new (Context) CaseStmt(Empty);
3152 break;
3153
3154 case pch::STMT_DEFAULT:
3155 S = new (Context) DefaultStmt(Empty);
3156 break;
3157
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003158 case pch::STMT_LABEL:
3159 S = new (Context) LabelStmt(Empty);
3160 break;
3161
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003162 case pch::STMT_IF:
3163 S = new (Context) IfStmt(Empty);
3164 break;
3165
3166 case pch::STMT_SWITCH:
3167 S = new (Context) SwitchStmt(Empty);
3168 break;
3169
Douglas Gregora6b503f2009-04-17 00:16:09 +00003170 case pch::STMT_WHILE:
3171 S = new (Context) WhileStmt(Empty);
3172 break;
3173
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003174 case pch::STMT_DO:
3175 S = new (Context) DoStmt(Empty);
3176 break;
3177
3178 case pch::STMT_FOR:
3179 S = new (Context) ForStmt(Empty);
3180 break;
3181
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003182 case pch::STMT_GOTO:
3183 S = new (Context) GotoStmt(Empty);
3184 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003185
3186 case pch::STMT_INDIRECT_GOTO:
3187 S = new (Context) IndirectGotoStmt(Empty);
3188 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003189
Douglas Gregora6b503f2009-04-17 00:16:09 +00003190 case pch::STMT_CONTINUE:
3191 S = new (Context) ContinueStmt(Empty);
3192 break;
3193
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003194 case pch::STMT_BREAK:
3195 S = new (Context) BreakStmt(Empty);
3196 break;
3197
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003198 case pch::STMT_RETURN:
3199 S = new (Context) ReturnStmt(Empty);
3200 break;
3201
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003202 case pch::STMT_DECL:
3203 S = new (Context) DeclStmt(Empty);
3204 break;
3205
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003206 case pch::STMT_ASM:
3207 S = new (Context) AsmStmt(Empty);
3208 break;
3209
Douglas Gregora151ba42009-04-14 23:32:43 +00003210 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003211 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003212 break;
3213
3214 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003215 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003216 break;
3217
3218 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003219 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003220 break;
3221
3222 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003223 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003224 break;
3225
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003226 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003227 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003228 break;
3229
Douglas Gregor596e0932009-04-15 16:35:07 +00003230 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003231 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003232 Record[PCHStmtReader::NumExprFields + 1]);
3233 break;
3234
Douglas Gregora151ba42009-04-14 23:32:43 +00003235 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003236 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003237 break;
3238
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003239 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003240 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003241 break;
3242
Douglas Gregor12d74052009-04-15 15:58:59 +00003243 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003244 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003245 break;
3246
3247 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003248 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003249 break;
3250
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003251 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003252 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003253 break;
3254
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003255 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003256 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003257 break;
3258
3259 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003260 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003261 break;
3262
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003263 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003264 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003265 break;
3266
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003267 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003268 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003269 break;
3270
3271 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003272 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003273 break;
3274
Douglas Gregora151ba42009-04-14 23:32:43 +00003275 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003276 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003277 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003278
3279 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003280 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003281 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003282
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003283 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003284 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003285 break;
3286
Douglas Gregorec0b8292009-04-15 23:02:49 +00003287 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003288 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003289 break;
3290
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003291 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003292 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003293 break;
3294
3295 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003296 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003297 Record[PCHStmtReader::NumExprFields] - 1);
3298
3299 break;
3300
3301 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003302 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003303 break;
3304
Douglas Gregorec0b8292009-04-15 23:02:49 +00003305 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003306 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003307 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003308
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003309 case pch::EXPR_ADDR_LABEL:
3310 S = new (Context) AddrLabelExpr(Empty);
3311 break;
3312
Douglas Gregoreca12f62009-04-17 19:05:30 +00003313 case pch::EXPR_STMT:
3314 S = new (Context) StmtExpr(Empty);
3315 break;
3316
Douglas Gregor209d4622009-04-15 23:33:31 +00003317 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003318 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003319 break;
3320
3321 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003322 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003323 break;
3324
3325 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003326 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003327 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003328
3329 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003330 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003331 break;
3332
Douglas Gregore246b742009-04-17 19:21:43 +00003333 case pch::EXPR_BLOCK:
3334 S = new (Context) BlockExpr(Empty);
3335 break;
3336
Douglas Gregor725e94b2009-04-16 00:01:45 +00003337 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003338 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003339 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003340
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003341 case pch::EXPR_OBJC_STRING_LITERAL:
3342 S = new (Context) ObjCStringLiteral(Empty);
3343 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003344 case pch::EXPR_OBJC_ENCODE:
3345 S = new (Context) ObjCEncodeExpr(Empty);
3346 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003347 case pch::EXPR_OBJC_SELECTOR_EXPR:
3348 S = new (Context) ObjCSelectorExpr(Empty);
3349 break;
3350 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3351 S = new (Context) ObjCProtocolExpr(Empty);
3352 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003353 }
3354
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003355 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003356 if (Finished)
3357 break;
3358
Douglas Gregor456e0952009-04-17 22:13:46 +00003359 ++NumStatementsRead;
3360
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003361 if (S) {
3362 unsigned NumSubStmts = Reader.Visit(S);
3363 while (NumSubStmts > 0) {
3364 StmtStack.pop_back();
3365 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003366 }
3367 }
3368
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003369 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003370 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003371 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003372 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003373 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003374 return StmtStack.back();
3375}
3376
3377Expr *PCHReader::ReadExpr() {
3378 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003379}
3380
Douglas Gregor179cfb12009-04-10 20:39:37 +00003381DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003382 return Diag(SourceLocation(), DiagID);
3383}
3384
3385DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3386 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003387 Context.getSourceManager()),
3388 DiagID);
3389}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003390
Douglas Gregorc713da92009-04-21 22:25:48 +00003391/// \brief Retrieve the identifier table associated with the
3392/// preprocessor.
3393IdentifierTable &PCHReader::getIdentifierTable() {
3394 return PP.getIdentifierTable();
3395}
3396
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003397/// \brief Record that the given ID maps to the given switch-case
3398/// statement.
3399void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3400 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3401 SwitchCaseStmts[ID] = SC;
3402}
3403
3404/// \brief Retrieve the switch-case statement with the given ID.
3405SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3406 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3407 return SwitchCaseStmts[ID];
3408}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003409
3410/// \brief Record that the given label statement has been
3411/// deserialized and has the given ID.
3412void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3413 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3414 "Deserialized label twice");
3415 LabelStmts[ID] = S;
3416
3417 // If we've already seen any goto statements that point to this
3418 // label, resolve them now.
3419 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3420 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3421 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3422 Goto->second->setLabel(S);
3423 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003424
3425 // If we've already seen any address-label statements that point to
3426 // this label, resolve them now.
3427 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3428 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3429 = UnresolvedAddrLabelExprs.equal_range(ID);
3430 for (AddrLabelIter AddrLabel = AddrLabels.first;
3431 AddrLabel != AddrLabels.second; ++AddrLabel)
3432 AddrLabel->second->setLabel(S);
3433 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003434}
3435
3436/// \brief Set the label of the given statement to the label
3437/// identified by ID.
3438///
3439/// Depending on the order in which the label and other statements
3440/// referencing that label occur, this operation may complete
3441/// immediately (updating the statement) or it may queue the
3442/// statement to be back-patched later.
3443void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3444 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3445 if (Label != LabelStmts.end()) {
3446 // We've already seen this label, so set the label of the goto and
3447 // we're done.
3448 S->setLabel(Label->second);
3449 } else {
3450 // We haven't seen this label yet, so add this goto to the set of
3451 // unresolved goto statements.
3452 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3453 }
3454}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003455
3456/// \brief Set the label of the given expression to the label
3457/// identified by ID.
3458///
3459/// Depending on the order in which the label and other statements
3460/// referencing that label occur, this operation may complete
3461/// immediately (updating the statement) or it may queue the
3462/// statement to be back-patched later.
3463void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3464 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3465 if (Label != LabelStmts.end()) {
3466 // We've already seen this label, so set the label of the
3467 // label-address expression and we're done.
3468 S->setLabel(Label->second);
3469 } else {
3470 // We haven't seen this label yet, so add this label-address
3471 // expression to the set of unresolved label-address expressions.
3472 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3473 }
3474}