blob: 6c79b8a5a57d6c3ad7b71c10a07740effea1d378 [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++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000143 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor982365e2009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000167}
168
Douglas Gregorc34897d2009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000180}
181
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor2a491792009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000200 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000201 unsigned NumParams = Record[Idx++];
202 llvm::SmallVector<ParmVarDecl *, 16> Params;
203 Params.reserve(NumParams);
204 for (unsigned I = 0; I != NumParams; ++I)
205 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
206 BD->setParams(Reader.getContext(), &Params[0], NumParams);
207}
208
Douglas Gregorc34897d2009-04-09 22:27:44 +0000209std::pair<uint64_t, uint64_t>
210PCHDeclReader::VisitDeclContext(DeclContext *DC) {
211 uint64_t LexicalOffset = Record[Idx++];
212 uint64_t VisibleOffset = 0;
213 if (DC->getPrimaryContext() == DC)
214 VisibleOffset = Record[Idx++];
215 return std::make_pair(LexicalOffset, VisibleOffset);
216}
217
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000218//===----------------------------------------------------------------------===//
219// Statement/expression deserialization
220//===----------------------------------------------------------------------===//
221namespace {
222 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000223 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000224 PCHReader &Reader;
225 const PCHReader::RecordData &Record;
226 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000227 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000228
229 public:
230 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000231 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
232 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000233
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000234 /// \brief The number of record fields required for the Stmt class
235 /// itself.
236 static const unsigned NumStmtFields = 0;
237
Douglas Gregor596e0932009-04-15 16:35:07 +0000238 /// \brief The number of record fields required for the Expr class
239 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000240 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000241
Douglas Gregora151ba42009-04-14 23:32:43 +0000242 // Each of the Visit* functions reads in part of the expression
243 // from the given record and the current expression stack, then
244 // return the total number of operands that it read from the
245 // expression stack.
246
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000247 unsigned VisitStmt(Stmt *S);
248 unsigned VisitNullStmt(NullStmt *S);
249 unsigned VisitCompoundStmt(CompoundStmt *S);
250 unsigned VisitSwitchCase(SwitchCase *S);
251 unsigned VisitCaseStmt(CaseStmt *S);
252 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000253 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000254 unsigned VisitIfStmt(IfStmt *S);
255 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000256 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000257 unsigned VisitDoStmt(DoStmt *S);
258 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000259 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000260 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000261 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000262 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000263 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000264 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000265 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000266 unsigned VisitExpr(Expr *E);
267 unsigned VisitPredefinedExpr(PredefinedExpr *E);
268 unsigned VisitDeclRefExpr(DeclRefExpr *E);
269 unsigned VisitIntegerLiteral(IntegerLiteral *E);
270 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000271 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000272 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000273 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000274 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000275 unsigned VisitUnaryOperator(UnaryOperator *E);
276 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000277 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000278 unsigned VisitCallExpr(CallExpr *E);
279 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000280 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000281 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000282 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
283 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000284 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000285 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
286 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000287 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000288 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000289 unsigned VisitInitListExpr(InitListExpr *E);
290 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
291 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000292 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000293 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000294 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000295 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
296 unsigned VisitChooseExpr(ChooseExpr *E);
297 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000298 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000299 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000300 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000301 };
302}
303
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000304unsigned PCHStmtReader::VisitStmt(Stmt *S) {
305 assert(Idx == NumStmtFields && "Incorrect statement field count");
306 return 0;
307}
308
309unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
310 VisitStmt(S);
311 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
312 return 0;
313}
314
315unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
316 VisitStmt(S);
317 unsigned NumStmts = Record[Idx++];
318 S->setStmts(Reader.getContext(),
319 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
320 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
321 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
322 return NumStmts;
323}
324
325unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
326 VisitStmt(S);
327 Reader.RecordSwitchCaseID(S, Record[Idx++]);
328 return 0;
329}
330
331unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
332 VisitSwitchCase(S);
333 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
334 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
335 S->setSubStmt(StmtStack.back());
336 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 return 3;
338}
339
340unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
341 VisitSwitchCase(S);
342 S->setSubStmt(StmtStack.back());
343 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
344 return 1;
345}
346
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000347unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
348 VisitStmt(S);
349 S->setID(Reader.GetIdentifierInfo(Record, Idx));
350 S->setSubStmt(StmtStack.back());
351 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 Reader.RecordLabelStmt(S, Record[Idx++]);
353 return 1;
354}
355
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000356unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
357 VisitStmt(S);
358 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
359 S->setThen(StmtStack[StmtStack.size() - 2]);
360 S->setElse(StmtStack[StmtStack.size() - 1]);
361 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
362 return 3;
363}
364
365unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
366 VisitStmt(S);
367 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
368 S->setBody(StmtStack.back());
369 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
370 SwitchCase *PrevSC = 0;
371 for (unsigned N = Record.size(); Idx != N; ++Idx) {
372 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
373 if (PrevSC)
374 PrevSC->setNextSwitchCase(SC);
375 else
376 S->setSwitchCaseList(SC);
377 PrevSC = SC;
378 }
379 return 2;
380}
381
Douglas Gregora6b503f2009-04-17 00:16:09 +0000382unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
383 VisitStmt(S);
384 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
385 S->setBody(StmtStack.back());
386 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
387 return 2;
388}
389
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000390unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
391 VisitStmt(S);
392 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
393 S->setBody(StmtStack.back());
394 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
395 return 2;
396}
397
398unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
399 VisitStmt(S);
400 S->setInit(StmtStack[StmtStack.size() - 4]);
401 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
402 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
403 S->setBody(StmtStack.back());
404 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
405 return 4;
406}
407
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000408unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
409 VisitStmt(S);
410 Reader.SetLabelOf(S, Record[Idx++]);
411 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
412 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 return 0;
414}
415
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000416unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
417 VisitStmt(S);
418 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
419 return 1;
420}
421
Douglas Gregora6b503f2009-04-17 00:16:09 +0000422unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
423 VisitStmt(S);
424 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
425 return 0;
426}
427
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000428unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
429 VisitStmt(S);
430 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
431 return 0;
432}
433
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000434unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
435 VisitStmt(S);
436 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
437 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
438 return 1;
439}
440
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000441unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
442 VisitStmt(S);
443 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
444 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
445
446 if (Idx + 1 == Record.size()) {
447 // Single declaration
448 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
449 } else {
450 llvm::SmallVector<Decl *, 16> Decls;
451 Decls.reserve(Record.size() - Idx);
452 for (unsigned N = Record.size(); Idx != N; ++Idx)
453 Decls.push_back(Reader.GetDecl(Record[Idx]));
454 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
455 &Decls[0], Decls.size())));
456 }
457 return 0;
458}
459
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000460unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
461 VisitStmt(S);
462 unsigned NumOutputs = Record[Idx++];
463 unsigned NumInputs = Record[Idx++];
464 unsigned NumClobbers = Record[Idx++];
465 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
466 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
467 S->setVolatile(Record[Idx++]);
468 S->setSimple(Record[Idx++]);
469
470 unsigned StackIdx
471 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
472 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
473
474 // Outputs and inputs
475 llvm::SmallVector<std::string, 16> Names;
476 llvm::SmallVector<StringLiteral*, 16> Constraints;
477 llvm::SmallVector<Stmt*, 16> Exprs;
478 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
479 Names.push_back(Reader.ReadString(Record, Idx));
480 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
481 Exprs.push_back(StmtStack[StackIdx++]);
482 }
483 S->setOutputsAndInputs(NumOutputs, NumInputs,
484 &Names[0], &Constraints[0], &Exprs[0]);
485
486 // Constraints
487 llvm::SmallVector<StringLiteral*, 16> Clobbers;
488 for (unsigned I = 0; I != NumClobbers; ++I)
489 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
490 S->setClobbers(&Clobbers[0], NumClobbers);
491
492 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
493 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
494}
495
Douglas Gregora151ba42009-04-14 23:32:43 +0000496unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000497 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000498 E->setType(Reader.GetType(Record[Idx++]));
499 E->setTypeDependent(Record[Idx++]);
500 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000501 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000502 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000503}
504
Douglas Gregora151ba42009-04-14 23:32:43 +0000505unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000506 VisitExpr(E);
507 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000509 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000510}
511
Douglas Gregora151ba42009-04-14 23:32:43 +0000512unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000513 VisitExpr(E);
514 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
515 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000516 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000517}
518
Douglas Gregora151ba42009-04-14 23:32:43 +0000519unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000520 VisitExpr(E);
521 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
522 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000523 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000524}
525
Douglas Gregora151ba42009-04-14 23:32:43 +0000526unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000527 VisitExpr(E);
528 E->setValue(Reader.ReadAPFloat(Record, Idx));
529 E->setExact(Record[Idx++]);
530 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000531 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000532}
533
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000534unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
535 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000536 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000537 return 1;
538}
539
Douglas Gregor596e0932009-04-15 16:35:07 +0000540unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
541 VisitExpr(E);
542 unsigned Len = Record[Idx++];
543 assert(Record[Idx] == E->getNumConcatenated() &&
544 "Wrong number of concatenated tokens!");
545 ++Idx;
546 E->setWide(Record[Idx++]);
547
548 // Read string data
549 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
550 E->setStrData(Reader.getContext(), &Str[0], Len);
551 Idx += Len;
552
553 // Read source locations
554 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
555 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
556
557 return 0;
558}
559
Douglas Gregora151ba42009-04-14 23:32:43 +0000560unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000561 VisitExpr(E);
562 E->setValue(Record[Idx++]);
563 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
564 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000565 return 0;
566}
567
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000568unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
569 VisitExpr(E);
570 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
571 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000572 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000573 return 1;
574}
575
Douglas Gregor12d74052009-04-15 15:58:59 +0000576unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
577 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000578 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000579 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
580 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 return 1;
582}
583
584unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
585 VisitExpr(E);
586 E->setSizeof(Record[Idx++]);
587 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000588 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000589 ++Idx;
590 } else {
591 E->setArgument(Reader.GetType(Record[Idx++]));
592 }
593 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595 return E->isArgumentType()? 0 : 1;
596}
597
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000598unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
599 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000600 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
601 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000602 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 return 2;
604}
605
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000606unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
607 VisitExpr(E);
608 E->setNumArgs(Reader.getContext(), Record[Idx++]);
609 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000610 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000611 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000612 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000613 return E->getNumArgs() + 1;
614}
615
616unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
617 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000618 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000619 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
620 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
621 E->setArrow(Record[Idx++]);
622 return 1;
623}
624
Douglas Gregora151ba42009-04-14 23:32:43 +0000625unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
626 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000627 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000628 return 1;
629}
630
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000631unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
632 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000633 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
634 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000635 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
636 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 2;
638}
639
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000640unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
641 VisitBinaryOperator(E);
642 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
643 E->setComputationResultType(Reader.GetType(Record[Idx++]));
644 return 2;
645}
646
647unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
648 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000649 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
650 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
651 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000652 return 3;
653}
654
Douglas Gregora151ba42009-04-14 23:32:43 +0000655unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
656 VisitCastExpr(E);
657 E->setLvalueCast(Record[Idx++]);
658 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000659}
660
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000661unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
662 VisitCastExpr(E);
663 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
664 return 1;
665}
666
667unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
668 VisitExplicitCastExpr(E);
669 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
670 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
671 return 1;
672}
673
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000674unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
675 VisitExpr(E);
676 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000677 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000678 E->setFileScope(Record[Idx++]);
679 return 1;
680}
681
Douglas Gregorec0b8292009-04-15 23:02:49 +0000682unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
683 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000684 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000685 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
686 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
687 return 1;
688}
689
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000690unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
691 VisitExpr(E);
692 unsigned NumInits = Record[Idx++];
693 E->reserveInits(NumInits);
694 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000695 E->updateInit(I,
696 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
697 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000698 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
699 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
700 E->setInitializedFieldInUnion(
701 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
702 E->sawArrayRangeDesignator(Record[Idx++]);
703 return NumInits + 1;
704}
705
706unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
707 typedef DesignatedInitExpr::Designator Designator;
708
709 VisitExpr(E);
710 unsigned NumSubExprs = Record[Idx++];
711 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
712 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000713 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000714 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
715 E->setGNUSyntax(Record[Idx++]);
716
717 llvm::SmallVector<Designator, 4> Designators;
718 while (Idx < Record.size()) {
719 switch ((pch::DesignatorTypes)Record[Idx++]) {
720 case pch::DESIG_FIELD_DECL: {
721 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
722 SourceLocation DotLoc
723 = SourceLocation::getFromRawEncoding(Record[Idx++]);
724 SourceLocation FieldLoc
725 = SourceLocation::getFromRawEncoding(Record[Idx++]);
726 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
727 FieldLoc));
728 Designators.back().setField(Field);
729 break;
730 }
731
732 case pch::DESIG_FIELD_NAME: {
733 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
734 SourceLocation DotLoc
735 = SourceLocation::getFromRawEncoding(Record[Idx++]);
736 SourceLocation FieldLoc
737 = SourceLocation::getFromRawEncoding(Record[Idx++]);
738 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
739 break;
740 }
741
742 case pch::DESIG_ARRAY: {
743 unsigned Index = Record[Idx++];
744 SourceLocation LBracketLoc
745 = SourceLocation::getFromRawEncoding(Record[Idx++]);
746 SourceLocation RBracketLoc
747 = SourceLocation::getFromRawEncoding(Record[Idx++]);
748 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
749 break;
750 }
751
752 case pch::DESIG_ARRAY_RANGE: {
753 unsigned Index = Record[Idx++];
754 SourceLocation LBracketLoc
755 = SourceLocation::getFromRawEncoding(Record[Idx++]);
756 SourceLocation EllipsisLoc
757 = SourceLocation::getFromRawEncoding(Record[Idx++]);
758 SourceLocation RBracketLoc
759 = SourceLocation::getFromRawEncoding(Record[Idx++]);
760 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
761 RBracketLoc));
762 break;
763 }
764 }
765 }
766 E->setDesignators(&Designators[0], Designators.size());
767
768 return NumSubExprs;
769}
770
771unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
772 VisitExpr(E);
773 return 0;
774}
775
Douglas Gregorec0b8292009-04-15 23:02:49 +0000776unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
777 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000778 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000779 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
780 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 return 1;
782}
783
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000784unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
785 VisitExpr(E);
786 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
787 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 Reader.SetLabelOf(E, Record[Idx++]);
789 return 0;
790}
791
Douglas Gregoreca12f62009-04-17 19:05:30 +0000792unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
793 VisitExpr(E);
794 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
796 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
797 return 1;
798}
799
Douglas Gregor209d4622009-04-15 23:33:31 +0000800unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
801 VisitExpr(E);
802 E->setArgType1(Reader.GetType(Record[Idx++]));
803 E->setArgType2(Reader.GetType(Record[Idx++]));
804 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
805 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 return 0;
807}
808
809unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
810 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000811 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
812 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
813 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000814 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
815 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
816 return 3;
817}
818
819unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
820 VisitExpr(E);
821 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
822 return 0;
823}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000824
Douglas Gregor725e94b2009-04-16 00:01:45 +0000825unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
826 VisitExpr(E);
827 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000828 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000829 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
830 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
831 return NumExprs;
832}
833
Douglas Gregore246b742009-04-17 19:21:43 +0000834unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
835 VisitExpr(E);
836 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
837 E->setHasBlockDeclRefExprs(Record[Idx++]);
838 return 0;
839}
840
Douglas Gregor725e94b2009-04-16 00:01:45 +0000841unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
842 VisitExpr(E);
843 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
844 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
845 E->setByRef(Record[Idx++]);
846 return 0;
847}
848
Douglas Gregorc34897d2009-04-09 22:27:44 +0000849// FIXME: use the diagnostics machinery
850static bool Error(const char *Str) {
851 std::fprintf(stderr, "%s\n", Str);
852 return true;
853}
854
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000855/// \brief Check the contents of the predefines buffer against the
856/// contents of the predefines buffer used to build the PCH file.
857///
858/// The contents of the two predefines buffers should be the same. If
859/// not, then some command-line option changed the preprocessor state
860/// and we must reject the PCH file.
861///
862/// \param PCHPredef The start of the predefines buffer in the PCH
863/// file.
864///
865/// \param PCHPredefLen The length of the predefines buffer in the PCH
866/// file.
867///
868/// \param PCHBufferID The FileID for the PCH predefines buffer.
869///
870/// \returns true if there was a mismatch (in which case the PCH file
871/// should be ignored), or false otherwise.
872bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
873 unsigned PCHPredefLen,
874 FileID PCHBufferID) {
875 const char *Predef = PP.getPredefines().c_str();
876 unsigned PredefLen = PP.getPredefines().size();
877
878 // If the two predefines buffers compare equal, we're done!.
879 if (PredefLen == PCHPredefLen &&
880 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
881 return false;
882
883 // The predefines buffers are different. Produce a reasonable
884 // diagnostic showing where they are different.
885
886 // The source locations (potentially in the two different predefines
887 // buffers)
888 SourceLocation Loc1, Loc2;
889 SourceManager &SourceMgr = PP.getSourceManager();
890
891 // Create a source buffer for our predefines string, so
892 // that we can build a diagnostic that points into that
893 // source buffer.
894 FileID BufferID;
895 if (Predef && Predef[0]) {
896 llvm::MemoryBuffer *Buffer
897 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
898 "<built-in>");
899 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
900 }
901
902 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
903 std::pair<const char *, const char *> Locations
904 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
905
906 if (Locations.first != Predef + MinLen) {
907 // We found the location in the two buffers where there is a
908 // difference. Form source locations to point there (in both
909 // buffers).
910 unsigned Offset = Locations.first - Predef;
911 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
912 .getFileLocWithOffset(Offset);
913 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
914 .getFileLocWithOffset(Offset);
915 } else if (PredefLen > PCHPredefLen) {
916 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
917 .getFileLocWithOffset(MinLen);
918 } else {
919 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
920 .getFileLocWithOffset(MinLen);
921 }
922
923 Diag(Loc1, diag::warn_pch_preprocessor);
924 if (Loc2.isValid())
925 Diag(Loc2, diag::note_predef_in_pch);
926 Diag(diag::note_ignoring_pch) << FileName;
927 return true;
928}
929
Douglas Gregor635f97f2009-04-13 16:31:14 +0000930/// \brief Read the line table in the source manager block.
931/// \returns true if ther was an error.
932static bool ParseLineTable(SourceManager &SourceMgr,
933 llvm::SmallVectorImpl<uint64_t> &Record) {
934 unsigned Idx = 0;
935 LineTableInfo &LineTable = SourceMgr.getLineTable();
936
937 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000938 std::map<int, int> FileIDs;
939 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000940 // Extract the file name
941 unsigned FilenameLen = Record[Idx++];
942 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
943 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000944 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
945 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000946 }
947
948 // Parse the line entries
949 std::vector<LineEntry> Entries;
950 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000951 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000952
953 // Extract the line entries
954 unsigned NumEntries = Record[Idx++];
955 Entries.clear();
956 Entries.reserve(NumEntries);
957 for (unsigned I = 0; I != NumEntries; ++I) {
958 unsigned FileOffset = Record[Idx++];
959 unsigned LineNo = Record[Idx++];
960 int FilenameID = Record[Idx++];
961 SrcMgr::CharacteristicKind FileKind
962 = (SrcMgr::CharacteristicKind)Record[Idx++];
963 unsigned IncludeOffset = Record[Idx++];
964 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
965 FileKind, IncludeOffset));
966 }
967 LineTable.AddEntry(FID, Entries);
968 }
969
970 return false;
971}
972
Douglas Gregorab1cef72009-04-10 03:52:48 +0000973/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000974PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000975 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000976 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
977 Error("Malformed source manager block record");
978 return Failure;
979 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000980
981 SourceManager &SourceMgr = Context.getSourceManager();
982 RecordData Record;
983 while (true) {
984 unsigned Code = Stream.ReadCode();
985 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000986 if (Stream.ReadBlockEnd()) {
987 Error("Error at end of Source Manager block");
988 return Failure;
989 }
990
991 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000992 }
993
994 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
995 // No known subblocks, always skip them.
996 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000997 if (Stream.SkipBlock()) {
998 Error("Malformed block record");
999 return Failure;
1000 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001001 continue;
1002 }
1003
1004 if (Code == llvm::bitc::DEFINE_ABBREV) {
1005 Stream.ReadAbbrevRecord();
1006 continue;
1007 }
1008
1009 // Read a record.
1010 const char *BlobStart;
1011 unsigned BlobLen;
1012 Record.clear();
1013 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1014 default: // Default behavior: ignore.
1015 break;
1016
1017 case pch::SM_SLOC_FILE_ENTRY: {
1018 // FIXME: We would really like to delay the creation of this
1019 // FileEntry until it is actually required, e.g., when producing
1020 // a diagnostic with a source location in this file.
1021 const FileEntry *File
1022 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1023 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001024 FileID ID = SourceMgr.createFileID(File,
1025 SourceLocation::getFromRawEncoding(Record[1]),
1026 (CharacteristicKind)Record[2]);
1027 if (Record[3])
1028 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1029 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001030 break;
1031 }
1032
1033 case pch::SM_SLOC_BUFFER_ENTRY: {
1034 const char *Name = BlobStart;
1035 unsigned Code = Stream.ReadCode();
1036 Record.clear();
1037 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1038 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001039 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001040 llvm::MemoryBuffer *Buffer
1041 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1042 BlobStart + BlobLen - 1,
1043 Name);
1044 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1045
1046 if (strcmp(Name, "<built-in>") == 0
1047 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1048 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001049 break;
1050 }
1051
1052 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1053 SourceLocation SpellingLoc
1054 = SourceLocation::getFromRawEncoding(Record[1]);
1055 SourceMgr.createInstantiationLoc(
1056 SpellingLoc,
1057 SourceLocation::getFromRawEncoding(Record[2]),
1058 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001059 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001060 break;
1061 }
1062
Chris Lattnere1be6022009-04-14 23:22:57 +00001063 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001064 if (ParseLineTable(SourceMgr, Record))
1065 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001066 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001067 }
1068 }
1069}
1070
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001071bool PCHReader::ReadPreprocessorBlock() {
1072 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1073 return Error("Malformed preprocessor block record");
1074
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001075 RecordData Record;
1076 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1077 MacroInfo *LastMacro = 0;
1078
1079 while (true) {
1080 unsigned Code = Stream.ReadCode();
1081 switch (Code) {
1082 case llvm::bitc::END_BLOCK:
1083 if (Stream.ReadBlockEnd())
1084 return Error("Error at end of preprocessor block");
1085 return false;
1086
1087 case llvm::bitc::ENTER_SUBBLOCK:
1088 // No known subblocks, always skip them.
1089 Stream.ReadSubBlockID();
1090 if (Stream.SkipBlock())
1091 return Error("Malformed block record");
1092 continue;
1093
1094 case llvm::bitc::DEFINE_ABBREV:
1095 Stream.ReadAbbrevRecord();
1096 continue;
1097 default: break;
1098 }
1099
1100 // Read a record.
1101 Record.clear();
1102 pch::PreprocessorRecordTypes RecType =
1103 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1104 switch (RecType) {
1105 default: // Default behavior: ignore unknown records.
1106 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001107 case pch::PP_COUNTER_VALUE:
1108 if (!Record.empty())
1109 PP.setCounterValue(Record[0]);
1110 break;
1111
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001112 case pch::PP_MACRO_OBJECT_LIKE:
1113 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001114 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1115 if (II == 0)
1116 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001117 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1118 bool isUsed = Record[2];
1119
1120 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1121 MI->setIsUsed(isUsed);
1122
1123 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1124 // Decode function-like macro info.
1125 bool isC99VarArgs = Record[3];
1126 bool isGNUVarArgs = Record[4];
1127 MacroArgs.clear();
1128 unsigned NumArgs = Record[5];
1129 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001130 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001131
1132 // Install function-like macro info.
1133 MI->setIsFunctionLike();
1134 if (isC99VarArgs) MI->setIsC99Varargs();
1135 if (isGNUVarArgs) MI->setIsGNUVarargs();
1136 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1137 PP.getPreprocessorAllocator());
1138 }
1139
1140 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001141 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001142
1143 // Remember that we saw this macro last so that we add the tokens that
1144 // form its body to it.
1145 LastMacro = MI;
1146 break;
1147 }
1148
1149 case pch::PP_TOKEN: {
1150 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1151 // pretend we didn't see this.
1152 if (LastMacro == 0) break;
1153
1154 Token Tok;
1155 Tok.startToken();
1156 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1157 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001158 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1159 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001160 Tok.setKind((tok::TokenKind)Record[3]);
1161 Tok.setFlag((Token::TokenFlags)Record[4]);
1162 LastMacro->AddTokenToBody(Tok);
1163 break;
1164 }
1165 }
1166 }
1167}
1168
Douglas Gregor179cfb12009-04-10 20:39:37 +00001169PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1170 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1171 Error("Malformed block record");
1172 return Failure;
1173 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001174
Chris Lattner29241862009-04-11 21:15:38 +00001175 uint64_t PreprocessorBlockBit = 0;
Douglas Gregor456e0952009-04-17 22:13:46 +00001176
Douglas Gregorc34897d2009-04-09 22:27:44 +00001177 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001178 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001179 while (!Stream.AtEndOfStream()) {
1180 unsigned Code = Stream.ReadCode();
1181 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001182 // If we saw the preprocessor block, read it now.
1183 if (PreprocessorBlockBit) {
1184 uint64_t SavedPos = Stream.GetCurrentBitNo();
1185 Stream.JumpToBit(PreprocessorBlockBit);
1186 if (ReadPreprocessorBlock()) {
1187 Error("Malformed preprocessor block");
1188 return Failure;
1189 }
1190 Stream.JumpToBit(SavedPos);
1191 }
1192
Douglas Gregor179cfb12009-04-10 20:39:37 +00001193 if (Stream.ReadBlockEnd()) {
1194 Error("Error at end of module block");
1195 return Failure;
1196 }
Chris Lattner29241862009-04-11 21:15:38 +00001197
Douglas Gregor179cfb12009-04-10 20:39:37 +00001198 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001199 }
1200
1201 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1202 switch (Stream.ReadSubBlockID()) {
1203 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1204 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1205 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001206 if (Stream.SkipBlock()) {
1207 Error("Malformed block record");
1208 return Failure;
1209 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001210 break;
1211
Chris Lattner29241862009-04-11 21:15:38 +00001212 case pch::PREPROCESSOR_BLOCK_ID:
1213 // Skip the preprocessor block for now, but remember where it is. We
1214 // want to read it in after the identifier table.
1215 if (PreprocessorBlockBit) {
1216 Error("Multiple preprocessor blocks found.");
1217 return Failure;
1218 }
1219 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1220 if (Stream.SkipBlock()) {
1221 Error("Malformed block record");
1222 return Failure;
1223 }
1224 break;
1225
Douglas Gregorab1cef72009-04-10 03:52:48 +00001226 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001227 switch (ReadSourceManagerBlock()) {
1228 case Success:
1229 break;
1230
1231 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001232 Error("Malformed source manager block");
1233 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001234
1235 case IgnorePCH:
1236 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001237 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001238 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001239 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001240 continue;
1241 }
1242
1243 if (Code == llvm::bitc::DEFINE_ABBREV) {
1244 Stream.ReadAbbrevRecord();
1245 continue;
1246 }
1247
1248 // Read and process a record.
1249 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001250 const char *BlobStart = 0;
1251 unsigned BlobLen = 0;
1252 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1253 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001254 default: // Default behavior: ignore.
1255 break;
1256
1257 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001258 if (!TypeOffsets.empty()) {
1259 Error("Duplicate TYPE_OFFSET record in PCH file");
1260 return Failure;
1261 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001262 TypeOffsets.swap(Record);
1263 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1264 break;
1265
1266 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001267 if (!DeclOffsets.empty()) {
1268 Error("Duplicate DECL_OFFSET record in PCH file");
1269 return Failure;
1270 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001271 DeclOffsets.swap(Record);
1272 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1273 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001274
1275 case pch::LANGUAGE_OPTIONS:
1276 if (ParseLanguageOptions(Record))
1277 return IgnorePCH;
1278 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001279
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001280 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001281 std::string TargetTriple(BlobStart, BlobLen);
1282 if (TargetTriple != Context.Target.getTargetTriple()) {
1283 Diag(diag::warn_pch_target_triple)
1284 << TargetTriple << Context.Target.getTargetTriple();
1285 Diag(diag::note_ignoring_pch) << FileName;
1286 return IgnorePCH;
1287 }
1288 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001289 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001290
1291 case pch::IDENTIFIER_TABLE:
1292 IdentifierTable = BlobStart;
1293 break;
1294
1295 case pch::IDENTIFIER_OFFSET:
1296 if (!IdentifierData.empty()) {
1297 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1298 return Failure;
1299 }
1300 IdentifierData.swap(Record);
1301#ifndef NDEBUG
1302 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1303 if ((IdentifierData[I] & 0x01) == 0) {
1304 Error("Malformed identifier table in the precompiled header");
1305 return Failure;
1306 }
1307 }
1308#endif
1309 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001310
1311 case pch::EXTERNAL_DEFINITIONS:
1312 if (!ExternalDefinitions.empty()) {
1313 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1314 return Failure;
1315 }
1316 ExternalDefinitions.swap(Record);
1317 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001318
Douglas Gregore01ad442009-04-18 05:55:16 +00001319 case pch::SPECIAL_TYPES:
1320 SpecialTypes.swap(Record);
1321 break;
1322
Douglas Gregor456e0952009-04-17 22:13:46 +00001323 case pch::STATISTICS:
1324 TotalNumStatements = Record[0];
1325 break;
1326
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001327 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001328 }
1329
Douglas Gregor179cfb12009-04-10 20:39:37 +00001330 Error("Premature end of bitstream");
1331 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001332}
1333
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001334PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001335 // Set the PCH file name.
1336 this->FileName = FileName;
1337
Douglas Gregorc34897d2009-04-09 22:27:44 +00001338 // Open the PCH file.
1339 std::string ErrStr;
1340 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001341 if (!Buffer) {
1342 Error(ErrStr.c_str());
1343 return IgnorePCH;
1344 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001345
1346 // Initialize the stream
1347 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1348 (const unsigned char *)Buffer->getBufferEnd());
1349
1350 // Sniff for the signature.
1351 if (Stream.Read(8) != 'C' ||
1352 Stream.Read(8) != 'P' ||
1353 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001354 Stream.Read(8) != 'H') {
1355 Error("Not a PCH file");
1356 return IgnorePCH;
1357 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001358
1359 // We expect a number of well-defined blocks, though we don't necessarily
1360 // need to understand them all.
1361 while (!Stream.AtEndOfStream()) {
1362 unsigned Code = Stream.ReadCode();
1363
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001364 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1365 Error("Invalid record at top-level");
1366 return Failure;
1367 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001368
1369 unsigned BlockID = Stream.ReadSubBlockID();
1370
1371 // We only know the PCH subblock ID.
1372 switch (BlockID) {
1373 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001374 if (Stream.ReadBlockInfoBlock()) {
1375 Error("Malformed BlockInfoBlock");
1376 return Failure;
1377 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001378 break;
1379 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001380 switch (ReadPCHBlock()) {
1381 case Success:
1382 break;
1383
1384 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001385 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001386
1387 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001388 // FIXME: We could consider reading through to the end of this
1389 // PCH block, skipping subblocks, to see if there are other
1390 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001391 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001392 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001393 break;
1394 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001395 if (Stream.SkipBlock()) {
1396 Error("Malformed block record");
1397 return Failure;
1398 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001399 break;
1400 }
1401 }
1402
1403 // Load the translation unit declaration
1404 ReadDeclRecord(DeclOffsets[0], 0);
1405
Douglas Gregore01ad442009-04-18 05:55:16 +00001406 // Load the special types.
1407 Context.setBuiltinVaListType(
1408 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1409
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001410 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001411}
1412
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001413namespace {
1414 /// \brief Helper class that saves the current stream position and
1415 /// then restores it when destroyed.
1416 struct VISIBILITY_HIDDEN SavedStreamPosition {
1417 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001418 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001419
1420 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001421 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001422 }
1423
1424 private:
1425 llvm::BitstreamReader &Stream;
1426 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001427 };
1428}
1429
Douglas Gregor179cfb12009-04-10 20:39:37 +00001430/// \brief Parse the record that corresponds to a LangOptions data
1431/// structure.
1432///
1433/// This routine compares the language options used to generate the
1434/// PCH file against the language options set for the current
1435/// compilation. For each option, we classify differences between the
1436/// two compiler states as either "benign" or "important". Benign
1437/// differences don't matter, and we accept them without complaint
1438/// (and without modifying the language options). Differences between
1439/// the states for important options cause the PCH file to be
1440/// unusable, so we emit a warning and return true to indicate that
1441/// there was an error.
1442///
1443/// \returns true if the PCH file is unacceptable, false otherwise.
1444bool PCHReader::ParseLanguageOptions(
1445 const llvm::SmallVectorImpl<uint64_t> &Record) {
1446 const LangOptions &LangOpts = Context.getLangOptions();
1447#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1448#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1449 if (Record[Idx] != LangOpts.Option) { \
1450 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1451 Diag(diag::note_ignoring_pch) << FileName; \
1452 return true; \
1453 } \
1454 ++Idx
1455
1456 unsigned Idx = 0;
1457 PARSE_LANGOPT_BENIGN(Trigraphs);
1458 PARSE_LANGOPT_BENIGN(BCPLComment);
1459 PARSE_LANGOPT_BENIGN(DollarIdents);
1460 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1461 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1462 PARSE_LANGOPT_BENIGN(ImplicitInt);
1463 PARSE_LANGOPT_BENIGN(Digraphs);
1464 PARSE_LANGOPT_BENIGN(HexFloats);
1465 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1466 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1467 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1468 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1469 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1470 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1471 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1472 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1473 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1474 PARSE_LANGOPT_BENIGN(PascalStrings);
1475 PARSE_LANGOPT_BENIGN(Boolean);
1476 PARSE_LANGOPT_BENIGN(WritableStrings);
1477 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1478 diag::warn_pch_lax_vector_conversions);
1479 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1480 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1481 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1482 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1483 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1484 diag::warn_pch_thread_safe_statics);
1485 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1486 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1487 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1488 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1489 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1490 diag::warn_pch_heinous_extensions);
1491 // FIXME: Most of the options below are benign if the macro wasn't
1492 // used. Unfortunately, this means that a PCH compiled without
1493 // optimization can't be used with optimization turned on, even
1494 // though the only thing that changes is whether __OPTIMIZE__ was
1495 // defined... but if __OPTIMIZE__ never showed up in the header, it
1496 // doesn't matter. We could consider making this some special kind
1497 // of check.
1498 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1499 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1500 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1501 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1502 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1503 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1504 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1505 Diag(diag::warn_pch_gc_mode)
1506 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1507 Diag(diag::note_ignoring_pch) << FileName;
1508 return true;
1509 }
1510 ++Idx;
1511 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1512 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1513#undef PARSE_LANGOPT_IRRELEVANT
1514#undef PARSE_LANGOPT_BENIGN
1515
1516 return false;
1517}
1518
Douglas Gregorc34897d2009-04-09 22:27:44 +00001519/// \brief Read and return the type at the given offset.
1520///
1521/// This routine actually reads the record corresponding to the type
1522/// at the given offset in the bitstream. It is a helper routine for
1523/// GetType, which deals with reading type IDs.
1524QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001525 // Keep track of where we are in the stream, then jump back there
1526 // after reading this type.
1527 SavedStreamPosition SavedPosition(Stream);
1528
Douglas Gregorc34897d2009-04-09 22:27:44 +00001529 Stream.JumpToBit(Offset);
1530 RecordData Record;
1531 unsigned Code = Stream.ReadCode();
1532 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001533 case pch::TYPE_EXT_QUAL: {
1534 assert(Record.size() == 3 &&
1535 "Incorrect encoding of extended qualifier type");
1536 QualType Base = GetType(Record[0]);
1537 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1538 unsigned AddressSpace = Record[2];
1539
1540 QualType T = Base;
1541 if (GCAttr != QualType::GCNone)
1542 T = Context.getObjCGCQualType(T, GCAttr);
1543 if (AddressSpace)
1544 T = Context.getAddrSpaceQualType(T, AddressSpace);
1545 return T;
1546 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001547
Douglas Gregorc34897d2009-04-09 22:27:44 +00001548 case pch::TYPE_FIXED_WIDTH_INT: {
1549 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1550 return Context.getFixedWidthIntType(Record[0], Record[1]);
1551 }
1552
1553 case pch::TYPE_COMPLEX: {
1554 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1555 QualType ElemType = GetType(Record[0]);
1556 return Context.getComplexType(ElemType);
1557 }
1558
1559 case pch::TYPE_POINTER: {
1560 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1561 QualType PointeeType = GetType(Record[0]);
1562 return Context.getPointerType(PointeeType);
1563 }
1564
1565 case pch::TYPE_BLOCK_POINTER: {
1566 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1567 QualType PointeeType = GetType(Record[0]);
1568 return Context.getBlockPointerType(PointeeType);
1569 }
1570
1571 case pch::TYPE_LVALUE_REFERENCE: {
1572 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1573 QualType PointeeType = GetType(Record[0]);
1574 return Context.getLValueReferenceType(PointeeType);
1575 }
1576
1577 case pch::TYPE_RVALUE_REFERENCE: {
1578 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1579 QualType PointeeType = GetType(Record[0]);
1580 return Context.getRValueReferenceType(PointeeType);
1581 }
1582
1583 case pch::TYPE_MEMBER_POINTER: {
1584 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1585 QualType PointeeType = GetType(Record[0]);
1586 QualType ClassType = GetType(Record[1]);
1587 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1588 }
1589
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001590 case pch::TYPE_CONSTANT_ARRAY: {
1591 QualType ElementType = GetType(Record[0]);
1592 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1593 unsigned IndexTypeQuals = Record[2];
1594 unsigned Idx = 3;
1595 llvm::APInt Size = ReadAPInt(Record, Idx);
1596 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1597 }
1598
1599 case pch::TYPE_INCOMPLETE_ARRAY: {
1600 QualType ElementType = GetType(Record[0]);
1601 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1602 unsigned IndexTypeQuals = Record[2];
1603 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1604 }
1605
1606 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001607 QualType ElementType = GetType(Record[0]);
1608 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1609 unsigned IndexTypeQuals = Record[2];
1610 return Context.getVariableArrayType(ElementType, ReadExpr(),
1611 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001612 }
1613
1614 case pch::TYPE_VECTOR: {
1615 if (Record.size() != 2) {
1616 Error("Incorrect encoding of vector type in PCH file");
1617 return QualType();
1618 }
1619
1620 QualType ElementType = GetType(Record[0]);
1621 unsigned NumElements = Record[1];
1622 return Context.getVectorType(ElementType, NumElements);
1623 }
1624
1625 case pch::TYPE_EXT_VECTOR: {
1626 if (Record.size() != 2) {
1627 Error("Incorrect encoding of extended vector type in PCH file");
1628 return QualType();
1629 }
1630
1631 QualType ElementType = GetType(Record[0]);
1632 unsigned NumElements = Record[1];
1633 return Context.getExtVectorType(ElementType, NumElements);
1634 }
1635
1636 case pch::TYPE_FUNCTION_NO_PROTO: {
1637 if (Record.size() != 1) {
1638 Error("Incorrect encoding of no-proto function type");
1639 return QualType();
1640 }
1641 QualType ResultType = GetType(Record[0]);
1642 return Context.getFunctionNoProtoType(ResultType);
1643 }
1644
1645 case pch::TYPE_FUNCTION_PROTO: {
1646 QualType ResultType = GetType(Record[0]);
1647 unsigned Idx = 1;
1648 unsigned NumParams = Record[Idx++];
1649 llvm::SmallVector<QualType, 16> ParamTypes;
1650 for (unsigned I = 0; I != NumParams; ++I)
1651 ParamTypes.push_back(GetType(Record[Idx++]));
1652 bool isVariadic = Record[Idx++];
1653 unsigned Quals = Record[Idx++];
1654 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1655 isVariadic, Quals);
1656 }
1657
1658 case pch::TYPE_TYPEDEF:
1659 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1660 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1661
1662 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001663 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001664
1665 case pch::TYPE_TYPEOF: {
1666 if (Record.size() != 1) {
1667 Error("Incorrect encoding of typeof(type) in PCH file");
1668 return QualType();
1669 }
1670 QualType UnderlyingType = GetType(Record[0]);
1671 return Context.getTypeOfType(UnderlyingType);
1672 }
1673
1674 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001675 assert(Record.size() == 1 && "Incorrect encoding of record type");
1676 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001677
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001678 case pch::TYPE_ENUM:
1679 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1680 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1681
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001682 case pch::TYPE_OBJC_INTERFACE:
1683 // FIXME: Deserialize ObjCInterfaceType
1684 assert(false && "Cannot de-serialize ObjC interface types yet");
1685 return QualType();
1686
1687 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1688 // FIXME: Deserialize ObjCQualifiedInterfaceType
1689 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1690 return QualType();
1691
1692 case pch::TYPE_OBJC_QUALIFIED_ID:
1693 // FIXME: Deserialize ObjCQualifiedIdType
1694 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1695 return QualType();
1696
1697 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1698 // FIXME: Deserialize ObjCQualifiedClassType
1699 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1700 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001701 }
1702
1703 // Suppress a GCC warning
1704 return QualType();
1705}
1706
1707/// \brief Note that we have loaded the declaration with the given
1708/// Index.
1709///
1710/// This routine notes that this declaration has already been loaded,
1711/// so that future GetDecl calls will return this declaration rather
1712/// than trying to load a new declaration.
1713inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1714 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1715 DeclAlreadyLoaded[Index] = true;
1716 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1717}
1718
1719/// \brief Read the declaration at the given offset from the PCH file.
1720Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001721 // Keep track of where we are in the stream, then jump back there
1722 // after reading this declaration.
1723 SavedStreamPosition SavedPosition(Stream);
1724
Douglas Gregorc34897d2009-04-09 22:27:44 +00001725 Decl *D = 0;
1726 Stream.JumpToBit(Offset);
1727 RecordData Record;
1728 unsigned Code = Stream.ReadCode();
1729 unsigned Idx = 0;
1730 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001731
Douglas Gregorc34897d2009-04-09 22:27:44 +00001732 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001733 case pch::DECL_ATTR:
1734 case pch::DECL_CONTEXT_LEXICAL:
1735 case pch::DECL_CONTEXT_VISIBLE:
1736 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1737 break;
1738
Douglas Gregorc34897d2009-04-09 22:27:44 +00001739 case pch::DECL_TRANSLATION_UNIT:
1740 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001741 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001742 break;
1743
1744 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001745 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001746 break;
1747 }
1748
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001749 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001750 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001751 break;
1752 }
1753
Douglas Gregor982365e2009-04-13 21:20:57 +00001754 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001755 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1756 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001757 break;
1758 }
1759
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001760 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001761 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1762 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001763 break;
1764 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001765
1766 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001767 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1768 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001769 break;
1770 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001771
Douglas Gregor982365e2009-04-13 21:20:57 +00001772 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001773 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1774 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001775 break;
1776 }
1777
Douglas Gregorc34897d2009-04-09 22:27:44 +00001778 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001779 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1780 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001781 break;
1782 }
1783
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001784 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001785 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1786 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001787 break;
1788 }
1789
1790 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001791 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001792 QualType(), QualType(), VarDecl::None,
1793 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001794 break;
1795 }
1796
Douglas Gregor2a491792009-04-13 22:49:25 +00001797 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001798 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001799 break;
1800 }
1801
1802 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001803 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001804 break;
1805 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001806 }
1807
Douglas Gregorddf4d092009-04-16 22:29:51 +00001808 assert(D && "Unknown declaration creating PCH file");
1809 if (D) {
1810 LoadedDecl(Index, D);
1811 Reader.Visit(D);
1812 }
1813
Douglas Gregorc34897d2009-04-09 22:27:44 +00001814 // If this declaration is also a declaration context, get the
1815 // offsets for its tables of lexical and visible declarations.
1816 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1817 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1818 if (Offsets.first || Offsets.second) {
1819 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1820 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1821 DeclContextOffsets[DC] = Offsets;
1822 }
1823 }
1824 assert(Idx == Record.size());
1825
1826 return D;
1827}
1828
Douglas Gregorac8f2802009-04-10 17:25:41 +00001829QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001830 unsigned Quals = ID & 0x07;
1831 unsigned Index = ID >> 3;
1832
1833 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1834 QualType T;
1835 switch ((pch::PredefinedTypeIDs)Index) {
1836 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1837 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1838 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1839
1840 case pch::PREDEF_TYPE_CHAR_U_ID:
1841 case pch::PREDEF_TYPE_CHAR_S_ID:
1842 // FIXME: Check that the signedness of CharTy is correct!
1843 T = Context.CharTy;
1844 break;
1845
1846 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1847 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1848 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1849 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1850 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1851 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1852 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1853 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1854 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1855 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1856 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1857 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1858 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1859 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1860 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1861 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1862 }
1863
1864 assert(!T.isNull() && "Unknown predefined type");
1865 return T.getQualifiedType(Quals);
1866 }
1867
1868 Index -= pch::NUM_PREDEF_TYPE_IDS;
1869 if (!TypeAlreadyLoaded[Index]) {
1870 // Load the type from the PCH file.
1871 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1872 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1873 TypeAlreadyLoaded[Index] = true;
1874 }
1875
1876 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1877}
1878
Douglas Gregorac8f2802009-04-10 17:25:41 +00001879Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001880 if (ID == 0)
1881 return 0;
1882
1883 unsigned Index = ID - 1;
1884 if (DeclAlreadyLoaded[Index])
1885 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1886
1887 // Load the declaration from the PCH file.
1888 return ReadDeclRecord(DeclOffsets[Index], Index);
1889}
1890
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00001891Stmt *PCHReader::GetStmt(uint64_t Offset) {
1892 // Keep track of where we are in the stream, then jump back there
1893 // after reading this declaration.
1894 SavedStreamPosition SavedPosition(Stream);
1895
1896 Stream.JumpToBit(Offset);
1897 return ReadStmt();
1898}
1899
Douglas Gregorc34897d2009-04-09 22:27:44 +00001900bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001901 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001902 assert(DC->hasExternalLexicalStorage() &&
1903 "DeclContext has no lexical decls in storage");
1904 uint64_t Offset = DeclContextOffsets[DC].first;
1905 assert(Offset && "DeclContext has no lexical decls in storage");
1906
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001907 // Keep track of where we are in the stream, then jump back there
1908 // after reading this context.
1909 SavedStreamPosition SavedPosition(Stream);
1910
Douglas Gregorc34897d2009-04-09 22:27:44 +00001911 // Load the record containing all of the declarations lexically in
1912 // this context.
1913 Stream.JumpToBit(Offset);
1914 RecordData Record;
1915 unsigned Code = Stream.ReadCode();
1916 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001917 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001918 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1919
1920 // Load all of the declaration IDs
1921 Decls.clear();
1922 Decls.insert(Decls.end(), Record.begin(), Record.end());
1923 return false;
1924}
1925
1926bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1927 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1928 assert(DC->hasExternalVisibleStorage() &&
1929 "DeclContext has no visible decls in storage");
1930 uint64_t Offset = DeclContextOffsets[DC].second;
1931 assert(Offset && "DeclContext has no visible decls in storage");
1932
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001933 // Keep track of where we are in the stream, then jump back there
1934 // after reading this context.
1935 SavedStreamPosition SavedPosition(Stream);
1936
Douglas Gregorc34897d2009-04-09 22:27:44 +00001937 // Load the record containing all of the declarations visible in
1938 // this context.
1939 Stream.JumpToBit(Offset);
1940 RecordData Record;
1941 unsigned Code = Stream.ReadCode();
1942 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001943 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001944 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1945 if (Record.size() == 0)
1946 return false;
1947
1948 Decls.clear();
1949
1950 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001951 while (Idx < Record.size()) {
1952 Decls.push_back(VisibleDeclaration());
1953 Decls.back().Name = ReadDeclarationName(Record, Idx);
1954
Douglas Gregorc34897d2009-04-09 22:27:44 +00001955 unsigned Size = Record[Idx++];
1956 llvm::SmallVector<unsigned, 4> & LoadedDecls
1957 = Decls.back().Declarations;
1958 LoadedDecls.reserve(Size);
1959 for (unsigned I = 0; I < Size; ++I)
1960 LoadedDecls.push_back(Record[Idx++]);
1961 }
1962
1963 return false;
1964}
1965
Douglas Gregor631f6c62009-04-14 00:24:19 +00001966void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1967 if (!Consumer)
1968 return;
1969
1970 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1971 Decl *D = GetDecl(ExternalDefinitions[I]);
1972 DeclGroupRef DG(D);
1973 Consumer->HandleTopLevelDecl(DG);
1974 }
1975}
1976
Douglas Gregorc34897d2009-04-09 22:27:44 +00001977void PCHReader::PrintStats() {
1978 std::fprintf(stderr, "*** PCH Statistics:\n");
1979
1980 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1981 TypeAlreadyLoaded.end(),
1982 true);
1983 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1984 DeclAlreadyLoaded.end(),
1985 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001986 unsigned NumIdentifiersLoaded = 0;
1987 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1988 if ((IdentifierData[I] & 0x01) == 0)
1989 ++NumIdentifiersLoaded;
1990 }
1991
Douglas Gregorc34897d2009-04-09 22:27:44 +00001992 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1993 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001994 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001995 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1996 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001997 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1998 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1999 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2000 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002001 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2002 NumStatementsRead, TotalNumStatements,
2003 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002004 std::fprintf(stderr, "\n");
2005}
2006
Chris Lattner29241862009-04-11 21:15:38 +00002007IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002008 if (ID == 0)
2009 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002010
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002011 if (!IdentifierTable || IdentifierData.empty()) {
2012 Error("No identifier table in PCH file");
2013 return 0;
2014 }
Chris Lattner29241862009-04-11 21:15:38 +00002015
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002016 if (IdentifierData[ID - 1] & 0x01) {
2017 uint64_t Offset = IdentifierData[ID - 1];
2018 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00002019 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002020 }
Chris Lattner29241862009-04-11 21:15:38 +00002021
2022 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002023}
2024
2025DeclarationName
2026PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2027 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2028 switch (Kind) {
2029 case DeclarationName::Identifier:
2030 return DeclarationName(GetIdentifierInfo(Record, Idx));
2031
2032 case DeclarationName::ObjCZeroArgSelector:
2033 case DeclarationName::ObjCOneArgSelector:
2034 case DeclarationName::ObjCMultiArgSelector:
2035 assert(false && "Unable to de-serialize Objective-C selectors");
2036 break;
2037
2038 case DeclarationName::CXXConstructorName:
2039 return Context.DeclarationNames.getCXXConstructorName(
2040 GetType(Record[Idx++]));
2041
2042 case DeclarationName::CXXDestructorName:
2043 return Context.DeclarationNames.getCXXDestructorName(
2044 GetType(Record[Idx++]));
2045
2046 case DeclarationName::CXXConversionFunctionName:
2047 return Context.DeclarationNames.getCXXConversionFunctionName(
2048 GetType(Record[Idx++]));
2049
2050 case DeclarationName::CXXOperatorName:
2051 return Context.DeclarationNames.getCXXOperatorName(
2052 (OverloadedOperatorKind)Record[Idx++]);
2053
2054 case DeclarationName::CXXUsingDirective:
2055 return DeclarationName::getUsingDirectiveName();
2056 }
2057
2058 // Required to silence GCC warning
2059 return DeclarationName();
2060}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002061
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002062/// \brief Read an integral value
2063llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2064 unsigned BitWidth = Record[Idx++];
2065 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2066 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2067 Idx += NumWords;
2068 return Result;
2069}
2070
2071/// \brief Read a signed integral value
2072llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2073 bool isUnsigned = Record[Idx++];
2074 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2075}
2076
Douglas Gregore2f37202009-04-14 21:55:33 +00002077/// \brief Read a floating-point value
2078llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002079 return llvm::APFloat(ReadAPInt(Record, Idx));
2080}
2081
Douglas Gregor1c507882009-04-15 21:30:51 +00002082// \brief Read a string
2083std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2084 unsigned Len = Record[Idx++];
2085 std::string Result(&Record[Idx], &Record[Idx] + Len);
2086 Idx += Len;
2087 return Result;
2088}
2089
2090/// \brief Reads attributes from the current stream position.
2091Attr *PCHReader::ReadAttributes() {
2092 unsigned Code = Stream.ReadCode();
2093 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2094 "Expected unabbreviated record"); (void)Code;
2095
2096 RecordData Record;
2097 unsigned Idx = 0;
2098 unsigned RecCode = Stream.ReadRecord(Code, Record);
2099 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2100 (void)RecCode;
2101
2102#define SIMPLE_ATTR(Name) \
2103 case Attr::Name: \
2104 New = ::new (Context) Name##Attr(); \
2105 break
2106
2107#define STRING_ATTR(Name) \
2108 case Attr::Name: \
2109 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2110 break
2111
2112#define UNSIGNED_ATTR(Name) \
2113 case Attr::Name: \
2114 New = ::new (Context) Name##Attr(Record[Idx++]); \
2115 break
2116
2117 Attr *Attrs = 0;
2118 while (Idx < Record.size()) {
2119 Attr *New = 0;
2120 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2121 bool IsInherited = Record[Idx++];
2122
2123 switch (Kind) {
2124 STRING_ATTR(Alias);
2125 UNSIGNED_ATTR(Aligned);
2126 SIMPLE_ATTR(AlwaysInline);
2127 SIMPLE_ATTR(AnalyzerNoReturn);
2128 STRING_ATTR(Annotate);
2129 STRING_ATTR(AsmLabel);
2130
2131 case Attr::Blocks:
2132 New = ::new (Context) BlocksAttr(
2133 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2134 break;
2135
2136 case Attr::Cleanup:
2137 New = ::new (Context) CleanupAttr(
2138 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2139 break;
2140
2141 SIMPLE_ATTR(Const);
2142 UNSIGNED_ATTR(Constructor);
2143 SIMPLE_ATTR(DLLExport);
2144 SIMPLE_ATTR(DLLImport);
2145 SIMPLE_ATTR(Deprecated);
2146 UNSIGNED_ATTR(Destructor);
2147 SIMPLE_ATTR(FastCall);
2148
2149 case Attr::Format: {
2150 std::string Type = ReadString(Record, Idx);
2151 unsigned FormatIdx = Record[Idx++];
2152 unsigned FirstArg = Record[Idx++];
2153 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2154 break;
2155 }
2156
2157 SIMPLE_ATTR(GNUCInline);
2158
2159 case Attr::IBOutletKind:
2160 New = ::new (Context) IBOutletAttr();
2161 break;
2162
2163 SIMPLE_ATTR(NoReturn);
2164 SIMPLE_ATTR(NoThrow);
2165 SIMPLE_ATTR(Nodebug);
2166 SIMPLE_ATTR(Noinline);
2167
2168 case Attr::NonNull: {
2169 unsigned Size = Record[Idx++];
2170 llvm::SmallVector<unsigned, 16> ArgNums;
2171 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2172 Idx += Size;
2173 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2174 break;
2175 }
2176
2177 SIMPLE_ATTR(ObjCException);
2178 SIMPLE_ATTR(ObjCNSObject);
2179 SIMPLE_ATTR(Overloadable);
2180 UNSIGNED_ATTR(Packed);
2181 SIMPLE_ATTR(Pure);
2182 UNSIGNED_ATTR(Regparm);
2183 STRING_ATTR(Section);
2184 SIMPLE_ATTR(StdCall);
2185 SIMPLE_ATTR(TransparentUnion);
2186 SIMPLE_ATTR(Unavailable);
2187 SIMPLE_ATTR(Unused);
2188 SIMPLE_ATTR(Used);
2189
2190 case Attr::Visibility:
2191 New = ::new (Context) VisibilityAttr(
2192 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2193 break;
2194
2195 SIMPLE_ATTR(WarnUnusedResult);
2196 SIMPLE_ATTR(Weak);
2197 SIMPLE_ATTR(WeakImport);
2198 }
2199
2200 assert(New && "Unable to decode attribute?");
2201 New->setInherited(IsInherited);
2202 New->setNext(Attrs);
2203 Attrs = New;
2204 }
2205#undef UNSIGNED_ATTR
2206#undef STRING_ATTR
2207#undef SIMPLE_ATTR
2208
2209 // The list of attributes was built backwards. Reverse the list
2210 // before returning it.
2211 Attr *PrevAttr = 0, *NextAttr = 0;
2212 while (Attrs) {
2213 NextAttr = Attrs->getNext();
2214 Attrs->setNext(PrevAttr);
2215 PrevAttr = Attrs;
2216 Attrs = NextAttr;
2217 }
2218
2219 return PrevAttr;
2220}
2221
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002222Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002223 // Within the bitstream, expressions are stored in Reverse Polish
2224 // Notation, with each of the subexpressions preceding the
2225 // expression they are stored in. To evaluate expressions, we
2226 // continue reading expressions and placing them on the stack, with
2227 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002228 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002229 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002230 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002231 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002232 llvm::SmallVector<Stmt *, 16> StmtStack;
2233 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002234 Stmt::EmptyShell Empty;
2235
Douglas Gregora151ba42009-04-14 23:32:43 +00002236 while (true) {
2237 unsigned Code = Stream.ReadCode();
2238 if (Code == llvm::bitc::END_BLOCK) {
2239 if (Stream.ReadBlockEnd()) {
2240 Error("Error at end of Source Manager block");
2241 return 0;
2242 }
2243 break;
2244 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002245
Douglas Gregora151ba42009-04-14 23:32:43 +00002246 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2247 // No known subblocks, always skip them.
2248 Stream.ReadSubBlockID();
2249 if (Stream.SkipBlock()) {
2250 Error("Malformed block record");
2251 return 0;
2252 }
2253 continue;
2254 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002255
Douglas Gregora151ba42009-04-14 23:32:43 +00002256 if (Code == llvm::bitc::DEFINE_ABBREV) {
2257 Stream.ReadAbbrevRecord();
2258 continue;
2259 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002260
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002261 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002262 Idx = 0;
2263 Record.clear();
2264 bool Finished = false;
2265 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002266 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002267 Finished = true;
2268 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002269
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002270 case pch::STMT_NULL_PTR:
2271 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002272 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002273
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002274 case pch::STMT_NULL:
2275 S = new (Context) NullStmt(Empty);
2276 break;
2277
2278 case pch::STMT_COMPOUND:
2279 S = new (Context) CompoundStmt(Empty);
2280 break;
2281
2282 case pch::STMT_CASE:
2283 S = new (Context) CaseStmt(Empty);
2284 break;
2285
2286 case pch::STMT_DEFAULT:
2287 S = new (Context) DefaultStmt(Empty);
2288 break;
2289
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002290 case pch::STMT_LABEL:
2291 S = new (Context) LabelStmt(Empty);
2292 break;
2293
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002294 case pch::STMT_IF:
2295 S = new (Context) IfStmt(Empty);
2296 break;
2297
2298 case pch::STMT_SWITCH:
2299 S = new (Context) SwitchStmt(Empty);
2300 break;
2301
Douglas Gregora6b503f2009-04-17 00:16:09 +00002302 case pch::STMT_WHILE:
2303 S = new (Context) WhileStmt(Empty);
2304 break;
2305
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002306 case pch::STMT_DO:
2307 S = new (Context) DoStmt(Empty);
2308 break;
2309
2310 case pch::STMT_FOR:
2311 S = new (Context) ForStmt(Empty);
2312 break;
2313
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002314 case pch::STMT_GOTO:
2315 S = new (Context) GotoStmt(Empty);
2316 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002317
2318 case pch::STMT_INDIRECT_GOTO:
2319 S = new (Context) IndirectGotoStmt(Empty);
2320 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002321
Douglas Gregora6b503f2009-04-17 00:16:09 +00002322 case pch::STMT_CONTINUE:
2323 S = new (Context) ContinueStmt(Empty);
2324 break;
2325
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002326 case pch::STMT_BREAK:
2327 S = new (Context) BreakStmt(Empty);
2328 break;
2329
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002330 case pch::STMT_RETURN:
2331 S = new (Context) ReturnStmt(Empty);
2332 break;
2333
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002334 case pch::STMT_DECL:
2335 S = new (Context) DeclStmt(Empty);
2336 break;
2337
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002338 case pch::STMT_ASM:
2339 S = new (Context) AsmStmt(Empty);
2340 break;
2341
Douglas Gregora151ba42009-04-14 23:32:43 +00002342 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002343 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002344 break;
2345
2346 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002347 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002348 break;
2349
2350 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002351 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002352 break;
2353
2354 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002355 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002356 break;
2357
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002358 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002359 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002360 break;
2361
Douglas Gregor596e0932009-04-15 16:35:07 +00002362 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002363 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002364 Record[PCHStmtReader::NumExprFields + 1]);
2365 break;
2366
Douglas Gregora151ba42009-04-14 23:32:43 +00002367 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002368 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002369 break;
2370
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002371 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002372 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002373 break;
2374
Douglas Gregor12d74052009-04-15 15:58:59 +00002375 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002376 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002377 break;
2378
2379 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002380 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002381 break;
2382
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002383 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002384 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002385 break;
2386
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002387 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002388 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002389 break;
2390
2391 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002392 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002393 break;
2394
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002395 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002396 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002397 break;
2398
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002399 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002400 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002401 break;
2402
2403 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002404 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002405 break;
2406
Douglas Gregora151ba42009-04-14 23:32:43 +00002407 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002408 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002409 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002410
2411 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002412 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002413 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002414
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002415 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002416 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002417 break;
2418
Douglas Gregorec0b8292009-04-15 23:02:49 +00002419 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002420 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002421 break;
2422
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002423 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002424 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002425 break;
2426
2427 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002428 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002429 Record[PCHStmtReader::NumExprFields] - 1);
2430
2431 break;
2432
2433 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002434 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002435 break;
2436
Douglas Gregorec0b8292009-04-15 23:02:49 +00002437 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002438 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002439 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002440
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002441 case pch::EXPR_ADDR_LABEL:
2442 S = new (Context) AddrLabelExpr(Empty);
2443 break;
2444
Douglas Gregoreca12f62009-04-17 19:05:30 +00002445 case pch::EXPR_STMT:
2446 S = new (Context) StmtExpr(Empty);
2447 break;
2448
Douglas Gregor209d4622009-04-15 23:33:31 +00002449 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002450 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002451 break;
2452
2453 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002454 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002455 break;
2456
2457 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002458 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002459 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002460
2461 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002462 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002463 break;
2464
Douglas Gregore246b742009-04-17 19:21:43 +00002465 case pch::EXPR_BLOCK:
2466 S = new (Context) BlockExpr(Empty);
2467 break;
2468
Douglas Gregor725e94b2009-04-16 00:01:45 +00002469 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002470 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002471 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002472 }
2473
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002474 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002475 if (Finished)
2476 break;
2477
Douglas Gregor456e0952009-04-17 22:13:46 +00002478 ++NumStatementsRead;
2479
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002480 if (S) {
2481 unsigned NumSubStmts = Reader.Visit(S);
2482 while (NumSubStmts > 0) {
2483 StmtStack.pop_back();
2484 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002485 }
2486 }
2487
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002488 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002489 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002490 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002491 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002492 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002493 return StmtStack.back();
2494}
2495
2496Expr *PCHReader::ReadExpr() {
2497 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002498}
2499
Douglas Gregor179cfb12009-04-10 20:39:37 +00002500DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002501 return Diag(SourceLocation(), DiagID);
2502}
2503
2504DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2505 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002506 Context.getSourceManager()),
2507 DiagID);
2508}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002509
2510/// \brief Record that the given ID maps to the given switch-case
2511/// statement.
2512void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2513 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2514 SwitchCaseStmts[ID] = SC;
2515}
2516
2517/// \brief Retrieve the switch-case statement with the given ID.
2518SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2519 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2520 return SwitchCaseStmts[ID];
2521}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002522
2523/// \brief Record that the given label statement has been
2524/// deserialized and has the given ID.
2525void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2526 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2527 "Deserialized label twice");
2528 LabelStmts[ID] = S;
2529
2530 // If we've already seen any goto statements that point to this
2531 // label, resolve them now.
2532 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2533 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2534 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2535 Goto->second->setLabel(S);
2536 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002537
2538 // If we've already seen any address-label statements that point to
2539 // this label, resolve them now.
2540 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2541 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2542 = UnresolvedAddrLabelExprs.equal_range(ID);
2543 for (AddrLabelIter AddrLabel = AddrLabels.first;
2544 AddrLabel != AddrLabels.second; ++AddrLabel)
2545 AddrLabel->second->setLabel(S);
2546 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002547}
2548
2549/// \brief Set the label of the given statement to the label
2550/// identified by ID.
2551///
2552/// Depending on the order in which the label and other statements
2553/// referencing that label occur, this operation may complete
2554/// immediately (updating the statement) or it may queue the
2555/// statement to be back-patched later.
2556void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2557 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2558 if (Label != LabelStmts.end()) {
2559 // We've already seen this label, so set the label of the goto and
2560 // we're done.
2561 S->setLabel(Label->second);
2562 } else {
2563 // We haven't seen this label yet, so add this goto to the set of
2564 // unresolved goto statements.
2565 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2566 }
2567}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002568
2569/// \brief Set the label of the given expression to the label
2570/// identified by ID.
2571///
2572/// Depending on the order in which the label and other statements
2573/// referencing that label occur, this operation may complete
2574/// immediately (updating the statement) or it may queue the
2575/// statement to be back-patched later.
2576void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2577 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2578 if (Label != LabelStmts.end()) {
2579 // We've already seen this label, so set the label of the
2580 // label-address expression and we're done.
2581 S->setLabel(Label->second);
2582 } else {
2583 // We haven't seen this label yet, so add this label-address
2584 // expression to the set of unresolved label-address expressions.
2585 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2586 }
2587}