blob: a24ba124bc538e71168c5b84b52e064eb810eac8 [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
Chris Lattner4fc71eb2009-04-27 01:05:14 +00001696 while (true) {
1697 unsigned Code = Cursor.ReadCode();
1698
1699 // We expect all abbrevs to be at the start of the block.
1700 if (Code != llvm::bitc::DEFINE_ABBREV)
1701 return false;
1702 Cursor.ReadAbbrevRecord();
1703 }
1704}
1705
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001706void PCHReader::ReadMacroRecord(uint64_t Offset) {
1707 // Keep track of where we are in the stream, then jump back there
1708 // after reading this macro.
1709 SavedStreamPosition SavedPosition(Stream);
1710
1711 Stream.JumpToBit(Offset);
1712 RecordData Record;
1713 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1714 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001715
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001716 while (true) {
1717 unsigned Code = Stream.ReadCode();
1718 switch (Code) {
1719 case llvm::bitc::END_BLOCK:
1720 return;
1721
1722 case llvm::bitc::ENTER_SUBBLOCK:
1723 // No known subblocks, always skip them.
1724 Stream.ReadSubBlockID();
1725 if (Stream.SkipBlock()) {
1726 Error("Malformed block record");
1727 return;
1728 }
1729 continue;
1730
1731 case llvm::bitc::DEFINE_ABBREV:
1732 Stream.ReadAbbrevRecord();
1733 continue;
1734 default: break;
1735 }
1736
1737 // Read a record.
1738 Record.clear();
1739 pch::PreprocessorRecordTypes RecType =
1740 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1741 switch (RecType) {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001742 case pch::PP_MACRO_OBJECT_LIKE:
1743 case pch::PP_MACRO_FUNCTION_LIKE: {
1744 // If we already have a macro, that means that we've hit the end
1745 // of the definition of the macro we were looking for. We're
1746 // done.
1747 if (Macro)
1748 return;
1749
1750 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1751 if (II == 0) {
1752 Error("Macro must have a name");
1753 return;
1754 }
1755 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1756 bool isUsed = Record[2];
1757
1758 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1759 MI->setIsUsed(isUsed);
1760
1761 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1762 // Decode function-like macro info.
1763 bool isC99VarArgs = Record[3];
1764 bool isGNUVarArgs = Record[4];
1765 MacroArgs.clear();
1766 unsigned NumArgs = Record[5];
1767 for (unsigned i = 0; i != NumArgs; ++i)
1768 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1769
1770 // Install function-like macro info.
1771 MI->setIsFunctionLike();
1772 if (isC99VarArgs) MI->setIsC99Varargs();
1773 if (isGNUVarArgs) MI->setIsGNUVarargs();
1774 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1775 PP.getPreprocessorAllocator());
1776 }
1777
1778 // Finally, install the macro.
1779 PP.setMacroInfo(II, MI);
1780
1781 // Remember that we saw this macro last so that we add the tokens that
1782 // form its body to it.
1783 Macro = MI;
1784 ++NumMacrosRead;
1785 break;
1786 }
1787
1788 case pch::PP_TOKEN: {
1789 // If we see a TOKEN before a PP_MACRO_*, then the file is
1790 // erroneous, just pretend we didn't see this.
1791 if (Macro == 0) break;
1792
1793 Token Tok;
1794 Tok.startToken();
1795 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1796 Tok.setLength(Record[1]);
1797 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1798 Tok.setIdentifierInfo(II);
1799 Tok.setKind((tok::TokenKind)Record[3]);
1800 Tok.setFlag((Token::TokenFlags)Record[4]);
1801 Macro->AddTokenToBody(Tok);
1802 break;
1803 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001804 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001805 }
1806}
1807
Douglas Gregorc713da92009-04-21 22:25:48 +00001808PCHReader::PCHReadResult
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001809PCHReader::ReadPCHBlock() {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001810 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1811 Error("Malformed block record");
1812 return Failure;
1813 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001814
1815 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001816 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001817 while (!Stream.AtEndOfStream()) {
1818 unsigned Code = Stream.ReadCode();
1819 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001820 if (Stream.ReadBlockEnd()) {
1821 Error("Error at end of module block");
1822 return Failure;
1823 }
Chris Lattner29241862009-04-11 21:15:38 +00001824
Douglas Gregor179cfb12009-04-10 20:39:37 +00001825 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001826 }
1827
1828 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1829 switch (Stream.ReadSubBlockID()) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001830 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1831 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001832 if (Stream.SkipBlock()) {
1833 Error("Malformed block record");
1834 return Failure;
1835 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001836 break;
1837
Chris Lattner4fc71eb2009-04-27 01:05:14 +00001838 case pch::DECLS_BLOCK_ID:
1839 // We lazily load the decls block, but we want to set up the
1840 // DeclsCursor cursor to point into it. Clone our current bitcode
1841 // cursor to it, enter the block and read the abbrevs in that block.
1842 // With the main cursor, we just skip over it.
1843 DeclsCursor = Stream;
1844 if (Stream.SkipBlock() || // Skip with the main cursor.
1845 // Read the abbrevs.
1846 ReadBlockAbbrevs(DeclsCursor, pch::DECLS_BLOCK_ID)) {
1847 Error("Malformed block record");
1848 return Failure;
1849 }
1850 break;
1851
Chris Lattner29241862009-04-11 21:15:38 +00001852 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner29241862009-04-11 21:15:38 +00001853 if (Stream.SkipBlock()) {
1854 Error("Malformed block record");
1855 return Failure;
1856 }
1857 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001858
Douglas Gregorab1cef72009-04-10 03:52:48 +00001859 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001860 switch (ReadSourceManagerBlock()) {
1861 case Success:
1862 break;
1863
1864 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001865 Error("Malformed source manager block");
1866 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001867
1868 case IgnorePCH:
1869 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001870 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001871 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001872 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001873 continue;
1874 }
1875
1876 if (Code == llvm::bitc::DEFINE_ABBREV) {
1877 Stream.ReadAbbrevRecord();
1878 continue;
1879 }
1880
1881 // Read and process a record.
1882 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001883 const char *BlobStart = 0;
1884 unsigned BlobLen = 0;
1885 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1886 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001887 default: // Default behavior: ignore.
1888 break;
1889
1890 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001891 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001892 Error("Duplicate TYPE_OFFSET record in PCH file");
1893 return Failure;
1894 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001895 TypeOffsets = (const uint64_t *)BlobStart;
1896 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001897 break;
1898
1899 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001900 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001901 Error("Duplicate DECL_OFFSET record in PCH file");
1902 return Failure;
1903 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001904 DeclOffsets = (const uint64_t *)BlobStart;
1905 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001906 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001907
1908 case pch::LANGUAGE_OPTIONS:
1909 if (ParseLanguageOptions(Record))
1910 return IgnorePCH;
1911 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001912
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001913 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001914 std::string TargetTriple(BlobStart, BlobLen);
1915 if (TargetTriple != Context.Target.getTargetTriple()) {
1916 Diag(diag::warn_pch_target_triple)
1917 << TargetTriple << Context.Target.getTargetTriple();
1918 Diag(diag::note_ignoring_pch) << FileName;
1919 return IgnorePCH;
1920 }
1921 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001922 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001923
1924 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001925 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001926 if (Record[0]) {
1927 IdentifierLookupTable
1928 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +00001929 (const unsigned char *)IdentifierTableData + Record[0],
1930 (const unsigned char *)IdentifierTableData,
1931 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001932 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1933 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001934 break;
1935
1936 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001937 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001938 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1939 return Failure;
1940 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001941 IdentifierOffsets = (const uint32_t *)BlobStart;
1942 IdentifiersLoaded.resize(Record[0]);
Douglas Gregoreccb51d2009-04-25 23:30:02 +00001943 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001944 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001945
1946 case pch::EXTERNAL_DEFINITIONS:
1947 if (!ExternalDefinitions.empty()) {
1948 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1949 return Failure;
1950 }
1951 ExternalDefinitions.swap(Record);
1952 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001953
Douglas Gregore01ad442009-04-18 05:55:16 +00001954 case pch::SPECIAL_TYPES:
1955 SpecialTypes.swap(Record);
1956 break;
1957
Douglas Gregor456e0952009-04-17 22:13:46 +00001958 case pch::STATISTICS:
1959 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001960 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001961 TotalLexicalDeclContexts = Record[2];
1962 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001963 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001964 case pch::TENTATIVE_DEFINITIONS:
1965 if (!TentativeDefinitions.empty()) {
1966 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1967 return Failure;
1968 }
1969 TentativeDefinitions.swap(Record);
1970 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001971
1972 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1973 if (!LocallyScopedExternalDecls.empty()) {
1974 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1975 return Failure;
1976 }
1977 LocallyScopedExternalDecls.swap(Record);
1978 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001979
Douglas Gregor2d711832009-04-25 17:48:32 +00001980 case pch::SELECTOR_OFFSETS:
1981 SelectorOffsets = (const uint32_t *)BlobStart;
1982 TotalNumSelectors = Record[0];
1983 SelectorsLoaded.resize(TotalNumSelectors);
1984 break;
1985
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001986 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +00001987 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1988 if (Record[0])
1989 MethodPoolLookupTable
1990 = PCHMethodPoolLookupTable::Create(
1991 MethodPoolLookupTableData + Record[0],
1992 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001993 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +00001994 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001995 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001996
1997 case pch::PP_COUNTER_VALUE:
1998 if (!Record.empty())
1999 PP.setCounterValue(Record[0]);
2000 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002001 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002002 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00002003 Error("Premature end of bitstream");
2004 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002005}
2006
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002007PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002008 // Set the PCH file name.
2009 this->FileName = FileName;
2010
Douglas Gregorc34897d2009-04-09 22:27:44 +00002011 // Open the PCH file.
2012 std::string ErrStr;
2013 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002014 if (!Buffer) {
2015 Error(ErrStr.c_str());
2016 return IgnorePCH;
2017 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002018
2019 // Initialize the stream
Chris Lattner587788a2009-04-26 20:59:20 +00002020 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
2021 (const unsigned char *)Buffer->getBufferEnd());
2022 Stream.init(StreamFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002023
2024 // Sniff for the signature.
2025 if (Stream.Read(8) != 'C' ||
2026 Stream.Read(8) != 'P' ||
2027 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002028 Stream.Read(8) != 'H') {
2029 Error("Not a PCH file");
2030 return IgnorePCH;
2031 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002032
Douglas Gregorc34897d2009-04-09 22:27:44 +00002033 while (!Stream.AtEndOfStream()) {
2034 unsigned Code = Stream.ReadCode();
2035
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002036 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2037 Error("Invalid record at top-level");
2038 return Failure;
2039 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002040
2041 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00002042
Douglas Gregorc34897d2009-04-09 22:27:44 +00002043 // We only know the PCH subblock ID.
2044 switch (BlockID) {
2045 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002046 if (Stream.ReadBlockInfoBlock()) {
2047 Error("Malformed BlockInfoBlock");
2048 return Failure;
2049 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002050 break;
2051 case pch::PCH_BLOCK_ID:
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002052 switch (ReadPCHBlock()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002053 case Success:
2054 break;
2055
2056 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002057 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002058
2059 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002060 // FIXME: We could consider reading through to the end of this
2061 // PCH block, skipping subblocks, to see if there are other
2062 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002063 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002064 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002065 break;
2066 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002067 if (Stream.SkipBlock()) {
2068 Error("Malformed block record");
2069 return Failure;
2070 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002071 break;
2072 }
2073 }
2074
2075 // Load the translation unit declaration
2076 ReadDeclRecord(DeclOffsets[0], 0);
2077
Douglas Gregorc713da92009-04-21 22:25:48 +00002078 // Initialization of builtins and library builtins occurs before the
2079 // PCH file is read, so there may be some identifiers that were
2080 // loaded into the IdentifierTable before we intercepted the
2081 // creation of identifiers. Iterate through the list of known
2082 // identifiers and determine whether we have to establish
2083 // preprocessor definitions or top-level identifier declaration
2084 // chains for those identifiers.
2085 //
2086 // We copy the IdentifierInfo pointers to a small vector first,
2087 // since de-serializing declarations or macro definitions can add
2088 // new entries into the identifier table, invalidating the
2089 // iterators.
2090 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2091 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2092 IdEnd = PP.getIdentifierTable().end();
2093 Id != IdEnd; ++Id)
2094 Identifiers.push_back(Id->second);
2095 PCHIdentifierLookupTable *IdTable
2096 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2097 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2098 IdentifierInfo *II = Identifiers[I];
2099 // Look in the on-disk hash table for an entry for
2100 PCHIdentifierLookupTrait Info(*this, II);
2101 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2102 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2103 if (Pos == IdTable->end())
2104 continue;
2105
2106 // Dereferencing the iterator has the effect of populating the
2107 // IdentifierInfo node with the various declarations it needs.
2108 (void)*Pos;
2109 }
2110
Douglas Gregore01ad442009-04-18 05:55:16 +00002111 // Load the special types.
2112 Context.setBuiltinVaListType(
2113 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002114 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2115 Context.setObjCIdType(GetType(Id));
2116 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2117 Context.setObjCSelType(GetType(Sel));
2118 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2119 Context.setObjCProtoType(GetType(Proto));
2120 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2121 Context.setObjCClassType(GetType(Class));
2122 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2123 Context.setCFConstantStringType(GetType(String));
2124 if (unsigned FastEnum
2125 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2126 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002127
Douglas Gregorc713da92009-04-21 22:25:48 +00002128 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002129}
2130
Douglas Gregor179cfb12009-04-10 20:39:37 +00002131/// \brief Parse the record that corresponds to a LangOptions data
2132/// structure.
2133///
2134/// This routine compares the language options used to generate the
2135/// PCH file against the language options set for the current
2136/// compilation. For each option, we classify differences between the
2137/// two compiler states as either "benign" or "important". Benign
2138/// differences don't matter, and we accept them without complaint
2139/// (and without modifying the language options). Differences between
2140/// the states for important options cause the PCH file to be
2141/// unusable, so we emit a warning and return true to indicate that
2142/// there was an error.
2143///
2144/// \returns true if the PCH file is unacceptable, false otherwise.
2145bool PCHReader::ParseLanguageOptions(
2146 const llvm::SmallVectorImpl<uint64_t> &Record) {
2147 const LangOptions &LangOpts = Context.getLangOptions();
2148#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2149#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2150 if (Record[Idx] != LangOpts.Option) { \
2151 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2152 Diag(diag::note_ignoring_pch) << FileName; \
2153 return true; \
2154 } \
2155 ++Idx
2156
2157 unsigned Idx = 0;
2158 PARSE_LANGOPT_BENIGN(Trigraphs);
2159 PARSE_LANGOPT_BENIGN(BCPLComment);
2160 PARSE_LANGOPT_BENIGN(DollarIdents);
2161 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2162 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2163 PARSE_LANGOPT_BENIGN(ImplicitInt);
2164 PARSE_LANGOPT_BENIGN(Digraphs);
2165 PARSE_LANGOPT_BENIGN(HexFloats);
2166 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2167 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2168 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2169 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2170 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2171 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2172 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2173 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2174 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2175 PARSE_LANGOPT_BENIGN(PascalStrings);
2176 PARSE_LANGOPT_BENIGN(Boolean);
2177 PARSE_LANGOPT_BENIGN(WritableStrings);
2178 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2179 diag::warn_pch_lax_vector_conversions);
2180 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2181 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2182 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2183 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2184 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2185 diag::warn_pch_thread_safe_statics);
2186 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2187 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2188 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2189 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2190 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2191 diag::warn_pch_heinous_extensions);
2192 // FIXME: Most of the options below are benign if the macro wasn't
2193 // used. Unfortunately, this means that a PCH compiled without
2194 // optimization can't be used with optimization turned on, even
2195 // though the only thing that changes is whether __OPTIMIZE__ was
2196 // defined... but if __OPTIMIZE__ never showed up in the header, it
2197 // doesn't matter. We could consider making this some special kind
2198 // of check.
2199 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2200 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2201 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2202 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2203 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2204 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2205 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2206 Diag(diag::warn_pch_gc_mode)
2207 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2208 Diag(diag::note_ignoring_pch) << FileName;
2209 return true;
2210 }
2211 ++Idx;
2212 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2213 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2214#undef PARSE_LANGOPT_IRRELEVANT
2215#undef PARSE_LANGOPT_BENIGN
2216
2217 return false;
2218}
2219
Douglas Gregorc34897d2009-04-09 22:27:44 +00002220/// \brief Read and return the type at the given offset.
2221///
2222/// This routine actually reads the record corresponding to the type
2223/// at the given offset in the bitstream. It is a helper routine for
2224/// GetType, which deals with reading type IDs.
2225QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002226 // Keep track of where we are in the stream, then jump back there
2227 // after reading this type.
2228 SavedStreamPosition SavedPosition(Stream);
2229
Douglas Gregorc34897d2009-04-09 22:27:44 +00002230 Stream.JumpToBit(Offset);
2231 RecordData Record;
2232 unsigned Code = Stream.ReadCode();
2233 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002234 case pch::TYPE_EXT_QUAL: {
2235 assert(Record.size() == 3 &&
2236 "Incorrect encoding of extended qualifier type");
2237 QualType Base = GetType(Record[0]);
2238 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2239 unsigned AddressSpace = Record[2];
2240
2241 QualType T = Base;
2242 if (GCAttr != QualType::GCNone)
2243 T = Context.getObjCGCQualType(T, GCAttr);
2244 if (AddressSpace)
2245 T = Context.getAddrSpaceQualType(T, AddressSpace);
2246 return T;
2247 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002248
Douglas Gregorc34897d2009-04-09 22:27:44 +00002249 case pch::TYPE_FIXED_WIDTH_INT: {
2250 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2251 return Context.getFixedWidthIntType(Record[0], Record[1]);
2252 }
2253
2254 case pch::TYPE_COMPLEX: {
2255 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2256 QualType ElemType = GetType(Record[0]);
2257 return Context.getComplexType(ElemType);
2258 }
2259
2260 case pch::TYPE_POINTER: {
2261 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2262 QualType PointeeType = GetType(Record[0]);
2263 return Context.getPointerType(PointeeType);
2264 }
2265
2266 case pch::TYPE_BLOCK_POINTER: {
2267 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2268 QualType PointeeType = GetType(Record[0]);
2269 return Context.getBlockPointerType(PointeeType);
2270 }
2271
2272 case pch::TYPE_LVALUE_REFERENCE: {
2273 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2274 QualType PointeeType = GetType(Record[0]);
2275 return Context.getLValueReferenceType(PointeeType);
2276 }
2277
2278 case pch::TYPE_RVALUE_REFERENCE: {
2279 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2280 QualType PointeeType = GetType(Record[0]);
2281 return Context.getRValueReferenceType(PointeeType);
2282 }
2283
2284 case pch::TYPE_MEMBER_POINTER: {
2285 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2286 QualType PointeeType = GetType(Record[0]);
2287 QualType ClassType = GetType(Record[1]);
2288 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2289 }
2290
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002291 case pch::TYPE_CONSTANT_ARRAY: {
2292 QualType ElementType = GetType(Record[0]);
2293 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2294 unsigned IndexTypeQuals = Record[2];
2295 unsigned Idx = 3;
2296 llvm::APInt Size = ReadAPInt(Record, Idx);
2297 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2298 }
2299
2300 case pch::TYPE_INCOMPLETE_ARRAY: {
2301 QualType ElementType = GetType(Record[0]);
2302 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2303 unsigned IndexTypeQuals = Record[2];
2304 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2305 }
2306
2307 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002308 QualType ElementType = GetType(Record[0]);
2309 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2310 unsigned IndexTypeQuals = Record[2];
2311 return Context.getVariableArrayType(ElementType, ReadExpr(),
2312 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002313 }
2314
2315 case pch::TYPE_VECTOR: {
2316 if (Record.size() != 2) {
2317 Error("Incorrect encoding of vector type in PCH file");
2318 return QualType();
2319 }
2320
2321 QualType ElementType = GetType(Record[0]);
2322 unsigned NumElements = Record[1];
2323 return Context.getVectorType(ElementType, NumElements);
2324 }
2325
2326 case pch::TYPE_EXT_VECTOR: {
2327 if (Record.size() != 2) {
2328 Error("Incorrect encoding of extended vector type in PCH file");
2329 return QualType();
2330 }
2331
2332 QualType ElementType = GetType(Record[0]);
2333 unsigned NumElements = Record[1];
2334 return Context.getExtVectorType(ElementType, NumElements);
2335 }
2336
2337 case pch::TYPE_FUNCTION_NO_PROTO: {
2338 if (Record.size() != 1) {
2339 Error("Incorrect encoding of no-proto function type");
2340 return QualType();
2341 }
2342 QualType ResultType = GetType(Record[0]);
2343 return Context.getFunctionNoProtoType(ResultType);
2344 }
2345
2346 case pch::TYPE_FUNCTION_PROTO: {
2347 QualType ResultType = GetType(Record[0]);
2348 unsigned Idx = 1;
2349 unsigned NumParams = Record[Idx++];
2350 llvm::SmallVector<QualType, 16> ParamTypes;
2351 for (unsigned I = 0; I != NumParams; ++I)
2352 ParamTypes.push_back(GetType(Record[Idx++]));
2353 bool isVariadic = Record[Idx++];
2354 unsigned Quals = Record[Idx++];
2355 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2356 isVariadic, Quals);
2357 }
2358
2359 case pch::TYPE_TYPEDEF:
2360 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2361 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2362
2363 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002364 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002365
2366 case pch::TYPE_TYPEOF: {
2367 if (Record.size() != 1) {
2368 Error("Incorrect encoding of typeof(type) in PCH file");
2369 return QualType();
2370 }
2371 QualType UnderlyingType = GetType(Record[0]);
2372 return Context.getTypeOfType(UnderlyingType);
2373 }
2374
2375 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002376 assert(Record.size() == 1 && "Incorrect encoding of record type");
2377 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002378
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002379 case pch::TYPE_ENUM:
2380 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2381 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2382
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002383 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002384 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2385 return Context.getObjCInterfaceType(
2386 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002387
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002388 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2389 unsigned Idx = 0;
2390 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2391 unsigned NumProtos = Record[Idx++];
2392 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2393 for (unsigned I = 0; I != NumProtos; ++I)
2394 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2395 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2396 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002397
Chris Lattner9b9f2352009-04-22 06:40:03 +00002398 case pch::TYPE_OBJC_QUALIFIED_ID: {
2399 unsigned Idx = 0;
2400 unsigned NumProtos = Record[Idx++];
2401 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2402 for (unsigned I = 0; I != NumProtos; ++I)
2403 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2404 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2405 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002406 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002407 // Suppress a GCC warning
2408 return QualType();
2409}
2410
2411/// \brief Note that we have loaded the declaration with the given
2412/// Index.
2413///
2414/// This routine notes that this declaration has already been loaded,
2415/// so that future GetDecl calls will return this declaration rather
2416/// than trying to load a new declaration.
2417inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002418 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2419 DeclsLoaded[Index] = D;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420}
2421
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002422/// \brief Determine whether the consumer will be interested in seeing
2423/// this declaration (via HandleTopLevelDecl).
2424///
2425/// This routine should return true for anything that might affect
2426/// code generation, e.g., inline function definitions, Objective-C
2427/// declarations with metadata, etc.
2428static bool isConsumerInterestedIn(Decl *D) {
2429 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2430 return Var->isFileVarDecl() && Var->getInit();
2431 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2432 return Func->isThisDeclarationADefinition();
2433 return isa<ObjCProtocolDecl>(D);
2434}
2435
Douglas Gregorc34897d2009-04-09 22:27:44 +00002436/// \brief Read the declaration at the given offset from the PCH file.
2437Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002438 // Keep track of where we are in the stream, then jump back there
2439 // after reading this declaration.
2440 SavedStreamPosition SavedPosition(Stream);
2441
Douglas Gregorc34897d2009-04-09 22:27:44 +00002442 Decl *D = 0;
2443 Stream.JumpToBit(Offset);
2444 RecordData Record;
2445 unsigned Code = Stream.ReadCode();
2446 unsigned Idx = 0;
2447 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002448
Douglas Gregorc34897d2009-04-09 22:27:44 +00002449 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002450 case pch::DECL_ATTR:
2451 case pch::DECL_CONTEXT_LEXICAL:
2452 case pch::DECL_CONTEXT_VISIBLE:
2453 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2454 break;
2455
Douglas Gregorc34897d2009-04-09 22:27:44 +00002456 case pch::DECL_TRANSLATION_UNIT:
2457 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002458 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002459 break;
2460
2461 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002462 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002463 break;
2464 }
2465
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002466 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002467 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002468 break;
2469 }
2470
Douglas Gregor982365e2009-04-13 21:20:57 +00002471 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002472 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2473 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002474 break;
2475 }
2476
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002477 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002478 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2479 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002480 break;
2481 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002482
2483 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002484 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2485 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002486 break;
2487 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002488
Steve Naroff79ea0e02009-04-20 15:06:07 +00002489 case pch::DECL_OBJC_METHOD: {
2490 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2491 Selector(), QualType(), 0);
2492 break;
2493 }
2494
Steve Naroff97b53bd2009-04-21 15:12:33 +00002495 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002496 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2497 break;
2498 }
2499
Steve Naroff97b53bd2009-04-21 15:12:33 +00002500 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002501 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2502 ObjCIvarDecl::None);
2503 break;
2504 }
2505
Steve Naroff97b53bd2009-04-21 15:12:33 +00002506 case pch::DECL_OBJC_PROTOCOL: {
2507 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2508 break;
2509 }
2510
2511 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2512 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2513 QualType(), 0);
2514 break;
2515 }
2516
2517 case pch::DECL_OBJC_CLASS: {
2518 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2519 break;
2520 }
2521
2522 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2523 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2524 break;
2525 }
2526
2527 case pch::DECL_OBJC_CATEGORY: {
2528 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2529 break;
2530 }
2531
2532 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002533 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002534 break;
2535 }
2536
2537 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002538 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002539 break;
2540 }
2541
2542 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002543 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002544 break;
2545 }
2546
2547 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002548 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002549 break;
2550 }
2551
2552 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002553 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2554 SourceLocation(), 0,
2555 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002556 break;
2557 }
2558
Douglas Gregor982365e2009-04-13 21:20:57 +00002559 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002560 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2561 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002562 break;
2563 }
2564
Douglas Gregorce066712009-04-26 22:20:50 +00002565 case pch::DECL_VAR:
Douglas Gregorddf4d092009-04-16 22:29:51 +00002566 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2567 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002568 break;
Douglas Gregorce066712009-04-26 22:20:50 +00002569
2570 case pch::DECL_IMPLICIT_PARAM:
2571 D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType());
2572 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002573
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002574 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002575 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2576 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002577 break;
2578 }
2579
2580 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002581 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002582 QualType(), QualType(), VarDecl::None,
2583 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002584 break;
2585 }
2586
Douglas Gregor2a491792009-04-13 22:49:25 +00002587 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002588 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002589 break;
2590 }
2591
2592 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002593 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002594 break;
2595 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002596 }
2597
Douglas Gregorc713da92009-04-21 22:25:48 +00002598 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002599 if (D) {
2600 LoadedDecl(Index, D);
2601 Reader.Visit(D);
2602 }
2603
Douglas Gregorc34897d2009-04-09 22:27:44 +00002604 // If this declaration is also a declaration context, get the
2605 // offsets for its tables of lexical and visible declarations.
2606 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2607 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2608 if (Offsets.first || Offsets.second) {
2609 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2610 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2611 DeclContextOffsets[DC] = Offsets;
2612 }
2613 }
2614 assert(Idx == Record.size());
2615
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002616 // If we have deserialized a declaration that has a definition the
2617 // AST consumer might need to know about, notify the consumer
2618 // about that definition now or queue it for later.
2619 if (isConsumerInterestedIn(D)) {
2620 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002621 DeclGroupRef DG(D);
2622 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002623 } else {
2624 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002625 }
2626 }
2627
Douglas Gregorc34897d2009-04-09 22:27:44 +00002628 return D;
2629}
2630
Douglas Gregorac8f2802009-04-10 17:25:41 +00002631QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002632 unsigned Quals = ID & 0x07;
2633 unsigned Index = ID >> 3;
2634
2635 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2636 QualType T;
2637 switch ((pch::PredefinedTypeIDs)Index) {
2638 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2639 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2640 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2641
2642 case pch::PREDEF_TYPE_CHAR_U_ID:
2643 case pch::PREDEF_TYPE_CHAR_S_ID:
2644 // FIXME: Check that the signedness of CharTy is correct!
2645 T = Context.CharTy;
2646 break;
2647
2648 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2649 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2650 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2651 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2652 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2653 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2654 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2655 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2656 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2657 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2658 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2659 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2660 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2661 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2662 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2663 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2664 }
2665
2666 assert(!T.isNull() && "Unknown predefined type");
2667 return T.getQualifiedType(Quals);
2668 }
2669
2670 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregore43f0972009-04-26 03:49:13 +00002671 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Douglas Gregor24a224c2009-04-25 18:35:21 +00002672 if (!TypesLoaded[Index])
2673 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002674
Douglas Gregor24a224c2009-04-25 18:35:21 +00002675 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002676}
2677
Douglas Gregorac8f2802009-04-10 17:25:41 +00002678Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002679 if (ID == 0)
2680 return 0;
2681
Douglas Gregor24a224c2009-04-25 18:35:21 +00002682 if (ID > DeclsLoaded.size()) {
2683 Error("Declaration ID out-of-range for PCH file");
2684 return 0;
2685 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002686
Douglas Gregor24a224c2009-04-25 18:35:21 +00002687 unsigned Index = ID - 1;
2688 if (!DeclsLoaded[Index])
2689 ReadDeclRecord(DeclOffsets[Index], Index);
2690
2691 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002692}
2693
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002694Stmt *PCHReader::GetStmt(uint64_t Offset) {
2695 // Keep track of where we are in the stream, then jump back there
2696 // after reading this declaration.
2697 SavedStreamPosition SavedPosition(Stream);
2698
2699 Stream.JumpToBit(Offset);
2700 return ReadStmt();
2701}
2702
Douglas Gregorc34897d2009-04-09 22:27:44 +00002703bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002704 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002705 assert(DC->hasExternalLexicalStorage() &&
2706 "DeclContext has no lexical decls in storage");
2707 uint64_t Offset = DeclContextOffsets[DC].first;
2708 assert(Offset && "DeclContext has no lexical decls in storage");
2709
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002710 // Keep track of where we are in the stream, then jump back there
2711 // after reading this context.
2712 SavedStreamPosition SavedPosition(Stream);
2713
Douglas Gregorc34897d2009-04-09 22:27:44 +00002714 // Load the record containing all of the declarations lexically in
2715 // this context.
2716 Stream.JumpToBit(Offset);
2717 RecordData Record;
2718 unsigned Code = Stream.ReadCode();
2719 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002720 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002721 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2722
2723 // Load all of the declaration IDs
2724 Decls.clear();
2725 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002726 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002727 return false;
2728}
2729
2730bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2731 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2732 assert(DC->hasExternalVisibleStorage() &&
2733 "DeclContext has no visible decls in storage");
2734 uint64_t Offset = DeclContextOffsets[DC].second;
2735 assert(Offset && "DeclContext has no visible decls in storage");
2736
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002737 // Keep track of where we are in the stream, then jump back there
2738 // after reading this context.
2739 SavedStreamPosition SavedPosition(Stream);
2740
Douglas Gregorc34897d2009-04-09 22:27:44 +00002741 // Load the record containing all of the declarations visible in
2742 // this context.
2743 Stream.JumpToBit(Offset);
2744 RecordData Record;
2745 unsigned Code = Stream.ReadCode();
2746 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002747 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002748 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2749 if (Record.size() == 0)
2750 return false;
2751
2752 Decls.clear();
2753
2754 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002755 while (Idx < Record.size()) {
2756 Decls.push_back(VisibleDeclaration());
2757 Decls.back().Name = ReadDeclarationName(Record, Idx);
2758
Douglas Gregorc34897d2009-04-09 22:27:44 +00002759 unsigned Size = Record[Idx++];
2760 llvm::SmallVector<unsigned, 4> & LoadedDecls
2761 = Decls.back().Declarations;
2762 LoadedDecls.reserve(Size);
2763 for (unsigned I = 0; I < Size; ++I)
2764 LoadedDecls.push_back(Record[Idx++]);
2765 }
2766
Douglas Gregoraf136d92009-04-22 22:34:57 +00002767 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002768 return false;
2769}
2770
Douglas Gregor631f6c62009-04-14 00:24:19 +00002771void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002772 this->Consumer = Consumer;
2773
Douglas Gregor631f6c62009-04-14 00:24:19 +00002774 if (!Consumer)
2775 return;
2776
2777 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2778 Decl *D = GetDecl(ExternalDefinitions[I]);
2779 DeclGroupRef DG(D);
2780 Consumer->HandleTopLevelDecl(DG);
2781 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002782
2783 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2784 DeclGroupRef DG(InterestingDecls[I]);
2785 Consumer->HandleTopLevelDecl(DG);
2786 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002787}
2788
Douglas Gregorc34897d2009-04-09 22:27:44 +00002789void PCHReader::PrintStats() {
2790 std::fprintf(stderr, "*** PCH Statistics:\n");
2791
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002792 unsigned NumTypesLoaded
2793 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2794 (Type *)0);
2795 unsigned NumDeclsLoaded
2796 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2797 (Decl *)0);
2798 unsigned NumIdentifiersLoaded
2799 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2800 IdentifiersLoaded.end(),
2801 (IdentifierInfo *)0);
2802 unsigned NumSelectorsLoaded
2803 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2804 SelectorsLoaded.end(),
2805 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00002806
Douglas Gregor24a224c2009-04-25 18:35:21 +00002807 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002808 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002809 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2810 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2811 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002812 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002813 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2814 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002815 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002816 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002817 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2818 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00002819 if (TotalNumSelectors)
2820 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2821 NumSelectorsLoaded, TotalNumSelectors,
2822 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2823 if (TotalNumStatements)
2824 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2825 NumStatementsRead, TotalNumStatements,
2826 ((float)NumStatementsRead/TotalNumStatements * 100));
2827 if (TotalNumMacros)
2828 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2829 NumMacrosRead, TotalNumMacros,
2830 ((float)NumMacrosRead/TotalNumMacros * 100));
2831 if (TotalLexicalDeclContexts)
2832 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2833 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2834 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2835 * 100));
2836 if (TotalVisibleDeclContexts)
2837 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2838 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2839 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2840 * 100));
2841 if (TotalSelectorsInMethodPool) {
2842 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2843 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2844 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2845 * 100));
2846 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2847 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002848 std::fprintf(stderr, "\n");
2849}
2850
Douglas Gregorc713da92009-04-21 22:25:48 +00002851void PCHReader::InitializeSema(Sema &S) {
2852 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002853 S.ExternalSource = this;
2854
Douglas Gregor2554cf22009-04-22 21:15:06 +00002855 // Makes sure any declarations that were deserialized "too early"
2856 // still get added to the identifier's declaration chains.
2857 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2858 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2859 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002860 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002861 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002862
2863 // If there were any tentative definitions, deserialize them and add
2864 // them to Sema's table of tentative definitions.
2865 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2866 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2867 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2868 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002869
2870 // If there were any locally-scoped external declarations,
2871 // deserialize them and add them to Sema's table of locally-scoped
2872 // external declarations.
2873 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2874 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2875 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2876 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002877}
2878
2879IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2880 // Try to find this name within our on-disk hash table
2881 PCHIdentifierLookupTable *IdTable
2882 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2883 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2884 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2885 if (Pos == IdTable->end())
2886 return 0;
2887
2888 // Dereferencing the iterator has the effect of building the
2889 // IdentifierInfo node and populating it with the various
2890 // declarations it needs.
2891 return *Pos;
2892}
2893
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002894std::pair<ObjCMethodList, ObjCMethodList>
2895PCHReader::ReadMethodPool(Selector Sel) {
2896 if (!MethodPoolLookupTable)
2897 return std::pair<ObjCMethodList, ObjCMethodList>();
2898
2899 // Try to find this selector within our on-disk hash table.
2900 PCHMethodPoolLookupTable *PoolTable
2901 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2902 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00002903 if (Pos == PoolTable->end()) {
2904 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002905 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00002906 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002907
Douglas Gregor2d711832009-04-25 17:48:32 +00002908 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002909 return *Pos;
2910}
2911
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002912void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00002913 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002914 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2915 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00002916}
2917
Chris Lattner29241862009-04-11 21:15:38 +00002918IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002919 if (ID == 0)
2920 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002921
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002922 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002923 Error("No identifier table in PCH file");
2924 return 0;
2925 }
Chris Lattner29241862009-04-11 21:15:38 +00002926
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002927 if (!IdentifiersLoaded[ID - 1]) {
2928 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002929 const char *Str = IdentifierTableData + Offset;
Douglas Gregor85c4a872009-04-25 21:04:17 +00002930
2931 // If there is an identifier lookup table, but the offset of this
2932 // string is after the identifier table itself, then we know that
2933 // this string is not in the on-disk hash table. Therefore,
2934 // disable lookup into the hash table when looking for this
2935 // identifier.
2936 PCHIdentifierLookupTable *IdTable
2937 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002938 if (!IdTable ||
2939 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
2940 // Turn off lookup into the on-disk hash table. We know that
2941 // this identifier is not there.
2942 if (IdTable)
2943 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002944
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002945 // All of the strings in the PCH file are preceded by a 16-bit
2946 // length. Extract that 16-bit length to avoid having to execute
2947 // strlen().
2948 const char *StrLenPtr = Str - 2;
2949 unsigned StrLen = (((unsigned) StrLenPtr[0])
2950 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
2951 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002952
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002953 // Turn on lookup into the on-disk hash table, if we have an
2954 // on-disk hash table.
2955 if (IdTable)
2956 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2957 } else {
2958 // The identifier is a key in our on-disk hash table. Since we
2959 // know where the hash table entry starts, just read in this
2960 // (key, value) pair.
2961 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
2962 const unsigned char *Pos = (const unsigned char *)Str - 4;
2963 std::pair<unsigned, unsigned> KeyDataLengths
2964 = Trait.ReadKeyDataLength(Pos);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002965
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002966 PCHIdentifierLookupTrait::internal_key_type InternalKey
2967 = Trait.ReadKey(Pos, KeyDataLengths.first);
2968 Pos = (const unsigned char *)Str + KeyDataLengths.first;
2969 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
2970 KeyDataLengths.second);
2971 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002972 }
Chris Lattner29241862009-04-11 21:15:38 +00002973
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002974 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002975}
2976
Steve Naroff9e84d782009-04-23 10:39:46 +00002977Selector PCHReader::DecodeSelector(unsigned ID) {
2978 if (ID == 0)
2979 return Selector();
2980
Douglas Gregor2d711832009-04-25 17:48:32 +00002981 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002982 Error("No selector table in PCH file");
2983 return Selector();
2984 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002985
2986 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002987 Error("Selector ID out of range");
2988 return Selector();
2989 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002990
2991 unsigned Index = ID - 1;
2992 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2993 // Load this selector from the selector table.
2994 // FIXME: endianness portability issues with SelectorOffsets table
2995 PCHMethodPoolLookupTrait Trait(*this);
2996 SelectorsLoaded[Index]
2997 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2998 }
2999
3000 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00003001}
3002
Douglas Gregorc34897d2009-04-09 22:27:44 +00003003DeclarationName
3004PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
3005 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
3006 switch (Kind) {
3007 case DeclarationName::Identifier:
3008 return DeclarationName(GetIdentifierInfo(Record, Idx));
3009
3010 case DeclarationName::ObjCZeroArgSelector:
3011 case DeclarationName::ObjCOneArgSelector:
3012 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00003013 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00003014
3015 case DeclarationName::CXXConstructorName:
3016 return Context.DeclarationNames.getCXXConstructorName(
3017 GetType(Record[Idx++]));
3018
3019 case DeclarationName::CXXDestructorName:
3020 return Context.DeclarationNames.getCXXDestructorName(
3021 GetType(Record[Idx++]));
3022
3023 case DeclarationName::CXXConversionFunctionName:
3024 return Context.DeclarationNames.getCXXConversionFunctionName(
3025 GetType(Record[Idx++]));
3026
3027 case DeclarationName::CXXOperatorName:
3028 return Context.DeclarationNames.getCXXOperatorName(
3029 (OverloadedOperatorKind)Record[Idx++]);
3030
3031 case DeclarationName::CXXUsingDirective:
3032 return DeclarationName::getUsingDirectiveName();
3033 }
3034
3035 // Required to silence GCC warning
3036 return DeclarationName();
3037}
Douglas Gregor179cfb12009-04-10 20:39:37 +00003038
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00003039/// \brief Read an integral value
3040llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
3041 unsigned BitWidth = Record[Idx++];
3042 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
3043 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
3044 Idx += NumWords;
3045 return Result;
3046}
3047
3048/// \brief Read a signed integral value
3049llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3050 bool isUnsigned = Record[Idx++];
3051 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3052}
3053
Douglas Gregore2f37202009-04-14 21:55:33 +00003054/// \brief Read a floating-point value
3055llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00003056 return llvm::APFloat(ReadAPInt(Record, Idx));
3057}
3058
Douglas Gregor1c507882009-04-15 21:30:51 +00003059// \brief Read a string
3060std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3061 unsigned Len = Record[Idx++];
3062 std::string Result(&Record[Idx], &Record[Idx] + Len);
3063 Idx += Len;
3064 return Result;
3065}
3066
3067/// \brief Reads attributes from the current stream position.
3068Attr *PCHReader::ReadAttributes() {
3069 unsigned Code = Stream.ReadCode();
3070 assert(Code == llvm::bitc::UNABBREV_RECORD &&
3071 "Expected unabbreviated record"); (void)Code;
3072
3073 RecordData Record;
3074 unsigned Idx = 0;
3075 unsigned RecCode = Stream.ReadRecord(Code, Record);
3076 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
3077 (void)RecCode;
3078
3079#define SIMPLE_ATTR(Name) \
3080 case Attr::Name: \
3081 New = ::new (Context) Name##Attr(); \
3082 break
3083
3084#define STRING_ATTR(Name) \
3085 case Attr::Name: \
3086 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
3087 break
3088
3089#define UNSIGNED_ATTR(Name) \
3090 case Attr::Name: \
3091 New = ::new (Context) Name##Attr(Record[Idx++]); \
3092 break
3093
3094 Attr *Attrs = 0;
3095 while (Idx < Record.size()) {
3096 Attr *New = 0;
3097 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
3098 bool IsInherited = Record[Idx++];
3099
3100 switch (Kind) {
3101 STRING_ATTR(Alias);
3102 UNSIGNED_ATTR(Aligned);
3103 SIMPLE_ATTR(AlwaysInline);
3104 SIMPLE_ATTR(AnalyzerNoReturn);
3105 STRING_ATTR(Annotate);
3106 STRING_ATTR(AsmLabel);
3107
3108 case Attr::Blocks:
3109 New = ::new (Context) BlocksAttr(
3110 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3111 break;
3112
3113 case Attr::Cleanup:
3114 New = ::new (Context) CleanupAttr(
3115 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3116 break;
3117
3118 SIMPLE_ATTR(Const);
3119 UNSIGNED_ATTR(Constructor);
3120 SIMPLE_ATTR(DLLExport);
3121 SIMPLE_ATTR(DLLImport);
3122 SIMPLE_ATTR(Deprecated);
3123 UNSIGNED_ATTR(Destructor);
3124 SIMPLE_ATTR(FastCall);
3125
3126 case Attr::Format: {
3127 std::string Type = ReadString(Record, Idx);
3128 unsigned FormatIdx = Record[Idx++];
3129 unsigned FirstArg = Record[Idx++];
3130 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3131 break;
3132 }
3133
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003134 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003135
3136 case Attr::IBOutletKind:
3137 New = ::new (Context) IBOutletAttr();
3138 break;
3139
3140 SIMPLE_ATTR(NoReturn);
3141 SIMPLE_ATTR(NoThrow);
3142 SIMPLE_ATTR(Nodebug);
3143 SIMPLE_ATTR(Noinline);
3144
3145 case Attr::NonNull: {
3146 unsigned Size = Record[Idx++];
3147 llvm::SmallVector<unsigned, 16> ArgNums;
3148 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3149 Idx += Size;
3150 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3151 break;
3152 }
3153
3154 SIMPLE_ATTR(ObjCException);
3155 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003156 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003157 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003158 SIMPLE_ATTR(Overloadable);
3159 UNSIGNED_ATTR(Packed);
3160 SIMPLE_ATTR(Pure);
3161 UNSIGNED_ATTR(Regparm);
3162 STRING_ATTR(Section);
3163 SIMPLE_ATTR(StdCall);
3164 SIMPLE_ATTR(TransparentUnion);
3165 SIMPLE_ATTR(Unavailable);
3166 SIMPLE_ATTR(Unused);
3167 SIMPLE_ATTR(Used);
3168
3169 case Attr::Visibility:
3170 New = ::new (Context) VisibilityAttr(
3171 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3172 break;
3173
3174 SIMPLE_ATTR(WarnUnusedResult);
3175 SIMPLE_ATTR(Weak);
3176 SIMPLE_ATTR(WeakImport);
3177 }
3178
3179 assert(New && "Unable to decode attribute?");
3180 New->setInherited(IsInherited);
3181 New->setNext(Attrs);
3182 Attrs = New;
3183 }
3184#undef UNSIGNED_ATTR
3185#undef STRING_ATTR
3186#undef SIMPLE_ATTR
3187
3188 // The list of attributes was built backwards. Reverse the list
3189 // before returning it.
3190 Attr *PrevAttr = 0, *NextAttr = 0;
3191 while (Attrs) {
3192 NextAttr = Attrs->getNext();
3193 Attrs->setNext(PrevAttr);
3194 PrevAttr = Attrs;
3195 Attrs = NextAttr;
3196 }
3197
3198 return PrevAttr;
3199}
3200
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003201Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003202 // Within the bitstream, expressions are stored in Reverse Polish
3203 // Notation, with each of the subexpressions preceding the
3204 // expression they are stored in. To evaluate expressions, we
3205 // continue reading expressions and placing them on the stack, with
3206 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003207 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003208 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003209 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003210 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003211 llvm::SmallVector<Stmt *, 16> StmtStack;
3212 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003213 Stmt::EmptyShell Empty;
3214
Douglas Gregora151ba42009-04-14 23:32:43 +00003215 while (true) {
3216 unsigned Code = Stream.ReadCode();
3217 if (Code == llvm::bitc::END_BLOCK) {
3218 if (Stream.ReadBlockEnd()) {
3219 Error("Error at end of Source Manager block");
3220 return 0;
3221 }
3222 break;
3223 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003224
Douglas Gregora151ba42009-04-14 23:32:43 +00003225 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3226 // No known subblocks, always skip them.
3227 Stream.ReadSubBlockID();
3228 if (Stream.SkipBlock()) {
3229 Error("Malformed block record");
3230 return 0;
3231 }
3232 continue;
3233 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003234
Douglas Gregora151ba42009-04-14 23:32:43 +00003235 if (Code == llvm::bitc::DEFINE_ABBREV) {
3236 Stream.ReadAbbrevRecord();
3237 continue;
3238 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003239
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003240 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003241 Idx = 0;
3242 Record.clear();
3243 bool Finished = false;
3244 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003245 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003246 Finished = true;
3247 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003248
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003249 case pch::STMT_NULL_PTR:
3250 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003251 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003252
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003253 case pch::STMT_NULL:
3254 S = new (Context) NullStmt(Empty);
3255 break;
3256
3257 case pch::STMT_COMPOUND:
3258 S = new (Context) CompoundStmt(Empty);
3259 break;
3260
3261 case pch::STMT_CASE:
3262 S = new (Context) CaseStmt(Empty);
3263 break;
3264
3265 case pch::STMT_DEFAULT:
3266 S = new (Context) DefaultStmt(Empty);
3267 break;
3268
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003269 case pch::STMT_LABEL:
3270 S = new (Context) LabelStmt(Empty);
3271 break;
3272
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003273 case pch::STMT_IF:
3274 S = new (Context) IfStmt(Empty);
3275 break;
3276
3277 case pch::STMT_SWITCH:
3278 S = new (Context) SwitchStmt(Empty);
3279 break;
3280
Douglas Gregora6b503f2009-04-17 00:16:09 +00003281 case pch::STMT_WHILE:
3282 S = new (Context) WhileStmt(Empty);
3283 break;
3284
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003285 case pch::STMT_DO:
3286 S = new (Context) DoStmt(Empty);
3287 break;
3288
3289 case pch::STMT_FOR:
3290 S = new (Context) ForStmt(Empty);
3291 break;
3292
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003293 case pch::STMT_GOTO:
3294 S = new (Context) GotoStmt(Empty);
3295 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003296
3297 case pch::STMT_INDIRECT_GOTO:
3298 S = new (Context) IndirectGotoStmt(Empty);
3299 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003300
Douglas Gregora6b503f2009-04-17 00:16:09 +00003301 case pch::STMT_CONTINUE:
3302 S = new (Context) ContinueStmt(Empty);
3303 break;
3304
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003305 case pch::STMT_BREAK:
3306 S = new (Context) BreakStmt(Empty);
3307 break;
3308
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003309 case pch::STMT_RETURN:
3310 S = new (Context) ReturnStmt(Empty);
3311 break;
3312
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003313 case pch::STMT_DECL:
3314 S = new (Context) DeclStmt(Empty);
3315 break;
3316
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003317 case pch::STMT_ASM:
3318 S = new (Context) AsmStmt(Empty);
3319 break;
3320
Douglas Gregora151ba42009-04-14 23:32:43 +00003321 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003322 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003323 break;
3324
3325 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003326 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003327 break;
3328
3329 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003330 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003331 break;
3332
3333 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003334 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003335 break;
3336
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003337 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003338 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003339 break;
3340
Douglas Gregor596e0932009-04-15 16:35:07 +00003341 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003342 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003343 Record[PCHStmtReader::NumExprFields + 1]);
3344 break;
3345
Douglas Gregora151ba42009-04-14 23:32:43 +00003346 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003347 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003348 break;
3349
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003350 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003351 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003352 break;
3353
Douglas Gregor12d74052009-04-15 15:58:59 +00003354 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003355 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003356 break;
3357
3358 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003359 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003360 break;
3361
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003362 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003363 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003364 break;
3365
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003366 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003367 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003368 break;
3369
3370 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003371 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003372 break;
3373
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003374 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003375 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003376 break;
3377
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003378 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003379 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003380 break;
3381
3382 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003383 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003384 break;
3385
Douglas Gregora151ba42009-04-14 23:32:43 +00003386 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003387 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003388 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003389
3390 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003391 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003392 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003393
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003394 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003395 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003396 break;
3397
Douglas Gregorec0b8292009-04-15 23:02:49 +00003398 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003399 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003400 break;
3401
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003402 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003403 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003404 break;
3405
3406 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003407 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003408 Record[PCHStmtReader::NumExprFields] - 1);
3409
3410 break;
3411
3412 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003413 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003414 break;
3415
Douglas Gregorec0b8292009-04-15 23:02:49 +00003416 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003417 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003418 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003419
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003420 case pch::EXPR_ADDR_LABEL:
3421 S = new (Context) AddrLabelExpr(Empty);
3422 break;
3423
Douglas Gregoreca12f62009-04-17 19:05:30 +00003424 case pch::EXPR_STMT:
3425 S = new (Context) StmtExpr(Empty);
3426 break;
3427
Douglas Gregor209d4622009-04-15 23:33:31 +00003428 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003429 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003430 break;
3431
3432 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003433 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003434 break;
3435
3436 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003437 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003438 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003439
3440 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003441 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003442 break;
3443
Douglas Gregore246b742009-04-17 19:21:43 +00003444 case pch::EXPR_BLOCK:
3445 S = new (Context) BlockExpr(Empty);
3446 break;
3447
Douglas Gregor725e94b2009-04-16 00:01:45 +00003448 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003449 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003450 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003451
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003452 case pch::EXPR_OBJC_STRING_LITERAL:
3453 S = new (Context) ObjCStringLiteral(Empty);
3454 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003455 case pch::EXPR_OBJC_ENCODE:
3456 S = new (Context) ObjCEncodeExpr(Empty);
3457 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003458 case pch::EXPR_OBJC_SELECTOR_EXPR:
3459 S = new (Context) ObjCSelectorExpr(Empty);
3460 break;
3461 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3462 S = new (Context) ObjCProtocolExpr(Empty);
3463 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003464 case pch::EXPR_OBJC_IVAR_REF_EXPR:
3465 S = new (Context) ObjCIvarRefExpr(Empty);
3466 break;
3467 case pch::EXPR_OBJC_PROPERTY_REF_EXPR:
3468 S = new (Context) ObjCPropertyRefExpr(Empty);
3469 break;
3470 case pch::EXPR_OBJC_KVC_REF_EXPR:
3471 S = new (Context) ObjCKVCRefExpr(Empty);
3472 break;
Steve Narofffb3e4022009-04-25 14:04:28 +00003473 case pch::EXPR_OBJC_MESSAGE_EXPR:
3474 S = new (Context) ObjCMessageExpr(Empty);
3475 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003476 case pch::EXPR_OBJC_SUPER_EXPR:
3477 S = new (Context) ObjCSuperExpr(Empty);
3478 break;
Steve Naroff79762bd2009-04-26 18:52:16 +00003479 case pch::STMT_OBJC_FOR_COLLECTION:
3480 S = new (Context) ObjCForCollectionStmt(Empty);
3481 break;
3482 case pch::STMT_OBJC_CATCH:
3483 S = new (Context) ObjCAtCatchStmt(Empty);
3484 break;
3485 case pch::STMT_OBJC_FINALLY:
3486 S = new (Context) ObjCAtFinallyStmt(Empty);
3487 break;
3488 case pch::STMT_OBJC_AT_TRY:
3489 S = new (Context) ObjCAtTryStmt(Empty);
3490 break;
3491 case pch::STMT_OBJC_AT_SYNCHRONIZED:
3492 S = new (Context) ObjCAtSynchronizedStmt(Empty);
3493 break;
3494 case pch::STMT_OBJC_AT_THROW:
3495 S = new (Context) ObjCAtThrowStmt(Empty);
3496 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003497 }
3498
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003499 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003500 if (Finished)
3501 break;
3502
Douglas Gregor456e0952009-04-17 22:13:46 +00003503 ++NumStatementsRead;
3504
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003505 if (S) {
3506 unsigned NumSubStmts = Reader.Visit(S);
3507 while (NumSubStmts > 0) {
3508 StmtStack.pop_back();
3509 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003510 }
3511 }
3512
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003513 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003514 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003515 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003516 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003517 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003518 return StmtStack.back();
3519}
3520
3521Expr *PCHReader::ReadExpr() {
3522 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003523}
3524
Douglas Gregor179cfb12009-04-10 20:39:37 +00003525DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003526 return Diag(SourceLocation(), DiagID);
3527}
3528
3529DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3530 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003531 Context.getSourceManager()),
3532 DiagID);
3533}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003534
Douglas Gregorc713da92009-04-21 22:25:48 +00003535/// \brief Retrieve the identifier table associated with the
3536/// preprocessor.
3537IdentifierTable &PCHReader::getIdentifierTable() {
3538 return PP.getIdentifierTable();
3539}
3540
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003541/// \brief Record that the given ID maps to the given switch-case
3542/// statement.
3543void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3544 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3545 SwitchCaseStmts[ID] = SC;
3546}
3547
3548/// \brief Retrieve the switch-case statement with the given ID.
3549SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3550 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3551 return SwitchCaseStmts[ID];
3552}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003553
3554/// \brief Record that the given label statement has been
3555/// deserialized and has the given ID.
3556void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3557 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3558 "Deserialized label twice");
3559 LabelStmts[ID] = S;
3560
3561 // If we've already seen any goto statements that point to this
3562 // label, resolve them now.
3563 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3564 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3565 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3566 Goto->second->setLabel(S);
3567 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003568
3569 // If we've already seen any address-label statements that point to
3570 // this label, resolve them now.
3571 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3572 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3573 = UnresolvedAddrLabelExprs.equal_range(ID);
3574 for (AddrLabelIter AddrLabel = AddrLabels.first;
3575 AddrLabel != AddrLabels.second; ++AddrLabel)
3576 AddrLabel->second->setLabel(S);
3577 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003578}
3579
3580/// \brief Set the label of the given statement to the label
3581/// identified by ID.
3582///
3583/// Depending on the order in which the label and other statements
3584/// referencing that label occur, this operation may complete
3585/// immediately (updating the statement) or it may queue the
3586/// statement to be back-patched later.
3587void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3588 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3589 if (Label != LabelStmts.end()) {
3590 // We've already seen this label, so set the label of the goto and
3591 // we're done.
3592 S->setLabel(Label->second);
3593 } else {
3594 // We haven't seen this label yet, so add this goto to the set of
3595 // unresolved goto statements.
3596 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3597 }
3598}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003599
3600/// \brief Set the label of the given expression to the label
3601/// identified by ID.
3602///
3603/// Depending on the order in which the label and other statements
3604/// referencing that label occur, this operation may complete
3605/// immediately (updating the statement) or it may queue the
3606/// statement to be back-patched later.
3607void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3608 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3609 if (Label != LabelStmts.end()) {
3610 // We've already seen this label, so set the label of the
3611 // label-address expression and we're done.
3612 S->setLabel(Label->second);
3613 } else {
3614 // We haven't seen this label yet, so add this label-address
3615 // expression to the set of unresolved label-address expressions.
3616 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3617 }
3618}