blob: c2d50ce9fc3fb8c0bc73f9d8629b244139ceade6 [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);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000500 unsigned VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
501 unsigned VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
502 unsigned VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000503 unsigned VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000504 unsigned VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000505
506 unsigned VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
507 unsigned VisitObjCCatchStmt(ObjCAtCatchStmt *);
508 unsigned VisitObjCFinallyStmt(ObjCAtFinallyStmt *);
509 unsigned VisitObjCAtTryStmt(ObjCAtTryStmt *);
510 unsigned VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
511 unsigned VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000512 };
513}
514
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000515unsigned PCHStmtReader::VisitStmt(Stmt *S) {
516 assert(Idx == NumStmtFields && "Incorrect statement field count");
517 return 0;
518}
519
520unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
521 VisitStmt(S);
522 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
523 return 0;
524}
525
526unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
527 VisitStmt(S);
528 unsigned NumStmts = Record[Idx++];
529 S->setStmts(Reader.getContext(),
530 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
531 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
532 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
533 return NumStmts;
534}
535
536unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
537 VisitStmt(S);
538 Reader.RecordSwitchCaseID(S, Record[Idx++]);
539 return 0;
540}
541
542unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
543 VisitSwitchCase(S);
544 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
545 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
546 S->setSubStmt(StmtStack.back());
547 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 return 3;
549}
550
551unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
552 VisitSwitchCase(S);
553 S->setSubStmt(StmtStack.back());
554 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 return 1;
556}
557
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000558unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
559 VisitStmt(S);
560 S->setID(Reader.GetIdentifierInfo(Record, Idx));
561 S->setSubStmt(StmtStack.back());
562 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
563 Reader.RecordLabelStmt(S, Record[Idx++]);
564 return 1;
565}
566
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000567unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
568 VisitStmt(S);
569 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
570 S->setThen(StmtStack[StmtStack.size() - 2]);
571 S->setElse(StmtStack[StmtStack.size() - 1]);
572 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
573 return 3;
574}
575
576unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
577 VisitStmt(S);
578 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
579 S->setBody(StmtStack.back());
580 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 SwitchCase *PrevSC = 0;
582 for (unsigned N = Record.size(); Idx != N; ++Idx) {
583 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
584 if (PrevSC)
585 PrevSC->setNextSwitchCase(SC);
586 else
587 S->setSwitchCaseList(SC);
588 PrevSC = SC;
589 }
590 return 2;
591}
592
Douglas Gregora6b503f2009-04-17 00:16:09 +0000593unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
594 VisitStmt(S);
595 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
596 S->setBody(StmtStack.back());
597 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 return 2;
599}
600
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000601unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
602 VisitStmt(S);
603 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
604 S->setBody(StmtStack.back());
605 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
606 return 2;
607}
608
609unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
610 VisitStmt(S);
611 S->setInit(StmtStack[StmtStack.size() - 4]);
612 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
613 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
614 S->setBody(StmtStack.back());
615 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616 return 4;
617}
618
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000619unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
620 VisitStmt(S);
621 Reader.SetLabelOf(S, Record[Idx++]);
622 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
624 return 0;
625}
626
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000627unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
628 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000629 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000630 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
631 return 1;
632}
633
Douglas Gregora6b503f2009-04-17 00:16:09 +0000634unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
635 VisitStmt(S);
636 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 0;
638}
639
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000640unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
641 VisitStmt(S);
642 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643 return 0;
644}
645
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000646unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
647 VisitStmt(S);
648 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
649 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
650 return 1;
651}
652
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000653unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
654 VisitStmt(S);
655 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
656 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
657
658 if (Idx + 1 == Record.size()) {
659 // Single declaration
660 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
661 } else {
662 llvm::SmallVector<Decl *, 16> Decls;
663 Decls.reserve(Record.size() - Idx);
664 for (unsigned N = Record.size(); Idx != N; ++Idx)
665 Decls.push_back(Reader.GetDecl(Record[Idx]));
666 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
667 &Decls[0], Decls.size())));
668 }
669 return 0;
670}
671
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000672unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
673 VisitStmt(S);
674 unsigned NumOutputs = Record[Idx++];
675 unsigned NumInputs = Record[Idx++];
676 unsigned NumClobbers = Record[Idx++];
677 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
678 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
679 S->setVolatile(Record[Idx++]);
680 S->setSimple(Record[Idx++]);
681
682 unsigned StackIdx
683 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
684 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
685
686 // Outputs and inputs
687 llvm::SmallVector<std::string, 16> Names;
688 llvm::SmallVector<StringLiteral*, 16> Constraints;
689 llvm::SmallVector<Stmt*, 16> Exprs;
690 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
691 Names.push_back(Reader.ReadString(Record, Idx));
692 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
693 Exprs.push_back(StmtStack[StackIdx++]);
694 }
695 S->setOutputsAndInputs(NumOutputs, NumInputs,
696 &Names[0], &Constraints[0], &Exprs[0]);
697
698 // Constraints
699 llvm::SmallVector<StringLiteral*, 16> Clobbers;
700 for (unsigned I = 0; I != NumClobbers; ++I)
701 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
702 S->setClobbers(&Clobbers[0], NumClobbers);
703
704 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
705 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
706}
707
Douglas Gregora151ba42009-04-14 23:32:43 +0000708unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000709 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000710 E->setType(Reader.GetType(Record[Idx++]));
711 E->setTypeDependent(Record[Idx++]);
712 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000713 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000714 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000715}
716
Douglas Gregora151ba42009-04-14 23:32:43 +0000717unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000718 VisitExpr(E);
719 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
720 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000721 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000722}
723
Douglas Gregora151ba42009-04-14 23:32:43 +0000724unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000725 VisitExpr(E);
726 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
727 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000728 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000729}
730
Douglas Gregora151ba42009-04-14 23:32:43 +0000731unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000732 VisitExpr(E);
733 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
734 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000735 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000736}
737
Douglas Gregora151ba42009-04-14 23:32:43 +0000738unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000739 VisitExpr(E);
740 E->setValue(Reader.ReadAPFloat(Record, Idx));
741 E->setExact(Record[Idx++]);
742 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000743 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000744}
745
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000746unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
747 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000748 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000749 return 1;
750}
751
Douglas Gregor596e0932009-04-15 16:35:07 +0000752unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
753 VisitExpr(E);
754 unsigned Len = Record[Idx++];
755 assert(Record[Idx] == E->getNumConcatenated() &&
756 "Wrong number of concatenated tokens!");
757 ++Idx;
758 E->setWide(Record[Idx++]);
759
760 // Read string data
761 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
762 E->setStrData(Reader.getContext(), &Str[0], Len);
763 Idx += Len;
764
765 // Read source locations
766 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
767 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
768
769 return 0;
770}
771
Douglas Gregora151ba42009-04-14 23:32:43 +0000772unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000773 VisitExpr(E);
774 E->setValue(Record[Idx++]);
775 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
776 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000777 return 0;
778}
779
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000780unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
781 VisitExpr(E);
782 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
783 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000784 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000785 return 1;
786}
787
Douglas Gregor12d74052009-04-15 15:58:59 +0000788unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
789 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000790 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000791 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
792 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 return 1;
794}
795
796unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
797 VisitExpr(E);
798 E->setSizeof(Record[Idx++]);
799 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000801 ++Idx;
802 } else {
803 E->setArgument(Reader.GetType(Record[Idx++]));
804 }
805 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
807 return E->isArgumentType()? 0 : 1;
808}
809
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000810unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
811 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000812 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Steve Naroff315ec172009-04-25 15:19:54 +0000813 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000814 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
815 return 2;
816}
817
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000818unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
819 VisitExpr(E);
820 E->setNumArgs(Reader.getContext(), Record[Idx++]);
821 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000822 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000823 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000824 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000825 return E->getNumArgs() + 1;
826}
827
828unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
829 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000830 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000831 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
832 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
833 E->setArrow(Record[Idx++]);
834 return 1;
835}
836
Douglas Gregora151ba42009-04-14 23:32:43 +0000837unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
838 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000839 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000840 return 1;
841}
842
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000843unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
844 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000845 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
846 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000847 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
848 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
849 return 2;
850}
851
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000852unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
853 VisitBinaryOperator(E);
854 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
855 E->setComputationResultType(Reader.GetType(Record[Idx++]));
856 return 2;
857}
858
859unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
860 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000861 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
862 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
863 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000864 return 3;
865}
866
Douglas Gregora151ba42009-04-14 23:32:43 +0000867unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
868 VisitCastExpr(E);
869 E->setLvalueCast(Record[Idx++]);
870 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000871}
872
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000873unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
874 VisitCastExpr(E);
875 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
876 return 1;
877}
878
879unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
880 VisitExplicitCastExpr(E);
881 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
882 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
883 return 1;
884}
885
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000886unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
887 VisitExpr(E);
888 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000889 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000890 E->setFileScope(Record[Idx++]);
891 return 1;
892}
893
Douglas Gregorec0b8292009-04-15 23:02:49 +0000894unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
895 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000896 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000897 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
898 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 return 1;
900}
901
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000902unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
903 VisitExpr(E);
904 unsigned NumInits = Record[Idx++];
905 E->reserveInits(NumInits);
906 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000907 E->updateInit(I,
908 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
909 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000910 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
911 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
912 E->setInitializedFieldInUnion(
913 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
914 E->sawArrayRangeDesignator(Record[Idx++]);
915 return NumInits + 1;
916}
917
918unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
919 typedef DesignatedInitExpr::Designator Designator;
920
921 VisitExpr(E);
922 unsigned NumSubExprs = Record[Idx++];
923 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
924 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000925 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000926 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
927 E->setGNUSyntax(Record[Idx++]);
928
929 llvm::SmallVector<Designator, 4> Designators;
930 while (Idx < Record.size()) {
931 switch ((pch::DesignatorTypes)Record[Idx++]) {
932 case pch::DESIG_FIELD_DECL: {
933 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
934 SourceLocation DotLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 SourceLocation FieldLoc
937 = SourceLocation::getFromRawEncoding(Record[Idx++]);
938 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
939 FieldLoc));
940 Designators.back().setField(Field);
941 break;
942 }
943
944 case pch::DESIG_FIELD_NAME: {
945 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
946 SourceLocation DotLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 SourceLocation FieldLoc
949 = SourceLocation::getFromRawEncoding(Record[Idx++]);
950 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
951 break;
952 }
953
954 case pch::DESIG_ARRAY: {
955 unsigned Index = Record[Idx++];
956 SourceLocation LBracketLoc
957 = SourceLocation::getFromRawEncoding(Record[Idx++]);
958 SourceLocation RBracketLoc
959 = SourceLocation::getFromRawEncoding(Record[Idx++]);
960 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
961 break;
962 }
963
964 case pch::DESIG_ARRAY_RANGE: {
965 unsigned Index = Record[Idx++];
966 SourceLocation LBracketLoc
967 = SourceLocation::getFromRawEncoding(Record[Idx++]);
968 SourceLocation EllipsisLoc
969 = SourceLocation::getFromRawEncoding(Record[Idx++]);
970 SourceLocation RBracketLoc
971 = SourceLocation::getFromRawEncoding(Record[Idx++]);
972 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
973 RBracketLoc));
974 break;
975 }
976 }
977 }
978 E->setDesignators(&Designators[0], Designators.size());
979
980 return NumSubExprs;
981}
982
983unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
984 VisitExpr(E);
985 return 0;
986}
987
Douglas Gregorec0b8292009-04-15 23:02:49 +0000988unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
989 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000990 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000991 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 return 1;
994}
995
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000996unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
997 VisitExpr(E);
998 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
999 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1000 Reader.SetLabelOf(E, Record[Idx++]);
1001 return 0;
1002}
1003
Douglas Gregoreca12f62009-04-17 19:05:30 +00001004unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
1005 VisitExpr(E);
1006 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1007 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1008 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
1009 return 1;
1010}
1011
Douglas Gregor209d4622009-04-15 23:33:31 +00001012unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1013 VisitExpr(E);
1014 E->setArgType1(Reader.GetType(Record[Idx++]));
1015 E->setArgType2(Reader.GetType(Record[Idx++]));
1016 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1017 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1018 return 0;
1019}
1020
1021unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1022 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001023 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1024 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1025 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001026 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1028 return 3;
1029}
1030
1031unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1032 VisitExpr(E);
1033 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1034 return 0;
1035}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001036
Douglas Gregor725e94b2009-04-16 00:01:45 +00001037unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1038 VisitExpr(E);
1039 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001040 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001041 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1042 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1043 return NumExprs;
1044}
1045
Douglas Gregore246b742009-04-17 19:21:43 +00001046unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1047 VisitExpr(E);
1048 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1049 E->setHasBlockDeclRefExprs(Record[Idx++]);
1050 return 0;
1051}
1052
Douglas Gregor725e94b2009-04-16 00:01:45 +00001053unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1054 VisitExpr(E);
1055 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1056 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1057 E->setByRef(Record[Idx++]);
1058 return 0;
1059}
1060
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001061//===----------------------------------------------------------------------===//
1062// Objective-C Expressions and Statements
1063
1064unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1065 VisitExpr(E);
1066 E->setString(cast<StringLiteral>(StmtStack.back()));
1067 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 return 1;
1069}
1070
Chris Lattner80f83c62009-04-22 05:57:30 +00001071unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1072 VisitExpr(E);
1073 E->setEncodedType(Reader.GetType(Record[Idx++]));
1074 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1075 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1076 return 0;
1077}
1078
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001079unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1080 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001081 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001082 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1083 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1084 return 0;
1085}
1086
1087unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1088 VisitExpr(E);
1089 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1090 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1091 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1092 return 0;
1093}
1094
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001095unsigned PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1096 VisitExpr(E);
1097 E->setDecl(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
1098 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1099 E->setBase(cast<Expr>(StmtStack.back()));
1100 E->setIsArrow(Record[Idx++]);
1101 E->setIsFreeIvar(Record[Idx++]);
1102 return 1;
1103}
1104
1105unsigned PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1106 VisitExpr(E);
1107 E->setProperty(cast<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
1108 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1109 E->setBase(cast<Expr>(StmtStack.back()));
1110 return 1;
1111}
1112
1113unsigned PCHStmtReader::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1114 VisitExpr(E);
1115 E->setGetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1116 E->setSetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1117 E->setClassProp(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
1118 E->setBase(cast<Expr>(StmtStack.back()));
1119 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1120 E->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1121 return 1;
1122}
1123
Steve Narofffb3e4022009-04-25 14:04:28 +00001124unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1125 VisitExpr(E);
1126 E->setNumArgs(Record[Idx++]);
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001127 E->setLeftLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1128 E->setRightLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Narofffb3e4022009-04-25 14:04:28 +00001129 E->setSelector(Reader.GetSelector(Record, Idx));
1130 E->setMethodDecl(cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001131
1132 ObjCMessageExpr::ClassInfo CI;
1133 CI.first = cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]));
1134 CI.second = Reader.GetIdentifierInfo(Record, Idx);
1135 if (E->getMethodDecl() == 0)
1136 E->setClassInfo(CI);
1137
Steve Narofffb3e4022009-04-25 14:04:28 +00001138 E->setReceiver(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
1139 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1140 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
1141 return E->getNumArgs() + 1;
1142}
1143
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001144unsigned PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1145 VisitExpr(E);
1146 E->setLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1147 return 0;
1148}
Chris Lattner80f83c62009-04-22 05:57:30 +00001149
Steve Naroff79762bd2009-04-26 18:52:16 +00001150unsigned PCHStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1151 VisitStmt(S);
1152 S->setElement(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1153 S->setCollection(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1154 S->setBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1155 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1156 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1157 return 3;
1158}
1159
1160unsigned PCHStmtReader::VisitObjCCatchStmt(ObjCAtCatchStmt *S) {
1161 VisitStmt(S);
1162 S->setCatchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1163 S->setNextCatchStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1164 S->setCatchParamDecl(cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
1165 S->setAtCatchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1166 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1167 return 2;
1168}
1169
1170unsigned PCHStmtReader::VisitObjCFinallyStmt(ObjCAtFinallyStmt *S) {
1171 VisitStmt(S);
1172 S->setFinallyBody(StmtStack.back());
1173 S->setAtFinallyLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1174 return 1;
1175}
1176
1177unsigned PCHStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1178 VisitStmt(S);
1179 S->setTryBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1180 S->setCatchStmts(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1181 S->setFinallyStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1182 S->setAtTryLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1183 return 3;
1184}
1185
1186unsigned PCHStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1187 VisitStmt(S);
1188 S->setSynchExpr(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1189 S->setSynchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1190 S->setAtSynchronizedLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1191 return 2;
1192}
1193
1194unsigned PCHStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1195 VisitStmt(S);
1196 S->setThrowExpr(StmtStack.back());
1197 S->setThrowLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1198 return 1;
1199}
1200
Douglas Gregorc713da92009-04-21 22:25:48 +00001201//===----------------------------------------------------------------------===//
1202// PCH reader implementation
1203//===----------------------------------------------------------------------===//
1204
1205namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001206class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1207 PCHReader &Reader;
1208
1209public:
1210 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1211
1212 typedef Selector external_key_type;
1213 typedef external_key_type internal_key_type;
1214
1215 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1216
1217 static bool EqualKey(const internal_key_type& a,
1218 const internal_key_type& b) {
1219 return a == b;
1220 }
1221
1222 static unsigned ComputeHash(Selector Sel) {
1223 unsigned N = Sel.getNumArgs();
1224 if (N == 0)
1225 ++N;
1226 unsigned R = 5381;
1227 for (unsigned I = 0; I != N; ++I)
1228 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1229 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1230 return R;
1231 }
1232
1233 // This hopefully will just get inlined and removed by the optimizer.
1234 static const internal_key_type&
1235 GetInternalKey(const external_key_type& x) { return x; }
1236
1237 static std::pair<unsigned, unsigned>
1238 ReadKeyDataLength(const unsigned char*& d) {
1239 using namespace clang::io;
1240 unsigned KeyLen = ReadUnalignedLE16(d);
1241 unsigned DataLen = ReadUnalignedLE16(d);
1242 return std::make_pair(KeyLen, DataLen);
1243 }
1244
Douglas Gregor2d711832009-04-25 17:48:32 +00001245 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001246 using namespace clang::io;
1247 SelectorTable &SelTable = Reader.getContext().Selectors;
1248 unsigned N = ReadUnalignedLE16(d);
1249 IdentifierInfo *FirstII
1250 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1251 if (N == 0)
1252 return SelTable.getNullarySelector(FirstII);
1253 else if (N == 1)
1254 return SelTable.getUnarySelector(FirstII);
1255
1256 llvm::SmallVector<IdentifierInfo *, 16> Args;
1257 Args.push_back(FirstII);
1258 for (unsigned I = 1; I != N; ++I)
1259 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1260
1261 return SelTable.getSelector(N, &Args[0]);
1262 }
1263
1264 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1265 using namespace clang::io;
1266 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1267 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1268
1269 data_type Result;
1270
1271 // Load instance methods
1272 ObjCMethodList *Prev = 0;
1273 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1274 ObjCMethodDecl *Method
1275 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1276 if (!Result.first.Method) {
1277 // This is the first method, which is the easy case.
1278 Result.first.Method = Method;
1279 Prev = &Result.first;
1280 continue;
1281 }
1282
1283 Prev->Next = new ObjCMethodList(Method, 0);
1284 Prev = Prev->Next;
1285 }
1286
1287 // Load factory methods
1288 Prev = 0;
1289 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1290 ObjCMethodDecl *Method
1291 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1292 if (!Result.second.Method) {
1293 // This is the first method, which is the easy case.
1294 Result.second.Method = Method;
1295 Prev = &Result.second;
1296 continue;
1297 }
1298
1299 Prev->Next = new ObjCMethodList(Method, 0);
1300 Prev = Prev->Next;
1301 }
1302
1303 return Result;
1304 }
1305};
1306
1307} // end anonymous namespace
1308
1309/// \brief The on-disk hash table used for the global method pool.
1310typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1311 PCHMethodPoolLookupTable;
1312
1313namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001314class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1315 PCHReader &Reader;
1316
1317 // If we know the IdentifierInfo in advance, it is here and we will
1318 // not build a new one. Used when deserializing information about an
1319 // identifier that was constructed before the PCH file was read.
1320 IdentifierInfo *KnownII;
1321
1322public:
1323 typedef IdentifierInfo * data_type;
1324
1325 typedef const std::pair<const char*, unsigned> external_key_type;
1326
1327 typedef external_key_type internal_key_type;
1328
1329 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1330 : Reader(Reader), KnownII(II) { }
1331
1332 static bool EqualKey(const internal_key_type& a,
1333 const internal_key_type& b) {
1334 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1335 : false;
1336 }
1337
1338 static unsigned ComputeHash(const internal_key_type& a) {
1339 return BernsteinHash(a.first, a.second);
1340 }
1341
1342 // This hopefully will just get inlined and removed by the optimizer.
1343 static const internal_key_type&
1344 GetInternalKey(const external_key_type& x) { return x; }
1345
1346 static std::pair<unsigned, unsigned>
1347 ReadKeyDataLength(const unsigned char*& d) {
1348 using namespace clang::io;
Douglas Gregor4bb24882009-04-25 20:26:24 +00001349 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001350 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregorc713da92009-04-21 22:25:48 +00001351 return std::make_pair(KeyLen, DataLen);
1352 }
1353
1354 static std::pair<const char*, unsigned>
1355 ReadKey(const unsigned char* d, unsigned n) {
1356 assert(n >= 2 && d[n-1] == '\0');
1357 return std::make_pair((const char*) d, n-1);
1358 }
1359
1360 IdentifierInfo *ReadData(const internal_key_type& k,
1361 const unsigned char* d,
1362 unsigned DataLen) {
1363 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001364 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001365 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1366 Bits >>= 1;
1367 bool Poisoned = Bits & 0x01;
1368 Bits >>= 1;
1369 bool ExtensionToken = Bits & 0x01;
1370 Bits >>= 1;
1371 bool hasMacroDefinition = Bits & 0x01;
1372 Bits >>= 1;
1373 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1374 Bits >>= 10;
1375 unsigned TokenID = Bits & 0xFF;
1376 Bits >>= 8;
1377
Douglas Gregorc713da92009-04-21 22:25:48 +00001378 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001379 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001380 DataLen -= 8;
1381
1382 // Build the IdentifierInfo itself and link the identifier ID with
1383 // the new IdentifierInfo.
1384 IdentifierInfo *II = KnownII;
1385 if (!II)
Douglas Gregor4bb24882009-04-25 20:26:24 +00001386 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1387 k.first, k.first + k.second);
Douglas Gregorc713da92009-04-21 22:25:48 +00001388 Reader.SetIdentifierInfo(ID, II);
1389
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001390 // Set or check the various bits in the IdentifierInfo structure.
1391 // FIXME: Load token IDs lazily, too?
1392 assert((unsigned)II->getTokenID() == TokenID &&
1393 "Incorrect token ID loaded");
1394 (void)TokenID;
1395 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1396 assert(II->isExtensionToken() == ExtensionToken &&
1397 "Incorrect extension token flag");
1398 (void)ExtensionToken;
1399 II->setIsPoisoned(Poisoned);
1400 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1401 "Incorrect C++ operator keyword flag");
1402 (void)CPlusPlusOperatorKeyword;
1403
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001404 // If this identifier is a macro, deserialize the macro
1405 // definition.
1406 if (hasMacroDefinition) {
1407 uint32_t Offset = ReadUnalignedLE64(d);
1408 Reader.ReadMacroRecord(Offset);
1409 DataLen -= 8;
1410 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001411
1412 // Read all of the declarations visible at global scope with this
1413 // name.
1414 Sema *SemaObj = Reader.getSema();
1415 while (DataLen > 0) {
1416 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001417 if (SemaObj) {
1418 // Introduce this declaration into the translation-unit scope
1419 // and add it to the declaration chain for this identifier, so
1420 // that (unqualified) name lookup will find it.
1421 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1422 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1423 } else {
1424 // Queue this declaration so that it will be added to the
1425 // translation unit scope and identifier's declaration chain
1426 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001427 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001428 }
1429
1430 DataLen -= 4;
1431 }
1432 return II;
1433 }
1434};
1435
1436} // end anonymous namespace
1437
1438/// \brief The on-disk hash table used to contain information about
1439/// all of the identifiers in the program.
1440typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1441 PCHIdentifierLookupTable;
1442
Douglas Gregorc34897d2009-04-09 22:27:44 +00001443// FIXME: use the diagnostics machinery
1444static bool Error(const char *Str) {
1445 std::fprintf(stderr, "%s\n", Str);
1446 return true;
1447}
1448
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001449/// \brief Check the contents of the predefines buffer against the
1450/// contents of the predefines buffer used to build the PCH file.
1451///
1452/// The contents of the two predefines buffers should be the same. If
1453/// not, then some command-line option changed the preprocessor state
1454/// and we must reject the PCH file.
1455///
1456/// \param PCHPredef The start of the predefines buffer in the PCH
1457/// file.
1458///
1459/// \param PCHPredefLen The length of the predefines buffer in the PCH
1460/// file.
1461///
1462/// \param PCHBufferID The FileID for the PCH predefines buffer.
1463///
1464/// \returns true if there was a mismatch (in which case the PCH file
1465/// should be ignored), or false otherwise.
1466bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1467 unsigned PCHPredefLen,
1468 FileID PCHBufferID) {
1469 const char *Predef = PP.getPredefines().c_str();
1470 unsigned PredefLen = PP.getPredefines().size();
1471
1472 // If the two predefines buffers compare equal, we're done!.
1473 if (PredefLen == PCHPredefLen &&
1474 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1475 return false;
1476
1477 // The predefines buffers are different. Produce a reasonable
1478 // diagnostic showing where they are different.
1479
1480 // The source locations (potentially in the two different predefines
1481 // buffers)
1482 SourceLocation Loc1, Loc2;
1483 SourceManager &SourceMgr = PP.getSourceManager();
1484
1485 // Create a source buffer for our predefines string, so
1486 // that we can build a diagnostic that points into that
1487 // source buffer.
1488 FileID BufferID;
1489 if (Predef && Predef[0]) {
1490 llvm::MemoryBuffer *Buffer
1491 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1492 "<built-in>");
1493 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1494 }
1495
1496 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1497 std::pair<const char *, const char *> Locations
1498 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1499
1500 if (Locations.first != Predef + MinLen) {
1501 // We found the location in the two buffers where there is a
1502 // difference. Form source locations to point there (in both
1503 // buffers).
1504 unsigned Offset = Locations.first - Predef;
1505 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1506 .getFileLocWithOffset(Offset);
1507 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1508 .getFileLocWithOffset(Offset);
1509 } else if (PredefLen > PCHPredefLen) {
1510 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1511 .getFileLocWithOffset(MinLen);
1512 } else {
1513 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1514 .getFileLocWithOffset(MinLen);
1515 }
1516
1517 Diag(Loc1, diag::warn_pch_preprocessor);
1518 if (Loc2.isValid())
1519 Diag(Loc2, diag::note_predef_in_pch);
1520 Diag(diag::note_ignoring_pch) << FileName;
1521 return true;
1522}
1523
Douglas Gregor635f97f2009-04-13 16:31:14 +00001524/// \brief Read the line table in the source manager block.
1525/// \returns true if ther was an error.
1526static bool ParseLineTable(SourceManager &SourceMgr,
1527 llvm::SmallVectorImpl<uint64_t> &Record) {
1528 unsigned Idx = 0;
1529 LineTableInfo &LineTable = SourceMgr.getLineTable();
1530
1531 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001532 std::map<int, int> FileIDs;
1533 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001534 // Extract the file name
1535 unsigned FilenameLen = Record[Idx++];
1536 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1537 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001538 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1539 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001540 }
1541
1542 // Parse the line entries
1543 std::vector<LineEntry> Entries;
1544 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001545 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001546
1547 // Extract the line entries
1548 unsigned NumEntries = Record[Idx++];
1549 Entries.clear();
1550 Entries.reserve(NumEntries);
1551 for (unsigned I = 0; I != NumEntries; ++I) {
1552 unsigned FileOffset = Record[Idx++];
1553 unsigned LineNo = Record[Idx++];
1554 int FilenameID = Record[Idx++];
1555 SrcMgr::CharacteristicKind FileKind
1556 = (SrcMgr::CharacteristicKind)Record[Idx++];
1557 unsigned IncludeOffset = Record[Idx++];
1558 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1559 FileKind, IncludeOffset));
1560 }
1561 LineTable.AddEntry(FID, Entries);
1562 }
1563
1564 return false;
1565}
1566
Douglas Gregorab1cef72009-04-10 03:52:48 +00001567/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001568PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001569 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001570 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1571 Error("Malformed source manager block record");
1572 return Failure;
1573 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001574
1575 SourceManager &SourceMgr = Context.getSourceManager();
1576 RecordData Record;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001577 unsigned NumHeaderInfos = 0;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001578 while (true) {
1579 unsigned Code = Stream.ReadCode();
1580 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001581 if (Stream.ReadBlockEnd()) {
1582 Error("Error at end of Source Manager block");
1583 return Failure;
1584 }
1585
1586 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001587 }
1588
1589 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1590 // No known subblocks, always skip them.
1591 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001592 if (Stream.SkipBlock()) {
1593 Error("Malformed block record");
1594 return Failure;
1595 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001596 continue;
1597 }
1598
1599 if (Code == llvm::bitc::DEFINE_ABBREV) {
1600 Stream.ReadAbbrevRecord();
1601 continue;
1602 }
1603
1604 // Read a record.
1605 const char *BlobStart;
1606 unsigned BlobLen;
1607 Record.clear();
1608 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1609 default: // Default behavior: ignore.
1610 break;
1611
1612 case pch::SM_SLOC_FILE_ENTRY: {
1613 // FIXME: We would really like to delay the creation of this
1614 // FileEntry until it is actually required, e.g., when producing
1615 // a diagnostic with a source location in this file.
1616 const FileEntry *File
1617 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1618 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001619 FileID ID = SourceMgr.createFileID(File,
1620 SourceLocation::getFromRawEncoding(Record[1]),
1621 (CharacteristicKind)Record[2]);
1622 if (Record[3])
1623 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1624 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001625 break;
1626 }
1627
1628 case pch::SM_SLOC_BUFFER_ENTRY: {
1629 const char *Name = BlobStart;
1630 unsigned Code = Stream.ReadCode();
1631 Record.clear();
1632 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1633 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001634 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001635 llvm::MemoryBuffer *Buffer
1636 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1637 BlobStart + BlobLen - 1,
1638 Name);
1639 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1640
1641 if (strcmp(Name, "<built-in>") == 0
1642 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1643 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001644 break;
1645 }
1646
1647 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1648 SourceLocation SpellingLoc
1649 = SourceLocation::getFromRawEncoding(Record[1]);
1650 SourceMgr.createInstantiationLoc(
1651 SpellingLoc,
1652 SourceLocation::getFromRawEncoding(Record[2]),
1653 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001654 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001655 break;
1656 }
1657
Chris Lattnere1be6022009-04-14 23:22:57 +00001658 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001659 if (ParseLineTable(SourceMgr, Record))
1660 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001661 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001662
1663 case pch::SM_HEADER_FILE_INFO: {
1664 HeaderFileInfo HFI;
1665 HFI.isImport = Record[0];
1666 HFI.DirInfo = Record[1];
1667 HFI.NumIncludes = Record[2];
1668 HFI.ControllingMacroID = Record[3];
1669 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1670 break;
1671 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001672 }
1673 }
1674}
1675
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001676void PCHReader::ReadMacroRecord(uint64_t Offset) {
1677 // Keep track of where we are in the stream, then jump back there
1678 // after reading this macro.
1679 SavedStreamPosition SavedPosition(Stream);
1680
1681 Stream.JumpToBit(Offset);
1682 RecordData Record;
1683 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1684 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001685
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001686 while (true) {
1687 unsigned Code = Stream.ReadCode();
1688 switch (Code) {
1689 case llvm::bitc::END_BLOCK:
1690 return;
1691
1692 case llvm::bitc::ENTER_SUBBLOCK:
1693 // No known subblocks, always skip them.
1694 Stream.ReadSubBlockID();
1695 if (Stream.SkipBlock()) {
1696 Error("Malformed block record");
1697 return;
1698 }
1699 continue;
1700
1701 case llvm::bitc::DEFINE_ABBREV:
1702 Stream.ReadAbbrevRecord();
1703 continue;
1704 default: break;
1705 }
1706
1707 // Read a record.
1708 Record.clear();
1709 pch::PreprocessorRecordTypes RecType =
1710 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1711 switch (RecType) {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001712 case pch::PP_MACRO_OBJECT_LIKE:
1713 case pch::PP_MACRO_FUNCTION_LIKE: {
1714 // If we already have a macro, that means that we've hit the end
1715 // of the definition of the macro we were looking for. We're
1716 // done.
1717 if (Macro)
1718 return;
1719
1720 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1721 if (II == 0) {
1722 Error("Macro must have a name");
1723 return;
1724 }
1725 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1726 bool isUsed = Record[2];
1727
1728 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1729 MI->setIsUsed(isUsed);
1730
1731 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1732 // Decode function-like macro info.
1733 bool isC99VarArgs = Record[3];
1734 bool isGNUVarArgs = Record[4];
1735 MacroArgs.clear();
1736 unsigned NumArgs = Record[5];
1737 for (unsigned i = 0; i != NumArgs; ++i)
1738 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1739
1740 // Install function-like macro info.
1741 MI->setIsFunctionLike();
1742 if (isC99VarArgs) MI->setIsC99Varargs();
1743 if (isGNUVarArgs) MI->setIsGNUVarargs();
1744 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1745 PP.getPreprocessorAllocator());
1746 }
1747
1748 // Finally, install the macro.
1749 PP.setMacroInfo(II, MI);
1750
1751 // Remember that we saw this macro last so that we add the tokens that
1752 // form its body to it.
1753 Macro = MI;
1754 ++NumMacrosRead;
1755 break;
1756 }
1757
1758 case pch::PP_TOKEN: {
1759 // If we see a TOKEN before a PP_MACRO_*, then the file is
1760 // erroneous, just pretend we didn't see this.
1761 if (Macro == 0) break;
1762
1763 Token Tok;
1764 Tok.startToken();
1765 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1766 Tok.setLength(Record[1]);
1767 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1768 Tok.setIdentifierInfo(II);
1769 Tok.setKind((tok::TokenKind)Record[3]);
1770 Tok.setFlag((Token::TokenFlags)Record[4]);
1771 Macro->AddTokenToBody(Tok);
1772 break;
1773 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001774 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001775 }
1776}
1777
Douglas Gregorc713da92009-04-21 22:25:48 +00001778PCHReader::PCHReadResult
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001779PCHReader::ReadPCHBlock() {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001780 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1781 Error("Malformed block record");
1782 return Failure;
1783 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001784
1785 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001786 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787 while (!Stream.AtEndOfStream()) {
1788 unsigned Code = Stream.ReadCode();
1789 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001790 if (Stream.ReadBlockEnd()) {
1791 Error("Error at end of module block");
1792 return Failure;
1793 }
Chris Lattner29241862009-04-11 21:15:38 +00001794
Douglas Gregor179cfb12009-04-10 20:39:37 +00001795 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001796 }
1797
1798 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1799 switch (Stream.ReadSubBlockID()) {
1800 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1801 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1802 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001803 if (Stream.SkipBlock()) {
1804 Error("Malformed block record");
1805 return Failure;
1806 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001807 break;
1808
Chris Lattner29241862009-04-11 21:15:38 +00001809 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner29241862009-04-11 21:15:38 +00001810 if (Stream.SkipBlock()) {
1811 Error("Malformed block record");
1812 return Failure;
1813 }
1814 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001815
Douglas Gregorab1cef72009-04-10 03:52:48 +00001816 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001817 switch (ReadSourceManagerBlock()) {
1818 case Success:
1819 break;
1820
1821 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001822 Error("Malformed source manager block");
1823 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001824
1825 case IgnorePCH:
1826 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001827 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001828 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001829 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001830 continue;
1831 }
1832
1833 if (Code == llvm::bitc::DEFINE_ABBREV) {
1834 Stream.ReadAbbrevRecord();
1835 continue;
1836 }
1837
1838 // Read and process a record.
1839 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001840 const char *BlobStart = 0;
1841 unsigned BlobLen = 0;
1842 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1843 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001844 default: // Default behavior: ignore.
1845 break;
1846
1847 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001848 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001849 Error("Duplicate TYPE_OFFSET record in PCH file");
1850 return Failure;
1851 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001852 TypeOffsets = (const uint64_t *)BlobStart;
1853 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001854 break;
1855
1856 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001857 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001858 Error("Duplicate DECL_OFFSET record in PCH file");
1859 return Failure;
1860 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001861 DeclOffsets = (const uint64_t *)BlobStart;
1862 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001863 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001864
1865 case pch::LANGUAGE_OPTIONS:
1866 if (ParseLanguageOptions(Record))
1867 return IgnorePCH;
1868 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001869
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001870 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001871 std::string TargetTriple(BlobStart, BlobLen);
1872 if (TargetTriple != Context.Target.getTargetTriple()) {
1873 Diag(diag::warn_pch_target_triple)
1874 << TargetTriple << Context.Target.getTargetTriple();
1875 Diag(diag::note_ignoring_pch) << FileName;
1876 return IgnorePCH;
1877 }
1878 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001879 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001880
1881 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001882 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001883 if (Record[0]) {
1884 IdentifierLookupTable
1885 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +00001886 (const unsigned char *)IdentifierTableData + Record[0],
1887 (const unsigned char *)IdentifierTableData,
1888 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001889 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1890 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001891 break;
1892
1893 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001894 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001895 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1896 return Failure;
1897 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001898 IdentifierOffsets = (const uint32_t *)BlobStart;
1899 IdentifiersLoaded.resize(Record[0]);
Douglas Gregoreccb51d2009-04-25 23:30:02 +00001900 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001901 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001902
1903 case pch::EXTERNAL_DEFINITIONS:
1904 if (!ExternalDefinitions.empty()) {
1905 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1906 return Failure;
1907 }
1908 ExternalDefinitions.swap(Record);
1909 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001910
Douglas Gregore01ad442009-04-18 05:55:16 +00001911 case pch::SPECIAL_TYPES:
1912 SpecialTypes.swap(Record);
1913 break;
1914
Douglas Gregor456e0952009-04-17 22:13:46 +00001915 case pch::STATISTICS:
1916 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001917 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001918 TotalLexicalDeclContexts = Record[2];
1919 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001920 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001921 case pch::TENTATIVE_DEFINITIONS:
1922 if (!TentativeDefinitions.empty()) {
1923 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1924 return Failure;
1925 }
1926 TentativeDefinitions.swap(Record);
1927 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001928
1929 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1930 if (!LocallyScopedExternalDecls.empty()) {
1931 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1932 return Failure;
1933 }
1934 LocallyScopedExternalDecls.swap(Record);
1935 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001936
Douglas Gregor2d711832009-04-25 17:48:32 +00001937 case pch::SELECTOR_OFFSETS:
1938 SelectorOffsets = (const uint32_t *)BlobStart;
1939 TotalNumSelectors = Record[0];
1940 SelectorsLoaded.resize(TotalNumSelectors);
1941 break;
1942
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001943 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +00001944 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1945 if (Record[0])
1946 MethodPoolLookupTable
1947 = PCHMethodPoolLookupTable::Create(
1948 MethodPoolLookupTableData + Record[0],
1949 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001950 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +00001951 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001952 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001953
1954 case pch::PP_COUNTER_VALUE:
1955 if (!Record.empty())
1956 PP.setCounterValue(Record[0]);
1957 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001958 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001959 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001960 Error("Premature end of bitstream");
1961 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001962}
1963
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001964PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001965 // Set the PCH file name.
1966 this->FileName = FileName;
1967
Douglas Gregorc34897d2009-04-09 22:27:44 +00001968 // Open the PCH file.
1969 std::string ErrStr;
1970 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001971 if (!Buffer) {
1972 Error(ErrStr.c_str());
1973 return IgnorePCH;
1974 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001975
1976 // Initialize the stream
1977 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1978 (const unsigned char *)Buffer->getBufferEnd());
1979
1980 // Sniff for the signature.
1981 if (Stream.Read(8) != 'C' ||
1982 Stream.Read(8) != 'P' ||
1983 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001984 Stream.Read(8) != 'H') {
1985 Error("Not a PCH file");
1986 return IgnorePCH;
1987 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001988
Douglas Gregorc34897d2009-04-09 22:27:44 +00001989 while (!Stream.AtEndOfStream()) {
1990 unsigned Code = Stream.ReadCode();
1991
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001992 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1993 Error("Invalid record at top-level");
1994 return Failure;
1995 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001996
1997 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001998
Douglas Gregorc34897d2009-04-09 22:27:44 +00001999 // We only know the PCH subblock ID.
2000 switch (BlockID) {
2001 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002002 if (Stream.ReadBlockInfoBlock()) {
2003 Error("Malformed BlockInfoBlock");
2004 return Failure;
2005 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002006 break;
2007 case pch::PCH_BLOCK_ID:
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002008 switch (ReadPCHBlock()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002009 case Success:
2010 break;
2011
2012 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002013 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002014
2015 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002016 // FIXME: We could consider reading through to the end of this
2017 // PCH block, skipping subblocks, to see if there are other
2018 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002019 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002020 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002021 break;
2022 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002023 if (Stream.SkipBlock()) {
2024 Error("Malformed block record");
2025 return Failure;
2026 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002027 break;
2028 }
2029 }
2030
2031 // Load the translation unit declaration
2032 ReadDeclRecord(DeclOffsets[0], 0);
2033
Douglas Gregorc713da92009-04-21 22:25:48 +00002034 // Initialization of builtins and library builtins occurs before the
2035 // PCH file is read, so there may be some identifiers that were
2036 // loaded into the IdentifierTable before we intercepted the
2037 // creation of identifiers. Iterate through the list of known
2038 // identifiers and determine whether we have to establish
2039 // preprocessor definitions or top-level identifier declaration
2040 // chains for those identifiers.
2041 //
2042 // We copy the IdentifierInfo pointers to a small vector first,
2043 // since de-serializing declarations or macro definitions can add
2044 // new entries into the identifier table, invalidating the
2045 // iterators.
2046 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2047 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2048 IdEnd = PP.getIdentifierTable().end();
2049 Id != IdEnd; ++Id)
2050 Identifiers.push_back(Id->second);
2051 PCHIdentifierLookupTable *IdTable
2052 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2053 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2054 IdentifierInfo *II = Identifiers[I];
2055 // Look in the on-disk hash table for an entry for
2056 PCHIdentifierLookupTrait Info(*this, II);
2057 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2058 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2059 if (Pos == IdTable->end())
2060 continue;
2061
2062 // Dereferencing the iterator has the effect of populating the
2063 // IdentifierInfo node with the various declarations it needs.
2064 (void)*Pos;
2065 }
2066
Douglas Gregore01ad442009-04-18 05:55:16 +00002067 // Load the special types.
2068 Context.setBuiltinVaListType(
2069 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002070 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2071 Context.setObjCIdType(GetType(Id));
2072 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2073 Context.setObjCSelType(GetType(Sel));
2074 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2075 Context.setObjCProtoType(GetType(Proto));
2076 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2077 Context.setObjCClassType(GetType(Class));
2078 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2079 Context.setCFConstantStringType(GetType(String));
2080 if (unsigned FastEnum
2081 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2082 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002083
Douglas Gregorc713da92009-04-21 22:25:48 +00002084 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002085}
2086
Douglas Gregor179cfb12009-04-10 20:39:37 +00002087/// \brief Parse the record that corresponds to a LangOptions data
2088/// structure.
2089///
2090/// This routine compares the language options used to generate the
2091/// PCH file against the language options set for the current
2092/// compilation. For each option, we classify differences between the
2093/// two compiler states as either "benign" or "important". Benign
2094/// differences don't matter, and we accept them without complaint
2095/// (and without modifying the language options). Differences between
2096/// the states for important options cause the PCH file to be
2097/// unusable, so we emit a warning and return true to indicate that
2098/// there was an error.
2099///
2100/// \returns true if the PCH file is unacceptable, false otherwise.
2101bool PCHReader::ParseLanguageOptions(
2102 const llvm::SmallVectorImpl<uint64_t> &Record) {
2103 const LangOptions &LangOpts = Context.getLangOptions();
2104#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2105#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2106 if (Record[Idx] != LangOpts.Option) { \
2107 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2108 Diag(diag::note_ignoring_pch) << FileName; \
2109 return true; \
2110 } \
2111 ++Idx
2112
2113 unsigned Idx = 0;
2114 PARSE_LANGOPT_BENIGN(Trigraphs);
2115 PARSE_LANGOPT_BENIGN(BCPLComment);
2116 PARSE_LANGOPT_BENIGN(DollarIdents);
2117 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2118 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2119 PARSE_LANGOPT_BENIGN(ImplicitInt);
2120 PARSE_LANGOPT_BENIGN(Digraphs);
2121 PARSE_LANGOPT_BENIGN(HexFloats);
2122 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2123 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2124 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2125 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2126 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2127 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2128 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2129 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2130 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2131 PARSE_LANGOPT_BENIGN(PascalStrings);
2132 PARSE_LANGOPT_BENIGN(Boolean);
2133 PARSE_LANGOPT_BENIGN(WritableStrings);
2134 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2135 diag::warn_pch_lax_vector_conversions);
2136 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2137 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2138 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2139 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2140 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2141 diag::warn_pch_thread_safe_statics);
2142 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2143 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2144 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2145 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2146 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2147 diag::warn_pch_heinous_extensions);
2148 // FIXME: Most of the options below are benign if the macro wasn't
2149 // used. Unfortunately, this means that a PCH compiled without
2150 // optimization can't be used with optimization turned on, even
2151 // though the only thing that changes is whether __OPTIMIZE__ was
2152 // defined... but if __OPTIMIZE__ never showed up in the header, it
2153 // doesn't matter. We could consider making this some special kind
2154 // of check.
2155 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2156 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2157 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2158 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2159 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2160 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2161 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2162 Diag(diag::warn_pch_gc_mode)
2163 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2164 Diag(diag::note_ignoring_pch) << FileName;
2165 return true;
2166 }
2167 ++Idx;
2168 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2169 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2170#undef PARSE_LANGOPT_IRRELEVANT
2171#undef PARSE_LANGOPT_BENIGN
2172
2173 return false;
2174}
2175
Douglas Gregorc34897d2009-04-09 22:27:44 +00002176/// \brief Read and return the type at the given offset.
2177///
2178/// This routine actually reads the record corresponding to the type
2179/// at the given offset in the bitstream. It is a helper routine for
2180/// GetType, which deals with reading type IDs.
2181QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002182 // Keep track of where we are in the stream, then jump back there
2183 // after reading this type.
2184 SavedStreamPosition SavedPosition(Stream);
2185
Douglas Gregorc34897d2009-04-09 22:27:44 +00002186 Stream.JumpToBit(Offset);
2187 RecordData Record;
2188 unsigned Code = Stream.ReadCode();
2189 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002190 case pch::TYPE_EXT_QUAL: {
2191 assert(Record.size() == 3 &&
2192 "Incorrect encoding of extended qualifier type");
2193 QualType Base = GetType(Record[0]);
2194 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2195 unsigned AddressSpace = Record[2];
2196
2197 QualType T = Base;
2198 if (GCAttr != QualType::GCNone)
2199 T = Context.getObjCGCQualType(T, GCAttr);
2200 if (AddressSpace)
2201 T = Context.getAddrSpaceQualType(T, AddressSpace);
2202 return T;
2203 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002204
Douglas Gregorc34897d2009-04-09 22:27:44 +00002205 case pch::TYPE_FIXED_WIDTH_INT: {
2206 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2207 return Context.getFixedWidthIntType(Record[0], Record[1]);
2208 }
2209
2210 case pch::TYPE_COMPLEX: {
2211 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2212 QualType ElemType = GetType(Record[0]);
2213 return Context.getComplexType(ElemType);
2214 }
2215
2216 case pch::TYPE_POINTER: {
2217 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2218 QualType PointeeType = GetType(Record[0]);
2219 return Context.getPointerType(PointeeType);
2220 }
2221
2222 case pch::TYPE_BLOCK_POINTER: {
2223 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2224 QualType PointeeType = GetType(Record[0]);
2225 return Context.getBlockPointerType(PointeeType);
2226 }
2227
2228 case pch::TYPE_LVALUE_REFERENCE: {
2229 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2230 QualType PointeeType = GetType(Record[0]);
2231 return Context.getLValueReferenceType(PointeeType);
2232 }
2233
2234 case pch::TYPE_RVALUE_REFERENCE: {
2235 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2236 QualType PointeeType = GetType(Record[0]);
2237 return Context.getRValueReferenceType(PointeeType);
2238 }
2239
2240 case pch::TYPE_MEMBER_POINTER: {
2241 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2242 QualType PointeeType = GetType(Record[0]);
2243 QualType ClassType = GetType(Record[1]);
2244 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2245 }
2246
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002247 case pch::TYPE_CONSTANT_ARRAY: {
2248 QualType ElementType = GetType(Record[0]);
2249 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2250 unsigned IndexTypeQuals = Record[2];
2251 unsigned Idx = 3;
2252 llvm::APInt Size = ReadAPInt(Record, Idx);
2253 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2254 }
2255
2256 case pch::TYPE_INCOMPLETE_ARRAY: {
2257 QualType ElementType = GetType(Record[0]);
2258 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2259 unsigned IndexTypeQuals = Record[2];
2260 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2261 }
2262
2263 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002264 QualType ElementType = GetType(Record[0]);
2265 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2266 unsigned IndexTypeQuals = Record[2];
2267 return Context.getVariableArrayType(ElementType, ReadExpr(),
2268 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002269 }
2270
2271 case pch::TYPE_VECTOR: {
2272 if (Record.size() != 2) {
2273 Error("Incorrect encoding of vector type in PCH file");
2274 return QualType();
2275 }
2276
2277 QualType ElementType = GetType(Record[0]);
2278 unsigned NumElements = Record[1];
2279 return Context.getVectorType(ElementType, NumElements);
2280 }
2281
2282 case pch::TYPE_EXT_VECTOR: {
2283 if (Record.size() != 2) {
2284 Error("Incorrect encoding of extended vector type in PCH file");
2285 return QualType();
2286 }
2287
2288 QualType ElementType = GetType(Record[0]);
2289 unsigned NumElements = Record[1];
2290 return Context.getExtVectorType(ElementType, NumElements);
2291 }
2292
2293 case pch::TYPE_FUNCTION_NO_PROTO: {
2294 if (Record.size() != 1) {
2295 Error("Incorrect encoding of no-proto function type");
2296 return QualType();
2297 }
2298 QualType ResultType = GetType(Record[0]);
2299 return Context.getFunctionNoProtoType(ResultType);
2300 }
2301
2302 case pch::TYPE_FUNCTION_PROTO: {
2303 QualType ResultType = GetType(Record[0]);
2304 unsigned Idx = 1;
2305 unsigned NumParams = Record[Idx++];
2306 llvm::SmallVector<QualType, 16> ParamTypes;
2307 for (unsigned I = 0; I != NumParams; ++I)
2308 ParamTypes.push_back(GetType(Record[Idx++]));
2309 bool isVariadic = Record[Idx++];
2310 unsigned Quals = Record[Idx++];
2311 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2312 isVariadic, Quals);
2313 }
2314
2315 case pch::TYPE_TYPEDEF:
2316 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2317 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2318
2319 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002320 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002321
2322 case pch::TYPE_TYPEOF: {
2323 if (Record.size() != 1) {
2324 Error("Incorrect encoding of typeof(type) in PCH file");
2325 return QualType();
2326 }
2327 QualType UnderlyingType = GetType(Record[0]);
2328 return Context.getTypeOfType(UnderlyingType);
2329 }
2330
2331 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002332 assert(Record.size() == 1 && "Incorrect encoding of record type");
2333 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002334
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002335 case pch::TYPE_ENUM:
2336 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2337 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2338
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002339 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002340 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2341 return Context.getObjCInterfaceType(
2342 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002343
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002344 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2345 unsigned Idx = 0;
2346 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2347 unsigned NumProtos = Record[Idx++];
2348 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2349 for (unsigned I = 0; I != NumProtos; ++I)
2350 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2351 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2352 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002353
Chris Lattner9b9f2352009-04-22 06:40:03 +00002354 case pch::TYPE_OBJC_QUALIFIED_ID: {
2355 unsigned Idx = 0;
2356 unsigned NumProtos = Record[Idx++];
2357 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2358 for (unsigned I = 0; I != NumProtos; ++I)
2359 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2360 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2361 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002362 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002363 // Suppress a GCC warning
2364 return QualType();
2365}
2366
2367/// \brief Note that we have loaded the declaration with the given
2368/// Index.
2369///
2370/// This routine notes that this declaration has already been loaded,
2371/// so that future GetDecl calls will return this declaration rather
2372/// than trying to load a new declaration.
2373inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002374 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2375 DeclsLoaded[Index] = D;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002376}
2377
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002378/// \brief Determine whether the consumer will be interested in seeing
2379/// this declaration (via HandleTopLevelDecl).
2380///
2381/// This routine should return true for anything that might affect
2382/// code generation, e.g., inline function definitions, Objective-C
2383/// declarations with metadata, etc.
2384static bool isConsumerInterestedIn(Decl *D) {
2385 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2386 return Var->isFileVarDecl() && Var->getInit();
2387 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2388 return Func->isThisDeclarationADefinition();
2389 return isa<ObjCProtocolDecl>(D);
2390}
2391
Douglas Gregorc34897d2009-04-09 22:27:44 +00002392/// \brief Read the declaration at the given offset from the PCH file.
2393Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002394 // Keep track of where we are in the stream, then jump back there
2395 // after reading this declaration.
2396 SavedStreamPosition SavedPosition(Stream);
2397
Douglas Gregorc34897d2009-04-09 22:27:44 +00002398 Decl *D = 0;
2399 Stream.JumpToBit(Offset);
2400 RecordData Record;
2401 unsigned Code = Stream.ReadCode();
2402 unsigned Idx = 0;
2403 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002404
Douglas Gregorc34897d2009-04-09 22:27:44 +00002405 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002406 case pch::DECL_ATTR:
2407 case pch::DECL_CONTEXT_LEXICAL:
2408 case pch::DECL_CONTEXT_VISIBLE:
2409 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2410 break;
2411
Douglas Gregorc34897d2009-04-09 22:27:44 +00002412 case pch::DECL_TRANSLATION_UNIT:
2413 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002414 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002415 break;
2416
2417 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002418 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002419 break;
2420 }
2421
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002422 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002423 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002424 break;
2425 }
2426
Douglas Gregor982365e2009-04-13 21:20:57 +00002427 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002428 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2429 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002430 break;
2431 }
2432
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002433 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002434 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2435 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002436 break;
2437 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002438
2439 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002440 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2441 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002442 break;
2443 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002444
Steve Naroff79ea0e02009-04-20 15:06:07 +00002445 case pch::DECL_OBJC_METHOD: {
2446 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2447 Selector(), QualType(), 0);
2448 break;
2449 }
2450
Steve Naroff97b53bd2009-04-21 15:12:33 +00002451 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002452 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2453 break;
2454 }
2455
Steve Naroff97b53bd2009-04-21 15:12:33 +00002456 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002457 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2458 ObjCIvarDecl::None);
2459 break;
2460 }
2461
Steve Naroff97b53bd2009-04-21 15:12:33 +00002462 case pch::DECL_OBJC_PROTOCOL: {
2463 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2464 break;
2465 }
2466
2467 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2468 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2469 QualType(), 0);
2470 break;
2471 }
2472
2473 case pch::DECL_OBJC_CLASS: {
2474 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2475 break;
2476 }
2477
2478 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2479 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2480 break;
2481 }
2482
2483 case pch::DECL_OBJC_CATEGORY: {
2484 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2485 break;
2486 }
2487
2488 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002489 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002490 break;
2491 }
2492
2493 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002494 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002495 break;
2496 }
2497
2498 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002499 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002500 break;
2501 }
2502
2503 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002504 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002505 break;
2506 }
2507
2508 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002509 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2510 SourceLocation(), 0,
2511 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002512 break;
2513 }
2514
Douglas Gregor982365e2009-04-13 21:20:57 +00002515 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002516 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2517 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002518 break;
2519 }
2520
Douglas Gregorc34897d2009-04-09 22:27:44 +00002521 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002522 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2523 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002524 break;
2525 }
2526
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002527 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002528 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2529 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002530 break;
2531 }
2532
2533 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002534 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002535 QualType(), QualType(), VarDecl::None,
2536 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002537 break;
2538 }
2539
Douglas Gregor2a491792009-04-13 22:49:25 +00002540 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002541 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002542 break;
2543 }
2544
2545 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002546 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002547 break;
2548 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002549 }
2550
Douglas Gregorc713da92009-04-21 22:25:48 +00002551 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002552 if (D) {
2553 LoadedDecl(Index, D);
2554 Reader.Visit(D);
2555 }
2556
Douglas Gregorc34897d2009-04-09 22:27:44 +00002557 // If this declaration is also a declaration context, get the
2558 // offsets for its tables of lexical and visible declarations.
2559 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2560 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2561 if (Offsets.first || Offsets.second) {
2562 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2563 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2564 DeclContextOffsets[DC] = Offsets;
2565 }
2566 }
2567 assert(Idx == Record.size());
2568
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002569 // If we have deserialized a declaration that has a definition the
2570 // AST consumer might need to know about, notify the consumer
2571 // about that definition now or queue it for later.
2572 if (isConsumerInterestedIn(D)) {
2573 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002574 DeclGroupRef DG(D);
2575 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002576 } else {
2577 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002578 }
2579 }
2580
Douglas Gregorc34897d2009-04-09 22:27:44 +00002581 return D;
2582}
2583
Douglas Gregorac8f2802009-04-10 17:25:41 +00002584QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002585 unsigned Quals = ID & 0x07;
2586 unsigned Index = ID >> 3;
2587
2588 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2589 QualType T;
2590 switch ((pch::PredefinedTypeIDs)Index) {
2591 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2592 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2593 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2594
2595 case pch::PREDEF_TYPE_CHAR_U_ID:
2596 case pch::PREDEF_TYPE_CHAR_S_ID:
2597 // FIXME: Check that the signedness of CharTy is correct!
2598 T = Context.CharTy;
2599 break;
2600
2601 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2602 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2603 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2604 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2605 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2606 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2607 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2608 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2609 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2610 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2611 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2612 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2613 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2614 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2615 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2616 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2617 }
2618
2619 assert(!T.isNull() && "Unknown predefined type");
2620 return T.getQualifiedType(Quals);
2621 }
2622
2623 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregore43f0972009-04-26 03:49:13 +00002624 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Douglas Gregor24a224c2009-04-25 18:35:21 +00002625 if (!TypesLoaded[Index])
2626 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002627
Douglas Gregor24a224c2009-04-25 18:35:21 +00002628 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002629}
2630
Douglas Gregorac8f2802009-04-10 17:25:41 +00002631Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002632 if (ID == 0)
2633 return 0;
2634
Douglas Gregor24a224c2009-04-25 18:35:21 +00002635 if (ID > DeclsLoaded.size()) {
2636 Error("Declaration ID out-of-range for PCH file");
2637 return 0;
2638 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002639
Douglas Gregor24a224c2009-04-25 18:35:21 +00002640 unsigned Index = ID - 1;
2641 if (!DeclsLoaded[Index])
2642 ReadDeclRecord(DeclOffsets[Index], Index);
2643
2644 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002645}
2646
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002647Stmt *PCHReader::GetStmt(uint64_t Offset) {
2648 // Keep track of where we are in the stream, then jump back there
2649 // after reading this declaration.
2650 SavedStreamPosition SavedPosition(Stream);
2651
2652 Stream.JumpToBit(Offset);
2653 return ReadStmt();
2654}
2655
Douglas Gregorc34897d2009-04-09 22:27:44 +00002656bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002657 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002658 assert(DC->hasExternalLexicalStorage() &&
2659 "DeclContext has no lexical decls in storage");
2660 uint64_t Offset = DeclContextOffsets[DC].first;
2661 assert(Offset && "DeclContext has no lexical decls in storage");
2662
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002663 // Keep track of where we are in the stream, then jump back there
2664 // after reading this context.
2665 SavedStreamPosition SavedPosition(Stream);
2666
Douglas Gregorc34897d2009-04-09 22:27:44 +00002667 // Load the record containing all of the declarations lexically in
2668 // this context.
2669 Stream.JumpToBit(Offset);
2670 RecordData Record;
2671 unsigned Code = Stream.ReadCode();
2672 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002673 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002674 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2675
2676 // Load all of the declaration IDs
2677 Decls.clear();
2678 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002679 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002680 return false;
2681}
2682
2683bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2684 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2685 assert(DC->hasExternalVisibleStorage() &&
2686 "DeclContext has no visible decls in storage");
2687 uint64_t Offset = DeclContextOffsets[DC].second;
2688 assert(Offset && "DeclContext has no visible decls in storage");
2689
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002690 // Keep track of where we are in the stream, then jump back there
2691 // after reading this context.
2692 SavedStreamPosition SavedPosition(Stream);
2693
Douglas Gregorc34897d2009-04-09 22:27:44 +00002694 // Load the record containing all of the declarations visible in
2695 // this context.
2696 Stream.JumpToBit(Offset);
2697 RecordData Record;
2698 unsigned Code = Stream.ReadCode();
2699 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002700 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002701 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2702 if (Record.size() == 0)
2703 return false;
2704
2705 Decls.clear();
2706
2707 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002708 while (Idx < Record.size()) {
2709 Decls.push_back(VisibleDeclaration());
2710 Decls.back().Name = ReadDeclarationName(Record, Idx);
2711
Douglas Gregorc34897d2009-04-09 22:27:44 +00002712 unsigned Size = Record[Idx++];
2713 llvm::SmallVector<unsigned, 4> & LoadedDecls
2714 = Decls.back().Declarations;
2715 LoadedDecls.reserve(Size);
2716 for (unsigned I = 0; I < Size; ++I)
2717 LoadedDecls.push_back(Record[Idx++]);
2718 }
2719
Douglas Gregoraf136d92009-04-22 22:34:57 +00002720 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002721 return false;
2722}
2723
Douglas Gregor631f6c62009-04-14 00:24:19 +00002724void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002725 this->Consumer = Consumer;
2726
Douglas Gregor631f6c62009-04-14 00:24:19 +00002727 if (!Consumer)
2728 return;
2729
2730 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2731 Decl *D = GetDecl(ExternalDefinitions[I]);
2732 DeclGroupRef DG(D);
2733 Consumer->HandleTopLevelDecl(DG);
2734 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002735
2736 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2737 DeclGroupRef DG(InterestingDecls[I]);
2738 Consumer->HandleTopLevelDecl(DG);
2739 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002740}
2741
Douglas Gregorc34897d2009-04-09 22:27:44 +00002742void PCHReader::PrintStats() {
2743 std::fprintf(stderr, "*** PCH Statistics:\n");
2744
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002745 unsigned NumTypesLoaded
2746 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2747 (Type *)0);
2748 unsigned NumDeclsLoaded
2749 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2750 (Decl *)0);
2751 unsigned NumIdentifiersLoaded
2752 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2753 IdentifiersLoaded.end(),
2754 (IdentifierInfo *)0);
2755 unsigned NumSelectorsLoaded
2756 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2757 SelectorsLoaded.end(),
2758 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00002759
Douglas Gregor24a224c2009-04-25 18:35:21 +00002760 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002761 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002762 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2763 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2764 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002765 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002766 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2767 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002768 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002769 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002770 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2771 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00002772 if (TotalNumSelectors)
2773 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2774 NumSelectorsLoaded, TotalNumSelectors,
2775 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2776 if (TotalNumStatements)
2777 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2778 NumStatementsRead, TotalNumStatements,
2779 ((float)NumStatementsRead/TotalNumStatements * 100));
2780 if (TotalNumMacros)
2781 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2782 NumMacrosRead, TotalNumMacros,
2783 ((float)NumMacrosRead/TotalNumMacros * 100));
2784 if (TotalLexicalDeclContexts)
2785 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2786 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2787 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2788 * 100));
2789 if (TotalVisibleDeclContexts)
2790 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2791 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2792 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2793 * 100));
2794 if (TotalSelectorsInMethodPool) {
2795 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2796 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2797 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2798 * 100));
2799 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2800 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002801 std::fprintf(stderr, "\n");
2802}
2803
Douglas Gregorc713da92009-04-21 22:25:48 +00002804void PCHReader::InitializeSema(Sema &S) {
2805 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002806 S.ExternalSource = this;
2807
Douglas Gregor2554cf22009-04-22 21:15:06 +00002808 // Makes sure any declarations that were deserialized "too early"
2809 // still get added to the identifier's declaration chains.
2810 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2811 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2812 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002813 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002814 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002815
2816 // If there were any tentative definitions, deserialize them and add
2817 // them to Sema's table of tentative definitions.
2818 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2819 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2820 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2821 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002822
2823 // If there were any locally-scoped external declarations,
2824 // deserialize them and add them to Sema's table of locally-scoped
2825 // external declarations.
2826 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2827 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2828 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2829 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002830}
2831
2832IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2833 // Try to find this name within our on-disk hash table
2834 PCHIdentifierLookupTable *IdTable
2835 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2836 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2837 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2838 if (Pos == IdTable->end())
2839 return 0;
2840
2841 // Dereferencing the iterator has the effect of building the
2842 // IdentifierInfo node and populating it with the various
2843 // declarations it needs.
2844 return *Pos;
2845}
2846
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002847std::pair<ObjCMethodList, ObjCMethodList>
2848PCHReader::ReadMethodPool(Selector Sel) {
2849 if (!MethodPoolLookupTable)
2850 return std::pair<ObjCMethodList, ObjCMethodList>();
2851
2852 // Try to find this selector within our on-disk hash table.
2853 PCHMethodPoolLookupTable *PoolTable
2854 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2855 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00002856 if (Pos == PoolTable->end()) {
2857 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002858 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00002859 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002860
Douglas Gregor2d711832009-04-25 17:48:32 +00002861 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002862 return *Pos;
2863}
2864
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002865void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00002866 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002867 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2868 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00002869}
2870
Chris Lattner29241862009-04-11 21:15:38 +00002871IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002872 if (ID == 0)
2873 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002874
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002875 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002876 Error("No identifier table in PCH file");
2877 return 0;
2878 }
Chris Lattner29241862009-04-11 21:15:38 +00002879
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002880 if (!IdentifiersLoaded[ID - 1]) {
2881 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002882 const char *Str = IdentifierTableData + Offset;
Douglas Gregor85c4a872009-04-25 21:04:17 +00002883
2884 // If there is an identifier lookup table, but the offset of this
2885 // string is after the identifier table itself, then we know that
2886 // this string is not in the on-disk hash table. Therefore,
2887 // disable lookup into the hash table when looking for this
2888 // identifier.
2889 PCHIdentifierLookupTable *IdTable
2890 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002891 if (!IdTable ||
2892 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
2893 // Turn off lookup into the on-disk hash table. We know that
2894 // this identifier is not there.
2895 if (IdTable)
2896 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002897
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002898 // All of the strings in the PCH file are preceded by a 16-bit
2899 // length. Extract that 16-bit length to avoid having to execute
2900 // strlen().
2901 const char *StrLenPtr = Str - 2;
2902 unsigned StrLen = (((unsigned) StrLenPtr[0])
2903 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
2904 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002905
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002906 // Turn on lookup into the on-disk hash table, if we have an
2907 // on-disk hash table.
2908 if (IdTable)
2909 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2910 } else {
2911 // The identifier is a key in our on-disk hash table. Since we
2912 // know where the hash table entry starts, just read in this
2913 // (key, value) pair.
2914 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
2915 const unsigned char *Pos = (const unsigned char *)Str - 4;
2916 std::pair<unsigned, unsigned> KeyDataLengths
2917 = Trait.ReadKeyDataLength(Pos);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002918
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002919 PCHIdentifierLookupTrait::internal_key_type InternalKey
2920 = Trait.ReadKey(Pos, KeyDataLengths.first);
2921 Pos = (const unsigned char *)Str + KeyDataLengths.first;
2922 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
2923 KeyDataLengths.second);
2924 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002925 }
Chris Lattner29241862009-04-11 21:15:38 +00002926
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002927 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002928}
2929
Steve Naroff9e84d782009-04-23 10:39:46 +00002930Selector PCHReader::DecodeSelector(unsigned ID) {
2931 if (ID == 0)
2932 return Selector();
2933
Douglas Gregor2d711832009-04-25 17:48:32 +00002934 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002935 Error("No selector table in PCH file");
2936 return Selector();
2937 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002938
2939 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002940 Error("Selector ID out of range");
2941 return Selector();
2942 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002943
2944 unsigned Index = ID - 1;
2945 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2946 // Load this selector from the selector table.
2947 // FIXME: endianness portability issues with SelectorOffsets table
2948 PCHMethodPoolLookupTrait Trait(*this);
2949 SelectorsLoaded[Index]
2950 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2951 }
2952
2953 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00002954}
2955
Douglas Gregorc34897d2009-04-09 22:27:44 +00002956DeclarationName
2957PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2958 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2959 switch (Kind) {
2960 case DeclarationName::Identifier:
2961 return DeclarationName(GetIdentifierInfo(Record, Idx));
2962
2963 case DeclarationName::ObjCZeroArgSelector:
2964 case DeclarationName::ObjCOneArgSelector:
2965 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002966 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002967
2968 case DeclarationName::CXXConstructorName:
2969 return Context.DeclarationNames.getCXXConstructorName(
2970 GetType(Record[Idx++]));
2971
2972 case DeclarationName::CXXDestructorName:
2973 return Context.DeclarationNames.getCXXDestructorName(
2974 GetType(Record[Idx++]));
2975
2976 case DeclarationName::CXXConversionFunctionName:
2977 return Context.DeclarationNames.getCXXConversionFunctionName(
2978 GetType(Record[Idx++]));
2979
2980 case DeclarationName::CXXOperatorName:
2981 return Context.DeclarationNames.getCXXOperatorName(
2982 (OverloadedOperatorKind)Record[Idx++]);
2983
2984 case DeclarationName::CXXUsingDirective:
2985 return DeclarationName::getUsingDirectiveName();
2986 }
2987
2988 // Required to silence GCC warning
2989 return DeclarationName();
2990}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002991
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002992/// \brief Read an integral value
2993llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2994 unsigned BitWidth = Record[Idx++];
2995 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2996 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2997 Idx += NumWords;
2998 return Result;
2999}
3000
3001/// \brief Read a signed integral value
3002llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3003 bool isUnsigned = Record[Idx++];
3004 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3005}
3006
Douglas Gregore2f37202009-04-14 21:55:33 +00003007/// \brief Read a floating-point value
3008llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00003009 return llvm::APFloat(ReadAPInt(Record, Idx));
3010}
3011
Douglas Gregor1c507882009-04-15 21:30:51 +00003012// \brief Read a string
3013std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3014 unsigned Len = Record[Idx++];
3015 std::string Result(&Record[Idx], &Record[Idx] + Len);
3016 Idx += Len;
3017 return Result;
3018}
3019
3020/// \brief Reads attributes from the current stream position.
3021Attr *PCHReader::ReadAttributes() {
3022 unsigned Code = Stream.ReadCode();
3023 assert(Code == llvm::bitc::UNABBREV_RECORD &&
3024 "Expected unabbreviated record"); (void)Code;
3025
3026 RecordData Record;
3027 unsigned Idx = 0;
3028 unsigned RecCode = Stream.ReadRecord(Code, Record);
3029 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
3030 (void)RecCode;
3031
3032#define SIMPLE_ATTR(Name) \
3033 case Attr::Name: \
3034 New = ::new (Context) Name##Attr(); \
3035 break
3036
3037#define STRING_ATTR(Name) \
3038 case Attr::Name: \
3039 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
3040 break
3041
3042#define UNSIGNED_ATTR(Name) \
3043 case Attr::Name: \
3044 New = ::new (Context) Name##Attr(Record[Idx++]); \
3045 break
3046
3047 Attr *Attrs = 0;
3048 while (Idx < Record.size()) {
3049 Attr *New = 0;
3050 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
3051 bool IsInherited = Record[Idx++];
3052
3053 switch (Kind) {
3054 STRING_ATTR(Alias);
3055 UNSIGNED_ATTR(Aligned);
3056 SIMPLE_ATTR(AlwaysInline);
3057 SIMPLE_ATTR(AnalyzerNoReturn);
3058 STRING_ATTR(Annotate);
3059 STRING_ATTR(AsmLabel);
3060
3061 case Attr::Blocks:
3062 New = ::new (Context) BlocksAttr(
3063 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3064 break;
3065
3066 case Attr::Cleanup:
3067 New = ::new (Context) CleanupAttr(
3068 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3069 break;
3070
3071 SIMPLE_ATTR(Const);
3072 UNSIGNED_ATTR(Constructor);
3073 SIMPLE_ATTR(DLLExport);
3074 SIMPLE_ATTR(DLLImport);
3075 SIMPLE_ATTR(Deprecated);
3076 UNSIGNED_ATTR(Destructor);
3077 SIMPLE_ATTR(FastCall);
3078
3079 case Attr::Format: {
3080 std::string Type = ReadString(Record, Idx);
3081 unsigned FormatIdx = Record[Idx++];
3082 unsigned FirstArg = Record[Idx++];
3083 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3084 break;
3085 }
3086
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003087 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003088
3089 case Attr::IBOutletKind:
3090 New = ::new (Context) IBOutletAttr();
3091 break;
3092
3093 SIMPLE_ATTR(NoReturn);
3094 SIMPLE_ATTR(NoThrow);
3095 SIMPLE_ATTR(Nodebug);
3096 SIMPLE_ATTR(Noinline);
3097
3098 case Attr::NonNull: {
3099 unsigned Size = Record[Idx++];
3100 llvm::SmallVector<unsigned, 16> ArgNums;
3101 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3102 Idx += Size;
3103 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3104 break;
3105 }
3106
3107 SIMPLE_ATTR(ObjCException);
3108 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003109 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003110 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003111 SIMPLE_ATTR(Overloadable);
3112 UNSIGNED_ATTR(Packed);
3113 SIMPLE_ATTR(Pure);
3114 UNSIGNED_ATTR(Regparm);
3115 STRING_ATTR(Section);
3116 SIMPLE_ATTR(StdCall);
3117 SIMPLE_ATTR(TransparentUnion);
3118 SIMPLE_ATTR(Unavailable);
3119 SIMPLE_ATTR(Unused);
3120 SIMPLE_ATTR(Used);
3121
3122 case Attr::Visibility:
3123 New = ::new (Context) VisibilityAttr(
3124 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3125 break;
3126
3127 SIMPLE_ATTR(WarnUnusedResult);
3128 SIMPLE_ATTR(Weak);
3129 SIMPLE_ATTR(WeakImport);
3130 }
3131
3132 assert(New && "Unable to decode attribute?");
3133 New->setInherited(IsInherited);
3134 New->setNext(Attrs);
3135 Attrs = New;
3136 }
3137#undef UNSIGNED_ATTR
3138#undef STRING_ATTR
3139#undef SIMPLE_ATTR
3140
3141 // The list of attributes was built backwards. Reverse the list
3142 // before returning it.
3143 Attr *PrevAttr = 0, *NextAttr = 0;
3144 while (Attrs) {
3145 NextAttr = Attrs->getNext();
3146 Attrs->setNext(PrevAttr);
3147 PrevAttr = Attrs;
3148 Attrs = NextAttr;
3149 }
3150
3151 return PrevAttr;
3152}
3153
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003154Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003155 // Within the bitstream, expressions are stored in Reverse Polish
3156 // Notation, with each of the subexpressions preceding the
3157 // expression they are stored in. To evaluate expressions, we
3158 // continue reading expressions and placing them on the stack, with
3159 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003160 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003161 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003162 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003163 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003164 llvm::SmallVector<Stmt *, 16> StmtStack;
3165 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003166 Stmt::EmptyShell Empty;
3167
Douglas Gregora151ba42009-04-14 23:32:43 +00003168 while (true) {
3169 unsigned Code = Stream.ReadCode();
3170 if (Code == llvm::bitc::END_BLOCK) {
3171 if (Stream.ReadBlockEnd()) {
3172 Error("Error at end of Source Manager block");
3173 return 0;
3174 }
3175 break;
3176 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003177
Douglas Gregora151ba42009-04-14 23:32:43 +00003178 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3179 // No known subblocks, always skip them.
3180 Stream.ReadSubBlockID();
3181 if (Stream.SkipBlock()) {
3182 Error("Malformed block record");
3183 return 0;
3184 }
3185 continue;
3186 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003187
Douglas Gregora151ba42009-04-14 23:32:43 +00003188 if (Code == llvm::bitc::DEFINE_ABBREV) {
3189 Stream.ReadAbbrevRecord();
3190 continue;
3191 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003192
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003193 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003194 Idx = 0;
3195 Record.clear();
3196 bool Finished = false;
3197 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003198 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003199 Finished = true;
3200 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003201
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003202 case pch::STMT_NULL_PTR:
3203 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003204 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003205
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003206 case pch::STMT_NULL:
3207 S = new (Context) NullStmt(Empty);
3208 break;
3209
3210 case pch::STMT_COMPOUND:
3211 S = new (Context) CompoundStmt(Empty);
3212 break;
3213
3214 case pch::STMT_CASE:
3215 S = new (Context) CaseStmt(Empty);
3216 break;
3217
3218 case pch::STMT_DEFAULT:
3219 S = new (Context) DefaultStmt(Empty);
3220 break;
3221
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003222 case pch::STMT_LABEL:
3223 S = new (Context) LabelStmt(Empty);
3224 break;
3225
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003226 case pch::STMT_IF:
3227 S = new (Context) IfStmt(Empty);
3228 break;
3229
3230 case pch::STMT_SWITCH:
3231 S = new (Context) SwitchStmt(Empty);
3232 break;
3233
Douglas Gregora6b503f2009-04-17 00:16:09 +00003234 case pch::STMT_WHILE:
3235 S = new (Context) WhileStmt(Empty);
3236 break;
3237
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003238 case pch::STMT_DO:
3239 S = new (Context) DoStmt(Empty);
3240 break;
3241
3242 case pch::STMT_FOR:
3243 S = new (Context) ForStmt(Empty);
3244 break;
3245
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003246 case pch::STMT_GOTO:
3247 S = new (Context) GotoStmt(Empty);
3248 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003249
3250 case pch::STMT_INDIRECT_GOTO:
3251 S = new (Context) IndirectGotoStmt(Empty);
3252 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003253
Douglas Gregora6b503f2009-04-17 00:16:09 +00003254 case pch::STMT_CONTINUE:
3255 S = new (Context) ContinueStmt(Empty);
3256 break;
3257
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003258 case pch::STMT_BREAK:
3259 S = new (Context) BreakStmt(Empty);
3260 break;
3261
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003262 case pch::STMT_RETURN:
3263 S = new (Context) ReturnStmt(Empty);
3264 break;
3265
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003266 case pch::STMT_DECL:
3267 S = new (Context) DeclStmt(Empty);
3268 break;
3269
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003270 case pch::STMT_ASM:
3271 S = new (Context) AsmStmt(Empty);
3272 break;
3273
Douglas Gregora151ba42009-04-14 23:32:43 +00003274 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003275 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003276 break;
3277
3278 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003279 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003280 break;
3281
3282 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003283 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003284 break;
3285
3286 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003287 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003288 break;
3289
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003290 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003291 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003292 break;
3293
Douglas Gregor596e0932009-04-15 16:35:07 +00003294 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003295 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003296 Record[PCHStmtReader::NumExprFields + 1]);
3297 break;
3298
Douglas Gregora151ba42009-04-14 23:32:43 +00003299 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003300 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003301 break;
3302
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003303 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003304 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003305 break;
3306
Douglas Gregor12d74052009-04-15 15:58:59 +00003307 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003308 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003309 break;
3310
3311 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003312 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003313 break;
3314
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003315 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003316 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003317 break;
3318
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003319 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003320 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003321 break;
3322
3323 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003324 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003325 break;
3326
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003327 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003328 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003329 break;
3330
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003331 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003332 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003333 break;
3334
3335 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003336 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003337 break;
3338
Douglas Gregora151ba42009-04-14 23:32:43 +00003339 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003340 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003341 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003342
3343 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003344 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003345 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003346
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003347 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003348 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003349 break;
3350
Douglas Gregorec0b8292009-04-15 23:02:49 +00003351 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003352 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003353 break;
3354
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003355 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003356 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003357 break;
3358
3359 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003360 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003361 Record[PCHStmtReader::NumExprFields] - 1);
3362
3363 break;
3364
3365 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003366 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003367 break;
3368
Douglas Gregorec0b8292009-04-15 23:02:49 +00003369 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003370 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003371 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003372
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003373 case pch::EXPR_ADDR_LABEL:
3374 S = new (Context) AddrLabelExpr(Empty);
3375 break;
3376
Douglas Gregoreca12f62009-04-17 19:05:30 +00003377 case pch::EXPR_STMT:
3378 S = new (Context) StmtExpr(Empty);
3379 break;
3380
Douglas Gregor209d4622009-04-15 23:33:31 +00003381 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003382 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003383 break;
3384
3385 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003386 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003387 break;
3388
3389 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003390 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003391 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003392
3393 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003394 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003395 break;
3396
Douglas Gregore246b742009-04-17 19:21:43 +00003397 case pch::EXPR_BLOCK:
3398 S = new (Context) BlockExpr(Empty);
3399 break;
3400
Douglas Gregor725e94b2009-04-16 00:01:45 +00003401 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003402 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003403 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003404
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003405 case pch::EXPR_OBJC_STRING_LITERAL:
3406 S = new (Context) ObjCStringLiteral(Empty);
3407 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003408 case pch::EXPR_OBJC_ENCODE:
3409 S = new (Context) ObjCEncodeExpr(Empty);
3410 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003411 case pch::EXPR_OBJC_SELECTOR_EXPR:
3412 S = new (Context) ObjCSelectorExpr(Empty);
3413 break;
3414 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3415 S = new (Context) ObjCProtocolExpr(Empty);
3416 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003417 case pch::EXPR_OBJC_IVAR_REF_EXPR:
3418 S = new (Context) ObjCIvarRefExpr(Empty);
3419 break;
3420 case pch::EXPR_OBJC_PROPERTY_REF_EXPR:
3421 S = new (Context) ObjCPropertyRefExpr(Empty);
3422 break;
3423 case pch::EXPR_OBJC_KVC_REF_EXPR:
3424 S = new (Context) ObjCKVCRefExpr(Empty);
3425 break;
Steve Narofffb3e4022009-04-25 14:04:28 +00003426 case pch::EXPR_OBJC_MESSAGE_EXPR:
3427 S = new (Context) ObjCMessageExpr(Empty);
3428 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003429 case pch::EXPR_OBJC_SUPER_EXPR:
3430 S = new (Context) ObjCSuperExpr(Empty);
3431 break;
Steve Naroff79762bd2009-04-26 18:52:16 +00003432 case pch::STMT_OBJC_FOR_COLLECTION:
3433 S = new (Context) ObjCForCollectionStmt(Empty);
3434 break;
3435 case pch::STMT_OBJC_CATCH:
3436 S = new (Context) ObjCAtCatchStmt(Empty);
3437 break;
3438 case pch::STMT_OBJC_FINALLY:
3439 S = new (Context) ObjCAtFinallyStmt(Empty);
3440 break;
3441 case pch::STMT_OBJC_AT_TRY:
3442 S = new (Context) ObjCAtTryStmt(Empty);
3443 break;
3444 case pch::STMT_OBJC_AT_SYNCHRONIZED:
3445 S = new (Context) ObjCAtSynchronizedStmt(Empty);
3446 break;
3447 case pch::STMT_OBJC_AT_THROW:
3448 S = new (Context) ObjCAtThrowStmt(Empty);
3449 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003450 }
3451
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003452 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003453 if (Finished)
3454 break;
3455
Douglas Gregor456e0952009-04-17 22:13:46 +00003456 ++NumStatementsRead;
3457
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003458 if (S) {
3459 unsigned NumSubStmts = Reader.Visit(S);
3460 while (NumSubStmts > 0) {
3461 StmtStack.pop_back();
3462 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003463 }
3464 }
3465
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003466 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003467 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003468 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003469 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003470 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003471 return StmtStack.back();
3472}
3473
3474Expr *PCHReader::ReadExpr() {
3475 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003476}
3477
Douglas Gregor179cfb12009-04-10 20:39:37 +00003478DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003479 return Diag(SourceLocation(), DiagID);
3480}
3481
3482DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3483 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003484 Context.getSourceManager()),
3485 DiagID);
3486}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003487
Douglas Gregorc713da92009-04-21 22:25:48 +00003488/// \brief Retrieve the identifier table associated with the
3489/// preprocessor.
3490IdentifierTable &PCHReader::getIdentifierTable() {
3491 return PP.getIdentifierTable();
3492}
3493
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003494/// \brief Record that the given ID maps to the given switch-case
3495/// statement.
3496void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3497 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3498 SwitchCaseStmts[ID] = SC;
3499}
3500
3501/// \brief Retrieve the switch-case statement with the given ID.
3502SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3503 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3504 return SwitchCaseStmts[ID];
3505}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003506
3507/// \brief Record that the given label statement has been
3508/// deserialized and has the given ID.
3509void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3510 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3511 "Deserialized label twice");
3512 LabelStmts[ID] = S;
3513
3514 // If we've already seen any goto statements that point to this
3515 // label, resolve them now.
3516 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3517 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3518 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3519 Goto->second->setLabel(S);
3520 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003521
3522 // If we've already seen any address-label statements that point to
3523 // this label, resolve them now.
3524 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3525 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3526 = UnresolvedAddrLabelExprs.equal_range(ID);
3527 for (AddrLabelIter AddrLabel = AddrLabels.first;
3528 AddrLabel != AddrLabels.second; ++AddrLabel)
3529 AddrLabel->second->setLabel(S);
3530 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003531}
3532
3533/// \brief Set the label of the given statement to the label
3534/// identified by ID.
3535///
3536/// Depending on the order in which the label and other statements
3537/// referencing that label occur, this operation may complete
3538/// immediately (updating the statement) or it may queue the
3539/// statement to be back-patched later.
3540void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3541 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3542 if (Label != LabelStmts.end()) {
3543 // We've already seen this label, so set the label of the goto and
3544 // we're done.
3545 S->setLabel(Label->second);
3546 } else {
3547 // We haven't seen this label yet, so add this goto to the set of
3548 // unresolved goto statements.
3549 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3550 }
3551}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003552
3553/// \brief Set the label of the given expression to the label
3554/// identified by ID.
3555///
3556/// Depending on the order in which the label and other statements
3557/// referencing that label occur, this operation may complete
3558/// immediately (updating the statement) or it may queue the
3559/// statement to be back-patched later.
3560void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3561 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3562 if (Label != LabelStmts.end()) {
3563 // We've already seen this label, so set the label of the
3564 // label-address expression and we're done.
3565 S->setLabel(Label->second);
3566 } else {
3567 // We haven't seen this label yet, so add this label-address
3568 // expression to the set of unresolved label-address expressions.
3569 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3570 }
3571}