blob: 0502a876a3a98d851eac56ee1425ab3b00987481 [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++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff33feeb02009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor291be392009-04-23 03:59:07 +0000230 unsigned NumProtocols = Record[Idx++];
231 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
232 Protocols.reserve(NumProtocols);
233 for (unsigned I = 0; I != NumProtocols; ++I)
234 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff33feeb02009-04-20 20:09:33 +0000235 unsigned NumIvars = Record[Idx++];
236 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
237 IVars.reserve(NumIvars);
238 for (unsigned I = 0; I != NumIvars; ++I)
239 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
240 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
241
242 ID->setForwardDecl(Record[Idx++]);
243 ID->setImplicitInterfaceDecl(Record[Idx++]);
244 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
245 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000246 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor291be392009-04-23 03:59:07 +0000247 // FIXME: add categories.
Steve Naroff33feeb02009-04-20 20:09:33 +0000248}
249
250void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
251 VisitFieldDecl(IVD);
252 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
253}
254
Steve Naroff30833f82009-04-21 15:12:33 +0000255void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
256 VisitObjCContainerDecl(PD);
257 PD->setForwardDecl(Record[Idx++]);
258 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
259 unsigned NumProtoRefs = Record[Idx++];
260 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
261 ProtoRefs.reserve(NumProtoRefs);
262 for (unsigned I = 0; I != NumProtoRefs; ++I)
263 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
264 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
265}
266
267void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
268 VisitFieldDecl(FD);
269}
270
271void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
272 VisitDecl(CD);
273 unsigned NumClassRefs = Record[Idx++];
274 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
275 ClassRefs.reserve(NumClassRefs);
276 for (unsigned I = 0; I != NumClassRefs; ++I)
277 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
278 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
279}
280
281void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
282 VisitDecl(FPD);
283 unsigned NumProtoRefs = Record[Idx++];
284 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
285 ProtoRefs.reserve(NumProtoRefs);
286 for (unsigned I = 0; I != NumProtoRefs; ++I)
287 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
288 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
289}
290
291void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
292 VisitObjCContainerDecl(CD);
293 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
294 unsigned NumProtoRefs = Record[Idx++];
295 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
296 ProtoRefs.reserve(NumProtoRefs);
297 for (unsigned I = 0; I != NumProtoRefs; ++I)
298 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
299 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
300 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
301 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
302}
303
304void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
305 VisitNamedDecl(CAD);
306 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
307}
308
309void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
310 VisitNamedDecl(D);
Douglas Gregor70e5a142009-04-22 23:20:34 +0000311 D->setType(Reader.GetType(Record[Idx++]));
312 // FIXME: stable encoding
313 D->setPropertyAttributes(
314 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
315 // FIXME: stable encoding
316 D->setPropertyImplementation(
317 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
318 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
319 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
320 D->setGetterMethodDecl(
321 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
322 D->setSetterMethodDecl(
323 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
324 D->setPropertyIvarDecl(
325 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000326}
327
328void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
329 VisitDecl(D);
Douglas Gregor2c2d43c2009-04-23 02:42:49 +0000330 D->setClassInterface(
331 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
332 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff30833f82009-04-21 15:12:33 +0000333}
334
335void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
336 VisitObjCImplDecl(D);
Douglas Gregor10b0e1f2009-04-23 02:53:57 +0000337 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff30833f82009-04-21 15:12:33 +0000338}
339
340void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
341 VisitObjCImplDecl(D);
Douglas Gregor8f36aba2009-04-23 03:23:08 +0000342 D->setSuperClass(
343 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000344}
345
346
347void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
348 VisitDecl(D);
Douglas Gregor8818c4f2009-04-23 03:43:53 +0000349 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
350 D->setPropertyDecl(
351 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
352 D->setPropertyIvarDecl(
353 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000354}
355
Douglas Gregor8c700062009-04-13 21:20:57 +0000356void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
357 VisitValueDecl(FD);
358 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000359 if (Record[Idx++])
360 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000361}
362
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
364 VisitValueDecl(VD);
365 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
366 VD->setThreadSpecified(Record[Idx++]);
367 VD->setCXXDirectInitializer(Record[Idx++]);
368 VD->setDeclaredInCondition(Record[Idx++]);
369 VD->setPreviousDeclaration(
370 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
371 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000372 if (Record[Idx++])
373 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000376void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
377 VisitVarDecl(PD);
378 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000379 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000380}
381
382void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
383 VisitParmVarDecl(PD);
384 PD->setOriginalType(Reader.GetType(Record[Idx++]));
385}
386
Douglas Gregor1028bc62009-04-13 22:49:25 +0000387void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
388 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000389 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000390}
391
392void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
393 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000394 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000395 unsigned NumParams = Record[Idx++];
396 llvm::SmallVector<ParmVarDecl *, 16> Params;
397 Params.reserve(NumParams);
398 for (unsigned I = 0; I != NumParams; ++I)
399 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
400 BD->setParams(Reader.getContext(), &Params[0], NumParams);
401}
402
Douglas Gregor2cf26342009-04-09 22:27:44 +0000403std::pair<uint64_t, uint64_t>
404PCHDeclReader::VisitDeclContext(DeclContext *DC) {
405 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000406 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000407 return std::make_pair(LexicalOffset, VisibleOffset);
408}
409
Douglas Gregor0b748912009-04-14 21:18:50 +0000410//===----------------------------------------------------------------------===//
411// Statement/expression deserialization
412//===----------------------------------------------------------------------===//
413namespace {
414 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000415 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000416 PCHReader &Reader;
417 const PCHReader::RecordData &Record;
418 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000419 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000420
421 public:
422 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000423 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
424 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000425
Douglas Gregor025452f2009-04-17 00:04:06 +0000426 /// \brief The number of record fields required for the Stmt class
427 /// itself.
428 static const unsigned NumStmtFields = 0;
429
Douglas Gregor673ecd62009-04-15 16:35:07 +0000430 /// \brief The number of record fields required for the Expr class
431 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000432 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000433
Douglas Gregor087fd532009-04-14 23:32:43 +0000434 // Each of the Visit* functions reads in part of the expression
435 // from the given record and the current expression stack, then
436 // return the total number of operands that it read from the
437 // expression stack.
438
Douglas Gregor025452f2009-04-17 00:04:06 +0000439 unsigned VisitStmt(Stmt *S);
440 unsigned VisitNullStmt(NullStmt *S);
441 unsigned VisitCompoundStmt(CompoundStmt *S);
442 unsigned VisitSwitchCase(SwitchCase *S);
443 unsigned VisitCaseStmt(CaseStmt *S);
444 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000445 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000446 unsigned VisitIfStmt(IfStmt *S);
447 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000448 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000449 unsigned VisitDoStmt(DoStmt *S);
450 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000451 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000452 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000453 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000454 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000455 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000456 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000457 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000458 unsigned VisitExpr(Expr *E);
459 unsigned VisitPredefinedExpr(PredefinedExpr *E);
460 unsigned VisitDeclRefExpr(DeclRefExpr *E);
461 unsigned VisitIntegerLiteral(IntegerLiteral *E);
462 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000463 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000464 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000465 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000466 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000467 unsigned VisitUnaryOperator(UnaryOperator *E);
468 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000469 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000470 unsigned VisitCallExpr(CallExpr *E);
471 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000472 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000473 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000474 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
475 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000476 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000477 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
478 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000479 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000480 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000481 unsigned VisitInitListExpr(InitListExpr *E);
482 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
483 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000484 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000485 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000486 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000487 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
488 unsigned VisitChooseExpr(ChooseExpr *E);
489 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000490 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000491 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000492 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000493 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000494 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000495 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
496 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000497 };
498}
499
Douglas Gregor025452f2009-04-17 00:04:06 +0000500unsigned PCHStmtReader::VisitStmt(Stmt *S) {
501 assert(Idx == NumStmtFields && "Incorrect statement field count");
502 return 0;
503}
504
505unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
506 VisitStmt(S);
507 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 return 0;
509}
510
511unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
512 VisitStmt(S);
513 unsigned NumStmts = Record[Idx++];
514 S->setStmts(Reader.getContext(),
515 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
516 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
517 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
518 return NumStmts;
519}
520
521unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
522 VisitStmt(S);
523 Reader.RecordSwitchCaseID(S, Record[Idx++]);
524 return 0;
525}
526
527unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
528 VisitSwitchCase(S);
529 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
530 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
531 S->setSubStmt(StmtStack.back());
532 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
533 return 3;
534}
535
536unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
537 VisitSwitchCase(S);
538 S->setSubStmt(StmtStack.back());
539 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
540 return 1;
541}
542
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000543unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
544 VisitStmt(S);
545 S->setID(Reader.GetIdentifierInfo(Record, Idx));
546 S->setSubStmt(StmtStack.back());
547 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 Reader.RecordLabelStmt(S, Record[Idx++]);
549 return 1;
550}
551
Douglas Gregor025452f2009-04-17 00:04:06 +0000552unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
553 VisitStmt(S);
554 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
555 S->setThen(StmtStack[StmtStack.size() - 2]);
556 S->setElse(StmtStack[StmtStack.size() - 1]);
557 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
558 return 3;
559}
560
561unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
562 VisitStmt(S);
563 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
564 S->setBody(StmtStack.back());
565 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 SwitchCase *PrevSC = 0;
567 for (unsigned N = Record.size(); Idx != N; ++Idx) {
568 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
569 if (PrevSC)
570 PrevSC->setNextSwitchCase(SC);
571 else
572 S->setSwitchCaseList(SC);
573 PrevSC = SC;
574 }
575 return 2;
576}
577
Douglas Gregord921cf92009-04-17 00:16:09 +0000578unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
579 VisitStmt(S);
580 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
581 S->setBody(StmtStack.back());
582 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
583 return 2;
584}
585
Douglas Gregor67d82492009-04-17 00:29:51 +0000586unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
587 VisitStmt(S);
588 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
589 S->setBody(StmtStack.back());
590 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
591 return 2;
592}
593
594unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
595 VisitStmt(S);
596 S->setInit(StmtStack[StmtStack.size() - 4]);
597 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
598 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
599 S->setBody(StmtStack.back());
600 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
601 return 4;
602}
603
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000604unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
605 VisitStmt(S);
606 Reader.SetLabelOf(S, Record[Idx++]);
607 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
608 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
609 return 0;
610}
611
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000612unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
613 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000614 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000615 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
616 return 1;
617}
618
Douglas Gregord921cf92009-04-17 00:16:09 +0000619unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
620 VisitStmt(S);
621 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
622 return 0;
623}
624
Douglas Gregor025452f2009-04-17 00:04:06 +0000625unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
626 VisitStmt(S);
627 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
628 return 0;
629}
630
Douglas Gregor0de9d882009-04-17 16:34:57 +0000631unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
632 VisitStmt(S);
633 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
634 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
635 return 1;
636}
637
Douglas Gregor84f21702009-04-17 16:55:36 +0000638unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
639 VisitStmt(S);
640 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
641 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
642
643 if (Idx + 1 == Record.size()) {
644 // Single declaration
645 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
646 } else {
647 llvm::SmallVector<Decl *, 16> Decls;
648 Decls.reserve(Record.size() - Idx);
649 for (unsigned N = Record.size(); Idx != N; ++Idx)
650 Decls.push_back(Reader.GetDecl(Record[Idx]));
651 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
652 &Decls[0], Decls.size())));
653 }
654 return 0;
655}
656
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000657unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
658 VisitStmt(S);
659 unsigned NumOutputs = Record[Idx++];
660 unsigned NumInputs = Record[Idx++];
661 unsigned NumClobbers = Record[Idx++];
662 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
663 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
664 S->setVolatile(Record[Idx++]);
665 S->setSimple(Record[Idx++]);
666
667 unsigned StackIdx
668 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
669 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
670
671 // Outputs and inputs
672 llvm::SmallVector<std::string, 16> Names;
673 llvm::SmallVector<StringLiteral*, 16> Constraints;
674 llvm::SmallVector<Stmt*, 16> Exprs;
675 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
676 Names.push_back(Reader.ReadString(Record, Idx));
677 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
678 Exprs.push_back(StmtStack[StackIdx++]);
679 }
680 S->setOutputsAndInputs(NumOutputs, NumInputs,
681 &Names[0], &Constraints[0], &Exprs[0]);
682
683 // Constraints
684 llvm::SmallVector<StringLiteral*, 16> Clobbers;
685 for (unsigned I = 0; I != NumClobbers; ++I)
686 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
687 S->setClobbers(&Clobbers[0], NumClobbers);
688
689 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
690 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
691}
692
Douglas Gregor087fd532009-04-14 23:32:43 +0000693unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000694 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000695 E->setType(Reader.GetType(Record[Idx++]));
696 E->setTypeDependent(Record[Idx++]);
697 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000698 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000699 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000700}
701
Douglas Gregor087fd532009-04-14 23:32:43 +0000702unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000703 VisitExpr(E);
704 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
705 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000706 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000707}
708
Douglas Gregor087fd532009-04-14 23:32:43 +0000709unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000710 VisitExpr(E);
711 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
712 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000713 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000714}
715
Douglas Gregor087fd532009-04-14 23:32:43 +0000716unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000717 VisitExpr(E);
718 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
719 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000720 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000721}
722
Douglas Gregor087fd532009-04-14 23:32:43 +0000723unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000724 VisitExpr(E);
725 E->setValue(Reader.ReadAPFloat(Record, Idx));
726 E->setExact(Record[Idx++]);
727 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000728 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000729}
730
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000731unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
732 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000733 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000734 return 1;
735}
736
Douglas Gregor673ecd62009-04-15 16:35:07 +0000737unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
738 VisitExpr(E);
739 unsigned Len = Record[Idx++];
740 assert(Record[Idx] == E->getNumConcatenated() &&
741 "Wrong number of concatenated tokens!");
742 ++Idx;
743 E->setWide(Record[Idx++]);
744
745 // Read string data
746 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
747 E->setStrData(Reader.getContext(), &Str[0], Len);
748 Idx += Len;
749
750 // Read source locations
751 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
752 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
753
754 return 0;
755}
756
Douglas Gregor087fd532009-04-14 23:32:43 +0000757unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000758 VisitExpr(E);
759 E->setValue(Record[Idx++]);
760 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
761 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000762 return 0;
763}
764
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000765unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
766 VisitExpr(E);
767 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000769 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000770 return 1;
771}
772
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000773unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
774 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000775 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000776 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
777 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
778 return 1;
779}
780
781unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
782 VisitExpr(E);
783 E->setSizeof(Record[Idx++]);
784 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000785 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000786 ++Idx;
787 } else {
788 E->setArgument(Reader.GetType(Record[Idx++]));
789 }
790 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
791 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
792 return E->isArgumentType()? 0 : 1;
793}
794
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000795unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
796 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000797 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
798 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000799 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
800 return 2;
801}
802
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000803unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
804 VisitExpr(E);
805 E->setNumArgs(Reader.getContext(), Record[Idx++]);
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000807 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000808 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000809 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000810 return E->getNumArgs() + 1;
811}
812
813unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
814 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000815 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000816 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
817 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
818 E->setArrow(Record[Idx++]);
819 return 1;
820}
821
Douglas Gregor087fd532009-04-14 23:32:43 +0000822unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
823 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000824 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000825 return 1;
826}
827
Douglas Gregordb600c32009-04-15 00:25:59 +0000828unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
829 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000830 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
831 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000832 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
833 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
834 return 2;
835}
836
Douglas Gregorad90e962009-04-15 22:40:36 +0000837unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
838 VisitBinaryOperator(E);
839 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
840 E->setComputationResultType(Reader.GetType(Record[Idx++]));
841 return 2;
842}
843
844unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
845 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000846 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
847 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
848 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000849 return 3;
850}
851
Douglas Gregor087fd532009-04-14 23:32:43 +0000852unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
853 VisitCastExpr(E);
854 E->setLvalueCast(Record[Idx++]);
855 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000856}
857
Douglas Gregordb600c32009-04-15 00:25:59 +0000858unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
859 VisitCastExpr(E);
860 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
861 return 1;
862}
863
864unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
865 VisitExplicitCastExpr(E);
866 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
867 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
868 return 1;
869}
870
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000871unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
872 VisitExpr(E);
873 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000874 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000875 E->setFileScope(Record[Idx++]);
876 return 1;
877}
878
Douglas Gregord3c98a02009-04-15 23:02:49 +0000879unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
880 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000881 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000882 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
883 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
884 return 1;
885}
886
Douglas Gregord077d752009-04-16 00:55:48 +0000887unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
888 VisitExpr(E);
889 unsigned NumInits = Record[Idx++];
890 E->reserveInits(NumInits);
891 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000892 E->updateInit(I,
893 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
894 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000895 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
896 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
897 E->setInitializedFieldInUnion(
898 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
899 E->sawArrayRangeDesignator(Record[Idx++]);
900 return NumInits + 1;
901}
902
903unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
904 typedef DesignatedInitExpr::Designator Designator;
905
906 VisitExpr(E);
907 unsigned NumSubExprs = Record[Idx++];
908 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
909 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000910 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000911 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
912 E->setGNUSyntax(Record[Idx++]);
913
914 llvm::SmallVector<Designator, 4> Designators;
915 while (Idx < Record.size()) {
916 switch ((pch::DesignatorTypes)Record[Idx++]) {
917 case pch::DESIG_FIELD_DECL: {
918 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
919 SourceLocation DotLoc
920 = SourceLocation::getFromRawEncoding(Record[Idx++]);
921 SourceLocation FieldLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
924 FieldLoc));
925 Designators.back().setField(Field);
926 break;
927 }
928
929 case pch::DESIG_FIELD_NAME: {
930 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
931 SourceLocation DotLoc
932 = SourceLocation::getFromRawEncoding(Record[Idx++]);
933 SourceLocation FieldLoc
934 = SourceLocation::getFromRawEncoding(Record[Idx++]);
935 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
936 break;
937 }
938
939 case pch::DESIG_ARRAY: {
940 unsigned Index = Record[Idx++];
941 SourceLocation LBracketLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 SourceLocation RBracketLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
946 break;
947 }
948
949 case pch::DESIG_ARRAY_RANGE: {
950 unsigned Index = Record[Idx++];
951 SourceLocation LBracketLoc
952 = SourceLocation::getFromRawEncoding(Record[Idx++]);
953 SourceLocation EllipsisLoc
954 = SourceLocation::getFromRawEncoding(Record[Idx++]);
955 SourceLocation RBracketLoc
956 = SourceLocation::getFromRawEncoding(Record[Idx++]);
957 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
958 RBracketLoc));
959 break;
960 }
961 }
962 }
963 E->setDesignators(&Designators[0], Designators.size());
964
965 return NumSubExprs;
966}
967
968unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
969 VisitExpr(E);
970 return 0;
971}
972
Douglas Gregord3c98a02009-04-15 23:02:49 +0000973unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
974 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000975 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000976 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
977 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
978 return 1;
979}
980
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000981unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
982 VisitExpr(E);
983 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
984 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
985 Reader.SetLabelOf(E, Record[Idx++]);
986 return 0;
987}
988
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000989unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
990 VisitExpr(E);
991 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
994 return 1;
995}
996
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000997unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
998 VisitExpr(E);
999 E->setArgType1(Reader.GetType(Record[Idx++]));
1000 E->setArgType2(Reader.GetType(Record[Idx++]));
1001 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1002 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 return 0;
1004}
1005
1006unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1007 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1009 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1010 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001011 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1012 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1013 return 3;
1014}
1015
1016unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1017 VisitExpr(E);
1018 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1019 return 0;
1020}
Douglas Gregord3c98a02009-04-15 23:02:49 +00001021
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001022unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1023 VisitExpr(E);
1024 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +00001025 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001026 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1028 return NumExprs;
1029}
1030
Douglas Gregor84af7c22009-04-17 19:21:43 +00001031unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1032 VisitExpr(E);
1033 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1034 E->setHasBlockDeclRefExprs(Record[Idx++]);
1035 return 0;
1036}
1037
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001038unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1039 VisitExpr(E);
1040 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1041 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1042 E->setByRef(Record[Idx++]);
1043 return 0;
1044}
1045
Chris Lattner3a57a372009-04-22 06:29:42 +00001046//===----------------------------------------------------------------------===//
1047// Objective-C Expressions and Statements
1048
1049unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1050 VisitExpr(E);
1051 E->setString(cast<StringLiteral>(StmtStack.back()));
1052 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1053 return 1;
1054}
1055
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001056unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1057 VisitExpr(E);
1058 E->setEncodedType(Reader.GetType(Record[Idx++]));
1059 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1060 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1061 return 0;
1062}
1063
Chris Lattner3a57a372009-04-22 06:29:42 +00001064unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1065 VisitExpr(E);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001066 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattner3a57a372009-04-22 06:29:42 +00001067 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1069 return 0;
1070}
1071
1072unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1073 VisitExpr(E);
1074 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1075 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1076 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1077 return 0;
1078}
1079
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001080
Douglas Gregor668c1a42009-04-21 22:25:48 +00001081//===----------------------------------------------------------------------===//
1082// PCH reader implementation
1083//===----------------------------------------------------------------------===//
1084
1085namespace {
1086class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1087 PCHReader &Reader;
1088
1089 // If we know the IdentifierInfo in advance, it is here and we will
1090 // not build a new one. Used when deserializing information about an
1091 // identifier that was constructed before the PCH file was read.
1092 IdentifierInfo *KnownII;
1093
1094public:
1095 typedef IdentifierInfo * data_type;
1096
1097 typedef const std::pair<const char*, unsigned> external_key_type;
1098
1099 typedef external_key_type internal_key_type;
1100
1101 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1102 : Reader(Reader), KnownII(II) { }
1103
1104 static bool EqualKey(const internal_key_type& a,
1105 const internal_key_type& b) {
1106 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1107 : false;
1108 }
1109
1110 static unsigned ComputeHash(const internal_key_type& a) {
1111 return BernsteinHash(a.first, a.second);
1112 }
1113
1114 // This hopefully will just get inlined and removed by the optimizer.
1115 static const internal_key_type&
1116 GetInternalKey(const external_key_type& x) { return x; }
1117
1118 static std::pair<unsigned, unsigned>
1119 ReadKeyDataLength(const unsigned char*& d) {
1120 using namespace clang::io;
1121 unsigned KeyLen = ReadUnalignedLE16(d);
1122 unsigned DataLen = ReadUnalignedLE16(d);
1123 return std::make_pair(KeyLen, DataLen);
1124 }
1125
1126 static std::pair<const char*, unsigned>
1127 ReadKey(const unsigned char* d, unsigned n) {
1128 assert(n >= 2 && d[n-1] == '\0');
1129 return std::make_pair((const char*) d, n-1);
1130 }
1131
1132 IdentifierInfo *ReadData(const internal_key_type& k,
1133 const unsigned char* d,
1134 unsigned DataLen) {
1135 using namespace clang::io;
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001136 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001137 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1138 Bits >>= 1;
1139 bool Poisoned = Bits & 0x01;
1140 Bits >>= 1;
1141 bool ExtensionToken = Bits & 0x01;
1142 Bits >>= 1;
1143 bool hasMacroDefinition = Bits & 0x01;
1144 Bits >>= 1;
1145 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1146 Bits >>= 10;
1147 unsigned TokenID = Bits & 0xFF;
1148 Bits >>= 8;
1149
Douglas Gregor668c1a42009-04-21 22:25:48 +00001150 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001151 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor668c1a42009-04-21 22:25:48 +00001152 DataLen -= 8;
1153
1154 // Build the IdentifierInfo itself and link the identifier ID with
1155 // the new IdentifierInfo.
1156 IdentifierInfo *II = KnownII;
1157 if (!II)
1158 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1159 k.first, k.first + k.second);
1160 Reader.SetIdentifierInfo(ID, II);
1161
Douglas Gregor2deaea32009-04-22 18:49:13 +00001162 // Set or check the various bits in the IdentifierInfo structure.
1163 // FIXME: Load token IDs lazily, too?
1164 assert((unsigned)II->getTokenID() == TokenID &&
1165 "Incorrect token ID loaded");
1166 (void)TokenID;
1167 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1168 assert(II->isExtensionToken() == ExtensionToken &&
1169 "Incorrect extension token flag");
1170 (void)ExtensionToken;
1171 II->setIsPoisoned(Poisoned);
1172 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1173 "Incorrect C++ operator keyword flag");
1174 (void)CPlusPlusOperatorKeyword;
1175
Douglas Gregor37e26842009-04-21 23:56:24 +00001176 // If this identifier is a macro, deserialize the macro
1177 // definition.
1178 if (hasMacroDefinition) {
1179 uint32_t Offset = ReadUnalignedLE64(d);
1180 Reader.ReadMacroRecord(Offset);
1181 DataLen -= 8;
1182 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001183
1184 // Read all of the declarations visible at global scope with this
1185 // name.
1186 Sema *SemaObj = Reader.getSema();
1187 while (DataLen > 0) {
1188 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001189 if (SemaObj) {
1190 // Introduce this declaration into the translation-unit scope
1191 // and add it to the declaration chain for this identifier, so
1192 // that (unqualified) name lookup will find it.
1193 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1194 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1195 } else {
1196 // Queue this declaration so that it will be added to the
1197 // translation unit scope and identifier's declaration chain
1198 // once a Sema object is known.
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001199 Reader.PreloadedDecls.push_back(D);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001200 }
1201
1202 DataLen -= 4;
1203 }
1204 return II;
1205 }
1206};
1207
1208} // end anonymous namespace
1209
1210/// \brief The on-disk hash table used to contain information about
1211/// all of the identifiers in the program.
1212typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1213 PCHIdentifierLookupTable;
1214
Douglas Gregor2cf26342009-04-09 22:27:44 +00001215// FIXME: use the diagnostics machinery
1216static bool Error(const char *Str) {
1217 std::fprintf(stderr, "%s\n", Str);
1218 return true;
1219}
1220
Douglas Gregore1d918e2009-04-10 23:10:45 +00001221/// \brief Check the contents of the predefines buffer against the
1222/// contents of the predefines buffer used to build the PCH file.
1223///
1224/// The contents of the two predefines buffers should be the same. If
1225/// not, then some command-line option changed the preprocessor state
1226/// and we must reject the PCH file.
1227///
1228/// \param PCHPredef The start of the predefines buffer in the PCH
1229/// file.
1230///
1231/// \param PCHPredefLen The length of the predefines buffer in the PCH
1232/// file.
1233///
1234/// \param PCHBufferID The FileID for the PCH predefines buffer.
1235///
1236/// \returns true if there was a mismatch (in which case the PCH file
1237/// should be ignored), or false otherwise.
1238bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1239 unsigned PCHPredefLen,
1240 FileID PCHBufferID) {
1241 const char *Predef = PP.getPredefines().c_str();
1242 unsigned PredefLen = PP.getPredefines().size();
1243
1244 // If the two predefines buffers compare equal, we're done!.
1245 if (PredefLen == PCHPredefLen &&
1246 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1247 return false;
1248
1249 // The predefines buffers are different. Produce a reasonable
1250 // diagnostic showing where they are different.
1251
1252 // The source locations (potentially in the two different predefines
1253 // buffers)
1254 SourceLocation Loc1, Loc2;
1255 SourceManager &SourceMgr = PP.getSourceManager();
1256
1257 // Create a source buffer for our predefines string, so
1258 // that we can build a diagnostic that points into that
1259 // source buffer.
1260 FileID BufferID;
1261 if (Predef && Predef[0]) {
1262 llvm::MemoryBuffer *Buffer
1263 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1264 "<built-in>");
1265 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1266 }
1267
1268 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1269 std::pair<const char *, const char *> Locations
1270 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1271
1272 if (Locations.first != Predef + MinLen) {
1273 // We found the location in the two buffers where there is a
1274 // difference. Form source locations to point there (in both
1275 // buffers).
1276 unsigned Offset = Locations.first - Predef;
1277 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1278 .getFileLocWithOffset(Offset);
1279 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1280 .getFileLocWithOffset(Offset);
1281 } else if (PredefLen > PCHPredefLen) {
1282 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1283 .getFileLocWithOffset(MinLen);
1284 } else {
1285 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1286 .getFileLocWithOffset(MinLen);
1287 }
1288
1289 Diag(Loc1, diag::warn_pch_preprocessor);
1290 if (Loc2.isValid())
1291 Diag(Loc2, diag::note_predef_in_pch);
1292 Diag(diag::note_ignoring_pch) << FileName;
1293 return true;
1294}
1295
Douglas Gregorbd945002009-04-13 16:31:14 +00001296/// \brief Read the line table in the source manager block.
1297/// \returns true if ther was an error.
1298static bool ParseLineTable(SourceManager &SourceMgr,
1299 llvm::SmallVectorImpl<uint64_t> &Record) {
1300 unsigned Idx = 0;
1301 LineTableInfo &LineTable = SourceMgr.getLineTable();
1302
1303 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001304 std::map<int, int> FileIDs;
1305 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001306 // Extract the file name
1307 unsigned FilenameLen = Record[Idx++];
1308 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1309 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +00001310 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1311 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001312 }
1313
1314 // Parse the line entries
1315 std::vector<LineEntry> Entries;
1316 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +00001317 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +00001318
1319 // Extract the line entries
1320 unsigned NumEntries = Record[Idx++];
1321 Entries.clear();
1322 Entries.reserve(NumEntries);
1323 for (unsigned I = 0; I != NumEntries; ++I) {
1324 unsigned FileOffset = Record[Idx++];
1325 unsigned LineNo = Record[Idx++];
1326 int FilenameID = Record[Idx++];
1327 SrcMgr::CharacteristicKind FileKind
1328 = (SrcMgr::CharacteristicKind)Record[Idx++];
1329 unsigned IncludeOffset = Record[Idx++];
1330 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1331 FileKind, IncludeOffset));
1332 }
1333 LineTable.AddEntry(FID, Entries);
1334 }
1335
1336 return false;
1337}
1338
Douglas Gregor14f79002009-04-10 03:52:48 +00001339/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001340PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001341 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001342 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1343 Error("Malformed source manager block record");
1344 return Failure;
1345 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001346
1347 SourceManager &SourceMgr = Context.getSourceManager();
1348 RecordData Record;
1349 while (true) {
1350 unsigned Code = Stream.ReadCode();
1351 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001352 if (Stream.ReadBlockEnd()) {
1353 Error("Error at end of Source Manager block");
1354 return Failure;
1355 }
1356
1357 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001358 }
1359
1360 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1361 // No known subblocks, always skip them.
1362 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001363 if (Stream.SkipBlock()) {
1364 Error("Malformed block record");
1365 return Failure;
1366 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001367 continue;
1368 }
1369
1370 if (Code == llvm::bitc::DEFINE_ABBREV) {
1371 Stream.ReadAbbrevRecord();
1372 continue;
1373 }
1374
1375 // Read a record.
1376 const char *BlobStart;
1377 unsigned BlobLen;
1378 Record.clear();
1379 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1380 default: // Default behavior: ignore.
1381 break;
1382
1383 case pch::SM_SLOC_FILE_ENTRY: {
1384 // FIXME: We would really like to delay the creation of this
1385 // FileEntry until it is actually required, e.g., when producing
1386 // a diagnostic with a source location in this file.
1387 const FileEntry *File
1388 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1389 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001390 FileID ID = SourceMgr.createFileID(File,
1391 SourceLocation::getFromRawEncoding(Record[1]),
1392 (CharacteristicKind)Record[2]);
1393 if (Record[3])
1394 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1395 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 break;
1397 }
1398
1399 case pch::SM_SLOC_BUFFER_ENTRY: {
1400 const char *Name = BlobStart;
1401 unsigned Code = Stream.ReadCode();
1402 Record.clear();
1403 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1404 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001405 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001406 llvm::MemoryBuffer *Buffer
1407 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1408 BlobStart + BlobLen - 1,
1409 Name);
1410 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1411
1412 if (strcmp(Name, "<built-in>") == 0
1413 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1414 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001415 break;
1416 }
1417
1418 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1419 SourceLocation SpellingLoc
1420 = SourceLocation::getFromRawEncoding(Record[1]);
1421 SourceMgr.createInstantiationLoc(
1422 SpellingLoc,
1423 SourceLocation::getFromRawEncoding(Record[2]),
1424 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001425 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001426 break;
1427 }
1428
Chris Lattner2c78b872009-04-14 23:22:57 +00001429 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001430 if (ParseLineTable(SourceMgr, Record))
1431 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001432 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001433 }
1434 }
1435}
1436
Douglas Gregor37e26842009-04-21 23:56:24 +00001437void PCHReader::ReadMacroRecord(uint64_t Offset) {
1438 // Keep track of where we are in the stream, then jump back there
1439 // after reading this macro.
1440 SavedStreamPosition SavedPosition(Stream);
1441
1442 Stream.JumpToBit(Offset);
1443 RecordData Record;
1444 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1445 MacroInfo *Macro = 0;
1446 while (true) {
1447 unsigned Code = Stream.ReadCode();
1448 switch (Code) {
1449 case llvm::bitc::END_BLOCK:
1450 return;
1451
1452 case llvm::bitc::ENTER_SUBBLOCK:
1453 // No known subblocks, always skip them.
1454 Stream.ReadSubBlockID();
1455 if (Stream.SkipBlock()) {
1456 Error("Malformed block record");
1457 return;
1458 }
1459 continue;
1460
1461 case llvm::bitc::DEFINE_ABBREV:
1462 Stream.ReadAbbrevRecord();
1463 continue;
1464 default: break;
1465 }
1466
1467 // Read a record.
1468 Record.clear();
1469 pch::PreprocessorRecordTypes RecType =
1470 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1471 switch (RecType) {
1472 case pch::PP_COUNTER_VALUE:
1473 // Skip this record.
1474 break;
1475
1476 case pch::PP_MACRO_OBJECT_LIKE:
1477 case pch::PP_MACRO_FUNCTION_LIKE: {
1478 // If we already have a macro, that means that we've hit the end
1479 // of the definition of the macro we were looking for. We're
1480 // done.
1481 if (Macro)
1482 return;
1483
1484 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1485 if (II == 0) {
1486 Error("Macro must have a name");
1487 return;
1488 }
1489 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1490 bool isUsed = Record[2];
1491
1492 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1493 MI->setIsUsed(isUsed);
1494
1495 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1496 // Decode function-like macro info.
1497 bool isC99VarArgs = Record[3];
1498 bool isGNUVarArgs = Record[4];
1499 MacroArgs.clear();
1500 unsigned NumArgs = Record[5];
1501 for (unsigned i = 0; i != NumArgs; ++i)
1502 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1503
1504 // Install function-like macro info.
1505 MI->setIsFunctionLike();
1506 if (isC99VarArgs) MI->setIsC99Varargs();
1507 if (isGNUVarArgs) MI->setIsGNUVarargs();
1508 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1509 PP.getPreprocessorAllocator());
1510 }
1511
1512 // Finally, install the macro.
1513 PP.setMacroInfo(II, MI);
1514
1515 // Remember that we saw this macro last so that we add the tokens that
1516 // form its body to it.
1517 Macro = MI;
1518 ++NumMacrosRead;
1519 break;
1520 }
1521
1522 case pch::PP_TOKEN: {
1523 // If we see a TOKEN before a PP_MACRO_*, then the file is
1524 // erroneous, just pretend we didn't see this.
1525 if (Macro == 0) break;
1526
1527 Token Tok;
1528 Tok.startToken();
1529 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1530 Tok.setLength(Record[1]);
1531 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1532 Tok.setIdentifierInfo(II);
1533 Tok.setKind((tok::TokenKind)Record[3]);
1534 Tok.setFlag((Token::TokenFlags)Record[4]);
1535 Macro->AddTokenToBody(Tok);
1536 break;
1537 }
1538 }
1539 }
1540}
1541
Chris Lattner42d42b52009-04-10 21:41:48 +00001542bool PCHReader::ReadPreprocessorBlock() {
1543 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1544 return Error("Malformed preprocessor block record");
1545
Chris Lattner42d42b52009-04-10 21:41:48 +00001546 RecordData Record;
Chris Lattner42d42b52009-04-10 21:41:48 +00001547 while (true) {
1548 unsigned Code = Stream.ReadCode();
1549 switch (Code) {
1550 case llvm::bitc::END_BLOCK:
1551 if (Stream.ReadBlockEnd())
1552 return Error("Error at end of preprocessor block");
1553 return false;
1554
1555 case llvm::bitc::ENTER_SUBBLOCK:
1556 // No known subblocks, always skip them.
1557 Stream.ReadSubBlockID();
1558 if (Stream.SkipBlock())
1559 return Error("Malformed block record");
1560 continue;
1561
1562 case llvm::bitc::DEFINE_ABBREV:
1563 Stream.ReadAbbrevRecord();
1564 continue;
1565 default: break;
1566 }
1567
1568 // Read a record.
1569 Record.clear();
1570 pch::PreprocessorRecordTypes RecType =
1571 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1572 switch (RecType) {
1573 default: // Default behavior: ignore unknown records.
1574 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001575 case pch::PP_COUNTER_VALUE:
1576 if (!Record.empty())
1577 PP.setCounterValue(Record[0]);
1578 break;
1579
Chris Lattner42d42b52009-04-10 21:41:48 +00001580 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregor37e26842009-04-21 23:56:24 +00001581 case pch::PP_MACRO_FUNCTION_LIKE:
1582 case pch::PP_TOKEN:
1583 // Once we've hit a macro definition or a token, we're done.
1584 return false;
Chris Lattner42d42b52009-04-10 21:41:48 +00001585 }
1586 }
1587}
1588
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001589bool PCHReader::ReadSelectorBlock() {
1590 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1591 return Error("Malformed selector block record");
1592
1593 RecordData Record;
1594 while (true) {
1595 unsigned Code = Stream.ReadCode();
1596 switch (Code) {
1597 case llvm::bitc::END_BLOCK:
1598 if (Stream.ReadBlockEnd())
1599 return Error("Error at end of preprocessor block");
1600 return false;
1601
1602 case llvm::bitc::ENTER_SUBBLOCK:
1603 // No known subblocks, always skip them.
1604 Stream.ReadSubBlockID();
1605 if (Stream.SkipBlock())
1606 return Error("Malformed block record");
1607 continue;
1608
1609 case llvm::bitc::DEFINE_ABBREV:
1610 Stream.ReadAbbrevRecord();
1611 continue;
1612 default: break;
1613 }
1614
1615 // Read a record.
1616 Record.clear();
1617 pch::PCHRecordTypes RecType =
1618 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1619 switch (RecType) {
1620 default: // Default behavior: ignore unknown records.
1621 break;
1622 case pch::SELECTOR_TABLE:
1623 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1624 unsigned NumSels = Record[Idx++];
1625
1626 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1627 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1628 unsigned NumArgs = Record[Idx++];
1629 KeyIdents.clear();
1630 if (NumArgs <= 1) {
1631 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1632 assert(II && "DecodeIdentifierInfo returned 0");
1633 KeyIdents.push_back(II);
1634 } else {
1635 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1636 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1637 assert(II && "DecodeIdentifierInfo returned 0");
1638 KeyIdents.push_back(II);
1639 }
1640 }
1641 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1642 SelectorData.push_back(Sel);
1643 }
1644 }
1645 }
1646 return false;
1647}
1648
Douglas Gregor668c1a42009-04-21 22:25:48 +00001649PCHReader::PCHReadResult
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001650PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1651 uint64_t &SelectorBlockOffset) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001652 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1653 Error("Malformed block record");
1654 return Failure;
1655 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001656
1657 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001658 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001659 while (!Stream.AtEndOfStream()) {
1660 unsigned Code = Stream.ReadCode();
1661 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001662 if (Stream.ReadBlockEnd()) {
1663 Error("Error at end of module block");
1664 return Failure;
1665 }
Chris Lattner7356a312009-04-11 21:15:38 +00001666
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001667 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001668 }
1669
1670 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1671 switch (Stream.ReadSubBlockID()) {
1672 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1673 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1674 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001675 if (Stream.SkipBlock()) {
1676 Error("Malformed block record");
1677 return Failure;
1678 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001679 break;
1680
Chris Lattner7356a312009-04-11 21:15:38 +00001681 case pch::PREPROCESSOR_BLOCK_ID:
1682 // Skip the preprocessor block for now, but remember where it is. We
1683 // want to read it in after the identifier table.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001684 if (PreprocessorBlockOffset) {
Chris Lattner7356a312009-04-11 21:15:38 +00001685 Error("Multiple preprocessor blocks found.");
1686 return Failure;
1687 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001688 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001689 if (Stream.SkipBlock()) {
1690 Error("Malformed block record");
1691 return Failure;
1692 }
1693 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001694
1695 case pch::SELECTOR_BLOCK_ID:
1696 // Skip the selector block for now, but remember where it is. We
1697 // want to read it in after the identifier table.
1698 if (SelectorBlockOffset) {
1699 Error("Multiple selector blocks found.");
1700 return Failure;
1701 }
1702 SelectorBlockOffset = Stream.GetCurrentBitNo();
1703 if (Stream.SkipBlock()) {
1704 Error("Malformed block record");
1705 return Failure;
1706 }
1707 break;
Chris Lattner7356a312009-04-11 21:15:38 +00001708
Douglas Gregor14f79002009-04-10 03:52:48 +00001709 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001710 switch (ReadSourceManagerBlock()) {
1711 case Success:
1712 break;
1713
1714 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001715 Error("Malformed source manager block");
1716 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001717
1718 case IgnorePCH:
1719 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001720 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001721 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001722 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001723 continue;
1724 }
1725
1726 if (Code == llvm::bitc::DEFINE_ABBREV) {
1727 Stream.ReadAbbrevRecord();
1728 continue;
1729 }
1730
1731 // Read and process a record.
1732 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001733 const char *BlobStart = 0;
1734 unsigned BlobLen = 0;
1735 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1736 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001737 default: // Default behavior: ignore.
1738 break;
1739
1740 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001741 if (!TypeOffsets.empty()) {
1742 Error("Duplicate TYPE_OFFSET record in PCH file");
1743 return Failure;
1744 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001745 TypeOffsets.swap(Record);
1746 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1747 break;
1748
1749 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001750 if (!DeclOffsets.empty()) {
1751 Error("Duplicate DECL_OFFSET record in PCH file");
1752 return Failure;
1753 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001754 DeclOffsets.swap(Record);
1755 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1756 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001757
1758 case pch::LANGUAGE_OPTIONS:
1759 if (ParseLanguageOptions(Record))
1760 return IgnorePCH;
1761 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001762
Douglas Gregorafaf3082009-04-11 00:14:32 +00001763 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001764 std::string TargetTriple(BlobStart, BlobLen);
1765 if (TargetTriple != Context.Target.getTargetTriple()) {
1766 Diag(diag::warn_pch_target_triple)
1767 << TargetTriple << Context.Target.getTargetTriple();
1768 Diag(diag::note_ignoring_pch) << FileName;
1769 return IgnorePCH;
1770 }
1771 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001773
1774 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001775 IdentifierTableData = BlobStart;
1776 IdentifierLookupTable
1777 = PCHIdentifierLookupTable::Create(
1778 (const unsigned char *)IdentifierTableData + Record[0],
1779 (const unsigned char *)IdentifierTableData,
1780 PCHIdentifierLookupTrait(*this));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001781 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001782 break;
1783
1784 case pch::IDENTIFIER_OFFSET:
1785 if (!IdentifierData.empty()) {
1786 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1787 return Failure;
1788 }
1789 IdentifierData.swap(Record);
1790#ifndef NDEBUG
1791 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1792 if ((IdentifierData[I] & 0x01) == 0) {
1793 Error("Malformed identifier table in the precompiled header");
1794 return Failure;
1795 }
1796 }
1797#endif
1798 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001799
1800 case pch::EXTERNAL_DEFINITIONS:
1801 if (!ExternalDefinitions.empty()) {
1802 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1803 return Failure;
1804 }
1805 ExternalDefinitions.swap(Record);
1806 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001807
Douglas Gregorad1de002009-04-18 05:55:16 +00001808 case pch::SPECIAL_TYPES:
1809 SpecialTypes.swap(Record);
1810 break;
1811
Douglas Gregor3e1af842009-04-17 22:13:46 +00001812 case pch::STATISTICS:
1813 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001814 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001815 TotalLexicalDeclContexts = Record[2];
1816 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001817 break;
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001818 case pch::TENTATIVE_DEFINITIONS:
1819 if (!TentativeDefinitions.empty()) {
1820 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1821 return Failure;
1822 }
1823 TentativeDefinitions.swap(Record);
1824 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001825
1826 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1827 if (!LocallyScopedExternalDecls.empty()) {
1828 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1829 return Failure;
1830 }
1831 LocallyScopedExternalDecls.swap(Record);
1832 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001833 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001834 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001835 Error("Premature end of bitstream");
1836 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001837}
1838
Douglas Gregore1d918e2009-04-10 23:10:45 +00001839PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001840 // Set the PCH file name.
1841 this->FileName = FileName;
1842
Douglas Gregor2cf26342009-04-09 22:27:44 +00001843 // Open the PCH file.
1844 std::string ErrStr;
1845 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001846 if (!Buffer) {
1847 Error(ErrStr.c_str());
1848 return IgnorePCH;
1849 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001850
1851 // Initialize the stream
1852 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1853 (const unsigned char *)Buffer->getBufferEnd());
1854
1855 // Sniff for the signature.
1856 if (Stream.Read(8) != 'C' ||
1857 Stream.Read(8) != 'P' ||
1858 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001859 Stream.Read(8) != 'H') {
1860 Error("Not a PCH file");
1861 return IgnorePCH;
1862 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001863
1864 // We expect a number of well-defined blocks, though we don't necessarily
1865 // need to understand them all.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001866 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001867 uint64_t SelectorBlockOffset = 0;
1868
Douglas Gregor2cf26342009-04-09 22:27:44 +00001869 while (!Stream.AtEndOfStream()) {
1870 unsigned Code = Stream.ReadCode();
1871
Douglas Gregore1d918e2009-04-10 23:10:45 +00001872 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1873 Error("Invalid record at top-level");
1874 return Failure;
1875 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001876
1877 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001878
Douglas Gregor2cf26342009-04-09 22:27:44 +00001879 // We only know the PCH subblock ID.
1880 switch (BlockID) {
1881 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001882 if (Stream.ReadBlockInfoBlock()) {
1883 Error("Malformed BlockInfoBlock");
1884 return Failure;
1885 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001886 break;
1887 case pch::PCH_BLOCK_ID:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001888 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001889 case Success:
1890 break;
1891
1892 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001893 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001894
1895 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001896 // FIXME: We could consider reading through to the end of this
1897 // PCH block, skipping subblocks, to see if there are other
1898 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001899 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001900 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901 break;
1902 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001903 if (Stream.SkipBlock()) {
1904 Error("Malformed block record");
1905 return Failure;
1906 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001907 break;
1908 }
1909 }
1910
1911 // Load the translation unit declaration
1912 ReadDeclRecord(DeclOffsets[0], 0);
1913
Douglas Gregor668c1a42009-04-21 22:25:48 +00001914 // Initialization of builtins and library builtins occurs before the
1915 // PCH file is read, so there may be some identifiers that were
1916 // loaded into the IdentifierTable before we intercepted the
1917 // creation of identifiers. Iterate through the list of known
1918 // identifiers and determine whether we have to establish
1919 // preprocessor definitions or top-level identifier declaration
1920 // chains for those identifiers.
1921 //
1922 // We copy the IdentifierInfo pointers to a small vector first,
1923 // since de-serializing declarations or macro definitions can add
1924 // new entries into the identifier table, invalidating the
1925 // iterators.
1926 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1927 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1928 IdEnd = PP.getIdentifierTable().end();
1929 Id != IdEnd; ++Id)
1930 Identifiers.push_back(Id->second);
1931 PCHIdentifierLookupTable *IdTable
1932 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1933 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1934 IdentifierInfo *II = Identifiers[I];
1935 // Look in the on-disk hash table for an entry for
1936 PCHIdentifierLookupTrait Info(*this, II);
1937 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1938 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1939 if (Pos == IdTable->end())
1940 continue;
1941
1942 // Dereferencing the iterator has the effect of populating the
1943 // IdentifierInfo node with the various declarations it needs.
1944 (void)*Pos;
1945 }
1946
Douglas Gregorad1de002009-04-18 05:55:16 +00001947 // Load the special types.
1948 Context.setBuiltinVaListType(
1949 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1950
Douglas Gregor668c1a42009-04-21 22:25:48 +00001951 // If we saw the preprocessor block, read it now.
1952 if (PreprocessorBlockOffset) {
1953 SavedStreamPosition SavedPos(Stream);
1954 Stream.JumpToBit(PreprocessorBlockOffset);
1955 if (ReadPreprocessorBlock()) {
1956 Error("Malformed preprocessor block");
1957 return Failure;
Douglas Gregor0b748912009-04-14 21:18:50 +00001958 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001959 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001960 if (SelectorBlockOffset) {
1961 SavedStreamPosition SavedPos(Stream);
1962 Stream.JumpToBit(SelectorBlockOffset);
1963 if (ReadSelectorBlock()) {
1964 Error("Malformed preprocessor block");
1965 return Failure;
1966 }
1967 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001968
Douglas Gregor668c1a42009-04-21 22:25:48 +00001969 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001970}
1971
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001972/// \brief Parse the record that corresponds to a LangOptions data
1973/// structure.
1974///
1975/// This routine compares the language options used to generate the
1976/// PCH file against the language options set for the current
1977/// compilation. For each option, we classify differences between the
1978/// two compiler states as either "benign" or "important". Benign
1979/// differences don't matter, and we accept them without complaint
1980/// (and without modifying the language options). Differences between
1981/// the states for important options cause the PCH file to be
1982/// unusable, so we emit a warning and return true to indicate that
1983/// there was an error.
1984///
1985/// \returns true if the PCH file is unacceptable, false otherwise.
1986bool PCHReader::ParseLanguageOptions(
1987 const llvm::SmallVectorImpl<uint64_t> &Record) {
1988 const LangOptions &LangOpts = Context.getLangOptions();
1989#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1990#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1991 if (Record[Idx] != LangOpts.Option) { \
1992 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1993 Diag(diag::note_ignoring_pch) << FileName; \
1994 return true; \
1995 } \
1996 ++Idx
1997
1998 unsigned Idx = 0;
1999 PARSE_LANGOPT_BENIGN(Trigraphs);
2000 PARSE_LANGOPT_BENIGN(BCPLComment);
2001 PARSE_LANGOPT_BENIGN(DollarIdents);
2002 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2003 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2004 PARSE_LANGOPT_BENIGN(ImplicitInt);
2005 PARSE_LANGOPT_BENIGN(Digraphs);
2006 PARSE_LANGOPT_BENIGN(HexFloats);
2007 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2008 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2009 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2010 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2011 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2012 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2013 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2014 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2015 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2016 PARSE_LANGOPT_BENIGN(PascalStrings);
2017 PARSE_LANGOPT_BENIGN(Boolean);
2018 PARSE_LANGOPT_BENIGN(WritableStrings);
2019 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2020 diag::warn_pch_lax_vector_conversions);
2021 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2022 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2023 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2024 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2025 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2026 diag::warn_pch_thread_safe_statics);
2027 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2028 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2029 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2030 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2031 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2032 diag::warn_pch_heinous_extensions);
2033 // FIXME: Most of the options below are benign if the macro wasn't
2034 // used. Unfortunately, this means that a PCH compiled without
2035 // optimization can't be used with optimization turned on, even
2036 // though the only thing that changes is whether __OPTIMIZE__ was
2037 // defined... but if __OPTIMIZE__ never showed up in the header, it
2038 // doesn't matter. We could consider making this some special kind
2039 // of check.
2040 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2041 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2042 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2043 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2044 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2045 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2046 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2047 Diag(diag::warn_pch_gc_mode)
2048 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2049 Diag(diag::note_ignoring_pch) << FileName;
2050 return true;
2051 }
2052 ++Idx;
2053 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2054 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2055#undef PARSE_LANGOPT_IRRELEVANT
2056#undef PARSE_LANGOPT_BENIGN
2057
2058 return false;
2059}
2060
Douglas Gregor2cf26342009-04-09 22:27:44 +00002061/// \brief Read and return the type at the given offset.
2062///
2063/// This routine actually reads the record corresponding to the type
2064/// at the given offset in the bitstream. It is a helper routine for
2065/// GetType, which deals with reading type IDs.
2066QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002067 // Keep track of where we are in the stream, then jump back there
2068 // after reading this type.
2069 SavedStreamPosition SavedPosition(Stream);
2070
Douglas Gregor2cf26342009-04-09 22:27:44 +00002071 Stream.JumpToBit(Offset);
2072 RecordData Record;
2073 unsigned Code = Stream.ReadCode();
2074 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00002075 case pch::TYPE_EXT_QUAL: {
2076 assert(Record.size() == 3 &&
2077 "Incorrect encoding of extended qualifier type");
2078 QualType Base = GetType(Record[0]);
2079 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2080 unsigned AddressSpace = Record[2];
2081
2082 QualType T = Base;
2083 if (GCAttr != QualType::GCNone)
2084 T = Context.getObjCGCQualType(T, GCAttr);
2085 if (AddressSpace)
2086 T = Context.getAddrSpaceQualType(T, AddressSpace);
2087 return T;
2088 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002089
Douglas Gregor2cf26342009-04-09 22:27:44 +00002090 case pch::TYPE_FIXED_WIDTH_INT: {
2091 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2092 return Context.getFixedWidthIntType(Record[0], Record[1]);
2093 }
2094
2095 case pch::TYPE_COMPLEX: {
2096 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2097 QualType ElemType = GetType(Record[0]);
2098 return Context.getComplexType(ElemType);
2099 }
2100
2101 case pch::TYPE_POINTER: {
2102 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2103 QualType PointeeType = GetType(Record[0]);
2104 return Context.getPointerType(PointeeType);
2105 }
2106
2107 case pch::TYPE_BLOCK_POINTER: {
2108 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2109 QualType PointeeType = GetType(Record[0]);
2110 return Context.getBlockPointerType(PointeeType);
2111 }
2112
2113 case pch::TYPE_LVALUE_REFERENCE: {
2114 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2115 QualType PointeeType = GetType(Record[0]);
2116 return Context.getLValueReferenceType(PointeeType);
2117 }
2118
2119 case pch::TYPE_RVALUE_REFERENCE: {
2120 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2121 QualType PointeeType = GetType(Record[0]);
2122 return Context.getRValueReferenceType(PointeeType);
2123 }
2124
2125 case pch::TYPE_MEMBER_POINTER: {
2126 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2127 QualType PointeeType = GetType(Record[0]);
2128 QualType ClassType = GetType(Record[1]);
2129 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2130 }
2131
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002132 case pch::TYPE_CONSTANT_ARRAY: {
2133 QualType ElementType = GetType(Record[0]);
2134 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2135 unsigned IndexTypeQuals = Record[2];
2136 unsigned Idx = 3;
2137 llvm::APInt Size = ReadAPInt(Record, Idx);
2138 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2139 }
2140
2141 case pch::TYPE_INCOMPLETE_ARRAY: {
2142 QualType ElementType = GetType(Record[0]);
2143 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2144 unsigned IndexTypeQuals = Record[2];
2145 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2146 }
2147
2148 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002149 QualType ElementType = GetType(Record[0]);
2150 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2151 unsigned IndexTypeQuals = Record[2];
2152 return Context.getVariableArrayType(ElementType, ReadExpr(),
2153 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002154 }
2155
2156 case pch::TYPE_VECTOR: {
2157 if (Record.size() != 2) {
2158 Error("Incorrect encoding of vector type in PCH file");
2159 return QualType();
2160 }
2161
2162 QualType ElementType = GetType(Record[0]);
2163 unsigned NumElements = Record[1];
2164 return Context.getVectorType(ElementType, NumElements);
2165 }
2166
2167 case pch::TYPE_EXT_VECTOR: {
2168 if (Record.size() != 2) {
2169 Error("Incorrect encoding of extended vector type in PCH file");
2170 return QualType();
2171 }
2172
2173 QualType ElementType = GetType(Record[0]);
2174 unsigned NumElements = Record[1];
2175 return Context.getExtVectorType(ElementType, NumElements);
2176 }
2177
2178 case pch::TYPE_FUNCTION_NO_PROTO: {
2179 if (Record.size() != 1) {
2180 Error("Incorrect encoding of no-proto function type");
2181 return QualType();
2182 }
2183 QualType ResultType = GetType(Record[0]);
2184 return Context.getFunctionNoProtoType(ResultType);
2185 }
2186
2187 case pch::TYPE_FUNCTION_PROTO: {
2188 QualType ResultType = GetType(Record[0]);
2189 unsigned Idx = 1;
2190 unsigned NumParams = Record[Idx++];
2191 llvm::SmallVector<QualType, 16> ParamTypes;
2192 for (unsigned I = 0; I != NumParams; ++I)
2193 ParamTypes.push_back(GetType(Record[Idx++]));
2194 bool isVariadic = Record[Idx++];
2195 unsigned Quals = Record[Idx++];
2196 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2197 isVariadic, Quals);
2198 }
2199
2200 case pch::TYPE_TYPEDEF:
2201 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2202 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2203
2204 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00002205 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002206
2207 case pch::TYPE_TYPEOF: {
2208 if (Record.size() != 1) {
2209 Error("Incorrect encoding of typeof(type) in PCH file");
2210 return QualType();
2211 }
2212 QualType UnderlyingType = GetType(Record[0]);
2213 return Context.getTypeOfType(UnderlyingType);
2214 }
2215
2216 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00002217 assert(Record.size() == 1 && "Incorrect encoding of record type");
2218 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002219
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002220 case pch::TYPE_ENUM:
2221 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2222 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2223
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002224 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner4dcf151a2009-04-22 05:57:30 +00002225 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2226 return Context.getObjCInterfaceType(
2227 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002228
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002229 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2230 unsigned Idx = 0;
2231 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2232 unsigned NumProtos = Record[Idx++];
2233 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2234 for (unsigned I = 0; I != NumProtos; ++I)
2235 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2236 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2237 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002238
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002239 case pch::TYPE_OBJC_QUALIFIED_ID: {
2240 unsigned Idx = 0;
2241 unsigned NumProtos = Record[Idx++];
2242 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2243 for (unsigned I = 0; I != NumProtos; ++I)
2244 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2245 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2246 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002248 // Suppress a GCC warning
2249 return QualType();
2250}
2251
2252/// \brief Note that we have loaded the declaration with the given
2253/// Index.
2254///
2255/// This routine notes that this declaration has already been loaded,
2256/// so that future GetDecl calls will return this declaration rather
2257/// than trying to load a new declaration.
2258inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2259 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2260 DeclAlreadyLoaded[Index] = true;
2261 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2262}
2263
2264/// \brief Read the declaration at the given offset from the PCH file.
2265Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002266 // Keep track of where we are in the stream, then jump back there
2267 // after reading this declaration.
2268 SavedStreamPosition SavedPosition(Stream);
2269
Douglas Gregor2cf26342009-04-09 22:27:44 +00002270 Decl *D = 0;
2271 Stream.JumpToBit(Offset);
2272 RecordData Record;
2273 unsigned Code = Stream.ReadCode();
2274 unsigned Idx = 0;
2275 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00002276
Douglas Gregor2cf26342009-04-09 22:27:44 +00002277 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002278 case pch::DECL_ATTR:
2279 case pch::DECL_CONTEXT_LEXICAL:
2280 case pch::DECL_CONTEXT_VISIBLE:
2281 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2282 break;
2283
Douglas Gregor2cf26342009-04-09 22:27:44 +00002284 case pch::DECL_TRANSLATION_UNIT:
2285 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 break;
2288
2289 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002290 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002291 break;
2292 }
2293
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002294 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002295 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002296 break;
2297 }
2298
Douglas Gregor8c700062009-04-13 21:20:57 +00002299 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002300 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2301 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00002302 break;
2303 }
2304
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002305 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002306 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2307 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002308 break;
2309 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002310
2311 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002312 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2313 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002314 break;
2315 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002316
Steve Naroff53c9d8a2009-04-20 15:06:07 +00002317 case pch::DECL_OBJC_METHOD: {
2318 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2319 Selector(), QualType(), 0);
2320 break;
2321 }
2322
Steve Naroff30833f82009-04-21 15:12:33 +00002323 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002324 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2325 break;
2326 }
2327
Steve Naroff30833f82009-04-21 15:12:33 +00002328 case pch::DECL_OBJC_IVAR: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002329 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2330 ObjCIvarDecl::None);
2331 break;
2332 }
2333
Steve Naroff30833f82009-04-21 15:12:33 +00002334 case pch::DECL_OBJC_PROTOCOL: {
2335 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2336 break;
2337 }
2338
2339 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2340 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2341 QualType(), 0);
2342 break;
2343 }
2344
2345 case pch::DECL_OBJC_CLASS: {
2346 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2347 break;
2348 }
2349
2350 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2351 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2352 break;
2353 }
2354
2355 case pch::DECL_OBJC_CATEGORY: {
2356 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2357 break;
2358 }
2359
2360 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor10b0e1f2009-04-23 02:53:57 +00002361 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002362 break;
2363 }
2364
2365 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor8f36aba2009-04-23 03:23:08 +00002366 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002367 break;
2368 }
2369
2370 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregor17eee4a2009-04-23 03:51:49 +00002371 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002372 break;
2373 }
2374
2375 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor70e5a142009-04-22 23:20:34 +00002376 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff30833f82009-04-21 15:12:33 +00002377 break;
2378 }
2379
2380 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor8818c4f2009-04-23 03:43:53 +00002381 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2382 SourceLocation(), 0,
2383 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002384 break;
2385 }
2386
Douglas Gregor8c700062009-04-13 21:20:57 +00002387 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002388 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2389 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00002390 break;
2391 }
2392
Douglas Gregor2cf26342009-04-09 22:27:44 +00002393 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002394 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2395 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002396 break;
2397 }
2398
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002399 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002400 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2401 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002402 break;
2403 }
2404
2405 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002406 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002407 QualType(), QualType(), VarDecl::None,
2408 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002409 break;
2410 }
2411
Douglas Gregor1028bc62009-04-13 22:49:25 +00002412 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002413 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00002414 break;
2415 }
2416
2417 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002418 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00002419 break;
2420 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002421 }
2422
Douglas Gregor668c1a42009-04-21 22:25:48 +00002423 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002424 if (D) {
2425 LoadedDecl(Index, D);
2426 Reader.Visit(D);
2427 }
2428
Douglas Gregor2cf26342009-04-09 22:27:44 +00002429 // If this declaration is also a declaration context, get the
2430 // offsets for its tables of lexical and visible declarations.
2431 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2432 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2433 if (Offsets.first || Offsets.second) {
2434 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2435 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2436 DeclContextOffsets[DC] = Offsets;
2437 }
2438 }
2439 assert(Idx == Record.size());
2440
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002441 if (Consumer) {
2442 // If we have deserialized a declaration that has a definition the
2443 // AST consumer might need to know about, notify the consumer
2444 // about that definition now.
2445 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2446 if (Var->isFileVarDecl() && Var->getInit()) {
2447 DeclGroupRef DG(Var);
2448 Consumer->HandleTopLevelDecl(DG);
2449 }
2450 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2451 if (Func->isThisDeclarationADefinition()) {
2452 DeclGroupRef DG(Func);
2453 Consumer->HandleTopLevelDecl(DG);
2454 }
2455 }
2456 }
2457
Douglas Gregor2cf26342009-04-09 22:27:44 +00002458 return D;
2459}
2460
Douglas Gregor8038d512009-04-10 17:25:41 +00002461QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002462 unsigned Quals = ID & 0x07;
2463 unsigned Index = ID >> 3;
2464
2465 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2466 QualType T;
2467 switch ((pch::PredefinedTypeIDs)Index) {
2468 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2469 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2470 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2471
2472 case pch::PREDEF_TYPE_CHAR_U_ID:
2473 case pch::PREDEF_TYPE_CHAR_S_ID:
2474 // FIXME: Check that the signedness of CharTy is correct!
2475 T = Context.CharTy;
2476 break;
2477
2478 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2479 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2480 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2481 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2482 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2483 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2484 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2485 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2486 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2487 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2488 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2489 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2490 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2491 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2492 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2493 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2494 }
2495
2496 assert(!T.isNull() && "Unknown predefined type");
2497 return T.getQualifiedType(Quals);
2498 }
2499
2500 Index -= pch::NUM_PREDEF_TYPE_IDS;
2501 if (!TypeAlreadyLoaded[Index]) {
2502 // Load the type from the PCH file.
2503 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2504 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2505 TypeAlreadyLoaded[Index] = true;
2506 }
2507
2508 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2509}
2510
Douglas Gregor8038d512009-04-10 17:25:41 +00002511Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002512 if (ID == 0)
2513 return 0;
2514
2515 unsigned Index = ID - 1;
2516 if (DeclAlreadyLoaded[Index])
2517 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2518
2519 // Load the declaration from the PCH file.
2520 return ReadDeclRecord(DeclOffsets[Index], Index);
2521}
2522
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002523Stmt *PCHReader::GetStmt(uint64_t Offset) {
2524 // Keep track of where we are in the stream, then jump back there
2525 // after reading this declaration.
2526 SavedStreamPosition SavedPosition(Stream);
2527
2528 Stream.JumpToBit(Offset);
2529 return ReadStmt();
2530}
2531
Douglas Gregor2cf26342009-04-09 22:27:44 +00002532bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002533 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002534 assert(DC->hasExternalLexicalStorage() &&
2535 "DeclContext has no lexical decls in storage");
2536 uint64_t Offset = DeclContextOffsets[DC].first;
2537 assert(Offset && "DeclContext has no lexical decls in storage");
2538
Douglas Gregor0b748912009-04-14 21:18:50 +00002539 // Keep track of where we are in the stream, then jump back there
2540 // after reading this context.
2541 SavedStreamPosition SavedPosition(Stream);
2542
Douglas Gregor2cf26342009-04-09 22:27:44 +00002543 // Load the record containing all of the declarations lexically in
2544 // this context.
2545 Stream.JumpToBit(Offset);
2546 RecordData Record;
2547 unsigned Code = Stream.ReadCode();
2548 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002549 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002550 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2551
2552 // Load all of the declaration IDs
2553 Decls.clear();
2554 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002555 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002556 return false;
2557}
2558
2559bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2560 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2561 assert(DC->hasExternalVisibleStorage() &&
2562 "DeclContext has no visible decls in storage");
2563 uint64_t Offset = DeclContextOffsets[DC].second;
2564 assert(Offset && "DeclContext has no visible decls in storage");
2565
Douglas Gregor0b748912009-04-14 21:18:50 +00002566 // Keep track of where we are in the stream, then jump back there
2567 // after reading this context.
2568 SavedStreamPosition SavedPosition(Stream);
2569
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570 // Load the record containing all of the declarations visible in
2571 // this context.
2572 Stream.JumpToBit(Offset);
2573 RecordData Record;
2574 unsigned Code = Stream.ReadCode();
2575 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002576 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002577 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2578 if (Record.size() == 0)
2579 return false;
2580
2581 Decls.clear();
2582
2583 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584 while (Idx < Record.size()) {
2585 Decls.push_back(VisibleDeclaration());
2586 Decls.back().Name = ReadDeclarationName(Record, Idx);
2587
Douglas Gregor2cf26342009-04-09 22:27:44 +00002588 unsigned Size = Record[Idx++];
2589 llvm::SmallVector<unsigned, 4> & LoadedDecls
2590 = Decls.back().Declarations;
2591 LoadedDecls.reserve(Size);
2592 for (unsigned I = 0; I < Size; ++I)
2593 LoadedDecls.push_back(Record[Idx++]);
2594 }
2595
Douglas Gregor25123082009-04-22 22:34:57 +00002596 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002597 return false;
2598}
2599
Douglas Gregorfdd01722009-04-14 00:24:19 +00002600void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002601 this->Consumer = Consumer;
2602
Douglas Gregorfdd01722009-04-14 00:24:19 +00002603 if (!Consumer)
2604 return;
2605
2606 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2607 Decl *D = GetDecl(ExternalDefinitions[I]);
2608 DeclGroupRef DG(D);
2609 Consumer->HandleTopLevelDecl(DG);
2610 }
2611}
2612
Douglas Gregor2cf26342009-04-09 22:27:44 +00002613void PCHReader::PrintStats() {
2614 std::fprintf(stderr, "*** PCH Statistics:\n");
2615
2616 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2617 TypeAlreadyLoaded.end(),
2618 true);
2619 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2620 DeclAlreadyLoaded.end(),
2621 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002622 unsigned NumIdentifiersLoaded = 0;
2623 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2624 if ((IdentifierData[I] & 0x01) == 0)
2625 ++NumIdentifiersLoaded;
2626 }
2627
Douglas Gregor2cf26342009-04-09 22:27:44 +00002628 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2629 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002630 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002631 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2632 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002633 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2634 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2635 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2636 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002637 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2638 NumStatementsRead, TotalNumStatements,
2639 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor37e26842009-04-21 23:56:24 +00002640 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2641 NumMacrosRead, TotalNumMacros,
2642 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregor25123082009-04-22 22:34:57 +00002643 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2644 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2645 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2646 * 100));
2647 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2648 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2649 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2650 * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002651 std::fprintf(stderr, "\n");
2652}
2653
Douglas Gregor668c1a42009-04-21 22:25:48 +00002654void PCHReader::InitializeSema(Sema &S) {
2655 SemaObj = &S;
2656
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002657 // Makes sure any declarations that were deserialized "too early"
2658 // still get added to the identifier's declaration chains.
2659 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2660 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2661 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002662 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002663 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002664
2665 // If there were any tentative definitions, deserialize them and add
2666 // them to Sema's table of tentative definitions.
2667 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2668 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2669 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2670 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002671
2672 // If there were any locally-scoped external declarations,
2673 // deserialize them and add them to Sema's table of locally-scoped
2674 // external declarations.
2675 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2676 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2677 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2678 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002679}
2680
2681IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2682 // Try to find this name within our on-disk hash table
2683 PCHIdentifierLookupTable *IdTable
2684 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2685 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2686 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2687 if (Pos == IdTable->end())
2688 return 0;
2689
2690 // Dereferencing the iterator has the effect of building the
2691 // IdentifierInfo node and populating it with the various
2692 // declarations it needs.
2693 return *Pos;
2694}
2695
2696void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2697 assert(ID && "Non-zero identifier ID required");
2698 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2699}
2700
Chris Lattner7356a312009-04-11 21:15:38 +00002701IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002702 if (ID == 0)
2703 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002704
Douglas Gregor668c1a42009-04-21 22:25:48 +00002705 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002706 Error("No identifier table in PCH file");
2707 return 0;
2708 }
Chris Lattner7356a312009-04-11 21:15:38 +00002709
Douglas Gregorafaf3082009-04-11 00:14:32 +00002710 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002711 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002712 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregor668c1a42009-04-21 22:25:48 +00002713 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002714 }
Chris Lattner7356a312009-04-11 21:15:38 +00002715
2716 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002717}
2718
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002719Selector PCHReader::DecodeSelector(unsigned ID) {
2720 if (ID == 0)
2721 return Selector();
2722
2723 if (SelectorData.empty()) {
2724 Error("No selector table in PCH file");
2725 return Selector();
2726 }
2727
2728 if (ID > SelectorData.size()) {
2729 Error("Selector ID out of range");
2730 return Selector();
2731 }
2732 return SelectorData[ID-1];
2733}
2734
Douglas Gregor2cf26342009-04-09 22:27:44 +00002735DeclarationName
2736PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2737 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2738 switch (Kind) {
2739 case DeclarationName::Identifier:
2740 return DeclarationName(GetIdentifierInfo(Record, Idx));
2741
2742 case DeclarationName::ObjCZeroArgSelector:
2743 case DeclarationName::ObjCOneArgSelector:
2744 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00002745 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002746 break;
2747
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}