blob: d2fb8ab3cbd89479b0daf495645364165c22261b [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000107}
108
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor982365e2009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregorc34897d2009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor982365e2009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000167}
168
Douglas Gregorc34897d2009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000180}
181
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor2a491792009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
200 unsigned NumParams = Record[Idx++];
201 llvm::SmallVector<ParmVarDecl *, 16> Params;
202 Params.reserve(NumParams);
203 for (unsigned I = 0; I != NumParams; ++I)
204 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
205 BD->setParams(Reader.getContext(), &Params[0], NumParams);
206}
207
Douglas Gregorc34897d2009-04-09 22:27:44 +0000208std::pair<uint64_t, uint64_t>
209PCHDeclReader::VisitDeclContext(DeclContext *DC) {
210 uint64_t LexicalOffset = Record[Idx++];
211 uint64_t VisibleOffset = 0;
212 if (DC->getPrimaryContext() == DC)
213 VisibleOffset = Record[Idx++];
214 return std::make_pair(LexicalOffset, VisibleOffset);
215}
216
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000217//===----------------------------------------------------------------------===//
218// Statement/expression deserialization
219//===----------------------------------------------------------------------===//
220namespace {
221 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000222 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000223 PCHReader &Reader;
224 const PCHReader::RecordData &Record;
225 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000226 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000227
228 public:
229 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000230 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
231 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000232
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000233 /// \brief The number of record fields required for the Stmt class
234 /// itself.
235 static const unsigned NumStmtFields = 0;
236
Douglas Gregor596e0932009-04-15 16:35:07 +0000237 /// \brief The number of record fields required for the Expr class
238 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000239 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000240
Douglas Gregora151ba42009-04-14 23:32:43 +0000241 // Each of the Visit* functions reads in part of the expression
242 // from the given record and the current expression stack, then
243 // return the total number of operands that it read from the
244 // expression stack.
245
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000246 unsigned VisitStmt(Stmt *S);
247 unsigned VisitNullStmt(NullStmt *S);
248 unsigned VisitCompoundStmt(CompoundStmt *S);
249 unsigned VisitSwitchCase(SwitchCase *S);
250 unsigned VisitCaseStmt(CaseStmt *S);
251 unsigned VisitDefaultStmt(DefaultStmt *S);
252 unsigned VisitIfStmt(IfStmt *S);
253 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000254 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000255 unsigned VisitDoStmt(DoStmt *S);
256 unsigned VisitForStmt(ForStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000257 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000258 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000259 unsigned VisitExpr(Expr *E);
260 unsigned VisitPredefinedExpr(PredefinedExpr *E);
261 unsigned VisitDeclRefExpr(DeclRefExpr *E);
262 unsigned VisitIntegerLiteral(IntegerLiteral *E);
263 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000264 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000265 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000266 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000267 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000268 unsigned VisitUnaryOperator(UnaryOperator *E);
269 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000270 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000271 unsigned VisitCallExpr(CallExpr *E);
272 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000273 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000274 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000275 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
276 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000277 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000278 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
279 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000280 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000281 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000282 unsigned VisitInitListExpr(InitListExpr *E);
283 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
284 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000285 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000286 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
287 unsigned VisitChooseExpr(ChooseExpr *E);
288 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000289 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
290 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000291 };
292}
293
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000294unsigned PCHStmtReader::VisitStmt(Stmt *S) {
295 assert(Idx == NumStmtFields && "Incorrect statement field count");
296 return 0;
297}
298
299unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
300 VisitStmt(S);
301 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
302 return 0;
303}
304
305unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
306 VisitStmt(S);
307 unsigned NumStmts = Record[Idx++];
308 S->setStmts(Reader.getContext(),
309 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
310 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
311 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
312 return NumStmts;
313}
314
315unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
316 VisitStmt(S);
317 Reader.RecordSwitchCaseID(S, Record[Idx++]);
318 return 0;
319}
320
321unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
322 VisitSwitchCase(S);
323 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
324 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
325 S->setSubStmt(StmtStack.back());
326 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
327 return 3;
328}
329
330unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
331 VisitSwitchCase(S);
332 S->setSubStmt(StmtStack.back());
333 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
334 return 1;
335}
336
337unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
338 VisitStmt(S);
339 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
340 S->setThen(StmtStack[StmtStack.size() - 2]);
341 S->setElse(StmtStack[StmtStack.size() - 1]);
342 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
343 return 3;
344}
345
346unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
347 VisitStmt(S);
348 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
349 S->setBody(StmtStack.back());
350 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
351 SwitchCase *PrevSC = 0;
352 for (unsigned N = Record.size(); Idx != N; ++Idx) {
353 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
354 if (PrevSC)
355 PrevSC->setNextSwitchCase(SC);
356 else
357 S->setSwitchCaseList(SC);
358 PrevSC = SC;
359 }
360 return 2;
361}
362
Douglas Gregora6b503f2009-04-17 00:16:09 +0000363unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
364 VisitStmt(S);
365 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
366 S->setBody(StmtStack.back());
367 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
368 return 2;
369}
370
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000371unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
372 VisitStmt(S);
373 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
374 S->setBody(StmtStack.back());
375 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
376 return 2;
377}
378
379unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
380 VisitStmt(S);
381 S->setInit(StmtStack[StmtStack.size() - 4]);
382 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
383 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
384 S->setBody(StmtStack.back());
385 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
386 return 4;
387}
388
Douglas Gregora6b503f2009-04-17 00:16:09 +0000389unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
390 VisitStmt(S);
391 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
392 return 0;
393}
394
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000395unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
396 VisitStmt(S);
397 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
398 return 0;
399}
400
Douglas Gregora151ba42009-04-14 23:32:43 +0000401unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000402 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000403 E->setType(Reader.GetType(Record[Idx++]));
404 E->setTypeDependent(Record[Idx++]);
405 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000406 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000407 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000408}
409
Douglas Gregora151ba42009-04-14 23:32:43 +0000410unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000411 VisitExpr(E);
412 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
413 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000414 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000415}
416
Douglas Gregora151ba42009-04-14 23:32:43 +0000417unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000418 VisitExpr(E);
419 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
420 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000421 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000422}
423
Douglas Gregora151ba42009-04-14 23:32:43 +0000424unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000425 VisitExpr(E);
426 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
427 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000428 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000429}
430
Douglas Gregora151ba42009-04-14 23:32:43 +0000431unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000432 VisitExpr(E);
433 E->setValue(Reader.ReadAPFloat(Record, Idx));
434 E->setExact(Record[Idx++]);
435 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000436 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000437}
438
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000439unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
440 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000441 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000442 return 1;
443}
444
Douglas Gregor596e0932009-04-15 16:35:07 +0000445unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
446 VisitExpr(E);
447 unsigned Len = Record[Idx++];
448 assert(Record[Idx] == E->getNumConcatenated() &&
449 "Wrong number of concatenated tokens!");
450 ++Idx;
451 E->setWide(Record[Idx++]);
452
453 // Read string data
454 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
455 E->setStrData(Reader.getContext(), &Str[0], Len);
456 Idx += Len;
457
458 // Read source locations
459 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
460 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
461
462 return 0;
463}
464
Douglas Gregora151ba42009-04-14 23:32:43 +0000465unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000466 VisitExpr(E);
467 E->setValue(Record[Idx++]);
468 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
469 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000470 return 0;
471}
472
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000473unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
474 VisitExpr(E);
475 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
476 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000477 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000478 return 1;
479}
480
Douglas Gregor12d74052009-04-15 15:58:59 +0000481unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
482 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000483 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000484 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
485 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
486 return 1;
487}
488
489unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
490 VisitExpr(E);
491 E->setSizeof(Record[Idx++]);
492 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000493 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000494 ++Idx;
495 } else {
496 E->setArgument(Reader.GetType(Record[Idx++]));
497 }
498 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
499 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
500 return E->isArgumentType()? 0 : 1;
501}
502
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000503unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
504 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000505 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
506 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000507 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
508 return 2;
509}
510
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000511unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
512 VisitExpr(E);
513 E->setNumArgs(Reader.getContext(), Record[Idx++]);
514 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000515 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000516 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000517 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000518 return E->getNumArgs() + 1;
519}
520
521unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
522 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000523 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000524 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
525 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
526 E->setArrow(Record[Idx++]);
527 return 1;
528}
529
Douglas Gregora151ba42009-04-14 23:32:43 +0000530unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
531 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000532 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000533 return 1;
534}
535
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000536unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
537 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000538 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
539 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000540 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
541 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
542 return 2;
543}
544
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000545unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
546 VisitBinaryOperator(E);
547 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
548 E->setComputationResultType(Reader.GetType(Record[Idx++]));
549 return 2;
550}
551
552unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
553 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000554 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
555 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
556 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000557 return 3;
558}
559
Douglas Gregora151ba42009-04-14 23:32:43 +0000560unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
561 VisitCastExpr(E);
562 E->setLvalueCast(Record[Idx++]);
563 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000564}
565
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000566unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
567 VisitCastExpr(E);
568 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
569 return 1;
570}
571
572unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
573 VisitExplicitCastExpr(E);
574 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
575 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
576 return 1;
577}
578
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000579unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
580 VisitExpr(E);
581 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000582 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000583 E->setFileScope(Record[Idx++]);
584 return 1;
585}
586
Douglas Gregorec0b8292009-04-15 23:02:49 +0000587unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
588 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000589 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000590 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
591 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 return 1;
593}
594
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000595unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
596 VisitExpr(E);
597 unsigned NumInits = Record[Idx++];
598 E->reserveInits(NumInits);
599 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000600 E->updateInit(I,
601 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
602 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000603 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
604 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
605 E->setInitializedFieldInUnion(
606 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
607 E->sawArrayRangeDesignator(Record[Idx++]);
608 return NumInits + 1;
609}
610
611unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
612 typedef DesignatedInitExpr::Designator Designator;
613
614 VisitExpr(E);
615 unsigned NumSubExprs = Record[Idx++];
616 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
617 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000618 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000619 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
620 E->setGNUSyntax(Record[Idx++]);
621
622 llvm::SmallVector<Designator, 4> Designators;
623 while (Idx < Record.size()) {
624 switch ((pch::DesignatorTypes)Record[Idx++]) {
625 case pch::DESIG_FIELD_DECL: {
626 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
627 SourceLocation DotLoc
628 = SourceLocation::getFromRawEncoding(Record[Idx++]);
629 SourceLocation FieldLoc
630 = SourceLocation::getFromRawEncoding(Record[Idx++]);
631 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
632 FieldLoc));
633 Designators.back().setField(Field);
634 break;
635 }
636
637 case pch::DESIG_FIELD_NAME: {
638 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
639 SourceLocation DotLoc
640 = SourceLocation::getFromRawEncoding(Record[Idx++]);
641 SourceLocation FieldLoc
642 = SourceLocation::getFromRawEncoding(Record[Idx++]);
643 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
644 break;
645 }
646
647 case pch::DESIG_ARRAY: {
648 unsigned Index = Record[Idx++];
649 SourceLocation LBracketLoc
650 = SourceLocation::getFromRawEncoding(Record[Idx++]);
651 SourceLocation RBracketLoc
652 = SourceLocation::getFromRawEncoding(Record[Idx++]);
653 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
654 break;
655 }
656
657 case pch::DESIG_ARRAY_RANGE: {
658 unsigned Index = Record[Idx++];
659 SourceLocation LBracketLoc
660 = SourceLocation::getFromRawEncoding(Record[Idx++]);
661 SourceLocation EllipsisLoc
662 = SourceLocation::getFromRawEncoding(Record[Idx++]);
663 SourceLocation RBracketLoc
664 = SourceLocation::getFromRawEncoding(Record[Idx++]);
665 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
666 RBracketLoc));
667 break;
668 }
669 }
670 }
671 E->setDesignators(&Designators[0], Designators.size());
672
673 return NumSubExprs;
674}
675
676unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
677 VisitExpr(E);
678 return 0;
679}
680
Douglas Gregorec0b8292009-04-15 23:02:49 +0000681unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
682 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000683 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000684 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
685 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
686 return 1;
687}
688
Douglas Gregor209d4622009-04-15 23:33:31 +0000689unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
690 VisitExpr(E);
691 E->setArgType1(Reader.GetType(Record[Idx++]));
692 E->setArgType2(Reader.GetType(Record[Idx++]));
693 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
694 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
695 return 0;
696}
697
698unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
699 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000700 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
701 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
702 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000703 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
704 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
705 return 3;
706}
707
708unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
709 VisitExpr(E);
710 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
711 return 0;
712}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000713
Douglas Gregor725e94b2009-04-16 00:01:45 +0000714unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
715 VisitExpr(E);
716 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000717 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000718 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
719 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
720 return NumExprs;
721}
722
723unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
724 VisitExpr(E);
725 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
726 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
727 E->setByRef(Record[Idx++]);
728 return 0;
729}
730
Douglas Gregorc34897d2009-04-09 22:27:44 +0000731// FIXME: use the diagnostics machinery
732static bool Error(const char *Str) {
733 std::fprintf(stderr, "%s\n", Str);
734 return true;
735}
736
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000737/// \brief Check the contents of the predefines buffer against the
738/// contents of the predefines buffer used to build the PCH file.
739///
740/// The contents of the two predefines buffers should be the same. If
741/// not, then some command-line option changed the preprocessor state
742/// and we must reject the PCH file.
743///
744/// \param PCHPredef The start of the predefines buffer in the PCH
745/// file.
746///
747/// \param PCHPredefLen The length of the predefines buffer in the PCH
748/// file.
749///
750/// \param PCHBufferID The FileID for the PCH predefines buffer.
751///
752/// \returns true if there was a mismatch (in which case the PCH file
753/// should be ignored), or false otherwise.
754bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
755 unsigned PCHPredefLen,
756 FileID PCHBufferID) {
757 const char *Predef = PP.getPredefines().c_str();
758 unsigned PredefLen = PP.getPredefines().size();
759
760 // If the two predefines buffers compare equal, we're done!.
761 if (PredefLen == PCHPredefLen &&
762 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
763 return false;
764
765 // The predefines buffers are different. Produce a reasonable
766 // diagnostic showing where they are different.
767
768 // The source locations (potentially in the two different predefines
769 // buffers)
770 SourceLocation Loc1, Loc2;
771 SourceManager &SourceMgr = PP.getSourceManager();
772
773 // Create a source buffer for our predefines string, so
774 // that we can build a diagnostic that points into that
775 // source buffer.
776 FileID BufferID;
777 if (Predef && Predef[0]) {
778 llvm::MemoryBuffer *Buffer
779 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
780 "<built-in>");
781 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
782 }
783
784 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
785 std::pair<const char *, const char *> Locations
786 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
787
788 if (Locations.first != Predef + MinLen) {
789 // We found the location in the two buffers where there is a
790 // difference. Form source locations to point there (in both
791 // buffers).
792 unsigned Offset = Locations.first - Predef;
793 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
794 .getFileLocWithOffset(Offset);
795 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
796 .getFileLocWithOffset(Offset);
797 } else if (PredefLen > PCHPredefLen) {
798 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
799 .getFileLocWithOffset(MinLen);
800 } else {
801 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
802 .getFileLocWithOffset(MinLen);
803 }
804
805 Diag(Loc1, diag::warn_pch_preprocessor);
806 if (Loc2.isValid())
807 Diag(Loc2, diag::note_predef_in_pch);
808 Diag(diag::note_ignoring_pch) << FileName;
809 return true;
810}
811
Douglas Gregor635f97f2009-04-13 16:31:14 +0000812/// \brief Read the line table in the source manager block.
813/// \returns true if ther was an error.
814static bool ParseLineTable(SourceManager &SourceMgr,
815 llvm::SmallVectorImpl<uint64_t> &Record) {
816 unsigned Idx = 0;
817 LineTableInfo &LineTable = SourceMgr.getLineTable();
818
819 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000820 std::map<int, int> FileIDs;
821 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000822 // Extract the file name
823 unsigned FilenameLen = Record[Idx++];
824 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
825 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000826 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
827 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000828 }
829
830 // Parse the line entries
831 std::vector<LineEntry> Entries;
832 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000833 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000834
835 // Extract the line entries
836 unsigned NumEntries = Record[Idx++];
837 Entries.clear();
838 Entries.reserve(NumEntries);
839 for (unsigned I = 0; I != NumEntries; ++I) {
840 unsigned FileOffset = Record[Idx++];
841 unsigned LineNo = Record[Idx++];
842 int FilenameID = Record[Idx++];
843 SrcMgr::CharacteristicKind FileKind
844 = (SrcMgr::CharacteristicKind)Record[Idx++];
845 unsigned IncludeOffset = Record[Idx++];
846 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
847 FileKind, IncludeOffset));
848 }
849 LineTable.AddEntry(FID, Entries);
850 }
851
852 return false;
853}
854
Douglas Gregorab1cef72009-04-10 03:52:48 +0000855/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000856PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000857 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000858 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
859 Error("Malformed source manager block record");
860 return Failure;
861 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000862
863 SourceManager &SourceMgr = Context.getSourceManager();
864 RecordData Record;
865 while (true) {
866 unsigned Code = Stream.ReadCode();
867 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000868 if (Stream.ReadBlockEnd()) {
869 Error("Error at end of Source Manager block");
870 return Failure;
871 }
872
873 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000874 }
875
876 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
877 // No known subblocks, always skip them.
878 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000879 if (Stream.SkipBlock()) {
880 Error("Malformed block record");
881 return Failure;
882 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000883 continue;
884 }
885
886 if (Code == llvm::bitc::DEFINE_ABBREV) {
887 Stream.ReadAbbrevRecord();
888 continue;
889 }
890
891 // Read a record.
892 const char *BlobStart;
893 unsigned BlobLen;
894 Record.clear();
895 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
896 default: // Default behavior: ignore.
897 break;
898
899 case pch::SM_SLOC_FILE_ENTRY: {
900 // FIXME: We would really like to delay the creation of this
901 // FileEntry until it is actually required, e.g., when producing
902 // a diagnostic with a source location in this file.
903 const FileEntry *File
904 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
905 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000906 FileID ID = SourceMgr.createFileID(File,
907 SourceLocation::getFromRawEncoding(Record[1]),
908 (CharacteristicKind)Record[2]);
909 if (Record[3])
910 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
911 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000912 break;
913 }
914
915 case pch::SM_SLOC_BUFFER_ENTRY: {
916 const char *Name = BlobStart;
917 unsigned Code = Stream.ReadCode();
918 Record.clear();
919 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
920 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000921 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000922 llvm::MemoryBuffer *Buffer
923 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
924 BlobStart + BlobLen - 1,
925 Name);
926 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
927
928 if (strcmp(Name, "<built-in>") == 0
929 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
930 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000931 break;
932 }
933
934 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
935 SourceLocation SpellingLoc
936 = SourceLocation::getFromRawEncoding(Record[1]);
937 SourceMgr.createInstantiationLoc(
938 SpellingLoc,
939 SourceLocation::getFromRawEncoding(Record[2]),
940 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000941 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000942 break;
943 }
944
Chris Lattnere1be6022009-04-14 23:22:57 +0000945 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000946 if (ParseLineTable(SourceMgr, Record))
947 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000948 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000949 }
950 }
951}
952
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000953bool PCHReader::ReadPreprocessorBlock() {
954 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
955 return Error("Malformed preprocessor block record");
956
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000957 RecordData Record;
958 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
959 MacroInfo *LastMacro = 0;
960
961 while (true) {
962 unsigned Code = Stream.ReadCode();
963 switch (Code) {
964 case llvm::bitc::END_BLOCK:
965 if (Stream.ReadBlockEnd())
966 return Error("Error at end of preprocessor block");
967 return false;
968
969 case llvm::bitc::ENTER_SUBBLOCK:
970 // No known subblocks, always skip them.
971 Stream.ReadSubBlockID();
972 if (Stream.SkipBlock())
973 return Error("Malformed block record");
974 continue;
975
976 case llvm::bitc::DEFINE_ABBREV:
977 Stream.ReadAbbrevRecord();
978 continue;
979 default: break;
980 }
981
982 // Read a record.
983 Record.clear();
984 pch::PreprocessorRecordTypes RecType =
985 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
986 switch (RecType) {
987 default: // Default behavior: ignore unknown records.
988 break;
Chris Lattner4b21c202009-04-13 01:29:17 +0000989 case pch::PP_COUNTER_VALUE:
990 if (!Record.empty())
991 PP.setCounterValue(Record[0]);
992 break;
993
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000994 case pch::PP_MACRO_OBJECT_LIKE:
995 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +0000996 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
997 if (II == 0)
998 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000999 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1000 bool isUsed = Record[2];
1001
1002 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1003 MI->setIsUsed(isUsed);
1004
1005 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1006 // Decode function-like macro info.
1007 bool isC99VarArgs = Record[3];
1008 bool isGNUVarArgs = Record[4];
1009 MacroArgs.clear();
1010 unsigned NumArgs = Record[5];
1011 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001012 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001013
1014 // Install function-like macro info.
1015 MI->setIsFunctionLike();
1016 if (isC99VarArgs) MI->setIsC99Varargs();
1017 if (isGNUVarArgs) MI->setIsGNUVarargs();
1018 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1019 PP.getPreprocessorAllocator());
1020 }
1021
1022 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001023 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001024
1025 // Remember that we saw this macro last so that we add the tokens that
1026 // form its body to it.
1027 LastMacro = MI;
1028 break;
1029 }
1030
1031 case pch::PP_TOKEN: {
1032 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1033 // pretend we didn't see this.
1034 if (LastMacro == 0) break;
1035
1036 Token Tok;
1037 Tok.startToken();
1038 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1039 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001040 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1041 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001042 Tok.setKind((tok::TokenKind)Record[3]);
1043 Tok.setFlag((Token::TokenFlags)Record[4]);
1044 LastMacro->AddTokenToBody(Tok);
1045 break;
1046 }
1047 }
1048 }
1049}
1050
Douglas Gregor179cfb12009-04-10 20:39:37 +00001051PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1052 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1053 Error("Malformed block record");
1054 return Failure;
1055 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001056
Chris Lattner29241862009-04-11 21:15:38 +00001057 uint64_t PreprocessorBlockBit = 0;
1058
Douglas Gregorc34897d2009-04-09 22:27:44 +00001059 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001060 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001061 while (!Stream.AtEndOfStream()) {
1062 unsigned Code = Stream.ReadCode();
1063 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001064 // If we saw the preprocessor block, read it now.
1065 if (PreprocessorBlockBit) {
1066 uint64_t SavedPos = Stream.GetCurrentBitNo();
1067 Stream.JumpToBit(PreprocessorBlockBit);
1068 if (ReadPreprocessorBlock()) {
1069 Error("Malformed preprocessor block");
1070 return Failure;
1071 }
1072 Stream.JumpToBit(SavedPos);
1073 }
1074
Douglas Gregor179cfb12009-04-10 20:39:37 +00001075 if (Stream.ReadBlockEnd()) {
1076 Error("Error at end of module block");
1077 return Failure;
1078 }
Chris Lattner29241862009-04-11 21:15:38 +00001079
Douglas Gregor179cfb12009-04-10 20:39:37 +00001080 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001081 }
1082
1083 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1084 switch (Stream.ReadSubBlockID()) {
1085 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1086 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1087 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001088 if (Stream.SkipBlock()) {
1089 Error("Malformed block record");
1090 return Failure;
1091 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001092 break;
1093
Chris Lattner29241862009-04-11 21:15:38 +00001094 case pch::PREPROCESSOR_BLOCK_ID:
1095 // Skip the preprocessor block for now, but remember where it is. We
1096 // want to read it in after the identifier table.
1097 if (PreprocessorBlockBit) {
1098 Error("Multiple preprocessor blocks found.");
1099 return Failure;
1100 }
1101 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1102 if (Stream.SkipBlock()) {
1103 Error("Malformed block record");
1104 return Failure;
1105 }
1106 break;
1107
Douglas Gregorab1cef72009-04-10 03:52:48 +00001108 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001109 switch (ReadSourceManagerBlock()) {
1110 case Success:
1111 break;
1112
1113 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001114 Error("Malformed source manager block");
1115 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001116
1117 case IgnorePCH:
1118 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001119 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001120 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001121 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001122 continue;
1123 }
1124
1125 if (Code == llvm::bitc::DEFINE_ABBREV) {
1126 Stream.ReadAbbrevRecord();
1127 continue;
1128 }
1129
1130 // Read and process a record.
1131 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001132 const char *BlobStart = 0;
1133 unsigned BlobLen = 0;
1134 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1135 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001136 default: // Default behavior: ignore.
1137 break;
1138
1139 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001140 if (!TypeOffsets.empty()) {
1141 Error("Duplicate TYPE_OFFSET record in PCH file");
1142 return Failure;
1143 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001144 TypeOffsets.swap(Record);
1145 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1146 break;
1147
1148 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001149 if (!DeclOffsets.empty()) {
1150 Error("Duplicate DECL_OFFSET record in PCH file");
1151 return Failure;
1152 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001153 DeclOffsets.swap(Record);
1154 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1155 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001156
1157 case pch::LANGUAGE_OPTIONS:
1158 if (ParseLanguageOptions(Record))
1159 return IgnorePCH;
1160 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001161
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001162 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001163 std::string TargetTriple(BlobStart, BlobLen);
1164 if (TargetTriple != Context.Target.getTargetTriple()) {
1165 Diag(diag::warn_pch_target_triple)
1166 << TargetTriple << Context.Target.getTargetTriple();
1167 Diag(diag::note_ignoring_pch) << FileName;
1168 return IgnorePCH;
1169 }
1170 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001171 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001172
1173 case pch::IDENTIFIER_TABLE:
1174 IdentifierTable = BlobStart;
1175 break;
1176
1177 case pch::IDENTIFIER_OFFSET:
1178 if (!IdentifierData.empty()) {
1179 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1180 return Failure;
1181 }
1182 IdentifierData.swap(Record);
1183#ifndef NDEBUG
1184 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1185 if ((IdentifierData[I] & 0x01) == 0) {
1186 Error("Malformed identifier table in the precompiled header");
1187 return Failure;
1188 }
1189 }
1190#endif
1191 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001192
1193 case pch::EXTERNAL_DEFINITIONS:
1194 if (!ExternalDefinitions.empty()) {
1195 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1196 return Failure;
1197 }
1198 ExternalDefinitions.swap(Record);
1199 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001200 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001201 }
1202
Douglas Gregor179cfb12009-04-10 20:39:37 +00001203 Error("Premature end of bitstream");
1204 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001205}
1206
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001207PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001208 // Set the PCH file name.
1209 this->FileName = FileName;
1210
Douglas Gregorc34897d2009-04-09 22:27:44 +00001211 // Open the PCH file.
1212 std::string ErrStr;
1213 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001214 if (!Buffer) {
1215 Error(ErrStr.c_str());
1216 return IgnorePCH;
1217 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001218
1219 // Initialize the stream
1220 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1221 (const unsigned char *)Buffer->getBufferEnd());
1222
1223 // Sniff for the signature.
1224 if (Stream.Read(8) != 'C' ||
1225 Stream.Read(8) != 'P' ||
1226 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001227 Stream.Read(8) != 'H') {
1228 Error("Not a PCH file");
1229 return IgnorePCH;
1230 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001231
1232 // We expect a number of well-defined blocks, though we don't necessarily
1233 // need to understand them all.
1234 while (!Stream.AtEndOfStream()) {
1235 unsigned Code = Stream.ReadCode();
1236
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001237 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1238 Error("Invalid record at top-level");
1239 return Failure;
1240 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001241
1242 unsigned BlockID = Stream.ReadSubBlockID();
1243
1244 // We only know the PCH subblock ID.
1245 switch (BlockID) {
1246 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001247 if (Stream.ReadBlockInfoBlock()) {
1248 Error("Malformed BlockInfoBlock");
1249 return Failure;
1250 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001251 break;
1252 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001253 switch (ReadPCHBlock()) {
1254 case Success:
1255 break;
1256
1257 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001258 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001259
1260 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001261 // FIXME: We could consider reading through to the end of this
1262 // PCH block, skipping subblocks, to see if there are other
1263 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001264 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001265 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001266 break;
1267 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001268 if (Stream.SkipBlock()) {
1269 Error("Malformed block record");
1270 return Failure;
1271 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001272 break;
1273 }
1274 }
1275
1276 // Load the translation unit declaration
1277 ReadDeclRecord(DeclOffsets[0], 0);
1278
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001279 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001280}
1281
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001282namespace {
1283 /// \brief Helper class that saves the current stream position and
1284 /// then restores it when destroyed.
1285 struct VISIBILITY_HIDDEN SavedStreamPosition {
1286 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001287 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001288
1289 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001290 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001291 }
1292
1293 private:
1294 llvm::BitstreamReader &Stream;
1295 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001296 };
1297}
1298
Douglas Gregor179cfb12009-04-10 20:39:37 +00001299/// \brief Parse the record that corresponds to a LangOptions data
1300/// structure.
1301///
1302/// This routine compares the language options used to generate the
1303/// PCH file against the language options set for the current
1304/// compilation. For each option, we classify differences between the
1305/// two compiler states as either "benign" or "important". Benign
1306/// differences don't matter, and we accept them without complaint
1307/// (and without modifying the language options). Differences between
1308/// the states for important options cause the PCH file to be
1309/// unusable, so we emit a warning and return true to indicate that
1310/// there was an error.
1311///
1312/// \returns true if the PCH file is unacceptable, false otherwise.
1313bool PCHReader::ParseLanguageOptions(
1314 const llvm::SmallVectorImpl<uint64_t> &Record) {
1315 const LangOptions &LangOpts = Context.getLangOptions();
1316#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1317#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1318 if (Record[Idx] != LangOpts.Option) { \
1319 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1320 Diag(diag::note_ignoring_pch) << FileName; \
1321 return true; \
1322 } \
1323 ++Idx
1324
1325 unsigned Idx = 0;
1326 PARSE_LANGOPT_BENIGN(Trigraphs);
1327 PARSE_LANGOPT_BENIGN(BCPLComment);
1328 PARSE_LANGOPT_BENIGN(DollarIdents);
1329 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1330 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1331 PARSE_LANGOPT_BENIGN(ImplicitInt);
1332 PARSE_LANGOPT_BENIGN(Digraphs);
1333 PARSE_LANGOPT_BENIGN(HexFloats);
1334 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1335 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1336 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1337 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1338 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1339 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1340 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1341 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1342 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1343 PARSE_LANGOPT_BENIGN(PascalStrings);
1344 PARSE_LANGOPT_BENIGN(Boolean);
1345 PARSE_LANGOPT_BENIGN(WritableStrings);
1346 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1347 diag::warn_pch_lax_vector_conversions);
1348 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1349 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1350 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1351 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1352 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1353 diag::warn_pch_thread_safe_statics);
1354 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1355 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1356 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1357 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1358 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1359 diag::warn_pch_heinous_extensions);
1360 // FIXME: Most of the options below are benign if the macro wasn't
1361 // used. Unfortunately, this means that a PCH compiled without
1362 // optimization can't be used with optimization turned on, even
1363 // though the only thing that changes is whether __OPTIMIZE__ was
1364 // defined... but if __OPTIMIZE__ never showed up in the header, it
1365 // doesn't matter. We could consider making this some special kind
1366 // of check.
1367 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1368 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1369 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1370 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1371 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1372 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1373 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1374 Diag(diag::warn_pch_gc_mode)
1375 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1376 Diag(diag::note_ignoring_pch) << FileName;
1377 return true;
1378 }
1379 ++Idx;
1380 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1381 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1382#undef PARSE_LANGOPT_IRRELEVANT
1383#undef PARSE_LANGOPT_BENIGN
1384
1385 return false;
1386}
1387
Douglas Gregorc34897d2009-04-09 22:27:44 +00001388/// \brief Read and return the type at the given offset.
1389///
1390/// This routine actually reads the record corresponding to the type
1391/// at the given offset in the bitstream. It is a helper routine for
1392/// GetType, which deals with reading type IDs.
1393QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001394 // Keep track of where we are in the stream, then jump back there
1395 // after reading this type.
1396 SavedStreamPosition SavedPosition(Stream);
1397
Douglas Gregorc34897d2009-04-09 22:27:44 +00001398 Stream.JumpToBit(Offset);
1399 RecordData Record;
1400 unsigned Code = Stream.ReadCode();
1401 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001402 case pch::TYPE_EXT_QUAL: {
1403 assert(Record.size() == 3 &&
1404 "Incorrect encoding of extended qualifier type");
1405 QualType Base = GetType(Record[0]);
1406 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1407 unsigned AddressSpace = Record[2];
1408
1409 QualType T = Base;
1410 if (GCAttr != QualType::GCNone)
1411 T = Context.getObjCGCQualType(T, GCAttr);
1412 if (AddressSpace)
1413 T = Context.getAddrSpaceQualType(T, AddressSpace);
1414 return T;
1415 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001416
Douglas Gregorc34897d2009-04-09 22:27:44 +00001417 case pch::TYPE_FIXED_WIDTH_INT: {
1418 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1419 return Context.getFixedWidthIntType(Record[0], Record[1]);
1420 }
1421
1422 case pch::TYPE_COMPLEX: {
1423 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1424 QualType ElemType = GetType(Record[0]);
1425 return Context.getComplexType(ElemType);
1426 }
1427
1428 case pch::TYPE_POINTER: {
1429 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1430 QualType PointeeType = GetType(Record[0]);
1431 return Context.getPointerType(PointeeType);
1432 }
1433
1434 case pch::TYPE_BLOCK_POINTER: {
1435 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1436 QualType PointeeType = GetType(Record[0]);
1437 return Context.getBlockPointerType(PointeeType);
1438 }
1439
1440 case pch::TYPE_LVALUE_REFERENCE: {
1441 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1442 QualType PointeeType = GetType(Record[0]);
1443 return Context.getLValueReferenceType(PointeeType);
1444 }
1445
1446 case pch::TYPE_RVALUE_REFERENCE: {
1447 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1448 QualType PointeeType = GetType(Record[0]);
1449 return Context.getRValueReferenceType(PointeeType);
1450 }
1451
1452 case pch::TYPE_MEMBER_POINTER: {
1453 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1454 QualType PointeeType = GetType(Record[0]);
1455 QualType ClassType = GetType(Record[1]);
1456 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1457 }
1458
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001459 case pch::TYPE_CONSTANT_ARRAY: {
1460 QualType ElementType = GetType(Record[0]);
1461 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1462 unsigned IndexTypeQuals = Record[2];
1463 unsigned Idx = 3;
1464 llvm::APInt Size = ReadAPInt(Record, Idx);
1465 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1466 }
1467
1468 case pch::TYPE_INCOMPLETE_ARRAY: {
1469 QualType ElementType = GetType(Record[0]);
1470 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1471 unsigned IndexTypeQuals = Record[2];
1472 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1473 }
1474
1475 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001476 QualType ElementType = GetType(Record[0]);
1477 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1478 unsigned IndexTypeQuals = Record[2];
1479 return Context.getVariableArrayType(ElementType, ReadExpr(),
1480 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001481 }
1482
1483 case pch::TYPE_VECTOR: {
1484 if (Record.size() != 2) {
1485 Error("Incorrect encoding of vector type in PCH file");
1486 return QualType();
1487 }
1488
1489 QualType ElementType = GetType(Record[0]);
1490 unsigned NumElements = Record[1];
1491 return Context.getVectorType(ElementType, NumElements);
1492 }
1493
1494 case pch::TYPE_EXT_VECTOR: {
1495 if (Record.size() != 2) {
1496 Error("Incorrect encoding of extended vector type in PCH file");
1497 return QualType();
1498 }
1499
1500 QualType ElementType = GetType(Record[0]);
1501 unsigned NumElements = Record[1];
1502 return Context.getExtVectorType(ElementType, NumElements);
1503 }
1504
1505 case pch::TYPE_FUNCTION_NO_PROTO: {
1506 if (Record.size() != 1) {
1507 Error("Incorrect encoding of no-proto function type");
1508 return QualType();
1509 }
1510 QualType ResultType = GetType(Record[0]);
1511 return Context.getFunctionNoProtoType(ResultType);
1512 }
1513
1514 case pch::TYPE_FUNCTION_PROTO: {
1515 QualType ResultType = GetType(Record[0]);
1516 unsigned Idx = 1;
1517 unsigned NumParams = Record[Idx++];
1518 llvm::SmallVector<QualType, 16> ParamTypes;
1519 for (unsigned I = 0; I != NumParams; ++I)
1520 ParamTypes.push_back(GetType(Record[Idx++]));
1521 bool isVariadic = Record[Idx++];
1522 unsigned Quals = Record[Idx++];
1523 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1524 isVariadic, Quals);
1525 }
1526
1527 case pch::TYPE_TYPEDEF:
1528 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1529 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1530
1531 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001532 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001533
1534 case pch::TYPE_TYPEOF: {
1535 if (Record.size() != 1) {
1536 Error("Incorrect encoding of typeof(type) in PCH file");
1537 return QualType();
1538 }
1539 QualType UnderlyingType = GetType(Record[0]);
1540 return Context.getTypeOfType(UnderlyingType);
1541 }
1542
1543 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001544 assert(Record.size() == 1 && "Incorrect encoding of record type");
1545 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001546
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001547 case pch::TYPE_ENUM:
1548 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1549 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1550
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001551 case pch::TYPE_OBJC_INTERFACE:
1552 // FIXME: Deserialize ObjCInterfaceType
1553 assert(false && "Cannot de-serialize ObjC interface types yet");
1554 return QualType();
1555
1556 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1557 // FIXME: Deserialize ObjCQualifiedInterfaceType
1558 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1559 return QualType();
1560
1561 case pch::TYPE_OBJC_QUALIFIED_ID:
1562 // FIXME: Deserialize ObjCQualifiedIdType
1563 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1564 return QualType();
1565
1566 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1567 // FIXME: Deserialize ObjCQualifiedClassType
1568 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1569 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001570 }
1571
1572 // Suppress a GCC warning
1573 return QualType();
1574}
1575
1576/// \brief Note that we have loaded the declaration with the given
1577/// Index.
1578///
1579/// This routine notes that this declaration has already been loaded,
1580/// so that future GetDecl calls will return this declaration rather
1581/// than trying to load a new declaration.
1582inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1583 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1584 DeclAlreadyLoaded[Index] = true;
1585 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1586}
1587
1588/// \brief Read the declaration at the given offset from the PCH file.
1589Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001590 // Keep track of where we are in the stream, then jump back there
1591 // after reading this declaration.
1592 SavedStreamPosition SavedPosition(Stream);
1593
Douglas Gregorc34897d2009-04-09 22:27:44 +00001594 Decl *D = 0;
1595 Stream.JumpToBit(Offset);
1596 RecordData Record;
1597 unsigned Code = Stream.ReadCode();
1598 unsigned Idx = 0;
1599 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001600
Douglas Gregorc34897d2009-04-09 22:27:44 +00001601 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001602 case pch::DECL_ATTR:
1603 case pch::DECL_CONTEXT_LEXICAL:
1604 case pch::DECL_CONTEXT_VISIBLE:
1605 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1606 break;
1607
Douglas Gregorc34897d2009-04-09 22:27:44 +00001608 case pch::DECL_TRANSLATION_UNIT:
1609 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001610 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001611 break;
1612
1613 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001614 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001615 break;
1616 }
1617
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001618 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001619 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001620 break;
1621 }
1622
Douglas Gregor982365e2009-04-13 21:20:57 +00001623 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001624 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1625 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001626 break;
1627 }
1628
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001629 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001630 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1631 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001632 break;
1633 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001634
1635 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001636 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1637 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001638 break;
1639 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001640
Douglas Gregor982365e2009-04-13 21:20:57 +00001641 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001642 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1643 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001644 break;
1645 }
1646
Douglas Gregorc34897d2009-04-09 22:27:44 +00001647 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001648 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1649 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001650 break;
1651 }
1652
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001653 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001654 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1655 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001656 break;
1657 }
1658
1659 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001660 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001661 QualType(), QualType(), VarDecl::None,
1662 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001663 break;
1664 }
1665
Douglas Gregor2a491792009-04-13 22:49:25 +00001666 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001667 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001668 break;
1669 }
1670
1671 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001672 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001673 break;
1674 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001675 }
1676
Douglas Gregorddf4d092009-04-16 22:29:51 +00001677 assert(D && "Unknown declaration creating PCH file");
1678 if (D) {
1679 LoadedDecl(Index, D);
1680 Reader.Visit(D);
1681 }
1682
Douglas Gregorc34897d2009-04-09 22:27:44 +00001683 // If this declaration is also a declaration context, get the
1684 // offsets for its tables of lexical and visible declarations.
1685 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1686 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1687 if (Offsets.first || Offsets.second) {
1688 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1689 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1690 DeclContextOffsets[DC] = Offsets;
1691 }
1692 }
1693 assert(Idx == Record.size());
1694
1695 return D;
1696}
1697
Douglas Gregorac8f2802009-04-10 17:25:41 +00001698QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001699 unsigned Quals = ID & 0x07;
1700 unsigned Index = ID >> 3;
1701
1702 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1703 QualType T;
1704 switch ((pch::PredefinedTypeIDs)Index) {
1705 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1706 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1707 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1708
1709 case pch::PREDEF_TYPE_CHAR_U_ID:
1710 case pch::PREDEF_TYPE_CHAR_S_ID:
1711 // FIXME: Check that the signedness of CharTy is correct!
1712 T = Context.CharTy;
1713 break;
1714
1715 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1716 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1717 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1718 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1719 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1720 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1721 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1722 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1723 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1724 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1725 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1726 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1727 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1728 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1729 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1730 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1731 }
1732
1733 assert(!T.isNull() && "Unknown predefined type");
1734 return T.getQualifiedType(Quals);
1735 }
1736
1737 Index -= pch::NUM_PREDEF_TYPE_IDS;
1738 if (!TypeAlreadyLoaded[Index]) {
1739 // Load the type from the PCH file.
1740 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1741 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1742 TypeAlreadyLoaded[Index] = true;
1743 }
1744
1745 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1746}
1747
Douglas Gregorac8f2802009-04-10 17:25:41 +00001748Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001749 if (ID == 0)
1750 return 0;
1751
1752 unsigned Index = ID - 1;
1753 if (DeclAlreadyLoaded[Index])
1754 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1755
1756 // Load the declaration from the PCH file.
1757 return ReadDeclRecord(DeclOffsets[Index], Index);
1758}
1759
1760bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001761 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001762 assert(DC->hasExternalLexicalStorage() &&
1763 "DeclContext has no lexical decls in storage");
1764 uint64_t Offset = DeclContextOffsets[DC].first;
1765 assert(Offset && "DeclContext has no lexical decls in storage");
1766
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001767 // Keep track of where we are in the stream, then jump back there
1768 // after reading this context.
1769 SavedStreamPosition SavedPosition(Stream);
1770
Douglas Gregorc34897d2009-04-09 22:27:44 +00001771 // Load the record containing all of the declarations lexically in
1772 // this context.
1773 Stream.JumpToBit(Offset);
1774 RecordData Record;
1775 unsigned Code = Stream.ReadCode();
1776 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001777 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001778 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1779
1780 // Load all of the declaration IDs
1781 Decls.clear();
1782 Decls.insert(Decls.end(), Record.begin(), Record.end());
1783 return false;
1784}
1785
1786bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1787 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1788 assert(DC->hasExternalVisibleStorage() &&
1789 "DeclContext has no visible decls in storage");
1790 uint64_t Offset = DeclContextOffsets[DC].second;
1791 assert(Offset && "DeclContext has no visible decls in storage");
1792
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001793 // Keep track of where we are in the stream, then jump back there
1794 // after reading this context.
1795 SavedStreamPosition SavedPosition(Stream);
1796
Douglas Gregorc34897d2009-04-09 22:27:44 +00001797 // Load the record containing all of the declarations visible in
1798 // this context.
1799 Stream.JumpToBit(Offset);
1800 RecordData Record;
1801 unsigned Code = Stream.ReadCode();
1802 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001803 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001804 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1805 if (Record.size() == 0)
1806 return false;
1807
1808 Decls.clear();
1809
1810 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001811 while (Idx < Record.size()) {
1812 Decls.push_back(VisibleDeclaration());
1813 Decls.back().Name = ReadDeclarationName(Record, Idx);
1814
Douglas Gregorc34897d2009-04-09 22:27:44 +00001815 unsigned Size = Record[Idx++];
1816 llvm::SmallVector<unsigned, 4> & LoadedDecls
1817 = Decls.back().Declarations;
1818 LoadedDecls.reserve(Size);
1819 for (unsigned I = 0; I < Size; ++I)
1820 LoadedDecls.push_back(Record[Idx++]);
1821 }
1822
1823 return false;
1824}
1825
Douglas Gregor631f6c62009-04-14 00:24:19 +00001826void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1827 if (!Consumer)
1828 return;
1829
1830 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1831 Decl *D = GetDecl(ExternalDefinitions[I]);
1832 DeclGroupRef DG(D);
1833 Consumer->HandleTopLevelDecl(DG);
1834 }
1835}
1836
Douglas Gregorc34897d2009-04-09 22:27:44 +00001837void PCHReader::PrintStats() {
1838 std::fprintf(stderr, "*** PCH Statistics:\n");
1839
1840 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1841 TypeAlreadyLoaded.end(),
1842 true);
1843 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1844 DeclAlreadyLoaded.end(),
1845 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001846 unsigned NumIdentifiersLoaded = 0;
1847 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1848 if ((IdentifierData[I] & 0x01) == 0)
1849 ++NumIdentifiersLoaded;
1850 }
1851
Douglas Gregorc34897d2009-04-09 22:27:44 +00001852 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1853 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001854 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001855 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1856 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001857 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1858 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1859 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1860 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001861 std::fprintf(stderr, "\n");
1862}
1863
Chris Lattner29241862009-04-11 21:15:38 +00001864IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001865 if (ID == 0)
1866 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001867
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001868 if (!IdentifierTable || IdentifierData.empty()) {
1869 Error("No identifier table in PCH file");
1870 return 0;
1871 }
Chris Lattner29241862009-04-11 21:15:38 +00001872
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001873 if (IdentifierData[ID - 1] & 0x01) {
1874 uint64_t Offset = IdentifierData[ID - 1];
1875 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001876 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001877 }
Chris Lattner29241862009-04-11 21:15:38 +00001878
1879 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001880}
1881
1882DeclarationName
1883PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1884 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1885 switch (Kind) {
1886 case DeclarationName::Identifier:
1887 return DeclarationName(GetIdentifierInfo(Record, Idx));
1888
1889 case DeclarationName::ObjCZeroArgSelector:
1890 case DeclarationName::ObjCOneArgSelector:
1891 case DeclarationName::ObjCMultiArgSelector:
1892 assert(false && "Unable to de-serialize Objective-C selectors");
1893 break;
1894
1895 case DeclarationName::CXXConstructorName:
1896 return Context.DeclarationNames.getCXXConstructorName(
1897 GetType(Record[Idx++]));
1898
1899 case DeclarationName::CXXDestructorName:
1900 return Context.DeclarationNames.getCXXDestructorName(
1901 GetType(Record[Idx++]));
1902
1903 case DeclarationName::CXXConversionFunctionName:
1904 return Context.DeclarationNames.getCXXConversionFunctionName(
1905 GetType(Record[Idx++]));
1906
1907 case DeclarationName::CXXOperatorName:
1908 return Context.DeclarationNames.getCXXOperatorName(
1909 (OverloadedOperatorKind)Record[Idx++]);
1910
1911 case DeclarationName::CXXUsingDirective:
1912 return DeclarationName::getUsingDirectiveName();
1913 }
1914
1915 // Required to silence GCC warning
1916 return DeclarationName();
1917}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001918
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001919/// \brief Read an integral value
1920llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1921 unsigned BitWidth = Record[Idx++];
1922 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1923 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1924 Idx += NumWords;
1925 return Result;
1926}
1927
1928/// \brief Read a signed integral value
1929llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1930 bool isUnsigned = Record[Idx++];
1931 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1932}
1933
Douglas Gregore2f37202009-04-14 21:55:33 +00001934/// \brief Read a floating-point value
1935llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001936 return llvm::APFloat(ReadAPInt(Record, Idx));
1937}
1938
Douglas Gregor1c507882009-04-15 21:30:51 +00001939// \brief Read a string
1940std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1941 unsigned Len = Record[Idx++];
1942 std::string Result(&Record[Idx], &Record[Idx] + Len);
1943 Idx += Len;
1944 return Result;
1945}
1946
1947/// \brief Reads attributes from the current stream position.
1948Attr *PCHReader::ReadAttributes() {
1949 unsigned Code = Stream.ReadCode();
1950 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1951 "Expected unabbreviated record"); (void)Code;
1952
1953 RecordData Record;
1954 unsigned Idx = 0;
1955 unsigned RecCode = Stream.ReadRecord(Code, Record);
1956 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1957 (void)RecCode;
1958
1959#define SIMPLE_ATTR(Name) \
1960 case Attr::Name: \
1961 New = ::new (Context) Name##Attr(); \
1962 break
1963
1964#define STRING_ATTR(Name) \
1965 case Attr::Name: \
1966 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1967 break
1968
1969#define UNSIGNED_ATTR(Name) \
1970 case Attr::Name: \
1971 New = ::new (Context) Name##Attr(Record[Idx++]); \
1972 break
1973
1974 Attr *Attrs = 0;
1975 while (Idx < Record.size()) {
1976 Attr *New = 0;
1977 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1978 bool IsInherited = Record[Idx++];
1979
1980 switch (Kind) {
1981 STRING_ATTR(Alias);
1982 UNSIGNED_ATTR(Aligned);
1983 SIMPLE_ATTR(AlwaysInline);
1984 SIMPLE_ATTR(AnalyzerNoReturn);
1985 STRING_ATTR(Annotate);
1986 STRING_ATTR(AsmLabel);
1987
1988 case Attr::Blocks:
1989 New = ::new (Context) BlocksAttr(
1990 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1991 break;
1992
1993 case Attr::Cleanup:
1994 New = ::new (Context) CleanupAttr(
1995 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1996 break;
1997
1998 SIMPLE_ATTR(Const);
1999 UNSIGNED_ATTR(Constructor);
2000 SIMPLE_ATTR(DLLExport);
2001 SIMPLE_ATTR(DLLImport);
2002 SIMPLE_ATTR(Deprecated);
2003 UNSIGNED_ATTR(Destructor);
2004 SIMPLE_ATTR(FastCall);
2005
2006 case Attr::Format: {
2007 std::string Type = ReadString(Record, Idx);
2008 unsigned FormatIdx = Record[Idx++];
2009 unsigned FirstArg = Record[Idx++];
2010 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2011 break;
2012 }
2013
2014 SIMPLE_ATTR(GNUCInline);
2015
2016 case Attr::IBOutletKind:
2017 New = ::new (Context) IBOutletAttr();
2018 break;
2019
2020 SIMPLE_ATTR(NoReturn);
2021 SIMPLE_ATTR(NoThrow);
2022 SIMPLE_ATTR(Nodebug);
2023 SIMPLE_ATTR(Noinline);
2024
2025 case Attr::NonNull: {
2026 unsigned Size = Record[Idx++];
2027 llvm::SmallVector<unsigned, 16> ArgNums;
2028 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2029 Idx += Size;
2030 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2031 break;
2032 }
2033
2034 SIMPLE_ATTR(ObjCException);
2035 SIMPLE_ATTR(ObjCNSObject);
2036 SIMPLE_ATTR(Overloadable);
2037 UNSIGNED_ATTR(Packed);
2038 SIMPLE_ATTR(Pure);
2039 UNSIGNED_ATTR(Regparm);
2040 STRING_ATTR(Section);
2041 SIMPLE_ATTR(StdCall);
2042 SIMPLE_ATTR(TransparentUnion);
2043 SIMPLE_ATTR(Unavailable);
2044 SIMPLE_ATTR(Unused);
2045 SIMPLE_ATTR(Used);
2046
2047 case Attr::Visibility:
2048 New = ::new (Context) VisibilityAttr(
2049 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2050 break;
2051
2052 SIMPLE_ATTR(WarnUnusedResult);
2053 SIMPLE_ATTR(Weak);
2054 SIMPLE_ATTR(WeakImport);
2055 }
2056
2057 assert(New && "Unable to decode attribute?");
2058 New->setInherited(IsInherited);
2059 New->setNext(Attrs);
2060 Attrs = New;
2061 }
2062#undef UNSIGNED_ATTR
2063#undef STRING_ATTR
2064#undef SIMPLE_ATTR
2065
2066 // The list of attributes was built backwards. Reverse the list
2067 // before returning it.
2068 Attr *PrevAttr = 0, *NextAttr = 0;
2069 while (Attrs) {
2070 NextAttr = Attrs->getNext();
2071 Attrs->setNext(PrevAttr);
2072 PrevAttr = Attrs;
2073 Attrs = NextAttr;
2074 }
2075
2076 return PrevAttr;
2077}
2078
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002079Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002080 // Within the bitstream, expressions are stored in Reverse Polish
2081 // Notation, with each of the subexpressions preceding the
2082 // expression they are stored in. To evaluate expressions, we
2083 // continue reading expressions and placing them on the stack, with
2084 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002085 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002086 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002087 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002088 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002089 llvm::SmallVector<Stmt *, 16> StmtStack;
2090 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002091 Stmt::EmptyShell Empty;
2092
Douglas Gregora151ba42009-04-14 23:32:43 +00002093 while (true) {
2094 unsigned Code = Stream.ReadCode();
2095 if (Code == llvm::bitc::END_BLOCK) {
2096 if (Stream.ReadBlockEnd()) {
2097 Error("Error at end of Source Manager block");
2098 return 0;
2099 }
2100 break;
2101 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002102
Douglas Gregora151ba42009-04-14 23:32:43 +00002103 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2104 // No known subblocks, always skip them.
2105 Stream.ReadSubBlockID();
2106 if (Stream.SkipBlock()) {
2107 Error("Malformed block record");
2108 return 0;
2109 }
2110 continue;
2111 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002112
Douglas Gregora151ba42009-04-14 23:32:43 +00002113 if (Code == llvm::bitc::DEFINE_ABBREV) {
2114 Stream.ReadAbbrevRecord();
2115 continue;
2116 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002117
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002118 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002119 Idx = 0;
2120 Record.clear();
2121 bool Finished = false;
2122 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002123 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002124 Finished = true;
2125 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002126
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002127 case pch::STMT_NULL_PTR:
2128 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002129 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002130
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002131 case pch::STMT_NULL:
2132 S = new (Context) NullStmt(Empty);
2133 break;
2134
2135 case pch::STMT_COMPOUND:
2136 S = new (Context) CompoundStmt(Empty);
2137 break;
2138
2139 case pch::STMT_CASE:
2140 S = new (Context) CaseStmt(Empty);
2141 break;
2142
2143 case pch::STMT_DEFAULT:
2144 S = new (Context) DefaultStmt(Empty);
2145 break;
2146
2147 case pch::STMT_IF:
2148 S = new (Context) IfStmt(Empty);
2149 break;
2150
2151 case pch::STMT_SWITCH:
2152 S = new (Context) SwitchStmt(Empty);
2153 break;
2154
Douglas Gregora6b503f2009-04-17 00:16:09 +00002155 case pch::STMT_WHILE:
2156 S = new (Context) WhileStmt(Empty);
2157 break;
2158
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002159 case pch::STMT_DO:
2160 S = new (Context) DoStmt(Empty);
2161 break;
2162
2163 case pch::STMT_FOR:
2164 S = new (Context) ForStmt(Empty);
2165 break;
2166
Douglas Gregora6b503f2009-04-17 00:16:09 +00002167 case pch::STMT_CONTINUE:
2168 S = new (Context) ContinueStmt(Empty);
2169 break;
2170
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002171 case pch::STMT_BREAK:
2172 S = new (Context) BreakStmt(Empty);
2173 break;
2174
Douglas Gregora151ba42009-04-14 23:32:43 +00002175 case pch::EXPR_PREDEFINED:
2176 // FIXME: untested (until we can serialize function bodies).
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002177 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002178 break;
2179
2180 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002181 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002182 break;
2183
2184 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002185 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002186 break;
2187
2188 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002189 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002190 break;
2191
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002192 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002193 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002194 break;
2195
Douglas Gregor596e0932009-04-15 16:35:07 +00002196 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002197 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002198 Record[PCHStmtReader::NumExprFields + 1]);
2199 break;
2200
Douglas Gregora151ba42009-04-14 23:32:43 +00002201 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002202 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002203 break;
2204
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002205 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002206 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002207 break;
2208
Douglas Gregor12d74052009-04-15 15:58:59 +00002209 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002210 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002211 break;
2212
2213 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002214 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002215 break;
2216
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002217 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002218 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002219 break;
2220
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002221 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002222 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002223 break;
2224
2225 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002226 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002227 break;
2228
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002229 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002230 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002231 break;
2232
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002233 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002234 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002235 break;
2236
2237 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002238 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002239 break;
2240
Douglas Gregora151ba42009-04-14 23:32:43 +00002241 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002242 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002243 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002244
2245 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002246 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002247 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002248
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002249 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002250 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002251 break;
2252
Douglas Gregorec0b8292009-04-15 23:02:49 +00002253 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002254 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002255 break;
2256
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002257 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002258 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002259 break;
2260
2261 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002262 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002263 Record[PCHStmtReader::NumExprFields] - 1);
2264
2265 break;
2266
2267 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002268 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002269 break;
2270
Douglas Gregorec0b8292009-04-15 23:02:49 +00002271 case pch::EXPR_VA_ARG:
2272 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002273 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002274 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002275
2276 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002277 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002278 break;
2279
2280 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002281 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002282 break;
2283
2284 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002285 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002286 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002287
2288 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002289 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002290 break;
2291
2292 case pch::EXPR_BLOCK_DECL_REF:
2293 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002294 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002295 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002296 }
2297
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002298 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002299 if (Finished)
2300 break;
2301
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002302 if (S) {
2303 unsigned NumSubStmts = Reader.Visit(S);
2304 while (NumSubStmts > 0) {
2305 StmtStack.pop_back();
2306 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002307 }
2308 }
2309
2310 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002311 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002312 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002313 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
2314 return StmtStack.back();
2315}
2316
2317Expr *PCHReader::ReadExpr() {
2318 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002319}
2320
Douglas Gregor179cfb12009-04-10 20:39:37 +00002321DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002322 return Diag(SourceLocation(), DiagID);
2323}
2324
2325DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2326 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002327 Context.getSourceManager()),
2328 DiagID);
2329}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002330
2331/// \brief Record that the given ID maps to the given switch-case
2332/// statement.
2333void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2334 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2335 SwitchCaseStmts[ID] = SC;
2336}
2337
2338/// \brief Retrieve the switch-case statement with the given ID.
2339SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2340 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2341 return SwitchCaseStmts[ID];
2342}