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