blob: e474dc7a70daeff60933e203047a5883ab0b0ce1 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000107}
108
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor982365e2009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregorc34897d2009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor982365e2009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000167}
168
Douglas Gregorc34897d2009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000180}
181
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor2a491792009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
200 unsigned NumParams = Record[Idx++];
201 llvm::SmallVector<ParmVarDecl *, 16> Params;
202 Params.reserve(NumParams);
203 for (unsigned I = 0; I != NumParams; ++I)
204 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
205 BD->setParams(Reader.getContext(), &Params[0], NumParams);
206}
207
Douglas Gregorc34897d2009-04-09 22:27:44 +0000208std::pair<uint64_t, uint64_t>
209PCHDeclReader::VisitDeclContext(DeclContext *DC) {
210 uint64_t LexicalOffset = Record[Idx++];
211 uint64_t VisibleOffset = 0;
212 if (DC->getPrimaryContext() == DC)
213 VisibleOffset = Record[Idx++];
214 return std::make_pair(LexicalOffset, VisibleOffset);
215}
216
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000217//===----------------------------------------------------------------------===//
218// Statement/expression deserialization
219//===----------------------------------------------------------------------===//
220namespace {
221 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000222 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000223 PCHReader &Reader;
224 const PCHReader::RecordData &Record;
225 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000226 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000227
228 public:
229 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000230 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
231 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000232
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000233 /// \brief The number of record fields required for the Stmt class
234 /// itself.
235 static const unsigned NumStmtFields = 0;
236
Douglas Gregor596e0932009-04-15 16:35:07 +0000237 /// \brief The number of record fields required for the Expr class
238 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000239 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000240
Douglas Gregora151ba42009-04-14 23:32:43 +0000241 // Each of the Visit* functions reads in part of the expression
242 // from the given record and the current expression stack, then
243 // return the total number of operands that it read from the
244 // expression stack.
245
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000246 unsigned VisitStmt(Stmt *S);
247 unsigned VisitNullStmt(NullStmt *S);
248 unsigned VisitCompoundStmt(CompoundStmt *S);
249 unsigned VisitSwitchCase(SwitchCase *S);
250 unsigned VisitCaseStmt(CaseStmt *S);
251 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000252 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000253 unsigned VisitIfStmt(IfStmt *S);
254 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000255 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000256 unsigned VisitDoStmt(DoStmt *S);
257 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000258 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000259 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000260 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000261 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000262 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000263 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000264 unsigned VisitExpr(Expr *E);
265 unsigned VisitPredefinedExpr(PredefinedExpr *E);
266 unsigned VisitDeclRefExpr(DeclRefExpr *E);
267 unsigned VisitIntegerLiteral(IntegerLiteral *E);
268 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000269 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000270 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000271 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000272 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000273 unsigned VisitUnaryOperator(UnaryOperator *E);
274 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000275 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000276 unsigned VisitCallExpr(CallExpr *E);
277 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000278 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000279 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000280 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
281 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000282 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000283 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
284 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000285 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000286 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000287 unsigned VisitInitListExpr(InitListExpr *E);
288 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
289 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000290 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000291 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000292 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000293 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
294 unsigned VisitChooseExpr(ChooseExpr *E);
295 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000296 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
297 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000298 };
299}
300
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000301unsigned PCHStmtReader::VisitStmt(Stmt *S) {
302 assert(Idx == NumStmtFields && "Incorrect statement field count");
303 return 0;
304}
305
306unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
307 VisitStmt(S);
308 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
309 return 0;
310}
311
312unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
313 VisitStmt(S);
314 unsigned NumStmts = Record[Idx++];
315 S->setStmts(Reader.getContext(),
316 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
317 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
318 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
319 return NumStmts;
320}
321
322unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
323 VisitStmt(S);
324 Reader.RecordSwitchCaseID(S, Record[Idx++]);
325 return 0;
326}
327
328unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
329 VisitSwitchCase(S);
330 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
331 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
332 S->setSubStmt(StmtStack.back());
333 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
334 return 3;
335}
336
337unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
338 VisitSwitchCase(S);
339 S->setSubStmt(StmtStack.back());
340 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
341 return 1;
342}
343
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000344unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
345 VisitStmt(S);
346 S->setID(Reader.GetIdentifierInfo(Record, Idx));
347 S->setSubStmt(StmtStack.back());
348 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
349 Reader.RecordLabelStmt(S, Record[Idx++]);
350 return 1;
351}
352
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000353unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
354 VisitStmt(S);
355 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
356 S->setThen(StmtStack[StmtStack.size() - 2]);
357 S->setElse(StmtStack[StmtStack.size() - 1]);
358 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
359 return 3;
360}
361
362unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
363 VisitStmt(S);
364 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
365 S->setBody(StmtStack.back());
366 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
367 SwitchCase *PrevSC = 0;
368 for (unsigned N = Record.size(); Idx != N; ++Idx) {
369 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
370 if (PrevSC)
371 PrevSC->setNextSwitchCase(SC);
372 else
373 S->setSwitchCaseList(SC);
374 PrevSC = SC;
375 }
376 return 2;
377}
378
Douglas Gregora6b503f2009-04-17 00:16:09 +0000379unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
380 VisitStmt(S);
381 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
382 S->setBody(StmtStack.back());
383 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
384 return 2;
385}
386
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000387unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
388 VisitStmt(S);
389 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
390 S->setBody(StmtStack.back());
391 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
392 return 2;
393}
394
395unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
396 VisitStmt(S);
397 S->setInit(StmtStack[StmtStack.size() - 4]);
398 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
399 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
400 S->setBody(StmtStack.back());
401 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
402 return 4;
403}
404
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000405unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
406 VisitStmt(S);
407 Reader.SetLabelOf(S, Record[Idx++]);
408 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
409 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
410 return 0;
411}
412
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000413unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
414 VisitStmt(S);
415 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
416 return 1;
417}
418
Douglas Gregora6b503f2009-04-17 00:16:09 +0000419unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
420 VisitStmt(S);
421 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
422 return 0;
423}
424
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000425unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
426 VisitStmt(S);
427 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
428 return 0;
429}
430
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000431unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
432 VisitStmt(S);
433 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
434 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
435 return 1;
436}
437
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000438unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
439 VisitStmt(S);
440 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
441 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
442
443 if (Idx + 1 == Record.size()) {
444 // Single declaration
445 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
446 } else {
447 llvm::SmallVector<Decl *, 16> Decls;
448 Decls.reserve(Record.size() - Idx);
449 for (unsigned N = Record.size(); Idx != N; ++Idx)
450 Decls.push_back(Reader.GetDecl(Record[Idx]));
451 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
452 &Decls[0], Decls.size())));
453 }
454 return 0;
455}
456
Douglas Gregora151ba42009-04-14 23:32:43 +0000457unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000458 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000459 E->setType(Reader.GetType(Record[Idx++]));
460 E->setTypeDependent(Record[Idx++]);
461 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000462 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000463 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000464}
465
Douglas Gregora151ba42009-04-14 23:32:43 +0000466unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000467 VisitExpr(E);
468 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
469 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000470 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000471}
472
Douglas Gregora151ba42009-04-14 23:32:43 +0000473unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000474 VisitExpr(E);
475 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
476 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000477 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000478}
479
Douglas Gregora151ba42009-04-14 23:32:43 +0000480unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000481 VisitExpr(E);
482 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
483 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000484 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000485}
486
Douglas Gregora151ba42009-04-14 23:32:43 +0000487unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000488 VisitExpr(E);
489 E->setValue(Reader.ReadAPFloat(Record, Idx));
490 E->setExact(Record[Idx++]);
491 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000492 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000493}
494
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000495unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
496 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000497 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000498 return 1;
499}
500
Douglas Gregor596e0932009-04-15 16:35:07 +0000501unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
502 VisitExpr(E);
503 unsigned Len = Record[Idx++];
504 assert(Record[Idx] == E->getNumConcatenated() &&
505 "Wrong number of concatenated tokens!");
506 ++Idx;
507 E->setWide(Record[Idx++]);
508
509 // Read string data
510 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
511 E->setStrData(Reader.getContext(), &Str[0], Len);
512 Idx += Len;
513
514 // Read source locations
515 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
516 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
517
518 return 0;
519}
520
Douglas Gregora151ba42009-04-14 23:32:43 +0000521unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000522 VisitExpr(E);
523 E->setValue(Record[Idx++]);
524 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
525 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000526 return 0;
527}
528
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000529unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
530 VisitExpr(E);
531 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
532 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000533 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000534 return 1;
535}
536
Douglas Gregor12d74052009-04-15 15:58:59 +0000537unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
538 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000539 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000540 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
541 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 return 1;
543}
544
545unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
546 VisitExpr(E);
547 E->setSizeof(Record[Idx++]);
548 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000549 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000550 ++Idx;
551 } else {
552 E->setArgument(Reader.GetType(Record[Idx++]));
553 }
554 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 return E->isArgumentType()? 0 : 1;
557}
558
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000559unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
560 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000561 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
562 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000563 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
564 return 2;
565}
566
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000567unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
568 VisitExpr(E);
569 E->setNumArgs(Reader.getContext(), Record[Idx++]);
570 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000571 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000572 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000573 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000574 return E->getNumArgs() + 1;
575}
576
577unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
578 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000579 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000580 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
581 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
582 E->setArrow(Record[Idx++]);
583 return 1;
584}
585
Douglas Gregora151ba42009-04-14 23:32:43 +0000586unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
587 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000588 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000589 return 1;
590}
591
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000592unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
593 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000594 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
595 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000596 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
597 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
598 return 2;
599}
600
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000601unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
602 VisitBinaryOperator(E);
603 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
604 E->setComputationResultType(Reader.GetType(Record[Idx++]));
605 return 2;
606}
607
608unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
609 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000610 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
611 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
612 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000613 return 3;
614}
615
Douglas Gregora151ba42009-04-14 23:32:43 +0000616unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
617 VisitCastExpr(E);
618 E->setLvalueCast(Record[Idx++]);
619 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000620}
621
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000622unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
623 VisitCastExpr(E);
624 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
625 return 1;
626}
627
628unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
629 VisitExplicitCastExpr(E);
630 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
631 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
632 return 1;
633}
634
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000635unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
636 VisitExpr(E);
637 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000638 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000639 E->setFileScope(Record[Idx++]);
640 return 1;
641}
642
Douglas Gregorec0b8292009-04-15 23:02:49 +0000643unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
644 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000645 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000646 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
647 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
648 return 1;
649}
650
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000651unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
652 VisitExpr(E);
653 unsigned NumInits = Record[Idx++];
654 E->reserveInits(NumInits);
655 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000656 E->updateInit(I,
657 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
658 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000659 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
660 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
661 E->setInitializedFieldInUnion(
662 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
663 E->sawArrayRangeDesignator(Record[Idx++]);
664 return NumInits + 1;
665}
666
667unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
668 typedef DesignatedInitExpr::Designator Designator;
669
670 VisitExpr(E);
671 unsigned NumSubExprs = Record[Idx++];
672 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
673 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000674 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000675 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
676 E->setGNUSyntax(Record[Idx++]);
677
678 llvm::SmallVector<Designator, 4> Designators;
679 while (Idx < Record.size()) {
680 switch ((pch::DesignatorTypes)Record[Idx++]) {
681 case pch::DESIG_FIELD_DECL: {
682 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
683 SourceLocation DotLoc
684 = SourceLocation::getFromRawEncoding(Record[Idx++]);
685 SourceLocation FieldLoc
686 = SourceLocation::getFromRawEncoding(Record[Idx++]);
687 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
688 FieldLoc));
689 Designators.back().setField(Field);
690 break;
691 }
692
693 case pch::DESIG_FIELD_NAME: {
694 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
695 SourceLocation DotLoc
696 = SourceLocation::getFromRawEncoding(Record[Idx++]);
697 SourceLocation FieldLoc
698 = SourceLocation::getFromRawEncoding(Record[Idx++]);
699 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
700 break;
701 }
702
703 case pch::DESIG_ARRAY: {
704 unsigned Index = Record[Idx++];
705 SourceLocation LBracketLoc
706 = SourceLocation::getFromRawEncoding(Record[Idx++]);
707 SourceLocation RBracketLoc
708 = SourceLocation::getFromRawEncoding(Record[Idx++]);
709 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
710 break;
711 }
712
713 case pch::DESIG_ARRAY_RANGE: {
714 unsigned Index = Record[Idx++];
715 SourceLocation LBracketLoc
716 = SourceLocation::getFromRawEncoding(Record[Idx++]);
717 SourceLocation EllipsisLoc
718 = SourceLocation::getFromRawEncoding(Record[Idx++]);
719 SourceLocation RBracketLoc
720 = SourceLocation::getFromRawEncoding(Record[Idx++]);
721 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
722 RBracketLoc));
723 break;
724 }
725 }
726 }
727 E->setDesignators(&Designators[0], Designators.size());
728
729 return NumSubExprs;
730}
731
732unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
733 VisitExpr(E);
734 return 0;
735}
736
Douglas Gregorec0b8292009-04-15 23:02:49 +0000737unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
738 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000739 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000740 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
741 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
742 return 1;
743}
744
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000745unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
746 VisitExpr(E);
747 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
748 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
749 Reader.SetLabelOf(E, Record[Idx++]);
750 return 0;
751}
752
Douglas Gregoreca12f62009-04-17 19:05:30 +0000753unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
754 VisitExpr(E);
755 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
756 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
757 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
758 return 1;
759}
760
Douglas Gregor209d4622009-04-15 23:33:31 +0000761unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
762 VisitExpr(E);
763 E->setArgType1(Reader.GetType(Record[Idx++]));
764 E->setArgType2(Reader.GetType(Record[Idx++]));
765 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
766 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
767 return 0;
768}
769
770unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
771 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000772 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
773 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
774 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000775 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
776 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
777 return 3;
778}
779
780unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
781 VisitExpr(E);
782 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
783 return 0;
784}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000785
Douglas Gregor725e94b2009-04-16 00:01:45 +0000786unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
787 VisitExpr(E);
788 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000789 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000790 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
791 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
792 return NumExprs;
793}
794
795unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
796 VisitExpr(E);
797 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
798 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
799 E->setByRef(Record[Idx++]);
800 return 0;
801}
802
Douglas Gregorc34897d2009-04-09 22:27:44 +0000803// FIXME: use the diagnostics machinery
804static bool Error(const char *Str) {
805 std::fprintf(stderr, "%s\n", Str);
806 return true;
807}
808
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000809/// \brief Check the contents of the predefines buffer against the
810/// contents of the predefines buffer used to build the PCH file.
811///
812/// The contents of the two predefines buffers should be the same. If
813/// not, then some command-line option changed the preprocessor state
814/// and we must reject the PCH file.
815///
816/// \param PCHPredef The start of the predefines buffer in the PCH
817/// file.
818///
819/// \param PCHPredefLen The length of the predefines buffer in the PCH
820/// file.
821///
822/// \param PCHBufferID The FileID for the PCH predefines buffer.
823///
824/// \returns true if there was a mismatch (in which case the PCH file
825/// should be ignored), or false otherwise.
826bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
827 unsigned PCHPredefLen,
828 FileID PCHBufferID) {
829 const char *Predef = PP.getPredefines().c_str();
830 unsigned PredefLen = PP.getPredefines().size();
831
832 // If the two predefines buffers compare equal, we're done!.
833 if (PredefLen == PCHPredefLen &&
834 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
835 return false;
836
837 // The predefines buffers are different. Produce a reasonable
838 // diagnostic showing where they are different.
839
840 // The source locations (potentially in the two different predefines
841 // buffers)
842 SourceLocation Loc1, Loc2;
843 SourceManager &SourceMgr = PP.getSourceManager();
844
845 // Create a source buffer for our predefines string, so
846 // that we can build a diagnostic that points into that
847 // source buffer.
848 FileID BufferID;
849 if (Predef && Predef[0]) {
850 llvm::MemoryBuffer *Buffer
851 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
852 "<built-in>");
853 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
854 }
855
856 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
857 std::pair<const char *, const char *> Locations
858 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
859
860 if (Locations.first != Predef + MinLen) {
861 // We found the location in the two buffers where there is a
862 // difference. Form source locations to point there (in both
863 // buffers).
864 unsigned Offset = Locations.first - Predef;
865 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
866 .getFileLocWithOffset(Offset);
867 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
868 .getFileLocWithOffset(Offset);
869 } else if (PredefLen > PCHPredefLen) {
870 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
871 .getFileLocWithOffset(MinLen);
872 } else {
873 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
874 .getFileLocWithOffset(MinLen);
875 }
876
877 Diag(Loc1, diag::warn_pch_preprocessor);
878 if (Loc2.isValid())
879 Diag(Loc2, diag::note_predef_in_pch);
880 Diag(diag::note_ignoring_pch) << FileName;
881 return true;
882}
883
Douglas Gregor635f97f2009-04-13 16:31:14 +0000884/// \brief Read the line table in the source manager block.
885/// \returns true if ther was an error.
886static bool ParseLineTable(SourceManager &SourceMgr,
887 llvm::SmallVectorImpl<uint64_t> &Record) {
888 unsigned Idx = 0;
889 LineTableInfo &LineTable = SourceMgr.getLineTable();
890
891 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000892 std::map<int, int> FileIDs;
893 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000894 // Extract the file name
895 unsigned FilenameLen = Record[Idx++];
896 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
897 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000898 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
899 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000900 }
901
902 // Parse the line entries
903 std::vector<LineEntry> Entries;
904 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000905 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000906
907 // Extract the line entries
908 unsigned NumEntries = Record[Idx++];
909 Entries.clear();
910 Entries.reserve(NumEntries);
911 for (unsigned I = 0; I != NumEntries; ++I) {
912 unsigned FileOffset = Record[Idx++];
913 unsigned LineNo = Record[Idx++];
914 int FilenameID = Record[Idx++];
915 SrcMgr::CharacteristicKind FileKind
916 = (SrcMgr::CharacteristicKind)Record[Idx++];
917 unsigned IncludeOffset = Record[Idx++];
918 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
919 FileKind, IncludeOffset));
920 }
921 LineTable.AddEntry(FID, Entries);
922 }
923
924 return false;
925}
926
Douglas Gregorab1cef72009-04-10 03:52:48 +0000927/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000928PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000929 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000930 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
931 Error("Malformed source manager block record");
932 return Failure;
933 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000934
935 SourceManager &SourceMgr = Context.getSourceManager();
936 RecordData Record;
937 while (true) {
938 unsigned Code = Stream.ReadCode();
939 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000940 if (Stream.ReadBlockEnd()) {
941 Error("Error at end of Source Manager block");
942 return Failure;
943 }
944
945 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000946 }
947
948 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
949 // No known subblocks, always skip them.
950 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000951 if (Stream.SkipBlock()) {
952 Error("Malformed block record");
953 return Failure;
954 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000955 continue;
956 }
957
958 if (Code == llvm::bitc::DEFINE_ABBREV) {
959 Stream.ReadAbbrevRecord();
960 continue;
961 }
962
963 // Read a record.
964 const char *BlobStart;
965 unsigned BlobLen;
966 Record.clear();
967 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
968 default: // Default behavior: ignore.
969 break;
970
971 case pch::SM_SLOC_FILE_ENTRY: {
972 // FIXME: We would really like to delay the creation of this
973 // FileEntry until it is actually required, e.g., when producing
974 // a diagnostic with a source location in this file.
975 const FileEntry *File
976 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
977 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000978 FileID ID = SourceMgr.createFileID(File,
979 SourceLocation::getFromRawEncoding(Record[1]),
980 (CharacteristicKind)Record[2]);
981 if (Record[3])
982 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
983 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000984 break;
985 }
986
987 case pch::SM_SLOC_BUFFER_ENTRY: {
988 const char *Name = BlobStart;
989 unsigned Code = Stream.ReadCode();
990 Record.clear();
991 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
992 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000993 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000994 llvm::MemoryBuffer *Buffer
995 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
996 BlobStart + BlobLen - 1,
997 Name);
998 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
999
1000 if (strcmp(Name, "<built-in>") == 0
1001 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1002 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001003 break;
1004 }
1005
1006 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1007 SourceLocation SpellingLoc
1008 = SourceLocation::getFromRawEncoding(Record[1]);
1009 SourceMgr.createInstantiationLoc(
1010 SpellingLoc,
1011 SourceLocation::getFromRawEncoding(Record[2]),
1012 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001013 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001014 break;
1015 }
1016
Chris Lattnere1be6022009-04-14 23:22:57 +00001017 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001018 if (ParseLineTable(SourceMgr, Record))
1019 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001020 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001021 }
1022 }
1023}
1024
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001025bool PCHReader::ReadPreprocessorBlock() {
1026 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1027 return Error("Malformed preprocessor block record");
1028
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001029 RecordData Record;
1030 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1031 MacroInfo *LastMacro = 0;
1032
1033 while (true) {
1034 unsigned Code = Stream.ReadCode();
1035 switch (Code) {
1036 case llvm::bitc::END_BLOCK:
1037 if (Stream.ReadBlockEnd())
1038 return Error("Error at end of preprocessor block");
1039 return false;
1040
1041 case llvm::bitc::ENTER_SUBBLOCK:
1042 // No known subblocks, always skip them.
1043 Stream.ReadSubBlockID();
1044 if (Stream.SkipBlock())
1045 return Error("Malformed block record");
1046 continue;
1047
1048 case llvm::bitc::DEFINE_ABBREV:
1049 Stream.ReadAbbrevRecord();
1050 continue;
1051 default: break;
1052 }
1053
1054 // Read a record.
1055 Record.clear();
1056 pch::PreprocessorRecordTypes RecType =
1057 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1058 switch (RecType) {
1059 default: // Default behavior: ignore unknown records.
1060 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001061 case pch::PP_COUNTER_VALUE:
1062 if (!Record.empty())
1063 PP.setCounterValue(Record[0]);
1064 break;
1065
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001066 case pch::PP_MACRO_OBJECT_LIKE:
1067 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001068 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1069 if (II == 0)
1070 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001071 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1072 bool isUsed = Record[2];
1073
1074 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1075 MI->setIsUsed(isUsed);
1076
1077 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1078 // Decode function-like macro info.
1079 bool isC99VarArgs = Record[3];
1080 bool isGNUVarArgs = Record[4];
1081 MacroArgs.clear();
1082 unsigned NumArgs = Record[5];
1083 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001084 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001085
1086 // Install function-like macro info.
1087 MI->setIsFunctionLike();
1088 if (isC99VarArgs) MI->setIsC99Varargs();
1089 if (isGNUVarArgs) MI->setIsGNUVarargs();
1090 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1091 PP.getPreprocessorAllocator());
1092 }
1093
1094 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001095 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001096
1097 // Remember that we saw this macro last so that we add the tokens that
1098 // form its body to it.
1099 LastMacro = MI;
1100 break;
1101 }
1102
1103 case pch::PP_TOKEN: {
1104 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1105 // pretend we didn't see this.
1106 if (LastMacro == 0) break;
1107
1108 Token Tok;
1109 Tok.startToken();
1110 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1111 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001112 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1113 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001114 Tok.setKind((tok::TokenKind)Record[3]);
1115 Tok.setFlag((Token::TokenFlags)Record[4]);
1116 LastMacro->AddTokenToBody(Tok);
1117 break;
1118 }
1119 }
1120 }
1121}
1122
Douglas Gregor179cfb12009-04-10 20:39:37 +00001123PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1124 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1125 Error("Malformed block record");
1126 return Failure;
1127 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001128
Chris Lattner29241862009-04-11 21:15:38 +00001129 uint64_t PreprocessorBlockBit = 0;
1130
Douglas Gregorc34897d2009-04-09 22:27:44 +00001131 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001132 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001133 while (!Stream.AtEndOfStream()) {
1134 unsigned Code = Stream.ReadCode();
1135 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001136 // If we saw the preprocessor block, read it now.
1137 if (PreprocessorBlockBit) {
1138 uint64_t SavedPos = Stream.GetCurrentBitNo();
1139 Stream.JumpToBit(PreprocessorBlockBit);
1140 if (ReadPreprocessorBlock()) {
1141 Error("Malformed preprocessor block");
1142 return Failure;
1143 }
1144 Stream.JumpToBit(SavedPos);
1145 }
1146
Douglas Gregor179cfb12009-04-10 20:39:37 +00001147 if (Stream.ReadBlockEnd()) {
1148 Error("Error at end of module block");
1149 return Failure;
1150 }
Chris Lattner29241862009-04-11 21:15:38 +00001151
Douglas Gregor179cfb12009-04-10 20:39:37 +00001152 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001153 }
1154
1155 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1156 switch (Stream.ReadSubBlockID()) {
1157 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1158 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1159 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001160 if (Stream.SkipBlock()) {
1161 Error("Malformed block record");
1162 return Failure;
1163 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001164 break;
1165
Chris Lattner29241862009-04-11 21:15:38 +00001166 case pch::PREPROCESSOR_BLOCK_ID:
1167 // Skip the preprocessor block for now, but remember where it is. We
1168 // want to read it in after the identifier table.
1169 if (PreprocessorBlockBit) {
1170 Error("Multiple preprocessor blocks found.");
1171 return Failure;
1172 }
1173 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1174 if (Stream.SkipBlock()) {
1175 Error("Malformed block record");
1176 return Failure;
1177 }
1178 break;
1179
Douglas Gregorab1cef72009-04-10 03:52:48 +00001180 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001181 switch (ReadSourceManagerBlock()) {
1182 case Success:
1183 break;
1184
1185 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001186 Error("Malformed source manager block");
1187 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001188
1189 case IgnorePCH:
1190 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001191 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001192 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001193 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001194 continue;
1195 }
1196
1197 if (Code == llvm::bitc::DEFINE_ABBREV) {
1198 Stream.ReadAbbrevRecord();
1199 continue;
1200 }
1201
1202 // Read and process a record.
1203 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001204 const char *BlobStart = 0;
1205 unsigned BlobLen = 0;
1206 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1207 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001208 default: // Default behavior: ignore.
1209 break;
1210
1211 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001212 if (!TypeOffsets.empty()) {
1213 Error("Duplicate TYPE_OFFSET record in PCH file");
1214 return Failure;
1215 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001216 TypeOffsets.swap(Record);
1217 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1218 break;
1219
1220 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001221 if (!DeclOffsets.empty()) {
1222 Error("Duplicate DECL_OFFSET record in PCH file");
1223 return Failure;
1224 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001225 DeclOffsets.swap(Record);
1226 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1227 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001228
1229 case pch::LANGUAGE_OPTIONS:
1230 if (ParseLanguageOptions(Record))
1231 return IgnorePCH;
1232 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001233
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001234 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001235 std::string TargetTriple(BlobStart, BlobLen);
1236 if (TargetTriple != Context.Target.getTargetTriple()) {
1237 Diag(diag::warn_pch_target_triple)
1238 << TargetTriple << Context.Target.getTargetTriple();
1239 Diag(diag::note_ignoring_pch) << FileName;
1240 return IgnorePCH;
1241 }
1242 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001243 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001244
1245 case pch::IDENTIFIER_TABLE:
1246 IdentifierTable = BlobStart;
1247 break;
1248
1249 case pch::IDENTIFIER_OFFSET:
1250 if (!IdentifierData.empty()) {
1251 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1252 return Failure;
1253 }
1254 IdentifierData.swap(Record);
1255#ifndef NDEBUG
1256 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1257 if ((IdentifierData[I] & 0x01) == 0) {
1258 Error("Malformed identifier table in the precompiled header");
1259 return Failure;
1260 }
1261 }
1262#endif
1263 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001264
1265 case pch::EXTERNAL_DEFINITIONS:
1266 if (!ExternalDefinitions.empty()) {
1267 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1268 return Failure;
1269 }
1270 ExternalDefinitions.swap(Record);
1271 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001272 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001273 }
1274
Douglas Gregor179cfb12009-04-10 20:39:37 +00001275 Error("Premature end of bitstream");
1276 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001277}
1278
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001279PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001280 // Set the PCH file name.
1281 this->FileName = FileName;
1282
Douglas Gregorc34897d2009-04-09 22:27:44 +00001283 // Open the PCH file.
1284 std::string ErrStr;
1285 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001286 if (!Buffer) {
1287 Error(ErrStr.c_str());
1288 return IgnorePCH;
1289 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001290
1291 // Initialize the stream
1292 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1293 (const unsigned char *)Buffer->getBufferEnd());
1294
1295 // Sniff for the signature.
1296 if (Stream.Read(8) != 'C' ||
1297 Stream.Read(8) != 'P' ||
1298 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001299 Stream.Read(8) != 'H') {
1300 Error("Not a PCH file");
1301 return IgnorePCH;
1302 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001303
1304 // We expect a number of well-defined blocks, though we don't necessarily
1305 // need to understand them all.
1306 while (!Stream.AtEndOfStream()) {
1307 unsigned Code = Stream.ReadCode();
1308
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001309 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1310 Error("Invalid record at top-level");
1311 return Failure;
1312 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001313
1314 unsigned BlockID = Stream.ReadSubBlockID();
1315
1316 // We only know the PCH subblock ID.
1317 switch (BlockID) {
1318 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001319 if (Stream.ReadBlockInfoBlock()) {
1320 Error("Malformed BlockInfoBlock");
1321 return Failure;
1322 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001323 break;
1324 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001325 switch (ReadPCHBlock()) {
1326 case Success:
1327 break;
1328
1329 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001330 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001331
1332 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001333 // FIXME: We could consider reading through to the end of this
1334 // PCH block, skipping subblocks, to see if there are other
1335 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001336 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001337 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001338 break;
1339 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001340 if (Stream.SkipBlock()) {
1341 Error("Malformed block record");
1342 return Failure;
1343 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001344 break;
1345 }
1346 }
1347
1348 // Load the translation unit declaration
1349 ReadDeclRecord(DeclOffsets[0], 0);
1350
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001351 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001352}
1353
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001354namespace {
1355 /// \brief Helper class that saves the current stream position and
1356 /// then restores it when destroyed.
1357 struct VISIBILITY_HIDDEN SavedStreamPosition {
1358 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001359 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001360
1361 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001362 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001363 }
1364
1365 private:
1366 llvm::BitstreamReader &Stream;
1367 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001368 };
1369}
1370
Douglas Gregor179cfb12009-04-10 20:39:37 +00001371/// \brief Parse the record that corresponds to a LangOptions data
1372/// structure.
1373///
1374/// This routine compares the language options used to generate the
1375/// PCH file against the language options set for the current
1376/// compilation. For each option, we classify differences between the
1377/// two compiler states as either "benign" or "important". Benign
1378/// differences don't matter, and we accept them without complaint
1379/// (and without modifying the language options). Differences between
1380/// the states for important options cause the PCH file to be
1381/// unusable, so we emit a warning and return true to indicate that
1382/// there was an error.
1383///
1384/// \returns true if the PCH file is unacceptable, false otherwise.
1385bool PCHReader::ParseLanguageOptions(
1386 const llvm::SmallVectorImpl<uint64_t> &Record) {
1387 const LangOptions &LangOpts = Context.getLangOptions();
1388#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1389#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1390 if (Record[Idx] != LangOpts.Option) { \
1391 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1392 Diag(diag::note_ignoring_pch) << FileName; \
1393 return true; \
1394 } \
1395 ++Idx
1396
1397 unsigned Idx = 0;
1398 PARSE_LANGOPT_BENIGN(Trigraphs);
1399 PARSE_LANGOPT_BENIGN(BCPLComment);
1400 PARSE_LANGOPT_BENIGN(DollarIdents);
1401 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1402 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1403 PARSE_LANGOPT_BENIGN(ImplicitInt);
1404 PARSE_LANGOPT_BENIGN(Digraphs);
1405 PARSE_LANGOPT_BENIGN(HexFloats);
1406 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1407 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1408 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1409 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1410 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1411 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1412 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1413 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1414 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1415 PARSE_LANGOPT_BENIGN(PascalStrings);
1416 PARSE_LANGOPT_BENIGN(Boolean);
1417 PARSE_LANGOPT_BENIGN(WritableStrings);
1418 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1419 diag::warn_pch_lax_vector_conversions);
1420 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1421 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1422 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1423 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1424 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1425 diag::warn_pch_thread_safe_statics);
1426 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1427 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1428 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1429 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1430 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1431 diag::warn_pch_heinous_extensions);
1432 // FIXME: Most of the options below are benign if the macro wasn't
1433 // used. Unfortunately, this means that a PCH compiled without
1434 // optimization can't be used with optimization turned on, even
1435 // though the only thing that changes is whether __OPTIMIZE__ was
1436 // defined... but if __OPTIMIZE__ never showed up in the header, it
1437 // doesn't matter. We could consider making this some special kind
1438 // of check.
1439 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1440 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1441 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1442 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1443 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1444 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1445 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1446 Diag(diag::warn_pch_gc_mode)
1447 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1448 Diag(diag::note_ignoring_pch) << FileName;
1449 return true;
1450 }
1451 ++Idx;
1452 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1453 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1454#undef PARSE_LANGOPT_IRRELEVANT
1455#undef PARSE_LANGOPT_BENIGN
1456
1457 return false;
1458}
1459
Douglas Gregorc34897d2009-04-09 22:27:44 +00001460/// \brief Read and return the type at the given offset.
1461///
1462/// This routine actually reads the record corresponding to the type
1463/// at the given offset in the bitstream. It is a helper routine for
1464/// GetType, which deals with reading type IDs.
1465QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001466 // Keep track of where we are in the stream, then jump back there
1467 // after reading this type.
1468 SavedStreamPosition SavedPosition(Stream);
1469
Douglas Gregorc34897d2009-04-09 22:27:44 +00001470 Stream.JumpToBit(Offset);
1471 RecordData Record;
1472 unsigned Code = Stream.ReadCode();
1473 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001474 case pch::TYPE_EXT_QUAL: {
1475 assert(Record.size() == 3 &&
1476 "Incorrect encoding of extended qualifier type");
1477 QualType Base = GetType(Record[0]);
1478 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1479 unsigned AddressSpace = Record[2];
1480
1481 QualType T = Base;
1482 if (GCAttr != QualType::GCNone)
1483 T = Context.getObjCGCQualType(T, GCAttr);
1484 if (AddressSpace)
1485 T = Context.getAddrSpaceQualType(T, AddressSpace);
1486 return T;
1487 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001488
Douglas Gregorc34897d2009-04-09 22:27:44 +00001489 case pch::TYPE_FIXED_WIDTH_INT: {
1490 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1491 return Context.getFixedWidthIntType(Record[0], Record[1]);
1492 }
1493
1494 case pch::TYPE_COMPLEX: {
1495 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1496 QualType ElemType = GetType(Record[0]);
1497 return Context.getComplexType(ElemType);
1498 }
1499
1500 case pch::TYPE_POINTER: {
1501 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1502 QualType PointeeType = GetType(Record[0]);
1503 return Context.getPointerType(PointeeType);
1504 }
1505
1506 case pch::TYPE_BLOCK_POINTER: {
1507 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1508 QualType PointeeType = GetType(Record[0]);
1509 return Context.getBlockPointerType(PointeeType);
1510 }
1511
1512 case pch::TYPE_LVALUE_REFERENCE: {
1513 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1514 QualType PointeeType = GetType(Record[0]);
1515 return Context.getLValueReferenceType(PointeeType);
1516 }
1517
1518 case pch::TYPE_RVALUE_REFERENCE: {
1519 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1520 QualType PointeeType = GetType(Record[0]);
1521 return Context.getRValueReferenceType(PointeeType);
1522 }
1523
1524 case pch::TYPE_MEMBER_POINTER: {
1525 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1526 QualType PointeeType = GetType(Record[0]);
1527 QualType ClassType = GetType(Record[1]);
1528 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1529 }
1530
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001531 case pch::TYPE_CONSTANT_ARRAY: {
1532 QualType ElementType = GetType(Record[0]);
1533 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1534 unsigned IndexTypeQuals = Record[2];
1535 unsigned Idx = 3;
1536 llvm::APInt Size = ReadAPInt(Record, Idx);
1537 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1538 }
1539
1540 case pch::TYPE_INCOMPLETE_ARRAY: {
1541 QualType ElementType = GetType(Record[0]);
1542 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1543 unsigned IndexTypeQuals = Record[2];
1544 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1545 }
1546
1547 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001548 QualType ElementType = GetType(Record[0]);
1549 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1550 unsigned IndexTypeQuals = Record[2];
1551 return Context.getVariableArrayType(ElementType, ReadExpr(),
1552 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001553 }
1554
1555 case pch::TYPE_VECTOR: {
1556 if (Record.size() != 2) {
1557 Error("Incorrect encoding of vector type in PCH file");
1558 return QualType();
1559 }
1560
1561 QualType ElementType = GetType(Record[0]);
1562 unsigned NumElements = Record[1];
1563 return Context.getVectorType(ElementType, NumElements);
1564 }
1565
1566 case pch::TYPE_EXT_VECTOR: {
1567 if (Record.size() != 2) {
1568 Error("Incorrect encoding of extended vector type in PCH file");
1569 return QualType();
1570 }
1571
1572 QualType ElementType = GetType(Record[0]);
1573 unsigned NumElements = Record[1];
1574 return Context.getExtVectorType(ElementType, NumElements);
1575 }
1576
1577 case pch::TYPE_FUNCTION_NO_PROTO: {
1578 if (Record.size() != 1) {
1579 Error("Incorrect encoding of no-proto function type");
1580 return QualType();
1581 }
1582 QualType ResultType = GetType(Record[0]);
1583 return Context.getFunctionNoProtoType(ResultType);
1584 }
1585
1586 case pch::TYPE_FUNCTION_PROTO: {
1587 QualType ResultType = GetType(Record[0]);
1588 unsigned Idx = 1;
1589 unsigned NumParams = Record[Idx++];
1590 llvm::SmallVector<QualType, 16> ParamTypes;
1591 for (unsigned I = 0; I != NumParams; ++I)
1592 ParamTypes.push_back(GetType(Record[Idx++]));
1593 bool isVariadic = Record[Idx++];
1594 unsigned Quals = Record[Idx++];
1595 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1596 isVariadic, Quals);
1597 }
1598
1599 case pch::TYPE_TYPEDEF:
1600 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1601 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1602
1603 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001604 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001605
1606 case pch::TYPE_TYPEOF: {
1607 if (Record.size() != 1) {
1608 Error("Incorrect encoding of typeof(type) in PCH file");
1609 return QualType();
1610 }
1611 QualType UnderlyingType = GetType(Record[0]);
1612 return Context.getTypeOfType(UnderlyingType);
1613 }
1614
1615 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001616 assert(Record.size() == 1 && "Incorrect encoding of record type");
1617 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001618
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001619 case pch::TYPE_ENUM:
1620 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1621 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1622
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001623 case pch::TYPE_OBJC_INTERFACE:
1624 // FIXME: Deserialize ObjCInterfaceType
1625 assert(false && "Cannot de-serialize ObjC interface types yet");
1626 return QualType();
1627
1628 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1629 // FIXME: Deserialize ObjCQualifiedInterfaceType
1630 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1631 return QualType();
1632
1633 case pch::TYPE_OBJC_QUALIFIED_ID:
1634 // FIXME: Deserialize ObjCQualifiedIdType
1635 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1636 return QualType();
1637
1638 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1639 // FIXME: Deserialize ObjCQualifiedClassType
1640 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1641 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001642 }
1643
1644 // Suppress a GCC warning
1645 return QualType();
1646}
1647
1648/// \brief Note that we have loaded the declaration with the given
1649/// Index.
1650///
1651/// This routine notes that this declaration has already been loaded,
1652/// so that future GetDecl calls will return this declaration rather
1653/// than trying to load a new declaration.
1654inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1655 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1656 DeclAlreadyLoaded[Index] = true;
1657 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1658}
1659
1660/// \brief Read the declaration at the given offset from the PCH file.
1661Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001662 // Keep track of where we are in the stream, then jump back there
1663 // after reading this declaration.
1664 SavedStreamPosition SavedPosition(Stream);
1665
Douglas Gregorc34897d2009-04-09 22:27:44 +00001666 Decl *D = 0;
1667 Stream.JumpToBit(Offset);
1668 RecordData Record;
1669 unsigned Code = Stream.ReadCode();
1670 unsigned Idx = 0;
1671 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001672
Douglas Gregorc34897d2009-04-09 22:27:44 +00001673 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001674 case pch::DECL_ATTR:
1675 case pch::DECL_CONTEXT_LEXICAL:
1676 case pch::DECL_CONTEXT_VISIBLE:
1677 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1678 break;
1679
Douglas Gregorc34897d2009-04-09 22:27:44 +00001680 case pch::DECL_TRANSLATION_UNIT:
1681 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001682 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001683 break;
1684
1685 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001686 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001687 break;
1688 }
1689
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001690 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001691 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001692 break;
1693 }
1694
Douglas Gregor982365e2009-04-13 21:20:57 +00001695 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001696 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1697 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001698 break;
1699 }
1700
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001701 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001702 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1703 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001704 break;
1705 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001706
1707 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001708 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1709 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001710 break;
1711 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001712
Douglas Gregor982365e2009-04-13 21:20:57 +00001713 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001714 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1715 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001716 break;
1717 }
1718
Douglas Gregorc34897d2009-04-09 22:27:44 +00001719 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001720 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1721 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001722 break;
1723 }
1724
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001725 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001726 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1727 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001728 break;
1729 }
1730
1731 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001732 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001733 QualType(), QualType(), VarDecl::None,
1734 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001735 break;
1736 }
1737
Douglas Gregor2a491792009-04-13 22:49:25 +00001738 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001739 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001740 break;
1741 }
1742
1743 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001744 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001745 break;
1746 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001747 }
1748
Douglas Gregorddf4d092009-04-16 22:29:51 +00001749 assert(D && "Unknown declaration creating PCH file");
1750 if (D) {
1751 LoadedDecl(Index, D);
1752 Reader.Visit(D);
1753 }
1754
Douglas Gregorc34897d2009-04-09 22:27:44 +00001755 // If this declaration is also a declaration context, get the
1756 // offsets for its tables of lexical and visible declarations.
1757 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1758 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1759 if (Offsets.first || Offsets.second) {
1760 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1761 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1762 DeclContextOffsets[DC] = Offsets;
1763 }
1764 }
1765 assert(Idx == Record.size());
1766
1767 return D;
1768}
1769
Douglas Gregorac8f2802009-04-10 17:25:41 +00001770QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001771 unsigned Quals = ID & 0x07;
1772 unsigned Index = ID >> 3;
1773
1774 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1775 QualType T;
1776 switch ((pch::PredefinedTypeIDs)Index) {
1777 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1778 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1779 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1780
1781 case pch::PREDEF_TYPE_CHAR_U_ID:
1782 case pch::PREDEF_TYPE_CHAR_S_ID:
1783 // FIXME: Check that the signedness of CharTy is correct!
1784 T = Context.CharTy;
1785 break;
1786
1787 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1788 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1789 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1790 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1791 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1792 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1793 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1794 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1795 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1796 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1797 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1798 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1799 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1800 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1801 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1802 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1803 }
1804
1805 assert(!T.isNull() && "Unknown predefined type");
1806 return T.getQualifiedType(Quals);
1807 }
1808
1809 Index -= pch::NUM_PREDEF_TYPE_IDS;
1810 if (!TypeAlreadyLoaded[Index]) {
1811 // Load the type from the PCH file.
1812 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1813 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1814 TypeAlreadyLoaded[Index] = true;
1815 }
1816
1817 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1818}
1819
Douglas Gregorac8f2802009-04-10 17:25:41 +00001820Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001821 if (ID == 0)
1822 return 0;
1823
1824 unsigned Index = ID - 1;
1825 if (DeclAlreadyLoaded[Index])
1826 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1827
1828 // Load the declaration from the PCH file.
1829 return ReadDeclRecord(DeclOffsets[Index], Index);
1830}
1831
1832bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001833 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001834 assert(DC->hasExternalLexicalStorage() &&
1835 "DeclContext has no lexical decls in storage");
1836 uint64_t Offset = DeclContextOffsets[DC].first;
1837 assert(Offset && "DeclContext has no lexical decls in storage");
1838
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001839 // Keep track of where we are in the stream, then jump back there
1840 // after reading this context.
1841 SavedStreamPosition SavedPosition(Stream);
1842
Douglas Gregorc34897d2009-04-09 22:27:44 +00001843 // Load the record containing all of the declarations lexically in
1844 // this context.
1845 Stream.JumpToBit(Offset);
1846 RecordData Record;
1847 unsigned Code = Stream.ReadCode();
1848 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001849 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001850 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1851
1852 // Load all of the declaration IDs
1853 Decls.clear();
1854 Decls.insert(Decls.end(), Record.begin(), Record.end());
1855 return false;
1856}
1857
1858bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1859 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1860 assert(DC->hasExternalVisibleStorage() &&
1861 "DeclContext has no visible decls in storage");
1862 uint64_t Offset = DeclContextOffsets[DC].second;
1863 assert(Offset && "DeclContext has no visible decls in storage");
1864
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001865 // Keep track of where we are in the stream, then jump back there
1866 // after reading this context.
1867 SavedStreamPosition SavedPosition(Stream);
1868
Douglas Gregorc34897d2009-04-09 22:27:44 +00001869 // Load the record containing all of the declarations visible in
1870 // this context.
1871 Stream.JumpToBit(Offset);
1872 RecordData Record;
1873 unsigned Code = Stream.ReadCode();
1874 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001875 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001876 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1877 if (Record.size() == 0)
1878 return false;
1879
1880 Decls.clear();
1881
1882 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001883 while (Idx < Record.size()) {
1884 Decls.push_back(VisibleDeclaration());
1885 Decls.back().Name = ReadDeclarationName(Record, Idx);
1886
Douglas Gregorc34897d2009-04-09 22:27:44 +00001887 unsigned Size = Record[Idx++];
1888 llvm::SmallVector<unsigned, 4> & LoadedDecls
1889 = Decls.back().Declarations;
1890 LoadedDecls.reserve(Size);
1891 for (unsigned I = 0; I < Size; ++I)
1892 LoadedDecls.push_back(Record[Idx++]);
1893 }
1894
1895 return false;
1896}
1897
Douglas Gregor631f6c62009-04-14 00:24:19 +00001898void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1899 if (!Consumer)
1900 return;
1901
1902 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1903 Decl *D = GetDecl(ExternalDefinitions[I]);
1904 DeclGroupRef DG(D);
1905 Consumer->HandleTopLevelDecl(DG);
1906 }
1907}
1908
Douglas Gregorc34897d2009-04-09 22:27:44 +00001909void PCHReader::PrintStats() {
1910 std::fprintf(stderr, "*** PCH Statistics:\n");
1911
1912 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1913 TypeAlreadyLoaded.end(),
1914 true);
1915 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1916 DeclAlreadyLoaded.end(),
1917 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001918 unsigned NumIdentifiersLoaded = 0;
1919 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1920 if ((IdentifierData[I] & 0x01) == 0)
1921 ++NumIdentifiersLoaded;
1922 }
1923
Douglas Gregorc34897d2009-04-09 22:27:44 +00001924 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1925 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001926 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001927 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1928 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001929 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1930 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1931 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1932 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001933 std::fprintf(stderr, "\n");
1934}
1935
Chris Lattner29241862009-04-11 21:15:38 +00001936IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001937 if (ID == 0)
1938 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001939
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001940 if (!IdentifierTable || IdentifierData.empty()) {
1941 Error("No identifier table in PCH file");
1942 return 0;
1943 }
Chris Lattner29241862009-04-11 21:15:38 +00001944
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001945 if (IdentifierData[ID - 1] & 0x01) {
1946 uint64_t Offset = IdentifierData[ID - 1];
1947 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001948 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001949 }
Chris Lattner29241862009-04-11 21:15:38 +00001950
1951 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001952}
1953
1954DeclarationName
1955PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1956 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1957 switch (Kind) {
1958 case DeclarationName::Identifier:
1959 return DeclarationName(GetIdentifierInfo(Record, Idx));
1960
1961 case DeclarationName::ObjCZeroArgSelector:
1962 case DeclarationName::ObjCOneArgSelector:
1963 case DeclarationName::ObjCMultiArgSelector:
1964 assert(false && "Unable to de-serialize Objective-C selectors");
1965 break;
1966
1967 case DeclarationName::CXXConstructorName:
1968 return Context.DeclarationNames.getCXXConstructorName(
1969 GetType(Record[Idx++]));
1970
1971 case DeclarationName::CXXDestructorName:
1972 return Context.DeclarationNames.getCXXDestructorName(
1973 GetType(Record[Idx++]));
1974
1975 case DeclarationName::CXXConversionFunctionName:
1976 return Context.DeclarationNames.getCXXConversionFunctionName(
1977 GetType(Record[Idx++]));
1978
1979 case DeclarationName::CXXOperatorName:
1980 return Context.DeclarationNames.getCXXOperatorName(
1981 (OverloadedOperatorKind)Record[Idx++]);
1982
1983 case DeclarationName::CXXUsingDirective:
1984 return DeclarationName::getUsingDirectiveName();
1985 }
1986
1987 // Required to silence GCC warning
1988 return DeclarationName();
1989}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001990
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001991/// \brief Read an integral value
1992llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1993 unsigned BitWidth = Record[Idx++];
1994 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1995 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1996 Idx += NumWords;
1997 return Result;
1998}
1999
2000/// \brief Read a signed integral value
2001llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2002 bool isUnsigned = Record[Idx++];
2003 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2004}
2005
Douglas Gregore2f37202009-04-14 21:55:33 +00002006/// \brief Read a floating-point value
2007llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002008 return llvm::APFloat(ReadAPInt(Record, Idx));
2009}
2010
Douglas Gregor1c507882009-04-15 21:30:51 +00002011// \brief Read a string
2012std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2013 unsigned Len = Record[Idx++];
2014 std::string Result(&Record[Idx], &Record[Idx] + Len);
2015 Idx += Len;
2016 return Result;
2017}
2018
2019/// \brief Reads attributes from the current stream position.
2020Attr *PCHReader::ReadAttributes() {
2021 unsigned Code = Stream.ReadCode();
2022 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2023 "Expected unabbreviated record"); (void)Code;
2024
2025 RecordData Record;
2026 unsigned Idx = 0;
2027 unsigned RecCode = Stream.ReadRecord(Code, Record);
2028 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2029 (void)RecCode;
2030
2031#define SIMPLE_ATTR(Name) \
2032 case Attr::Name: \
2033 New = ::new (Context) Name##Attr(); \
2034 break
2035
2036#define STRING_ATTR(Name) \
2037 case Attr::Name: \
2038 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2039 break
2040
2041#define UNSIGNED_ATTR(Name) \
2042 case Attr::Name: \
2043 New = ::new (Context) Name##Attr(Record[Idx++]); \
2044 break
2045
2046 Attr *Attrs = 0;
2047 while (Idx < Record.size()) {
2048 Attr *New = 0;
2049 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2050 bool IsInherited = Record[Idx++];
2051
2052 switch (Kind) {
2053 STRING_ATTR(Alias);
2054 UNSIGNED_ATTR(Aligned);
2055 SIMPLE_ATTR(AlwaysInline);
2056 SIMPLE_ATTR(AnalyzerNoReturn);
2057 STRING_ATTR(Annotate);
2058 STRING_ATTR(AsmLabel);
2059
2060 case Attr::Blocks:
2061 New = ::new (Context) BlocksAttr(
2062 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2063 break;
2064
2065 case Attr::Cleanup:
2066 New = ::new (Context) CleanupAttr(
2067 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2068 break;
2069
2070 SIMPLE_ATTR(Const);
2071 UNSIGNED_ATTR(Constructor);
2072 SIMPLE_ATTR(DLLExport);
2073 SIMPLE_ATTR(DLLImport);
2074 SIMPLE_ATTR(Deprecated);
2075 UNSIGNED_ATTR(Destructor);
2076 SIMPLE_ATTR(FastCall);
2077
2078 case Attr::Format: {
2079 std::string Type = ReadString(Record, Idx);
2080 unsigned FormatIdx = Record[Idx++];
2081 unsigned FirstArg = Record[Idx++];
2082 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2083 break;
2084 }
2085
2086 SIMPLE_ATTR(GNUCInline);
2087
2088 case Attr::IBOutletKind:
2089 New = ::new (Context) IBOutletAttr();
2090 break;
2091
2092 SIMPLE_ATTR(NoReturn);
2093 SIMPLE_ATTR(NoThrow);
2094 SIMPLE_ATTR(Nodebug);
2095 SIMPLE_ATTR(Noinline);
2096
2097 case Attr::NonNull: {
2098 unsigned Size = Record[Idx++];
2099 llvm::SmallVector<unsigned, 16> ArgNums;
2100 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2101 Idx += Size;
2102 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2103 break;
2104 }
2105
2106 SIMPLE_ATTR(ObjCException);
2107 SIMPLE_ATTR(ObjCNSObject);
2108 SIMPLE_ATTR(Overloadable);
2109 UNSIGNED_ATTR(Packed);
2110 SIMPLE_ATTR(Pure);
2111 UNSIGNED_ATTR(Regparm);
2112 STRING_ATTR(Section);
2113 SIMPLE_ATTR(StdCall);
2114 SIMPLE_ATTR(TransparentUnion);
2115 SIMPLE_ATTR(Unavailable);
2116 SIMPLE_ATTR(Unused);
2117 SIMPLE_ATTR(Used);
2118
2119 case Attr::Visibility:
2120 New = ::new (Context) VisibilityAttr(
2121 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2122 break;
2123
2124 SIMPLE_ATTR(WarnUnusedResult);
2125 SIMPLE_ATTR(Weak);
2126 SIMPLE_ATTR(WeakImport);
2127 }
2128
2129 assert(New && "Unable to decode attribute?");
2130 New->setInherited(IsInherited);
2131 New->setNext(Attrs);
2132 Attrs = New;
2133 }
2134#undef UNSIGNED_ATTR
2135#undef STRING_ATTR
2136#undef SIMPLE_ATTR
2137
2138 // The list of attributes was built backwards. Reverse the list
2139 // before returning it.
2140 Attr *PrevAttr = 0, *NextAttr = 0;
2141 while (Attrs) {
2142 NextAttr = Attrs->getNext();
2143 Attrs->setNext(PrevAttr);
2144 PrevAttr = Attrs;
2145 Attrs = NextAttr;
2146 }
2147
2148 return PrevAttr;
2149}
2150
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002151Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002152 // Within the bitstream, expressions are stored in Reverse Polish
2153 // Notation, with each of the subexpressions preceding the
2154 // expression they are stored in. To evaluate expressions, we
2155 // continue reading expressions and placing them on the stack, with
2156 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002157 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002158 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002159 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002160 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002161 llvm::SmallVector<Stmt *, 16> StmtStack;
2162 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002163 Stmt::EmptyShell Empty;
2164
Douglas Gregora151ba42009-04-14 23:32:43 +00002165 while (true) {
2166 unsigned Code = Stream.ReadCode();
2167 if (Code == llvm::bitc::END_BLOCK) {
2168 if (Stream.ReadBlockEnd()) {
2169 Error("Error at end of Source Manager block");
2170 return 0;
2171 }
2172 break;
2173 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002174
Douglas Gregora151ba42009-04-14 23:32:43 +00002175 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2176 // No known subblocks, always skip them.
2177 Stream.ReadSubBlockID();
2178 if (Stream.SkipBlock()) {
2179 Error("Malformed block record");
2180 return 0;
2181 }
2182 continue;
2183 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002184
Douglas Gregora151ba42009-04-14 23:32:43 +00002185 if (Code == llvm::bitc::DEFINE_ABBREV) {
2186 Stream.ReadAbbrevRecord();
2187 continue;
2188 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002189
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002190 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002191 Idx = 0;
2192 Record.clear();
2193 bool Finished = false;
2194 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002195 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002196 Finished = true;
2197 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002198
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002199 case pch::STMT_NULL_PTR:
2200 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002201 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002202
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002203 case pch::STMT_NULL:
2204 S = new (Context) NullStmt(Empty);
2205 break;
2206
2207 case pch::STMT_COMPOUND:
2208 S = new (Context) CompoundStmt(Empty);
2209 break;
2210
2211 case pch::STMT_CASE:
2212 S = new (Context) CaseStmt(Empty);
2213 break;
2214
2215 case pch::STMT_DEFAULT:
2216 S = new (Context) DefaultStmt(Empty);
2217 break;
2218
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002219 case pch::STMT_LABEL:
2220 S = new (Context) LabelStmt(Empty);
2221 break;
2222
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002223 case pch::STMT_IF:
2224 S = new (Context) IfStmt(Empty);
2225 break;
2226
2227 case pch::STMT_SWITCH:
2228 S = new (Context) SwitchStmt(Empty);
2229 break;
2230
Douglas Gregora6b503f2009-04-17 00:16:09 +00002231 case pch::STMT_WHILE:
2232 S = new (Context) WhileStmt(Empty);
2233 break;
2234
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002235 case pch::STMT_DO:
2236 S = new (Context) DoStmt(Empty);
2237 break;
2238
2239 case pch::STMT_FOR:
2240 S = new (Context) ForStmt(Empty);
2241 break;
2242
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002243 case pch::STMT_GOTO:
2244 S = new (Context) GotoStmt(Empty);
2245 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002246
2247 case pch::STMT_INDIRECT_GOTO:
2248 S = new (Context) IndirectGotoStmt(Empty);
2249 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002250
Douglas Gregora6b503f2009-04-17 00:16:09 +00002251 case pch::STMT_CONTINUE:
2252 S = new (Context) ContinueStmt(Empty);
2253 break;
2254
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002255 case pch::STMT_BREAK:
2256 S = new (Context) BreakStmt(Empty);
2257 break;
2258
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002259 case pch::STMT_RETURN:
2260 S = new (Context) ReturnStmt(Empty);
2261 break;
2262
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002263 case pch::STMT_DECL:
2264 S = new (Context) DeclStmt(Empty);
2265 break;
2266
Douglas Gregora151ba42009-04-14 23:32:43 +00002267 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002268 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002269 break;
2270
2271 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002272 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002273 break;
2274
2275 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002276 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002277 break;
2278
2279 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002280 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002281 break;
2282
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002283 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002284 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002285 break;
2286
Douglas Gregor596e0932009-04-15 16:35:07 +00002287 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002288 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002289 Record[PCHStmtReader::NumExprFields + 1]);
2290 break;
2291
Douglas Gregora151ba42009-04-14 23:32:43 +00002292 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002293 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002294 break;
2295
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002296 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002297 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002298 break;
2299
Douglas Gregor12d74052009-04-15 15:58:59 +00002300 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002301 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002302 break;
2303
2304 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002305 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002306 break;
2307
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002308 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002309 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002310 break;
2311
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002312 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002313 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002314 break;
2315
2316 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002317 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002318 break;
2319
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002320 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002321 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002322 break;
2323
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002324 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002325 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002326 break;
2327
2328 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002329 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002330 break;
2331
Douglas Gregora151ba42009-04-14 23:32:43 +00002332 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002333 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002334 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002335
2336 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002337 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002338 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002339
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002340 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002341 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002342 break;
2343
Douglas Gregorec0b8292009-04-15 23:02:49 +00002344 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002345 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002346 break;
2347
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002348 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002349 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002350 break;
2351
2352 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002353 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002354 Record[PCHStmtReader::NumExprFields] - 1);
2355
2356 break;
2357
2358 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002359 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002360 break;
2361
Douglas Gregorec0b8292009-04-15 23:02:49 +00002362 case pch::EXPR_VA_ARG:
2363 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002364 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002365 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002366
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002367 case pch::EXPR_ADDR_LABEL:
2368 S = new (Context) AddrLabelExpr(Empty);
2369 break;
2370
Douglas Gregoreca12f62009-04-17 19:05:30 +00002371 case pch::EXPR_STMT:
2372 S = new (Context) StmtExpr(Empty);
2373 break;
2374
Douglas Gregor209d4622009-04-15 23:33:31 +00002375 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002376 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002377 break;
2378
2379 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002380 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002381 break;
2382
2383 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002384 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002385 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002386
2387 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002388 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002389 break;
2390
2391 case pch::EXPR_BLOCK_DECL_REF:
2392 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002393 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002394 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002395 }
2396
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002397 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002398 if (Finished)
2399 break;
2400
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002401 if (S) {
2402 unsigned NumSubStmts = Reader.Visit(S);
2403 while (NumSubStmts > 0) {
2404 StmtStack.pop_back();
2405 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002406 }
2407 }
2408
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002409 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002410 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002411 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002412 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002413 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002414 return StmtStack.back();
2415}
2416
2417Expr *PCHReader::ReadExpr() {
2418 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002419}
2420
Douglas Gregor179cfb12009-04-10 20:39:37 +00002421DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002422 return Diag(SourceLocation(), DiagID);
2423}
2424
2425DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2426 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002427 Context.getSourceManager()),
2428 DiagID);
2429}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002430
2431/// \brief Record that the given ID maps to the given switch-case
2432/// statement.
2433void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2434 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2435 SwitchCaseStmts[ID] = SC;
2436}
2437
2438/// \brief Retrieve the switch-case statement with the given ID.
2439SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2440 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2441 return SwitchCaseStmts[ID];
2442}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002443
2444/// \brief Record that the given label statement has been
2445/// deserialized and has the given ID.
2446void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2447 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2448 "Deserialized label twice");
2449 LabelStmts[ID] = S;
2450
2451 // If we've already seen any goto statements that point to this
2452 // label, resolve them now.
2453 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2454 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2455 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2456 Goto->second->setLabel(S);
2457 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002458
2459 // If we've already seen any address-label statements that point to
2460 // this label, resolve them now.
2461 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2462 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2463 = UnresolvedAddrLabelExprs.equal_range(ID);
2464 for (AddrLabelIter AddrLabel = AddrLabels.first;
2465 AddrLabel != AddrLabels.second; ++AddrLabel)
2466 AddrLabel->second->setLabel(S);
2467 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002468}
2469
2470/// \brief Set the label of the given statement to the label
2471/// identified by ID.
2472///
2473/// Depending on the order in which the label and other statements
2474/// referencing that label occur, this operation may complete
2475/// immediately (updating the statement) or it may queue the
2476/// statement to be back-patched later.
2477void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2478 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2479 if (Label != LabelStmts.end()) {
2480 // We've already seen this label, so set the label of the goto and
2481 // we're done.
2482 S->setLabel(Label->second);
2483 } else {
2484 // We haven't seen this label yet, so add this goto to the set of
2485 // unresolved goto statements.
2486 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2487 }
2488}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002489
2490/// \brief Set the label of the given expression to the label
2491/// identified by ID.
2492///
2493/// Depending on the order in which the label and other statements
2494/// referencing that label occur, this operation may complete
2495/// immediately (updating the statement) or it may queue the
2496/// statement to be back-patched later.
2497void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2498 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2499 if (Label != LabelStmts.end()) {
2500 // We've already seen this label, so set the label of the
2501 // label-address expression and we're done.
2502 S->setLabel(Label->second);
2503 } else {
2504 // We haven't seen this label yet, so add this label-address
2505 // expression to the set of unresolved label-address expressions.
2506 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2507 }
2508}