blob: 9597b37082ce0b2e8f2e4f0069bb2a2e69759891 [file] [log] [blame]
Douglas Gregor2cf26342009-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 Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-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 Gregorcb70bb22009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-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 Gregor0a2b45e2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-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 Gregor68a2eb02009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-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 Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Douglas Gregor0a2b45e2009-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 Gregor8c700062009-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 Gregor2cf26342009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor3a2f7e42009-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 Gregor8c700062009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000167}
168
Douglas Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-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 Gregor1028bc62009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-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 Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000217//===----------------------------------------------------------------------===//
218// Statement/expression deserialization
219//===----------------------------------------------------------------------===//
220namespace {
221 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000222 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000223 PCHReader &Reader;
224 const PCHReader::RecordData &Record;
225 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000226 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000227
228 public:
229 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000230 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
231 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000232
Douglas Gregor025452f2009-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 Gregor673ecd62009-04-15 16:35:07 +0000237 /// \brief The number of record fields required for the Expr class
238 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000239 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000240
Douglas Gregor087fd532009-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 Gregor025452f2009-04-17 00:04:06 +0000246 unsigned VisitStmt(Stmt *S);
247 unsigned VisitNullStmt(NullStmt *S);
248 unsigned VisitCompoundStmt(CompoundStmt *S);
249 unsigned VisitSwitchCase(SwitchCase *S);
250 unsigned VisitCaseStmt(CaseStmt *S);
251 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000252 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000253 unsigned VisitIfStmt(IfStmt *S);
254 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000255 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000256 unsigned VisitDoStmt(DoStmt *S);
257 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000258 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000259 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000260 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000261 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000262 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000263 unsigned VisitExpr(Expr *E);
264 unsigned VisitPredefinedExpr(PredefinedExpr *E);
265 unsigned VisitDeclRefExpr(DeclRefExpr *E);
266 unsigned VisitIntegerLiteral(IntegerLiteral *E);
267 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000268 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000269 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000270 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000271 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000272 unsigned VisitUnaryOperator(UnaryOperator *E);
273 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000274 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000275 unsigned VisitCallExpr(CallExpr *E);
276 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000277 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000278 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000279 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
280 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000281 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000282 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
283 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000284 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000285 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000286 unsigned VisitInitListExpr(InitListExpr *E);
287 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
288 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000289 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000290 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
291 unsigned VisitChooseExpr(ChooseExpr *E);
292 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000293 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
294 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000295 };
296}
297
Douglas Gregor025452f2009-04-17 00:04:06 +0000298unsigned PCHStmtReader::VisitStmt(Stmt *S) {
299 assert(Idx == NumStmtFields && "Incorrect statement field count");
300 return 0;
301}
302
303unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
304 VisitStmt(S);
305 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
306 return 0;
307}
308
309unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
310 VisitStmt(S);
311 unsigned NumStmts = Record[Idx++];
312 S->setStmts(Reader.getContext(),
313 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
314 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
315 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
316 return NumStmts;
317}
318
319unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
320 VisitStmt(S);
321 Reader.RecordSwitchCaseID(S, Record[Idx++]);
322 return 0;
323}
324
325unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
326 VisitSwitchCase(S);
327 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
328 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
329 S->setSubStmt(StmtStack.back());
330 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
331 return 3;
332}
333
334unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
335 VisitSwitchCase(S);
336 S->setSubStmt(StmtStack.back());
337 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
338 return 1;
339}
340
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000341unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
342 VisitStmt(S);
343 S->setID(Reader.GetIdentifierInfo(Record, Idx));
344 S->setSubStmt(StmtStack.back());
345 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
346 Reader.RecordLabelStmt(S, Record[Idx++]);
347 return 1;
348}
349
Douglas Gregor025452f2009-04-17 00:04:06 +0000350unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
351 VisitStmt(S);
352 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
353 S->setThen(StmtStack[StmtStack.size() - 2]);
354 S->setElse(StmtStack[StmtStack.size() - 1]);
355 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
356 return 3;
357}
358
359unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
360 VisitStmt(S);
361 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
362 S->setBody(StmtStack.back());
363 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
364 SwitchCase *PrevSC = 0;
365 for (unsigned N = Record.size(); Idx != N; ++Idx) {
366 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
367 if (PrevSC)
368 PrevSC->setNextSwitchCase(SC);
369 else
370 S->setSwitchCaseList(SC);
371 PrevSC = SC;
372 }
373 return 2;
374}
375
Douglas Gregord921cf92009-04-17 00:16:09 +0000376unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
377 VisitStmt(S);
378 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
379 S->setBody(StmtStack.back());
380 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
381 return 2;
382}
383
Douglas Gregor67d82492009-04-17 00:29:51 +0000384unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
385 VisitStmt(S);
386 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
387 S->setBody(StmtStack.back());
388 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
389 return 2;
390}
391
392unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
393 VisitStmt(S);
394 S->setInit(StmtStack[StmtStack.size() - 4]);
395 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
396 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
397 S->setBody(StmtStack.back());
398 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
399 return 4;
400}
401
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000402unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
403 VisitStmt(S);
404 Reader.SetLabelOf(S, Record[Idx++]);
405 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
406 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
407 return 0;
408}
409
Douglas Gregord921cf92009-04-17 00:16:09 +0000410unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
411 VisitStmt(S);
412 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 return 0;
414}
415
Douglas Gregor025452f2009-04-17 00:04:06 +0000416unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
417 VisitStmt(S);
418 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
419 return 0;
420}
421
Douglas Gregor0de9d882009-04-17 16:34:57 +0000422unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
423 VisitStmt(S);
424 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
425 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
426 return 1;
427}
428
Douglas Gregor84f21702009-04-17 16:55:36 +0000429unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
430 VisitStmt(S);
431 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
432 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
433
434 if (Idx + 1 == Record.size()) {
435 // Single declaration
436 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
437 } else {
438 llvm::SmallVector<Decl *, 16> Decls;
439 Decls.reserve(Record.size() - Idx);
440 for (unsigned N = Record.size(); Idx != N; ++Idx)
441 Decls.push_back(Reader.GetDecl(Record[Idx]));
442 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
443 &Decls[0], Decls.size())));
444 }
445 return 0;
446}
447
Douglas Gregor087fd532009-04-14 23:32:43 +0000448unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000449 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000450 E->setType(Reader.GetType(Record[Idx++]));
451 E->setTypeDependent(Record[Idx++]);
452 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000453 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000454 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000455}
456
Douglas Gregor087fd532009-04-14 23:32:43 +0000457unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000458 VisitExpr(E);
459 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
460 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000461 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000462}
463
Douglas Gregor087fd532009-04-14 23:32:43 +0000464unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000465 VisitExpr(E);
466 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
467 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000468 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000469}
470
Douglas Gregor087fd532009-04-14 23:32:43 +0000471unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000472 VisitExpr(E);
473 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
474 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000475 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000476}
477
Douglas Gregor087fd532009-04-14 23:32:43 +0000478unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000479 VisitExpr(E);
480 E->setValue(Reader.ReadAPFloat(Record, Idx));
481 E->setExact(Record[Idx++]);
482 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000483 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000484}
485
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000486unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
487 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000488 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000489 return 1;
490}
491
Douglas Gregor673ecd62009-04-15 16:35:07 +0000492unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
493 VisitExpr(E);
494 unsigned Len = Record[Idx++];
495 assert(Record[Idx] == E->getNumConcatenated() &&
496 "Wrong number of concatenated tokens!");
497 ++Idx;
498 E->setWide(Record[Idx++]);
499
500 // Read string data
501 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
502 E->setStrData(Reader.getContext(), &Str[0], Len);
503 Idx += Len;
504
505 // Read source locations
506 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
507 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
508
509 return 0;
510}
511
Douglas Gregor087fd532009-04-14 23:32:43 +0000512unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000513 VisitExpr(E);
514 E->setValue(Record[Idx++]);
515 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
516 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000517 return 0;
518}
519
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000520unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
521 VisitExpr(E);
522 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
523 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000524 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000525 return 1;
526}
527
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000528unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
529 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000530 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000531 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
532 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
533 return 1;
534}
535
536unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
537 VisitExpr(E);
538 E->setSizeof(Record[Idx++]);
539 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000540 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000541 ++Idx;
542 } else {
543 E->setArgument(Reader.GetType(Record[Idx++]));
544 }
545 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
546 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
547 return E->isArgumentType()? 0 : 1;
548}
549
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000550unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
551 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000552 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
553 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000554 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 return 2;
556}
557
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000558unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
559 VisitExpr(E);
560 E->setNumArgs(Reader.getContext(), Record[Idx++]);
561 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000562 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000563 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000564 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000565 return E->getNumArgs() + 1;
566}
567
568unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
569 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000570 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000571 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
572 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
573 E->setArrow(Record[Idx++]);
574 return 1;
575}
576
Douglas Gregor087fd532009-04-14 23:32:43 +0000577unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
578 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000579 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000580 return 1;
581}
582
Douglas Gregordb600c32009-04-15 00:25:59 +0000583unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
584 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000585 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
586 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000587 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
588 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
589 return 2;
590}
591
Douglas Gregorad90e962009-04-15 22:40:36 +0000592unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
593 VisitBinaryOperator(E);
594 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
595 E->setComputationResultType(Reader.GetType(Record[Idx++]));
596 return 2;
597}
598
599unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
600 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000601 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
602 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
603 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000604 return 3;
605}
606
Douglas Gregor087fd532009-04-14 23:32:43 +0000607unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
608 VisitCastExpr(E);
609 E->setLvalueCast(Record[Idx++]);
610 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000611}
612
Douglas Gregordb600c32009-04-15 00:25:59 +0000613unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
614 VisitCastExpr(E);
615 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
616 return 1;
617}
618
619unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
620 VisitExplicitCastExpr(E);
621 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
622 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
623 return 1;
624}
625
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000626unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
627 VisitExpr(E);
628 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000629 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000630 E->setFileScope(Record[Idx++]);
631 return 1;
632}
633
Douglas Gregord3c98a02009-04-15 23:02:49 +0000634unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
635 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000636 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000637 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
638 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
639 return 1;
640}
641
Douglas Gregord077d752009-04-16 00:55:48 +0000642unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
643 VisitExpr(E);
644 unsigned NumInits = Record[Idx++];
645 E->reserveInits(NumInits);
646 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000647 E->updateInit(I,
648 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
649 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000650 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
651 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
652 E->setInitializedFieldInUnion(
653 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
654 E->sawArrayRangeDesignator(Record[Idx++]);
655 return NumInits + 1;
656}
657
658unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
659 typedef DesignatedInitExpr::Designator Designator;
660
661 VisitExpr(E);
662 unsigned NumSubExprs = Record[Idx++];
663 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
664 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000665 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000666 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
667 E->setGNUSyntax(Record[Idx++]);
668
669 llvm::SmallVector<Designator, 4> Designators;
670 while (Idx < Record.size()) {
671 switch ((pch::DesignatorTypes)Record[Idx++]) {
672 case pch::DESIG_FIELD_DECL: {
673 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
674 SourceLocation DotLoc
675 = SourceLocation::getFromRawEncoding(Record[Idx++]);
676 SourceLocation FieldLoc
677 = SourceLocation::getFromRawEncoding(Record[Idx++]);
678 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
679 FieldLoc));
680 Designators.back().setField(Field);
681 break;
682 }
683
684 case pch::DESIG_FIELD_NAME: {
685 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
686 SourceLocation DotLoc
687 = SourceLocation::getFromRawEncoding(Record[Idx++]);
688 SourceLocation FieldLoc
689 = SourceLocation::getFromRawEncoding(Record[Idx++]);
690 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
691 break;
692 }
693
694 case pch::DESIG_ARRAY: {
695 unsigned Index = Record[Idx++];
696 SourceLocation LBracketLoc
697 = SourceLocation::getFromRawEncoding(Record[Idx++]);
698 SourceLocation RBracketLoc
699 = SourceLocation::getFromRawEncoding(Record[Idx++]);
700 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
701 break;
702 }
703
704 case pch::DESIG_ARRAY_RANGE: {
705 unsigned Index = Record[Idx++];
706 SourceLocation LBracketLoc
707 = SourceLocation::getFromRawEncoding(Record[Idx++]);
708 SourceLocation EllipsisLoc
709 = SourceLocation::getFromRawEncoding(Record[Idx++]);
710 SourceLocation RBracketLoc
711 = SourceLocation::getFromRawEncoding(Record[Idx++]);
712 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
713 RBracketLoc));
714 break;
715 }
716 }
717 }
718 E->setDesignators(&Designators[0], Designators.size());
719
720 return NumSubExprs;
721}
722
723unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
724 VisitExpr(E);
725 return 0;
726}
727
Douglas Gregord3c98a02009-04-15 23:02:49 +0000728unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
729 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000730 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000731 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
732 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
733 return 1;
734}
735
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000736unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
737 VisitExpr(E);
738 E->setArgType1(Reader.GetType(Record[Idx++]));
739 E->setArgType2(Reader.GetType(Record[Idx++]));
740 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
741 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
742 return 0;
743}
744
745unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
746 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000747 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
748 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
749 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000750 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
751 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
752 return 3;
753}
754
755unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
756 VisitExpr(E);
757 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
758 return 0;
759}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000760
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000761unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
762 VisitExpr(E);
763 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000764 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000765 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
766 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
767 return NumExprs;
768}
769
770unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
771 VisitExpr(E);
772 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
773 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
774 E->setByRef(Record[Idx++]);
775 return 0;
776}
777
Douglas Gregor2cf26342009-04-09 22:27:44 +0000778// FIXME: use the diagnostics machinery
779static bool Error(const char *Str) {
780 std::fprintf(stderr, "%s\n", Str);
781 return true;
782}
783
Douglas Gregore1d918e2009-04-10 23:10:45 +0000784/// \brief Check the contents of the predefines buffer against the
785/// contents of the predefines buffer used to build the PCH file.
786///
787/// The contents of the two predefines buffers should be the same. If
788/// not, then some command-line option changed the preprocessor state
789/// and we must reject the PCH file.
790///
791/// \param PCHPredef The start of the predefines buffer in the PCH
792/// file.
793///
794/// \param PCHPredefLen The length of the predefines buffer in the PCH
795/// file.
796///
797/// \param PCHBufferID The FileID for the PCH predefines buffer.
798///
799/// \returns true if there was a mismatch (in which case the PCH file
800/// should be ignored), or false otherwise.
801bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
802 unsigned PCHPredefLen,
803 FileID PCHBufferID) {
804 const char *Predef = PP.getPredefines().c_str();
805 unsigned PredefLen = PP.getPredefines().size();
806
807 // If the two predefines buffers compare equal, we're done!.
808 if (PredefLen == PCHPredefLen &&
809 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
810 return false;
811
812 // The predefines buffers are different. Produce a reasonable
813 // diagnostic showing where they are different.
814
815 // The source locations (potentially in the two different predefines
816 // buffers)
817 SourceLocation Loc1, Loc2;
818 SourceManager &SourceMgr = PP.getSourceManager();
819
820 // Create a source buffer for our predefines string, so
821 // that we can build a diagnostic that points into that
822 // source buffer.
823 FileID BufferID;
824 if (Predef && Predef[0]) {
825 llvm::MemoryBuffer *Buffer
826 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
827 "<built-in>");
828 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
829 }
830
831 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
832 std::pair<const char *, const char *> Locations
833 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
834
835 if (Locations.first != Predef + MinLen) {
836 // We found the location in the two buffers where there is a
837 // difference. Form source locations to point there (in both
838 // buffers).
839 unsigned Offset = Locations.first - Predef;
840 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
841 .getFileLocWithOffset(Offset);
842 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
843 .getFileLocWithOffset(Offset);
844 } else if (PredefLen > PCHPredefLen) {
845 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
846 .getFileLocWithOffset(MinLen);
847 } else {
848 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
849 .getFileLocWithOffset(MinLen);
850 }
851
852 Diag(Loc1, diag::warn_pch_preprocessor);
853 if (Loc2.isValid())
854 Diag(Loc2, diag::note_predef_in_pch);
855 Diag(diag::note_ignoring_pch) << FileName;
856 return true;
857}
858
Douglas Gregorbd945002009-04-13 16:31:14 +0000859/// \brief Read the line table in the source manager block.
860/// \returns true if ther was an error.
861static bool ParseLineTable(SourceManager &SourceMgr,
862 llvm::SmallVectorImpl<uint64_t> &Record) {
863 unsigned Idx = 0;
864 LineTableInfo &LineTable = SourceMgr.getLineTable();
865
866 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000867 std::map<int, int> FileIDs;
868 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000869 // Extract the file name
870 unsigned FilenameLen = Record[Idx++];
871 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
872 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000873 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
874 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000875 }
876
877 // Parse the line entries
878 std::vector<LineEntry> Entries;
879 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000880 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000881
882 // Extract the line entries
883 unsigned NumEntries = Record[Idx++];
884 Entries.clear();
885 Entries.reserve(NumEntries);
886 for (unsigned I = 0; I != NumEntries; ++I) {
887 unsigned FileOffset = Record[Idx++];
888 unsigned LineNo = Record[Idx++];
889 int FilenameID = Record[Idx++];
890 SrcMgr::CharacteristicKind FileKind
891 = (SrcMgr::CharacteristicKind)Record[Idx++];
892 unsigned IncludeOffset = Record[Idx++];
893 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
894 FileKind, IncludeOffset));
895 }
896 LineTable.AddEntry(FID, Entries);
897 }
898
899 return false;
900}
901
Douglas Gregor14f79002009-04-10 03:52:48 +0000902/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000903PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000904 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000905 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
906 Error("Malformed source manager block record");
907 return Failure;
908 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000909
910 SourceManager &SourceMgr = Context.getSourceManager();
911 RecordData Record;
912 while (true) {
913 unsigned Code = Stream.ReadCode();
914 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000915 if (Stream.ReadBlockEnd()) {
916 Error("Error at end of Source Manager block");
917 return Failure;
918 }
919
920 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000921 }
922
923 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
924 // No known subblocks, always skip them.
925 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000926 if (Stream.SkipBlock()) {
927 Error("Malformed block record");
928 return Failure;
929 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000930 continue;
931 }
932
933 if (Code == llvm::bitc::DEFINE_ABBREV) {
934 Stream.ReadAbbrevRecord();
935 continue;
936 }
937
938 // Read a record.
939 const char *BlobStart;
940 unsigned BlobLen;
941 Record.clear();
942 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
943 default: // Default behavior: ignore.
944 break;
945
946 case pch::SM_SLOC_FILE_ENTRY: {
947 // FIXME: We would really like to delay the creation of this
948 // FileEntry until it is actually required, e.g., when producing
949 // a diagnostic with a source location in this file.
950 const FileEntry *File
951 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
952 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +0000953 FileID ID = SourceMgr.createFileID(File,
954 SourceLocation::getFromRawEncoding(Record[1]),
955 (CharacteristicKind)Record[2]);
956 if (Record[3])
957 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
958 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +0000959 break;
960 }
961
962 case pch::SM_SLOC_BUFFER_ENTRY: {
963 const char *Name = BlobStart;
964 unsigned Code = Stream.ReadCode();
965 Record.clear();
966 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
967 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000968 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000969 llvm::MemoryBuffer *Buffer
970 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
971 BlobStart + BlobLen - 1,
972 Name);
973 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
974
975 if (strcmp(Name, "<built-in>") == 0
976 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
977 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 break;
979 }
980
981 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
982 SourceLocation SpellingLoc
983 = SourceLocation::getFromRawEncoding(Record[1]);
984 SourceMgr.createInstantiationLoc(
985 SpellingLoc,
986 SourceLocation::getFromRawEncoding(Record[2]),
987 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +0000988 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +0000989 break;
990 }
991
Chris Lattner2c78b872009-04-14 23:22:57 +0000992 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +0000993 if (ParseLineTable(SourceMgr, Record))
994 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000995 break;
Douglas Gregor14f79002009-04-10 03:52:48 +0000996 }
997 }
998}
999
Chris Lattner42d42b52009-04-10 21:41:48 +00001000bool PCHReader::ReadPreprocessorBlock() {
1001 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1002 return Error("Malformed preprocessor block record");
1003
Chris Lattner42d42b52009-04-10 21:41:48 +00001004 RecordData Record;
1005 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1006 MacroInfo *LastMacro = 0;
1007
1008 while (true) {
1009 unsigned Code = Stream.ReadCode();
1010 switch (Code) {
1011 case llvm::bitc::END_BLOCK:
1012 if (Stream.ReadBlockEnd())
1013 return Error("Error at end of preprocessor block");
1014 return false;
1015
1016 case llvm::bitc::ENTER_SUBBLOCK:
1017 // No known subblocks, always skip them.
1018 Stream.ReadSubBlockID();
1019 if (Stream.SkipBlock())
1020 return Error("Malformed block record");
1021 continue;
1022
1023 case llvm::bitc::DEFINE_ABBREV:
1024 Stream.ReadAbbrevRecord();
1025 continue;
1026 default: break;
1027 }
1028
1029 // Read a record.
1030 Record.clear();
1031 pch::PreprocessorRecordTypes RecType =
1032 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1033 switch (RecType) {
1034 default: // Default behavior: ignore unknown records.
1035 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001036 case pch::PP_COUNTER_VALUE:
1037 if (!Record.empty())
1038 PP.setCounterValue(Record[0]);
1039 break;
1040
Chris Lattner42d42b52009-04-10 21:41:48 +00001041 case pch::PP_MACRO_OBJECT_LIKE:
1042 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +00001043 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1044 if (II == 0)
1045 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +00001046 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1047 bool isUsed = Record[2];
1048
1049 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1050 MI->setIsUsed(isUsed);
1051
1052 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1053 // Decode function-like macro info.
1054 bool isC99VarArgs = Record[3];
1055 bool isGNUVarArgs = Record[4];
1056 MacroArgs.clear();
1057 unsigned NumArgs = Record[5];
1058 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +00001059 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +00001060
1061 // Install function-like macro info.
1062 MI->setIsFunctionLike();
1063 if (isC99VarArgs) MI->setIsC99Varargs();
1064 if (isGNUVarArgs) MI->setIsGNUVarargs();
1065 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1066 PP.getPreprocessorAllocator());
1067 }
1068
1069 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +00001070 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +00001071
1072 // Remember that we saw this macro last so that we add the tokens that
1073 // form its body to it.
1074 LastMacro = MI;
1075 break;
1076 }
1077
1078 case pch::PP_TOKEN: {
1079 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1080 // pretend we didn't see this.
1081 if (LastMacro == 0) break;
1082
1083 Token Tok;
1084 Tok.startToken();
1085 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1086 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001087 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1088 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001089 Tok.setKind((tok::TokenKind)Record[3]);
1090 Tok.setFlag((Token::TokenFlags)Record[4]);
1091 LastMacro->AddTokenToBody(Tok);
1092 break;
1093 }
1094 }
1095 }
1096}
1097
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001098PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1099 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1100 Error("Malformed block record");
1101 return Failure;
1102 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001103
Chris Lattner7356a312009-04-11 21:15:38 +00001104 uint64_t PreprocessorBlockBit = 0;
1105
Douglas Gregor2cf26342009-04-09 22:27:44 +00001106 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001107 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001108 while (!Stream.AtEndOfStream()) {
1109 unsigned Code = Stream.ReadCode();
1110 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001111 // If we saw the preprocessor block, read it now.
1112 if (PreprocessorBlockBit) {
1113 uint64_t SavedPos = Stream.GetCurrentBitNo();
1114 Stream.JumpToBit(PreprocessorBlockBit);
1115 if (ReadPreprocessorBlock()) {
1116 Error("Malformed preprocessor block");
1117 return Failure;
1118 }
1119 Stream.JumpToBit(SavedPos);
1120 }
1121
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001122 if (Stream.ReadBlockEnd()) {
1123 Error("Error at end of module block");
1124 return Failure;
1125 }
Chris Lattner7356a312009-04-11 21:15:38 +00001126
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001127 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001128 }
1129
1130 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1131 switch (Stream.ReadSubBlockID()) {
1132 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1133 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1134 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001135 if (Stream.SkipBlock()) {
1136 Error("Malformed block record");
1137 return Failure;
1138 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001139 break;
1140
Chris Lattner7356a312009-04-11 21:15:38 +00001141 case pch::PREPROCESSOR_BLOCK_ID:
1142 // Skip the preprocessor block for now, but remember where it is. We
1143 // want to read it in after the identifier table.
1144 if (PreprocessorBlockBit) {
1145 Error("Multiple preprocessor blocks found.");
1146 return Failure;
1147 }
1148 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1149 if (Stream.SkipBlock()) {
1150 Error("Malformed block record");
1151 return Failure;
1152 }
1153 break;
1154
Douglas Gregor14f79002009-04-10 03:52:48 +00001155 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001156 switch (ReadSourceManagerBlock()) {
1157 case Success:
1158 break;
1159
1160 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001161 Error("Malformed source manager block");
1162 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001163
1164 case IgnorePCH:
1165 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001166 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001167 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001168 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001169 continue;
1170 }
1171
1172 if (Code == llvm::bitc::DEFINE_ABBREV) {
1173 Stream.ReadAbbrevRecord();
1174 continue;
1175 }
1176
1177 // Read and process a record.
1178 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001179 const char *BlobStart = 0;
1180 unsigned BlobLen = 0;
1181 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1182 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001183 default: // Default behavior: ignore.
1184 break;
1185
1186 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001187 if (!TypeOffsets.empty()) {
1188 Error("Duplicate TYPE_OFFSET record in PCH file");
1189 return Failure;
1190 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001191 TypeOffsets.swap(Record);
1192 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1193 break;
1194
1195 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001196 if (!DeclOffsets.empty()) {
1197 Error("Duplicate DECL_OFFSET record in PCH file");
1198 return Failure;
1199 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001200 DeclOffsets.swap(Record);
1201 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1202 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001203
1204 case pch::LANGUAGE_OPTIONS:
1205 if (ParseLanguageOptions(Record))
1206 return IgnorePCH;
1207 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001208
Douglas Gregorafaf3082009-04-11 00:14:32 +00001209 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001210 std::string TargetTriple(BlobStart, BlobLen);
1211 if (TargetTriple != Context.Target.getTargetTriple()) {
1212 Diag(diag::warn_pch_target_triple)
1213 << TargetTriple << Context.Target.getTargetTriple();
1214 Diag(diag::note_ignoring_pch) << FileName;
1215 return IgnorePCH;
1216 }
1217 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001218 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001219
1220 case pch::IDENTIFIER_TABLE:
1221 IdentifierTable = BlobStart;
1222 break;
1223
1224 case pch::IDENTIFIER_OFFSET:
1225 if (!IdentifierData.empty()) {
1226 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1227 return Failure;
1228 }
1229 IdentifierData.swap(Record);
1230#ifndef NDEBUG
1231 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1232 if ((IdentifierData[I] & 0x01) == 0) {
1233 Error("Malformed identifier table in the precompiled header");
1234 return Failure;
1235 }
1236 }
1237#endif
1238 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001239
1240 case pch::EXTERNAL_DEFINITIONS:
1241 if (!ExternalDefinitions.empty()) {
1242 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1243 return Failure;
1244 }
1245 ExternalDefinitions.swap(Record);
1246 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001247 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001248 }
1249
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001250 Error("Premature end of bitstream");
1251 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001252}
1253
Douglas Gregore1d918e2009-04-10 23:10:45 +00001254PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001255 // Set the PCH file name.
1256 this->FileName = FileName;
1257
Douglas Gregor2cf26342009-04-09 22:27:44 +00001258 // Open the PCH file.
1259 std::string ErrStr;
1260 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001261 if (!Buffer) {
1262 Error(ErrStr.c_str());
1263 return IgnorePCH;
1264 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001265
1266 // Initialize the stream
1267 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1268 (const unsigned char *)Buffer->getBufferEnd());
1269
1270 // Sniff for the signature.
1271 if (Stream.Read(8) != 'C' ||
1272 Stream.Read(8) != 'P' ||
1273 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001274 Stream.Read(8) != 'H') {
1275 Error("Not a PCH file");
1276 return IgnorePCH;
1277 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001278
1279 // We expect a number of well-defined blocks, though we don't necessarily
1280 // need to understand them all.
1281 while (!Stream.AtEndOfStream()) {
1282 unsigned Code = Stream.ReadCode();
1283
Douglas Gregore1d918e2009-04-10 23:10:45 +00001284 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1285 Error("Invalid record at top-level");
1286 return Failure;
1287 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001288
1289 unsigned BlockID = Stream.ReadSubBlockID();
1290
1291 // We only know the PCH subblock ID.
1292 switch (BlockID) {
1293 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001294 if (Stream.ReadBlockInfoBlock()) {
1295 Error("Malformed BlockInfoBlock");
1296 return Failure;
1297 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001298 break;
1299 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001300 switch (ReadPCHBlock()) {
1301 case Success:
1302 break;
1303
1304 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001305 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001306
1307 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001308 // FIXME: We could consider reading through to the end of this
1309 // PCH block, skipping subblocks, to see if there are other
1310 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001311 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001312 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001313 break;
1314 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001315 if (Stream.SkipBlock()) {
1316 Error("Malformed block record");
1317 return Failure;
1318 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001319 break;
1320 }
1321 }
1322
1323 // Load the translation unit declaration
1324 ReadDeclRecord(DeclOffsets[0], 0);
1325
Douglas Gregore1d918e2009-04-10 23:10:45 +00001326 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001327}
1328
Douglas Gregor0b748912009-04-14 21:18:50 +00001329namespace {
1330 /// \brief Helper class that saves the current stream position and
1331 /// then restores it when destroyed.
1332 struct VISIBILITY_HIDDEN SavedStreamPosition {
1333 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001334 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001335
1336 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001337 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001338 }
1339
1340 private:
1341 llvm::BitstreamReader &Stream;
1342 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001343 };
1344}
1345
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001346/// \brief Parse the record that corresponds to a LangOptions data
1347/// structure.
1348///
1349/// This routine compares the language options used to generate the
1350/// PCH file against the language options set for the current
1351/// compilation. For each option, we classify differences between the
1352/// two compiler states as either "benign" or "important". Benign
1353/// differences don't matter, and we accept them without complaint
1354/// (and without modifying the language options). Differences between
1355/// the states for important options cause the PCH file to be
1356/// unusable, so we emit a warning and return true to indicate that
1357/// there was an error.
1358///
1359/// \returns true if the PCH file is unacceptable, false otherwise.
1360bool PCHReader::ParseLanguageOptions(
1361 const llvm::SmallVectorImpl<uint64_t> &Record) {
1362 const LangOptions &LangOpts = Context.getLangOptions();
1363#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1364#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1365 if (Record[Idx] != LangOpts.Option) { \
1366 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1367 Diag(diag::note_ignoring_pch) << FileName; \
1368 return true; \
1369 } \
1370 ++Idx
1371
1372 unsigned Idx = 0;
1373 PARSE_LANGOPT_BENIGN(Trigraphs);
1374 PARSE_LANGOPT_BENIGN(BCPLComment);
1375 PARSE_LANGOPT_BENIGN(DollarIdents);
1376 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1377 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1378 PARSE_LANGOPT_BENIGN(ImplicitInt);
1379 PARSE_LANGOPT_BENIGN(Digraphs);
1380 PARSE_LANGOPT_BENIGN(HexFloats);
1381 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1382 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1383 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1384 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1385 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1386 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1387 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1388 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1389 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1390 PARSE_LANGOPT_BENIGN(PascalStrings);
1391 PARSE_LANGOPT_BENIGN(Boolean);
1392 PARSE_LANGOPT_BENIGN(WritableStrings);
1393 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1394 diag::warn_pch_lax_vector_conversions);
1395 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1396 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1397 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1398 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1399 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1400 diag::warn_pch_thread_safe_statics);
1401 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1402 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1403 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1404 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1405 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1406 diag::warn_pch_heinous_extensions);
1407 // FIXME: Most of the options below are benign if the macro wasn't
1408 // used. Unfortunately, this means that a PCH compiled without
1409 // optimization can't be used with optimization turned on, even
1410 // though the only thing that changes is whether __OPTIMIZE__ was
1411 // defined... but if __OPTIMIZE__ never showed up in the header, it
1412 // doesn't matter. We could consider making this some special kind
1413 // of check.
1414 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1415 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1416 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1417 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1418 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1419 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1420 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1421 Diag(diag::warn_pch_gc_mode)
1422 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1423 Diag(diag::note_ignoring_pch) << FileName;
1424 return true;
1425 }
1426 ++Idx;
1427 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1428 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1429#undef PARSE_LANGOPT_IRRELEVANT
1430#undef PARSE_LANGOPT_BENIGN
1431
1432 return false;
1433}
1434
Douglas Gregor2cf26342009-04-09 22:27:44 +00001435/// \brief Read and return the type at the given offset.
1436///
1437/// This routine actually reads the record corresponding to the type
1438/// at the given offset in the bitstream. It is a helper routine for
1439/// GetType, which deals with reading type IDs.
1440QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001441 // Keep track of where we are in the stream, then jump back there
1442 // after reading this type.
1443 SavedStreamPosition SavedPosition(Stream);
1444
Douglas Gregor2cf26342009-04-09 22:27:44 +00001445 Stream.JumpToBit(Offset);
1446 RecordData Record;
1447 unsigned Code = Stream.ReadCode();
1448 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001449 case pch::TYPE_EXT_QUAL: {
1450 assert(Record.size() == 3 &&
1451 "Incorrect encoding of extended qualifier type");
1452 QualType Base = GetType(Record[0]);
1453 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1454 unsigned AddressSpace = Record[2];
1455
1456 QualType T = Base;
1457 if (GCAttr != QualType::GCNone)
1458 T = Context.getObjCGCQualType(T, GCAttr);
1459 if (AddressSpace)
1460 T = Context.getAddrSpaceQualType(T, AddressSpace);
1461 return T;
1462 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001463
Douglas Gregor2cf26342009-04-09 22:27:44 +00001464 case pch::TYPE_FIXED_WIDTH_INT: {
1465 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1466 return Context.getFixedWidthIntType(Record[0], Record[1]);
1467 }
1468
1469 case pch::TYPE_COMPLEX: {
1470 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1471 QualType ElemType = GetType(Record[0]);
1472 return Context.getComplexType(ElemType);
1473 }
1474
1475 case pch::TYPE_POINTER: {
1476 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1477 QualType PointeeType = GetType(Record[0]);
1478 return Context.getPointerType(PointeeType);
1479 }
1480
1481 case pch::TYPE_BLOCK_POINTER: {
1482 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1483 QualType PointeeType = GetType(Record[0]);
1484 return Context.getBlockPointerType(PointeeType);
1485 }
1486
1487 case pch::TYPE_LVALUE_REFERENCE: {
1488 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1489 QualType PointeeType = GetType(Record[0]);
1490 return Context.getLValueReferenceType(PointeeType);
1491 }
1492
1493 case pch::TYPE_RVALUE_REFERENCE: {
1494 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1495 QualType PointeeType = GetType(Record[0]);
1496 return Context.getRValueReferenceType(PointeeType);
1497 }
1498
1499 case pch::TYPE_MEMBER_POINTER: {
1500 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1501 QualType PointeeType = GetType(Record[0]);
1502 QualType ClassType = GetType(Record[1]);
1503 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1504 }
1505
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001506 case pch::TYPE_CONSTANT_ARRAY: {
1507 QualType ElementType = GetType(Record[0]);
1508 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1509 unsigned IndexTypeQuals = Record[2];
1510 unsigned Idx = 3;
1511 llvm::APInt Size = ReadAPInt(Record, Idx);
1512 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1513 }
1514
1515 case pch::TYPE_INCOMPLETE_ARRAY: {
1516 QualType ElementType = GetType(Record[0]);
1517 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1518 unsigned IndexTypeQuals = Record[2];
1519 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1520 }
1521
1522 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001523 QualType ElementType = GetType(Record[0]);
1524 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1525 unsigned IndexTypeQuals = Record[2];
1526 return Context.getVariableArrayType(ElementType, ReadExpr(),
1527 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001528 }
1529
1530 case pch::TYPE_VECTOR: {
1531 if (Record.size() != 2) {
1532 Error("Incorrect encoding of vector type in PCH file");
1533 return QualType();
1534 }
1535
1536 QualType ElementType = GetType(Record[0]);
1537 unsigned NumElements = Record[1];
1538 return Context.getVectorType(ElementType, NumElements);
1539 }
1540
1541 case pch::TYPE_EXT_VECTOR: {
1542 if (Record.size() != 2) {
1543 Error("Incorrect encoding of extended vector type in PCH file");
1544 return QualType();
1545 }
1546
1547 QualType ElementType = GetType(Record[0]);
1548 unsigned NumElements = Record[1];
1549 return Context.getExtVectorType(ElementType, NumElements);
1550 }
1551
1552 case pch::TYPE_FUNCTION_NO_PROTO: {
1553 if (Record.size() != 1) {
1554 Error("Incorrect encoding of no-proto function type");
1555 return QualType();
1556 }
1557 QualType ResultType = GetType(Record[0]);
1558 return Context.getFunctionNoProtoType(ResultType);
1559 }
1560
1561 case pch::TYPE_FUNCTION_PROTO: {
1562 QualType ResultType = GetType(Record[0]);
1563 unsigned Idx = 1;
1564 unsigned NumParams = Record[Idx++];
1565 llvm::SmallVector<QualType, 16> ParamTypes;
1566 for (unsigned I = 0; I != NumParams; ++I)
1567 ParamTypes.push_back(GetType(Record[Idx++]));
1568 bool isVariadic = Record[Idx++];
1569 unsigned Quals = Record[Idx++];
1570 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1571 isVariadic, Quals);
1572 }
1573
1574 case pch::TYPE_TYPEDEF:
1575 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1576 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1577
1578 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001579 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001580
1581 case pch::TYPE_TYPEOF: {
1582 if (Record.size() != 1) {
1583 Error("Incorrect encoding of typeof(type) in PCH file");
1584 return QualType();
1585 }
1586 QualType UnderlyingType = GetType(Record[0]);
1587 return Context.getTypeOfType(UnderlyingType);
1588 }
1589
1590 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001591 assert(Record.size() == 1 && "Incorrect encoding of record type");
1592 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001593
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001594 case pch::TYPE_ENUM:
1595 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1596 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1597
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001598 case pch::TYPE_OBJC_INTERFACE:
1599 // FIXME: Deserialize ObjCInterfaceType
1600 assert(false && "Cannot de-serialize ObjC interface types yet");
1601 return QualType();
1602
1603 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1604 // FIXME: Deserialize ObjCQualifiedInterfaceType
1605 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1606 return QualType();
1607
1608 case pch::TYPE_OBJC_QUALIFIED_ID:
1609 // FIXME: Deserialize ObjCQualifiedIdType
1610 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1611 return QualType();
1612
1613 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1614 // FIXME: Deserialize ObjCQualifiedClassType
1615 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1616 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001617 }
1618
1619 // Suppress a GCC warning
1620 return QualType();
1621}
1622
1623/// \brief Note that we have loaded the declaration with the given
1624/// Index.
1625///
1626/// This routine notes that this declaration has already been loaded,
1627/// so that future GetDecl calls will return this declaration rather
1628/// than trying to load a new declaration.
1629inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1630 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1631 DeclAlreadyLoaded[Index] = true;
1632 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1633}
1634
1635/// \brief Read the declaration at the given offset from the PCH file.
1636Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001637 // Keep track of where we are in the stream, then jump back there
1638 // after reading this declaration.
1639 SavedStreamPosition SavedPosition(Stream);
1640
Douglas Gregor2cf26342009-04-09 22:27:44 +00001641 Decl *D = 0;
1642 Stream.JumpToBit(Offset);
1643 RecordData Record;
1644 unsigned Code = Stream.ReadCode();
1645 unsigned Idx = 0;
1646 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001647
Douglas Gregor2cf26342009-04-09 22:27:44 +00001648 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001649 case pch::DECL_ATTR:
1650 case pch::DECL_CONTEXT_LEXICAL:
1651 case pch::DECL_CONTEXT_VISIBLE:
1652 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1653 break;
1654
Douglas Gregor2cf26342009-04-09 22:27:44 +00001655 case pch::DECL_TRANSLATION_UNIT:
1656 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001657 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001658 break;
1659
1660 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001661 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001662 break;
1663 }
1664
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001665 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001666 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001667 break;
1668 }
1669
Douglas Gregor8c700062009-04-13 21:20:57 +00001670 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001671 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1672 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001673 break;
1674 }
1675
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001676 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001677 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1678 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001679 break;
1680 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001681
1682 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001683 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1684 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001685 break;
1686 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001687
Douglas Gregor8c700062009-04-13 21:20:57 +00001688 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001689 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1690 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001691 break;
1692 }
1693
Douglas Gregor2cf26342009-04-09 22:27:44 +00001694 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001695 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1696 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001697 break;
1698 }
1699
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001700 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001701 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1702 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001703 break;
1704 }
1705
1706 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001707 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001708 QualType(), QualType(), VarDecl::None,
1709 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001710 break;
1711 }
1712
Douglas Gregor1028bc62009-04-13 22:49:25 +00001713 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001714 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001715 break;
1716 }
1717
1718 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001719 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001720 break;
1721 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001722 }
1723
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001724 assert(D && "Unknown declaration creating PCH file");
1725 if (D) {
1726 LoadedDecl(Index, D);
1727 Reader.Visit(D);
1728 }
1729
Douglas Gregor2cf26342009-04-09 22:27:44 +00001730 // If this declaration is also a declaration context, get the
1731 // offsets for its tables of lexical and visible declarations.
1732 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1733 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1734 if (Offsets.first || Offsets.second) {
1735 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1736 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1737 DeclContextOffsets[DC] = Offsets;
1738 }
1739 }
1740 assert(Idx == Record.size());
1741
1742 return D;
1743}
1744
Douglas Gregor8038d512009-04-10 17:25:41 +00001745QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001746 unsigned Quals = ID & 0x07;
1747 unsigned Index = ID >> 3;
1748
1749 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1750 QualType T;
1751 switch ((pch::PredefinedTypeIDs)Index) {
1752 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1753 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1754 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1755
1756 case pch::PREDEF_TYPE_CHAR_U_ID:
1757 case pch::PREDEF_TYPE_CHAR_S_ID:
1758 // FIXME: Check that the signedness of CharTy is correct!
1759 T = Context.CharTy;
1760 break;
1761
1762 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1763 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1764 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1765 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1766 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1767 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1768 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1769 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1770 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1771 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1772 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1773 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1774 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1775 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1776 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1777 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1778 }
1779
1780 assert(!T.isNull() && "Unknown predefined type");
1781 return T.getQualifiedType(Quals);
1782 }
1783
1784 Index -= pch::NUM_PREDEF_TYPE_IDS;
1785 if (!TypeAlreadyLoaded[Index]) {
1786 // Load the type from the PCH file.
1787 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1788 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1789 TypeAlreadyLoaded[Index] = true;
1790 }
1791
1792 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1793}
1794
Douglas Gregor8038d512009-04-10 17:25:41 +00001795Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001796 if (ID == 0)
1797 return 0;
1798
1799 unsigned Index = ID - 1;
1800 if (DeclAlreadyLoaded[Index])
1801 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1802
1803 // Load the declaration from the PCH file.
1804 return ReadDeclRecord(DeclOffsets[Index], Index);
1805}
1806
1807bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001808 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001809 assert(DC->hasExternalLexicalStorage() &&
1810 "DeclContext has no lexical decls in storage");
1811 uint64_t Offset = DeclContextOffsets[DC].first;
1812 assert(Offset && "DeclContext has no lexical decls in storage");
1813
Douglas Gregor0b748912009-04-14 21:18:50 +00001814 // Keep track of where we are in the stream, then jump back there
1815 // after reading this context.
1816 SavedStreamPosition SavedPosition(Stream);
1817
Douglas Gregor2cf26342009-04-09 22:27:44 +00001818 // Load the record containing all of the declarations lexically in
1819 // this context.
1820 Stream.JumpToBit(Offset);
1821 RecordData Record;
1822 unsigned Code = Stream.ReadCode();
1823 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001824 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001825 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1826
1827 // Load all of the declaration IDs
1828 Decls.clear();
1829 Decls.insert(Decls.end(), Record.begin(), Record.end());
1830 return false;
1831}
1832
1833bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1834 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1835 assert(DC->hasExternalVisibleStorage() &&
1836 "DeclContext has no visible decls in storage");
1837 uint64_t Offset = DeclContextOffsets[DC].second;
1838 assert(Offset && "DeclContext has no visible decls in storage");
1839
Douglas Gregor0b748912009-04-14 21:18:50 +00001840 // Keep track of where we are in the stream, then jump back there
1841 // after reading this context.
1842 SavedStreamPosition SavedPosition(Stream);
1843
Douglas Gregor2cf26342009-04-09 22:27:44 +00001844 // Load the record containing all of the declarations visible in
1845 // this context.
1846 Stream.JumpToBit(Offset);
1847 RecordData Record;
1848 unsigned Code = Stream.ReadCode();
1849 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001850 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001851 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1852 if (Record.size() == 0)
1853 return false;
1854
1855 Decls.clear();
1856
1857 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001858 while (Idx < Record.size()) {
1859 Decls.push_back(VisibleDeclaration());
1860 Decls.back().Name = ReadDeclarationName(Record, Idx);
1861
Douglas Gregor2cf26342009-04-09 22:27:44 +00001862 unsigned Size = Record[Idx++];
1863 llvm::SmallVector<unsigned, 4> & LoadedDecls
1864 = Decls.back().Declarations;
1865 LoadedDecls.reserve(Size);
1866 for (unsigned I = 0; I < Size; ++I)
1867 LoadedDecls.push_back(Record[Idx++]);
1868 }
1869
1870 return false;
1871}
1872
Douglas Gregorfdd01722009-04-14 00:24:19 +00001873void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1874 if (!Consumer)
1875 return;
1876
1877 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1878 Decl *D = GetDecl(ExternalDefinitions[I]);
1879 DeclGroupRef DG(D);
1880 Consumer->HandleTopLevelDecl(DG);
1881 }
1882}
1883
Douglas Gregor2cf26342009-04-09 22:27:44 +00001884void PCHReader::PrintStats() {
1885 std::fprintf(stderr, "*** PCH Statistics:\n");
1886
1887 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1888 TypeAlreadyLoaded.end(),
1889 true);
1890 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1891 DeclAlreadyLoaded.end(),
1892 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001893 unsigned NumIdentifiersLoaded = 0;
1894 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1895 if ((IdentifierData[I] & 0x01) == 0)
1896 ++NumIdentifiersLoaded;
1897 }
1898
Douglas Gregor2cf26342009-04-09 22:27:44 +00001899 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1900 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001901 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001902 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1903 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001904 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1905 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1906 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1907 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001908 std::fprintf(stderr, "\n");
1909}
1910
Chris Lattner7356a312009-04-11 21:15:38 +00001911IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001912 if (ID == 0)
1913 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001914
Douglas Gregorafaf3082009-04-11 00:14:32 +00001915 if (!IdentifierTable || IdentifierData.empty()) {
1916 Error("No identifier table in PCH file");
1917 return 0;
1918 }
Chris Lattner7356a312009-04-11 21:15:38 +00001919
Douglas Gregorafaf3082009-04-11 00:14:32 +00001920 if (IdentifierData[ID - 1] & 0x01) {
1921 uint64_t Offset = IdentifierData[ID - 1];
1922 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001923 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001924 }
Chris Lattner7356a312009-04-11 21:15:38 +00001925
1926 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001927}
1928
1929DeclarationName
1930PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1931 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1932 switch (Kind) {
1933 case DeclarationName::Identifier:
1934 return DeclarationName(GetIdentifierInfo(Record, Idx));
1935
1936 case DeclarationName::ObjCZeroArgSelector:
1937 case DeclarationName::ObjCOneArgSelector:
1938 case DeclarationName::ObjCMultiArgSelector:
1939 assert(false && "Unable to de-serialize Objective-C selectors");
1940 break;
1941
1942 case DeclarationName::CXXConstructorName:
1943 return Context.DeclarationNames.getCXXConstructorName(
1944 GetType(Record[Idx++]));
1945
1946 case DeclarationName::CXXDestructorName:
1947 return Context.DeclarationNames.getCXXDestructorName(
1948 GetType(Record[Idx++]));
1949
1950 case DeclarationName::CXXConversionFunctionName:
1951 return Context.DeclarationNames.getCXXConversionFunctionName(
1952 GetType(Record[Idx++]));
1953
1954 case DeclarationName::CXXOperatorName:
1955 return Context.DeclarationNames.getCXXOperatorName(
1956 (OverloadedOperatorKind)Record[Idx++]);
1957
1958 case DeclarationName::CXXUsingDirective:
1959 return DeclarationName::getUsingDirectiveName();
1960 }
1961
1962 // Required to silence GCC warning
1963 return DeclarationName();
1964}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001965
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001966/// \brief Read an integral value
1967llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1968 unsigned BitWidth = Record[Idx++];
1969 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1970 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1971 Idx += NumWords;
1972 return Result;
1973}
1974
1975/// \brief Read a signed integral value
1976llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1977 bool isUnsigned = Record[Idx++];
1978 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1979}
1980
Douglas Gregor17fc2232009-04-14 21:55:33 +00001981/// \brief Read a floating-point value
1982llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00001983 return llvm::APFloat(ReadAPInt(Record, Idx));
1984}
1985
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001986// \brief Read a string
1987std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1988 unsigned Len = Record[Idx++];
1989 std::string Result(&Record[Idx], &Record[Idx] + Len);
1990 Idx += Len;
1991 return Result;
1992}
1993
1994/// \brief Reads attributes from the current stream position.
1995Attr *PCHReader::ReadAttributes() {
1996 unsigned Code = Stream.ReadCode();
1997 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1998 "Expected unabbreviated record"); (void)Code;
1999
2000 RecordData Record;
2001 unsigned Idx = 0;
2002 unsigned RecCode = Stream.ReadRecord(Code, Record);
2003 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2004 (void)RecCode;
2005
2006#define SIMPLE_ATTR(Name) \
2007 case Attr::Name: \
2008 New = ::new (Context) Name##Attr(); \
2009 break
2010
2011#define STRING_ATTR(Name) \
2012 case Attr::Name: \
2013 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2014 break
2015
2016#define UNSIGNED_ATTR(Name) \
2017 case Attr::Name: \
2018 New = ::new (Context) Name##Attr(Record[Idx++]); \
2019 break
2020
2021 Attr *Attrs = 0;
2022 while (Idx < Record.size()) {
2023 Attr *New = 0;
2024 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2025 bool IsInherited = Record[Idx++];
2026
2027 switch (Kind) {
2028 STRING_ATTR(Alias);
2029 UNSIGNED_ATTR(Aligned);
2030 SIMPLE_ATTR(AlwaysInline);
2031 SIMPLE_ATTR(AnalyzerNoReturn);
2032 STRING_ATTR(Annotate);
2033 STRING_ATTR(AsmLabel);
2034
2035 case Attr::Blocks:
2036 New = ::new (Context) BlocksAttr(
2037 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2038 break;
2039
2040 case Attr::Cleanup:
2041 New = ::new (Context) CleanupAttr(
2042 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2043 break;
2044
2045 SIMPLE_ATTR(Const);
2046 UNSIGNED_ATTR(Constructor);
2047 SIMPLE_ATTR(DLLExport);
2048 SIMPLE_ATTR(DLLImport);
2049 SIMPLE_ATTR(Deprecated);
2050 UNSIGNED_ATTR(Destructor);
2051 SIMPLE_ATTR(FastCall);
2052
2053 case Attr::Format: {
2054 std::string Type = ReadString(Record, Idx);
2055 unsigned FormatIdx = Record[Idx++];
2056 unsigned FirstArg = Record[Idx++];
2057 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2058 break;
2059 }
2060
2061 SIMPLE_ATTR(GNUCInline);
2062
2063 case Attr::IBOutletKind:
2064 New = ::new (Context) IBOutletAttr();
2065 break;
2066
2067 SIMPLE_ATTR(NoReturn);
2068 SIMPLE_ATTR(NoThrow);
2069 SIMPLE_ATTR(Nodebug);
2070 SIMPLE_ATTR(Noinline);
2071
2072 case Attr::NonNull: {
2073 unsigned Size = Record[Idx++];
2074 llvm::SmallVector<unsigned, 16> ArgNums;
2075 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2076 Idx += Size;
2077 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2078 break;
2079 }
2080
2081 SIMPLE_ATTR(ObjCException);
2082 SIMPLE_ATTR(ObjCNSObject);
2083 SIMPLE_ATTR(Overloadable);
2084 UNSIGNED_ATTR(Packed);
2085 SIMPLE_ATTR(Pure);
2086 UNSIGNED_ATTR(Regparm);
2087 STRING_ATTR(Section);
2088 SIMPLE_ATTR(StdCall);
2089 SIMPLE_ATTR(TransparentUnion);
2090 SIMPLE_ATTR(Unavailable);
2091 SIMPLE_ATTR(Unused);
2092 SIMPLE_ATTR(Used);
2093
2094 case Attr::Visibility:
2095 New = ::new (Context) VisibilityAttr(
2096 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2097 break;
2098
2099 SIMPLE_ATTR(WarnUnusedResult);
2100 SIMPLE_ATTR(Weak);
2101 SIMPLE_ATTR(WeakImport);
2102 }
2103
2104 assert(New && "Unable to decode attribute?");
2105 New->setInherited(IsInherited);
2106 New->setNext(Attrs);
2107 Attrs = New;
2108 }
2109#undef UNSIGNED_ATTR
2110#undef STRING_ATTR
2111#undef SIMPLE_ATTR
2112
2113 // The list of attributes was built backwards. Reverse the list
2114 // before returning it.
2115 Attr *PrevAttr = 0, *NextAttr = 0;
2116 while (Attrs) {
2117 NextAttr = Attrs->getNext();
2118 Attrs->setNext(PrevAttr);
2119 PrevAttr = Attrs;
2120 Attrs = NextAttr;
2121 }
2122
2123 return PrevAttr;
2124}
2125
Douglas Gregorc9490c02009-04-16 22:23:12 +00002126Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002127 // Within the bitstream, expressions are stored in Reverse Polish
2128 // Notation, with each of the subexpressions preceding the
2129 // expression they are stored in. To evaluate expressions, we
2130 // continue reading expressions and placing them on the stack, with
2131 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002132 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002133 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002134 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002135 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002136 llvm::SmallVector<Stmt *, 16> StmtStack;
2137 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002138 Stmt::EmptyShell Empty;
2139
Douglas Gregor087fd532009-04-14 23:32:43 +00002140 while (true) {
2141 unsigned Code = Stream.ReadCode();
2142 if (Code == llvm::bitc::END_BLOCK) {
2143 if (Stream.ReadBlockEnd()) {
2144 Error("Error at end of Source Manager block");
2145 return 0;
2146 }
2147 break;
2148 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002149
Douglas Gregor087fd532009-04-14 23:32:43 +00002150 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2151 // No known subblocks, always skip them.
2152 Stream.ReadSubBlockID();
2153 if (Stream.SkipBlock()) {
2154 Error("Malformed block record");
2155 return 0;
2156 }
2157 continue;
2158 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002159
Douglas Gregor087fd532009-04-14 23:32:43 +00002160 if (Code == llvm::bitc::DEFINE_ABBREV) {
2161 Stream.ReadAbbrevRecord();
2162 continue;
2163 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002164
Douglas Gregorc9490c02009-04-16 22:23:12 +00002165 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002166 Idx = 0;
2167 Record.clear();
2168 bool Finished = false;
2169 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002170 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002171 Finished = true;
2172 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002173
Douglas Gregorc9490c02009-04-16 22:23:12 +00002174 case pch::STMT_NULL_PTR:
2175 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002176 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002177
Douglas Gregor025452f2009-04-17 00:04:06 +00002178 case pch::STMT_NULL:
2179 S = new (Context) NullStmt(Empty);
2180 break;
2181
2182 case pch::STMT_COMPOUND:
2183 S = new (Context) CompoundStmt(Empty);
2184 break;
2185
2186 case pch::STMT_CASE:
2187 S = new (Context) CaseStmt(Empty);
2188 break;
2189
2190 case pch::STMT_DEFAULT:
2191 S = new (Context) DefaultStmt(Empty);
2192 break;
2193
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002194 case pch::STMT_LABEL:
2195 S = new (Context) LabelStmt(Empty);
2196 break;
2197
Douglas Gregor025452f2009-04-17 00:04:06 +00002198 case pch::STMT_IF:
2199 S = new (Context) IfStmt(Empty);
2200 break;
2201
2202 case pch::STMT_SWITCH:
2203 S = new (Context) SwitchStmt(Empty);
2204 break;
2205
Douglas Gregord921cf92009-04-17 00:16:09 +00002206 case pch::STMT_WHILE:
2207 S = new (Context) WhileStmt(Empty);
2208 break;
2209
Douglas Gregor67d82492009-04-17 00:29:51 +00002210 case pch::STMT_DO:
2211 S = new (Context) DoStmt(Empty);
2212 break;
2213
2214 case pch::STMT_FOR:
2215 S = new (Context) ForStmt(Empty);
2216 break;
2217
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002218 case pch::STMT_GOTO:
2219 S = new (Context) GotoStmt(Empty);
2220 break;
2221
Douglas Gregord921cf92009-04-17 00:16:09 +00002222 case pch::STMT_CONTINUE:
2223 S = new (Context) ContinueStmt(Empty);
2224 break;
2225
Douglas Gregor025452f2009-04-17 00:04:06 +00002226 case pch::STMT_BREAK:
2227 S = new (Context) BreakStmt(Empty);
2228 break;
2229
Douglas Gregor0de9d882009-04-17 16:34:57 +00002230 case pch::STMT_RETURN:
2231 S = new (Context) ReturnStmt(Empty);
2232 break;
2233
Douglas Gregor84f21702009-04-17 16:55:36 +00002234 case pch::STMT_DECL:
2235 S = new (Context) DeclStmt(Empty);
2236 break;
2237
Douglas Gregor087fd532009-04-14 23:32:43 +00002238 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002239 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002240 break;
2241
2242 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002243 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002244 break;
2245
2246 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002247 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002248 break;
2249
2250 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002251 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002252 break;
2253
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002254 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002255 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002256 break;
2257
Douglas Gregor673ecd62009-04-15 16:35:07 +00002258 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002259 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002260 Record[PCHStmtReader::NumExprFields + 1]);
2261 break;
2262
Douglas Gregor087fd532009-04-14 23:32:43 +00002263 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002264 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002265 break;
2266
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002267 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002268 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002269 break;
2270
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002271 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002272 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002273 break;
2274
2275 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002276 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002277 break;
2278
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002279 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002280 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002281 break;
2282
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002283 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002284 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002285 break;
2286
2287 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002288 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002289 break;
2290
Douglas Gregordb600c32009-04-15 00:25:59 +00002291 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002292 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002293 break;
2294
Douglas Gregorad90e962009-04-15 22:40:36 +00002295 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002296 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002297 break;
2298
2299 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002300 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002301 break;
2302
Douglas Gregor087fd532009-04-14 23:32:43 +00002303 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002304 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002305 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002306
2307 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002308 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002309 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002310
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002311 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002312 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002313 break;
2314
Douglas Gregord3c98a02009-04-15 23:02:49 +00002315 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002316 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002317 break;
2318
Douglas Gregord077d752009-04-16 00:55:48 +00002319 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002320 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002321 break;
2322
2323 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002324 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002325 Record[PCHStmtReader::NumExprFields] - 1);
2326
2327 break;
2328
2329 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002330 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002331 break;
2332
Douglas Gregord3c98a02009-04-15 23:02:49 +00002333 case pch::EXPR_VA_ARG:
2334 // FIXME: untested; we need function bodies first
Douglas Gregorc9490c02009-04-16 22:23:12 +00002335 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002336 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002337
2338 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002339 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002340 break;
2341
2342 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002343 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002344 break;
2345
2346 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002347 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002348 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002349
2350 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002351 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002352 break;
2353
2354 case pch::EXPR_BLOCK_DECL_REF:
2355 // FIXME: untested until we have statement and block support
Douglas Gregorc9490c02009-04-16 22:23:12 +00002356 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002357 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002358 }
2359
Douglas Gregorc9490c02009-04-16 22:23:12 +00002360 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002361 if (Finished)
2362 break;
2363
Douglas Gregorc9490c02009-04-16 22:23:12 +00002364 if (S) {
2365 unsigned NumSubStmts = Reader.Visit(S);
2366 while (NumSubStmts > 0) {
2367 StmtStack.pop_back();
2368 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002369 }
2370 }
2371
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002372 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002373 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002374 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002375 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00002376 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002377 return StmtStack.back();
2378}
2379
2380Expr *PCHReader::ReadExpr() {
2381 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002382}
2383
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002384DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002385 return Diag(SourceLocation(), DiagID);
2386}
2387
2388DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2389 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002390 Context.getSourceManager()),
2391 DiagID);
2392}
Douglas Gregor025452f2009-04-17 00:04:06 +00002393
2394/// \brief Record that the given ID maps to the given switch-case
2395/// statement.
2396void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2397 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2398 SwitchCaseStmts[ID] = SC;
2399}
2400
2401/// \brief Retrieve the switch-case statement with the given ID.
2402SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2403 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2404 return SwitchCaseStmts[ID];
2405}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002406
2407/// \brief Record that the given label statement has been
2408/// deserialized and has the given ID.
2409void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2410 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2411 "Deserialized label twice");
2412 LabelStmts[ID] = S;
2413
2414 // If we've already seen any goto statements that point to this
2415 // label, resolve them now.
2416 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2417 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2418 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2419 Goto->second->setLabel(S);
2420 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
2421}
2422
2423/// \brief Set the label of the given statement to the label
2424/// identified by ID.
2425///
2426/// Depending on the order in which the label and other statements
2427/// referencing that label occur, this operation may complete
2428/// immediately (updating the statement) or it may queue the
2429/// statement to be back-patched later.
2430void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2431 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2432 if (Label != LabelStmts.end()) {
2433 // We've already seen this label, so set the label of the goto and
2434 // we're done.
2435 S->setLabel(Label->second);
2436 } else {
2437 // We haven't seen this label yet, so add this goto to the set of
2438 // unresolved goto statements.
2439 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2440 }
2441}