blob: da7b4229aa1d4871eaf10b2cff7467c59d09e5ba [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor8c700062009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor8c700062009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000167}
168
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor1028bc62009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000200 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000201 unsigned NumParams = Record[Idx++];
202 llvm::SmallVector<ParmVarDecl *, 16> Params;
203 Params.reserve(NumParams);
204 for (unsigned I = 0; I != NumParams; ++I)
205 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
206 BD->setParams(Reader.getContext(), &Params[0], NumParams);
207}
208
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209std::pair<uint64_t, uint64_t>
210PCHDeclReader::VisitDeclContext(DeclContext *DC) {
211 uint64_t LexicalOffset = Record[Idx++];
212 uint64_t VisibleOffset = 0;
213 if (DC->getPrimaryContext() == DC)
214 VisibleOffset = Record[Idx++];
215 return std::make_pair(LexicalOffset, VisibleOffset);
216}
217
Douglas Gregor0b748912009-04-14 21:18:50 +0000218//===----------------------------------------------------------------------===//
219// Statement/expression deserialization
220//===----------------------------------------------------------------------===//
221namespace {
222 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000223 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000224 PCHReader &Reader;
225 const PCHReader::RecordData &Record;
226 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000227 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000228
229 public:
230 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000231 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
232 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000233
Douglas Gregor025452f2009-04-17 00:04:06 +0000234 /// \brief The number of record fields required for the Stmt class
235 /// itself.
236 static const unsigned NumStmtFields = 0;
237
Douglas Gregor673ecd62009-04-15 16:35:07 +0000238 /// \brief The number of record fields required for the Expr class
239 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000240 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000241
Douglas Gregor087fd532009-04-14 23:32:43 +0000242 // Each of the Visit* functions reads in part of the expression
243 // from the given record and the current expression stack, then
244 // return the total number of operands that it read from the
245 // expression stack.
246
Douglas Gregor025452f2009-04-17 00:04:06 +0000247 unsigned VisitStmt(Stmt *S);
248 unsigned VisitNullStmt(NullStmt *S);
249 unsigned VisitCompoundStmt(CompoundStmt *S);
250 unsigned VisitSwitchCase(SwitchCase *S);
251 unsigned VisitCaseStmt(CaseStmt *S);
252 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000253 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000254 unsigned VisitIfStmt(IfStmt *S);
255 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000256 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregor67d82492009-04-17 00:29:51 +0000257 unsigned VisitDoStmt(DoStmt *S);
258 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000259 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000260 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregord921cf92009-04-17 00:16:09 +0000261 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000262 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor0de9d882009-04-17 16:34:57 +0000263 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor84f21702009-04-17 16:55:36 +0000264 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000265 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000266 unsigned VisitExpr(Expr *E);
267 unsigned VisitPredefinedExpr(PredefinedExpr *E);
268 unsigned VisitDeclRefExpr(DeclRefExpr *E);
269 unsigned VisitIntegerLiteral(IntegerLiteral *E);
270 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000271 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000272 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000273 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000274 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000275 unsigned VisitUnaryOperator(UnaryOperator *E);
276 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000277 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000278 unsigned VisitCallExpr(CallExpr *E);
279 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000280 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000281 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000282 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
283 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000284 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000285 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
286 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000287 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000288 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000289 unsigned VisitInitListExpr(InitListExpr *E);
290 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
291 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000292 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000293 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000294 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000295 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
296 unsigned VisitChooseExpr(ChooseExpr *E);
297 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000298 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregor84af7c22009-04-17 19:21:43 +0000299 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000300 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000301 };
302}
303
Douglas Gregor025452f2009-04-17 00:04:06 +0000304unsigned PCHStmtReader::VisitStmt(Stmt *S) {
305 assert(Idx == NumStmtFields && "Incorrect statement field count");
306 return 0;
307}
308
309unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
310 VisitStmt(S);
311 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
312 return 0;
313}
314
315unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
316 VisitStmt(S);
317 unsigned NumStmts = Record[Idx++];
318 S->setStmts(Reader.getContext(),
319 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
320 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
321 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
322 return NumStmts;
323}
324
325unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
326 VisitStmt(S);
327 Reader.RecordSwitchCaseID(S, Record[Idx++]);
328 return 0;
329}
330
331unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
332 VisitSwitchCase(S);
333 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
334 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
335 S->setSubStmt(StmtStack.back());
336 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 return 3;
338}
339
340unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
341 VisitSwitchCase(S);
342 S->setSubStmt(StmtStack.back());
343 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
344 return 1;
345}
346
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000347unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
348 VisitStmt(S);
349 S->setID(Reader.GetIdentifierInfo(Record, Idx));
350 S->setSubStmt(StmtStack.back());
351 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 Reader.RecordLabelStmt(S, Record[Idx++]);
353 return 1;
354}
355
Douglas Gregor025452f2009-04-17 00:04:06 +0000356unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
357 VisitStmt(S);
358 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
359 S->setThen(StmtStack[StmtStack.size() - 2]);
360 S->setElse(StmtStack[StmtStack.size() - 1]);
361 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
362 return 3;
363}
364
365unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
366 VisitStmt(S);
367 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
368 S->setBody(StmtStack.back());
369 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
370 SwitchCase *PrevSC = 0;
371 for (unsigned N = Record.size(); Idx != N; ++Idx) {
372 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
373 if (PrevSC)
374 PrevSC->setNextSwitchCase(SC);
375 else
376 S->setSwitchCaseList(SC);
377 PrevSC = SC;
378 }
379 return 2;
380}
381
Douglas Gregord921cf92009-04-17 00:16:09 +0000382unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
383 VisitStmt(S);
384 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
385 S->setBody(StmtStack.back());
386 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
387 return 2;
388}
389
Douglas Gregor67d82492009-04-17 00:29:51 +0000390unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
391 VisitStmt(S);
392 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
393 S->setBody(StmtStack.back());
394 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
395 return 2;
396}
397
398unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
399 VisitStmt(S);
400 S->setInit(StmtStack[StmtStack.size() - 4]);
401 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
402 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
403 S->setBody(StmtStack.back());
404 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
405 return 4;
406}
407
Douglas Gregor1de05fe2009-04-17 18:18:49 +0000408unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
409 VisitStmt(S);
410 Reader.SetLabelOf(S, Record[Idx++]);
411 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
412 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 return 0;
414}
415
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000416unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
417 VisitStmt(S);
418 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
419 return 1;
420}
421
Douglas Gregord921cf92009-04-17 00:16:09 +0000422unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
423 VisitStmt(S);
424 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
425 return 0;
426}
427
Douglas Gregor025452f2009-04-17 00:04:06 +0000428unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
429 VisitStmt(S);
430 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
431 return 0;
432}
433
Douglas Gregor0de9d882009-04-17 16:34:57 +0000434unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
435 VisitStmt(S);
436 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
437 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
438 return 1;
439}
440
Douglas Gregor84f21702009-04-17 16:55:36 +0000441unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
442 VisitStmt(S);
443 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
444 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
445
446 if (Idx + 1 == Record.size()) {
447 // Single declaration
448 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
449 } else {
450 llvm::SmallVector<Decl *, 16> Decls;
451 Decls.reserve(Record.size() - Idx);
452 for (unsigned N = Record.size(); Idx != N; ++Idx)
453 Decls.push_back(Reader.GetDecl(Record[Idx]));
454 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
455 &Decls[0], Decls.size())));
456 }
457 return 0;
458}
459
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000460unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
461 VisitStmt(S);
462 unsigned NumOutputs = Record[Idx++];
463 unsigned NumInputs = Record[Idx++];
464 unsigned NumClobbers = Record[Idx++];
465 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
466 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
467 S->setVolatile(Record[Idx++]);
468 S->setSimple(Record[Idx++]);
469
470 unsigned StackIdx
471 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
472 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
473
474 // Outputs and inputs
475 llvm::SmallVector<std::string, 16> Names;
476 llvm::SmallVector<StringLiteral*, 16> Constraints;
477 llvm::SmallVector<Stmt*, 16> Exprs;
478 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
479 Names.push_back(Reader.ReadString(Record, Idx));
480 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
481 Exprs.push_back(StmtStack[StackIdx++]);
482 }
483 S->setOutputsAndInputs(NumOutputs, NumInputs,
484 &Names[0], &Constraints[0], &Exprs[0]);
485
486 // Constraints
487 llvm::SmallVector<StringLiteral*, 16> Clobbers;
488 for (unsigned I = 0; I != NumClobbers; ++I)
489 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
490 S->setClobbers(&Clobbers[0], NumClobbers);
491
492 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
493 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
494}
495
Douglas Gregor087fd532009-04-14 23:32:43 +0000496unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000497 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000498 E->setType(Reader.GetType(Record[Idx++]));
499 E->setTypeDependent(Record[Idx++]);
500 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000501 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000502 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000503}
504
Douglas Gregor087fd532009-04-14 23:32:43 +0000505unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000506 VisitExpr(E);
507 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000509 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000510}
511
Douglas Gregor087fd532009-04-14 23:32:43 +0000512unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000513 VisitExpr(E);
514 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
515 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000516 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000517}
518
Douglas Gregor087fd532009-04-14 23:32:43 +0000519unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000520 VisitExpr(E);
521 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
522 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000523 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000524}
525
Douglas Gregor087fd532009-04-14 23:32:43 +0000526unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000527 VisitExpr(E);
528 E->setValue(Reader.ReadAPFloat(Record, Idx));
529 E->setExact(Record[Idx++]);
530 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000531 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000532}
533
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000534unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
535 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000536 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000537 return 1;
538}
539
Douglas Gregor673ecd62009-04-15 16:35:07 +0000540unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
541 VisitExpr(E);
542 unsigned Len = Record[Idx++];
543 assert(Record[Idx] == E->getNumConcatenated() &&
544 "Wrong number of concatenated tokens!");
545 ++Idx;
546 E->setWide(Record[Idx++]);
547
548 // Read string data
549 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
550 E->setStrData(Reader.getContext(), &Str[0], Len);
551 Idx += Len;
552
553 // Read source locations
554 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
555 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
556
557 return 0;
558}
559
Douglas Gregor087fd532009-04-14 23:32:43 +0000560unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000561 VisitExpr(E);
562 E->setValue(Record[Idx++]);
563 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
564 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000565 return 0;
566}
567
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000568unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
569 VisitExpr(E);
570 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
571 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000572 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000573 return 1;
574}
575
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000576unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
577 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000578 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000579 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
580 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
581 return 1;
582}
583
584unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
585 VisitExpr(E);
586 E->setSizeof(Record[Idx++]);
587 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000588 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000589 ++Idx;
590 } else {
591 E->setArgument(Reader.GetType(Record[Idx++]));
592 }
593 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
594 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
595 return E->isArgumentType()? 0 : 1;
596}
597
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000598unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
599 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000600 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
601 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000602 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
603 return 2;
604}
605
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000606unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
607 VisitExpr(E);
608 E->setNumArgs(Reader.getContext(), Record[Idx++]);
609 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000610 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000611 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000612 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000613 return E->getNumArgs() + 1;
614}
615
616unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
617 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000618 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000619 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
620 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
621 E->setArrow(Record[Idx++]);
622 return 1;
623}
624
Douglas Gregor087fd532009-04-14 23:32:43 +0000625unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
626 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000627 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000628 return 1;
629}
630
Douglas Gregordb600c32009-04-15 00:25:59 +0000631unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
632 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000633 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
634 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000635 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
636 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
637 return 2;
638}
639
Douglas Gregorad90e962009-04-15 22:40:36 +0000640unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
641 VisitBinaryOperator(E);
642 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
643 E->setComputationResultType(Reader.GetType(Record[Idx++]));
644 return 2;
645}
646
647unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
648 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000649 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
650 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
651 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000652 return 3;
653}
654
Douglas Gregor087fd532009-04-14 23:32:43 +0000655unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
656 VisitCastExpr(E);
657 E->setLvalueCast(Record[Idx++]);
658 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000659}
660
Douglas Gregordb600c32009-04-15 00:25:59 +0000661unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
662 VisitCastExpr(E);
663 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
664 return 1;
665}
666
667unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
668 VisitExplicitCastExpr(E);
669 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
670 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
671 return 1;
672}
673
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000674unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
675 VisitExpr(E);
676 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000677 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000678 E->setFileScope(Record[Idx++]);
679 return 1;
680}
681
Douglas Gregord3c98a02009-04-15 23:02:49 +0000682unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
683 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000684 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000685 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
686 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
687 return 1;
688}
689
Douglas Gregord077d752009-04-16 00:55:48 +0000690unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
691 VisitExpr(E);
692 unsigned NumInits = Record[Idx++];
693 E->reserveInits(NumInits);
694 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000695 E->updateInit(I,
696 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
697 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000698 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
699 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
700 E->setInitializedFieldInUnion(
701 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
702 E->sawArrayRangeDesignator(Record[Idx++]);
703 return NumInits + 1;
704}
705
706unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
707 typedef DesignatedInitExpr::Designator Designator;
708
709 VisitExpr(E);
710 unsigned NumSubExprs = Record[Idx++];
711 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
712 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000713 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000714 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
715 E->setGNUSyntax(Record[Idx++]);
716
717 llvm::SmallVector<Designator, 4> Designators;
718 while (Idx < Record.size()) {
719 switch ((pch::DesignatorTypes)Record[Idx++]) {
720 case pch::DESIG_FIELD_DECL: {
721 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
722 SourceLocation DotLoc
723 = SourceLocation::getFromRawEncoding(Record[Idx++]);
724 SourceLocation FieldLoc
725 = SourceLocation::getFromRawEncoding(Record[Idx++]);
726 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
727 FieldLoc));
728 Designators.back().setField(Field);
729 break;
730 }
731
732 case pch::DESIG_FIELD_NAME: {
733 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
734 SourceLocation DotLoc
735 = SourceLocation::getFromRawEncoding(Record[Idx++]);
736 SourceLocation FieldLoc
737 = SourceLocation::getFromRawEncoding(Record[Idx++]);
738 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
739 break;
740 }
741
742 case pch::DESIG_ARRAY: {
743 unsigned Index = Record[Idx++];
744 SourceLocation LBracketLoc
745 = SourceLocation::getFromRawEncoding(Record[Idx++]);
746 SourceLocation RBracketLoc
747 = SourceLocation::getFromRawEncoding(Record[Idx++]);
748 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
749 break;
750 }
751
752 case pch::DESIG_ARRAY_RANGE: {
753 unsigned Index = Record[Idx++];
754 SourceLocation LBracketLoc
755 = SourceLocation::getFromRawEncoding(Record[Idx++]);
756 SourceLocation EllipsisLoc
757 = SourceLocation::getFromRawEncoding(Record[Idx++]);
758 SourceLocation RBracketLoc
759 = SourceLocation::getFromRawEncoding(Record[Idx++]);
760 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
761 RBracketLoc));
762 break;
763 }
764 }
765 }
766 E->setDesignators(&Designators[0], Designators.size());
767
768 return NumSubExprs;
769}
770
771unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
772 VisitExpr(E);
773 return 0;
774}
775
Douglas Gregord3c98a02009-04-15 23:02:49 +0000776unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
777 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000778 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000779 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
780 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
781 return 1;
782}
783
Douglas Gregor7d5c2f22009-04-17 18:58:21 +0000784unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
785 VisitExpr(E);
786 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
787 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
788 Reader.SetLabelOf(E, Record[Idx++]);
789 return 0;
790}
791
Douglas Gregor6a2dd552009-04-17 19:05:30 +0000792unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
793 VisitExpr(E);
794 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
795 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
796 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
797 return 1;
798}
799
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000800unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
801 VisitExpr(E);
802 E->setArgType1(Reader.GetType(Record[Idx++]));
803 E->setArgType2(Reader.GetType(Record[Idx++]));
804 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
805 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
806 return 0;
807}
808
809unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
810 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000811 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
812 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
813 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000814 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
815 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
816 return 3;
817}
818
819unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
820 VisitExpr(E);
821 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
822 return 0;
823}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000824
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000825unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
826 VisitExpr(E);
827 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000828 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000829 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
830 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
831 return NumExprs;
832}
833
Douglas Gregor84af7c22009-04-17 19:21:43 +0000834unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
835 VisitExpr(E);
836 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
837 E->setHasBlockDeclRefExprs(Record[Idx++]);
838 return 0;
839}
840
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000841unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
842 VisitExpr(E);
843 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
844 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
845 E->setByRef(Record[Idx++]);
846 return 0;
847}
848
Douglas Gregor2cf26342009-04-09 22:27:44 +0000849// FIXME: use the diagnostics machinery
850static bool Error(const char *Str) {
851 std::fprintf(stderr, "%s\n", Str);
852 return true;
853}
854
Douglas Gregore1d918e2009-04-10 23:10:45 +0000855/// \brief Check the contents of the predefines buffer against the
856/// contents of the predefines buffer used to build the PCH file.
857///
858/// The contents of the two predefines buffers should be the same. If
859/// not, then some command-line option changed the preprocessor state
860/// and we must reject the PCH file.
861///
862/// \param PCHPredef The start of the predefines buffer in the PCH
863/// file.
864///
865/// \param PCHPredefLen The length of the predefines buffer in the PCH
866/// file.
867///
868/// \param PCHBufferID The FileID for the PCH predefines buffer.
869///
870/// \returns true if there was a mismatch (in which case the PCH file
871/// should be ignored), or false otherwise.
872bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
873 unsigned PCHPredefLen,
874 FileID PCHBufferID) {
875 const char *Predef = PP.getPredefines().c_str();
876 unsigned PredefLen = PP.getPredefines().size();
877
878 // If the two predefines buffers compare equal, we're done!.
879 if (PredefLen == PCHPredefLen &&
880 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
881 return false;
882
883 // The predefines buffers are different. Produce a reasonable
884 // diagnostic showing where they are different.
885
886 // The source locations (potentially in the two different predefines
887 // buffers)
888 SourceLocation Loc1, Loc2;
889 SourceManager &SourceMgr = PP.getSourceManager();
890
891 // Create a source buffer for our predefines string, so
892 // that we can build a diagnostic that points into that
893 // source buffer.
894 FileID BufferID;
895 if (Predef && Predef[0]) {
896 llvm::MemoryBuffer *Buffer
897 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
898 "<built-in>");
899 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
900 }
901
902 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
903 std::pair<const char *, const char *> Locations
904 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
905
906 if (Locations.first != Predef + MinLen) {
907 // We found the location in the two buffers where there is a
908 // difference. Form source locations to point there (in both
909 // buffers).
910 unsigned Offset = Locations.first - Predef;
911 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
912 .getFileLocWithOffset(Offset);
913 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
914 .getFileLocWithOffset(Offset);
915 } else if (PredefLen > PCHPredefLen) {
916 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
917 .getFileLocWithOffset(MinLen);
918 } else {
919 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
920 .getFileLocWithOffset(MinLen);
921 }
922
923 Diag(Loc1, diag::warn_pch_preprocessor);
924 if (Loc2.isValid())
925 Diag(Loc2, diag::note_predef_in_pch);
926 Diag(diag::note_ignoring_pch) << FileName;
927 return true;
928}
929
Douglas Gregorbd945002009-04-13 16:31:14 +0000930/// \brief Read the line table in the source manager block.
931/// \returns true if ther was an error.
932static bool ParseLineTable(SourceManager &SourceMgr,
933 llvm::SmallVectorImpl<uint64_t> &Record) {
934 unsigned Idx = 0;
935 LineTableInfo &LineTable = SourceMgr.getLineTable();
936
937 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000938 std::map<int, int> FileIDs;
939 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000940 // Extract the file name
941 unsigned FilenameLen = Record[Idx++];
942 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
943 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000944 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
945 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000946 }
947
948 // Parse the line entries
949 std::vector<LineEntry> Entries;
950 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000951 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000952
953 // Extract the line entries
954 unsigned NumEntries = Record[Idx++];
955 Entries.clear();
956 Entries.reserve(NumEntries);
957 for (unsigned I = 0; I != NumEntries; ++I) {
958 unsigned FileOffset = Record[Idx++];
959 unsigned LineNo = Record[Idx++];
960 int FilenameID = Record[Idx++];
961 SrcMgr::CharacteristicKind FileKind
962 = (SrcMgr::CharacteristicKind)Record[Idx++];
963 unsigned IncludeOffset = Record[Idx++];
964 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
965 FileKind, IncludeOffset));
966 }
967 LineTable.AddEntry(FID, Entries);
968 }
969
970 return false;
971}
972
Douglas Gregor14f79002009-04-10 03:52:48 +0000973/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000974PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000976 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
977 Error("Malformed source manager block record");
978 return Failure;
979 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000980
981 SourceManager &SourceMgr = Context.getSourceManager();
982 RecordData Record;
983 while (true) {
984 unsigned Code = Stream.ReadCode();
985 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000986 if (Stream.ReadBlockEnd()) {
987 Error("Error at end of Source Manager block");
988 return Failure;
989 }
990
991 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 }
993
994 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
995 // No known subblocks, always skip them.
996 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000997 if (Stream.SkipBlock()) {
998 Error("Malformed block record");
999 return Failure;
1000 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001001 continue;
1002 }
1003
1004 if (Code == llvm::bitc::DEFINE_ABBREV) {
1005 Stream.ReadAbbrevRecord();
1006 continue;
1007 }
1008
1009 // Read a record.
1010 const char *BlobStart;
1011 unsigned BlobLen;
1012 Record.clear();
1013 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1014 default: // Default behavior: ignore.
1015 break;
1016
1017 case pch::SM_SLOC_FILE_ENTRY: {
1018 // FIXME: We would really like to delay the creation of this
1019 // FileEntry until it is actually required, e.g., when producing
1020 // a diagnostic with a source location in this file.
1021 const FileEntry *File
1022 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1023 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +00001024 FileID ID = SourceMgr.createFileID(File,
1025 SourceLocation::getFromRawEncoding(Record[1]),
1026 (CharacteristicKind)Record[2]);
1027 if (Record[3])
1028 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1029 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +00001030 break;
1031 }
1032
1033 case pch::SM_SLOC_BUFFER_ENTRY: {
1034 const char *Name = BlobStart;
1035 unsigned Code = Stream.ReadCode();
1036 Record.clear();
1037 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1038 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001039 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001040 llvm::MemoryBuffer *Buffer
1041 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1042 BlobStart + BlobLen - 1,
1043 Name);
1044 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1045
1046 if (strcmp(Name, "<built-in>") == 0
1047 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1048 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +00001049 break;
1050 }
1051
1052 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1053 SourceLocation SpellingLoc
1054 = SourceLocation::getFromRawEncoding(Record[1]);
1055 SourceMgr.createInstantiationLoc(
1056 SpellingLoc,
1057 SourceLocation::getFromRawEncoding(Record[2]),
1058 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +00001059 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +00001060 break;
1061 }
1062
Chris Lattner2c78b872009-04-14 23:22:57 +00001063 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +00001064 if (ParseLineTable(SourceMgr, Record))
1065 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +00001066 break;
Douglas Gregor14f79002009-04-10 03:52:48 +00001067 }
1068 }
1069}
1070
Chris Lattner42d42b52009-04-10 21:41:48 +00001071bool PCHReader::ReadPreprocessorBlock() {
1072 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1073 return Error("Malformed preprocessor block record");
1074
Chris Lattner42d42b52009-04-10 21:41:48 +00001075 RecordData Record;
1076 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1077 MacroInfo *LastMacro = 0;
1078
1079 while (true) {
1080 unsigned Code = Stream.ReadCode();
1081 switch (Code) {
1082 case llvm::bitc::END_BLOCK:
1083 if (Stream.ReadBlockEnd())
1084 return Error("Error at end of preprocessor block");
1085 return false;
1086
1087 case llvm::bitc::ENTER_SUBBLOCK:
1088 // No known subblocks, always skip them.
1089 Stream.ReadSubBlockID();
1090 if (Stream.SkipBlock())
1091 return Error("Malformed block record");
1092 continue;
1093
1094 case llvm::bitc::DEFINE_ABBREV:
1095 Stream.ReadAbbrevRecord();
1096 continue;
1097 default: break;
1098 }
1099
1100 // Read a record.
1101 Record.clear();
1102 pch::PreprocessorRecordTypes RecType =
1103 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1104 switch (RecType) {
1105 default: // Default behavior: ignore unknown records.
1106 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001107 case pch::PP_COUNTER_VALUE:
1108 if (!Record.empty())
1109 PP.setCounterValue(Record[0]);
1110 break;
1111
Chris Lattner42d42b52009-04-10 21:41:48 +00001112 case pch::PP_MACRO_OBJECT_LIKE:
1113 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +00001114 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1115 if (II == 0)
1116 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +00001117 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1118 bool isUsed = Record[2];
1119
1120 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1121 MI->setIsUsed(isUsed);
1122
1123 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1124 // Decode function-like macro info.
1125 bool isC99VarArgs = Record[3];
1126 bool isGNUVarArgs = Record[4];
1127 MacroArgs.clear();
1128 unsigned NumArgs = Record[5];
1129 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +00001130 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +00001131
1132 // Install function-like macro info.
1133 MI->setIsFunctionLike();
1134 if (isC99VarArgs) MI->setIsC99Varargs();
1135 if (isGNUVarArgs) MI->setIsGNUVarargs();
1136 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1137 PP.getPreprocessorAllocator());
1138 }
1139
1140 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +00001141 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +00001142
1143 // Remember that we saw this macro last so that we add the tokens that
1144 // form its body to it.
1145 LastMacro = MI;
1146 break;
1147 }
1148
1149 case pch::PP_TOKEN: {
1150 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1151 // pretend we didn't see this.
1152 if (LastMacro == 0) break;
1153
1154 Token Tok;
1155 Tok.startToken();
1156 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1157 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001158 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1159 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001160 Tok.setKind((tok::TokenKind)Record[3]);
1161 Tok.setFlag((Token::TokenFlags)Record[4]);
1162 LastMacro->AddTokenToBody(Tok);
1163 break;
1164 }
1165 }
1166 }
1167}
1168
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001169PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1170 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1171 Error("Malformed block record");
1172 return Failure;
1173 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001174
Chris Lattner7356a312009-04-11 21:15:38 +00001175 uint64_t PreprocessorBlockBit = 0;
1176
Douglas Gregor2cf26342009-04-09 22:27:44 +00001177 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001178 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001179 while (!Stream.AtEndOfStream()) {
1180 unsigned Code = Stream.ReadCode();
1181 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001182 // If we saw the preprocessor block, read it now.
1183 if (PreprocessorBlockBit) {
1184 uint64_t SavedPos = Stream.GetCurrentBitNo();
1185 Stream.JumpToBit(PreprocessorBlockBit);
1186 if (ReadPreprocessorBlock()) {
1187 Error("Malformed preprocessor block");
1188 return Failure;
1189 }
1190 Stream.JumpToBit(SavedPos);
1191 }
1192
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001193 if (Stream.ReadBlockEnd()) {
1194 Error("Error at end of module block");
1195 return Failure;
1196 }
Chris Lattner7356a312009-04-11 21:15:38 +00001197
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001198 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001199 }
1200
1201 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1202 switch (Stream.ReadSubBlockID()) {
1203 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1204 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1205 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001206 if (Stream.SkipBlock()) {
1207 Error("Malformed block record");
1208 return Failure;
1209 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001210 break;
1211
Chris Lattner7356a312009-04-11 21:15:38 +00001212 case pch::PREPROCESSOR_BLOCK_ID:
1213 // Skip the preprocessor block for now, but remember where it is. We
1214 // want to read it in after the identifier table.
1215 if (PreprocessorBlockBit) {
1216 Error("Multiple preprocessor blocks found.");
1217 return Failure;
1218 }
1219 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1220 if (Stream.SkipBlock()) {
1221 Error("Malformed block record");
1222 return Failure;
1223 }
1224 break;
1225
Douglas Gregor14f79002009-04-10 03:52:48 +00001226 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001227 switch (ReadSourceManagerBlock()) {
1228 case Success:
1229 break;
1230
1231 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001232 Error("Malformed source manager block");
1233 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001234
1235 case IgnorePCH:
1236 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001237 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001238 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001239 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001240 continue;
1241 }
1242
1243 if (Code == llvm::bitc::DEFINE_ABBREV) {
1244 Stream.ReadAbbrevRecord();
1245 continue;
1246 }
1247
1248 // Read and process a record.
1249 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001250 const char *BlobStart = 0;
1251 unsigned BlobLen = 0;
1252 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1253 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001254 default: // Default behavior: ignore.
1255 break;
1256
1257 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001258 if (!TypeOffsets.empty()) {
1259 Error("Duplicate TYPE_OFFSET record in PCH file");
1260 return Failure;
1261 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001262 TypeOffsets.swap(Record);
1263 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1264 break;
1265
1266 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001267 if (!DeclOffsets.empty()) {
1268 Error("Duplicate DECL_OFFSET record in PCH file");
1269 return Failure;
1270 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001271 DeclOffsets.swap(Record);
1272 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1273 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001274
1275 case pch::LANGUAGE_OPTIONS:
1276 if (ParseLanguageOptions(Record))
1277 return IgnorePCH;
1278 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001279
Douglas Gregorafaf3082009-04-11 00:14:32 +00001280 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001281 std::string TargetTriple(BlobStart, BlobLen);
1282 if (TargetTriple != Context.Target.getTargetTriple()) {
1283 Diag(diag::warn_pch_target_triple)
1284 << TargetTriple << Context.Target.getTargetTriple();
1285 Diag(diag::note_ignoring_pch) << FileName;
1286 return IgnorePCH;
1287 }
1288 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001289 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001290
1291 case pch::IDENTIFIER_TABLE:
1292 IdentifierTable = BlobStart;
1293 break;
1294
1295 case pch::IDENTIFIER_OFFSET:
1296 if (!IdentifierData.empty()) {
1297 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1298 return Failure;
1299 }
1300 IdentifierData.swap(Record);
1301#ifndef NDEBUG
1302 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1303 if ((IdentifierData[I] & 0x01) == 0) {
1304 Error("Malformed identifier table in the precompiled header");
1305 return Failure;
1306 }
1307 }
1308#endif
1309 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001310
1311 case pch::EXTERNAL_DEFINITIONS:
1312 if (!ExternalDefinitions.empty()) {
1313 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1314 return Failure;
1315 }
1316 ExternalDefinitions.swap(Record);
1317 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001318 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001319 }
1320
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001321 Error("Premature end of bitstream");
1322 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001323}
1324
Douglas Gregore1d918e2009-04-10 23:10:45 +00001325PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001326 // Set the PCH file name.
1327 this->FileName = FileName;
1328
Douglas Gregor2cf26342009-04-09 22:27:44 +00001329 // Open the PCH file.
1330 std::string ErrStr;
1331 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001332 if (!Buffer) {
1333 Error(ErrStr.c_str());
1334 return IgnorePCH;
1335 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001336
1337 // Initialize the stream
1338 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1339 (const unsigned char *)Buffer->getBufferEnd());
1340
1341 // Sniff for the signature.
1342 if (Stream.Read(8) != 'C' ||
1343 Stream.Read(8) != 'P' ||
1344 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001345 Stream.Read(8) != 'H') {
1346 Error("Not a PCH file");
1347 return IgnorePCH;
1348 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001349
1350 // We expect a number of well-defined blocks, though we don't necessarily
1351 // need to understand them all.
1352 while (!Stream.AtEndOfStream()) {
1353 unsigned Code = Stream.ReadCode();
1354
Douglas Gregore1d918e2009-04-10 23:10:45 +00001355 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1356 Error("Invalid record at top-level");
1357 return Failure;
1358 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001359
1360 unsigned BlockID = Stream.ReadSubBlockID();
1361
1362 // We only know the PCH subblock ID.
1363 switch (BlockID) {
1364 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001365 if (Stream.ReadBlockInfoBlock()) {
1366 Error("Malformed BlockInfoBlock");
1367 return Failure;
1368 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001369 break;
1370 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001371 switch (ReadPCHBlock()) {
1372 case Success:
1373 break;
1374
1375 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001376 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001377
1378 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001379 // FIXME: We could consider reading through to the end of this
1380 // PCH block, skipping subblocks, to see if there are other
1381 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001382 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001383 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001384 break;
1385 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001386 if (Stream.SkipBlock()) {
1387 Error("Malformed block record");
1388 return Failure;
1389 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001390 break;
1391 }
1392 }
1393
1394 // Load the translation unit declaration
1395 ReadDeclRecord(DeclOffsets[0], 0);
1396
Douglas Gregore1d918e2009-04-10 23:10:45 +00001397 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001398}
1399
Douglas Gregor0b748912009-04-14 21:18:50 +00001400namespace {
1401 /// \brief Helper class that saves the current stream position and
1402 /// then restores it when destroyed.
1403 struct VISIBILITY_HIDDEN SavedStreamPosition {
1404 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001405 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001406
1407 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001408 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001409 }
1410
1411 private:
1412 llvm::BitstreamReader &Stream;
1413 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001414 };
1415}
1416
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001417/// \brief Parse the record that corresponds to a LangOptions data
1418/// structure.
1419///
1420/// This routine compares the language options used to generate the
1421/// PCH file against the language options set for the current
1422/// compilation. For each option, we classify differences between the
1423/// two compiler states as either "benign" or "important". Benign
1424/// differences don't matter, and we accept them without complaint
1425/// (and without modifying the language options). Differences between
1426/// the states for important options cause the PCH file to be
1427/// unusable, so we emit a warning and return true to indicate that
1428/// there was an error.
1429///
1430/// \returns true if the PCH file is unacceptable, false otherwise.
1431bool PCHReader::ParseLanguageOptions(
1432 const llvm::SmallVectorImpl<uint64_t> &Record) {
1433 const LangOptions &LangOpts = Context.getLangOptions();
1434#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1435#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1436 if (Record[Idx] != LangOpts.Option) { \
1437 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1438 Diag(diag::note_ignoring_pch) << FileName; \
1439 return true; \
1440 } \
1441 ++Idx
1442
1443 unsigned Idx = 0;
1444 PARSE_LANGOPT_BENIGN(Trigraphs);
1445 PARSE_LANGOPT_BENIGN(BCPLComment);
1446 PARSE_LANGOPT_BENIGN(DollarIdents);
1447 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1448 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1449 PARSE_LANGOPT_BENIGN(ImplicitInt);
1450 PARSE_LANGOPT_BENIGN(Digraphs);
1451 PARSE_LANGOPT_BENIGN(HexFloats);
1452 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1453 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1454 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1455 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1456 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1457 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1458 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1459 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1460 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1461 PARSE_LANGOPT_BENIGN(PascalStrings);
1462 PARSE_LANGOPT_BENIGN(Boolean);
1463 PARSE_LANGOPT_BENIGN(WritableStrings);
1464 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1465 diag::warn_pch_lax_vector_conversions);
1466 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1467 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1468 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1469 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1470 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1471 diag::warn_pch_thread_safe_statics);
1472 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1473 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1474 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1475 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1476 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1477 diag::warn_pch_heinous_extensions);
1478 // FIXME: Most of the options below are benign if the macro wasn't
1479 // used. Unfortunately, this means that a PCH compiled without
1480 // optimization can't be used with optimization turned on, even
1481 // though the only thing that changes is whether __OPTIMIZE__ was
1482 // defined... but if __OPTIMIZE__ never showed up in the header, it
1483 // doesn't matter. We could consider making this some special kind
1484 // of check.
1485 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1486 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1487 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1488 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1489 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1490 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1491 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1492 Diag(diag::warn_pch_gc_mode)
1493 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1494 Diag(diag::note_ignoring_pch) << FileName;
1495 return true;
1496 }
1497 ++Idx;
1498 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1499 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1500#undef PARSE_LANGOPT_IRRELEVANT
1501#undef PARSE_LANGOPT_BENIGN
1502
1503 return false;
1504}
1505
Douglas Gregor2cf26342009-04-09 22:27:44 +00001506/// \brief Read and return the type at the given offset.
1507///
1508/// This routine actually reads the record corresponding to the type
1509/// at the given offset in the bitstream. It is a helper routine for
1510/// GetType, which deals with reading type IDs.
1511QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001512 // Keep track of where we are in the stream, then jump back there
1513 // after reading this type.
1514 SavedStreamPosition SavedPosition(Stream);
1515
Douglas Gregor2cf26342009-04-09 22:27:44 +00001516 Stream.JumpToBit(Offset);
1517 RecordData Record;
1518 unsigned Code = Stream.ReadCode();
1519 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001520 case pch::TYPE_EXT_QUAL: {
1521 assert(Record.size() == 3 &&
1522 "Incorrect encoding of extended qualifier type");
1523 QualType Base = GetType(Record[0]);
1524 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1525 unsigned AddressSpace = Record[2];
1526
1527 QualType T = Base;
1528 if (GCAttr != QualType::GCNone)
1529 T = Context.getObjCGCQualType(T, GCAttr);
1530 if (AddressSpace)
1531 T = Context.getAddrSpaceQualType(T, AddressSpace);
1532 return T;
1533 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001534
Douglas Gregor2cf26342009-04-09 22:27:44 +00001535 case pch::TYPE_FIXED_WIDTH_INT: {
1536 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1537 return Context.getFixedWidthIntType(Record[0], Record[1]);
1538 }
1539
1540 case pch::TYPE_COMPLEX: {
1541 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1542 QualType ElemType = GetType(Record[0]);
1543 return Context.getComplexType(ElemType);
1544 }
1545
1546 case pch::TYPE_POINTER: {
1547 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1548 QualType PointeeType = GetType(Record[0]);
1549 return Context.getPointerType(PointeeType);
1550 }
1551
1552 case pch::TYPE_BLOCK_POINTER: {
1553 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1554 QualType PointeeType = GetType(Record[0]);
1555 return Context.getBlockPointerType(PointeeType);
1556 }
1557
1558 case pch::TYPE_LVALUE_REFERENCE: {
1559 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1560 QualType PointeeType = GetType(Record[0]);
1561 return Context.getLValueReferenceType(PointeeType);
1562 }
1563
1564 case pch::TYPE_RVALUE_REFERENCE: {
1565 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1566 QualType PointeeType = GetType(Record[0]);
1567 return Context.getRValueReferenceType(PointeeType);
1568 }
1569
1570 case pch::TYPE_MEMBER_POINTER: {
1571 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1572 QualType PointeeType = GetType(Record[0]);
1573 QualType ClassType = GetType(Record[1]);
1574 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1575 }
1576
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001577 case pch::TYPE_CONSTANT_ARRAY: {
1578 QualType ElementType = GetType(Record[0]);
1579 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1580 unsigned IndexTypeQuals = Record[2];
1581 unsigned Idx = 3;
1582 llvm::APInt Size = ReadAPInt(Record, Idx);
1583 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1584 }
1585
1586 case pch::TYPE_INCOMPLETE_ARRAY: {
1587 QualType ElementType = GetType(Record[0]);
1588 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1589 unsigned IndexTypeQuals = Record[2];
1590 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1591 }
1592
1593 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001594 QualType ElementType = GetType(Record[0]);
1595 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1596 unsigned IndexTypeQuals = Record[2];
1597 return Context.getVariableArrayType(ElementType, ReadExpr(),
1598 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001599 }
1600
1601 case pch::TYPE_VECTOR: {
1602 if (Record.size() != 2) {
1603 Error("Incorrect encoding of vector type in PCH file");
1604 return QualType();
1605 }
1606
1607 QualType ElementType = GetType(Record[0]);
1608 unsigned NumElements = Record[1];
1609 return Context.getVectorType(ElementType, NumElements);
1610 }
1611
1612 case pch::TYPE_EXT_VECTOR: {
1613 if (Record.size() != 2) {
1614 Error("Incorrect encoding of extended vector type in PCH file");
1615 return QualType();
1616 }
1617
1618 QualType ElementType = GetType(Record[0]);
1619 unsigned NumElements = Record[1];
1620 return Context.getExtVectorType(ElementType, NumElements);
1621 }
1622
1623 case pch::TYPE_FUNCTION_NO_PROTO: {
1624 if (Record.size() != 1) {
1625 Error("Incorrect encoding of no-proto function type");
1626 return QualType();
1627 }
1628 QualType ResultType = GetType(Record[0]);
1629 return Context.getFunctionNoProtoType(ResultType);
1630 }
1631
1632 case pch::TYPE_FUNCTION_PROTO: {
1633 QualType ResultType = GetType(Record[0]);
1634 unsigned Idx = 1;
1635 unsigned NumParams = Record[Idx++];
1636 llvm::SmallVector<QualType, 16> ParamTypes;
1637 for (unsigned I = 0; I != NumParams; ++I)
1638 ParamTypes.push_back(GetType(Record[Idx++]));
1639 bool isVariadic = Record[Idx++];
1640 unsigned Quals = Record[Idx++];
1641 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1642 isVariadic, Quals);
1643 }
1644
1645 case pch::TYPE_TYPEDEF:
1646 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1647 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1648
1649 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001650 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001651
1652 case pch::TYPE_TYPEOF: {
1653 if (Record.size() != 1) {
1654 Error("Incorrect encoding of typeof(type) in PCH file");
1655 return QualType();
1656 }
1657 QualType UnderlyingType = GetType(Record[0]);
1658 return Context.getTypeOfType(UnderlyingType);
1659 }
1660
1661 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001662 assert(Record.size() == 1 && "Incorrect encoding of record type");
1663 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001664
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001665 case pch::TYPE_ENUM:
1666 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1667 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1668
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001669 case pch::TYPE_OBJC_INTERFACE:
1670 // FIXME: Deserialize ObjCInterfaceType
1671 assert(false && "Cannot de-serialize ObjC interface types yet");
1672 return QualType();
1673
1674 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1675 // FIXME: Deserialize ObjCQualifiedInterfaceType
1676 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1677 return QualType();
1678
1679 case pch::TYPE_OBJC_QUALIFIED_ID:
1680 // FIXME: Deserialize ObjCQualifiedIdType
1681 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1682 return QualType();
1683
1684 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1685 // FIXME: Deserialize ObjCQualifiedClassType
1686 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1687 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001688 }
1689
1690 // Suppress a GCC warning
1691 return QualType();
1692}
1693
1694/// \brief Note that we have loaded the declaration with the given
1695/// Index.
1696///
1697/// This routine notes that this declaration has already been loaded,
1698/// so that future GetDecl calls will return this declaration rather
1699/// than trying to load a new declaration.
1700inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1701 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1702 DeclAlreadyLoaded[Index] = true;
1703 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1704}
1705
1706/// \brief Read the declaration at the given offset from the PCH file.
1707Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001708 // Keep track of where we are in the stream, then jump back there
1709 // after reading this declaration.
1710 SavedStreamPosition SavedPosition(Stream);
1711
Douglas Gregor2cf26342009-04-09 22:27:44 +00001712 Decl *D = 0;
1713 Stream.JumpToBit(Offset);
1714 RecordData Record;
1715 unsigned Code = Stream.ReadCode();
1716 unsigned Idx = 0;
1717 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001718
Douglas Gregor2cf26342009-04-09 22:27:44 +00001719 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001720 case pch::DECL_ATTR:
1721 case pch::DECL_CONTEXT_LEXICAL:
1722 case pch::DECL_CONTEXT_VISIBLE:
1723 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1724 break;
1725
Douglas Gregor2cf26342009-04-09 22:27:44 +00001726 case pch::DECL_TRANSLATION_UNIT:
1727 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001728 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001729 break;
1730
1731 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001732 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001733 break;
1734 }
1735
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001736 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001737 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001738 break;
1739 }
1740
Douglas Gregor8c700062009-04-13 21:20:57 +00001741 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001742 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1743 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001744 break;
1745 }
1746
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001747 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001748 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1749 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001750 break;
1751 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001752
1753 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001754 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1755 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001756 break;
1757 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001758
Douglas Gregor8c700062009-04-13 21:20:57 +00001759 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001760 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1761 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001762 break;
1763 }
1764
Douglas Gregor2cf26342009-04-09 22:27:44 +00001765 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001766 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1767 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 break;
1769 }
1770
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001771 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001772 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1773 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001774 break;
1775 }
1776
1777 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001778 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001779 QualType(), QualType(), VarDecl::None,
1780 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001781 break;
1782 }
1783
Douglas Gregor1028bc62009-04-13 22:49:25 +00001784 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001785 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001786 break;
1787 }
1788
1789 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001790 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001791 break;
1792 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001793 }
1794
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001795 assert(D && "Unknown declaration creating PCH file");
1796 if (D) {
1797 LoadedDecl(Index, D);
1798 Reader.Visit(D);
1799 }
1800
Douglas Gregor2cf26342009-04-09 22:27:44 +00001801 // If this declaration is also a declaration context, get the
1802 // offsets for its tables of lexical and visible declarations.
1803 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1804 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1805 if (Offsets.first || Offsets.second) {
1806 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1807 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1808 DeclContextOffsets[DC] = Offsets;
1809 }
1810 }
1811 assert(Idx == Record.size());
1812
1813 return D;
1814}
1815
Douglas Gregor8038d512009-04-10 17:25:41 +00001816QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001817 unsigned Quals = ID & 0x07;
1818 unsigned Index = ID >> 3;
1819
1820 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1821 QualType T;
1822 switch ((pch::PredefinedTypeIDs)Index) {
1823 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1824 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1825 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1826
1827 case pch::PREDEF_TYPE_CHAR_U_ID:
1828 case pch::PREDEF_TYPE_CHAR_S_ID:
1829 // FIXME: Check that the signedness of CharTy is correct!
1830 T = Context.CharTy;
1831 break;
1832
1833 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1834 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1835 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1836 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1837 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1838 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1839 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1840 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1841 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1842 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1843 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1844 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1845 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1846 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1847 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1848 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1849 }
1850
1851 assert(!T.isNull() && "Unknown predefined type");
1852 return T.getQualifiedType(Quals);
1853 }
1854
1855 Index -= pch::NUM_PREDEF_TYPE_IDS;
1856 if (!TypeAlreadyLoaded[Index]) {
1857 // Load the type from the PCH file.
1858 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1859 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1860 TypeAlreadyLoaded[Index] = true;
1861 }
1862
1863 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1864}
1865
Douglas Gregor8038d512009-04-10 17:25:41 +00001866Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001867 if (ID == 0)
1868 return 0;
1869
1870 unsigned Index = ID - 1;
1871 if (DeclAlreadyLoaded[Index])
1872 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1873
1874 // Load the declaration from the PCH file.
1875 return ReadDeclRecord(DeclOffsets[Index], Index);
1876}
1877
1878bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001879 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001880 assert(DC->hasExternalLexicalStorage() &&
1881 "DeclContext has no lexical decls in storage");
1882 uint64_t Offset = DeclContextOffsets[DC].first;
1883 assert(Offset && "DeclContext has no lexical decls in storage");
1884
Douglas Gregor0b748912009-04-14 21:18:50 +00001885 // Keep track of where we are in the stream, then jump back there
1886 // after reading this context.
1887 SavedStreamPosition SavedPosition(Stream);
1888
Douglas Gregor2cf26342009-04-09 22:27:44 +00001889 // Load the record containing all of the declarations lexically in
1890 // this context.
1891 Stream.JumpToBit(Offset);
1892 RecordData Record;
1893 unsigned Code = Stream.ReadCode();
1894 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001895 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001896 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1897
1898 // Load all of the declaration IDs
1899 Decls.clear();
1900 Decls.insert(Decls.end(), Record.begin(), Record.end());
1901 return false;
1902}
1903
1904bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1905 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1906 assert(DC->hasExternalVisibleStorage() &&
1907 "DeclContext has no visible decls in storage");
1908 uint64_t Offset = DeclContextOffsets[DC].second;
1909 assert(Offset && "DeclContext has no visible decls in storage");
1910
Douglas Gregor0b748912009-04-14 21:18:50 +00001911 // Keep track of where we are in the stream, then jump back there
1912 // after reading this context.
1913 SavedStreamPosition SavedPosition(Stream);
1914
Douglas Gregor2cf26342009-04-09 22:27:44 +00001915 // Load the record containing all of the declarations visible in
1916 // this context.
1917 Stream.JumpToBit(Offset);
1918 RecordData Record;
1919 unsigned Code = Stream.ReadCode();
1920 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001921 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001922 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1923 if (Record.size() == 0)
1924 return false;
1925
1926 Decls.clear();
1927
1928 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001929 while (Idx < Record.size()) {
1930 Decls.push_back(VisibleDeclaration());
1931 Decls.back().Name = ReadDeclarationName(Record, Idx);
1932
Douglas Gregor2cf26342009-04-09 22:27:44 +00001933 unsigned Size = Record[Idx++];
1934 llvm::SmallVector<unsigned, 4> & LoadedDecls
1935 = Decls.back().Declarations;
1936 LoadedDecls.reserve(Size);
1937 for (unsigned I = 0; I < Size; ++I)
1938 LoadedDecls.push_back(Record[Idx++]);
1939 }
1940
1941 return false;
1942}
1943
Douglas Gregorfdd01722009-04-14 00:24:19 +00001944void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1945 if (!Consumer)
1946 return;
1947
1948 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1949 Decl *D = GetDecl(ExternalDefinitions[I]);
1950 DeclGroupRef DG(D);
1951 Consumer->HandleTopLevelDecl(DG);
1952 }
1953}
1954
Douglas Gregor2cf26342009-04-09 22:27:44 +00001955void PCHReader::PrintStats() {
1956 std::fprintf(stderr, "*** PCH Statistics:\n");
1957
1958 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1959 TypeAlreadyLoaded.end(),
1960 true);
1961 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1962 DeclAlreadyLoaded.end(),
1963 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001964 unsigned NumIdentifiersLoaded = 0;
1965 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1966 if ((IdentifierData[I] & 0x01) == 0)
1967 ++NumIdentifiersLoaded;
1968 }
1969
Douglas Gregor2cf26342009-04-09 22:27:44 +00001970 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1971 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001972 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001973 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1974 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001975 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1976 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1977 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1978 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001979 std::fprintf(stderr, "\n");
1980}
1981
Chris Lattner7356a312009-04-11 21:15:38 +00001982IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001983 if (ID == 0)
1984 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001985
Douglas Gregorafaf3082009-04-11 00:14:32 +00001986 if (!IdentifierTable || IdentifierData.empty()) {
1987 Error("No identifier table in PCH file");
1988 return 0;
1989 }
Chris Lattner7356a312009-04-11 21:15:38 +00001990
Douglas Gregorafaf3082009-04-11 00:14:32 +00001991 if (IdentifierData[ID - 1] & 0x01) {
1992 uint64_t Offset = IdentifierData[ID - 1];
1993 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001994 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001995 }
Chris Lattner7356a312009-04-11 21:15:38 +00001996
1997 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001998}
1999
2000DeclarationName
2001PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2002 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2003 switch (Kind) {
2004 case DeclarationName::Identifier:
2005 return DeclarationName(GetIdentifierInfo(Record, Idx));
2006
2007 case DeclarationName::ObjCZeroArgSelector:
2008 case DeclarationName::ObjCOneArgSelector:
2009 case DeclarationName::ObjCMultiArgSelector:
2010 assert(false && "Unable to de-serialize Objective-C selectors");
2011 break;
2012
2013 case DeclarationName::CXXConstructorName:
2014 return Context.DeclarationNames.getCXXConstructorName(
2015 GetType(Record[Idx++]));
2016
2017 case DeclarationName::CXXDestructorName:
2018 return Context.DeclarationNames.getCXXDestructorName(
2019 GetType(Record[Idx++]));
2020
2021 case DeclarationName::CXXConversionFunctionName:
2022 return Context.DeclarationNames.getCXXConversionFunctionName(
2023 GetType(Record[Idx++]));
2024
2025 case DeclarationName::CXXOperatorName:
2026 return Context.DeclarationNames.getCXXOperatorName(
2027 (OverloadedOperatorKind)Record[Idx++]);
2028
2029 case DeclarationName::CXXUsingDirective:
2030 return DeclarationName::getUsingDirectiveName();
2031 }
2032
2033 // Required to silence GCC warning
2034 return DeclarationName();
2035}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002036
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002037/// \brief Read an integral value
2038llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2039 unsigned BitWidth = Record[Idx++];
2040 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2041 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2042 Idx += NumWords;
2043 return Result;
2044}
2045
2046/// \brief Read a signed integral value
2047llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2048 bool isUnsigned = Record[Idx++];
2049 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2050}
2051
Douglas Gregor17fc2232009-04-14 21:55:33 +00002052/// \brief Read a floating-point value
2053llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002054 return llvm::APFloat(ReadAPInt(Record, Idx));
2055}
2056
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002057// \brief Read a string
2058std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2059 unsigned Len = Record[Idx++];
2060 std::string Result(&Record[Idx], &Record[Idx] + Len);
2061 Idx += Len;
2062 return Result;
2063}
2064
2065/// \brief Reads attributes from the current stream position.
2066Attr *PCHReader::ReadAttributes() {
2067 unsigned Code = Stream.ReadCode();
2068 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2069 "Expected unabbreviated record"); (void)Code;
2070
2071 RecordData Record;
2072 unsigned Idx = 0;
2073 unsigned RecCode = Stream.ReadRecord(Code, Record);
2074 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2075 (void)RecCode;
2076
2077#define SIMPLE_ATTR(Name) \
2078 case Attr::Name: \
2079 New = ::new (Context) Name##Attr(); \
2080 break
2081
2082#define STRING_ATTR(Name) \
2083 case Attr::Name: \
2084 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2085 break
2086
2087#define UNSIGNED_ATTR(Name) \
2088 case Attr::Name: \
2089 New = ::new (Context) Name##Attr(Record[Idx++]); \
2090 break
2091
2092 Attr *Attrs = 0;
2093 while (Idx < Record.size()) {
2094 Attr *New = 0;
2095 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2096 bool IsInherited = Record[Idx++];
2097
2098 switch (Kind) {
2099 STRING_ATTR(Alias);
2100 UNSIGNED_ATTR(Aligned);
2101 SIMPLE_ATTR(AlwaysInline);
2102 SIMPLE_ATTR(AnalyzerNoReturn);
2103 STRING_ATTR(Annotate);
2104 STRING_ATTR(AsmLabel);
2105
2106 case Attr::Blocks:
2107 New = ::new (Context) BlocksAttr(
2108 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2109 break;
2110
2111 case Attr::Cleanup:
2112 New = ::new (Context) CleanupAttr(
2113 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2114 break;
2115
2116 SIMPLE_ATTR(Const);
2117 UNSIGNED_ATTR(Constructor);
2118 SIMPLE_ATTR(DLLExport);
2119 SIMPLE_ATTR(DLLImport);
2120 SIMPLE_ATTR(Deprecated);
2121 UNSIGNED_ATTR(Destructor);
2122 SIMPLE_ATTR(FastCall);
2123
2124 case Attr::Format: {
2125 std::string Type = ReadString(Record, Idx);
2126 unsigned FormatIdx = Record[Idx++];
2127 unsigned FirstArg = Record[Idx++];
2128 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2129 break;
2130 }
2131
2132 SIMPLE_ATTR(GNUCInline);
2133
2134 case Attr::IBOutletKind:
2135 New = ::new (Context) IBOutletAttr();
2136 break;
2137
2138 SIMPLE_ATTR(NoReturn);
2139 SIMPLE_ATTR(NoThrow);
2140 SIMPLE_ATTR(Nodebug);
2141 SIMPLE_ATTR(Noinline);
2142
2143 case Attr::NonNull: {
2144 unsigned Size = Record[Idx++];
2145 llvm::SmallVector<unsigned, 16> ArgNums;
2146 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2147 Idx += Size;
2148 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2149 break;
2150 }
2151
2152 SIMPLE_ATTR(ObjCException);
2153 SIMPLE_ATTR(ObjCNSObject);
2154 SIMPLE_ATTR(Overloadable);
2155 UNSIGNED_ATTR(Packed);
2156 SIMPLE_ATTR(Pure);
2157 UNSIGNED_ATTR(Regparm);
2158 STRING_ATTR(Section);
2159 SIMPLE_ATTR(StdCall);
2160 SIMPLE_ATTR(TransparentUnion);
2161 SIMPLE_ATTR(Unavailable);
2162 SIMPLE_ATTR(Unused);
2163 SIMPLE_ATTR(Used);
2164
2165 case Attr::Visibility:
2166 New = ::new (Context) VisibilityAttr(
2167 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2168 break;
2169
2170 SIMPLE_ATTR(WarnUnusedResult);
2171 SIMPLE_ATTR(Weak);
2172 SIMPLE_ATTR(WeakImport);
2173 }
2174
2175 assert(New && "Unable to decode attribute?");
2176 New->setInherited(IsInherited);
2177 New->setNext(Attrs);
2178 Attrs = New;
2179 }
2180#undef UNSIGNED_ATTR
2181#undef STRING_ATTR
2182#undef SIMPLE_ATTR
2183
2184 // The list of attributes was built backwards. Reverse the list
2185 // before returning it.
2186 Attr *PrevAttr = 0, *NextAttr = 0;
2187 while (Attrs) {
2188 NextAttr = Attrs->getNext();
2189 Attrs->setNext(PrevAttr);
2190 PrevAttr = Attrs;
2191 Attrs = NextAttr;
2192 }
2193
2194 return PrevAttr;
2195}
2196
Douglas Gregorc9490c02009-04-16 22:23:12 +00002197Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002198 // Within the bitstream, expressions are stored in Reverse Polish
2199 // Notation, with each of the subexpressions preceding the
2200 // expression they are stored in. To evaluate expressions, we
2201 // continue reading expressions and placing them on the stack, with
2202 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002203 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002204 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002205 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002206 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002207 llvm::SmallVector<Stmt *, 16> StmtStack;
2208 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002209 Stmt::EmptyShell Empty;
2210
Douglas Gregor087fd532009-04-14 23:32:43 +00002211 while (true) {
2212 unsigned Code = Stream.ReadCode();
2213 if (Code == llvm::bitc::END_BLOCK) {
2214 if (Stream.ReadBlockEnd()) {
2215 Error("Error at end of Source Manager block");
2216 return 0;
2217 }
2218 break;
2219 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002220
Douglas Gregor087fd532009-04-14 23:32:43 +00002221 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2222 // No known subblocks, always skip them.
2223 Stream.ReadSubBlockID();
2224 if (Stream.SkipBlock()) {
2225 Error("Malformed block record");
2226 return 0;
2227 }
2228 continue;
2229 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002230
Douglas Gregor087fd532009-04-14 23:32:43 +00002231 if (Code == llvm::bitc::DEFINE_ABBREV) {
2232 Stream.ReadAbbrevRecord();
2233 continue;
2234 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002235
Douglas Gregorc9490c02009-04-16 22:23:12 +00002236 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002237 Idx = 0;
2238 Record.clear();
2239 bool Finished = false;
2240 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002241 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002242 Finished = true;
2243 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002244
Douglas Gregorc9490c02009-04-16 22:23:12 +00002245 case pch::STMT_NULL_PTR:
2246 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002247 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002248
Douglas Gregor025452f2009-04-17 00:04:06 +00002249 case pch::STMT_NULL:
2250 S = new (Context) NullStmt(Empty);
2251 break;
2252
2253 case pch::STMT_COMPOUND:
2254 S = new (Context) CompoundStmt(Empty);
2255 break;
2256
2257 case pch::STMT_CASE:
2258 S = new (Context) CaseStmt(Empty);
2259 break;
2260
2261 case pch::STMT_DEFAULT:
2262 S = new (Context) DefaultStmt(Empty);
2263 break;
2264
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002265 case pch::STMT_LABEL:
2266 S = new (Context) LabelStmt(Empty);
2267 break;
2268
Douglas Gregor025452f2009-04-17 00:04:06 +00002269 case pch::STMT_IF:
2270 S = new (Context) IfStmt(Empty);
2271 break;
2272
2273 case pch::STMT_SWITCH:
2274 S = new (Context) SwitchStmt(Empty);
2275 break;
2276
Douglas Gregord921cf92009-04-17 00:16:09 +00002277 case pch::STMT_WHILE:
2278 S = new (Context) WhileStmt(Empty);
2279 break;
2280
Douglas Gregor67d82492009-04-17 00:29:51 +00002281 case pch::STMT_DO:
2282 S = new (Context) DoStmt(Empty);
2283 break;
2284
2285 case pch::STMT_FOR:
2286 S = new (Context) ForStmt(Empty);
2287 break;
2288
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002289 case pch::STMT_GOTO:
2290 S = new (Context) GotoStmt(Empty);
2291 break;
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002292
2293 case pch::STMT_INDIRECT_GOTO:
2294 S = new (Context) IndirectGotoStmt(Empty);
2295 break;
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002296
Douglas Gregord921cf92009-04-17 00:16:09 +00002297 case pch::STMT_CONTINUE:
2298 S = new (Context) ContinueStmt(Empty);
2299 break;
2300
Douglas Gregor025452f2009-04-17 00:04:06 +00002301 case pch::STMT_BREAK:
2302 S = new (Context) BreakStmt(Empty);
2303 break;
2304
Douglas Gregor0de9d882009-04-17 16:34:57 +00002305 case pch::STMT_RETURN:
2306 S = new (Context) ReturnStmt(Empty);
2307 break;
2308
Douglas Gregor84f21702009-04-17 16:55:36 +00002309 case pch::STMT_DECL:
2310 S = new (Context) DeclStmt(Empty);
2311 break;
2312
Douglas Gregorcd7d5a92009-04-17 20:57:14 +00002313 case pch::STMT_ASM:
2314 S = new (Context) AsmStmt(Empty);
2315 break;
2316
Douglas Gregor087fd532009-04-14 23:32:43 +00002317 case pch::EXPR_PREDEFINED:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002318 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002319 break;
2320
2321 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002322 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002323 break;
2324
2325 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002326 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002327 break;
2328
2329 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002330 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002331 break;
2332
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002333 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002334 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002335 break;
2336
Douglas Gregor673ecd62009-04-15 16:35:07 +00002337 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002338 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002339 Record[PCHStmtReader::NumExprFields + 1]);
2340 break;
2341
Douglas Gregor087fd532009-04-14 23:32:43 +00002342 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002343 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002344 break;
2345
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002346 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002347 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002348 break;
2349
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002350 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002351 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002352 break;
2353
2354 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002355 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002356 break;
2357
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002358 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002359 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002360 break;
2361
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002362 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002363 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002364 break;
2365
2366 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002367 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002368 break;
2369
Douglas Gregordb600c32009-04-15 00:25:59 +00002370 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002371 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002372 break;
2373
Douglas Gregorad90e962009-04-15 22:40:36 +00002374 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002375 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002376 break;
2377
2378 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002379 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002380 break;
2381
Douglas Gregor087fd532009-04-14 23:32:43 +00002382 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002383 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002384 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002385
2386 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002387 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002388 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002389
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002390 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002391 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002392 break;
2393
Douglas Gregord3c98a02009-04-15 23:02:49 +00002394 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002395 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002396 break;
2397
Douglas Gregord077d752009-04-16 00:55:48 +00002398 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002399 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002400 break;
2401
2402 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002403 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002404 Record[PCHStmtReader::NumExprFields] - 1);
2405
2406 break;
2407
2408 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002409 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002410 break;
2411
Douglas Gregord3c98a02009-04-15 23:02:49 +00002412 case pch::EXPR_VA_ARG:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002413 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002414 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002415
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002416 case pch::EXPR_ADDR_LABEL:
2417 S = new (Context) AddrLabelExpr(Empty);
2418 break;
2419
Douglas Gregor6a2dd552009-04-17 19:05:30 +00002420 case pch::EXPR_STMT:
2421 S = new (Context) StmtExpr(Empty);
2422 break;
2423
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002424 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002425 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002426 break;
2427
2428 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002429 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002430 break;
2431
2432 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002433 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002434 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002435
2436 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002437 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002438 break;
2439
Douglas Gregor84af7c22009-04-17 19:21:43 +00002440 case pch::EXPR_BLOCK:
2441 S = new (Context) BlockExpr(Empty);
2442 break;
2443
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002444 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002445 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002446 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002447 }
2448
Douglas Gregorc9490c02009-04-16 22:23:12 +00002449 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002450 if (Finished)
2451 break;
2452
Douglas Gregorc9490c02009-04-16 22:23:12 +00002453 if (S) {
2454 unsigned NumSubStmts = Reader.Visit(S);
2455 while (NumSubStmts > 0) {
2456 StmtStack.pop_back();
2457 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002458 }
2459 }
2460
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002461 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002462 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002463 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002464 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor0de9d882009-04-17 16:34:57 +00002465 SwitchCaseStmts.clear();
Douglas Gregorc9490c02009-04-16 22:23:12 +00002466 return StmtStack.back();
2467}
2468
2469Expr *PCHReader::ReadExpr() {
2470 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002471}
2472
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002473DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002474 return Diag(SourceLocation(), DiagID);
2475}
2476
2477DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2478 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002479 Context.getSourceManager()),
2480 DiagID);
2481}
Douglas Gregor025452f2009-04-17 00:04:06 +00002482
2483/// \brief Record that the given ID maps to the given switch-case
2484/// statement.
2485void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2486 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2487 SwitchCaseStmts[ID] = SC;
2488}
2489
2490/// \brief Retrieve the switch-case statement with the given ID.
2491SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2492 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2493 return SwitchCaseStmts[ID];
2494}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002495
2496/// \brief Record that the given label statement has been
2497/// deserialized and has the given ID.
2498void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2499 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2500 "Deserialized label twice");
2501 LabelStmts[ID] = S;
2502
2503 // If we've already seen any goto statements that point to this
2504 // label, resolve them now.
2505 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2506 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2507 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2508 Goto->second->setLabel(S);
2509 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002510
2511 // If we've already seen any address-label statements that point to
2512 // this label, resolve them now.
2513 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2514 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2515 = UnresolvedAddrLabelExprs.equal_range(ID);
2516 for (AddrLabelIter AddrLabel = AddrLabels.first;
2517 AddrLabel != AddrLabels.second; ++AddrLabel)
2518 AddrLabel->second->setLabel(S);
2519 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor1de05fe2009-04-17 18:18:49 +00002520}
2521
2522/// \brief Set the label of the given statement to the label
2523/// identified by ID.
2524///
2525/// Depending on the order in which the label and other statements
2526/// referencing that label occur, this operation may complete
2527/// immediately (updating the statement) or it may queue the
2528/// statement to be back-patched later.
2529void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2530 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2531 if (Label != LabelStmts.end()) {
2532 // We've already seen this label, so set the label of the goto and
2533 // we're done.
2534 S->setLabel(Label->second);
2535 } else {
2536 // We haven't seen this label yet, so add this goto to the set of
2537 // unresolved goto statements.
2538 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2539 }
2540}
Douglas Gregor7d5c2f22009-04-17 18:58:21 +00002541
2542/// \brief Set the label of the given expression to the label
2543/// identified by ID.
2544///
2545/// Depending on the order in which the label and other statements
2546/// referencing that label occur, this operation may complete
2547/// immediately (updating the statement) or it may queue the
2548/// statement to be back-patched later.
2549void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2550 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2551 if (Label != LabelStmts.end()) {
2552 // We've already seen this label, so set the label of the
2553 // label-address expression and we're done.
2554 S->setLabel(Label->second);
2555 } else {
2556 // We haven't seen this label yet, so add this label-address
2557 // expression to the set of unresolved label-address expressions.
2558 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2559 }
2560}