blob: 300761a0e5d3ccd5863c30e3470658e71b98a23f [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"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000032#include "llvm/Bitcode/BitstreamReader.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include <algorithm>
36#include <cstdio>
37
38using namespace clang;
39
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000040namespace {
41 /// \brief Helper class that saves the current stream position and
42 /// then restores it when destroyed.
43 struct VISIBILITY_HIDDEN SavedStreamPosition {
44 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
45 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
46
47 ~SavedStreamPosition() {
48 Stream.JumpToBit(Offset);
49 }
50
51 private:
52 llvm::BitstreamReader &Stream;
53 uint64_t Offset;
54 };
55}
56
Douglas Gregorc34897d2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000063 PCHReader &Reader;
64 const PCHReader::RecordData &Record;
65 unsigned &Idx;
66
67 public:
68 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
69 unsigned &Idx)
70 : Reader(Reader), Record(Record), Idx(Idx) { }
71
72 void VisitDecl(Decl *D);
73 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
74 void VisitNamedDecl(NamedDecl *ND);
75 void VisitTypeDecl(TypeDecl *TD);
76 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000085 void VisitParmVarDecl(ParmVarDecl *PD);
86 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000087 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
88 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000089 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000090 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *D);
92 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
93 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000094 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
95 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
96 void VisitObjCClassDecl(ObjCClassDecl *D);
97 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
98 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
99 void VisitObjCImplDecl(ObjCImplDecl *D);
100 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
101 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
102 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
103 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
104 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000105 };
106}
107
108void PCHDeclReader::VisitDecl(Decl *D) {
109 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
110 D->setLexicalDeclContext(
111 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
112 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
113 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000114 if (Record[Idx++])
115 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000116 D->setImplicit(Record[Idx++]);
117 D->setAccess((AccessSpecifier)Record[Idx++]);
118}
119
120void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
121 VisitDecl(TU);
122}
123
124void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
125 VisitDecl(ND);
126 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
127}
128
129void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
130 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000131 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
132}
133
134void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000135 // Note that we cannot use VisitTypeDecl here, because we need to
136 // set the underlying type of the typedef *before* we try to read
137 // the type associated with the TypedefDecl.
138 VisitNamedDecl(TD);
139 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
140 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
141 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000142}
143
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000144void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
145 VisitTypeDecl(TD);
146 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
147 TD->setDefinition(Record[Idx++]);
148 TD->setTypedefForAnonDecl(
149 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
150}
151
152void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
153 VisitTagDecl(ED);
154 ED->setIntegerType(Reader.GetType(Record[Idx++]));
155}
156
Douglas Gregor982365e2009-04-13 21:20:57 +0000157void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
158 VisitTagDecl(RD);
159 RD->setHasFlexibleArrayMember(Record[Idx++]);
160 RD->setAnonymousStructOrUnion(Record[Idx++]);
161}
162
Douglas Gregorc34897d2009-04-09 22:27:44 +0000163void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
164 VisitNamedDecl(VD);
165 VD->setType(Reader.GetType(Record[Idx++]));
166}
167
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000168void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
169 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000170 if (Record[Idx++])
171 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000172 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
173}
174
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000175void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
176 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000177 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000178 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179 FD->setPreviousDeclaration(
180 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
181 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
182 FD->setInline(Record[Idx++]);
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000183 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000184 FD->setVirtual(Record[Idx++]);
185 FD->setPure(Record[Idx++]);
186 FD->setInheritedPrototype(Record[Idx++]);
187 FD->setHasPrototype(Record[Idx++]);
188 FD->setDeleted(Record[Idx++]);
189 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
190 unsigned NumParams = Record[Idx++];
191 llvm::SmallVector<ParmVarDecl *, 16> Params;
192 Params.reserve(NumParams);
193 for (unsigned I = 0; I != NumParams; ++I)
194 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
195 FD->setParams(Reader.getContext(), &Params[0], NumParams);
196}
197
Steve Naroff79ea0e02009-04-20 15:06:07 +0000198void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
199 VisitNamedDecl(MD);
200 if (Record[Idx++]) {
201 // In practice, this won't be executed (since method definitions
202 // don't occur in header files).
203 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
204 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
205 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
206 }
207 MD->setInstanceMethod(Record[Idx++]);
208 MD->setVariadic(Record[Idx++]);
209 MD->setSynthesized(Record[Idx++]);
210 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
211 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
212 MD->setResultType(Reader.GetType(Record[Idx++]));
213 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
214 unsigned NumParams = Record[Idx++];
215 llvm::SmallVector<ParmVarDecl *, 16> Params;
216 Params.reserve(NumParams);
217 for (unsigned I = 0; I != NumParams; ++I)
218 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
219 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
220}
221
Steve Naroff7333b492009-04-20 20:09:33 +0000222void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
223 VisitNamedDecl(CD);
224 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
225}
226
227void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
228 VisitObjCContainerDecl(ID);
229 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000230 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
231 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000232 unsigned NumProtocols = Record[Idx++];
233 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
234 Protocols.reserve(NumProtocols);
235 for (unsigned I = 0; I != NumProtocols; ++I)
236 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor5efc1052009-04-24 22:01:00 +0000237 ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext());
Steve Naroff7333b492009-04-20 20:09:33 +0000238 unsigned NumIvars = Record[Idx++];
239 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
240 IVars.reserve(NumIvars);
241 for (unsigned I = 0; I != NumIvars; ++I)
242 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
243 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000244 ID->setCategoryList(
245 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000246 ID->setForwardDecl(Record[Idx++]);
247 ID->setImplicitInterfaceDecl(Record[Idx++]);
248 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
249 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000250 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000251}
252
253void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
254 VisitFieldDecl(IVD);
255 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
256}
257
Steve Naroff97b53bd2009-04-21 15:12:33 +0000258void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
259 VisitObjCContainerDecl(PD);
260 PD->setForwardDecl(Record[Idx++]);
261 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
262 unsigned NumProtoRefs = Record[Idx++];
263 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
264 ProtoRefs.reserve(NumProtoRefs);
265 for (unsigned I = 0; I != NumProtoRefs; ++I)
266 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
267 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
268}
269
270void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
271 VisitFieldDecl(FD);
272}
273
274void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
275 VisitDecl(CD);
276 unsigned NumClassRefs = Record[Idx++];
277 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
278 ClassRefs.reserve(NumClassRefs);
279 for (unsigned I = 0; I != NumClassRefs; ++I)
280 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
281 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
282}
283
284void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
285 VisitDecl(FPD);
286 unsigned NumProtoRefs = Record[Idx++];
287 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
288 ProtoRefs.reserve(NumProtoRefs);
289 for (unsigned I = 0; I != NumProtoRefs; ++I)
290 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
291 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
292}
293
294void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
295 VisitObjCContainerDecl(CD);
296 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
297 unsigned NumProtoRefs = Record[Idx++];
298 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
299 ProtoRefs.reserve(NumProtoRefs);
300 for (unsigned I = 0; I != NumProtoRefs; ++I)
301 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
302 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000303 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000304 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
305}
306
307void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
308 VisitNamedDecl(CAD);
309 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
310}
311
312void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
313 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000314 D->setType(Reader.GetType(Record[Idx++]));
315 // FIXME: stable encoding
316 D->setPropertyAttributes(
317 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
318 // FIXME: stable encoding
319 D->setPropertyImplementation(
320 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
321 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
323 D->setGetterMethodDecl(
324 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
325 D->setSetterMethodDecl(
326 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setPropertyIvarDecl(
328 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000329}
330
331void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000332 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000333 D->setClassInterface(
334 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
335 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000336}
337
338void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
339 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000340 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000341}
342
343void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
344 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000345 D->setSuperClass(
346 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000347}
348
349
350void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
351 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000352 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
353 D->setPropertyDecl(
354 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
355 D->setPropertyIvarDecl(
356 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000357}
358
Douglas Gregor982365e2009-04-13 21:20:57 +0000359void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
360 VisitValueDecl(FD);
361 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000364}
365
Douglas Gregorc34897d2009-04-09 22:27:44 +0000366void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
367 VisitValueDecl(VD);
368 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
369 VD->setThreadSpecified(Record[Idx++]);
370 VD->setCXXDirectInitializer(Record[Idx++]);
371 VD->setDeclaredInCondition(Record[Idx++]);
372 VD->setPreviousDeclaration(
373 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
374 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375 if (Record[Idx++])
376 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000377}
378
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000379void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
380 VisitVarDecl(PD);
381 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000382 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383}
384
385void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
386 VisitParmVarDecl(PD);
387 PD->setOriginalType(Reader.GetType(Record[Idx++]));
388}
389
Douglas Gregor2a491792009-04-13 22:49:25 +0000390void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
391 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000392 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000393}
394
395void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
396 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000397 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000398 unsigned NumParams = Record[Idx++];
399 llvm::SmallVector<ParmVarDecl *, 16> Params;
400 Params.reserve(NumParams);
401 for (unsigned I = 0; I != NumParams; ++I)
402 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
403 BD->setParams(Reader.getContext(), &Params[0], NumParams);
404}
405
Douglas Gregorc34897d2009-04-09 22:27:44 +0000406std::pair<uint64_t, uint64_t>
407PCHDeclReader::VisitDeclContext(DeclContext *DC) {
408 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000409 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000410 return std::make_pair(LexicalOffset, VisibleOffset);
411}
412
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000413//===----------------------------------------------------------------------===//
414// Statement/expression deserialization
415//===----------------------------------------------------------------------===//
416namespace {
417 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000418 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000419 PCHReader &Reader;
420 const PCHReader::RecordData &Record;
421 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000422 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000423
424 public:
425 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000426 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
427 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000428
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000429 /// \brief The number of record fields required for the Stmt class
430 /// itself.
431 static const unsigned NumStmtFields = 0;
432
Douglas Gregor596e0932009-04-15 16:35:07 +0000433 /// \brief The number of record fields required for the Expr class
434 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000435 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000436
Douglas Gregora151ba42009-04-14 23:32:43 +0000437 // Each of the Visit* functions reads in part of the expression
438 // from the given record and the current expression stack, then
439 // return the total number of operands that it read from the
440 // expression stack.
441
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000442 unsigned VisitStmt(Stmt *S);
443 unsigned VisitNullStmt(NullStmt *S);
444 unsigned VisitCompoundStmt(CompoundStmt *S);
445 unsigned VisitSwitchCase(SwitchCase *S);
446 unsigned VisitCaseStmt(CaseStmt *S);
447 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000448 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000449 unsigned VisitIfStmt(IfStmt *S);
450 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000451 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000452 unsigned VisitDoStmt(DoStmt *S);
453 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000455 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000456 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000457 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000458 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000459 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000460 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000461 unsigned VisitExpr(Expr *E);
462 unsigned VisitPredefinedExpr(PredefinedExpr *E);
463 unsigned VisitDeclRefExpr(DeclRefExpr *E);
464 unsigned VisitIntegerLiteral(IntegerLiteral *E);
465 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000466 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000467 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000468 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000469 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000470 unsigned VisitUnaryOperator(UnaryOperator *E);
471 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000472 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000473 unsigned VisitCallExpr(CallExpr *E);
474 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000475 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000476 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000477 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000480 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
481 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000482 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000483 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000484 unsigned VisitInitListExpr(InitListExpr *E);
485 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000488 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000489 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000490 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
491 unsigned VisitChooseExpr(ChooseExpr *E);
492 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000493 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000494 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000495 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000496 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000497 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000498 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
499 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000500 };
501}
502
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000503unsigned PCHStmtReader::VisitStmt(Stmt *S) {
504 assert(Idx == NumStmtFields && "Incorrect statement field count");
505 return 0;
506}
507
508unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
509 VisitStmt(S);
510 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
511 return 0;
512}
513
514unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
515 VisitStmt(S);
516 unsigned NumStmts = Record[Idx++];
517 S->setStmts(Reader.getContext(),
518 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
519 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
520 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
521 return NumStmts;
522}
523
524unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
525 VisitStmt(S);
526 Reader.RecordSwitchCaseID(S, Record[Idx++]);
527 return 0;
528}
529
530unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
531 VisitSwitchCase(S);
532 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
533 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
534 S->setSubStmt(StmtStack.back());
535 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 return 3;
537}
538
539unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
540 VisitSwitchCase(S);
541 S->setSubStmt(StmtStack.back());
542 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
543 return 1;
544}
545
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000546unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
547 VisitStmt(S);
548 S->setID(Reader.GetIdentifierInfo(Record, Idx));
549 S->setSubStmt(StmtStack.back());
550 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
551 Reader.RecordLabelStmt(S, Record[Idx++]);
552 return 1;
553}
554
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000555unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
556 VisitStmt(S);
557 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
558 S->setThen(StmtStack[StmtStack.size() - 2]);
559 S->setElse(StmtStack[StmtStack.size() - 1]);
560 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
561 return 3;
562}
563
564unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
565 VisitStmt(S);
566 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
567 S->setBody(StmtStack.back());
568 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
569 SwitchCase *PrevSC = 0;
570 for (unsigned N = Record.size(); Idx != N; ++Idx) {
571 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
572 if (PrevSC)
573 PrevSC->setNextSwitchCase(SC);
574 else
575 S->setSwitchCaseList(SC);
576 PrevSC = SC;
577 }
578 return 2;
579}
580
Douglas Gregora6b503f2009-04-17 00:16:09 +0000581unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
582 VisitStmt(S);
583 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
584 S->setBody(StmtStack.back());
585 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
586 return 2;
587}
588
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000589unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
590 VisitStmt(S);
591 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
592 S->setBody(StmtStack.back());
593 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 return 2;
595}
596
597unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
598 VisitStmt(S);
599 S->setInit(StmtStack[StmtStack.size() - 4]);
600 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
601 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
602 S->setBody(StmtStack.back());
603 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 4;
605}
606
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000607unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
608 VisitStmt(S);
609 Reader.SetLabelOf(S, Record[Idx++]);
610 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
612 return 0;
613}
614
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000615unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
616 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000617 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000618 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
619 return 1;
620}
621
Douglas Gregora6b503f2009-04-17 00:16:09 +0000622unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
623 VisitStmt(S);
624 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
625 return 0;
626}
627
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000628unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
629 VisitStmt(S);
630 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
631 return 0;
632}
633
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000634unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
635 VisitStmt(S);
636 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
637 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
638 return 1;
639}
640
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000641unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
642 VisitStmt(S);
643 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
644 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
645
646 if (Idx + 1 == Record.size()) {
647 // Single declaration
648 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
649 } else {
650 llvm::SmallVector<Decl *, 16> Decls;
651 Decls.reserve(Record.size() - Idx);
652 for (unsigned N = Record.size(); Idx != N; ++Idx)
653 Decls.push_back(Reader.GetDecl(Record[Idx]));
654 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
655 &Decls[0], Decls.size())));
656 }
657 return 0;
658}
659
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000660unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
661 VisitStmt(S);
662 unsigned NumOutputs = Record[Idx++];
663 unsigned NumInputs = Record[Idx++];
664 unsigned NumClobbers = Record[Idx++];
665 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
666 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
667 S->setVolatile(Record[Idx++]);
668 S->setSimple(Record[Idx++]);
669
670 unsigned StackIdx
671 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
672 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
673
674 // Outputs and inputs
675 llvm::SmallVector<std::string, 16> Names;
676 llvm::SmallVector<StringLiteral*, 16> Constraints;
677 llvm::SmallVector<Stmt*, 16> Exprs;
678 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
679 Names.push_back(Reader.ReadString(Record, Idx));
680 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
681 Exprs.push_back(StmtStack[StackIdx++]);
682 }
683 S->setOutputsAndInputs(NumOutputs, NumInputs,
684 &Names[0], &Constraints[0], &Exprs[0]);
685
686 // Constraints
687 llvm::SmallVector<StringLiteral*, 16> Clobbers;
688 for (unsigned I = 0; I != NumClobbers; ++I)
689 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
690 S->setClobbers(&Clobbers[0], NumClobbers);
691
692 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
693 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
694}
695
Douglas Gregora151ba42009-04-14 23:32:43 +0000696unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000697 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000698 E->setType(Reader.GetType(Record[Idx++]));
699 E->setTypeDependent(Record[Idx++]);
700 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000701 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000702 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000703}
704
Douglas Gregora151ba42009-04-14 23:32:43 +0000705unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000706 VisitExpr(E);
707 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
708 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000709 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000710}
711
Douglas Gregora151ba42009-04-14 23:32:43 +0000712unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000713 VisitExpr(E);
714 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
715 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000716 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000717}
718
Douglas Gregora151ba42009-04-14 23:32:43 +0000719unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000720 VisitExpr(E);
721 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
722 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000723 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000724}
725
Douglas Gregora151ba42009-04-14 23:32:43 +0000726unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000727 VisitExpr(E);
728 E->setValue(Reader.ReadAPFloat(Record, Idx));
729 E->setExact(Record[Idx++]);
730 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000731 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000732}
733
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000734unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
735 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000736 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000737 return 1;
738}
739
Douglas Gregor596e0932009-04-15 16:35:07 +0000740unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
741 VisitExpr(E);
742 unsigned Len = Record[Idx++];
743 assert(Record[Idx] == E->getNumConcatenated() &&
744 "Wrong number of concatenated tokens!");
745 ++Idx;
746 E->setWide(Record[Idx++]);
747
748 // Read string data
749 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
750 E->setStrData(Reader.getContext(), &Str[0], Len);
751 Idx += Len;
752
753 // Read source locations
754 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
755 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
756
757 return 0;
758}
759
Douglas Gregora151ba42009-04-14 23:32:43 +0000760unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000761 VisitExpr(E);
762 E->setValue(Record[Idx++]);
763 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
764 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000765 return 0;
766}
767
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000768unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
769 VisitExpr(E);
770 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
771 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000772 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000773 return 1;
774}
775
Douglas Gregor12d74052009-04-15 15:58:59 +0000776unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
777 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000778 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000779 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
780 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 return 1;
782}
783
784unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
785 VisitExpr(E);
786 E->setSizeof(Record[Idx++]);
787 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000788 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000789 ++Idx;
790 } else {
791 E->setArgument(Reader.GetType(Record[Idx++]));
792 }
793 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 return E->isArgumentType()? 0 : 1;
796}
797
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000798unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
799 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
801 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000802 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
803 return 2;
804}
805
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000806unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
807 VisitExpr(E);
808 E->setNumArgs(Reader.getContext(), Record[Idx++]);
809 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000810 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000811 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000812 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000813 return E->getNumArgs() + 1;
814}
815
816unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
817 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000818 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000819 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
820 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
821 E->setArrow(Record[Idx++]);
822 return 1;
823}
824
Douglas Gregora151ba42009-04-14 23:32:43 +0000825unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
826 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000827 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000828 return 1;
829}
830
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000831unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
832 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000833 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
834 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000835 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
836 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
837 return 2;
838}
839
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000840unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
841 VisitBinaryOperator(E);
842 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
843 E->setComputationResultType(Reader.GetType(Record[Idx++]));
844 return 2;
845}
846
847unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
848 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000849 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
850 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
851 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000852 return 3;
853}
854
Douglas Gregora151ba42009-04-14 23:32:43 +0000855unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
856 VisitCastExpr(E);
857 E->setLvalueCast(Record[Idx++]);
858 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000859}
860
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000861unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
862 VisitCastExpr(E);
863 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
864 return 1;
865}
866
867unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
868 VisitExplicitCastExpr(E);
869 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
871 return 1;
872}
873
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000874unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
875 VisitExpr(E);
876 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000877 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000878 E->setFileScope(Record[Idx++]);
879 return 1;
880}
881
Douglas Gregorec0b8292009-04-15 23:02:49 +0000882unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
883 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000884 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000885 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
886 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
887 return 1;
888}
889
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000890unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
891 VisitExpr(E);
892 unsigned NumInits = Record[Idx++];
893 E->reserveInits(NumInits);
894 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000895 E->updateInit(I,
896 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
897 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000898 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
900 E->setInitializedFieldInUnion(
901 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
902 E->sawArrayRangeDesignator(Record[Idx++]);
903 return NumInits + 1;
904}
905
906unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
907 typedef DesignatedInitExpr::Designator Designator;
908
909 VisitExpr(E);
910 unsigned NumSubExprs = Record[Idx++];
911 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
912 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000913 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000914 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
915 E->setGNUSyntax(Record[Idx++]);
916
917 llvm::SmallVector<Designator, 4> Designators;
918 while (Idx < Record.size()) {
919 switch ((pch::DesignatorTypes)Record[Idx++]) {
920 case pch::DESIG_FIELD_DECL: {
921 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
922 SourceLocation DotLoc
923 = SourceLocation::getFromRawEncoding(Record[Idx++]);
924 SourceLocation FieldLoc
925 = SourceLocation::getFromRawEncoding(Record[Idx++]);
926 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
927 FieldLoc));
928 Designators.back().setField(Field);
929 break;
930 }
931
932 case pch::DESIG_FIELD_NAME: {
933 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
934 SourceLocation DotLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 SourceLocation FieldLoc
937 = SourceLocation::getFromRawEncoding(Record[Idx++]);
938 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
939 break;
940 }
941
942 case pch::DESIG_ARRAY: {
943 unsigned Index = Record[Idx++];
944 SourceLocation LBracketLoc
945 = SourceLocation::getFromRawEncoding(Record[Idx++]);
946 SourceLocation RBracketLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
949 break;
950 }
951
952 case pch::DESIG_ARRAY_RANGE: {
953 unsigned Index = Record[Idx++];
954 SourceLocation LBracketLoc
955 = SourceLocation::getFromRawEncoding(Record[Idx++]);
956 SourceLocation EllipsisLoc
957 = SourceLocation::getFromRawEncoding(Record[Idx++]);
958 SourceLocation RBracketLoc
959 = SourceLocation::getFromRawEncoding(Record[Idx++]);
960 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
961 RBracketLoc));
962 break;
963 }
964 }
965 }
966 E->setDesignators(&Designators[0], Designators.size());
967
968 return NumSubExprs;
969}
970
971unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
972 VisitExpr(E);
973 return 0;
974}
975
Douglas Gregorec0b8292009-04-15 23:02:49 +0000976unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
977 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000978 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000979 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
981 return 1;
982}
983
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000984unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
985 VisitExpr(E);
986 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
988 Reader.SetLabelOf(E, Record[Idx++]);
989 return 0;
990}
991
Douglas Gregoreca12f62009-04-17 19:05:30 +0000992unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
993 VisitExpr(E);
994 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
996 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
997 return 1;
998}
999
Douglas Gregor209d4622009-04-15 23:33:31 +00001000unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1001 VisitExpr(E);
1002 E->setArgType1(Reader.GetType(Record[Idx++]));
1003 E->setArgType2(Reader.GetType(Record[Idx++]));
1004 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1005 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1006 return 0;
1007}
1008
1009unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1010 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001011 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1012 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1013 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001014 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1016 return 3;
1017}
1018
1019unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1020 VisitExpr(E);
1021 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1022 return 0;
1023}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001024
Douglas Gregor725e94b2009-04-16 00:01:45 +00001025unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1026 VisitExpr(E);
1027 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001028 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001029 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1030 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1031 return NumExprs;
1032}
1033
Douglas Gregore246b742009-04-17 19:21:43 +00001034unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1035 VisitExpr(E);
1036 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1037 E->setHasBlockDeclRefExprs(Record[Idx++]);
1038 return 0;
1039}
1040
Douglas Gregor725e94b2009-04-16 00:01:45 +00001041unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1042 VisitExpr(E);
1043 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1044 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1045 E->setByRef(Record[Idx++]);
1046 return 0;
1047}
1048
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001049//===----------------------------------------------------------------------===//
1050// Objective-C Expressions and Statements
1051
1052unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1053 VisitExpr(E);
1054 E->setString(cast<StringLiteral>(StmtStack.back()));
1055 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1056 return 1;
1057}
1058
Chris Lattner80f83c62009-04-22 05:57:30 +00001059unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1060 VisitExpr(E);
1061 E->setEncodedType(Reader.GetType(Record[Idx++]));
1062 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1063 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1064 return 0;
1065}
1066
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001067unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1068 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001069 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001070 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1071 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1072 return 0;
1073}
1074
1075unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1076 VisitExpr(E);
1077 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1078 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1079 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1080 return 0;
1081}
1082
Chris Lattner80f83c62009-04-22 05:57:30 +00001083
Douglas Gregorc713da92009-04-21 22:25:48 +00001084//===----------------------------------------------------------------------===//
1085// PCH reader implementation
1086//===----------------------------------------------------------------------===//
1087
1088namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001089class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1090 PCHReader &Reader;
1091
1092public:
1093 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1094
1095 typedef Selector external_key_type;
1096 typedef external_key_type internal_key_type;
1097
1098 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1099
1100 static bool EqualKey(const internal_key_type& a,
1101 const internal_key_type& b) {
1102 return a == b;
1103 }
1104
1105 static unsigned ComputeHash(Selector Sel) {
1106 unsigned N = Sel.getNumArgs();
1107 if (N == 0)
1108 ++N;
1109 unsigned R = 5381;
1110 for (unsigned I = 0; I != N; ++I)
1111 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1112 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1113 return R;
1114 }
1115
1116 // This hopefully will just get inlined and removed by the optimizer.
1117 static const internal_key_type&
1118 GetInternalKey(const external_key_type& x) { return x; }
1119
1120 static std::pair<unsigned, unsigned>
1121 ReadKeyDataLength(const unsigned char*& d) {
1122 using namespace clang::io;
1123 unsigned KeyLen = ReadUnalignedLE16(d);
1124 unsigned DataLen = ReadUnalignedLE16(d);
1125 return std::make_pair(KeyLen, DataLen);
1126 }
1127
1128 internal_key_type ReadKey(const unsigned char* d, unsigned n) {
1129 using namespace clang::io;
1130 SelectorTable &SelTable = Reader.getContext().Selectors;
1131 unsigned N = ReadUnalignedLE16(d);
1132 IdentifierInfo *FirstII
1133 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1134 if (N == 0)
1135 return SelTable.getNullarySelector(FirstII);
1136 else if (N == 1)
1137 return SelTable.getUnarySelector(FirstII);
1138
1139 llvm::SmallVector<IdentifierInfo *, 16> Args;
1140 Args.push_back(FirstII);
1141 for (unsigned I = 1; I != N; ++I)
1142 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1143
1144 return SelTable.getSelector(N, &Args[0]);
1145 }
1146
1147 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1148 using namespace clang::io;
1149 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1150 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1151
1152 data_type Result;
1153
1154 // Load instance methods
1155 ObjCMethodList *Prev = 0;
1156 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1157 ObjCMethodDecl *Method
1158 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1159 if (!Result.first.Method) {
1160 // This is the first method, which is the easy case.
1161 Result.first.Method = Method;
1162 Prev = &Result.first;
1163 continue;
1164 }
1165
1166 Prev->Next = new ObjCMethodList(Method, 0);
1167 Prev = Prev->Next;
1168 }
1169
1170 // Load factory methods
1171 Prev = 0;
1172 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1173 ObjCMethodDecl *Method
1174 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1175 if (!Result.second.Method) {
1176 // This is the first method, which is the easy case.
1177 Result.second.Method = Method;
1178 Prev = &Result.second;
1179 continue;
1180 }
1181
1182 Prev->Next = new ObjCMethodList(Method, 0);
1183 Prev = Prev->Next;
1184 }
1185
1186 return Result;
1187 }
1188};
1189
1190} // end anonymous namespace
1191
1192/// \brief The on-disk hash table used for the global method pool.
1193typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1194 PCHMethodPoolLookupTable;
1195
1196namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001197class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1198 PCHReader &Reader;
1199
1200 // If we know the IdentifierInfo in advance, it is here and we will
1201 // not build a new one. Used when deserializing information about an
1202 // identifier that was constructed before the PCH file was read.
1203 IdentifierInfo *KnownII;
1204
1205public:
1206 typedef IdentifierInfo * data_type;
1207
1208 typedef const std::pair<const char*, unsigned> external_key_type;
1209
1210 typedef external_key_type internal_key_type;
1211
1212 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1213 : Reader(Reader), KnownII(II) { }
1214
1215 static bool EqualKey(const internal_key_type& a,
1216 const internal_key_type& b) {
1217 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1218 : false;
1219 }
1220
1221 static unsigned ComputeHash(const internal_key_type& a) {
1222 return BernsteinHash(a.first, a.second);
1223 }
1224
1225 // This hopefully will just get inlined and removed by the optimizer.
1226 static const internal_key_type&
1227 GetInternalKey(const external_key_type& x) { return x; }
1228
1229 static std::pair<unsigned, unsigned>
1230 ReadKeyDataLength(const unsigned char*& d) {
1231 using namespace clang::io;
1232 unsigned KeyLen = ReadUnalignedLE16(d);
1233 unsigned DataLen = ReadUnalignedLE16(d);
1234 return std::make_pair(KeyLen, DataLen);
1235 }
1236
1237 static std::pair<const char*, unsigned>
1238 ReadKey(const unsigned char* d, unsigned n) {
1239 assert(n >= 2 && d[n-1] == '\0');
1240 return std::make_pair((const char*) d, n-1);
1241 }
1242
1243 IdentifierInfo *ReadData(const internal_key_type& k,
1244 const unsigned char* d,
1245 unsigned DataLen) {
1246 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001247 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001248 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1249 Bits >>= 1;
1250 bool Poisoned = Bits & 0x01;
1251 Bits >>= 1;
1252 bool ExtensionToken = Bits & 0x01;
1253 Bits >>= 1;
1254 bool hasMacroDefinition = Bits & 0x01;
1255 Bits >>= 1;
1256 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1257 Bits >>= 10;
1258 unsigned TokenID = Bits & 0xFF;
1259 Bits >>= 8;
1260
Douglas Gregorc713da92009-04-21 22:25:48 +00001261 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001262 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001263 DataLen -= 8;
1264
1265 // Build the IdentifierInfo itself and link the identifier ID with
1266 // the new IdentifierInfo.
1267 IdentifierInfo *II = KnownII;
1268 if (!II)
1269 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1270 k.first, k.first + k.second);
1271 Reader.SetIdentifierInfo(ID, II);
1272
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001273 // Set or check the various bits in the IdentifierInfo structure.
1274 // FIXME: Load token IDs lazily, too?
1275 assert((unsigned)II->getTokenID() == TokenID &&
1276 "Incorrect token ID loaded");
1277 (void)TokenID;
1278 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1279 assert(II->isExtensionToken() == ExtensionToken &&
1280 "Incorrect extension token flag");
1281 (void)ExtensionToken;
1282 II->setIsPoisoned(Poisoned);
1283 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1284 "Incorrect C++ operator keyword flag");
1285 (void)CPlusPlusOperatorKeyword;
1286
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001287 // If this identifier is a macro, deserialize the macro
1288 // definition.
1289 if (hasMacroDefinition) {
1290 uint32_t Offset = ReadUnalignedLE64(d);
1291 Reader.ReadMacroRecord(Offset);
1292 DataLen -= 8;
1293 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001294
1295 // Read all of the declarations visible at global scope with this
1296 // name.
1297 Sema *SemaObj = Reader.getSema();
1298 while (DataLen > 0) {
1299 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001300 if (SemaObj) {
1301 // Introduce this declaration into the translation-unit scope
1302 // and add it to the declaration chain for this identifier, so
1303 // that (unqualified) name lookup will find it.
1304 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1305 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1306 } else {
1307 // Queue this declaration so that it will be added to the
1308 // translation unit scope and identifier's declaration chain
1309 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001310 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001311 }
1312
1313 DataLen -= 4;
1314 }
1315 return II;
1316 }
1317};
1318
1319} // end anonymous namespace
1320
1321/// \brief The on-disk hash table used to contain information about
1322/// all of the identifiers in the program.
1323typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1324 PCHIdentifierLookupTable;
1325
Douglas Gregorc34897d2009-04-09 22:27:44 +00001326// FIXME: use the diagnostics machinery
1327static bool Error(const char *Str) {
1328 std::fprintf(stderr, "%s\n", Str);
1329 return true;
1330}
1331
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001332/// \brief Check the contents of the predefines buffer against the
1333/// contents of the predefines buffer used to build the PCH file.
1334///
1335/// The contents of the two predefines buffers should be the same. If
1336/// not, then some command-line option changed the preprocessor state
1337/// and we must reject the PCH file.
1338///
1339/// \param PCHPredef The start of the predefines buffer in the PCH
1340/// file.
1341///
1342/// \param PCHPredefLen The length of the predefines buffer in the PCH
1343/// file.
1344///
1345/// \param PCHBufferID The FileID for the PCH predefines buffer.
1346///
1347/// \returns true if there was a mismatch (in which case the PCH file
1348/// should be ignored), or false otherwise.
1349bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1350 unsigned PCHPredefLen,
1351 FileID PCHBufferID) {
1352 const char *Predef = PP.getPredefines().c_str();
1353 unsigned PredefLen = PP.getPredefines().size();
1354
1355 // If the two predefines buffers compare equal, we're done!.
1356 if (PredefLen == PCHPredefLen &&
1357 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1358 return false;
1359
1360 // The predefines buffers are different. Produce a reasonable
1361 // diagnostic showing where they are different.
1362
1363 // The source locations (potentially in the two different predefines
1364 // buffers)
1365 SourceLocation Loc1, Loc2;
1366 SourceManager &SourceMgr = PP.getSourceManager();
1367
1368 // Create a source buffer for our predefines string, so
1369 // that we can build a diagnostic that points into that
1370 // source buffer.
1371 FileID BufferID;
1372 if (Predef && Predef[0]) {
1373 llvm::MemoryBuffer *Buffer
1374 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1375 "<built-in>");
1376 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1377 }
1378
1379 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1380 std::pair<const char *, const char *> Locations
1381 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1382
1383 if (Locations.first != Predef + MinLen) {
1384 // We found the location in the two buffers where there is a
1385 // difference. Form source locations to point there (in both
1386 // buffers).
1387 unsigned Offset = Locations.first - Predef;
1388 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1389 .getFileLocWithOffset(Offset);
1390 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1391 .getFileLocWithOffset(Offset);
1392 } else if (PredefLen > PCHPredefLen) {
1393 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1394 .getFileLocWithOffset(MinLen);
1395 } else {
1396 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1397 .getFileLocWithOffset(MinLen);
1398 }
1399
1400 Diag(Loc1, diag::warn_pch_preprocessor);
1401 if (Loc2.isValid())
1402 Diag(Loc2, diag::note_predef_in_pch);
1403 Diag(diag::note_ignoring_pch) << FileName;
1404 return true;
1405}
1406
Douglas Gregor635f97f2009-04-13 16:31:14 +00001407/// \brief Read the line table in the source manager block.
1408/// \returns true if ther was an error.
1409static bool ParseLineTable(SourceManager &SourceMgr,
1410 llvm::SmallVectorImpl<uint64_t> &Record) {
1411 unsigned Idx = 0;
1412 LineTableInfo &LineTable = SourceMgr.getLineTable();
1413
1414 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001415 std::map<int, int> FileIDs;
1416 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001417 // Extract the file name
1418 unsigned FilenameLen = Record[Idx++];
1419 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1420 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001421 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1422 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001423 }
1424
1425 // Parse the line entries
1426 std::vector<LineEntry> Entries;
1427 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001428 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001429
1430 // Extract the line entries
1431 unsigned NumEntries = Record[Idx++];
1432 Entries.clear();
1433 Entries.reserve(NumEntries);
1434 for (unsigned I = 0; I != NumEntries; ++I) {
1435 unsigned FileOffset = Record[Idx++];
1436 unsigned LineNo = Record[Idx++];
1437 int FilenameID = Record[Idx++];
1438 SrcMgr::CharacteristicKind FileKind
1439 = (SrcMgr::CharacteristicKind)Record[Idx++];
1440 unsigned IncludeOffset = Record[Idx++];
1441 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1442 FileKind, IncludeOffset));
1443 }
1444 LineTable.AddEntry(FID, Entries);
1445 }
1446
1447 return false;
1448}
1449
Douglas Gregorab1cef72009-04-10 03:52:48 +00001450/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001451PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001452 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001453 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1454 Error("Malformed source manager block record");
1455 return Failure;
1456 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001457
1458 SourceManager &SourceMgr = Context.getSourceManager();
1459 RecordData Record;
1460 while (true) {
1461 unsigned Code = Stream.ReadCode();
1462 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001463 if (Stream.ReadBlockEnd()) {
1464 Error("Error at end of Source Manager block");
1465 return Failure;
1466 }
1467
1468 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001469 }
1470
1471 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1472 // No known subblocks, always skip them.
1473 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001474 if (Stream.SkipBlock()) {
1475 Error("Malformed block record");
1476 return Failure;
1477 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001478 continue;
1479 }
1480
1481 if (Code == llvm::bitc::DEFINE_ABBREV) {
1482 Stream.ReadAbbrevRecord();
1483 continue;
1484 }
1485
1486 // Read a record.
1487 const char *BlobStart;
1488 unsigned BlobLen;
1489 Record.clear();
1490 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1491 default: // Default behavior: ignore.
1492 break;
1493
1494 case pch::SM_SLOC_FILE_ENTRY: {
1495 // FIXME: We would really like to delay the creation of this
1496 // FileEntry until it is actually required, e.g., when producing
1497 // a diagnostic with a source location in this file.
1498 const FileEntry *File
1499 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1500 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001501 FileID ID = SourceMgr.createFileID(File,
1502 SourceLocation::getFromRawEncoding(Record[1]),
1503 (CharacteristicKind)Record[2]);
1504 if (Record[3])
1505 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1506 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001507 break;
1508 }
1509
1510 case pch::SM_SLOC_BUFFER_ENTRY: {
1511 const char *Name = BlobStart;
1512 unsigned Code = Stream.ReadCode();
1513 Record.clear();
1514 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1515 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001516 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001517 llvm::MemoryBuffer *Buffer
1518 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1519 BlobStart + BlobLen - 1,
1520 Name);
1521 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1522
1523 if (strcmp(Name, "<built-in>") == 0
1524 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1525 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001526 break;
1527 }
1528
1529 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1530 SourceLocation SpellingLoc
1531 = SourceLocation::getFromRawEncoding(Record[1]);
1532 SourceMgr.createInstantiationLoc(
1533 SpellingLoc,
1534 SourceLocation::getFromRawEncoding(Record[2]),
1535 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001536 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001537 break;
1538 }
1539
Chris Lattnere1be6022009-04-14 23:22:57 +00001540 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001541 if (ParseLineTable(SourceMgr, Record))
1542 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001543 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001544 }
1545 }
1546}
1547
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001548void PCHReader::ReadMacroRecord(uint64_t Offset) {
1549 // Keep track of where we are in the stream, then jump back there
1550 // after reading this macro.
1551 SavedStreamPosition SavedPosition(Stream);
1552
1553 Stream.JumpToBit(Offset);
1554 RecordData Record;
1555 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1556 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001557
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001558 while (true) {
1559 unsigned Code = Stream.ReadCode();
1560 switch (Code) {
1561 case llvm::bitc::END_BLOCK:
1562 return;
1563
1564 case llvm::bitc::ENTER_SUBBLOCK:
1565 // No known subblocks, always skip them.
1566 Stream.ReadSubBlockID();
1567 if (Stream.SkipBlock()) {
1568 Error("Malformed block record");
1569 return;
1570 }
1571 continue;
1572
1573 case llvm::bitc::DEFINE_ABBREV:
1574 Stream.ReadAbbrevRecord();
1575 continue;
1576 default: break;
1577 }
1578
1579 // Read a record.
1580 Record.clear();
1581 pch::PreprocessorRecordTypes RecType =
1582 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1583 switch (RecType) {
1584 case pch::PP_COUNTER_VALUE:
1585 // Skip this record.
1586 break;
1587
1588 case pch::PP_MACRO_OBJECT_LIKE:
1589 case pch::PP_MACRO_FUNCTION_LIKE: {
1590 // If we already have a macro, that means that we've hit the end
1591 // of the definition of the macro we were looking for. We're
1592 // done.
1593 if (Macro)
1594 return;
1595
1596 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1597 if (II == 0) {
1598 Error("Macro must have a name");
1599 return;
1600 }
1601 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1602 bool isUsed = Record[2];
1603
1604 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1605 MI->setIsUsed(isUsed);
1606
1607 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1608 // Decode function-like macro info.
1609 bool isC99VarArgs = Record[3];
1610 bool isGNUVarArgs = Record[4];
1611 MacroArgs.clear();
1612 unsigned NumArgs = Record[5];
1613 for (unsigned i = 0; i != NumArgs; ++i)
1614 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1615
1616 // Install function-like macro info.
1617 MI->setIsFunctionLike();
1618 if (isC99VarArgs) MI->setIsC99Varargs();
1619 if (isGNUVarArgs) MI->setIsGNUVarargs();
1620 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1621 PP.getPreprocessorAllocator());
1622 }
1623
1624 // Finally, install the macro.
1625 PP.setMacroInfo(II, MI);
1626
1627 // Remember that we saw this macro last so that we add the tokens that
1628 // form its body to it.
1629 Macro = MI;
1630 ++NumMacrosRead;
1631 break;
1632 }
1633
1634 case pch::PP_TOKEN: {
1635 // If we see a TOKEN before a PP_MACRO_*, then the file is
1636 // erroneous, just pretend we didn't see this.
1637 if (Macro == 0) break;
1638
1639 Token Tok;
1640 Tok.startToken();
1641 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1642 Tok.setLength(Record[1]);
1643 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1644 Tok.setIdentifierInfo(II);
1645 Tok.setKind((tok::TokenKind)Record[3]);
1646 Tok.setFlag((Token::TokenFlags)Record[4]);
1647 Macro->AddTokenToBody(Tok);
1648 break;
1649 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001650 case pch::PP_HEADER_FILE_INFO:
1651 break; // Already processed by ReadPreprocessorBlock().
1652 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001653 }
1654}
1655
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001656bool PCHReader::ReadPreprocessorBlock() {
1657 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1658 return Error("Malformed preprocessor block record");
1659
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001660 RecordData Record;
Steve Naroffcda68f22009-04-24 20:03:17 +00001661 unsigned NumHeaderInfos = 0;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001662 while (true) {
1663 unsigned Code = Stream.ReadCode();
1664 switch (Code) {
1665 case llvm::bitc::END_BLOCK:
1666 if (Stream.ReadBlockEnd())
1667 return Error("Error at end of preprocessor block");
1668 return false;
1669
1670 case llvm::bitc::ENTER_SUBBLOCK:
1671 // No known subblocks, always skip them.
1672 Stream.ReadSubBlockID();
1673 if (Stream.SkipBlock())
1674 return Error("Malformed block record");
1675 continue;
1676
1677 case llvm::bitc::DEFINE_ABBREV:
1678 Stream.ReadAbbrevRecord();
1679 continue;
1680 default: break;
1681 }
1682
1683 // Read a record.
1684 Record.clear();
1685 pch::PreprocessorRecordTypes RecType =
1686 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1687 switch (RecType) {
1688 default: // Default behavior: ignore unknown records.
1689 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001690 case pch::PP_COUNTER_VALUE:
1691 if (!Record.empty())
1692 PP.setCounterValue(Record[0]);
1693 break;
1694
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001695 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001696 case pch::PP_MACRO_FUNCTION_LIKE:
1697 case pch::PP_TOKEN:
Steve Naroffcda68f22009-04-24 20:03:17 +00001698 break;
1699 case pch::PP_HEADER_FILE_INFO: {
1700 HeaderFileInfo HFI;
1701 HFI.isImport = Record[0];
1702 HFI.DirInfo = Record[1];
1703 HFI.NumIncludes = Record[2];
1704 HFI.ControllingMacro = DecodeIdentifierInfo(Record[3]);
1705 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1706 break;
1707 }
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001708 }
1709 }
1710}
1711
Steve Naroff9e84d782009-04-23 10:39:46 +00001712bool PCHReader::ReadSelectorBlock() {
1713 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1714 return Error("Malformed selector block record");
1715
1716 RecordData Record;
1717 while (true) {
1718 unsigned Code = Stream.ReadCode();
1719 switch (Code) {
1720 case llvm::bitc::END_BLOCK:
1721 if (Stream.ReadBlockEnd())
1722 return Error("Error at end of preprocessor block");
1723 return false;
1724
1725 case llvm::bitc::ENTER_SUBBLOCK:
1726 // No known subblocks, always skip them.
1727 Stream.ReadSubBlockID();
1728 if (Stream.SkipBlock())
1729 return Error("Malformed block record");
1730 continue;
1731
1732 case llvm::bitc::DEFINE_ABBREV:
1733 Stream.ReadAbbrevRecord();
1734 continue;
1735 default: break;
1736 }
1737
1738 // Read a record.
1739 Record.clear();
1740 pch::PCHRecordTypes RecType =
1741 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1742 switch (RecType) {
1743 default: // Default behavior: ignore unknown records.
1744 break;
1745 case pch::SELECTOR_TABLE:
1746 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1747 unsigned NumSels = Record[Idx++];
1748
1749 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1750 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1751 unsigned NumArgs = Record[Idx++];
1752 KeyIdents.clear();
Steve Naroffc1166732009-04-25 12:07:12 +00001753 if (NumArgs == 0) {
Steve Naroff0a6e5b72009-04-25 12:18:35 +00001754 // If the number of arguments is 0, we must have an Identifier.
Steve Naroff9e84d782009-04-23 10:39:46 +00001755 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1756 assert(II && "DecodeIdentifierInfo returned 0");
1757 KeyIdents.push_back(II);
1758 } else {
Steve Naroffc1166732009-04-25 12:07:12 +00001759 // For keyword selectors, the Identifier is optional (::: is legal!).
Steve Naroff9e84d782009-04-23 10:39:46 +00001760 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1761 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
Steve Naroff9e84d782009-04-23 10:39:46 +00001762 KeyIdents.push_back(II);
1763 }
1764 }
1765 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1766 SelectorData.push_back(Sel);
1767 }
1768 }
1769 }
1770 return false;
1771}
1772
Douglas Gregorc713da92009-04-21 22:25:48 +00001773PCHReader::PCHReadResult
Steve Naroff9e84d782009-04-23 10:39:46 +00001774PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1775 uint64_t &SelectorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001776 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1777 Error("Malformed block record");
1778 return Failure;
1779 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001780
1781 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001782 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001783 while (!Stream.AtEndOfStream()) {
1784 unsigned Code = Stream.ReadCode();
1785 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001786 if (Stream.ReadBlockEnd()) {
1787 Error("Error at end of module block");
1788 return Failure;
1789 }
Chris Lattner29241862009-04-11 21:15:38 +00001790
Douglas Gregor179cfb12009-04-10 20:39:37 +00001791 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001792 }
1793
1794 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1795 switch (Stream.ReadSubBlockID()) {
1796 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1797 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1798 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001799 if (Stream.SkipBlock()) {
1800 Error("Malformed block record");
1801 return Failure;
1802 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001803 break;
1804
Chris Lattner29241862009-04-11 21:15:38 +00001805 case pch::PREPROCESSOR_BLOCK_ID:
1806 // Skip the preprocessor block for now, but remember where it is. We
1807 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001808 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001809 Error("Multiple preprocessor blocks found.");
1810 return Failure;
1811 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001812 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001813 if (Stream.SkipBlock()) {
1814 Error("Malformed block record");
1815 return Failure;
1816 }
1817 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001818
1819 case pch::SELECTOR_BLOCK_ID:
1820 // Skip the selector block for now, but remember where it is. We
1821 // want to read it in after the identifier table.
1822 if (SelectorBlockOffset) {
1823 Error("Multiple selector blocks found.");
1824 return Failure;
1825 }
1826 SelectorBlockOffset = Stream.GetCurrentBitNo();
1827 if (Stream.SkipBlock()) {
1828 Error("Malformed block record");
1829 return Failure;
1830 }
1831 break;
Chris Lattner29241862009-04-11 21:15:38 +00001832
Douglas Gregorab1cef72009-04-10 03:52:48 +00001833 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001834 switch (ReadSourceManagerBlock()) {
1835 case Success:
1836 break;
1837
1838 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001839 Error("Malformed source manager block");
1840 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001841
1842 case IgnorePCH:
1843 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001844 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001845 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001846 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001847 continue;
1848 }
1849
1850 if (Code == llvm::bitc::DEFINE_ABBREV) {
1851 Stream.ReadAbbrevRecord();
1852 continue;
1853 }
1854
1855 // Read and process a record.
1856 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001857 const char *BlobStart = 0;
1858 unsigned BlobLen = 0;
1859 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1860 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001861 default: // Default behavior: ignore.
1862 break;
1863
1864 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001865 if (!TypeOffsets.empty()) {
1866 Error("Duplicate TYPE_OFFSET record in PCH file");
1867 return Failure;
1868 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001869 TypeOffsets.swap(Record);
1870 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1871 break;
1872
1873 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001874 if (!DeclOffsets.empty()) {
1875 Error("Duplicate DECL_OFFSET record in PCH file");
1876 return Failure;
1877 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001878 DeclOffsets.swap(Record);
1879 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1880 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001881
1882 case pch::LANGUAGE_OPTIONS:
1883 if (ParseLanguageOptions(Record))
1884 return IgnorePCH;
1885 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001886
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001887 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001888 std::string TargetTriple(BlobStart, BlobLen);
1889 if (TargetTriple != Context.Target.getTargetTriple()) {
1890 Diag(diag::warn_pch_target_triple)
1891 << TargetTriple << Context.Target.getTargetTriple();
1892 Diag(diag::note_ignoring_pch) << FileName;
1893 return IgnorePCH;
1894 }
1895 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001896 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001897
1898 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001899 IdentifierTableData = BlobStart;
1900 IdentifierLookupTable
1901 = PCHIdentifierLookupTable::Create(
1902 (const unsigned char *)IdentifierTableData + Record[0],
1903 (const unsigned char *)IdentifierTableData,
1904 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001905 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001906 break;
1907
1908 case pch::IDENTIFIER_OFFSET:
1909 if (!IdentifierData.empty()) {
1910 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1911 return Failure;
1912 }
1913 IdentifierData.swap(Record);
1914#ifndef NDEBUG
1915 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1916 if ((IdentifierData[I] & 0x01) == 0) {
1917 Error("Malformed identifier table in the precompiled header");
1918 return Failure;
1919 }
1920 }
1921#endif
1922 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001923
1924 case pch::EXTERNAL_DEFINITIONS:
1925 if (!ExternalDefinitions.empty()) {
1926 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1927 return Failure;
1928 }
1929 ExternalDefinitions.swap(Record);
1930 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001931
Douglas Gregore01ad442009-04-18 05:55:16 +00001932 case pch::SPECIAL_TYPES:
1933 SpecialTypes.swap(Record);
1934 break;
1935
Douglas Gregor456e0952009-04-17 22:13:46 +00001936 case pch::STATISTICS:
1937 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001938 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001939 TotalLexicalDeclContexts = Record[2];
1940 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001941 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001942 case pch::TENTATIVE_DEFINITIONS:
1943 if (!TentativeDefinitions.empty()) {
1944 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1945 return Failure;
1946 }
1947 TentativeDefinitions.swap(Record);
1948 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001949
1950 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1951 if (!LocallyScopedExternalDecls.empty()) {
1952 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1953 return Failure;
1954 }
1955 LocallyScopedExternalDecls.swap(Record);
1956 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001957
1958 case pch::METHOD_POOL:
1959 MethodPoolLookupTable
1960 = PCHMethodPoolLookupTable::Create(
1961 (const unsigned char *)BlobStart + Record[0],
1962 (const unsigned char *)BlobStart,
1963 PCHMethodPoolLookupTrait(*this));
1964 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001965 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001966 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001967 Error("Premature end of bitstream");
1968 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001969}
1970
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001971PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001972 // Set the PCH file name.
1973 this->FileName = FileName;
1974
Douglas Gregorc34897d2009-04-09 22:27:44 +00001975 // Open the PCH file.
1976 std::string ErrStr;
1977 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001978 if (!Buffer) {
1979 Error(ErrStr.c_str());
1980 return IgnorePCH;
1981 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001982
1983 // Initialize the stream
1984 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1985 (const unsigned char *)Buffer->getBufferEnd());
1986
1987 // Sniff for the signature.
1988 if (Stream.Read(8) != 'C' ||
1989 Stream.Read(8) != 'P' ||
1990 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001991 Stream.Read(8) != 'H') {
1992 Error("Not a PCH file");
1993 return IgnorePCH;
1994 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001995
1996 // We expect a number of well-defined blocks, though we don't necessarily
1997 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001998 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff9e84d782009-04-23 10:39:46 +00001999 uint64_t SelectorBlockOffset = 0;
2000
Douglas Gregorc34897d2009-04-09 22:27:44 +00002001 while (!Stream.AtEndOfStream()) {
2002 unsigned Code = Stream.ReadCode();
2003
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002004 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
2005 Error("Invalid record at top-level");
2006 return Failure;
2007 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002008
2009 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00002010
Douglas Gregorc34897d2009-04-09 22:27:44 +00002011 // We only know the PCH subblock ID.
2012 switch (BlockID) {
2013 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002014 if (Stream.ReadBlockInfoBlock()) {
2015 Error("Malformed BlockInfoBlock");
2016 return Failure;
2017 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002018 break;
2019 case pch::PCH_BLOCK_ID:
Steve Naroff9e84d782009-04-23 10:39:46 +00002020 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002021 case Success:
2022 break;
2023
2024 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002025 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002026
2027 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002028 // FIXME: We could consider reading through to the end of this
2029 // PCH block, skipping subblocks, to see if there are other
2030 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002031 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002032 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002033 break;
2034 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002035 if (Stream.SkipBlock()) {
2036 Error("Malformed block record");
2037 return Failure;
2038 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002039 break;
2040 }
2041 }
2042
2043 // Load the translation unit declaration
2044 ReadDeclRecord(DeclOffsets[0], 0);
2045
Douglas Gregorc713da92009-04-21 22:25:48 +00002046 // Initialization of builtins and library builtins occurs before the
2047 // PCH file is read, so there may be some identifiers that were
2048 // loaded into the IdentifierTable before we intercepted the
2049 // creation of identifiers. Iterate through the list of known
2050 // identifiers and determine whether we have to establish
2051 // preprocessor definitions or top-level identifier declaration
2052 // chains for those identifiers.
2053 //
2054 // We copy the IdentifierInfo pointers to a small vector first,
2055 // since de-serializing declarations or macro definitions can add
2056 // new entries into the identifier table, invalidating the
2057 // iterators.
2058 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2059 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2060 IdEnd = PP.getIdentifierTable().end();
2061 Id != IdEnd; ++Id)
2062 Identifiers.push_back(Id->second);
2063 PCHIdentifierLookupTable *IdTable
2064 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2065 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2066 IdentifierInfo *II = Identifiers[I];
2067 // Look in the on-disk hash table for an entry for
2068 PCHIdentifierLookupTrait Info(*this, II);
2069 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2070 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2071 if (Pos == IdTable->end())
2072 continue;
2073
2074 // Dereferencing the iterator has the effect of populating the
2075 // IdentifierInfo node with the various declarations it needs.
2076 (void)*Pos;
2077 }
2078
Douglas Gregore01ad442009-04-18 05:55:16 +00002079 // Load the special types.
2080 Context.setBuiltinVaListType(
2081 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002082 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2083 Context.setObjCIdType(GetType(Id));
2084 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2085 Context.setObjCSelType(GetType(Sel));
2086 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2087 Context.setObjCProtoType(GetType(Proto));
2088 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2089 Context.setObjCClassType(GetType(Class));
2090 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2091 Context.setCFConstantStringType(GetType(String));
2092 if (unsigned FastEnum
2093 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2094 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc713da92009-04-21 22:25:48 +00002095 // If we saw the preprocessor block, read it now.
2096 if (PreprocessorBlockOffset) {
2097 SavedStreamPosition SavedPos(Stream);
2098 Stream.JumpToBit(PreprocessorBlockOffset);
2099 if (ReadPreprocessorBlock()) {
2100 Error("Malformed preprocessor block");
2101 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002102 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002103 }
Steve Naroff9e84d782009-04-23 10:39:46 +00002104 if (SelectorBlockOffset) {
2105 SavedStreamPosition SavedPos(Stream);
2106 Stream.JumpToBit(SelectorBlockOffset);
2107 if (ReadSelectorBlock()) {
2108 Error("Malformed preprocessor block");
2109 return Failure;
2110 }
2111 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002112
Douglas Gregorc713da92009-04-21 22:25:48 +00002113 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002114}
2115
Douglas Gregor179cfb12009-04-10 20:39:37 +00002116/// \brief Parse the record that corresponds to a LangOptions data
2117/// structure.
2118///
2119/// This routine compares the language options used to generate the
2120/// PCH file against the language options set for the current
2121/// compilation. For each option, we classify differences between the
2122/// two compiler states as either "benign" or "important". Benign
2123/// differences don't matter, and we accept them without complaint
2124/// (and without modifying the language options). Differences between
2125/// the states for important options cause the PCH file to be
2126/// unusable, so we emit a warning and return true to indicate that
2127/// there was an error.
2128///
2129/// \returns true if the PCH file is unacceptable, false otherwise.
2130bool PCHReader::ParseLanguageOptions(
2131 const llvm::SmallVectorImpl<uint64_t> &Record) {
2132 const LangOptions &LangOpts = Context.getLangOptions();
2133#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2134#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2135 if (Record[Idx] != LangOpts.Option) { \
2136 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2137 Diag(diag::note_ignoring_pch) << FileName; \
2138 return true; \
2139 } \
2140 ++Idx
2141
2142 unsigned Idx = 0;
2143 PARSE_LANGOPT_BENIGN(Trigraphs);
2144 PARSE_LANGOPT_BENIGN(BCPLComment);
2145 PARSE_LANGOPT_BENIGN(DollarIdents);
2146 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2147 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2148 PARSE_LANGOPT_BENIGN(ImplicitInt);
2149 PARSE_LANGOPT_BENIGN(Digraphs);
2150 PARSE_LANGOPT_BENIGN(HexFloats);
2151 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2152 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2153 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2154 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2155 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2156 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2157 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2158 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2159 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2160 PARSE_LANGOPT_BENIGN(PascalStrings);
2161 PARSE_LANGOPT_BENIGN(Boolean);
2162 PARSE_LANGOPT_BENIGN(WritableStrings);
2163 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2164 diag::warn_pch_lax_vector_conversions);
2165 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2166 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2167 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2168 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2169 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2170 diag::warn_pch_thread_safe_statics);
2171 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2172 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2173 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2174 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2175 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2176 diag::warn_pch_heinous_extensions);
2177 // FIXME: Most of the options below are benign if the macro wasn't
2178 // used. Unfortunately, this means that a PCH compiled without
2179 // optimization can't be used with optimization turned on, even
2180 // though the only thing that changes is whether __OPTIMIZE__ was
2181 // defined... but if __OPTIMIZE__ never showed up in the header, it
2182 // doesn't matter. We could consider making this some special kind
2183 // of check.
2184 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2185 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2186 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2187 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2188 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2189 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2190 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2191 Diag(diag::warn_pch_gc_mode)
2192 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2193 Diag(diag::note_ignoring_pch) << FileName;
2194 return true;
2195 }
2196 ++Idx;
2197 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2198 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2199#undef PARSE_LANGOPT_IRRELEVANT
2200#undef PARSE_LANGOPT_BENIGN
2201
2202 return false;
2203}
2204
Douglas Gregorc34897d2009-04-09 22:27:44 +00002205/// \brief Read and return the type at the given offset.
2206///
2207/// This routine actually reads the record corresponding to the type
2208/// at the given offset in the bitstream. It is a helper routine for
2209/// GetType, which deals with reading type IDs.
2210QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002211 // Keep track of where we are in the stream, then jump back there
2212 // after reading this type.
2213 SavedStreamPosition SavedPosition(Stream);
2214
Douglas Gregorc34897d2009-04-09 22:27:44 +00002215 Stream.JumpToBit(Offset);
2216 RecordData Record;
2217 unsigned Code = Stream.ReadCode();
2218 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002219 case pch::TYPE_EXT_QUAL: {
2220 assert(Record.size() == 3 &&
2221 "Incorrect encoding of extended qualifier type");
2222 QualType Base = GetType(Record[0]);
2223 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2224 unsigned AddressSpace = Record[2];
2225
2226 QualType T = Base;
2227 if (GCAttr != QualType::GCNone)
2228 T = Context.getObjCGCQualType(T, GCAttr);
2229 if (AddressSpace)
2230 T = Context.getAddrSpaceQualType(T, AddressSpace);
2231 return T;
2232 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002233
Douglas Gregorc34897d2009-04-09 22:27:44 +00002234 case pch::TYPE_FIXED_WIDTH_INT: {
2235 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2236 return Context.getFixedWidthIntType(Record[0], Record[1]);
2237 }
2238
2239 case pch::TYPE_COMPLEX: {
2240 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2241 QualType ElemType = GetType(Record[0]);
2242 return Context.getComplexType(ElemType);
2243 }
2244
2245 case pch::TYPE_POINTER: {
2246 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2247 QualType PointeeType = GetType(Record[0]);
2248 return Context.getPointerType(PointeeType);
2249 }
2250
2251 case pch::TYPE_BLOCK_POINTER: {
2252 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2253 QualType PointeeType = GetType(Record[0]);
2254 return Context.getBlockPointerType(PointeeType);
2255 }
2256
2257 case pch::TYPE_LVALUE_REFERENCE: {
2258 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2259 QualType PointeeType = GetType(Record[0]);
2260 return Context.getLValueReferenceType(PointeeType);
2261 }
2262
2263 case pch::TYPE_RVALUE_REFERENCE: {
2264 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2265 QualType PointeeType = GetType(Record[0]);
2266 return Context.getRValueReferenceType(PointeeType);
2267 }
2268
2269 case pch::TYPE_MEMBER_POINTER: {
2270 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2271 QualType PointeeType = GetType(Record[0]);
2272 QualType ClassType = GetType(Record[1]);
2273 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2274 }
2275
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002276 case pch::TYPE_CONSTANT_ARRAY: {
2277 QualType ElementType = GetType(Record[0]);
2278 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2279 unsigned IndexTypeQuals = Record[2];
2280 unsigned Idx = 3;
2281 llvm::APInt Size = ReadAPInt(Record, Idx);
2282 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2283 }
2284
2285 case pch::TYPE_INCOMPLETE_ARRAY: {
2286 QualType ElementType = GetType(Record[0]);
2287 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2288 unsigned IndexTypeQuals = Record[2];
2289 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2290 }
2291
2292 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002293 QualType ElementType = GetType(Record[0]);
2294 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2295 unsigned IndexTypeQuals = Record[2];
2296 return Context.getVariableArrayType(ElementType, ReadExpr(),
2297 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002298 }
2299
2300 case pch::TYPE_VECTOR: {
2301 if (Record.size() != 2) {
2302 Error("Incorrect encoding of vector type in PCH file");
2303 return QualType();
2304 }
2305
2306 QualType ElementType = GetType(Record[0]);
2307 unsigned NumElements = Record[1];
2308 return Context.getVectorType(ElementType, NumElements);
2309 }
2310
2311 case pch::TYPE_EXT_VECTOR: {
2312 if (Record.size() != 2) {
2313 Error("Incorrect encoding of extended vector type in PCH file");
2314 return QualType();
2315 }
2316
2317 QualType ElementType = GetType(Record[0]);
2318 unsigned NumElements = Record[1];
2319 return Context.getExtVectorType(ElementType, NumElements);
2320 }
2321
2322 case pch::TYPE_FUNCTION_NO_PROTO: {
2323 if (Record.size() != 1) {
2324 Error("Incorrect encoding of no-proto function type");
2325 return QualType();
2326 }
2327 QualType ResultType = GetType(Record[0]);
2328 return Context.getFunctionNoProtoType(ResultType);
2329 }
2330
2331 case pch::TYPE_FUNCTION_PROTO: {
2332 QualType ResultType = GetType(Record[0]);
2333 unsigned Idx = 1;
2334 unsigned NumParams = Record[Idx++];
2335 llvm::SmallVector<QualType, 16> ParamTypes;
2336 for (unsigned I = 0; I != NumParams; ++I)
2337 ParamTypes.push_back(GetType(Record[Idx++]));
2338 bool isVariadic = Record[Idx++];
2339 unsigned Quals = Record[Idx++];
2340 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2341 isVariadic, Quals);
2342 }
2343
2344 case pch::TYPE_TYPEDEF:
2345 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2346 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2347
2348 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002349 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002350
2351 case pch::TYPE_TYPEOF: {
2352 if (Record.size() != 1) {
2353 Error("Incorrect encoding of typeof(type) in PCH file");
2354 return QualType();
2355 }
2356 QualType UnderlyingType = GetType(Record[0]);
2357 return Context.getTypeOfType(UnderlyingType);
2358 }
2359
2360 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002361 assert(Record.size() == 1 && "Incorrect encoding of record type");
2362 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002363
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002364 case pch::TYPE_ENUM:
2365 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2366 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2367
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002368 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002369 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2370 return Context.getObjCInterfaceType(
2371 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002372
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002373 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2374 unsigned Idx = 0;
2375 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2376 unsigned NumProtos = Record[Idx++];
2377 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2378 for (unsigned I = 0; I != NumProtos; ++I)
2379 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2380 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2381 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002382
Chris Lattner9b9f2352009-04-22 06:40:03 +00002383 case pch::TYPE_OBJC_QUALIFIED_ID: {
2384 unsigned Idx = 0;
2385 unsigned NumProtos = Record[Idx++];
2386 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2387 for (unsigned I = 0; I != NumProtos; ++I)
2388 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2389 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2390 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002391 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002392 // Suppress a GCC warning
2393 return QualType();
2394}
2395
2396/// \brief Note that we have loaded the declaration with the given
2397/// Index.
2398///
2399/// This routine notes that this declaration has already been loaded,
2400/// so that future GetDecl calls will return this declaration rather
2401/// than trying to load a new declaration.
2402inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2403 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2404 DeclAlreadyLoaded[Index] = true;
2405 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2406}
2407
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002408/// \brief Determine whether the consumer will be interested in seeing
2409/// this declaration (via HandleTopLevelDecl).
2410///
2411/// This routine should return true for anything that might affect
2412/// code generation, e.g., inline function definitions, Objective-C
2413/// declarations with metadata, etc.
2414static bool isConsumerInterestedIn(Decl *D) {
2415 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2416 return Var->isFileVarDecl() && Var->getInit();
2417 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2418 return Func->isThisDeclarationADefinition();
2419 return isa<ObjCProtocolDecl>(D);
2420}
2421
Douglas Gregorc34897d2009-04-09 22:27:44 +00002422/// \brief Read the declaration at the given offset from the PCH file.
2423Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002424 // Keep track of where we are in the stream, then jump back there
2425 // after reading this declaration.
2426 SavedStreamPosition SavedPosition(Stream);
2427
Douglas Gregorc34897d2009-04-09 22:27:44 +00002428 Decl *D = 0;
2429 Stream.JumpToBit(Offset);
2430 RecordData Record;
2431 unsigned Code = Stream.ReadCode();
2432 unsigned Idx = 0;
2433 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002434
Douglas Gregorc34897d2009-04-09 22:27:44 +00002435 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002436 case pch::DECL_ATTR:
2437 case pch::DECL_CONTEXT_LEXICAL:
2438 case pch::DECL_CONTEXT_VISIBLE:
2439 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2440 break;
2441
Douglas Gregorc34897d2009-04-09 22:27:44 +00002442 case pch::DECL_TRANSLATION_UNIT:
2443 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002444 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002445 break;
2446
2447 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002448 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002449 break;
2450 }
2451
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002452 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002453 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002454 break;
2455 }
2456
Douglas Gregor982365e2009-04-13 21:20:57 +00002457 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002458 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2459 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002460 break;
2461 }
2462
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002463 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002464 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2465 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002466 break;
2467 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002468
2469 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002470 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2471 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002472 break;
2473 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002474
Steve Naroff79ea0e02009-04-20 15:06:07 +00002475 case pch::DECL_OBJC_METHOD: {
2476 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2477 Selector(), QualType(), 0);
2478 break;
2479 }
2480
Steve Naroff97b53bd2009-04-21 15:12:33 +00002481 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002482 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2483 break;
2484 }
2485
Steve Naroff97b53bd2009-04-21 15:12:33 +00002486 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002487 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2488 ObjCIvarDecl::None);
2489 break;
2490 }
2491
Steve Naroff97b53bd2009-04-21 15:12:33 +00002492 case pch::DECL_OBJC_PROTOCOL: {
2493 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2494 break;
2495 }
2496
2497 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2498 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2499 QualType(), 0);
2500 break;
2501 }
2502
2503 case pch::DECL_OBJC_CLASS: {
2504 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2505 break;
2506 }
2507
2508 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2509 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2510 break;
2511 }
2512
2513 case pch::DECL_OBJC_CATEGORY: {
2514 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2515 break;
2516 }
2517
2518 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002519 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002520 break;
2521 }
2522
2523 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002524 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002525 break;
2526 }
2527
2528 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002529 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002530 break;
2531 }
2532
2533 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002534 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002535 break;
2536 }
2537
2538 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002539 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2540 SourceLocation(), 0,
2541 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002542 break;
2543 }
2544
Douglas Gregor982365e2009-04-13 21:20:57 +00002545 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002546 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2547 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002548 break;
2549 }
2550
Douglas Gregorc34897d2009-04-09 22:27:44 +00002551 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002552 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2553 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002554 break;
2555 }
2556
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002557 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002558 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2559 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002560 break;
2561 }
2562
2563 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002564 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002565 QualType(), QualType(), VarDecl::None,
2566 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002567 break;
2568 }
2569
Douglas Gregor2a491792009-04-13 22:49:25 +00002570 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002571 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002572 break;
2573 }
2574
2575 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002576 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002577 break;
2578 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002579 }
2580
Douglas Gregorc713da92009-04-21 22:25:48 +00002581 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002582 if (D) {
2583 LoadedDecl(Index, D);
2584 Reader.Visit(D);
2585 }
2586
Douglas Gregorc34897d2009-04-09 22:27:44 +00002587 // If this declaration is also a declaration context, get the
2588 // offsets for its tables of lexical and visible declarations.
2589 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2590 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2591 if (Offsets.first || Offsets.second) {
2592 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2593 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2594 DeclContextOffsets[DC] = Offsets;
2595 }
2596 }
2597 assert(Idx == Record.size());
2598
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002599 // If we have deserialized a declaration that has a definition the
2600 // AST consumer might need to know about, notify the consumer
2601 // about that definition now or queue it for later.
2602 if (isConsumerInterestedIn(D)) {
2603 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002604 DeclGroupRef DG(D);
2605 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002606 } else {
2607 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002608 }
2609 }
2610
Douglas Gregorc34897d2009-04-09 22:27:44 +00002611 return D;
2612}
2613
Douglas Gregorac8f2802009-04-10 17:25:41 +00002614QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002615 unsigned Quals = ID & 0x07;
2616 unsigned Index = ID >> 3;
2617
2618 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2619 QualType T;
2620 switch ((pch::PredefinedTypeIDs)Index) {
2621 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2622 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2623 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2624
2625 case pch::PREDEF_TYPE_CHAR_U_ID:
2626 case pch::PREDEF_TYPE_CHAR_S_ID:
2627 // FIXME: Check that the signedness of CharTy is correct!
2628 T = Context.CharTy;
2629 break;
2630
2631 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2632 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2633 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2634 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2635 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2636 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2637 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2638 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2639 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2640 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2641 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2642 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2643 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2644 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2645 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2646 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2647 }
2648
2649 assert(!T.isNull() && "Unknown predefined type");
2650 return T.getQualifiedType(Quals);
2651 }
2652
2653 Index -= pch::NUM_PREDEF_TYPE_IDS;
2654 if (!TypeAlreadyLoaded[Index]) {
2655 // Load the type from the PCH file.
2656 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2657 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2658 TypeAlreadyLoaded[Index] = true;
2659 }
2660
2661 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2662}
2663
Douglas Gregorac8f2802009-04-10 17:25:41 +00002664Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002665 if (ID == 0)
2666 return 0;
2667
2668 unsigned Index = ID - 1;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002669 assert(Index < DeclAlreadyLoaded.size() && "Declaration ID out of range");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002670 if (DeclAlreadyLoaded[Index])
2671 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2672
2673 // Load the declaration from the PCH file.
2674 return ReadDeclRecord(DeclOffsets[Index], Index);
2675}
2676
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002677Stmt *PCHReader::GetStmt(uint64_t Offset) {
2678 // Keep track of where we are in the stream, then jump back there
2679 // after reading this declaration.
2680 SavedStreamPosition SavedPosition(Stream);
2681
2682 Stream.JumpToBit(Offset);
2683 return ReadStmt();
2684}
2685
Douglas Gregorc34897d2009-04-09 22:27:44 +00002686bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002687 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002688 assert(DC->hasExternalLexicalStorage() &&
2689 "DeclContext has no lexical decls in storage");
2690 uint64_t Offset = DeclContextOffsets[DC].first;
2691 assert(Offset && "DeclContext has no lexical decls in storage");
2692
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002693 // Keep track of where we are in the stream, then jump back there
2694 // after reading this context.
2695 SavedStreamPosition SavedPosition(Stream);
2696
Douglas Gregorc34897d2009-04-09 22:27:44 +00002697 // Load the record containing all of the declarations lexically in
2698 // this context.
2699 Stream.JumpToBit(Offset);
2700 RecordData Record;
2701 unsigned Code = Stream.ReadCode();
2702 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002703 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002704 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2705
2706 // Load all of the declaration IDs
2707 Decls.clear();
2708 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002709 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002710 return false;
2711}
2712
2713bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2714 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2715 assert(DC->hasExternalVisibleStorage() &&
2716 "DeclContext has no visible decls in storage");
2717 uint64_t Offset = DeclContextOffsets[DC].second;
2718 assert(Offset && "DeclContext has no visible decls in storage");
2719
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002720 // Keep track of where we are in the stream, then jump back there
2721 // after reading this context.
2722 SavedStreamPosition SavedPosition(Stream);
2723
Douglas Gregorc34897d2009-04-09 22:27:44 +00002724 // Load the record containing all of the declarations visible in
2725 // this context.
2726 Stream.JumpToBit(Offset);
2727 RecordData Record;
2728 unsigned Code = Stream.ReadCode();
2729 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002730 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002731 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2732 if (Record.size() == 0)
2733 return false;
2734
2735 Decls.clear();
2736
2737 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002738 while (Idx < Record.size()) {
2739 Decls.push_back(VisibleDeclaration());
2740 Decls.back().Name = ReadDeclarationName(Record, Idx);
2741
Douglas Gregorc34897d2009-04-09 22:27:44 +00002742 unsigned Size = Record[Idx++];
2743 llvm::SmallVector<unsigned, 4> & LoadedDecls
2744 = Decls.back().Declarations;
2745 LoadedDecls.reserve(Size);
2746 for (unsigned I = 0; I < Size; ++I)
2747 LoadedDecls.push_back(Record[Idx++]);
2748 }
2749
Douglas Gregoraf136d92009-04-22 22:34:57 +00002750 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002751 return false;
2752}
2753
Douglas Gregor631f6c62009-04-14 00:24:19 +00002754void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002755 this->Consumer = Consumer;
2756
Douglas Gregor631f6c62009-04-14 00:24:19 +00002757 if (!Consumer)
2758 return;
2759
2760 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2761 Decl *D = GetDecl(ExternalDefinitions[I]);
2762 DeclGroupRef DG(D);
2763 Consumer->HandleTopLevelDecl(DG);
2764 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002765
2766 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2767 DeclGroupRef DG(InterestingDecls[I]);
2768 Consumer->HandleTopLevelDecl(DG);
2769 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002770}
2771
Douglas Gregorc34897d2009-04-09 22:27:44 +00002772void PCHReader::PrintStats() {
2773 std::fprintf(stderr, "*** PCH Statistics:\n");
2774
2775 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2776 TypeAlreadyLoaded.end(),
2777 true);
2778 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2779 DeclAlreadyLoaded.end(),
2780 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002781 unsigned NumIdentifiersLoaded = 0;
2782 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2783 if ((IdentifierData[I] & 0x01) == 0)
2784 ++NumIdentifiersLoaded;
2785 }
2786
Douglas Gregorc34897d2009-04-09 22:27:44 +00002787 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2788 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002789 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002790 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2791 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002792 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2793 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2794 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2795 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002796 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2797 NumStatementsRead, TotalNumStatements,
2798 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002799 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2800 NumMacrosRead, TotalNumMacros,
2801 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002802 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2803 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2804 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2805 * 100));
2806 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2807 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2808 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2809 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002810 std::fprintf(stderr, "\n");
2811}
2812
Douglas Gregorc713da92009-04-21 22:25:48 +00002813void PCHReader::InitializeSema(Sema &S) {
2814 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002815 S.ExternalSource = this;
2816
Douglas Gregor2554cf22009-04-22 21:15:06 +00002817 // Makes sure any declarations that were deserialized "too early"
2818 // still get added to the identifier's declaration chains.
2819 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2820 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2821 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002822 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002823 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002824
2825 // If there were any tentative definitions, deserialize them and add
2826 // them to Sema's table of tentative definitions.
2827 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2828 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2829 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2830 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002831
2832 // If there were any locally-scoped external declarations,
2833 // deserialize them and add them to Sema's table of locally-scoped
2834 // external declarations.
2835 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2836 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2837 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2838 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002839}
2840
2841IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2842 // Try to find this name within our on-disk hash table
2843 PCHIdentifierLookupTable *IdTable
2844 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2845 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2846 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2847 if (Pos == IdTable->end())
2848 return 0;
2849
2850 // Dereferencing the iterator has the effect of building the
2851 // IdentifierInfo node and populating it with the various
2852 // declarations it needs.
2853 return *Pos;
2854}
2855
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002856std::pair<ObjCMethodList, ObjCMethodList>
2857PCHReader::ReadMethodPool(Selector Sel) {
2858 if (!MethodPoolLookupTable)
2859 return std::pair<ObjCMethodList, ObjCMethodList>();
2860
2861 // Try to find this selector within our on-disk hash table.
2862 PCHMethodPoolLookupTable *PoolTable
2863 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2864 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
2865 if (Pos == PoolTable->end())
2866 return std::pair<ObjCMethodList, ObjCMethodList>();;
2867
2868 return *Pos;
2869}
2870
Douglas Gregorc713da92009-04-21 22:25:48 +00002871void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2872 assert(ID && "Non-zero identifier ID required");
2873 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2874}
2875
Chris Lattner29241862009-04-11 21:15:38 +00002876IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002877 if (ID == 0)
2878 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002879
Douglas Gregorc713da92009-04-21 22:25:48 +00002880 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002881 Error("No identifier table in PCH file");
2882 return 0;
2883 }
Chris Lattner29241862009-04-11 21:15:38 +00002884
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002885 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002886 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002887 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002888 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002889 }
Chris Lattner29241862009-04-11 21:15:38 +00002890
2891 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002892}
2893
Steve Naroff9e84d782009-04-23 10:39:46 +00002894Selector PCHReader::DecodeSelector(unsigned ID) {
2895 if (ID == 0)
2896 return Selector();
2897
2898 if (SelectorData.empty()) {
2899 Error("No selector table in PCH file");
2900 return Selector();
2901 }
2902
2903 if (ID > SelectorData.size()) {
2904 Error("Selector ID out of range");
2905 return Selector();
2906 }
2907 return SelectorData[ID-1];
2908}
2909
Douglas Gregorc34897d2009-04-09 22:27:44 +00002910DeclarationName
2911PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2912 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2913 switch (Kind) {
2914 case DeclarationName::Identifier:
2915 return DeclarationName(GetIdentifierInfo(Record, Idx));
2916
2917 case DeclarationName::ObjCZeroArgSelector:
2918 case DeclarationName::ObjCOneArgSelector:
2919 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002920 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002921
2922 case DeclarationName::CXXConstructorName:
2923 return Context.DeclarationNames.getCXXConstructorName(
2924 GetType(Record[Idx++]));
2925
2926 case DeclarationName::CXXDestructorName:
2927 return Context.DeclarationNames.getCXXDestructorName(
2928 GetType(Record[Idx++]));
2929
2930 case DeclarationName::CXXConversionFunctionName:
2931 return Context.DeclarationNames.getCXXConversionFunctionName(
2932 GetType(Record[Idx++]));
2933
2934 case DeclarationName::CXXOperatorName:
2935 return Context.DeclarationNames.getCXXOperatorName(
2936 (OverloadedOperatorKind)Record[Idx++]);
2937
2938 case DeclarationName::CXXUsingDirective:
2939 return DeclarationName::getUsingDirectiveName();
2940 }
2941
2942 // Required to silence GCC warning
2943 return DeclarationName();
2944}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002945
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002946/// \brief Read an integral value
2947llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2948 unsigned BitWidth = Record[Idx++];
2949 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2950 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2951 Idx += NumWords;
2952 return Result;
2953}
2954
2955/// \brief Read a signed integral value
2956llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2957 bool isUnsigned = Record[Idx++];
2958 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2959}
2960
Douglas Gregore2f37202009-04-14 21:55:33 +00002961/// \brief Read a floating-point value
2962llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002963 return llvm::APFloat(ReadAPInt(Record, Idx));
2964}
2965
Douglas Gregor1c507882009-04-15 21:30:51 +00002966// \brief Read a string
2967std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2968 unsigned Len = Record[Idx++];
2969 std::string Result(&Record[Idx], &Record[Idx] + Len);
2970 Idx += Len;
2971 return Result;
2972}
2973
2974/// \brief Reads attributes from the current stream position.
2975Attr *PCHReader::ReadAttributes() {
2976 unsigned Code = Stream.ReadCode();
2977 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2978 "Expected unabbreviated record"); (void)Code;
2979
2980 RecordData Record;
2981 unsigned Idx = 0;
2982 unsigned RecCode = Stream.ReadRecord(Code, Record);
2983 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2984 (void)RecCode;
2985
2986#define SIMPLE_ATTR(Name) \
2987 case Attr::Name: \
2988 New = ::new (Context) Name##Attr(); \
2989 break
2990
2991#define STRING_ATTR(Name) \
2992 case Attr::Name: \
2993 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2994 break
2995
2996#define UNSIGNED_ATTR(Name) \
2997 case Attr::Name: \
2998 New = ::new (Context) Name##Attr(Record[Idx++]); \
2999 break
3000
3001 Attr *Attrs = 0;
3002 while (Idx < Record.size()) {
3003 Attr *New = 0;
3004 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
3005 bool IsInherited = Record[Idx++];
3006
3007 switch (Kind) {
3008 STRING_ATTR(Alias);
3009 UNSIGNED_ATTR(Aligned);
3010 SIMPLE_ATTR(AlwaysInline);
3011 SIMPLE_ATTR(AnalyzerNoReturn);
3012 STRING_ATTR(Annotate);
3013 STRING_ATTR(AsmLabel);
3014
3015 case Attr::Blocks:
3016 New = ::new (Context) BlocksAttr(
3017 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3018 break;
3019
3020 case Attr::Cleanup:
3021 New = ::new (Context) CleanupAttr(
3022 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3023 break;
3024
3025 SIMPLE_ATTR(Const);
3026 UNSIGNED_ATTR(Constructor);
3027 SIMPLE_ATTR(DLLExport);
3028 SIMPLE_ATTR(DLLImport);
3029 SIMPLE_ATTR(Deprecated);
3030 UNSIGNED_ATTR(Destructor);
3031 SIMPLE_ATTR(FastCall);
3032
3033 case Attr::Format: {
3034 std::string Type = ReadString(Record, Idx);
3035 unsigned FormatIdx = Record[Idx++];
3036 unsigned FirstArg = Record[Idx++];
3037 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3038 break;
3039 }
3040
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003041 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003042
3043 case Attr::IBOutletKind:
3044 New = ::new (Context) IBOutletAttr();
3045 break;
3046
3047 SIMPLE_ATTR(NoReturn);
3048 SIMPLE_ATTR(NoThrow);
3049 SIMPLE_ATTR(Nodebug);
3050 SIMPLE_ATTR(Noinline);
3051
3052 case Attr::NonNull: {
3053 unsigned Size = Record[Idx++];
3054 llvm::SmallVector<unsigned, 16> ArgNums;
3055 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3056 Idx += Size;
3057 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3058 break;
3059 }
3060
3061 SIMPLE_ATTR(ObjCException);
3062 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003063 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003064 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003065 SIMPLE_ATTR(Overloadable);
3066 UNSIGNED_ATTR(Packed);
3067 SIMPLE_ATTR(Pure);
3068 UNSIGNED_ATTR(Regparm);
3069 STRING_ATTR(Section);
3070 SIMPLE_ATTR(StdCall);
3071 SIMPLE_ATTR(TransparentUnion);
3072 SIMPLE_ATTR(Unavailable);
3073 SIMPLE_ATTR(Unused);
3074 SIMPLE_ATTR(Used);
3075
3076 case Attr::Visibility:
3077 New = ::new (Context) VisibilityAttr(
3078 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3079 break;
3080
3081 SIMPLE_ATTR(WarnUnusedResult);
3082 SIMPLE_ATTR(Weak);
3083 SIMPLE_ATTR(WeakImport);
3084 }
3085
3086 assert(New && "Unable to decode attribute?");
3087 New->setInherited(IsInherited);
3088 New->setNext(Attrs);
3089 Attrs = New;
3090 }
3091#undef UNSIGNED_ATTR
3092#undef STRING_ATTR
3093#undef SIMPLE_ATTR
3094
3095 // The list of attributes was built backwards. Reverse the list
3096 // before returning it.
3097 Attr *PrevAttr = 0, *NextAttr = 0;
3098 while (Attrs) {
3099 NextAttr = Attrs->getNext();
3100 Attrs->setNext(PrevAttr);
3101 PrevAttr = Attrs;
3102 Attrs = NextAttr;
3103 }
3104
3105 return PrevAttr;
3106}
3107
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003108Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003109 // Within the bitstream, expressions are stored in Reverse Polish
3110 // Notation, with each of the subexpressions preceding the
3111 // expression they are stored in. To evaluate expressions, we
3112 // continue reading expressions and placing them on the stack, with
3113 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003114 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003115 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003116 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003117 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003118 llvm::SmallVector<Stmt *, 16> StmtStack;
3119 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003120 Stmt::EmptyShell Empty;
3121
Douglas Gregora151ba42009-04-14 23:32:43 +00003122 while (true) {
3123 unsigned Code = Stream.ReadCode();
3124 if (Code == llvm::bitc::END_BLOCK) {
3125 if (Stream.ReadBlockEnd()) {
3126 Error("Error at end of Source Manager block");
3127 return 0;
3128 }
3129 break;
3130 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003131
Douglas Gregora151ba42009-04-14 23:32:43 +00003132 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3133 // No known subblocks, always skip them.
3134 Stream.ReadSubBlockID();
3135 if (Stream.SkipBlock()) {
3136 Error("Malformed block record");
3137 return 0;
3138 }
3139 continue;
3140 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003141
Douglas Gregora151ba42009-04-14 23:32:43 +00003142 if (Code == llvm::bitc::DEFINE_ABBREV) {
3143 Stream.ReadAbbrevRecord();
3144 continue;
3145 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003146
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003147 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003148 Idx = 0;
3149 Record.clear();
3150 bool Finished = false;
3151 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003152 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003153 Finished = true;
3154 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003155
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003156 case pch::STMT_NULL_PTR:
3157 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003158 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003159
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003160 case pch::STMT_NULL:
3161 S = new (Context) NullStmt(Empty);
3162 break;
3163
3164 case pch::STMT_COMPOUND:
3165 S = new (Context) CompoundStmt(Empty);
3166 break;
3167
3168 case pch::STMT_CASE:
3169 S = new (Context) CaseStmt(Empty);
3170 break;
3171
3172 case pch::STMT_DEFAULT:
3173 S = new (Context) DefaultStmt(Empty);
3174 break;
3175
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003176 case pch::STMT_LABEL:
3177 S = new (Context) LabelStmt(Empty);
3178 break;
3179
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003180 case pch::STMT_IF:
3181 S = new (Context) IfStmt(Empty);
3182 break;
3183
3184 case pch::STMT_SWITCH:
3185 S = new (Context) SwitchStmt(Empty);
3186 break;
3187
Douglas Gregora6b503f2009-04-17 00:16:09 +00003188 case pch::STMT_WHILE:
3189 S = new (Context) WhileStmt(Empty);
3190 break;
3191
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003192 case pch::STMT_DO:
3193 S = new (Context) DoStmt(Empty);
3194 break;
3195
3196 case pch::STMT_FOR:
3197 S = new (Context) ForStmt(Empty);
3198 break;
3199
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003200 case pch::STMT_GOTO:
3201 S = new (Context) GotoStmt(Empty);
3202 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003203
3204 case pch::STMT_INDIRECT_GOTO:
3205 S = new (Context) IndirectGotoStmt(Empty);
3206 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003207
Douglas Gregora6b503f2009-04-17 00:16:09 +00003208 case pch::STMT_CONTINUE:
3209 S = new (Context) ContinueStmt(Empty);
3210 break;
3211
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003212 case pch::STMT_BREAK:
3213 S = new (Context) BreakStmt(Empty);
3214 break;
3215
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003216 case pch::STMT_RETURN:
3217 S = new (Context) ReturnStmt(Empty);
3218 break;
3219
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003220 case pch::STMT_DECL:
3221 S = new (Context) DeclStmt(Empty);
3222 break;
3223
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003224 case pch::STMT_ASM:
3225 S = new (Context) AsmStmt(Empty);
3226 break;
3227
Douglas Gregora151ba42009-04-14 23:32:43 +00003228 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003229 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003230 break;
3231
3232 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003233 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003234 break;
3235
3236 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003237 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003238 break;
3239
3240 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003241 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003242 break;
3243
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003244 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003245 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003246 break;
3247
Douglas Gregor596e0932009-04-15 16:35:07 +00003248 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003249 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003250 Record[PCHStmtReader::NumExprFields + 1]);
3251 break;
3252
Douglas Gregora151ba42009-04-14 23:32:43 +00003253 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003254 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003255 break;
3256
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003257 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003258 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003259 break;
3260
Douglas Gregor12d74052009-04-15 15:58:59 +00003261 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003262 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003263 break;
3264
3265 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003266 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003267 break;
3268
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003269 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003270 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003271 break;
3272
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003273 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003274 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003275 break;
3276
3277 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003278 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003279 break;
3280
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003281 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003282 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003283 break;
3284
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003285 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003286 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003287 break;
3288
3289 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003290 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003291 break;
3292
Douglas Gregora151ba42009-04-14 23:32:43 +00003293 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003294 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003295 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003296
3297 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003298 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003299 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003300
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003301 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003302 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003303 break;
3304
Douglas Gregorec0b8292009-04-15 23:02:49 +00003305 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003306 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003307 break;
3308
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003309 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003310 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003311 break;
3312
3313 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003314 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003315 Record[PCHStmtReader::NumExprFields] - 1);
3316
3317 break;
3318
3319 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003320 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003321 break;
3322
Douglas Gregorec0b8292009-04-15 23:02:49 +00003323 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003324 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003325 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003326
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003327 case pch::EXPR_ADDR_LABEL:
3328 S = new (Context) AddrLabelExpr(Empty);
3329 break;
3330
Douglas Gregoreca12f62009-04-17 19:05:30 +00003331 case pch::EXPR_STMT:
3332 S = new (Context) StmtExpr(Empty);
3333 break;
3334
Douglas Gregor209d4622009-04-15 23:33:31 +00003335 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003336 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003337 break;
3338
3339 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003340 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003341 break;
3342
3343 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003344 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003345 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003346
3347 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003348 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003349 break;
3350
Douglas Gregore246b742009-04-17 19:21:43 +00003351 case pch::EXPR_BLOCK:
3352 S = new (Context) BlockExpr(Empty);
3353 break;
3354
Douglas Gregor725e94b2009-04-16 00:01:45 +00003355 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003356 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003357 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003358
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003359 case pch::EXPR_OBJC_STRING_LITERAL:
3360 S = new (Context) ObjCStringLiteral(Empty);
3361 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003362 case pch::EXPR_OBJC_ENCODE:
3363 S = new (Context) ObjCEncodeExpr(Empty);
3364 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003365 case pch::EXPR_OBJC_SELECTOR_EXPR:
3366 S = new (Context) ObjCSelectorExpr(Empty);
3367 break;
3368 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3369 S = new (Context) ObjCProtocolExpr(Empty);
3370 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003371 }
3372
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003373 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003374 if (Finished)
3375 break;
3376
Douglas Gregor456e0952009-04-17 22:13:46 +00003377 ++NumStatementsRead;
3378
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003379 if (S) {
3380 unsigned NumSubStmts = Reader.Visit(S);
3381 while (NumSubStmts > 0) {
3382 StmtStack.pop_back();
3383 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003384 }
3385 }
3386
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003387 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003388 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003389 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003390 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003391 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003392 return StmtStack.back();
3393}
3394
3395Expr *PCHReader::ReadExpr() {
3396 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003397}
3398
Douglas Gregor179cfb12009-04-10 20:39:37 +00003399DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003400 return Diag(SourceLocation(), DiagID);
3401}
3402
3403DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3404 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003405 Context.getSourceManager()),
3406 DiagID);
3407}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003408
Douglas Gregorc713da92009-04-21 22:25:48 +00003409/// \brief Retrieve the identifier table associated with the
3410/// preprocessor.
3411IdentifierTable &PCHReader::getIdentifierTable() {
3412 return PP.getIdentifierTable();
3413}
3414
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003415/// \brief Record that the given ID maps to the given switch-case
3416/// statement.
3417void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3418 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3419 SwitchCaseStmts[ID] = SC;
3420}
3421
3422/// \brief Retrieve the switch-case statement with the given ID.
3423SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3424 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3425 return SwitchCaseStmts[ID];
3426}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003427
3428/// \brief Record that the given label statement has been
3429/// deserialized and has the given ID.
3430void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3431 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3432 "Deserialized label twice");
3433 LabelStmts[ID] = S;
3434
3435 // If we've already seen any goto statements that point to this
3436 // label, resolve them now.
3437 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3438 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3439 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3440 Goto->second->setLabel(S);
3441 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003442
3443 // If we've already seen any address-label statements that point to
3444 // this label, resolve them now.
3445 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3446 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3447 = UnresolvedAddrLabelExprs.equal_range(ID);
3448 for (AddrLabelIter AddrLabel = AddrLabels.first;
3449 AddrLabel != AddrLabels.second; ++AddrLabel)
3450 AddrLabel->second->setLabel(S);
3451 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003452}
3453
3454/// \brief Set the label of the given statement to the label
3455/// identified by ID.
3456///
3457/// Depending on the order in which the label and other statements
3458/// referencing that label occur, this operation may complete
3459/// immediately (updating the statement) or it may queue the
3460/// statement to be back-patched later.
3461void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3462 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3463 if (Label != LabelStmts.end()) {
3464 // We've already seen this label, so set the label of the goto and
3465 // we're done.
3466 S->setLabel(Label->second);
3467 } else {
3468 // We haven't seen this label yet, so add this goto to the set of
3469 // unresolved goto statements.
3470 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3471 }
3472}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003473
3474/// \brief Set the label of the given expression to the label
3475/// identified by ID.
3476///
3477/// Depending on the order in which the label and other statements
3478/// referencing that label occur, this operation may complete
3479/// immediately (updating the statement) or it may queue the
3480/// statement to be back-patched later.
3481void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3482 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3483 if (Label != LabelStmts.end()) {
3484 // We've already seen this label, so set the label of the
3485 // label-address expression and we're done.
3486 S->setLabel(Label->second);
3487 } else {
3488 // We haven't seen this label yet, so add this label-address
3489 // expression to the set of unresolved label-address expressions.
3490 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3491 }
3492}