blob: 6a6cd641202f92ce01f18753207976f0a7f0bd29 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000039namespace {
40 /// \brief Helper class that saves the current stream position and
41 /// then restores it when destroyed.
42 struct VISIBILITY_HIDDEN SavedStreamPosition {
43 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
44 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
45
46 ~SavedStreamPosition() {
47 Stream.JumpToBit(Offset);
48 }
49
50 private:
51 llvm::BitstreamReader &Stream;
52 uint64_t Offset;
53 };
54}
55
Douglas Gregorc34897d2009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 PCHReader &Reader;
63 const PCHReader::RecordData &Record;
64 unsigned &Idx;
65
66 public:
67 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
68 unsigned &Idx)
69 : Reader(Reader), Record(Record), Idx(Idx) { }
70
71 void VisitDecl(Decl *D);
72 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
73 void VisitNamedDecl(NamedDecl *ND);
74 void VisitTypeDecl(TypeDecl *TD);
75 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000093 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
94 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
95 void VisitObjCClassDecl(ObjCClassDecl *D);
96 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
97 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
98 void VisitObjCImplDecl(ObjCImplDecl *D);
99 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
100 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
101 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
102 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
103 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000104 };
105}
106
107void PCHDeclReader::VisitDecl(Decl *D) {
108 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
109 D->setLexicalDeclContext(
110 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
112 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000115 D->setImplicit(Record[Idx++]);
116 D->setAccess((AccessSpecifier)Record[Idx++]);
117}
118
119void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
120 VisitDecl(TU);
121}
122
123void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
124 VisitDecl(ND);
125 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
126}
127
128void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
129 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000134 // Note that we cannot use VisitTypeDecl here, because we need to
135 // set the underlying type of the typedef *before* we try to read
136 // the type associated with the TypedefDecl.
137 VisitNamedDecl(TD);
138 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
139 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
140 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000141}
142
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000143void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
144 VisitTypeDecl(TD);
145 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
146 TD->setDefinition(Record[Idx++]);
147 TD->setTypedefForAnonDecl(
148 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
149}
150
151void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
152 VisitTagDecl(ED);
153 ED->setIntegerType(Reader.GetType(Record[Idx++]));
154}
155
Douglas Gregor982365e2009-04-13 21:20:57 +0000156void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
157 VisitTagDecl(RD);
158 RD->setHasFlexibleArrayMember(Record[Idx++]);
159 RD->setAnonymousStructOrUnion(Record[Idx++]);
160}
161
Douglas Gregorc34897d2009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000178 FD->setPreviousDeclaration(
179 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
180 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
181 FD->setInline(Record[Idx++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff79ea0e02009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff7333b492009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
228 ID->setSuperClass(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
229 unsigned NumIvars = Record[Idx++];
230 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
231 IVars.reserve(NumIvars);
232 for (unsigned I = 0; I != NumIvars; ++I)
233 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
234 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
235
236 ID->setForwardDecl(Record[Idx++]);
237 ID->setImplicitInterfaceDecl(Record[Idx++]);
238 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
239 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000240 // FIXME: add protocols, categories.
241}
242
243void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
244 VisitFieldDecl(IVD);
245 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
246}
247
Steve Naroff97b53bd2009-04-21 15:12:33 +0000248void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
249 VisitObjCContainerDecl(PD);
250 PD->setForwardDecl(Record[Idx++]);
251 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
252 unsigned NumProtoRefs = Record[Idx++];
253 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
254 ProtoRefs.reserve(NumProtoRefs);
255 for (unsigned I = 0; I != NumProtoRefs; ++I)
256 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
257 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
258}
259
260void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
261 VisitFieldDecl(FD);
262}
263
264void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
265 VisitDecl(CD);
266 unsigned NumClassRefs = Record[Idx++];
267 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
268 ClassRefs.reserve(NumClassRefs);
269 for (unsigned I = 0; I != NumClassRefs; ++I)
270 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
271 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
272}
273
274void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
275 VisitDecl(FPD);
276 unsigned NumProtoRefs = Record[Idx++];
277 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
278 ProtoRefs.reserve(NumProtoRefs);
279 for (unsigned I = 0; I != NumProtoRefs; ++I)
280 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
281 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
282}
283
284void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
285 VisitObjCContainerDecl(CD);
286 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
287 unsigned NumProtoRefs = Record[Idx++];
288 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
289 ProtoRefs.reserve(NumProtoRefs);
290 for (unsigned I = 0; I != NumProtoRefs; ++I)
291 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
292 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
293 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
295}
296
297void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
298 VisitNamedDecl(CAD);
299 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
300}
301
302void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
303 VisitNamedDecl(D);
304 // FIXME: Implement.
305}
306
307void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
308 VisitDecl(D);
309 // FIXME: Implement.
310}
311
312void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
313 VisitObjCImplDecl(D);
314 // FIXME: Implement.
315}
316
317void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
318 VisitObjCImplDecl(D);
319 // FIXME: Implement.
320}
321
322
323void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
324 VisitDecl(D);
325 // FIXME: Implement.
326}
327
Douglas Gregor982365e2009-04-13 21:20:57 +0000328void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
329 VisitValueDecl(FD);
330 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000331 if (Record[Idx++])
332 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000333}
334
Douglas Gregorc34897d2009-04-09 22:27:44 +0000335void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
336 VisitValueDecl(VD);
337 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
338 VD->setThreadSpecified(Record[Idx++]);
339 VD->setCXXDirectInitializer(Record[Idx++]);
340 VD->setDeclaredInCondition(Record[Idx++]);
341 VD->setPreviousDeclaration(
342 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
343 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000344 if (Record[Idx++])
345 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000346}
347
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000348void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
349 VisitVarDecl(PD);
350 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000351 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000352}
353
354void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
355 VisitParmVarDecl(PD);
356 PD->setOriginalType(Reader.GetType(Record[Idx++]));
357}
358
Douglas Gregor2a491792009-04-13 22:49:25 +0000359void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
360 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000361 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000362}
363
364void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
365 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000366 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000367 unsigned NumParams = Record[Idx++];
368 llvm::SmallVector<ParmVarDecl *, 16> Params;
369 Params.reserve(NumParams);
370 for (unsigned I = 0; I != NumParams; ++I)
371 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
372 BD->setParams(Reader.getContext(), &Params[0], NumParams);
373}
374
Douglas Gregorc34897d2009-04-09 22:27:44 +0000375std::pair<uint64_t, uint64_t>
376PCHDeclReader::VisitDeclContext(DeclContext *DC) {
377 uint64_t LexicalOffset = Record[Idx++];
378 uint64_t VisibleOffset = 0;
379 if (DC->getPrimaryContext() == DC)
380 VisibleOffset = Record[Idx++];
381 return std::make_pair(LexicalOffset, VisibleOffset);
382}
383
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000384//===----------------------------------------------------------------------===//
385// Statement/expression deserialization
386//===----------------------------------------------------------------------===//
387namespace {
388 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000389 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000390 PCHReader &Reader;
391 const PCHReader::RecordData &Record;
392 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000393 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000394
395 public:
396 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000397 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
398 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000399
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000400 /// \brief The number of record fields required for the Stmt class
401 /// itself.
402 static const unsigned NumStmtFields = 0;
403
Douglas Gregor596e0932009-04-15 16:35:07 +0000404 /// \brief The number of record fields required for the Expr class
405 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000406 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000407
Douglas Gregora151ba42009-04-14 23:32:43 +0000408 // Each of the Visit* functions reads in part of the expression
409 // from the given record and the current expression stack, then
410 // return the total number of operands that it read from the
411 // expression stack.
412
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000413 unsigned VisitStmt(Stmt *S);
414 unsigned VisitNullStmt(NullStmt *S);
415 unsigned VisitCompoundStmt(CompoundStmt *S);
416 unsigned VisitSwitchCase(SwitchCase *S);
417 unsigned VisitCaseStmt(CaseStmt *S);
418 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000419 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000420 unsigned VisitIfStmt(IfStmt *S);
421 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000422 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000423 unsigned VisitDoStmt(DoStmt *S);
424 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000425 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000426 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000427 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000428 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000429 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000430 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000431 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000432 unsigned VisitExpr(Expr *E);
433 unsigned VisitPredefinedExpr(PredefinedExpr *E);
434 unsigned VisitDeclRefExpr(DeclRefExpr *E);
435 unsigned VisitIntegerLiteral(IntegerLiteral *E);
436 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000437 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000438 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000439 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000440 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000441 unsigned VisitUnaryOperator(UnaryOperator *E);
442 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000443 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000444 unsigned VisitCallExpr(CallExpr *E);
445 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000446 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000447 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000448 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
449 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000450 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000451 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
452 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000453 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000454 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000455 unsigned VisitInitListExpr(InitListExpr *E);
456 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
457 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000458 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000459 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000460 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000461 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
462 unsigned VisitChooseExpr(ChooseExpr *E);
463 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000464 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000465 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000466 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000467 };
468}
469
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000470unsigned PCHStmtReader::VisitStmt(Stmt *S) {
471 assert(Idx == NumStmtFields && "Incorrect statement field count");
472 return 0;
473}
474
475unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
476 VisitStmt(S);
477 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
478 return 0;
479}
480
481unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
482 VisitStmt(S);
483 unsigned NumStmts = Record[Idx++];
484 S->setStmts(Reader.getContext(),
485 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
486 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
487 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
488 return NumStmts;
489}
490
491unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
492 VisitStmt(S);
493 Reader.RecordSwitchCaseID(S, Record[Idx++]);
494 return 0;
495}
496
497unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
498 VisitSwitchCase(S);
499 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
500 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
501 S->setSubStmt(StmtStack.back());
502 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
503 return 3;
504}
505
506unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
507 VisitSwitchCase(S);
508 S->setSubStmt(StmtStack.back());
509 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
510 return 1;
511}
512
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000513unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
514 VisitStmt(S);
515 S->setID(Reader.GetIdentifierInfo(Record, Idx));
516 S->setSubStmt(StmtStack.back());
517 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
518 Reader.RecordLabelStmt(S, Record[Idx++]);
519 return 1;
520}
521
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000522unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
523 VisitStmt(S);
524 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
525 S->setThen(StmtStack[StmtStack.size() - 2]);
526 S->setElse(StmtStack[StmtStack.size() - 1]);
527 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
528 return 3;
529}
530
531unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
532 VisitStmt(S);
533 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
534 S->setBody(StmtStack.back());
535 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 SwitchCase *PrevSC = 0;
537 for (unsigned N = Record.size(); Idx != N; ++Idx) {
538 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
539 if (PrevSC)
540 PrevSC->setNextSwitchCase(SC);
541 else
542 S->setSwitchCaseList(SC);
543 PrevSC = SC;
544 }
545 return 2;
546}
547
Douglas Gregora6b503f2009-04-17 00:16:09 +0000548unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
549 VisitStmt(S);
550 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
551 S->setBody(StmtStack.back());
552 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
553 return 2;
554}
555
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000556unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
557 VisitStmt(S);
558 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
559 S->setBody(StmtStack.back());
560 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
561 return 2;
562}
563
564unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
565 VisitStmt(S);
566 S->setInit(StmtStack[StmtStack.size() - 4]);
567 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
568 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
569 S->setBody(StmtStack.back());
570 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
571 return 4;
572}
573
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000574unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
575 VisitStmt(S);
576 Reader.SetLabelOf(S, Record[Idx++]);
577 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
578 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
579 return 0;
580}
581
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000582unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
583 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000584 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000585 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
586 return 1;
587}
588
Douglas Gregora6b503f2009-04-17 00:16:09 +0000589unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
590 VisitStmt(S);
591 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 return 0;
593}
594
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000595unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
596 VisitStmt(S);
597 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 return 0;
599}
600
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000601unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
602 VisitStmt(S);
603 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
604 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
605 return 1;
606}
607
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000608unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
609 VisitStmt(S);
610 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
612
613 if (Idx + 1 == Record.size()) {
614 // Single declaration
615 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
616 } else {
617 llvm::SmallVector<Decl *, 16> Decls;
618 Decls.reserve(Record.size() - Idx);
619 for (unsigned N = Record.size(); Idx != N; ++Idx)
620 Decls.push_back(Reader.GetDecl(Record[Idx]));
621 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
622 &Decls[0], Decls.size())));
623 }
624 return 0;
625}
626
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000627unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
628 VisitStmt(S);
629 unsigned NumOutputs = Record[Idx++];
630 unsigned NumInputs = Record[Idx++];
631 unsigned NumClobbers = Record[Idx++];
632 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
633 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
634 S->setVolatile(Record[Idx++]);
635 S->setSimple(Record[Idx++]);
636
637 unsigned StackIdx
638 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
639 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
640
641 // Outputs and inputs
642 llvm::SmallVector<std::string, 16> Names;
643 llvm::SmallVector<StringLiteral*, 16> Constraints;
644 llvm::SmallVector<Stmt*, 16> Exprs;
645 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
646 Names.push_back(Reader.ReadString(Record, Idx));
647 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
648 Exprs.push_back(StmtStack[StackIdx++]);
649 }
650 S->setOutputsAndInputs(NumOutputs, NumInputs,
651 &Names[0], &Constraints[0], &Exprs[0]);
652
653 // Constraints
654 llvm::SmallVector<StringLiteral*, 16> Clobbers;
655 for (unsigned I = 0; I != NumClobbers; ++I)
656 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
657 S->setClobbers(&Clobbers[0], NumClobbers);
658
659 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
660 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
661}
662
Douglas Gregora151ba42009-04-14 23:32:43 +0000663unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000664 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000665 E->setType(Reader.GetType(Record[Idx++]));
666 E->setTypeDependent(Record[Idx++]);
667 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000668 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000669 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000670}
671
Douglas Gregora151ba42009-04-14 23:32:43 +0000672unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000673 VisitExpr(E);
674 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
675 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000676 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000677}
678
Douglas Gregora151ba42009-04-14 23:32:43 +0000679unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000680 VisitExpr(E);
681 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
682 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000683 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000684}
685
Douglas Gregora151ba42009-04-14 23:32:43 +0000686unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000687 VisitExpr(E);
688 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
689 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000690 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000691}
692
Douglas Gregora151ba42009-04-14 23:32:43 +0000693unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000694 VisitExpr(E);
695 E->setValue(Reader.ReadAPFloat(Record, Idx));
696 E->setExact(Record[Idx++]);
697 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000698 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000699}
700
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000701unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
702 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000703 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000704 return 1;
705}
706
Douglas Gregor596e0932009-04-15 16:35:07 +0000707unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
708 VisitExpr(E);
709 unsigned Len = Record[Idx++];
710 assert(Record[Idx] == E->getNumConcatenated() &&
711 "Wrong number of concatenated tokens!");
712 ++Idx;
713 E->setWide(Record[Idx++]);
714
715 // Read string data
716 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
717 E->setStrData(Reader.getContext(), &Str[0], Len);
718 Idx += Len;
719
720 // Read source locations
721 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
722 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
723
724 return 0;
725}
726
Douglas Gregora151ba42009-04-14 23:32:43 +0000727unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000728 VisitExpr(E);
729 E->setValue(Record[Idx++]);
730 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
731 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000732 return 0;
733}
734
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000735unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
736 VisitExpr(E);
737 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
738 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000739 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000740 return 1;
741}
742
Douglas Gregor12d74052009-04-15 15:58:59 +0000743unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
744 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000745 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000746 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
747 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
748 return 1;
749}
750
751unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
752 VisitExpr(E);
753 E->setSizeof(Record[Idx++]);
754 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000755 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000756 ++Idx;
757 } else {
758 E->setArgument(Reader.GetType(Record[Idx++]));
759 }
760 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
761 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
762 return E->isArgumentType()? 0 : 1;
763}
764
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000765unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
766 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000767 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
768 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000769 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
770 return 2;
771}
772
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000773unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
774 VisitExpr(E);
775 E->setNumArgs(Reader.getContext(), Record[Idx++]);
776 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000777 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000778 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000779 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000780 return E->getNumArgs() + 1;
781}
782
783unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
784 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000785 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000786 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
787 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 E->setArrow(Record[Idx++]);
789 return 1;
790}
791
Douglas Gregora151ba42009-04-14 23:32:43 +0000792unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
793 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000794 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000795 return 1;
796}
797
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000798unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
799 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
801 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000802 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
803 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
804 return 2;
805}
806
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000807unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
808 VisitBinaryOperator(E);
809 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
810 E->setComputationResultType(Reader.GetType(Record[Idx++]));
811 return 2;
812}
813
814unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
815 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000816 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
817 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
818 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000819 return 3;
820}
821
Douglas Gregora151ba42009-04-14 23:32:43 +0000822unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
823 VisitCastExpr(E);
824 E->setLvalueCast(Record[Idx++]);
825 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000826}
827
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000828unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
829 VisitCastExpr(E);
830 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
831 return 1;
832}
833
834unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
835 VisitExplicitCastExpr(E);
836 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
837 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
838 return 1;
839}
840
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000841unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
842 VisitExpr(E);
843 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000844 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000845 E->setFileScope(Record[Idx++]);
846 return 1;
847}
848
Douglas Gregorec0b8292009-04-15 23:02:49 +0000849unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
850 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000851 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000852 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
853 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
854 return 1;
855}
856
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000857unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
858 VisitExpr(E);
859 unsigned NumInits = Record[Idx++];
860 E->reserveInits(NumInits);
861 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000862 E->updateInit(I,
863 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
864 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000865 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
866 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
867 E->setInitializedFieldInUnion(
868 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
869 E->sawArrayRangeDesignator(Record[Idx++]);
870 return NumInits + 1;
871}
872
873unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
874 typedef DesignatedInitExpr::Designator Designator;
875
876 VisitExpr(E);
877 unsigned NumSubExprs = Record[Idx++];
878 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
879 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000880 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000881 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
882 E->setGNUSyntax(Record[Idx++]);
883
884 llvm::SmallVector<Designator, 4> Designators;
885 while (Idx < Record.size()) {
886 switch ((pch::DesignatorTypes)Record[Idx++]) {
887 case pch::DESIG_FIELD_DECL: {
888 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
889 SourceLocation DotLoc
890 = SourceLocation::getFromRawEncoding(Record[Idx++]);
891 SourceLocation FieldLoc
892 = SourceLocation::getFromRawEncoding(Record[Idx++]);
893 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
894 FieldLoc));
895 Designators.back().setField(Field);
896 break;
897 }
898
899 case pch::DESIG_FIELD_NAME: {
900 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
901 SourceLocation DotLoc
902 = SourceLocation::getFromRawEncoding(Record[Idx++]);
903 SourceLocation FieldLoc
904 = SourceLocation::getFromRawEncoding(Record[Idx++]);
905 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
906 break;
907 }
908
909 case pch::DESIG_ARRAY: {
910 unsigned Index = Record[Idx++];
911 SourceLocation LBracketLoc
912 = SourceLocation::getFromRawEncoding(Record[Idx++]);
913 SourceLocation RBracketLoc
914 = SourceLocation::getFromRawEncoding(Record[Idx++]);
915 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
916 break;
917 }
918
919 case pch::DESIG_ARRAY_RANGE: {
920 unsigned Index = Record[Idx++];
921 SourceLocation LBracketLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 SourceLocation EllipsisLoc
924 = SourceLocation::getFromRawEncoding(Record[Idx++]);
925 SourceLocation RBracketLoc
926 = SourceLocation::getFromRawEncoding(Record[Idx++]);
927 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
928 RBracketLoc));
929 break;
930 }
931 }
932 }
933 E->setDesignators(&Designators[0], Designators.size());
934
935 return NumSubExprs;
936}
937
938unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
939 VisitExpr(E);
940 return 0;
941}
942
Douglas Gregorec0b8292009-04-15 23:02:49 +0000943unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
944 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000945 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000946 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
947 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
948 return 1;
949}
950
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000951unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
952 VisitExpr(E);
953 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
954 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
955 Reader.SetLabelOf(E, Record[Idx++]);
956 return 0;
957}
958
Douglas Gregoreca12f62009-04-17 19:05:30 +0000959unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
960 VisitExpr(E);
961 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
962 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
963 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
964 return 1;
965}
966
Douglas Gregor209d4622009-04-15 23:33:31 +0000967unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
968 VisitExpr(E);
969 E->setArgType1(Reader.GetType(Record[Idx++]));
970 E->setArgType2(Reader.GetType(Record[Idx++]));
971 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
972 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
973 return 0;
974}
975
976unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
977 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000978 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
979 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
980 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000981 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
982 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
983 return 3;
984}
985
986unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
987 VisitExpr(E);
988 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
989 return 0;
990}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000991
Douglas Gregor725e94b2009-04-16 00:01:45 +0000992unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
993 VisitExpr(E);
994 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000995 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000996 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
997 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
998 return NumExprs;
999}
1000
Douglas Gregore246b742009-04-17 19:21:43 +00001001unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1002 VisitExpr(E);
1003 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1004 E->setHasBlockDeclRefExprs(Record[Idx++]);
1005 return 0;
1006}
1007
Douglas Gregor725e94b2009-04-16 00:01:45 +00001008unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1009 VisitExpr(E);
1010 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1011 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1012 E->setByRef(Record[Idx++]);
1013 return 0;
1014}
1015
Douglas Gregorc713da92009-04-21 22:25:48 +00001016//===----------------------------------------------------------------------===//
1017// PCH reader implementation
1018//===----------------------------------------------------------------------===//
1019
1020namespace {
1021class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1022 PCHReader &Reader;
1023
1024 // If we know the IdentifierInfo in advance, it is here and we will
1025 // not build a new one. Used when deserializing information about an
1026 // identifier that was constructed before the PCH file was read.
1027 IdentifierInfo *KnownII;
1028
1029public:
1030 typedef IdentifierInfo * data_type;
1031
1032 typedef const std::pair<const char*, unsigned> external_key_type;
1033
1034 typedef external_key_type internal_key_type;
1035
1036 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1037 : Reader(Reader), KnownII(II) { }
1038
1039 static bool EqualKey(const internal_key_type& a,
1040 const internal_key_type& b) {
1041 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1042 : false;
1043 }
1044
1045 static unsigned ComputeHash(const internal_key_type& a) {
1046 return BernsteinHash(a.first, a.second);
1047 }
1048
1049 // This hopefully will just get inlined and removed by the optimizer.
1050 static const internal_key_type&
1051 GetInternalKey(const external_key_type& x) { return x; }
1052
1053 static std::pair<unsigned, unsigned>
1054 ReadKeyDataLength(const unsigned char*& d) {
1055 using namespace clang::io;
1056 unsigned KeyLen = ReadUnalignedLE16(d);
1057 unsigned DataLen = ReadUnalignedLE16(d);
1058 return std::make_pair(KeyLen, DataLen);
1059 }
1060
1061 static std::pair<const char*, unsigned>
1062 ReadKey(const unsigned char* d, unsigned n) {
1063 assert(n >= 2 && d[n-1] == '\0');
1064 return std::make_pair((const char*) d, n-1);
1065 }
1066
1067 IdentifierInfo *ReadData(const internal_key_type& k,
1068 const unsigned char* d,
1069 unsigned DataLen) {
1070 using namespace clang::io;
1071 uint32_t Bits = ReadUnalignedLE32(d); // FIXME: use these?
1072 (void)Bits;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001073 bool hasMacroDefinition = (Bits >> 3) & 0x01;
1074
Douglas Gregorc713da92009-04-21 22:25:48 +00001075 pch::IdentID ID = ReadUnalignedLE32(d);
1076 DataLen -= 8;
1077
1078 // Build the IdentifierInfo itself and link the identifier ID with
1079 // the new IdentifierInfo.
1080 IdentifierInfo *II = KnownII;
1081 if (!II)
1082 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1083 k.first, k.first + k.second);
1084 Reader.SetIdentifierInfo(ID, II);
1085
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001086 // If this identifier is a macro, deserialize the macro
1087 // definition.
1088 if (hasMacroDefinition) {
1089 uint32_t Offset = ReadUnalignedLE64(d);
1090 Reader.ReadMacroRecord(Offset);
1091 DataLen -= 8;
1092 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001093
1094 // Read all of the declarations visible at global scope with this
1095 // name.
1096 Sema *SemaObj = Reader.getSema();
1097 while (DataLen > 0) {
1098 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1099
1100 if (SemaObj) {
1101 // Introduce this declaration into the translation-unit scope
1102 // and add it to the declaration chain for this identifier, so
1103 // that (unqualified) name lookup will find it.
1104 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1105 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1106 } else {
1107 // Queue this declaration so that it will be added to the
1108 // translation unit scope and identifier's declaration chain
1109 // once a Sema object is known.
1110 // FIXME: This is a temporary hack. It will go away once we have
1111 // lazy deserialization of macros.
1112 Reader.TUDecls.push_back(D);
1113 }
1114
1115 DataLen -= 4;
1116 }
1117 return II;
1118 }
1119};
1120
1121} // end anonymous namespace
1122
1123/// \brief The on-disk hash table used to contain information about
1124/// all of the identifiers in the program.
1125typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1126 PCHIdentifierLookupTable;
1127
Douglas Gregorc34897d2009-04-09 22:27:44 +00001128// FIXME: use the diagnostics machinery
1129static bool Error(const char *Str) {
1130 std::fprintf(stderr, "%s\n", Str);
1131 return true;
1132}
1133
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001134/// \brief Check the contents of the predefines buffer against the
1135/// contents of the predefines buffer used to build the PCH file.
1136///
1137/// The contents of the two predefines buffers should be the same. If
1138/// not, then some command-line option changed the preprocessor state
1139/// and we must reject the PCH file.
1140///
1141/// \param PCHPredef The start of the predefines buffer in the PCH
1142/// file.
1143///
1144/// \param PCHPredefLen The length of the predefines buffer in the PCH
1145/// file.
1146///
1147/// \param PCHBufferID The FileID for the PCH predefines buffer.
1148///
1149/// \returns true if there was a mismatch (in which case the PCH file
1150/// should be ignored), or false otherwise.
1151bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1152 unsigned PCHPredefLen,
1153 FileID PCHBufferID) {
1154 const char *Predef = PP.getPredefines().c_str();
1155 unsigned PredefLen = PP.getPredefines().size();
1156
1157 // If the two predefines buffers compare equal, we're done!.
1158 if (PredefLen == PCHPredefLen &&
1159 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1160 return false;
1161
1162 // The predefines buffers are different. Produce a reasonable
1163 // diagnostic showing where they are different.
1164
1165 // The source locations (potentially in the two different predefines
1166 // buffers)
1167 SourceLocation Loc1, Loc2;
1168 SourceManager &SourceMgr = PP.getSourceManager();
1169
1170 // Create a source buffer for our predefines string, so
1171 // that we can build a diagnostic that points into that
1172 // source buffer.
1173 FileID BufferID;
1174 if (Predef && Predef[0]) {
1175 llvm::MemoryBuffer *Buffer
1176 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1177 "<built-in>");
1178 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1179 }
1180
1181 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1182 std::pair<const char *, const char *> Locations
1183 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1184
1185 if (Locations.first != Predef + MinLen) {
1186 // We found the location in the two buffers where there is a
1187 // difference. Form source locations to point there (in both
1188 // buffers).
1189 unsigned Offset = Locations.first - Predef;
1190 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1191 .getFileLocWithOffset(Offset);
1192 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1193 .getFileLocWithOffset(Offset);
1194 } else if (PredefLen > PCHPredefLen) {
1195 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1196 .getFileLocWithOffset(MinLen);
1197 } else {
1198 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1199 .getFileLocWithOffset(MinLen);
1200 }
1201
1202 Diag(Loc1, diag::warn_pch_preprocessor);
1203 if (Loc2.isValid())
1204 Diag(Loc2, diag::note_predef_in_pch);
1205 Diag(diag::note_ignoring_pch) << FileName;
1206 return true;
1207}
1208
Douglas Gregor635f97f2009-04-13 16:31:14 +00001209/// \brief Read the line table in the source manager block.
1210/// \returns true if ther was an error.
1211static bool ParseLineTable(SourceManager &SourceMgr,
1212 llvm::SmallVectorImpl<uint64_t> &Record) {
1213 unsigned Idx = 0;
1214 LineTableInfo &LineTable = SourceMgr.getLineTable();
1215
1216 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001217 std::map<int, int> FileIDs;
1218 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001219 // Extract the file name
1220 unsigned FilenameLen = Record[Idx++];
1221 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1222 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001223 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1224 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001225 }
1226
1227 // Parse the line entries
1228 std::vector<LineEntry> Entries;
1229 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001230 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001231
1232 // Extract the line entries
1233 unsigned NumEntries = Record[Idx++];
1234 Entries.clear();
1235 Entries.reserve(NumEntries);
1236 for (unsigned I = 0; I != NumEntries; ++I) {
1237 unsigned FileOffset = Record[Idx++];
1238 unsigned LineNo = Record[Idx++];
1239 int FilenameID = Record[Idx++];
1240 SrcMgr::CharacteristicKind FileKind
1241 = (SrcMgr::CharacteristicKind)Record[Idx++];
1242 unsigned IncludeOffset = Record[Idx++];
1243 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1244 FileKind, IncludeOffset));
1245 }
1246 LineTable.AddEntry(FID, Entries);
1247 }
1248
1249 return false;
1250}
1251
Douglas Gregorab1cef72009-04-10 03:52:48 +00001252/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001253PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001254 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001255 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1256 Error("Malformed source manager block record");
1257 return Failure;
1258 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001259
1260 SourceManager &SourceMgr = Context.getSourceManager();
1261 RecordData Record;
1262 while (true) {
1263 unsigned Code = Stream.ReadCode();
1264 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001265 if (Stream.ReadBlockEnd()) {
1266 Error("Error at end of Source Manager block");
1267 return Failure;
1268 }
1269
1270 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001271 }
1272
1273 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1274 // No known subblocks, always skip them.
1275 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001276 if (Stream.SkipBlock()) {
1277 Error("Malformed block record");
1278 return Failure;
1279 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001280 continue;
1281 }
1282
1283 if (Code == llvm::bitc::DEFINE_ABBREV) {
1284 Stream.ReadAbbrevRecord();
1285 continue;
1286 }
1287
1288 // Read a record.
1289 const char *BlobStart;
1290 unsigned BlobLen;
1291 Record.clear();
1292 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1293 default: // Default behavior: ignore.
1294 break;
1295
1296 case pch::SM_SLOC_FILE_ENTRY: {
1297 // FIXME: We would really like to delay the creation of this
1298 // FileEntry until it is actually required, e.g., when producing
1299 // a diagnostic with a source location in this file.
1300 const FileEntry *File
1301 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1302 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001303 FileID ID = SourceMgr.createFileID(File,
1304 SourceLocation::getFromRawEncoding(Record[1]),
1305 (CharacteristicKind)Record[2]);
1306 if (Record[3])
1307 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1308 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001309 break;
1310 }
1311
1312 case pch::SM_SLOC_BUFFER_ENTRY: {
1313 const char *Name = BlobStart;
1314 unsigned Code = Stream.ReadCode();
1315 Record.clear();
1316 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1317 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001318 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001319 llvm::MemoryBuffer *Buffer
1320 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1321 BlobStart + BlobLen - 1,
1322 Name);
1323 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1324
1325 if (strcmp(Name, "<built-in>") == 0
1326 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1327 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001328 break;
1329 }
1330
1331 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1332 SourceLocation SpellingLoc
1333 = SourceLocation::getFromRawEncoding(Record[1]);
1334 SourceMgr.createInstantiationLoc(
1335 SpellingLoc,
1336 SourceLocation::getFromRawEncoding(Record[2]),
1337 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001338 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001339 break;
1340 }
1341
Chris Lattnere1be6022009-04-14 23:22:57 +00001342 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001343 if (ParseLineTable(SourceMgr, Record))
1344 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001345 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001346 }
1347 }
1348}
1349
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001350void PCHReader::ReadMacroRecord(uint64_t Offset) {
1351 // Keep track of where we are in the stream, then jump back there
1352 // after reading this macro.
1353 SavedStreamPosition SavedPosition(Stream);
1354
1355 Stream.JumpToBit(Offset);
1356 RecordData Record;
1357 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1358 MacroInfo *Macro = 0;
1359 while (true) {
1360 unsigned Code = Stream.ReadCode();
1361 switch (Code) {
1362 case llvm::bitc::END_BLOCK:
1363 return;
1364
1365 case llvm::bitc::ENTER_SUBBLOCK:
1366 // No known subblocks, always skip them.
1367 Stream.ReadSubBlockID();
1368 if (Stream.SkipBlock()) {
1369 Error("Malformed block record");
1370 return;
1371 }
1372 continue;
1373
1374 case llvm::bitc::DEFINE_ABBREV:
1375 Stream.ReadAbbrevRecord();
1376 continue;
1377 default: break;
1378 }
1379
1380 // Read a record.
1381 Record.clear();
1382 pch::PreprocessorRecordTypes RecType =
1383 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1384 switch (RecType) {
1385 case pch::PP_COUNTER_VALUE:
1386 // Skip this record.
1387 break;
1388
1389 case pch::PP_MACRO_OBJECT_LIKE:
1390 case pch::PP_MACRO_FUNCTION_LIKE: {
1391 // If we already have a macro, that means that we've hit the end
1392 // of the definition of the macro we were looking for. We're
1393 // done.
1394 if (Macro)
1395 return;
1396
1397 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1398 if (II == 0) {
1399 Error("Macro must have a name");
1400 return;
1401 }
1402 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1403 bool isUsed = Record[2];
1404
1405 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1406 MI->setIsUsed(isUsed);
1407
1408 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1409 // Decode function-like macro info.
1410 bool isC99VarArgs = Record[3];
1411 bool isGNUVarArgs = Record[4];
1412 MacroArgs.clear();
1413 unsigned NumArgs = Record[5];
1414 for (unsigned i = 0; i != NumArgs; ++i)
1415 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1416
1417 // Install function-like macro info.
1418 MI->setIsFunctionLike();
1419 if (isC99VarArgs) MI->setIsC99Varargs();
1420 if (isGNUVarArgs) MI->setIsGNUVarargs();
1421 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1422 PP.getPreprocessorAllocator());
1423 }
1424
1425 // Finally, install the macro.
1426 PP.setMacroInfo(II, MI);
1427
1428 // Remember that we saw this macro last so that we add the tokens that
1429 // form its body to it.
1430 Macro = MI;
1431 ++NumMacrosRead;
1432 break;
1433 }
1434
1435 case pch::PP_TOKEN: {
1436 // If we see a TOKEN before a PP_MACRO_*, then the file is
1437 // erroneous, just pretend we didn't see this.
1438 if (Macro == 0) break;
1439
1440 Token Tok;
1441 Tok.startToken();
1442 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1443 Tok.setLength(Record[1]);
1444 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1445 Tok.setIdentifierInfo(II);
1446 Tok.setKind((tok::TokenKind)Record[3]);
1447 Tok.setFlag((Token::TokenFlags)Record[4]);
1448 Macro->AddTokenToBody(Tok);
1449 break;
1450 }
1451 }
1452 }
1453}
1454
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001455bool PCHReader::ReadPreprocessorBlock() {
1456 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1457 return Error("Malformed preprocessor block record");
1458
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001459 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001460 while (true) {
1461 unsigned Code = Stream.ReadCode();
1462 switch (Code) {
1463 case llvm::bitc::END_BLOCK:
1464 if (Stream.ReadBlockEnd())
1465 return Error("Error at end of preprocessor block");
1466 return false;
1467
1468 case llvm::bitc::ENTER_SUBBLOCK:
1469 // No known subblocks, always skip them.
1470 Stream.ReadSubBlockID();
1471 if (Stream.SkipBlock())
1472 return Error("Malformed block record");
1473 continue;
1474
1475 case llvm::bitc::DEFINE_ABBREV:
1476 Stream.ReadAbbrevRecord();
1477 continue;
1478 default: break;
1479 }
1480
1481 // Read a record.
1482 Record.clear();
1483 pch::PreprocessorRecordTypes RecType =
1484 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1485 switch (RecType) {
1486 default: // Default behavior: ignore unknown records.
1487 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001488 case pch::PP_COUNTER_VALUE:
1489 if (!Record.empty())
1490 PP.setCounterValue(Record[0]);
1491 break;
1492
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001493 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001494 case pch::PP_MACRO_FUNCTION_LIKE:
1495 case pch::PP_TOKEN:
1496 // Once we've hit a macro definition or a token, we're done.
1497 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001498 }
1499 }
1500}
1501
Douglas Gregorc713da92009-04-21 22:25:48 +00001502PCHReader::PCHReadResult
1503PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001504 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1505 Error("Malformed block record");
1506 return Failure;
1507 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001508
1509 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001510 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001511 while (!Stream.AtEndOfStream()) {
1512 unsigned Code = Stream.ReadCode();
1513 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001514 if (Stream.ReadBlockEnd()) {
1515 Error("Error at end of module block");
1516 return Failure;
1517 }
Chris Lattner29241862009-04-11 21:15:38 +00001518
Douglas Gregor179cfb12009-04-10 20:39:37 +00001519 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001520 }
1521
1522 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1523 switch (Stream.ReadSubBlockID()) {
1524 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1525 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1526 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001527 if (Stream.SkipBlock()) {
1528 Error("Malformed block record");
1529 return Failure;
1530 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001531 break;
1532
Chris Lattner29241862009-04-11 21:15:38 +00001533 case pch::PREPROCESSOR_BLOCK_ID:
1534 // Skip the preprocessor block for now, but remember where it is. We
1535 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001536 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001537 Error("Multiple preprocessor blocks found.");
1538 return Failure;
1539 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001540 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001541 if (Stream.SkipBlock()) {
1542 Error("Malformed block record");
1543 return Failure;
1544 }
1545 break;
1546
Douglas Gregorab1cef72009-04-10 03:52:48 +00001547 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001548 switch (ReadSourceManagerBlock()) {
1549 case Success:
1550 break;
1551
1552 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001553 Error("Malformed source manager block");
1554 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001555
1556 case IgnorePCH:
1557 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001558 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001559 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001560 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001561 continue;
1562 }
1563
1564 if (Code == llvm::bitc::DEFINE_ABBREV) {
1565 Stream.ReadAbbrevRecord();
1566 continue;
1567 }
1568
1569 // Read and process a record.
1570 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001571 const char *BlobStart = 0;
1572 unsigned BlobLen = 0;
1573 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1574 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001575 default: // Default behavior: ignore.
1576 break;
1577
1578 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001579 if (!TypeOffsets.empty()) {
1580 Error("Duplicate TYPE_OFFSET record in PCH file");
1581 return Failure;
1582 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001583 TypeOffsets.swap(Record);
1584 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1585 break;
1586
1587 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001588 if (!DeclOffsets.empty()) {
1589 Error("Duplicate DECL_OFFSET record in PCH file");
1590 return Failure;
1591 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001592 DeclOffsets.swap(Record);
1593 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1594 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001595
1596 case pch::LANGUAGE_OPTIONS:
1597 if (ParseLanguageOptions(Record))
1598 return IgnorePCH;
1599 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001600
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001601 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001602 std::string TargetTriple(BlobStart, BlobLen);
1603 if (TargetTriple != Context.Target.getTargetTriple()) {
1604 Diag(diag::warn_pch_target_triple)
1605 << TargetTriple << Context.Target.getTargetTriple();
1606 Diag(diag::note_ignoring_pch) << FileName;
1607 return IgnorePCH;
1608 }
1609 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001610 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001611
1612 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001613 IdentifierTableData = BlobStart;
1614 IdentifierLookupTable
1615 = PCHIdentifierLookupTable::Create(
1616 (const unsigned char *)IdentifierTableData + Record[0],
1617 (const unsigned char *)IdentifierTableData,
1618 PCHIdentifierLookupTrait(*this));
1619 // FIXME: What about any identifiers already placed into the
1620 // identifier table? Should we load decls with those names now?
1621 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001622 break;
1623
1624 case pch::IDENTIFIER_OFFSET:
1625 if (!IdentifierData.empty()) {
1626 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1627 return Failure;
1628 }
1629 IdentifierData.swap(Record);
1630#ifndef NDEBUG
1631 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1632 if ((IdentifierData[I] & 0x01) == 0) {
1633 Error("Malformed identifier table in the precompiled header");
1634 return Failure;
1635 }
1636 }
1637#endif
1638 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001639
1640 case pch::EXTERNAL_DEFINITIONS:
1641 if (!ExternalDefinitions.empty()) {
1642 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1643 return Failure;
1644 }
1645 ExternalDefinitions.swap(Record);
1646 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001647
Douglas Gregore01ad442009-04-18 05:55:16 +00001648 case pch::SPECIAL_TYPES:
1649 SpecialTypes.swap(Record);
1650 break;
1651
Douglas Gregor456e0952009-04-17 22:13:46 +00001652 case pch::STATISTICS:
1653 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001654 TotalNumMacros = Record[1];
Douglas Gregor456e0952009-04-17 22:13:46 +00001655 break;
1656
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001657 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001658 }
1659
Douglas Gregor179cfb12009-04-10 20:39:37 +00001660 Error("Premature end of bitstream");
1661 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001662}
1663
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001664PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001665 // Set the PCH file name.
1666 this->FileName = FileName;
1667
Douglas Gregorc34897d2009-04-09 22:27:44 +00001668 // Open the PCH file.
1669 std::string ErrStr;
1670 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001671 if (!Buffer) {
1672 Error(ErrStr.c_str());
1673 return IgnorePCH;
1674 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675
1676 // Initialize the stream
1677 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1678 (const unsigned char *)Buffer->getBufferEnd());
1679
1680 // Sniff for the signature.
1681 if (Stream.Read(8) != 'C' ||
1682 Stream.Read(8) != 'P' ||
1683 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001684 Stream.Read(8) != 'H') {
1685 Error("Not a PCH file");
1686 return IgnorePCH;
1687 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001688
1689 // We expect a number of well-defined blocks, though we don't necessarily
1690 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001691 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001692 while (!Stream.AtEndOfStream()) {
1693 unsigned Code = Stream.ReadCode();
1694
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001695 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1696 Error("Invalid record at top-level");
1697 return Failure;
1698 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001699
1700 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001701
Douglas Gregorc34897d2009-04-09 22:27:44 +00001702 // We only know the PCH subblock ID.
1703 switch (BlockID) {
1704 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001705 if (Stream.ReadBlockInfoBlock()) {
1706 Error("Malformed BlockInfoBlock");
1707 return Failure;
1708 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001709 break;
1710 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001711 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001712 case Success:
1713 break;
1714
1715 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001716 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001717
1718 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001719 // FIXME: We could consider reading through to the end of this
1720 // PCH block, skipping subblocks, to see if there are other
1721 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001722 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001723 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001724 break;
1725 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001726 if (Stream.SkipBlock()) {
1727 Error("Malformed block record");
1728 return Failure;
1729 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001730 break;
1731 }
1732 }
1733
1734 // Load the translation unit declaration
1735 ReadDeclRecord(DeclOffsets[0], 0);
1736
Douglas Gregorc713da92009-04-21 22:25:48 +00001737 // Initialization of builtins and library builtins occurs before the
1738 // PCH file is read, so there may be some identifiers that were
1739 // loaded into the IdentifierTable before we intercepted the
1740 // creation of identifiers. Iterate through the list of known
1741 // identifiers and determine whether we have to establish
1742 // preprocessor definitions or top-level identifier declaration
1743 // chains for those identifiers.
1744 //
1745 // We copy the IdentifierInfo pointers to a small vector first,
1746 // since de-serializing declarations or macro definitions can add
1747 // new entries into the identifier table, invalidating the
1748 // iterators.
1749 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1750 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1751 IdEnd = PP.getIdentifierTable().end();
1752 Id != IdEnd; ++Id)
1753 Identifiers.push_back(Id->second);
1754 PCHIdentifierLookupTable *IdTable
1755 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1756 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1757 IdentifierInfo *II = Identifiers[I];
1758 // Look in the on-disk hash table for an entry for
1759 PCHIdentifierLookupTrait Info(*this, II);
1760 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1761 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1762 if (Pos == IdTable->end())
1763 continue;
1764
1765 // Dereferencing the iterator has the effect of populating the
1766 // IdentifierInfo node with the various declarations it needs.
1767 (void)*Pos;
1768 }
1769
Douglas Gregore01ad442009-04-18 05:55:16 +00001770 // Load the special types.
1771 Context.setBuiltinVaListType(
1772 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1773
Douglas Gregorc713da92009-04-21 22:25:48 +00001774 // If we saw the preprocessor block, read it now.
1775 if (PreprocessorBlockOffset) {
1776 SavedStreamPosition SavedPos(Stream);
1777 Stream.JumpToBit(PreprocessorBlockOffset);
1778 if (ReadPreprocessorBlock()) {
1779 Error("Malformed preprocessor block");
1780 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001781 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001782 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001783
Douglas Gregorc713da92009-04-21 22:25:48 +00001784 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001785}
1786
Douglas Gregor179cfb12009-04-10 20:39:37 +00001787/// \brief Parse the record that corresponds to a LangOptions data
1788/// structure.
1789///
1790/// This routine compares the language options used to generate the
1791/// PCH file against the language options set for the current
1792/// compilation. For each option, we classify differences between the
1793/// two compiler states as either "benign" or "important". Benign
1794/// differences don't matter, and we accept them without complaint
1795/// (and without modifying the language options). Differences between
1796/// the states for important options cause the PCH file to be
1797/// unusable, so we emit a warning and return true to indicate that
1798/// there was an error.
1799///
1800/// \returns true if the PCH file is unacceptable, false otherwise.
1801bool PCHReader::ParseLanguageOptions(
1802 const llvm::SmallVectorImpl<uint64_t> &Record) {
1803 const LangOptions &LangOpts = Context.getLangOptions();
1804#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1805#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1806 if (Record[Idx] != LangOpts.Option) { \
1807 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1808 Diag(diag::note_ignoring_pch) << FileName; \
1809 return true; \
1810 } \
1811 ++Idx
1812
1813 unsigned Idx = 0;
1814 PARSE_LANGOPT_BENIGN(Trigraphs);
1815 PARSE_LANGOPT_BENIGN(BCPLComment);
1816 PARSE_LANGOPT_BENIGN(DollarIdents);
1817 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1818 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1819 PARSE_LANGOPT_BENIGN(ImplicitInt);
1820 PARSE_LANGOPT_BENIGN(Digraphs);
1821 PARSE_LANGOPT_BENIGN(HexFloats);
1822 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1823 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1824 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1825 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1826 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1827 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1828 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1829 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1830 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1831 PARSE_LANGOPT_BENIGN(PascalStrings);
1832 PARSE_LANGOPT_BENIGN(Boolean);
1833 PARSE_LANGOPT_BENIGN(WritableStrings);
1834 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1835 diag::warn_pch_lax_vector_conversions);
1836 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1837 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1838 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1839 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1840 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1841 diag::warn_pch_thread_safe_statics);
1842 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1843 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1844 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1845 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1846 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1847 diag::warn_pch_heinous_extensions);
1848 // FIXME: Most of the options below are benign if the macro wasn't
1849 // used. Unfortunately, this means that a PCH compiled without
1850 // optimization can't be used with optimization turned on, even
1851 // though the only thing that changes is whether __OPTIMIZE__ was
1852 // defined... but if __OPTIMIZE__ never showed up in the header, it
1853 // doesn't matter. We could consider making this some special kind
1854 // of check.
1855 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1856 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1857 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1858 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1859 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1860 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1861 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1862 Diag(diag::warn_pch_gc_mode)
1863 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1864 Diag(diag::note_ignoring_pch) << FileName;
1865 return true;
1866 }
1867 ++Idx;
1868 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1869 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1870#undef PARSE_LANGOPT_IRRELEVANT
1871#undef PARSE_LANGOPT_BENIGN
1872
1873 return false;
1874}
1875
Douglas Gregorc34897d2009-04-09 22:27:44 +00001876/// \brief Read and return the type at the given offset.
1877///
1878/// This routine actually reads the record corresponding to the type
1879/// at the given offset in the bitstream. It is a helper routine for
1880/// GetType, which deals with reading type IDs.
1881QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001882 // Keep track of where we are in the stream, then jump back there
1883 // after reading this type.
1884 SavedStreamPosition SavedPosition(Stream);
1885
Douglas Gregorc34897d2009-04-09 22:27:44 +00001886 Stream.JumpToBit(Offset);
1887 RecordData Record;
1888 unsigned Code = Stream.ReadCode();
1889 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001890 case pch::TYPE_EXT_QUAL: {
1891 assert(Record.size() == 3 &&
1892 "Incorrect encoding of extended qualifier type");
1893 QualType Base = GetType(Record[0]);
1894 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1895 unsigned AddressSpace = Record[2];
1896
1897 QualType T = Base;
1898 if (GCAttr != QualType::GCNone)
1899 T = Context.getObjCGCQualType(T, GCAttr);
1900 if (AddressSpace)
1901 T = Context.getAddrSpaceQualType(T, AddressSpace);
1902 return T;
1903 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001904
Douglas Gregorc34897d2009-04-09 22:27:44 +00001905 case pch::TYPE_FIXED_WIDTH_INT: {
1906 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1907 return Context.getFixedWidthIntType(Record[0], Record[1]);
1908 }
1909
1910 case pch::TYPE_COMPLEX: {
1911 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1912 QualType ElemType = GetType(Record[0]);
1913 return Context.getComplexType(ElemType);
1914 }
1915
1916 case pch::TYPE_POINTER: {
1917 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1918 QualType PointeeType = GetType(Record[0]);
1919 return Context.getPointerType(PointeeType);
1920 }
1921
1922 case pch::TYPE_BLOCK_POINTER: {
1923 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1924 QualType PointeeType = GetType(Record[0]);
1925 return Context.getBlockPointerType(PointeeType);
1926 }
1927
1928 case pch::TYPE_LVALUE_REFERENCE: {
1929 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1930 QualType PointeeType = GetType(Record[0]);
1931 return Context.getLValueReferenceType(PointeeType);
1932 }
1933
1934 case pch::TYPE_RVALUE_REFERENCE: {
1935 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1936 QualType PointeeType = GetType(Record[0]);
1937 return Context.getRValueReferenceType(PointeeType);
1938 }
1939
1940 case pch::TYPE_MEMBER_POINTER: {
1941 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1942 QualType PointeeType = GetType(Record[0]);
1943 QualType ClassType = GetType(Record[1]);
1944 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1945 }
1946
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001947 case pch::TYPE_CONSTANT_ARRAY: {
1948 QualType ElementType = GetType(Record[0]);
1949 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1950 unsigned IndexTypeQuals = Record[2];
1951 unsigned Idx = 3;
1952 llvm::APInt Size = ReadAPInt(Record, Idx);
1953 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1954 }
1955
1956 case pch::TYPE_INCOMPLETE_ARRAY: {
1957 QualType ElementType = GetType(Record[0]);
1958 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1959 unsigned IndexTypeQuals = Record[2];
1960 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1961 }
1962
1963 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001964 QualType ElementType = GetType(Record[0]);
1965 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1966 unsigned IndexTypeQuals = Record[2];
1967 return Context.getVariableArrayType(ElementType, ReadExpr(),
1968 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001969 }
1970
1971 case pch::TYPE_VECTOR: {
1972 if (Record.size() != 2) {
1973 Error("Incorrect encoding of vector type in PCH file");
1974 return QualType();
1975 }
1976
1977 QualType ElementType = GetType(Record[0]);
1978 unsigned NumElements = Record[1];
1979 return Context.getVectorType(ElementType, NumElements);
1980 }
1981
1982 case pch::TYPE_EXT_VECTOR: {
1983 if (Record.size() != 2) {
1984 Error("Incorrect encoding of extended vector type in PCH file");
1985 return QualType();
1986 }
1987
1988 QualType ElementType = GetType(Record[0]);
1989 unsigned NumElements = Record[1];
1990 return Context.getExtVectorType(ElementType, NumElements);
1991 }
1992
1993 case pch::TYPE_FUNCTION_NO_PROTO: {
1994 if (Record.size() != 1) {
1995 Error("Incorrect encoding of no-proto function type");
1996 return QualType();
1997 }
1998 QualType ResultType = GetType(Record[0]);
1999 return Context.getFunctionNoProtoType(ResultType);
2000 }
2001
2002 case pch::TYPE_FUNCTION_PROTO: {
2003 QualType ResultType = GetType(Record[0]);
2004 unsigned Idx = 1;
2005 unsigned NumParams = Record[Idx++];
2006 llvm::SmallVector<QualType, 16> ParamTypes;
2007 for (unsigned I = 0; I != NumParams; ++I)
2008 ParamTypes.push_back(GetType(Record[Idx++]));
2009 bool isVariadic = Record[Idx++];
2010 unsigned Quals = Record[Idx++];
2011 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2012 isVariadic, Quals);
2013 }
2014
2015 case pch::TYPE_TYPEDEF:
2016 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2017 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2018
2019 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002020 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002021
2022 case pch::TYPE_TYPEOF: {
2023 if (Record.size() != 1) {
2024 Error("Incorrect encoding of typeof(type) in PCH file");
2025 return QualType();
2026 }
2027 QualType UnderlyingType = GetType(Record[0]);
2028 return Context.getTypeOfType(UnderlyingType);
2029 }
2030
2031 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002032 assert(Record.size() == 1 && "Incorrect encoding of record type");
2033 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002034
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002035 case pch::TYPE_ENUM:
2036 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2037 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2038
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002039 case pch::TYPE_OBJC_INTERFACE:
2040 // FIXME: Deserialize ObjCInterfaceType
2041 assert(false && "Cannot de-serialize ObjC interface types yet");
2042 return QualType();
2043
2044 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
2045 // FIXME: Deserialize ObjCQualifiedInterfaceType
2046 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
2047 return QualType();
2048
2049 case pch::TYPE_OBJC_QUALIFIED_ID:
2050 // FIXME: Deserialize ObjCQualifiedIdType
2051 assert(false && "Cannot de-serialize ObjC qualified id types yet");
2052 return QualType();
2053
2054 case pch::TYPE_OBJC_QUALIFIED_CLASS:
2055 // FIXME: Deserialize ObjCQualifiedClassType
2056 assert(false && "Cannot de-serialize ObjC qualified class types yet");
2057 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002058 }
2059
2060 // Suppress a GCC warning
2061 return QualType();
2062}
2063
2064/// \brief Note that we have loaded the declaration with the given
2065/// Index.
2066///
2067/// This routine notes that this declaration has already been loaded,
2068/// so that future GetDecl calls will return this declaration rather
2069/// than trying to load a new declaration.
2070inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2071 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2072 DeclAlreadyLoaded[Index] = true;
2073 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2074}
2075
2076/// \brief Read the declaration at the given offset from the PCH file.
2077Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002078 // Keep track of where we are in the stream, then jump back there
2079 // after reading this declaration.
2080 SavedStreamPosition SavedPosition(Stream);
2081
Douglas Gregorc34897d2009-04-09 22:27:44 +00002082 Decl *D = 0;
2083 Stream.JumpToBit(Offset);
2084 RecordData Record;
2085 unsigned Code = Stream.ReadCode();
2086 unsigned Idx = 0;
2087 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002088
Douglas Gregorc34897d2009-04-09 22:27:44 +00002089 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002090 case pch::DECL_ATTR:
2091 case pch::DECL_CONTEXT_LEXICAL:
2092 case pch::DECL_CONTEXT_VISIBLE:
2093 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2094 break;
2095
Douglas Gregorc34897d2009-04-09 22:27:44 +00002096 case pch::DECL_TRANSLATION_UNIT:
2097 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002098 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002099 break;
2100
2101 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002102 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002103 break;
2104 }
2105
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002106 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002107 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002108 break;
2109 }
2110
Douglas Gregor982365e2009-04-13 21:20:57 +00002111 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002112 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2113 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002114 break;
2115 }
2116
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002117 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002118 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2119 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002120 break;
2121 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002122
2123 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002124 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2125 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002126 break;
2127 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002128
Steve Naroff79ea0e02009-04-20 15:06:07 +00002129 case pch::DECL_OBJC_METHOD: {
2130 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2131 Selector(), QualType(), 0);
2132 break;
2133 }
2134
Steve Naroff97b53bd2009-04-21 15:12:33 +00002135 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002136 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2137 break;
2138 }
2139
Steve Naroff97b53bd2009-04-21 15:12:33 +00002140 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002141 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2142 ObjCIvarDecl::None);
2143 break;
2144 }
2145
Steve Naroff97b53bd2009-04-21 15:12:33 +00002146 case pch::DECL_OBJC_PROTOCOL: {
2147 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2148 break;
2149 }
2150
2151 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2152 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2153 QualType(), 0);
2154 break;
2155 }
2156
2157 case pch::DECL_OBJC_CLASS: {
2158 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2159 break;
2160 }
2161
2162 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2163 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2164 break;
2165 }
2166
2167 case pch::DECL_OBJC_CATEGORY: {
2168 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2169 break;
2170 }
2171
2172 case pch::DECL_OBJC_CATEGORY_IMPL: {
2173 // FIXME: Implement.
2174 break;
2175 }
2176
2177 case pch::DECL_OBJC_IMPLEMENTATION: {
2178 // FIXME: Implement.
2179 break;
2180 }
2181
2182 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2183 // FIXME: Implement.
2184 break;
2185 }
2186
2187 case pch::DECL_OBJC_PROPERTY: {
2188 // FIXME: Implement.
2189 break;
2190 }
2191
2192 case pch::DECL_OBJC_PROPERTY_IMPL: {
2193 // FIXME: Implement.
2194 break;
2195 }
2196
Douglas Gregor982365e2009-04-13 21:20:57 +00002197 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002198 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2199 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002200 break;
2201 }
2202
Douglas Gregorc34897d2009-04-09 22:27:44 +00002203 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002204 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2205 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002206 break;
2207 }
2208
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002209 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002210 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2211 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002212 break;
2213 }
2214
2215 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002216 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002217 QualType(), QualType(), VarDecl::None,
2218 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002219 break;
2220 }
2221
Douglas Gregor2a491792009-04-13 22:49:25 +00002222 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002223 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002224 break;
2225 }
2226
2227 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002228 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002229 break;
2230 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002231 }
2232
Douglas Gregorc713da92009-04-21 22:25:48 +00002233 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002234 if (D) {
2235 LoadedDecl(Index, D);
2236 Reader.Visit(D);
2237 }
2238
Douglas Gregorc34897d2009-04-09 22:27:44 +00002239 // If this declaration is also a declaration context, get the
2240 // offsets for its tables of lexical and visible declarations.
2241 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2242 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2243 if (Offsets.first || Offsets.second) {
2244 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2245 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2246 DeclContextOffsets[DC] = Offsets;
2247 }
2248 }
2249 assert(Idx == Record.size());
2250
2251 return D;
2252}
2253
Douglas Gregorac8f2802009-04-10 17:25:41 +00002254QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002255 unsigned Quals = ID & 0x07;
2256 unsigned Index = ID >> 3;
2257
2258 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2259 QualType T;
2260 switch ((pch::PredefinedTypeIDs)Index) {
2261 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2262 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2263 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2264
2265 case pch::PREDEF_TYPE_CHAR_U_ID:
2266 case pch::PREDEF_TYPE_CHAR_S_ID:
2267 // FIXME: Check that the signedness of CharTy is correct!
2268 T = Context.CharTy;
2269 break;
2270
2271 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2272 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2273 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2274 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2275 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2276 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2277 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2278 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2279 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2280 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2281 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2282 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2283 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2284 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2285 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2286 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2287 }
2288
2289 assert(!T.isNull() && "Unknown predefined type");
2290 return T.getQualifiedType(Quals);
2291 }
2292
2293 Index -= pch::NUM_PREDEF_TYPE_IDS;
2294 if (!TypeAlreadyLoaded[Index]) {
2295 // Load the type from the PCH file.
2296 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2297 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2298 TypeAlreadyLoaded[Index] = true;
2299 }
2300
2301 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2302}
2303
Douglas Gregorac8f2802009-04-10 17:25:41 +00002304Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002305 if (ID == 0)
2306 return 0;
2307
2308 unsigned Index = ID - 1;
2309 if (DeclAlreadyLoaded[Index])
2310 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2311
2312 // Load the declaration from the PCH file.
2313 return ReadDeclRecord(DeclOffsets[Index], Index);
2314}
2315
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002316Stmt *PCHReader::GetStmt(uint64_t Offset) {
2317 // Keep track of where we are in the stream, then jump back there
2318 // after reading this declaration.
2319 SavedStreamPosition SavedPosition(Stream);
2320
2321 Stream.JumpToBit(Offset);
2322 return ReadStmt();
2323}
2324
Douglas Gregorc34897d2009-04-09 22:27:44 +00002325bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002326 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002327 assert(DC->hasExternalLexicalStorage() &&
2328 "DeclContext has no lexical decls in storage");
2329 uint64_t Offset = DeclContextOffsets[DC].first;
2330 assert(Offset && "DeclContext has no lexical decls in storage");
2331
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002332 // Keep track of where we are in the stream, then jump back there
2333 // after reading this context.
2334 SavedStreamPosition SavedPosition(Stream);
2335
Douglas Gregorc34897d2009-04-09 22:27:44 +00002336 // Load the record containing all of the declarations lexically in
2337 // this context.
2338 Stream.JumpToBit(Offset);
2339 RecordData Record;
2340 unsigned Code = Stream.ReadCode();
2341 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002342 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002343 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2344
2345 // Load all of the declaration IDs
2346 Decls.clear();
2347 Decls.insert(Decls.end(), Record.begin(), Record.end());
2348 return false;
2349}
2350
2351bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2352 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2353 assert(DC->hasExternalVisibleStorage() &&
2354 "DeclContext has no visible decls in storage");
2355 uint64_t Offset = DeclContextOffsets[DC].second;
2356 assert(Offset && "DeclContext has no visible decls in storage");
2357
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002358 // Keep track of where we are in the stream, then jump back there
2359 // after reading this context.
2360 SavedStreamPosition SavedPosition(Stream);
2361
Douglas Gregorc34897d2009-04-09 22:27:44 +00002362 // Load the record containing all of the declarations visible in
2363 // this context.
2364 Stream.JumpToBit(Offset);
2365 RecordData Record;
2366 unsigned Code = Stream.ReadCode();
2367 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002368 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002369 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2370 if (Record.size() == 0)
2371 return false;
2372
2373 Decls.clear();
2374
2375 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002376 while (Idx < Record.size()) {
2377 Decls.push_back(VisibleDeclaration());
2378 Decls.back().Name = ReadDeclarationName(Record, Idx);
2379
Douglas Gregorc34897d2009-04-09 22:27:44 +00002380 unsigned Size = Record[Idx++];
2381 llvm::SmallVector<unsigned, 4> & LoadedDecls
2382 = Decls.back().Declarations;
2383 LoadedDecls.reserve(Size);
2384 for (unsigned I = 0; I < Size; ++I)
2385 LoadedDecls.push_back(Record[Idx++]);
2386 }
2387
2388 return false;
2389}
2390
Douglas Gregor631f6c62009-04-14 00:24:19 +00002391void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2392 if (!Consumer)
2393 return;
2394
2395 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2396 Decl *D = GetDecl(ExternalDefinitions[I]);
2397 DeclGroupRef DG(D);
2398 Consumer->HandleTopLevelDecl(DG);
2399 }
2400}
2401
Douglas Gregorc34897d2009-04-09 22:27:44 +00002402void PCHReader::PrintStats() {
2403 std::fprintf(stderr, "*** PCH Statistics:\n");
2404
2405 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2406 TypeAlreadyLoaded.end(),
2407 true);
2408 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2409 DeclAlreadyLoaded.end(),
2410 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002411 unsigned NumIdentifiersLoaded = 0;
2412 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2413 if ((IdentifierData[I] & 0x01) == 0)
2414 ++NumIdentifiersLoaded;
2415 }
2416
Douglas Gregorc34897d2009-04-09 22:27:44 +00002417 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2418 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002419 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2421 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002422 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2423 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2424 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2425 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002426 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2427 NumStatementsRead, TotalNumStatements,
2428 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002429 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2430 NumMacrosRead, TotalNumMacros,
2431 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002432 std::fprintf(stderr, "\n");
2433}
2434
Douglas Gregorc713da92009-04-21 22:25:48 +00002435void PCHReader::InitializeSema(Sema &S) {
2436 SemaObj = &S;
2437
2438 // FIXME: this makes sure any declarations that were deserialized
2439 // "too early" still get added to the identifier's declaration
2440 // chains.
2441 for (unsigned I = 0, N = TUDecls.size(); I != N; ++I) {
2442 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(TUDecls[I]));
2443 SemaObj->IdResolver.AddDecl(TUDecls[I]);
2444 }
2445 TUDecls.clear();
2446}
2447
2448IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2449 // Try to find this name within our on-disk hash table
2450 PCHIdentifierLookupTable *IdTable
2451 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2452 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2453 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2454 if (Pos == IdTable->end())
2455 return 0;
2456
2457 // Dereferencing the iterator has the effect of building the
2458 // IdentifierInfo node and populating it with the various
2459 // declarations it needs.
2460 return *Pos;
2461}
2462
2463void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2464 assert(ID && "Non-zero identifier ID required");
2465 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2466}
2467
Chris Lattner29241862009-04-11 21:15:38 +00002468IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002469 if (ID == 0)
2470 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002471
Douglas Gregorc713da92009-04-21 22:25:48 +00002472 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002473 Error("No identifier table in PCH file");
2474 return 0;
2475 }
Chris Lattner29241862009-04-11 21:15:38 +00002476
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002477 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002478 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002479 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002480 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002481 }
Chris Lattner29241862009-04-11 21:15:38 +00002482
2483 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002484}
2485
2486DeclarationName
2487PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2488 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2489 switch (Kind) {
2490 case DeclarationName::Identifier:
2491 return DeclarationName(GetIdentifierInfo(Record, Idx));
2492
2493 case DeclarationName::ObjCZeroArgSelector:
2494 case DeclarationName::ObjCOneArgSelector:
2495 case DeclarationName::ObjCMultiArgSelector:
2496 assert(false && "Unable to de-serialize Objective-C selectors");
2497 break;
2498
2499 case DeclarationName::CXXConstructorName:
2500 return Context.DeclarationNames.getCXXConstructorName(
2501 GetType(Record[Idx++]));
2502
2503 case DeclarationName::CXXDestructorName:
2504 return Context.DeclarationNames.getCXXDestructorName(
2505 GetType(Record[Idx++]));
2506
2507 case DeclarationName::CXXConversionFunctionName:
2508 return Context.DeclarationNames.getCXXConversionFunctionName(
2509 GetType(Record[Idx++]));
2510
2511 case DeclarationName::CXXOperatorName:
2512 return Context.DeclarationNames.getCXXOperatorName(
2513 (OverloadedOperatorKind)Record[Idx++]);
2514
2515 case DeclarationName::CXXUsingDirective:
2516 return DeclarationName::getUsingDirectiveName();
2517 }
2518
2519 // Required to silence GCC warning
2520 return DeclarationName();
2521}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002522
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002523/// \brief Read an integral value
2524llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2525 unsigned BitWidth = Record[Idx++];
2526 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2527 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2528 Idx += NumWords;
2529 return Result;
2530}
2531
2532/// \brief Read a signed integral value
2533llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2534 bool isUnsigned = Record[Idx++];
2535 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2536}
2537
Douglas Gregore2f37202009-04-14 21:55:33 +00002538/// \brief Read a floating-point value
2539llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002540 return llvm::APFloat(ReadAPInt(Record, Idx));
2541}
2542
Douglas Gregor1c507882009-04-15 21:30:51 +00002543// \brief Read a string
2544std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2545 unsigned Len = Record[Idx++];
2546 std::string Result(&Record[Idx], &Record[Idx] + Len);
2547 Idx += Len;
2548 return Result;
2549}
2550
2551/// \brief Reads attributes from the current stream position.
2552Attr *PCHReader::ReadAttributes() {
2553 unsigned Code = Stream.ReadCode();
2554 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2555 "Expected unabbreviated record"); (void)Code;
2556
2557 RecordData Record;
2558 unsigned Idx = 0;
2559 unsigned RecCode = Stream.ReadRecord(Code, Record);
2560 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2561 (void)RecCode;
2562
2563#define SIMPLE_ATTR(Name) \
2564 case Attr::Name: \
2565 New = ::new (Context) Name##Attr(); \
2566 break
2567
2568#define STRING_ATTR(Name) \
2569 case Attr::Name: \
2570 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2571 break
2572
2573#define UNSIGNED_ATTR(Name) \
2574 case Attr::Name: \
2575 New = ::new (Context) Name##Attr(Record[Idx++]); \
2576 break
2577
2578 Attr *Attrs = 0;
2579 while (Idx < Record.size()) {
2580 Attr *New = 0;
2581 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2582 bool IsInherited = Record[Idx++];
2583
2584 switch (Kind) {
2585 STRING_ATTR(Alias);
2586 UNSIGNED_ATTR(Aligned);
2587 SIMPLE_ATTR(AlwaysInline);
2588 SIMPLE_ATTR(AnalyzerNoReturn);
2589 STRING_ATTR(Annotate);
2590 STRING_ATTR(AsmLabel);
2591
2592 case Attr::Blocks:
2593 New = ::new (Context) BlocksAttr(
2594 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2595 break;
2596
2597 case Attr::Cleanup:
2598 New = ::new (Context) CleanupAttr(
2599 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2600 break;
2601
2602 SIMPLE_ATTR(Const);
2603 UNSIGNED_ATTR(Constructor);
2604 SIMPLE_ATTR(DLLExport);
2605 SIMPLE_ATTR(DLLImport);
2606 SIMPLE_ATTR(Deprecated);
2607 UNSIGNED_ATTR(Destructor);
2608 SIMPLE_ATTR(FastCall);
2609
2610 case Attr::Format: {
2611 std::string Type = ReadString(Record, Idx);
2612 unsigned FormatIdx = Record[Idx++];
2613 unsigned FirstArg = Record[Idx++];
2614 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2615 break;
2616 }
2617
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002618 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002619
2620 case Attr::IBOutletKind:
2621 New = ::new (Context) IBOutletAttr();
2622 break;
2623
2624 SIMPLE_ATTR(NoReturn);
2625 SIMPLE_ATTR(NoThrow);
2626 SIMPLE_ATTR(Nodebug);
2627 SIMPLE_ATTR(Noinline);
2628
2629 case Attr::NonNull: {
2630 unsigned Size = Record[Idx++];
2631 llvm::SmallVector<unsigned, 16> ArgNums;
2632 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2633 Idx += Size;
2634 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2635 break;
2636 }
2637
2638 SIMPLE_ATTR(ObjCException);
2639 SIMPLE_ATTR(ObjCNSObject);
2640 SIMPLE_ATTR(Overloadable);
2641 UNSIGNED_ATTR(Packed);
2642 SIMPLE_ATTR(Pure);
2643 UNSIGNED_ATTR(Regparm);
2644 STRING_ATTR(Section);
2645 SIMPLE_ATTR(StdCall);
2646 SIMPLE_ATTR(TransparentUnion);
2647 SIMPLE_ATTR(Unavailable);
2648 SIMPLE_ATTR(Unused);
2649 SIMPLE_ATTR(Used);
2650
2651 case Attr::Visibility:
2652 New = ::new (Context) VisibilityAttr(
2653 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2654 break;
2655
2656 SIMPLE_ATTR(WarnUnusedResult);
2657 SIMPLE_ATTR(Weak);
2658 SIMPLE_ATTR(WeakImport);
2659 }
2660
2661 assert(New && "Unable to decode attribute?");
2662 New->setInherited(IsInherited);
2663 New->setNext(Attrs);
2664 Attrs = New;
2665 }
2666#undef UNSIGNED_ATTR
2667#undef STRING_ATTR
2668#undef SIMPLE_ATTR
2669
2670 // The list of attributes was built backwards. Reverse the list
2671 // before returning it.
2672 Attr *PrevAttr = 0, *NextAttr = 0;
2673 while (Attrs) {
2674 NextAttr = Attrs->getNext();
2675 Attrs->setNext(PrevAttr);
2676 PrevAttr = Attrs;
2677 Attrs = NextAttr;
2678 }
2679
2680 return PrevAttr;
2681}
2682
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002683Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002684 // Within the bitstream, expressions are stored in Reverse Polish
2685 // Notation, with each of the subexpressions preceding the
2686 // expression they are stored in. To evaluate expressions, we
2687 // continue reading expressions and placing them on the stack, with
2688 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002689 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002690 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002691 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002692 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002693 llvm::SmallVector<Stmt *, 16> StmtStack;
2694 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002695 Stmt::EmptyShell Empty;
2696
Douglas Gregora151ba42009-04-14 23:32:43 +00002697 while (true) {
2698 unsigned Code = Stream.ReadCode();
2699 if (Code == llvm::bitc::END_BLOCK) {
2700 if (Stream.ReadBlockEnd()) {
2701 Error("Error at end of Source Manager block");
2702 return 0;
2703 }
2704 break;
2705 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002706
Douglas Gregora151ba42009-04-14 23:32:43 +00002707 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2708 // No known subblocks, always skip them.
2709 Stream.ReadSubBlockID();
2710 if (Stream.SkipBlock()) {
2711 Error("Malformed block record");
2712 return 0;
2713 }
2714 continue;
2715 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002716
Douglas Gregora151ba42009-04-14 23:32:43 +00002717 if (Code == llvm::bitc::DEFINE_ABBREV) {
2718 Stream.ReadAbbrevRecord();
2719 continue;
2720 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002721
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002722 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002723 Idx = 0;
2724 Record.clear();
2725 bool Finished = false;
2726 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002727 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002728 Finished = true;
2729 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002730
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002731 case pch::STMT_NULL_PTR:
2732 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002733 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002734
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002735 case pch::STMT_NULL:
2736 S = new (Context) NullStmt(Empty);
2737 break;
2738
2739 case pch::STMT_COMPOUND:
2740 S = new (Context) CompoundStmt(Empty);
2741 break;
2742
2743 case pch::STMT_CASE:
2744 S = new (Context) CaseStmt(Empty);
2745 break;
2746
2747 case pch::STMT_DEFAULT:
2748 S = new (Context) DefaultStmt(Empty);
2749 break;
2750
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002751 case pch::STMT_LABEL:
2752 S = new (Context) LabelStmt(Empty);
2753 break;
2754
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002755 case pch::STMT_IF:
2756 S = new (Context) IfStmt(Empty);
2757 break;
2758
2759 case pch::STMT_SWITCH:
2760 S = new (Context) SwitchStmt(Empty);
2761 break;
2762
Douglas Gregora6b503f2009-04-17 00:16:09 +00002763 case pch::STMT_WHILE:
2764 S = new (Context) WhileStmt(Empty);
2765 break;
2766
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002767 case pch::STMT_DO:
2768 S = new (Context) DoStmt(Empty);
2769 break;
2770
2771 case pch::STMT_FOR:
2772 S = new (Context) ForStmt(Empty);
2773 break;
2774
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002775 case pch::STMT_GOTO:
2776 S = new (Context) GotoStmt(Empty);
2777 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002778
2779 case pch::STMT_INDIRECT_GOTO:
2780 S = new (Context) IndirectGotoStmt(Empty);
2781 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002782
Douglas Gregora6b503f2009-04-17 00:16:09 +00002783 case pch::STMT_CONTINUE:
2784 S = new (Context) ContinueStmt(Empty);
2785 break;
2786
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002787 case pch::STMT_BREAK:
2788 S = new (Context) BreakStmt(Empty);
2789 break;
2790
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002791 case pch::STMT_RETURN:
2792 S = new (Context) ReturnStmt(Empty);
2793 break;
2794
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002795 case pch::STMT_DECL:
2796 S = new (Context) DeclStmt(Empty);
2797 break;
2798
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002799 case pch::STMT_ASM:
2800 S = new (Context) AsmStmt(Empty);
2801 break;
2802
Douglas Gregora151ba42009-04-14 23:32:43 +00002803 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002804 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002805 break;
2806
2807 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002808 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002809 break;
2810
2811 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002812 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002813 break;
2814
2815 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002816 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002817 break;
2818
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002819 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002820 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002821 break;
2822
Douglas Gregor596e0932009-04-15 16:35:07 +00002823 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002824 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002825 Record[PCHStmtReader::NumExprFields + 1]);
2826 break;
2827
Douglas Gregora151ba42009-04-14 23:32:43 +00002828 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002829 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002830 break;
2831
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002832 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002833 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002834 break;
2835
Douglas Gregor12d74052009-04-15 15:58:59 +00002836 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002837 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002838 break;
2839
2840 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002841 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002842 break;
2843
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002844 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002845 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002846 break;
2847
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002848 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002849 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002850 break;
2851
2852 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002853 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002854 break;
2855
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002856 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002857 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002858 break;
2859
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002860 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002861 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002862 break;
2863
2864 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002865 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002866 break;
2867
Douglas Gregora151ba42009-04-14 23:32:43 +00002868 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002869 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002870 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002871
2872 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002873 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002874 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002875
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002876 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002877 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002878 break;
2879
Douglas Gregorec0b8292009-04-15 23:02:49 +00002880 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002881 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002882 break;
2883
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002884 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002885 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002886 break;
2887
2888 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002889 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002890 Record[PCHStmtReader::NumExprFields] - 1);
2891
2892 break;
2893
2894 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002895 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002896 break;
2897
Douglas Gregorec0b8292009-04-15 23:02:49 +00002898 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002899 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002900 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002901
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002902 case pch::EXPR_ADDR_LABEL:
2903 S = new (Context) AddrLabelExpr(Empty);
2904 break;
2905
Douglas Gregoreca12f62009-04-17 19:05:30 +00002906 case pch::EXPR_STMT:
2907 S = new (Context) StmtExpr(Empty);
2908 break;
2909
Douglas Gregor209d4622009-04-15 23:33:31 +00002910 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002911 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002912 break;
2913
2914 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002915 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002916 break;
2917
2918 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002919 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002920 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002921
2922 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002923 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002924 break;
2925
Douglas Gregore246b742009-04-17 19:21:43 +00002926 case pch::EXPR_BLOCK:
2927 S = new (Context) BlockExpr(Empty);
2928 break;
2929
Douglas Gregor725e94b2009-04-16 00:01:45 +00002930 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002931 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002932 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002933 }
2934
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002935 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002936 if (Finished)
2937 break;
2938
Douglas Gregor456e0952009-04-17 22:13:46 +00002939 ++NumStatementsRead;
2940
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002941 if (S) {
2942 unsigned NumSubStmts = Reader.Visit(S);
2943 while (NumSubStmts > 0) {
2944 StmtStack.pop_back();
2945 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002946 }
2947 }
2948
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002949 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002950 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002951 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002952 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002953 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002954 return StmtStack.back();
2955}
2956
2957Expr *PCHReader::ReadExpr() {
2958 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002959}
2960
Douglas Gregor179cfb12009-04-10 20:39:37 +00002961DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002962 return Diag(SourceLocation(), DiagID);
2963}
2964
2965DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2966 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002967 Context.getSourceManager()),
2968 DiagID);
2969}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002970
Douglas Gregorc713da92009-04-21 22:25:48 +00002971/// \brief Retrieve the identifier table associated with the
2972/// preprocessor.
2973IdentifierTable &PCHReader::getIdentifierTable() {
2974 return PP.getIdentifierTable();
2975}
2976
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002977/// \brief Record that the given ID maps to the given switch-case
2978/// statement.
2979void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2980 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2981 SwitchCaseStmts[ID] = SC;
2982}
2983
2984/// \brief Retrieve the switch-case statement with the given ID.
2985SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2986 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2987 return SwitchCaseStmts[ID];
2988}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002989
2990/// \brief Record that the given label statement has been
2991/// deserialized and has the given ID.
2992void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2993 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2994 "Deserialized label twice");
2995 LabelStmts[ID] = S;
2996
2997 // If we've already seen any goto statements that point to this
2998 // label, resolve them now.
2999 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3000 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3001 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3002 Goto->second->setLabel(S);
3003 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003004
3005 // If we've already seen any address-label statements that point to
3006 // this label, resolve them now.
3007 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3008 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3009 = UnresolvedAddrLabelExprs.equal_range(ID);
3010 for (AddrLabelIter AddrLabel = AddrLabels.first;
3011 AddrLabel != AddrLabels.second; ++AddrLabel)
3012 AddrLabel->second->setLabel(S);
3013 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003014}
3015
3016/// \brief Set the label of the given statement to the label
3017/// identified by ID.
3018///
3019/// Depending on the order in which the label and other statements
3020/// referencing that label occur, this operation may complete
3021/// immediately (updating the statement) or it may queue the
3022/// statement to be back-patched later.
3023void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3024 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3025 if (Label != LabelStmts.end()) {
3026 // We've already seen this label, so set the label of the goto and
3027 // we're done.
3028 S->setLabel(Label->second);
3029 } else {
3030 // We haven't seen this label yet, so add this goto to the set of
3031 // unresolved goto statements.
3032 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3033 }
3034}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003035
3036/// \brief Set the label of the given expression to the label
3037/// identified by ID.
3038///
3039/// Depending on the order in which the label and other statements
3040/// referencing that label occur, this operation may complete
3041/// immediately (updating the statement) or it may queue the
3042/// statement to be back-patched later.
3043void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3044 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3045 if (Label != LabelStmts.end()) {
3046 // We've already seen this label, so set the label of the
3047 // label-address expression and we're done.
3048 S->setLabel(Label->second);
3049 } else {
3050 // We haven't seen this label yet, so add this label-address
3051 // expression to the set of unresolved label-address expressions.
3052 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3053 }
3054}