blob: 9af75aef2b4f90687849a2926a53931b4ef61367 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor8c700062009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000142 if (Record[Idx++])
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000143 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor8c700062009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000167}
168
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor1028bc62009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000200 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-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 Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000218//===----------------------------------------------------------------------===//
219// Statement/expression deserialization
220//===----------------------------------------------------------------------===//
221namespace {
222 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000223 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000224 PCHReader &Reader;
225 const PCHReader::RecordData &Record;
226 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000227 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000228
229 public:
230 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000231 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
232 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000233
Douglas Gregor025452f2009-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 Gregor673ecd62009-04-15 16:35:07 +0000238 /// \brief The number of record fields required for the Expr class
239 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000240 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000241
Douglas Gregor087fd532009-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 Gregor025452f2009-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 Gregor1de05fe2009-04-17 18:18:49 +0000253 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000254 unsigned VisitIfStmt(IfStmt *S);
255 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000256 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000257 unsigned VisitDoStmt(DoStmt *S);
258 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000259 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000260 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000261 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000262 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000263 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000264 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000265 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-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 Gregorcb2ca732009-04-15 22:19:53 +0000271 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000272 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000273 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000274 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000275 unsigned VisitUnaryOperator(UnaryOperator *E);
276 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000277 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000278 unsigned VisitCallExpr(CallExpr *E);
279 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000280 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000281 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000282 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
283 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000284 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000285 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
286 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000287 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000288 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000289 unsigned VisitInitListExpr(InitListExpr *E);
290 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
291 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000292 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000293 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000294 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000295 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
296 unsigned VisitChooseExpr(ChooseExpr *E);
297 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000298 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000299 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000300 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000301 };
302}
303
Douglas Gregor025452f2009-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 Gregor1de05fe2009-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 Gregor025452f2009-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 Gregord921cf92009-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 Gregor67d82492009-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 Gregor1de05fe2009-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 Gregor7d5c2f22009-04-17 18:58:21 +0000416unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
417 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000418 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000419 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
420 return 1;
421}
422
Douglas Gregord921cf92009-04-17 00:16:09 +0000423unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
424 VisitStmt(S);
425 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
426 return 0;
427}
428
Douglas Gregor025452f2009-04-17 00:04:06 +0000429unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
430 VisitStmt(S);
431 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
432 return 0;
433}
434
Douglas Gregor0de9d882009-04-17 16:34:57 +0000435unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
436 VisitStmt(S);
437 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
438 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
439 return 1;
440}
441
Douglas Gregor84f21702009-04-17 16:55:36 +0000442unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
443 VisitStmt(S);
444 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
445 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
446
447 if (Idx + 1 == Record.size()) {
448 // Single declaration
449 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
450 } else {
451 llvm::SmallVector<Decl *, 16> Decls;
452 Decls.reserve(Record.size() - Idx);
453 for (unsigned N = Record.size(); Idx != N; ++Idx)
454 Decls.push_back(Reader.GetDecl(Record[Idx]));
455 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
456 &Decls[0], Decls.size())));
457 }
458 return 0;
459}
460
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000461unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
462 VisitStmt(S);
463 unsigned NumOutputs = Record[Idx++];
464 unsigned NumInputs = Record[Idx++];
465 unsigned NumClobbers = Record[Idx++];
466 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
467 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
468 S->setVolatile(Record[Idx++]);
469 S->setSimple(Record[Idx++]);
470
471 unsigned StackIdx
472 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
473 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
474
475 // Outputs and inputs
476 llvm::SmallVector<std::string, 16> Names;
477 llvm::SmallVector<StringLiteral*, 16> Constraints;
478 llvm::SmallVector<Stmt*, 16> Exprs;
479 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
480 Names.push_back(Reader.ReadString(Record, Idx));
481 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
482 Exprs.push_back(StmtStack[StackIdx++]);
483 }
484 S->setOutputsAndInputs(NumOutputs, NumInputs,
485 &Names[0], &Constraints[0], &Exprs[0]);
486
487 // Constraints
488 llvm::SmallVector<StringLiteral*, 16> Clobbers;
489 for (unsigned I = 0; I != NumClobbers; ++I)
490 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
491 S->setClobbers(&Clobbers[0], NumClobbers);
492
493 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
494 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
495}
496
Douglas Gregor087fd532009-04-14 23:32:43 +0000497unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000498 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000499 E->setType(Reader.GetType(Record[Idx++]));
500 E->setTypeDependent(Record[Idx++]);
501 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000502 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000503 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000504}
505
Douglas Gregor087fd532009-04-14 23:32:43 +0000506unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000507 VisitExpr(E);
508 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
509 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000510 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000511}
512
Douglas Gregor087fd532009-04-14 23:32:43 +0000513unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000514 VisitExpr(E);
515 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
516 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000517 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000518}
519
Douglas Gregor087fd532009-04-14 23:32:43 +0000520unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000521 VisitExpr(E);
522 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
523 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000524 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000525}
526
Douglas Gregor087fd532009-04-14 23:32:43 +0000527unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000528 VisitExpr(E);
529 E->setValue(Reader.ReadAPFloat(Record, Idx));
530 E->setExact(Record[Idx++]);
531 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000532 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000533}
534
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000535unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
536 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000537 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000538 return 1;
539}
540
Douglas Gregor673ecd62009-04-15 16:35:07 +0000541unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
542 VisitExpr(E);
543 unsigned Len = Record[Idx++];
544 assert(Record[Idx] == E->getNumConcatenated() &&
545 "Wrong number of concatenated tokens!");
546 ++Idx;
547 E->setWide(Record[Idx++]);
548
549 // Read string data
550 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
551 E->setStrData(Reader.getContext(), &Str[0], Len);
552 Idx += Len;
553
554 // Read source locations
555 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
556 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
557
558 return 0;
559}
560
Douglas Gregor087fd532009-04-14 23:32:43 +0000561unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000562 VisitExpr(E);
563 E->setValue(Record[Idx++]);
564 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
565 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000566 return 0;
567}
568
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000569unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
570 VisitExpr(E);
571 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
572 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000573 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000574 return 1;
575}
576
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000577unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
578 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000579 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000580 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
581 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
582 return 1;
583}
584
585unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
586 VisitExpr(E);
587 E->setSizeof(Record[Idx++]);
588 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000589 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000590 ++Idx;
591 } else {
592 E->setArgument(Reader.GetType(Record[Idx++]));
593 }
594 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
596 return E->isArgumentType()? 0 : 1;
597}
598
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000599unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
600 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000601 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
602 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000603 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 return 2;
605}
606
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000607unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
608 VisitExpr(E);
609 E->setNumArgs(Reader.getContext(), Record[Idx++]);
610 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000611 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000612 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000613 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000614 return E->getNumArgs() + 1;
615}
616
617unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
618 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000619 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000620 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
621 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
622 E->setArrow(Record[Idx++]);
623 return 1;
624}
625
Douglas Gregor087fd532009-04-14 23:32:43 +0000626unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
627 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000628 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000629 return 1;
630}
631
Douglas Gregordb600c32009-04-15 00:25:59 +0000632unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
633 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000634 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
635 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000636 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
637 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
638 return 2;
639}
640
Douglas Gregorad90e962009-04-15 22:40:36 +0000641unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
642 VisitBinaryOperator(E);
643 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
644 E->setComputationResultType(Reader.GetType(Record[Idx++]));
645 return 2;
646}
647
648unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
649 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000650 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
651 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
652 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000653 return 3;
654}
655
Douglas Gregor087fd532009-04-14 23:32:43 +0000656unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
657 VisitCastExpr(E);
658 E->setLvalueCast(Record[Idx++]);
659 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000660}
661
Douglas Gregordb600c32009-04-15 00:25:59 +0000662unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
663 VisitCastExpr(E);
664 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
665 return 1;
666}
667
668unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
669 VisitExplicitCastExpr(E);
670 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
671 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
672 return 1;
673}
674
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000675unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
676 VisitExpr(E);
677 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000678 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000679 E->setFileScope(Record[Idx++]);
680 return 1;
681}
682
Douglas Gregord3c98a02009-04-15 23:02:49 +0000683unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
684 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000685 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000686 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
687 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
688 return 1;
689}
690
Douglas Gregord077d752009-04-16 00:55:48 +0000691unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
692 VisitExpr(E);
693 unsigned NumInits = Record[Idx++];
694 E->reserveInits(NumInits);
695 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000696 E->updateInit(I,
697 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
698 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000699 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
700 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
701 E->setInitializedFieldInUnion(
702 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
703 E->sawArrayRangeDesignator(Record[Idx++]);
704 return NumInits + 1;
705}
706
707unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
708 typedef DesignatedInitExpr::Designator Designator;
709
710 VisitExpr(E);
711 unsigned NumSubExprs = Record[Idx++];
712 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
713 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000714 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000715 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
716 E->setGNUSyntax(Record[Idx++]);
717
718 llvm::SmallVector<Designator, 4> Designators;
719 while (Idx < Record.size()) {
720 switch ((pch::DesignatorTypes)Record[Idx++]) {
721 case pch::DESIG_FIELD_DECL: {
722 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
723 SourceLocation DotLoc
724 = SourceLocation::getFromRawEncoding(Record[Idx++]);
725 SourceLocation FieldLoc
726 = SourceLocation::getFromRawEncoding(Record[Idx++]);
727 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
728 FieldLoc));
729 Designators.back().setField(Field);
730 break;
731 }
732
733 case pch::DESIG_FIELD_NAME: {
734 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
735 SourceLocation DotLoc
736 = SourceLocation::getFromRawEncoding(Record[Idx++]);
737 SourceLocation FieldLoc
738 = SourceLocation::getFromRawEncoding(Record[Idx++]);
739 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
740 break;
741 }
742
743 case pch::DESIG_ARRAY: {
744 unsigned Index = Record[Idx++];
745 SourceLocation LBracketLoc
746 = SourceLocation::getFromRawEncoding(Record[Idx++]);
747 SourceLocation RBracketLoc
748 = SourceLocation::getFromRawEncoding(Record[Idx++]);
749 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
750 break;
751 }
752
753 case pch::DESIG_ARRAY_RANGE: {
754 unsigned Index = Record[Idx++];
755 SourceLocation LBracketLoc
756 = SourceLocation::getFromRawEncoding(Record[Idx++]);
757 SourceLocation EllipsisLoc
758 = SourceLocation::getFromRawEncoding(Record[Idx++]);
759 SourceLocation RBracketLoc
760 = SourceLocation::getFromRawEncoding(Record[Idx++]);
761 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
762 RBracketLoc));
763 break;
764 }
765 }
766 }
767 E->setDesignators(&Designators[0], Designators.size());
768
769 return NumSubExprs;
770}
771
772unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
773 VisitExpr(E);
774 return 0;
775}
776
Douglas Gregord3c98a02009-04-15 23:02:49 +0000777unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
778 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000779 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000780 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
782 return 1;
783}
784
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000785unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
786 VisitExpr(E);
787 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
789 Reader.SetLabelOf(E, Record[Idx++]);
790 return 0;
791}
792
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000793unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
794 VisitExpr(E);
795 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
796 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
797 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
798 return 1;
799}
800
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000801unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
802 VisitExpr(E);
803 E->setArgType1(Reader.GetType(Record[Idx++]));
804 E->setArgType2(Reader.GetType(Record[Idx++]));
805 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
807 return 0;
808}
809
810unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
811 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000812 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
813 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
814 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000815 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
816 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
817 return 3;
818}
819
820unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
821 VisitExpr(E);
822 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
823 return 0;
824}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000825
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000826unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
827 VisitExpr(E);
828 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000829 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000830 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
831 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
832 return NumExprs;
833}
834
Douglas Gregor84af7c22009-04-17 19:21:43 +0000835unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
836 VisitExpr(E);
837 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
838 E->setHasBlockDeclRefExprs(Record[Idx++]);
839 return 0;
840}
841
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000842unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
843 VisitExpr(E);
844 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
845 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
846 E->setByRef(Record[Idx++]);
847 return 0;
848}
849
Douglas Gregor2cf26342009-04-09 22:27:44 +0000850// FIXME: use the diagnostics machinery
851static bool Error(const char *Str) {
852 std::fprintf(stderr, "%s\n", Str);
853 return true;
854}
855
Douglas Gregore1d918e2009-04-10 23:10:45 +0000856/// \brief Check the contents of the predefines buffer against the
857/// contents of the predefines buffer used to build the PCH file.
858///
859/// The contents of the two predefines buffers should be the same. If
860/// not, then some command-line option changed the preprocessor state
861/// and we must reject the PCH file.
862///
863/// \param PCHPredef The start of the predefines buffer in the PCH
864/// file.
865///
866/// \param PCHPredefLen The length of the predefines buffer in the PCH
867/// file.
868///
869/// \param PCHBufferID The FileID for the PCH predefines buffer.
870///
871/// \returns true if there was a mismatch (in which case the PCH file
872/// should be ignored), or false otherwise.
873bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
874 unsigned PCHPredefLen,
875 FileID PCHBufferID) {
876 const char *Predef = PP.getPredefines().c_str();
877 unsigned PredefLen = PP.getPredefines().size();
878
879 // If the two predefines buffers compare equal, we're done!.
880 if (PredefLen == PCHPredefLen &&
881 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
882 return false;
883
884 // The predefines buffers are different. Produce a reasonable
885 // diagnostic showing where they are different.
886
887 // The source locations (potentially in the two different predefines
888 // buffers)
889 SourceLocation Loc1, Loc2;
890 SourceManager &SourceMgr = PP.getSourceManager();
891
892 // Create a source buffer for our predefines string, so
893 // that we can build a diagnostic that points into that
894 // source buffer.
895 FileID BufferID;
896 if (Predef && Predef[0]) {
897 llvm::MemoryBuffer *Buffer
898 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
899 "<built-in>");
900 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
901 }
902
903 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
904 std::pair<const char *, const char *> Locations
905 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
906
907 if (Locations.first != Predef + MinLen) {
908 // We found the location in the two buffers where there is a
909 // difference. Form source locations to point there (in both
910 // buffers).
911 unsigned Offset = Locations.first - Predef;
912 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
913 .getFileLocWithOffset(Offset);
914 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
915 .getFileLocWithOffset(Offset);
916 } else if (PredefLen > PCHPredefLen) {
917 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
918 .getFileLocWithOffset(MinLen);
919 } else {
920 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
921 .getFileLocWithOffset(MinLen);
922 }
923
924 Diag(Loc1, diag::warn_pch_preprocessor);
925 if (Loc2.isValid())
926 Diag(Loc2, diag::note_predef_in_pch);
927 Diag(diag::note_ignoring_pch) << FileName;
928 return true;
929}
930
Douglas Gregorbd945002009-04-13 16:31:14 +0000931/// \brief Read the line table in the source manager block.
932/// \returns true if ther was an error.
933static bool ParseLineTable(SourceManager &SourceMgr,
934 llvm::SmallVectorImpl<uint64_t> &Record) {
935 unsigned Idx = 0;
936 LineTableInfo &LineTable = SourceMgr.getLineTable();
937
938 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000939 std::map<int, int> FileIDs;
940 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000941 // Extract the file name
942 unsigned FilenameLen = Record[Idx++];
943 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
944 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000945 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
946 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000947 }
948
949 // Parse the line entries
950 std::vector<LineEntry> Entries;
951 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000952 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000953
954 // Extract the line entries
955 unsigned NumEntries = Record[Idx++];
956 Entries.clear();
957 Entries.reserve(NumEntries);
958 for (unsigned I = 0; I != NumEntries; ++I) {
959 unsigned FileOffset = Record[Idx++];
960 unsigned LineNo = Record[Idx++];
961 int FilenameID = Record[Idx++];
962 SrcMgr::CharacteristicKind FileKind
963 = (SrcMgr::CharacteristicKind)Record[Idx++];
964 unsigned IncludeOffset = Record[Idx++];
965 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
966 FileKind, IncludeOffset));
967 }
968 LineTable.AddEntry(FID, Entries);
969 }
970
971 return false;
972}
973
Douglas Gregor14f79002009-04-10 03:52:48 +0000974/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000975PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000976 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000977 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
978 Error("Malformed source manager block record");
979 return Failure;
980 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000981
982 SourceManager &SourceMgr = Context.getSourceManager();
983 RecordData Record;
984 while (true) {
985 unsigned Code = Stream.ReadCode();
986 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000987 if (Stream.ReadBlockEnd()) {
988 Error("Error at end of Source Manager block");
989 return Failure;
990 }
991
992 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000993 }
994
995 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
996 // No known subblocks, always skip them.
997 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000998 if (Stream.SkipBlock()) {
999 Error("Malformed block record");
1000 return Failure;
1001 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001002 continue;
1003 }
1004
1005 if (Code == llvm::bitc::DEFINE_ABBREV) {
1006 Stream.ReadAbbrevRecord();
1007 continue;
1008 }
1009
1010 // Read a record.
1011 const char *BlobStart;
1012 unsigned BlobLen;
1013 Record.clear();
1014 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1015 default: // Default behavior: ignore.
1016 break;
1017
1018 case pch::SM_SLOC_FILE_ENTRY: {
1019 // FIXME: We would really like to delay the creation of this
1020 // FileEntry until it is actually required, e.g., when producing
1021 // a diagnostic with a source location in this file.
1022 const FileEntry *File
1023 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1024 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001025 FileID ID = SourceMgr.createFileID(File,
1026 SourceLocation::getFromRawEncoding(Record[1]),
1027 (CharacteristicKind)Record[2]);
1028 if (Record[3])
1029 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1030 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001031 break;
1032 }
1033
1034 case pch::SM_SLOC_BUFFER_ENTRY: {
1035 const char *Name = BlobStart;
1036 unsigned Code = Stream.ReadCode();
1037 Record.clear();
1038 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1039 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001040 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001041 llvm::MemoryBuffer *Buffer
1042 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1043 BlobStart + BlobLen - 1,
1044 Name);
1045 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1046
1047 if (strcmp(Name, "<built-in>") == 0
1048 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1049 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001050 break;
1051 }
1052
1053 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1054 SourceLocation SpellingLoc
1055 = SourceLocation::getFromRawEncoding(Record[1]);
1056 SourceMgr.createInstantiationLoc(
1057 SpellingLoc,
1058 SourceLocation::getFromRawEncoding(Record[2]),
1059 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001060 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001061 break;
1062 }
1063
Chris Lattner2c78b872009-04-14 23:22:57 +00001064 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001065 if (ParseLineTable(SourceMgr, Record))
1066 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001067 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001068 }
1069 }
1070}
1071
Chris Lattner42d42b52009-04-10 21:41:48 +00001072bool PCHReader::ReadPreprocessorBlock() {
1073 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1074 return Error("Malformed preprocessor block record");
1075
Chris Lattner42d42b52009-04-10 21:41:48 +00001076 RecordData Record;
1077 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1078 MacroInfo *LastMacro = 0;
1079
1080 while (true) {
1081 unsigned Code = Stream.ReadCode();
1082 switch (Code) {
1083 case llvm::bitc::END_BLOCK:
1084 if (Stream.ReadBlockEnd())
1085 return Error("Error at end of preprocessor block");
1086 return false;
1087
1088 case llvm::bitc::ENTER_SUBBLOCK:
1089 // No known subblocks, always skip them.
1090 Stream.ReadSubBlockID();
1091 if (Stream.SkipBlock())
1092 return Error("Malformed block record");
1093 continue;
1094
1095 case llvm::bitc::DEFINE_ABBREV:
1096 Stream.ReadAbbrevRecord();
1097 continue;
1098 default: break;
1099 }
1100
1101 // Read a record.
1102 Record.clear();
1103 pch::PreprocessorRecordTypes RecType =
1104 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1105 switch (RecType) {
1106 default: // Default behavior: ignore unknown records.
1107 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001108 case pch::PP_COUNTER_VALUE:
1109 if (!Record.empty())
1110 PP.setCounterValue(Record[0]);
1111 break;
1112
Chris Lattner42d42b52009-04-10 21:41:48 +00001113 case pch::PP_MACRO_OBJECT_LIKE:
1114 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +00001115 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1116 if (II == 0)
1117 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +00001118 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1119 bool isUsed = Record[2];
1120
1121 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1122 MI->setIsUsed(isUsed);
1123
1124 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1125 // Decode function-like macro info.
1126 bool isC99VarArgs = Record[3];
1127 bool isGNUVarArgs = Record[4];
1128 MacroArgs.clear();
1129 unsigned NumArgs = Record[5];
1130 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +00001131 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +00001132
1133 // Install function-like macro info.
1134 MI->setIsFunctionLike();
1135 if (isC99VarArgs) MI->setIsC99Varargs();
1136 if (isGNUVarArgs) MI->setIsGNUVarargs();
1137 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1138 PP.getPreprocessorAllocator());
1139 }
1140
1141 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +00001142 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +00001143
1144 // Remember that we saw this macro last so that we add the tokens that
1145 // form its body to it.
1146 LastMacro = MI;
1147 break;
1148 }
1149
1150 case pch::PP_TOKEN: {
1151 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1152 // pretend we didn't see this.
1153 if (LastMacro == 0) break;
1154
1155 Token Tok;
1156 Tok.startToken();
1157 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1158 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001159 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1160 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001161 Tok.setKind((tok::TokenKind)Record[3]);
1162 Tok.setFlag((Token::TokenFlags)Record[4]);
1163 LastMacro->AddTokenToBody(Tok);
1164 break;
1165 }
1166 }
1167 }
1168}
1169
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001170PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1171 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1172 Error("Malformed block record");
1173 return Failure;
1174 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001175
Chris Lattner7356a312009-04-11 21:15:38 +00001176 uint64_t PreprocessorBlockBit = 0;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001177
Douglas Gregor2cf26342009-04-09 22:27:44 +00001178 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001179 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001180 while (!Stream.AtEndOfStream()) {
1181 unsigned Code = Stream.ReadCode();
1182 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001183 // If we saw the preprocessor block, read it now.
1184 if (PreprocessorBlockBit) {
1185 uint64_t SavedPos = Stream.GetCurrentBitNo();
1186 Stream.JumpToBit(PreprocessorBlockBit);
1187 if (ReadPreprocessorBlock()) {
1188 Error("Malformed preprocessor block");
1189 return Failure;
1190 }
1191 Stream.JumpToBit(SavedPos);
1192 }
1193
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001194 if (Stream.ReadBlockEnd()) {
1195 Error("Error at end of module block");
1196 return Failure;
1197 }
Chris Lattner7356a312009-04-11 21:15:38 +00001198
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001199 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001200 }
1201
1202 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1203 switch (Stream.ReadSubBlockID()) {
1204 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1205 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1206 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001207 if (Stream.SkipBlock()) {
1208 Error("Malformed block record");
1209 return Failure;
1210 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001211 break;
1212
Chris Lattner7356a312009-04-11 21:15:38 +00001213 case pch::PREPROCESSOR_BLOCK_ID:
1214 // Skip the preprocessor block for now, but remember where it is. We
1215 // want to read it in after the identifier table.
1216 if (PreprocessorBlockBit) {
1217 Error("Multiple preprocessor blocks found.");
1218 return Failure;
1219 }
1220 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1221 if (Stream.SkipBlock()) {
1222 Error("Malformed block record");
1223 return Failure;
1224 }
1225 break;
1226
Douglas Gregor14f79002009-04-10 03:52:48 +00001227 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001228 switch (ReadSourceManagerBlock()) {
1229 case Success:
1230 break;
1231
1232 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001233 Error("Malformed source manager block");
1234 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001235
1236 case IgnorePCH:
1237 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001238 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001239 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001240 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001241 continue;
1242 }
1243
1244 if (Code == llvm::bitc::DEFINE_ABBREV) {
1245 Stream.ReadAbbrevRecord();
1246 continue;
1247 }
1248
1249 // Read and process a record.
1250 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001251 const char *BlobStart = 0;
1252 unsigned BlobLen = 0;
1253 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1254 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001255 default: // Default behavior: ignore.
1256 break;
1257
1258 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001259 if (!TypeOffsets.empty()) {
1260 Error("Duplicate TYPE_OFFSET record in PCH file");
1261 return Failure;
1262 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001263 TypeOffsets.swap(Record);
1264 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1265 break;
1266
1267 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001268 if (!DeclOffsets.empty()) {
1269 Error("Duplicate DECL_OFFSET record in PCH file");
1270 return Failure;
1271 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001272 DeclOffsets.swap(Record);
1273 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1274 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001275
1276 case pch::LANGUAGE_OPTIONS:
1277 if (ParseLanguageOptions(Record))
1278 return IgnorePCH;
1279 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001280
Douglas Gregorafaf3082009-04-11 00:14:32 +00001281 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001282 std::string TargetTriple(BlobStart, BlobLen);
1283 if (TargetTriple != Context.Target.getTargetTriple()) {
1284 Diag(diag::warn_pch_target_triple)
1285 << TargetTriple << Context.Target.getTargetTriple();
1286 Diag(diag::note_ignoring_pch) << FileName;
1287 return IgnorePCH;
1288 }
1289 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001290 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001291
1292 case pch::IDENTIFIER_TABLE:
1293 IdentifierTable = BlobStart;
1294 break;
1295
1296 case pch::IDENTIFIER_OFFSET:
1297 if (!IdentifierData.empty()) {
1298 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1299 return Failure;
1300 }
1301 IdentifierData.swap(Record);
1302#ifndef NDEBUG
1303 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1304 if ((IdentifierData[I] & 0x01) == 0) {
1305 Error("Malformed identifier table in the precompiled header");
1306 return Failure;
1307 }
1308 }
1309#endif
1310 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001311
1312 case pch::EXTERNAL_DEFINITIONS:
1313 if (!ExternalDefinitions.empty()) {
1314 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1315 return Failure;
1316 }
1317 ExternalDefinitions.swap(Record);
1318 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001319
Douglas Gregorad1de002009-04-18 05:55:16 +00001320 case pch::SPECIAL_TYPES:
1321 SpecialTypes.swap(Record);
1322 break;
1323
Douglas Gregor3e1af842009-04-17 22:13:46 +00001324 case pch::STATISTICS:
1325 TotalNumStatements = Record[0];
1326 break;
1327
Douglas Gregorafaf3082009-04-11 00:14:32 +00001328 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001329 }
1330
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001331 Error("Premature end of bitstream");
1332 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001333}
1334
Douglas Gregore1d918e2009-04-10 23:10:45 +00001335PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001336 // Set the PCH file name.
1337 this->FileName = FileName;
1338
Douglas Gregor2cf26342009-04-09 22:27:44 +00001339 // Open the PCH file.
1340 std::string ErrStr;
1341 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001342 if (!Buffer) {
1343 Error(ErrStr.c_str());
1344 return IgnorePCH;
1345 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001346
1347 // Initialize the stream
1348 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1349 (const unsigned char *)Buffer->getBufferEnd());
1350
1351 // Sniff for the signature.
1352 if (Stream.Read(8) != 'C' ||
1353 Stream.Read(8) != 'P' ||
1354 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001355 Stream.Read(8) != 'H') {
1356 Error("Not a PCH file");
1357 return IgnorePCH;
1358 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001359
1360 // We expect a number of well-defined blocks, though we don't necessarily
1361 // need to understand them all.
1362 while (!Stream.AtEndOfStream()) {
1363 unsigned Code = Stream.ReadCode();
1364
Douglas Gregore1d918e2009-04-10 23:10:45 +00001365 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1366 Error("Invalid record at top-level");
1367 return Failure;
1368 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001369
1370 unsigned BlockID = Stream.ReadSubBlockID();
1371
1372 // We only know the PCH subblock ID.
1373 switch (BlockID) {
1374 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001375 if (Stream.ReadBlockInfoBlock()) {
1376 Error("Malformed BlockInfoBlock");
1377 return Failure;
1378 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001379 break;
1380 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001381 switch (ReadPCHBlock()) {
1382 case Success:
1383 break;
1384
1385 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001386 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001387
1388 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001389 // FIXME: We could consider reading through to the end of this
1390 // PCH block, skipping subblocks, to see if there are other
1391 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001392 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001393 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001394 break;
1395 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001396 if (Stream.SkipBlock()) {
1397 Error("Malformed block record");
1398 return Failure;
1399 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001400 break;
1401 }
1402 }
1403
1404 // Load the translation unit declaration
1405 ReadDeclRecord(DeclOffsets[0], 0);
1406
Douglas Gregorad1de002009-04-18 05:55:16 +00001407 // Load the special types.
1408 Context.setBuiltinVaListType(
1409 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1410
Douglas Gregore1d918e2009-04-10 23:10:45 +00001411 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412}
1413
Douglas Gregor0b748912009-04-14 21:18:50 +00001414namespace {
1415 /// \brief Helper class that saves the current stream position and
1416 /// then restores it when destroyed.
1417 struct VISIBILITY_HIDDEN SavedStreamPosition {
1418 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001419 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001420
1421 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001422 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001423 }
1424
1425 private:
1426 llvm::BitstreamReader &Stream;
1427 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001428 };
1429}
1430
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001431/// \brief Parse the record that corresponds to a LangOptions data
1432/// structure.
1433///
1434/// This routine compares the language options used to generate the
1435/// PCH file against the language options set for the current
1436/// compilation. For each option, we classify differences between the
1437/// two compiler states as either "benign" or "important". Benign
1438/// differences don't matter, and we accept them without complaint
1439/// (and without modifying the language options). Differences between
1440/// the states for important options cause the PCH file to be
1441/// unusable, so we emit a warning and return true to indicate that
1442/// there was an error.
1443///
1444/// \returns true if the PCH file is unacceptable, false otherwise.
1445bool PCHReader::ParseLanguageOptions(
1446 const llvm::SmallVectorImpl<uint64_t> &Record) {
1447 const LangOptions &LangOpts = Context.getLangOptions();
1448#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1449#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1450 if (Record[Idx] != LangOpts.Option) { \
1451 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1452 Diag(diag::note_ignoring_pch) << FileName; \
1453 return true; \
1454 } \
1455 ++Idx
1456
1457 unsigned Idx = 0;
1458 PARSE_LANGOPT_BENIGN(Trigraphs);
1459 PARSE_LANGOPT_BENIGN(BCPLComment);
1460 PARSE_LANGOPT_BENIGN(DollarIdents);
1461 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1462 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1463 PARSE_LANGOPT_BENIGN(ImplicitInt);
1464 PARSE_LANGOPT_BENIGN(Digraphs);
1465 PARSE_LANGOPT_BENIGN(HexFloats);
1466 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1467 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1468 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1469 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1470 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1471 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1472 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1473 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1474 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1475 PARSE_LANGOPT_BENIGN(PascalStrings);
1476 PARSE_LANGOPT_BENIGN(Boolean);
1477 PARSE_LANGOPT_BENIGN(WritableStrings);
1478 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1479 diag::warn_pch_lax_vector_conversions);
1480 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1481 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1482 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1483 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1484 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1485 diag::warn_pch_thread_safe_statics);
1486 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1487 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1488 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1489 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1490 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1491 diag::warn_pch_heinous_extensions);
1492 // FIXME: Most of the options below are benign if the macro wasn't
1493 // used. Unfortunately, this means that a PCH compiled without
1494 // optimization can't be used with optimization turned on, even
1495 // though the only thing that changes is whether __OPTIMIZE__ was
1496 // defined... but if __OPTIMIZE__ never showed up in the header, it
1497 // doesn't matter. We could consider making this some special kind
1498 // of check.
1499 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1500 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1501 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1502 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1503 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1504 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1505 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1506 Diag(diag::warn_pch_gc_mode)
1507 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1508 Diag(diag::note_ignoring_pch) << FileName;
1509 return true;
1510 }
1511 ++Idx;
1512 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1513 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1514#undef PARSE_LANGOPT_IRRELEVANT
1515#undef PARSE_LANGOPT_BENIGN
1516
1517 return false;
1518}
1519
Douglas Gregor2cf26342009-04-09 22:27:44 +00001520/// \brief Read and return the type at the given offset.
1521///
1522/// This routine actually reads the record corresponding to the type
1523/// at the given offset in the bitstream. It is a helper routine for
1524/// GetType, which deals with reading type IDs.
1525QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001526 // Keep track of where we are in the stream, then jump back there
1527 // after reading this type.
1528 SavedStreamPosition SavedPosition(Stream);
1529
Douglas Gregor2cf26342009-04-09 22:27:44 +00001530 Stream.JumpToBit(Offset);
1531 RecordData Record;
1532 unsigned Code = Stream.ReadCode();
1533 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001534 case pch::TYPE_EXT_QUAL: {
1535 assert(Record.size() == 3 &&
1536 "Incorrect encoding of extended qualifier type");
1537 QualType Base = GetType(Record[0]);
1538 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1539 unsigned AddressSpace = Record[2];
1540
1541 QualType T = Base;
1542 if (GCAttr != QualType::GCNone)
1543 T = Context.getObjCGCQualType(T, GCAttr);
1544 if (AddressSpace)
1545 T = Context.getAddrSpaceQualType(T, AddressSpace);
1546 return T;
1547 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001548
Douglas Gregor2cf26342009-04-09 22:27:44 +00001549 case pch::TYPE_FIXED_WIDTH_INT: {
1550 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1551 return Context.getFixedWidthIntType(Record[0], Record[1]);
1552 }
1553
1554 case pch::TYPE_COMPLEX: {
1555 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1556 QualType ElemType = GetType(Record[0]);
1557 return Context.getComplexType(ElemType);
1558 }
1559
1560 case pch::TYPE_POINTER: {
1561 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1562 QualType PointeeType = GetType(Record[0]);
1563 return Context.getPointerType(PointeeType);
1564 }
1565
1566 case pch::TYPE_BLOCK_POINTER: {
1567 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1568 QualType PointeeType = GetType(Record[0]);
1569 return Context.getBlockPointerType(PointeeType);
1570 }
1571
1572 case pch::TYPE_LVALUE_REFERENCE: {
1573 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1574 QualType PointeeType = GetType(Record[0]);
1575 return Context.getLValueReferenceType(PointeeType);
1576 }
1577
1578 case pch::TYPE_RVALUE_REFERENCE: {
1579 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1580 QualType PointeeType = GetType(Record[0]);
1581 return Context.getRValueReferenceType(PointeeType);
1582 }
1583
1584 case pch::TYPE_MEMBER_POINTER: {
1585 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1586 QualType PointeeType = GetType(Record[0]);
1587 QualType ClassType = GetType(Record[1]);
1588 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1589 }
1590
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001591 case pch::TYPE_CONSTANT_ARRAY: {
1592 QualType ElementType = GetType(Record[0]);
1593 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1594 unsigned IndexTypeQuals = Record[2];
1595 unsigned Idx = 3;
1596 llvm::APInt Size = ReadAPInt(Record, Idx);
1597 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1598 }
1599
1600 case pch::TYPE_INCOMPLETE_ARRAY: {
1601 QualType ElementType = GetType(Record[0]);
1602 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1603 unsigned IndexTypeQuals = Record[2];
1604 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1605 }
1606
1607 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001608 QualType ElementType = GetType(Record[0]);
1609 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1610 unsigned IndexTypeQuals = Record[2];
1611 return Context.getVariableArrayType(ElementType, ReadExpr(),
1612 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001613 }
1614
1615 case pch::TYPE_VECTOR: {
1616 if (Record.size() != 2) {
1617 Error("Incorrect encoding of vector type in PCH file");
1618 return QualType();
1619 }
1620
1621 QualType ElementType = GetType(Record[0]);
1622 unsigned NumElements = Record[1];
1623 return Context.getVectorType(ElementType, NumElements);
1624 }
1625
1626 case pch::TYPE_EXT_VECTOR: {
1627 if (Record.size() != 2) {
1628 Error("Incorrect encoding of extended vector type in PCH file");
1629 return QualType();
1630 }
1631
1632 QualType ElementType = GetType(Record[0]);
1633 unsigned NumElements = Record[1];
1634 return Context.getExtVectorType(ElementType, NumElements);
1635 }
1636
1637 case pch::TYPE_FUNCTION_NO_PROTO: {
1638 if (Record.size() != 1) {
1639 Error("Incorrect encoding of no-proto function type");
1640 return QualType();
1641 }
1642 QualType ResultType = GetType(Record[0]);
1643 return Context.getFunctionNoProtoType(ResultType);
1644 }
1645
1646 case pch::TYPE_FUNCTION_PROTO: {
1647 QualType ResultType = GetType(Record[0]);
1648 unsigned Idx = 1;
1649 unsigned NumParams = Record[Idx++];
1650 llvm::SmallVector<QualType, 16> ParamTypes;
1651 for (unsigned I = 0; I != NumParams; ++I)
1652 ParamTypes.push_back(GetType(Record[Idx++]));
1653 bool isVariadic = Record[Idx++];
1654 unsigned Quals = Record[Idx++];
1655 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1656 isVariadic, Quals);
1657 }
1658
1659 case pch::TYPE_TYPEDEF:
1660 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1661 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1662
1663 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001664 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001665
1666 case pch::TYPE_TYPEOF: {
1667 if (Record.size() != 1) {
1668 Error("Incorrect encoding of typeof(type) in PCH file");
1669 return QualType();
1670 }
1671 QualType UnderlyingType = GetType(Record[0]);
1672 return Context.getTypeOfType(UnderlyingType);
1673 }
1674
1675 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001676 assert(Record.size() == 1 && "Incorrect encoding of record type");
1677 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001678
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001679 case pch::TYPE_ENUM:
1680 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1681 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1682
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001683 case pch::TYPE_OBJC_INTERFACE:
1684 // FIXME: Deserialize ObjCInterfaceType
1685 assert(false && "Cannot de-serialize ObjC interface types yet");
1686 return QualType();
1687
1688 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1689 // FIXME: Deserialize ObjCQualifiedInterfaceType
1690 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1691 return QualType();
1692
1693 case pch::TYPE_OBJC_QUALIFIED_ID:
1694 // FIXME: Deserialize ObjCQualifiedIdType
1695 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1696 return QualType();
1697
1698 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1699 // FIXME: Deserialize ObjCQualifiedClassType
1700 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1701 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001702 }
1703
1704 // Suppress a GCC warning
1705 return QualType();
1706}
1707
1708/// \brief Note that we have loaded the declaration with the given
1709/// Index.
1710///
1711/// This routine notes that this declaration has already been loaded,
1712/// so that future GetDecl calls will return this declaration rather
1713/// than trying to load a new declaration.
1714inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1715 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1716 DeclAlreadyLoaded[Index] = true;
1717 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1718}
1719
1720/// \brief Read the declaration at the given offset from the PCH file.
1721Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001722 // Keep track of where we are in the stream, then jump back there
1723 // after reading this declaration.
1724 SavedStreamPosition SavedPosition(Stream);
1725
Douglas Gregor2cf26342009-04-09 22:27:44 +00001726 Decl *D = 0;
1727 Stream.JumpToBit(Offset);
1728 RecordData Record;
1729 unsigned Code = Stream.ReadCode();
1730 unsigned Idx = 0;
1731 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001732
Douglas Gregor2cf26342009-04-09 22:27:44 +00001733 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001734 case pch::DECL_ATTR:
1735 case pch::DECL_CONTEXT_LEXICAL:
1736 case pch::DECL_CONTEXT_VISIBLE:
1737 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1738 break;
1739
Douglas Gregor2cf26342009-04-09 22:27:44 +00001740 case pch::DECL_TRANSLATION_UNIT:
1741 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001742 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001743 break;
1744
1745 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001746 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001747 break;
1748 }
1749
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001750 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001751 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001752 break;
1753 }
1754
Douglas Gregor8c700062009-04-13 21:20:57 +00001755 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001756 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1757 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001758 break;
1759 }
1760
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001761 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001762 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1763 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001764 break;
1765 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001766
1767 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001768 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1769 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001770 break;
1771 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001772
Douglas Gregor8c700062009-04-13 21:20:57 +00001773 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001774 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1775 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001776 break;
1777 }
1778
Douglas Gregor2cf26342009-04-09 22:27:44 +00001779 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001780 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1781 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001782 break;
1783 }
1784
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001785 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001786 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1787 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001788 break;
1789 }
1790
1791 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001792 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001793 QualType(), QualType(), VarDecl::None,
1794 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001795 break;
1796 }
1797
Douglas Gregor1028bc62009-04-13 22:49:25 +00001798 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001799 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001800 break;
1801 }
1802
1803 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001804 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001805 break;
1806 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001807 }
1808
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001809 assert(D && "Unknown declaration creating PCH file");
1810 if (D) {
1811 LoadedDecl(Index, D);
1812 Reader.Visit(D);
1813 }
1814
Douglas Gregor2cf26342009-04-09 22:27:44 +00001815 // If this declaration is also a declaration context, get the
1816 // offsets for its tables of lexical and visible declarations.
1817 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1818 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1819 if (Offsets.first || Offsets.second) {
1820 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1821 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1822 DeclContextOffsets[DC] = Offsets;
1823 }
1824 }
1825 assert(Idx == Record.size());
1826
1827 return D;
1828}
1829
Douglas Gregor8038d512009-04-10 17:25:41 +00001830QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001831 unsigned Quals = ID & 0x07;
1832 unsigned Index = ID >> 3;
1833
1834 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1835 QualType T;
1836 switch ((pch::PredefinedTypeIDs)Index) {
1837 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1838 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1839 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1840
1841 case pch::PREDEF_TYPE_CHAR_U_ID:
1842 case pch::PREDEF_TYPE_CHAR_S_ID:
1843 // FIXME: Check that the signedness of CharTy is correct!
1844 T = Context.CharTy;
1845 break;
1846
1847 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1848 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1849 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1850 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1851 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1852 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1853 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1854 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1855 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1856 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1857 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1858 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1859 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1860 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1861 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1862 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1863 }
1864
1865 assert(!T.isNull() && "Unknown predefined type");
1866 return T.getQualifiedType(Quals);
1867 }
1868
1869 Index -= pch::NUM_PREDEF_TYPE_IDS;
1870 if (!TypeAlreadyLoaded[Index]) {
1871 // Load the type from the PCH file.
1872 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1873 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1874 TypeAlreadyLoaded[Index] = true;
1875 }
1876
1877 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1878}
1879
Douglas Gregor8038d512009-04-10 17:25:41 +00001880Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001881 if (ID == 0)
1882 return 0;
1883
1884 unsigned Index = ID - 1;
1885 if (DeclAlreadyLoaded[Index])
1886 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1887
1888 // Load the declaration from the PCH file.
1889 return ReadDeclRecord(DeclOffsets[Index], Index);
1890}
1891
Douglas Gregor250fc9c2009-04-18 00:07:54 +00001892Stmt *PCHReader::GetStmt(uint64_t Offset) {
1893 // Keep track of where we are in the stream, then jump back there
1894 // after reading this declaration.
1895 SavedStreamPosition SavedPosition(Stream);
1896
1897 Stream.JumpToBit(Offset);
1898 return ReadStmt();
1899}
1900
Douglas Gregor2cf26342009-04-09 22:27:44 +00001901bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001902 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001903 assert(DC->hasExternalLexicalStorage() &&
1904 "DeclContext has no lexical decls in storage");
1905 uint64_t Offset = DeclContextOffsets[DC].first;
1906 assert(Offset && "DeclContext has no lexical decls in storage");
1907
Douglas Gregor0b748912009-04-14 21:18:50 +00001908 // Keep track of where we are in the stream, then jump back there
1909 // after reading this context.
1910 SavedStreamPosition SavedPosition(Stream);
1911
Douglas Gregor2cf26342009-04-09 22:27:44 +00001912 // Load the record containing all of the declarations lexically in
1913 // this context.
1914 Stream.JumpToBit(Offset);
1915 RecordData Record;
1916 unsigned Code = Stream.ReadCode();
1917 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001918 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001919 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1920
1921 // Load all of the declaration IDs
1922 Decls.clear();
1923 Decls.insert(Decls.end(), Record.begin(), Record.end());
1924 return false;
1925}
1926
1927bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1928 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1929 assert(DC->hasExternalVisibleStorage() &&
1930 "DeclContext has no visible decls in storage");
1931 uint64_t Offset = DeclContextOffsets[DC].second;
1932 assert(Offset && "DeclContext has no visible decls in storage");
1933
Douglas Gregor0b748912009-04-14 21:18:50 +00001934 // Keep track of where we are in the stream, then jump back there
1935 // after reading this context.
1936 SavedStreamPosition SavedPosition(Stream);
1937
Douglas Gregor2cf26342009-04-09 22:27:44 +00001938 // Load the record containing all of the declarations visible in
1939 // this context.
1940 Stream.JumpToBit(Offset);
1941 RecordData Record;
1942 unsigned Code = Stream.ReadCode();
1943 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001944 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001945 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1946 if (Record.size() == 0)
1947 return false;
1948
1949 Decls.clear();
1950
1951 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001952 while (Idx < Record.size()) {
1953 Decls.push_back(VisibleDeclaration());
1954 Decls.back().Name = ReadDeclarationName(Record, Idx);
1955
Douglas Gregor2cf26342009-04-09 22:27:44 +00001956 unsigned Size = Record[Idx++];
1957 llvm::SmallVector<unsigned, 4> & LoadedDecls
1958 = Decls.back().Declarations;
1959 LoadedDecls.reserve(Size);
1960 for (unsigned I = 0; I < Size; ++I)
1961 LoadedDecls.push_back(Record[Idx++]);
1962 }
1963
1964 return false;
1965}
1966
Douglas Gregorfdd01722009-04-14 00:24:19 +00001967void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1968 if (!Consumer)
1969 return;
1970
1971 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1972 Decl *D = GetDecl(ExternalDefinitions[I]);
1973 DeclGroupRef DG(D);
1974 Consumer->HandleTopLevelDecl(DG);
1975 }
1976}
1977
Douglas Gregor2cf26342009-04-09 22:27:44 +00001978void PCHReader::PrintStats() {
1979 std::fprintf(stderr, "*** PCH Statistics:\n");
1980
1981 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1982 TypeAlreadyLoaded.end(),
1983 true);
1984 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1985 DeclAlreadyLoaded.end(),
1986 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001987 unsigned NumIdentifiersLoaded = 0;
1988 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1989 if ((IdentifierData[I] & 0x01) == 0)
1990 ++NumIdentifiersLoaded;
1991 }
1992
Douglas Gregor2cf26342009-04-09 22:27:44 +00001993 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1994 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001995 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001996 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1997 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001998 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1999 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2000 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2001 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002002 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2003 NumStatementsRead, TotalNumStatements,
2004 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002005 std::fprintf(stderr, "\n");
2006}
2007
Chris Lattner7356a312009-04-11 21:15:38 +00002008IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002009 if (ID == 0)
2010 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002011
Douglas Gregorafaf3082009-04-11 00:14:32 +00002012 if (!IdentifierTable || IdentifierData.empty()) {
2013 Error("No identifier table in PCH file");
2014 return 0;
2015 }
Chris Lattner7356a312009-04-11 21:15:38 +00002016
Douglas Gregorafaf3082009-04-11 00:14:32 +00002017 if (IdentifierData[ID - 1] & 0x01) {
2018 uint64_t Offset = IdentifierData[ID - 1];
2019 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00002020 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002021 }
Chris Lattner7356a312009-04-11 21:15:38 +00002022
2023 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024}
2025
2026DeclarationName
2027PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2028 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2029 switch (Kind) {
2030 case DeclarationName::Identifier:
2031 return DeclarationName(GetIdentifierInfo(Record, Idx));
2032
2033 case DeclarationName::ObjCZeroArgSelector:
2034 case DeclarationName::ObjCOneArgSelector:
2035 case DeclarationName::ObjCMultiArgSelector:
2036 assert(false && "Unable to de-serialize Objective-C selectors");
2037 break;
2038
2039 case DeclarationName::CXXConstructorName:
2040 return Context.DeclarationNames.getCXXConstructorName(
2041 GetType(Record[Idx++]));
2042
2043 case DeclarationName::CXXDestructorName:
2044 return Context.DeclarationNames.getCXXDestructorName(
2045 GetType(Record[Idx++]));
2046
2047 case DeclarationName::CXXConversionFunctionName:
2048 return Context.DeclarationNames.getCXXConversionFunctionName(
2049 GetType(Record[Idx++]));
2050
2051 case DeclarationName::CXXOperatorName:
2052 return Context.DeclarationNames.getCXXOperatorName(
2053 (OverloadedOperatorKind)Record[Idx++]);
2054
2055 case DeclarationName::CXXUsingDirective:
2056 return DeclarationName::getUsingDirectiveName();
2057 }
2058
2059 // Required to silence GCC warning
2060 return DeclarationName();
2061}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002062
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002063/// \brief Read an integral value
2064llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2065 unsigned BitWidth = Record[Idx++];
2066 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2067 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2068 Idx += NumWords;
2069 return Result;
2070}
2071
2072/// \brief Read a signed integral value
2073llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2074 bool isUnsigned = Record[Idx++];
2075 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2076}
2077
Douglas Gregor17fc2232009-04-14 21:55:33 +00002078/// \brief Read a floating-point value
2079llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002080 return llvm::APFloat(ReadAPInt(Record, Idx));
2081}
2082
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002083// \brief Read a string
2084std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2085 unsigned Len = Record[Idx++];
2086 std::string Result(&Record[Idx], &Record[Idx] + Len);
2087 Idx += Len;
2088 return Result;
2089}
2090
2091/// \brief Reads attributes from the current stream position.
2092Attr *PCHReader::ReadAttributes() {
2093 unsigned Code = Stream.ReadCode();
2094 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2095 "Expected unabbreviated record"); (void)Code;
2096
2097 RecordData Record;
2098 unsigned Idx = 0;
2099 unsigned RecCode = Stream.ReadRecord(Code, Record);
2100 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2101 (void)RecCode;
2102
2103#define SIMPLE_ATTR(Name) \
2104 case Attr::Name: \
2105 New = ::new (Context) Name##Attr(); \
2106 break
2107
2108#define STRING_ATTR(Name) \
2109 case Attr::Name: \
2110 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2111 break
2112
2113#define UNSIGNED_ATTR(Name) \
2114 case Attr::Name: \
2115 New = ::new (Context) Name##Attr(Record[Idx++]); \
2116 break
2117
2118 Attr *Attrs = 0;
2119 while (Idx < Record.size()) {
2120 Attr *New = 0;
2121 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2122 bool IsInherited = Record[Idx++];
2123
2124 switch (Kind) {
2125 STRING_ATTR(Alias);
2126 UNSIGNED_ATTR(Aligned);
2127 SIMPLE_ATTR(AlwaysInline);
2128 SIMPLE_ATTR(AnalyzerNoReturn);
2129 STRING_ATTR(Annotate);
2130 STRING_ATTR(AsmLabel);
2131
2132 case Attr::Blocks:
2133 New = ::new (Context) BlocksAttr(
2134 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2135 break;
2136
2137 case Attr::Cleanup:
2138 New = ::new (Context) CleanupAttr(
2139 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2140 break;
2141
2142 SIMPLE_ATTR(Const);
2143 UNSIGNED_ATTR(Constructor);
2144 SIMPLE_ATTR(DLLExport);
2145 SIMPLE_ATTR(DLLImport);
2146 SIMPLE_ATTR(Deprecated);
2147 UNSIGNED_ATTR(Destructor);
2148 SIMPLE_ATTR(FastCall);
2149
2150 case Attr::Format: {
2151 std::string Type = ReadString(Record, Idx);
2152 unsigned FormatIdx = Record[Idx++];
2153 unsigned FirstArg = Record[Idx++];
2154 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2155 break;
2156 }
2157
2158 SIMPLE_ATTR(GNUCInline);
2159
2160 case Attr::IBOutletKind:
2161 New = ::new (Context) IBOutletAttr();
2162 break;
2163
2164 SIMPLE_ATTR(NoReturn);
2165 SIMPLE_ATTR(NoThrow);
2166 SIMPLE_ATTR(Nodebug);
2167 SIMPLE_ATTR(Noinline);
2168
2169 case Attr::NonNull: {
2170 unsigned Size = Record[Idx++];
2171 llvm::SmallVector<unsigned, 16> ArgNums;
2172 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2173 Idx += Size;
2174 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2175 break;
2176 }
2177
2178 SIMPLE_ATTR(ObjCException);
2179 SIMPLE_ATTR(ObjCNSObject);
2180 SIMPLE_ATTR(Overloadable);
2181 UNSIGNED_ATTR(Packed);
2182 SIMPLE_ATTR(Pure);
2183 UNSIGNED_ATTR(Regparm);
2184 STRING_ATTR(Section);
2185 SIMPLE_ATTR(StdCall);
2186 SIMPLE_ATTR(TransparentUnion);
2187 SIMPLE_ATTR(Unavailable);
2188 SIMPLE_ATTR(Unused);
2189 SIMPLE_ATTR(Used);
2190
2191 case Attr::Visibility:
2192 New = ::new (Context) VisibilityAttr(
2193 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2194 break;
2195
2196 SIMPLE_ATTR(WarnUnusedResult);
2197 SIMPLE_ATTR(Weak);
2198 SIMPLE_ATTR(WeakImport);
2199 }
2200
2201 assert(New && "Unable to decode attribute?");
2202 New->setInherited(IsInherited);
2203 New->setNext(Attrs);
2204 Attrs = New;
2205 }
2206#undef UNSIGNED_ATTR
2207#undef STRING_ATTR
2208#undef SIMPLE_ATTR
2209
2210 // The list of attributes was built backwards. Reverse the list
2211 // before returning it.
2212 Attr *PrevAttr = 0, *NextAttr = 0;
2213 while (Attrs) {
2214 NextAttr = Attrs->getNext();
2215 Attrs->setNext(PrevAttr);
2216 PrevAttr = Attrs;
2217 Attrs = NextAttr;
2218 }
2219
2220 return PrevAttr;
2221}
2222
Douglas Gregorc9490c02009-04-16 22:23:12 +00002223Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002224 // Within the bitstream, expressions are stored in Reverse Polish
2225 // Notation, with each of the subexpressions preceding the
2226 // expression they are stored in. To evaluate expressions, we
2227 // continue reading expressions and placing them on the stack, with
2228 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002229 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002230 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002231 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002232 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002233 llvm::SmallVector<Stmt *, 16> StmtStack;
2234 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002235 Stmt::EmptyShell Empty;
2236
Douglas Gregor087fd532009-04-14 23:32:43 +00002237 while (true) {
2238 unsigned Code = Stream.ReadCode();
2239 if (Code == llvm::bitc::END_BLOCK) {
2240 if (Stream.ReadBlockEnd()) {
2241 Error("Error at end of Source Manager block");
2242 return 0;
2243 }
2244 break;
2245 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002246
Douglas Gregor087fd532009-04-14 23:32:43 +00002247 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2248 // No known subblocks, always skip them.
2249 Stream.ReadSubBlockID();
2250 if (Stream.SkipBlock()) {
2251 Error("Malformed block record");
2252 return 0;
2253 }
2254 continue;
2255 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002256
Douglas Gregor087fd532009-04-14 23:32:43 +00002257 if (Code == llvm::bitc::DEFINE_ABBREV) {
2258 Stream.ReadAbbrevRecord();
2259 continue;
2260 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002261
Douglas Gregorc9490c02009-04-16 22:23:12 +00002262 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002263 Idx = 0;
2264 Record.clear();
2265 bool Finished = false;
2266 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002267 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002268 Finished = true;
2269 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002270
Douglas Gregorc9490c02009-04-16 22:23:12 +00002271 case pch::STMT_NULL_PTR:
2272 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002273 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002274
Douglas Gregor025452f2009-04-17 00:04:06 +00002275 case pch::STMT_NULL:
2276 S = new (Context) NullStmt(Empty);
2277 break;
2278
2279 case pch::STMT_COMPOUND:
2280 S = new (Context) CompoundStmt(Empty);
2281 break;
2282
2283 case pch::STMT_CASE:
2284 S = new (Context) CaseStmt(Empty);
2285 break;
2286
2287 case pch::STMT_DEFAULT:
2288 S = new (Context) DefaultStmt(Empty);
2289 break;
2290
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002291 case pch::STMT_LABEL:
2292 S = new (Context) LabelStmt(Empty);
2293 break;
2294
Douglas Gregor025452f2009-04-17 00:04:06 +00002295 case pch::STMT_IF:
2296 S = new (Context) IfStmt(Empty);
2297 break;
2298
2299 case pch::STMT_SWITCH:
2300 S = new (Context) SwitchStmt(Empty);
2301 break;
2302
Douglas Gregord921cf92009-04-17 00:16:09 +00002303 case pch::STMT_WHILE:
2304 S = new (Context) WhileStmt(Empty);
2305 break;
2306
Douglas Gregor67d82492009-04-17 00:29:51 +00002307 case pch::STMT_DO:
2308 S = new (Context) DoStmt(Empty);
2309 break;
2310
2311 case pch::STMT_FOR:
2312 S = new (Context) ForStmt(Empty);
2313 break;
2314
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002315 case pch::STMT_GOTO:
2316 S = new (Context) GotoStmt(Empty);
2317 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002318
2319 case pch::STMT_INDIRECT_GOTO:
2320 S = new (Context) IndirectGotoStmt(Empty);
2321 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002322
Douglas Gregord921cf92009-04-17 00:16:09 +00002323 case pch::STMT_CONTINUE:
2324 S = new (Context) ContinueStmt(Empty);
2325 break;
2326
Douglas Gregor025452f2009-04-17 00:04:06 +00002327 case pch::STMT_BREAK:
2328 S = new (Context) BreakStmt(Empty);
2329 break;
2330
Douglas Gregor0de9d882009-04-17 16:34:57 +00002331 case pch::STMT_RETURN:
2332 S = new (Context) ReturnStmt(Empty);
2333 break;
2334
Douglas Gregor84f21702009-04-17 16:55:36 +00002335 case pch::STMT_DECL:
2336 S = new (Context) DeclStmt(Empty);
2337 break;
2338
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00002339 case pch::STMT_ASM:
2340 S = new (Context) AsmStmt(Empty);
2341 break;
2342
Douglas Gregor087fd532009-04-14 23:32:43 +00002343 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002344 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002345 break;
2346
2347 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002348 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002349 break;
2350
2351 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002352 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002353 break;
2354
2355 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002356 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002357 break;
2358
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002359 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002360 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002361 break;
2362
Douglas Gregor673ecd62009-04-15 16:35:07 +00002363 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002364 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002365 Record[PCHStmtReader::NumExprFields + 1]);
2366 break;
2367
Douglas Gregor087fd532009-04-14 23:32:43 +00002368 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002369 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002370 break;
2371
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002372 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002373 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002374 break;
2375
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002376 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002377 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002378 break;
2379
2380 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002381 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002382 break;
2383
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002384 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002385 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002386 break;
2387
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002388 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002389 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002390 break;
2391
2392 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002393 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002394 break;
2395
Douglas Gregordb600c32009-04-15 00:25:59 +00002396 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002397 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002398 break;
2399
Douglas Gregorad90e962009-04-15 22:40:36 +00002400 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002401 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002402 break;
2403
2404 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002405 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002406 break;
2407
Douglas Gregor087fd532009-04-14 23:32:43 +00002408 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002409 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002410 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002411
2412 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002413 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002414 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002415
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002416 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002417 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002418 break;
2419
Douglas Gregord3c98a02009-04-15 23:02:49 +00002420 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002421 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002422 break;
2423
Douglas Gregord077d752009-04-16 00:55:48 +00002424 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002425 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002426 break;
2427
2428 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002429 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002430 Record[PCHStmtReader::NumExprFields] - 1);
2431
2432 break;
2433
2434 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002435 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002436 break;
2437
Douglas Gregord3c98a02009-04-15 23:02:49 +00002438 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002439 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002440 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002441
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002442 case pch::EXPR_ADDR_LABEL:
2443 S = new (Context) AddrLabelExpr(Empty);
2444 break;
2445
Douglas Gregor6a2dd552009-04-17 19:05:30 +00002446 case pch::EXPR_STMT:
2447 S = new (Context) StmtExpr(Empty);
2448 break;
2449
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002450 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002451 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002452 break;
2453
2454 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002455 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002456 break;
2457
2458 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002459 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002460 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002461
2462 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002463 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002464 break;
2465
Douglas Gregor84af7c22009-04-17 19:21:43 +00002466 case pch::EXPR_BLOCK:
2467 S = new (Context) BlockExpr(Empty);
2468 break;
2469
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002470 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002471 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002472 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002473 }
2474
Douglas Gregorc9490c02009-04-16 22:23:12 +00002475 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002476 if (Finished)
2477 break;
2478
Douglas Gregor3e1af842009-04-17 22:13:46 +00002479 ++NumStatementsRead;
2480
Douglas Gregorc9490c02009-04-16 22:23:12 +00002481 if (S) {
2482 unsigned NumSubStmts = Reader.Visit(S);
2483 while (NumSubStmts > 0) {
2484 StmtStack.pop_back();
2485 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002486 }
2487 }
2488
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002489 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002490 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002491 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002492 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00002493 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002494 return StmtStack.back();
2495}
2496
2497Expr *PCHReader::ReadExpr() {
2498 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002499}
2500
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002501DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002502 return Diag(SourceLocation(), DiagID);
2503}
2504
2505DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2506 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002507 Context.getSourceManager()),
2508 DiagID);
2509}
Douglas Gregor025452f2009-04-17 00:04:06 +00002510
2511/// \brief Record that the given ID maps to the given switch-case
2512/// statement.
2513void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2514 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2515 SwitchCaseStmts[ID] = SC;
2516}
2517
2518/// \brief Retrieve the switch-case statement with the given ID.
2519SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2520 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2521 return SwitchCaseStmts[ID];
2522}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002523
2524/// \brief Record that the given label statement has been
2525/// deserialized and has the given ID.
2526void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2527 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2528 "Deserialized label twice");
2529 LabelStmts[ID] = S;
2530
2531 // If we've already seen any goto statements that point to this
2532 // label, resolve them now.
2533 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2534 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2535 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2536 Goto->second->setLabel(S);
2537 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002538
2539 // If we've already seen any address-label statements that point to
2540 // this label, resolve them now.
2541 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2542 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2543 = UnresolvedAddrLabelExprs.equal_range(ID);
2544 for (AddrLabelIter AddrLabel = AddrLabels.first;
2545 AddrLabel != AddrLabels.second; ++AddrLabel)
2546 AddrLabel->second->setLabel(S);
2547 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002548}
2549
2550/// \brief Set the label of the given statement to the label
2551/// identified by ID.
2552///
2553/// Depending on the order in which the label and other statements
2554/// referencing that label occur, this operation may complete
2555/// immediately (updating the statement) or it may queue the
2556/// statement to be back-patched later.
2557void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2558 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2559 if (Label != LabelStmts.end()) {
2560 // We've already seen this label, so set the label of the goto and
2561 // we're done.
2562 S->setLabel(Label->second);
2563 } else {
2564 // We haven't seen this label yet, so add this goto to the set of
2565 // unresolved goto statements.
2566 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2567 }
2568}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002569
2570/// \brief Set the label of the given expression to the label
2571/// identified by ID.
2572///
2573/// Depending on the order in which the label and other statements
2574/// referencing that label occur, this operation may complete
2575/// immediately (updating the statement) or it may queue the
2576/// statement to be back-patched later.
2577void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2578 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2579 if (Label != LabelStmts.end()) {
2580 // We've already seen this label, so set the label of the
2581 // label-address expression and we're done.
2582 S->setLabel(Label->second);
2583 } else {
2584 // We haven't seen this label yet, so add this label-address
2585 // expression to the set of unresolved label-address expressions.
2586 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2587 }
2588}