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