blob: 7018b26c2767e72a4aea4ad971a67e58a5ad8d7b [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
Douglas Gregor37f477e2009-04-22 04:56:28 +00001765 fprintf(stderr, "Looked up pre-allocated IdentifierInfo \"%s\"\n",
1766 II->getName());
1767
Douglas Gregorc713da92009-04-21 22:25:48 +00001768 // Dereferencing the iterator has the effect of populating the
1769 // IdentifierInfo node with the various declarations it needs.
1770 (void)*Pos;
1771 }
1772
Douglas Gregore01ad442009-04-18 05:55:16 +00001773 // Load the special types.
1774 Context.setBuiltinVaListType(
1775 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1776
Douglas Gregorc713da92009-04-21 22:25:48 +00001777 // If we saw the preprocessor block, read it now.
1778 if (PreprocessorBlockOffset) {
1779 SavedStreamPosition SavedPos(Stream);
1780 Stream.JumpToBit(PreprocessorBlockOffset);
1781 if (ReadPreprocessorBlock()) {
1782 Error("Malformed preprocessor block");
1783 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001784 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001785 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001786
Douglas Gregorc713da92009-04-21 22:25:48 +00001787 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001788}
1789
Douglas Gregor179cfb12009-04-10 20:39:37 +00001790/// \brief Parse the record that corresponds to a LangOptions data
1791/// structure.
1792///
1793/// This routine compares the language options used to generate the
1794/// PCH file against the language options set for the current
1795/// compilation. For each option, we classify differences between the
1796/// two compiler states as either "benign" or "important". Benign
1797/// differences don't matter, and we accept them without complaint
1798/// (and without modifying the language options). Differences between
1799/// the states for important options cause the PCH file to be
1800/// unusable, so we emit a warning and return true to indicate that
1801/// there was an error.
1802///
1803/// \returns true if the PCH file is unacceptable, false otherwise.
1804bool PCHReader::ParseLanguageOptions(
1805 const llvm::SmallVectorImpl<uint64_t> &Record) {
1806 const LangOptions &LangOpts = Context.getLangOptions();
1807#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1808#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1809 if (Record[Idx] != LangOpts.Option) { \
1810 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1811 Diag(diag::note_ignoring_pch) << FileName; \
1812 return true; \
1813 } \
1814 ++Idx
1815
1816 unsigned Idx = 0;
1817 PARSE_LANGOPT_BENIGN(Trigraphs);
1818 PARSE_LANGOPT_BENIGN(BCPLComment);
1819 PARSE_LANGOPT_BENIGN(DollarIdents);
1820 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1821 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1822 PARSE_LANGOPT_BENIGN(ImplicitInt);
1823 PARSE_LANGOPT_BENIGN(Digraphs);
1824 PARSE_LANGOPT_BENIGN(HexFloats);
1825 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1826 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1827 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1828 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1829 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1830 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1831 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1832 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1833 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1834 PARSE_LANGOPT_BENIGN(PascalStrings);
1835 PARSE_LANGOPT_BENIGN(Boolean);
1836 PARSE_LANGOPT_BENIGN(WritableStrings);
1837 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1838 diag::warn_pch_lax_vector_conversions);
1839 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1840 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1841 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1842 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1843 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1844 diag::warn_pch_thread_safe_statics);
1845 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1846 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1847 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1848 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1849 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1850 diag::warn_pch_heinous_extensions);
1851 // FIXME: Most of the options below are benign if the macro wasn't
1852 // used. Unfortunately, this means that a PCH compiled without
1853 // optimization can't be used with optimization turned on, even
1854 // though the only thing that changes is whether __OPTIMIZE__ was
1855 // defined... but if __OPTIMIZE__ never showed up in the header, it
1856 // doesn't matter. We could consider making this some special kind
1857 // of check.
1858 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1859 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1860 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1861 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1862 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1863 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1864 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1865 Diag(diag::warn_pch_gc_mode)
1866 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1867 Diag(diag::note_ignoring_pch) << FileName;
1868 return true;
1869 }
1870 ++Idx;
1871 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1872 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1873#undef PARSE_LANGOPT_IRRELEVANT
1874#undef PARSE_LANGOPT_BENIGN
1875
1876 return false;
1877}
1878
Douglas Gregorc34897d2009-04-09 22:27:44 +00001879/// \brief Read and return the type at the given offset.
1880///
1881/// This routine actually reads the record corresponding to the type
1882/// at the given offset in the bitstream. It is a helper routine for
1883/// GetType, which deals with reading type IDs.
1884QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001885 // Keep track of where we are in the stream, then jump back there
1886 // after reading this type.
1887 SavedStreamPosition SavedPosition(Stream);
1888
Douglas Gregorc34897d2009-04-09 22:27:44 +00001889 Stream.JumpToBit(Offset);
1890 RecordData Record;
1891 unsigned Code = Stream.ReadCode();
1892 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001893 case pch::TYPE_EXT_QUAL: {
1894 assert(Record.size() == 3 &&
1895 "Incorrect encoding of extended qualifier type");
1896 QualType Base = GetType(Record[0]);
1897 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1898 unsigned AddressSpace = Record[2];
1899
1900 QualType T = Base;
1901 if (GCAttr != QualType::GCNone)
1902 T = Context.getObjCGCQualType(T, GCAttr);
1903 if (AddressSpace)
1904 T = Context.getAddrSpaceQualType(T, AddressSpace);
1905 return T;
1906 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001907
Douglas Gregorc34897d2009-04-09 22:27:44 +00001908 case pch::TYPE_FIXED_WIDTH_INT: {
1909 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1910 return Context.getFixedWidthIntType(Record[0], Record[1]);
1911 }
1912
1913 case pch::TYPE_COMPLEX: {
1914 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1915 QualType ElemType = GetType(Record[0]);
1916 return Context.getComplexType(ElemType);
1917 }
1918
1919 case pch::TYPE_POINTER: {
1920 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1921 QualType PointeeType = GetType(Record[0]);
1922 return Context.getPointerType(PointeeType);
1923 }
1924
1925 case pch::TYPE_BLOCK_POINTER: {
1926 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1927 QualType PointeeType = GetType(Record[0]);
1928 return Context.getBlockPointerType(PointeeType);
1929 }
1930
1931 case pch::TYPE_LVALUE_REFERENCE: {
1932 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1933 QualType PointeeType = GetType(Record[0]);
1934 return Context.getLValueReferenceType(PointeeType);
1935 }
1936
1937 case pch::TYPE_RVALUE_REFERENCE: {
1938 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1939 QualType PointeeType = GetType(Record[0]);
1940 return Context.getRValueReferenceType(PointeeType);
1941 }
1942
1943 case pch::TYPE_MEMBER_POINTER: {
1944 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1945 QualType PointeeType = GetType(Record[0]);
1946 QualType ClassType = GetType(Record[1]);
1947 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1948 }
1949
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001950 case pch::TYPE_CONSTANT_ARRAY: {
1951 QualType ElementType = GetType(Record[0]);
1952 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1953 unsigned IndexTypeQuals = Record[2];
1954 unsigned Idx = 3;
1955 llvm::APInt Size = ReadAPInt(Record, Idx);
1956 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1957 }
1958
1959 case pch::TYPE_INCOMPLETE_ARRAY: {
1960 QualType ElementType = GetType(Record[0]);
1961 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1962 unsigned IndexTypeQuals = Record[2];
1963 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1964 }
1965
1966 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001967 QualType ElementType = GetType(Record[0]);
1968 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1969 unsigned IndexTypeQuals = Record[2];
1970 return Context.getVariableArrayType(ElementType, ReadExpr(),
1971 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001972 }
1973
1974 case pch::TYPE_VECTOR: {
1975 if (Record.size() != 2) {
1976 Error("Incorrect encoding of vector type in PCH file");
1977 return QualType();
1978 }
1979
1980 QualType ElementType = GetType(Record[0]);
1981 unsigned NumElements = Record[1];
1982 return Context.getVectorType(ElementType, NumElements);
1983 }
1984
1985 case pch::TYPE_EXT_VECTOR: {
1986 if (Record.size() != 2) {
1987 Error("Incorrect encoding of extended vector type in PCH file");
1988 return QualType();
1989 }
1990
1991 QualType ElementType = GetType(Record[0]);
1992 unsigned NumElements = Record[1];
1993 return Context.getExtVectorType(ElementType, NumElements);
1994 }
1995
1996 case pch::TYPE_FUNCTION_NO_PROTO: {
1997 if (Record.size() != 1) {
1998 Error("Incorrect encoding of no-proto function type");
1999 return QualType();
2000 }
2001 QualType ResultType = GetType(Record[0]);
2002 return Context.getFunctionNoProtoType(ResultType);
2003 }
2004
2005 case pch::TYPE_FUNCTION_PROTO: {
2006 QualType ResultType = GetType(Record[0]);
2007 unsigned Idx = 1;
2008 unsigned NumParams = Record[Idx++];
2009 llvm::SmallVector<QualType, 16> ParamTypes;
2010 for (unsigned I = 0; I != NumParams; ++I)
2011 ParamTypes.push_back(GetType(Record[Idx++]));
2012 bool isVariadic = Record[Idx++];
2013 unsigned Quals = Record[Idx++];
2014 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2015 isVariadic, Quals);
2016 }
2017
2018 case pch::TYPE_TYPEDEF:
2019 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2020 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2021
2022 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002023 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002024
2025 case pch::TYPE_TYPEOF: {
2026 if (Record.size() != 1) {
2027 Error("Incorrect encoding of typeof(type) in PCH file");
2028 return QualType();
2029 }
2030 QualType UnderlyingType = GetType(Record[0]);
2031 return Context.getTypeOfType(UnderlyingType);
2032 }
2033
2034 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002035 assert(Record.size() == 1 && "Incorrect encoding of record type");
2036 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002037
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002038 case pch::TYPE_ENUM:
2039 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2040 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2041
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002042 case pch::TYPE_OBJC_INTERFACE:
2043 // FIXME: Deserialize ObjCInterfaceType
2044 assert(false && "Cannot de-serialize ObjC interface types yet");
2045 return QualType();
2046
2047 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
2048 // FIXME: Deserialize ObjCQualifiedInterfaceType
2049 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
2050 return QualType();
2051
2052 case pch::TYPE_OBJC_QUALIFIED_ID:
2053 // FIXME: Deserialize ObjCQualifiedIdType
2054 assert(false && "Cannot de-serialize ObjC qualified id types yet");
2055 return QualType();
2056
2057 case pch::TYPE_OBJC_QUALIFIED_CLASS:
2058 // FIXME: Deserialize ObjCQualifiedClassType
2059 assert(false && "Cannot de-serialize ObjC qualified class types yet");
2060 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002061 }
2062
2063 // Suppress a GCC warning
2064 return QualType();
2065}
2066
2067/// \brief Note that we have loaded the declaration with the given
2068/// Index.
2069///
2070/// This routine notes that this declaration has already been loaded,
2071/// so that future GetDecl calls will return this declaration rather
2072/// than trying to load a new declaration.
2073inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2074 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2075 DeclAlreadyLoaded[Index] = true;
2076 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2077}
2078
2079/// \brief Read the declaration at the given offset from the PCH file.
2080Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002081 // Keep track of where we are in the stream, then jump back there
2082 // after reading this declaration.
2083 SavedStreamPosition SavedPosition(Stream);
2084
Douglas Gregorc34897d2009-04-09 22:27:44 +00002085 Decl *D = 0;
2086 Stream.JumpToBit(Offset);
2087 RecordData Record;
2088 unsigned Code = Stream.ReadCode();
2089 unsigned Idx = 0;
2090 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002091
Douglas Gregorc34897d2009-04-09 22:27:44 +00002092 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002093 case pch::DECL_ATTR:
2094 case pch::DECL_CONTEXT_LEXICAL:
2095 case pch::DECL_CONTEXT_VISIBLE:
2096 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2097 break;
2098
Douglas Gregorc34897d2009-04-09 22:27:44 +00002099 case pch::DECL_TRANSLATION_UNIT:
2100 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002101 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002102 break;
2103
2104 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002105 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002106 break;
2107 }
2108
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002109 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002110 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002111 break;
2112 }
2113
Douglas Gregor982365e2009-04-13 21:20:57 +00002114 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002115 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2116 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002117 break;
2118 }
2119
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002120 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002121 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2122 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002123 break;
2124 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002125
2126 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002127 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2128 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002129 break;
2130 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002131
Steve Naroff79ea0e02009-04-20 15:06:07 +00002132 case pch::DECL_OBJC_METHOD: {
2133 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2134 Selector(), QualType(), 0);
2135 break;
2136 }
2137
Steve Naroff97b53bd2009-04-21 15:12:33 +00002138 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002139 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2140 break;
2141 }
2142
Steve Naroff97b53bd2009-04-21 15:12:33 +00002143 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002144 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2145 ObjCIvarDecl::None);
2146 break;
2147 }
2148
Steve Naroff97b53bd2009-04-21 15:12:33 +00002149 case pch::DECL_OBJC_PROTOCOL: {
2150 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2151 break;
2152 }
2153
2154 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2155 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2156 QualType(), 0);
2157 break;
2158 }
2159
2160 case pch::DECL_OBJC_CLASS: {
2161 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2162 break;
2163 }
2164
2165 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2166 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2167 break;
2168 }
2169
2170 case pch::DECL_OBJC_CATEGORY: {
2171 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2172 break;
2173 }
2174
2175 case pch::DECL_OBJC_CATEGORY_IMPL: {
2176 // FIXME: Implement.
2177 break;
2178 }
2179
2180 case pch::DECL_OBJC_IMPLEMENTATION: {
2181 // FIXME: Implement.
2182 break;
2183 }
2184
2185 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2186 // FIXME: Implement.
2187 break;
2188 }
2189
2190 case pch::DECL_OBJC_PROPERTY: {
2191 // FIXME: Implement.
2192 break;
2193 }
2194
2195 case pch::DECL_OBJC_PROPERTY_IMPL: {
2196 // FIXME: Implement.
2197 break;
2198 }
2199
Douglas Gregor982365e2009-04-13 21:20:57 +00002200 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002201 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2202 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002203 break;
2204 }
2205
Douglas Gregorc34897d2009-04-09 22:27:44 +00002206 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002207 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2208 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002209 break;
2210 }
2211
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002212 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002213 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2214 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002215 break;
2216 }
2217
2218 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002219 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002220 QualType(), QualType(), VarDecl::None,
2221 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002222 break;
2223 }
2224
Douglas Gregor2a491792009-04-13 22:49:25 +00002225 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002226 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002227 break;
2228 }
2229
2230 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002231 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002232 break;
2233 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002234 }
2235
Douglas Gregorc713da92009-04-21 22:25:48 +00002236 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002237 if (D) {
2238 LoadedDecl(Index, D);
2239 Reader.Visit(D);
2240 }
2241
Douglas Gregorc34897d2009-04-09 22:27:44 +00002242 // If this declaration is also a declaration context, get the
2243 // offsets for its tables of lexical and visible declarations.
2244 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2245 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2246 if (Offsets.first || Offsets.second) {
2247 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2248 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2249 DeclContextOffsets[DC] = Offsets;
2250 }
2251 }
2252 assert(Idx == Record.size());
2253
2254 return D;
2255}
2256
Douglas Gregorac8f2802009-04-10 17:25:41 +00002257QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002258 unsigned Quals = ID & 0x07;
2259 unsigned Index = ID >> 3;
2260
2261 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2262 QualType T;
2263 switch ((pch::PredefinedTypeIDs)Index) {
2264 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2265 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2266 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2267
2268 case pch::PREDEF_TYPE_CHAR_U_ID:
2269 case pch::PREDEF_TYPE_CHAR_S_ID:
2270 // FIXME: Check that the signedness of CharTy is correct!
2271 T = Context.CharTy;
2272 break;
2273
2274 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2275 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2276 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2277 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2278 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2279 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2280 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2281 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2282 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2283 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2284 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2285 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2286 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2287 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2288 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2289 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2290 }
2291
2292 assert(!T.isNull() && "Unknown predefined type");
2293 return T.getQualifiedType(Quals);
2294 }
2295
2296 Index -= pch::NUM_PREDEF_TYPE_IDS;
2297 if (!TypeAlreadyLoaded[Index]) {
2298 // Load the type from the PCH file.
2299 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2300 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2301 TypeAlreadyLoaded[Index] = true;
2302 }
2303
2304 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2305}
2306
Douglas Gregorac8f2802009-04-10 17:25:41 +00002307Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002308 if (ID == 0)
2309 return 0;
2310
2311 unsigned Index = ID - 1;
2312 if (DeclAlreadyLoaded[Index])
2313 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2314
2315 // Load the declaration from the PCH file.
2316 return ReadDeclRecord(DeclOffsets[Index], Index);
2317}
2318
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002319Stmt *PCHReader::GetStmt(uint64_t Offset) {
2320 // Keep track of where we are in the stream, then jump back there
2321 // after reading this declaration.
2322 SavedStreamPosition SavedPosition(Stream);
2323
2324 Stream.JumpToBit(Offset);
2325 return ReadStmt();
2326}
2327
Douglas Gregorc34897d2009-04-09 22:27:44 +00002328bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002329 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002330 assert(DC->hasExternalLexicalStorage() &&
2331 "DeclContext has no lexical decls in storage");
2332 uint64_t Offset = DeclContextOffsets[DC].first;
2333 assert(Offset && "DeclContext has no lexical decls in storage");
2334
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002335 // Keep track of where we are in the stream, then jump back there
2336 // after reading this context.
2337 SavedStreamPosition SavedPosition(Stream);
2338
Douglas Gregorc34897d2009-04-09 22:27:44 +00002339 // Load the record containing all of the declarations lexically in
2340 // this context.
2341 Stream.JumpToBit(Offset);
2342 RecordData Record;
2343 unsigned Code = Stream.ReadCode();
2344 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002345 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002346 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2347
2348 // Load all of the declaration IDs
2349 Decls.clear();
2350 Decls.insert(Decls.end(), Record.begin(), Record.end());
2351 return false;
2352}
2353
2354bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2355 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2356 assert(DC->hasExternalVisibleStorage() &&
2357 "DeclContext has no visible decls in storage");
2358 uint64_t Offset = DeclContextOffsets[DC].second;
2359 assert(Offset && "DeclContext has no visible decls in storage");
2360
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002361 // Keep track of where we are in the stream, then jump back there
2362 // after reading this context.
2363 SavedStreamPosition SavedPosition(Stream);
2364
Douglas Gregorc34897d2009-04-09 22:27:44 +00002365 // Load the record containing all of the declarations visible in
2366 // this context.
2367 Stream.JumpToBit(Offset);
2368 RecordData Record;
2369 unsigned Code = Stream.ReadCode();
2370 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002371 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002372 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2373 if (Record.size() == 0)
2374 return false;
2375
2376 Decls.clear();
2377
2378 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002379 while (Idx < Record.size()) {
2380 Decls.push_back(VisibleDeclaration());
2381 Decls.back().Name = ReadDeclarationName(Record, Idx);
2382
Douglas Gregorc34897d2009-04-09 22:27:44 +00002383 unsigned Size = Record[Idx++];
2384 llvm::SmallVector<unsigned, 4> & LoadedDecls
2385 = Decls.back().Declarations;
2386 LoadedDecls.reserve(Size);
2387 for (unsigned I = 0; I < Size; ++I)
2388 LoadedDecls.push_back(Record[Idx++]);
2389 }
2390
2391 return false;
2392}
2393
Douglas Gregor631f6c62009-04-14 00:24:19 +00002394void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2395 if (!Consumer)
2396 return;
2397
2398 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2399 Decl *D = GetDecl(ExternalDefinitions[I]);
2400 DeclGroupRef DG(D);
2401 Consumer->HandleTopLevelDecl(DG);
2402 }
2403}
2404
Douglas Gregorc34897d2009-04-09 22:27:44 +00002405void PCHReader::PrintStats() {
2406 std::fprintf(stderr, "*** PCH Statistics:\n");
2407
2408 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2409 TypeAlreadyLoaded.end(),
2410 true);
2411 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2412 DeclAlreadyLoaded.end(),
2413 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002414 unsigned NumIdentifiersLoaded = 0;
2415 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2416 if ((IdentifierData[I] & 0x01) == 0)
2417 ++NumIdentifiersLoaded;
2418 }
2419
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2421 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002422 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002423 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2424 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002425 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2426 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2427 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2428 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002429 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2430 NumStatementsRead, TotalNumStatements,
2431 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002432 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2433 NumMacrosRead, TotalNumMacros,
2434 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002435 std::fprintf(stderr, "\n");
2436}
2437
Douglas Gregorc713da92009-04-21 22:25:48 +00002438void PCHReader::InitializeSema(Sema &S) {
2439 SemaObj = &S;
2440
2441 // FIXME: this makes sure any declarations that were deserialized
2442 // "too early" still get added to the identifier's declaration
2443 // chains.
2444 for (unsigned I = 0, N = TUDecls.size(); I != N; ++I) {
2445 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(TUDecls[I]));
2446 SemaObj->IdResolver.AddDecl(TUDecls[I]);
2447 }
2448 TUDecls.clear();
2449}
2450
2451IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2452 // Try to find this name within our on-disk hash table
2453 PCHIdentifierLookupTable *IdTable
2454 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2455 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2456 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2457 if (Pos == IdTable->end())
2458 return 0;
2459
2460 // Dereferencing the iterator has the effect of building the
2461 // IdentifierInfo node and populating it with the various
2462 // declarations it needs.
2463 return *Pos;
2464}
2465
2466void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2467 assert(ID && "Non-zero identifier ID required");
2468 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2469}
2470
Chris Lattner29241862009-04-11 21:15:38 +00002471IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002472 if (ID == 0)
2473 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002474
Douglas Gregorc713da92009-04-21 22:25:48 +00002475 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002476 Error("No identifier table in PCH file");
2477 return 0;
2478 }
Chris Lattner29241862009-04-11 21:15:38 +00002479
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002480 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002481 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002482 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002483 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002484 }
Chris Lattner29241862009-04-11 21:15:38 +00002485
2486 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002487}
2488
2489DeclarationName
2490PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2491 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2492 switch (Kind) {
2493 case DeclarationName::Identifier:
2494 return DeclarationName(GetIdentifierInfo(Record, Idx));
2495
2496 case DeclarationName::ObjCZeroArgSelector:
2497 case DeclarationName::ObjCOneArgSelector:
2498 case DeclarationName::ObjCMultiArgSelector:
2499 assert(false && "Unable to de-serialize Objective-C selectors");
2500 break;
2501
2502 case DeclarationName::CXXConstructorName:
2503 return Context.DeclarationNames.getCXXConstructorName(
2504 GetType(Record[Idx++]));
2505
2506 case DeclarationName::CXXDestructorName:
2507 return Context.DeclarationNames.getCXXDestructorName(
2508 GetType(Record[Idx++]));
2509
2510 case DeclarationName::CXXConversionFunctionName:
2511 return Context.DeclarationNames.getCXXConversionFunctionName(
2512 GetType(Record[Idx++]));
2513
2514 case DeclarationName::CXXOperatorName:
2515 return Context.DeclarationNames.getCXXOperatorName(
2516 (OverloadedOperatorKind)Record[Idx++]);
2517
2518 case DeclarationName::CXXUsingDirective:
2519 return DeclarationName::getUsingDirectiveName();
2520 }
2521
2522 // Required to silence GCC warning
2523 return DeclarationName();
2524}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002525
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002526/// \brief Read an integral value
2527llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2528 unsigned BitWidth = Record[Idx++];
2529 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2530 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2531 Idx += NumWords;
2532 return Result;
2533}
2534
2535/// \brief Read a signed integral value
2536llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2537 bool isUnsigned = Record[Idx++];
2538 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2539}
2540
Douglas Gregore2f37202009-04-14 21:55:33 +00002541/// \brief Read a floating-point value
2542llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002543 return llvm::APFloat(ReadAPInt(Record, Idx));
2544}
2545
Douglas Gregor1c507882009-04-15 21:30:51 +00002546// \brief Read a string
2547std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2548 unsigned Len = Record[Idx++];
2549 std::string Result(&Record[Idx], &Record[Idx] + Len);
2550 Idx += Len;
2551 return Result;
2552}
2553
2554/// \brief Reads attributes from the current stream position.
2555Attr *PCHReader::ReadAttributes() {
2556 unsigned Code = Stream.ReadCode();
2557 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2558 "Expected unabbreviated record"); (void)Code;
2559
2560 RecordData Record;
2561 unsigned Idx = 0;
2562 unsigned RecCode = Stream.ReadRecord(Code, Record);
2563 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2564 (void)RecCode;
2565
2566#define SIMPLE_ATTR(Name) \
2567 case Attr::Name: \
2568 New = ::new (Context) Name##Attr(); \
2569 break
2570
2571#define STRING_ATTR(Name) \
2572 case Attr::Name: \
2573 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2574 break
2575
2576#define UNSIGNED_ATTR(Name) \
2577 case Attr::Name: \
2578 New = ::new (Context) Name##Attr(Record[Idx++]); \
2579 break
2580
2581 Attr *Attrs = 0;
2582 while (Idx < Record.size()) {
2583 Attr *New = 0;
2584 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2585 bool IsInherited = Record[Idx++];
2586
2587 switch (Kind) {
2588 STRING_ATTR(Alias);
2589 UNSIGNED_ATTR(Aligned);
2590 SIMPLE_ATTR(AlwaysInline);
2591 SIMPLE_ATTR(AnalyzerNoReturn);
2592 STRING_ATTR(Annotate);
2593 STRING_ATTR(AsmLabel);
2594
2595 case Attr::Blocks:
2596 New = ::new (Context) BlocksAttr(
2597 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2598 break;
2599
2600 case Attr::Cleanup:
2601 New = ::new (Context) CleanupAttr(
2602 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2603 break;
2604
2605 SIMPLE_ATTR(Const);
2606 UNSIGNED_ATTR(Constructor);
2607 SIMPLE_ATTR(DLLExport);
2608 SIMPLE_ATTR(DLLImport);
2609 SIMPLE_ATTR(Deprecated);
2610 UNSIGNED_ATTR(Destructor);
2611 SIMPLE_ATTR(FastCall);
2612
2613 case Attr::Format: {
2614 std::string Type = ReadString(Record, Idx);
2615 unsigned FormatIdx = Record[Idx++];
2616 unsigned FirstArg = Record[Idx++];
2617 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2618 break;
2619 }
2620
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002621 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002622
2623 case Attr::IBOutletKind:
2624 New = ::new (Context) IBOutletAttr();
2625 break;
2626
2627 SIMPLE_ATTR(NoReturn);
2628 SIMPLE_ATTR(NoThrow);
2629 SIMPLE_ATTR(Nodebug);
2630 SIMPLE_ATTR(Noinline);
2631
2632 case Attr::NonNull: {
2633 unsigned Size = Record[Idx++];
2634 llvm::SmallVector<unsigned, 16> ArgNums;
2635 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2636 Idx += Size;
2637 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2638 break;
2639 }
2640
2641 SIMPLE_ATTR(ObjCException);
2642 SIMPLE_ATTR(ObjCNSObject);
2643 SIMPLE_ATTR(Overloadable);
2644 UNSIGNED_ATTR(Packed);
2645 SIMPLE_ATTR(Pure);
2646 UNSIGNED_ATTR(Regparm);
2647 STRING_ATTR(Section);
2648 SIMPLE_ATTR(StdCall);
2649 SIMPLE_ATTR(TransparentUnion);
2650 SIMPLE_ATTR(Unavailable);
2651 SIMPLE_ATTR(Unused);
2652 SIMPLE_ATTR(Used);
2653
2654 case Attr::Visibility:
2655 New = ::new (Context) VisibilityAttr(
2656 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2657 break;
2658
2659 SIMPLE_ATTR(WarnUnusedResult);
2660 SIMPLE_ATTR(Weak);
2661 SIMPLE_ATTR(WeakImport);
2662 }
2663
2664 assert(New && "Unable to decode attribute?");
2665 New->setInherited(IsInherited);
2666 New->setNext(Attrs);
2667 Attrs = New;
2668 }
2669#undef UNSIGNED_ATTR
2670#undef STRING_ATTR
2671#undef SIMPLE_ATTR
2672
2673 // The list of attributes was built backwards. Reverse the list
2674 // before returning it.
2675 Attr *PrevAttr = 0, *NextAttr = 0;
2676 while (Attrs) {
2677 NextAttr = Attrs->getNext();
2678 Attrs->setNext(PrevAttr);
2679 PrevAttr = Attrs;
2680 Attrs = NextAttr;
2681 }
2682
2683 return PrevAttr;
2684}
2685
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002686Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002687 // Within the bitstream, expressions are stored in Reverse Polish
2688 // Notation, with each of the subexpressions preceding the
2689 // expression they are stored in. To evaluate expressions, we
2690 // continue reading expressions and placing them on the stack, with
2691 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002692 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002693 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002694 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002695 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002696 llvm::SmallVector<Stmt *, 16> StmtStack;
2697 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002698 Stmt::EmptyShell Empty;
2699
Douglas Gregora151ba42009-04-14 23:32:43 +00002700 while (true) {
2701 unsigned Code = Stream.ReadCode();
2702 if (Code == llvm::bitc::END_BLOCK) {
2703 if (Stream.ReadBlockEnd()) {
2704 Error("Error at end of Source Manager block");
2705 return 0;
2706 }
2707 break;
2708 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002709
Douglas Gregora151ba42009-04-14 23:32:43 +00002710 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2711 // No known subblocks, always skip them.
2712 Stream.ReadSubBlockID();
2713 if (Stream.SkipBlock()) {
2714 Error("Malformed block record");
2715 return 0;
2716 }
2717 continue;
2718 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002719
Douglas Gregora151ba42009-04-14 23:32:43 +00002720 if (Code == llvm::bitc::DEFINE_ABBREV) {
2721 Stream.ReadAbbrevRecord();
2722 continue;
2723 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002724
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002725 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002726 Idx = 0;
2727 Record.clear();
2728 bool Finished = false;
2729 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002730 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002731 Finished = true;
2732 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002733
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002734 case pch::STMT_NULL_PTR:
2735 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002736 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002737
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002738 case pch::STMT_NULL:
2739 S = new (Context) NullStmt(Empty);
2740 break;
2741
2742 case pch::STMT_COMPOUND:
2743 S = new (Context) CompoundStmt(Empty);
2744 break;
2745
2746 case pch::STMT_CASE:
2747 S = new (Context) CaseStmt(Empty);
2748 break;
2749
2750 case pch::STMT_DEFAULT:
2751 S = new (Context) DefaultStmt(Empty);
2752 break;
2753
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002754 case pch::STMT_LABEL:
2755 S = new (Context) LabelStmt(Empty);
2756 break;
2757
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002758 case pch::STMT_IF:
2759 S = new (Context) IfStmt(Empty);
2760 break;
2761
2762 case pch::STMT_SWITCH:
2763 S = new (Context) SwitchStmt(Empty);
2764 break;
2765
Douglas Gregora6b503f2009-04-17 00:16:09 +00002766 case pch::STMT_WHILE:
2767 S = new (Context) WhileStmt(Empty);
2768 break;
2769
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002770 case pch::STMT_DO:
2771 S = new (Context) DoStmt(Empty);
2772 break;
2773
2774 case pch::STMT_FOR:
2775 S = new (Context) ForStmt(Empty);
2776 break;
2777
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002778 case pch::STMT_GOTO:
2779 S = new (Context) GotoStmt(Empty);
2780 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002781
2782 case pch::STMT_INDIRECT_GOTO:
2783 S = new (Context) IndirectGotoStmt(Empty);
2784 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002785
Douglas Gregora6b503f2009-04-17 00:16:09 +00002786 case pch::STMT_CONTINUE:
2787 S = new (Context) ContinueStmt(Empty);
2788 break;
2789
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002790 case pch::STMT_BREAK:
2791 S = new (Context) BreakStmt(Empty);
2792 break;
2793
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002794 case pch::STMT_RETURN:
2795 S = new (Context) ReturnStmt(Empty);
2796 break;
2797
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002798 case pch::STMT_DECL:
2799 S = new (Context) DeclStmt(Empty);
2800 break;
2801
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002802 case pch::STMT_ASM:
2803 S = new (Context) AsmStmt(Empty);
2804 break;
2805
Douglas Gregora151ba42009-04-14 23:32:43 +00002806 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002807 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002808 break;
2809
2810 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002811 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002812 break;
2813
2814 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002815 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002816 break;
2817
2818 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002819 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002820 break;
2821
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002822 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002823 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002824 break;
2825
Douglas Gregor596e0932009-04-15 16:35:07 +00002826 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002827 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002828 Record[PCHStmtReader::NumExprFields + 1]);
2829 break;
2830
Douglas Gregora151ba42009-04-14 23:32:43 +00002831 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002832 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002833 break;
2834
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002835 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002836 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002837 break;
2838
Douglas Gregor12d74052009-04-15 15:58:59 +00002839 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002840 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002841 break;
2842
2843 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002844 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002845 break;
2846
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002847 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002848 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002849 break;
2850
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002851 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002852 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002853 break;
2854
2855 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002856 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002857 break;
2858
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002859 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002860 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002861 break;
2862
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002863 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002864 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002865 break;
2866
2867 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002868 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002869 break;
2870
Douglas Gregora151ba42009-04-14 23:32:43 +00002871 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002872 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002873 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002874
2875 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002876 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002877 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002878
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002879 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002880 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002881 break;
2882
Douglas Gregorec0b8292009-04-15 23:02:49 +00002883 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002884 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002885 break;
2886
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002887 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002888 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002889 break;
2890
2891 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002892 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002893 Record[PCHStmtReader::NumExprFields] - 1);
2894
2895 break;
2896
2897 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002898 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002899 break;
2900
Douglas Gregorec0b8292009-04-15 23:02:49 +00002901 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002902 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002903 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002904
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002905 case pch::EXPR_ADDR_LABEL:
2906 S = new (Context) AddrLabelExpr(Empty);
2907 break;
2908
Douglas Gregoreca12f62009-04-17 19:05:30 +00002909 case pch::EXPR_STMT:
2910 S = new (Context) StmtExpr(Empty);
2911 break;
2912
Douglas Gregor209d4622009-04-15 23:33:31 +00002913 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002914 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002915 break;
2916
2917 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002918 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002919 break;
2920
2921 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002922 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002923 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002924
2925 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002926 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002927 break;
2928
Douglas Gregore246b742009-04-17 19:21:43 +00002929 case pch::EXPR_BLOCK:
2930 S = new (Context) BlockExpr(Empty);
2931 break;
2932
Douglas Gregor725e94b2009-04-16 00:01:45 +00002933 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002934 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002935 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002936 }
2937
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002938 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002939 if (Finished)
2940 break;
2941
Douglas Gregor456e0952009-04-17 22:13:46 +00002942 ++NumStatementsRead;
2943
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002944 if (S) {
2945 unsigned NumSubStmts = Reader.Visit(S);
2946 while (NumSubStmts > 0) {
2947 StmtStack.pop_back();
2948 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002949 }
2950 }
2951
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002952 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002953 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002954 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002955 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002956 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002957 return StmtStack.back();
2958}
2959
2960Expr *PCHReader::ReadExpr() {
2961 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002962}
2963
Douglas Gregor179cfb12009-04-10 20:39:37 +00002964DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002965 return Diag(SourceLocation(), DiagID);
2966}
2967
2968DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2969 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002970 Context.getSourceManager()),
2971 DiagID);
2972}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002973
Douglas Gregorc713da92009-04-21 22:25:48 +00002974/// \brief Retrieve the identifier table associated with the
2975/// preprocessor.
2976IdentifierTable &PCHReader::getIdentifierTable() {
2977 return PP.getIdentifierTable();
2978}
2979
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002980/// \brief Record that the given ID maps to the given switch-case
2981/// statement.
2982void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2983 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2984 SwitchCaseStmts[ID] = SC;
2985}
2986
2987/// \brief Retrieve the switch-case statement with the given ID.
2988SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2989 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2990 return SwitchCaseStmts[ID];
2991}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002992
2993/// \brief Record that the given label statement has been
2994/// deserialized and has the given ID.
2995void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2996 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2997 "Deserialized label twice");
2998 LabelStmts[ID] = S;
2999
3000 // If we've already seen any goto statements that point to this
3001 // label, resolve them now.
3002 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3003 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3004 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3005 Goto->second->setLabel(S);
3006 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003007
3008 // If we've already seen any address-label statements that point to
3009 // this label, resolve them now.
3010 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3011 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3012 = UnresolvedAddrLabelExprs.equal_range(ID);
3013 for (AddrLabelIter AddrLabel = AddrLabels.first;
3014 AddrLabel != AddrLabels.second; ++AddrLabel)
3015 AddrLabel->second->setLabel(S);
3016 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003017}
3018
3019/// \brief Set the label of the given statement to the label
3020/// identified by ID.
3021///
3022/// Depending on the order in which the label and other statements
3023/// referencing that label occur, this operation may complete
3024/// immediately (updating the statement) or it may queue the
3025/// statement to be back-patched later.
3026void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3027 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3028 if (Label != LabelStmts.end()) {
3029 // We've already seen this label, so set the label of the goto and
3030 // we're done.
3031 S->setLabel(Label->second);
3032 } else {
3033 // We haven't seen this label yet, so add this goto to the set of
3034 // unresolved goto statements.
3035 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3036 }
3037}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003038
3039/// \brief Set the label of the given expression to the label
3040/// identified by ID.
3041///
3042/// Depending on the order in which the label and other statements
3043/// referencing that label occur, this operation may complete
3044/// immediately (updating the statement) or it may queue the
3045/// statement to be back-patched later.
3046void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3047 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3048 if (Label != LabelStmts.end()) {
3049 // We've already seen this label, so set the label of the
3050 // label-address expression and we're done.
3051 S->setLabel(Label->second);
3052 } else {
3053 // We haven't seen this label yet, so add this label-address
3054 // expression to the set of unresolved label-address expressions.
3055 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3056 }
3057}