blob: 851eebc35520ef91629b91632ba09aaa29dcb6c3 [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"
Douglas Gregorc713da92009-04-21 22:25:48 +000026#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000028#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000029#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include <algorithm>
35#include <cstdio>
36
37using namespace clang;
38
Douglas Gregore0ad2dd2009-04-21 23:56:24 +000039namespace {
40 /// \brief Helper class that saves the current stream position and
41 /// then restores it when destroyed.
42 struct VISIBILITY_HIDDEN SavedStreamPosition {
43 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
44 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
45
46 ~SavedStreamPosition() {
47 Stream.JumpToBit(Offset);
48 }
49
50 private:
51 llvm::BitstreamReader &Stream;
52 uint64_t Offset;
53 };
54}
55
Douglas Gregorc34897d2009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Declaration deserialization
58//===----------------------------------------------------------------------===//
59namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000060 class VISIBILITY_HIDDEN PCHDeclReader
61 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 PCHReader &Reader;
63 const PCHReader::RecordData &Record;
64 unsigned &Idx;
65
66 public:
67 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
68 unsigned &Idx)
69 : Reader(Reader), Record(Record), Idx(Idx) { }
70
71 void VisitDecl(Decl *D);
72 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
73 void VisitNamedDecl(NamedDecl *ND);
74 void VisitTypeDecl(TypeDecl *TD);
75 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000076 void VisitTagDecl(TagDecl *TD);
77 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000078 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000079 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000080 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000081 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000082 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000083 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000084 void VisitParmVarDecl(ParmVarDecl *PD);
85 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000086 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
87 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000088 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000089 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000090 void VisitObjCContainerDecl(ObjCContainerDecl *D);
91 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
92 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Steve Naroff97b53bd2009-04-21 15:12:33 +000093 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
94 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
95 void VisitObjCClassDecl(ObjCClassDecl *D);
96 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
97 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
98 void VisitObjCImplDecl(ObjCImplDecl *D);
99 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
100 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
101 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
102 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
103 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000104 };
105}
106
107void PCHDeclReader::VisitDecl(Decl *D) {
108 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
109 D->setLexicalDeclContext(
110 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
111 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
112 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +0000113 if (Record[Idx++])
114 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000115 D->setImplicit(Record[Idx++]);
116 D->setAccess((AccessSpecifier)Record[Idx++]);
117}
118
119void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
120 VisitDecl(TU);
121}
122
123void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
124 VisitDecl(ND);
125 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
126}
127
128void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
129 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
131}
132
133void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000134 // Note that we cannot use VisitTypeDecl here, because we need to
135 // set the underlying type of the typedef *before* we try to read
136 // the type associated with the TypedefDecl.
137 VisitNamedDecl(TD);
138 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
139 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
140 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000141}
142
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000143void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
144 VisitTypeDecl(TD);
145 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
146 TD->setDefinition(Record[Idx++]);
147 TD->setTypedefForAnonDecl(
148 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
149}
150
151void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
152 VisitTagDecl(ED);
153 ED->setIntegerType(Reader.GetType(Record[Idx++]));
154}
155
Douglas Gregor982365e2009-04-13 21:20:57 +0000156void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
157 VisitTagDecl(RD);
158 RD->setHasFlexibleArrayMember(Record[Idx++]);
159 RD->setAnonymousStructOrUnion(Record[Idx++]);
160}
161
Douglas Gregorc34897d2009-04-09 22:27:44 +0000162void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
163 VisitNamedDecl(VD);
164 VD->setType(Reader.GetType(Record[Idx++]));
165}
166
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000167void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
168 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000169 if (Record[Idx++])
170 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000171 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
172}
173
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000174void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
175 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000176 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000177 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000178 FD->setPreviousDeclaration(
179 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
180 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
181 FD->setInline(Record[Idx++]);
182 FD->setVirtual(Record[Idx++]);
183 FD->setPure(Record[Idx++]);
184 FD->setInheritedPrototype(Record[Idx++]);
185 FD->setHasPrototype(Record[Idx++]);
186 FD->setDeleted(Record[Idx++]);
187 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
188 unsigned NumParams = Record[Idx++];
189 llvm::SmallVector<ParmVarDecl *, 16> Params;
190 Params.reserve(NumParams);
191 for (unsigned I = 0; I != NumParams; ++I)
192 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
193 FD->setParams(Reader.getContext(), &Params[0], NumParams);
194}
195
Steve Naroff79ea0e02009-04-20 15:06:07 +0000196void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
197 VisitNamedDecl(MD);
198 if (Record[Idx++]) {
199 // In practice, this won't be executed (since method definitions
200 // don't occur in header files).
201 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
202 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
203 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
204 }
205 MD->setInstanceMethod(Record[Idx++]);
206 MD->setVariadic(Record[Idx++]);
207 MD->setSynthesized(Record[Idx++]);
208 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
209 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
210 MD->setResultType(Reader.GetType(Record[Idx++]));
211 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
212 unsigned NumParams = Record[Idx++];
213 llvm::SmallVector<ParmVarDecl *, 16> Params;
214 Params.reserve(NumParams);
215 for (unsigned I = 0; I != NumParams; ++I)
216 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
217 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
218}
219
Steve Naroff7333b492009-04-20 20:09:33 +0000220void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
221 VisitNamedDecl(CD);
222 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
226 VisitObjCContainerDecl(ID);
227 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
Chris Lattner80f83c62009-04-22 05:57:30 +0000228 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
229 (Reader.GetDecl(Record[Idx++])));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000230 unsigned NumProtocols = Record[Idx++];
231 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
232 Protocols.reserve(NumProtocols);
233 for (unsigned I = 0; I != NumProtocols; ++I)
234 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000235 unsigned NumIvars = Record[Idx++];
236 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
237 IVars.reserve(NumIvars);
238 for (unsigned I = 0; I != NumIvars; ++I)
239 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
240 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
241
242 ID->setForwardDecl(Record[Idx++]);
243 ID->setImplicitInterfaceDecl(Record[Idx++]);
244 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
245 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000246 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor37a54fd2009-04-23 03:59:07 +0000247 // FIXME: add categories.
Steve Naroff7333b492009-04-20 20:09:33 +0000248}
249
250void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
251 VisitFieldDecl(IVD);
252 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
253}
254
Steve Naroff97b53bd2009-04-21 15:12:33 +0000255void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
256 VisitObjCContainerDecl(PD);
257 PD->setForwardDecl(Record[Idx++]);
258 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
259 unsigned NumProtoRefs = Record[Idx++];
260 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
261 ProtoRefs.reserve(NumProtoRefs);
262 for (unsigned I = 0; I != NumProtoRefs; ++I)
263 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
264 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
265}
266
267void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
268 VisitFieldDecl(FD);
269}
270
271void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
272 VisitDecl(CD);
273 unsigned NumClassRefs = Record[Idx++];
274 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
275 ClassRefs.reserve(NumClassRefs);
276 for (unsigned I = 0; I != NumClassRefs; ++I)
277 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
278 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
279}
280
281void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
282 VisitDecl(FPD);
283 unsigned NumProtoRefs = Record[Idx++];
284 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
285 ProtoRefs.reserve(NumProtoRefs);
286 for (unsigned I = 0; I != NumProtoRefs; ++I)
287 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
288 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
289}
290
291void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
292 VisitObjCContainerDecl(CD);
293 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
294 unsigned NumProtoRefs = Record[Idx++];
295 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
296 ProtoRefs.reserve(NumProtoRefs);
297 for (unsigned I = 0; I != NumProtoRefs; ++I)
298 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
299 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
300 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
301 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
302}
303
304void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
305 VisitNamedDecl(CAD);
306 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
307}
308
309void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
310 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000311 D->setType(Reader.GetType(Record[Idx++]));
312 // FIXME: stable encoding
313 D->setPropertyAttributes(
314 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
315 // FIXME: stable encoding
316 D->setPropertyImplementation(
317 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
318 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
319 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
320 D->setGetterMethodDecl(
321 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
322 D->setSetterMethodDecl(
323 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
324 D->setPropertyIvarDecl(
325 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000326}
327
328void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
329 VisitDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000330 D->setClassInterface(
331 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
332 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000333}
334
335void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
336 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000337 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000338}
339
340void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
341 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000342 D->setSuperClass(
343 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000344}
345
346
347void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
348 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000349 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
350 D->setPropertyDecl(
351 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
352 D->setPropertyIvarDecl(
353 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000354}
355
Douglas Gregor982365e2009-04-13 21:20:57 +0000356void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
357 VisitValueDecl(FD);
358 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000359 if (Record[Idx++])
360 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000361}
362
Douglas Gregorc34897d2009-04-09 22:27:44 +0000363void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
364 VisitValueDecl(VD);
365 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
366 VD->setThreadSpecified(Record[Idx++]);
367 VD->setCXXDirectInitializer(Record[Idx++]);
368 VD->setDeclaredInCondition(Record[Idx++]);
369 VD->setPreviousDeclaration(
370 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
371 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000372 if (Record[Idx++])
373 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000374}
375
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000376void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
377 VisitVarDecl(PD);
378 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000379 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000380}
381
382void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
383 VisitParmVarDecl(PD);
384 PD->setOriginalType(Reader.GetType(Record[Idx++]));
385}
386
Douglas Gregor2a491792009-04-13 22:49:25 +0000387void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
388 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000389 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000390}
391
392void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
393 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000394 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000395 unsigned NumParams = Record[Idx++];
396 llvm::SmallVector<ParmVarDecl *, 16> Params;
397 Params.reserve(NumParams);
398 for (unsigned I = 0; I != NumParams; ++I)
399 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
400 BD->setParams(Reader.getContext(), &Params[0], NumParams);
401}
402
Douglas Gregorc34897d2009-04-09 22:27:44 +0000403std::pair<uint64_t, uint64_t>
404PCHDeclReader::VisitDeclContext(DeclContext *DC) {
405 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000406 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000407 return std::make_pair(LexicalOffset, VisibleOffset);
408}
409
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000410//===----------------------------------------------------------------------===//
411// Statement/expression deserialization
412//===----------------------------------------------------------------------===//
413namespace {
414 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000415 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000416 PCHReader &Reader;
417 const PCHReader::RecordData &Record;
418 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000419 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000420
421 public:
422 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000423 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
424 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000425
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000426 /// \brief The number of record fields required for the Stmt class
427 /// itself.
428 static const unsigned NumStmtFields = 0;
429
Douglas Gregor596e0932009-04-15 16:35:07 +0000430 /// \brief The number of record fields required for the Expr class
431 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000432 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000433
Douglas Gregora151ba42009-04-14 23:32:43 +0000434 // Each of the Visit* functions reads in part of the expression
435 // from the given record and the current expression stack, then
436 // return the total number of operands that it read from the
437 // expression stack.
438
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000439 unsigned VisitStmt(Stmt *S);
440 unsigned VisitNullStmt(NullStmt *S);
441 unsigned VisitCompoundStmt(CompoundStmt *S);
442 unsigned VisitSwitchCase(SwitchCase *S);
443 unsigned VisitCaseStmt(CaseStmt *S);
444 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000445 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000446 unsigned VisitIfStmt(IfStmt *S);
447 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000448 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000449 unsigned VisitDoStmt(DoStmt *S);
450 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000451 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000452 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000453 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000454 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000455 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000456 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000457 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000458 unsigned VisitExpr(Expr *E);
459 unsigned VisitPredefinedExpr(PredefinedExpr *E);
460 unsigned VisitDeclRefExpr(DeclRefExpr *E);
461 unsigned VisitIntegerLiteral(IntegerLiteral *E);
462 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000463 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000464 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000465 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000466 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000467 unsigned VisitUnaryOperator(UnaryOperator *E);
468 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000469 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000470 unsigned VisitCallExpr(CallExpr *E);
471 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000472 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000473 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000474 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
475 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000476 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000477 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
478 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000479 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000480 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000481 unsigned VisitInitListExpr(InitListExpr *E);
482 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
483 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000484 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000485 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000486 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000487 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
488 unsigned VisitChooseExpr(ChooseExpr *E);
489 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000490 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000491 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000492 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000493 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000494 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000495 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
496 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000497 };
498}
499
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000500unsigned PCHStmtReader::VisitStmt(Stmt *S) {
501 assert(Idx == NumStmtFields && "Incorrect statement field count");
502 return 0;
503}
504
505unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
506 VisitStmt(S);
507 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 return 0;
509}
510
511unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
512 VisitStmt(S);
513 unsigned NumStmts = Record[Idx++];
514 S->setStmts(Reader.getContext(),
515 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
516 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
517 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
518 return NumStmts;
519}
520
521unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
522 VisitStmt(S);
523 Reader.RecordSwitchCaseID(S, Record[Idx++]);
524 return 0;
525}
526
527unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
528 VisitSwitchCase(S);
529 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
530 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
531 S->setSubStmt(StmtStack.back());
532 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
533 return 3;
534}
535
536unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
537 VisitSwitchCase(S);
538 S->setSubStmt(StmtStack.back());
539 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
540 return 1;
541}
542
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000543unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
544 VisitStmt(S);
545 S->setID(Reader.GetIdentifierInfo(Record, Idx));
546 S->setSubStmt(StmtStack.back());
547 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 Reader.RecordLabelStmt(S, Record[Idx++]);
549 return 1;
550}
551
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000552unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
553 VisitStmt(S);
554 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
555 S->setThen(StmtStack[StmtStack.size() - 2]);
556 S->setElse(StmtStack[StmtStack.size() - 1]);
557 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
558 return 3;
559}
560
561unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
562 VisitStmt(S);
563 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
564 S->setBody(StmtStack.back());
565 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 SwitchCase *PrevSC = 0;
567 for (unsigned N = Record.size(); Idx != N; ++Idx) {
568 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
569 if (PrevSC)
570 PrevSC->setNextSwitchCase(SC);
571 else
572 S->setSwitchCaseList(SC);
573 PrevSC = SC;
574 }
575 return 2;
576}
577
Douglas Gregora6b503f2009-04-17 00:16:09 +0000578unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
579 VisitStmt(S);
580 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
581 S->setBody(StmtStack.back());
582 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
583 return 2;
584}
585
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000586unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
587 VisitStmt(S);
588 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
589 S->setBody(StmtStack.back());
590 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
591 return 2;
592}
593
594unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
595 VisitStmt(S);
596 S->setInit(StmtStack[StmtStack.size() - 4]);
597 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
598 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
599 S->setBody(StmtStack.back());
600 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
601 return 4;
602}
603
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000604unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
605 VisitStmt(S);
606 Reader.SetLabelOf(S, Record[Idx++]);
607 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
608 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
609 return 0;
610}
611
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000612unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
613 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000614 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000615 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
616 return 1;
617}
618
Douglas Gregora6b503f2009-04-17 00:16:09 +0000619unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
620 VisitStmt(S);
621 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
622 return 0;
623}
624
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000625unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
626 VisitStmt(S);
627 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
628 return 0;
629}
630
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000631unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
632 VisitStmt(S);
633 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
634 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
635 return 1;
636}
637
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000638unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
639 VisitStmt(S);
640 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
641 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
642
643 if (Idx + 1 == Record.size()) {
644 // Single declaration
645 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
646 } else {
647 llvm::SmallVector<Decl *, 16> Decls;
648 Decls.reserve(Record.size() - Idx);
649 for (unsigned N = Record.size(); Idx != N; ++Idx)
650 Decls.push_back(Reader.GetDecl(Record[Idx]));
651 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
652 &Decls[0], Decls.size())));
653 }
654 return 0;
655}
656
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000657unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
658 VisitStmt(S);
659 unsigned NumOutputs = Record[Idx++];
660 unsigned NumInputs = Record[Idx++];
661 unsigned NumClobbers = Record[Idx++];
662 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
663 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
664 S->setVolatile(Record[Idx++]);
665 S->setSimple(Record[Idx++]);
666
667 unsigned StackIdx
668 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
669 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
670
671 // Outputs and inputs
672 llvm::SmallVector<std::string, 16> Names;
673 llvm::SmallVector<StringLiteral*, 16> Constraints;
674 llvm::SmallVector<Stmt*, 16> Exprs;
675 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
676 Names.push_back(Reader.ReadString(Record, Idx));
677 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
678 Exprs.push_back(StmtStack[StackIdx++]);
679 }
680 S->setOutputsAndInputs(NumOutputs, NumInputs,
681 &Names[0], &Constraints[0], &Exprs[0]);
682
683 // Constraints
684 llvm::SmallVector<StringLiteral*, 16> Clobbers;
685 for (unsigned I = 0; I != NumClobbers; ++I)
686 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
687 S->setClobbers(&Clobbers[0], NumClobbers);
688
689 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
690 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
691}
692
Douglas Gregora151ba42009-04-14 23:32:43 +0000693unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000694 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000695 E->setType(Reader.GetType(Record[Idx++]));
696 E->setTypeDependent(Record[Idx++]);
697 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000698 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000699 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000700}
701
Douglas Gregora151ba42009-04-14 23:32:43 +0000702unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000703 VisitExpr(E);
704 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
705 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000706 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000707}
708
Douglas Gregora151ba42009-04-14 23:32:43 +0000709unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000710 VisitExpr(E);
711 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
712 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000713 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000714}
715
Douglas Gregora151ba42009-04-14 23:32:43 +0000716unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000717 VisitExpr(E);
718 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
719 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000720 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000721}
722
Douglas Gregora151ba42009-04-14 23:32:43 +0000723unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000724 VisitExpr(E);
725 E->setValue(Reader.ReadAPFloat(Record, Idx));
726 E->setExact(Record[Idx++]);
727 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000728 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000729}
730
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000731unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
732 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000733 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000734 return 1;
735}
736
Douglas Gregor596e0932009-04-15 16:35:07 +0000737unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
738 VisitExpr(E);
739 unsigned Len = Record[Idx++];
740 assert(Record[Idx] == E->getNumConcatenated() &&
741 "Wrong number of concatenated tokens!");
742 ++Idx;
743 E->setWide(Record[Idx++]);
744
745 // Read string data
746 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
747 E->setStrData(Reader.getContext(), &Str[0], Len);
748 Idx += Len;
749
750 // Read source locations
751 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
752 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
753
754 return 0;
755}
756
Douglas Gregora151ba42009-04-14 23:32:43 +0000757unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000758 VisitExpr(E);
759 E->setValue(Record[Idx++]);
760 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
761 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000762 return 0;
763}
764
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000765unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
766 VisitExpr(E);
767 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000769 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000770 return 1;
771}
772
Douglas Gregor12d74052009-04-15 15:58:59 +0000773unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
774 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000775 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000776 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
777 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
778 return 1;
779}
780
781unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
782 VisitExpr(E);
783 E->setSizeof(Record[Idx++]);
784 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000785 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000786 ++Idx;
787 } else {
788 E->setArgument(Reader.GetType(Record[Idx++]));
789 }
790 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
791 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
792 return E->isArgumentType()? 0 : 1;
793}
794
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000795unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
796 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000797 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
798 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000799 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
800 return 2;
801}
802
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000803unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
804 VisitExpr(E);
805 E->setNumArgs(Reader.getContext(), Record[Idx++]);
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000807 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000808 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000809 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000810 return E->getNumArgs() + 1;
811}
812
813unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
814 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000815 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000816 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
817 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
818 E->setArrow(Record[Idx++]);
819 return 1;
820}
821
Douglas Gregora151ba42009-04-14 23:32:43 +0000822unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
823 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000824 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000825 return 1;
826}
827
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000828unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
829 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000830 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
831 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000832 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
833 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
834 return 2;
835}
836
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000837unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
838 VisitBinaryOperator(E);
839 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
840 E->setComputationResultType(Reader.GetType(Record[Idx++]));
841 return 2;
842}
843
844unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
845 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000846 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
847 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
848 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000849 return 3;
850}
851
Douglas Gregora151ba42009-04-14 23:32:43 +0000852unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
853 VisitCastExpr(E);
854 E->setLvalueCast(Record[Idx++]);
855 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000856}
857
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000858unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
859 VisitCastExpr(E);
860 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
861 return 1;
862}
863
864unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
865 VisitExplicitCastExpr(E);
866 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
867 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
868 return 1;
869}
870
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000871unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
872 VisitExpr(E);
873 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000874 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000875 E->setFileScope(Record[Idx++]);
876 return 1;
877}
878
Douglas Gregorec0b8292009-04-15 23:02:49 +0000879unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
880 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000881 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000882 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
883 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
884 return 1;
885}
886
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000887unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
888 VisitExpr(E);
889 unsigned NumInits = Record[Idx++];
890 E->reserveInits(NumInits);
891 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000892 E->updateInit(I,
893 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
894 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000895 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
896 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
897 E->setInitializedFieldInUnion(
898 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
899 E->sawArrayRangeDesignator(Record[Idx++]);
900 return NumInits + 1;
901}
902
903unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
904 typedef DesignatedInitExpr::Designator Designator;
905
906 VisitExpr(E);
907 unsigned NumSubExprs = Record[Idx++];
908 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
909 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000910 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000911 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
912 E->setGNUSyntax(Record[Idx++]);
913
914 llvm::SmallVector<Designator, 4> Designators;
915 while (Idx < Record.size()) {
916 switch ((pch::DesignatorTypes)Record[Idx++]) {
917 case pch::DESIG_FIELD_DECL: {
918 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
919 SourceLocation DotLoc
920 = SourceLocation::getFromRawEncoding(Record[Idx++]);
921 SourceLocation FieldLoc
922 = SourceLocation::getFromRawEncoding(Record[Idx++]);
923 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
924 FieldLoc));
925 Designators.back().setField(Field);
926 break;
927 }
928
929 case pch::DESIG_FIELD_NAME: {
930 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
931 SourceLocation DotLoc
932 = SourceLocation::getFromRawEncoding(Record[Idx++]);
933 SourceLocation FieldLoc
934 = SourceLocation::getFromRawEncoding(Record[Idx++]);
935 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
936 break;
937 }
938
939 case pch::DESIG_ARRAY: {
940 unsigned Index = Record[Idx++];
941 SourceLocation LBracketLoc
942 = SourceLocation::getFromRawEncoding(Record[Idx++]);
943 SourceLocation RBracketLoc
944 = SourceLocation::getFromRawEncoding(Record[Idx++]);
945 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
946 break;
947 }
948
949 case pch::DESIG_ARRAY_RANGE: {
950 unsigned Index = Record[Idx++];
951 SourceLocation LBracketLoc
952 = SourceLocation::getFromRawEncoding(Record[Idx++]);
953 SourceLocation EllipsisLoc
954 = SourceLocation::getFromRawEncoding(Record[Idx++]);
955 SourceLocation RBracketLoc
956 = SourceLocation::getFromRawEncoding(Record[Idx++]);
957 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
958 RBracketLoc));
959 break;
960 }
961 }
962 }
963 E->setDesignators(&Designators[0], Designators.size());
964
965 return NumSubExprs;
966}
967
968unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
969 VisitExpr(E);
970 return 0;
971}
972
Douglas Gregorec0b8292009-04-15 23:02:49 +0000973unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
974 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000975 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000976 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
977 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
978 return 1;
979}
980
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000981unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
982 VisitExpr(E);
983 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
984 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
985 Reader.SetLabelOf(E, Record[Idx++]);
986 return 0;
987}
988
Douglas Gregoreca12f62009-04-17 19:05:30 +0000989unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
990 VisitExpr(E);
991 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
992 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
993 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
994 return 1;
995}
996
Douglas Gregor209d4622009-04-15 23:33:31 +0000997unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
998 VisitExpr(E);
999 E->setArgType1(Reader.GetType(Record[Idx++]));
1000 E->setArgType2(Reader.GetType(Record[Idx++]));
1001 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1002 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1003 return 0;
1004}
1005
1006unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1007 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001008 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1009 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1010 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001011 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1012 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1013 return 3;
1014}
1015
1016unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1017 VisitExpr(E);
1018 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1019 return 0;
1020}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001021
Douglas Gregor725e94b2009-04-16 00:01:45 +00001022unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1023 VisitExpr(E);
1024 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001025 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001026 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1027 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1028 return NumExprs;
1029}
1030
Douglas Gregore246b742009-04-17 19:21:43 +00001031unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1032 VisitExpr(E);
1033 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1034 E->setHasBlockDeclRefExprs(Record[Idx++]);
1035 return 0;
1036}
1037
Douglas Gregor725e94b2009-04-16 00:01:45 +00001038unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1039 VisitExpr(E);
1040 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1041 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1042 E->setByRef(Record[Idx++]);
1043 return 0;
1044}
1045
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001046//===----------------------------------------------------------------------===//
1047// Objective-C Expressions and Statements
1048
1049unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1050 VisitExpr(E);
1051 E->setString(cast<StringLiteral>(StmtStack.back()));
1052 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1053 return 1;
1054}
1055
Chris Lattner80f83c62009-04-22 05:57:30 +00001056unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1057 VisitExpr(E);
1058 E->setEncodedType(Reader.GetType(Record[Idx++]));
1059 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1060 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1061 return 0;
1062}
1063
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001064unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1065 VisitExpr(E);
1066 // FIXME: Selectors.
1067 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1068 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1069 return 0;
1070}
1071
1072unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1073 VisitExpr(E);
1074 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1075 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1076 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1077 return 0;
1078}
1079
Chris Lattner80f83c62009-04-22 05:57:30 +00001080
Douglas Gregorc713da92009-04-21 22:25:48 +00001081//===----------------------------------------------------------------------===//
1082// PCH reader implementation
1083//===----------------------------------------------------------------------===//
1084
1085namespace {
1086class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1087 PCHReader &Reader;
1088
1089 // If we know the IdentifierInfo in advance, it is here and we will
1090 // not build a new one. Used when deserializing information about an
1091 // identifier that was constructed before the PCH file was read.
1092 IdentifierInfo *KnownII;
1093
1094public:
1095 typedef IdentifierInfo * data_type;
1096
1097 typedef const std::pair<const char*, unsigned> external_key_type;
1098
1099 typedef external_key_type internal_key_type;
1100
1101 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1102 : Reader(Reader), KnownII(II) { }
1103
1104 static bool EqualKey(const internal_key_type& a,
1105 const internal_key_type& b) {
1106 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1107 : false;
1108 }
1109
1110 static unsigned ComputeHash(const internal_key_type& a) {
1111 return BernsteinHash(a.first, a.second);
1112 }
1113
1114 // This hopefully will just get inlined and removed by the optimizer.
1115 static const internal_key_type&
1116 GetInternalKey(const external_key_type& x) { return x; }
1117
1118 static std::pair<unsigned, unsigned>
1119 ReadKeyDataLength(const unsigned char*& d) {
1120 using namespace clang::io;
1121 unsigned KeyLen = ReadUnalignedLE16(d);
1122 unsigned DataLen = ReadUnalignedLE16(d);
1123 return std::make_pair(KeyLen, DataLen);
1124 }
1125
1126 static std::pair<const char*, unsigned>
1127 ReadKey(const unsigned char* d, unsigned n) {
1128 assert(n >= 2 && d[n-1] == '\0');
1129 return std::make_pair((const char*) d, n-1);
1130 }
1131
1132 IdentifierInfo *ReadData(const internal_key_type& k,
1133 const unsigned char* d,
1134 unsigned DataLen) {
1135 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001136 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001137 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1138 Bits >>= 1;
1139 bool Poisoned = Bits & 0x01;
1140 Bits >>= 1;
1141 bool ExtensionToken = Bits & 0x01;
1142 Bits >>= 1;
1143 bool hasMacroDefinition = Bits & 0x01;
1144 Bits >>= 1;
1145 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1146 Bits >>= 10;
1147 unsigned TokenID = Bits & 0xFF;
1148 Bits >>= 8;
1149
Douglas Gregorc713da92009-04-21 22:25:48 +00001150 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001151 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001152 DataLen -= 8;
1153
1154 // Build the IdentifierInfo itself and link the identifier ID with
1155 // the new IdentifierInfo.
1156 IdentifierInfo *II = KnownII;
1157 if (!II)
1158 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1159 k.first, k.first + k.second);
1160 Reader.SetIdentifierInfo(ID, II);
1161
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001162 // Set or check the various bits in the IdentifierInfo structure.
1163 // FIXME: Load token IDs lazily, too?
1164 assert((unsigned)II->getTokenID() == TokenID &&
1165 "Incorrect token ID loaded");
1166 (void)TokenID;
1167 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1168 assert(II->isExtensionToken() == ExtensionToken &&
1169 "Incorrect extension token flag");
1170 (void)ExtensionToken;
1171 II->setIsPoisoned(Poisoned);
1172 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1173 "Incorrect C++ operator keyword flag");
1174 (void)CPlusPlusOperatorKeyword;
1175
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001176 // If this identifier is a macro, deserialize the macro
1177 // definition.
1178 if (hasMacroDefinition) {
1179 uint32_t Offset = ReadUnalignedLE64(d);
1180 Reader.ReadMacroRecord(Offset);
1181 DataLen -= 8;
1182 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001183
1184 // Read all of the declarations visible at global scope with this
1185 // name.
1186 Sema *SemaObj = Reader.getSema();
1187 while (DataLen > 0) {
1188 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001189 if (SemaObj) {
1190 // Introduce this declaration into the translation-unit scope
1191 // and add it to the declaration chain for this identifier, so
1192 // that (unqualified) name lookup will find it.
1193 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1194 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1195 } else {
1196 // Queue this declaration so that it will be added to the
1197 // translation unit scope and identifier's declaration chain
1198 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001199 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001200 }
1201
1202 DataLen -= 4;
1203 }
1204 return II;
1205 }
1206};
1207
1208} // end anonymous namespace
1209
1210/// \brief The on-disk hash table used to contain information about
1211/// all of the identifiers in the program.
1212typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1213 PCHIdentifierLookupTable;
1214
Douglas Gregorc34897d2009-04-09 22:27:44 +00001215// FIXME: use the diagnostics machinery
1216static bool Error(const char *Str) {
1217 std::fprintf(stderr, "%s\n", Str);
1218 return true;
1219}
1220
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001221/// \brief Check the contents of the predefines buffer against the
1222/// contents of the predefines buffer used to build the PCH file.
1223///
1224/// The contents of the two predefines buffers should be the same. If
1225/// not, then some command-line option changed the preprocessor state
1226/// and we must reject the PCH file.
1227///
1228/// \param PCHPredef The start of the predefines buffer in the PCH
1229/// file.
1230///
1231/// \param PCHPredefLen The length of the predefines buffer in the PCH
1232/// file.
1233///
1234/// \param PCHBufferID The FileID for the PCH predefines buffer.
1235///
1236/// \returns true if there was a mismatch (in which case the PCH file
1237/// should be ignored), or false otherwise.
1238bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1239 unsigned PCHPredefLen,
1240 FileID PCHBufferID) {
1241 const char *Predef = PP.getPredefines().c_str();
1242 unsigned PredefLen = PP.getPredefines().size();
1243
1244 // If the two predefines buffers compare equal, we're done!.
1245 if (PredefLen == PCHPredefLen &&
1246 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1247 return false;
1248
1249 // The predefines buffers are different. Produce a reasonable
1250 // diagnostic showing where they are different.
1251
1252 // The source locations (potentially in the two different predefines
1253 // buffers)
1254 SourceLocation Loc1, Loc2;
1255 SourceManager &SourceMgr = PP.getSourceManager();
1256
1257 // Create a source buffer for our predefines string, so
1258 // that we can build a diagnostic that points into that
1259 // source buffer.
1260 FileID BufferID;
1261 if (Predef && Predef[0]) {
1262 llvm::MemoryBuffer *Buffer
1263 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1264 "<built-in>");
1265 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1266 }
1267
1268 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1269 std::pair<const char *, const char *> Locations
1270 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1271
1272 if (Locations.first != Predef + MinLen) {
1273 // We found the location in the two buffers where there is a
1274 // difference. Form source locations to point there (in both
1275 // buffers).
1276 unsigned Offset = Locations.first - Predef;
1277 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1278 .getFileLocWithOffset(Offset);
1279 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1280 .getFileLocWithOffset(Offset);
1281 } else if (PredefLen > PCHPredefLen) {
1282 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1283 .getFileLocWithOffset(MinLen);
1284 } else {
1285 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1286 .getFileLocWithOffset(MinLen);
1287 }
1288
1289 Diag(Loc1, diag::warn_pch_preprocessor);
1290 if (Loc2.isValid())
1291 Diag(Loc2, diag::note_predef_in_pch);
1292 Diag(diag::note_ignoring_pch) << FileName;
1293 return true;
1294}
1295
Douglas Gregor635f97f2009-04-13 16:31:14 +00001296/// \brief Read the line table in the source manager block.
1297/// \returns true if ther was an error.
1298static bool ParseLineTable(SourceManager &SourceMgr,
1299 llvm::SmallVectorImpl<uint64_t> &Record) {
1300 unsigned Idx = 0;
1301 LineTableInfo &LineTable = SourceMgr.getLineTable();
1302
1303 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001304 std::map<int, int> FileIDs;
1305 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001306 // Extract the file name
1307 unsigned FilenameLen = Record[Idx++];
1308 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1309 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001310 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1311 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001312 }
1313
1314 // Parse the line entries
1315 std::vector<LineEntry> Entries;
1316 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001317 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001318
1319 // Extract the line entries
1320 unsigned NumEntries = Record[Idx++];
1321 Entries.clear();
1322 Entries.reserve(NumEntries);
1323 for (unsigned I = 0; I != NumEntries; ++I) {
1324 unsigned FileOffset = Record[Idx++];
1325 unsigned LineNo = Record[Idx++];
1326 int FilenameID = Record[Idx++];
1327 SrcMgr::CharacteristicKind FileKind
1328 = (SrcMgr::CharacteristicKind)Record[Idx++];
1329 unsigned IncludeOffset = Record[Idx++];
1330 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1331 FileKind, IncludeOffset));
1332 }
1333 LineTable.AddEntry(FID, Entries);
1334 }
1335
1336 return false;
1337}
1338
Douglas Gregorab1cef72009-04-10 03:52:48 +00001339/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001340PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001341 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001342 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1343 Error("Malformed source manager block record");
1344 return Failure;
1345 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001346
1347 SourceManager &SourceMgr = Context.getSourceManager();
1348 RecordData Record;
1349 while (true) {
1350 unsigned Code = Stream.ReadCode();
1351 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001352 if (Stream.ReadBlockEnd()) {
1353 Error("Error at end of Source Manager block");
1354 return Failure;
1355 }
1356
1357 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001358 }
1359
1360 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1361 // No known subblocks, always skip them.
1362 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001363 if (Stream.SkipBlock()) {
1364 Error("Malformed block record");
1365 return Failure;
1366 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001367 continue;
1368 }
1369
1370 if (Code == llvm::bitc::DEFINE_ABBREV) {
1371 Stream.ReadAbbrevRecord();
1372 continue;
1373 }
1374
1375 // Read a record.
1376 const char *BlobStart;
1377 unsigned BlobLen;
1378 Record.clear();
1379 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1380 default: // Default behavior: ignore.
1381 break;
1382
1383 case pch::SM_SLOC_FILE_ENTRY: {
1384 // FIXME: We would really like to delay the creation of this
1385 // FileEntry until it is actually required, e.g., when producing
1386 // a diagnostic with a source location in this file.
1387 const FileEntry *File
1388 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1389 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001390 FileID ID = SourceMgr.createFileID(File,
1391 SourceLocation::getFromRawEncoding(Record[1]),
1392 (CharacteristicKind)Record[2]);
1393 if (Record[3])
1394 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1395 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001396 break;
1397 }
1398
1399 case pch::SM_SLOC_BUFFER_ENTRY: {
1400 const char *Name = BlobStart;
1401 unsigned Code = Stream.ReadCode();
1402 Record.clear();
1403 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1404 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001405 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001406 llvm::MemoryBuffer *Buffer
1407 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1408 BlobStart + BlobLen - 1,
1409 Name);
1410 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1411
1412 if (strcmp(Name, "<built-in>") == 0
1413 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1414 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001415 break;
1416 }
1417
1418 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1419 SourceLocation SpellingLoc
1420 = SourceLocation::getFromRawEncoding(Record[1]);
1421 SourceMgr.createInstantiationLoc(
1422 SpellingLoc,
1423 SourceLocation::getFromRawEncoding(Record[2]),
1424 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001425 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001426 break;
1427 }
1428
Chris Lattnere1be6022009-04-14 23:22:57 +00001429 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001430 if (ParseLineTable(SourceMgr, Record))
1431 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001432 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001433 }
1434 }
1435}
1436
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001437void PCHReader::ReadMacroRecord(uint64_t Offset) {
1438 // Keep track of where we are in the stream, then jump back there
1439 // after reading this macro.
1440 SavedStreamPosition SavedPosition(Stream);
1441
1442 Stream.JumpToBit(Offset);
1443 RecordData Record;
1444 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1445 MacroInfo *Macro = 0;
1446 while (true) {
1447 unsigned Code = Stream.ReadCode();
1448 switch (Code) {
1449 case llvm::bitc::END_BLOCK:
1450 return;
1451
1452 case llvm::bitc::ENTER_SUBBLOCK:
1453 // No known subblocks, always skip them.
1454 Stream.ReadSubBlockID();
1455 if (Stream.SkipBlock()) {
1456 Error("Malformed block record");
1457 return;
1458 }
1459 continue;
1460
1461 case llvm::bitc::DEFINE_ABBREV:
1462 Stream.ReadAbbrevRecord();
1463 continue;
1464 default: break;
1465 }
1466
1467 // Read a record.
1468 Record.clear();
1469 pch::PreprocessorRecordTypes RecType =
1470 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1471 switch (RecType) {
1472 case pch::PP_COUNTER_VALUE:
1473 // Skip this record.
1474 break;
1475
1476 case pch::PP_MACRO_OBJECT_LIKE:
1477 case pch::PP_MACRO_FUNCTION_LIKE: {
1478 // If we already have a macro, that means that we've hit the end
1479 // of the definition of the macro we were looking for. We're
1480 // done.
1481 if (Macro)
1482 return;
1483
1484 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1485 if (II == 0) {
1486 Error("Macro must have a name");
1487 return;
1488 }
1489 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1490 bool isUsed = Record[2];
1491
1492 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1493 MI->setIsUsed(isUsed);
1494
1495 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1496 // Decode function-like macro info.
1497 bool isC99VarArgs = Record[3];
1498 bool isGNUVarArgs = Record[4];
1499 MacroArgs.clear();
1500 unsigned NumArgs = Record[5];
1501 for (unsigned i = 0; i != NumArgs; ++i)
1502 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1503
1504 // Install function-like macro info.
1505 MI->setIsFunctionLike();
1506 if (isC99VarArgs) MI->setIsC99Varargs();
1507 if (isGNUVarArgs) MI->setIsGNUVarargs();
1508 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1509 PP.getPreprocessorAllocator());
1510 }
1511
1512 // Finally, install the macro.
1513 PP.setMacroInfo(II, MI);
1514
1515 // Remember that we saw this macro last so that we add the tokens that
1516 // form its body to it.
1517 Macro = MI;
1518 ++NumMacrosRead;
1519 break;
1520 }
1521
1522 case pch::PP_TOKEN: {
1523 // If we see a TOKEN before a PP_MACRO_*, then the file is
1524 // erroneous, just pretend we didn't see this.
1525 if (Macro == 0) break;
1526
1527 Token Tok;
1528 Tok.startToken();
1529 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1530 Tok.setLength(Record[1]);
1531 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1532 Tok.setIdentifierInfo(II);
1533 Tok.setKind((tok::TokenKind)Record[3]);
1534 Tok.setFlag((Token::TokenFlags)Record[4]);
1535 Macro->AddTokenToBody(Tok);
1536 break;
1537 }
1538 }
1539 }
1540}
1541
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001542bool PCHReader::ReadPreprocessorBlock() {
1543 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1544 return Error("Malformed preprocessor block record");
1545
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001546 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001547 while (true) {
1548 unsigned Code = Stream.ReadCode();
1549 switch (Code) {
1550 case llvm::bitc::END_BLOCK:
1551 if (Stream.ReadBlockEnd())
1552 return Error("Error at end of preprocessor block");
1553 return false;
1554
1555 case llvm::bitc::ENTER_SUBBLOCK:
1556 // No known subblocks, always skip them.
1557 Stream.ReadSubBlockID();
1558 if (Stream.SkipBlock())
1559 return Error("Malformed block record");
1560 continue;
1561
1562 case llvm::bitc::DEFINE_ABBREV:
1563 Stream.ReadAbbrevRecord();
1564 continue;
1565 default: break;
1566 }
1567
1568 // Read a record.
1569 Record.clear();
1570 pch::PreprocessorRecordTypes RecType =
1571 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1572 switch (RecType) {
1573 default: // Default behavior: ignore unknown records.
1574 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001575 case pch::PP_COUNTER_VALUE:
1576 if (!Record.empty())
1577 PP.setCounterValue(Record[0]);
1578 break;
1579
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001580 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001581 case pch::PP_MACRO_FUNCTION_LIKE:
1582 case pch::PP_TOKEN:
1583 // Once we've hit a macro definition or a token, we're done.
1584 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001585 }
1586 }
1587}
1588
Douglas Gregorc713da92009-04-21 22:25:48 +00001589PCHReader::PCHReadResult
1590PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001591 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1592 Error("Malformed block record");
1593 return Failure;
1594 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001595
1596 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001597 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001598 while (!Stream.AtEndOfStream()) {
1599 unsigned Code = Stream.ReadCode();
1600 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001601 if (Stream.ReadBlockEnd()) {
1602 Error("Error at end of module block");
1603 return Failure;
1604 }
Chris Lattner29241862009-04-11 21:15:38 +00001605
Douglas Gregor179cfb12009-04-10 20:39:37 +00001606 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001607 }
1608
1609 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1610 switch (Stream.ReadSubBlockID()) {
1611 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1612 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1613 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001614 if (Stream.SkipBlock()) {
1615 Error("Malformed block record");
1616 return Failure;
1617 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001618 break;
1619
Chris Lattner29241862009-04-11 21:15:38 +00001620 case pch::PREPROCESSOR_BLOCK_ID:
1621 // Skip the preprocessor block for now, but remember where it is. We
1622 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001623 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001624 Error("Multiple preprocessor blocks found.");
1625 return Failure;
1626 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001627 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001628 if (Stream.SkipBlock()) {
1629 Error("Malformed block record");
1630 return Failure;
1631 }
1632 break;
1633
Douglas Gregorab1cef72009-04-10 03:52:48 +00001634 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001635 switch (ReadSourceManagerBlock()) {
1636 case Success:
1637 break;
1638
1639 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001640 Error("Malformed source manager block");
1641 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001642
1643 case IgnorePCH:
1644 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001645 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001646 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001647 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001648 continue;
1649 }
1650
1651 if (Code == llvm::bitc::DEFINE_ABBREV) {
1652 Stream.ReadAbbrevRecord();
1653 continue;
1654 }
1655
1656 // Read and process a record.
1657 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001658 const char *BlobStart = 0;
1659 unsigned BlobLen = 0;
1660 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1661 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001662 default: // Default behavior: ignore.
1663 break;
1664
1665 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001666 if (!TypeOffsets.empty()) {
1667 Error("Duplicate TYPE_OFFSET record in PCH file");
1668 return Failure;
1669 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001670 TypeOffsets.swap(Record);
1671 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1672 break;
1673
1674 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001675 if (!DeclOffsets.empty()) {
1676 Error("Duplicate DECL_OFFSET record in PCH file");
1677 return Failure;
1678 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001679 DeclOffsets.swap(Record);
1680 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1681 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001682
1683 case pch::LANGUAGE_OPTIONS:
1684 if (ParseLanguageOptions(Record))
1685 return IgnorePCH;
1686 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001687
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001688 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001689 std::string TargetTriple(BlobStart, BlobLen);
1690 if (TargetTriple != Context.Target.getTargetTriple()) {
1691 Diag(diag::warn_pch_target_triple)
1692 << TargetTriple << Context.Target.getTargetTriple();
1693 Diag(diag::note_ignoring_pch) << FileName;
1694 return IgnorePCH;
1695 }
1696 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001697 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001698
1699 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001700 IdentifierTableData = BlobStart;
1701 IdentifierLookupTable
1702 = PCHIdentifierLookupTable::Create(
1703 (const unsigned char *)IdentifierTableData + Record[0],
1704 (const unsigned char *)IdentifierTableData,
1705 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001706 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001707 break;
1708
1709 case pch::IDENTIFIER_OFFSET:
1710 if (!IdentifierData.empty()) {
1711 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1712 return Failure;
1713 }
1714 IdentifierData.swap(Record);
1715#ifndef NDEBUG
1716 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1717 if ((IdentifierData[I] & 0x01) == 0) {
1718 Error("Malformed identifier table in the precompiled header");
1719 return Failure;
1720 }
1721 }
1722#endif
1723 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001724
1725 case pch::EXTERNAL_DEFINITIONS:
1726 if (!ExternalDefinitions.empty()) {
1727 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1728 return Failure;
1729 }
1730 ExternalDefinitions.swap(Record);
1731 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001732
Douglas Gregore01ad442009-04-18 05:55:16 +00001733 case pch::SPECIAL_TYPES:
1734 SpecialTypes.swap(Record);
1735 break;
1736
Douglas Gregor456e0952009-04-17 22:13:46 +00001737 case pch::STATISTICS:
1738 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001739 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001740 TotalLexicalDeclContexts = Record[2];
1741 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001742 break;
1743
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001744 case pch::TENTATIVE_DEFINITIONS:
1745 if (!TentativeDefinitions.empty()) {
1746 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1747 return Failure;
1748 }
1749 TentativeDefinitions.swap(Record);
1750 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001751
1752 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1753 if (!LocallyScopedExternalDecls.empty()) {
1754 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1755 return Failure;
1756 }
1757 LocallyScopedExternalDecls.swap(Record);
1758 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001759 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001760 }
1761
Douglas Gregor179cfb12009-04-10 20:39:37 +00001762 Error("Premature end of bitstream");
1763 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001764}
1765
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001766PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001767 // Set the PCH file name.
1768 this->FileName = FileName;
1769
Douglas Gregorc34897d2009-04-09 22:27:44 +00001770 // Open the PCH file.
1771 std::string ErrStr;
1772 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001773 if (!Buffer) {
1774 Error(ErrStr.c_str());
1775 return IgnorePCH;
1776 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001777
1778 // Initialize the stream
1779 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1780 (const unsigned char *)Buffer->getBufferEnd());
1781
1782 // Sniff for the signature.
1783 if (Stream.Read(8) != 'C' ||
1784 Stream.Read(8) != 'P' ||
1785 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001786 Stream.Read(8) != 'H') {
1787 Error("Not a PCH file");
1788 return IgnorePCH;
1789 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001790
1791 // We expect a number of well-defined blocks, though we don't necessarily
1792 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001793 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001794 while (!Stream.AtEndOfStream()) {
1795 unsigned Code = Stream.ReadCode();
1796
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001797 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1798 Error("Invalid record at top-level");
1799 return Failure;
1800 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001801
1802 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001803
Douglas Gregorc34897d2009-04-09 22:27:44 +00001804 // We only know the PCH subblock ID.
1805 switch (BlockID) {
1806 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001807 if (Stream.ReadBlockInfoBlock()) {
1808 Error("Malformed BlockInfoBlock");
1809 return Failure;
1810 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001811 break;
1812 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001813 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001814 case Success:
1815 break;
1816
1817 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001818 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001819
1820 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001821 // FIXME: We could consider reading through to the end of this
1822 // PCH block, skipping subblocks, to see if there are other
1823 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001824 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001825 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001826 break;
1827 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001828 if (Stream.SkipBlock()) {
1829 Error("Malformed block record");
1830 return Failure;
1831 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001832 break;
1833 }
1834 }
1835
1836 // Load the translation unit declaration
1837 ReadDeclRecord(DeclOffsets[0], 0);
1838
Douglas Gregorc713da92009-04-21 22:25:48 +00001839 // Initialization of builtins and library builtins occurs before the
1840 // PCH file is read, so there may be some identifiers that were
1841 // loaded into the IdentifierTable before we intercepted the
1842 // creation of identifiers. Iterate through the list of known
1843 // identifiers and determine whether we have to establish
1844 // preprocessor definitions or top-level identifier declaration
1845 // chains for those identifiers.
1846 //
1847 // We copy the IdentifierInfo pointers to a small vector first,
1848 // since de-serializing declarations or macro definitions can add
1849 // new entries into the identifier table, invalidating the
1850 // iterators.
1851 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1852 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1853 IdEnd = PP.getIdentifierTable().end();
1854 Id != IdEnd; ++Id)
1855 Identifiers.push_back(Id->second);
1856 PCHIdentifierLookupTable *IdTable
1857 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1858 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1859 IdentifierInfo *II = Identifiers[I];
1860 // Look in the on-disk hash table for an entry for
1861 PCHIdentifierLookupTrait Info(*this, II);
1862 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1863 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1864 if (Pos == IdTable->end())
1865 continue;
1866
1867 // Dereferencing the iterator has the effect of populating the
1868 // IdentifierInfo node with the various declarations it needs.
1869 (void)*Pos;
1870 }
1871
Douglas Gregore01ad442009-04-18 05:55:16 +00001872 // Load the special types.
1873 Context.setBuiltinVaListType(
1874 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1875
Douglas Gregorc713da92009-04-21 22:25:48 +00001876 // If we saw the preprocessor block, read it now.
1877 if (PreprocessorBlockOffset) {
1878 SavedStreamPosition SavedPos(Stream);
1879 Stream.JumpToBit(PreprocessorBlockOffset);
1880 if (ReadPreprocessorBlock()) {
1881 Error("Malformed preprocessor block");
1882 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001883 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001884 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001885
Douglas Gregorc713da92009-04-21 22:25:48 +00001886 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001887}
1888
Douglas Gregor179cfb12009-04-10 20:39:37 +00001889/// \brief Parse the record that corresponds to a LangOptions data
1890/// structure.
1891///
1892/// This routine compares the language options used to generate the
1893/// PCH file against the language options set for the current
1894/// compilation. For each option, we classify differences between the
1895/// two compiler states as either "benign" or "important". Benign
1896/// differences don't matter, and we accept them without complaint
1897/// (and without modifying the language options). Differences between
1898/// the states for important options cause the PCH file to be
1899/// unusable, so we emit a warning and return true to indicate that
1900/// there was an error.
1901///
1902/// \returns true if the PCH file is unacceptable, false otherwise.
1903bool PCHReader::ParseLanguageOptions(
1904 const llvm::SmallVectorImpl<uint64_t> &Record) {
1905 const LangOptions &LangOpts = Context.getLangOptions();
1906#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1907#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1908 if (Record[Idx] != LangOpts.Option) { \
1909 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1910 Diag(diag::note_ignoring_pch) << FileName; \
1911 return true; \
1912 } \
1913 ++Idx
1914
1915 unsigned Idx = 0;
1916 PARSE_LANGOPT_BENIGN(Trigraphs);
1917 PARSE_LANGOPT_BENIGN(BCPLComment);
1918 PARSE_LANGOPT_BENIGN(DollarIdents);
1919 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1920 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1921 PARSE_LANGOPT_BENIGN(ImplicitInt);
1922 PARSE_LANGOPT_BENIGN(Digraphs);
1923 PARSE_LANGOPT_BENIGN(HexFloats);
1924 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1925 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1926 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1927 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1928 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1929 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1930 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1931 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1932 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1933 PARSE_LANGOPT_BENIGN(PascalStrings);
1934 PARSE_LANGOPT_BENIGN(Boolean);
1935 PARSE_LANGOPT_BENIGN(WritableStrings);
1936 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1937 diag::warn_pch_lax_vector_conversions);
1938 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1939 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1940 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1941 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1942 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1943 diag::warn_pch_thread_safe_statics);
1944 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1945 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1946 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1947 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1948 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1949 diag::warn_pch_heinous_extensions);
1950 // FIXME: Most of the options below are benign if the macro wasn't
1951 // used. Unfortunately, this means that a PCH compiled without
1952 // optimization can't be used with optimization turned on, even
1953 // though the only thing that changes is whether __OPTIMIZE__ was
1954 // defined... but if __OPTIMIZE__ never showed up in the header, it
1955 // doesn't matter. We could consider making this some special kind
1956 // of check.
1957 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1958 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1959 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1960 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1961 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1962 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1963 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1964 Diag(diag::warn_pch_gc_mode)
1965 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1966 Diag(diag::note_ignoring_pch) << FileName;
1967 return true;
1968 }
1969 ++Idx;
1970 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1971 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1972#undef PARSE_LANGOPT_IRRELEVANT
1973#undef PARSE_LANGOPT_BENIGN
1974
1975 return false;
1976}
1977
Douglas Gregorc34897d2009-04-09 22:27:44 +00001978/// \brief Read and return the type at the given offset.
1979///
1980/// This routine actually reads the record corresponding to the type
1981/// at the given offset in the bitstream. It is a helper routine for
1982/// GetType, which deals with reading type IDs.
1983QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001984 // Keep track of where we are in the stream, then jump back there
1985 // after reading this type.
1986 SavedStreamPosition SavedPosition(Stream);
1987
Douglas Gregorc34897d2009-04-09 22:27:44 +00001988 Stream.JumpToBit(Offset);
1989 RecordData Record;
1990 unsigned Code = Stream.ReadCode();
1991 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001992 case pch::TYPE_EXT_QUAL: {
1993 assert(Record.size() == 3 &&
1994 "Incorrect encoding of extended qualifier type");
1995 QualType Base = GetType(Record[0]);
1996 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1997 unsigned AddressSpace = Record[2];
1998
1999 QualType T = Base;
2000 if (GCAttr != QualType::GCNone)
2001 T = Context.getObjCGCQualType(T, GCAttr);
2002 if (AddressSpace)
2003 T = Context.getAddrSpaceQualType(T, AddressSpace);
2004 return T;
2005 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002006
Douglas Gregorc34897d2009-04-09 22:27:44 +00002007 case pch::TYPE_FIXED_WIDTH_INT: {
2008 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2009 return Context.getFixedWidthIntType(Record[0], Record[1]);
2010 }
2011
2012 case pch::TYPE_COMPLEX: {
2013 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2014 QualType ElemType = GetType(Record[0]);
2015 return Context.getComplexType(ElemType);
2016 }
2017
2018 case pch::TYPE_POINTER: {
2019 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2020 QualType PointeeType = GetType(Record[0]);
2021 return Context.getPointerType(PointeeType);
2022 }
2023
2024 case pch::TYPE_BLOCK_POINTER: {
2025 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2026 QualType PointeeType = GetType(Record[0]);
2027 return Context.getBlockPointerType(PointeeType);
2028 }
2029
2030 case pch::TYPE_LVALUE_REFERENCE: {
2031 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2032 QualType PointeeType = GetType(Record[0]);
2033 return Context.getLValueReferenceType(PointeeType);
2034 }
2035
2036 case pch::TYPE_RVALUE_REFERENCE: {
2037 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2038 QualType PointeeType = GetType(Record[0]);
2039 return Context.getRValueReferenceType(PointeeType);
2040 }
2041
2042 case pch::TYPE_MEMBER_POINTER: {
2043 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2044 QualType PointeeType = GetType(Record[0]);
2045 QualType ClassType = GetType(Record[1]);
2046 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2047 }
2048
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002049 case pch::TYPE_CONSTANT_ARRAY: {
2050 QualType ElementType = GetType(Record[0]);
2051 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2052 unsigned IndexTypeQuals = Record[2];
2053 unsigned Idx = 3;
2054 llvm::APInt Size = ReadAPInt(Record, Idx);
2055 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2056 }
2057
2058 case pch::TYPE_INCOMPLETE_ARRAY: {
2059 QualType ElementType = GetType(Record[0]);
2060 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2061 unsigned IndexTypeQuals = Record[2];
2062 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2063 }
2064
2065 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002066 QualType ElementType = GetType(Record[0]);
2067 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2068 unsigned IndexTypeQuals = Record[2];
2069 return Context.getVariableArrayType(ElementType, ReadExpr(),
2070 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002071 }
2072
2073 case pch::TYPE_VECTOR: {
2074 if (Record.size() != 2) {
2075 Error("Incorrect encoding of vector type in PCH file");
2076 return QualType();
2077 }
2078
2079 QualType ElementType = GetType(Record[0]);
2080 unsigned NumElements = Record[1];
2081 return Context.getVectorType(ElementType, NumElements);
2082 }
2083
2084 case pch::TYPE_EXT_VECTOR: {
2085 if (Record.size() != 2) {
2086 Error("Incorrect encoding of extended vector type in PCH file");
2087 return QualType();
2088 }
2089
2090 QualType ElementType = GetType(Record[0]);
2091 unsigned NumElements = Record[1];
2092 return Context.getExtVectorType(ElementType, NumElements);
2093 }
2094
2095 case pch::TYPE_FUNCTION_NO_PROTO: {
2096 if (Record.size() != 1) {
2097 Error("Incorrect encoding of no-proto function type");
2098 return QualType();
2099 }
2100 QualType ResultType = GetType(Record[0]);
2101 return Context.getFunctionNoProtoType(ResultType);
2102 }
2103
2104 case pch::TYPE_FUNCTION_PROTO: {
2105 QualType ResultType = GetType(Record[0]);
2106 unsigned Idx = 1;
2107 unsigned NumParams = Record[Idx++];
2108 llvm::SmallVector<QualType, 16> ParamTypes;
2109 for (unsigned I = 0; I != NumParams; ++I)
2110 ParamTypes.push_back(GetType(Record[Idx++]));
2111 bool isVariadic = Record[Idx++];
2112 unsigned Quals = Record[Idx++];
2113 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2114 isVariadic, Quals);
2115 }
2116
2117 case pch::TYPE_TYPEDEF:
2118 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2119 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2120
2121 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002122 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002123
2124 case pch::TYPE_TYPEOF: {
2125 if (Record.size() != 1) {
2126 Error("Incorrect encoding of typeof(type) in PCH file");
2127 return QualType();
2128 }
2129 QualType UnderlyingType = GetType(Record[0]);
2130 return Context.getTypeOfType(UnderlyingType);
2131 }
2132
2133 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002134 assert(Record.size() == 1 && "Incorrect encoding of record type");
2135 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002136
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002137 case pch::TYPE_ENUM:
2138 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2139 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2140
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002141 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002142 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2143 return Context.getObjCInterfaceType(
2144 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002145
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002146 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2147 unsigned Idx = 0;
2148 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2149 unsigned NumProtos = Record[Idx++];
2150 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2151 for (unsigned I = 0; I != NumProtos; ++I)
2152 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2153 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2154 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002155
Chris Lattner9b9f2352009-04-22 06:40:03 +00002156 case pch::TYPE_OBJC_QUALIFIED_ID: {
2157 unsigned Idx = 0;
2158 unsigned NumProtos = Record[Idx++];
2159 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2160 for (unsigned I = 0; I != NumProtos; ++I)
2161 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2162 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2163 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002164 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002165 // Suppress a GCC warning
2166 return QualType();
2167}
2168
2169/// \brief Note that we have loaded the declaration with the given
2170/// Index.
2171///
2172/// This routine notes that this declaration has already been loaded,
2173/// so that future GetDecl calls will return this declaration rather
2174/// than trying to load a new declaration.
2175inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2176 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2177 DeclAlreadyLoaded[Index] = true;
2178 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2179}
2180
2181/// \brief Read the declaration at the given offset from the PCH file.
2182Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002183 // Keep track of where we are in the stream, then jump back there
2184 // after reading this declaration.
2185 SavedStreamPosition SavedPosition(Stream);
2186
Douglas Gregorc34897d2009-04-09 22:27:44 +00002187 Decl *D = 0;
2188 Stream.JumpToBit(Offset);
2189 RecordData Record;
2190 unsigned Code = Stream.ReadCode();
2191 unsigned Idx = 0;
2192 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002193
Douglas Gregorc34897d2009-04-09 22:27:44 +00002194 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002195 case pch::DECL_ATTR:
2196 case pch::DECL_CONTEXT_LEXICAL:
2197 case pch::DECL_CONTEXT_VISIBLE:
2198 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2199 break;
2200
Douglas Gregorc34897d2009-04-09 22:27:44 +00002201 case pch::DECL_TRANSLATION_UNIT:
2202 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002203 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002204 break;
2205
2206 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002207 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002208 break;
2209 }
2210
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002211 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002212 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002213 break;
2214 }
2215
Douglas Gregor982365e2009-04-13 21:20:57 +00002216 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002217 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2218 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002219 break;
2220 }
2221
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002222 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002223 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2224 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002225 break;
2226 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002227
2228 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002229 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2230 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002231 break;
2232 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002233
Steve Naroff79ea0e02009-04-20 15:06:07 +00002234 case pch::DECL_OBJC_METHOD: {
2235 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2236 Selector(), QualType(), 0);
2237 break;
2238 }
2239
Steve Naroff97b53bd2009-04-21 15:12:33 +00002240 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002241 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2242 break;
2243 }
2244
Steve Naroff97b53bd2009-04-21 15:12:33 +00002245 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002246 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2247 ObjCIvarDecl::None);
2248 break;
2249 }
2250
Steve Naroff97b53bd2009-04-21 15:12:33 +00002251 case pch::DECL_OBJC_PROTOCOL: {
2252 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2253 break;
2254 }
2255
2256 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2257 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2258 QualType(), 0);
2259 break;
2260 }
2261
2262 case pch::DECL_OBJC_CLASS: {
2263 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2264 break;
2265 }
2266
2267 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2268 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2269 break;
2270 }
2271
2272 case pch::DECL_OBJC_CATEGORY: {
2273 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2274 break;
2275 }
2276
2277 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002278 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002279 break;
2280 }
2281
2282 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002283 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002284 break;
2285 }
2286
2287 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
Douglas Gregorf4936c72009-04-23 03:51:49 +00002288 D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002289 break;
2290 }
2291
2292 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002293 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002294 break;
2295 }
2296
2297 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002298 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2299 SourceLocation(), 0,
2300 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002301 break;
2302 }
2303
Douglas Gregor982365e2009-04-13 21:20:57 +00002304 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002305 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2306 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002307 break;
2308 }
2309
Douglas Gregorc34897d2009-04-09 22:27:44 +00002310 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002311 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2312 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002313 break;
2314 }
2315
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002316 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002317 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2318 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002319 break;
2320 }
2321
2322 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002323 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002324 QualType(), QualType(), VarDecl::None,
2325 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002326 break;
2327 }
2328
Douglas Gregor2a491792009-04-13 22:49:25 +00002329 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002330 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002331 break;
2332 }
2333
2334 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002335 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002336 break;
2337 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002338 }
2339
Douglas Gregorc713da92009-04-21 22:25:48 +00002340 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002341 if (D) {
2342 LoadedDecl(Index, D);
2343 Reader.Visit(D);
2344 }
2345
Douglas Gregorc34897d2009-04-09 22:27:44 +00002346 // If this declaration is also a declaration context, get the
2347 // offsets for its tables of lexical and visible declarations.
2348 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2349 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2350 if (Offsets.first || Offsets.second) {
2351 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2352 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2353 DeclContextOffsets[DC] = Offsets;
2354 }
2355 }
2356 assert(Idx == Record.size());
2357
Douglas Gregor405b6432009-04-22 19:09:20 +00002358 if (Consumer) {
2359 // If we have deserialized a declaration that has a definition the
2360 // AST consumer might need to know about, notify the consumer
2361 // about that definition now.
2362 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2363 if (Var->isFileVarDecl() && Var->getInit()) {
2364 DeclGroupRef DG(Var);
2365 Consumer->HandleTopLevelDecl(DG);
2366 }
2367 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2368 if (Func->isThisDeclarationADefinition()) {
2369 DeclGroupRef DG(Func);
2370 Consumer->HandleTopLevelDecl(DG);
2371 }
2372 }
2373 }
2374
Douglas Gregorc34897d2009-04-09 22:27:44 +00002375 return D;
2376}
2377
Douglas Gregorac8f2802009-04-10 17:25:41 +00002378QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002379 unsigned Quals = ID & 0x07;
2380 unsigned Index = ID >> 3;
2381
2382 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2383 QualType T;
2384 switch ((pch::PredefinedTypeIDs)Index) {
2385 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2386 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2387 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2388
2389 case pch::PREDEF_TYPE_CHAR_U_ID:
2390 case pch::PREDEF_TYPE_CHAR_S_ID:
2391 // FIXME: Check that the signedness of CharTy is correct!
2392 T = Context.CharTy;
2393 break;
2394
2395 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2396 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2397 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2398 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2399 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2400 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2401 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2402 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2403 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2404 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2405 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2406 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2407 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2408 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2409 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2410 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2411 }
2412
2413 assert(!T.isNull() && "Unknown predefined type");
2414 return T.getQualifiedType(Quals);
2415 }
2416
2417 Index -= pch::NUM_PREDEF_TYPE_IDS;
2418 if (!TypeAlreadyLoaded[Index]) {
2419 // Load the type from the PCH file.
2420 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2421 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2422 TypeAlreadyLoaded[Index] = true;
2423 }
2424
2425 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2426}
2427
Douglas Gregorac8f2802009-04-10 17:25:41 +00002428Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002429 if (ID == 0)
2430 return 0;
2431
2432 unsigned Index = ID - 1;
2433 if (DeclAlreadyLoaded[Index])
2434 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2435
2436 // Load the declaration from the PCH file.
2437 return ReadDeclRecord(DeclOffsets[Index], Index);
2438}
2439
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002440Stmt *PCHReader::GetStmt(uint64_t Offset) {
2441 // Keep track of where we are in the stream, then jump back there
2442 // after reading this declaration.
2443 SavedStreamPosition SavedPosition(Stream);
2444
2445 Stream.JumpToBit(Offset);
2446 return ReadStmt();
2447}
2448
Douglas Gregorc34897d2009-04-09 22:27:44 +00002449bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002450 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002451 assert(DC->hasExternalLexicalStorage() &&
2452 "DeclContext has no lexical decls in storage");
2453 uint64_t Offset = DeclContextOffsets[DC].first;
2454 assert(Offset && "DeclContext has no lexical decls in storage");
2455
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002456 // Keep track of where we are in the stream, then jump back there
2457 // after reading this context.
2458 SavedStreamPosition SavedPosition(Stream);
2459
Douglas Gregorc34897d2009-04-09 22:27:44 +00002460 // Load the record containing all of the declarations lexically in
2461 // this context.
2462 Stream.JumpToBit(Offset);
2463 RecordData Record;
2464 unsigned Code = Stream.ReadCode();
2465 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002466 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002467 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2468
2469 // Load all of the declaration IDs
2470 Decls.clear();
2471 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002472 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002473 return false;
2474}
2475
2476bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2477 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2478 assert(DC->hasExternalVisibleStorage() &&
2479 "DeclContext has no visible decls in storage");
2480 uint64_t Offset = DeclContextOffsets[DC].second;
2481 assert(Offset && "DeclContext has no visible decls in storage");
2482
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002483 // Keep track of where we are in the stream, then jump back there
2484 // after reading this context.
2485 SavedStreamPosition SavedPosition(Stream);
2486
Douglas Gregorc34897d2009-04-09 22:27:44 +00002487 // Load the record containing all of the declarations visible in
2488 // this context.
2489 Stream.JumpToBit(Offset);
2490 RecordData Record;
2491 unsigned Code = Stream.ReadCode();
2492 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002493 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002494 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2495 if (Record.size() == 0)
2496 return false;
2497
2498 Decls.clear();
2499
2500 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002501 while (Idx < Record.size()) {
2502 Decls.push_back(VisibleDeclaration());
2503 Decls.back().Name = ReadDeclarationName(Record, Idx);
2504
Douglas Gregorc34897d2009-04-09 22:27:44 +00002505 unsigned Size = Record[Idx++];
2506 llvm::SmallVector<unsigned, 4> & LoadedDecls
2507 = Decls.back().Declarations;
2508 LoadedDecls.reserve(Size);
2509 for (unsigned I = 0; I < Size; ++I)
2510 LoadedDecls.push_back(Record[Idx++]);
2511 }
2512
Douglas Gregoraf136d92009-04-22 22:34:57 +00002513 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002514 return false;
2515}
2516
Douglas Gregor631f6c62009-04-14 00:24:19 +00002517void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002518 this->Consumer = Consumer;
2519
Douglas Gregor631f6c62009-04-14 00:24:19 +00002520 if (!Consumer)
2521 return;
2522
2523 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2524 Decl *D = GetDecl(ExternalDefinitions[I]);
2525 DeclGroupRef DG(D);
2526 Consumer->HandleTopLevelDecl(DG);
2527 }
2528}
2529
Douglas Gregorc34897d2009-04-09 22:27:44 +00002530void PCHReader::PrintStats() {
2531 std::fprintf(stderr, "*** PCH Statistics:\n");
2532
2533 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2534 TypeAlreadyLoaded.end(),
2535 true);
2536 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2537 DeclAlreadyLoaded.end(),
2538 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002539 unsigned NumIdentifiersLoaded = 0;
2540 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2541 if ((IdentifierData[I] & 0x01) == 0)
2542 ++NumIdentifiersLoaded;
2543 }
2544
Douglas Gregorc34897d2009-04-09 22:27:44 +00002545 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2546 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002547 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002548 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2549 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002550 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2551 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2552 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2553 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002554 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2555 NumStatementsRead, TotalNumStatements,
2556 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002557 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2558 NumMacrosRead, TotalNumMacros,
2559 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002560 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2561 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2562 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2563 * 100));
2564 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2565 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2566 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2567 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002568 std::fprintf(stderr, "\n");
2569}
2570
Douglas Gregorc713da92009-04-21 22:25:48 +00002571void PCHReader::InitializeSema(Sema &S) {
2572 SemaObj = &S;
2573
Douglas Gregor2554cf22009-04-22 21:15:06 +00002574 // Makes sure any declarations that were deserialized "too early"
2575 // still get added to the identifier's declaration chains.
2576 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2577 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2578 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002579 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002580 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002581
2582 // If there were any tentative definitions, deserialize them and add
2583 // them to Sema's table of tentative definitions.
2584 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2585 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2586 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2587 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002588
2589 // If there were any locally-scoped external declarations,
2590 // deserialize them and add them to Sema's table of locally-scoped
2591 // external declarations.
2592 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2593 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2594 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2595 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002596}
2597
2598IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2599 // Try to find this name within our on-disk hash table
2600 PCHIdentifierLookupTable *IdTable
2601 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2602 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2603 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2604 if (Pos == IdTable->end())
2605 return 0;
2606
2607 // Dereferencing the iterator has the effect of building the
2608 // IdentifierInfo node and populating it with the various
2609 // declarations it needs.
2610 return *Pos;
2611}
2612
2613void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2614 assert(ID && "Non-zero identifier ID required");
2615 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2616}
2617
Chris Lattner29241862009-04-11 21:15:38 +00002618IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002619 if (ID == 0)
2620 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002621
Douglas Gregorc713da92009-04-21 22:25:48 +00002622 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002623 Error("No identifier table in PCH file");
2624 return 0;
2625 }
Chris Lattner29241862009-04-11 21:15:38 +00002626
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002627 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002628 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002629 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002630 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002631 }
Chris Lattner29241862009-04-11 21:15:38 +00002632
2633 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002634}
2635
2636DeclarationName
2637PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2638 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2639 switch (Kind) {
2640 case DeclarationName::Identifier:
2641 return DeclarationName(GetIdentifierInfo(Record, Idx));
2642
2643 case DeclarationName::ObjCZeroArgSelector:
2644 case DeclarationName::ObjCOneArgSelector:
2645 case DeclarationName::ObjCMultiArgSelector:
2646 assert(false && "Unable to de-serialize Objective-C selectors");
2647 break;
2648
2649 case DeclarationName::CXXConstructorName:
2650 return Context.DeclarationNames.getCXXConstructorName(
2651 GetType(Record[Idx++]));
2652
2653 case DeclarationName::CXXDestructorName:
2654 return Context.DeclarationNames.getCXXDestructorName(
2655 GetType(Record[Idx++]));
2656
2657 case DeclarationName::CXXConversionFunctionName:
2658 return Context.DeclarationNames.getCXXConversionFunctionName(
2659 GetType(Record[Idx++]));
2660
2661 case DeclarationName::CXXOperatorName:
2662 return Context.DeclarationNames.getCXXOperatorName(
2663 (OverloadedOperatorKind)Record[Idx++]);
2664
2665 case DeclarationName::CXXUsingDirective:
2666 return DeclarationName::getUsingDirectiveName();
2667 }
2668
2669 // Required to silence GCC warning
2670 return DeclarationName();
2671}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002672
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002673/// \brief Read an integral value
2674llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2675 unsigned BitWidth = Record[Idx++];
2676 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2677 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2678 Idx += NumWords;
2679 return Result;
2680}
2681
2682/// \brief Read a signed integral value
2683llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2684 bool isUnsigned = Record[Idx++];
2685 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2686}
2687
Douglas Gregore2f37202009-04-14 21:55:33 +00002688/// \brief Read a floating-point value
2689llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002690 return llvm::APFloat(ReadAPInt(Record, Idx));
2691}
2692
Douglas Gregor1c507882009-04-15 21:30:51 +00002693// \brief Read a string
2694std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2695 unsigned Len = Record[Idx++];
2696 std::string Result(&Record[Idx], &Record[Idx] + Len);
2697 Idx += Len;
2698 return Result;
2699}
2700
2701/// \brief Reads attributes from the current stream position.
2702Attr *PCHReader::ReadAttributes() {
2703 unsigned Code = Stream.ReadCode();
2704 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2705 "Expected unabbreviated record"); (void)Code;
2706
2707 RecordData Record;
2708 unsigned Idx = 0;
2709 unsigned RecCode = Stream.ReadRecord(Code, Record);
2710 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2711 (void)RecCode;
2712
2713#define SIMPLE_ATTR(Name) \
2714 case Attr::Name: \
2715 New = ::new (Context) Name##Attr(); \
2716 break
2717
2718#define STRING_ATTR(Name) \
2719 case Attr::Name: \
2720 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2721 break
2722
2723#define UNSIGNED_ATTR(Name) \
2724 case Attr::Name: \
2725 New = ::new (Context) Name##Attr(Record[Idx++]); \
2726 break
2727
2728 Attr *Attrs = 0;
2729 while (Idx < Record.size()) {
2730 Attr *New = 0;
2731 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2732 bool IsInherited = Record[Idx++];
2733
2734 switch (Kind) {
2735 STRING_ATTR(Alias);
2736 UNSIGNED_ATTR(Aligned);
2737 SIMPLE_ATTR(AlwaysInline);
2738 SIMPLE_ATTR(AnalyzerNoReturn);
2739 STRING_ATTR(Annotate);
2740 STRING_ATTR(AsmLabel);
2741
2742 case Attr::Blocks:
2743 New = ::new (Context) BlocksAttr(
2744 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2745 break;
2746
2747 case Attr::Cleanup:
2748 New = ::new (Context) CleanupAttr(
2749 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2750 break;
2751
2752 SIMPLE_ATTR(Const);
2753 UNSIGNED_ATTR(Constructor);
2754 SIMPLE_ATTR(DLLExport);
2755 SIMPLE_ATTR(DLLImport);
2756 SIMPLE_ATTR(Deprecated);
2757 UNSIGNED_ATTR(Destructor);
2758 SIMPLE_ATTR(FastCall);
2759
2760 case Attr::Format: {
2761 std::string Type = ReadString(Record, Idx);
2762 unsigned FormatIdx = Record[Idx++];
2763 unsigned FirstArg = Record[Idx++];
2764 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2765 break;
2766 }
2767
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002768 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002769
2770 case Attr::IBOutletKind:
2771 New = ::new (Context) IBOutletAttr();
2772 break;
2773
2774 SIMPLE_ATTR(NoReturn);
2775 SIMPLE_ATTR(NoThrow);
2776 SIMPLE_ATTR(Nodebug);
2777 SIMPLE_ATTR(Noinline);
2778
2779 case Attr::NonNull: {
2780 unsigned Size = Record[Idx++];
2781 llvm::SmallVector<unsigned, 16> ArgNums;
2782 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2783 Idx += Size;
2784 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2785 break;
2786 }
2787
2788 SIMPLE_ATTR(ObjCException);
2789 SIMPLE_ATTR(ObjCNSObject);
2790 SIMPLE_ATTR(Overloadable);
2791 UNSIGNED_ATTR(Packed);
2792 SIMPLE_ATTR(Pure);
2793 UNSIGNED_ATTR(Regparm);
2794 STRING_ATTR(Section);
2795 SIMPLE_ATTR(StdCall);
2796 SIMPLE_ATTR(TransparentUnion);
2797 SIMPLE_ATTR(Unavailable);
2798 SIMPLE_ATTR(Unused);
2799 SIMPLE_ATTR(Used);
2800
2801 case Attr::Visibility:
2802 New = ::new (Context) VisibilityAttr(
2803 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2804 break;
2805
2806 SIMPLE_ATTR(WarnUnusedResult);
2807 SIMPLE_ATTR(Weak);
2808 SIMPLE_ATTR(WeakImport);
2809 }
2810
2811 assert(New && "Unable to decode attribute?");
2812 New->setInherited(IsInherited);
2813 New->setNext(Attrs);
2814 Attrs = New;
2815 }
2816#undef UNSIGNED_ATTR
2817#undef STRING_ATTR
2818#undef SIMPLE_ATTR
2819
2820 // The list of attributes was built backwards. Reverse the list
2821 // before returning it.
2822 Attr *PrevAttr = 0, *NextAttr = 0;
2823 while (Attrs) {
2824 NextAttr = Attrs->getNext();
2825 Attrs->setNext(PrevAttr);
2826 PrevAttr = Attrs;
2827 Attrs = NextAttr;
2828 }
2829
2830 return PrevAttr;
2831}
2832
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002833Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002834 // Within the bitstream, expressions are stored in Reverse Polish
2835 // Notation, with each of the subexpressions preceding the
2836 // expression they are stored in. To evaluate expressions, we
2837 // continue reading expressions and placing them on the stack, with
2838 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002839 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002840 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002841 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002842 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002843 llvm::SmallVector<Stmt *, 16> StmtStack;
2844 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002845 Stmt::EmptyShell Empty;
2846
Douglas Gregora151ba42009-04-14 23:32:43 +00002847 while (true) {
2848 unsigned Code = Stream.ReadCode();
2849 if (Code == llvm::bitc::END_BLOCK) {
2850 if (Stream.ReadBlockEnd()) {
2851 Error("Error at end of Source Manager block");
2852 return 0;
2853 }
2854 break;
2855 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002856
Douglas Gregora151ba42009-04-14 23:32:43 +00002857 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2858 // No known subblocks, always skip them.
2859 Stream.ReadSubBlockID();
2860 if (Stream.SkipBlock()) {
2861 Error("Malformed block record");
2862 return 0;
2863 }
2864 continue;
2865 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002866
Douglas Gregora151ba42009-04-14 23:32:43 +00002867 if (Code == llvm::bitc::DEFINE_ABBREV) {
2868 Stream.ReadAbbrevRecord();
2869 continue;
2870 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002871
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002872 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002873 Idx = 0;
2874 Record.clear();
2875 bool Finished = false;
2876 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002877 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002878 Finished = true;
2879 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002880
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002881 case pch::STMT_NULL_PTR:
2882 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002883 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002884
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002885 case pch::STMT_NULL:
2886 S = new (Context) NullStmt(Empty);
2887 break;
2888
2889 case pch::STMT_COMPOUND:
2890 S = new (Context) CompoundStmt(Empty);
2891 break;
2892
2893 case pch::STMT_CASE:
2894 S = new (Context) CaseStmt(Empty);
2895 break;
2896
2897 case pch::STMT_DEFAULT:
2898 S = new (Context) DefaultStmt(Empty);
2899 break;
2900
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002901 case pch::STMT_LABEL:
2902 S = new (Context) LabelStmt(Empty);
2903 break;
2904
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002905 case pch::STMT_IF:
2906 S = new (Context) IfStmt(Empty);
2907 break;
2908
2909 case pch::STMT_SWITCH:
2910 S = new (Context) SwitchStmt(Empty);
2911 break;
2912
Douglas Gregora6b503f2009-04-17 00:16:09 +00002913 case pch::STMT_WHILE:
2914 S = new (Context) WhileStmt(Empty);
2915 break;
2916
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002917 case pch::STMT_DO:
2918 S = new (Context) DoStmt(Empty);
2919 break;
2920
2921 case pch::STMT_FOR:
2922 S = new (Context) ForStmt(Empty);
2923 break;
2924
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002925 case pch::STMT_GOTO:
2926 S = new (Context) GotoStmt(Empty);
2927 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002928
2929 case pch::STMT_INDIRECT_GOTO:
2930 S = new (Context) IndirectGotoStmt(Empty);
2931 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002932
Douglas Gregora6b503f2009-04-17 00:16:09 +00002933 case pch::STMT_CONTINUE:
2934 S = new (Context) ContinueStmt(Empty);
2935 break;
2936
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002937 case pch::STMT_BREAK:
2938 S = new (Context) BreakStmt(Empty);
2939 break;
2940
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002941 case pch::STMT_RETURN:
2942 S = new (Context) ReturnStmt(Empty);
2943 break;
2944
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002945 case pch::STMT_DECL:
2946 S = new (Context) DeclStmt(Empty);
2947 break;
2948
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002949 case pch::STMT_ASM:
2950 S = new (Context) AsmStmt(Empty);
2951 break;
2952
Douglas Gregora151ba42009-04-14 23:32:43 +00002953 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002954 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002955 break;
2956
2957 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002958 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002959 break;
2960
2961 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002962 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002963 break;
2964
2965 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002966 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002967 break;
2968
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002969 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002970 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002971 break;
2972
Douglas Gregor596e0932009-04-15 16:35:07 +00002973 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002974 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002975 Record[PCHStmtReader::NumExprFields + 1]);
2976 break;
2977
Douglas Gregora151ba42009-04-14 23:32:43 +00002978 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002979 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002980 break;
2981
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002982 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002983 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002984 break;
2985
Douglas Gregor12d74052009-04-15 15:58:59 +00002986 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002987 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002988 break;
2989
2990 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002991 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002992 break;
2993
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002994 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002995 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002996 break;
2997
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002998 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002999 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003000 break;
3001
3002 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003003 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00003004 break;
3005
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003006 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003007 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003008 break;
3009
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003010 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003011 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003012 break;
3013
3014 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003015 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003016 break;
3017
Douglas Gregora151ba42009-04-14 23:32:43 +00003018 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003019 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003020 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003021
3022 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003023 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003024 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003025
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003026 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003027 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003028 break;
3029
Douglas Gregorec0b8292009-04-15 23:02:49 +00003030 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003031 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003032 break;
3033
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003034 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003035 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003036 break;
3037
3038 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003039 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003040 Record[PCHStmtReader::NumExprFields] - 1);
3041
3042 break;
3043
3044 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003045 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003046 break;
3047
Douglas Gregorec0b8292009-04-15 23:02:49 +00003048 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003049 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003050 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003051
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003052 case pch::EXPR_ADDR_LABEL:
3053 S = new (Context) AddrLabelExpr(Empty);
3054 break;
3055
Douglas Gregoreca12f62009-04-17 19:05:30 +00003056 case pch::EXPR_STMT:
3057 S = new (Context) StmtExpr(Empty);
3058 break;
3059
Douglas Gregor209d4622009-04-15 23:33:31 +00003060 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003061 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003062 break;
3063
3064 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003065 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003066 break;
3067
3068 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003069 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003070 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003071
3072 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003073 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003074 break;
3075
Douglas Gregore246b742009-04-17 19:21:43 +00003076 case pch::EXPR_BLOCK:
3077 S = new (Context) BlockExpr(Empty);
3078 break;
3079
Douglas Gregor725e94b2009-04-16 00:01:45 +00003080 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003081 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003082 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003083
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003084 case pch::EXPR_OBJC_STRING_LITERAL:
3085 S = new (Context) ObjCStringLiteral(Empty);
3086 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003087 case pch::EXPR_OBJC_ENCODE:
3088 S = new (Context) ObjCEncodeExpr(Empty);
3089 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003090 case pch::EXPR_OBJC_SELECTOR_EXPR:
3091 S = new (Context) ObjCSelectorExpr(Empty);
3092 break;
3093 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3094 S = new (Context) ObjCProtocolExpr(Empty);
3095 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003096 }
3097
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003098 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003099 if (Finished)
3100 break;
3101
Douglas Gregor456e0952009-04-17 22:13:46 +00003102 ++NumStatementsRead;
3103
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003104 if (S) {
3105 unsigned NumSubStmts = Reader.Visit(S);
3106 while (NumSubStmts > 0) {
3107 StmtStack.pop_back();
3108 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003109 }
3110 }
3111
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003112 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003113 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003114 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003115 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003116 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003117 return StmtStack.back();
3118}
3119
3120Expr *PCHReader::ReadExpr() {
3121 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003122}
3123
Douglas Gregor179cfb12009-04-10 20:39:37 +00003124DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003125 return Diag(SourceLocation(), DiagID);
3126}
3127
3128DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3129 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003130 Context.getSourceManager()),
3131 DiagID);
3132}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003133
Douglas Gregorc713da92009-04-21 22:25:48 +00003134/// \brief Retrieve the identifier table associated with the
3135/// preprocessor.
3136IdentifierTable &PCHReader::getIdentifierTable() {
3137 return PP.getIdentifierTable();
3138}
3139
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003140/// \brief Record that the given ID maps to the given switch-case
3141/// statement.
3142void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3143 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3144 SwitchCaseStmts[ID] = SC;
3145}
3146
3147/// \brief Retrieve the switch-case statement with the given ID.
3148SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3149 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3150 return SwitchCaseStmts[ID];
3151}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003152
3153/// \brief Record that the given label statement has been
3154/// deserialized and has the given ID.
3155void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3156 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3157 "Deserialized label twice");
3158 LabelStmts[ID] = S;
3159
3160 // If we've already seen any goto statements that point to this
3161 // label, resolve them now.
3162 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3163 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3164 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3165 Goto->second->setLabel(S);
3166 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003167
3168 // If we've already seen any address-label statements that point to
3169 // this label, resolve them now.
3170 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3171 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3172 = UnresolvedAddrLabelExprs.equal_range(ID);
3173 for (AddrLabelIter AddrLabel = AddrLabels.first;
3174 AddrLabel != AddrLabels.second; ++AddrLabel)
3175 AddrLabel->second->setLabel(S);
3176 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003177}
3178
3179/// \brief Set the label of the given statement to the label
3180/// identified by ID.
3181///
3182/// Depending on the order in which the label and other statements
3183/// referencing that label occur, this operation may complete
3184/// immediately (updating the statement) or it may queue the
3185/// statement to be back-patched later.
3186void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3187 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3188 if (Label != LabelStmts.end()) {
3189 // We've already seen this label, so set the label of the goto and
3190 // we're done.
3191 S->setLabel(Label->second);
3192 } else {
3193 // We haven't seen this label yet, so add this goto to the set of
3194 // unresolved goto statements.
3195 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3196 }
3197}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003198
3199/// \brief Set the label of the given expression to the label
3200/// identified by ID.
3201///
3202/// Depending on the order in which the label and other statements
3203/// referencing that label occur, this operation may complete
3204/// immediately (updating the statement) or it may queue the
3205/// statement to be back-patched later.
3206void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3207 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3208 if (Label != LabelStmts.end()) {
3209 // We've already seen this label, so set the label of the
3210 // label-address expression and we're done.
3211 S->setLabel(Label->second);
3212 } else {
3213 // We haven't seen this label yet, so add this label-address
3214 // expression to the set of unresolved label-address expressions.
3215 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3216 }
3217}