blob: 0791143a00c270afc1699d8c3f34a19f64d5bd0a [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++])));
Steve Naroff7333b492009-04-20 20:09:33 +0000230 unsigned NumIvars = Record[Idx++];
231 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
232 IVars.reserve(NumIvars);
233 for (unsigned I = 0; I != NumIvars; ++I)
234 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
235 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
236
237 ID->setForwardDecl(Record[Idx++]);
238 ID->setImplicitInterfaceDecl(Record[Idx++]);
239 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
240 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner80f83c62009-04-22 05:57:30 +0000241 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff7333b492009-04-20 20:09:33 +0000242 // FIXME: add protocols, categories.
243}
244
245void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
246 VisitFieldDecl(IVD);
247 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
248}
249
Steve Naroff97b53bd2009-04-21 15:12:33 +0000250void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
251 VisitObjCContainerDecl(PD);
252 PD->setForwardDecl(Record[Idx++]);
253 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
259 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
260}
261
262void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
263 VisitFieldDecl(FD);
264}
265
266void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
267 VisitDecl(CD);
268 unsigned NumClassRefs = Record[Idx++];
269 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
270 ClassRefs.reserve(NumClassRefs);
271 for (unsigned I = 0; I != NumClassRefs; ++I)
272 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
273 CD->setClassList(Reader.getContext(), &ClassRefs[0], NumClassRefs);
274}
275
276void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
277 VisitDecl(FPD);
278 unsigned NumProtoRefs = Record[Idx++];
279 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
280 ProtoRefs.reserve(NumProtoRefs);
281 for (unsigned I = 0; I != NumProtoRefs; ++I)
282 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
283 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
284}
285
286void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
287 VisitObjCContainerDecl(CD);
288 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
289 unsigned NumProtoRefs = Record[Idx++];
290 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
291 ProtoRefs.reserve(NumProtoRefs);
292 for (unsigned I = 0; I != NumProtoRefs; ++I)
293 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
294 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, Reader.getContext());
295 CD->setNextClassCategory(cast<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
296 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
297}
298
299void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
300 VisitNamedDecl(CAD);
301 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
302}
303
304void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
305 VisitNamedDecl(D);
Douglas Gregor3839f1c2009-04-22 23:20:34 +0000306 D->setType(Reader.GetType(Record[Idx++]));
307 // FIXME: stable encoding
308 D->setPropertyAttributes(
309 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
310 // FIXME: stable encoding
311 D->setPropertyImplementation(
312 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
313 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
314 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
315 D->setGetterMethodDecl(
316 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
317 D->setSetterMethodDecl(
318 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
319 D->setPropertyIvarDecl(
320 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000321}
322
323void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
324 VisitDecl(D);
Douglas Gregorbd336c52009-04-23 02:42:49 +0000325 D->setClassInterface(
326 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000328}
329
330void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
331 VisitObjCImplDecl(D);
Douglas Gregor58e7ce42009-04-23 02:53:57 +0000332 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000333}
334
335void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
336 VisitObjCImplDecl(D);
Douglas Gregor087dbf32009-04-23 03:23:08 +0000337 D->setSuperClass(
338 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000339}
340
341
342void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
343 VisitDecl(D);
Douglas Gregor3f2c5052009-04-23 03:43:53 +0000344 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
345 D->setPropertyDecl(
346 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
347 D->setPropertyIvarDecl(
348 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Steve Naroff97b53bd2009-04-21 15:12:33 +0000349}
350
Douglas Gregor982365e2009-04-13 21:20:57 +0000351void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
352 VisitValueDecl(FD);
353 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000354 if (Record[Idx++])
355 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000356}
357
Douglas Gregorc34897d2009-04-09 22:27:44 +0000358void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
359 VisitValueDecl(VD);
360 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
361 VD->setThreadSpecified(Record[Idx++]);
362 VD->setCXXDirectInitializer(Record[Idx++]);
363 VD->setDeclaredInCondition(Record[Idx++]);
364 VD->setPreviousDeclaration(
365 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
366 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000367 if (Record[Idx++])
368 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000369}
370
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000371void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
372 VisitVarDecl(PD);
373 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000374 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000375}
376
377void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
378 VisitParmVarDecl(PD);
379 PD->setOriginalType(Reader.GetType(Record[Idx++]));
380}
381
Douglas Gregor2a491792009-04-13 22:49:25 +0000382void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
383 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000384 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000385}
386
387void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
388 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000389 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000390 unsigned NumParams = Record[Idx++];
391 llvm::SmallVector<ParmVarDecl *, 16> Params;
392 Params.reserve(NumParams);
393 for (unsigned I = 0; I != NumParams; ++I)
394 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
395 BD->setParams(Reader.getContext(), &Params[0], NumParams);
396}
397
Douglas Gregorc34897d2009-04-09 22:27:44 +0000398std::pair<uint64_t, uint64_t>
399PCHDeclReader::VisitDeclContext(DeclContext *DC) {
400 uint64_t LexicalOffset = Record[Idx++];
Douglas Gregor405b6432009-04-22 19:09:20 +0000401 uint64_t VisibleOffset = Record[Idx++];
Douglas Gregorc34897d2009-04-09 22:27:44 +0000402 return std::make_pair(LexicalOffset, VisibleOffset);
403}
404
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000405//===----------------------------------------------------------------------===//
406// Statement/expression deserialization
407//===----------------------------------------------------------------------===//
408namespace {
409 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000410 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000411 PCHReader &Reader;
412 const PCHReader::RecordData &Record;
413 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000414 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000415
416 public:
417 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000418 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
419 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000420
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000421 /// \brief The number of record fields required for the Stmt class
422 /// itself.
423 static const unsigned NumStmtFields = 0;
424
Douglas Gregor596e0932009-04-15 16:35:07 +0000425 /// \brief The number of record fields required for the Expr class
426 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000427 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000428
Douglas Gregora151ba42009-04-14 23:32:43 +0000429 // Each of the Visit* functions reads in part of the expression
430 // from the given record and the current expression stack, then
431 // return the total number of operands that it read from the
432 // expression stack.
433
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000434 unsigned VisitStmt(Stmt *S);
435 unsigned VisitNullStmt(NullStmt *S);
436 unsigned VisitCompoundStmt(CompoundStmt *S);
437 unsigned VisitSwitchCase(SwitchCase *S);
438 unsigned VisitCaseStmt(CaseStmt *S);
439 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000440 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000441 unsigned VisitIfStmt(IfStmt *S);
442 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000443 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000444 unsigned VisitDoStmt(DoStmt *S);
445 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000446 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000447 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000448 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000449 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000450 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000451 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000452 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000453 unsigned VisitExpr(Expr *E);
454 unsigned VisitPredefinedExpr(PredefinedExpr *E);
455 unsigned VisitDeclRefExpr(DeclRefExpr *E);
456 unsigned VisitIntegerLiteral(IntegerLiteral *E);
457 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000458 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000459 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000460 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000461 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000462 unsigned VisitUnaryOperator(UnaryOperator *E);
463 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000464 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000465 unsigned VisitCallExpr(CallExpr *E);
466 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000467 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000468 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000469 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
470 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000471 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000472 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
473 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000474 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000475 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000476 unsigned VisitInitListExpr(InitListExpr *E);
477 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
478 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000479 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000480 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000481 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000482 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
483 unsigned VisitChooseExpr(ChooseExpr *E);
484 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000485 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000486 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000487 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000488 unsigned VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000489 unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000490 unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E);
491 unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000492 };
493}
494
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000495unsigned PCHStmtReader::VisitStmt(Stmt *S) {
496 assert(Idx == NumStmtFields && "Incorrect statement field count");
497 return 0;
498}
499
500unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
501 VisitStmt(S);
502 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
503 return 0;
504}
505
506unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
507 VisitStmt(S);
508 unsigned NumStmts = Record[Idx++];
509 S->setStmts(Reader.getContext(),
510 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
511 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
512 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
513 return NumStmts;
514}
515
516unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
517 VisitStmt(S);
518 Reader.RecordSwitchCaseID(S, Record[Idx++]);
519 return 0;
520}
521
522unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
523 VisitSwitchCase(S);
524 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
525 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
526 S->setSubStmt(StmtStack.back());
527 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
528 return 3;
529}
530
531unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
532 VisitSwitchCase(S);
533 S->setSubStmt(StmtStack.back());
534 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
535 return 1;
536}
537
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000538unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
539 VisitStmt(S);
540 S->setID(Reader.GetIdentifierInfo(Record, Idx));
541 S->setSubStmt(StmtStack.back());
542 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
543 Reader.RecordLabelStmt(S, Record[Idx++]);
544 return 1;
545}
546
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000547unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
548 VisitStmt(S);
549 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
550 S->setThen(StmtStack[StmtStack.size() - 2]);
551 S->setElse(StmtStack[StmtStack.size() - 1]);
552 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
553 return 3;
554}
555
556unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
557 VisitStmt(S);
558 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
559 S->setBody(StmtStack.back());
560 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
561 SwitchCase *PrevSC = 0;
562 for (unsigned N = Record.size(); Idx != N; ++Idx) {
563 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
564 if (PrevSC)
565 PrevSC->setNextSwitchCase(SC);
566 else
567 S->setSwitchCaseList(SC);
568 PrevSC = SC;
569 }
570 return 2;
571}
572
Douglas Gregora6b503f2009-04-17 00:16:09 +0000573unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
574 VisitStmt(S);
575 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
576 S->setBody(StmtStack.back());
577 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
578 return 2;
579}
580
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000581unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
582 VisitStmt(S);
583 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
584 S->setBody(StmtStack.back());
585 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
586 return 2;
587}
588
589unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
590 VisitStmt(S);
591 S->setInit(StmtStack[StmtStack.size() - 4]);
592 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
593 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
594 S->setBody(StmtStack.back());
595 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
596 return 4;
597}
598
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000599unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
600 VisitStmt(S);
601 Reader.SetLabelOf(S, Record[Idx++]);
602 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 0;
605}
606
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000607unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
608 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000609 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000610 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
611 return 1;
612}
613
Douglas Gregora6b503f2009-04-17 00:16:09 +0000614unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
615 VisitStmt(S);
616 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
617 return 0;
618}
619
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000620unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
621 VisitStmt(S);
622 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 return 0;
624}
625
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000626unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
627 VisitStmt(S);
628 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
629 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630 return 1;
631}
632
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000633unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
634 VisitStmt(S);
635 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
636 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637
638 if (Idx + 1 == Record.size()) {
639 // Single declaration
640 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
641 } else {
642 llvm::SmallVector<Decl *, 16> Decls;
643 Decls.reserve(Record.size() - Idx);
644 for (unsigned N = Record.size(); Idx != N; ++Idx)
645 Decls.push_back(Reader.GetDecl(Record[Idx]));
646 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
647 &Decls[0], Decls.size())));
648 }
649 return 0;
650}
651
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000652unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
653 VisitStmt(S);
654 unsigned NumOutputs = Record[Idx++];
655 unsigned NumInputs = Record[Idx++];
656 unsigned NumClobbers = Record[Idx++];
657 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
658 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
659 S->setVolatile(Record[Idx++]);
660 S->setSimple(Record[Idx++]);
661
662 unsigned StackIdx
663 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
664 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
665
666 // Outputs and inputs
667 llvm::SmallVector<std::string, 16> Names;
668 llvm::SmallVector<StringLiteral*, 16> Constraints;
669 llvm::SmallVector<Stmt*, 16> Exprs;
670 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
671 Names.push_back(Reader.ReadString(Record, Idx));
672 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
673 Exprs.push_back(StmtStack[StackIdx++]);
674 }
675 S->setOutputsAndInputs(NumOutputs, NumInputs,
676 &Names[0], &Constraints[0], &Exprs[0]);
677
678 // Constraints
679 llvm::SmallVector<StringLiteral*, 16> Clobbers;
680 for (unsigned I = 0; I != NumClobbers; ++I)
681 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
682 S->setClobbers(&Clobbers[0], NumClobbers);
683
684 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
685 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
686}
687
Douglas Gregora151ba42009-04-14 23:32:43 +0000688unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000689 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000690 E->setType(Reader.GetType(Record[Idx++]));
691 E->setTypeDependent(Record[Idx++]);
692 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000693 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000694 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000695}
696
Douglas Gregora151ba42009-04-14 23:32:43 +0000697unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000698 VisitExpr(E);
699 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
700 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000701 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000702}
703
Douglas Gregora151ba42009-04-14 23:32:43 +0000704unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000705 VisitExpr(E);
706 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
707 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000708 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000709}
710
Douglas Gregora151ba42009-04-14 23:32:43 +0000711unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000712 VisitExpr(E);
713 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
714 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000715 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000716}
717
Douglas Gregora151ba42009-04-14 23:32:43 +0000718unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000719 VisitExpr(E);
720 E->setValue(Reader.ReadAPFloat(Record, Idx));
721 E->setExact(Record[Idx++]);
722 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000723 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000724}
725
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000726unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
727 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000728 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000729 return 1;
730}
731
Douglas Gregor596e0932009-04-15 16:35:07 +0000732unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
733 VisitExpr(E);
734 unsigned Len = Record[Idx++];
735 assert(Record[Idx] == E->getNumConcatenated() &&
736 "Wrong number of concatenated tokens!");
737 ++Idx;
738 E->setWide(Record[Idx++]);
739
740 // Read string data
741 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
742 E->setStrData(Reader.getContext(), &Str[0], Len);
743 Idx += Len;
744
745 // Read source locations
746 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
747 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
748
749 return 0;
750}
751
Douglas Gregora151ba42009-04-14 23:32:43 +0000752unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000753 VisitExpr(E);
754 E->setValue(Record[Idx++]);
755 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
756 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000757 return 0;
758}
759
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000760unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
761 VisitExpr(E);
762 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
763 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000764 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000765 return 1;
766}
767
Douglas Gregor12d74052009-04-15 15:58:59 +0000768unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
769 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000770 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000771 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
772 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
773 return 1;
774}
775
776unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
777 VisitExpr(E);
778 E->setSizeof(Record[Idx++]);
779 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000780 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000781 ++Idx;
782 } else {
783 E->setArgument(Reader.GetType(Record[Idx++]));
784 }
785 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
786 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
787 return E->isArgumentType()? 0 : 1;
788}
789
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000790unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
791 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000792 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
793 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000794 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 return 2;
796}
797
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000798unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
799 VisitExpr(E);
800 E->setNumArgs(Reader.getContext(), Record[Idx++]);
801 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000802 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000803 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000804 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000805 return E->getNumArgs() + 1;
806}
807
808unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
809 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000810 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000811 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
812 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
813 E->setArrow(Record[Idx++]);
814 return 1;
815}
816
Douglas Gregora151ba42009-04-14 23:32:43 +0000817unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
818 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000819 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000820 return 1;
821}
822
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000823unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
824 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000825 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
826 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000827 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
828 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
829 return 2;
830}
831
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000832unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
833 VisitBinaryOperator(E);
834 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
835 E->setComputationResultType(Reader.GetType(Record[Idx++]));
836 return 2;
837}
838
839unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
840 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000841 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
842 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
843 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000844 return 3;
845}
846
Douglas Gregora151ba42009-04-14 23:32:43 +0000847unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
848 VisitCastExpr(E);
849 E->setLvalueCast(Record[Idx++]);
850 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000851}
852
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000853unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
854 VisitCastExpr(E);
855 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
856 return 1;
857}
858
859unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
860 VisitExplicitCastExpr(E);
861 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
862 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
863 return 1;
864}
865
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000866unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
867 VisitExpr(E);
868 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000869 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000870 E->setFileScope(Record[Idx++]);
871 return 1;
872}
873
Douglas Gregorec0b8292009-04-15 23:02:49 +0000874unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
875 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000876 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000877 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
878 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
879 return 1;
880}
881
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000882unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
883 VisitExpr(E);
884 unsigned NumInits = Record[Idx++];
885 E->reserveInits(NumInits);
886 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000887 E->updateInit(I,
888 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
889 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000890 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
891 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
892 E->setInitializedFieldInUnion(
893 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
894 E->sawArrayRangeDesignator(Record[Idx++]);
895 return NumInits + 1;
896}
897
898unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
899 typedef DesignatedInitExpr::Designator Designator;
900
901 VisitExpr(E);
902 unsigned NumSubExprs = Record[Idx++];
903 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
904 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000905 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000906 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
907 E->setGNUSyntax(Record[Idx++]);
908
909 llvm::SmallVector<Designator, 4> Designators;
910 while (Idx < Record.size()) {
911 switch ((pch::DesignatorTypes)Record[Idx++]) {
912 case pch::DESIG_FIELD_DECL: {
913 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
914 SourceLocation DotLoc
915 = SourceLocation::getFromRawEncoding(Record[Idx++]);
916 SourceLocation FieldLoc
917 = SourceLocation::getFromRawEncoding(Record[Idx++]);
918 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
919 FieldLoc));
920 Designators.back().setField(Field);
921 break;
922 }
923
924 case pch::DESIG_FIELD_NAME: {
925 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
926 SourceLocation DotLoc
927 = SourceLocation::getFromRawEncoding(Record[Idx++]);
928 SourceLocation FieldLoc
929 = SourceLocation::getFromRawEncoding(Record[Idx++]);
930 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
931 break;
932 }
933
934 case pch::DESIG_ARRAY: {
935 unsigned Index = Record[Idx++];
936 SourceLocation LBracketLoc
937 = SourceLocation::getFromRawEncoding(Record[Idx++]);
938 SourceLocation RBracketLoc
939 = SourceLocation::getFromRawEncoding(Record[Idx++]);
940 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
941 break;
942 }
943
944 case pch::DESIG_ARRAY_RANGE: {
945 unsigned Index = Record[Idx++];
946 SourceLocation LBracketLoc
947 = SourceLocation::getFromRawEncoding(Record[Idx++]);
948 SourceLocation EllipsisLoc
949 = SourceLocation::getFromRawEncoding(Record[Idx++]);
950 SourceLocation RBracketLoc
951 = SourceLocation::getFromRawEncoding(Record[Idx++]);
952 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
953 RBracketLoc));
954 break;
955 }
956 }
957 }
958 E->setDesignators(&Designators[0], Designators.size());
959
960 return NumSubExprs;
961}
962
963unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
964 VisitExpr(E);
965 return 0;
966}
967
Douglas Gregorec0b8292009-04-15 23:02:49 +0000968unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
969 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000970 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000971 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
972 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
973 return 1;
974}
975
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000976unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
977 VisitExpr(E);
978 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
979 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
980 Reader.SetLabelOf(E, Record[Idx++]);
981 return 0;
982}
983
Douglas Gregoreca12f62009-04-17 19:05:30 +0000984unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
985 VisitExpr(E);
986 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
987 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
988 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
989 return 1;
990}
991
Douglas Gregor209d4622009-04-15 23:33:31 +0000992unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
993 VisitExpr(E);
994 E->setArgType1(Reader.GetType(Record[Idx++]));
995 E->setArgType2(Reader.GetType(Record[Idx++]));
996 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
997 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
998 return 0;
999}
1000
1001unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
1002 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001003 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
1004 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
1005 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +00001006 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1007 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1008 return 3;
1009}
1010
1011unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
1012 VisitExpr(E);
1013 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1014 return 0;
1015}
Douglas Gregorec0b8292009-04-15 23:02:49 +00001016
Douglas Gregor725e94b2009-04-16 00:01:45 +00001017unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1018 VisitExpr(E);
1019 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001020 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +00001021 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1022 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1023 return NumExprs;
1024}
1025
Douglas Gregore246b742009-04-17 19:21:43 +00001026unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
1027 VisitExpr(E);
1028 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
1029 E->setHasBlockDeclRefExprs(Record[Idx++]);
1030 return 0;
1031}
1032
Douglas Gregor725e94b2009-04-16 00:01:45 +00001033unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1034 VisitExpr(E);
1035 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
1036 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
1037 E->setByRef(Record[Idx++]);
1038 return 0;
1039}
1040
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001041//===----------------------------------------------------------------------===//
1042// Objective-C Expressions and Statements
1043
1044unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
1045 VisitExpr(E);
1046 E->setString(cast<StringLiteral>(StmtStack.back()));
1047 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1048 return 1;
1049}
1050
Chris Lattner80f83c62009-04-22 05:57:30 +00001051unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
1052 VisitExpr(E);
1053 E->setEncodedType(Reader.GetType(Record[Idx++]));
1054 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1055 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1056 return 0;
1057}
1058
Chris Lattnerc49bbe72009-04-22 06:29:42 +00001059unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
1060 VisitExpr(E);
1061 // FIXME: Selectors.
1062 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1063 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1064 return 0;
1065}
1066
1067unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
1068 VisitExpr(E);
1069 E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
1070 E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1071 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
1072 return 0;
1073}
1074
Chris Lattner80f83c62009-04-22 05:57:30 +00001075
Douglas Gregorc713da92009-04-21 22:25:48 +00001076//===----------------------------------------------------------------------===//
1077// PCH reader implementation
1078//===----------------------------------------------------------------------===//
1079
1080namespace {
1081class VISIBILITY_HIDDEN PCHIdentifierLookupTrait {
1082 PCHReader &Reader;
1083
1084 // If we know the IdentifierInfo in advance, it is here and we will
1085 // not build a new one. Used when deserializing information about an
1086 // identifier that was constructed before the PCH file was read.
1087 IdentifierInfo *KnownII;
1088
1089public:
1090 typedef IdentifierInfo * data_type;
1091
1092 typedef const std::pair<const char*, unsigned> external_key_type;
1093
1094 typedef external_key_type internal_key_type;
1095
1096 explicit PCHIdentifierLookupTrait(PCHReader &Reader, IdentifierInfo *II = 0)
1097 : Reader(Reader), KnownII(II) { }
1098
1099 static bool EqualKey(const internal_key_type& a,
1100 const internal_key_type& b) {
1101 return (a.second == b.second) ? memcmp(a.first, b.first, a.second) == 0
1102 : false;
1103 }
1104
1105 static unsigned ComputeHash(const internal_key_type& a) {
1106 return BernsteinHash(a.first, a.second);
1107 }
1108
1109 // This hopefully will just get inlined and removed by the optimizer.
1110 static const internal_key_type&
1111 GetInternalKey(const external_key_type& x) { return x; }
1112
1113 static std::pair<unsigned, unsigned>
1114 ReadKeyDataLength(const unsigned char*& d) {
1115 using namespace clang::io;
1116 unsigned KeyLen = ReadUnalignedLE16(d);
1117 unsigned DataLen = ReadUnalignedLE16(d);
1118 return std::make_pair(KeyLen, DataLen);
1119 }
1120
1121 static std::pair<const char*, unsigned>
1122 ReadKey(const unsigned char* d, unsigned n) {
1123 assert(n >= 2 && d[n-1] == '\0');
1124 return std::make_pair((const char*) d, n-1);
1125 }
1126
1127 IdentifierInfo *ReadData(const internal_key_type& k,
1128 const unsigned char* d,
1129 unsigned DataLen) {
1130 using namespace clang::io;
Douglas Gregor2554cf22009-04-22 21:15:06 +00001131 uint32_t Bits = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001132 bool CPlusPlusOperatorKeyword = Bits & 0x01;
1133 Bits >>= 1;
1134 bool Poisoned = Bits & 0x01;
1135 Bits >>= 1;
1136 bool ExtensionToken = Bits & 0x01;
1137 Bits >>= 1;
1138 bool hasMacroDefinition = Bits & 0x01;
1139 Bits >>= 1;
1140 unsigned ObjCOrBuiltinID = Bits & 0x3FF;
1141 Bits >>= 10;
1142 unsigned TokenID = Bits & 0xFF;
1143 Bits >>= 8;
1144
Douglas Gregorc713da92009-04-21 22:25:48 +00001145 pch::IdentID ID = ReadUnalignedLE32(d);
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001146 assert(Bits == 0 && "Extra bits in the identifier?");
Douglas Gregorc713da92009-04-21 22:25:48 +00001147 DataLen -= 8;
1148
1149 // Build the IdentifierInfo itself and link the identifier ID with
1150 // the new IdentifierInfo.
1151 IdentifierInfo *II = KnownII;
1152 if (!II)
1153 II = &Reader.getIdentifierTable().CreateIdentifierInfo(
1154 k.first, k.first + k.second);
1155 Reader.SetIdentifierInfo(ID, II);
1156
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001157 // Set or check the various bits in the IdentifierInfo structure.
1158 // FIXME: Load token IDs lazily, too?
1159 assert((unsigned)II->getTokenID() == TokenID &&
1160 "Incorrect token ID loaded");
1161 (void)TokenID;
1162 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1163 assert(II->isExtensionToken() == ExtensionToken &&
1164 "Incorrect extension token flag");
1165 (void)ExtensionToken;
1166 II->setIsPoisoned(Poisoned);
1167 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1168 "Incorrect C++ operator keyword flag");
1169 (void)CPlusPlusOperatorKeyword;
1170
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001171 // If this identifier is a macro, deserialize the macro
1172 // definition.
1173 if (hasMacroDefinition) {
1174 uint32_t Offset = ReadUnalignedLE64(d);
1175 Reader.ReadMacroRecord(Offset);
1176 DataLen -= 8;
1177 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001178
1179 // Read all of the declarations visible at global scope with this
1180 // name.
1181 Sema *SemaObj = Reader.getSema();
1182 while (DataLen > 0) {
1183 NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(ReadUnalignedLE32(d)));
Douglas Gregorc713da92009-04-21 22:25:48 +00001184 if (SemaObj) {
1185 // Introduce this declaration into the translation-unit scope
1186 // and add it to the declaration chain for this identifier, so
1187 // that (unqualified) name lookup will find it.
1188 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(D));
1189 SemaObj->IdResolver.AddDeclToIdentifierChain(II, D);
1190 } else {
1191 // Queue this declaration so that it will be added to the
1192 // translation unit scope and identifier's declaration chain
1193 // once a Sema object is known.
Douglas Gregor2554cf22009-04-22 21:15:06 +00001194 Reader.PreloadedDecls.push_back(D);
Douglas Gregorc713da92009-04-21 22:25:48 +00001195 }
1196
1197 DataLen -= 4;
1198 }
1199 return II;
1200 }
1201};
1202
1203} // end anonymous namespace
1204
1205/// \brief The on-disk hash table used to contain information about
1206/// all of the identifiers in the program.
1207typedef OnDiskChainedHashTable<PCHIdentifierLookupTrait>
1208 PCHIdentifierLookupTable;
1209
Douglas Gregorc34897d2009-04-09 22:27:44 +00001210// FIXME: use the diagnostics machinery
1211static bool Error(const char *Str) {
1212 std::fprintf(stderr, "%s\n", Str);
1213 return true;
1214}
1215
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001216/// \brief Check the contents of the predefines buffer against the
1217/// contents of the predefines buffer used to build the PCH file.
1218///
1219/// The contents of the two predefines buffers should be the same. If
1220/// not, then some command-line option changed the preprocessor state
1221/// and we must reject the PCH file.
1222///
1223/// \param PCHPredef The start of the predefines buffer in the PCH
1224/// file.
1225///
1226/// \param PCHPredefLen The length of the predefines buffer in the PCH
1227/// file.
1228///
1229/// \param PCHBufferID The FileID for the PCH predefines buffer.
1230///
1231/// \returns true if there was a mismatch (in which case the PCH file
1232/// should be ignored), or false otherwise.
1233bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
1234 unsigned PCHPredefLen,
1235 FileID PCHBufferID) {
1236 const char *Predef = PP.getPredefines().c_str();
1237 unsigned PredefLen = PP.getPredefines().size();
1238
1239 // If the two predefines buffers compare equal, we're done!.
1240 if (PredefLen == PCHPredefLen &&
1241 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
1242 return false;
1243
1244 // The predefines buffers are different. Produce a reasonable
1245 // diagnostic showing where they are different.
1246
1247 // The source locations (potentially in the two different predefines
1248 // buffers)
1249 SourceLocation Loc1, Loc2;
1250 SourceManager &SourceMgr = PP.getSourceManager();
1251
1252 // Create a source buffer for our predefines string, so
1253 // that we can build a diagnostic that points into that
1254 // source buffer.
1255 FileID BufferID;
1256 if (Predef && Predef[0]) {
1257 llvm::MemoryBuffer *Buffer
1258 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
1259 "<built-in>");
1260 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1261 }
1262
1263 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
1264 std::pair<const char *, const char *> Locations
1265 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
1266
1267 if (Locations.first != Predef + MinLen) {
1268 // We found the location in the two buffers where there is a
1269 // difference. Form source locations to point there (in both
1270 // buffers).
1271 unsigned Offset = Locations.first - Predef;
1272 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1273 .getFileLocWithOffset(Offset);
1274 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1275 .getFileLocWithOffset(Offset);
1276 } else if (PredefLen > PCHPredefLen) {
1277 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
1278 .getFileLocWithOffset(MinLen);
1279 } else {
1280 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
1281 .getFileLocWithOffset(MinLen);
1282 }
1283
1284 Diag(Loc1, diag::warn_pch_preprocessor);
1285 if (Loc2.isValid())
1286 Diag(Loc2, diag::note_predef_in_pch);
1287 Diag(diag::note_ignoring_pch) << FileName;
1288 return true;
1289}
1290
Douglas Gregor635f97f2009-04-13 16:31:14 +00001291/// \brief Read the line table in the source manager block.
1292/// \returns true if ther was an error.
1293static bool ParseLineTable(SourceManager &SourceMgr,
1294 llvm::SmallVectorImpl<uint64_t> &Record) {
1295 unsigned Idx = 0;
1296 LineTableInfo &LineTable = SourceMgr.getLineTable();
1297
1298 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +00001299 std::map<int, int> FileIDs;
1300 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +00001301 // Extract the file name
1302 unsigned FilenameLen = Record[Idx++];
1303 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1304 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001305 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1306 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001307 }
1308
1309 // Parse the line entries
1310 std::vector<LineEntry> Entries;
1311 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001312 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001313
1314 // Extract the line entries
1315 unsigned NumEntries = Record[Idx++];
1316 Entries.clear();
1317 Entries.reserve(NumEntries);
1318 for (unsigned I = 0; I != NumEntries; ++I) {
1319 unsigned FileOffset = Record[Idx++];
1320 unsigned LineNo = Record[Idx++];
1321 int FilenameID = Record[Idx++];
1322 SrcMgr::CharacteristicKind FileKind
1323 = (SrcMgr::CharacteristicKind)Record[Idx++];
1324 unsigned IncludeOffset = Record[Idx++];
1325 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1326 FileKind, IncludeOffset));
1327 }
1328 LineTable.AddEntry(FID, Entries);
1329 }
1330
1331 return false;
1332}
1333
Douglas Gregorab1cef72009-04-10 03:52:48 +00001334/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001335PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001336 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001337 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1338 Error("Malformed source manager block record");
1339 return Failure;
1340 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001341
1342 SourceManager &SourceMgr = Context.getSourceManager();
1343 RecordData Record;
1344 while (true) {
1345 unsigned Code = Stream.ReadCode();
1346 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001347 if (Stream.ReadBlockEnd()) {
1348 Error("Error at end of Source Manager block");
1349 return Failure;
1350 }
1351
1352 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001353 }
1354
1355 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1356 // No known subblocks, always skip them.
1357 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001358 if (Stream.SkipBlock()) {
1359 Error("Malformed block record");
1360 return Failure;
1361 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001362 continue;
1363 }
1364
1365 if (Code == llvm::bitc::DEFINE_ABBREV) {
1366 Stream.ReadAbbrevRecord();
1367 continue;
1368 }
1369
1370 // Read a record.
1371 const char *BlobStart;
1372 unsigned BlobLen;
1373 Record.clear();
1374 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1375 default: // Default behavior: ignore.
1376 break;
1377
1378 case pch::SM_SLOC_FILE_ENTRY: {
1379 // FIXME: We would really like to delay the creation of this
1380 // FileEntry until it is actually required, e.g., when producing
1381 // a diagnostic with a source location in this file.
1382 const FileEntry *File
1383 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1384 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001385 FileID ID = SourceMgr.createFileID(File,
1386 SourceLocation::getFromRawEncoding(Record[1]),
1387 (CharacteristicKind)Record[2]);
1388 if (Record[3])
1389 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1390 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001391 break;
1392 }
1393
1394 case pch::SM_SLOC_BUFFER_ENTRY: {
1395 const char *Name = BlobStart;
1396 unsigned Code = Stream.ReadCode();
1397 Record.clear();
1398 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1399 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001400 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001401 llvm::MemoryBuffer *Buffer
1402 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1403 BlobStart + BlobLen - 1,
1404 Name);
1405 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1406
1407 if (strcmp(Name, "<built-in>") == 0
1408 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1409 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001410 break;
1411 }
1412
1413 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1414 SourceLocation SpellingLoc
1415 = SourceLocation::getFromRawEncoding(Record[1]);
1416 SourceMgr.createInstantiationLoc(
1417 SpellingLoc,
1418 SourceLocation::getFromRawEncoding(Record[2]),
1419 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001420 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001421 break;
1422 }
1423
Chris Lattnere1be6022009-04-14 23:22:57 +00001424 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001425 if (ParseLineTable(SourceMgr, Record))
1426 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001427 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001428 }
1429 }
1430}
1431
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001432void PCHReader::ReadMacroRecord(uint64_t Offset) {
1433 // Keep track of where we are in the stream, then jump back there
1434 // after reading this macro.
1435 SavedStreamPosition SavedPosition(Stream);
1436
1437 Stream.JumpToBit(Offset);
1438 RecordData Record;
1439 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1440 MacroInfo *Macro = 0;
1441 while (true) {
1442 unsigned Code = Stream.ReadCode();
1443 switch (Code) {
1444 case llvm::bitc::END_BLOCK:
1445 return;
1446
1447 case llvm::bitc::ENTER_SUBBLOCK:
1448 // No known subblocks, always skip them.
1449 Stream.ReadSubBlockID();
1450 if (Stream.SkipBlock()) {
1451 Error("Malformed block record");
1452 return;
1453 }
1454 continue;
1455
1456 case llvm::bitc::DEFINE_ABBREV:
1457 Stream.ReadAbbrevRecord();
1458 continue;
1459 default: break;
1460 }
1461
1462 // Read a record.
1463 Record.clear();
1464 pch::PreprocessorRecordTypes RecType =
1465 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1466 switch (RecType) {
1467 case pch::PP_COUNTER_VALUE:
1468 // Skip this record.
1469 break;
1470
1471 case pch::PP_MACRO_OBJECT_LIKE:
1472 case pch::PP_MACRO_FUNCTION_LIKE: {
1473 // If we already have a macro, that means that we've hit the end
1474 // of the definition of the macro we were looking for. We're
1475 // done.
1476 if (Macro)
1477 return;
1478
1479 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1480 if (II == 0) {
1481 Error("Macro must have a name");
1482 return;
1483 }
1484 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1485 bool isUsed = Record[2];
1486
1487 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1488 MI->setIsUsed(isUsed);
1489
1490 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1491 // Decode function-like macro info.
1492 bool isC99VarArgs = Record[3];
1493 bool isGNUVarArgs = Record[4];
1494 MacroArgs.clear();
1495 unsigned NumArgs = Record[5];
1496 for (unsigned i = 0; i != NumArgs; ++i)
1497 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
1498
1499 // Install function-like macro info.
1500 MI->setIsFunctionLike();
1501 if (isC99VarArgs) MI->setIsC99Varargs();
1502 if (isGNUVarArgs) MI->setIsGNUVarargs();
1503 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1504 PP.getPreprocessorAllocator());
1505 }
1506
1507 // Finally, install the macro.
1508 PP.setMacroInfo(II, MI);
1509
1510 // Remember that we saw this macro last so that we add the tokens that
1511 // form its body to it.
1512 Macro = MI;
1513 ++NumMacrosRead;
1514 break;
1515 }
1516
1517 case pch::PP_TOKEN: {
1518 // If we see a TOKEN before a PP_MACRO_*, then the file is
1519 // erroneous, just pretend we didn't see this.
1520 if (Macro == 0) break;
1521
1522 Token Tok;
1523 Tok.startToken();
1524 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1525 Tok.setLength(Record[1]);
1526 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1527 Tok.setIdentifierInfo(II);
1528 Tok.setKind((tok::TokenKind)Record[3]);
1529 Tok.setFlag((Token::TokenFlags)Record[4]);
1530 Macro->AddTokenToBody(Tok);
1531 break;
1532 }
1533 }
1534 }
1535}
1536
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001537bool PCHReader::ReadPreprocessorBlock() {
1538 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1539 return Error("Malformed preprocessor block record");
1540
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001541 RecordData Record;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001542 while (true) {
1543 unsigned Code = Stream.ReadCode();
1544 switch (Code) {
1545 case llvm::bitc::END_BLOCK:
1546 if (Stream.ReadBlockEnd())
1547 return Error("Error at end of preprocessor block");
1548 return false;
1549
1550 case llvm::bitc::ENTER_SUBBLOCK:
1551 // No known subblocks, always skip them.
1552 Stream.ReadSubBlockID();
1553 if (Stream.SkipBlock())
1554 return Error("Malformed block record");
1555 continue;
1556
1557 case llvm::bitc::DEFINE_ABBREV:
1558 Stream.ReadAbbrevRecord();
1559 continue;
1560 default: break;
1561 }
1562
1563 // Read a record.
1564 Record.clear();
1565 pch::PreprocessorRecordTypes RecType =
1566 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1567 switch (RecType) {
1568 default: // Default behavior: ignore unknown records.
1569 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001570 case pch::PP_COUNTER_VALUE:
1571 if (!Record.empty())
1572 PP.setCounterValue(Record[0]);
1573 break;
1574
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001575 case pch::PP_MACRO_OBJECT_LIKE:
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001576 case pch::PP_MACRO_FUNCTION_LIKE:
1577 case pch::PP_TOKEN:
1578 // Once we've hit a macro definition or a token, we're done.
1579 return false;
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001580 }
1581 }
1582}
1583
Douglas Gregorc713da92009-04-21 22:25:48 +00001584PCHReader::PCHReadResult
1585PCHReader::ReadPCHBlock(uint64_t &PreprocessorBlockOffset) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001586 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1587 Error("Malformed block record");
1588 return Failure;
1589 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001590
1591 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001592 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001593 while (!Stream.AtEndOfStream()) {
1594 unsigned Code = Stream.ReadCode();
1595 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001596 if (Stream.ReadBlockEnd()) {
1597 Error("Error at end of module block");
1598 return Failure;
1599 }
Chris Lattner29241862009-04-11 21:15:38 +00001600
Douglas Gregor179cfb12009-04-10 20:39:37 +00001601 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001602 }
1603
1604 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1605 switch (Stream.ReadSubBlockID()) {
1606 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1607 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1608 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001609 if (Stream.SkipBlock()) {
1610 Error("Malformed block record");
1611 return Failure;
1612 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001613 break;
1614
Chris Lattner29241862009-04-11 21:15:38 +00001615 case pch::PREPROCESSOR_BLOCK_ID:
1616 // Skip the preprocessor block for now, but remember where it is. We
1617 // want to read it in after the identifier table.
Douglas Gregorc713da92009-04-21 22:25:48 +00001618 if (PreprocessorBlockOffset) {
Chris Lattner29241862009-04-11 21:15:38 +00001619 Error("Multiple preprocessor blocks found.");
1620 return Failure;
1621 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001622 PreprocessorBlockOffset = Stream.GetCurrentBitNo();
Chris Lattner29241862009-04-11 21:15:38 +00001623 if (Stream.SkipBlock()) {
1624 Error("Malformed block record");
1625 return Failure;
1626 }
1627 break;
1628
Douglas Gregorab1cef72009-04-10 03:52:48 +00001629 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001630 switch (ReadSourceManagerBlock()) {
1631 case Success:
1632 break;
1633
1634 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001635 Error("Malformed source manager block");
1636 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001637
1638 case IgnorePCH:
1639 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001640 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001641 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001642 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001643 continue;
1644 }
1645
1646 if (Code == llvm::bitc::DEFINE_ABBREV) {
1647 Stream.ReadAbbrevRecord();
1648 continue;
1649 }
1650
1651 // Read and process a record.
1652 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001653 const char *BlobStart = 0;
1654 unsigned BlobLen = 0;
1655 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1656 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001657 default: // Default behavior: ignore.
1658 break;
1659
1660 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001661 if (!TypeOffsets.empty()) {
1662 Error("Duplicate TYPE_OFFSET record in PCH file");
1663 return Failure;
1664 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001665 TypeOffsets.swap(Record);
1666 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1667 break;
1668
1669 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001670 if (!DeclOffsets.empty()) {
1671 Error("Duplicate DECL_OFFSET record in PCH file");
1672 return Failure;
1673 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001674 DeclOffsets.swap(Record);
1675 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1676 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001677
1678 case pch::LANGUAGE_OPTIONS:
1679 if (ParseLanguageOptions(Record))
1680 return IgnorePCH;
1681 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001682
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001683 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001684 std::string TargetTriple(BlobStart, BlobLen);
1685 if (TargetTriple != Context.Target.getTargetTriple()) {
1686 Diag(diag::warn_pch_target_triple)
1687 << TargetTriple << Context.Target.getTargetTriple();
1688 Diag(diag::note_ignoring_pch) << FileName;
1689 return IgnorePCH;
1690 }
1691 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001692 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001693
1694 case pch::IDENTIFIER_TABLE:
Douglas Gregorc713da92009-04-21 22:25:48 +00001695 IdentifierTableData = BlobStart;
1696 IdentifierLookupTable
1697 = PCHIdentifierLookupTable::Create(
1698 (const unsigned char *)IdentifierTableData + Record[0],
1699 (const unsigned char *)IdentifierTableData,
1700 PCHIdentifierLookupTrait(*this));
Douglas Gregorc713da92009-04-21 22:25:48 +00001701 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001702 break;
1703
1704 case pch::IDENTIFIER_OFFSET:
1705 if (!IdentifierData.empty()) {
1706 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1707 return Failure;
1708 }
1709 IdentifierData.swap(Record);
1710#ifndef NDEBUG
1711 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1712 if ((IdentifierData[I] & 0x01) == 0) {
1713 Error("Malformed identifier table in the precompiled header");
1714 return Failure;
1715 }
1716 }
1717#endif
1718 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001719
1720 case pch::EXTERNAL_DEFINITIONS:
1721 if (!ExternalDefinitions.empty()) {
1722 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1723 return Failure;
1724 }
1725 ExternalDefinitions.swap(Record);
1726 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001727
Douglas Gregore01ad442009-04-18 05:55:16 +00001728 case pch::SPECIAL_TYPES:
1729 SpecialTypes.swap(Record);
1730 break;
1731
Douglas Gregor456e0952009-04-17 22:13:46 +00001732 case pch::STATISTICS:
1733 TotalNumStatements = Record[0];
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001734 TotalNumMacros = Record[1];
Douglas Gregoraf136d92009-04-22 22:34:57 +00001735 TotalLexicalDeclContexts = Record[2];
1736 TotalVisibleDeclContexts = Record[3];
Douglas Gregor456e0952009-04-17 22:13:46 +00001737 break;
1738
Douglas Gregor77b2cd52009-04-22 22:02:47 +00001739 case pch::TENTATIVE_DEFINITIONS:
1740 if (!TentativeDefinitions.empty()) {
1741 Error("Duplicate TENTATIVE_DEFINITIONS record in PCH file");
1742 return Failure;
1743 }
1744 TentativeDefinitions.swap(Record);
1745 break;
Douglas Gregor062d9482009-04-22 22:18:58 +00001746
1747 case pch::LOCALLY_SCOPED_EXTERNAL_DECLS:
1748 if (!LocallyScopedExternalDecls.empty()) {
1749 Error("Duplicate LOCALLY_SCOPED_EXTERNAL_DECLS record in PCH file");
1750 return Failure;
1751 }
1752 LocallyScopedExternalDecls.swap(Record);
1753 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001754 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001755 }
1756
Douglas Gregor179cfb12009-04-10 20:39:37 +00001757 Error("Premature end of bitstream");
1758 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001759}
1760
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001761PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001762 // Set the PCH file name.
1763 this->FileName = FileName;
1764
Douglas Gregorc34897d2009-04-09 22:27:44 +00001765 // Open the PCH file.
1766 std::string ErrStr;
1767 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001768 if (!Buffer) {
1769 Error(ErrStr.c_str());
1770 return IgnorePCH;
1771 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001772
1773 // Initialize the stream
1774 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1775 (const unsigned char *)Buffer->getBufferEnd());
1776
1777 // Sniff for the signature.
1778 if (Stream.Read(8) != 'C' ||
1779 Stream.Read(8) != 'P' ||
1780 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001781 Stream.Read(8) != 'H') {
1782 Error("Not a PCH file");
1783 return IgnorePCH;
1784 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001785
1786 // We expect a number of well-defined blocks, though we don't necessarily
1787 // need to understand them all.
Douglas Gregorc713da92009-04-21 22:25:48 +00001788 uint64_t PreprocessorBlockOffset = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001789 while (!Stream.AtEndOfStream()) {
1790 unsigned Code = Stream.ReadCode();
1791
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001792 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1793 Error("Invalid record at top-level");
1794 return Failure;
1795 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001796
1797 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregorc713da92009-04-21 22:25:48 +00001798
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799 // We only know the PCH subblock ID.
1800 switch (BlockID) {
1801 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001802 if (Stream.ReadBlockInfoBlock()) {
1803 Error("Malformed BlockInfoBlock");
1804 return Failure;
1805 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001806 break;
1807 case pch::PCH_BLOCK_ID:
Douglas Gregorc713da92009-04-21 22:25:48 +00001808 switch (ReadPCHBlock(PreprocessorBlockOffset)) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001809 case Success:
1810 break;
1811
1812 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001813 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001814
1815 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001816 // FIXME: We could consider reading through to the end of this
1817 // PCH block, skipping subblocks, to see if there are other
1818 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001819 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001820 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001821 break;
1822 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001823 if (Stream.SkipBlock()) {
1824 Error("Malformed block record");
1825 return Failure;
1826 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001827 break;
1828 }
1829 }
1830
1831 // Load the translation unit declaration
1832 ReadDeclRecord(DeclOffsets[0], 0);
1833
Douglas Gregorc713da92009-04-21 22:25:48 +00001834 // Initialization of builtins and library builtins occurs before the
1835 // PCH file is read, so there may be some identifiers that were
1836 // loaded into the IdentifierTable before we intercepted the
1837 // creation of identifiers. Iterate through the list of known
1838 // identifiers and determine whether we have to establish
1839 // preprocessor definitions or top-level identifier declaration
1840 // chains for those identifiers.
1841 //
1842 // We copy the IdentifierInfo pointers to a small vector first,
1843 // since de-serializing declarations or macro definitions can add
1844 // new entries into the identifier table, invalidating the
1845 // iterators.
1846 llvm::SmallVector<IdentifierInfo *, 128> Identifiers;
1847 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
1848 IdEnd = PP.getIdentifierTable().end();
1849 Id != IdEnd; ++Id)
1850 Identifiers.push_back(Id->second);
1851 PCHIdentifierLookupTable *IdTable
1852 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
1853 for (unsigned I = 0, N = Identifiers.size(); I != N; ++I) {
1854 IdentifierInfo *II = Identifiers[I];
1855 // Look in the on-disk hash table for an entry for
1856 PCHIdentifierLookupTrait Info(*this, II);
1857 std::pair<const char*, unsigned> Key(II->getName(), II->getLength());
1858 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Info);
1859 if (Pos == IdTable->end())
1860 continue;
1861
1862 // Dereferencing the iterator has the effect of populating the
1863 // IdentifierInfo node with the various declarations it needs.
1864 (void)*Pos;
1865 }
1866
Douglas Gregore01ad442009-04-18 05:55:16 +00001867 // Load the special types.
1868 Context.setBuiltinVaListType(
1869 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1870
Douglas Gregorc713da92009-04-21 22:25:48 +00001871 // If we saw the preprocessor block, read it now.
1872 if (PreprocessorBlockOffset) {
1873 SavedStreamPosition SavedPos(Stream);
1874 Stream.JumpToBit(PreprocessorBlockOffset);
1875 if (ReadPreprocessorBlock()) {
1876 Error("Malformed preprocessor block");
1877 return Failure;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001878 }
Douglas Gregorc713da92009-04-21 22:25:48 +00001879 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001880
Douglas Gregorc713da92009-04-21 22:25:48 +00001881 return Success;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001882}
1883
Douglas Gregor179cfb12009-04-10 20:39:37 +00001884/// \brief Parse the record that corresponds to a LangOptions data
1885/// structure.
1886///
1887/// This routine compares the language options used to generate the
1888/// PCH file against the language options set for the current
1889/// compilation. For each option, we classify differences between the
1890/// two compiler states as either "benign" or "important". Benign
1891/// differences don't matter, and we accept them without complaint
1892/// (and without modifying the language options). Differences between
1893/// the states for important options cause the PCH file to be
1894/// unusable, so we emit a warning and return true to indicate that
1895/// there was an error.
1896///
1897/// \returns true if the PCH file is unacceptable, false otherwise.
1898bool PCHReader::ParseLanguageOptions(
1899 const llvm::SmallVectorImpl<uint64_t> &Record) {
1900 const LangOptions &LangOpts = Context.getLangOptions();
1901#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1902#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1903 if (Record[Idx] != LangOpts.Option) { \
1904 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1905 Diag(diag::note_ignoring_pch) << FileName; \
1906 return true; \
1907 } \
1908 ++Idx
1909
1910 unsigned Idx = 0;
1911 PARSE_LANGOPT_BENIGN(Trigraphs);
1912 PARSE_LANGOPT_BENIGN(BCPLComment);
1913 PARSE_LANGOPT_BENIGN(DollarIdents);
1914 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1915 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1916 PARSE_LANGOPT_BENIGN(ImplicitInt);
1917 PARSE_LANGOPT_BENIGN(Digraphs);
1918 PARSE_LANGOPT_BENIGN(HexFloats);
1919 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1920 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1921 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1922 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1923 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1924 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1925 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1926 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1927 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1928 PARSE_LANGOPT_BENIGN(PascalStrings);
1929 PARSE_LANGOPT_BENIGN(Boolean);
1930 PARSE_LANGOPT_BENIGN(WritableStrings);
1931 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1932 diag::warn_pch_lax_vector_conversions);
1933 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1934 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1935 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1936 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1937 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1938 diag::warn_pch_thread_safe_statics);
1939 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1940 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1941 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1942 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1943 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1944 diag::warn_pch_heinous_extensions);
1945 // FIXME: Most of the options below are benign if the macro wasn't
1946 // used. Unfortunately, this means that a PCH compiled without
1947 // optimization can't be used with optimization turned on, even
1948 // though the only thing that changes is whether __OPTIMIZE__ was
1949 // defined... but if __OPTIMIZE__ never showed up in the header, it
1950 // doesn't matter. We could consider making this some special kind
1951 // of check.
1952 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1953 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1954 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1955 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1956 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1957 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1958 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1959 Diag(diag::warn_pch_gc_mode)
1960 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1961 Diag(diag::note_ignoring_pch) << FileName;
1962 return true;
1963 }
1964 ++Idx;
1965 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1966 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1967#undef PARSE_LANGOPT_IRRELEVANT
1968#undef PARSE_LANGOPT_BENIGN
1969
1970 return false;
1971}
1972
Douglas Gregorc34897d2009-04-09 22:27:44 +00001973/// \brief Read and return the type at the given offset.
1974///
1975/// This routine actually reads the record corresponding to the type
1976/// at the given offset in the bitstream. It is a helper routine for
1977/// GetType, which deals with reading type IDs.
1978QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001979 // Keep track of where we are in the stream, then jump back there
1980 // after reading this type.
1981 SavedStreamPosition SavedPosition(Stream);
1982
Douglas Gregorc34897d2009-04-09 22:27:44 +00001983 Stream.JumpToBit(Offset);
1984 RecordData Record;
1985 unsigned Code = Stream.ReadCode();
1986 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001987 case pch::TYPE_EXT_QUAL: {
1988 assert(Record.size() == 3 &&
1989 "Incorrect encoding of extended qualifier type");
1990 QualType Base = GetType(Record[0]);
1991 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1992 unsigned AddressSpace = Record[2];
1993
1994 QualType T = Base;
1995 if (GCAttr != QualType::GCNone)
1996 T = Context.getObjCGCQualType(T, GCAttr);
1997 if (AddressSpace)
1998 T = Context.getAddrSpaceQualType(T, AddressSpace);
1999 return T;
2000 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002001
Douglas Gregorc34897d2009-04-09 22:27:44 +00002002 case pch::TYPE_FIXED_WIDTH_INT: {
2003 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
2004 return Context.getFixedWidthIntType(Record[0], Record[1]);
2005 }
2006
2007 case pch::TYPE_COMPLEX: {
2008 assert(Record.size() == 1 && "Incorrect encoding of complex type");
2009 QualType ElemType = GetType(Record[0]);
2010 return Context.getComplexType(ElemType);
2011 }
2012
2013 case pch::TYPE_POINTER: {
2014 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
2015 QualType PointeeType = GetType(Record[0]);
2016 return Context.getPointerType(PointeeType);
2017 }
2018
2019 case pch::TYPE_BLOCK_POINTER: {
2020 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
2021 QualType PointeeType = GetType(Record[0]);
2022 return Context.getBlockPointerType(PointeeType);
2023 }
2024
2025 case pch::TYPE_LVALUE_REFERENCE: {
2026 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
2027 QualType PointeeType = GetType(Record[0]);
2028 return Context.getLValueReferenceType(PointeeType);
2029 }
2030
2031 case pch::TYPE_RVALUE_REFERENCE: {
2032 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
2033 QualType PointeeType = GetType(Record[0]);
2034 return Context.getRValueReferenceType(PointeeType);
2035 }
2036
2037 case pch::TYPE_MEMBER_POINTER: {
2038 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
2039 QualType PointeeType = GetType(Record[0]);
2040 QualType ClassType = GetType(Record[1]);
2041 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
2042 }
2043
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002044 case pch::TYPE_CONSTANT_ARRAY: {
2045 QualType ElementType = GetType(Record[0]);
2046 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2047 unsigned IndexTypeQuals = Record[2];
2048 unsigned Idx = 3;
2049 llvm::APInt Size = ReadAPInt(Record, Idx);
2050 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
2051 }
2052
2053 case pch::TYPE_INCOMPLETE_ARRAY: {
2054 QualType ElementType = GetType(Record[0]);
2055 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2056 unsigned IndexTypeQuals = Record[2];
2057 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
2058 }
2059
2060 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002061 QualType ElementType = GetType(Record[0]);
2062 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
2063 unsigned IndexTypeQuals = Record[2];
2064 return Context.getVariableArrayType(ElementType, ReadExpr(),
2065 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002066 }
2067
2068 case pch::TYPE_VECTOR: {
2069 if (Record.size() != 2) {
2070 Error("Incorrect encoding of vector type in PCH file");
2071 return QualType();
2072 }
2073
2074 QualType ElementType = GetType(Record[0]);
2075 unsigned NumElements = Record[1];
2076 return Context.getVectorType(ElementType, NumElements);
2077 }
2078
2079 case pch::TYPE_EXT_VECTOR: {
2080 if (Record.size() != 2) {
2081 Error("Incorrect encoding of extended vector type in PCH file");
2082 return QualType();
2083 }
2084
2085 QualType ElementType = GetType(Record[0]);
2086 unsigned NumElements = Record[1];
2087 return Context.getExtVectorType(ElementType, NumElements);
2088 }
2089
2090 case pch::TYPE_FUNCTION_NO_PROTO: {
2091 if (Record.size() != 1) {
2092 Error("Incorrect encoding of no-proto function type");
2093 return QualType();
2094 }
2095 QualType ResultType = GetType(Record[0]);
2096 return Context.getFunctionNoProtoType(ResultType);
2097 }
2098
2099 case pch::TYPE_FUNCTION_PROTO: {
2100 QualType ResultType = GetType(Record[0]);
2101 unsigned Idx = 1;
2102 unsigned NumParams = Record[Idx++];
2103 llvm::SmallVector<QualType, 16> ParamTypes;
2104 for (unsigned I = 0; I != NumParams; ++I)
2105 ParamTypes.push_back(GetType(Record[Idx++]));
2106 bool isVariadic = Record[Idx++];
2107 unsigned Quals = Record[Idx++];
2108 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
2109 isVariadic, Quals);
2110 }
2111
2112 case pch::TYPE_TYPEDEF:
2113 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
2114 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
2115
2116 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002117 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002118
2119 case pch::TYPE_TYPEOF: {
2120 if (Record.size() != 1) {
2121 Error("Incorrect encoding of typeof(type) in PCH file");
2122 return QualType();
2123 }
2124 QualType UnderlyingType = GetType(Record[0]);
2125 return Context.getTypeOfType(UnderlyingType);
2126 }
2127
2128 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00002129 assert(Record.size() == 1 && "Incorrect encoding of record type");
2130 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002131
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002132 case pch::TYPE_ENUM:
2133 assert(Record.size() == 1 && "Incorrect encoding of enum type");
2134 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
2135
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002136 case pch::TYPE_OBJC_INTERFACE:
Chris Lattner80f83c62009-04-22 05:57:30 +00002137 assert(Record.size() == 1 && "Incorrect encoding of objc interface type");
2138 return Context.getObjCInterfaceType(
2139 cast<ObjCInterfaceDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002140
Chris Lattnerbab2c0f2009-04-22 06:45:28 +00002141 case pch::TYPE_OBJC_QUALIFIED_INTERFACE: {
2142 unsigned Idx = 0;
2143 ObjCInterfaceDecl *ItfD = cast<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
2144 unsigned NumProtos = Record[Idx++];
2145 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2146 for (unsigned I = 0; I != NumProtos; ++I)
2147 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2148 return Context.getObjCQualifiedInterfaceType(ItfD, &Protos[0], NumProtos);
2149 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00002150
Chris Lattner9b9f2352009-04-22 06:40:03 +00002151 case pch::TYPE_OBJC_QUALIFIED_ID: {
2152 unsigned Idx = 0;
2153 unsigned NumProtos = Record[Idx++];
2154 llvm::SmallVector<ObjCProtocolDecl*, 4> Protos;
2155 for (unsigned I = 0; I != NumProtos; ++I)
2156 Protos.push_back(cast<ObjCProtocolDecl>(GetDecl(Record[Idx++])));
2157 return Context.getObjCQualifiedIdType(&Protos[0], NumProtos);
2158 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002159 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002160 // Suppress a GCC warning
2161 return QualType();
2162}
2163
2164/// \brief Note that we have loaded the declaration with the given
2165/// Index.
2166///
2167/// This routine notes that this declaration has already been loaded,
2168/// so that future GetDecl calls will return this declaration rather
2169/// than trying to load a new declaration.
2170inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
2171 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
2172 DeclAlreadyLoaded[Index] = true;
2173 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
2174}
2175
2176/// \brief Read the declaration at the given offset from the PCH file.
2177Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002178 // Keep track of where we are in the stream, then jump back there
2179 // after reading this declaration.
2180 SavedStreamPosition SavedPosition(Stream);
2181
Douglas Gregorc34897d2009-04-09 22:27:44 +00002182 Decl *D = 0;
2183 Stream.JumpToBit(Offset);
2184 RecordData Record;
2185 unsigned Code = Stream.ReadCode();
2186 unsigned Idx = 0;
2187 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002188
Douglas Gregorc34897d2009-04-09 22:27:44 +00002189 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00002190 case pch::DECL_ATTR:
2191 case pch::DECL_CONTEXT_LEXICAL:
2192 case pch::DECL_CONTEXT_VISIBLE:
2193 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
2194 break;
2195
Douglas Gregorc34897d2009-04-09 22:27:44 +00002196 case pch::DECL_TRANSLATION_UNIT:
2197 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00002198 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002199 break;
2200
2201 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002202 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002203 break;
2204 }
2205
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002206 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002207 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002208 break;
2209 }
2210
Douglas Gregor982365e2009-04-13 21:20:57 +00002211 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002212 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
2213 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00002214 break;
2215 }
2216
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002217 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002218 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2219 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002220 break;
2221 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002222
2223 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002224 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
2225 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002226 break;
2227 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002228
Steve Naroff79ea0e02009-04-20 15:06:07 +00002229 case pch::DECL_OBJC_METHOD: {
2230 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
2231 Selector(), QualType(), 0);
2232 break;
2233 }
2234
Steve Naroff97b53bd2009-04-21 15:12:33 +00002235 case pch::DECL_OBJC_INTERFACE: {
Steve Naroff7333b492009-04-20 20:09:33 +00002236 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
2237 break;
2238 }
2239
Steve Naroff97b53bd2009-04-21 15:12:33 +00002240 case pch::DECL_OBJC_IVAR: {
Steve Naroff7333b492009-04-20 20:09:33 +00002241 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2242 ObjCIvarDecl::None);
2243 break;
2244 }
2245
Steve Naroff97b53bd2009-04-21 15:12:33 +00002246 case pch::DECL_OBJC_PROTOCOL: {
2247 D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
2248 break;
2249 }
2250
2251 case pch::DECL_OBJC_AT_DEFS_FIELD: {
2252 D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(), 0,
2253 QualType(), 0);
2254 break;
2255 }
2256
2257 case pch::DECL_OBJC_CLASS: {
2258 D = ObjCClassDecl::Create(Context, 0, SourceLocation());
2259 break;
2260 }
2261
2262 case pch::DECL_OBJC_FORWARD_PROTOCOL: {
2263 D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
2264 break;
2265 }
2266
2267 case pch::DECL_OBJC_CATEGORY: {
2268 D = ObjCCategoryDecl::Create(Context, 0, SourceLocation(), 0);
2269 break;
2270 }
2271
2272 case pch::DECL_OBJC_CATEGORY_IMPL: {
Douglas Gregor58e7ce42009-04-23 02:53:57 +00002273 D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002274 break;
2275 }
2276
2277 case pch::DECL_OBJC_IMPLEMENTATION: {
Douglas Gregor087dbf32009-04-23 03:23:08 +00002278 D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002279 break;
2280 }
2281
2282 case pch::DECL_OBJC_COMPATIBLE_ALIAS: {
2283 // FIXME: Implement.
2284 break;
2285 }
2286
2287 case pch::DECL_OBJC_PROPERTY: {
Douglas Gregor3839f1c2009-04-22 23:20:34 +00002288 D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Steve Naroff97b53bd2009-04-21 15:12:33 +00002289 break;
2290 }
2291
2292 case pch::DECL_OBJC_PROPERTY_IMPL: {
Douglas Gregor3f2c5052009-04-23 03:43:53 +00002293 D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
2294 SourceLocation(), 0,
2295 ObjCPropertyImplDecl::Dynamic, 0);
Steve Naroff97b53bd2009-04-21 15:12:33 +00002296 break;
2297 }
2298
Douglas Gregor982365e2009-04-13 21:20:57 +00002299 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002300 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
2301 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00002302 break;
2303 }
2304
Douglas Gregorc34897d2009-04-09 22:27:44 +00002305 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002306 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2307 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00002308 break;
2309 }
2310
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002311 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002312 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
2313 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002314 break;
2315 }
2316
2317 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002318 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002319 QualType(), QualType(), VarDecl::None,
2320 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00002321 break;
2322 }
2323
Douglas Gregor2a491792009-04-13 22:49:25 +00002324 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002325 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00002326 break;
2327 }
2328
2329 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00002330 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00002331 break;
2332 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002333 }
2334
Douglas Gregorc713da92009-04-21 22:25:48 +00002335 assert(D && "Unknown declaration reading PCH file");
Douglas Gregorddf4d092009-04-16 22:29:51 +00002336 if (D) {
2337 LoadedDecl(Index, D);
2338 Reader.Visit(D);
2339 }
2340
Douglas Gregorc34897d2009-04-09 22:27:44 +00002341 // If this declaration is also a declaration context, get the
2342 // offsets for its tables of lexical and visible declarations.
2343 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
2344 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
2345 if (Offsets.first || Offsets.second) {
2346 DC->setHasExternalLexicalStorage(Offsets.first != 0);
2347 DC->setHasExternalVisibleStorage(Offsets.second != 0);
2348 DeclContextOffsets[DC] = Offsets;
2349 }
2350 }
2351 assert(Idx == Record.size());
2352
Douglas Gregor405b6432009-04-22 19:09:20 +00002353 if (Consumer) {
2354 // If we have deserialized a declaration that has a definition the
2355 // AST consumer might need to know about, notify the consumer
2356 // about that definition now.
2357 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2358 if (Var->isFileVarDecl() && Var->getInit()) {
2359 DeclGroupRef DG(Var);
2360 Consumer->HandleTopLevelDecl(DG);
2361 }
2362 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
2363 if (Func->isThisDeclarationADefinition()) {
2364 DeclGroupRef DG(Func);
2365 Consumer->HandleTopLevelDecl(DG);
2366 }
2367 }
2368 }
2369
Douglas Gregorc34897d2009-04-09 22:27:44 +00002370 return D;
2371}
2372
Douglas Gregorac8f2802009-04-10 17:25:41 +00002373QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002374 unsigned Quals = ID & 0x07;
2375 unsigned Index = ID >> 3;
2376
2377 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
2378 QualType T;
2379 switch ((pch::PredefinedTypeIDs)Index) {
2380 case pch::PREDEF_TYPE_NULL_ID: return QualType();
2381 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
2382 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
2383
2384 case pch::PREDEF_TYPE_CHAR_U_ID:
2385 case pch::PREDEF_TYPE_CHAR_S_ID:
2386 // FIXME: Check that the signedness of CharTy is correct!
2387 T = Context.CharTy;
2388 break;
2389
2390 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
2391 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
2392 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
2393 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
2394 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
2395 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
2396 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
2397 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
2398 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
2399 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
2400 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
2401 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
2402 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
2403 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
2404 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
2405 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
2406 }
2407
2408 assert(!T.isNull() && "Unknown predefined type");
2409 return T.getQualifiedType(Quals);
2410 }
2411
2412 Index -= pch::NUM_PREDEF_TYPE_IDS;
2413 if (!TypeAlreadyLoaded[Index]) {
2414 // Load the type from the PCH file.
2415 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
2416 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
2417 TypeAlreadyLoaded[Index] = true;
2418 }
2419
2420 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
2421}
2422
Douglas Gregorac8f2802009-04-10 17:25:41 +00002423Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002424 if (ID == 0)
2425 return 0;
2426
2427 unsigned Index = ID - 1;
2428 if (DeclAlreadyLoaded[Index])
2429 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
2430
2431 // Load the declaration from the PCH file.
2432 return ReadDeclRecord(DeclOffsets[Index], Index);
2433}
2434
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00002435Stmt *PCHReader::GetStmt(uint64_t Offset) {
2436 // Keep track of where we are in the stream, then jump back there
2437 // after reading this declaration.
2438 SavedStreamPosition SavedPosition(Stream);
2439
2440 Stream.JumpToBit(Offset);
2441 return ReadStmt();
2442}
2443
Douglas Gregorc34897d2009-04-09 22:27:44 +00002444bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00002445 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00002446 assert(DC->hasExternalLexicalStorage() &&
2447 "DeclContext has no lexical decls in storage");
2448 uint64_t Offset = DeclContextOffsets[DC].first;
2449 assert(Offset && "DeclContext has no lexical decls in storage");
2450
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002451 // Keep track of where we are in the stream, then jump back there
2452 // after reading this context.
2453 SavedStreamPosition SavedPosition(Stream);
2454
Douglas Gregorc34897d2009-04-09 22:27:44 +00002455 // Load the record containing all of the declarations lexically in
2456 // this context.
2457 Stream.JumpToBit(Offset);
2458 RecordData Record;
2459 unsigned Code = Stream.ReadCode();
2460 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002461 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002462 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
2463
2464 // Load all of the declaration IDs
2465 Decls.clear();
2466 Decls.insert(Decls.end(), Record.begin(), Record.end());
Douglas Gregoraf136d92009-04-22 22:34:57 +00002467 ++NumLexicalDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002468 return false;
2469}
2470
2471bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2472 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2473 assert(DC->hasExternalVisibleStorage() &&
2474 "DeclContext has no visible decls in storage");
2475 uint64_t Offset = DeclContextOffsets[DC].second;
2476 assert(Offset && "DeclContext has no visible decls in storage");
2477
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002478 // Keep track of where we are in the stream, then jump back there
2479 // after reading this context.
2480 SavedStreamPosition SavedPosition(Stream);
2481
Douglas Gregorc34897d2009-04-09 22:27:44 +00002482 // Load the record containing all of the declarations visible in
2483 // this context.
2484 Stream.JumpToBit(Offset);
2485 RecordData Record;
2486 unsigned Code = Stream.ReadCode();
2487 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002488 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002489 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2490 if (Record.size() == 0)
2491 return false;
2492
2493 Decls.clear();
2494
2495 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002496 while (Idx < Record.size()) {
2497 Decls.push_back(VisibleDeclaration());
2498 Decls.back().Name = ReadDeclarationName(Record, Idx);
2499
Douglas Gregorc34897d2009-04-09 22:27:44 +00002500 unsigned Size = Record[Idx++];
2501 llvm::SmallVector<unsigned, 4> & LoadedDecls
2502 = Decls.back().Declarations;
2503 LoadedDecls.reserve(Size);
2504 for (unsigned I = 0; I < Size; ++I)
2505 LoadedDecls.push_back(Record[Idx++]);
2506 }
2507
Douglas Gregoraf136d92009-04-22 22:34:57 +00002508 ++NumVisibleDeclContextsRead;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002509 return false;
2510}
2511
Douglas Gregor631f6c62009-04-14 00:24:19 +00002512void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor405b6432009-04-22 19:09:20 +00002513 this->Consumer = Consumer;
2514
Douglas Gregor631f6c62009-04-14 00:24:19 +00002515 if (!Consumer)
2516 return;
2517
2518 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2519 Decl *D = GetDecl(ExternalDefinitions[I]);
2520 DeclGroupRef DG(D);
2521 Consumer->HandleTopLevelDecl(DG);
2522 }
2523}
2524
Douglas Gregorc34897d2009-04-09 22:27:44 +00002525void PCHReader::PrintStats() {
2526 std::fprintf(stderr, "*** PCH Statistics:\n");
2527
2528 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2529 TypeAlreadyLoaded.end(),
2530 true);
2531 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2532 DeclAlreadyLoaded.end(),
2533 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002534 unsigned NumIdentifiersLoaded = 0;
2535 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2536 if ((IdentifierData[I] & 0x01) == 0)
2537 ++NumIdentifiersLoaded;
2538 }
2539
Douglas Gregorc34897d2009-04-09 22:27:44 +00002540 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2541 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002542 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002543 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2544 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002545 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2546 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2547 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2548 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002549 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2550 NumStatementsRead, TotalNumStatements,
2551 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002552 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
2553 NumMacrosRead, TotalNumMacros,
2554 ((float)NumMacrosRead/TotalNumMacros * 100));
Douglas Gregoraf136d92009-04-22 22:34:57 +00002555 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
2556 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
2557 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
2558 * 100));
2559 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
2560 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
2561 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
2562 * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002563 std::fprintf(stderr, "\n");
2564}
2565
Douglas Gregorc713da92009-04-21 22:25:48 +00002566void PCHReader::InitializeSema(Sema &S) {
2567 SemaObj = &S;
2568
Douglas Gregor2554cf22009-04-22 21:15:06 +00002569 // Makes sure any declarations that were deserialized "too early"
2570 // still get added to the identifier's declaration chains.
2571 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
2572 SemaObj->TUScope->AddDecl(Action::DeclPtrTy::make(PreloadedDecls[I]));
2573 SemaObj->IdResolver.AddDecl(PreloadedDecls[I]);
Douglas Gregorc713da92009-04-21 22:25:48 +00002574 }
Douglas Gregor2554cf22009-04-22 21:15:06 +00002575 PreloadedDecls.clear();
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002576
2577 // If there were any tentative definitions, deserialize them and add
2578 // them to Sema's table of tentative definitions.
2579 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
2580 VarDecl *Var = cast<VarDecl>(GetDecl(TentativeDefinitions[I]));
2581 SemaObj->TentativeDefinitions[Var->getDeclName()] = Var;
2582 }
Douglas Gregor062d9482009-04-22 22:18:58 +00002583
2584 // If there were any locally-scoped external declarations,
2585 // deserialize them and add them to Sema's table of locally-scoped
2586 // external declarations.
2587 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
2588 NamedDecl *D = cast<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
2589 SemaObj->LocallyScopedExternalDecls[D->getDeclName()] = D;
2590 }
Douglas Gregorc713da92009-04-21 22:25:48 +00002591}
2592
2593IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) {
2594 // Try to find this name within our on-disk hash table
2595 PCHIdentifierLookupTable *IdTable
2596 = (PCHIdentifierLookupTable *)IdentifierLookupTable;
2597 std::pair<const char*, unsigned> Key(NameStart, NameEnd - NameStart);
2598 PCHIdentifierLookupTable::iterator Pos = IdTable->find(Key);
2599 if (Pos == IdTable->end())
2600 return 0;
2601
2602 // Dereferencing the iterator has the effect of building the
2603 // IdentifierInfo node and populating it with the various
2604 // declarations it needs.
2605 return *Pos;
2606}
2607
2608void PCHReader::SetIdentifierInfo(unsigned ID, const IdentifierInfo *II) {
2609 assert(ID && "Non-zero identifier ID required");
2610 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(II);
2611}
2612
Chris Lattner29241862009-04-11 21:15:38 +00002613IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002614 if (ID == 0)
2615 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002616
Douglas Gregorc713da92009-04-21 22:25:48 +00002617 if (!IdentifierTableData || IdentifierData.empty()) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002618 Error("No identifier table in PCH file");
2619 return 0;
2620 }
Chris Lattner29241862009-04-11 21:15:38 +00002621
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002622 if (IdentifierData[ID - 1] & 0x01) {
Douglas Gregorff9a6092009-04-20 20:36:09 +00002623 uint64_t Offset = IdentifierData[ID - 1] >> 1;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002624 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Douglas Gregorc713da92009-04-21 22:25:48 +00002625 &Context.Idents.get(IdentifierTableData + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002626 }
Chris Lattner29241862009-04-11 21:15:38 +00002627
2628 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002629}
2630
2631DeclarationName
2632PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2633 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2634 switch (Kind) {
2635 case DeclarationName::Identifier:
2636 return DeclarationName(GetIdentifierInfo(Record, Idx));
2637
2638 case DeclarationName::ObjCZeroArgSelector:
2639 case DeclarationName::ObjCOneArgSelector:
2640 case DeclarationName::ObjCMultiArgSelector:
2641 assert(false && "Unable to de-serialize Objective-C selectors");
2642 break;
2643
2644 case DeclarationName::CXXConstructorName:
2645 return Context.DeclarationNames.getCXXConstructorName(
2646 GetType(Record[Idx++]));
2647
2648 case DeclarationName::CXXDestructorName:
2649 return Context.DeclarationNames.getCXXDestructorName(
2650 GetType(Record[Idx++]));
2651
2652 case DeclarationName::CXXConversionFunctionName:
2653 return Context.DeclarationNames.getCXXConversionFunctionName(
2654 GetType(Record[Idx++]));
2655
2656 case DeclarationName::CXXOperatorName:
2657 return Context.DeclarationNames.getCXXOperatorName(
2658 (OverloadedOperatorKind)Record[Idx++]);
2659
2660 case DeclarationName::CXXUsingDirective:
2661 return DeclarationName::getUsingDirectiveName();
2662 }
2663
2664 // Required to silence GCC warning
2665 return DeclarationName();
2666}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002667
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002668/// \brief Read an integral value
2669llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2670 unsigned BitWidth = Record[Idx++];
2671 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2672 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2673 Idx += NumWords;
2674 return Result;
2675}
2676
2677/// \brief Read a signed integral value
2678llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2679 bool isUnsigned = Record[Idx++];
2680 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2681}
2682
Douglas Gregore2f37202009-04-14 21:55:33 +00002683/// \brief Read a floating-point value
2684llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002685 return llvm::APFloat(ReadAPInt(Record, Idx));
2686}
2687
Douglas Gregor1c507882009-04-15 21:30:51 +00002688// \brief Read a string
2689std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2690 unsigned Len = Record[Idx++];
2691 std::string Result(&Record[Idx], &Record[Idx] + Len);
2692 Idx += Len;
2693 return Result;
2694}
2695
2696/// \brief Reads attributes from the current stream position.
2697Attr *PCHReader::ReadAttributes() {
2698 unsigned Code = Stream.ReadCode();
2699 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2700 "Expected unabbreviated record"); (void)Code;
2701
2702 RecordData Record;
2703 unsigned Idx = 0;
2704 unsigned RecCode = Stream.ReadRecord(Code, Record);
2705 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2706 (void)RecCode;
2707
2708#define SIMPLE_ATTR(Name) \
2709 case Attr::Name: \
2710 New = ::new (Context) Name##Attr(); \
2711 break
2712
2713#define STRING_ATTR(Name) \
2714 case Attr::Name: \
2715 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2716 break
2717
2718#define UNSIGNED_ATTR(Name) \
2719 case Attr::Name: \
2720 New = ::new (Context) Name##Attr(Record[Idx++]); \
2721 break
2722
2723 Attr *Attrs = 0;
2724 while (Idx < Record.size()) {
2725 Attr *New = 0;
2726 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2727 bool IsInherited = Record[Idx++];
2728
2729 switch (Kind) {
2730 STRING_ATTR(Alias);
2731 UNSIGNED_ATTR(Aligned);
2732 SIMPLE_ATTR(AlwaysInline);
2733 SIMPLE_ATTR(AnalyzerNoReturn);
2734 STRING_ATTR(Annotate);
2735 STRING_ATTR(AsmLabel);
2736
2737 case Attr::Blocks:
2738 New = ::new (Context) BlocksAttr(
2739 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2740 break;
2741
2742 case Attr::Cleanup:
2743 New = ::new (Context) CleanupAttr(
2744 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2745 break;
2746
2747 SIMPLE_ATTR(Const);
2748 UNSIGNED_ATTR(Constructor);
2749 SIMPLE_ATTR(DLLExport);
2750 SIMPLE_ATTR(DLLImport);
2751 SIMPLE_ATTR(Deprecated);
2752 UNSIGNED_ATTR(Destructor);
2753 SIMPLE_ATTR(FastCall);
2754
2755 case Attr::Format: {
2756 std::string Type = ReadString(Record, Idx);
2757 unsigned FormatIdx = Record[Idx++];
2758 unsigned FirstArg = Record[Idx++];
2759 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2760 break;
2761 }
2762
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002763 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002764
2765 case Attr::IBOutletKind:
2766 New = ::new (Context) IBOutletAttr();
2767 break;
2768
2769 SIMPLE_ATTR(NoReturn);
2770 SIMPLE_ATTR(NoThrow);
2771 SIMPLE_ATTR(Nodebug);
2772 SIMPLE_ATTR(Noinline);
2773
2774 case Attr::NonNull: {
2775 unsigned Size = Record[Idx++];
2776 llvm::SmallVector<unsigned, 16> ArgNums;
2777 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2778 Idx += Size;
2779 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2780 break;
2781 }
2782
2783 SIMPLE_ATTR(ObjCException);
2784 SIMPLE_ATTR(ObjCNSObject);
2785 SIMPLE_ATTR(Overloadable);
2786 UNSIGNED_ATTR(Packed);
2787 SIMPLE_ATTR(Pure);
2788 UNSIGNED_ATTR(Regparm);
2789 STRING_ATTR(Section);
2790 SIMPLE_ATTR(StdCall);
2791 SIMPLE_ATTR(TransparentUnion);
2792 SIMPLE_ATTR(Unavailable);
2793 SIMPLE_ATTR(Unused);
2794 SIMPLE_ATTR(Used);
2795
2796 case Attr::Visibility:
2797 New = ::new (Context) VisibilityAttr(
2798 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2799 break;
2800
2801 SIMPLE_ATTR(WarnUnusedResult);
2802 SIMPLE_ATTR(Weak);
2803 SIMPLE_ATTR(WeakImport);
2804 }
2805
2806 assert(New && "Unable to decode attribute?");
2807 New->setInherited(IsInherited);
2808 New->setNext(Attrs);
2809 Attrs = New;
2810 }
2811#undef UNSIGNED_ATTR
2812#undef STRING_ATTR
2813#undef SIMPLE_ATTR
2814
2815 // The list of attributes was built backwards. Reverse the list
2816 // before returning it.
2817 Attr *PrevAttr = 0, *NextAttr = 0;
2818 while (Attrs) {
2819 NextAttr = Attrs->getNext();
2820 Attrs->setNext(PrevAttr);
2821 PrevAttr = Attrs;
2822 Attrs = NextAttr;
2823 }
2824
2825 return PrevAttr;
2826}
2827
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002828Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002829 // Within the bitstream, expressions are stored in Reverse Polish
2830 // Notation, with each of the subexpressions preceding the
2831 // expression they are stored in. To evaluate expressions, we
2832 // continue reading expressions and placing them on the stack, with
2833 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002834 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002835 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002836 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002837 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002838 llvm::SmallVector<Stmt *, 16> StmtStack;
2839 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002840 Stmt::EmptyShell Empty;
2841
Douglas Gregora151ba42009-04-14 23:32:43 +00002842 while (true) {
2843 unsigned Code = Stream.ReadCode();
2844 if (Code == llvm::bitc::END_BLOCK) {
2845 if (Stream.ReadBlockEnd()) {
2846 Error("Error at end of Source Manager block");
2847 return 0;
2848 }
2849 break;
2850 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002851
Douglas Gregora151ba42009-04-14 23:32:43 +00002852 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2853 // No known subblocks, always skip them.
2854 Stream.ReadSubBlockID();
2855 if (Stream.SkipBlock()) {
2856 Error("Malformed block record");
2857 return 0;
2858 }
2859 continue;
2860 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002861
Douglas Gregora151ba42009-04-14 23:32:43 +00002862 if (Code == llvm::bitc::DEFINE_ABBREV) {
2863 Stream.ReadAbbrevRecord();
2864 continue;
2865 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002866
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002867 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002868 Idx = 0;
2869 Record.clear();
2870 bool Finished = false;
2871 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002872 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002873 Finished = true;
2874 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002875
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002876 case pch::STMT_NULL_PTR:
2877 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002878 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002879
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002880 case pch::STMT_NULL:
2881 S = new (Context) NullStmt(Empty);
2882 break;
2883
2884 case pch::STMT_COMPOUND:
2885 S = new (Context) CompoundStmt(Empty);
2886 break;
2887
2888 case pch::STMT_CASE:
2889 S = new (Context) CaseStmt(Empty);
2890 break;
2891
2892 case pch::STMT_DEFAULT:
2893 S = new (Context) DefaultStmt(Empty);
2894 break;
2895
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002896 case pch::STMT_LABEL:
2897 S = new (Context) LabelStmt(Empty);
2898 break;
2899
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002900 case pch::STMT_IF:
2901 S = new (Context) IfStmt(Empty);
2902 break;
2903
2904 case pch::STMT_SWITCH:
2905 S = new (Context) SwitchStmt(Empty);
2906 break;
2907
Douglas Gregora6b503f2009-04-17 00:16:09 +00002908 case pch::STMT_WHILE:
2909 S = new (Context) WhileStmt(Empty);
2910 break;
2911
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002912 case pch::STMT_DO:
2913 S = new (Context) DoStmt(Empty);
2914 break;
2915
2916 case pch::STMT_FOR:
2917 S = new (Context) ForStmt(Empty);
2918 break;
2919
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002920 case pch::STMT_GOTO:
2921 S = new (Context) GotoStmt(Empty);
2922 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002923
2924 case pch::STMT_INDIRECT_GOTO:
2925 S = new (Context) IndirectGotoStmt(Empty);
2926 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002927
Douglas Gregora6b503f2009-04-17 00:16:09 +00002928 case pch::STMT_CONTINUE:
2929 S = new (Context) ContinueStmt(Empty);
2930 break;
2931
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002932 case pch::STMT_BREAK:
2933 S = new (Context) BreakStmt(Empty);
2934 break;
2935
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002936 case pch::STMT_RETURN:
2937 S = new (Context) ReturnStmt(Empty);
2938 break;
2939
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002940 case pch::STMT_DECL:
2941 S = new (Context) DeclStmt(Empty);
2942 break;
2943
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002944 case pch::STMT_ASM:
2945 S = new (Context) AsmStmt(Empty);
2946 break;
2947
Douglas Gregora151ba42009-04-14 23:32:43 +00002948 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002949 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002950 break;
2951
2952 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002953 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002954 break;
2955
2956 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002957 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002958 break;
2959
2960 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002961 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002962 break;
2963
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002964 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002965 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002966 break;
2967
Douglas Gregor596e0932009-04-15 16:35:07 +00002968 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002969 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002970 Record[PCHStmtReader::NumExprFields + 1]);
2971 break;
2972
Douglas Gregora151ba42009-04-14 23:32:43 +00002973 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002974 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002975 break;
2976
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002977 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002978 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002979 break;
2980
Douglas Gregor12d74052009-04-15 15:58:59 +00002981 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002982 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002983 break;
2984
2985 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002986 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002987 break;
2988
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002989 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002990 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002991 break;
2992
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002993 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002994 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002995 break;
2996
2997 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002998 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002999 break;
3000
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003001 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003002 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003003 break;
3004
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003005 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003006 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003007 break;
3008
3009 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003010 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00003011 break;
3012
Douglas Gregora151ba42009-04-14 23:32:43 +00003013 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003014 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00003015 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003016
3017 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003018 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00003019 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00003020
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003021 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003022 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00003023 break;
3024
Douglas Gregorec0b8292009-04-15 23:02:49 +00003025 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003026 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003027 break;
3028
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003029 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003030 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003031 break;
3032
3033 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003034 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003035 Record[PCHStmtReader::NumExprFields] - 1);
3036
3037 break;
3038
3039 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003040 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00003041 break;
3042
Douglas Gregorec0b8292009-04-15 23:02:49 +00003043 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003044 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00003045 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00003046
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003047 case pch::EXPR_ADDR_LABEL:
3048 S = new (Context) AddrLabelExpr(Empty);
3049 break;
3050
Douglas Gregoreca12f62009-04-17 19:05:30 +00003051 case pch::EXPR_STMT:
3052 S = new (Context) StmtExpr(Empty);
3053 break;
3054
Douglas Gregor209d4622009-04-15 23:33:31 +00003055 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003056 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003057 break;
3058
3059 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003060 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003061 break;
3062
3063 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003064 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00003065 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00003066
3067 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003068 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003069 break;
3070
Douglas Gregore246b742009-04-17 19:21:43 +00003071 case pch::EXPR_BLOCK:
3072 S = new (Context) BlockExpr(Empty);
3073 break;
3074
Douglas Gregor725e94b2009-04-16 00:01:45 +00003075 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003076 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00003077 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003078
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003079 case pch::EXPR_OBJC_STRING_LITERAL:
3080 S = new (Context) ObjCStringLiteral(Empty);
3081 break;
Chris Lattner80f83c62009-04-22 05:57:30 +00003082 case pch::EXPR_OBJC_ENCODE:
3083 S = new (Context) ObjCEncodeExpr(Empty);
3084 break;
Chris Lattnerc49bbe72009-04-22 06:29:42 +00003085 case pch::EXPR_OBJC_SELECTOR_EXPR:
3086 S = new (Context) ObjCSelectorExpr(Empty);
3087 break;
3088 case pch::EXPR_OBJC_PROTOCOL_EXPR:
3089 S = new (Context) ObjCProtocolExpr(Empty);
3090 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00003091 }
3092
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003093 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00003094 if (Finished)
3095 break;
3096
Douglas Gregor456e0952009-04-17 22:13:46 +00003097 ++NumStatementsRead;
3098
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003099 if (S) {
3100 unsigned NumSubStmts = Reader.Visit(S);
3101 while (NumSubStmts > 0) {
3102 StmtStack.pop_back();
3103 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00003104 }
3105 }
3106
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003107 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003108 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003109 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003110 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00003111 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00003112 return StmtStack.back();
3113}
3114
3115Expr *PCHReader::ReadExpr() {
3116 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00003117}
3118
Douglas Gregor179cfb12009-04-10 20:39:37 +00003119DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00003120 return Diag(SourceLocation(), DiagID);
3121}
3122
3123DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
3124 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00003125 Context.getSourceManager()),
3126 DiagID);
3127}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003128
Douglas Gregorc713da92009-04-21 22:25:48 +00003129/// \brief Retrieve the identifier table associated with the
3130/// preprocessor.
3131IdentifierTable &PCHReader::getIdentifierTable() {
3132 return PP.getIdentifierTable();
3133}
3134
Douglas Gregor9c4782a2009-04-17 00:04:06 +00003135/// \brief Record that the given ID maps to the given switch-case
3136/// statement.
3137void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
3138 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
3139 SwitchCaseStmts[ID] = SC;
3140}
3141
3142/// \brief Retrieve the switch-case statement with the given ID.
3143SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
3144 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
3145 return SwitchCaseStmts[ID];
3146}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003147
3148/// \brief Record that the given label statement has been
3149/// deserialized and has the given ID.
3150void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
3151 assert(LabelStmts.find(ID) == LabelStmts.end() &&
3152 "Deserialized label twice");
3153 LabelStmts[ID] = S;
3154
3155 // If we've already seen any goto statements that point to this
3156 // label, resolve them now.
3157 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
3158 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
3159 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
3160 Goto->second->setLabel(S);
3161 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003162
3163 // If we've already seen any address-label statements that point to
3164 // this label, resolve them now.
3165 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
3166 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
3167 = UnresolvedAddrLabelExprs.equal_range(ID);
3168 for (AddrLabelIter AddrLabel = AddrLabels.first;
3169 AddrLabel != AddrLabels.second; ++AddrLabel)
3170 AddrLabel->second->setLabel(S);
3171 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00003172}
3173
3174/// \brief Set the label of the given statement to the label
3175/// identified by ID.
3176///
3177/// Depending on the order in which the label and other statements
3178/// referencing that label occur, this operation may complete
3179/// immediately (updating the statement) or it may queue the
3180/// statement to be back-patched later.
3181void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
3182 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3183 if (Label != LabelStmts.end()) {
3184 // We've already seen this label, so set the label of the goto and
3185 // we're done.
3186 S->setLabel(Label->second);
3187 } else {
3188 // We haven't seen this label yet, so add this goto to the set of
3189 // unresolved goto statements.
3190 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
3191 }
3192}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00003193
3194/// \brief Set the label of the given expression to the label
3195/// identified by ID.
3196///
3197/// Depending on the order in which the label and other statements
3198/// referencing that label occur, this operation may complete
3199/// immediately (updating the statement) or it may queue the
3200/// statement to be back-patched later.
3201void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
3202 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
3203 if (Label != LabelStmts.end()) {
3204 // We've already seen this label, so set the label of the
3205 // label-address expression and we're done.
3206 S->setLabel(Label->second);
3207 } else {
3208 // We haven't seen this label yet, so add this label-address
3209 // expression to the set of unresolved label-address expressions.
3210 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
3211 }
3212}