blob: 8928b063cf23652c80d9babd9c689f0053c18346 [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);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor982365e2009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000167}
168
Douglas Gregorc34897d2009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000180}
181
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor2a491792009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
200 unsigned NumParams = Record[Idx++];
201 llvm::SmallVector<ParmVarDecl *, 16> Params;
202 Params.reserve(NumParams);
203 for (unsigned I = 0; I != NumParams; ++I)
204 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
205 BD->setParams(Reader.getContext(), &Params[0], NumParams);
206}
207
Douglas Gregorc34897d2009-04-09 22:27:44 +0000208std::pair<uint64_t, uint64_t>
209PCHDeclReader::VisitDeclContext(DeclContext *DC) {
210 uint64_t LexicalOffset = Record[Idx++];
211 uint64_t VisibleOffset = 0;
212 if (DC->getPrimaryContext() == DC)
213 VisibleOffset = Record[Idx++];
214 return std::make_pair(LexicalOffset, VisibleOffset);
215}
216
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000217//===----------------------------------------------------------------------===//
218// Statement/expression deserialization
219//===----------------------------------------------------------------------===//
220namespace {
221 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000222 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000223 PCHReader &Reader;
224 const PCHReader::RecordData &Record;
225 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000226 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000227
228 public:
229 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000230 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
231 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000232
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000233 /// \brief The number of record fields required for the Stmt class
234 /// itself.
235 static const unsigned NumStmtFields = 0;
236
Douglas Gregor596e0932009-04-15 16:35:07 +0000237 /// \brief The number of record fields required for the Expr class
238 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000239 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000240
Douglas Gregora151ba42009-04-14 23:32:43 +0000241 // Each of the Visit* functions reads in part of the expression
242 // from the given record and the current expression stack, then
243 // return the total number of operands that it read from the
244 // expression stack.
245
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000246 unsigned VisitStmt(Stmt *S);
247 unsigned VisitNullStmt(NullStmt *S);
248 unsigned VisitCompoundStmt(CompoundStmt *S);
249 unsigned VisitSwitchCase(SwitchCase *S);
250 unsigned VisitCaseStmt(CaseStmt *S);
251 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000252 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000253 unsigned VisitIfStmt(IfStmt *S);
254 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000255 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000256 unsigned VisitDoStmt(DoStmt *S);
257 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000258 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000259 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000260 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000261 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000262 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000263 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000264 unsigned VisitExpr(Expr *E);
265 unsigned VisitPredefinedExpr(PredefinedExpr *E);
266 unsigned VisitDeclRefExpr(DeclRefExpr *E);
267 unsigned VisitIntegerLiteral(IntegerLiteral *E);
268 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000269 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000270 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000271 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000272 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000273 unsigned VisitUnaryOperator(UnaryOperator *E);
274 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000275 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000276 unsigned VisitCallExpr(CallExpr *E);
277 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000278 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000279 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000280 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
281 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000282 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000283 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
284 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000285 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000286 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000287 unsigned VisitInitListExpr(InitListExpr *E);
288 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
289 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000290 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000291 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000292 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
293 unsigned VisitChooseExpr(ChooseExpr *E);
294 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000295 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
296 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000297 };
298}
299
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000300unsigned PCHStmtReader::VisitStmt(Stmt *S) {
301 assert(Idx == NumStmtFields && "Incorrect statement field count");
302 return 0;
303}
304
305unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
306 VisitStmt(S);
307 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
308 return 0;
309}
310
311unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
312 VisitStmt(S);
313 unsigned NumStmts = Record[Idx++];
314 S->setStmts(Reader.getContext(),
315 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
316 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
317 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
318 return NumStmts;
319}
320
321unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
322 VisitStmt(S);
323 Reader.RecordSwitchCaseID(S, Record[Idx++]);
324 return 0;
325}
326
327unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
328 VisitSwitchCase(S);
329 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
330 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
331 S->setSubStmt(StmtStack.back());
332 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
333 return 3;
334}
335
336unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
337 VisitSwitchCase(S);
338 S->setSubStmt(StmtStack.back());
339 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
340 return 1;
341}
342
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000343unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
344 VisitStmt(S);
345 S->setID(Reader.GetIdentifierInfo(Record, Idx));
346 S->setSubStmt(StmtStack.back());
347 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
348 Reader.RecordLabelStmt(S, Record[Idx++]);
349 return 1;
350}
351
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000352unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
353 VisitStmt(S);
354 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
355 S->setThen(StmtStack[StmtStack.size() - 2]);
356 S->setElse(StmtStack[StmtStack.size() - 1]);
357 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
358 return 3;
359}
360
361unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
362 VisitStmt(S);
363 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
364 S->setBody(StmtStack.back());
365 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
366 SwitchCase *PrevSC = 0;
367 for (unsigned N = Record.size(); Idx != N; ++Idx) {
368 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
369 if (PrevSC)
370 PrevSC->setNextSwitchCase(SC);
371 else
372 S->setSwitchCaseList(SC);
373 PrevSC = SC;
374 }
375 return 2;
376}
377
Douglas Gregora6b503f2009-04-17 00:16:09 +0000378unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
379 VisitStmt(S);
380 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
381 S->setBody(StmtStack.back());
382 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
383 return 2;
384}
385
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000386unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
387 VisitStmt(S);
388 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
389 S->setBody(StmtStack.back());
390 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
391 return 2;
392}
393
394unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
395 VisitStmt(S);
396 S->setInit(StmtStack[StmtStack.size() - 4]);
397 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
398 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
399 S->setBody(StmtStack.back());
400 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
401 return 4;
402}
403
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000404unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
405 VisitStmt(S);
406 Reader.SetLabelOf(S, Record[Idx++]);
407 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
408 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
409 return 0;
410}
411
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000412unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
413 VisitStmt(S);
414 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
415 return 1;
416}
417
Douglas Gregora6b503f2009-04-17 00:16:09 +0000418unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
419 VisitStmt(S);
420 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
421 return 0;
422}
423
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000424unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
425 VisitStmt(S);
426 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
427 return 0;
428}
429
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000430unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
431 VisitStmt(S);
432 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
433 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
434 return 1;
435}
436
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000437unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
438 VisitStmt(S);
439 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
440 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
441
442 if (Idx + 1 == Record.size()) {
443 // Single declaration
444 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
445 } else {
446 llvm::SmallVector<Decl *, 16> Decls;
447 Decls.reserve(Record.size() - Idx);
448 for (unsigned N = Record.size(); Idx != N; ++Idx)
449 Decls.push_back(Reader.GetDecl(Record[Idx]));
450 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
451 &Decls[0], Decls.size())));
452 }
453 return 0;
454}
455
Douglas Gregora151ba42009-04-14 23:32:43 +0000456unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000457 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000458 E->setType(Reader.GetType(Record[Idx++]));
459 E->setTypeDependent(Record[Idx++]);
460 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000461 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000462 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000463}
464
Douglas Gregora151ba42009-04-14 23:32:43 +0000465unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000466 VisitExpr(E);
467 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
468 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000469 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000470}
471
Douglas Gregora151ba42009-04-14 23:32:43 +0000472unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000473 VisitExpr(E);
474 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
475 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000476 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000477}
478
Douglas Gregora151ba42009-04-14 23:32:43 +0000479unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000480 VisitExpr(E);
481 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
482 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000483 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000484}
485
Douglas Gregora151ba42009-04-14 23:32:43 +0000486unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000487 VisitExpr(E);
488 E->setValue(Reader.ReadAPFloat(Record, Idx));
489 E->setExact(Record[Idx++]);
490 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000491 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000492}
493
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000494unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
495 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000496 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000497 return 1;
498}
499
Douglas Gregor596e0932009-04-15 16:35:07 +0000500unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
501 VisitExpr(E);
502 unsigned Len = Record[Idx++];
503 assert(Record[Idx] == E->getNumConcatenated() &&
504 "Wrong number of concatenated tokens!");
505 ++Idx;
506 E->setWide(Record[Idx++]);
507
508 // Read string data
509 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
510 E->setStrData(Reader.getContext(), &Str[0], Len);
511 Idx += Len;
512
513 // Read source locations
514 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
515 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
516
517 return 0;
518}
519
Douglas Gregora151ba42009-04-14 23:32:43 +0000520unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000521 VisitExpr(E);
522 E->setValue(Record[Idx++]);
523 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
524 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000525 return 0;
526}
527
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000528unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
529 VisitExpr(E);
530 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
531 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000532 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000533 return 1;
534}
535
Douglas Gregor12d74052009-04-15 15:58:59 +0000536unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
537 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000538 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000539 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
540 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
541 return 1;
542}
543
544unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
545 VisitExpr(E);
546 E->setSizeof(Record[Idx++]);
547 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000548 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000549 ++Idx;
550 } else {
551 E->setArgument(Reader.GetType(Record[Idx++]));
552 }
553 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
554 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 return E->isArgumentType()? 0 : 1;
556}
557
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000558unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
559 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000560 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
561 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000562 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
563 return 2;
564}
565
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000566unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
567 VisitExpr(E);
568 E->setNumArgs(Reader.getContext(), Record[Idx++]);
569 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000570 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000571 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000572 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000573 return E->getNumArgs() + 1;
574}
575
576unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
577 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000578 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000579 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
580 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 E->setArrow(Record[Idx++]);
582 return 1;
583}
584
Douglas Gregora151ba42009-04-14 23:32:43 +0000585unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
586 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000587 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000588 return 1;
589}
590
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000591unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
592 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000593 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
594 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000595 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
596 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
597 return 2;
598}
599
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000600unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
601 VisitBinaryOperator(E);
602 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
603 E->setComputationResultType(Reader.GetType(Record[Idx++]));
604 return 2;
605}
606
607unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
608 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000609 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
610 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
611 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000612 return 3;
613}
614
Douglas Gregora151ba42009-04-14 23:32:43 +0000615unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
616 VisitCastExpr(E);
617 E->setLvalueCast(Record[Idx++]);
618 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000619}
620
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000621unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
622 VisitCastExpr(E);
623 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
624 return 1;
625}
626
627unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
628 VisitExplicitCastExpr(E);
629 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
630 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
631 return 1;
632}
633
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000634unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
635 VisitExpr(E);
636 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000637 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000638 E->setFileScope(Record[Idx++]);
639 return 1;
640}
641
Douglas Gregorec0b8292009-04-15 23:02:49 +0000642unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
643 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000644 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000645 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
646 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
647 return 1;
648}
649
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000650unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
651 VisitExpr(E);
652 unsigned NumInits = Record[Idx++];
653 E->reserveInits(NumInits);
654 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000655 E->updateInit(I,
656 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
657 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000658 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
659 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
660 E->setInitializedFieldInUnion(
661 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
662 E->sawArrayRangeDesignator(Record[Idx++]);
663 return NumInits + 1;
664}
665
666unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
667 typedef DesignatedInitExpr::Designator Designator;
668
669 VisitExpr(E);
670 unsigned NumSubExprs = Record[Idx++];
671 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
672 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000673 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000674 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
675 E->setGNUSyntax(Record[Idx++]);
676
677 llvm::SmallVector<Designator, 4> Designators;
678 while (Idx < Record.size()) {
679 switch ((pch::DesignatorTypes)Record[Idx++]) {
680 case pch::DESIG_FIELD_DECL: {
681 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
682 SourceLocation DotLoc
683 = SourceLocation::getFromRawEncoding(Record[Idx++]);
684 SourceLocation FieldLoc
685 = SourceLocation::getFromRawEncoding(Record[Idx++]);
686 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
687 FieldLoc));
688 Designators.back().setField(Field);
689 break;
690 }
691
692 case pch::DESIG_FIELD_NAME: {
693 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
694 SourceLocation DotLoc
695 = SourceLocation::getFromRawEncoding(Record[Idx++]);
696 SourceLocation FieldLoc
697 = SourceLocation::getFromRawEncoding(Record[Idx++]);
698 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
699 break;
700 }
701
702 case pch::DESIG_ARRAY: {
703 unsigned Index = Record[Idx++];
704 SourceLocation LBracketLoc
705 = SourceLocation::getFromRawEncoding(Record[Idx++]);
706 SourceLocation RBracketLoc
707 = SourceLocation::getFromRawEncoding(Record[Idx++]);
708 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
709 break;
710 }
711
712 case pch::DESIG_ARRAY_RANGE: {
713 unsigned Index = Record[Idx++];
714 SourceLocation LBracketLoc
715 = SourceLocation::getFromRawEncoding(Record[Idx++]);
716 SourceLocation EllipsisLoc
717 = SourceLocation::getFromRawEncoding(Record[Idx++]);
718 SourceLocation RBracketLoc
719 = SourceLocation::getFromRawEncoding(Record[Idx++]);
720 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
721 RBracketLoc));
722 break;
723 }
724 }
725 }
726 E->setDesignators(&Designators[0], Designators.size());
727
728 return NumSubExprs;
729}
730
731unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
732 VisitExpr(E);
733 return 0;
734}
735
Douglas Gregorec0b8292009-04-15 23:02:49 +0000736unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
737 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000738 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000739 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
740 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
741 return 1;
742}
743
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000744unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
745 VisitExpr(E);
746 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
747 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
748 Reader.SetLabelOf(E, Record[Idx++]);
749 return 0;
750}
751
Douglas Gregor209d4622009-04-15 23:33:31 +0000752unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
753 VisitExpr(E);
754 E->setArgType1(Reader.GetType(Record[Idx++]));
755 E->setArgType2(Reader.GetType(Record[Idx++]));
756 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
757 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
758 return 0;
759}
760
761unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
762 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000763 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
764 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
765 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000766 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
767 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 return 3;
769}
770
771unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
772 VisitExpr(E);
773 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
774 return 0;
775}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000776
Douglas Gregor725e94b2009-04-16 00:01:45 +0000777unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
778 VisitExpr(E);
779 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000780 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000781 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
782 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
783 return NumExprs;
784}
785
786unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
787 VisitExpr(E);
788 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
789 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
790 E->setByRef(Record[Idx++]);
791 return 0;
792}
793
Douglas Gregorc34897d2009-04-09 22:27:44 +0000794// FIXME: use the diagnostics machinery
795static bool Error(const char *Str) {
796 std::fprintf(stderr, "%s\n", Str);
797 return true;
798}
799
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000800/// \brief Check the contents of the predefines buffer against the
801/// contents of the predefines buffer used to build the PCH file.
802///
803/// The contents of the two predefines buffers should be the same. If
804/// not, then some command-line option changed the preprocessor state
805/// and we must reject the PCH file.
806///
807/// \param PCHPredef The start of the predefines buffer in the PCH
808/// file.
809///
810/// \param PCHPredefLen The length of the predefines buffer in the PCH
811/// file.
812///
813/// \param PCHBufferID The FileID for the PCH predefines buffer.
814///
815/// \returns true if there was a mismatch (in which case the PCH file
816/// should be ignored), or false otherwise.
817bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
818 unsigned PCHPredefLen,
819 FileID PCHBufferID) {
820 const char *Predef = PP.getPredefines().c_str();
821 unsigned PredefLen = PP.getPredefines().size();
822
823 // If the two predefines buffers compare equal, we're done!.
824 if (PredefLen == PCHPredefLen &&
825 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
826 return false;
827
828 // The predefines buffers are different. Produce a reasonable
829 // diagnostic showing where they are different.
830
831 // The source locations (potentially in the two different predefines
832 // buffers)
833 SourceLocation Loc1, Loc2;
834 SourceManager &SourceMgr = PP.getSourceManager();
835
836 // Create a source buffer for our predefines string, so
837 // that we can build a diagnostic that points into that
838 // source buffer.
839 FileID BufferID;
840 if (Predef && Predef[0]) {
841 llvm::MemoryBuffer *Buffer
842 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
843 "<built-in>");
844 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
845 }
846
847 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
848 std::pair<const char *, const char *> Locations
849 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
850
851 if (Locations.first != Predef + MinLen) {
852 // We found the location in the two buffers where there is a
853 // difference. Form source locations to point there (in both
854 // buffers).
855 unsigned Offset = Locations.first - Predef;
856 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
857 .getFileLocWithOffset(Offset);
858 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
859 .getFileLocWithOffset(Offset);
860 } else if (PredefLen > PCHPredefLen) {
861 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
862 .getFileLocWithOffset(MinLen);
863 } else {
864 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
865 .getFileLocWithOffset(MinLen);
866 }
867
868 Diag(Loc1, diag::warn_pch_preprocessor);
869 if (Loc2.isValid())
870 Diag(Loc2, diag::note_predef_in_pch);
871 Diag(diag::note_ignoring_pch) << FileName;
872 return true;
873}
874
Douglas Gregor635f97f2009-04-13 16:31:14 +0000875/// \brief Read the line table in the source manager block.
876/// \returns true if ther was an error.
877static bool ParseLineTable(SourceManager &SourceMgr,
878 llvm::SmallVectorImpl<uint64_t> &Record) {
879 unsigned Idx = 0;
880 LineTableInfo &LineTable = SourceMgr.getLineTable();
881
882 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000883 std::map<int, int> FileIDs;
884 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000885 // Extract the file name
886 unsigned FilenameLen = Record[Idx++];
887 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
888 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000889 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
890 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000891 }
892
893 // Parse the line entries
894 std::vector<LineEntry> Entries;
895 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000896 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000897
898 // Extract the line entries
899 unsigned NumEntries = Record[Idx++];
900 Entries.clear();
901 Entries.reserve(NumEntries);
902 for (unsigned I = 0; I != NumEntries; ++I) {
903 unsigned FileOffset = Record[Idx++];
904 unsigned LineNo = Record[Idx++];
905 int FilenameID = Record[Idx++];
906 SrcMgr::CharacteristicKind FileKind
907 = (SrcMgr::CharacteristicKind)Record[Idx++];
908 unsigned IncludeOffset = Record[Idx++];
909 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
910 FileKind, IncludeOffset));
911 }
912 LineTable.AddEntry(FID, Entries);
913 }
914
915 return false;
916}
917
Douglas Gregorab1cef72009-04-10 03:52:48 +0000918/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000919PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000920 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000921 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
922 Error("Malformed source manager block record");
923 return Failure;
924 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000925
926 SourceManager &SourceMgr = Context.getSourceManager();
927 RecordData Record;
928 while (true) {
929 unsigned Code = Stream.ReadCode();
930 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000931 if (Stream.ReadBlockEnd()) {
932 Error("Error at end of Source Manager block");
933 return Failure;
934 }
935
936 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000937 }
938
939 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
940 // No known subblocks, always skip them.
941 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000942 if (Stream.SkipBlock()) {
943 Error("Malformed block record");
944 return Failure;
945 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000946 continue;
947 }
948
949 if (Code == llvm::bitc::DEFINE_ABBREV) {
950 Stream.ReadAbbrevRecord();
951 continue;
952 }
953
954 // Read a record.
955 const char *BlobStart;
956 unsigned BlobLen;
957 Record.clear();
958 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
959 default: // Default behavior: ignore.
960 break;
961
962 case pch::SM_SLOC_FILE_ENTRY: {
963 // FIXME: We would really like to delay the creation of this
964 // FileEntry until it is actually required, e.g., when producing
965 // a diagnostic with a source location in this file.
966 const FileEntry *File
967 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
968 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000969 FileID ID = SourceMgr.createFileID(File,
970 SourceLocation::getFromRawEncoding(Record[1]),
971 (CharacteristicKind)Record[2]);
972 if (Record[3])
973 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
974 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000975 break;
976 }
977
978 case pch::SM_SLOC_BUFFER_ENTRY: {
979 const char *Name = BlobStart;
980 unsigned Code = Stream.ReadCode();
981 Record.clear();
982 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
983 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000984 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000985 llvm::MemoryBuffer *Buffer
986 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
987 BlobStart + BlobLen - 1,
988 Name);
989 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
990
991 if (strcmp(Name, "<built-in>") == 0
992 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
993 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000994 break;
995 }
996
997 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
998 SourceLocation SpellingLoc
999 = SourceLocation::getFromRawEncoding(Record[1]);
1000 SourceMgr.createInstantiationLoc(
1001 SpellingLoc,
1002 SourceLocation::getFromRawEncoding(Record[2]),
1003 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001004 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001005 break;
1006 }
1007
Chris Lattnere1be6022009-04-14 23:22:57 +00001008 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001009 if (ParseLineTable(SourceMgr, Record))
1010 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001011 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001012 }
1013 }
1014}
1015
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001016bool PCHReader::ReadPreprocessorBlock() {
1017 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1018 return Error("Malformed preprocessor block record");
1019
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001020 RecordData Record;
1021 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1022 MacroInfo *LastMacro = 0;
1023
1024 while (true) {
1025 unsigned Code = Stream.ReadCode();
1026 switch (Code) {
1027 case llvm::bitc::END_BLOCK:
1028 if (Stream.ReadBlockEnd())
1029 return Error("Error at end of preprocessor block");
1030 return false;
1031
1032 case llvm::bitc::ENTER_SUBBLOCK:
1033 // No known subblocks, always skip them.
1034 Stream.ReadSubBlockID();
1035 if (Stream.SkipBlock())
1036 return Error("Malformed block record");
1037 continue;
1038
1039 case llvm::bitc::DEFINE_ABBREV:
1040 Stream.ReadAbbrevRecord();
1041 continue;
1042 default: break;
1043 }
1044
1045 // Read a record.
1046 Record.clear();
1047 pch::PreprocessorRecordTypes RecType =
1048 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1049 switch (RecType) {
1050 default: // Default behavior: ignore unknown records.
1051 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001052 case pch::PP_COUNTER_VALUE:
1053 if (!Record.empty())
1054 PP.setCounterValue(Record[0]);
1055 break;
1056
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001057 case pch::PP_MACRO_OBJECT_LIKE:
1058 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001059 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1060 if (II == 0)
1061 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001062 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1063 bool isUsed = Record[2];
1064
1065 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1066 MI->setIsUsed(isUsed);
1067
1068 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1069 // Decode function-like macro info.
1070 bool isC99VarArgs = Record[3];
1071 bool isGNUVarArgs = Record[4];
1072 MacroArgs.clear();
1073 unsigned NumArgs = Record[5];
1074 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001075 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001076
1077 // Install function-like macro info.
1078 MI->setIsFunctionLike();
1079 if (isC99VarArgs) MI->setIsC99Varargs();
1080 if (isGNUVarArgs) MI->setIsGNUVarargs();
1081 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1082 PP.getPreprocessorAllocator());
1083 }
1084
1085 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001086 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001087
1088 // Remember that we saw this macro last so that we add the tokens that
1089 // form its body to it.
1090 LastMacro = MI;
1091 break;
1092 }
1093
1094 case pch::PP_TOKEN: {
1095 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1096 // pretend we didn't see this.
1097 if (LastMacro == 0) break;
1098
1099 Token Tok;
1100 Tok.startToken();
1101 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1102 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001103 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1104 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001105 Tok.setKind((tok::TokenKind)Record[3]);
1106 Tok.setFlag((Token::TokenFlags)Record[4]);
1107 LastMacro->AddTokenToBody(Tok);
1108 break;
1109 }
1110 }
1111 }
1112}
1113
Douglas Gregor179cfb12009-04-10 20:39:37 +00001114PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1115 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1116 Error("Malformed block record");
1117 return Failure;
1118 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001119
Chris Lattner29241862009-04-11 21:15:38 +00001120 uint64_t PreprocessorBlockBit = 0;
1121
Douglas Gregorc34897d2009-04-09 22:27:44 +00001122 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001123 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001124 while (!Stream.AtEndOfStream()) {
1125 unsigned Code = Stream.ReadCode();
1126 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001127 // If we saw the preprocessor block, read it now.
1128 if (PreprocessorBlockBit) {
1129 uint64_t SavedPos = Stream.GetCurrentBitNo();
1130 Stream.JumpToBit(PreprocessorBlockBit);
1131 if (ReadPreprocessorBlock()) {
1132 Error("Malformed preprocessor block");
1133 return Failure;
1134 }
1135 Stream.JumpToBit(SavedPos);
1136 }
1137
Douglas Gregor179cfb12009-04-10 20:39:37 +00001138 if (Stream.ReadBlockEnd()) {
1139 Error("Error at end of module block");
1140 return Failure;
1141 }
Chris Lattner29241862009-04-11 21:15:38 +00001142
Douglas Gregor179cfb12009-04-10 20:39:37 +00001143 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001144 }
1145
1146 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1147 switch (Stream.ReadSubBlockID()) {
1148 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1149 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1150 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001151 if (Stream.SkipBlock()) {
1152 Error("Malformed block record");
1153 return Failure;
1154 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001155 break;
1156
Chris Lattner29241862009-04-11 21:15:38 +00001157 case pch::PREPROCESSOR_BLOCK_ID:
1158 // Skip the preprocessor block for now, but remember where it is. We
1159 // want to read it in after the identifier table.
1160 if (PreprocessorBlockBit) {
1161 Error("Multiple preprocessor blocks found.");
1162 return Failure;
1163 }
1164 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1165 if (Stream.SkipBlock()) {
1166 Error("Malformed block record");
1167 return Failure;
1168 }
1169 break;
1170
Douglas Gregorab1cef72009-04-10 03:52:48 +00001171 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001172 switch (ReadSourceManagerBlock()) {
1173 case Success:
1174 break;
1175
1176 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001177 Error("Malformed source manager block");
1178 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001179
1180 case IgnorePCH:
1181 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001182 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001183 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001184 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001185 continue;
1186 }
1187
1188 if (Code == llvm::bitc::DEFINE_ABBREV) {
1189 Stream.ReadAbbrevRecord();
1190 continue;
1191 }
1192
1193 // Read and process a record.
1194 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001195 const char *BlobStart = 0;
1196 unsigned BlobLen = 0;
1197 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1198 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001199 default: // Default behavior: ignore.
1200 break;
1201
1202 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001203 if (!TypeOffsets.empty()) {
1204 Error("Duplicate TYPE_OFFSET record in PCH file");
1205 return Failure;
1206 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001207 TypeOffsets.swap(Record);
1208 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1209 break;
1210
1211 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001212 if (!DeclOffsets.empty()) {
1213 Error("Duplicate DECL_OFFSET record in PCH file");
1214 return Failure;
1215 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001216 DeclOffsets.swap(Record);
1217 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1218 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001219
1220 case pch::LANGUAGE_OPTIONS:
1221 if (ParseLanguageOptions(Record))
1222 return IgnorePCH;
1223 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001224
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001225 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001226 std::string TargetTriple(BlobStart, BlobLen);
1227 if (TargetTriple != Context.Target.getTargetTriple()) {
1228 Diag(diag::warn_pch_target_triple)
1229 << TargetTriple << Context.Target.getTargetTriple();
1230 Diag(diag::note_ignoring_pch) << FileName;
1231 return IgnorePCH;
1232 }
1233 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001234 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001235
1236 case pch::IDENTIFIER_TABLE:
1237 IdentifierTable = BlobStart;
1238 break;
1239
1240 case pch::IDENTIFIER_OFFSET:
1241 if (!IdentifierData.empty()) {
1242 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1243 return Failure;
1244 }
1245 IdentifierData.swap(Record);
1246#ifndef NDEBUG
1247 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1248 if ((IdentifierData[I] & 0x01) == 0) {
1249 Error("Malformed identifier table in the precompiled header");
1250 return Failure;
1251 }
1252 }
1253#endif
1254 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001255
1256 case pch::EXTERNAL_DEFINITIONS:
1257 if (!ExternalDefinitions.empty()) {
1258 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1259 return Failure;
1260 }
1261 ExternalDefinitions.swap(Record);
1262 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001263 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001264 }
1265
Douglas Gregor179cfb12009-04-10 20:39:37 +00001266 Error("Premature end of bitstream");
1267 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001268}
1269
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001270PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001271 // Set the PCH file name.
1272 this->FileName = FileName;
1273
Douglas Gregorc34897d2009-04-09 22:27:44 +00001274 // Open the PCH file.
1275 std::string ErrStr;
1276 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001277 if (!Buffer) {
1278 Error(ErrStr.c_str());
1279 return IgnorePCH;
1280 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001281
1282 // Initialize the stream
1283 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1284 (const unsigned char *)Buffer->getBufferEnd());
1285
1286 // Sniff for the signature.
1287 if (Stream.Read(8) != 'C' ||
1288 Stream.Read(8) != 'P' ||
1289 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001290 Stream.Read(8) != 'H') {
1291 Error("Not a PCH file");
1292 return IgnorePCH;
1293 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001294
1295 // We expect a number of well-defined blocks, though we don't necessarily
1296 // need to understand them all.
1297 while (!Stream.AtEndOfStream()) {
1298 unsigned Code = Stream.ReadCode();
1299
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001300 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1301 Error("Invalid record at top-level");
1302 return Failure;
1303 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001304
1305 unsigned BlockID = Stream.ReadSubBlockID();
1306
1307 // We only know the PCH subblock ID.
1308 switch (BlockID) {
1309 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001310 if (Stream.ReadBlockInfoBlock()) {
1311 Error("Malformed BlockInfoBlock");
1312 return Failure;
1313 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001314 break;
1315 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001316 switch (ReadPCHBlock()) {
1317 case Success:
1318 break;
1319
1320 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001321 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001322
1323 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001324 // FIXME: We could consider reading through to the end of this
1325 // PCH block, skipping subblocks, to see if there are other
1326 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001327 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001328 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001329 break;
1330 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001331 if (Stream.SkipBlock()) {
1332 Error("Malformed block record");
1333 return Failure;
1334 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001335 break;
1336 }
1337 }
1338
1339 // Load the translation unit declaration
1340 ReadDeclRecord(DeclOffsets[0], 0);
1341
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001342 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001343}
1344
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001345namespace {
1346 /// \brief Helper class that saves the current stream position and
1347 /// then restores it when destroyed.
1348 struct VISIBILITY_HIDDEN SavedStreamPosition {
1349 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001350 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001351
1352 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001353 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001354 }
1355
1356 private:
1357 llvm::BitstreamReader &Stream;
1358 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001359 };
1360}
1361
Douglas Gregor179cfb12009-04-10 20:39:37 +00001362/// \brief Parse the record that corresponds to a LangOptions data
1363/// structure.
1364///
1365/// This routine compares the language options used to generate the
1366/// PCH file against the language options set for the current
1367/// compilation. For each option, we classify differences between the
1368/// two compiler states as either "benign" or "important". Benign
1369/// differences don't matter, and we accept them without complaint
1370/// (and without modifying the language options). Differences between
1371/// the states for important options cause the PCH file to be
1372/// unusable, so we emit a warning and return true to indicate that
1373/// there was an error.
1374///
1375/// \returns true if the PCH file is unacceptable, false otherwise.
1376bool PCHReader::ParseLanguageOptions(
1377 const llvm::SmallVectorImpl<uint64_t> &Record) {
1378 const LangOptions &LangOpts = Context.getLangOptions();
1379#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1380#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1381 if (Record[Idx] != LangOpts.Option) { \
1382 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1383 Diag(diag::note_ignoring_pch) << FileName; \
1384 return true; \
1385 } \
1386 ++Idx
1387
1388 unsigned Idx = 0;
1389 PARSE_LANGOPT_BENIGN(Trigraphs);
1390 PARSE_LANGOPT_BENIGN(BCPLComment);
1391 PARSE_LANGOPT_BENIGN(DollarIdents);
1392 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1393 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1394 PARSE_LANGOPT_BENIGN(ImplicitInt);
1395 PARSE_LANGOPT_BENIGN(Digraphs);
1396 PARSE_LANGOPT_BENIGN(HexFloats);
1397 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1398 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1399 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1400 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1401 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1402 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1403 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1404 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1405 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1406 PARSE_LANGOPT_BENIGN(PascalStrings);
1407 PARSE_LANGOPT_BENIGN(Boolean);
1408 PARSE_LANGOPT_BENIGN(WritableStrings);
1409 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1410 diag::warn_pch_lax_vector_conversions);
1411 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1412 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1413 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1414 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1415 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1416 diag::warn_pch_thread_safe_statics);
1417 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1418 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1419 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1420 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1421 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1422 diag::warn_pch_heinous_extensions);
1423 // FIXME: Most of the options below are benign if the macro wasn't
1424 // used. Unfortunately, this means that a PCH compiled without
1425 // optimization can't be used with optimization turned on, even
1426 // though the only thing that changes is whether __OPTIMIZE__ was
1427 // defined... but if __OPTIMIZE__ never showed up in the header, it
1428 // doesn't matter. We could consider making this some special kind
1429 // of check.
1430 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1431 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1432 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1433 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1434 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1435 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1436 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1437 Diag(diag::warn_pch_gc_mode)
1438 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1439 Diag(diag::note_ignoring_pch) << FileName;
1440 return true;
1441 }
1442 ++Idx;
1443 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1444 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1445#undef PARSE_LANGOPT_IRRELEVANT
1446#undef PARSE_LANGOPT_BENIGN
1447
1448 return false;
1449}
1450
Douglas Gregorc34897d2009-04-09 22:27:44 +00001451/// \brief Read and return the type at the given offset.
1452///
1453/// This routine actually reads the record corresponding to the type
1454/// at the given offset in the bitstream. It is a helper routine for
1455/// GetType, which deals with reading type IDs.
1456QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001457 // Keep track of where we are in the stream, then jump back there
1458 // after reading this type.
1459 SavedStreamPosition SavedPosition(Stream);
1460
Douglas Gregorc34897d2009-04-09 22:27:44 +00001461 Stream.JumpToBit(Offset);
1462 RecordData Record;
1463 unsigned Code = Stream.ReadCode();
1464 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001465 case pch::TYPE_EXT_QUAL: {
1466 assert(Record.size() == 3 &&
1467 "Incorrect encoding of extended qualifier type");
1468 QualType Base = GetType(Record[0]);
1469 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1470 unsigned AddressSpace = Record[2];
1471
1472 QualType T = Base;
1473 if (GCAttr != QualType::GCNone)
1474 T = Context.getObjCGCQualType(T, GCAttr);
1475 if (AddressSpace)
1476 T = Context.getAddrSpaceQualType(T, AddressSpace);
1477 return T;
1478 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001479
Douglas Gregorc34897d2009-04-09 22:27:44 +00001480 case pch::TYPE_FIXED_WIDTH_INT: {
1481 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1482 return Context.getFixedWidthIntType(Record[0], Record[1]);
1483 }
1484
1485 case pch::TYPE_COMPLEX: {
1486 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1487 QualType ElemType = GetType(Record[0]);
1488 return Context.getComplexType(ElemType);
1489 }
1490
1491 case pch::TYPE_POINTER: {
1492 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1493 QualType PointeeType = GetType(Record[0]);
1494 return Context.getPointerType(PointeeType);
1495 }
1496
1497 case pch::TYPE_BLOCK_POINTER: {
1498 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1499 QualType PointeeType = GetType(Record[0]);
1500 return Context.getBlockPointerType(PointeeType);
1501 }
1502
1503 case pch::TYPE_LVALUE_REFERENCE: {
1504 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1505 QualType PointeeType = GetType(Record[0]);
1506 return Context.getLValueReferenceType(PointeeType);
1507 }
1508
1509 case pch::TYPE_RVALUE_REFERENCE: {
1510 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1511 QualType PointeeType = GetType(Record[0]);
1512 return Context.getRValueReferenceType(PointeeType);
1513 }
1514
1515 case pch::TYPE_MEMBER_POINTER: {
1516 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1517 QualType PointeeType = GetType(Record[0]);
1518 QualType ClassType = GetType(Record[1]);
1519 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1520 }
1521
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001522 case pch::TYPE_CONSTANT_ARRAY: {
1523 QualType ElementType = GetType(Record[0]);
1524 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1525 unsigned IndexTypeQuals = Record[2];
1526 unsigned Idx = 3;
1527 llvm::APInt Size = ReadAPInt(Record, Idx);
1528 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1529 }
1530
1531 case pch::TYPE_INCOMPLETE_ARRAY: {
1532 QualType ElementType = GetType(Record[0]);
1533 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1534 unsigned IndexTypeQuals = Record[2];
1535 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1536 }
1537
1538 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001539 QualType ElementType = GetType(Record[0]);
1540 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1541 unsigned IndexTypeQuals = Record[2];
1542 return Context.getVariableArrayType(ElementType, ReadExpr(),
1543 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001544 }
1545
1546 case pch::TYPE_VECTOR: {
1547 if (Record.size() != 2) {
1548 Error("Incorrect encoding of vector type in PCH file");
1549 return QualType();
1550 }
1551
1552 QualType ElementType = GetType(Record[0]);
1553 unsigned NumElements = Record[1];
1554 return Context.getVectorType(ElementType, NumElements);
1555 }
1556
1557 case pch::TYPE_EXT_VECTOR: {
1558 if (Record.size() != 2) {
1559 Error("Incorrect encoding of extended vector type in PCH file");
1560 return QualType();
1561 }
1562
1563 QualType ElementType = GetType(Record[0]);
1564 unsigned NumElements = Record[1];
1565 return Context.getExtVectorType(ElementType, NumElements);
1566 }
1567
1568 case pch::TYPE_FUNCTION_NO_PROTO: {
1569 if (Record.size() != 1) {
1570 Error("Incorrect encoding of no-proto function type");
1571 return QualType();
1572 }
1573 QualType ResultType = GetType(Record[0]);
1574 return Context.getFunctionNoProtoType(ResultType);
1575 }
1576
1577 case pch::TYPE_FUNCTION_PROTO: {
1578 QualType ResultType = GetType(Record[0]);
1579 unsigned Idx = 1;
1580 unsigned NumParams = Record[Idx++];
1581 llvm::SmallVector<QualType, 16> ParamTypes;
1582 for (unsigned I = 0; I != NumParams; ++I)
1583 ParamTypes.push_back(GetType(Record[Idx++]));
1584 bool isVariadic = Record[Idx++];
1585 unsigned Quals = Record[Idx++];
1586 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1587 isVariadic, Quals);
1588 }
1589
1590 case pch::TYPE_TYPEDEF:
1591 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1592 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1593
1594 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001595 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001596
1597 case pch::TYPE_TYPEOF: {
1598 if (Record.size() != 1) {
1599 Error("Incorrect encoding of typeof(type) in PCH file");
1600 return QualType();
1601 }
1602 QualType UnderlyingType = GetType(Record[0]);
1603 return Context.getTypeOfType(UnderlyingType);
1604 }
1605
1606 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001607 assert(Record.size() == 1 && "Incorrect encoding of record type");
1608 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001609
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001610 case pch::TYPE_ENUM:
1611 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1612 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1613
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001614 case pch::TYPE_OBJC_INTERFACE:
1615 // FIXME: Deserialize ObjCInterfaceType
1616 assert(false && "Cannot de-serialize ObjC interface types yet");
1617 return QualType();
1618
1619 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1620 // FIXME: Deserialize ObjCQualifiedInterfaceType
1621 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1622 return QualType();
1623
1624 case pch::TYPE_OBJC_QUALIFIED_ID:
1625 // FIXME: Deserialize ObjCQualifiedIdType
1626 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1627 return QualType();
1628
1629 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1630 // FIXME: Deserialize ObjCQualifiedClassType
1631 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1632 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633 }
1634
1635 // Suppress a GCC warning
1636 return QualType();
1637}
1638
1639/// \brief Note that we have loaded the declaration with the given
1640/// Index.
1641///
1642/// This routine notes that this declaration has already been loaded,
1643/// so that future GetDecl calls will return this declaration rather
1644/// than trying to load a new declaration.
1645inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1646 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1647 DeclAlreadyLoaded[Index] = true;
1648 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1649}
1650
1651/// \brief Read the declaration at the given offset from the PCH file.
1652Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001653 // Keep track of where we are in the stream, then jump back there
1654 // after reading this declaration.
1655 SavedStreamPosition SavedPosition(Stream);
1656
Douglas Gregorc34897d2009-04-09 22:27:44 +00001657 Decl *D = 0;
1658 Stream.JumpToBit(Offset);
1659 RecordData Record;
1660 unsigned Code = Stream.ReadCode();
1661 unsigned Idx = 0;
1662 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001663
Douglas Gregorc34897d2009-04-09 22:27:44 +00001664 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001665 case pch::DECL_ATTR:
1666 case pch::DECL_CONTEXT_LEXICAL:
1667 case pch::DECL_CONTEXT_VISIBLE:
1668 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1669 break;
1670
Douglas Gregorc34897d2009-04-09 22:27:44 +00001671 case pch::DECL_TRANSLATION_UNIT:
1672 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001673 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001674 break;
1675
1676 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001677 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001678 break;
1679 }
1680
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001681 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001682 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001683 break;
1684 }
1685
Douglas Gregor982365e2009-04-13 21:20:57 +00001686 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001687 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1688 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001689 break;
1690 }
1691
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001692 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001693 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1694 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001695 break;
1696 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001697
1698 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001699 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1700 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001701 break;
1702 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001703
Douglas Gregor982365e2009-04-13 21:20:57 +00001704 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001705 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1706 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001707 break;
1708 }
1709
Douglas Gregorc34897d2009-04-09 22:27:44 +00001710 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001711 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1712 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001713 break;
1714 }
1715
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001716 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001717 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1718 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001719 break;
1720 }
1721
1722 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001723 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001724 QualType(), QualType(), VarDecl::None,
1725 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001726 break;
1727 }
1728
Douglas Gregor2a491792009-04-13 22:49:25 +00001729 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001730 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001731 break;
1732 }
1733
1734 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001735 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001736 break;
1737 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001738 }
1739
Douglas Gregorddf4d092009-04-16 22:29:51 +00001740 assert(D && "Unknown declaration creating PCH file");
1741 if (D) {
1742 LoadedDecl(Index, D);
1743 Reader.Visit(D);
1744 }
1745
Douglas Gregorc34897d2009-04-09 22:27:44 +00001746 // If this declaration is also a declaration context, get the
1747 // offsets for its tables of lexical and visible declarations.
1748 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1749 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1750 if (Offsets.first || Offsets.second) {
1751 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1752 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1753 DeclContextOffsets[DC] = Offsets;
1754 }
1755 }
1756 assert(Idx == Record.size());
1757
1758 return D;
1759}
1760
Douglas Gregorac8f2802009-04-10 17:25:41 +00001761QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001762 unsigned Quals = ID & 0x07;
1763 unsigned Index = ID >> 3;
1764
1765 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1766 QualType T;
1767 switch ((pch::PredefinedTypeIDs)Index) {
1768 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1769 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1770 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1771
1772 case pch::PREDEF_TYPE_CHAR_U_ID:
1773 case pch::PREDEF_TYPE_CHAR_S_ID:
1774 // FIXME: Check that the signedness of CharTy is correct!
1775 T = Context.CharTy;
1776 break;
1777
1778 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1779 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1780 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1781 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1782 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1783 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1784 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1785 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1786 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1787 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1788 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1789 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1790 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1791 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1792 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1793 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1794 }
1795
1796 assert(!T.isNull() && "Unknown predefined type");
1797 return T.getQualifiedType(Quals);
1798 }
1799
1800 Index -= pch::NUM_PREDEF_TYPE_IDS;
1801 if (!TypeAlreadyLoaded[Index]) {
1802 // Load the type from the PCH file.
1803 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1804 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1805 TypeAlreadyLoaded[Index] = true;
1806 }
1807
1808 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1809}
1810
Douglas Gregorac8f2802009-04-10 17:25:41 +00001811Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001812 if (ID == 0)
1813 return 0;
1814
1815 unsigned Index = ID - 1;
1816 if (DeclAlreadyLoaded[Index])
1817 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1818
1819 // Load the declaration from the PCH file.
1820 return ReadDeclRecord(DeclOffsets[Index], Index);
1821}
1822
1823bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001824 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001825 assert(DC->hasExternalLexicalStorage() &&
1826 "DeclContext has no lexical decls in storage");
1827 uint64_t Offset = DeclContextOffsets[DC].first;
1828 assert(Offset && "DeclContext has no lexical decls in storage");
1829
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001830 // Keep track of where we are in the stream, then jump back there
1831 // after reading this context.
1832 SavedStreamPosition SavedPosition(Stream);
1833
Douglas Gregorc34897d2009-04-09 22:27:44 +00001834 // Load the record containing all of the declarations lexically in
1835 // this context.
1836 Stream.JumpToBit(Offset);
1837 RecordData Record;
1838 unsigned Code = Stream.ReadCode();
1839 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001840 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001841 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1842
1843 // Load all of the declaration IDs
1844 Decls.clear();
1845 Decls.insert(Decls.end(), Record.begin(), Record.end());
1846 return false;
1847}
1848
1849bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1850 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1851 assert(DC->hasExternalVisibleStorage() &&
1852 "DeclContext has no visible decls in storage");
1853 uint64_t Offset = DeclContextOffsets[DC].second;
1854 assert(Offset && "DeclContext has no visible decls in storage");
1855
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001856 // Keep track of where we are in the stream, then jump back there
1857 // after reading this context.
1858 SavedStreamPosition SavedPosition(Stream);
1859
Douglas Gregorc34897d2009-04-09 22:27:44 +00001860 // Load the record containing all of the declarations visible in
1861 // this context.
1862 Stream.JumpToBit(Offset);
1863 RecordData Record;
1864 unsigned Code = Stream.ReadCode();
1865 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001866 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001867 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1868 if (Record.size() == 0)
1869 return false;
1870
1871 Decls.clear();
1872
1873 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001874 while (Idx < Record.size()) {
1875 Decls.push_back(VisibleDeclaration());
1876 Decls.back().Name = ReadDeclarationName(Record, Idx);
1877
Douglas Gregorc34897d2009-04-09 22:27:44 +00001878 unsigned Size = Record[Idx++];
1879 llvm::SmallVector<unsigned, 4> & LoadedDecls
1880 = Decls.back().Declarations;
1881 LoadedDecls.reserve(Size);
1882 for (unsigned I = 0; I < Size; ++I)
1883 LoadedDecls.push_back(Record[Idx++]);
1884 }
1885
1886 return false;
1887}
1888
Douglas Gregor631f6c62009-04-14 00:24:19 +00001889void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1890 if (!Consumer)
1891 return;
1892
1893 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1894 Decl *D = GetDecl(ExternalDefinitions[I]);
1895 DeclGroupRef DG(D);
1896 Consumer->HandleTopLevelDecl(DG);
1897 }
1898}
1899
Douglas Gregorc34897d2009-04-09 22:27:44 +00001900void PCHReader::PrintStats() {
1901 std::fprintf(stderr, "*** PCH Statistics:\n");
1902
1903 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1904 TypeAlreadyLoaded.end(),
1905 true);
1906 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1907 DeclAlreadyLoaded.end(),
1908 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001909 unsigned NumIdentifiersLoaded = 0;
1910 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1911 if ((IdentifierData[I] & 0x01) == 0)
1912 ++NumIdentifiersLoaded;
1913 }
1914
Douglas Gregorc34897d2009-04-09 22:27:44 +00001915 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1916 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001917 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001918 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1919 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001920 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1921 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1922 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1923 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001924 std::fprintf(stderr, "\n");
1925}
1926
Chris Lattner29241862009-04-11 21:15:38 +00001927IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001928 if (ID == 0)
1929 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001930
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001931 if (!IdentifierTable || IdentifierData.empty()) {
1932 Error("No identifier table in PCH file");
1933 return 0;
1934 }
Chris Lattner29241862009-04-11 21:15:38 +00001935
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001936 if (IdentifierData[ID - 1] & 0x01) {
1937 uint64_t Offset = IdentifierData[ID - 1];
1938 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001939 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001940 }
Chris Lattner29241862009-04-11 21:15:38 +00001941
1942 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001943}
1944
1945DeclarationName
1946PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1947 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1948 switch (Kind) {
1949 case DeclarationName::Identifier:
1950 return DeclarationName(GetIdentifierInfo(Record, Idx));
1951
1952 case DeclarationName::ObjCZeroArgSelector:
1953 case DeclarationName::ObjCOneArgSelector:
1954 case DeclarationName::ObjCMultiArgSelector:
1955 assert(false && "Unable to de-serialize Objective-C selectors");
1956 break;
1957
1958 case DeclarationName::CXXConstructorName:
1959 return Context.DeclarationNames.getCXXConstructorName(
1960 GetType(Record[Idx++]));
1961
1962 case DeclarationName::CXXDestructorName:
1963 return Context.DeclarationNames.getCXXDestructorName(
1964 GetType(Record[Idx++]));
1965
1966 case DeclarationName::CXXConversionFunctionName:
1967 return Context.DeclarationNames.getCXXConversionFunctionName(
1968 GetType(Record[Idx++]));
1969
1970 case DeclarationName::CXXOperatorName:
1971 return Context.DeclarationNames.getCXXOperatorName(
1972 (OverloadedOperatorKind)Record[Idx++]);
1973
1974 case DeclarationName::CXXUsingDirective:
1975 return DeclarationName::getUsingDirectiveName();
1976 }
1977
1978 // Required to silence GCC warning
1979 return DeclarationName();
1980}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001981
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001982/// \brief Read an integral value
1983llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1984 unsigned BitWidth = Record[Idx++];
1985 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1986 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1987 Idx += NumWords;
1988 return Result;
1989}
1990
1991/// \brief Read a signed integral value
1992llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1993 bool isUnsigned = Record[Idx++];
1994 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1995}
1996
Douglas Gregore2f37202009-04-14 21:55:33 +00001997/// \brief Read a floating-point value
1998llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001999 return llvm::APFloat(ReadAPInt(Record, Idx));
2000}
2001
Douglas Gregor1c507882009-04-15 21:30:51 +00002002// \brief Read a string
2003std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2004 unsigned Len = Record[Idx++];
2005 std::string Result(&Record[Idx], &Record[Idx] + Len);
2006 Idx += Len;
2007 return Result;
2008}
2009
2010/// \brief Reads attributes from the current stream position.
2011Attr *PCHReader::ReadAttributes() {
2012 unsigned Code = Stream.ReadCode();
2013 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2014 "Expected unabbreviated record"); (void)Code;
2015
2016 RecordData Record;
2017 unsigned Idx = 0;
2018 unsigned RecCode = Stream.ReadRecord(Code, Record);
2019 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2020 (void)RecCode;
2021
2022#define SIMPLE_ATTR(Name) \
2023 case Attr::Name: \
2024 New = ::new (Context) Name##Attr(); \
2025 break
2026
2027#define STRING_ATTR(Name) \
2028 case Attr::Name: \
2029 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2030 break
2031
2032#define UNSIGNED_ATTR(Name) \
2033 case Attr::Name: \
2034 New = ::new (Context) Name##Attr(Record[Idx++]); \
2035 break
2036
2037 Attr *Attrs = 0;
2038 while (Idx < Record.size()) {
2039 Attr *New = 0;
2040 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2041 bool IsInherited = Record[Idx++];
2042
2043 switch (Kind) {
2044 STRING_ATTR(Alias);
2045 UNSIGNED_ATTR(Aligned);
2046 SIMPLE_ATTR(AlwaysInline);
2047 SIMPLE_ATTR(AnalyzerNoReturn);
2048 STRING_ATTR(Annotate);
2049 STRING_ATTR(AsmLabel);
2050
2051 case Attr::Blocks:
2052 New = ::new (Context) BlocksAttr(
2053 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2054 break;
2055
2056 case Attr::Cleanup:
2057 New = ::new (Context) CleanupAttr(
2058 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2059 break;
2060
2061 SIMPLE_ATTR(Const);
2062 UNSIGNED_ATTR(Constructor);
2063 SIMPLE_ATTR(DLLExport);
2064 SIMPLE_ATTR(DLLImport);
2065 SIMPLE_ATTR(Deprecated);
2066 UNSIGNED_ATTR(Destructor);
2067 SIMPLE_ATTR(FastCall);
2068
2069 case Attr::Format: {
2070 std::string Type = ReadString(Record, Idx);
2071 unsigned FormatIdx = Record[Idx++];
2072 unsigned FirstArg = Record[Idx++];
2073 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2074 break;
2075 }
2076
2077 SIMPLE_ATTR(GNUCInline);
2078
2079 case Attr::IBOutletKind:
2080 New = ::new (Context) IBOutletAttr();
2081 break;
2082
2083 SIMPLE_ATTR(NoReturn);
2084 SIMPLE_ATTR(NoThrow);
2085 SIMPLE_ATTR(Nodebug);
2086 SIMPLE_ATTR(Noinline);
2087
2088 case Attr::NonNull: {
2089 unsigned Size = Record[Idx++];
2090 llvm::SmallVector<unsigned, 16> ArgNums;
2091 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2092 Idx += Size;
2093 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2094 break;
2095 }
2096
2097 SIMPLE_ATTR(ObjCException);
2098 SIMPLE_ATTR(ObjCNSObject);
2099 SIMPLE_ATTR(Overloadable);
2100 UNSIGNED_ATTR(Packed);
2101 SIMPLE_ATTR(Pure);
2102 UNSIGNED_ATTR(Regparm);
2103 STRING_ATTR(Section);
2104 SIMPLE_ATTR(StdCall);
2105 SIMPLE_ATTR(TransparentUnion);
2106 SIMPLE_ATTR(Unavailable);
2107 SIMPLE_ATTR(Unused);
2108 SIMPLE_ATTR(Used);
2109
2110 case Attr::Visibility:
2111 New = ::new (Context) VisibilityAttr(
2112 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2113 break;
2114
2115 SIMPLE_ATTR(WarnUnusedResult);
2116 SIMPLE_ATTR(Weak);
2117 SIMPLE_ATTR(WeakImport);
2118 }
2119
2120 assert(New && "Unable to decode attribute?");
2121 New->setInherited(IsInherited);
2122 New->setNext(Attrs);
2123 Attrs = New;
2124 }
2125#undef UNSIGNED_ATTR
2126#undef STRING_ATTR
2127#undef SIMPLE_ATTR
2128
2129 // The list of attributes was built backwards. Reverse the list
2130 // before returning it.
2131 Attr *PrevAttr = 0, *NextAttr = 0;
2132 while (Attrs) {
2133 NextAttr = Attrs->getNext();
2134 Attrs->setNext(PrevAttr);
2135 PrevAttr = Attrs;
2136 Attrs = NextAttr;
2137 }
2138
2139 return PrevAttr;
2140}
2141
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002142Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002143 // Within the bitstream, expressions are stored in Reverse Polish
2144 // Notation, with each of the subexpressions preceding the
2145 // expression they are stored in. To evaluate expressions, we
2146 // continue reading expressions and placing them on the stack, with
2147 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002148 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002149 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002150 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002151 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002152 llvm::SmallVector<Stmt *, 16> StmtStack;
2153 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002154 Stmt::EmptyShell Empty;
2155
Douglas Gregora151ba42009-04-14 23:32:43 +00002156 while (true) {
2157 unsigned Code = Stream.ReadCode();
2158 if (Code == llvm::bitc::END_BLOCK) {
2159 if (Stream.ReadBlockEnd()) {
2160 Error("Error at end of Source Manager block");
2161 return 0;
2162 }
2163 break;
2164 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002165
Douglas Gregora151ba42009-04-14 23:32:43 +00002166 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2167 // No known subblocks, always skip them.
2168 Stream.ReadSubBlockID();
2169 if (Stream.SkipBlock()) {
2170 Error("Malformed block record");
2171 return 0;
2172 }
2173 continue;
2174 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002175
Douglas Gregora151ba42009-04-14 23:32:43 +00002176 if (Code == llvm::bitc::DEFINE_ABBREV) {
2177 Stream.ReadAbbrevRecord();
2178 continue;
2179 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002180
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002181 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002182 Idx = 0;
2183 Record.clear();
2184 bool Finished = false;
2185 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002186 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002187 Finished = true;
2188 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002189
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002190 case pch::STMT_NULL_PTR:
2191 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002192 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002193
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002194 case pch::STMT_NULL:
2195 S = new (Context) NullStmt(Empty);
2196 break;
2197
2198 case pch::STMT_COMPOUND:
2199 S = new (Context) CompoundStmt(Empty);
2200 break;
2201
2202 case pch::STMT_CASE:
2203 S = new (Context) CaseStmt(Empty);
2204 break;
2205
2206 case pch::STMT_DEFAULT:
2207 S = new (Context) DefaultStmt(Empty);
2208 break;
2209
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002210 case pch::STMT_LABEL:
2211 S = new (Context) LabelStmt(Empty);
2212 break;
2213
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002214 case pch::STMT_IF:
2215 S = new (Context) IfStmt(Empty);
2216 break;
2217
2218 case pch::STMT_SWITCH:
2219 S = new (Context) SwitchStmt(Empty);
2220 break;
2221
Douglas Gregora6b503f2009-04-17 00:16:09 +00002222 case pch::STMT_WHILE:
2223 S = new (Context) WhileStmt(Empty);
2224 break;
2225
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002226 case pch::STMT_DO:
2227 S = new (Context) DoStmt(Empty);
2228 break;
2229
2230 case pch::STMT_FOR:
2231 S = new (Context) ForStmt(Empty);
2232 break;
2233
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002234 case pch::STMT_GOTO:
2235 S = new (Context) GotoStmt(Empty);
2236 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002237
2238 case pch::STMT_INDIRECT_GOTO:
2239 S = new (Context) IndirectGotoStmt(Empty);
2240 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002241
Douglas Gregora6b503f2009-04-17 00:16:09 +00002242 case pch::STMT_CONTINUE:
2243 S = new (Context) ContinueStmt(Empty);
2244 break;
2245
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002246 case pch::STMT_BREAK:
2247 S = new (Context) BreakStmt(Empty);
2248 break;
2249
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002250 case pch::STMT_RETURN:
2251 S = new (Context) ReturnStmt(Empty);
2252 break;
2253
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002254 case pch::STMT_DECL:
2255 S = new (Context) DeclStmt(Empty);
2256 break;
2257
Douglas Gregora151ba42009-04-14 23:32:43 +00002258 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002259 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002260 break;
2261
2262 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002263 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002264 break;
2265
2266 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002267 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002268 break;
2269
2270 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002271 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002272 break;
2273
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002274 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002275 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002276 break;
2277
Douglas Gregor596e0932009-04-15 16:35:07 +00002278 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002279 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002280 Record[PCHStmtReader::NumExprFields + 1]);
2281 break;
2282
Douglas Gregora151ba42009-04-14 23:32:43 +00002283 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002284 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002285 break;
2286
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002287 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002288 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002289 break;
2290
Douglas Gregor12d74052009-04-15 15:58:59 +00002291 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002292 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002293 break;
2294
2295 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002296 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002297 break;
2298
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002299 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002300 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002301 break;
2302
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002303 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002304 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002305 break;
2306
2307 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002308 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002309 break;
2310
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002311 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002312 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002313 break;
2314
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002315 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002316 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002317 break;
2318
2319 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002320 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002321 break;
2322
Douglas Gregora151ba42009-04-14 23:32:43 +00002323 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002324 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002325 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002326
2327 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002328 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002329 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002330
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002331 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002332 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002333 break;
2334
Douglas Gregorec0b8292009-04-15 23:02:49 +00002335 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002336 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002337 break;
2338
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002339 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002340 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002341 break;
2342
2343 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002344 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002345 Record[PCHStmtReader::NumExprFields] - 1);
2346
2347 break;
2348
2349 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002350 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002351 break;
2352
Douglas Gregorec0b8292009-04-15 23:02:49 +00002353 case pch::EXPR_VA_ARG:
2354 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002355 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002356 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002357
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002358 case pch::EXPR_ADDR_LABEL:
2359 S = new (Context) AddrLabelExpr(Empty);
2360 break;
2361
Douglas Gregor209d4622009-04-15 23:33:31 +00002362 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002363 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002364 break;
2365
2366 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002367 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002368 break;
2369
2370 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002371 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002372 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002373
2374 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002375 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002376 break;
2377
2378 case pch::EXPR_BLOCK_DECL_REF:
2379 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002380 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002381 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002382 }
2383
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002384 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002385 if (Finished)
2386 break;
2387
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002388 if (S) {
2389 unsigned NumSubStmts = Reader.Visit(S);
2390 while (NumSubStmts > 0) {
2391 StmtStack.pop_back();
2392 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002393 }
2394 }
2395
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002396 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002397 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002398 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002399 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002400 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002401 return StmtStack.back();
2402}
2403
2404Expr *PCHReader::ReadExpr() {
2405 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002406}
2407
Douglas Gregor179cfb12009-04-10 20:39:37 +00002408DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002409 return Diag(SourceLocation(), DiagID);
2410}
2411
2412DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2413 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002414 Context.getSourceManager()),
2415 DiagID);
2416}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002417
2418/// \brief Record that the given ID maps to the given switch-case
2419/// statement.
2420void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2421 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2422 SwitchCaseStmts[ID] = SC;
2423}
2424
2425/// \brief Retrieve the switch-case statement with the given ID.
2426SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2427 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2428 return SwitchCaseStmts[ID];
2429}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002430
2431/// \brief Record that the given label statement has been
2432/// deserialized and has the given ID.
2433void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2434 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2435 "Deserialized label twice");
2436 LabelStmts[ID] = S;
2437
2438 // If we've already seen any goto statements that point to this
2439 // label, resolve them now.
2440 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2441 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2442 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2443 Goto->second->setLabel(S);
2444 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002445
2446 // If we've already seen any address-label statements that point to
2447 // this label, resolve them now.
2448 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2449 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2450 = UnresolvedAddrLabelExprs.equal_range(ID);
2451 for (AddrLabelIter AddrLabel = AddrLabels.first;
2452 AddrLabel != AddrLabels.second; ++AddrLabel)
2453 AddrLabel->second->setLabel(S);
2454 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002455}
2456
2457/// \brief Set the label of the given statement to the label
2458/// identified by ID.
2459///
2460/// Depending on the order in which the label and other statements
2461/// referencing that label occur, this operation may complete
2462/// immediately (updating the statement) or it may queue the
2463/// statement to be back-patched later.
2464void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2465 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2466 if (Label != LabelStmts.end()) {
2467 // We've already seen this label, so set the label of the goto and
2468 // we're done.
2469 S->setLabel(Label->second);
2470 } else {
2471 // We haven't seen this label yet, so add this goto to the set of
2472 // unresolved goto statements.
2473 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2474 }
2475}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002476
2477/// \brief Set the label of the given expression to the label
2478/// identified by ID.
2479///
2480/// Depending on the order in which the label and other statements
2481/// referencing that label occur, this operation may complete
2482/// immediately (updating the statement) or it may queue the
2483/// statement to be back-patched later.
2484void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2485 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2486 if (Label != LabelStmts.end()) {
2487 // We've already seen this label, so set the label of the
2488 // label-address expression and we're done.
2489 S->setLabel(Label->second);
2490 } else {
2491 // We haven't seen this label yet, so add this label-address
2492 // expression to the set of unresolved label-address expressions.
2493 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2494 }
2495}