blob: b10778a1e1dfcac18c784dcab612a471c775214d [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
2407/// \brief Read the declaration at the given offset from the PCH file.
2408Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002409 // Keep track of where we are in the stream, then jump back there
2410 // after reading this declaration.
2411 SavedStreamPosition SavedPosition(Stream);
2412
Douglas Gregorc34897d2009-04-09 22:27:44 +00002413 Decl *D = 0;
2414 Stream.JumpToBit(Offset);
2415 RecordData Record;
2416 unsigned Code = Stream.ReadCode();
2417 unsigned Idx = 0;
2418 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002419
Douglas Gregorc34897d2009-04-09 22:27:44 +00002420 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002421 case pch::DECL_ATTR:
2422 case pch::DECL_CONTEXT_LEXICAL:
2423 case pch::DECL_CONTEXT_VISIBLE:
2424 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2425 break;
2426
Douglas Gregorc34897d2009-04-09 22:27:44 +00002427 case pch::DECL_TRANSLATION_UNIT:
2428 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002429 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002430 break;
2431
2432 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002433 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002434 break;
2435 }
2436
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002437 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002438 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002439 break;
2440 }
2441
Douglas Gregor982365e2009-04-13 21:20:57 +00002442 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002443 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2444 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002445 break;
2446 }
2447
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002448 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002449 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2450 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002451 break;
2452 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002453
2454 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002455 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2456 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002457 break;
2458 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002459
Steve Naroff79ea0e02009-04-20 15:06:07 +00002460 case pch::DECL_OBJC_METHOD: {
2461 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2462 Selector(), QualType(), 0);
2463 break;
2464 }
2465
Steve Naroff97b53bd2009-04-21 15:12:33 +00002466 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002467 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2468 break;
2469 }
2470
Steve Naroff97b53bd2009-04-21 15:12:33 +00002471 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002472 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2473 ObjCIvarDecl::None);
2474 break;
2475 }
2476
Steve Naroff97b53bd2009-04-21 15:12:33 +00002477 case pch::DECL_OBJC_PROTOCOL: {
2478 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2479 break;
2480 }
2481
2482 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2483 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2484 QualType(), 0);
2485 break;
2486 }
2487
2488 case pch::DECL_OBJC_CLASS: {
2489 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2490 break;
2491 }
2492
2493 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2494 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2495 break;
2496 }
2497
2498 case pch::DECL_OBJC_CATEGORY: {
2499 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2500 break;
2501 }
2502
2503 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002504 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002505 break;
2506 }
2507
2508 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002509 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002510 break;
2511 }
2512
2513 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002514 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002515 break;
2516 }
2517
2518 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002519 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002520 break;
2521 }
2522
2523 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002524 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2525 SourceLocation(), 0,
2526 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002527 break;
2528 }
2529
Douglas Gregor982365e2009-04-13 21:20:57 +00002530 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002531 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2532 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002533 break;
2534 }
2535
Douglas Gregorc34897d2009-04-09 22:27:44 +00002536 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002537 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2538 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002539 break;
2540 }
2541
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002542 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002543 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2544 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002545 break;
2546 }
2547
2548 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002549 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002550 QualType(), QualType(), VarDecl::None,
2551 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002552 break;
2553 }
2554
Douglas Gregor2a491792009-04-13 22:49:25 +00002555 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002556 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002557 break;
2558 }
2559
2560 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002561 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002562 break;
2563 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002564 }
2565
Douglas Gregorc713da92009-04-21 22:25:48 +00002566 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002567 if (D) {
2568 LoadedDecl(Index, D);
2569 Reader.Visit(D);
2570 }
2571
Douglas Gregorc34897d2009-04-09 22:27:44 +00002572 // If this declaration is also a declaration context, get the
2573 // offsets for its tables of lexical and visible declarations.
2574 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2575 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2576 if (Offsets.first || Offsets.second) {
2577 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2578 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2579 DeclContextOffsets[DC] = Offsets;
2580 }
2581 }
2582 assert(Idx == Record.size());
2583
Douglas Gregor405b6432009-04-22 19:09:20 +00002584 if (Consumer) {
2585 // If we have deserialized a declaration that has a definition the
2586 // AST consumer might need to know about, notify the consumer
2587 // about that definition now.
2588 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2589 if (Var->isFileVarDecl() && Var->getInit()) {
2590 DeclGroupRef DG(Var);
2591 Consumer->HandleTopLevelDecl(DG);
2592 }
2593 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2594 if (Func->isThisDeclarationADefinition()) {
2595 DeclGroupRef DG(Func);
2596 Consumer->HandleTopLevelDecl(DG);
2597 }
Douglas Gregorafb99482009-04-24 23:42:14 +00002598 } else if (isa<ObjCProtocolDecl>(D)) {
2599 DeclGroupRef DG(D);
2600 Consumer->HandleTopLevelDecl(DG);
Douglas Gregor405b6432009-04-22 19:09:20 +00002601 }
2602 }
2603
Douglas Gregorc34897d2009-04-09 22:27:44 +00002604 return D;
2605}
2606
Douglas Gregorac8f2802009-04-10 17:25:41 +00002607QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002608 unsigned Quals = ID & 0x07;
2609 unsigned Index = ID >> 3;
2610
2611 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2612 QualType T;
2613 switch ((pch::PredefinedTypeIDs)Index) {
2614 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2615 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2616 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2617
2618 case pch::PREDEF_TYPE_CHAR_U_ID:
2619 case pch::PREDEF_TYPE_CHAR_S_ID:
2620 // FIXME: Check that the signedness of CharTy is correct!
2621 T = Context.CharTy;
2622 break;
2623
2624 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2625 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2626 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2627 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2628 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2629 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2630 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2631 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2632 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2633 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2634 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2635 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2636 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2637 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2638 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2639 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2640 }
2641
2642 assert(!T.isNull() && "Unknown predefined type");
2643 return T.getQualifiedType(Quals);
2644 }
2645
2646 Index -= pch::NUM_PREDEF_TYPE_IDS;
2647 if (!TypeAlreadyLoaded[Index]) {
2648 // Load the type from the PCH file.
2649 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2650 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2651 TypeAlreadyLoaded[Index] = true;
2652 }
2653
2654 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2655}
2656
Douglas Gregorac8f2802009-04-10 17:25:41 +00002657Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002658 if (ID == 0)
2659 return 0;
2660
2661 unsigned Index = ID - 1;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002662 assert(Index < DeclAlreadyLoaded.size() && "Declaration ID out of range");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002663 if (DeclAlreadyLoaded[Index])
2664 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2665
2666 // Load the declaration from the PCH file.
2667 return ReadDeclRecord(DeclOffsets[Index], Index);
2668}
2669
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002670Stmt *PCHReader::GetStmt(uint64_t Offset) {
2671 // Keep track of where we are in the stream, then jump back there
2672 // after reading this declaration.
2673 SavedStreamPosition SavedPosition(Stream);
2674
2675 Stream.JumpToBit(Offset);
2676 return ReadStmt();
2677}
2678
Douglas Gregorc34897d2009-04-09 22:27:44 +00002679bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002680 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002681 assert(DC->hasExternalLexicalStorage() &&
2682 "DeclContext has no lexical decls in storage");
2683 uint64_t Offset = DeclContextOffsets[DC].first;
2684 assert(Offset && "DeclContext has no lexical decls in storage");
2685
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002686 // Keep track of where we are in the stream, then jump back there
2687 // after reading this context.
2688 SavedStreamPosition SavedPosition(Stream);
2689
Douglas Gregorc34897d2009-04-09 22:27:44 +00002690 // Load the record containing all of the declarations lexically in
2691 // this context.
2692 Stream.JumpToBit(Offset);
2693 RecordData Record;
2694 unsigned Code = Stream.ReadCode();
2695 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002696 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002697 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2698
2699 // Load all of the declaration IDs
2700 Decls.clear();
2701 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002702 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002703 return false;
2704}
2705
2706bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2707 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2708 assert(DC->hasExternalVisibleStorage() &&
2709 "DeclContext has no visible decls in storage");
2710 uint64_t Offset = DeclContextOffsets[DC].second;
2711 assert(Offset && "DeclContext has no visible decls in storage");
2712
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002713 // Keep track of where we are in the stream, then jump back there
2714 // after reading this context.
2715 SavedStreamPosition SavedPosition(Stream);
2716
Douglas Gregorc34897d2009-04-09 22:27:44 +00002717 // Load the record containing all of the declarations visible in
2718 // this context.
2719 Stream.JumpToBit(Offset);
2720 RecordData Record;
2721 unsigned Code = Stream.ReadCode();
2722 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002723 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002724 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2725 if (Record.size() == 0)
2726 return false;
2727
2728 Decls.clear();
2729
2730 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002731 while (Idx < Record.size()) {
2732 Decls.push_back(VisibleDeclaration());
2733 Decls.back().Name = ReadDeclarationName(Record, Idx);
2734
Douglas Gregorc34897d2009-04-09 22:27:44 +00002735 unsigned Size = Record[Idx++];
2736 llvm::SmallVector<unsigned, 4> & LoadedDecls
2737 = Decls.back().Declarations;
2738 LoadedDecls.reserve(Size);
2739 for (unsigned I = 0; I < Size; ++I)
2740 LoadedDecls.push_back(Record[Idx++]);
2741 }
2742
Douglas Gregoraf136d92009-04-22 22:34:57 +00002743 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002744 return false;
2745}
2746
Douglas Gregor631f6c62009-04-14 00:24:19 +00002747void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002748 this->Consumer = Consumer;
2749
Douglas Gregor631f6c62009-04-14 00:24:19 +00002750 if (!Consumer)
2751 return;
2752
2753 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2754 Decl *D = GetDecl(ExternalDefinitions[I]);
2755 DeclGroupRef DG(D);
2756 Consumer->HandleTopLevelDecl(DG);
2757 }
2758}
2759
Douglas Gregorc34897d2009-04-09 22:27:44 +00002760void PCHReader::PrintStats() {
2761 std::fprintf(stderr, "*** PCH Statistics:\n");
2762
2763 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2764 TypeAlreadyLoaded.end(),
2765 true);
2766 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2767 DeclAlreadyLoaded.end(),
2768 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002769 unsigned NumIdentifiersLoaded = 0;
2770 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2771 if ((IdentifierData[I] & 0x01) == 0)
2772 ++NumIdentifiersLoaded;
2773 }
2774
Douglas Gregorc34897d2009-04-09 22:27:44 +00002775 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2776 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002777 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002778 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2779 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002780 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2781 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2782 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2783 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002784 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2785 NumStatementsRead, TotalNumStatements,
2786 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002787 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2788 NumMacrosRead, TotalNumMacros,
2789 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002790 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2791 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2792 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2793 * 100));
2794 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2795 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2796 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2797 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002798 std::fprintf(stderr, "\n");
2799}
2800
Douglas Gregorc713da92009-04-21 22:25:48 +00002801void PCHReader::InitializeSema(Sema &S) {
2802 SemaObj = &S;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002803 S.ExternalSource = this;
2804
Douglas Gregor2554cf22009-04-22 21:15:06 +00002805 // Makes sure any declarations that were deserialized "too early"
2806 // still get added to the identifier's declaration chains.
2807 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2808 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2809 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002810 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002811 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002812
2813 // If there were any tentative definitions, deserialize them and add
2814 // them to Sema's table of tentative definitions.
2815 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2816 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2817 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2818 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002819
2820 // If there were any locally-scoped external declarations,
2821 // deserialize them and add them to Sema's table of locally-scoped
2822 // external declarations.
2823 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2824 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2825 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2826 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002827}
2828
2829IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2830 // Try to find this name within our on-disk hash table
2831 PCHIdentifierLookupTable *IdTable
2832 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2833 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2834 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2835 if (Pos == IdTable->end())
2836 return 0;
2837
2838 // Dereferencing the iterator has the effect of building the
2839 // IdentifierInfo node and populating it with the various
2840 // declarations it needs.
2841 return *Pos;
2842}
2843
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002844std::pair<ObjCMethodList, ObjCMethodList>
2845PCHReader::ReadMethodPool(Selector Sel) {
2846 if (!MethodPoolLookupTable)
2847 return std::pair<ObjCMethodList, ObjCMethodList>();
2848
2849 // Try to find this selector within our on-disk hash table.
2850 PCHMethodPoolLookupTable *PoolTable
2851 = (PCHMethodPoolLookupTable*)MethodPoolLookupTable;
2852 PCHMethodPoolLookupTable::iterator Pos = PoolTable->find(Sel);
2853 if (Pos == PoolTable->end())
2854 return std::pair<ObjCMethodList, ObjCMethodList>();;
2855
2856 return *Pos;
2857}
2858
Douglas Gregorc713da92009-04-21 22:25:48 +00002859void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2860 assert(ID && "Non-zero identifier ID required");
2861 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2862}
2863
Chris Lattner29241862009-04-11 21:15:38 +00002864IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002865 if (ID == 0)
2866 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002867
Douglas Gregorc713da92009-04-21 22:25:48 +00002868 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002869 Error("No identifier table in PCH file");
2870 return 0;
2871 }
Chris Lattner29241862009-04-11 21:15:38 +00002872
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002873 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002874 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002875 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002876 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002877 }
Chris Lattner29241862009-04-11 21:15:38 +00002878
2879 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002880}
2881
Steve Naroff9e84d782009-04-23 10:39:46 +00002882Selector PCHReader::DecodeSelector(unsigned ID) {
2883 if (ID == 0)
2884 return Selector();
2885
2886 if (SelectorData.empty()) {
2887 Error("No selector table in PCH file");
2888 return Selector();
2889 }
2890
2891 if (ID > SelectorData.size()) {
2892 Error("Selector ID out of range");
2893 return Selector();
2894 }
2895 return SelectorData[ID-1];
2896}
2897
Douglas Gregorc34897d2009-04-09 22:27:44 +00002898DeclarationName
2899PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2900 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2901 switch (Kind) {
2902 case DeclarationName::Identifier:
2903 return DeclarationName(GetIdentifierInfo(Record, Idx));
2904
2905 case DeclarationName::ObjCZeroArgSelector:
2906 case DeclarationName::ObjCOneArgSelector:
2907 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002908 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002909
2910 case DeclarationName::CXXConstructorName:
2911 return Context.DeclarationNames.getCXXConstructorName(
2912 GetType(Record[Idx++]));
2913
2914 case DeclarationName::CXXDestructorName:
2915 return Context.DeclarationNames.getCXXDestructorName(
2916 GetType(Record[Idx++]));
2917
2918 case DeclarationName::CXXConversionFunctionName:
2919 return Context.DeclarationNames.getCXXConversionFunctionName(
2920 GetType(Record[Idx++]));
2921
2922 case DeclarationName::CXXOperatorName:
2923 return Context.DeclarationNames.getCXXOperatorName(
2924 (OverloadedOperatorKind)Record[Idx++]);
2925
2926 case DeclarationName::CXXUsingDirective:
2927 return DeclarationName::getUsingDirectiveName();
2928 }
2929
2930 // Required to silence GCC warning
2931 return DeclarationName();
2932}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002933
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002934/// \brief Read an integral value
2935llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2936 unsigned BitWidth = Record[Idx++];
2937 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2938 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2939 Idx += NumWords;
2940 return Result;
2941}
2942
2943/// \brief Read a signed integral value
2944llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2945 bool isUnsigned = Record[Idx++];
2946 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2947}
2948
Douglas Gregore2f37202009-04-14 21:55:33 +00002949/// \brief Read a floating-point value
2950llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002951 return llvm::APFloat(ReadAPInt(Record, Idx));
2952}
2953
Douglas Gregor1c507882009-04-15 21:30:51 +00002954// \brief Read a string
2955std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2956 unsigned Len = Record[Idx++];
2957 std::string Result(&Record[Idx], &Record[Idx] + Len);
2958 Idx += Len;
2959 return Result;
2960}
2961
2962/// \brief Reads attributes from the current stream position.
2963Attr *PCHReader::ReadAttributes() {
2964 unsigned Code = Stream.ReadCode();
2965 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2966 "Expected unabbreviated record"); (void)Code;
2967
2968 RecordData Record;
2969 unsigned Idx = 0;
2970 unsigned RecCode = Stream.ReadRecord(Code, Record);
2971 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2972 (void)RecCode;
2973
2974#define SIMPLE_ATTR(Name) \
2975 case Attr::Name: \
2976 New = ::new (Context) Name##Attr(); \
2977 break
2978
2979#define STRING_ATTR(Name) \
2980 case Attr::Name: \
2981 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2982 break
2983
2984#define UNSIGNED_ATTR(Name) \
2985 case Attr::Name: \
2986 New = ::new (Context) Name##Attr(Record[Idx++]); \
2987 break
2988
2989 Attr *Attrs = 0;
2990 while (Idx < Record.size()) {
2991 Attr *New = 0;
2992 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2993 bool IsInherited = Record[Idx++];
2994
2995 switch (Kind) {
2996 STRING_ATTR(Alias);
2997 UNSIGNED_ATTR(Aligned);
2998 SIMPLE_ATTR(AlwaysInline);
2999 SIMPLE_ATTR(AnalyzerNoReturn);
3000 STRING_ATTR(Annotate);
3001 STRING_ATTR(AsmLabel);
3002
3003 case Attr::Blocks:
3004 New = ::new (Context) BlocksAttr(
3005 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
3006 break;
3007
3008 case Attr::Cleanup:
3009 New = ::new (Context) CleanupAttr(
3010 cast<FunctionDecl>(GetDecl(Record[Idx++])));
3011 break;
3012
3013 SIMPLE_ATTR(Const);
3014 UNSIGNED_ATTR(Constructor);
3015 SIMPLE_ATTR(DLLExport);
3016 SIMPLE_ATTR(DLLImport);
3017 SIMPLE_ATTR(Deprecated);
3018 UNSIGNED_ATTR(Destructor);
3019 SIMPLE_ATTR(FastCall);
3020
3021 case Attr::Format: {
3022 std::string Type = ReadString(Record, Idx);
3023 unsigned FormatIdx = Record[Idx++];
3024 unsigned FirstArg = Record[Idx++];
3025 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
3026 break;
3027 }
3028
Chris Lattner15ce6cc2009-04-20 19:12:28 +00003029 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00003030
3031 case Attr::IBOutletKind:
3032 New = ::new (Context) IBOutletAttr();
3033 break;
3034
3035 SIMPLE_ATTR(NoReturn);
3036 SIMPLE_ATTR(NoThrow);
3037 SIMPLE_ATTR(Nodebug);
3038 SIMPLE_ATTR(Noinline);
3039
3040 case Attr::NonNull: {
3041 unsigned Size = Record[Idx++];
3042 llvm::SmallVector<unsigned, 16> ArgNums;
3043 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
3044 Idx += Size;
3045 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
3046 break;
3047 }
3048
3049 SIMPLE_ATTR(ObjCException);
3050 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb98860c2009-04-25 00:17:17 +00003051 SIMPLE_ATTR(ObjCOwnershipRetain);
Ted Kremenekaa6e3182009-04-24 23:09:54 +00003052 SIMPLE_ATTR(ObjCOwnershipReturns);
Douglas Gregor1c507882009-04-15 21:30:51 +00003053 SIMPLE_ATTR(Overloadable);
3054 UNSIGNED_ATTR(Packed);
3055 SIMPLE_ATTR(Pure);
3056 UNSIGNED_ATTR(Regparm);
3057 STRING_ATTR(Section);
3058 SIMPLE_ATTR(StdCall);
3059 SIMPLE_ATTR(TransparentUnion);
3060 SIMPLE_ATTR(Unavailable);
3061 SIMPLE_ATTR(Unused);
3062 SIMPLE_ATTR(Used);
3063
3064 case Attr::Visibility:
3065 New = ::new (Context) VisibilityAttr(
3066 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
3067 break;
3068
3069 SIMPLE_ATTR(WarnUnusedResult);
3070 SIMPLE_ATTR(Weak);
3071 SIMPLE_ATTR(WeakImport);
3072 }
3073
3074 assert(New && "Unable to decode attribute?");
3075 New->setInherited(IsInherited);
3076 New->setNext(Attrs);
3077 Attrs = New;
3078 }
3079#undef UNSIGNED_ATTR
3080#undef STRING_ATTR
3081#undef SIMPLE_ATTR
3082
3083 // The list of attributes was built backwards. Reverse the list
3084 // before returning it.
3085 Attr *PrevAttr = 0, *NextAttr = 0;
3086 while (Attrs) {
3087 NextAttr = Attrs->getNext();
3088 Attrs->setNext(PrevAttr);
3089 PrevAttr = Attrs;
3090 Attrs = NextAttr;
3091 }
3092
3093 return PrevAttr;
3094}
3095
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003096Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00003097 // Within the bitstream, expressions are stored in Reverse Polish
3098 // Notation, with each of the subexpressions preceding the
3099 // expression they are stored in. To evaluate expressions, we
3100 // continue reading expressions and placing them on the stack, with
3101 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003102 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00003103 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003104 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00003105 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003106 llvm::SmallVector<Stmt *, 16> StmtStack;
3107 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003108 Stmt::EmptyShell Empty;
3109
Douglas Gregora151ba42009-04-14 23:32:43 +00003110 while (true) {
3111 unsigned Code = Stream.ReadCode();
3112 if (Code == llvm::bitc::END_BLOCK) {
3113 if (Stream.ReadBlockEnd()) {
3114 Error("Error at end of Source Manager block");
3115 return 0;
3116 }
3117 break;
3118 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003119
Douglas Gregora151ba42009-04-14 23:32:43 +00003120 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3121 // No known subblocks, always skip them.
3122 Stream.ReadSubBlockID();
3123 if (Stream.SkipBlock()) {
3124 Error("Malformed block record");
3125 return 0;
3126 }
3127 continue;
3128 }
Douglas Gregore2f37202009-04-14 21:55:33 +00003129
Douglas Gregora151ba42009-04-14 23:32:43 +00003130 if (Code == llvm::bitc::DEFINE_ABBREV) {
3131 Stream.ReadAbbrevRecord();
3132 continue;
3133 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003134
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003135 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003136 Idx = 0;
3137 Record.clear();
3138 bool Finished = false;
3139 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003140 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003141 Finished = true;
3142 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003143
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003144 case pch::STMT_NULL_PTR:
3145 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003146 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003147
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003148 case pch::STMT_NULL:
3149 S = new (Context) NullStmt(Empty);
3150 break;
3151
3152 case pch::STMT_COMPOUND:
3153 S = new (Context) CompoundStmt(Empty);
3154 break;
3155
3156 case pch::STMT_CASE:
3157 S = new (Context) CaseStmt(Empty);
3158 break;
3159
3160 case pch::STMT_DEFAULT:
3161 S = new (Context) DefaultStmt(Empty);
3162 break;
3163
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003164 case pch::STMT_LABEL:
3165 S = new (Context) LabelStmt(Empty);
3166 break;
3167
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003168 case pch::STMT_IF:
3169 S = new (Context) IfStmt(Empty);
3170 break;
3171
3172 case pch::STMT_SWITCH:
3173 S = new (Context) SwitchStmt(Empty);
3174 break;
3175
Douglas Gregora6b503f2009-04-17 00:16:09 +00003176 case pch::STMT_WHILE:
3177 S = new (Context) WhileStmt(Empty);
3178 break;
3179
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003180 case pch::STMT_DO:
3181 S = new (Context) DoStmt(Empty);
3182 break;
3183
3184 case pch::STMT_FOR:
3185 S = new (Context) ForStmt(Empty);
3186 break;
3187
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003188 case pch::STMT_GOTO:
3189 S = new (Context) GotoStmt(Empty);
3190 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003191
3192 case pch::STMT_INDIRECT_GOTO:
3193 S = new (Context) IndirectGotoStmt(Empty);
3194 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003195
Douglas Gregora6b503f2009-04-17 00:16:09 +00003196 case pch::STMT_CONTINUE:
3197 S = new (Context) ContinueStmt(Empty);
3198 break;
3199
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003200 case pch::STMT_BREAK:
3201 S = new (Context) BreakStmt(Empty);
3202 break;
3203
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003204 case pch::STMT_RETURN:
3205 S = new (Context) ReturnStmt(Empty);
3206 break;
3207
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003208 case pch::STMT_DECL:
3209 S = new (Context) DeclStmt(Empty);
3210 break;
3211
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003212 case pch::STMT_ASM:
3213 S = new (Context) AsmStmt(Empty);
3214 break;
3215
Douglas Gregora151ba42009-04-14 23:32:43 +00003216 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003217 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003218 break;
3219
3220 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003221 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003222 break;
3223
3224 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003225 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003226 break;
3227
3228 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003229 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003230 break;
3231
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003232 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003233 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003234 break;
3235
Douglas Gregor596e0932009-04-15 16:35:07 +00003236 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003237 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003238 Record[PCHStmtReader::NumExprFields + 1]);
3239 break;
3240
Douglas Gregora151ba42009-04-14 23:32:43 +00003241 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003242 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003243 break;
3244
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003245 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003246 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003247 break;
3248
Douglas Gregor12d74052009-04-15 15:58:59 +00003249 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003250 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003251 break;
3252
3253 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003254 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003255 break;
3256
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003257 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003258 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003259 break;
3260
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003261 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003262 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003263 break;
3264
3265 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003266 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003267 break;
3268
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003269 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003270 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003271 break;
3272
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003273 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003274 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003275 break;
3276
3277 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003278 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003279 break;
3280
Douglas Gregora151ba42009-04-14 23:32:43 +00003281 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003282 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003283 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003284
3285 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003286 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003287 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003288
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003289 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003290 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003291 break;
3292
Douglas Gregorec0b8292009-04-15 23:02:49 +00003293 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003294 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003295 break;
3296
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003297 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003298 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003299 break;
3300
3301 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003302 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003303 Record[PCHStmtReader::NumExprFields] - 1);
3304
3305 break;
3306
3307 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003308 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003309 break;
3310
Douglas Gregorec0b8292009-04-15 23:02:49 +00003311 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003312 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003313 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003314
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003315 case pch::EXPR_ADDR_LABEL:
3316 S = new (Context) AddrLabelExpr(Empty);
3317 break;
3318
Douglas Gregoreca12f62009-04-17 19:05:30 +00003319 case pch::EXPR_STMT:
3320 S = new (Context) StmtExpr(Empty);
3321 break;
3322
Douglas Gregor209d4622009-04-15 23:33:31 +00003323 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003324 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003325 break;
3326
3327 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003328 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003329 break;
3330
3331 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003332 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003333 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003334
3335 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003336 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003337 break;
3338
Douglas Gregore246b742009-04-17 19:21:43 +00003339 case pch::EXPR_BLOCK:
3340 S = new (Context) BlockExpr(Empty);
3341 break;
3342
Douglas Gregor725e94b2009-04-16 00:01:45 +00003343 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003344 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003345 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003346
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003347 case pch::EXPR_OBJC_STRING_LITERAL:
3348 S = new (Context) ObjCStringLiteral(Empty);
3349 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003350 case pch::EXPR_OBJC_ENCODE:
3351 S = new (Context) ObjCEncodeExpr(Empty);
3352 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003353 case pch::EXPR_OBJC_SELECTOR_EXPR:
3354 S = new (Context) ObjCSelectorExpr(Empty);
3355 break;
3356 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3357 S = new (Context) ObjCProtocolExpr(Empty);
3358 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003359 }
3360
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003361 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003362 if (Finished)
3363 break;
3364
Douglas Gregor456e0952009-04-17 22:13:46 +00003365 ++NumStatementsRead;
3366
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003367 if (S) {
3368 unsigned NumSubStmts = Reader.Visit(S);
3369 while (NumSubStmts > 0) {
3370 StmtStack.pop_back();
3371 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003372 }
3373 }
3374
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003375 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003376 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003377 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003378 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003379 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003380 return StmtStack.back();
3381}
3382
3383Expr *PCHReader::ReadExpr() {
3384 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003385}
3386
Douglas Gregor179cfb12009-04-10 20:39:37 +00003387DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003388 return Diag(SourceLocation(), DiagID);
3389}
3390
3391DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3392 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003393 Context.getSourceManager()),
3394 DiagID);
3395}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003396
Douglas Gregorc713da92009-04-21 22:25:48 +00003397/// \brief Retrieve the identifier table associated with the
3398/// preprocessor.
3399IdentifierTable &PCHReader::getIdentifierTable() {
3400 return PP.getIdentifierTable();
3401}
3402
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003403/// \brief Record that the given ID maps to the given switch-case
3404/// statement.
3405void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3406 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3407 SwitchCaseStmts[ID] = SC;
3408}
3409
3410/// \brief Retrieve the switch-case statement with the given ID.
3411SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3412 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3413 return SwitchCaseStmts[ID];
3414}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003415
3416/// \brief Record that the given label statement has been
3417/// deserialized and has the given ID.
3418void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3419 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3420 "Deserialized label twice");
3421 LabelStmts[ID] = S;
3422
3423 // If we've already seen any goto statements that point to this
3424 // label, resolve them now.
3425 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3426 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3427 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3428 Goto->second->setLabel(S);
3429 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003430
3431 // If we've already seen any address-label statements that point to
3432 // this label, resolve them now.
3433 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3434 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3435 = UnresolvedAddrLabelExprs.equal_range(ID);
3436 for (AddrLabelIter AddrLabel = AddrLabels.first;
3437 AddrLabel != AddrLabels.second; ++AddrLabel)
3438 AddrLabel->second->setLabel(S);
3439 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003440}
3441
3442/// \brief Set the label of the given statement to the label
3443/// identified by ID.
3444///
3445/// Depending on the order in which the label and other statements
3446/// referencing that label occur, this operation may complete
3447/// immediately (updating the statement) or it may queue the
3448/// statement to be back-patched later.
3449void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3450 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3451 if (Label != LabelStmts.end()) {
3452 // We've already seen this label, so set the label of the goto and
3453 // we're done.
3454 S->setLabel(Label->second);
3455 } else {
3456 // We haven't seen this label yet, so add this goto to the set of
3457 // unresolved goto statements.
3458 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3459 }
3460}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003461
3462/// \brief Set the label of the given expression to the label
3463/// identified by ID.
3464///
3465/// Depending on the order in which the label and other statements
3466/// referencing that label occur, this operation may complete
3467/// immediately (updating the statement) or it may queue the
3468/// statement to be back-patched later.
3469void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3470 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3471 if (Label != LabelStmts.end()) {
3472 // We've already seen this label, so set the label of the
3473 // label-address expression and we're done.
3474 S->setLabel(Label->second);
3475 } else {
3476 // We haven't seen this label yet, so add this label-address
3477 // expression to the set of unresolved label-address expressions.
3478 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3479 }
3480}