blob: 481f14855cbfdeb5f68af3d71f789b84dd348d1f [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);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000332 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000333}
334
335void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
336 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000337 D->setSuperClass(
338 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000339}
340
341
342void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
343 VisitDecl(D);
344 // FIXME: Implement.
345}
346
Douglas Gregor982365e2009-04-13 21:20:57 +0000347void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
348 VisitValueDecl(FD);
349 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000350 if (Record[Idx++])
351 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000352}
353
Douglas Gregorc34897d2009-04-09 22:27:44 +0000354void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
355 VisitValueDecl(VD);
356 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
357 VD->setThreadSpecified(Record[Idx++]);
358 VD->setCXXDirectInitializer(Record[Idx++]);
359 VD->setDeclaredInCondition(Record[Idx++]);
360 VD->setPreviousDeclaration(
361 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
362 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000363 if (Record[Idx++])
364 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000365}
366
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000367void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
368 VisitVarDecl(PD);
369 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000370 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000371}
372
373void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
374 VisitParmVarDecl(PD);
375 PD->setOriginalType(Reader.GetType(Record[Idx++]));
376}
377
Douglas Gregor2a491792009-04-13 22:49:25 +0000378void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
379 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000380 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000381}
382
383void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
384 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000385 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000386 unsigned NumParams = Record[Idx++];
387 llvm::SmallVector<ParmVarDecl *, 16> Params;
388 Params.reserve(NumParams);
389 for (unsigned I = 0; I != NumParams; ++I)
390 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
391 BD->setParams(Reader.getContext(), &Params[0], NumParams);
392}
393
Douglas Gregorc34897d2009-04-09 22:27:44 +0000394std::pair<uint64_t, uint64_t>
395PCHDeclReader::VisitDeclContext(DeclContext *DC) {
396 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000397 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000398 return std::make_pair(LexicalOffset, VisibleOffset);
399}
400
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000401//===----------------------------------------------------------------------===//
402// Statement/expression deserialization
403//===----------------------------------------------------------------------===//
404namespace {
405 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000406 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000407 PCHReader &Reader;
408 const PCHReader::RecordData &Record;
409 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000410 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000411
412 public:
413 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000414 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
415 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000416
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000417 /// \brief The number of record fields required for the Stmt class
418 /// itself.
419 static const unsigned NumStmtFields = 0;
420
Douglas Gregor596e0932009-04-15 16:35:07 +0000421 /// \brief The number of record fields required for the Expr class
422 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000423 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000424
Douglas Gregora151ba42009-04-14 23:32:43 +0000425 // Each of the Visit* functions reads in part of the expression
426 // from the given record and the current expression stack, then
427 // return the total number of operands that it read from the
428 // expression stack.
429
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000430 unsigned VisitStmt(Stmt *S);
431 unsigned VisitNullStmt(NullStmt *S);
432 unsigned VisitCompoundStmt(CompoundStmt *S);
433 unsigned VisitSwitchCase(SwitchCase *S);
434 unsigned VisitCaseStmt(CaseStmt *S);
435 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000436 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000437 unsigned VisitIfStmt(IfStmt *S);
438 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000439 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000440 unsigned VisitDoStmt(DoStmt *S);
441 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000442 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000443 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000444 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000445 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000446 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000447 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000448 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000449 unsigned VisitExpr(Expr *E);
450 unsigned VisitPredefinedExpr(PredefinedExpr *E);
451 unsigned VisitDeclRefExpr(DeclRefExpr *E);
452 unsigned VisitIntegerLiteral(IntegerLiteral *E);
453 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000454 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000455 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000456 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000457 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000458 unsigned VisitUnaryOperator(UnaryOperator *E);
459 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000460 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000461 unsigned VisitCallExpr(CallExpr *E);
462 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000463 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000464 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000465 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
466 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000467 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000468 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
469 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000470 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000471 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000472 unsigned VisitInitListExpr(InitListExpr *E);
473 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
474 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000475 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000476 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000477 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000478 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
479 unsigned VisitChooseExpr(ChooseExpr *E);
480 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000481 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000482 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000483 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000484 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000485 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000486 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
487 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000488 };
489}
490
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000491unsigned PCHStmtReader::VisitStmt(Stmt *S) {
492 assert(Idx == NumStmtFields && "Incorrect statement field count");
493 return 0;
494}
495
496unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
497 VisitStmt(S);
498 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
499 return 0;
500}
501
502unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
503 VisitStmt(S);
504 unsigned NumStmts = Record[Idx++];
505 S->setStmts(Reader.getContext(),
506 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
507 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
509 return NumStmts;
510}
511
512unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
513 VisitStmt(S);
514 Reader.RecordSwitchCaseID(S, Record[Idx++]);
515 return 0;
516}
517
518unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
519 VisitSwitchCase(S);
520 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
521 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
522 S->setSubStmt(StmtStack.back());
523 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
524 return 3;
525}
526
527unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
528 VisitSwitchCase(S);
529 S->setSubStmt(StmtStack.back());
530 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
531 return 1;
532}
533
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000534unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
535 VisitStmt(S);
536 S->setID(Reader.GetIdentifierInfo(Record, Idx));
537 S->setSubStmt(StmtStack.back());
538 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
539 Reader.RecordLabelStmt(S, Record[Idx++]);
540 return 1;
541}
542
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000543unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
544 VisitStmt(S);
545 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
546 S->setThen(StmtStack[StmtStack.size() - 2]);
547 S->setElse(StmtStack[StmtStack.size() - 1]);
548 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
549 return 3;
550}
551
552unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
553 VisitStmt(S);
554 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
555 S->setBody(StmtStack.back());
556 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
557 SwitchCase *PrevSC = 0;
558 for (unsigned N = Record.size(); Idx != N; ++Idx) {
559 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
560 if (PrevSC)
561 PrevSC->setNextSwitchCase(SC);
562 else
563 S->setSwitchCaseList(SC);
564 PrevSC = SC;
565 }
566 return 2;
567}
568
Douglas Gregora6b503f2009-04-17 00:16:09 +0000569unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
570 VisitStmt(S);
571 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
572 S->setBody(StmtStack.back());
573 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
574 return 2;
575}
576
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000577unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
578 VisitStmt(S);
579 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
580 S->setBody(StmtStack.back());
581 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
582 return 2;
583}
584
585unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
586 VisitStmt(S);
587 S->setInit(StmtStack[StmtStack.size() - 4]);
588 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
589 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
590 S->setBody(StmtStack.back());
591 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 return 4;
593}
594
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000595unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
596 VisitStmt(S);
597 Reader.SetLabelOf(S, Record[Idx++]);
598 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
599 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
600 return 0;
601}
602
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000603unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
604 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000605 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000606 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
607 return 1;
608}
609
Douglas Gregora6b503f2009-04-17 00:16:09 +0000610unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
611 VisitStmt(S);
612 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
613 return 0;
614}
615
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000616unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
617 VisitStmt(S);
618 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
619 return 0;
620}
621
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000622unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
623 VisitStmt(S);
624 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
625 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
626 return 1;
627}
628
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000629unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
630 VisitStmt(S);
631 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
632 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
633
634 if (Idx + 1 == Record.size()) {
635 // Single declaration
636 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
637 } else {
638 llvm::SmallVector<Decl *, 16> Decls;
639 Decls.reserve(Record.size() - Idx);
640 for (unsigned N = Record.size(); Idx != N; ++Idx)
641 Decls.push_back(Reader.GetDecl(Record[Idx]));
642 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
643 &Decls[0], Decls.size())));
644 }
645 return 0;
646}
647
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000648unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
649 VisitStmt(S);
650 unsigned NumOutputs = Record[Idx++];
651 unsigned NumInputs = Record[Idx++];
652 unsigned NumClobbers = Record[Idx++];
653 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
654 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
655 S->setVolatile(Record[Idx++]);
656 S->setSimple(Record[Idx++]);
657
658 unsigned StackIdx
659 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
660 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
661
662 // Outputs and inputs
663 llvm::SmallVector<std::string, 16> Names;
664 llvm::SmallVector<StringLiteral*, 16> Constraints;
665 llvm::SmallVector<Stmt*, 16> Exprs;
666 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
667 Names.push_back(Reader.ReadString(Record, Idx));
668 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
669 Exprs.push_back(StmtStack[StackIdx++]);
670 }
671 S->setOutputsAndInputs(NumOutputs, NumInputs,
672 &Names[0], &Constraints[0], &Exprs[0]);
673
674 // Constraints
675 llvm::SmallVector<StringLiteral*, 16> Clobbers;
676 for (unsigned I = 0; I != NumClobbers; ++I)
677 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
678 S->setClobbers(&Clobbers[0], NumClobbers);
679
680 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
681 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
682}
683
Douglas Gregora151ba42009-04-14 23:32:43 +0000684unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000685 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000686 E->setType(Reader.GetType(Record[Idx++]));
687 E->setTypeDependent(Record[Idx++]);
688 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000689 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000690 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000691}
692
Douglas Gregora151ba42009-04-14 23:32:43 +0000693unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000694 VisitExpr(E);
695 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
696 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000697 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000698}
699
Douglas Gregora151ba42009-04-14 23:32:43 +0000700unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000701 VisitExpr(E);
702 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
703 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000704 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000705}
706
Douglas Gregora151ba42009-04-14 23:32:43 +0000707unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000708 VisitExpr(E);
709 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
710 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000711 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000712}
713
Douglas Gregora151ba42009-04-14 23:32:43 +0000714unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000715 VisitExpr(E);
716 E->setValue(Reader.ReadAPFloat(Record, Idx));
717 E->setExact(Record[Idx++]);
718 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000719 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000720}
721
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000722unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
723 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000724 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000725 return 1;
726}
727
Douglas Gregor596e0932009-04-15 16:35:07 +0000728unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
729 VisitExpr(E);
730 unsigned Len = Record[Idx++];
731 assert(Record[Idx] == E->getNumConcatenated() &&
732 "Wrong number of concatenated tokens!");
733 ++Idx;
734 E->setWide(Record[Idx++]);
735
736 // Read string data
737 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
738 E->setStrData(Reader.getContext(), &Str[0], Len);
739 Idx += Len;
740
741 // Read source locations
742 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
743 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
744
745 return 0;
746}
747
Douglas Gregora151ba42009-04-14 23:32:43 +0000748unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000749 VisitExpr(E);
750 E->setValue(Record[Idx++]);
751 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
752 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000753 return 0;
754}
755
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000756unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
757 VisitExpr(E);
758 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
759 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000760 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000761 return 1;
762}
763
Douglas Gregor12d74052009-04-15 15:58:59 +0000764unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
765 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000766 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000767 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
768 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
769 return 1;
770}
771
772unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
773 VisitExpr(E);
774 E->setSizeof(Record[Idx++]);
775 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000776 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000777 ++Idx;
778 } else {
779 E->setArgument(Reader.GetType(Record[Idx++]));
780 }
781 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
782 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
783 return E->isArgumentType()? 0 : 1;
784}
785
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000786unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
787 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000788 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
789 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000790 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
791 return 2;
792}
793
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000794unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
795 VisitExpr(E);
796 E->setNumArgs(Reader.getContext(), Record[Idx++]);
797 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000798 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000799 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000801 return E->getNumArgs() + 1;
802}
803
804unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
805 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000806 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000807 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
808 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
809 E->setArrow(Record[Idx++]);
810 return 1;
811}
812
Douglas Gregora151ba42009-04-14 23:32:43 +0000813unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
814 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000815 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000816 return 1;
817}
818
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000819unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
820 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000821 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
822 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000823 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
824 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
825 return 2;
826}
827
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000828unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
829 VisitBinaryOperator(E);
830 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
831 E->setComputationResultType(Reader.GetType(Record[Idx++]));
832 return 2;
833}
834
835unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
836 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000837 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
838 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
839 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000840 return 3;
841}
842
Douglas Gregora151ba42009-04-14 23:32:43 +0000843unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
844 VisitCastExpr(E);
845 E->setLvalueCast(Record[Idx++]);
846 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000847}
848
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000849unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
850 VisitCastExpr(E);
851 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
852 return 1;
853}
854
855unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
856 VisitExplicitCastExpr(E);
857 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
858 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
859 return 1;
860}
861
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000862unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
863 VisitExpr(E);
864 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000865 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000866 E->setFileScope(Record[Idx++]);
867 return 1;
868}
869
Douglas Gregorec0b8292009-04-15 23:02:49 +0000870unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
871 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000872 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000873 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
874 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
875 return 1;
876}
877
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000878unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
879 VisitExpr(E);
880 unsigned NumInits = Record[Idx++];
881 E->reserveInits(NumInits);
882 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000883 E->updateInit(I,
884 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
885 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000886 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
887 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
888 E->setInitializedFieldInUnion(
889 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
890 E->sawArrayRangeDesignator(Record[Idx++]);
891 return NumInits + 1;
892}
893
894unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
895 typedef DesignatedInitExpr::Designator Designator;
896
897 VisitExpr(E);
898 unsigned NumSubExprs = Record[Idx++];
899 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
900 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000901 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000902 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
903 E->setGNUSyntax(Record[Idx++]);
904
905 llvm::SmallVector<Designator, 4> Designators;
906 while (Idx < Record.size()) {
907 switch ((pch::DesignatorTypes)Record[Idx++]) {
908 case pch::DESIG_FIELD_DECL: {
909 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
910 SourceLocation DotLoc
911 = SourceLocation::getFromRawEncoding(Record[Idx++]);
912 SourceLocation FieldLoc
913 = SourceLocation::getFromRawEncoding(Record[Idx++]);
914 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
915 FieldLoc));
916 Designators.back().setField(Field);
917 break;
918 }
919
920 case pch::DESIG_FIELD_NAME: {
921 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
922 SourceLocation DotLoc
923 = SourceLocation::getFromRawEncoding(Record[Idx++]);
924 SourceLocation FieldLoc
925 = SourceLocation::getFromRawEncoding(Record[Idx++]);
926 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
927 break;
928 }
929
930 case pch::DESIG_ARRAY: {
931 unsigned Index = Record[Idx++];
932 SourceLocation LBracketLoc
933 = SourceLocation::getFromRawEncoding(Record[Idx++]);
934 SourceLocation RBracketLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
937 break;
938 }
939
940 case pch::DESIG_ARRAY_RANGE: {
941 unsigned Index = Record[Idx++];
942 SourceLocation LBracketLoc
943 = SourceLocation::getFromRawEncoding(Record[Idx++]);
944 SourceLocation EllipsisLoc
945 = SourceLocation::getFromRawEncoding(Record[Idx++]);
946 SourceLocation RBracketLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
949 RBracketLoc));
950 break;
951 }
952 }
953 }
954 E->setDesignators(&Designators[0], Designators.size());
955
956 return NumSubExprs;
957}
958
959unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
960 VisitExpr(E);
961 return 0;
962}
963
Douglas Gregorec0b8292009-04-15 23:02:49 +0000964unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
965 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000966 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000967 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
968 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
969 return 1;
970}
971
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000972unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
973 VisitExpr(E);
974 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
975 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
976 Reader.SetLabelOf(E, Record[Idx++]);
977 return 0;
978}
979
Douglas Gregoreca12f62009-04-17 19:05:30 +0000980unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
981 VisitExpr(E);
982 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
983 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
984 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
985 return 1;
986}
987
Douglas Gregor209d4622009-04-15 23:33:31 +0000988unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
989 VisitExpr(E);
990 E->setArgType1(Reader.GetType(Record[Idx++]));
991 E->setArgType2(Reader.GetType(Record[Idx++]));
992 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
994 return 0;
995}
996
997unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
998 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000999 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1000 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1001 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001002 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 return 3;
1005}
1006
1007unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1008 VisitExpr(E);
1009 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1010 return 0;
1011}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001012
Douglas Gregor725e94b2009-04-16 00:01:45 +00001013unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1014 VisitExpr(E);
1015 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001016 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001017 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1018 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1019 return NumExprs;
1020}
1021
Douglas Gregore246b742009-04-17 19:21:43 +00001022unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1023 VisitExpr(E);
1024 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1025 E->setHasBlockDeclRefExprs(Record[Idx++]);
1026 return 0;
1027}
1028
Douglas Gregor725e94b2009-04-16 00:01:45 +00001029unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1030 VisitExpr(E);
1031 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1032 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1033 E->setByRef(Record[Idx++]);
1034 return 0;
1035}
1036
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001037//===----------------------------------------------------------------------===//
1038// Objective-C Expressions and Statements
1039
1040unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1041 VisitExpr(E);
1042 E->setString(cast<StringLiteral>(StmtStack.back()));
1043 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1044 return 1;
1045}
1046
Chris Lattner80f83c62009-04-22 05:57:30 +00001047unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1048 VisitExpr(E);
1049 E->setEncodedType(Reader.GetType(Record[Idx++]));
1050 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1051 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1052 return 0;
1053}
1054
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001055unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1056 VisitExpr(E);
1057 // FIXME: Selectors.
1058 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1059 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1060 return 0;
1061}
1062
1063unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1064 VisitExpr(E);
1065 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1066 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1067 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 return 0;
1069}
1070
Chris Lattner80f83c62009-04-22 05:57:30 +00001071
Douglas Gregorc713da92009-04-21 22:25:48 +00001072//===----------------------------------------------------------------------===//
1073// PCH reader implementation
1074//===----------------------------------------------------------------------===//
1075
1076namespace {
1077class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1078 PCHReader &Reader;
1079
1080 // If we know the IdentifierInfo in advance, it is here and we will
1081 // not build a new one. Used when deserializing information about an
1082 // identifier that was constructed before the PCH file was read.
1083 IdentifierInfo *KnownII;
1084
1085public:
1086 typedef IdentifierInfo * data_type;
1087
1088 typedef const std::pair<const char*, unsigned> external_key_type;
1089
1090 typedef external_key_type internal_key_type;
1091
1092 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1093 : Reader(Reader), KnownII(II) { }
1094
1095 static bool EqualKey(const internal_key_type& a,
1096 const internal_key_type& b) {
1097 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1098 : false;
1099 }
1100
1101 static unsigned ComputeHash(const internal_key_type& a) {
1102 return BernsteinHash(a.first, a.second);
1103 }
1104
1105 // This hopefully will just get inlined and removed by the optimizer.
1106 static const internal_key_type&
1107 GetInternalKey(const external_key_type& x) { return x; }
1108
1109 static std::pair<unsigned, unsigned>
1110 ReadKeyDataLength(const unsigned char*& d) {
1111 using namespace clang::io;
1112 unsigned KeyLen = ReadUnalignedLE16(d);
1113 unsigned DataLen = ReadUnalignedLE16(d);
1114 return std::make_pair(KeyLen, DataLen);
1115 }
1116
1117 static std::pair<const char*, unsigned>
1118 ReadKey(const unsigned char* d, unsigned n) {
1119 assert(n >= 2 && d[n-1] == '\0');
1120 return std::make_pair((const char*) d, n-1);
1121 }
1122
1123 IdentifierInfo *ReadData(const internal_key_type& k,
1124 const unsigned char* d,
1125 unsigned DataLen) {
1126 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001127 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001128 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1129 Bits >>= 1;
1130 bool Poisoned = Bits & 0x01;
1131 Bits >>= 1;
1132 bool ExtensionToken = Bits & 0x01;
1133 Bits >>= 1;
1134 bool hasMacroDefinition = Bits & 0x01;
1135 Bits >>= 1;
1136 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1137 Bits >>= 10;
1138 unsigned TokenID = Bits & 0xFF;
1139 Bits >>= 8;
1140
Douglas Gregorc713da92009-04-21 22:25:48 +00001141 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001142 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001143 DataLen -= 8;
1144
1145 // Build the IdentifierInfo itself and link the identifier ID with
1146 // the new IdentifierInfo.
1147 IdentifierInfo *II = KnownII;
1148 if (!II)
1149 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1150 k.first, k.first + k.second);
1151 Reader.SetIdentifierInfo(ID, II);
1152
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001153 // Set or check the various bits in the IdentifierInfo structure.
1154 // FIXME: Load token IDs lazily, too?
1155 assert((unsigned)II->getTokenID() == TokenID &&
1156 "Incorrect token ID loaded");
1157 (void)TokenID;
1158 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1159 assert(II->isExtensionToken() == ExtensionToken &&
1160 "Incorrect extension token flag");
1161 (void)ExtensionToken;
1162 II->setIsPoisoned(Poisoned);
1163 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1164 "Incorrect C++ operator keyword flag");
1165 (void)CPlusPlusOperatorKeyword;
1166
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001167 // If this identifier is a macro, deserialize the macro
1168 // definition.
1169 if (hasMacroDefinition) {
1170 uint32_t Offset = ReadUnalignedLE64(d);
1171 Reader.ReadMacroRecord(Offset);
1172 DataLen -= 8;
1173 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001174
1175 // Read all of the declarations visible at global scope with this
1176 // name.
1177 Sema *SemaObj = Reader.getSema();
1178 while (DataLen > 0) {
1179 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001180 if (SemaObj) {
1181 // Introduce this declaration into the translation-unit scope
1182 // and add it to the declaration chain for this identifier, so
1183 // that (unqualified) name lookup will find it.
1184 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1185 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1186 } else {
1187 // Queue this declaration so that it will be added to the
1188 // translation unit scope and identifier's declaration chain
1189 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001190 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001191 }
1192
1193 DataLen -= 4;
1194 }
1195 return II;
1196 }
1197};
1198
1199} // end anonymous namespace
1200
1201/// \brief The on-disk hash table used to contain information about
1202/// all of the identifiers in the program.
1203typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1204 PCHIdentifierLookupTable;
1205
Douglas Gregorc34897d2009-04-09 22:27:44 +00001206// FIXME: use the diagnostics machinery
1207static bool Error(const char *Str) {
1208 std::fprintf(stderr, "%s\n", Str);
1209 return true;
1210}
1211
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001212/// \brief Check the contents of the predefines buffer against the
1213/// contents of the predefines buffer used to build the PCH file.
1214///
1215/// The contents of the two predefines buffers should be the same. If
1216/// not, then some command-line option changed the preprocessor state
1217/// and we must reject the PCH file.
1218///
1219/// \param PCHPredef The start of the predefines buffer in the PCH
1220/// file.
1221///
1222/// \param PCHPredefLen The length of the predefines buffer in the PCH
1223/// file.
1224///
1225/// \param PCHBufferID The FileID for the PCH predefines buffer.
1226///
1227/// \returns true if there was a mismatch (in which case the PCH file
1228/// should be ignored), or false otherwise.
1229bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1230 unsigned PCHPredefLen,
1231 FileID PCHBufferID) {
1232 const char *Predef = PP.getPredefines().c_str();
1233 unsigned PredefLen = PP.getPredefines().size();
1234
1235 // If the two predefines buffers compare equal, we're done!.
1236 if (PredefLen == PCHPredefLen &&
1237 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1238 return false;
1239
1240 // The predefines buffers are different. Produce a reasonable
1241 // diagnostic showing where they are different.
1242
1243 // The source locations (potentially in the two different predefines
1244 // buffers)
1245 SourceLocation Loc1, Loc2;
1246 SourceManager &SourceMgr = PP.getSourceManager();
1247
1248 // Create a source buffer for our predefines string, so
1249 // that we can build a diagnostic that points into that
1250 // source buffer.
1251 FileID BufferID;
1252 if (Predef && Predef[0]) {
1253 llvm::MemoryBuffer *Buffer
1254 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1255 "<built-in>");
1256 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1257 }
1258
1259 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1260 std::pair<const char *, const char *> Locations
1261 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1262
1263 if (Locations.first != Predef + MinLen) {
1264 // We found the location in the two buffers where there is a
1265 // difference. Form source locations to point there (in both
1266 // buffers).
1267 unsigned Offset = Locations.first - Predef;
1268 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1269 .getFileLocWithOffset(Offset);
1270 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1271 .getFileLocWithOffset(Offset);
1272 } else if (PredefLen > PCHPredefLen) {
1273 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1274 .getFileLocWithOffset(MinLen);
1275 } else {
1276 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1277 .getFileLocWithOffset(MinLen);
1278 }
1279
1280 Diag(Loc1, diag::warn_pch_preprocessor);
1281 if (Loc2.isValid())
1282 Diag(Loc2, diag::note_predef_in_pch);
1283 Diag(diag::note_ignoring_pch) << FileName;
1284 return true;
1285}
1286
Douglas Gregor635f97f2009-04-13 16:31:14 +00001287/// \brief Read the line table in the source manager block.
1288/// \returns true if ther was an error.
1289static bool ParseLineTable(SourceManager &SourceMgr,
1290 llvm::SmallVectorImpl<uint64_t> &Record) {
1291 unsigned Idx = 0;
1292 LineTableInfo &LineTable = SourceMgr.getLineTable();
1293
1294 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001295 std::map<int, int> FileIDs;
1296 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001297 // Extract the file name
1298 unsigned FilenameLen = Record[Idx++];
1299 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1300 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001301 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1302 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001303 }
1304
1305 // Parse the line entries
1306 std::vector<LineEntry> Entries;
1307 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001308 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001309
1310 // Extract the line entries
1311 unsigned NumEntries = Record[Idx++];
1312 Entries.clear();
1313 Entries.reserve(NumEntries);
1314 for (unsigned I = 0; I != NumEntries; ++I) {
1315 unsigned FileOffset = Record[Idx++];
1316 unsigned LineNo = Record[Idx++];
1317 int FilenameID = Record[Idx++];
1318 SrcMgr::CharacteristicKind FileKind
1319 = (SrcMgr::CharacteristicKind)Record[Idx++];
1320 unsigned IncludeOffset = Record[Idx++];
1321 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1322 FileKind, IncludeOffset));
1323 }
1324 LineTable.AddEntry(FID, Entries);
1325 }
1326
1327 return false;
1328}
1329
Douglas Gregorab1cef72009-04-10 03:52:48 +00001330/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001331PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001332 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001333 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1334 Error("Malformed source manager block record");
1335 return Failure;
1336 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001337
1338 SourceManager &SourceMgr = Context.getSourceManager();
1339 RecordData Record;
1340 while (true) {
1341 unsigned Code = Stream.ReadCode();
1342 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001343 if (Stream.ReadBlockEnd()) {
1344 Error("Error at end of Source Manager block");
1345 return Failure;
1346 }
1347
1348 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001349 }
1350
1351 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1352 // No known subblocks, always skip them.
1353 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001354 if (Stream.SkipBlock()) {
1355 Error("Malformed block record");
1356 return Failure;
1357 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001358 continue;
1359 }
1360
1361 if (Code == llvm::bitc::DEFINE_ABBREV) {
1362 Stream.ReadAbbrevRecord();
1363 continue;
1364 }
1365
1366 // Read a record.
1367 const char *BlobStart;
1368 unsigned BlobLen;
1369 Record.clear();
1370 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1371 default: // Default behavior: ignore.
1372 break;
1373
1374 case pch::SM_SLOC_FILE_ENTRY: {
1375 // FIXME: We would really like to delay the creation of this
1376 // FileEntry until it is actually required, e.g., when producing
1377 // a diagnostic with a source location in this file.
1378 const FileEntry *File
1379 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1380 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001381 FileID ID = SourceMgr.createFileID(File,
1382 SourceLocation::getFromRawEncoding(Record[1]),
1383 (CharacteristicKind)Record[2]);
1384 if (Record[3])
1385 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1386 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001387 break;
1388 }
1389
1390 case pch::SM_SLOC_BUFFER_ENTRY: {
1391 const char *Name = BlobStart;
1392 unsigned Code = Stream.ReadCode();
1393 Record.clear();
1394 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1395 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001396 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001397 llvm::MemoryBuffer *Buffer
1398 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1399 BlobStart + BlobLen - 1,
1400 Name);
1401 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1402
1403 if (strcmp(Name, "<built-in>") == 0
1404 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1405 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001406 break;
1407 }
1408
1409 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1410 SourceLocation SpellingLoc
1411 = SourceLocation::getFromRawEncoding(Record[1]);
1412 SourceMgr.createInstantiationLoc(
1413 SpellingLoc,
1414 SourceLocation::getFromRawEncoding(Record[2]),
1415 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001416 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001417 break;
1418 }
1419
Chris Lattnere1be6022009-04-14 23:22:57 +00001420 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001421 if (ParseLineTable(SourceMgr, Record))
1422 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001423 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001424 }
1425 }
1426}
1427
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001428void PCHReader::ReadMacroRecord(uint64_t Offset) {
1429 // Keep track of where we are in the stream, then jump back there
1430 // after reading this macro.
1431 SavedStreamPosition SavedPosition(Stream);
1432
1433 Stream.JumpToBit(Offset);
1434 RecordData Record;
1435 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1436 MacroInfo *Macro = 0;
1437 while (true) {
1438 unsigned Code = Stream.ReadCode();
1439 switch (Code) {
1440 case llvm::bitc::END_BLOCK:
1441 return;
1442
1443 case llvm::bitc::ENTER_SUBBLOCK:
1444 // No known subblocks, always skip them.
1445 Stream.ReadSubBlockID();
1446 if (Stream.SkipBlock()) {
1447 Error("Malformed block record");
1448 return;
1449 }
1450 continue;
1451
1452 case llvm::bitc::DEFINE_ABBREV:
1453 Stream.ReadAbbrevRecord();
1454 continue;
1455 default: break;
1456 }
1457
1458 // Read a record.
1459 Record.clear();
1460 pch::PreprocessorRecordTypes RecType =
1461 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1462 switch (RecType) {
1463 case pch::PP_COUNTER_VALUE:
1464 // Skip this record.
1465 break;
1466
1467 case pch::PP_MACRO_OBJECT_LIKE:
1468 case pch::PP_MACRO_FUNCTION_LIKE: {
1469 // If we already have a macro, that means that we've hit the end
1470 // of the definition of the macro we were looking for. We're
1471 // done.
1472 if (Macro)
1473 return;
1474
1475 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1476 if (II == 0) {
1477 Error("Macro must have a name");
1478 return;
1479 }
1480 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1481 bool isUsed = Record[2];
1482
1483 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1484 MI->setIsUsed(isUsed);
1485
1486 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1487 // Decode function-like macro info.
1488 bool isC99VarArgs = Record[3];
1489 bool isGNUVarArgs = Record[4];
1490 MacroArgs.clear();
1491 unsigned NumArgs = Record[5];
1492 for (unsigned i = 0; i != NumArgs; ++i)
1493 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1494
1495 // Install function-like macro info.
1496 MI->setIsFunctionLike();
1497 if (isC99VarArgs) MI->setIsC99Varargs();
1498 if (isGNUVarArgs) MI->setIsGNUVarargs();
1499 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1500 PP.getPreprocessorAllocator());
1501 }
1502
1503 // Finally, install the macro.
1504 PP.setMacroInfo(II, MI);
1505
1506 // Remember that we saw this macro last so that we add the tokens that
1507 // form its body to it.
1508 Macro = MI;
1509 ++NumMacrosRead;
1510 break;
1511 }
1512
1513 case pch::PP_TOKEN: {
1514 // If we see a TOKEN before a PP_MACRO_*, then the file is
1515 // erroneous, just pretend we didn't see this.
1516 if (Macro == 0) break;
1517
1518 Token Tok;
1519 Tok.startToken();
1520 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1521 Tok.setLength(Record[1]);
1522 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1523 Tok.setIdentifierInfo(II);
1524 Tok.setKind((tok::TokenKind)Record[3]);
1525 Tok.setFlag((Token::TokenFlags)Record[4]);
1526 Macro->AddTokenToBody(Tok);
1527 break;
1528 }
1529 }
1530 }
1531}
1532
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001533bool PCHReader::ReadPreprocessorBlock() {
1534 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1535 return Error("Malformed preprocessor block record");
1536
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001537 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001538 while (true) {
1539 unsigned Code = Stream.ReadCode();
1540 switch (Code) {
1541 case llvm::bitc::END_BLOCK:
1542 if (Stream.ReadBlockEnd())
1543 return Error("Error at end of preprocessor block");
1544 return false;
1545
1546 case llvm::bitc::ENTER_SUBBLOCK:
1547 // No known subblocks, always skip them.
1548 Stream.ReadSubBlockID();
1549 if (Stream.SkipBlock())
1550 return Error("Malformed block record");
1551 continue;
1552
1553 case llvm::bitc::DEFINE_ABBREV:
1554 Stream.ReadAbbrevRecord();
1555 continue;
1556 default: break;
1557 }
1558
1559 // Read a record.
1560 Record.clear();
1561 pch::PreprocessorRecordTypes RecType =
1562 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1563 switch (RecType) {
1564 default: // Default behavior: ignore unknown records.
1565 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001566 case pch::PP_COUNTER_VALUE:
1567 if (!Record.empty())
1568 PP.setCounterValue(Record[0]);
1569 break;
1570
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001571 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001572 case pch::PP_MACRO_FUNCTION_LIKE:
1573 case pch::PP_TOKEN:
1574 // Once we've hit a macro definition or a token, we're done.
1575 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001576 }
1577 }
1578}
1579
Douglas Gregorc713da92009-04-21 22:25:48 +00001580PCHReader::PCHReadResult
1581PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001582 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1583 Error("Malformed block record");
1584 return Failure;
1585 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001586
1587 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001588 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001589 while (!Stream.AtEndOfStream()) {
1590 unsigned Code = Stream.ReadCode();
1591 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001592 if (Stream.ReadBlockEnd()) {
1593 Error("Error at end of module block");
1594 return Failure;
1595 }
Chris Lattner29241862009-04-11 21:15:38 +00001596
Douglas Gregor179cfb12009-04-10 20:39:37 +00001597 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001598 }
1599
1600 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1601 switch (Stream.ReadSubBlockID()) {
1602 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1603 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1604 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001605 if (Stream.SkipBlock()) {
1606 Error("Malformed block record");
1607 return Failure;
1608 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001609 break;
1610
Chris Lattner29241862009-04-11 21:15:38 +00001611 case pch::PREPROCESSOR_BLOCK_ID:
1612 // Skip the preprocessor block for now, but remember where it is. We
1613 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001614 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001615 Error("Multiple preprocessor blocks found.");
1616 return Failure;
1617 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001618 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001619 if (Stream.SkipBlock()) {
1620 Error("Malformed block record");
1621 return Failure;
1622 }
1623 break;
1624
Douglas Gregorab1cef72009-04-10 03:52:48 +00001625 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001626 switch (ReadSourceManagerBlock()) {
1627 case Success:
1628 break;
1629
1630 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001631 Error("Malformed source manager block");
1632 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001633
1634 case IgnorePCH:
1635 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001636 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001637 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001638 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001639 continue;
1640 }
1641
1642 if (Code == llvm::bitc::DEFINE_ABBREV) {
1643 Stream.ReadAbbrevRecord();
1644 continue;
1645 }
1646
1647 // Read and process a record.
1648 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001649 const char *BlobStart = 0;
1650 unsigned BlobLen = 0;
1651 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1652 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001653 default: // Default behavior: ignore.
1654 break;
1655
1656 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001657 if (!TypeOffsets.empty()) {
1658 Error("Duplicate TYPE_OFFSET record in PCH file");
1659 return Failure;
1660 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001661 TypeOffsets.swap(Record);
1662 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1663 break;
1664
1665 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001666 if (!DeclOffsets.empty()) {
1667 Error("Duplicate DECL_OFFSET record in PCH file");
1668 return Failure;
1669 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001670 DeclOffsets.swap(Record);
1671 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1672 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001673
1674 case pch::LANGUAGE_OPTIONS:
1675 if (ParseLanguageOptions(Record))
1676 return IgnorePCH;
1677 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001678
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001679 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001680 std::string TargetTriple(BlobStart, BlobLen);
1681 if (TargetTriple != Context.Target.getTargetTriple()) {
1682 Diag(diag::warn_pch_target_triple)
1683 << TargetTriple << Context.Target.getTargetTriple();
1684 Diag(diag::note_ignoring_pch) << FileName;
1685 return IgnorePCH;
1686 }
1687 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001688 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001689
1690 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001691 IdentifierTableData = BlobStart;
1692 IdentifierLookupTable
1693 = PCHIdentifierLookupTable::Create(
1694 (const unsigned char *)IdentifierTableData + Record[0],
1695 (const unsigned char *)IdentifierTableData,
1696 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001697 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001698 break;
1699
1700 case pch::IDENTIFIER_OFFSET:
1701 if (!IdentifierData.empty()) {
1702 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1703 return Failure;
1704 }
1705 IdentifierData.swap(Record);
1706#ifndef NDEBUG
1707 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1708 if ((IdentifierData[I] & 0x01) == 0) {
1709 Error("Malformed identifier table in the precompiled header");
1710 return Failure;
1711 }
1712 }
1713#endif
1714 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001715
1716 case pch::EXTERNAL_DEFINITIONS:
1717 if (!ExternalDefinitions.empty()) {
1718 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1719 return Failure;
1720 }
1721 ExternalDefinitions.swap(Record);
1722 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001723
Douglas Gregore01ad442009-04-18 05:55:16 +00001724 case pch::SPECIAL_TYPES:
1725 SpecialTypes.swap(Record);
1726 break;
1727
Douglas Gregor456e0952009-04-17 22:13:46 +00001728 case pch::STATISTICS:
1729 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001730 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001731 TotalLexicalDeclContexts = Record[2];
1732 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001733 break;
1734
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001735 case pch::TENTATIVE_DEFINITIONS:
1736 if (!TentativeDefinitions.empty()) {
1737 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1738 return Failure;
1739 }
1740 TentativeDefinitions.swap(Record);
1741 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001742
1743 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1744 if (!LocallyScopedExternalDecls.empty()) {
1745 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1746 return Failure;
1747 }
1748 LocallyScopedExternalDecls.swap(Record);
1749 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001750 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001751 }
1752
Douglas Gregor179cfb12009-04-10 20:39:37 +00001753 Error("Premature end of bitstream");
1754 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001755}
1756
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001757PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001758 // Set the PCH file name.
1759 this->FileName = FileName;
1760
Douglas Gregorc34897d2009-04-09 22:27:44 +00001761 // Open the PCH file.
1762 std::string ErrStr;
1763 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001764 if (!Buffer) {
1765 Error(ErrStr.c_str());
1766 return IgnorePCH;
1767 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001768
1769 // Initialize the stream
1770 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1771 (const unsigned char *)Buffer->getBufferEnd());
1772
1773 // Sniff for the signature.
1774 if (Stream.Read(8) != 'C' ||
1775 Stream.Read(8) != 'P' ||
1776 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001777 Stream.Read(8) != 'H') {
1778 Error("Not a PCH file");
1779 return IgnorePCH;
1780 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781
1782 // We expect a number of well-defined blocks, though we don't necessarily
1783 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001784 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001785 while (!Stream.AtEndOfStream()) {
1786 unsigned Code = Stream.ReadCode();
1787
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001788 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1789 Error("Invalid record at top-level");
1790 return Failure;
1791 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001792
1793 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001794
Douglas Gregorc34897d2009-04-09 22:27:44 +00001795 // We only know the PCH subblock ID.
1796 switch (BlockID) {
1797 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001798 if (Stream.ReadBlockInfoBlock()) {
1799 Error("Malformed BlockInfoBlock");
1800 return Failure;
1801 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001802 break;
1803 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001804 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001805 case Success:
1806 break;
1807
1808 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001809 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001810
1811 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001812 // FIXME: We could consider reading through to the end of this
1813 // PCH block, skipping subblocks, to see if there are other
1814 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001815 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001816 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001817 break;
1818 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001819 if (Stream.SkipBlock()) {
1820 Error("Malformed block record");
1821 return Failure;
1822 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001823 break;
1824 }
1825 }
1826
1827 // Load the translation unit declaration
1828 ReadDeclRecord(DeclOffsets[0], 0);
1829
Douglas Gregorc713da92009-04-21 22:25:48 +00001830 // Initialization of builtins and library builtins occurs before the
1831 // PCH file is read, so there may be some identifiers that were
1832 // loaded into the IdentifierTable before we intercepted the
1833 // creation of identifiers. Iterate through the list of known
1834 // identifiers and determine whether we have to establish
1835 // preprocessor definitions or top-level identifier declaration
1836 // chains for those identifiers.
1837 //
1838 // We copy the IdentifierInfo pointers to a small vector first,
1839 // since de-serializing declarations or macro definitions can add
1840 // new entries into the identifier table, invalidating the
1841 // iterators.
1842 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1843 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1844 IdEnd = PP.getIdentifierTable().end();
1845 Id != IdEnd; ++Id)
1846 Identifiers.push_back(Id->second);
1847 PCHIdentifierLookupTable *IdTable
1848 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1849 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1850 IdentifierInfo *II = Identifiers[I];
1851 // Look in the on-disk hash table for an entry for
1852 PCHIdentifierLookupTrait Info(*this, II);
1853 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1854 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1855 if (Pos == IdTable->end())
1856 continue;
1857
1858 // Dereferencing the iterator has the effect of populating the
1859 // IdentifierInfo node with the various declarations it needs.
1860 (void)*Pos;
1861 }
1862
Douglas Gregore01ad442009-04-18 05:55:16 +00001863 // Load the special types.
1864 Context.setBuiltinVaListType(
1865 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1866
Douglas Gregorc713da92009-04-21 22:25:48 +00001867 // If we saw the preprocessor block, read it now.
1868 if (PreprocessorBlockOffset) {
1869 SavedStreamPosition SavedPos(Stream);
1870 Stream.JumpToBit(PreprocessorBlockOffset);
1871 if (ReadPreprocessorBlock()) {
1872 Error("Malformed preprocessor block");
1873 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001874 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001875 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001876
Douglas Gregorc713da92009-04-21 22:25:48 +00001877 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001878}
1879
Douglas Gregor179cfb12009-04-10 20:39:37 +00001880/// \brief Parse the record that corresponds to a LangOptions data
1881/// structure.
1882///
1883/// This routine compares the language options used to generate the
1884/// PCH file against the language options set for the current
1885/// compilation. For each option, we classify differences between the
1886/// two compiler states as either "benign" or "important". Benign
1887/// differences don't matter, and we accept them without complaint
1888/// (and without modifying the language options). Differences between
1889/// the states for important options cause the PCH file to be
1890/// unusable, so we emit a warning and return true to indicate that
1891/// there was an error.
1892///
1893/// \returns true if the PCH file is unacceptable, false otherwise.
1894bool PCHReader::ParseLanguageOptions(
1895 const llvm::SmallVectorImpl<uint64_t> &Record) {
1896 const LangOptions &LangOpts = Context.getLangOptions();
1897#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1898#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1899 if (Record[Idx] != LangOpts.Option) { \
1900 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1901 Diag(diag::note_ignoring_pch) << FileName; \
1902 return true; \
1903 } \
1904 ++Idx
1905
1906 unsigned Idx = 0;
1907 PARSE_LANGOPT_BENIGN(Trigraphs);
1908 PARSE_LANGOPT_BENIGN(BCPLComment);
1909 PARSE_LANGOPT_BENIGN(DollarIdents);
1910 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1911 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1912 PARSE_LANGOPT_BENIGN(ImplicitInt);
1913 PARSE_LANGOPT_BENIGN(Digraphs);
1914 PARSE_LANGOPT_BENIGN(HexFloats);
1915 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1916 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1917 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1918 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1919 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1920 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1921 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1922 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1923 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1924 PARSE_LANGOPT_BENIGN(PascalStrings);
1925 PARSE_LANGOPT_BENIGN(Boolean);
1926 PARSE_LANGOPT_BENIGN(WritableStrings);
1927 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1928 diag::warn_pch_lax_vector_conversions);
1929 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1930 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1931 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1932 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1933 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1934 diag::warn_pch_thread_safe_statics);
1935 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1936 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1937 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1938 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1939 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1940 diag::warn_pch_heinous_extensions);
1941 // FIXME: Most of the options below are benign if the macro wasn't
1942 // used. Unfortunately, this means that a PCH compiled without
1943 // optimization can't be used with optimization turned on, even
1944 // though the only thing that changes is whether __OPTIMIZE__ was
1945 // defined... but if __OPTIMIZE__ never showed up in the header, it
1946 // doesn't matter. We could consider making this some special kind
1947 // of check.
1948 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1949 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1950 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1951 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1952 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1953 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1954 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1955 Diag(diag::warn_pch_gc_mode)
1956 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1957 Diag(diag::note_ignoring_pch) << FileName;
1958 return true;
1959 }
1960 ++Idx;
1961 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1962 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1963#undef PARSE_LANGOPT_IRRELEVANT
1964#undef PARSE_LANGOPT_BENIGN
1965
1966 return false;
1967}
1968
Douglas Gregorc34897d2009-04-09 22:27:44 +00001969/// \brief Read and return the type at the given offset.
1970///
1971/// This routine actually reads the record corresponding to the type
1972/// at the given offset in the bitstream. It is a helper routine for
1973/// GetType, which deals with reading type IDs.
1974QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001975 // Keep track of where we are in the stream, then jump back there
1976 // after reading this type.
1977 SavedStreamPosition SavedPosition(Stream);
1978
Douglas Gregorc34897d2009-04-09 22:27:44 +00001979 Stream.JumpToBit(Offset);
1980 RecordData Record;
1981 unsigned Code = Stream.ReadCode();
1982 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001983 case pch::TYPE_EXT_QUAL: {
1984 assert(Record.size() == 3 &&
1985 "Incorrect encoding of extended qualifier type");
1986 QualType Base = GetType(Record[0]);
1987 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1988 unsigned AddressSpace = Record[2];
1989
1990 QualType T = Base;
1991 if (GCAttr != QualType::GCNone)
1992 T = Context.getObjCGCQualType(T, GCAttr);
1993 if (AddressSpace)
1994 T = Context.getAddrSpaceQualType(T, AddressSpace);
1995 return T;
1996 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001997
Douglas Gregorc34897d2009-04-09 22:27:44 +00001998 case pch::TYPE_FIXED_WIDTH_INT: {
1999 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2000 return Context.getFixedWidthIntType(Record[0], Record[1]);
2001 }
2002
2003 case pch::TYPE_COMPLEX: {
2004 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2005 QualType ElemType = GetType(Record[0]);
2006 return Context.getComplexType(ElemType);
2007 }
2008
2009 case pch::TYPE_POINTER: {
2010 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2011 QualType PointeeType = GetType(Record[0]);
2012 return Context.getPointerType(PointeeType);
2013 }
2014
2015 case pch::TYPE_BLOCK_POINTER: {
2016 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2017 QualType PointeeType = GetType(Record[0]);
2018 return Context.getBlockPointerType(PointeeType);
2019 }
2020
2021 case pch::TYPE_LVALUE_REFERENCE: {
2022 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2023 QualType PointeeType = GetType(Record[0]);
2024 return Context.getLValueReferenceType(PointeeType);
2025 }
2026
2027 case pch::TYPE_RVALUE_REFERENCE: {
2028 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2029 QualType PointeeType = GetType(Record[0]);
2030 return Context.getRValueReferenceType(PointeeType);
2031 }
2032
2033 case pch::TYPE_MEMBER_POINTER: {
2034 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2035 QualType PointeeType = GetType(Record[0]);
2036 QualType ClassType = GetType(Record[1]);
2037 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2038 }
2039
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002040 case pch::TYPE_CONSTANT_ARRAY: {
2041 QualType ElementType = GetType(Record[0]);
2042 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2043 unsigned IndexTypeQuals = Record[2];
2044 unsigned Idx = 3;
2045 llvm::APInt Size = ReadAPInt(Record, Idx);
2046 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2047 }
2048
2049 case pch::TYPE_INCOMPLETE_ARRAY: {
2050 QualType ElementType = GetType(Record[0]);
2051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2052 unsigned IndexTypeQuals = Record[2];
2053 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2054 }
2055
2056 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002057 QualType ElementType = GetType(Record[0]);
2058 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2059 unsigned IndexTypeQuals = Record[2];
2060 return Context.getVariableArrayType(ElementType, ReadExpr(),
2061 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002062 }
2063
2064 case pch::TYPE_VECTOR: {
2065 if (Record.size() != 2) {
2066 Error("Incorrect encoding of vector type in PCH file");
2067 return QualType();
2068 }
2069
2070 QualType ElementType = GetType(Record[0]);
2071 unsigned NumElements = Record[1];
2072 return Context.getVectorType(ElementType, NumElements);
2073 }
2074
2075 case pch::TYPE_EXT_VECTOR: {
2076 if (Record.size() != 2) {
2077 Error("Incorrect encoding of extended vector type in PCH file");
2078 return QualType();
2079 }
2080
2081 QualType ElementType = GetType(Record[0]);
2082 unsigned NumElements = Record[1];
2083 return Context.getExtVectorType(ElementType, NumElements);
2084 }
2085
2086 case pch::TYPE_FUNCTION_NO_PROTO: {
2087 if (Record.size() != 1) {
2088 Error("Incorrect encoding of no-proto function type");
2089 return QualType();
2090 }
2091 QualType ResultType = GetType(Record[0]);
2092 return Context.getFunctionNoProtoType(ResultType);
2093 }
2094
2095 case pch::TYPE_FUNCTION_PROTO: {
2096 QualType ResultType = GetType(Record[0]);
2097 unsigned Idx = 1;
2098 unsigned NumParams = Record[Idx++];
2099 llvm::SmallVector<QualType, 16> ParamTypes;
2100 for (unsigned I = 0; I != NumParams; ++I)
2101 ParamTypes.push_back(GetType(Record[Idx++]));
2102 bool isVariadic = Record[Idx++];
2103 unsigned Quals = Record[Idx++];
2104 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2105 isVariadic, Quals);
2106 }
2107
2108 case pch::TYPE_TYPEDEF:
2109 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2110 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2111
2112 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002113 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002114
2115 case pch::TYPE_TYPEOF: {
2116 if (Record.size() != 1) {
2117 Error("Incorrect encoding of typeof(type) in PCH file");
2118 return QualType();
2119 }
2120 QualType UnderlyingType = GetType(Record[0]);
2121 return Context.getTypeOfType(UnderlyingType);
2122 }
2123
2124 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002125 assert(Record.size() == 1 && "Incorrect encoding of record type");
2126 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002127
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002128 case pch::TYPE_ENUM:
2129 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2130 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2131
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002132 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002133 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2134 return Context.getObjCInterfaceType(
2135 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002136
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002137 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2138 unsigned Idx = 0;
2139 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2140 unsigned NumProtos = Record[Idx++];
2141 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2142 for (unsigned I = 0; I != NumProtos; ++I)
2143 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2144 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2145 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002146
Chris Lattner9b9f2352009-04-22 06:40:03 +00002147 case pch::TYPE_OBJC_QUALIFIED_ID: {
2148 unsigned Idx = 0;
2149 unsigned NumProtos = Record[Idx++];
2150 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2151 for (unsigned I = 0; I != NumProtos; ++I)
2152 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2153 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2154 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002155 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002156 // Suppress a GCC warning
2157 return QualType();
2158}
2159
2160/// \brief Note that we have loaded the declaration with the given
2161/// Index.
2162///
2163/// This routine notes that this declaration has already been loaded,
2164/// so that future GetDecl calls will return this declaration rather
2165/// than trying to load a new declaration.
2166inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2167 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2168 DeclAlreadyLoaded[Index] = true;
2169 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2170}
2171
2172/// \brief Read the declaration at the given offset from the PCH file.
2173Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002174 // Keep track of where we are in the stream, then jump back there
2175 // after reading this declaration.
2176 SavedStreamPosition SavedPosition(Stream);
2177
Douglas Gregorc34897d2009-04-09 22:27:44 +00002178 Decl *D = 0;
2179 Stream.JumpToBit(Offset);
2180 RecordData Record;
2181 unsigned Code = Stream.ReadCode();
2182 unsigned Idx = 0;
2183 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002184
Douglas Gregorc34897d2009-04-09 22:27:44 +00002185 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002186 case pch::DECL_ATTR:
2187 case pch::DECL_CONTEXT_LEXICAL:
2188 case pch::DECL_CONTEXT_VISIBLE:
2189 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2190 break;
2191
Douglas Gregorc34897d2009-04-09 22:27:44 +00002192 case pch::DECL_TRANSLATION_UNIT:
2193 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002194 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002195 break;
2196
2197 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002198 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002199 break;
2200 }
2201
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002202 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002203 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002204 break;
2205 }
2206
Douglas Gregor982365e2009-04-13 21:20:57 +00002207 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002208 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2209 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002210 break;
2211 }
2212
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002213 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002214 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2215 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002216 break;
2217 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002218
2219 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002220 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2221 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002222 break;
2223 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002224
Steve Naroff79ea0e02009-04-20 15:06:07 +00002225 case pch::DECL_OBJC_METHOD: {
2226 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2227 Selector(), QualType(), 0);
2228 break;
2229 }
2230
Steve Naroff97b53bd2009-04-21 15:12:33 +00002231 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002232 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2233 break;
2234 }
2235
Steve Naroff97b53bd2009-04-21 15:12:33 +00002236 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002237 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2238 ObjCIvarDecl::None);
2239 break;
2240 }
2241
Steve Naroff97b53bd2009-04-21 15:12:33 +00002242 case pch::DECL_OBJC_PROTOCOL: {
2243 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2244 break;
2245 }
2246
2247 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2248 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2249 QualType(), 0);
2250 break;
2251 }
2252
2253 case pch::DECL_OBJC_CLASS: {
2254 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2255 break;
2256 }
2257
2258 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2259 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2260 break;
2261 }
2262
2263 case pch::DECL_OBJC_CATEGORY: {
2264 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2265 break;
2266 }
2267
2268 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002269 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002270 break;
2271 }
2272
2273 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002274 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002275 break;
2276 }
2277
2278 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2279 // FIXME: Implement.
2280 break;
2281 }
2282
2283 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002284 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002285 break;
2286 }
2287
2288 case pch::DECL_OBJC_PROPERTY_IMPL: {
2289 // FIXME: Implement.
2290 break;
2291 }
2292
Douglas Gregor982365e2009-04-13 21:20:57 +00002293 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002294 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2295 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002296 break;
2297 }
2298
Douglas Gregorc34897d2009-04-09 22:27:44 +00002299 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002300 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2301 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002302 break;
2303 }
2304
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002305 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002306 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2307 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002308 break;
2309 }
2310
2311 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002312 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002313 QualType(), QualType(), VarDecl::None,
2314 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002315 break;
2316 }
2317
Douglas Gregor2a491792009-04-13 22:49:25 +00002318 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002319 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002320 break;
2321 }
2322
2323 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002324 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002325 break;
2326 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002327 }
2328
Douglas Gregorc713da92009-04-21 22:25:48 +00002329 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002330 if (D) {
2331 LoadedDecl(Index, D);
2332 Reader.Visit(D);
2333 }
2334
Douglas Gregorc34897d2009-04-09 22:27:44 +00002335 // If this declaration is also a declaration context, get the
2336 // offsets for its tables of lexical and visible declarations.
2337 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2338 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2339 if (Offsets.first || Offsets.second) {
2340 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2341 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2342 DeclContextOffsets[DC] = Offsets;
2343 }
2344 }
2345 assert(Idx == Record.size());
2346
Douglas Gregor405b6432009-04-22 19:09:20 +00002347 if (Consumer) {
2348 // If we have deserialized a declaration that has a definition the
2349 // AST consumer might need to know about, notify the consumer
2350 // about that definition now.
2351 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2352 if (Var->isFileVarDecl() && Var->getInit()) {
2353 DeclGroupRef DG(Var);
2354 Consumer->HandleTopLevelDecl(DG);
2355 }
2356 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2357 if (Func->isThisDeclarationADefinition()) {
2358 DeclGroupRef DG(Func);
2359 Consumer->HandleTopLevelDecl(DG);
2360 }
2361 }
2362 }
2363
Douglas Gregorc34897d2009-04-09 22:27:44 +00002364 return D;
2365}
2366
Douglas Gregorac8f2802009-04-10 17:25:41 +00002367QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002368 unsigned Quals = ID & 0x07;
2369 unsigned Index = ID >> 3;
2370
2371 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2372 QualType T;
2373 switch ((pch::PredefinedTypeIDs)Index) {
2374 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2375 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2376 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2377
2378 case pch::PREDEF_TYPE_CHAR_U_ID:
2379 case pch::PREDEF_TYPE_CHAR_S_ID:
2380 // FIXME: Check that the signedness of CharTy is correct!
2381 T = Context.CharTy;
2382 break;
2383
2384 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2385 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2386 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2387 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2388 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2389 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2390 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2391 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2392 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2393 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2394 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2395 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2396 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2397 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2398 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2399 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2400 }
2401
2402 assert(!T.isNull() && "Unknown predefined type");
2403 return T.getQualifiedType(Quals);
2404 }
2405
2406 Index -= pch::NUM_PREDEF_TYPE_IDS;
2407 if (!TypeAlreadyLoaded[Index]) {
2408 // Load the type from the PCH file.
2409 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2410 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2411 TypeAlreadyLoaded[Index] = true;
2412 }
2413
2414 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2415}
2416
Douglas Gregorac8f2802009-04-10 17:25:41 +00002417Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002418 if (ID == 0)
2419 return 0;
2420
2421 unsigned Index = ID - 1;
2422 if (DeclAlreadyLoaded[Index])
2423 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2424
2425 // Load the declaration from the PCH file.
2426 return ReadDeclRecord(DeclOffsets[Index], Index);
2427}
2428
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002429Stmt *PCHReader::GetStmt(uint64_t Offset) {
2430 // Keep track of where we are in the stream, then jump back there
2431 // after reading this declaration.
2432 SavedStreamPosition SavedPosition(Stream);
2433
2434 Stream.JumpToBit(Offset);
2435 return ReadStmt();
2436}
2437
Douglas Gregorc34897d2009-04-09 22:27:44 +00002438bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002439 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002440 assert(DC->hasExternalLexicalStorage() &&
2441 "DeclContext has no lexical decls in storage");
2442 uint64_t Offset = DeclContextOffsets[DC].first;
2443 assert(Offset && "DeclContext has no lexical decls in storage");
2444
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002445 // Keep track of where we are in the stream, then jump back there
2446 // after reading this context.
2447 SavedStreamPosition SavedPosition(Stream);
2448
Douglas Gregorc34897d2009-04-09 22:27:44 +00002449 // Load the record containing all of the declarations lexically in
2450 // this context.
2451 Stream.JumpToBit(Offset);
2452 RecordData Record;
2453 unsigned Code = Stream.ReadCode();
2454 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002455 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002456 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2457
2458 // Load all of the declaration IDs
2459 Decls.clear();
2460 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002461 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002462 return false;
2463}
2464
2465bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2466 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2467 assert(DC->hasExternalVisibleStorage() &&
2468 "DeclContext has no visible decls in storage");
2469 uint64_t Offset = DeclContextOffsets[DC].second;
2470 assert(Offset && "DeclContext has no visible decls in storage");
2471
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002472 // Keep track of where we are in the stream, then jump back there
2473 // after reading this context.
2474 SavedStreamPosition SavedPosition(Stream);
2475
Douglas Gregorc34897d2009-04-09 22:27:44 +00002476 // Load the record containing all of the declarations visible in
2477 // this context.
2478 Stream.JumpToBit(Offset);
2479 RecordData Record;
2480 unsigned Code = Stream.ReadCode();
2481 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002482 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002483 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2484 if (Record.size() == 0)
2485 return false;
2486
2487 Decls.clear();
2488
2489 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002490 while (Idx < Record.size()) {
2491 Decls.push_back(VisibleDeclaration());
2492 Decls.back().Name = ReadDeclarationName(Record, Idx);
2493
Douglas Gregorc34897d2009-04-09 22:27:44 +00002494 unsigned Size = Record[Idx++];
2495 llvm::SmallVector<unsigned, 4> & LoadedDecls
2496 = Decls.back().Declarations;
2497 LoadedDecls.reserve(Size);
2498 for (unsigned I = 0; I < Size; ++I)
2499 LoadedDecls.push_back(Record[Idx++]);
2500 }
2501
Douglas Gregoraf136d92009-04-22 22:34:57 +00002502 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002503 return false;
2504}
2505
Douglas Gregor631f6c62009-04-14 00:24:19 +00002506void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002507 this->Consumer = Consumer;
2508
Douglas Gregor631f6c62009-04-14 00:24:19 +00002509 if (!Consumer)
2510 return;
2511
2512 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2513 Decl *D = GetDecl(ExternalDefinitions[I]);
2514 DeclGroupRef DG(D);
2515 Consumer->HandleTopLevelDecl(DG);
2516 }
2517}
2518
Douglas Gregorc34897d2009-04-09 22:27:44 +00002519void PCHReader::PrintStats() {
2520 std::fprintf(stderr, "*** PCH Statistics:\n");
2521
2522 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2523 TypeAlreadyLoaded.end(),
2524 true);
2525 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2526 DeclAlreadyLoaded.end(),
2527 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002528 unsigned NumIdentifiersLoaded = 0;
2529 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2530 if ((IdentifierData[I] & 0x01) == 0)
2531 ++NumIdentifiersLoaded;
2532 }
2533
Douglas Gregorc34897d2009-04-09 22:27:44 +00002534 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2535 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002536 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002537 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2538 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002539 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2540 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2541 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2542 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002543 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2544 NumStatementsRead, TotalNumStatements,
2545 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002546 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2547 NumMacrosRead, TotalNumMacros,
2548 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002549 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2550 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2551 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2552 * 100));
2553 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2554 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2555 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2556 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002557 std::fprintf(stderr, "\n");
2558}
2559
Douglas Gregorc713da92009-04-21 22:25:48 +00002560void PCHReader::InitializeSema(Sema &S) {
2561 SemaObj = &S;
2562
Douglas Gregor2554cf22009-04-22 21:15:06 +00002563 // Makes sure any declarations that were deserialized "too early"
2564 // still get added to the identifier's declaration chains.
2565 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2566 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2567 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002568 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002569 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002570
2571 // If there were any tentative definitions, deserialize them and add
2572 // them to Sema's table of tentative definitions.
2573 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2574 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2575 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2576 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002577
2578 // If there were any locally-scoped external declarations,
2579 // deserialize them and add them to Sema's table of locally-scoped
2580 // external declarations.
2581 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2582 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2583 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2584 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002585}
2586
2587IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2588 // Try to find this name within our on-disk hash table
2589 PCHIdentifierLookupTable *IdTable
2590 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2591 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2592 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2593 if (Pos == IdTable->end())
2594 return 0;
2595
2596 // Dereferencing the iterator has the effect of building the
2597 // IdentifierInfo node and populating it with the various
2598 // declarations it needs.
2599 return *Pos;
2600}
2601
2602void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2603 assert(ID && "Non-zero identifier ID required");
2604 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2605}
2606
Chris Lattner29241862009-04-11 21:15:38 +00002607IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002608 if (ID == 0)
2609 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002610
Douglas Gregorc713da92009-04-21 22:25:48 +00002611 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002612 Error("No identifier table in PCH file");
2613 return 0;
2614 }
Chris Lattner29241862009-04-11 21:15:38 +00002615
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002616 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002617 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002618 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002619 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002620 }
Chris Lattner29241862009-04-11 21:15:38 +00002621
2622 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002623}
2624
2625DeclarationName
2626PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2627 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2628 switch (Kind) {
2629 case DeclarationName::Identifier:
2630 return DeclarationName(GetIdentifierInfo(Record, Idx));
2631
2632 case DeclarationName::ObjCZeroArgSelector:
2633 case DeclarationName::ObjCOneArgSelector:
2634 case DeclarationName::ObjCMultiArgSelector:
2635 assert(false && "Unable to de-serialize Objective-C selectors");
2636 break;
2637
2638 case DeclarationName::CXXConstructorName:
2639 return Context.DeclarationNames.getCXXConstructorName(
2640 GetType(Record[Idx++]));
2641
2642 case DeclarationName::CXXDestructorName:
2643 return Context.DeclarationNames.getCXXDestructorName(
2644 GetType(Record[Idx++]));
2645
2646 case DeclarationName::CXXConversionFunctionName:
2647 return Context.DeclarationNames.getCXXConversionFunctionName(
2648 GetType(Record[Idx++]));
2649
2650 case DeclarationName::CXXOperatorName:
2651 return Context.DeclarationNames.getCXXOperatorName(
2652 (OverloadedOperatorKind)Record[Idx++]);
2653
2654 case DeclarationName::CXXUsingDirective:
2655 return DeclarationName::getUsingDirectiveName();
2656 }
2657
2658 // Required to silence GCC warning
2659 return DeclarationName();
2660}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002661
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002662/// \brief Read an integral value
2663llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2664 unsigned BitWidth = Record[Idx++];
2665 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2666 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2667 Idx += NumWords;
2668 return Result;
2669}
2670
2671/// \brief Read a signed integral value
2672llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2673 bool isUnsigned = Record[Idx++];
2674 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2675}
2676
Douglas Gregore2f37202009-04-14 21:55:33 +00002677/// \brief Read a floating-point value
2678llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002679 return llvm::APFloat(ReadAPInt(Record, Idx));
2680}
2681
Douglas Gregor1c507882009-04-15 21:30:51 +00002682// \brief Read a string
2683std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2684 unsigned Len = Record[Idx++];
2685 std::string Result(&Record[Idx], &Record[Idx] + Len);
2686 Idx += Len;
2687 return Result;
2688}
2689
2690/// \brief Reads attributes from the current stream position.
2691Attr *PCHReader::ReadAttributes() {
2692 unsigned Code = Stream.ReadCode();
2693 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2694 "Expected unabbreviated record"); (void)Code;
2695
2696 RecordData Record;
2697 unsigned Idx = 0;
2698 unsigned RecCode = Stream.ReadRecord(Code, Record);
2699 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2700 (void)RecCode;
2701
2702#define SIMPLE_ATTR(Name) \
2703 case Attr::Name: \
2704 New = ::new (Context) Name##Attr(); \
2705 break
2706
2707#define STRING_ATTR(Name) \
2708 case Attr::Name: \
2709 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2710 break
2711
2712#define UNSIGNED_ATTR(Name) \
2713 case Attr::Name: \
2714 New = ::new (Context) Name##Attr(Record[Idx++]); \
2715 break
2716
2717 Attr *Attrs = 0;
2718 while (Idx < Record.size()) {
2719 Attr *New = 0;
2720 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2721 bool IsInherited = Record[Idx++];
2722
2723 switch (Kind) {
2724 STRING_ATTR(Alias);
2725 UNSIGNED_ATTR(Aligned);
2726 SIMPLE_ATTR(AlwaysInline);
2727 SIMPLE_ATTR(AnalyzerNoReturn);
2728 STRING_ATTR(Annotate);
2729 STRING_ATTR(AsmLabel);
2730
2731 case Attr::Blocks:
2732 New = ::new (Context) BlocksAttr(
2733 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2734 break;
2735
2736 case Attr::Cleanup:
2737 New = ::new (Context) CleanupAttr(
2738 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2739 break;
2740
2741 SIMPLE_ATTR(Const);
2742 UNSIGNED_ATTR(Constructor);
2743 SIMPLE_ATTR(DLLExport);
2744 SIMPLE_ATTR(DLLImport);
2745 SIMPLE_ATTR(Deprecated);
2746 UNSIGNED_ATTR(Destructor);
2747 SIMPLE_ATTR(FastCall);
2748
2749 case Attr::Format: {
2750 std::string Type = ReadString(Record, Idx);
2751 unsigned FormatIdx = Record[Idx++];
2752 unsigned FirstArg = Record[Idx++];
2753 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2754 break;
2755 }
2756
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002757 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002758
2759 case Attr::IBOutletKind:
2760 New = ::new (Context) IBOutletAttr();
2761 break;
2762
2763 SIMPLE_ATTR(NoReturn);
2764 SIMPLE_ATTR(NoThrow);
2765 SIMPLE_ATTR(Nodebug);
2766 SIMPLE_ATTR(Noinline);
2767
2768 case Attr::NonNull: {
2769 unsigned Size = Record[Idx++];
2770 llvm::SmallVector<unsigned, 16> ArgNums;
2771 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2772 Idx += Size;
2773 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2774 break;
2775 }
2776
2777 SIMPLE_ATTR(ObjCException);
2778 SIMPLE_ATTR(ObjCNSObject);
2779 SIMPLE_ATTR(Overloadable);
2780 UNSIGNED_ATTR(Packed);
2781 SIMPLE_ATTR(Pure);
2782 UNSIGNED_ATTR(Regparm);
2783 STRING_ATTR(Section);
2784 SIMPLE_ATTR(StdCall);
2785 SIMPLE_ATTR(TransparentUnion);
2786 SIMPLE_ATTR(Unavailable);
2787 SIMPLE_ATTR(Unused);
2788 SIMPLE_ATTR(Used);
2789
2790 case Attr::Visibility:
2791 New = ::new (Context) VisibilityAttr(
2792 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2793 break;
2794
2795 SIMPLE_ATTR(WarnUnusedResult);
2796 SIMPLE_ATTR(Weak);
2797 SIMPLE_ATTR(WeakImport);
2798 }
2799
2800 assert(New && "Unable to decode attribute?");
2801 New->setInherited(IsInherited);
2802 New->setNext(Attrs);
2803 Attrs = New;
2804 }
2805#undef UNSIGNED_ATTR
2806#undef STRING_ATTR
2807#undef SIMPLE_ATTR
2808
2809 // The list of attributes was built backwards. Reverse the list
2810 // before returning it.
2811 Attr *PrevAttr = 0, *NextAttr = 0;
2812 while (Attrs) {
2813 NextAttr = Attrs->getNext();
2814 Attrs->setNext(PrevAttr);
2815 PrevAttr = Attrs;
2816 Attrs = NextAttr;
2817 }
2818
2819 return PrevAttr;
2820}
2821
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002822Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002823 // Within the bitstream, expressions are stored in Reverse Polish
2824 // Notation, with each of the subexpressions preceding the
2825 // expression they are stored in. To evaluate expressions, we
2826 // continue reading expressions and placing them on the stack, with
2827 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002828 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002829 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002830 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002831 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002832 llvm::SmallVector<Stmt *, 16> StmtStack;
2833 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002834 Stmt::EmptyShell Empty;
2835
Douglas Gregora151ba42009-04-14 23:32:43 +00002836 while (true) {
2837 unsigned Code = Stream.ReadCode();
2838 if (Code == llvm::bitc::END_BLOCK) {
2839 if (Stream.ReadBlockEnd()) {
2840 Error("Error at end of Source Manager block");
2841 return 0;
2842 }
2843 break;
2844 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002845
Douglas Gregora151ba42009-04-14 23:32:43 +00002846 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2847 // No known subblocks, always skip them.
2848 Stream.ReadSubBlockID();
2849 if (Stream.SkipBlock()) {
2850 Error("Malformed block record");
2851 return 0;
2852 }
2853 continue;
2854 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002855
Douglas Gregora151ba42009-04-14 23:32:43 +00002856 if (Code == llvm::bitc::DEFINE_ABBREV) {
2857 Stream.ReadAbbrevRecord();
2858 continue;
2859 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002860
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002861 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002862 Idx = 0;
2863 Record.clear();
2864 bool Finished = false;
2865 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002866 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002867 Finished = true;
2868 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002869
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002870 case pch::STMT_NULL_PTR:
2871 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002872 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002873
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002874 case pch::STMT_NULL:
2875 S = new (Context) NullStmt(Empty);
2876 break;
2877
2878 case pch::STMT_COMPOUND:
2879 S = new (Context) CompoundStmt(Empty);
2880 break;
2881
2882 case pch::STMT_CASE:
2883 S = new (Context) CaseStmt(Empty);
2884 break;
2885
2886 case pch::STMT_DEFAULT:
2887 S = new (Context) DefaultStmt(Empty);
2888 break;
2889
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002890 case pch::STMT_LABEL:
2891 S = new (Context) LabelStmt(Empty);
2892 break;
2893
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002894 case pch::STMT_IF:
2895 S = new (Context) IfStmt(Empty);
2896 break;
2897
2898 case pch::STMT_SWITCH:
2899 S = new (Context) SwitchStmt(Empty);
2900 break;
2901
Douglas Gregora6b503f2009-04-17 00:16:09 +00002902 case pch::STMT_WHILE:
2903 S = new (Context) WhileStmt(Empty);
2904 break;
2905
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002906 case pch::STMT_DO:
2907 S = new (Context) DoStmt(Empty);
2908 break;
2909
2910 case pch::STMT_FOR:
2911 S = new (Context) ForStmt(Empty);
2912 break;
2913
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002914 case pch::STMT_GOTO:
2915 S = new (Context) GotoStmt(Empty);
2916 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002917
2918 case pch::STMT_INDIRECT_GOTO:
2919 S = new (Context) IndirectGotoStmt(Empty);
2920 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002921
Douglas Gregora6b503f2009-04-17 00:16:09 +00002922 case pch::STMT_CONTINUE:
2923 S = new (Context) ContinueStmt(Empty);
2924 break;
2925
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002926 case pch::STMT_BREAK:
2927 S = new (Context) BreakStmt(Empty);
2928 break;
2929
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002930 case pch::STMT_RETURN:
2931 S = new (Context) ReturnStmt(Empty);
2932 break;
2933
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002934 case pch::STMT_DECL:
2935 S = new (Context) DeclStmt(Empty);
2936 break;
2937
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002938 case pch::STMT_ASM:
2939 S = new (Context) AsmStmt(Empty);
2940 break;
2941
Douglas Gregora151ba42009-04-14 23:32:43 +00002942 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002943 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002944 break;
2945
2946 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002947 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002948 break;
2949
2950 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002951 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002952 break;
2953
2954 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002955 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002956 break;
2957
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002958 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002959 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002960 break;
2961
Douglas Gregor596e0932009-04-15 16:35:07 +00002962 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002963 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002964 Record[PCHStmtReader::NumExprFields + 1]);
2965 break;
2966
Douglas Gregora151ba42009-04-14 23:32:43 +00002967 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002968 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002969 break;
2970
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002971 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002972 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002973 break;
2974
Douglas Gregor12d74052009-04-15 15:58:59 +00002975 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002976 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002977 break;
2978
2979 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002980 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002981 break;
2982
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002983 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002984 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002985 break;
2986
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002987 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002988 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002989 break;
2990
2991 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002992 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002993 break;
2994
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002995 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002996 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002997 break;
2998
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002999 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003000 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003001 break;
3002
3003 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003004 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003005 break;
3006
Douglas Gregora151ba42009-04-14 23:32:43 +00003007 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003008 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003009 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003010
3011 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003012 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003013 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003014
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003015 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003016 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003017 break;
3018
Douglas Gregorec0b8292009-04-15 23:02:49 +00003019 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003020 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003021 break;
3022
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003023 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003024 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003025 break;
3026
3027 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003028 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003029 Record[PCHStmtReader::NumExprFields] - 1);
3030
3031 break;
3032
3033 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003034 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003035 break;
3036
Douglas Gregorec0b8292009-04-15 23:02:49 +00003037 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003038 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003039 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003040
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003041 case pch::EXPR_ADDR_LABEL:
3042 S = new (Context) AddrLabelExpr(Empty);
3043 break;
3044
Douglas Gregoreca12f62009-04-17 19:05:30 +00003045 case pch::EXPR_STMT:
3046 S = new (Context) StmtExpr(Empty);
3047 break;
3048
Douglas Gregor209d4622009-04-15 23:33:31 +00003049 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003050 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003051 break;
3052
3053 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003054 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003055 break;
3056
3057 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003058 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003059 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003060
3061 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003062 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003063 break;
3064
Douglas Gregore246b742009-04-17 19:21:43 +00003065 case pch::EXPR_BLOCK:
3066 S = new (Context) BlockExpr(Empty);
3067 break;
3068
Douglas Gregor725e94b2009-04-16 00:01:45 +00003069 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003070 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003071 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003072
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003073 case pch::EXPR_OBJC_STRING_LITERAL:
3074 S = new (Context) ObjCStringLiteral(Empty);
3075 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003076 case pch::EXPR_OBJC_ENCODE:
3077 S = new (Context) ObjCEncodeExpr(Empty);
3078 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003079 case pch::EXPR_OBJC_SELECTOR_EXPR:
3080 S = new (Context) ObjCSelectorExpr(Empty);
3081 break;
3082 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3083 S = new (Context) ObjCProtocolExpr(Empty);
3084 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003085 }
3086
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003087 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003088 if (Finished)
3089 break;
3090
Douglas Gregor456e0952009-04-17 22:13:46 +00003091 ++NumStatementsRead;
3092
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003093 if (S) {
3094 unsigned NumSubStmts = Reader.Visit(S);
3095 while (NumSubStmts > 0) {
3096 StmtStack.pop_back();
3097 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003098 }
3099 }
3100
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003101 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003102 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003103 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003104 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003105 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003106 return StmtStack.back();
3107}
3108
3109Expr *PCHReader::ReadExpr() {
3110 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003111}
3112
Douglas Gregor179cfb12009-04-10 20:39:37 +00003113DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003114 return Diag(SourceLocation(), DiagID);
3115}
3116
3117DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3118 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003119 Context.getSourceManager()),
3120 DiagID);
3121}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003122
Douglas Gregorc713da92009-04-21 22:25:48 +00003123/// \brief Retrieve the identifier table associated with the
3124/// preprocessor.
3125IdentifierTable &PCHReader::getIdentifierTable() {
3126 return PP.getIdentifierTable();
3127}
3128
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003129/// \brief Record that the given ID maps to the given switch-case
3130/// statement.
3131void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3132 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3133 SwitchCaseStmts[ID] = SC;
3134}
3135
3136/// \brief Retrieve the switch-case statement with the given ID.
3137SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3138 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3139 return SwitchCaseStmts[ID];
3140}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003141
3142/// \brief Record that the given label statement has been
3143/// deserialized and has the given ID.
3144void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3145 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3146 "Deserialized label twice");
3147 LabelStmts[ID] = S;
3148
3149 // If we've already seen any goto statements that point to this
3150 // label, resolve them now.
3151 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3152 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3153 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3154 Goto->second->setLabel(S);
3155 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003156
3157 // If we've already seen any address-label statements that point to
3158 // this label, resolve them now.
3159 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3160 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3161 = UnresolvedAddrLabelExprs.equal_range(ID);
3162 for (AddrLabelIter AddrLabel = AddrLabels.first;
3163 AddrLabel != AddrLabels.second; ++AddrLabel)
3164 AddrLabel->second->setLabel(S);
3165 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003166}
3167
3168/// \brief Set the label of the given statement to the label
3169/// identified by ID.
3170///
3171/// Depending on the order in which the label and other statements
3172/// referencing that label occur, this operation may complete
3173/// immediately (updating the statement) or it may queue the
3174/// statement to be back-patched later.
3175void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3176 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3177 if (Label != LabelStmts.end()) {
3178 // We've already seen this label, so set the label of the goto and
3179 // we're done.
3180 S->setLabel(Label->second);
3181 } else {
3182 // We haven't seen this label yet, so add this goto to the set of
3183 // unresolved goto statements.
3184 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3185 }
3186}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003187
3188/// \brief Set the label of the given expression to the label
3189/// identified by ID.
3190///
3191/// Depending on the order in which the label and other statements
3192/// referencing that label occur, this operation may complete
3193/// immediately (updating the statement) or it may queue the
3194/// statement to be back-patched later.
3195void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3196 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3197 if (Label != LabelStmts.end()) {
3198 // We've already seen this label, so set the label of the
3199 // label-address expression and we're done.
3200 S->setLabel(Label->second);
3201 } else {
3202 // We haven't seen this label yet, so add this label-address
3203 // expression to the set of unresolved label-address expressions.
3204 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3205 }
3206}