blob: e2cffb54543d8f9d2e44054ca0866a24e427db30 [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++])));
Steve Naroff33feeb02009-04-20 20:09:33 +0000230 unsigned NumIvars = Record[Idx++];
231 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
232 IVars.reserve(NumIvars);
233 for (unsigned I = 0; I != NumIvars; ++I)
234 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
235 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
236
237 ID->setForwardDecl(Record[Idx++]);
238 ID->setImplicitInterfaceDecl(Record[Idx++]);
239 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
240 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff33feeb02009-04-20 20:09:33 +0000242 // FIXME: add protocols, categories.
243}
244
245void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
246 VisitFieldDecl(IVD);
247 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
248}
249
Steve Naroff30833f82009-04-21 15:12:33 +0000250void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
251 VisitObjCContainerDecl(PD);
252 PD->setForwardDecl(Record[Idx++]);
253 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
259 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
260}
261
262void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
263 VisitFieldDecl(FD);
264}
265
266void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
267 VisitDecl(CD);
268 unsigned NumClassRefs = Record[Idx++];
269 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
270 ClassRefs.reserve(NumClassRefs);
271 for (unsigned I = 0; I != NumClassRefs; ++I)
272 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
274}
275
276void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
277 VisitDecl(FPD);
278 unsigned NumProtoRefs = Record[Idx++];
279 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
280 ProtoRefs.reserve(NumProtoRefs);
281 for (unsigned I = 0; I != NumProtoRefs; ++I)
282 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
283 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
284}
285
286void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
287 VisitObjCContainerDecl(CD);
288 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
289 unsigned NumProtoRefs = Record[Idx++];
290 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
291 ProtoRefs.reserve(NumProtoRefs);
292 for (unsigned I = 0; I != NumProtoRefs; ++I)
293 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
295 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
296 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
297}
298
299void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
300 VisitNamedDecl(CAD);
301 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
302}
303
304void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
305 VisitNamedDecl(D);
Douglas Gregor70e5a142009-04-22 23:20:34 +0000306 D->setType(Reader.GetType(Record[Idx++]));
307 // FIXME: stable encoding
308 D->setPropertyAttributes(
309 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
310 // FIXME: stable encoding
311 D->setPropertyImplementation(
312 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
313 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
314 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
315 D->setGetterMethodDecl(
316 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
317 D->setSetterMethodDecl(
318 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
319 D->setPropertyIvarDecl(
320 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000321}
322
323void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
324 VisitDecl(D);
325 // FIXME: Implement.
326}
327
328void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
329 VisitObjCImplDecl(D);
330 // FIXME: Implement.
331}
332
333void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
334 VisitObjCImplDecl(D);
335 // FIXME: Implement.
336}
337
338
339void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
340 VisitDecl(D);
341 // FIXME: Implement.
342}
343
Douglas Gregor8c700062009-04-13 21:20:57 +0000344void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
345 VisitValueDecl(FD);
346 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000347 if (Record[Idx++])
348 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000349}
350
Douglas Gregor2cf26342009-04-09 22:27:44 +0000351void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
352 VisitValueDecl(VD);
353 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
354 VD->setThreadSpecified(Record[Idx++]);
355 VD->setCXXDirectInitializer(Record[Idx++]);
356 VD->setDeclaredInCondition(Record[Idx++]);
357 VD->setPreviousDeclaration(
358 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
359 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000360 if (Record[Idx++])
361 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000362}
363
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000364void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
365 VisitVarDecl(PD);
366 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000367 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000368}
369
370void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
371 VisitParmVarDecl(PD);
372 PD->setOriginalType(Reader.GetType(Record[Idx++]));
373}
374
Douglas Gregor1028bc62009-04-13 22:49:25 +0000375void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
376 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000377 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000378}
379
380void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
381 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000382 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000383 unsigned NumParams = Record[Idx++];
384 llvm::SmallVector<ParmVarDecl *, 16> Params;
385 Params.reserve(NumParams);
386 for (unsigned I = 0; I != NumParams; ++I)
387 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
388 BD->setParams(Reader.getContext(), &Params[0], NumParams);
389}
390
Douglas Gregor2cf26342009-04-09 22:27:44 +0000391std::pair<uint64_t, uint64_t>
392PCHDeclReader::VisitDeclContext(DeclContext *DC) {
393 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000394 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000395 return std::make_pair(LexicalOffset, VisibleOffset);
396}
397
Douglas Gregor0b748912009-04-14 21:18:50 +0000398//===----------------------------------------------------------------------===//
399// Statement/expression deserialization
400//===----------------------------------------------------------------------===//
401namespace {
402 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000403 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000404 PCHReader &Reader;
405 const PCHReader::RecordData &Record;
406 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000407 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000408
409 public:
410 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000411 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
412 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000413
Douglas Gregor025452f2009-04-17 00:04:06 +0000414 /// \brief The number of record fields required for the Stmt class
415 /// itself.
416 static const unsigned NumStmtFields = 0;
417
Douglas Gregor673ecd62009-04-15 16:35:07 +0000418 /// \brief The number of record fields required for the Expr class
419 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000420 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000421
Douglas Gregor087fd532009-04-14 23:32:43 +0000422 // Each of the Visit* functions reads in part of the expression
423 // from the given record and the current expression stack, then
424 // return the total number of operands that it read from the
425 // expression stack.
426
Douglas Gregor025452f2009-04-17 00:04:06 +0000427 unsigned VisitStmt(Stmt *S);
428 unsigned VisitNullStmt(NullStmt *S);
429 unsigned VisitCompoundStmt(CompoundStmt *S);
430 unsigned VisitSwitchCase(SwitchCase *S);
431 unsigned VisitCaseStmt(CaseStmt *S);
432 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000433 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000434 unsigned VisitIfStmt(IfStmt *S);
435 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000436 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000437 unsigned VisitDoStmt(DoStmt *S);
438 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000439 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000440 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000441 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000442 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000443 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000444 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000445 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000446 unsigned VisitExpr(Expr *E);
447 unsigned VisitPredefinedExpr(PredefinedExpr *E);
448 unsigned VisitDeclRefExpr(DeclRefExpr *E);
449 unsigned VisitIntegerLiteral(IntegerLiteral *E);
450 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000451 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000452 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000453 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000454 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000455 unsigned VisitUnaryOperator(UnaryOperator *E);
456 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000457 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000458 unsigned VisitCallExpr(CallExpr *E);
459 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000460 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000461 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000462 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
463 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000464 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000465 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
466 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000467 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000468 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000469 unsigned VisitInitListExpr(InitListExpr *E);
470 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
471 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000472 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000473 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000474 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000475 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
476 unsigned VisitChooseExpr(ChooseExpr *E);
477 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000478 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000479 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000480 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000481 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000482 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000483 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
484 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000485 };
486}
487
Douglas Gregor025452f2009-04-17 00:04:06 +0000488unsigned PCHStmtReader::VisitStmt(Stmt *S) {
489 assert(Idx == NumStmtFields && "Incorrect statement field count");
490 return 0;
491}
492
493unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
494 VisitStmt(S);
495 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
496 return 0;
497}
498
499unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
500 VisitStmt(S);
501 unsigned NumStmts = Record[Idx++];
502 S->setStmts(Reader.getContext(),
503 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
504 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
505 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
506 return NumStmts;
507}
508
509unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
510 VisitStmt(S);
511 Reader.RecordSwitchCaseID(S, Record[Idx++]);
512 return 0;
513}
514
515unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
516 VisitSwitchCase(S);
517 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
518 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
519 S->setSubStmt(StmtStack.back());
520 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
521 return 3;
522}
523
524unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
525 VisitSwitchCase(S);
526 S->setSubStmt(StmtStack.back());
527 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
528 return 1;
529}
530
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000531unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
532 VisitStmt(S);
533 S->setID(Reader.GetIdentifierInfo(Record, Idx));
534 S->setSubStmt(StmtStack.back());
535 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 Reader.RecordLabelStmt(S, Record[Idx++]);
537 return 1;
538}
539
Douglas Gregor025452f2009-04-17 00:04:06 +0000540unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
541 VisitStmt(S);
542 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
543 S->setThen(StmtStack[StmtStack.size() - 2]);
544 S->setElse(StmtStack[StmtStack.size() - 1]);
545 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
546 return 3;
547}
548
549unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
550 VisitStmt(S);
551 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
552 S->setBody(StmtStack.back());
553 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
554 SwitchCase *PrevSC = 0;
555 for (unsigned N = Record.size(); Idx != N; ++Idx) {
556 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
557 if (PrevSC)
558 PrevSC->setNextSwitchCase(SC);
559 else
560 S->setSwitchCaseList(SC);
561 PrevSC = SC;
562 }
563 return 2;
564}
565
Douglas Gregord921cf92009-04-17 00:16:09 +0000566unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
567 VisitStmt(S);
568 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
569 S->setBody(StmtStack.back());
570 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
571 return 2;
572}
573
Douglas Gregor67d82492009-04-17 00:29:51 +0000574unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
575 VisitStmt(S);
576 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
577 S->setBody(StmtStack.back());
578 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
579 return 2;
580}
581
582unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
583 VisitStmt(S);
584 S->setInit(StmtStack[StmtStack.size() - 4]);
585 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
586 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
587 S->setBody(StmtStack.back());
588 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
589 return 4;
590}
591
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000592unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
593 VisitStmt(S);
594 Reader.SetLabelOf(S, Record[Idx++]);
595 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
596 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
597 return 0;
598}
599
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000600unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
601 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000602 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000603 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
604 return 1;
605}
606
Douglas Gregord921cf92009-04-17 00:16:09 +0000607unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
608 VisitStmt(S);
609 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
610 return 0;
611}
612
Douglas Gregor025452f2009-04-17 00:04:06 +0000613unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
614 VisitStmt(S);
615 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616 return 0;
617}
618
Douglas Gregor0de9d882009-04-17 16:34:57 +0000619unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
620 VisitStmt(S);
621 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
622 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 return 1;
624}
625
Douglas Gregor84f21702009-04-17 16:55:36 +0000626unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
627 VisitStmt(S);
628 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
629 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630
631 if (Idx + 1 == Record.size()) {
632 // Single declaration
633 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
634 } else {
635 llvm::SmallVector<Decl *, 16> Decls;
636 Decls.reserve(Record.size() - Idx);
637 for (unsigned N = Record.size(); Idx != N; ++Idx)
638 Decls.push_back(Reader.GetDecl(Record[Idx]));
639 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
640 &Decls[0], Decls.size())));
641 }
642 return 0;
643}
644
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000645unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
646 VisitStmt(S);
647 unsigned NumOutputs = Record[Idx++];
648 unsigned NumInputs = Record[Idx++];
649 unsigned NumClobbers = Record[Idx++];
650 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
651 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
652 S->setVolatile(Record[Idx++]);
653 S->setSimple(Record[Idx++]);
654
655 unsigned StackIdx
656 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
657 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
658
659 // Outputs and inputs
660 llvm::SmallVector<std::string, 16> Names;
661 llvm::SmallVector<StringLiteral*, 16> Constraints;
662 llvm::SmallVector<Stmt*, 16> Exprs;
663 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
664 Names.push_back(Reader.ReadString(Record, Idx));
665 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
666 Exprs.push_back(StmtStack[StackIdx++]);
667 }
668 S->setOutputsAndInputs(NumOutputs, NumInputs,
669 &Names[0], &Constraints[0], &Exprs[0]);
670
671 // Constraints
672 llvm::SmallVector<StringLiteral*, 16> Clobbers;
673 for (unsigned I = 0; I != NumClobbers; ++I)
674 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
675 S->setClobbers(&Clobbers[0], NumClobbers);
676
677 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
678 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
679}
680
Douglas Gregor087fd532009-04-14 23:32:43 +0000681unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000682 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000683 E->setType(Reader.GetType(Record[Idx++]));
684 E->setTypeDependent(Record[Idx++]);
685 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000686 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000687 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000688}
689
Douglas Gregor087fd532009-04-14 23:32:43 +0000690unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000691 VisitExpr(E);
692 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
693 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000694 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000695}
696
Douglas Gregor087fd532009-04-14 23:32:43 +0000697unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000698 VisitExpr(E);
699 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
700 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000701 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000702}
703
Douglas Gregor087fd532009-04-14 23:32:43 +0000704unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000705 VisitExpr(E);
706 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
707 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000708 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000709}
710
Douglas Gregor087fd532009-04-14 23:32:43 +0000711unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000712 VisitExpr(E);
713 E->setValue(Reader.ReadAPFloat(Record, Idx));
714 E->setExact(Record[Idx++]);
715 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000716 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000717}
718
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000719unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
720 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000721 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000722 return 1;
723}
724
Douglas Gregor673ecd62009-04-15 16:35:07 +0000725unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
726 VisitExpr(E);
727 unsigned Len = Record[Idx++];
728 assert(Record[Idx] == E->getNumConcatenated() &&
729 "Wrong number of concatenated tokens!");
730 ++Idx;
731 E->setWide(Record[Idx++]);
732
733 // Read string data
734 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
735 E->setStrData(Reader.getContext(), &Str[0], Len);
736 Idx += Len;
737
738 // Read source locations
739 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
740 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
741
742 return 0;
743}
744
Douglas Gregor087fd532009-04-14 23:32:43 +0000745unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000746 VisitExpr(E);
747 E->setValue(Record[Idx++]);
748 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
749 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000750 return 0;
751}
752
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000753unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
754 VisitExpr(E);
755 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
756 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000757 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000758 return 1;
759}
760
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000761unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
762 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000763 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000764 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
765 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
766 return 1;
767}
768
769unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
770 VisitExpr(E);
771 E->setSizeof(Record[Idx++]);
772 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000773 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000774 ++Idx;
775 } else {
776 E->setArgument(Reader.GetType(Record[Idx++]));
777 }
778 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
779 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
780 return E->isArgumentType()? 0 : 1;
781}
782
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000783unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
784 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000785 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
786 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000787 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 return 2;
789}
790
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000791unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
792 VisitExpr(E);
793 E->setNumArgs(Reader.getContext(), Record[Idx++]);
794 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000795 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000796 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000797 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000798 return E->getNumArgs() + 1;
799}
800
801unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
802 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000803 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000804 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
805 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 E->setArrow(Record[Idx++]);
807 return 1;
808}
809
Douglas Gregor087fd532009-04-14 23:32:43 +0000810unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
811 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000812 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000813 return 1;
814}
815
Douglas Gregordb600c32009-04-15 00:25:59 +0000816unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
817 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000818 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
819 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000820 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
821 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
822 return 2;
823}
824
Douglas Gregorad90e962009-04-15 22:40:36 +0000825unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
826 VisitBinaryOperator(E);
827 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
828 E->setComputationResultType(Reader.GetType(Record[Idx++]));
829 return 2;
830}
831
832unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
833 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000834 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
835 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
836 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000837 return 3;
838}
839
Douglas Gregor087fd532009-04-14 23:32:43 +0000840unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
841 VisitCastExpr(E);
842 E->setLvalueCast(Record[Idx++]);
843 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000844}
845
Douglas Gregordb600c32009-04-15 00:25:59 +0000846unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
847 VisitCastExpr(E);
848 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
849 return 1;
850}
851
852unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
853 VisitExplicitCastExpr(E);
854 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
855 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
856 return 1;
857}
858
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000859unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
860 VisitExpr(E);
861 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000862 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000863 E->setFileScope(Record[Idx++]);
864 return 1;
865}
866
Douglas Gregord3c98a02009-04-15 23:02:49 +0000867unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
868 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000869 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000870 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
871 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
872 return 1;
873}
874
Douglas Gregord077d752009-04-16 00:55:48 +0000875unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
876 VisitExpr(E);
877 unsigned NumInits = Record[Idx++];
878 E->reserveInits(NumInits);
879 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000880 E->updateInit(I,
881 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
882 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000883 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
884 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
885 E->setInitializedFieldInUnion(
886 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
887 E->sawArrayRangeDesignator(Record[Idx++]);
888 return NumInits + 1;
889}
890
891unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
892 typedef DesignatedInitExpr::Designator Designator;
893
894 VisitExpr(E);
895 unsigned NumSubExprs = Record[Idx++];
896 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
897 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000898 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000899 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
900 E->setGNUSyntax(Record[Idx++]);
901
902 llvm::SmallVector<Designator, 4> Designators;
903 while (Idx < Record.size()) {
904 switch ((pch::DesignatorTypes)Record[Idx++]) {
905 case pch::DESIG_FIELD_DECL: {
906 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
907 SourceLocation DotLoc
908 = SourceLocation::getFromRawEncoding(Record[Idx++]);
909 SourceLocation FieldLoc
910 = SourceLocation::getFromRawEncoding(Record[Idx++]);
911 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
912 FieldLoc));
913 Designators.back().setField(Field);
914 break;
915 }
916
917 case pch::DESIG_FIELD_NAME: {
918 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
919 SourceLocation DotLoc
920 = SourceLocation::getFromRawEncoding(Record[Idx++]);
921 SourceLocation FieldLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
924 break;
925 }
926
927 case pch::DESIG_ARRAY: {
928 unsigned Index = Record[Idx++];
929 SourceLocation LBracketLoc
930 = SourceLocation::getFromRawEncoding(Record[Idx++]);
931 SourceLocation RBracketLoc
932 = SourceLocation::getFromRawEncoding(Record[Idx++]);
933 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
934 break;
935 }
936
937 case pch::DESIG_ARRAY_RANGE: {
938 unsigned Index = Record[Idx++];
939 SourceLocation LBracketLoc
940 = SourceLocation::getFromRawEncoding(Record[Idx++]);
941 SourceLocation EllipsisLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 SourceLocation RBracketLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
946 RBracketLoc));
947 break;
948 }
949 }
950 }
951 E->setDesignators(&Designators[0], Designators.size());
952
953 return NumSubExprs;
954}
955
956unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
957 VisitExpr(E);
958 return 0;
959}
960
Douglas Gregord3c98a02009-04-15 23:02:49 +0000961unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
962 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000963 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000964 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
965 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
966 return 1;
967}
968
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000969unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
970 VisitExpr(E);
971 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
972 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
973 Reader.SetLabelOf(E, Record[Idx++]);
974 return 0;
975}
976
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000977unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
978 VisitExpr(E);
979 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
981 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
982 return 1;
983}
984
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000985unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
986 VisitExpr(E);
987 E->setArgType1(Reader.GetType(Record[Idx++]));
988 E->setArgType2(Reader.GetType(Record[Idx++]));
989 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
990 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
991 return 0;
992}
993
994unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
995 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000996 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
997 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
998 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000999 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1000 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1001 return 3;
1002}
1003
1004unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1005 VisitExpr(E);
1006 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1007 return 0;
1008}
Douglas Gregord3c98a02009-04-15 23:02:49 +00001009
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001010unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1011 VisitExpr(E);
1012 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +00001013 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001014 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1016 return NumExprs;
1017}
1018
Douglas Gregor84af7c22009-04-17 19:21:43 +00001019unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1020 VisitExpr(E);
1021 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1022 E->setHasBlockDeclRefExprs(Record[Idx++]);
1023 return 0;
1024}
1025
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001026unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1027 VisitExpr(E);
1028 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1029 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1030 E->setByRef(Record[Idx++]);
1031 return 0;
1032}
1033
Chris Lattner3a57a372009-04-22 06:29:42 +00001034//===----------------------------------------------------------------------===//
1035// Objective-C Expressions and Statements
1036
1037unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1038 VisitExpr(E);
1039 E->setString(cast<StringLiteral>(StmtStack.back()));
1040 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1041 return 1;
1042}
1043
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001044unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1045 VisitExpr(E);
1046 E->setEncodedType(Reader.GetType(Record[Idx++]));
1047 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1048 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1049 return 0;
1050}
1051
Chris Lattner3a57a372009-04-22 06:29:42 +00001052unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1053 VisitExpr(E);
1054 // FIXME: Selectors.
1055 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1056 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1057 return 0;
1058}
1059
1060unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1061 VisitExpr(E);
1062 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1063 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1064 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1065 return 0;
1066}
1067
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001068
Douglas Gregor668c1a42009-04-21 22:25:48 +00001069//===----------------------------------------------------------------------===//
1070// PCH reader implementation
1071//===----------------------------------------------------------------------===//
1072
1073namespace {
1074class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1075 PCHReader &Reader;
1076
1077 // If we know the IdentifierInfo in advance, it is here and we will
1078 // not build a new one. Used when deserializing information about an
1079 // identifier that was constructed before the PCH file was read.
1080 IdentifierInfo *KnownII;
1081
1082public:
1083 typedef IdentifierInfo * data_type;
1084
1085 typedef const std::pair<const char*, unsigned> external_key_type;
1086
1087 typedef external_key_type internal_key_type;
1088
1089 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1090 : Reader(Reader), KnownII(II) { }
1091
1092 static bool EqualKey(const internal_key_type& a,
1093 const internal_key_type& b) {
1094 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1095 : false;
1096 }
1097
1098 static unsigned ComputeHash(const internal_key_type& a) {
1099 return BernsteinHash(a.first, a.second);
1100 }
1101
1102 // This hopefully will just get inlined and removed by the optimizer.
1103 static const internal_key_type&
1104 GetInternalKey(const external_key_type& x) { return x; }
1105
1106 static std::pair<unsigned, unsigned>
1107 ReadKeyDataLength(const unsigned char*& d) {
1108 using namespace clang::io;
1109 unsigned KeyLen = ReadUnalignedLE16(d);
1110 unsigned DataLen = ReadUnalignedLE16(d);
1111 return std::make_pair(KeyLen, DataLen);
1112 }
1113
1114 static std::pair<const char*, unsigned>
1115 ReadKey(const unsigned char* d, unsigned n) {
1116 assert(n >= 2 && d[n-1] == '\0');
1117 return std::make_pair((const char*) d, n-1);
1118 }
1119
1120 IdentifierInfo *ReadData(const internal_key_type& k,
1121 const unsigned char* d,
1122 unsigned DataLen) {
1123 using namespace clang::io;
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001124 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001125 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1126 Bits >>= 1;
1127 bool Poisoned = Bits & 0x01;
1128 Bits >>= 1;
1129 bool ExtensionToken = Bits & 0x01;
1130 Bits >>= 1;
1131 bool hasMacroDefinition = Bits & 0x01;
1132 Bits >>= 1;
1133 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1134 Bits >>= 10;
1135 unsigned TokenID = Bits & 0xFF;
1136 Bits >>= 8;
1137
Douglas Gregor668c1a42009-04-21 22:25:48 +00001138 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001139 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor668c1a42009-04-21 22:25:48 +00001140 DataLen -= 8;
1141
1142 // Build the IdentifierInfo itself and link the identifier ID with
1143 // the new IdentifierInfo.
1144 IdentifierInfo *II = KnownII;
1145 if (!II)
1146 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1147 k.first, k.first + k.second);
1148 Reader.SetIdentifierInfo(ID, II);
1149
Douglas Gregor2deaea32009-04-22 18:49:13 +00001150 // Set or check the various bits in the IdentifierInfo structure.
1151 // FIXME: Load token IDs lazily, too?
1152 assert((unsigned)II->getTokenID() == TokenID &&
1153 "Incorrect token ID loaded");
1154 (void)TokenID;
1155 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1156 assert(II->isExtensionToken() == ExtensionToken &&
1157 "Incorrect extension token flag");
1158 (void)ExtensionToken;
1159 II->setIsPoisoned(Poisoned);
1160 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1161 "Incorrect C++ operator keyword flag");
1162 (void)CPlusPlusOperatorKeyword;
1163
Douglas Gregor37e26842009-04-21 23:56:24 +00001164 // If this identifier is a macro, deserialize the macro
1165 // definition.
1166 if (hasMacroDefinition) {
1167 uint32_t Offset = ReadUnalignedLE64(d);
1168 Reader.ReadMacroRecord(Offset);
1169 DataLen -= 8;
1170 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001171
1172 // Read all of the declarations visible at global scope with this
1173 // name.
1174 Sema *SemaObj = Reader.getSema();
1175 while (DataLen > 0) {
1176 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001177 if (SemaObj) {
1178 // Introduce this declaration into the translation-unit scope
1179 // and add it to the declaration chain for this identifier, so
1180 // that (unqualified) name lookup will find it.
1181 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1182 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1183 } else {
1184 // Queue this declaration so that it will be added to the
1185 // translation unit scope and identifier's declaration chain
1186 // once a Sema object is known.
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001187 Reader.PreloadedDecls.push_back(D);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001188 }
1189
1190 DataLen -= 4;
1191 }
1192 return II;
1193 }
1194};
1195
1196} // end anonymous namespace
1197
1198/// \brief The on-disk hash table used to contain information about
1199/// all of the identifiers in the program.
1200typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1201 PCHIdentifierLookupTable;
1202
Douglas Gregor2cf26342009-04-09 22:27:44 +00001203// FIXME: use the diagnostics machinery
1204static bool Error(const char *Str) {
1205 std::fprintf(stderr, "%s\n", Str);
1206 return true;
1207}
1208
Douglas Gregore1d918e2009-04-10 23:10:45 +00001209/// \brief Check the contents of the predefines buffer against the
1210/// contents of the predefines buffer used to build the PCH file.
1211///
1212/// The contents of the two predefines buffers should be the same. If
1213/// not, then some command-line option changed the preprocessor state
1214/// and we must reject the PCH file.
1215///
1216/// \param PCHPredef The start of the predefines buffer in the PCH
1217/// file.
1218///
1219/// \param PCHPredefLen The length of the predefines buffer in the PCH
1220/// file.
1221///
1222/// \param PCHBufferID The FileID for the PCH predefines buffer.
1223///
1224/// \returns true if there was a mismatch (in which case the PCH file
1225/// should be ignored), or false otherwise.
1226bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1227 unsigned PCHPredefLen,
1228 FileID PCHBufferID) {
1229 const char *Predef = PP.getPredefines().c_str();
1230 unsigned PredefLen = PP.getPredefines().size();
1231
1232 // If the two predefines buffers compare equal, we're done!.
1233 if (PredefLen == PCHPredefLen &&
1234 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1235 return false;
1236
1237 // The predefines buffers are different. Produce a reasonable
1238 // diagnostic showing where they are different.
1239
1240 // The source locations (potentially in the two different predefines
1241 // buffers)
1242 SourceLocation Loc1, Loc2;
1243 SourceManager &SourceMgr = PP.getSourceManager();
1244
1245 // Create a source buffer for our predefines string, so
1246 // that we can build a diagnostic that points into that
1247 // source buffer.
1248 FileID BufferID;
1249 if (Predef && Predef[0]) {
1250 llvm::MemoryBuffer *Buffer
1251 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1252 "<built-in>");
1253 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1254 }
1255
1256 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1257 std::pair<const char *, const char *> Locations
1258 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1259
1260 if (Locations.first != Predef + MinLen) {
1261 // We found the location in the two buffers where there is a
1262 // difference. Form source locations to point there (in both
1263 // buffers).
1264 unsigned Offset = Locations.first - Predef;
1265 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1266 .getFileLocWithOffset(Offset);
1267 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1268 .getFileLocWithOffset(Offset);
1269 } else if (PredefLen > PCHPredefLen) {
1270 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1271 .getFileLocWithOffset(MinLen);
1272 } else {
1273 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1274 .getFileLocWithOffset(MinLen);
1275 }
1276
1277 Diag(Loc1, diag::warn_pch_preprocessor);
1278 if (Loc2.isValid())
1279 Diag(Loc2, diag::note_predef_in_pch);
1280 Diag(diag::note_ignoring_pch) << FileName;
1281 return true;
1282}
1283
Douglas Gregorbd945002009-04-13 16:31:14 +00001284/// \brief Read the line table in the source manager block.
1285/// \returns true if ther was an error.
1286static bool ParseLineTable(SourceManager &SourceMgr,
1287 llvm::SmallVectorImpl<uint64_t> &Record) {
1288 unsigned Idx = 0;
1289 LineTableInfo &LineTable = SourceMgr.getLineTable();
1290
1291 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001292 std::map<int, int> FileIDs;
1293 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001294 // Extract the file name
1295 unsigned FilenameLen = Record[Idx++];
1296 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1297 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +00001298 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1299 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001300 }
1301
1302 // Parse the line entries
1303 std::vector<LineEntry> Entries;
1304 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +00001305 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +00001306
1307 // Extract the line entries
1308 unsigned NumEntries = Record[Idx++];
1309 Entries.clear();
1310 Entries.reserve(NumEntries);
1311 for (unsigned I = 0; I != NumEntries; ++I) {
1312 unsigned FileOffset = Record[Idx++];
1313 unsigned LineNo = Record[Idx++];
1314 int FilenameID = Record[Idx++];
1315 SrcMgr::CharacteristicKind FileKind
1316 = (SrcMgr::CharacteristicKind)Record[Idx++];
1317 unsigned IncludeOffset = Record[Idx++];
1318 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1319 FileKind, IncludeOffset));
1320 }
1321 LineTable.AddEntry(FID, Entries);
1322 }
1323
1324 return false;
1325}
1326
Douglas Gregor14f79002009-04-10 03:52:48 +00001327/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001328PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001329 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001330 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1331 Error("Malformed source manager block record");
1332 return Failure;
1333 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001334
1335 SourceManager &SourceMgr = Context.getSourceManager();
1336 RecordData Record;
1337 while (true) {
1338 unsigned Code = Stream.ReadCode();
1339 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001340 if (Stream.ReadBlockEnd()) {
1341 Error("Error at end of Source Manager block");
1342 return Failure;
1343 }
1344
1345 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001346 }
1347
1348 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1349 // No known subblocks, always skip them.
1350 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001351 if (Stream.SkipBlock()) {
1352 Error("Malformed block record");
1353 return Failure;
1354 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001355 continue;
1356 }
1357
1358 if (Code == llvm::bitc::DEFINE_ABBREV) {
1359 Stream.ReadAbbrevRecord();
1360 continue;
1361 }
1362
1363 // Read a record.
1364 const char *BlobStart;
1365 unsigned BlobLen;
1366 Record.clear();
1367 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1368 default: // Default behavior: ignore.
1369 break;
1370
1371 case pch::SM_SLOC_FILE_ENTRY: {
1372 // FIXME: We would really like to delay the creation of this
1373 // FileEntry until it is actually required, e.g., when producing
1374 // a diagnostic with a source location in this file.
1375 const FileEntry *File
1376 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1377 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001378 FileID ID = SourceMgr.createFileID(File,
1379 SourceLocation::getFromRawEncoding(Record[1]),
1380 (CharacteristicKind)Record[2]);
1381 if (Record[3])
1382 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1383 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001384 break;
1385 }
1386
1387 case pch::SM_SLOC_BUFFER_ENTRY: {
1388 const char *Name = BlobStart;
1389 unsigned Code = Stream.ReadCode();
1390 Record.clear();
1391 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1392 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001393 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001394 llvm::MemoryBuffer *Buffer
1395 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1396 BlobStart + BlobLen - 1,
1397 Name);
1398 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1399
1400 if (strcmp(Name, "<built-in>") == 0
1401 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1402 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001403 break;
1404 }
1405
1406 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1407 SourceLocation SpellingLoc
1408 = SourceLocation::getFromRawEncoding(Record[1]);
1409 SourceMgr.createInstantiationLoc(
1410 SpellingLoc,
1411 SourceLocation::getFromRawEncoding(Record[2]),
1412 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001413 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001414 break;
1415 }
1416
Chris Lattner2c78b872009-04-14 23:22:57 +00001417 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001418 if (ParseLineTable(SourceMgr, Record))
1419 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001420 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001421 }
1422 }
1423}
1424
Douglas Gregor37e26842009-04-21 23:56:24 +00001425void PCHReader::ReadMacroRecord(uint64_t Offset) {
1426 // Keep track of where we are in the stream, then jump back there
1427 // after reading this macro.
1428 SavedStreamPosition SavedPosition(Stream);
1429
1430 Stream.JumpToBit(Offset);
1431 RecordData Record;
1432 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1433 MacroInfo *Macro = 0;
1434 while (true) {
1435 unsigned Code = Stream.ReadCode();
1436 switch (Code) {
1437 case llvm::bitc::END_BLOCK:
1438 return;
1439
1440 case llvm::bitc::ENTER_SUBBLOCK:
1441 // No known subblocks, always skip them.
1442 Stream.ReadSubBlockID();
1443 if (Stream.SkipBlock()) {
1444 Error("Malformed block record");
1445 return;
1446 }
1447 continue;
1448
1449 case llvm::bitc::DEFINE_ABBREV:
1450 Stream.ReadAbbrevRecord();
1451 continue;
1452 default: break;
1453 }
1454
1455 // Read a record.
1456 Record.clear();
1457 pch::PreprocessorRecordTypes RecType =
1458 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1459 switch (RecType) {
1460 case pch::PP_COUNTER_VALUE:
1461 // Skip this record.
1462 break;
1463
1464 case pch::PP_MACRO_OBJECT_LIKE:
1465 case pch::PP_MACRO_FUNCTION_LIKE: {
1466 // If we already have a macro, that means that we've hit the end
1467 // of the definition of the macro we were looking for. We're
1468 // done.
1469 if (Macro)
1470 return;
1471
1472 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1473 if (II == 0) {
1474 Error("Macro must have a name");
1475 return;
1476 }
1477 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1478 bool isUsed = Record[2];
1479
1480 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1481 MI->setIsUsed(isUsed);
1482
1483 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1484 // Decode function-like macro info.
1485 bool isC99VarArgs = Record[3];
1486 bool isGNUVarArgs = Record[4];
1487 MacroArgs.clear();
1488 unsigned NumArgs = Record[5];
1489 for (unsigned i = 0; i != NumArgs; ++i)
1490 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1491
1492 // Install function-like macro info.
1493 MI->setIsFunctionLike();
1494 if (isC99VarArgs) MI->setIsC99Varargs();
1495 if (isGNUVarArgs) MI->setIsGNUVarargs();
1496 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1497 PP.getPreprocessorAllocator());
1498 }
1499
1500 // Finally, install the macro.
1501 PP.setMacroInfo(II, MI);
1502
1503 // Remember that we saw this macro last so that we add the tokens that
1504 // form its body to it.
1505 Macro = MI;
1506 ++NumMacrosRead;
1507 break;
1508 }
1509
1510 case pch::PP_TOKEN: {
1511 // If we see a TOKEN before a PP_MACRO_*, then the file is
1512 // erroneous, just pretend we didn't see this.
1513 if (Macro == 0) break;
1514
1515 Token Tok;
1516 Tok.startToken();
1517 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1518 Tok.setLength(Record[1]);
1519 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1520 Tok.setIdentifierInfo(II);
1521 Tok.setKind((tok::TokenKind)Record[3]);
1522 Tok.setFlag((Token::TokenFlags)Record[4]);
1523 Macro->AddTokenToBody(Tok);
1524 break;
1525 }
1526 }
1527 }
1528}
1529
Chris Lattner42d42b52009-04-10 21:41:48 +00001530bool PCHReader::ReadPreprocessorBlock() {
1531 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1532 return Error("Malformed preprocessor block record");
1533
Chris Lattner42d42b52009-04-10 21:41:48 +00001534 RecordData Record;
Chris Lattner42d42b52009-04-10 21:41:48 +00001535 while (true) {
1536 unsigned Code = Stream.ReadCode();
1537 switch (Code) {
1538 case llvm::bitc::END_BLOCK:
1539 if (Stream.ReadBlockEnd())
1540 return Error("Error at end of preprocessor block");
1541 return false;
1542
1543 case llvm::bitc::ENTER_SUBBLOCK:
1544 // No known subblocks, always skip them.
1545 Stream.ReadSubBlockID();
1546 if (Stream.SkipBlock())
1547 return Error("Malformed block record");
1548 continue;
1549
1550 case llvm::bitc::DEFINE_ABBREV:
1551 Stream.ReadAbbrevRecord();
1552 continue;
1553 default: break;
1554 }
1555
1556 // Read a record.
1557 Record.clear();
1558 pch::PreprocessorRecordTypes RecType =
1559 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1560 switch (RecType) {
1561 default: // Default behavior: ignore unknown records.
1562 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001563 case pch::PP_COUNTER_VALUE:
1564 if (!Record.empty())
1565 PP.setCounterValue(Record[0]);
1566 break;
1567
Chris Lattner42d42b52009-04-10 21:41:48 +00001568 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregor37e26842009-04-21 23:56:24 +00001569 case pch::PP_MACRO_FUNCTION_LIKE:
1570 case pch::PP_TOKEN:
1571 // Once we've hit a macro definition or a token, we're done.
1572 return false;
Chris Lattner42d42b52009-04-10 21:41:48 +00001573 }
1574 }
1575}
1576
Douglas Gregor668c1a42009-04-21 22:25:48 +00001577PCHReader::PCHReadResult
1578PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001579 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1580 Error("Malformed block record");
1581 return Failure;
1582 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001583
1584 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001585 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001586 while (!Stream.AtEndOfStream()) {
1587 unsigned Code = Stream.ReadCode();
1588 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001589 if (Stream.ReadBlockEnd()) {
1590 Error("Error at end of module block");
1591 return Failure;
1592 }
Chris Lattner7356a312009-04-11 21:15:38 +00001593
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001594 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001595 }
1596
1597 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1598 switch (Stream.ReadSubBlockID()) {
1599 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1600 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1601 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001602 if (Stream.SkipBlock()) {
1603 Error("Malformed block record");
1604 return Failure;
1605 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001606 break;
1607
Chris Lattner7356a312009-04-11 21:15:38 +00001608 case pch::PREPROCESSOR_BLOCK_ID:
1609 // Skip the preprocessor block for now, but remember where it is. We
1610 // want to read it in after the identifier table.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001611 if (PreprocessorBlockOffset) {
Chris Lattner7356a312009-04-11 21:15:38 +00001612 Error("Multiple preprocessor blocks found.");
1613 return Failure;
1614 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001615 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001616 if (Stream.SkipBlock()) {
1617 Error("Malformed block record");
1618 return Failure;
1619 }
1620 break;
1621
Douglas Gregor14f79002009-04-10 03:52:48 +00001622 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001623 switch (ReadSourceManagerBlock()) {
1624 case Success:
1625 break;
1626
1627 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001628 Error("Malformed source manager block");
1629 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001630
1631 case IgnorePCH:
1632 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001633 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001634 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001635 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001636 continue;
1637 }
1638
1639 if (Code == llvm::bitc::DEFINE_ABBREV) {
1640 Stream.ReadAbbrevRecord();
1641 continue;
1642 }
1643
1644 // Read and process a record.
1645 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001646 const char *BlobStart = 0;
1647 unsigned BlobLen = 0;
1648 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1649 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001650 default: // Default behavior: ignore.
1651 break;
1652
1653 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001654 if (!TypeOffsets.empty()) {
1655 Error("Duplicate TYPE_OFFSET record in PCH file");
1656 return Failure;
1657 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001658 TypeOffsets.swap(Record);
1659 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1660 break;
1661
1662 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001663 if (!DeclOffsets.empty()) {
1664 Error("Duplicate DECL_OFFSET record in PCH file");
1665 return Failure;
1666 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001667 DeclOffsets.swap(Record);
1668 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1669 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001670
1671 case pch::LANGUAGE_OPTIONS:
1672 if (ParseLanguageOptions(Record))
1673 return IgnorePCH;
1674 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001675
Douglas Gregorafaf3082009-04-11 00:14:32 +00001676 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001677 std::string TargetTriple(BlobStart, BlobLen);
1678 if (TargetTriple != Context.Target.getTargetTriple()) {
1679 Diag(diag::warn_pch_target_triple)
1680 << TargetTriple << Context.Target.getTargetTriple();
1681 Diag(diag::note_ignoring_pch) << FileName;
1682 return IgnorePCH;
1683 }
1684 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001685 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001686
1687 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001688 IdentifierTableData = BlobStart;
1689 IdentifierLookupTable
1690 = PCHIdentifierLookupTable::Create(
1691 (const unsigned char *)IdentifierTableData + Record[0],
1692 (const unsigned char *)IdentifierTableData,
1693 PCHIdentifierLookupTrait(*this));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001694 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001695 break;
1696
1697 case pch::IDENTIFIER_OFFSET:
1698 if (!IdentifierData.empty()) {
1699 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1700 return Failure;
1701 }
1702 IdentifierData.swap(Record);
1703#ifndef NDEBUG
1704 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1705 if ((IdentifierData[I] & 0x01) == 0) {
1706 Error("Malformed identifier table in the precompiled header");
1707 return Failure;
1708 }
1709 }
1710#endif
1711 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001712
1713 case pch::EXTERNAL_DEFINITIONS:
1714 if (!ExternalDefinitions.empty()) {
1715 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1716 return Failure;
1717 }
1718 ExternalDefinitions.swap(Record);
1719 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001720
Douglas Gregorad1de002009-04-18 05:55:16 +00001721 case pch::SPECIAL_TYPES:
1722 SpecialTypes.swap(Record);
1723 break;
1724
Douglas Gregor3e1af842009-04-17 22:13:46 +00001725 case pch::STATISTICS:
1726 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001727 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001728 TotalLexicalDeclContexts = Record[2];
1729 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001730 break;
1731
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001732 case pch::TENTATIVE_DEFINITIONS:
1733 if (!TentativeDefinitions.empty()) {
1734 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1735 return Failure;
1736 }
1737 TentativeDefinitions.swap(Record);
1738 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001739
1740 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1741 if (!LocallyScopedExternalDecls.empty()) {
1742 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1743 return Failure;
1744 }
1745 LocallyScopedExternalDecls.swap(Record);
1746 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001747 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001748 }
1749
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001750 Error("Premature end of bitstream");
1751 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001752}
1753
Douglas Gregore1d918e2009-04-10 23:10:45 +00001754PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001755 // Set the PCH file name.
1756 this->FileName = FileName;
1757
Douglas Gregor2cf26342009-04-09 22:27:44 +00001758 // Open the PCH file.
1759 std::string ErrStr;
1760 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001761 if (!Buffer) {
1762 Error(ErrStr.c_str());
1763 return IgnorePCH;
1764 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001765
1766 // Initialize the stream
1767 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1768 (const unsigned char *)Buffer->getBufferEnd());
1769
1770 // Sniff for the signature.
1771 if (Stream.Read(8) != 'C' ||
1772 Stream.Read(8) != 'P' ||
1773 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001774 Stream.Read(8) != 'H') {
1775 Error("Not a PCH file");
1776 return IgnorePCH;
1777 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001778
1779 // We expect a number of well-defined blocks, though we don't necessarily
1780 // need to understand them all.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001781 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001782 while (!Stream.AtEndOfStream()) {
1783 unsigned Code = Stream.ReadCode();
1784
Douglas Gregore1d918e2009-04-10 23:10:45 +00001785 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1786 Error("Invalid record at top-level");
1787 return Failure;
1788 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001789
1790 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001791
Douglas Gregor2cf26342009-04-09 22:27:44 +00001792 // We only know the PCH subblock ID.
1793 switch (BlockID) {
1794 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001795 if (Stream.ReadBlockInfoBlock()) {
1796 Error("Malformed BlockInfoBlock");
1797 return Failure;
1798 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001799 break;
1800 case pch::PCH_BLOCK_ID:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001801 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001802 case Success:
1803 break;
1804
1805 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001806 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001807
1808 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001809 // FIXME: We could consider reading through to the end of this
1810 // PCH block, skipping subblocks, to see if there are other
1811 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001812 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001813 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001814 break;
1815 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001816 if (Stream.SkipBlock()) {
1817 Error("Malformed block record");
1818 return Failure;
1819 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001820 break;
1821 }
1822 }
1823
1824 // Load the translation unit declaration
1825 ReadDeclRecord(DeclOffsets[0], 0);
1826
Douglas Gregor668c1a42009-04-21 22:25:48 +00001827 // Initialization of builtins and library builtins occurs before the
1828 // PCH file is read, so there may be some identifiers that were
1829 // loaded into the IdentifierTable before we intercepted the
1830 // creation of identifiers. Iterate through the list of known
1831 // identifiers and determine whether we have to establish
1832 // preprocessor definitions or top-level identifier declaration
1833 // chains for those identifiers.
1834 //
1835 // We copy the IdentifierInfo pointers to a small vector first,
1836 // since de-serializing declarations or macro definitions can add
1837 // new entries into the identifier table, invalidating the
1838 // iterators.
1839 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1840 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1841 IdEnd = PP.getIdentifierTable().end();
1842 Id != IdEnd; ++Id)
1843 Identifiers.push_back(Id->second);
1844 PCHIdentifierLookupTable *IdTable
1845 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1846 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1847 IdentifierInfo *II = Identifiers[I];
1848 // Look in the on-disk hash table for an entry for
1849 PCHIdentifierLookupTrait Info(*this, II);
1850 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1851 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1852 if (Pos == IdTable->end())
1853 continue;
1854
1855 // Dereferencing the iterator has the effect of populating the
1856 // IdentifierInfo node with the various declarations it needs.
1857 (void)*Pos;
1858 }
1859
Douglas Gregorad1de002009-04-18 05:55:16 +00001860 // Load the special types.
1861 Context.setBuiltinVaListType(
1862 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1863
Douglas Gregor668c1a42009-04-21 22:25:48 +00001864 // If we saw the preprocessor block, read it now.
1865 if (PreprocessorBlockOffset) {
1866 SavedStreamPosition SavedPos(Stream);
1867 Stream.JumpToBit(PreprocessorBlockOffset);
1868 if (ReadPreprocessorBlock()) {
1869 Error("Malformed preprocessor block");
1870 return Failure;
Douglas Gregor0b748912009-04-14 21:18:50 +00001871 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001872 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001873
Douglas Gregor668c1a42009-04-21 22:25:48 +00001874 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001875}
1876
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001877/// \brief Parse the record that corresponds to a LangOptions data
1878/// structure.
1879///
1880/// This routine compares the language options used to generate the
1881/// PCH file against the language options set for the current
1882/// compilation. For each option, we classify differences between the
1883/// two compiler states as either "benign" or "important". Benign
1884/// differences don't matter, and we accept them without complaint
1885/// (and without modifying the language options). Differences between
1886/// the states for important options cause the PCH file to be
1887/// unusable, so we emit a warning and return true to indicate that
1888/// there was an error.
1889///
1890/// \returns true if the PCH file is unacceptable, false otherwise.
1891bool PCHReader::ParseLanguageOptions(
1892 const llvm::SmallVectorImpl<uint64_t> &Record) {
1893 const LangOptions &LangOpts = Context.getLangOptions();
1894#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1895#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1896 if (Record[Idx] != LangOpts.Option) { \
1897 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1898 Diag(diag::note_ignoring_pch) << FileName; \
1899 return true; \
1900 } \
1901 ++Idx
1902
1903 unsigned Idx = 0;
1904 PARSE_LANGOPT_BENIGN(Trigraphs);
1905 PARSE_LANGOPT_BENIGN(BCPLComment);
1906 PARSE_LANGOPT_BENIGN(DollarIdents);
1907 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1908 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1909 PARSE_LANGOPT_BENIGN(ImplicitInt);
1910 PARSE_LANGOPT_BENIGN(Digraphs);
1911 PARSE_LANGOPT_BENIGN(HexFloats);
1912 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1913 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1914 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1915 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1916 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1917 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1918 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1919 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1920 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1921 PARSE_LANGOPT_BENIGN(PascalStrings);
1922 PARSE_LANGOPT_BENIGN(Boolean);
1923 PARSE_LANGOPT_BENIGN(WritableStrings);
1924 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1925 diag::warn_pch_lax_vector_conversions);
1926 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1927 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1928 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1929 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1930 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1931 diag::warn_pch_thread_safe_statics);
1932 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1933 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1934 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1935 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1936 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1937 diag::warn_pch_heinous_extensions);
1938 // FIXME: Most of the options below are benign if the macro wasn't
1939 // used. Unfortunately, this means that a PCH compiled without
1940 // optimization can't be used with optimization turned on, even
1941 // though the only thing that changes is whether __OPTIMIZE__ was
1942 // defined... but if __OPTIMIZE__ never showed up in the header, it
1943 // doesn't matter. We could consider making this some special kind
1944 // of check.
1945 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1946 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1947 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1948 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1949 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1950 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1951 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1952 Diag(diag::warn_pch_gc_mode)
1953 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1954 Diag(diag::note_ignoring_pch) << FileName;
1955 return true;
1956 }
1957 ++Idx;
1958 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1959 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1960#undef PARSE_LANGOPT_IRRELEVANT
1961#undef PARSE_LANGOPT_BENIGN
1962
1963 return false;
1964}
1965
Douglas Gregor2cf26342009-04-09 22:27:44 +00001966/// \brief Read and return the type at the given offset.
1967///
1968/// This routine actually reads the record corresponding to the type
1969/// at the given offset in the bitstream. It is a helper routine for
1970/// GetType, which deals with reading type IDs.
1971QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001972 // Keep track of where we are in the stream, then jump back there
1973 // after reading this type.
1974 SavedStreamPosition SavedPosition(Stream);
1975
Douglas Gregor2cf26342009-04-09 22:27:44 +00001976 Stream.JumpToBit(Offset);
1977 RecordData Record;
1978 unsigned Code = Stream.ReadCode();
1979 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001980 case pch::TYPE_EXT_QUAL: {
1981 assert(Record.size() == 3 &&
1982 "Incorrect encoding of extended qualifier type");
1983 QualType Base = GetType(Record[0]);
1984 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1985 unsigned AddressSpace = Record[2];
1986
1987 QualType T = Base;
1988 if (GCAttr != QualType::GCNone)
1989 T = Context.getObjCGCQualType(T, GCAttr);
1990 if (AddressSpace)
1991 T = Context.getAddrSpaceQualType(T, AddressSpace);
1992 return T;
1993 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001994
Douglas Gregor2cf26342009-04-09 22:27:44 +00001995 case pch::TYPE_FIXED_WIDTH_INT: {
1996 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1997 return Context.getFixedWidthIntType(Record[0], Record[1]);
1998 }
1999
2000 case pch::TYPE_COMPLEX: {
2001 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2002 QualType ElemType = GetType(Record[0]);
2003 return Context.getComplexType(ElemType);
2004 }
2005
2006 case pch::TYPE_POINTER: {
2007 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2008 QualType PointeeType = GetType(Record[0]);
2009 return Context.getPointerType(PointeeType);
2010 }
2011
2012 case pch::TYPE_BLOCK_POINTER: {
2013 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2014 QualType PointeeType = GetType(Record[0]);
2015 return Context.getBlockPointerType(PointeeType);
2016 }
2017
2018 case pch::TYPE_LVALUE_REFERENCE: {
2019 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2020 QualType PointeeType = GetType(Record[0]);
2021 return Context.getLValueReferenceType(PointeeType);
2022 }
2023
2024 case pch::TYPE_RVALUE_REFERENCE: {
2025 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2026 QualType PointeeType = GetType(Record[0]);
2027 return Context.getRValueReferenceType(PointeeType);
2028 }
2029
2030 case pch::TYPE_MEMBER_POINTER: {
2031 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2032 QualType PointeeType = GetType(Record[0]);
2033 QualType ClassType = GetType(Record[1]);
2034 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2035 }
2036
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002037 case pch::TYPE_CONSTANT_ARRAY: {
2038 QualType ElementType = GetType(Record[0]);
2039 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2040 unsigned IndexTypeQuals = Record[2];
2041 unsigned Idx = 3;
2042 llvm::APInt Size = ReadAPInt(Record, Idx);
2043 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2044 }
2045
2046 case pch::TYPE_INCOMPLETE_ARRAY: {
2047 QualType ElementType = GetType(Record[0]);
2048 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2049 unsigned IndexTypeQuals = Record[2];
2050 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2051 }
2052
2053 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002054 QualType ElementType = GetType(Record[0]);
2055 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2056 unsigned IndexTypeQuals = Record[2];
2057 return Context.getVariableArrayType(ElementType, ReadExpr(),
2058 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002059 }
2060
2061 case pch::TYPE_VECTOR: {
2062 if (Record.size() != 2) {
2063 Error("Incorrect encoding of vector type in PCH file");
2064 return QualType();
2065 }
2066
2067 QualType ElementType = GetType(Record[0]);
2068 unsigned NumElements = Record[1];
2069 return Context.getVectorType(ElementType, NumElements);
2070 }
2071
2072 case pch::TYPE_EXT_VECTOR: {
2073 if (Record.size() != 2) {
2074 Error("Incorrect encoding of extended vector type in PCH file");
2075 return QualType();
2076 }
2077
2078 QualType ElementType = GetType(Record[0]);
2079 unsigned NumElements = Record[1];
2080 return Context.getExtVectorType(ElementType, NumElements);
2081 }
2082
2083 case pch::TYPE_FUNCTION_NO_PROTO: {
2084 if (Record.size() != 1) {
2085 Error("Incorrect encoding of no-proto function type");
2086 return QualType();
2087 }
2088 QualType ResultType = GetType(Record[0]);
2089 return Context.getFunctionNoProtoType(ResultType);
2090 }
2091
2092 case pch::TYPE_FUNCTION_PROTO: {
2093 QualType ResultType = GetType(Record[0]);
2094 unsigned Idx = 1;
2095 unsigned NumParams = Record[Idx++];
2096 llvm::SmallVector<QualType, 16> ParamTypes;
2097 for (unsigned I = 0; I != NumParams; ++I)
2098 ParamTypes.push_back(GetType(Record[Idx++]));
2099 bool isVariadic = Record[Idx++];
2100 unsigned Quals = Record[Idx++];
2101 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2102 isVariadic, Quals);
2103 }
2104
2105 case pch::TYPE_TYPEDEF:
2106 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2107 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2108
2109 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00002110 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002111
2112 case pch::TYPE_TYPEOF: {
2113 if (Record.size() != 1) {
2114 Error("Incorrect encoding of typeof(type) in PCH file");
2115 return QualType();
2116 }
2117 QualType UnderlyingType = GetType(Record[0]);
2118 return Context.getTypeOfType(UnderlyingType);
2119 }
2120
2121 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00002122 assert(Record.size() == 1 && "Incorrect encoding of record type");
2123 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002124
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002125 case pch::TYPE_ENUM:
2126 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2127 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2128
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002129 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner4dcf151a2009-04-22 05:57:30 +00002130 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2131 return Context.getObjCInterfaceType(
2132 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002133
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002134 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2135 unsigned Idx = 0;
2136 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2137 unsigned NumProtos = Record[Idx++];
2138 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2139 for (unsigned I = 0; I != NumProtos; ++I)
2140 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2141 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2142 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002143
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002144 case pch::TYPE_OBJC_QUALIFIED_ID: {
2145 unsigned Idx = 0;
2146 unsigned NumProtos = Record[Idx++];
2147 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2148 for (unsigned I = 0; I != NumProtos; ++I)
2149 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2150 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2151 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002152 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002153 // Suppress a GCC warning
2154 return QualType();
2155}
2156
2157/// \brief Note that we have loaded the declaration with the given
2158/// Index.
2159///
2160/// This routine notes that this declaration has already been loaded,
2161/// so that future GetDecl calls will return this declaration rather
2162/// than trying to load a new declaration.
2163inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2164 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2165 DeclAlreadyLoaded[Index] = true;
2166 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2167}
2168
2169/// \brief Read the declaration at the given offset from the PCH file.
2170Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002171 // Keep track of where we are in the stream, then jump back there
2172 // after reading this declaration.
2173 SavedStreamPosition SavedPosition(Stream);
2174
Douglas Gregor2cf26342009-04-09 22:27:44 +00002175 Decl *D = 0;
2176 Stream.JumpToBit(Offset);
2177 RecordData Record;
2178 unsigned Code = Stream.ReadCode();
2179 unsigned Idx = 0;
2180 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00002181
Douglas Gregor2cf26342009-04-09 22:27:44 +00002182 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002183 case pch::DECL_ATTR:
2184 case pch::DECL_CONTEXT_LEXICAL:
2185 case pch::DECL_CONTEXT_VISIBLE:
2186 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2187 break;
2188
Douglas Gregor2cf26342009-04-09 22:27:44 +00002189 case pch::DECL_TRANSLATION_UNIT:
2190 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00002191 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002192 break;
2193
2194 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002195 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002196 break;
2197 }
2198
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002199 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002200 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002201 break;
2202 }
2203
Douglas Gregor8c700062009-04-13 21:20:57 +00002204 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002205 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2206 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00002207 break;
2208 }
2209
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002210 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002211 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2212 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002213 break;
2214 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002215
2216 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002217 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2218 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002219 break;
2220 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002221
Steve Naroff53c9d8a2009-04-20 15:06:07 +00002222 case pch::DECL_OBJC_METHOD: {
2223 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2224 Selector(), QualType(), 0);
2225 break;
2226 }
2227
Steve Naroff30833f82009-04-21 15:12:33 +00002228 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002229 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2230 break;
2231 }
2232
Steve Naroff30833f82009-04-21 15:12:33 +00002233 case pch::DECL_OBJC_IVAR: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002234 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2235 ObjCIvarDecl::None);
2236 break;
2237 }
2238
Steve Naroff30833f82009-04-21 15:12:33 +00002239 case pch::DECL_OBJC_PROTOCOL: {
2240 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2241 break;
2242 }
2243
2244 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2245 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2246 QualType(), 0);
2247 break;
2248 }
2249
2250 case pch::DECL_OBJC_CLASS: {
2251 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2252 break;
2253 }
2254
2255 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2256 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2257 break;
2258 }
2259
2260 case pch::DECL_OBJC_CATEGORY: {
2261 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2262 break;
2263 }
2264
2265 case pch::DECL_OBJC_CATEGORY_IMPL: {
2266 // FIXME: Implement.
2267 break;
2268 }
2269
2270 case pch::DECL_OBJC_IMPLEMENTATION: {
2271 // FIXME: Implement.
2272 break;
2273 }
2274
2275 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2276 // FIXME: Implement.
2277 break;
2278 }
2279
2280 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor70e5a142009-04-22 23:20:34 +00002281 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff30833f82009-04-21 15:12:33 +00002282 break;
2283 }
2284
2285 case pch::DECL_OBJC_PROPERTY_IMPL: {
2286 // FIXME: Implement.
2287 break;
2288 }
2289
Douglas Gregor8c700062009-04-13 21:20:57 +00002290 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002291 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2292 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00002293 break;
2294 }
2295
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002297 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2298 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002299 break;
2300 }
2301
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002302 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002303 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2304 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002305 break;
2306 }
2307
2308 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002309 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002310 QualType(), QualType(), VarDecl::None,
2311 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002312 break;
2313 }
2314
Douglas Gregor1028bc62009-04-13 22:49:25 +00002315 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002316 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00002317 break;
2318 }
2319
2320 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002321 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00002322 break;
2323 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002324 }
2325
Douglas Gregor668c1a42009-04-21 22:25:48 +00002326 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002327 if (D) {
2328 LoadedDecl(Index, D);
2329 Reader.Visit(D);
2330 }
2331
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332 // If this declaration is also a declaration context, get the
2333 // offsets for its tables of lexical and visible declarations.
2334 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2335 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2336 if (Offsets.first || Offsets.second) {
2337 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2338 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2339 DeclContextOffsets[DC] = Offsets;
2340 }
2341 }
2342 assert(Idx == Record.size());
2343
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002344 if (Consumer) {
2345 // If we have deserialized a declaration that has a definition the
2346 // AST consumer might need to know about, notify the consumer
2347 // about that definition now.
2348 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2349 if (Var->isFileVarDecl() && Var->getInit()) {
2350 DeclGroupRef DG(Var);
2351 Consumer->HandleTopLevelDecl(DG);
2352 }
2353 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2354 if (Func->isThisDeclarationADefinition()) {
2355 DeclGroupRef DG(Func);
2356 Consumer->HandleTopLevelDecl(DG);
2357 }
2358 }
2359 }
2360
Douglas Gregor2cf26342009-04-09 22:27:44 +00002361 return D;
2362}
2363
Douglas Gregor8038d512009-04-10 17:25:41 +00002364QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002365 unsigned Quals = ID & 0x07;
2366 unsigned Index = ID >> 3;
2367
2368 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2369 QualType T;
2370 switch ((pch::PredefinedTypeIDs)Index) {
2371 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2372 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2373 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2374
2375 case pch::PREDEF_TYPE_CHAR_U_ID:
2376 case pch::PREDEF_TYPE_CHAR_S_ID:
2377 // FIXME: Check that the signedness of CharTy is correct!
2378 T = Context.CharTy;
2379 break;
2380
2381 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2382 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2383 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2384 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2385 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2386 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2387 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2388 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2389 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2390 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2391 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2392 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2393 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2394 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2395 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2396 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2397 }
2398
2399 assert(!T.isNull() && "Unknown predefined type");
2400 return T.getQualifiedType(Quals);
2401 }
2402
2403 Index -= pch::NUM_PREDEF_TYPE_IDS;
2404 if (!TypeAlreadyLoaded[Index]) {
2405 // Load the type from the PCH file.
2406 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2407 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2408 TypeAlreadyLoaded[Index] = true;
2409 }
2410
2411 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2412}
2413
Douglas Gregor8038d512009-04-10 17:25:41 +00002414Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002415 if (ID == 0)
2416 return 0;
2417
2418 unsigned Index = ID - 1;
2419 if (DeclAlreadyLoaded[Index])
2420 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2421
2422 // Load the declaration from the PCH file.
2423 return ReadDeclRecord(DeclOffsets[Index], Index);
2424}
2425
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002426Stmt *PCHReader::GetStmt(uint64_t Offset) {
2427 // Keep track of where we are in the stream, then jump back there
2428 // after reading this declaration.
2429 SavedStreamPosition SavedPosition(Stream);
2430
2431 Stream.JumpToBit(Offset);
2432 return ReadStmt();
2433}
2434
Douglas Gregor2cf26342009-04-09 22:27:44 +00002435bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002436 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002437 assert(DC->hasExternalLexicalStorage() &&
2438 "DeclContext has no lexical decls in storage");
2439 uint64_t Offset = DeclContextOffsets[DC].first;
2440 assert(Offset && "DeclContext has no lexical decls in storage");
2441
Douglas Gregor0b748912009-04-14 21:18:50 +00002442 // Keep track of where we are in the stream, then jump back there
2443 // after reading this context.
2444 SavedStreamPosition SavedPosition(Stream);
2445
Douglas Gregor2cf26342009-04-09 22:27:44 +00002446 // Load the record containing all of the declarations lexically in
2447 // this context.
2448 Stream.JumpToBit(Offset);
2449 RecordData Record;
2450 unsigned Code = Stream.ReadCode();
2451 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002452 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002453 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2454
2455 // Load all of the declaration IDs
2456 Decls.clear();
2457 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002458 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002459 return false;
2460}
2461
2462bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2463 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2464 assert(DC->hasExternalVisibleStorage() &&
2465 "DeclContext has no visible decls in storage");
2466 uint64_t Offset = DeclContextOffsets[DC].second;
2467 assert(Offset && "DeclContext has no visible decls in storage");
2468
Douglas Gregor0b748912009-04-14 21:18:50 +00002469 // Keep track of where we are in the stream, then jump back there
2470 // after reading this context.
2471 SavedStreamPosition SavedPosition(Stream);
2472
Douglas Gregor2cf26342009-04-09 22:27:44 +00002473 // Load the record containing all of the declarations visible in
2474 // this context.
2475 Stream.JumpToBit(Offset);
2476 RecordData Record;
2477 unsigned Code = Stream.ReadCode();
2478 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002479 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002480 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2481 if (Record.size() == 0)
2482 return false;
2483
2484 Decls.clear();
2485
2486 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002487 while (Idx < Record.size()) {
2488 Decls.push_back(VisibleDeclaration());
2489 Decls.back().Name = ReadDeclarationName(Record, Idx);
2490
Douglas Gregor2cf26342009-04-09 22:27:44 +00002491 unsigned Size = Record[Idx++];
2492 llvm::SmallVector<unsigned, 4> & LoadedDecls
2493 = Decls.back().Declarations;
2494 LoadedDecls.reserve(Size);
2495 for (unsigned I = 0; I < Size; ++I)
2496 LoadedDecls.push_back(Record[Idx++]);
2497 }
2498
Douglas Gregor25123082009-04-22 22:34:57 +00002499 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500 return false;
2501}
2502
Douglas Gregorfdd01722009-04-14 00:24:19 +00002503void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002504 this->Consumer = Consumer;
2505
Douglas Gregorfdd01722009-04-14 00:24:19 +00002506 if (!Consumer)
2507 return;
2508
2509 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2510 Decl *D = GetDecl(ExternalDefinitions[I]);
2511 DeclGroupRef DG(D);
2512 Consumer->HandleTopLevelDecl(DG);
2513 }
2514}
2515
Douglas Gregor2cf26342009-04-09 22:27:44 +00002516void PCHReader::PrintStats() {
2517 std::fprintf(stderr, "*** PCH Statistics:\n");
2518
2519 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2520 TypeAlreadyLoaded.end(),
2521 true);
2522 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2523 DeclAlreadyLoaded.end(),
2524 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002525 unsigned NumIdentifiersLoaded = 0;
2526 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2527 if ((IdentifierData[I] & 0x01) == 0)
2528 ++NumIdentifiersLoaded;
2529 }
2530
Douglas Gregor2cf26342009-04-09 22:27:44 +00002531 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2532 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002533 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002534 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2535 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002536 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2537 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2538 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2539 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002540 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2541 NumStatementsRead, TotalNumStatements,
2542 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor37e26842009-04-21 23:56:24 +00002543 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2544 NumMacrosRead, TotalNumMacros,
2545 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregor25123082009-04-22 22:34:57 +00002546 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2547 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2548 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2549 * 100));
2550 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2551 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2552 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2553 * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002554 std::fprintf(stderr, "\n");
2555}
2556
Douglas Gregor668c1a42009-04-21 22:25:48 +00002557void PCHReader::InitializeSema(Sema &S) {
2558 SemaObj = &S;
2559
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002560 // Makes sure any declarations that were deserialized "too early"
2561 // still get added to the identifier's declaration chains.
2562 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2563 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2564 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002565 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002566 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002567
2568 // If there were any tentative definitions, deserialize them and add
2569 // them to Sema's table of tentative definitions.
2570 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2571 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2572 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2573 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002574
2575 // If there were any locally-scoped external declarations,
2576 // deserialize them and add them to Sema's table of locally-scoped
2577 // external declarations.
2578 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2579 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2580 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2581 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002582}
2583
2584IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2585 // Try to find this name within our on-disk hash table
2586 PCHIdentifierLookupTable *IdTable
2587 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2588 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2589 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2590 if (Pos == IdTable->end())
2591 return 0;
2592
2593 // Dereferencing the iterator has the effect of building the
2594 // IdentifierInfo node and populating it with the various
2595 // declarations it needs.
2596 return *Pos;
2597}
2598
2599void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2600 assert(ID && "Non-zero identifier ID required");
2601 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2602}
2603
Chris Lattner7356a312009-04-11 21:15:38 +00002604IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002605 if (ID == 0)
2606 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002607
Douglas Gregor668c1a42009-04-21 22:25:48 +00002608 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002609 Error("No identifier table in PCH file");
2610 return 0;
2611 }
Chris Lattner7356a312009-04-11 21:15:38 +00002612
Douglas Gregorafaf3082009-04-11 00:14:32 +00002613 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002614 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002615 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregor668c1a42009-04-21 22:25:48 +00002616 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002617 }
Chris Lattner7356a312009-04-11 21:15:38 +00002618
2619 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002620}
2621
2622DeclarationName
2623PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2624 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2625 switch (Kind) {
2626 case DeclarationName::Identifier:
2627 return DeclarationName(GetIdentifierInfo(Record, Idx));
2628
2629 case DeclarationName::ObjCZeroArgSelector:
2630 case DeclarationName::ObjCOneArgSelector:
2631 case DeclarationName::ObjCMultiArgSelector:
2632 assert(false && "Unable to de-serialize Objective-C selectors");
2633 break;
2634
2635 case DeclarationName::CXXConstructorName:
2636 return Context.DeclarationNames.getCXXConstructorName(
2637 GetType(Record[Idx++]));
2638
2639 case DeclarationName::CXXDestructorName:
2640 return Context.DeclarationNames.getCXXDestructorName(
2641 GetType(Record[Idx++]));
2642
2643 case DeclarationName::CXXConversionFunctionName:
2644 return Context.DeclarationNames.getCXXConversionFunctionName(
2645 GetType(Record[Idx++]));
2646
2647 case DeclarationName::CXXOperatorName:
2648 return Context.DeclarationNames.getCXXOperatorName(
2649 (OverloadedOperatorKind)Record[Idx++]);
2650
2651 case DeclarationName::CXXUsingDirective:
2652 return DeclarationName::getUsingDirectiveName();
2653 }
2654
2655 // Required to silence GCC warning
2656 return DeclarationName();
2657}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002658
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002659/// \brief Read an integral value
2660llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2661 unsigned BitWidth = Record[Idx++];
2662 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2663 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2664 Idx += NumWords;
2665 return Result;
2666}
2667
2668/// \brief Read a signed integral value
2669llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2670 bool isUnsigned = Record[Idx++];
2671 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2672}
2673
Douglas Gregor17fc2232009-04-14 21:55:33 +00002674/// \brief Read a floating-point value
2675llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002676 return llvm::APFloat(ReadAPInt(Record, Idx));
2677}
2678
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002679// \brief Read a string
2680std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2681 unsigned Len = Record[Idx++];
2682 std::string Result(&Record[Idx], &Record[Idx] + Len);
2683 Idx += Len;
2684 return Result;
2685}
2686
2687/// \brief Reads attributes from the current stream position.
2688Attr *PCHReader::ReadAttributes() {
2689 unsigned Code = Stream.ReadCode();
2690 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2691 "Expected unabbreviated record"); (void)Code;
2692
2693 RecordData Record;
2694 unsigned Idx = 0;
2695 unsigned RecCode = Stream.ReadRecord(Code, Record);
2696 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2697 (void)RecCode;
2698
2699#define SIMPLE_ATTR(Name) \
2700 case Attr::Name: \
2701 New = ::new (Context) Name##Attr(); \
2702 break
2703
2704#define STRING_ATTR(Name) \
2705 case Attr::Name: \
2706 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2707 break
2708
2709#define UNSIGNED_ATTR(Name) \
2710 case Attr::Name: \
2711 New = ::new (Context) Name##Attr(Record[Idx++]); \
2712 break
2713
2714 Attr *Attrs = 0;
2715 while (Idx < Record.size()) {
2716 Attr *New = 0;
2717 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2718 bool IsInherited = Record[Idx++];
2719
2720 switch (Kind) {
2721 STRING_ATTR(Alias);
2722 UNSIGNED_ATTR(Aligned);
2723 SIMPLE_ATTR(AlwaysInline);
2724 SIMPLE_ATTR(AnalyzerNoReturn);
2725 STRING_ATTR(Annotate);
2726 STRING_ATTR(AsmLabel);
2727
2728 case Attr::Blocks:
2729 New = ::new (Context) BlocksAttr(
2730 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2731 break;
2732
2733 case Attr::Cleanup:
2734 New = ::new (Context) CleanupAttr(
2735 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2736 break;
2737
2738 SIMPLE_ATTR(Const);
2739 UNSIGNED_ATTR(Constructor);
2740 SIMPLE_ATTR(DLLExport);
2741 SIMPLE_ATTR(DLLImport);
2742 SIMPLE_ATTR(Deprecated);
2743 UNSIGNED_ATTR(Destructor);
2744 SIMPLE_ATTR(FastCall);
2745
2746 case Attr::Format: {
2747 std::string Type = ReadString(Record, Idx);
2748 unsigned FormatIdx = Record[Idx++];
2749 unsigned FirstArg = Record[Idx++];
2750 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2751 break;
2752 }
2753
Chris Lattnercf2a7212009-04-20 19:12:28 +00002754 SIMPLE_ATTR(GNUInline);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002755
2756 case Attr::IBOutletKind:
2757 New = ::new (Context) IBOutletAttr();
2758 break;
2759
2760 SIMPLE_ATTR(NoReturn);
2761 SIMPLE_ATTR(NoThrow);
2762 SIMPLE_ATTR(Nodebug);
2763 SIMPLE_ATTR(Noinline);
2764
2765 case Attr::NonNull: {
2766 unsigned Size = Record[Idx++];
2767 llvm::SmallVector<unsigned, 16> ArgNums;
2768 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2769 Idx += Size;
2770 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2771 break;
2772 }
2773
2774 SIMPLE_ATTR(ObjCException);
2775 SIMPLE_ATTR(ObjCNSObject);
2776 SIMPLE_ATTR(Overloadable);
2777 UNSIGNED_ATTR(Packed);
2778 SIMPLE_ATTR(Pure);
2779 UNSIGNED_ATTR(Regparm);
2780 STRING_ATTR(Section);
2781 SIMPLE_ATTR(StdCall);
2782 SIMPLE_ATTR(TransparentUnion);
2783 SIMPLE_ATTR(Unavailable);
2784 SIMPLE_ATTR(Unused);
2785 SIMPLE_ATTR(Used);
2786
2787 case Attr::Visibility:
2788 New = ::new (Context) VisibilityAttr(
2789 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2790 break;
2791
2792 SIMPLE_ATTR(WarnUnusedResult);
2793 SIMPLE_ATTR(Weak);
2794 SIMPLE_ATTR(WeakImport);
2795 }
2796
2797 assert(New && "Unable to decode attribute?");
2798 New->setInherited(IsInherited);
2799 New->setNext(Attrs);
2800 Attrs = New;
2801 }
2802#undef UNSIGNED_ATTR
2803#undef STRING_ATTR
2804#undef SIMPLE_ATTR
2805
2806 // The list of attributes was built backwards. Reverse the list
2807 // before returning it.
2808 Attr *PrevAttr = 0, *NextAttr = 0;
2809 while (Attrs) {
2810 NextAttr = Attrs->getNext();
2811 Attrs->setNext(PrevAttr);
2812 PrevAttr = Attrs;
2813 Attrs = NextAttr;
2814 }
2815
2816 return PrevAttr;
2817}
2818
Douglas Gregorc9490c02009-04-16 22:23:12 +00002819Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002820 // Within the bitstream, expressions are stored in Reverse Polish
2821 // Notation, with each of the subexpressions preceding the
2822 // expression they are stored in. To evaluate expressions, we
2823 // continue reading expressions and placing them on the stack, with
2824 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002825 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002826 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002827 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002828 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002829 llvm::SmallVector<Stmt *, 16> StmtStack;
2830 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002831 Stmt::EmptyShell Empty;
2832
Douglas Gregor087fd532009-04-14 23:32:43 +00002833 while (true) {
2834 unsigned Code = Stream.ReadCode();
2835 if (Code == llvm::bitc::END_BLOCK) {
2836 if (Stream.ReadBlockEnd()) {
2837 Error("Error at end of Source Manager block");
2838 return 0;
2839 }
2840 break;
2841 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002842
Douglas Gregor087fd532009-04-14 23:32:43 +00002843 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2844 // No known subblocks, always skip them.
2845 Stream.ReadSubBlockID();
2846 if (Stream.SkipBlock()) {
2847 Error("Malformed block record");
2848 return 0;
2849 }
2850 continue;
2851 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002852
Douglas Gregor087fd532009-04-14 23:32:43 +00002853 if (Code == llvm::bitc::DEFINE_ABBREV) {
2854 Stream.ReadAbbrevRecord();
2855 continue;
2856 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002857
Douglas Gregorc9490c02009-04-16 22:23:12 +00002858 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002859 Idx = 0;
2860 Record.clear();
2861 bool Finished = false;
2862 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002863 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002864 Finished = true;
2865 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002866
Douglas Gregorc9490c02009-04-16 22:23:12 +00002867 case pch::STMT_NULL_PTR:
2868 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002869 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002870
Douglas Gregor025452f2009-04-17 00:04:06 +00002871 case pch::STMT_NULL:
2872 S = new (Context) NullStmt(Empty);
2873 break;
2874
2875 case pch::STMT_COMPOUND:
2876 S = new (Context) CompoundStmt(Empty);
2877 break;
2878
2879 case pch::STMT_CASE:
2880 S = new (Context) CaseStmt(Empty);
2881 break;
2882
2883 case pch::STMT_DEFAULT:
2884 S = new (Context) DefaultStmt(Empty);
2885 break;
2886
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002887 case pch::STMT_LABEL:
2888 S = new (Context) LabelStmt(Empty);
2889 break;
2890
Douglas Gregor025452f2009-04-17 00:04:06 +00002891 case pch::STMT_IF:
2892 S = new (Context) IfStmt(Empty);
2893 break;
2894
2895 case pch::STMT_SWITCH:
2896 S = new (Context) SwitchStmt(Empty);
2897 break;
2898
Douglas Gregord921cf92009-04-17 00:16:09 +00002899 case pch::STMT_WHILE:
2900 S = new (Context) WhileStmt(Empty);
2901 break;
2902
Douglas Gregor67d82492009-04-17 00:29:51 +00002903 case pch::STMT_DO:
2904 S = new (Context) DoStmt(Empty);
2905 break;
2906
2907 case pch::STMT_FOR:
2908 S = new (Context) ForStmt(Empty);
2909 break;
2910
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002911 case pch::STMT_GOTO:
2912 S = new (Context) GotoStmt(Empty);
2913 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002914
2915 case pch::STMT_INDIRECT_GOTO:
2916 S = new (Context) IndirectGotoStmt(Empty);
2917 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002918
Douglas Gregord921cf92009-04-17 00:16:09 +00002919 case pch::STMT_CONTINUE:
2920 S = new (Context) ContinueStmt(Empty);
2921 break;
2922
Douglas Gregor025452f2009-04-17 00:04:06 +00002923 case pch::STMT_BREAK:
2924 S = new (Context) BreakStmt(Empty);
2925 break;
2926
Douglas Gregor0de9d882009-04-17 16:34:57 +00002927 case pch::STMT_RETURN:
2928 S = new (Context) ReturnStmt(Empty);
2929 break;
2930
Douglas Gregor84f21702009-04-17 16:55:36 +00002931 case pch::STMT_DECL:
2932 S = new (Context) DeclStmt(Empty);
2933 break;
2934
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00002935 case pch::STMT_ASM:
2936 S = new (Context) AsmStmt(Empty);
2937 break;
2938
Douglas Gregor087fd532009-04-14 23:32:43 +00002939 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002940 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002941 break;
2942
2943 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002944 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002945 break;
2946
2947 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002948 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002949 break;
2950
2951 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002952 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002953 break;
2954
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002955 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002956 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002957 break;
2958
Douglas Gregor673ecd62009-04-15 16:35:07 +00002959 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002960 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002961 Record[PCHStmtReader::NumExprFields + 1]);
2962 break;
2963
Douglas Gregor087fd532009-04-14 23:32:43 +00002964 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002965 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002966 break;
2967
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002968 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002969 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002970 break;
2971
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002972 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002973 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002974 break;
2975
2976 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002977 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002978 break;
2979
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002980 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002981 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002982 break;
2983
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002984 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002985 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002986 break;
2987
2988 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002989 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002990 break;
2991
Douglas Gregordb600c32009-04-15 00:25:59 +00002992 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002993 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002994 break;
2995
Douglas Gregorad90e962009-04-15 22:40:36 +00002996 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002997 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002998 break;
2999
3000 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003001 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00003002 break;
3003
Douglas Gregor087fd532009-04-14 23:32:43 +00003004 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003005 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003006 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00003007
3008 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003009 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00003010 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00003011
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003012 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003013 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003014 break;
3015
Douglas Gregord3c98a02009-04-15 23:02:49 +00003016 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003017 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003018 break;
3019
Douglas Gregord077d752009-04-16 00:55:48 +00003020 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003021 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003022 break;
3023
3024 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003025 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00003026 Record[PCHStmtReader::NumExprFields] - 1);
3027
3028 break;
3029
3030 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003031 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003032 break;
3033
Douglas Gregord3c98a02009-04-15 23:02:49 +00003034 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003035 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003036 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003037
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003038 case pch::EXPR_ADDR_LABEL:
3039 S = new (Context) AddrLabelExpr(Empty);
3040 break;
3041
Douglas Gregor6a2dd552009-04-17 19:05:30 +00003042 case pch::EXPR_STMT:
3043 S = new (Context) StmtExpr(Empty);
3044 break;
3045
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003046 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003047 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003048 break;
3049
3050 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003051 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003052 break;
3053
3054 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003055 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003056 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003057
3058 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003059 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003060 break;
3061
Douglas Gregor84af7c22009-04-17 19:21:43 +00003062 case pch::EXPR_BLOCK:
3063 S = new (Context) BlockExpr(Empty);
3064 break;
3065
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003066 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003067 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003068 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003069
Chris Lattner3a57a372009-04-22 06:29:42 +00003070 case pch::EXPR_OBJC_STRING_LITERAL:
3071 S = new (Context) ObjCStringLiteral(Empty);
3072 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003073 case pch::EXPR_OBJC_ENCODE:
3074 S = new (Context) ObjCEncodeExpr(Empty);
3075 break;
Chris Lattner3a57a372009-04-22 06:29:42 +00003076 case pch::EXPR_OBJC_SELECTOR_EXPR:
3077 S = new (Context) ObjCSelectorExpr(Empty);
3078 break;
3079 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3080 S = new (Context) ObjCProtocolExpr(Empty);
3081 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00003082 }
3083
Douglas Gregorc9490c02009-04-16 22:23:12 +00003084 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00003085 if (Finished)
3086 break;
3087
Douglas Gregor3e1af842009-04-17 22:13:46 +00003088 ++NumStatementsRead;
3089
Douglas Gregorc9490c02009-04-16 22:23:12 +00003090 if (S) {
3091 unsigned NumSubStmts = Reader.Visit(S);
3092 while (NumSubStmts > 0) {
3093 StmtStack.pop_back();
3094 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00003095 }
3096 }
3097
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003098 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00003099 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00003100 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00003101 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00003102 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00003103 return StmtStack.back();
3104}
3105
3106Expr *PCHReader::ReadExpr() {
3107 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00003108}
3109
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003110DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003111 return Diag(SourceLocation(), DiagID);
3112}
3113
3114DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3115 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003116 Context.getSourceManager()),
3117 DiagID);
3118}
Douglas Gregor025452f2009-04-17 00:04:06 +00003119
Douglas Gregor668c1a42009-04-21 22:25:48 +00003120/// \brief Retrieve the identifier table associated with the
3121/// preprocessor.
3122IdentifierTable &PCHReader::getIdentifierTable() {
3123 return PP.getIdentifierTable();
3124}
3125
Douglas Gregor025452f2009-04-17 00:04:06 +00003126/// \brief Record that the given ID maps to the given switch-case
3127/// statement.
3128void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3129 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3130 SwitchCaseStmts[ID] = SC;
3131}
3132
3133/// \brief Retrieve the switch-case statement with the given ID.
3134SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3135 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3136 return SwitchCaseStmts[ID];
3137}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003138
3139/// \brief Record that the given label statement has been
3140/// deserialized and has the given ID.
3141void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3142 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3143 "Deserialized label twice");
3144 LabelStmts[ID] = S;
3145
3146 // If we've already seen any goto statements that point to this
3147 // label, resolve them now.
3148 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3149 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3150 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3151 Goto->second->setLabel(S);
3152 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003153
3154 // If we've already seen any address-label statements that point to
3155 // this label, resolve them now.
3156 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3157 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3158 = UnresolvedAddrLabelExprs.equal_range(ID);
3159 for (AddrLabelIter AddrLabel = AddrLabels.first;
3160 AddrLabel != AddrLabels.second; ++AddrLabel)
3161 AddrLabel->second->setLabel(S);
3162 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003163}
3164
3165/// \brief Set the label of the given statement to the label
3166/// identified by ID.
3167///
3168/// Depending on the order in which the label and other statements
3169/// referencing that label occur, this operation may complete
3170/// immediately (updating the statement) or it may queue the
3171/// statement to be back-patched later.
3172void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3173 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3174 if (Label != LabelStmts.end()) {
3175 // We've already seen this label, so set the label of the goto and
3176 // we're done.
3177 S->setLabel(Label->second);
3178 } else {
3179 // We haven't seen this label yet, so add this goto to the set of
3180 // unresolved goto statements.
3181 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3182 }
3183}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003184
3185/// \brief Set the label of the given expression to the label
3186/// identified by ID.
3187///
3188/// Depending on the order in which the label and other statements
3189/// referencing that label occur, this operation may complete
3190/// immediately (updating the statement) or it may queue the
3191/// statement to be back-patched later.
3192void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3193 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3194 if (Label != LabelStmts.end()) {
3195 // We've already seen this label, so set the label of the
3196 // label-address expression and we're done.
3197 S->setLabel(Label->second);
3198 } else {
3199 // We haven't seen this label yet, so add this label-address
3200 // expression to the set of unresolved label-address expressions.
3201 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3202 }
3203}