blob: abdb24f0cadb634b14796bf4b2485ca87597e2c3 [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 Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000071 void VisitObjCContainerDecl(ObjCContainerDecl *D);
72 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
73 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000074 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
75 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
76 void VisitObjCClassDecl(ObjCClassDecl *D);
77 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
78 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
79 void VisitObjCImplDecl(ObjCImplDecl *D);
80 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
81 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
82 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
83 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
84 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +000085 };
86}
87
88void PCHDeclReader::VisitDecl(Decl *D) {
89 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
90 D->setLexicalDeclContext(
91 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
92 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
93 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000094 if (Record[Idx++])
95 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000096 D->setImplicit(Record[Idx++]);
97 D->setAccess((AccessSpecifier)Record[Idx++]);
98}
99
100void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
101 VisitDecl(TU);
102}
103
104void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
105 VisitDecl(ND);
106 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
107}
108
109void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
110 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000111 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
112}
113
114void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000115 // Note that we cannot use VisitTypeDecl here, because we need to
116 // set the underlying type of the typedef *before* we try to read
117 // the type associated with the TypedefDecl.
118 VisitNamedDecl(TD);
119 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
120 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
121 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000122}
123
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000124void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
125 VisitTypeDecl(TD);
126 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
127 TD->setDefinition(Record[Idx++]);
128 TD->setTypedefForAnonDecl(
129 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
130}
131
132void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
133 VisitTagDecl(ED);
134 ED->setIntegerType(Reader.GetType(Record[Idx++]));
135}
136
Douglas Gregor982365e2009-04-13 21:20:57 +0000137void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
138 VisitTagDecl(RD);
139 RD->setHasFlexibleArrayMember(Record[Idx++]);
140 RD->setAnonymousStructOrUnion(Record[Idx++]);
141}
142
Douglas Gregorc34897d2009-04-09 22:27:44 +0000143void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
144 VisitNamedDecl(VD);
145 VD->setType(Reader.GetType(Record[Idx++]));
146}
147
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000148void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
149 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000150 if (Record[Idx++])
151 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000152 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
153}
154
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000155void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
156 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000157 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000158 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000159 FD->setPreviousDeclaration(
160 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
161 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
162 FD->setInline(Record[Idx++]);
163 FD->setVirtual(Record[Idx++]);
164 FD->setPure(Record[Idx++]);
165 FD->setInheritedPrototype(Record[Idx++]);
166 FD->setHasPrototype(Record[Idx++]);
167 FD->setDeleted(Record[Idx++]);
168 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
169 unsigned NumParams = Record[Idx++];
170 llvm::SmallVector<ParmVarDecl *, 16> Params;
171 Params.reserve(NumParams);
172 for (unsigned I = 0; I != NumParams; ++I)
173 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
174 FD->setParams(Reader.getContext(), &Params[0], NumParams);
175}
176
Steve Naroff79ea0e02009-04-20 15:06:07 +0000177void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
178 VisitNamedDecl(MD);
179 if (Record[Idx++]) {
180 // In practice, this won't be executed (since method definitions
181 // don't occur in header files).
182 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
183 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
184 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
185 }
186 MD->setInstanceMethod(Record[Idx++]);
187 MD->setVariadic(Record[Idx++]);
188 MD->setSynthesized(Record[Idx++]);
189 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
190 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
191 MD->setResultType(Reader.GetType(Record[Idx++]));
192 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
193 unsigned NumParams = Record[Idx++];
194 llvm::SmallVector<ParmVarDecl *, 16> Params;
195 Params.reserve(NumParams);
196 for (unsigned I = 0; I != NumParams; ++I)
197 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
198 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
199}
200
Steve Naroff7333b492009-04-20 20:09:33 +0000201void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
202 VisitNamedDecl(CD);
203 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
204}
205
206void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
207 VisitObjCContainerDecl(ID);
208 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
209 ID->setSuperClass(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
210 unsigned NumIvars = Record[Idx++];
211 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
212 IVars.reserve(NumIvars);
213 for (unsigned I = 0; I != NumIvars; ++I)
214 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
215 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
216
217 ID->setForwardDecl(Record[Idx++]);
218 ID->setImplicitInterfaceDecl(Record[Idx++]);
219 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
220 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000221 // FIXME: add protocols, categories.
222}
223
224void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
225 VisitFieldDecl(IVD);
226 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
227}
228
Steve Naroff97b53bd2009-04-21 15:12:33 +0000229void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
230 VisitObjCContainerDecl(PD);
231 PD->setForwardDecl(Record[Idx++]);
232 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
233 unsigned NumProtoRefs = Record[Idx++];
234 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
235 ProtoRefs.reserve(NumProtoRefs);
236 for (unsigned I = 0; I != NumProtoRefs; ++I)
237 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
238 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
239}
240
241void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
242 VisitFieldDecl(FD);
243}
244
245void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
246 VisitDecl(CD);
247 unsigned NumClassRefs = Record[Idx++];
248 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
249 ClassRefs.reserve(NumClassRefs);
250 for (unsigned I = 0; I != NumClassRefs; ++I)
251 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
252 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
253}
254
255void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
256 VisitDecl(FPD);
257 unsigned NumProtoRefs = Record[Idx++];
258 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
259 ProtoRefs.reserve(NumProtoRefs);
260 for (unsigned I = 0; I != NumProtoRefs; ++I)
261 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
262 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
263}
264
265void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
266 VisitObjCContainerDecl(CD);
267 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
268 unsigned NumProtoRefs = Record[Idx++];
269 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
270 ProtoRefs.reserve(NumProtoRefs);
271 for (unsigned I = 0; I != NumProtoRefs; ++I)
272 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
274 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
275 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
276}
277
278void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
279 VisitNamedDecl(CAD);
280 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
281}
282
283void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
284 VisitNamedDecl(D);
285 // FIXME: Implement.
286}
287
288void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
289 VisitDecl(D);
290 // FIXME: Implement.
291}
292
293void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
294 VisitObjCImplDecl(D);
295 // FIXME: Implement.
296}
297
298void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
299 VisitObjCImplDecl(D);
300 // FIXME: Implement.
301}
302
303
304void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
305 VisitDecl(D);
306 // FIXME: Implement.
307}
308
Douglas Gregor982365e2009-04-13 21:20:57 +0000309void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
310 VisitValueDecl(FD);
311 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000312 if (Record[Idx++])
313 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000314}
315
Douglas Gregorc34897d2009-04-09 22:27:44 +0000316void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
317 VisitValueDecl(VD);
318 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
319 VD->setThreadSpecified(Record[Idx++]);
320 VD->setCXXDirectInitializer(Record[Idx++]);
321 VD->setDeclaredInCondition(Record[Idx++]);
322 VD->setPreviousDeclaration(
323 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
324 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000325 if (Record[Idx++])
326 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000327}
328
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000329void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
330 VisitVarDecl(PD);
331 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000332 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000333}
334
335void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
336 VisitParmVarDecl(PD);
337 PD->setOriginalType(Reader.GetType(Record[Idx++]));
338}
339
Douglas Gregor2a491792009-04-13 22:49:25 +0000340void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
341 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000342 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000343}
344
345void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
346 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000347 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000348 unsigned NumParams = Record[Idx++];
349 llvm::SmallVector<ParmVarDecl *, 16> Params;
350 Params.reserve(NumParams);
351 for (unsigned I = 0; I != NumParams; ++I)
352 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
353 BD->setParams(Reader.getContext(), &Params[0], NumParams);
354}
355
Douglas Gregorc34897d2009-04-09 22:27:44 +0000356std::pair<uint64_t, uint64_t>
357PCHDeclReader::VisitDeclContext(DeclContext *DC) {
358 uint64_t LexicalOffset = Record[Idx++];
359 uint64_t VisibleOffset = 0;
360 if (DC->getPrimaryContext() == DC)
361 VisibleOffset = Record[Idx++];
362 return std::make_pair(LexicalOffset, VisibleOffset);
363}
364
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000365//===----------------------------------------------------------------------===//
366// Statement/expression deserialization
367//===----------------------------------------------------------------------===//
368namespace {
369 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000370 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000371 PCHReader &Reader;
372 const PCHReader::RecordData &Record;
373 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000374 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375
376 public:
377 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000378 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
379 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000380
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000381 /// \brief The number of record fields required for the Stmt class
382 /// itself.
383 static const unsigned NumStmtFields = 0;
384
Douglas Gregor596e0932009-04-15 16:35:07 +0000385 /// \brief The number of record fields required for the Expr class
386 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000387 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000388
Douglas Gregora151ba42009-04-14 23:32:43 +0000389 // Each of the Visit* functions reads in part of the expression
390 // from the given record and the current expression stack, then
391 // return the total number of operands that it read from the
392 // expression stack.
393
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000394 unsigned VisitStmt(Stmt *S);
395 unsigned VisitNullStmt(NullStmt *S);
396 unsigned VisitCompoundStmt(CompoundStmt *S);
397 unsigned VisitSwitchCase(SwitchCase *S);
398 unsigned VisitCaseStmt(CaseStmt *S);
399 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000400 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000401 unsigned VisitIfStmt(IfStmt *S);
402 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000403 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000404 unsigned VisitDoStmt(DoStmt *S);
405 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000406 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000407 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000408 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000409 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000410 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000411 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000412 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000413 unsigned VisitExpr(Expr *E);
414 unsigned VisitPredefinedExpr(PredefinedExpr *E);
415 unsigned VisitDeclRefExpr(DeclRefExpr *E);
416 unsigned VisitIntegerLiteral(IntegerLiteral *E);
417 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000418 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000419 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000420 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000421 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000422 unsigned VisitUnaryOperator(UnaryOperator *E);
423 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000424 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000425 unsigned VisitCallExpr(CallExpr *E);
426 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000427 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000428 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000429 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
430 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000431 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000432 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
433 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000434 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000435 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000436 unsigned VisitInitListExpr(InitListExpr *E);
437 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
438 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000439 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000440 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000441 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000442 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
443 unsigned VisitChooseExpr(ChooseExpr *E);
444 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000445 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000446 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000447 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000448 };
449}
450
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000451unsigned PCHStmtReader::VisitStmt(Stmt *S) {
452 assert(Idx == NumStmtFields && "Incorrect statement field count");
453 return 0;
454}
455
456unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
457 VisitStmt(S);
458 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
459 return 0;
460}
461
462unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
463 VisitStmt(S);
464 unsigned NumStmts = Record[Idx++];
465 S->setStmts(Reader.getContext(),
466 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
467 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
468 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
469 return NumStmts;
470}
471
472unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
473 VisitStmt(S);
474 Reader.RecordSwitchCaseID(S, Record[Idx++]);
475 return 0;
476}
477
478unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
479 VisitSwitchCase(S);
480 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
481 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
482 S->setSubStmt(StmtStack.back());
483 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
484 return 3;
485}
486
487unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
488 VisitSwitchCase(S);
489 S->setSubStmt(StmtStack.back());
490 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
491 return 1;
492}
493
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000494unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
495 VisitStmt(S);
496 S->setID(Reader.GetIdentifierInfo(Record, Idx));
497 S->setSubStmt(StmtStack.back());
498 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
499 Reader.RecordLabelStmt(S, Record[Idx++]);
500 return 1;
501}
502
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000503unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
504 VisitStmt(S);
505 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
506 S->setThen(StmtStack[StmtStack.size() - 2]);
507 S->setElse(StmtStack[StmtStack.size() - 1]);
508 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
509 return 3;
510}
511
512unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
513 VisitStmt(S);
514 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
515 S->setBody(StmtStack.back());
516 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
517 SwitchCase *PrevSC = 0;
518 for (unsigned N = Record.size(); Idx != N; ++Idx) {
519 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
520 if (PrevSC)
521 PrevSC->setNextSwitchCase(SC);
522 else
523 S->setSwitchCaseList(SC);
524 PrevSC = SC;
525 }
526 return 2;
527}
528
Douglas Gregora6b503f2009-04-17 00:16:09 +0000529unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
530 VisitStmt(S);
531 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
532 S->setBody(StmtStack.back());
533 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
534 return 2;
535}
536
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000537unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
538 VisitStmt(S);
539 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
540 S->setBody(StmtStack.back());
541 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 return 2;
543}
544
545unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
546 VisitStmt(S);
547 S->setInit(StmtStack[StmtStack.size() - 4]);
548 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
549 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
550 S->setBody(StmtStack.back());
551 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
552 return 4;
553}
554
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000555unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
556 VisitStmt(S);
557 Reader.SetLabelOf(S, Record[Idx++]);
558 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
559 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
560 return 0;
561}
562
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000563unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
564 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000565 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000566 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
567 return 1;
568}
569
Douglas Gregora6b503f2009-04-17 00:16:09 +0000570unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
571 VisitStmt(S);
572 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
573 return 0;
574}
575
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000576unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
577 VisitStmt(S);
578 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
579 return 0;
580}
581
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000582unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
583 VisitStmt(S);
584 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
585 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
586 return 1;
587}
588
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000589unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
590 VisitStmt(S);
591 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
593
594 if (Idx + 1 == Record.size()) {
595 // Single declaration
596 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
597 } else {
598 llvm::SmallVector<Decl *, 16> Decls;
599 Decls.reserve(Record.size() - Idx);
600 for (unsigned N = Record.size(); Idx != N; ++Idx)
601 Decls.push_back(Reader.GetDecl(Record[Idx]));
602 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
603 &Decls[0], Decls.size())));
604 }
605 return 0;
606}
607
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000608unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
609 VisitStmt(S);
610 unsigned NumOutputs = Record[Idx++];
611 unsigned NumInputs = Record[Idx++];
612 unsigned NumClobbers = Record[Idx++];
613 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
614 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
615 S->setVolatile(Record[Idx++]);
616 S->setSimple(Record[Idx++]);
617
618 unsigned StackIdx
619 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
620 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
621
622 // Outputs and inputs
623 llvm::SmallVector<std::string, 16> Names;
624 llvm::SmallVector<StringLiteral*, 16> Constraints;
625 llvm::SmallVector<Stmt*, 16> Exprs;
626 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
627 Names.push_back(Reader.ReadString(Record, Idx));
628 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
629 Exprs.push_back(StmtStack[StackIdx++]);
630 }
631 S->setOutputsAndInputs(NumOutputs, NumInputs,
632 &Names[0], &Constraints[0], &Exprs[0]);
633
634 // Constraints
635 llvm::SmallVector<StringLiteral*, 16> Clobbers;
636 for (unsigned I = 0; I != NumClobbers; ++I)
637 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
638 S->setClobbers(&Clobbers[0], NumClobbers);
639
640 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
641 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
642}
643
Douglas Gregora151ba42009-04-14 23:32:43 +0000644unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000645 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000646 E->setType(Reader.GetType(Record[Idx++]));
647 E->setTypeDependent(Record[Idx++]);
648 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000649 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000650 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000651}
652
Douglas Gregora151ba42009-04-14 23:32:43 +0000653unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000654 VisitExpr(E);
655 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
656 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000657 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000658}
659
Douglas Gregora151ba42009-04-14 23:32:43 +0000660unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000661 VisitExpr(E);
662 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
663 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000664 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000665}
666
Douglas Gregora151ba42009-04-14 23:32:43 +0000667unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000668 VisitExpr(E);
669 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
670 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000671 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000672}
673
Douglas Gregora151ba42009-04-14 23:32:43 +0000674unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000675 VisitExpr(E);
676 E->setValue(Reader.ReadAPFloat(Record, Idx));
677 E->setExact(Record[Idx++]);
678 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000679 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000680}
681
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000682unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
683 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000684 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000685 return 1;
686}
687
Douglas Gregor596e0932009-04-15 16:35:07 +0000688unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
689 VisitExpr(E);
690 unsigned Len = Record[Idx++];
691 assert(Record[Idx] == E->getNumConcatenated() &&
692 "Wrong number of concatenated tokens!");
693 ++Idx;
694 E->setWide(Record[Idx++]);
695
696 // Read string data
697 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
698 E->setStrData(Reader.getContext(), &Str[0], Len);
699 Idx += Len;
700
701 // Read source locations
702 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
703 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
704
705 return 0;
706}
707
Douglas Gregora151ba42009-04-14 23:32:43 +0000708unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000709 VisitExpr(E);
710 E->setValue(Record[Idx++]);
711 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
712 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000713 return 0;
714}
715
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000716unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
717 VisitExpr(E);
718 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
719 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000720 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000721 return 1;
722}
723
Douglas Gregor12d74052009-04-15 15:58:59 +0000724unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
725 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000726 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000727 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
728 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
729 return 1;
730}
731
732unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
733 VisitExpr(E);
734 E->setSizeof(Record[Idx++]);
735 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000736 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000737 ++Idx;
738 } else {
739 E->setArgument(Reader.GetType(Record[Idx++]));
740 }
741 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
742 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
743 return E->isArgumentType()? 0 : 1;
744}
745
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000746unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
747 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000748 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
749 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000750 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
751 return 2;
752}
753
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000754unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
755 VisitExpr(E);
756 E->setNumArgs(Reader.getContext(), Record[Idx++]);
757 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000758 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000759 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000760 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000761 return E->getNumArgs() + 1;
762}
763
764unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
765 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000766 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000767 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
768 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
769 E->setArrow(Record[Idx++]);
770 return 1;
771}
772
Douglas Gregora151ba42009-04-14 23:32:43 +0000773unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
774 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000775 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000776 return 1;
777}
778
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000779unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
780 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000781 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
782 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000783 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
784 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
785 return 2;
786}
787
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000788unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
789 VisitBinaryOperator(E);
790 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
791 E->setComputationResultType(Reader.GetType(Record[Idx++]));
792 return 2;
793}
794
795unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
796 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000797 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
798 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
799 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000800 return 3;
801}
802
Douglas Gregora151ba42009-04-14 23:32:43 +0000803unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
804 VisitCastExpr(E);
805 E->setLvalueCast(Record[Idx++]);
806 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000807}
808
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000809unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
810 VisitCastExpr(E);
811 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
812 return 1;
813}
814
815unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
816 VisitExplicitCastExpr(E);
817 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
818 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
819 return 1;
820}
821
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000822unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
823 VisitExpr(E);
824 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000825 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000826 E->setFileScope(Record[Idx++]);
827 return 1;
828}
829
Douglas Gregorec0b8292009-04-15 23:02:49 +0000830unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
831 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000832 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000833 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
834 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
835 return 1;
836}
837
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000838unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
839 VisitExpr(E);
840 unsigned NumInits = Record[Idx++];
841 E->reserveInits(NumInits);
842 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000843 E->updateInit(I,
844 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
845 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000846 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
847 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
848 E->setInitializedFieldInUnion(
849 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
850 E->sawArrayRangeDesignator(Record[Idx++]);
851 return NumInits + 1;
852}
853
854unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
855 typedef DesignatedInitExpr::Designator Designator;
856
857 VisitExpr(E);
858 unsigned NumSubExprs = Record[Idx++];
859 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
860 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000861 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000862 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
863 E->setGNUSyntax(Record[Idx++]);
864
865 llvm::SmallVector<Designator, 4> Designators;
866 while (Idx < Record.size()) {
867 switch ((pch::DesignatorTypes)Record[Idx++]) {
868 case pch::DESIG_FIELD_DECL: {
869 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
870 SourceLocation DotLoc
871 = SourceLocation::getFromRawEncoding(Record[Idx++]);
872 SourceLocation FieldLoc
873 = SourceLocation::getFromRawEncoding(Record[Idx++]);
874 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
875 FieldLoc));
876 Designators.back().setField(Field);
877 break;
878 }
879
880 case pch::DESIG_FIELD_NAME: {
881 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
882 SourceLocation DotLoc
883 = SourceLocation::getFromRawEncoding(Record[Idx++]);
884 SourceLocation FieldLoc
885 = SourceLocation::getFromRawEncoding(Record[Idx++]);
886 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
887 break;
888 }
889
890 case pch::DESIG_ARRAY: {
891 unsigned Index = Record[Idx++];
892 SourceLocation LBracketLoc
893 = SourceLocation::getFromRawEncoding(Record[Idx++]);
894 SourceLocation RBracketLoc
895 = SourceLocation::getFromRawEncoding(Record[Idx++]);
896 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
897 break;
898 }
899
900 case pch::DESIG_ARRAY_RANGE: {
901 unsigned Index = Record[Idx++];
902 SourceLocation LBracketLoc
903 = SourceLocation::getFromRawEncoding(Record[Idx++]);
904 SourceLocation EllipsisLoc
905 = SourceLocation::getFromRawEncoding(Record[Idx++]);
906 SourceLocation RBracketLoc
907 = SourceLocation::getFromRawEncoding(Record[Idx++]);
908 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
909 RBracketLoc));
910 break;
911 }
912 }
913 }
914 E->setDesignators(&Designators[0], Designators.size());
915
916 return NumSubExprs;
917}
918
919unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
920 VisitExpr(E);
921 return 0;
922}
923
Douglas Gregorec0b8292009-04-15 23:02:49 +0000924unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
925 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000926 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000927 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
928 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
929 return 1;
930}
931
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000932unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
933 VisitExpr(E);
934 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
935 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
936 Reader.SetLabelOf(E, Record[Idx++]);
937 return 0;
938}
939
Douglas Gregoreca12f62009-04-17 19:05:30 +0000940unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
941 VisitExpr(E);
942 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
943 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
944 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
945 return 1;
946}
947
Douglas Gregor209d4622009-04-15 23:33:31 +0000948unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
949 VisitExpr(E);
950 E->setArgType1(Reader.GetType(Record[Idx++]));
951 E->setArgType2(Reader.GetType(Record[Idx++]));
952 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
953 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
954 return 0;
955}
956
957unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
958 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000959 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
960 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
961 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000962 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
963 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
964 return 3;
965}
966
967unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
968 VisitExpr(E);
969 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
970 return 0;
971}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000972
Douglas Gregor725e94b2009-04-16 00:01:45 +0000973unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
974 VisitExpr(E);
975 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000976 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000977 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
978 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 return NumExprs;
980}
981
Douglas Gregore246b742009-04-17 19:21:43 +0000982unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
983 VisitExpr(E);
984 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
985 E->setHasBlockDeclRefExprs(Record[Idx++]);
986 return 0;
987}
988
Douglas Gregor725e94b2009-04-16 00:01:45 +0000989unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
990 VisitExpr(E);
991 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
992 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 E->setByRef(Record[Idx++]);
994 return 0;
995}
996
Douglas Gregorc34897d2009-04-09 22:27:44 +0000997// FIXME: use the diagnostics machinery
998static bool Error(const char *Str) {
999 std::fprintf(stderr, "%s\n", Str);
1000 return true;
1001}
1002
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001003/// \brief Check the contents of the predefines buffer against the
1004/// contents of the predefines buffer used to build the PCH file.
1005///
1006/// The contents of the two predefines buffers should be the same. If
1007/// not, then some command-line option changed the preprocessor state
1008/// and we must reject the PCH file.
1009///
1010/// \param PCHPredef The start of the predefines buffer in the PCH
1011/// file.
1012///
1013/// \param PCHPredefLen The length of the predefines buffer in the PCH
1014/// file.
1015///
1016/// \param PCHBufferID The FileID for the PCH predefines buffer.
1017///
1018/// \returns true if there was a mismatch (in which case the PCH file
1019/// should be ignored), or false otherwise.
1020bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1021 unsigned PCHPredefLen,
1022 FileID PCHBufferID) {
1023 const char *Predef = PP.getPredefines().c_str();
1024 unsigned PredefLen = PP.getPredefines().size();
1025
1026 // If the two predefines buffers compare equal, we're done!.
1027 if (PredefLen == PCHPredefLen &&
1028 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1029 return false;
1030
1031 // The predefines buffers are different. Produce a reasonable
1032 // diagnostic showing where they are different.
1033
1034 // The source locations (potentially in the two different predefines
1035 // buffers)
1036 SourceLocation Loc1, Loc2;
1037 SourceManager &SourceMgr = PP.getSourceManager();
1038
1039 // Create a source buffer for our predefines string, so
1040 // that we can build a diagnostic that points into that
1041 // source buffer.
1042 FileID BufferID;
1043 if (Predef && Predef[0]) {
1044 llvm::MemoryBuffer *Buffer
1045 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1046 "<built-in>");
1047 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1048 }
1049
1050 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1051 std::pair<const char *, const char *> Locations
1052 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1053
1054 if (Locations.first != Predef + MinLen) {
1055 // We found the location in the two buffers where there is a
1056 // difference. Form source locations to point there (in both
1057 // buffers).
1058 unsigned Offset = Locations.first - Predef;
1059 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1060 .getFileLocWithOffset(Offset);
1061 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1062 .getFileLocWithOffset(Offset);
1063 } else if (PredefLen > PCHPredefLen) {
1064 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1065 .getFileLocWithOffset(MinLen);
1066 } else {
1067 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1068 .getFileLocWithOffset(MinLen);
1069 }
1070
1071 Diag(Loc1, diag::warn_pch_preprocessor);
1072 if (Loc2.isValid())
1073 Diag(Loc2, diag::note_predef_in_pch);
1074 Diag(diag::note_ignoring_pch) << FileName;
1075 return true;
1076}
1077
Douglas Gregor635f97f2009-04-13 16:31:14 +00001078/// \brief Read the line table in the source manager block.
1079/// \returns true if ther was an error.
1080static bool ParseLineTable(SourceManager &SourceMgr,
1081 llvm::SmallVectorImpl<uint64_t> &Record) {
1082 unsigned Idx = 0;
1083 LineTableInfo &LineTable = SourceMgr.getLineTable();
1084
1085 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001086 std::map<int, int> FileIDs;
1087 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001088 // Extract the file name
1089 unsigned FilenameLen = Record[Idx++];
1090 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1091 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001092 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1093 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001094 }
1095
1096 // Parse the line entries
1097 std::vector<LineEntry> Entries;
1098 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001099 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001100
1101 // Extract the line entries
1102 unsigned NumEntries = Record[Idx++];
1103 Entries.clear();
1104 Entries.reserve(NumEntries);
1105 for (unsigned I = 0; I != NumEntries; ++I) {
1106 unsigned FileOffset = Record[Idx++];
1107 unsigned LineNo = Record[Idx++];
1108 int FilenameID = Record[Idx++];
1109 SrcMgr::CharacteristicKind FileKind
1110 = (SrcMgr::CharacteristicKind)Record[Idx++];
1111 unsigned IncludeOffset = Record[Idx++];
1112 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1113 FileKind, IncludeOffset));
1114 }
1115 LineTable.AddEntry(FID, Entries);
1116 }
1117
1118 return false;
1119}
1120
Douglas Gregorab1cef72009-04-10 03:52:48 +00001121/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001122PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001123 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001124 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1125 Error("Malformed source manager block record");
1126 return Failure;
1127 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001128
1129 SourceManager &SourceMgr = Context.getSourceManager();
1130 RecordData Record;
1131 while (true) {
1132 unsigned Code = Stream.ReadCode();
1133 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001134 if (Stream.ReadBlockEnd()) {
1135 Error("Error at end of Source Manager block");
1136 return Failure;
1137 }
1138
1139 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001140 }
1141
1142 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1143 // No known subblocks, always skip them.
1144 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001145 if (Stream.SkipBlock()) {
1146 Error("Malformed block record");
1147 return Failure;
1148 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001149 continue;
1150 }
1151
1152 if (Code == llvm::bitc::DEFINE_ABBREV) {
1153 Stream.ReadAbbrevRecord();
1154 continue;
1155 }
1156
1157 // Read a record.
1158 const char *BlobStart;
1159 unsigned BlobLen;
1160 Record.clear();
1161 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1162 default: // Default behavior: ignore.
1163 break;
1164
1165 case pch::SM_SLOC_FILE_ENTRY: {
1166 // FIXME: We would really like to delay the creation of this
1167 // FileEntry until it is actually required, e.g., when producing
1168 // a diagnostic with a source location in this file.
1169 const FileEntry *File
1170 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1171 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001172 FileID ID = SourceMgr.createFileID(File,
1173 SourceLocation::getFromRawEncoding(Record[1]),
1174 (CharacteristicKind)Record[2]);
1175 if (Record[3])
1176 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1177 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001178 break;
1179 }
1180
1181 case pch::SM_SLOC_BUFFER_ENTRY: {
1182 const char *Name = BlobStart;
1183 unsigned Code = Stream.ReadCode();
1184 Record.clear();
1185 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1186 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001187 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001188 llvm::MemoryBuffer *Buffer
1189 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1190 BlobStart + BlobLen - 1,
1191 Name);
1192 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1193
1194 if (strcmp(Name, "<built-in>") == 0
1195 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1196 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001197 break;
1198 }
1199
1200 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1201 SourceLocation SpellingLoc
1202 = SourceLocation::getFromRawEncoding(Record[1]);
1203 SourceMgr.createInstantiationLoc(
1204 SpellingLoc,
1205 SourceLocation::getFromRawEncoding(Record[2]),
1206 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001207 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001208 break;
1209 }
1210
Chris Lattnere1be6022009-04-14 23:22:57 +00001211 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001212 if (ParseLineTable(SourceMgr, Record))
1213 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001214 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001215 }
1216 }
1217}
1218
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001219bool PCHReader::ReadPreprocessorBlock() {
1220 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1221 return Error("Malformed preprocessor block record");
1222
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001223 RecordData Record;
1224 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1225 MacroInfo *LastMacro = 0;
1226
1227 while (true) {
1228 unsigned Code = Stream.ReadCode();
1229 switch (Code) {
1230 case llvm::bitc::END_BLOCK:
1231 if (Stream.ReadBlockEnd())
1232 return Error("Error at end of preprocessor block");
1233 return false;
1234
1235 case llvm::bitc::ENTER_SUBBLOCK:
1236 // No known subblocks, always skip them.
1237 Stream.ReadSubBlockID();
1238 if (Stream.SkipBlock())
1239 return Error("Malformed block record");
1240 continue;
1241
1242 case llvm::bitc::DEFINE_ABBREV:
1243 Stream.ReadAbbrevRecord();
1244 continue;
1245 default: break;
1246 }
1247
1248 // Read a record.
1249 Record.clear();
1250 pch::PreprocessorRecordTypes RecType =
1251 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1252 switch (RecType) {
1253 default: // Default behavior: ignore unknown records.
1254 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001255 case pch::PP_COUNTER_VALUE:
1256 if (!Record.empty())
1257 PP.setCounterValue(Record[0]);
1258 break;
1259
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001260 case pch::PP_MACRO_OBJECT_LIKE:
1261 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001262 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1263 if (II == 0)
1264 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001265 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1266 bool isUsed = Record[2];
1267
1268 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1269 MI->setIsUsed(isUsed);
1270
1271 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1272 // Decode function-like macro info.
1273 bool isC99VarArgs = Record[3];
1274 bool isGNUVarArgs = Record[4];
1275 MacroArgs.clear();
1276 unsigned NumArgs = Record[5];
1277 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001278 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001279
1280 // Install function-like macro info.
1281 MI->setIsFunctionLike();
1282 if (isC99VarArgs) MI->setIsC99Varargs();
1283 if (isGNUVarArgs) MI->setIsGNUVarargs();
1284 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1285 PP.getPreprocessorAllocator());
1286 }
1287
1288 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001289 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001290
1291 // Remember that we saw this macro last so that we add the tokens that
1292 // form its body to it.
1293 LastMacro = MI;
1294 break;
1295 }
1296
1297 case pch::PP_TOKEN: {
1298 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1299 // pretend we didn't see this.
1300 if (LastMacro == 0) break;
1301
1302 Token Tok;
1303 Tok.startToken();
1304 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1305 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001306 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1307 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001308 Tok.setKind((tok::TokenKind)Record[3]);
1309 Tok.setFlag((Token::TokenFlags)Record[4]);
1310 LastMacro->AddTokenToBody(Tok);
1311 break;
1312 }
1313 }
1314 }
1315}
1316
Douglas Gregor179cfb12009-04-10 20:39:37 +00001317PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1318 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1319 Error("Malformed block record");
1320 return Failure;
1321 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001322
Chris Lattner29241862009-04-11 21:15:38 +00001323 uint64_t PreprocessorBlockBit = 0;
Douglas Gregor456e0952009-04-17 22:13:46 +00001324
Douglas Gregorc34897d2009-04-09 22:27:44 +00001325 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001326 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001327 while (!Stream.AtEndOfStream()) {
1328 unsigned Code = Stream.ReadCode();
1329 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001330 // If we saw the preprocessor block, read it now.
1331 if (PreprocessorBlockBit) {
1332 uint64_t SavedPos = Stream.GetCurrentBitNo();
1333 Stream.JumpToBit(PreprocessorBlockBit);
1334 if (ReadPreprocessorBlock()) {
1335 Error("Malformed preprocessor block");
1336 return Failure;
1337 }
1338 Stream.JumpToBit(SavedPos);
1339 }
1340
Douglas Gregor179cfb12009-04-10 20:39:37 +00001341 if (Stream.ReadBlockEnd()) {
1342 Error("Error at end of module block");
1343 return Failure;
1344 }
Chris Lattner29241862009-04-11 21:15:38 +00001345
Douglas Gregor179cfb12009-04-10 20:39:37 +00001346 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001347 }
1348
1349 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1350 switch (Stream.ReadSubBlockID()) {
1351 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1352 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1353 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001354 if (Stream.SkipBlock()) {
1355 Error("Malformed block record");
1356 return Failure;
1357 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001358 break;
1359
Chris Lattner29241862009-04-11 21:15:38 +00001360 case pch::PREPROCESSOR_BLOCK_ID:
1361 // Skip the preprocessor block for now, but remember where it is. We
1362 // want to read it in after the identifier table.
1363 if (PreprocessorBlockBit) {
1364 Error("Multiple preprocessor blocks found.");
1365 return Failure;
1366 }
1367 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1368 if (Stream.SkipBlock()) {
1369 Error("Malformed block record");
1370 return Failure;
1371 }
1372 break;
1373
Douglas Gregorab1cef72009-04-10 03:52:48 +00001374 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001375 switch (ReadSourceManagerBlock()) {
1376 case Success:
1377 break;
1378
1379 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001380 Error("Malformed source manager block");
1381 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001382
1383 case IgnorePCH:
1384 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001385 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001386 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001387 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001388 continue;
1389 }
1390
1391 if (Code == llvm::bitc::DEFINE_ABBREV) {
1392 Stream.ReadAbbrevRecord();
1393 continue;
1394 }
1395
1396 // Read and process a record.
1397 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001398 const char *BlobStart = 0;
1399 unsigned BlobLen = 0;
1400 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1401 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001402 default: // Default behavior: ignore.
1403 break;
1404
1405 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001406 if (!TypeOffsets.empty()) {
1407 Error("Duplicate TYPE_OFFSET record in PCH file");
1408 return Failure;
1409 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001410 TypeOffsets.swap(Record);
1411 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1412 break;
1413
1414 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001415 if (!DeclOffsets.empty()) {
1416 Error("Duplicate DECL_OFFSET record in PCH file");
1417 return Failure;
1418 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001419 DeclOffsets.swap(Record);
1420 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1421 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001422
1423 case pch::LANGUAGE_OPTIONS:
1424 if (ParseLanguageOptions(Record))
1425 return IgnorePCH;
1426 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001427
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001428 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001429 std::string TargetTriple(BlobStart, BlobLen);
1430 if (TargetTriple != Context.Target.getTargetTriple()) {
1431 Diag(diag::warn_pch_target_triple)
1432 << TargetTriple << Context.Target.getTargetTriple();
1433 Diag(diag::note_ignoring_pch) << FileName;
1434 return IgnorePCH;
1435 }
1436 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001437 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001438
1439 case pch::IDENTIFIER_TABLE:
1440 IdentifierTable = BlobStart;
1441 break;
1442
1443 case pch::IDENTIFIER_OFFSET:
1444 if (!IdentifierData.empty()) {
1445 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1446 return Failure;
1447 }
1448 IdentifierData.swap(Record);
1449#ifndef NDEBUG
1450 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1451 if ((IdentifierData[I] & 0x01) == 0) {
1452 Error("Malformed identifier table in the precompiled header");
1453 return Failure;
1454 }
1455 }
1456#endif
1457 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001458
1459 case pch::EXTERNAL_DEFINITIONS:
1460 if (!ExternalDefinitions.empty()) {
1461 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1462 return Failure;
1463 }
1464 ExternalDefinitions.swap(Record);
1465 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001466
Douglas Gregore01ad442009-04-18 05:55:16 +00001467 case pch::SPECIAL_TYPES:
1468 SpecialTypes.swap(Record);
1469 break;
1470
Douglas Gregor456e0952009-04-17 22:13:46 +00001471 case pch::STATISTICS:
1472 TotalNumStatements = Record[0];
1473 break;
1474
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001475 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001476 }
1477
Douglas Gregor179cfb12009-04-10 20:39:37 +00001478 Error("Premature end of bitstream");
1479 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001480}
1481
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001482PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001483 // Set the PCH file name.
1484 this->FileName = FileName;
1485
Douglas Gregorc34897d2009-04-09 22:27:44 +00001486 // Open the PCH file.
1487 std::string ErrStr;
1488 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001489 if (!Buffer) {
1490 Error(ErrStr.c_str());
1491 return IgnorePCH;
1492 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001493
1494 // Initialize the stream
1495 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1496 (const unsigned char *)Buffer->getBufferEnd());
1497
1498 // Sniff for the signature.
1499 if (Stream.Read(8) != 'C' ||
1500 Stream.Read(8) != 'P' ||
1501 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001502 Stream.Read(8) != 'H') {
1503 Error("Not a PCH file");
1504 return IgnorePCH;
1505 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001506
1507 // We expect a number of well-defined blocks, though we don't necessarily
1508 // need to understand them all.
1509 while (!Stream.AtEndOfStream()) {
1510 unsigned Code = Stream.ReadCode();
1511
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001512 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1513 Error("Invalid record at top-level");
1514 return Failure;
1515 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001516
1517 unsigned BlockID = Stream.ReadSubBlockID();
1518
1519 // We only know the PCH subblock ID.
1520 switch (BlockID) {
1521 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001522 if (Stream.ReadBlockInfoBlock()) {
1523 Error("Malformed BlockInfoBlock");
1524 return Failure;
1525 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001526 break;
1527 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001528 switch (ReadPCHBlock()) {
1529 case Success:
1530 break;
1531
1532 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001533 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001534
1535 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001536 // FIXME: We could consider reading through to the end of this
1537 // PCH block, skipping subblocks, to see if there are other
1538 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001539 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001540 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001541 break;
1542 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001543 if (Stream.SkipBlock()) {
1544 Error("Malformed block record");
1545 return Failure;
1546 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001547 break;
1548 }
1549 }
1550
1551 // Load the translation unit declaration
1552 ReadDeclRecord(DeclOffsets[0], 0);
1553
Douglas Gregore01ad442009-04-18 05:55:16 +00001554 // Load the special types.
1555 Context.setBuiltinVaListType(
1556 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1557
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001558 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001559}
1560
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001561namespace {
1562 /// \brief Helper class that saves the current stream position and
1563 /// then restores it when destroyed.
1564 struct VISIBILITY_HIDDEN SavedStreamPosition {
1565 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001566 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001567
1568 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001569 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001570 }
1571
1572 private:
1573 llvm::BitstreamReader &Stream;
1574 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001575 };
1576}
1577
Douglas Gregor179cfb12009-04-10 20:39:37 +00001578/// \brief Parse the record that corresponds to a LangOptions data
1579/// structure.
1580///
1581/// This routine compares the language options used to generate the
1582/// PCH file against the language options set for the current
1583/// compilation. For each option, we classify differences between the
1584/// two compiler states as either "benign" or "important". Benign
1585/// differences don't matter, and we accept them without complaint
1586/// (and without modifying the language options). Differences between
1587/// the states for important options cause the PCH file to be
1588/// unusable, so we emit a warning and return true to indicate that
1589/// there was an error.
1590///
1591/// \returns true if the PCH file is unacceptable, false otherwise.
1592bool PCHReader::ParseLanguageOptions(
1593 const llvm::SmallVectorImpl<uint64_t> &Record) {
1594 const LangOptions &LangOpts = Context.getLangOptions();
1595#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1596#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1597 if (Record[Idx] != LangOpts.Option) { \
1598 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1599 Diag(diag::note_ignoring_pch) << FileName; \
1600 return true; \
1601 } \
1602 ++Idx
1603
1604 unsigned Idx = 0;
1605 PARSE_LANGOPT_BENIGN(Trigraphs);
1606 PARSE_LANGOPT_BENIGN(BCPLComment);
1607 PARSE_LANGOPT_BENIGN(DollarIdents);
1608 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1609 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1610 PARSE_LANGOPT_BENIGN(ImplicitInt);
1611 PARSE_LANGOPT_BENIGN(Digraphs);
1612 PARSE_LANGOPT_BENIGN(HexFloats);
1613 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1614 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1615 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1616 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1617 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1618 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1619 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1620 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1621 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1622 PARSE_LANGOPT_BENIGN(PascalStrings);
1623 PARSE_LANGOPT_BENIGN(Boolean);
1624 PARSE_LANGOPT_BENIGN(WritableStrings);
1625 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1626 diag::warn_pch_lax_vector_conversions);
1627 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1628 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1629 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1630 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1631 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1632 diag::warn_pch_thread_safe_statics);
1633 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1634 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1635 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1636 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1637 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1638 diag::warn_pch_heinous_extensions);
1639 // FIXME: Most of the options below are benign if the macro wasn't
1640 // used. Unfortunately, this means that a PCH compiled without
1641 // optimization can't be used with optimization turned on, even
1642 // though the only thing that changes is whether __OPTIMIZE__ was
1643 // defined... but if __OPTIMIZE__ never showed up in the header, it
1644 // doesn't matter. We could consider making this some special kind
1645 // of check.
1646 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1647 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1648 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1649 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1650 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1651 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1652 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1653 Diag(diag::warn_pch_gc_mode)
1654 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1655 Diag(diag::note_ignoring_pch) << FileName;
1656 return true;
1657 }
1658 ++Idx;
1659 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1660 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1661#undef PARSE_LANGOPT_IRRELEVANT
1662#undef PARSE_LANGOPT_BENIGN
1663
1664 return false;
1665}
1666
Douglas Gregorc34897d2009-04-09 22:27:44 +00001667/// \brief Read and return the type at the given offset.
1668///
1669/// This routine actually reads the record corresponding to the type
1670/// at the given offset in the bitstream. It is a helper routine for
1671/// GetType, which deals with reading type IDs.
1672QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001673 // Keep track of where we are in the stream, then jump back there
1674 // after reading this type.
1675 SavedStreamPosition SavedPosition(Stream);
1676
Douglas Gregorc34897d2009-04-09 22:27:44 +00001677 Stream.JumpToBit(Offset);
1678 RecordData Record;
1679 unsigned Code = Stream.ReadCode();
1680 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001681 case pch::TYPE_EXT_QUAL: {
1682 assert(Record.size() == 3 &&
1683 "Incorrect encoding of extended qualifier type");
1684 QualType Base = GetType(Record[0]);
1685 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1686 unsigned AddressSpace = Record[2];
1687
1688 QualType T = Base;
1689 if (GCAttr != QualType::GCNone)
1690 T = Context.getObjCGCQualType(T, GCAttr);
1691 if (AddressSpace)
1692 T = Context.getAddrSpaceQualType(T, AddressSpace);
1693 return T;
1694 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001695
Douglas Gregorc34897d2009-04-09 22:27:44 +00001696 case pch::TYPE_FIXED_WIDTH_INT: {
1697 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1698 return Context.getFixedWidthIntType(Record[0], Record[1]);
1699 }
1700
1701 case pch::TYPE_COMPLEX: {
1702 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1703 QualType ElemType = GetType(Record[0]);
1704 return Context.getComplexType(ElemType);
1705 }
1706
1707 case pch::TYPE_POINTER: {
1708 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1709 QualType PointeeType = GetType(Record[0]);
1710 return Context.getPointerType(PointeeType);
1711 }
1712
1713 case pch::TYPE_BLOCK_POINTER: {
1714 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1715 QualType PointeeType = GetType(Record[0]);
1716 return Context.getBlockPointerType(PointeeType);
1717 }
1718
1719 case pch::TYPE_LVALUE_REFERENCE: {
1720 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1721 QualType PointeeType = GetType(Record[0]);
1722 return Context.getLValueReferenceType(PointeeType);
1723 }
1724
1725 case pch::TYPE_RVALUE_REFERENCE: {
1726 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1727 QualType PointeeType = GetType(Record[0]);
1728 return Context.getRValueReferenceType(PointeeType);
1729 }
1730
1731 case pch::TYPE_MEMBER_POINTER: {
1732 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1733 QualType PointeeType = GetType(Record[0]);
1734 QualType ClassType = GetType(Record[1]);
1735 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1736 }
1737
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001738 case pch::TYPE_CONSTANT_ARRAY: {
1739 QualType ElementType = GetType(Record[0]);
1740 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1741 unsigned IndexTypeQuals = Record[2];
1742 unsigned Idx = 3;
1743 llvm::APInt Size = ReadAPInt(Record, Idx);
1744 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1745 }
1746
1747 case pch::TYPE_INCOMPLETE_ARRAY: {
1748 QualType ElementType = GetType(Record[0]);
1749 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1750 unsigned IndexTypeQuals = Record[2];
1751 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1752 }
1753
1754 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001755 QualType ElementType = GetType(Record[0]);
1756 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1757 unsigned IndexTypeQuals = Record[2];
1758 return Context.getVariableArrayType(ElementType, ReadExpr(),
1759 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001760 }
1761
1762 case pch::TYPE_VECTOR: {
1763 if (Record.size() != 2) {
1764 Error("Incorrect encoding of vector type in PCH file");
1765 return QualType();
1766 }
1767
1768 QualType ElementType = GetType(Record[0]);
1769 unsigned NumElements = Record[1];
1770 return Context.getVectorType(ElementType, NumElements);
1771 }
1772
1773 case pch::TYPE_EXT_VECTOR: {
1774 if (Record.size() != 2) {
1775 Error("Incorrect encoding of extended vector type in PCH file");
1776 return QualType();
1777 }
1778
1779 QualType ElementType = GetType(Record[0]);
1780 unsigned NumElements = Record[1];
1781 return Context.getExtVectorType(ElementType, NumElements);
1782 }
1783
1784 case pch::TYPE_FUNCTION_NO_PROTO: {
1785 if (Record.size() != 1) {
1786 Error("Incorrect encoding of no-proto function type");
1787 return QualType();
1788 }
1789 QualType ResultType = GetType(Record[0]);
1790 return Context.getFunctionNoProtoType(ResultType);
1791 }
1792
1793 case pch::TYPE_FUNCTION_PROTO: {
1794 QualType ResultType = GetType(Record[0]);
1795 unsigned Idx = 1;
1796 unsigned NumParams = Record[Idx++];
1797 llvm::SmallVector<QualType, 16> ParamTypes;
1798 for (unsigned I = 0; I != NumParams; ++I)
1799 ParamTypes.push_back(GetType(Record[Idx++]));
1800 bool isVariadic = Record[Idx++];
1801 unsigned Quals = Record[Idx++];
1802 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1803 isVariadic, Quals);
1804 }
1805
1806 case pch::TYPE_TYPEDEF:
1807 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1808 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1809
1810 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001811 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001812
1813 case pch::TYPE_TYPEOF: {
1814 if (Record.size() != 1) {
1815 Error("Incorrect encoding of typeof(type) in PCH file");
1816 return QualType();
1817 }
1818 QualType UnderlyingType = GetType(Record[0]);
1819 return Context.getTypeOfType(UnderlyingType);
1820 }
1821
1822 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001823 assert(Record.size() == 1 && "Incorrect encoding of record type");
1824 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001825
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001826 case pch::TYPE_ENUM:
1827 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1828 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1829
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001830 case pch::TYPE_OBJC_INTERFACE:
1831 // FIXME: Deserialize ObjCInterfaceType
1832 assert(false && "Cannot de-serialize ObjC interface types yet");
1833 return QualType();
1834
1835 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1836 // FIXME: Deserialize ObjCQualifiedInterfaceType
1837 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1838 return QualType();
1839
1840 case pch::TYPE_OBJC_QUALIFIED_ID:
1841 // FIXME: Deserialize ObjCQualifiedIdType
1842 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1843 return QualType();
1844
1845 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1846 // FIXME: Deserialize ObjCQualifiedClassType
1847 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1848 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001849 }
1850
1851 // Suppress a GCC warning
1852 return QualType();
1853}
1854
1855/// \brief Note that we have loaded the declaration with the given
1856/// Index.
1857///
1858/// This routine notes that this declaration has already been loaded,
1859/// so that future GetDecl calls will return this declaration rather
1860/// than trying to load a new declaration.
1861inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1862 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1863 DeclAlreadyLoaded[Index] = true;
1864 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1865}
1866
1867/// \brief Read the declaration at the given offset from the PCH file.
1868Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001869 // Keep track of where we are in the stream, then jump back there
1870 // after reading this declaration.
1871 SavedStreamPosition SavedPosition(Stream);
1872
Douglas Gregorc34897d2009-04-09 22:27:44 +00001873 Decl *D = 0;
1874 Stream.JumpToBit(Offset);
1875 RecordData Record;
1876 unsigned Code = Stream.ReadCode();
1877 unsigned Idx = 0;
1878 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001879
Douglas Gregorc34897d2009-04-09 22:27:44 +00001880 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001881 case pch::DECL_ATTR:
1882 case pch::DECL_CONTEXT_LEXICAL:
1883 case pch::DECL_CONTEXT_VISIBLE:
1884 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1885 break;
1886
Douglas Gregorc34897d2009-04-09 22:27:44 +00001887 case pch::DECL_TRANSLATION_UNIT:
1888 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001889 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001890 break;
1891
1892 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001893 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001894 break;
1895 }
1896
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001897 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001898 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001899 break;
1900 }
1901
Douglas Gregor982365e2009-04-13 21:20:57 +00001902 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001903 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1904 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001905 break;
1906 }
1907
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001908 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001909 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1910 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001911 break;
1912 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001913
1914 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001915 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1916 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001917 break;
1918 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001919
Steve Naroff79ea0e02009-04-20 15:06:07 +00001920 case pch::DECL_OBJC_METHOD: {
1921 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1922 Selector(), QualType(), 0);
1923 break;
1924 }
1925
Steve Naroff97b53bd2009-04-21 15:12:33 +00001926 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00001927 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
1928 break;
1929 }
1930
Steve Naroff97b53bd2009-04-21 15:12:33 +00001931 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00001932 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1933 ObjCIvarDecl::None);
1934 break;
1935 }
1936
Steve Naroff97b53bd2009-04-21 15:12:33 +00001937 case pch::DECL_OBJC_PROTOCOL: {
1938 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
1939 break;
1940 }
1941
1942 case pch::DECL_OBJC_AT_DEFS_FIELD: {
1943 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
1944 QualType(), 0);
1945 break;
1946 }
1947
1948 case pch::DECL_OBJC_CLASS: {
1949 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
1950 break;
1951 }
1952
1953 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
1954 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
1955 break;
1956 }
1957
1958 case pch::DECL_OBJC_CATEGORY: {
1959 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
1960 break;
1961 }
1962
1963 case pch::DECL_OBJC_CATEGORY_IMPL: {
1964 // FIXME: Implement.
1965 break;
1966 }
1967
1968 case pch::DECL_OBJC_IMPLEMENTATION: {
1969 // FIXME: Implement.
1970 break;
1971 }
1972
1973 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
1974 // FIXME: Implement.
1975 break;
1976 }
1977
1978 case pch::DECL_OBJC_PROPERTY: {
1979 // FIXME: Implement.
1980 break;
1981 }
1982
1983 case pch::DECL_OBJC_PROPERTY_IMPL: {
1984 // FIXME: Implement.
1985 break;
1986 }
1987
Douglas Gregor982365e2009-04-13 21:20:57 +00001988 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001989 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1990 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001991 break;
1992 }
1993
Douglas Gregorc34897d2009-04-09 22:27:44 +00001994 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001995 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1996 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001997 break;
1998 }
1999
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002000 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002001 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2002 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002003 break;
2004 }
2005
2006 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002007 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002008 QualType(), QualType(), VarDecl::None,
2009 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002010 break;
2011 }
2012
Douglas Gregor2a491792009-04-13 22:49:25 +00002013 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002014 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002015 break;
2016 }
2017
2018 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002019 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002020 break;
2021 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002022 }
2023
Douglas Gregorddf4d092009-04-16 22:29:51 +00002024 assert(D && "Unknown declaration creating PCH file");
2025 if (D) {
2026 LoadedDecl(Index, D);
2027 Reader.Visit(D);
2028 }
2029
Douglas Gregorc34897d2009-04-09 22:27:44 +00002030 // If this declaration is also a declaration context, get the
2031 // offsets for its tables of lexical and visible declarations.
2032 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2033 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2034 if (Offsets.first || Offsets.second) {
2035 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2036 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2037 DeclContextOffsets[DC] = Offsets;
2038 }
2039 }
2040 assert(Idx == Record.size());
2041
2042 return D;
2043}
2044
Douglas Gregorac8f2802009-04-10 17:25:41 +00002045QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002046 unsigned Quals = ID & 0x07;
2047 unsigned Index = ID >> 3;
2048
2049 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2050 QualType T;
2051 switch ((pch::PredefinedTypeIDs)Index) {
2052 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2053 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2054 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2055
2056 case pch::PREDEF_TYPE_CHAR_U_ID:
2057 case pch::PREDEF_TYPE_CHAR_S_ID:
2058 // FIXME: Check that the signedness of CharTy is correct!
2059 T = Context.CharTy;
2060 break;
2061
2062 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2063 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2064 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2065 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2066 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2067 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2068 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2069 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2070 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2071 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2072 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2073 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2074 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2075 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2076 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2077 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2078 }
2079
2080 assert(!T.isNull() && "Unknown predefined type");
2081 return T.getQualifiedType(Quals);
2082 }
2083
2084 Index -= pch::NUM_PREDEF_TYPE_IDS;
2085 if (!TypeAlreadyLoaded[Index]) {
2086 // Load the type from the PCH file.
2087 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2088 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2089 TypeAlreadyLoaded[Index] = true;
2090 }
2091
2092 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2093}
2094
Douglas Gregorac8f2802009-04-10 17:25:41 +00002095Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002096 if (ID == 0)
2097 return 0;
2098
2099 unsigned Index = ID - 1;
2100 if (DeclAlreadyLoaded[Index])
2101 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2102
2103 // Load the declaration from the PCH file.
2104 return ReadDeclRecord(DeclOffsets[Index], Index);
2105}
2106
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002107Stmt *PCHReader::GetStmt(uint64_t Offset) {
2108 // Keep track of where we are in the stream, then jump back there
2109 // after reading this declaration.
2110 SavedStreamPosition SavedPosition(Stream);
2111
2112 Stream.JumpToBit(Offset);
2113 return ReadStmt();
2114}
2115
Douglas Gregorc34897d2009-04-09 22:27:44 +00002116bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002117 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002118 assert(DC->hasExternalLexicalStorage() &&
2119 "DeclContext has no lexical decls in storage");
2120 uint64_t Offset = DeclContextOffsets[DC].first;
2121 assert(Offset && "DeclContext has no lexical decls in storage");
2122
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002123 // Keep track of where we are in the stream, then jump back there
2124 // after reading this context.
2125 SavedStreamPosition SavedPosition(Stream);
2126
Douglas Gregorc34897d2009-04-09 22:27:44 +00002127 // Load the record containing all of the declarations lexically in
2128 // this context.
2129 Stream.JumpToBit(Offset);
2130 RecordData Record;
2131 unsigned Code = Stream.ReadCode();
2132 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002133 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002134 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2135
2136 // Load all of the declaration IDs
2137 Decls.clear();
2138 Decls.insert(Decls.end(), Record.begin(), Record.end());
2139 return false;
2140}
2141
2142bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2143 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2144 assert(DC->hasExternalVisibleStorage() &&
2145 "DeclContext has no visible decls in storage");
2146 uint64_t Offset = DeclContextOffsets[DC].second;
2147 assert(Offset && "DeclContext has no visible decls in storage");
2148
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002149 // Keep track of where we are in the stream, then jump back there
2150 // after reading this context.
2151 SavedStreamPosition SavedPosition(Stream);
2152
Douglas Gregorc34897d2009-04-09 22:27:44 +00002153 // Load the record containing all of the declarations visible in
2154 // this context.
2155 Stream.JumpToBit(Offset);
2156 RecordData Record;
2157 unsigned Code = Stream.ReadCode();
2158 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002159 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002160 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2161 if (Record.size() == 0)
2162 return false;
2163
2164 Decls.clear();
2165
2166 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002167 while (Idx < Record.size()) {
2168 Decls.push_back(VisibleDeclaration());
2169 Decls.back().Name = ReadDeclarationName(Record, Idx);
2170
Douglas Gregorc34897d2009-04-09 22:27:44 +00002171 unsigned Size = Record[Idx++];
2172 llvm::SmallVector<unsigned, 4> & LoadedDecls
2173 = Decls.back().Declarations;
2174 LoadedDecls.reserve(Size);
2175 for (unsigned I = 0; I < Size; ++I)
2176 LoadedDecls.push_back(Record[Idx++]);
2177 }
2178
2179 return false;
2180}
2181
Douglas Gregor631f6c62009-04-14 00:24:19 +00002182void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2183 if (!Consumer)
2184 return;
2185
2186 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2187 Decl *D = GetDecl(ExternalDefinitions[I]);
2188 DeclGroupRef DG(D);
2189 Consumer->HandleTopLevelDecl(DG);
2190 }
2191}
2192
Douglas Gregorc34897d2009-04-09 22:27:44 +00002193void PCHReader::PrintStats() {
2194 std::fprintf(stderr, "*** PCH Statistics:\n");
2195
2196 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2197 TypeAlreadyLoaded.end(),
2198 true);
2199 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2200 DeclAlreadyLoaded.end(),
2201 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002202 unsigned NumIdentifiersLoaded = 0;
2203 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2204 if ((IdentifierData[I] & 0x01) == 0)
2205 ++NumIdentifiersLoaded;
2206 }
2207
Douglas Gregorc34897d2009-04-09 22:27:44 +00002208 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2209 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002210 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002211 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2212 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002213 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2214 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2215 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2216 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002217 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2218 NumStatementsRead, TotalNumStatements,
2219 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002220 std::fprintf(stderr, "\n");
2221}
2222
Chris Lattner29241862009-04-11 21:15:38 +00002223IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002224 if (ID == 0)
2225 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002226
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002227 if (!IdentifierTable || IdentifierData.empty()) {
2228 Error("No identifier table in PCH file");
2229 return 0;
2230 }
Chris Lattner29241862009-04-11 21:15:38 +00002231
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002232 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002233 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002234 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorff9a6092009-04-20 20:36:09 +00002235 &Context.Idents.get(IdentifierTable + Offset));
2236 // FIXME: also read the contents of the IdentifierInfo.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002237 }
Chris Lattner29241862009-04-11 21:15:38 +00002238
2239 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002240}
2241
2242DeclarationName
2243PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2244 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2245 switch (Kind) {
2246 case DeclarationName::Identifier:
2247 return DeclarationName(GetIdentifierInfo(Record, Idx));
2248
2249 case DeclarationName::ObjCZeroArgSelector:
2250 case DeclarationName::ObjCOneArgSelector:
2251 case DeclarationName::ObjCMultiArgSelector:
2252 assert(false && "Unable to de-serialize Objective-C selectors");
2253 break;
2254
2255 case DeclarationName::CXXConstructorName:
2256 return Context.DeclarationNames.getCXXConstructorName(
2257 GetType(Record[Idx++]));
2258
2259 case DeclarationName::CXXDestructorName:
2260 return Context.DeclarationNames.getCXXDestructorName(
2261 GetType(Record[Idx++]));
2262
2263 case DeclarationName::CXXConversionFunctionName:
2264 return Context.DeclarationNames.getCXXConversionFunctionName(
2265 GetType(Record[Idx++]));
2266
2267 case DeclarationName::CXXOperatorName:
2268 return Context.DeclarationNames.getCXXOperatorName(
2269 (OverloadedOperatorKind)Record[Idx++]);
2270
2271 case DeclarationName::CXXUsingDirective:
2272 return DeclarationName::getUsingDirectiveName();
2273 }
2274
2275 // Required to silence GCC warning
2276 return DeclarationName();
2277}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002278
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002279/// \brief Read an integral value
2280llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2281 unsigned BitWidth = Record[Idx++];
2282 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2283 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2284 Idx += NumWords;
2285 return Result;
2286}
2287
2288/// \brief Read a signed integral value
2289llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2290 bool isUnsigned = Record[Idx++];
2291 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2292}
2293
Douglas Gregore2f37202009-04-14 21:55:33 +00002294/// \brief Read a floating-point value
2295llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002296 return llvm::APFloat(ReadAPInt(Record, Idx));
2297}
2298
Douglas Gregor1c507882009-04-15 21:30:51 +00002299// \brief Read a string
2300std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2301 unsigned Len = Record[Idx++];
2302 std::string Result(&Record[Idx], &Record[Idx] + Len);
2303 Idx += Len;
2304 return Result;
2305}
2306
2307/// \brief Reads attributes from the current stream position.
2308Attr *PCHReader::ReadAttributes() {
2309 unsigned Code = Stream.ReadCode();
2310 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2311 "Expected unabbreviated record"); (void)Code;
2312
2313 RecordData Record;
2314 unsigned Idx = 0;
2315 unsigned RecCode = Stream.ReadRecord(Code, Record);
2316 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2317 (void)RecCode;
2318
2319#define SIMPLE_ATTR(Name) \
2320 case Attr::Name: \
2321 New = ::new (Context) Name##Attr(); \
2322 break
2323
2324#define STRING_ATTR(Name) \
2325 case Attr::Name: \
2326 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2327 break
2328
2329#define UNSIGNED_ATTR(Name) \
2330 case Attr::Name: \
2331 New = ::new (Context) Name##Attr(Record[Idx++]); \
2332 break
2333
2334 Attr *Attrs = 0;
2335 while (Idx < Record.size()) {
2336 Attr *New = 0;
2337 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2338 bool IsInherited = Record[Idx++];
2339
2340 switch (Kind) {
2341 STRING_ATTR(Alias);
2342 UNSIGNED_ATTR(Aligned);
2343 SIMPLE_ATTR(AlwaysInline);
2344 SIMPLE_ATTR(AnalyzerNoReturn);
2345 STRING_ATTR(Annotate);
2346 STRING_ATTR(AsmLabel);
2347
2348 case Attr::Blocks:
2349 New = ::new (Context) BlocksAttr(
2350 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2351 break;
2352
2353 case Attr::Cleanup:
2354 New = ::new (Context) CleanupAttr(
2355 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2356 break;
2357
2358 SIMPLE_ATTR(Const);
2359 UNSIGNED_ATTR(Constructor);
2360 SIMPLE_ATTR(DLLExport);
2361 SIMPLE_ATTR(DLLImport);
2362 SIMPLE_ATTR(Deprecated);
2363 UNSIGNED_ATTR(Destructor);
2364 SIMPLE_ATTR(FastCall);
2365
2366 case Attr::Format: {
2367 std::string Type = ReadString(Record, Idx);
2368 unsigned FormatIdx = Record[Idx++];
2369 unsigned FirstArg = Record[Idx++];
2370 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2371 break;
2372 }
2373
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002374 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002375
2376 case Attr::IBOutletKind:
2377 New = ::new (Context) IBOutletAttr();
2378 break;
2379
2380 SIMPLE_ATTR(NoReturn);
2381 SIMPLE_ATTR(NoThrow);
2382 SIMPLE_ATTR(Nodebug);
2383 SIMPLE_ATTR(Noinline);
2384
2385 case Attr::NonNull: {
2386 unsigned Size = Record[Idx++];
2387 llvm::SmallVector<unsigned, 16> ArgNums;
2388 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2389 Idx += Size;
2390 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2391 break;
2392 }
2393
2394 SIMPLE_ATTR(ObjCException);
2395 SIMPLE_ATTR(ObjCNSObject);
2396 SIMPLE_ATTR(Overloadable);
2397 UNSIGNED_ATTR(Packed);
2398 SIMPLE_ATTR(Pure);
2399 UNSIGNED_ATTR(Regparm);
2400 STRING_ATTR(Section);
2401 SIMPLE_ATTR(StdCall);
2402 SIMPLE_ATTR(TransparentUnion);
2403 SIMPLE_ATTR(Unavailable);
2404 SIMPLE_ATTR(Unused);
2405 SIMPLE_ATTR(Used);
2406
2407 case Attr::Visibility:
2408 New = ::new (Context) VisibilityAttr(
2409 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2410 break;
2411
2412 SIMPLE_ATTR(WarnUnusedResult);
2413 SIMPLE_ATTR(Weak);
2414 SIMPLE_ATTR(WeakImport);
2415 }
2416
2417 assert(New && "Unable to decode attribute?");
2418 New->setInherited(IsInherited);
2419 New->setNext(Attrs);
2420 Attrs = New;
2421 }
2422#undef UNSIGNED_ATTR
2423#undef STRING_ATTR
2424#undef SIMPLE_ATTR
2425
2426 // The list of attributes was built backwards. Reverse the list
2427 // before returning it.
2428 Attr *PrevAttr = 0, *NextAttr = 0;
2429 while (Attrs) {
2430 NextAttr = Attrs->getNext();
2431 Attrs->setNext(PrevAttr);
2432 PrevAttr = Attrs;
2433 Attrs = NextAttr;
2434 }
2435
2436 return PrevAttr;
2437}
2438
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002439Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002440 // Within the bitstream, expressions are stored in Reverse Polish
2441 // Notation, with each of the subexpressions preceding the
2442 // expression they are stored in. To evaluate expressions, we
2443 // continue reading expressions and placing them on the stack, with
2444 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002445 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002446 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002447 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002448 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002449 llvm::SmallVector<Stmt *, 16> StmtStack;
2450 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002451 Stmt::EmptyShell Empty;
2452
Douglas Gregora151ba42009-04-14 23:32:43 +00002453 while (true) {
2454 unsigned Code = Stream.ReadCode();
2455 if (Code == llvm::bitc::END_BLOCK) {
2456 if (Stream.ReadBlockEnd()) {
2457 Error("Error at end of Source Manager block");
2458 return 0;
2459 }
2460 break;
2461 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002462
Douglas Gregora151ba42009-04-14 23:32:43 +00002463 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2464 // No known subblocks, always skip them.
2465 Stream.ReadSubBlockID();
2466 if (Stream.SkipBlock()) {
2467 Error("Malformed block record");
2468 return 0;
2469 }
2470 continue;
2471 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002472
Douglas Gregora151ba42009-04-14 23:32:43 +00002473 if (Code == llvm::bitc::DEFINE_ABBREV) {
2474 Stream.ReadAbbrevRecord();
2475 continue;
2476 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002477
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002478 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002479 Idx = 0;
2480 Record.clear();
2481 bool Finished = false;
2482 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002483 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002484 Finished = true;
2485 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002486
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002487 case pch::STMT_NULL_PTR:
2488 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002489 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002490
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002491 case pch::STMT_NULL:
2492 S = new (Context) NullStmt(Empty);
2493 break;
2494
2495 case pch::STMT_COMPOUND:
2496 S = new (Context) CompoundStmt(Empty);
2497 break;
2498
2499 case pch::STMT_CASE:
2500 S = new (Context) CaseStmt(Empty);
2501 break;
2502
2503 case pch::STMT_DEFAULT:
2504 S = new (Context) DefaultStmt(Empty);
2505 break;
2506
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002507 case pch::STMT_LABEL:
2508 S = new (Context) LabelStmt(Empty);
2509 break;
2510
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002511 case pch::STMT_IF:
2512 S = new (Context) IfStmt(Empty);
2513 break;
2514
2515 case pch::STMT_SWITCH:
2516 S = new (Context) SwitchStmt(Empty);
2517 break;
2518
Douglas Gregora6b503f2009-04-17 00:16:09 +00002519 case pch::STMT_WHILE:
2520 S = new (Context) WhileStmt(Empty);
2521 break;
2522
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002523 case pch::STMT_DO:
2524 S = new (Context) DoStmt(Empty);
2525 break;
2526
2527 case pch::STMT_FOR:
2528 S = new (Context) ForStmt(Empty);
2529 break;
2530
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002531 case pch::STMT_GOTO:
2532 S = new (Context) GotoStmt(Empty);
2533 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002534
2535 case pch::STMT_INDIRECT_GOTO:
2536 S = new (Context) IndirectGotoStmt(Empty);
2537 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002538
Douglas Gregora6b503f2009-04-17 00:16:09 +00002539 case pch::STMT_CONTINUE:
2540 S = new (Context) ContinueStmt(Empty);
2541 break;
2542
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002543 case pch::STMT_BREAK:
2544 S = new (Context) BreakStmt(Empty);
2545 break;
2546
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002547 case pch::STMT_RETURN:
2548 S = new (Context) ReturnStmt(Empty);
2549 break;
2550
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002551 case pch::STMT_DECL:
2552 S = new (Context) DeclStmt(Empty);
2553 break;
2554
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002555 case pch::STMT_ASM:
2556 S = new (Context) AsmStmt(Empty);
2557 break;
2558
Douglas Gregora151ba42009-04-14 23:32:43 +00002559 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002560 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002561 break;
2562
2563 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002564 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002565 break;
2566
2567 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002568 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002569 break;
2570
2571 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002572 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002573 break;
2574
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002575 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002576 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002577 break;
2578
Douglas Gregor596e0932009-04-15 16:35:07 +00002579 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002580 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002581 Record[PCHStmtReader::NumExprFields + 1]);
2582 break;
2583
Douglas Gregora151ba42009-04-14 23:32:43 +00002584 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002585 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002586 break;
2587
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002588 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002589 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002590 break;
2591
Douglas Gregor12d74052009-04-15 15:58:59 +00002592 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002593 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002594 break;
2595
2596 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002597 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002598 break;
2599
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002600 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002601 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002602 break;
2603
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002604 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002605 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002606 break;
2607
2608 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002609 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002610 break;
2611
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002612 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002613 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002614 break;
2615
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002616 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002617 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002618 break;
2619
2620 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002621 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002622 break;
2623
Douglas Gregora151ba42009-04-14 23:32:43 +00002624 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002625 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002626 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002627
2628 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002629 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002630 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002631
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002632 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002633 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002634 break;
2635
Douglas Gregorec0b8292009-04-15 23:02:49 +00002636 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002637 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002638 break;
2639
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002640 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002641 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002642 break;
2643
2644 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002645 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002646 Record[PCHStmtReader::NumExprFields] - 1);
2647
2648 break;
2649
2650 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002651 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002652 break;
2653
Douglas Gregorec0b8292009-04-15 23:02:49 +00002654 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002655 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002656 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002657
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002658 case pch::EXPR_ADDR_LABEL:
2659 S = new (Context) AddrLabelExpr(Empty);
2660 break;
2661
Douglas Gregoreca12f62009-04-17 19:05:30 +00002662 case pch::EXPR_STMT:
2663 S = new (Context) StmtExpr(Empty);
2664 break;
2665
Douglas Gregor209d4622009-04-15 23:33:31 +00002666 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002667 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002668 break;
2669
2670 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002671 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002672 break;
2673
2674 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002675 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002676 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002677
2678 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002679 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002680 break;
2681
Douglas Gregore246b742009-04-17 19:21:43 +00002682 case pch::EXPR_BLOCK:
2683 S = new (Context) BlockExpr(Empty);
2684 break;
2685
Douglas Gregor725e94b2009-04-16 00:01:45 +00002686 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002687 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002688 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002689 }
2690
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002691 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002692 if (Finished)
2693 break;
2694
Douglas Gregor456e0952009-04-17 22:13:46 +00002695 ++NumStatementsRead;
2696
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002697 if (S) {
2698 unsigned NumSubStmts = Reader.Visit(S);
2699 while (NumSubStmts > 0) {
2700 StmtStack.pop_back();
2701 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002702 }
2703 }
2704
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002705 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002706 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002707 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002708 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002709 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002710 return StmtStack.back();
2711}
2712
2713Expr *PCHReader::ReadExpr() {
2714 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002715}
2716
Douglas Gregor179cfb12009-04-10 20:39:37 +00002717DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002718 return Diag(SourceLocation(), DiagID);
2719}
2720
2721DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2722 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002723 Context.getSourceManager()),
2724 DiagID);
2725}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002726
2727/// \brief Record that the given ID maps to the given switch-case
2728/// statement.
2729void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2730 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2731 SwitchCaseStmts[ID] = SC;
2732}
2733
2734/// \brief Retrieve the switch-case statement with the given ID.
2735SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2736 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2737 return SwitchCaseStmts[ID];
2738}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002739
2740/// \brief Record that the given label statement has been
2741/// deserialized and has the given ID.
2742void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2743 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2744 "Deserialized label twice");
2745 LabelStmts[ID] = S;
2746
2747 // If we've already seen any goto statements that point to this
2748 // label, resolve them now.
2749 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2750 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2751 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2752 Goto->second->setLabel(S);
2753 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002754
2755 // If we've already seen any address-label statements that point to
2756 // this label, resolve them now.
2757 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2758 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2759 = UnresolvedAddrLabelExprs.equal_range(ID);
2760 for (AddrLabelIter AddrLabel = AddrLabels.first;
2761 AddrLabel != AddrLabels.second; ++AddrLabel)
2762 AddrLabel->second->setLabel(S);
2763 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002764}
2765
2766/// \brief Set the label of the given statement to the label
2767/// identified by ID.
2768///
2769/// Depending on the order in which the label and other statements
2770/// referencing that label occur, this operation may complete
2771/// immediately (updating the statement) or it may queue the
2772/// statement to be back-patched later.
2773void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2774 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2775 if (Label != LabelStmts.end()) {
2776 // We've already seen this label, so set the label of the goto and
2777 // we're done.
2778 S->setLabel(Label->second);
2779 } else {
2780 // We haven't seen this label yet, so add this goto to the set of
2781 // unresolved goto statements.
2782 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2783 }
2784}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002785
2786/// \brief Set the label of the given expression to the label
2787/// identified by ID.
2788///
2789/// Depending on the order in which the label and other statements
2790/// referencing that label occur, this operation may complete
2791/// immediately (updating the statement) or it may queue the
2792/// statement to be back-patched later.
2793void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2794 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2795 if (Label != LabelStmts.end()) {
2796 // We've already seen this label, so set the label of the
2797 // label-address expression and we're done.
2798 S->setLabel(Label->second);
2799 } else {
2800 // We haven't seen this label yet, so add this label-address
2801 // expression to the set of unresolved label-address expressions.
2802 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2803 }
2804}