blob: 0f208fb7ac9d1add2b21edc84f68054bb1cd7375 [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 Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000107}
108
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor982365e2009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregorc34897d2009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
142 // FIXME: function body
143 FD->setPreviousDeclaration(
144 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
145 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
146 FD->setInline(Record[Idx++]);
147 FD->setVirtual(Record[Idx++]);
148 FD->setPure(Record[Idx++]);
149 FD->setInheritedPrototype(Record[Idx++]);
150 FD->setHasPrototype(Record[Idx++]);
151 FD->setDeleted(Record[Idx++]);
152 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
153 unsigned NumParams = Record[Idx++];
154 llvm::SmallVector<ParmVarDecl *, 16> Params;
155 Params.reserve(NumParams);
156 for (unsigned I = 0; I != NumParams; ++I)
157 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
158 FD->setParams(Reader.getContext(), &Params[0], NumParams);
159}
160
Douglas Gregor982365e2009-04-13 21:20:57 +0000161void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
162 VisitValueDecl(FD);
163 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000164 if (Record[Idx++])
165 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000166}
167
Douglas Gregorc34897d2009-04-09 22:27:44 +0000168void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
169 VisitValueDecl(VD);
170 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
171 VD->setThreadSpecified(Record[Idx++]);
172 VD->setCXXDirectInitializer(Record[Idx++]);
173 VD->setDeclaredInCondition(Record[Idx++]);
174 VD->setPreviousDeclaration(
175 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
176 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000177 if (Record[Idx++])
178 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000179}
180
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000181void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
182 VisitVarDecl(PD);
183 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000184 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000185}
186
187void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
188 VisitParmVarDecl(PD);
189 PD->setOriginalType(Reader.GetType(Record[Idx++]));
190}
191
Douglas Gregor2a491792009-04-13 22:49:25 +0000192void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
193 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000194 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000195}
196
197void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
198 VisitDecl(BD);
199 unsigned NumParams = Record[Idx++];
200 llvm::SmallVector<ParmVarDecl *, 16> Params;
201 Params.reserve(NumParams);
202 for (unsigned I = 0; I != NumParams; ++I)
203 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
204 BD->setParams(Reader.getContext(), &Params[0], NumParams);
205}
206
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207std::pair<uint64_t, uint64_t>
208PCHDeclReader::VisitDeclContext(DeclContext *DC) {
209 uint64_t LexicalOffset = Record[Idx++];
210 uint64_t VisibleOffset = 0;
211 if (DC->getPrimaryContext() == DC)
212 VisibleOffset = Record[Idx++];
213 return std::make_pair(LexicalOffset, VisibleOffset);
214}
215
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000216//===----------------------------------------------------------------------===//
217// Statement/expression deserialization
218//===----------------------------------------------------------------------===//
219namespace {
220 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000221 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000222 PCHReader &Reader;
223 const PCHReader::RecordData &Record;
224 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000225 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000226
227 public:
228 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000229 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
230 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000231
Douglas Gregor596e0932009-04-15 16:35:07 +0000232 /// \brief The number of record fields required for the Expr class
233 /// itself.
234 static const unsigned NumExprFields = 3;
235
Douglas Gregora151ba42009-04-14 23:32:43 +0000236 // Each of the Visit* functions reads in part of the expression
237 // from the given record and the current expression stack, then
238 // return the total number of operands that it read from the
239 // expression stack.
240
241 unsigned VisitExpr(Expr *E);
242 unsigned VisitPredefinedExpr(PredefinedExpr *E);
243 unsigned VisitDeclRefExpr(DeclRefExpr *E);
244 unsigned VisitIntegerLiteral(IntegerLiteral *E);
245 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000246 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000247 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000248 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000249 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000250 unsigned VisitUnaryOperator(UnaryOperator *E);
251 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000252 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000253 unsigned VisitCallExpr(CallExpr *E);
254 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000255 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000256 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000257 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
258 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000259 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000260 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
261 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000262 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000263 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000264 unsigned VisitInitListExpr(InitListExpr *E);
265 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
266 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000267 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000268 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
269 unsigned VisitChooseExpr(ChooseExpr *E);
270 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000271 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
272 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000273 };
274}
275
Douglas Gregora151ba42009-04-14 23:32:43 +0000276unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000277 E->setType(Reader.GetType(Record[Idx++]));
278 E->setTypeDependent(Record[Idx++]);
279 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000280 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000281 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000282}
283
Douglas Gregora151ba42009-04-14 23:32:43 +0000284unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000285 VisitExpr(E);
286 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
287 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000288 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000289}
290
Douglas Gregora151ba42009-04-14 23:32:43 +0000291unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000292 VisitExpr(E);
293 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
294 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000295 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000296}
297
Douglas Gregora151ba42009-04-14 23:32:43 +0000298unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000299 VisitExpr(E);
300 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
301 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000302 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000303}
304
Douglas Gregora151ba42009-04-14 23:32:43 +0000305unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000306 VisitExpr(E);
307 E->setValue(Reader.ReadAPFloat(Record, Idx));
308 E->setExact(Record[Idx++]);
309 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000310 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000311}
312
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000313unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
314 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000315 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000316 return 1;
317}
318
Douglas Gregor596e0932009-04-15 16:35:07 +0000319unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
320 VisitExpr(E);
321 unsigned Len = Record[Idx++];
322 assert(Record[Idx] == E->getNumConcatenated() &&
323 "Wrong number of concatenated tokens!");
324 ++Idx;
325 E->setWide(Record[Idx++]);
326
327 // Read string data
328 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
329 E->setStrData(Reader.getContext(), &Str[0], Len);
330 Idx += Len;
331
332 // Read source locations
333 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
334 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
335
336 return 0;
337}
338
Douglas Gregora151ba42009-04-14 23:32:43 +0000339unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000340 VisitExpr(E);
341 E->setValue(Record[Idx++]);
342 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
343 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000344 return 0;
345}
346
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000347unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
348 VisitExpr(E);
349 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
350 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000351 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000352 return 1;
353}
354
Douglas Gregor12d74052009-04-15 15:58:59 +0000355unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
356 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000357 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000358 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
359 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
360 return 1;
361}
362
363unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
364 VisitExpr(E);
365 E->setSizeof(Record[Idx++]);
366 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000367 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000368 ++Idx;
369 } else {
370 E->setArgument(Reader.GetType(Record[Idx++]));
371 }
372 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
373 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
374 return E->isArgumentType()? 0 : 1;
375}
376
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000377unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
378 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000379 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
380 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000381 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
382 return 2;
383}
384
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000385unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
386 VisitExpr(E);
387 E->setNumArgs(Reader.getContext(), Record[Idx++]);
388 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000389 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000390 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000391 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000392 return E->getNumArgs() + 1;
393}
394
395unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
396 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000397 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000398 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
399 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
400 E->setArrow(Record[Idx++]);
401 return 1;
402}
403
Douglas Gregora151ba42009-04-14 23:32:43 +0000404unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
405 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000406 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000407 return 1;
408}
409
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000410unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
411 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000412 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
413 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000414 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
415 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
416 return 2;
417}
418
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000419unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
420 VisitBinaryOperator(E);
421 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
422 E->setComputationResultType(Reader.GetType(Record[Idx++]));
423 return 2;
424}
425
426unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
427 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000428 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
429 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
430 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000431 return 3;
432}
433
Douglas Gregora151ba42009-04-14 23:32:43 +0000434unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
435 VisitCastExpr(E);
436 E->setLvalueCast(Record[Idx++]);
437 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000438}
439
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000440unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
441 VisitCastExpr(E);
442 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
443 return 1;
444}
445
446unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
447 VisitExplicitCastExpr(E);
448 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
449 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
450 return 1;
451}
452
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000453unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
454 VisitExpr(E);
455 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000456 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000457 E->setFileScope(Record[Idx++]);
458 return 1;
459}
460
Douglas Gregorec0b8292009-04-15 23:02:49 +0000461unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
462 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000463 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000464 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
465 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
466 return 1;
467}
468
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000469unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
470 VisitExpr(E);
471 unsigned NumInits = Record[Idx++];
472 E->reserveInits(NumInits);
473 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000474 E->updateInit(I,
475 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
476 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000477 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
478 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
479 E->setInitializedFieldInUnion(
480 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
481 E->sawArrayRangeDesignator(Record[Idx++]);
482 return NumInits + 1;
483}
484
485unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
486 typedef DesignatedInitExpr::Designator Designator;
487
488 VisitExpr(E);
489 unsigned NumSubExprs = Record[Idx++];
490 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
491 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000492 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000493 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
494 E->setGNUSyntax(Record[Idx++]);
495
496 llvm::SmallVector<Designator, 4> Designators;
497 while (Idx < Record.size()) {
498 switch ((pch::DesignatorTypes)Record[Idx++]) {
499 case pch::DESIG_FIELD_DECL: {
500 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
501 SourceLocation DotLoc
502 = SourceLocation::getFromRawEncoding(Record[Idx++]);
503 SourceLocation FieldLoc
504 = SourceLocation::getFromRawEncoding(Record[Idx++]);
505 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
506 FieldLoc));
507 Designators.back().setField(Field);
508 break;
509 }
510
511 case pch::DESIG_FIELD_NAME: {
512 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
513 SourceLocation DotLoc
514 = SourceLocation::getFromRawEncoding(Record[Idx++]);
515 SourceLocation FieldLoc
516 = SourceLocation::getFromRawEncoding(Record[Idx++]);
517 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
518 break;
519 }
520
521 case pch::DESIG_ARRAY: {
522 unsigned Index = Record[Idx++];
523 SourceLocation LBracketLoc
524 = SourceLocation::getFromRawEncoding(Record[Idx++]);
525 SourceLocation RBracketLoc
526 = SourceLocation::getFromRawEncoding(Record[Idx++]);
527 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
528 break;
529 }
530
531 case pch::DESIG_ARRAY_RANGE: {
532 unsigned Index = Record[Idx++];
533 SourceLocation LBracketLoc
534 = SourceLocation::getFromRawEncoding(Record[Idx++]);
535 SourceLocation EllipsisLoc
536 = SourceLocation::getFromRawEncoding(Record[Idx++]);
537 SourceLocation RBracketLoc
538 = SourceLocation::getFromRawEncoding(Record[Idx++]);
539 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
540 RBracketLoc));
541 break;
542 }
543 }
544 }
545 E->setDesignators(&Designators[0], Designators.size());
546
547 return NumSubExprs;
548}
549
550unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
551 VisitExpr(E);
552 return 0;
553}
554
Douglas Gregorec0b8292009-04-15 23:02:49 +0000555unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
556 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000557 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000558 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
559 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
560 return 1;
561}
562
Douglas Gregor209d4622009-04-15 23:33:31 +0000563unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
564 VisitExpr(E);
565 E->setArgType1(Reader.GetType(Record[Idx++]));
566 E->setArgType2(Reader.GetType(Record[Idx++]));
567 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
568 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
569 return 0;
570}
571
572unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
573 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000574 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
575 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
576 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000577 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
578 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
579 return 3;
580}
581
582unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
583 VisitExpr(E);
584 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
585 return 0;
586}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000587
Douglas Gregor725e94b2009-04-16 00:01:45 +0000588unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
589 VisitExpr(E);
590 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000591 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000592 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
593 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 return NumExprs;
595}
596
597unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
598 VisitExpr(E);
599 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
600 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
601 E->setByRef(Record[Idx++]);
602 return 0;
603}
604
Douglas Gregorc34897d2009-04-09 22:27:44 +0000605// FIXME: use the diagnostics machinery
606static bool Error(const char *Str) {
607 std::fprintf(stderr, "%s\n", Str);
608 return true;
609}
610
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000611/// \brief Check the contents of the predefines buffer against the
612/// contents of the predefines buffer used to build the PCH file.
613///
614/// The contents of the two predefines buffers should be the same. If
615/// not, then some command-line option changed the preprocessor state
616/// and we must reject the PCH file.
617///
618/// \param PCHPredef The start of the predefines buffer in the PCH
619/// file.
620///
621/// \param PCHPredefLen The length of the predefines buffer in the PCH
622/// file.
623///
624/// \param PCHBufferID The FileID for the PCH predefines buffer.
625///
626/// \returns true if there was a mismatch (in which case the PCH file
627/// should be ignored), or false otherwise.
628bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
629 unsigned PCHPredefLen,
630 FileID PCHBufferID) {
631 const char *Predef = PP.getPredefines().c_str();
632 unsigned PredefLen = PP.getPredefines().size();
633
634 // If the two predefines buffers compare equal, we're done!.
635 if (PredefLen == PCHPredefLen &&
636 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
637 return false;
638
639 // The predefines buffers are different. Produce a reasonable
640 // diagnostic showing where they are different.
641
642 // The source locations (potentially in the two different predefines
643 // buffers)
644 SourceLocation Loc1, Loc2;
645 SourceManager &SourceMgr = PP.getSourceManager();
646
647 // Create a source buffer for our predefines string, so
648 // that we can build a diagnostic that points into that
649 // source buffer.
650 FileID BufferID;
651 if (Predef && Predef[0]) {
652 llvm::MemoryBuffer *Buffer
653 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
654 "<built-in>");
655 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
656 }
657
658 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
659 std::pair<const char *, const char *> Locations
660 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
661
662 if (Locations.first != Predef + MinLen) {
663 // We found the location in the two buffers where there is a
664 // difference. Form source locations to point there (in both
665 // buffers).
666 unsigned Offset = Locations.first - Predef;
667 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
668 .getFileLocWithOffset(Offset);
669 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
670 .getFileLocWithOffset(Offset);
671 } else if (PredefLen > PCHPredefLen) {
672 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
673 .getFileLocWithOffset(MinLen);
674 } else {
675 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
676 .getFileLocWithOffset(MinLen);
677 }
678
679 Diag(Loc1, diag::warn_pch_preprocessor);
680 if (Loc2.isValid())
681 Diag(Loc2, diag::note_predef_in_pch);
682 Diag(diag::note_ignoring_pch) << FileName;
683 return true;
684}
685
Douglas Gregor635f97f2009-04-13 16:31:14 +0000686/// \brief Read the line table in the source manager block.
687/// \returns true if ther was an error.
688static bool ParseLineTable(SourceManager &SourceMgr,
689 llvm::SmallVectorImpl<uint64_t> &Record) {
690 unsigned Idx = 0;
691 LineTableInfo &LineTable = SourceMgr.getLineTable();
692
693 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000694 std::map<int, int> FileIDs;
695 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000696 // Extract the file name
697 unsigned FilenameLen = Record[Idx++];
698 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
699 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000700 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
701 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000702 }
703
704 // Parse the line entries
705 std::vector<LineEntry> Entries;
706 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000707 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000708
709 // Extract the line entries
710 unsigned NumEntries = Record[Idx++];
711 Entries.clear();
712 Entries.reserve(NumEntries);
713 for (unsigned I = 0; I != NumEntries; ++I) {
714 unsigned FileOffset = Record[Idx++];
715 unsigned LineNo = Record[Idx++];
716 int FilenameID = Record[Idx++];
717 SrcMgr::CharacteristicKind FileKind
718 = (SrcMgr::CharacteristicKind)Record[Idx++];
719 unsigned IncludeOffset = Record[Idx++];
720 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
721 FileKind, IncludeOffset));
722 }
723 LineTable.AddEntry(FID, Entries);
724 }
725
726 return false;
727}
728
Douglas Gregorab1cef72009-04-10 03:52:48 +0000729/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000730PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000731 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000732 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
733 Error("Malformed source manager block record");
734 return Failure;
735 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000736
737 SourceManager &SourceMgr = Context.getSourceManager();
738 RecordData Record;
739 while (true) {
740 unsigned Code = Stream.ReadCode();
741 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000742 if (Stream.ReadBlockEnd()) {
743 Error("Error at end of Source Manager block");
744 return Failure;
745 }
746
747 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000748 }
749
750 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
751 // No known subblocks, always skip them.
752 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000753 if (Stream.SkipBlock()) {
754 Error("Malformed block record");
755 return Failure;
756 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000757 continue;
758 }
759
760 if (Code == llvm::bitc::DEFINE_ABBREV) {
761 Stream.ReadAbbrevRecord();
762 continue;
763 }
764
765 // Read a record.
766 const char *BlobStart;
767 unsigned BlobLen;
768 Record.clear();
769 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
770 default: // Default behavior: ignore.
771 break;
772
773 case pch::SM_SLOC_FILE_ENTRY: {
774 // FIXME: We would really like to delay the creation of this
775 // FileEntry until it is actually required, e.g., when producing
776 // a diagnostic with a source location in this file.
777 const FileEntry *File
778 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
779 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000780 FileID ID = SourceMgr.createFileID(File,
781 SourceLocation::getFromRawEncoding(Record[1]),
782 (CharacteristicKind)Record[2]);
783 if (Record[3])
784 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
785 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000786 break;
787 }
788
789 case pch::SM_SLOC_BUFFER_ENTRY: {
790 const char *Name = BlobStart;
791 unsigned Code = Stream.ReadCode();
792 Record.clear();
793 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
794 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000795 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000796 llvm::MemoryBuffer *Buffer
797 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
798 BlobStart + BlobLen - 1,
799 Name);
800 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
801
802 if (strcmp(Name, "<built-in>") == 0
803 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
804 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000805 break;
806 }
807
808 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
809 SourceLocation SpellingLoc
810 = SourceLocation::getFromRawEncoding(Record[1]);
811 SourceMgr.createInstantiationLoc(
812 SpellingLoc,
813 SourceLocation::getFromRawEncoding(Record[2]),
814 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000815 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000816 break;
817 }
818
Chris Lattnere1be6022009-04-14 23:22:57 +0000819 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000820 if (ParseLineTable(SourceMgr, Record))
821 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000822 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000823 }
824 }
825}
826
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000827bool PCHReader::ReadPreprocessorBlock() {
828 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
829 return Error("Malformed preprocessor block record");
830
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000831 RecordData Record;
832 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
833 MacroInfo *LastMacro = 0;
834
835 while (true) {
836 unsigned Code = Stream.ReadCode();
837 switch (Code) {
838 case llvm::bitc::END_BLOCK:
839 if (Stream.ReadBlockEnd())
840 return Error("Error at end of preprocessor block");
841 return false;
842
843 case llvm::bitc::ENTER_SUBBLOCK:
844 // No known subblocks, always skip them.
845 Stream.ReadSubBlockID();
846 if (Stream.SkipBlock())
847 return Error("Malformed block record");
848 continue;
849
850 case llvm::bitc::DEFINE_ABBREV:
851 Stream.ReadAbbrevRecord();
852 continue;
853 default: break;
854 }
855
856 // Read a record.
857 Record.clear();
858 pch::PreprocessorRecordTypes RecType =
859 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
860 switch (RecType) {
861 default: // Default behavior: ignore unknown records.
862 break;
Chris Lattner4b21c202009-04-13 01:29:17 +0000863 case pch::PP_COUNTER_VALUE:
864 if (!Record.empty())
865 PP.setCounterValue(Record[0]);
866 break;
867
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000868 case pch::PP_MACRO_OBJECT_LIKE:
869 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +0000870 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
871 if (II == 0)
872 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000873 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
874 bool isUsed = Record[2];
875
876 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
877 MI->setIsUsed(isUsed);
878
879 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
880 // Decode function-like macro info.
881 bool isC99VarArgs = Record[3];
882 bool isGNUVarArgs = Record[4];
883 MacroArgs.clear();
884 unsigned NumArgs = Record[5];
885 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +0000886 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000887
888 // Install function-like macro info.
889 MI->setIsFunctionLike();
890 if (isC99VarArgs) MI->setIsC99Varargs();
891 if (isGNUVarArgs) MI->setIsGNUVarargs();
892 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
893 PP.getPreprocessorAllocator());
894 }
895
896 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000897 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000898
899 // Remember that we saw this macro last so that we add the tokens that
900 // form its body to it.
901 LastMacro = MI;
902 break;
903 }
904
905 case pch::PP_TOKEN: {
906 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
907 // pretend we didn't see this.
908 if (LastMacro == 0) break;
909
910 Token Tok;
911 Tok.startToken();
912 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
913 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +0000914 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
915 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000916 Tok.setKind((tok::TokenKind)Record[3]);
917 Tok.setFlag((Token::TokenFlags)Record[4]);
918 LastMacro->AddTokenToBody(Tok);
919 break;
920 }
921 }
922 }
923}
924
Douglas Gregor179cfb12009-04-10 20:39:37 +0000925PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
926 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
927 Error("Malformed block record");
928 return Failure;
929 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000930
Chris Lattner29241862009-04-11 21:15:38 +0000931 uint64_t PreprocessorBlockBit = 0;
932
Douglas Gregorc34897d2009-04-09 22:27:44 +0000933 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +0000934 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000935 while (!Stream.AtEndOfStream()) {
936 unsigned Code = Stream.ReadCode();
937 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +0000938 // If we saw the preprocessor block, read it now.
939 if (PreprocessorBlockBit) {
940 uint64_t SavedPos = Stream.GetCurrentBitNo();
941 Stream.JumpToBit(PreprocessorBlockBit);
942 if (ReadPreprocessorBlock()) {
943 Error("Malformed preprocessor block");
944 return Failure;
945 }
946 Stream.JumpToBit(SavedPos);
947 }
948
Douglas Gregor179cfb12009-04-10 20:39:37 +0000949 if (Stream.ReadBlockEnd()) {
950 Error("Error at end of module block");
951 return Failure;
952 }
Chris Lattner29241862009-04-11 21:15:38 +0000953
Douglas Gregor179cfb12009-04-10 20:39:37 +0000954 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000955 }
956
957 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
958 switch (Stream.ReadSubBlockID()) {
959 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
960 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
961 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000962 if (Stream.SkipBlock()) {
963 Error("Malformed block record");
964 return Failure;
965 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000966 break;
967
Chris Lattner29241862009-04-11 21:15:38 +0000968 case pch::PREPROCESSOR_BLOCK_ID:
969 // Skip the preprocessor block for now, but remember where it is. We
970 // want to read it in after the identifier table.
971 if (PreprocessorBlockBit) {
972 Error("Multiple preprocessor blocks found.");
973 return Failure;
974 }
975 PreprocessorBlockBit = Stream.GetCurrentBitNo();
976 if (Stream.SkipBlock()) {
977 Error("Malformed block record");
978 return Failure;
979 }
980 break;
981
Douglas Gregorab1cef72009-04-10 03:52:48 +0000982 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000983 switch (ReadSourceManagerBlock()) {
984 case Success:
985 break;
986
987 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000988 Error("Malformed source manager block");
989 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000990
991 case IgnorePCH:
992 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000993 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000994 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000995 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000996 continue;
997 }
998
999 if (Code == llvm::bitc::DEFINE_ABBREV) {
1000 Stream.ReadAbbrevRecord();
1001 continue;
1002 }
1003
1004 // Read and process a record.
1005 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001006 const char *BlobStart = 0;
1007 unsigned BlobLen = 0;
1008 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1009 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001010 default: // Default behavior: ignore.
1011 break;
1012
1013 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001014 if (!TypeOffsets.empty()) {
1015 Error("Duplicate TYPE_OFFSET record in PCH file");
1016 return Failure;
1017 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001018 TypeOffsets.swap(Record);
1019 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1020 break;
1021
1022 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001023 if (!DeclOffsets.empty()) {
1024 Error("Duplicate DECL_OFFSET record in PCH file");
1025 return Failure;
1026 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001027 DeclOffsets.swap(Record);
1028 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1029 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001030
1031 case pch::LANGUAGE_OPTIONS:
1032 if (ParseLanguageOptions(Record))
1033 return IgnorePCH;
1034 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001035
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001036 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001037 std::string TargetTriple(BlobStart, BlobLen);
1038 if (TargetTriple != Context.Target.getTargetTriple()) {
1039 Diag(diag::warn_pch_target_triple)
1040 << TargetTriple << Context.Target.getTargetTriple();
1041 Diag(diag::note_ignoring_pch) << FileName;
1042 return IgnorePCH;
1043 }
1044 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001045 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001046
1047 case pch::IDENTIFIER_TABLE:
1048 IdentifierTable = BlobStart;
1049 break;
1050
1051 case pch::IDENTIFIER_OFFSET:
1052 if (!IdentifierData.empty()) {
1053 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1054 return Failure;
1055 }
1056 IdentifierData.swap(Record);
1057#ifndef NDEBUG
1058 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1059 if ((IdentifierData[I] & 0x01) == 0) {
1060 Error("Malformed identifier table in the precompiled header");
1061 return Failure;
1062 }
1063 }
1064#endif
1065 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001066
1067 case pch::EXTERNAL_DEFINITIONS:
1068 if (!ExternalDefinitions.empty()) {
1069 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1070 return Failure;
1071 }
1072 ExternalDefinitions.swap(Record);
1073 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001074 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001075 }
1076
Douglas Gregor179cfb12009-04-10 20:39:37 +00001077 Error("Premature end of bitstream");
1078 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001079}
1080
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001081PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001082 // Set the PCH file name.
1083 this->FileName = FileName;
1084
Douglas Gregorc34897d2009-04-09 22:27:44 +00001085 // Open the PCH file.
1086 std::string ErrStr;
1087 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001088 if (!Buffer) {
1089 Error(ErrStr.c_str());
1090 return IgnorePCH;
1091 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001092
1093 // Initialize the stream
1094 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1095 (const unsigned char *)Buffer->getBufferEnd());
1096
1097 // Sniff for the signature.
1098 if (Stream.Read(8) != 'C' ||
1099 Stream.Read(8) != 'P' ||
1100 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001101 Stream.Read(8) != 'H') {
1102 Error("Not a PCH file");
1103 return IgnorePCH;
1104 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001105
1106 // We expect a number of well-defined blocks, though we don't necessarily
1107 // need to understand them all.
1108 while (!Stream.AtEndOfStream()) {
1109 unsigned Code = Stream.ReadCode();
1110
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001111 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1112 Error("Invalid record at top-level");
1113 return Failure;
1114 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001115
1116 unsigned BlockID = Stream.ReadSubBlockID();
1117
1118 // We only know the PCH subblock ID.
1119 switch (BlockID) {
1120 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001121 if (Stream.ReadBlockInfoBlock()) {
1122 Error("Malformed BlockInfoBlock");
1123 return Failure;
1124 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001125 break;
1126 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001127 switch (ReadPCHBlock()) {
1128 case Success:
1129 break;
1130
1131 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001132 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001133
1134 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001135 // FIXME: We could consider reading through to the end of this
1136 // PCH block, skipping subblocks, to see if there are other
1137 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001138 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001139 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001140 break;
1141 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001142 if (Stream.SkipBlock()) {
1143 Error("Malformed block record");
1144 return Failure;
1145 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001146 break;
1147 }
1148 }
1149
1150 // Load the translation unit declaration
1151 ReadDeclRecord(DeclOffsets[0], 0);
1152
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001153 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001154}
1155
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001156namespace {
1157 /// \brief Helper class that saves the current stream position and
1158 /// then restores it when destroyed.
1159 struct VISIBILITY_HIDDEN SavedStreamPosition {
1160 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001161 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001162
1163 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001164 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001165 }
1166
1167 private:
1168 llvm::BitstreamReader &Stream;
1169 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001170 };
1171}
1172
Douglas Gregor179cfb12009-04-10 20:39:37 +00001173/// \brief Parse the record that corresponds to a LangOptions data
1174/// structure.
1175///
1176/// This routine compares the language options used to generate the
1177/// PCH file against the language options set for the current
1178/// compilation. For each option, we classify differences between the
1179/// two compiler states as either "benign" or "important". Benign
1180/// differences don't matter, and we accept them without complaint
1181/// (and without modifying the language options). Differences between
1182/// the states for important options cause the PCH file to be
1183/// unusable, so we emit a warning and return true to indicate that
1184/// there was an error.
1185///
1186/// \returns true if the PCH file is unacceptable, false otherwise.
1187bool PCHReader::ParseLanguageOptions(
1188 const llvm::SmallVectorImpl<uint64_t> &Record) {
1189 const LangOptions &LangOpts = Context.getLangOptions();
1190#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1191#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1192 if (Record[Idx] != LangOpts.Option) { \
1193 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1194 Diag(diag::note_ignoring_pch) << FileName; \
1195 return true; \
1196 } \
1197 ++Idx
1198
1199 unsigned Idx = 0;
1200 PARSE_LANGOPT_BENIGN(Trigraphs);
1201 PARSE_LANGOPT_BENIGN(BCPLComment);
1202 PARSE_LANGOPT_BENIGN(DollarIdents);
1203 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1204 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1205 PARSE_LANGOPT_BENIGN(ImplicitInt);
1206 PARSE_LANGOPT_BENIGN(Digraphs);
1207 PARSE_LANGOPT_BENIGN(HexFloats);
1208 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1209 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1210 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1211 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1212 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1213 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1214 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1215 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1216 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1217 PARSE_LANGOPT_BENIGN(PascalStrings);
1218 PARSE_LANGOPT_BENIGN(Boolean);
1219 PARSE_LANGOPT_BENIGN(WritableStrings);
1220 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1221 diag::warn_pch_lax_vector_conversions);
1222 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1223 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1224 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1225 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1226 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1227 diag::warn_pch_thread_safe_statics);
1228 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1229 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1230 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1231 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1232 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1233 diag::warn_pch_heinous_extensions);
1234 // FIXME: Most of the options below are benign if the macro wasn't
1235 // used. Unfortunately, this means that a PCH compiled without
1236 // optimization can't be used with optimization turned on, even
1237 // though the only thing that changes is whether __OPTIMIZE__ was
1238 // defined... but if __OPTIMIZE__ never showed up in the header, it
1239 // doesn't matter. We could consider making this some special kind
1240 // of check.
1241 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1242 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1243 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1244 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1245 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1246 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1247 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1248 Diag(diag::warn_pch_gc_mode)
1249 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1250 Diag(diag::note_ignoring_pch) << FileName;
1251 return true;
1252 }
1253 ++Idx;
1254 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1255 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1256#undef PARSE_LANGOPT_IRRELEVANT
1257#undef PARSE_LANGOPT_BENIGN
1258
1259 return false;
1260}
1261
Douglas Gregorc34897d2009-04-09 22:27:44 +00001262/// \brief Read and return the type at the given offset.
1263///
1264/// This routine actually reads the record corresponding to the type
1265/// at the given offset in the bitstream. It is a helper routine for
1266/// GetType, which deals with reading type IDs.
1267QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001268 // Keep track of where we are in the stream, then jump back there
1269 // after reading this type.
1270 SavedStreamPosition SavedPosition(Stream);
1271
Douglas Gregorc34897d2009-04-09 22:27:44 +00001272 Stream.JumpToBit(Offset);
1273 RecordData Record;
1274 unsigned Code = Stream.ReadCode();
1275 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001276 case pch::TYPE_EXT_QUAL: {
1277 assert(Record.size() == 3 &&
1278 "Incorrect encoding of extended qualifier type");
1279 QualType Base = GetType(Record[0]);
1280 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1281 unsigned AddressSpace = Record[2];
1282
1283 QualType T = Base;
1284 if (GCAttr != QualType::GCNone)
1285 T = Context.getObjCGCQualType(T, GCAttr);
1286 if (AddressSpace)
1287 T = Context.getAddrSpaceQualType(T, AddressSpace);
1288 return T;
1289 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001290
Douglas Gregorc34897d2009-04-09 22:27:44 +00001291 case pch::TYPE_FIXED_WIDTH_INT: {
1292 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1293 return Context.getFixedWidthIntType(Record[0], Record[1]);
1294 }
1295
1296 case pch::TYPE_COMPLEX: {
1297 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1298 QualType ElemType = GetType(Record[0]);
1299 return Context.getComplexType(ElemType);
1300 }
1301
1302 case pch::TYPE_POINTER: {
1303 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1304 QualType PointeeType = GetType(Record[0]);
1305 return Context.getPointerType(PointeeType);
1306 }
1307
1308 case pch::TYPE_BLOCK_POINTER: {
1309 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1310 QualType PointeeType = GetType(Record[0]);
1311 return Context.getBlockPointerType(PointeeType);
1312 }
1313
1314 case pch::TYPE_LVALUE_REFERENCE: {
1315 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1316 QualType PointeeType = GetType(Record[0]);
1317 return Context.getLValueReferenceType(PointeeType);
1318 }
1319
1320 case pch::TYPE_RVALUE_REFERENCE: {
1321 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1322 QualType PointeeType = GetType(Record[0]);
1323 return Context.getRValueReferenceType(PointeeType);
1324 }
1325
1326 case pch::TYPE_MEMBER_POINTER: {
1327 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1328 QualType PointeeType = GetType(Record[0]);
1329 QualType ClassType = GetType(Record[1]);
1330 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1331 }
1332
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001333 case pch::TYPE_CONSTANT_ARRAY: {
1334 QualType ElementType = GetType(Record[0]);
1335 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1336 unsigned IndexTypeQuals = Record[2];
1337 unsigned Idx = 3;
1338 llvm::APInt Size = ReadAPInt(Record, Idx);
1339 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1340 }
1341
1342 case pch::TYPE_INCOMPLETE_ARRAY: {
1343 QualType ElementType = GetType(Record[0]);
1344 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1345 unsigned IndexTypeQuals = Record[2];
1346 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1347 }
1348
1349 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001350 QualType ElementType = GetType(Record[0]);
1351 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1352 unsigned IndexTypeQuals = Record[2];
1353 return Context.getVariableArrayType(ElementType, ReadExpr(),
1354 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001355 }
1356
1357 case pch::TYPE_VECTOR: {
1358 if (Record.size() != 2) {
1359 Error("Incorrect encoding of vector type in PCH file");
1360 return QualType();
1361 }
1362
1363 QualType ElementType = GetType(Record[0]);
1364 unsigned NumElements = Record[1];
1365 return Context.getVectorType(ElementType, NumElements);
1366 }
1367
1368 case pch::TYPE_EXT_VECTOR: {
1369 if (Record.size() != 2) {
1370 Error("Incorrect encoding of extended vector type in PCH file");
1371 return QualType();
1372 }
1373
1374 QualType ElementType = GetType(Record[0]);
1375 unsigned NumElements = Record[1];
1376 return Context.getExtVectorType(ElementType, NumElements);
1377 }
1378
1379 case pch::TYPE_FUNCTION_NO_PROTO: {
1380 if (Record.size() != 1) {
1381 Error("Incorrect encoding of no-proto function type");
1382 return QualType();
1383 }
1384 QualType ResultType = GetType(Record[0]);
1385 return Context.getFunctionNoProtoType(ResultType);
1386 }
1387
1388 case pch::TYPE_FUNCTION_PROTO: {
1389 QualType ResultType = GetType(Record[0]);
1390 unsigned Idx = 1;
1391 unsigned NumParams = Record[Idx++];
1392 llvm::SmallVector<QualType, 16> ParamTypes;
1393 for (unsigned I = 0; I != NumParams; ++I)
1394 ParamTypes.push_back(GetType(Record[Idx++]));
1395 bool isVariadic = Record[Idx++];
1396 unsigned Quals = Record[Idx++];
1397 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1398 isVariadic, Quals);
1399 }
1400
1401 case pch::TYPE_TYPEDEF:
1402 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1403 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1404
1405 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001406 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001407
1408 case pch::TYPE_TYPEOF: {
1409 if (Record.size() != 1) {
1410 Error("Incorrect encoding of typeof(type) in PCH file");
1411 return QualType();
1412 }
1413 QualType UnderlyingType = GetType(Record[0]);
1414 return Context.getTypeOfType(UnderlyingType);
1415 }
1416
1417 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001418 assert(Record.size() == 1 && "Incorrect encoding of record type");
1419 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001420
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001421 case pch::TYPE_ENUM:
1422 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1423 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1424
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001425 case pch::TYPE_OBJC_INTERFACE:
1426 // FIXME: Deserialize ObjCInterfaceType
1427 assert(false && "Cannot de-serialize ObjC interface types yet");
1428 return QualType();
1429
1430 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1431 // FIXME: Deserialize ObjCQualifiedInterfaceType
1432 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1433 return QualType();
1434
1435 case pch::TYPE_OBJC_QUALIFIED_ID:
1436 // FIXME: Deserialize ObjCQualifiedIdType
1437 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1438 return QualType();
1439
1440 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1441 // FIXME: Deserialize ObjCQualifiedClassType
1442 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1443 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001444 }
1445
1446 // Suppress a GCC warning
1447 return QualType();
1448}
1449
1450/// \brief Note that we have loaded the declaration with the given
1451/// Index.
1452///
1453/// This routine notes that this declaration has already been loaded,
1454/// so that future GetDecl calls will return this declaration rather
1455/// than trying to load a new declaration.
1456inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1457 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1458 DeclAlreadyLoaded[Index] = true;
1459 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1460}
1461
1462/// \brief Read the declaration at the given offset from the PCH file.
1463Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001464 // Keep track of where we are in the stream, then jump back there
1465 // after reading this declaration.
1466 SavedStreamPosition SavedPosition(Stream);
1467
Douglas Gregorc34897d2009-04-09 22:27:44 +00001468 Decl *D = 0;
1469 Stream.JumpToBit(Offset);
1470 RecordData Record;
1471 unsigned Code = Stream.ReadCode();
1472 unsigned Idx = 0;
1473 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001474
Douglas Gregorc34897d2009-04-09 22:27:44 +00001475 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001476 case pch::DECL_ATTR:
1477 case pch::DECL_CONTEXT_LEXICAL:
1478 case pch::DECL_CONTEXT_VISIBLE:
1479 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1480 break;
1481
Douglas Gregorc34897d2009-04-09 22:27:44 +00001482 case pch::DECL_TRANSLATION_UNIT:
1483 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001484 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001485 break;
1486
1487 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001488 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001489 break;
1490 }
1491
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001492 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001493 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001494 break;
1495 }
1496
Douglas Gregor982365e2009-04-13 21:20:57 +00001497 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001498 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1499 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001500 break;
1501 }
1502
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001503 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001504 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1505 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001506 break;
1507 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001508
1509 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001510 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1511 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001512 break;
1513 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001514
Douglas Gregor982365e2009-04-13 21:20:57 +00001515 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001516 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1517 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001518 break;
1519 }
1520
Douglas Gregorc34897d2009-04-09 22:27:44 +00001521 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001522 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1523 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001524 break;
1525 }
1526
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001527 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001528 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1529 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001530 break;
1531 }
1532
1533 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001534 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001535 QualType(), QualType(), VarDecl::None,
1536 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001537 break;
1538 }
1539
Douglas Gregor2a491792009-04-13 22:49:25 +00001540 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001541 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001542 break;
1543 }
1544
1545 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001546 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001547 break;
1548 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001549 }
1550
Douglas Gregorddf4d092009-04-16 22:29:51 +00001551 assert(D && "Unknown declaration creating PCH file");
1552 if (D) {
1553 LoadedDecl(Index, D);
1554 Reader.Visit(D);
1555 }
1556
Douglas Gregorc34897d2009-04-09 22:27:44 +00001557 // If this declaration is also a declaration context, get the
1558 // offsets for its tables of lexical and visible declarations.
1559 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1560 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1561 if (Offsets.first || Offsets.second) {
1562 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1563 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1564 DeclContextOffsets[DC] = Offsets;
1565 }
1566 }
1567 assert(Idx == Record.size());
1568
1569 return D;
1570}
1571
Douglas Gregorac8f2802009-04-10 17:25:41 +00001572QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001573 unsigned Quals = ID & 0x07;
1574 unsigned Index = ID >> 3;
1575
1576 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1577 QualType T;
1578 switch ((pch::PredefinedTypeIDs)Index) {
1579 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1580 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1581 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1582
1583 case pch::PREDEF_TYPE_CHAR_U_ID:
1584 case pch::PREDEF_TYPE_CHAR_S_ID:
1585 // FIXME: Check that the signedness of CharTy is correct!
1586 T = Context.CharTy;
1587 break;
1588
1589 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1590 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1591 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1592 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1593 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1594 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1595 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1596 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1597 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1598 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1599 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1600 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1601 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1602 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1603 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1604 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1605 }
1606
1607 assert(!T.isNull() && "Unknown predefined type");
1608 return T.getQualifiedType(Quals);
1609 }
1610
1611 Index -= pch::NUM_PREDEF_TYPE_IDS;
1612 if (!TypeAlreadyLoaded[Index]) {
1613 // Load the type from the PCH file.
1614 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1615 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1616 TypeAlreadyLoaded[Index] = true;
1617 }
1618
1619 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1620}
1621
Douglas Gregorac8f2802009-04-10 17:25:41 +00001622Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001623 if (ID == 0)
1624 return 0;
1625
1626 unsigned Index = ID - 1;
1627 if (DeclAlreadyLoaded[Index])
1628 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1629
1630 // Load the declaration from the PCH file.
1631 return ReadDeclRecord(DeclOffsets[Index], Index);
1632}
1633
1634bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001635 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001636 assert(DC->hasExternalLexicalStorage() &&
1637 "DeclContext has no lexical decls in storage");
1638 uint64_t Offset = DeclContextOffsets[DC].first;
1639 assert(Offset && "DeclContext has no lexical decls in storage");
1640
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001641 // Keep track of where we are in the stream, then jump back there
1642 // after reading this context.
1643 SavedStreamPosition SavedPosition(Stream);
1644
Douglas Gregorc34897d2009-04-09 22:27:44 +00001645 // Load the record containing all of the declarations lexically in
1646 // this context.
1647 Stream.JumpToBit(Offset);
1648 RecordData Record;
1649 unsigned Code = Stream.ReadCode();
1650 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001651 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001652 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1653
1654 // Load all of the declaration IDs
1655 Decls.clear();
1656 Decls.insert(Decls.end(), Record.begin(), Record.end());
1657 return false;
1658}
1659
1660bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1661 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1662 assert(DC->hasExternalVisibleStorage() &&
1663 "DeclContext has no visible decls in storage");
1664 uint64_t Offset = DeclContextOffsets[DC].second;
1665 assert(Offset && "DeclContext has no visible decls in storage");
1666
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001667 // Keep track of where we are in the stream, then jump back there
1668 // after reading this context.
1669 SavedStreamPosition SavedPosition(Stream);
1670
Douglas Gregorc34897d2009-04-09 22:27:44 +00001671 // Load the record containing all of the declarations visible in
1672 // this context.
1673 Stream.JumpToBit(Offset);
1674 RecordData Record;
1675 unsigned Code = Stream.ReadCode();
1676 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001677 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001678 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1679 if (Record.size() == 0)
1680 return false;
1681
1682 Decls.clear();
1683
1684 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001685 while (Idx < Record.size()) {
1686 Decls.push_back(VisibleDeclaration());
1687 Decls.back().Name = ReadDeclarationName(Record, Idx);
1688
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689 unsigned Size = Record[Idx++];
1690 llvm::SmallVector<unsigned, 4> & LoadedDecls
1691 = Decls.back().Declarations;
1692 LoadedDecls.reserve(Size);
1693 for (unsigned I = 0; I < Size; ++I)
1694 LoadedDecls.push_back(Record[Idx++]);
1695 }
1696
1697 return false;
1698}
1699
Douglas Gregor631f6c62009-04-14 00:24:19 +00001700void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1701 if (!Consumer)
1702 return;
1703
1704 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1705 Decl *D = GetDecl(ExternalDefinitions[I]);
1706 DeclGroupRef DG(D);
1707 Consumer->HandleTopLevelDecl(DG);
1708 }
1709}
1710
Douglas Gregorc34897d2009-04-09 22:27:44 +00001711void PCHReader::PrintStats() {
1712 std::fprintf(stderr, "*** PCH Statistics:\n");
1713
1714 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1715 TypeAlreadyLoaded.end(),
1716 true);
1717 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1718 DeclAlreadyLoaded.end(),
1719 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001720 unsigned NumIdentifiersLoaded = 0;
1721 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1722 if ((IdentifierData[I] & 0x01) == 0)
1723 ++NumIdentifiersLoaded;
1724 }
1725
Douglas Gregorc34897d2009-04-09 22:27:44 +00001726 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1727 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001728 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001729 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1730 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001731 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1732 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1733 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1734 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001735 std::fprintf(stderr, "\n");
1736}
1737
Chris Lattner29241862009-04-11 21:15:38 +00001738IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001739 if (ID == 0)
1740 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001741
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001742 if (!IdentifierTable || IdentifierData.empty()) {
1743 Error("No identifier table in PCH file");
1744 return 0;
1745 }
Chris Lattner29241862009-04-11 21:15:38 +00001746
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001747 if (IdentifierData[ID - 1] & 0x01) {
1748 uint64_t Offset = IdentifierData[ID - 1];
1749 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001750 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001751 }
Chris Lattner29241862009-04-11 21:15:38 +00001752
1753 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001754}
1755
1756DeclarationName
1757PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1758 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1759 switch (Kind) {
1760 case DeclarationName::Identifier:
1761 return DeclarationName(GetIdentifierInfo(Record, Idx));
1762
1763 case DeclarationName::ObjCZeroArgSelector:
1764 case DeclarationName::ObjCOneArgSelector:
1765 case DeclarationName::ObjCMultiArgSelector:
1766 assert(false && "Unable to de-serialize Objective-C selectors");
1767 break;
1768
1769 case DeclarationName::CXXConstructorName:
1770 return Context.DeclarationNames.getCXXConstructorName(
1771 GetType(Record[Idx++]));
1772
1773 case DeclarationName::CXXDestructorName:
1774 return Context.DeclarationNames.getCXXDestructorName(
1775 GetType(Record[Idx++]));
1776
1777 case DeclarationName::CXXConversionFunctionName:
1778 return Context.DeclarationNames.getCXXConversionFunctionName(
1779 GetType(Record[Idx++]));
1780
1781 case DeclarationName::CXXOperatorName:
1782 return Context.DeclarationNames.getCXXOperatorName(
1783 (OverloadedOperatorKind)Record[Idx++]);
1784
1785 case DeclarationName::CXXUsingDirective:
1786 return DeclarationName::getUsingDirectiveName();
1787 }
1788
1789 // Required to silence GCC warning
1790 return DeclarationName();
1791}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001792
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001793/// \brief Read an integral value
1794llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1795 unsigned BitWidth = Record[Idx++];
1796 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1797 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1798 Idx += NumWords;
1799 return Result;
1800}
1801
1802/// \brief Read a signed integral value
1803llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1804 bool isUnsigned = Record[Idx++];
1805 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1806}
1807
Douglas Gregore2f37202009-04-14 21:55:33 +00001808/// \brief Read a floating-point value
1809llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001810 return llvm::APFloat(ReadAPInt(Record, Idx));
1811}
1812
Douglas Gregor1c507882009-04-15 21:30:51 +00001813// \brief Read a string
1814std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1815 unsigned Len = Record[Idx++];
1816 std::string Result(&Record[Idx], &Record[Idx] + Len);
1817 Idx += Len;
1818 return Result;
1819}
1820
1821/// \brief Reads attributes from the current stream position.
1822Attr *PCHReader::ReadAttributes() {
1823 unsigned Code = Stream.ReadCode();
1824 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1825 "Expected unabbreviated record"); (void)Code;
1826
1827 RecordData Record;
1828 unsigned Idx = 0;
1829 unsigned RecCode = Stream.ReadRecord(Code, Record);
1830 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1831 (void)RecCode;
1832
1833#define SIMPLE_ATTR(Name) \
1834 case Attr::Name: \
1835 New = ::new (Context) Name##Attr(); \
1836 break
1837
1838#define STRING_ATTR(Name) \
1839 case Attr::Name: \
1840 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1841 break
1842
1843#define UNSIGNED_ATTR(Name) \
1844 case Attr::Name: \
1845 New = ::new (Context) Name##Attr(Record[Idx++]); \
1846 break
1847
1848 Attr *Attrs = 0;
1849 while (Idx < Record.size()) {
1850 Attr *New = 0;
1851 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1852 bool IsInherited = Record[Idx++];
1853
1854 switch (Kind) {
1855 STRING_ATTR(Alias);
1856 UNSIGNED_ATTR(Aligned);
1857 SIMPLE_ATTR(AlwaysInline);
1858 SIMPLE_ATTR(AnalyzerNoReturn);
1859 STRING_ATTR(Annotate);
1860 STRING_ATTR(AsmLabel);
1861
1862 case Attr::Blocks:
1863 New = ::new (Context) BlocksAttr(
1864 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1865 break;
1866
1867 case Attr::Cleanup:
1868 New = ::new (Context) CleanupAttr(
1869 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1870 break;
1871
1872 SIMPLE_ATTR(Const);
1873 UNSIGNED_ATTR(Constructor);
1874 SIMPLE_ATTR(DLLExport);
1875 SIMPLE_ATTR(DLLImport);
1876 SIMPLE_ATTR(Deprecated);
1877 UNSIGNED_ATTR(Destructor);
1878 SIMPLE_ATTR(FastCall);
1879
1880 case Attr::Format: {
1881 std::string Type = ReadString(Record, Idx);
1882 unsigned FormatIdx = Record[Idx++];
1883 unsigned FirstArg = Record[Idx++];
1884 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1885 break;
1886 }
1887
1888 SIMPLE_ATTR(GNUCInline);
1889
1890 case Attr::IBOutletKind:
1891 New = ::new (Context) IBOutletAttr();
1892 break;
1893
1894 SIMPLE_ATTR(NoReturn);
1895 SIMPLE_ATTR(NoThrow);
1896 SIMPLE_ATTR(Nodebug);
1897 SIMPLE_ATTR(Noinline);
1898
1899 case Attr::NonNull: {
1900 unsigned Size = Record[Idx++];
1901 llvm::SmallVector<unsigned, 16> ArgNums;
1902 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1903 Idx += Size;
1904 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1905 break;
1906 }
1907
1908 SIMPLE_ATTR(ObjCException);
1909 SIMPLE_ATTR(ObjCNSObject);
1910 SIMPLE_ATTR(Overloadable);
1911 UNSIGNED_ATTR(Packed);
1912 SIMPLE_ATTR(Pure);
1913 UNSIGNED_ATTR(Regparm);
1914 STRING_ATTR(Section);
1915 SIMPLE_ATTR(StdCall);
1916 SIMPLE_ATTR(TransparentUnion);
1917 SIMPLE_ATTR(Unavailable);
1918 SIMPLE_ATTR(Unused);
1919 SIMPLE_ATTR(Used);
1920
1921 case Attr::Visibility:
1922 New = ::new (Context) VisibilityAttr(
1923 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1924 break;
1925
1926 SIMPLE_ATTR(WarnUnusedResult);
1927 SIMPLE_ATTR(Weak);
1928 SIMPLE_ATTR(WeakImport);
1929 }
1930
1931 assert(New && "Unable to decode attribute?");
1932 New->setInherited(IsInherited);
1933 New->setNext(Attrs);
1934 Attrs = New;
1935 }
1936#undef UNSIGNED_ATTR
1937#undef STRING_ATTR
1938#undef SIMPLE_ATTR
1939
1940 // The list of attributes was built backwards. Reverse the list
1941 // before returning it.
1942 Attr *PrevAttr = 0, *NextAttr = 0;
1943 while (Attrs) {
1944 NextAttr = Attrs->getNext();
1945 Attrs->setNext(PrevAttr);
1946 PrevAttr = Attrs;
1947 Attrs = NextAttr;
1948 }
1949
1950 return PrevAttr;
1951}
1952
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001953Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00001954 // Within the bitstream, expressions are stored in Reverse Polish
1955 // Notation, with each of the subexpressions preceding the
1956 // expression they are stored in. To evaluate expressions, we
1957 // continue reading expressions and placing them on the stack, with
1958 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001959 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00001960 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001961 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00001962 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001963 llvm::SmallVector<Stmt *, 16> StmtStack;
1964 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001965 Stmt::EmptyShell Empty;
1966
Douglas Gregora151ba42009-04-14 23:32:43 +00001967 while (true) {
1968 unsigned Code = Stream.ReadCode();
1969 if (Code == llvm::bitc::END_BLOCK) {
1970 if (Stream.ReadBlockEnd()) {
1971 Error("Error at end of Source Manager block");
1972 return 0;
1973 }
1974 break;
1975 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001976
Douglas Gregora151ba42009-04-14 23:32:43 +00001977 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1978 // No known subblocks, always skip them.
1979 Stream.ReadSubBlockID();
1980 if (Stream.SkipBlock()) {
1981 Error("Malformed block record");
1982 return 0;
1983 }
1984 continue;
1985 }
Douglas Gregore2f37202009-04-14 21:55:33 +00001986
Douglas Gregora151ba42009-04-14 23:32:43 +00001987 if (Code == llvm::bitc::DEFINE_ABBREV) {
1988 Stream.ReadAbbrevRecord();
1989 continue;
1990 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001991
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001992 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00001993 Idx = 0;
1994 Record.clear();
1995 bool Finished = false;
1996 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001997 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00001998 Finished = true;
1999 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002000
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002001 case pch::STMT_NULL_PTR:
2002 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002003 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002004
Douglas Gregora151ba42009-04-14 23:32:43 +00002005 case pch::EXPR_PREDEFINED:
2006 // FIXME: untested (until we can serialize function bodies).
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002007 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002008 break;
2009
2010 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002011 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002012 break;
2013
2014 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002015 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002016 break;
2017
2018 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002019 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002020 break;
2021
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002022 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002023 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002024 break;
2025
Douglas Gregor596e0932009-04-15 16:35:07 +00002026 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002027 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002028 Record[PCHStmtReader::NumExprFields + 1]);
2029 break;
2030
Douglas Gregora151ba42009-04-14 23:32:43 +00002031 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002032 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002033 break;
2034
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002035 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002036 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002037 break;
2038
Douglas Gregor12d74052009-04-15 15:58:59 +00002039 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002040 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002041 break;
2042
2043 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002044 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002045 break;
2046
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002047 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002048 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002049 break;
2050
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002051 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002052 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002053 break;
2054
2055 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002056 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002057 break;
2058
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002059 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002060 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002061 break;
2062
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002063 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002064 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002065 break;
2066
2067 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002068 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002069 break;
2070
Douglas Gregora151ba42009-04-14 23:32:43 +00002071 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002072 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002073 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002074
2075 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002076 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002077 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002078
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002079 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002080 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002081 break;
2082
Douglas Gregorec0b8292009-04-15 23:02:49 +00002083 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002084 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002085 break;
2086
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002087 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002088 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002089 break;
2090
2091 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002092 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002093 Record[PCHStmtReader::NumExprFields] - 1);
2094
2095 break;
2096
2097 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002098 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002099 break;
2100
Douglas Gregorec0b8292009-04-15 23:02:49 +00002101 case pch::EXPR_VA_ARG:
2102 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002103 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002104 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002105
2106 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002107 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002108 break;
2109
2110 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002111 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002112 break;
2113
2114 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002115 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002116 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002117
2118 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002119 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002120 break;
2121
2122 case pch::EXPR_BLOCK_DECL_REF:
2123 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002124 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002125 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002126 }
2127
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002128 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002129 if (Finished)
2130 break;
2131
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002132 if (S) {
2133 unsigned NumSubStmts = Reader.Visit(S);
2134 while (NumSubStmts > 0) {
2135 StmtStack.pop_back();
2136 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002137 }
2138 }
2139
2140 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002141 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002142 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002143 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
2144 return StmtStack.back();
2145}
2146
2147Expr *PCHReader::ReadExpr() {
2148 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002149}
2150
Douglas Gregor179cfb12009-04-10 20:39:37 +00002151DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002152 return Diag(SourceLocation(), DiagID);
2153}
2154
2155DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2156 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002157 Context.getSourceManager()),
2158 DiagID);
2159}