blob: 2e8e7dd287d5d45157e2c755d6f3d621ce5e816a [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000107}
108
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor982365e2009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregorc34897d2009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor982365e2009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000167}
168
Douglas Gregorc34897d2009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000180}
181
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor2a491792009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000200 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000201 unsigned NumParams = Record[Idx++];
202 llvm::SmallVector<ParmVarDecl *, 16> Params;
203 Params.reserve(NumParams);
204 for (unsigned I = 0; I != NumParams; ++I)
205 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
206 BD->setParams(Reader.getContext(), &Params[0], NumParams);
207}
208
Douglas Gregorc34897d2009-04-09 22:27:44 +0000209std::pair<uint64_t, uint64_t>
210PCHDeclReader::VisitDeclContext(DeclContext *DC) {
211 uint64_t LexicalOffset = Record[Idx++];
212 uint64_t VisibleOffset = 0;
213 if (DC->getPrimaryContext() == DC)
214 VisibleOffset = Record[Idx++];
215 return std::make_pair(LexicalOffset, VisibleOffset);
216}
217
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000218//===----------------------------------------------------------------------===//
219// Statement/expression deserialization
220//===----------------------------------------------------------------------===//
221namespace {
222 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000223 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000224 PCHReader &Reader;
225 const PCHReader::RecordData &Record;
226 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000227 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000228
229 public:
230 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000231 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
232 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000233
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000234 /// \brief The number of record fields required for the Stmt class
235 /// itself.
236 static const unsigned NumStmtFields = 0;
237
Douglas Gregor596e0932009-04-15 16:35:07 +0000238 /// \brief The number of record fields required for the Expr class
239 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000240 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000241
Douglas Gregora151ba42009-04-14 23:32:43 +0000242 // Each of the Visit* functions reads in part of the expression
243 // from the given record and the current expression stack, then
244 // return the total number of operands that it read from the
245 // expression stack.
246
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000247 unsigned VisitStmt(Stmt *S);
248 unsigned VisitNullStmt(NullStmt *S);
249 unsigned VisitCompoundStmt(CompoundStmt *S);
250 unsigned VisitSwitchCase(SwitchCase *S);
251 unsigned VisitCaseStmt(CaseStmt *S);
252 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000253 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000254 unsigned VisitIfStmt(IfStmt *S);
255 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000256 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000257 unsigned VisitDoStmt(DoStmt *S);
258 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000259 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000260 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000261 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000262 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000263 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000264 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000265 unsigned VisitExpr(Expr *E);
266 unsigned VisitPredefinedExpr(PredefinedExpr *E);
267 unsigned VisitDeclRefExpr(DeclRefExpr *E);
268 unsigned VisitIntegerLiteral(IntegerLiteral *E);
269 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000270 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000271 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000272 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000273 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000274 unsigned VisitUnaryOperator(UnaryOperator *E);
275 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000276 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000277 unsigned VisitCallExpr(CallExpr *E);
278 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000279 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000280 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000281 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
282 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000283 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000284 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
285 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000286 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000287 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000288 unsigned VisitInitListExpr(InitListExpr *E);
289 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
290 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000291 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000292 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000293 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000294 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
295 unsigned VisitChooseExpr(ChooseExpr *E);
296 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000297 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000298 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000299 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000300 };
301}
302
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000303unsigned PCHStmtReader::VisitStmt(Stmt *S) {
304 assert(Idx == NumStmtFields && "Incorrect statement field count");
305 return 0;
306}
307
308unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
309 VisitStmt(S);
310 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
311 return 0;
312}
313
314unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
315 VisitStmt(S);
316 unsigned NumStmts = Record[Idx++];
317 S->setStmts(Reader.getContext(),
318 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
319 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
320 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
321 return NumStmts;
322}
323
324unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
325 VisitStmt(S);
326 Reader.RecordSwitchCaseID(S, Record[Idx++]);
327 return 0;
328}
329
330unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
331 VisitSwitchCase(S);
332 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
333 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
334 S->setSubStmt(StmtStack.back());
335 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
336 return 3;
337}
338
339unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
340 VisitSwitchCase(S);
341 S->setSubStmt(StmtStack.back());
342 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
343 return 1;
344}
345
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000346unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
347 VisitStmt(S);
348 S->setID(Reader.GetIdentifierInfo(Record, Idx));
349 S->setSubStmt(StmtStack.back());
350 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
351 Reader.RecordLabelStmt(S, Record[Idx++]);
352 return 1;
353}
354
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000355unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
356 VisitStmt(S);
357 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
358 S->setThen(StmtStack[StmtStack.size() - 2]);
359 S->setElse(StmtStack[StmtStack.size() - 1]);
360 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
361 return 3;
362}
363
364unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
365 VisitStmt(S);
366 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
367 S->setBody(StmtStack.back());
368 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369 SwitchCase *PrevSC = 0;
370 for (unsigned N = Record.size(); Idx != N; ++Idx) {
371 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
372 if (PrevSC)
373 PrevSC->setNextSwitchCase(SC);
374 else
375 S->setSwitchCaseList(SC);
376 PrevSC = SC;
377 }
378 return 2;
379}
380
Douglas Gregora6b503f2009-04-17 00:16:09 +0000381unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
382 VisitStmt(S);
383 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
384 S->setBody(StmtStack.back());
385 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
386 return 2;
387}
388
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000389unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
390 VisitStmt(S);
391 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
392 S->setBody(StmtStack.back());
393 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
394 return 2;
395}
396
397unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
398 VisitStmt(S);
399 S->setInit(StmtStack[StmtStack.size() - 4]);
400 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
401 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
402 S->setBody(StmtStack.back());
403 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
404 return 4;
405}
406
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000407unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
408 VisitStmt(S);
409 Reader.SetLabelOf(S, Record[Idx++]);
410 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
411 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
412 return 0;
413}
414
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000415unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
416 VisitStmt(S);
417 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
418 return 1;
419}
420
Douglas Gregora6b503f2009-04-17 00:16:09 +0000421unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
422 VisitStmt(S);
423 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
424 return 0;
425}
426
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000427unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
428 VisitStmt(S);
429 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
430 return 0;
431}
432
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000433unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
434 VisitStmt(S);
435 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
436 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
437 return 1;
438}
439
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000440unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
441 VisitStmt(S);
442 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
443 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
444
445 if (Idx + 1 == Record.size()) {
446 // Single declaration
447 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
448 } else {
449 llvm::SmallVector<Decl *, 16> Decls;
450 Decls.reserve(Record.size() - Idx);
451 for (unsigned N = Record.size(); Idx != N; ++Idx)
452 Decls.push_back(Reader.GetDecl(Record[Idx]));
453 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
454 &Decls[0], Decls.size())));
455 }
456 return 0;
457}
458
Douglas Gregora151ba42009-04-14 23:32:43 +0000459unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000460 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000461 E->setType(Reader.GetType(Record[Idx++]));
462 E->setTypeDependent(Record[Idx++]);
463 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000464 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000465 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000466}
467
Douglas Gregora151ba42009-04-14 23:32:43 +0000468unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000469 VisitExpr(E);
470 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
471 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000472 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000473}
474
Douglas Gregora151ba42009-04-14 23:32:43 +0000475unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000476 VisitExpr(E);
477 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
478 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000479 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000480}
481
Douglas Gregora151ba42009-04-14 23:32:43 +0000482unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000483 VisitExpr(E);
484 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
485 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000486 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000487}
488
Douglas Gregora151ba42009-04-14 23:32:43 +0000489unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000490 VisitExpr(E);
491 E->setValue(Reader.ReadAPFloat(Record, Idx));
492 E->setExact(Record[Idx++]);
493 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000494 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000495}
496
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000497unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
498 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000499 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000500 return 1;
501}
502
Douglas Gregor596e0932009-04-15 16:35:07 +0000503unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
504 VisitExpr(E);
505 unsigned Len = Record[Idx++];
506 assert(Record[Idx] == E->getNumConcatenated() &&
507 "Wrong number of concatenated tokens!");
508 ++Idx;
509 E->setWide(Record[Idx++]);
510
511 // Read string data
512 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
513 E->setStrData(Reader.getContext(), &Str[0], Len);
514 Idx += Len;
515
516 // Read source locations
517 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
518 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
519
520 return 0;
521}
522
Douglas Gregora151ba42009-04-14 23:32:43 +0000523unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000524 VisitExpr(E);
525 E->setValue(Record[Idx++]);
526 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
527 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000528 return 0;
529}
530
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000531unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
532 VisitExpr(E);
533 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
534 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000535 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000536 return 1;
537}
538
Douglas Gregor12d74052009-04-15 15:58:59 +0000539unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
540 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000541 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000542 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
543 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
544 return 1;
545}
546
547unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
548 VisitExpr(E);
549 E->setSizeof(Record[Idx++]);
550 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000551 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000552 ++Idx;
553 } else {
554 E->setArgument(Reader.GetType(Record[Idx++]));
555 }
556 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
557 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
558 return E->isArgumentType()? 0 : 1;
559}
560
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000561unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
562 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000563 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
564 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000565 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 return 2;
567}
568
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000569unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
570 VisitExpr(E);
571 E->setNumArgs(Reader.getContext(), Record[Idx++]);
572 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000573 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000574 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000575 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000576 return E->getNumArgs() + 1;
577}
578
579unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
580 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000581 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000582 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
583 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
584 E->setArrow(Record[Idx++]);
585 return 1;
586}
587
Douglas Gregora151ba42009-04-14 23:32:43 +0000588unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
589 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000590 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000591 return 1;
592}
593
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000594unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
595 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000596 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
597 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000598 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
599 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
600 return 2;
601}
602
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000603unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
604 VisitBinaryOperator(E);
605 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
606 E->setComputationResultType(Reader.GetType(Record[Idx++]));
607 return 2;
608}
609
610unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
611 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000612 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
613 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
614 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000615 return 3;
616}
617
Douglas Gregora151ba42009-04-14 23:32:43 +0000618unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
619 VisitCastExpr(E);
620 E->setLvalueCast(Record[Idx++]);
621 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000622}
623
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000624unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
625 VisitCastExpr(E);
626 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
627 return 1;
628}
629
630unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
631 VisitExplicitCastExpr(E);
632 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
633 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
634 return 1;
635}
636
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000637unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
638 VisitExpr(E);
639 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000640 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000641 E->setFileScope(Record[Idx++]);
642 return 1;
643}
644
Douglas Gregorec0b8292009-04-15 23:02:49 +0000645unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
646 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000647 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000648 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
649 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
650 return 1;
651}
652
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000653unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
654 VisitExpr(E);
655 unsigned NumInits = Record[Idx++];
656 E->reserveInits(NumInits);
657 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000658 E->updateInit(I,
659 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
660 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000661 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
662 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
663 E->setInitializedFieldInUnion(
664 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
665 E->sawArrayRangeDesignator(Record[Idx++]);
666 return NumInits + 1;
667}
668
669unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
670 typedef DesignatedInitExpr::Designator Designator;
671
672 VisitExpr(E);
673 unsigned NumSubExprs = Record[Idx++];
674 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
675 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000676 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000677 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
678 E->setGNUSyntax(Record[Idx++]);
679
680 llvm::SmallVector<Designator, 4> Designators;
681 while (Idx < Record.size()) {
682 switch ((pch::DesignatorTypes)Record[Idx++]) {
683 case pch::DESIG_FIELD_DECL: {
684 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
685 SourceLocation DotLoc
686 = SourceLocation::getFromRawEncoding(Record[Idx++]);
687 SourceLocation FieldLoc
688 = SourceLocation::getFromRawEncoding(Record[Idx++]);
689 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
690 FieldLoc));
691 Designators.back().setField(Field);
692 break;
693 }
694
695 case pch::DESIG_FIELD_NAME: {
696 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
697 SourceLocation DotLoc
698 = SourceLocation::getFromRawEncoding(Record[Idx++]);
699 SourceLocation FieldLoc
700 = SourceLocation::getFromRawEncoding(Record[Idx++]);
701 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
702 break;
703 }
704
705 case pch::DESIG_ARRAY: {
706 unsigned Index = Record[Idx++];
707 SourceLocation LBracketLoc
708 = SourceLocation::getFromRawEncoding(Record[Idx++]);
709 SourceLocation RBracketLoc
710 = SourceLocation::getFromRawEncoding(Record[Idx++]);
711 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
712 break;
713 }
714
715 case pch::DESIG_ARRAY_RANGE: {
716 unsigned Index = Record[Idx++];
717 SourceLocation LBracketLoc
718 = SourceLocation::getFromRawEncoding(Record[Idx++]);
719 SourceLocation EllipsisLoc
720 = SourceLocation::getFromRawEncoding(Record[Idx++]);
721 SourceLocation RBracketLoc
722 = SourceLocation::getFromRawEncoding(Record[Idx++]);
723 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
724 RBracketLoc));
725 break;
726 }
727 }
728 }
729 E->setDesignators(&Designators[0], Designators.size());
730
731 return NumSubExprs;
732}
733
734unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
735 VisitExpr(E);
736 return 0;
737}
738
Douglas Gregorec0b8292009-04-15 23:02:49 +0000739unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
740 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000741 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000742 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
743 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
744 return 1;
745}
746
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000747unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
748 VisitExpr(E);
749 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
750 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
751 Reader.SetLabelOf(E, Record[Idx++]);
752 return 0;
753}
754
Douglas Gregoreca12f62009-04-17 19:05:30 +0000755unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
756 VisitExpr(E);
757 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
758 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
759 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
760 return 1;
761}
762
Douglas Gregor209d4622009-04-15 23:33:31 +0000763unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
764 VisitExpr(E);
765 E->setArgType1(Reader.GetType(Record[Idx++]));
766 E->setArgType2(Reader.GetType(Record[Idx++]));
767 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
768 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
769 return 0;
770}
771
772unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
773 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000774 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
775 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
776 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000777 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
778 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
779 return 3;
780}
781
782unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
783 VisitExpr(E);
784 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
785 return 0;
786}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000787
Douglas Gregor725e94b2009-04-16 00:01:45 +0000788unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
789 VisitExpr(E);
790 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000791 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000792 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
793 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
794 return NumExprs;
795}
796
Douglas Gregore246b742009-04-17 19:21:43 +0000797unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
798 VisitExpr(E);
799 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
800 E->setHasBlockDeclRefExprs(Record[Idx++]);
801 return 0;
802}
803
Douglas Gregor725e94b2009-04-16 00:01:45 +0000804unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
805 VisitExpr(E);
806 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
807 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
808 E->setByRef(Record[Idx++]);
809 return 0;
810}
811
Douglas Gregorc34897d2009-04-09 22:27:44 +0000812// FIXME: use the diagnostics machinery
813static bool Error(const char *Str) {
814 std::fprintf(stderr, "%s\n", Str);
815 return true;
816}
817
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000818/// \brief Check the contents of the predefines buffer against the
819/// contents of the predefines buffer used to build the PCH file.
820///
821/// The contents of the two predefines buffers should be the same. If
822/// not, then some command-line option changed the preprocessor state
823/// and we must reject the PCH file.
824///
825/// \param PCHPredef The start of the predefines buffer in the PCH
826/// file.
827///
828/// \param PCHPredefLen The length of the predefines buffer in the PCH
829/// file.
830///
831/// \param PCHBufferID The FileID for the PCH predefines buffer.
832///
833/// \returns true if there was a mismatch (in which case the PCH file
834/// should be ignored), or false otherwise.
835bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
836 unsigned PCHPredefLen,
837 FileID PCHBufferID) {
838 const char *Predef = PP.getPredefines().c_str();
839 unsigned PredefLen = PP.getPredefines().size();
840
841 // If the two predefines buffers compare equal, we're done!.
842 if (PredefLen == PCHPredefLen &&
843 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
844 return false;
845
846 // The predefines buffers are different. Produce a reasonable
847 // diagnostic showing where they are different.
848
849 // The source locations (potentially in the two different predefines
850 // buffers)
851 SourceLocation Loc1, Loc2;
852 SourceManager &SourceMgr = PP.getSourceManager();
853
854 // Create a source buffer for our predefines string, so
855 // that we can build a diagnostic that points into that
856 // source buffer.
857 FileID BufferID;
858 if (Predef && Predef[0]) {
859 llvm::MemoryBuffer *Buffer
860 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
861 "<built-in>");
862 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
863 }
864
865 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
866 std::pair<const char *, const char *> Locations
867 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
868
869 if (Locations.first != Predef + MinLen) {
870 // We found the location in the two buffers where there is a
871 // difference. Form source locations to point there (in both
872 // buffers).
873 unsigned Offset = Locations.first - Predef;
874 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
875 .getFileLocWithOffset(Offset);
876 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
877 .getFileLocWithOffset(Offset);
878 } else if (PredefLen > PCHPredefLen) {
879 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
880 .getFileLocWithOffset(MinLen);
881 } else {
882 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
883 .getFileLocWithOffset(MinLen);
884 }
885
886 Diag(Loc1, diag::warn_pch_preprocessor);
887 if (Loc2.isValid())
888 Diag(Loc2, diag::note_predef_in_pch);
889 Diag(diag::note_ignoring_pch) << FileName;
890 return true;
891}
892
Douglas Gregor635f97f2009-04-13 16:31:14 +0000893/// \brief Read the line table in the source manager block.
894/// \returns true if ther was an error.
895static bool ParseLineTable(SourceManager &SourceMgr,
896 llvm::SmallVectorImpl<uint64_t> &Record) {
897 unsigned Idx = 0;
898 LineTableInfo &LineTable = SourceMgr.getLineTable();
899
900 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000901 std::map<int, int> FileIDs;
902 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000903 // Extract the file name
904 unsigned FilenameLen = Record[Idx++];
905 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
906 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000907 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
908 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000909 }
910
911 // Parse the line entries
912 std::vector<LineEntry> Entries;
913 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000914 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000915
916 // Extract the line entries
917 unsigned NumEntries = Record[Idx++];
918 Entries.clear();
919 Entries.reserve(NumEntries);
920 for (unsigned I = 0; I != NumEntries; ++I) {
921 unsigned FileOffset = Record[Idx++];
922 unsigned LineNo = Record[Idx++];
923 int FilenameID = Record[Idx++];
924 SrcMgr::CharacteristicKind FileKind
925 = (SrcMgr::CharacteristicKind)Record[Idx++];
926 unsigned IncludeOffset = Record[Idx++];
927 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
928 FileKind, IncludeOffset));
929 }
930 LineTable.AddEntry(FID, Entries);
931 }
932
933 return false;
934}
935
Douglas Gregorab1cef72009-04-10 03:52:48 +0000936/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000937PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000938 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000939 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
940 Error("Malformed source manager block record");
941 return Failure;
942 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000943
944 SourceManager &SourceMgr = Context.getSourceManager();
945 RecordData Record;
946 while (true) {
947 unsigned Code = Stream.ReadCode();
948 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000949 if (Stream.ReadBlockEnd()) {
950 Error("Error at end of Source Manager block");
951 return Failure;
952 }
953
954 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000955 }
956
957 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
958 // No known subblocks, always skip them.
959 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000960 if (Stream.SkipBlock()) {
961 Error("Malformed block record");
962 return Failure;
963 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000964 continue;
965 }
966
967 if (Code == llvm::bitc::DEFINE_ABBREV) {
968 Stream.ReadAbbrevRecord();
969 continue;
970 }
971
972 // Read a record.
973 const char *BlobStart;
974 unsigned BlobLen;
975 Record.clear();
976 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
977 default: // Default behavior: ignore.
978 break;
979
980 case pch::SM_SLOC_FILE_ENTRY: {
981 // FIXME: We would really like to delay the creation of this
982 // FileEntry until it is actually required, e.g., when producing
983 // a diagnostic with a source location in this file.
984 const FileEntry *File
985 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
986 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000987 FileID ID = SourceMgr.createFileID(File,
988 SourceLocation::getFromRawEncoding(Record[1]),
989 (CharacteristicKind)Record[2]);
990 if (Record[3])
991 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
992 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000993 break;
994 }
995
996 case pch::SM_SLOC_BUFFER_ENTRY: {
997 const char *Name = BlobStart;
998 unsigned Code = Stream.ReadCode();
999 Record.clear();
1000 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1001 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001002 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001003 llvm::MemoryBuffer *Buffer
1004 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1005 BlobStart + BlobLen - 1,
1006 Name);
1007 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1008
1009 if (strcmp(Name, "<built-in>") == 0
1010 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1011 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001012 break;
1013 }
1014
1015 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1016 SourceLocation SpellingLoc
1017 = SourceLocation::getFromRawEncoding(Record[1]);
1018 SourceMgr.createInstantiationLoc(
1019 SpellingLoc,
1020 SourceLocation::getFromRawEncoding(Record[2]),
1021 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001022 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001023 break;
1024 }
1025
Chris Lattnere1be6022009-04-14 23:22:57 +00001026 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001027 if (ParseLineTable(SourceMgr, Record))
1028 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001029 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001030 }
1031 }
1032}
1033
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001034bool PCHReader::ReadPreprocessorBlock() {
1035 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1036 return Error("Malformed preprocessor block record");
1037
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001038 RecordData Record;
1039 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1040 MacroInfo *LastMacro = 0;
1041
1042 while (true) {
1043 unsigned Code = Stream.ReadCode();
1044 switch (Code) {
1045 case llvm::bitc::END_BLOCK:
1046 if (Stream.ReadBlockEnd())
1047 return Error("Error at end of preprocessor block");
1048 return false;
1049
1050 case llvm::bitc::ENTER_SUBBLOCK:
1051 // No known subblocks, always skip them.
1052 Stream.ReadSubBlockID();
1053 if (Stream.SkipBlock())
1054 return Error("Malformed block record");
1055 continue;
1056
1057 case llvm::bitc::DEFINE_ABBREV:
1058 Stream.ReadAbbrevRecord();
1059 continue;
1060 default: break;
1061 }
1062
1063 // Read a record.
1064 Record.clear();
1065 pch::PreprocessorRecordTypes RecType =
1066 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1067 switch (RecType) {
1068 default: // Default behavior: ignore unknown records.
1069 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001070 case pch::PP_COUNTER_VALUE:
1071 if (!Record.empty())
1072 PP.setCounterValue(Record[0]);
1073 break;
1074
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001075 case pch::PP_MACRO_OBJECT_LIKE:
1076 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001077 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1078 if (II == 0)
1079 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001080 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1081 bool isUsed = Record[2];
1082
1083 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1084 MI->setIsUsed(isUsed);
1085
1086 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1087 // Decode function-like macro info.
1088 bool isC99VarArgs = Record[3];
1089 bool isGNUVarArgs = Record[4];
1090 MacroArgs.clear();
1091 unsigned NumArgs = Record[5];
1092 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001093 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001094
1095 // Install function-like macro info.
1096 MI->setIsFunctionLike();
1097 if (isC99VarArgs) MI->setIsC99Varargs();
1098 if (isGNUVarArgs) MI->setIsGNUVarargs();
1099 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1100 PP.getPreprocessorAllocator());
1101 }
1102
1103 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001104 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001105
1106 // Remember that we saw this macro last so that we add the tokens that
1107 // form its body to it.
1108 LastMacro = MI;
1109 break;
1110 }
1111
1112 case pch::PP_TOKEN: {
1113 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1114 // pretend we didn't see this.
1115 if (LastMacro == 0) break;
1116
1117 Token Tok;
1118 Tok.startToken();
1119 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1120 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001121 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1122 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001123 Tok.setKind((tok::TokenKind)Record[3]);
1124 Tok.setFlag((Token::TokenFlags)Record[4]);
1125 LastMacro->AddTokenToBody(Tok);
1126 break;
1127 }
1128 }
1129 }
1130}
1131
Douglas Gregor179cfb12009-04-10 20:39:37 +00001132PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1133 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1134 Error("Malformed block record");
1135 return Failure;
1136 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001137
Chris Lattner29241862009-04-11 21:15:38 +00001138 uint64_t PreprocessorBlockBit = 0;
1139
Douglas Gregorc34897d2009-04-09 22:27:44 +00001140 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001141 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001142 while (!Stream.AtEndOfStream()) {
1143 unsigned Code = Stream.ReadCode();
1144 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001145 // If we saw the preprocessor block, read it now.
1146 if (PreprocessorBlockBit) {
1147 uint64_t SavedPos = Stream.GetCurrentBitNo();
1148 Stream.JumpToBit(PreprocessorBlockBit);
1149 if (ReadPreprocessorBlock()) {
1150 Error("Malformed preprocessor block");
1151 return Failure;
1152 }
1153 Stream.JumpToBit(SavedPos);
1154 }
1155
Douglas Gregor179cfb12009-04-10 20:39:37 +00001156 if (Stream.ReadBlockEnd()) {
1157 Error("Error at end of module block");
1158 return Failure;
1159 }
Chris Lattner29241862009-04-11 21:15:38 +00001160
Douglas Gregor179cfb12009-04-10 20:39:37 +00001161 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001162 }
1163
1164 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1165 switch (Stream.ReadSubBlockID()) {
1166 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1167 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1168 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001169 if (Stream.SkipBlock()) {
1170 Error("Malformed block record");
1171 return Failure;
1172 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001173 break;
1174
Chris Lattner29241862009-04-11 21:15:38 +00001175 case pch::PREPROCESSOR_BLOCK_ID:
1176 // Skip the preprocessor block for now, but remember where it is. We
1177 // want to read it in after the identifier table.
1178 if (PreprocessorBlockBit) {
1179 Error("Multiple preprocessor blocks found.");
1180 return Failure;
1181 }
1182 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1183 if (Stream.SkipBlock()) {
1184 Error("Malformed block record");
1185 return Failure;
1186 }
1187 break;
1188
Douglas Gregorab1cef72009-04-10 03:52:48 +00001189 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001190 switch (ReadSourceManagerBlock()) {
1191 case Success:
1192 break;
1193
1194 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001195 Error("Malformed source manager block");
1196 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001197
1198 case IgnorePCH:
1199 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001200 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001201 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001202 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001203 continue;
1204 }
1205
1206 if (Code == llvm::bitc::DEFINE_ABBREV) {
1207 Stream.ReadAbbrevRecord();
1208 continue;
1209 }
1210
1211 // Read and process a record.
1212 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001213 const char *BlobStart = 0;
1214 unsigned BlobLen = 0;
1215 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1216 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001217 default: // Default behavior: ignore.
1218 break;
1219
1220 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001221 if (!TypeOffsets.empty()) {
1222 Error("Duplicate TYPE_OFFSET record in PCH file");
1223 return Failure;
1224 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001225 TypeOffsets.swap(Record);
1226 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1227 break;
1228
1229 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001230 if (!DeclOffsets.empty()) {
1231 Error("Duplicate DECL_OFFSET record in PCH file");
1232 return Failure;
1233 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001234 DeclOffsets.swap(Record);
1235 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1236 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001237
1238 case pch::LANGUAGE_OPTIONS:
1239 if (ParseLanguageOptions(Record))
1240 return IgnorePCH;
1241 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001242
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001243 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001244 std::string TargetTriple(BlobStart, BlobLen);
1245 if (TargetTriple != Context.Target.getTargetTriple()) {
1246 Diag(diag::warn_pch_target_triple)
1247 << TargetTriple << Context.Target.getTargetTriple();
1248 Diag(diag::note_ignoring_pch) << FileName;
1249 return IgnorePCH;
1250 }
1251 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001252 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001253
1254 case pch::IDENTIFIER_TABLE:
1255 IdentifierTable = BlobStart;
1256 break;
1257
1258 case pch::IDENTIFIER_OFFSET:
1259 if (!IdentifierData.empty()) {
1260 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1261 return Failure;
1262 }
1263 IdentifierData.swap(Record);
1264#ifndef NDEBUG
1265 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1266 if ((IdentifierData[I] & 0x01) == 0) {
1267 Error("Malformed identifier table in the precompiled header");
1268 return Failure;
1269 }
1270 }
1271#endif
1272 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001273
1274 case pch::EXTERNAL_DEFINITIONS:
1275 if (!ExternalDefinitions.empty()) {
1276 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1277 return Failure;
1278 }
1279 ExternalDefinitions.swap(Record);
1280 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001281 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001282 }
1283
Douglas Gregor179cfb12009-04-10 20:39:37 +00001284 Error("Premature end of bitstream");
1285 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001286}
1287
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001288PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001289 // Set the PCH file name.
1290 this->FileName = FileName;
1291
Douglas Gregorc34897d2009-04-09 22:27:44 +00001292 // Open the PCH file.
1293 std::string ErrStr;
1294 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001295 if (!Buffer) {
1296 Error(ErrStr.c_str());
1297 return IgnorePCH;
1298 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001299
1300 // Initialize the stream
1301 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1302 (const unsigned char *)Buffer->getBufferEnd());
1303
1304 // Sniff for the signature.
1305 if (Stream.Read(8) != 'C' ||
1306 Stream.Read(8) != 'P' ||
1307 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001308 Stream.Read(8) != 'H') {
1309 Error("Not a PCH file");
1310 return IgnorePCH;
1311 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001312
1313 // We expect a number of well-defined blocks, though we don't necessarily
1314 // need to understand them all.
1315 while (!Stream.AtEndOfStream()) {
1316 unsigned Code = Stream.ReadCode();
1317
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001318 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1319 Error("Invalid record at top-level");
1320 return Failure;
1321 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001322
1323 unsigned BlockID = Stream.ReadSubBlockID();
1324
1325 // We only know the PCH subblock ID.
1326 switch (BlockID) {
1327 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001328 if (Stream.ReadBlockInfoBlock()) {
1329 Error("Malformed BlockInfoBlock");
1330 return Failure;
1331 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001332 break;
1333 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001334 switch (ReadPCHBlock()) {
1335 case Success:
1336 break;
1337
1338 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001339 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001340
1341 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001342 // FIXME: We could consider reading through to the end of this
1343 // PCH block, skipping subblocks, to see if there are other
1344 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001345 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001346 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001347 break;
1348 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001349 if (Stream.SkipBlock()) {
1350 Error("Malformed block record");
1351 return Failure;
1352 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001353 break;
1354 }
1355 }
1356
1357 // Load the translation unit declaration
1358 ReadDeclRecord(DeclOffsets[0], 0);
1359
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001360 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001361}
1362
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001363namespace {
1364 /// \brief Helper class that saves the current stream position and
1365 /// then restores it when destroyed.
1366 struct VISIBILITY_HIDDEN SavedStreamPosition {
1367 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001368 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001369
1370 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001371 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001372 }
1373
1374 private:
1375 llvm::BitstreamReader &Stream;
1376 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001377 };
1378}
1379
Douglas Gregor179cfb12009-04-10 20:39:37 +00001380/// \brief Parse the record that corresponds to a LangOptions data
1381/// structure.
1382///
1383/// This routine compares the language options used to generate the
1384/// PCH file against the language options set for the current
1385/// compilation. For each option, we classify differences between the
1386/// two compiler states as either "benign" or "important". Benign
1387/// differences don't matter, and we accept them without complaint
1388/// (and without modifying the language options). Differences between
1389/// the states for important options cause the PCH file to be
1390/// unusable, so we emit a warning and return true to indicate that
1391/// there was an error.
1392///
1393/// \returns true if the PCH file is unacceptable, false otherwise.
1394bool PCHReader::ParseLanguageOptions(
1395 const llvm::SmallVectorImpl<uint64_t> &Record) {
1396 const LangOptions &LangOpts = Context.getLangOptions();
1397#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1398#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1399 if (Record[Idx] != LangOpts.Option) { \
1400 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1401 Diag(diag::note_ignoring_pch) << FileName; \
1402 return true; \
1403 } \
1404 ++Idx
1405
1406 unsigned Idx = 0;
1407 PARSE_LANGOPT_BENIGN(Trigraphs);
1408 PARSE_LANGOPT_BENIGN(BCPLComment);
1409 PARSE_LANGOPT_BENIGN(DollarIdents);
1410 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1411 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1412 PARSE_LANGOPT_BENIGN(ImplicitInt);
1413 PARSE_LANGOPT_BENIGN(Digraphs);
1414 PARSE_LANGOPT_BENIGN(HexFloats);
1415 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1416 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1417 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1418 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1419 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1420 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1421 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1422 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1423 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1424 PARSE_LANGOPT_BENIGN(PascalStrings);
1425 PARSE_LANGOPT_BENIGN(Boolean);
1426 PARSE_LANGOPT_BENIGN(WritableStrings);
1427 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1428 diag::warn_pch_lax_vector_conversions);
1429 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1430 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1431 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1432 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1433 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1434 diag::warn_pch_thread_safe_statics);
1435 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1436 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1437 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1438 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1439 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1440 diag::warn_pch_heinous_extensions);
1441 // FIXME: Most of the options below are benign if the macro wasn't
1442 // used. Unfortunately, this means that a PCH compiled without
1443 // optimization can't be used with optimization turned on, even
1444 // though the only thing that changes is whether __OPTIMIZE__ was
1445 // defined... but if __OPTIMIZE__ never showed up in the header, it
1446 // doesn't matter. We could consider making this some special kind
1447 // of check.
1448 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1449 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1450 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1451 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1452 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1453 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1454 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1455 Diag(diag::warn_pch_gc_mode)
1456 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1457 Diag(diag::note_ignoring_pch) << FileName;
1458 return true;
1459 }
1460 ++Idx;
1461 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1462 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1463#undef PARSE_LANGOPT_IRRELEVANT
1464#undef PARSE_LANGOPT_BENIGN
1465
1466 return false;
1467}
1468
Douglas Gregorc34897d2009-04-09 22:27:44 +00001469/// \brief Read and return the type at the given offset.
1470///
1471/// This routine actually reads the record corresponding to the type
1472/// at the given offset in the bitstream. It is a helper routine for
1473/// GetType, which deals with reading type IDs.
1474QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001475 // Keep track of where we are in the stream, then jump back there
1476 // after reading this type.
1477 SavedStreamPosition SavedPosition(Stream);
1478
Douglas Gregorc34897d2009-04-09 22:27:44 +00001479 Stream.JumpToBit(Offset);
1480 RecordData Record;
1481 unsigned Code = Stream.ReadCode();
1482 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001483 case pch::TYPE_EXT_QUAL: {
1484 assert(Record.size() == 3 &&
1485 "Incorrect encoding of extended qualifier type");
1486 QualType Base = GetType(Record[0]);
1487 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1488 unsigned AddressSpace = Record[2];
1489
1490 QualType T = Base;
1491 if (GCAttr != QualType::GCNone)
1492 T = Context.getObjCGCQualType(T, GCAttr);
1493 if (AddressSpace)
1494 T = Context.getAddrSpaceQualType(T, AddressSpace);
1495 return T;
1496 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001497
Douglas Gregorc34897d2009-04-09 22:27:44 +00001498 case pch::TYPE_FIXED_WIDTH_INT: {
1499 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1500 return Context.getFixedWidthIntType(Record[0], Record[1]);
1501 }
1502
1503 case pch::TYPE_COMPLEX: {
1504 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1505 QualType ElemType = GetType(Record[0]);
1506 return Context.getComplexType(ElemType);
1507 }
1508
1509 case pch::TYPE_POINTER: {
1510 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1511 QualType PointeeType = GetType(Record[0]);
1512 return Context.getPointerType(PointeeType);
1513 }
1514
1515 case pch::TYPE_BLOCK_POINTER: {
1516 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1517 QualType PointeeType = GetType(Record[0]);
1518 return Context.getBlockPointerType(PointeeType);
1519 }
1520
1521 case pch::TYPE_LVALUE_REFERENCE: {
1522 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1523 QualType PointeeType = GetType(Record[0]);
1524 return Context.getLValueReferenceType(PointeeType);
1525 }
1526
1527 case pch::TYPE_RVALUE_REFERENCE: {
1528 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1529 QualType PointeeType = GetType(Record[0]);
1530 return Context.getRValueReferenceType(PointeeType);
1531 }
1532
1533 case pch::TYPE_MEMBER_POINTER: {
1534 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1535 QualType PointeeType = GetType(Record[0]);
1536 QualType ClassType = GetType(Record[1]);
1537 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1538 }
1539
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001540 case pch::TYPE_CONSTANT_ARRAY: {
1541 QualType ElementType = GetType(Record[0]);
1542 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1543 unsigned IndexTypeQuals = Record[2];
1544 unsigned Idx = 3;
1545 llvm::APInt Size = ReadAPInt(Record, Idx);
1546 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1547 }
1548
1549 case pch::TYPE_INCOMPLETE_ARRAY: {
1550 QualType ElementType = GetType(Record[0]);
1551 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1552 unsigned IndexTypeQuals = Record[2];
1553 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1554 }
1555
1556 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001557 QualType ElementType = GetType(Record[0]);
1558 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1559 unsigned IndexTypeQuals = Record[2];
1560 return Context.getVariableArrayType(ElementType, ReadExpr(),
1561 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001562 }
1563
1564 case pch::TYPE_VECTOR: {
1565 if (Record.size() != 2) {
1566 Error("Incorrect encoding of vector type in PCH file");
1567 return QualType();
1568 }
1569
1570 QualType ElementType = GetType(Record[0]);
1571 unsigned NumElements = Record[1];
1572 return Context.getVectorType(ElementType, NumElements);
1573 }
1574
1575 case pch::TYPE_EXT_VECTOR: {
1576 if (Record.size() != 2) {
1577 Error("Incorrect encoding of extended vector type in PCH file");
1578 return QualType();
1579 }
1580
1581 QualType ElementType = GetType(Record[0]);
1582 unsigned NumElements = Record[1];
1583 return Context.getExtVectorType(ElementType, NumElements);
1584 }
1585
1586 case pch::TYPE_FUNCTION_NO_PROTO: {
1587 if (Record.size() != 1) {
1588 Error("Incorrect encoding of no-proto function type");
1589 return QualType();
1590 }
1591 QualType ResultType = GetType(Record[0]);
1592 return Context.getFunctionNoProtoType(ResultType);
1593 }
1594
1595 case pch::TYPE_FUNCTION_PROTO: {
1596 QualType ResultType = GetType(Record[0]);
1597 unsigned Idx = 1;
1598 unsigned NumParams = Record[Idx++];
1599 llvm::SmallVector<QualType, 16> ParamTypes;
1600 for (unsigned I = 0; I != NumParams; ++I)
1601 ParamTypes.push_back(GetType(Record[Idx++]));
1602 bool isVariadic = Record[Idx++];
1603 unsigned Quals = Record[Idx++];
1604 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1605 isVariadic, Quals);
1606 }
1607
1608 case pch::TYPE_TYPEDEF:
1609 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1610 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1611
1612 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001613 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001614
1615 case pch::TYPE_TYPEOF: {
1616 if (Record.size() != 1) {
1617 Error("Incorrect encoding of typeof(type) in PCH file");
1618 return QualType();
1619 }
1620 QualType UnderlyingType = GetType(Record[0]);
1621 return Context.getTypeOfType(UnderlyingType);
1622 }
1623
1624 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001625 assert(Record.size() == 1 && "Incorrect encoding of record type");
1626 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001627
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001628 case pch::TYPE_ENUM:
1629 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1630 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1631
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001632 case pch::TYPE_OBJC_INTERFACE:
1633 // FIXME: Deserialize ObjCInterfaceType
1634 assert(false && "Cannot de-serialize ObjC interface types yet");
1635 return QualType();
1636
1637 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1638 // FIXME: Deserialize ObjCQualifiedInterfaceType
1639 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1640 return QualType();
1641
1642 case pch::TYPE_OBJC_QUALIFIED_ID:
1643 // FIXME: Deserialize ObjCQualifiedIdType
1644 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1645 return QualType();
1646
1647 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1648 // FIXME: Deserialize ObjCQualifiedClassType
1649 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1650 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001651 }
1652
1653 // Suppress a GCC warning
1654 return QualType();
1655}
1656
1657/// \brief Note that we have loaded the declaration with the given
1658/// Index.
1659///
1660/// This routine notes that this declaration has already been loaded,
1661/// so that future GetDecl calls will return this declaration rather
1662/// than trying to load a new declaration.
1663inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1664 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1665 DeclAlreadyLoaded[Index] = true;
1666 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1667}
1668
1669/// \brief Read the declaration at the given offset from the PCH file.
1670Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001671 // Keep track of where we are in the stream, then jump back there
1672 // after reading this declaration.
1673 SavedStreamPosition SavedPosition(Stream);
1674
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675 Decl *D = 0;
1676 Stream.JumpToBit(Offset);
1677 RecordData Record;
1678 unsigned Code = Stream.ReadCode();
1679 unsigned Idx = 0;
1680 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001681
Douglas Gregorc34897d2009-04-09 22:27:44 +00001682 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001683 case pch::DECL_ATTR:
1684 case pch::DECL_CONTEXT_LEXICAL:
1685 case pch::DECL_CONTEXT_VISIBLE:
1686 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1687 break;
1688
Douglas Gregorc34897d2009-04-09 22:27:44 +00001689 case pch::DECL_TRANSLATION_UNIT:
1690 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001691 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001692 break;
1693
1694 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001695 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001696 break;
1697 }
1698
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001699 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001700 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001701 break;
1702 }
1703
Douglas Gregor982365e2009-04-13 21:20:57 +00001704 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001705 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1706 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001707 break;
1708 }
1709
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001710 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001711 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1712 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001713 break;
1714 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001715
1716 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001717 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1718 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001719 break;
1720 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001721
Douglas Gregor982365e2009-04-13 21:20:57 +00001722 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001723 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1724 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001725 break;
1726 }
1727
Douglas Gregorc34897d2009-04-09 22:27:44 +00001728 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001729 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1730 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001731 break;
1732 }
1733
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001734 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001735 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1736 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001737 break;
1738 }
1739
1740 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001741 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001742 QualType(), QualType(), VarDecl::None,
1743 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001744 break;
1745 }
1746
Douglas Gregor2a491792009-04-13 22:49:25 +00001747 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001748 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001749 break;
1750 }
1751
1752 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001753 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001754 break;
1755 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001756 }
1757
Douglas Gregorddf4d092009-04-16 22:29:51 +00001758 assert(D && "Unknown declaration creating PCH file");
1759 if (D) {
1760 LoadedDecl(Index, D);
1761 Reader.Visit(D);
1762 }
1763
Douglas Gregorc34897d2009-04-09 22:27:44 +00001764 // If this declaration is also a declaration context, get the
1765 // offsets for its tables of lexical and visible declarations.
1766 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1767 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1768 if (Offsets.first || Offsets.second) {
1769 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1770 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1771 DeclContextOffsets[DC] = Offsets;
1772 }
1773 }
1774 assert(Idx == Record.size());
1775
1776 return D;
1777}
1778
Douglas Gregorac8f2802009-04-10 17:25:41 +00001779QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001780 unsigned Quals = ID & 0x07;
1781 unsigned Index = ID >> 3;
1782
1783 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1784 QualType T;
1785 switch ((pch::PredefinedTypeIDs)Index) {
1786 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1787 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1788 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1789
1790 case pch::PREDEF_TYPE_CHAR_U_ID:
1791 case pch::PREDEF_TYPE_CHAR_S_ID:
1792 // FIXME: Check that the signedness of CharTy is correct!
1793 T = Context.CharTy;
1794 break;
1795
1796 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1797 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1798 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1799 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1800 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1801 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1802 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1803 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1804 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1805 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1806 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1807 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1808 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1809 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1810 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1811 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1812 }
1813
1814 assert(!T.isNull() && "Unknown predefined type");
1815 return T.getQualifiedType(Quals);
1816 }
1817
1818 Index -= pch::NUM_PREDEF_TYPE_IDS;
1819 if (!TypeAlreadyLoaded[Index]) {
1820 // Load the type from the PCH file.
1821 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1822 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1823 TypeAlreadyLoaded[Index] = true;
1824 }
1825
1826 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1827}
1828
Douglas Gregorac8f2802009-04-10 17:25:41 +00001829Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001830 if (ID == 0)
1831 return 0;
1832
1833 unsigned Index = ID - 1;
1834 if (DeclAlreadyLoaded[Index])
1835 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1836
1837 // Load the declaration from the PCH file.
1838 return ReadDeclRecord(DeclOffsets[Index], Index);
1839}
1840
1841bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001842 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001843 assert(DC->hasExternalLexicalStorage() &&
1844 "DeclContext has no lexical decls in storage");
1845 uint64_t Offset = DeclContextOffsets[DC].first;
1846 assert(Offset && "DeclContext has no lexical decls in storage");
1847
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001848 // Keep track of where we are in the stream, then jump back there
1849 // after reading this context.
1850 SavedStreamPosition SavedPosition(Stream);
1851
Douglas Gregorc34897d2009-04-09 22:27:44 +00001852 // Load the record containing all of the declarations lexically in
1853 // this context.
1854 Stream.JumpToBit(Offset);
1855 RecordData Record;
1856 unsigned Code = Stream.ReadCode();
1857 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001858 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001859 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1860
1861 // Load all of the declaration IDs
1862 Decls.clear();
1863 Decls.insert(Decls.end(), Record.begin(), Record.end());
1864 return false;
1865}
1866
1867bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1868 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1869 assert(DC->hasExternalVisibleStorage() &&
1870 "DeclContext has no visible decls in storage");
1871 uint64_t Offset = DeclContextOffsets[DC].second;
1872 assert(Offset && "DeclContext has no visible decls in storage");
1873
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001874 // Keep track of where we are in the stream, then jump back there
1875 // after reading this context.
1876 SavedStreamPosition SavedPosition(Stream);
1877
Douglas Gregorc34897d2009-04-09 22:27:44 +00001878 // Load the record containing all of the declarations visible in
1879 // this context.
1880 Stream.JumpToBit(Offset);
1881 RecordData Record;
1882 unsigned Code = Stream.ReadCode();
1883 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001884 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001885 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1886 if (Record.size() == 0)
1887 return false;
1888
1889 Decls.clear();
1890
1891 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001892 while (Idx < Record.size()) {
1893 Decls.push_back(VisibleDeclaration());
1894 Decls.back().Name = ReadDeclarationName(Record, Idx);
1895
Douglas Gregorc34897d2009-04-09 22:27:44 +00001896 unsigned Size = Record[Idx++];
1897 llvm::SmallVector<unsigned, 4> & LoadedDecls
1898 = Decls.back().Declarations;
1899 LoadedDecls.reserve(Size);
1900 for (unsigned I = 0; I < Size; ++I)
1901 LoadedDecls.push_back(Record[Idx++]);
1902 }
1903
1904 return false;
1905}
1906
Douglas Gregor631f6c62009-04-14 00:24:19 +00001907void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1908 if (!Consumer)
1909 return;
1910
1911 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1912 Decl *D = GetDecl(ExternalDefinitions[I]);
1913 DeclGroupRef DG(D);
1914 Consumer->HandleTopLevelDecl(DG);
1915 }
1916}
1917
Douglas Gregorc34897d2009-04-09 22:27:44 +00001918void PCHReader::PrintStats() {
1919 std::fprintf(stderr, "*** PCH Statistics:\n");
1920
1921 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1922 TypeAlreadyLoaded.end(),
1923 true);
1924 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1925 DeclAlreadyLoaded.end(),
1926 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001927 unsigned NumIdentifiersLoaded = 0;
1928 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1929 if ((IdentifierData[I] & 0x01) == 0)
1930 ++NumIdentifiersLoaded;
1931 }
1932
Douglas Gregorc34897d2009-04-09 22:27:44 +00001933 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1934 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001935 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001936 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1937 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001938 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1939 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1940 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1941 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001942 std::fprintf(stderr, "\n");
1943}
1944
Chris Lattner29241862009-04-11 21:15:38 +00001945IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001946 if (ID == 0)
1947 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001948
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001949 if (!IdentifierTable || IdentifierData.empty()) {
1950 Error("No identifier table in PCH file");
1951 return 0;
1952 }
Chris Lattner29241862009-04-11 21:15:38 +00001953
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001954 if (IdentifierData[ID - 1] & 0x01) {
1955 uint64_t Offset = IdentifierData[ID - 1];
1956 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001957 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001958 }
Chris Lattner29241862009-04-11 21:15:38 +00001959
1960 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001961}
1962
1963DeclarationName
1964PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1965 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1966 switch (Kind) {
1967 case DeclarationName::Identifier:
1968 return DeclarationName(GetIdentifierInfo(Record, Idx));
1969
1970 case DeclarationName::ObjCZeroArgSelector:
1971 case DeclarationName::ObjCOneArgSelector:
1972 case DeclarationName::ObjCMultiArgSelector:
1973 assert(false && "Unable to de-serialize Objective-C selectors");
1974 break;
1975
1976 case DeclarationName::CXXConstructorName:
1977 return Context.DeclarationNames.getCXXConstructorName(
1978 GetType(Record[Idx++]));
1979
1980 case DeclarationName::CXXDestructorName:
1981 return Context.DeclarationNames.getCXXDestructorName(
1982 GetType(Record[Idx++]));
1983
1984 case DeclarationName::CXXConversionFunctionName:
1985 return Context.DeclarationNames.getCXXConversionFunctionName(
1986 GetType(Record[Idx++]));
1987
1988 case DeclarationName::CXXOperatorName:
1989 return Context.DeclarationNames.getCXXOperatorName(
1990 (OverloadedOperatorKind)Record[Idx++]);
1991
1992 case DeclarationName::CXXUsingDirective:
1993 return DeclarationName::getUsingDirectiveName();
1994 }
1995
1996 // Required to silence GCC warning
1997 return DeclarationName();
1998}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001999
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002000/// \brief Read an integral value
2001llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2002 unsigned BitWidth = Record[Idx++];
2003 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2004 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2005 Idx += NumWords;
2006 return Result;
2007}
2008
2009/// \brief Read a signed integral value
2010llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2011 bool isUnsigned = Record[Idx++];
2012 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2013}
2014
Douglas Gregore2f37202009-04-14 21:55:33 +00002015/// \brief Read a floating-point value
2016llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002017 return llvm::APFloat(ReadAPInt(Record, Idx));
2018}
2019
Douglas Gregor1c507882009-04-15 21:30:51 +00002020// \brief Read a string
2021std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2022 unsigned Len = Record[Idx++];
2023 std::string Result(&Record[Idx], &Record[Idx] + Len);
2024 Idx += Len;
2025 return Result;
2026}
2027
2028/// \brief Reads attributes from the current stream position.
2029Attr *PCHReader::ReadAttributes() {
2030 unsigned Code = Stream.ReadCode();
2031 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2032 "Expected unabbreviated record"); (void)Code;
2033
2034 RecordData Record;
2035 unsigned Idx = 0;
2036 unsigned RecCode = Stream.ReadRecord(Code, Record);
2037 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2038 (void)RecCode;
2039
2040#define SIMPLE_ATTR(Name) \
2041 case Attr::Name: \
2042 New = ::new (Context) Name##Attr(); \
2043 break
2044
2045#define STRING_ATTR(Name) \
2046 case Attr::Name: \
2047 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2048 break
2049
2050#define UNSIGNED_ATTR(Name) \
2051 case Attr::Name: \
2052 New = ::new (Context) Name##Attr(Record[Idx++]); \
2053 break
2054
2055 Attr *Attrs = 0;
2056 while (Idx < Record.size()) {
2057 Attr *New = 0;
2058 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2059 bool IsInherited = Record[Idx++];
2060
2061 switch (Kind) {
2062 STRING_ATTR(Alias);
2063 UNSIGNED_ATTR(Aligned);
2064 SIMPLE_ATTR(AlwaysInline);
2065 SIMPLE_ATTR(AnalyzerNoReturn);
2066 STRING_ATTR(Annotate);
2067 STRING_ATTR(AsmLabel);
2068
2069 case Attr::Blocks:
2070 New = ::new (Context) BlocksAttr(
2071 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2072 break;
2073
2074 case Attr::Cleanup:
2075 New = ::new (Context) CleanupAttr(
2076 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2077 break;
2078
2079 SIMPLE_ATTR(Const);
2080 UNSIGNED_ATTR(Constructor);
2081 SIMPLE_ATTR(DLLExport);
2082 SIMPLE_ATTR(DLLImport);
2083 SIMPLE_ATTR(Deprecated);
2084 UNSIGNED_ATTR(Destructor);
2085 SIMPLE_ATTR(FastCall);
2086
2087 case Attr::Format: {
2088 std::string Type = ReadString(Record, Idx);
2089 unsigned FormatIdx = Record[Idx++];
2090 unsigned FirstArg = Record[Idx++];
2091 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2092 break;
2093 }
2094
2095 SIMPLE_ATTR(GNUCInline);
2096
2097 case Attr::IBOutletKind:
2098 New = ::new (Context) IBOutletAttr();
2099 break;
2100
2101 SIMPLE_ATTR(NoReturn);
2102 SIMPLE_ATTR(NoThrow);
2103 SIMPLE_ATTR(Nodebug);
2104 SIMPLE_ATTR(Noinline);
2105
2106 case Attr::NonNull: {
2107 unsigned Size = Record[Idx++];
2108 llvm::SmallVector<unsigned, 16> ArgNums;
2109 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2110 Idx += Size;
2111 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2112 break;
2113 }
2114
2115 SIMPLE_ATTR(ObjCException);
2116 SIMPLE_ATTR(ObjCNSObject);
2117 SIMPLE_ATTR(Overloadable);
2118 UNSIGNED_ATTR(Packed);
2119 SIMPLE_ATTR(Pure);
2120 UNSIGNED_ATTR(Regparm);
2121 STRING_ATTR(Section);
2122 SIMPLE_ATTR(StdCall);
2123 SIMPLE_ATTR(TransparentUnion);
2124 SIMPLE_ATTR(Unavailable);
2125 SIMPLE_ATTR(Unused);
2126 SIMPLE_ATTR(Used);
2127
2128 case Attr::Visibility:
2129 New = ::new (Context) VisibilityAttr(
2130 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2131 break;
2132
2133 SIMPLE_ATTR(WarnUnusedResult);
2134 SIMPLE_ATTR(Weak);
2135 SIMPLE_ATTR(WeakImport);
2136 }
2137
2138 assert(New && "Unable to decode attribute?");
2139 New->setInherited(IsInherited);
2140 New->setNext(Attrs);
2141 Attrs = New;
2142 }
2143#undef UNSIGNED_ATTR
2144#undef STRING_ATTR
2145#undef SIMPLE_ATTR
2146
2147 // The list of attributes was built backwards. Reverse the list
2148 // before returning it.
2149 Attr *PrevAttr = 0, *NextAttr = 0;
2150 while (Attrs) {
2151 NextAttr = Attrs->getNext();
2152 Attrs->setNext(PrevAttr);
2153 PrevAttr = Attrs;
2154 Attrs = NextAttr;
2155 }
2156
2157 return PrevAttr;
2158}
2159
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002160Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002161 // Within the bitstream, expressions are stored in Reverse Polish
2162 // Notation, with each of the subexpressions preceding the
2163 // expression they are stored in. To evaluate expressions, we
2164 // continue reading expressions and placing them on the stack, with
2165 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002166 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002167 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002168 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002169 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002170 llvm::SmallVector<Stmt *, 16> StmtStack;
2171 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002172 Stmt::EmptyShell Empty;
2173
Douglas Gregora151ba42009-04-14 23:32:43 +00002174 while (true) {
2175 unsigned Code = Stream.ReadCode();
2176 if (Code == llvm::bitc::END_BLOCK) {
2177 if (Stream.ReadBlockEnd()) {
2178 Error("Error at end of Source Manager block");
2179 return 0;
2180 }
2181 break;
2182 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002183
Douglas Gregora151ba42009-04-14 23:32:43 +00002184 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2185 // No known subblocks, always skip them.
2186 Stream.ReadSubBlockID();
2187 if (Stream.SkipBlock()) {
2188 Error("Malformed block record");
2189 return 0;
2190 }
2191 continue;
2192 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002193
Douglas Gregora151ba42009-04-14 23:32:43 +00002194 if (Code == llvm::bitc::DEFINE_ABBREV) {
2195 Stream.ReadAbbrevRecord();
2196 continue;
2197 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002198
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002199 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002200 Idx = 0;
2201 Record.clear();
2202 bool Finished = false;
2203 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002204 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002205 Finished = true;
2206 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002207
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002208 case pch::STMT_NULL_PTR:
2209 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002210 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002211
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002212 case pch::STMT_NULL:
2213 S = new (Context) NullStmt(Empty);
2214 break;
2215
2216 case pch::STMT_COMPOUND:
2217 S = new (Context) CompoundStmt(Empty);
2218 break;
2219
2220 case pch::STMT_CASE:
2221 S = new (Context) CaseStmt(Empty);
2222 break;
2223
2224 case pch::STMT_DEFAULT:
2225 S = new (Context) DefaultStmt(Empty);
2226 break;
2227
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002228 case pch::STMT_LABEL:
2229 S = new (Context) LabelStmt(Empty);
2230 break;
2231
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002232 case pch::STMT_IF:
2233 S = new (Context) IfStmt(Empty);
2234 break;
2235
2236 case pch::STMT_SWITCH:
2237 S = new (Context) SwitchStmt(Empty);
2238 break;
2239
Douglas Gregora6b503f2009-04-17 00:16:09 +00002240 case pch::STMT_WHILE:
2241 S = new (Context) WhileStmt(Empty);
2242 break;
2243
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002244 case pch::STMT_DO:
2245 S = new (Context) DoStmt(Empty);
2246 break;
2247
2248 case pch::STMT_FOR:
2249 S = new (Context) ForStmt(Empty);
2250 break;
2251
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002252 case pch::STMT_GOTO:
2253 S = new (Context) GotoStmt(Empty);
2254 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002255
2256 case pch::STMT_INDIRECT_GOTO:
2257 S = new (Context) IndirectGotoStmt(Empty);
2258 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002259
Douglas Gregora6b503f2009-04-17 00:16:09 +00002260 case pch::STMT_CONTINUE:
2261 S = new (Context) ContinueStmt(Empty);
2262 break;
2263
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002264 case pch::STMT_BREAK:
2265 S = new (Context) BreakStmt(Empty);
2266 break;
2267
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002268 case pch::STMT_RETURN:
2269 S = new (Context) ReturnStmt(Empty);
2270 break;
2271
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002272 case pch::STMT_DECL:
2273 S = new (Context) DeclStmt(Empty);
2274 break;
2275
Douglas Gregora151ba42009-04-14 23:32:43 +00002276 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002277 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002278 break;
2279
2280 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002281 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002282 break;
2283
2284 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002285 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002286 break;
2287
2288 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002289 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002290 break;
2291
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002292 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002293 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002294 break;
2295
Douglas Gregor596e0932009-04-15 16:35:07 +00002296 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002297 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002298 Record[PCHStmtReader::NumExprFields + 1]);
2299 break;
2300
Douglas Gregora151ba42009-04-14 23:32:43 +00002301 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002302 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002303 break;
2304
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002305 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002306 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002307 break;
2308
Douglas Gregor12d74052009-04-15 15:58:59 +00002309 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002310 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002311 break;
2312
2313 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002314 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002315 break;
2316
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002317 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002318 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002319 break;
2320
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002321 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002322 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002323 break;
2324
2325 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002326 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002327 break;
2328
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002329 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002330 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002331 break;
2332
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002333 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002334 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002335 break;
2336
2337 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002338 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002339 break;
2340
Douglas Gregora151ba42009-04-14 23:32:43 +00002341 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002342 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002343 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002344
2345 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002346 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002347 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002348
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002349 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002350 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002351 break;
2352
Douglas Gregorec0b8292009-04-15 23:02:49 +00002353 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002354 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002355 break;
2356
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002357 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002358 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002359 break;
2360
2361 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002362 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002363 Record[PCHStmtReader::NumExprFields] - 1);
2364
2365 break;
2366
2367 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002368 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002369 break;
2370
Douglas Gregorec0b8292009-04-15 23:02:49 +00002371 case pch::EXPR_VA_ARG:
2372 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002373 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002374 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002375
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002376 case pch::EXPR_ADDR_LABEL:
2377 S = new (Context) AddrLabelExpr(Empty);
2378 break;
2379
Douglas Gregoreca12f62009-04-17 19:05:30 +00002380 case pch::EXPR_STMT:
2381 S = new (Context) StmtExpr(Empty);
2382 break;
2383
Douglas Gregor209d4622009-04-15 23:33:31 +00002384 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002385 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002386 break;
2387
2388 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002389 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002390 break;
2391
2392 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002393 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002394 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002395
2396 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002397 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002398 break;
2399
Douglas Gregore246b742009-04-17 19:21:43 +00002400 case pch::EXPR_BLOCK:
2401 S = new (Context) BlockExpr(Empty);
2402 break;
2403
Douglas Gregor725e94b2009-04-16 00:01:45 +00002404 case pch::EXPR_BLOCK_DECL_REF:
2405 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002406 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002407 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002408 }
2409
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002410 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002411 if (Finished)
2412 break;
2413
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002414 if (S) {
2415 unsigned NumSubStmts = Reader.Visit(S);
2416 while (NumSubStmts > 0) {
2417 StmtStack.pop_back();
2418 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002419 }
2420 }
2421
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002422 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002423 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002424 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002425 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002426 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002427 return StmtStack.back();
2428}
2429
2430Expr *PCHReader::ReadExpr() {
2431 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002432}
2433
Douglas Gregor179cfb12009-04-10 20:39:37 +00002434DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002435 return Diag(SourceLocation(), DiagID);
2436}
2437
2438DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2439 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002440 Context.getSourceManager()),
2441 DiagID);
2442}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002443
2444/// \brief Record that the given ID maps to the given switch-case
2445/// statement.
2446void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2447 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2448 SwitchCaseStmts[ID] = SC;
2449}
2450
2451/// \brief Retrieve the switch-case statement with the given ID.
2452SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2453 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2454 return SwitchCaseStmts[ID];
2455}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002456
2457/// \brief Record that the given label statement has been
2458/// deserialized and has the given ID.
2459void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2460 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2461 "Deserialized label twice");
2462 LabelStmts[ID] = S;
2463
2464 // If we've already seen any goto statements that point to this
2465 // label, resolve them now.
2466 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2467 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2468 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2469 Goto->second->setLabel(S);
2470 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002471
2472 // If we've already seen any address-label statements that point to
2473 // this label, resolve them now.
2474 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2475 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2476 = UnresolvedAddrLabelExprs.equal_range(ID);
2477 for (AddrLabelIter AddrLabel = AddrLabels.first;
2478 AddrLabel != AddrLabels.second; ++AddrLabel)
2479 AddrLabel->second->setLabel(S);
2480 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002481}
2482
2483/// \brief Set the label of the given statement to the label
2484/// identified by ID.
2485///
2486/// Depending on the order in which the label and other statements
2487/// referencing that label occur, this operation may complete
2488/// immediately (updating the statement) or it may queue the
2489/// statement to be back-patched later.
2490void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2491 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2492 if (Label != LabelStmts.end()) {
2493 // We've already seen this label, so set the label of the goto and
2494 // we're done.
2495 S->setLabel(Label->second);
2496 } else {
2497 // We haven't seen this label yet, so add this goto to the set of
2498 // unresolved goto statements.
2499 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2500 }
2501}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002502
2503/// \brief Set the label of the given expression to the label
2504/// identified by ID.
2505///
2506/// Depending on the order in which the label and other statements
2507/// referencing that label occur, this operation may complete
2508/// immediately (updating the statement) or it may queue the
2509/// statement to be back-patched later.
2510void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2511 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2512 if (Label != LabelStmts.end()) {
2513 // We've already seen this label, so set the label of the
2514 // label-address expression and we're done.
2515 S->setLabel(Label->second);
2516 } else {
2517 // We haven't seen this label yet, so add this label-address
2518 // expression to the set of unresolved label-address expressions.
2519 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2520 }
2521}