blob: 918fd93fdd347236d30d084ce92154a1a607774d [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000015#include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere
Douglas Gregor631f6c62009-04-14 00:24:19 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000019#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000026#include "clang/Lex/HeaderSearch.h"
Douglas Gregorc713da92009-04-21 22:25:48 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000032#include "llvm/Bitcode/BitstreamReader.h"
33#include "llvm/Support/Compiler.h"
34#include "llvm/Support/MemoryBuffer.h"
35#include <algorithm>
36#include <cstdio>
37
38using namespace clang;
39
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000040namespace {
41 /// \brief Helper class that saves the current stream position and
42 /// then restores it when destroyed.
43 struct VISIBILITY_HIDDEN SavedStreamPosition {
44 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
45 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
46
47 ~SavedStreamPosition() {
48 Stream.JumpToBit(Offset);
49 }
50
51 private:
52 llvm::BitstreamReader &Stream;
53 uint64_t Offset;
54 };
55}
56
Douglas Gregorc34897d2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000063 PCHReader &Reader;
64 const PCHReader::RecordData &Record;
65 unsigned &Idx;
66
67 public:
68 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
69 unsigned &Idx)
70 : Reader(Reader), Record(Record), Idx(Idx) { }
71
72 void VisitDecl(Decl *D);
73 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
74 void VisitNamedDecl(NamedDecl *ND);
75 void VisitTypeDecl(TypeDecl *TD);
76 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000085 void VisitParmVarDecl(ParmVarDecl *PD);
86 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000087 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
88 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000089 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000090 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *D);
92 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
93 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000094 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
95 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
96 void VisitObjCClassDecl(ObjCClassDecl *D);
97 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
98 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
99 void VisitObjCImplDecl(ObjCImplDecl *D);
100 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
101 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
102 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
103 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
104 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000105 };
106}
107
108void PCHDeclReader::VisitDecl(Decl *D) {
109 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
110 D->setLexicalDeclContext(
111 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
112 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
113 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000114 if (Record[Idx++])
115 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000116 D->setImplicit(Record[Idx++]);
117 D->setAccess((AccessSpecifier)Record[Idx++]);
118}
119
120void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
121 VisitDecl(TU);
122}
123
124void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
125 VisitDecl(ND);
126 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
127}
128
129void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
130 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000131 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
132}
133
134void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000135 // Note that we cannot use VisitTypeDecl here, because we need to
136 // set the underlying type of the typedef *before* we try to read
137 // the type associated with the TypedefDecl.
138 VisitNamedDecl(TD);
139 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
140 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
141 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000142}
143
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000144void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
145 VisitTypeDecl(TD);
146 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
147 TD->setDefinition(Record[Idx++]);
148 TD->setTypedefForAnonDecl(
149 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
150}
151
152void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
153 VisitTagDecl(ED);
154 ED->setIntegerType(Reader.GetType(Record[Idx++]));
155}
156
Douglas Gregor982365e2009-04-13 21:20:57 +0000157void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
158 VisitTagDecl(RD);
159 RD->setHasFlexibleArrayMember(Record[Idx++]);
160 RD->setAnonymousStructOrUnion(Record[Idx++]);
161}
162
Douglas Gregorc34897d2009-04-09 22:27:44 +0000163void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
164 VisitNamedDecl(VD);
165 VD->setType(Reader.GetType(Record[Idx++]));
166}
167
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000168void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
169 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000170 if (Record[Idx++])
171 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000172 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
173}
174
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000175void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
176 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000177 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000178 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179 FD->setPreviousDeclaration(
180 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
181 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
182 FD->setInline(Record[Idx++]);
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000183 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000184 FD->setVirtual(Record[Idx++]);
185 FD->setPure(Record[Idx++]);
186 FD->setInheritedPrototype(Record[Idx++]);
187 FD->setHasPrototype(Record[Idx++]);
188 FD->setDeleted(Record[Idx++]);
189 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
190 unsigned NumParams = Record[Idx++];
191 llvm::SmallVector<ParmVarDecl *, 16> Params;
192 Params.reserve(NumParams);
193 for (unsigned I = 0; I != NumParams; ++I)
194 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
195 FD->setParams(Reader.getContext(), &Params[0], NumParams);
196}
197
Steve Naroff79ea0e02009-04-20 15:06:07 +0000198void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
199 VisitNamedDecl(MD);
200 if (Record[Idx++]) {
201 // In practice, this won't be executed (since method definitions
202 // don't occur in header files).
203 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
204 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
205 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
206 }
207 MD->setInstanceMethod(Record[Idx++]);
208 MD->setVariadic(Record[Idx++]);
209 MD->setSynthesized(Record[Idx++]);
210 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
211 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
212 MD->setResultType(Reader.GetType(Record[Idx++]));
213 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
214 unsigned NumParams = Record[Idx++];
215 llvm::SmallVector<ParmVarDecl *, 16> Params;
216 Params.reserve(NumParams);
217 for (unsigned I = 0; I != NumParams; ++I)
218 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
219 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
220}
221
Steve Naroff7333b492009-04-20 20:09:33 +0000222void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
223 VisitNamedDecl(CD);
224 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
225}
226
227void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
228 VisitObjCContainerDecl(ID);
229 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000230 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
231 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000232 unsigned NumProtocols = Record[Idx++];
233 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
234 Protocols.reserve(NumProtocols);
235 for (unsigned I = 0; I != NumProtocols; ++I)
236 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor5efc1052009-04-24 22:01:00 +0000237 ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext());
Steve Naroff7333b492009-04-20 20:09:33 +0000238 unsigned NumIvars = Record[Idx++];
239 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
240 IVars.reserve(NumIvars);
241 for (unsigned I = 0; I != NumIvars; ++I)
242 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
243 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000244 ID->setCategoryList(
245 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000246 ID->setForwardDecl(Record[Idx++]);
247 ID->setImplicitInterfaceDecl(Record[Idx++]);
248 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
249 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000250 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000251}
252
253void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
254 VisitFieldDecl(IVD);
255 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
256}
257
Steve Naroff97b53bd2009-04-21 15:12:33 +0000258void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
259 VisitObjCContainerDecl(PD);
260 PD->setForwardDecl(Record[Idx++]);
261 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
262 unsigned NumProtoRefs = Record[Idx++];
263 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
264 ProtoRefs.reserve(NumProtoRefs);
265 for (unsigned I = 0; I != NumProtoRefs; ++I)
266 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
267 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
268}
269
270void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
271 VisitFieldDecl(FD);
272}
273
274void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
275 VisitDecl(CD);
276 unsigned NumClassRefs = Record[Idx++];
277 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
278 ClassRefs.reserve(NumClassRefs);
279 for (unsigned I = 0; I != NumClassRefs; ++I)
280 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
281 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
282}
283
284void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
285 VisitDecl(FPD);
286 unsigned NumProtoRefs = Record[Idx++];
287 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
288 ProtoRefs.reserve(NumProtoRefs);
289 for (unsigned I = 0; I != NumProtoRefs; ++I)
290 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
291 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
292}
293
294void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
295 VisitObjCContainerDecl(CD);
296 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
297 unsigned NumProtoRefs = Record[Idx++];
298 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
299 ProtoRefs.reserve(NumProtoRefs);
300 for (unsigned I = 0; I != NumProtoRefs; ++I)
301 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
302 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000303 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000304 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
305}
306
307void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
308 VisitNamedDecl(CAD);
309 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
310}
311
312void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
313 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000314 D->setType(Reader.GetType(Record[Idx++]));
315 // FIXME: stable encoding
316 D->setPropertyAttributes(
317 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
318 // FIXME: stable encoding
319 D->setPropertyImplementation(
320 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
321 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
323 D->setGetterMethodDecl(
324 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
325 D->setSetterMethodDecl(
326 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setPropertyIvarDecl(
328 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000329}
330
331void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000332 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000333 D->setClassInterface(
334 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
335 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000336}
337
338void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
339 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000340 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000341}
342
343void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
344 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000345 D->setSuperClass(
346 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000347}
348
349
350void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
351 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000352 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
353 D->setPropertyDecl(
354 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
355 D->setPropertyIvarDecl(
356 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000357}
358
Douglas Gregor982365e2009-04-13 21:20:57 +0000359void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
360 VisitValueDecl(FD);
361 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000364}
365
Douglas Gregorc34897d2009-04-09 22:27:44 +0000366void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
367 VisitValueDecl(VD);
368 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
369 VD->setThreadSpecified(Record[Idx++]);
370 VD->setCXXDirectInitializer(Record[Idx++]);
371 VD->setDeclaredInCondition(Record[Idx++]);
372 VD->setPreviousDeclaration(
373 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
374 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375 if (Record[Idx++])
376 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000377}
378
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000379void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
380 VisitVarDecl(PD);
381 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000382 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383}
384
385void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
386 VisitParmVarDecl(PD);
387 PD->setOriginalType(Reader.GetType(Record[Idx++]));
388}
389
Douglas Gregor2a491792009-04-13 22:49:25 +0000390void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
391 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000392 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000393}
394
395void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
396 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000397 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000398 unsigned NumParams = Record[Idx++];
399 llvm::SmallVector<ParmVarDecl *, 16> Params;
400 Params.reserve(NumParams);
401 for (unsigned I = 0; I != NumParams; ++I)
402 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
403 BD->setParams(Reader.getContext(), &Params[0], NumParams);
404}
405
Douglas Gregorc34897d2009-04-09 22:27:44 +0000406std::pair<uint64_t, uint64_t>
407PCHDeclReader::VisitDeclContext(DeclContext *DC) {
408 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000409 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000410 return std::make_pair(LexicalOffset, VisibleOffset);
411}
412
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000413//===----------------------------------------------------------------------===//
414// Statement/expression deserialization
415//===----------------------------------------------------------------------===//
416namespace {
417 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000418 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000419 PCHReader &Reader;
420 const PCHReader::RecordData &Record;
421 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000422 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000423
424 public:
425 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000426 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
427 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000428
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000429 /// \brief The number of record fields required for the Stmt class
430 /// itself.
431 static const unsigned NumStmtFields = 0;
432
Douglas Gregor596e0932009-04-15 16:35:07 +0000433 /// \brief The number of record fields required for the Expr class
434 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000435 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000436
Douglas Gregora151ba42009-04-14 23:32:43 +0000437 // Each of the Visit* functions reads in part of the expression
438 // from the given record and the current expression stack, then
439 // return the total number of operands that it read from the
440 // expression stack.
441
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000442 unsigned VisitStmt(Stmt *S);
443 unsigned VisitNullStmt(NullStmt *S);
444 unsigned VisitCompoundStmt(CompoundStmt *S);
445 unsigned VisitSwitchCase(SwitchCase *S);
446 unsigned VisitCaseStmt(CaseStmt *S);
447 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000448 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000449 unsigned VisitIfStmt(IfStmt *S);
450 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000451 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000452 unsigned VisitDoStmt(DoStmt *S);
453 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000455 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000456 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000457 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000458 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000459 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000460 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000461 unsigned VisitExpr(Expr *E);
462 unsigned VisitPredefinedExpr(PredefinedExpr *E);
463 unsigned VisitDeclRefExpr(DeclRefExpr *E);
464 unsigned VisitIntegerLiteral(IntegerLiteral *E);
465 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000466 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000467 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000468 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000469 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000470 unsigned VisitUnaryOperator(UnaryOperator *E);
471 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000472 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000473 unsigned VisitCallExpr(CallExpr *E);
474 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000475 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000476 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000477 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000480 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
481 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000482 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000483 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000484 unsigned VisitInitListExpr(InitListExpr *E);
485 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000488 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000489 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000490 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
491 unsigned VisitChooseExpr(ChooseExpr *E);
492 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000493 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000494 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000495 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000496 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000497 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000498 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
499 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000500 unsigned VisitObjCMessageExpr(ObjCMessageExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000501 };
502}
503
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000504unsigned PCHStmtReader::VisitStmt(Stmt *S) {
505 assert(Idx == NumStmtFields && "Incorrect statement field count");
506 return 0;
507}
508
509unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
510 VisitStmt(S);
511 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
512 return 0;
513}
514
515unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
516 VisitStmt(S);
517 unsigned NumStmts = Record[Idx++];
518 S->setStmts(Reader.getContext(),
519 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
520 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
521 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
522 return NumStmts;
523}
524
525unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
526 VisitStmt(S);
527 Reader.RecordSwitchCaseID(S, Record[Idx++]);
528 return 0;
529}
530
531unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
532 VisitSwitchCase(S);
533 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
534 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
535 S->setSubStmt(StmtStack.back());
536 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
537 return 3;
538}
539
540unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
541 VisitSwitchCase(S);
542 S->setSubStmt(StmtStack.back());
543 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
544 return 1;
545}
546
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000547unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
548 VisitStmt(S);
549 S->setID(Reader.GetIdentifierInfo(Record, Idx));
550 S->setSubStmt(StmtStack.back());
551 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
552 Reader.RecordLabelStmt(S, Record[Idx++]);
553 return 1;
554}
555
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000556unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
557 VisitStmt(S);
558 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
559 S->setThen(StmtStack[StmtStack.size() - 2]);
560 S->setElse(StmtStack[StmtStack.size() - 1]);
561 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
562 return 3;
563}
564
565unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
566 VisitStmt(S);
567 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
568 S->setBody(StmtStack.back());
569 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
570 SwitchCase *PrevSC = 0;
571 for (unsigned N = Record.size(); Idx != N; ++Idx) {
572 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
573 if (PrevSC)
574 PrevSC->setNextSwitchCase(SC);
575 else
576 S->setSwitchCaseList(SC);
577 PrevSC = SC;
578 }
579 return 2;
580}
581
Douglas Gregora6b503f2009-04-17 00:16:09 +0000582unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
583 VisitStmt(S);
584 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
585 S->setBody(StmtStack.back());
586 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
587 return 2;
588}
589
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000590unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
591 VisitStmt(S);
592 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
593 S->setBody(StmtStack.back());
594 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595 return 2;
596}
597
598unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
599 VisitStmt(S);
600 S->setInit(StmtStack[StmtStack.size() - 4]);
601 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
602 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
603 S->setBody(StmtStack.back());
604 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
605 return 4;
606}
607
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000608unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
609 VisitStmt(S);
610 Reader.SetLabelOf(S, Record[Idx++]);
611 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
612 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
613 return 0;
614}
615
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000616unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
617 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000618 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000619 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
620 return 1;
621}
622
Douglas Gregora6b503f2009-04-17 00:16:09 +0000623unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
624 VisitStmt(S);
625 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
626 return 0;
627}
628
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000629unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
630 VisitStmt(S);
631 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
632 return 0;
633}
634
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000635unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
636 VisitStmt(S);
637 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
638 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
639 return 1;
640}
641
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000642unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
643 VisitStmt(S);
644 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
645 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
646
647 if (Idx + 1 == Record.size()) {
648 // Single declaration
649 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
650 } else {
651 llvm::SmallVector<Decl *, 16> Decls;
652 Decls.reserve(Record.size() - Idx);
653 for (unsigned N = Record.size(); Idx != N; ++Idx)
654 Decls.push_back(Reader.GetDecl(Record[Idx]));
655 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
656 &Decls[0], Decls.size())));
657 }
658 return 0;
659}
660
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000661unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
662 VisitStmt(S);
663 unsigned NumOutputs = Record[Idx++];
664 unsigned NumInputs = Record[Idx++];
665 unsigned NumClobbers = Record[Idx++];
666 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
667 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
668 S->setVolatile(Record[Idx++]);
669 S->setSimple(Record[Idx++]);
670
671 unsigned StackIdx
672 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
673 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
674
675 // Outputs and inputs
676 llvm::SmallVector<std::string, 16> Names;
677 llvm::SmallVector<StringLiteral*, 16> Constraints;
678 llvm::SmallVector<Stmt*, 16> Exprs;
679 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
680 Names.push_back(Reader.ReadString(Record, Idx));
681 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
682 Exprs.push_back(StmtStack[StackIdx++]);
683 }
684 S->setOutputsAndInputs(NumOutputs, NumInputs,
685 &Names[0], &Constraints[0], &Exprs[0]);
686
687 // Constraints
688 llvm::SmallVector<StringLiteral*, 16> Clobbers;
689 for (unsigned I = 0; I != NumClobbers; ++I)
690 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
691 S->setClobbers(&Clobbers[0], NumClobbers);
692
693 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
694 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
695}
696
Douglas Gregora151ba42009-04-14 23:32:43 +0000697unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000698 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000699 E->setType(Reader.GetType(Record[Idx++]));
700 E->setTypeDependent(Record[Idx++]);
701 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000702 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000703 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000704}
705
Douglas Gregora151ba42009-04-14 23:32:43 +0000706unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000707 VisitExpr(E);
708 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
709 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000710 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000711}
712
Douglas Gregora151ba42009-04-14 23:32:43 +0000713unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000714 VisitExpr(E);
715 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
716 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000717 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000718}
719
Douglas Gregora151ba42009-04-14 23:32:43 +0000720unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000721 VisitExpr(E);
722 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
723 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000724 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000725}
726
Douglas Gregora151ba42009-04-14 23:32:43 +0000727unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000728 VisitExpr(E);
729 E->setValue(Reader.ReadAPFloat(Record, Idx));
730 E->setExact(Record[Idx++]);
731 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000732 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000733}
734
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000735unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
736 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000737 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000738 return 1;
739}
740
Douglas Gregor596e0932009-04-15 16:35:07 +0000741unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
742 VisitExpr(E);
743 unsigned Len = Record[Idx++];
744 assert(Record[Idx] == E->getNumConcatenated() &&
745 "Wrong number of concatenated tokens!");
746 ++Idx;
747 E->setWide(Record[Idx++]);
748
749 // Read string data
750 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
751 E->setStrData(Reader.getContext(), &Str[0], Len);
752 Idx += Len;
753
754 // Read source locations
755 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
756 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
757
758 return 0;
759}
760
Douglas Gregora151ba42009-04-14 23:32:43 +0000761unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000762 VisitExpr(E);
763 E->setValue(Record[Idx++]);
764 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
765 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000766 return 0;
767}
768
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000769unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
770 VisitExpr(E);
771 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
772 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000773 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000774 return 1;
775}
776
Douglas Gregor12d74052009-04-15 15:58:59 +0000777unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
778 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000779 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000780 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
781 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
782 return 1;
783}
784
785unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
786 VisitExpr(E);
787 E->setSizeof(Record[Idx++]);
788 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000789 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000790 ++Idx;
791 } else {
792 E->setArgument(Reader.GetType(Record[Idx++]));
793 }
794 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
796 return E->isArgumentType()? 0 : 1;
797}
798
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000799unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
800 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000801 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Steve Naroff315ec172009-04-25 15:19:54 +0000802 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000803 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
804 return 2;
805}
806
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000807unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
808 VisitExpr(E);
809 E->setNumArgs(Reader.getContext(), Record[Idx++]);
810 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000811 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000812 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000813 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000814 return E->getNumArgs() + 1;
815}
816
817unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
818 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000819 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000820 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
821 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
822 E->setArrow(Record[Idx++]);
823 return 1;
824}
825
Douglas Gregora151ba42009-04-14 23:32:43 +0000826unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
827 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000828 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000829 return 1;
830}
831
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000832unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
833 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000834 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
835 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000836 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
837 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
838 return 2;
839}
840
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000841unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
842 VisitBinaryOperator(E);
843 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
844 E->setComputationResultType(Reader.GetType(Record[Idx++]));
845 return 2;
846}
847
848unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
849 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000850 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
851 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
852 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000853 return 3;
854}
855
Douglas Gregora151ba42009-04-14 23:32:43 +0000856unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
857 VisitCastExpr(E);
858 E->setLvalueCast(Record[Idx++]);
859 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000860}
861
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000862unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
863 VisitCastExpr(E);
864 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
865 return 1;
866}
867
868unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
869 VisitExplicitCastExpr(E);
870 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
871 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
872 return 1;
873}
874
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000875unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
876 VisitExpr(E);
877 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000878 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000879 E->setFileScope(Record[Idx++]);
880 return 1;
881}
882
Douglas Gregorec0b8292009-04-15 23:02:49 +0000883unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
884 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000885 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000886 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
887 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
888 return 1;
889}
890
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000891unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
892 VisitExpr(E);
893 unsigned NumInits = Record[Idx++];
894 E->reserveInits(NumInits);
895 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000896 E->updateInit(I,
897 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
898 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000899 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
900 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
901 E->setInitializedFieldInUnion(
902 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
903 E->sawArrayRangeDesignator(Record[Idx++]);
904 return NumInits + 1;
905}
906
907unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
908 typedef DesignatedInitExpr::Designator Designator;
909
910 VisitExpr(E);
911 unsigned NumSubExprs = Record[Idx++];
912 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
913 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000914 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000915 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
916 E->setGNUSyntax(Record[Idx++]);
917
918 llvm::SmallVector<Designator, 4> Designators;
919 while (Idx < Record.size()) {
920 switch ((pch::DesignatorTypes)Record[Idx++]) {
921 case pch::DESIG_FIELD_DECL: {
922 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
923 SourceLocation DotLoc
924 = SourceLocation::getFromRawEncoding(Record[Idx++]);
925 SourceLocation FieldLoc
926 = SourceLocation::getFromRawEncoding(Record[Idx++]);
927 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
928 FieldLoc));
929 Designators.back().setField(Field);
930 break;
931 }
932
933 case pch::DESIG_FIELD_NAME: {
934 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
935 SourceLocation DotLoc
936 = SourceLocation::getFromRawEncoding(Record[Idx++]);
937 SourceLocation FieldLoc
938 = SourceLocation::getFromRawEncoding(Record[Idx++]);
939 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
940 break;
941 }
942
943 case pch::DESIG_ARRAY: {
944 unsigned Index = Record[Idx++];
945 SourceLocation LBracketLoc
946 = SourceLocation::getFromRawEncoding(Record[Idx++]);
947 SourceLocation RBracketLoc
948 = SourceLocation::getFromRawEncoding(Record[Idx++]);
949 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
950 break;
951 }
952
953 case pch::DESIG_ARRAY_RANGE: {
954 unsigned Index = Record[Idx++];
955 SourceLocation LBracketLoc
956 = SourceLocation::getFromRawEncoding(Record[Idx++]);
957 SourceLocation EllipsisLoc
958 = SourceLocation::getFromRawEncoding(Record[Idx++]);
959 SourceLocation RBracketLoc
960 = SourceLocation::getFromRawEncoding(Record[Idx++]);
961 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
962 RBracketLoc));
963 break;
964 }
965 }
966 }
967 E->setDesignators(&Designators[0], Designators.size());
968
969 return NumSubExprs;
970}
971
972unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
973 VisitExpr(E);
974 return 0;
975}
976
Douglas Gregorec0b8292009-04-15 23:02:49 +0000977unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
978 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000979 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000980 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
981 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
982 return 1;
983}
984
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000985unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
986 VisitExpr(E);
987 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
988 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
989 Reader.SetLabelOf(E, Record[Idx++]);
990 return 0;
991}
992
Douglas Gregoreca12f62009-04-17 19:05:30 +0000993unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
994 VisitExpr(E);
995 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
996 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
997 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
998 return 1;
999}
1000
Douglas Gregor209d4622009-04-15 23:33:31 +00001001unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1002 VisitExpr(E);
1003 E->setArgType1(Reader.GetType(Record[Idx++]));
1004 E->setArgType2(Reader.GetType(Record[Idx++]));
1005 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1006 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1007 return 0;
1008}
1009
1010unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1011 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001012 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1013 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1014 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001015 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1016 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1017 return 3;
1018}
1019
1020unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1021 VisitExpr(E);
1022 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1023 return 0;
1024}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001025
Douglas Gregor725e94b2009-04-16 00:01:45 +00001026unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1027 VisitExpr(E);
1028 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001029 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001030 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1031 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1032 return NumExprs;
1033}
1034
Douglas Gregore246b742009-04-17 19:21:43 +00001035unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1036 VisitExpr(E);
1037 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1038 E->setHasBlockDeclRefExprs(Record[Idx++]);
1039 return 0;
1040}
1041
Douglas Gregor725e94b2009-04-16 00:01:45 +00001042unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1043 VisitExpr(E);
1044 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1045 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1046 E->setByRef(Record[Idx++]);
1047 return 0;
1048}
1049
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001050//===----------------------------------------------------------------------===//
1051// Objective-C Expressions and Statements
1052
1053unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1054 VisitExpr(E);
1055 E->setString(cast<StringLiteral>(StmtStack.back()));
1056 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1057 return 1;
1058}
1059
Chris Lattner80f83c62009-04-22 05:57:30 +00001060unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1061 VisitExpr(E);
1062 E->setEncodedType(Reader.GetType(Record[Idx++]));
1063 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1064 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1065 return 0;
1066}
1067
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001068unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1069 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001070 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001071 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1072 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1073 return 0;
1074}
1075
1076unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1077 VisitExpr(E);
1078 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1079 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1080 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1081 return 0;
1082}
1083
Steve Narofffb3e4022009-04-25 14:04:28 +00001084unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1085 VisitExpr(E);
1086 E->setNumArgs(Record[Idx++]);
1087 SourceRange SR(SourceLocation::getFromRawEncoding(Record[Idx++]),
1088 SourceLocation::getFromRawEncoding(Record[Idx++]));
1089 E->setSourceRange(SR);
1090 E->setSelector(Reader.GetSelector(Record, Idx));
1091 E->setMethodDecl(cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1092 // FIXME: deal with class messages.
1093 E->setReceiver(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
1094 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1095 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
1096 return E->getNumArgs() + 1;
1097}
1098
Chris Lattner80f83c62009-04-22 05:57:30 +00001099
Douglas Gregorc713da92009-04-21 22:25:48 +00001100//===----------------------------------------------------------------------===//
1101// PCH reader implementation
1102//===----------------------------------------------------------------------===//
1103
1104namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001105class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1106 PCHReader &Reader;
1107
1108public:
1109 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1110
1111 typedef Selector external_key_type;
1112 typedef external_key_type internal_key_type;
1113
1114 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1115
1116 static bool EqualKey(const internal_key_type& a,
1117 const internal_key_type& b) {
1118 return a == b;
1119 }
1120
1121 static unsigned ComputeHash(Selector Sel) {
1122 unsigned N = Sel.getNumArgs();
1123 if (N == 0)
1124 ++N;
1125 unsigned R = 5381;
1126 for (unsigned I = 0; I != N; ++I)
1127 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1128 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1129 return R;
1130 }
1131
1132 // This hopefully will just get inlined and removed by the optimizer.
1133 static const internal_key_type&
1134 GetInternalKey(const external_key_type& x) { return x; }
1135
1136 static std::pair<unsigned, unsigned>
1137 ReadKeyDataLength(const unsigned char*& d) {
1138 using namespace clang::io;
1139 unsigned KeyLen = ReadUnalignedLE16(d);
1140 unsigned DataLen = ReadUnalignedLE16(d);
1141 return std::make_pair(KeyLen, DataLen);
1142 }
1143
Douglas Gregor2d711832009-04-25 17:48:32 +00001144 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001145 using namespace clang::io;
1146 SelectorTable &SelTable = Reader.getContext().Selectors;
1147 unsigned N = ReadUnalignedLE16(d);
1148 IdentifierInfo *FirstII
1149 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1150 if (N == 0)
1151 return SelTable.getNullarySelector(FirstII);
1152 else if (N == 1)
1153 return SelTable.getUnarySelector(FirstII);
1154
1155 llvm::SmallVector<IdentifierInfo *, 16> Args;
1156 Args.push_back(FirstII);
1157 for (unsigned I = 1; I != N; ++I)
1158 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1159
1160 return SelTable.getSelector(N, &Args[0]);
1161 }
1162
1163 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1164 using namespace clang::io;
1165 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1166 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1167
1168 data_type Result;
1169
1170 // Load instance methods
1171 ObjCMethodList *Prev = 0;
1172 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1173 ObjCMethodDecl *Method
1174 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1175 if (!Result.first.Method) {
1176 // This is the first method, which is the easy case.
1177 Result.first.Method = Method;
1178 Prev = &Result.first;
1179 continue;
1180 }
1181
1182 Prev->Next = new ObjCMethodList(Method, 0);
1183 Prev = Prev->Next;
1184 }
1185
1186 // Load factory methods
1187 Prev = 0;
1188 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1189 ObjCMethodDecl *Method
1190 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1191 if (!Result.second.Method) {
1192 // This is the first method, which is the easy case.
1193 Result.second.Method = Method;
1194 Prev = &Result.second;
1195 continue;
1196 }
1197
1198 Prev->Next = new ObjCMethodList(Method, 0);
1199 Prev = Prev->Next;
1200 }
1201
1202 return Result;
1203 }
1204};
1205
1206} // end anonymous namespace
1207
1208/// \brief The on-disk hash table used for the global method pool.
1209typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1210 PCHMethodPoolLookupTable;
1211
1212namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001213class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1214 PCHReader &Reader;
1215
1216 // If we know the IdentifierInfo in advance, it is here and we will
1217 // not build a new one. Used when deserializing information about an
1218 // identifier that was constructed before the PCH file was read.
1219 IdentifierInfo *KnownII;
1220
1221public:
1222 typedef IdentifierInfo * data_type;
1223
1224 typedef const std::pair<const char*, unsigned> external_key_type;
1225
1226 typedef external_key_type internal_key_type;
1227
1228 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1229 : Reader(Reader), KnownII(II) { }
1230
1231 static bool EqualKey(const internal_key_type& a,
1232 const internal_key_type& b) {
1233 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1234 : false;
1235 }
1236
1237 static unsigned ComputeHash(const internal_key_type& a) {
1238 return BernsteinHash(a.first, a.second);
1239 }
1240
1241 // This hopefully will just get inlined and removed by the optimizer.
1242 static const internal_key_type&
1243 GetInternalKey(const external_key_type& x) { return x; }
1244
1245 static std::pair<unsigned, unsigned>
1246 ReadKeyDataLength(const unsigned char*& d) {
1247 using namespace clang::io;
Douglas Gregorc713da92009-04-21 22:25:48 +00001248 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor0d614eb2009-04-25 19:25:49 +00001249 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregorc713da92009-04-21 22:25:48 +00001250 return std::make_pair(KeyLen, DataLen);
1251 }
1252
1253 static std::pair<const char*, unsigned>
1254 ReadKey(const unsigned char* d, unsigned n) {
1255 assert(n >= 2 && d[n-1] == '\0');
1256 return std::make_pair((const char*) d, n-1);
1257 }
1258
1259 IdentifierInfo *ReadData(const internal_key_type& k,
1260 const unsigned char* d,
1261 unsigned DataLen) {
1262 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001263 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001264 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1265 Bits >>= 1;
1266 bool Poisoned = Bits & 0x01;
1267 Bits >>= 1;
1268 bool ExtensionToken = Bits & 0x01;
1269 Bits >>= 1;
1270 bool hasMacroDefinition = Bits & 0x01;
1271 Bits >>= 1;
1272 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1273 Bits >>= 10;
1274 unsigned TokenID = Bits & 0xFF;
1275 Bits >>= 8;
1276
Douglas Gregorc713da92009-04-21 22:25:48 +00001277 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001278 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001279 DataLen -= 8;
1280
1281 // Build the IdentifierInfo itself and link the identifier ID with
1282 // the new IdentifierInfo.
1283 IdentifierInfo *II = KnownII;
1284 if (!II)
Douglas Gregor99664112009-04-25 20:21:25 +00001285 II = &Reader.BuildIdentifierInfoInsidePCH((const unsigned char *)k.first);
Douglas Gregorc713da92009-04-21 22:25:48 +00001286 Reader.SetIdentifierInfo(ID, II);
1287
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001288 // Set or check the various bits in the IdentifierInfo structure.
1289 // FIXME: Load token IDs lazily, too?
1290 assert((unsigned)II->getTokenID() == TokenID &&
1291 "Incorrect token ID loaded");
1292 (void)TokenID;
1293 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1294 assert(II->isExtensionToken() == ExtensionToken &&
1295 "Incorrect extension token flag");
1296 (void)ExtensionToken;
1297 II->setIsPoisoned(Poisoned);
1298 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1299 "Incorrect C++ operator keyword flag");
1300 (void)CPlusPlusOperatorKeyword;
1301
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001302 // If this identifier is a macro, deserialize the macro
1303 // definition.
1304 if (hasMacroDefinition) {
1305 uint32_t Offset = ReadUnalignedLE64(d);
1306 Reader.ReadMacroRecord(Offset);
1307 DataLen -= 8;
1308 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001309
1310 // Read all of the declarations visible at global scope with this
1311 // name.
1312 Sema *SemaObj = Reader.getSema();
1313 while (DataLen > 0) {
1314 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001315 if (SemaObj) {
1316 // Introduce this declaration into the translation-unit scope
1317 // and add it to the declaration chain for this identifier, so
1318 // that (unqualified) name lookup will find it.
1319 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1320 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1321 } else {
1322 // Queue this declaration so that it will be added to the
1323 // translation unit scope and identifier's declaration chain
1324 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001325 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001326 }
1327
1328 DataLen -= 4;
1329 }
1330 return II;
1331 }
1332};
1333
1334} // end anonymous namespace
1335
1336/// \brief The on-disk hash table used to contain information about
1337/// all of the identifiers in the program.
1338typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1339 PCHIdentifierLookupTable;
1340
Douglas Gregorc34897d2009-04-09 22:27:44 +00001341// FIXME: use the diagnostics machinery
1342static bool Error(const char *Str) {
1343 std::fprintf(stderr, "%s\n", Str);
1344 return true;
1345}
1346
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001347/// \brief Check the contents of the predefines buffer against the
1348/// contents of the predefines buffer used to build the PCH file.
1349///
1350/// The contents of the two predefines buffers should be the same. If
1351/// not, then some command-line option changed the preprocessor state
1352/// and we must reject the PCH file.
1353///
1354/// \param PCHPredef The start of the predefines buffer in the PCH
1355/// file.
1356///
1357/// \param PCHPredefLen The length of the predefines buffer in the PCH
1358/// file.
1359///
1360/// \param PCHBufferID The FileID for the PCH predefines buffer.
1361///
1362/// \returns true if there was a mismatch (in which case the PCH file
1363/// should be ignored), or false otherwise.
1364bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1365 unsigned PCHPredefLen,
1366 FileID PCHBufferID) {
1367 const char *Predef = PP.getPredefines().c_str();
1368 unsigned PredefLen = PP.getPredefines().size();
1369
1370 // If the two predefines buffers compare equal, we're done!.
1371 if (PredefLen == PCHPredefLen &&
1372 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1373 return false;
1374
1375 // The predefines buffers are different. Produce a reasonable
1376 // diagnostic showing where they are different.
1377
1378 // The source locations (potentially in the two different predefines
1379 // buffers)
1380 SourceLocation Loc1, Loc2;
1381 SourceManager &SourceMgr = PP.getSourceManager();
1382
1383 // Create a source buffer for our predefines string, so
1384 // that we can build a diagnostic that points into that
1385 // source buffer.
1386 FileID BufferID;
1387 if (Predef && Predef[0]) {
1388 llvm::MemoryBuffer *Buffer
1389 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1390 "<built-in>");
1391 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1392 }
1393
1394 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1395 std::pair<const char *, const char *> Locations
1396 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1397
1398 if (Locations.first != Predef + MinLen) {
1399 // We found the location in the two buffers where there is a
1400 // difference. Form source locations to point there (in both
1401 // buffers).
1402 unsigned Offset = Locations.first - Predef;
1403 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1404 .getFileLocWithOffset(Offset);
1405 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1406 .getFileLocWithOffset(Offset);
1407 } else if (PredefLen > PCHPredefLen) {
1408 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1409 .getFileLocWithOffset(MinLen);
1410 } else {
1411 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1412 .getFileLocWithOffset(MinLen);
1413 }
1414
1415 Diag(Loc1, diag::warn_pch_preprocessor);
1416 if (Loc2.isValid())
1417 Diag(Loc2, diag::note_predef_in_pch);
1418 Diag(diag::note_ignoring_pch) << FileName;
1419 return true;
1420}
1421
Douglas Gregor635f97f2009-04-13 16:31:14 +00001422/// \brief Read the line table in the source manager block.
1423/// \returns true if ther was an error.
1424static bool ParseLineTable(SourceManager &SourceMgr,
1425 llvm::SmallVectorImpl<uint64_t> &Record) {
1426 unsigned Idx = 0;
1427 LineTableInfo &LineTable = SourceMgr.getLineTable();
1428
1429 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001430 std::map<int, int> FileIDs;
1431 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001432 // Extract the file name
1433 unsigned FilenameLen = Record[Idx++];
1434 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1435 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001436 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1437 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001438 }
1439
1440 // Parse the line entries
1441 std::vector<LineEntry> Entries;
1442 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001443 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001444
1445 // Extract the line entries
1446 unsigned NumEntries = Record[Idx++];
1447 Entries.clear();
1448 Entries.reserve(NumEntries);
1449 for (unsigned I = 0; I != NumEntries; ++I) {
1450 unsigned FileOffset = Record[Idx++];
1451 unsigned LineNo = Record[Idx++];
1452 int FilenameID = Record[Idx++];
1453 SrcMgr::CharacteristicKind FileKind
1454 = (SrcMgr::CharacteristicKind)Record[Idx++];
1455 unsigned IncludeOffset = Record[Idx++];
1456 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1457 FileKind, IncludeOffset));
1458 }
1459 LineTable.AddEntry(FID, Entries);
1460 }
1461
1462 return false;
1463}
1464
Douglas Gregorab1cef72009-04-10 03:52:48 +00001465/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001466PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001467 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001468 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1469 Error("Malformed source manager block record");
1470 return Failure;
1471 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001472
1473 SourceManager &SourceMgr = Context.getSourceManager();
1474 RecordData Record;
1475 while (true) {
1476 unsigned Code = Stream.ReadCode();
1477 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001478 if (Stream.ReadBlockEnd()) {
1479 Error("Error at end of Source Manager block");
1480 return Failure;
1481 }
1482
1483 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001484 }
1485
1486 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1487 // No known subblocks, always skip them.
1488 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001489 if (Stream.SkipBlock()) {
1490 Error("Malformed block record");
1491 return Failure;
1492 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001493 continue;
1494 }
1495
1496 if (Code == llvm::bitc::DEFINE_ABBREV) {
1497 Stream.ReadAbbrevRecord();
1498 continue;
1499 }
1500
1501 // Read a record.
1502 const char *BlobStart;
1503 unsigned BlobLen;
1504 Record.clear();
1505 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1506 default: // Default behavior: ignore.
1507 break;
1508
1509 case pch::SM_SLOC_FILE_ENTRY: {
1510 // FIXME: We would really like to delay the creation of this
1511 // FileEntry until it is actually required, e.g., when producing
1512 // a diagnostic with a source location in this file.
1513 const FileEntry *File
1514 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1515 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001516 FileID ID = SourceMgr.createFileID(File,
1517 SourceLocation::getFromRawEncoding(Record[1]),
1518 (CharacteristicKind)Record[2]);
1519 if (Record[3])
1520 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1521 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001522 break;
1523 }
1524
1525 case pch::SM_SLOC_BUFFER_ENTRY: {
1526 const char *Name = BlobStart;
1527 unsigned Code = Stream.ReadCode();
1528 Record.clear();
1529 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1530 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001531 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001532 llvm::MemoryBuffer *Buffer
1533 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1534 BlobStart + BlobLen - 1,
1535 Name);
1536 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1537
1538 if (strcmp(Name, "<built-in>") == 0
1539 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1540 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001541 break;
1542 }
1543
1544 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1545 SourceLocation SpellingLoc
1546 = SourceLocation::getFromRawEncoding(Record[1]);
1547 SourceMgr.createInstantiationLoc(
1548 SpellingLoc,
1549 SourceLocation::getFromRawEncoding(Record[2]),
1550 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001551 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001552 break;
1553 }
1554
Chris Lattnere1be6022009-04-14 23:22:57 +00001555 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001556 if (ParseLineTable(SourceMgr, Record))
1557 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001558 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001559 }
1560 }
1561}
1562
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001563void PCHReader::ReadMacroRecord(uint64_t Offset) {
1564 // Keep track of where we are in the stream, then jump back there
1565 // after reading this macro.
1566 SavedStreamPosition SavedPosition(Stream);
1567
1568 Stream.JumpToBit(Offset);
1569 RecordData Record;
1570 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1571 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001572
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001573 while (true) {
1574 unsigned Code = Stream.ReadCode();
1575 switch (Code) {
1576 case llvm::bitc::END_BLOCK:
1577 return;
1578
1579 case llvm::bitc::ENTER_SUBBLOCK:
1580 // No known subblocks, always skip them.
1581 Stream.ReadSubBlockID();
1582 if (Stream.SkipBlock()) {
1583 Error("Malformed block record");
1584 return;
1585 }
1586 continue;
1587
1588 case llvm::bitc::DEFINE_ABBREV:
1589 Stream.ReadAbbrevRecord();
1590 continue;
1591 default: break;
1592 }
1593
1594 // Read a record.
1595 Record.clear();
1596 pch::PreprocessorRecordTypes RecType =
1597 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1598 switch (RecType) {
1599 case pch::PP_COUNTER_VALUE:
1600 // Skip this record.
1601 break;
1602
1603 case pch::PP_MACRO_OBJECT_LIKE:
1604 case pch::PP_MACRO_FUNCTION_LIKE: {
1605 // If we already have a macro, that means that we've hit the end
1606 // of the definition of the macro we were looking for. We're
1607 // done.
1608 if (Macro)
1609 return;
1610
1611 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1612 if (II == 0) {
1613 Error("Macro must have a name");
1614 return;
1615 }
1616 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1617 bool isUsed = Record[2];
1618
1619 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1620 MI->setIsUsed(isUsed);
1621
1622 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1623 // Decode function-like macro info.
1624 bool isC99VarArgs = Record[3];
1625 bool isGNUVarArgs = Record[4];
1626 MacroArgs.clear();
1627 unsigned NumArgs = Record[5];
1628 for (unsigned i = 0; i != NumArgs; ++i)
1629 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1630
1631 // Install function-like macro info.
1632 MI->setIsFunctionLike();
1633 if (isC99VarArgs) MI->setIsC99Varargs();
1634 if (isGNUVarArgs) MI->setIsGNUVarargs();
1635 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1636 PP.getPreprocessorAllocator());
1637 }
1638
1639 // Finally, install the macro.
1640 PP.setMacroInfo(II, MI);
1641
1642 // Remember that we saw this macro last so that we add the tokens that
1643 // form its body to it.
1644 Macro = MI;
1645 ++NumMacrosRead;
1646 break;
1647 }
1648
1649 case pch::PP_TOKEN: {
1650 // If we see a TOKEN before a PP_MACRO_*, then the file is
1651 // erroneous, just pretend we didn't see this.
1652 if (Macro == 0) break;
1653
1654 Token Tok;
1655 Tok.startToken();
1656 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1657 Tok.setLength(Record[1]);
1658 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1659 Tok.setIdentifierInfo(II);
1660 Tok.setKind((tok::TokenKind)Record[3]);
1661 Tok.setFlag((Token::TokenFlags)Record[4]);
1662 Macro->AddTokenToBody(Tok);
1663 break;
1664 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001665 case pch::PP_HEADER_FILE_INFO:
1666 break; // Already processed by ReadPreprocessorBlock().
1667 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001668 }
1669}
1670
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001671bool PCHReader::ReadPreprocessorBlock() {
1672 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1673 return Error("Malformed preprocessor block record");
1674
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001675 RecordData Record;
Steve Naroffcda68f22009-04-24 20:03:17 +00001676 unsigned NumHeaderInfos = 0;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001677 while (true) {
1678 unsigned Code = Stream.ReadCode();
1679 switch (Code) {
1680 case llvm::bitc::END_BLOCK:
1681 if (Stream.ReadBlockEnd())
1682 return Error("Error at end of preprocessor block");
1683 return false;
1684
1685 case llvm::bitc::ENTER_SUBBLOCK:
1686 // No known subblocks, always skip them.
1687 Stream.ReadSubBlockID();
1688 if (Stream.SkipBlock())
1689 return Error("Malformed block record");
1690 continue;
1691
1692 case llvm::bitc::DEFINE_ABBREV:
1693 Stream.ReadAbbrevRecord();
1694 continue;
1695 default: break;
1696 }
1697
1698 // Read a record.
1699 Record.clear();
1700 pch::PreprocessorRecordTypes RecType =
1701 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1702 switch (RecType) {
1703 default: // Default behavior: ignore unknown records.
1704 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001705 case pch::PP_COUNTER_VALUE:
1706 if (!Record.empty())
1707 PP.setCounterValue(Record[0]);
1708 break;
1709
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001710 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001711 case pch::PP_MACRO_FUNCTION_LIKE:
1712 case pch::PP_TOKEN:
Steve Naroffcda68f22009-04-24 20:03:17 +00001713 break;
1714 case pch::PP_HEADER_FILE_INFO: {
1715 HeaderFileInfo HFI;
1716 HFI.isImport = Record[0];
1717 HFI.DirInfo = Record[1];
1718 HFI.NumIncludes = Record[2];
1719 HFI.ControllingMacro = DecodeIdentifierInfo(Record[3]);
1720 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1721 break;
1722 }
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001723 }
1724 }
1725}
1726
Douglas Gregorc713da92009-04-21 22:25:48 +00001727PCHReader::PCHReadResult
Douglas Gregor2d711832009-04-25 17:48:32 +00001728PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001729 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1730 Error("Malformed block record");
1731 return Failure;
1732 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001733
1734 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001735 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001736 while (!Stream.AtEndOfStream()) {
1737 unsigned Code = Stream.ReadCode();
1738 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001739 if (Stream.ReadBlockEnd()) {
1740 Error("Error at end of module block");
1741 return Failure;
1742 }
Chris Lattner29241862009-04-11 21:15:38 +00001743
Douglas Gregor179cfb12009-04-10 20:39:37 +00001744 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001745 }
1746
1747 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1748 switch (Stream.ReadSubBlockID()) {
1749 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1750 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1751 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001752 if (Stream.SkipBlock()) {
1753 Error("Malformed block record");
1754 return Failure;
1755 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001756 break;
1757
Chris Lattner29241862009-04-11 21:15:38 +00001758 case pch::PREPROCESSOR_BLOCK_ID:
1759 // Skip the preprocessor block for now, but remember where it is. We
1760 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001761 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001762 Error("Multiple preprocessor blocks found.");
1763 return Failure;
1764 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001765 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001766 if (Stream.SkipBlock()) {
1767 Error("Malformed block record");
1768 return Failure;
1769 }
1770 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001771
Douglas Gregorab1cef72009-04-10 03:52:48 +00001772 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001773 switch (ReadSourceManagerBlock()) {
1774 case Success:
1775 break;
1776
1777 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001778 Error("Malformed source manager block");
1779 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001780
1781 case IgnorePCH:
1782 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001783 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001784 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001785 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001786 continue;
1787 }
1788
1789 if (Code == llvm::bitc::DEFINE_ABBREV) {
1790 Stream.ReadAbbrevRecord();
1791 continue;
1792 }
1793
1794 // Read and process a record.
1795 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001796 const char *BlobStart = 0;
1797 unsigned BlobLen = 0;
1798 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1799 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001800 default: // Default behavior: ignore.
1801 break;
1802
1803 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001804 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001805 Error("Duplicate TYPE_OFFSET record in PCH file");
1806 return Failure;
1807 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001808 TypeOffsets = (const uint64_t *)BlobStart;
1809 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001810 break;
1811
1812 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001813 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001814 Error("Duplicate DECL_OFFSET record in PCH file");
1815 return Failure;
1816 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001817 DeclOffsets = (const uint64_t *)BlobStart;
1818 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001819 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001820
1821 case pch::LANGUAGE_OPTIONS:
1822 if (ParseLanguageOptions(Record))
1823 return IgnorePCH;
1824 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001825
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001826 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001827 std::string TargetTriple(BlobStart, BlobLen);
1828 if (TargetTriple != Context.Target.getTargetTriple()) {
1829 Diag(diag::warn_pch_target_triple)
1830 << TargetTriple << Context.Target.getTargetTriple();
1831 Diag(diag::note_ignoring_pch) << FileName;
1832 return IgnorePCH;
1833 }
1834 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001835 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001836
1837 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001838 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001839 if (Record[0]) {
1840 IdentifierLookupTable
1841 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +00001842 (const unsigned char *)IdentifierTableData + Record[0],
1843 (const unsigned char *)IdentifierTableData,
1844 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001845 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1846 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001847 break;
1848
1849 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001850 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001851 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1852 return Failure;
1853 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001854 IdentifierOffsets = (const uint32_t *)BlobStart;
1855 IdentifiersLoaded.resize(Record[0]);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001856 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001857
1858 case pch::EXTERNAL_DEFINITIONS:
1859 if (!ExternalDefinitions.empty()) {
1860 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1861 return Failure;
1862 }
1863 ExternalDefinitions.swap(Record);
1864 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001865
Douglas Gregore01ad442009-04-18 05:55:16 +00001866 case pch::SPECIAL_TYPES:
1867 SpecialTypes.swap(Record);
1868 break;
1869
Douglas Gregor456e0952009-04-17 22:13:46 +00001870 case pch::STATISTICS:
1871 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001872 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001873 TotalLexicalDeclContexts = Record[2];
1874 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001875 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001876 case pch::TENTATIVE_DEFINITIONS:
1877 if (!TentativeDefinitions.empty()) {
1878 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1879 return Failure;
1880 }
1881 TentativeDefinitions.swap(Record);
1882 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001883
1884 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1885 if (!LocallyScopedExternalDecls.empty()) {
1886 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1887 return Failure;
1888 }
1889 LocallyScopedExternalDecls.swap(Record);
1890 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001891
Douglas Gregor2d711832009-04-25 17:48:32 +00001892 case pch::SELECTOR_OFFSETS:
1893 SelectorOffsets = (const uint32_t *)BlobStart;
1894 TotalNumSelectors = Record[0];
1895 SelectorsLoaded.resize(TotalNumSelectors);
1896 break;
1897
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001898 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +00001899 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1900 if (Record[0])
1901 MethodPoolLookupTable
1902 = PCHMethodPoolLookupTable::Create(
1903 MethodPoolLookupTableData + Record[0],
1904 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001905 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +00001906 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001907 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001908 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001909 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001910 Error("Premature end of bitstream");
1911 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001912}
1913
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001914PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001915 // Set the PCH file name.
1916 this->FileName = FileName;
1917
Douglas Gregorc34897d2009-04-09 22:27:44 +00001918 // Open the PCH file.
1919 std::string ErrStr;
1920 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001921 if (!Buffer) {
1922 Error(ErrStr.c_str());
1923 return IgnorePCH;
1924 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001925
1926 // Initialize the stream
1927 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1928 (const unsigned char *)Buffer->getBufferEnd());
1929
1930 // Sniff for the signature.
1931 if (Stream.Read(8) != 'C' ||
1932 Stream.Read(8) != 'P' ||
1933 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001934 Stream.Read(8) != 'H') {
1935 Error("Not a PCH file");
1936 return IgnorePCH;
1937 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001938
1939 // We expect a number of well-defined blocks, though we don't necessarily
1940 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001941 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff9e84d782009-04-23 10:39:46 +00001942
Douglas Gregorc34897d2009-04-09 22:27:44 +00001943 while (!Stream.AtEndOfStream()) {
1944 unsigned Code = Stream.ReadCode();
1945
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001946 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1947 Error("Invalid record at top-level");
1948 return Failure;
1949 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001950
1951 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001952
Douglas Gregorc34897d2009-04-09 22:27:44 +00001953 // We only know the PCH subblock ID.
1954 switch (BlockID) {
1955 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001956 if (Stream.ReadBlockInfoBlock()) {
1957 Error("Malformed BlockInfoBlock");
1958 return Failure;
1959 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001960 break;
1961 case pch::PCH_BLOCK_ID:
Douglas Gregor2d711832009-04-25 17:48:32 +00001962 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001963 case Success:
1964 break;
1965
1966 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001967 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001968
1969 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001970 // FIXME: We could consider reading through to the end of this
1971 // PCH block, skipping subblocks, to see if there are other
1972 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001973 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001974 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001975 break;
1976 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001977 if (Stream.SkipBlock()) {
1978 Error("Malformed block record");
1979 return Failure;
1980 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001981 break;
1982 }
1983 }
1984
1985 // Load the translation unit declaration
1986 ReadDeclRecord(DeclOffsets[0], 0);
1987
Douglas Gregorc713da92009-04-21 22:25:48 +00001988 // Initialization of builtins and library builtins occurs before the
1989 // PCH file is read, so there may be some identifiers that were
1990 // loaded into the IdentifierTable before we intercepted the
1991 // creation of identifiers. Iterate through the list of known
1992 // identifiers and determine whether we have to establish
1993 // preprocessor definitions or top-level identifier declaration
1994 // chains for those identifiers.
1995 //
1996 // We copy the IdentifierInfo pointers to a small vector first,
1997 // since de-serializing declarations or macro definitions can add
1998 // new entries into the identifier table, invalidating the
1999 // iterators.
2000 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2001 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2002 IdEnd = PP.getIdentifierTable().end();
2003 Id != IdEnd; ++Id)
2004 Identifiers.push_back(Id->second);
2005 PCHIdentifierLookupTable *IdTable
2006 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2007 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2008 IdentifierInfo *II = Identifiers[I];
2009 // Look in the on-disk hash table for an entry for
2010 PCHIdentifierLookupTrait Info(*this, II);
2011 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2012 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2013 if (Pos == IdTable->end())
2014 continue;
2015
2016 // Dereferencing the iterator has the effect of populating the
2017 // IdentifierInfo node with the various declarations it needs.
2018 (void)*Pos;
2019 }
2020
Douglas Gregore01ad442009-04-18 05:55:16 +00002021 // Load the special types.
2022 Context.setBuiltinVaListType(
2023 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002024 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2025 Context.setObjCIdType(GetType(Id));
2026 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2027 Context.setObjCSelType(GetType(Sel));
2028 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2029 Context.setObjCProtoType(GetType(Proto));
2030 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2031 Context.setObjCClassType(GetType(Class));
2032 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2033 Context.setCFConstantStringType(GetType(String));
2034 if (unsigned FastEnum
2035 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2036 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc713da92009-04-21 22:25:48 +00002037 // If we saw the preprocessor block, read it now.
2038 if (PreprocessorBlockOffset) {
2039 SavedStreamPosition SavedPos(Stream);
2040 Stream.JumpToBit(PreprocessorBlockOffset);
2041 if (ReadPreprocessorBlock()) {
2042 Error("Malformed preprocessor block");
2043 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002044 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002045 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002046
Douglas Gregorc713da92009-04-21 22:25:48 +00002047 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002048}
2049
Douglas Gregor179cfb12009-04-10 20:39:37 +00002050/// \brief Parse the record that corresponds to a LangOptions data
2051/// structure.
2052///
2053/// This routine compares the language options used to generate the
2054/// PCH file against the language options set for the current
2055/// compilation. For each option, we classify differences between the
2056/// two compiler states as either "benign" or "important". Benign
2057/// differences don't matter, and we accept them without complaint
2058/// (and without modifying the language options). Differences between
2059/// the states for important options cause the PCH file to be
2060/// unusable, so we emit a warning and return true to indicate that
2061/// there was an error.
2062///
2063/// \returns true if the PCH file is unacceptable, false otherwise.
2064bool PCHReader::ParseLanguageOptions(
2065 const llvm::SmallVectorImpl<uint64_t> &Record) {
2066 const LangOptions &LangOpts = Context.getLangOptions();
2067#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2068#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2069 if (Record[Idx] != LangOpts.Option) { \
2070 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2071 Diag(diag::note_ignoring_pch) << FileName; \
2072 return true; \
2073 } \
2074 ++Idx
2075
2076 unsigned Idx = 0;
2077 PARSE_LANGOPT_BENIGN(Trigraphs);
2078 PARSE_LANGOPT_BENIGN(BCPLComment);
2079 PARSE_LANGOPT_BENIGN(DollarIdents);
2080 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2081 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2082 PARSE_LANGOPT_BENIGN(ImplicitInt);
2083 PARSE_LANGOPT_BENIGN(Digraphs);
2084 PARSE_LANGOPT_BENIGN(HexFloats);
2085 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2086 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2087 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2088 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2089 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2090 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2091 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2092 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2093 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2094 PARSE_LANGOPT_BENIGN(PascalStrings);
2095 PARSE_LANGOPT_BENIGN(Boolean);
2096 PARSE_LANGOPT_BENIGN(WritableStrings);
2097 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2098 diag::warn_pch_lax_vector_conversions);
2099 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2100 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2101 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2102 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2103 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2104 diag::warn_pch_thread_safe_statics);
2105 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2106 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2107 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2108 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2109 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2110 diag::warn_pch_heinous_extensions);
2111 // FIXME: Most of the options below are benign if the macro wasn't
2112 // used. Unfortunately, this means that a PCH compiled without
2113 // optimization can't be used with optimization turned on, even
2114 // though the only thing that changes is whether __OPTIMIZE__ was
2115 // defined... but if __OPTIMIZE__ never showed up in the header, it
2116 // doesn't matter. We could consider making this some special kind
2117 // of check.
2118 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2119 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2120 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2121 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2122 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2123 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2124 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2125 Diag(diag::warn_pch_gc_mode)
2126 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2127 Diag(diag::note_ignoring_pch) << FileName;
2128 return true;
2129 }
2130 ++Idx;
2131 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2132 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2133#undef PARSE_LANGOPT_IRRELEVANT
2134#undef PARSE_LANGOPT_BENIGN
2135
2136 return false;
2137}
2138
Douglas Gregorc34897d2009-04-09 22:27:44 +00002139/// \brief Read and return the type at the given offset.
2140///
2141/// This routine actually reads the record corresponding to the type
2142/// at the given offset in the bitstream. It is a helper routine for
2143/// GetType, which deals with reading type IDs.
2144QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002145 // Keep track of where we are in the stream, then jump back there
2146 // after reading this type.
2147 SavedStreamPosition SavedPosition(Stream);
2148
Douglas Gregorc34897d2009-04-09 22:27:44 +00002149 Stream.JumpToBit(Offset);
2150 RecordData Record;
2151 unsigned Code = Stream.ReadCode();
2152 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002153 case pch::TYPE_EXT_QUAL: {
2154 assert(Record.size() == 3 &&
2155 "Incorrect encoding of extended qualifier type");
2156 QualType Base = GetType(Record[0]);
2157 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2158 unsigned AddressSpace = Record[2];
2159
2160 QualType T = Base;
2161 if (GCAttr != QualType::GCNone)
2162 T = Context.getObjCGCQualType(T, GCAttr);
2163 if (AddressSpace)
2164 T = Context.getAddrSpaceQualType(T, AddressSpace);
2165 return T;
2166 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002167
Douglas Gregorc34897d2009-04-09 22:27:44 +00002168 case pch::TYPE_FIXED_WIDTH_INT: {
2169 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2170 return Context.getFixedWidthIntType(Record[0], Record[1]);
2171 }
2172
2173 case pch::TYPE_COMPLEX: {
2174 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2175 QualType ElemType = GetType(Record[0]);
2176 return Context.getComplexType(ElemType);
2177 }
2178
2179 case pch::TYPE_POINTER: {
2180 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2181 QualType PointeeType = GetType(Record[0]);
2182 return Context.getPointerType(PointeeType);
2183 }
2184
2185 case pch::TYPE_BLOCK_POINTER: {
2186 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2187 QualType PointeeType = GetType(Record[0]);
2188 return Context.getBlockPointerType(PointeeType);
2189 }
2190
2191 case pch::TYPE_LVALUE_REFERENCE: {
2192 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2193 QualType PointeeType = GetType(Record[0]);
2194 return Context.getLValueReferenceType(PointeeType);
2195 }
2196
2197 case pch::TYPE_RVALUE_REFERENCE: {
2198 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2199 QualType PointeeType = GetType(Record[0]);
2200 return Context.getRValueReferenceType(PointeeType);
2201 }
2202
2203 case pch::TYPE_MEMBER_POINTER: {
2204 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2205 QualType PointeeType = GetType(Record[0]);
2206 QualType ClassType = GetType(Record[1]);
2207 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2208 }
2209
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002210 case pch::TYPE_CONSTANT_ARRAY: {
2211 QualType ElementType = GetType(Record[0]);
2212 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2213 unsigned IndexTypeQuals = Record[2];
2214 unsigned Idx = 3;
2215 llvm::APInt Size = ReadAPInt(Record, Idx);
2216 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2217 }
2218
2219 case pch::TYPE_INCOMPLETE_ARRAY: {
2220 QualType ElementType = GetType(Record[0]);
2221 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2222 unsigned IndexTypeQuals = Record[2];
2223 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2224 }
2225
2226 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002227 QualType ElementType = GetType(Record[0]);
2228 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2229 unsigned IndexTypeQuals = Record[2];
2230 return Context.getVariableArrayType(ElementType, ReadExpr(),
2231 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002232 }
2233
2234 case pch::TYPE_VECTOR: {
2235 if (Record.size() != 2) {
2236 Error("Incorrect encoding of vector type in PCH file");
2237 return QualType();
2238 }
2239
2240 QualType ElementType = GetType(Record[0]);
2241 unsigned NumElements = Record[1];
2242 return Context.getVectorType(ElementType, NumElements);
2243 }
2244
2245 case pch::TYPE_EXT_VECTOR: {
2246 if (Record.size() != 2) {
2247 Error("Incorrect encoding of extended vector type in PCH file");
2248 return QualType();
2249 }
2250
2251 QualType ElementType = GetType(Record[0]);
2252 unsigned NumElements = Record[1];
2253 return Context.getExtVectorType(ElementType, NumElements);
2254 }
2255
2256 case pch::TYPE_FUNCTION_NO_PROTO: {
2257 if (Record.size() != 1) {
2258 Error("Incorrect encoding of no-proto function type");
2259 return QualType();
2260 }
2261 QualType ResultType = GetType(Record[0]);
2262 return Context.getFunctionNoProtoType(ResultType);
2263 }
2264
2265 case pch::TYPE_FUNCTION_PROTO: {
2266 QualType ResultType = GetType(Record[0]);
2267 unsigned Idx = 1;
2268 unsigned NumParams = Record[Idx++];
2269 llvm::SmallVector<QualType, 16> ParamTypes;
2270 for (unsigned I = 0; I != NumParams; ++I)
2271 ParamTypes.push_back(GetType(Record[Idx++]));
2272 bool isVariadic = Record[Idx++];
2273 unsigned Quals = Record[Idx++];
2274 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2275 isVariadic, Quals);
2276 }
2277
2278 case pch::TYPE_TYPEDEF:
2279 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2280 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2281
2282 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002283 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002284
2285 case pch::TYPE_TYPEOF: {
2286 if (Record.size() != 1) {
2287 Error("Incorrect encoding of typeof(type) in PCH file");
2288 return QualType();
2289 }
2290 QualType UnderlyingType = GetType(Record[0]);
2291 return Context.getTypeOfType(UnderlyingType);
2292 }
2293
2294 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002295 assert(Record.size() == 1 && "Incorrect encoding of record type");
2296 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002297
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002298 case pch::TYPE_ENUM:
2299 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2300 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2301
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002302 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002303 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2304 return Context.getObjCInterfaceType(
2305 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002306
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002307 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2308 unsigned Idx = 0;
2309 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2310 unsigned NumProtos = Record[Idx++];
2311 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2312 for (unsigned I = 0; I != NumProtos; ++I)
2313 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2314 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2315 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002316
Chris Lattner9b9f2352009-04-22 06:40:03 +00002317 case pch::TYPE_OBJC_QUALIFIED_ID: {
2318 unsigned Idx = 0;
2319 unsigned NumProtos = Record[Idx++];
2320 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2321 for (unsigned I = 0; I != NumProtos; ++I)
2322 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2323 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2324 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002325 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002326 // Suppress a GCC warning
2327 return QualType();
2328}
2329
2330/// \brief Note that we have loaded the declaration with the given
2331/// Index.
2332///
2333/// This routine notes that this declaration has already been loaded,
2334/// so that future GetDecl calls will return this declaration rather
2335/// than trying to load a new declaration.
2336inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002337 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2338 DeclsLoaded[Index] = D;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002339}
2340
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002341/// \brief Determine whether the consumer will be interested in seeing
2342/// this declaration (via HandleTopLevelDecl).
2343///
2344/// This routine should return true for anything that might affect
2345/// code generation, e.g., inline function definitions, Objective-C
2346/// declarations with metadata, etc.
2347static bool isConsumerInterestedIn(Decl *D) {
2348 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2349 return Var->isFileVarDecl() && Var->getInit();
2350 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2351 return Func->isThisDeclarationADefinition();
2352 return isa<ObjCProtocolDecl>(D);
2353}
2354
Douglas Gregorc34897d2009-04-09 22:27:44 +00002355/// \brief Read the declaration at the given offset from the PCH file.
2356Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002357 // Keep track of where we are in the stream, then jump back there
2358 // after reading this declaration.
2359 SavedStreamPosition SavedPosition(Stream);
2360
Douglas Gregorc34897d2009-04-09 22:27:44 +00002361 Decl *D = 0;
2362 Stream.JumpToBit(Offset);
2363 RecordData Record;
2364 unsigned Code = Stream.ReadCode();
2365 unsigned Idx = 0;
2366 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002367
Douglas Gregorc34897d2009-04-09 22:27:44 +00002368 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002369 case pch::DECL_ATTR:
2370 case pch::DECL_CONTEXT_LEXICAL:
2371 case pch::DECL_CONTEXT_VISIBLE:
2372 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2373 break;
2374
Douglas Gregorc34897d2009-04-09 22:27:44 +00002375 case pch::DECL_TRANSLATION_UNIT:
2376 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002377 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002378 break;
2379
2380 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002381 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002382 break;
2383 }
2384
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002385 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002386 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002387 break;
2388 }
2389
Douglas Gregor982365e2009-04-13 21:20:57 +00002390 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002391 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2392 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002393 break;
2394 }
2395
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002396 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002397 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2398 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002399 break;
2400 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002401
2402 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002403 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2404 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002405 break;
2406 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002407
Steve Naroff79ea0e02009-04-20 15:06:07 +00002408 case pch::DECL_OBJC_METHOD: {
2409 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2410 Selector(), QualType(), 0);
2411 break;
2412 }
2413
Steve Naroff97b53bd2009-04-21 15:12:33 +00002414 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002415 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2416 break;
2417 }
2418
Steve Naroff97b53bd2009-04-21 15:12:33 +00002419 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002420 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2421 ObjCIvarDecl::None);
2422 break;
2423 }
2424
Steve Naroff97b53bd2009-04-21 15:12:33 +00002425 case pch::DECL_OBJC_PROTOCOL: {
2426 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2427 break;
2428 }
2429
2430 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2431 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2432 QualType(), 0);
2433 break;
2434 }
2435
2436 case pch::DECL_OBJC_CLASS: {
2437 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2438 break;
2439 }
2440
2441 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2442 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2443 break;
2444 }
2445
2446 case pch::DECL_OBJC_CATEGORY: {
2447 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2448 break;
2449 }
2450
2451 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002452 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002453 break;
2454 }
2455
2456 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002457 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002458 break;
2459 }
2460
2461 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002462 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002463 break;
2464 }
2465
2466 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002467 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002468 break;
2469 }
2470
2471 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002472 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2473 SourceLocation(), 0,
2474 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002475 break;
2476 }
2477
Douglas Gregor982365e2009-04-13 21:20:57 +00002478 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002479 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2480 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002481 break;
2482 }
2483
Douglas Gregorc34897d2009-04-09 22:27:44 +00002484 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002485 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2486 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002487 break;
2488 }
2489
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002490 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002491 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2492 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002493 break;
2494 }
2495
2496 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002497 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002498 QualType(), QualType(), VarDecl::None,
2499 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002500 break;
2501 }
2502
Douglas Gregor2a491792009-04-13 22:49:25 +00002503 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002504 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002505 break;
2506 }
2507
2508 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002509 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002510 break;
2511 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002512 }
2513
Douglas Gregorc713da92009-04-21 22:25:48 +00002514 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002515 if (D) {
2516 LoadedDecl(Index, D);
2517 Reader.Visit(D);
2518 }
2519
Douglas Gregorc34897d2009-04-09 22:27:44 +00002520 // If this declaration is also a declaration context, get the
2521 // offsets for its tables of lexical and visible declarations.
2522 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2523 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2524 if (Offsets.first || Offsets.second) {
2525 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2526 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2527 DeclContextOffsets[DC] = Offsets;
2528 }
2529 }
2530 assert(Idx == Record.size());
2531
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002532 // If we have deserialized a declaration that has a definition the
2533 // AST consumer might need to know about, notify the consumer
2534 // about that definition now or queue it for later.
2535 if (isConsumerInterestedIn(D)) {
2536 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002537 DeclGroupRef DG(D);
2538 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002539 } else {
2540 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002541 }
2542 }
2543
Douglas Gregorc34897d2009-04-09 22:27:44 +00002544 return D;
2545}
2546
Douglas Gregorac8f2802009-04-10 17:25:41 +00002547QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002548 unsigned Quals = ID & 0x07;
2549 unsigned Index = ID >> 3;
2550
2551 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2552 QualType T;
2553 switch ((pch::PredefinedTypeIDs)Index) {
2554 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2555 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2556 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2557
2558 case pch::PREDEF_TYPE_CHAR_U_ID:
2559 case pch::PREDEF_TYPE_CHAR_S_ID:
2560 // FIXME: Check that the signedness of CharTy is correct!
2561 T = Context.CharTy;
2562 break;
2563
2564 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2565 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2566 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2567 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2568 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2569 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2570 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2571 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2572 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2573 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2574 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2575 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2576 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2577 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2578 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2579 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2580 }
2581
2582 assert(!T.isNull() && "Unknown predefined type");
2583 return T.getQualifiedType(Quals);
2584 }
2585
2586 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002587 if (!TypesLoaded[Index])
2588 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002589
Douglas Gregor24a224c2009-04-25 18:35:21 +00002590 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002591}
2592
Douglas Gregorac8f2802009-04-10 17:25:41 +00002593Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002594 if (ID == 0)
2595 return 0;
2596
Douglas Gregor24a224c2009-04-25 18:35:21 +00002597 if (ID > DeclsLoaded.size()) {
2598 Error("Declaration ID out-of-range for PCH file");
2599 return 0;
2600 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002601
Douglas Gregor24a224c2009-04-25 18:35:21 +00002602 unsigned Index = ID - 1;
2603 if (!DeclsLoaded[Index])
2604 ReadDeclRecord(DeclOffsets[Index], Index);
2605
2606 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002607}
2608
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002609Stmt *PCHReader::GetStmt(uint64_t Offset) {
2610 // Keep track of where we are in the stream, then jump back there
2611 // after reading this declaration.
2612 SavedStreamPosition SavedPosition(Stream);
2613
2614 Stream.JumpToBit(Offset);
2615 return ReadStmt();
2616}
2617
Douglas Gregorc34897d2009-04-09 22:27:44 +00002618bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002619 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002620 assert(DC->hasExternalLexicalStorage() &&
2621 "DeclContext has no lexical decls in storage");
2622 uint64_t Offset = DeclContextOffsets[DC].first;
2623 assert(Offset && "DeclContext has no lexical decls in storage");
2624
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002625 // Keep track of where we are in the stream, then jump back there
2626 // after reading this context.
2627 SavedStreamPosition SavedPosition(Stream);
2628
Douglas Gregorc34897d2009-04-09 22:27:44 +00002629 // Load the record containing all of the declarations lexically in
2630 // this context.
2631 Stream.JumpToBit(Offset);
2632 RecordData Record;
2633 unsigned Code = Stream.ReadCode();
2634 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002635 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002636 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2637
2638 // Load all of the declaration IDs
2639 Decls.clear();
2640 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002641 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002642 return false;
2643}
2644
2645bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2646 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2647 assert(DC->hasExternalVisibleStorage() &&
2648 "DeclContext has no visible decls in storage");
2649 uint64_t Offset = DeclContextOffsets[DC].second;
2650 assert(Offset && "DeclContext has no visible decls in storage");
2651
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002652 // Keep track of where we are in the stream, then jump back there
2653 // after reading this context.
2654 SavedStreamPosition SavedPosition(Stream);
2655
Douglas Gregorc34897d2009-04-09 22:27:44 +00002656 // Load the record containing all of the declarations visible in
2657 // this context.
2658 Stream.JumpToBit(Offset);
2659 RecordData Record;
2660 unsigned Code = Stream.ReadCode();
2661 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002662 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002663 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2664 if (Record.size() == 0)
2665 return false;
2666
2667 Decls.clear();
2668
2669 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002670 while (Idx < Record.size()) {
2671 Decls.push_back(VisibleDeclaration());
2672 Decls.back().Name = ReadDeclarationName(Record, Idx);
2673
Douglas Gregorc34897d2009-04-09 22:27:44 +00002674 unsigned Size = Record[Idx++];
2675 llvm::SmallVector<unsigned, 4> & LoadedDecls
2676 = Decls.back().Declarations;
2677 LoadedDecls.reserve(Size);
2678 for (unsigned I = 0; I < Size; ++I)
2679 LoadedDecls.push_back(Record[Idx++]);
2680 }
2681
Douglas Gregoraf136d92009-04-22 22:34:57 +00002682 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002683 return false;
2684}
2685
Douglas Gregor631f6c62009-04-14 00:24:19 +00002686void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002687 this->Consumer = Consumer;
2688
Douglas Gregor631f6c62009-04-14 00:24:19 +00002689 if (!Consumer)
2690 return;
2691
2692 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2693 Decl *D = GetDecl(ExternalDefinitions[I]);
2694 DeclGroupRef DG(D);
2695 Consumer->HandleTopLevelDecl(DG);
2696 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002697
2698 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2699 DeclGroupRef DG(InterestingDecls[I]);
2700 Consumer->HandleTopLevelDecl(DG);
2701 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002702}
2703
Douglas Gregorc34897d2009-04-09 22:27:44 +00002704void PCHReader::PrintStats() {
2705 std::fprintf(stderr, "*** PCH Statistics:\n");
2706
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002707 unsigned NumTypesLoaded
2708 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2709 (Type *)0);
2710 unsigned NumDeclsLoaded
2711 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2712 (Decl *)0);
2713 unsigned NumIdentifiersLoaded
2714 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2715 IdentifiersLoaded.end(),
2716 (IdentifierInfo *)0);
2717 unsigned NumSelectorsLoaded
2718 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2719 SelectorsLoaded.end(),
2720 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00002721
Douglas Gregor24a224c2009-04-25 18:35:21 +00002722 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002723 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002724 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2725 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2726 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002727 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002728 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2729 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002730 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002731 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002732 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2733 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00002734 if (TotalNumSelectors)
2735 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2736 NumSelectorsLoaded, TotalNumSelectors,
2737 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2738 if (TotalNumStatements)
2739 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2740 NumStatementsRead, TotalNumStatements,
2741 ((float)NumStatementsRead/TotalNumStatements * 100));
2742 if (TotalNumMacros)
2743 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2744 NumMacrosRead, TotalNumMacros,
2745 ((float)NumMacrosRead/TotalNumMacros * 100));
2746 if (TotalLexicalDeclContexts)
2747 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2748 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2749 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2750 * 100));
2751 if (TotalVisibleDeclContexts)
2752 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2753 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2754 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2755 * 100));
2756 if (TotalSelectorsInMethodPool) {
2757 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2758 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2759 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2760 * 100));
2761 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2762 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002763 std::fprintf(stderr, "\n");
2764}
2765
Douglas Gregorc713da92009-04-21 22:25:48 +00002766void PCHReader::InitializeSema(Sema &S) {
2767 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002768 S.ExternalSource = this;
2769
Douglas Gregor2554cf22009-04-22 21:15:06 +00002770 // Makes sure any declarations that were deserialized "too early"
2771 // still get added to the identifier's declaration chains.
2772 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2773 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2774 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002775 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002776 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002777
2778 // If there were any tentative definitions, deserialize them and add
2779 // them to Sema's table of tentative definitions.
2780 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2781 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2782 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2783 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002784
2785 // If there were any locally-scoped external declarations,
2786 // deserialize them and add them to Sema's table of locally-scoped
2787 // external declarations.
2788 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2789 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2790 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2791 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002792}
2793
2794IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2795 // Try to find this name within our on-disk hash table
2796 PCHIdentifierLookupTable *IdTable
2797 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2798 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2799 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2800 if (Pos == IdTable->end())
2801 return 0;
2802
2803 // Dereferencing the iterator has the effect of building the
2804 // IdentifierInfo node and populating it with the various
2805 // declarations it needs.
2806 return *Pos;
2807}
2808
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002809std::pair<ObjCMethodList, ObjCMethodList>
2810PCHReader::ReadMethodPool(Selector Sel) {
2811 if (!MethodPoolLookupTable)
2812 return std::pair<ObjCMethodList, ObjCMethodList>();
2813
2814 // Try to find this selector within our on-disk hash table.
2815 PCHMethodPoolLookupTable *PoolTable
2816 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2817 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00002818 if (Pos == PoolTable->end()) {
2819 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002820 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00002821 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002822
Douglas Gregor2d711832009-04-25 17:48:32 +00002823 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002824 return *Pos;
2825}
2826
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002827void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00002828 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002829 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2830 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00002831}
2832
Chris Lattner29241862009-04-11 21:15:38 +00002833IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002834 if (ID == 0)
2835 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002836
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002837 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002838 Error("No identifier table in PCH file");
2839 return 0;
2840 }
Chris Lattner29241862009-04-11 21:15:38 +00002841
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002842 if (!IdentifiersLoaded[ID - 1]) {
2843 uint32_t Offset = IdentifierOffsets[ID - 1];
2844 IdentifiersLoaded[ID - 1]
2845 = &Context.Idents.get(IdentifierTableData + Offset);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002846 }
Chris Lattner29241862009-04-11 21:15:38 +00002847
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002848 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002849}
2850
Douglas Gregor99664112009-04-25 20:21:25 +00002851IdentifierInfo &
2852PCHReader::BuildIdentifierInfoInsidePCH(const unsigned char *Str) {
2853 // Allocate the object.
2854 std::pair<IdentifierInfo,const unsigned char*> *Mem =
2855 Alloc.Allocate<std::pair<IdentifierInfo,const unsigned char*> >();
2856
2857 // Build the IdentifierInfo itself.
2858 Mem->second = Str;
2859 assert(Str[0] != '\0');
2860 IdentifierInfo *II = new ((void*) Mem) IdentifierInfo();
2861 return *II;
2862}
2863
Steve Naroff9e84d782009-04-23 10:39:46 +00002864Selector PCHReader::DecodeSelector(unsigned ID) {
2865 if (ID == 0)
2866 return Selector();
2867
Douglas Gregor2d711832009-04-25 17:48:32 +00002868 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002869 Error("No selector table in PCH file");
2870 return Selector();
2871 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002872
2873 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002874 Error("Selector ID out of range");
2875 return Selector();
2876 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002877
2878 unsigned Index = ID - 1;
2879 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2880 // Load this selector from the selector table.
2881 // FIXME: endianness portability issues with SelectorOffsets table
2882 PCHMethodPoolLookupTrait Trait(*this);
2883 SelectorsLoaded[Index]
2884 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2885 }
2886
2887 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00002888}
2889
Douglas Gregorc34897d2009-04-09 22:27:44 +00002890DeclarationName
2891PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2892 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2893 switch (Kind) {
2894 case DeclarationName::Identifier:
2895 return DeclarationName(GetIdentifierInfo(Record, Idx));
2896
2897 case DeclarationName::ObjCZeroArgSelector:
2898 case DeclarationName::ObjCOneArgSelector:
2899 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002900 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002901
2902 case DeclarationName::CXXConstructorName:
2903 return Context.DeclarationNames.getCXXConstructorName(
2904 GetType(Record[Idx++]));
2905
2906 case DeclarationName::CXXDestructorName:
2907 return Context.DeclarationNames.getCXXDestructorName(
2908 GetType(Record[Idx++]));
2909
2910 case DeclarationName::CXXConversionFunctionName:
2911 return Context.DeclarationNames.getCXXConversionFunctionName(
2912 GetType(Record[Idx++]));
2913
2914 case DeclarationName::CXXOperatorName:
2915 return Context.DeclarationNames.getCXXOperatorName(
2916 (OverloadedOperatorKind)Record[Idx++]);
2917
2918 case DeclarationName::CXXUsingDirective:
2919 return DeclarationName::getUsingDirectiveName();
2920 }
2921
2922 // Required to silence GCC warning
2923 return DeclarationName();
2924}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002925
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002926/// \brief Read an integral value
2927llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2928 unsigned BitWidth = Record[Idx++];
2929 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2930 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2931 Idx += NumWords;
2932 return Result;
2933}
2934
2935/// \brief Read a signed integral value
2936llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2937 bool isUnsigned = Record[Idx++];
2938 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2939}
2940
Douglas Gregore2f37202009-04-14 21:55:33 +00002941/// \brief Read a floating-point value
2942llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002943 return llvm::APFloat(ReadAPInt(Record, Idx));
2944}
2945
Douglas Gregor1c507882009-04-15 21:30:51 +00002946// \brief Read a string
2947std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2948 unsigned Len = Record[Idx++];
2949 std::string Result(&Record[Idx], &Record[Idx] + Len);
2950 Idx += Len;
2951 return Result;
2952}
2953
2954/// \brief Reads attributes from the current stream position.
2955Attr *PCHReader::ReadAttributes() {
2956 unsigned Code = Stream.ReadCode();
2957 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2958 "Expected unabbreviated record"); (void)Code;
2959
2960 RecordData Record;
2961 unsigned Idx = 0;
2962 unsigned RecCode = Stream.ReadRecord(Code, Record);
2963 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2964 (void)RecCode;
2965
2966#define SIMPLE_ATTR(Name) \
2967 case Attr::Name: \
2968 New = ::new (Context) Name##Attr(); \
2969 break
2970
2971#define STRING_ATTR(Name) \
2972 case Attr::Name: \
2973 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2974 break
2975
2976#define UNSIGNED_ATTR(Name) \
2977 case Attr::Name: \
2978 New = ::new (Context) Name##Attr(Record[Idx++]); \
2979 break
2980
2981 Attr *Attrs = 0;
2982 while (Idx < Record.size()) {
2983 Attr *New = 0;
2984 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2985 bool IsInherited = Record[Idx++];
2986
2987 switch (Kind) {
2988 STRING_ATTR(Alias);
2989 UNSIGNED_ATTR(Aligned);
2990 SIMPLE_ATTR(AlwaysInline);
2991 SIMPLE_ATTR(AnalyzerNoReturn);
2992 STRING_ATTR(Annotate);
2993 STRING_ATTR(AsmLabel);
2994
2995 case Attr::Blocks:
2996 New = ::new (Context) BlocksAttr(
2997 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2998 break;
2999
3000 case Attr::Cleanup:
3001 New = ::new (Context) CleanupAttr(
3002 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3003 break;
3004
3005 SIMPLE_ATTR(Const);
3006 UNSIGNED_ATTR(Constructor);
3007 SIMPLE_ATTR(DLLExport);
3008 SIMPLE_ATTR(DLLImport);
3009 SIMPLE_ATTR(Deprecated);
3010 UNSIGNED_ATTR(Destructor);
3011 SIMPLE_ATTR(FastCall);
3012
3013 case Attr::Format: {
3014 std::string Type = ReadString(Record, Idx);
3015 unsigned FormatIdx = Record[Idx++];
3016 unsigned FirstArg = Record[Idx++];
3017 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3018 break;
3019 }
3020
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003021 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003022
3023 case Attr::IBOutletKind:
3024 New = ::new (Context) IBOutletAttr();
3025 break;
3026
3027 SIMPLE_ATTR(NoReturn);
3028 SIMPLE_ATTR(NoThrow);
3029 SIMPLE_ATTR(Nodebug);
3030 SIMPLE_ATTR(Noinline);
3031
3032 case Attr::NonNull: {
3033 unsigned Size = Record[Idx++];
3034 llvm::SmallVector<unsigned, 16> ArgNums;
3035 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3036 Idx += Size;
3037 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3038 break;
3039 }
3040
3041 SIMPLE_ATTR(ObjCException);
3042 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003043 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003044 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003045 SIMPLE_ATTR(Overloadable);
3046 UNSIGNED_ATTR(Packed);
3047 SIMPLE_ATTR(Pure);
3048 UNSIGNED_ATTR(Regparm);
3049 STRING_ATTR(Section);
3050 SIMPLE_ATTR(StdCall);
3051 SIMPLE_ATTR(TransparentUnion);
3052 SIMPLE_ATTR(Unavailable);
3053 SIMPLE_ATTR(Unused);
3054 SIMPLE_ATTR(Used);
3055
3056 case Attr::Visibility:
3057 New = ::new (Context) VisibilityAttr(
3058 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3059 break;
3060
3061 SIMPLE_ATTR(WarnUnusedResult);
3062 SIMPLE_ATTR(Weak);
3063 SIMPLE_ATTR(WeakImport);
3064 }
3065
3066 assert(New && "Unable to decode attribute?");
3067 New->setInherited(IsInherited);
3068 New->setNext(Attrs);
3069 Attrs = New;
3070 }
3071#undef UNSIGNED_ATTR
3072#undef STRING_ATTR
3073#undef SIMPLE_ATTR
3074
3075 // The list of attributes was built backwards. Reverse the list
3076 // before returning it.
3077 Attr *PrevAttr = 0, *NextAttr = 0;
3078 while (Attrs) {
3079 NextAttr = Attrs->getNext();
3080 Attrs->setNext(PrevAttr);
3081 PrevAttr = Attrs;
3082 Attrs = NextAttr;
3083 }
3084
3085 return PrevAttr;
3086}
3087
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003088Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003089 // Within the bitstream, expressions are stored in Reverse Polish
3090 // Notation, with each of the subexpressions preceding the
3091 // expression they are stored in. To evaluate expressions, we
3092 // continue reading expressions and placing them on the stack, with
3093 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003094 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003095 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003096 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003097 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003098 llvm::SmallVector<Stmt *, 16> StmtStack;
3099 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003100 Stmt::EmptyShell Empty;
3101
Douglas Gregora151ba42009-04-14 23:32:43 +00003102 while (true) {
3103 unsigned Code = Stream.ReadCode();
3104 if (Code == llvm::bitc::END_BLOCK) {
3105 if (Stream.ReadBlockEnd()) {
3106 Error("Error at end of Source Manager block");
3107 return 0;
3108 }
3109 break;
3110 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003111
Douglas Gregora151ba42009-04-14 23:32:43 +00003112 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3113 // No known subblocks, always skip them.
3114 Stream.ReadSubBlockID();
3115 if (Stream.SkipBlock()) {
3116 Error("Malformed block record");
3117 return 0;
3118 }
3119 continue;
3120 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003121
Douglas Gregora151ba42009-04-14 23:32:43 +00003122 if (Code == llvm::bitc::DEFINE_ABBREV) {
3123 Stream.ReadAbbrevRecord();
3124 continue;
3125 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003126
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003127 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003128 Idx = 0;
3129 Record.clear();
3130 bool Finished = false;
3131 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003132 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003133 Finished = true;
3134 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003135
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003136 case pch::STMT_NULL_PTR:
3137 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003138 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003139
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003140 case pch::STMT_NULL:
3141 S = new (Context) NullStmt(Empty);
3142 break;
3143
3144 case pch::STMT_COMPOUND:
3145 S = new (Context) CompoundStmt(Empty);
3146 break;
3147
3148 case pch::STMT_CASE:
3149 S = new (Context) CaseStmt(Empty);
3150 break;
3151
3152 case pch::STMT_DEFAULT:
3153 S = new (Context) DefaultStmt(Empty);
3154 break;
3155
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003156 case pch::STMT_LABEL:
3157 S = new (Context) LabelStmt(Empty);
3158 break;
3159
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003160 case pch::STMT_IF:
3161 S = new (Context) IfStmt(Empty);
3162 break;
3163
3164 case pch::STMT_SWITCH:
3165 S = new (Context) SwitchStmt(Empty);
3166 break;
3167
Douglas Gregora6b503f2009-04-17 00:16:09 +00003168 case pch::STMT_WHILE:
3169 S = new (Context) WhileStmt(Empty);
3170 break;
3171
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003172 case pch::STMT_DO:
3173 S = new (Context) DoStmt(Empty);
3174 break;
3175
3176 case pch::STMT_FOR:
3177 S = new (Context) ForStmt(Empty);
3178 break;
3179
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003180 case pch::STMT_GOTO:
3181 S = new (Context) GotoStmt(Empty);
3182 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003183
3184 case pch::STMT_INDIRECT_GOTO:
3185 S = new (Context) IndirectGotoStmt(Empty);
3186 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003187
Douglas Gregora6b503f2009-04-17 00:16:09 +00003188 case pch::STMT_CONTINUE:
3189 S = new (Context) ContinueStmt(Empty);
3190 break;
3191
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003192 case pch::STMT_BREAK:
3193 S = new (Context) BreakStmt(Empty);
3194 break;
3195
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003196 case pch::STMT_RETURN:
3197 S = new (Context) ReturnStmt(Empty);
3198 break;
3199
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003200 case pch::STMT_DECL:
3201 S = new (Context) DeclStmt(Empty);
3202 break;
3203
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003204 case pch::STMT_ASM:
3205 S = new (Context) AsmStmt(Empty);
3206 break;
3207
Douglas Gregora151ba42009-04-14 23:32:43 +00003208 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003209 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003210 break;
3211
3212 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003213 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003214 break;
3215
3216 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003217 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003218 break;
3219
3220 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003221 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003222 break;
3223
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003224 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003225 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003226 break;
3227
Douglas Gregor596e0932009-04-15 16:35:07 +00003228 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003229 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003230 Record[PCHStmtReader::NumExprFields + 1]);
3231 break;
3232
Douglas Gregora151ba42009-04-14 23:32:43 +00003233 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003234 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003235 break;
3236
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003237 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003238 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003239 break;
3240
Douglas Gregor12d74052009-04-15 15:58:59 +00003241 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003242 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003243 break;
3244
3245 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003246 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003247 break;
3248
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003249 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003250 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003251 break;
3252
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003253 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003254 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003255 break;
3256
3257 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003258 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003259 break;
3260
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003261 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003262 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003263 break;
3264
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003265 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003266 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003267 break;
3268
3269 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003270 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003271 break;
3272
Douglas Gregora151ba42009-04-14 23:32:43 +00003273 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003274 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003275 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003276
3277 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003278 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003279 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003280
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003281 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003282 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003283 break;
3284
Douglas Gregorec0b8292009-04-15 23:02:49 +00003285 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003286 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003287 break;
3288
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003289 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003290 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003291 break;
3292
3293 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003294 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003295 Record[PCHStmtReader::NumExprFields] - 1);
3296
3297 break;
3298
3299 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003300 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003301 break;
3302
Douglas Gregorec0b8292009-04-15 23:02:49 +00003303 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003304 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003305 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003306
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003307 case pch::EXPR_ADDR_LABEL:
3308 S = new (Context) AddrLabelExpr(Empty);
3309 break;
3310
Douglas Gregoreca12f62009-04-17 19:05:30 +00003311 case pch::EXPR_STMT:
3312 S = new (Context) StmtExpr(Empty);
3313 break;
3314
Douglas Gregor209d4622009-04-15 23:33:31 +00003315 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003316 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003317 break;
3318
3319 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003320 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003321 break;
3322
3323 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003324 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003325 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003326
3327 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003328 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003329 break;
3330
Douglas Gregore246b742009-04-17 19:21:43 +00003331 case pch::EXPR_BLOCK:
3332 S = new (Context) BlockExpr(Empty);
3333 break;
3334
Douglas Gregor725e94b2009-04-16 00:01:45 +00003335 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003336 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003337 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003338
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003339 case pch::EXPR_OBJC_STRING_LITERAL:
3340 S = new (Context) ObjCStringLiteral(Empty);
3341 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003342 case pch::EXPR_OBJC_ENCODE:
3343 S = new (Context) ObjCEncodeExpr(Empty);
3344 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003345 case pch::EXPR_OBJC_SELECTOR_EXPR:
3346 S = new (Context) ObjCSelectorExpr(Empty);
3347 break;
3348 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3349 S = new (Context) ObjCProtocolExpr(Empty);
3350 break;
Steve Narofffb3e4022009-04-25 14:04:28 +00003351 case pch::EXPR_OBJC_MESSAGE_EXPR:
3352 S = new (Context) ObjCMessageExpr(Empty);
3353 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003354 }
3355
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003356 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003357 if (Finished)
3358 break;
3359
Douglas Gregor456e0952009-04-17 22:13:46 +00003360 ++NumStatementsRead;
3361
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003362 if (S) {
3363 unsigned NumSubStmts = Reader.Visit(S);
3364 while (NumSubStmts > 0) {
3365 StmtStack.pop_back();
3366 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003367 }
3368 }
3369
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003370 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003371 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003372 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003373 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003374 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003375 return StmtStack.back();
3376}
3377
3378Expr *PCHReader::ReadExpr() {
3379 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003380}
3381
Douglas Gregor179cfb12009-04-10 20:39:37 +00003382DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003383 return Diag(SourceLocation(), DiagID);
3384}
3385
3386DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3387 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003388 Context.getSourceManager()),
3389 DiagID);
3390}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003391
Douglas Gregorc713da92009-04-21 22:25:48 +00003392/// \brief Retrieve the identifier table associated with the
3393/// preprocessor.
3394IdentifierTable &PCHReader::getIdentifierTable() {
3395 return PP.getIdentifierTable();
3396}
3397
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003398/// \brief Record that the given ID maps to the given switch-case
3399/// statement.
3400void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3401 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3402 SwitchCaseStmts[ID] = SC;
3403}
3404
3405/// \brief Retrieve the switch-case statement with the given ID.
3406SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3407 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3408 return SwitchCaseStmts[ID];
3409}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003410
3411/// \brief Record that the given label statement has been
3412/// deserialized and has the given ID.
3413void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3414 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3415 "Deserialized label twice");
3416 LabelStmts[ID] = S;
3417
3418 // If we've already seen any goto statements that point to this
3419 // label, resolve them now.
3420 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3421 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3422 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3423 Goto->second->setLabel(S);
3424 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003425
3426 // If we've already seen any address-label statements that point to
3427 // this label, resolve them now.
3428 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3429 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3430 = UnresolvedAddrLabelExprs.equal_range(ID);
3431 for (AddrLabelIter AddrLabel = AddrLabels.first;
3432 AddrLabel != AddrLabels.second; ++AddrLabel)
3433 AddrLabel->second->setLabel(S);
3434 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003435}
3436
3437/// \brief Set the label of the given statement to the label
3438/// identified by ID.
3439///
3440/// Depending on the order in which the label and other statements
3441/// referencing that label occur, this operation may complete
3442/// immediately (updating the statement) or it may queue the
3443/// statement to be back-patched later.
3444void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3445 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3446 if (Label != LabelStmts.end()) {
3447 // We've already seen this label, so set the label of the goto and
3448 // we're done.
3449 S->setLabel(Label->second);
3450 } else {
3451 // We haven't seen this label yet, so add this goto to the set of
3452 // unresolved goto statements.
3453 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3454 }
3455}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003456
3457/// \brief Set the label of the given expression to the label
3458/// identified by ID.
3459///
3460/// Depending on the order in which the label and other statements
3461/// referencing that label occur, this operation may complete
3462/// immediately (updating the statement) or it may queue the
3463/// statement to be back-patched later.
3464void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3465 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3466 if (Label != LabelStmts.end()) {
3467 // We've already seen this label, so set the label of the
3468 // label-address expression and we're done.
3469 S->setLabel(Label->second);
3470 } else {
3471 // We haven't seen this label yet, so add this label-address
3472 // expression to the set of unresolved label-address expressions.
3473 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3474 }
3475}