blob: c3d4bcfa52990653f42325583f1c55c2088300cf [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++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff79ea0e02009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff7333b492009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000230 unsigned NumIvars = Record[Idx++];
231 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
232 IVars.reserve(NumIvars);
233 for (unsigned I = 0; I != NumIvars; ++I)
234 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
235 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
236
237 ID->setForwardDecl(Record[Idx++]);
238 ID->setImplicitInterfaceDecl(Record[Idx++]);
239 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
240 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000242 // FIXME: add protocols, categories.
243}
244
245void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
246 VisitFieldDecl(IVD);
247 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
248}
249
Steve Naroff97b53bd2009-04-21 15:12:33 +0000250void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
251 VisitObjCContainerDecl(PD);
252 PD->setForwardDecl(Record[Idx++]);
253 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
259 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
260}
261
262void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
263 VisitFieldDecl(FD);
264}
265
266void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
267 VisitDecl(CD);
268 unsigned NumClassRefs = Record[Idx++];
269 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
270 ClassRefs.reserve(NumClassRefs);
271 for (unsigned I = 0; I != NumClassRefs; ++I)
272 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
274}
275
276void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
277 VisitDecl(FPD);
278 unsigned NumProtoRefs = Record[Idx++];
279 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
280 ProtoRefs.reserve(NumProtoRefs);
281 for (unsigned I = 0; I != NumProtoRefs; ++I)
282 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
283 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
284}
285
286void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
287 VisitObjCContainerDecl(CD);
288 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
289 unsigned NumProtoRefs = Record[Idx++];
290 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
291 ProtoRefs.reserve(NumProtoRefs);
292 for (unsigned I = 0; I != NumProtoRefs; ++I)
293 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
295 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
296 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
297}
298
299void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
300 VisitNamedDecl(CAD);
301 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
302}
303
304void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
305 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000306 D->setType(Reader.GetType(Record[Idx++]));
307 // FIXME: stable encoding
308 D->setPropertyAttributes(
309 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
310 // FIXME: stable encoding
311 D->setPropertyImplementation(
312 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
313 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
314 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
315 D->setGetterMethodDecl(
316 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
317 D->setSetterMethodDecl(
318 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
319 D->setPropertyIvarDecl(
320 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000321}
322
323void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
324 VisitDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000325 D->setClassInterface(
326 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000328}
329
330void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
331 VisitObjCImplDecl(D);
332 // FIXME: Implement.
333}
334
335void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
336 VisitObjCImplDecl(D);
337 // FIXME: Implement.
338}
339
340
341void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
342 VisitDecl(D);
343 // FIXME: Implement.
344}
345
Douglas Gregor982365e2009-04-13 21:20:57 +0000346void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
347 VisitValueDecl(FD);
348 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000349 if (Record[Idx++])
350 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000351}
352
Douglas Gregorc34897d2009-04-09 22:27:44 +0000353void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
354 VisitValueDecl(VD);
355 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
356 VD->setThreadSpecified(Record[Idx++]);
357 VD->setCXXDirectInitializer(Record[Idx++]);
358 VD->setDeclaredInCondition(Record[Idx++]);
359 VD->setPreviousDeclaration(
360 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
361 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000364}
365
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000366void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
367 VisitVarDecl(PD);
368 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000369 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000370}
371
372void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
373 VisitParmVarDecl(PD);
374 PD->setOriginalType(Reader.GetType(Record[Idx++]));
375}
376
Douglas Gregor2a491792009-04-13 22:49:25 +0000377void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
378 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000379 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000380}
381
382void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
383 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000384 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000385 unsigned NumParams = Record[Idx++];
386 llvm::SmallVector<ParmVarDecl *, 16> Params;
387 Params.reserve(NumParams);
388 for (unsigned I = 0; I != NumParams; ++I)
389 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
390 BD->setParams(Reader.getContext(), &Params[0], NumParams);
391}
392
Douglas Gregorc34897d2009-04-09 22:27:44 +0000393std::pair<uint64_t, uint64_t>
394PCHDeclReader::VisitDeclContext(DeclContext *DC) {
395 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000396 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000397 return std::make_pair(LexicalOffset, VisibleOffset);
398}
399
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000400//===----------------------------------------------------------------------===//
401// Statement/expression deserialization
402//===----------------------------------------------------------------------===//
403namespace {
404 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000405 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000406 PCHReader &Reader;
407 const PCHReader::RecordData &Record;
408 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000409 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000410
411 public:
412 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000413 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
414 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000415
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000416 /// \brief The number of record fields required for the Stmt class
417 /// itself.
418 static const unsigned NumStmtFields = 0;
419
Douglas Gregor596e0932009-04-15 16:35:07 +0000420 /// \brief The number of record fields required for the Expr class
421 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000422 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000423
Douglas Gregora151ba42009-04-14 23:32:43 +0000424 // Each of the Visit* functions reads in part of the expression
425 // from the given record and the current expression stack, then
426 // return the total number of operands that it read from the
427 // expression stack.
428
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000429 unsigned VisitStmt(Stmt *S);
430 unsigned VisitNullStmt(NullStmt *S);
431 unsigned VisitCompoundStmt(CompoundStmt *S);
432 unsigned VisitSwitchCase(SwitchCase *S);
433 unsigned VisitCaseStmt(CaseStmt *S);
434 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000435 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000436 unsigned VisitIfStmt(IfStmt *S);
437 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000438 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000439 unsigned VisitDoStmt(DoStmt *S);
440 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000441 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000442 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000443 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000444 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000445 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000446 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000447 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000448 unsigned VisitExpr(Expr *E);
449 unsigned VisitPredefinedExpr(PredefinedExpr *E);
450 unsigned VisitDeclRefExpr(DeclRefExpr *E);
451 unsigned VisitIntegerLiteral(IntegerLiteral *E);
452 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000453 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000454 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000455 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000456 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000457 unsigned VisitUnaryOperator(UnaryOperator *E);
458 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000459 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000460 unsigned VisitCallExpr(CallExpr *E);
461 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000462 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000463 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000464 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
465 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000466 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000467 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
468 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000469 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000470 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000471 unsigned VisitInitListExpr(InitListExpr *E);
472 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
473 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000474 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000475 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000476 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000477 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
478 unsigned VisitChooseExpr(ChooseExpr *E);
479 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000480 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000481 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000482 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000483 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000484 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000485 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
486 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000487 };
488}
489
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000490unsigned PCHStmtReader::VisitStmt(Stmt *S) {
491 assert(Idx == NumStmtFields && "Incorrect statement field count");
492 return 0;
493}
494
495unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
496 VisitStmt(S);
497 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
498 return 0;
499}
500
501unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
502 VisitStmt(S);
503 unsigned NumStmts = Record[Idx++];
504 S->setStmts(Reader.getContext(),
505 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
506 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
507 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 return NumStmts;
509}
510
511unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
512 VisitStmt(S);
513 Reader.RecordSwitchCaseID(S, Record[Idx++]);
514 return 0;
515}
516
517unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
518 VisitSwitchCase(S);
519 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
520 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
521 S->setSubStmt(StmtStack.back());
522 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
523 return 3;
524}
525
526unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
527 VisitSwitchCase(S);
528 S->setSubStmt(StmtStack.back());
529 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
530 return 1;
531}
532
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000533unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
534 VisitStmt(S);
535 S->setID(Reader.GetIdentifierInfo(Record, Idx));
536 S->setSubStmt(StmtStack.back());
537 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
538 Reader.RecordLabelStmt(S, Record[Idx++]);
539 return 1;
540}
541
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000542unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
543 VisitStmt(S);
544 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
545 S->setThen(StmtStack[StmtStack.size() - 2]);
546 S->setElse(StmtStack[StmtStack.size() - 1]);
547 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 return 3;
549}
550
551unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
552 VisitStmt(S);
553 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
554 S->setBody(StmtStack.back());
555 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 SwitchCase *PrevSC = 0;
557 for (unsigned N = Record.size(); Idx != N; ++Idx) {
558 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
559 if (PrevSC)
560 PrevSC->setNextSwitchCase(SC);
561 else
562 S->setSwitchCaseList(SC);
563 PrevSC = SC;
564 }
565 return 2;
566}
567
Douglas Gregora6b503f2009-04-17 00:16:09 +0000568unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
569 VisitStmt(S);
570 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
571 S->setBody(StmtStack.back());
572 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
573 return 2;
574}
575
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000576unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
577 VisitStmt(S);
578 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
579 S->setBody(StmtStack.back());
580 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 return 2;
582}
583
584unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
585 VisitStmt(S);
586 S->setInit(StmtStack[StmtStack.size() - 4]);
587 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
588 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
589 S->setBody(StmtStack.back());
590 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
591 return 4;
592}
593
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000594unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
595 VisitStmt(S);
596 Reader.SetLabelOf(S, Record[Idx++]);
597 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
599 return 0;
600}
601
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000602unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
603 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000604 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000605 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
606 return 1;
607}
608
Douglas Gregora6b503f2009-04-17 00:16:09 +0000609unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
610 VisitStmt(S);
611 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
612 return 0;
613}
614
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000615unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
616 VisitStmt(S);
617 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
618 return 0;
619}
620
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000621unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
622 VisitStmt(S);
623 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
624 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
625 return 1;
626}
627
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000628unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
629 VisitStmt(S);
630 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
631 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
632
633 if (Idx + 1 == Record.size()) {
634 // Single declaration
635 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
636 } else {
637 llvm::SmallVector<Decl *, 16> Decls;
638 Decls.reserve(Record.size() - Idx);
639 for (unsigned N = Record.size(); Idx != N; ++Idx)
640 Decls.push_back(Reader.GetDecl(Record[Idx]));
641 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
642 &Decls[0], Decls.size())));
643 }
644 return 0;
645}
646
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000647unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
648 VisitStmt(S);
649 unsigned NumOutputs = Record[Idx++];
650 unsigned NumInputs = Record[Idx++];
651 unsigned NumClobbers = Record[Idx++];
652 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
653 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
654 S->setVolatile(Record[Idx++]);
655 S->setSimple(Record[Idx++]);
656
657 unsigned StackIdx
658 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
659 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
660
661 // Outputs and inputs
662 llvm::SmallVector<std::string, 16> Names;
663 llvm::SmallVector<StringLiteral*, 16> Constraints;
664 llvm::SmallVector<Stmt*, 16> Exprs;
665 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
666 Names.push_back(Reader.ReadString(Record, Idx));
667 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
668 Exprs.push_back(StmtStack[StackIdx++]);
669 }
670 S->setOutputsAndInputs(NumOutputs, NumInputs,
671 &Names[0], &Constraints[0], &Exprs[0]);
672
673 // Constraints
674 llvm::SmallVector<StringLiteral*, 16> Clobbers;
675 for (unsigned I = 0; I != NumClobbers; ++I)
676 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
677 S->setClobbers(&Clobbers[0], NumClobbers);
678
679 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
680 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
681}
682
Douglas Gregora151ba42009-04-14 23:32:43 +0000683unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000684 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000685 E->setType(Reader.GetType(Record[Idx++]));
686 E->setTypeDependent(Record[Idx++]);
687 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000688 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000689 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000690}
691
Douglas Gregora151ba42009-04-14 23:32:43 +0000692unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000693 VisitExpr(E);
694 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
695 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000696 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000697}
698
Douglas Gregora151ba42009-04-14 23:32:43 +0000699unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000700 VisitExpr(E);
701 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
702 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000703 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000704}
705
Douglas Gregora151ba42009-04-14 23:32:43 +0000706unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000707 VisitExpr(E);
708 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
709 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000710 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000711}
712
Douglas Gregora151ba42009-04-14 23:32:43 +0000713unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000714 VisitExpr(E);
715 E->setValue(Reader.ReadAPFloat(Record, Idx));
716 E->setExact(Record[Idx++]);
717 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000718 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000719}
720
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000721unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
722 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000723 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000724 return 1;
725}
726
Douglas Gregor596e0932009-04-15 16:35:07 +0000727unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
728 VisitExpr(E);
729 unsigned Len = Record[Idx++];
730 assert(Record[Idx] == E->getNumConcatenated() &&
731 "Wrong number of concatenated tokens!");
732 ++Idx;
733 E->setWide(Record[Idx++]);
734
735 // Read string data
736 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
737 E->setStrData(Reader.getContext(), &Str[0], Len);
738 Idx += Len;
739
740 // Read source locations
741 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
742 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
743
744 return 0;
745}
746
Douglas Gregora151ba42009-04-14 23:32:43 +0000747unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000748 VisitExpr(E);
749 E->setValue(Record[Idx++]);
750 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
751 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000752 return 0;
753}
754
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000755unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
756 VisitExpr(E);
757 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
758 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000759 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000760 return 1;
761}
762
Douglas Gregor12d74052009-04-15 15:58:59 +0000763unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
764 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000765 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000766 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
767 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 return 1;
769}
770
771unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
772 VisitExpr(E);
773 E->setSizeof(Record[Idx++]);
774 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000775 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000776 ++Idx;
777 } else {
778 E->setArgument(Reader.GetType(Record[Idx++]));
779 }
780 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
782 return E->isArgumentType()? 0 : 1;
783}
784
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000785unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
786 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000787 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
788 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000789 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
790 return 2;
791}
792
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000793unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
794 VisitExpr(E);
795 E->setNumArgs(Reader.getContext(), Record[Idx++]);
796 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000797 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000798 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000799 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000800 return E->getNumArgs() + 1;
801}
802
803unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
804 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000805 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000806 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
807 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
808 E->setArrow(Record[Idx++]);
809 return 1;
810}
811
Douglas Gregora151ba42009-04-14 23:32:43 +0000812unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
813 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000814 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000815 return 1;
816}
817
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000818unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
819 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000820 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
821 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000822 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
823 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
824 return 2;
825}
826
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000827unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
828 VisitBinaryOperator(E);
829 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
830 E->setComputationResultType(Reader.GetType(Record[Idx++]));
831 return 2;
832}
833
834unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
835 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000836 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
837 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
838 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000839 return 3;
840}
841
Douglas Gregora151ba42009-04-14 23:32:43 +0000842unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
843 VisitCastExpr(E);
844 E->setLvalueCast(Record[Idx++]);
845 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000846}
847
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000848unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
849 VisitCastExpr(E);
850 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
851 return 1;
852}
853
854unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
855 VisitExplicitCastExpr(E);
856 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
857 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
858 return 1;
859}
860
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000861unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
862 VisitExpr(E);
863 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000864 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000865 E->setFileScope(Record[Idx++]);
866 return 1;
867}
868
Douglas Gregorec0b8292009-04-15 23:02:49 +0000869unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
870 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000871 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000872 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
873 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
874 return 1;
875}
876
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000877unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
878 VisitExpr(E);
879 unsigned NumInits = Record[Idx++];
880 E->reserveInits(NumInits);
881 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000882 E->updateInit(I,
883 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
884 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000885 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
886 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
887 E->setInitializedFieldInUnion(
888 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
889 E->sawArrayRangeDesignator(Record[Idx++]);
890 return NumInits + 1;
891}
892
893unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
894 typedef DesignatedInitExpr::Designator Designator;
895
896 VisitExpr(E);
897 unsigned NumSubExprs = Record[Idx++];
898 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
899 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000900 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000901 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
902 E->setGNUSyntax(Record[Idx++]);
903
904 llvm::SmallVector<Designator, 4> Designators;
905 while (Idx < Record.size()) {
906 switch ((pch::DesignatorTypes)Record[Idx++]) {
907 case pch::DESIG_FIELD_DECL: {
908 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
909 SourceLocation DotLoc
910 = SourceLocation::getFromRawEncoding(Record[Idx++]);
911 SourceLocation FieldLoc
912 = SourceLocation::getFromRawEncoding(Record[Idx++]);
913 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
914 FieldLoc));
915 Designators.back().setField(Field);
916 break;
917 }
918
919 case pch::DESIG_FIELD_NAME: {
920 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
921 SourceLocation DotLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 SourceLocation FieldLoc
924 = SourceLocation::getFromRawEncoding(Record[Idx++]);
925 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
926 break;
927 }
928
929 case pch::DESIG_ARRAY: {
930 unsigned Index = Record[Idx++];
931 SourceLocation LBracketLoc
932 = SourceLocation::getFromRawEncoding(Record[Idx++]);
933 SourceLocation RBracketLoc
934 = SourceLocation::getFromRawEncoding(Record[Idx++]);
935 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
936 break;
937 }
938
939 case pch::DESIG_ARRAY_RANGE: {
940 unsigned Index = Record[Idx++];
941 SourceLocation LBracketLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 SourceLocation EllipsisLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 SourceLocation RBracketLoc
946 = SourceLocation::getFromRawEncoding(Record[Idx++]);
947 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
948 RBracketLoc));
949 break;
950 }
951 }
952 }
953 E->setDesignators(&Designators[0], Designators.size());
954
955 return NumSubExprs;
956}
957
958unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
959 VisitExpr(E);
960 return 0;
961}
962
Douglas Gregorec0b8292009-04-15 23:02:49 +0000963unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
964 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000965 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000966 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
967 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
968 return 1;
969}
970
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000971unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
972 VisitExpr(E);
973 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
974 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
975 Reader.SetLabelOf(E, Record[Idx++]);
976 return 0;
977}
978
Douglas Gregoreca12f62009-04-17 19:05:30 +0000979unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
980 VisitExpr(E);
981 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
982 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
983 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
984 return 1;
985}
986
Douglas Gregor209d4622009-04-15 23:33:31 +0000987unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
988 VisitExpr(E);
989 E->setArgType1(Reader.GetType(Record[Idx++]));
990 E->setArgType2(Reader.GetType(Record[Idx++]));
991 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 return 0;
994}
995
996unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
997 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000998 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
999 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1000 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001001 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1002 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 return 3;
1004}
1005
1006unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1007 VisitExpr(E);
1008 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1009 return 0;
1010}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001011
Douglas Gregor725e94b2009-04-16 00:01:45 +00001012unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1013 VisitExpr(E);
1014 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001015 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001016 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1017 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1018 return NumExprs;
1019}
1020
Douglas Gregore246b742009-04-17 19:21:43 +00001021unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1022 VisitExpr(E);
1023 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1024 E->setHasBlockDeclRefExprs(Record[Idx++]);
1025 return 0;
1026}
1027
Douglas Gregor725e94b2009-04-16 00:01:45 +00001028unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1029 VisitExpr(E);
1030 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1031 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1032 E->setByRef(Record[Idx++]);
1033 return 0;
1034}
1035
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001036//===----------------------------------------------------------------------===//
1037// Objective-C Expressions and Statements
1038
1039unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1040 VisitExpr(E);
1041 E->setString(cast<StringLiteral>(StmtStack.back()));
1042 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1043 return 1;
1044}
1045
Chris Lattner80f83c62009-04-22 05:57:30 +00001046unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1047 VisitExpr(E);
1048 E->setEncodedType(Reader.GetType(Record[Idx++]));
1049 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1050 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1051 return 0;
1052}
1053
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001054unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1055 VisitExpr(E);
1056 // FIXME: Selectors.
1057 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1058 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1059 return 0;
1060}
1061
1062unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1063 VisitExpr(E);
1064 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1065 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1066 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1067 return 0;
1068}
1069
Chris Lattner80f83c62009-04-22 05:57:30 +00001070
Douglas Gregorc713da92009-04-21 22:25:48 +00001071//===----------------------------------------------------------------------===//
1072// PCH reader implementation
1073//===----------------------------------------------------------------------===//
1074
1075namespace {
1076class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1077 PCHReader &Reader;
1078
1079 // If we know the IdentifierInfo in advance, it is here and we will
1080 // not build a new one. Used when deserializing information about an
1081 // identifier that was constructed before the PCH file was read.
1082 IdentifierInfo *KnownII;
1083
1084public:
1085 typedef IdentifierInfo * data_type;
1086
1087 typedef const std::pair<const char*, unsigned> external_key_type;
1088
1089 typedef external_key_type internal_key_type;
1090
1091 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1092 : Reader(Reader), KnownII(II) { }
1093
1094 static bool EqualKey(const internal_key_type& a,
1095 const internal_key_type& b) {
1096 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1097 : false;
1098 }
1099
1100 static unsigned ComputeHash(const internal_key_type& a) {
1101 return BernsteinHash(a.first, a.second);
1102 }
1103
1104 // This hopefully will just get inlined and removed by the optimizer.
1105 static const internal_key_type&
1106 GetInternalKey(const external_key_type& x) { return x; }
1107
1108 static std::pair<unsigned, unsigned>
1109 ReadKeyDataLength(const unsigned char*& d) {
1110 using namespace clang::io;
1111 unsigned KeyLen = ReadUnalignedLE16(d);
1112 unsigned DataLen = ReadUnalignedLE16(d);
1113 return std::make_pair(KeyLen, DataLen);
1114 }
1115
1116 static std::pair<const char*, unsigned>
1117 ReadKey(const unsigned char* d, unsigned n) {
1118 assert(n >= 2 && d[n-1] == '\0');
1119 return std::make_pair((const char*) d, n-1);
1120 }
1121
1122 IdentifierInfo *ReadData(const internal_key_type& k,
1123 const unsigned char* d,
1124 unsigned DataLen) {
1125 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001126 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001127 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1128 Bits >>= 1;
1129 bool Poisoned = Bits & 0x01;
1130 Bits >>= 1;
1131 bool ExtensionToken = Bits & 0x01;
1132 Bits >>= 1;
1133 bool hasMacroDefinition = Bits & 0x01;
1134 Bits >>= 1;
1135 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1136 Bits >>= 10;
1137 unsigned TokenID = Bits & 0xFF;
1138 Bits >>= 8;
1139
Douglas Gregorc713da92009-04-21 22:25:48 +00001140 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001141 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001142 DataLen -= 8;
1143
1144 // Build the IdentifierInfo itself and link the identifier ID with
1145 // the new IdentifierInfo.
1146 IdentifierInfo *II = KnownII;
1147 if (!II)
1148 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1149 k.first, k.first + k.second);
1150 Reader.SetIdentifierInfo(ID, II);
1151
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001152 // Set or check the various bits in the IdentifierInfo structure.
1153 // FIXME: Load token IDs lazily, too?
1154 assert((unsigned)II->getTokenID() == TokenID &&
1155 "Incorrect token ID loaded");
1156 (void)TokenID;
1157 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1158 assert(II->isExtensionToken() == ExtensionToken &&
1159 "Incorrect extension token flag");
1160 (void)ExtensionToken;
1161 II->setIsPoisoned(Poisoned);
1162 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1163 "Incorrect C++ operator keyword flag");
1164 (void)CPlusPlusOperatorKeyword;
1165
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001166 // If this identifier is a macro, deserialize the macro
1167 // definition.
1168 if (hasMacroDefinition) {
1169 uint32_t Offset = ReadUnalignedLE64(d);
1170 Reader.ReadMacroRecord(Offset);
1171 DataLen -= 8;
1172 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001173
1174 // Read all of the declarations visible at global scope with this
1175 // name.
1176 Sema *SemaObj = Reader.getSema();
1177 while (DataLen > 0) {
1178 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001179 if (SemaObj) {
1180 // Introduce this declaration into the translation-unit scope
1181 // and add it to the declaration chain for this identifier, so
1182 // that (unqualified) name lookup will find it.
1183 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1184 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1185 } else {
1186 // Queue this declaration so that it will be added to the
1187 // translation unit scope and identifier's declaration chain
1188 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001189 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001190 }
1191
1192 DataLen -= 4;
1193 }
1194 return II;
1195 }
1196};
1197
1198} // end anonymous namespace
1199
1200/// \brief The on-disk hash table used to contain information about
1201/// all of the identifiers in the program.
1202typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1203 PCHIdentifierLookupTable;
1204
Douglas Gregorc34897d2009-04-09 22:27:44 +00001205// FIXME: use the diagnostics machinery
1206static bool Error(const char *Str) {
1207 std::fprintf(stderr, "%s\n", Str);
1208 return true;
1209}
1210
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001211/// \brief Check the contents of the predefines buffer against the
1212/// contents of the predefines buffer used to build the PCH file.
1213///
1214/// The contents of the two predefines buffers should be the same. If
1215/// not, then some command-line option changed the preprocessor state
1216/// and we must reject the PCH file.
1217///
1218/// \param PCHPredef The start of the predefines buffer in the PCH
1219/// file.
1220///
1221/// \param PCHPredefLen The length of the predefines buffer in the PCH
1222/// file.
1223///
1224/// \param PCHBufferID The FileID for the PCH predefines buffer.
1225///
1226/// \returns true if there was a mismatch (in which case the PCH file
1227/// should be ignored), or false otherwise.
1228bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1229 unsigned PCHPredefLen,
1230 FileID PCHBufferID) {
1231 const char *Predef = PP.getPredefines().c_str();
1232 unsigned PredefLen = PP.getPredefines().size();
1233
1234 // If the two predefines buffers compare equal, we're done!.
1235 if (PredefLen == PCHPredefLen &&
1236 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1237 return false;
1238
1239 // The predefines buffers are different. Produce a reasonable
1240 // diagnostic showing where they are different.
1241
1242 // The source locations (potentially in the two different predefines
1243 // buffers)
1244 SourceLocation Loc1, Loc2;
1245 SourceManager &SourceMgr = PP.getSourceManager();
1246
1247 // Create a source buffer for our predefines string, so
1248 // that we can build a diagnostic that points into that
1249 // source buffer.
1250 FileID BufferID;
1251 if (Predef && Predef[0]) {
1252 llvm::MemoryBuffer *Buffer
1253 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1254 "<built-in>");
1255 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1256 }
1257
1258 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1259 std::pair<const char *, const char *> Locations
1260 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1261
1262 if (Locations.first != Predef + MinLen) {
1263 // We found the location in the two buffers where there is a
1264 // difference. Form source locations to point there (in both
1265 // buffers).
1266 unsigned Offset = Locations.first - Predef;
1267 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1268 .getFileLocWithOffset(Offset);
1269 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1270 .getFileLocWithOffset(Offset);
1271 } else if (PredefLen > PCHPredefLen) {
1272 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1273 .getFileLocWithOffset(MinLen);
1274 } else {
1275 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1276 .getFileLocWithOffset(MinLen);
1277 }
1278
1279 Diag(Loc1, diag::warn_pch_preprocessor);
1280 if (Loc2.isValid())
1281 Diag(Loc2, diag::note_predef_in_pch);
1282 Diag(diag::note_ignoring_pch) << FileName;
1283 return true;
1284}
1285
Douglas Gregor635f97f2009-04-13 16:31:14 +00001286/// \brief Read the line table in the source manager block.
1287/// \returns true if ther was an error.
1288static bool ParseLineTable(SourceManager &SourceMgr,
1289 llvm::SmallVectorImpl<uint64_t> &Record) {
1290 unsigned Idx = 0;
1291 LineTableInfo &LineTable = SourceMgr.getLineTable();
1292
1293 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001294 std::map<int, int> FileIDs;
1295 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001296 // Extract the file name
1297 unsigned FilenameLen = Record[Idx++];
1298 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1299 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001300 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1301 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001302 }
1303
1304 // Parse the line entries
1305 std::vector<LineEntry> Entries;
1306 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001307 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001308
1309 // Extract the line entries
1310 unsigned NumEntries = Record[Idx++];
1311 Entries.clear();
1312 Entries.reserve(NumEntries);
1313 for (unsigned I = 0; I != NumEntries; ++I) {
1314 unsigned FileOffset = Record[Idx++];
1315 unsigned LineNo = Record[Idx++];
1316 int FilenameID = Record[Idx++];
1317 SrcMgr::CharacteristicKind FileKind
1318 = (SrcMgr::CharacteristicKind)Record[Idx++];
1319 unsigned IncludeOffset = Record[Idx++];
1320 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1321 FileKind, IncludeOffset));
1322 }
1323 LineTable.AddEntry(FID, Entries);
1324 }
1325
1326 return false;
1327}
1328
Douglas Gregorab1cef72009-04-10 03:52:48 +00001329/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001330PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001331 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001332 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1333 Error("Malformed source manager block record");
1334 return Failure;
1335 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001336
1337 SourceManager &SourceMgr = Context.getSourceManager();
1338 RecordData Record;
1339 while (true) {
1340 unsigned Code = Stream.ReadCode();
1341 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001342 if (Stream.ReadBlockEnd()) {
1343 Error("Error at end of Source Manager block");
1344 return Failure;
1345 }
1346
1347 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001348 }
1349
1350 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1351 // No known subblocks, always skip them.
1352 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001353 if (Stream.SkipBlock()) {
1354 Error("Malformed block record");
1355 return Failure;
1356 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001357 continue;
1358 }
1359
1360 if (Code == llvm::bitc::DEFINE_ABBREV) {
1361 Stream.ReadAbbrevRecord();
1362 continue;
1363 }
1364
1365 // Read a record.
1366 const char *BlobStart;
1367 unsigned BlobLen;
1368 Record.clear();
1369 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1370 default: // Default behavior: ignore.
1371 break;
1372
1373 case pch::SM_SLOC_FILE_ENTRY: {
1374 // FIXME: We would really like to delay the creation of this
1375 // FileEntry until it is actually required, e.g., when producing
1376 // a diagnostic with a source location in this file.
1377 const FileEntry *File
1378 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1379 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001380 FileID ID = SourceMgr.createFileID(File,
1381 SourceLocation::getFromRawEncoding(Record[1]),
1382 (CharacteristicKind)Record[2]);
1383 if (Record[3])
1384 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1385 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001386 break;
1387 }
1388
1389 case pch::SM_SLOC_BUFFER_ENTRY: {
1390 const char *Name = BlobStart;
1391 unsigned Code = Stream.ReadCode();
1392 Record.clear();
1393 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1394 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001395 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001396 llvm::MemoryBuffer *Buffer
1397 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1398 BlobStart + BlobLen - 1,
1399 Name);
1400 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1401
1402 if (strcmp(Name, "<built-in>") == 0
1403 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1404 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001405 break;
1406 }
1407
1408 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1409 SourceLocation SpellingLoc
1410 = SourceLocation::getFromRawEncoding(Record[1]);
1411 SourceMgr.createInstantiationLoc(
1412 SpellingLoc,
1413 SourceLocation::getFromRawEncoding(Record[2]),
1414 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001415 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001416 break;
1417 }
1418
Chris Lattnere1be6022009-04-14 23:22:57 +00001419 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001420 if (ParseLineTable(SourceMgr, Record))
1421 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001422 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001423 }
1424 }
1425}
1426
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001427void PCHReader::ReadMacroRecord(uint64_t Offset) {
1428 // Keep track of where we are in the stream, then jump back there
1429 // after reading this macro.
1430 SavedStreamPosition SavedPosition(Stream);
1431
1432 Stream.JumpToBit(Offset);
1433 RecordData Record;
1434 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1435 MacroInfo *Macro = 0;
1436 while (true) {
1437 unsigned Code = Stream.ReadCode();
1438 switch (Code) {
1439 case llvm::bitc::END_BLOCK:
1440 return;
1441
1442 case llvm::bitc::ENTER_SUBBLOCK:
1443 // No known subblocks, always skip them.
1444 Stream.ReadSubBlockID();
1445 if (Stream.SkipBlock()) {
1446 Error("Malformed block record");
1447 return;
1448 }
1449 continue;
1450
1451 case llvm::bitc::DEFINE_ABBREV:
1452 Stream.ReadAbbrevRecord();
1453 continue;
1454 default: break;
1455 }
1456
1457 // Read a record.
1458 Record.clear();
1459 pch::PreprocessorRecordTypes RecType =
1460 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1461 switch (RecType) {
1462 case pch::PP_COUNTER_VALUE:
1463 // Skip this record.
1464 break;
1465
1466 case pch::PP_MACRO_OBJECT_LIKE:
1467 case pch::PP_MACRO_FUNCTION_LIKE: {
1468 // If we already have a macro, that means that we've hit the end
1469 // of the definition of the macro we were looking for. We're
1470 // done.
1471 if (Macro)
1472 return;
1473
1474 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1475 if (II == 0) {
1476 Error("Macro must have a name");
1477 return;
1478 }
1479 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1480 bool isUsed = Record[2];
1481
1482 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1483 MI->setIsUsed(isUsed);
1484
1485 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1486 // Decode function-like macro info.
1487 bool isC99VarArgs = Record[3];
1488 bool isGNUVarArgs = Record[4];
1489 MacroArgs.clear();
1490 unsigned NumArgs = Record[5];
1491 for (unsigned i = 0; i != NumArgs; ++i)
1492 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1493
1494 // Install function-like macro info.
1495 MI->setIsFunctionLike();
1496 if (isC99VarArgs) MI->setIsC99Varargs();
1497 if (isGNUVarArgs) MI->setIsGNUVarargs();
1498 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1499 PP.getPreprocessorAllocator());
1500 }
1501
1502 // Finally, install the macro.
1503 PP.setMacroInfo(II, MI);
1504
1505 // Remember that we saw this macro last so that we add the tokens that
1506 // form its body to it.
1507 Macro = MI;
1508 ++NumMacrosRead;
1509 break;
1510 }
1511
1512 case pch::PP_TOKEN: {
1513 // If we see a TOKEN before a PP_MACRO_*, then the file is
1514 // erroneous, just pretend we didn't see this.
1515 if (Macro == 0) break;
1516
1517 Token Tok;
1518 Tok.startToken();
1519 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1520 Tok.setLength(Record[1]);
1521 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1522 Tok.setIdentifierInfo(II);
1523 Tok.setKind((tok::TokenKind)Record[3]);
1524 Tok.setFlag((Token::TokenFlags)Record[4]);
1525 Macro->AddTokenToBody(Tok);
1526 break;
1527 }
1528 }
1529 }
1530}
1531
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001532bool PCHReader::ReadPreprocessorBlock() {
1533 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1534 return Error("Malformed preprocessor block record");
1535
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001536 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001537 while (true) {
1538 unsigned Code = Stream.ReadCode();
1539 switch (Code) {
1540 case llvm::bitc::END_BLOCK:
1541 if (Stream.ReadBlockEnd())
1542 return Error("Error at end of preprocessor block");
1543 return false;
1544
1545 case llvm::bitc::ENTER_SUBBLOCK:
1546 // No known subblocks, always skip them.
1547 Stream.ReadSubBlockID();
1548 if (Stream.SkipBlock())
1549 return Error("Malformed block record");
1550 continue;
1551
1552 case llvm::bitc::DEFINE_ABBREV:
1553 Stream.ReadAbbrevRecord();
1554 continue;
1555 default: break;
1556 }
1557
1558 // Read a record.
1559 Record.clear();
1560 pch::PreprocessorRecordTypes RecType =
1561 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1562 switch (RecType) {
1563 default: // Default behavior: ignore unknown records.
1564 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001565 case pch::PP_COUNTER_VALUE:
1566 if (!Record.empty())
1567 PP.setCounterValue(Record[0]);
1568 break;
1569
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001570 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001571 case pch::PP_MACRO_FUNCTION_LIKE:
1572 case pch::PP_TOKEN:
1573 // Once we've hit a macro definition or a token, we're done.
1574 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001575 }
1576 }
1577}
1578
Douglas Gregorc713da92009-04-21 22:25:48 +00001579PCHReader::PCHReadResult
1580PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001581 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1582 Error("Malformed block record");
1583 return Failure;
1584 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001585
1586 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001587 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001588 while (!Stream.AtEndOfStream()) {
1589 unsigned Code = Stream.ReadCode();
1590 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001591 if (Stream.ReadBlockEnd()) {
1592 Error("Error at end of module block");
1593 return Failure;
1594 }
Chris Lattner29241862009-04-11 21:15:38 +00001595
Douglas Gregor179cfb12009-04-10 20:39:37 +00001596 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001597 }
1598
1599 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1600 switch (Stream.ReadSubBlockID()) {
1601 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1602 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1603 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001604 if (Stream.SkipBlock()) {
1605 Error("Malformed block record");
1606 return Failure;
1607 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001608 break;
1609
Chris Lattner29241862009-04-11 21:15:38 +00001610 case pch::PREPROCESSOR_BLOCK_ID:
1611 // Skip the preprocessor block for now, but remember where it is. We
1612 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001613 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001614 Error("Multiple preprocessor blocks found.");
1615 return Failure;
1616 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001617 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001618 if (Stream.SkipBlock()) {
1619 Error("Malformed block record");
1620 return Failure;
1621 }
1622 break;
1623
Douglas Gregorab1cef72009-04-10 03:52:48 +00001624 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001625 switch (ReadSourceManagerBlock()) {
1626 case Success:
1627 break;
1628
1629 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001630 Error("Malformed source manager block");
1631 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001632
1633 case IgnorePCH:
1634 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001635 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001636 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001637 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001638 continue;
1639 }
1640
1641 if (Code == llvm::bitc::DEFINE_ABBREV) {
1642 Stream.ReadAbbrevRecord();
1643 continue;
1644 }
1645
1646 // Read and process a record.
1647 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001648 const char *BlobStart = 0;
1649 unsigned BlobLen = 0;
1650 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1651 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001652 default: // Default behavior: ignore.
1653 break;
1654
1655 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001656 if (!TypeOffsets.empty()) {
1657 Error("Duplicate TYPE_OFFSET record in PCH file");
1658 return Failure;
1659 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001660 TypeOffsets.swap(Record);
1661 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1662 break;
1663
1664 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001665 if (!DeclOffsets.empty()) {
1666 Error("Duplicate DECL_OFFSET record in PCH file");
1667 return Failure;
1668 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001669 DeclOffsets.swap(Record);
1670 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1671 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001672
1673 case pch::LANGUAGE_OPTIONS:
1674 if (ParseLanguageOptions(Record))
1675 return IgnorePCH;
1676 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001677
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001678 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001679 std::string TargetTriple(BlobStart, BlobLen);
1680 if (TargetTriple != Context.Target.getTargetTriple()) {
1681 Diag(diag::warn_pch_target_triple)
1682 << TargetTriple << Context.Target.getTargetTriple();
1683 Diag(diag::note_ignoring_pch) << FileName;
1684 return IgnorePCH;
1685 }
1686 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001688
1689 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001690 IdentifierTableData = BlobStart;
1691 IdentifierLookupTable
1692 = PCHIdentifierLookupTable::Create(
1693 (const unsigned char *)IdentifierTableData + Record[0],
1694 (const unsigned char *)IdentifierTableData,
1695 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001696 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001697 break;
1698
1699 case pch::IDENTIFIER_OFFSET:
1700 if (!IdentifierData.empty()) {
1701 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1702 return Failure;
1703 }
1704 IdentifierData.swap(Record);
1705#ifndef NDEBUG
1706 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1707 if ((IdentifierData[I] & 0x01) == 0) {
1708 Error("Malformed identifier table in the precompiled header");
1709 return Failure;
1710 }
1711 }
1712#endif
1713 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001714
1715 case pch::EXTERNAL_DEFINITIONS:
1716 if (!ExternalDefinitions.empty()) {
1717 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1718 return Failure;
1719 }
1720 ExternalDefinitions.swap(Record);
1721 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001722
Douglas Gregore01ad442009-04-18 05:55:16 +00001723 case pch::SPECIAL_TYPES:
1724 SpecialTypes.swap(Record);
1725 break;
1726
Douglas Gregor456e0952009-04-17 22:13:46 +00001727 case pch::STATISTICS:
1728 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001729 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001730 TotalLexicalDeclContexts = Record[2];
1731 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001732 break;
1733
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001734 case pch::TENTATIVE_DEFINITIONS:
1735 if (!TentativeDefinitions.empty()) {
1736 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1737 return Failure;
1738 }
1739 TentativeDefinitions.swap(Record);
1740 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001741
1742 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1743 if (!LocallyScopedExternalDecls.empty()) {
1744 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1745 return Failure;
1746 }
1747 LocallyScopedExternalDecls.swap(Record);
1748 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001749 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001750 }
1751
Douglas Gregor179cfb12009-04-10 20:39:37 +00001752 Error("Premature end of bitstream");
1753 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001754}
1755
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001756PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001757 // Set the PCH file name.
1758 this->FileName = FileName;
1759
Douglas Gregorc34897d2009-04-09 22:27:44 +00001760 // Open the PCH file.
1761 std::string ErrStr;
1762 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001763 if (!Buffer) {
1764 Error(ErrStr.c_str());
1765 return IgnorePCH;
1766 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001767
1768 // Initialize the stream
1769 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1770 (const unsigned char *)Buffer->getBufferEnd());
1771
1772 // Sniff for the signature.
1773 if (Stream.Read(8) != 'C' ||
1774 Stream.Read(8) != 'P' ||
1775 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001776 Stream.Read(8) != 'H') {
1777 Error("Not a PCH file");
1778 return IgnorePCH;
1779 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001780
1781 // We expect a number of well-defined blocks, though we don't necessarily
1782 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001783 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001784 while (!Stream.AtEndOfStream()) {
1785 unsigned Code = Stream.ReadCode();
1786
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001787 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1788 Error("Invalid record at top-level");
1789 return Failure;
1790 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001791
1792 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001793
Douglas Gregorc34897d2009-04-09 22:27:44 +00001794 // We only know the PCH subblock ID.
1795 switch (BlockID) {
1796 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001797 if (Stream.ReadBlockInfoBlock()) {
1798 Error("Malformed BlockInfoBlock");
1799 return Failure;
1800 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001801 break;
1802 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001803 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001804 case Success:
1805 break;
1806
1807 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001808 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001809
1810 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001811 // FIXME: We could consider reading through to the end of this
1812 // PCH block, skipping subblocks, to see if there are other
1813 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001814 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001815 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001816 break;
1817 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001818 if (Stream.SkipBlock()) {
1819 Error("Malformed block record");
1820 return Failure;
1821 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001822 break;
1823 }
1824 }
1825
1826 // Load the translation unit declaration
1827 ReadDeclRecord(DeclOffsets[0], 0);
1828
Douglas Gregorc713da92009-04-21 22:25:48 +00001829 // Initialization of builtins and library builtins occurs before the
1830 // PCH file is read, so there may be some identifiers that were
1831 // loaded into the IdentifierTable before we intercepted the
1832 // creation of identifiers. Iterate through the list of known
1833 // identifiers and determine whether we have to establish
1834 // preprocessor definitions or top-level identifier declaration
1835 // chains for those identifiers.
1836 //
1837 // We copy the IdentifierInfo pointers to a small vector first,
1838 // since de-serializing declarations or macro definitions can add
1839 // new entries into the identifier table, invalidating the
1840 // iterators.
1841 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1842 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1843 IdEnd = PP.getIdentifierTable().end();
1844 Id != IdEnd; ++Id)
1845 Identifiers.push_back(Id->second);
1846 PCHIdentifierLookupTable *IdTable
1847 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1848 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1849 IdentifierInfo *II = Identifiers[I];
1850 // Look in the on-disk hash table for an entry for
1851 PCHIdentifierLookupTrait Info(*this, II);
1852 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1853 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1854 if (Pos == IdTable->end())
1855 continue;
1856
1857 // Dereferencing the iterator has the effect of populating the
1858 // IdentifierInfo node with the various declarations it needs.
1859 (void)*Pos;
1860 }
1861
Douglas Gregore01ad442009-04-18 05:55:16 +00001862 // Load the special types.
1863 Context.setBuiltinVaListType(
1864 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1865
Douglas Gregorc713da92009-04-21 22:25:48 +00001866 // If we saw the preprocessor block, read it now.
1867 if (PreprocessorBlockOffset) {
1868 SavedStreamPosition SavedPos(Stream);
1869 Stream.JumpToBit(PreprocessorBlockOffset);
1870 if (ReadPreprocessorBlock()) {
1871 Error("Malformed preprocessor block");
1872 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001873 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001874 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001875
Douglas Gregorc713da92009-04-21 22:25:48 +00001876 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001877}
1878
Douglas Gregor179cfb12009-04-10 20:39:37 +00001879/// \brief Parse the record that corresponds to a LangOptions data
1880/// structure.
1881///
1882/// This routine compares the language options used to generate the
1883/// PCH file against the language options set for the current
1884/// compilation. For each option, we classify differences between the
1885/// two compiler states as either "benign" or "important". Benign
1886/// differences don't matter, and we accept them without complaint
1887/// (and without modifying the language options). Differences between
1888/// the states for important options cause the PCH file to be
1889/// unusable, so we emit a warning and return true to indicate that
1890/// there was an error.
1891///
1892/// \returns true if the PCH file is unacceptable, false otherwise.
1893bool PCHReader::ParseLanguageOptions(
1894 const llvm::SmallVectorImpl<uint64_t> &Record) {
1895 const LangOptions &LangOpts = Context.getLangOptions();
1896#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1897#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1898 if (Record[Idx] != LangOpts.Option) { \
1899 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1900 Diag(diag::note_ignoring_pch) << FileName; \
1901 return true; \
1902 } \
1903 ++Idx
1904
1905 unsigned Idx = 0;
1906 PARSE_LANGOPT_BENIGN(Trigraphs);
1907 PARSE_LANGOPT_BENIGN(BCPLComment);
1908 PARSE_LANGOPT_BENIGN(DollarIdents);
1909 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1910 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1911 PARSE_LANGOPT_BENIGN(ImplicitInt);
1912 PARSE_LANGOPT_BENIGN(Digraphs);
1913 PARSE_LANGOPT_BENIGN(HexFloats);
1914 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1915 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1916 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1917 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1918 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1919 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1920 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1921 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1922 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1923 PARSE_LANGOPT_BENIGN(PascalStrings);
1924 PARSE_LANGOPT_BENIGN(Boolean);
1925 PARSE_LANGOPT_BENIGN(WritableStrings);
1926 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1927 diag::warn_pch_lax_vector_conversions);
1928 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1929 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1930 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1931 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1932 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1933 diag::warn_pch_thread_safe_statics);
1934 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1935 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1936 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1937 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1938 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1939 diag::warn_pch_heinous_extensions);
1940 // FIXME: Most of the options below are benign if the macro wasn't
1941 // used. Unfortunately, this means that a PCH compiled without
1942 // optimization can't be used with optimization turned on, even
1943 // though the only thing that changes is whether __OPTIMIZE__ was
1944 // defined... but if __OPTIMIZE__ never showed up in the header, it
1945 // doesn't matter. We could consider making this some special kind
1946 // of check.
1947 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1948 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1949 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1950 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1951 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1952 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1953 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1954 Diag(diag::warn_pch_gc_mode)
1955 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1956 Diag(diag::note_ignoring_pch) << FileName;
1957 return true;
1958 }
1959 ++Idx;
1960 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1961 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1962#undef PARSE_LANGOPT_IRRELEVANT
1963#undef PARSE_LANGOPT_BENIGN
1964
1965 return false;
1966}
1967
Douglas Gregorc34897d2009-04-09 22:27:44 +00001968/// \brief Read and return the type at the given offset.
1969///
1970/// This routine actually reads the record corresponding to the type
1971/// at the given offset in the bitstream. It is a helper routine for
1972/// GetType, which deals with reading type IDs.
1973QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001974 // Keep track of where we are in the stream, then jump back there
1975 // after reading this type.
1976 SavedStreamPosition SavedPosition(Stream);
1977
Douglas Gregorc34897d2009-04-09 22:27:44 +00001978 Stream.JumpToBit(Offset);
1979 RecordData Record;
1980 unsigned Code = Stream.ReadCode();
1981 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001982 case pch::TYPE_EXT_QUAL: {
1983 assert(Record.size() == 3 &&
1984 "Incorrect encoding of extended qualifier type");
1985 QualType Base = GetType(Record[0]);
1986 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1987 unsigned AddressSpace = Record[2];
1988
1989 QualType T = Base;
1990 if (GCAttr != QualType::GCNone)
1991 T = Context.getObjCGCQualType(T, GCAttr);
1992 if (AddressSpace)
1993 T = Context.getAddrSpaceQualType(T, AddressSpace);
1994 return T;
1995 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001996
Douglas Gregorc34897d2009-04-09 22:27:44 +00001997 case pch::TYPE_FIXED_WIDTH_INT: {
1998 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1999 return Context.getFixedWidthIntType(Record[0], Record[1]);
2000 }
2001
2002 case pch::TYPE_COMPLEX: {
2003 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2004 QualType ElemType = GetType(Record[0]);
2005 return Context.getComplexType(ElemType);
2006 }
2007
2008 case pch::TYPE_POINTER: {
2009 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2010 QualType PointeeType = GetType(Record[0]);
2011 return Context.getPointerType(PointeeType);
2012 }
2013
2014 case pch::TYPE_BLOCK_POINTER: {
2015 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2016 QualType PointeeType = GetType(Record[0]);
2017 return Context.getBlockPointerType(PointeeType);
2018 }
2019
2020 case pch::TYPE_LVALUE_REFERENCE: {
2021 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2022 QualType PointeeType = GetType(Record[0]);
2023 return Context.getLValueReferenceType(PointeeType);
2024 }
2025
2026 case pch::TYPE_RVALUE_REFERENCE: {
2027 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2028 QualType PointeeType = GetType(Record[0]);
2029 return Context.getRValueReferenceType(PointeeType);
2030 }
2031
2032 case pch::TYPE_MEMBER_POINTER: {
2033 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2034 QualType PointeeType = GetType(Record[0]);
2035 QualType ClassType = GetType(Record[1]);
2036 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2037 }
2038
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002039 case pch::TYPE_CONSTANT_ARRAY: {
2040 QualType ElementType = GetType(Record[0]);
2041 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2042 unsigned IndexTypeQuals = Record[2];
2043 unsigned Idx = 3;
2044 llvm::APInt Size = ReadAPInt(Record, Idx);
2045 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2046 }
2047
2048 case pch::TYPE_INCOMPLETE_ARRAY: {
2049 QualType ElementType = GetType(Record[0]);
2050 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2051 unsigned IndexTypeQuals = Record[2];
2052 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2053 }
2054
2055 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002056 QualType ElementType = GetType(Record[0]);
2057 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2058 unsigned IndexTypeQuals = Record[2];
2059 return Context.getVariableArrayType(ElementType, ReadExpr(),
2060 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002061 }
2062
2063 case pch::TYPE_VECTOR: {
2064 if (Record.size() != 2) {
2065 Error("Incorrect encoding of vector type in PCH file");
2066 return QualType();
2067 }
2068
2069 QualType ElementType = GetType(Record[0]);
2070 unsigned NumElements = Record[1];
2071 return Context.getVectorType(ElementType, NumElements);
2072 }
2073
2074 case pch::TYPE_EXT_VECTOR: {
2075 if (Record.size() != 2) {
2076 Error("Incorrect encoding of extended vector type in PCH file");
2077 return QualType();
2078 }
2079
2080 QualType ElementType = GetType(Record[0]);
2081 unsigned NumElements = Record[1];
2082 return Context.getExtVectorType(ElementType, NumElements);
2083 }
2084
2085 case pch::TYPE_FUNCTION_NO_PROTO: {
2086 if (Record.size() != 1) {
2087 Error("Incorrect encoding of no-proto function type");
2088 return QualType();
2089 }
2090 QualType ResultType = GetType(Record[0]);
2091 return Context.getFunctionNoProtoType(ResultType);
2092 }
2093
2094 case pch::TYPE_FUNCTION_PROTO: {
2095 QualType ResultType = GetType(Record[0]);
2096 unsigned Idx = 1;
2097 unsigned NumParams = Record[Idx++];
2098 llvm::SmallVector<QualType, 16> ParamTypes;
2099 for (unsigned I = 0; I != NumParams; ++I)
2100 ParamTypes.push_back(GetType(Record[Idx++]));
2101 bool isVariadic = Record[Idx++];
2102 unsigned Quals = Record[Idx++];
2103 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2104 isVariadic, Quals);
2105 }
2106
2107 case pch::TYPE_TYPEDEF:
2108 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2109 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2110
2111 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002112 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002113
2114 case pch::TYPE_TYPEOF: {
2115 if (Record.size() != 1) {
2116 Error("Incorrect encoding of typeof(type) in PCH file");
2117 return QualType();
2118 }
2119 QualType UnderlyingType = GetType(Record[0]);
2120 return Context.getTypeOfType(UnderlyingType);
2121 }
2122
2123 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002124 assert(Record.size() == 1 && "Incorrect encoding of record type");
2125 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002126
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002127 case pch::TYPE_ENUM:
2128 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2129 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2130
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002131 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002132 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2133 return Context.getObjCInterfaceType(
2134 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002135
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002136 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2137 unsigned Idx = 0;
2138 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2139 unsigned NumProtos = Record[Idx++];
2140 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2141 for (unsigned I = 0; I != NumProtos; ++I)
2142 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2143 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2144 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002145
Chris Lattner9b9f2352009-04-22 06:40:03 +00002146 case pch::TYPE_OBJC_QUALIFIED_ID: {
2147 unsigned Idx = 0;
2148 unsigned NumProtos = Record[Idx++];
2149 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2150 for (unsigned I = 0; I != NumProtos; ++I)
2151 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2152 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2153 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002154 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002155 // Suppress a GCC warning
2156 return QualType();
2157}
2158
2159/// \brief Note that we have loaded the declaration with the given
2160/// Index.
2161///
2162/// This routine notes that this declaration has already been loaded,
2163/// so that future GetDecl calls will return this declaration rather
2164/// than trying to load a new declaration.
2165inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2166 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2167 DeclAlreadyLoaded[Index] = true;
2168 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2169}
2170
2171/// \brief Read the declaration at the given offset from the PCH file.
2172Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002173 // Keep track of where we are in the stream, then jump back there
2174 // after reading this declaration.
2175 SavedStreamPosition SavedPosition(Stream);
2176
Douglas Gregorc34897d2009-04-09 22:27:44 +00002177 Decl *D = 0;
2178 Stream.JumpToBit(Offset);
2179 RecordData Record;
2180 unsigned Code = Stream.ReadCode();
2181 unsigned Idx = 0;
2182 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002183
Douglas Gregorc34897d2009-04-09 22:27:44 +00002184 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002185 case pch::DECL_ATTR:
2186 case pch::DECL_CONTEXT_LEXICAL:
2187 case pch::DECL_CONTEXT_VISIBLE:
2188 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2189 break;
2190
Douglas Gregorc34897d2009-04-09 22:27:44 +00002191 case pch::DECL_TRANSLATION_UNIT:
2192 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002193 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002194 break;
2195
2196 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002197 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002198 break;
2199 }
2200
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002201 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002202 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002203 break;
2204 }
2205
Douglas Gregor982365e2009-04-13 21:20:57 +00002206 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002207 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2208 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002209 break;
2210 }
2211
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002212 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002213 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2214 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002215 break;
2216 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002217
2218 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002219 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2220 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002221 break;
2222 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002223
Steve Naroff79ea0e02009-04-20 15:06:07 +00002224 case pch::DECL_OBJC_METHOD: {
2225 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2226 Selector(), QualType(), 0);
2227 break;
2228 }
2229
Steve Naroff97b53bd2009-04-21 15:12:33 +00002230 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002231 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2232 break;
2233 }
2234
Steve Naroff97b53bd2009-04-21 15:12:33 +00002235 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002236 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2237 ObjCIvarDecl::None);
2238 break;
2239 }
2240
Steve Naroff97b53bd2009-04-21 15:12:33 +00002241 case pch::DECL_OBJC_PROTOCOL: {
2242 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2243 break;
2244 }
2245
2246 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2247 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2248 QualType(), 0);
2249 break;
2250 }
2251
2252 case pch::DECL_OBJC_CLASS: {
2253 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2254 break;
2255 }
2256
2257 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2258 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2259 break;
2260 }
2261
2262 case pch::DECL_OBJC_CATEGORY: {
2263 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2264 break;
2265 }
2266
2267 case pch::DECL_OBJC_CATEGORY_IMPL: {
2268 // FIXME: Implement.
2269 break;
2270 }
2271
2272 case pch::DECL_OBJC_IMPLEMENTATION: {
2273 // FIXME: Implement.
2274 break;
2275 }
2276
2277 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2278 // FIXME: Implement.
2279 break;
2280 }
2281
2282 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002283 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002284 break;
2285 }
2286
2287 case pch::DECL_OBJC_PROPERTY_IMPL: {
2288 // FIXME: Implement.
2289 break;
2290 }
2291
Douglas Gregor982365e2009-04-13 21:20:57 +00002292 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002293 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2294 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002295 break;
2296 }
2297
Douglas Gregorc34897d2009-04-09 22:27:44 +00002298 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002299 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2300 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002301 break;
2302 }
2303
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002304 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002305 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2306 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002307 break;
2308 }
2309
2310 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002311 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002312 QualType(), QualType(), VarDecl::None,
2313 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002314 break;
2315 }
2316
Douglas Gregor2a491792009-04-13 22:49:25 +00002317 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002318 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002319 break;
2320 }
2321
2322 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002323 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002324 break;
2325 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002326 }
2327
Douglas Gregorc713da92009-04-21 22:25:48 +00002328 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002329 if (D) {
2330 LoadedDecl(Index, D);
2331 Reader.Visit(D);
2332 }
2333
Douglas Gregorc34897d2009-04-09 22:27:44 +00002334 // If this declaration is also a declaration context, get the
2335 // offsets for its tables of lexical and visible declarations.
2336 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2337 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2338 if (Offsets.first || Offsets.second) {
2339 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2340 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2341 DeclContextOffsets[DC] = Offsets;
2342 }
2343 }
2344 assert(Idx == Record.size());
2345
Douglas Gregor405b6432009-04-22 19:09:20 +00002346 if (Consumer) {
2347 // If we have deserialized a declaration that has a definition the
2348 // AST consumer might need to know about, notify the consumer
2349 // about that definition now.
2350 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2351 if (Var->isFileVarDecl() && Var->getInit()) {
2352 DeclGroupRef DG(Var);
2353 Consumer->HandleTopLevelDecl(DG);
2354 }
2355 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2356 if (Func->isThisDeclarationADefinition()) {
2357 DeclGroupRef DG(Func);
2358 Consumer->HandleTopLevelDecl(DG);
2359 }
2360 }
2361 }
2362
Douglas Gregorc34897d2009-04-09 22:27:44 +00002363 return D;
2364}
2365
Douglas Gregorac8f2802009-04-10 17:25:41 +00002366QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002367 unsigned Quals = ID & 0x07;
2368 unsigned Index = ID >> 3;
2369
2370 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2371 QualType T;
2372 switch ((pch::PredefinedTypeIDs)Index) {
2373 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2374 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2375 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2376
2377 case pch::PREDEF_TYPE_CHAR_U_ID:
2378 case pch::PREDEF_TYPE_CHAR_S_ID:
2379 // FIXME: Check that the signedness of CharTy is correct!
2380 T = Context.CharTy;
2381 break;
2382
2383 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2384 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2385 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2386 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2387 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2388 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2389 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2390 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2391 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2392 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2393 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2394 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2395 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2396 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2397 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2398 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2399 }
2400
2401 assert(!T.isNull() && "Unknown predefined type");
2402 return T.getQualifiedType(Quals);
2403 }
2404
2405 Index -= pch::NUM_PREDEF_TYPE_IDS;
2406 if (!TypeAlreadyLoaded[Index]) {
2407 // Load the type from the PCH file.
2408 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2409 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2410 TypeAlreadyLoaded[Index] = true;
2411 }
2412
2413 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2414}
2415
Douglas Gregorac8f2802009-04-10 17:25:41 +00002416Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002417 if (ID == 0)
2418 return 0;
2419
2420 unsigned Index = ID - 1;
2421 if (DeclAlreadyLoaded[Index])
2422 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2423
2424 // Load the declaration from the PCH file.
2425 return ReadDeclRecord(DeclOffsets[Index], Index);
2426}
2427
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002428Stmt *PCHReader::GetStmt(uint64_t Offset) {
2429 // Keep track of where we are in the stream, then jump back there
2430 // after reading this declaration.
2431 SavedStreamPosition SavedPosition(Stream);
2432
2433 Stream.JumpToBit(Offset);
2434 return ReadStmt();
2435}
2436
Douglas Gregorc34897d2009-04-09 22:27:44 +00002437bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002438 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002439 assert(DC->hasExternalLexicalStorage() &&
2440 "DeclContext has no lexical decls in storage");
2441 uint64_t Offset = DeclContextOffsets[DC].first;
2442 assert(Offset && "DeclContext has no lexical decls in storage");
2443
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002444 // Keep track of where we are in the stream, then jump back there
2445 // after reading this context.
2446 SavedStreamPosition SavedPosition(Stream);
2447
Douglas Gregorc34897d2009-04-09 22:27:44 +00002448 // Load the record containing all of the declarations lexically in
2449 // this context.
2450 Stream.JumpToBit(Offset);
2451 RecordData Record;
2452 unsigned Code = Stream.ReadCode();
2453 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002454 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002455 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2456
2457 // Load all of the declaration IDs
2458 Decls.clear();
2459 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002460 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002461 return false;
2462}
2463
2464bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2465 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2466 assert(DC->hasExternalVisibleStorage() &&
2467 "DeclContext has no visible decls in storage");
2468 uint64_t Offset = DeclContextOffsets[DC].second;
2469 assert(Offset && "DeclContext has no visible decls in storage");
2470
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002471 // Keep track of where we are in the stream, then jump back there
2472 // after reading this context.
2473 SavedStreamPosition SavedPosition(Stream);
2474
Douglas Gregorc34897d2009-04-09 22:27:44 +00002475 // Load the record containing all of the declarations visible in
2476 // this context.
2477 Stream.JumpToBit(Offset);
2478 RecordData Record;
2479 unsigned Code = Stream.ReadCode();
2480 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002481 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002482 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2483 if (Record.size() == 0)
2484 return false;
2485
2486 Decls.clear();
2487
2488 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002489 while (Idx < Record.size()) {
2490 Decls.push_back(VisibleDeclaration());
2491 Decls.back().Name = ReadDeclarationName(Record, Idx);
2492
Douglas Gregorc34897d2009-04-09 22:27:44 +00002493 unsigned Size = Record[Idx++];
2494 llvm::SmallVector<unsigned, 4> & LoadedDecls
2495 = Decls.back().Declarations;
2496 LoadedDecls.reserve(Size);
2497 for (unsigned I = 0; I < Size; ++I)
2498 LoadedDecls.push_back(Record[Idx++]);
2499 }
2500
Douglas Gregoraf136d92009-04-22 22:34:57 +00002501 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002502 return false;
2503}
2504
Douglas Gregor631f6c62009-04-14 00:24:19 +00002505void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002506 this->Consumer = Consumer;
2507
Douglas Gregor631f6c62009-04-14 00:24:19 +00002508 if (!Consumer)
2509 return;
2510
2511 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2512 Decl *D = GetDecl(ExternalDefinitions[I]);
2513 DeclGroupRef DG(D);
2514 Consumer->HandleTopLevelDecl(DG);
2515 }
2516}
2517
Douglas Gregorc34897d2009-04-09 22:27:44 +00002518void PCHReader::PrintStats() {
2519 std::fprintf(stderr, "*** PCH Statistics:\n");
2520
2521 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2522 TypeAlreadyLoaded.end(),
2523 true);
2524 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2525 DeclAlreadyLoaded.end(),
2526 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002527 unsigned NumIdentifiersLoaded = 0;
2528 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2529 if ((IdentifierData[I] & 0x01) == 0)
2530 ++NumIdentifiersLoaded;
2531 }
2532
Douglas Gregorc34897d2009-04-09 22:27:44 +00002533 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2534 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002535 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002536 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2537 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002538 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2539 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2540 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2541 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002542 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2543 NumStatementsRead, TotalNumStatements,
2544 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002545 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2546 NumMacrosRead, TotalNumMacros,
2547 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002548 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2549 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2550 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2551 * 100));
2552 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2553 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2554 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2555 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002556 std::fprintf(stderr, "\n");
2557}
2558
Douglas Gregorc713da92009-04-21 22:25:48 +00002559void PCHReader::InitializeSema(Sema &S) {
2560 SemaObj = &S;
2561
Douglas Gregor2554cf22009-04-22 21:15:06 +00002562 // Makes sure any declarations that were deserialized "too early"
2563 // still get added to the identifier's declaration chains.
2564 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2565 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2566 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002567 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002568 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002569
2570 // If there were any tentative definitions, deserialize them and add
2571 // them to Sema's table of tentative definitions.
2572 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2573 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2574 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2575 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002576
2577 // If there were any locally-scoped external declarations,
2578 // deserialize them and add them to Sema's table of locally-scoped
2579 // external declarations.
2580 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2581 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2582 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2583 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002584}
2585
2586IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2587 // Try to find this name within our on-disk hash table
2588 PCHIdentifierLookupTable *IdTable
2589 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2590 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2591 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2592 if (Pos == IdTable->end())
2593 return 0;
2594
2595 // Dereferencing the iterator has the effect of building the
2596 // IdentifierInfo node and populating it with the various
2597 // declarations it needs.
2598 return *Pos;
2599}
2600
2601void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2602 assert(ID && "Non-zero identifier ID required");
2603 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2604}
2605
Chris Lattner29241862009-04-11 21:15:38 +00002606IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002607 if (ID == 0)
2608 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002609
Douglas Gregorc713da92009-04-21 22:25:48 +00002610 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002611 Error("No identifier table in PCH file");
2612 return 0;
2613 }
Chris Lattner29241862009-04-11 21:15:38 +00002614
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002615 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002616 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002617 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002618 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002619 }
Chris Lattner29241862009-04-11 21:15:38 +00002620
2621 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002622}
2623
2624DeclarationName
2625PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2626 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2627 switch (Kind) {
2628 case DeclarationName::Identifier:
2629 return DeclarationName(GetIdentifierInfo(Record, Idx));
2630
2631 case DeclarationName::ObjCZeroArgSelector:
2632 case DeclarationName::ObjCOneArgSelector:
2633 case DeclarationName::ObjCMultiArgSelector:
2634 assert(false && "Unable to de-serialize Objective-C selectors");
2635 break;
2636
2637 case DeclarationName::CXXConstructorName:
2638 return Context.DeclarationNames.getCXXConstructorName(
2639 GetType(Record[Idx++]));
2640
2641 case DeclarationName::CXXDestructorName:
2642 return Context.DeclarationNames.getCXXDestructorName(
2643 GetType(Record[Idx++]));
2644
2645 case DeclarationName::CXXConversionFunctionName:
2646 return Context.DeclarationNames.getCXXConversionFunctionName(
2647 GetType(Record[Idx++]));
2648
2649 case DeclarationName::CXXOperatorName:
2650 return Context.DeclarationNames.getCXXOperatorName(
2651 (OverloadedOperatorKind)Record[Idx++]);
2652
2653 case DeclarationName::CXXUsingDirective:
2654 return DeclarationName::getUsingDirectiveName();
2655 }
2656
2657 // Required to silence GCC warning
2658 return DeclarationName();
2659}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002660
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002661/// \brief Read an integral value
2662llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2663 unsigned BitWidth = Record[Idx++];
2664 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2665 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2666 Idx += NumWords;
2667 return Result;
2668}
2669
2670/// \brief Read a signed integral value
2671llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2672 bool isUnsigned = Record[Idx++];
2673 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2674}
2675
Douglas Gregore2f37202009-04-14 21:55:33 +00002676/// \brief Read a floating-point value
2677llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002678 return llvm::APFloat(ReadAPInt(Record, Idx));
2679}
2680
Douglas Gregor1c507882009-04-15 21:30:51 +00002681// \brief Read a string
2682std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2683 unsigned Len = Record[Idx++];
2684 std::string Result(&Record[Idx], &Record[Idx] + Len);
2685 Idx += Len;
2686 return Result;
2687}
2688
2689/// \brief Reads attributes from the current stream position.
2690Attr *PCHReader::ReadAttributes() {
2691 unsigned Code = Stream.ReadCode();
2692 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2693 "Expected unabbreviated record"); (void)Code;
2694
2695 RecordData Record;
2696 unsigned Idx = 0;
2697 unsigned RecCode = Stream.ReadRecord(Code, Record);
2698 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2699 (void)RecCode;
2700
2701#define SIMPLE_ATTR(Name) \
2702 case Attr::Name: \
2703 New = ::new (Context) Name##Attr(); \
2704 break
2705
2706#define STRING_ATTR(Name) \
2707 case Attr::Name: \
2708 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2709 break
2710
2711#define UNSIGNED_ATTR(Name) \
2712 case Attr::Name: \
2713 New = ::new (Context) Name##Attr(Record[Idx++]); \
2714 break
2715
2716 Attr *Attrs = 0;
2717 while (Idx < Record.size()) {
2718 Attr *New = 0;
2719 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2720 bool IsInherited = Record[Idx++];
2721
2722 switch (Kind) {
2723 STRING_ATTR(Alias);
2724 UNSIGNED_ATTR(Aligned);
2725 SIMPLE_ATTR(AlwaysInline);
2726 SIMPLE_ATTR(AnalyzerNoReturn);
2727 STRING_ATTR(Annotate);
2728 STRING_ATTR(AsmLabel);
2729
2730 case Attr::Blocks:
2731 New = ::new (Context) BlocksAttr(
2732 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2733 break;
2734
2735 case Attr::Cleanup:
2736 New = ::new (Context) CleanupAttr(
2737 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2738 break;
2739
2740 SIMPLE_ATTR(Const);
2741 UNSIGNED_ATTR(Constructor);
2742 SIMPLE_ATTR(DLLExport);
2743 SIMPLE_ATTR(DLLImport);
2744 SIMPLE_ATTR(Deprecated);
2745 UNSIGNED_ATTR(Destructor);
2746 SIMPLE_ATTR(FastCall);
2747
2748 case Attr::Format: {
2749 std::string Type = ReadString(Record, Idx);
2750 unsigned FormatIdx = Record[Idx++];
2751 unsigned FirstArg = Record[Idx++];
2752 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2753 break;
2754 }
2755
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002756 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002757
2758 case Attr::IBOutletKind:
2759 New = ::new (Context) IBOutletAttr();
2760 break;
2761
2762 SIMPLE_ATTR(NoReturn);
2763 SIMPLE_ATTR(NoThrow);
2764 SIMPLE_ATTR(Nodebug);
2765 SIMPLE_ATTR(Noinline);
2766
2767 case Attr::NonNull: {
2768 unsigned Size = Record[Idx++];
2769 llvm::SmallVector<unsigned, 16> ArgNums;
2770 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2771 Idx += Size;
2772 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2773 break;
2774 }
2775
2776 SIMPLE_ATTR(ObjCException);
2777 SIMPLE_ATTR(ObjCNSObject);
2778 SIMPLE_ATTR(Overloadable);
2779 UNSIGNED_ATTR(Packed);
2780 SIMPLE_ATTR(Pure);
2781 UNSIGNED_ATTR(Regparm);
2782 STRING_ATTR(Section);
2783 SIMPLE_ATTR(StdCall);
2784 SIMPLE_ATTR(TransparentUnion);
2785 SIMPLE_ATTR(Unavailable);
2786 SIMPLE_ATTR(Unused);
2787 SIMPLE_ATTR(Used);
2788
2789 case Attr::Visibility:
2790 New = ::new (Context) VisibilityAttr(
2791 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2792 break;
2793
2794 SIMPLE_ATTR(WarnUnusedResult);
2795 SIMPLE_ATTR(Weak);
2796 SIMPLE_ATTR(WeakImport);
2797 }
2798
2799 assert(New && "Unable to decode attribute?");
2800 New->setInherited(IsInherited);
2801 New->setNext(Attrs);
2802 Attrs = New;
2803 }
2804#undef UNSIGNED_ATTR
2805#undef STRING_ATTR
2806#undef SIMPLE_ATTR
2807
2808 // The list of attributes was built backwards. Reverse the list
2809 // before returning it.
2810 Attr *PrevAttr = 0, *NextAttr = 0;
2811 while (Attrs) {
2812 NextAttr = Attrs->getNext();
2813 Attrs->setNext(PrevAttr);
2814 PrevAttr = Attrs;
2815 Attrs = NextAttr;
2816 }
2817
2818 return PrevAttr;
2819}
2820
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002821Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002822 // Within the bitstream, expressions are stored in Reverse Polish
2823 // Notation, with each of the subexpressions preceding the
2824 // expression they are stored in. To evaluate expressions, we
2825 // continue reading expressions and placing them on the stack, with
2826 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002827 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002828 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002829 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002830 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002831 llvm::SmallVector<Stmt *, 16> StmtStack;
2832 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002833 Stmt::EmptyShell Empty;
2834
Douglas Gregora151ba42009-04-14 23:32:43 +00002835 while (true) {
2836 unsigned Code = Stream.ReadCode();
2837 if (Code == llvm::bitc::END_BLOCK) {
2838 if (Stream.ReadBlockEnd()) {
2839 Error("Error at end of Source Manager block");
2840 return 0;
2841 }
2842 break;
2843 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002844
Douglas Gregora151ba42009-04-14 23:32:43 +00002845 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2846 // No known subblocks, always skip them.
2847 Stream.ReadSubBlockID();
2848 if (Stream.SkipBlock()) {
2849 Error("Malformed block record");
2850 return 0;
2851 }
2852 continue;
2853 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002854
Douglas Gregora151ba42009-04-14 23:32:43 +00002855 if (Code == llvm::bitc::DEFINE_ABBREV) {
2856 Stream.ReadAbbrevRecord();
2857 continue;
2858 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002859
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002860 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002861 Idx = 0;
2862 Record.clear();
2863 bool Finished = false;
2864 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002865 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002866 Finished = true;
2867 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002868
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002869 case pch::STMT_NULL_PTR:
2870 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002871 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002872
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002873 case pch::STMT_NULL:
2874 S = new (Context) NullStmt(Empty);
2875 break;
2876
2877 case pch::STMT_COMPOUND:
2878 S = new (Context) CompoundStmt(Empty);
2879 break;
2880
2881 case pch::STMT_CASE:
2882 S = new (Context) CaseStmt(Empty);
2883 break;
2884
2885 case pch::STMT_DEFAULT:
2886 S = new (Context) DefaultStmt(Empty);
2887 break;
2888
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002889 case pch::STMT_LABEL:
2890 S = new (Context) LabelStmt(Empty);
2891 break;
2892
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002893 case pch::STMT_IF:
2894 S = new (Context) IfStmt(Empty);
2895 break;
2896
2897 case pch::STMT_SWITCH:
2898 S = new (Context) SwitchStmt(Empty);
2899 break;
2900
Douglas Gregora6b503f2009-04-17 00:16:09 +00002901 case pch::STMT_WHILE:
2902 S = new (Context) WhileStmt(Empty);
2903 break;
2904
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002905 case pch::STMT_DO:
2906 S = new (Context) DoStmt(Empty);
2907 break;
2908
2909 case pch::STMT_FOR:
2910 S = new (Context) ForStmt(Empty);
2911 break;
2912
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002913 case pch::STMT_GOTO:
2914 S = new (Context) GotoStmt(Empty);
2915 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002916
2917 case pch::STMT_INDIRECT_GOTO:
2918 S = new (Context) IndirectGotoStmt(Empty);
2919 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002920
Douglas Gregora6b503f2009-04-17 00:16:09 +00002921 case pch::STMT_CONTINUE:
2922 S = new (Context) ContinueStmt(Empty);
2923 break;
2924
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002925 case pch::STMT_BREAK:
2926 S = new (Context) BreakStmt(Empty);
2927 break;
2928
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002929 case pch::STMT_RETURN:
2930 S = new (Context) ReturnStmt(Empty);
2931 break;
2932
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002933 case pch::STMT_DECL:
2934 S = new (Context) DeclStmt(Empty);
2935 break;
2936
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002937 case pch::STMT_ASM:
2938 S = new (Context) AsmStmt(Empty);
2939 break;
2940
Douglas Gregora151ba42009-04-14 23:32:43 +00002941 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002942 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002943 break;
2944
2945 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002946 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002947 break;
2948
2949 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002950 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002951 break;
2952
2953 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002954 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002955 break;
2956
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002957 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002958 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002959 break;
2960
Douglas Gregor596e0932009-04-15 16:35:07 +00002961 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002962 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002963 Record[PCHStmtReader::NumExprFields + 1]);
2964 break;
2965
Douglas Gregora151ba42009-04-14 23:32:43 +00002966 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002967 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002968 break;
2969
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002970 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002971 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002972 break;
2973
Douglas Gregor12d74052009-04-15 15:58:59 +00002974 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002975 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002976 break;
2977
2978 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002979 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002980 break;
2981
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002982 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002983 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002984 break;
2985
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002986 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002987 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002988 break;
2989
2990 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002991 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002992 break;
2993
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002994 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002995 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002996 break;
2997
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002998 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002999 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003000 break;
3001
3002 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003003 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003004 break;
3005
Douglas Gregora151ba42009-04-14 23:32:43 +00003006 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003007 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003008 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003009
3010 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003011 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003012 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003013
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003014 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003015 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003016 break;
3017
Douglas Gregorec0b8292009-04-15 23:02:49 +00003018 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003019 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003020 break;
3021
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003022 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003023 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003024 break;
3025
3026 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003027 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003028 Record[PCHStmtReader::NumExprFields] - 1);
3029
3030 break;
3031
3032 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003033 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003034 break;
3035
Douglas Gregorec0b8292009-04-15 23:02:49 +00003036 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003037 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003038 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003039
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003040 case pch::EXPR_ADDR_LABEL:
3041 S = new (Context) AddrLabelExpr(Empty);
3042 break;
3043
Douglas Gregoreca12f62009-04-17 19:05:30 +00003044 case pch::EXPR_STMT:
3045 S = new (Context) StmtExpr(Empty);
3046 break;
3047
Douglas Gregor209d4622009-04-15 23:33:31 +00003048 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003049 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003050 break;
3051
3052 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003053 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003054 break;
3055
3056 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003057 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003058 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003059
3060 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003061 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003062 break;
3063
Douglas Gregore246b742009-04-17 19:21:43 +00003064 case pch::EXPR_BLOCK:
3065 S = new (Context) BlockExpr(Empty);
3066 break;
3067
Douglas Gregor725e94b2009-04-16 00:01:45 +00003068 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003069 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003070 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003071
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003072 case pch::EXPR_OBJC_STRING_LITERAL:
3073 S = new (Context) ObjCStringLiteral(Empty);
3074 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003075 case pch::EXPR_OBJC_ENCODE:
3076 S = new (Context) ObjCEncodeExpr(Empty);
3077 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003078 case pch::EXPR_OBJC_SELECTOR_EXPR:
3079 S = new (Context) ObjCSelectorExpr(Empty);
3080 break;
3081 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3082 S = new (Context) ObjCProtocolExpr(Empty);
3083 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003084 }
3085
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003086 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003087 if (Finished)
3088 break;
3089
Douglas Gregor456e0952009-04-17 22:13:46 +00003090 ++NumStatementsRead;
3091
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003092 if (S) {
3093 unsigned NumSubStmts = Reader.Visit(S);
3094 while (NumSubStmts > 0) {
3095 StmtStack.pop_back();
3096 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003097 }
3098 }
3099
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003100 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003101 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003102 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003103 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003104 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003105 return StmtStack.back();
3106}
3107
3108Expr *PCHReader::ReadExpr() {
3109 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003110}
3111
Douglas Gregor179cfb12009-04-10 20:39:37 +00003112DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003113 return Diag(SourceLocation(), DiagID);
3114}
3115
3116DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3117 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003118 Context.getSourceManager()),
3119 DiagID);
3120}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003121
Douglas Gregorc713da92009-04-21 22:25:48 +00003122/// \brief Retrieve the identifier table associated with the
3123/// preprocessor.
3124IdentifierTable &PCHReader::getIdentifierTable() {
3125 return PP.getIdentifierTable();
3126}
3127
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003128/// \brief Record that the given ID maps to the given switch-case
3129/// statement.
3130void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3131 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3132 SwitchCaseStmts[ID] = SC;
3133}
3134
3135/// \brief Retrieve the switch-case statement with the given ID.
3136SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3137 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3138 return SwitchCaseStmts[ID];
3139}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003140
3141/// \brief Record that the given label statement has been
3142/// deserialized and has the given ID.
3143void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3144 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3145 "Deserialized label twice");
3146 LabelStmts[ID] = S;
3147
3148 // If we've already seen any goto statements that point to this
3149 // label, resolve them now.
3150 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3151 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3152 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3153 Goto->second->setLabel(S);
3154 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003155
3156 // If we've already seen any address-label statements that point to
3157 // this label, resolve them now.
3158 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3159 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3160 = UnresolvedAddrLabelExprs.equal_range(ID);
3161 for (AddrLabelIter AddrLabel = AddrLabels.first;
3162 AddrLabel != AddrLabels.second; ++AddrLabel)
3163 AddrLabel->second->setLabel(S);
3164 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003165}
3166
3167/// \brief Set the label of the given statement to the label
3168/// identified by ID.
3169///
3170/// Depending on the order in which the label and other statements
3171/// referencing that label occur, this operation may complete
3172/// immediately (updating the statement) or it may queue the
3173/// statement to be back-patched later.
3174void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3175 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3176 if (Label != LabelStmts.end()) {
3177 // We've already seen this label, so set the label of the goto and
3178 // we're done.
3179 S->setLabel(Label->second);
3180 } else {
3181 // We haven't seen this label yet, so add this goto to the set of
3182 // unresolved goto statements.
3183 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3184 }
3185}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003186
3187/// \brief Set the label of the given expression to the label
3188/// identified by ID.
3189///
3190/// Depending on the order in which the label and other statements
3191/// referencing that label occur, this operation may complete
3192/// immediately (updating the statement) or it may queue the
3193/// statement to be back-patched later.
3194void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3195 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3196 if (Label != LabelStmts.end()) {
3197 // We've already seen this label, so set the label of the
3198 // label-address expression and we're done.
3199 S->setLabel(Label->second);
3200 } else {
3201 // We haven't seen this label yet, so add this label-address
3202 // expression to the set of unresolved label-address expressions.
3203 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3204 }
3205}