blob: bdadd35e3f22f98377316e937040320c30f37821 [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor0a0428e2009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorcb70bb22009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorcb70bb22009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregor2cf26342009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
70 };
71}
72
73void PCHDeclReader::VisitDecl(Decl *D) {
74 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLexicalDeclContext(
76 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
77 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
78 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +000079 if (Record[Idx++])
80 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +000081 D->setImplicit(Record[Idx++]);
82 D->setAccess((AccessSpecifier)Record[Idx++]);
83}
84
85void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
86 VisitDecl(TU);
87}
88
89void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
90 VisitDecl(ND);
91 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
92}
93
94void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
95 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000096 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
97}
98
99void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +0000100 // Note that we cannot use VisitTypeDecl here, because we need to
101 // set the underlying type of the typedef *before* we try to read
102 // the type associated with the TypedefDecl.
103 VisitNamedDecl(TD);
104 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
105 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
106 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000109void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
110 VisitTypeDecl(TD);
111 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
112 TD->setDefinition(Record[Idx++]);
113 TD->setTypedefForAnonDecl(
114 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
115}
116
117void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
118 VisitTagDecl(ED);
119 ED->setIntegerType(Reader.GetType(Record[Idx++]));
120}
121
Douglas Gregor8c700062009-04-13 21:20:57 +0000122void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
123 VisitTagDecl(RD);
124 RD->setHasFlexibleArrayMember(Record[Idx++]);
125 RD->setAnonymousStructOrUnion(Record[Idx++]);
126}
127
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
129 VisitNamedDecl(VD);
130 VD->setType(Reader.GetType(Record[Idx++]));
131}
132
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000133void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
134 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000135 if (Record[Idx++])
136 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000137 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
138}
139
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000140void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
141 VisitValueDecl(FD);
Douglas Gregor025452f2009-04-17 00:04:06 +0000142 if (Record[Idx++])
143 FD->setBody(cast<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000144 FD->setPreviousDeclaration(
145 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
146 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
147 FD->setInline(Record[Idx++]);
148 FD->setVirtual(Record[Idx++]);
149 FD->setPure(Record[Idx++]);
150 FD->setInheritedPrototype(Record[Idx++]);
151 FD->setHasPrototype(Record[Idx++]);
152 FD->setDeleted(Record[Idx++]);
153 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
154 unsigned NumParams = Record[Idx++];
155 llvm::SmallVector<ParmVarDecl *, 16> Params;
156 Params.reserve(NumParams);
157 for (unsigned I = 0; I != NumParams; ++I)
158 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
159 FD->setParams(Reader.getContext(), &Params[0], NumParams);
160}
161
Douglas Gregor8c700062009-04-13 21:20:57 +0000162void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
163 VisitValueDecl(FD);
164 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000165 if (Record[Idx++])
166 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000167}
168
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
170 VisitValueDecl(VD);
171 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
172 VD->setThreadSpecified(Record[Idx++]);
173 VD->setCXXDirectInitializer(Record[Idx++]);
174 VD->setDeclaredInCondition(Record[Idx++]);
175 VD->setPreviousDeclaration(
176 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
177 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000178 if (Record[Idx++])
179 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000182void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
183 VisitVarDecl(PD);
184 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000185 // FIXME: default argument (C++ only)
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000186}
187
188void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
189 VisitParmVarDecl(PD);
190 PD->setOriginalType(Reader.GetType(Record[Idx++]));
191}
192
Douglas Gregor1028bc62009-04-13 22:49:25 +0000193void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
194 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000195 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000196}
197
198void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
199 VisitDecl(BD);
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 Gregor2cf26342009-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 Gregor0b748912009-04-14 21:18:50 +0000217//===----------------------------------------------------------------------===//
218// Statement/expression deserialization
219//===----------------------------------------------------------------------===//
220namespace {
221 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000222 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000223 PCHReader &Reader;
224 const PCHReader::RecordData &Record;
225 unsigned &Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +0000226 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000227
228 public:
229 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc9490c02009-04-16 22:23:12 +0000230 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
231 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000232
Douglas Gregor025452f2009-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 Gregor673ecd62009-04-15 16:35:07 +0000237 /// \brief The number of record fields required for the Expr class
238 /// itself.
Douglas Gregor025452f2009-04-17 00:04:06 +0000239 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000240
Douglas Gregor087fd532009-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 Gregor025452f2009-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 Gregord921cf92009-04-17 00:16:09 +0000254 unsigned VisitWhileStmt(WhileStmt *S);
255 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor025452f2009-04-17 00:04:06 +0000256 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000257 unsigned VisitExpr(Expr *E);
258 unsigned VisitPredefinedExpr(PredefinedExpr *E);
259 unsigned VisitDeclRefExpr(DeclRefExpr *E);
260 unsigned VisitIntegerLiteral(IntegerLiteral *E);
261 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000262 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000263 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000264 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000265 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000266 unsigned VisitUnaryOperator(UnaryOperator *E);
267 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000268 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000269 unsigned VisitCallExpr(CallExpr *E);
270 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000271 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000272 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000273 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
274 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000275 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000276 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
277 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000278 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000279 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000280 unsigned VisitInitListExpr(InitListExpr *E);
281 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
282 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000283 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000284 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
285 unsigned VisitChooseExpr(ChooseExpr *E);
286 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000287 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
288 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000289 };
290}
291
Douglas Gregor025452f2009-04-17 00:04:06 +0000292unsigned PCHStmtReader::VisitStmt(Stmt *S) {
293 assert(Idx == NumStmtFields && "Incorrect statement field count");
294 return 0;
295}
296
297unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
298 VisitStmt(S);
299 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
300 return 0;
301}
302
303unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
304 VisitStmt(S);
305 unsigned NumStmts = Record[Idx++];
306 S->setStmts(Reader.getContext(),
307 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
308 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
309 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
310 return NumStmts;
311}
312
313unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
314 VisitStmt(S);
315 Reader.RecordSwitchCaseID(S, Record[Idx++]);
316 return 0;
317}
318
319unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
320 VisitSwitchCase(S);
321 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
322 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
323 S->setSubStmt(StmtStack.back());
324 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
325 return 3;
326}
327
328unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
329 VisitSwitchCase(S);
330 S->setSubStmt(StmtStack.back());
331 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
332 return 1;
333}
334
335unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
336 VisitStmt(S);
337 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
338 S->setThen(StmtStack[StmtStack.size() - 2]);
339 S->setElse(StmtStack[StmtStack.size() - 1]);
340 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
341 return 3;
342}
343
344unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
345 VisitStmt(S);
346 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
347 S->setBody(StmtStack.back());
348 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
349 SwitchCase *PrevSC = 0;
350 for (unsigned N = Record.size(); Idx != N; ++Idx) {
351 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
352 if (PrevSC)
353 PrevSC->setNextSwitchCase(SC);
354 else
355 S->setSwitchCaseList(SC);
356 PrevSC = SC;
357 }
358 return 2;
359}
360
Douglas Gregord921cf92009-04-17 00:16:09 +0000361unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
362 VisitStmt(S);
363 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
364 S->setBody(StmtStack.back());
365 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
366 return 2;
367}
368
369unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
370 VisitStmt(S);
371 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
372 return 0;
373}
374
Douglas Gregor025452f2009-04-17 00:04:06 +0000375unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
376 VisitStmt(S);
377 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
378 return 0;
379}
380
Douglas Gregor087fd532009-04-14 23:32:43 +0000381unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000382 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000383 E->setType(Reader.GetType(Record[Idx++]));
384 E->setTypeDependent(Record[Idx++]);
385 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000386 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000387 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000388}
389
Douglas Gregor087fd532009-04-14 23:32:43 +0000390unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000391 VisitExpr(E);
392 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
393 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000394 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000395}
396
Douglas Gregor087fd532009-04-14 23:32:43 +0000397unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000398 VisitExpr(E);
399 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
400 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000401 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000402}
403
Douglas Gregor087fd532009-04-14 23:32:43 +0000404unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000405 VisitExpr(E);
406 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
407 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000408 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000409}
410
Douglas Gregor087fd532009-04-14 23:32:43 +0000411unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000412 VisitExpr(E);
413 E->setValue(Reader.ReadAPFloat(Record, Idx));
414 E->setExact(Record[Idx++]);
415 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000416 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000417}
418
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000419unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
420 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000421 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000422 return 1;
423}
424
Douglas Gregor673ecd62009-04-15 16:35:07 +0000425unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
426 VisitExpr(E);
427 unsigned Len = Record[Idx++];
428 assert(Record[Idx] == E->getNumConcatenated() &&
429 "Wrong number of concatenated tokens!");
430 ++Idx;
431 E->setWide(Record[Idx++]);
432
433 // Read string data
434 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
435 E->setStrData(Reader.getContext(), &Str[0], Len);
436 Idx += Len;
437
438 // Read source locations
439 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
440 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
441
442 return 0;
443}
444
Douglas Gregor087fd532009-04-14 23:32:43 +0000445unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000446 VisitExpr(E);
447 E->setValue(Record[Idx++]);
448 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
449 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000450 return 0;
451}
452
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000453unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
454 VisitExpr(E);
455 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
456 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000457 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000458 return 1;
459}
460
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000461unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
462 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000463 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000464 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
465 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
466 return 1;
467}
468
469unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
470 VisitExpr(E);
471 E->setSizeof(Record[Idx++]);
472 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000473 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000474 ++Idx;
475 } else {
476 E->setArgument(Reader.GetType(Record[Idx++]));
477 }
478 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
479 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
480 return E->isArgumentType()? 0 : 1;
481}
482
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000483unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
484 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000485 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
486 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000487 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
488 return 2;
489}
490
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000491unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
492 VisitExpr(E);
493 E->setNumArgs(Reader.getContext(), Record[Idx++]);
494 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000495 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000496 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000497 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000498 return E->getNumArgs() + 1;
499}
500
501unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
502 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000503 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000504 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
505 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
506 E->setArrow(Record[Idx++]);
507 return 1;
508}
509
Douglas Gregor087fd532009-04-14 23:32:43 +0000510unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
511 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000512 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000513 return 1;
514}
515
Douglas Gregordb600c32009-04-15 00:25:59 +0000516unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
517 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000518 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
519 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000520 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
521 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
522 return 2;
523}
524
Douglas Gregorad90e962009-04-15 22:40:36 +0000525unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
526 VisitBinaryOperator(E);
527 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
528 E->setComputationResultType(Reader.GetType(Record[Idx++]));
529 return 2;
530}
531
532unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
533 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000534 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
535 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
536 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000537 return 3;
538}
539
Douglas Gregor087fd532009-04-14 23:32:43 +0000540unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
541 VisitCastExpr(E);
542 E->setLvalueCast(Record[Idx++]);
543 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000544}
545
Douglas Gregordb600c32009-04-15 00:25:59 +0000546unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
547 VisitCastExpr(E);
548 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
549 return 1;
550}
551
552unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
553 VisitExplicitCastExpr(E);
554 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
555 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 return 1;
557}
558
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000559unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
560 VisitExpr(E);
561 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000562 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000563 E->setFileScope(Record[Idx++]);
564 return 1;
565}
566
Douglas Gregord3c98a02009-04-15 23:02:49 +0000567unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
568 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000569 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000570 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
571 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
572 return 1;
573}
574
Douglas Gregord077d752009-04-16 00:55:48 +0000575unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
576 VisitExpr(E);
577 unsigned NumInits = Record[Idx++];
578 E->reserveInits(NumInits);
579 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000580 E->updateInit(I,
581 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
582 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000583 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
584 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
585 E->setInitializedFieldInUnion(
586 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
587 E->sawArrayRangeDesignator(Record[Idx++]);
588 return NumInits + 1;
589}
590
591unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
592 typedef DesignatedInitExpr::Designator Designator;
593
594 VisitExpr(E);
595 unsigned NumSubExprs = Record[Idx++];
596 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
597 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000598 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000599 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
600 E->setGNUSyntax(Record[Idx++]);
601
602 llvm::SmallVector<Designator, 4> Designators;
603 while (Idx < Record.size()) {
604 switch ((pch::DesignatorTypes)Record[Idx++]) {
605 case pch::DESIG_FIELD_DECL: {
606 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
607 SourceLocation DotLoc
608 = SourceLocation::getFromRawEncoding(Record[Idx++]);
609 SourceLocation FieldLoc
610 = SourceLocation::getFromRawEncoding(Record[Idx++]);
611 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
612 FieldLoc));
613 Designators.back().setField(Field);
614 break;
615 }
616
617 case pch::DESIG_FIELD_NAME: {
618 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
619 SourceLocation DotLoc
620 = SourceLocation::getFromRawEncoding(Record[Idx++]);
621 SourceLocation FieldLoc
622 = SourceLocation::getFromRawEncoding(Record[Idx++]);
623 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
624 break;
625 }
626
627 case pch::DESIG_ARRAY: {
628 unsigned Index = Record[Idx++];
629 SourceLocation LBracketLoc
630 = SourceLocation::getFromRawEncoding(Record[Idx++]);
631 SourceLocation RBracketLoc
632 = SourceLocation::getFromRawEncoding(Record[Idx++]);
633 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
634 break;
635 }
636
637 case pch::DESIG_ARRAY_RANGE: {
638 unsigned Index = Record[Idx++];
639 SourceLocation LBracketLoc
640 = SourceLocation::getFromRawEncoding(Record[Idx++]);
641 SourceLocation EllipsisLoc
642 = SourceLocation::getFromRawEncoding(Record[Idx++]);
643 SourceLocation RBracketLoc
644 = SourceLocation::getFromRawEncoding(Record[Idx++]);
645 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
646 RBracketLoc));
647 break;
648 }
649 }
650 }
651 E->setDesignators(&Designators[0], Designators.size());
652
653 return NumSubExprs;
654}
655
656unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
657 VisitExpr(E);
658 return 0;
659}
660
Douglas Gregord3c98a02009-04-15 23:02:49 +0000661unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
662 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000663 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000664 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
665 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
666 return 1;
667}
668
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000669unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
670 VisitExpr(E);
671 E->setArgType1(Reader.GetType(Record[Idx++]));
672 E->setArgType2(Reader.GetType(Record[Idx++]));
673 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
674 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
675 return 0;
676}
677
678unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
679 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000680 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
681 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
682 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000683 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
684 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
685 return 3;
686}
687
688unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
689 VisitExpr(E);
690 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
691 return 0;
692}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000693
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000694unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
695 VisitExpr(E);
696 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000697 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000698 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
699 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
700 return NumExprs;
701}
702
703unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
704 VisitExpr(E);
705 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
706 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
707 E->setByRef(Record[Idx++]);
708 return 0;
709}
710
Douglas Gregor2cf26342009-04-09 22:27:44 +0000711// FIXME: use the diagnostics machinery
712static bool Error(const char *Str) {
713 std::fprintf(stderr, "%s\n", Str);
714 return true;
715}
716
Douglas Gregore1d918e2009-04-10 23:10:45 +0000717/// \brief Check the contents of the predefines buffer against the
718/// contents of the predefines buffer used to build the PCH file.
719///
720/// The contents of the two predefines buffers should be the same. If
721/// not, then some command-line option changed the preprocessor state
722/// and we must reject the PCH file.
723///
724/// \param PCHPredef The start of the predefines buffer in the PCH
725/// file.
726///
727/// \param PCHPredefLen The length of the predefines buffer in the PCH
728/// file.
729///
730/// \param PCHBufferID The FileID for the PCH predefines buffer.
731///
732/// \returns true if there was a mismatch (in which case the PCH file
733/// should be ignored), or false otherwise.
734bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
735 unsigned PCHPredefLen,
736 FileID PCHBufferID) {
737 const char *Predef = PP.getPredefines().c_str();
738 unsigned PredefLen = PP.getPredefines().size();
739
740 // If the two predefines buffers compare equal, we're done!.
741 if (PredefLen == PCHPredefLen &&
742 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
743 return false;
744
745 // The predefines buffers are different. Produce a reasonable
746 // diagnostic showing where they are different.
747
748 // The source locations (potentially in the two different predefines
749 // buffers)
750 SourceLocation Loc1, Loc2;
751 SourceManager &SourceMgr = PP.getSourceManager();
752
753 // Create a source buffer for our predefines string, so
754 // that we can build a diagnostic that points into that
755 // source buffer.
756 FileID BufferID;
757 if (Predef && Predef[0]) {
758 llvm::MemoryBuffer *Buffer
759 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
760 "<built-in>");
761 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
762 }
763
764 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
765 std::pair<const char *, const char *> Locations
766 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
767
768 if (Locations.first != Predef + MinLen) {
769 // We found the location in the two buffers where there is a
770 // difference. Form source locations to point there (in both
771 // buffers).
772 unsigned Offset = Locations.first - Predef;
773 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
774 .getFileLocWithOffset(Offset);
775 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
776 .getFileLocWithOffset(Offset);
777 } else if (PredefLen > PCHPredefLen) {
778 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
779 .getFileLocWithOffset(MinLen);
780 } else {
781 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
782 .getFileLocWithOffset(MinLen);
783 }
784
785 Diag(Loc1, diag::warn_pch_preprocessor);
786 if (Loc2.isValid())
787 Diag(Loc2, diag::note_predef_in_pch);
788 Diag(diag::note_ignoring_pch) << FileName;
789 return true;
790}
791
Douglas Gregorbd945002009-04-13 16:31:14 +0000792/// \brief Read the line table in the source manager block.
793/// \returns true if ther was an error.
794static bool ParseLineTable(SourceManager &SourceMgr,
795 llvm::SmallVectorImpl<uint64_t> &Record) {
796 unsigned Idx = 0;
797 LineTableInfo &LineTable = SourceMgr.getLineTable();
798
799 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000800 std::map<int, int> FileIDs;
801 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000802 // Extract the file name
803 unsigned FilenameLen = Record[Idx++];
804 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
805 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000806 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
807 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000808 }
809
810 // Parse the line entries
811 std::vector<LineEntry> Entries;
812 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000813 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000814
815 // Extract the line entries
816 unsigned NumEntries = Record[Idx++];
817 Entries.clear();
818 Entries.reserve(NumEntries);
819 for (unsigned I = 0; I != NumEntries; ++I) {
820 unsigned FileOffset = Record[Idx++];
821 unsigned LineNo = Record[Idx++];
822 int FilenameID = Record[Idx++];
823 SrcMgr::CharacteristicKind FileKind
824 = (SrcMgr::CharacteristicKind)Record[Idx++];
825 unsigned IncludeOffset = Record[Idx++];
826 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
827 FileKind, IncludeOffset));
828 }
829 LineTable.AddEntry(FID, Entries);
830 }
831
832 return false;
833}
834
Douglas Gregor14f79002009-04-10 03:52:48 +0000835/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000836PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000837 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000838 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
839 Error("Malformed source manager block record");
840 return Failure;
841 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000842
843 SourceManager &SourceMgr = Context.getSourceManager();
844 RecordData Record;
845 while (true) {
846 unsigned Code = Stream.ReadCode();
847 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000848 if (Stream.ReadBlockEnd()) {
849 Error("Error at end of Source Manager block");
850 return Failure;
851 }
852
853 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000854 }
855
856 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
857 // No known subblocks, always skip them.
858 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000859 if (Stream.SkipBlock()) {
860 Error("Malformed block record");
861 return Failure;
862 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000863 continue;
864 }
865
866 if (Code == llvm::bitc::DEFINE_ABBREV) {
867 Stream.ReadAbbrevRecord();
868 continue;
869 }
870
871 // Read a record.
872 const char *BlobStart;
873 unsigned BlobLen;
874 Record.clear();
875 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
876 default: // Default behavior: ignore.
877 break;
878
879 case pch::SM_SLOC_FILE_ENTRY: {
880 // FIXME: We would really like to delay the creation of this
881 // FileEntry until it is actually required, e.g., when producing
882 // a diagnostic with a source location in this file.
883 const FileEntry *File
884 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
885 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +0000886 FileID ID = SourceMgr.createFileID(File,
887 SourceLocation::getFromRawEncoding(Record[1]),
888 (CharacteristicKind)Record[2]);
889 if (Record[3])
890 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
891 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +0000892 break;
893 }
894
895 case pch::SM_SLOC_BUFFER_ENTRY: {
896 const char *Name = BlobStart;
897 unsigned Code = Stream.ReadCode();
898 Record.clear();
899 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
900 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000901 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000902 llvm::MemoryBuffer *Buffer
903 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
904 BlobStart + BlobLen - 1,
905 Name);
906 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
907
908 if (strcmp(Name, "<built-in>") == 0
909 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
910 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +0000911 break;
912 }
913
914 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
915 SourceLocation SpellingLoc
916 = SourceLocation::getFromRawEncoding(Record[1]);
917 SourceMgr.createInstantiationLoc(
918 SpellingLoc,
919 SourceLocation::getFromRawEncoding(Record[2]),
920 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +0000921 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +0000922 break;
923 }
924
Chris Lattner2c78b872009-04-14 23:22:57 +0000925 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +0000926 if (ParseLineTable(SourceMgr, Record))
927 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000928 break;
Douglas Gregor14f79002009-04-10 03:52:48 +0000929 }
930 }
931}
932
Chris Lattner42d42b52009-04-10 21:41:48 +0000933bool PCHReader::ReadPreprocessorBlock() {
934 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
935 return Error("Malformed preprocessor block record");
936
Chris Lattner42d42b52009-04-10 21:41:48 +0000937 RecordData Record;
938 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
939 MacroInfo *LastMacro = 0;
940
941 while (true) {
942 unsigned Code = Stream.ReadCode();
943 switch (Code) {
944 case llvm::bitc::END_BLOCK:
945 if (Stream.ReadBlockEnd())
946 return Error("Error at end of preprocessor block");
947 return false;
948
949 case llvm::bitc::ENTER_SUBBLOCK:
950 // No known subblocks, always skip them.
951 Stream.ReadSubBlockID();
952 if (Stream.SkipBlock())
953 return Error("Malformed block record");
954 continue;
955
956 case llvm::bitc::DEFINE_ABBREV:
957 Stream.ReadAbbrevRecord();
958 continue;
959 default: break;
960 }
961
962 // Read a record.
963 Record.clear();
964 pch::PreprocessorRecordTypes RecType =
965 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
966 switch (RecType) {
967 default: // Default behavior: ignore unknown records.
968 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000969 case pch::PP_COUNTER_VALUE:
970 if (!Record.empty())
971 PP.setCounterValue(Record[0]);
972 break;
973
Chris Lattner42d42b52009-04-10 21:41:48 +0000974 case pch::PP_MACRO_OBJECT_LIKE:
975 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +0000976 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
977 if (II == 0)
978 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +0000979 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
980 bool isUsed = Record[2];
981
982 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
983 MI->setIsUsed(isUsed);
984
985 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
986 // Decode function-like macro info.
987 bool isC99VarArgs = Record[3];
988 bool isGNUVarArgs = Record[4];
989 MacroArgs.clear();
990 unsigned NumArgs = Record[5];
991 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +0000992 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +0000993
994 // Install function-like macro info.
995 MI->setIsFunctionLike();
996 if (isC99VarArgs) MI->setIsC99Varargs();
997 if (isGNUVarArgs) MI->setIsGNUVarargs();
998 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
999 PP.getPreprocessorAllocator());
1000 }
1001
1002 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +00001003 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +00001004
1005 // Remember that we saw this macro last so that we add the tokens that
1006 // form its body to it.
1007 LastMacro = MI;
1008 break;
1009 }
1010
1011 case pch::PP_TOKEN: {
1012 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1013 // pretend we didn't see this.
1014 if (LastMacro == 0) break;
1015
1016 Token Tok;
1017 Tok.startToken();
1018 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1019 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001020 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1021 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001022 Tok.setKind((tok::TokenKind)Record[3]);
1023 Tok.setFlag((Token::TokenFlags)Record[4]);
1024 LastMacro->AddTokenToBody(Tok);
1025 break;
1026 }
1027 }
1028 }
1029}
1030
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001031PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1032 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1033 Error("Malformed block record");
1034 return Failure;
1035 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001036
Chris Lattner7356a312009-04-11 21:15:38 +00001037 uint64_t PreprocessorBlockBit = 0;
1038
Douglas Gregor2cf26342009-04-09 22:27:44 +00001039 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001040 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001041 while (!Stream.AtEndOfStream()) {
1042 unsigned Code = Stream.ReadCode();
1043 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001044 // If we saw the preprocessor block, read it now.
1045 if (PreprocessorBlockBit) {
1046 uint64_t SavedPos = Stream.GetCurrentBitNo();
1047 Stream.JumpToBit(PreprocessorBlockBit);
1048 if (ReadPreprocessorBlock()) {
1049 Error("Malformed preprocessor block");
1050 return Failure;
1051 }
1052 Stream.JumpToBit(SavedPos);
1053 }
1054
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001055 if (Stream.ReadBlockEnd()) {
1056 Error("Error at end of module block");
1057 return Failure;
1058 }
Chris Lattner7356a312009-04-11 21:15:38 +00001059
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001060 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001061 }
1062
1063 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1064 switch (Stream.ReadSubBlockID()) {
1065 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1066 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1067 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068 if (Stream.SkipBlock()) {
1069 Error("Malformed block record");
1070 return Failure;
1071 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001072 break;
1073
Chris Lattner7356a312009-04-11 21:15:38 +00001074 case pch::PREPROCESSOR_BLOCK_ID:
1075 // Skip the preprocessor block for now, but remember where it is. We
1076 // want to read it in after the identifier table.
1077 if (PreprocessorBlockBit) {
1078 Error("Multiple preprocessor blocks found.");
1079 return Failure;
1080 }
1081 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1082 if (Stream.SkipBlock()) {
1083 Error("Malformed block record");
1084 return Failure;
1085 }
1086 break;
1087
Douglas Gregor14f79002009-04-10 03:52:48 +00001088 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001089 switch (ReadSourceManagerBlock()) {
1090 case Success:
1091 break;
1092
1093 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001094 Error("Malformed source manager block");
1095 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001096
1097 case IgnorePCH:
1098 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001099 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001100 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001101 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001102 continue;
1103 }
1104
1105 if (Code == llvm::bitc::DEFINE_ABBREV) {
1106 Stream.ReadAbbrevRecord();
1107 continue;
1108 }
1109
1110 // Read and process a record.
1111 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001112 const char *BlobStart = 0;
1113 unsigned BlobLen = 0;
1114 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1115 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001116 default: // Default behavior: ignore.
1117 break;
1118
1119 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001120 if (!TypeOffsets.empty()) {
1121 Error("Duplicate TYPE_OFFSET record in PCH file");
1122 return Failure;
1123 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001124 TypeOffsets.swap(Record);
1125 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1126 break;
1127
1128 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001129 if (!DeclOffsets.empty()) {
1130 Error("Duplicate DECL_OFFSET record in PCH file");
1131 return Failure;
1132 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001133 DeclOffsets.swap(Record);
1134 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1135 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001136
1137 case pch::LANGUAGE_OPTIONS:
1138 if (ParseLanguageOptions(Record))
1139 return IgnorePCH;
1140 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001141
Douglas Gregorafaf3082009-04-11 00:14:32 +00001142 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001143 std::string TargetTriple(BlobStart, BlobLen);
1144 if (TargetTriple != Context.Target.getTargetTriple()) {
1145 Diag(diag::warn_pch_target_triple)
1146 << TargetTriple << Context.Target.getTargetTriple();
1147 Diag(diag::note_ignoring_pch) << FileName;
1148 return IgnorePCH;
1149 }
1150 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001151 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001152
1153 case pch::IDENTIFIER_TABLE:
1154 IdentifierTable = BlobStart;
1155 break;
1156
1157 case pch::IDENTIFIER_OFFSET:
1158 if (!IdentifierData.empty()) {
1159 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1160 return Failure;
1161 }
1162 IdentifierData.swap(Record);
1163#ifndef NDEBUG
1164 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1165 if ((IdentifierData[I] & 0x01) == 0) {
1166 Error("Malformed identifier table in the precompiled header");
1167 return Failure;
1168 }
1169 }
1170#endif
1171 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001172
1173 case pch::EXTERNAL_DEFINITIONS:
1174 if (!ExternalDefinitions.empty()) {
1175 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1176 return Failure;
1177 }
1178 ExternalDefinitions.swap(Record);
1179 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001180 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001181 }
1182
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001183 Error("Premature end of bitstream");
1184 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001185}
1186
Douglas Gregore1d918e2009-04-10 23:10:45 +00001187PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001188 // Set the PCH file name.
1189 this->FileName = FileName;
1190
Douglas Gregor2cf26342009-04-09 22:27:44 +00001191 // Open the PCH file.
1192 std::string ErrStr;
1193 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001194 if (!Buffer) {
1195 Error(ErrStr.c_str());
1196 return IgnorePCH;
1197 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001198
1199 // Initialize the stream
1200 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1201 (const unsigned char *)Buffer->getBufferEnd());
1202
1203 // Sniff for the signature.
1204 if (Stream.Read(8) != 'C' ||
1205 Stream.Read(8) != 'P' ||
1206 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001207 Stream.Read(8) != 'H') {
1208 Error("Not a PCH file");
1209 return IgnorePCH;
1210 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001211
1212 // We expect a number of well-defined blocks, though we don't necessarily
1213 // need to understand them all.
1214 while (!Stream.AtEndOfStream()) {
1215 unsigned Code = Stream.ReadCode();
1216
Douglas Gregore1d918e2009-04-10 23:10:45 +00001217 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1218 Error("Invalid record at top-level");
1219 return Failure;
1220 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001221
1222 unsigned BlockID = Stream.ReadSubBlockID();
1223
1224 // We only know the PCH subblock ID.
1225 switch (BlockID) {
1226 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001227 if (Stream.ReadBlockInfoBlock()) {
1228 Error("Malformed BlockInfoBlock");
1229 return Failure;
1230 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001231 break;
1232 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001233 switch (ReadPCHBlock()) {
1234 case Success:
1235 break;
1236
1237 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001238 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001239
1240 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001241 // FIXME: We could consider reading through to the end of this
1242 // PCH block, skipping subblocks, to see if there are other
1243 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001244 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001245 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001246 break;
1247 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001248 if (Stream.SkipBlock()) {
1249 Error("Malformed block record");
1250 return Failure;
1251 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001252 break;
1253 }
1254 }
1255
1256 // Load the translation unit declaration
1257 ReadDeclRecord(DeclOffsets[0], 0);
1258
Douglas Gregore1d918e2009-04-10 23:10:45 +00001259 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001260}
1261
Douglas Gregor0b748912009-04-14 21:18:50 +00001262namespace {
1263 /// \brief Helper class that saves the current stream position and
1264 /// then restores it when destroyed.
1265 struct VISIBILITY_HIDDEN SavedStreamPosition {
1266 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001267 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001268
1269 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001270 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001271 }
1272
1273 private:
1274 llvm::BitstreamReader &Stream;
1275 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001276 };
1277}
1278
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001279/// \brief Parse the record that corresponds to a LangOptions data
1280/// structure.
1281///
1282/// This routine compares the language options used to generate the
1283/// PCH file against the language options set for the current
1284/// compilation. For each option, we classify differences between the
1285/// two compiler states as either "benign" or "important". Benign
1286/// differences don't matter, and we accept them without complaint
1287/// (and without modifying the language options). Differences between
1288/// the states for important options cause the PCH file to be
1289/// unusable, so we emit a warning and return true to indicate that
1290/// there was an error.
1291///
1292/// \returns true if the PCH file is unacceptable, false otherwise.
1293bool PCHReader::ParseLanguageOptions(
1294 const llvm::SmallVectorImpl<uint64_t> &Record) {
1295 const LangOptions &LangOpts = Context.getLangOptions();
1296#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1297#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1298 if (Record[Idx] != LangOpts.Option) { \
1299 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1300 Diag(diag::note_ignoring_pch) << FileName; \
1301 return true; \
1302 } \
1303 ++Idx
1304
1305 unsigned Idx = 0;
1306 PARSE_LANGOPT_BENIGN(Trigraphs);
1307 PARSE_LANGOPT_BENIGN(BCPLComment);
1308 PARSE_LANGOPT_BENIGN(DollarIdents);
1309 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1310 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1311 PARSE_LANGOPT_BENIGN(ImplicitInt);
1312 PARSE_LANGOPT_BENIGN(Digraphs);
1313 PARSE_LANGOPT_BENIGN(HexFloats);
1314 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1315 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1316 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1317 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1318 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1319 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1320 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1321 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1322 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1323 PARSE_LANGOPT_BENIGN(PascalStrings);
1324 PARSE_LANGOPT_BENIGN(Boolean);
1325 PARSE_LANGOPT_BENIGN(WritableStrings);
1326 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1327 diag::warn_pch_lax_vector_conversions);
1328 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1329 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1330 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1331 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1332 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1333 diag::warn_pch_thread_safe_statics);
1334 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1335 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1336 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1337 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1338 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1339 diag::warn_pch_heinous_extensions);
1340 // FIXME: Most of the options below are benign if the macro wasn't
1341 // used. Unfortunately, this means that a PCH compiled without
1342 // optimization can't be used with optimization turned on, even
1343 // though the only thing that changes is whether __OPTIMIZE__ was
1344 // defined... but if __OPTIMIZE__ never showed up in the header, it
1345 // doesn't matter. We could consider making this some special kind
1346 // of check.
1347 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1348 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1349 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1350 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1351 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1352 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1353 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1354 Diag(diag::warn_pch_gc_mode)
1355 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1356 Diag(diag::note_ignoring_pch) << FileName;
1357 return true;
1358 }
1359 ++Idx;
1360 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1361 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1362#undef PARSE_LANGOPT_IRRELEVANT
1363#undef PARSE_LANGOPT_BENIGN
1364
1365 return false;
1366}
1367
Douglas Gregor2cf26342009-04-09 22:27:44 +00001368/// \brief Read and return the type at the given offset.
1369///
1370/// This routine actually reads the record corresponding to the type
1371/// at the given offset in the bitstream. It is a helper routine for
1372/// GetType, which deals with reading type IDs.
1373QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001374 // Keep track of where we are in the stream, then jump back there
1375 // after reading this type.
1376 SavedStreamPosition SavedPosition(Stream);
1377
Douglas Gregor2cf26342009-04-09 22:27:44 +00001378 Stream.JumpToBit(Offset);
1379 RecordData Record;
1380 unsigned Code = Stream.ReadCode();
1381 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001382 case pch::TYPE_EXT_QUAL: {
1383 assert(Record.size() == 3 &&
1384 "Incorrect encoding of extended qualifier type");
1385 QualType Base = GetType(Record[0]);
1386 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1387 unsigned AddressSpace = Record[2];
1388
1389 QualType T = Base;
1390 if (GCAttr != QualType::GCNone)
1391 T = Context.getObjCGCQualType(T, GCAttr);
1392 if (AddressSpace)
1393 T = Context.getAddrSpaceQualType(T, AddressSpace);
1394 return T;
1395 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001396
Douglas Gregor2cf26342009-04-09 22:27:44 +00001397 case pch::TYPE_FIXED_WIDTH_INT: {
1398 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1399 return Context.getFixedWidthIntType(Record[0], Record[1]);
1400 }
1401
1402 case pch::TYPE_COMPLEX: {
1403 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1404 QualType ElemType = GetType(Record[0]);
1405 return Context.getComplexType(ElemType);
1406 }
1407
1408 case pch::TYPE_POINTER: {
1409 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1410 QualType PointeeType = GetType(Record[0]);
1411 return Context.getPointerType(PointeeType);
1412 }
1413
1414 case pch::TYPE_BLOCK_POINTER: {
1415 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1416 QualType PointeeType = GetType(Record[0]);
1417 return Context.getBlockPointerType(PointeeType);
1418 }
1419
1420 case pch::TYPE_LVALUE_REFERENCE: {
1421 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1422 QualType PointeeType = GetType(Record[0]);
1423 return Context.getLValueReferenceType(PointeeType);
1424 }
1425
1426 case pch::TYPE_RVALUE_REFERENCE: {
1427 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1428 QualType PointeeType = GetType(Record[0]);
1429 return Context.getRValueReferenceType(PointeeType);
1430 }
1431
1432 case pch::TYPE_MEMBER_POINTER: {
1433 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1434 QualType PointeeType = GetType(Record[0]);
1435 QualType ClassType = GetType(Record[1]);
1436 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1437 }
1438
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001439 case pch::TYPE_CONSTANT_ARRAY: {
1440 QualType ElementType = GetType(Record[0]);
1441 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1442 unsigned IndexTypeQuals = Record[2];
1443 unsigned Idx = 3;
1444 llvm::APInt Size = ReadAPInt(Record, Idx);
1445 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1446 }
1447
1448 case pch::TYPE_INCOMPLETE_ARRAY: {
1449 QualType ElementType = GetType(Record[0]);
1450 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1451 unsigned IndexTypeQuals = Record[2];
1452 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1453 }
1454
1455 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001456 QualType ElementType = GetType(Record[0]);
1457 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1458 unsigned IndexTypeQuals = Record[2];
1459 return Context.getVariableArrayType(ElementType, ReadExpr(),
1460 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001461 }
1462
1463 case pch::TYPE_VECTOR: {
1464 if (Record.size() != 2) {
1465 Error("Incorrect encoding of vector type in PCH file");
1466 return QualType();
1467 }
1468
1469 QualType ElementType = GetType(Record[0]);
1470 unsigned NumElements = Record[1];
1471 return Context.getVectorType(ElementType, NumElements);
1472 }
1473
1474 case pch::TYPE_EXT_VECTOR: {
1475 if (Record.size() != 2) {
1476 Error("Incorrect encoding of extended vector type in PCH file");
1477 return QualType();
1478 }
1479
1480 QualType ElementType = GetType(Record[0]);
1481 unsigned NumElements = Record[1];
1482 return Context.getExtVectorType(ElementType, NumElements);
1483 }
1484
1485 case pch::TYPE_FUNCTION_NO_PROTO: {
1486 if (Record.size() != 1) {
1487 Error("Incorrect encoding of no-proto function type");
1488 return QualType();
1489 }
1490 QualType ResultType = GetType(Record[0]);
1491 return Context.getFunctionNoProtoType(ResultType);
1492 }
1493
1494 case pch::TYPE_FUNCTION_PROTO: {
1495 QualType ResultType = GetType(Record[0]);
1496 unsigned Idx = 1;
1497 unsigned NumParams = Record[Idx++];
1498 llvm::SmallVector<QualType, 16> ParamTypes;
1499 for (unsigned I = 0; I != NumParams; ++I)
1500 ParamTypes.push_back(GetType(Record[Idx++]));
1501 bool isVariadic = Record[Idx++];
1502 unsigned Quals = Record[Idx++];
1503 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1504 isVariadic, Quals);
1505 }
1506
1507 case pch::TYPE_TYPEDEF:
1508 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1509 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1510
1511 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001512 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001513
1514 case pch::TYPE_TYPEOF: {
1515 if (Record.size() != 1) {
1516 Error("Incorrect encoding of typeof(type) in PCH file");
1517 return QualType();
1518 }
1519 QualType UnderlyingType = GetType(Record[0]);
1520 return Context.getTypeOfType(UnderlyingType);
1521 }
1522
1523 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001524 assert(Record.size() == 1 && "Incorrect encoding of record type");
1525 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001526
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001527 case pch::TYPE_ENUM:
1528 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1529 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1530
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001531 case pch::TYPE_OBJC_INTERFACE:
1532 // FIXME: Deserialize ObjCInterfaceType
1533 assert(false && "Cannot de-serialize ObjC interface types yet");
1534 return QualType();
1535
1536 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1537 // FIXME: Deserialize ObjCQualifiedInterfaceType
1538 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1539 return QualType();
1540
1541 case pch::TYPE_OBJC_QUALIFIED_ID:
1542 // FIXME: Deserialize ObjCQualifiedIdType
1543 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1544 return QualType();
1545
1546 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1547 // FIXME: Deserialize ObjCQualifiedClassType
1548 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1549 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001550 }
1551
1552 // Suppress a GCC warning
1553 return QualType();
1554}
1555
1556/// \brief Note that we have loaded the declaration with the given
1557/// Index.
1558///
1559/// This routine notes that this declaration has already been loaded,
1560/// so that future GetDecl calls will return this declaration rather
1561/// than trying to load a new declaration.
1562inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1563 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1564 DeclAlreadyLoaded[Index] = true;
1565 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1566}
1567
1568/// \brief Read the declaration at the given offset from the PCH file.
1569Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001570 // Keep track of where we are in the stream, then jump back there
1571 // after reading this declaration.
1572 SavedStreamPosition SavedPosition(Stream);
1573
Douglas Gregor2cf26342009-04-09 22:27:44 +00001574 Decl *D = 0;
1575 Stream.JumpToBit(Offset);
1576 RecordData Record;
1577 unsigned Code = Stream.ReadCode();
1578 unsigned Idx = 0;
1579 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001580
Douglas Gregor2cf26342009-04-09 22:27:44 +00001581 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001582 case pch::DECL_ATTR:
1583 case pch::DECL_CONTEXT_LEXICAL:
1584 case pch::DECL_CONTEXT_VISIBLE:
1585 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1586 break;
1587
Douglas Gregor2cf26342009-04-09 22:27:44 +00001588 case pch::DECL_TRANSLATION_UNIT:
1589 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001590 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001591 break;
1592
1593 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001594 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001595 break;
1596 }
1597
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001598 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001599 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001600 break;
1601 }
1602
Douglas Gregor8c700062009-04-13 21:20:57 +00001603 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001604 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1605 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001606 break;
1607 }
1608
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001609 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001610 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1611 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001612 break;
1613 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001614
1615 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001616 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1617 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001618 break;
1619 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001620
Douglas Gregor8c700062009-04-13 21:20:57 +00001621 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001622 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1623 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001624 break;
1625 }
1626
Douglas Gregor2cf26342009-04-09 22:27:44 +00001627 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001628 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1629 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001630 break;
1631 }
1632
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001633 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001634 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1635 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001636 break;
1637 }
1638
1639 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001640 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001641 QualType(), QualType(), VarDecl::None,
1642 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001643 break;
1644 }
1645
Douglas Gregor1028bc62009-04-13 22:49:25 +00001646 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001647 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001648 break;
1649 }
1650
1651 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001652 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001653 break;
1654 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001655 }
1656
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001657 assert(D && "Unknown declaration creating PCH file");
1658 if (D) {
1659 LoadedDecl(Index, D);
1660 Reader.Visit(D);
1661 }
1662
Douglas Gregor2cf26342009-04-09 22:27:44 +00001663 // If this declaration is also a declaration context, get the
1664 // offsets for its tables of lexical and visible declarations.
1665 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1666 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1667 if (Offsets.first || Offsets.second) {
1668 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1669 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1670 DeclContextOffsets[DC] = Offsets;
1671 }
1672 }
1673 assert(Idx == Record.size());
1674
1675 return D;
1676}
1677
Douglas Gregor8038d512009-04-10 17:25:41 +00001678QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001679 unsigned Quals = ID & 0x07;
1680 unsigned Index = ID >> 3;
1681
1682 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1683 QualType T;
1684 switch ((pch::PredefinedTypeIDs)Index) {
1685 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1686 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1687 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1688
1689 case pch::PREDEF_TYPE_CHAR_U_ID:
1690 case pch::PREDEF_TYPE_CHAR_S_ID:
1691 // FIXME: Check that the signedness of CharTy is correct!
1692 T = Context.CharTy;
1693 break;
1694
1695 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1696 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1697 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1698 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1699 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1700 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1701 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1702 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1703 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1704 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1705 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1706 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1707 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1708 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1709 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1710 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1711 }
1712
1713 assert(!T.isNull() && "Unknown predefined type");
1714 return T.getQualifiedType(Quals);
1715 }
1716
1717 Index -= pch::NUM_PREDEF_TYPE_IDS;
1718 if (!TypeAlreadyLoaded[Index]) {
1719 // Load the type from the PCH file.
1720 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1721 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1722 TypeAlreadyLoaded[Index] = true;
1723 }
1724
1725 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1726}
1727
Douglas Gregor8038d512009-04-10 17:25:41 +00001728Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001729 if (ID == 0)
1730 return 0;
1731
1732 unsigned Index = ID - 1;
1733 if (DeclAlreadyLoaded[Index])
1734 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1735
1736 // Load the declaration from the PCH file.
1737 return ReadDeclRecord(DeclOffsets[Index], Index);
1738}
1739
1740bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001741 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001742 assert(DC->hasExternalLexicalStorage() &&
1743 "DeclContext has no lexical decls in storage");
1744 uint64_t Offset = DeclContextOffsets[DC].first;
1745 assert(Offset && "DeclContext has no lexical decls in storage");
1746
Douglas Gregor0b748912009-04-14 21:18:50 +00001747 // Keep track of where we are in the stream, then jump back there
1748 // after reading this context.
1749 SavedStreamPosition SavedPosition(Stream);
1750
Douglas Gregor2cf26342009-04-09 22:27:44 +00001751 // Load the record containing all of the declarations lexically in
1752 // this context.
1753 Stream.JumpToBit(Offset);
1754 RecordData Record;
1755 unsigned Code = Stream.ReadCode();
1756 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001757 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001758 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1759
1760 // Load all of the declaration IDs
1761 Decls.clear();
1762 Decls.insert(Decls.end(), Record.begin(), Record.end());
1763 return false;
1764}
1765
1766bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1767 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1768 assert(DC->hasExternalVisibleStorage() &&
1769 "DeclContext has no visible decls in storage");
1770 uint64_t Offset = DeclContextOffsets[DC].second;
1771 assert(Offset && "DeclContext has no visible decls in storage");
1772
Douglas Gregor0b748912009-04-14 21:18:50 +00001773 // Keep track of where we are in the stream, then jump back there
1774 // after reading this context.
1775 SavedStreamPosition SavedPosition(Stream);
1776
Douglas Gregor2cf26342009-04-09 22:27:44 +00001777 // Load the record containing all of the declarations visible in
1778 // this context.
1779 Stream.JumpToBit(Offset);
1780 RecordData Record;
1781 unsigned Code = Stream.ReadCode();
1782 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001783 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001784 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1785 if (Record.size() == 0)
1786 return false;
1787
1788 Decls.clear();
1789
1790 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001791 while (Idx < Record.size()) {
1792 Decls.push_back(VisibleDeclaration());
1793 Decls.back().Name = ReadDeclarationName(Record, Idx);
1794
Douglas Gregor2cf26342009-04-09 22:27:44 +00001795 unsigned Size = Record[Idx++];
1796 llvm::SmallVector<unsigned, 4> & LoadedDecls
1797 = Decls.back().Declarations;
1798 LoadedDecls.reserve(Size);
1799 for (unsigned I = 0; I < Size; ++I)
1800 LoadedDecls.push_back(Record[Idx++]);
1801 }
1802
1803 return false;
1804}
1805
Douglas Gregorfdd01722009-04-14 00:24:19 +00001806void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1807 if (!Consumer)
1808 return;
1809
1810 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1811 Decl *D = GetDecl(ExternalDefinitions[I]);
1812 DeclGroupRef DG(D);
1813 Consumer->HandleTopLevelDecl(DG);
1814 }
1815}
1816
Douglas Gregor2cf26342009-04-09 22:27:44 +00001817void PCHReader::PrintStats() {
1818 std::fprintf(stderr, "*** PCH Statistics:\n");
1819
1820 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1821 TypeAlreadyLoaded.end(),
1822 true);
1823 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1824 DeclAlreadyLoaded.end(),
1825 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001826 unsigned NumIdentifiersLoaded = 0;
1827 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1828 if ((IdentifierData[I] & 0x01) == 0)
1829 ++NumIdentifiersLoaded;
1830 }
1831
Douglas Gregor2cf26342009-04-09 22:27:44 +00001832 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1833 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001834 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001835 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1836 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001837 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1838 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1839 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1840 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001841 std::fprintf(stderr, "\n");
1842}
1843
Chris Lattner7356a312009-04-11 21:15:38 +00001844IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001845 if (ID == 0)
1846 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001847
Douglas Gregorafaf3082009-04-11 00:14:32 +00001848 if (!IdentifierTable || IdentifierData.empty()) {
1849 Error("No identifier table in PCH file");
1850 return 0;
1851 }
Chris Lattner7356a312009-04-11 21:15:38 +00001852
Douglas Gregorafaf3082009-04-11 00:14:32 +00001853 if (IdentifierData[ID - 1] & 0x01) {
1854 uint64_t Offset = IdentifierData[ID - 1];
1855 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001856 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001857 }
Chris Lattner7356a312009-04-11 21:15:38 +00001858
1859 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001860}
1861
1862DeclarationName
1863PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1864 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1865 switch (Kind) {
1866 case DeclarationName::Identifier:
1867 return DeclarationName(GetIdentifierInfo(Record, Idx));
1868
1869 case DeclarationName::ObjCZeroArgSelector:
1870 case DeclarationName::ObjCOneArgSelector:
1871 case DeclarationName::ObjCMultiArgSelector:
1872 assert(false && "Unable to de-serialize Objective-C selectors");
1873 break;
1874
1875 case DeclarationName::CXXConstructorName:
1876 return Context.DeclarationNames.getCXXConstructorName(
1877 GetType(Record[Idx++]));
1878
1879 case DeclarationName::CXXDestructorName:
1880 return Context.DeclarationNames.getCXXDestructorName(
1881 GetType(Record[Idx++]));
1882
1883 case DeclarationName::CXXConversionFunctionName:
1884 return Context.DeclarationNames.getCXXConversionFunctionName(
1885 GetType(Record[Idx++]));
1886
1887 case DeclarationName::CXXOperatorName:
1888 return Context.DeclarationNames.getCXXOperatorName(
1889 (OverloadedOperatorKind)Record[Idx++]);
1890
1891 case DeclarationName::CXXUsingDirective:
1892 return DeclarationName::getUsingDirectiveName();
1893 }
1894
1895 // Required to silence GCC warning
1896 return DeclarationName();
1897}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001898
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001899/// \brief Read an integral value
1900llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1901 unsigned BitWidth = Record[Idx++];
1902 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1903 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1904 Idx += NumWords;
1905 return Result;
1906}
1907
1908/// \brief Read a signed integral value
1909llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1910 bool isUnsigned = Record[Idx++];
1911 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1912}
1913
Douglas Gregor17fc2232009-04-14 21:55:33 +00001914/// \brief Read a floating-point value
1915llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00001916 return llvm::APFloat(ReadAPInt(Record, Idx));
1917}
1918
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001919// \brief Read a string
1920std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1921 unsigned Len = Record[Idx++];
1922 std::string Result(&Record[Idx], &Record[Idx] + Len);
1923 Idx += Len;
1924 return Result;
1925}
1926
1927/// \brief Reads attributes from the current stream position.
1928Attr *PCHReader::ReadAttributes() {
1929 unsigned Code = Stream.ReadCode();
1930 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1931 "Expected unabbreviated record"); (void)Code;
1932
1933 RecordData Record;
1934 unsigned Idx = 0;
1935 unsigned RecCode = Stream.ReadRecord(Code, Record);
1936 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1937 (void)RecCode;
1938
1939#define SIMPLE_ATTR(Name) \
1940 case Attr::Name: \
1941 New = ::new (Context) Name##Attr(); \
1942 break
1943
1944#define STRING_ATTR(Name) \
1945 case Attr::Name: \
1946 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1947 break
1948
1949#define UNSIGNED_ATTR(Name) \
1950 case Attr::Name: \
1951 New = ::new (Context) Name##Attr(Record[Idx++]); \
1952 break
1953
1954 Attr *Attrs = 0;
1955 while (Idx < Record.size()) {
1956 Attr *New = 0;
1957 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1958 bool IsInherited = Record[Idx++];
1959
1960 switch (Kind) {
1961 STRING_ATTR(Alias);
1962 UNSIGNED_ATTR(Aligned);
1963 SIMPLE_ATTR(AlwaysInline);
1964 SIMPLE_ATTR(AnalyzerNoReturn);
1965 STRING_ATTR(Annotate);
1966 STRING_ATTR(AsmLabel);
1967
1968 case Attr::Blocks:
1969 New = ::new (Context) BlocksAttr(
1970 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1971 break;
1972
1973 case Attr::Cleanup:
1974 New = ::new (Context) CleanupAttr(
1975 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1976 break;
1977
1978 SIMPLE_ATTR(Const);
1979 UNSIGNED_ATTR(Constructor);
1980 SIMPLE_ATTR(DLLExport);
1981 SIMPLE_ATTR(DLLImport);
1982 SIMPLE_ATTR(Deprecated);
1983 UNSIGNED_ATTR(Destructor);
1984 SIMPLE_ATTR(FastCall);
1985
1986 case Attr::Format: {
1987 std::string Type = ReadString(Record, Idx);
1988 unsigned FormatIdx = Record[Idx++];
1989 unsigned FirstArg = Record[Idx++];
1990 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1991 break;
1992 }
1993
1994 SIMPLE_ATTR(GNUCInline);
1995
1996 case Attr::IBOutletKind:
1997 New = ::new (Context) IBOutletAttr();
1998 break;
1999
2000 SIMPLE_ATTR(NoReturn);
2001 SIMPLE_ATTR(NoThrow);
2002 SIMPLE_ATTR(Nodebug);
2003 SIMPLE_ATTR(Noinline);
2004
2005 case Attr::NonNull: {
2006 unsigned Size = Record[Idx++];
2007 llvm::SmallVector<unsigned, 16> ArgNums;
2008 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2009 Idx += Size;
2010 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2011 break;
2012 }
2013
2014 SIMPLE_ATTR(ObjCException);
2015 SIMPLE_ATTR(ObjCNSObject);
2016 SIMPLE_ATTR(Overloadable);
2017 UNSIGNED_ATTR(Packed);
2018 SIMPLE_ATTR(Pure);
2019 UNSIGNED_ATTR(Regparm);
2020 STRING_ATTR(Section);
2021 SIMPLE_ATTR(StdCall);
2022 SIMPLE_ATTR(TransparentUnion);
2023 SIMPLE_ATTR(Unavailable);
2024 SIMPLE_ATTR(Unused);
2025 SIMPLE_ATTR(Used);
2026
2027 case Attr::Visibility:
2028 New = ::new (Context) VisibilityAttr(
2029 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2030 break;
2031
2032 SIMPLE_ATTR(WarnUnusedResult);
2033 SIMPLE_ATTR(Weak);
2034 SIMPLE_ATTR(WeakImport);
2035 }
2036
2037 assert(New && "Unable to decode attribute?");
2038 New->setInherited(IsInherited);
2039 New->setNext(Attrs);
2040 Attrs = New;
2041 }
2042#undef UNSIGNED_ATTR
2043#undef STRING_ATTR
2044#undef SIMPLE_ATTR
2045
2046 // The list of attributes was built backwards. Reverse the list
2047 // before returning it.
2048 Attr *PrevAttr = 0, *NextAttr = 0;
2049 while (Attrs) {
2050 NextAttr = Attrs->getNext();
2051 Attrs->setNext(PrevAttr);
2052 PrevAttr = Attrs;
2053 Attrs = NextAttr;
2054 }
2055
2056 return PrevAttr;
2057}
2058
Douglas Gregorc9490c02009-04-16 22:23:12 +00002059Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002060 // Within the bitstream, expressions are stored in Reverse Polish
2061 // Notation, with each of the subexpressions preceding the
2062 // expression they are stored in. To evaluate expressions, we
2063 // continue reading expressions and placing them on the stack, with
2064 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002065 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002066 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002067 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002068 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002069 llvm::SmallVector<Stmt *, 16> StmtStack;
2070 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002071 Stmt::EmptyShell Empty;
2072
Douglas Gregor087fd532009-04-14 23:32:43 +00002073 while (true) {
2074 unsigned Code = Stream.ReadCode();
2075 if (Code == llvm::bitc::END_BLOCK) {
2076 if (Stream.ReadBlockEnd()) {
2077 Error("Error at end of Source Manager block");
2078 return 0;
2079 }
2080 break;
2081 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002082
Douglas Gregor087fd532009-04-14 23:32:43 +00002083 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2084 // No known subblocks, always skip them.
2085 Stream.ReadSubBlockID();
2086 if (Stream.SkipBlock()) {
2087 Error("Malformed block record");
2088 return 0;
2089 }
2090 continue;
2091 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002092
Douglas Gregor087fd532009-04-14 23:32:43 +00002093 if (Code == llvm::bitc::DEFINE_ABBREV) {
2094 Stream.ReadAbbrevRecord();
2095 continue;
2096 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002097
Douglas Gregorc9490c02009-04-16 22:23:12 +00002098 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002099 Idx = 0;
2100 Record.clear();
2101 bool Finished = false;
2102 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002103 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002104 Finished = true;
2105 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002106
Douglas Gregorc9490c02009-04-16 22:23:12 +00002107 case pch::STMT_NULL_PTR:
2108 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002109 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002110
Douglas Gregor025452f2009-04-17 00:04:06 +00002111 case pch::STMT_NULL:
2112 S = new (Context) NullStmt(Empty);
2113 break;
2114
2115 case pch::STMT_COMPOUND:
2116 S = new (Context) CompoundStmt(Empty);
2117 break;
2118
2119 case pch::STMT_CASE:
2120 S = new (Context) CaseStmt(Empty);
2121 break;
2122
2123 case pch::STMT_DEFAULT:
2124 S = new (Context) DefaultStmt(Empty);
2125 break;
2126
2127 case pch::STMT_IF:
2128 S = new (Context) IfStmt(Empty);
2129 break;
2130
2131 case pch::STMT_SWITCH:
2132 S = new (Context) SwitchStmt(Empty);
2133 break;
2134
Douglas Gregord921cf92009-04-17 00:16:09 +00002135 case pch::STMT_WHILE:
2136 S = new (Context) WhileStmt(Empty);
2137 break;
2138
2139 case pch::STMT_CONTINUE:
2140 S = new (Context) ContinueStmt(Empty);
2141 break;
2142
Douglas Gregor025452f2009-04-17 00:04:06 +00002143 case pch::STMT_BREAK:
2144 S = new (Context) BreakStmt(Empty);
2145 break;
2146
Douglas Gregor087fd532009-04-14 23:32:43 +00002147 case pch::EXPR_PREDEFINED:
2148 // FIXME: untested (until we can serialize function bodies).
Douglas Gregorc9490c02009-04-16 22:23:12 +00002149 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002150 break;
2151
2152 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002153 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002154 break;
2155
2156 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002157 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002158 break;
2159
2160 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002161 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002162 break;
2163
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002164 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002165 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002166 break;
2167
Douglas Gregor673ecd62009-04-15 16:35:07 +00002168 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002169 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002170 Record[PCHStmtReader::NumExprFields + 1]);
2171 break;
2172
Douglas Gregor087fd532009-04-14 23:32:43 +00002173 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002174 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002175 break;
2176
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002177 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002178 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002179 break;
2180
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002181 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002182 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002183 break;
2184
2185 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002186 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002187 break;
2188
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002189 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002190 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002191 break;
2192
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002193 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002194 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002195 break;
2196
2197 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002198 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002199 break;
2200
Douglas Gregordb600c32009-04-15 00:25:59 +00002201 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002202 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002203 break;
2204
Douglas Gregorad90e962009-04-15 22:40:36 +00002205 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002206 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002207 break;
2208
2209 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002210 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002211 break;
2212
Douglas Gregor087fd532009-04-14 23:32:43 +00002213 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002214 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002215 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002216
2217 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002218 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002219 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002220
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002221 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002222 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002223 break;
2224
Douglas Gregord3c98a02009-04-15 23:02:49 +00002225 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002226 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002227 break;
2228
Douglas Gregord077d752009-04-16 00:55:48 +00002229 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002230 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002231 break;
2232
2233 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002234 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002235 Record[PCHStmtReader::NumExprFields] - 1);
2236
2237 break;
2238
2239 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002240 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002241 break;
2242
Douglas Gregord3c98a02009-04-15 23:02:49 +00002243 case pch::EXPR_VA_ARG:
2244 // FIXME: untested; we need function bodies first
Douglas Gregorc9490c02009-04-16 22:23:12 +00002245 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002246 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002247
2248 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002249 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002250 break;
2251
2252 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002253 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002254 break;
2255
2256 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002257 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002258 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002259
2260 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002261 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002262 break;
2263
2264 case pch::EXPR_BLOCK_DECL_REF:
2265 // FIXME: untested until we have statement and block support
Douglas Gregorc9490c02009-04-16 22:23:12 +00002266 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002267 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002268 }
2269
Douglas Gregorc9490c02009-04-16 22:23:12 +00002270 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002271 if (Finished)
2272 break;
2273
Douglas Gregorc9490c02009-04-16 22:23:12 +00002274 if (S) {
2275 unsigned NumSubStmts = Reader.Visit(S);
2276 while (NumSubStmts > 0) {
2277 StmtStack.pop_back();
2278 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002279 }
2280 }
2281
2282 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002283 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002284 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002285 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
2286 return StmtStack.back();
2287}
2288
2289Expr *PCHReader::ReadExpr() {
2290 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002291}
2292
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002293DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002294 return Diag(SourceLocation(), DiagID);
2295}
2296
2297DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2298 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002299 Context.getSourceManager()),
2300 DiagID);
2301}
Douglas Gregor025452f2009-04-17 00:04:06 +00002302
2303/// \brief Record that the given ID maps to the given switch-case
2304/// statement.
2305void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2306 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2307 SwitchCaseStmts[ID] = SC;
2308}
2309
2310/// \brief Retrieve the switch-case statement with the given ID.
2311SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2312 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2313 return SwitchCaseStmts[ID];
2314}