blob: e3413cbcd1d21366d6afd4e6d5ad5a33308bca16 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000039namespace {
40 /// \brief Helper class that saves the current stream position and
41 /// then restores it when destroyed.
42 struct VISIBILITY_HIDDEN SavedStreamPosition {
43 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
44 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
45
46 ~SavedStreamPosition() {
47 Stream.JumpToBit(Offset);
48 }
49
50 private:
51 llvm::BitstreamReader &Stream;
52 uint64_t Offset;
53 };
54}
55
Douglas Gregorc34897d2009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 PCHReader &Reader;
63 const PCHReader::RecordData &Record;
64 unsigned &Idx;
65
66 public:
67 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
68 unsigned &Idx)
69 : Reader(Reader), Record(Record), Idx(Idx) { }
70
71 void VisitDecl(Decl *D);
72 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
73 void VisitNamedDecl(NamedDecl *ND);
74 void VisitTypeDecl(TypeDecl *TD);
75 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000093 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
94 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
95 void VisitObjCClassDecl(ObjCClassDecl *D);
96 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
97 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
98 void VisitObjCImplDecl(ObjCImplDecl *D);
99 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
100 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
101 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
102 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
103 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000104 };
105}
106
107void PCHDeclReader::VisitDecl(Decl *D) {
108 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
109 D->setLexicalDeclContext(
110 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
112 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000115 D->setImplicit(Record[Idx++]);
116 D->setAccess((AccessSpecifier)Record[Idx++]);
117}
118
119void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
120 VisitDecl(TU);
121}
122
123void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
124 VisitDecl(ND);
125 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
126}
127
128void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
129 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000134 // Note that we cannot use VisitTypeDecl here, because we need to
135 // set the underlying type of the typedef *before* we try to read
136 // the type associated with the TypedefDecl.
137 VisitNamedDecl(TD);
138 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
139 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
140 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000141}
142
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000143void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
144 VisitTypeDecl(TD);
145 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
146 TD->setDefinition(Record[Idx++]);
147 TD->setTypedefForAnonDecl(
148 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
149}
150
151void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
152 VisitTagDecl(ED);
153 ED->setIntegerType(Reader.GetType(Record[Idx++]));
154}
155
Douglas Gregor982365e2009-04-13 21:20:57 +0000156void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
157 VisitTagDecl(RD);
158 RD->setHasFlexibleArrayMember(Record[Idx++]);
159 RD->setAnonymousStructOrUnion(Record[Idx++]);
160}
161
Douglas Gregorc34897d2009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000178 FD->setPreviousDeclaration(
179 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
180 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
181 FD->setInline(Record[Idx++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff79ea0e02009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff7333b492009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000230 unsigned NumIvars = Record[Idx++];
231 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
232 IVars.reserve(NumIvars);
233 for (unsigned I = 0; I != NumIvars; ++I)
234 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
235 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
236
237 ID->setForwardDecl(Record[Idx++]);
238 ID->setImplicitInterfaceDecl(Record[Idx++]);
239 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
240 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000242 // FIXME: add protocols, categories.
243}
244
245void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
246 VisitFieldDecl(IVD);
247 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
248}
249
Steve Naroff97b53bd2009-04-21 15:12:33 +0000250void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
251 VisitObjCContainerDecl(PD);
252 PD->setForwardDecl(Record[Idx++]);
253 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
259 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
260}
261
262void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
263 VisitFieldDecl(FD);
264}
265
266void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
267 VisitDecl(CD);
268 unsigned NumClassRefs = Record[Idx++];
269 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
270 ClassRefs.reserve(NumClassRefs);
271 for (unsigned I = 0; I != NumClassRefs; ++I)
272 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
274}
275
276void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
277 VisitDecl(FPD);
278 unsigned NumProtoRefs = Record[Idx++];
279 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
280 ProtoRefs.reserve(NumProtoRefs);
281 for (unsigned I = 0; I != NumProtoRefs; ++I)
282 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
283 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
284}
285
286void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
287 VisitObjCContainerDecl(CD);
288 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
289 unsigned NumProtoRefs = Record[Idx++];
290 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
291 ProtoRefs.reserve(NumProtoRefs);
292 for (unsigned I = 0; I != NumProtoRefs; ++I)
293 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
295 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
296 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
297}
298
299void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
300 VisitNamedDecl(CAD);
301 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
302}
303
304void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
305 VisitNamedDecl(D);
306 // FIXME: Implement.
307}
308
309void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
310 VisitDecl(D);
311 // FIXME: Implement.
312}
313
314void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
315 VisitObjCImplDecl(D);
316 // FIXME: Implement.
317}
318
319void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
320 VisitObjCImplDecl(D);
321 // FIXME: Implement.
322}
323
324
325void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
326 VisitDecl(D);
327 // FIXME: Implement.
328}
329
Douglas Gregor982365e2009-04-13 21:20:57 +0000330void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
331 VisitValueDecl(FD);
332 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000333 if (Record[Idx++])
334 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000335}
336
Douglas Gregorc34897d2009-04-09 22:27:44 +0000337void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
338 VisitValueDecl(VD);
339 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
340 VD->setThreadSpecified(Record[Idx++]);
341 VD->setCXXDirectInitializer(Record[Idx++]);
342 VD->setDeclaredInCondition(Record[Idx++]);
343 VD->setPreviousDeclaration(
344 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
345 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000346 if (Record[Idx++])
347 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000348}
349
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000350void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
351 VisitVarDecl(PD);
352 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000353 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000354}
355
356void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
357 VisitParmVarDecl(PD);
358 PD->setOriginalType(Reader.GetType(Record[Idx++]));
359}
360
Douglas Gregor2a491792009-04-13 22:49:25 +0000361void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
362 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000363 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000364}
365
366void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
367 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000368 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000369 unsigned NumParams = Record[Idx++];
370 llvm::SmallVector<ParmVarDecl *, 16> Params;
371 Params.reserve(NumParams);
372 for (unsigned I = 0; I != NumParams; ++I)
373 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
374 BD->setParams(Reader.getContext(), &Params[0], NumParams);
375}
376
Douglas Gregorc34897d2009-04-09 22:27:44 +0000377std::pair<uint64_t, uint64_t>
378PCHDeclReader::VisitDeclContext(DeclContext *DC) {
379 uint64_t LexicalOffset = Record[Idx++];
380 uint64_t VisibleOffset = 0;
381 if (DC->getPrimaryContext() == DC)
382 VisibleOffset = Record[Idx++];
383 return std::make_pair(LexicalOffset, VisibleOffset);
384}
385
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000386//===----------------------------------------------------------------------===//
387// Statement/expression deserialization
388//===----------------------------------------------------------------------===//
389namespace {
390 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000391 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000392 PCHReader &Reader;
393 const PCHReader::RecordData &Record;
394 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000395 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000396
397 public:
398 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000399 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
400 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000401
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000402 /// \brief The number of record fields required for the Stmt class
403 /// itself.
404 static const unsigned NumStmtFields = 0;
405
Douglas Gregor596e0932009-04-15 16:35:07 +0000406 /// \brief The number of record fields required for the Expr class
407 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000408 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000409
Douglas Gregora151ba42009-04-14 23:32:43 +0000410 // Each of the Visit* functions reads in part of the expression
411 // from the given record and the current expression stack, then
412 // return the total number of operands that it read from the
413 // expression stack.
414
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000415 unsigned VisitStmt(Stmt *S);
416 unsigned VisitNullStmt(NullStmt *S);
417 unsigned VisitCompoundStmt(CompoundStmt *S);
418 unsigned VisitSwitchCase(SwitchCase *S);
419 unsigned VisitCaseStmt(CaseStmt *S);
420 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000421 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000422 unsigned VisitIfStmt(IfStmt *S);
423 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000424 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000425 unsigned VisitDoStmt(DoStmt *S);
426 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000427 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000428 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000429 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000430 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000431 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000432 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000433 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000434 unsigned VisitExpr(Expr *E);
435 unsigned VisitPredefinedExpr(PredefinedExpr *E);
436 unsigned VisitDeclRefExpr(DeclRefExpr *E);
437 unsigned VisitIntegerLiteral(IntegerLiteral *E);
438 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000439 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000440 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000441 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000442 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000443 unsigned VisitUnaryOperator(UnaryOperator *E);
444 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000445 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000446 unsigned VisitCallExpr(CallExpr *E);
447 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000448 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000449 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000450 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
451 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000452 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000453 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
454 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000455 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000456 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000457 unsigned VisitInitListExpr(InitListExpr *E);
458 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
459 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000460 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000461 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000462 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000463 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
464 unsigned VisitChooseExpr(ChooseExpr *E);
465 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000466 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000467 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000468 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000469 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000470 };
471}
472
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000473unsigned PCHStmtReader::VisitStmt(Stmt *S) {
474 assert(Idx == NumStmtFields && "Incorrect statement field count");
475 return 0;
476}
477
478unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
479 VisitStmt(S);
480 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
481 return 0;
482}
483
484unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
485 VisitStmt(S);
486 unsigned NumStmts = Record[Idx++];
487 S->setStmts(Reader.getContext(),
488 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
489 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
490 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
491 return NumStmts;
492}
493
494unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
495 VisitStmt(S);
496 Reader.RecordSwitchCaseID(S, Record[Idx++]);
497 return 0;
498}
499
500unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
501 VisitSwitchCase(S);
502 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
503 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
504 S->setSubStmt(StmtStack.back());
505 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
506 return 3;
507}
508
509unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
510 VisitSwitchCase(S);
511 S->setSubStmt(StmtStack.back());
512 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
513 return 1;
514}
515
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000516unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
517 VisitStmt(S);
518 S->setID(Reader.GetIdentifierInfo(Record, Idx));
519 S->setSubStmt(StmtStack.back());
520 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
521 Reader.RecordLabelStmt(S, Record[Idx++]);
522 return 1;
523}
524
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000525unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
526 VisitStmt(S);
527 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
528 S->setThen(StmtStack[StmtStack.size() - 2]);
529 S->setElse(StmtStack[StmtStack.size() - 1]);
530 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
531 return 3;
532}
533
534unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
535 VisitStmt(S);
536 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
537 S->setBody(StmtStack.back());
538 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
539 SwitchCase *PrevSC = 0;
540 for (unsigned N = Record.size(); Idx != N; ++Idx) {
541 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
542 if (PrevSC)
543 PrevSC->setNextSwitchCase(SC);
544 else
545 S->setSwitchCaseList(SC);
546 PrevSC = SC;
547 }
548 return 2;
549}
550
Douglas Gregora6b503f2009-04-17 00:16:09 +0000551unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
552 VisitStmt(S);
553 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
554 S->setBody(StmtStack.back());
555 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 return 2;
557}
558
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000559unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
560 VisitStmt(S);
561 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
562 S->setBody(StmtStack.back());
563 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
564 return 2;
565}
566
567unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
568 VisitStmt(S);
569 S->setInit(StmtStack[StmtStack.size() - 4]);
570 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
571 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
572 S->setBody(StmtStack.back());
573 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
574 return 4;
575}
576
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000577unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
578 VisitStmt(S);
579 Reader.SetLabelOf(S, Record[Idx++]);
580 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
582 return 0;
583}
584
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000585unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
586 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000587 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000588 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
589 return 1;
590}
591
Douglas Gregora6b503f2009-04-17 00:16:09 +0000592unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
593 VisitStmt(S);
594 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595 return 0;
596}
597
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000598unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
599 VisitStmt(S);
600 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
601 return 0;
602}
603
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000604unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
605 VisitStmt(S);
606 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
607 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
608 return 1;
609}
610
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000611unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
612 VisitStmt(S);
613 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
614 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
615
616 if (Idx + 1 == Record.size()) {
617 // Single declaration
618 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
619 } else {
620 llvm::SmallVector<Decl *, 16> Decls;
621 Decls.reserve(Record.size() - Idx);
622 for (unsigned N = Record.size(); Idx != N; ++Idx)
623 Decls.push_back(Reader.GetDecl(Record[Idx]));
624 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
625 &Decls[0], Decls.size())));
626 }
627 return 0;
628}
629
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000630unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
631 VisitStmt(S);
632 unsigned NumOutputs = Record[Idx++];
633 unsigned NumInputs = Record[Idx++];
634 unsigned NumClobbers = Record[Idx++];
635 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
636 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 S->setVolatile(Record[Idx++]);
638 S->setSimple(Record[Idx++]);
639
640 unsigned StackIdx
641 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
642 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
643
644 // Outputs and inputs
645 llvm::SmallVector<std::string, 16> Names;
646 llvm::SmallVector<StringLiteral*, 16> Constraints;
647 llvm::SmallVector<Stmt*, 16> Exprs;
648 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
649 Names.push_back(Reader.ReadString(Record, Idx));
650 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
651 Exprs.push_back(StmtStack[StackIdx++]);
652 }
653 S->setOutputsAndInputs(NumOutputs, NumInputs,
654 &Names[0], &Constraints[0], &Exprs[0]);
655
656 // Constraints
657 llvm::SmallVector<StringLiteral*, 16> Clobbers;
658 for (unsigned I = 0; I != NumClobbers; ++I)
659 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
660 S->setClobbers(&Clobbers[0], NumClobbers);
661
662 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
663 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
664}
665
Douglas Gregora151ba42009-04-14 23:32:43 +0000666unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000667 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000668 E->setType(Reader.GetType(Record[Idx++]));
669 E->setTypeDependent(Record[Idx++]);
670 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000671 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000672 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000673}
674
Douglas Gregora151ba42009-04-14 23:32:43 +0000675unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000676 VisitExpr(E);
677 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
678 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000679 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000680}
681
Douglas Gregora151ba42009-04-14 23:32:43 +0000682unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000683 VisitExpr(E);
684 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
685 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000686 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000687}
688
Douglas Gregora151ba42009-04-14 23:32:43 +0000689unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000690 VisitExpr(E);
691 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
692 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000693 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000694}
695
Douglas Gregora151ba42009-04-14 23:32:43 +0000696unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000697 VisitExpr(E);
698 E->setValue(Reader.ReadAPFloat(Record, Idx));
699 E->setExact(Record[Idx++]);
700 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000701 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000702}
703
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000704unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
705 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000706 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000707 return 1;
708}
709
Douglas Gregor596e0932009-04-15 16:35:07 +0000710unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
711 VisitExpr(E);
712 unsigned Len = Record[Idx++];
713 assert(Record[Idx] == E->getNumConcatenated() &&
714 "Wrong number of concatenated tokens!");
715 ++Idx;
716 E->setWide(Record[Idx++]);
717
718 // Read string data
719 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
720 E->setStrData(Reader.getContext(), &Str[0], Len);
721 Idx += Len;
722
723 // Read source locations
724 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
725 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
726
727 return 0;
728}
729
Douglas Gregora151ba42009-04-14 23:32:43 +0000730unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000731 VisitExpr(E);
732 E->setValue(Record[Idx++]);
733 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
734 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000735 return 0;
736}
737
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000738unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
739 VisitExpr(E);
740 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
741 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000742 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000743 return 1;
744}
745
Douglas Gregor12d74052009-04-15 15:58:59 +0000746unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
747 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000748 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000749 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
750 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
751 return 1;
752}
753
754unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
755 VisitExpr(E);
756 E->setSizeof(Record[Idx++]);
757 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000758 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000759 ++Idx;
760 } else {
761 E->setArgument(Reader.GetType(Record[Idx++]));
762 }
763 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
764 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
765 return E->isArgumentType()? 0 : 1;
766}
767
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000768unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
769 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000770 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
771 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000772 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
773 return 2;
774}
775
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000776unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
777 VisitExpr(E);
778 E->setNumArgs(Reader.getContext(), Record[Idx++]);
779 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000780 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000781 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000782 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000783 return E->getNumArgs() + 1;
784}
785
786unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
787 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000788 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000789 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
790 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
791 E->setArrow(Record[Idx++]);
792 return 1;
793}
794
Douglas Gregora151ba42009-04-14 23:32:43 +0000795unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
796 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000797 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000798 return 1;
799}
800
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000801unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
802 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000803 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
804 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000805 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
806 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
807 return 2;
808}
809
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000810unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
811 VisitBinaryOperator(E);
812 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
813 E->setComputationResultType(Reader.GetType(Record[Idx++]));
814 return 2;
815}
816
817unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
818 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000819 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
820 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
821 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000822 return 3;
823}
824
Douglas Gregora151ba42009-04-14 23:32:43 +0000825unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
826 VisitCastExpr(E);
827 E->setLvalueCast(Record[Idx++]);
828 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000829}
830
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000831unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
832 VisitCastExpr(E);
833 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
834 return 1;
835}
836
837unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
838 VisitExplicitCastExpr(E);
839 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
840 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
841 return 1;
842}
843
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000844unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
845 VisitExpr(E);
846 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000847 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000848 E->setFileScope(Record[Idx++]);
849 return 1;
850}
851
Douglas Gregorec0b8292009-04-15 23:02:49 +0000852unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
853 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000854 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000855 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
856 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
857 return 1;
858}
859
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000860unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
861 VisitExpr(E);
862 unsigned NumInits = Record[Idx++];
863 E->reserveInits(NumInits);
864 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000865 E->updateInit(I,
866 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
867 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000868 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
869 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 E->setInitializedFieldInUnion(
871 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
872 E->sawArrayRangeDesignator(Record[Idx++]);
873 return NumInits + 1;
874}
875
876unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
877 typedef DesignatedInitExpr::Designator Designator;
878
879 VisitExpr(E);
880 unsigned NumSubExprs = Record[Idx++];
881 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
882 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000883 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000884 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
885 E->setGNUSyntax(Record[Idx++]);
886
887 llvm::SmallVector<Designator, 4> Designators;
888 while (Idx < Record.size()) {
889 switch ((pch::DesignatorTypes)Record[Idx++]) {
890 case pch::DESIG_FIELD_DECL: {
891 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
892 SourceLocation DotLoc
893 = SourceLocation::getFromRawEncoding(Record[Idx++]);
894 SourceLocation FieldLoc
895 = SourceLocation::getFromRawEncoding(Record[Idx++]);
896 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
897 FieldLoc));
898 Designators.back().setField(Field);
899 break;
900 }
901
902 case pch::DESIG_FIELD_NAME: {
903 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
904 SourceLocation DotLoc
905 = SourceLocation::getFromRawEncoding(Record[Idx++]);
906 SourceLocation FieldLoc
907 = SourceLocation::getFromRawEncoding(Record[Idx++]);
908 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
909 break;
910 }
911
912 case pch::DESIG_ARRAY: {
913 unsigned Index = Record[Idx++];
914 SourceLocation LBracketLoc
915 = SourceLocation::getFromRawEncoding(Record[Idx++]);
916 SourceLocation RBracketLoc
917 = SourceLocation::getFromRawEncoding(Record[Idx++]);
918 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
919 break;
920 }
921
922 case pch::DESIG_ARRAY_RANGE: {
923 unsigned Index = Record[Idx++];
924 SourceLocation LBracketLoc
925 = SourceLocation::getFromRawEncoding(Record[Idx++]);
926 SourceLocation EllipsisLoc
927 = SourceLocation::getFromRawEncoding(Record[Idx++]);
928 SourceLocation RBracketLoc
929 = SourceLocation::getFromRawEncoding(Record[Idx++]);
930 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
931 RBracketLoc));
932 break;
933 }
934 }
935 }
936 E->setDesignators(&Designators[0], Designators.size());
937
938 return NumSubExprs;
939}
940
941unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
942 VisitExpr(E);
943 return 0;
944}
945
Douglas Gregorec0b8292009-04-15 23:02:49 +0000946unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
947 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000948 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000949 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
950 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
951 return 1;
952}
953
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000954unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
955 VisitExpr(E);
956 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
957 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
958 Reader.SetLabelOf(E, Record[Idx++]);
959 return 0;
960}
961
Douglas Gregoreca12f62009-04-17 19:05:30 +0000962unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
963 VisitExpr(E);
964 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
965 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
966 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
967 return 1;
968}
969
Douglas Gregor209d4622009-04-15 23:33:31 +0000970unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
971 VisitExpr(E);
972 E->setArgType1(Reader.GetType(Record[Idx++]));
973 E->setArgType2(Reader.GetType(Record[Idx++]));
974 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
975 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
976 return 0;
977}
978
979unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
980 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000981 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
982 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
983 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000984 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
985 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 return 3;
987}
988
989unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
990 VisitExpr(E);
991 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 return 0;
993}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000994
Douglas Gregor725e94b2009-04-16 00:01:45 +0000995unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
996 VisitExpr(E);
997 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000998 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000999 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1000 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1001 return NumExprs;
1002}
1003
Douglas Gregore246b742009-04-17 19:21:43 +00001004unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1005 VisitExpr(E);
1006 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1007 E->setHasBlockDeclRefExprs(Record[Idx++]);
1008 return 0;
1009}
1010
Douglas Gregor725e94b2009-04-16 00:01:45 +00001011unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1012 VisitExpr(E);
1013 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1014 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 E->setByRef(Record[Idx++]);
1016 return 0;
1017}
1018
Chris Lattner80f83c62009-04-22 05:57:30 +00001019unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1020 VisitExpr(E);
1021 E->setEncodedType(Reader.GetType(Record[Idx++]));
1022 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1023 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1024 return 0;
1025}
1026
1027
Douglas Gregorc713da92009-04-21 22:25:48 +00001028//===----------------------------------------------------------------------===//
1029// PCH reader implementation
1030//===----------------------------------------------------------------------===//
1031
1032namespace {
1033class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1034 PCHReader &Reader;
1035
1036 // If we know the IdentifierInfo in advance, it is here and we will
1037 // not build a new one. Used when deserializing information about an
1038 // identifier that was constructed before the PCH file was read.
1039 IdentifierInfo *KnownII;
1040
1041public:
1042 typedef IdentifierInfo * data_type;
1043
1044 typedef const std::pair<const char*, unsigned> external_key_type;
1045
1046 typedef external_key_type internal_key_type;
1047
1048 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1049 : Reader(Reader), KnownII(II) { }
1050
1051 static bool EqualKey(const internal_key_type& a,
1052 const internal_key_type& b) {
1053 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1054 : false;
1055 }
1056
1057 static unsigned ComputeHash(const internal_key_type& a) {
1058 return BernsteinHash(a.first, a.second);
1059 }
1060
1061 // This hopefully will just get inlined and removed by the optimizer.
1062 static const internal_key_type&
1063 GetInternalKey(const external_key_type& x) { return x; }
1064
1065 static std::pair<unsigned, unsigned>
1066 ReadKeyDataLength(const unsigned char*& d) {
1067 using namespace clang::io;
1068 unsigned KeyLen = ReadUnalignedLE16(d);
1069 unsigned DataLen = ReadUnalignedLE16(d);
1070 return std::make_pair(KeyLen, DataLen);
1071 }
1072
1073 static std::pair<const char*, unsigned>
1074 ReadKey(const unsigned char* d, unsigned n) {
1075 assert(n >= 2 && d[n-1] == '\0');
1076 return std::make_pair((const char*) d, n-1);
1077 }
1078
1079 IdentifierInfo *ReadData(const internal_key_type& k,
1080 const unsigned char* d,
1081 unsigned DataLen) {
1082 using namespace clang::io;
1083 uint32_t Bits = ReadUnalignedLE32(d); // FIXME: use these?
1084 (void)Bits;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001085 bool hasMacroDefinition = (Bits >> 3) & 0x01;
1086
Douglas Gregorc713da92009-04-21 22:25:48 +00001087 pch::IdentID ID = ReadUnalignedLE32(d);
1088 DataLen -= 8;
1089
1090 // Build the IdentifierInfo itself and link the identifier ID with
1091 // the new IdentifierInfo.
1092 IdentifierInfo *II = KnownII;
1093 if (!II)
1094 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1095 k.first, k.first + k.second);
1096 Reader.SetIdentifierInfo(ID, II);
1097
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001098 // If this identifier is a macro, deserialize the macro
1099 // definition.
1100 if (hasMacroDefinition) {
1101 uint32_t Offset = ReadUnalignedLE64(d);
1102 Reader.ReadMacroRecord(Offset);
1103 DataLen -= 8;
1104 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001105
1106 // Read all of the declarations visible at global scope with this
1107 // name.
1108 Sema *SemaObj = Reader.getSema();
1109 while (DataLen > 0) {
1110 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1111
1112 if (SemaObj) {
1113 // Introduce this declaration into the translation-unit scope
1114 // and add it to the declaration chain for this identifier, so
1115 // that (unqualified) name lookup will find it.
1116 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1117 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1118 } else {
1119 // Queue this declaration so that it will be added to the
1120 // translation unit scope and identifier's declaration chain
1121 // once a Sema object is known.
1122 // FIXME: This is a temporary hack. It will go away once we have
1123 // lazy deserialization of macros.
1124 Reader.TUDecls.push_back(D);
1125 }
1126
1127 DataLen -= 4;
1128 }
1129 return II;
1130 }
1131};
1132
1133} // end anonymous namespace
1134
1135/// \brief The on-disk hash table used to contain information about
1136/// all of the identifiers in the program.
1137typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1138 PCHIdentifierLookupTable;
1139
Douglas Gregorc34897d2009-04-09 22:27:44 +00001140// FIXME: use the diagnostics machinery
1141static bool Error(const char *Str) {
1142 std::fprintf(stderr, "%s\n", Str);
1143 return true;
1144}
1145
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001146/// \brief Check the contents of the predefines buffer against the
1147/// contents of the predefines buffer used to build the PCH file.
1148///
1149/// The contents of the two predefines buffers should be the same. If
1150/// not, then some command-line option changed the preprocessor state
1151/// and we must reject the PCH file.
1152///
1153/// \param PCHPredef The start of the predefines buffer in the PCH
1154/// file.
1155///
1156/// \param PCHPredefLen The length of the predefines buffer in the PCH
1157/// file.
1158///
1159/// \param PCHBufferID The FileID for the PCH predefines buffer.
1160///
1161/// \returns true if there was a mismatch (in which case the PCH file
1162/// should be ignored), or false otherwise.
1163bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1164 unsigned PCHPredefLen,
1165 FileID PCHBufferID) {
1166 const char *Predef = PP.getPredefines().c_str();
1167 unsigned PredefLen = PP.getPredefines().size();
1168
1169 // If the two predefines buffers compare equal, we're done!.
1170 if (PredefLen == PCHPredefLen &&
1171 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1172 return false;
1173
1174 // The predefines buffers are different. Produce a reasonable
1175 // diagnostic showing where they are different.
1176
1177 // The source locations (potentially in the two different predefines
1178 // buffers)
1179 SourceLocation Loc1, Loc2;
1180 SourceManager &SourceMgr = PP.getSourceManager();
1181
1182 // Create a source buffer for our predefines string, so
1183 // that we can build a diagnostic that points into that
1184 // source buffer.
1185 FileID BufferID;
1186 if (Predef && Predef[0]) {
1187 llvm::MemoryBuffer *Buffer
1188 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1189 "<built-in>");
1190 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1191 }
1192
1193 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1194 std::pair<const char *, const char *> Locations
1195 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1196
1197 if (Locations.first != Predef + MinLen) {
1198 // We found the location in the two buffers where there is a
1199 // difference. Form source locations to point there (in both
1200 // buffers).
1201 unsigned Offset = Locations.first - Predef;
1202 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1203 .getFileLocWithOffset(Offset);
1204 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1205 .getFileLocWithOffset(Offset);
1206 } else if (PredefLen > PCHPredefLen) {
1207 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1208 .getFileLocWithOffset(MinLen);
1209 } else {
1210 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1211 .getFileLocWithOffset(MinLen);
1212 }
1213
1214 Diag(Loc1, diag::warn_pch_preprocessor);
1215 if (Loc2.isValid())
1216 Diag(Loc2, diag::note_predef_in_pch);
1217 Diag(diag::note_ignoring_pch) << FileName;
1218 return true;
1219}
1220
Douglas Gregor635f97f2009-04-13 16:31:14 +00001221/// \brief Read the line table in the source manager block.
1222/// \returns true if ther was an error.
1223static bool ParseLineTable(SourceManager &SourceMgr,
1224 llvm::SmallVectorImpl<uint64_t> &Record) {
1225 unsigned Idx = 0;
1226 LineTableInfo &LineTable = SourceMgr.getLineTable();
1227
1228 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001229 std::map<int, int> FileIDs;
1230 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001231 // Extract the file name
1232 unsigned FilenameLen = Record[Idx++];
1233 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1234 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001235 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1236 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001237 }
1238
1239 // Parse the line entries
1240 std::vector<LineEntry> Entries;
1241 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001242 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001243
1244 // Extract the line entries
1245 unsigned NumEntries = Record[Idx++];
1246 Entries.clear();
1247 Entries.reserve(NumEntries);
1248 for (unsigned I = 0; I != NumEntries; ++I) {
1249 unsigned FileOffset = Record[Idx++];
1250 unsigned LineNo = Record[Idx++];
1251 int FilenameID = Record[Idx++];
1252 SrcMgr::CharacteristicKind FileKind
1253 = (SrcMgr::CharacteristicKind)Record[Idx++];
1254 unsigned IncludeOffset = Record[Idx++];
1255 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1256 FileKind, IncludeOffset));
1257 }
1258 LineTable.AddEntry(FID, Entries);
1259 }
1260
1261 return false;
1262}
1263
Douglas Gregorab1cef72009-04-10 03:52:48 +00001264/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001265PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001266 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001267 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1268 Error("Malformed source manager block record");
1269 return Failure;
1270 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001271
1272 SourceManager &SourceMgr = Context.getSourceManager();
1273 RecordData Record;
1274 while (true) {
1275 unsigned Code = Stream.ReadCode();
1276 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001277 if (Stream.ReadBlockEnd()) {
1278 Error("Error at end of Source Manager block");
1279 return Failure;
1280 }
1281
1282 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001283 }
1284
1285 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1286 // No known subblocks, always skip them.
1287 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001288 if (Stream.SkipBlock()) {
1289 Error("Malformed block record");
1290 return Failure;
1291 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001292 continue;
1293 }
1294
1295 if (Code == llvm::bitc::DEFINE_ABBREV) {
1296 Stream.ReadAbbrevRecord();
1297 continue;
1298 }
1299
1300 // Read a record.
1301 const char *BlobStart;
1302 unsigned BlobLen;
1303 Record.clear();
1304 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1305 default: // Default behavior: ignore.
1306 break;
1307
1308 case pch::SM_SLOC_FILE_ENTRY: {
1309 // FIXME: We would really like to delay the creation of this
1310 // FileEntry until it is actually required, e.g., when producing
1311 // a diagnostic with a source location in this file.
1312 const FileEntry *File
1313 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1314 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001315 FileID ID = SourceMgr.createFileID(File,
1316 SourceLocation::getFromRawEncoding(Record[1]),
1317 (CharacteristicKind)Record[2]);
1318 if (Record[3])
1319 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1320 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001321 break;
1322 }
1323
1324 case pch::SM_SLOC_BUFFER_ENTRY: {
1325 const char *Name = BlobStart;
1326 unsigned Code = Stream.ReadCode();
1327 Record.clear();
1328 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1329 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001330 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001331 llvm::MemoryBuffer *Buffer
1332 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1333 BlobStart + BlobLen - 1,
1334 Name);
1335 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1336
1337 if (strcmp(Name, "<built-in>") == 0
1338 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1339 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001340 break;
1341 }
1342
1343 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1344 SourceLocation SpellingLoc
1345 = SourceLocation::getFromRawEncoding(Record[1]);
1346 SourceMgr.createInstantiationLoc(
1347 SpellingLoc,
1348 SourceLocation::getFromRawEncoding(Record[2]),
1349 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001350 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001351 break;
1352 }
1353
Chris Lattnere1be6022009-04-14 23:22:57 +00001354 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001355 if (ParseLineTable(SourceMgr, Record))
1356 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001357 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001358 }
1359 }
1360}
1361
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001362void PCHReader::ReadMacroRecord(uint64_t Offset) {
1363 // Keep track of where we are in the stream, then jump back there
1364 // after reading this macro.
1365 SavedStreamPosition SavedPosition(Stream);
1366
1367 Stream.JumpToBit(Offset);
1368 RecordData Record;
1369 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1370 MacroInfo *Macro = 0;
1371 while (true) {
1372 unsigned Code = Stream.ReadCode();
1373 switch (Code) {
1374 case llvm::bitc::END_BLOCK:
1375 return;
1376
1377 case llvm::bitc::ENTER_SUBBLOCK:
1378 // No known subblocks, always skip them.
1379 Stream.ReadSubBlockID();
1380 if (Stream.SkipBlock()) {
1381 Error("Malformed block record");
1382 return;
1383 }
1384 continue;
1385
1386 case llvm::bitc::DEFINE_ABBREV:
1387 Stream.ReadAbbrevRecord();
1388 continue;
1389 default: break;
1390 }
1391
1392 // Read a record.
1393 Record.clear();
1394 pch::PreprocessorRecordTypes RecType =
1395 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1396 switch (RecType) {
1397 case pch::PP_COUNTER_VALUE:
1398 // Skip this record.
1399 break;
1400
1401 case pch::PP_MACRO_OBJECT_LIKE:
1402 case pch::PP_MACRO_FUNCTION_LIKE: {
1403 // If we already have a macro, that means that we've hit the end
1404 // of the definition of the macro we were looking for. We're
1405 // done.
1406 if (Macro)
1407 return;
1408
1409 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1410 if (II == 0) {
1411 Error("Macro must have a name");
1412 return;
1413 }
1414 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1415 bool isUsed = Record[2];
1416
1417 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1418 MI->setIsUsed(isUsed);
1419
1420 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1421 // Decode function-like macro info.
1422 bool isC99VarArgs = Record[3];
1423 bool isGNUVarArgs = Record[4];
1424 MacroArgs.clear();
1425 unsigned NumArgs = Record[5];
1426 for (unsigned i = 0; i != NumArgs; ++i)
1427 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1428
1429 // Install function-like macro info.
1430 MI->setIsFunctionLike();
1431 if (isC99VarArgs) MI->setIsC99Varargs();
1432 if (isGNUVarArgs) MI->setIsGNUVarargs();
1433 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1434 PP.getPreprocessorAllocator());
1435 }
1436
1437 // Finally, install the macro.
1438 PP.setMacroInfo(II, MI);
1439
1440 // Remember that we saw this macro last so that we add the tokens that
1441 // form its body to it.
1442 Macro = MI;
1443 ++NumMacrosRead;
1444 break;
1445 }
1446
1447 case pch::PP_TOKEN: {
1448 // If we see a TOKEN before a PP_MACRO_*, then the file is
1449 // erroneous, just pretend we didn't see this.
1450 if (Macro == 0) break;
1451
1452 Token Tok;
1453 Tok.startToken();
1454 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1455 Tok.setLength(Record[1]);
1456 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1457 Tok.setIdentifierInfo(II);
1458 Tok.setKind((tok::TokenKind)Record[3]);
1459 Tok.setFlag((Token::TokenFlags)Record[4]);
1460 Macro->AddTokenToBody(Tok);
1461 break;
1462 }
1463 }
1464 }
1465}
1466
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001467bool PCHReader::ReadPreprocessorBlock() {
1468 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1469 return Error("Malformed preprocessor block record");
1470
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001471 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001472 while (true) {
1473 unsigned Code = Stream.ReadCode();
1474 switch (Code) {
1475 case llvm::bitc::END_BLOCK:
1476 if (Stream.ReadBlockEnd())
1477 return Error("Error at end of preprocessor block");
1478 return false;
1479
1480 case llvm::bitc::ENTER_SUBBLOCK:
1481 // No known subblocks, always skip them.
1482 Stream.ReadSubBlockID();
1483 if (Stream.SkipBlock())
1484 return Error("Malformed block record");
1485 continue;
1486
1487 case llvm::bitc::DEFINE_ABBREV:
1488 Stream.ReadAbbrevRecord();
1489 continue;
1490 default: break;
1491 }
1492
1493 // Read a record.
1494 Record.clear();
1495 pch::PreprocessorRecordTypes RecType =
1496 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1497 switch (RecType) {
1498 default: // Default behavior: ignore unknown records.
1499 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001500 case pch::PP_COUNTER_VALUE:
1501 if (!Record.empty())
1502 PP.setCounterValue(Record[0]);
1503 break;
1504
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001505 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001506 case pch::PP_MACRO_FUNCTION_LIKE:
1507 case pch::PP_TOKEN:
1508 // Once we've hit a macro definition or a token, we're done.
1509 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001510 }
1511 }
1512}
1513
Douglas Gregorc713da92009-04-21 22:25:48 +00001514PCHReader::PCHReadResult
1515PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001516 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1517 Error("Malformed block record");
1518 return Failure;
1519 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001520
1521 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001522 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001523 while (!Stream.AtEndOfStream()) {
1524 unsigned Code = Stream.ReadCode();
1525 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001526 if (Stream.ReadBlockEnd()) {
1527 Error("Error at end of module block");
1528 return Failure;
1529 }
Chris Lattner29241862009-04-11 21:15:38 +00001530
Douglas Gregor179cfb12009-04-10 20:39:37 +00001531 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001532 }
1533
1534 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1535 switch (Stream.ReadSubBlockID()) {
1536 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1537 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1538 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001539 if (Stream.SkipBlock()) {
1540 Error("Malformed block record");
1541 return Failure;
1542 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001543 break;
1544
Chris Lattner29241862009-04-11 21:15:38 +00001545 case pch::PREPROCESSOR_BLOCK_ID:
1546 // Skip the preprocessor block for now, but remember where it is. We
1547 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001548 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001549 Error("Multiple preprocessor blocks found.");
1550 return Failure;
1551 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001552 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001553 if (Stream.SkipBlock()) {
1554 Error("Malformed block record");
1555 return Failure;
1556 }
1557 break;
1558
Douglas Gregorab1cef72009-04-10 03:52:48 +00001559 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001560 switch (ReadSourceManagerBlock()) {
1561 case Success:
1562 break;
1563
1564 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001565 Error("Malformed source manager block");
1566 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001567
1568 case IgnorePCH:
1569 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001570 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001571 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001572 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001573 continue;
1574 }
1575
1576 if (Code == llvm::bitc::DEFINE_ABBREV) {
1577 Stream.ReadAbbrevRecord();
1578 continue;
1579 }
1580
1581 // Read and process a record.
1582 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001583 const char *BlobStart = 0;
1584 unsigned BlobLen = 0;
1585 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1586 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001587 default: // Default behavior: ignore.
1588 break;
1589
1590 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001591 if (!TypeOffsets.empty()) {
1592 Error("Duplicate TYPE_OFFSET record in PCH file");
1593 return Failure;
1594 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001595 TypeOffsets.swap(Record);
1596 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1597 break;
1598
1599 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001600 if (!DeclOffsets.empty()) {
1601 Error("Duplicate DECL_OFFSET record in PCH file");
1602 return Failure;
1603 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001604 DeclOffsets.swap(Record);
1605 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1606 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001607
1608 case pch::LANGUAGE_OPTIONS:
1609 if (ParseLanguageOptions(Record))
1610 return IgnorePCH;
1611 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001612
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001613 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001614 std::string TargetTriple(BlobStart, BlobLen);
1615 if (TargetTriple != Context.Target.getTargetTriple()) {
1616 Diag(diag::warn_pch_target_triple)
1617 << TargetTriple << Context.Target.getTargetTriple();
1618 Diag(diag::note_ignoring_pch) << FileName;
1619 return IgnorePCH;
1620 }
1621 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001622 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001623
1624 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001625 IdentifierTableData = BlobStart;
1626 IdentifierLookupTable
1627 = PCHIdentifierLookupTable::Create(
1628 (const unsigned char *)IdentifierTableData + Record[0],
1629 (const unsigned char *)IdentifierTableData,
1630 PCHIdentifierLookupTrait(*this));
1631 // FIXME: What about any identifiers already placed into the
1632 // identifier table? Should we load decls with those names now?
1633 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001634 break;
1635
1636 case pch::IDENTIFIER_OFFSET:
1637 if (!IdentifierData.empty()) {
1638 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1639 return Failure;
1640 }
1641 IdentifierData.swap(Record);
1642#ifndef NDEBUG
1643 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1644 if ((IdentifierData[I] & 0x01) == 0) {
1645 Error("Malformed identifier table in the precompiled header");
1646 return Failure;
1647 }
1648 }
1649#endif
1650 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001651
1652 case pch::EXTERNAL_DEFINITIONS:
1653 if (!ExternalDefinitions.empty()) {
1654 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1655 return Failure;
1656 }
1657 ExternalDefinitions.swap(Record);
1658 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001659
Douglas Gregore01ad442009-04-18 05:55:16 +00001660 case pch::SPECIAL_TYPES:
1661 SpecialTypes.swap(Record);
1662 break;
1663
Douglas Gregor456e0952009-04-17 22:13:46 +00001664 case pch::STATISTICS:
1665 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001666 TotalNumMacros = Record[1];
Douglas Gregor456e0952009-04-17 22:13:46 +00001667 break;
1668
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001669 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001670 }
1671
Douglas Gregor179cfb12009-04-10 20:39:37 +00001672 Error("Premature end of bitstream");
1673 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001674}
1675
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001676PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001677 // Set the PCH file name.
1678 this->FileName = FileName;
1679
Douglas Gregorc34897d2009-04-09 22:27:44 +00001680 // Open the PCH file.
1681 std::string ErrStr;
1682 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001683 if (!Buffer) {
1684 Error(ErrStr.c_str());
1685 return IgnorePCH;
1686 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687
1688 // Initialize the stream
1689 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1690 (const unsigned char *)Buffer->getBufferEnd());
1691
1692 // Sniff for the signature.
1693 if (Stream.Read(8) != 'C' ||
1694 Stream.Read(8) != 'P' ||
1695 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001696 Stream.Read(8) != 'H') {
1697 Error("Not a PCH file");
1698 return IgnorePCH;
1699 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001700
1701 // We expect a number of well-defined blocks, though we don't necessarily
1702 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001703 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001704 while (!Stream.AtEndOfStream()) {
1705 unsigned Code = Stream.ReadCode();
1706
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001707 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1708 Error("Invalid record at top-level");
1709 return Failure;
1710 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001711
1712 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001713
Douglas Gregorc34897d2009-04-09 22:27:44 +00001714 // We only know the PCH subblock ID.
1715 switch (BlockID) {
1716 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001717 if (Stream.ReadBlockInfoBlock()) {
1718 Error("Malformed BlockInfoBlock");
1719 return Failure;
1720 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001721 break;
1722 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001723 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001724 case Success:
1725 break;
1726
1727 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001728 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001729
1730 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001731 // FIXME: We could consider reading through to the end of this
1732 // PCH block, skipping subblocks, to see if there are other
1733 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001734 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001735 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001736 break;
1737 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001738 if (Stream.SkipBlock()) {
1739 Error("Malformed block record");
1740 return Failure;
1741 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001742 break;
1743 }
1744 }
1745
1746 // Load the translation unit declaration
1747 ReadDeclRecord(DeclOffsets[0], 0);
1748
Douglas Gregorc713da92009-04-21 22:25:48 +00001749 // Initialization of builtins and library builtins occurs before the
1750 // PCH file is read, so there may be some identifiers that were
1751 // loaded into the IdentifierTable before we intercepted the
1752 // creation of identifiers. Iterate through the list of known
1753 // identifiers and determine whether we have to establish
1754 // preprocessor definitions or top-level identifier declaration
1755 // chains for those identifiers.
1756 //
1757 // We copy the IdentifierInfo pointers to a small vector first,
1758 // since de-serializing declarations or macro definitions can add
1759 // new entries into the identifier table, invalidating the
1760 // iterators.
1761 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1762 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1763 IdEnd = PP.getIdentifierTable().end();
1764 Id != IdEnd; ++Id)
1765 Identifiers.push_back(Id->second);
1766 PCHIdentifierLookupTable *IdTable
1767 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1768 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1769 IdentifierInfo *II = Identifiers[I];
1770 // Look in the on-disk hash table for an entry for
1771 PCHIdentifierLookupTrait Info(*this, II);
1772 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1773 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1774 if (Pos == IdTable->end())
1775 continue;
1776
Douglas Gregor37f477e2009-04-22 04:56:28 +00001777 fprintf(stderr, "Looked up pre-allocated IdentifierInfo \"%s\"\n",
1778 II->getName());
1779
Douglas Gregorc713da92009-04-21 22:25:48 +00001780 // Dereferencing the iterator has the effect of populating the
1781 // IdentifierInfo node with the various declarations it needs.
1782 (void)*Pos;
1783 }
1784
Douglas Gregore01ad442009-04-18 05:55:16 +00001785 // Load the special types.
1786 Context.setBuiltinVaListType(
1787 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1788
Douglas Gregorc713da92009-04-21 22:25:48 +00001789 // If we saw the preprocessor block, read it now.
1790 if (PreprocessorBlockOffset) {
1791 SavedStreamPosition SavedPos(Stream);
1792 Stream.JumpToBit(PreprocessorBlockOffset);
1793 if (ReadPreprocessorBlock()) {
1794 Error("Malformed preprocessor block");
1795 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001796 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001797 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001798
Douglas Gregorc713da92009-04-21 22:25:48 +00001799 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001800}
1801
Douglas Gregor179cfb12009-04-10 20:39:37 +00001802/// \brief Parse the record that corresponds to a LangOptions data
1803/// structure.
1804///
1805/// This routine compares the language options used to generate the
1806/// PCH file against the language options set for the current
1807/// compilation. For each option, we classify differences between the
1808/// two compiler states as either "benign" or "important". Benign
1809/// differences don't matter, and we accept them without complaint
1810/// (and without modifying the language options). Differences between
1811/// the states for important options cause the PCH file to be
1812/// unusable, so we emit a warning and return true to indicate that
1813/// there was an error.
1814///
1815/// \returns true if the PCH file is unacceptable, false otherwise.
1816bool PCHReader::ParseLanguageOptions(
1817 const llvm::SmallVectorImpl<uint64_t> &Record) {
1818 const LangOptions &LangOpts = Context.getLangOptions();
1819#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1820#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1821 if (Record[Idx] != LangOpts.Option) { \
1822 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1823 Diag(diag::note_ignoring_pch) << FileName; \
1824 return true; \
1825 } \
1826 ++Idx
1827
1828 unsigned Idx = 0;
1829 PARSE_LANGOPT_BENIGN(Trigraphs);
1830 PARSE_LANGOPT_BENIGN(BCPLComment);
1831 PARSE_LANGOPT_BENIGN(DollarIdents);
1832 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1833 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1834 PARSE_LANGOPT_BENIGN(ImplicitInt);
1835 PARSE_LANGOPT_BENIGN(Digraphs);
1836 PARSE_LANGOPT_BENIGN(HexFloats);
1837 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1838 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1839 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1840 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1841 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1842 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1843 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1844 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1845 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1846 PARSE_LANGOPT_BENIGN(PascalStrings);
1847 PARSE_LANGOPT_BENIGN(Boolean);
1848 PARSE_LANGOPT_BENIGN(WritableStrings);
1849 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1850 diag::warn_pch_lax_vector_conversions);
1851 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1852 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1853 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1854 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1855 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1856 diag::warn_pch_thread_safe_statics);
1857 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1858 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1859 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1860 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1861 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1862 diag::warn_pch_heinous_extensions);
1863 // FIXME: Most of the options below are benign if the macro wasn't
1864 // used. Unfortunately, this means that a PCH compiled without
1865 // optimization can't be used with optimization turned on, even
1866 // though the only thing that changes is whether __OPTIMIZE__ was
1867 // defined... but if __OPTIMIZE__ never showed up in the header, it
1868 // doesn't matter. We could consider making this some special kind
1869 // of check.
1870 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1871 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1872 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1873 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1874 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1875 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1876 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1877 Diag(diag::warn_pch_gc_mode)
1878 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1879 Diag(diag::note_ignoring_pch) << FileName;
1880 return true;
1881 }
1882 ++Idx;
1883 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1884 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1885#undef PARSE_LANGOPT_IRRELEVANT
1886#undef PARSE_LANGOPT_BENIGN
1887
1888 return false;
1889}
1890
Douglas Gregorc34897d2009-04-09 22:27:44 +00001891/// \brief Read and return the type at the given offset.
1892///
1893/// This routine actually reads the record corresponding to the type
1894/// at the given offset in the bitstream. It is a helper routine for
1895/// GetType, which deals with reading type IDs.
1896QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001897 // Keep track of where we are in the stream, then jump back there
1898 // after reading this type.
1899 SavedStreamPosition SavedPosition(Stream);
1900
Douglas Gregorc34897d2009-04-09 22:27:44 +00001901 Stream.JumpToBit(Offset);
1902 RecordData Record;
1903 unsigned Code = Stream.ReadCode();
1904 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001905 case pch::TYPE_EXT_QUAL: {
1906 assert(Record.size() == 3 &&
1907 "Incorrect encoding of extended qualifier type");
1908 QualType Base = GetType(Record[0]);
1909 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1910 unsigned AddressSpace = Record[2];
1911
1912 QualType T = Base;
1913 if (GCAttr != QualType::GCNone)
1914 T = Context.getObjCGCQualType(T, GCAttr);
1915 if (AddressSpace)
1916 T = Context.getAddrSpaceQualType(T, AddressSpace);
1917 return T;
1918 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001919
Douglas Gregorc34897d2009-04-09 22:27:44 +00001920 case pch::TYPE_FIXED_WIDTH_INT: {
1921 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1922 return Context.getFixedWidthIntType(Record[0], Record[1]);
1923 }
1924
1925 case pch::TYPE_COMPLEX: {
1926 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1927 QualType ElemType = GetType(Record[0]);
1928 return Context.getComplexType(ElemType);
1929 }
1930
1931 case pch::TYPE_POINTER: {
1932 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1933 QualType PointeeType = GetType(Record[0]);
1934 return Context.getPointerType(PointeeType);
1935 }
1936
1937 case pch::TYPE_BLOCK_POINTER: {
1938 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1939 QualType PointeeType = GetType(Record[0]);
1940 return Context.getBlockPointerType(PointeeType);
1941 }
1942
1943 case pch::TYPE_LVALUE_REFERENCE: {
1944 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1945 QualType PointeeType = GetType(Record[0]);
1946 return Context.getLValueReferenceType(PointeeType);
1947 }
1948
1949 case pch::TYPE_RVALUE_REFERENCE: {
1950 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1951 QualType PointeeType = GetType(Record[0]);
1952 return Context.getRValueReferenceType(PointeeType);
1953 }
1954
1955 case pch::TYPE_MEMBER_POINTER: {
1956 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1957 QualType PointeeType = GetType(Record[0]);
1958 QualType ClassType = GetType(Record[1]);
1959 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1960 }
1961
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001962 case pch::TYPE_CONSTANT_ARRAY: {
1963 QualType ElementType = GetType(Record[0]);
1964 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1965 unsigned IndexTypeQuals = Record[2];
1966 unsigned Idx = 3;
1967 llvm::APInt Size = ReadAPInt(Record, Idx);
1968 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1969 }
1970
1971 case pch::TYPE_INCOMPLETE_ARRAY: {
1972 QualType ElementType = GetType(Record[0]);
1973 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1974 unsigned IndexTypeQuals = Record[2];
1975 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1976 }
1977
1978 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001979 QualType ElementType = GetType(Record[0]);
1980 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1981 unsigned IndexTypeQuals = Record[2];
1982 return Context.getVariableArrayType(ElementType, ReadExpr(),
1983 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001984 }
1985
1986 case pch::TYPE_VECTOR: {
1987 if (Record.size() != 2) {
1988 Error("Incorrect encoding of vector type in PCH file");
1989 return QualType();
1990 }
1991
1992 QualType ElementType = GetType(Record[0]);
1993 unsigned NumElements = Record[1];
1994 return Context.getVectorType(ElementType, NumElements);
1995 }
1996
1997 case pch::TYPE_EXT_VECTOR: {
1998 if (Record.size() != 2) {
1999 Error("Incorrect encoding of extended vector type in PCH file");
2000 return QualType();
2001 }
2002
2003 QualType ElementType = GetType(Record[0]);
2004 unsigned NumElements = Record[1];
2005 return Context.getExtVectorType(ElementType, NumElements);
2006 }
2007
2008 case pch::TYPE_FUNCTION_NO_PROTO: {
2009 if (Record.size() != 1) {
2010 Error("Incorrect encoding of no-proto function type");
2011 return QualType();
2012 }
2013 QualType ResultType = GetType(Record[0]);
2014 return Context.getFunctionNoProtoType(ResultType);
2015 }
2016
2017 case pch::TYPE_FUNCTION_PROTO: {
2018 QualType ResultType = GetType(Record[0]);
2019 unsigned Idx = 1;
2020 unsigned NumParams = Record[Idx++];
2021 llvm::SmallVector<QualType, 16> ParamTypes;
2022 for (unsigned I = 0; I != NumParams; ++I)
2023 ParamTypes.push_back(GetType(Record[Idx++]));
2024 bool isVariadic = Record[Idx++];
2025 unsigned Quals = Record[Idx++];
2026 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2027 isVariadic, Quals);
2028 }
2029
2030 case pch::TYPE_TYPEDEF:
2031 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2032 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2033
2034 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002035 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002036
2037 case pch::TYPE_TYPEOF: {
2038 if (Record.size() != 1) {
2039 Error("Incorrect encoding of typeof(type) in PCH file");
2040 return QualType();
2041 }
2042 QualType UnderlyingType = GetType(Record[0]);
2043 return Context.getTypeOfType(UnderlyingType);
2044 }
2045
2046 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002047 assert(Record.size() == 1 && "Incorrect encoding of record type");
2048 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002049
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002050 case pch::TYPE_ENUM:
2051 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2052 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2053
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002054 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002055 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2056 return Context.getObjCInterfaceType(
2057 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002058
2059 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
2060 // FIXME: Deserialize ObjCQualifiedInterfaceType
2061 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
2062 return QualType();
2063
2064 case pch::TYPE_OBJC_QUALIFIED_ID:
2065 // FIXME: Deserialize ObjCQualifiedIdType
2066 assert(false && "Cannot de-serialize ObjC qualified id types yet");
2067 return QualType();
2068
2069 case pch::TYPE_OBJC_QUALIFIED_CLASS:
2070 // FIXME: Deserialize ObjCQualifiedClassType
2071 assert(false && "Cannot de-serialize ObjC qualified class types yet");
2072 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002073 }
2074
2075 // Suppress a GCC warning
2076 return QualType();
2077}
2078
2079/// \brief Note that we have loaded the declaration with the given
2080/// Index.
2081///
2082/// This routine notes that this declaration has already been loaded,
2083/// so that future GetDecl calls will return this declaration rather
2084/// than trying to load a new declaration.
2085inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2086 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2087 DeclAlreadyLoaded[Index] = true;
2088 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2089}
2090
2091/// \brief Read the declaration at the given offset from the PCH file.
2092Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002093 // Keep track of where we are in the stream, then jump back there
2094 // after reading this declaration.
2095 SavedStreamPosition SavedPosition(Stream);
2096
Douglas Gregorc34897d2009-04-09 22:27:44 +00002097 Decl *D = 0;
2098 Stream.JumpToBit(Offset);
2099 RecordData Record;
2100 unsigned Code = Stream.ReadCode();
2101 unsigned Idx = 0;
2102 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002103
Douglas Gregorc34897d2009-04-09 22:27:44 +00002104 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002105 case pch::DECL_ATTR:
2106 case pch::DECL_CONTEXT_LEXICAL:
2107 case pch::DECL_CONTEXT_VISIBLE:
2108 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2109 break;
2110
Douglas Gregorc34897d2009-04-09 22:27:44 +00002111 case pch::DECL_TRANSLATION_UNIT:
2112 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002113 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002114 break;
2115
2116 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002117 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002118 break;
2119 }
2120
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002121 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002122 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002123 break;
2124 }
2125
Douglas Gregor982365e2009-04-13 21:20:57 +00002126 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002127 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2128 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002129 break;
2130 }
2131
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002132 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002133 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2134 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002135 break;
2136 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002137
2138 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002139 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2140 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002141 break;
2142 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002143
Steve Naroff79ea0e02009-04-20 15:06:07 +00002144 case pch::DECL_OBJC_METHOD: {
2145 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2146 Selector(), QualType(), 0);
2147 break;
2148 }
2149
Steve Naroff97b53bd2009-04-21 15:12:33 +00002150 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002151 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2152 break;
2153 }
2154
Steve Naroff97b53bd2009-04-21 15:12:33 +00002155 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002156 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2157 ObjCIvarDecl::None);
2158 break;
2159 }
2160
Steve Naroff97b53bd2009-04-21 15:12:33 +00002161 case pch::DECL_OBJC_PROTOCOL: {
2162 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2163 break;
2164 }
2165
2166 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2167 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2168 QualType(), 0);
2169 break;
2170 }
2171
2172 case pch::DECL_OBJC_CLASS: {
2173 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2174 break;
2175 }
2176
2177 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2178 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2179 break;
2180 }
2181
2182 case pch::DECL_OBJC_CATEGORY: {
2183 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2184 break;
2185 }
2186
2187 case pch::DECL_OBJC_CATEGORY_IMPL: {
2188 // FIXME: Implement.
2189 break;
2190 }
2191
2192 case pch::DECL_OBJC_IMPLEMENTATION: {
2193 // FIXME: Implement.
2194 break;
2195 }
2196
2197 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2198 // FIXME: Implement.
2199 break;
2200 }
2201
2202 case pch::DECL_OBJC_PROPERTY: {
2203 // FIXME: Implement.
2204 break;
2205 }
2206
2207 case pch::DECL_OBJC_PROPERTY_IMPL: {
2208 // FIXME: Implement.
2209 break;
2210 }
2211
Douglas Gregor982365e2009-04-13 21:20:57 +00002212 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002213 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2214 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002215 break;
2216 }
2217
Douglas Gregorc34897d2009-04-09 22:27:44 +00002218 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002219 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2220 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002221 break;
2222 }
2223
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002224 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002225 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2226 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002227 break;
2228 }
2229
2230 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002231 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002232 QualType(), QualType(), VarDecl::None,
2233 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002234 break;
2235 }
2236
Douglas Gregor2a491792009-04-13 22:49:25 +00002237 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002238 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002239 break;
2240 }
2241
2242 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002243 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002244 break;
2245 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002246 }
2247
Douglas Gregorc713da92009-04-21 22:25:48 +00002248 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002249 if (D) {
2250 LoadedDecl(Index, D);
2251 Reader.Visit(D);
2252 }
2253
Douglas Gregorc34897d2009-04-09 22:27:44 +00002254 // If this declaration is also a declaration context, get the
2255 // offsets for its tables of lexical and visible declarations.
2256 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2257 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2258 if (Offsets.first || Offsets.second) {
2259 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2260 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2261 DeclContextOffsets[DC] = Offsets;
2262 }
2263 }
2264 assert(Idx == Record.size());
2265
2266 return D;
2267}
2268
Douglas Gregorac8f2802009-04-10 17:25:41 +00002269QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002270 unsigned Quals = ID & 0x07;
2271 unsigned Index = ID >> 3;
2272
2273 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2274 QualType T;
2275 switch ((pch::PredefinedTypeIDs)Index) {
2276 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2277 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2278 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2279
2280 case pch::PREDEF_TYPE_CHAR_U_ID:
2281 case pch::PREDEF_TYPE_CHAR_S_ID:
2282 // FIXME: Check that the signedness of CharTy is correct!
2283 T = Context.CharTy;
2284 break;
2285
2286 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2287 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2288 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2289 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2290 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2291 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2292 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2293 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2294 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2295 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2296 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2297 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2298 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2299 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2300 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2301 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2302 }
2303
2304 assert(!T.isNull() && "Unknown predefined type");
2305 return T.getQualifiedType(Quals);
2306 }
2307
2308 Index -= pch::NUM_PREDEF_TYPE_IDS;
2309 if (!TypeAlreadyLoaded[Index]) {
2310 // Load the type from the PCH file.
2311 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2312 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2313 TypeAlreadyLoaded[Index] = true;
2314 }
2315
2316 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2317}
2318
Douglas Gregorac8f2802009-04-10 17:25:41 +00002319Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002320 if (ID == 0)
2321 return 0;
2322
2323 unsigned Index = ID - 1;
2324 if (DeclAlreadyLoaded[Index])
2325 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2326
2327 // Load the declaration from the PCH file.
2328 return ReadDeclRecord(DeclOffsets[Index], Index);
2329}
2330
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002331Stmt *PCHReader::GetStmt(uint64_t Offset) {
2332 // Keep track of where we are in the stream, then jump back there
2333 // after reading this declaration.
2334 SavedStreamPosition SavedPosition(Stream);
2335
2336 Stream.JumpToBit(Offset);
2337 return ReadStmt();
2338}
2339
Douglas Gregorc34897d2009-04-09 22:27:44 +00002340bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002341 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002342 assert(DC->hasExternalLexicalStorage() &&
2343 "DeclContext has no lexical decls in storage");
2344 uint64_t Offset = DeclContextOffsets[DC].first;
2345 assert(Offset && "DeclContext has no lexical decls in storage");
2346
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002347 // Keep track of where we are in the stream, then jump back there
2348 // after reading this context.
2349 SavedStreamPosition SavedPosition(Stream);
2350
Douglas Gregorc34897d2009-04-09 22:27:44 +00002351 // Load the record containing all of the declarations lexically in
2352 // this context.
2353 Stream.JumpToBit(Offset);
2354 RecordData Record;
2355 unsigned Code = Stream.ReadCode();
2356 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002357 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002358 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2359
2360 // Load all of the declaration IDs
2361 Decls.clear();
2362 Decls.insert(Decls.end(), Record.begin(), Record.end());
2363 return false;
2364}
2365
2366bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2367 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2368 assert(DC->hasExternalVisibleStorage() &&
2369 "DeclContext has no visible decls in storage");
2370 uint64_t Offset = DeclContextOffsets[DC].second;
2371 assert(Offset && "DeclContext has no visible decls in storage");
2372
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002373 // Keep track of where we are in the stream, then jump back there
2374 // after reading this context.
2375 SavedStreamPosition SavedPosition(Stream);
2376
Douglas Gregorc34897d2009-04-09 22:27:44 +00002377 // Load the record containing all of the declarations visible in
2378 // this context.
2379 Stream.JumpToBit(Offset);
2380 RecordData Record;
2381 unsigned Code = Stream.ReadCode();
2382 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002383 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002384 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2385 if (Record.size() == 0)
2386 return false;
2387
2388 Decls.clear();
2389
2390 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002391 while (Idx < Record.size()) {
2392 Decls.push_back(VisibleDeclaration());
2393 Decls.back().Name = ReadDeclarationName(Record, Idx);
2394
Douglas Gregorc34897d2009-04-09 22:27:44 +00002395 unsigned Size = Record[Idx++];
2396 llvm::SmallVector<unsigned, 4> & LoadedDecls
2397 = Decls.back().Declarations;
2398 LoadedDecls.reserve(Size);
2399 for (unsigned I = 0; I < Size; ++I)
2400 LoadedDecls.push_back(Record[Idx++]);
2401 }
2402
2403 return false;
2404}
2405
Douglas Gregor631f6c62009-04-14 00:24:19 +00002406void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2407 if (!Consumer)
2408 return;
2409
2410 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2411 Decl *D = GetDecl(ExternalDefinitions[I]);
2412 DeclGroupRef DG(D);
2413 Consumer->HandleTopLevelDecl(DG);
2414 }
2415}
2416
Douglas Gregorc34897d2009-04-09 22:27:44 +00002417void PCHReader::PrintStats() {
2418 std::fprintf(stderr, "*** PCH Statistics:\n");
2419
2420 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2421 TypeAlreadyLoaded.end(),
2422 true);
2423 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2424 DeclAlreadyLoaded.end(),
2425 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002426 unsigned NumIdentifiersLoaded = 0;
2427 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2428 if ((IdentifierData[I] & 0x01) == 0)
2429 ++NumIdentifiersLoaded;
2430 }
2431
Douglas Gregorc34897d2009-04-09 22:27:44 +00002432 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2433 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002434 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002435 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2436 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002437 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2438 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2439 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2440 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002441 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2442 NumStatementsRead, TotalNumStatements,
2443 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002444 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2445 NumMacrosRead, TotalNumMacros,
2446 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002447 std::fprintf(stderr, "\n");
2448}
2449
Douglas Gregorc713da92009-04-21 22:25:48 +00002450void PCHReader::InitializeSema(Sema &S) {
2451 SemaObj = &S;
2452
2453 // FIXME: this makes sure any declarations that were deserialized
2454 // "too early" still get added to the identifier's declaration
2455 // chains.
2456 for (unsigned I = 0, N = TUDecls.size(); I != N; ++I) {
2457 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(TUDecls[I]));
2458 SemaObj->IdResolver.AddDecl(TUDecls[I]);
2459 }
2460 TUDecls.clear();
2461}
2462
2463IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2464 // Try to find this name within our on-disk hash table
2465 PCHIdentifierLookupTable *IdTable
2466 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2467 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2468 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2469 if (Pos == IdTable->end())
2470 return 0;
2471
2472 // Dereferencing the iterator has the effect of building the
2473 // IdentifierInfo node and populating it with the various
2474 // declarations it needs.
2475 return *Pos;
2476}
2477
2478void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2479 assert(ID && "Non-zero identifier ID required");
2480 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2481}
2482
Chris Lattner29241862009-04-11 21:15:38 +00002483IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002484 if (ID == 0)
2485 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002486
Douglas Gregorc713da92009-04-21 22:25:48 +00002487 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002488 Error("No identifier table in PCH file");
2489 return 0;
2490 }
Chris Lattner29241862009-04-11 21:15:38 +00002491
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002492 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002493 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002494 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002495 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002496 }
Chris Lattner29241862009-04-11 21:15:38 +00002497
2498 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002499}
2500
2501DeclarationName
2502PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2503 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2504 switch (Kind) {
2505 case DeclarationName::Identifier:
2506 return DeclarationName(GetIdentifierInfo(Record, Idx));
2507
2508 case DeclarationName::ObjCZeroArgSelector:
2509 case DeclarationName::ObjCOneArgSelector:
2510 case DeclarationName::ObjCMultiArgSelector:
2511 assert(false && "Unable to de-serialize Objective-C selectors");
2512 break;
2513
2514 case DeclarationName::CXXConstructorName:
2515 return Context.DeclarationNames.getCXXConstructorName(
2516 GetType(Record[Idx++]));
2517
2518 case DeclarationName::CXXDestructorName:
2519 return Context.DeclarationNames.getCXXDestructorName(
2520 GetType(Record[Idx++]));
2521
2522 case DeclarationName::CXXConversionFunctionName:
2523 return Context.DeclarationNames.getCXXConversionFunctionName(
2524 GetType(Record[Idx++]));
2525
2526 case DeclarationName::CXXOperatorName:
2527 return Context.DeclarationNames.getCXXOperatorName(
2528 (OverloadedOperatorKind)Record[Idx++]);
2529
2530 case DeclarationName::CXXUsingDirective:
2531 return DeclarationName::getUsingDirectiveName();
2532 }
2533
2534 // Required to silence GCC warning
2535 return DeclarationName();
2536}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002537
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002538/// \brief Read an integral value
2539llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2540 unsigned BitWidth = Record[Idx++];
2541 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2542 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2543 Idx += NumWords;
2544 return Result;
2545}
2546
2547/// \brief Read a signed integral value
2548llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2549 bool isUnsigned = Record[Idx++];
2550 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2551}
2552
Douglas Gregore2f37202009-04-14 21:55:33 +00002553/// \brief Read a floating-point value
2554llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002555 return llvm::APFloat(ReadAPInt(Record, Idx));
2556}
2557
Douglas Gregor1c507882009-04-15 21:30:51 +00002558// \brief Read a string
2559std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2560 unsigned Len = Record[Idx++];
2561 std::string Result(&Record[Idx], &Record[Idx] + Len);
2562 Idx += Len;
2563 return Result;
2564}
2565
2566/// \brief Reads attributes from the current stream position.
2567Attr *PCHReader::ReadAttributes() {
2568 unsigned Code = Stream.ReadCode();
2569 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2570 "Expected unabbreviated record"); (void)Code;
2571
2572 RecordData Record;
2573 unsigned Idx = 0;
2574 unsigned RecCode = Stream.ReadRecord(Code, Record);
2575 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2576 (void)RecCode;
2577
2578#define SIMPLE_ATTR(Name) \
2579 case Attr::Name: \
2580 New = ::new (Context) Name##Attr(); \
2581 break
2582
2583#define STRING_ATTR(Name) \
2584 case Attr::Name: \
2585 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2586 break
2587
2588#define UNSIGNED_ATTR(Name) \
2589 case Attr::Name: \
2590 New = ::new (Context) Name##Attr(Record[Idx++]); \
2591 break
2592
2593 Attr *Attrs = 0;
2594 while (Idx < Record.size()) {
2595 Attr *New = 0;
2596 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2597 bool IsInherited = Record[Idx++];
2598
2599 switch (Kind) {
2600 STRING_ATTR(Alias);
2601 UNSIGNED_ATTR(Aligned);
2602 SIMPLE_ATTR(AlwaysInline);
2603 SIMPLE_ATTR(AnalyzerNoReturn);
2604 STRING_ATTR(Annotate);
2605 STRING_ATTR(AsmLabel);
2606
2607 case Attr::Blocks:
2608 New = ::new (Context) BlocksAttr(
2609 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2610 break;
2611
2612 case Attr::Cleanup:
2613 New = ::new (Context) CleanupAttr(
2614 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2615 break;
2616
2617 SIMPLE_ATTR(Const);
2618 UNSIGNED_ATTR(Constructor);
2619 SIMPLE_ATTR(DLLExport);
2620 SIMPLE_ATTR(DLLImport);
2621 SIMPLE_ATTR(Deprecated);
2622 UNSIGNED_ATTR(Destructor);
2623 SIMPLE_ATTR(FastCall);
2624
2625 case Attr::Format: {
2626 std::string Type = ReadString(Record, Idx);
2627 unsigned FormatIdx = Record[Idx++];
2628 unsigned FirstArg = Record[Idx++];
2629 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2630 break;
2631 }
2632
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002633 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002634
2635 case Attr::IBOutletKind:
2636 New = ::new (Context) IBOutletAttr();
2637 break;
2638
2639 SIMPLE_ATTR(NoReturn);
2640 SIMPLE_ATTR(NoThrow);
2641 SIMPLE_ATTR(Nodebug);
2642 SIMPLE_ATTR(Noinline);
2643
2644 case Attr::NonNull: {
2645 unsigned Size = Record[Idx++];
2646 llvm::SmallVector<unsigned, 16> ArgNums;
2647 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2648 Idx += Size;
2649 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2650 break;
2651 }
2652
2653 SIMPLE_ATTR(ObjCException);
2654 SIMPLE_ATTR(ObjCNSObject);
2655 SIMPLE_ATTR(Overloadable);
2656 UNSIGNED_ATTR(Packed);
2657 SIMPLE_ATTR(Pure);
2658 UNSIGNED_ATTR(Regparm);
2659 STRING_ATTR(Section);
2660 SIMPLE_ATTR(StdCall);
2661 SIMPLE_ATTR(TransparentUnion);
2662 SIMPLE_ATTR(Unavailable);
2663 SIMPLE_ATTR(Unused);
2664 SIMPLE_ATTR(Used);
2665
2666 case Attr::Visibility:
2667 New = ::new (Context) VisibilityAttr(
2668 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2669 break;
2670
2671 SIMPLE_ATTR(WarnUnusedResult);
2672 SIMPLE_ATTR(Weak);
2673 SIMPLE_ATTR(WeakImport);
2674 }
2675
2676 assert(New && "Unable to decode attribute?");
2677 New->setInherited(IsInherited);
2678 New->setNext(Attrs);
2679 Attrs = New;
2680 }
2681#undef UNSIGNED_ATTR
2682#undef STRING_ATTR
2683#undef SIMPLE_ATTR
2684
2685 // The list of attributes was built backwards. Reverse the list
2686 // before returning it.
2687 Attr *PrevAttr = 0, *NextAttr = 0;
2688 while (Attrs) {
2689 NextAttr = Attrs->getNext();
2690 Attrs->setNext(PrevAttr);
2691 PrevAttr = Attrs;
2692 Attrs = NextAttr;
2693 }
2694
2695 return PrevAttr;
2696}
2697
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002698Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002699 // Within the bitstream, expressions are stored in Reverse Polish
2700 // Notation, with each of the subexpressions preceding the
2701 // expression they are stored in. To evaluate expressions, we
2702 // continue reading expressions and placing them on the stack, with
2703 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002704 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002705 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002706 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002707 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002708 llvm::SmallVector<Stmt *, 16> StmtStack;
2709 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002710 Stmt::EmptyShell Empty;
2711
Douglas Gregora151ba42009-04-14 23:32:43 +00002712 while (true) {
2713 unsigned Code = Stream.ReadCode();
2714 if (Code == llvm::bitc::END_BLOCK) {
2715 if (Stream.ReadBlockEnd()) {
2716 Error("Error at end of Source Manager block");
2717 return 0;
2718 }
2719 break;
2720 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002721
Douglas Gregora151ba42009-04-14 23:32:43 +00002722 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2723 // No known subblocks, always skip them.
2724 Stream.ReadSubBlockID();
2725 if (Stream.SkipBlock()) {
2726 Error("Malformed block record");
2727 return 0;
2728 }
2729 continue;
2730 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002731
Douglas Gregora151ba42009-04-14 23:32:43 +00002732 if (Code == llvm::bitc::DEFINE_ABBREV) {
2733 Stream.ReadAbbrevRecord();
2734 continue;
2735 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002736
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002737 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002738 Idx = 0;
2739 Record.clear();
2740 bool Finished = false;
2741 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002742 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002743 Finished = true;
2744 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002745
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002746 case pch::STMT_NULL_PTR:
2747 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002748 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002749
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002750 case pch::STMT_NULL:
2751 S = new (Context) NullStmt(Empty);
2752 break;
2753
2754 case pch::STMT_COMPOUND:
2755 S = new (Context) CompoundStmt(Empty);
2756 break;
2757
2758 case pch::STMT_CASE:
2759 S = new (Context) CaseStmt(Empty);
2760 break;
2761
2762 case pch::STMT_DEFAULT:
2763 S = new (Context) DefaultStmt(Empty);
2764 break;
2765
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002766 case pch::STMT_LABEL:
2767 S = new (Context) LabelStmt(Empty);
2768 break;
2769
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002770 case pch::STMT_IF:
2771 S = new (Context) IfStmt(Empty);
2772 break;
2773
2774 case pch::STMT_SWITCH:
2775 S = new (Context) SwitchStmt(Empty);
2776 break;
2777
Douglas Gregora6b503f2009-04-17 00:16:09 +00002778 case pch::STMT_WHILE:
2779 S = new (Context) WhileStmt(Empty);
2780 break;
2781
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002782 case pch::STMT_DO:
2783 S = new (Context) DoStmt(Empty);
2784 break;
2785
2786 case pch::STMT_FOR:
2787 S = new (Context) ForStmt(Empty);
2788 break;
2789
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002790 case pch::STMT_GOTO:
2791 S = new (Context) GotoStmt(Empty);
2792 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002793
2794 case pch::STMT_INDIRECT_GOTO:
2795 S = new (Context) IndirectGotoStmt(Empty);
2796 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002797
Douglas Gregora6b503f2009-04-17 00:16:09 +00002798 case pch::STMT_CONTINUE:
2799 S = new (Context) ContinueStmt(Empty);
2800 break;
2801
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002802 case pch::STMT_BREAK:
2803 S = new (Context) BreakStmt(Empty);
2804 break;
2805
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002806 case pch::STMT_RETURN:
2807 S = new (Context) ReturnStmt(Empty);
2808 break;
2809
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002810 case pch::STMT_DECL:
2811 S = new (Context) DeclStmt(Empty);
2812 break;
2813
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002814 case pch::STMT_ASM:
2815 S = new (Context) AsmStmt(Empty);
2816 break;
2817
Douglas Gregora151ba42009-04-14 23:32:43 +00002818 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002819 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002820 break;
2821
2822 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002823 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002824 break;
2825
2826 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002827 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002828 break;
2829
2830 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002831 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002832 break;
2833
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002834 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002835 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002836 break;
2837
Douglas Gregor596e0932009-04-15 16:35:07 +00002838 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002839 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002840 Record[PCHStmtReader::NumExprFields + 1]);
2841 break;
2842
Douglas Gregora151ba42009-04-14 23:32:43 +00002843 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002844 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002845 break;
2846
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002847 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002848 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002849 break;
2850
Douglas Gregor12d74052009-04-15 15:58:59 +00002851 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002852 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002853 break;
2854
2855 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002856 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002857 break;
2858
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002859 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002860 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002861 break;
2862
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002863 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002864 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002865 break;
2866
2867 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002868 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002869 break;
2870
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002871 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002872 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002873 break;
2874
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002875 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002876 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002877 break;
2878
2879 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002880 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002881 break;
2882
Douglas Gregora151ba42009-04-14 23:32:43 +00002883 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002884 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002885 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002886
2887 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002888 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002889 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002890
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002891 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002892 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002893 break;
2894
Douglas Gregorec0b8292009-04-15 23:02:49 +00002895 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002896 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002897 break;
2898
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002899 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002900 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002901 break;
2902
2903 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002904 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002905 Record[PCHStmtReader::NumExprFields] - 1);
2906
2907 break;
2908
2909 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002910 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002911 break;
2912
Douglas Gregorec0b8292009-04-15 23:02:49 +00002913 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002914 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002915 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002916
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002917 case pch::EXPR_ADDR_LABEL:
2918 S = new (Context) AddrLabelExpr(Empty);
2919 break;
2920
Douglas Gregoreca12f62009-04-17 19:05:30 +00002921 case pch::EXPR_STMT:
2922 S = new (Context) StmtExpr(Empty);
2923 break;
2924
Douglas Gregor209d4622009-04-15 23:33:31 +00002925 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002926 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002927 break;
2928
2929 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002930 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002931 break;
2932
2933 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002934 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002935 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002936
2937 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002938 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002939 break;
2940
Douglas Gregore246b742009-04-17 19:21:43 +00002941 case pch::EXPR_BLOCK:
2942 S = new (Context) BlockExpr(Empty);
2943 break;
2944
Douglas Gregor725e94b2009-04-16 00:01:45 +00002945 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002946 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002947 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00002948
2949 case pch::EXPR_OBJC_ENCODE:
2950 S = new (Context) ObjCEncodeExpr(Empty);
2951 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002952 }
2953
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002954 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002955 if (Finished)
2956 break;
2957
Douglas Gregor456e0952009-04-17 22:13:46 +00002958 ++NumStatementsRead;
2959
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002960 if (S) {
2961 unsigned NumSubStmts = Reader.Visit(S);
2962 while (NumSubStmts > 0) {
2963 StmtStack.pop_back();
2964 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002965 }
2966 }
2967
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002968 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002969 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002970 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002971 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002972 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002973 return StmtStack.back();
2974}
2975
2976Expr *PCHReader::ReadExpr() {
2977 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002978}
2979
Douglas Gregor179cfb12009-04-10 20:39:37 +00002980DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002981 return Diag(SourceLocation(), DiagID);
2982}
2983
2984DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2985 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002986 Context.getSourceManager()),
2987 DiagID);
2988}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002989
Douglas Gregorc713da92009-04-21 22:25:48 +00002990/// \brief Retrieve the identifier table associated with the
2991/// preprocessor.
2992IdentifierTable &PCHReader::getIdentifierTable() {
2993 return PP.getIdentifierTable();
2994}
2995
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002996/// \brief Record that the given ID maps to the given switch-case
2997/// statement.
2998void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2999 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3000 SwitchCaseStmts[ID] = SC;
3001}
3002
3003/// \brief Retrieve the switch-case statement with the given ID.
3004SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3005 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3006 return SwitchCaseStmts[ID];
3007}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003008
3009/// \brief Record that the given label statement has been
3010/// deserialized and has the given ID.
3011void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3012 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3013 "Deserialized label twice");
3014 LabelStmts[ID] = S;
3015
3016 // If we've already seen any goto statements that point to this
3017 // label, resolve them now.
3018 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3019 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3020 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3021 Goto->second->setLabel(S);
3022 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003023
3024 // If we've already seen any address-label statements that point to
3025 // this label, resolve them now.
3026 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3027 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3028 = UnresolvedAddrLabelExprs.equal_range(ID);
3029 for (AddrLabelIter AddrLabel = AddrLabels.first;
3030 AddrLabel != AddrLabels.second; ++AddrLabel)
3031 AddrLabel->second->setLabel(S);
3032 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003033}
3034
3035/// \brief Set the label of the given statement to the label
3036/// identified by ID.
3037///
3038/// Depending on the order in which the label and other statements
3039/// referencing that label occur, this operation may complete
3040/// immediately (updating the statement) or it may queue the
3041/// statement to be back-patched later.
3042void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3043 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3044 if (Label != LabelStmts.end()) {
3045 // We've already seen this label, so set the label of the goto and
3046 // we're done.
3047 S->setLabel(Label->second);
3048 } else {
3049 // We haven't seen this label yet, so add this goto to the set of
3050 // unresolved goto statements.
3051 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3052 }
3053}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003054
3055/// \brief Set the label of the given expression to the label
3056/// identified by ID.
3057///
3058/// Depending on the order in which the label and other statements
3059/// referencing that label occur, this operation may complete
3060/// immediately (updating the statement) or it may queue the
3061/// statement to be back-patched later.
3062void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3063 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3064 if (Label != LabelStmts.end()) {
3065 // We've already seen this label, so set the label of the
3066 // label-address expression and we're done.
3067 S->setLabel(Label->second);
3068 } else {
3069 // We haven't seen this label yet, so add this label-address
3070 // expression to the set of unresolved label-address expressions.
3071 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3072 }
3073}