blob: 62d0ba4f9f095b7da54c0ab3f8be2c9bed44d051 [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++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000237 unsigned NumIvars = Record[Idx++];
238 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
239 IVars.reserve(NumIvars);
240 for (unsigned I = 0; I != NumIvars; ++I)
241 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
242 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
Douglas Gregorae660c72009-04-23 22:34:55 +0000243 ID->setCategoryList(
244 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000245 ID->setForwardDecl(Record[Idx++]);
246 ID->setImplicitInterfaceDecl(Record[Idx++]);
247 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
248 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000249 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000250}
251
252void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
253 VisitFieldDecl(IVD);
254 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
255}
256
Steve Naroff97b53bd2009-04-21 15:12:33 +0000257void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
258 VisitObjCContainerDecl(PD);
259 PD->setForwardDecl(Record[Idx++]);
260 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
261 unsigned NumProtoRefs = Record[Idx++];
262 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
263 ProtoRefs.reserve(NumProtoRefs);
264 for (unsigned I = 0; I != NumProtoRefs; ++I)
265 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
266 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
267}
268
269void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
270 VisitFieldDecl(FD);
271}
272
273void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
274 VisitDecl(CD);
275 unsigned NumClassRefs = Record[Idx++];
276 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
277 ClassRefs.reserve(NumClassRefs);
278 for (unsigned I = 0; I != NumClassRefs; ++I)
279 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
280 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
281}
282
283void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
284 VisitDecl(FPD);
285 unsigned NumProtoRefs = Record[Idx++];
286 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
287 ProtoRefs.reserve(NumProtoRefs);
288 for (unsigned I = 0; I != NumProtoRefs; ++I)
289 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
290 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
291}
292
293void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
294 VisitObjCContainerDecl(CD);
295 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
296 unsigned NumProtoRefs = Record[Idx++];
297 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
298 ProtoRefs.reserve(NumProtoRefs);
299 for (unsigned I = 0; I != NumProtoRefs; ++I)
300 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
301 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
Steve Naroffbac01db2009-04-24 16:59:10 +0000302 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000303 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
304}
305
306void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
307 VisitNamedDecl(CAD);
308 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
309}
310
311void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
312 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000313 D->setType(Reader.GetType(Record[Idx++]));
314 // FIXME: stable encoding
315 D->setPropertyAttributes(
316 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
317 // FIXME: stable encoding
318 D->setPropertyImplementation(
319 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
320 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
321 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
322 D->setGetterMethodDecl(
323 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
324 D->setSetterMethodDecl(
325 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
326 D->setPropertyIvarDecl(
327 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000328}
329
330void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000331 VisitNamedDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000332 D->setClassInterface(
333 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
334 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000335}
336
337void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
338 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000339 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000340}
341
342void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
343 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000344 D->setSuperClass(
345 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000346}
347
348
349void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
350 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000351 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 D->setPropertyDecl(
353 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
354 D->setPropertyIvarDecl(
355 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000356}
357
Douglas Gregor982365e2009-04-13 21:20:57 +0000358void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
359 VisitValueDecl(FD);
360 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000361 if (Record[Idx++])
362 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000363}
364
Douglas Gregorc34897d2009-04-09 22:27:44 +0000365void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
366 VisitValueDecl(VD);
367 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
368 VD->setThreadSpecified(Record[Idx++]);
369 VD->setCXXDirectInitializer(Record[Idx++]);
370 VD->setDeclaredInCondition(Record[Idx++]);
371 VD->setPreviousDeclaration(
372 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
373 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000374 if (Record[Idx++])
375 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000376}
377
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000378void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
379 VisitVarDecl(PD);
380 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000381 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000382}
383
384void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
385 VisitParmVarDecl(PD);
386 PD->setOriginalType(Reader.GetType(Record[Idx++]));
387}
388
Douglas Gregor2a491792009-04-13 22:49:25 +0000389void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
390 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000391 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000392}
393
394void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
395 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000396 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000397 unsigned NumParams = Record[Idx++];
398 llvm::SmallVector<ParmVarDecl *, 16> Params;
399 Params.reserve(NumParams);
400 for (unsigned I = 0; I != NumParams; ++I)
401 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
402 BD->setParams(Reader.getContext(), &Params[0], NumParams);
403}
404
Douglas Gregorc34897d2009-04-09 22:27:44 +0000405std::pair<uint64_t, uint64_t>
406PCHDeclReader::VisitDeclContext(DeclContext *DC) {
407 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000408 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000409 return std::make_pair(LexicalOffset, VisibleOffset);
410}
411
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000412//===----------------------------------------------------------------------===//
413// Statement/expression deserialization
414//===----------------------------------------------------------------------===//
415namespace {
416 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000417 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000418 PCHReader &Reader;
419 const PCHReader::RecordData &Record;
420 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000421 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000422
423 public:
424 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000425 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
426 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000427
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000428 /// \brief The number of record fields required for the Stmt class
429 /// itself.
430 static const unsigned NumStmtFields = 0;
431
Douglas Gregor596e0932009-04-15 16:35:07 +0000432 /// \brief The number of record fields required for the Expr class
433 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000434 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000435
Douglas Gregora151ba42009-04-14 23:32:43 +0000436 // Each of the Visit* functions reads in part of the expression
437 // from the given record and the current expression stack, then
438 // return the total number of operands that it read from the
439 // expression stack.
440
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000441 unsigned VisitStmt(Stmt *S);
442 unsigned VisitNullStmt(NullStmt *S);
443 unsigned VisitCompoundStmt(CompoundStmt *S);
444 unsigned VisitSwitchCase(SwitchCase *S);
445 unsigned VisitCaseStmt(CaseStmt *S);
446 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000447 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000448 unsigned VisitIfStmt(IfStmt *S);
449 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000450 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000451 unsigned VisitDoStmt(DoStmt *S);
452 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000453 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000454 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000455 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000456 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000457 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000458 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000459 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000460 unsigned VisitExpr(Expr *E);
461 unsigned VisitPredefinedExpr(PredefinedExpr *E);
462 unsigned VisitDeclRefExpr(DeclRefExpr *E);
463 unsigned VisitIntegerLiteral(IntegerLiteral *E);
464 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000465 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000466 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000467 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000468 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000469 unsigned VisitUnaryOperator(UnaryOperator *E);
470 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000471 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000472 unsigned VisitCallExpr(CallExpr *E);
473 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000474 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000475 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000476 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
477 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000478 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000479 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
480 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000481 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000482 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000483 unsigned VisitInitListExpr(InitListExpr *E);
484 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
485 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000486 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000487 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000488 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000489 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
490 unsigned VisitChooseExpr(ChooseExpr *E);
491 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000492 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000493 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000494 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000495 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000496 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000497 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
498 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000499 };
500}
501
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000502unsigned PCHStmtReader::VisitStmt(Stmt *S) {
503 assert(Idx == NumStmtFields && "Incorrect statement field count");
504 return 0;
505}
506
507unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
508 VisitStmt(S);
509 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
510 return 0;
511}
512
513unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
514 VisitStmt(S);
515 unsigned NumStmts = Record[Idx++];
516 S->setStmts(Reader.getContext(),
517 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
518 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
519 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
520 return NumStmts;
521}
522
523unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
524 VisitStmt(S);
525 Reader.RecordSwitchCaseID(S, Record[Idx++]);
526 return 0;
527}
528
529unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
530 VisitSwitchCase(S);
531 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
532 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
533 S->setSubStmt(StmtStack.back());
534 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
535 return 3;
536}
537
538unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
539 VisitSwitchCase(S);
540 S->setSubStmt(StmtStack.back());
541 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 return 1;
543}
544
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000545unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
546 VisitStmt(S);
547 S->setID(Reader.GetIdentifierInfo(Record, Idx));
548 S->setSubStmt(StmtStack.back());
549 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
550 Reader.RecordLabelStmt(S, Record[Idx++]);
551 return 1;
552}
553
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000554unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
555 VisitStmt(S);
556 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
557 S->setThen(StmtStack[StmtStack.size() - 2]);
558 S->setElse(StmtStack[StmtStack.size() - 1]);
559 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
560 return 3;
561}
562
563unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
564 VisitStmt(S);
565 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
566 S->setBody(StmtStack.back());
567 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
568 SwitchCase *PrevSC = 0;
569 for (unsigned N = Record.size(); Idx != N; ++Idx) {
570 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
571 if (PrevSC)
572 PrevSC->setNextSwitchCase(SC);
573 else
574 S->setSwitchCaseList(SC);
575 PrevSC = SC;
576 }
577 return 2;
578}
579
Douglas Gregora6b503f2009-04-17 00:16:09 +0000580unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
581 VisitStmt(S);
582 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
583 S->setBody(StmtStack.back());
584 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
585 return 2;
586}
587
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000588unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
589 VisitStmt(S);
590 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
591 S->setBody(StmtStack.back());
592 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
593 return 2;
594}
595
596unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
597 VisitStmt(S);
598 S->setInit(StmtStack[StmtStack.size() - 4]);
599 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
600 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
601 S->setBody(StmtStack.back());
602 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 return 4;
604}
605
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000606unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
607 VisitStmt(S);
608 Reader.SetLabelOf(S, Record[Idx++]);
609 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
610 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
611 return 0;
612}
613
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000614unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
615 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000616 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000617 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
618 return 1;
619}
620
Douglas Gregora6b503f2009-04-17 00:16:09 +0000621unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
622 VisitStmt(S);
623 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
624 return 0;
625}
626
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000627unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
628 VisitStmt(S);
629 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630 return 0;
631}
632
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000633unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
634 VisitStmt(S);
635 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
636 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 1;
638}
639
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000640unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
641 VisitStmt(S);
642 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
643 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
644
645 if (Idx + 1 == Record.size()) {
646 // Single declaration
647 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
648 } else {
649 llvm::SmallVector<Decl *, 16> Decls;
650 Decls.reserve(Record.size() - Idx);
651 for (unsigned N = Record.size(); Idx != N; ++Idx)
652 Decls.push_back(Reader.GetDecl(Record[Idx]));
653 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
654 &Decls[0], Decls.size())));
655 }
656 return 0;
657}
658
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000659unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
660 VisitStmt(S);
661 unsigned NumOutputs = Record[Idx++];
662 unsigned NumInputs = Record[Idx++];
663 unsigned NumClobbers = Record[Idx++];
664 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
665 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
666 S->setVolatile(Record[Idx++]);
667 S->setSimple(Record[Idx++]);
668
669 unsigned StackIdx
670 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
671 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
672
673 // Outputs and inputs
674 llvm::SmallVector<std::string, 16> Names;
675 llvm::SmallVector<StringLiteral*, 16> Constraints;
676 llvm::SmallVector<Stmt*, 16> Exprs;
677 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
678 Names.push_back(Reader.ReadString(Record, Idx));
679 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
680 Exprs.push_back(StmtStack[StackIdx++]);
681 }
682 S->setOutputsAndInputs(NumOutputs, NumInputs,
683 &Names[0], &Constraints[0], &Exprs[0]);
684
685 // Constraints
686 llvm::SmallVector<StringLiteral*, 16> Clobbers;
687 for (unsigned I = 0; I != NumClobbers; ++I)
688 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
689 S->setClobbers(&Clobbers[0], NumClobbers);
690
691 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
692 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
693}
694
Douglas Gregora151ba42009-04-14 23:32:43 +0000695unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000696 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000697 E->setType(Reader.GetType(Record[Idx++]));
698 E->setTypeDependent(Record[Idx++]);
699 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000700 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000701 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000702}
703
Douglas Gregora151ba42009-04-14 23:32:43 +0000704unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000705 VisitExpr(E);
706 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
707 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000708 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000709}
710
Douglas Gregora151ba42009-04-14 23:32:43 +0000711unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000712 VisitExpr(E);
713 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
714 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000715 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000716}
717
Douglas Gregora151ba42009-04-14 23:32:43 +0000718unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000719 VisitExpr(E);
720 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
721 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000722 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000723}
724
Douglas Gregora151ba42009-04-14 23:32:43 +0000725unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000726 VisitExpr(E);
727 E->setValue(Reader.ReadAPFloat(Record, Idx));
728 E->setExact(Record[Idx++]);
729 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000730 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000731}
732
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000733unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
734 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000735 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000736 return 1;
737}
738
Douglas Gregor596e0932009-04-15 16:35:07 +0000739unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
740 VisitExpr(E);
741 unsigned Len = Record[Idx++];
742 assert(Record[Idx] == E->getNumConcatenated() &&
743 "Wrong number of concatenated tokens!");
744 ++Idx;
745 E->setWide(Record[Idx++]);
746
747 // Read string data
748 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
749 E->setStrData(Reader.getContext(), &Str[0], Len);
750 Idx += Len;
751
752 // Read source locations
753 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
754 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
755
756 return 0;
757}
758
Douglas Gregora151ba42009-04-14 23:32:43 +0000759unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000760 VisitExpr(E);
761 E->setValue(Record[Idx++]);
762 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
763 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000764 return 0;
765}
766
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000767unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
768 VisitExpr(E);
769 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
770 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000771 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000772 return 1;
773}
774
Douglas Gregor12d74052009-04-15 15:58:59 +0000775unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
776 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000777 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000778 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
779 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
780 return 1;
781}
782
783unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
784 VisitExpr(E);
785 E->setSizeof(Record[Idx++]);
786 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000787 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000788 ++Idx;
789 } else {
790 E->setArgument(Reader.GetType(Record[Idx++]));
791 }
792 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 return E->isArgumentType()? 0 : 1;
795}
796
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000797unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
798 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000799 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
800 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000801 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
802 return 2;
803}
804
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000805unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
806 VisitExpr(E);
807 E->setNumArgs(Reader.getContext(), Record[Idx++]);
808 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000809 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000810 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000811 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000812 return E->getNumArgs() + 1;
813}
814
815unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
816 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000817 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000818 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
819 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
820 E->setArrow(Record[Idx++]);
821 return 1;
822}
823
Douglas Gregora151ba42009-04-14 23:32:43 +0000824unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
825 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000826 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000827 return 1;
828}
829
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000830unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
831 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000832 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
833 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000834 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
835 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
836 return 2;
837}
838
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000839unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
840 VisitBinaryOperator(E);
841 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
842 E->setComputationResultType(Reader.GetType(Record[Idx++]));
843 return 2;
844}
845
846unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
847 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000848 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
849 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
850 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000851 return 3;
852}
853
Douglas Gregora151ba42009-04-14 23:32:43 +0000854unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
855 VisitCastExpr(E);
856 E->setLvalueCast(Record[Idx++]);
857 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000858}
859
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000860unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
861 VisitCastExpr(E);
862 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
863 return 1;
864}
865
866unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
867 VisitExplicitCastExpr(E);
868 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
869 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
870 return 1;
871}
872
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000873unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
874 VisitExpr(E);
875 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000876 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000877 E->setFileScope(Record[Idx++]);
878 return 1;
879}
880
Douglas Gregorec0b8292009-04-15 23:02:49 +0000881unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
882 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000883 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000884 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
885 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
886 return 1;
887}
888
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000889unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
890 VisitExpr(E);
891 unsigned NumInits = Record[Idx++];
892 E->reserveInits(NumInits);
893 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000894 E->updateInit(I,
895 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
896 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000897 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
898 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
899 E->setInitializedFieldInUnion(
900 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
901 E->sawArrayRangeDesignator(Record[Idx++]);
902 return NumInits + 1;
903}
904
905unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
906 typedef DesignatedInitExpr::Designator Designator;
907
908 VisitExpr(E);
909 unsigned NumSubExprs = Record[Idx++];
910 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
911 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000912 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000913 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
914 E->setGNUSyntax(Record[Idx++]);
915
916 llvm::SmallVector<Designator, 4> Designators;
917 while (Idx < Record.size()) {
918 switch ((pch::DesignatorTypes)Record[Idx++]) {
919 case pch::DESIG_FIELD_DECL: {
920 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
921 SourceLocation DotLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 SourceLocation FieldLoc
924 = SourceLocation::getFromRawEncoding(Record[Idx++]);
925 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
926 FieldLoc));
927 Designators.back().setField(Field);
928 break;
929 }
930
931 case pch::DESIG_FIELD_NAME: {
932 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
933 SourceLocation DotLoc
934 = SourceLocation::getFromRawEncoding(Record[Idx++]);
935 SourceLocation FieldLoc
936 = SourceLocation::getFromRawEncoding(Record[Idx++]);
937 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
938 break;
939 }
940
941 case pch::DESIG_ARRAY: {
942 unsigned Index = Record[Idx++];
943 SourceLocation LBracketLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 SourceLocation RBracketLoc
946 = SourceLocation::getFromRawEncoding(Record[Idx++]);
947 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
948 break;
949 }
950
951 case pch::DESIG_ARRAY_RANGE: {
952 unsigned Index = Record[Idx++];
953 SourceLocation LBracketLoc
954 = SourceLocation::getFromRawEncoding(Record[Idx++]);
955 SourceLocation EllipsisLoc
956 = SourceLocation::getFromRawEncoding(Record[Idx++]);
957 SourceLocation RBracketLoc
958 = SourceLocation::getFromRawEncoding(Record[Idx++]);
959 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
960 RBracketLoc));
961 break;
962 }
963 }
964 }
965 E->setDesignators(&Designators[0], Designators.size());
966
967 return NumSubExprs;
968}
969
970unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
971 VisitExpr(E);
972 return 0;
973}
974
Douglas Gregorec0b8292009-04-15 23:02:49 +0000975unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
976 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000977 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000978 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 return 1;
981}
982
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000983unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
984 VisitExpr(E);
985 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
986 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 Reader.SetLabelOf(E, Record[Idx++]);
988 return 0;
989}
990
Douglas Gregoreca12f62009-04-17 19:05:30 +0000991unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
992 VisitExpr(E);
993 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
994 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
995 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
996 return 1;
997}
998
Douglas Gregor209d4622009-04-15 23:33:31 +0000999unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1000 VisitExpr(E);
1001 E->setArgType1(Reader.GetType(Record[Idx++]));
1002 E->setArgType2(Reader.GetType(Record[Idx++]));
1003 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1004 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1005 return 0;
1006}
1007
1008unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1009 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001010 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1011 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1012 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001013 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1014 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1015 return 3;
1016}
1017
1018unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1019 VisitExpr(E);
1020 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1021 return 0;
1022}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001023
Douglas Gregor725e94b2009-04-16 00:01:45 +00001024unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1025 VisitExpr(E);
1026 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001027 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001028 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1029 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1030 return NumExprs;
1031}
1032
Douglas Gregore246b742009-04-17 19:21:43 +00001033unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1034 VisitExpr(E);
1035 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1036 E->setHasBlockDeclRefExprs(Record[Idx++]);
1037 return 0;
1038}
1039
Douglas Gregor725e94b2009-04-16 00:01:45 +00001040unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1041 VisitExpr(E);
1042 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1043 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1044 E->setByRef(Record[Idx++]);
1045 return 0;
1046}
1047
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001048//===----------------------------------------------------------------------===//
1049// Objective-C Expressions and Statements
1050
1051unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1052 VisitExpr(E);
1053 E->setString(cast<StringLiteral>(StmtStack.back()));
1054 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1055 return 1;
1056}
1057
Chris Lattner80f83c62009-04-22 05:57:30 +00001058unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1059 VisitExpr(E);
1060 E->setEncodedType(Reader.GetType(Record[Idx++]));
1061 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1062 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1063 return 0;
1064}
1065
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001066unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1067 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +00001068 E->setSelector(Reader.GetSelector(Record, Idx));
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001069 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1070 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1071 return 0;
1072}
1073
1074unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1075 VisitExpr(E);
1076 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1077 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1078 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1079 return 0;
1080}
1081
Chris Lattner80f83c62009-04-22 05:57:30 +00001082
Douglas Gregorc713da92009-04-21 22:25:48 +00001083//===----------------------------------------------------------------------===//
1084// PCH reader implementation
1085//===----------------------------------------------------------------------===//
1086
1087namespace {
1088class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1089 PCHReader &Reader;
1090
1091 // If we know the IdentifierInfo in advance, it is here and we will
1092 // not build a new one. Used when deserializing information about an
1093 // identifier that was constructed before the PCH file was read.
1094 IdentifierInfo *KnownII;
1095
1096public:
1097 typedef IdentifierInfo * data_type;
1098
1099 typedef const std::pair<const char*, unsigned> external_key_type;
1100
1101 typedef external_key_type internal_key_type;
1102
1103 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1104 : Reader(Reader), KnownII(II) { }
1105
1106 static bool EqualKey(const internal_key_type& a,
1107 const internal_key_type& b) {
1108 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1109 : false;
1110 }
1111
1112 static unsigned ComputeHash(const internal_key_type& a) {
1113 return BernsteinHash(a.first, a.second);
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 static std::pair<const char*, unsigned>
1129 ReadKey(const unsigned char* d, unsigned n) {
1130 assert(n >= 2 && d[n-1] == '\0');
1131 return std::make_pair((const char*) d, n-1);
1132 }
1133
1134 IdentifierInfo *ReadData(const internal_key_type& k,
1135 const unsigned char* d,
1136 unsigned DataLen) {
1137 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001138 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001139 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1140 Bits >>= 1;
1141 bool Poisoned = Bits & 0x01;
1142 Bits >>= 1;
1143 bool ExtensionToken = Bits & 0x01;
1144 Bits >>= 1;
1145 bool hasMacroDefinition = Bits & 0x01;
1146 Bits >>= 1;
1147 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1148 Bits >>= 10;
1149 unsigned TokenID = Bits & 0xFF;
1150 Bits >>= 8;
1151
Douglas Gregorc713da92009-04-21 22:25:48 +00001152 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001153 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001154 DataLen -= 8;
1155
1156 // Build the IdentifierInfo itself and link the identifier ID with
1157 // the new IdentifierInfo.
1158 IdentifierInfo *II = KnownII;
1159 if (!II)
1160 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1161 k.first, k.first + k.second);
1162 Reader.SetIdentifierInfo(ID, II);
1163
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001164 // Set or check the various bits in the IdentifierInfo structure.
1165 // FIXME: Load token IDs lazily, too?
1166 assert((unsigned)II->getTokenID() == TokenID &&
1167 "Incorrect token ID loaded");
1168 (void)TokenID;
1169 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1170 assert(II->isExtensionToken() == ExtensionToken &&
1171 "Incorrect extension token flag");
1172 (void)ExtensionToken;
1173 II->setIsPoisoned(Poisoned);
1174 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1175 "Incorrect C++ operator keyword flag");
1176 (void)CPlusPlusOperatorKeyword;
1177
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001178 // If this identifier is a macro, deserialize the macro
1179 // definition.
1180 if (hasMacroDefinition) {
1181 uint32_t Offset = ReadUnalignedLE64(d);
1182 Reader.ReadMacroRecord(Offset);
1183 DataLen -= 8;
1184 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001185
1186 // Read all of the declarations visible at global scope with this
1187 // name.
1188 Sema *SemaObj = Reader.getSema();
1189 while (DataLen > 0) {
1190 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001191 if (SemaObj) {
1192 // Introduce this declaration into the translation-unit scope
1193 // and add it to the declaration chain for this identifier, so
1194 // that (unqualified) name lookup will find it.
1195 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1196 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1197 } else {
1198 // Queue this declaration so that it will be added to the
1199 // translation unit scope and identifier's declaration chain
1200 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001201 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001202 }
1203
1204 DataLen -= 4;
1205 }
1206 return II;
1207 }
1208};
1209
1210} // end anonymous namespace
1211
1212/// \brief The on-disk hash table used to contain information about
1213/// all of the identifiers in the program.
1214typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1215 PCHIdentifierLookupTable;
1216
Douglas Gregorc34897d2009-04-09 22:27:44 +00001217// FIXME: use the diagnostics machinery
1218static bool Error(const char *Str) {
1219 std::fprintf(stderr, "%s\n", Str);
1220 return true;
1221}
1222
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001223/// \brief Check the contents of the predefines buffer against the
1224/// contents of the predefines buffer used to build the PCH file.
1225///
1226/// The contents of the two predefines buffers should be the same. If
1227/// not, then some command-line option changed the preprocessor state
1228/// and we must reject the PCH file.
1229///
1230/// \param PCHPredef The start of the predefines buffer in the PCH
1231/// file.
1232///
1233/// \param PCHPredefLen The length of the predefines buffer in the PCH
1234/// file.
1235///
1236/// \param PCHBufferID The FileID for the PCH predefines buffer.
1237///
1238/// \returns true if there was a mismatch (in which case the PCH file
1239/// should be ignored), or false otherwise.
1240bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1241 unsigned PCHPredefLen,
1242 FileID PCHBufferID) {
1243 const char *Predef = PP.getPredefines().c_str();
1244 unsigned PredefLen = PP.getPredefines().size();
1245
1246 // If the two predefines buffers compare equal, we're done!.
1247 if (PredefLen == PCHPredefLen &&
1248 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1249 return false;
1250
1251 // The predefines buffers are different. Produce a reasonable
1252 // diagnostic showing where they are different.
1253
1254 // The source locations (potentially in the two different predefines
1255 // buffers)
1256 SourceLocation Loc1, Loc2;
1257 SourceManager &SourceMgr = PP.getSourceManager();
1258
1259 // Create a source buffer for our predefines string, so
1260 // that we can build a diagnostic that points into that
1261 // source buffer.
1262 FileID BufferID;
1263 if (Predef && Predef[0]) {
1264 llvm::MemoryBuffer *Buffer
1265 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1266 "<built-in>");
1267 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1268 }
1269
1270 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1271 std::pair<const char *, const char *> Locations
1272 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1273
1274 if (Locations.first != Predef + MinLen) {
1275 // We found the location in the two buffers where there is a
1276 // difference. Form source locations to point there (in both
1277 // buffers).
1278 unsigned Offset = Locations.first - Predef;
1279 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1280 .getFileLocWithOffset(Offset);
1281 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1282 .getFileLocWithOffset(Offset);
1283 } else if (PredefLen > PCHPredefLen) {
1284 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1285 .getFileLocWithOffset(MinLen);
1286 } else {
1287 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1288 .getFileLocWithOffset(MinLen);
1289 }
1290
1291 Diag(Loc1, diag::warn_pch_preprocessor);
1292 if (Loc2.isValid())
1293 Diag(Loc2, diag::note_predef_in_pch);
1294 Diag(diag::note_ignoring_pch) << FileName;
1295 return true;
1296}
1297
Douglas Gregor635f97f2009-04-13 16:31:14 +00001298/// \brief Read the line table in the source manager block.
1299/// \returns true if ther was an error.
1300static bool ParseLineTable(SourceManager &SourceMgr,
1301 llvm::SmallVectorImpl<uint64_t> &Record) {
1302 unsigned Idx = 0;
1303 LineTableInfo &LineTable = SourceMgr.getLineTable();
1304
1305 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001306 std::map<int, int> FileIDs;
1307 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001308 // Extract the file name
1309 unsigned FilenameLen = Record[Idx++];
1310 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1311 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001312 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1313 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001314 }
1315
1316 // Parse the line entries
1317 std::vector<LineEntry> Entries;
1318 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001319 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001320
1321 // Extract the line entries
1322 unsigned NumEntries = Record[Idx++];
1323 Entries.clear();
1324 Entries.reserve(NumEntries);
1325 for (unsigned I = 0; I != NumEntries; ++I) {
1326 unsigned FileOffset = Record[Idx++];
1327 unsigned LineNo = Record[Idx++];
1328 int FilenameID = Record[Idx++];
1329 SrcMgr::CharacteristicKind FileKind
1330 = (SrcMgr::CharacteristicKind)Record[Idx++];
1331 unsigned IncludeOffset = Record[Idx++];
1332 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1333 FileKind, IncludeOffset));
1334 }
1335 LineTable.AddEntry(FID, Entries);
1336 }
1337
1338 return false;
1339}
1340
Douglas Gregorab1cef72009-04-10 03:52:48 +00001341/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001342PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001343 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001344 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1345 Error("Malformed source manager block record");
1346 return Failure;
1347 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001348
1349 SourceManager &SourceMgr = Context.getSourceManager();
1350 RecordData Record;
1351 while (true) {
1352 unsigned Code = Stream.ReadCode();
1353 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001354 if (Stream.ReadBlockEnd()) {
1355 Error("Error at end of Source Manager block");
1356 return Failure;
1357 }
1358
1359 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001360 }
1361
1362 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1363 // No known subblocks, always skip them.
1364 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001365 if (Stream.SkipBlock()) {
1366 Error("Malformed block record");
1367 return Failure;
1368 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001369 continue;
1370 }
1371
1372 if (Code == llvm::bitc::DEFINE_ABBREV) {
1373 Stream.ReadAbbrevRecord();
1374 continue;
1375 }
1376
1377 // Read a record.
1378 const char *BlobStart;
1379 unsigned BlobLen;
1380 Record.clear();
1381 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1382 default: // Default behavior: ignore.
1383 break;
1384
1385 case pch::SM_SLOC_FILE_ENTRY: {
1386 // FIXME: We would really like to delay the creation of this
1387 // FileEntry until it is actually required, e.g., when producing
1388 // a diagnostic with a source location in this file.
1389 const FileEntry *File
1390 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1391 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001392 FileID ID = SourceMgr.createFileID(File,
1393 SourceLocation::getFromRawEncoding(Record[1]),
1394 (CharacteristicKind)Record[2]);
1395 if (Record[3])
1396 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1397 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001398 break;
1399 }
1400
1401 case pch::SM_SLOC_BUFFER_ENTRY: {
1402 const char *Name = BlobStart;
1403 unsigned Code = Stream.ReadCode();
1404 Record.clear();
1405 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1406 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001407 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001408 llvm::MemoryBuffer *Buffer
1409 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1410 BlobStart + BlobLen - 1,
1411 Name);
1412 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1413
1414 if (strcmp(Name, "<built-in>") == 0
1415 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1416 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001417 break;
1418 }
1419
1420 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1421 SourceLocation SpellingLoc
1422 = SourceLocation::getFromRawEncoding(Record[1]);
1423 SourceMgr.createInstantiationLoc(
1424 SpellingLoc,
1425 SourceLocation::getFromRawEncoding(Record[2]),
1426 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001427 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001428 break;
1429 }
1430
Chris Lattnere1be6022009-04-14 23:22:57 +00001431 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001432 if (ParseLineTable(SourceMgr, Record))
1433 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001434 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001435 }
1436 }
1437}
1438
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001439void PCHReader::ReadMacroRecord(uint64_t Offset) {
1440 // Keep track of where we are in the stream, then jump back there
1441 // after reading this macro.
1442 SavedStreamPosition SavedPosition(Stream);
1443
1444 Stream.JumpToBit(Offset);
1445 RecordData Record;
1446 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1447 MacroInfo *Macro = 0;
Steve Naroffcda68f22009-04-24 20:03:17 +00001448
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001449 while (true) {
1450 unsigned Code = Stream.ReadCode();
1451 switch (Code) {
1452 case llvm::bitc::END_BLOCK:
1453 return;
1454
1455 case llvm::bitc::ENTER_SUBBLOCK:
1456 // No known subblocks, always skip them.
1457 Stream.ReadSubBlockID();
1458 if (Stream.SkipBlock()) {
1459 Error("Malformed block record");
1460 return;
1461 }
1462 continue;
1463
1464 case llvm::bitc::DEFINE_ABBREV:
1465 Stream.ReadAbbrevRecord();
1466 continue;
1467 default: break;
1468 }
1469
1470 // Read a record.
1471 Record.clear();
1472 pch::PreprocessorRecordTypes RecType =
1473 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1474 switch (RecType) {
1475 case pch::PP_COUNTER_VALUE:
1476 // Skip this record.
1477 break;
1478
1479 case pch::PP_MACRO_OBJECT_LIKE:
1480 case pch::PP_MACRO_FUNCTION_LIKE: {
1481 // If we already have a macro, that means that we've hit the end
1482 // of the definition of the macro we were looking for. We're
1483 // done.
1484 if (Macro)
1485 return;
1486
1487 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1488 if (II == 0) {
1489 Error("Macro must have a name");
1490 return;
1491 }
1492 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1493 bool isUsed = Record[2];
1494
1495 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1496 MI->setIsUsed(isUsed);
1497
1498 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1499 // Decode function-like macro info.
1500 bool isC99VarArgs = Record[3];
1501 bool isGNUVarArgs = Record[4];
1502 MacroArgs.clear();
1503 unsigned NumArgs = Record[5];
1504 for (unsigned i = 0; i != NumArgs; ++i)
1505 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1506
1507 // Install function-like macro info.
1508 MI->setIsFunctionLike();
1509 if (isC99VarArgs) MI->setIsC99Varargs();
1510 if (isGNUVarArgs) MI->setIsGNUVarargs();
1511 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1512 PP.getPreprocessorAllocator());
1513 }
1514
1515 // Finally, install the macro.
1516 PP.setMacroInfo(II, MI);
1517
1518 // Remember that we saw this macro last so that we add the tokens that
1519 // form its body to it.
1520 Macro = MI;
1521 ++NumMacrosRead;
1522 break;
1523 }
1524
1525 case pch::PP_TOKEN: {
1526 // If we see a TOKEN before a PP_MACRO_*, then the file is
1527 // erroneous, just pretend we didn't see this.
1528 if (Macro == 0) break;
1529
1530 Token Tok;
1531 Tok.startToken();
1532 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1533 Tok.setLength(Record[1]);
1534 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1535 Tok.setIdentifierInfo(II);
1536 Tok.setKind((tok::TokenKind)Record[3]);
1537 Tok.setFlag((Token::TokenFlags)Record[4]);
1538 Macro->AddTokenToBody(Tok);
1539 break;
1540 }
Steve Naroffcda68f22009-04-24 20:03:17 +00001541 case pch::PP_HEADER_FILE_INFO:
1542 break; // Already processed by ReadPreprocessorBlock().
1543 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001544 }
1545}
1546
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001547bool PCHReader::ReadPreprocessorBlock() {
1548 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1549 return Error("Malformed preprocessor block record");
1550
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001551 RecordData Record;
Steve Naroffcda68f22009-04-24 20:03:17 +00001552 unsigned NumHeaderInfos = 0;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001553 while (true) {
1554 unsigned Code = Stream.ReadCode();
1555 switch (Code) {
1556 case llvm::bitc::END_BLOCK:
1557 if (Stream.ReadBlockEnd())
1558 return Error("Error at end of preprocessor block");
1559 return false;
1560
1561 case llvm::bitc::ENTER_SUBBLOCK:
1562 // No known subblocks, always skip them.
1563 Stream.ReadSubBlockID();
1564 if (Stream.SkipBlock())
1565 return Error("Malformed block record");
1566 continue;
1567
1568 case llvm::bitc::DEFINE_ABBREV:
1569 Stream.ReadAbbrevRecord();
1570 continue;
1571 default: break;
1572 }
1573
1574 // Read a record.
1575 Record.clear();
1576 pch::PreprocessorRecordTypes RecType =
1577 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1578 switch (RecType) {
1579 default: // Default behavior: ignore unknown records.
1580 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001581 case pch::PP_COUNTER_VALUE:
1582 if (!Record.empty())
1583 PP.setCounterValue(Record[0]);
1584 break;
1585
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001586 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001587 case pch::PP_MACRO_FUNCTION_LIKE:
1588 case pch::PP_TOKEN:
Steve Naroffcda68f22009-04-24 20:03:17 +00001589 break;
1590 case pch::PP_HEADER_FILE_INFO: {
1591 HeaderFileInfo HFI;
1592 HFI.isImport = Record[0];
1593 HFI.DirInfo = Record[1];
1594 HFI.NumIncludes = Record[2];
1595 HFI.ControllingMacro = DecodeIdentifierInfo(Record[3]);
1596 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, NumHeaderInfos++);
1597 break;
1598 }
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001599 }
1600 }
1601}
1602
Steve Naroff9e84d782009-04-23 10:39:46 +00001603bool PCHReader::ReadSelectorBlock() {
1604 if (Stream.EnterSubBlock(pch::SELECTOR_BLOCK_ID))
1605 return Error("Malformed selector block record");
1606
1607 RecordData Record;
1608 while (true) {
1609 unsigned Code = Stream.ReadCode();
1610 switch (Code) {
1611 case llvm::bitc::END_BLOCK:
1612 if (Stream.ReadBlockEnd())
1613 return Error("Error at end of preprocessor block");
1614 return false;
1615
1616 case llvm::bitc::ENTER_SUBBLOCK:
1617 // No known subblocks, always skip them.
1618 Stream.ReadSubBlockID();
1619 if (Stream.SkipBlock())
1620 return Error("Malformed block record");
1621 continue;
1622
1623 case llvm::bitc::DEFINE_ABBREV:
1624 Stream.ReadAbbrevRecord();
1625 continue;
1626 default: break;
1627 }
1628
1629 // Read a record.
1630 Record.clear();
1631 pch::PCHRecordTypes RecType =
1632 (pch::PCHRecordTypes)Stream.ReadRecord(Code, Record);
1633 switch (RecType) {
1634 default: // Default behavior: ignore unknown records.
1635 break;
1636 case pch::SELECTOR_TABLE:
1637 unsigned Idx = 1; // Record[0] == pch::SELECTOR_TABLE.
1638 unsigned NumSels = Record[Idx++];
1639
1640 llvm::SmallVector<IdentifierInfo *, 8> KeyIdents;
1641 for (unsigned SelIdx = 0; SelIdx < NumSels; SelIdx++) {
1642 unsigned NumArgs = Record[Idx++];
1643 KeyIdents.clear();
1644 if (NumArgs <= 1) {
1645 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1646 assert(II && "DecodeIdentifierInfo returned 0");
1647 KeyIdents.push_back(II);
1648 } else {
1649 for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) {
1650 IdentifierInfo *II = DecodeIdentifierInfo(Record[Idx++]);
1651 assert(II && "DecodeIdentifierInfo returned 0");
1652 KeyIdents.push_back(II);
1653 }
1654 }
1655 Selector Sel = PP.getSelectorTable().getSelector(NumArgs,&KeyIdents[0]);
1656 SelectorData.push_back(Sel);
1657 }
1658 }
1659 }
1660 return false;
1661}
1662
Douglas Gregorc713da92009-04-21 22:25:48 +00001663PCHReader::PCHReadResult
Steve Naroff9e84d782009-04-23 10:39:46 +00001664PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset,
1665 uint64_t &SelectorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001666 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1667 Error("Malformed block record");
1668 return Failure;
1669 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001670
1671 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001672 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001673 while (!Stream.AtEndOfStream()) {
1674 unsigned Code = Stream.ReadCode();
1675 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001676 if (Stream.ReadBlockEnd()) {
1677 Error("Error at end of module block");
1678 return Failure;
1679 }
Chris Lattner29241862009-04-11 21:15:38 +00001680
Douglas Gregor179cfb12009-04-10 20:39:37 +00001681 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001682 }
1683
1684 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1685 switch (Stream.ReadSubBlockID()) {
1686 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1687 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1688 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001689 if (Stream.SkipBlock()) {
1690 Error("Malformed block record");
1691 return Failure;
1692 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001693 break;
1694
Chris Lattner29241862009-04-11 21:15:38 +00001695 case pch::PREPROCESSOR_BLOCK_ID:
1696 // Skip the preprocessor block for now, but remember where it is. We
1697 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001698 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001699 Error("Multiple preprocessor blocks found.");
1700 return Failure;
1701 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001702 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001703 if (Stream.SkipBlock()) {
1704 Error("Malformed block record");
1705 return Failure;
1706 }
1707 break;
Steve Naroff9e84d782009-04-23 10:39:46 +00001708
1709 case pch::SELECTOR_BLOCK_ID:
1710 // Skip the selector block for now, but remember where it is. We
1711 // want to read it in after the identifier table.
1712 if (SelectorBlockOffset) {
1713 Error("Multiple selector blocks found.");
1714 return Failure;
1715 }
1716 SelectorBlockOffset = Stream.GetCurrentBitNo();
1717 if (Stream.SkipBlock()) {
1718 Error("Malformed block record");
1719 return Failure;
1720 }
1721 break;
Chris Lattner29241862009-04-11 21:15:38 +00001722
Douglas Gregorab1cef72009-04-10 03:52:48 +00001723 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001724 switch (ReadSourceManagerBlock()) {
1725 case Success:
1726 break;
1727
1728 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001729 Error("Malformed source manager block");
1730 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001731
1732 case IgnorePCH:
1733 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001734 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001735 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001736 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001737 continue;
1738 }
1739
1740 if (Code == llvm::bitc::DEFINE_ABBREV) {
1741 Stream.ReadAbbrevRecord();
1742 continue;
1743 }
1744
1745 // Read and process a record.
1746 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001747 const char *BlobStart = 0;
1748 unsigned BlobLen = 0;
1749 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1750 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001751 default: // Default behavior: ignore.
1752 break;
1753
1754 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001755 if (!TypeOffsets.empty()) {
1756 Error("Duplicate TYPE_OFFSET record in PCH file");
1757 return Failure;
1758 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001759 TypeOffsets.swap(Record);
1760 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1761 break;
1762
1763 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001764 if (!DeclOffsets.empty()) {
1765 Error("Duplicate DECL_OFFSET record in PCH file");
1766 return Failure;
1767 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001768 DeclOffsets.swap(Record);
1769 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1770 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001771
1772 case pch::LANGUAGE_OPTIONS:
1773 if (ParseLanguageOptions(Record))
1774 return IgnorePCH;
1775 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001776
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001777 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001778 std::string TargetTriple(BlobStart, BlobLen);
1779 if (TargetTriple != Context.Target.getTargetTriple()) {
1780 Diag(diag::warn_pch_target_triple)
1781 << TargetTriple << Context.Target.getTargetTriple();
1782 Diag(diag::note_ignoring_pch) << FileName;
1783 return IgnorePCH;
1784 }
1785 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001786 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001787
1788 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001789 IdentifierTableData = BlobStart;
1790 IdentifierLookupTable
1791 = PCHIdentifierLookupTable::Create(
1792 (const unsigned char *)IdentifierTableData + Record[0],
1793 (const unsigned char *)IdentifierTableData,
1794 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001795 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001796 break;
1797
1798 case pch::IDENTIFIER_OFFSET:
1799 if (!IdentifierData.empty()) {
1800 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1801 return Failure;
1802 }
1803 IdentifierData.swap(Record);
1804#ifndef NDEBUG
1805 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1806 if ((IdentifierData[I] & 0x01) == 0) {
1807 Error("Malformed identifier table in the precompiled header");
1808 return Failure;
1809 }
1810 }
1811#endif
1812 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001813
1814 case pch::EXTERNAL_DEFINITIONS:
1815 if (!ExternalDefinitions.empty()) {
1816 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1817 return Failure;
1818 }
1819 ExternalDefinitions.swap(Record);
1820 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001821
Douglas Gregore01ad442009-04-18 05:55:16 +00001822 case pch::SPECIAL_TYPES:
1823 SpecialTypes.swap(Record);
1824 break;
1825
Douglas Gregor456e0952009-04-17 22:13:46 +00001826 case pch::STATISTICS:
1827 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001828 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001829 TotalLexicalDeclContexts = Record[2];
1830 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001831 break;
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001832 case pch::TENTATIVE_DEFINITIONS:
1833 if (!TentativeDefinitions.empty()) {
1834 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1835 return Failure;
1836 }
1837 TentativeDefinitions.swap(Record);
1838 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001839
1840 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1841 if (!LocallyScopedExternalDecls.empty()) {
1842 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1843 return Failure;
1844 }
1845 LocallyScopedExternalDecls.swap(Record);
1846 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001847 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001848 }
Douglas Gregor179cfb12009-04-10 20:39:37 +00001849 Error("Premature end of bitstream");
1850 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001851}
1852
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001853PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001854 // Set the PCH file name.
1855 this->FileName = FileName;
1856
Douglas Gregorc34897d2009-04-09 22:27:44 +00001857 // Open the PCH file.
1858 std::string ErrStr;
1859 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001860 if (!Buffer) {
1861 Error(ErrStr.c_str());
1862 return IgnorePCH;
1863 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001864
1865 // Initialize the stream
1866 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1867 (const unsigned char *)Buffer->getBufferEnd());
1868
1869 // Sniff for the signature.
1870 if (Stream.Read(8) != 'C' ||
1871 Stream.Read(8) != 'P' ||
1872 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001873 Stream.Read(8) != 'H') {
1874 Error("Not a PCH file");
1875 return IgnorePCH;
1876 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001877
1878 // We expect a number of well-defined blocks, though we don't necessarily
1879 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001880 uint64_t PreprocessorBlockOffset = 0;
Steve Naroff9e84d782009-04-23 10:39:46 +00001881 uint64_t SelectorBlockOffset = 0;
1882
Douglas Gregorc34897d2009-04-09 22:27:44 +00001883 while (!Stream.AtEndOfStream()) {
1884 unsigned Code = Stream.ReadCode();
1885
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001886 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1887 Error("Invalid record at top-level");
1888 return Failure;
1889 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001890
1891 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001892
Douglas Gregorc34897d2009-04-09 22:27:44 +00001893 // We only know the PCH subblock ID.
1894 switch (BlockID) {
1895 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001896 if (Stream.ReadBlockInfoBlock()) {
1897 Error("Malformed BlockInfoBlock");
1898 return Failure;
1899 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001900 break;
1901 case pch::PCH_BLOCK_ID:
Steve Naroff9e84d782009-04-23 10:39:46 +00001902 switch (ReadPCHBlock(PreprocessorBlockOffset, SelectorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001903 case Success:
1904 break;
1905
1906 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001907 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001908
1909 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001910 // FIXME: We could consider reading through to the end of this
1911 // PCH block, skipping subblocks, to see if there are other
1912 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001913 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001914 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001915 break;
1916 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001917 if (Stream.SkipBlock()) {
1918 Error("Malformed block record");
1919 return Failure;
1920 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001921 break;
1922 }
1923 }
1924
1925 // Load the translation unit declaration
1926 ReadDeclRecord(DeclOffsets[0], 0);
1927
Douglas Gregorc713da92009-04-21 22:25:48 +00001928 // Initialization of builtins and library builtins occurs before the
1929 // PCH file is read, so there may be some identifiers that were
1930 // loaded into the IdentifierTable before we intercepted the
1931 // creation of identifiers. Iterate through the list of known
1932 // identifiers and determine whether we have to establish
1933 // preprocessor definitions or top-level identifier declaration
1934 // chains for those identifiers.
1935 //
1936 // We copy the IdentifierInfo pointers to a small vector first,
1937 // since de-serializing declarations or macro definitions can add
1938 // new entries into the identifier table, invalidating the
1939 // iterators.
1940 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1941 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1942 IdEnd = PP.getIdentifierTable().end();
1943 Id != IdEnd; ++Id)
1944 Identifiers.push_back(Id->second);
1945 PCHIdentifierLookupTable *IdTable
1946 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1947 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1948 IdentifierInfo *II = Identifiers[I];
1949 // Look in the on-disk hash table for an entry for
1950 PCHIdentifierLookupTrait Info(*this, II);
1951 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1952 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1953 if (Pos == IdTable->end())
1954 continue;
1955
1956 // Dereferencing the iterator has the effect of populating the
1957 // IdentifierInfo node with the various declarations it needs.
1958 (void)*Pos;
1959 }
1960
Douglas Gregore01ad442009-04-18 05:55:16 +00001961 // Load the special types.
1962 Context.setBuiltinVaListType(
1963 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00001964 if (unsigned Id = SpecialTypes[pch::SPECIAL_TYPE_OBJC_ID])
1965 Context.setObjCIdType(GetType(Id));
1966 if (unsigned Sel = SpecialTypes[pch::SPECIAL_TYPE_OBJC_SELECTOR])
1967 Context.setObjCSelType(GetType(Sel));
1968 if (unsigned Proto = SpecialTypes[pch::SPECIAL_TYPE_OBJC_PROTOCOL])
1969 Context.setObjCProtoType(GetType(Proto));
1970 if (unsigned Class = SpecialTypes[pch::SPECIAL_TYPE_OBJC_CLASS])
1971 Context.setObjCClassType(GetType(Class));
1972 if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_CF_CONSTANT_STRING])
1973 Context.setCFConstantStringType(GetType(String));
1974 if (unsigned FastEnum
1975 = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
1976 Context.setObjCFastEnumerationStateType(GetType(FastEnum));
Douglas Gregorc713da92009-04-21 22:25:48 +00001977 // If we saw the preprocessor block, read it now.
1978 if (PreprocessorBlockOffset) {
1979 SavedStreamPosition SavedPos(Stream);
1980 Stream.JumpToBit(PreprocessorBlockOffset);
1981 if (ReadPreprocessorBlock()) {
1982 Error("Malformed preprocessor block");
1983 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001984 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001985 }
Steve Naroff9e84d782009-04-23 10:39:46 +00001986 if (SelectorBlockOffset) {
1987 SavedStreamPosition SavedPos(Stream);
1988 Stream.JumpToBit(SelectorBlockOffset);
1989 if (ReadSelectorBlock()) {
1990 Error("Malformed preprocessor block");
1991 return Failure;
1992 }
1993 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001994
Douglas Gregorc713da92009-04-21 22:25:48 +00001995 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001996}
1997
Douglas Gregor179cfb12009-04-10 20:39:37 +00001998/// \brief Parse the record that corresponds to a LangOptions data
1999/// structure.
2000///
2001/// This routine compares the language options used to generate the
2002/// PCH file against the language options set for the current
2003/// compilation. For each option, we classify differences between the
2004/// two compiler states as either "benign" or "important". Benign
2005/// differences don't matter, and we accept them without complaint
2006/// (and without modifying the language options). Differences between
2007/// the states for important options cause the PCH file to be
2008/// unusable, so we emit a warning and return true to indicate that
2009/// there was an error.
2010///
2011/// \returns true if the PCH file is unacceptable, false otherwise.
2012bool PCHReader::ParseLanguageOptions(
2013 const llvm::SmallVectorImpl<uint64_t> &Record) {
2014 const LangOptions &LangOpts = Context.getLangOptions();
2015#define PARSE_LANGOPT_BENIGN(Option) ++Idx
2016#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
2017 if (Record[Idx] != LangOpts.Option) { \
2018 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
2019 Diag(diag::note_ignoring_pch) << FileName; \
2020 return true; \
2021 } \
2022 ++Idx
2023
2024 unsigned Idx = 0;
2025 PARSE_LANGOPT_BENIGN(Trigraphs);
2026 PARSE_LANGOPT_BENIGN(BCPLComment);
2027 PARSE_LANGOPT_BENIGN(DollarIdents);
2028 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
2029 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
2030 PARSE_LANGOPT_BENIGN(ImplicitInt);
2031 PARSE_LANGOPT_BENIGN(Digraphs);
2032 PARSE_LANGOPT_BENIGN(HexFloats);
2033 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
2034 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
2035 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
2036 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
2037 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
2038 PARSE_LANGOPT_BENIGN(CXXOperatorName);
2039 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
2040 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
2041 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
2042 PARSE_LANGOPT_BENIGN(PascalStrings);
2043 PARSE_LANGOPT_BENIGN(Boolean);
2044 PARSE_LANGOPT_BENIGN(WritableStrings);
2045 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
2046 diag::warn_pch_lax_vector_conversions);
2047 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
2048 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
2049 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
2050 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
2051 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
2052 diag::warn_pch_thread_safe_statics);
2053 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
2054 PARSE_LANGOPT_BENIGN(EmitAllDecls);
2055 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
2056 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
2057 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
2058 diag::warn_pch_heinous_extensions);
2059 // FIXME: Most of the options below are benign if the macro wasn't
2060 // used. Unfortunately, this means that a PCH compiled without
2061 // optimization can't be used with optimization turned on, even
2062 // though the only thing that changes is whether __OPTIMIZE__ was
2063 // defined... but if __OPTIMIZE__ never showed up in the header, it
2064 // doesn't matter. We could consider making this some special kind
2065 // of check.
2066 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
2067 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
2068 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
2069 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
2070 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
2071 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
2072 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
2073 Diag(diag::warn_pch_gc_mode)
2074 << (unsigned)Record[Idx] << LangOpts.getGCMode();
2075 Diag(diag::note_ignoring_pch) << FileName;
2076 return true;
2077 }
2078 ++Idx;
2079 PARSE_LANGOPT_BENIGN(getVisibilityMode());
2080 PARSE_LANGOPT_BENIGN(InstantiationDepth);
2081#undef PARSE_LANGOPT_IRRELEVANT
2082#undef PARSE_LANGOPT_BENIGN
2083
2084 return false;
2085}
2086
Douglas Gregorc34897d2009-04-09 22:27:44 +00002087/// \brief Read and return the type at the given offset.
2088///
2089/// This routine actually reads the record corresponding to the type
2090/// at the given offset in the bitstream. It is a helper routine for
2091/// GetType, which deals with reading type IDs.
2092QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002093 // Keep track of where we are in the stream, then jump back there
2094 // after reading this type.
2095 SavedStreamPosition SavedPosition(Stream);
2096
Douglas Gregorc34897d2009-04-09 22:27:44 +00002097 Stream.JumpToBit(Offset);
2098 RecordData Record;
2099 unsigned Code = Stream.ReadCode();
2100 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00002101 case pch::TYPE_EXT_QUAL: {
2102 assert(Record.size() == 3 &&
2103 "Incorrect encoding of extended qualifier type");
2104 QualType Base = GetType(Record[0]);
2105 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
2106 unsigned AddressSpace = Record[2];
2107
2108 QualType T = Base;
2109 if (GCAttr != QualType::GCNone)
2110 T = Context.getObjCGCQualType(T, GCAttr);
2111 if (AddressSpace)
2112 T = Context.getAddrSpaceQualType(T, AddressSpace);
2113 return T;
2114 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002115
Douglas Gregorc34897d2009-04-09 22:27:44 +00002116 case pch::TYPE_FIXED_WIDTH_INT: {
2117 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2118 return Context.getFixedWidthIntType(Record[0], Record[1]);
2119 }
2120
2121 case pch::TYPE_COMPLEX: {
2122 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2123 QualType ElemType = GetType(Record[0]);
2124 return Context.getComplexType(ElemType);
2125 }
2126
2127 case pch::TYPE_POINTER: {
2128 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2129 QualType PointeeType = GetType(Record[0]);
2130 return Context.getPointerType(PointeeType);
2131 }
2132
2133 case pch::TYPE_BLOCK_POINTER: {
2134 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2135 QualType PointeeType = GetType(Record[0]);
2136 return Context.getBlockPointerType(PointeeType);
2137 }
2138
2139 case pch::TYPE_LVALUE_REFERENCE: {
2140 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2141 QualType PointeeType = GetType(Record[0]);
2142 return Context.getLValueReferenceType(PointeeType);
2143 }
2144
2145 case pch::TYPE_RVALUE_REFERENCE: {
2146 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2147 QualType PointeeType = GetType(Record[0]);
2148 return Context.getRValueReferenceType(PointeeType);
2149 }
2150
2151 case pch::TYPE_MEMBER_POINTER: {
2152 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2153 QualType PointeeType = GetType(Record[0]);
2154 QualType ClassType = GetType(Record[1]);
2155 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2156 }
2157
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002158 case pch::TYPE_CONSTANT_ARRAY: {
2159 QualType ElementType = GetType(Record[0]);
2160 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2161 unsigned IndexTypeQuals = Record[2];
2162 unsigned Idx = 3;
2163 llvm::APInt Size = ReadAPInt(Record, Idx);
2164 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2165 }
2166
2167 case pch::TYPE_INCOMPLETE_ARRAY: {
2168 QualType ElementType = GetType(Record[0]);
2169 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2170 unsigned IndexTypeQuals = Record[2];
2171 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2172 }
2173
2174 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002175 QualType ElementType = GetType(Record[0]);
2176 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2177 unsigned IndexTypeQuals = Record[2];
2178 return Context.getVariableArrayType(ElementType, ReadExpr(),
2179 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002180 }
2181
2182 case pch::TYPE_VECTOR: {
2183 if (Record.size() != 2) {
2184 Error("Incorrect encoding of vector type in PCH file");
2185 return QualType();
2186 }
2187
2188 QualType ElementType = GetType(Record[0]);
2189 unsigned NumElements = Record[1];
2190 return Context.getVectorType(ElementType, NumElements);
2191 }
2192
2193 case pch::TYPE_EXT_VECTOR: {
2194 if (Record.size() != 2) {
2195 Error("Incorrect encoding of extended vector type in PCH file");
2196 return QualType();
2197 }
2198
2199 QualType ElementType = GetType(Record[0]);
2200 unsigned NumElements = Record[1];
2201 return Context.getExtVectorType(ElementType, NumElements);
2202 }
2203
2204 case pch::TYPE_FUNCTION_NO_PROTO: {
2205 if (Record.size() != 1) {
2206 Error("Incorrect encoding of no-proto function type");
2207 return QualType();
2208 }
2209 QualType ResultType = GetType(Record[0]);
2210 return Context.getFunctionNoProtoType(ResultType);
2211 }
2212
2213 case pch::TYPE_FUNCTION_PROTO: {
2214 QualType ResultType = GetType(Record[0]);
2215 unsigned Idx = 1;
2216 unsigned NumParams = Record[Idx++];
2217 llvm::SmallVector<QualType, 16> ParamTypes;
2218 for (unsigned I = 0; I != NumParams; ++I)
2219 ParamTypes.push_back(GetType(Record[Idx++]));
2220 bool isVariadic = Record[Idx++];
2221 unsigned Quals = Record[Idx++];
2222 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2223 isVariadic, Quals);
2224 }
2225
2226 case pch::TYPE_TYPEDEF:
2227 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2228 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2229
2230 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002231 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002232
2233 case pch::TYPE_TYPEOF: {
2234 if (Record.size() != 1) {
2235 Error("Incorrect encoding of typeof(type) in PCH file");
2236 return QualType();
2237 }
2238 QualType UnderlyingType = GetType(Record[0]);
2239 return Context.getTypeOfType(UnderlyingType);
2240 }
2241
2242 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002243 assert(Record.size() == 1 && "Incorrect encoding of record type");
2244 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002245
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002246 case pch::TYPE_ENUM:
2247 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2248 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2249
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002250 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002251 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2252 return Context.getObjCInterfaceType(
2253 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002254
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002255 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2256 unsigned Idx = 0;
2257 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2258 unsigned NumProtos = Record[Idx++];
2259 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2260 for (unsigned I = 0; I != NumProtos; ++I)
2261 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2262 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2263 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002264
Chris Lattner9b9f2352009-04-22 06:40:03 +00002265 case pch::TYPE_OBJC_QUALIFIED_ID: {
2266 unsigned Idx = 0;
2267 unsigned NumProtos = Record[Idx++];
2268 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2269 for (unsigned I = 0; I != NumProtos; ++I)
2270 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2271 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2272 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002273 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002274 // Suppress a GCC warning
2275 return QualType();
2276}
2277
2278/// \brief Note that we have loaded the declaration with the given
2279/// Index.
2280///
2281/// This routine notes that this declaration has already been loaded,
2282/// so that future GetDecl calls will return this declaration rather
2283/// than trying to load a new declaration.
2284inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2285 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2286 DeclAlreadyLoaded[Index] = true;
2287 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2288}
2289
2290/// \brief Read the declaration at the given offset from the PCH file.
2291Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002292 // Keep track of where we are in the stream, then jump back there
2293 // after reading this declaration.
2294 SavedStreamPosition SavedPosition(Stream);
2295
Douglas Gregorc34897d2009-04-09 22:27:44 +00002296 Decl *D = 0;
2297 Stream.JumpToBit(Offset);
2298 RecordData Record;
2299 unsigned Code = Stream.ReadCode();
2300 unsigned Idx = 0;
2301 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002302
Douglas Gregorc34897d2009-04-09 22:27:44 +00002303 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002304 case pch::DECL_ATTR:
2305 case pch::DECL_CONTEXT_LEXICAL:
2306 case pch::DECL_CONTEXT_VISIBLE:
2307 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2308 break;
2309
Douglas Gregorc34897d2009-04-09 22:27:44 +00002310 case pch::DECL_TRANSLATION_UNIT:
2311 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002312 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002313 break;
2314
2315 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002316 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002317 break;
2318 }
2319
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002320 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002321 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002322 break;
2323 }
2324
Douglas Gregor982365e2009-04-13 21:20:57 +00002325 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002326 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2327 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002328 break;
2329 }
2330
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002331 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002332 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2333 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002334 break;
2335 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002336
2337 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002338 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2339 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002340 break;
2341 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002342
Steve Naroff79ea0e02009-04-20 15:06:07 +00002343 case pch::DECL_OBJC_METHOD: {
2344 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2345 Selector(), QualType(), 0);
2346 break;
2347 }
2348
Steve Naroff97b53bd2009-04-21 15:12:33 +00002349 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002350 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2351 break;
2352 }
2353
Steve Naroff97b53bd2009-04-21 15:12:33 +00002354 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002355 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2356 ObjCIvarDecl::None);
2357 break;
2358 }
2359
Steve Naroff97b53bd2009-04-21 15:12:33 +00002360 case pch::DECL_OBJC_PROTOCOL: {
2361 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2362 break;
2363 }
2364
2365 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2366 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2367 QualType(), 0);
2368 break;
2369 }
2370
2371 case pch::DECL_OBJC_CLASS: {
2372 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2373 break;
2374 }
2375
2376 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2377 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2378 break;
2379 }
2380
2381 case pch::DECL_OBJC_CATEGORY: {
2382 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2383 break;
2384 }
2385
2386 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002387 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002388 break;
2389 }
2390
2391 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002392 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002393 break;
2394 }
2395
2396 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002397 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002398 break;
2399 }
2400
2401 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002402 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002403 break;
2404 }
2405
2406 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002407 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2408 SourceLocation(), 0,
2409 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002410 break;
2411 }
2412
Douglas Gregor982365e2009-04-13 21:20:57 +00002413 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002414 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2415 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002416 break;
2417 }
2418
Douglas Gregorc34897d2009-04-09 22:27:44 +00002419 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002420 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2421 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002422 break;
2423 }
2424
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002425 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002426 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2427 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002428 break;
2429 }
2430
2431 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002432 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002433 QualType(), QualType(), VarDecl::None,
2434 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002435 break;
2436 }
2437
Douglas Gregor2a491792009-04-13 22:49:25 +00002438 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002439 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002440 break;
2441 }
2442
2443 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002444 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002445 break;
2446 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002447 }
2448
Douglas Gregorc713da92009-04-21 22:25:48 +00002449 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002450 if (D) {
2451 LoadedDecl(Index, D);
2452 Reader.Visit(D);
2453 }
2454
Douglas Gregorc34897d2009-04-09 22:27:44 +00002455 // If this declaration is also a declaration context, get the
2456 // offsets for its tables of lexical and visible declarations.
2457 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2458 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2459 if (Offsets.first || Offsets.second) {
2460 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2461 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2462 DeclContextOffsets[DC] = Offsets;
2463 }
2464 }
2465 assert(Idx == Record.size());
2466
Douglas Gregor405b6432009-04-22 19:09:20 +00002467 if (Consumer) {
2468 // If we have deserialized a declaration that has a definition the
2469 // AST consumer might need to know about, notify the consumer
2470 // about that definition now.
2471 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2472 if (Var->isFileVarDecl() && Var->getInit()) {
2473 DeclGroupRef DG(Var);
2474 Consumer->HandleTopLevelDecl(DG);
2475 }
2476 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2477 if (Func->isThisDeclarationADefinition()) {
2478 DeclGroupRef DG(Func);
2479 Consumer->HandleTopLevelDecl(DG);
2480 }
2481 }
2482 }
2483
Douglas Gregorc34897d2009-04-09 22:27:44 +00002484 return D;
2485}
2486
Douglas Gregorac8f2802009-04-10 17:25:41 +00002487QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002488 unsigned Quals = ID & 0x07;
2489 unsigned Index = ID >> 3;
2490
2491 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2492 QualType T;
2493 switch ((pch::PredefinedTypeIDs)Index) {
2494 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2495 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2496 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2497
2498 case pch::PREDEF_TYPE_CHAR_U_ID:
2499 case pch::PREDEF_TYPE_CHAR_S_ID:
2500 // FIXME: Check that the signedness of CharTy is correct!
2501 T = Context.CharTy;
2502 break;
2503
2504 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2505 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2506 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2507 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2508 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2509 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2510 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2511 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2512 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2513 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2514 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2515 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2516 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2517 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2518 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2519 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2520 }
2521
2522 assert(!T.isNull() && "Unknown predefined type");
2523 return T.getQualifiedType(Quals);
2524 }
2525
2526 Index -= pch::NUM_PREDEF_TYPE_IDS;
2527 if (!TypeAlreadyLoaded[Index]) {
2528 // Load the type from the PCH file.
2529 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2530 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2531 TypeAlreadyLoaded[Index] = true;
2532 }
2533
2534 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2535}
2536
Douglas Gregorac8f2802009-04-10 17:25:41 +00002537Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002538 if (ID == 0)
2539 return 0;
2540
2541 unsigned Index = ID - 1;
2542 if (DeclAlreadyLoaded[Index])
2543 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2544
2545 // Load the declaration from the PCH file.
2546 return ReadDeclRecord(DeclOffsets[Index], Index);
2547}
2548
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002549Stmt *PCHReader::GetStmt(uint64_t Offset) {
2550 // Keep track of where we are in the stream, then jump back there
2551 // after reading this declaration.
2552 SavedStreamPosition SavedPosition(Stream);
2553
2554 Stream.JumpToBit(Offset);
2555 return ReadStmt();
2556}
2557
Douglas Gregorc34897d2009-04-09 22:27:44 +00002558bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002559 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002560 assert(DC->hasExternalLexicalStorage() &&
2561 "DeclContext has no lexical decls in storage");
2562 uint64_t Offset = DeclContextOffsets[DC].first;
2563 assert(Offset && "DeclContext has no lexical decls in storage");
2564
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002565 // Keep track of where we are in the stream, then jump back there
2566 // after reading this context.
2567 SavedStreamPosition SavedPosition(Stream);
2568
Douglas Gregorc34897d2009-04-09 22:27:44 +00002569 // Load the record containing all of the declarations lexically in
2570 // this context.
2571 Stream.JumpToBit(Offset);
2572 RecordData Record;
2573 unsigned Code = Stream.ReadCode();
2574 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002575 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002576 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2577
2578 // Load all of the declaration IDs
2579 Decls.clear();
2580 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002581 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002582 return false;
2583}
2584
2585bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2586 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2587 assert(DC->hasExternalVisibleStorage() &&
2588 "DeclContext has no visible decls in storage");
2589 uint64_t Offset = DeclContextOffsets[DC].second;
2590 assert(Offset && "DeclContext has no visible decls in storage");
2591
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002592 // Keep track of where we are in the stream, then jump back there
2593 // after reading this context.
2594 SavedStreamPosition SavedPosition(Stream);
2595
Douglas Gregorc34897d2009-04-09 22:27:44 +00002596 // Load the record containing all of the declarations visible in
2597 // this context.
2598 Stream.JumpToBit(Offset);
2599 RecordData Record;
2600 unsigned Code = Stream.ReadCode();
2601 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002602 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002603 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2604 if (Record.size() == 0)
2605 return false;
2606
2607 Decls.clear();
2608
2609 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002610 while (Idx < Record.size()) {
2611 Decls.push_back(VisibleDeclaration());
2612 Decls.back().Name = ReadDeclarationName(Record, Idx);
2613
Douglas Gregorc34897d2009-04-09 22:27:44 +00002614 unsigned Size = Record[Idx++];
2615 llvm::SmallVector<unsigned, 4> & LoadedDecls
2616 = Decls.back().Declarations;
2617 LoadedDecls.reserve(Size);
2618 for (unsigned I = 0; I < Size; ++I)
2619 LoadedDecls.push_back(Record[Idx++]);
2620 }
2621
Douglas Gregoraf136d92009-04-22 22:34:57 +00002622 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002623 return false;
2624}
2625
Douglas Gregor631f6c62009-04-14 00:24:19 +00002626void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002627 this->Consumer = Consumer;
2628
Douglas Gregor631f6c62009-04-14 00:24:19 +00002629 if (!Consumer)
2630 return;
2631
2632 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2633 Decl *D = GetDecl(ExternalDefinitions[I]);
2634 DeclGroupRef DG(D);
2635 Consumer->HandleTopLevelDecl(DG);
2636 }
2637}
2638
Douglas Gregorc34897d2009-04-09 22:27:44 +00002639void PCHReader::PrintStats() {
2640 std::fprintf(stderr, "*** PCH Statistics:\n");
2641
2642 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2643 TypeAlreadyLoaded.end(),
2644 true);
2645 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2646 DeclAlreadyLoaded.end(),
2647 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002648 unsigned NumIdentifiersLoaded = 0;
2649 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2650 if ((IdentifierData[I] & 0x01) == 0)
2651 ++NumIdentifiersLoaded;
2652 }
2653
Douglas Gregorc34897d2009-04-09 22:27:44 +00002654 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2655 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002656 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002657 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2658 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002659 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2660 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2661 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2662 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002663 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2664 NumStatementsRead, TotalNumStatements,
2665 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002666 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2667 NumMacrosRead, TotalNumMacros,
2668 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002669 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2670 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2671 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2672 * 100));
2673 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2674 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2675 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2676 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002677 std::fprintf(stderr, "\n");
2678}
2679
Douglas Gregorc713da92009-04-21 22:25:48 +00002680void PCHReader::InitializeSema(Sema &S) {
2681 SemaObj = &S;
2682
Douglas Gregor2554cf22009-04-22 21:15:06 +00002683 // Makes sure any declarations that were deserialized "too early"
2684 // still get added to the identifier's declaration chains.
2685 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2686 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2687 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002688 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002689 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002690
2691 // If there were any tentative definitions, deserialize them and add
2692 // them to Sema's table of tentative definitions.
2693 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2694 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2695 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2696 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002697
2698 // If there were any locally-scoped external declarations,
2699 // deserialize them and add them to Sema's table of locally-scoped
2700 // external declarations.
2701 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2702 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2703 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2704 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002705}
2706
2707IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2708 // Try to find this name within our on-disk hash table
2709 PCHIdentifierLookupTable *IdTable
2710 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2711 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2712 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2713 if (Pos == IdTable->end())
2714 return 0;
2715
2716 // Dereferencing the iterator has the effect of building the
2717 // IdentifierInfo node and populating it with the various
2718 // declarations it needs.
2719 return *Pos;
2720}
2721
2722void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2723 assert(ID && "Non-zero identifier ID required");
2724 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2725}
2726
Chris Lattner29241862009-04-11 21:15:38 +00002727IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002728 if (ID == 0)
2729 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002730
Douglas Gregorc713da92009-04-21 22:25:48 +00002731 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002732 Error("No identifier table in PCH file");
2733 return 0;
2734 }
Chris Lattner29241862009-04-11 21:15:38 +00002735
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002736 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002737 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002738 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002739 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002740 }
Chris Lattner29241862009-04-11 21:15:38 +00002741
2742 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002743}
2744
Steve Naroff9e84d782009-04-23 10:39:46 +00002745Selector PCHReader::DecodeSelector(unsigned ID) {
2746 if (ID == 0)
2747 return Selector();
2748
2749 if (SelectorData.empty()) {
2750 Error("No selector table in PCH file");
2751 return Selector();
2752 }
2753
2754 if (ID > SelectorData.size()) {
2755 Error("Selector ID out of range");
2756 return Selector();
2757 }
2758 return SelectorData[ID-1];
2759}
2760
Douglas Gregorc34897d2009-04-09 22:27:44 +00002761DeclarationName
2762PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2763 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2764 switch (Kind) {
2765 case DeclarationName::Identifier:
2766 return DeclarationName(GetIdentifierInfo(Record, Idx));
2767
2768 case DeclarationName::ObjCZeroArgSelector:
2769 case DeclarationName::ObjCOneArgSelector:
2770 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff104956f2009-04-23 15:15:40 +00002771 return DeclarationName(GetSelector(Record, Idx));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002772
2773 case DeclarationName::CXXConstructorName:
2774 return Context.DeclarationNames.getCXXConstructorName(
2775 GetType(Record[Idx++]));
2776
2777 case DeclarationName::CXXDestructorName:
2778 return Context.DeclarationNames.getCXXDestructorName(
2779 GetType(Record[Idx++]));
2780
2781 case DeclarationName::CXXConversionFunctionName:
2782 return Context.DeclarationNames.getCXXConversionFunctionName(
2783 GetType(Record[Idx++]));
2784
2785 case DeclarationName::CXXOperatorName:
2786 return Context.DeclarationNames.getCXXOperatorName(
2787 (OverloadedOperatorKind)Record[Idx++]);
2788
2789 case DeclarationName::CXXUsingDirective:
2790 return DeclarationName::getUsingDirectiveName();
2791 }
2792
2793 // Required to silence GCC warning
2794 return DeclarationName();
2795}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002796
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002797/// \brief Read an integral value
2798llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2799 unsigned BitWidth = Record[Idx++];
2800 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2801 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2802 Idx += NumWords;
2803 return Result;
2804}
2805
2806/// \brief Read a signed integral value
2807llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2808 bool isUnsigned = Record[Idx++];
2809 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2810}
2811
Douglas Gregore2f37202009-04-14 21:55:33 +00002812/// \brief Read a floating-point value
2813llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002814 return llvm::APFloat(ReadAPInt(Record, Idx));
2815}
2816
Douglas Gregor1c507882009-04-15 21:30:51 +00002817// \brief Read a string
2818std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2819 unsigned Len = Record[Idx++];
2820 std::string Result(&Record[Idx], &Record[Idx] + Len);
2821 Idx += Len;
2822 return Result;
2823}
2824
2825/// \brief Reads attributes from the current stream position.
2826Attr *PCHReader::ReadAttributes() {
2827 unsigned Code = Stream.ReadCode();
2828 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2829 "Expected unabbreviated record"); (void)Code;
2830
2831 RecordData Record;
2832 unsigned Idx = 0;
2833 unsigned RecCode = Stream.ReadRecord(Code, Record);
2834 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2835 (void)RecCode;
2836
2837#define SIMPLE_ATTR(Name) \
2838 case Attr::Name: \
2839 New = ::new (Context) Name##Attr(); \
2840 break
2841
2842#define STRING_ATTR(Name) \
2843 case Attr::Name: \
2844 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2845 break
2846
2847#define UNSIGNED_ATTR(Name) \
2848 case Attr::Name: \
2849 New = ::new (Context) Name##Attr(Record[Idx++]); \
2850 break
2851
2852 Attr *Attrs = 0;
2853 while (Idx < Record.size()) {
2854 Attr *New = 0;
2855 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2856 bool IsInherited = Record[Idx++];
2857
2858 switch (Kind) {
2859 STRING_ATTR(Alias);
2860 UNSIGNED_ATTR(Aligned);
2861 SIMPLE_ATTR(AlwaysInline);
2862 SIMPLE_ATTR(AnalyzerNoReturn);
2863 STRING_ATTR(Annotate);
2864 STRING_ATTR(AsmLabel);
2865
2866 case Attr::Blocks:
2867 New = ::new (Context) BlocksAttr(
2868 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2869 break;
2870
2871 case Attr::Cleanup:
2872 New = ::new (Context) CleanupAttr(
2873 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2874 break;
2875
2876 SIMPLE_ATTR(Const);
2877 UNSIGNED_ATTR(Constructor);
2878 SIMPLE_ATTR(DLLExport);
2879 SIMPLE_ATTR(DLLImport);
2880 SIMPLE_ATTR(Deprecated);
2881 UNSIGNED_ATTR(Destructor);
2882 SIMPLE_ATTR(FastCall);
2883
2884 case Attr::Format: {
2885 std::string Type = ReadString(Record, Idx);
2886 unsigned FormatIdx = Record[Idx++];
2887 unsigned FirstArg = Record[Idx++];
2888 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2889 break;
2890 }
2891
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002892 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002893
2894 case Attr::IBOutletKind:
2895 New = ::new (Context) IBOutletAttr();
2896 break;
2897
2898 SIMPLE_ATTR(NoReturn);
2899 SIMPLE_ATTR(NoThrow);
2900 SIMPLE_ATTR(Nodebug);
2901 SIMPLE_ATTR(Noinline);
2902
2903 case Attr::NonNull: {
2904 unsigned Size = Record[Idx++];
2905 llvm::SmallVector<unsigned, 16> ArgNums;
2906 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2907 Idx += Size;
2908 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2909 break;
2910 }
2911
2912 SIMPLE_ATTR(ObjCException);
2913 SIMPLE_ATTR(ObjCNSObject);
2914 SIMPLE_ATTR(Overloadable);
2915 UNSIGNED_ATTR(Packed);
2916 SIMPLE_ATTR(Pure);
2917 UNSIGNED_ATTR(Regparm);
2918 STRING_ATTR(Section);
2919 SIMPLE_ATTR(StdCall);
2920 SIMPLE_ATTR(TransparentUnion);
2921 SIMPLE_ATTR(Unavailable);
2922 SIMPLE_ATTR(Unused);
2923 SIMPLE_ATTR(Used);
2924
2925 case Attr::Visibility:
2926 New = ::new (Context) VisibilityAttr(
2927 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2928 break;
2929
2930 SIMPLE_ATTR(WarnUnusedResult);
2931 SIMPLE_ATTR(Weak);
2932 SIMPLE_ATTR(WeakImport);
2933 }
2934
2935 assert(New && "Unable to decode attribute?");
2936 New->setInherited(IsInherited);
2937 New->setNext(Attrs);
2938 Attrs = New;
2939 }
2940#undef UNSIGNED_ATTR
2941#undef STRING_ATTR
2942#undef SIMPLE_ATTR
2943
2944 // The list of attributes was built backwards. Reverse the list
2945 // before returning it.
2946 Attr *PrevAttr = 0, *NextAttr = 0;
2947 while (Attrs) {
2948 NextAttr = Attrs->getNext();
2949 Attrs->setNext(PrevAttr);
2950 PrevAttr = Attrs;
2951 Attrs = NextAttr;
2952 }
2953
2954 return PrevAttr;
2955}
2956
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002957Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002958 // Within the bitstream, expressions are stored in Reverse Polish
2959 // Notation, with each of the subexpressions preceding the
2960 // expression they are stored in. To evaluate expressions, we
2961 // continue reading expressions and placing them on the stack, with
2962 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002963 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002964 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002965 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002966 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002967 llvm::SmallVector<Stmt *, 16> StmtStack;
2968 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002969 Stmt::EmptyShell Empty;
2970
Douglas Gregora151ba42009-04-14 23:32:43 +00002971 while (true) {
2972 unsigned Code = Stream.ReadCode();
2973 if (Code == llvm::bitc::END_BLOCK) {
2974 if (Stream.ReadBlockEnd()) {
2975 Error("Error at end of Source Manager block");
2976 return 0;
2977 }
2978 break;
2979 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002980
Douglas Gregora151ba42009-04-14 23:32:43 +00002981 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2982 // No known subblocks, always skip them.
2983 Stream.ReadSubBlockID();
2984 if (Stream.SkipBlock()) {
2985 Error("Malformed block record");
2986 return 0;
2987 }
2988 continue;
2989 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002990
Douglas Gregora151ba42009-04-14 23:32:43 +00002991 if (Code == llvm::bitc::DEFINE_ABBREV) {
2992 Stream.ReadAbbrevRecord();
2993 continue;
2994 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002995
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002996 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002997 Idx = 0;
2998 Record.clear();
2999 bool Finished = false;
3000 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003001 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00003002 Finished = true;
3003 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003004
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003005 case pch::STMT_NULL_PTR:
3006 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00003007 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003008
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003009 case pch::STMT_NULL:
3010 S = new (Context) NullStmt(Empty);
3011 break;
3012
3013 case pch::STMT_COMPOUND:
3014 S = new (Context) CompoundStmt(Empty);
3015 break;
3016
3017 case pch::STMT_CASE:
3018 S = new (Context) CaseStmt(Empty);
3019 break;
3020
3021 case pch::STMT_DEFAULT:
3022 S = new (Context) DefaultStmt(Empty);
3023 break;
3024
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003025 case pch::STMT_LABEL:
3026 S = new (Context) LabelStmt(Empty);
3027 break;
3028
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003029 case pch::STMT_IF:
3030 S = new (Context) IfStmt(Empty);
3031 break;
3032
3033 case pch::STMT_SWITCH:
3034 S = new (Context) SwitchStmt(Empty);
3035 break;
3036
Douglas Gregora6b503f2009-04-17 00:16:09 +00003037 case pch::STMT_WHILE:
3038 S = new (Context) WhileStmt(Empty);
3039 break;
3040
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00003041 case pch::STMT_DO:
3042 S = new (Context) DoStmt(Empty);
3043 break;
3044
3045 case pch::STMT_FOR:
3046 S = new (Context) ForStmt(Empty);
3047 break;
3048
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003049 case pch::STMT_GOTO:
3050 S = new (Context) GotoStmt(Empty);
3051 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003052
3053 case pch::STMT_INDIRECT_GOTO:
3054 S = new (Context) IndirectGotoStmt(Empty);
3055 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003056
Douglas Gregora6b503f2009-04-17 00:16:09 +00003057 case pch::STMT_CONTINUE:
3058 S = new (Context) ContinueStmt(Empty);
3059 break;
3060
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003061 case pch::STMT_BREAK:
3062 S = new (Context) BreakStmt(Empty);
3063 break;
3064
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003065 case pch::STMT_RETURN:
3066 S = new (Context) ReturnStmt(Empty);
3067 break;
3068
Douglas Gregor78ff29f2009-04-17 16:55:36 +00003069 case pch::STMT_DECL:
3070 S = new (Context) DeclStmt(Empty);
3071 break;
3072
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00003073 case pch::STMT_ASM:
3074 S = new (Context) AsmStmt(Empty);
3075 break;
3076
Douglas Gregora151ba42009-04-14 23:32:43 +00003077 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003078 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003079 break;
3080
3081 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003082 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003083 break;
3084
3085 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003086 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003087 break;
3088
3089 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003090 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003091 break;
3092
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003093 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003094 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003095 break;
3096
Douglas Gregor596e0932009-04-15 16:35:07 +00003097 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003098 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00003099 Record[PCHStmtReader::NumExprFields + 1]);
3100 break;
3101
Douglas Gregora151ba42009-04-14 23:32:43 +00003102 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003103 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003104 break;
3105
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003106 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003107 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00003108 break;
3109
Douglas Gregor12d74052009-04-15 15:58:59 +00003110 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003111 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003112 break;
3113
3114 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003115 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00003116 break;
3117
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003118 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003119 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00003120 break;
3121
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003122 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003123 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003124 break;
3125
3126 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003127 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003128 break;
3129
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003130 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003131 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003132 break;
3133
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003134 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003135 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003136 break;
3137
3138 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003139 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003140 break;
3141
Douglas Gregora151ba42009-04-14 23:32:43 +00003142 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003143 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003144 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003145
3146 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003147 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003148 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003149
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003150 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003151 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003152 break;
3153
Douglas Gregorec0b8292009-04-15 23:02:49 +00003154 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003155 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003156 break;
3157
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003158 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003159 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003160 break;
3161
3162 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003163 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003164 Record[PCHStmtReader::NumExprFields] - 1);
3165
3166 break;
3167
3168 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003169 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003170 break;
3171
Douglas Gregorec0b8292009-04-15 23:02:49 +00003172 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003173 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003174 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003175
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003176 case pch::EXPR_ADDR_LABEL:
3177 S = new (Context) AddrLabelExpr(Empty);
3178 break;
3179
Douglas Gregoreca12f62009-04-17 19:05:30 +00003180 case pch::EXPR_STMT:
3181 S = new (Context) StmtExpr(Empty);
3182 break;
3183
Douglas Gregor209d4622009-04-15 23:33:31 +00003184 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003185 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003186 break;
3187
3188 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003189 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003190 break;
3191
3192 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003193 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003194 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003195
3196 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003197 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003198 break;
3199
Douglas Gregore246b742009-04-17 19:21:43 +00003200 case pch::EXPR_BLOCK:
3201 S = new (Context) BlockExpr(Empty);
3202 break;
3203
Douglas Gregor725e94b2009-04-16 00:01:45 +00003204 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003205 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003206 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003207
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003208 case pch::EXPR_OBJC_STRING_LITERAL:
3209 S = new (Context) ObjCStringLiteral(Empty);
3210 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003211 case pch::EXPR_OBJC_ENCODE:
3212 S = new (Context) ObjCEncodeExpr(Empty);
3213 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003214 case pch::EXPR_OBJC_SELECTOR_EXPR:
3215 S = new (Context) ObjCSelectorExpr(Empty);
3216 break;
3217 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3218 S = new (Context) ObjCProtocolExpr(Empty);
3219 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003220 }
3221
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003222 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003223 if (Finished)
3224 break;
3225
Douglas Gregor456e0952009-04-17 22:13:46 +00003226 ++NumStatementsRead;
3227
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003228 if (S) {
3229 unsigned NumSubStmts = Reader.Visit(S);
3230 while (NumSubStmts > 0) {
3231 StmtStack.pop_back();
3232 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003233 }
3234 }
3235
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003236 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003237 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003238 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003239 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003240 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003241 return StmtStack.back();
3242}
3243
3244Expr *PCHReader::ReadExpr() {
3245 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003246}
3247
Douglas Gregor179cfb12009-04-10 20:39:37 +00003248DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003249 return Diag(SourceLocation(), DiagID);
3250}
3251
3252DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3253 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003254 Context.getSourceManager()),
3255 DiagID);
3256}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003257
Douglas Gregorc713da92009-04-21 22:25:48 +00003258/// \brief Retrieve the identifier table associated with the
3259/// preprocessor.
3260IdentifierTable &PCHReader::getIdentifierTable() {
3261 return PP.getIdentifierTable();
3262}
3263
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003264/// \brief Record that the given ID maps to the given switch-case
3265/// statement.
3266void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3267 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3268 SwitchCaseStmts[ID] = SC;
3269}
3270
3271/// \brief Retrieve the switch-case statement with the given ID.
3272SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3273 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3274 return SwitchCaseStmts[ID];
3275}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003276
3277/// \brief Record that the given label statement has been
3278/// deserialized and has the given ID.
3279void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3280 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3281 "Deserialized label twice");
3282 LabelStmts[ID] = S;
3283
3284 // If we've already seen any goto statements that point to this
3285 // label, resolve them now.
3286 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3287 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3288 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3289 Goto->second->setLabel(S);
3290 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003291
3292 // If we've already seen any address-label statements that point to
3293 // this label, resolve them now.
3294 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3295 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3296 = UnresolvedAddrLabelExprs.equal_range(ID);
3297 for (AddrLabelIter AddrLabel = AddrLabels.first;
3298 AddrLabel != AddrLabels.second; ++AddrLabel)
3299 AddrLabel->second->setLabel(S);
3300 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003301}
3302
3303/// \brief Set the label of the given statement to the label
3304/// identified by ID.
3305///
3306/// Depending on the order in which the label and other statements
3307/// referencing that label occur, this operation may complete
3308/// immediately (updating the statement) or it may queue the
3309/// statement to be back-patched later.
3310void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3311 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3312 if (Label != LabelStmts.end()) {
3313 // We've already seen this label, so set the label of the goto and
3314 // we're done.
3315 S->setLabel(Label->second);
3316 } else {
3317 // We haven't seen this label yet, so add this goto to the set of
3318 // unresolved goto statements.
3319 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3320 }
3321}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003322
3323/// \brief Set the label of the given expression to the label
3324/// identified by ID.
3325///
3326/// Depending on the order in which the label and other statements
3327/// referencing that label occur, this operation may complete
3328/// immediately (updating the statement) or it may queue the
3329/// statement to be back-patched later.
3330void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3331 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3332 if (Label != LabelStmts.end()) {
3333 // We've already seen this label, so set the label of the
3334 // label-address expression and we're done.
3335 S->setLabel(Label->second);
3336 } else {
3337 // We haven't seen this label yet, so add this label-address
3338 // expression to the set of unresolved label-address expressions.
3339 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3340 }
3341}