blob: 3ade493b4ec0919b742bfb1aa5866e4c9599695d [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregorfdd01722009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-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 Gregor37e26842009-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 Gregor2cf26342009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-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 Gregor0a2b45e2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff53c9d8a2009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-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 Gregor2cf26342009-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 Gregor68a2eb02009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-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 Gregor2cf26342009-04-09 22:27:44 +0000141}
142
Douglas Gregor0a2b45e2009-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 Gregor8c700062009-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 Gregor2cf26342009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor3a2f7e42009-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 Naroff53c9d8a2009-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 Naroff33feeb02009-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 Lattner4dcf151a2009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Steve Naroff33feeb02009-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 Lattner4dcf151a2009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff33feeb02009-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 Naroff30833f82009-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);
306 // FIXME: Implement.
307}
308
309void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
310 VisitDecl(D);
311 // FIXME: Implement.
312}
313
314void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
315 VisitObjCImplDecl(D);
316 // FIXME: Implement.
317}
318
319void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
320 VisitObjCImplDecl(D);
321 // FIXME: Implement.
322}
323
324
325void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
326 VisitDecl(D);
327 // FIXME: Implement.
328}
329
Douglas Gregor8c700062009-04-13 21:20:57 +0000330void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
331 VisitValueDecl(FD);
332 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000333 if (Record[Idx++])
334 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000335}
336
Douglas Gregor2cf26342009-04-09 22:27:44 +0000337void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
338 VisitValueDecl(VD);
339 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
340 VD->setThreadSpecified(Record[Idx++]);
341 VD->setCXXDirectInitializer(Record[Idx++]);
342 VD->setDeclaredInCondition(Record[Idx++]);
343 VD->setPreviousDeclaration(
344 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
345 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000346 if (Record[Idx++])
347 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000348}
349
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000350void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
351 VisitVarDecl(PD);
352 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000353 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000354}
355
356void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
357 VisitParmVarDecl(PD);
358 PD->setOriginalType(Reader.GetType(Record[Idx++]));
359}
360
Douglas Gregor1028bc62009-04-13 22:49:25 +0000361void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
362 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000363 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000364}
365
366void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
367 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000368 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000369 unsigned NumParams = Record[Idx++];
370 llvm::SmallVector<ParmVarDecl *, 16> Params;
371 Params.reserve(NumParams);
372 for (unsigned I = 0; I != NumParams; ++I)
373 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
374 BD->setParams(Reader.getContext(), &Params[0], NumParams);
375}
376
Douglas Gregor2cf26342009-04-09 22:27:44 +0000377std::pair<uint64_t, uint64_t>
378PCHDeclReader::VisitDeclContext(DeclContext *DC) {
379 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000380 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000381 return std::make_pair(LexicalOffset, VisibleOffset);
382}
383
Douglas Gregor0b748912009-04-14 21:18:50 +0000384//===----------------------------------------------------------------------===//
385// Statement/expression deserialization
386//===----------------------------------------------------------------------===//
387namespace {
388 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000389 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000390 PCHReader &Reader;
391 const PCHReader::RecordData &Record;
392 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000393 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000394
395 public:
396 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000397 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
398 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000399
Douglas Gregor025452f2009-04-17 00:04:06 +0000400 /// \brief The number of record fields required for the Stmt class
401 /// itself.
402 static const unsigned NumStmtFields = 0;
403
Douglas Gregor673ecd62009-04-15 16:35:07 +0000404 /// \brief The number of record fields required for the Expr class
405 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000406 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000407
Douglas Gregor087fd532009-04-14 23:32:43 +0000408 // Each of the Visit* functions reads in part of the expression
409 // from the given record and the current expression stack, then
410 // return the total number of operands that it read from the
411 // expression stack.
412
Douglas Gregor025452f2009-04-17 00:04:06 +0000413 unsigned VisitStmt(Stmt *S);
414 unsigned VisitNullStmt(NullStmt *S);
415 unsigned VisitCompoundStmt(CompoundStmt *S);
416 unsigned VisitSwitchCase(SwitchCase *S);
417 unsigned VisitCaseStmt(CaseStmt *S);
418 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000419 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000420 unsigned VisitIfStmt(IfStmt *S);
421 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000422 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000423 unsigned VisitDoStmt(DoStmt *S);
424 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000425 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000426 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000427 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000428 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000429 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000430 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000431 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000432 unsigned VisitExpr(Expr *E);
433 unsigned VisitPredefinedExpr(PredefinedExpr *E);
434 unsigned VisitDeclRefExpr(DeclRefExpr *E);
435 unsigned VisitIntegerLiteral(IntegerLiteral *E);
436 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000437 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000438 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000439 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000440 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000441 unsigned VisitUnaryOperator(UnaryOperator *E);
442 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000443 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000444 unsigned VisitCallExpr(CallExpr *E);
445 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000446 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000447 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000448 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
449 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000450 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000451 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
452 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000453 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000454 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000455 unsigned VisitInitListExpr(InitListExpr *E);
456 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
457 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000458 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000459 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000460 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000461 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
462 unsigned VisitChooseExpr(ChooseExpr *E);
463 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000464 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000465 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000466 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000467 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000468 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000469 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
470 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000471 };
472}
473
Douglas Gregor025452f2009-04-17 00:04:06 +0000474unsigned PCHStmtReader::VisitStmt(Stmt *S) {
475 assert(Idx == NumStmtFields && "Incorrect statement field count");
476 return 0;
477}
478
479unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
480 VisitStmt(S);
481 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
482 return 0;
483}
484
485unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
486 VisitStmt(S);
487 unsigned NumStmts = Record[Idx++];
488 S->setStmts(Reader.getContext(),
489 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
490 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
491 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
492 return NumStmts;
493}
494
495unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
496 VisitStmt(S);
497 Reader.RecordSwitchCaseID(S, Record[Idx++]);
498 return 0;
499}
500
501unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
502 VisitSwitchCase(S);
503 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
504 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
505 S->setSubStmt(StmtStack.back());
506 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
507 return 3;
508}
509
510unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
511 VisitSwitchCase(S);
512 S->setSubStmt(StmtStack.back());
513 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
514 return 1;
515}
516
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000517unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
518 VisitStmt(S);
519 S->setID(Reader.GetIdentifierInfo(Record, Idx));
520 S->setSubStmt(StmtStack.back());
521 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
522 Reader.RecordLabelStmt(S, Record[Idx++]);
523 return 1;
524}
525
Douglas Gregor025452f2009-04-17 00:04:06 +0000526unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
527 VisitStmt(S);
528 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
529 S->setThen(StmtStack[StmtStack.size() - 2]);
530 S->setElse(StmtStack[StmtStack.size() - 1]);
531 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
532 return 3;
533}
534
535unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
536 VisitStmt(S);
537 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
538 S->setBody(StmtStack.back());
539 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
540 SwitchCase *PrevSC = 0;
541 for (unsigned N = Record.size(); Idx != N; ++Idx) {
542 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
543 if (PrevSC)
544 PrevSC->setNextSwitchCase(SC);
545 else
546 S->setSwitchCaseList(SC);
547 PrevSC = SC;
548 }
549 return 2;
550}
551
Douglas Gregord921cf92009-04-17 00:16:09 +0000552unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
553 VisitStmt(S);
554 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
555 S->setBody(StmtStack.back());
556 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
557 return 2;
558}
559
Douglas Gregor67d82492009-04-17 00:29:51 +0000560unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
561 VisitStmt(S);
562 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
563 S->setBody(StmtStack.back());
564 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
565 return 2;
566}
567
568unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
569 VisitStmt(S);
570 S->setInit(StmtStack[StmtStack.size() - 4]);
571 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
572 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
573 S->setBody(StmtStack.back());
574 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
575 return 4;
576}
577
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000578unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
579 VisitStmt(S);
580 Reader.SetLabelOf(S, Record[Idx++]);
581 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
582 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
583 return 0;
584}
585
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000586unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
587 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000588 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000589 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
590 return 1;
591}
592
Douglas Gregord921cf92009-04-17 00:16:09 +0000593unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
594 VisitStmt(S);
595 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
596 return 0;
597}
598
Douglas Gregor025452f2009-04-17 00:04:06 +0000599unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
600 VisitStmt(S);
601 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
602 return 0;
603}
604
Douglas Gregor0de9d882009-04-17 16:34:57 +0000605unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
606 VisitStmt(S);
607 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
608 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
609 return 1;
610}
611
Douglas Gregor84f21702009-04-17 16:55:36 +0000612unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
613 VisitStmt(S);
614 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
615 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616
617 if (Idx + 1 == Record.size()) {
618 // Single declaration
619 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
620 } else {
621 llvm::SmallVector<Decl *, 16> Decls;
622 Decls.reserve(Record.size() - Idx);
623 for (unsigned N = Record.size(); Idx != N; ++Idx)
624 Decls.push_back(Reader.GetDecl(Record[Idx]));
625 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
626 &Decls[0], Decls.size())));
627 }
628 return 0;
629}
630
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000631unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
632 VisitStmt(S);
633 unsigned NumOutputs = Record[Idx++];
634 unsigned NumInputs = Record[Idx++];
635 unsigned NumClobbers = Record[Idx++];
636 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
638 S->setVolatile(Record[Idx++]);
639 S->setSimple(Record[Idx++]);
640
641 unsigned StackIdx
642 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
643 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
644
645 // Outputs and inputs
646 llvm::SmallVector<std::string, 16> Names;
647 llvm::SmallVector<StringLiteral*, 16> Constraints;
648 llvm::SmallVector<Stmt*, 16> Exprs;
649 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
650 Names.push_back(Reader.ReadString(Record, Idx));
651 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
652 Exprs.push_back(StmtStack[StackIdx++]);
653 }
654 S->setOutputsAndInputs(NumOutputs, NumInputs,
655 &Names[0], &Constraints[0], &Exprs[0]);
656
657 // Constraints
658 llvm::SmallVector<StringLiteral*, 16> Clobbers;
659 for (unsigned I = 0; I != NumClobbers; ++I)
660 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
661 S->setClobbers(&Clobbers[0], NumClobbers);
662
663 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
664 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
665}
666
Douglas Gregor087fd532009-04-14 23:32:43 +0000667unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000668 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000669 E->setType(Reader.GetType(Record[Idx++]));
670 E->setTypeDependent(Record[Idx++]);
671 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000672 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000673 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000674}
675
Douglas Gregor087fd532009-04-14 23:32:43 +0000676unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000677 VisitExpr(E);
678 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
679 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000680 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000681}
682
Douglas Gregor087fd532009-04-14 23:32:43 +0000683unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000684 VisitExpr(E);
685 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
686 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000687 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000688}
689
Douglas Gregor087fd532009-04-14 23:32:43 +0000690unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000691 VisitExpr(E);
692 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
693 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000694 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000695}
696
Douglas Gregor087fd532009-04-14 23:32:43 +0000697unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000698 VisitExpr(E);
699 E->setValue(Reader.ReadAPFloat(Record, Idx));
700 E->setExact(Record[Idx++]);
701 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000702 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000703}
704
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000705unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
706 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000707 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000708 return 1;
709}
710
Douglas Gregor673ecd62009-04-15 16:35:07 +0000711unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
712 VisitExpr(E);
713 unsigned Len = Record[Idx++];
714 assert(Record[Idx] == E->getNumConcatenated() &&
715 "Wrong number of concatenated tokens!");
716 ++Idx;
717 E->setWide(Record[Idx++]);
718
719 // Read string data
720 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
721 E->setStrData(Reader.getContext(), &Str[0], Len);
722 Idx += Len;
723
724 // Read source locations
725 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
726 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
727
728 return 0;
729}
730
Douglas Gregor087fd532009-04-14 23:32:43 +0000731unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000732 VisitExpr(E);
733 E->setValue(Record[Idx++]);
734 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
735 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000736 return 0;
737}
738
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000739unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
740 VisitExpr(E);
741 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
742 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000743 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000744 return 1;
745}
746
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000747unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
748 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000749 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000750 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
751 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
752 return 1;
753}
754
755unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
756 VisitExpr(E);
757 E->setSizeof(Record[Idx++]);
758 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000759 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000760 ++Idx;
761 } else {
762 E->setArgument(Reader.GetType(Record[Idx++]));
763 }
764 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
765 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
766 return E->isArgumentType()? 0 : 1;
767}
768
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000769unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
770 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000771 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
772 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000773 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
774 return 2;
775}
776
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000777unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
778 VisitExpr(E);
779 E->setNumArgs(Reader.getContext(), Record[Idx++]);
780 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000781 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000782 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000783 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000784 return E->getNumArgs() + 1;
785}
786
787unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
788 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000789 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000790 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
791 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
792 E->setArrow(Record[Idx++]);
793 return 1;
794}
795
Douglas Gregor087fd532009-04-14 23:32:43 +0000796unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
797 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000798 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000799 return 1;
800}
801
Douglas Gregordb600c32009-04-15 00:25:59 +0000802unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
803 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000804 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
805 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000806 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
807 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
808 return 2;
809}
810
Douglas Gregorad90e962009-04-15 22:40:36 +0000811unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
812 VisitBinaryOperator(E);
813 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
814 E->setComputationResultType(Reader.GetType(Record[Idx++]));
815 return 2;
816}
817
818unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
819 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000820 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
821 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
822 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000823 return 3;
824}
825
Douglas Gregor087fd532009-04-14 23:32:43 +0000826unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
827 VisitCastExpr(E);
828 E->setLvalueCast(Record[Idx++]);
829 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000830}
831
Douglas Gregordb600c32009-04-15 00:25:59 +0000832unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
833 VisitCastExpr(E);
834 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
835 return 1;
836}
837
838unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
839 VisitExplicitCastExpr(E);
840 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
841 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
842 return 1;
843}
844
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000845unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
846 VisitExpr(E);
847 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000848 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000849 E->setFileScope(Record[Idx++]);
850 return 1;
851}
852
Douglas Gregord3c98a02009-04-15 23:02:49 +0000853unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
854 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000855 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000856 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
857 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
858 return 1;
859}
860
Douglas Gregord077d752009-04-16 00:55:48 +0000861unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
862 VisitExpr(E);
863 unsigned NumInits = Record[Idx++];
864 E->reserveInits(NumInits);
865 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000866 E->updateInit(I,
867 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
868 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000869 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
871 E->setInitializedFieldInUnion(
872 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
873 E->sawArrayRangeDesignator(Record[Idx++]);
874 return NumInits + 1;
875}
876
877unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
878 typedef DesignatedInitExpr::Designator Designator;
879
880 VisitExpr(E);
881 unsigned NumSubExprs = Record[Idx++];
882 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
883 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000884 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000885 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
886 E->setGNUSyntax(Record[Idx++]);
887
888 llvm::SmallVector<Designator, 4> Designators;
889 while (Idx < Record.size()) {
890 switch ((pch::DesignatorTypes)Record[Idx++]) {
891 case pch::DESIG_FIELD_DECL: {
892 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
893 SourceLocation DotLoc
894 = SourceLocation::getFromRawEncoding(Record[Idx++]);
895 SourceLocation FieldLoc
896 = SourceLocation::getFromRawEncoding(Record[Idx++]);
897 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
898 FieldLoc));
899 Designators.back().setField(Field);
900 break;
901 }
902
903 case pch::DESIG_FIELD_NAME: {
904 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
905 SourceLocation DotLoc
906 = SourceLocation::getFromRawEncoding(Record[Idx++]);
907 SourceLocation FieldLoc
908 = SourceLocation::getFromRawEncoding(Record[Idx++]);
909 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
910 break;
911 }
912
913 case pch::DESIG_ARRAY: {
914 unsigned Index = Record[Idx++];
915 SourceLocation LBracketLoc
916 = SourceLocation::getFromRawEncoding(Record[Idx++]);
917 SourceLocation RBracketLoc
918 = SourceLocation::getFromRawEncoding(Record[Idx++]);
919 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
920 break;
921 }
922
923 case pch::DESIG_ARRAY_RANGE: {
924 unsigned Index = Record[Idx++];
925 SourceLocation LBracketLoc
926 = SourceLocation::getFromRawEncoding(Record[Idx++]);
927 SourceLocation EllipsisLoc
928 = SourceLocation::getFromRawEncoding(Record[Idx++]);
929 SourceLocation RBracketLoc
930 = SourceLocation::getFromRawEncoding(Record[Idx++]);
931 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
932 RBracketLoc));
933 break;
934 }
935 }
936 }
937 E->setDesignators(&Designators[0], Designators.size());
938
939 return NumSubExprs;
940}
941
942unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
943 VisitExpr(E);
944 return 0;
945}
946
Douglas Gregord3c98a02009-04-15 23:02:49 +0000947unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
948 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000949 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000950 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
951 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
952 return 1;
953}
954
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000955unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
956 VisitExpr(E);
957 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
958 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
959 Reader.SetLabelOf(E, Record[Idx++]);
960 return 0;
961}
962
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000963unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
964 VisitExpr(E);
965 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
966 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
967 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
968 return 1;
969}
970
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000971unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
972 VisitExpr(E);
973 E->setArgType1(Reader.GetType(Record[Idx++]));
974 E->setArgType2(Reader.GetType(Record[Idx++]));
975 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
976 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
977 return 0;
978}
979
980unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
981 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000982 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
983 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
984 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000985 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 return 3;
988}
989
990unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
991 VisitExpr(E);
992 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 return 0;
994}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000995
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000996unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
997 VisitExpr(E);
998 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000999 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001000 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1001 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1002 return NumExprs;
1003}
1004
Douglas Gregor84af7c22009-04-17 19:21:43 +00001005unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1006 VisitExpr(E);
1007 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1008 E->setHasBlockDeclRefExprs(Record[Idx++]);
1009 return 0;
1010}
1011
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001012unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1013 VisitExpr(E);
1014 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1015 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1016 E->setByRef(Record[Idx++]);
1017 return 0;
1018}
1019
Chris Lattner3a57a372009-04-22 06:29:42 +00001020//===----------------------------------------------------------------------===//
1021// Objective-C Expressions and Statements
1022
1023unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1024 VisitExpr(E);
1025 E->setString(cast<StringLiteral>(StmtStack.back()));
1026 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 return 1;
1028}
1029
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001030unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1031 VisitExpr(E);
1032 E->setEncodedType(Reader.GetType(Record[Idx++]));
1033 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1034 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1035 return 0;
1036}
1037
Chris Lattner3a57a372009-04-22 06:29:42 +00001038unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1039 VisitExpr(E);
1040 // FIXME: Selectors.
1041 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1042 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1043 return 0;
1044}
1045
1046unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1047 VisitExpr(E);
1048 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1049 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1050 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1051 return 0;
1052}
1053
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001054
Douglas Gregor668c1a42009-04-21 22:25:48 +00001055//===----------------------------------------------------------------------===//
1056// PCH reader implementation
1057//===----------------------------------------------------------------------===//
1058
1059namespace {
1060class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1061 PCHReader &Reader;
1062
1063 // If we know the IdentifierInfo in advance, it is here and we will
1064 // not build a new one. Used when deserializing information about an
1065 // identifier that was constructed before the PCH file was read.
1066 IdentifierInfo *KnownII;
1067
1068public:
1069 typedef IdentifierInfo * data_type;
1070
1071 typedef const std::pair<const char*, unsigned> external_key_type;
1072
1073 typedef external_key_type internal_key_type;
1074
1075 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1076 : Reader(Reader), KnownII(II) { }
1077
1078 static bool EqualKey(const internal_key_type& a,
1079 const internal_key_type& b) {
1080 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1081 : false;
1082 }
1083
1084 static unsigned ComputeHash(const internal_key_type& a) {
1085 return BernsteinHash(a.first, a.second);
1086 }
1087
1088 // This hopefully will just get inlined and removed by the optimizer.
1089 static const internal_key_type&
1090 GetInternalKey(const external_key_type& x) { return x; }
1091
1092 static std::pair<unsigned, unsigned>
1093 ReadKeyDataLength(const unsigned char*& d) {
1094 using namespace clang::io;
1095 unsigned KeyLen = ReadUnalignedLE16(d);
1096 unsigned DataLen = ReadUnalignedLE16(d);
1097 return std::make_pair(KeyLen, DataLen);
1098 }
1099
1100 static std::pair<const char*, unsigned>
1101 ReadKey(const unsigned char* d, unsigned n) {
1102 assert(n >= 2 && d[n-1] == '\0');
1103 return std::make_pair((const char*) d, n-1);
1104 }
1105
1106 IdentifierInfo *ReadData(const internal_key_type& k,
1107 const unsigned char* d,
1108 unsigned DataLen) {
1109 using namespace clang::io;
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001110 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001111 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1112 Bits >>= 1;
1113 bool Poisoned = Bits & 0x01;
1114 Bits >>= 1;
1115 bool ExtensionToken = Bits & 0x01;
1116 Bits >>= 1;
1117 bool hasMacroDefinition = Bits & 0x01;
1118 Bits >>= 1;
1119 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1120 Bits >>= 10;
1121 unsigned TokenID = Bits & 0xFF;
1122 Bits >>= 8;
1123
Douglas Gregor668c1a42009-04-21 22:25:48 +00001124 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001125 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor668c1a42009-04-21 22:25:48 +00001126 DataLen -= 8;
1127
1128 // Build the IdentifierInfo itself and link the identifier ID with
1129 // the new IdentifierInfo.
1130 IdentifierInfo *II = KnownII;
1131 if (!II)
1132 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1133 k.first, k.first + k.second);
1134 Reader.SetIdentifierInfo(ID, II);
1135
Douglas Gregor2deaea32009-04-22 18:49:13 +00001136 // Set or check the various bits in the IdentifierInfo structure.
1137 // FIXME: Load token IDs lazily, too?
1138 assert((unsigned)II->getTokenID() == TokenID &&
1139 "Incorrect token ID loaded");
1140 (void)TokenID;
1141 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1142 assert(II->isExtensionToken() == ExtensionToken &&
1143 "Incorrect extension token flag");
1144 (void)ExtensionToken;
1145 II->setIsPoisoned(Poisoned);
1146 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1147 "Incorrect C++ operator keyword flag");
1148 (void)CPlusPlusOperatorKeyword;
1149
Douglas Gregor37e26842009-04-21 23:56:24 +00001150 // If this identifier is a macro, deserialize the macro
1151 // definition.
1152 if (hasMacroDefinition) {
1153 uint32_t Offset = ReadUnalignedLE64(d);
1154 Reader.ReadMacroRecord(Offset);
1155 DataLen -= 8;
1156 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001157
1158 // Read all of the declarations visible at global scope with this
1159 // name.
1160 Sema *SemaObj = Reader.getSema();
1161 while (DataLen > 0) {
1162 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001163 if (SemaObj) {
1164 // Introduce this declaration into the translation-unit scope
1165 // and add it to the declaration chain for this identifier, so
1166 // that (unqualified) name lookup will find it.
1167 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1168 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1169 } else {
1170 // Queue this declaration so that it will be added to the
1171 // translation unit scope and identifier's declaration chain
1172 // once a Sema object is known.
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001173 Reader.PreloadedDecls.push_back(D);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001174 }
1175
1176 DataLen -= 4;
1177 }
1178 return II;
1179 }
1180};
1181
1182} // end anonymous namespace
1183
1184/// \brief The on-disk hash table used to contain information about
1185/// all of the identifiers in the program.
1186typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1187 PCHIdentifierLookupTable;
1188
Douglas Gregor2cf26342009-04-09 22:27:44 +00001189// FIXME: use the diagnostics machinery
1190static bool Error(const char *Str) {
1191 std::fprintf(stderr, "%s\n", Str);
1192 return true;
1193}
1194
Douglas Gregore1d918e2009-04-10 23:10:45 +00001195/// \brief Check the contents of the predefines buffer against the
1196/// contents of the predefines buffer used to build the PCH file.
1197///
1198/// The contents of the two predefines buffers should be the same. If
1199/// not, then some command-line option changed the preprocessor state
1200/// and we must reject the PCH file.
1201///
1202/// \param PCHPredef The start of the predefines buffer in the PCH
1203/// file.
1204///
1205/// \param PCHPredefLen The length of the predefines buffer in the PCH
1206/// file.
1207///
1208/// \param PCHBufferID The FileID for the PCH predefines buffer.
1209///
1210/// \returns true if there was a mismatch (in which case the PCH file
1211/// should be ignored), or false otherwise.
1212bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1213 unsigned PCHPredefLen,
1214 FileID PCHBufferID) {
1215 const char *Predef = PP.getPredefines().c_str();
1216 unsigned PredefLen = PP.getPredefines().size();
1217
1218 // If the two predefines buffers compare equal, we're done!.
1219 if (PredefLen == PCHPredefLen &&
1220 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1221 return false;
1222
1223 // The predefines buffers are different. Produce a reasonable
1224 // diagnostic showing where they are different.
1225
1226 // The source locations (potentially in the two different predefines
1227 // buffers)
1228 SourceLocation Loc1, Loc2;
1229 SourceManager &SourceMgr = PP.getSourceManager();
1230
1231 // Create a source buffer for our predefines string, so
1232 // that we can build a diagnostic that points into that
1233 // source buffer.
1234 FileID BufferID;
1235 if (Predef && Predef[0]) {
1236 llvm::MemoryBuffer *Buffer
1237 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1238 "<built-in>");
1239 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1240 }
1241
1242 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1243 std::pair<const char *, const char *> Locations
1244 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1245
1246 if (Locations.first != Predef + MinLen) {
1247 // We found the location in the two buffers where there is a
1248 // difference. Form source locations to point there (in both
1249 // buffers).
1250 unsigned Offset = Locations.first - Predef;
1251 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1252 .getFileLocWithOffset(Offset);
1253 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1254 .getFileLocWithOffset(Offset);
1255 } else if (PredefLen > PCHPredefLen) {
1256 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1257 .getFileLocWithOffset(MinLen);
1258 } else {
1259 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1260 .getFileLocWithOffset(MinLen);
1261 }
1262
1263 Diag(Loc1, diag::warn_pch_preprocessor);
1264 if (Loc2.isValid())
1265 Diag(Loc2, diag::note_predef_in_pch);
1266 Diag(diag::note_ignoring_pch) << FileName;
1267 return true;
1268}
1269
Douglas Gregorbd945002009-04-13 16:31:14 +00001270/// \brief Read the line table in the source manager block.
1271/// \returns true if ther was an error.
1272static bool ParseLineTable(SourceManager &SourceMgr,
1273 llvm::SmallVectorImpl<uint64_t> &Record) {
1274 unsigned Idx = 0;
1275 LineTableInfo &LineTable = SourceMgr.getLineTable();
1276
1277 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001278 std::map<int, int> FileIDs;
1279 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001280 // Extract the file name
1281 unsigned FilenameLen = Record[Idx++];
1282 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1283 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +00001284 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1285 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001286 }
1287
1288 // Parse the line entries
1289 std::vector<LineEntry> Entries;
1290 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +00001291 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +00001292
1293 // Extract the line entries
1294 unsigned NumEntries = Record[Idx++];
1295 Entries.clear();
1296 Entries.reserve(NumEntries);
1297 for (unsigned I = 0; I != NumEntries; ++I) {
1298 unsigned FileOffset = Record[Idx++];
1299 unsigned LineNo = Record[Idx++];
1300 int FilenameID = Record[Idx++];
1301 SrcMgr::CharacteristicKind FileKind
1302 = (SrcMgr::CharacteristicKind)Record[Idx++];
1303 unsigned IncludeOffset = Record[Idx++];
1304 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1305 FileKind, IncludeOffset));
1306 }
1307 LineTable.AddEntry(FID, Entries);
1308 }
1309
1310 return false;
1311}
1312
Douglas Gregor14f79002009-04-10 03:52:48 +00001313/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001314PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001315 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001316 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1317 Error("Malformed source manager block record");
1318 return Failure;
1319 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001320
1321 SourceManager &SourceMgr = Context.getSourceManager();
1322 RecordData Record;
1323 while (true) {
1324 unsigned Code = Stream.ReadCode();
1325 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001326 if (Stream.ReadBlockEnd()) {
1327 Error("Error at end of Source Manager block");
1328 return Failure;
1329 }
1330
1331 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001332 }
1333
1334 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1335 // No known subblocks, always skip them.
1336 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001337 if (Stream.SkipBlock()) {
1338 Error("Malformed block record");
1339 return Failure;
1340 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001341 continue;
1342 }
1343
1344 if (Code == llvm::bitc::DEFINE_ABBREV) {
1345 Stream.ReadAbbrevRecord();
1346 continue;
1347 }
1348
1349 // Read a record.
1350 const char *BlobStart;
1351 unsigned BlobLen;
1352 Record.clear();
1353 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1354 default: // Default behavior: ignore.
1355 break;
1356
1357 case pch::SM_SLOC_FILE_ENTRY: {
1358 // FIXME: We would really like to delay the creation of this
1359 // FileEntry until it is actually required, e.g., when producing
1360 // a diagnostic with a source location in this file.
1361 const FileEntry *File
1362 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1363 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001364 FileID ID = SourceMgr.createFileID(File,
1365 SourceLocation::getFromRawEncoding(Record[1]),
1366 (CharacteristicKind)Record[2]);
1367 if (Record[3])
1368 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1369 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001370 break;
1371 }
1372
1373 case pch::SM_SLOC_BUFFER_ENTRY: {
1374 const char *Name = BlobStart;
1375 unsigned Code = Stream.ReadCode();
1376 Record.clear();
1377 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1378 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001379 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001380 llvm::MemoryBuffer *Buffer
1381 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1382 BlobStart + BlobLen - 1,
1383 Name);
1384 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1385
1386 if (strcmp(Name, "<built-in>") == 0
1387 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1388 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001389 break;
1390 }
1391
1392 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1393 SourceLocation SpellingLoc
1394 = SourceLocation::getFromRawEncoding(Record[1]);
1395 SourceMgr.createInstantiationLoc(
1396 SpellingLoc,
1397 SourceLocation::getFromRawEncoding(Record[2]),
1398 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001399 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001400 break;
1401 }
1402
Chris Lattner2c78b872009-04-14 23:22:57 +00001403 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001404 if (ParseLineTable(SourceMgr, Record))
1405 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001406 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001407 }
1408 }
1409}
1410
Douglas Gregor37e26842009-04-21 23:56:24 +00001411void PCHReader::ReadMacroRecord(uint64_t Offset) {
1412 // Keep track of where we are in the stream, then jump back there
1413 // after reading this macro.
1414 SavedStreamPosition SavedPosition(Stream);
1415
1416 Stream.JumpToBit(Offset);
1417 RecordData Record;
1418 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1419 MacroInfo *Macro = 0;
1420 while (true) {
1421 unsigned Code = Stream.ReadCode();
1422 switch (Code) {
1423 case llvm::bitc::END_BLOCK:
1424 return;
1425
1426 case llvm::bitc::ENTER_SUBBLOCK:
1427 // No known subblocks, always skip them.
1428 Stream.ReadSubBlockID();
1429 if (Stream.SkipBlock()) {
1430 Error("Malformed block record");
1431 return;
1432 }
1433 continue;
1434
1435 case llvm::bitc::DEFINE_ABBREV:
1436 Stream.ReadAbbrevRecord();
1437 continue;
1438 default: break;
1439 }
1440
1441 // Read a record.
1442 Record.clear();
1443 pch::PreprocessorRecordTypes RecType =
1444 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1445 switch (RecType) {
1446 case pch::PP_COUNTER_VALUE:
1447 // Skip this record.
1448 break;
1449
1450 case pch::PP_MACRO_OBJECT_LIKE:
1451 case pch::PP_MACRO_FUNCTION_LIKE: {
1452 // If we already have a macro, that means that we've hit the end
1453 // of the definition of the macro we were looking for. We're
1454 // done.
1455 if (Macro)
1456 return;
1457
1458 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1459 if (II == 0) {
1460 Error("Macro must have a name");
1461 return;
1462 }
1463 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1464 bool isUsed = Record[2];
1465
1466 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1467 MI->setIsUsed(isUsed);
1468
1469 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1470 // Decode function-like macro info.
1471 bool isC99VarArgs = Record[3];
1472 bool isGNUVarArgs = Record[4];
1473 MacroArgs.clear();
1474 unsigned NumArgs = Record[5];
1475 for (unsigned i = 0; i != NumArgs; ++i)
1476 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1477
1478 // Install function-like macro info.
1479 MI->setIsFunctionLike();
1480 if (isC99VarArgs) MI->setIsC99Varargs();
1481 if (isGNUVarArgs) MI->setIsGNUVarargs();
1482 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1483 PP.getPreprocessorAllocator());
1484 }
1485
1486 // Finally, install the macro.
1487 PP.setMacroInfo(II, MI);
1488
1489 // Remember that we saw this macro last so that we add the tokens that
1490 // form its body to it.
1491 Macro = MI;
1492 ++NumMacrosRead;
1493 break;
1494 }
1495
1496 case pch::PP_TOKEN: {
1497 // If we see a TOKEN before a PP_MACRO_*, then the file is
1498 // erroneous, just pretend we didn't see this.
1499 if (Macro == 0) break;
1500
1501 Token Tok;
1502 Tok.startToken();
1503 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1504 Tok.setLength(Record[1]);
1505 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1506 Tok.setIdentifierInfo(II);
1507 Tok.setKind((tok::TokenKind)Record[3]);
1508 Tok.setFlag((Token::TokenFlags)Record[4]);
1509 Macro->AddTokenToBody(Tok);
1510 break;
1511 }
1512 }
1513 }
1514}
1515
Chris Lattner42d42b52009-04-10 21:41:48 +00001516bool PCHReader::ReadPreprocessorBlock() {
1517 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1518 return Error("Malformed preprocessor block record");
1519
Chris Lattner42d42b52009-04-10 21:41:48 +00001520 RecordData Record;
Chris Lattner42d42b52009-04-10 21:41:48 +00001521 while (true) {
1522 unsigned Code = Stream.ReadCode();
1523 switch (Code) {
1524 case llvm::bitc::END_BLOCK:
1525 if (Stream.ReadBlockEnd())
1526 return Error("Error at end of preprocessor block");
1527 return false;
1528
1529 case llvm::bitc::ENTER_SUBBLOCK:
1530 // No known subblocks, always skip them.
1531 Stream.ReadSubBlockID();
1532 if (Stream.SkipBlock())
1533 return Error("Malformed block record");
1534 continue;
1535
1536 case llvm::bitc::DEFINE_ABBREV:
1537 Stream.ReadAbbrevRecord();
1538 continue;
1539 default: break;
1540 }
1541
1542 // Read a record.
1543 Record.clear();
1544 pch::PreprocessorRecordTypes RecType =
1545 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1546 switch (RecType) {
1547 default: // Default behavior: ignore unknown records.
1548 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001549 case pch::PP_COUNTER_VALUE:
1550 if (!Record.empty())
1551 PP.setCounterValue(Record[0]);
1552 break;
1553
Chris Lattner42d42b52009-04-10 21:41:48 +00001554 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregor37e26842009-04-21 23:56:24 +00001555 case pch::PP_MACRO_FUNCTION_LIKE:
1556 case pch::PP_TOKEN:
1557 // Once we've hit a macro definition or a token, we're done.
1558 return false;
Chris Lattner42d42b52009-04-10 21:41:48 +00001559 }
1560 }
1561}
1562
Douglas Gregor668c1a42009-04-21 22:25:48 +00001563PCHReader::PCHReadResult
1564PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001565 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1566 Error("Malformed block record");
1567 return Failure;
1568 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001569
1570 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001571 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001572 while (!Stream.AtEndOfStream()) {
1573 unsigned Code = Stream.ReadCode();
1574 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001575 if (Stream.ReadBlockEnd()) {
1576 Error("Error at end of module block");
1577 return Failure;
1578 }
Chris Lattner7356a312009-04-11 21:15:38 +00001579
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001580 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001581 }
1582
1583 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1584 switch (Stream.ReadSubBlockID()) {
1585 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1586 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1587 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001588 if (Stream.SkipBlock()) {
1589 Error("Malformed block record");
1590 return Failure;
1591 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001592 break;
1593
Chris Lattner7356a312009-04-11 21:15:38 +00001594 case pch::PREPROCESSOR_BLOCK_ID:
1595 // Skip the preprocessor block for now, but remember where it is. We
1596 // want to read it in after the identifier table.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001597 if (PreprocessorBlockOffset) {
Chris Lattner7356a312009-04-11 21:15:38 +00001598 Error("Multiple preprocessor blocks found.");
1599 return Failure;
1600 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001601 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001602 if (Stream.SkipBlock()) {
1603 Error("Malformed block record");
1604 return Failure;
1605 }
1606 break;
1607
Douglas Gregor14f79002009-04-10 03:52:48 +00001608 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001609 switch (ReadSourceManagerBlock()) {
1610 case Success:
1611 break;
1612
1613 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001614 Error("Malformed source manager block");
1615 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001616
1617 case IgnorePCH:
1618 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001619 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001620 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001621 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001622 continue;
1623 }
1624
1625 if (Code == llvm::bitc::DEFINE_ABBREV) {
1626 Stream.ReadAbbrevRecord();
1627 continue;
1628 }
1629
1630 // Read and process a record.
1631 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001632 const char *BlobStart = 0;
1633 unsigned BlobLen = 0;
1634 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1635 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001636 default: // Default behavior: ignore.
1637 break;
1638
1639 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001640 if (!TypeOffsets.empty()) {
1641 Error("Duplicate TYPE_OFFSET record in PCH file");
1642 return Failure;
1643 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001644 TypeOffsets.swap(Record);
1645 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1646 break;
1647
1648 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001649 if (!DeclOffsets.empty()) {
1650 Error("Duplicate DECL_OFFSET record in PCH file");
1651 return Failure;
1652 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001653 DeclOffsets.swap(Record);
1654 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1655 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001656
1657 case pch::LANGUAGE_OPTIONS:
1658 if (ParseLanguageOptions(Record))
1659 return IgnorePCH;
1660 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001661
Douglas Gregorafaf3082009-04-11 00:14:32 +00001662 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001663 std::string TargetTriple(BlobStart, BlobLen);
1664 if (TargetTriple != Context.Target.getTargetTriple()) {
1665 Diag(diag::warn_pch_target_triple)
1666 << TargetTriple << Context.Target.getTargetTriple();
1667 Diag(diag::note_ignoring_pch) << FileName;
1668 return IgnorePCH;
1669 }
1670 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001671 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001672
1673 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001674 IdentifierTableData = BlobStart;
1675 IdentifierLookupTable
1676 = PCHIdentifierLookupTable::Create(
1677 (const unsigned char *)IdentifierTableData + Record[0],
1678 (const unsigned char *)IdentifierTableData,
1679 PCHIdentifierLookupTrait(*this));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001680 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001681 break;
1682
1683 case pch::IDENTIFIER_OFFSET:
1684 if (!IdentifierData.empty()) {
1685 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1686 return Failure;
1687 }
1688 IdentifierData.swap(Record);
1689#ifndef NDEBUG
1690 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1691 if ((IdentifierData[I] & 0x01) == 0) {
1692 Error("Malformed identifier table in the precompiled header");
1693 return Failure;
1694 }
1695 }
1696#endif
1697 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001698
1699 case pch::EXTERNAL_DEFINITIONS:
1700 if (!ExternalDefinitions.empty()) {
1701 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1702 return Failure;
1703 }
1704 ExternalDefinitions.swap(Record);
1705 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001706
Douglas Gregorad1de002009-04-18 05:55:16 +00001707 case pch::SPECIAL_TYPES:
1708 SpecialTypes.swap(Record);
1709 break;
1710
Douglas Gregor3e1af842009-04-17 22:13:46 +00001711 case pch::STATISTICS:
1712 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001713 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001714 TotalLexicalDeclContexts = Record[2];
1715 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001716 break;
1717
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001718 case pch::TENTATIVE_DEFINITIONS:
1719 if (!TentativeDefinitions.empty()) {
1720 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1721 return Failure;
1722 }
1723 TentativeDefinitions.swap(Record);
1724 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001725
1726 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1727 if (!LocallyScopedExternalDecls.empty()) {
1728 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1729 return Failure;
1730 }
1731 LocallyScopedExternalDecls.swap(Record);
1732 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001733 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001734 }
1735
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001736 Error("Premature end of bitstream");
1737 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001738}
1739
Douglas Gregore1d918e2009-04-10 23:10:45 +00001740PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001741 // Set the PCH file name.
1742 this->FileName = FileName;
1743
Douglas Gregor2cf26342009-04-09 22:27:44 +00001744 // Open the PCH file.
1745 std::string ErrStr;
1746 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001747 if (!Buffer) {
1748 Error(ErrStr.c_str());
1749 return IgnorePCH;
1750 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001751
1752 // Initialize the stream
1753 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1754 (const unsigned char *)Buffer->getBufferEnd());
1755
1756 // Sniff for the signature.
1757 if (Stream.Read(8) != 'C' ||
1758 Stream.Read(8) != 'P' ||
1759 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001760 Stream.Read(8) != 'H') {
1761 Error("Not a PCH file");
1762 return IgnorePCH;
1763 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001764
1765 // We expect a number of well-defined blocks, though we don't necessarily
1766 // need to understand them all.
Douglas Gregor668c1a42009-04-21 22:25:48 +00001767 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 while (!Stream.AtEndOfStream()) {
1769 unsigned Code = Stream.ReadCode();
1770
Douglas Gregore1d918e2009-04-10 23:10:45 +00001771 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1772 Error("Invalid record at top-level");
1773 return Failure;
1774 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001775
1776 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001777
Douglas Gregor2cf26342009-04-09 22:27:44 +00001778 // We only know the PCH subblock ID.
1779 switch (BlockID) {
1780 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001781 if (Stream.ReadBlockInfoBlock()) {
1782 Error("Malformed BlockInfoBlock");
1783 return Failure;
1784 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001785 break;
1786 case pch::PCH_BLOCK_ID:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001787 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001788 case Success:
1789 break;
1790
1791 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001792 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001793
1794 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001795 // FIXME: We could consider reading through to the end of this
1796 // PCH block, skipping subblocks, to see if there are other
1797 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001798 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001799 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001800 break;
1801 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001802 if (Stream.SkipBlock()) {
1803 Error("Malformed block record");
1804 return Failure;
1805 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001806 break;
1807 }
1808 }
1809
1810 // Load the translation unit declaration
1811 ReadDeclRecord(DeclOffsets[0], 0);
1812
Douglas Gregor668c1a42009-04-21 22:25:48 +00001813 // Initialization of builtins and library builtins occurs before the
1814 // PCH file is read, so there may be some identifiers that were
1815 // loaded into the IdentifierTable before we intercepted the
1816 // creation of identifiers. Iterate through the list of known
1817 // identifiers and determine whether we have to establish
1818 // preprocessor definitions or top-level identifier declaration
1819 // chains for those identifiers.
1820 //
1821 // We copy the IdentifierInfo pointers to a small vector first,
1822 // since de-serializing declarations or macro definitions can add
1823 // new entries into the identifier table, invalidating the
1824 // iterators.
1825 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1826 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1827 IdEnd = PP.getIdentifierTable().end();
1828 Id != IdEnd; ++Id)
1829 Identifiers.push_back(Id->second);
1830 PCHIdentifierLookupTable *IdTable
1831 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1832 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1833 IdentifierInfo *II = Identifiers[I];
1834 // Look in the on-disk hash table for an entry for
1835 PCHIdentifierLookupTrait Info(*this, II);
1836 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1837 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1838 if (Pos == IdTable->end())
1839 continue;
1840
1841 // Dereferencing the iterator has the effect of populating the
1842 // IdentifierInfo node with the various declarations it needs.
1843 (void)*Pos;
1844 }
1845
Douglas Gregorad1de002009-04-18 05:55:16 +00001846 // Load the special types.
1847 Context.setBuiltinVaListType(
1848 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1849
Douglas Gregor668c1a42009-04-21 22:25:48 +00001850 // If we saw the preprocessor block, read it now.
1851 if (PreprocessorBlockOffset) {
1852 SavedStreamPosition SavedPos(Stream);
1853 Stream.JumpToBit(PreprocessorBlockOffset);
1854 if (ReadPreprocessorBlock()) {
1855 Error("Malformed preprocessor block");
1856 return Failure;
Douglas Gregor0b748912009-04-14 21:18:50 +00001857 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001858 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001859
Douglas Gregor668c1a42009-04-21 22:25:48 +00001860 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00001861}
1862
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001863/// \brief Parse the record that corresponds to a LangOptions data
1864/// structure.
1865///
1866/// This routine compares the language options used to generate the
1867/// PCH file against the language options set for the current
1868/// compilation. For each option, we classify differences between the
1869/// two compiler states as either "benign" or "important". Benign
1870/// differences don't matter, and we accept them without complaint
1871/// (and without modifying the language options). Differences between
1872/// the states for important options cause the PCH file to be
1873/// unusable, so we emit a warning and return true to indicate that
1874/// there was an error.
1875///
1876/// \returns true if the PCH file is unacceptable, false otherwise.
1877bool PCHReader::ParseLanguageOptions(
1878 const llvm::SmallVectorImpl<uint64_t> &Record) {
1879 const LangOptions &LangOpts = Context.getLangOptions();
1880#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1881#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1882 if (Record[Idx] != LangOpts.Option) { \
1883 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1884 Diag(diag::note_ignoring_pch) << FileName; \
1885 return true; \
1886 } \
1887 ++Idx
1888
1889 unsigned Idx = 0;
1890 PARSE_LANGOPT_BENIGN(Trigraphs);
1891 PARSE_LANGOPT_BENIGN(BCPLComment);
1892 PARSE_LANGOPT_BENIGN(DollarIdents);
1893 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1894 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1895 PARSE_LANGOPT_BENIGN(ImplicitInt);
1896 PARSE_LANGOPT_BENIGN(Digraphs);
1897 PARSE_LANGOPT_BENIGN(HexFloats);
1898 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1899 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1900 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1901 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1902 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1903 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1904 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1905 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1906 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1907 PARSE_LANGOPT_BENIGN(PascalStrings);
1908 PARSE_LANGOPT_BENIGN(Boolean);
1909 PARSE_LANGOPT_BENIGN(WritableStrings);
1910 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1911 diag::warn_pch_lax_vector_conversions);
1912 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1913 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1914 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1915 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1916 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1917 diag::warn_pch_thread_safe_statics);
1918 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1919 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1920 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1921 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1922 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1923 diag::warn_pch_heinous_extensions);
1924 // FIXME: Most of the options below are benign if the macro wasn't
1925 // used. Unfortunately, this means that a PCH compiled without
1926 // optimization can't be used with optimization turned on, even
1927 // though the only thing that changes is whether __OPTIMIZE__ was
1928 // defined... but if __OPTIMIZE__ never showed up in the header, it
1929 // doesn't matter. We could consider making this some special kind
1930 // of check.
1931 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1932 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1933 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1934 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1935 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1936 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1937 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1938 Diag(diag::warn_pch_gc_mode)
1939 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1940 Diag(diag::note_ignoring_pch) << FileName;
1941 return true;
1942 }
1943 ++Idx;
1944 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1945 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1946#undef PARSE_LANGOPT_IRRELEVANT
1947#undef PARSE_LANGOPT_BENIGN
1948
1949 return false;
1950}
1951
Douglas Gregor2cf26342009-04-09 22:27:44 +00001952/// \brief Read and return the type at the given offset.
1953///
1954/// This routine actually reads the record corresponding to the type
1955/// at the given offset in the bitstream. It is a helper routine for
1956/// GetType, which deals with reading type IDs.
1957QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001958 // Keep track of where we are in the stream, then jump back there
1959 // after reading this type.
1960 SavedStreamPosition SavedPosition(Stream);
1961
Douglas Gregor2cf26342009-04-09 22:27:44 +00001962 Stream.JumpToBit(Offset);
1963 RecordData Record;
1964 unsigned Code = Stream.ReadCode();
1965 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001966 case pch::TYPE_EXT_QUAL: {
1967 assert(Record.size() == 3 &&
1968 "Incorrect encoding of extended qualifier type");
1969 QualType Base = GetType(Record[0]);
1970 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1971 unsigned AddressSpace = Record[2];
1972
1973 QualType T = Base;
1974 if (GCAttr != QualType::GCNone)
1975 T = Context.getObjCGCQualType(T, GCAttr);
1976 if (AddressSpace)
1977 T = Context.getAddrSpaceQualType(T, AddressSpace);
1978 return T;
1979 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001980
Douglas Gregor2cf26342009-04-09 22:27:44 +00001981 case pch::TYPE_FIXED_WIDTH_INT: {
1982 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1983 return Context.getFixedWidthIntType(Record[0], Record[1]);
1984 }
1985
1986 case pch::TYPE_COMPLEX: {
1987 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1988 QualType ElemType = GetType(Record[0]);
1989 return Context.getComplexType(ElemType);
1990 }
1991
1992 case pch::TYPE_POINTER: {
1993 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1994 QualType PointeeType = GetType(Record[0]);
1995 return Context.getPointerType(PointeeType);
1996 }
1997
1998 case pch::TYPE_BLOCK_POINTER: {
1999 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2000 QualType PointeeType = GetType(Record[0]);
2001 return Context.getBlockPointerType(PointeeType);
2002 }
2003
2004 case pch::TYPE_LVALUE_REFERENCE: {
2005 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2006 QualType PointeeType = GetType(Record[0]);
2007 return Context.getLValueReferenceType(PointeeType);
2008 }
2009
2010 case pch::TYPE_RVALUE_REFERENCE: {
2011 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2012 QualType PointeeType = GetType(Record[0]);
2013 return Context.getRValueReferenceType(PointeeType);
2014 }
2015
2016 case pch::TYPE_MEMBER_POINTER: {
2017 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2018 QualType PointeeType = GetType(Record[0]);
2019 QualType ClassType = GetType(Record[1]);
2020 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2021 }
2022
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002023 case pch::TYPE_CONSTANT_ARRAY: {
2024 QualType ElementType = GetType(Record[0]);
2025 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2026 unsigned IndexTypeQuals = Record[2];
2027 unsigned Idx = 3;
2028 llvm::APInt Size = ReadAPInt(Record, Idx);
2029 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2030 }
2031
2032 case pch::TYPE_INCOMPLETE_ARRAY: {
2033 QualType ElementType = GetType(Record[0]);
2034 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2035 unsigned IndexTypeQuals = Record[2];
2036 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2037 }
2038
2039 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002040 QualType ElementType = GetType(Record[0]);
2041 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2042 unsigned IndexTypeQuals = Record[2];
2043 return Context.getVariableArrayType(ElementType, ReadExpr(),
2044 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002045 }
2046
2047 case pch::TYPE_VECTOR: {
2048 if (Record.size() != 2) {
2049 Error("Incorrect encoding of vector type in PCH file");
2050 return QualType();
2051 }
2052
2053 QualType ElementType = GetType(Record[0]);
2054 unsigned NumElements = Record[1];
2055 return Context.getVectorType(ElementType, NumElements);
2056 }
2057
2058 case pch::TYPE_EXT_VECTOR: {
2059 if (Record.size() != 2) {
2060 Error("Incorrect encoding of extended vector type in PCH file");
2061 return QualType();
2062 }
2063
2064 QualType ElementType = GetType(Record[0]);
2065 unsigned NumElements = Record[1];
2066 return Context.getExtVectorType(ElementType, NumElements);
2067 }
2068
2069 case pch::TYPE_FUNCTION_NO_PROTO: {
2070 if (Record.size() != 1) {
2071 Error("Incorrect encoding of no-proto function type");
2072 return QualType();
2073 }
2074 QualType ResultType = GetType(Record[0]);
2075 return Context.getFunctionNoProtoType(ResultType);
2076 }
2077
2078 case pch::TYPE_FUNCTION_PROTO: {
2079 QualType ResultType = GetType(Record[0]);
2080 unsigned Idx = 1;
2081 unsigned NumParams = Record[Idx++];
2082 llvm::SmallVector<QualType, 16> ParamTypes;
2083 for (unsigned I = 0; I != NumParams; ++I)
2084 ParamTypes.push_back(GetType(Record[Idx++]));
2085 bool isVariadic = Record[Idx++];
2086 unsigned Quals = Record[Idx++];
2087 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2088 isVariadic, Quals);
2089 }
2090
2091 case pch::TYPE_TYPEDEF:
2092 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2093 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2094
2095 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00002096 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002097
2098 case pch::TYPE_TYPEOF: {
2099 if (Record.size() != 1) {
2100 Error("Incorrect encoding of typeof(type) in PCH file");
2101 return QualType();
2102 }
2103 QualType UnderlyingType = GetType(Record[0]);
2104 return Context.getTypeOfType(UnderlyingType);
2105 }
2106
2107 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00002108 assert(Record.size() == 1 && "Incorrect encoding of record type");
2109 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002110
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002111 case pch::TYPE_ENUM:
2112 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2113 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2114
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002115 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner4dcf151a2009-04-22 05:57:30 +00002116 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2117 return Context.getObjCInterfaceType(
2118 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002119
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002120 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2121 unsigned Idx = 0;
2122 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2123 unsigned NumProtos = Record[Idx++];
2124 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2125 for (unsigned I = 0; I != NumProtos; ++I)
2126 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2127 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2128 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002129
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002130 case pch::TYPE_OBJC_QUALIFIED_ID: {
2131 unsigned Idx = 0;
2132 unsigned NumProtos = Record[Idx++];
2133 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2134 for (unsigned I = 0; I != NumProtos; ++I)
2135 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2136 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2137 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002138 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002139 // Suppress a GCC warning
2140 return QualType();
2141}
2142
2143/// \brief Note that we have loaded the declaration with the given
2144/// Index.
2145///
2146/// This routine notes that this declaration has already been loaded,
2147/// so that future GetDecl calls will return this declaration rather
2148/// than trying to load a new declaration.
2149inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2150 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2151 DeclAlreadyLoaded[Index] = true;
2152 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2153}
2154
2155/// \brief Read the declaration at the given offset from the PCH file.
2156Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002157 // Keep track of where we are in the stream, then jump back there
2158 // after reading this declaration.
2159 SavedStreamPosition SavedPosition(Stream);
2160
Douglas Gregor2cf26342009-04-09 22:27:44 +00002161 Decl *D = 0;
2162 Stream.JumpToBit(Offset);
2163 RecordData Record;
2164 unsigned Code = Stream.ReadCode();
2165 unsigned Idx = 0;
2166 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00002167
Douglas Gregor2cf26342009-04-09 22:27:44 +00002168 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002169 case pch::DECL_ATTR:
2170 case pch::DECL_CONTEXT_LEXICAL:
2171 case pch::DECL_CONTEXT_VISIBLE:
2172 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2173 break;
2174
Douglas Gregor2cf26342009-04-09 22:27:44 +00002175 case pch::DECL_TRANSLATION_UNIT:
2176 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00002177 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002178 break;
2179
2180 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002181 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002182 break;
2183 }
2184
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002185 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002186 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002187 break;
2188 }
2189
Douglas Gregor8c700062009-04-13 21:20:57 +00002190 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002191 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2192 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00002193 break;
2194 }
2195
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002196 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002197 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2198 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002199 break;
2200 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002201
2202 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002203 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2204 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002205 break;
2206 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002207
Steve Naroff53c9d8a2009-04-20 15:06:07 +00002208 case pch::DECL_OBJC_METHOD: {
2209 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2210 Selector(), QualType(), 0);
2211 break;
2212 }
2213
Steve Naroff30833f82009-04-21 15:12:33 +00002214 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002215 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2216 break;
2217 }
2218
Steve Naroff30833f82009-04-21 15:12:33 +00002219 case pch::DECL_OBJC_IVAR: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002220 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2221 ObjCIvarDecl::None);
2222 break;
2223 }
2224
Steve Naroff30833f82009-04-21 15:12:33 +00002225 case pch::DECL_OBJC_PROTOCOL: {
2226 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2227 break;
2228 }
2229
2230 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2231 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2232 QualType(), 0);
2233 break;
2234 }
2235
2236 case pch::DECL_OBJC_CLASS: {
2237 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2238 break;
2239 }
2240
2241 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2242 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2243 break;
2244 }
2245
2246 case pch::DECL_OBJC_CATEGORY: {
2247 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2248 break;
2249 }
2250
2251 case pch::DECL_OBJC_CATEGORY_IMPL: {
2252 // FIXME: Implement.
2253 break;
2254 }
2255
2256 case pch::DECL_OBJC_IMPLEMENTATION: {
2257 // FIXME: Implement.
2258 break;
2259 }
2260
2261 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2262 // FIXME: Implement.
2263 break;
2264 }
2265
2266 case pch::DECL_OBJC_PROPERTY: {
2267 // FIXME: Implement.
2268 break;
2269 }
2270
2271 case pch::DECL_OBJC_PROPERTY_IMPL: {
2272 // FIXME: Implement.
2273 break;
2274 }
2275
Douglas Gregor8c700062009-04-13 21:20:57 +00002276 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002277 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2278 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00002279 break;
2280 }
2281
Douglas Gregor2cf26342009-04-09 22:27:44 +00002282 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002283 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2284 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002285 break;
2286 }
2287
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002288 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002289 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2290 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002291 break;
2292 }
2293
2294 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002295 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002296 QualType(), QualType(), VarDecl::None,
2297 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002298 break;
2299 }
2300
Douglas Gregor1028bc62009-04-13 22:49:25 +00002301 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002302 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00002303 break;
2304 }
2305
2306 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002307 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00002308 break;
2309 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002310 }
2311
Douglas Gregor668c1a42009-04-21 22:25:48 +00002312 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002313 if (D) {
2314 LoadedDecl(Index, D);
2315 Reader.Visit(D);
2316 }
2317
Douglas Gregor2cf26342009-04-09 22:27:44 +00002318 // If this declaration is also a declaration context, get the
2319 // offsets for its tables of lexical and visible declarations.
2320 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2321 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2322 if (Offsets.first || Offsets.second) {
2323 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2324 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2325 DeclContextOffsets[DC] = Offsets;
2326 }
2327 }
2328 assert(Idx == Record.size());
2329
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002330 if (Consumer) {
2331 // If we have deserialized a declaration that has a definition the
2332 // AST consumer might need to know about, notify the consumer
2333 // about that definition now.
2334 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2335 if (Var->isFileVarDecl() && Var->getInit()) {
2336 DeclGroupRef DG(Var);
2337 Consumer->HandleTopLevelDecl(DG);
2338 }
2339 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2340 if (Func->isThisDeclarationADefinition()) {
2341 DeclGroupRef DG(Func);
2342 Consumer->HandleTopLevelDecl(DG);
2343 }
2344 }
2345 }
2346
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347 return D;
2348}
2349
Douglas Gregor8038d512009-04-10 17:25:41 +00002350QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002351 unsigned Quals = ID & 0x07;
2352 unsigned Index = ID >> 3;
2353
2354 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2355 QualType T;
2356 switch ((pch::PredefinedTypeIDs)Index) {
2357 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2358 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2359 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2360
2361 case pch::PREDEF_TYPE_CHAR_U_ID:
2362 case pch::PREDEF_TYPE_CHAR_S_ID:
2363 // FIXME: Check that the signedness of CharTy is correct!
2364 T = Context.CharTy;
2365 break;
2366
2367 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2368 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2369 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2370 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2371 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2372 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2373 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2374 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2375 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2376 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2377 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2378 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2379 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2380 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2381 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2382 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2383 }
2384
2385 assert(!T.isNull() && "Unknown predefined type");
2386 return T.getQualifiedType(Quals);
2387 }
2388
2389 Index -= pch::NUM_PREDEF_TYPE_IDS;
2390 if (!TypeAlreadyLoaded[Index]) {
2391 // Load the type from the PCH file.
2392 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2393 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2394 TypeAlreadyLoaded[Index] = true;
2395 }
2396
2397 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2398}
2399
Douglas Gregor8038d512009-04-10 17:25:41 +00002400Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002401 if (ID == 0)
2402 return 0;
2403
2404 unsigned Index = ID - 1;
2405 if (DeclAlreadyLoaded[Index])
2406 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2407
2408 // Load the declaration from the PCH file.
2409 return ReadDeclRecord(DeclOffsets[Index], Index);
2410}
2411
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002412Stmt *PCHReader::GetStmt(uint64_t Offset) {
2413 // Keep track of where we are in the stream, then jump back there
2414 // after reading this declaration.
2415 SavedStreamPosition SavedPosition(Stream);
2416
2417 Stream.JumpToBit(Offset);
2418 return ReadStmt();
2419}
2420
Douglas Gregor2cf26342009-04-09 22:27:44 +00002421bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002422 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002423 assert(DC->hasExternalLexicalStorage() &&
2424 "DeclContext has no lexical decls in storage");
2425 uint64_t Offset = DeclContextOffsets[DC].first;
2426 assert(Offset && "DeclContext has no lexical decls in storage");
2427
Douglas Gregor0b748912009-04-14 21:18:50 +00002428 // Keep track of where we are in the stream, then jump back there
2429 // after reading this context.
2430 SavedStreamPosition SavedPosition(Stream);
2431
Douglas Gregor2cf26342009-04-09 22:27:44 +00002432 // Load the record containing all of the declarations lexically in
2433 // this context.
2434 Stream.JumpToBit(Offset);
2435 RecordData Record;
2436 unsigned Code = Stream.ReadCode();
2437 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002438 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002439 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2440
2441 // Load all of the declaration IDs
2442 Decls.clear();
2443 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002444 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002445 return false;
2446}
2447
2448bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2449 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2450 assert(DC->hasExternalVisibleStorage() &&
2451 "DeclContext has no visible decls in storage");
2452 uint64_t Offset = DeclContextOffsets[DC].second;
2453 assert(Offset && "DeclContext has no visible decls in storage");
2454
Douglas Gregor0b748912009-04-14 21:18:50 +00002455 // Keep track of where we are in the stream, then jump back there
2456 // after reading this context.
2457 SavedStreamPosition SavedPosition(Stream);
2458
Douglas Gregor2cf26342009-04-09 22:27:44 +00002459 // Load the record containing all of the declarations visible in
2460 // this context.
2461 Stream.JumpToBit(Offset);
2462 RecordData Record;
2463 unsigned Code = Stream.ReadCode();
2464 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002465 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002466 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2467 if (Record.size() == 0)
2468 return false;
2469
2470 Decls.clear();
2471
2472 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002473 while (Idx < Record.size()) {
2474 Decls.push_back(VisibleDeclaration());
2475 Decls.back().Name = ReadDeclarationName(Record, Idx);
2476
Douglas Gregor2cf26342009-04-09 22:27:44 +00002477 unsigned Size = Record[Idx++];
2478 llvm::SmallVector<unsigned, 4> & LoadedDecls
2479 = Decls.back().Declarations;
2480 LoadedDecls.reserve(Size);
2481 for (unsigned I = 0; I < Size; ++I)
2482 LoadedDecls.push_back(Record[Idx++]);
2483 }
2484
Douglas Gregor25123082009-04-22 22:34:57 +00002485 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002486 return false;
2487}
2488
Douglas Gregorfdd01722009-04-14 00:24:19 +00002489void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002490 this->Consumer = Consumer;
2491
Douglas Gregorfdd01722009-04-14 00:24:19 +00002492 if (!Consumer)
2493 return;
2494
2495 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2496 Decl *D = GetDecl(ExternalDefinitions[I]);
2497 DeclGroupRef DG(D);
2498 Consumer->HandleTopLevelDecl(DG);
2499 }
2500}
2501
Douglas Gregor2cf26342009-04-09 22:27:44 +00002502void PCHReader::PrintStats() {
2503 std::fprintf(stderr, "*** PCH Statistics:\n");
2504
2505 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2506 TypeAlreadyLoaded.end(),
2507 true);
2508 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2509 DeclAlreadyLoaded.end(),
2510 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002511 unsigned NumIdentifiersLoaded = 0;
2512 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2513 if ((IdentifierData[I] & 0x01) == 0)
2514 ++NumIdentifiersLoaded;
2515 }
2516
Douglas Gregor2cf26342009-04-09 22:27:44 +00002517 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2518 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002519 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002520 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2521 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002522 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2523 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2524 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2525 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002526 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2527 NumStatementsRead, TotalNumStatements,
2528 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor37e26842009-04-21 23:56:24 +00002529 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2530 NumMacrosRead, TotalNumMacros,
2531 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregor25123082009-04-22 22:34:57 +00002532 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2533 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2534 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2535 * 100));
2536 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2537 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2538 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2539 * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002540 std::fprintf(stderr, "\n");
2541}
2542
Douglas Gregor668c1a42009-04-21 22:25:48 +00002543void PCHReader::InitializeSema(Sema &S) {
2544 SemaObj = &S;
2545
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002546 // Makes sure any declarations that were deserialized "too early"
2547 // still get added to the identifier's declaration chains.
2548 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2549 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2550 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002551 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002552 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002553
2554 // If there were any tentative definitions, deserialize them and add
2555 // them to Sema's table of tentative definitions.
2556 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2557 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2558 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2559 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002560
2561 // If there were any locally-scoped external declarations,
2562 // deserialize them and add them to Sema's table of locally-scoped
2563 // external declarations.
2564 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2565 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2566 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2567 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002568}
2569
2570IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2571 // Try to find this name within our on-disk hash table
2572 PCHIdentifierLookupTable *IdTable
2573 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2574 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2575 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2576 if (Pos == IdTable->end())
2577 return 0;
2578
2579 // Dereferencing the iterator has the effect of building the
2580 // IdentifierInfo node and populating it with the various
2581 // declarations it needs.
2582 return *Pos;
2583}
2584
2585void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2586 assert(ID && "Non-zero identifier ID required");
2587 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2588}
2589
Chris Lattner7356a312009-04-11 21:15:38 +00002590IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002591 if (ID == 0)
2592 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002593
Douglas Gregor668c1a42009-04-21 22:25:48 +00002594 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002595 Error("No identifier table in PCH file");
2596 return 0;
2597 }
Chris Lattner7356a312009-04-11 21:15:38 +00002598
Douglas Gregorafaf3082009-04-11 00:14:32 +00002599 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002600 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002601 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregor668c1a42009-04-21 22:25:48 +00002602 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002603 }
Chris Lattner7356a312009-04-11 21:15:38 +00002604
2605 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002606}
2607
2608DeclarationName
2609PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2610 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2611 switch (Kind) {
2612 case DeclarationName::Identifier:
2613 return DeclarationName(GetIdentifierInfo(Record, Idx));
2614
2615 case DeclarationName::ObjCZeroArgSelector:
2616 case DeclarationName::ObjCOneArgSelector:
2617 case DeclarationName::ObjCMultiArgSelector:
2618 assert(false && "Unable to de-serialize Objective-C selectors");
2619 break;
2620
2621 case DeclarationName::CXXConstructorName:
2622 return Context.DeclarationNames.getCXXConstructorName(
2623 GetType(Record[Idx++]));
2624
2625 case DeclarationName::CXXDestructorName:
2626 return Context.DeclarationNames.getCXXDestructorName(
2627 GetType(Record[Idx++]));
2628
2629 case DeclarationName::CXXConversionFunctionName:
2630 return Context.DeclarationNames.getCXXConversionFunctionName(
2631 GetType(Record[Idx++]));
2632
2633 case DeclarationName::CXXOperatorName:
2634 return Context.DeclarationNames.getCXXOperatorName(
2635 (OverloadedOperatorKind)Record[Idx++]);
2636
2637 case DeclarationName::CXXUsingDirective:
2638 return DeclarationName::getUsingDirectiveName();
2639 }
2640
2641 // Required to silence GCC warning
2642 return DeclarationName();
2643}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002644
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002645/// \brief Read an integral value
2646llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2647 unsigned BitWidth = Record[Idx++];
2648 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2649 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2650 Idx += NumWords;
2651 return Result;
2652}
2653
2654/// \brief Read a signed integral value
2655llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2656 bool isUnsigned = Record[Idx++];
2657 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2658}
2659
Douglas Gregor17fc2232009-04-14 21:55:33 +00002660/// \brief Read a floating-point value
2661llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002662 return llvm::APFloat(ReadAPInt(Record, Idx));
2663}
2664
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002665// \brief Read a string
2666std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2667 unsigned Len = Record[Idx++];
2668 std::string Result(&Record[Idx], &Record[Idx] + Len);
2669 Idx += Len;
2670 return Result;
2671}
2672
2673/// \brief Reads attributes from the current stream position.
2674Attr *PCHReader::ReadAttributes() {
2675 unsigned Code = Stream.ReadCode();
2676 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2677 "Expected unabbreviated record"); (void)Code;
2678
2679 RecordData Record;
2680 unsigned Idx = 0;
2681 unsigned RecCode = Stream.ReadRecord(Code, Record);
2682 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2683 (void)RecCode;
2684
2685#define SIMPLE_ATTR(Name) \
2686 case Attr::Name: \
2687 New = ::new (Context) Name##Attr(); \
2688 break
2689
2690#define STRING_ATTR(Name) \
2691 case Attr::Name: \
2692 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2693 break
2694
2695#define UNSIGNED_ATTR(Name) \
2696 case Attr::Name: \
2697 New = ::new (Context) Name##Attr(Record[Idx++]); \
2698 break
2699
2700 Attr *Attrs = 0;
2701 while (Idx < Record.size()) {
2702 Attr *New = 0;
2703 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2704 bool IsInherited = Record[Idx++];
2705
2706 switch (Kind) {
2707 STRING_ATTR(Alias);
2708 UNSIGNED_ATTR(Aligned);
2709 SIMPLE_ATTR(AlwaysInline);
2710 SIMPLE_ATTR(AnalyzerNoReturn);
2711 STRING_ATTR(Annotate);
2712 STRING_ATTR(AsmLabel);
2713
2714 case Attr::Blocks:
2715 New = ::new (Context) BlocksAttr(
2716 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2717 break;
2718
2719 case Attr::Cleanup:
2720 New = ::new (Context) CleanupAttr(
2721 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2722 break;
2723
2724 SIMPLE_ATTR(Const);
2725 UNSIGNED_ATTR(Constructor);
2726 SIMPLE_ATTR(DLLExport);
2727 SIMPLE_ATTR(DLLImport);
2728 SIMPLE_ATTR(Deprecated);
2729 UNSIGNED_ATTR(Destructor);
2730 SIMPLE_ATTR(FastCall);
2731
2732 case Attr::Format: {
2733 std::string Type = ReadString(Record, Idx);
2734 unsigned FormatIdx = Record[Idx++];
2735 unsigned FirstArg = Record[Idx++];
2736 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2737 break;
2738 }
2739
Chris Lattnercf2a7212009-04-20 19:12:28 +00002740 SIMPLE_ATTR(GNUInline);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002741
2742 case Attr::IBOutletKind:
2743 New = ::new (Context) IBOutletAttr();
2744 break;
2745
2746 SIMPLE_ATTR(NoReturn);
2747 SIMPLE_ATTR(NoThrow);
2748 SIMPLE_ATTR(Nodebug);
2749 SIMPLE_ATTR(Noinline);
2750
2751 case Attr::NonNull: {
2752 unsigned Size = Record[Idx++];
2753 llvm::SmallVector<unsigned, 16> ArgNums;
2754 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2755 Idx += Size;
2756 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2757 break;
2758 }
2759
2760 SIMPLE_ATTR(ObjCException);
2761 SIMPLE_ATTR(ObjCNSObject);
2762 SIMPLE_ATTR(Overloadable);
2763 UNSIGNED_ATTR(Packed);
2764 SIMPLE_ATTR(Pure);
2765 UNSIGNED_ATTR(Regparm);
2766 STRING_ATTR(Section);
2767 SIMPLE_ATTR(StdCall);
2768 SIMPLE_ATTR(TransparentUnion);
2769 SIMPLE_ATTR(Unavailable);
2770 SIMPLE_ATTR(Unused);
2771 SIMPLE_ATTR(Used);
2772
2773 case Attr::Visibility:
2774 New = ::new (Context) VisibilityAttr(
2775 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2776 break;
2777
2778 SIMPLE_ATTR(WarnUnusedResult);
2779 SIMPLE_ATTR(Weak);
2780 SIMPLE_ATTR(WeakImport);
2781 }
2782
2783 assert(New && "Unable to decode attribute?");
2784 New->setInherited(IsInherited);
2785 New->setNext(Attrs);
2786 Attrs = New;
2787 }
2788#undef UNSIGNED_ATTR
2789#undef STRING_ATTR
2790#undef SIMPLE_ATTR
2791
2792 // The list of attributes was built backwards. Reverse the list
2793 // before returning it.
2794 Attr *PrevAttr = 0, *NextAttr = 0;
2795 while (Attrs) {
2796 NextAttr = Attrs->getNext();
2797 Attrs->setNext(PrevAttr);
2798 PrevAttr = Attrs;
2799 Attrs = NextAttr;
2800 }
2801
2802 return PrevAttr;
2803}
2804
Douglas Gregorc9490c02009-04-16 22:23:12 +00002805Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002806 // Within the bitstream, expressions are stored in Reverse Polish
2807 // Notation, with each of the subexpressions preceding the
2808 // expression they are stored in. To evaluate expressions, we
2809 // continue reading expressions and placing them on the stack, with
2810 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002811 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002812 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002813 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002814 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002815 llvm::SmallVector<Stmt *, 16> StmtStack;
2816 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002817 Stmt::EmptyShell Empty;
2818
Douglas Gregor087fd532009-04-14 23:32:43 +00002819 while (true) {
2820 unsigned Code = Stream.ReadCode();
2821 if (Code == llvm::bitc::END_BLOCK) {
2822 if (Stream.ReadBlockEnd()) {
2823 Error("Error at end of Source Manager block");
2824 return 0;
2825 }
2826 break;
2827 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002828
Douglas Gregor087fd532009-04-14 23:32:43 +00002829 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2830 // No known subblocks, always skip them.
2831 Stream.ReadSubBlockID();
2832 if (Stream.SkipBlock()) {
2833 Error("Malformed block record");
2834 return 0;
2835 }
2836 continue;
2837 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002838
Douglas Gregor087fd532009-04-14 23:32:43 +00002839 if (Code == llvm::bitc::DEFINE_ABBREV) {
2840 Stream.ReadAbbrevRecord();
2841 continue;
2842 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002843
Douglas Gregorc9490c02009-04-16 22:23:12 +00002844 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002845 Idx = 0;
2846 Record.clear();
2847 bool Finished = false;
2848 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002849 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002850 Finished = true;
2851 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002852
Douglas Gregorc9490c02009-04-16 22:23:12 +00002853 case pch::STMT_NULL_PTR:
2854 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002855 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002856
Douglas Gregor025452f2009-04-17 00:04:06 +00002857 case pch::STMT_NULL:
2858 S = new (Context) NullStmt(Empty);
2859 break;
2860
2861 case pch::STMT_COMPOUND:
2862 S = new (Context) CompoundStmt(Empty);
2863 break;
2864
2865 case pch::STMT_CASE:
2866 S = new (Context) CaseStmt(Empty);
2867 break;
2868
2869 case pch::STMT_DEFAULT:
2870 S = new (Context) DefaultStmt(Empty);
2871 break;
2872
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002873 case pch::STMT_LABEL:
2874 S = new (Context) LabelStmt(Empty);
2875 break;
2876
Douglas Gregor025452f2009-04-17 00:04:06 +00002877 case pch::STMT_IF:
2878 S = new (Context) IfStmt(Empty);
2879 break;
2880
2881 case pch::STMT_SWITCH:
2882 S = new (Context) SwitchStmt(Empty);
2883 break;
2884
Douglas Gregord921cf92009-04-17 00:16:09 +00002885 case pch::STMT_WHILE:
2886 S = new (Context) WhileStmt(Empty);
2887 break;
2888
Douglas Gregor67d82492009-04-17 00:29:51 +00002889 case pch::STMT_DO:
2890 S = new (Context) DoStmt(Empty);
2891 break;
2892
2893 case pch::STMT_FOR:
2894 S = new (Context) ForStmt(Empty);
2895 break;
2896
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002897 case pch::STMT_GOTO:
2898 S = new (Context) GotoStmt(Empty);
2899 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002900
2901 case pch::STMT_INDIRECT_GOTO:
2902 S = new (Context) IndirectGotoStmt(Empty);
2903 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002904
Douglas Gregord921cf92009-04-17 00:16:09 +00002905 case pch::STMT_CONTINUE:
2906 S = new (Context) ContinueStmt(Empty);
2907 break;
2908
Douglas Gregor025452f2009-04-17 00:04:06 +00002909 case pch::STMT_BREAK:
2910 S = new (Context) BreakStmt(Empty);
2911 break;
2912
Douglas Gregor0de9d882009-04-17 16:34:57 +00002913 case pch::STMT_RETURN:
2914 S = new (Context) ReturnStmt(Empty);
2915 break;
2916
Douglas Gregor84f21702009-04-17 16:55:36 +00002917 case pch::STMT_DECL:
2918 S = new (Context) DeclStmt(Empty);
2919 break;
2920
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00002921 case pch::STMT_ASM:
2922 S = new (Context) AsmStmt(Empty);
2923 break;
2924
Douglas Gregor087fd532009-04-14 23:32:43 +00002925 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002926 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002927 break;
2928
2929 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002930 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002931 break;
2932
2933 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002934 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002935 break;
2936
2937 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002938 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002939 break;
2940
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002941 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002942 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002943 break;
2944
Douglas Gregor673ecd62009-04-15 16:35:07 +00002945 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002946 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002947 Record[PCHStmtReader::NumExprFields + 1]);
2948 break;
2949
Douglas Gregor087fd532009-04-14 23:32:43 +00002950 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002951 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002952 break;
2953
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002954 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002955 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002956 break;
2957
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002958 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002959 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002960 break;
2961
2962 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002963 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002964 break;
2965
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002966 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002967 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002968 break;
2969
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002970 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002971 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002972 break;
2973
2974 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002975 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002976 break;
2977
Douglas Gregordb600c32009-04-15 00:25:59 +00002978 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002979 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002980 break;
2981
Douglas Gregorad90e962009-04-15 22:40:36 +00002982 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002983 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002984 break;
2985
2986 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002987 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002988 break;
2989
Douglas Gregor087fd532009-04-14 23:32:43 +00002990 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002991 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002992 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002993
2994 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002995 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002996 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002997
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002998 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002999 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003000 break;
3001
Douglas Gregord3c98a02009-04-15 23:02:49 +00003002 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003003 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003004 break;
3005
Douglas Gregord077d752009-04-16 00:55:48 +00003006 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003007 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003008 break;
3009
3010 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003011 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00003012 Record[PCHStmtReader::NumExprFields] - 1);
3013
3014 break;
3015
3016 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003017 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003018 break;
3019
Douglas Gregord3c98a02009-04-15 23:02:49 +00003020 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003021 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003022 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003023
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003024 case pch::EXPR_ADDR_LABEL:
3025 S = new (Context) AddrLabelExpr(Empty);
3026 break;
3027
Douglas Gregor6a2dd552009-04-17 19:05:30 +00003028 case pch::EXPR_STMT:
3029 S = new (Context) StmtExpr(Empty);
3030 break;
3031
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003032 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003033 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003034 break;
3035
3036 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003037 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003038 break;
3039
3040 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003041 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003042 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003043
3044 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003045 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003046 break;
3047
Douglas Gregor84af7c22009-04-17 19:21:43 +00003048 case pch::EXPR_BLOCK:
3049 S = new (Context) BlockExpr(Empty);
3050 break;
3051
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003052 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003053 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003054 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003055
Chris Lattner3a57a372009-04-22 06:29:42 +00003056 case pch::EXPR_OBJC_STRING_LITERAL:
3057 S = new (Context) ObjCStringLiteral(Empty);
3058 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003059 case pch::EXPR_OBJC_ENCODE:
3060 S = new (Context) ObjCEncodeExpr(Empty);
3061 break;
Chris Lattner3a57a372009-04-22 06:29:42 +00003062 case pch::EXPR_OBJC_SELECTOR_EXPR:
3063 S = new (Context) ObjCSelectorExpr(Empty);
3064 break;
3065 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3066 S = new (Context) ObjCProtocolExpr(Empty);
3067 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00003068 }
3069
Douglas Gregorc9490c02009-04-16 22:23:12 +00003070 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00003071 if (Finished)
3072 break;
3073
Douglas Gregor3e1af842009-04-17 22:13:46 +00003074 ++NumStatementsRead;
3075
Douglas Gregorc9490c02009-04-16 22:23:12 +00003076 if (S) {
3077 unsigned NumSubStmts = Reader.Visit(S);
3078 while (NumSubStmts > 0) {
3079 StmtStack.pop_back();
3080 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00003081 }
3082 }
3083
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003084 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00003085 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00003086 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00003087 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00003088 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00003089 return StmtStack.back();
3090}
3091
3092Expr *PCHReader::ReadExpr() {
3093 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00003094}
3095
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003096DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003097 return Diag(SourceLocation(), DiagID);
3098}
3099
3100DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3101 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003102 Context.getSourceManager()),
3103 DiagID);
3104}
Douglas Gregor025452f2009-04-17 00:04:06 +00003105
Douglas Gregor668c1a42009-04-21 22:25:48 +00003106/// \brief Retrieve the identifier table associated with the
3107/// preprocessor.
3108IdentifierTable &PCHReader::getIdentifierTable() {
3109 return PP.getIdentifierTable();
3110}
3111
Douglas Gregor025452f2009-04-17 00:04:06 +00003112/// \brief Record that the given ID maps to the given switch-case
3113/// statement.
3114void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3115 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3116 SwitchCaseStmts[ID] = SC;
3117}
3118
3119/// \brief Retrieve the switch-case statement with the given ID.
3120SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3121 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3122 return SwitchCaseStmts[ID];
3123}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003124
3125/// \brief Record that the given label statement has been
3126/// deserialized and has the given ID.
3127void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3128 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3129 "Deserialized label twice");
3130 LabelStmts[ID] = S;
3131
3132 // If we've already seen any goto statements that point to this
3133 // label, resolve them now.
3134 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3135 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3136 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3137 Goto->second->setLabel(S);
3138 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003139
3140 // If we've already seen any address-label statements that point to
3141 // this label, resolve them now.
3142 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3143 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3144 = UnresolvedAddrLabelExprs.equal_range(ID);
3145 for (AddrLabelIter AddrLabel = AddrLabels.first;
3146 AddrLabel != AddrLabels.second; ++AddrLabel)
3147 AddrLabel->second->setLabel(S);
3148 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003149}
3150
3151/// \brief Set the label of the given statement to the label
3152/// identified by ID.
3153///
3154/// Depending on the order in which the label and other statements
3155/// referencing that label occur, this operation may complete
3156/// immediately (updating the statement) or it may queue the
3157/// statement to be back-patched later.
3158void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3159 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3160 if (Label != LabelStmts.end()) {
3161 // We've already seen this label, so set the label of the goto and
3162 // we're done.
3163 S->setLabel(Label->second);
3164 } else {
3165 // We haven't seen this label yet, so add this goto to the set of
3166 // unresolved goto statements.
3167 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3168 }
3169}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003170
3171/// \brief Set the label of the given expression to the label
3172/// identified by ID.
3173///
3174/// Depending on the order in which the label and other statements
3175/// referencing that label occur, this operation may complete
3176/// immediately (updating the statement) or it may queue the
3177/// statement to be back-patched later.
3178void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3179 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3180 if (Label != LabelStmts.end()) {
3181 // We've already seen this label, so set the label of the
3182 // label-address expression and we're done.
3183 S->setLabel(Label->second);
3184 } else {
3185 // We haven't seen this label yet, so add this label-address
3186 // expression to the set of unresolved label-address expressions.
3187 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3188 }
3189}