blob: 00075c88b670c10d37f00434a6fda5e13c3774ff [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Declaration deserialization
41//===----------------------------------------------------------------------===//
42namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000043 class VISIBILITY_HIDDEN PCHDeclReader
44 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000045 PCHReader &Reader;
46 const PCHReader::RecordData &Record;
47 unsigned &Idx;
48
49 public:
50 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
51 unsigned &Idx)
52 : Reader(Reader), Record(Record), Idx(Idx) { }
53
54 void VisitDecl(Decl *D);
55 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
56 void VisitNamedDecl(NamedDecl *ND);
57 void VisitTypeDecl(TypeDecl *TD);
58 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000059 void VisitTagDecl(TagDecl *TD);
60 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000061 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000063 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000064 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000065 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000066 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000067 void VisitParmVarDecl(ParmVarDecl *PD);
68 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000069 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
70 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000071 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000072 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000073 void VisitObjCContainerDecl(ObjCContainerDecl *D);
74 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
75 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000076 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
77 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
78 void VisitObjCClassDecl(ObjCClassDecl *D);
79 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
80 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
81 void VisitObjCImplDecl(ObjCImplDecl *D);
82 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
83 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
84 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
85 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
86 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +000087 };
88}
89
90void PCHDeclReader::VisitDecl(Decl *D) {
91 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
92 D->setLexicalDeclContext(
93 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
94 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
95 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000096 if (Record[Idx++])
97 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000098 D->setImplicit(Record[Idx++]);
99 D->setAccess((AccessSpecifier)Record[Idx++]);
100}
101
102void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
103 VisitDecl(TU);
104}
105
106void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
107 VisitDecl(ND);
108 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
109}
110
111void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
112 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000113 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
114}
115
116void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000117 // Note that we cannot use VisitTypeDecl here, because we need to
118 // set the underlying type of the typedef *before* we try to read
119 // the type associated with the TypedefDecl.
120 VisitNamedDecl(TD);
121 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
122 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
123 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000124}
125
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000126void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
127 VisitTypeDecl(TD);
128 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
129 TD->setDefinition(Record[Idx++]);
130 TD->setTypedefForAnonDecl(
131 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
132}
133
134void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
135 VisitTagDecl(ED);
136 ED->setIntegerType(Reader.GetType(Record[Idx++]));
137}
138
Douglas Gregor982365e2009-04-13 21:20:57 +0000139void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
140 VisitTagDecl(RD);
141 RD->setHasFlexibleArrayMember(Record[Idx++]);
142 RD->setAnonymousStructOrUnion(Record[Idx++]);
143}
144
Douglas Gregorc34897d2009-04-09 22:27:44 +0000145void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
146 VisitNamedDecl(VD);
147 VD->setType(Reader.GetType(Record[Idx++]));
148}
149
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000150void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
151 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000152 if (Record[Idx++])
153 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000154 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
155}
156
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000157void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
158 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000159 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000160 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000161 FD->setPreviousDeclaration(
162 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
163 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
164 FD->setInline(Record[Idx++]);
165 FD->setVirtual(Record[Idx++]);
166 FD->setPure(Record[Idx++]);
167 FD->setInheritedPrototype(Record[Idx++]);
168 FD->setHasPrototype(Record[Idx++]);
169 FD->setDeleted(Record[Idx++]);
170 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
171 unsigned NumParams = Record[Idx++];
172 llvm::SmallVector<ParmVarDecl *, 16> Params;
173 Params.reserve(NumParams);
174 for (unsigned I = 0; I != NumParams; ++I)
175 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
176 FD->setParams(Reader.getContext(), &Params[0], NumParams);
177}
178
Steve Naroff79ea0e02009-04-20 15:06:07 +0000179void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
180 VisitNamedDecl(MD);
181 if (Record[Idx++]) {
182 // In practice, this won't be executed (since method definitions
183 // don't occur in header files).
184 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
185 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
186 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
187 }
188 MD->setInstanceMethod(Record[Idx++]);
189 MD->setVariadic(Record[Idx++]);
190 MD->setSynthesized(Record[Idx++]);
191 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
192 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
193 MD->setResultType(Reader.GetType(Record[Idx++]));
194 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
195 unsigned NumParams = Record[Idx++];
196 llvm::SmallVector<ParmVarDecl *, 16> Params;
197 Params.reserve(NumParams);
198 for (unsigned I = 0; I != NumParams; ++I)
199 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
200 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
201}
202
Steve Naroff7333b492009-04-20 20:09:33 +0000203void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
204 VisitNamedDecl(CD);
205 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
206}
207
208void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
209 VisitObjCContainerDecl(ID);
210 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
211 ID->setSuperClass(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
212 unsigned NumIvars = Record[Idx++];
213 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
214 IVars.reserve(NumIvars);
215 for (unsigned I = 0; I != NumIvars; ++I)
216 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
217 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
218
219 ID->setForwardDecl(Record[Idx++]);
220 ID->setImplicitInterfaceDecl(Record[Idx++]);
221 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
222 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000223 // FIXME: add protocols, categories.
224}
225
226void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
227 VisitFieldDecl(IVD);
228 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
229}
230
Steve Naroff97b53bd2009-04-21 15:12:33 +0000231void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
232 VisitObjCContainerDecl(PD);
233 PD->setForwardDecl(Record[Idx++]);
234 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
235 unsigned NumProtoRefs = Record[Idx++];
236 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
237 ProtoRefs.reserve(NumProtoRefs);
238 for (unsigned I = 0; I != NumProtoRefs; ++I)
239 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
240 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
241}
242
243void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
244 VisitFieldDecl(FD);
245}
246
247void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
248 VisitDecl(CD);
249 unsigned NumClassRefs = Record[Idx++];
250 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
251 ClassRefs.reserve(NumClassRefs);
252 for (unsigned I = 0; I != NumClassRefs; ++I)
253 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
254 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
255}
256
257void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
258 VisitDecl(FPD);
259 unsigned NumProtoRefs = Record[Idx++];
260 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
261 ProtoRefs.reserve(NumProtoRefs);
262 for (unsigned I = 0; I != NumProtoRefs; ++I)
263 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
264 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
265}
266
267void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
268 VisitObjCContainerDecl(CD);
269 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
270 unsigned NumProtoRefs = Record[Idx++];
271 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
272 ProtoRefs.reserve(NumProtoRefs);
273 for (unsigned I = 0; I != NumProtoRefs; ++I)
274 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
275 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
276 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
277 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
278}
279
280void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
281 VisitNamedDecl(CAD);
282 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
283}
284
285void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
286 VisitNamedDecl(D);
287 // FIXME: Implement.
288}
289
290void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
291 VisitDecl(D);
292 // FIXME: Implement.
293}
294
295void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
296 VisitObjCImplDecl(D);
297 // FIXME: Implement.
298}
299
300void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
301 VisitObjCImplDecl(D);
302 // FIXME: Implement.
303}
304
305
306void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
307 VisitDecl(D);
308 // FIXME: Implement.
309}
310
Douglas Gregor982365e2009-04-13 21:20:57 +0000311void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
312 VisitValueDecl(FD);
313 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000314 if (Record[Idx++])
315 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000316}
317
Douglas Gregorc34897d2009-04-09 22:27:44 +0000318void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
319 VisitValueDecl(VD);
320 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
321 VD->setThreadSpecified(Record[Idx++]);
322 VD->setCXXDirectInitializer(Record[Idx++]);
323 VD->setDeclaredInCondition(Record[Idx++]);
324 VD->setPreviousDeclaration(
325 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
326 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000327 if (Record[Idx++])
328 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000329}
330
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000331void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
332 VisitVarDecl(PD);
333 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000334 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000335}
336
337void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
338 VisitParmVarDecl(PD);
339 PD->setOriginalType(Reader.GetType(Record[Idx++]));
340}
341
Douglas Gregor2a491792009-04-13 22:49:25 +0000342void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
343 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000344 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000345}
346
347void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
348 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000349 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000350 unsigned NumParams = Record[Idx++];
351 llvm::SmallVector<ParmVarDecl *, 16> Params;
352 Params.reserve(NumParams);
353 for (unsigned I = 0; I != NumParams; ++I)
354 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
355 BD->setParams(Reader.getContext(), &Params[0], NumParams);
356}
357
Douglas Gregorc34897d2009-04-09 22:27:44 +0000358std::pair<uint64_t, uint64_t>
359PCHDeclReader::VisitDeclContext(DeclContext *DC) {
360 uint64_t LexicalOffset = Record[Idx++];
361 uint64_t VisibleOffset = 0;
362 if (DC->getPrimaryContext() == DC)
363 VisibleOffset = Record[Idx++];
364 return std::make_pair(LexicalOffset, VisibleOffset);
365}
366
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000367//===----------------------------------------------------------------------===//
368// Statement/expression deserialization
369//===----------------------------------------------------------------------===//
370namespace {
371 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000372 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000373 PCHReader &Reader;
374 const PCHReader::RecordData &Record;
375 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000376 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000377
378 public:
379 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000380 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
381 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000382
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000383 /// \brief The number of record fields required for the Stmt class
384 /// itself.
385 static const unsigned NumStmtFields = 0;
386
Douglas Gregor596e0932009-04-15 16:35:07 +0000387 /// \brief The number of record fields required for the Expr class
388 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000389 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000390
Douglas Gregora151ba42009-04-14 23:32:43 +0000391 // Each of the Visit* functions reads in part of the expression
392 // from the given record and the current expression stack, then
393 // return the total number of operands that it read from the
394 // expression stack.
395
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000396 unsigned VisitStmt(Stmt *S);
397 unsigned VisitNullStmt(NullStmt *S);
398 unsigned VisitCompoundStmt(CompoundStmt *S);
399 unsigned VisitSwitchCase(SwitchCase *S);
400 unsigned VisitCaseStmt(CaseStmt *S);
401 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000402 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000403 unsigned VisitIfStmt(IfStmt *S);
404 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000405 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000406 unsigned VisitDoStmt(DoStmt *S);
407 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000408 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000409 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000410 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000411 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000412 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000413 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000414 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000415 unsigned VisitExpr(Expr *E);
416 unsigned VisitPredefinedExpr(PredefinedExpr *E);
417 unsigned VisitDeclRefExpr(DeclRefExpr *E);
418 unsigned VisitIntegerLiteral(IntegerLiteral *E);
419 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000420 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000421 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000422 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000423 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000424 unsigned VisitUnaryOperator(UnaryOperator *E);
425 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000426 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000427 unsigned VisitCallExpr(CallExpr *E);
428 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000429 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000430 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000431 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
432 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000433 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000434 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
435 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000436 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000437 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000438 unsigned VisitInitListExpr(InitListExpr *E);
439 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
440 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000441 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000442 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000443 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000444 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
445 unsigned VisitChooseExpr(ChooseExpr *E);
446 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000447 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000448 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000449 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000450 };
451}
452
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000453unsigned PCHStmtReader::VisitStmt(Stmt *S) {
454 assert(Idx == NumStmtFields && "Incorrect statement field count");
455 return 0;
456}
457
458unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
459 VisitStmt(S);
460 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
461 return 0;
462}
463
464unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
465 VisitStmt(S);
466 unsigned NumStmts = Record[Idx++];
467 S->setStmts(Reader.getContext(),
468 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
469 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
470 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
471 return NumStmts;
472}
473
474unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
475 VisitStmt(S);
476 Reader.RecordSwitchCaseID(S, Record[Idx++]);
477 return 0;
478}
479
480unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
481 VisitSwitchCase(S);
482 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
483 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
484 S->setSubStmt(StmtStack.back());
485 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
486 return 3;
487}
488
489unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
490 VisitSwitchCase(S);
491 S->setSubStmt(StmtStack.back());
492 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
493 return 1;
494}
495
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000496unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
497 VisitStmt(S);
498 S->setID(Reader.GetIdentifierInfo(Record, Idx));
499 S->setSubStmt(StmtStack.back());
500 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
501 Reader.RecordLabelStmt(S, Record[Idx++]);
502 return 1;
503}
504
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000505unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
506 VisitStmt(S);
507 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
508 S->setThen(StmtStack[StmtStack.size() - 2]);
509 S->setElse(StmtStack[StmtStack.size() - 1]);
510 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
511 return 3;
512}
513
514unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
515 VisitStmt(S);
516 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
517 S->setBody(StmtStack.back());
518 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
519 SwitchCase *PrevSC = 0;
520 for (unsigned N = Record.size(); Idx != N; ++Idx) {
521 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
522 if (PrevSC)
523 PrevSC->setNextSwitchCase(SC);
524 else
525 S->setSwitchCaseList(SC);
526 PrevSC = SC;
527 }
528 return 2;
529}
530
Douglas Gregora6b503f2009-04-17 00:16:09 +0000531unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
532 VisitStmt(S);
533 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
534 S->setBody(StmtStack.back());
535 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 return 2;
537}
538
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000539unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
540 VisitStmt(S);
541 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
542 S->setBody(StmtStack.back());
543 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
544 return 2;
545}
546
547unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
548 VisitStmt(S);
549 S->setInit(StmtStack[StmtStack.size() - 4]);
550 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
551 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
552 S->setBody(StmtStack.back());
553 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
554 return 4;
555}
556
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000557unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
558 VisitStmt(S);
559 Reader.SetLabelOf(S, Record[Idx++]);
560 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
561 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
562 return 0;
563}
564
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000565unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
566 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000567 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000568 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
569 return 1;
570}
571
Douglas Gregora6b503f2009-04-17 00:16:09 +0000572unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
573 VisitStmt(S);
574 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
575 return 0;
576}
577
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000578unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
579 VisitStmt(S);
580 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 return 0;
582}
583
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000584unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
585 VisitStmt(S);
586 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
587 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
588 return 1;
589}
590
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000591unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
592 VisitStmt(S);
593 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595
596 if (Idx + 1 == Record.size()) {
597 // Single declaration
598 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
599 } else {
600 llvm::SmallVector<Decl *, 16> Decls;
601 Decls.reserve(Record.size() - Idx);
602 for (unsigned N = Record.size(); Idx != N; ++Idx)
603 Decls.push_back(Reader.GetDecl(Record[Idx]));
604 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
605 &Decls[0], Decls.size())));
606 }
607 return 0;
608}
609
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000610unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
611 VisitStmt(S);
612 unsigned NumOutputs = Record[Idx++];
613 unsigned NumInputs = Record[Idx++];
614 unsigned NumClobbers = Record[Idx++];
615 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
617 S->setVolatile(Record[Idx++]);
618 S->setSimple(Record[Idx++]);
619
620 unsigned StackIdx
621 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
622 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
623
624 // Outputs and inputs
625 llvm::SmallVector<std::string, 16> Names;
626 llvm::SmallVector<StringLiteral*, 16> Constraints;
627 llvm::SmallVector<Stmt*, 16> Exprs;
628 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
629 Names.push_back(Reader.ReadString(Record, Idx));
630 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
631 Exprs.push_back(StmtStack[StackIdx++]);
632 }
633 S->setOutputsAndInputs(NumOutputs, NumInputs,
634 &Names[0], &Constraints[0], &Exprs[0]);
635
636 // Constraints
637 llvm::SmallVector<StringLiteral*, 16> Clobbers;
638 for (unsigned I = 0; I != NumClobbers; ++I)
639 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
640 S->setClobbers(&Clobbers[0], NumClobbers);
641
642 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
643 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
644}
645
Douglas Gregora151ba42009-04-14 23:32:43 +0000646unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000647 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000648 E->setType(Reader.GetType(Record[Idx++]));
649 E->setTypeDependent(Record[Idx++]);
650 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000651 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000652 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000653}
654
Douglas Gregora151ba42009-04-14 23:32:43 +0000655unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000656 VisitExpr(E);
657 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
658 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000659 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000660}
661
Douglas Gregora151ba42009-04-14 23:32:43 +0000662unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000663 VisitExpr(E);
664 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
665 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000666 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000667}
668
Douglas Gregora151ba42009-04-14 23:32:43 +0000669unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000670 VisitExpr(E);
671 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
672 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000673 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000674}
675
Douglas Gregora151ba42009-04-14 23:32:43 +0000676unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000677 VisitExpr(E);
678 E->setValue(Reader.ReadAPFloat(Record, Idx));
679 E->setExact(Record[Idx++]);
680 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000681 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000682}
683
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000684unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
685 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000686 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000687 return 1;
688}
689
Douglas Gregor596e0932009-04-15 16:35:07 +0000690unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
691 VisitExpr(E);
692 unsigned Len = Record[Idx++];
693 assert(Record[Idx] == E->getNumConcatenated() &&
694 "Wrong number of concatenated tokens!");
695 ++Idx;
696 E->setWide(Record[Idx++]);
697
698 // Read string data
699 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
700 E->setStrData(Reader.getContext(), &Str[0], Len);
701 Idx += Len;
702
703 // Read source locations
704 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
705 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
706
707 return 0;
708}
709
Douglas Gregora151ba42009-04-14 23:32:43 +0000710unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000711 VisitExpr(E);
712 E->setValue(Record[Idx++]);
713 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
714 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000715 return 0;
716}
717
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000718unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
719 VisitExpr(E);
720 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
721 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000722 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000723 return 1;
724}
725
Douglas Gregor12d74052009-04-15 15:58:59 +0000726unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
727 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000728 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000729 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
730 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
731 return 1;
732}
733
734unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
735 VisitExpr(E);
736 E->setSizeof(Record[Idx++]);
737 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000738 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000739 ++Idx;
740 } else {
741 E->setArgument(Reader.GetType(Record[Idx++]));
742 }
743 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
744 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
745 return E->isArgumentType()? 0 : 1;
746}
747
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000748unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
749 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000750 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
751 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000752 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
753 return 2;
754}
755
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000756unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
757 VisitExpr(E);
758 E->setNumArgs(Reader.getContext(), Record[Idx++]);
759 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000760 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000761 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000762 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000763 return E->getNumArgs() + 1;
764}
765
766unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
767 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000768 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000769 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
770 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
771 E->setArrow(Record[Idx++]);
772 return 1;
773}
774
Douglas Gregora151ba42009-04-14 23:32:43 +0000775unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
776 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000777 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000778 return 1;
779}
780
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000781unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
782 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000783 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
784 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000785 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
786 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
787 return 2;
788}
789
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000790unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
791 VisitBinaryOperator(E);
792 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
793 E->setComputationResultType(Reader.GetType(Record[Idx++]));
794 return 2;
795}
796
797unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
798 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000799 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
800 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
801 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000802 return 3;
803}
804
Douglas Gregora151ba42009-04-14 23:32:43 +0000805unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
806 VisitCastExpr(E);
807 E->setLvalueCast(Record[Idx++]);
808 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000809}
810
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000811unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
812 VisitCastExpr(E);
813 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
814 return 1;
815}
816
817unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
818 VisitExplicitCastExpr(E);
819 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
820 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
821 return 1;
822}
823
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000824unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
825 VisitExpr(E);
826 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000827 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000828 E->setFileScope(Record[Idx++]);
829 return 1;
830}
831
Douglas Gregorec0b8292009-04-15 23:02:49 +0000832unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
833 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000834 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000835 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
836 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
837 return 1;
838}
839
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000840unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
841 VisitExpr(E);
842 unsigned NumInits = Record[Idx++];
843 E->reserveInits(NumInits);
844 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000845 E->updateInit(I,
846 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
847 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000848 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
849 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
850 E->setInitializedFieldInUnion(
851 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
852 E->sawArrayRangeDesignator(Record[Idx++]);
853 return NumInits + 1;
854}
855
856unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
857 typedef DesignatedInitExpr::Designator Designator;
858
859 VisitExpr(E);
860 unsigned NumSubExprs = Record[Idx++];
861 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
862 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000863 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000864 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
865 E->setGNUSyntax(Record[Idx++]);
866
867 llvm::SmallVector<Designator, 4> Designators;
868 while (Idx < Record.size()) {
869 switch ((pch::DesignatorTypes)Record[Idx++]) {
870 case pch::DESIG_FIELD_DECL: {
871 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
872 SourceLocation DotLoc
873 = SourceLocation::getFromRawEncoding(Record[Idx++]);
874 SourceLocation FieldLoc
875 = SourceLocation::getFromRawEncoding(Record[Idx++]);
876 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
877 FieldLoc));
878 Designators.back().setField(Field);
879 break;
880 }
881
882 case pch::DESIG_FIELD_NAME: {
883 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
884 SourceLocation DotLoc
885 = SourceLocation::getFromRawEncoding(Record[Idx++]);
886 SourceLocation FieldLoc
887 = SourceLocation::getFromRawEncoding(Record[Idx++]);
888 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
889 break;
890 }
891
892 case pch::DESIG_ARRAY: {
893 unsigned Index = Record[Idx++];
894 SourceLocation LBracketLoc
895 = SourceLocation::getFromRawEncoding(Record[Idx++]);
896 SourceLocation RBracketLoc
897 = SourceLocation::getFromRawEncoding(Record[Idx++]);
898 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
899 break;
900 }
901
902 case pch::DESIG_ARRAY_RANGE: {
903 unsigned Index = Record[Idx++];
904 SourceLocation LBracketLoc
905 = SourceLocation::getFromRawEncoding(Record[Idx++]);
906 SourceLocation EllipsisLoc
907 = SourceLocation::getFromRawEncoding(Record[Idx++]);
908 SourceLocation RBracketLoc
909 = SourceLocation::getFromRawEncoding(Record[Idx++]);
910 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
911 RBracketLoc));
912 break;
913 }
914 }
915 }
916 E->setDesignators(&Designators[0], Designators.size());
917
918 return NumSubExprs;
919}
920
921unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
922 VisitExpr(E);
923 return 0;
924}
925
Douglas Gregorec0b8292009-04-15 23:02:49 +0000926unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
927 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000928 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000929 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
930 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
931 return 1;
932}
933
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000934unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
935 VisitExpr(E);
936 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
937 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
938 Reader.SetLabelOf(E, Record[Idx++]);
939 return 0;
940}
941
Douglas Gregoreca12f62009-04-17 19:05:30 +0000942unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
943 VisitExpr(E);
944 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
945 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
946 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
947 return 1;
948}
949
Douglas Gregor209d4622009-04-15 23:33:31 +0000950unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
951 VisitExpr(E);
952 E->setArgType1(Reader.GetType(Record[Idx++]));
953 E->setArgType2(Reader.GetType(Record[Idx++]));
954 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
955 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
956 return 0;
957}
958
959unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
960 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000961 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
962 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
963 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000964 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
965 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
966 return 3;
967}
968
969unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
970 VisitExpr(E);
971 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
972 return 0;
973}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000974
Douglas Gregor725e94b2009-04-16 00:01:45 +0000975unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
976 VisitExpr(E);
977 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000978 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000979 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
981 return NumExprs;
982}
983
Douglas Gregore246b742009-04-17 19:21:43 +0000984unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
985 VisitExpr(E);
986 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
987 E->setHasBlockDeclRefExprs(Record[Idx++]);
988 return 0;
989}
990
Douglas Gregor725e94b2009-04-16 00:01:45 +0000991unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
992 VisitExpr(E);
993 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
994 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 E->setByRef(Record[Idx++]);
996 return 0;
997}
998
Douglas Gregorc713da92009-04-21 22:25:48 +0000999//===----------------------------------------------------------------------===//
1000// PCH reader implementation
1001//===----------------------------------------------------------------------===//
1002
1003namespace {
1004class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1005 PCHReader &Reader;
1006
1007 // If we know the IdentifierInfo in advance, it is here and we will
1008 // not build a new one. Used when deserializing information about an
1009 // identifier that was constructed before the PCH file was read.
1010 IdentifierInfo *KnownII;
1011
1012public:
1013 typedef IdentifierInfo * data_type;
1014
1015 typedef const std::pair<const char*, unsigned> external_key_type;
1016
1017 typedef external_key_type internal_key_type;
1018
1019 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1020 : Reader(Reader), KnownII(II) { }
1021
1022 static bool EqualKey(const internal_key_type& a,
1023 const internal_key_type& b) {
1024 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1025 : false;
1026 }
1027
1028 static unsigned ComputeHash(const internal_key_type& a) {
1029 return BernsteinHash(a.first, a.second);
1030 }
1031
1032 // This hopefully will just get inlined and removed by the optimizer.
1033 static const internal_key_type&
1034 GetInternalKey(const external_key_type& x) { return x; }
1035
1036 static std::pair<unsigned, unsigned>
1037 ReadKeyDataLength(const unsigned char*& d) {
1038 using namespace clang::io;
1039 unsigned KeyLen = ReadUnalignedLE16(d);
1040 unsigned DataLen = ReadUnalignedLE16(d);
1041 return std::make_pair(KeyLen, DataLen);
1042 }
1043
1044 static std::pair<const char*, unsigned>
1045 ReadKey(const unsigned char* d, unsigned n) {
1046 assert(n >= 2 && d[n-1] == '\0');
1047 return std::make_pair((const char*) d, n-1);
1048 }
1049
1050 IdentifierInfo *ReadData(const internal_key_type& k,
1051 const unsigned char* d,
1052 unsigned DataLen) {
1053 using namespace clang::io;
1054 uint32_t Bits = ReadUnalignedLE32(d); // FIXME: use these?
1055 (void)Bits;
1056 pch::IdentID ID = ReadUnalignedLE32(d);
1057 DataLen -= 8;
1058
1059 // Build the IdentifierInfo itself and link the identifier ID with
1060 // the new IdentifierInfo.
1061 IdentifierInfo *II = KnownII;
1062 if (!II)
1063 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1064 k.first, k.first + k.second);
1065 Reader.SetIdentifierInfo(ID, II);
1066
1067 // FIXME: If this identifier is a macro, deserialize the macro
1068 // definition now.
1069
1070 // Read all of the declarations visible at global scope with this
1071 // name.
1072 Sema *SemaObj = Reader.getSema();
1073 while (DataLen > 0) {
1074 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1075
1076 if (SemaObj) {
1077 // Introduce this declaration into the translation-unit scope
1078 // and add it to the declaration chain for this identifier, so
1079 // that (unqualified) name lookup will find it.
1080 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1081 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1082 } else {
1083 // Queue this declaration so that it will be added to the
1084 // translation unit scope and identifier's declaration chain
1085 // once a Sema object is known.
1086 // FIXME: This is a temporary hack. It will go away once we have
1087 // lazy deserialization of macros.
1088 Reader.TUDecls.push_back(D);
1089 }
1090
1091 DataLen -= 4;
1092 }
1093 return II;
1094 }
1095};
1096
1097} // end anonymous namespace
1098
1099/// \brief The on-disk hash table used to contain information about
1100/// all of the identifiers in the program.
1101typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1102 PCHIdentifierLookupTable;
1103
Douglas Gregorc34897d2009-04-09 22:27:44 +00001104// FIXME: use the diagnostics machinery
1105static bool Error(const char *Str) {
1106 std::fprintf(stderr, "%s\n", Str);
1107 return true;
1108}
1109
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001110/// \brief Check the contents of the predefines buffer against the
1111/// contents of the predefines buffer used to build the PCH file.
1112///
1113/// The contents of the two predefines buffers should be the same. If
1114/// not, then some command-line option changed the preprocessor state
1115/// and we must reject the PCH file.
1116///
1117/// \param PCHPredef The start of the predefines buffer in the PCH
1118/// file.
1119///
1120/// \param PCHPredefLen The length of the predefines buffer in the PCH
1121/// file.
1122///
1123/// \param PCHBufferID The FileID for the PCH predefines buffer.
1124///
1125/// \returns true if there was a mismatch (in which case the PCH file
1126/// should be ignored), or false otherwise.
1127bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1128 unsigned PCHPredefLen,
1129 FileID PCHBufferID) {
1130 const char *Predef = PP.getPredefines().c_str();
1131 unsigned PredefLen = PP.getPredefines().size();
1132
1133 // If the two predefines buffers compare equal, we're done!.
1134 if (PredefLen == PCHPredefLen &&
1135 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1136 return false;
1137
1138 // The predefines buffers are different. Produce a reasonable
1139 // diagnostic showing where they are different.
1140
1141 // The source locations (potentially in the two different predefines
1142 // buffers)
1143 SourceLocation Loc1, Loc2;
1144 SourceManager &SourceMgr = PP.getSourceManager();
1145
1146 // Create a source buffer for our predefines string, so
1147 // that we can build a diagnostic that points into that
1148 // source buffer.
1149 FileID BufferID;
1150 if (Predef && Predef[0]) {
1151 llvm::MemoryBuffer *Buffer
1152 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1153 "<built-in>");
1154 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1155 }
1156
1157 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1158 std::pair<const char *, const char *> Locations
1159 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1160
1161 if (Locations.first != Predef + MinLen) {
1162 // We found the location in the two buffers where there is a
1163 // difference. Form source locations to point there (in both
1164 // buffers).
1165 unsigned Offset = Locations.first - Predef;
1166 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1167 .getFileLocWithOffset(Offset);
1168 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1169 .getFileLocWithOffset(Offset);
1170 } else if (PredefLen > PCHPredefLen) {
1171 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1172 .getFileLocWithOffset(MinLen);
1173 } else {
1174 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1175 .getFileLocWithOffset(MinLen);
1176 }
1177
1178 Diag(Loc1, diag::warn_pch_preprocessor);
1179 if (Loc2.isValid())
1180 Diag(Loc2, diag::note_predef_in_pch);
1181 Diag(diag::note_ignoring_pch) << FileName;
1182 return true;
1183}
1184
Douglas Gregor635f97f2009-04-13 16:31:14 +00001185/// \brief Read the line table in the source manager block.
1186/// \returns true if ther was an error.
1187static bool ParseLineTable(SourceManager &SourceMgr,
1188 llvm::SmallVectorImpl<uint64_t> &Record) {
1189 unsigned Idx = 0;
1190 LineTableInfo &LineTable = SourceMgr.getLineTable();
1191
1192 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001193 std::map<int, int> FileIDs;
1194 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001195 // Extract the file name
1196 unsigned FilenameLen = Record[Idx++];
1197 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1198 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001199 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1200 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001201 }
1202
1203 // Parse the line entries
1204 std::vector<LineEntry> Entries;
1205 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001206 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001207
1208 // Extract the line entries
1209 unsigned NumEntries = Record[Idx++];
1210 Entries.clear();
1211 Entries.reserve(NumEntries);
1212 for (unsigned I = 0; I != NumEntries; ++I) {
1213 unsigned FileOffset = Record[Idx++];
1214 unsigned LineNo = Record[Idx++];
1215 int FilenameID = Record[Idx++];
1216 SrcMgr::CharacteristicKind FileKind
1217 = (SrcMgr::CharacteristicKind)Record[Idx++];
1218 unsigned IncludeOffset = Record[Idx++];
1219 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1220 FileKind, IncludeOffset));
1221 }
1222 LineTable.AddEntry(FID, Entries);
1223 }
1224
1225 return false;
1226}
1227
Douglas Gregorab1cef72009-04-10 03:52:48 +00001228/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001229PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001230 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001231 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1232 Error("Malformed source manager block record");
1233 return Failure;
1234 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001235
1236 SourceManager &SourceMgr = Context.getSourceManager();
1237 RecordData Record;
1238 while (true) {
1239 unsigned Code = Stream.ReadCode();
1240 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001241 if (Stream.ReadBlockEnd()) {
1242 Error("Error at end of Source Manager block");
1243 return Failure;
1244 }
1245
1246 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001247 }
1248
1249 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1250 // No known subblocks, always skip them.
1251 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001252 if (Stream.SkipBlock()) {
1253 Error("Malformed block record");
1254 return Failure;
1255 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001256 continue;
1257 }
1258
1259 if (Code == llvm::bitc::DEFINE_ABBREV) {
1260 Stream.ReadAbbrevRecord();
1261 continue;
1262 }
1263
1264 // Read a record.
1265 const char *BlobStart;
1266 unsigned BlobLen;
1267 Record.clear();
1268 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1269 default: // Default behavior: ignore.
1270 break;
1271
1272 case pch::SM_SLOC_FILE_ENTRY: {
1273 // FIXME: We would really like to delay the creation of this
1274 // FileEntry until it is actually required, e.g., when producing
1275 // a diagnostic with a source location in this file.
1276 const FileEntry *File
1277 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1278 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001279 FileID ID = SourceMgr.createFileID(File,
1280 SourceLocation::getFromRawEncoding(Record[1]),
1281 (CharacteristicKind)Record[2]);
1282 if (Record[3])
1283 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1284 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001285 break;
1286 }
1287
1288 case pch::SM_SLOC_BUFFER_ENTRY: {
1289 const char *Name = BlobStart;
1290 unsigned Code = Stream.ReadCode();
1291 Record.clear();
1292 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1293 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001294 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001295 llvm::MemoryBuffer *Buffer
1296 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1297 BlobStart + BlobLen - 1,
1298 Name);
1299 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1300
1301 if (strcmp(Name, "<built-in>") == 0
1302 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1303 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001304 break;
1305 }
1306
1307 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1308 SourceLocation SpellingLoc
1309 = SourceLocation::getFromRawEncoding(Record[1]);
1310 SourceMgr.createInstantiationLoc(
1311 SpellingLoc,
1312 SourceLocation::getFromRawEncoding(Record[2]),
1313 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001314 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001315 break;
1316 }
1317
Chris Lattnere1be6022009-04-14 23:22:57 +00001318 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001319 if (ParseLineTable(SourceMgr, Record))
1320 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001321 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001322 }
1323 }
1324}
1325
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001326bool PCHReader::ReadPreprocessorBlock() {
1327 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1328 return Error("Malformed preprocessor block record");
1329
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001330 RecordData Record;
1331 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1332 MacroInfo *LastMacro = 0;
1333
1334 while (true) {
1335 unsigned Code = Stream.ReadCode();
1336 switch (Code) {
1337 case llvm::bitc::END_BLOCK:
1338 if (Stream.ReadBlockEnd())
1339 return Error("Error at end of preprocessor block");
1340 return false;
1341
1342 case llvm::bitc::ENTER_SUBBLOCK:
1343 // No known subblocks, always skip them.
1344 Stream.ReadSubBlockID();
1345 if (Stream.SkipBlock())
1346 return Error("Malformed block record");
1347 continue;
1348
1349 case llvm::bitc::DEFINE_ABBREV:
1350 Stream.ReadAbbrevRecord();
1351 continue;
1352 default: break;
1353 }
1354
1355 // Read a record.
1356 Record.clear();
1357 pch::PreprocessorRecordTypes RecType =
1358 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1359 switch (RecType) {
1360 default: // Default behavior: ignore unknown records.
1361 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001362 case pch::PP_COUNTER_VALUE:
1363 if (!Record.empty())
1364 PP.setCounterValue(Record[0]);
1365 break;
1366
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001367 case pch::PP_MACRO_OBJECT_LIKE:
1368 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001369 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1370 if (II == 0)
1371 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001372 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1373 bool isUsed = Record[2];
1374
1375 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1376 MI->setIsUsed(isUsed);
1377
1378 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1379 // Decode function-like macro info.
1380 bool isC99VarArgs = Record[3];
1381 bool isGNUVarArgs = Record[4];
1382 MacroArgs.clear();
1383 unsigned NumArgs = Record[5];
1384 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001385 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001386
1387 // Install function-like macro info.
1388 MI->setIsFunctionLike();
1389 if (isC99VarArgs) MI->setIsC99Varargs();
1390 if (isGNUVarArgs) MI->setIsGNUVarargs();
1391 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1392 PP.getPreprocessorAllocator());
1393 }
1394
1395 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001396 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001397
1398 // Remember that we saw this macro last so that we add the tokens that
1399 // form its body to it.
1400 LastMacro = MI;
1401 break;
1402 }
1403
1404 case pch::PP_TOKEN: {
1405 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1406 // pretend we didn't see this.
1407 if (LastMacro == 0) break;
1408
1409 Token Tok;
1410 Tok.startToken();
1411 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1412 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001413 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1414 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001415 Tok.setKind((tok::TokenKind)Record[3]);
1416 Tok.setFlag((Token::TokenFlags)Record[4]);
1417 LastMacro->AddTokenToBody(Tok);
1418 break;
1419 }
1420 }
1421 }
1422}
1423
Douglas Gregorc713da92009-04-21 22:25:48 +00001424PCHReader::PCHReadResult
1425PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001426 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1427 Error("Malformed block record");
1428 return Failure;
1429 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001430
1431 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001432 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001433 while (!Stream.AtEndOfStream()) {
1434 unsigned Code = Stream.ReadCode();
1435 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001436 if (Stream.ReadBlockEnd()) {
1437 Error("Error at end of module block");
1438 return Failure;
1439 }
Chris Lattner29241862009-04-11 21:15:38 +00001440
Douglas Gregor179cfb12009-04-10 20:39:37 +00001441 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001442 }
1443
1444 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1445 switch (Stream.ReadSubBlockID()) {
1446 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1447 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1448 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001449 if (Stream.SkipBlock()) {
1450 Error("Malformed block record");
1451 return Failure;
1452 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001453 break;
1454
Chris Lattner29241862009-04-11 21:15:38 +00001455 case pch::PREPROCESSOR_BLOCK_ID:
1456 // Skip the preprocessor block for now, but remember where it is. We
1457 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001458 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001459 Error("Multiple preprocessor blocks found.");
1460 return Failure;
1461 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001462 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001463 if (Stream.SkipBlock()) {
1464 Error("Malformed block record");
1465 return Failure;
1466 }
1467 break;
1468
Douglas Gregorab1cef72009-04-10 03:52:48 +00001469 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001470 switch (ReadSourceManagerBlock()) {
1471 case Success:
1472 break;
1473
1474 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001475 Error("Malformed source manager block");
1476 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001477
1478 case IgnorePCH:
1479 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001480 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001481 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001482 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001483 continue;
1484 }
1485
1486 if (Code == llvm::bitc::DEFINE_ABBREV) {
1487 Stream.ReadAbbrevRecord();
1488 continue;
1489 }
1490
1491 // Read and process a record.
1492 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001493 const char *BlobStart = 0;
1494 unsigned BlobLen = 0;
1495 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1496 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001497 default: // Default behavior: ignore.
1498 break;
1499
1500 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001501 if (!TypeOffsets.empty()) {
1502 Error("Duplicate TYPE_OFFSET record in PCH file");
1503 return Failure;
1504 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001505 TypeOffsets.swap(Record);
1506 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1507 break;
1508
1509 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001510 if (!DeclOffsets.empty()) {
1511 Error("Duplicate DECL_OFFSET record in PCH file");
1512 return Failure;
1513 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001514 DeclOffsets.swap(Record);
1515 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1516 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001517
1518 case pch::LANGUAGE_OPTIONS:
1519 if (ParseLanguageOptions(Record))
1520 return IgnorePCH;
1521 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001522
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001523 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001524 std::string TargetTriple(BlobStart, BlobLen);
1525 if (TargetTriple != Context.Target.getTargetTriple()) {
1526 Diag(diag::warn_pch_target_triple)
1527 << TargetTriple << Context.Target.getTargetTriple();
1528 Diag(diag::note_ignoring_pch) << FileName;
1529 return IgnorePCH;
1530 }
1531 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001532 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001533
1534 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001535 IdentifierTableData = BlobStart;
1536 IdentifierLookupTable
1537 = PCHIdentifierLookupTable::Create(
1538 (const unsigned char *)IdentifierTableData + Record[0],
1539 (const unsigned char *)IdentifierTableData,
1540 PCHIdentifierLookupTrait(*this));
1541 // FIXME: What about any identifiers already placed into the
1542 // identifier table? Should we load decls with those names now?
1543 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001544 break;
1545
1546 case pch::IDENTIFIER_OFFSET:
1547 if (!IdentifierData.empty()) {
1548 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1549 return Failure;
1550 }
1551 IdentifierData.swap(Record);
1552#ifndef NDEBUG
1553 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1554 if ((IdentifierData[I] & 0x01) == 0) {
1555 Error("Malformed identifier table in the precompiled header");
1556 return Failure;
1557 }
1558 }
1559#endif
1560 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001561
1562 case pch::EXTERNAL_DEFINITIONS:
1563 if (!ExternalDefinitions.empty()) {
1564 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1565 return Failure;
1566 }
1567 ExternalDefinitions.swap(Record);
1568 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001569
Douglas Gregore01ad442009-04-18 05:55:16 +00001570 case pch::SPECIAL_TYPES:
1571 SpecialTypes.swap(Record);
1572 break;
1573
Douglas Gregor456e0952009-04-17 22:13:46 +00001574 case pch::STATISTICS:
1575 TotalNumStatements = Record[0];
1576 break;
1577
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001578 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001579 }
1580
Douglas Gregor179cfb12009-04-10 20:39:37 +00001581 Error("Premature end of bitstream");
1582 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001583}
1584
Douglas Gregorc713da92009-04-21 22:25:48 +00001585namespace {
1586 /// \brief Helper class that saves the current stream position and
1587 /// then restores it when destroyed.
1588 struct VISIBILITY_HIDDEN SavedStreamPosition {
1589 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
1590 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
1591
1592 ~SavedStreamPosition() {
1593 Stream.JumpToBit(Offset);
1594 }
1595
1596 private:
1597 llvm::BitstreamReader &Stream;
1598 uint64_t Offset;
1599 };
1600}
1601
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001602PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001603 // Set the PCH file name.
1604 this->FileName = FileName;
1605
Douglas Gregorc34897d2009-04-09 22:27:44 +00001606 // Open the PCH file.
1607 std::string ErrStr;
1608 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001609 if (!Buffer) {
1610 Error(ErrStr.c_str());
1611 return IgnorePCH;
1612 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001613
1614 // Initialize the stream
1615 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1616 (const unsigned char *)Buffer->getBufferEnd());
1617
1618 // Sniff for the signature.
1619 if (Stream.Read(8) != 'C' ||
1620 Stream.Read(8) != 'P' ||
1621 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001622 Stream.Read(8) != 'H') {
1623 Error("Not a PCH file");
1624 return IgnorePCH;
1625 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001626
1627 // We expect a number of well-defined blocks, though we don't necessarily
1628 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001629 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001630 while (!Stream.AtEndOfStream()) {
1631 unsigned Code = Stream.ReadCode();
1632
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001633 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1634 Error("Invalid record at top-level");
1635 return Failure;
1636 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001637
1638 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001639
Douglas Gregorc34897d2009-04-09 22:27:44 +00001640 // We only know the PCH subblock ID.
1641 switch (BlockID) {
1642 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001643 if (Stream.ReadBlockInfoBlock()) {
1644 Error("Malformed BlockInfoBlock");
1645 return Failure;
1646 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001647 break;
1648 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001649 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001650 case Success:
1651 break;
1652
1653 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001654 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001655
1656 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001657 // FIXME: We could consider reading through to the end of this
1658 // PCH block, skipping subblocks, to see if there are other
1659 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001660 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001661 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001662 break;
1663 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001664 if (Stream.SkipBlock()) {
1665 Error("Malformed block record");
1666 return Failure;
1667 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001668 break;
1669 }
1670 }
1671
1672 // Load the translation unit declaration
1673 ReadDeclRecord(DeclOffsets[0], 0);
1674
Douglas Gregorc713da92009-04-21 22:25:48 +00001675 // Initialization of builtins and library builtins occurs before the
1676 // PCH file is read, so there may be some identifiers that were
1677 // loaded into the IdentifierTable before we intercepted the
1678 // creation of identifiers. Iterate through the list of known
1679 // identifiers and determine whether we have to establish
1680 // preprocessor definitions or top-level identifier declaration
1681 // chains for those identifiers.
1682 //
1683 // We copy the IdentifierInfo pointers to a small vector first,
1684 // since de-serializing declarations or macro definitions can add
1685 // new entries into the identifier table, invalidating the
1686 // iterators.
1687 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1688 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1689 IdEnd = PP.getIdentifierTable().end();
1690 Id != IdEnd; ++Id)
1691 Identifiers.push_back(Id->second);
1692 PCHIdentifierLookupTable *IdTable
1693 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1694 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1695 IdentifierInfo *II = Identifiers[I];
1696 // Look in the on-disk hash table for an entry for
1697 PCHIdentifierLookupTrait Info(*this, II);
1698 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1699 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1700 if (Pos == IdTable->end())
1701 continue;
1702
1703 // Dereferencing the iterator has the effect of populating the
1704 // IdentifierInfo node with the various declarations it needs.
1705 (void)*Pos;
1706 }
1707
Douglas Gregore01ad442009-04-18 05:55:16 +00001708 // Load the special types.
1709 Context.setBuiltinVaListType(
1710 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1711
Douglas Gregorc713da92009-04-21 22:25:48 +00001712 // If we saw the preprocessor block, read it now.
1713 if (PreprocessorBlockOffset) {
1714 SavedStreamPosition SavedPos(Stream);
1715 Stream.JumpToBit(PreprocessorBlockOffset);
1716 if (ReadPreprocessorBlock()) {
1717 Error("Malformed preprocessor block");
1718 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001719 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001720 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001721
Douglas Gregorc713da92009-04-21 22:25:48 +00001722 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001723}
1724
Douglas Gregor179cfb12009-04-10 20:39:37 +00001725/// \brief Parse the record that corresponds to a LangOptions data
1726/// structure.
1727///
1728/// This routine compares the language options used to generate the
1729/// PCH file against the language options set for the current
1730/// compilation. For each option, we classify differences between the
1731/// two compiler states as either "benign" or "important". Benign
1732/// differences don't matter, and we accept them without complaint
1733/// (and without modifying the language options). Differences between
1734/// the states for important options cause the PCH file to be
1735/// unusable, so we emit a warning and return true to indicate that
1736/// there was an error.
1737///
1738/// \returns true if the PCH file is unacceptable, false otherwise.
1739bool PCHReader::ParseLanguageOptions(
1740 const llvm::SmallVectorImpl<uint64_t> &Record) {
1741 const LangOptions &LangOpts = Context.getLangOptions();
1742#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1743#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1744 if (Record[Idx] != LangOpts.Option) { \
1745 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1746 Diag(diag::note_ignoring_pch) << FileName; \
1747 return true; \
1748 } \
1749 ++Idx
1750
1751 unsigned Idx = 0;
1752 PARSE_LANGOPT_BENIGN(Trigraphs);
1753 PARSE_LANGOPT_BENIGN(BCPLComment);
1754 PARSE_LANGOPT_BENIGN(DollarIdents);
1755 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1756 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1757 PARSE_LANGOPT_BENIGN(ImplicitInt);
1758 PARSE_LANGOPT_BENIGN(Digraphs);
1759 PARSE_LANGOPT_BENIGN(HexFloats);
1760 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1761 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1762 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1763 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1764 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1765 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1766 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1767 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1768 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1769 PARSE_LANGOPT_BENIGN(PascalStrings);
1770 PARSE_LANGOPT_BENIGN(Boolean);
1771 PARSE_LANGOPT_BENIGN(WritableStrings);
1772 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1773 diag::warn_pch_lax_vector_conversions);
1774 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1775 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1776 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1777 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1778 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1779 diag::warn_pch_thread_safe_statics);
1780 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1781 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1782 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1783 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1784 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1785 diag::warn_pch_heinous_extensions);
1786 // FIXME: Most of the options below are benign if the macro wasn't
1787 // used. Unfortunately, this means that a PCH compiled without
1788 // optimization can't be used with optimization turned on, even
1789 // though the only thing that changes is whether __OPTIMIZE__ was
1790 // defined... but if __OPTIMIZE__ never showed up in the header, it
1791 // doesn't matter. We could consider making this some special kind
1792 // of check.
1793 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1794 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1795 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1796 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1797 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1798 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1799 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1800 Diag(diag::warn_pch_gc_mode)
1801 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1802 Diag(diag::note_ignoring_pch) << FileName;
1803 return true;
1804 }
1805 ++Idx;
1806 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1807 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1808#undef PARSE_LANGOPT_IRRELEVANT
1809#undef PARSE_LANGOPT_BENIGN
1810
1811 return false;
1812}
1813
Douglas Gregorc34897d2009-04-09 22:27:44 +00001814/// \brief Read and return the type at the given offset.
1815///
1816/// This routine actually reads the record corresponding to the type
1817/// at the given offset in the bitstream. It is a helper routine for
1818/// GetType, which deals with reading type IDs.
1819QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001820 // Keep track of where we are in the stream, then jump back there
1821 // after reading this type.
1822 SavedStreamPosition SavedPosition(Stream);
1823
Douglas Gregorc34897d2009-04-09 22:27:44 +00001824 Stream.JumpToBit(Offset);
1825 RecordData Record;
1826 unsigned Code = Stream.ReadCode();
1827 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001828 case pch::TYPE_EXT_QUAL: {
1829 assert(Record.size() == 3 &&
1830 "Incorrect encoding of extended qualifier type");
1831 QualType Base = GetType(Record[0]);
1832 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1833 unsigned AddressSpace = Record[2];
1834
1835 QualType T = Base;
1836 if (GCAttr != QualType::GCNone)
1837 T = Context.getObjCGCQualType(T, GCAttr);
1838 if (AddressSpace)
1839 T = Context.getAddrSpaceQualType(T, AddressSpace);
1840 return T;
1841 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001842
Douglas Gregorc34897d2009-04-09 22:27:44 +00001843 case pch::TYPE_FIXED_WIDTH_INT: {
1844 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1845 return Context.getFixedWidthIntType(Record[0], Record[1]);
1846 }
1847
1848 case pch::TYPE_COMPLEX: {
1849 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1850 QualType ElemType = GetType(Record[0]);
1851 return Context.getComplexType(ElemType);
1852 }
1853
1854 case pch::TYPE_POINTER: {
1855 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1856 QualType PointeeType = GetType(Record[0]);
1857 return Context.getPointerType(PointeeType);
1858 }
1859
1860 case pch::TYPE_BLOCK_POINTER: {
1861 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1862 QualType PointeeType = GetType(Record[0]);
1863 return Context.getBlockPointerType(PointeeType);
1864 }
1865
1866 case pch::TYPE_LVALUE_REFERENCE: {
1867 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1868 QualType PointeeType = GetType(Record[0]);
1869 return Context.getLValueReferenceType(PointeeType);
1870 }
1871
1872 case pch::TYPE_RVALUE_REFERENCE: {
1873 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1874 QualType PointeeType = GetType(Record[0]);
1875 return Context.getRValueReferenceType(PointeeType);
1876 }
1877
1878 case pch::TYPE_MEMBER_POINTER: {
1879 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1880 QualType PointeeType = GetType(Record[0]);
1881 QualType ClassType = GetType(Record[1]);
1882 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1883 }
1884
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001885 case pch::TYPE_CONSTANT_ARRAY: {
1886 QualType ElementType = GetType(Record[0]);
1887 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1888 unsigned IndexTypeQuals = Record[2];
1889 unsigned Idx = 3;
1890 llvm::APInt Size = ReadAPInt(Record, Idx);
1891 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1892 }
1893
1894 case pch::TYPE_INCOMPLETE_ARRAY: {
1895 QualType ElementType = GetType(Record[0]);
1896 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1897 unsigned IndexTypeQuals = Record[2];
1898 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1899 }
1900
1901 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001902 QualType ElementType = GetType(Record[0]);
1903 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1904 unsigned IndexTypeQuals = Record[2];
1905 return Context.getVariableArrayType(ElementType, ReadExpr(),
1906 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001907 }
1908
1909 case pch::TYPE_VECTOR: {
1910 if (Record.size() != 2) {
1911 Error("Incorrect encoding of vector type in PCH file");
1912 return QualType();
1913 }
1914
1915 QualType ElementType = GetType(Record[0]);
1916 unsigned NumElements = Record[1];
1917 return Context.getVectorType(ElementType, NumElements);
1918 }
1919
1920 case pch::TYPE_EXT_VECTOR: {
1921 if (Record.size() != 2) {
1922 Error("Incorrect encoding of extended vector type in PCH file");
1923 return QualType();
1924 }
1925
1926 QualType ElementType = GetType(Record[0]);
1927 unsigned NumElements = Record[1];
1928 return Context.getExtVectorType(ElementType, NumElements);
1929 }
1930
1931 case pch::TYPE_FUNCTION_NO_PROTO: {
1932 if (Record.size() != 1) {
1933 Error("Incorrect encoding of no-proto function type");
1934 return QualType();
1935 }
1936 QualType ResultType = GetType(Record[0]);
1937 return Context.getFunctionNoProtoType(ResultType);
1938 }
1939
1940 case pch::TYPE_FUNCTION_PROTO: {
1941 QualType ResultType = GetType(Record[0]);
1942 unsigned Idx = 1;
1943 unsigned NumParams = Record[Idx++];
1944 llvm::SmallVector<QualType, 16> ParamTypes;
1945 for (unsigned I = 0; I != NumParams; ++I)
1946 ParamTypes.push_back(GetType(Record[Idx++]));
1947 bool isVariadic = Record[Idx++];
1948 unsigned Quals = Record[Idx++];
1949 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1950 isVariadic, Quals);
1951 }
1952
1953 case pch::TYPE_TYPEDEF:
1954 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1955 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1956
1957 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001958 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001959
1960 case pch::TYPE_TYPEOF: {
1961 if (Record.size() != 1) {
1962 Error("Incorrect encoding of typeof(type) in PCH file");
1963 return QualType();
1964 }
1965 QualType UnderlyingType = GetType(Record[0]);
1966 return Context.getTypeOfType(UnderlyingType);
1967 }
1968
1969 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001970 assert(Record.size() == 1 && "Incorrect encoding of record type");
1971 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001972
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001973 case pch::TYPE_ENUM:
1974 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1975 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1976
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001977 case pch::TYPE_OBJC_INTERFACE:
1978 // FIXME: Deserialize ObjCInterfaceType
1979 assert(false && "Cannot de-serialize ObjC interface types yet");
1980 return QualType();
1981
1982 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1983 // FIXME: Deserialize ObjCQualifiedInterfaceType
1984 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1985 return QualType();
1986
1987 case pch::TYPE_OBJC_QUALIFIED_ID:
1988 // FIXME: Deserialize ObjCQualifiedIdType
1989 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1990 return QualType();
1991
1992 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1993 // FIXME: Deserialize ObjCQualifiedClassType
1994 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1995 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001996 }
1997
1998 // Suppress a GCC warning
1999 return QualType();
2000}
2001
2002/// \brief Note that we have loaded the declaration with the given
2003/// Index.
2004///
2005/// This routine notes that this declaration has already been loaded,
2006/// so that future GetDecl calls will return this declaration rather
2007/// than trying to load a new declaration.
2008inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2009 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2010 DeclAlreadyLoaded[Index] = true;
2011 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2012}
2013
2014/// \brief Read the declaration at the given offset from the PCH file.
2015Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002016 // Keep track of where we are in the stream, then jump back there
2017 // after reading this declaration.
2018 SavedStreamPosition SavedPosition(Stream);
2019
Douglas Gregorc34897d2009-04-09 22:27:44 +00002020 Decl *D = 0;
2021 Stream.JumpToBit(Offset);
2022 RecordData Record;
2023 unsigned Code = Stream.ReadCode();
2024 unsigned Idx = 0;
2025 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002026
Douglas Gregorc34897d2009-04-09 22:27:44 +00002027 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002028 case pch::DECL_ATTR:
2029 case pch::DECL_CONTEXT_LEXICAL:
2030 case pch::DECL_CONTEXT_VISIBLE:
2031 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2032 break;
2033
Douglas Gregorc34897d2009-04-09 22:27:44 +00002034 case pch::DECL_TRANSLATION_UNIT:
2035 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002036 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002037 break;
2038
2039 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002040 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002041 break;
2042 }
2043
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002044 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002045 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002046 break;
2047 }
2048
Douglas Gregor982365e2009-04-13 21:20:57 +00002049 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002050 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2051 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002052 break;
2053 }
2054
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002055 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002056 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2057 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002058 break;
2059 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002060
2061 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002062 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2063 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002064 break;
2065 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002066
Steve Naroff79ea0e02009-04-20 15:06:07 +00002067 case pch::DECL_OBJC_METHOD: {
2068 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2069 Selector(), QualType(), 0);
2070 break;
2071 }
2072
Steve Naroff97b53bd2009-04-21 15:12:33 +00002073 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002074 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2075 break;
2076 }
2077
Steve Naroff97b53bd2009-04-21 15:12:33 +00002078 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002079 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2080 ObjCIvarDecl::None);
2081 break;
2082 }
2083
Steve Naroff97b53bd2009-04-21 15:12:33 +00002084 case pch::DECL_OBJC_PROTOCOL: {
2085 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2086 break;
2087 }
2088
2089 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2090 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2091 QualType(), 0);
2092 break;
2093 }
2094
2095 case pch::DECL_OBJC_CLASS: {
2096 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2097 break;
2098 }
2099
2100 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2101 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2102 break;
2103 }
2104
2105 case pch::DECL_OBJC_CATEGORY: {
2106 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2107 break;
2108 }
2109
2110 case pch::DECL_OBJC_CATEGORY_IMPL: {
2111 // FIXME: Implement.
2112 break;
2113 }
2114
2115 case pch::DECL_OBJC_IMPLEMENTATION: {
2116 // FIXME: Implement.
2117 break;
2118 }
2119
2120 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2121 // FIXME: Implement.
2122 break;
2123 }
2124
2125 case pch::DECL_OBJC_PROPERTY: {
2126 // FIXME: Implement.
2127 break;
2128 }
2129
2130 case pch::DECL_OBJC_PROPERTY_IMPL: {
2131 // FIXME: Implement.
2132 break;
2133 }
2134
Douglas Gregor982365e2009-04-13 21:20:57 +00002135 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002136 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2137 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002138 break;
2139 }
2140
Douglas Gregorc34897d2009-04-09 22:27:44 +00002141 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002142 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2143 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002144 break;
2145 }
2146
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002147 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002148 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2149 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002150 break;
2151 }
2152
2153 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002154 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002155 QualType(), QualType(), VarDecl::None,
2156 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002157 break;
2158 }
2159
Douglas Gregor2a491792009-04-13 22:49:25 +00002160 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002161 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002162 break;
2163 }
2164
2165 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002166 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002167 break;
2168 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002169 }
2170
Douglas Gregorc713da92009-04-21 22:25:48 +00002171 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002172 if (D) {
2173 LoadedDecl(Index, D);
2174 Reader.Visit(D);
2175 }
2176
Douglas Gregorc34897d2009-04-09 22:27:44 +00002177 // If this declaration is also a declaration context, get the
2178 // offsets for its tables of lexical and visible declarations.
2179 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2180 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2181 if (Offsets.first || Offsets.second) {
2182 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2183 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2184 DeclContextOffsets[DC] = Offsets;
2185 }
2186 }
2187 assert(Idx == Record.size());
2188
2189 return D;
2190}
2191
Douglas Gregorac8f2802009-04-10 17:25:41 +00002192QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002193 unsigned Quals = ID & 0x07;
2194 unsigned Index = ID >> 3;
2195
2196 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2197 QualType T;
2198 switch ((pch::PredefinedTypeIDs)Index) {
2199 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2200 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2201 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2202
2203 case pch::PREDEF_TYPE_CHAR_U_ID:
2204 case pch::PREDEF_TYPE_CHAR_S_ID:
2205 // FIXME: Check that the signedness of CharTy is correct!
2206 T = Context.CharTy;
2207 break;
2208
2209 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2210 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2211 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2212 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2213 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2214 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2215 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2216 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2217 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2218 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2219 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2220 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2221 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2222 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2223 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2224 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2225 }
2226
2227 assert(!T.isNull() && "Unknown predefined type");
2228 return T.getQualifiedType(Quals);
2229 }
2230
2231 Index -= pch::NUM_PREDEF_TYPE_IDS;
2232 if (!TypeAlreadyLoaded[Index]) {
2233 // Load the type from the PCH file.
2234 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2235 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2236 TypeAlreadyLoaded[Index] = true;
2237 }
2238
2239 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2240}
2241
Douglas Gregorac8f2802009-04-10 17:25:41 +00002242Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002243 if (ID == 0)
2244 return 0;
2245
2246 unsigned Index = ID - 1;
2247 if (DeclAlreadyLoaded[Index])
2248 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2249
2250 // Load the declaration from the PCH file.
2251 return ReadDeclRecord(DeclOffsets[Index], Index);
2252}
2253
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002254Stmt *PCHReader::GetStmt(uint64_t Offset) {
2255 // Keep track of where we are in the stream, then jump back there
2256 // after reading this declaration.
2257 SavedStreamPosition SavedPosition(Stream);
2258
2259 Stream.JumpToBit(Offset);
2260 return ReadStmt();
2261}
2262
Douglas Gregorc34897d2009-04-09 22:27:44 +00002263bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002264 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002265 assert(DC->hasExternalLexicalStorage() &&
2266 "DeclContext has no lexical decls in storage");
2267 uint64_t Offset = DeclContextOffsets[DC].first;
2268 assert(Offset && "DeclContext has no lexical decls in storage");
2269
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002270 // Keep track of where we are in the stream, then jump back there
2271 // after reading this context.
2272 SavedStreamPosition SavedPosition(Stream);
2273
Douglas Gregorc34897d2009-04-09 22:27:44 +00002274 // Load the record containing all of the declarations lexically in
2275 // this context.
2276 Stream.JumpToBit(Offset);
2277 RecordData Record;
2278 unsigned Code = Stream.ReadCode();
2279 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002280 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002281 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2282
2283 // Load all of the declaration IDs
2284 Decls.clear();
2285 Decls.insert(Decls.end(), Record.begin(), Record.end());
2286 return false;
2287}
2288
2289bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2290 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2291 assert(DC->hasExternalVisibleStorage() &&
2292 "DeclContext has no visible decls in storage");
2293 uint64_t Offset = DeclContextOffsets[DC].second;
2294 assert(Offset && "DeclContext has no visible decls in storage");
2295
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002296 // Keep track of where we are in the stream, then jump back there
2297 // after reading this context.
2298 SavedStreamPosition SavedPosition(Stream);
2299
Douglas Gregorc34897d2009-04-09 22:27:44 +00002300 // Load the record containing all of the declarations visible in
2301 // this context.
2302 Stream.JumpToBit(Offset);
2303 RecordData Record;
2304 unsigned Code = Stream.ReadCode();
2305 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002306 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002307 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2308 if (Record.size() == 0)
2309 return false;
2310
2311 Decls.clear();
2312
2313 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002314 while (Idx < Record.size()) {
2315 Decls.push_back(VisibleDeclaration());
2316 Decls.back().Name = ReadDeclarationName(Record, Idx);
2317
Douglas Gregorc34897d2009-04-09 22:27:44 +00002318 unsigned Size = Record[Idx++];
2319 llvm::SmallVector<unsigned, 4> & LoadedDecls
2320 = Decls.back().Declarations;
2321 LoadedDecls.reserve(Size);
2322 for (unsigned I = 0; I < Size; ++I)
2323 LoadedDecls.push_back(Record[Idx++]);
2324 }
2325
2326 return false;
2327}
2328
Douglas Gregor631f6c62009-04-14 00:24:19 +00002329void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2330 if (!Consumer)
2331 return;
2332
2333 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2334 Decl *D = GetDecl(ExternalDefinitions[I]);
2335 DeclGroupRef DG(D);
2336 Consumer->HandleTopLevelDecl(DG);
2337 }
2338}
2339
Douglas Gregorc34897d2009-04-09 22:27:44 +00002340void PCHReader::PrintStats() {
2341 std::fprintf(stderr, "*** PCH Statistics:\n");
2342
2343 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2344 TypeAlreadyLoaded.end(),
2345 true);
2346 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2347 DeclAlreadyLoaded.end(),
2348 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002349 unsigned NumIdentifiersLoaded = 0;
2350 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2351 if ((IdentifierData[I] & 0x01) == 0)
2352 ++NumIdentifiersLoaded;
2353 }
2354
Douglas Gregorc34897d2009-04-09 22:27:44 +00002355 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2356 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002357 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002358 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2359 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002360 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2361 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2362 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2363 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002364 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2365 NumStatementsRead, TotalNumStatements,
2366 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002367 std::fprintf(stderr, "\n");
2368}
2369
Douglas Gregorc713da92009-04-21 22:25:48 +00002370void PCHReader::InitializeSema(Sema &S) {
2371 SemaObj = &S;
2372
2373 // FIXME: this makes sure any declarations that were deserialized
2374 // "too early" still get added to the identifier's declaration
2375 // chains.
2376 for (unsigned I = 0, N = TUDecls.size(); I != N; ++I) {
2377 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(TUDecls[I]));
2378 SemaObj->IdResolver.AddDecl(TUDecls[I]);
2379 }
2380 TUDecls.clear();
2381}
2382
2383IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2384 // Try to find this name within our on-disk hash table
2385 PCHIdentifierLookupTable *IdTable
2386 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2387 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2388 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2389 if (Pos == IdTable->end())
2390 return 0;
2391
2392 // Dereferencing the iterator has the effect of building the
2393 // IdentifierInfo node and populating it with the various
2394 // declarations it needs.
2395 return *Pos;
2396}
2397
2398void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2399 assert(ID && "Non-zero identifier ID required");
2400 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2401}
2402
Chris Lattner29241862009-04-11 21:15:38 +00002403IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002404 if (ID == 0)
2405 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002406
Douglas Gregorc713da92009-04-21 22:25:48 +00002407 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002408 Error("No identifier table in PCH file");
2409 return 0;
2410 }
Chris Lattner29241862009-04-11 21:15:38 +00002411
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002412 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002413 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002414 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002415 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002416 }
Chris Lattner29241862009-04-11 21:15:38 +00002417
2418 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002419}
2420
2421DeclarationName
2422PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2423 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2424 switch (Kind) {
2425 case DeclarationName::Identifier:
2426 return DeclarationName(GetIdentifierInfo(Record, Idx));
2427
2428 case DeclarationName::ObjCZeroArgSelector:
2429 case DeclarationName::ObjCOneArgSelector:
2430 case DeclarationName::ObjCMultiArgSelector:
2431 assert(false && "Unable to de-serialize Objective-C selectors");
2432 break;
2433
2434 case DeclarationName::CXXConstructorName:
2435 return Context.DeclarationNames.getCXXConstructorName(
2436 GetType(Record[Idx++]));
2437
2438 case DeclarationName::CXXDestructorName:
2439 return Context.DeclarationNames.getCXXDestructorName(
2440 GetType(Record[Idx++]));
2441
2442 case DeclarationName::CXXConversionFunctionName:
2443 return Context.DeclarationNames.getCXXConversionFunctionName(
2444 GetType(Record[Idx++]));
2445
2446 case DeclarationName::CXXOperatorName:
2447 return Context.DeclarationNames.getCXXOperatorName(
2448 (OverloadedOperatorKind)Record[Idx++]);
2449
2450 case DeclarationName::CXXUsingDirective:
2451 return DeclarationName::getUsingDirectiveName();
2452 }
2453
2454 // Required to silence GCC warning
2455 return DeclarationName();
2456}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002457
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002458/// \brief Read an integral value
2459llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2460 unsigned BitWidth = Record[Idx++];
2461 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2462 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2463 Idx += NumWords;
2464 return Result;
2465}
2466
2467/// \brief Read a signed integral value
2468llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2469 bool isUnsigned = Record[Idx++];
2470 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2471}
2472
Douglas Gregore2f37202009-04-14 21:55:33 +00002473/// \brief Read a floating-point value
2474llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002475 return llvm::APFloat(ReadAPInt(Record, Idx));
2476}
2477
Douglas Gregor1c507882009-04-15 21:30:51 +00002478// \brief Read a string
2479std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2480 unsigned Len = Record[Idx++];
2481 std::string Result(&Record[Idx], &Record[Idx] + Len);
2482 Idx += Len;
2483 return Result;
2484}
2485
2486/// \brief Reads attributes from the current stream position.
2487Attr *PCHReader::ReadAttributes() {
2488 unsigned Code = Stream.ReadCode();
2489 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2490 "Expected unabbreviated record"); (void)Code;
2491
2492 RecordData Record;
2493 unsigned Idx = 0;
2494 unsigned RecCode = Stream.ReadRecord(Code, Record);
2495 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2496 (void)RecCode;
2497
2498#define SIMPLE_ATTR(Name) \
2499 case Attr::Name: \
2500 New = ::new (Context) Name##Attr(); \
2501 break
2502
2503#define STRING_ATTR(Name) \
2504 case Attr::Name: \
2505 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2506 break
2507
2508#define UNSIGNED_ATTR(Name) \
2509 case Attr::Name: \
2510 New = ::new (Context) Name##Attr(Record[Idx++]); \
2511 break
2512
2513 Attr *Attrs = 0;
2514 while (Idx < Record.size()) {
2515 Attr *New = 0;
2516 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2517 bool IsInherited = Record[Idx++];
2518
2519 switch (Kind) {
2520 STRING_ATTR(Alias);
2521 UNSIGNED_ATTR(Aligned);
2522 SIMPLE_ATTR(AlwaysInline);
2523 SIMPLE_ATTR(AnalyzerNoReturn);
2524 STRING_ATTR(Annotate);
2525 STRING_ATTR(AsmLabel);
2526
2527 case Attr::Blocks:
2528 New = ::new (Context) BlocksAttr(
2529 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2530 break;
2531
2532 case Attr::Cleanup:
2533 New = ::new (Context) CleanupAttr(
2534 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2535 break;
2536
2537 SIMPLE_ATTR(Const);
2538 UNSIGNED_ATTR(Constructor);
2539 SIMPLE_ATTR(DLLExport);
2540 SIMPLE_ATTR(DLLImport);
2541 SIMPLE_ATTR(Deprecated);
2542 UNSIGNED_ATTR(Destructor);
2543 SIMPLE_ATTR(FastCall);
2544
2545 case Attr::Format: {
2546 std::string Type = ReadString(Record, Idx);
2547 unsigned FormatIdx = Record[Idx++];
2548 unsigned FirstArg = Record[Idx++];
2549 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2550 break;
2551 }
2552
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002553 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002554
2555 case Attr::IBOutletKind:
2556 New = ::new (Context) IBOutletAttr();
2557 break;
2558
2559 SIMPLE_ATTR(NoReturn);
2560 SIMPLE_ATTR(NoThrow);
2561 SIMPLE_ATTR(Nodebug);
2562 SIMPLE_ATTR(Noinline);
2563
2564 case Attr::NonNull: {
2565 unsigned Size = Record[Idx++];
2566 llvm::SmallVector<unsigned, 16> ArgNums;
2567 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2568 Idx += Size;
2569 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2570 break;
2571 }
2572
2573 SIMPLE_ATTR(ObjCException);
2574 SIMPLE_ATTR(ObjCNSObject);
2575 SIMPLE_ATTR(Overloadable);
2576 UNSIGNED_ATTR(Packed);
2577 SIMPLE_ATTR(Pure);
2578 UNSIGNED_ATTR(Regparm);
2579 STRING_ATTR(Section);
2580 SIMPLE_ATTR(StdCall);
2581 SIMPLE_ATTR(TransparentUnion);
2582 SIMPLE_ATTR(Unavailable);
2583 SIMPLE_ATTR(Unused);
2584 SIMPLE_ATTR(Used);
2585
2586 case Attr::Visibility:
2587 New = ::new (Context) VisibilityAttr(
2588 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2589 break;
2590
2591 SIMPLE_ATTR(WarnUnusedResult);
2592 SIMPLE_ATTR(Weak);
2593 SIMPLE_ATTR(WeakImport);
2594 }
2595
2596 assert(New && "Unable to decode attribute?");
2597 New->setInherited(IsInherited);
2598 New->setNext(Attrs);
2599 Attrs = New;
2600 }
2601#undef UNSIGNED_ATTR
2602#undef STRING_ATTR
2603#undef SIMPLE_ATTR
2604
2605 // The list of attributes was built backwards. Reverse the list
2606 // before returning it.
2607 Attr *PrevAttr = 0, *NextAttr = 0;
2608 while (Attrs) {
2609 NextAttr = Attrs->getNext();
2610 Attrs->setNext(PrevAttr);
2611 PrevAttr = Attrs;
2612 Attrs = NextAttr;
2613 }
2614
2615 return PrevAttr;
2616}
2617
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002618Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002619 // Within the bitstream, expressions are stored in Reverse Polish
2620 // Notation, with each of the subexpressions preceding the
2621 // expression they are stored in. To evaluate expressions, we
2622 // continue reading expressions and placing them on the stack, with
2623 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002624 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002625 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002626 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002627 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002628 llvm::SmallVector<Stmt *, 16> StmtStack;
2629 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002630 Stmt::EmptyShell Empty;
2631
Douglas Gregora151ba42009-04-14 23:32:43 +00002632 while (true) {
2633 unsigned Code = Stream.ReadCode();
2634 if (Code == llvm::bitc::END_BLOCK) {
2635 if (Stream.ReadBlockEnd()) {
2636 Error("Error at end of Source Manager block");
2637 return 0;
2638 }
2639 break;
2640 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002641
Douglas Gregora151ba42009-04-14 23:32:43 +00002642 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2643 // No known subblocks, always skip them.
2644 Stream.ReadSubBlockID();
2645 if (Stream.SkipBlock()) {
2646 Error("Malformed block record");
2647 return 0;
2648 }
2649 continue;
2650 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002651
Douglas Gregora151ba42009-04-14 23:32:43 +00002652 if (Code == llvm::bitc::DEFINE_ABBREV) {
2653 Stream.ReadAbbrevRecord();
2654 continue;
2655 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002656
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002657 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002658 Idx = 0;
2659 Record.clear();
2660 bool Finished = false;
2661 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002662 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002663 Finished = true;
2664 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002665
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002666 case pch::STMT_NULL_PTR:
2667 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002668 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002669
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002670 case pch::STMT_NULL:
2671 S = new (Context) NullStmt(Empty);
2672 break;
2673
2674 case pch::STMT_COMPOUND:
2675 S = new (Context) CompoundStmt(Empty);
2676 break;
2677
2678 case pch::STMT_CASE:
2679 S = new (Context) CaseStmt(Empty);
2680 break;
2681
2682 case pch::STMT_DEFAULT:
2683 S = new (Context) DefaultStmt(Empty);
2684 break;
2685
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002686 case pch::STMT_LABEL:
2687 S = new (Context) LabelStmt(Empty);
2688 break;
2689
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002690 case pch::STMT_IF:
2691 S = new (Context) IfStmt(Empty);
2692 break;
2693
2694 case pch::STMT_SWITCH:
2695 S = new (Context) SwitchStmt(Empty);
2696 break;
2697
Douglas Gregora6b503f2009-04-17 00:16:09 +00002698 case pch::STMT_WHILE:
2699 S = new (Context) WhileStmt(Empty);
2700 break;
2701
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002702 case pch::STMT_DO:
2703 S = new (Context) DoStmt(Empty);
2704 break;
2705
2706 case pch::STMT_FOR:
2707 S = new (Context) ForStmt(Empty);
2708 break;
2709
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002710 case pch::STMT_GOTO:
2711 S = new (Context) GotoStmt(Empty);
2712 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002713
2714 case pch::STMT_INDIRECT_GOTO:
2715 S = new (Context) IndirectGotoStmt(Empty);
2716 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002717
Douglas Gregora6b503f2009-04-17 00:16:09 +00002718 case pch::STMT_CONTINUE:
2719 S = new (Context) ContinueStmt(Empty);
2720 break;
2721
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002722 case pch::STMT_BREAK:
2723 S = new (Context) BreakStmt(Empty);
2724 break;
2725
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002726 case pch::STMT_RETURN:
2727 S = new (Context) ReturnStmt(Empty);
2728 break;
2729
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002730 case pch::STMT_DECL:
2731 S = new (Context) DeclStmt(Empty);
2732 break;
2733
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002734 case pch::STMT_ASM:
2735 S = new (Context) AsmStmt(Empty);
2736 break;
2737
Douglas Gregora151ba42009-04-14 23:32:43 +00002738 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002739 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002740 break;
2741
2742 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002743 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002744 break;
2745
2746 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002747 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002748 break;
2749
2750 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002751 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002752 break;
2753
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002754 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002755 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002756 break;
2757
Douglas Gregor596e0932009-04-15 16:35:07 +00002758 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002759 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002760 Record[PCHStmtReader::NumExprFields + 1]);
2761 break;
2762
Douglas Gregora151ba42009-04-14 23:32:43 +00002763 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002764 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002765 break;
2766
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002767 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002768 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002769 break;
2770
Douglas Gregor12d74052009-04-15 15:58:59 +00002771 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002772 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002773 break;
2774
2775 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002776 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002777 break;
2778
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002779 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002780 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002781 break;
2782
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002783 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002784 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002785 break;
2786
2787 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002788 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002789 break;
2790
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002791 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002792 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002793 break;
2794
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002795 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002796 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002797 break;
2798
2799 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002800 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002801 break;
2802
Douglas Gregora151ba42009-04-14 23:32:43 +00002803 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002804 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002805 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002806
2807 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002808 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002809 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002810
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002811 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002812 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002813 break;
2814
Douglas Gregorec0b8292009-04-15 23:02:49 +00002815 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002816 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002817 break;
2818
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002819 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002820 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002821 break;
2822
2823 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002824 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002825 Record[PCHStmtReader::NumExprFields] - 1);
2826
2827 break;
2828
2829 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002830 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002831 break;
2832
Douglas Gregorec0b8292009-04-15 23:02:49 +00002833 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002834 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002835 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002836
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002837 case pch::EXPR_ADDR_LABEL:
2838 S = new (Context) AddrLabelExpr(Empty);
2839 break;
2840
Douglas Gregoreca12f62009-04-17 19:05:30 +00002841 case pch::EXPR_STMT:
2842 S = new (Context) StmtExpr(Empty);
2843 break;
2844
Douglas Gregor209d4622009-04-15 23:33:31 +00002845 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002846 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002847 break;
2848
2849 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002850 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002851 break;
2852
2853 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002854 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002855 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002856
2857 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002858 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002859 break;
2860
Douglas Gregore246b742009-04-17 19:21:43 +00002861 case pch::EXPR_BLOCK:
2862 S = new (Context) BlockExpr(Empty);
2863 break;
2864
Douglas Gregor725e94b2009-04-16 00:01:45 +00002865 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002866 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002867 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002868 }
2869
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002870 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002871 if (Finished)
2872 break;
2873
Douglas Gregor456e0952009-04-17 22:13:46 +00002874 ++NumStatementsRead;
2875
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002876 if (S) {
2877 unsigned NumSubStmts = Reader.Visit(S);
2878 while (NumSubStmts > 0) {
2879 StmtStack.pop_back();
2880 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002881 }
2882 }
2883
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002884 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002885 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002886 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002887 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002888 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002889 return StmtStack.back();
2890}
2891
2892Expr *PCHReader::ReadExpr() {
2893 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002894}
2895
Douglas Gregor179cfb12009-04-10 20:39:37 +00002896DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002897 return Diag(SourceLocation(), DiagID);
2898}
2899
2900DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2901 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002902 Context.getSourceManager()),
2903 DiagID);
2904}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002905
Douglas Gregorc713da92009-04-21 22:25:48 +00002906/// \brief Retrieve the identifier table associated with the
2907/// preprocessor.
2908IdentifierTable &PCHReader::getIdentifierTable() {
2909 return PP.getIdentifierTable();
2910}
2911
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002912/// \brief Record that the given ID maps to the given switch-case
2913/// statement.
2914void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2915 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2916 SwitchCaseStmts[ID] = SC;
2917}
2918
2919/// \brief Retrieve the switch-case statement with the given ID.
2920SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2921 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2922 return SwitchCaseStmts[ID];
2923}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002924
2925/// \brief Record that the given label statement has been
2926/// deserialized and has the given ID.
2927void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2928 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2929 "Deserialized label twice");
2930 LabelStmts[ID] = S;
2931
2932 // If we've already seen any goto statements that point to this
2933 // label, resolve them now.
2934 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2935 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2936 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2937 Goto->second->setLabel(S);
2938 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002939
2940 // If we've already seen any address-label statements that point to
2941 // this label, resolve them now.
2942 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2943 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2944 = UnresolvedAddrLabelExprs.equal_range(ID);
2945 for (AddrLabelIter AddrLabel = AddrLabels.first;
2946 AddrLabel != AddrLabels.second; ++AddrLabel)
2947 AddrLabel->second->setLabel(S);
2948 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002949}
2950
2951/// \brief Set the label of the given statement to the label
2952/// identified by ID.
2953///
2954/// Depending on the order in which the label and other statements
2955/// referencing that label occur, this operation may complete
2956/// immediately (updating the statement) or it may queue the
2957/// statement to be back-patched later.
2958void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2959 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2960 if (Label != LabelStmts.end()) {
2961 // We've already seen this label, so set the label of the goto and
2962 // we're done.
2963 S->setLabel(Label->second);
2964 } else {
2965 // We haven't seen this label yet, so add this goto to the set of
2966 // unresolved goto statements.
2967 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2968 }
2969}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002970
2971/// \brief Set the label of the given expression to the label
2972/// identified by ID.
2973///
2974/// Depending on the order in which the label and other statements
2975/// referencing that label occur, this operation may complete
2976/// immediately (updating the statement) or it may queue the
2977/// statement to be back-patched later.
2978void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2979 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2980 if (Label != LabelStmts.end()) {
2981 // We've already seen this label, so set the label of the
2982 // label-address expression and we're done.
2983 S->setLabel(Label->second);
2984 } else {
2985 // We haven't seen this label yet, so add this label-address
2986 // expression to the set of unresolved label-address expressions.
2987 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2988 }
2989}