blob: 50378966e1614c58a420f583e4dc93772faea4f1 [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);
252 unsigned VisitIfStmt(IfStmt *S);
253 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000254 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000255 unsigned VisitDoStmt(DoStmt *S);
256 unsigned VisitForStmt(ForStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000257 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000258 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000259 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000260 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000261 unsigned VisitExpr(Expr *E);
262 unsigned VisitPredefinedExpr(PredefinedExpr *E);
263 unsigned VisitDeclRefExpr(DeclRefExpr *E);
264 unsigned VisitIntegerLiteral(IntegerLiteral *E);
265 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000266 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000267 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000268 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000269 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000270 unsigned VisitUnaryOperator(UnaryOperator *E);
271 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000272 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000273 unsigned VisitCallExpr(CallExpr *E);
274 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000275 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000276 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000277 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
278 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000279 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000280 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
281 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000282 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000283 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000284 unsigned VisitInitListExpr(InitListExpr *E);
285 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
286 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000287 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000288 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
289 unsigned VisitChooseExpr(ChooseExpr *E);
290 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000291 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
292 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000293 };
294}
295
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000296unsigned PCHStmtReader::VisitStmt(Stmt *S) {
297 assert(Idx == NumStmtFields && "Incorrect statement field count");
298 return 0;
299}
300
301unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
302 VisitStmt(S);
303 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
304 return 0;
305}
306
307unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
308 VisitStmt(S);
309 unsigned NumStmts = Record[Idx++];
310 S->setStmts(Reader.getContext(),
311 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
312 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
313 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
314 return NumStmts;
315}
316
317unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
318 VisitStmt(S);
319 Reader.RecordSwitchCaseID(S, Record[Idx++]);
320 return 0;
321}
322
323unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
324 VisitSwitchCase(S);
325 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
326 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
327 S->setSubStmt(StmtStack.back());
328 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
329 return 3;
330}
331
332unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
333 VisitSwitchCase(S);
334 S->setSubStmt(StmtStack.back());
335 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
336 return 1;
337}
338
339unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
340 VisitStmt(S);
341 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
342 S->setThen(StmtStack[StmtStack.size() - 2]);
343 S->setElse(StmtStack[StmtStack.size() - 1]);
344 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
345 return 3;
346}
347
348unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
349 VisitStmt(S);
350 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
351 S->setBody(StmtStack.back());
352 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
353 SwitchCase *PrevSC = 0;
354 for (unsigned N = Record.size(); Idx != N; ++Idx) {
355 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
356 if (PrevSC)
357 PrevSC->setNextSwitchCase(SC);
358 else
359 S->setSwitchCaseList(SC);
360 PrevSC = SC;
361 }
362 return 2;
363}
364
Douglas Gregora6b503f2009-04-17 00:16:09 +0000365unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
366 VisitStmt(S);
367 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
368 S->setBody(StmtStack.back());
369 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
370 return 2;
371}
372
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000373unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
374 VisitStmt(S);
375 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
376 S->setBody(StmtStack.back());
377 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
378 return 2;
379}
380
381unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
382 VisitStmt(S);
383 S->setInit(StmtStack[StmtStack.size() - 4]);
384 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
385 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
386 S->setBody(StmtStack.back());
387 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
388 return 4;
389}
390
Douglas Gregora6b503f2009-04-17 00:16:09 +0000391unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
392 VisitStmt(S);
393 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
394 return 0;
395}
396
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000397unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
398 VisitStmt(S);
399 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
400 return 0;
401}
402
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000403unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
404 VisitStmt(S);
405 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
406 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
407 return 1;
408}
409
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000410unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
411 VisitStmt(S);
412 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
414
415 if (Idx + 1 == Record.size()) {
416 // Single declaration
417 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
418 } else {
419 llvm::SmallVector<Decl *, 16> Decls;
420 Decls.reserve(Record.size() - Idx);
421 for (unsigned N = Record.size(); Idx != N; ++Idx)
422 Decls.push_back(Reader.GetDecl(Record[Idx]));
423 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
424 &Decls[0], Decls.size())));
425 }
426 return 0;
427}
428
Douglas Gregora151ba42009-04-14 23:32:43 +0000429unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000430 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000431 E->setType(Reader.GetType(Record[Idx++]));
432 E->setTypeDependent(Record[Idx++]);
433 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000434 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000435 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000436}
437
Douglas Gregora151ba42009-04-14 23:32:43 +0000438unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000439 VisitExpr(E);
440 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
441 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000442 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000443}
444
Douglas Gregora151ba42009-04-14 23:32:43 +0000445unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000446 VisitExpr(E);
447 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
448 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000449 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000450}
451
Douglas Gregora151ba42009-04-14 23:32:43 +0000452unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000453 VisitExpr(E);
454 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
455 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000456 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000457}
458
Douglas Gregora151ba42009-04-14 23:32:43 +0000459unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000460 VisitExpr(E);
461 E->setValue(Reader.ReadAPFloat(Record, Idx));
462 E->setExact(Record[Idx++]);
463 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000464 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000465}
466
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000467unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
468 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000469 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000470 return 1;
471}
472
Douglas Gregor596e0932009-04-15 16:35:07 +0000473unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
474 VisitExpr(E);
475 unsigned Len = Record[Idx++];
476 assert(Record[Idx] == E->getNumConcatenated() &&
477 "Wrong number of concatenated tokens!");
478 ++Idx;
479 E->setWide(Record[Idx++]);
480
481 // Read string data
482 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
483 E->setStrData(Reader.getContext(), &Str[0], Len);
484 Idx += Len;
485
486 // Read source locations
487 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
488 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
489
490 return 0;
491}
492
Douglas Gregora151ba42009-04-14 23:32:43 +0000493unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000494 VisitExpr(E);
495 E->setValue(Record[Idx++]);
496 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
497 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000498 return 0;
499}
500
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000501unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
502 VisitExpr(E);
503 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
504 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000505 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000506 return 1;
507}
508
Douglas Gregor12d74052009-04-15 15:58:59 +0000509unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
510 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000511 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000512 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
513 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
514 return 1;
515}
516
517unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
518 VisitExpr(E);
519 E->setSizeof(Record[Idx++]);
520 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000521 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000522 ++Idx;
523 } else {
524 E->setArgument(Reader.GetType(Record[Idx++]));
525 }
526 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
527 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
528 return E->isArgumentType()? 0 : 1;
529}
530
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000531unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
532 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000533 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
534 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000535 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
536 return 2;
537}
538
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000539unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
540 VisitExpr(E);
541 E->setNumArgs(Reader.getContext(), Record[Idx++]);
542 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000543 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000544 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000545 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000546 return E->getNumArgs() + 1;
547}
548
549unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
550 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000551 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000552 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
553 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
554 E->setArrow(Record[Idx++]);
555 return 1;
556}
557
Douglas Gregora151ba42009-04-14 23:32:43 +0000558unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
559 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000560 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000561 return 1;
562}
563
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000564unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
565 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000566 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
567 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000568 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
569 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
570 return 2;
571}
572
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000573unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
574 VisitBinaryOperator(E);
575 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
576 E->setComputationResultType(Reader.GetType(Record[Idx++]));
577 return 2;
578}
579
580unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
581 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000582 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
583 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
584 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000585 return 3;
586}
587
Douglas Gregora151ba42009-04-14 23:32:43 +0000588unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
589 VisitCastExpr(E);
590 E->setLvalueCast(Record[Idx++]);
591 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000592}
593
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000594unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
595 VisitCastExpr(E);
596 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
597 return 1;
598}
599
600unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
601 VisitExplicitCastExpr(E);
602 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 1;
605}
606
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000607unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
608 VisitExpr(E);
609 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000610 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000611 E->setFileScope(Record[Idx++]);
612 return 1;
613}
614
Douglas Gregorec0b8292009-04-15 23:02:49 +0000615unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
616 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000617 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000618 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
619 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
620 return 1;
621}
622
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000623unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
624 VisitExpr(E);
625 unsigned NumInits = Record[Idx++];
626 E->reserveInits(NumInits);
627 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000628 E->updateInit(I,
629 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
630 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000631 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
632 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
633 E->setInitializedFieldInUnion(
634 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
635 E->sawArrayRangeDesignator(Record[Idx++]);
636 return NumInits + 1;
637}
638
639unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
640 typedef DesignatedInitExpr::Designator Designator;
641
642 VisitExpr(E);
643 unsigned NumSubExprs = Record[Idx++];
644 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
645 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000646 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000647 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
648 E->setGNUSyntax(Record[Idx++]);
649
650 llvm::SmallVector<Designator, 4> Designators;
651 while (Idx < Record.size()) {
652 switch ((pch::DesignatorTypes)Record[Idx++]) {
653 case pch::DESIG_FIELD_DECL: {
654 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
655 SourceLocation DotLoc
656 = SourceLocation::getFromRawEncoding(Record[Idx++]);
657 SourceLocation FieldLoc
658 = SourceLocation::getFromRawEncoding(Record[Idx++]);
659 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
660 FieldLoc));
661 Designators.back().setField(Field);
662 break;
663 }
664
665 case pch::DESIG_FIELD_NAME: {
666 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
667 SourceLocation DotLoc
668 = SourceLocation::getFromRawEncoding(Record[Idx++]);
669 SourceLocation FieldLoc
670 = SourceLocation::getFromRawEncoding(Record[Idx++]);
671 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
672 break;
673 }
674
675 case pch::DESIG_ARRAY: {
676 unsigned Index = Record[Idx++];
677 SourceLocation LBracketLoc
678 = SourceLocation::getFromRawEncoding(Record[Idx++]);
679 SourceLocation RBracketLoc
680 = SourceLocation::getFromRawEncoding(Record[Idx++]);
681 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
682 break;
683 }
684
685 case pch::DESIG_ARRAY_RANGE: {
686 unsigned Index = Record[Idx++];
687 SourceLocation LBracketLoc
688 = SourceLocation::getFromRawEncoding(Record[Idx++]);
689 SourceLocation EllipsisLoc
690 = SourceLocation::getFromRawEncoding(Record[Idx++]);
691 SourceLocation RBracketLoc
692 = SourceLocation::getFromRawEncoding(Record[Idx++]);
693 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
694 RBracketLoc));
695 break;
696 }
697 }
698 }
699 E->setDesignators(&Designators[0], Designators.size());
700
701 return NumSubExprs;
702}
703
704unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
705 VisitExpr(E);
706 return 0;
707}
708
Douglas Gregorec0b8292009-04-15 23:02:49 +0000709unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
710 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000711 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000712 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
713 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
714 return 1;
715}
716
Douglas Gregor209d4622009-04-15 23:33:31 +0000717unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
718 VisitExpr(E);
719 E->setArgType1(Reader.GetType(Record[Idx++]));
720 E->setArgType2(Reader.GetType(Record[Idx++]));
721 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
722 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
723 return 0;
724}
725
726unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
727 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000728 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
729 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
730 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000731 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
732 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
733 return 3;
734}
735
736unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
737 VisitExpr(E);
738 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
739 return 0;
740}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000741
Douglas Gregor725e94b2009-04-16 00:01:45 +0000742unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
743 VisitExpr(E);
744 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000745 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000746 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
747 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
748 return NumExprs;
749}
750
751unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
752 VisitExpr(E);
753 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
754 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
755 E->setByRef(Record[Idx++]);
756 return 0;
757}
758
Douglas Gregorc34897d2009-04-09 22:27:44 +0000759// FIXME: use the diagnostics machinery
760static bool Error(const char *Str) {
761 std::fprintf(stderr, "%s\n", Str);
762 return true;
763}
764
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000765/// \brief Check the contents of the predefines buffer against the
766/// contents of the predefines buffer used to build the PCH file.
767///
768/// The contents of the two predefines buffers should be the same. If
769/// not, then some command-line option changed the preprocessor state
770/// and we must reject the PCH file.
771///
772/// \param PCHPredef The start of the predefines buffer in the PCH
773/// file.
774///
775/// \param PCHPredefLen The length of the predefines buffer in the PCH
776/// file.
777///
778/// \param PCHBufferID The FileID for the PCH predefines buffer.
779///
780/// \returns true if there was a mismatch (in which case the PCH file
781/// should be ignored), or false otherwise.
782bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
783 unsigned PCHPredefLen,
784 FileID PCHBufferID) {
785 const char *Predef = PP.getPredefines().c_str();
786 unsigned PredefLen = PP.getPredefines().size();
787
788 // If the two predefines buffers compare equal, we're done!.
789 if (PredefLen == PCHPredefLen &&
790 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
791 return false;
792
793 // The predefines buffers are different. Produce a reasonable
794 // diagnostic showing where they are different.
795
796 // The source locations (potentially in the two different predefines
797 // buffers)
798 SourceLocation Loc1, Loc2;
799 SourceManager &SourceMgr = PP.getSourceManager();
800
801 // Create a source buffer for our predefines string, so
802 // that we can build a diagnostic that points into that
803 // source buffer.
804 FileID BufferID;
805 if (Predef && Predef[0]) {
806 llvm::MemoryBuffer *Buffer
807 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
808 "<built-in>");
809 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
810 }
811
812 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
813 std::pair<const char *, const char *> Locations
814 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
815
816 if (Locations.first != Predef + MinLen) {
817 // We found the location in the two buffers where there is a
818 // difference. Form source locations to point there (in both
819 // buffers).
820 unsigned Offset = Locations.first - Predef;
821 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
822 .getFileLocWithOffset(Offset);
823 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
824 .getFileLocWithOffset(Offset);
825 } else if (PredefLen > PCHPredefLen) {
826 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
827 .getFileLocWithOffset(MinLen);
828 } else {
829 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
830 .getFileLocWithOffset(MinLen);
831 }
832
833 Diag(Loc1, diag::warn_pch_preprocessor);
834 if (Loc2.isValid())
835 Diag(Loc2, diag::note_predef_in_pch);
836 Diag(diag::note_ignoring_pch) << FileName;
837 return true;
838}
839
Douglas Gregor635f97f2009-04-13 16:31:14 +0000840/// \brief Read the line table in the source manager block.
841/// \returns true if ther was an error.
842static bool ParseLineTable(SourceManager &SourceMgr,
843 llvm::SmallVectorImpl<uint64_t> &Record) {
844 unsigned Idx = 0;
845 LineTableInfo &LineTable = SourceMgr.getLineTable();
846
847 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000848 std::map<int, int> FileIDs;
849 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000850 // Extract the file name
851 unsigned FilenameLen = Record[Idx++];
852 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
853 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000854 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
855 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000856 }
857
858 // Parse the line entries
859 std::vector<LineEntry> Entries;
860 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000861 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000862
863 // Extract the line entries
864 unsigned NumEntries = Record[Idx++];
865 Entries.clear();
866 Entries.reserve(NumEntries);
867 for (unsigned I = 0; I != NumEntries; ++I) {
868 unsigned FileOffset = Record[Idx++];
869 unsigned LineNo = Record[Idx++];
870 int FilenameID = Record[Idx++];
871 SrcMgr::CharacteristicKind FileKind
872 = (SrcMgr::CharacteristicKind)Record[Idx++];
873 unsigned IncludeOffset = Record[Idx++];
874 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
875 FileKind, IncludeOffset));
876 }
877 LineTable.AddEntry(FID, Entries);
878 }
879
880 return false;
881}
882
Douglas Gregorab1cef72009-04-10 03:52:48 +0000883/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000884PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000885 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000886 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
887 Error("Malformed source manager block record");
888 return Failure;
889 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000890
891 SourceManager &SourceMgr = Context.getSourceManager();
892 RecordData Record;
893 while (true) {
894 unsigned Code = Stream.ReadCode();
895 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000896 if (Stream.ReadBlockEnd()) {
897 Error("Error at end of Source Manager block");
898 return Failure;
899 }
900
901 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000902 }
903
904 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
905 // No known subblocks, always skip them.
906 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000907 if (Stream.SkipBlock()) {
908 Error("Malformed block record");
909 return Failure;
910 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000911 continue;
912 }
913
914 if (Code == llvm::bitc::DEFINE_ABBREV) {
915 Stream.ReadAbbrevRecord();
916 continue;
917 }
918
919 // Read a record.
920 const char *BlobStart;
921 unsigned BlobLen;
922 Record.clear();
923 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
924 default: // Default behavior: ignore.
925 break;
926
927 case pch::SM_SLOC_FILE_ENTRY: {
928 // FIXME: We would really like to delay the creation of this
929 // FileEntry until it is actually required, e.g., when producing
930 // a diagnostic with a source location in this file.
931 const FileEntry *File
932 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
933 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000934 FileID ID = SourceMgr.createFileID(File,
935 SourceLocation::getFromRawEncoding(Record[1]),
936 (CharacteristicKind)Record[2]);
937 if (Record[3])
938 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
939 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000940 break;
941 }
942
943 case pch::SM_SLOC_BUFFER_ENTRY: {
944 const char *Name = BlobStart;
945 unsigned Code = Stream.ReadCode();
946 Record.clear();
947 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
948 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000949 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000950 llvm::MemoryBuffer *Buffer
951 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
952 BlobStart + BlobLen - 1,
953 Name);
954 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
955
956 if (strcmp(Name, "<built-in>") == 0
957 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
958 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000959 break;
960 }
961
962 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
963 SourceLocation SpellingLoc
964 = SourceLocation::getFromRawEncoding(Record[1]);
965 SourceMgr.createInstantiationLoc(
966 SpellingLoc,
967 SourceLocation::getFromRawEncoding(Record[2]),
968 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000969 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000970 break;
971 }
972
Chris Lattnere1be6022009-04-14 23:22:57 +0000973 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000974 if (ParseLineTable(SourceMgr, Record))
975 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000976 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000977 }
978 }
979}
980
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000981bool PCHReader::ReadPreprocessorBlock() {
982 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
983 return Error("Malformed preprocessor block record");
984
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000985 RecordData Record;
986 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
987 MacroInfo *LastMacro = 0;
988
989 while (true) {
990 unsigned Code = Stream.ReadCode();
991 switch (Code) {
992 case llvm::bitc::END_BLOCK:
993 if (Stream.ReadBlockEnd())
994 return Error("Error at end of preprocessor block");
995 return false;
996
997 case llvm::bitc::ENTER_SUBBLOCK:
998 // No known subblocks, always skip them.
999 Stream.ReadSubBlockID();
1000 if (Stream.SkipBlock())
1001 return Error("Malformed block record");
1002 continue;
1003
1004 case llvm::bitc::DEFINE_ABBREV:
1005 Stream.ReadAbbrevRecord();
1006 continue;
1007 default: break;
1008 }
1009
1010 // Read a record.
1011 Record.clear();
1012 pch::PreprocessorRecordTypes RecType =
1013 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1014 switch (RecType) {
1015 default: // Default behavior: ignore unknown records.
1016 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001017 case pch::PP_COUNTER_VALUE:
1018 if (!Record.empty())
1019 PP.setCounterValue(Record[0]);
1020 break;
1021
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001022 case pch::PP_MACRO_OBJECT_LIKE:
1023 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001024 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1025 if (II == 0)
1026 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001027 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1028 bool isUsed = Record[2];
1029
1030 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1031 MI->setIsUsed(isUsed);
1032
1033 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1034 // Decode function-like macro info.
1035 bool isC99VarArgs = Record[3];
1036 bool isGNUVarArgs = Record[4];
1037 MacroArgs.clear();
1038 unsigned NumArgs = Record[5];
1039 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001040 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001041
1042 // Install function-like macro info.
1043 MI->setIsFunctionLike();
1044 if (isC99VarArgs) MI->setIsC99Varargs();
1045 if (isGNUVarArgs) MI->setIsGNUVarargs();
1046 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1047 PP.getPreprocessorAllocator());
1048 }
1049
1050 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001051 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001052
1053 // Remember that we saw this macro last so that we add the tokens that
1054 // form its body to it.
1055 LastMacro = MI;
1056 break;
1057 }
1058
1059 case pch::PP_TOKEN: {
1060 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1061 // pretend we didn't see this.
1062 if (LastMacro == 0) break;
1063
1064 Token Tok;
1065 Tok.startToken();
1066 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1067 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001068 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1069 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001070 Tok.setKind((tok::TokenKind)Record[3]);
1071 Tok.setFlag((Token::TokenFlags)Record[4]);
1072 LastMacro->AddTokenToBody(Tok);
1073 break;
1074 }
1075 }
1076 }
1077}
1078
Douglas Gregor179cfb12009-04-10 20:39:37 +00001079PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1080 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1081 Error("Malformed block record");
1082 return Failure;
1083 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001084
Chris Lattner29241862009-04-11 21:15:38 +00001085 uint64_t PreprocessorBlockBit = 0;
1086
Douglas Gregorc34897d2009-04-09 22:27:44 +00001087 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001088 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001089 while (!Stream.AtEndOfStream()) {
1090 unsigned Code = Stream.ReadCode();
1091 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001092 // If we saw the preprocessor block, read it now.
1093 if (PreprocessorBlockBit) {
1094 uint64_t SavedPos = Stream.GetCurrentBitNo();
1095 Stream.JumpToBit(PreprocessorBlockBit);
1096 if (ReadPreprocessorBlock()) {
1097 Error("Malformed preprocessor block");
1098 return Failure;
1099 }
1100 Stream.JumpToBit(SavedPos);
1101 }
1102
Douglas Gregor179cfb12009-04-10 20:39:37 +00001103 if (Stream.ReadBlockEnd()) {
1104 Error("Error at end of module block");
1105 return Failure;
1106 }
Chris Lattner29241862009-04-11 21:15:38 +00001107
Douglas Gregor179cfb12009-04-10 20:39:37 +00001108 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001109 }
1110
1111 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1112 switch (Stream.ReadSubBlockID()) {
1113 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1114 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1115 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001116 if (Stream.SkipBlock()) {
1117 Error("Malformed block record");
1118 return Failure;
1119 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001120 break;
1121
Chris Lattner29241862009-04-11 21:15:38 +00001122 case pch::PREPROCESSOR_BLOCK_ID:
1123 // Skip the preprocessor block for now, but remember where it is. We
1124 // want to read it in after the identifier table.
1125 if (PreprocessorBlockBit) {
1126 Error("Multiple preprocessor blocks found.");
1127 return Failure;
1128 }
1129 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1130 if (Stream.SkipBlock()) {
1131 Error("Malformed block record");
1132 return Failure;
1133 }
1134 break;
1135
Douglas Gregorab1cef72009-04-10 03:52:48 +00001136 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001137 switch (ReadSourceManagerBlock()) {
1138 case Success:
1139 break;
1140
1141 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001142 Error("Malformed source manager block");
1143 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001144
1145 case IgnorePCH:
1146 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001147 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001148 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001149 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001150 continue;
1151 }
1152
1153 if (Code == llvm::bitc::DEFINE_ABBREV) {
1154 Stream.ReadAbbrevRecord();
1155 continue;
1156 }
1157
1158 // Read and process a record.
1159 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001160 const char *BlobStart = 0;
1161 unsigned BlobLen = 0;
1162 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1163 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001164 default: // Default behavior: ignore.
1165 break;
1166
1167 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001168 if (!TypeOffsets.empty()) {
1169 Error("Duplicate TYPE_OFFSET record in PCH file");
1170 return Failure;
1171 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001172 TypeOffsets.swap(Record);
1173 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1174 break;
1175
1176 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001177 if (!DeclOffsets.empty()) {
1178 Error("Duplicate DECL_OFFSET record in PCH file");
1179 return Failure;
1180 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001181 DeclOffsets.swap(Record);
1182 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1183 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001184
1185 case pch::LANGUAGE_OPTIONS:
1186 if (ParseLanguageOptions(Record))
1187 return IgnorePCH;
1188 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001189
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001190 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001191 std::string TargetTriple(BlobStart, BlobLen);
1192 if (TargetTriple != Context.Target.getTargetTriple()) {
1193 Diag(diag::warn_pch_target_triple)
1194 << TargetTriple << Context.Target.getTargetTriple();
1195 Diag(diag::note_ignoring_pch) << FileName;
1196 return IgnorePCH;
1197 }
1198 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001199 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001200
1201 case pch::IDENTIFIER_TABLE:
1202 IdentifierTable = BlobStart;
1203 break;
1204
1205 case pch::IDENTIFIER_OFFSET:
1206 if (!IdentifierData.empty()) {
1207 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1208 return Failure;
1209 }
1210 IdentifierData.swap(Record);
1211#ifndef NDEBUG
1212 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1213 if ((IdentifierData[I] & 0x01) == 0) {
1214 Error("Malformed identifier table in the precompiled header");
1215 return Failure;
1216 }
1217 }
1218#endif
1219 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001220
1221 case pch::EXTERNAL_DEFINITIONS:
1222 if (!ExternalDefinitions.empty()) {
1223 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1224 return Failure;
1225 }
1226 ExternalDefinitions.swap(Record);
1227 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001228 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001229 }
1230
Douglas Gregor179cfb12009-04-10 20:39:37 +00001231 Error("Premature end of bitstream");
1232 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001233}
1234
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001235PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001236 // Set the PCH file name.
1237 this->FileName = FileName;
1238
Douglas Gregorc34897d2009-04-09 22:27:44 +00001239 // Open the PCH file.
1240 std::string ErrStr;
1241 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001242 if (!Buffer) {
1243 Error(ErrStr.c_str());
1244 return IgnorePCH;
1245 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001246
1247 // Initialize the stream
1248 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1249 (const unsigned char *)Buffer->getBufferEnd());
1250
1251 // Sniff for the signature.
1252 if (Stream.Read(8) != 'C' ||
1253 Stream.Read(8) != 'P' ||
1254 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001255 Stream.Read(8) != 'H') {
1256 Error("Not a PCH file");
1257 return IgnorePCH;
1258 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001259
1260 // We expect a number of well-defined blocks, though we don't necessarily
1261 // need to understand them all.
1262 while (!Stream.AtEndOfStream()) {
1263 unsigned Code = Stream.ReadCode();
1264
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001265 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1266 Error("Invalid record at top-level");
1267 return Failure;
1268 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001269
1270 unsigned BlockID = Stream.ReadSubBlockID();
1271
1272 // We only know the PCH subblock ID.
1273 switch (BlockID) {
1274 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001275 if (Stream.ReadBlockInfoBlock()) {
1276 Error("Malformed BlockInfoBlock");
1277 return Failure;
1278 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001279 break;
1280 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001281 switch (ReadPCHBlock()) {
1282 case Success:
1283 break;
1284
1285 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001286 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001287
1288 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001289 // FIXME: We could consider reading through to the end of this
1290 // PCH block, skipping subblocks, to see if there are other
1291 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001292 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001293 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001294 break;
1295 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001296 if (Stream.SkipBlock()) {
1297 Error("Malformed block record");
1298 return Failure;
1299 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001300 break;
1301 }
1302 }
1303
1304 // Load the translation unit declaration
1305 ReadDeclRecord(DeclOffsets[0], 0);
1306
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001307 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001308}
1309
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001310namespace {
1311 /// \brief Helper class that saves the current stream position and
1312 /// then restores it when destroyed.
1313 struct VISIBILITY_HIDDEN SavedStreamPosition {
1314 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001315 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001316
1317 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001318 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001319 }
1320
1321 private:
1322 llvm::BitstreamReader &Stream;
1323 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001324 };
1325}
1326
Douglas Gregor179cfb12009-04-10 20:39:37 +00001327/// \brief Parse the record that corresponds to a LangOptions data
1328/// structure.
1329///
1330/// This routine compares the language options used to generate the
1331/// PCH file against the language options set for the current
1332/// compilation. For each option, we classify differences between the
1333/// two compiler states as either "benign" or "important". Benign
1334/// differences don't matter, and we accept them without complaint
1335/// (and without modifying the language options). Differences between
1336/// the states for important options cause the PCH file to be
1337/// unusable, so we emit a warning and return true to indicate that
1338/// there was an error.
1339///
1340/// \returns true if the PCH file is unacceptable, false otherwise.
1341bool PCHReader::ParseLanguageOptions(
1342 const llvm::SmallVectorImpl<uint64_t> &Record) {
1343 const LangOptions &LangOpts = Context.getLangOptions();
1344#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1345#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1346 if (Record[Idx] != LangOpts.Option) { \
1347 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1348 Diag(diag::note_ignoring_pch) << FileName; \
1349 return true; \
1350 } \
1351 ++Idx
1352
1353 unsigned Idx = 0;
1354 PARSE_LANGOPT_BENIGN(Trigraphs);
1355 PARSE_LANGOPT_BENIGN(BCPLComment);
1356 PARSE_LANGOPT_BENIGN(DollarIdents);
1357 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1358 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1359 PARSE_LANGOPT_BENIGN(ImplicitInt);
1360 PARSE_LANGOPT_BENIGN(Digraphs);
1361 PARSE_LANGOPT_BENIGN(HexFloats);
1362 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1363 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1364 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1365 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1366 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1367 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1368 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1369 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1370 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1371 PARSE_LANGOPT_BENIGN(PascalStrings);
1372 PARSE_LANGOPT_BENIGN(Boolean);
1373 PARSE_LANGOPT_BENIGN(WritableStrings);
1374 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1375 diag::warn_pch_lax_vector_conversions);
1376 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1377 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1378 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1379 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1380 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1381 diag::warn_pch_thread_safe_statics);
1382 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1383 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1384 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1385 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1386 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1387 diag::warn_pch_heinous_extensions);
1388 // FIXME: Most of the options below are benign if the macro wasn't
1389 // used. Unfortunately, this means that a PCH compiled without
1390 // optimization can't be used with optimization turned on, even
1391 // though the only thing that changes is whether __OPTIMIZE__ was
1392 // defined... but if __OPTIMIZE__ never showed up in the header, it
1393 // doesn't matter. We could consider making this some special kind
1394 // of check.
1395 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1396 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1397 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1398 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1399 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1400 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1401 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1402 Diag(diag::warn_pch_gc_mode)
1403 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1404 Diag(diag::note_ignoring_pch) << FileName;
1405 return true;
1406 }
1407 ++Idx;
1408 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1409 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1410#undef PARSE_LANGOPT_IRRELEVANT
1411#undef PARSE_LANGOPT_BENIGN
1412
1413 return false;
1414}
1415
Douglas Gregorc34897d2009-04-09 22:27:44 +00001416/// \brief Read and return the type at the given offset.
1417///
1418/// This routine actually reads the record corresponding to the type
1419/// at the given offset in the bitstream. It is a helper routine for
1420/// GetType, which deals with reading type IDs.
1421QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001422 // Keep track of where we are in the stream, then jump back there
1423 // after reading this type.
1424 SavedStreamPosition SavedPosition(Stream);
1425
Douglas Gregorc34897d2009-04-09 22:27:44 +00001426 Stream.JumpToBit(Offset);
1427 RecordData Record;
1428 unsigned Code = Stream.ReadCode();
1429 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001430 case pch::TYPE_EXT_QUAL: {
1431 assert(Record.size() == 3 &&
1432 "Incorrect encoding of extended qualifier type");
1433 QualType Base = GetType(Record[0]);
1434 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1435 unsigned AddressSpace = Record[2];
1436
1437 QualType T = Base;
1438 if (GCAttr != QualType::GCNone)
1439 T = Context.getObjCGCQualType(T, GCAttr);
1440 if (AddressSpace)
1441 T = Context.getAddrSpaceQualType(T, AddressSpace);
1442 return T;
1443 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001444
Douglas Gregorc34897d2009-04-09 22:27:44 +00001445 case pch::TYPE_FIXED_WIDTH_INT: {
1446 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1447 return Context.getFixedWidthIntType(Record[0], Record[1]);
1448 }
1449
1450 case pch::TYPE_COMPLEX: {
1451 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1452 QualType ElemType = GetType(Record[0]);
1453 return Context.getComplexType(ElemType);
1454 }
1455
1456 case pch::TYPE_POINTER: {
1457 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1458 QualType PointeeType = GetType(Record[0]);
1459 return Context.getPointerType(PointeeType);
1460 }
1461
1462 case pch::TYPE_BLOCK_POINTER: {
1463 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1464 QualType PointeeType = GetType(Record[0]);
1465 return Context.getBlockPointerType(PointeeType);
1466 }
1467
1468 case pch::TYPE_LVALUE_REFERENCE: {
1469 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1470 QualType PointeeType = GetType(Record[0]);
1471 return Context.getLValueReferenceType(PointeeType);
1472 }
1473
1474 case pch::TYPE_RVALUE_REFERENCE: {
1475 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1476 QualType PointeeType = GetType(Record[0]);
1477 return Context.getRValueReferenceType(PointeeType);
1478 }
1479
1480 case pch::TYPE_MEMBER_POINTER: {
1481 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1482 QualType PointeeType = GetType(Record[0]);
1483 QualType ClassType = GetType(Record[1]);
1484 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1485 }
1486
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001487 case pch::TYPE_CONSTANT_ARRAY: {
1488 QualType ElementType = GetType(Record[0]);
1489 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1490 unsigned IndexTypeQuals = Record[2];
1491 unsigned Idx = 3;
1492 llvm::APInt Size = ReadAPInt(Record, Idx);
1493 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1494 }
1495
1496 case pch::TYPE_INCOMPLETE_ARRAY: {
1497 QualType ElementType = GetType(Record[0]);
1498 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1499 unsigned IndexTypeQuals = Record[2];
1500 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1501 }
1502
1503 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001504 QualType ElementType = GetType(Record[0]);
1505 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1506 unsigned IndexTypeQuals = Record[2];
1507 return Context.getVariableArrayType(ElementType, ReadExpr(),
1508 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001509 }
1510
1511 case pch::TYPE_VECTOR: {
1512 if (Record.size() != 2) {
1513 Error("Incorrect encoding of vector type in PCH file");
1514 return QualType();
1515 }
1516
1517 QualType ElementType = GetType(Record[0]);
1518 unsigned NumElements = Record[1];
1519 return Context.getVectorType(ElementType, NumElements);
1520 }
1521
1522 case pch::TYPE_EXT_VECTOR: {
1523 if (Record.size() != 2) {
1524 Error("Incorrect encoding of extended vector type in PCH file");
1525 return QualType();
1526 }
1527
1528 QualType ElementType = GetType(Record[0]);
1529 unsigned NumElements = Record[1];
1530 return Context.getExtVectorType(ElementType, NumElements);
1531 }
1532
1533 case pch::TYPE_FUNCTION_NO_PROTO: {
1534 if (Record.size() != 1) {
1535 Error("Incorrect encoding of no-proto function type");
1536 return QualType();
1537 }
1538 QualType ResultType = GetType(Record[0]);
1539 return Context.getFunctionNoProtoType(ResultType);
1540 }
1541
1542 case pch::TYPE_FUNCTION_PROTO: {
1543 QualType ResultType = GetType(Record[0]);
1544 unsigned Idx = 1;
1545 unsigned NumParams = Record[Idx++];
1546 llvm::SmallVector<QualType, 16> ParamTypes;
1547 for (unsigned I = 0; I != NumParams; ++I)
1548 ParamTypes.push_back(GetType(Record[Idx++]));
1549 bool isVariadic = Record[Idx++];
1550 unsigned Quals = Record[Idx++];
1551 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1552 isVariadic, Quals);
1553 }
1554
1555 case pch::TYPE_TYPEDEF:
1556 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1557 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1558
1559 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001560 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001561
1562 case pch::TYPE_TYPEOF: {
1563 if (Record.size() != 1) {
1564 Error("Incorrect encoding of typeof(type) in PCH file");
1565 return QualType();
1566 }
1567 QualType UnderlyingType = GetType(Record[0]);
1568 return Context.getTypeOfType(UnderlyingType);
1569 }
1570
1571 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001572 assert(Record.size() == 1 && "Incorrect encoding of record type");
1573 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001574
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001575 case pch::TYPE_ENUM:
1576 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1577 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1578
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001579 case pch::TYPE_OBJC_INTERFACE:
1580 // FIXME: Deserialize ObjCInterfaceType
1581 assert(false && "Cannot de-serialize ObjC interface types yet");
1582 return QualType();
1583
1584 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1585 // FIXME: Deserialize ObjCQualifiedInterfaceType
1586 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1587 return QualType();
1588
1589 case pch::TYPE_OBJC_QUALIFIED_ID:
1590 // FIXME: Deserialize ObjCQualifiedIdType
1591 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1592 return QualType();
1593
1594 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1595 // FIXME: Deserialize ObjCQualifiedClassType
1596 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1597 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001598 }
1599
1600 // Suppress a GCC warning
1601 return QualType();
1602}
1603
1604/// \brief Note that we have loaded the declaration with the given
1605/// Index.
1606///
1607/// This routine notes that this declaration has already been loaded,
1608/// so that future GetDecl calls will return this declaration rather
1609/// than trying to load a new declaration.
1610inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1611 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1612 DeclAlreadyLoaded[Index] = true;
1613 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1614}
1615
1616/// \brief Read the declaration at the given offset from the PCH file.
1617Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001618 // Keep track of where we are in the stream, then jump back there
1619 // after reading this declaration.
1620 SavedStreamPosition SavedPosition(Stream);
1621
Douglas Gregorc34897d2009-04-09 22:27:44 +00001622 Decl *D = 0;
1623 Stream.JumpToBit(Offset);
1624 RecordData Record;
1625 unsigned Code = Stream.ReadCode();
1626 unsigned Idx = 0;
1627 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001628
Douglas Gregorc34897d2009-04-09 22:27:44 +00001629 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001630 case pch::DECL_ATTR:
1631 case pch::DECL_CONTEXT_LEXICAL:
1632 case pch::DECL_CONTEXT_VISIBLE:
1633 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1634 break;
1635
Douglas Gregorc34897d2009-04-09 22:27:44 +00001636 case pch::DECL_TRANSLATION_UNIT:
1637 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001638 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001639 break;
1640
1641 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001642 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001643 break;
1644 }
1645
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001646 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001647 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001648 break;
1649 }
1650
Douglas Gregor982365e2009-04-13 21:20:57 +00001651 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001652 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1653 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001654 break;
1655 }
1656
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001657 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001658 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1659 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001660 break;
1661 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001662
1663 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001664 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1665 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001666 break;
1667 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001668
Douglas Gregor982365e2009-04-13 21:20:57 +00001669 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001670 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1671 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001672 break;
1673 }
1674
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001676 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1677 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001678 break;
1679 }
1680
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001681 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001682 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1683 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001684 break;
1685 }
1686
1687 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001688 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001689 QualType(), QualType(), VarDecl::None,
1690 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001691 break;
1692 }
1693
Douglas Gregor2a491792009-04-13 22:49:25 +00001694 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001695 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001696 break;
1697 }
1698
1699 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001700 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001701 break;
1702 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001703 }
1704
Douglas Gregorddf4d092009-04-16 22:29:51 +00001705 assert(D && "Unknown declaration creating PCH file");
1706 if (D) {
1707 LoadedDecl(Index, D);
1708 Reader.Visit(D);
1709 }
1710
Douglas Gregorc34897d2009-04-09 22:27:44 +00001711 // If this declaration is also a declaration context, get the
1712 // offsets for its tables of lexical and visible declarations.
1713 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1714 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1715 if (Offsets.first || Offsets.second) {
1716 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1717 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1718 DeclContextOffsets[DC] = Offsets;
1719 }
1720 }
1721 assert(Idx == Record.size());
1722
1723 return D;
1724}
1725
Douglas Gregorac8f2802009-04-10 17:25:41 +00001726QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001727 unsigned Quals = ID & 0x07;
1728 unsigned Index = ID >> 3;
1729
1730 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1731 QualType T;
1732 switch ((pch::PredefinedTypeIDs)Index) {
1733 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1734 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1735 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1736
1737 case pch::PREDEF_TYPE_CHAR_U_ID:
1738 case pch::PREDEF_TYPE_CHAR_S_ID:
1739 // FIXME: Check that the signedness of CharTy is correct!
1740 T = Context.CharTy;
1741 break;
1742
1743 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1744 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1745 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1746 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1747 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1748 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1749 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1750 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1751 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1752 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1753 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1754 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1755 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1756 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1757 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1758 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1759 }
1760
1761 assert(!T.isNull() && "Unknown predefined type");
1762 return T.getQualifiedType(Quals);
1763 }
1764
1765 Index -= pch::NUM_PREDEF_TYPE_IDS;
1766 if (!TypeAlreadyLoaded[Index]) {
1767 // Load the type from the PCH file.
1768 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1769 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1770 TypeAlreadyLoaded[Index] = true;
1771 }
1772
1773 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1774}
1775
Douglas Gregorac8f2802009-04-10 17:25:41 +00001776Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001777 if (ID == 0)
1778 return 0;
1779
1780 unsigned Index = ID - 1;
1781 if (DeclAlreadyLoaded[Index])
1782 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1783
1784 // Load the declaration from the PCH file.
1785 return ReadDeclRecord(DeclOffsets[Index], Index);
1786}
1787
1788bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001789 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001790 assert(DC->hasExternalLexicalStorage() &&
1791 "DeclContext has no lexical decls in storage");
1792 uint64_t Offset = DeclContextOffsets[DC].first;
1793 assert(Offset && "DeclContext has no lexical decls in storage");
1794
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001795 // Keep track of where we are in the stream, then jump back there
1796 // after reading this context.
1797 SavedStreamPosition SavedPosition(Stream);
1798
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799 // Load the record containing all of the declarations lexically in
1800 // this context.
1801 Stream.JumpToBit(Offset);
1802 RecordData Record;
1803 unsigned Code = Stream.ReadCode();
1804 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001805 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001806 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1807
1808 // Load all of the declaration IDs
1809 Decls.clear();
1810 Decls.insert(Decls.end(), Record.begin(), Record.end());
1811 return false;
1812}
1813
1814bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1815 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1816 assert(DC->hasExternalVisibleStorage() &&
1817 "DeclContext has no visible decls in storage");
1818 uint64_t Offset = DeclContextOffsets[DC].second;
1819 assert(Offset && "DeclContext has no visible decls in storage");
1820
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001821 // Keep track of where we are in the stream, then jump back there
1822 // after reading this context.
1823 SavedStreamPosition SavedPosition(Stream);
1824
Douglas Gregorc34897d2009-04-09 22:27:44 +00001825 // Load the record containing all of the declarations visible in
1826 // this context.
1827 Stream.JumpToBit(Offset);
1828 RecordData Record;
1829 unsigned Code = Stream.ReadCode();
1830 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001831 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001832 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1833 if (Record.size() == 0)
1834 return false;
1835
1836 Decls.clear();
1837
1838 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001839 while (Idx < Record.size()) {
1840 Decls.push_back(VisibleDeclaration());
1841 Decls.back().Name = ReadDeclarationName(Record, Idx);
1842
Douglas Gregorc34897d2009-04-09 22:27:44 +00001843 unsigned Size = Record[Idx++];
1844 llvm::SmallVector<unsigned, 4> & LoadedDecls
1845 = Decls.back().Declarations;
1846 LoadedDecls.reserve(Size);
1847 for (unsigned I = 0; I < Size; ++I)
1848 LoadedDecls.push_back(Record[Idx++]);
1849 }
1850
1851 return false;
1852}
1853
Douglas Gregor631f6c62009-04-14 00:24:19 +00001854void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1855 if (!Consumer)
1856 return;
1857
1858 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1859 Decl *D = GetDecl(ExternalDefinitions[I]);
1860 DeclGroupRef DG(D);
1861 Consumer->HandleTopLevelDecl(DG);
1862 }
1863}
1864
Douglas Gregorc34897d2009-04-09 22:27:44 +00001865void PCHReader::PrintStats() {
1866 std::fprintf(stderr, "*** PCH Statistics:\n");
1867
1868 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1869 TypeAlreadyLoaded.end(),
1870 true);
1871 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1872 DeclAlreadyLoaded.end(),
1873 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001874 unsigned NumIdentifiersLoaded = 0;
1875 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1876 if ((IdentifierData[I] & 0x01) == 0)
1877 ++NumIdentifiersLoaded;
1878 }
1879
Douglas Gregorc34897d2009-04-09 22:27:44 +00001880 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1881 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001882 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001883 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1884 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001885 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1886 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1887 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1888 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001889 std::fprintf(stderr, "\n");
1890}
1891
Chris Lattner29241862009-04-11 21:15:38 +00001892IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001893 if (ID == 0)
1894 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001895
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001896 if (!IdentifierTable || IdentifierData.empty()) {
1897 Error("No identifier table in PCH file");
1898 return 0;
1899 }
Chris Lattner29241862009-04-11 21:15:38 +00001900
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001901 if (IdentifierData[ID - 1] & 0x01) {
1902 uint64_t Offset = IdentifierData[ID - 1];
1903 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001904 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001905 }
Chris Lattner29241862009-04-11 21:15:38 +00001906
1907 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001908}
1909
1910DeclarationName
1911PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1912 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1913 switch (Kind) {
1914 case DeclarationName::Identifier:
1915 return DeclarationName(GetIdentifierInfo(Record, Idx));
1916
1917 case DeclarationName::ObjCZeroArgSelector:
1918 case DeclarationName::ObjCOneArgSelector:
1919 case DeclarationName::ObjCMultiArgSelector:
1920 assert(false && "Unable to de-serialize Objective-C selectors");
1921 break;
1922
1923 case DeclarationName::CXXConstructorName:
1924 return Context.DeclarationNames.getCXXConstructorName(
1925 GetType(Record[Idx++]));
1926
1927 case DeclarationName::CXXDestructorName:
1928 return Context.DeclarationNames.getCXXDestructorName(
1929 GetType(Record[Idx++]));
1930
1931 case DeclarationName::CXXConversionFunctionName:
1932 return Context.DeclarationNames.getCXXConversionFunctionName(
1933 GetType(Record[Idx++]));
1934
1935 case DeclarationName::CXXOperatorName:
1936 return Context.DeclarationNames.getCXXOperatorName(
1937 (OverloadedOperatorKind)Record[Idx++]);
1938
1939 case DeclarationName::CXXUsingDirective:
1940 return DeclarationName::getUsingDirectiveName();
1941 }
1942
1943 // Required to silence GCC warning
1944 return DeclarationName();
1945}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001946
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001947/// \brief Read an integral value
1948llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1949 unsigned BitWidth = Record[Idx++];
1950 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1951 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1952 Idx += NumWords;
1953 return Result;
1954}
1955
1956/// \brief Read a signed integral value
1957llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1958 bool isUnsigned = Record[Idx++];
1959 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1960}
1961
Douglas Gregore2f37202009-04-14 21:55:33 +00001962/// \brief Read a floating-point value
1963llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001964 return llvm::APFloat(ReadAPInt(Record, Idx));
1965}
1966
Douglas Gregor1c507882009-04-15 21:30:51 +00001967// \brief Read a string
1968std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1969 unsigned Len = Record[Idx++];
1970 std::string Result(&Record[Idx], &Record[Idx] + Len);
1971 Idx += Len;
1972 return Result;
1973}
1974
1975/// \brief Reads attributes from the current stream position.
1976Attr *PCHReader::ReadAttributes() {
1977 unsigned Code = Stream.ReadCode();
1978 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1979 "Expected unabbreviated record"); (void)Code;
1980
1981 RecordData Record;
1982 unsigned Idx = 0;
1983 unsigned RecCode = Stream.ReadRecord(Code, Record);
1984 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1985 (void)RecCode;
1986
1987#define SIMPLE_ATTR(Name) \
1988 case Attr::Name: \
1989 New = ::new (Context) Name##Attr(); \
1990 break
1991
1992#define STRING_ATTR(Name) \
1993 case Attr::Name: \
1994 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1995 break
1996
1997#define UNSIGNED_ATTR(Name) \
1998 case Attr::Name: \
1999 New = ::new (Context) Name##Attr(Record[Idx++]); \
2000 break
2001
2002 Attr *Attrs = 0;
2003 while (Idx < Record.size()) {
2004 Attr *New = 0;
2005 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2006 bool IsInherited = Record[Idx++];
2007
2008 switch (Kind) {
2009 STRING_ATTR(Alias);
2010 UNSIGNED_ATTR(Aligned);
2011 SIMPLE_ATTR(AlwaysInline);
2012 SIMPLE_ATTR(AnalyzerNoReturn);
2013 STRING_ATTR(Annotate);
2014 STRING_ATTR(AsmLabel);
2015
2016 case Attr::Blocks:
2017 New = ::new (Context) BlocksAttr(
2018 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2019 break;
2020
2021 case Attr::Cleanup:
2022 New = ::new (Context) CleanupAttr(
2023 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2024 break;
2025
2026 SIMPLE_ATTR(Const);
2027 UNSIGNED_ATTR(Constructor);
2028 SIMPLE_ATTR(DLLExport);
2029 SIMPLE_ATTR(DLLImport);
2030 SIMPLE_ATTR(Deprecated);
2031 UNSIGNED_ATTR(Destructor);
2032 SIMPLE_ATTR(FastCall);
2033
2034 case Attr::Format: {
2035 std::string Type = ReadString(Record, Idx);
2036 unsigned FormatIdx = Record[Idx++];
2037 unsigned FirstArg = Record[Idx++];
2038 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2039 break;
2040 }
2041
2042 SIMPLE_ATTR(GNUCInline);
2043
2044 case Attr::IBOutletKind:
2045 New = ::new (Context) IBOutletAttr();
2046 break;
2047
2048 SIMPLE_ATTR(NoReturn);
2049 SIMPLE_ATTR(NoThrow);
2050 SIMPLE_ATTR(Nodebug);
2051 SIMPLE_ATTR(Noinline);
2052
2053 case Attr::NonNull: {
2054 unsigned Size = Record[Idx++];
2055 llvm::SmallVector<unsigned, 16> ArgNums;
2056 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2057 Idx += Size;
2058 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2059 break;
2060 }
2061
2062 SIMPLE_ATTR(ObjCException);
2063 SIMPLE_ATTR(ObjCNSObject);
2064 SIMPLE_ATTR(Overloadable);
2065 UNSIGNED_ATTR(Packed);
2066 SIMPLE_ATTR(Pure);
2067 UNSIGNED_ATTR(Regparm);
2068 STRING_ATTR(Section);
2069 SIMPLE_ATTR(StdCall);
2070 SIMPLE_ATTR(TransparentUnion);
2071 SIMPLE_ATTR(Unavailable);
2072 SIMPLE_ATTR(Unused);
2073 SIMPLE_ATTR(Used);
2074
2075 case Attr::Visibility:
2076 New = ::new (Context) VisibilityAttr(
2077 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2078 break;
2079
2080 SIMPLE_ATTR(WarnUnusedResult);
2081 SIMPLE_ATTR(Weak);
2082 SIMPLE_ATTR(WeakImport);
2083 }
2084
2085 assert(New && "Unable to decode attribute?");
2086 New->setInherited(IsInherited);
2087 New->setNext(Attrs);
2088 Attrs = New;
2089 }
2090#undef UNSIGNED_ATTR
2091#undef STRING_ATTR
2092#undef SIMPLE_ATTR
2093
2094 // The list of attributes was built backwards. Reverse the list
2095 // before returning it.
2096 Attr *PrevAttr = 0, *NextAttr = 0;
2097 while (Attrs) {
2098 NextAttr = Attrs->getNext();
2099 Attrs->setNext(PrevAttr);
2100 PrevAttr = Attrs;
2101 Attrs = NextAttr;
2102 }
2103
2104 return PrevAttr;
2105}
2106
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002107Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002108 // Within the bitstream, expressions are stored in Reverse Polish
2109 // Notation, with each of the subexpressions preceding the
2110 // expression they are stored in. To evaluate expressions, we
2111 // continue reading expressions and placing them on the stack, with
2112 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002113 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002114 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002115 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002116 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002117 llvm::SmallVector<Stmt *, 16> StmtStack;
2118 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002119 Stmt::EmptyShell Empty;
2120
Douglas Gregora151ba42009-04-14 23:32:43 +00002121 while (true) {
2122 unsigned Code = Stream.ReadCode();
2123 if (Code == llvm::bitc::END_BLOCK) {
2124 if (Stream.ReadBlockEnd()) {
2125 Error("Error at end of Source Manager block");
2126 return 0;
2127 }
2128 break;
2129 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002130
Douglas Gregora151ba42009-04-14 23:32:43 +00002131 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2132 // No known subblocks, always skip them.
2133 Stream.ReadSubBlockID();
2134 if (Stream.SkipBlock()) {
2135 Error("Malformed block record");
2136 return 0;
2137 }
2138 continue;
2139 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002140
Douglas Gregora151ba42009-04-14 23:32:43 +00002141 if (Code == llvm::bitc::DEFINE_ABBREV) {
2142 Stream.ReadAbbrevRecord();
2143 continue;
2144 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002145
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002146 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002147 Idx = 0;
2148 Record.clear();
2149 bool Finished = false;
2150 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002151 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002152 Finished = true;
2153 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002154
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002155 case pch::STMT_NULL_PTR:
2156 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002157 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002158
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002159 case pch::STMT_NULL:
2160 S = new (Context) NullStmt(Empty);
2161 break;
2162
2163 case pch::STMT_COMPOUND:
2164 S = new (Context) CompoundStmt(Empty);
2165 break;
2166
2167 case pch::STMT_CASE:
2168 S = new (Context) CaseStmt(Empty);
2169 break;
2170
2171 case pch::STMT_DEFAULT:
2172 S = new (Context) DefaultStmt(Empty);
2173 break;
2174
2175 case pch::STMT_IF:
2176 S = new (Context) IfStmt(Empty);
2177 break;
2178
2179 case pch::STMT_SWITCH:
2180 S = new (Context) SwitchStmt(Empty);
2181 break;
2182
Douglas Gregora6b503f2009-04-17 00:16:09 +00002183 case pch::STMT_WHILE:
2184 S = new (Context) WhileStmt(Empty);
2185 break;
2186
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002187 case pch::STMT_DO:
2188 S = new (Context) DoStmt(Empty);
2189 break;
2190
2191 case pch::STMT_FOR:
2192 S = new (Context) ForStmt(Empty);
2193 break;
2194
Douglas Gregora6b503f2009-04-17 00:16:09 +00002195 case pch::STMT_CONTINUE:
2196 S = new (Context) ContinueStmt(Empty);
2197 break;
2198
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002199 case pch::STMT_BREAK:
2200 S = new (Context) BreakStmt(Empty);
2201 break;
2202
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002203 case pch::STMT_RETURN:
2204 S = new (Context) ReturnStmt(Empty);
2205 break;
2206
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002207 case pch::STMT_DECL:
2208 S = new (Context) DeclStmt(Empty);
2209 break;
2210
Douglas Gregora151ba42009-04-14 23:32:43 +00002211 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002212 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002213 break;
2214
2215 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002216 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002217 break;
2218
2219 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002220 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002221 break;
2222
2223 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002224 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002225 break;
2226
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002227 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002228 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002229 break;
2230
Douglas Gregor596e0932009-04-15 16:35:07 +00002231 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002232 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002233 Record[PCHStmtReader::NumExprFields + 1]);
2234 break;
2235
Douglas Gregora151ba42009-04-14 23:32:43 +00002236 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002237 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002238 break;
2239
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002240 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002241 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002242 break;
2243
Douglas Gregor12d74052009-04-15 15:58:59 +00002244 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002245 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002246 break;
2247
2248 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002249 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002250 break;
2251
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002252 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002253 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002254 break;
2255
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002256 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002257 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002258 break;
2259
2260 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002261 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002262 break;
2263
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002264 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002265 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002266 break;
2267
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002268 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002269 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002270 break;
2271
2272 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002273 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002274 break;
2275
Douglas Gregora151ba42009-04-14 23:32:43 +00002276 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002277 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002278 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002279
2280 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002281 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002282 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002283
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002284 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002285 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002286 break;
2287
Douglas Gregorec0b8292009-04-15 23:02:49 +00002288 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002289 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002290 break;
2291
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002292 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002293 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002294 break;
2295
2296 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002297 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002298 Record[PCHStmtReader::NumExprFields] - 1);
2299
2300 break;
2301
2302 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002303 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002304 break;
2305
Douglas Gregorec0b8292009-04-15 23:02:49 +00002306 case pch::EXPR_VA_ARG:
2307 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002308 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002309 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002310
2311 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002312 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002313 break;
2314
2315 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002316 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002317 break;
2318
2319 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002320 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002321 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002322
2323 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002324 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002325 break;
2326
2327 case pch::EXPR_BLOCK_DECL_REF:
2328 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002329 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002330 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002331 }
2332
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002333 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002334 if (Finished)
2335 break;
2336
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002337 if (S) {
2338 unsigned NumSubStmts = Reader.Visit(S);
2339 while (NumSubStmts > 0) {
2340 StmtStack.pop_back();
2341 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002342 }
2343 }
2344
2345 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002346 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002347 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002348 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002349 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002350 return StmtStack.back();
2351}
2352
2353Expr *PCHReader::ReadExpr() {
2354 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002355}
2356
Douglas Gregor179cfb12009-04-10 20:39:37 +00002357DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002358 return Diag(SourceLocation(), DiagID);
2359}
2360
2361DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2362 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002363 Context.getSourceManager()),
2364 DiagID);
2365}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002366
2367/// \brief Record that the given ID maps to the given switch-case
2368/// statement.
2369void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2370 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2371 SwitchCaseStmts[ID] = SC;
2372}
2373
2374/// \brief Retrieve the switch-case statement with the given ID.
2375SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2376 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2377 return SwitchCaseStmts[ID];
2378}