blob: 391937b9234050917ac6bede05d67f437a389877 [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);
Steve Naroff53c9d8a2009-04-20 15:06:07 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor2cf26342009-04-09 22:27:44 +000071 };
72}
73
74void PCHDeclReader::VisitDecl(Decl *D) {
75 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
76 D->setLexicalDeclContext(
77 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
78 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
79 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +000080 if (Record[Idx++])
81 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +000082 D->setImplicit(Record[Idx++]);
83 D->setAccess((AccessSpecifier)Record[Idx++]);
84}
85
86void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
87 VisitDecl(TU);
88}
89
90void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
91 VisitDecl(ND);
92 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
93}
94
95void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
96 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000097 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
98}
99
100void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +0000101 // Note that we cannot use VisitTypeDecl here, because we need to
102 // set the underlying type of the typedef *before* we try to read
103 // the type associated with the TypedefDecl.
104 VisitNamedDecl(TD);
105 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
106 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
107 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000110void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
111 VisitTypeDecl(TD);
112 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
113 TD->setDefinition(Record[Idx++]);
114 TD->setTypedefForAnonDecl(
115 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
116}
117
118void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
119 VisitTagDecl(ED);
120 ED->setIntegerType(Reader.GetType(Record[Idx++]));
121}
122
Douglas Gregor8c700062009-04-13 21:20:57 +0000123void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
124 VisitTagDecl(RD);
125 RD->setHasFlexibleArrayMember(Record[Idx++]);
126 RD->setAnonymousStructOrUnion(Record[Idx++]);
127}
128
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
130 VisitNamedDecl(VD);
131 VD->setType(Reader.GetType(Record[Idx++]));
132}
133
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000134void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
135 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000136 if (Record[Idx++])
137 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000138 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
139}
140
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000141void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
142 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000143 if (Record[Idx++])
Douglas Gregor250fc9c2009-04-18 00:07:54 +0000144 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000145 FD->setPreviousDeclaration(
146 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
147 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
148 FD->setInline(Record[Idx++]);
149 FD->setVirtual(Record[Idx++]);
150 FD->setPure(Record[Idx++]);
151 FD->setInheritedPrototype(Record[Idx++]);
152 FD->setHasPrototype(Record[Idx++]);
153 FD->setDeleted(Record[Idx++]);
154 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
155 unsigned NumParams = Record[Idx++];
156 llvm::SmallVector<ParmVarDecl *, 16> Params;
157 Params.reserve(NumParams);
158 for (unsigned I = 0; I != NumParams; ++I)
159 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
160 FD->setParams(Reader.getContext(), &Params[0], NumParams);
161}
162
Steve Naroff53c9d8a2009-04-20 15:06:07 +0000163void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
164 VisitNamedDecl(MD);
165 if (Record[Idx++]) {
166 // In practice, this won't be executed (since method definitions
167 // don't occur in header files).
168 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
169 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
170 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
171 }
172 MD->setInstanceMethod(Record[Idx++]);
173 MD->setVariadic(Record[Idx++]);
174 MD->setSynthesized(Record[Idx++]);
175 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
176 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
177 MD->setResultType(Reader.GetType(Record[Idx++]));
178 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
179 unsigned NumParams = Record[Idx++];
180 llvm::SmallVector<ParmVarDecl *, 16> Params;
181 Params.reserve(NumParams);
182 for (unsigned I = 0; I != NumParams; ++I)
183 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
184 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
185}
186
Douglas Gregor8c700062009-04-13 21:20:57 +0000187void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
188 VisitValueDecl(FD);
189 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000190 if (Record[Idx++])
191 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000192}
193
Douglas Gregor2cf26342009-04-09 22:27:44 +0000194void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
195 VisitValueDecl(VD);
196 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
197 VD->setThreadSpecified(Record[Idx++]);
198 VD->setCXXDirectInitializer(Record[Idx++]);
199 VD->setDeclaredInCondition(Record[Idx++]);
200 VD->setPreviousDeclaration(
201 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
202 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000203 if (Record[Idx++])
204 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205}
206
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000207void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
208 VisitVarDecl(PD);
209 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000210 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000211}
212
213void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
214 VisitParmVarDecl(PD);
215 PD->setOriginalType(Reader.GetType(Record[Idx++]));
216}
217
Douglas Gregor1028bc62009-04-13 22:49:25 +0000218void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
219 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000220 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000221}
222
223void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
224 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000225 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000226 unsigned NumParams = Record[Idx++];
227 llvm::SmallVector<ParmVarDecl *, 16> Params;
228 Params.reserve(NumParams);
229 for (unsigned I = 0; I != NumParams; ++I)
230 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
231 BD->setParams(Reader.getContext(), &Params[0], NumParams);
232}
233
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234std::pair<uint64_t, uint64_t>
235PCHDeclReader::VisitDeclContext(DeclContext *DC) {
236 uint64_t LexicalOffset = Record[Idx++];
237 uint64_t VisibleOffset = 0;
238 if (DC->getPrimaryContext() == DC)
239 VisibleOffset = Record[Idx++];
240 return std::make_pair(LexicalOffset, VisibleOffset);
241}
242
Douglas Gregor0b748912009-04-14 21:18:50 +0000243//===----------------------------------------------------------------------===//
244// Statement/expression deserialization
245//===----------------------------------------------------------------------===//
246namespace {
247 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000248 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000249 PCHReader &Reader;
250 const PCHReader::RecordData &Record;
251 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000252 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000253
254 public:
255 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000256 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
257 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000258
Douglas Gregor025452f2009-04-17 00:04:06 +0000259 /// \brief The number of record fields required for the Stmt class
260 /// itself.
261 static const unsigned NumStmtFields = 0;
262
Douglas Gregor673ecd62009-04-15 16:35:07 +0000263 /// \brief The number of record fields required for the Expr class
264 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000265 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000266
Douglas Gregor087fd532009-04-14 23:32:43 +0000267 // Each of the Visit* functions reads in part of the expression
268 // from the given record and the current expression stack, then
269 // return the total number of operands that it read from the
270 // expression stack.
271
Douglas Gregor025452f2009-04-17 00:04:06 +0000272 unsigned VisitStmt(Stmt *S);
273 unsigned VisitNullStmt(NullStmt *S);
274 unsigned VisitCompoundStmt(CompoundStmt *S);
275 unsigned VisitSwitchCase(SwitchCase *S);
276 unsigned VisitCaseStmt(CaseStmt *S);
277 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000278 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000279 unsigned VisitIfStmt(IfStmt *S);
280 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000281 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000282 unsigned VisitDoStmt(DoStmt *S);
283 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000284 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000285 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000286 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000287 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000288 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000289 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000290 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000291 unsigned VisitExpr(Expr *E);
292 unsigned VisitPredefinedExpr(PredefinedExpr *E);
293 unsigned VisitDeclRefExpr(DeclRefExpr *E);
294 unsigned VisitIntegerLiteral(IntegerLiteral *E);
295 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000296 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000297 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000298 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000299 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000300 unsigned VisitUnaryOperator(UnaryOperator *E);
301 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000302 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000303 unsigned VisitCallExpr(CallExpr *E);
304 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000305 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000306 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000307 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
308 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000309 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000310 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
311 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000312 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000313 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000314 unsigned VisitInitListExpr(InitListExpr *E);
315 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
316 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000317 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000318 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000319 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000320 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
321 unsigned VisitChooseExpr(ChooseExpr *E);
322 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000323 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000324 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000325 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000326 };
327}
328
Douglas Gregor025452f2009-04-17 00:04:06 +0000329unsigned PCHStmtReader::VisitStmt(Stmt *S) {
330 assert(Idx == NumStmtFields && "Incorrect statement field count");
331 return 0;
332}
333
334unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
335 VisitStmt(S);
336 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 return 0;
338}
339
340unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
341 VisitStmt(S);
342 unsigned NumStmts = Record[Idx++];
343 S->setStmts(Reader.getContext(),
344 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
345 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
346 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
347 return NumStmts;
348}
349
350unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
351 VisitStmt(S);
352 Reader.RecordSwitchCaseID(S, Record[Idx++]);
353 return 0;
354}
355
356unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
357 VisitSwitchCase(S);
358 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
359 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
360 S->setSubStmt(StmtStack.back());
361 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
362 return 3;
363}
364
365unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
366 VisitSwitchCase(S);
367 S->setSubStmt(StmtStack.back());
368 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369 return 1;
370}
371
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000372unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
373 VisitStmt(S);
374 S->setID(Reader.GetIdentifierInfo(Record, Idx));
375 S->setSubStmt(StmtStack.back());
376 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
377 Reader.RecordLabelStmt(S, Record[Idx++]);
378 return 1;
379}
380
Douglas Gregor025452f2009-04-17 00:04:06 +0000381unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
382 VisitStmt(S);
383 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
384 S->setThen(StmtStack[StmtStack.size() - 2]);
385 S->setElse(StmtStack[StmtStack.size() - 1]);
386 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
387 return 3;
388}
389
390unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
391 VisitStmt(S);
392 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
393 S->setBody(StmtStack.back());
394 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
395 SwitchCase *PrevSC = 0;
396 for (unsigned N = Record.size(); Idx != N; ++Idx) {
397 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
398 if (PrevSC)
399 PrevSC->setNextSwitchCase(SC);
400 else
401 S->setSwitchCaseList(SC);
402 PrevSC = SC;
403 }
404 return 2;
405}
406
Douglas Gregord921cf92009-04-17 00:16:09 +0000407unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
408 VisitStmt(S);
409 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
410 S->setBody(StmtStack.back());
411 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
412 return 2;
413}
414
Douglas Gregor67d82492009-04-17 00:29:51 +0000415unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
416 VisitStmt(S);
417 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
418 S->setBody(StmtStack.back());
419 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
420 return 2;
421}
422
423unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
424 VisitStmt(S);
425 S->setInit(StmtStack[StmtStack.size() - 4]);
426 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
427 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
428 S->setBody(StmtStack.back());
429 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
430 return 4;
431}
432
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000433unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
434 VisitStmt(S);
435 Reader.SetLabelOf(S, Record[Idx++]);
436 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
437 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
438 return 0;
439}
440
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000441unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
442 VisitStmt(S);
Chris Lattnerad56d682009-04-19 01:04:21 +0000443 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000444 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
445 return 1;
446}
447
Douglas Gregord921cf92009-04-17 00:16:09 +0000448unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
449 VisitStmt(S);
450 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
451 return 0;
452}
453
Douglas Gregor025452f2009-04-17 00:04:06 +0000454unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
455 VisitStmt(S);
456 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
457 return 0;
458}
459
Douglas Gregor0de9d882009-04-17 16:34:57 +0000460unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
461 VisitStmt(S);
462 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
463 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
464 return 1;
465}
466
Douglas Gregor84f21702009-04-17 16:55:36 +0000467unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
468 VisitStmt(S);
469 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
470 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
471
472 if (Idx + 1 == Record.size()) {
473 // Single declaration
474 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
475 } else {
476 llvm::SmallVector<Decl *, 16> Decls;
477 Decls.reserve(Record.size() - Idx);
478 for (unsigned N = Record.size(); Idx != N; ++Idx)
479 Decls.push_back(Reader.GetDecl(Record[Idx]));
480 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
481 &Decls[0], Decls.size())));
482 }
483 return 0;
484}
485
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000486unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
487 VisitStmt(S);
488 unsigned NumOutputs = Record[Idx++];
489 unsigned NumInputs = Record[Idx++];
490 unsigned NumClobbers = Record[Idx++];
491 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
492 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
493 S->setVolatile(Record[Idx++]);
494 S->setSimple(Record[Idx++]);
495
496 unsigned StackIdx
497 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
498 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
499
500 // Outputs and inputs
501 llvm::SmallVector<std::string, 16> Names;
502 llvm::SmallVector<StringLiteral*, 16> Constraints;
503 llvm::SmallVector<Stmt*, 16> Exprs;
504 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
505 Names.push_back(Reader.ReadString(Record, Idx));
506 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
507 Exprs.push_back(StmtStack[StackIdx++]);
508 }
509 S->setOutputsAndInputs(NumOutputs, NumInputs,
510 &Names[0], &Constraints[0], &Exprs[0]);
511
512 // Constraints
513 llvm::SmallVector<StringLiteral*, 16> Clobbers;
514 for (unsigned I = 0; I != NumClobbers; ++I)
515 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
516 S->setClobbers(&Clobbers[0], NumClobbers);
517
518 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
519 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
520}
521
Douglas Gregor087fd532009-04-14 23:32:43 +0000522unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000523 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000524 E->setType(Reader.GetType(Record[Idx++]));
525 E->setTypeDependent(Record[Idx++]);
526 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000527 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000528 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000529}
530
Douglas Gregor087fd532009-04-14 23:32:43 +0000531unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000532 VisitExpr(E);
533 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
534 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000535 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000536}
537
Douglas Gregor087fd532009-04-14 23:32:43 +0000538unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000539 VisitExpr(E);
540 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
541 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000542 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000543}
544
Douglas Gregor087fd532009-04-14 23:32:43 +0000545unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000546 VisitExpr(E);
547 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
548 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000549 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000550}
551
Douglas Gregor087fd532009-04-14 23:32:43 +0000552unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000553 VisitExpr(E);
554 E->setValue(Reader.ReadAPFloat(Record, Idx));
555 E->setExact(Record[Idx++]);
556 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000557 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000558}
559
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000560unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
561 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000562 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000563 return 1;
564}
565
Douglas Gregor673ecd62009-04-15 16:35:07 +0000566unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
567 VisitExpr(E);
568 unsigned Len = Record[Idx++];
569 assert(Record[Idx] == E->getNumConcatenated() &&
570 "Wrong number of concatenated tokens!");
571 ++Idx;
572 E->setWide(Record[Idx++]);
573
574 // Read string data
575 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
576 E->setStrData(Reader.getContext(), &Str[0], Len);
577 Idx += Len;
578
579 // Read source locations
580 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
581 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
582
583 return 0;
584}
585
Douglas Gregor087fd532009-04-14 23:32:43 +0000586unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000587 VisitExpr(E);
588 E->setValue(Record[Idx++]);
589 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
590 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000591 return 0;
592}
593
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000594unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
595 VisitExpr(E);
596 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
597 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000598 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000599 return 1;
600}
601
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000602unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
603 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000604 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000605 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
606 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
607 return 1;
608}
609
610unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
611 VisitExpr(E);
612 E->setSizeof(Record[Idx++]);
613 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000614 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000615 ++Idx;
616 } else {
617 E->setArgument(Reader.GetType(Record[Idx++]));
618 }
619 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
620 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
621 return E->isArgumentType()? 0 : 1;
622}
623
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000624unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
625 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000626 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
627 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000628 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
629 return 2;
630}
631
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000632unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
633 VisitExpr(E);
634 E->setNumArgs(Reader.getContext(), Record[Idx++]);
635 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000636 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000637 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000638 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000639 return E->getNumArgs() + 1;
640}
641
642unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
643 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000644 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000645 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
646 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
647 E->setArrow(Record[Idx++]);
648 return 1;
649}
650
Douglas Gregor087fd532009-04-14 23:32:43 +0000651unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
652 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000653 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000654 return 1;
655}
656
Douglas Gregordb600c32009-04-15 00:25:59 +0000657unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
658 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000659 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
660 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000661 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
662 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
663 return 2;
664}
665
Douglas Gregorad90e962009-04-15 22:40:36 +0000666unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
667 VisitBinaryOperator(E);
668 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
669 E->setComputationResultType(Reader.GetType(Record[Idx++]));
670 return 2;
671}
672
673unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
674 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000675 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
676 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
677 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000678 return 3;
679}
680
Douglas Gregor087fd532009-04-14 23:32:43 +0000681unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
682 VisitCastExpr(E);
683 E->setLvalueCast(Record[Idx++]);
684 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000685}
686
Douglas Gregordb600c32009-04-15 00:25:59 +0000687unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
688 VisitCastExpr(E);
689 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
690 return 1;
691}
692
693unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
694 VisitExplicitCastExpr(E);
695 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
696 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
697 return 1;
698}
699
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000700unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
701 VisitExpr(E);
702 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000703 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000704 E->setFileScope(Record[Idx++]);
705 return 1;
706}
707
Douglas Gregord3c98a02009-04-15 23:02:49 +0000708unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
709 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000710 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000711 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
712 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
713 return 1;
714}
715
Douglas Gregord077d752009-04-16 00:55:48 +0000716unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
717 VisitExpr(E);
718 unsigned NumInits = Record[Idx++];
719 E->reserveInits(NumInits);
720 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000721 E->updateInit(I,
722 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
723 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000724 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
725 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
726 E->setInitializedFieldInUnion(
727 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
728 E->sawArrayRangeDesignator(Record[Idx++]);
729 return NumInits + 1;
730}
731
732unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
733 typedef DesignatedInitExpr::Designator Designator;
734
735 VisitExpr(E);
736 unsigned NumSubExprs = Record[Idx++];
737 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
738 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000739 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000740 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
741 E->setGNUSyntax(Record[Idx++]);
742
743 llvm::SmallVector<Designator, 4> Designators;
744 while (Idx < Record.size()) {
745 switch ((pch::DesignatorTypes)Record[Idx++]) {
746 case pch::DESIG_FIELD_DECL: {
747 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
748 SourceLocation DotLoc
749 = SourceLocation::getFromRawEncoding(Record[Idx++]);
750 SourceLocation FieldLoc
751 = SourceLocation::getFromRawEncoding(Record[Idx++]);
752 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
753 FieldLoc));
754 Designators.back().setField(Field);
755 break;
756 }
757
758 case pch::DESIG_FIELD_NAME: {
759 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
760 SourceLocation DotLoc
761 = SourceLocation::getFromRawEncoding(Record[Idx++]);
762 SourceLocation FieldLoc
763 = SourceLocation::getFromRawEncoding(Record[Idx++]);
764 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
765 break;
766 }
767
768 case pch::DESIG_ARRAY: {
769 unsigned Index = Record[Idx++];
770 SourceLocation LBracketLoc
771 = SourceLocation::getFromRawEncoding(Record[Idx++]);
772 SourceLocation RBracketLoc
773 = SourceLocation::getFromRawEncoding(Record[Idx++]);
774 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
775 break;
776 }
777
778 case pch::DESIG_ARRAY_RANGE: {
779 unsigned Index = Record[Idx++];
780 SourceLocation LBracketLoc
781 = SourceLocation::getFromRawEncoding(Record[Idx++]);
782 SourceLocation EllipsisLoc
783 = SourceLocation::getFromRawEncoding(Record[Idx++]);
784 SourceLocation RBracketLoc
785 = SourceLocation::getFromRawEncoding(Record[Idx++]);
786 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
787 RBracketLoc));
788 break;
789 }
790 }
791 }
792 E->setDesignators(&Designators[0], Designators.size());
793
794 return NumSubExprs;
795}
796
797unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
798 VisitExpr(E);
799 return 0;
800}
801
Douglas Gregord3c98a02009-04-15 23:02:49 +0000802unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
803 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000804 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000805 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
807 return 1;
808}
809
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000810unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
811 VisitExpr(E);
812 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
813 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
814 Reader.SetLabelOf(E, Record[Idx++]);
815 return 0;
816}
817
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000818unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
819 VisitExpr(E);
820 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
821 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
822 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
823 return 1;
824}
825
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000826unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
827 VisitExpr(E);
828 E->setArgType1(Reader.GetType(Record[Idx++]));
829 E->setArgType2(Reader.GetType(Record[Idx++]));
830 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
831 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
832 return 0;
833}
834
835unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
836 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000837 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
838 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
839 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000840 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
841 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
842 return 3;
843}
844
845unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
846 VisitExpr(E);
847 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
848 return 0;
849}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000850
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000851unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
852 VisitExpr(E);
853 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000854 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000855 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
856 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
857 return NumExprs;
858}
859
Douglas Gregor84af7c22009-04-17 19:21:43 +0000860unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
861 VisitExpr(E);
862 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
863 E->setHasBlockDeclRefExprs(Record[Idx++]);
864 return 0;
865}
866
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000867unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
868 VisitExpr(E);
869 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
870 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
871 E->setByRef(Record[Idx++]);
872 return 0;
873}
874
Douglas Gregor2cf26342009-04-09 22:27:44 +0000875// FIXME: use the diagnostics machinery
876static bool Error(const char *Str) {
877 std::fprintf(stderr, "%s\n", Str);
878 return true;
879}
880
Douglas Gregore1d918e2009-04-10 23:10:45 +0000881/// \brief Check the contents of the predefines buffer against the
882/// contents of the predefines buffer used to build the PCH file.
883///
884/// The contents of the two predefines buffers should be the same. If
885/// not, then some command-line option changed the preprocessor state
886/// and we must reject the PCH file.
887///
888/// \param PCHPredef The start of the predefines buffer in the PCH
889/// file.
890///
891/// \param PCHPredefLen The length of the predefines buffer in the PCH
892/// file.
893///
894/// \param PCHBufferID The FileID for the PCH predefines buffer.
895///
896/// \returns true if there was a mismatch (in which case the PCH file
897/// should be ignored), or false otherwise.
898bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
899 unsigned PCHPredefLen,
900 FileID PCHBufferID) {
901 const char *Predef = PP.getPredefines().c_str();
902 unsigned PredefLen = PP.getPredefines().size();
903
904 // If the two predefines buffers compare equal, we're done!.
905 if (PredefLen == PCHPredefLen &&
906 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
907 return false;
908
909 // The predefines buffers are different. Produce a reasonable
910 // diagnostic showing where they are different.
911
912 // The source locations (potentially in the two different predefines
913 // buffers)
914 SourceLocation Loc1, Loc2;
915 SourceManager &SourceMgr = PP.getSourceManager();
916
917 // Create a source buffer for our predefines string, so
918 // that we can build a diagnostic that points into that
919 // source buffer.
920 FileID BufferID;
921 if (Predef && Predef[0]) {
922 llvm::MemoryBuffer *Buffer
923 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
924 "<built-in>");
925 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
926 }
927
928 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
929 std::pair<const char *, const char *> Locations
930 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
931
932 if (Locations.first != Predef + MinLen) {
933 // We found the location in the two buffers where there is a
934 // difference. Form source locations to point there (in both
935 // buffers).
936 unsigned Offset = Locations.first - Predef;
937 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
938 .getFileLocWithOffset(Offset);
939 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
940 .getFileLocWithOffset(Offset);
941 } else if (PredefLen > PCHPredefLen) {
942 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
943 .getFileLocWithOffset(MinLen);
944 } else {
945 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
946 .getFileLocWithOffset(MinLen);
947 }
948
949 Diag(Loc1, diag::warn_pch_preprocessor);
950 if (Loc2.isValid())
951 Diag(Loc2, diag::note_predef_in_pch);
952 Diag(diag::note_ignoring_pch) << FileName;
953 return true;
954}
955
Douglas Gregorbd945002009-04-13 16:31:14 +0000956/// \brief Read the line table in the source manager block.
957/// \returns true if ther was an error.
958static bool ParseLineTable(SourceManager &SourceMgr,
959 llvm::SmallVectorImpl<uint64_t> &Record) {
960 unsigned Idx = 0;
961 LineTableInfo &LineTable = SourceMgr.getLineTable();
962
963 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000964 std::map<int, int> FileIDs;
965 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000966 // Extract the file name
967 unsigned FilenameLen = Record[Idx++];
968 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
969 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000970 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
971 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000972 }
973
974 // Parse the line entries
975 std::vector<LineEntry> Entries;
976 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000977 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000978
979 // Extract the line entries
980 unsigned NumEntries = Record[Idx++];
981 Entries.clear();
982 Entries.reserve(NumEntries);
983 for (unsigned I = 0; I != NumEntries; ++I) {
984 unsigned FileOffset = Record[Idx++];
985 unsigned LineNo = Record[Idx++];
986 int FilenameID = Record[Idx++];
987 SrcMgr::CharacteristicKind FileKind
988 = (SrcMgr::CharacteristicKind)Record[Idx++];
989 unsigned IncludeOffset = Record[Idx++];
990 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
991 FileKind, IncludeOffset));
992 }
993 LineTable.AddEntry(FID, Entries);
994 }
995
996 return false;
997}
998
Douglas Gregor14f79002009-04-10 03:52:48 +0000999/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +00001000PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +00001001 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001002 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1003 Error("Malformed source manager block record");
1004 return Failure;
1005 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001006
1007 SourceManager &SourceMgr = Context.getSourceManager();
1008 RecordData Record;
1009 while (true) {
1010 unsigned Code = Stream.ReadCode();
1011 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001012 if (Stream.ReadBlockEnd()) {
1013 Error("Error at end of Source Manager block");
1014 return Failure;
1015 }
1016
1017 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001018 }
1019
1020 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1021 // No known subblocks, always skip them.
1022 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +00001023 if (Stream.SkipBlock()) {
1024 Error("Malformed block record");
1025 return Failure;
1026 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001027 continue;
1028 }
1029
1030 if (Code == llvm::bitc::DEFINE_ABBREV) {
1031 Stream.ReadAbbrevRecord();
1032 continue;
1033 }
1034
1035 // Read a record.
1036 const char *BlobStart;
1037 unsigned BlobLen;
1038 Record.clear();
1039 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1040 default: // Default behavior: ignore.
1041 break;
1042
1043 case pch::SM_SLOC_FILE_ENTRY: {
1044 // FIXME: We would really like to delay the creation of this
1045 // FileEntry until it is actually required, e.g., when producing
1046 // a diagnostic with a source location in this file.
1047 const FileEntry *File
1048 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1049 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001050 FileID ID = SourceMgr.createFileID(File,
1051 SourceLocation::getFromRawEncoding(Record[1]),
1052 (CharacteristicKind)Record[2]);
1053 if (Record[3])
1054 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1055 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001056 break;
1057 }
1058
1059 case pch::SM_SLOC_BUFFER_ENTRY: {
1060 const char *Name = BlobStart;
1061 unsigned Code = Stream.ReadCode();
1062 Record.clear();
1063 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1064 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001065 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001066 llvm::MemoryBuffer *Buffer
1067 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1068 BlobStart + BlobLen - 1,
1069 Name);
1070 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1071
1072 if (strcmp(Name, "<built-in>") == 0
1073 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1074 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001075 break;
1076 }
1077
1078 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1079 SourceLocation SpellingLoc
1080 = SourceLocation::getFromRawEncoding(Record[1]);
1081 SourceMgr.createInstantiationLoc(
1082 SpellingLoc,
1083 SourceLocation::getFromRawEncoding(Record[2]),
1084 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001085 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001086 break;
1087 }
1088
Chris Lattner2c78b872009-04-14 23:22:57 +00001089 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001090 if (ParseLineTable(SourceMgr, Record))
1091 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001092 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001093 }
1094 }
1095}
1096
Chris Lattner42d42b52009-04-10 21:41:48 +00001097bool PCHReader::ReadPreprocessorBlock() {
1098 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1099 return Error("Malformed preprocessor block record");
1100
Chris Lattner42d42b52009-04-10 21:41:48 +00001101 RecordData Record;
1102 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1103 MacroInfo *LastMacro = 0;
1104
1105 while (true) {
1106 unsigned Code = Stream.ReadCode();
1107 switch (Code) {
1108 case llvm::bitc::END_BLOCK:
1109 if (Stream.ReadBlockEnd())
1110 return Error("Error at end of preprocessor block");
1111 return false;
1112
1113 case llvm::bitc::ENTER_SUBBLOCK:
1114 // No known subblocks, always skip them.
1115 Stream.ReadSubBlockID();
1116 if (Stream.SkipBlock())
1117 return Error("Malformed block record");
1118 continue;
1119
1120 case llvm::bitc::DEFINE_ABBREV:
1121 Stream.ReadAbbrevRecord();
1122 continue;
1123 default: break;
1124 }
1125
1126 // Read a record.
1127 Record.clear();
1128 pch::PreprocessorRecordTypes RecType =
1129 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1130 switch (RecType) {
1131 default: // Default behavior: ignore unknown records.
1132 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001133 case pch::PP_COUNTER_VALUE:
1134 if (!Record.empty())
1135 PP.setCounterValue(Record[0]);
1136 break;
1137
Chris Lattner42d42b52009-04-10 21:41:48 +00001138 case pch::PP_MACRO_OBJECT_LIKE:
1139 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +00001140 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1141 if (II == 0)
1142 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +00001143 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1144 bool isUsed = Record[2];
1145
1146 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1147 MI->setIsUsed(isUsed);
1148
1149 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1150 // Decode function-like macro info.
1151 bool isC99VarArgs = Record[3];
1152 bool isGNUVarArgs = Record[4];
1153 MacroArgs.clear();
1154 unsigned NumArgs = Record[5];
1155 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +00001156 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +00001157
1158 // Install function-like macro info.
1159 MI->setIsFunctionLike();
1160 if (isC99VarArgs) MI->setIsC99Varargs();
1161 if (isGNUVarArgs) MI->setIsGNUVarargs();
1162 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1163 PP.getPreprocessorAllocator());
1164 }
1165
1166 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +00001167 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +00001168
1169 // Remember that we saw this macro last so that we add the tokens that
1170 // form its body to it.
1171 LastMacro = MI;
1172 break;
1173 }
1174
1175 case pch::PP_TOKEN: {
1176 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1177 // pretend we didn't see this.
1178 if (LastMacro == 0) break;
1179
1180 Token Tok;
1181 Tok.startToken();
1182 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1183 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001184 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1185 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001186 Tok.setKind((tok::TokenKind)Record[3]);
1187 Tok.setFlag((Token::TokenFlags)Record[4]);
1188 LastMacro->AddTokenToBody(Tok);
1189 break;
1190 }
1191 }
1192 }
1193}
1194
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001195PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1196 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1197 Error("Malformed block record");
1198 return Failure;
1199 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001200
Chris Lattner7356a312009-04-11 21:15:38 +00001201 uint64_t PreprocessorBlockBit = 0;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001202
Douglas Gregor2cf26342009-04-09 22:27:44 +00001203 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001204 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001205 while (!Stream.AtEndOfStream()) {
1206 unsigned Code = Stream.ReadCode();
1207 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001208 // If we saw the preprocessor block, read it now.
1209 if (PreprocessorBlockBit) {
1210 uint64_t SavedPos = Stream.GetCurrentBitNo();
1211 Stream.JumpToBit(PreprocessorBlockBit);
1212 if (ReadPreprocessorBlock()) {
1213 Error("Malformed preprocessor block");
1214 return Failure;
1215 }
1216 Stream.JumpToBit(SavedPos);
1217 }
1218
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001219 if (Stream.ReadBlockEnd()) {
1220 Error("Error at end of module block");
1221 return Failure;
1222 }
Chris Lattner7356a312009-04-11 21:15:38 +00001223
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001224 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001225 }
1226
1227 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1228 switch (Stream.ReadSubBlockID()) {
1229 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1230 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1231 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001232 if (Stream.SkipBlock()) {
1233 Error("Malformed block record");
1234 return Failure;
1235 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001236 break;
1237
Chris Lattner7356a312009-04-11 21:15:38 +00001238 case pch::PREPROCESSOR_BLOCK_ID:
1239 // Skip the preprocessor block for now, but remember where it is. We
1240 // want to read it in after the identifier table.
1241 if (PreprocessorBlockBit) {
1242 Error("Multiple preprocessor blocks found.");
1243 return Failure;
1244 }
1245 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1246 if (Stream.SkipBlock()) {
1247 Error("Malformed block record");
1248 return Failure;
1249 }
1250 break;
1251
Douglas Gregor14f79002009-04-10 03:52:48 +00001252 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001253 switch (ReadSourceManagerBlock()) {
1254 case Success:
1255 break;
1256
1257 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001258 Error("Malformed source manager block");
1259 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001260
1261 case IgnorePCH:
1262 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001263 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001264 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001265 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001266 continue;
1267 }
1268
1269 if (Code == llvm::bitc::DEFINE_ABBREV) {
1270 Stream.ReadAbbrevRecord();
1271 continue;
1272 }
1273
1274 // Read and process a record.
1275 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001276 const char *BlobStart = 0;
1277 unsigned BlobLen = 0;
1278 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1279 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001280 default: // Default behavior: ignore.
1281 break;
1282
1283 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001284 if (!TypeOffsets.empty()) {
1285 Error("Duplicate TYPE_OFFSET record in PCH file");
1286 return Failure;
1287 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001288 TypeOffsets.swap(Record);
1289 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1290 break;
1291
1292 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001293 if (!DeclOffsets.empty()) {
1294 Error("Duplicate DECL_OFFSET record in PCH file");
1295 return Failure;
1296 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001297 DeclOffsets.swap(Record);
1298 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1299 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001300
1301 case pch::LANGUAGE_OPTIONS:
1302 if (ParseLanguageOptions(Record))
1303 return IgnorePCH;
1304 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001305
Douglas Gregorafaf3082009-04-11 00:14:32 +00001306 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001307 std::string TargetTriple(BlobStart, BlobLen);
1308 if (TargetTriple != Context.Target.getTargetTriple()) {
1309 Diag(diag::warn_pch_target_triple)
1310 << TargetTriple << Context.Target.getTargetTriple();
1311 Diag(diag::note_ignoring_pch) << FileName;
1312 return IgnorePCH;
1313 }
1314 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001315 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001316
1317 case pch::IDENTIFIER_TABLE:
1318 IdentifierTable = BlobStart;
1319 break;
1320
1321 case pch::IDENTIFIER_OFFSET:
1322 if (!IdentifierData.empty()) {
1323 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1324 return Failure;
1325 }
1326 IdentifierData.swap(Record);
1327#ifndef NDEBUG
1328 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1329 if ((IdentifierData[I] & 0x01) == 0) {
1330 Error("Malformed identifier table in the precompiled header");
1331 return Failure;
1332 }
1333 }
1334#endif
1335 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001336
1337 case pch::EXTERNAL_DEFINITIONS:
1338 if (!ExternalDefinitions.empty()) {
1339 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1340 return Failure;
1341 }
1342 ExternalDefinitions.swap(Record);
1343 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001344
Douglas Gregorad1de002009-04-18 05:55:16 +00001345 case pch::SPECIAL_TYPES:
1346 SpecialTypes.swap(Record);
1347 break;
1348
Douglas Gregor3e1af842009-04-17 22:13:46 +00001349 case pch::STATISTICS:
1350 TotalNumStatements = Record[0];
1351 break;
1352
Douglas Gregorafaf3082009-04-11 00:14:32 +00001353 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001354 }
1355
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001356 Error("Premature end of bitstream");
1357 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001358}
1359
Douglas Gregore1d918e2009-04-10 23:10:45 +00001360PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001361 // Set the PCH file name.
1362 this->FileName = FileName;
1363
Douglas Gregor2cf26342009-04-09 22:27:44 +00001364 // Open the PCH file.
1365 std::string ErrStr;
1366 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001367 if (!Buffer) {
1368 Error(ErrStr.c_str());
1369 return IgnorePCH;
1370 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001371
1372 // Initialize the stream
1373 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1374 (const unsigned char *)Buffer->getBufferEnd());
1375
1376 // Sniff for the signature.
1377 if (Stream.Read(8) != 'C' ||
1378 Stream.Read(8) != 'P' ||
1379 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001380 Stream.Read(8) != 'H') {
1381 Error("Not a PCH file");
1382 return IgnorePCH;
1383 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384
1385 // We expect a number of well-defined blocks, though we don't necessarily
1386 // need to understand them all.
1387 while (!Stream.AtEndOfStream()) {
1388 unsigned Code = Stream.ReadCode();
1389
Douglas Gregore1d918e2009-04-10 23:10:45 +00001390 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1391 Error("Invalid record at top-level");
1392 return Failure;
1393 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001394
1395 unsigned BlockID = Stream.ReadSubBlockID();
1396
1397 // We only know the PCH subblock ID.
1398 switch (BlockID) {
1399 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001400 if (Stream.ReadBlockInfoBlock()) {
1401 Error("Malformed BlockInfoBlock");
1402 return Failure;
1403 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001404 break;
1405 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001406 switch (ReadPCHBlock()) {
1407 case Success:
1408 break;
1409
1410 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001411 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001412
1413 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001414 // FIXME: We could consider reading through to the end of this
1415 // PCH block, skipping subblocks, to see if there are other
1416 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001417 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001418 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001419 break;
1420 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001421 if (Stream.SkipBlock()) {
1422 Error("Malformed block record");
1423 return Failure;
1424 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001425 break;
1426 }
1427 }
1428
1429 // Load the translation unit declaration
1430 ReadDeclRecord(DeclOffsets[0], 0);
1431
Douglas Gregorad1de002009-04-18 05:55:16 +00001432 // Load the special types.
1433 Context.setBuiltinVaListType(
1434 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1435
Douglas Gregore1d918e2009-04-10 23:10:45 +00001436 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437}
1438
Douglas Gregor0b748912009-04-14 21:18:50 +00001439namespace {
1440 /// \brief Helper class that saves the current stream position and
1441 /// then restores it when destroyed.
1442 struct VISIBILITY_HIDDEN SavedStreamPosition {
1443 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001444 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001445
1446 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001447 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001448 }
1449
1450 private:
1451 llvm::BitstreamReader &Stream;
1452 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001453 };
1454}
1455
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001456/// \brief Parse the record that corresponds to a LangOptions data
1457/// structure.
1458///
1459/// This routine compares the language options used to generate the
1460/// PCH file against the language options set for the current
1461/// compilation. For each option, we classify differences between the
1462/// two compiler states as either "benign" or "important". Benign
1463/// differences don't matter, and we accept them without complaint
1464/// (and without modifying the language options). Differences between
1465/// the states for important options cause the PCH file to be
1466/// unusable, so we emit a warning and return true to indicate that
1467/// there was an error.
1468///
1469/// \returns true if the PCH file is unacceptable, false otherwise.
1470bool PCHReader::ParseLanguageOptions(
1471 const llvm::SmallVectorImpl<uint64_t> &Record) {
1472 const LangOptions &LangOpts = Context.getLangOptions();
1473#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1474#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1475 if (Record[Idx] != LangOpts.Option) { \
1476 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1477 Diag(diag::note_ignoring_pch) << FileName; \
1478 return true; \
1479 } \
1480 ++Idx
1481
1482 unsigned Idx = 0;
1483 PARSE_LANGOPT_BENIGN(Trigraphs);
1484 PARSE_LANGOPT_BENIGN(BCPLComment);
1485 PARSE_LANGOPT_BENIGN(DollarIdents);
1486 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1487 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1488 PARSE_LANGOPT_BENIGN(ImplicitInt);
1489 PARSE_LANGOPT_BENIGN(Digraphs);
1490 PARSE_LANGOPT_BENIGN(HexFloats);
1491 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1492 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1493 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1494 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1495 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1496 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1497 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1498 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1499 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1500 PARSE_LANGOPT_BENIGN(PascalStrings);
1501 PARSE_LANGOPT_BENIGN(Boolean);
1502 PARSE_LANGOPT_BENIGN(WritableStrings);
1503 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1504 diag::warn_pch_lax_vector_conversions);
1505 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1506 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1507 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1508 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1509 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1510 diag::warn_pch_thread_safe_statics);
1511 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1512 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1513 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1514 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1515 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1516 diag::warn_pch_heinous_extensions);
1517 // FIXME: Most of the options below are benign if the macro wasn't
1518 // used. Unfortunately, this means that a PCH compiled without
1519 // optimization can't be used with optimization turned on, even
1520 // though the only thing that changes is whether __OPTIMIZE__ was
1521 // defined... but if __OPTIMIZE__ never showed up in the header, it
1522 // doesn't matter. We could consider making this some special kind
1523 // of check.
1524 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1525 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1526 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1527 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1528 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1529 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1530 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1531 Diag(diag::warn_pch_gc_mode)
1532 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1533 Diag(diag::note_ignoring_pch) << FileName;
1534 return true;
1535 }
1536 ++Idx;
1537 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1538 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1539#undef PARSE_LANGOPT_IRRELEVANT
1540#undef PARSE_LANGOPT_BENIGN
1541
1542 return false;
1543}
1544
Douglas Gregor2cf26342009-04-09 22:27:44 +00001545/// \brief Read and return the type at the given offset.
1546///
1547/// This routine actually reads the record corresponding to the type
1548/// at the given offset in the bitstream. It is a helper routine for
1549/// GetType, which deals with reading type IDs.
1550QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001551 // Keep track of where we are in the stream, then jump back there
1552 // after reading this type.
1553 SavedStreamPosition SavedPosition(Stream);
1554
Douglas Gregor2cf26342009-04-09 22:27:44 +00001555 Stream.JumpToBit(Offset);
1556 RecordData Record;
1557 unsigned Code = Stream.ReadCode();
1558 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001559 case pch::TYPE_EXT_QUAL: {
1560 assert(Record.size() == 3 &&
1561 "Incorrect encoding of extended qualifier type");
1562 QualType Base = GetType(Record[0]);
1563 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1564 unsigned AddressSpace = Record[2];
1565
1566 QualType T = Base;
1567 if (GCAttr != QualType::GCNone)
1568 T = Context.getObjCGCQualType(T, GCAttr);
1569 if (AddressSpace)
1570 T = Context.getAddrSpaceQualType(T, AddressSpace);
1571 return T;
1572 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001573
Douglas Gregor2cf26342009-04-09 22:27:44 +00001574 case pch::TYPE_FIXED_WIDTH_INT: {
1575 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1576 return Context.getFixedWidthIntType(Record[0], Record[1]);
1577 }
1578
1579 case pch::TYPE_COMPLEX: {
1580 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1581 QualType ElemType = GetType(Record[0]);
1582 return Context.getComplexType(ElemType);
1583 }
1584
1585 case pch::TYPE_POINTER: {
1586 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1587 QualType PointeeType = GetType(Record[0]);
1588 return Context.getPointerType(PointeeType);
1589 }
1590
1591 case pch::TYPE_BLOCK_POINTER: {
1592 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1593 QualType PointeeType = GetType(Record[0]);
1594 return Context.getBlockPointerType(PointeeType);
1595 }
1596
1597 case pch::TYPE_LVALUE_REFERENCE: {
1598 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1599 QualType PointeeType = GetType(Record[0]);
1600 return Context.getLValueReferenceType(PointeeType);
1601 }
1602
1603 case pch::TYPE_RVALUE_REFERENCE: {
1604 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1605 QualType PointeeType = GetType(Record[0]);
1606 return Context.getRValueReferenceType(PointeeType);
1607 }
1608
1609 case pch::TYPE_MEMBER_POINTER: {
1610 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1611 QualType PointeeType = GetType(Record[0]);
1612 QualType ClassType = GetType(Record[1]);
1613 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1614 }
1615
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001616 case pch::TYPE_CONSTANT_ARRAY: {
1617 QualType ElementType = GetType(Record[0]);
1618 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1619 unsigned IndexTypeQuals = Record[2];
1620 unsigned Idx = 3;
1621 llvm::APInt Size = ReadAPInt(Record, Idx);
1622 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1623 }
1624
1625 case pch::TYPE_INCOMPLETE_ARRAY: {
1626 QualType ElementType = GetType(Record[0]);
1627 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1628 unsigned IndexTypeQuals = Record[2];
1629 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1630 }
1631
1632 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001633 QualType ElementType = GetType(Record[0]);
1634 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1635 unsigned IndexTypeQuals = Record[2];
1636 return Context.getVariableArrayType(ElementType, ReadExpr(),
1637 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001638 }
1639
1640 case pch::TYPE_VECTOR: {
1641 if (Record.size() != 2) {
1642 Error("Incorrect encoding of vector type in PCH file");
1643 return QualType();
1644 }
1645
1646 QualType ElementType = GetType(Record[0]);
1647 unsigned NumElements = Record[1];
1648 return Context.getVectorType(ElementType, NumElements);
1649 }
1650
1651 case pch::TYPE_EXT_VECTOR: {
1652 if (Record.size() != 2) {
1653 Error("Incorrect encoding of extended vector type in PCH file");
1654 return QualType();
1655 }
1656
1657 QualType ElementType = GetType(Record[0]);
1658 unsigned NumElements = Record[1];
1659 return Context.getExtVectorType(ElementType, NumElements);
1660 }
1661
1662 case pch::TYPE_FUNCTION_NO_PROTO: {
1663 if (Record.size() != 1) {
1664 Error("Incorrect encoding of no-proto function type");
1665 return QualType();
1666 }
1667 QualType ResultType = GetType(Record[0]);
1668 return Context.getFunctionNoProtoType(ResultType);
1669 }
1670
1671 case pch::TYPE_FUNCTION_PROTO: {
1672 QualType ResultType = GetType(Record[0]);
1673 unsigned Idx = 1;
1674 unsigned NumParams = Record[Idx++];
1675 llvm::SmallVector<QualType, 16> ParamTypes;
1676 for (unsigned I = 0; I != NumParams; ++I)
1677 ParamTypes.push_back(GetType(Record[Idx++]));
1678 bool isVariadic = Record[Idx++];
1679 unsigned Quals = Record[Idx++];
1680 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1681 isVariadic, Quals);
1682 }
1683
1684 case pch::TYPE_TYPEDEF:
1685 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1686 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1687
1688 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001689 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001690
1691 case pch::TYPE_TYPEOF: {
1692 if (Record.size() != 1) {
1693 Error("Incorrect encoding of typeof(type) in PCH file");
1694 return QualType();
1695 }
1696 QualType UnderlyingType = GetType(Record[0]);
1697 return Context.getTypeOfType(UnderlyingType);
1698 }
1699
1700 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001701 assert(Record.size() == 1 && "Incorrect encoding of record type");
1702 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001703
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001704 case pch::TYPE_ENUM:
1705 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1706 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1707
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001708 case pch::TYPE_OBJC_INTERFACE:
1709 // FIXME: Deserialize ObjCInterfaceType
1710 assert(false && "Cannot de-serialize ObjC interface types yet");
1711 return QualType();
1712
1713 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1714 // FIXME: Deserialize ObjCQualifiedInterfaceType
1715 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1716 return QualType();
1717
1718 case pch::TYPE_OBJC_QUALIFIED_ID:
1719 // FIXME: Deserialize ObjCQualifiedIdType
1720 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1721 return QualType();
1722
1723 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1724 // FIXME: Deserialize ObjCQualifiedClassType
1725 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1726 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001727 }
1728
1729 // Suppress a GCC warning
1730 return QualType();
1731}
1732
1733/// \brief Note that we have loaded the declaration with the given
1734/// Index.
1735///
1736/// This routine notes that this declaration has already been loaded,
1737/// so that future GetDecl calls will return this declaration rather
1738/// than trying to load a new declaration.
1739inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1740 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1741 DeclAlreadyLoaded[Index] = true;
1742 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1743}
1744
1745/// \brief Read the declaration at the given offset from the PCH file.
1746Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001747 // Keep track of where we are in the stream, then jump back there
1748 // after reading this declaration.
1749 SavedStreamPosition SavedPosition(Stream);
1750
Douglas Gregor2cf26342009-04-09 22:27:44 +00001751 Decl *D = 0;
1752 Stream.JumpToBit(Offset);
1753 RecordData Record;
1754 unsigned Code = Stream.ReadCode();
1755 unsigned Idx = 0;
1756 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001757
Douglas Gregor2cf26342009-04-09 22:27:44 +00001758 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001759 case pch::DECL_ATTR:
1760 case pch::DECL_CONTEXT_LEXICAL:
1761 case pch::DECL_CONTEXT_VISIBLE:
1762 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1763 break;
1764
Douglas Gregor2cf26342009-04-09 22:27:44 +00001765 case pch::DECL_TRANSLATION_UNIT:
1766 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001767 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 break;
1769
1770 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001771 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001772 break;
1773 }
1774
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001775 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001776 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001777 break;
1778 }
1779
Douglas Gregor8c700062009-04-13 21:20:57 +00001780 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001781 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1782 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001783 break;
1784 }
1785
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001786 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001787 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1788 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001789 break;
1790 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001791
1792 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001793 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1794 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001795 break;
1796 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001797
Steve Naroff53c9d8a2009-04-20 15:06:07 +00001798 case pch::DECL_OBJC_METHOD: {
1799 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1800 Selector(), QualType(), 0);
1801 break;
1802 }
1803
Douglas Gregor8c700062009-04-13 21:20:57 +00001804 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001805 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1806 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001807 break;
1808 }
1809
Douglas Gregor2cf26342009-04-09 22:27:44 +00001810 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001811 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1812 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001813 break;
1814 }
1815
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001816 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001817 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1818 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001819 break;
1820 }
1821
1822 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001823 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001824 QualType(), QualType(), VarDecl::None,
1825 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001826 break;
1827 }
1828
Douglas Gregor1028bc62009-04-13 22:49:25 +00001829 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001830 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001831 break;
1832 }
1833
1834 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001835 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001836 break;
1837 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001838 }
1839
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001840 assert(D && "Unknown declaration creating PCH file");
1841 if (D) {
1842 LoadedDecl(Index, D);
1843 Reader.Visit(D);
1844 }
1845
Douglas Gregor2cf26342009-04-09 22:27:44 +00001846 // If this declaration is also a declaration context, get the
1847 // offsets for its tables of lexical and visible declarations.
1848 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1849 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1850 if (Offsets.first || Offsets.second) {
1851 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1852 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1853 DeclContextOffsets[DC] = Offsets;
1854 }
1855 }
1856 assert(Idx == Record.size());
1857
1858 return D;
1859}
1860
Douglas Gregor8038d512009-04-10 17:25:41 +00001861QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001862 unsigned Quals = ID & 0x07;
1863 unsigned Index = ID >> 3;
1864
1865 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1866 QualType T;
1867 switch ((pch::PredefinedTypeIDs)Index) {
1868 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1869 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1870 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1871
1872 case pch::PREDEF_TYPE_CHAR_U_ID:
1873 case pch::PREDEF_TYPE_CHAR_S_ID:
1874 // FIXME: Check that the signedness of CharTy is correct!
1875 T = Context.CharTy;
1876 break;
1877
1878 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1879 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1880 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1881 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1882 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1883 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1884 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1885 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1886 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1887 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1888 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1889 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1890 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1891 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1892 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1893 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1894 }
1895
1896 assert(!T.isNull() && "Unknown predefined type");
1897 return T.getQualifiedType(Quals);
1898 }
1899
1900 Index -= pch::NUM_PREDEF_TYPE_IDS;
1901 if (!TypeAlreadyLoaded[Index]) {
1902 // Load the type from the PCH file.
1903 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1904 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1905 TypeAlreadyLoaded[Index] = true;
1906 }
1907
1908 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1909}
1910
Douglas Gregor8038d512009-04-10 17:25:41 +00001911Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001912 if (ID == 0)
1913 return 0;
1914
1915 unsigned Index = ID - 1;
1916 if (DeclAlreadyLoaded[Index])
1917 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1918
1919 // Load the declaration from the PCH file.
1920 return ReadDeclRecord(DeclOffsets[Index], Index);
1921}
1922
Douglas Gregor250fc9c2009-04-18 00:07:54 +00001923Stmt *PCHReader::GetStmt(uint64_t Offset) {
1924 // Keep track of where we are in the stream, then jump back there
1925 // after reading this declaration.
1926 SavedStreamPosition SavedPosition(Stream);
1927
1928 Stream.JumpToBit(Offset);
1929 return ReadStmt();
1930}
1931
Douglas Gregor2cf26342009-04-09 22:27:44 +00001932bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001933 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001934 assert(DC->hasExternalLexicalStorage() &&
1935 "DeclContext has no lexical decls in storage");
1936 uint64_t Offset = DeclContextOffsets[DC].first;
1937 assert(Offset && "DeclContext has no lexical decls in storage");
1938
Douglas Gregor0b748912009-04-14 21:18:50 +00001939 // Keep track of where we are in the stream, then jump back there
1940 // after reading this context.
1941 SavedStreamPosition SavedPosition(Stream);
1942
Douglas Gregor2cf26342009-04-09 22:27:44 +00001943 // Load the record containing all of the declarations lexically in
1944 // this context.
1945 Stream.JumpToBit(Offset);
1946 RecordData Record;
1947 unsigned Code = Stream.ReadCode();
1948 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001949 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001950 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1951
1952 // Load all of the declaration IDs
1953 Decls.clear();
1954 Decls.insert(Decls.end(), Record.begin(), Record.end());
1955 return false;
1956}
1957
1958bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1959 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1960 assert(DC->hasExternalVisibleStorage() &&
1961 "DeclContext has no visible decls in storage");
1962 uint64_t Offset = DeclContextOffsets[DC].second;
1963 assert(Offset && "DeclContext has no visible decls in storage");
1964
Douglas Gregor0b748912009-04-14 21:18:50 +00001965 // Keep track of where we are in the stream, then jump back there
1966 // after reading this context.
1967 SavedStreamPosition SavedPosition(Stream);
1968
Douglas Gregor2cf26342009-04-09 22:27:44 +00001969 // Load the record containing all of the declarations visible in
1970 // this context.
1971 Stream.JumpToBit(Offset);
1972 RecordData Record;
1973 unsigned Code = Stream.ReadCode();
1974 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001975 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001976 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1977 if (Record.size() == 0)
1978 return false;
1979
1980 Decls.clear();
1981
1982 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001983 while (Idx < Record.size()) {
1984 Decls.push_back(VisibleDeclaration());
1985 Decls.back().Name = ReadDeclarationName(Record, Idx);
1986
Douglas Gregor2cf26342009-04-09 22:27:44 +00001987 unsigned Size = Record[Idx++];
1988 llvm::SmallVector<unsigned, 4> & LoadedDecls
1989 = Decls.back().Declarations;
1990 LoadedDecls.reserve(Size);
1991 for (unsigned I = 0; I < Size; ++I)
1992 LoadedDecls.push_back(Record[Idx++]);
1993 }
1994
1995 return false;
1996}
1997
Douglas Gregorfdd01722009-04-14 00:24:19 +00001998void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1999 if (!Consumer)
2000 return;
2001
2002 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2003 Decl *D = GetDecl(ExternalDefinitions[I]);
2004 DeclGroupRef DG(D);
2005 Consumer->HandleTopLevelDecl(DG);
2006 }
2007}
2008
Douglas Gregor2cf26342009-04-09 22:27:44 +00002009void PCHReader::PrintStats() {
2010 std::fprintf(stderr, "*** PCH Statistics:\n");
2011
2012 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2013 TypeAlreadyLoaded.end(),
2014 true);
2015 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2016 DeclAlreadyLoaded.end(),
2017 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002018 unsigned NumIdentifiersLoaded = 0;
2019 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2020 if ((IdentifierData[I] & 0x01) == 0)
2021 ++NumIdentifiersLoaded;
2022 }
2023
Douglas Gregor2cf26342009-04-09 22:27:44 +00002024 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2025 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002026 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002027 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2028 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00002029 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2030 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2031 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2032 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor3e1af842009-04-17 22:13:46 +00002033 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2034 NumStatementsRead, TotalNumStatements,
2035 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002036 std::fprintf(stderr, "\n");
2037}
2038
Chris Lattner7356a312009-04-11 21:15:38 +00002039IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002040 if (ID == 0)
2041 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00002042
Douglas Gregorafaf3082009-04-11 00:14:32 +00002043 if (!IdentifierTable || IdentifierData.empty()) {
2044 Error("No identifier table in PCH file");
2045 return 0;
2046 }
Chris Lattner7356a312009-04-11 21:15:38 +00002047
Douglas Gregorafaf3082009-04-11 00:14:32 +00002048 if (IdentifierData[ID - 1] & 0x01) {
2049 uint64_t Offset = IdentifierData[ID - 1];
2050 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00002051 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002052 }
Chris Lattner7356a312009-04-11 21:15:38 +00002053
2054 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002055}
2056
2057DeclarationName
2058PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2059 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2060 switch (Kind) {
2061 case DeclarationName::Identifier:
2062 return DeclarationName(GetIdentifierInfo(Record, Idx));
2063
2064 case DeclarationName::ObjCZeroArgSelector:
2065 case DeclarationName::ObjCOneArgSelector:
2066 case DeclarationName::ObjCMultiArgSelector:
2067 assert(false && "Unable to de-serialize Objective-C selectors");
2068 break;
2069
2070 case DeclarationName::CXXConstructorName:
2071 return Context.DeclarationNames.getCXXConstructorName(
2072 GetType(Record[Idx++]));
2073
2074 case DeclarationName::CXXDestructorName:
2075 return Context.DeclarationNames.getCXXDestructorName(
2076 GetType(Record[Idx++]));
2077
2078 case DeclarationName::CXXConversionFunctionName:
2079 return Context.DeclarationNames.getCXXConversionFunctionName(
2080 GetType(Record[Idx++]));
2081
2082 case DeclarationName::CXXOperatorName:
2083 return Context.DeclarationNames.getCXXOperatorName(
2084 (OverloadedOperatorKind)Record[Idx++]);
2085
2086 case DeclarationName::CXXUsingDirective:
2087 return DeclarationName::getUsingDirectiveName();
2088 }
2089
2090 // Required to silence GCC warning
2091 return DeclarationName();
2092}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002093
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002094/// \brief Read an integral value
2095llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2096 unsigned BitWidth = Record[Idx++];
2097 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2098 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2099 Idx += NumWords;
2100 return Result;
2101}
2102
2103/// \brief Read a signed integral value
2104llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2105 bool isUnsigned = Record[Idx++];
2106 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2107}
2108
Douglas Gregor17fc2232009-04-14 21:55:33 +00002109/// \brief Read a floating-point value
2110llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002111 return llvm::APFloat(ReadAPInt(Record, Idx));
2112}
2113
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002114// \brief Read a string
2115std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2116 unsigned Len = Record[Idx++];
2117 std::string Result(&Record[Idx], &Record[Idx] + Len);
2118 Idx += Len;
2119 return Result;
2120}
2121
2122/// \brief Reads attributes from the current stream position.
2123Attr *PCHReader::ReadAttributes() {
2124 unsigned Code = Stream.ReadCode();
2125 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2126 "Expected unabbreviated record"); (void)Code;
2127
2128 RecordData Record;
2129 unsigned Idx = 0;
2130 unsigned RecCode = Stream.ReadRecord(Code, Record);
2131 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2132 (void)RecCode;
2133
2134#define SIMPLE_ATTR(Name) \
2135 case Attr::Name: \
2136 New = ::new (Context) Name##Attr(); \
2137 break
2138
2139#define STRING_ATTR(Name) \
2140 case Attr::Name: \
2141 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2142 break
2143
2144#define UNSIGNED_ATTR(Name) \
2145 case Attr::Name: \
2146 New = ::new (Context) Name##Attr(Record[Idx++]); \
2147 break
2148
2149 Attr *Attrs = 0;
2150 while (Idx < Record.size()) {
2151 Attr *New = 0;
2152 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2153 bool IsInherited = Record[Idx++];
2154
2155 switch (Kind) {
2156 STRING_ATTR(Alias);
2157 UNSIGNED_ATTR(Aligned);
2158 SIMPLE_ATTR(AlwaysInline);
2159 SIMPLE_ATTR(AnalyzerNoReturn);
2160 STRING_ATTR(Annotate);
2161 STRING_ATTR(AsmLabel);
2162
2163 case Attr::Blocks:
2164 New = ::new (Context) BlocksAttr(
2165 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2166 break;
2167
2168 case Attr::Cleanup:
2169 New = ::new (Context) CleanupAttr(
2170 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2171 break;
2172
2173 SIMPLE_ATTR(Const);
2174 UNSIGNED_ATTR(Constructor);
2175 SIMPLE_ATTR(DLLExport);
2176 SIMPLE_ATTR(DLLImport);
2177 SIMPLE_ATTR(Deprecated);
2178 UNSIGNED_ATTR(Destructor);
2179 SIMPLE_ATTR(FastCall);
2180
2181 case Attr::Format: {
2182 std::string Type = ReadString(Record, Idx);
2183 unsigned FormatIdx = Record[Idx++];
2184 unsigned FirstArg = Record[Idx++];
2185 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2186 break;
2187 }
2188
2189 SIMPLE_ATTR(GNUCInline);
2190
2191 case Attr::IBOutletKind:
2192 New = ::new (Context) IBOutletAttr();
2193 break;
2194
2195 SIMPLE_ATTR(NoReturn);
2196 SIMPLE_ATTR(NoThrow);
2197 SIMPLE_ATTR(Nodebug);
2198 SIMPLE_ATTR(Noinline);
2199
2200 case Attr::NonNull: {
2201 unsigned Size = Record[Idx++];
2202 llvm::SmallVector<unsigned, 16> ArgNums;
2203 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2204 Idx += Size;
2205 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2206 break;
2207 }
2208
2209 SIMPLE_ATTR(ObjCException);
2210 SIMPLE_ATTR(ObjCNSObject);
2211 SIMPLE_ATTR(Overloadable);
2212 UNSIGNED_ATTR(Packed);
2213 SIMPLE_ATTR(Pure);
2214 UNSIGNED_ATTR(Regparm);
2215 STRING_ATTR(Section);
2216 SIMPLE_ATTR(StdCall);
2217 SIMPLE_ATTR(TransparentUnion);
2218 SIMPLE_ATTR(Unavailable);
2219 SIMPLE_ATTR(Unused);
2220 SIMPLE_ATTR(Used);
2221
2222 case Attr::Visibility:
2223 New = ::new (Context) VisibilityAttr(
2224 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2225 break;
2226
2227 SIMPLE_ATTR(WarnUnusedResult);
2228 SIMPLE_ATTR(Weak);
2229 SIMPLE_ATTR(WeakImport);
2230 }
2231
2232 assert(New && "Unable to decode attribute?");
2233 New->setInherited(IsInherited);
2234 New->setNext(Attrs);
2235 Attrs = New;
2236 }
2237#undef UNSIGNED_ATTR
2238#undef STRING_ATTR
2239#undef SIMPLE_ATTR
2240
2241 // The list of attributes was built backwards. Reverse the list
2242 // before returning it.
2243 Attr *PrevAttr = 0, *NextAttr = 0;
2244 while (Attrs) {
2245 NextAttr = Attrs->getNext();
2246 Attrs->setNext(PrevAttr);
2247 PrevAttr = Attrs;
2248 Attrs = NextAttr;
2249 }
2250
2251 return PrevAttr;
2252}
2253
Douglas Gregorc9490c02009-04-16 22:23:12 +00002254Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002255 // Within the bitstream, expressions are stored in Reverse Polish
2256 // Notation, with each of the subexpressions preceding the
2257 // expression they are stored in. To evaluate expressions, we
2258 // continue reading expressions and placing them on the stack, with
2259 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002260 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002261 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002262 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002263 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002264 llvm::SmallVector<Stmt *, 16> StmtStack;
2265 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002266 Stmt::EmptyShell Empty;
2267
Douglas Gregor087fd532009-04-14 23:32:43 +00002268 while (true) {
2269 unsigned Code = Stream.ReadCode();
2270 if (Code == llvm::bitc::END_BLOCK) {
2271 if (Stream.ReadBlockEnd()) {
2272 Error("Error at end of Source Manager block");
2273 return 0;
2274 }
2275 break;
2276 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002277
Douglas Gregor087fd532009-04-14 23:32:43 +00002278 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2279 // No known subblocks, always skip them.
2280 Stream.ReadSubBlockID();
2281 if (Stream.SkipBlock()) {
2282 Error("Malformed block record");
2283 return 0;
2284 }
2285 continue;
2286 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002287
Douglas Gregor087fd532009-04-14 23:32:43 +00002288 if (Code == llvm::bitc::DEFINE_ABBREV) {
2289 Stream.ReadAbbrevRecord();
2290 continue;
2291 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002292
Douglas Gregorc9490c02009-04-16 22:23:12 +00002293 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002294 Idx = 0;
2295 Record.clear();
2296 bool Finished = false;
2297 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002298 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002299 Finished = true;
2300 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002301
Douglas Gregorc9490c02009-04-16 22:23:12 +00002302 case pch::STMT_NULL_PTR:
2303 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002304 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002305
Douglas Gregor025452f2009-04-17 00:04:06 +00002306 case pch::STMT_NULL:
2307 S = new (Context) NullStmt(Empty);
2308 break;
2309
2310 case pch::STMT_COMPOUND:
2311 S = new (Context) CompoundStmt(Empty);
2312 break;
2313
2314 case pch::STMT_CASE:
2315 S = new (Context) CaseStmt(Empty);
2316 break;
2317
2318 case pch::STMT_DEFAULT:
2319 S = new (Context) DefaultStmt(Empty);
2320 break;
2321
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002322 case pch::STMT_LABEL:
2323 S = new (Context) LabelStmt(Empty);
2324 break;
2325
Douglas Gregor025452f2009-04-17 00:04:06 +00002326 case pch::STMT_IF:
2327 S = new (Context) IfStmt(Empty);
2328 break;
2329
2330 case pch::STMT_SWITCH:
2331 S = new (Context) SwitchStmt(Empty);
2332 break;
2333
Douglas Gregord921cf92009-04-17 00:16:09 +00002334 case pch::STMT_WHILE:
2335 S = new (Context) WhileStmt(Empty);
2336 break;
2337
Douglas Gregor67d82492009-04-17 00:29:51 +00002338 case pch::STMT_DO:
2339 S = new (Context) DoStmt(Empty);
2340 break;
2341
2342 case pch::STMT_FOR:
2343 S = new (Context) ForStmt(Empty);
2344 break;
2345
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002346 case pch::STMT_GOTO:
2347 S = new (Context) GotoStmt(Empty);
2348 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002349
2350 case pch::STMT_INDIRECT_GOTO:
2351 S = new (Context) IndirectGotoStmt(Empty);
2352 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002353
Douglas Gregord921cf92009-04-17 00:16:09 +00002354 case pch::STMT_CONTINUE:
2355 S = new (Context) ContinueStmt(Empty);
2356 break;
2357
Douglas Gregor025452f2009-04-17 00:04:06 +00002358 case pch::STMT_BREAK:
2359 S = new (Context) BreakStmt(Empty);
2360 break;
2361
Douglas Gregor0de9d882009-04-17 16:34:57 +00002362 case pch::STMT_RETURN:
2363 S = new (Context) ReturnStmt(Empty);
2364 break;
2365
Douglas Gregor84f21702009-04-17 16:55:36 +00002366 case pch::STMT_DECL:
2367 S = new (Context) DeclStmt(Empty);
2368 break;
2369
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00002370 case pch::STMT_ASM:
2371 S = new (Context) AsmStmt(Empty);
2372 break;
2373
Douglas Gregor087fd532009-04-14 23:32:43 +00002374 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002375 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002376 break;
2377
2378 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002379 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002380 break;
2381
2382 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002383 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002384 break;
2385
2386 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002387 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002388 break;
2389
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002390 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002391 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002392 break;
2393
Douglas Gregor673ecd62009-04-15 16:35:07 +00002394 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002395 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002396 Record[PCHStmtReader::NumExprFields + 1]);
2397 break;
2398
Douglas Gregor087fd532009-04-14 23:32:43 +00002399 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002400 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002401 break;
2402
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002403 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002404 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002405 break;
2406
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002407 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002408 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002409 break;
2410
2411 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002412 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002413 break;
2414
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002415 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002416 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002417 break;
2418
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002419 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002420 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002421 break;
2422
2423 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002424 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002425 break;
2426
Douglas Gregordb600c32009-04-15 00:25:59 +00002427 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002428 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002429 break;
2430
Douglas Gregorad90e962009-04-15 22:40:36 +00002431 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002432 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002433 break;
2434
2435 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002436 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002437 break;
2438
Douglas Gregor087fd532009-04-14 23:32:43 +00002439 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002440 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002441 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002442
2443 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002444 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002445 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002446
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002447 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002448 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002449 break;
2450
Douglas Gregord3c98a02009-04-15 23:02:49 +00002451 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002452 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002453 break;
2454
Douglas Gregord077d752009-04-16 00:55:48 +00002455 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002456 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002457 break;
2458
2459 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002460 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002461 Record[PCHStmtReader::NumExprFields] - 1);
2462
2463 break;
2464
2465 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002466 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002467 break;
2468
Douglas Gregord3c98a02009-04-15 23:02:49 +00002469 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002470 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002471 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002472
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002473 case pch::EXPR_ADDR_LABEL:
2474 S = new (Context) AddrLabelExpr(Empty);
2475 break;
2476
Douglas Gregor6a2dd552009-04-17 19:05:30 +00002477 case pch::EXPR_STMT:
2478 S = new (Context) StmtExpr(Empty);
2479 break;
2480
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002481 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002482 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002483 break;
2484
2485 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002486 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002487 break;
2488
2489 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002490 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002491 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002492
2493 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002494 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002495 break;
2496
Douglas Gregor84af7c22009-04-17 19:21:43 +00002497 case pch::EXPR_BLOCK:
2498 S = new (Context) BlockExpr(Empty);
2499 break;
2500
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002501 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002502 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002503 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002504 }
2505
Douglas Gregorc9490c02009-04-16 22:23:12 +00002506 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002507 if (Finished)
2508 break;
2509
Douglas Gregor3e1af842009-04-17 22:13:46 +00002510 ++NumStatementsRead;
2511
Douglas Gregorc9490c02009-04-16 22:23:12 +00002512 if (S) {
2513 unsigned NumSubStmts = Reader.Visit(S);
2514 while (NumSubStmts > 0) {
2515 StmtStack.pop_back();
2516 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002517 }
2518 }
2519
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002520 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002521 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002522 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002523 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00002524 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002525 return StmtStack.back();
2526}
2527
2528Expr *PCHReader::ReadExpr() {
2529 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002530}
2531
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002532DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002533 return Diag(SourceLocation(), DiagID);
2534}
2535
2536DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2537 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002538 Context.getSourceManager()),
2539 DiagID);
2540}
Douglas Gregor025452f2009-04-17 00:04:06 +00002541
2542/// \brief Record that the given ID maps to the given switch-case
2543/// statement.
2544void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2545 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2546 SwitchCaseStmts[ID] = SC;
2547}
2548
2549/// \brief Retrieve the switch-case statement with the given ID.
2550SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2551 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2552 return SwitchCaseStmts[ID];
2553}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002554
2555/// \brief Record that the given label statement has been
2556/// deserialized and has the given ID.
2557void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2558 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2559 "Deserialized label twice");
2560 LabelStmts[ID] = S;
2561
2562 // If we've already seen any goto statements that point to this
2563 // label, resolve them now.
2564 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2565 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2566 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2567 Goto->second->setLabel(S);
2568 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002569
2570 // If we've already seen any address-label statements that point to
2571 // this label, resolve them now.
2572 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2573 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2574 = UnresolvedAddrLabelExprs.equal_range(ID);
2575 for (AddrLabelIter AddrLabel = AddrLabels.first;
2576 AddrLabel != AddrLabels.second; ++AddrLabel)
2577 AddrLabel->second->setLabel(S);
2578 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002579}
2580
2581/// \brief Set the label of the given statement to the label
2582/// identified by ID.
2583///
2584/// Depending on the order in which the label and other statements
2585/// referencing that label occur, this operation may complete
2586/// immediately (updating the statement) or it may queue the
2587/// statement to be back-patched later.
2588void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2589 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2590 if (Label != LabelStmts.end()) {
2591 // We've already seen this label, so set the label of the goto and
2592 // we're done.
2593 S->setLabel(Label->second);
2594 } else {
2595 // We haven't seen this label yet, so add this goto to the set of
2596 // unresolved goto statements.
2597 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2598 }
2599}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002600
2601/// \brief Set the label of the given expression to the label
2602/// identified by ID.
2603///
2604/// Depending on the order in which the label and other statements
2605/// referencing that label occur, this operation may complete
2606/// immediately (updating the statement) or it may queue the
2607/// statement to be back-patched later.
2608void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2609 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2610 if (Label != LabelStmts.end()) {
2611 // We've already seen this label, so set the label of the
2612 // label-address expression and we're done.
2613 S->setLabel(Label->second);
2614 } else {
2615 // We haven't seen this label yet, so add this label-address
2616 // expression to the set of unresolved label-address expressions.
2617 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2618 }
2619}