blob: 375fcdddf545a9f50676fba7c4e9d7e13fa9567f [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 {
Chris Lattner587788a2009-04-26 20:59:20 +000044 explicit SavedStreamPosition(llvm::BitstreamCursor &Cursor)
45 : Cursor(Cursor), Offset(Cursor.GetCurrentBitNo()) { }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000046
47 ~SavedStreamPosition() {
Chris Lattner587788a2009-04-26 20:59:20 +000048 Cursor.JumpToBit(Offset);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000049 }
50
51 private:
Chris Lattner587788a2009-04-26 20:59:20 +000052 llvm::BitstreamCursor &Cursor;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000053 uint64_t Offset;
54 };
55}
56
Douglas Gregorc34897d2009-04-09 22:27:44 +000057//===----------------------------------------------------------------------===//
58// Declaration deserialization
59//===----------------------------------------------------------------------===//
60namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000061 class VISIBILITY_HIDDEN PCHDeclReader
62 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000063 PCHReader &Reader;
64 const PCHReader::RecordData &Record;
65 unsigned &Idx;
66
67 public:
68 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
69 unsigned &Idx)
70 : Reader(Reader), Record(Record), Idx(Idx) { }
71
72 void VisitDecl(Decl *D);
73 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
74 void VisitNamedDecl(NamedDecl *ND);
75 void VisitTypeDecl(TypeDecl *TD);
76 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000077 void VisitTagDecl(TagDecl *TD);
78 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000079 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000080 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000081 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000082 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000083 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000084 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000085 void VisitParmVarDecl(ParmVarDecl *PD);
86 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000087 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
88 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000089 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000090 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000091 void VisitObjCContainerDecl(ObjCContainerDecl *D);
92 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
93 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000094 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
95 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
96 void VisitObjCClassDecl(ObjCClassDecl *D);
97 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
98 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
99 void VisitObjCImplDecl(ObjCImplDecl *D);
100 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
101 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
102 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
103 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
104 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000105 };
106}
107
108void PCHDeclReader::VisitDecl(Decl *D) {
109 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
110 D->setLexicalDeclContext(
111 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
112 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
113 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000114 if (Record[Idx++])
115 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000116 D->setImplicit(Record[Idx++]);
117 D->setAccess((AccessSpecifier)Record[Idx++]);
118}
119
120void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
121 VisitDecl(TU);
122}
123
124void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
125 VisitDecl(ND);
126 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
127}
128
129void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
130 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000131 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
132}
133
134void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000135 // Note that we cannot use VisitTypeDecl here, because we need to
136 // set the underlying type of the typedef *before* we try to read
137 // the type associated with the TypedefDecl.
138 VisitNamedDecl(TD);
139 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
140 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
141 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000142}
143
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000144void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
145 VisitTypeDecl(TD);
146 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
147 TD->setDefinition(Record[Idx++]);
148 TD->setTypedefForAnonDecl(
149 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
150}
151
152void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
153 VisitTagDecl(ED);
154 ED->setIntegerType(Reader.GetType(Record[Idx++]));
155}
156
Douglas Gregor982365e2009-04-13 21:20:57 +0000157void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
158 VisitTagDecl(RD);
159 RD->setHasFlexibleArrayMember(Record[Idx++]);
160 RD->setAnonymousStructOrUnion(Record[Idx++]);
161}
162
Douglas Gregorc34897d2009-04-09 22:27:44 +0000163void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
164 VisitNamedDecl(VD);
165 VD->setType(Reader.GetType(Record[Idx++]));
166}
167
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000168void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
169 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000170 if (Record[Idx++])
171 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000172 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
173}
174
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000175void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
176 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000177 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000178 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179 FD->setPreviousDeclaration(
180 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
181 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
182 FD->setInline(Record[Idx++]);
Douglas Gregor9b6348d2009-04-23 18:22:55 +0000183 FD->setC99InlineDefinition(Record[Idx++]);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000184 FD->setVirtual(Record[Idx++]);
185 FD->setPure(Record[Idx++]);
186 FD->setInheritedPrototype(Record[Idx++]);
187 FD->setHasPrototype(Record[Idx++]);
188 FD->setDeleted(Record[Idx++]);
189 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
190 unsigned NumParams = Record[Idx++];
191 llvm::SmallVector<ParmVarDecl *, 16> Params;
192 Params.reserve(NumParams);
193 for (unsigned I = 0; I != NumParams; ++I)
194 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
195 FD->setParams(Reader.getContext(), &Params[0], NumParams);
196}
197
Steve Naroff79ea0e02009-04-20 15:06:07 +0000198void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
199 VisitNamedDecl(MD);
200 if (Record[Idx++]) {
201 // In practice, this won't be executed (since method definitions
202 // don't occur in header files).
203 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
204 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
205 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
206 }
207 MD->setInstanceMethod(Record[Idx++]);
208 MD->setVariadic(Record[Idx++]);
209 MD->setSynthesized(Record[Idx++]);
210 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
211 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
212 MD->setResultType(Reader.GetType(Record[Idx++]));
213 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
214 unsigned NumParams = Record[Idx++];
215 llvm::SmallVector<ParmVarDecl *, 16> Params;
216 Params.reserve(NumParams);
217 for (unsigned I = 0; I != NumParams; ++I)
218 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
219 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
220}
221
Steve Naroff7333b492009-04-20 20:09:33 +0000222void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
223 VisitNamedDecl(CD);
224 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
225}
226
227void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
228 VisitObjCContainerDecl(ID);
229 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000230 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
231 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000232 unsigned NumProtocols = Record[Idx++];
233 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
234 Protocols.reserve(NumProtocols);
235 for (unsigned I = 0; I != NumProtocols; ++I)
236 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor5efc1052009-04-24 22:01:00 +0000237 ID->setProtocolList(&Protocols[0], NumProtocols, Reader.getContext());
Steve Naroff7333b492009-04-20 20:09:33 +0000238 unsigned NumIvars = Record[Idx++];
239 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
240 IVars.reserve(NumIvars);
241 for (unsigned I = 0; I != NumIvars; ++I)
242 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
243 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000244 ID->setCategoryList(
245 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000246 ID->setForwardDecl(Record[Idx++]);
247 ID->setImplicitInterfaceDecl(Record[Idx++]);
248 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
249 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000250 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000251}
252
253void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
254 VisitFieldDecl(IVD);
255 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
256}
257
Steve Naroff97b53bd2009-04-21 15:12:33 +0000258void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
259 VisitObjCContainerDecl(PD);
260 PD->setForwardDecl(Record[Idx++]);
261 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
262 unsigned NumProtoRefs = Record[Idx++];
263 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
264 ProtoRefs.reserve(NumProtoRefs);
265 for (unsigned I = 0; I != NumProtoRefs; ++I)
266 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
267 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
268}
269
270void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
271 VisitFieldDecl(FD);
272}
273
274void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
275 VisitDecl(CD);
276 unsigned NumClassRefs = Record[Idx++];
277 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
278 ClassRefs.reserve(NumClassRefs);
279 for (unsigned I = 0; I != NumClassRefs; ++I)
280 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
281 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
282}
283
284void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
285 VisitDecl(FPD);
286 unsigned NumProtoRefs = Record[Idx++];
287 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
288 ProtoRefs.reserve(NumProtoRefs);
289 for (unsigned I = 0; I != NumProtoRefs; ++I)
290 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
291 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
292}
293
294void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
295 VisitObjCContainerDecl(CD);
296 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
297 unsigned NumProtoRefs = Record[Idx++];
298 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
299 ProtoRefs.reserve(NumProtoRefs);
300 for (unsigned I = 0; I != NumProtoRefs; ++I)
301 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
302 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000303 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000304 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
305}
306
307void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
308 VisitNamedDecl(CAD);
309 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
310}
311
312void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
313 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000314 D->setType(Reader.GetType(Record[Idx++]));
315 // FIXME: stable encoding
316 D->setPropertyAttributes(
317 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
318 // FIXME: stable encoding
319 D->setPropertyImplementation(
320 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
321 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
323 D->setGetterMethodDecl(
324 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
325 D->setSetterMethodDecl(
326 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setPropertyIvarDecl(
328 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000329}
330
331void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000332 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000333 D->setClassInterface(
334 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
335 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000336}
337
338void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
339 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000340 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000341}
342
343void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
344 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000345 D->setSuperClass(
346 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000347}
348
349
350void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
351 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000352 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
353 D->setPropertyDecl(
354 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
355 D->setPropertyIvarDecl(
356 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000357}
358
Douglas Gregor982365e2009-04-13 21:20:57 +0000359void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
360 VisitValueDecl(FD);
361 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000362 if (Record[Idx++])
363 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000364}
365
Douglas Gregorc34897d2009-04-09 22:27:44 +0000366void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
367 VisitValueDecl(VD);
368 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
369 VD->setThreadSpecified(Record[Idx++]);
370 VD->setCXXDirectInitializer(Record[Idx++]);
371 VD->setDeclaredInCondition(Record[Idx++]);
372 VD->setPreviousDeclaration(
373 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
374 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000375 if (Record[Idx++])
376 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000377}
378
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000379void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
380 VisitVarDecl(PD);
381 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000382 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383}
384
385void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
386 VisitParmVarDecl(PD);
387 PD->setOriginalType(Reader.GetType(Record[Idx++]));
388}
389
Douglas Gregor2a491792009-04-13 22:49:25 +0000390void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
391 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000392 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000393}
394
395void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
396 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000397 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000398 unsigned NumParams = Record[Idx++];
399 llvm::SmallVector<ParmVarDecl *, 16> Params;
400 Params.reserve(NumParams);
401 for (unsigned I = 0; I != NumParams; ++I)
402 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
403 BD->setParams(Reader.getContext(), &Params[0], NumParams);
404}
405
Douglas Gregorc34897d2009-04-09 22:27:44 +0000406std::pair<uint64_t, uint64_t>
407PCHDeclReader::VisitDeclContext(DeclContext *DC) {
408 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000409 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000410 return std::make_pair(LexicalOffset, VisibleOffset);
411}
412
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000413//===----------------------------------------------------------------------===//
414// Statement/expression deserialization
415//===----------------------------------------------------------------------===//
416namespace {
417 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000418 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000419 PCHReader &Reader;
420 const PCHReader::RecordData &Record;
421 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000422 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000423
424 public:
425 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000426 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
427 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000428
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000429 /// \brief The number of record fields required for the Stmt class
430 /// itself.
431 static const unsigned NumStmtFields = 0;
432
Douglas Gregor596e0932009-04-15 16:35:07 +0000433 /// \brief The number of record fields required for the Expr class
434 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000435 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000436
Douglas Gregora151ba42009-04-14 23:32:43 +0000437 // Each of the Visit* functions reads in part of the expression
438 // from the given record and the current expression stack, then
439 // return the total number of operands that it read from the
440 // expression stack.
441
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000442 unsigned VisitStmt(Stmt *S);
443 unsigned VisitNullStmt(NullStmt *S);
444 unsigned VisitCompoundStmt(CompoundStmt *S);
445 unsigned VisitSwitchCase(SwitchCase *S);
446 unsigned VisitCaseStmt(CaseStmt *S);
447 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000448 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000449 unsigned VisitIfStmt(IfStmt *S);
450 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000451 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000452 unsigned VisitDoStmt(DoStmt *S);
453 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000455 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000456 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000457 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000458 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000459 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000460 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000461 unsigned VisitExpr(Expr *E);
462 unsigned VisitPredefinedExpr(PredefinedExpr *E);
463 unsigned VisitDeclRefExpr(DeclRefExpr *E);
464 unsigned VisitIntegerLiteral(IntegerLiteral *E);
465 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000466 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000467 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000468 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000469 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000470 unsigned VisitUnaryOperator(UnaryOperator *E);
471 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000472 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000473 unsigned VisitCallExpr(CallExpr *E);
474 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000475 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000476 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000477 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
478 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000480 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
481 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000482 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000483 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000484 unsigned VisitInitListExpr(InitListExpr *E);
485 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
486 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000488 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000489 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000490 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
491 unsigned VisitChooseExpr(ChooseExpr *E);
492 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000493 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000494 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000495 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000496 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000497 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000498 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
499 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000500 unsigned VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
501 unsigned VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
502 unsigned VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000503 unsigned VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000504 unsigned VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000505
506 unsigned VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
507 unsigned VisitObjCCatchStmt(ObjCAtCatchStmt *);
508 unsigned VisitObjCFinallyStmt(ObjCAtFinallyStmt *);
509 unsigned VisitObjCAtTryStmt(ObjCAtTryStmt *);
510 unsigned VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
511 unsigned VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000512 };
513}
514
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000515unsigned PCHStmtReader::VisitStmt(Stmt *S) {
516 assert(Idx == NumStmtFields && "Incorrect statement field count");
517 return 0;
518}
519
520unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
521 VisitStmt(S);
522 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
523 return 0;
524}
525
526unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
527 VisitStmt(S);
528 unsigned NumStmts = Record[Idx++];
529 S->setStmts(Reader.getContext(),
530 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
531 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
532 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
533 return NumStmts;
534}
535
536unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
537 VisitStmt(S);
538 Reader.RecordSwitchCaseID(S, Record[Idx++]);
539 return 0;
540}
541
542unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
543 VisitSwitchCase(S);
544 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
545 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
546 S->setSubStmt(StmtStack.back());
547 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 return 3;
549}
550
551unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
552 VisitSwitchCase(S);
553 S->setSubStmt(StmtStack.back());
554 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 return 1;
556}
557
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000558unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
559 VisitStmt(S);
560 S->setID(Reader.GetIdentifierInfo(Record, Idx));
561 S->setSubStmt(StmtStack.back());
562 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
563 Reader.RecordLabelStmt(S, Record[Idx++]);
564 return 1;
565}
566
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000567unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
568 VisitStmt(S);
569 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
570 S->setThen(StmtStack[StmtStack.size() - 2]);
571 S->setElse(StmtStack[StmtStack.size() - 1]);
572 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
573 return 3;
574}
575
576unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
577 VisitStmt(S);
578 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
579 S->setBody(StmtStack.back());
580 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 SwitchCase *PrevSC = 0;
582 for (unsigned N = Record.size(); Idx != N; ++Idx) {
583 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
584 if (PrevSC)
585 PrevSC->setNextSwitchCase(SC);
586 else
587 S->setSwitchCaseList(SC);
588 PrevSC = SC;
589 }
590 return 2;
591}
592
Douglas Gregora6b503f2009-04-17 00:16:09 +0000593unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
594 VisitStmt(S);
595 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
596 S->setBody(StmtStack.back());
597 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 return 2;
599}
600
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000601unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
602 VisitStmt(S);
603 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
604 S->setBody(StmtStack.back());
605 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
606 return 2;
607}
608
609unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
610 VisitStmt(S);
611 S->setInit(StmtStack[StmtStack.size() - 4]);
612 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
613 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
614 S->setBody(StmtStack.back());
615 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
616 return 4;
617}
618
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000619unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
620 VisitStmt(S);
621 Reader.SetLabelOf(S, Record[Idx++]);
622 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
624 return 0;
625}
626
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000627unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
628 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000629 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000630 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
631 return 1;
632}
633
Douglas Gregora6b503f2009-04-17 00:16:09 +0000634unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
635 VisitStmt(S);
636 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 0;
638}
639
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000640unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
641 VisitStmt(S);
642 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643 return 0;
644}
645
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000646unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
647 VisitStmt(S);
648 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
649 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
650 return 1;
651}
652
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000653unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
654 VisitStmt(S);
655 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
656 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
657
658 if (Idx + 1 == Record.size()) {
659 // Single declaration
660 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
661 } else {
662 llvm::SmallVector<Decl *, 16> Decls;
663 Decls.reserve(Record.size() - Idx);
664 for (unsigned N = Record.size(); Idx != N; ++Idx)
665 Decls.push_back(Reader.GetDecl(Record[Idx]));
666 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
667 &Decls[0], Decls.size())));
668 }
669 return 0;
670}
671
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000672unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
673 VisitStmt(S);
674 unsigned NumOutputs = Record[Idx++];
675 unsigned NumInputs = Record[Idx++];
676 unsigned NumClobbers = Record[Idx++];
677 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
678 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
679 S->setVolatile(Record[Idx++]);
680 S->setSimple(Record[Idx++]);
681
682 unsigned StackIdx
683 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
684 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
685
686 // Outputs and inputs
687 llvm::SmallVector<std::string, 16> Names;
688 llvm::SmallVector<StringLiteral*, 16> Constraints;
689 llvm::SmallVector<Stmt*, 16> Exprs;
690 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
691 Names.push_back(Reader.ReadString(Record, Idx));
692 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
693 Exprs.push_back(StmtStack[StackIdx++]);
694 }
695 S->setOutputsAndInputs(NumOutputs, NumInputs,
696 &Names[0], &Constraints[0], &Exprs[0]);
697
698 // Constraints
699 llvm::SmallVector<StringLiteral*, 16> Clobbers;
700 for (unsigned I = 0; I != NumClobbers; ++I)
701 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
702 S->setClobbers(&Clobbers[0], NumClobbers);
703
704 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
705 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
706}
707
Douglas Gregora151ba42009-04-14 23:32:43 +0000708unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000709 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000710 E->setType(Reader.GetType(Record[Idx++]));
711 E->setTypeDependent(Record[Idx++]);
712 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000713 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000714 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000715}
716
Douglas Gregora151ba42009-04-14 23:32:43 +0000717unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000718 VisitExpr(E);
719 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
720 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000721 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000722}
723
Douglas Gregora151ba42009-04-14 23:32:43 +0000724unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000725 VisitExpr(E);
726 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
727 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000728 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000729}
730
Douglas Gregora151ba42009-04-14 23:32:43 +0000731unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000732 VisitExpr(E);
733 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
734 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000735 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000736}
737
Douglas Gregora151ba42009-04-14 23:32:43 +0000738unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000739 VisitExpr(E);
740 E->setValue(Reader.ReadAPFloat(Record, Idx));
741 E->setExact(Record[Idx++]);
742 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000743 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000744}
745
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000746unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
747 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000748 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000749 return 1;
750}
751
Douglas Gregor596e0932009-04-15 16:35:07 +0000752unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
753 VisitExpr(E);
754 unsigned Len = Record[Idx++];
755 assert(Record[Idx] == E->getNumConcatenated() &&
756 "Wrong number of concatenated tokens!");
757 ++Idx;
758 E->setWide(Record[Idx++]);
759
760 // Read string data
761 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
762 E->setStrData(Reader.getContext(), &Str[0], Len);
763 Idx += Len;
764
765 // Read source locations
766 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
767 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
768
769 return 0;
770}
771
Douglas Gregora151ba42009-04-14 23:32:43 +0000772unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000773 VisitExpr(E);
774 E->setValue(Record[Idx++]);
775 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
776 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000777 return 0;
778}
779
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000780unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
781 VisitExpr(E);
782 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
783 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000784 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000785 return 1;
786}
787
Douglas Gregor12d74052009-04-15 15:58:59 +0000788unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
789 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000790 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000791 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
792 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 return 1;
794}
795
796unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
797 VisitExpr(E);
798 E->setSizeof(Record[Idx++]);
799 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000800 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000801 ++Idx;
802 } else {
803 E->setArgument(Reader.GetType(Record[Idx++]));
804 }
805 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
807 return E->isArgumentType()? 0 : 1;
808}
809
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000810unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
811 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000812 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Steve Naroff315ec172009-04-25 15:19:54 +0000813 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000814 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
815 return 2;
816}
817
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000818unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
819 VisitExpr(E);
820 E->setNumArgs(Reader.getContext(), Record[Idx++]);
821 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000822 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000823 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000824 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000825 return E->getNumArgs() + 1;
826}
827
828unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
829 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000830 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000831 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
832 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
833 E->setArrow(Record[Idx++]);
834 return 1;
835}
836
Douglas Gregora151ba42009-04-14 23:32:43 +0000837unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
838 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000839 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000840 return 1;
841}
842
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000843unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
844 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000845 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
846 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000847 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
848 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
849 return 2;
850}
851
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000852unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
853 VisitBinaryOperator(E);
854 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
855 E->setComputationResultType(Reader.GetType(Record[Idx++]));
856 return 2;
857}
858
859unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
860 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000861 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
862 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
863 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000864 return 3;
865}
866
Douglas Gregora151ba42009-04-14 23:32:43 +0000867unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
868 VisitCastExpr(E);
869 E->setLvalueCast(Record[Idx++]);
870 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000871}
872
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000873unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
874 VisitCastExpr(E);
875 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
876 return 1;
877}
878
879unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
880 VisitExplicitCastExpr(E);
881 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
882 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
883 return 1;
884}
885
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000886unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
887 VisitExpr(E);
888 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000889 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000890 E->setFileScope(Record[Idx++]);
891 return 1;
892}
893
Douglas Gregorec0b8292009-04-15 23:02:49 +0000894unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
895 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000896 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000897 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
898 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 return 1;
900}
901
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000902unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
903 VisitExpr(E);
904 unsigned NumInits = Record[Idx++];
905 E->reserveInits(NumInits);
906 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000907 E->updateInit(I,
908 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
909 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000910 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
911 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
912 E->setInitializedFieldInUnion(
913 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
914 E->sawArrayRangeDesignator(Record[Idx++]);
915 return NumInits + 1;
916}
917
918unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
919 typedef DesignatedInitExpr::Designator Designator;
920
921 VisitExpr(E);
922 unsigned NumSubExprs = Record[Idx++];
923 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
924 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000925 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000926 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
927 E->setGNUSyntax(Record[Idx++]);
928
929 llvm::SmallVector<Designator, 4> Designators;
930 while (Idx < Record.size()) {
931 switch ((pch::DesignatorTypes)Record[Idx++]) {
932 case pch::DESIG_FIELD_DECL: {
933 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
934 SourceLocation DotLoc
935 = SourceLocation::getFromRawEncoding(Record[Idx++]);
936 SourceLocation FieldLoc
937 = SourceLocation::getFromRawEncoding(Record[Idx++]);
938 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
939 FieldLoc));
940 Designators.back().setField(Field);
941 break;
942 }
943
944 case pch::DESIG_FIELD_NAME: {
945 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
946 SourceLocation DotLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 SourceLocation FieldLoc
949 = SourceLocation::getFromRawEncoding(Record[Idx++]);
950 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
951 break;
952 }
953
954 case pch::DESIG_ARRAY: {
955 unsigned Index = Record[Idx++];
956 SourceLocation LBracketLoc
957 = SourceLocation::getFromRawEncoding(Record[Idx++]);
958 SourceLocation RBracketLoc
959 = SourceLocation::getFromRawEncoding(Record[Idx++]);
960 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
961 break;
962 }
963
964 case pch::DESIG_ARRAY_RANGE: {
965 unsigned Index = Record[Idx++];
966 SourceLocation LBracketLoc
967 = SourceLocation::getFromRawEncoding(Record[Idx++]);
968 SourceLocation EllipsisLoc
969 = SourceLocation::getFromRawEncoding(Record[Idx++]);
970 SourceLocation RBracketLoc
971 = SourceLocation::getFromRawEncoding(Record[Idx++]);
972 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
973 RBracketLoc));
974 break;
975 }
976 }
977 }
978 E->setDesignators(&Designators[0], Designators.size());
979
980 return NumSubExprs;
981}
982
983unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
984 VisitExpr(E);
985 return 0;
986}
987
Douglas Gregorec0b8292009-04-15 23:02:49 +0000988unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
989 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000990 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000991 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 return 1;
994}
995
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000996unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
997 VisitExpr(E);
998 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
999 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1000 Reader.SetLabelOf(E, Record[Idx++]);
1001 return 0;
1002}
1003
Douglas Gregoreca12f62009-04-17 19:05:30 +00001004unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
1005 VisitExpr(E);
1006 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1007 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1008 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
1009 return 1;
1010}
1011
Douglas Gregor209d4622009-04-15 23:33:31 +00001012unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1013 VisitExpr(E);
1014 E->setArgType1(Reader.GetType(Record[Idx++]));
1015 E->setArgType2(Reader.GetType(Record[Idx++]));
1016 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1017 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1018 return 0;
1019}
1020
1021unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1022 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001023 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1024 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1025 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001026 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1028 return 3;
1029}
1030
1031unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1032 VisitExpr(E);
1033 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1034 return 0;
1035}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001036
Douglas Gregor725e94b2009-04-16 00:01:45 +00001037unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1038 VisitExpr(E);
1039 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001040 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001041 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1042 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1043 return NumExprs;
1044}
1045
Douglas Gregore246b742009-04-17 19:21:43 +00001046unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1047 VisitExpr(E);
1048 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1049 E->setHasBlockDeclRefExprs(Record[Idx++]);
1050 return 0;
1051}
1052
Douglas Gregor725e94b2009-04-16 00:01:45 +00001053unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1054 VisitExpr(E);
1055 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1056 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1057 E->setByRef(Record[Idx++]);
1058 return 0;
1059}
1060
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001061//===----------------------------------------------------------------------===//
1062// Objective-C Expressions and Statements
1063
1064unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1065 VisitExpr(E);
1066 E->setString(cast<StringLiteral>(StmtStack.back()));
1067 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 return 1;
1069}
1070
Chris Lattner80f83c62009-04-22 05:57:30 +00001071unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1072 VisitExpr(E);
1073 E->setEncodedType(Reader.GetType(Record[Idx++]));
1074 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1075 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1076 return 0;
1077}
1078
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001079unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1080 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001081 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001082 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1083 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1084 return 0;
1085}
1086
1087unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1088 VisitExpr(E);
1089 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1090 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1091 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1092 return 0;
1093}
1094
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001095unsigned PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1096 VisitExpr(E);
1097 E->setDecl(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
1098 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1099 E->setBase(cast<Expr>(StmtStack.back()));
1100 E->setIsArrow(Record[Idx++]);
1101 E->setIsFreeIvar(Record[Idx++]);
1102 return 1;
1103}
1104
1105unsigned PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1106 VisitExpr(E);
1107 E->setProperty(cast<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
1108 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1109 E->setBase(cast<Expr>(StmtStack.back()));
1110 return 1;
1111}
1112
1113unsigned PCHStmtReader::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
1114 VisitExpr(E);
1115 E->setGetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1116 E->setSetterMethod(cast<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
1117 E->setClassProp(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
1118 E->setBase(cast<Expr>(StmtStack.back()));
1119 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1120 E->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1121 return 1;
1122}
1123
Steve Narofffb3e4022009-04-25 14:04:28 +00001124unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1125 VisitExpr(E);
1126 E->setNumArgs(Record[Idx++]);
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001127 E->setLeftLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1128 E->setRightLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Narofffb3e4022009-04-25 14:04:28 +00001129 E->setSelector(Reader.GetSelector(Record, Idx));
1130 E->setMethodDecl(cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001131
1132 ObjCMessageExpr::ClassInfo CI;
1133 CI.first = cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]));
1134 CI.second = Reader.GetIdentifierInfo(Record, Idx);
1135 if (E->getMethodDecl() == 0)
1136 E->setClassInfo(CI);
1137
Steve Narofffb3e4022009-04-25 14:04:28 +00001138 E->setReceiver(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
1139 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1140 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
1141 return E->getNumArgs() + 1;
1142}
1143
Chris Lattnerc0478bf2009-04-26 00:44:05 +00001144unsigned PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) {
1145 VisitExpr(E);
1146 E->setLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1147 return 0;
1148}
Chris Lattner80f83c62009-04-22 05:57:30 +00001149
Steve Naroff79762bd2009-04-26 18:52:16 +00001150unsigned PCHStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1151 VisitStmt(S);
1152 S->setElement(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1153 S->setCollection(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1154 S->setBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1155 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1156 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1157 return 3;
1158}
1159
1160unsigned PCHStmtReader::VisitObjCCatchStmt(ObjCAtCatchStmt *S) {
1161 VisitStmt(S);
1162 S->setCatchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1163 S->setNextCatchStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1164 S->setCatchParamDecl(cast_or_null<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
1165 S->setAtCatchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1166 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1167 return 2;
1168}
1169
1170unsigned PCHStmtReader::VisitObjCFinallyStmt(ObjCAtFinallyStmt *S) {
1171 VisitStmt(S);
1172 S->setFinallyBody(StmtStack.back());
1173 S->setAtFinallyLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1174 return 1;
1175}
1176
1177unsigned PCHStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1178 VisitStmt(S);
1179 S->setTryBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3]));
1180 S->setCatchStmts(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1181 S->setFinallyStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1182 S->setAtTryLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1183 return 3;
1184}
1185
1186unsigned PCHStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1187 VisitStmt(S);
1188 S->setSynchExpr(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2]));
1189 S->setSynchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1]));
1190 S->setAtSynchronizedLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1191 return 2;
1192}
1193
1194unsigned PCHStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1195 VisitStmt(S);
1196 S->setThrowExpr(StmtStack.back());
1197 S->setThrowLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1198 return 1;
1199}
1200
Douglas Gregorc713da92009-04-21 22:25:48 +00001201//===----------------------------------------------------------------------===//
1202// PCH reader implementation
1203//===----------------------------------------------------------------------===//
1204
1205namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001206class VISIBILITY_HIDDEN PCHMethodPoolLookupTrait {
1207 PCHReader &Reader;
1208
1209public:
1210 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1211
1212 typedef Selector external_key_type;
1213 typedef external_key_type internal_key_type;
1214
1215 explicit PCHMethodPoolLookupTrait(PCHReader &Reader) : Reader(Reader) { }
1216
1217 static bool EqualKey(const internal_key_type& a,
1218 const internal_key_type& b) {
1219 return a == b;
1220 }
1221
1222 static unsigned ComputeHash(Selector Sel) {
1223 unsigned N = Sel.getNumArgs();
1224 if (N == 0)
1225 ++N;
1226 unsigned R = 5381;
1227 for (unsigned I = 0; I != N; ++I)
1228 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1229 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1230 return R;
1231 }
1232
1233 // This hopefully will just get inlined and removed by the optimizer.
1234 static const internal_key_type&
1235 GetInternalKey(const external_key_type& x) { return x; }
1236
1237 static std::pair<unsigned, unsigned>
1238 ReadKeyDataLength(const unsigned char*& d) {
1239 using namespace clang::io;
1240 unsigned KeyLen = ReadUnalignedLE16(d);
1241 unsigned DataLen = ReadUnalignedLE16(d);
1242 return std::make_pair(KeyLen, DataLen);
1243 }
1244
Douglas Gregor2d711832009-04-25 17:48:32 +00001245 internal_key_type ReadKey(const unsigned char* d, unsigned) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001246 using namespace clang::io;
1247 SelectorTable &SelTable = Reader.getContext().Selectors;
1248 unsigned N = ReadUnalignedLE16(d);
1249 IdentifierInfo *FirstII
1250 = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d));
1251 if (N == 0)
1252 return SelTable.getNullarySelector(FirstII);
1253 else if (N == 1)
1254 return SelTable.getUnarySelector(FirstII);
1255
1256 llvm::SmallVector<IdentifierInfo *, 16> Args;
1257 Args.push_back(FirstII);
1258 for (unsigned I = 1; I != N; ++I)
1259 Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)));
1260
1261 return SelTable.getSelector(N, &Args[0]);
1262 }
1263
1264 data_type ReadData(Selector, const unsigned char* d, unsigned DataLen) {
1265 using namespace clang::io;
1266 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
1267 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
1268
1269 data_type Result;
1270
1271 // Load instance methods
1272 ObjCMethodList *Prev = 0;
1273 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
1274 ObjCMethodDecl *Method
1275 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1276 if (!Result.first.Method) {
1277 // This is the first method, which is the easy case.
1278 Result.first.Method = Method;
1279 Prev = &Result.first;
1280 continue;
1281 }
1282
1283 Prev->Next = new ObjCMethodList(Method, 0);
1284 Prev = Prev->Next;
1285 }
1286
1287 // Load factory methods
1288 Prev = 0;
1289 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
1290 ObjCMethodDecl *Method
1291 = cast<ObjCMethodDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
1292 if (!Result.second.Method) {
1293 // This is the first method, which is the easy case.
1294 Result.second.Method = Method;
1295 Prev = &Result.second;
1296 continue;
1297 }
1298
1299 Prev->Next = new ObjCMethodList(Method, 0);
1300 Prev = Prev->Next;
1301 }
1302
1303 return Result;
1304 }
1305};
1306
1307} // end anonymous namespace
1308
1309/// \brief The on-disk hash table used for the global method pool.
1310typedef OnDiskChainedHashTable<PCHMethodPoolLookupTrait>
1311 PCHMethodPoolLookupTable;
1312
1313namespace {
Douglas Gregorc713da92009-04-21 22:25:48 +00001314class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1315 PCHReader &Reader;
1316
1317 // If we know the IdentifierInfo in advance, it is here and we will
1318 // not build a new one. Used when deserializing information about an
1319 // identifier that was constructed before the PCH file was read.
1320 IdentifierInfo *KnownII;
1321
1322public:
1323 typedef IdentifierInfo * data_type;
1324
1325 typedef const std::pair<const char*, unsigned> external_key_type;
1326
1327 typedef external_key_type internal_key_type;
1328
1329 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1330 : Reader(Reader), KnownII(II) { }
1331
1332 static bool EqualKey(const internal_key_type& a,
1333 const internal_key_type& b) {
1334 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1335 : false;
1336 }
1337
1338 static unsigned ComputeHash(const internal_key_type& a) {
1339 return BernsteinHash(a.first, a.second);
1340 }
1341
1342 // This hopefully will just get inlined and removed by the optimizer.
1343 static const internal_key_type&
1344 GetInternalKey(const external_key_type& x) { return x; }
1345
1346 static std::pair<unsigned, unsigned>
1347 ReadKeyDataLength(const unsigned char*& d) {
1348 using namespace clang::io;
Douglas Gregor4bb24882009-04-25 20:26:24 +00001349 unsigned DataLen = ReadUnalignedLE16(d);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001350 unsigned KeyLen = ReadUnalignedLE16(d);
Douglas Gregorc713da92009-04-21 22:25:48 +00001351 return std::make_pair(KeyLen, DataLen);
1352 }
1353
1354 static std::pair<const char*, unsigned>
1355 ReadKey(const unsigned char* d, unsigned n) {
1356 assert(n >= 2 && d[n-1] == '\0');
1357 return std::make_pair((const char*) d, n-1);
1358 }
1359
1360 IdentifierInfo *ReadData(const internal_key_type& k,
1361 const unsigned char* d,
1362 unsigned DataLen) {
1363 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001364 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001365 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1366 Bits >>= 1;
1367 bool Poisoned = Bits & 0x01;
1368 Bits >>= 1;
1369 bool ExtensionToken = Bits & 0x01;
1370 Bits >>= 1;
1371 bool hasMacroDefinition = Bits & 0x01;
1372 Bits >>= 1;
1373 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1374 Bits >>= 10;
1375 unsigned TokenID = Bits & 0xFF;
1376 Bits >>= 8;
1377
Douglas Gregorc713da92009-04-21 22:25:48 +00001378 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001379 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001380 DataLen -= 8;
1381
1382 // Build the IdentifierInfo itself and link the identifier ID with
1383 // the new IdentifierInfo.
1384 IdentifierInfo *II = KnownII;
1385 if (!II)
Douglas Gregor4bb24882009-04-25 20:26:24 +00001386 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1387 k.first, k.first + k.second);
Douglas Gregorc713da92009-04-21 22:25:48 +00001388 Reader.SetIdentifierInfo(ID, II);
1389
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001390 // Set or check the various bits in the IdentifierInfo structure.
1391 // FIXME: Load token IDs lazily, too?
1392 assert((unsigned)II->getTokenID() == TokenID &&
1393 "Incorrect token ID loaded");
1394 (void)TokenID;
1395 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1396 assert(II->isExtensionToken() == ExtensionToken &&
1397 "Incorrect extension token flag");
1398 (void)ExtensionToken;
1399 II->setIsPoisoned(Poisoned);
1400 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1401 "Incorrect C++ operator keyword flag");
1402 (void)CPlusPlusOperatorKeyword;
1403
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001404 // If this identifier is a macro, deserialize the macro
1405 // definition.
1406 if (hasMacroDefinition) {
1407 uint32_t Offset = ReadUnalignedLE64(d);
1408 Reader.ReadMacroRecord(Offset);
1409 DataLen -= 8;
1410 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001411
1412 // Read all of the declarations visible at global scope with this
1413 // name.
1414 Sema *SemaObj = Reader.getSema();
1415 while (DataLen > 0) {
1416 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001417 if (SemaObj) {
1418 // Introduce this declaration into the translation-unit scope
1419 // and add it to the declaration chain for this identifier, so
1420 // that (unqualified) name lookup will find it.
1421 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1422 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1423 } else {
1424 // Queue this declaration so that it will be added to the
1425 // translation unit scope and identifier's declaration chain
1426 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001427 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001428 }
1429
1430 DataLen -= 4;
1431 }
1432 return II;
1433 }
1434};
1435
1436} // end anonymous namespace
1437
1438/// \brief The on-disk hash table used to contain information about
1439/// all of the identifiers in the program.
1440typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1441 PCHIdentifierLookupTable;
1442
Douglas Gregorc34897d2009-04-09 22:27:44 +00001443// FIXME: use the diagnostics machinery
1444static bool Error(const char *Str) {
1445 std::fprintf(stderr, "%s\n", Str);
1446 return true;
1447}
1448
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001449/// \brief Check the contents of the predefines buffer against the
1450/// contents of the predefines buffer used to build the PCH file.
1451///
1452/// The contents of the two predefines buffers should be the same. If
1453/// not, then some command-line option changed the preprocessor state
1454/// and we must reject the PCH file.
1455///
1456/// \param PCHPredef The start of the predefines buffer in the PCH
1457/// file.
1458///
1459/// \param PCHPredefLen The length of the predefines buffer in the PCH
1460/// file.
1461///
1462/// \param PCHBufferID The FileID for the PCH predefines buffer.
1463///
1464/// \returns true if there was a mismatch (in which case the PCH file
1465/// should be ignored), or false otherwise.
1466bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1467 unsigned PCHPredefLen,
1468 FileID PCHBufferID) {
1469 const char *Predef = PP.getPredefines().c_str();
1470 unsigned PredefLen = PP.getPredefines().size();
1471
1472 // If the two predefines buffers compare equal, we're done!.
1473 if (PredefLen == PCHPredefLen &&
1474 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1475 return false;
1476
1477 // The predefines buffers are different. Produce a reasonable
1478 // diagnostic showing where they are different.
1479
1480 // The source locations (potentially in the two different predefines
1481 // buffers)
1482 SourceLocation Loc1, Loc2;
1483 SourceManager &SourceMgr = PP.getSourceManager();
1484
1485 // Create a source buffer for our predefines string, so
1486 // that we can build a diagnostic that points into that
1487 // source buffer.
1488 FileID BufferID;
1489 if (Predef && Predef[0]) {
1490 llvm::MemoryBuffer *Buffer
1491 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1492 "<built-in>");
1493 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1494 }
1495
1496 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1497 std::pair<const char *, const char *> Locations
1498 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1499
1500 if (Locations.first != Predef + MinLen) {
1501 // We found the location in the two buffers where there is a
1502 // difference. Form source locations to point there (in both
1503 // buffers).
1504 unsigned Offset = Locations.first - Predef;
1505 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1506 .getFileLocWithOffset(Offset);
1507 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1508 .getFileLocWithOffset(Offset);
1509 } else if (PredefLen > PCHPredefLen) {
1510 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1511 .getFileLocWithOffset(MinLen);
1512 } else {
1513 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1514 .getFileLocWithOffset(MinLen);
1515 }
1516
1517 Diag(Loc1, diag::warn_pch_preprocessor);
1518 if (Loc2.isValid())
1519 Diag(Loc2, diag::note_predef_in_pch);
1520 Diag(diag::note_ignoring_pch) << FileName;
1521 return true;
1522}
1523
Douglas Gregor635f97f2009-04-13 16:31:14 +00001524/// \brief Read the line table in the source manager block.
1525/// \returns true if ther was an error.
1526static bool ParseLineTable(SourceManager &SourceMgr,
1527 llvm::SmallVectorImpl<uint64_t> &Record) {
1528 unsigned Idx = 0;
1529 LineTableInfo &LineTable = SourceMgr.getLineTable();
1530
1531 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001532 std::map<int, int> FileIDs;
1533 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001534 // Extract the file name
1535 unsigned FilenameLen = Record[Idx++];
1536 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1537 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001538 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1539 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001540 }
1541
1542 // Parse the line entries
1543 std::vector<LineEntry> Entries;
1544 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001545 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001546
1547 // Extract the line entries
1548 unsigned NumEntries = Record[Idx++];
1549 Entries.clear();
1550 Entries.reserve(NumEntries);
1551 for (unsigned I = 0; I != NumEntries; ++I) {
1552 unsigned FileOffset = Record[Idx++];
1553 unsigned LineNo = Record[Idx++];
1554 int FilenameID = Record[Idx++];
1555 SrcMgr::CharacteristicKind FileKind
1556 = (SrcMgr::CharacteristicKind)Record[Idx++];
1557 unsigned IncludeOffset = Record[Idx++];
1558 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1559 FileKind, IncludeOffset));
1560 }
1561 LineTable.AddEntry(FID, Entries);
1562 }
1563
1564 return false;
1565}
1566
Douglas Gregorab1cef72009-04-10 03:52:48 +00001567/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001568PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001569 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001570 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1571 Error("Malformed source manager block record");
1572 return Failure;
1573 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001574
1575 SourceManager &SourceMgr = Context.getSourceManager();
1576 RecordData Record;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001577 unsigned NumHeaderInfos = 0;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001578 while (true) {
1579 unsigned Code = Stream.ReadCode();
1580 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001581 if (Stream.ReadBlockEnd()) {
1582 Error("Error at end of Source Manager block");
1583 return Failure;
1584 }
1585
1586 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001587 }
1588
1589 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1590 // No known subblocks, always skip them.
1591 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001592 if (Stream.SkipBlock()) {
1593 Error("Malformed block record");
1594 return Failure;
1595 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001596 continue;
1597 }
1598
1599 if (Code == llvm::bitc::DEFINE_ABBREV) {
1600 Stream.ReadAbbrevRecord();
1601 continue;
1602 }
1603
1604 // Read a record.
1605 const char *BlobStart;
1606 unsigned BlobLen;
1607 Record.clear();
1608 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1609 default: // Default behavior: ignore.
1610 break;
1611
1612 case pch::SM_SLOC_FILE_ENTRY: {
1613 // FIXME: We would really like to delay the creation of this
1614 // FileEntry until it is actually required, e.g., when producing
1615 // a diagnostic with a source location in this file.
1616 const FileEntry *File
1617 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1618 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001619 FileID ID = SourceMgr.createFileID(File,
1620 SourceLocation::getFromRawEncoding(Record[1]),
1621 (CharacteristicKind)Record[2]);
1622 if (Record[3])
1623 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1624 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001625 break;
1626 }
1627
1628 case pch::SM_SLOC_BUFFER_ENTRY: {
1629 const char *Name = BlobStart;
1630 unsigned Code = Stream.ReadCode();
1631 Record.clear();
1632 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1633 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001634 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001635 llvm::MemoryBuffer *Buffer
1636 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1637 BlobStart + BlobLen - 1,
1638 Name);
1639 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1640
1641 if (strcmp(Name, "<built-in>") == 0
1642 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1643 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001644 break;
1645 }
1646
1647 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1648 SourceLocation SpellingLoc
1649 = SourceLocation::getFromRawEncoding(Record[1]);
1650 SourceMgr.createInstantiationLoc(
1651 SpellingLoc,
1652 SourceLocation::getFromRawEncoding(Record[2]),
1653 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001654 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001655 break;
1656 }
1657
Chris Lattnere1be6022009-04-14 23:22:57 +00001658 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001659 if (ParseLineTable(SourceMgr, Record))
1660 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001661 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001662
1663 case pch::SM_HEADER_FILE_INFO: {
1664 HeaderFileInfo HFI;
1665 HFI.isImport = Record[0];
1666 HFI.DirInfo = Record[1];
1667 HFI.NumIncludes = Record[2];
1668 HFI.ControllingMacroID = Record[3];
1669 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1670 break;
1671 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001672 }
1673 }
1674}
1675
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001676void PCHReader::ReadMacroRecord(uint64_t Offset) {
1677 // Keep track of where we are in the stream, then jump back there
1678 // after reading this macro.
1679 SavedStreamPosition SavedPosition(Stream);
1680
1681 Stream.JumpToBit(Offset);
1682 RecordData Record;
1683 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1684 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001685
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001686 while (true) {
1687 unsigned Code = Stream.ReadCode();
1688 switch (Code) {
1689 case llvm::bitc::END_BLOCK:
1690 return;
1691
1692 case llvm::bitc::ENTER_SUBBLOCK:
1693 // No known subblocks, always skip them.
1694 Stream.ReadSubBlockID();
1695 if (Stream.SkipBlock()) {
1696 Error("Malformed block record");
1697 return;
1698 }
1699 continue;
1700
1701 case llvm::bitc::DEFINE_ABBREV:
1702 Stream.ReadAbbrevRecord();
1703 continue;
1704 default: break;
1705 }
1706
1707 // Read a record.
1708 Record.clear();
1709 pch::PreprocessorRecordTypes RecType =
1710 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1711 switch (RecType) {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001712 case pch::PP_MACRO_OBJECT_LIKE:
1713 case pch::PP_MACRO_FUNCTION_LIKE: {
1714 // If we already have a macro, that means that we've hit the end
1715 // of the definition of the macro we were looking for. We're
1716 // done.
1717 if (Macro)
1718 return;
1719
1720 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1721 if (II == 0) {
1722 Error("Macro must have a name");
1723 return;
1724 }
1725 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1726 bool isUsed = Record[2];
1727
1728 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1729 MI->setIsUsed(isUsed);
1730
1731 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1732 // Decode function-like macro info.
1733 bool isC99VarArgs = Record[3];
1734 bool isGNUVarArgs = Record[4];
1735 MacroArgs.clear();
1736 unsigned NumArgs = Record[5];
1737 for (unsigned i = 0; i != NumArgs; ++i)
1738 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1739
1740 // Install function-like macro info.
1741 MI->setIsFunctionLike();
1742 if (isC99VarArgs) MI->setIsC99Varargs();
1743 if (isGNUVarArgs) MI->setIsGNUVarargs();
1744 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1745 PP.getPreprocessorAllocator());
1746 }
1747
1748 // Finally, install the macro.
1749 PP.setMacroInfo(II, MI);
1750
1751 // Remember that we saw this macro last so that we add the tokens that
1752 // form its body to it.
1753 Macro = MI;
1754 ++NumMacrosRead;
1755 break;
1756 }
1757
1758 case pch::PP_TOKEN: {
1759 // If we see a TOKEN before a PP_MACRO_*, then the file is
1760 // erroneous, just pretend we didn't see this.
1761 if (Macro == 0) break;
1762
1763 Token Tok;
1764 Tok.startToken();
1765 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1766 Tok.setLength(Record[1]);
1767 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1768 Tok.setIdentifierInfo(II);
1769 Tok.setKind((tok::TokenKind)Record[3]);
1770 Tok.setFlag((Token::TokenFlags)Record[4]);
1771 Macro->AddTokenToBody(Tok);
1772 break;
1773 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001774 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001775 }
1776}
1777
Douglas Gregorc713da92009-04-21 22:25:48 +00001778PCHReader::PCHReadResult
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001779PCHReader::ReadPCHBlock() {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001780 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1781 Error("Malformed block record");
1782 return Failure;
1783 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001784
1785 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001786 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787 while (!Stream.AtEndOfStream()) {
1788 unsigned Code = Stream.ReadCode();
1789 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001790 if (Stream.ReadBlockEnd()) {
1791 Error("Error at end of module block");
1792 return Failure;
1793 }
Chris Lattner29241862009-04-11 21:15:38 +00001794
Douglas Gregor179cfb12009-04-10 20:39:37 +00001795 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001796 }
1797
1798 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1799 switch (Stream.ReadSubBlockID()) {
1800 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1801 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1802 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001803 if (Stream.SkipBlock()) {
1804 Error("Malformed block record");
1805 return Failure;
1806 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001807 break;
1808
Chris Lattner29241862009-04-11 21:15:38 +00001809 case pch::PREPROCESSOR_BLOCK_ID:
Chris Lattner29241862009-04-11 21:15:38 +00001810 if (Stream.SkipBlock()) {
1811 Error("Malformed block record");
1812 return Failure;
1813 }
1814 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001815
Douglas Gregorab1cef72009-04-10 03:52:48 +00001816 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001817 switch (ReadSourceManagerBlock()) {
1818 case Success:
1819 break;
1820
1821 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001822 Error("Malformed source manager block");
1823 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001824
1825 case IgnorePCH:
1826 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001827 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001828 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001829 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001830 continue;
1831 }
1832
1833 if (Code == llvm::bitc::DEFINE_ABBREV) {
1834 Stream.ReadAbbrevRecord();
1835 continue;
1836 }
1837
1838 // Read and process a record.
1839 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001840 const char *BlobStart = 0;
1841 unsigned BlobLen = 0;
1842 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1843 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001844 default: // Default behavior: ignore.
1845 break;
1846
1847 case pch::TYPE_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001848 if (!TypesLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001849 Error("Duplicate TYPE_OFFSET record in PCH file");
1850 return Failure;
1851 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001852 TypeOffsets = (const uint64_t *)BlobStart;
1853 TypesLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001854 break;
1855
1856 case pch::DECL_OFFSET:
Douglas Gregor24a224c2009-04-25 18:35:21 +00001857 if (!DeclsLoaded.empty()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001858 Error("Duplicate DECL_OFFSET record in PCH file");
1859 return Failure;
1860 }
Douglas Gregor24a224c2009-04-25 18:35:21 +00001861 DeclOffsets = (const uint64_t *)BlobStart;
1862 DeclsLoaded.resize(Record[0]);
Douglas Gregorac8f2802009-04-10 17:25:41 +00001863 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001864
1865 case pch::LANGUAGE_OPTIONS:
1866 if (ParseLanguageOptions(Record))
1867 return IgnorePCH;
1868 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001869
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001870 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001871 std::string TargetTriple(BlobStart, BlobLen);
1872 if (TargetTriple != Context.Target.getTargetTriple()) {
1873 Diag(diag::warn_pch_target_triple)
1874 << TargetTriple << Context.Target.getTargetTriple();
1875 Diag(diag::note_ignoring_pch) << FileName;
1876 return IgnorePCH;
1877 }
1878 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001879 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001880
1881 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001882 IdentifierTableData = BlobStart;
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001883 if (Record[0]) {
1884 IdentifierLookupTable
1885 = PCHIdentifierLookupTable::Create(
Douglas Gregorc713da92009-04-21 22:25:48 +00001886 (const unsigned char *)IdentifierTableData + Record[0],
1887 (const unsigned char *)IdentifierTableData,
1888 PCHIdentifierLookupTrait(*this));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001889 PP.getIdentifierTable().setExternalIdentifierLookup(this);
1890 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001891 break;
1892
1893 case pch::IDENTIFIER_OFFSET:
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001894 if (!IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001895 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1896 return Failure;
1897 }
Douglas Gregorde44c9f2009-04-25 19:10:14 +00001898 IdentifierOffsets = (const uint32_t *)BlobStart;
1899 IdentifiersLoaded.resize(Record[0]);
Douglas Gregoreccb51d2009-04-25 23:30:02 +00001900 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001901 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001902
1903 case pch::EXTERNAL_DEFINITIONS:
1904 if (!ExternalDefinitions.empty()) {
1905 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1906 return Failure;
1907 }
1908 ExternalDefinitions.swap(Record);
1909 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001910
Douglas Gregore01ad442009-04-18 05:55:16 +00001911 case pch::SPECIAL_TYPES:
1912 SpecialTypes.swap(Record);
1913 break;
1914
Douglas Gregor456e0952009-04-17 22:13:46 +00001915 case pch::STATISTICS:
1916 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001917 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001918 TotalLexicalDeclContexts = Record[2];
1919 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001920 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001921 case pch::TENTATIVE_DEFINITIONS:
1922 if (!TentativeDefinitions.empty()) {
1923 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1924 return Failure;
1925 }
1926 TentativeDefinitions.swap(Record);
1927 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001928
1929 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1930 if (!LocallyScopedExternalDecls.empty()) {
1931 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1932 return Failure;
1933 }
1934 LocallyScopedExternalDecls.swap(Record);
1935 break;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001936
Douglas Gregor2d711832009-04-25 17:48:32 +00001937 case pch::SELECTOR_OFFSETS:
1938 SelectorOffsets = (const uint32_t *)BlobStart;
1939 TotalNumSelectors = Record[0];
1940 SelectorsLoaded.resize(TotalNumSelectors);
1941 break;
1942
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001943 case pch::METHOD_POOL:
Douglas Gregor2d711832009-04-25 17:48:32 +00001944 MethodPoolLookupTableData = (const unsigned char *)BlobStart;
1945 if (Record[0])
1946 MethodPoolLookupTable
1947 = PCHMethodPoolLookupTable::Create(
1948 MethodPoolLookupTableData + Record[0],
1949 MethodPoolLookupTableData,
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001950 PCHMethodPoolLookupTrait(*this));
Douglas Gregor2d711832009-04-25 17:48:32 +00001951 TotalSelectorsInMethodPool = Record[1];
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001952 break;
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001953
1954 case pch::PP_COUNTER_VALUE:
1955 if (!Record.empty())
1956 PP.setCounterValue(Record[0]);
1957 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001958 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001959 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001960 Error("Premature end of bitstream");
1961 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001962}
1963
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001964PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001965 // Set the PCH file name.
1966 this->FileName = FileName;
1967
Douglas Gregorc34897d2009-04-09 22:27:44 +00001968 // Open the PCH file.
1969 std::string ErrStr;
1970 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001971 if (!Buffer) {
1972 Error(ErrStr.c_str());
1973 return IgnorePCH;
1974 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001975
1976 // Initialize the stream
Chris Lattner587788a2009-04-26 20:59:20 +00001977 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
1978 (const unsigned char *)Buffer->getBufferEnd());
1979 Stream.init(StreamFile);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001980
1981 // Sniff for the signature.
1982 if (Stream.Read(8) != 'C' ||
1983 Stream.Read(8) != 'P' ||
1984 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001985 Stream.Read(8) != 'H') {
1986 Error("Not a PCH file");
1987 return IgnorePCH;
1988 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001989
Douglas Gregorc34897d2009-04-09 22:27:44 +00001990 while (!Stream.AtEndOfStream()) {
1991 unsigned Code = Stream.ReadCode();
1992
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001993 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1994 Error("Invalid record at top-level");
1995 return Failure;
1996 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001997
1998 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001999
Douglas Gregorc34897d2009-04-09 22:27:44 +00002000 // We only know the PCH subblock ID.
2001 switch (BlockID) {
2002 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002003 if (Stream.ReadBlockInfoBlock()) {
2004 Error("Malformed BlockInfoBlock");
2005 return Failure;
2006 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002007 break;
2008 case pch::PCH_BLOCK_ID:
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002009 switch (ReadPCHBlock()) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00002010 case Success:
2011 break;
2012
2013 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002014 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002015
2016 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00002017 // FIXME: We could consider reading through to the end of this
2018 // PCH block, skipping subblocks, to see if there are other
2019 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002020 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00002021 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002022 break;
2023 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002024 if (Stream.SkipBlock()) {
2025 Error("Malformed block record");
2026 return Failure;
2027 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002028 break;
2029 }
2030 }
2031
2032 // Load the translation unit declaration
2033 ReadDeclRecord(DeclOffsets[0], 0);
2034
Douglas Gregorc713da92009-04-21 22:25:48 +00002035 // Initialization of builtins and library builtins occurs before the
2036 // PCH file is read, so there may be some identifiers that were
2037 // loaded into the IdentifierTable before we intercepted the
2038 // creation of identifiers. Iterate through the list of known
2039 // identifiers and determine whether we have to establish
2040 // preprocessor definitions or top-level identifier declaration
2041 // chains for those identifiers.
2042 //
2043 // We copy the IdentifierInfo pointers to a small vector first,
2044 // since de-serializing declarations or macro definitions can add
2045 // new entries into the identifier table, invalidating the
2046 // iterators.
2047 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
2048 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2049 IdEnd = PP.getIdentifierTable().end();
2050 Id != IdEnd; ++Id)
2051 Identifiers.push_back(Id->second);
2052 PCHIdentifierLookupTable *IdTable
2053 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2054 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
2055 IdentifierInfo *II = Identifiers[I];
2056 // Look in the on-disk hash table for an entry for
2057 PCHIdentifierLookupTrait Info(*this, II);
2058 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
2059 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
2060 if (Pos == IdTable->end())
2061 continue;
2062
2063 // Dereferencing the iterator has the effect of populating the
2064 // IdentifierInfo node with the various declarations it needs.
2065 (void)*Pos;
2066 }
2067
Douglas Gregore01ad442009-04-18 05:55:16 +00002068 // Load the special types.
2069 Context.setBuiltinVaListType(
2070 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002071 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
2072 Context.setObjCIdType(GetType(Id));
2073 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
2074 Context.setObjCSelType(GetType(Sel));
2075 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
2076 Context.setObjCProtoType(GetType(Proto));
2077 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
2078 Context.setObjCClassType(GetType(Class));
2079 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
2080 Context.setCFConstantStringType(GetType(String));
2081 if (unsigned FastEnum
2082 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
2083 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002084
Douglas Gregorc713da92009-04-21 22:25:48 +00002085 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002086}
2087
Douglas Gregor179cfb12009-04-10 20:39:37 +00002088/// \brief Parse the record that corresponds to a LangOptions data
2089/// structure.
2090///
2091/// This routine compares the language options used to generate the
2092/// PCH file against the language options set for the current
2093/// compilation. For each option, we classify differences between the
2094/// two compiler states as either "benign" or "important". Benign
2095/// differences don't matter, and we accept them without complaint
2096/// (and without modifying the language options). Differences between
2097/// the states for important options cause the PCH file to be
2098/// unusable, so we emit a warning and return true to indicate that
2099/// there was an error.
2100///
2101/// \returns true if the PCH file is unacceptable, false otherwise.
2102bool PCHReader::ParseLanguageOptions(
2103 const llvm::SmallVectorImpl<uint64_t> &Record) {
2104 const LangOptions &LangOpts = Context.getLangOptions();
2105#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2106#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2107 if (Record[Idx] != LangOpts.Option) { \
2108 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2109 Diag(diag::note_ignoring_pch) << FileName; \
2110 return true; \
2111 } \
2112 ++Idx
2113
2114 unsigned Idx = 0;
2115 PARSE_LANGOPT_BENIGN(Trigraphs);
2116 PARSE_LANGOPT_BENIGN(BCPLComment);
2117 PARSE_LANGOPT_BENIGN(DollarIdents);
2118 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2119 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2120 PARSE_LANGOPT_BENIGN(ImplicitInt);
2121 PARSE_LANGOPT_BENIGN(Digraphs);
2122 PARSE_LANGOPT_BENIGN(HexFloats);
2123 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2124 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2125 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2126 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2127 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2128 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2129 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2130 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2131 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2132 PARSE_LANGOPT_BENIGN(PascalStrings);
2133 PARSE_LANGOPT_BENIGN(Boolean);
2134 PARSE_LANGOPT_BENIGN(WritableStrings);
2135 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2136 diag::warn_pch_lax_vector_conversions);
2137 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2138 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2139 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2140 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2141 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2142 diag::warn_pch_thread_safe_statics);
2143 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2144 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2145 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2146 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2147 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2148 diag::warn_pch_heinous_extensions);
2149 // FIXME: Most of the options below are benign if the macro wasn't
2150 // used. Unfortunately, this means that a PCH compiled without
2151 // optimization can't be used with optimization turned on, even
2152 // though the only thing that changes is whether __OPTIMIZE__ was
2153 // defined... but if __OPTIMIZE__ never showed up in the header, it
2154 // doesn't matter. We could consider making this some special kind
2155 // of check.
2156 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2157 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2158 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2159 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2160 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2161 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2162 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2163 Diag(diag::warn_pch_gc_mode)
2164 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2165 Diag(diag::note_ignoring_pch) << FileName;
2166 return true;
2167 }
2168 ++Idx;
2169 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2170 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2171#undef PARSE_LANGOPT_IRRELEVANT
2172#undef PARSE_LANGOPT_BENIGN
2173
2174 return false;
2175}
2176
Douglas Gregorc34897d2009-04-09 22:27:44 +00002177/// \brief Read and return the type at the given offset.
2178///
2179/// This routine actually reads the record corresponding to the type
2180/// at the given offset in the bitstream. It is a helper routine for
2181/// GetType, which deals with reading type IDs.
2182QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002183 // Keep track of where we are in the stream, then jump back there
2184 // after reading this type.
2185 SavedStreamPosition SavedPosition(Stream);
2186
Douglas Gregorc34897d2009-04-09 22:27:44 +00002187 Stream.JumpToBit(Offset);
2188 RecordData Record;
2189 unsigned Code = Stream.ReadCode();
2190 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002191 case pch::TYPE_EXT_QUAL: {
2192 assert(Record.size() == 3 &&
2193 "Incorrect encoding of extended qualifier type");
2194 QualType Base = GetType(Record[0]);
2195 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2196 unsigned AddressSpace = Record[2];
2197
2198 QualType T = Base;
2199 if (GCAttr != QualType::GCNone)
2200 T = Context.getObjCGCQualType(T, GCAttr);
2201 if (AddressSpace)
2202 T = Context.getAddrSpaceQualType(T, AddressSpace);
2203 return T;
2204 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002205
Douglas Gregorc34897d2009-04-09 22:27:44 +00002206 case pch::TYPE_FIXED_WIDTH_INT: {
2207 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2208 return Context.getFixedWidthIntType(Record[0], Record[1]);
2209 }
2210
2211 case pch::TYPE_COMPLEX: {
2212 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2213 QualType ElemType = GetType(Record[0]);
2214 return Context.getComplexType(ElemType);
2215 }
2216
2217 case pch::TYPE_POINTER: {
2218 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2219 QualType PointeeType = GetType(Record[0]);
2220 return Context.getPointerType(PointeeType);
2221 }
2222
2223 case pch::TYPE_BLOCK_POINTER: {
2224 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2225 QualType PointeeType = GetType(Record[0]);
2226 return Context.getBlockPointerType(PointeeType);
2227 }
2228
2229 case pch::TYPE_LVALUE_REFERENCE: {
2230 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2231 QualType PointeeType = GetType(Record[0]);
2232 return Context.getLValueReferenceType(PointeeType);
2233 }
2234
2235 case pch::TYPE_RVALUE_REFERENCE: {
2236 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2237 QualType PointeeType = GetType(Record[0]);
2238 return Context.getRValueReferenceType(PointeeType);
2239 }
2240
2241 case pch::TYPE_MEMBER_POINTER: {
2242 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2243 QualType PointeeType = GetType(Record[0]);
2244 QualType ClassType = GetType(Record[1]);
2245 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2246 }
2247
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002248 case pch::TYPE_CONSTANT_ARRAY: {
2249 QualType ElementType = GetType(Record[0]);
2250 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2251 unsigned IndexTypeQuals = Record[2];
2252 unsigned Idx = 3;
2253 llvm::APInt Size = ReadAPInt(Record, Idx);
2254 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2255 }
2256
2257 case pch::TYPE_INCOMPLETE_ARRAY: {
2258 QualType ElementType = GetType(Record[0]);
2259 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2260 unsigned IndexTypeQuals = Record[2];
2261 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2262 }
2263
2264 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002265 QualType ElementType = GetType(Record[0]);
2266 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2267 unsigned IndexTypeQuals = Record[2];
2268 return Context.getVariableArrayType(ElementType, ReadExpr(),
2269 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002270 }
2271
2272 case pch::TYPE_VECTOR: {
2273 if (Record.size() != 2) {
2274 Error("Incorrect encoding of vector type in PCH file");
2275 return QualType();
2276 }
2277
2278 QualType ElementType = GetType(Record[0]);
2279 unsigned NumElements = Record[1];
2280 return Context.getVectorType(ElementType, NumElements);
2281 }
2282
2283 case pch::TYPE_EXT_VECTOR: {
2284 if (Record.size() != 2) {
2285 Error("Incorrect encoding of extended vector type in PCH file");
2286 return QualType();
2287 }
2288
2289 QualType ElementType = GetType(Record[0]);
2290 unsigned NumElements = Record[1];
2291 return Context.getExtVectorType(ElementType, NumElements);
2292 }
2293
2294 case pch::TYPE_FUNCTION_NO_PROTO: {
2295 if (Record.size() != 1) {
2296 Error("Incorrect encoding of no-proto function type");
2297 return QualType();
2298 }
2299 QualType ResultType = GetType(Record[0]);
2300 return Context.getFunctionNoProtoType(ResultType);
2301 }
2302
2303 case pch::TYPE_FUNCTION_PROTO: {
2304 QualType ResultType = GetType(Record[0]);
2305 unsigned Idx = 1;
2306 unsigned NumParams = Record[Idx++];
2307 llvm::SmallVector<QualType, 16> ParamTypes;
2308 for (unsigned I = 0; I != NumParams; ++I)
2309 ParamTypes.push_back(GetType(Record[Idx++]));
2310 bool isVariadic = Record[Idx++];
2311 unsigned Quals = Record[Idx++];
2312 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2313 isVariadic, Quals);
2314 }
2315
2316 case pch::TYPE_TYPEDEF:
2317 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2318 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2319
2320 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002321 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002322
2323 case pch::TYPE_TYPEOF: {
2324 if (Record.size() != 1) {
2325 Error("Incorrect encoding of typeof(type) in PCH file");
2326 return QualType();
2327 }
2328 QualType UnderlyingType = GetType(Record[0]);
2329 return Context.getTypeOfType(UnderlyingType);
2330 }
2331
2332 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002333 assert(Record.size() == 1 && "Incorrect encoding of record type");
2334 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002335
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002336 case pch::TYPE_ENUM:
2337 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2338 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2339
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002340 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002341 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2342 return Context.getObjCInterfaceType(
2343 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002344
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002345 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2346 unsigned Idx = 0;
2347 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2348 unsigned NumProtos = Record[Idx++];
2349 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2350 for (unsigned I = 0; I != NumProtos; ++I)
2351 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2352 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2353 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002354
Chris Lattner9b9f2352009-04-22 06:40:03 +00002355 case pch::TYPE_OBJC_QUALIFIED_ID: {
2356 unsigned Idx = 0;
2357 unsigned NumProtos = Record[Idx++];
2358 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2359 for (unsigned I = 0; I != NumProtos; ++I)
2360 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2361 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2362 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002363 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002364 // Suppress a GCC warning
2365 return QualType();
2366}
2367
2368/// \brief Note that we have loaded the declaration with the given
2369/// Index.
2370///
2371/// This routine notes that this declaration has already been loaded,
2372/// so that future GetDecl calls will return this declaration rather
2373/// than trying to load a new declaration.
2374inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002375 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
2376 DeclsLoaded[Index] = D;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002377}
2378
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002379/// \brief Determine whether the consumer will be interested in seeing
2380/// this declaration (via HandleTopLevelDecl).
2381///
2382/// This routine should return true for anything that might affect
2383/// code generation, e.g., inline function definitions, Objective-C
2384/// declarations with metadata, etc.
2385static bool isConsumerInterestedIn(Decl *D) {
2386 if (VarDecl *Var = dyn_cast<VarDecl>(D))
2387 return Var->isFileVarDecl() && Var->getInit();
2388 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
2389 return Func->isThisDeclarationADefinition();
2390 return isa<ObjCProtocolDecl>(D);
2391}
2392
Douglas Gregorc34897d2009-04-09 22:27:44 +00002393/// \brief Read the declaration at the given offset from the PCH file.
2394Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002395 // Keep track of where we are in the stream, then jump back there
2396 // after reading this declaration.
2397 SavedStreamPosition SavedPosition(Stream);
2398
Douglas Gregorc34897d2009-04-09 22:27:44 +00002399 Decl *D = 0;
2400 Stream.JumpToBit(Offset);
2401 RecordData Record;
2402 unsigned Code = Stream.ReadCode();
2403 unsigned Idx = 0;
2404 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002405
Douglas Gregorc34897d2009-04-09 22:27:44 +00002406 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002407 case pch::DECL_ATTR:
2408 case pch::DECL_CONTEXT_LEXICAL:
2409 case pch::DECL_CONTEXT_VISIBLE:
2410 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2411 break;
2412
Douglas Gregorc34897d2009-04-09 22:27:44 +00002413 case pch::DECL_TRANSLATION_UNIT:
2414 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002415 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002416 break;
2417
2418 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002419 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420 break;
2421 }
2422
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002423 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002424 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002425 break;
2426 }
2427
Douglas Gregor982365e2009-04-13 21:20:57 +00002428 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002429 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2430 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002431 break;
2432 }
2433
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002434 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002435 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2436 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002437 break;
2438 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002439
2440 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002441 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2442 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002443 break;
2444 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002445
Steve Naroff79ea0e02009-04-20 15:06:07 +00002446 case pch::DECL_OBJC_METHOD: {
2447 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2448 Selector(), QualType(), 0);
2449 break;
2450 }
2451
Steve Naroff97b53bd2009-04-21 15:12:33 +00002452 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002453 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2454 break;
2455 }
2456
Steve Naroff97b53bd2009-04-21 15:12:33 +00002457 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002458 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2459 ObjCIvarDecl::None);
2460 break;
2461 }
2462
Steve Naroff97b53bd2009-04-21 15:12:33 +00002463 case pch::DECL_OBJC_PROTOCOL: {
2464 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2465 break;
2466 }
2467
2468 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2469 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2470 QualType(), 0);
2471 break;
2472 }
2473
2474 case pch::DECL_OBJC_CLASS: {
2475 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2476 break;
2477 }
2478
2479 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2480 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2481 break;
2482 }
2483
2484 case pch::DECL_OBJC_CATEGORY: {
2485 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2486 break;
2487 }
2488
2489 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002490 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002491 break;
2492 }
2493
2494 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002495 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002496 break;
2497 }
2498
2499 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002500 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002501 break;
2502 }
2503
2504 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002505 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002506 break;
2507 }
2508
2509 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002510 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2511 SourceLocation(), 0,
2512 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002513 break;
2514 }
2515
Douglas Gregor982365e2009-04-13 21:20:57 +00002516 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002517 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2518 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002519 break;
2520 }
2521
Douglas Gregorc34897d2009-04-09 22:27:44 +00002522 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002523 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2524 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002525 break;
2526 }
2527
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002528 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002529 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2530 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002531 break;
2532 }
2533
2534 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002535 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002536 QualType(), QualType(), VarDecl::None,
2537 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002538 break;
2539 }
2540
Douglas Gregor2a491792009-04-13 22:49:25 +00002541 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002542 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002543 break;
2544 }
2545
2546 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002547 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002548 break;
2549 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002550 }
2551
Douglas Gregorc713da92009-04-21 22:25:48 +00002552 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002553 if (D) {
2554 LoadedDecl(Index, D);
2555 Reader.Visit(D);
2556 }
2557
Douglas Gregorc34897d2009-04-09 22:27:44 +00002558 // If this declaration is also a declaration context, get the
2559 // offsets for its tables of lexical and visible declarations.
2560 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2561 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2562 if (Offsets.first || Offsets.second) {
2563 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2564 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2565 DeclContextOffsets[DC] = Offsets;
2566 }
2567 }
2568 assert(Idx == Record.size());
2569
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002570 // If we have deserialized a declaration that has a definition the
2571 // AST consumer might need to know about, notify the consumer
2572 // about that definition now or queue it for later.
2573 if (isConsumerInterestedIn(D)) {
2574 if (Consumer) {
Douglas Gregorafb99482009-04-24 23:42:14 +00002575 DeclGroupRef DG(D);
2576 Consumer->HandleTopLevelDecl(DG);
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002577 } else {
2578 InterestingDecls.push_back(D);
Douglas Gregor405b6432009-04-22 19:09:20 +00002579 }
2580 }
2581
Douglas Gregorc34897d2009-04-09 22:27:44 +00002582 return D;
2583}
2584
Douglas Gregorac8f2802009-04-10 17:25:41 +00002585QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002586 unsigned Quals = ID & 0x07;
2587 unsigned Index = ID >> 3;
2588
2589 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2590 QualType T;
2591 switch ((pch::PredefinedTypeIDs)Index) {
2592 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2593 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2594 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2595
2596 case pch::PREDEF_TYPE_CHAR_U_ID:
2597 case pch::PREDEF_TYPE_CHAR_S_ID:
2598 // FIXME: Check that the signedness of CharTy is correct!
2599 T = Context.CharTy;
2600 break;
2601
2602 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2603 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2604 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2605 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2606 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2607 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2608 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2609 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2610 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2611 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2612 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2613 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2614 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2615 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2616 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2617 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2618 }
2619
2620 assert(!T.isNull() && "Unknown predefined type");
2621 return T.getQualifiedType(Quals);
2622 }
2623
2624 Index -= pch::NUM_PREDEF_TYPE_IDS;
Douglas Gregore43f0972009-04-26 03:49:13 +00002625 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Douglas Gregor24a224c2009-04-25 18:35:21 +00002626 if (!TypesLoaded[Index])
2627 TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]).getTypePtr();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002628
Douglas Gregor24a224c2009-04-25 18:35:21 +00002629 return QualType(TypesLoaded[Index], Quals);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002630}
2631
Douglas Gregorac8f2802009-04-10 17:25:41 +00002632Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002633 if (ID == 0)
2634 return 0;
2635
Douglas Gregor24a224c2009-04-25 18:35:21 +00002636 if (ID > DeclsLoaded.size()) {
2637 Error("Declaration ID out-of-range for PCH file");
2638 return 0;
2639 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002640
Douglas Gregor24a224c2009-04-25 18:35:21 +00002641 unsigned Index = ID - 1;
2642 if (!DeclsLoaded[Index])
2643 ReadDeclRecord(DeclOffsets[Index], Index);
2644
2645 return DeclsLoaded[Index];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002646}
2647
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002648Stmt *PCHReader::GetStmt(uint64_t Offset) {
2649 // Keep track of where we are in the stream, then jump back there
2650 // after reading this declaration.
2651 SavedStreamPosition SavedPosition(Stream);
2652
2653 Stream.JumpToBit(Offset);
2654 return ReadStmt();
2655}
2656
Douglas Gregorc34897d2009-04-09 22:27:44 +00002657bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002658 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002659 assert(DC->hasExternalLexicalStorage() &&
2660 "DeclContext has no lexical decls in storage");
2661 uint64_t Offset = DeclContextOffsets[DC].first;
2662 assert(Offset && "DeclContext has no lexical decls in storage");
2663
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002664 // Keep track of where we are in the stream, then jump back there
2665 // after reading this context.
2666 SavedStreamPosition SavedPosition(Stream);
2667
Douglas Gregorc34897d2009-04-09 22:27:44 +00002668 // Load the record containing all of the declarations lexically in
2669 // this context.
2670 Stream.JumpToBit(Offset);
2671 RecordData Record;
2672 unsigned Code = Stream.ReadCode();
2673 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002674 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002675 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2676
2677 // Load all of the declaration IDs
2678 Decls.clear();
2679 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002680 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002681 return false;
2682}
2683
2684bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2685 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2686 assert(DC->hasExternalVisibleStorage() &&
2687 "DeclContext has no visible decls in storage");
2688 uint64_t Offset = DeclContextOffsets[DC].second;
2689 assert(Offset && "DeclContext has no visible decls in storage");
2690
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002691 // Keep track of where we are in the stream, then jump back there
2692 // after reading this context.
2693 SavedStreamPosition SavedPosition(Stream);
2694
Douglas Gregorc34897d2009-04-09 22:27:44 +00002695 // Load the record containing all of the declarations visible in
2696 // this context.
2697 Stream.JumpToBit(Offset);
2698 RecordData Record;
2699 unsigned Code = Stream.ReadCode();
2700 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002701 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002702 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2703 if (Record.size() == 0)
2704 return false;
2705
2706 Decls.clear();
2707
2708 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002709 while (Idx < Record.size()) {
2710 Decls.push_back(VisibleDeclaration());
2711 Decls.back().Name = ReadDeclarationName(Record, Idx);
2712
Douglas Gregorc34897d2009-04-09 22:27:44 +00002713 unsigned Size = Record[Idx++];
2714 llvm::SmallVector<unsigned, 4> & LoadedDecls
2715 = Decls.back().Declarations;
2716 LoadedDecls.reserve(Size);
2717 for (unsigned I = 0; I < Size; ++I)
2718 LoadedDecls.push_back(Record[Idx++]);
2719 }
2720
Douglas Gregoraf136d92009-04-22 22:34:57 +00002721 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002722 return false;
2723}
2724
Douglas Gregor631f6c62009-04-14 00:24:19 +00002725void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002726 this->Consumer = Consumer;
2727
Douglas Gregor631f6c62009-04-14 00:24:19 +00002728 if (!Consumer)
2729 return;
2730
2731 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2732 Decl *D = GetDecl(ExternalDefinitions[I]);
2733 DeclGroupRef DG(D);
2734 Consumer->HandleTopLevelDecl(DG);
2735 }
Douglas Gregorf93cfee2009-04-25 00:41:30 +00002736
2737 for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) {
2738 DeclGroupRef DG(InterestingDecls[I]);
2739 Consumer->HandleTopLevelDecl(DG);
2740 }
Douglas Gregor631f6c62009-04-14 00:24:19 +00002741}
2742
Douglas Gregorc34897d2009-04-09 22:27:44 +00002743void PCHReader::PrintStats() {
2744 std::fprintf(stderr, "*** PCH Statistics:\n");
2745
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002746 unsigned NumTypesLoaded
2747 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
2748 (Type *)0);
2749 unsigned NumDeclsLoaded
2750 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
2751 (Decl *)0);
2752 unsigned NumIdentifiersLoaded
2753 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
2754 IdentifiersLoaded.end(),
2755 (IdentifierInfo *)0);
2756 unsigned NumSelectorsLoaded
2757 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
2758 SelectorsLoaded.end(),
2759 Selector());
Douglas Gregor9cf47422009-04-13 20:50:16 +00002760
Douglas Gregor24a224c2009-04-25 18:35:21 +00002761 if (!TypesLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002762 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002763 NumTypesLoaded, (unsigned)TypesLoaded.size(),
2764 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
2765 if (!DeclsLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002766 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor24a224c2009-04-25 18:35:21 +00002767 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
2768 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002769 if (!IdentifiersLoaded.empty())
Douglas Gregor2d711832009-04-25 17:48:32 +00002770 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002771 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
2772 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregor2d711832009-04-25 17:48:32 +00002773 if (TotalNumSelectors)
2774 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
2775 NumSelectorsLoaded, TotalNumSelectors,
2776 ((float)NumSelectorsLoaded/TotalNumSelectors * 100));
2777 if (TotalNumStatements)
2778 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2779 NumStatementsRead, TotalNumStatements,
2780 ((float)NumStatementsRead/TotalNumStatements * 100));
2781 if (TotalNumMacros)
2782 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2783 NumMacrosRead, TotalNumMacros,
2784 ((float)NumMacrosRead/TotalNumMacros * 100));
2785 if (TotalLexicalDeclContexts)
2786 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2787 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2788 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2789 * 100));
2790 if (TotalVisibleDeclContexts)
2791 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2792 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2793 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2794 * 100));
2795 if (TotalSelectorsInMethodPool) {
2796 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
2797 NumMethodPoolSelectorsRead, TotalSelectorsInMethodPool,
2798 ((float)NumMethodPoolSelectorsRead/TotalSelectorsInMethodPool
2799 * 100));
2800 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
2801 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002802 std::fprintf(stderr, "\n");
2803}
2804
Douglas Gregorc713da92009-04-21 22:25:48 +00002805void PCHReader::InitializeSema(Sema &S) {
2806 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002807 S.ExternalSource = this;
2808
Douglas Gregor2554cf22009-04-22 21:15:06 +00002809 // Makes sure any declarations that were deserialized "too early"
2810 // still get added to the identifier's declaration chains.
2811 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2812 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2813 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002814 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002815 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002816
2817 // If there were any tentative definitions, deserialize them and add
2818 // them to Sema's table of tentative definitions.
2819 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2820 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2821 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2822 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002823
2824 // If there were any locally-scoped external declarations,
2825 // deserialize them and add them to Sema's table of locally-scoped
2826 // external declarations.
2827 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2828 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2829 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2830 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002831}
2832
2833IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2834 // Try to find this name within our on-disk hash table
2835 PCHIdentifierLookupTable *IdTable
2836 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2837 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2838 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2839 if (Pos == IdTable->end())
2840 return 0;
2841
2842 // Dereferencing the iterator has the effect of building the
2843 // IdentifierInfo node and populating it with the various
2844 // declarations it needs.
2845 return *Pos;
2846}
2847
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002848std::pair<ObjCMethodList, ObjCMethodList>
2849PCHReader::ReadMethodPool(Selector Sel) {
2850 if (!MethodPoolLookupTable)
2851 return std::pair<ObjCMethodList, ObjCMethodList>();
2852
2853 // Try to find this selector within our on-disk hash table.
2854 PCHMethodPoolLookupTable *PoolTable
2855 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2856 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
Douglas Gregor2d711832009-04-25 17:48:32 +00002857 if (Pos == PoolTable->end()) {
2858 ++NumMethodPoolMisses;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002859 return std::pair<ObjCMethodList, ObjCMethodList>();;
Douglas Gregor2d711832009-04-25 17:48:32 +00002860 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002861
Douglas Gregor2d711832009-04-25 17:48:32 +00002862 ++NumMethodPoolSelectorsRead;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002863 return *Pos;
2864}
2865
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002866void PCHReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) {
Douglas Gregorc713da92009-04-21 22:25:48 +00002867 assert(ID && "Non-zero identifier ID required");
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002868 assert(ID <= IdentifiersLoaded.size() && "Identifier ID out of range");
2869 IdentifiersLoaded[ID - 1] = II;
Douglas Gregorc713da92009-04-21 22:25:48 +00002870}
2871
Chris Lattner29241862009-04-11 21:15:38 +00002872IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002873 if (ID == 0)
2874 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002875
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002876 if (!IdentifierTableData || IdentifiersLoaded.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002877 Error("No identifier table in PCH file");
2878 return 0;
2879 }
Chris Lattner29241862009-04-11 21:15:38 +00002880
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002881 if (!IdentifiersLoaded[ID - 1]) {
2882 uint32_t Offset = IdentifierOffsets[ID - 1];
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002883 const char *Str = IdentifierTableData + Offset;
Douglas Gregor85c4a872009-04-25 21:04:17 +00002884
2885 // If there is an identifier lookup table, but the offset of this
2886 // string is after the identifier table itself, then we know that
2887 // this string is not in the on-disk hash table. Therefore,
2888 // disable lookup into the hash table when looking for this
2889 // identifier.
2890 PCHIdentifierLookupTable *IdTable
2891 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002892 if (!IdTable ||
2893 Offset >= uint32_t(IdTable->getBuckets() - IdTable->getBase())) {
2894 // Turn off lookup into the on-disk hash table. We know that
2895 // this identifier is not there.
2896 if (IdTable)
2897 PP.getIdentifierTable().setExternalIdentifierLookup(0);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002898
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002899 // All of the strings in the PCH file are preceded by a 16-bit
2900 // length. Extract that 16-bit length to avoid having to execute
2901 // strlen().
2902 const char *StrLenPtr = Str - 2;
2903 unsigned StrLen = (((unsigned) StrLenPtr[0])
2904 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
2905 IdentifiersLoaded[ID - 1] = &Context.Idents.get(Str, Str + StrLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002906
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002907 // Turn on lookup into the on-disk hash table, if we have an
2908 // on-disk hash table.
2909 if (IdTable)
2910 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2911 } else {
2912 // The identifier is a key in our on-disk hash table. Since we
2913 // know where the hash table entry starts, just read in this
2914 // (key, value) pair.
2915 PCHIdentifierLookupTrait Trait(const_cast<PCHReader &>(*this));
2916 const unsigned char *Pos = (const unsigned char *)Str - 4;
2917 std::pair<unsigned, unsigned> KeyDataLengths
2918 = Trait.ReadKeyDataLength(Pos);
Douglas Gregor85c4a872009-04-25 21:04:17 +00002919
Douglas Gregor4d7a6e42009-04-25 21:21:38 +00002920 PCHIdentifierLookupTrait::internal_key_type InternalKey
2921 = Trait.ReadKey(Pos, KeyDataLengths.first);
2922 Pos = (const unsigned char *)Str + KeyDataLengths.first;
2923 IdentifiersLoaded[ID - 1] = Trait.ReadData(InternalKey, Pos,
2924 KeyDataLengths.second);
2925 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002926 }
Chris Lattner29241862009-04-11 21:15:38 +00002927
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002928 return IdentifiersLoaded[ID - 1];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002929}
2930
Steve Naroff9e84d782009-04-23 10:39:46 +00002931Selector PCHReader::DecodeSelector(unsigned ID) {
2932 if (ID == 0)
2933 return Selector();
2934
Douglas Gregor2d711832009-04-25 17:48:32 +00002935 if (!MethodPoolLookupTableData) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002936 Error("No selector table in PCH file");
2937 return Selector();
2938 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002939
2940 if (ID > TotalNumSelectors) {
Steve Naroff9e84d782009-04-23 10:39:46 +00002941 Error("Selector ID out of range");
2942 return Selector();
2943 }
Douglas Gregor2d711832009-04-25 17:48:32 +00002944
2945 unsigned Index = ID - 1;
2946 if (SelectorsLoaded[Index].getAsOpaquePtr() == 0) {
2947 // Load this selector from the selector table.
2948 // FIXME: endianness portability issues with SelectorOffsets table
2949 PCHMethodPoolLookupTrait Trait(*this);
2950 SelectorsLoaded[Index]
2951 = Trait.ReadKey(MethodPoolLookupTableData + SelectorOffsets[Index], 0);
2952 }
2953
2954 return SelectorsLoaded[Index];
Steve Naroff9e84d782009-04-23 10:39:46 +00002955}
2956
Douglas Gregorc34897d2009-04-09 22:27:44 +00002957DeclarationName
2958PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2959 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2960 switch (Kind) {
2961 case DeclarationName::Identifier:
2962 return DeclarationName(GetIdentifierInfo(Record, Idx));
2963
2964 case DeclarationName::ObjCZeroArgSelector:
2965 case DeclarationName::ObjCOneArgSelector:
2966 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002967 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002968
2969 case DeclarationName::CXXConstructorName:
2970 return Context.DeclarationNames.getCXXConstructorName(
2971 GetType(Record[Idx++]));
2972
2973 case DeclarationName::CXXDestructorName:
2974 return Context.DeclarationNames.getCXXDestructorName(
2975 GetType(Record[Idx++]));
2976
2977 case DeclarationName::CXXConversionFunctionName:
2978 return Context.DeclarationNames.getCXXConversionFunctionName(
2979 GetType(Record[Idx++]));
2980
2981 case DeclarationName::CXXOperatorName:
2982 return Context.DeclarationNames.getCXXOperatorName(
2983 (OverloadedOperatorKind)Record[Idx++]);
2984
2985 case DeclarationName::CXXUsingDirective:
2986 return DeclarationName::getUsingDirectiveName();
2987 }
2988
2989 // Required to silence GCC warning
2990 return DeclarationName();
2991}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002992
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002993/// \brief Read an integral value
2994llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2995 unsigned BitWidth = Record[Idx++];
2996 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2997 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2998 Idx += NumWords;
2999 return Result;
3000}
3001
3002/// \brief Read a signed integral value
3003llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
3004 bool isUnsigned = Record[Idx++];
3005 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
3006}
3007
Douglas Gregore2f37202009-04-14 21:55:33 +00003008/// \brief Read a floating-point value
3009llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00003010 return llvm::APFloat(ReadAPInt(Record, Idx));
3011}
3012
Douglas Gregor1c507882009-04-15 21:30:51 +00003013// \brief Read a string
3014std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
3015 unsigned Len = Record[Idx++];
3016 std::string Result(&Record[Idx], &Record[Idx] + Len);
3017 Idx += Len;
3018 return Result;
3019}
3020
3021/// \brief Reads attributes from the current stream position.
3022Attr *PCHReader::ReadAttributes() {
3023 unsigned Code = Stream.ReadCode();
3024 assert(Code == llvm::bitc::UNABBREV_RECORD &&
3025 "Expected unabbreviated record"); (void)Code;
3026
3027 RecordData Record;
3028 unsigned Idx = 0;
3029 unsigned RecCode = Stream.ReadRecord(Code, Record);
3030 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
3031 (void)RecCode;
3032
3033#define SIMPLE_ATTR(Name) \
3034 case Attr::Name: \
3035 New = ::new (Context) Name##Attr(); \
3036 break
3037
3038#define STRING_ATTR(Name) \
3039 case Attr::Name: \
3040 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
3041 break
3042
3043#define UNSIGNED_ATTR(Name) \
3044 case Attr::Name: \
3045 New = ::new (Context) Name##Attr(Record[Idx++]); \
3046 break
3047
3048 Attr *Attrs = 0;
3049 while (Idx < Record.size()) {
3050 Attr *New = 0;
3051 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
3052 bool IsInherited = Record[Idx++];
3053
3054 switch (Kind) {
3055 STRING_ATTR(Alias);
3056 UNSIGNED_ATTR(Aligned);
3057 SIMPLE_ATTR(AlwaysInline);
3058 SIMPLE_ATTR(AnalyzerNoReturn);
3059 STRING_ATTR(Annotate);
3060 STRING_ATTR(AsmLabel);
3061
3062 case Attr::Blocks:
3063 New = ::new (Context) BlocksAttr(
3064 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3065 break;
3066
3067 case Attr::Cleanup:
3068 New = ::new (Context) CleanupAttr(
3069 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3070 break;
3071
3072 SIMPLE_ATTR(Const);
3073 UNSIGNED_ATTR(Constructor);
3074 SIMPLE_ATTR(DLLExport);
3075 SIMPLE_ATTR(DLLImport);
3076 SIMPLE_ATTR(Deprecated);
3077 UNSIGNED_ATTR(Destructor);
3078 SIMPLE_ATTR(FastCall);
3079
3080 case Attr::Format: {
3081 std::string Type = ReadString(Record, Idx);
3082 unsigned FormatIdx = Record[Idx++];
3083 unsigned FirstArg = Record[Idx++];
3084 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3085 break;
3086 }
3087
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003088 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003089
3090 case Attr::IBOutletKind:
3091 New = ::new (Context) IBOutletAttr();
3092 break;
3093
3094 SIMPLE_ATTR(NoReturn);
3095 SIMPLE_ATTR(NoThrow);
3096 SIMPLE_ATTR(Nodebug);
3097 SIMPLE_ATTR(Noinline);
3098
3099 case Attr::NonNull: {
3100 unsigned Size = Record[Idx++];
3101 llvm::SmallVector<unsigned, 16> ArgNums;
3102 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3103 Idx += Size;
3104 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3105 break;
3106 }
3107
3108 SIMPLE_ATTR(ObjCException);
3109 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003110 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003111 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003112 SIMPLE_ATTR(Overloadable);
3113 UNSIGNED_ATTR(Packed);
3114 SIMPLE_ATTR(Pure);
3115 UNSIGNED_ATTR(Regparm);
3116 STRING_ATTR(Section);
3117 SIMPLE_ATTR(StdCall);
3118 SIMPLE_ATTR(TransparentUnion);
3119 SIMPLE_ATTR(Unavailable);
3120 SIMPLE_ATTR(Unused);
3121 SIMPLE_ATTR(Used);
3122
3123 case Attr::Visibility:
3124 New = ::new (Context) VisibilityAttr(
3125 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3126 break;
3127
3128 SIMPLE_ATTR(WarnUnusedResult);
3129 SIMPLE_ATTR(Weak);
3130 SIMPLE_ATTR(WeakImport);
3131 }
3132
3133 assert(New && "Unable to decode attribute?");
3134 New->setInherited(IsInherited);
3135 New->setNext(Attrs);
3136 Attrs = New;
3137 }
3138#undef UNSIGNED_ATTR
3139#undef STRING_ATTR
3140#undef SIMPLE_ATTR
3141
3142 // The list of attributes was built backwards. Reverse the list
3143 // before returning it.
3144 Attr *PrevAttr = 0, *NextAttr = 0;
3145 while (Attrs) {
3146 NextAttr = Attrs->getNext();
3147 Attrs->setNext(PrevAttr);
3148 PrevAttr = Attrs;
3149 Attrs = NextAttr;
3150 }
3151
3152 return PrevAttr;
3153}
3154
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003155Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003156 // Within the bitstream, expressions are stored in Reverse Polish
3157 // Notation, with each of the subexpressions preceding the
3158 // expression they are stored in. To evaluate expressions, we
3159 // continue reading expressions and placing them on the stack, with
3160 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003161 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003162 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003163 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003164 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003165 llvm::SmallVector<Stmt *, 16> StmtStack;
3166 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003167 Stmt::EmptyShell Empty;
3168
Douglas Gregora151ba42009-04-14 23:32:43 +00003169 while (true) {
3170 unsigned Code = Stream.ReadCode();
3171 if (Code == llvm::bitc::END_BLOCK) {
3172 if (Stream.ReadBlockEnd()) {
3173 Error("Error at end of Source Manager block");
3174 return 0;
3175 }
3176 break;
3177 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003178
Douglas Gregora151ba42009-04-14 23:32:43 +00003179 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3180 // No known subblocks, always skip them.
3181 Stream.ReadSubBlockID();
3182 if (Stream.SkipBlock()) {
3183 Error("Malformed block record");
3184 return 0;
3185 }
3186 continue;
3187 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003188
Douglas Gregora151ba42009-04-14 23:32:43 +00003189 if (Code == llvm::bitc::DEFINE_ABBREV) {
3190 Stream.ReadAbbrevRecord();
3191 continue;
3192 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003193
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003194 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003195 Idx = 0;
3196 Record.clear();
3197 bool Finished = false;
3198 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003199 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003200 Finished = true;
3201 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003202
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003203 case pch::STMT_NULL_PTR:
3204 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003205 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003206
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003207 case pch::STMT_NULL:
3208 S = new (Context) NullStmt(Empty);
3209 break;
3210
3211 case pch::STMT_COMPOUND:
3212 S = new (Context) CompoundStmt(Empty);
3213 break;
3214
3215 case pch::STMT_CASE:
3216 S = new (Context) CaseStmt(Empty);
3217 break;
3218
3219 case pch::STMT_DEFAULT:
3220 S = new (Context) DefaultStmt(Empty);
3221 break;
3222
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003223 case pch::STMT_LABEL:
3224 S = new (Context) LabelStmt(Empty);
3225 break;
3226
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003227 case pch::STMT_IF:
3228 S = new (Context) IfStmt(Empty);
3229 break;
3230
3231 case pch::STMT_SWITCH:
3232 S = new (Context) SwitchStmt(Empty);
3233 break;
3234
Douglas Gregora6b503f2009-04-17 00:16:09 +00003235 case pch::STMT_WHILE:
3236 S = new (Context) WhileStmt(Empty);
3237 break;
3238
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003239 case pch::STMT_DO:
3240 S = new (Context) DoStmt(Empty);
3241 break;
3242
3243 case pch::STMT_FOR:
3244 S = new (Context) ForStmt(Empty);
3245 break;
3246
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003247 case pch::STMT_GOTO:
3248 S = new (Context) GotoStmt(Empty);
3249 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003250
3251 case pch::STMT_INDIRECT_GOTO:
3252 S = new (Context) IndirectGotoStmt(Empty);
3253 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003254
Douglas Gregora6b503f2009-04-17 00:16:09 +00003255 case pch::STMT_CONTINUE:
3256 S = new (Context) ContinueStmt(Empty);
3257 break;
3258
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003259 case pch::STMT_BREAK:
3260 S = new (Context) BreakStmt(Empty);
3261 break;
3262
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003263 case pch::STMT_RETURN:
3264 S = new (Context) ReturnStmt(Empty);
3265 break;
3266
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003267 case pch::STMT_DECL:
3268 S = new (Context) DeclStmt(Empty);
3269 break;
3270
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003271 case pch::STMT_ASM:
3272 S = new (Context) AsmStmt(Empty);
3273 break;
3274
Douglas Gregora151ba42009-04-14 23:32:43 +00003275 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003276 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003277 break;
3278
3279 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003280 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003281 break;
3282
3283 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003284 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003285 break;
3286
3287 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003288 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003289 break;
3290
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003291 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003292 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003293 break;
3294
Douglas Gregor596e0932009-04-15 16:35:07 +00003295 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003296 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003297 Record[PCHStmtReader::NumExprFields + 1]);
3298 break;
3299
Douglas Gregora151ba42009-04-14 23:32:43 +00003300 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003301 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003302 break;
3303
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003304 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003305 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003306 break;
3307
Douglas Gregor12d74052009-04-15 15:58:59 +00003308 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003309 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003310 break;
3311
3312 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003313 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003314 break;
3315
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003316 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003317 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003318 break;
3319
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003320 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003321 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003322 break;
3323
3324 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003325 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003326 break;
3327
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003328 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003329 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003330 break;
3331
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003332 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003333 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003334 break;
3335
3336 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003337 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003338 break;
3339
Douglas Gregora151ba42009-04-14 23:32:43 +00003340 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003341 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003342 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003343
3344 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003345 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003346 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003347
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003348 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003349 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003350 break;
3351
Douglas Gregorec0b8292009-04-15 23:02:49 +00003352 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003353 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003354 break;
3355
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003356 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003357 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003358 break;
3359
3360 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003361 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003362 Record[PCHStmtReader::NumExprFields] - 1);
3363
3364 break;
3365
3366 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003367 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003368 break;
3369
Douglas Gregorec0b8292009-04-15 23:02:49 +00003370 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003371 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003372 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003373
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003374 case pch::EXPR_ADDR_LABEL:
3375 S = new (Context) AddrLabelExpr(Empty);
3376 break;
3377
Douglas Gregoreca12f62009-04-17 19:05:30 +00003378 case pch::EXPR_STMT:
3379 S = new (Context) StmtExpr(Empty);
3380 break;
3381
Douglas Gregor209d4622009-04-15 23:33:31 +00003382 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003383 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003384 break;
3385
3386 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003387 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003388 break;
3389
3390 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003391 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003392 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003393
3394 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003395 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003396 break;
3397
Douglas Gregore246b742009-04-17 19:21:43 +00003398 case pch::EXPR_BLOCK:
3399 S = new (Context) BlockExpr(Empty);
3400 break;
3401
Douglas Gregor725e94b2009-04-16 00:01:45 +00003402 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003403 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003404 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003405
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003406 case pch::EXPR_OBJC_STRING_LITERAL:
3407 S = new (Context) ObjCStringLiteral(Empty);
3408 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003409 case pch::EXPR_OBJC_ENCODE:
3410 S = new (Context) ObjCEncodeExpr(Empty);
3411 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003412 case pch::EXPR_OBJC_SELECTOR_EXPR:
3413 S = new (Context) ObjCSelectorExpr(Empty);
3414 break;
3415 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3416 S = new (Context) ObjCProtocolExpr(Empty);
3417 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003418 case pch::EXPR_OBJC_IVAR_REF_EXPR:
3419 S = new (Context) ObjCIvarRefExpr(Empty);
3420 break;
3421 case pch::EXPR_OBJC_PROPERTY_REF_EXPR:
3422 S = new (Context) ObjCPropertyRefExpr(Empty);
3423 break;
3424 case pch::EXPR_OBJC_KVC_REF_EXPR:
3425 S = new (Context) ObjCKVCRefExpr(Empty);
3426 break;
Steve Narofffb3e4022009-04-25 14:04:28 +00003427 case pch::EXPR_OBJC_MESSAGE_EXPR:
3428 S = new (Context) ObjCMessageExpr(Empty);
3429 break;
Chris Lattnerc0478bf2009-04-26 00:44:05 +00003430 case pch::EXPR_OBJC_SUPER_EXPR:
3431 S = new (Context) ObjCSuperExpr(Empty);
3432 break;
Steve Naroff79762bd2009-04-26 18:52:16 +00003433 case pch::STMT_OBJC_FOR_COLLECTION:
3434 S = new (Context) ObjCForCollectionStmt(Empty);
3435 break;
3436 case pch::STMT_OBJC_CATCH:
3437 S = new (Context) ObjCAtCatchStmt(Empty);
3438 break;
3439 case pch::STMT_OBJC_FINALLY:
3440 S = new (Context) ObjCAtFinallyStmt(Empty);
3441 break;
3442 case pch::STMT_OBJC_AT_TRY:
3443 S = new (Context) ObjCAtTryStmt(Empty);
3444 break;
3445 case pch::STMT_OBJC_AT_SYNCHRONIZED:
3446 S = new (Context) ObjCAtSynchronizedStmt(Empty);
3447 break;
3448 case pch::STMT_OBJC_AT_THROW:
3449 S = new (Context) ObjCAtThrowStmt(Empty);
3450 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003451 }
3452
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003453 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003454 if (Finished)
3455 break;
3456
Douglas Gregor456e0952009-04-17 22:13:46 +00003457 ++NumStatementsRead;
3458
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003459 if (S) {
3460 unsigned NumSubStmts = Reader.Visit(S);
3461 while (NumSubStmts > 0) {
3462 StmtStack.pop_back();
3463 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003464 }
3465 }
3466
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003467 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003468 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003469 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003470 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003471 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003472 return StmtStack.back();
3473}
3474
3475Expr *PCHReader::ReadExpr() {
3476 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003477}
3478
Douglas Gregor179cfb12009-04-10 20:39:37 +00003479DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003480 return Diag(SourceLocation(), DiagID);
3481}
3482
3483DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3484 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003485 Context.getSourceManager()),
3486 DiagID);
3487}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003488
Douglas Gregorc713da92009-04-21 22:25:48 +00003489/// \brief Retrieve the identifier table associated with the
3490/// preprocessor.
3491IdentifierTable &PCHReader::getIdentifierTable() {
3492 return PP.getIdentifierTable();
3493}
3494
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003495/// \brief Record that the given ID maps to the given switch-case
3496/// statement.
3497void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3498 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3499 SwitchCaseStmts[ID] = SC;
3500}
3501
3502/// \brief Retrieve the switch-case statement with the given ID.
3503SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3504 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3505 return SwitchCaseStmts[ID];
3506}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003507
3508/// \brief Record that the given label statement has been
3509/// deserialized and has the given ID.
3510void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3511 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3512 "Deserialized label twice");
3513 LabelStmts[ID] = S;
3514
3515 // If we've already seen any goto statements that point to this
3516 // label, resolve them now.
3517 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3518 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3519 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3520 Goto->second->setLabel(S);
3521 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003522
3523 // If we've already seen any address-label statements that point to
3524 // this label, resolve them now.
3525 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3526 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3527 = UnresolvedAddrLabelExprs.equal_range(ID);
3528 for (AddrLabelIter AddrLabel = AddrLabels.first;
3529 AddrLabel != AddrLabels.second; ++AddrLabel)
3530 AddrLabel->second->setLabel(S);
3531 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003532}
3533
3534/// \brief Set the label of the given statement to the label
3535/// identified by ID.
3536///
3537/// Depending on the order in which the label and other statements
3538/// referencing that label occur, this operation may complete
3539/// immediately (updating the statement) or it may queue the
3540/// statement to be back-patched later.
3541void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3542 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3543 if (Label != LabelStmts.end()) {
3544 // We've already seen this label, so set the label of the goto and
3545 // we're done.
3546 S->setLabel(Label->second);
3547 } else {
3548 // We haven't seen this label yet, so add this goto to the set of
3549 // unresolved goto statements.
3550 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3551 }
3552}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003553
3554/// \brief Set the label of the given expression to the label
3555/// identified by ID.
3556///
3557/// Depending on the order in which the label and other statements
3558/// referencing that label occur, this operation may complete
3559/// immediately (updating the statement) or it may queue the
3560/// statement to be back-patched later.
3561void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3562 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3563 if (Label != LabelStmts.end()) {
3564 // We've already seen this label, so set the label of the
3565 // label-address expression and we're done.
3566 S->setLabel(Label->second);
3567 } else {
3568 // We haven't seen this label yet, so add this label-address
3569 // expression to the set of unresolved label-address expressions.
3570 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3571 }
3572}