blob: 3a311d49c242adc9e6b9113d7e68f5654f707940 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000039namespace {
40 /// \brief Helper class that saves the current stream position and
41 /// then restores it when destroyed.
42 struct VISIBILITY_HIDDEN SavedStreamPosition {
43 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
44 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
45
46 ~SavedStreamPosition() {
47 Stream.JumpToBit(Offset);
48 }
49
50 private:
51 llvm::BitstreamReader &Stream;
52 uint64_t Offset;
53 };
54}
55
Douglas Gregorc34897d2009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 PCHReader &Reader;
63 const PCHReader::RecordData &Record;
64 unsigned &Idx;
65
66 public:
67 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
68 unsigned &Idx)
69 : Reader(Reader), Record(Record), Idx(Idx) { }
70
71 void VisitDecl(Decl *D);
72 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
73 void VisitNamedDecl(NamedDecl *ND);
74 void VisitTypeDecl(TypeDecl *TD);
75 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000093 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
94 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
95 void VisitObjCClassDecl(ObjCClassDecl *D);
96 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
97 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
98 void VisitObjCImplDecl(ObjCImplDecl *D);
99 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
100 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
101 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
102 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
103 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000104 };
105}
106
107void PCHDeclReader::VisitDecl(Decl *D) {
108 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
109 D->setLexicalDeclContext(
110 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
112 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000115 D->setImplicit(Record[Idx++]);
116 D->setAccess((AccessSpecifier)Record[Idx++]);
117}
118
119void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
120 VisitDecl(TU);
121}
122
123void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
124 VisitDecl(ND);
125 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
126}
127
128void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
129 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000134 // Note that we cannot use VisitTypeDecl here, because we need to
135 // set the underlying type of the typedef *before* we try to read
136 // the type associated with the TypedefDecl.
137 VisitNamedDecl(TD);
138 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
139 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
140 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000141}
142
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000143void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
144 VisitTypeDecl(TD);
145 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
146 TD->setDefinition(Record[Idx++]);
147 TD->setTypedefForAnonDecl(
148 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
149}
150
151void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
152 VisitTagDecl(ED);
153 ED->setIntegerType(Reader.GetType(Record[Idx++]));
154}
155
Douglas Gregor982365e2009-04-13 21:20:57 +0000156void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
157 VisitTagDecl(RD);
158 RD->setHasFlexibleArrayMember(Record[Idx++]);
159 RD->setAnonymousStructOrUnion(Record[Idx++]);
160}
161
Douglas Gregorc34897d2009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000178 FD->setPreviousDeclaration(
179 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
180 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
181 FD->setInline(Record[Idx++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff79ea0e02009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff7333b492009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000230 unsigned NumIvars = Record[Idx++];
231 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
232 IVars.reserve(NumIvars);
233 for (unsigned I = 0; I != NumIvars; ++I)
234 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
235 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
236
237 ID->setForwardDecl(Record[Idx++]);
238 ID->setImplicitInterfaceDecl(Record[Idx++]);
239 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
240 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000242 // FIXME: add protocols, categories.
243}
244
245void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
246 VisitFieldDecl(IVD);
247 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
248}
249
Steve Naroff97b53bd2009-04-21 15:12:33 +0000250void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
251 VisitObjCContainerDecl(PD);
252 PD->setForwardDecl(Record[Idx++]);
253 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
259 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
260}
261
262void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
263 VisitFieldDecl(FD);
264}
265
266void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
267 VisitDecl(CD);
268 unsigned NumClassRefs = Record[Idx++];
269 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
270 ClassRefs.reserve(NumClassRefs);
271 for (unsigned I = 0; I != NumClassRefs; ++I)
272 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
274}
275
276void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
277 VisitDecl(FPD);
278 unsigned NumProtoRefs = Record[Idx++];
279 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
280 ProtoRefs.reserve(NumProtoRefs);
281 for (unsigned I = 0; I != NumProtoRefs; ++I)
282 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
283 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
284}
285
286void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
287 VisitObjCContainerDecl(CD);
288 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
289 unsigned NumProtoRefs = Record[Idx++];
290 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
291 ProtoRefs.reserve(NumProtoRefs);
292 for (unsigned I = 0; I != NumProtoRefs; ++I)
293 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
295 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
296 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
297}
298
299void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
300 VisitNamedDecl(CAD);
301 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
302}
303
304void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
305 VisitNamedDecl(D);
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 Gregor982365e2009-04-13 21:20:57 +0000330void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
331 VisitValueDecl(FD);
332 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000333 if (Record[Idx++])
334 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000335}
336
Douglas Gregorc34897d2009-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 Gregorc10f86f2009-04-14 21:18:50 +0000346 if (Record[Idx++])
347 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000348}
349
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000350void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
351 VisitVarDecl(PD);
352 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000353 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-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 Gregor2a491792009-04-13 22:49:25 +0000361void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
362 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000363 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000364}
365
366void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
367 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000368 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-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 Gregorc34897d2009-04-09 22:27:44 +0000377std::pair<uint64_t, uint64_t>
378PCHDeclReader::VisitDeclContext(DeclContext *DC) {
379 uint64_t LexicalOffset = Record[Idx++];
380 uint64_t VisibleOffset = 0;
381 if (DC->getPrimaryContext() == DC)
382 VisibleOffset = Record[Idx++];
383 return std::make_pair(LexicalOffset, VisibleOffset);
384}
385
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000386//===----------------------------------------------------------------------===//
387// Statement/expression deserialization
388//===----------------------------------------------------------------------===//
389namespace {
390 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000391 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000392 PCHReader &Reader;
393 const PCHReader::RecordData &Record;
394 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000395 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000396
397 public:
398 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000399 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
400 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000401
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000402 /// \brief The number of record fields required for the Stmt class
403 /// itself.
404 static const unsigned NumStmtFields = 0;
405
Douglas Gregor596e0932009-04-15 16:35:07 +0000406 /// \brief The number of record fields required for the Expr class
407 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000408 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000409
Douglas Gregora151ba42009-04-14 23:32:43 +0000410 // Each of the Visit* functions reads in part of the expression
411 // from the given record and the current expression stack, then
412 // return the total number of operands that it read from the
413 // expression stack.
414
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000415 unsigned VisitStmt(Stmt *S);
416 unsigned VisitNullStmt(NullStmt *S);
417 unsigned VisitCompoundStmt(CompoundStmt *S);
418 unsigned VisitSwitchCase(SwitchCase *S);
419 unsigned VisitCaseStmt(CaseStmt *S);
420 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000421 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000422 unsigned VisitIfStmt(IfStmt *S);
423 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000424 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000425 unsigned VisitDoStmt(DoStmt *S);
426 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000427 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000428 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000429 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000430 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000431 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000432 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000433 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000434 unsigned VisitExpr(Expr *E);
435 unsigned VisitPredefinedExpr(PredefinedExpr *E);
436 unsigned VisitDeclRefExpr(DeclRefExpr *E);
437 unsigned VisitIntegerLiteral(IntegerLiteral *E);
438 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000439 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000440 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000441 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000442 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000443 unsigned VisitUnaryOperator(UnaryOperator *E);
444 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000445 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000446 unsigned VisitCallExpr(CallExpr *E);
447 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000448 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000449 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000450 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
451 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000452 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000453 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
454 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000455 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000456 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000457 unsigned VisitInitListExpr(InitListExpr *E);
458 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
459 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000460 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000461 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000462 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000463 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
464 unsigned VisitChooseExpr(ChooseExpr *E);
465 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000466 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000467 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000468 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000469 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000470 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000471 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
472 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000473 };
474}
475
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000476unsigned PCHStmtReader::VisitStmt(Stmt *S) {
477 assert(Idx == NumStmtFields && "Incorrect statement field count");
478 return 0;
479}
480
481unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
482 VisitStmt(S);
483 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
484 return 0;
485}
486
487unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
488 VisitStmt(S);
489 unsigned NumStmts = Record[Idx++];
490 S->setStmts(Reader.getContext(),
491 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
492 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
493 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
494 return NumStmts;
495}
496
497unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
498 VisitStmt(S);
499 Reader.RecordSwitchCaseID(S, Record[Idx++]);
500 return 0;
501}
502
503unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
504 VisitSwitchCase(S);
505 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
506 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
507 S->setSubStmt(StmtStack.back());
508 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
509 return 3;
510}
511
512unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
513 VisitSwitchCase(S);
514 S->setSubStmt(StmtStack.back());
515 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
516 return 1;
517}
518
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000519unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
520 VisitStmt(S);
521 S->setID(Reader.GetIdentifierInfo(Record, Idx));
522 S->setSubStmt(StmtStack.back());
523 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
524 Reader.RecordLabelStmt(S, Record[Idx++]);
525 return 1;
526}
527
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000528unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
529 VisitStmt(S);
530 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
531 S->setThen(StmtStack[StmtStack.size() - 2]);
532 S->setElse(StmtStack[StmtStack.size() - 1]);
533 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
534 return 3;
535}
536
537unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
538 VisitStmt(S);
539 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
540 S->setBody(StmtStack.back());
541 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 SwitchCase *PrevSC = 0;
543 for (unsigned N = Record.size(); Idx != N; ++Idx) {
544 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
545 if (PrevSC)
546 PrevSC->setNextSwitchCase(SC);
547 else
548 S->setSwitchCaseList(SC);
549 PrevSC = SC;
550 }
551 return 2;
552}
553
Douglas Gregora6b503f2009-04-17 00:16:09 +0000554unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
555 VisitStmt(S);
556 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
557 S->setBody(StmtStack.back());
558 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
559 return 2;
560}
561
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000562unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
563 VisitStmt(S);
564 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
565 S->setBody(StmtStack.back());
566 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
567 return 2;
568}
569
570unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
571 VisitStmt(S);
572 S->setInit(StmtStack[StmtStack.size() - 4]);
573 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
574 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
575 S->setBody(StmtStack.back());
576 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
577 return 4;
578}
579
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000580unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
581 VisitStmt(S);
582 Reader.SetLabelOf(S, Record[Idx++]);
583 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
584 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
585 return 0;
586}
587
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000588unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
589 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000590 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000591 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
592 return 1;
593}
594
Douglas Gregora6b503f2009-04-17 00:16:09 +0000595unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
596 VisitStmt(S);
597 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 return 0;
599}
600
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000601unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
602 VisitStmt(S);
603 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 0;
605}
606
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000607unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
608 VisitStmt(S);
609 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
610 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 return 1;
612}
613
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000614unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
615 VisitStmt(S);
616 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
617 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
618
619 if (Idx + 1 == Record.size()) {
620 // Single declaration
621 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
622 } else {
623 llvm::SmallVector<Decl *, 16> Decls;
624 Decls.reserve(Record.size() - Idx);
625 for (unsigned N = Record.size(); Idx != N; ++Idx)
626 Decls.push_back(Reader.GetDecl(Record[Idx]));
627 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
628 &Decls[0], Decls.size())));
629 }
630 return 0;
631}
632
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000633unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
634 VisitStmt(S);
635 unsigned NumOutputs = Record[Idx++];
636 unsigned NumInputs = Record[Idx++];
637 unsigned NumClobbers = Record[Idx++];
638 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
639 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
640 S->setVolatile(Record[Idx++]);
641 S->setSimple(Record[Idx++]);
642
643 unsigned StackIdx
644 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
645 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
646
647 // Outputs and inputs
648 llvm::SmallVector<std::string, 16> Names;
649 llvm::SmallVector<StringLiteral*, 16> Constraints;
650 llvm::SmallVector<Stmt*, 16> Exprs;
651 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
652 Names.push_back(Reader.ReadString(Record, Idx));
653 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
654 Exprs.push_back(StmtStack[StackIdx++]);
655 }
656 S->setOutputsAndInputs(NumOutputs, NumInputs,
657 &Names[0], &Constraints[0], &Exprs[0]);
658
659 // Constraints
660 llvm::SmallVector<StringLiteral*, 16> Clobbers;
661 for (unsigned I = 0; I != NumClobbers; ++I)
662 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
663 S->setClobbers(&Clobbers[0], NumClobbers);
664
665 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
666 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
667}
668
Douglas Gregora151ba42009-04-14 23:32:43 +0000669unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000670 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000671 E->setType(Reader.GetType(Record[Idx++]));
672 E->setTypeDependent(Record[Idx++]);
673 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000674 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000675 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000676}
677
Douglas Gregora151ba42009-04-14 23:32:43 +0000678unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000679 VisitExpr(E);
680 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
681 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000682 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000683}
684
Douglas Gregora151ba42009-04-14 23:32:43 +0000685unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000686 VisitExpr(E);
687 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
688 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000689 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000690}
691
Douglas Gregora151ba42009-04-14 23:32:43 +0000692unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000693 VisitExpr(E);
694 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
695 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000696 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000697}
698
Douglas Gregora151ba42009-04-14 23:32:43 +0000699unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000700 VisitExpr(E);
701 E->setValue(Reader.ReadAPFloat(Record, Idx));
702 E->setExact(Record[Idx++]);
703 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000704 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000705}
706
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000707unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
708 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000709 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000710 return 1;
711}
712
Douglas Gregor596e0932009-04-15 16:35:07 +0000713unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
714 VisitExpr(E);
715 unsigned Len = Record[Idx++];
716 assert(Record[Idx] == E->getNumConcatenated() &&
717 "Wrong number of concatenated tokens!");
718 ++Idx;
719 E->setWide(Record[Idx++]);
720
721 // Read string data
722 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
723 E->setStrData(Reader.getContext(), &Str[0], Len);
724 Idx += Len;
725
726 // Read source locations
727 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
728 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
729
730 return 0;
731}
732
Douglas Gregora151ba42009-04-14 23:32:43 +0000733unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000734 VisitExpr(E);
735 E->setValue(Record[Idx++]);
736 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
737 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000738 return 0;
739}
740
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000741unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
742 VisitExpr(E);
743 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
744 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000745 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000746 return 1;
747}
748
Douglas Gregor12d74052009-04-15 15:58:59 +0000749unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
750 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000751 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000752 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
753 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
754 return 1;
755}
756
757unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
758 VisitExpr(E);
759 E->setSizeof(Record[Idx++]);
760 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000761 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000762 ++Idx;
763 } else {
764 E->setArgument(Reader.GetType(Record[Idx++]));
765 }
766 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
767 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 return E->isArgumentType()? 0 : 1;
769}
770
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000771unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
772 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000773 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
774 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000775 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
776 return 2;
777}
778
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000779unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
780 VisitExpr(E);
781 E->setNumArgs(Reader.getContext(), Record[Idx++]);
782 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000783 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000784 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000785 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000786 return E->getNumArgs() + 1;
787}
788
789unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
790 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000791 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000792 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
793 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 E->setArrow(Record[Idx++]);
795 return 1;
796}
797
Douglas Gregora151ba42009-04-14 23:32:43 +0000798unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
799 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000801 return 1;
802}
803
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000804unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
805 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000806 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
807 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000808 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
809 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
810 return 2;
811}
812
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000813unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
814 VisitBinaryOperator(E);
815 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
816 E->setComputationResultType(Reader.GetType(Record[Idx++]));
817 return 2;
818}
819
820unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
821 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000822 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
823 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
824 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000825 return 3;
826}
827
Douglas Gregora151ba42009-04-14 23:32:43 +0000828unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
829 VisitCastExpr(E);
830 E->setLvalueCast(Record[Idx++]);
831 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000832}
833
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000834unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
835 VisitCastExpr(E);
836 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
837 return 1;
838}
839
840unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
841 VisitExplicitCastExpr(E);
842 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
843 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
844 return 1;
845}
846
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000847unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
848 VisitExpr(E);
849 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000850 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000851 E->setFileScope(Record[Idx++]);
852 return 1;
853}
854
Douglas Gregorec0b8292009-04-15 23:02:49 +0000855unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
856 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000857 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000858 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
859 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
860 return 1;
861}
862
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000863unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
864 VisitExpr(E);
865 unsigned NumInits = Record[Idx++];
866 E->reserveInits(NumInits);
867 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000868 E->updateInit(I,
869 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
870 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000871 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
872 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
873 E->setInitializedFieldInUnion(
874 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
875 E->sawArrayRangeDesignator(Record[Idx++]);
876 return NumInits + 1;
877}
878
879unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
880 typedef DesignatedInitExpr::Designator Designator;
881
882 VisitExpr(E);
883 unsigned NumSubExprs = Record[Idx++];
884 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
885 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000886 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000887 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
888 E->setGNUSyntax(Record[Idx++]);
889
890 llvm::SmallVector<Designator, 4> Designators;
891 while (Idx < Record.size()) {
892 switch ((pch::DesignatorTypes)Record[Idx++]) {
893 case pch::DESIG_FIELD_DECL: {
894 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
895 SourceLocation DotLoc
896 = SourceLocation::getFromRawEncoding(Record[Idx++]);
897 SourceLocation FieldLoc
898 = SourceLocation::getFromRawEncoding(Record[Idx++]);
899 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
900 FieldLoc));
901 Designators.back().setField(Field);
902 break;
903 }
904
905 case pch::DESIG_FIELD_NAME: {
906 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
907 SourceLocation DotLoc
908 = SourceLocation::getFromRawEncoding(Record[Idx++]);
909 SourceLocation FieldLoc
910 = SourceLocation::getFromRawEncoding(Record[Idx++]);
911 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
912 break;
913 }
914
915 case pch::DESIG_ARRAY: {
916 unsigned Index = Record[Idx++];
917 SourceLocation LBracketLoc
918 = SourceLocation::getFromRawEncoding(Record[Idx++]);
919 SourceLocation RBracketLoc
920 = SourceLocation::getFromRawEncoding(Record[Idx++]);
921 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
922 break;
923 }
924
925 case pch::DESIG_ARRAY_RANGE: {
926 unsigned Index = Record[Idx++];
927 SourceLocation LBracketLoc
928 = SourceLocation::getFromRawEncoding(Record[Idx++]);
929 SourceLocation EllipsisLoc
930 = SourceLocation::getFromRawEncoding(Record[Idx++]);
931 SourceLocation RBracketLoc
932 = SourceLocation::getFromRawEncoding(Record[Idx++]);
933 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
934 RBracketLoc));
935 break;
936 }
937 }
938 }
939 E->setDesignators(&Designators[0], Designators.size());
940
941 return NumSubExprs;
942}
943
944unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
945 VisitExpr(E);
946 return 0;
947}
948
Douglas Gregorec0b8292009-04-15 23:02:49 +0000949unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
950 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000951 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000952 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
953 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
954 return 1;
955}
956
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000957unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
958 VisitExpr(E);
959 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
960 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
961 Reader.SetLabelOf(E, Record[Idx++]);
962 return 0;
963}
964
Douglas Gregoreca12f62009-04-17 19:05:30 +0000965unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
966 VisitExpr(E);
967 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
968 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
969 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
970 return 1;
971}
972
Douglas Gregor209d4622009-04-15 23:33:31 +0000973unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
974 VisitExpr(E);
975 E->setArgType1(Reader.GetType(Record[Idx++]));
976 E->setArgType2(Reader.GetType(Record[Idx++]));
977 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
978 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 return 0;
980}
981
982unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
983 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000984 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
985 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
986 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000987 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
988 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
989 return 3;
990}
991
992unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
993 VisitExpr(E);
994 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 return 0;
996}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000997
Douglas Gregor725e94b2009-04-16 00:01:45 +0000998unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
999 VisitExpr(E);
1000 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001001 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001002 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 return NumExprs;
1005}
1006
Douglas Gregore246b742009-04-17 19:21:43 +00001007unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1008 VisitExpr(E);
1009 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1010 E->setHasBlockDeclRefExprs(Record[Idx++]);
1011 return 0;
1012}
1013
Douglas Gregor725e94b2009-04-16 00:01:45 +00001014unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1015 VisitExpr(E);
1016 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1017 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1018 E->setByRef(Record[Idx++]);
1019 return 0;
1020}
1021
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001022//===----------------------------------------------------------------------===//
1023// Objective-C Expressions and Statements
1024
1025unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1026 VisitExpr(E);
1027 E->setString(cast<StringLiteral>(StmtStack.back()));
1028 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1029 return 1;
1030}
1031
Chris Lattner80f83c62009-04-22 05:57:30 +00001032unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1033 VisitExpr(E);
1034 E->setEncodedType(Reader.GetType(Record[Idx++]));
1035 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1036 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1037 return 0;
1038}
1039
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001040unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1041 VisitExpr(E);
1042 // FIXME: Selectors.
1043 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1044 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1045 return 0;
1046}
1047
1048unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1049 VisitExpr(E);
1050 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1051 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1052 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1053 return 0;
1054}
1055
Chris Lattner80f83c62009-04-22 05:57:30 +00001056
Douglas Gregorc713da92009-04-21 22:25:48 +00001057//===----------------------------------------------------------------------===//
1058// PCH reader implementation
1059//===----------------------------------------------------------------------===//
1060
1061namespace {
1062class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1063 PCHReader &Reader;
1064
1065 // If we know the IdentifierInfo in advance, it is here and we will
1066 // not build a new one. Used when deserializing information about an
1067 // identifier that was constructed before the PCH file was read.
1068 IdentifierInfo *KnownII;
1069
1070public:
1071 typedef IdentifierInfo * data_type;
1072
1073 typedef const std::pair<const char*, unsigned> external_key_type;
1074
1075 typedef external_key_type internal_key_type;
1076
1077 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1078 : Reader(Reader), KnownII(II) { }
1079
1080 static bool EqualKey(const internal_key_type& a,
1081 const internal_key_type& b) {
1082 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1083 : false;
1084 }
1085
1086 static unsigned ComputeHash(const internal_key_type& a) {
1087 return BernsteinHash(a.first, a.second);
1088 }
1089
1090 // This hopefully will just get inlined and removed by the optimizer.
1091 static const internal_key_type&
1092 GetInternalKey(const external_key_type& x) { return x; }
1093
1094 static std::pair<unsigned, unsigned>
1095 ReadKeyDataLength(const unsigned char*& d) {
1096 using namespace clang::io;
1097 unsigned KeyLen = ReadUnalignedLE16(d);
1098 unsigned DataLen = ReadUnalignedLE16(d);
1099 return std::make_pair(KeyLen, DataLen);
1100 }
1101
1102 static std::pair<const char*, unsigned>
1103 ReadKey(const unsigned char* d, unsigned n) {
1104 assert(n >= 2 && d[n-1] == '\0');
1105 return std::make_pair((const char*) d, n-1);
1106 }
1107
1108 IdentifierInfo *ReadData(const internal_key_type& k,
1109 const unsigned char* d,
1110 unsigned DataLen) {
1111 using namespace clang::io;
1112 uint32_t Bits = ReadUnalignedLE32(d); // FIXME: use these?
1113 (void)Bits;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001114 bool hasMacroDefinition = (Bits >> 3) & 0x01;
1115
Douglas Gregorc713da92009-04-21 22:25:48 +00001116 pch::IdentID ID = ReadUnalignedLE32(d);
1117 DataLen -= 8;
1118
1119 // Build the IdentifierInfo itself and link the identifier ID with
1120 // the new IdentifierInfo.
1121 IdentifierInfo *II = KnownII;
1122 if (!II)
1123 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1124 k.first, k.first + k.second);
1125 Reader.SetIdentifierInfo(ID, II);
1126
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001127 // If this identifier is a macro, deserialize the macro
1128 // definition.
1129 if (hasMacroDefinition) {
1130 uint32_t Offset = ReadUnalignedLE64(d);
1131 Reader.ReadMacroRecord(Offset);
1132 DataLen -= 8;
1133 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001134
1135 // Read all of the declarations visible at global scope with this
1136 // name.
1137 Sema *SemaObj = Reader.getSema();
1138 while (DataLen > 0) {
1139 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1140
1141 if (SemaObj) {
1142 // Introduce this declaration into the translation-unit scope
1143 // and add it to the declaration chain for this identifier, so
1144 // that (unqualified) name lookup will find it.
1145 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1146 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1147 } else {
1148 // Queue this declaration so that it will be added to the
1149 // translation unit scope and identifier's declaration chain
1150 // once a Sema object is known.
1151 // FIXME: This is a temporary hack. It will go away once we have
1152 // lazy deserialization of macros.
1153 Reader.TUDecls.push_back(D);
1154 }
1155
1156 DataLen -= 4;
1157 }
1158 return II;
1159 }
1160};
1161
1162} // end anonymous namespace
1163
1164/// \brief The on-disk hash table used to contain information about
1165/// all of the identifiers in the program.
1166typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1167 PCHIdentifierLookupTable;
1168
Douglas Gregorc34897d2009-04-09 22:27:44 +00001169// FIXME: use the diagnostics machinery
1170static bool Error(const char *Str) {
1171 std::fprintf(stderr, "%s\n", Str);
1172 return true;
1173}
1174
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001175/// \brief Check the contents of the predefines buffer against the
1176/// contents of the predefines buffer used to build the PCH file.
1177///
1178/// The contents of the two predefines buffers should be the same. If
1179/// not, then some command-line option changed the preprocessor state
1180/// and we must reject the PCH file.
1181///
1182/// \param PCHPredef The start of the predefines buffer in the PCH
1183/// file.
1184///
1185/// \param PCHPredefLen The length of the predefines buffer in the PCH
1186/// file.
1187///
1188/// \param PCHBufferID The FileID for the PCH predefines buffer.
1189///
1190/// \returns true if there was a mismatch (in which case the PCH file
1191/// should be ignored), or false otherwise.
1192bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1193 unsigned PCHPredefLen,
1194 FileID PCHBufferID) {
1195 const char *Predef = PP.getPredefines().c_str();
1196 unsigned PredefLen = PP.getPredefines().size();
1197
1198 // If the two predefines buffers compare equal, we're done!.
1199 if (PredefLen == PCHPredefLen &&
1200 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1201 return false;
1202
1203 // The predefines buffers are different. Produce a reasonable
1204 // diagnostic showing where they are different.
1205
1206 // The source locations (potentially in the two different predefines
1207 // buffers)
1208 SourceLocation Loc1, Loc2;
1209 SourceManager &SourceMgr = PP.getSourceManager();
1210
1211 // Create a source buffer for our predefines string, so
1212 // that we can build a diagnostic that points into that
1213 // source buffer.
1214 FileID BufferID;
1215 if (Predef && Predef[0]) {
1216 llvm::MemoryBuffer *Buffer
1217 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1218 "<built-in>");
1219 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1220 }
1221
1222 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1223 std::pair<const char *, const char *> Locations
1224 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1225
1226 if (Locations.first != Predef + MinLen) {
1227 // We found the location in the two buffers where there is a
1228 // difference. Form source locations to point there (in both
1229 // buffers).
1230 unsigned Offset = Locations.first - Predef;
1231 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1232 .getFileLocWithOffset(Offset);
1233 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1234 .getFileLocWithOffset(Offset);
1235 } else if (PredefLen > PCHPredefLen) {
1236 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1237 .getFileLocWithOffset(MinLen);
1238 } else {
1239 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1240 .getFileLocWithOffset(MinLen);
1241 }
1242
1243 Diag(Loc1, diag::warn_pch_preprocessor);
1244 if (Loc2.isValid())
1245 Diag(Loc2, diag::note_predef_in_pch);
1246 Diag(diag::note_ignoring_pch) << FileName;
1247 return true;
1248}
1249
Douglas Gregor635f97f2009-04-13 16:31:14 +00001250/// \brief Read the line table in the source manager block.
1251/// \returns true if ther was an error.
1252static bool ParseLineTable(SourceManager &SourceMgr,
1253 llvm::SmallVectorImpl<uint64_t> &Record) {
1254 unsigned Idx = 0;
1255 LineTableInfo &LineTable = SourceMgr.getLineTable();
1256
1257 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001258 std::map<int, int> FileIDs;
1259 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001260 // Extract the file name
1261 unsigned FilenameLen = Record[Idx++];
1262 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1263 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001264 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1265 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001266 }
1267
1268 // Parse the line entries
1269 std::vector<LineEntry> Entries;
1270 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001271 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001272
1273 // Extract the line entries
1274 unsigned NumEntries = Record[Idx++];
1275 Entries.clear();
1276 Entries.reserve(NumEntries);
1277 for (unsigned I = 0; I != NumEntries; ++I) {
1278 unsigned FileOffset = Record[Idx++];
1279 unsigned LineNo = Record[Idx++];
1280 int FilenameID = Record[Idx++];
1281 SrcMgr::CharacteristicKind FileKind
1282 = (SrcMgr::CharacteristicKind)Record[Idx++];
1283 unsigned IncludeOffset = Record[Idx++];
1284 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1285 FileKind, IncludeOffset));
1286 }
1287 LineTable.AddEntry(FID, Entries);
1288 }
1289
1290 return false;
1291}
1292
Douglas Gregorab1cef72009-04-10 03:52:48 +00001293/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001294PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001295 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001296 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1297 Error("Malformed source manager block record");
1298 return Failure;
1299 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001300
1301 SourceManager &SourceMgr = Context.getSourceManager();
1302 RecordData Record;
1303 while (true) {
1304 unsigned Code = Stream.ReadCode();
1305 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001306 if (Stream.ReadBlockEnd()) {
1307 Error("Error at end of Source Manager block");
1308 return Failure;
1309 }
1310
1311 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001312 }
1313
1314 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1315 // No known subblocks, always skip them.
1316 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001317 if (Stream.SkipBlock()) {
1318 Error("Malformed block record");
1319 return Failure;
1320 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001321 continue;
1322 }
1323
1324 if (Code == llvm::bitc::DEFINE_ABBREV) {
1325 Stream.ReadAbbrevRecord();
1326 continue;
1327 }
1328
1329 // Read a record.
1330 const char *BlobStart;
1331 unsigned BlobLen;
1332 Record.clear();
1333 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1334 default: // Default behavior: ignore.
1335 break;
1336
1337 case pch::SM_SLOC_FILE_ENTRY: {
1338 // FIXME: We would really like to delay the creation of this
1339 // FileEntry until it is actually required, e.g., when producing
1340 // a diagnostic with a source location in this file.
1341 const FileEntry *File
1342 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1343 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001344 FileID ID = SourceMgr.createFileID(File,
1345 SourceLocation::getFromRawEncoding(Record[1]),
1346 (CharacteristicKind)Record[2]);
1347 if (Record[3])
1348 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1349 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001350 break;
1351 }
1352
1353 case pch::SM_SLOC_BUFFER_ENTRY: {
1354 const char *Name = BlobStart;
1355 unsigned Code = Stream.ReadCode();
1356 Record.clear();
1357 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1358 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001359 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001360 llvm::MemoryBuffer *Buffer
1361 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1362 BlobStart + BlobLen - 1,
1363 Name);
1364 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1365
1366 if (strcmp(Name, "<built-in>") == 0
1367 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1368 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001369 break;
1370 }
1371
1372 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1373 SourceLocation SpellingLoc
1374 = SourceLocation::getFromRawEncoding(Record[1]);
1375 SourceMgr.createInstantiationLoc(
1376 SpellingLoc,
1377 SourceLocation::getFromRawEncoding(Record[2]),
1378 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001379 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001380 break;
1381 }
1382
Chris Lattnere1be6022009-04-14 23:22:57 +00001383 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001384 if (ParseLineTable(SourceMgr, Record))
1385 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001386 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001387 }
1388 }
1389}
1390
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001391void PCHReader::ReadMacroRecord(uint64_t Offset) {
1392 // Keep track of where we are in the stream, then jump back there
1393 // after reading this macro.
1394 SavedStreamPosition SavedPosition(Stream);
1395
1396 Stream.JumpToBit(Offset);
1397 RecordData Record;
1398 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1399 MacroInfo *Macro = 0;
1400 while (true) {
1401 unsigned Code = Stream.ReadCode();
1402 switch (Code) {
1403 case llvm::bitc::END_BLOCK:
1404 return;
1405
1406 case llvm::bitc::ENTER_SUBBLOCK:
1407 // No known subblocks, always skip them.
1408 Stream.ReadSubBlockID();
1409 if (Stream.SkipBlock()) {
1410 Error("Malformed block record");
1411 return;
1412 }
1413 continue;
1414
1415 case llvm::bitc::DEFINE_ABBREV:
1416 Stream.ReadAbbrevRecord();
1417 continue;
1418 default: break;
1419 }
1420
1421 // Read a record.
1422 Record.clear();
1423 pch::PreprocessorRecordTypes RecType =
1424 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1425 switch (RecType) {
1426 case pch::PP_COUNTER_VALUE:
1427 // Skip this record.
1428 break;
1429
1430 case pch::PP_MACRO_OBJECT_LIKE:
1431 case pch::PP_MACRO_FUNCTION_LIKE: {
1432 // If we already have a macro, that means that we've hit the end
1433 // of the definition of the macro we were looking for. We're
1434 // done.
1435 if (Macro)
1436 return;
1437
1438 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1439 if (II == 0) {
1440 Error("Macro must have a name");
1441 return;
1442 }
1443 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1444 bool isUsed = Record[2];
1445
1446 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1447 MI->setIsUsed(isUsed);
1448
1449 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1450 // Decode function-like macro info.
1451 bool isC99VarArgs = Record[3];
1452 bool isGNUVarArgs = Record[4];
1453 MacroArgs.clear();
1454 unsigned NumArgs = Record[5];
1455 for (unsigned i = 0; i != NumArgs; ++i)
1456 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1457
1458 // Install function-like macro info.
1459 MI->setIsFunctionLike();
1460 if (isC99VarArgs) MI->setIsC99Varargs();
1461 if (isGNUVarArgs) MI->setIsGNUVarargs();
1462 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1463 PP.getPreprocessorAllocator());
1464 }
1465
1466 // Finally, install the macro.
1467 PP.setMacroInfo(II, MI);
1468
1469 // Remember that we saw this macro last so that we add the tokens that
1470 // form its body to it.
1471 Macro = MI;
1472 ++NumMacrosRead;
1473 break;
1474 }
1475
1476 case pch::PP_TOKEN: {
1477 // If we see a TOKEN before a PP_MACRO_*, then the file is
1478 // erroneous, just pretend we didn't see this.
1479 if (Macro == 0) break;
1480
1481 Token Tok;
1482 Tok.startToken();
1483 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1484 Tok.setLength(Record[1]);
1485 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1486 Tok.setIdentifierInfo(II);
1487 Tok.setKind((tok::TokenKind)Record[3]);
1488 Tok.setFlag((Token::TokenFlags)Record[4]);
1489 Macro->AddTokenToBody(Tok);
1490 break;
1491 }
1492 }
1493 }
1494}
1495
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001496bool PCHReader::ReadPreprocessorBlock() {
1497 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1498 return Error("Malformed preprocessor block record");
1499
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001500 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001501 while (true) {
1502 unsigned Code = Stream.ReadCode();
1503 switch (Code) {
1504 case llvm::bitc::END_BLOCK:
1505 if (Stream.ReadBlockEnd())
1506 return Error("Error at end of preprocessor block");
1507 return false;
1508
1509 case llvm::bitc::ENTER_SUBBLOCK:
1510 // No known subblocks, always skip them.
1511 Stream.ReadSubBlockID();
1512 if (Stream.SkipBlock())
1513 return Error("Malformed block record");
1514 continue;
1515
1516 case llvm::bitc::DEFINE_ABBREV:
1517 Stream.ReadAbbrevRecord();
1518 continue;
1519 default: break;
1520 }
1521
1522 // Read a record.
1523 Record.clear();
1524 pch::PreprocessorRecordTypes RecType =
1525 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1526 switch (RecType) {
1527 default: // Default behavior: ignore unknown records.
1528 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001529 case pch::PP_COUNTER_VALUE:
1530 if (!Record.empty())
1531 PP.setCounterValue(Record[0]);
1532 break;
1533
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001534 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001535 case pch::PP_MACRO_FUNCTION_LIKE:
1536 case pch::PP_TOKEN:
1537 // Once we've hit a macro definition or a token, we're done.
1538 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001539 }
1540 }
1541}
1542
Douglas Gregorc713da92009-04-21 22:25:48 +00001543PCHReader::PCHReadResult
1544PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001545 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1546 Error("Malformed block record");
1547 return Failure;
1548 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001549
1550 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001551 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552 while (!Stream.AtEndOfStream()) {
1553 unsigned Code = Stream.ReadCode();
1554 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001555 if (Stream.ReadBlockEnd()) {
1556 Error("Error at end of module block");
1557 return Failure;
1558 }
Chris Lattner29241862009-04-11 21:15:38 +00001559
Douglas Gregor179cfb12009-04-10 20:39:37 +00001560 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001561 }
1562
1563 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1564 switch (Stream.ReadSubBlockID()) {
1565 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1566 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1567 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001568 if (Stream.SkipBlock()) {
1569 Error("Malformed block record");
1570 return Failure;
1571 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001572 break;
1573
Chris Lattner29241862009-04-11 21:15:38 +00001574 case pch::PREPROCESSOR_BLOCK_ID:
1575 // Skip the preprocessor block for now, but remember where it is. We
1576 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001577 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001578 Error("Multiple preprocessor blocks found.");
1579 return Failure;
1580 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001581 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001582 if (Stream.SkipBlock()) {
1583 Error("Malformed block record");
1584 return Failure;
1585 }
1586 break;
1587
Douglas Gregorab1cef72009-04-10 03:52:48 +00001588 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001589 switch (ReadSourceManagerBlock()) {
1590 case Success:
1591 break;
1592
1593 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001594 Error("Malformed source manager block");
1595 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001596
1597 case IgnorePCH:
1598 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001599 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001600 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001601 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001602 continue;
1603 }
1604
1605 if (Code == llvm::bitc::DEFINE_ABBREV) {
1606 Stream.ReadAbbrevRecord();
1607 continue;
1608 }
1609
1610 // Read and process a record.
1611 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001612 const char *BlobStart = 0;
1613 unsigned BlobLen = 0;
1614 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1615 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001616 default: // Default behavior: ignore.
1617 break;
1618
1619 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001620 if (!TypeOffsets.empty()) {
1621 Error("Duplicate TYPE_OFFSET record in PCH file");
1622 return Failure;
1623 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001624 TypeOffsets.swap(Record);
1625 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1626 break;
1627
1628 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001629 if (!DeclOffsets.empty()) {
1630 Error("Duplicate DECL_OFFSET record in PCH file");
1631 return Failure;
1632 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001633 DeclOffsets.swap(Record);
1634 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1635 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001636
1637 case pch::LANGUAGE_OPTIONS:
1638 if (ParseLanguageOptions(Record))
1639 return IgnorePCH;
1640 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001641
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001642 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001643 std::string TargetTriple(BlobStart, BlobLen);
1644 if (TargetTriple != Context.Target.getTargetTriple()) {
1645 Diag(diag::warn_pch_target_triple)
1646 << TargetTriple << Context.Target.getTargetTriple();
1647 Diag(diag::note_ignoring_pch) << FileName;
1648 return IgnorePCH;
1649 }
1650 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001651 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001652
1653 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001654 IdentifierTableData = BlobStart;
1655 IdentifierLookupTable
1656 = PCHIdentifierLookupTable::Create(
1657 (const unsigned char *)IdentifierTableData + Record[0],
1658 (const unsigned char *)IdentifierTableData,
1659 PCHIdentifierLookupTrait(*this));
1660 // FIXME: What about any identifiers already placed into the
1661 // identifier table? Should we load decls with those names now?
1662 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001663 break;
1664
1665 case pch::IDENTIFIER_OFFSET:
1666 if (!IdentifierData.empty()) {
1667 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1668 return Failure;
1669 }
1670 IdentifierData.swap(Record);
1671#ifndef NDEBUG
1672 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1673 if ((IdentifierData[I] & 0x01) == 0) {
1674 Error("Malformed identifier table in the precompiled header");
1675 return Failure;
1676 }
1677 }
1678#endif
1679 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001680
1681 case pch::EXTERNAL_DEFINITIONS:
1682 if (!ExternalDefinitions.empty()) {
1683 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1684 return Failure;
1685 }
1686 ExternalDefinitions.swap(Record);
1687 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001688
Douglas Gregore01ad442009-04-18 05:55:16 +00001689 case pch::SPECIAL_TYPES:
1690 SpecialTypes.swap(Record);
1691 break;
1692
Douglas Gregor456e0952009-04-17 22:13:46 +00001693 case pch::STATISTICS:
1694 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001695 TotalNumMacros = Record[1];
Douglas Gregor456e0952009-04-17 22:13:46 +00001696 break;
1697
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001698 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001699 }
1700
Douglas Gregor179cfb12009-04-10 20:39:37 +00001701 Error("Premature end of bitstream");
1702 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001703}
1704
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001705PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001706 // Set the PCH file name.
1707 this->FileName = FileName;
1708
Douglas Gregorc34897d2009-04-09 22:27:44 +00001709 // Open the PCH file.
1710 std::string ErrStr;
1711 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001712 if (!Buffer) {
1713 Error(ErrStr.c_str());
1714 return IgnorePCH;
1715 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001716
1717 // Initialize the stream
1718 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1719 (const unsigned char *)Buffer->getBufferEnd());
1720
1721 // Sniff for the signature.
1722 if (Stream.Read(8) != 'C' ||
1723 Stream.Read(8) != 'P' ||
1724 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001725 Stream.Read(8) != 'H') {
1726 Error("Not a PCH file");
1727 return IgnorePCH;
1728 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001729
1730 // We expect a number of well-defined blocks, though we don't necessarily
1731 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001732 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001733 while (!Stream.AtEndOfStream()) {
1734 unsigned Code = Stream.ReadCode();
1735
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001736 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1737 Error("Invalid record at top-level");
1738 return Failure;
1739 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001740
1741 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001742
Douglas Gregorc34897d2009-04-09 22:27:44 +00001743 // We only know the PCH subblock ID.
1744 switch (BlockID) {
1745 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001746 if (Stream.ReadBlockInfoBlock()) {
1747 Error("Malformed BlockInfoBlock");
1748 return Failure;
1749 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001750 break;
1751 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001752 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001753 case Success:
1754 break;
1755
1756 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001757 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001758
1759 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001760 // FIXME: We could consider reading through to the end of this
1761 // PCH block, skipping subblocks, to see if there are other
1762 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001763 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001764 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001765 break;
1766 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001767 if (Stream.SkipBlock()) {
1768 Error("Malformed block record");
1769 return Failure;
1770 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001771 break;
1772 }
1773 }
1774
1775 // Load the translation unit declaration
1776 ReadDeclRecord(DeclOffsets[0], 0);
1777
Douglas Gregorc713da92009-04-21 22:25:48 +00001778 // Initialization of builtins and library builtins occurs before the
1779 // PCH file is read, so there may be some identifiers that were
1780 // loaded into the IdentifierTable before we intercepted the
1781 // creation of identifiers. Iterate through the list of known
1782 // identifiers and determine whether we have to establish
1783 // preprocessor definitions or top-level identifier declaration
1784 // chains for those identifiers.
1785 //
1786 // We copy the IdentifierInfo pointers to a small vector first,
1787 // since de-serializing declarations or macro definitions can add
1788 // new entries into the identifier table, invalidating the
1789 // iterators.
1790 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1791 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1792 IdEnd = PP.getIdentifierTable().end();
1793 Id != IdEnd; ++Id)
1794 Identifiers.push_back(Id->second);
1795 PCHIdentifierLookupTable *IdTable
1796 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1797 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1798 IdentifierInfo *II = Identifiers[I];
1799 // Look in the on-disk hash table for an entry for
1800 PCHIdentifierLookupTrait Info(*this, II);
1801 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1802 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1803 if (Pos == IdTable->end())
1804 continue;
1805
Douglas Gregor37f477e2009-04-22 04:56:28 +00001806 fprintf(stderr, "Looked up pre-allocated IdentifierInfo \"%s\"\n",
1807 II->getName());
1808
Douglas Gregorc713da92009-04-21 22:25:48 +00001809 // Dereferencing the iterator has the effect of populating the
1810 // IdentifierInfo node with the various declarations it needs.
1811 (void)*Pos;
1812 }
1813
Douglas Gregore01ad442009-04-18 05:55:16 +00001814 // Load the special types.
1815 Context.setBuiltinVaListType(
1816 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1817
Douglas Gregorc713da92009-04-21 22:25:48 +00001818 // If we saw the preprocessor block, read it now.
1819 if (PreprocessorBlockOffset) {
1820 SavedStreamPosition SavedPos(Stream);
1821 Stream.JumpToBit(PreprocessorBlockOffset);
1822 if (ReadPreprocessorBlock()) {
1823 Error("Malformed preprocessor block");
1824 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001825 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001826 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001827
Douglas Gregorc713da92009-04-21 22:25:48 +00001828 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001829}
1830
Douglas Gregor179cfb12009-04-10 20:39:37 +00001831/// \brief Parse the record that corresponds to a LangOptions data
1832/// structure.
1833///
1834/// This routine compares the language options used to generate the
1835/// PCH file against the language options set for the current
1836/// compilation. For each option, we classify differences between the
1837/// two compiler states as either "benign" or "important". Benign
1838/// differences don't matter, and we accept them without complaint
1839/// (and without modifying the language options). Differences between
1840/// the states for important options cause the PCH file to be
1841/// unusable, so we emit a warning and return true to indicate that
1842/// there was an error.
1843///
1844/// \returns true if the PCH file is unacceptable, false otherwise.
1845bool PCHReader::ParseLanguageOptions(
1846 const llvm::SmallVectorImpl<uint64_t> &Record) {
1847 const LangOptions &LangOpts = Context.getLangOptions();
1848#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1849#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1850 if (Record[Idx] != LangOpts.Option) { \
1851 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1852 Diag(diag::note_ignoring_pch) << FileName; \
1853 return true; \
1854 } \
1855 ++Idx
1856
1857 unsigned Idx = 0;
1858 PARSE_LANGOPT_BENIGN(Trigraphs);
1859 PARSE_LANGOPT_BENIGN(BCPLComment);
1860 PARSE_LANGOPT_BENIGN(DollarIdents);
1861 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1862 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1863 PARSE_LANGOPT_BENIGN(ImplicitInt);
1864 PARSE_LANGOPT_BENIGN(Digraphs);
1865 PARSE_LANGOPT_BENIGN(HexFloats);
1866 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1867 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1868 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1869 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1870 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1871 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1872 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1873 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1874 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1875 PARSE_LANGOPT_BENIGN(PascalStrings);
1876 PARSE_LANGOPT_BENIGN(Boolean);
1877 PARSE_LANGOPT_BENIGN(WritableStrings);
1878 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1879 diag::warn_pch_lax_vector_conversions);
1880 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1881 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1882 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1883 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1884 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1885 diag::warn_pch_thread_safe_statics);
1886 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1887 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1888 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1889 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1890 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1891 diag::warn_pch_heinous_extensions);
1892 // FIXME: Most of the options below are benign if the macro wasn't
1893 // used. Unfortunately, this means that a PCH compiled without
1894 // optimization can't be used with optimization turned on, even
1895 // though the only thing that changes is whether __OPTIMIZE__ was
1896 // defined... but if __OPTIMIZE__ never showed up in the header, it
1897 // doesn't matter. We could consider making this some special kind
1898 // of check.
1899 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1900 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1901 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1902 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1903 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1904 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1905 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1906 Diag(diag::warn_pch_gc_mode)
1907 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1908 Diag(diag::note_ignoring_pch) << FileName;
1909 return true;
1910 }
1911 ++Idx;
1912 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1913 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1914#undef PARSE_LANGOPT_IRRELEVANT
1915#undef PARSE_LANGOPT_BENIGN
1916
1917 return false;
1918}
1919
Douglas Gregorc34897d2009-04-09 22:27:44 +00001920/// \brief Read and return the type at the given offset.
1921///
1922/// This routine actually reads the record corresponding to the type
1923/// at the given offset in the bitstream. It is a helper routine for
1924/// GetType, which deals with reading type IDs.
1925QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001926 // Keep track of where we are in the stream, then jump back there
1927 // after reading this type.
1928 SavedStreamPosition SavedPosition(Stream);
1929
Douglas Gregorc34897d2009-04-09 22:27:44 +00001930 Stream.JumpToBit(Offset);
1931 RecordData Record;
1932 unsigned Code = Stream.ReadCode();
1933 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001934 case pch::TYPE_EXT_QUAL: {
1935 assert(Record.size() == 3 &&
1936 "Incorrect encoding of extended qualifier type");
1937 QualType Base = GetType(Record[0]);
1938 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1939 unsigned AddressSpace = Record[2];
1940
1941 QualType T = Base;
1942 if (GCAttr != QualType::GCNone)
1943 T = Context.getObjCGCQualType(T, GCAttr);
1944 if (AddressSpace)
1945 T = Context.getAddrSpaceQualType(T, AddressSpace);
1946 return T;
1947 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001948
Douglas Gregorc34897d2009-04-09 22:27:44 +00001949 case pch::TYPE_FIXED_WIDTH_INT: {
1950 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1951 return Context.getFixedWidthIntType(Record[0], Record[1]);
1952 }
1953
1954 case pch::TYPE_COMPLEX: {
1955 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1956 QualType ElemType = GetType(Record[0]);
1957 return Context.getComplexType(ElemType);
1958 }
1959
1960 case pch::TYPE_POINTER: {
1961 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1962 QualType PointeeType = GetType(Record[0]);
1963 return Context.getPointerType(PointeeType);
1964 }
1965
1966 case pch::TYPE_BLOCK_POINTER: {
1967 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1968 QualType PointeeType = GetType(Record[0]);
1969 return Context.getBlockPointerType(PointeeType);
1970 }
1971
1972 case pch::TYPE_LVALUE_REFERENCE: {
1973 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1974 QualType PointeeType = GetType(Record[0]);
1975 return Context.getLValueReferenceType(PointeeType);
1976 }
1977
1978 case pch::TYPE_RVALUE_REFERENCE: {
1979 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1980 QualType PointeeType = GetType(Record[0]);
1981 return Context.getRValueReferenceType(PointeeType);
1982 }
1983
1984 case pch::TYPE_MEMBER_POINTER: {
1985 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1986 QualType PointeeType = GetType(Record[0]);
1987 QualType ClassType = GetType(Record[1]);
1988 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1989 }
1990
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001991 case pch::TYPE_CONSTANT_ARRAY: {
1992 QualType ElementType = GetType(Record[0]);
1993 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1994 unsigned IndexTypeQuals = Record[2];
1995 unsigned Idx = 3;
1996 llvm::APInt Size = ReadAPInt(Record, Idx);
1997 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1998 }
1999
2000 case pch::TYPE_INCOMPLETE_ARRAY: {
2001 QualType ElementType = GetType(Record[0]);
2002 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2003 unsigned IndexTypeQuals = Record[2];
2004 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2005 }
2006
2007 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002008 QualType ElementType = GetType(Record[0]);
2009 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2010 unsigned IndexTypeQuals = Record[2];
2011 return Context.getVariableArrayType(ElementType, ReadExpr(),
2012 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002013 }
2014
2015 case pch::TYPE_VECTOR: {
2016 if (Record.size() != 2) {
2017 Error("Incorrect encoding of vector type in PCH file");
2018 return QualType();
2019 }
2020
2021 QualType ElementType = GetType(Record[0]);
2022 unsigned NumElements = Record[1];
2023 return Context.getVectorType(ElementType, NumElements);
2024 }
2025
2026 case pch::TYPE_EXT_VECTOR: {
2027 if (Record.size() != 2) {
2028 Error("Incorrect encoding of extended vector type in PCH file");
2029 return QualType();
2030 }
2031
2032 QualType ElementType = GetType(Record[0]);
2033 unsigned NumElements = Record[1];
2034 return Context.getExtVectorType(ElementType, NumElements);
2035 }
2036
2037 case pch::TYPE_FUNCTION_NO_PROTO: {
2038 if (Record.size() != 1) {
2039 Error("Incorrect encoding of no-proto function type");
2040 return QualType();
2041 }
2042 QualType ResultType = GetType(Record[0]);
2043 return Context.getFunctionNoProtoType(ResultType);
2044 }
2045
2046 case pch::TYPE_FUNCTION_PROTO: {
2047 QualType ResultType = GetType(Record[0]);
2048 unsigned Idx = 1;
2049 unsigned NumParams = Record[Idx++];
2050 llvm::SmallVector<QualType, 16> ParamTypes;
2051 for (unsigned I = 0; I != NumParams; ++I)
2052 ParamTypes.push_back(GetType(Record[Idx++]));
2053 bool isVariadic = Record[Idx++];
2054 unsigned Quals = Record[Idx++];
2055 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2056 isVariadic, Quals);
2057 }
2058
2059 case pch::TYPE_TYPEDEF:
2060 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2061 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2062
2063 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002064 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002065
2066 case pch::TYPE_TYPEOF: {
2067 if (Record.size() != 1) {
2068 Error("Incorrect encoding of typeof(type) in PCH file");
2069 return QualType();
2070 }
2071 QualType UnderlyingType = GetType(Record[0]);
2072 return Context.getTypeOfType(UnderlyingType);
2073 }
2074
2075 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002076 assert(Record.size() == 1 && "Incorrect encoding of record type");
2077 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002078
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002079 case pch::TYPE_ENUM:
2080 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2081 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2082
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002083 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002084 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2085 return Context.getObjCInterfaceType(
2086 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002087
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002088 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2089 unsigned Idx = 0;
2090 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2091 unsigned NumProtos = Record[Idx++];
2092 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2093 for (unsigned I = 0; I != NumProtos; ++I)
2094 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2095 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2096 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002097
Chris Lattner9b9f2352009-04-22 06:40:03 +00002098 case pch::TYPE_OBJC_QUALIFIED_ID: {
2099 unsigned Idx = 0;
2100 unsigned NumProtos = Record[Idx++];
2101 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2102 for (unsigned I = 0; I != NumProtos; ++I)
2103 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2104 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2105 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002106
2107 case pch::TYPE_OBJC_QUALIFIED_CLASS:
2108 // FIXME: Deserialize ObjCQualifiedClassType
2109 assert(false && "Cannot de-serialize ObjC qualified class types yet");
2110 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002111 }
2112
2113 // Suppress a GCC warning
2114 return QualType();
2115}
2116
2117/// \brief Note that we have loaded the declaration with the given
2118/// Index.
2119///
2120/// This routine notes that this declaration has already been loaded,
2121/// so that future GetDecl calls will return this declaration rather
2122/// than trying to load a new declaration.
2123inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2124 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2125 DeclAlreadyLoaded[Index] = true;
2126 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2127}
2128
2129/// \brief Read the declaration at the given offset from the PCH file.
2130Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002131 // Keep track of where we are in the stream, then jump back there
2132 // after reading this declaration.
2133 SavedStreamPosition SavedPosition(Stream);
2134
Douglas Gregorc34897d2009-04-09 22:27:44 +00002135 Decl *D = 0;
2136 Stream.JumpToBit(Offset);
2137 RecordData Record;
2138 unsigned Code = Stream.ReadCode();
2139 unsigned Idx = 0;
2140 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002141
Douglas Gregorc34897d2009-04-09 22:27:44 +00002142 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002143 case pch::DECL_ATTR:
2144 case pch::DECL_CONTEXT_LEXICAL:
2145 case pch::DECL_CONTEXT_VISIBLE:
2146 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2147 break;
2148
Douglas Gregorc34897d2009-04-09 22:27:44 +00002149 case pch::DECL_TRANSLATION_UNIT:
2150 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002151 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002152 break;
2153
2154 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002155 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002156 break;
2157 }
2158
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002159 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002160 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002161 break;
2162 }
2163
Douglas Gregor982365e2009-04-13 21:20:57 +00002164 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002165 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2166 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002167 break;
2168 }
2169
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002170 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002171 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2172 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002173 break;
2174 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002175
2176 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002177 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2178 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002179 break;
2180 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002181
Steve Naroff79ea0e02009-04-20 15:06:07 +00002182 case pch::DECL_OBJC_METHOD: {
2183 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2184 Selector(), QualType(), 0);
2185 break;
2186 }
2187
Steve Naroff97b53bd2009-04-21 15:12:33 +00002188 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002189 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2190 break;
2191 }
2192
Steve Naroff97b53bd2009-04-21 15:12:33 +00002193 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002194 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2195 ObjCIvarDecl::None);
2196 break;
2197 }
2198
Steve Naroff97b53bd2009-04-21 15:12:33 +00002199 case pch::DECL_OBJC_PROTOCOL: {
2200 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2201 break;
2202 }
2203
2204 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2205 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2206 QualType(), 0);
2207 break;
2208 }
2209
2210 case pch::DECL_OBJC_CLASS: {
2211 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2212 break;
2213 }
2214
2215 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2216 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2217 break;
2218 }
2219
2220 case pch::DECL_OBJC_CATEGORY: {
2221 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2222 break;
2223 }
2224
2225 case pch::DECL_OBJC_CATEGORY_IMPL: {
2226 // FIXME: Implement.
2227 break;
2228 }
2229
2230 case pch::DECL_OBJC_IMPLEMENTATION: {
2231 // FIXME: Implement.
2232 break;
2233 }
2234
2235 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2236 // FIXME: Implement.
2237 break;
2238 }
2239
2240 case pch::DECL_OBJC_PROPERTY: {
2241 // FIXME: Implement.
2242 break;
2243 }
2244
2245 case pch::DECL_OBJC_PROPERTY_IMPL: {
2246 // FIXME: Implement.
2247 break;
2248 }
2249
Douglas Gregor982365e2009-04-13 21:20:57 +00002250 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002251 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2252 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002253 break;
2254 }
2255
Douglas Gregorc34897d2009-04-09 22:27:44 +00002256 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002257 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2258 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002259 break;
2260 }
2261
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002262 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002263 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2264 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002265 break;
2266 }
2267
2268 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002269 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002270 QualType(), QualType(), VarDecl::None,
2271 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002272 break;
2273 }
2274
Douglas Gregor2a491792009-04-13 22:49:25 +00002275 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002276 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002277 break;
2278 }
2279
2280 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002281 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002282 break;
2283 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002284 }
2285
Douglas Gregorc713da92009-04-21 22:25:48 +00002286 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002287 if (D) {
2288 LoadedDecl(Index, D);
2289 Reader.Visit(D);
2290 }
2291
Douglas Gregorc34897d2009-04-09 22:27:44 +00002292 // If this declaration is also a declaration context, get the
2293 // offsets for its tables of lexical and visible declarations.
2294 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2295 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2296 if (Offsets.first || Offsets.second) {
2297 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2298 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2299 DeclContextOffsets[DC] = Offsets;
2300 }
2301 }
2302 assert(Idx == Record.size());
2303
2304 return D;
2305}
2306
Douglas Gregorac8f2802009-04-10 17:25:41 +00002307QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002308 unsigned Quals = ID & 0x07;
2309 unsigned Index = ID >> 3;
2310
2311 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2312 QualType T;
2313 switch ((pch::PredefinedTypeIDs)Index) {
2314 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2315 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2316 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2317
2318 case pch::PREDEF_TYPE_CHAR_U_ID:
2319 case pch::PREDEF_TYPE_CHAR_S_ID:
2320 // FIXME: Check that the signedness of CharTy is correct!
2321 T = Context.CharTy;
2322 break;
2323
2324 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2325 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2326 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2327 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2328 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2329 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2330 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2331 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2332 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2333 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2334 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2335 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2336 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2337 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2338 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2339 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2340 }
2341
2342 assert(!T.isNull() && "Unknown predefined type");
2343 return T.getQualifiedType(Quals);
2344 }
2345
2346 Index -= pch::NUM_PREDEF_TYPE_IDS;
2347 if (!TypeAlreadyLoaded[Index]) {
2348 // Load the type from the PCH file.
2349 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2350 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2351 TypeAlreadyLoaded[Index] = true;
2352 }
2353
2354 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2355}
2356
Douglas Gregorac8f2802009-04-10 17:25:41 +00002357Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002358 if (ID == 0)
2359 return 0;
2360
2361 unsigned Index = ID - 1;
2362 if (DeclAlreadyLoaded[Index])
2363 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2364
2365 // Load the declaration from the PCH file.
2366 return ReadDeclRecord(DeclOffsets[Index], Index);
2367}
2368
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002369Stmt *PCHReader::GetStmt(uint64_t Offset) {
2370 // Keep track of where we are in the stream, then jump back there
2371 // after reading this declaration.
2372 SavedStreamPosition SavedPosition(Stream);
2373
2374 Stream.JumpToBit(Offset);
2375 return ReadStmt();
2376}
2377
Douglas Gregorc34897d2009-04-09 22:27:44 +00002378bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002379 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002380 assert(DC->hasExternalLexicalStorage() &&
2381 "DeclContext has no lexical decls in storage");
2382 uint64_t Offset = DeclContextOffsets[DC].first;
2383 assert(Offset && "DeclContext has no lexical decls in storage");
2384
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002385 // Keep track of where we are in the stream, then jump back there
2386 // after reading this context.
2387 SavedStreamPosition SavedPosition(Stream);
2388
Douglas Gregorc34897d2009-04-09 22:27:44 +00002389 // Load the record containing all of the declarations lexically in
2390 // this context.
2391 Stream.JumpToBit(Offset);
2392 RecordData Record;
2393 unsigned Code = Stream.ReadCode();
2394 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002395 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002396 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2397
2398 // Load all of the declaration IDs
2399 Decls.clear();
2400 Decls.insert(Decls.end(), Record.begin(), Record.end());
2401 return false;
2402}
2403
2404bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2405 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2406 assert(DC->hasExternalVisibleStorage() &&
2407 "DeclContext has no visible decls in storage");
2408 uint64_t Offset = DeclContextOffsets[DC].second;
2409 assert(Offset && "DeclContext has no visible decls in storage");
2410
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002411 // Keep track of where we are in the stream, then jump back there
2412 // after reading this context.
2413 SavedStreamPosition SavedPosition(Stream);
2414
Douglas Gregorc34897d2009-04-09 22:27:44 +00002415 // Load the record containing all of the declarations visible in
2416 // this context.
2417 Stream.JumpToBit(Offset);
2418 RecordData Record;
2419 unsigned Code = Stream.ReadCode();
2420 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002421 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002422 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2423 if (Record.size() == 0)
2424 return false;
2425
2426 Decls.clear();
2427
2428 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002429 while (Idx < Record.size()) {
2430 Decls.push_back(VisibleDeclaration());
2431 Decls.back().Name = ReadDeclarationName(Record, Idx);
2432
Douglas Gregorc34897d2009-04-09 22:27:44 +00002433 unsigned Size = Record[Idx++];
2434 llvm::SmallVector<unsigned, 4> & LoadedDecls
2435 = Decls.back().Declarations;
2436 LoadedDecls.reserve(Size);
2437 for (unsigned I = 0; I < Size; ++I)
2438 LoadedDecls.push_back(Record[Idx++]);
2439 }
2440
2441 return false;
2442}
2443
Douglas Gregor631f6c62009-04-14 00:24:19 +00002444void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2445 if (!Consumer)
2446 return;
2447
2448 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2449 Decl *D = GetDecl(ExternalDefinitions[I]);
2450 DeclGroupRef DG(D);
2451 Consumer->HandleTopLevelDecl(DG);
2452 }
2453}
2454
Douglas Gregorc34897d2009-04-09 22:27:44 +00002455void PCHReader::PrintStats() {
2456 std::fprintf(stderr, "*** PCH Statistics:\n");
2457
2458 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2459 TypeAlreadyLoaded.end(),
2460 true);
2461 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2462 DeclAlreadyLoaded.end(),
2463 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002464 unsigned NumIdentifiersLoaded = 0;
2465 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2466 if ((IdentifierData[I] & 0x01) == 0)
2467 ++NumIdentifiersLoaded;
2468 }
2469
Douglas Gregorc34897d2009-04-09 22:27:44 +00002470 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2471 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002472 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002473 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2474 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002475 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2476 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2477 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2478 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002479 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2480 NumStatementsRead, TotalNumStatements,
2481 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002482 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2483 NumMacrosRead, TotalNumMacros,
2484 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002485 std::fprintf(stderr, "\n");
2486}
2487
Douglas Gregorc713da92009-04-21 22:25:48 +00002488void PCHReader::InitializeSema(Sema &S) {
2489 SemaObj = &S;
2490
2491 // FIXME: this makes sure any declarations that were deserialized
2492 // "too early" still get added to the identifier's declaration
2493 // chains.
2494 for (unsigned I = 0, N = TUDecls.size(); I != N; ++I) {
2495 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(TUDecls[I]));
2496 SemaObj->IdResolver.AddDecl(TUDecls[I]);
2497 }
2498 TUDecls.clear();
2499}
2500
2501IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2502 // Try to find this name within our on-disk hash table
2503 PCHIdentifierLookupTable *IdTable
2504 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2505 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2506 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2507 if (Pos == IdTable->end())
2508 return 0;
2509
2510 // Dereferencing the iterator has the effect of building the
2511 // IdentifierInfo node and populating it with the various
2512 // declarations it needs.
2513 return *Pos;
2514}
2515
2516void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2517 assert(ID && "Non-zero identifier ID required");
2518 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2519}
2520
Chris Lattner29241862009-04-11 21:15:38 +00002521IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002522 if (ID == 0)
2523 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002524
Douglas Gregorc713da92009-04-21 22:25:48 +00002525 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002526 Error("No identifier table in PCH file");
2527 return 0;
2528 }
Chris Lattner29241862009-04-11 21:15:38 +00002529
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002530 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002531 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002532 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002533 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002534 }
Chris Lattner29241862009-04-11 21:15:38 +00002535
2536 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002537}
2538
2539DeclarationName
2540PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2541 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2542 switch (Kind) {
2543 case DeclarationName::Identifier:
2544 return DeclarationName(GetIdentifierInfo(Record, Idx));
2545
2546 case DeclarationName::ObjCZeroArgSelector:
2547 case DeclarationName::ObjCOneArgSelector:
2548 case DeclarationName::ObjCMultiArgSelector:
2549 assert(false && "Unable to de-serialize Objective-C selectors");
2550 break;
2551
2552 case DeclarationName::CXXConstructorName:
2553 return Context.DeclarationNames.getCXXConstructorName(
2554 GetType(Record[Idx++]));
2555
2556 case DeclarationName::CXXDestructorName:
2557 return Context.DeclarationNames.getCXXDestructorName(
2558 GetType(Record[Idx++]));
2559
2560 case DeclarationName::CXXConversionFunctionName:
2561 return Context.DeclarationNames.getCXXConversionFunctionName(
2562 GetType(Record[Idx++]));
2563
2564 case DeclarationName::CXXOperatorName:
2565 return Context.DeclarationNames.getCXXOperatorName(
2566 (OverloadedOperatorKind)Record[Idx++]);
2567
2568 case DeclarationName::CXXUsingDirective:
2569 return DeclarationName::getUsingDirectiveName();
2570 }
2571
2572 // Required to silence GCC warning
2573 return DeclarationName();
2574}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002575
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002576/// \brief Read an integral value
2577llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2578 unsigned BitWidth = Record[Idx++];
2579 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2580 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2581 Idx += NumWords;
2582 return Result;
2583}
2584
2585/// \brief Read a signed integral value
2586llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2587 bool isUnsigned = Record[Idx++];
2588 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2589}
2590
Douglas Gregore2f37202009-04-14 21:55:33 +00002591/// \brief Read a floating-point value
2592llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002593 return llvm::APFloat(ReadAPInt(Record, Idx));
2594}
2595
Douglas Gregor1c507882009-04-15 21:30:51 +00002596// \brief Read a string
2597std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2598 unsigned Len = Record[Idx++];
2599 std::string Result(&Record[Idx], &Record[Idx] + Len);
2600 Idx += Len;
2601 return Result;
2602}
2603
2604/// \brief Reads attributes from the current stream position.
2605Attr *PCHReader::ReadAttributes() {
2606 unsigned Code = Stream.ReadCode();
2607 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2608 "Expected unabbreviated record"); (void)Code;
2609
2610 RecordData Record;
2611 unsigned Idx = 0;
2612 unsigned RecCode = Stream.ReadRecord(Code, Record);
2613 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2614 (void)RecCode;
2615
2616#define SIMPLE_ATTR(Name) \
2617 case Attr::Name: \
2618 New = ::new (Context) Name##Attr(); \
2619 break
2620
2621#define STRING_ATTR(Name) \
2622 case Attr::Name: \
2623 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2624 break
2625
2626#define UNSIGNED_ATTR(Name) \
2627 case Attr::Name: \
2628 New = ::new (Context) Name##Attr(Record[Idx++]); \
2629 break
2630
2631 Attr *Attrs = 0;
2632 while (Idx < Record.size()) {
2633 Attr *New = 0;
2634 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2635 bool IsInherited = Record[Idx++];
2636
2637 switch (Kind) {
2638 STRING_ATTR(Alias);
2639 UNSIGNED_ATTR(Aligned);
2640 SIMPLE_ATTR(AlwaysInline);
2641 SIMPLE_ATTR(AnalyzerNoReturn);
2642 STRING_ATTR(Annotate);
2643 STRING_ATTR(AsmLabel);
2644
2645 case Attr::Blocks:
2646 New = ::new (Context) BlocksAttr(
2647 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2648 break;
2649
2650 case Attr::Cleanup:
2651 New = ::new (Context) CleanupAttr(
2652 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2653 break;
2654
2655 SIMPLE_ATTR(Const);
2656 UNSIGNED_ATTR(Constructor);
2657 SIMPLE_ATTR(DLLExport);
2658 SIMPLE_ATTR(DLLImport);
2659 SIMPLE_ATTR(Deprecated);
2660 UNSIGNED_ATTR(Destructor);
2661 SIMPLE_ATTR(FastCall);
2662
2663 case Attr::Format: {
2664 std::string Type = ReadString(Record, Idx);
2665 unsigned FormatIdx = Record[Idx++];
2666 unsigned FirstArg = Record[Idx++];
2667 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2668 break;
2669 }
2670
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002671 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002672
2673 case Attr::IBOutletKind:
2674 New = ::new (Context) IBOutletAttr();
2675 break;
2676
2677 SIMPLE_ATTR(NoReturn);
2678 SIMPLE_ATTR(NoThrow);
2679 SIMPLE_ATTR(Nodebug);
2680 SIMPLE_ATTR(Noinline);
2681
2682 case Attr::NonNull: {
2683 unsigned Size = Record[Idx++];
2684 llvm::SmallVector<unsigned, 16> ArgNums;
2685 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2686 Idx += Size;
2687 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2688 break;
2689 }
2690
2691 SIMPLE_ATTR(ObjCException);
2692 SIMPLE_ATTR(ObjCNSObject);
2693 SIMPLE_ATTR(Overloadable);
2694 UNSIGNED_ATTR(Packed);
2695 SIMPLE_ATTR(Pure);
2696 UNSIGNED_ATTR(Regparm);
2697 STRING_ATTR(Section);
2698 SIMPLE_ATTR(StdCall);
2699 SIMPLE_ATTR(TransparentUnion);
2700 SIMPLE_ATTR(Unavailable);
2701 SIMPLE_ATTR(Unused);
2702 SIMPLE_ATTR(Used);
2703
2704 case Attr::Visibility:
2705 New = ::new (Context) VisibilityAttr(
2706 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2707 break;
2708
2709 SIMPLE_ATTR(WarnUnusedResult);
2710 SIMPLE_ATTR(Weak);
2711 SIMPLE_ATTR(WeakImport);
2712 }
2713
2714 assert(New && "Unable to decode attribute?");
2715 New->setInherited(IsInherited);
2716 New->setNext(Attrs);
2717 Attrs = New;
2718 }
2719#undef UNSIGNED_ATTR
2720#undef STRING_ATTR
2721#undef SIMPLE_ATTR
2722
2723 // The list of attributes was built backwards. Reverse the list
2724 // before returning it.
2725 Attr *PrevAttr = 0, *NextAttr = 0;
2726 while (Attrs) {
2727 NextAttr = Attrs->getNext();
2728 Attrs->setNext(PrevAttr);
2729 PrevAttr = Attrs;
2730 Attrs = NextAttr;
2731 }
2732
2733 return PrevAttr;
2734}
2735
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002736Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002737 // Within the bitstream, expressions are stored in Reverse Polish
2738 // Notation, with each of the subexpressions preceding the
2739 // expression they are stored in. To evaluate expressions, we
2740 // continue reading expressions and placing them on the stack, with
2741 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002742 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002743 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002744 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002745 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002746 llvm::SmallVector<Stmt *, 16> StmtStack;
2747 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002748 Stmt::EmptyShell Empty;
2749
Douglas Gregora151ba42009-04-14 23:32:43 +00002750 while (true) {
2751 unsigned Code = Stream.ReadCode();
2752 if (Code == llvm::bitc::END_BLOCK) {
2753 if (Stream.ReadBlockEnd()) {
2754 Error("Error at end of Source Manager block");
2755 return 0;
2756 }
2757 break;
2758 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002759
Douglas Gregora151ba42009-04-14 23:32:43 +00002760 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2761 // No known subblocks, always skip them.
2762 Stream.ReadSubBlockID();
2763 if (Stream.SkipBlock()) {
2764 Error("Malformed block record");
2765 return 0;
2766 }
2767 continue;
2768 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002769
Douglas Gregora151ba42009-04-14 23:32:43 +00002770 if (Code == llvm::bitc::DEFINE_ABBREV) {
2771 Stream.ReadAbbrevRecord();
2772 continue;
2773 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002774
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002775 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002776 Idx = 0;
2777 Record.clear();
2778 bool Finished = false;
2779 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002780 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002781 Finished = true;
2782 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002783
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002784 case pch::STMT_NULL_PTR:
2785 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002786 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002787
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002788 case pch::STMT_NULL:
2789 S = new (Context) NullStmt(Empty);
2790 break;
2791
2792 case pch::STMT_COMPOUND:
2793 S = new (Context) CompoundStmt(Empty);
2794 break;
2795
2796 case pch::STMT_CASE:
2797 S = new (Context) CaseStmt(Empty);
2798 break;
2799
2800 case pch::STMT_DEFAULT:
2801 S = new (Context) DefaultStmt(Empty);
2802 break;
2803
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002804 case pch::STMT_LABEL:
2805 S = new (Context) LabelStmt(Empty);
2806 break;
2807
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002808 case pch::STMT_IF:
2809 S = new (Context) IfStmt(Empty);
2810 break;
2811
2812 case pch::STMT_SWITCH:
2813 S = new (Context) SwitchStmt(Empty);
2814 break;
2815
Douglas Gregora6b503f2009-04-17 00:16:09 +00002816 case pch::STMT_WHILE:
2817 S = new (Context) WhileStmt(Empty);
2818 break;
2819
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002820 case pch::STMT_DO:
2821 S = new (Context) DoStmt(Empty);
2822 break;
2823
2824 case pch::STMT_FOR:
2825 S = new (Context) ForStmt(Empty);
2826 break;
2827
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002828 case pch::STMT_GOTO:
2829 S = new (Context) GotoStmt(Empty);
2830 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002831
2832 case pch::STMT_INDIRECT_GOTO:
2833 S = new (Context) IndirectGotoStmt(Empty);
2834 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002835
Douglas Gregora6b503f2009-04-17 00:16:09 +00002836 case pch::STMT_CONTINUE:
2837 S = new (Context) ContinueStmt(Empty);
2838 break;
2839
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002840 case pch::STMT_BREAK:
2841 S = new (Context) BreakStmt(Empty);
2842 break;
2843
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002844 case pch::STMT_RETURN:
2845 S = new (Context) ReturnStmt(Empty);
2846 break;
2847
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002848 case pch::STMT_DECL:
2849 S = new (Context) DeclStmt(Empty);
2850 break;
2851
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002852 case pch::STMT_ASM:
2853 S = new (Context) AsmStmt(Empty);
2854 break;
2855
Douglas Gregora151ba42009-04-14 23:32:43 +00002856 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002857 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002858 break;
2859
2860 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002861 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002862 break;
2863
2864 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002865 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002866 break;
2867
2868 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002869 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002870 break;
2871
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002872 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002873 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002874 break;
2875
Douglas Gregor596e0932009-04-15 16:35:07 +00002876 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002877 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002878 Record[PCHStmtReader::NumExprFields + 1]);
2879 break;
2880
Douglas Gregora151ba42009-04-14 23:32:43 +00002881 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002882 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002883 break;
2884
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002885 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002886 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002887 break;
2888
Douglas Gregor12d74052009-04-15 15:58:59 +00002889 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002890 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002891 break;
2892
2893 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002894 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002895 break;
2896
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002897 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002898 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002899 break;
2900
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002901 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002902 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002903 break;
2904
2905 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002906 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002907 break;
2908
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002909 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002910 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002911 break;
2912
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002913 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002914 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002915 break;
2916
2917 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002918 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002919 break;
2920
Douglas Gregora151ba42009-04-14 23:32:43 +00002921 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002922 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002923 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002924
2925 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002926 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002927 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002928
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002929 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002930 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002931 break;
2932
Douglas Gregorec0b8292009-04-15 23:02:49 +00002933 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002934 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002935 break;
2936
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002937 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002938 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002939 break;
2940
2941 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002942 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002943 Record[PCHStmtReader::NumExprFields] - 1);
2944
2945 break;
2946
2947 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002948 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002949 break;
2950
Douglas Gregorec0b8292009-04-15 23:02:49 +00002951 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002952 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002953 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002954
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002955 case pch::EXPR_ADDR_LABEL:
2956 S = new (Context) AddrLabelExpr(Empty);
2957 break;
2958
Douglas Gregoreca12f62009-04-17 19:05:30 +00002959 case pch::EXPR_STMT:
2960 S = new (Context) StmtExpr(Empty);
2961 break;
2962
Douglas Gregor209d4622009-04-15 23:33:31 +00002963 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002964 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002965 break;
2966
2967 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002968 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002969 break;
2970
2971 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002972 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002973 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002974
2975 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002976 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002977 break;
2978
Douglas Gregore246b742009-04-17 19:21:43 +00002979 case pch::EXPR_BLOCK:
2980 S = new (Context) BlockExpr(Empty);
2981 break;
2982
Douglas Gregor725e94b2009-04-16 00:01:45 +00002983 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002984 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002985 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00002986
Chris Lattnerc49bbe72009-04-22 06:29:42 +00002987 case pch::EXPR_OBJC_STRING_LITERAL:
2988 S = new (Context) ObjCStringLiteral(Empty);
2989 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00002990 case pch::EXPR_OBJC_ENCODE:
2991 S = new (Context) ObjCEncodeExpr(Empty);
2992 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00002993 case pch::EXPR_OBJC_SELECTOR_EXPR:
2994 S = new (Context) ObjCSelectorExpr(Empty);
2995 break;
2996 case pch::EXPR_OBJC_PROTOCOL_EXPR:
2997 S = new (Context) ObjCProtocolExpr(Empty);
2998 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002999 }
3000
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003001 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003002 if (Finished)
3003 break;
3004
Douglas Gregor456e0952009-04-17 22:13:46 +00003005 ++NumStatementsRead;
3006
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003007 if (S) {
3008 unsigned NumSubStmts = Reader.Visit(S);
3009 while (NumSubStmts > 0) {
3010 StmtStack.pop_back();
3011 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003012 }
3013 }
3014
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003015 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003016 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003017 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003018 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003019 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003020 return StmtStack.back();
3021}
3022
3023Expr *PCHReader::ReadExpr() {
3024 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003025}
3026
Douglas Gregor179cfb12009-04-10 20:39:37 +00003027DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003028 return Diag(SourceLocation(), DiagID);
3029}
3030
3031DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3032 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003033 Context.getSourceManager()),
3034 DiagID);
3035}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003036
Douglas Gregorc713da92009-04-21 22:25:48 +00003037/// \brief Retrieve the identifier table associated with the
3038/// preprocessor.
3039IdentifierTable &PCHReader::getIdentifierTable() {
3040 return PP.getIdentifierTable();
3041}
3042
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003043/// \brief Record that the given ID maps to the given switch-case
3044/// statement.
3045void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3046 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3047 SwitchCaseStmts[ID] = SC;
3048}
3049
3050/// \brief Retrieve the switch-case statement with the given ID.
3051SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3052 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3053 return SwitchCaseStmts[ID];
3054}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003055
3056/// \brief Record that the given label statement has been
3057/// deserialized and has the given ID.
3058void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3059 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3060 "Deserialized label twice");
3061 LabelStmts[ID] = S;
3062
3063 // If we've already seen any goto statements that point to this
3064 // label, resolve them now.
3065 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3066 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3067 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3068 Goto->second->setLabel(S);
3069 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003070
3071 // If we've already seen any address-label statements that point to
3072 // this label, resolve them now.
3073 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3074 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3075 = UnresolvedAddrLabelExprs.equal_range(ID);
3076 for (AddrLabelIter AddrLabel = AddrLabels.first;
3077 AddrLabel != AddrLabels.second; ++AddrLabel)
3078 AddrLabel->second->setLabel(S);
3079 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003080}
3081
3082/// \brief Set the label of the given statement to the label
3083/// identified by ID.
3084///
3085/// Depending on the order in which the label and other statements
3086/// referencing that label occur, this operation may complete
3087/// immediately (updating the statement) or it may queue the
3088/// statement to be back-patched later.
3089void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3090 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3091 if (Label != LabelStmts.end()) {
3092 // We've already seen this label, so set the label of the goto and
3093 // we're done.
3094 S->setLabel(Label->second);
3095 } else {
3096 // We haven't seen this label yet, so add this goto to the set of
3097 // unresolved goto statements.
3098 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3099 }
3100}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003101
3102/// \brief Set the label of the given expression to the label
3103/// identified by ID.
3104///
3105/// Depending on the order in which the label and other statements
3106/// referencing that label occur, this operation may complete
3107/// immediately (updating the statement) or it may queue the
3108/// statement to be back-patched later.
3109void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3110 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3111 if (Label != LabelStmts.end()) {
3112 // We've already seen this label, so set the label of the
3113 // label-address expression and we're done.
3114 S->setLabel(Label->second);
3115 } else {
3116 // We haven't seen this label yet, so add this label-address
3117 // expression to the set of unresolved label-address expressions.
3118 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3119 }
3120}