blob: d225c68392f29463897e01ac2103ee56267a5bc3 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregorfdd01722009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
Douglas Gregor37e26842009-04-21 23:56:24 +000039namespace {
40 /// \brief Helper class that saves the current stream position and
41 /// then restores it when destroyed.
42 struct VISIBILITY_HIDDEN SavedStreamPosition {
43 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
44 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
45
46 ~SavedStreamPosition() {
47 Stream.JumpToBit(Offset);
48 }
49
50 private:
51 llvm::BitstreamReader &Stream;
52 uint64_t Offset;
53 };
54}
55
Douglas Gregor2cf26342009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-04-09 22:27:44 +000062 PCHReader &Reader;
63 const PCHReader::RecordData &Record;
64 unsigned &Idx;
65
66 public:
67 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
68 unsigned &Idx)
69 : Reader(Reader), Record(Record), Idx(Idx) { }
70
71 void VisitDecl(Decl *D);
72 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
73 void VisitNamedDecl(NamedDecl *ND);
74 void VisitTypeDecl(TypeDecl *TD);
75 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff53c9d8a2009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-04-21 15:12:33 +000093 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
94 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
95 void VisitObjCClassDecl(ObjCClassDecl *D);
96 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
97 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
98 void VisitObjCImplDecl(ObjCImplDecl *D);
99 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
100 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
101 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
102 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
103 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 };
105}
106
107void PCHDeclReader::VisitDecl(Decl *D) {
108 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
109 D->setLexicalDeclContext(
110 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
112 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115 D->setImplicit(Record[Idx++]);
116 D->setAccess((AccessSpecifier)Record[Idx++]);
117}
118
119void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
120 VisitDecl(TU);
121}
122
123void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
124 VisitDecl(ND);
125 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
126}
127
128void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
129 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +0000134 // Note that we cannot use VisitTypeDecl here, because we need to
135 // set the underlying type of the typedef *before* we try to read
136 // the type associated with the TypedefDecl.
137 VisitNamedDecl(TD);
138 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
139 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
140 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000141}
142
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000143void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
144 VisitTypeDecl(TD);
145 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
146 TD->setDefinition(Record[Idx++]);
147 TD->setTypedefForAnonDecl(
148 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
149}
150
151void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
152 VisitTagDecl(ED);
153 ED->setIntegerType(Reader.GetType(Record[Idx++]));
154}
155
Douglas Gregor8c700062009-04-13 21:20:57 +0000156void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
157 VisitTagDecl(RD);
158 RD->setHasFlexibleArrayMember(Record[Idx++]);
159 RD->setAnonymousStructOrUnion(Record[Idx++]);
160}
161
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000178 FD->setPreviousDeclaration(
179 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
180 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
181 FD->setInline(Record[Idx++]);
Douglas Gregorb3efa982009-04-23 18:22:55 +0000182 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000183 FD->setVirtual(Record[Idx++]);
184 FD->setPure(Record[Idx++]);
185 FD->setInheritedPrototype(Record[Idx++]);
186 FD->setHasPrototype(Record[Idx++]);
187 FD->setDeleted(Record[Idx++]);
188 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
189 unsigned NumParams = Record[Idx++];
190 llvm::SmallVector<ParmVarDecl *, 16> Params;
191 Params.reserve(NumParams);
192 for (unsigned I = 0; I != NumParams; ++I)
193 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
194 FD->setParams(Reader.getContext(), &Params[0], NumParams);
195}
196
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000197void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
198 VisitNamedDecl(MD);
199 if (Record[Idx++]) {
200 // In practice, this won't be executed (since method definitions
201 // don't occur in header files).
202 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
203 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
205 }
206 MD->setInstanceMethod(Record[Idx++]);
207 MD->setVariadic(Record[Idx++]);
208 MD->setSynthesized(Record[Idx++]);
209 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
210 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
211 MD->setResultType(Reader.GetType(Record[Idx++]));
212 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
213 unsigned NumParams = Record[Idx++];
214 llvm::SmallVector<ParmVarDecl *, 16> Params;
215 Params.reserve(NumParams);
216 for (unsigned I = 0; I != NumParams; ++I)
217 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
218 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
219}
220
Steve Naroff33feeb02009-04-20 20:09:33 +0000221void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
222 VisitNamedDecl(CD);
223 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
224}
225
226void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
227 VisitObjCContainerDecl(ID);
228 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000229 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
230 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor291be392009-04-23 03:59:07 +0000231 unsigned NumProtocols = Record[Idx++];
232 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
233 Protocols.reserve(NumProtocols);
234 for (unsigned I = 0; I != NumProtocols; ++I)
235 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff33feeb02009-04-20 20:09:33 +0000236 unsigned NumIvars = Record[Idx++];
237 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
238 IVars.reserve(NumIvars);
239 for (unsigned I = 0; I != NumIvars; ++I)
240 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
241 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
242
243 ID->setForwardDecl(Record[Idx++]);
244 ID->setImplicitInterfaceDecl(Record[Idx++]);
245 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
246 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000247 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor291be392009-04-23 03:59:07 +0000248 // FIXME: add categories.
Steve Naroff33feeb02009-04-20 20:09:33 +0000249}
250
251void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
252 VisitFieldDecl(IVD);
253 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
254}
255
Steve Naroff30833f82009-04-21 15:12:33 +0000256void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
257 VisitObjCContainerDecl(PD);
258 PD->setForwardDecl(Record[Idx++]);
259 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
260 unsigned NumProtoRefs = Record[Idx++];
261 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
262 ProtoRefs.reserve(NumProtoRefs);
263 for (unsigned I = 0; I != NumProtoRefs; ++I)
264 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
265 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
266}
267
268void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
269 VisitFieldDecl(FD);
270}
271
272void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
273 VisitDecl(CD);
274 unsigned NumClassRefs = Record[Idx++];
275 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
276 ClassRefs.reserve(NumClassRefs);
277 for (unsigned I = 0; I != NumClassRefs; ++I)
278 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
279 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
280}
281
282void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
283 VisitDecl(FPD);
284 unsigned NumProtoRefs = Record[Idx++];
285 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
286 ProtoRefs.reserve(NumProtoRefs);
287 for (unsigned I = 0; I != NumProtoRefs; ++I)
288 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
289 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
290}
291
292void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
293 VisitObjCContainerDecl(CD);
294 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
295 unsigned NumProtoRefs = Record[Idx++];
296 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
297 ProtoRefs.reserve(NumProtoRefs);
298 for (unsigned I = 0; I != NumProtoRefs; ++I)
299 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
300 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
301 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
302 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
303}
304
305void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
306 VisitNamedDecl(CAD);
307 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
308}
309
310void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
311 VisitNamedDecl(D);
Douglas Gregor70e5a142009-04-22 23:20:34 +0000312 D->setType(Reader.GetType(Record[Idx++]));
313 // FIXME: stable encoding
314 D->setPropertyAttributes(
315 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
316 // FIXME: stable encoding
317 D->setPropertyImplementation(
318 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
319 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
320 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
321 D->setGetterMethodDecl(
322 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
323 D->setSetterMethodDecl(
324 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
325 D->setPropertyIvarDecl(
326 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000327}
328
329void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
330 VisitDecl(D);
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000331 D->setClassInterface(
332 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
333 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff30833f82009-04-21 15:12:33 +0000334}
335
336void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
337 VisitObjCImplDecl(D);
Douglas Gregor10b0e1f2009-04-23 02:53:57 +0000338 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff30833f82009-04-21 15:12:33 +0000339}
340
341void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
342 VisitObjCImplDecl(D);
Douglas Gregor8f36aba2009-04-23 03:23:08 +0000343 D->setSuperClass(
344 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000345}
346
347
348void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
349 VisitDecl(D);
Douglas Gregor8818c4f2009-04-23 03:43:53 +0000350 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
351 D->setPropertyDecl(
352 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
353 D->setPropertyIvarDecl(
354 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000355}
356
Douglas Gregor8c700062009-04-13 21:20:57 +0000357void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
358 VisitValueDecl(FD);
359 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000360 if (Record[Idx++])
361 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000362}
363
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
365 VisitValueDecl(VD);
366 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
367 VD->setThreadSpecified(Record[Idx++]);
368 VD->setCXXDirectInitializer(Record[Idx++]);
369 VD->setDeclaredInCondition(Record[Idx++]);
370 VD->setPreviousDeclaration(
371 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
372 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000373 if (Record[Idx++])
374 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000375}
376
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000377void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
378 VisitVarDecl(PD);
379 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000380 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000381}
382
383void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
384 VisitParmVarDecl(PD);
385 PD->setOriginalType(Reader.GetType(Record[Idx++]));
386}
387
Douglas Gregor1028bc62009-04-13 22:49:25 +0000388void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
389 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000390 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000391}
392
393void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
394 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000395 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000396 unsigned NumParams = Record[Idx++];
397 llvm::SmallVector<ParmVarDecl *, 16> Params;
398 Params.reserve(NumParams);
399 for (unsigned I = 0; I != NumParams; ++I)
400 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
401 BD->setParams(Reader.getContext(), &Params[0], NumParams);
402}
403
Douglas Gregor2cf26342009-04-09 22:27:44 +0000404std::pair<uint64_t, uint64_t>
405PCHDeclReader::VisitDeclContext(DeclContext *DC) {
406 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000407 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000408 return std::make_pair(LexicalOffset, VisibleOffset);
409}
410
Douglas Gregor0b748912009-04-14 21:18:50 +0000411//===----------------------------------------------------------------------===//
412// Statement/expression deserialization
413//===----------------------------------------------------------------------===//
414namespace {
415 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000416 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000417 PCHReader &Reader;
418 const PCHReader::RecordData &Record;
419 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000420 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000421
422 public:
423 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000424 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
425 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000426
Douglas Gregor025452f2009-04-17 00:04:06 +0000427 /// \brief The number of record fields required for the Stmt class
428 /// itself.
429 static const unsigned NumStmtFields = 0;
430
Douglas Gregor673ecd62009-04-15 16:35:07 +0000431 /// \brief The number of record fields required for the Expr class
432 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000433 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000434
Douglas Gregor087fd532009-04-14 23:32:43 +0000435 // Each of the Visit* functions reads in part of the expression
436 // from the given record and the current expression stack, then
437 // return the total number of operands that it read from the
438 // expression stack.
439
Douglas Gregor025452f2009-04-17 00:04:06 +0000440 unsigned VisitStmt(Stmt *S);
441 unsigned VisitNullStmt(NullStmt *S);
442 unsigned VisitCompoundStmt(CompoundStmt *S);
443 unsigned VisitSwitchCase(SwitchCase *S);
444 unsigned VisitCaseStmt(CaseStmt *S);
445 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000446 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000447 unsigned VisitIfStmt(IfStmt *S);
448 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000449 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000450 unsigned VisitDoStmt(DoStmt *S);
451 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000452 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000453 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000454 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000455 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000456 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000457 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000458 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000459 unsigned VisitExpr(Expr *E);
460 unsigned VisitPredefinedExpr(PredefinedExpr *E);
461 unsigned VisitDeclRefExpr(DeclRefExpr *E);
462 unsigned VisitIntegerLiteral(IntegerLiteral *E);
463 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000464 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000465 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000466 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000467 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000468 unsigned VisitUnaryOperator(UnaryOperator *E);
469 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000470 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000471 unsigned VisitCallExpr(CallExpr *E);
472 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000473 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000474 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000475 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
476 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000477 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000478 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
479 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000480 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000481 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000482 unsigned VisitInitListExpr(InitListExpr *E);
483 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
484 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000485 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000486 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000487 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000488 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
489 unsigned VisitChooseExpr(ChooseExpr *E);
490 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000491 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000492 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000493 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000494 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000495 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000496 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
497 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000498 };
499}
500
Douglas Gregor025452f2009-04-17 00:04:06 +0000501unsigned PCHStmtReader::VisitStmt(Stmt *S) {
502 assert(Idx == NumStmtFields && "Incorrect statement field count");
503 return 0;
504}
505
506unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
507 VisitStmt(S);
508 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
509 return 0;
510}
511
512unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
513 VisitStmt(S);
514 unsigned NumStmts = Record[Idx++];
515 S->setStmts(Reader.getContext(),
516 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
517 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
518 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
519 return NumStmts;
520}
521
522unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
523 VisitStmt(S);
524 Reader.RecordSwitchCaseID(S, Record[Idx++]);
525 return 0;
526}
527
528unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
529 VisitSwitchCase(S);
530 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
531 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
532 S->setSubStmt(StmtStack.back());
533 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
534 return 3;
535}
536
537unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
538 VisitSwitchCase(S);
539 S->setSubStmt(StmtStack.back());
540 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
541 return 1;
542}
543
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000544unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
545 VisitStmt(S);
546 S->setID(Reader.GetIdentifierInfo(Record, Idx));
547 S->setSubStmt(StmtStack.back());
548 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
549 Reader.RecordLabelStmt(S, Record[Idx++]);
550 return 1;
551}
552
Douglas Gregor025452f2009-04-17 00:04:06 +0000553unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
554 VisitStmt(S);
555 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
556 S->setThen(StmtStack[StmtStack.size() - 2]);
557 S->setElse(StmtStack[StmtStack.size() - 1]);
558 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
559 return 3;
560}
561
562unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
563 VisitStmt(S);
564 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
565 S->setBody(StmtStack.back());
566 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
567 SwitchCase *PrevSC = 0;
568 for (unsigned N = Record.size(); Idx != N; ++Idx) {
569 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
570 if (PrevSC)
571 PrevSC->setNextSwitchCase(SC);
572 else
573 S->setSwitchCaseList(SC);
574 PrevSC = SC;
575 }
576 return 2;
577}
578
Douglas Gregord921cf92009-04-17 00:16:09 +0000579unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
580 VisitStmt(S);
581 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
582 S->setBody(StmtStack.back());
583 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
584 return 2;
585}
586
Douglas Gregor67d82492009-04-17 00:29:51 +0000587unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
588 VisitStmt(S);
589 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
590 S->setBody(StmtStack.back());
591 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 return 2;
593}
594
595unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
596 VisitStmt(S);
597 S->setInit(StmtStack[StmtStack.size() - 4]);
598 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
599 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
600 S->setBody(StmtStack.back());
601 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
602 return 4;
603}
604
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000605unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
606 VisitStmt(S);
607 Reader.SetLabelOf(S, Record[Idx++]);
608 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
609 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
610 return 0;
611}
612
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000613unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
614 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000615 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000616 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
617 return 1;
618}
619
Douglas Gregord921cf92009-04-17 00:16:09 +0000620unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
621 VisitStmt(S);
622 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 return 0;
624}
625
Douglas Gregor025452f2009-04-17 00:04:06 +0000626unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
627 VisitStmt(S);
628 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
629 return 0;
630}
631
Douglas Gregor0de9d882009-04-17 16:34:57 +0000632unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
633 VisitStmt(S);
634 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
635 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
636 return 1;
637}
638
Douglas Gregor84f21702009-04-17 16:55:36 +0000639unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
640 VisitStmt(S);
641 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
642 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643
644 if (Idx + 1 == Record.size()) {
645 // Single declaration
646 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
647 } else {
648 llvm::SmallVector<Decl *, 16> Decls;
649 Decls.reserve(Record.size() - Idx);
650 for (unsigned N = Record.size(); Idx != N; ++Idx)
651 Decls.push_back(Reader.GetDecl(Record[Idx]));
652 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
653 &Decls[0], Decls.size())));
654 }
655 return 0;
656}
657
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000658unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
659 VisitStmt(S);
660 unsigned NumOutputs = Record[Idx++];
661 unsigned NumInputs = Record[Idx++];
662 unsigned NumClobbers = Record[Idx++];
663 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
664 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
665 S->setVolatile(Record[Idx++]);
666 S->setSimple(Record[Idx++]);
667
668 unsigned StackIdx
669 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
670 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
671
672 // Outputs and inputs
673 llvm::SmallVector<std::string, 16> Names;
674 llvm::SmallVector<StringLiteral*, 16> Constraints;
675 llvm::SmallVector<Stmt*, 16> Exprs;
676 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
677 Names.push_back(Reader.ReadString(Record, Idx));
678 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
679 Exprs.push_back(StmtStack[StackIdx++]);
680 }
681 S->setOutputsAndInputs(NumOutputs, NumInputs,
682 &Names[0], &Constraints[0], &Exprs[0]);
683
684 // Constraints
685 llvm::SmallVector<StringLiteral*, 16> Clobbers;
686 for (unsigned I = 0; I != NumClobbers; ++I)
687 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
688 S->setClobbers(&Clobbers[0], NumClobbers);
689
690 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
691 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
692}
693
Douglas Gregor087fd532009-04-14 23:32:43 +0000694unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000695 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000696 E->setType(Reader.GetType(Record[Idx++]));
697 E->setTypeDependent(Record[Idx++]);
698 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000699 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000700 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000701}
702
Douglas Gregor087fd532009-04-14 23:32:43 +0000703unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000704 VisitExpr(E);
705 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
706 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000707 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000708}
709
Douglas Gregor087fd532009-04-14 23:32:43 +0000710unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000711 VisitExpr(E);
712 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
713 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000714 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000715}
716
Douglas Gregor087fd532009-04-14 23:32:43 +0000717unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000718 VisitExpr(E);
719 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
720 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000721 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000722}
723
Douglas Gregor087fd532009-04-14 23:32:43 +0000724unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000725 VisitExpr(E);
726 E->setValue(Reader.ReadAPFloat(Record, Idx));
727 E->setExact(Record[Idx++]);
728 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000729 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000730}
731
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000732unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
733 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000734 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000735 return 1;
736}
737
Douglas Gregor673ecd62009-04-15 16:35:07 +0000738unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
739 VisitExpr(E);
740 unsigned Len = Record[Idx++];
741 assert(Record[Idx] == E->getNumConcatenated() &&
742 "Wrong number of concatenated tokens!");
743 ++Idx;
744 E->setWide(Record[Idx++]);
745
746 // Read string data
747 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
748 E->setStrData(Reader.getContext(), &Str[0], Len);
749 Idx += Len;
750
751 // Read source locations
752 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
753 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
754
755 return 0;
756}
757
Douglas Gregor087fd532009-04-14 23:32:43 +0000758unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000759 VisitExpr(E);
760 E->setValue(Record[Idx++]);
761 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
762 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000763 return 0;
764}
765
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000766unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
767 VisitExpr(E);
768 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
769 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000770 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000771 return 1;
772}
773
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000774unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
775 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000776 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000777 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
778 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
779 return 1;
780}
781
782unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
783 VisitExpr(E);
784 E->setSizeof(Record[Idx++]);
785 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000786 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000787 ++Idx;
788 } else {
789 E->setArgument(Reader.GetType(Record[Idx++]));
790 }
791 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
792 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 return E->isArgumentType()? 0 : 1;
794}
795
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000796unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
797 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000798 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
799 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000800 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
801 return 2;
802}
803
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000804unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
805 VisitExpr(E);
806 E->setNumArgs(Reader.getContext(), Record[Idx++]);
807 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000808 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000809 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000810 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000811 return E->getNumArgs() + 1;
812}
813
814unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
815 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000816 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000817 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
818 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
819 E->setArrow(Record[Idx++]);
820 return 1;
821}
822
Douglas Gregor087fd532009-04-14 23:32:43 +0000823unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
824 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000825 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000826 return 1;
827}
828
Douglas Gregordb600c32009-04-15 00:25:59 +0000829unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
830 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000831 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
832 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000833 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
834 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
835 return 2;
836}
837
Douglas Gregorad90e962009-04-15 22:40:36 +0000838unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
839 VisitBinaryOperator(E);
840 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
841 E->setComputationResultType(Reader.GetType(Record[Idx++]));
842 return 2;
843}
844
845unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
846 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000847 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
848 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
849 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000850 return 3;
851}
852
Douglas Gregor087fd532009-04-14 23:32:43 +0000853unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
854 VisitCastExpr(E);
855 E->setLvalueCast(Record[Idx++]);
856 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000857}
858
Douglas Gregordb600c32009-04-15 00:25:59 +0000859unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
860 VisitCastExpr(E);
861 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
862 return 1;
863}
864
865unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
866 VisitExplicitCastExpr(E);
867 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
868 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
869 return 1;
870}
871
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000872unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
873 VisitExpr(E);
874 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000875 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000876 E->setFileScope(Record[Idx++]);
877 return 1;
878}
879
Douglas Gregord3c98a02009-04-15 23:02:49 +0000880unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
881 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000882 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000883 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
884 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
885 return 1;
886}
887
Douglas Gregord077d752009-04-16 00:55:48 +0000888unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
889 VisitExpr(E);
890 unsigned NumInits = Record[Idx++];
891 E->reserveInits(NumInits);
892 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000893 E->updateInit(I,
894 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
895 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000896 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
897 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
898 E->setInitializedFieldInUnion(
899 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
900 E->sawArrayRangeDesignator(Record[Idx++]);
901 return NumInits + 1;
902}
903
904unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
905 typedef DesignatedInitExpr::Designator Designator;
906
907 VisitExpr(E);
908 unsigned NumSubExprs = Record[Idx++];
909 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
910 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000911 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000912 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
913 E->setGNUSyntax(Record[Idx++]);
914
915 llvm::SmallVector<Designator, 4> Designators;
916 while (Idx < Record.size()) {
917 switch ((pch::DesignatorTypes)Record[Idx++]) {
918 case pch::DESIG_FIELD_DECL: {
919 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
920 SourceLocation DotLoc
921 = SourceLocation::getFromRawEncoding(Record[Idx++]);
922 SourceLocation FieldLoc
923 = SourceLocation::getFromRawEncoding(Record[Idx++]);
924 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
925 FieldLoc));
926 Designators.back().setField(Field);
927 break;
928 }
929
930 case pch::DESIG_FIELD_NAME: {
931 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
932 SourceLocation DotLoc
933 = SourceLocation::getFromRawEncoding(Record[Idx++]);
934 SourceLocation FieldLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
937 break;
938 }
939
940 case pch::DESIG_ARRAY: {
941 unsigned Index = Record[Idx++];
942 SourceLocation LBracketLoc
943 = SourceLocation::getFromRawEncoding(Record[Idx++]);
944 SourceLocation RBracketLoc
945 = SourceLocation::getFromRawEncoding(Record[Idx++]);
946 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
947 break;
948 }
949
950 case pch::DESIG_ARRAY_RANGE: {
951 unsigned Index = Record[Idx++];
952 SourceLocation LBracketLoc
953 = SourceLocation::getFromRawEncoding(Record[Idx++]);
954 SourceLocation EllipsisLoc
955 = SourceLocation::getFromRawEncoding(Record[Idx++]);
956 SourceLocation RBracketLoc
957 = SourceLocation::getFromRawEncoding(Record[Idx++]);
958 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
959 RBracketLoc));
960 break;
961 }
962 }
963 }
964 E->setDesignators(&Designators[0], Designators.size());
965
966 return NumSubExprs;
967}
968
969unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
970 VisitExpr(E);
971 return 0;
972}
973
Douglas Gregord3c98a02009-04-15 23:02:49 +0000974unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
975 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000976 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000977 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
978 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 return 1;
980}
981
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000982unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
983 VisitExpr(E);
984 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
985 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 Reader.SetLabelOf(E, Record[Idx++]);
987 return 0;
988}
989
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000990unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
991 VisitExpr(E);
992 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
994 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
995 return 1;
996}
997
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000998unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
999 VisitExpr(E);
1000 E->setArgType1(Reader.GetType(Record[Idx++]));
1001 E->setArgType2(Reader.GetType(Record[Idx++]));
1002 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 return 0;
1005}
1006
1007unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1008 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001009 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1010 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1011 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001012 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1013 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1014 return 3;
1015}
1016
1017unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1018 VisitExpr(E);
1019 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1020 return 0;
1021}
Douglas Gregord3c98a02009-04-15 23:02:49 +00001022
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001023unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1024 VisitExpr(E);
1025 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +00001026 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001027 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1028 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1029 return NumExprs;
1030}
1031
Douglas Gregor84af7c22009-04-17 19:21:43 +00001032unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1033 VisitExpr(E);
1034 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1035 E->setHasBlockDeclRefExprs(Record[Idx++]);
1036 return 0;
1037}
1038
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001039unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1040 VisitExpr(E);
1041 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1042 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1043 E->setByRef(Record[Idx++]);
1044 return 0;
1045}
1046
Chris Lattner3a57a372009-04-22 06:29:42 +00001047//===----------------------------------------------------------------------===//
1048// Objective-C Expressions and Statements
1049
1050unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1051 VisitExpr(E);
1052 E->setString(cast<StringLiteral>(StmtStack.back()));
1053 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1054 return 1;
1055}
1056
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001057unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1058 VisitExpr(E);
1059 E->setEncodedType(Reader.GetType(Record[Idx++]));
1060 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1061 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1062 return 0;
1063}
1064
Chris Lattner3a57a372009-04-22 06:29:42 +00001065unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1066 VisitExpr(E);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001067 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattner3a57a372009-04-22 06:29:42 +00001068 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1069 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1070 return 0;
1071}
1072
1073unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1074 VisitExpr(E);
1075 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1076 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1077 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1078 return 0;
1079}
1080
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001081
Douglas Gregor668c1a42009-04-21 22:25:48 +00001082//===----------------------------------------------------------------------===//
1083// PCH reader implementation
1084//===----------------------------------------------------------------------===//
1085
1086namespace {
1087class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1088 PCHReader &Reader;
1089
1090 // If we know the IdentifierInfo in advance, it is here and we will
1091 // not build a new one. Used when deserializing information about an
1092 // identifier that was constructed before the PCH file was read.
1093 IdentifierInfo *KnownII;
1094
1095public:
1096 typedef IdentifierInfo * data_type;
1097
1098 typedef const std::pair<const char*, unsigned> external_key_type;
1099
1100 typedef external_key_type internal_key_type;
1101
1102 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1103 : Reader(Reader), KnownII(II) { }
1104
1105 static bool EqualKey(const internal_key_type& a,
1106 const internal_key_type& b) {
1107 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1108 : false;
1109 }
1110
1111 static unsigned ComputeHash(const internal_key_type& a) {
1112 return BernsteinHash(a.first, a.second);
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 static std::pair<const char*, unsigned>
1128 ReadKey(const unsigned char* d, unsigned n) {
1129 assert(n >= 2 && d[n-1] == '\0');
1130 return std::make_pair((const char*) d, n-1);
1131 }
1132
1133 IdentifierInfo *ReadData(const internal_key_type& k,
1134 const unsigned char* d,
1135 unsigned DataLen) {
1136 using namespace clang::io;
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001137 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001138 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1139 Bits >>= 1;
1140 bool Poisoned = Bits & 0x01;
1141 Bits >>= 1;
1142 bool ExtensionToken = Bits & 0x01;
1143 Bits >>= 1;
1144 bool hasMacroDefinition = Bits & 0x01;
1145 Bits >>= 1;
1146 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1147 Bits >>= 10;
1148 unsigned TokenID = Bits & 0xFF;
1149 Bits >>= 8;
1150
Douglas Gregor668c1a42009-04-21 22:25:48 +00001151 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001152 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor668c1a42009-04-21 22:25:48 +00001153 DataLen -= 8;
1154
1155 // Build the IdentifierInfo itself and link the identifier ID with
1156 // the new IdentifierInfo.
1157 IdentifierInfo *II = KnownII;
1158 if (!II)
1159 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1160 k.first, k.first + k.second);
1161 Reader.SetIdentifierInfo(ID, II);
1162
Douglas Gregor2deaea32009-04-22 18:49:13 +00001163 // Set or check the various bits in the IdentifierInfo structure.
1164 // FIXME: Load token IDs lazily, too?
1165 assert((unsigned)II->getTokenID() == TokenID &&
1166 "Incorrect token ID loaded");
1167 (void)TokenID;
1168 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1169 assert(II->isExtensionToken() == ExtensionToken &&
1170 "Incorrect extension token flag");
1171 (void)ExtensionToken;
1172 II->setIsPoisoned(Poisoned);
1173 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1174 "Incorrect C++ operator keyword flag");
1175 (void)CPlusPlusOperatorKeyword;
1176
Douglas Gregor37e26842009-04-21 23:56:24 +00001177 // If this identifier is a macro, deserialize the macro
1178 // definition.
1179 if (hasMacroDefinition) {
1180 uint32_t Offset = ReadUnalignedLE64(d);
1181 Reader.ReadMacroRecord(Offset);
1182 DataLen -= 8;
1183 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001184
1185 // Read all of the declarations visible at global scope with this
1186 // name.
1187 Sema *SemaObj = Reader.getSema();
1188 while (DataLen > 0) {
1189 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001190 if (SemaObj) {
1191 // Introduce this declaration into the translation-unit scope
1192 // and add it to the declaration chain for this identifier, so
1193 // that (unqualified) name lookup will find it.
1194 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1195 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1196 } else {
1197 // Queue this declaration so that it will be added to the
1198 // translation unit scope and identifier's declaration chain
1199 // once a Sema object is known.
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001200 Reader.PreloadedDecls.push_back(D);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001201 }
1202
1203 DataLen -= 4;
1204 }
1205 return II;
1206 }
1207};
1208
1209} // end anonymous namespace
1210
1211/// \brief The on-disk hash table used to contain information about
1212/// all of the identifiers in the program.
1213typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1214 PCHIdentifierLookupTable;
1215
Douglas Gregor2cf26342009-04-09 22:27:44 +00001216// FIXME: use the diagnostics machinery
1217static bool Error(const char *Str) {
1218 std::fprintf(stderr, "%s\n", Str);
1219 return true;
1220}
1221
Douglas Gregore1d918e2009-04-10 23:10:45 +00001222/// \brief Check the contents of the predefines buffer against the
1223/// contents of the predefines buffer used to build the PCH file.
1224///
1225/// The contents of the two predefines buffers should be the same. If
1226/// not, then some command-line option changed the preprocessor state
1227/// and we must reject the PCH file.
1228///
1229/// \param PCHPredef The start of the predefines buffer in the PCH
1230/// file.
1231///
1232/// \param PCHPredefLen The length of the predefines buffer in the PCH
1233/// file.
1234///
1235/// \param PCHBufferID The FileID for the PCH predefines buffer.
1236///
1237/// \returns true if there was a mismatch (in which case the PCH file
1238/// should be ignored), or false otherwise.
1239bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1240 unsigned PCHPredefLen,
1241 FileID PCHBufferID) {
1242 const char *Predef = PP.getPredefines().c_str();
1243 unsigned PredefLen = PP.getPredefines().size();
1244
1245 // If the two predefines buffers compare equal, we're done!.
1246 if (PredefLen == PCHPredefLen &&
1247 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1248 return false;
1249
1250 // The predefines buffers are different. Produce a reasonable
1251 // diagnostic showing where they are different.
1252
1253 // The source locations (potentially in the two different predefines
1254 // buffers)
1255 SourceLocation Loc1, Loc2;
1256 SourceManager &SourceMgr = PP.getSourceManager();
1257
1258 // Create a source buffer for our predefines string, so
1259 // that we can build a diagnostic that points into that
1260 // source buffer.
1261 FileID BufferID;
1262 if (Predef && Predef[0]) {
1263 llvm::MemoryBuffer *Buffer
1264 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1265 "<built-in>");
1266 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1267 }
1268
1269 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1270 std::pair<const char *, const char *> Locations
1271 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1272
1273 if (Locations.first != Predef + MinLen) {
1274 // We found the location in the two buffers where there is a
1275 // difference. Form source locations to point there (in both
1276 // buffers).
1277 unsigned Offset = Locations.first - Predef;
1278 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1279 .getFileLocWithOffset(Offset);
1280 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1281 .getFileLocWithOffset(Offset);
1282 } else if (PredefLen > PCHPredefLen) {
1283 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1284 .getFileLocWithOffset(MinLen);
1285 } else {
1286 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1287 .getFileLocWithOffset(MinLen);
1288 }
1289
1290 Diag(Loc1, diag::warn_pch_preprocessor);
1291 if (Loc2.isValid())
1292 Diag(Loc2, diag::note_predef_in_pch);
1293 Diag(diag::note_ignoring_pch) << FileName;
1294 return true;
1295}
1296
Douglas Gregorbd945002009-04-13 16:31:14 +00001297/// \brief Read the line table in the source manager block.
1298/// \returns true if ther was an error.
1299static bool ParseLineTable(SourceManager &SourceMgr,
1300 llvm::SmallVectorImpl<uint64_t> &Record) {
1301 unsigned Idx = 0;
1302 LineTableInfo &LineTable = SourceMgr.getLineTable();
1303
1304 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001305 std::map<int, int> FileIDs;
1306 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001307 // Extract the file name
1308 unsigned FilenameLen = Record[Idx++];
1309 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1310 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +00001311 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1312 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001313 }
1314
1315 // Parse the line entries
1316 std::vector<LineEntry> Entries;
1317 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +00001318 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +00001319
1320 // Extract the line entries
1321 unsigned NumEntries = Record[Idx++];
1322 Entries.clear();
1323 Entries.reserve(NumEntries);
1324 for (unsigned I = 0; I != NumEntries; ++I) {
1325 unsigned FileOffset = Record[Idx++];
1326 unsigned LineNo = Record[Idx++];
1327 int FilenameID = Record[Idx++];
1328 SrcMgr::CharacteristicKind FileKind
1329 = (SrcMgr::CharacteristicKind)Record[Idx++];
1330 unsigned IncludeOffset = Record[Idx++];
1331 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1332 FileKind, IncludeOffset));
1333 }
1334 LineTable.AddEntry(FID, Entries);
1335 }
1336
1337 return false;
1338}
1339
Douglas Gregor14f79002009-04-10 03:52:48 +00001340/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001341PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001342 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001343 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1344 Error("Malformed source manager block record");
1345 return Failure;
1346 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001347
1348 SourceManager &SourceMgr = Context.getSourceManager();
1349 RecordData Record;
1350 while (true) {
1351 unsigned Code = Stream.ReadCode();
1352 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001353 if (Stream.ReadBlockEnd()) {
1354 Error("Error at end of Source Manager block");
1355 return Failure;
1356 }
1357
1358 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001359 }
1360
1361 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1362 // No known subblocks, always skip them.
1363 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001364 if (Stream.SkipBlock()) {
1365 Error("Malformed block record");
1366 return Failure;
1367 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001368 continue;
1369 }
1370
1371 if (Code == llvm::bitc::DEFINE_ABBREV) {
1372 Stream.ReadAbbrevRecord();
1373 continue;
1374 }
1375
1376 // Read a record.
1377 const char *BlobStart;
1378 unsigned BlobLen;
1379 Record.clear();
1380 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1381 default: // Default behavior: ignore.
1382 break;
1383
1384 case pch::SM_SLOC_FILE_ENTRY: {
1385 // FIXME: We would really like to delay the creation of this
1386 // FileEntry until it is actually required, e.g., when producing
1387 // a diagnostic with a source location in this file.
1388 const FileEntry *File
1389 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1390 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001391 FileID ID = SourceMgr.createFileID(File,
1392 SourceLocation::getFromRawEncoding(Record[1]),
1393 (CharacteristicKind)Record[2]);
1394 if (Record[3])
1395 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1396 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001397 break;
1398 }
1399
1400 case pch::SM_SLOC_BUFFER_ENTRY: {
1401 const char *Name = BlobStart;
1402 unsigned Code = Stream.ReadCode();
1403 Record.clear();
1404 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1405 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001406 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001407 llvm::MemoryBuffer *Buffer
1408 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1409 BlobStart + BlobLen - 1,
1410 Name);
1411 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1412
1413 if (strcmp(Name, "<built-in>") == 0
1414 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1415 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001416 break;
1417 }
1418
1419 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1420 SourceLocation SpellingLoc
1421 = SourceLocation::getFromRawEncoding(Record[1]);
1422 SourceMgr.createInstantiationLoc(
1423 SpellingLoc,
1424 SourceLocation::getFromRawEncoding(Record[2]),
1425 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001426 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001427 break;
1428 }
1429
Chris Lattner2c78b872009-04-14 23:22:57 +00001430 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001431 if (ParseLineTable(SourceMgr, Record))
1432 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001433 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001434 }
1435 }
1436}
1437
Douglas Gregor37e26842009-04-21 23:56:24 +00001438void PCHReader::ReadMacroRecord(uint64_t Offset) {
1439 // Keep track of where we are in the stream, then jump back there
1440 // after reading this macro.
1441 SavedStreamPosition SavedPosition(Stream);
1442
1443 Stream.JumpToBit(Offset);
1444 RecordData Record;
1445 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1446 MacroInfo *Macro = 0;
1447 while (true) {
1448 unsigned Code = Stream.ReadCode();
1449 switch (Code) {
1450 case llvm::bitc::END_BLOCK:
1451 return;
1452
1453 case llvm::bitc::ENTER_SUBBLOCK:
1454 // No known subblocks, always skip them.
1455 Stream.ReadSubBlockID();
1456 if (Stream.SkipBlock()) {
1457 Error("Malformed block record");
1458 return;
1459 }
1460 continue;
1461
1462 case llvm::bitc::DEFINE_ABBREV:
1463 Stream.ReadAbbrevRecord();
1464 continue;
1465 default: break;
1466 }
1467
1468 // Read a record.
1469 Record.clear();
1470 pch::PreprocessorRecordTypes RecType =
1471 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1472 switch (RecType) {
1473 case pch::PP_COUNTER_VALUE:
1474 // Skip this record.
1475 break;
1476
1477 case pch::PP_MACRO_OBJECT_LIKE:
1478 case pch::PP_MACRO_FUNCTION_LIKE: {
1479 // If we already have a macro, that means that we've hit the end
1480 // of the definition of the macro we were looking for. We're
1481 // done.
1482 if (Macro)
1483 return;
1484
1485 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1486 if (II == 0) {
1487 Error("Macro must have a name");
1488 return;
1489 }
1490 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1491 bool isUsed = Record[2];
1492
1493 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1494 MI->setIsUsed(isUsed);
1495
1496 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1497 // Decode function-like macro info.
1498 bool isC99VarArgs = Record[3];
1499 bool isGNUVarArgs = Record[4];
1500 MacroArgs.clear();
1501 unsigned NumArgs = Record[5];
1502 for (unsigned i = 0; i != NumArgs; ++i)
1503 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1504
1505 // Install function-like macro info.
1506 MI->setIsFunctionLike();
1507 if (isC99VarArgs) MI->setIsC99Varargs();
1508 if (isGNUVarArgs) MI->setIsGNUVarargs();
1509 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1510 PP.getPreprocessorAllocator());
1511 }
1512
1513 // Finally, install the macro.
1514 PP.setMacroInfo(II, MI);
1515
1516 // Remember that we saw this macro last so that we add the tokens that
1517 // form its body to it.
1518 Macro = MI;
1519 ++NumMacrosRead;
1520 break;
1521 }
1522
1523 case pch::PP_TOKEN: {
1524 // If we see a TOKEN before a PP_MACRO_*, then the file is
1525 // erroneous, just pretend we didn't see this.
1526 if (Macro == 0) break;
1527
1528 Token Tok;
1529 Tok.startToken();
1530 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1531 Tok.setLength(Record[1]);
1532 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1533 Tok.setIdentifierInfo(II);
1534 Tok.setKind((tok::TokenKind)Record[3]);
1535 Tok.setFlag((Token::TokenFlags)Record[4]);
1536 Macro->AddTokenToBody(Tok);
1537 break;
1538 }
1539 }
1540 }
1541}
1542
Chris Lattner42d42b52009-04-10 21:41:48 +00001543bool PCHReader::ReadPreprocessorBlock() {
1544 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1545 return Error("Malformed preprocessor block record");
1546
Chris Lattner42d42b52009-04-10 21:41:48 +00001547 RecordData Record;
Chris Lattner42d42b52009-04-10 21:41:48 +00001548 while (true) {
1549 unsigned Code = Stream.ReadCode();
1550 switch (Code) {
1551 case llvm::bitc::END_BLOCK:
1552 if (Stream.ReadBlockEnd())
1553 return Error("Error at end of preprocessor block");
1554 return false;
1555
1556 case llvm::bitc::ENTER_SUBBLOCK:
1557 // No known subblocks, always skip them.
1558 Stream.ReadSubBlockID();
1559 if (Stream.SkipBlock())
1560 return Error("Malformed block record");
1561 continue;
1562
1563 case llvm::bitc::DEFINE_ABBREV:
1564 Stream.ReadAbbrevRecord();
1565 continue;
1566 default: break;
1567 }
1568
1569 // Read a record.
1570 Record.clear();
1571 pch::PreprocessorRecordTypes RecType =
1572 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1573 switch (RecType) {
1574 default: // Default behavior: ignore unknown records.
1575 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001576 case pch::PP_COUNTER_VALUE:
1577 if (!Record.empty())
1578 PP.setCounterValue(Record[0]);
1579 break;
1580
Chris Lattner42d42b52009-04-10 21:41:48 +00001581 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregor37e26842009-04-21 23:56:24 +00001582 case pch::PP_MACRO_FUNCTION_LIKE:
1583 case pch::PP_TOKEN:
1584 // Once we've hit a macro definition or a token, we're done.
1585 return false;
Chris Lattner42d42b52009-04-10 21:41:48 +00001586 }
1587 }
1588}
1589
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001590bool PCHReader::ReadSelectorBlock() {
1591 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1592 return Error("Malformed selector block record");
1593
1594 RecordData Record;
1595 while (true) {
1596 unsigned Code = Stream.ReadCode();
1597 switch (Code) {
1598 case llvm::bitc::END_BLOCK:
1599 if (Stream.ReadBlockEnd())
1600 return Error("Error at end of preprocessor block");
1601 return false;
1602
1603 case llvm::bitc::ENTER_SUBBLOCK:
1604 // No known subblocks, always skip them.
1605 Stream.ReadSubBlockID();
1606 if (Stream.SkipBlock())
1607 return Error("Malformed block record");
1608 continue;
1609
1610 case llvm::bitc::DEFINE_ABBREV:
1611 Stream.ReadAbbrevRecord();
1612 continue;
1613 default: break;
1614 }
1615
1616 // Read a record.
1617 Record.clear();
1618 pch::PCHRecordTypes RecType =
1619 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1620 switch (RecType) {
1621 default: // Default behavior: ignore unknown records.
1622 break;
1623 case pch::SELECTOR_TABLE:
1624 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1625 unsigned NumSels = Record[Idx++];
1626
1627 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1628 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1629 unsigned NumArgs = Record[Idx++];
1630 KeyIdents.clear();
1631 if (NumArgs <= 1) {
1632 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1633 assert(II && "DecodeIdentifierInfo returned 0");
1634 KeyIdents.push_back(II);
1635 } else {
1636 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1637 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1638 assert(II && "DecodeIdentifierInfo returned 0");
1639 KeyIdents.push_back(II);
1640 }
1641 }
1642 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1643 SelectorData.push_back(Sel);
1644 }
1645 }
1646 }
1647 return false;
1648}
1649
Douglas Gregor668c1a42009-04-21 22:25:48 +00001650PCHReader::PCHReadResult
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001651PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1652 uint64_t &SelectorBlockOffset) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001653 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1654 Error("Malformed block record");
1655 return Failure;
1656 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001657
1658 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001659 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001660 while (!Stream.AtEndOfStream()) {
1661 unsigned Code = Stream.ReadCode();
1662 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001663 if (Stream.ReadBlockEnd()) {
1664 Error("Error at end of module block");
1665 return Failure;
1666 }
Chris Lattner7356a312009-04-11 21:15:38 +00001667
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001668 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001669 }
1670
1671 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1672 switch (Stream.ReadSubBlockID()) {
1673 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1674 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1675 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001676 if (Stream.SkipBlock()) {
1677 Error("Malformed block record");
1678 return Failure;
1679 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001680 break;
1681
Chris Lattner7356a312009-04-11 21:15:38 +00001682 case pch::PREPROCESSOR_BLOCK_ID:
1683 // Skip the preprocessor block for now, but remember where it is. We
1684 // want to read it in after the identifier table.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001685 if (PreprocessorBlockOffset) {
Chris Lattner7356a312009-04-11 21:15:38 +00001686 Error("Multiple preprocessor blocks found.");
1687 return Failure;
1688 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001689 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001690 if (Stream.SkipBlock()) {
1691 Error("Malformed block record");
1692 return Failure;
1693 }
1694 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001695
1696 case pch::SELECTOR_BLOCK_ID:
1697 // Skip the selector block for now, but remember where it is. We
1698 // want to read it in after the identifier table.
1699 if (SelectorBlockOffset) {
1700 Error("Multiple selector blocks found.");
1701 return Failure;
1702 }
1703 SelectorBlockOffset = Stream.GetCurrentBitNo();
1704 if (Stream.SkipBlock()) {
1705 Error("Malformed block record");
1706 return Failure;
1707 }
1708 break;
Chris Lattner7356a312009-04-11 21:15:38 +00001709
Douglas Gregor14f79002009-04-10 03:52:48 +00001710 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001711 switch (ReadSourceManagerBlock()) {
1712 case Success:
1713 break;
1714
1715 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001716 Error("Malformed source manager block");
1717 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001718
1719 case IgnorePCH:
1720 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001721 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001722 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001723 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001724 continue;
1725 }
1726
1727 if (Code == llvm::bitc::DEFINE_ABBREV) {
1728 Stream.ReadAbbrevRecord();
1729 continue;
1730 }
1731
1732 // Read and process a record.
1733 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001734 const char *BlobStart = 0;
1735 unsigned BlobLen = 0;
1736 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1737 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001738 default: // Default behavior: ignore.
1739 break;
1740
1741 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001742 if (!TypeOffsets.empty()) {
1743 Error("Duplicate TYPE_OFFSET record in PCH file");
1744 return Failure;
1745 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001746 TypeOffsets.swap(Record);
1747 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1748 break;
1749
1750 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001751 if (!DeclOffsets.empty()) {
1752 Error("Duplicate DECL_OFFSET record in PCH file");
1753 return Failure;
1754 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001755 DeclOffsets.swap(Record);
1756 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1757 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001758
1759 case pch::LANGUAGE_OPTIONS:
1760 if (ParseLanguageOptions(Record))
1761 return IgnorePCH;
1762 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001763
Douglas Gregorafaf3082009-04-11 00:14:32 +00001764 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001765 std::string TargetTriple(BlobStart, BlobLen);
1766 if (TargetTriple != Context.Target.getTargetTriple()) {
1767 Diag(diag::warn_pch_target_triple)
1768 << TargetTriple << Context.Target.getTargetTriple();
1769 Diag(diag::note_ignoring_pch) << FileName;
1770 return IgnorePCH;
1771 }
1772 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001773 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001774
1775 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001776 IdentifierTableData = BlobStart;
1777 IdentifierLookupTable
1778 = PCHIdentifierLookupTable::Create(
1779 (const unsigned char *)IdentifierTableData + Record[0],
1780 (const unsigned char *)IdentifierTableData,
1781 PCHIdentifierLookupTrait(*this));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001782 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001783 break;
1784
1785 case pch::IDENTIFIER_OFFSET:
1786 if (!IdentifierData.empty()) {
1787 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1788 return Failure;
1789 }
1790 IdentifierData.swap(Record);
1791#ifndef NDEBUG
1792 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1793 if ((IdentifierData[I] & 0x01) == 0) {
1794 Error("Malformed identifier table in the precompiled header");
1795 return Failure;
1796 }
1797 }
1798#endif
1799 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001800
1801 case pch::EXTERNAL_DEFINITIONS:
1802 if (!ExternalDefinitions.empty()) {
1803 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1804 return Failure;
1805 }
1806 ExternalDefinitions.swap(Record);
1807 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001808
Douglas Gregorad1de002009-04-18 05:55:16 +00001809 case pch::SPECIAL_TYPES:
1810 SpecialTypes.swap(Record);
1811 break;
1812
Douglas Gregor3e1af842009-04-17 22:13:46 +00001813 case pch::STATISTICS:
1814 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001815 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001816 TotalLexicalDeclContexts = Record[2];
1817 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001818 break;
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001819 case pch::TENTATIVE_DEFINITIONS:
1820 if (!TentativeDefinitions.empty()) {
1821 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1822 return Failure;
1823 }
1824 TentativeDefinitions.swap(Record);
1825 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001826
1827 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1828 if (!LocallyScopedExternalDecls.empty()) {
1829 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1830 return Failure;
1831 }
1832 LocallyScopedExternalDecls.swap(Record);
1833 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001834 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001835 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001836 Error("Premature end of bitstream");
1837 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001838}
1839
Douglas Gregore1d918e2009-04-10 23:10:45 +00001840PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001841 // Set the PCH file name.
1842 this->FileName = FileName;
1843
Douglas Gregor2cf26342009-04-09 22:27:44 +00001844 // Open the PCH file.
1845 std::string ErrStr;
1846 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001847 if (!Buffer) {
1848 Error(ErrStr.c_str());
1849 return IgnorePCH;
1850 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001851
1852 // Initialize the stream
1853 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1854 (const unsigned char *)Buffer->getBufferEnd());
1855
1856 // Sniff for the signature.
1857 if (Stream.Read(8) != 'C' ||
1858 Stream.Read(8) != 'P' ||
1859 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001860 Stream.Read(8) != 'H') {
1861 Error("Not a PCH file");
1862 return IgnorePCH;
1863 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001864
1865 // We expect a number of well-defined blocks, though we don't necessarily
1866 // need to understand them all.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001867 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001868 uint64_t SelectorBlockOffset = 0;
1869
Douglas Gregor2cf26342009-04-09 22:27:44 +00001870 while (!Stream.AtEndOfStream()) {
1871 unsigned Code = Stream.ReadCode();
1872
Douglas Gregore1d918e2009-04-10 23:10:45 +00001873 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1874 Error("Invalid record at top-level");
1875 return Failure;
1876 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001877
1878 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001879
Douglas Gregor2cf26342009-04-09 22:27:44 +00001880 // We only know the PCH subblock ID.
1881 switch (BlockID) {
1882 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001883 if (Stream.ReadBlockInfoBlock()) {
1884 Error("Malformed BlockInfoBlock");
1885 return Failure;
1886 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001887 break;
1888 case pch::PCH_BLOCK_ID:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001889 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001890 case Success:
1891 break;
1892
1893 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001894 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001895
1896 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001897 // FIXME: We could consider reading through to the end of this
1898 // PCH block, skipping subblocks, to see if there are other
1899 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001900 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001901 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001902 break;
1903 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001904 if (Stream.SkipBlock()) {
1905 Error("Malformed block record");
1906 return Failure;
1907 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001908 break;
1909 }
1910 }
1911
1912 // Load the translation unit declaration
1913 ReadDeclRecord(DeclOffsets[0], 0);
1914
Douglas Gregor668c1a42009-04-21 22:25:48 +00001915 // Initialization of builtins and library builtins occurs before the
1916 // PCH file is read, so there may be some identifiers that were
1917 // loaded into the IdentifierTable before we intercepted the
1918 // creation of identifiers. Iterate through the list of known
1919 // identifiers and determine whether we have to establish
1920 // preprocessor definitions or top-level identifier declaration
1921 // chains for those identifiers.
1922 //
1923 // We copy the IdentifierInfo pointers to a small vector first,
1924 // since de-serializing declarations or macro definitions can add
1925 // new entries into the identifier table, invalidating the
1926 // iterators.
1927 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1928 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1929 IdEnd = PP.getIdentifierTable().end();
1930 Id != IdEnd; ++Id)
1931 Identifiers.push_back(Id->second);
1932 PCHIdentifierLookupTable *IdTable
1933 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1934 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1935 IdentifierInfo *II = Identifiers[I];
1936 // Look in the on-disk hash table for an entry for
1937 PCHIdentifierLookupTrait Info(*this, II);
1938 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1939 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1940 if (Pos == IdTable->end())
1941 continue;
1942
1943 // Dereferencing the iterator has the effect of populating the
1944 // IdentifierInfo node with the various declarations it needs.
1945 (void)*Pos;
1946 }
1947
Douglas Gregorad1de002009-04-18 05:55:16 +00001948 // Load the special types.
1949 Context.setBuiltinVaListType(
1950 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1951
Douglas Gregor668c1a42009-04-21 22:25:48 +00001952 // If we saw the preprocessor block, read it now.
1953 if (PreprocessorBlockOffset) {
1954 SavedStreamPosition SavedPos(Stream);
1955 Stream.JumpToBit(PreprocessorBlockOffset);
1956 if (ReadPreprocessorBlock()) {
1957 Error("Malformed preprocessor block");
1958 return Failure;
Douglas Gregor0b748912009-04-14 21:18:50 +00001959 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001960 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001961 if (SelectorBlockOffset) {
1962 SavedStreamPosition SavedPos(Stream);
1963 Stream.JumpToBit(SelectorBlockOffset);
1964 if (ReadSelectorBlock()) {
1965 Error("Malformed preprocessor block");
1966 return Failure;
1967 }
1968 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001969
Douglas Gregor668c1a42009-04-21 22:25:48 +00001970 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001971}
1972
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001973/// \brief Parse the record that corresponds to a LangOptions data
1974/// structure.
1975///
1976/// This routine compares the language options used to generate the
1977/// PCH file against the language options set for the current
1978/// compilation. For each option, we classify differences between the
1979/// two compiler states as either "benign" or "important". Benign
1980/// differences don't matter, and we accept them without complaint
1981/// (and without modifying the language options). Differences between
1982/// the states for important options cause the PCH file to be
1983/// unusable, so we emit a warning and return true to indicate that
1984/// there was an error.
1985///
1986/// \returns true if the PCH file is unacceptable, false otherwise.
1987bool PCHReader::ParseLanguageOptions(
1988 const llvm::SmallVectorImpl<uint64_t> &Record) {
1989 const LangOptions &LangOpts = Context.getLangOptions();
1990#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1991#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1992 if (Record[Idx] != LangOpts.Option) { \
1993 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1994 Diag(diag::note_ignoring_pch) << FileName; \
1995 return true; \
1996 } \
1997 ++Idx
1998
1999 unsigned Idx = 0;
2000 PARSE_LANGOPT_BENIGN(Trigraphs);
2001 PARSE_LANGOPT_BENIGN(BCPLComment);
2002 PARSE_LANGOPT_BENIGN(DollarIdents);
2003 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2004 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2005 PARSE_LANGOPT_BENIGN(ImplicitInt);
2006 PARSE_LANGOPT_BENIGN(Digraphs);
2007 PARSE_LANGOPT_BENIGN(HexFloats);
2008 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2009 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2010 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2011 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2012 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2013 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2014 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2015 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2016 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2017 PARSE_LANGOPT_BENIGN(PascalStrings);
2018 PARSE_LANGOPT_BENIGN(Boolean);
2019 PARSE_LANGOPT_BENIGN(WritableStrings);
2020 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2021 diag::warn_pch_lax_vector_conversions);
2022 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2023 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2024 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2025 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2026 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2027 diag::warn_pch_thread_safe_statics);
2028 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2029 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2030 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2031 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2032 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2033 diag::warn_pch_heinous_extensions);
2034 // FIXME: Most of the options below are benign if the macro wasn't
2035 // used. Unfortunately, this means that a PCH compiled without
2036 // optimization can't be used with optimization turned on, even
2037 // though the only thing that changes is whether __OPTIMIZE__ was
2038 // defined... but if __OPTIMIZE__ never showed up in the header, it
2039 // doesn't matter. We could consider making this some special kind
2040 // of check.
2041 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2042 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2043 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2044 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2045 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2046 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2047 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2048 Diag(diag::warn_pch_gc_mode)
2049 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2050 Diag(diag::note_ignoring_pch) << FileName;
2051 return true;
2052 }
2053 ++Idx;
2054 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2055 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2056#undef PARSE_LANGOPT_IRRELEVANT
2057#undef PARSE_LANGOPT_BENIGN
2058
2059 return false;
2060}
2061
Douglas Gregor2cf26342009-04-09 22:27:44 +00002062/// \brief Read and return the type at the given offset.
2063///
2064/// This routine actually reads the record corresponding to the type
2065/// at the given offset in the bitstream. It is a helper routine for
2066/// GetType, which deals with reading type IDs.
2067QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002068 // Keep track of where we are in the stream, then jump back there
2069 // after reading this type.
2070 SavedStreamPosition SavedPosition(Stream);
2071
Douglas Gregor2cf26342009-04-09 22:27:44 +00002072 Stream.JumpToBit(Offset);
2073 RecordData Record;
2074 unsigned Code = Stream.ReadCode();
2075 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00002076 case pch::TYPE_EXT_QUAL: {
2077 assert(Record.size() == 3 &&
2078 "Incorrect encoding of extended qualifier type");
2079 QualType Base = GetType(Record[0]);
2080 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2081 unsigned AddressSpace = Record[2];
2082
2083 QualType T = Base;
2084 if (GCAttr != QualType::GCNone)
2085 T = Context.getObjCGCQualType(T, GCAttr);
2086 if (AddressSpace)
2087 T = Context.getAddrSpaceQualType(T, AddressSpace);
2088 return T;
2089 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002090
Douglas Gregor2cf26342009-04-09 22:27:44 +00002091 case pch::TYPE_FIXED_WIDTH_INT: {
2092 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2093 return Context.getFixedWidthIntType(Record[0], Record[1]);
2094 }
2095
2096 case pch::TYPE_COMPLEX: {
2097 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2098 QualType ElemType = GetType(Record[0]);
2099 return Context.getComplexType(ElemType);
2100 }
2101
2102 case pch::TYPE_POINTER: {
2103 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2104 QualType PointeeType = GetType(Record[0]);
2105 return Context.getPointerType(PointeeType);
2106 }
2107
2108 case pch::TYPE_BLOCK_POINTER: {
2109 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2110 QualType PointeeType = GetType(Record[0]);
2111 return Context.getBlockPointerType(PointeeType);
2112 }
2113
2114 case pch::TYPE_LVALUE_REFERENCE: {
2115 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2116 QualType PointeeType = GetType(Record[0]);
2117 return Context.getLValueReferenceType(PointeeType);
2118 }
2119
2120 case pch::TYPE_RVALUE_REFERENCE: {
2121 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2122 QualType PointeeType = GetType(Record[0]);
2123 return Context.getRValueReferenceType(PointeeType);
2124 }
2125
2126 case pch::TYPE_MEMBER_POINTER: {
2127 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2128 QualType PointeeType = GetType(Record[0]);
2129 QualType ClassType = GetType(Record[1]);
2130 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2131 }
2132
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002133 case pch::TYPE_CONSTANT_ARRAY: {
2134 QualType ElementType = GetType(Record[0]);
2135 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2136 unsigned IndexTypeQuals = Record[2];
2137 unsigned Idx = 3;
2138 llvm::APInt Size = ReadAPInt(Record, Idx);
2139 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2140 }
2141
2142 case pch::TYPE_INCOMPLETE_ARRAY: {
2143 QualType ElementType = GetType(Record[0]);
2144 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2145 unsigned IndexTypeQuals = Record[2];
2146 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2147 }
2148
2149 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002150 QualType ElementType = GetType(Record[0]);
2151 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2152 unsigned IndexTypeQuals = Record[2];
2153 return Context.getVariableArrayType(ElementType, ReadExpr(),
2154 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002155 }
2156
2157 case pch::TYPE_VECTOR: {
2158 if (Record.size() != 2) {
2159 Error("Incorrect encoding of vector type in PCH file");
2160 return QualType();
2161 }
2162
2163 QualType ElementType = GetType(Record[0]);
2164 unsigned NumElements = Record[1];
2165 return Context.getVectorType(ElementType, NumElements);
2166 }
2167
2168 case pch::TYPE_EXT_VECTOR: {
2169 if (Record.size() != 2) {
2170 Error("Incorrect encoding of extended vector type in PCH file");
2171 return QualType();
2172 }
2173
2174 QualType ElementType = GetType(Record[0]);
2175 unsigned NumElements = Record[1];
2176 return Context.getExtVectorType(ElementType, NumElements);
2177 }
2178
2179 case pch::TYPE_FUNCTION_NO_PROTO: {
2180 if (Record.size() != 1) {
2181 Error("Incorrect encoding of no-proto function type");
2182 return QualType();
2183 }
2184 QualType ResultType = GetType(Record[0]);
2185 return Context.getFunctionNoProtoType(ResultType);
2186 }
2187
2188 case pch::TYPE_FUNCTION_PROTO: {
2189 QualType ResultType = GetType(Record[0]);
2190 unsigned Idx = 1;
2191 unsigned NumParams = Record[Idx++];
2192 llvm::SmallVector<QualType, 16> ParamTypes;
2193 for (unsigned I = 0; I != NumParams; ++I)
2194 ParamTypes.push_back(GetType(Record[Idx++]));
2195 bool isVariadic = Record[Idx++];
2196 unsigned Quals = Record[Idx++];
2197 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2198 isVariadic, Quals);
2199 }
2200
2201 case pch::TYPE_TYPEDEF:
2202 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2203 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2204
2205 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00002206 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002207
2208 case pch::TYPE_TYPEOF: {
2209 if (Record.size() != 1) {
2210 Error("Incorrect encoding of typeof(type) in PCH file");
2211 return QualType();
2212 }
2213 QualType UnderlyingType = GetType(Record[0]);
2214 return Context.getTypeOfType(UnderlyingType);
2215 }
2216
2217 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00002218 assert(Record.size() == 1 && "Incorrect encoding of record type");
2219 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002220
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002221 case pch::TYPE_ENUM:
2222 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2223 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2224
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002225 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner4dcf151a2009-04-22 05:57:30 +00002226 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2227 return Context.getObjCInterfaceType(
2228 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002229
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002230 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2231 unsigned Idx = 0;
2232 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2233 unsigned NumProtos = Record[Idx++];
2234 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2235 for (unsigned I = 0; I != NumProtos; ++I)
2236 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2237 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2238 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002239
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002240 case pch::TYPE_OBJC_QUALIFIED_ID: {
2241 unsigned Idx = 0;
2242 unsigned NumProtos = Record[Idx++];
2243 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2244 for (unsigned I = 0; I != NumProtos; ++I)
2245 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2246 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2247 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002248 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002249 // Suppress a GCC warning
2250 return QualType();
2251}
2252
2253/// \brief Note that we have loaded the declaration with the given
2254/// Index.
2255///
2256/// This routine notes that this declaration has already been loaded,
2257/// so that future GetDecl calls will return this declaration rather
2258/// than trying to load a new declaration.
2259inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2260 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2261 DeclAlreadyLoaded[Index] = true;
2262 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2263}
2264
2265/// \brief Read the declaration at the given offset from the PCH file.
2266Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002267 // Keep track of where we are in the stream, then jump back there
2268 // after reading this declaration.
2269 SavedStreamPosition SavedPosition(Stream);
2270
Douglas Gregor2cf26342009-04-09 22:27:44 +00002271 Decl *D = 0;
2272 Stream.JumpToBit(Offset);
2273 RecordData Record;
2274 unsigned Code = Stream.ReadCode();
2275 unsigned Idx = 0;
2276 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00002277
Douglas Gregor2cf26342009-04-09 22:27:44 +00002278 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002279 case pch::DECL_ATTR:
2280 case pch::DECL_CONTEXT_LEXICAL:
2281 case pch::DECL_CONTEXT_VISIBLE:
2282 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2283 break;
2284
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285 case pch::DECL_TRANSLATION_UNIT:
2286 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002288 break;
2289
2290 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002291 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 break;
2293 }
2294
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002295 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002296 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002297 break;
2298 }
2299
Douglas Gregor8c700062009-04-13 21:20:57 +00002300 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002301 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2302 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00002303 break;
2304 }
2305
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002306 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002307 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2308 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002309 break;
2310 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002311
2312 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002313 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2314 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002315 break;
2316 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002317
Steve Naroff53c9d8a2009-04-20 15:06:07 +00002318 case pch::DECL_OBJC_METHOD: {
2319 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2320 Selector(), QualType(), 0);
2321 break;
2322 }
2323
Steve Naroff30833f82009-04-21 15:12:33 +00002324 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002325 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2326 break;
2327 }
2328
Steve Naroff30833f82009-04-21 15:12:33 +00002329 case pch::DECL_OBJC_IVAR: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002330 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2331 ObjCIvarDecl::None);
2332 break;
2333 }
2334
Steve Naroff30833f82009-04-21 15:12:33 +00002335 case pch::DECL_OBJC_PROTOCOL: {
2336 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2337 break;
2338 }
2339
2340 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2341 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2342 QualType(), 0);
2343 break;
2344 }
2345
2346 case pch::DECL_OBJC_CLASS: {
2347 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2348 break;
2349 }
2350
2351 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2352 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2353 break;
2354 }
2355
2356 case pch::DECL_OBJC_CATEGORY: {
2357 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2358 break;
2359 }
2360
2361 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor10b0e1f2009-04-23 02:53:57 +00002362 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002363 break;
2364 }
2365
2366 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor8f36aba2009-04-23 03:23:08 +00002367 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002368 break;
2369 }
2370
2371 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregor17eee4a2009-04-23 03:51:49 +00002372 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002373 break;
2374 }
2375
2376 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor70e5a142009-04-22 23:20:34 +00002377 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff30833f82009-04-21 15:12:33 +00002378 break;
2379 }
2380
2381 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor8818c4f2009-04-23 03:43:53 +00002382 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2383 SourceLocation(), 0,
2384 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002385 break;
2386 }
2387
Douglas Gregor8c700062009-04-13 21:20:57 +00002388 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002389 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2390 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00002391 break;
2392 }
2393
Douglas Gregor2cf26342009-04-09 22:27:44 +00002394 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002395 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2396 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002397 break;
2398 }
2399
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002400 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002401 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2402 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002403 break;
2404 }
2405
2406 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002407 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002408 QualType(), QualType(), VarDecl::None,
2409 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002410 break;
2411 }
2412
Douglas Gregor1028bc62009-04-13 22:49:25 +00002413 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002414 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00002415 break;
2416 }
2417
2418 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002419 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00002420 break;
2421 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002422 }
2423
Douglas Gregor668c1a42009-04-21 22:25:48 +00002424 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002425 if (D) {
2426 LoadedDecl(Index, D);
2427 Reader.Visit(D);
2428 }
2429
Douglas Gregor2cf26342009-04-09 22:27:44 +00002430 // If this declaration is also a declaration context, get the
2431 // offsets for its tables of lexical and visible declarations.
2432 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2433 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2434 if (Offsets.first || Offsets.second) {
2435 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2436 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2437 DeclContextOffsets[DC] = Offsets;
2438 }
2439 }
2440 assert(Idx == Record.size());
2441
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002442 if (Consumer) {
2443 // If we have deserialized a declaration that has a definition the
2444 // AST consumer might need to know about, notify the consumer
2445 // about that definition now.
2446 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2447 if (Var->isFileVarDecl() && Var->getInit()) {
2448 DeclGroupRef DG(Var);
2449 Consumer->HandleTopLevelDecl(DG);
2450 }
2451 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2452 if (Func->isThisDeclarationADefinition()) {
2453 DeclGroupRef DG(Func);
2454 Consumer->HandleTopLevelDecl(DG);
2455 }
2456 }
2457 }
2458
Douglas Gregor2cf26342009-04-09 22:27:44 +00002459 return D;
2460}
2461
Douglas Gregor8038d512009-04-10 17:25:41 +00002462QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 unsigned Quals = ID & 0x07;
2464 unsigned Index = ID >> 3;
2465
2466 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2467 QualType T;
2468 switch ((pch::PredefinedTypeIDs)Index) {
2469 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2470 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2471 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2472
2473 case pch::PREDEF_TYPE_CHAR_U_ID:
2474 case pch::PREDEF_TYPE_CHAR_S_ID:
2475 // FIXME: Check that the signedness of CharTy is correct!
2476 T = Context.CharTy;
2477 break;
2478
2479 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2480 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2481 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2482 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2483 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2484 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2485 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2486 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2487 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2488 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2489 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2490 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2491 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2492 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2493 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2494 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2495 }
2496
2497 assert(!T.isNull() && "Unknown predefined type");
2498 return T.getQualifiedType(Quals);
2499 }
2500
2501 Index -= pch::NUM_PREDEF_TYPE_IDS;
2502 if (!TypeAlreadyLoaded[Index]) {
2503 // Load the type from the PCH file.
2504 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2505 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2506 TypeAlreadyLoaded[Index] = true;
2507 }
2508
2509 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2510}
2511
Douglas Gregor8038d512009-04-10 17:25:41 +00002512Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002513 if (ID == 0)
2514 return 0;
2515
2516 unsigned Index = ID - 1;
2517 if (DeclAlreadyLoaded[Index])
2518 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2519
2520 // Load the declaration from the PCH file.
2521 return ReadDeclRecord(DeclOffsets[Index], Index);
2522}
2523
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002524Stmt *PCHReader::GetStmt(uint64_t Offset) {
2525 // Keep track of where we are in the stream, then jump back there
2526 // after reading this declaration.
2527 SavedStreamPosition SavedPosition(Stream);
2528
2529 Stream.JumpToBit(Offset);
2530 return ReadStmt();
2531}
2532
Douglas Gregor2cf26342009-04-09 22:27:44 +00002533bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002534 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002535 assert(DC->hasExternalLexicalStorage() &&
2536 "DeclContext has no lexical decls in storage");
2537 uint64_t Offset = DeclContextOffsets[DC].first;
2538 assert(Offset && "DeclContext has no lexical decls in storage");
2539
Douglas Gregor0b748912009-04-14 21:18:50 +00002540 // Keep track of where we are in the stream, then jump back there
2541 // after reading this context.
2542 SavedStreamPosition SavedPosition(Stream);
2543
Douglas Gregor2cf26342009-04-09 22:27:44 +00002544 // Load the record containing all of the declarations lexically in
2545 // this context.
2546 Stream.JumpToBit(Offset);
2547 RecordData Record;
2548 unsigned Code = Stream.ReadCode();
2549 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002550 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2552
2553 // Load all of the declaration IDs
2554 Decls.clear();
2555 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002556 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002557 return false;
2558}
2559
2560bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2561 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2562 assert(DC->hasExternalVisibleStorage() &&
2563 "DeclContext has no visible decls in storage");
2564 uint64_t Offset = DeclContextOffsets[DC].second;
2565 assert(Offset && "DeclContext has no visible decls in storage");
2566
Douglas Gregor0b748912009-04-14 21:18:50 +00002567 // Keep track of where we are in the stream, then jump back there
2568 // after reading this context.
2569 SavedStreamPosition SavedPosition(Stream);
2570
Douglas Gregor2cf26342009-04-09 22:27:44 +00002571 // Load the record containing all of the declarations visible in
2572 // this context.
2573 Stream.JumpToBit(Offset);
2574 RecordData Record;
2575 unsigned Code = Stream.ReadCode();
2576 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002577 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002578 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2579 if (Record.size() == 0)
2580 return false;
2581
2582 Decls.clear();
2583
2584 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002585 while (Idx < Record.size()) {
2586 Decls.push_back(VisibleDeclaration());
2587 Decls.back().Name = ReadDeclarationName(Record, Idx);
2588
Douglas Gregor2cf26342009-04-09 22:27:44 +00002589 unsigned Size = Record[Idx++];
2590 llvm::SmallVector<unsigned, 4> & LoadedDecls
2591 = Decls.back().Declarations;
2592 LoadedDecls.reserve(Size);
2593 for (unsigned I = 0; I < Size; ++I)
2594 LoadedDecls.push_back(Record[Idx++]);
2595 }
2596
Douglas Gregor25123082009-04-22 22:34:57 +00002597 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002598 return false;
2599}
2600
Douglas Gregorfdd01722009-04-14 00:24:19 +00002601void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002602 this->Consumer = Consumer;
2603
Douglas Gregorfdd01722009-04-14 00:24:19 +00002604 if (!Consumer)
2605 return;
2606
2607 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2608 Decl *D = GetDecl(ExternalDefinitions[I]);
2609 DeclGroupRef DG(D);
2610 Consumer->HandleTopLevelDecl(DG);
2611 }
2612}
2613
Douglas Gregor2cf26342009-04-09 22:27:44 +00002614void PCHReader::PrintStats() {
2615 std::fprintf(stderr, "*** PCH Statistics:\n");
2616
2617 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2618 TypeAlreadyLoaded.end(),
2619 true);
2620 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2621 DeclAlreadyLoaded.end(),
2622 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002623 unsigned NumIdentifiersLoaded = 0;
2624 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2625 if ((IdentifierData[I] & 0x01) == 0)
2626 ++NumIdentifiersLoaded;
2627 }
2628
Douglas Gregor2cf26342009-04-09 22:27:44 +00002629 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2630 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002631 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002632 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2633 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002634 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2635 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2636 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2637 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002638 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2639 NumStatementsRead, TotalNumStatements,
2640 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor37e26842009-04-21 23:56:24 +00002641 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2642 NumMacrosRead, TotalNumMacros,
2643 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregor25123082009-04-22 22:34:57 +00002644 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2645 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2646 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2647 * 100));
2648 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2649 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2650 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2651 * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002652 std::fprintf(stderr, "\n");
2653}
2654
Douglas Gregor668c1a42009-04-21 22:25:48 +00002655void PCHReader::InitializeSema(Sema &S) {
2656 SemaObj = &S;
2657
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002658 // Makes sure any declarations that were deserialized "too early"
2659 // still get added to the identifier's declaration chains.
2660 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2661 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2662 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002663 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002664 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002665
2666 // If there were any tentative definitions, deserialize them and add
2667 // them to Sema's table of tentative definitions.
2668 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2669 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2670 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2671 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002672
2673 // If there were any locally-scoped external declarations,
2674 // deserialize them and add them to Sema's table of locally-scoped
2675 // external declarations.
2676 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2677 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2678 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2679 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002680}
2681
2682IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2683 // Try to find this name within our on-disk hash table
2684 PCHIdentifierLookupTable *IdTable
2685 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2686 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2687 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2688 if (Pos == IdTable->end())
2689 return 0;
2690
2691 // Dereferencing the iterator has the effect of building the
2692 // IdentifierInfo node and populating it with the various
2693 // declarations it needs.
2694 return *Pos;
2695}
2696
2697void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2698 assert(ID && "Non-zero identifier ID required");
2699 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2700}
2701
Chris Lattner7356a312009-04-11 21:15:38 +00002702IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002703 if (ID == 0)
2704 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002705
Douglas Gregor668c1a42009-04-21 22:25:48 +00002706 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002707 Error("No identifier table in PCH file");
2708 return 0;
2709 }
Chris Lattner7356a312009-04-11 21:15:38 +00002710
Douglas Gregorafaf3082009-04-11 00:14:32 +00002711 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002712 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002713 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregor668c1a42009-04-21 22:25:48 +00002714 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002715 }
Chris Lattner7356a312009-04-11 21:15:38 +00002716
2717 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002718}
2719
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002720Selector PCHReader::DecodeSelector(unsigned ID) {
2721 if (ID == 0)
2722 return Selector();
2723
2724 if (SelectorData.empty()) {
2725 Error("No selector table in PCH file");
2726 return Selector();
2727 }
2728
2729 if (ID > SelectorData.size()) {
2730 Error("Selector ID out of range");
2731 return Selector();
2732 }
2733 return SelectorData[ID-1];
2734}
2735
Douglas Gregor2cf26342009-04-09 22:27:44 +00002736DeclarationName
2737PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2738 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2739 switch (Kind) {
2740 case DeclarationName::Identifier:
2741 return DeclarationName(GetIdentifierInfo(Record, Idx));
2742
2743 case DeclarationName::ObjCZeroArgSelector:
2744 case DeclarationName::ObjCOneArgSelector:
2745 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00002746 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002747
2748 case DeclarationName::CXXConstructorName:
2749 return Context.DeclarationNames.getCXXConstructorName(
2750 GetType(Record[Idx++]));
2751
2752 case DeclarationName::CXXDestructorName:
2753 return Context.DeclarationNames.getCXXDestructorName(
2754 GetType(Record[Idx++]));
2755
2756 case DeclarationName::CXXConversionFunctionName:
2757 return Context.DeclarationNames.getCXXConversionFunctionName(
2758 GetType(Record[Idx++]));
2759
2760 case DeclarationName::CXXOperatorName:
2761 return Context.DeclarationNames.getCXXOperatorName(
2762 (OverloadedOperatorKind)Record[Idx++]);
2763
2764 case DeclarationName::CXXUsingDirective:
2765 return DeclarationName::getUsingDirectiveName();
2766 }
2767
2768 // Required to silence GCC warning
2769 return DeclarationName();
2770}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002771
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002772/// \brief Read an integral value
2773llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2774 unsigned BitWidth = Record[Idx++];
2775 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2776 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2777 Idx += NumWords;
2778 return Result;
2779}
2780
2781/// \brief Read a signed integral value
2782llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2783 bool isUnsigned = Record[Idx++];
2784 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2785}
2786
Douglas Gregor17fc2232009-04-14 21:55:33 +00002787/// \brief Read a floating-point value
2788llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002789 return llvm::APFloat(ReadAPInt(Record, Idx));
2790}
2791
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002792// \brief Read a string
2793std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2794 unsigned Len = Record[Idx++];
2795 std::string Result(&Record[Idx], &Record[Idx] + Len);
2796 Idx += Len;
2797 return Result;
2798}
2799
2800/// \brief Reads attributes from the current stream position.
2801Attr *PCHReader::ReadAttributes() {
2802 unsigned Code = Stream.ReadCode();
2803 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2804 "Expected unabbreviated record"); (void)Code;
2805
2806 RecordData Record;
2807 unsigned Idx = 0;
2808 unsigned RecCode = Stream.ReadRecord(Code, Record);
2809 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2810 (void)RecCode;
2811
2812#define SIMPLE_ATTR(Name) \
2813 case Attr::Name: \
2814 New = ::new (Context) Name##Attr(); \
2815 break
2816
2817#define STRING_ATTR(Name) \
2818 case Attr::Name: \
2819 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2820 break
2821
2822#define UNSIGNED_ATTR(Name) \
2823 case Attr::Name: \
2824 New = ::new (Context) Name##Attr(Record[Idx++]); \
2825 break
2826
2827 Attr *Attrs = 0;
2828 while (Idx < Record.size()) {
2829 Attr *New = 0;
2830 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2831 bool IsInherited = Record[Idx++];
2832
2833 switch (Kind) {
2834 STRING_ATTR(Alias);
2835 UNSIGNED_ATTR(Aligned);
2836 SIMPLE_ATTR(AlwaysInline);
2837 SIMPLE_ATTR(AnalyzerNoReturn);
2838 STRING_ATTR(Annotate);
2839 STRING_ATTR(AsmLabel);
2840
2841 case Attr::Blocks:
2842 New = ::new (Context) BlocksAttr(
2843 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2844 break;
2845
2846 case Attr::Cleanup:
2847 New = ::new (Context) CleanupAttr(
2848 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2849 break;
2850
2851 SIMPLE_ATTR(Const);
2852 UNSIGNED_ATTR(Constructor);
2853 SIMPLE_ATTR(DLLExport);
2854 SIMPLE_ATTR(DLLImport);
2855 SIMPLE_ATTR(Deprecated);
2856 UNSIGNED_ATTR(Destructor);
2857 SIMPLE_ATTR(FastCall);
2858
2859 case Attr::Format: {
2860 std::string Type = ReadString(Record, Idx);
2861 unsigned FormatIdx = Record[Idx++];
2862 unsigned FirstArg = Record[Idx++];
2863 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2864 break;
2865 }
2866
Chris Lattnercf2a7212009-04-20 19:12:28 +00002867 SIMPLE_ATTR(GNUInline);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002868
2869 case Attr::IBOutletKind:
2870 New = ::new (Context) IBOutletAttr();
2871 break;
2872
2873 SIMPLE_ATTR(NoReturn);
2874 SIMPLE_ATTR(NoThrow);
2875 SIMPLE_ATTR(Nodebug);
2876 SIMPLE_ATTR(Noinline);
2877
2878 case Attr::NonNull: {
2879 unsigned Size = Record[Idx++];
2880 llvm::SmallVector<unsigned, 16> ArgNums;
2881 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2882 Idx += Size;
2883 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2884 break;
2885 }
2886
2887 SIMPLE_ATTR(ObjCException);
2888 SIMPLE_ATTR(ObjCNSObject);
2889 SIMPLE_ATTR(Overloadable);
2890 UNSIGNED_ATTR(Packed);
2891 SIMPLE_ATTR(Pure);
2892 UNSIGNED_ATTR(Regparm);
2893 STRING_ATTR(Section);
2894 SIMPLE_ATTR(StdCall);
2895 SIMPLE_ATTR(TransparentUnion);
2896 SIMPLE_ATTR(Unavailable);
2897 SIMPLE_ATTR(Unused);
2898 SIMPLE_ATTR(Used);
2899
2900 case Attr::Visibility:
2901 New = ::new (Context) VisibilityAttr(
2902 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2903 break;
2904
2905 SIMPLE_ATTR(WarnUnusedResult);
2906 SIMPLE_ATTR(Weak);
2907 SIMPLE_ATTR(WeakImport);
2908 }
2909
2910 assert(New && "Unable to decode attribute?");
2911 New->setInherited(IsInherited);
2912 New->setNext(Attrs);
2913 Attrs = New;
2914 }
2915#undef UNSIGNED_ATTR
2916#undef STRING_ATTR
2917#undef SIMPLE_ATTR
2918
2919 // The list of attributes was built backwards. Reverse the list
2920 // before returning it.
2921 Attr *PrevAttr = 0, *NextAttr = 0;
2922 while (Attrs) {
2923 NextAttr = Attrs->getNext();
2924 Attrs->setNext(PrevAttr);
2925 PrevAttr = Attrs;
2926 Attrs = NextAttr;
2927 }
2928
2929 return PrevAttr;
2930}
2931
Douglas Gregorc9490c02009-04-16 22:23:12 +00002932Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002933 // Within the bitstream, expressions are stored in Reverse Polish
2934 // Notation, with each of the subexpressions preceding the
2935 // expression they are stored in. To evaluate expressions, we
2936 // continue reading expressions and placing them on the stack, with
2937 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002938 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002939 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002940 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002941 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002942 llvm::SmallVector<Stmt *, 16> StmtStack;
2943 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002944 Stmt::EmptyShell Empty;
2945
Douglas Gregor087fd532009-04-14 23:32:43 +00002946 while (true) {
2947 unsigned Code = Stream.ReadCode();
2948 if (Code == llvm::bitc::END_BLOCK) {
2949 if (Stream.ReadBlockEnd()) {
2950 Error("Error at end of Source Manager block");
2951 return 0;
2952 }
2953 break;
2954 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002955
Douglas Gregor087fd532009-04-14 23:32:43 +00002956 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2957 // No known subblocks, always skip them.
2958 Stream.ReadSubBlockID();
2959 if (Stream.SkipBlock()) {
2960 Error("Malformed block record");
2961 return 0;
2962 }
2963 continue;
2964 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002965
Douglas Gregor087fd532009-04-14 23:32:43 +00002966 if (Code == llvm::bitc::DEFINE_ABBREV) {
2967 Stream.ReadAbbrevRecord();
2968 continue;
2969 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002970
Douglas Gregorc9490c02009-04-16 22:23:12 +00002971 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002972 Idx = 0;
2973 Record.clear();
2974 bool Finished = false;
2975 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002976 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002977 Finished = true;
2978 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002979
Douglas Gregorc9490c02009-04-16 22:23:12 +00002980 case pch::STMT_NULL_PTR:
2981 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002982 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002983
Douglas Gregor025452f2009-04-17 00:04:06 +00002984 case pch::STMT_NULL:
2985 S = new (Context) NullStmt(Empty);
2986 break;
2987
2988 case pch::STMT_COMPOUND:
2989 S = new (Context) CompoundStmt(Empty);
2990 break;
2991
2992 case pch::STMT_CASE:
2993 S = new (Context) CaseStmt(Empty);
2994 break;
2995
2996 case pch::STMT_DEFAULT:
2997 S = new (Context) DefaultStmt(Empty);
2998 break;
2999
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003000 case pch::STMT_LABEL:
3001 S = new (Context) LabelStmt(Empty);
3002 break;
3003
Douglas Gregor025452f2009-04-17 00:04:06 +00003004 case pch::STMT_IF:
3005 S = new (Context) IfStmt(Empty);
3006 break;
3007
3008 case pch::STMT_SWITCH:
3009 S = new (Context) SwitchStmt(Empty);
3010 break;
3011
Douglas Gregord921cf92009-04-17 00:16:09 +00003012 case pch::STMT_WHILE:
3013 S = new (Context) WhileStmt(Empty);
3014 break;
3015
Douglas Gregor67d82492009-04-17 00:29:51 +00003016 case pch::STMT_DO:
3017 S = new (Context) DoStmt(Empty);
3018 break;
3019
3020 case pch::STMT_FOR:
3021 S = new (Context) ForStmt(Empty);
3022 break;
3023
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003024 case pch::STMT_GOTO:
3025 S = new (Context) GotoStmt(Empty);
3026 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003027
3028 case pch::STMT_INDIRECT_GOTO:
3029 S = new (Context) IndirectGotoStmt(Empty);
3030 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003031
Douglas Gregord921cf92009-04-17 00:16:09 +00003032 case pch::STMT_CONTINUE:
3033 S = new (Context) ContinueStmt(Empty);
3034 break;
3035
Douglas Gregor025452f2009-04-17 00:04:06 +00003036 case pch::STMT_BREAK:
3037 S = new (Context) BreakStmt(Empty);
3038 break;
3039
Douglas Gregor0de9d882009-04-17 16:34:57 +00003040 case pch::STMT_RETURN:
3041 S = new (Context) ReturnStmt(Empty);
3042 break;
3043
Douglas Gregor84f21702009-04-17 16:55:36 +00003044 case pch::STMT_DECL:
3045 S = new (Context) DeclStmt(Empty);
3046 break;
3047
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00003048 case pch::STMT_ASM:
3049 S = new (Context) AsmStmt(Empty);
3050 break;
3051
Douglas Gregor087fd532009-04-14 23:32:43 +00003052 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003053 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003054 break;
3055
3056 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003057 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003058 break;
3059
3060 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003061 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003062 break;
3063
3064 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003065 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003066 break;
3067
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003068 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003069 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003070 break;
3071
Douglas Gregor673ecd62009-04-15 16:35:07 +00003072 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003073 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00003074 Record[PCHStmtReader::NumExprFields + 1]);
3075 break;
3076
Douglas Gregor087fd532009-04-14 23:32:43 +00003077 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003078 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003079 break;
3080
Douglas Gregorc04db4f2009-04-14 23:59:37 +00003081 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003082 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00003083 break;
3084
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003085 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003086 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003087 break;
3088
3089 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003090 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003091 break;
3092
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003093 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003094 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003095 break;
3096
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003097 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003098 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003099 break;
3100
3101 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003102 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003103 break;
3104
Douglas Gregordb600c32009-04-15 00:25:59 +00003105 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003106 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00003107 break;
3108
Douglas Gregorad90e962009-04-15 22:40:36 +00003109 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003110 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00003111 break;
3112
3113 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003114 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00003115 break;
3116
Douglas Gregor087fd532009-04-14 23:32:43 +00003117 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003118 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003119 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00003120
3121 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003122 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00003123 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00003124
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003125 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003126 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003127 break;
3128
Douglas Gregord3c98a02009-04-15 23:02:49 +00003129 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003130 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003131 break;
3132
Douglas Gregord077d752009-04-16 00:55:48 +00003133 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003134 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003135 break;
3136
3137 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003138 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00003139 Record[PCHStmtReader::NumExprFields] - 1);
3140
3141 break;
3142
3143 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003144 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003145 break;
3146
Douglas Gregord3c98a02009-04-15 23:02:49 +00003147 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003148 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003149 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003150
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003151 case pch::EXPR_ADDR_LABEL:
3152 S = new (Context) AddrLabelExpr(Empty);
3153 break;
3154
Douglas Gregor6a2dd552009-04-17 19:05:30 +00003155 case pch::EXPR_STMT:
3156 S = new (Context) StmtExpr(Empty);
3157 break;
3158
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003159 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003160 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003161 break;
3162
3163 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003164 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003165 break;
3166
3167 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003168 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003169 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003170
3171 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003172 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003173 break;
3174
Douglas Gregor84af7c22009-04-17 19:21:43 +00003175 case pch::EXPR_BLOCK:
3176 S = new (Context) BlockExpr(Empty);
3177 break;
3178
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003179 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003180 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003181 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003182
Chris Lattner3a57a372009-04-22 06:29:42 +00003183 case pch::EXPR_OBJC_STRING_LITERAL:
3184 S = new (Context) ObjCStringLiteral(Empty);
3185 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003186 case pch::EXPR_OBJC_ENCODE:
3187 S = new (Context) ObjCEncodeExpr(Empty);
3188 break;
Chris Lattner3a57a372009-04-22 06:29:42 +00003189 case pch::EXPR_OBJC_SELECTOR_EXPR:
3190 S = new (Context) ObjCSelectorExpr(Empty);
3191 break;
3192 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3193 S = new (Context) ObjCProtocolExpr(Empty);
3194 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00003195 }
3196
Douglas Gregorc9490c02009-04-16 22:23:12 +00003197 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00003198 if (Finished)
3199 break;
3200
Douglas Gregor3e1af842009-04-17 22:13:46 +00003201 ++NumStatementsRead;
3202
Douglas Gregorc9490c02009-04-16 22:23:12 +00003203 if (S) {
3204 unsigned NumSubStmts = Reader.Visit(S);
3205 while (NumSubStmts > 0) {
3206 StmtStack.pop_back();
3207 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00003208 }
3209 }
3210
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003211 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00003212 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00003213 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00003214 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00003215 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00003216 return StmtStack.back();
3217}
3218
3219Expr *PCHReader::ReadExpr() {
3220 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00003221}
3222
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003223DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003224 return Diag(SourceLocation(), DiagID);
3225}
3226
3227DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3228 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003229 Context.getSourceManager()),
3230 DiagID);
3231}
Douglas Gregor025452f2009-04-17 00:04:06 +00003232
Douglas Gregor668c1a42009-04-21 22:25:48 +00003233/// \brief Retrieve the identifier table associated with the
3234/// preprocessor.
3235IdentifierTable &PCHReader::getIdentifierTable() {
3236 return PP.getIdentifierTable();
3237}
3238
Douglas Gregor025452f2009-04-17 00:04:06 +00003239/// \brief Record that the given ID maps to the given switch-case
3240/// statement.
3241void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3242 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3243 SwitchCaseStmts[ID] = SC;
3244}
3245
3246/// \brief Retrieve the switch-case statement with the given ID.
3247SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3248 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3249 return SwitchCaseStmts[ID];
3250}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003251
3252/// \brief Record that the given label statement has been
3253/// deserialized and has the given ID.
3254void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3255 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3256 "Deserialized label twice");
3257 LabelStmts[ID] = S;
3258
3259 // If we've already seen any goto statements that point to this
3260 // label, resolve them now.
3261 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3262 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3263 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3264 Goto->second->setLabel(S);
3265 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003266
3267 // If we've already seen any address-label statements that point to
3268 // this label, resolve them now.
3269 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3270 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3271 = UnresolvedAddrLabelExprs.equal_range(ID);
3272 for (AddrLabelIter AddrLabel = AddrLabels.first;
3273 AddrLabel != AddrLabels.second; ++AddrLabel)
3274 AddrLabel->second->setLabel(S);
3275 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003276}
3277
3278/// \brief Set the label of the given statement to the label
3279/// identified by ID.
3280///
3281/// Depending on the order in which the label and other statements
3282/// referencing that label occur, this operation may complete
3283/// immediately (updating the statement) or it may queue the
3284/// statement to be back-patched later.
3285void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3286 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3287 if (Label != LabelStmts.end()) {
3288 // We've already seen this label, so set the label of the goto and
3289 // we're done.
3290 S->setLabel(Label->second);
3291 } else {
3292 // We haven't seen this label yet, so add this goto to the set of
3293 // unresolved goto statements.
3294 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3295 }
3296}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003297
3298/// \brief Set the label of the given expression to the label
3299/// identified by ID.
3300///
3301/// Depending on the order in which the label and other statements
3302/// referencing that label occur, this operation may complete
3303/// immediately (updating the statement) or it may queue the
3304/// statement to be back-patched later.
3305void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3306 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3307 if (Label != LabelStmts.end()) {
3308 // We've already seen this label, so set the label of the
3309 // label-address expression and we're done.
3310 S->setLabel(Label->second);
3311 } else {
3312 // We haven't seen this label yet, so add this label-address
3313 // expression to the set of unresolved label-address expressions.
3314 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3315 }
3316}