blob: d50ab8310b0711adedaece3b22cb21499d07ed0d [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 Naroff9de1c952009-04-24 16:08:42 +0000301 unsigned nextCat = Record[Idx++];
302 CD->setNextClassCategory(nextCat ?
303 cast<ObjCCategoryDecl>(Reader.GetDecl(nextCat)) : 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +0000304 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
305}
306
307void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
308 VisitNamedDecl(CAD);
309 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
310}
311
312void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
313 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000314 D->setType(Reader.GetType(Record[Idx++]));
315 // FIXME: stable encoding
316 D->setPropertyAttributes(
317 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
318 // FIXME: stable encoding
319 D->setPropertyImplementation(
320 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
321 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
323 D->setGetterMethodDecl(
324 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
325 D->setSetterMethodDecl(
326 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setPropertyIvarDecl(
328 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000329}
330
331void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000332 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000333 D->setClassInterface(
334 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
335 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000336}
337
338void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
339 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000340 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000341}
342
343void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
344 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000345 D->setSuperClass(
346 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000347}
348
349
350void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
351 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000352 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
353 D->setPropertyDecl(
354 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
355 D->setPropertyIvarDecl(
356 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000357}
358
Douglas Gregor982365e2009-04-13 21:20:57 +0000359void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
360 VisitValueDecl(FD);
361 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000364}
365
Douglas Gregorc34897d2009-04-09 22:27:44 +0000366void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
367 VisitValueDecl(VD);
368 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
369 VD->setThreadSpecified(Record[Idx++]);
370 VD->setCXXDirectInitializer(Record[Idx++]);
371 VD->setDeclaredInCondition(Record[Idx++]);
372 VD->setPreviousDeclaration(
373 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
374 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375 if (Record[Idx++])
376 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000377}
378
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000379void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
380 VisitVarDecl(PD);
381 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000382 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383}
384
385void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
386 VisitParmVarDecl(PD);
387 PD->setOriginalType(Reader.GetType(Record[Idx++]));
388}
389
Douglas Gregor2a491792009-04-13 22:49:25 +0000390void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
391 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000392 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000393}
394
395void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
396 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000397 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000398 unsigned NumParams = Record[Idx++];
399 llvm::SmallVector<ParmVarDecl *, 16> Params;
400 Params.reserve(NumParams);
401 for (unsigned I = 0; I != NumParams; ++I)
402 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
403 BD->setParams(Reader.getContext(), &Params[0], NumParams);
404}
405
Douglas Gregorc34897d2009-04-09 22:27:44 +0000406std::pair<uint64_t, uint64_t>
407PCHDeclReader::VisitDeclContext(DeclContext *DC) {
408 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000409 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000410 return std::make_pair(LexicalOffset, VisibleOffset);
411}
412
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000413//===----------------------------------------------------------------------===//
414// Statement/expression deserialization
415//===----------------------------------------------------------------------===//
416namespace {
417 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000418 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000419 PCHReader &Reader;
420 const PCHReader::RecordData &Record;
421 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000422 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000423
424 public:
425 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000426 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
427 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000428
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000429 /// \brief The number of record fields required for the Stmt class
430 /// itself.
431 static const unsigned NumStmtFields = 0;
432
Douglas Gregor596e0932009-04-15 16:35:07 +0000433 /// \brief The number of record fields required for the Expr class
434 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000435 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000436
Douglas Gregora151ba42009-04-14 23:32:43 +0000437 // Each of the Visit* functions reads in part of the expression
438 // from the given record and the current expression stack, then
439 // return the total number of operands that it read from the
440 // expression stack.
441
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000442 unsigned VisitStmt(Stmt *S);
443 unsigned VisitNullStmt(NullStmt *S);
444 unsigned VisitCompoundStmt(CompoundStmt *S);
445 unsigned VisitSwitchCase(SwitchCase *S);
446 unsigned VisitCaseStmt(CaseStmt *S);
447 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000448 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000449 unsigned VisitIfStmt(IfStmt *S);
450 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000451 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000452 unsigned VisitDoStmt(DoStmt *S);
453 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000455 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000456 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000457 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000458 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000459 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000460 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000461 unsigned VisitExpr(Expr *E);
462 unsigned VisitPredefinedExpr(PredefinedExpr *E);
463 unsigned VisitDeclRefExpr(DeclRefExpr *E);
464 unsigned VisitIntegerLiteral(IntegerLiteral *E);
465 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000466 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000467 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000468 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000469 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000470 unsigned VisitUnaryOperator(UnaryOperator *E);
471 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000472 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000473 unsigned VisitCallExpr(CallExpr *E);
474 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000475 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000476 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000477 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000480 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
481 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000482 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000483 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000484 unsigned VisitInitListExpr(InitListExpr *E);
485 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000488 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000489 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000490 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
491 unsigned VisitChooseExpr(ChooseExpr *E);
492 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000493 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000494 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000495 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000496 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000497 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000498 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
499 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000500 };
501}
502
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000503unsigned PCHStmtReader::VisitStmt(Stmt *S) {
504 assert(Idx == NumStmtFields && "Incorrect statement field count");
505 return 0;
506}
507
508unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
509 VisitStmt(S);
510 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
511 return 0;
512}
513
514unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
515 VisitStmt(S);
516 unsigned NumStmts = Record[Idx++];
517 S->setStmts(Reader.getContext(),
518 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
519 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
520 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
521 return NumStmts;
522}
523
524unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
525 VisitStmt(S);
526 Reader.RecordSwitchCaseID(S, Record[Idx++]);
527 return 0;
528}
529
530unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
531 VisitSwitchCase(S);
532 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
533 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
534 S->setSubStmt(StmtStack.back());
535 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 return 3;
537}
538
539unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
540 VisitSwitchCase(S);
541 S->setSubStmt(StmtStack.back());
542 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
543 return 1;
544}
545
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000546unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
547 VisitStmt(S);
548 S->setID(Reader.GetIdentifierInfo(Record, Idx));
549 S->setSubStmt(StmtStack.back());
550 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
551 Reader.RecordLabelStmt(S, Record[Idx++]);
552 return 1;
553}
554
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000555unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
556 VisitStmt(S);
557 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
558 S->setThen(StmtStack[StmtStack.size() - 2]);
559 S->setElse(StmtStack[StmtStack.size() - 1]);
560 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
561 return 3;
562}
563
564unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
565 VisitStmt(S);
566 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
567 S->setBody(StmtStack.back());
568 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
569 SwitchCase *PrevSC = 0;
570 for (unsigned N = Record.size(); Idx != N; ++Idx) {
571 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
572 if (PrevSC)
573 PrevSC->setNextSwitchCase(SC);
574 else
575 S->setSwitchCaseList(SC);
576 PrevSC = SC;
577 }
578 return 2;
579}
580
Douglas Gregora6b503f2009-04-17 00:16:09 +0000581unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
582 VisitStmt(S);
583 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
584 S->setBody(StmtStack.back());
585 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
586 return 2;
587}
588
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000589unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
590 VisitStmt(S);
591 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
592 S->setBody(StmtStack.back());
593 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 return 2;
595}
596
597unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
598 VisitStmt(S);
599 S->setInit(StmtStack[StmtStack.size() - 4]);
600 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
601 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
602 S->setBody(StmtStack.back());
603 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 4;
605}
606
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000607unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
608 VisitStmt(S);
609 Reader.SetLabelOf(S, Record[Idx++]);
610 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
612 return 0;
613}
614
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000615unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
616 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000617 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000618 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
619 return 1;
620}
621
Douglas Gregora6b503f2009-04-17 00:16:09 +0000622unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
623 VisitStmt(S);
624 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
625 return 0;
626}
627
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000628unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
629 VisitStmt(S);
630 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
631 return 0;
632}
633
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000634unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
635 VisitStmt(S);
636 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
637 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
638 return 1;
639}
640
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000641unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
642 VisitStmt(S);
643 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
644 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
645
646 if (Idx + 1 == Record.size()) {
647 // Single declaration
648 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
649 } else {
650 llvm::SmallVector<Decl *, 16> Decls;
651 Decls.reserve(Record.size() - Idx);
652 for (unsigned N = Record.size(); Idx != N; ++Idx)
653 Decls.push_back(Reader.GetDecl(Record[Idx]));
654 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
655 &Decls[0], Decls.size())));
656 }
657 return 0;
658}
659
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000660unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
661 VisitStmt(S);
662 unsigned NumOutputs = Record[Idx++];
663 unsigned NumInputs = Record[Idx++];
664 unsigned NumClobbers = Record[Idx++];
665 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
666 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
667 S->setVolatile(Record[Idx++]);
668 S->setSimple(Record[Idx++]);
669
670 unsigned StackIdx
671 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
672 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
673
674 // Outputs and inputs
675 llvm::SmallVector<std::string, 16> Names;
676 llvm::SmallVector<StringLiteral*, 16> Constraints;
677 llvm::SmallVector<Stmt*, 16> Exprs;
678 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
679 Names.push_back(Reader.ReadString(Record, Idx));
680 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
681 Exprs.push_back(StmtStack[StackIdx++]);
682 }
683 S->setOutputsAndInputs(NumOutputs, NumInputs,
684 &Names[0], &Constraints[0], &Exprs[0]);
685
686 // Constraints
687 llvm::SmallVector<StringLiteral*, 16> Clobbers;
688 for (unsigned I = 0; I != NumClobbers; ++I)
689 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
690 S->setClobbers(&Clobbers[0], NumClobbers);
691
692 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
693 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
694}
695
Douglas Gregora151ba42009-04-14 23:32:43 +0000696unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000697 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000698 E->setType(Reader.GetType(Record[Idx++]));
699 E->setTypeDependent(Record[Idx++]);
700 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000701 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000702 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000703}
704
Douglas Gregora151ba42009-04-14 23:32:43 +0000705unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000706 VisitExpr(E);
707 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
708 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000709 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000710}
711
Douglas Gregora151ba42009-04-14 23:32:43 +0000712unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000713 VisitExpr(E);
714 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
715 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000716 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000717}
718
Douglas Gregora151ba42009-04-14 23:32:43 +0000719unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000720 VisitExpr(E);
721 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
722 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000723 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000724}
725
Douglas Gregora151ba42009-04-14 23:32:43 +0000726unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000727 VisitExpr(E);
728 E->setValue(Reader.ReadAPFloat(Record, Idx));
729 E->setExact(Record[Idx++]);
730 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000731 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000732}
733
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000734unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
735 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000736 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000737 return 1;
738}
739
Douglas Gregor596e0932009-04-15 16:35:07 +0000740unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
741 VisitExpr(E);
742 unsigned Len = Record[Idx++];
743 assert(Record[Idx] == E->getNumConcatenated() &&
744 "Wrong number of concatenated tokens!");
745 ++Idx;
746 E->setWide(Record[Idx++]);
747
748 // Read string data
749 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
750 E->setStrData(Reader.getContext(), &Str[0], Len);
751 Idx += Len;
752
753 // Read source locations
754 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
755 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
756
757 return 0;
758}
759
Douglas Gregora151ba42009-04-14 23:32:43 +0000760unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000761 VisitExpr(E);
762 E->setValue(Record[Idx++]);
763 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
764 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000765 return 0;
766}
767
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000768unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
769 VisitExpr(E);
770 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
771 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000772 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000773 return 1;
774}
775
Douglas Gregor12d74052009-04-15 15:58:59 +0000776unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
777 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000778 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000779 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
780 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 return 1;
782}
783
784unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
785 VisitExpr(E);
786 E->setSizeof(Record[Idx++]);
787 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000788 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000789 ++Idx;
790 } else {
791 E->setArgument(Reader.GetType(Record[Idx++]));
792 }
793 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 return E->isArgumentType()? 0 : 1;
796}
797
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000798unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
799 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
801 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000802 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
803 return 2;
804}
805
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000806unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
807 VisitExpr(E);
808 E->setNumArgs(Reader.getContext(), Record[Idx++]);
809 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000810 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000811 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000812 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000813 return E->getNumArgs() + 1;
814}
815
816unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
817 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000818 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000819 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
820 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
821 E->setArrow(Record[Idx++]);
822 return 1;
823}
824
Douglas Gregora151ba42009-04-14 23:32:43 +0000825unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
826 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000827 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000828 return 1;
829}
830
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000831unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
832 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000833 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
834 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000835 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
836 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
837 return 2;
838}
839
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000840unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
841 VisitBinaryOperator(E);
842 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
843 E->setComputationResultType(Reader.GetType(Record[Idx++]));
844 return 2;
845}
846
847unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
848 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000849 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
850 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
851 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000852 return 3;
853}
854
Douglas Gregora151ba42009-04-14 23:32:43 +0000855unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
856 VisitCastExpr(E);
857 E->setLvalueCast(Record[Idx++]);
858 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000859}
860
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000861unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
862 VisitCastExpr(E);
863 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
864 return 1;
865}
866
867unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
868 VisitExplicitCastExpr(E);
869 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
871 return 1;
872}
873
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000874unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
875 VisitExpr(E);
876 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000877 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000878 E->setFileScope(Record[Idx++]);
879 return 1;
880}
881
Douglas Gregorec0b8292009-04-15 23:02:49 +0000882unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
883 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000884 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000885 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
886 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
887 return 1;
888}
889
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000890unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
891 VisitExpr(E);
892 unsigned NumInits = Record[Idx++];
893 E->reserveInits(NumInits);
894 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000895 E->updateInit(I,
896 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
897 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000898 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
900 E->setInitializedFieldInUnion(
901 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
902 E->sawArrayRangeDesignator(Record[Idx++]);
903 return NumInits + 1;
904}
905
906unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
907 typedef DesignatedInitExpr::Designator Designator;
908
909 VisitExpr(E);
910 unsigned NumSubExprs = Record[Idx++];
911 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
912 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000913 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000914 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
915 E->setGNUSyntax(Record[Idx++]);
916
917 llvm::SmallVector<Designator, 4> Designators;
918 while (Idx < Record.size()) {
919 switch ((pch::DesignatorTypes)Record[Idx++]) {
920 case pch::DESIG_FIELD_DECL: {
921 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
922 SourceLocation DotLoc
923 = SourceLocation::getFromRawEncoding(Record[Idx++]);
924 SourceLocation FieldLoc
925 = SourceLocation::getFromRawEncoding(Record[Idx++]);
926 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
927 FieldLoc));
928 Designators.back().setField(Field);
929 break;
930 }
931
932 case pch::DESIG_FIELD_NAME: {
933 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
934 SourceLocation DotLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 SourceLocation FieldLoc
937 = SourceLocation::getFromRawEncoding(Record[Idx++]);
938 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
939 break;
940 }
941
942 case pch::DESIG_ARRAY: {
943 unsigned Index = Record[Idx++];
944 SourceLocation LBracketLoc
945 = SourceLocation::getFromRawEncoding(Record[Idx++]);
946 SourceLocation RBracketLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
949 break;
950 }
951
952 case pch::DESIG_ARRAY_RANGE: {
953 unsigned Index = Record[Idx++];
954 SourceLocation LBracketLoc
955 = SourceLocation::getFromRawEncoding(Record[Idx++]);
956 SourceLocation EllipsisLoc
957 = SourceLocation::getFromRawEncoding(Record[Idx++]);
958 SourceLocation RBracketLoc
959 = SourceLocation::getFromRawEncoding(Record[Idx++]);
960 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
961 RBracketLoc));
962 break;
963 }
964 }
965 }
966 E->setDesignators(&Designators[0], Designators.size());
967
968 return NumSubExprs;
969}
970
971unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
972 VisitExpr(E);
973 return 0;
974}
975
Douglas Gregorec0b8292009-04-15 23:02:49 +0000976unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
977 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000978 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000979 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
981 return 1;
982}
983
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000984unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
985 VisitExpr(E);
986 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
988 Reader.SetLabelOf(E, Record[Idx++]);
989 return 0;
990}
991
Douglas Gregoreca12f62009-04-17 19:05:30 +0000992unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
993 VisitExpr(E);
994 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
996 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
997 return 1;
998}
999
Douglas Gregor209d4622009-04-15 23:33:31 +00001000unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1001 VisitExpr(E);
1002 E->setArgType1(Reader.GetType(Record[Idx++]));
1003 E->setArgType2(Reader.GetType(Record[Idx++]));
1004 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1005 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1006 return 0;
1007}
1008
1009unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1010 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001011 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1012 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1013 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001014 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1016 return 3;
1017}
1018
1019unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1020 VisitExpr(E);
1021 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1022 return 0;
1023}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001024
Douglas Gregor725e94b2009-04-16 00:01:45 +00001025unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1026 VisitExpr(E);
1027 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001028 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001029 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1030 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1031 return NumExprs;
1032}
1033
Douglas Gregore246b742009-04-17 19:21:43 +00001034unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1035 VisitExpr(E);
1036 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1037 E->setHasBlockDeclRefExprs(Record[Idx++]);
1038 return 0;
1039}
1040
Douglas Gregor725e94b2009-04-16 00:01:45 +00001041unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1042 VisitExpr(E);
1043 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1044 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1045 E->setByRef(Record[Idx++]);
1046 return 0;
1047}
1048
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001049//===----------------------------------------------------------------------===//
1050// Objective-C Expressions and Statements
1051
1052unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1053 VisitExpr(E);
1054 E->setString(cast<StringLiteral>(StmtStack.back()));
1055 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1056 return 1;
1057}
1058
Chris Lattner80f83c62009-04-22 05:57:30 +00001059unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1060 VisitExpr(E);
1061 E->setEncodedType(Reader.GetType(Record[Idx++]));
1062 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1063 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1064 return 0;
1065}
1066
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001067unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1068 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001069 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001070 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1071 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1072 return 0;
1073}
1074
1075unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1076 VisitExpr(E);
1077 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1078 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1079 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1080 return 0;
1081}
1082
Chris Lattner80f83c62009-04-22 05:57:30 +00001083
Douglas Gregorc713da92009-04-21 22:25:48 +00001084//===----------------------------------------------------------------------===//
1085// PCH reader implementation
1086//===----------------------------------------------------------------------===//
1087
1088namespace {
1089class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1090 PCHReader &Reader;
1091
1092 // If we know the IdentifierInfo in advance, it is here and we will
1093 // not build a new one. Used when deserializing information about an
1094 // identifier that was constructed before the PCH file was read.
1095 IdentifierInfo *KnownII;
1096
1097public:
1098 typedef IdentifierInfo * data_type;
1099
1100 typedef const std::pair<const char*, unsigned> external_key_type;
1101
1102 typedef external_key_type internal_key_type;
1103
1104 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1105 : Reader(Reader), KnownII(II) { }
1106
1107 static bool EqualKey(const internal_key_type& a,
1108 const internal_key_type& b) {
1109 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1110 : false;
1111 }
1112
1113 static unsigned ComputeHash(const internal_key_type& a) {
1114 return BernsteinHash(a.first, a.second);
1115 }
1116
1117 // This hopefully will just get inlined and removed by the optimizer.
1118 static const internal_key_type&
1119 GetInternalKey(const external_key_type& x) { return x; }
1120
1121 static std::pair<unsigned, unsigned>
1122 ReadKeyDataLength(const unsigned char*& d) {
1123 using namespace clang::io;
1124 unsigned KeyLen = ReadUnalignedLE16(d);
1125 unsigned DataLen = ReadUnalignedLE16(d);
1126 return std::make_pair(KeyLen, DataLen);
1127 }
1128
1129 static std::pair<const char*, unsigned>
1130 ReadKey(const unsigned char* d, unsigned n) {
1131 assert(n >= 2 && d[n-1] == '\0');
1132 return std::make_pair((const char*) d, n-1);
1133 }
1134
1135 IdentifierInfo *ReadData(const internal_key_type& k,
1136 const unsigned char* d,
1137 unsigned DataLen) {
1138 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001139 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001140 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1141 Bits >>= 1;
1142 bool Poisoned = Bits & 0x01;
1143 Bits >>= 1;
1144 bool ExtensionToken = Bits & 0x01;
1145 Bits >>= 1;
1146 bool hasMacroDefinition = Bits & 0x01;
1147 Bits >>= 1;
1148 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1149 Bits >>= 10;
1150 unsigned TokenID = Bits & 0xFF;
1151 Bits >>= 8;
1152
Douglas Gregorc713da92009-04-21 22:25:48 +00001153 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001154 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001155 DataLen -= 8;
1156
1157 // Build the IdentifierInfo itself and link the identifier ID with
1158 // the new IdentifierInfo.
1159 IdentifierInfo *II = KnownII;
1160 if (!II)
1161 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1162 k.first, k.first + k.second);
1163 Reader.SetIdentifierInfo(ID, II);
1164
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001165 // Set or check the various bits in the IdentifierInfo structure.
1166 // FIXME: Load token IDs lazily, too?
1167 assert((unsigned)II->getTokenID() == TokenID &&
1168 "Incorrect token ID loaded");
1169 (void)TokenID;
1170 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1171 assert(II->isExtensionToken() == ExtensionToken &&
1172 "Incorrect extension token flag");
1173 (void)ExtensionToken;
1174 II->setIsPoisoned(Poisoned);
1175 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1176 "Incorrect C++ operator keyword flag");
1177 (void)CPlusPlusOperatorKeyword;
1178
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001179 // If this identifier is a macro, deserialize the macro
1180 // definition.
1181 if (hasMacroDefinition) {
1182 uint32_t Offset = ReadUnalignedLE64(d);
1183 Reader.ReadMacroRecord(Offset);
1184 DataLen -= 8;
1185 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001186
1187 // Read all of the declarations visible at global scope with this
1188 // name.
1189 Sema *SemaObj = Reader.getSema();
1190 while (DataLen > 0) {
1191 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001192 if (SemaObj) {
1193 // Introduce this declaration into the translation-unit scope
1194 // and add it to the declaration chain for this identifier, so
1195 // that (unqualified) name lookup will find it.
1196 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1197 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1198 } else {
1199 // Queue this declaration so that it will be added to the
1200 // translation unit scope and identifier's declaration chain
1201 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001202 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001203 }
1204
1205 DataLen -= 4;
1206 }
1207 return II;
1208 }
1209};
1210
1211} // end anonymous namespace
1212
1213/// \brief The on-disk hash table used to contain information about
1214/// all of the identifiers in the program.
1215typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1216 PCHIdentifierLookupTable;
1217
Douglas Gregorc34897d2009-04-09 22:27:44 +00001218// FIXME: use the diagnostics machinery
1219static bool Error(const char *Str) {
1220 std::fprintf(stderr, "%s\n", Str);
1221 return true;
1222}
1223
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001224/// \brief Check the contents of the predefines buffer against the
1225/// contents of the predefines buffer used to build the PCH file.
1226///
1227/// The contents of the two predefines buffers should be the same. If
1228/// not, then some command-line option changed the preprocessor state
1229/// and we must reject the PCH file.
1230///
1231/// \param PCHPredef The start of the predefines buffer in the PCH
1232/// file.
1233///
1234/// \param PCHPredefLen The length of the predefines buffer in the PCH
1235/// file.
1236///
1237/// \param PCHBufferID The FileID for the PCH predefines buffer.
1238///
1239/// \returns true if there was a mismatch (in which case the PCH file
1240/// should be ignored), or false otherwise.
1241bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1242 unsigned PCHPredefLen,
1243 FileID PCHBufferID) {
1244 const char *Predef = PP.getPredefines().c_str();
1245 unsigned PredefLen = PP.getPredefines().size();
1246
1247 // If the two predefines buffers compare equal, we're done!.
1248 if (PredefLen == PCHPredefLen &&
1249 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1250 return false;
1251
1252 // The predefines buffers are different. Produce a reasonable
1253 // diagnostic showing where they are different.
1254
1255 // The source locations (potentially in the two different predefines
1256 // buffers)
1257 SourceLocation Loc1, Loc2;
1258 SourceManager &SourceMgr = PP.getSourceManager();
1259
1260 // Create a source buffer for our predefines string, so
1261 // that we can build a diagnostic that points into that
1262 // source buffer.
1263 FileID BufferID;
1264 if (Predef && Predef[0]) {
1265 llvm::MemoryBuffer *Buffer
1266 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1267 "<built-in>");
1268 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1269 }
1270
1271 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1272 std::pair<const char *, const char *> Locations
1273 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1274
1275 if (Locations.first != Predef + MinLen) {
1276 // We found the location in the two buffers where there is a
1277 // difference. Form source locations to point there (in both
1278 // buffers).
1279 unsigned Offset = Locations.first - Predef;
1280 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1281 .getFileLocWithOffset(Offset);
1282 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1283 .getFileLocWithOffset(Offset);
1284 } else if (PredefLen > PCHPredefLen) {
1285 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1286 .getFileLocWithOffset(MinLen);
1287 } else {
1288 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1289 .getFileLocWithOffset(MinLen);
1290 }
1291
1292 Diag(Loc1, diag::warn_pch_preprocessor);
1293 if (Loc2.isValid())
1294 Diag(Loc2, diag::note_predef_in_pch);
1295 Diag(diag::note_ignoring_pch) << FileName;
1296 return true;
1297}
1298
Douglas Gregor635f97f2009-04-13 16:31:14 +00001299/// \brief Read the line table in the source manager block.
1300/// \returns true if ther was an error.
1301static bool ParseLineTable(SourceManager &SourceMgr,
1302 llvm::SmallVectorImpl<uint64_t> &Record) {
1303 unsigned Idx = 0;
1304 LineTableInfo &LineTable = SourceMgr.getLineTable();
1305
1306 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001307 std::map<int, int> FileIDs;
1308 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001309 // Extract the file name
1310 unsigned FilenameLen = Record[Idx++];
1311 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1312 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001313 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1314 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001315 }
1316
1317 // Parse the line entries
1318 std::vector<LineEntry> Entries;
1319 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001320 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001321
1322 // Extract the line entries
1323 unsigned NumEntries = Record[Idx++];
1324 Entries.clear();
1325 Entries.reserve(NumEntries);
1326 for (unsigned I = 0; I != NumEntries; ++I) {
1327 unsigned FileOffset = Record[Idx++];
1328 unsigned LineNo = Record[Idx++];
1329 int FilenameID = Record[Idx++];
1330 SrcMgr::CharacteristicKind FileKind
1331 = (SrcMgr::CharacteristicKind)Record[Idx++];
1332 unsigned IncludeOffset = Record[Idx++];
1333 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1334 FileKind, IncludeOffset));
1335 }
1336 LineTable.AddEntry(FID, Entries);
1337 }
1338
1339 return false;
1340}
1341
Douglas Gregorab1cef72009-04-10 03:52:48 +00001342/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001343PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001344 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001345 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1346 Error("Malformed source manager block record");
1347 return Failure;
1348 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001349
1350 SourceManager &SourceMgr = Context.getSourceManager();
1351 RecordData Record;
1352 while (true) {
1353 unsigned Code = Stream.ReadCode();
1354 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001355 if (Stream.ReadBlockEnd()) {
1356 Error("Error at end of Source Manager block");
1357 return Failure;
1358 }
1359
1360 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001361 }
1362
1363 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1364 // No known subblocks, always skip them.
1365 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001366 if (Stream.SkipBlock()) {
1367 Error("Malformed block record");
1368 return Failure;
1369 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001370 continue;
1371 }
1372
1373 if (Code == llvm::bitc::DEFINE_ABBREV) {
1374 Stream.ReadAbbrevRecord();
1375 continue;
1376 }
1377
1378 // Read a record.
1379 const char *BlobStart;
1380 unsigned BlobLen;
1381 Record.clear();
1382 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1383 default: // Default behavior: ignore.
1384 break;
1385
1386 case pch::SM_SLOC_FILE_ENTRY: {
1387 // FIXME: We would really like to delay the creation of this
1388 // FileEntry until it is actually required, e.g., when producing
1389 // a diagnostic with a source location in this file.
1390 const FileEntry *File
1391 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1392 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001393 FileID ID = SourceMgr.createFileID(File,
1394 SourceLocation::getFromRawEncoding(Record[1]),
1395 (CharacteristicKind)Record[2]);
1396 if (Record[3])
1397 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1398 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001399 break;
1400 }
1401
1402 case pch::SM_SLOC_BUFFER_ENTRY: {
1403 const char *Name = BlobStart;
1404 unsigned Code = Stream.ReadCode();
1405 Record.clear();
1406 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1407 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001408 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001409 llvm::MemoryBuffer *Buffer
1410 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1411 BlobStart + BlobLen - 1,
1412 Name);
1413 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1414
1415 if (strcmp(Name, "<built-in>") == 0
1416 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1417 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001418 break;
1419 }
1420
1421 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1422 SourceLocation SpellingLoc
1423 = SourceLocation::getFromRawEncoding(Record[1]);
1424 SourceMgr.createInstantiationLoc(
1425 SpellingLoc,
1426 SourceLocation::getFromRawEncoding(Record[2]),
1427 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001428 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001429 break;
1430 }
1431
Chris Lattnere1be6022009-04-14 23:22:57 +00001432 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001433 if (ParseLineTable(SourceMgr, Record))
1434 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001435 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001436 }
1437 }
1438}
1439
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001440void PCHReader::ReadMacroRecord(uint64_t Offset) {
1441 // Keep track of where we are in the stream, then jump back there
1442 // after reading this macro.
1443 SavedStreamPosition SavedPosition(Stream);
1444
1445 Stream.JumpToBit(Offset);
1446 RecordData Record;
1447 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1448 MacroInfo *Macro = 0;
1449 while (true) {
1450 unsigned Code = Stream.ReadCode();
1451 switch (Code) {
1452 case llvm::bitc::END_BLOCK:
1453 return;
1454
1455 case llvm::bitc::ENTER_SUBBLOCK:
1456 // No known subblocks, always skip them.
1457 Stream.ReadSubBlockID();
1458 if (Stream.SkipBlock()) {
1459 Error("Malformed block record");
1460 return;
1461 }
1462 continue;
1463
1464 case llvm::bitc::DEFINE_ABBREV:
1465 Stream.ReadAbbrevRecord();
1466 continue;
1467 default: break;
1468 }
1469
1470 // Read a record.
1471 Record.clear();
1472 pch::PreprocessorRecordTypes RecType =
1473 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1474 switch (RecType) {
1475 case pch::PP_COUNTER_VALUE:
1476 // Skip this record.
1477 break;
1478
1479 case pch::PP_MACRO_OBJECT_LIKE:
1480 case pch::PP_MACRO_FUNCTION_LIKE: {
1481 // If we already have a macro, that means that we've hit the end
1482 // of the definition of the macro we were looking for. We're
1483 // done.
1484 if (Macro)
1485 return;
1486
1487 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1488 if (II == 0) {
1489 Error("Macro must have a name");
1490 return;
1491 }
1492 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1493 bool isUsed = Record[2];
1494
1495 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1496 MI->setIsUsed(isUsed);
1497
1498 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1499 // Decode function-like macro info.
1500 bool isC99VarArgs = Record[3];
1501 bool isGNUVarArgs = Record[4];
1502 MacroArgs.clear();
1503 unsigned NumArgs = Record[5];
1504 for (unsigned i = 0; i != NumArgs; ++i)
1505 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1506
1507 // Install function-like macro info.
1508 MI->setIsFunctionLike();
1509 if (isC99VarArgs) MI->setIsC99Varargs();
1510 if (isGNUVarArgs) MI->setIsGNUVarargs();
1511 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1512 PP.getPreprocessorAllocator());
1513 }
1514
1515 // Finally, install the macro.
1516 PP.setMacroInfo(II, MI);
1517
1518 // Remember that we saw this macro last so that we add the tokens that
1519 // form its body to it.
1520 Macro = MI;
1521 ++NumMacrosRead;
1522 break;
1523 }
1524
1525 case pch::PP_TOKEN: {
1526 // If we see a TOKEN before a PP_MACRO_*, then the file is
1527 // erroneous, just pretend we didn't see this.
1528 if (Macro == 0) break;
1529
1530 Token Tok;
1531 Tok.startToken();
1532 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1533 Tok.setLength(Record[1]);
1534 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1535 Tok.setIdentifierInfo(II);
1536 Tok.setKind((tok::TokenKind)Record[3]);
1537 Tok.setFlag((Token::TokenFlags)Record[4]);
1538 Macro->AddTokenToBody(Tok);
1539 break;
1540 }
1541 }
1542 }
1543}
1544
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001545bool PCHReader::ReadPreprocessorBlock() {
1546 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1547 return Error("Malformed preprocessor block record");
1548
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001549 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001550 while (true) {
1551 unsigned Code = Stream.ReadCode();
1552 switch (Code) {
1553 case llvm::bitc::END_BLOCK:
1554 if (Stream.ReadBlockEnd())
1555 return Error("Error at end of preprocessor block");
1556 return false;
1557
1558 case llvm::bitc::ENTER_SUBBLOCK:
1559 // No known subblocks, always skip them.
1560 Stream.ReadSubBlockID();
1561 if (Stream.SkipBlock())
1562 return Error("Malformed block record");
1563 continue;
1564
1565 case llvm::bitc::DEFINE_ABBREV:
1566 Stream.ReadAbbrevRecord();
1567 continue;
1568 default: break;
1569 }
1570
1571 // Read a record.
1572 Record.clear();
1573 pch::PreprocessorRecordTypes RecType =
1574 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1575 switch (RecType) {
1576 default: // Default behavior: ignore unknown records.
1577 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001578 case pch::PP_COUNTER_VALUE:
1579 if (!Record.empty())
1580 PP.setCounterValue(Record[0]);
1581 break;
1582
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001583 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001584 case pch::PP_MACRO_FUNCTION_LIKE:
1585 case pch::PP_TOKEN:
1586 // Once we've hit a macro definition or a token, we're done.
1587 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001588 }
1589 }
1590}
1591
Steve Naroff9e84d782009-04-23 10:39:46 +00001592bool PCHReader::ReadSelectorBlock() {
1593 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1594 return Error("Malformed selector block record");
1595
1596 RecordData Record;
1597 while (true) {
1598 unsigned Code = Stream.ReadCode();
1599 switch (Code) {
1600 case llvm::bitc::END_BLOCK:
1601 if (Stream.ReadBlockEnd())
1602 return Error("Error at end of preprocessor block");
1603 return false;
1604
1605 case llvm::bitc::ENTER_SUBBLOCK:
1606 // No known subblocks, always skip them.
1607 Stream.ReadSubBlockID();
1608 if (Stream.SkipBlock())
1609 return Error("Malformed block record");
1610 continue;
1611
1612 case llvm::bitc::DEFINE_ABBREV:
1613 Stream.ReadAbbrevRecord();
1614 continue;
1615 default: break;
1616 }
1617
1618 // Read a record.
1619 Record.clear();
1620 pch::PCHRecordTypes RecType =
1621 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1622 switch (RecType) {
1623 default: // Default behavior: ignore unknown records.
1624 break;
1625 case pch::SELECTOR_TABLE:
1626 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1627 unsigned NumSels = Record[Idx++];
1628
1629 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1630 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1631 unsigned NumArgs = Record[Idx++];
1632 KeyIdents.clear();
1633 if (NumArgs <= 1) {
1634 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1635 assert(II && "DecodeIdentifierInfo returned 0");
1636 KeyIdents.push_back(II);
1637 } else {
1638 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1639 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1640 assert(II && "DecodeIdentifierInfo returned 0");
1641 KeyIdents.push_back(II);
1642 }
1643 }
1644 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1645 SelectorData.push_back(Sel);
1646 }
1647 }
1648 }
1649 return false;
1650}
1651
Douglas Gregorc713da92009-04-21 22:25:48 +00001652PCHReader::PCHReadResult
Steve Naroff9e84d782009-04-23 10:39:46 +00001653PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1654 uint64_t &SelectorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001655 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1656 Error("Malformed block record");
1657 return Failure;
1658 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001659
1660 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001661 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001662 while (!Stream.AtEndOfStream()) {
1663 unsigned Code = Stream.ReadCode();
1664 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001665 if (Stream.ReadBlockEnd()) {
1666 Error("Error at end of module block");
1667 return Failure;
1668 }
Chris Lattner29241862009-04-11 21:15:38 +00001669
Douglas Gregor179cfb12009-04-10 20:39:37 +00001670 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001671 }
1672
1673 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1674 switch (Stream.ReadSubBlockID()) {
1675 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1676 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1677 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001678 if (Stream.SkipBlock()) {
1679 Error("Malformed block record");
1680 return Failure;
1681 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001682 break;
1683
Chris Lattner29241862009-04-11 21:15:38 +00001684 case pch::PREPROCESSOR_BLOCK_ID:
1685 // Skip the preprocessor block for now, but remember where it is. We
1686 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001687 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001688 Error("Multiple preprocessor blocks found.");
1689 return Failure;
1690 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001691 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001692 if (Stream.SkipBlock()) {
1693 Error("Malformed block record");
1694 return Failure;
1695 }
1696 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001697
1698 case pch::SELECTOR_BLOCK_ID:
1699 // Skip the selector block for now, but remember where it is. We
1700 // want to read it in after the identifier table.
1701 if (SelectorBlockOffset) {
1702 Error("Multiple selector blocks found.");
1703 return Failure;
1704 }
1705 SelectorBlockOffset = Stream.GetCurrentBitNo();
1706 if (Stream.SkipBlock()) {
1707 Error("Malformed block record");
1708 return Failure;
1709 }
1710 break;
Chris Lattner29241862009-04-11 21:15:38 +00001711
Douglas Gregorab1cef72009-04-10 03:52:48 +00001712 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001713 switch (ReadSourceManagerBlock()) {
1714 case Success:
1715 break;
1716
1717 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001718 Error("Malformed source manager block");
1719 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001720
1721 case IgnorePCH:
1722 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001723 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001724 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001725 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001726 continue;
1727 }
1728
1729 if (Code == llvm::bitc::DEFINE_ABBREV) {
1730 Stream.ReadAbbrevRecord();
1731 continue;
1732 }
1733
1734 // Read and process a record.
1735 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001736 const char *BlobStart = 0;
1737 unsigned BlobLen = 0;
1738 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1739 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001740 default: // Default behavior: ignore.
1741 break;
1742
1743 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001744 if (!TypeOffsets.empty()) {
1745 Error("Duplicate TYPE_OFFSET record in PCH file");
1746 return Failure;
1747 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001748 TypeOffsets.swap(Record);
1749 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1750 break;
1751
1752 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001753 if (!DeclOffsets.empty()) {
1754 Error("Duplicate DECL_OFFSET record in PCH file");
1755 return Failure;
1756 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001757 DeclOffsets.swap(Record);
1758 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1759 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001760
1761 case pch::LANGUAGE_OPTIONS:
1762 if (ParseLanguageOptions(Record))
1763 return IgnorePCH;
1764 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001765
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001766 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001767 std::string TargetTriple(BlobStart, BlobLen);
1768 if (TargetTriple != Context.Target.getTargetTriple()) {
1769 Diag(diag::warn_pch_target_triple)
1770 << TargetTriple << Context.Target.getTargetTriple();
1771 Diag(diag::note_ignoring_pch) << FileName;
1772 return IgnorePCH;
1773 }
1774 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001775 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001776
1777 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001778 IdentifierTableData = BlobStart;
1779 IdentifierLookupTable
1780 = PCHIdentifierLookupTable::Create(
1781 (const unsigned char *)IdentifierTableData + Record[0],
1782 (const unsigned char *)IdentifierTableData,
1783 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001784 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001785 break;
1786
1787 case pch::IDENTIFIER_OFFSET:
1788 if (!IdentifierData.empty()) {
1789 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1790 return Failure;
1791 }
1792 IdentifierData.swap(Record);
1793#ifndef NDEBUG
1794 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1795 if ((IdentifierData[I] & 0x01) == 0) {
1796 Error("Malformed identifier table in the precompiled header");
1797 return Failure;
1798 }
1799 }
1800#endif
1801 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001802
1803 case pch::EXTERNAL_DEFINITIONS:
1804 if (!ExternalDefinitions.empty()) {
1805 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1806 return Failure;
1807 }
1808 ExternalDefinitions.swap(Record);
1809 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001810
Douglas Gregore01ad442009-04-18 05:55:16 +00001811 case pch::SPECIAL_TYPES:
1812 SpecialTypes.swap(Record);
1813 break;
1814
Douglas Gregor456e0952009-04-17 22:13:46 +00001815 case pch::STATISTICS:
1816 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001817 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001818 TotalLexicalDeclContexts = Record[2];
1819 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001820 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001821 case pch::TENTATIVE_DEFINITIONS:
1822 if (!TentativeDefinitions.empty()) {
1823 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1824 return Failure;
1825 }
1826 TentativeDefinitions.swap(Record);
1827 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001828
1829 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1830 if (!LocallyScopedExternalDecls.empty()) {
1831 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1832 return Failure;
1833 }
1834 LocallyScopedExternalDecls.swap(Record);
1835 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001836 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001837 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001838 Error("Premature end of bitstream");
1839 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001840}
1841
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001842PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001843 // Set the PCH file name.
1844 this->FileName = FileName;
1845
Douglas Gregorc34897d2009-04-09 22:27:44 +00001846 // Open the PCH file.
1847 std::string ErrStr;
1848 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001849 if (!Buffer) {
1850 Error(ErrStr.c_str());
1851 return IgnorePCH;
1852 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001853
1854 // Initialize the stream
1855 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1856 (const unsigned char *)Buffer->getBufferEnd());
1857
1858 // Sniff for the signature.
1859 if (Stream.Read(8) != 'C' ||
1860 Stream.Read(8) != 'P' ||
1861 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001862 Stream.Read(8) != 'H') {
1863 Error("Not a PCH file");
1864 return IgnorePCH;
1865 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001866
1867 // We expect a number of well-defined blocks, though we don't necessarily
1868 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001869 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff9e84d782009-04-23 10:39:46 +00001870 uint64_t SelectorBlockOffset = 0;
1871
Douglas Gregorc34897d2009-04-09 22:27:44 +00001872 while (!Stream.AtEndOfStream()) {
1873 unsigned Code = Stream.ReadCode();
1874
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001875 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1876 Error("Invalid record at top-level");
1877 return Failure;
1878 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001879
1880 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001881
Douglas Gregorc34897d2009-04-09 22:27:44 +00001882 // We only know the PCH subblock ID.
1883 switch (BlockID) {
1884 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001885 if (Stream.ReadBlockInfoBlock()) {
1886 Error("Malformed BlockInfoBlock");
1887 return Failure;
1888 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001889 break;
1890 case pch::PCH_BLOCK_ID:
Steve Naroff9e84d782009-04-23 10:39:46 +00001891 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001892 case Success:
1893 break;
1894
1895 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001896 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001897
1898 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001899 // FIXME: We could consider reading through to the end of this
1900 // PCH block, skipping subblocks, to see if there are other
1901 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001902 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001903 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001904 break;
1905 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001906 if (Stream.SkipBlock()) {
1907 Error("Malformed block record");
1908 return Failure;
1909 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001910 break;
1911 }
1912 }
1913
1914 // Load the translation unit declaration
1915 ReadDeclRecord(DeclOffsets[0], 0);
1916
Douglas Gregorc713da92009-04-21 22:25:48 +00001917 // Initialization of builtins and library builtins occurs before the
1918 // PCH file is read, so there may be some identifiers that were
1919 // loaded into the IdentifierTable before we intercepted the
1920 // creation of identifiers. Iterate through the list of known
1921 // identifiers and determine whether we have to establish
1922 // preprocessor definitions or top-level identifier declaration
1923 // chains for those identifiers.
1924 //
1925 // We copy the IdentifierInfo pointers to a small vector first,
1926 // since de-serializing declarations or macro definitions can add
1927 // new entries into the identifier table, invalidating the
1928 // iterators.
1929 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1930 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1931 IdEnd = PP.getIdentifierTable().end();
1932 Id != IdEnd; ++Id)
1933 Identifiers.push_back(Id->second);
1934 PCHIdentifierLookupTable *IdTable
1935 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1936 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1937 IdentifierInfo *II = Identifiers[I];
1938 // Look in the on-disk hash table for an entry for
1939 PCHIdentifierLookupTrait Info(*this, II);
1940 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1941 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1942 if (Pos == IdTable->end())
1943 continue;
1944
1945 // Dereferencing the iterator has the effect of populating the
1946 // IdentifierInfo node with the various declarations it needs.
1947 (void)*Pos;
1948 }
1949
Douglas Gregore01ad442009-04-18 05:55:16 +00001950 // Load the special types.
1951 Context.setBuiltinVaListType(
1952 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00001953 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1954 Context.setObjCIdType(GetType(Id));
1955 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1956 Context.setObjCSelType(GetType(Sel));
1957 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1958 Context.setObjCProtoType(GetType(Proto));
1959 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1960 Context.setObjCClassType(GetType(Class));
1961 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1962 Context.setCFConstantStringType(GetType(String));
1963 if (unsigned FastEnum
1964 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1965 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc713da92009-04-21 22:25:48 +00001966 // If we saw the preprocessor block, read it now.
1967 if (PreprocessorBlockOffset) {
1968 SavedStreamPosition SavedPos(Stream);
1969 Stream.JumpToBit(PreprocessorBlockOffset);
1970 if (ReadPreprocessorBlock()) {
1971 Error("Malformed preprocessor block");
1972 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001973 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001974 }
Steve Naroff9e84d782009-04-23 10:39:46 +00001975 if (SelectorBlockOffset) {
1976 SavedStreamPosition SavedPos(Stream);
1977 Stream.JumpToBit(SelectorBlockOffset);
1978 if (ReadSelectorBlock()) {
1979 Error("Malformed preprocessor block");
1980 return Failure;
1981 }
1982 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001983
Douglas Gregorc713da92009-04-21 22:25:48 +00001984 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001985}
1986
Douglas Gregor179cfb12009-04-10 20:39:37 +00001987/// \brief Parse the record that corresponds to a LangOptions data
1988/// structure.
1989///
1990/// This routine compares the language options used to generate the
1991/// PCH file against the language options set for the current
1992/// compilation. For each option, we classify differences between the
1993/// two compiler states as either "benign" or "important". Benign
1994/// differences don't matter, and we accept them without complaint
1995/// (and without modifying the language options). Differences between
1996/// the states for important options cause the PCH file to be
1997/// unusable, so we emit a warning and return true to indicate that
1998/// there was an error.
1999///
2000/// \returns true if the PCH file is unacceptable, false otherwise.
2001bool PCHReader::ParseLanguageOptions(
2002 const llvm::SmallVectorImpl<uint64_t> &Record) {
2003 const LangOptions &LangOpts = Context.getLangOptions();
2004#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2005#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2006 if (Record[Idx] != LangOpts.Option) { \
2007 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2008 Diag(diag::note_ignoring_pch) << FileName; \
2009 return true; \
2010 } \
2011 ++Idx
2012
2013 unsigned Idx = 0;
2014 PARSE_LANGOPT_BENIGN(Trigraphs);
2015 PARSE_LANGOPT_BENIGN(BCPLComment);
2016 PARSE_LANGOPT_BENIGN(DollarIdents);
2017 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2018 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2019 PARSE_LANGOPT_BENIGN(ImplicitInt);
2020 PARSE_LANGOPT_BENIGN(Digraphs);
2021 PARSE_LANGOPT_BENIGN(HexFloats);
2022 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2023 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2024 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2025 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2026 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2027 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2028 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2029 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2030 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2031 PARSE_LANGOPT_BENIGN(PascalStrings);
2032 PARSE_LANGOPT_BENIGN(Boolean);
2033 PARSE_LANGOPT_BENIGN(WritableStrings);
2034 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2035 diag::warn_pch_lax_vector_conversions);
2036 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2037 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2038 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2039 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2040 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2041 diag::warn_pch_thread_safe_statics);
2042 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2043 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2044 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2045 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2046 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2047 diag::warn_pch_heinous_extensions);
2048 // FIXME: Most of the options below are benign if the macro wasn't
2049 // used. Unfortunately, this means that a PCH compiled without
2050 // optimization can't be used with optimization turned on, even
2051 // though the only thing that changes is whether __OPTIMIZE__ was
2052 // defined... but if __OPTIMIZE__ never showed up in the header, it
2053 // doesn't matter. We could consider making this some special kind
2054 // of check.
2055 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2056 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2057 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2058 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2059 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2060 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2061 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2062 Diag(diag::warn_pch_gc_mode)
2063 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2064 Diag(diag::note_ignoring_pch) << FileName;
2065 return true;
2066 }
2067 ++Idx;
2068 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2069 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2070#undef PARSE_LANGOPT_IRRELEVANT
2071#undef PARSE_LANGOPT_BENIGN
2072
2073 return false;
2074}
2075
Douglas Gregorc34897d2009-04-09 22:27:44 +00002076/// \brief Read and return the type at the given offset.
2077///
2078/// This routine actually reads the record corresponding to the type
2079/// at the given offset in the bitstream. It is a helper routine for
2080/// GetType, which deals with reading type IDs.
2081QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002082 // Keep track of where we are in the stream, then jump back there
2083 // after reading this type.
2084 SavedStreamPosition SavedPosition(Stream);
2085
Douglas Gregorc34897d2009-04-09 22:27:44 +00002086 Stream.JumpToBit(Offset);
2087 RecordData Record;
2088 unsigned Code = Stream.ReadCode();
2089 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002090 case pch::TYPE_EXT_QUAL: {
2091 assert(Record.size() == 3 &&
2092 "Incorrect encoding of extended qualifier type");
2093 QualType Base = GetType(Record[0]);
2094 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2095 unsigned AddressSpace = Record[2];
2096
2097 QualType T = Base;
2098 if (GCAttr != QualType::GCNone)
2099 T = Context.getObjCGCQualType(T, GCAttr);
2100 if (AddressSpace)
2101 T = Context.getAddrSpaceQualType(T, AddressSpace);
2102 return T;
2103 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002104
Douglas Gregorc34897d2009-04-09 22:27:44 +00002105 case pch::TYPE_FIXED_WIDTH_INT: {
2106 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2107 return Context.getFixedWidthIntType(Record[0], Record[1]);
2108 }
2109
2110 case pch::TYPE_COMPLEX: {
2111 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2112 QualType ElemType = GetType(Record[0]);
2113 return Context.getComplexType(ElemType);
2114 }
2115
2116 case pch::TYPE_POINTER: {
2117 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2118 QualType PointeeType = GetType(Record[0]);
2119 return Context.getPointerType(PointeeType);
2120 }
2121
2122 case pch::TYPE_BLOCK_POINTER: {
2123 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2124 QualType PointeeType = GetType(Record[0]);
2125 return Context.getBlockPointerType(PointeeType);
2126 }
2127
2128 case pch::TYPE_LVALUE_REFERENCE: {
2129 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2130 QualType PointeeType = GetType(Record[0]);
2131 return Context.getLValueReferenceType(PointeeType);
2132 }
2133
2134 case pch::TYPE_RVALUE_REFERENCE: {
2135 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2136 QualType PointeeType = GetType(Record[0]);
2137 return Context.getRValueReferenceType(PointeeType);
2138 }
2139
2140 case pch::TYPE_MEMBER_POINTER: {
2141 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2142 QualType PointeeType = GetType(Record[0]);
2143 QualType ClassType = GetType(Record[1]);
2144 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2145 }
2146
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002147 case pch::TYPE_CONSTANT_ARRAY: {
2148 QualType ElementType = GetType(Record[0]);
2149 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2150 unsigned IndexTypeQuals = Record[2];
2151 unsigned Idx = 3;
2152 llvm::APInt Size = ReadAPInt(Record, Idx);
2153 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2154 }
2155
2156 case pch::TYPE_INCOMPLETE_ARRAY: {
2157 QualType ElementType = GetType(Record[0]);
2158 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2159 unsigned IndexTypeQuals = Record[2];
2160 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2161 }
2162
2163 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002164 QualType ElementType = GetType(Record[0]);
2165 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2166 unsigned IndexTypeQuals = Record[2];
2167 return Context.getVariableArrayType(ElementType, ReadExpr(),
2168 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002169 }
2170
2171 case pch::TYPE_VECTOR: {
2172 if (Record.size() != 2) {
2173 Error("Incorrect encoding of vector type in PCH file");
2174 return QualType();
2175 }
2176
2177 QualType ElementType = GetType(Record[0]);
2178 unsigned NumElements = Record[1];
2179 return Context.getVectorType(ElementType, NumElements);
2180 }
2181
2182 case pch::TYPE_EXT_VECTOR: {
2183 if (Record.size() != 2) {
2184 Error("Incorrect encoding of extended vector type in PCH file");
2185 return QualType();
2186 }
2187
2188 QualType ElementType = GetType(Record[0]);
2189 unsigned NumElements = Record[1];
2190 return Context.getExtVectorType(ElementType, NumElements);
2191 }
2192
2193 case pch::TYPE_FUNCTION_NO_PROTO: {
2194 if (Record.size() != 1) {
2195 Error("Incorrect encoding of no-proto function type");
2196 return QualType();
2197 }
2198 QualType ResultType = GetType(Record[0]);
2199 return Context.getFunctionNoProtoType(ResultType);
2200 }
2201
2202 case pch::TYPE_FUNCTION_PROTO: {
2203 QualType ResultType = GetType(Record[0]);
2204 unsigned Idx = 1;
2205 unsigned NumParams = Record[Idx++];
2206 llvm::SmallVector<QualType, 16> ParamTypes;
2207 for (unsigned I = 0; I != NumParams; ++I)
2208 ParamTypes.push_back(GetType(Record[Idx++]));
2209 bool isVariadic = Record[Idx++];
2210 unsigned Quals = Record[Idx++];
2211 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2212 isVariadic, Quals);
2213 }
2214
2215 case pch::TYPE_TYPEDEF:
2216 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2217 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2218
2219 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002220 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002221
2222 case pch::TYPE_TYPEOF: {
2223 if (Record.size() != 1) {
2224 Error("Incorrect encoding of typeof(type) in PCH file");
2225 return QualType();
2226 }
2227 QualType UnderlyingType = GetType(Record[0]);
2228 return Context.getTypeOfType(UnderlyingType);
2229 }
2230
2231 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002232 assert(Record.size() == 1 && "Incorrect encoding of record type");
2233 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002234
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002235 case pch::TYPE_ENUM:
2236 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2237 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2238
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002239 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002240 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2241 return Context.getObjCInterfaceType(
2242 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002243
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002244 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2245 unsigned Idx = 0;
2246 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2247 unsigned NumProtos = Record[Idx++];
2248 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2249 for (unsigned I = 0; I != NumProtos; ++I)
2250 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2251 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2252 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002253
Chris Lattner9b9f2352009-04-22 06:40:03 +00002254 case pch::TYPE_OBJC_QUALIFIED_ID: {
2255 unsigned Idx = 0;
2256 unsigned NumProtos = Record[Idx++];
2257 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2258 for (unsigned I = 0; I != NumProtos; ++I)
2259 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2260 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2261 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002262 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002263 // Suppress a GCC warning
2264 return QualType();
2265}
2266
2267/// \brief Note that we have loaded the declaration with the given
2268/// Index.
2269///
2270/// This routine notes that this declaration has already been loaded,
2271/// so that future GetDecl calls will return this declaration rather
2272/// than trying to load a new declaration.
2273inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2274 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2275 DeclAlreadyLoaded[Index] = true;
2276 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2277}
2278
2279/// \brief Read the declaration at the given offset from the PCH file.
2280Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002281 // Keep track of where we are in the stream, then jump back there
2282 // after reading this declaration.
2283 SavedStreamPosition SavedPosition(Stream);
2284
Douglas Gregorc34897d2009-04-09 22:27:44 +00002285 Decl *D = 0;
2286 Stream.JumpToBit(Offset);
2287 RecordData Record;
2288 unsigned Code = Stream.ReadCode();
2289 unsigned Idx = 0;
2290 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002291
Douglas Gregorc34897d2009-04-09 22:27:44 +00002292 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002293 case pch::DECL_ATTR:
2294 case pch::DECL_CONTEXT_LEXICAL:
2295 case pch::DECL_CONTEXT_VISIBLE:
2296 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2297 break;
2298
Douglas Gregorc34897d2009-04-09 22:27:44 +00002299 case pch::DECL_TRANSLATION_UNIT:
2300 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002301 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002302 break;
2303
2304 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002305 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002306 break;
2307 }
2308
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002309 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002310 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002311 break;
2312 }
2313
Douglas Gregor982365e2009-04-13 21:20:57 +00002314 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002315 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2316 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002317 break;
2318 }
2319
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002320 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002321 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2322 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002323 break;
2324 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002325
2326 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002327 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2328 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002329 break;
2330 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002331
Steve Naroff79ea0e02009-04-20 15:06:07 +00002332 case pch::DECL_OBJC_METHOD: {
2333 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2334 Selector(), QualType(), 0);
2335 break;
2336 }
2337
Steve Naroff97b53bd2009-04-21 15:12:33 +00002338 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002339 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2340 break;
2341 }
2342
Steve Naroff97b53bd2009-04-21 15:12:33 +00002343 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002344 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2345 ObjCIvarDecl::None);
2346 break;
2347 }
2348
Steve Naroff97b53bd2009-04-21 15:12:33 +00002349 case pch::DECL_OBJC_PROTOCOL: {
2350 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2351 break;
2352 }
2353
2354 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2355 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2356 QualType(), 0);
2357 break;
2358 }
2359
2360 case pch::DECL_OBJC_CLASS: {
2361 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2362 break;
2363 }
2364
2365 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2366 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2367 break;
2368 }
2369
2370 case pch::DECL_OBJC_CATEGORY: {
2371 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2372 break;
2373 }
2374
2375 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002376 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002377 break;
2378 }
2379
2380 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002381 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002382 break;
2383 }
2384
2385 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002386 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002387 break;
2388 }
2389
2390 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002391 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002392 break;
2393 }
2394
2395 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002396 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2397 SourceLocation(), 0,
2398 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002399 break;
2400 }
2401
Douglas Gregor982365e2009-04-13 21:20:57 +00002402 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002403 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2404 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002405 break;
2406 }
2407
Douglas Gregorc34897d2009-04-09 22:27:44 +00002408 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002409 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2410 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002411 break;
2412 }
2413
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002414 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002415 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2416 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002417 break;
2418 }
2419
2420 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002421 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002422 QualType(), QualType(), VarDecl::None,
2423 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002424 break;
2425 }
2426
Douglas Gregor2a491792009-04-13 22:49:25 +00002427 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002428 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002429 break;
2430 }
2431
2432 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002433 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002434 break;
2435 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002436 }
2437
Douglas Gregorc713da92009-04-21 22:25:48 +00002438 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002439 if (D) {
2440 LoadedDecl(Index, D);
2441 Reader.Visit(D);
2442 }
2443
Douglas Gregorc34897d2009-04-09 22:27:44 +00002444 // If this declaration is also a declaration context, get the
2445 // offsets for its tables of lexical and visible declarations.
2446 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2447 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2448 if (Offsets.first || Offsets.second) {
2449 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2450 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2451 DeclContextOffsets[DC] = Offsets;
2452 }
2453 }
2454 assert(Idx == Record.size());
2455
Douglas Gregor405b6432009-04-22 19:09:20 +00002456 if (Consumer) {
2457 // If we have deserialized a declaration that has a definition the
2458 // AST consumer might need to know about, notify the consumer
2459 // about that definition now.
2460 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2461 if (Var->isFileVarDecl() && Var->getInit()) {
2462 DeclGroupRef DG(Var);
2463 Consumer->HandleTopLevelDecl(DG);
2464 }
2465 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2466 if (Func->isThisDeclarationADefinition()) {
2467 DeclGroupRef DG(Func);
2468 Consumer->HandleTopLevelDecl(DG);
2469 }
2470 }
2471 }
2472
Douglas Gregorc34897d2009-04-09 22:27:44 +00002473 return D;
2474}
2475
Douglas Gregorac8f2802009-04-10 17:25:41 +00002476QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002477 unsigned Quals = ID & 0x07;
2478 unsigned Index = ID >> 3;
2479
2480 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2481 QualType T;
2482 switch ((pch::PredefinedTypeIDs)Index) {
2483 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2484 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2485 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2486
2487 case pch::PREDEF_TYPE_CHAR_U_ID:
2488 case pch::PREDEF_TYPE_CHAR_S_ID:
2489 // FIXME: Check that the signedness of CharTy is correct!
2490 T = Context.CharTy;
2491 break;
2492
2493 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2494 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2495 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2496 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2497 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2498 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2499 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2500 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2501 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2502 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2503 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2504 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2505 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2506 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2507 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2508 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2509 }
2510
2511 assert(!T.isNull() && "Unknown predefined type");
2512 return T.getQualifiedType(Quals);
2513 }
2514
2515 Index -= pch::NUM_PREDEF_TYPE_IDS;
2516 if (!TypeAlreadyLoaded[Index]) {
2517 // Load the type from the PCH file.
2518 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2519 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2520 TypeAlreadyLoaded[Index] = true;
2521 }
2522
2523 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2524}
2525
Douglas Gregorac8f2802009-04-10 17:25:41 +00002526Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002527 if (ID == 0)
2528 return 0;
2529
2530 unsigned Index = ID - 1;
2531 if (DeclAlreadyLoaded[Index])
2532 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2533
2534 // Load the declaration from the PCH file.
2535 return ReadDeclRecord(DeclOffsets[Index], Index);
2536}
2537
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002538Stmt *PCHReader::GetStmt(uint64_t Offset) {
2539 // Keep track of where we are in the stream, then jump back there
2540 // after reading this declaration.
2541 SavedStreamPosition SavedPosition(Stream);
2542
2543 Stream.JumpToBit(Offset);
2544 return ReadStmt();
2545}
2546
Douglas Gregorc34897d2009-04-09 22:27:44 +00002547bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002548 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002549 assert(DC->hasExternalLexicalStorage() &&
2550 "DeclContext has no lexical decls in storage");
2551 uint64_t Offset = DeclContextOffsets[DC].first;
2552 assert(Offset && "DeclContext has no lexical decls in storage");
2553
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002554 // Keep track of where we are in the stream, then jump back there
2555 // after reading this context.
2556 SavedStreamPosition SavedPosition(Stream);
2557
Douglas Gregorc34897d2009-04-09 22:27:44 +00002558 // Load the record containing all of the declarations lexically in
2559 // this context.
2560 Stream.JumpToBit(Offset);
2561 RecordData Record;
2562 unsigned Code = Stream.ReadCode();
2563 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002564 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002565 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2566
2567 // Load all of the declaration IDs
2568 Decls.clear();
2569 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002570 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002571 return false;
2572}
2573
2574bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2575 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2576 assert(DC->hasExternalVisibleStorage() &&
2577 "DeclContext has no visible decls in storage");
2578 uint64_t Offset = DeclContextOffsets[DC].second;
2579 assert(Offset && "DeclContext has no visible decls in storage");
2580
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002581 // Keep track of where we are in the stream, then jump back there
2582 // after reading this context.
2583 SavedStreamPosition SavedPosition(Stream);
2584
Douglas Gregorc34897d2009-04-09 22:27:44 +00002585 // Load the record containing all of the declarations visible in
2586 // this context.
2587 Stream.JumpToBit(Offset);
2588 RecordData Record;
2589 unsigned Code = Stream.ReadCode();
2590 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002591 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002592 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2593 if (Record.size() == 0)
2594 return false;
2595
2596 Decls.clear();
2597
2598 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002599 while (Idx < Record.size()) {
2600 Decls.push_back(VisibleDeclaration());
2601 Decls.back().Name = ReadDeclarationName(Record, Idx);
2602
Douglas Gregorc34897d2009-04-09 22:27:44 +00002603 unsigned Size = Record[Idx++];
2604 llvm::SmallVector<unsigned, 4> & LoadedDecls
2605 = Decls.back().Declarations;
2606 LoadedDecls.reserve(Size);
2607 for (unsigned I = 0; I < Size; ++I)
2608 LoadedDecls.push_back(Record[Idx++]);
2609 }
2610
Douglas Gregoraf136d92009-04-22 22:34:57 +00002611 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002612 return false;
2613}
2614
Douglas Gregor631f6c62009-04-14 00:24:19 +00002615void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002616 this->Consumer = Consumer;
2617
Douglas Gregor631f6c62009-04-14 00:24:19 +00002618 if (!Consumer)
2619 return;
2620
2621 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2622 Decl *D = GetDecl(ExternalDefinitions[I]);
2623 DeclGroupRef DG(D);
2624 Consumer->HandleTopLevelDecl(DG);
2625 }
2626}
2627
Douglas Gregorc34897d2009-04-09 22:27:44 +00002628void PCHReader::PrintStats() {
2629 std::fprintf(stderr, "*** PCH Statistics:\n");
2630
2631 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2632 TypeAlreadyLoaded.end(),
2633 true);
2634 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2635 DeclAlreadyLoaded.end(),
2636 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002637 unsigned NumIdentifiersLoaded = 0;
2638 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2639 if ((IdentifierData[I] & 0x01) == 0)
2640 ++NumIdentifiersLoaded;
2641 }
2642
Douglas Gregorc34897d2009-04-09 22:27:44 +00002643 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2644 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002645 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002646 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2647 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002648 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2649 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2650 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2651 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002652 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2653 NumStatementsRead, TotalNumStatements,
2654 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002655 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2656 NumMacrosRead, TotalNumMacros,
2657 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002658 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2659 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2660 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2661 * 100));
2662 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2663 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2664 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2665 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002666 std::fprintf(stderr, "\n");
2667}
2668
Douglas Gregorc713da92009-04-21 22:25:48 +00002669void PCHReader::InitializeSema(Sema &S) {
2670 SemaObj = &S;
2671
Douglas Gregor2554cf22009-04-22 21:15:06 +00002672 // Makes sure any declarations that were deserialized "too early"
2673 // still get added to the identifier's declaration chains.
2674 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2675 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2676 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002677 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002678 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002679
2680 // If there were any tentative definitions, deserialize them and add
2681 // them to Sema's table of tentative definitions.
2682 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2683 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2684 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2685 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002686
2687 // If there were any locally-scoped external declarations,
2688 // deserialize them and add them to Sema's table of locally-scoped
2689 // external declarations.
2690 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2691 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2692 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2693 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002694}
2695
2696IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2697 // Try to find this name within our on-disk hash table
2698 PCHIdentifierLookupTable *IdTable
2699 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2700 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2701 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2702 if (Pos == IdTable->end())
2703 return 0;
2704
2705 // Dereferencing the iterator has the effect of building the
2706 // IdentifierInfo node and populating it with the various
2707 // declarations it needs.
2708 return *Pos;
2709}
2710
2711void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2712 assert(ID && "Non-zero identifier ID required");
2713 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2714}
2715
Chris Lattner29241862009-04-11 21:15:38 +00002716IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002717 if (ID == 0)
2718 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002719
Douglas Gregorc713da92009-04-21 22:25:48 +00002720 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002721 Error("No identifier table in PCH file");
2722 return 0;
2723 }
Chris Lattner29241862009-04-11 21:15:38 +00002724
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002725 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002726 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002727 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002728 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002729 }
Chris Lattner29241862009-04-11 21:15:38 +00002730
2731 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002732}
2733
Steve Naroff9e84d782009-04-23 10:39:46 +00002734Selector PCHReader::DecodeSelector(unsigned ID) {
2735 if (ID == 0)
2736 return Selector();
2737
2738 if (SelectorData.empty()) {
2739 Error("No selector table in PCH file");
2740 return Selector();
2741 }
2742
2743 if (ID > SelectorData.size()) {
2744 Error("Selector ID out of range");
2745 return Selector();
2746 }
2747 return SelectorData[ID-1];
2748}
2749
Douglas Gregorc34897d2009-04-09 22:27:44 +00002750DeclarationName
2751PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2752 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2753 switch (Kind) {
2754 case DeclarationName::Identifier:
2755 return DeclarationName(GetIdentifierInfo(Record, Idx));
2756
2757 case DeclarationName::ObjCZeroArgSelector:
2758 case DeclarationName::ObjCOneArgSelector:
2759 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002760 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002761
2762 case DeclarationName::CXXConstructorName:
2763 return Context.DeclarationNames.getCXXConstructorName(
2764 GetType(Record[Idx++]));
2765
2766 case DeclarationName::CXXDestructorName:
2767 return Context.DeclarationNames.getCXXDestructorName(
2768 GetType(Record[Idx++]));
2769
2770 case DeclarationName::CXXConversionFunctionName:
2771 return Context.DeclarationNames.getCXXConversionFunctionName(
2772 GetType(Record[Idx++]));
2773
2774 case DeclarationName::CXXOperatorName:
2775 return Context.DeclarationNames.getCXXOperatorName(
2776 (OverloadedOperatorKind)Record[Idx++]);
2777
2778 case DeclarationName::CXXUsingDirective:
2779 return DeclarationName::getUsingDirectiveName();
2780 }
2781
2782 // Required to silence GCC warning
2783 return DeclarationName();
2784}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002785
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002786/// \brief Read an integral value
2787llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2788 unsigned BitWidth = Record[Idx++];
2789 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2790 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2791 Idx += NumWords;
2792 return Result;
2793}
2794
2795/// \brief Read a signed integral value
2796llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2797 bool isUnsigned = Record[Idx++];
2798 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2799}
2800
Douglas Gregore2f37202009-04-14 21:55:33 +00002801/// \brief Read a floating-point value
2802llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002803 return llvm::APFloat(ReadAPInt(Record, Idx));
2804}
2805
Douglas Gregor1c507882009-04-15 21:30:51 +00002806// \brief Read a string
2807std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2808 unsigned Len = Record[Idx++];
2809 std::string Result(&Record[Idx], &Record[Idx] + Len);
2810 Idx += Len;
2811 return Result;
2812}
2813
2814/// \brief Reads attributes from the current stream position.
2815Attr *PCHReader::ReadAttributes() {
2816 unsigned Code = Stream.ReadCode();
2817 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2818 "Expected unabbreviated record"); (void)Code;
2819
2820 RecordData Record;
2821 unsigned Idx = 0;
2822 unsigned RecCode = Stream.ReadRecord(Code, Record);
2823 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2824 (void)RecCode;
2825
2826#define SIMPLE_ATTR(Name) \
2827 case Attr::Name: \
2828 New = ::new (Context) Name##Attr(); \
2829 break
2830
2831#define STRING_ATTR(Name) \
2832 case Attr::Name: \
2833 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2834 break
2835
2836#define UNSIGNED_ATTR(Name) \
2837 case Attr::Name: \
2838 New = ::new (Context) Name##Attr(Record[Idx++]); \
2839 break
2840
2841 Attr *Attrs = 0;
2842 while (Idx < Record.size()) {
2843 Attr *New = 0;
2844 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2845 bool IsInherited = Record[Idx++];
2846
2847 switch (Kind) {
2848 STRING_ATTR(Alias);
2849 UNSIGNED_ATTR(Aligned);
2850 SIMPLE_ATTR(AlwaysInline);
2851 SIMPLE_ATTR(AnalyzerNoReturn);
2852 STRING_ATTR(Annotate);
2853 STRING_ATTR(AsmLabel);
2854
2855 case Attr::Blocks:
2856 New = ::new (Context) BlocksAttr(
2857 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2858 break;
2859
2860 case Attr::Cleanup:
2861 New = ::new (Context) CleanupAttr(
2862 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2863 break;
2864
2865 SIMPLE_ATTR(Const);
2866 UNSIGNED_ATTR(Constructor);
2867 SIMPLE_ATTR(DLLExport);
2868 SIMPLE_ATTR(DLLImport);
2869 SIMPLE_ATTR(Deprecated);
2870 UNSIGNED_ATTR(Destructor);
2871 SIMPLE_ATTR(FastCall);
2872
2873 case Attr::Format: {
2874 std::string Type = ReadString(Record, Idx);
2875 unsigned FormatIdx = Record[Idx++];
2876 unsigned FirstArg = Record[Idx++];
2877 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2878 break;
2879 }
2880
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002881 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002882
2883 case Attr::IBOutletKind:
2884 New = ::new (Context) IBOutletAttr();
2885 break;
2886
2887 SIMPLE_ATTR(NoReturn);
2888 SIMPLE_ATTR(NoThrow);
2889 SIMPLE_ATTR(Nodebug);
2890 SIMPLE_ATTR(Noinline);
2891
2892 case Attr::NonNull: {
2893 unsigned Size = Record[Idx++];
2894 llvm::SmallVector<unsigned, 16> ArgNums;
2895 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2896 Idx += Size;
2897 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2898 break;
2899 }
2900
2901 SIMPLE_ATTR(ObjCException);
2902 SIMPLE_ATTR(ObjCNSObject);
2903 SIMPLE_ATTR(Overloadable);
2904 UNSIGNED_ATTR(Packed);
2905 SIMPLE_ATTR(Pure);
2906 UNSIGNED_ATTR(Regparm);
2907 STRING_ATTR(Section);
2908 SIMPLE_ATTR(StdCall);
2909 SIMPLE_ATTR(TransparentUnion);
2910 SIMPLE_ATTR(Unavailable);
2911 SIMPLE_ATTR(Unused);
2912 SIMPLE_ATTR(Used);
2913
2914 case Attr::Visibility:
2915 New = ::new (Context) VisibilityAttr(
2916 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2917 break;
2918
2919 SIMPLE_ATTR(WarnUnusedResult);
2920 SIMPLE_ATTR(Weak);
2921 SIMPLE_ATTR(WeakImport);
2922 }
2923
2924 assert(New && "Unable to decode attribute?");
2925 New->setInherited(IsInherited);
2926 New->setNext(Attrs);
2927 Attrs = New;
2928 }
2929#undef UNSIGNED_ATTR
2930#undef STRING_ATTR
2931#undef SIMPLE_ATTR
2932
2933 // The list of attributes was built backwards. Reverse the list
2934 // before returning it.
2935 Attr *PrevAttr = 0, *NextAttr = 0;
2936 while (Attrs) {
2937 NextAttr = Attrs->getNext();
2938 Attrs->setNext(PrevAttr);
2939 PrevAttr = Attrs;
2940 Attrs = NextAttr;
2941 }
2942
2943 return PrevAttr;
2944}
2945
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002946Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002947 // Within the bitstream, expressions are stored in Reverse Polish
2948 // Notation, with each of the subexpressions preceding the
2949 // expression they are stored in. To evaluate expressions, we
2950 // continue reading expressions and placing them on the stack, with
2951 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002952 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002953 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002954 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002955 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002956 llvm::SmallVector<Stmt *, 16> StmtStack;
2957 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002958 Stmt::EmptyShell Empty;
2959
Douglas Gregora151ba42009-04-14 23:32:43 +00002960 while (true) {
2961 unsigned Code = Stream.ReadCode();
2962 if (Code == llvm::bitc::END_BLOCK) {
2963 if (Stream.ReadBlockEnd()) {
2964 Error("Error at end of Source Manager block");
2965 return 0;
2966 }
2967 break;
2968 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002969
Douglas Gregora151ba42009-04-14 23:32:43 +00002970 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2971 // No known subblocks, always skip them.
2972 Stream.ReadSubBlockID();
2973 if (Stream.SkipBlock()) {
2974 Error("Malformed block record");
2975 return 0;
2976 }
2977 continue;
2978 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002979
Douglas Gregora151ba42009-04-14 23:32:43 +00002980 if (Code == llvm::bitc::DEFINE_ABBREV) {
2981 Stream.ReadAbbrevRecord();
2982 continue;
2983 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002984
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002985 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002986 Idx = 0;
2987 Record.clear();
2988 bool Finished = false;
2989 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002990 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002991 Finished = true;
2992 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002993
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002994 case pch::STMT_NULL_PTR:
2995 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002996 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002997
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002998 case pch::STMT_NULL:
2999 S = new (Context) NullStmt(Empty);
3000 break;
3001
3002 case pch::STMT_COMPOUND:
3003 S = new (Context) CompoundStmt(Empty);
3004 break;
3005
3006 case pch::STMT_CASE:
3007 S = new (Context) CaseStmt(Empty);
3008 break;
3009
3010 case pch::STMT_DEFAULT:
3011 S = new (Context) DefaultStmt(Empty);
3012 break;
3013
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003014 case pch::STMT_LABEL:
3015 S = new (Context) LabelStmt(Empty);
3016 break;
3017
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003018 case pch::STMT_IF:
3019 S = new (Context) IfStmt(Empty);
3020 break;
3021
3022 case pch::STMT_SWITCH:
3023 S = new (Context) SwitchStmt(Empty);
3024 break;
3025
Douglas Gregora6b503f2009-04-17 00:16:09 +00003026 case pch::STMT_WHILE:
3027 S = new (Context) WhileStmt(Empty);
3028 break;
3029
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003030 case pch::STMT_DO:
3031 S = new (Context) DoStmt(Empty);
3032 break;
3033
3034 case pch::STMT_FOR:
3035 S = new (Context) ForStmt(Empty);
3036 break;
3037
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003038 case pch::STMT_GOTO:
3039 S = new (Context) GotoStmt(Empty);
3040 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003041
3042 case pch::STMT_INDIRECT_GOTO:
3043 S = new (Context) IndirectGotoStmt(Empty);
3044 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003045
Douglas Gregora6b503f2009-04-17 00:16:09 +00003046 case pch::STMT_CONTINUE:
3047 S = new (Context) ContinueStmt(Empty);
3048 break;
3049
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003050 case pch::STMT_BREAK:
3051 S = new (Context) BreakStmt(Empty);
3052 break;
3053
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003054 case pch::STMT_RETURN:
3055 S = new (Context) ReturnStmt(Empty);
3056 break;
3057
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003058 case pch::STMT_DECL:
3059 S = new (Context) DeclStmt(Empty);
3060 break;
3061
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003062 case pch::STMT_ASM:
3063 S = new (Context) AsmStmt(Empty);
3064 break;
3065
Douglas Gregora151ba42009-04-14 23:32:43 +00003066 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003067 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003068 break;
3069
3070 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003071 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003072 break;
3073
3074 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003075 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003076 break;
3077
3078 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003079 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003080 break;
3081
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003082 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003083 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003084 break;
3085
Douglas Gregor596e0932009-04-15 16:35:07 +00003086 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003087 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003088 Record[PCHStmtReader::NumExprFields + 1]);
3089 break;
3090
Douglas Gregora151ba42009-04-14 23:32:43 +00003091 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003092 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003093 break;
3094
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003095 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003096 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003097 break;
3098
Douglas Gregor12d74052009-04-15 15:58:59 +00003099 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003100 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003101 break;
3102
3103 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003104 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003105 break;
3106
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003107 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003108 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003109 break;
3110
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003111 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003112 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003113 break;
3114
3115 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003116 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003117 break;
3118
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003119 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003120 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003121 break;
3122
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003123 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003124 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003125 break;
3126
3127 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003128 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003129 break;
3130
Douglas Gregora151ba42009-04-14 23:32:43 +00003131 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003132 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003133 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003134
3135 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003136 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003137 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003138
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003139 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003140 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003141 break;
3142
Douglas Gregorec0b8292009-04-15 23:02:49 +00003143 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003144 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003145 break;
3146
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003147 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003148 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003149 break;
3150
3151 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003152 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003153 Record[PCHStmtReader::NumExprFields] - 1);
3154
3155 break;
3156
3157 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003158 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003159 break;
3160
Douglas Gregorec0b8292009-04-15 23:02:49 +00003161 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003162 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003163 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003164
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003165 case pch::EXPR_ADDR_LABEL:
3166 S = new (Context) AddrLabelExpr(Empty);
3167 break;
3168
Douglas Gregoreca12f62009-04-17 19:05:30 +00003169 case pch::EXPR_STMT:
3170 S = new (Context) StmtExpr(Empty);
3171 break;
3172
Douglas Gregor209d4622009-04-15 23:33:31 +00003173 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003174 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003175 break;
3176
3177 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003178 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003179 break;
3180
3181 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003182 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003183 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003184
3185 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003186 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003187 break;
3188
Douglas Gregore246b742009-04-17 19:21:43 +00003189 case pch::EXPR_BLOCK:
3190 S = new (Context) BlockExpr(Empty);
3191 break;
3192
Douglas Gregor725e94b2009-04-16 00:01:45 +00003193 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003194 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003195 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003196
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003197 case pch::EXPR_OBJC_STRING_LITERAL:
3198 S = new (Context) ObjCStringLiteral(Empty);
3199 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003200 case pch::EXPR_OBJC_ENCODE:
3201 S = new (Context) ObjCEncodeExpr(Empty);
3202 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003203 case pch::EXPR_OBJC_SELECTOR_EXPR:
3204 S = new (Context) ObjCSelectorExpr(Empty);
3205 break;
3206 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3207 S = new (Context) ObjCProtocolExpr(Empty);
3208 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003209 }
3210
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003211 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003212 if (Finished)
3213 break;
3214
Douglas Gregor456e0952009-04-17 22:13:46 +00003215 ++NumStatementsRead;
3216
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003217 if (S) {
3218 unsigned NumSubStmts = Reader.Visit(S);
3219 while (NumSubStmts > 0) {
3220 StmtStack.pop_back();
3221 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003222 }
3223 }
3224
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003225 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003226 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003227 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003228 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003229 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003230 return StmtStack.back();
3231}
3232
3233Expr *PCHReader::ReadExpr() {
3234 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003235}
3236
Douglas Gregor179cfb12009-04-10 20:39:37 +00003237DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003238 return Diag(SourceLocation(), DiagID);
3239}
3240
3241DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3242 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003243 Context.getSourceManager()),
3244 DiagID);
3245}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003246
Douglas Gregorc713da92009-04-21 22:25:48 +00003247/// \brief Retrieve the identifier table associated with the
3248/// preprocessor.
3249IdentifierTable &PCHReader::getIdentifierTable() {
3250 return PP.getIdentifierTable();
3251}
3252
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003253/// \brief Record that the given ID maps to the given switch-case
3254/// statement.
3255void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3256 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3257 SwitchCaseStmts[ID] = SC;
3258}
3259
3260/// \brief Retrieve the switch-case statement with the given ID.
3261SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3262 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3263 return SwitchCaseStmts[ID];
3264}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003265
3266/// \brief Record that the given label statement has been
3267/// deserialized and has the given ID.
3268void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3269 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3270 "Deserialized label twice");
3271 LabelStmts[ID] = S;
3272
3273 // If we've already seen any goto statements that point to this
3274 // label, resolve them now.
3275 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3276 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3277 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3278 Goto->second->setLabel(S);
3279 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003280
3281 // If we've already seen any address-label statements that point to
3282 // this label, resolve them now.
3283 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3284 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3285 = UnresolvedAddrLabelExprs.equal_range(ID);
3286 for (AddrLabelIter AddrLabel = AddrLabels.first;
3287 AddrLabel != AddrLabels.second; ++AddrLabel)
3288 AddrLabel->second->setLabel(S);
3289 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003290}
3291
3292/// \brief Set the label of the given statement to the label
3293/// identified by ID.
3294///
3295/// Depending on the order in which the label and other statements
3296/// referencing that label occur, this operation may complete
3297/// immediately (updating the statement) or it may queue the
3298/// statement to be back-patched later.
3299void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3300 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3301 if (Label != LabelStmts.end()) {
3302 // We've already seen this label, so set the label of the goto and
3303 // we're done.
3304 S->setLabel(Label->second);
3305 } else {
3306 // We haven't seen this label yet, so add this goto to the set of
3307 // unresolved goto statements.
3308 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3309 }
3310}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003311
3312/// \brief Set the label of the given expression to the label
3313/// identified by ID.
3314///
3315/// Depending on the order in which the label and other statements
3316/// referencing that label occur, this operation may complete
3317/// immediately (updating the statement) or it may queue the
3318/// statement to be back-patched later.
3319void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3320 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3321 if (Label != LabelStmts.end()) {
3322 // We've already seen this label, so set the label of the
3323 // label-address expression and we're done.
3324 S->setLabel(Label->second);
3325 } else {
3326 // We haven't seen this label yet, so add this label-address
3327 // expression to the set of unresolved label-address expressions.
3328 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3329 }
3330}