blob: 9970eb2b0905617ab9f6da0bccc1bbcbb006671f [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregorfdd01722009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-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 Gregor37e26842009-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 Gregor2cf26342009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-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 Gregor0a2b45e2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000085 void VisitParmVarDecl(ParmVarDecl *PD);
86 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000087 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
88 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000089 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff53c9d8a2009-04-20 15:06:07 +000090 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff33feeb02009-04-20 20:09:33 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *D);
92 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
93 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff30833f82009-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 Gregor2cf26342009-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 Gregor68a2eb02009-04-15 21:30:51 +0000114 if (Record[Idx++])
115 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +0000131 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
132}
133
134void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-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 Gregor2cf26342009-04-09 22:27:44 +0000142}
143
Douglas Gregor0a2b45e2009-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 Gregor8c700062009-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 Gregor2cf26342009-04-09 22:27:44 +0000163void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
164 VisitNamedDecl(VD);
165 VD->setType(Reader.GetType(Record[Idx++]));
166}
167
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000168void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
169 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000170 if (Record[Idx++])
171 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000172 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
173}
174
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000175void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
176 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000177 if (Record[Idx++])
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000178 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor3a2f7e42009-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 Gregorb3efa982009-04-23 18:22:55 +0000183 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor3a2f7e42009-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 Naroff53c9d8a2009-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 Naroff33feeb02009-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 Lattner4dcf151a2009-04-22 05:57:30 +0000230 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
231 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor291be392009-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 Gregord5e662d2009-04-24 22:01:00 +0000237 ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext());
Steve Naroff33feeb02009-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 Gregor133f4822009-04-23 22:34:55 +0000244 ID->setCategoryList(
245 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff33feeb02009-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 Lattner4dcf151a2009-04-22 05:57:30 +0000250 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff33feeb02009-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 Naroff30833f82009-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 Naroff07772602009-04-24 16:59:10 +0000303 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-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 Gregor70e5a142009-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 Naroff30833f82009-04-21 15:12:33 +0000329}
330
331void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000332 VisitNamedDecl(D);
Douglas Gregor2c2d43c2009-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 Naroff30833f82009-04-21 15:12:33 +0000336}
337
338void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
339 VisitObjCImplDecl(D);
Douglas Gregor10b0e1f2009-04-23 02:53:57 +0000340 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff30833f82009-04-21 15:12:33 +0000341}
342
343void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
344 VisitObjCImplDecl(D);
Douglas Gregor8f36aba2009-04-23 03:23:08 +0000345 D->setSuperClass(
346 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff30833f82009-04-21 15:12:33 +0000347}
348
349
350void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
351 VisitDecl(D);
Douglas Gregor8818c4f2009-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 Naroff30833f82009-04-21 15:12:33 +0000357}
358
Douglas Gregor8c700062009-04-13 21:20:57 +0000359void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
360 VisitValueDecl(FD);
361 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000364}
365
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000375 if (Record[Idx++])
376 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000377}
378
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000379void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
380 VisitVarDecl(PD);
381 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000382 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-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 Gregor1028bc62009-04-13 22:49:25 +0000390void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
391 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000392 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000393}
394
395void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
396 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000397 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-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 Gregor2cf26342009-04-09 22:27:44 +0000406std::pair<uint64_t, uint64_t>
407PCHDeclReader::VisitDeclContext(DeclContext *DC) {
408 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor0af2ca42009-04-22 19:09:20 +0000409 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000410 return std::make_pair(LexicalOffset, VisibleOffset);
411}
412
Douglas Gregor0b748912009-04-14 21:18:50 +0000413//===----------------------------------------------------------------------===//
414// Statement/expression deserialization
415//===----------------------------------------------------------------------===//
416namespace {
417 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000418 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000419 PCHReader &Reader;
420 const PCHReader::RecordData &Record;
421 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000422 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000423
424 public:
425 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000426 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
427 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000428
Douglas Gregor025452f2009-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 Gregor673ecd62009-04-15 16:35:07 +0000433 /// \brief The number of record fields required for the Expr class
434 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000435 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000436
Douglas Gregor087fd532009-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 Gregor025452f2009-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 Gregor1de05fe2009-04-17 18:18:49 +0000448 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000449 unsigned VisitIfStmt(IfStmt *S);
450 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000451 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000452 unsigned VisitDoStmt(DoStmt *S);
453 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000454 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000455 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000456 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000457 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000458 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000459 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000460 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-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 Gregorcb2ca732009-04-15 22:19:53 +0000466 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000467 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000468 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000469 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000470 unsigned VisitUnaryOperator(UnaryOperator *E);
471 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000472 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000473 unsigned VisitCallExpr(CallExpr *E);
474 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000475 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000476 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000477 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000479 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000480 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
481 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000482 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000483 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000484 unsigned VisitInitListExpr(InitListExpr *E);
485 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000487 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000488 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000489 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000490 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
491 unsigned VisitChooseExpr(ChooseExpr *E);
492 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000493 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000494 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000495 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000496 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000497 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattner3a57a372009-04-22 06:29:42 +0000498 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
499 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattner0389e6b2009-04-26 00:44:05 +0000500 unsigned VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
501 unsigned VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
502 unsigned VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Naroffc4f0bbd2009-04-25 14:04:28 +0000503 unsigned VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattner0389e6b2009-04-26 00:44:05 +0000504 unsigned VisitObjCSuperExpr(ObjCSuperExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000505 };
506}
507
Douglas Gregor025452f2009-04-17 00:04:06 +0000508unsigned PCHStmtReader::VisitStmt(Stmt *S) {
509 assert(Idx == NumStmtFields && "Incorrect statement field count");
510 return 0;
511}
512
513unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
514 VisitStmt(S);
515 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
516 return 0;
517}
518
519unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
520 VisitStmt(S);
521 unsigned NumStmts = Record[Idx++];
522 S->setStmts(Reader.getContext(),
523 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
524 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
525 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
526 return NumStmts;
527}
528
529unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
530 VisitStmt(S);
531 Reader.RecordSwitchCaseID(S, Record[Idx++]);
532 return 0;
533}
534
535unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
536 VisitSwitchCase(S);
537 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
538 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
539 S->setSubStmt(StmtStack.back());
540 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
541 return 3;
542}
543
544unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
545 VisitSwitchCase(S);
546 S->setSubStmt(StmtStack.back());
547 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 return 1;
549}
550
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000551unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
552 VisitStmt(S);
553 S->setID(Reader.GetIdentifierInfo(Record, Idx));
554 S->setSubStmt(StmtStack.back());
555 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 Reader.RecordLabelStmt(S, Record[Idx++]);
557 return 1;
558}
559
Douglas Gregor025452f2009-04-17 00:04:06 +0000560unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
561 VisitStmt(S);
562 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
563 S->setThen(StmtStack[StmtStack.size() - 2]);
564 S->setElse(StmtStack[StmtStack.size() - 1]);
565 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 return 3;
567}
568
569unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
570 VisitStmt(S);
571 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
572 S->setBody(StmtStack.back());
573 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
574 SwitchCase *PrevSC = 0;
575 for (unsigned N = Record.size(); Idx != N; ++Idx) {
576 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
577 if (PrevSC)
578 PrevSC->setNextSwitchCase(SC);
579 else
580 S->setSwitchCaseList(SC);
581 PrevSC = SC;
582 }
583 return 2;
584}
585
Douglas Gregord921cf92009-04-17 00:16:09 +0000586unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
587 VisitStmt(S);
588 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
589 S->setBody(StmtStack.back());
590 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
591 return 2;
592}
593
Douglas Gregor67d82492009-04-17 00:29:51 +0000594unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
595 VisitStmt(S);
596 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
597 S->setBody(StmtStack.back());
598 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
599 return 2;
600}
601
602unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
603 VisitStmt(S);
604 S->setInit(StmtStack[StmtStack.size() - 4]);
605 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
606 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
607 S->setBody(StmtStack.back());
608 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
609 return 4;
610}
611
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000612unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
613 VisitStmt(S);
614 Reader.SetLabelOf(S, Record[Idx++]);
615 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
617 return 0;
618}
619
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000620unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
621 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000622 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000623 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
624 return 1;
625}
626
Douglas Gregord921cf92009-04-17 00:16:09 +0000627unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
628 VisitStmt(S);
629 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630 return 0;
631}
632
Douglas Gregor025452f2009-04-17 00:04:06 +0000633unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
634 VisitStmt(S);
635 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
636 return 0;
637}
638
Douglas Gregor0de9d882009-04-17 16:34:57 +0000639unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
640 VisitStmt(S);
641 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
642 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643 return 1;
644}
645
Douglas Gregor84f21702009-04-17 16:55:36 +0000646unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
647 VisitStmt(S);
648 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
649 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
650
651 if (Idx + 1 == Record.size()) {
652 // Single declaration
653 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
654 } else {
655 llvm::SmallVector<Decl *, 16> Decls;
656 Decls.reserve(Record.size() - Idx);
657 for (unsigned N = Record.size(); Idx != N; ++Idx)
658 Decls.push_back(Reader.GetDecl(Record[Idx]));
659 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
660 &Decls[0], Decls.size())));
661 }
662 return 0;
663}
664
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000665unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
666 VisitStmt(S);
667 unsigned NumOutputs = Record[Idx++];
668 unsigned NumInputs = Record[Idx++];
669 unsigned NumClobbers = Record[Idx++];
670 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
671 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
672 S->setVolatile(Record[Idx++]);
673 S->setSimple(Record[Idx++]);
674
675 unsigned StackIdx
676 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
677 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
678
679 // Outputs and inputs
680 llvm::SmallVector<std::string, 16> Names;
681 llvm::SmallVector<StringLiteral*, 16> Constraints;
682 llvm::SmallVector<Stmt*, 16> Exprs;
683 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
684 Names.push_back(Reader.ReadString(Record, Idx));
685 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
686 Exprs.push_back(StmtStack[StackIdx++]);
687 }
688 S->setOutputsAndInputs(NumOutputs, NumInputs,
689 &Names[0], &Constraints[0], &Exprs[0]);
690
691 // Constraints
692 llvm::SmallVector<StringLiteral*, 16> Clobbers;
693 for (unsigned I = 0; I != NumClobbers; ++I)
694 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
695 S->setClobbers(&Clobbers[0], NumClobbers);
696
697 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
698 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
699}
700
Douglas Gregor087fd532009-04-14 23:32:43 +0000701unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000702 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000703 E->setType(Reader.GetType(Record[Idx++]));
704 E->setTypeDependent(Record[Idx++]);
705 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000706 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000707 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000708}
709
Douglas Gregor087fd532009-04-14 23:32:43 +0000710unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000711 VisitExpr(E);
712 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
713 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000714 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000715}
716
Douglas Gregor087fd532009-04-14 23:32:43 +0000717unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000718 VisitExpr(E);
719 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
720 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000721 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000722}
723
Douglas Gregor087fd532009-04-14 23:32:43 +0000724unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000725 VisitExpr(E);
726 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
727 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000728 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000729}
730
Douglas Gregor087fd532009-04-14 23:32:43 +0000731unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000732 VisitExpr(E);
733 E->setValue(Reader.ReadAPFloat(Record, Idx));
734 E->setExact(Record[Idx++]);
735 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000736 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000737}
738
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000739unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
740 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000741 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000742 return 1;
743}
744
Douglas Gregor673ecd62009-04-15 16:35:07 +0000745unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
746 VisitExpr(E);
747 unsigned Len = Record[Idx++];
748 assert(Record[Idx] == E->getNumConcatenated() &&
749 "Wrong number of concatenated tokens!");
750 ++Idx;
751 E->setWide(Record[Idx++]);
752
753 // Read string data
754 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
755 E->setStrData(Reader.getContext(), &Str[0], Len);
756 Idx += Len;
757
758 // Read source locations
759 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
760 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
761
762 return 0;
763}
764
Douglas Gregor087fd532009-04-14 23:32:43 +0000765unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000766 VisitExpr(E);
767 E->setValue(Record[Idx++]);
768 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
769 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000770 return 0;
771}
772
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000773unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
774 VisitExpr(E);
775 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
776 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000777 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000778 return 1;
779}
780
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000781unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
782 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000783 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000784 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
785 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
786 return 1;
787}
788
789unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
790 VisitExpr(E);
791 E->setSizeof(Record[Idx++]);
792 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000793 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000794 ++Idx;
795 } else {
796 E->setArgument(Reader.GetType(Record[Idx++]));
797 }
798 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
799 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
800 return E->isArgumentType()? 0 : 1;
801}
802
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000803unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
804 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000805 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Steve Naroffd3f632e2009-04-25 15:19:54 +0000806 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000807 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
808 return 2;
809}
810
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000811unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
812 VisitExpr(E);
813 E->setNumArgs(Reader.getContext(), Record[Idx++]);
814 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000815 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000816 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000817 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000818 return E->getNumArgs() + 1;
819}
820
821unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
822 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000823 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000824 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
825 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
826 E->setArrow(Record[Idx++]);
827 return 1;
828}
829
Douglas Gregor087fd532009-04-14 23:32:43 +0000830unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
831 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000832 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000833 return 1;
834}
835
Douglas Gregordb600c32009-04-15 00:25:59 +0000836unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
837 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000838 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
839 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000840 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
841 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
842 return 2;
843}
844
Douglas Gregorad90e962009-04-15 22:40:36 +0000845unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
846 VisitBinaryOperator(E);
847 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
848 E->setComputationResultType(Reader.GetType(Record[Idx++]));
849 return 2;
850}
851
852unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
853 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000854 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
855 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
856 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000857 return 3;
858}
859
Douglas Gregor087fd532009-04-14 23:32:43 +0000860unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
861 VisitCastExpr(E);
862 E->setLvalueCast(Record[Idx++]);
863 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000864}
865
Douglas Gregordb600c32009-04-15 00:25:59 +0000866unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
867 VisitCastExpr(E);
868 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
869 return 1;
870}
871
872unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
873 VisitExplicitCastExpr(E);
874 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
875 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
876 return 1;
877}
878
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000879unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
880 VisitExpr(E);
881 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000882 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000883 E->setFileScope(Record[Idx++]);
884 return 1;
885}
886
Douglas Gregord3c98a02009-04-15 23:02:49 +0000887unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
888 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000889 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000890 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
891 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
892 return 1;
893}
894
Douglas Gregord077d752009-04-16 00:55:48 +0000895unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
896 VisitExpr(E);
897 unsigned NumInits = Record[Idx++];
898 E->reserveInits(NumInits);
899 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000900 E->updateInit(I,
901 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
902 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000903 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
904 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
905 E->setInitializedFieldInUnion(
906 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
907 E->sawArrayRangeDesignator(Record[Idx++]);
908 return NumInits + 1;
909}
910
911unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
912 typedef DesignatedInitExpr::Designator Designator;
913
914 VisitExpr(E);
915 unsigned NumSubExprs = Record[Idx++];
916 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
917 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000918 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000919 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
920 E->setGNUSyntax(Record[Idx++]);
921
922 llvm::SmallVector<Designator, 4> Designators;
923 while (Idx < Record.size()) {
924 switch ((pch::DesignatorTypes)Record[Idx++]) {
925 case pch::DESIG_FIELD_DECL: {
926 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
927 SourceLocation DotLoc
928 = SourceLocation::getFromRawEncoding(Record[Idx++]);
929 SourceLocation FieldLoc
930 = SourceLocation::getFromRawEncoding(Record[Idx++]);
931 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
932 FieldLoc));
933 Designators.back().setField(Field);
934 break;
935 }
936
937 case pch::DESIG_FIELD_NAME: {
938 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
939 SourceLocation DotLoc
940 = SourceLocation::getFromRawEncoding(Record[Idx++]);
941 SourceLocation FieldLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
944 break;
945 }
946
947 case pch::DESIG_ARRAY: {
948 unsigned Index = Record[Idx++];
949 SourceLocation LBracketLoc
950 = SourceLocation::getFromRawEncoding(Record[Idx++]);
951 SourceLocation RBracketLoc
952 = SourceLocation::getFromRawEncoding(Record[Idx++]);
953 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
954 break;
955 }
956
957 case pch::DESIG_ARRAY_RANGE: {
958 unsigned Index = Record[Idx++];
959 SourceLocation LBracketLoc
960 = SourceLocation::getFromRawEncoding(Record[Idx++]);
961 SourceLocation EllipsisLoc
962 = SourceLocation::getFromRawEncoding(Record[Idx++]);
963 SourceLocation RBracketLoc
964 = SourceLocation::getFromRawEncoding(Record[Idx++]);
965 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
966 RBracketLoc));
967 break;
968 }
969 }
970 }
971 E->setDesignators(&Designators[0], Designators.size());
972
973 return NumSubExprs;
974}
975
976unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
977 VisitExpr(E);
978 return 0;
979}
980
Douglas Gregord3c98a02009-04-15 23:02:49 +0000981unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
982 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000983 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000984 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
985 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 return 1;
987}
988
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000989unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
990 VisitExpr(E);
991 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 Reader.SetLabelOf(E, Record[Idx++]);
994 return 0;
995}
996
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000997unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
998 VisitExpr(E);
999 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1000 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1001 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
1002 return 1;
1003}
1004
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001005unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1006 VisitExpr(E);
1007 E->setArgType1(Reader.GetType(Record[Idx++]));
1008 E->setArgType2(Reader.GetType(Record[Idx++]));
1009 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1010 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1011 return 0;
1012}
1013
1014unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1015 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +00001016 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1017 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1018 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +00001019 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1020 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1021 return 3;
1022}
1023
1024unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1025 VisitExpr(E);
1026 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 return 0;
1028}
Douglas Gregord3c98a02009-04-15 23:02:49 +00001029
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001030unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1031 VisitExpr(E);
1032 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +00001033 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001034 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1035 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1036 return NumExprs;
1037}
1038
Douglas Gregor84af7c22009-04-17 19:21:43 +00001039unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1040 VisitExpr(E);
1041 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1042 E->setHasBlockDeclRefExprs(Record[Idx++]);
1043 return 0;
1044}
1045
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001046unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1047 VisitExpr(E);
1048 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1049 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1050 E->setByRef(Record[Idx++]);
1051 return 0;
1052}
1053
Chris Lattner3a57a372009-04-22 06:29:42 +00001054//===----------------------------------------------------------------------===//
1055// Objective-C Expressions and Statements
1056
1057unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1058 VisitExpr(E);
1059 E->setString(cast<StringLiteral>(StmtStack.back()));
1060 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1061 return 1;
1062}
1063
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001064unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1065 VisitExpr(E);
1066 E->setEncodedType(Reader.GetType(Record[Idx++]));
1067 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1069 return 0;
1070}
1071
Chris Lattner3a57a372009-04-22 06:29:42 +00001072unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1073 VisitExpr(E);
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001074 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattner3a57a372009-04-22 06:29:42 +00001075 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1076 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1077 return 0;
1078}
1079
1080unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1081 VisitExpr(E);
1082 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1083 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1084 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1085 return 0;
1086}
1087
Chris Lattner0389e6b2009-04-26 00:44:05 +00001088unsigned PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1089 VisitExpr(E);
1090 E->setDecl(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
1091 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1092 E->setBase(cast<Expr>(StmtStack.back()));
1093 E->setIsArrow(Record[Idx++]);
1094 E->setIsFreeIvar(Record[Idx++]);
1095 return 1;
1096}
1097
1098unsigned PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1099 VisitExpr(E);
1100 E->setProperty(cast<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
1101 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1102 E->setBase(cast<Expr>(StmtStack.back()));
1103 return 1;
1104}
1105
1106unsigned PCHStmtReader::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1107 VisitExpr(E);
1108 E->setGetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1109 E->setSetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1110 E->setClassProp(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
1111 E->setBase(cast<Expr>(StmtStack.back()));
1112 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1113 E->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1114 return 1;
1115}
1116
Steve Naroffc4f0bbd2009-04-25 14:04:28 +00001117unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1118 VisitExpr(E);
1119 E->setNumArgs(Record[Idx++]);
Chris Lattner0389e6b2009-04-26 00:44:05 +00001120 E->setLeftLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1121 E->setRightLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroffc4f0bbd2009-04-25 14:04:28 +00001122 E->setSelector(Reader.GetSelector(Record, Idx));
1123 E->setMethodDecl(cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner0389e6b2009-04-26 00:44:05 +00001124
1125 ObjCMessageExpr::ClassInfo CI;
1126 CI.first = cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]));
1127 CI.second = Reader.GetIdentifierInfo(Record, Idx);
1128 if (E->getMethodDecl() == 0)
1129 E->setClassInfo(CI);
1130
Steve Naroffc4f0bbd2009-04-25 14:04:28 +00001131 E->setReceiver(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
1132 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1133 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
1134 return E->getNumArgs() + 1;
1135}
1136
Chris Lattner0389e6b2009-04-26 00:44:05 +00001137unsigned PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1138 VisitExpr(E);
1139 E->setLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1140 return 0;
1141}
Chris Lattner4dcf151a2009-04-22 05:57:30 +00001142
Douglas Gregor668c1a42009-04-21 22:25:48 +00001143//===----------------------------------------------------------------------===//
1144// PCH reader implementation
1145//===----------------------------------------------------------------------===//
1146
1147namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001148class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1149 PCHReader &Reader;
1150
1151public:
1152 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1153
1154 typedef Selector external_key_type;
1155 typedef external_key_type internal_key_type;
1156
1157 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1158
1159 static bool EqualKey(const internal_key_type& a,
1160 const internal_key_type& b) {
1161 return a == b;
1162 }
1163
1164 static unsigned ComputeHash(Selector Sel) {
1165 unsigned N = Sel.getNumArgs();
1166 if (N == 0)
1167 ++N;
1168 unsigned R = 5381;
1169 for (unsigned I = 0; I != N; ++I)
1170 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1171 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1172 return R;
1173 }
1174
1175 // This hopefully will just get inlined and removed by the optimizer.
1176 static const internal_key_type&
1177 GetInternalKey(const external_key_type& x) { return x; }
1178
1179 static std::pair<unsigned, unsigned>
1180 ReadKeyDataLength(const unsigned char*& d) {
1181 using namespace clang::io;
1182 unsigned KeyLen = ReadUnalignedLE16(d);
1183 unsigned DataLen = ReadUnalignedLE16(d);
1184 return std::make_pair(KeyLen, DataLen);
1185 }
1186
Douglas Gregor83941df2009-04-25 17:48:32 +00001187 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001188 using namespace clang::io;
1189 SelectorTable &SelTable = Reader.getContext().Selectors;
1190 unsigned N = ReadUnalignedLE16(d);
1191 IdentifierInfo *FirstII
1192 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1193 if (N == 0)
1194 return SelTable.getNullarySelector(FirstII);
1195 else if (N == 1)
1196 return SelTable.getUnarySelector(FirstII);
1197
1198 llvm::SmallVector<IdentifierInfo *, 16> Args;
1199 Args.push_back(FirstII);
1200 for (unsigned I = 1; I != N; ++I)
1201 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1202
1203 return SelTable.getSelector(N, &Args[0]);
1204 }
1205
1206 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1207 using namespace clang::io;
1208 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1209 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1210
1211 data_type Result;
1212
1213 // Load instance methods
1214 ObjCMethodList *Prev = 0;
1215 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1216 ObjCMethodDecl *Method
1217 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1218 if (!Result.first.Method) {
1219 // This is the first method, which is the easy case.
1220 Result.first.Method = Method;
1221 Prev = &Result.first;
1222 continue;
1223 }
1224
1225 Prev->Next = new ObjCMethodList(Method, 0);
1226 Prev = Prev->Next;
1227 }
1228
1229 // Load factory methods
1230 Prev = 0;
1231 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1232 ObjCMethodDecl *Method
1233 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1234 if (!Result.second.Method) {
1235 // This is the first method, which is the easy case.
1236 Result.second.Method = Method;
1237 Prev = &Result.second;
1238 continue;
1239 }
1240
1241 Prev->Next = new ObjCMethodList(Method, 0);
1242 Prev = Prev->Next;
1243 }
1244
1245 return Result;
1246 }
1247};
1248
1249} // end anonymous namespace
1250
1251/// \brief The on-disk hash table used for the global method pool.
1252typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1253 PCHMethodPoolLookupTable;
1254
1255namespace {
Douglas Gregor668c1a42009-04-21 22:25:48 +00001256class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1257 PCHReader &Reader;
1258
1259 // If we know the IdentifierInfo in advance, it is here and we will
1260 // not build a new one. Used when deserializing information about an
1261 // identifier that was constructed before the PCH file was read.
1262 IdentifierInfo *KnownII;
1263
1264public:
1265 typedef IdentifierInfo * data_type;
1266
1267 typedef const std::pair<const char*, unsigned> external_key_type;
1268
1269 typedef external_key_type internal_key_type;
1270
1271 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1272 : Reader(Reader), KnownII(II) { }
1273
1274 static bool EqualKey(const internal_key_type& a,
1275 const internal_key_type& b) {
1276 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1277 : false;
1278 }
1279
1280 static unsigned ComputeHash(const internal_key_type& a) {
1281 return BernsteinHash(a.first, a.second);
1282 }
1283
1284 // This hopefully will just get inlined and removed by the optimizer.
1285 static const internal_key_type&
1286 GetInternalKey(const external_key_type& x) { return x; }
1287
1288 static std::pair<unsigned, unsigned>
1289 ReadKeyDataLength(const unsigned char*& d) {
1290 using namespace clang::io;
Douglas Gregor5f8e3302009-04-25 20:26:24 +00001291 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregord6595a42009-04-25 21:04:17 +00001292 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001293 return std::make_pair(KeyLen, DataLen);
1294 }
1295
1296 static std::pair<const char*, unsigned>
1297 ReadKey(const unsigned char* d, unsigned n) {
1298 assert(n >= 2 && d[n-1] == '\0');
1299 return std::make_pair((const char*) d, n-1);
1300 }
1301
1302 IdentifierInfo *ReadData(const internal_key_type& k,
1303 const unsigned char* d,
1304 unsigned DataLen) {
1305 using namespace clang::io;
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001306 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001307 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1308 Bits >>= 1;
1309 bool Poisoned = Bits & 0x01;
1310 Bits >>= 1;
1311 bool ExtensionToken = Bits & 0x01;
1312 Bits >>= 1;
1313 bool hasMacroDefinition = Bits & 0x01;
1314 Bits >>= 1;
1315 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1316 Bits >>= 10;
1317 unsigned TokenID = Bits & 0xFF;
1318 Bits >>= 8;
1319
Douglas Gregor668c1a42009-04-21 22:25:48 +00001320 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregor2deaea32009-04-22 18:49:13 +00001321 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregor668c1a42009-04-21 22:25:48 +00001322 DataLen -= 8;
1323
1324 // Build the IdentifierInfo itself and link the identifier ID with
1325 // the new IdentifierInfo.
1326 IdentifierInfo *II = KnownII;
1327 if (!II)
Douglas Gregor5f8e3302009-04-25 20:26:24 +00001328 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1329 k.first, k.first + k.second);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001330 Reader.SetIdentifierInfo(ID, II);
1331
Douglas Gregor2deaea32009-04-22 18:49:13 +00001332 // Set or check the various bits in the IdentifierInfo structure.
1333 // FIXME: Load token IDs lazily, too?
1334 assert((unsigned)II->getTokenID() == TokenID &&
1335 "Incorrect token ID loaded");
1336 (void)TokenID;
1337 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1338 assert(II->isExtensionToken() == ExtensionToken &&
1339 "Incorrect extension token flag");
1340 (void)ExtensionToken;
1341 II->setIsPoisoned(Poisoned);
1342 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1343 "Incorrect C++ operator keyword flag");
1344 (void)CPlusPlusOperatorKeyword;
1345
Douglas Gregor37e26842009-04-21 23:56:24 +00001346 // If this identifier is a macro, deserialize the macro
1347 // definition.
1348 if (hasMacroDefinition) {
1349 uint32_t Offset = ReadUnalignedLE64(d);
1350 Reader.ReadMacroRecord(Offset);
1351 DataLen -= 8;
1352 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001353
1354 // Read all of the declarations visible at global scope with this
1355 // name.
1356 Sema *SemaObj = Reader.getSema();
1357 while (DataLen > 0) {
1358 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001359 if (SemaObj) {
1360 // Introduce this declaration into the translation-unit scope
1361 // and add it to the declaration chain for this identifier, so
1362 // that (unqualified) name lookup will find it.
1363 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1364 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1365 } else {
1366 // Queue this declaration so that it will be added to the
1367 // translation unit scope and identifier's declaration chain
1368 // once a Sema object is known.
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00001369 Reader.PreloadedDecls.push_back(D);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001370 }
1371
1372 DataLen -= 4;
1373 }
1374 return II;
1375 }
1376};
1377
1378} // end anonymous namespace
1379
1380/// \brief The on-disk hash table used to contain information about
1381/// all of the identifiers in the program.
1382typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1383 PCHIdentifierLookupTable;
1384
Douglas Gregor2cf26342009-04-09 22:27:44 +00001385// FIXME: use the diagnostics machinery
1386static bool Error(const char *Str) {
1387 std::fprintf(stderr, "%s\n", Str);
1388 return true;
1389}
1390
Douglas Gregore1d918e2009-04-10 23:10:45 +00001391/// \brief Check the contents of the predefines buffer against the
1392/// contents of the predefines buffer used to build the PCH file.
1393///
1394/// The contents of the two predefines buffers should be the same. If
1395/// not, then some command-line option changed the preprocessor state
1396/// and we must reject the PCH file.
1397///
1398/// \param PCHPredef The start of the predefines buffer in the PCH
1399/// file.
1400///
1401/// \param PCHPredefLen The length of the predefines buffer in the PCH
1402/// file.
1403///
1404/// \param PCHBufferID The FileID for the PCH predefines buffer.
1405///
1406/// \returns true if there was a mismatch (in which case the PCH file
1407/// should be ignored), or false otherwise.
1408bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1409 unsigned PCHPredefLen,
1410 FileID PCHBufferID) {
1411 const char *Predef = PP.getPredefines().c_str();
1412 unsigned PredefLen = PP.getPredefines().size();
1413
1414 // If the two predefines buffers compare equal, we're done!.
1415 if (PredefLen == PCHPredefLen &&
1416 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1417 return false;
1418
1419 // The predefines buffers are different. Produce a reasonable
1420 // diagnostic showing where they are different.
1421
1422 // The source locations (potentially in the two different predefines
1423 // buffers)
1424 SourceLocation Loc1, Loc2;
1425 SourceManager &SourceMgr = PP.getSourceManager();
1426
1427 // Create a source buffer for our predefines string, so
1428 // that we can build a diagnostic that points into that
1429 // source buffer.
1430 FileID BufferID;
1431 if (Predef && Predef[0]) {
1432 llvm::MemoryBuffer *Buffer
1433 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1434 "<built-in>");
1435 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1436 }
1437
1438 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1439 std::pair<const char *, const char *> Locations
1440 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1441
1442 if (Locations.first != Predef + MinLen) {
1443 // We found the location in the two buffers where there is a
1444 // difference. Form source locations to point there (in both
1445 // buffers).
1446 unsigned Offset = Locations.first - Predef;
1447 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1448 .getFileLocWithOffset(Offset);
1449 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1450 .getFileLocWithOffset(Offset);
1451 } else if (PredefLen > PCHPredefLen) {
1452 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1453 .getFileLocWithOffset(MinLen);
1454 } else {
1455 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1456 .getFileLocWithOffset(MinLen);
1457 }
1458
1459 Diag(Loc1, diag::warn_pch_preprocessor);
1460 if (Loc2.isValid())
1461 Diag(Loc2, diag::note_predef_in_pch);
1462 Diag(diag::note_ignoring_pch) << FileName;
1463 return true;
1464}
1465
Douglas Gregorbd945002009-04-13 16:31:14 +00001466/// \brief Read the line table in the source manager block.
1467/// \returns true if ther was an error.
1468static bool ParseLineTable(SourceManager &SourceMgr,
1469 llvm::SmallVectorImpl<uint64_t> &Record) {
1470 unsigned Idx = 0;
1471 LineTableInfo &LineTable = SourceMgr.getLineTable();
1472
1473 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +00001474 std::map<int, int> FileIDs;
1475 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +00001476 // Extract the file name
1477 unsigned FilenameLen = Record[Idx++];
1478 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1479 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +00001480 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1481 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +00001482 }
1483
1484 // Parse the line entries
1485 std::vector<LineEntry> Entries;
1486 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +00001487 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +00001488
1489 // Extract the line entries
1490 unsigned NumEntries = Record[Idx++];
1491 Entries.clear();
1492 Entries.reserve(NumEntries);
1493 for (unsigned I = 0; I != NumEntries; ++I) {
1494 unsigned FileOffset = Record[Idx++];
1495 unsigned LineNo = Record[Idx++];
1496 int FilenameID = Record[Idx++];
1497 SrcMgr::CharacteristicKind FileKind
1498 = (SrcMgr::CharacteristicKind)Record[Idx++];
1499 unsigned IncludeOffset = Record[Idx++];
1500 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1501 FileKind, IncludeOffset));
1502 }
1503 LineTable.AddEntry(FID, Entries);
1504 }
1505
1506 return false;
1507}
1508
Douglas Gregor14f79002009-04-10 03:52:48 +00001509/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001510PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001511 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001512 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1513 Error("Malformed source manager block record");
1514 return Failure;
1515 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001516
1517 SourceManager &SourceMgr = Context.getSourceManager();
1518 RecordData Record;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001519 unsigned NumHeaderInfos = 0;
Douglas Gregor14f79002009-04-10 03:52:48 +00001520 while (true) {
1521 unsigned Code = Stream.ReadCode();
1522 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001523 if (Stream.ReadBlockEnd()) {
1524 Error("Error at end of Source Manager block");
1525 return Failure;
1526 }
1527
1528 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001529 }
1530
1531 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1532 // No known subblocks, always skip them.
1533 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001534 if (Stream.SkipBlock()) {
1535 Error("Malformed block record");
1536 return Failure;
1537 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001538 continue;
1539 }
1540
1541 if (Code == llvm::bitc::DEFINE_ABBREV) {
1542 Stream.ReadAbbrevRecord();
1543 continue;
1544 }
1545
1546 // Read a record.
1547 const char *BlobStart;
1548 unsigned BlobLen;
1549 Record.clear();
1550 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1551 default: // Default behavior: ignore.
1552 break;
1553
1554 case pch::SM_SLOC_FILE_ENTRY: {
1555 // FIXME: We would really like to delay the creation of this
1556 // FileEntry until it is actually required, e.g., when producing
1557 // a diagnostic with a source location in this file.
1558 const FileEntry *File
1559 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1560 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001561 FileID ID = SourceMgr.createFileID(File,
1562 SourceLocation::getFromRawEncoding(Record[1]),
1563 (CharacteristicKind)Record[2]);
1564 if (Record[3])
1565 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1566 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001567 break;
1568 }
1569
1570 case pch::SM_SLOC_BUFFER_ENTRY: {
1571 const char *Name = BlobStart;
1572 unsigned Code = Stream.ReadCode();
1573 Record.clear();
1574 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1575 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001576 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001577 llvm::MemoryBuffer *Buffer
1578 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1579 BlobStart + BlobLen - 1,
1580 Name);
1581 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1582
1583 if (strcmp(Name, "<built-in>") == 0
1584 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1585 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001586 break;
1587 }
1588
1589 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1590 SourceLocation SpellingLoc
1591 = SourceLocation::getFromRawEncoding(Record[1]);
1592 SourceMgr.createInstantiationLoc(
1593 SpellingLoc,
1594 SourceLocation::getFromRawEncoding(Record[2]),
1595 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001596 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001597 break;
1598 }
1599
Chris Lattner2c78b872009-04-14 23:22:57 +00001600 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001601 if (ParseLineTable(SourceMgr, Record))
1602 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001603 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001604
1605 case pch::SM_HEADER_FILE_INFO: {
1606 HeaderFileInfo HFI;
1607 HFI.isImport = Record[0];
1608 HFI.DirInfo = Record[1];
1609 HFI.NumIncludes = Record[2];
1610 HFI.ControllingMacroID = Record[3];
1611 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1612 break;
1613 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001614 }
1615 }
1616}
1617
Douglas Gregor37e26842009-04-21 23:56:24 +00001618void PCHReader::ReadMacroRecord(uint64_t Offset) {
1619 // Keep track of where we are in the stream, then jump back there
1620 // after reading this macro.
1621 SavedStreamPosition SavedPosition(Stream);
1622
1623 Stream.JumpToBit(Offset);
1624 RecordData Record;
1625 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1626 MacroInfo *Macro = 0;
Steve Naroff83d63c72009-04-24 20:03:17 +00001627
Douglas Gregor37e26842009-04-21 23:56:24 +00001628 while (true) {
1629 unsigned Code = Stream.ReadCode();
1630 switch (Code) {
1631 case llvm::bitc::END_BLOCK:
1632 return;
1633
1634 case llvm::bitc::ENTER_SUBBLOCK:
1635 // No known subblocks, always skip them.
1636 Stream.ReadSubBlockID();
1637 if (Stream.SkipBlock()) {
1638 Error("Malformed block record");
1639 return;
1640 }
1641 continue;
1642
1643 case llvm::bitc::DEFINE_ABBREV:
1644 Stream.ReadAbbrevRecord();
1645 continue;
1646 default: break;
1647 }
1648
1649 // Read a record.
1650 Record.clear();
1651 pch::PreprocessorRecordTypes RecType =
1652 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1653 switch (RecType) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001654 case pch::PP_MACRO_OBJECT_LIKE:
1655 case pch::PP_MACRO_FUNCTION_LIKE: {
1656 // If we already have a macro, that means that we've hit the end
1657 // of the definition of the macro we were looking for. We're
1658 // done.
1659 if (Macro)
1660 return;
1661
1662 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1663 if (II == 0) {
1664 Error("Macro must have a name");
1665 return;
1666 }
1667 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1668 bool isUsed = Record[2];
1669
1670 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1671 MI->setIsUsed(isUsed);
1672
1673 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1674 // Decode function-like macro info.
1675 bool isC99VarArgs = Record[3];
1676 bool isGNUVarArgs = Record[4];
1677 MacroArgs.clear();
1678 unsigned NumArgs = Record[5];
1679 for (unsigned i = 0; i != NumArgs; ++i)
1680 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1681
1682 // Install function-like macro info.
1683 MI->setIsFunctionLike();
1684 if (isC99VarArgs) MI->setIsC99Varargs();
1685 if (isGNUVarArgs) MI->setIsGNUVarargs();
1686 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1687 PP.getPreprocessorAllocator());
1688 }
1689
1690 // Finally, install the macro.
1691 PP.setMacroInfo(II, MI);
1692
1693 // Remember that we saw this macro last so that we add the tokens that
1694 // form its body to it.
1695 Macro = MI;
1696 ++NumMacrosRead;
1697 break;
1698 }
1699
1700 case pch::PP_TOKEN: {
1701 // If we see a TOKEN before a PP_MACRO_*, then the file is
1702 // erroneous, just pretend we didn't see this.
1703 if (Macro == 0) break;
1704
1705 Token Tok;
1706 Tok.startToken();
1707 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1708 Tok.setLength(Record[1]);
1709 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1710 Tok.setIdentifierInfo(II);
1711 Tok.setKind((tok::TokenKind)Record[3]);
1712 Tok.setFlag((Token::TokenFlags)Record[4]);
1713 Macro->AddTokenToBody(Tok);
1714 break;
1715 }
Steve Naroff83d63c72009-04-24 20:03:17 +00001716 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001717 }
1718}
1719
Douglas Gregor668c1a42009-04-21 22:25:48 +00001720PCHReader::PCHReadResult
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001721PCHReader::ReadPCHBlock() {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001722 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1723 Error("Malformed block record");
1724 return Failure;
1725 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001726
1727 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001728 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001729 while (!Stream.AtEndOfStream()) {
1730 unsigned Code = Stream.ReadCode();
1731 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001732 if (Stream.ReadBlockEnd()) {
1733 Error("Error at end of module block");
1734 return Failure;
1735 }
Chris Lattner7356a312009-04-11 21:15:38 +00001736
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001737 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001738 }
1739
1740 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1741 switch (Stream.ReadSubBlockID()) {
1742 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1743 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1744 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001745 if (Stream.SkipBlock()) {
1746 Error("Malformed block record");
1747 return Failure;
1748 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001749 break;
1750
Chris Lattner7356a312009-04-11 21:15:38 +00001751 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner7356a312009-04-11 21:15:38 +00001752 if (Stream.SkipBlock()) {
1753 Error("Malformed block record");
1754 return Failure;
1755 }
1756 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001757
Douglas Gregor14f79002009-04-10 03:52:48 +00001758 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001759 switch (ReadSourceManagerBlock()) {
1760 case Success:
1761 break;
1762
1763 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001764 Error("Malformed source manager block");
1765 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001766
1767 case IgnorePCH:
1768 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001769 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001770 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001771 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001772 continue;
1773 }
1774
1775 if (Code == llvm::bitc::DEFINE_ABBREV) {
1776 Stream.ReadAbbrevRecord();
1777 continue;
1778 }
1779
1780 // Read and process a record.
1781 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001782 const char *BlobStart = 0;
1783 unsigned BlobLen = 0;
1784 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1785 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001786 default: // Default behavior: ignore.
1787 break;
1788
1789 case pch::TYPE_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001790 if (!TypesLoaded.empty()) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001791 Error("Duplicate TYPE_OFFSET record in PCH file");
1792 return Failure;
1793 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001794 TypeOffsets = (const uint64_t *)BlobStart;
1795 TypesLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001796 break;
1797
1798 case pch::DECL_OFFSET:
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001799 if (!DeclsLoaded.empty()) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001800 Error("Duplicate DECL_OFFSET record in PCH file");
1801 return Failure;
1802 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00001803 DeclOffsets = (const uint64_t *)BlobStart;
1804 DeclsLoaded.resize(Record[0]);
Douglas Gregor8038d512009-04-10 17:25:41 +00001805 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001806
1807 case pch::LANGUAGE_OPTIONS:
1808 if (ParseLanguageOptions(Record))
1809 return IgnorePCH;
1810 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001811
Douglas Gregorafaf3082009-04-11 00:14:32 +00001812 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001813 std::string TargetTriple(BlobStart, BlobLen);
1814 if (TargetTriple != Context.Target.getTargetTriple()) {
1815 Diag(diag::warn_pch_target_triple)
1816 << TargetTriple << Context.Target.getTargetTriple();
1817 Diag(diag::note_ignoring_pch) << FileName;
1818 return IgnorePCH;
1819 }
1820 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001821 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001822
1823 case pch::IDENTIFIER_TABLE:
Douglas Gregor668c1a42009-04-21 22:25:48 +00001824 IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001825 if (Record[0]) {
1826 IdentifierLookupTable
1827 = PCHIdentifierLookupTable::Create(
Douglas Gregor668c1a42009-04-21 22:25:48 +00001828 (const unsigned char *)IdentifierTableData + Record[0],
1829 (const unsigned char *)IdentifierTableData,
1830 PCHIdentifierLookupTrait(*this));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001831 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1832 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001833 break;
1834
1835 case pch::IDENTIFIER_OFFSET:
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001836 if (!IdentifiersLoaded.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1838 return Failure;
1839 }
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001840 IdentifierOffsets = (const uint32_t *)BlobStart;
1841 IdentifiersLoaded.resize(Record[0]);
Douglas Gregor8c5a7602009-04-25 23:30:02 +00001842 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001843 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001844
1845 case pch::EXTERNAL_DEFINITIONS:
1846 if (!ExternalDefinitions.empty()) {
1847 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1848 return Failure;
1849 }
1850 ExternalDefinitions.swap(Record);
1851 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001852
Douglas Gregorad1de002009-04-18 05:55:16 +00001853 case pch::SPECIAL_TYPES:
1854 SpecialTypes.swap(Record);
1855 break;
1856
Douglas Gregor3e1af842009-04-17 22:13:46 +00001857 case pch::STATISTICS:
1858 TotalNumStatements = Record[0];
Douglas Gregor37e26842009-04-21 23:56:24 +00001859 TotalNumMacros = Record[1];
Douglas Gregor25123082009-04-22 22:34:57 +00001860 TotalLexicalDeclContexts = Record[2];
1861 TotalVisibleDeclContexts = Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001862 break;
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00001863 case pch::TENTATIVE_DEFINITIONS:
1864 if (!TentativeDefinitions.empty()) {
1865 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1866 return Failure;
1867 }
1868 TentativeDefinitions.swap(Record);
1869 break;
Douglas Gregor14c22f22009-04-22 22:18:58 +00001870
1871 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1872 if (!LocallyScopedExternalDecls.empty()) {
1873 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1874 return Failure;
1875 }
1876 LocallyScopedExternalDecls.swap(Record);
1877 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001878
Douglas Gregor83941df2009-04-25 17:48:32 +00001879 case pch::SELECTOR_OFFSETS:
1880 SelectorOffsets = (const uint32_t *)BlobStart;
1881 TotalNumSelectors = Record[0];
1882 SelectorsLoaded.resize(TotalNumSelectors);
1883 break;
1884
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001885 case pch::METHOD_POOL:
Douglas Gregor83941df2009-04-25 17:48:32 +00001886 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1887 if (Record[0])
1888 MethodPoolLookupTable
1889 = PCHMethodPoolLookupTable::Create(
1890 MethodPoolLookupTableData + Record[0],
1891 MethodPoolLookupTableData,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001892 PCHMethodPoolLookupTrait(*this));
Douglas Gregor83941df2009-04-25 17:48:32 +00001893 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001894 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001895
1896 case pch::PP_COUNTER_VALUE:
1897 if (!Record.empty())
1898 PP.setCounterValue(Record[0]);
1899 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001900 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001902 Error("Premature end of bitstream");
1903 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001904}
1905
Douglas Gregore1d918e2009-04-10 23:10:45 +00001906PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001907 // Set the PCH file name.
1908 this->FileName = FileName;
1909
Douglas Gregor2cf26342009-04-09 22:27:44 +00001910 // Open the PCH file.
1911 std::string ErrStr;
1912 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001913 if (!Buffer) {
1914 Error(ErrStr.c_str());
1915 return IgnorePCH;
1916 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001917
1918 // Initialize the stream
1919 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1920 (const unsigned char *)Buffer->getBufferEnd());
1921
1922 // Sniff for the signature.
1923 if (Stream.Read(8) != 'C' ||
1924 Stream.Read(8) != 'P' ||
1925 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001926 Stream.Read(8) != 'H') {
1927 Error("Not a PCH file");
1928 return IgnorePCH;
1929 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001930
Douglas Gregor2cf26342009-04-09 22:27:44 +00001931 while (!Stream.AtEndOfStream()) {
1932 unsigned Code = Stream.ReadCode();
1933
Douglas Gregore1d918e2009-04-10 23:10:45 +00001934 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1935 Error("Invalid record at top-level");
1936 return Failure;
1937 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001938
1939 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00001940
Douglas Gregor2cf26342009-04-09 22:27:44 +00001941 // We only know the PCH subblock ID.
1942 switch (BlockID) {
1943 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001944 if (Stream.ReadBlockInfoBlock()) {
1945 Error("Malformed BlockInfoBlock");
1946 return Failure;
1947 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001948 break;
1949 case pch::PCH_BLOCK_ID:
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001950 switch (ReadPCHBlock()) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001951 case Success:
1952 break;
1953
1954 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001955 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001956
1957 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001958 // FIXME: We could consider reading through to the end of this
1959 // PCH block, skipping subblocks, to see if there are other
1960 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001961 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001962 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001963 break;
1964 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001965 if (Stream.SkipBlock()) {
1966 Error("Malformed block record");
1967 return Failure;
1968 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001969 break;
1970 }
1971 }
1972
1973 // Load the translation unit declaration
1974 ReadDeclRecord(DeclOffsets[0], 0);
1975
Douglas Gregor668c1a42009-04-21 22:25:48 +00001976 // Initialization of builtins and library builtins occurs before the
1977 // PCH file is read, so there may be some identifiers that were
1978 // loaded into the IdentifierTable before we intercepted the
1979 // creation of identifiers. Iterate through the list of known
1980 // identifiers and determine whether we have to establish
1981 // preprocessor definitions or top-level identifier declaration
1982 // chains for those identifiers.
1983 //
1984 // We copy the IdentifierInfo pointers to a small vector first,
1985 // since de-serializing declarations or macro definitions can add
1986 // new entries into the identifier table, invalidating the
1987 // iterators.
1988 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1989 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1990 IdEnd = PP.getIdentifierTable().end();
1991 Id != IdEnd; ++Id)
1992 Identifiers.push_back(Id->second);
1993 PCHIdentifierLookupTable *IdTable
1994 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1995 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1996 IdentifierInfo *II = Identifiers[I];
1997 // Look in the on-disk hash table for an entry for
1998 PCHIdentifierLookupTrait Info(*this, II);
1999 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2000 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2001 if (Pos == IdTable->end())
2002 continue;
2003
2004 // Dereferencing the iterator has the effect of populating the
2005 // IdentifierInfo node with the various declarations it needs.
2006 (void)*Pos;
2007 }
2008
Douglas Gregorad1de002009-04-18 05:55:16 +00002009 // Load the special types.
2010 Context.setBuiltinVaListType(
2011 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregor319ac892009-04-23 22:29:11 +00002012 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2013 Context.setObjCIdType(GetType(Id));
2014 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2015 Context.setObjCSelType(GetType(Sel));
2016 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2017 Context.setObjCProtoType(GetType(Proto));
2018 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2019 Context.setObjCClassType(GetType(Class));
2020 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2021 Context.setCFConstantStringType(GetType(String));
2022 if (unsigned FastEnum
2023 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2024 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregor0b748912009-04-14 21:18:50 +00002025
Douglas Gregor668c1a42009-04-21 22:25:48 +00002026 return Success;
Douglas Gregor0b748912009-04-14 21:18:50 +00002027}
2028
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002029/// \brief Parse the record that corresponds to a LangOptions data
2030/// structure.
2031///
2032/// This routine compares the language options used to generate the
2033/// PCH file against the language options set for the current
2034/// compilation. For each option, we classify differences between the
2035/// two compiler states as either "benign" or "important". Benign
2036/// differences don't matter, and we accept them without complaint
2037/// (and without modifying the language options). Differences between
2038/// the states for important options cause the PCH file to be
2039/// unusable, so we emit a warning and return true to indicate that
2040/// there was an error.
2041///
2042/// \returns true if the PCH file is unacceptable, false otherwise.
2043bool PCHReader::ParseLanguageOptions(
2044 const llvm::SmallVectorImpl<uint64_t> &Record) {
2045 const LangOptions &LangOpts = Context.getLangOptions();
2046#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2047#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2048 if (Record[Idx] != LangOpts.Option) { \
2049 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2050 Diag(diag::note_ignoring_pch) << FileName; \
2051 return true; \
2052 } \
2053 ++Idx
2054
2055 unsigned Idx = 0;
2056 PARSE_LANGOPT_BENIGN(Trigraphs);
2057 PARSE_LANGOPT_BENIGN(BCPLComment);
2058 PARSE_LANGOPT_BENIGN(DollarIdents);
2059 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2060 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2061 PARSE_LANGOPT_BENIGN(ImplicitInt);
2062 PARSE_LANGOPT_BENIGN(Digraphs);
2063 PARSE_LANGOPT_BENIGN(HexFloats);
2064 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2065 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2066 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2067 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2068 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2069 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2070 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2071 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2072 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2073 PARSE_LANGOPT_BENIGN(PascalStrings);
2074 PARSE_LANGOPT_BENIGN(Boolean);
2075 PARSE_LANGOPT_BENIGN(WritableStrings);
2076 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2077 diag::warn_pch_lax_vector_conversions);
2078 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2079 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2080 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2081 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2082 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2083 diag::warn_pch_thread_safe_statics);
2084 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2085 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2086 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2087 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2088 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2089 diag::warn_pch_heinous_extensions);
2090 // FIXME: Most of the options below are benign if the macro wasn't
2091 // used. Unfortunately, this means that a PCH compiled without
2092 // optimization can't be used with optimization turned on, even
2093 // though the only thing that changes is whether __OPTIMIZE__ was
2094 // defined... but if __OPTIMIZE__ never showed up in the header, it
2095 // doesn't matter. We could consider making this some special kind
2096 // of check.
2097 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2098 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2099 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2100 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2101 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2102 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2103 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2104 Diag(diag::warn_pch_gc_mode)
2105 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2106 Diag(diag::note_ignoring_pch) << FileName;
2107 return true;
2108 }
2109 ++Idx;
2110 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2111 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2112#undef PARSE_LANGOPT_IRRELEVANT
2113#undef PARSE_LANGOPT_BENIGN
2114
2115 return false;
2116}
2117
Douglas Gregor2cf26342009-04-09 22:27:44 +00002118/// \brief Read and return the type at the given offset.
2119///
2120/// This routine actually reads the record corresponding to the type
2121/// at the given offset in the bitstream. It is a helper routine for
2122/// GetType, which deals with reading type IDs.
2123QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002124 // Keep track of where we are in the stream, then jump back there
2125 // after reading this type.
2126 SavedStreamPosition SavedPosition(Stream);
2127
Douglas Gregor2cf26342009-04-09 22:27:44 +00002128 Stream.JumpToBit(Offset);
2129 RecordData Record;
2130 unsigned Code = Stream.ReadCode();
2131 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00002132 case pch::TYPE_EXT_QUAL: {
2133 assert(Record.size() == 3 &&
2134 "Incorrect encoding of extended qualifier type");
2135 QualType Base = GetType(Record[0]);
2136 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2137 unsigned AddressSpace = Record[2];
2138
2139 QualType T = Base;
2140 if (GCAttr != QualType::GCNone)
2141 T = Context.getObjCGCQualType(T, GCAttr);
2142 if (AddressSpace)
2143 T = Context.getAddrSpaceQualType(T, AddressSpace);
2144 return T;
2145 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002146
Douglas Gregor2cf26342009-04-09 22:27:44 +00002147 case pch::TYPE_FIXED_WIDTH_INT: {
2148 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2149 return Context.getFixedWidthIntType(Record[0], Record[1]);
2150 }
2151
2152 case pch::TYPE_COMPLEX: {
2153 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2154 QualType ElemType = GetType(Record[0]);
2155 return Context.getComplexType(ElemType);
2156 }
2157
2158 case pch::TYPE_POINTER: {
2159 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2160 QualType PointeeType = GetType(Record[0]);
2161 return Context.getPointerType(PointeeType);
2162 }
2163
2164 case pch::TYPE_BLOCK_POINTER: {
2165 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2166 QualType PointeeType = GetType(Record[0]);
2167 return Context.getBlockPointerType(PointeeType);
2168 }
2169
2170 case pch::TYPE_LVALUE_REFERENCE: {
2171 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2172 QualType PointeeType = GetType(Record[0]);
2173 return Context.getLValueReferenceType(PointeeType);
2174 }
2175
2176 case pch::TYPE_RVALUE_REFERENCE: {
2177 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2178 QualType PointeeType = GetType(Record[0]);
2179 return Context.getRValueReferenceType(PointeeType);
2180 }
2181
2182 case pch::TYPE_MEMBER_POINTER: {
2183 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2184 QualType PointeeType = GetType(Record[0]);
2185 QualType ClassType = GetType(Record[1]);
2186 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2187 }
2188
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002189 case pch::TYPE_CONSTANT_ARRAY: {
2190 QualType ElementType = GetType(Record[0]);
2191 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2192 unsigned IndexTypeQuals = Record[2];
2193 unsigned Idx = 3;
2194 llvm::APInt Size = ReadAPInt(Record, Idx);
2195 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2196 }
2197
2198 case pch::TYPE_INCOMPLETE_ARRAY: {
2199 QualType ElementType = GetType(Record[0]);
2200 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2201 unsigned IndexTypeQuals = Record[2];
2202 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2203 }
2204
2205 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00002206 QualType ElementType = GetType(Record[0]);
2207 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2208 unsigned IndexTypeQuals = Record[2];
2209 return Context.getVariableArrayType(ElementType, ReadExpr(),
2210 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002211 }
2212
2213 case pch::TYPE_VECTOR: {
2214 if (Record.size() != 2) {
2215 Error("Incorrect encoding of vector type in PCH file");
2216 return QualType();
2217 }
2218
2219 QualType ElementType = GetType(Record[0]);
2220 unsigned NumElements = Record[1];
2221 return Context.getVectorType(ElementType, NumElements);
2222 }
2223
2224 case pch::TYPE_EXT_VECTOR: {
2225 if (Record.size() != 2) {
2226 Error("Incorrect encoding of extended vector type in PCH file");
2227 return QualType();
2228 }
2229
2230 QualType ElementType = GetType(Record[0]);
2231 unsigned NumElements = Record[1];
2232 return Context.getExtVectorType(ElementType, NumElements);
2233 }
2234
2235 case pch::TYPE_FUNCTION_NO_PROTO: {
2236 if (Record.size() != 1) {
2237 Error("Incorrect encoding of no-proto function type");
2238 return QualType();
2239 }
2240 QualType ResultType = GetType(Record[0]);
2241 return Context.getFunctionNoProtoType(ResultType);
2242 }
2243
2244 case pch::TYPE_FUNCTION_PROTO: {
2245 QualType ResultType = GetType(Record[0]);
2246 unsigned Idx = 1;
2247 unsigned NumParams = Record[Idx++];
2248 llvm::SmallVector<QualType, 16> ParamTypes;
2249 for (unsigned I = 0; I != NumParams; ++I)
2250 ParamTypes.push_back(GetType(Record[Idx++]));
2251 bool isVariadic = Record[Idx++];
2252 unsigned Quals = Record[Idx++];
2253 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2254 isVariadic, Quals);
2255 }
2256
2257 case pch::TYPE_TYPEDEF:
2258 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2259 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2260
2261 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00002262 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002263
2264 case pch::TYPE_TYPEOF: {
2265 if (Record.size() != 1) {
2266 Error("Incorrect encoding of typeof(type) in PCH file");
2267 return QualType();
2268 }
2269 QualType UnderlyingType = GetType(Record[0]);
2270 return Context.getTypeOfType(UnderlyingType);
2271 }
2272
2273 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00002274 assert(Record.size() == 1 && "Incorrect encoding of record type");
2275 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002276
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002277 case pch::TYPE_ENUM:
2278 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2279 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2280
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002281 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner4dcf151a2009-04-22 05:57:30 +00002282 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2283 return Context.getObjCInterfaceType(
2284 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002285
Chris Lattnerc6fa4452009-04-22 06:45:28 +00002286 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2287 unsigned Idx = 0;
2288 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2289 unsigned NumProtos = Record[Idx++];
2290 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2291 for (unsigned I = 0; I != NumProtos; ++I)
2292 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2293 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2294 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00002295
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00002296 case pch::TYPE_OBJC_QUALIFIED_ID: {
2297 unsigned Idx = 0;
2298 unsigned NumProtos = Record[Idx++];
2299 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2300 for (unsigned I = 0; I != NumProtos; ++I)
2301 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2302 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2303 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002304 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002305 // Suppress a GCC warning
2306 return QualType();
2307}
2308
2309/// \brief Note that we have loaded the declaration with the given
2310/// Index.
2311///
2312/// This routine notes that this declaration has already been loaded,
2313/// so that future GetDecl calls will return this declaration rather
2314/// than trying to load a new declaration.
2315inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002316 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2317 DeclsLoaded[Index] = D;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002318}
2319
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002320/// \brief Determine whether the consumer will be interested in seeing
2321/// this declaration (via HandleTopLevelDecl).
2322///
2323/// This routine should return true for anything that might affect
2324/// code generation, e.g., inline function definitions, Objective-C
2325/// declarations with metadata, etc.
2326static bool isConsumerInterestedIn(Decl *D) {
2327 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2328 return Var->isFileVarDecl() && Var->getInit();
2329 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2330 return Func->isThisDeclarationADefinition();
2331 return isa<ObjCProtocolDecl>(D);
2332}
2333
Douglas Gregor2cf26342009-04-09 22:27:44 +00002334/// \brief Read the declaration at the given offset from the PCH file.
2335Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00002336 // Keep track of where we are in the stream, then jump back there
2337 // after reading this declaration.
2338 SavedStreamPosition SavedPosition(Stream);
2339
Douglas Gregor2cf26342009-04-09 22:27:44 +00002340 Decl *D = 0;
2341 Stream.JumpToBit(Offset);
2342 RecordData Record;
2343 unsigned Code = Stream.ReadCode();
2344 unsigned Idx = 0;
2345 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00002346
Douglas Gregor2cf26342009-04-09 22:27:44 +00002347 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002348 case pch::DECL_ATTR:
2349 case pch::DECL_CONTEXT_LEXICAL:
2350 case pch::DECL_CONTEXT_VISIBLE:
2351 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2352 break;
2353
Douglas Gregor2cf26342009-04-09 22:27:44 +00002354 case pch::DECL_TRANSLATION_UNIT:
2355 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357 break;
2358
2359 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002360 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002361 break;
2362 }
2363
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002364 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002365 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002366 break;
2367 }
2368
Douglas Gregor8c700062009-04-13 21:20:57 +00002369 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002370 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2371 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00002372 break;
2373 }
2374
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002375 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002376 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2377 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002378 break;
2379 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002380
2381 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002382 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2383 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002384 break;
2385 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002386
Steve Naroff53c9d8a2009-04-20 15:06:07 +00002387 case pch::DECL_OBJC_METHOD: {
2388 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2389 Selector(), QualType(), 0);
2390 break;
2391 }
2392
Steve Naroff30833f82009-04-21 15:12:33 +00002393 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002394 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2395 break;
2396 }
2397
Steve Naroff30833f82009-04-21 15:12:33 +00002398 case pch::DECL_OBJC_IVAR: {
Steve Naroff33feeb02009-04-20 20:09:33 +00002399 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2400 ObjCIvarDecl::None);
2401 break;
2402 }
2403
Steve Naroff30833f82009-04-21 15:12:33 +00002404 case pch::DECL_OBJC_PROTOCOL: {
2405 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2406 break;
2407 }
2408
2409 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2410 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2411 QualType(), 0);
2412 break;
2413 }
2414
2415 case pch::DECL_OBJC_CLASS: {
2416 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2417 break;
2418 }
2419
2420 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2421 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2422 break;
2423 }
2424
2425 case pch::DECL_OBJC_CATEGORY: {
2426 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2427 break;
2428 }
2429
2430 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor10b0e1f2009-04-23 02:53:57 +00002431 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002432 break;
2433 }
2434
2435 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor8f36aba2009-04-23 03:23:08 +00002436 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002437 break;
2438 }
2439
2440 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregor17eee4a2009-04-23 03:51:49 +00002441 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002442 break;
2443 }
2444
2445 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor70e5a142009-04-22 23:20:34 +00002446 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff30833f82009-04-21 15:12:33 +00002447 break;
2448 }
2449
2450 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor8818c4f2009-04-23 03:43:53 +00002451 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2452 SourceLocation(), 0,
2453 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff30833f82009-04-21 15:12:33 +00002454 break;
2455 }
2456
Douglas Gregor8c700062009-04-13 21:20:57 +00002457 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002458 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2459 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00002460 break;
2461 }
2462
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002464 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2465 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002466 break;
2467 }
2468
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002469 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002470 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2471 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002472 break;
2473 }
2474
2475 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002476 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002477 QualType(), QualType(), VarDecl::None,
2478 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00002479 break;
2480 }
2481
Douglas Gregor1028bc62009-04-13 22:49:25 +00002482 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002483 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00002484 break;
2485 }
2486
2487 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002488 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00002489 break;
2490 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002491 }
2492
Douglas Gregor668c1a42009-04-21 22:25:48 +00002493 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorcb70bb22009-04-16 22:29:51 +00002494 if (D) {
2495 LoadedDecl(Index, D);
2496 Reader.Visit(D);
2497 }
2498
Douglas Gregor2cf26342009-04-09 22:27:44 +00002499 // If this declaration is also a declaration context, get the
2500 // offsets for its tables of lexical and visible declarations.
2501 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2502 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2503 if (Offsets.first || Offsets.second) {
2504 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2505 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2506 DeclContextOffsets[DC] = Offsets;
2507 }
2508 }
2509 assert(Idx == Record.size());
2510
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002511 // If we have deserialized a declaration that has a definition the
2512 // AST consumer might need to know about, notify the consumer
2513 // about that definition now or queue it for later.
2514 if (isConsumerInterestedIn(D)) {
2515 if (Consumer) {
Douglas Gregorad38a852009-04-24 23:42:14 +00002516 DeclGroupRef DG(D);
2517 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002518 } else {
2519 InterestingDecls.push_back(D);
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002520 }
2521 }
2522
Douglas Gregor2cf26342009-04-09 22:27:44 +00002523 return D;
2524}
2525
Douglas Gregor8038d512009-04-10 17:25:41 +00002526QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002527 unsigned Quals = ID & 0x07;
2528 unsigned Index = ID >> 3;
2529
2530 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2531 QualType T;
2532 switch ((pch::PredefinedTypeIDs)Index) {
2533 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2534 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2535 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2536
2537 case pch::PREDEF_TYPE_CHAR_U_ID:
2538 case pch::PREDEF_TYPE_CHAR_S_ID:
2539 // FIXME: Check that the signedness of CharTy is correct!
2540 T = Context.CharTy;
2541 break;
2542
2543 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2544 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2545 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2546 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2547 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2548 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2549 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2550 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2551 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2552 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2553 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2554 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2555 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2556 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2557 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2558 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2559 }
2560
2561 assert(!T.isNull() && "Unknown predefined type");
2562 return T.getQualifiedType(Quals);
2563 }
2564
2565 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002566 if (!TypesLoaded[Index])
2567 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002568
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002569 return QualType(TypesLoaded[Index], Quals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570}
2571
Douglas Gregor8038d512009-04-10 17:25:41 +00002572Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002573 if (ID == 0)
2574 return 0;
2575
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002576 if (ID > DeclsLoaded.size()) {
2577 Error("Declaration ID out-of-range for PCH file");
2578 return 0;
2579 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002580
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002581 unsigned Index = ID - 1;
2582 if (!DeclsLoaded[Index])
2583 ReadDeclRecord(DeclOffsets[Index], Index);
2584
2585 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002586}
2587
Douglas Gregor250fc9c2009-04-18 00:07:54 +00002588Stmt *PCHReader::GetStmt(uint64_t Offset) {
2589 // Keep track of where we are in the stream, then jump back there
2590 // after reading this declaration.
2591 SavedStreamPosition SavedPosition(Stream);
2592
2593 Stream.JumpToBit(Offset);
2594 return ReadStmt();
2595}
2596
Douglas Gregor2cf26342009-04-09 22:27:44 +00002597bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00002598 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002599 assert(DC->hasExternalLexicalStorage() &&
2600 "DeclContext has no lexical decls in storage");
2601 uint64_t Offset = DeclContextOffsets[DC].first;
2602 assert(Offset && "DeclContext has no lexical decls in storage");
2603
Douglas Gregor0b748912009-04-14 21:18:50 +00002604 // Keep track of where we are in the stream, then jump back there
2605 // after reading this context.
2606 SavedStreamPosition SavedPosition(Stream);
2607
Douglas Gregor2cf26342009-04-09 22:27:44 +00002608 // Load the record containing all of the declarations lexically in
2609 // this context.
2610 Stream.JumpToBit(Offset);
2611 RecordData Record;
2612 unsigned Code = Stream.ReadCode();
2613 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002614 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002615 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2616
2617 // Load all of the declaration IDs
2618 Decls.clear();
2619 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregor25123082009-04-22 22:34:57 +00002620 ++NumLexicalDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002621 return false;
2622}
2623
2624bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2625 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2626 assert(DC->hasExternalVisibleStorage() &&
2627 "DeclContext has no visible decls in storage");
2628 uint64_t Offset = DeclContextOffsets[DC].second;
2629 assert(Offset && "DeclContext has no visible decls in storage");
2630
Douglas Gregor0b748912009-04-14 21:18:50 +00002631 // Keep track of where we are in the stream, then jump back there
2632 // after reading this context.
2633 SavedStreamPosition SavedPosition(Stream);
2634
Douglas Gregor2cf26342009-04-09 22:27:44 +00002635 // Load the record containing all of the declarations visible in
2636 // this context.
2637 Stream.JumpToBit(Offset);
2638 RecordData Record;
2639 unsigned Code = Stream.ReadCode();
2640 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00002641 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002642 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2643 if (Record.size() == 0)
2644 return false;
2645
2646 Decls.clear();
2647
2648 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002649 while (Idx < Record.size()) {
2650 Decls.push_back(VisibleDeclaration());
2651 Decls.back().Name = ReadDeclarationName(Record, Idx);
2652
Douglas Gregor2cf26342009-04-09 22:27:44 +00002653 unsigned Size = Record[Idx++];
2654 llvm::SmallVector<unsigned, 4> & LoadedDecls
2655 = Decls.back().Declarations;
2656 LoadedDecls.reserve(Size);
2657 for (unsigned I = 0; I < Size; ++I)
2658 LoadedDecls.push_back(Record[Idx++]);
2659 }
2660
Douglas Gregor25123082009-04-22 22:34:57 +00002661 ++NumVisibleDeclContextsRead;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002662 return false;
2663}
2664
Douglas Gregorfdd01722009-04-14 00:24:19 +00002665void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00002666 this->Consumer = Consumer;
2667
Douglas Gregorfdd01722009-04-14 00:24:19 +00002668 if (!Consumer)
2669 return;
2670
2671 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2672 Decl *D = GetDecl(ExternalDefinitions[I]);
2673 DeclGroupRef DG(D);
2674 Consumer->HandleTopLevelDecl(DG);
2675 }
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00002676
2677 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2678 DeclGroupRef DG(InterestingDecls[I]);
2679 Consumer->HandleTopLevelDecl(DG);
2680 }
Douglas Gregorfdd01722009-04-14 00:24:19 +00002681}
2682
Douglas Gregor2cf26342009-04-09 22:27:44 +00002683void PCHReader::PrintStats() {
2684 std::fprintf(stderr, "*** PCH Statistics:\n");
2685
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002686 unsigned NumTypesLoaded
2687 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2688 (Type *)0);
2689 unsigned NumDeclsLoaded
2690 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2691 (Decl *)0);
2692 unsigned NumIdentifiersLoaded
2693 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2694 IdentifiersLoaded.end(),
2695 (IdentifierInfo *)0);
2696 unsigned NumSelectorsLoaded
2697 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2698 SelectorsLoaded.end(),
2699 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002700
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002701 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002702 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002703 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2704 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2705 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002706 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002707 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2708 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002709 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00002710 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002711 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2712 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00002713 if (TotalNumSelectors)
2714 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2715 NumSelectorsLoaded, TotalNumSelectors,
2716 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2717 if (TotalNumStatements)
2718 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2719 NumStatementsRead, TotalNumStatements,
2720 ((float)NumStatementsRead/TotalNumStatements * 100));
2721 if (TotalNumMacros)
2722 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2723 NumMacrosRead, TotalNumMacros,
2724 ((float)NumMacrosRead/TotalNumMacros * 100));
2725 if (TotalLexicalDeclContexts)
2726 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2727 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2728 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2729 * 100));
2730 if (TotalVisibleDeclContexts)
2731 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2732 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2733 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2734 * 100));
2735 if (TotalSelectorsInMethodPool) {
2736 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2737 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2738 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2739 * 100));
2740 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2741 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002742 std::fprintf(stderr, "\n");
2743}
2744
Douglas Gregor668c1a42009-04-21 22:25:48 +00002745void PCHReader::InitializeSema(Sema &S) {
2746 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002747 S.ExternalSource = this;
2748
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002749 // Makes sure any declarations that were deserialized "too early"
2750 // still get added to the identifier's declaration chains.
2751 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2752 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2753 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002754 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00002755 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002756
2757 // If there were any tentative definitions, deserialize them and add
2758 // them to Sema's table of tentative definitions.
2759 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2760 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2761 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2762 }
Douglas Gregor14c22f22009-04-22 22:18:58 +00002763
2764 // If there were any locally-scoped external declarations,
2765 // deserialize them and add them to Sema's table of locally-scoped
2766 // external declarations.
2767 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2768 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2769 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2770 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002771}
2772
2773IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2774 // Try to find this name within our on-disk hash table
2775 PCHIdentifierLookupTable *IdTable
2776 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2777 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2778 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2779 if (Pos == IdTable->end())
2780 return 0;
2781
2782 // Dereferencing the iterator has the effect of building the
2783 // IdentifierInfo node and populating it with the various
2784 // declarations it needs.
2785 return *Pos;
2786}
2787
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002788std::pair<ObjCMethodList, ObjCMethodList>
2789PCHReader::ReadMethodPool(Selector Sel) {
2790 if (!MethodPoolLookupTable)
2791 return std::pair<ObjCMethodList, ObjCMethodList>();
2792
2793 // Try to find this selector within our on-disk hash table.
2794 PCHMethodPoolLookupTable *PoolTable
2795 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2796 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor83941df2009-04-25 17:48:32 +00002797 if (Pos == PoolTable->end()) {
2798 ++NumMethodPoolMisses;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002799 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor83941df2009-04-25 17:48:32 +00002800 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002801
Douglas Gregor83941df2009-04-25 17:48:32 +00002802 ++NumMethodPoolSelectorsRead;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002803 return *Pos;
2804}
2805
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002806void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00002807 assert(ID && "Non-zero identifier ID required");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002808 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2809 IdentifiersLoaded[ID - 1] = II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002810}
2811
Chris Lattner7356a312009-04-11 21:15:38 +00002812IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002813 if (ID == 0)
2814 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002815
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002816 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002817 Error("No identifier table in PCH file");
2818 return 0;
2819 }
Chris Lattner7356a312009-04-11 21:15:38 +00002820
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002821 if (!IdentifiersLoaded[ID - 1]) {
2822 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002823 const char *Str = IdentifierTableData + Offset;
Douglas Gregord6595a42009-04-25 21:04:17 +00002824
2825 // If there is an identifier lookup table, but the offset of this
2826 // string is after the identifier table itself, then we know that
2827 // this string is not in the on-disk hash table. Therefore,
2828 // disable lookup into the hash table when looking for this
2829 // identifier.
2830 PCHIdentifierLookupTable *IdTable
2831 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002832 if (!IdTable ||
2833 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
2834 // Turn off lookup into the on-disk hash table. We know that
2835 // this identifier is not there.
2836 if (IdTable)
2837 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregord6595a42009-04-25 21:04:17 +00002838
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002839 // All of the strings in the PCH file are preceded by a 16-bit
2840 // length. Extract that 16-bit length to avoid having to execute
2841 // strlen().
2842 const char *StrLenPtr = Str - 2;
2843 unsigned StrLen = (((unsigned) StrLenPtr[0])
2844 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
2845 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregord6595a42009-04-25 21:04:17 +00002846
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002847 // Turn on lookup into the on-disk hash table, if we have an
2848 // on-disk hash table.
2849 if (IdTable)
2850 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2851 } else {
2852 // The identifier is a key in our on-disk hash table. Since we
2853 // know where the hash table entry starts, just read in this
2854 // (key, value) pair.
2855 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
2856 const unsigned char *Pos = (const unsigned char *)Str - 4;
2857 std::pair<unsigned, unsigned> KeyDataLengths
2858 = Trait.ReadKeyDataLength(Pos);
Douglas Gregord6595a42009-04-25 21:04:17 +00002859
Douglas Gregor17e1c5e2009-04-25 21:21:38 +00002860 PCHIdentifierLookupTrait::internal_key_type InternalKey
2861 = Trait.ReadKey(Pos, KeyDataLengths.first);
2862 Pos = (const unsigned char *)Str + KeyDataLengths.first;
2863 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
2864 KeyDataLengths.second);
2865 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002866 }
Chris Lattner7356a312009-04-11 21:15:38 +00002867
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002868 return IdentifiersLoaded[ID - 1];
Douglas Gregor2cf26342009-04-09 22:27:44 +00002869}
2870
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002871Selector PCHReader::DecodeSelector(unsigned ID) {
2872 if (ID == 0)
2873 return Selector();
2874
Douglas Gregor83941df2009-04-25 17:48:32 +00002875 if (!MethodPoolLookupTableData) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002876 Error("No selector table in PCH file");
2877 return Selector();
2878 }
Douglas Gregor83941df2009-04-25 17:48:32 +00002879
2880 if (ID > TotalNumSelectors) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002881 Error("Selector ID out of range");
2882 return Selector();
2883 }
Douglas Gregor83941df2009-04-25 17:48:32 +00002884
2885 unsigned Index = ID - 1;
2886 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2887 // Load this selector from the selector table.
2888 // FIXME: endianness portability issues with SelectorOffsets table
2889 PCHMethodPoolLookupTrait Trait(*this);
2890 SelectorsLoaded[Index]
2891 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2892 }
2893
2894 return SelectorsLoaded[Index];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002895}
2896
Douglas Gregor2cf26342009-04-09 22:27:44 +00002897DeclarationName
2898PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2899 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2900 switch (Kind) {
2901 case DeclarationName::Identifier:
2902 return DeclarationName(GetIdentifierInfo(Record, Idx));
2903
2904 case DeclarationName::ObjCZeroArgSelector:
2905 case DeclarationName::ObjCOneArgSelector:
2906 case DeclarationName::ObjCMultiArgSelector:
Steve Naroffa7503a72009-04-23 15:15:40 +00002907 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002908
2909 case DeclarationName::CXXConstructorName:
2910 return Context.DeclarationNames.getCXXConstructorName(
2911 GetType(Record[Idx++]));
2912
2913 case DeclarationName::CXXDestructorName:
2914 return Context.DeclarationNames.getCXXDestructorName(
2915 GetType(Record[Idx++]));
2916
2917 case DeclarationName::CXXConversionFunctionName:
2918 return Context.DeclarationNames.getCXXConversionFunctionName(
2919 GetType(Record[Idx++]));
2920
2921 case DeclarationName::CXXOperatorName:
2922 return Context.DeclarationNames.getCXXOperatorName(
2923 (OverloadedOperatorKind)Record[Idx++]);
2924
2925 case DeclarationName::CXXUsingDirective:
2926 return DeclarationName::getUsingDirectiveName();
2927 }
2928
2929 // Required to silence GCC warning
2930 return DeclarationName();
2931}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002932
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002933/// \brief Read an integral value
2934llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2935 unsigned BitWidth = Record[Idx++];
2936 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2937 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2938 Idx += NumWords;
2939 return Result;
2940}
2941
2942/// \brief Read a signed integral value
2943llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2944 bool isUnsigned = Record[Idx++];
2945 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2946}
2947
Douglas Gregor17fc2232009-04-14 21:55:33 +00002948/// \brief Read a floating-point value
2949llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002950 return llvm::APFloat(ReadAPInt(Record, Idx));
2951}
2952
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002953// \brief Read a string
2954std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2955 unsigned Len = Record[Idx++];
2956 std::string Result(&Record[Idx], &Record[Idx] + Len);
2957 Idx += Len;
2958 return Result;
2959}
2960
2961/// \brief Reads attributes from the current stream position.
2962Attr *PCHReader::ReadAttributes() {
2963 unsigned Code = Stream.ReadCode();
2964 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2965 "Expected unabbreviated record"); (void)Code;
2966
2967 RecordData Record;
2968 unsigned Idx = 0;
2969 unsigned RecCode = Stream.ReadRecord(Code, Record);
2970 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2971 (void)RecCode;
2972
2973#define SIMPLE_ATTR(Name) \
2974 case Attr::Name: \
2975 New = ::new (Context) Name##Attr(); \
2976 break
2977
2978#define STRING_ATTR(Name) \
2979 case Attr::Name: \
2980 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2981 break
2982
2983#define UNSIGNED_ATTR(Name) \
2984 case Attr::Name: \
2985 New = ::new (Context) Name##Attr(Record[Idx++]); \
2986 break
2987
2988 Attr *Attrs = 0;
2989 while (Idx < Record.size()) {
2990 Attr *New = 0;
2991 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2992 bool IsInherited = Record[Idx++];
2993
2994 switch (Kind) {
2995 STRING_ATTR(Alias);
2996 UNSIGNED_ATTR(Aligned);
2997 SIMPLE_ATTR(AlwaysInline);
2998 SIMPLE_ATTR(AnalyzerNoReturn);
2999 STRING_ATTR(Annotate);
3000 STRING_ATTR(AsmLabel);
3001
3002 case Attr::Blocks:
3003 New = ::new (Context) BlocksAttr(
3004 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3005 break;
3006
3007 case Attr::Cleanup:
3008 New = ::new (Context) CleanupAttr(
3009 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3010 break;
3011
3012 SIMPLE_ATTR(Const);
3013 UNSIGNED_ATTR(Constructor);
3014 SIMPLE_ATTR(DLLExport);
3015 SIMPLE_ATTR(DLLImport);
3016 SIMPLE_ATTR(Deprecated);
3017 UNSIGNED_ATTR(Destructor);
3018 SIMPLE_ATTR(FastCall);
3019
3020 case Attr::Format: {
3021 std::string Type = ReadString(Record, Idx);
3022 unsigned FormatIdx = Record[Idx++];
3023 unsigned FirstArg = Record[Idx++];
3024 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3025 break;
3026 }
3027
Chris Lattnercf2a7212009-04-20 19:12:28 +00003028 SIMPLE_ATTR(GNUInline);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003029
3030 case Attr::IBOutletKind:
3031 New = ::new (Context) IBOutletAttr();
3032 break;
3033
3034 SIMPLE_ATTR(NoReturn);
3035 SIMPLE_ATTR(NoThrow);
3036 SIMPLE_ATTR(Nodebug);
3037 SIMPLE_ATTR(Noinline);
3038
3039 case Attr::NonNull: {
3040 unsigned Size = Record[Idx++];
3041 llvm::SmallVector<unsigned, 16> ArgNums;
3042 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3043 Idx += Size;
3044 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3045 break;
3046 }
3047
3048 SIMPLE_ATTR(ObjCException);
3049 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekde9a81b2009-04-25 00:17:17 +00003050 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenek0fc169e2009-04-24 23:09:54 +00003051 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003052 SIMPLE_ATTR(Overloadable);
3053 UNSIGNED_ATTR(Packed);
3054 SIMPLE_ATTR(Pure);
3055 UNSIGNED_ATTR(Regparm);
3056 STRING_ATTR(Section);
3057 SIMPLE_ATTR(StdCall);
3058 SIMPLE_ATTR(TransparentUnion);
3059 SIMPLE_ATTR(Unavailable);
3060 SIMPLE_ATTR(Unused);
3061 SIMPLE_ATTR(Used);
3062
3063 case Attr::Visibility:
3064 New = ::new (Context) VisibilityAttr(
3065 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3066 break;
3067
3068 SIMPLE_ATTR(WarnUnusedResult);
3069 SIMPLE_ATTR(Weak);
3070 SIMPLE_ATTR(WeakImport);
3071 }
3072
3073 assert(New && "Unable to decode attribute?");
3074 New->setInherited(IsInherited);
3075 New->setNext(Attrs);
3076 Attrs = New;
3077 }
3078#undef UNSIGNED_ATTR
3079#undef STRING_ATTR
3080#undef SIMPLE_ATTR
3081
3082 // The list of attributes was built backwards. Reverse the list
3083 // before returning it.
3084 Attr *PrevAttr = 0, *NextAttr = 0;
3085 while (Attrs) {
3086 NextAttr = Attrs->getNext();
3087 Attrs->setNext(PrevAttr);
3088 PrevAttr = Attrs;
3089 Attrs = NextAttr;
3090 }
3091
3092 return PrevAttr;
3093}
3094
Douglas Gregorc9490c02009-04-16 22:23:12 +00003095Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00003096 // Within the bitstream, expressions are stored in Reverse Polish
3097 // Notation, with each of the subexpressions preceding the
3098 // expression they are stored in. To evaluate expressions, we
3099 // continue reading expressions and placing them on the stack, with
3100 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00003101 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00003102 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00003103 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00003104 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00003105 llvm::SmallVector<Stmt *, 16> StmtStack;
3106 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00003107 Stmt::EmptyShell Empty;
3108
Douglas Gregor087fd532009-04-14 23:32:43 +00003109 while (true) {
3110 unsigned Code = Stream.ReadCode();
3111 if (Code == llvm::bitc::END_BLOCK) {
3112 if (Stream.ReadBlockEnd()) {
3113 Error("Error at end of Source Manager block");
3114 return 0;
3115 }
3116 break;
3117 }
Douglas Gregor0b748912009-04-14 21:18:50 +00003118
Douglas Gregor087fd532009-04-14 23:32:43 +00003119 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3120 // No known subblocks, always skip them.
3121 Stream.ReadSubBlockID();
3122 if (Stream.SkipBlock()) {
3123 Error("Malformed block record");
3124 return 0;
3125 }
3126 continue;
3127 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00003128
Douglas Gregor087fd532009-04-14 23:32:43 +00003129 if (Code == llvm::bitc::DEFINE_ABBREV) {
3130 Stream.ReadAbbrevRecord();
3131 continue;
3132 }
Douglas Gregor0b748912009-04-14 21:18:50 +00003133
Douglas Gregorc9490c02009-04-16 22:23:12 +00003134 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00003135 Idx = 0;
3136 Record.clear();
3137 bool Finished = false;
3138 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00003139 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00003140 Finished = true;
3141 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00003142
Douglas Gregorc9490c02009-04-16 22:23:12 +00003143 case pch::STMT_NULL_PTR:
3144 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00003145 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00003146
Douglas Gregor025452f2009-04-17 00:04:06 +00003147 case pch::STMT_NULL:
3148 S = new (Context) NullStmt(Empty);
3149 break;
3150
3151 case pch::STMT_COMPOUND:
3152 S = new (Context) CompoundStmt(Empty);
3153 break;
3154
3155 case pch::STMT_CASE:
3156 S = new (Context) CaseStmt(Empty);
3157 break;
3158
3159 case pch::STMT_DEFAULT:
3160 S = new (Context) DefaultStmt(Empty);
3161 break;
3162
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003163 case pch::STMT_LABEL:
3164 S = new (Context) LabelStmt(Empty);
3165 break;
3166
Douglas Gregor025452f2009-04-17 00:04:06 +00003167 case pch::STMT_IF:
3168 S = new (Context) IfStmt(Empty);
3169 break;
3170
3171 case pch::STMT_SWITCH:
3172 S = new (Context) SwitchStmt(Empty);
3173 break;
3174
Douglas Gregord921cf92009-04-17 00:16:09 +00003175 case pch::STMT_WHILE:
3176 S = new (Context) WhileStmt(Empty);
3177 break;
3178
Douglas Gregor67d82492009-04-17 00:29:51 +00003179 case pch::STMT_DO:
3180 S = new (Context) DoStmt(Empty);
3181 break;
3182
3183 case pch::STMT_FOR:
3184 S = new (Context) ForStmt(Empty);
3185 break;
3186
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003187 case pch::STMT_GOTO:
3188 S = new (Context) GotoStmt(Empty);
3189 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003190
3191 case pch::STMT_INDIRECT_GOTO:
3192 S = new (Context) IndirectGotoStmt(Empty);
3193 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003194
Douglas Gregord921cf92009-04-17 00:16:09 +00003195 case pch::STMT_CONTINUE:
3196 S = new (Context) ContinueStmt(Empty);
3197 break;
3198
Douglas Gregor025452f2009-04-17 00:04:06 +00003199 case pch::STMT_BREAK:
3200 S = new (Context) BreakStmt(Empty);
3201 break;
3202
Douglas Gregor0de9d882009-04-17 16:34:57 +00003203 case pch::STMT_RETURN:
3204 S = new (Context) ReturnStmt(Empty);
3205 break;
3206
Douglas Gregor84f21702009-04-17 16:55:36 +00003207 case pch::STMT_DECL:
3208 S = new (Context) DeclStmt(Empty);
3209 break;
3210
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00003211 case pch::STMT_ASM:
3212 S = new (Context) AsmStmt(Empty);
3213 break;
3214
Douglas Gregor087fd532009-04-14 23:32:43 +00003215 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003216 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003217 break;
3218
3219 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003220 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003221 break;
3222
3223 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003224 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003225 break;
3226
3227 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003228 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003229 break;
3230
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003231 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003232 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003233 break;
3234
Douglas Gregor673ecd62009-04-15 16:35:07 +00003235 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003236 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00003237 Record[PCHStmtReader::NumExprFields + 1]);
3238 break;
3239
Douglas Gregor087fd532009-04-14 23:32:43 +00003240 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003241 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003242 break;
3243
Douglas Gregorc04db4f2009-04-14 23:59:37 +00003244 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003245 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00003246 break;
3247
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003248 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003249 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003250 break;
3251
3252 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003253 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00003254 break;
3255
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003256 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003257 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00003258 break;
3259
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003260 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003261 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003262 break;
3263
3264 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003265 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00003266 break;
3267
Douglas Gregordb600c32009-04-15 00:25:59 +00003268 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003269 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00003270 break;
3271
Douglas Gregorad90e962009-04-15 22:40:36 +00003272 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003273 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00003274 break;
3275
3276 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003277 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00003278 break;
3279
Douglas Gregor087fd532009-04-14 23:32:43 +00003280 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003281 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00003282 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00003283
3284 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003285 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00003286 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00003287
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003288 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003289 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00003290 break;
3291
Douglas Gregord3c98a02009-04-15 23:02:49 +00003292 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003293 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003294 break;
3295
Douglas Gregord077d752009-04-16 00:55:48 +00003296 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003297 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003298 break;
3299
3300 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003301 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00003302 Record[PCHStmtReader::NumExprFields] - 1);
3303
3304 break;
3305
3306 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003307 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00003308 break;
3309
Douglas Gregord3c98a02009-04-15 23:02:49 +00003310 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003311 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00003312 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003313
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003314 case pch::EXPR_ADDR_LABEL:
3315 S = new (Context) AddrLabelExpr(Empty);
3316 break;
3317
Douglas Gregor6a2dd552009-04-17 19:05:30 +00003318 case pch::EXPR_STMT:
3319 S = new (Context) StmtExpr(Empty);
3320 break;
3321
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003322 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003323 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003324 break;
3325
3326 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003327 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003328 break;
3329
3330 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003331 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00003332 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003333
3334 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003335 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003336 break;
3337
Douglas Gregor84af7c22009-04-17 19:21:43 +00003338 case pch::EXPR_BLOCK:
3339 S = new (Context) BlockExpr(Empty);
3340 break;
3341
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003342 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00003343 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003344 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003345
Chris Lattner3a57a372009-04-22 06:29:42 +00003346 case pch::EXPR_OBJC_STRING_LITERAL:
3347 S = new (Context) ObjCStringLiteral(Empty);
3348 break;
Chris Lattner4dcf151a2009-04-22 05:57:30 +00003349 case pch::EXPR_OBJC_ENCODE:
3350 S = new (Context) ObjCEncodeExpr(Empty);
3351 break;
Chris Lattner3a57a372009-04-22 06:29:42 +00003352 case pch::EXPR_OBJC_SELECTOR_EXPR:
3353 S = new (Context) ObjCSelectorExpr(Empty);
3354 break;
3355 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3356 S = new (Context) ObjCProtocolExpr(Empty);
3357 break;
Chris Lattner0389e6b2009-04-26 00:44:05 +00003358 case pch::EXPR_OBJC_IVAR_REF_EXPR:
3359 S = new (Context) ObjCIvarRefExpr(Empty);
3360 break;
3361 case pch::EXPR_OBJC_PROPERTY_REF_EXPR:
3362 S = new (Context) ObjCPropertyRefExpr(Empty);
3363 break;
3364 case pch::EXPR_OBJC_KVC_REF_EXPR:
3365 S = new (Context) ObjCKVCRefExpr(Empty);
3366 break;
Steve Naroffc4f0bbd2009-04-25 14:04:28 +00003367 case pch::EXPR_OBJC_MESSAGE_EXPR:
3368 S = new (Context) ObjCMessageExpr(Empty);
3369 break;
Chris Lattner0389e6b2009-04-26 00:44:05 +00003370 case pch::EXPR_OBJC_SUPER_EXPR:
3371 S = new (Context) ObjCSuperExpr(Empty);
3372 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00003373 }
3374
Douglas Gregorc9490c02009-04-16 22:23:12 +00003375 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00003376 if (Finished)
3377 break;
3378
Douglas Gregor3e1af842009-04-17 22:13:46 +00003379 ++NumStatementsRead;
3380
Douglas Gregorc9490c02009-04-16 22:23:12 +00003381 if (S) {
3382 unsigned NumSubStmts = Reader.Visit(S);
3383 while (NumSubStmts > 0) {
3384 StmtStack.pop_back();
3385 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00003386 }
3387 }
3388
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003389 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00003390 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00003391 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00003392 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00003393 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00003394 return StmtStack.back();
3395}
3396
3397Expr *PCHReader::ReadExpr() {
3398 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00003399}
3400
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003401DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00003402 return Diag(SourceLocation(), DiagID);
3403}
3404
3405DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3406 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003407 Context.getSourceManager()),
3408 DiagID);
3409}
Douglas Gregor025452f2009-04-17 00:04:06 +00003410
Douglas Gregor668c1a42009-04-21 22:25:48 +00003411/// \brief Retrieve the identifier table associated with the
3412/// preprocessor.
3413IdentifierTable &PCHReader::getIdentifierTable() {
3414 return PP.getIdentifierTable();
3415}
3416
Douglas Gregor025452f2009-04-17 00:04:06 +00003417/// \brief Record that the given ID maps to the given switch-case
3418/// statement.
3419void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3420 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3421 SwitchCaseStmts[ID] = SC;
3422}
3423
3424/// \brief Retrieve the switch-case statement with the given ID.
3425SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3426 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3427 return SwitchCaseStmts[ID];
3428}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003429
3430/// \brief Record that the given label statement has been
3431/// deserialized and has the given ID.
3432void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3433 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3434 "Deserialized label twice");
3435 LabelStmts[ID] = S;
3436
3437 // If we've already seen any goto statements that point to this
3438 // label, resolve them now.
3439 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3440 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3441 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3442 Goto->second->setLabel(S);
3443 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003444
3445 // If we've already seen any address-label statements that point to
3446 // this label, resolve them now.
3447 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3448 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3449 = UnresolvedAddrLabelExprs.equal_range(ID);
3450 for (AddrLabelIter AddrLabel = AddrLabels.first;
3451 AddrLabel != AddrLabels.second; ++AddrLabel)
3452 AddrLabel->second->setLabel(S);
3453 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00003454}
3455
3456/// \brief Set the label of the given statement to the label
3457/// identified by ID.
3458///
3459/// Depending on the order in which the label and other statements
3460/// referencing that label occur, this operation may complete
3461/// immediately (updating the statement) or it may queue the
3462/// statement to be back-patched later.
3463void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3464 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3465 if (Label != LabelStmts.end()) {
3466 // We've already seen this label, so set the label of the goto and
3467 // we're done.
3468 S->setLabel(Label->second);
3469 } else {
3470 // We haven't seen this label yet, so add this goto to the set of
3471 // unresolved goto statements.
3472 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3473 }
3474}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00003475
3476/// \brief Set the label of the given expression to the label
3477/// identified by ID.
3478///
3479/// Depending on the order in which the label and other statements
3480/// referencing that label occur, this operation may complete
3481/// immediately (updating the statement) or it may queue the
3482/// statement to be back-patched later.
3483void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3484 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3485 if (Label != LabelStmts.end()) {
3486 // We've already seen this label, so set the label of the
3487 // label-address expression and we're done.
3488 S->setLabel(Label->second);
3489 } else {
3490 // We haven't seen this label yet, so add this label-address
3491 // expression to the set of unresolved label-address expressions.
3492 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3493 }
3494}