blob: e9a4f54856197fe0878c83bd793e3d1904ce65df [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000032#include "llvm/Bitcode/BitstreamReader.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include <algorithm>
36#include <cstdio>
37
38using namespace clang;
39
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000040namespace {
41 /// \brief Helper class that saves the current stream position and
42 /// then restores it when destroyed.
43 struct VISIBILITY_HIDDEN SavedStreamPosition {
Chris Lattner587788a2009-04-26 20:59:20 +000044 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
45 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000046
47 ~SavedStreamPosition() {
Chris Lattner587788a2009-04-26 20:59:20 +000048 Cursor.JumpToBit(Offset);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000049 }
50
51 private:
Chris Lattner587788a2009-04-26 20:59:20 +000052 llvm::BitstreamCursor &Cursor;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000053 uint64_t Offset;
54 };
55}
56
Douglas Gregorc34897d2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000063 PCHReader &Reader;
64 const PCHReader::RecordData &Record;
65 unsigned &Idx;
66
67 public:
68 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
69 unsigned &Idx)
70 : Reader(Reader), Record(Record), Idx(Idx) { }
71
72 void VisitDecl(Decl *D);
73 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
74 void VisitNamedDecl(NamedDecl *ND);
75 void VisitTypeDecl(TypeDecl *TD);
76 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregorce066712009-04-26 22:20:50 +000085 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000086 void VisitParmVarDecl(ParmVarDecl *PD);
87 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000088 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
89 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000090 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000091 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000092 void VisitObjCContainerDecl(ObjCContainerDecl *D);
93 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
94 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000095 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
96 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
97 void VisitObjCClassDecl(ObjCClassDecl *D);
98 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
99 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
100 void VisitObjCImplDecl(ObjCImplDecl *D);
101 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
102 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
103 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
104 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
105 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000106 };
107}
108
109void PCHDeclReader::VisitDecl(Decl *D) {
110 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLexicalDeclContext(
112 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
113 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
114 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000115 if (Record[Idx++])
116 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000117 D->setImplicit(Record[Idx++]);
118 D->setAccess((AccessSpecifier)Record[Idx++]);
119}
120
121void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
122 VisitDecl(TU);
123}
124
125void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
126 VisitDecl(ND);
127 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
128}
129
130void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
131 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000132 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
133}
134
135void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000136 // Note that we cannot use VisitTypeDecl here, because we need to
137 // set the underlying type of the typedef *before* we try to read
138 // the type associated with the TypedefDecl.
139 VisitNamedDecl(TD);
140 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
141 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
142 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000143}
144
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000145void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
146 VisitTypeDecl(TD);
147 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
148 TD->setDefinition(Record[Idx++]);
149 TD->setTypedefForAnonDecl(
150 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
151}
152
153void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
154 VisitTagDecl(ED);
155 ED->setIntegerType(Reader.GetType(Record[Idx++]));
156}
157
Douglas Gregor982365e2009-04-13 21:20:57 +0000158void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
159 VisitTagDecl(RD);
160 RD->setHasFlexibleArrayMember(Record[Idx++]);
161 RD->setAnonymousStructOrUnion(Record[Idx++]);
162}
163
Douglas Gregorc34897d2009-04-09 22:27:44 +0000164void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
165 VisitNamedDecl(VD);
166 VD->setType(Reader.GetType(Record[Idx++]));
167}
168
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000169void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
170 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000171 if (Record[Idx++])
172 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000173 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
174}
175
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000176void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
177 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000178 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000179 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000180 FD->setPreviousDeclaration(
181 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
182 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
183 FD->setInline(Record[Idx++]);
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000184 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000185 FD->setVirtual(Record[Idx++]);
186 FD->setPure(Record[Idx++]);
187 FD->setInheritedPrototype(Record[Idx++]);
188 FD->setHasPrototype(Record[Idx++]);
189 FD->setDeleted(Record[Idx++]);
190 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
191 unsigned NumParams = Record[Idx++];
192 llvm::SmallVector<ParmVarDecl *, 16> Params;
193 Params.reserve(NumParams);
194 for (unsigned I = 0; I != NumParams; ++I)
195 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
196 FD->setParams(Reader.getContext(), &Params[0], NumParams);
197}
198
Steve Naroff79ea0e02009-04-20 15:06:07 +0000199void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
200 VisitNamedDecl(MD);
201 if (Record[Idx++]) {
202 // In practice, this won't be executed (since method definitions
203 // don't occur in header files).
Douglas Gregorce066712009-04-26 22:20:50 +0000204 MD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Steve Naroff79ea0e02009-04-20 15:06:07 +0000205 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
206 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
207 }
208 MD->setInstanceMethod(Record[Idx++]);
209 MD->setVariadic(Record[Idx++]);
210 MD->setSynthesized(Record[Idx++]);
211 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
212 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
213 MD->setResultType(Reader.GetType(Record[Idx++]));
214 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
215 unsigned NumParams = Record[Idx++];
216 llvm::SmallVector<ParmVarDecl *, 16> Params;
217 Params.reserve(NumParams);
218 for (unsigned I = 0; I != NumParams; ++I)
219 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
220 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
221}
222
Steve Naroff7333b492009-04-20 20:09:33 +0000223void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
224 VisitNamedDecl(CD);
225 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
226}
227
228void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
229 VisitObjCContainerDecl(ID);
230 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000231 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
232 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000233 unsigned NumProtocols = Record[Idx++];
234 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
235 Protocols.reserve(NumProtocols);
236 for (unsigned I = 0; I != NumProtocols; ++I)
237 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor5efc1052009-04-24 22:01:00 +0000238 ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext());
Steve Naroff7333b492009-04-20 20:09:33 +0000239 unsigned NumIvars = Record[Idx++];
240 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
241 IVars.reserve(NumIvars);
242 for (unsigned I = 0; I != NumIvars; ++I)
243 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
244 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000245 ID->setCategoryList(
246 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000247 ID->setForwardDecl(Record[Idx++]);
248 ID->setImplicitInterfaceDecl(Record[Idx++]);
249 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
250 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000251 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000252}
253
254void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
255 VisitFieldDecl(IVD);
256 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
257}
258
Steve Naroff97b53bd2009-04-21 15:12:33 +0000259void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
260 VisitObjCContainerDecl(PD);
261 PD->setForwardDecl(Record[Idx++]);
262 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
263 unsigned NumProtoRefs = Record[Idx++];
264 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
265 ProtoRefs.reserve(NumProtoRefs);
266 for (unsigned I = 0; I != NumProtoRefs; ++I)
267 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
268 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
269}
270
271void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
272 VisitFieldDecl(FD);
273}
274
275void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
276 VisitDecl(CD);
277 unsigned NumClassRefs = Record[Idx++];
278 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
279 ClassRefs.reserve(NumClassRefs);
280 for (unsigned I = 0; I != NumClassRefs; ++I)
281 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
282 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
283}
284
285void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
286 VisitDecl(FPD);
287 unsigned NumProtoRefs = Record[Idx++];
288 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
289 ProtoRefs.reserve(NumProtoRefs);
290 for (unsigned I = 0; I != NumProtoRefs; ++I)
291 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
292 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
293}
294
295void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
296 VisitObjCContainerDecl(CD);
297 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
298 unsigned NumProtoRefs = Record[Idx++];
299 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
300 ProtoRefs.reserve(NumProtoRefs);
301 for (unsigned I = 0; I != NumProtoRefs; ++I)
302 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
303 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000304 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000305 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
306}
307
308void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
309 VisitNamedDecl(CAD);
310 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
311}
312
313void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
314 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000315 D->setType(Reader.GetType(Record[Idx++]));
316 // FIXME: stable encoding
317 D->setPropertyAttributes(
318 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
319 // FIXME: stable encoding
320 D->setPropertyImplementation(
321 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
322 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
323 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
324 D->setGetterMethodDecl(
325 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
326 D->setSetterMethodDecl(
327 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
328 D->setPropertyIvarDecl(
329 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000330}
331
332void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000333 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000334 D->setClassInterface(
335 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
336 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000337}
338
339void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
340 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000341 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000342}
343
344void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
345 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000346 D->setSuperClass(
347 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000348}
349
350
351void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
352 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000353 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
354 D->setPropertyDecl(
355 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
356 D->setPropertyIvarDecl(
357 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000358}
359
Douglas Gregor982365e2009-04-13 21:20:57 +0000360void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
361 VisitValueDecl(FD);
362 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000363 if (Record[Idx++])
364 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000365}
366
Douglas Gregorc34897d2009-04-09 22:27:44 +0000367void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
368 VisitValueDecl(VD);
369 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
370 VD->setThreadSpecified(Record[Idx++]);
371 VD->setCXXDirectInitializer(Record[Idx++]);
372 VD->setDeclaredInCondition(Record[Idx++]);
373 VD->setPreviousDeclaration(
374 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
375 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000376 if (Record[Idx++])
377 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000378}
379
Douglas Gregorce066712009-04-26 22:20:50 +0000380void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
381 VisitVarDecl(PD);
382}
383
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000384void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
385 VisitVarDecl(PD);
386 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000387 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000388}
389
390void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
391 VisitParmVarDecl(PD);
392 PD->setOriginalType(Reader.GetType(Record[Idx++]));
393}
394
Douglas Gregor2a491792009-04-13 22:49:25 +0000395void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
396 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000397 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000398}
399
400void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
401 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000402 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000403 unsigned NumParams = Record[Idx++];
404 llvm::SmallVector<ParmVarDecl *, 16> Params;
405 Params.reserve(NumParams);
406 for (unsigned I = 0; I != NumParams; ++I)
407 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
408 BD->setParams(Reader.getContext(), &Params[0], NumParams);
409}
410
Douglas Gregorc34897d2009-04-09 22:27:44 +0000411std::pair<uint64_t, uint64_t>
412PCHDeclReader::VisitDeclContext(DeclContext *DC) {
413 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000414 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000415 return std::make_pair(LexicalOffset, VisibleOffset);
416}
417
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000418//===----------------------------------------------------------------------===//
419// Statement/expression deserialization
420//===----------------------------------------------------------------------===//
421namespace {
422 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000423 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000424 PCHReader &Reader;
425 const PCHReader::RecordData &Record;
426 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000427 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000428
429 public:
430 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000431 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
432 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000433
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000434 /// \brief The number of record fields required for the Stmt class
435 /// itself.
436 static const unsigned NumStmtFields = 0;
437
Douglas Gregor596e0932009-04-15 16:35:07 +0000438 /// \brief The number of record fields required for the Expr class
439 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000440 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000441
Douglas Gregora151ba42009-04-14 23:32:43 +0000442 // Each of the Visit* functions reads in part of the expression
443 // from the given record and the current expression stack, then
444 // return the total number of operands that it read from the
445 // expression stack.
446
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000447 unsigned VisitStmt(Stmt *S);
448 unsigned VisitNullStmt(NullStmt *S);
449 unsigned VisitCompoundStmt(CompoundStmt *S);
450 unsigned VisitSwitchCase(SwitchCase *S);
451 unsigned VisitCaseStmt(CaseStmt *S);
452 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000453 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000454 unsigned VisitIfStmt(IfStmt *S);
455 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000456 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000457 unsigned VisitDoStmt(DoStmt *S);
458 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000459 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000460 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000461 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000462 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000463 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000464 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000465 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000466 unsigned VisitExpr(Expr *E);
467 unsigned VisitPredefinedExpr(PredefinedExpr *E);
468 unsigned VisitDeclRefExpr(DeclRefExpr *E);
469 unsigned VisitIntegerLiteral(IntegerLiteral *E);
470 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000471 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000472 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000473 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000474 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000475 unsigned VisitUnaryOperator(UnaryOperator *E);
476 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000477 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000478 unsigned VisitCallExpr(CallExpr *E);
479 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000480 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000481 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000482 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
483 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000484 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000485 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
486 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000487 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000488 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000489 unsigned VisitInitListExpr(InitListExpr *E);
490 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
491 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000492 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000493 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000494 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000495 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
496 unsigned VisitChooseExpr(ChooseExpr *E);
497 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000498 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000499 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000500 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000501 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000502 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000503 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
504 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000505 unsigned VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
506 unsigned VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
507 unsigned VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000508 unsigned VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000509 unsigned VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000510
511 unsigned VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
Douglas Gregorce066712009-04-26 22:20:50 +0000512 unsigned VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
513 unsigned VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
Steve Naroff79762bd2009-04-26 18:52:16 +0000514 unsigned VisitObjCAtTryStmt(ObjCAtTryStmt *);
515 unsigned VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
516 unsigned VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000517 };
518}
519
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000520unsigned PCHStmtReader::VisitStmt(Stmt *S) {
521 assert(Idx == NumStmtFields && "Incorrect statement field count");
522 return 0;
523}
524
525unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
526 VisitStmt(S);
527 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
528 return 0;
529}
530
531unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
532 VisitStmt(S);
533 unsigned NumStmts = Record[Idx++];
534 S->setStmts(Reader.getContext(),
535 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
536 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
537 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
538 return NumStmts;
539}
540
541unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
542 VisitStmt(S);
543 Reader.RecordSwitchCaseID(S, Record[Idx++]);
544 return 0;
545}
546
547unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
548 VisitSwitchCase(S);
549 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
550 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
551 S->setSubStmt(StmtStack.back());
552 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
553 return 3;
554}
555
556unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
557 VisitSwitchCase(S);
558 S->setSubStmt(StmtStack.back());
559 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
560 return 1;
561}
562
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000563unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
564 VisitStmt(S);
565 S->setID(Reader.GetIdentifierInfo(Record, Idx));
566 S->setSubStmt(StmtStack.back());
567 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
568 Reader.RecordLabelStmt(S, Record[Idx++]);
569 return 1;
570}
571
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000572unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
573 VisitStmt(S);
574 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
575 S->setThen(StmtStack[StmtStack.size() - 2]);
576 S->setElse(StmtStack[StmtStack.size() - 1]);
577 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
578 return 3;
579}
580
581unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
582 VisitStmt(S);
583 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
584 S->setBody(StmtStack.back());
585 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
586 SwitchCase *PrevSC = 0;
587 for (unsigned N = Record.size(); Idx != N; ++Idx) {
588 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
589 if (PrevSC)
590 PrevSC->setNextSwitchCase(SC);
591 else
592 S->setSwitchCaseList(SC);
593 PrevSC = SC;
594 }
595 return 2;
596}
597
Douglas Gregora6b503f2009-04-17 00:16:09 +0000598unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
599 VisitStmt(S);
600 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
601 S->setBody(StmtStack.back());
602 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 return 2;
604}
605
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000606unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
607 VisitStmt(S);
608 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
609 S->setBody(StmtStack.back());
610 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 return 2;
612}
613
614unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
615 VisitStmt(S);
616 S->setInit(StmtStack[StmtStack.size() - 4]);
617 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
618 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
619 S->setBody(StmtStack.back());
620 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
621 return 4;
622}
623
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000624unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
625 VisitStmt(S);
626 Reader.SetLabelOf(S, Record[Idx++]);
627 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
628 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
629 return 0;
630}
631
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000632unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
633 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000634 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000635 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
636 return 1;
637}
638
Douglas Gregora6b503f2009-04-17 00:16:09 +0000639unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
640 VisitStmt(S);
641 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
642 return 0;
643}
644
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000645unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
646 VisitStmt(S);
647 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
648 return 0;
649}
650
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000651unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
652 VisitStmt(S);
653 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
654 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
655 return 1;
656}
657
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000658unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
659 VisitStmt(S);
660 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
661 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
662
663 if (Idx + 1 == Record.size()) {
664 // Single declaration
665 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
666 } else {
667 llvm::SmallVector<Decl *, 16> Decls;
668 Decls.reserve(Record.size() - Idx);
669 for (unsigned N = Record.size(); Idx != N; ++Idx)
670 Decls.push_back(Reader.GetDecl(Record[Idx]));
671 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
672 &Decls[0], Decls.size())));
673 }
674 return 0;
675}
676
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000677unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
678 VisitStmt(S);
679 unsigned NumOutputs = Record[Idx++];
680 unsigned NumInputs = Record[Idx++];
681 unsigned NumClobbers = Record[Idx++];
682 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
683 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
684 S->setVolatile(Record[Idx++]);
685 S->setSimple(Record[Idx++]);
686
687 unsigned StackIdx
688 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
689 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
690
691 // Outputs and inputs
692 llvm::SmallVector<std::string, 16> Names;
693 llvm::SmallVector<StringLiteral*, 16> Constraints;
694 llvm::SmallVector<Stmt*, 16> Exprs;
695 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
696 Names.push_back(Reader.ReadString(Record, Idx));
697 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
698 Exprs.push_back(StmtStack[StackIdx++]);
699 }
700 S->setOutputsAndInputs(NumOutputs, NumInputs,
701 &Names[0], &Constraints[0], &Exprs[0]);
702
703 // Constraints
704 llvm::SmallVector<StringLiteral*, 16> Clobbers;
705 for (unsigned I = 0; I != NumClobbers; ++I)
706 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
707 S->setClobbers(&Clobbers[0], NumClobbers);
708
709 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
710 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
711}
712
Douglas Gregora151ba42009-04-14 23:32:43 +0000713unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000714 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000715 E->setType(Reader.GetType(Record[Idx++]));
716 E->setTypeDependent(Record[Idx++]);
717 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000718 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000719 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000720}
721
Douglas Gregora151ba42009-04-14 23:32:43 +0000722unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000723 VisitExpr(E);
724 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
725 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000726 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000727}
728
Douglas Gregora151ba42009-04-14 23:32:43 +0000729unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000730 VisitExpr(E);
731 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
732 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000733 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000734}
735
Douglas Gregora151ba42009-04-14 23:32:43 +0000736unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000737 VisitExpr(E);
738 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
739 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000740 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000741}
742
Douglas Gregora151ba42009-04-14 23:32:43 +0000743unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000744 VisitExpr(E);
745 E->setValue(Reader.ReadAPFloat(Record, Idx));
746 E->setExact(Record[Idx++]);
747 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000748 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000749}
750
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000751unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
752 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000753 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000754 return 1;
755}
756
Douglas Gregor596e0932009-04-15 16:35:07 +0000757unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
758 VisitExpr(E);
759 unsigned Len = Record[Idx++];
760 assert(Record[Idx] == E->getNumConcatenated() &&
761 "Wrong number of concatenated tokens!");
762 ++Idx;
763 E->setWide(Record[Idx++]);
764
765 // Read string data
766 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
767 E->setStrData(Reader.getContext(), &Str[0], Len);
768 Idx += Len;
769
770 // Read source locations
771 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
772 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
773
774 return 0;
775}
776
Douglas Gregora151ba42009-04-14 23:32:43 +0000777unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000778 VisitExpr(E);
779 E->setValue(Record[Idx++]);
780 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000782 return 0;
783}
784
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000785unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
786 VisitExpr(E);
787 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000789 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000790 return 1;
791}
792
Douglas Gregor12d74052009-04-15 15:58:59 +0000793unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
794 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000795 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000796 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
797 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
798 return 1;
799}
800
801unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
802 VisitExpr(E);
803 E->setSizeof(Record[Idx++]);
804 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000805 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000806 ++Idx;
807 } else {
808 E->setArgument(Reader.GetType(Record[Idx++]));
809 }
810 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
811 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
812 return E->isArgumentType()? 0 : 1;
813}
814
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000815unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
816 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000817 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Steve Naroff315ec172009-04-25 15:19:54 +0000818 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000819 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
820 return 2;
821}
822
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000823unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
824 VisitExpr(E);
825 E->setNumArgs(Reader.getContext(), Record[Idx++]);
826 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000827 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000828 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000829 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000830 return E->getNumArgs() + 1;
831}
832
833unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
834 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000835 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000836 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
837 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
838 E->setArrow(Record[Idx++]);
839 return 1;
840}
841
Douglas Gregora151ba42009-04-14 23:32:43 +0000842unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
843 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000844 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000845 return 1;
846}
847
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000848unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
849 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000850 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
851 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000852 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
853 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
854 return 2;
855}
856
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000857unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
858 VisitBinaryOperator(E);
859 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
860 E->setComputationResultType(Reader.GetType(Record[Idx++]));
861 return 2;
862}
863
864unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
865 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000866 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
867 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
868 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000869 return 3;
870}
871
Douglas Gregora151ba42009-04-14 23:32:43 +0000872unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
873 VisitCastExpr(E);
874 E->setLvalueCast(Record[Idx++]);
875 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000876}
877
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000878unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
879 VisitCastExpr(E);
880 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
881 return 1;
882}
883
884unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
885 VisitExplicitCastExpr(E);
886 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
887 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
888 return 1;
889}
890
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000891unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
892 VisitExpr(E);
893 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000894 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000895 E->setFileScope(Record[Idx++]);
896 return 1;
897}
898
Douglas Gregorec0b8292009-04-15 23:02:49 +0000899unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
900 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000901 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000902 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
903 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
904 return 1;
905}
906
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000907unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
908 VisitExpr(E);
909 unsigned NumInits = Record[Idx++];
910 E->reserveInits(NumInits);
911 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000912 E->updateInit(I,
913 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
914 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000915 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
916 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
917 E->setInitializedFieldInUnion(
918 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
919 E->sawArrayRangeDesignator(Record[Idx++]);
920 return NumInits + 1;
921}
922
923unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
924 typedef DesignatedInitExpr::Designator Designator;
925
926 VisitExpr(E);
927 unsigned NumSubExprs = Record[Idx++];
928 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
929 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000930 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000931 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
932 E->setGNUSyntax(Record[Idx++]);
933
934 llvm::SmallVector<Designator, 4> Designators;
935 while (Idx < Record.size()) {
936 switch ((pch::DesignatorTypes)Record[Idx++]) {
937 case pch::DESIG_FIELD_DECL: {
938 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
939 SourceLocation DotLoc
940 = SourceLocation::getFromRawEncoding(Record[Idx++]);
941 SourceLocation FieldLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
944 FieldLoc));
945 Designators.back().setField(Field);
946 break;
947 }
948
949 case pch::DESIG_FIELD_NAME: {
950 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
951 SourceLocation DotLoc
952 = SourceLocation::getFromRawEncoding(Record[Idx++]);
953 SourceLocation FieldLoc
954 = SourceLocation::getFromRawEncoding(Record[Idx++]);
955 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
956 break;
957 }
958
959 case pch::DESIG_ARRAY: {
960 unsigned Index = Record[Idx++];
961 SourceLocation LBracketLoc
962 = SourceLocation::getFromRawEncoding(Record[Idx++]);
963 SourceLocation RBracketLoc
964 = SourceLocation::getFromRawEncoding(Record[Idx++]);
965 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
966 break;
967 }
968
969 case pch::DESIG_ARRAY_RANGE: {
970 unsigned Index = Record[Idx++];
971 SourceLocation LBracketLoc
972 = SourceLocation::getFromRawEncoding(Record[Idx++]);
973 SourceLocation EllipsisLoc
974 = SourceLocation::getFromRawEncoding(Record[Idx++]);
975 SourceLocation RBracketLoc
976 = SourceLocation::getFromRawEncoding(Record[Idx++]);
977 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
978 RBracketLoc));
979 break;
980 }
981 }
982 }
983 E->setDesignators(&Designators[0], Designators.size());
984
985 return NumSubExprs;
986}
987
988unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
989 VisitExpr(E);
990 return 0;
991}
992
Douglas Gregorec0b8292009-04-15 23:02:49 +0000993unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
994 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000995 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000996 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
997 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
998 return 1;
999}
1000
Douglas Gregor95a8fe32009-04-17 18:58:21 +00001001unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
1002 VisitExpr(E);
1003 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1005 Reader.SetLabelOf(E, Record[Idx++]);
1006 return 0;
1007}
1008
Douglas Gregoreca12f62009-04-17 19:05:30 +00001009unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
1010 VisitExpr(E);
1011 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1012 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1013 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
1014 return 1;
1015}
1016
Douglas Gregor209d4622009-04-15 23:33:31 +00001017unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1018 VisitExpr(E);
1019 E->setArgType1(Reader.GetType(Record[Idx++]));
1020 E->setArgType2(Reader.GetType(Record[Idx++]));
1021 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1022 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1023 return 0;
1024}
1025
1026unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1027 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001028 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1029 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1030 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001031 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1032 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1033 return 3;
1034}
1035
1036unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1037 VisitExpr(E);
1038 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1039 return 0;
1040}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001041
Douglas Gregor725e94b2009-04-16 00:01:45 +00001042unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1043 VisitExpr(E);
1044 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001045 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001046 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1047 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1048 return NumExprs;
1049}
1050
Douglas Gregore246b742009-04-17 19:21:43 +00001051unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1052 VisitExpr(E);
1053 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1054 E->setHasBlockDeclRefExprs(Record[Idx++]);
1055 return 0;
1056}
1057
Douglas Gregor725e94b2009-04-16 00:01:45 +00001058unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1059 VisitExpr(E);
1060 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1061 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1062 E->setByRef(Record[Idx++]);
1063 return 0;
1064}
1065
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001066//===----------------------------------------------------------------------===//
1067// Objective-C Expressions and Statements
1068
1069unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1070 VisitExpr(E);
1071 E->setString(cast<StringLiteral>(StmtStack.back()));
1072 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1073 return 1;
1074}
1075
Chris Lattner80f83c62009-04-22 05:57:30 +00001076unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1077 VisitExpr(E);
1078 E->setEncodedType(Reader.GetType(Record[Idx++]));
1079 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1080 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1081 return 0;
1082}
1083
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001084unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1085 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001086 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001087 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1088 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1089 return 0;
1090}
1091
1092unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1093 VisitExpr(E);
1094 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1095 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1096 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1097 return 0;
1098}
1099
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001100unsigned PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1101 VisitExpr(E);
1102 E->setDecl(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
1103 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1104 E->setBase(cast<Expr>(StmtStack.back()));
1105 E->setIsArrow(Record[Idx++]);
1106 E->setIsFreeIvar(Record[Idx++]);
1107 return 1;
1108}
1109
1110unsigned PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1111 VisitExpr(E);
1112 E->setProperty(cast<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
1113 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1114 E->setBase(cast<Expr>(StmtStack.back()));
1115 return 1;
1116}
1117
1118unsigned PCHStmtReader::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1119 VisitExpr(E);
Douglas Gregorce066712009-04-26 22:20:50 +00001120 E->setGetterMethod(
1121 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1122 E->setSetterMethod(
1123 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1124 E->setClassProp(
1125 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
1126 E->setBase(cast_or_null<Expr>(StmtStack.back()));
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001127 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1128 E->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1129 return 1;
1130}
1131
Steve Narofffb3e4022009-04-25 14:04:28 +00001132unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1133 VisitExpr(E);
1134 E->setNumArgs(Record[Idx++]);
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001135 E->setLeftLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1136 E->setRightLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Narofffb3e4022009-04-25 14:04:28 +00001137 E->setSelector(Reader.GetSelector(Record, Idx));
1138 E->setMethodDecl(cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001139
Douglas Gregorce066712009-04-26 22:20:50 +00001140 E->setReceiver(
1141 cast_or_null<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
1142 if (!E->getReceiver()) {
1143 ObjCMessageExpr::ClassInfo CI;
1144 CI.first = cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]));
1145 CI.second = Reader.GetIdentifierInfo(Record, Idx);
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001146 E->setClassInfo(CI);
Douglas Gregorce066712009-04-26 22:20:50 +00001147 }
1148
Steve Narofffb3e4022009-04-25 14:04:28 +00001149 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1150 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
1151 return E->getNumArgs() + 1;
1152}
1153
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001154unsigned PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1155 VisitExpr(E);
1156 E->setLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1157 return 0;
1158}
Chris Lattner80f83c62009-04-22 05:57:30 +00001159
Steve Naroff79762bd2009-04-26 18:52:16 +00001160unsigned PCHStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1161 VisitStmt(S);
1162 S->setElement(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1163 S->setCollection(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1164 S->setBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1165 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1166 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1167 return 3;
1168}
1169
Douglas Gregorce066712009-04-26 22:20:50 +00001170unsigned PCHStmtReader::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001171 VisitStmt(S);
1172 S->setCatchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1173 S->setNextCatchStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1174 S->setCatchParamDecl(cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
1175 S->setAtCatchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1176 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1177 return 2;
1178}
1179
Douglas Gregorce066712009-04-26 22:20:50 +00001180unsigned PCHStmtReader::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +00001181 VisitStmt(S);
1182 S->setFinallyBody(StmtStack.back());
1183 S->setAtFinallyLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1184 return 1;
1185}
1186
1187unsigned PCHStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1188 VisitStmt(S);
1189 S->setTryBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1190 S->setCatchStmts(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1191 S->setFinallyStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1192 S->setAtTryLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1193 return 3;
1194}
1195
1196unsigned PCHStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1197 VisitStmt(S);
1198 S->setSynchExpr(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1199 S->setSynchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1200 S->setAtSynchronizedLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1201 return 2;
1202}
1203
1204unsigned PCHStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1205 VisitStmt(S);
1206 S->setThrowExpr(StmtStack.back());
1207 S->setThrowLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1208 return 1;
1209}
1210
Douglas Gregorc713da92009-04-21 22:25:48 +00001211//===----------------------------------------------------------------------===//
1212// PCH reader implementation
1213//===----------------------------------------------------------------------===//
1214
1215namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001216class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1217 PCHReader &Reader;
1218
1219public:
1220 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1221
1222 typedef Selector external_key_type;
1223 typedef external_key_type internal_key_type;
1224
1225 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1226
1227 static bool EqualKey(const internal_key_type& a,
1228 const internal_key_type& b) {
1229 return a == b;
1230 }
1231
1232 static unsigned ComputeHash(Selector Sel) {
1233 unsigned N = Sel.getNumArgs();
1234 if (N == 0)
1235 ++N;
1236 unsigned R = 5381;
1237 for (unsigned I = 0; I != N; ++I)
1238 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1239 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1240 return R;
1241 }
1242
1243 // This hopefully will just get inlined and removed by the optimizer.
1244 static const internal_key_type&
1245 GetInternalKey(const external_key_type& x) { return x; }
1246
1247 static std::pair<unsigned, unsigned>
1248 ReadKeyDataLength(const unsigned char*& d) {
1249 using namespace clang::io;
1250 unsigned KeyLen = ReadUnalignedLE16(d);
1251 unsigned DataLen = ReadUnalignedLE16(d);
1252 return std::make_pair(KeyLen, DataLen);
1253 }
1254
Douglas Gregor2d711832009-04-25 17:48:32 +00001255 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001256 using namespace clang::io;
1257 SelectorTable &SelTable = Reader.getContext().Selectors;
1258 unsigned N = ReadUnalignedLE16(d);
1259 IdentifierInfo *FirstII
1260 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1261 if (N == 0)
1262 return SelTable.getNullarySelector(FirstII);
1263 else if (N == 1)
1264 return SelTable.getUnarySelector(FirstII);
1265
1266 llvm::SmallVector<IdentifierInfo *, 16> Args;
1267 Args.push_back(FirstII);
1268 for (unsigned I = 1; I != N; ++I)
1269 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1270
1271 return SelTable.getSelector(N, &Args[0]);
1272 }
1273
1274 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1275 using namespace clang::io;
1276 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1277 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1278
1279 data_type Result;
1280
1281 // Load instance methods
1282 ObjCMethodList *Prev = 0;
1283 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1284 ObjCMethodDecl *Method
1285 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1286 if (!Result.first.Method) {
1287 // This is the first method, which is the easy case.
1288 Result.first.Method = Method;
1289 Prev = &Result.first;
1290 continue;
1291 }
1292
1293 Prev->Next = new ObjCMethodList(Method, 0);
1294 Prev = Prev->Next;
1295 }
1296
1297 // Load factory methods
1298 Prev = 0;
1299 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1300 ObjCMethodDecl *Method
1301 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1302 if (!Result.second.Method) {
1303 // This is the first method, which is the easy case.
1304 Result.second.Method = Method;
1305 Prev = &Result.second;
1306 continue;
1307 }
1308
1309 Prev->Next = new ObjCMethodList(Method, 0);
1310 Prev = Prev->Next;
1311 }
1312
1313 return Result;
1314 }
1315};
1316
1317} // end anonymous namespace
1318
1319/// \brief The on-disk hash table used for the global method pool.
1320typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1321 PCHMethodPoolLookupTable;
1322
1323namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001324class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1325 PCHReader &Reader;
1326
1327 // If we know the IdentifierInfo in advance, it is here and we will
1328 // not build a new one. Used when deserializing information about an
1329 // identifier that was constructed before the PCH file was read.
1330 IdentifierInfo *KnownII;
1331
1332public:
1333 typedef IdentifierInfo * data_type;
1334
1335 typedef const std::pair<const char*, unsigned> external_key_type;
1336
1337 typedef external_key_type internal_key_type;
1338
1339 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1340 : Reader(Reader), KnownII(II) { }
1341
1342 static bool EqualKey(const internal_key_type& a,
1343 const internal_key_type& b) {
1344 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1345 : false;
1346 }
1347
1348 static unsigned ComputeHash(const internal_key_type& a) {
1349 return BernsteinHash(a.first, a.second);
1350 }
1351
1352 // This hopefully will just get inlined and removed by the optimizer.
1353 static const internal_key_type&
1354 GetInternalKey(const external_key_type& x) { return x; }
1355
1356 static std::pair<unsigned, unsigned>
1357 ReadKeyDataLength(const unsigned char*& d) {
1358 using namespace clang::io;
Douglas Gregor4bb24882009-04-25 20:26:24 +00001359 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001360 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregorc713da92009-04-21 22:25:48 +00001361 return std::make_pair(KeyLen, DataLen);
1362 }
1363
1364 static std::pair<const char*, unsigned>
1365 ReadKey(const unsigned char* d, unsigned n) {
1366 assert(n >= 2 && d[n-1] == '\0');
1367 return std::make_pair((const char*) d, n-1);
1368 }
1369
1370 IdentifierInfo *ReadData(const internal_key_type& k,
1371 const unsigned char* d,
1372 unsigned DataLen) {
1373 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001374 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001375 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1376 Bits >>= 1;
1377 bool Poisoned = Bits & 0x01;
1378 Bits >>= 1;
1379 bool ExtensionToken = Bits & 0x01;
1380 Bits >>= 1;
1381 bool hasMacroDefinition = Bits & 0x01;
1382 Bits >>= 1;
1383 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1384 Bits >>= 10;
1385 unsigned TokenID = Bits & 0xFF;
1386 Bits >>= 8;
1387
Douglas Gregorc713da92009-04-21 22:25:48 +00001388 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001389 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001390 DataLen -= 8;
1391
1392 // Build the IdentifierInfo itself and link the identifier ID with
1393 // the new IdentifierInfo.
1394 IdentifierInfo *II = KnownII;
1395 if (!II)
Douglas Gregor4bb24882009-04-25 20:26:24 +00001396 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1397 k.first, k.first + k.second);
Douglas Gregorc713da92009-04-21 22:25:48 +00001398 Reader.SetIdentifierInfo(ID, II);
1399
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001400 // Set or check the various bits in the IdentifierInfo structure.
1401 // FIXME: Load token IDs lazily, too?
1402 assert((unsigned)II->getTokenID() == TokenID &&
1403 "Incorrect token ID loaded");
1404 (void)TokenID;
1405 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1406 assert(II->isExtensionToken() == ExtensionToken &&
1407 "Incorrect extension token flag");
1408 (void)ExtensionToken;
1409 II->setIsPoisoned(Poisoned);
1410 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1411 "Incorrect C++ operator keyword flag");
1412 (void)CPlusPlusOperatorKeyword;
1413
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001414 // If this identifier is a macro, deserialize the macro
1415 // definition.
1416 if (hasMacroDefinition) {
1417 uint32_t Offset = ReadUnalignedLE64(d);
1418 Reader.ReadMacroRecord(Offset);
1419 DataLen -= 8;
1420 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001421
1422 // Read all of the declarations visible at global scope with this
1423 // name.
1424 Sema *SemaObj = Reader.getSema();
1425 while (DataLen > 0) {
1426 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001427 if (SemaObj) {
1428 // Introduce this declaration into the translation-unit scope
1429 // and add it to the declaration chain for this identifier, so
1430 // that (unqualified) name lookup will find it.
1431 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1432 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1433 } else {
1434 // Queue this declaration so that it will be added to the
1435 // translation unit scope and identifier's declaration chain
1436 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001437 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001438 }
1439
1440 DataLen -= 4;
1441 }
1442 return II;
1443 }
1444};
1445
1446} // end anonymous namespace
1447
1448/// \brief The on-disk hash table used to contain information about
1449/// all of the identifiers in the program.
1450typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1451 PCHIdentifierLookupTable;
1452
Douglas Gregorc34897d2009-04-09 22:27:44 +00001453// FIXME: use the diagnostics machinery
1454static bool Error(const char *Str) {
1455 std::fprintf(stderr, "%s\n", Str);
1456 return true;
1457}
1458
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001459/// \brief Check the contents of the predefines buffer against the
1460/// contents of the predefines buffer used to build the PCH file.
1461///
1462/// The contents of the two predefines buffers should be the same. If
1463/// not, then some command-line option changed the preprocessor state
1464/// and we must reject the PCH file.
1465///
1466/// \param PCHPredef The start of the predefines buffer in the PCH
1467/// file.
1468///
1469/// \param PCHPredefLen The length of the predefines buffer in the PCH
1470/// file.
1471///
1472/// \param PCHBufferID The FileID for the PCH predefines buffer.
1473///
1474/// \returns true if there was a mismatch (in which case the PCH file
1475/// should be ignored), or false otherwise.
1476bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1477 unsigned PCHPredefLen,
1478 FileID PCHBufferID) {
1479 const char *Predef = PP.getPredefines().c_str();
1480 unsigned PredefLen = PP.getPredefines().size();
1481
1482 // If the two predefines buffers compare equal, we're done!.
1483 if (PredefLen == PCHPredefLen &&
1484 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1485 return false;
1486
1487 // The predefines buffers are different. Produce a reasonable
1488 // diagnostic showing where they are different.
1489
1490 // The source locations (potentially in the two different predefines
1491 // buffers)
1492 SourceLocation Loc1, Loc2;
1493 SourceManager &SourceMgr = PP.getSourceManager();
1494
1495 // Create a source buffer for our predefines string, so
1496 // that we can build a diagnostic that points into that
1497 // source buffer.
1498 FileID BufferID;
1499 if (Predef && Predef[0]) {
1500 llvm::MemoryBuffer *Buffer
1501 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1502 "<built-in>");
1503 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1504 }
1505
1506 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1507 std::pair<const char *, const char *> Locations
1508 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1509
1510 if (Locations.first != Predef + MinLen) {
1511 // We found the location in the two buffers where there is a
1512 // difference. Form source locations to point there (in both
1513 // buffers).
1514 unsigned Offset = Locations.first - Predef;
1515 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1516 .getFileLocWithOffset(Offset);
1517 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1518 .getFileLocWithOffset(Offset);
1519 } else if (PredefLen > PCHPredefLen) {
1520 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1521 .getFileLocWithOffset(MinLen);
1522 } else {
1523 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1524 .getFileLocWithOffset(MinLen);
1525 }
1526
1527 Diag(Loc1, diag::warn_pch_preprocessor);
1528 if (Loc2.isValid())
1529 Diag(Loc2, diag::note_predef_in_pch);
1530 Diag(diag::note_ignoring_pch) << FileName;
1531 return true;
1532}
1533
Douglas Gregor635f97f2009-04-13 16:31:14 +00001534/// \brief Read the line table in the source manager block.
1535/// \returns true if ther was an error.
1536static bool ParseLineTable(SourceManager &SourceMgr,
1537 llvm::SmallVectorImpl<uint64_t> &Record) {
1538 unsigned Idx = 0;
1539 LineTableInfo &LineTable = SourceMgr.getLineTable();
1540
1541 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001542 std::map<int, int> FileIDs;
1543 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001544 // Extract the file name
1545 unsigned FilenameLen = Record[Idx++];
1546 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1547 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001548 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1549 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001550 }
1551
1552 // Parse the line entries
1553 std::vector<LineEntry> Entries;
1554 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001555 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001556
1557 // Extract the line entries
1558 unsigned NumEntries = Record[Idx++];
1559 Entries.clear();
1560 Entries.reserve(NumEntries);
1561 for (unsigned I = 0; I != NumEntries; ++I) {
1562 unsigned FileOffset = Record[Idx++];
1563 unsigned LineNo = Record[Idx++];
1564 int FilenameID = Record[Idx++];
1565 SrcMgr::CharacteristicKind FileKind
1566 = (SrcMgr::CharacteristicKind)Record[Idx++];
1567 unsigned IncludeOffset = Record[Idx++];
1568 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1569 FileKind, IncludeOffset));
1570 }
1571 LineTable.AddEntry(FID, Entries);
1572 }
1573
1574 return false;
1575}
1576
Douglas Gregorab1cef72009-04-10 03:52:48 +00001577/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001578PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001579 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001580 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1581 Error("Malformed source manager block record");
1582 return Failure;
1583 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001584
1585 SourceManager &SourceMgr = Context.getSourceManager();
1586 RecordData Record;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001587 unsigned NumHeaderInfos = 0;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001588 while (true) {
1589 unsigned Code = Stream.ReadCode();
1590 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001591 if (Stream.ReadBlockEnd()) {
1592 Error("Error at end of Source Manager block");
1593 return Failure;
1594 }
1595
1596 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001597 }
1598
1599 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1600 // No known subblocks, always skip them.
1601 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001602 if (Stream.SkipBlock()) {
1603 Error("Malformed block record");
1604 return Failure;
1605 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001606 continue;
1607 }
1608
1609 if (Code == llvm::bitc::DEFINE_ABBREV) {
1610 Stream.ReadAbbrevRecord();
1611 continue;
1612 }
1613
1614 // Read a record.
1615 const char *BlobStart;
1616 unsigned BlobLen;
1617 Record.clear();
1618 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1619 default: // Default behavior: ignore.
1620 break;
1621
1622 case pch::SM_SLOC_FILE_ENTRY: {
1623 // FIXME: We would really like to delay the creation of this
1624 // FileEntry until it is actually required, e.g., when producing
1625 // a diagnostic with a source location in this file.
1626 const FileEntry *File
1627 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1628 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001629 FileID ID = SourceMgr.createFileID(File,
1630 SourceLocation::getFromRawEncoding(Record[1]),
1631 (CharacteristicKind)Record[2]);
1632 if (Record[3])
1633 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1634 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001635 break;
1636 }
1637
1638 case pch::SM_SLOC_BUFFER_ENTRY: {
1639 const char *Name = BlobStart;
1640 unsigned Code = Stream.ReadCode();
1641 Record.clear();
1642 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1643 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001644 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001645 llvm::MemoryBuffer *Buffer
1646 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1647 BlobStart + BlobLen - 1,
1648 Name);
1649 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1650
1651 if (strcmp(Name, "<built-in>") == 0
1652 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1653 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001654 break;
1655 }
1656
1657 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1658 SourceLocation SpellingLoc
1659 = SourceLocation::getFromRawEncoding(Record[1]);
1660 SourceMgr.createInstantiationLoc(
1661 SpellingLoc,
1662 SourceLocation::getFromRawEncoding(Record[2]),
1663 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001664 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001665 break;
1666 }
1667
Chris Lattnere1be6022009-04-14 23:22:57 +00001668 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001669 if (ParseLineTable(SourceMgr, Record))
1670 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001671 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001672
1673 case pch::SM_HEADER_FILE_INFO: {
1674 HeaderFileInfo HFI;
1675 HFI.isImport = Record[0];
1676 HFI.DirInfo = Record[1];
1677 HFI.NumIncludes = Record[2];
1678 HFI.ControllingMacroID = Record[3];
1679 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1680 break;
1681 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001682 }
1683 }
1684}
1685
Chris Lattner4fc71eb2009-04-27 01:05:14 +00001686/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1687/// specified cursor. Read the abbreviations that are at the top of the block
1688/// and then leave the cursor pointing into the block.
1689bool PCHReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
1690 unsigned BlockID) {
1691 if (Cursor.EnterSubBlock(BlockID)) {
1692 Error("Malformed block record");
1693 return Failure;
1694 }
1695
1696 RecordData Record;
1697 while (true) {
1698 unsigned Code = Cursor.ReadCode();
1699
1700 // We expect all abbrevs to be at the start of the block.
1701 if (Code != llvm::bitc::DEFINE_ABBREV)
1702 return false;
1703 Cursor.ReadAbbrevRecord();
1704 }
1705}
1706
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001707void PCHReader::ReadMacroRecord(uint64_t Offset) {
1708 // Keep track of where we are in the stream, then jump back there
1709 // after reading this macro.
1710 SavedStreamPosition SavedPosition(Stream);
1711
1712 Stream.JumpToBit(Offset);
1713 RecordData Record;
1714 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1715 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001716
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001717 while (true) {
1718 unsigned Code = Stream.ReadCode();
1719 switch (Code) {
1720 case llvm::bitc::END_BLOCK:
1721 return;
1722
1723 case llvm::bitc::ENTER_SUBBLOCK:
1724 // No known subblocks, always skip them.
1725 Stream.ReadSubBlockID();
1726 if (Stream.SkipBlock()) {
1727 Error("Malformed block record");
1728 return;
1729 }
1730 continue;
1731
1732 case llvm::bitc::DEFINE_ABBREV:
1733 Stream.ReadAbbrevRecord();
1734 continue;
1735 default: break;
1736 }
1737
1738 // Read a record.
1739 Record.clear();
1740 pch::PreprocessorRecordTypes RecType =
1741 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1742 switch (RecType) {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001743 case pch::PP_MACRO_OBJECT_LIKE:
1744 case pch::PP_MACRO_FUNCTION_LIKE: {
1745 // If we already have a macro, that means that we've hit the end
1746 // of the definition of the macro we were looking for. We're
1747 // done.
1748 if (Macro)
1749 return;
1750
1751 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1752 if (II == 0) {
1753 Error("Macro must have a name");
1754 return;
1755 }
1756 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1757 bool isUsed = Record[2];
1758
1759 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1760 MI->setIsUsed(isUsed);
1761
1762 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1763 // Decode function-like macro info.
1764 bool isC99VarArgs = Record[3];
1765 bool isGNUVarArgs = Record[4];
1766 MacroArgs.clear();
1767 unsigned NumArgs = Record[5];
1768 for (unsigned i = 0; i != NumArgs; ++i)
1769 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1770
1771 // Install function-like macro info.
1772 MI->setIsFunctionLike();
1773 if (isC99VarArgs) MI->setIsC99Varargs();
1774 if (isGNUVarArgs) MI->setIsGNUVarargs();
1775 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1776 PP.getPreprocessorAllocator());
1777 }
1778
1779 // Finally, install the macro.
1780 PP.setMacroInfo(II, MI);
1781
1782 // Remember that we saw this macro last so that we add the tokens that
1783 // form its body to it.
1784 Macro = MI;
1785 ++NumMacrosRead;
1786 break;
1787 }
1788
1789 case pch::PP_TOKEN: {
1790 // If we see a TOKEN before a PP_MACRO_*, then the file is
1791 // erroneous, just pretend we didn't see this.
1792 if (Macro == 0) break;
1793
1794 Token Tok;
1795 Tok.startToken();
1796 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1797 Tok.setLength(Record[1]);
1798 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1799 Tok.setIdentifierInfo(II);
1800 Tok.setKind((tok::TokenKind)Record[3]);
1801 Tok.setFlag((Token::TokenFlags)Record[4]);
1802 Macro->AddTokenToBody(Tok);
1803 break;
1804 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001805 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001806 }
1807}
1808
Douglas Gregorc713da92009-04-21 22:25:48 +00001809PCHReader::PCHReadResult
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001810PCHReader::ReadPCHBlock() {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001811 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1812 Error("Malformed block record");
1813 return Failure;
1814 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001815
1816 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001817 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001818 while (!Stream.AtEndOfStream()) {
1819 unsigned Code = Stream.ReadCode();
1820 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001821 if (Stream.ReadBlockEnd()) {
1822 Error("Error at end of module block");
1823 return Failure;
1824 }
Chris Lattner29241862009-04-11 21:15:38 +00001825
Douglas Gregor179cfb12009-04-10 20:39:37 +00001826 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001827 }
1828
1829 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1830 switch (Stream.ReadSubBlockID()) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001831 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1832 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001833 if (Stream.SkipBlock()) {
1834 Error("Malformed block record");
1835 return Failure;
1836 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001837 break;
1838
Chris Lattner4fc71eb2009-04-27 01:05:14 +00001839 case pch::DECLS_BLOCK_ID:
1840 // We lazily load the decls block, but we want to set up the
1841 // DeclsCursor cursor to point into it. Clone our current bitcode
1842 // cursor to it, enter the block and read the abbrevs in that block.
1843 // With the main cursor, we just skip over it.
1844 DeclsCursor = Stream;
1845 if (Stream.SkipBlock() || // Skip with the main cursor.
1846 // Read the abbrevs.
1847 ReadBlockAbbrevs(DeclsCursor, pch::DECLS_BLOCK_ID)) {
1848 Error("Malformed block record");
1849 return Failure;
1850 }
1851 break;
1852
Chris Lattner29241862009-04-11 21:15:38 +00001853 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner29241862009-04-11 21:15:38 +00001854 if (Stream.SkipBlock()) {
1855 Error("Malformed block record");
1856 return Failure;
1857 }
1858 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001859
Douglas Gregorab1cef72009-04-10 03:52:48 +00001860 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001861 switch (ReadSourceManagerBlock()) {
1862 case Success:
1863 break;
1864
1865 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001866 Error("Malformed source manager block");
1867 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001868
1869 case IgnorePCH:
1870 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001871 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001872 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001873 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001874 continue;
1875 }
1876
1877 if (Code == llvm::bitc::DEFINE_ABBREV) {
1878 Stream.ReadAbbrevRecord();
1879 continue;
1880 }
1881
1882 // Read and process a record.
1883 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001884 const char *BlobStart = 0;
1885 unsigned BlobLen = 0;
1886 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1887 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001888 default: // Default behavior: ignore.
1889 break;
1890
1891 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001892 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001893 Error("Duplicate TYPE_OFFSET record in PCH file");
1894 return Failure;
1895 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001896 TypeOffsets = (const uint64_t *)BlobStart;
1897 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001898 break;
1899
1900 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001901 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001902 Error("Duplicate DECL_OFFSET record in PCH file");
1903 return Failure;
1904 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001905 DeclOffsets = (const uint64_t *)BlobStart;
1906 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001907 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001908
1909 case pch::LANGUAGE_OPTIONS:
1910 if (ParseLanguageOptions(Record))
1911 return IgnorePCH;
1912 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001913
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001914 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001915 std::string TargetTriple(BlobStart, BlobLen);
1916 if (TargetTriple != Context.Target.getTargetTriple()) {
1917 Diag(diag::warn_pch_target_triple)
1918 << TargetTriple << Context.Target.getTargetTriple();
1919 Diag(diag::note_ignoring_pch) << FileName;
1920 return IgnorePCH;
1921 }
1922 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001923 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001924
1925 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001926 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001927 if (Record[0]) {
1928 IdentifierLookupTable
1929 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +00001930 (const unsigned char *)IdentifierTableData + Record[0],
1931 (const unsigned char *)IdentifierTableData,
1932 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001933 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1934 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001935 break;
1936
1937 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001938 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001939 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1940 return Failure;
1941 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001942 IdentifierOffsets = (const uint32_t *)BlobStart;
1943 IdentifiersLoaded.resize(Record[0]);
Douglas Gregoreccb51d2009-04-25 23:30:02 +00001944 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001945 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001946
1947 case pch::EXTERNAL_DEFINITIONS:
1948 if (!ExternalDefinitions.empty()) {
1949 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1950 return Failure;
1951 }
1952 ExternalDefinitions.swap(Record);
1953 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001954
Douglas Gregore01ad442009-04-18 05:55:16 +00001955 case pch::SPECIAL_TYPES:
1956 SpecialTypes.swap(Record);
1957 break;
1958
Douglas Gregor456e0952009-04-17 22:13:46 +00001959 case pch::STATISTICS:
1960 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001961 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001962 TotalLexicalDeclContexts = Record[2];
1963 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001964 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001965 case pch::TENTATIVE_DEFINITIONS:
1966 if (!TentativeDefinitions.empty()) {
1967 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1968 return Failure;
1969 }
1970 TentativeDefinitions.swap(Record);
1971 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001972
1973 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1974 if (!LocallyScopedExternalDecls.empty()) {
1975 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1976 return Failure;
1977 }
1978 LocallyScopedExternalDecls.swap(Record);
1979 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001980
Douglas Gregor2d711832009-04-25 17:48:32 +00001981 case pch::SELECTOR_OFFSETS:
1982 SelectorOffsets = (const uint32_t *)BlobStart;
1983 TotalNumSelectors = Record[0];
1984 SelectorsLoaded.resize(TotalNumSelectors);
1985 break;
1986
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001987 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +00001988 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1989 if (Record[0])
1990 MethodPoolLookupTable
1991 = PCHMethodPoolLookupTable::Create(
1992 MethodPoolLookupTableData + Record[0],
1993 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001994 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +00001995 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001996 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001997
1998 case pch::PP_COUNTER_VALUE:
1999 if (!Record.empty())
2000 PP.setCounterValue(Record[0]);
2001 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002002 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002003 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00002004 Error("Premature end of bitstream");
2005 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002006}
2007
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002008PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002009 // Set the PCH file name.
2010 this->FileName = FileName;
2011
Douglas Gregorc34897d2009-04-09 22:27:44 +00002012 // Open the PCH file.
2013 std::string ErrStr;
2014 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002015 if (!Buffer) {
2016 Error(ErrStr.c_str());
2017 return IgnorePCH;
2018 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002019
2020 // Initialize the stream
Chris Lattner587788a2009-04-26 20:59:20 +00002021 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2022 (const unsigned char *)Buffer->getBufferEnd());
2023 Stream.init(StreamFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002024
2025 // Sniff for the signature.
2026 if (Stream.Read(8) != 'C' ||
2027 Stream.Read(8) != 'P' ||
2028 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002029 Stream.Read(8) != 'H') {
2030 Error("Not a PCH file");
2031 return IgnorePCH;
2032 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002033
Douglas Gregorc34897d2009-04-09 22:27:44 +00002034 while (!Stream.AtEndOfStream()) {
2035 unsigned Code = Stream.ReadCode();
2036
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002037 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2038 Error("Invalid record at top-level");
2039 return Failure;
2040 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002041
2042 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00002043
Douglas Gregorc34897d2009-04-09 22:27:44 +00002044 // We only know the PCH subblock ID.
2045 switch (BlockID) {
2046 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002047 if (Stream.ReadBlockInfoBlock()) {
2048 Error("Malformed BlockInfoBlock");
2049 return Failure;
2050 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002051 break;
2052 case pch::PCH_BLOCK_ID:
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002053 switch (ReadPCHBlock()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002054 case Success:
2055 break;
2056
2057 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002058 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002059
2060 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002061 // FIXME: We could consider reading through to the end of this
2062 // PCH block, skipping subblocks, to see if there are other
2063 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002064 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002065 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002066 break;
2067 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002068 if (Stream.SkipBlock()) {
2069 Error("Malformed block record");
2070 return Failure;
2071 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002072 break;
2073 }
2074 }
2075
2076 // Load the translation unit declaration
2077 ReadDeclRecord(DeclOffsets[0], 0);
2078
Douglas Gregorc713da92009-04-21 22:25:48 +00002079 // Initialization of builtins and library builtins occurs before the
2080 // PCH file is read, so there may be some identifiers that were
2081 // loaded into the IdentifierTable before we intercepted the
2082 // creation of identifiers. Iterate through the list of known
2083 // identifiers and determine whether we have to establish
2084 // preprocessor definitions or top-level identifier declaration
2085 // chains for those identifiers.
2086 //
2087 // We copy the IdentifierInfo pointers to a small vector first,
2088 // since de-serializing declarations or macro definitions can add
2089 // new entries into the identifier table, invalidating the
2090 // iterators.
2091 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2092 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2093 IdEnd = PP.getIdentifierTable().end();
2094 Id != IdEnd; ++Id)
2095 Identifiers.push_back(Id->second);
2096 PCHIdentifierLookupTable *IdTable
2097 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2098 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2099 IdentifierInfo *II = Identifiers[I];
2100 // Look in the on-disk hash table for an entry for
2101 PCHIdentifierLookupTrait Info(*this, II);
2102 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2103 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2104 if (Pos == IdTable->end())
2105 continue;
2106
2107 // Dereferencing the iterator has the effect of populating the
2108 // IdentifierInfo node with the various declarations it needs.
2109 (void)*Pos;
2110 }
2111
Douglas Gregore01ad442009-04-18 05:55:16 +00002112 // Load the special types.
2113 Context.setBuiltinVaListType(
2114 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002115 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2116 Context.setObjCIdType(GetType(Id));
2117 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2118 Context.setObjCSelType(GetType(Sel));
2119 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2120 Context.setObjCProtoType(GetType(Proto));
2121 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2122 Context.setObjCClassType(GetType(Class));
2123 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2124 Context.setCFConstantStringType(GetType(String));
2125 if (unsigned FastEnum
2126 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2127 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002128
Douglas Gregorc713da92009-04-21 22:25:48 +00002129 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002130}
2131
Douglas Gregor179cfb12009-04-10 20:39:37 +00002132/// \brief Parse the record that corresponds to a LangOptions data
2133/// structure.
2134///
2135/// This routine compares the language options used to generate the
2136/// PCH file against the language options set for the current
2137/// compilation. For each option, we classify differences between the
2138/// two compiler states as either "benign" or "important". Benign
2139/// differences don't matter, and we accept them without complaint
2140/// (and without modifying the language options). Differences between
2141/// the states for important options cause the PCH file to be
2142/// unusable, so we emit a warning and return true to indicate that
2143/// there was an error.
2144///
2145/// \returns true if the PCH file is unacceptable, false otherwise.
2146bool PCHReader::ParseLanguageOptions(
2147 const llvm::SmallVectorImpl<uint64_t> &Record) {
2148 const LangOptions &LangOpts = Context.getLangOptions();
2149#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2150#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2151 if (Record[Idx] != LangOpts.Option) { \
2152 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2153 Diag(diag::note_ignoring_pch) << FileName; \
2154 return true; \
2155 } \
2156 ++Idx
2157
2158 unsigned Idx = 0;
2159 PARSE_LANGOPT_BENIGN(Trigraphs);
2160 PARSE_LANGOPT_BENIGN(BCPLComment);
2161 PARSE_LANGOPT_BENIGN(DollarIdents);
2162 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2163 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2164 PARSE_LANGOPT_BENIGN(ImplicitInt);
2165 PARSE_LANGOPT_BENIGN(Digraphs);
2166 PARSE_LANGOPT_BENIGN(HexFloats);
2167 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2168 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2169 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2170 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2171 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2172 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2173 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2174 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2175 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2176 PARSE_LANGOPT_BENIGN(PascalStrings);
2177 PARSE_LANGOPT_BENIGN(Boolean);
2178 PARSE_LANGOPT_BENIGN(WritableStrings);
2179 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2180 diag::warn_pch_lax_vector_conversions);
2181 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2182 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2183 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2184 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2185 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2186 diag::warn_pch_thread_safe_statics);
2187 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2188 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2189 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2190 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2191 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2192 diag::warn_pch_heinous_extensions);
2193 // FIXME: Most of the options below are benign if the macro wasn't
2194 // used. Unfortunately, this means that a PCH compiled without
2195 // optimization can't be used with optimization turned on, even
2196 // though the only thing that changes is whether __OPTIMIZE__ was
2197 // defined... but if __OPTIMIZE__ never showed up in the header, it
2198 // doesn't matter. We could consider making this some special kind
2199 // of check.
2200 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2201 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2202 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2203 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2204 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2205 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2206 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2207 Diag(diag::warn_pch_gc_mode)
2208 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2209 Diag(diag::note_ignoring_pch) << FileName;
2210 return true;
2211 }
2212 ++Idx;
2213 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2214 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2215#undef PARSE_LANGOPT_IRRELEVANT
2216#undef PARSE_LANGOPT_BENIGN
2217
2218 return false;
2219}
2220
Douglas Gregorc34897d2009-04-09 22:27:44 +00002221/// \brief Read and return the type at the given offset.
2222///
2223/// This routine actually reads the record corresponding to the type
2224/// at the given offset in the bitstream. It is a helper routine for
2225/// GetType, which deals with reading type IDs.
2226QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002227 // Keep track of where we are in the stream, then jump back there
2228 // after reading this type.
2229 SavedStreamPosition SavedPosition(Stream);
2230
Douglas Gregorc34897d2009-04-09 22:27:44 +00002231 Stream.JumpToBit(Offset);
2232 RecordData Record;
2233 unsigned Code = Stream.ReadCode();
2234 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002235 case pch::TYPE_EXT_QUAL: {
2236 assert(Record.size() == 3 &&
2237 "Incorrect encoding of extended qualifier type");
2238 QualType Base = GetType(Record[0]);
2239 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2240 unsigned AddressSpace = Record[2];
2241
2242 QualType T = Base;
2243 if (GCAttr != QualType::GCNone)
2244 T = Context.getObjCGCQualType(T, GCAttr);
2245 if (AddressSpace)
2246 T = Context.getAddrSpaceQualType(T, AddressSpace);
2247 return T;
2248 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002249
Douglas Gregorc34897d2009-04-09 22:27:44 +00002250 case pch::TYPE_FIXED_WIDTH_INT: {
2251 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2252 return Context.getFixedWidthIntType(Record[0], Record[1]);
2253 }
2254
2255 case pch::TYPE_COMPLEX: {
2256 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2257 QualType ElemType = GetType(Record[0]);
2258 return Context.getComplexType(ElemType);
2259 }
2260
2261 case pch::TYPE_POINTER: {
2262 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2263 QualType PointeeType = GetType(Record[0]);
2264 return Context.getPointerType(PointeeType);
2265 }
2266
2267 case pch::TYPE_BLOCK_POINTER: {
2268 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2269 QualType PointeeType = GetType(Record[0]);
2270 return Context.getBlockPointerType(PointeeType);
2271 }
2272
2273 case pch::TYPE_LVALUE_REFERENCE: {
2274 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2275 QualType PointeeType = GetType(Record[0]);
2276 return Context.getLValueReferenceType(PointeeType);
2277 }
2278
2279 case pch::TYPE_RVALUE_REFERENCE: {
2280 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2281 QualType PointeeType = GetType(Record[0]);
2282 return Context.getRValueReferenceType(PointeeType);
2283 }
2284
2285 case pch::TYPE_MEMBER_POINTER: {
2286 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2287 QualType PointeeType = GetType(Record[0]);
2288 QualType ClassType = GetType(Record[1]);
2289 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2290 }
2291
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002292 case pch::TYPE_CONSTANT_ARRAY: {
2293 QualType ElementType = GetType(Record[0]);
2294 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2295 unsigned IndexTypeQuals = Record[2];
2296 unsigned Idx = 3;
2297 llvm::APInt Size = ReadAPInt(Record, Idx);
2298 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2299 }
2300
2301 case pch::TYPE_INCOMPLETE_ARRAY: {
2302 QualType ElementType = GetType(Record[0]);
2303 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2304 unsigned IndexTypeQuals = Record[2];
2305 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2306 }
2307
2308 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002309 QualType ElementType = GetType(Record[0]);
2310 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2311 unsigned IndexTypeQuals = Record[2];
2312 return Context.getVariableArrayType(ElementType, ReadExpr(),
2313 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002314 }
2315
2316 case pch::TYPE_VECTOR: {
2317 if (Record.size() != 2) {
2318 Error("Incorrect encoding of vector type in PCH file");
2319 return QualType();
2320 }
2321
2322 QualType ElementType = GetType(Record[0]);
2323 unsigned NumElements = Record[1];
2324 return Context.getVectorType(ElementType, NumElements);
2325 }
2326
2327 case pch::TYPE_EXT_VECTOR: {
2328 if (Record.size() != 2) {
2329 Error("Incorrect encoding of extended vector type in PCH file");
2330 return QualType();
2331 }
2332
2333 QualType ElementType = GetType(Record[0]);
2334 unsigned NumElements = Record[1];
2335 return Context.getExtVectorType(ElementType, NumElements);
2336 }
2337
2338 case pch::TYPE_FUNCTION_NO_PROTO: {
2339 if (Record.size() != 1) {
2340 Error("Incorrect encoding of no-proto function type");
2341 return QualType();
2342 }
2343 QualType ResultType = GetType(Record[0]);
2344 return Context.getFunctionNoProtoType(ResultType);
2345 }
2346
2347 case pch::TYPE_FUNCTION_PROTO: {
2348 QualType ResultType = GetType(Record[0]);
2349 unsigned Idx = 1;
2350 unsigned NumParams = Record[Idx++];
2351 llvm::SmallVector<QualType, 16> ParamTypes;
2352 for (unsigned I = 0; I != NumParams; ++I)
2353 ParamTypes.push_back(GetType(Record[Idx++]));
2354 bool isVariadic = Record[Idx++];
2355 unsigned Quals = Record[Idx++];
2356 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2357 isVariadic, Quals);
2358 }
2359
2360 case pch::TYPE_TYPEDEF:
2361 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2362 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2363
2364 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002365 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002366
2367 case pch::TYPE_TYPEOF: {
2368 if (Record.size() != 1) {
2369 Error("Incorrect encoding of typeof(type) in PCH file");
2370 return QualType();
2371 }
2372 QualType UnderlyingType = GetType(Record[0]);
2373 return Context.getTypeOfType(UnderlyingType);
2374 }
2375
2376 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002377 assert(Record.size() == 1 && "Incorrect encoding of record type");
2378 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002379
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002380 case pch::TYPE_ENUM:
2381 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2382 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2383
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002384 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002385 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2386 return Context.getObjCInterfaceType(
2387 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002388
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002389 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2390 unsigned Idx = 0;
2391 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2392 unsigned NumProtos = Record[Idx++];
2393 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2394 for (unsigned I = 0; I != NumProtos; ++I)
2395 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2396 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2397 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002398
Chris Lattner9b9f2352009-04-22 06:40:03 +00002399 case pch::TYPE_OBJC_QUALIFIED_ID: {
2400 unsigned Idx = 0;
2401 unsigned NumProtos = Record[Idx++];
2402 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2403 for (unsigned I = 0; I != NumProtos; ++I)
2404 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2405 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2406 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002407 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002408 // Suppress a GCC warning
2409 return QualType();
2410}
2411
2412/// \brief Note that we have loaded the declaration with the given
2413/// Index.
2414///
2415/// This routine notes that this declaration has already been loaded,
2416/// so that future GetDecl calls will return this declaration rather
2417/// than trying to load a new declaration.
2418inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002419 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2420 DeclsLoaded[Index] = D;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002421}
2422
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002423/// \brief Determine whether the consumer will be interested in seeing
2424/// this declaration (via HandleTopLevelDecl).
2425///
2426/// This routine should return true for anything that might affect
2427/// code generation, e.g., inline function definitions, Objective-C
2428/// declarations with metadata, etc.
2429static bool isConsumerInterestedIn(Decl *D) {
2430 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2431 return Var->isFileVarDecl() && Var->getInit();
2432 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2433 return Func->isThisDeclarationADefinition();
2434 return isa<ObjCProtocolDecl>(D);
2435}
2436
Douglas Gregorc34897d2009-04-09 22:27:44 +00002437/// \brief Read the declaration at the given offset from the PCH file.
2438Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002439 // Keep track of where we are in the stream, then jump back there
2440 // after reading this declaration.
2441 SavedStreamPosition SavedPosition(Stream);
2442
Douglas Gregorc34897d2009-04-09 22:27:44 +00002443 Decl *D = 0;
2444 Stream.JumpToBit(Offset);
2445 RecordData Record;
2446 unsigned Code = Stream.ReadCode();
2447 unsigned Idx = 0;
2448 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002449
Douglas Gregorc34897d2009-04-09 22:27:44 +00002450 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002451 case pch::DECL_ATTR:
2452 case pch::DECL_CONTEXT_LEXICAL:
2453 case pch::DECL_CONTEXT_VISIBLE:
2454 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2455 break;
2456
Douglas Gregorc34897d2009-04-09 22:27:44 +00002457 case pch::DECL_TRANSLATION_UNIT:
2458 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002459 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002460 break;
2461
2462 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002463 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002464 break;
2465 }
2466
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002467 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002468 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002469 break;
2470 }
2471
Douglas Gregor982365e2009-04-13 21:20:57 +00002472 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002473 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2474 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002475 break;
2476 }
2477
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002478 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002479 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2480 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002481 break;
2482 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002483
2484 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002485 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2486 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002487 break;
2488 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002489
Steve Naroff79ea0e02009-04-20 15:06:07 +00002490 case pch::DECL_OBJC_METHOD: {
2491 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2492 Selector(), QualType(), 0);
2493 break;
2494 }
2495
Steve Naroff97b53bd2009-04-21 15:12:33 +00002496 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002497 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2498 break;
2499 }
2500
Steve Naroff97b53bd2009-04-21 15:12:33 +00002501 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002502 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2503 ObjCIvarDecl::None);
2504 break;
2505 }
2506
Steve Naroff97b53bd2009-04-21 15:12:33 +00002507 case pch::DECL_OBJC_PROTOCOL: {
2508 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2509 break;
2510 }
2511
2512 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2513 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2514 QualType(), 0);
2515 break;
2516 }
2517
2518 case pch::DECL_OBJC_CLASS: {
2519 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2520 break;
2521 }
2522
2523 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2524 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2525 break;
2526 }
2527
2528 case pch::DECL_OBJC_CATEGORY: {
2529 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2530 break;
2531 }
2532
2533 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002534 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002535 break;
2536 }
2537
2538 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002539 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002540 break;
2541 }
2542
2543 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002544 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002545 break;
2546 }
2547
2548 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002549 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002550 break;
2551 }
2552
2553 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002554 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2555 SourceLocation(), 0,
2556 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002557 break;
2558 }
2559
Douglas Gregor982365e2009-04-13 21:20:57 +00002560 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002561 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2562 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002563 break;
2564 }
2565
Douglas Gregorce066712009-04-26 22:20:50 +00002566 case pch::DECL_VAR:
Douglas Gregorddf4d092009-04-16 22:29:51 +00002567 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2568 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002569 break;
Douglas Gregorce066712009-04-26 22:20:50 +00002570
2571 case pch::DECL_IMPLICIT_PARAM:
2572 D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType());
2573 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002574
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002575 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002576 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2577 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002578 break;
2579 }
2580
2581 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002582 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002583 QualType(), QualType(), VarDecl::None,
2584 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002585 break;
2586 }
2587
Douglas Gregor2a491792009-04-13 22:49:25 +00002588 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002589 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002590 break;
2591 }
2592
2593 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002594 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002595 break;
2596 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002597 }
2598
Douglas Gregorc713da92009-04-21 22:25:48 +00002599 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002600 if (D) {
2601 LoadedDecl(Index, D);
2602 Reader.Visit(D);
2603 }
2604
Douglas Gregorc34897d2009-04-09 22:27:44 +00002605 // If this declaration is also a declaration context, get the
2606 // offsets for its tables of lexical and visible declarations.
2607 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2608 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2609 if (Offsets.first || Offsets.second) {
2610 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2611 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2612 DeclContextOffsets[DC] = Offsets;
2613 }
2614 }
2615 assert(Idx == Record.size());
2616
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002617 // If we have deserialized a declaration that has a definition the
2618 // AST consumer might need to know about, notify the consumer
2619 // about that definition now or queue it for later.
2620 if (isConsumerInterestedIn(D)) {
2621 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002622 DeclGroupRef DG(D);
2623 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002624 } else {
2625 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002626 }
2627 }
2628
Douglas Gregorc34897d2009-04-09 22:27:44 +00002629 return D;
2630}
2631
Douglas Gregorac8f2802009-04-10 17:25:41 +00002632QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002633 unsigned Quals = ID & 0x07;
2634 unsigned Index = ID >> 3;
2635
2636 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2637 QualType T;
2638 switch ((pch::PredefinedTypeIDs)Index) {
2639 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2640 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2641 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2642
2643 case pch::PREDEF_TYPE_CHAR_U_ID:
2644 case pch::PREDEF_TYPE_CHAR_S_ID:
2645 // FIXME: Check that the signedness of CharTy is correct!
2646 T = Context.CharTy;
2647 break;
2648
2649 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2650 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2651 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2652 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2653 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2654 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2655 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2656 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2657 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2658 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2659 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2660 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2661 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2662 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2663 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2664 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2665 }
2666
2667 assert(!T.isNull() && "Unknown predefined type");
2668 return T.getQualifiedType(Quals);
2669 }
2670
2671 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregore43f0972009-04-26 03:49:13 +00002672 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Douglas Gregor24a224c2009-04-25 18:35:21 +00002673 if (!TypesLoaded[Index])
2674 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002675
Douglas Gregor24a224c2009-04-25 18:35:21 +00002676 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002677}
2678
Douglas Gregorac8f2802009-04-10 17:25:41 +00002679Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002680 if (ID == 0)
2681 return 0;
2682
Douglas Gregor24a224c2009-04-25 18:35:21 +00002683 if (ID > DeclsLoaded.size()) {
2684 Error("Declaration ID out-of-range for PCH file");
2685 return 0;
2686 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002687
Douglas Gregor24a224c2009-04-25 18:35:21 +00002688 unsigned Index = ID - 1;
2689 if (!DeclsLoaded[Index])
2690 ReadDeclRecord(DeclOffsets[Index], Index);
2691
2692 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002693}
2694
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002695Stmt *PCHReader::GetStmt(uint64_t Offset) {
2696 // Keep track of where we are in the stream, then jump back there
2697 // after reading this declaration.
2698 SavedStreamPosition SavedPosition(Stream);
2699
2700 Stream.JumpToBit(Offset);
2701 return ReadStmt();
2702}
2703
Douglas Gregorc34897d2009-04-09 22:27:44 +00002704bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002705 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002706 assert(DC->hasExternalLexicalStorage() &&
2707 "DeclContext has no lexical decls in storage");
2708 uint64_t Offset = DeclContextOffsets[DC].first;
2709 assert(Offset && "DeclContext has no lexical decls in storage");
2710
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002711 // Keep track of where we are in the stream, then jump back there
2712 // after reading this context.
2713 SavedStreamPosition SavedPosition(Stream);
2714
Douglas Gregorc34897d2009-04-09 22:27:44 +00002715 // Load the record containing all of the declarations lexically in
2716 // this context.
2717 Stream.JumpToBit(Offset);
2718 RecordData Record;
2719 unsigned Code = Stream.ReadCode();
2720 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002721 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002722 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2723
2724 // Load all of the declaration IDs
2725 Decls.clear();
2726 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002727 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002728 return false;
2729}
2730
2731bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2732 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2733 assert(DC->hasExternalVisibleStorage() &&
2734 "DeclContext has no visible decls in storage");
2735 uint64_t Offset = DeclContextOffsets[DC].second;
2736 assert(Offset && "DeclContext has no visible decls in storage");
2737
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002738 // Keep track of where we are in the stream, then jump back there
2739 // after reading this context.
2740 SavedStreamPosition SavedPosition(Stream);
2741
Douglas Gregorc34897d2009-04-09 22:27:44 +00002742 // Load the record containing all of the declarations visible in
2743 // this context.
2744 Stream.JumpToBit(Offset);
2745 RecordData Record;
2746 unsigned Code = Stream.ReadCode();
2747 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002748 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002749 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2750 if (Record.size() == 0)
2751 return false;
2752
2753 Decls.clear();
2754
2755 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002756 while (Idx < Record.size()) {
2757 Decls.push_back(VisibleDeclaration());
2758 Decls.back().Name = ReadDeclarationName(Record, Idx);
2759
Douglas Gregorc34897d2009-04-09 22:27:44 +00002760 unsigned Size = Record[Idx++];
2761 llvm::SmallVector<unsigned, 4> & LoadedDecls
2762 = Decls.back().Declarations;
2763 LoadedDecls.reserve(Size);
2764 for (unsigned I = 0; I < Size; ++I)
2765 LoadedDecls.push_back(Record[Idx++]);
2766 }
2767
Douglas Gregoraf136d92009-04-22 22:34:57 +00002768 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002769 return false;
2770}
2771
Douglas Gregor631f6c62009-04-14 00:24:19 +00002772void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002773 this->Consumer = Consumer;
2774
Douglas Gregor631f6c62009-04-14 00:24:19 +00002775 if (!Consumer)
2776 return;
2777
2778 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2779 Decl *D = GetDecl(ExternalDefinitions[I]);
2780 DeclGroupRef DG(D);
2781 Consumer->HandleTopLevelDecl(DG);
2782 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002783
2784 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2785 DeclGroupRef DG(InterestingDecls[I]);
2786 Consumer->HandleTopLevelDecl(DG);
2787 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002788}
2789
Douglas Gregorc34897d2009-04-09 22:27:44 +00002790void PCHReader::PrintStats() {
2791 std::fprintf(stderr, "*** PCH Statistics:\n");
2792
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002793 unsigned NumTypesLoaded
2794 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2795 (Type *)0);
2796 unsigned NumDeclsLoaded
2797 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2798 (Decl *)0);
2799 unsigned NumIdentifiersLoaded
2800 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2801 IdentifiersLoaded.end(),
2802 (IdentifierInfo *)0);
2803 unsigned NumSelectorsLoaded
2804 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2805 SelectorsLoaded.end(),
2806 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00002807
Douglas Gregor24a224c2009-04-25 18:35:21 +00002808 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002809 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002810 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2811 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2812 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002813 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002814 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2815 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002816 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002817 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002818 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2819 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00002820 if (TotalNumSelectors)
2821 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2822 NumSelectorsLoaded, TotalNumSelectors,
2823 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2824 if (TotalNumStatements)
2825 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2826 NumStatementsRead, TotalNumStatements,
2827 ((float)NumStatementsRead/TotalNumStatements * 100));
2828 if (TotalNumMacros)
2829 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2830 NumMacrosRead, TotalNumMacros,
2831 ((float)NumMacrosRead/TotalNumMacros * 100));
2832 if (TotalLexicalDeclContexts)
2833 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2834 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2835 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2836 * 100));
2837 if (TotalVisibleDeclContexts)
2838 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2839 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2840 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2841 * 100));
2842 if (TotalSelectorsInMethodPool) {
2843 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2844 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2845 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2846 * 100));
2847 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2848 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002849 std::fprintf(stderr, "\n");
2850}
2851
Douglas Gregorc713da92009-04-21 22:25:48 +00002852void PCHReader::InitializeSema(Sema &S) {
2853 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002854 S.ExternalSource = this;
2855
Douglas Gregor2554cf22009-04-22 21:15:06 +00002856 // Makes sure any declarations that were deserialized "too early"
2857 // still get added to the identifier's declaration chains.
2858 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2859 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2860 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002861 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002862 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002863
2864 // If there were any tentative definitions, deserialize them and add
2865 // them to Sema's table of tentative definitions.
2866 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2867 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2868 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2869 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002870
2871 // If there were any locally-scoped external declarations,
2872 // deserialize them and add them to Sema's table of locally-scoped
2873 // external declarations.
2874 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2875 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2876 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2877 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002878}
2879
2880IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2881 // Try to find this name within our on-disk hash table
2882 PCHIdentifierLookupTable *IdTable
2883 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2884 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2885 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2886 if (Pos == IdTable->end())
2887 return 0;
2888
2889 // Dereferencing the iterator has the effect of building the
2890 // IdentifierInfo node and populating it with the various
2891 // declarations it needs.
2892 return *Pos;
2893}
2894
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002895std::pair<ObjCMethodList, ObjCMethodList>
2896PCHReader::ReadMethodPool(Selector Sel) {
2897 if (!MethodPoolLookupTable)
2898 return std::pair<ObjCMethodList, ObjCMethodList>();
2899
2900 // Try to find this selector within our on-disk hash table.
2901 PCHMethodPoolLookupTable *PoolTable
2902 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2903 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00002904 if (Pos == PoolTable->end()) {
2905 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002906 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00002907 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002908
Douglas Gregor2d711832009-04-25 17:48:32 +00002909 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002910 return *Pos;
2911}
2912
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002913void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00002914 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002915 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2916 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00002917}
2918
Chris Lattner29241862009-04-11 21:15:38 +00002919IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002920 if (ID == 0)
2921 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002922
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002923 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002924 Error("No identifier table in PCH file");
2925 return 0;
2926 }
Chris Lattner29241862009-04-11 21:15:38 +00002927
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002928 if (!IdentifiersLoaded[ID - 1]) {
2929 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002930 const char *Str = IdentifierTableData + Offset;
Douglas Gregor85c4a872009-04-25 21:04:17 +00002931
2932 // If there is an identifier lookup table, but the offset of this
2933 // string is after the identifier table itself, then we know that
2934 // this string is not in the on-disk hash table. Therefore,
2935 // disable lookup into the hash table when looking for this
2936 // identifier.
2937 PCHIdentifierLookupTable *IdTable
2938 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002939 if (!IdTable ||
2940 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
2941 // Turn off lookup into the on-disk hash table. We know that
2942 // this identifier is not there.
2943 if (IdTable)
2944 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002945
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002946 // All of the strings in the PCH file are preceded by a 16-bit
2947 // length. Extract that 16-bit length to avoid having to execute
2948 // strlen().
2949 const char *StrLenPtr = Str - 2;
2950 unsigned StrLen = (((unsigned) StrLenPtr[0])
2951 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
2952 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002953
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002954 // Turn on lookup into the on-disk hash table, if we have an
2955 // on-disk hash table.
2956 if (IdTable)
2957 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2958 } else {
2959 // The identifier is a key in our on-disk hash table. Since we
2960 // know where the hash table entry starts, just read in this
2961 // (key, value) pair.
2962 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
2963 const unsigned char *Pos = (const unsigned char *)Str - 4;
2964 std::pair<unsigned, unsigned> KeyDataLengths
2965 = Trait.ReadKeyDataLength(Pos);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002966
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002967 PCHIdentifierLookupTrait::internal_key_type InternalKey
2968 = Trait.ReadKey(Pos, KeyDataLengths.first);
2969 Pos = (const unsigned char *)Str + KeyDataLengths.first;
2970 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
2971 KeyDataLengths.second);
2972 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002973 }
Chris Lattner29241862009-04-11 21:15:38 +00002974
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002975 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002976}
2977
Steve Naroff9e84d782009-04-23 10:39:46 +00002978Selector PCHReader::DecodeSelector(unsigned ID) {
2979 if (ID == 0)
2980 return Selector();
2981
Douglas Gregor2d711832009-04-25 17:48:32 +00002982 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002983 Error("No selector table in PCH file");
2984 return Selector();
2985 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002986
2987 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002988 Error("Selector ID out of range");
2989 return Selector();
2990 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002991
2992 unsigned Index = ID - 1;
2993 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2994 // Load this selector from the selector table.
2995 // FIXME: endianness portability issues with SelectorOffsets table
2996 PCHMethodPoolLookupTrait Trait(*this);
2997 SelectorsLoaded[Index]
2998 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2999 }
3000
3001 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00003002}
3003
Douglas Gregorc34897d2009-04-09 22:27:44 +00003004DeclarationName
3005PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
3006 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3007 switch (Kind) {
3008 case DeclarationName::Identifier:
3009 return DeclarationName(GetIdentifierInfo(Record, Idx));
3010
3011 case DeclarationName::ObjCZeroArgSelector:
3012 case DeclarationName::ObjCOneArgSelector:
3013 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00003014 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00003015
3016 case DeclarationName::CXXConstructorName:
3017 return Context.DeclarationNames.getCXXConstructorName(
3018 GetType(Record[Idx++]));
3019
3020 case DeclarationName::CXXDestructorName:
3021 return Context.DeclarationNames.getCXXDestructorName(
3022 GetType(Record[Idx++]));
3023
3024 case DeclarationName::CXXConversionFunctionName:
3025 return Context.DeclarationNames.getCXXConversionFunctionName(
3026 GetType(Record[Idx++]));
3027
3028 case DeclarationName::CXXOperatorName:
3029 return Context.DeclarationNames.getCXXOperatorName(
3030 (OverloadedOperatorKind)Record[Idx++]);
3031
3032 case DeclarationName::CXXUsingDirective:
3033 return DeclarationName::getUsingDirectiveName();
3034 }
3035
3036 // Required to silence GCC warning
3037 return DeclarationName();
3038}
Douglas Gregor179cfb12009-04-10 20:39:37 +00003039
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00003040/// \brief Read an integral value
3041llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3042 unsigned BitWidth = Record[Idx++];
3043 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3044 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3045 Idx += NumWords;
3046 return Result;
3047}
3048
3049/// \brief Read a signed integral value
3050llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3051 bool isUnsigned = Record[Idx++];
3052 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3053}
3054
Douglas Gregore2f37202009-04-14 21:55:33 +00003055/// \brief Read a floating-point value
3056llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00003057 return llvm::APFloat(ReadAPInt(Record, Idx));
3058}
3059
Douglas Gregor1c507882009-04-15 21:30:51 +00003060// \brief Read a string
3061std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3062 unsigned Len = Record[Idx++];
3063 std::string Result(&Record[Idx], &Record[Idx] + Len);
3064 Idx += Len;
3065 return Result;
3066}
3067
3068/// \brief Reads attributes from the current stream position.
3069Attr *PCHReader::ReadAttributes() {
3070 unsigned Code = Stream.ReadCode();
3071 assert(Code == llvm::bitc::UNABBREV_RECORD &&
3072 "Expected unabbreviated record"); (void)Code;
3073
3074 RecordData Record;
3075 unsigned Idx = 0;
3076 unsigned RecCode = Stream.ReadRecord(Code, Record);
3077 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
3078 (void)RecCode;
3079
3080#define SIMPLE_ATTR(Name) \
3081 case Attr::Name: \
3082 New = ::new (Context) Name##Attr(); \
3083 break
3084
3085#define STRING_ATTR(Name) \
3086 case Attr::Name: \
3087 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
3088 break
3089
3090#define UNSIGNED_ATTR(Name) \
3091 case Attr::Name: \
3092 New = ::new (Context) Name##Attr(Record[Idx++]); \
3093 break
3094
3095 Attr *Attrs = 0;
3096 while (Idx < Record.size()) {
3097 Attr *New = 0;
3098 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
3099 bool IsInherited = Record[Idx++];
3100
3101 switch (Kind) {
3102 STRING_ATTR(Alias);
3103 UNSIGNED_ATTR(Aligned);
3104 SIMPLE_ATTR(AlwaysInline);
3105 SIMPLE_ATTR(AnalyzerNoReturn);
3106 STRING_ATTR(Annotate);
3107 STRING_ATTR(AsmLabel);
3108
3109 case Attr::Blocks:
3110 New = ::new (Context) BlocksAttr(
3111 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3112 break;
3113
3114 case Attr::Cleanup:
3115 New = ::new (Context) CleanupAttr(
3116 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3117 break;
3118
3119 SIMPLE_ATTR(Const);
3120 UNSIGNED_ATTR(Constructor);
3121 SIMPLE_ATTR(DLLExport);
3122 SIMPLE_ATTR(DLLImport);
3123 SIMPLE_ATTR(Deprecated);
3124 UNSIGNED_ATTR(Destructor);
3125 SIMPLE_ATTR(FastCall);
3126
3127 case Attr::Format: {
3128 std::string Type = ReadString(Record, Idx);
3129 unsigned FormatIdx = Record[Idx++];
3130 unsigned FirstArg = Record[Idx++];
3131 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3132 break;
3133 }
3134
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003135 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003136
3137 case Attr::IBOutletKind:
3138 New = ::new (Context) IBOutletAttr();
3139 break;
3140
3141 SIMPLE_ATTR(NoReturn);
3142 SIMPLE_ATTR(NoThrow);
3143 SIMPLE_ATTR(Nodebug);
3144 SIMPLE_ATTR(Noinline);
3145
3146 case Attr::NonNull: {
3147 unsigned Size = Record[Idx++];
3148 llvm::SmallVector<unsigned, 16> ArgNums;
3149 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3150 Idx += Size;
3151 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3152 break;
3153 }
3154
3155 SIMPLE_ATTR(ObjCException);
3156 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003157 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003158 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003159 SIMPLE_ATTR(Overloadable);
3160 UNSIGNED_ATTR(Packed);
3161 SIMPLE_ATTR(Pure);
3162 UNSIGNED_ATTR(Regparm);
3163 STRING_ATTR(Section);
3164 SIMPLE_ATTR(StdCall);
3165 SIMPLE_ATTR(TransparentUnion);
3166 SIMPLE_ATTR(Unavailable);
3167 SIMPLE_ATTR(Unused);
3168 SIMPLE_ATTR(Used);
3169
3170 case Attr::Visibility:
3171 New = ::new (Context) VisibilityAttr(
3172 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3173 break;
3174
3175 SIMPLE_ATTR(WarnUnusedResult);
3176 SIMPLE_ATTR(Weak);
3177 SIMPLE_ATTR(WeakImport);
3178 }
3179
3180 assert(New && "Unable to decode attribute?");
3181 New->setInherited(IsInherited);
3182 New->setNext(Attrs);
3183 Attrs = New;
3184 }
3185#undef UNSIGNED_ATTR
3186#undef STRING_ATTR
3187#undef SIMPLE_ATTR
3188
3189 // The list of attributes was built backwards. Reverse the list
3190 // before returning it.
3191 Attr *PrevAttr = 0, *NextAttr = 0;
3192 while (Attrs) {
3193 NextAttr = Attrs->getNext();
3194 Attrs->setNext(PrevAttr);
3195 PrevAttr = Attrs;
3196 Attrs = NextAttr;
3197 }
3198
3199 return PrevAttr;
3200}
3201
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003202Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003203 // Within the bitstream, expressions are stored in Reverse Polish
3204 // Notation, with each of the subexpressions preceding the
3205 // expression they are stored in. To evaluate expressions, we
3206 // continue reading expressions and placing them on the stack, with
3207 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003208 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003209 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003210 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003211 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003212 llvm::SmallVector<Stmt *, 16> StmtStack;
3213 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003214 Stmt::EmptyShell Empty;
3215
Douglas Gregora151ba42009-04-14 23:32:43 +00003216 while (true) {
3217 unsigned Code = Stream.ReadCode();
3218 if (Code == llvm::bitc::END_BLOCK) {
3219 if (Stream.ReadBlockEnd()) {
3220 Error("Error at end of Source Manager block");
3221 return 0;
3222 }
3223 break;
3224 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003225
Douglas Gregora151ba42009-04-14 23:32:43 +00003226 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3227 // No known subblocks, always skip them.
3228 Stream.ReadSubBlockID();
3229 if (Stream.SkipBlock()) {
3230 Error("Malformed block record");
3231 return 0;
3232 }
3233 continue;
3234 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003235
Douglas Gregora151ba42009-04-14 23:32:43 +00003236 if (Code == llvm::bitc::DEFINE_ABBREV) {
3237 Stream.ReadAbbrevRecord();
3238 continue;
3239 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003240
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003241 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003242 Idx = 0;
3243 Record.clear();
3244 bool Finished = false;
3245 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003246 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003247 Finished = true;
3248 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003249
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003250 case pch::STMT_NULL_PTR:
3251 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003252 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003253
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003254 case pch::STMT_NULL:
3255 S = new (Context) NullStmt(Empty);
3256 break;
3257
3258 case pch::STMT_COMPOUND:
3259 S = new (Context) CompoundStmt(Empty);
3260 break;
3261
3262 case pch::STMT_CASE:
3263 S = new (Context) CaseStmt(Empty);
3264 break;
3265
3266 case pch::STMT_DEFAULT:
3267 S = new (Context) DefaultStmt(Empty);
3268 break;
3269
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003270 case pch::STMT_LABEL:
3271 S = new (Context) LabelStmt(Empty);
3272 break;
3273
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003274 case pch::STMT_IF:
3275 S = new (Context) IfStmt(Empty);
3276 break;
3277
3278 case pch::STMT_SWITCH:
3279 S = new (Context) SwitchStmt(Empty);
3280 break;
3281
Douglas Gregora6b503f2009-04-17 00:16:09 +00003282 case pch::STMT_WHILE:
3283 S = new (Context) WhileStmt(Empty);
3284 break;
3285
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003286 case pch::STMT_DO:
3287 S = new (Context) DoStmt(Empty);
3288 break;
3289
3290 case pch::STMT_FOR:
3291 S = new (Context) ForStmt(Empty);
3292 break;
3293
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003294 case pch::STMT_GOTO:
3295 S = new (Context) GotoStmt(Empty);
3296 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003297
3298 case pch::STMT_INDIRECT_GOTO:
3299 S = new (Context) IndirectGotoStmt(Empty);
3300 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003301
Douglas Gregora6b503f2009-04-17 00:16:09 +00003302 case pch::STMT_CONTINUE:
3303 S = new (Context) ContinueStmt(Empty);
3304 break;
3305
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003306 case pch::STMT_BREAK:
3307 S = new (Context) BreakStmt(Empty);
3308 break;
3309
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003310 case pch::STMT_RETURN:
3311 S = new (Context) ReturnStmt(Empty);
3312 break;
3313
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003314 case pch::STMT_DECL:
3315 S = new (Context) DeclStmt(Empty);
3316 break;
3317
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003318 case pch::STMT_ASM:
3319 S = new (Context) AsmStmt(Empty);
3320 break;
3321
Douglas Gregora151ba42009-04-14 23:32:43 +00003322 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003323 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003324 break;
3325
3326 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003327 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003328 break;
3329
3330 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003331 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003332 break;
3333
3334 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003335 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003336 break;
3337
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003338 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003339 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003340 break;
3341
Douglas Gregor596e0932009-04-15 16:35:07 +00003342 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003343 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003344 Record[PCHStmtReader::NumExprFields + 1]);
3345 break;
3346
Douglas Gregora151ba42009-04-14 23:32:43 +00003347 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003348 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003349 break;
3350
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003351 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003352 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003353 break;
3354
Douglas Gregor12d74052009-04-15 15:58:59 +00003355 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003356 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003357 break;
3358
3359 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003360 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003361 break;
3362
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003363 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003364 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003365 break;
3366
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003367 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003368 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003369 break;
3370
3371 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003372 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003373 break;
3374
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003375 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003376 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003377 break;
3378
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003379 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003380 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003381 break;
3382
3383 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003384 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003385 break;
3386
Douglas Gregora151ba42009-04-14 23:32:43 +00003387 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003388 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003389 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003390
3391 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003392 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003393 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003394
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003395 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003396 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003397 break;
3398
Douglas Gregorec0b8292009-04-15 23:02:49 +00003399 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003400 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003401 break;
3402
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003403 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003404 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003405 break;
3406
3407 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003408 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003409 Record[PCHStmtReader::NumExprFields] - 1);
3410
3411 break;
3412
3413 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003414 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003415 break;
3416
Douglas Gregorec0b8292009-04-15 23:02:49 +00003417 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003418 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003419 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003420
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003421 case pch::EXPR_ADDR_LABEL:
3422 S = new (Context) AddrLabelExpr(Empty);
3423 break;
3424
Douglas Gregoreca12f62009-04-17 19:05:30 +00003425 case pch::EXPR_STMT:
3426 S = new (Context) StmtExpr(Empty);
3427 break;
3428
Douglas Gregor209d4622009-04-15 23:33:31 +00003429 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003430 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003431 break;
3432
3433 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003434 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003435 break;
3436
3437 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003438 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003439 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003440
3441 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003442 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003443 break;
3444
Douglas Gregore246b742009-04-17 19:21:43 +00003445 case pch::EXPR_BLOCK:
3446 S = new (Context) BlockExpr(Empty);
3447 break;
3448
Douglas Gregor725e94b2009-04-16 00:01:45 +00003449 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003450 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003451 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003452
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003453 case pch::EXPR_OBJC_STRING_LITERAL:
3454 S = new (Context) ObjCStringLiteral(Empty);
3455 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003456 case pch::EXPR_OBJC_ENCODE:
3457 S = new (Context) ObjCEncodeExpr(Empty);
3458 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003459 case pch::EXPR_OBJC_SELECTOR_EXPR:
3460 S = new (Context) ObjCSelectorExpr(Empty);
3461 break;
3462 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3463 S = new (Context) ObjCProtocolExpr(Empty);
3464 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003465 case pch::EXPR_OBJC_IVAR_REF_EXPR:
3466 S = new (Context) ObjCIvarRefExpr(Empty);
3467 break;
3468 case pch::EXPR_OBJC_PROPERTY_REF_EXPR:
3469 S = new (Context) ObjCPropertyRefExpr(Empty);
3470 break;
3471 case pch::EXPR_OBJC_KVC_REF_EXPR:
3472 S = new (Context) ObjCKVCRefExpr(Empty);
3473 break;
Steve Narofffb3e4022009-04-25 14:04:28 +00003474 case pch::EXPR_OBJC_MESSAGE_EXPR:
3475 S = new (Context) ObjCMessageExpr(Empty);
3476 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003477 case pch::EXPR_OBJC_SUPER_EXPR:
3478 S = new (Context) ObjCSuperExpr(Empty);
3479 break;
Steve Naroff79762bd2009-04-26 18:52:16 +00003480 case pch::STMT_OBJC_FOR_COLLECTION:
3481 S = new (Context) ObjCForCollectionStmt(Empty);
3482 break;
3483 case pch::STMT_OBJC_CATCH:
3484 S = new (Context) ObjCAtCatchStmt(Empty);
3485 break;
3486 case pch::STMT_OBJC_FINALLY:
3487 S = new (Context) ObjCAtFinallyStmt(Empty);
3488 break;
3489 case pch::STMT_OBJC_AT_TRY:
3490 S = new (Context) ObjCAtTryStmt(Empty);
3491 break;
3492 case pch::STMT_OBJC_AT_SYNCHRONIZED:
3493 S = new (Context) ObjCAtSynchronizedStmt(Empty);
3494 break;
3495 case pch::STMT_OBJC_AT_THROW:
3496 S = new (Context) ObjCAtThrowStmt(Empty);
3497 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003498 }
3499
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003500 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003501 if (Finished)
3502 break;
3503
Douglas Gregor456e0952009-04-17 22:13:46 +00003504 ++NumStatementsRead;
3505
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003506 if (S) {
3507 unsigned NumSubStmts = Reader.Visit(S);
3508 while (NumSubStmts > 0) {
3509 StmtStack.pop_back();
3510 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003511 }
3512 }
3513
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003514 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003515 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003516 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003517 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003518 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003519 return StmtStack.back();
3520}
3521
3522Expr *PCHReader::ReadExpr() {
3523 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003524}
3525
Douglas Gregor179cfb12009-04-10 20:39:37 +00003526DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003527 return Diag(SourceLocation(), DiagID);
3528}
3529
3530DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3531 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003532 Context.getSourceManager()),
3533 DiagID);
3534}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003535
Douglas Gregorc713da92009-04-21 22:25:48 +00003536/// \brief Retrieve the identifier table associated with the
3537/// preprocessor.
3538IdentifierTable &PCHReader::getIdentifierTable() {
3539 return PP.getIdentifierTable();
3540}
3541
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003542/// \brief Record that the given ID maps to the given switch-case
3543/// statement.
3544void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3545 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3546 SwitchCaseStmts[ID] = SC;
3547}
3548
3549/// \brief Retrieve the switch-case statement with the given ID.
3550SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3551 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3552 return SwitchCaseStmts[ID];
3553}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003554
3555/// \brief Record that the given label statement has been
3556/// deserialized and has the given ID.
3557void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3558 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3559 "Deserialized label twice");
3560 LabelStmts[ID] = S;
3561
3562 // If we've already seen any goto statements that point to this
3563 // label, resolve them now.
3564 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3565 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3566 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3567 Goto->second->setLabel(S);
3568 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003569
3570 // If we've already seen any address-label statements that point to
3571 // this label, resolve them now.
3572 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3573 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3574 = UnresolvedAddrLabelExprs.equal_range(ID);
3575 for (AddrLabelIter AddrLabel = AddrLabels.first;
3576 AddrLabel != AddrLabels.second; ++AddrLabel)
3577 AddrLabel->second->setLabel(S);
3578 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003579}
3580
3581/// \brief Set the label of the given statement to the label
3582/// identified by ID.
3583///
3584/// Depending on the order in which the label and other statements
3585/// referencing that label occur, this operation may complete
3586/// immediately (updating the statement) or it may queue the
3587/// statement to be back-patched later.
3588void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3589 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3590 if (Label != LabelStmts.end()) {
3591 // We've already seen this label, so set the label of the goto and
3592 // we're done.
3593 S->setLabel(Label->second);
3594 } else {
3595 // We haven't seen this label yet, so add this goto to the set of
3596 // unresolved goto statements.
3597 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3598 }
3599}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003600
3601/// \brief Set the label of the given expression to the label
3602/// identified by ID.
3603///
3604/// Depending on the order in which the label and other statements
3605/// referencing that label occur, this operation may complete
3606/// immediately (updating the statement) or it may queue the
3607/// statement to be back-patched later.
3608void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3609 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3610 if (Label != LabelStmts.end()) {
3611 // We've already seen this label, so set the label of the
3612 // label-address expression and we're done.
3613 S->setLabel(Label->second);
3614 } else {
3615 // We haven't seen this label yet, so add this label-address
3616 // expression to the set of unresolved label-address expressions.
3617 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3618 }
3619}