blob: e39f3e580b8f35d74b0024cdffad267ba3c27620 [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);
254 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor087fd532009-04-14 23:32:43 +0000255 unsigned VisitExpr(Expr *E);
256 unsigned VisitPredefinedExpr(PredefinedExpr *E);
257 unsigned VisitDeclRefExpr(DeclRefExpr *E);
258 unsigned VisitIntegerLiteral(IntegerLiteral *E);
259 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000260 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000261 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000262 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000263 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000264 unsigned VisitUnaryOperator(UnaryOperator *E);
265 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000266 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000267 unsigned VisitCallExpr(CallExpr *E);
268 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000269 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000270 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorad90e962009-04-15 22:40:36 +0000271 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
272 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000273 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000274 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
275 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000276 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000277 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregord077d752009-04-16 00:55:48 +0000278 unsigned VisitInitListExpr(InitListExpr *E);
279 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
280 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregord3c98a02009-04-15 23:02:49 +0000281 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000282 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
283 unsigned VisitChooseExpr(ChooseExpr *E);
284 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000285 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
286 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000287 };
288}
289
Douglas Gregor025452f2009-04-17 00:04:06 +0000290unsigned PCHStmtReader::VisitStmt(Stmt *S) {
291 assert(Idx == NumStmtFields && "Incorrect statement field count");
292 return 0;
293}
294
295unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
296 VisitStmt(S);
297 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
298 return 0;
299}
300
301unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
302 VisitStmt(S);
303 unsigned NumStmts = Record[Idx++];
304 S->setStmts(Reader.getContext(),
305 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
306 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
307 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
308 return NumStmts;
309}
310
311unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
312 VisitStmt(S);
313 Reader.RecordSwitchCaseID(S, Record[Idx++]);
314 return 0;
315}
316
317unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
318 VisitSwitchCase(S);
319 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
320 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
321 S->setSubStmt(StmtStack.back());
322 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
323 return 3;
324}
325
326unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
327 VisitSwitchCase(S);
328 S->setSubStmt(StmtStack.back());
329 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
330 return 1;
331}
332
333unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
334 VisitStmt(S);
335 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
336 S->setThen(StmtStack[StmtStack.size() - 2]);
337 S->setElse(StmtStack[StmtStack.size() - 1]);
338 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
339 return 3;
340}
341
342unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
343 VisitStmt(S);
344 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
345 S->setBody(StmtStack.back());
346 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
347 SwitchCase *PrevSC = 0;
348 for (unsigned N = Record.size(); Idx != N; ++Idx) {
349 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
350 if (PrevSC)
351 PrevSC->setNextSwitchCase(SC);
352 else
353 S->setSwitchCaseList(SC);
354 PrevSC = SC;
355 }
356 return 2;
357}
358
359unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
360 VisitStmt(S);
361 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
362 return 0;
363}
364
Douglas Gregor087fd532009-04-14 23:32:43 +0000365unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor025452f2009-04-17 00:04:06 +0000366 VisitStmt(E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000367 E->setType(Reader.GetType(Record[Idx++]));
368 E->setTypeDependent(Record[Idx++]);
369 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000370 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000371 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000372}
373
Douglas Gregor087fd532009-04-14 23:32:43 +0000374unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000375 VisitExpr(E);
376 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
377 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000378 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000379}
380
Douglas Gregor087fd532009-04-14 23:32:43 +0000381unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000382 VisitExpr(E);
383 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
384 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000385 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000386}
387
Douglas Gregor087fd532009-04-14 23:32:43 +0000388unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000389 VisitExpr(E);
390 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
391 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000392 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000393}
394
Douglas Gregor087fd532009-04-14 23:32:43 +0000395unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000396 VisitExpr(E);
397 E->setValue(Reader.ReadAPFloat(Record, Idx));
398 E->setExact(Record[Idx++]);
399 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000400 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000401}
402
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000403unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
404 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000405 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000406 return 1;
407}
408
Douglas Gregor673ecd62009-04-15 16:35:07 +0000409unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
410 VisitExpr(E);
411 unsigned Len = Record[Idx++];
412 assert(Record[Idx] == E->getNumConcatenated() &&
413 "Wrong number of concatenated tokens!");
414 ++Idx;
415 E->setWide(Record[Idx++]);
416
417 // Read string data
418 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
419 E->setStrData(Reader.getContext(), &Str[0], Len);
420 Idx += Len;
421
422 // Read source locations
423 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
424 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
425
426 return 0;
427}
428
Douglas Gregor087fd532009-04-14 23:32:43 +0000429unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000430 VisitExpr(E);
431 E->setValue(Record[Idx++]);
432 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
433 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000434 return 0;
435}
436
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000437unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
438 VisitExpr(E);
439 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
440 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000441 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000442 return 1;
443}
444
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000445unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
446 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000447 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000448 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
449 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
450 return 1;
451}
452
453unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
454 VisitExpr(E);
455 E->setSizeof(Record[Idx++]);
456 if (Record[Idx] == 0) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000457 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000458 ++Idx;
459 } else {
460 E->setArgument(Reader.GetType(Record[Idx++]));
461 }
462 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
463 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
464 return E->isArgumentType()? 0 : 1;
465}
466
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000467unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
468 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000469 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
470 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregorcb2ca732009-04-15 22:19:53 +0000471 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
472 return 2;
473}
474
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000475unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
476 VisitExpr(E);
477 E->setNumArgs(Reader.getContext(), Record[Idx++]);
478 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000479 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000480 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000481 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000482 return E->getNumArgs() + 1;
483}
484
485unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
486 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000487 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000488 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
489 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
490 E->setArrow(Record[Idx++]);
491 return 1;
492}
493
Douglas Gregor087fd532009-04-14 23:32:43 +0000494unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
495 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000496 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor087fd532009-04-14 23:32:43 +0000497 return 1;
498}
499
Douglas Gregordb600c32009-04-15 00:25:59 +0000500unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
501 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000502 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
503 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregordb600c32009-04-15 00:25:59 +0000504 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
505 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
506 return 2;
507}
508
Douglas Gregorad90e962009-04-15 22:40:36 +0000509unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
510 VisitBinaryOperator(E);
511 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
512 E->setComputationResultType(Reader.GetType(Record[Idx++]));
513 return 2;
514}
515
516unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
517 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000518 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
519 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
520 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorad90e962009-04-15 22:40:36 +0000521 return 3;
522}
523
Douglas Gregor087fd532009-04-14 23:32:43 +0000524unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
525 VisitCastExpr(E);
526 E->setLvalueCast(Record[Idx++]);
527 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000528}
529
Douglas Gregordb600c32009-04-15 00:25:59 +0000530unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
531 VisitCastExpr(E);
532 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
533 return 1;
534}
535
536unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
537 VisitExplicitCastExpr(E);
538 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
539 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
540 return 1;
541}
542
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000543unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
544 VisitExpr(E);
545 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc9490c02009-04-16 22:23:12 +0000546 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorba6d7e72009-04-16 02:33:48 +0000547 E->setFileScope(Record[Idx++]);
548 return 1;
549}
550
Douglas Gregord3c98a02009-04-15 23:02:49 +0000551unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
552 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000553 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000554 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
555 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
556 return 1;
557}
558
Douglas Gregord077d752009-04-16 00:55:48 +0000559unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
560 VisitExpr(E);
561 unsigned NumInits = Record[Idx++];
562 E->reserveInits(NumInits);
563 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000564 E->updateInit(I,
565 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
566 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregord077d752009-04-16 00:55:48 +0000567 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
568 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
569 E->setInitializedFieldInUnion(
570 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
571 E->sawArrayRangeDesignator(Record[Idx++]);
572 return NumInits + 1;
573}
574
575unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
576 typedef DesignatedInitExpr::Designator Designator;
577
578 VisitExpr(E);
579 unsigned NumSubExprs = Record[Idx++];
580 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
581 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc9490c02009-04-16 22:23:12 +0000582 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregord077d752009-04-16 00:55:48 +0000583 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
584 E->setGNUSyntax(Record[Idx++]);
585
586 llvm::SmallVector<Designator, 4> Designators;
587 while (Idx < Record.size()) {
588 switch ((pch::DesignatorTypes)Record[Idx++]) {
589 case pch::DESIG_FIELD_DECL: {
590 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
591 SourceLocation DotLoc
592 = SourceLocation::getFromRawEncoding(Record[Idx++]);
593 SourceLocation FieldLoc
594 = SourceLocation::getFromRawEncoding(Record[Idx++]);
595 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
596 FieldLoc));
597 Designators.back().setField(Field);
598 break;
599 }
600
601 case pch::DESIG_FIELD_NAME: {
602 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
603 SourceLocation DotLoc
604 = SourceLocation::getFromRawEncoding(Record[Idx++]);
605 SourceLocation FieldLoc
606 = SourceLocation::getFromRawEncoding(Record[Idx++]);
607 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
608 break;
609 }
610
611 case pch::DESIG_ARRAY: {
612 unsigned Index = Record[Idx++];
613 SourceLocation LBracketLoc
614 = SourceLocation::getFromRawEncoding(Record[Idx++]);
615 SourceLocation RBracketLoc
616 = SourceLocation::getFromRawEncoding(Record[Idx++]);
617 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
618 break;
619 }
620
621 case pch::DESIG_ARRAY_RANGE: {
622 unsigned Index = Record[Idx++];
623 SourceLocation LBracketLoc
624 = SourceLocation::getFromRawEncoding(Record[Idx++]);
625 SourceLocation EllipsisLoc
626 = SourceLocation::getFromRawEncoding(Record[Idx++]);
627 SourceLocation RBracketLoc
628 = SourceLocation::getFromRawEncoding(Record[Idx++]);
629 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
630 RBracketLoc));
631 break;
632 }
633 }
634 }
635 E->setDesignators(&Designators[0], Designators.size());
636
637 return NumSubExprs;
638}
639
640unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
641 VisitExpr(E);
642 return 0;
643}
644
Douglas Gregord3c98a02009-04-15 23:02:49 +0000645unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
646 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000647 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregord3c98a02009-04-15 23:02:49 +0000648 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
649 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
650 return 1;
651}
652
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000653unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
654 VisitExpr(E);
655 E->setArgType1(Reader.GetType(Record[Idx++]));
656 E->setArgType2(Reader.GetType(Record[Idx++]));
657 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
658 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
659 return 0;
660}
661
662unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
663 VisitExpr(E);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000664 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
665 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
666 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor44cae0c2009-04-15 23:33:31 +0000667 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
668 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
669 return 3;
670}
671
672unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
673 VisitExpr(E);
674 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
675 return 0;
676}
Douglas Gregord3c98a02009-04-15 23:02:49 +0000677
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000678unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
679 VisitExpr(E);
680 unsigned NumExprs = Record[Idx++];
Douglas Gregorc9490c02009-04-16 22:23:12 +0000681 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor94cd5d12009-04-16 00:01:45 +0000682 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
683 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
684 return NumExprs;
685}
686
687unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
688 VisitExpr(E);
689 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
690 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
691 E->setByRef(Record[Idx++]);
692 return 0;
693}
694
Douglas Gregor2cf26342009-04-09 22:27:44 +0000695// FIXME: use the diagnostics machinery
696static bool Error(const char *Str) {
697 std::fprintf(stderr, "%s\n", Str);
698 return true;
699}
700
Douglas Gregore1d918e2009-04-10 23:10:45 +0000701/// \brief Check the contents of the predefines buffer against the
702/// contents of the predefines buffer used to build the PCH file.
703///
704/// The contents of the two predefines buffers should be the same. If
705/// not, then some command-line option changed the preprocessor state
706/// and we must reject the PCH file.
707///
708/// \param PCHPredef The start of the predefines buffer in the PCH
709/// file.
710///
711/// \param PCHPredefLen The length of the predefines buffer in the PCH
712/// file.
713///
714/// \param PCHBufferID The FileID for the PCH predefines buffer.
715///
716/// \returns true if there was a mismatch (in which case the PCH file
717/// should be ignored), or false otherwise.
718bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
719 unsigned PCHPredefLen,
720 FileID PCHBufferID) {
721 const char *Predef = PP.getPredefines().c_str();
722 unsigned PredefLen = PP.getPredefines().size();
723
724 // If the two predefines buffers compare equal, we're done!.
725 if (PredefLen == PCHPredefLen &&
726 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
727 return false;
728
729 // The predefines buffers are different. Produce a reasonable
730 // diagnostic showing where they are different.
731
732 // The source locations (potentially in the two different predefines
733 // buffers)
734 SourceLocation Loc1, Loc2;
735 SourceManager &SourceMgr = PP.getSourceManager();
736
737 // Create a source buffer for our predefines string, so
738 // that we can build a diagnostic that points into that
739 // source buffer.
740 FileID BufferID;
741 if (Predef && Predef[0]) {
742 llvm::MemoryBuffer *Buffer
743 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
744 "<built-in>");
745 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
746 }
747
748 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
749 std::pair<const char *, const char *> Locations
750 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
751
752 if (Locations.first != Predef + MinLen) {
753 // We found the location in the two buffers where there is a
754 // difference. Form source locations to point there (in both
755 // buffers).
756 unsigned Offset = Locations.first - Predef;
757 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
758 .getFileLocWithOffset(Offset);
759 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
760 .getFileLocWithOffset(Offset);
761 } else if (PredefLen > PCHPredefLen) {
762 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
763 .getFileLocWithOffset(MinLen);
764 } else {
765 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
766 .getFileLocWithOffset(MinLen);
767 }
768
769 Diag(Loc1, diag::warn_pch_preprocessor);
770 if (Loc2.isValid())
771 Diag(Loc2, diag::note_predef_in_pch);
772 Diag(diag::note_ignoring_pch) << FileName;
773 return true;
774}
775
Douglas Gregorbd945002009-04-13 16:31:14 +0000776/// \brief Read the line table in the source manager block.
777/// \returns true if ther was an error.
778static bool ParseLineTable(SourceManager &SourceMgr,
779 llvm::SmallVectorImpl<uint64_t> &Record) {
780 unsigned Idx = 0;
781 LineTableInfo &LineTable = SourceMgr.getLineTable();
782
783 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000784 std::map<int, int> FileIDs;
785 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000786 // Extract the file name
787 unsigned FilenameLen = Record[Idx++];
788 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
789 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000790 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
791 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000792 }
793
794 // Parse the line entries
795 std::vector<LineEntry> Entries;
796 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000797 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000798
799 // Extract the line entries
800 unsigned NumEntries = Record[Idx++];
801 Entries.clear();
802 Entries.reserve(NumEntries);
803 for (unsigned I = 0; I != NumEntries; ++I) {
804 unsigned FileOffset = Record[Idx++];
805 unsigned LineNo = Record[Idx++];
806 int FilenameID = Record[Idx++];
807 SrcMgr::CharacteristicKind FileKind
808 = (SrcMgr::CharacteristicKind)Record[Idx++];
809 unsigned IncludeOffset = Record[Idx++];
810 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
811 FileKind, IncludeOffset));
812 }
813 LineTable.AddEntry(FID, Entries);
814 }
815
816 return false;
817}
818
Douglas Gregor14f79002009-04-10 03:52:48 +0000819/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000820PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000821 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000822 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
823 Error("Malformed source manager block record");
824 return Failure;
825 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000826
827 SourceManager &SourceMgr = Context.getSourceManager();
828 RecordData Record;
829 while (true) {
830 unsigned Code = Stream.ReadCode();
831 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000832 if (Stream.ReadBlockEnd()) {
833 Error("Error at end of Source Manager block");
834 return Failure;
835 }
836
837 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000838 }
839
840 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
841 // No known subblocks, always skip them.
842 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000843 if (Stream.SkipBlock()) {
844 Error("Malformed block record");
845 return Failure;
846 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000847 continue;
848 }
849
850 if (Code == llvm::bitc::DEFINE_ABBREV) {
851 Stream.ReadAbbrevRecord();
852 continue;
853 }
854
855 // Read a record.
856 const char *BlobStart;
857 unsigned BlobLen;
858 Record.clear();
859 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
860 default: // Default behavior: ignore.
861 break;
862
863 case pch::SM_SLOC_FILE_ENTRY: {
864 // FIXME: We would really like to delay the creation of this
865 // FileEntry until it is actually required, e.g., when producing
866 // a diagnostic with a source location in this file.
867 const FileEntry *File
868 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
869 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +0000870 FileID ID = SourceMgr.createFileID(File,
871 SourceLocation::getFromRawEncoding(Record[1]),
872 (CharacteristicKind)Record[2]);
873 if (Record[3])
874 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
875 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +0000876 break;
877 }
878
879 case pch::SM_SLOC_BUFFER_ENTRY: {
880 const char *Name = BlobStart;
881 unsigned Code = Stream.ReadCode();
882 Record.clear();
883 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
884 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000885 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000886 llvm::MemoryBuffer *Buffer
887 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
888 BlobStart + BlobLen - 1,
889 Name);
890 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
891
892 if (strcmp(Name, "<built-in>") == 0
893 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
894 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +0000895 break;
896 }
897
898 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
899 SourceLocation SpellingLoc
900 = SourceLocation::getFromRawEncoding(Record[1]);
901 SourceMgr.createInstantiationLoc(
902 SpellingLoc,
903 SourceLocation::getFromRawEncoding(Record[2]),
904 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +0000905 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +0000906 break;
907 }
908
Chris Lattner2c78b872009-04-14 23:22:57 +0000909 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +0000910 if (ParseLineTable(SourceMgr, Record))
911 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000912 break;
Douglas Gregor14f79002009-04-10 03:52:48 +0000913 }
914 }
915}
916
Chris Lattner42d42b52009-04-10 21:41:48 +0000917bool PCHReader::ReadPreprocessorBlock() {
918 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
919 return Error("Malformed preprocessor block record");
920
Chris Lattner42d42b52009-04-10 21:41:48 +0000921 RecordData Record;
922 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
923 MacroInfo *LastMacro = 0;
924
925 while (true) {
926 unsigned Code = Stream.ReadCode();
927 switch (Code) {
928 case llvm::bitc::END_BLOCK:
929 if (Stream.ReadBlockEnd())
930 return Error("Error at end of preprocessor block");
931 return false;
932
933 case llvm::bitc::ENTER_SUBBLOCK:
934 // No known subblocks, always skip them.
935 Stream.ReadSubBlockID();
936 if (Stream.SkipBlock())
937 return Error("Malformed block record");
938 continue;
939
940 case llvm::bitc::DEFINE_ABBREV:
941 Stream.ReadAbbrevRecord();
942 continue;
943 default: break;
944 }
945
946 // Read a record.
947 Record.clear();
948 pch::PreprocessorRecordTypes RecType =
949 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
950 switch (RecType) {
951 default: // Default behavior: ignore unknown records.
952 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000953 case pch::PP_COUNTER_VALUE:
954 if (!Record.empty())
955 PP.setCounterValue(Record[0]);
956 break;
957
Chris Lattner42d42b52009-04-10 21:41:48 +0000958 case pch::PP_MACRO_OBJECT_LIKE:
959 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +0000960 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
961 if (II == 0)
962 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +0000963 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
964 bool isUsed = Record[2];
965
966 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
967 MI->setIsUsed(isUsed);
968
969 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
970 // Decode function-like macro info.
971 bool isC99VarArgs = Record[3];
972 bool isGNUVarArgs = Record[4];
973 MacroArgs.clear();
974 unsigned NumArgs = Record[5];
975 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +0000976 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +0000977
978 // Install function-like macro info.
979 MI->setIsFunctionLike();
980 if (isC99VarArgs) MI->setIsC99Varargs();
981 if (isGNUVarArgs) MI->setIsGNUVarargs();
982 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
983 PP.getPreprocessorAllocator());
984 }
985
986 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +0000987 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +0000988
989 // Remember that we saw this macro last so that we add the tokens that
990 // form its body to it.
991 LastMacro = MI;
992 break;
993 }
994
995 case pch::PP_TOKEN: {
996 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
997 // pretend we didn't see this.
998 if (LastMacro == 0) break;
999
1000 Token Tok;
1001 Tok.startToken();
1002 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1003 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +00001004 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1005 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +00001006 Tok.setKind((tok::TokenKind)Record[3]);
1007 Tok.setFlag((Token::TokenFlags)Record[4]);
1008 LastMacro->AddTokenToBody(Tok);
1009 break;
1010 }
1011 }
1012 }
1013}
1014
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001015PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1016 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1017 Error("Malformed block record");
1018 return Failure;
1019 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001020
Chris Lattner7356a312009-04-11 21:15:38 +00001021 uint64_t PreprocessorBlockBit = 0;
1022
Douglas Gregor2cf26342009-04-09 22:27:44 +00001023 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001024 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001025 while (!Stream.AtEndOfStream()) {
1026 unsigned Code = Stream.ReadCode();
1027 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +00001028 // If we saw the preprocessor block, read it now.
1029 if (PreprocessorBlockBit) {
1030 uint64_t SavedPos = Stream.GetCurrentBitNo();
1031 Stream.JumpToBit(PreprocessorBlockBit);
1032 if (ReadPreprocessorBlock()) {
1033 Error("Malformed preprocessor block");
1034 return Failure;
1035 }
1036 Stream.JumpToBit(SavedPos);
1037 }
1038
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001039 if (Stream.ReadBlockEnd()) {
1040 Error("Error at end of module block");
1041 return Failure;
1042 }
Chris Lattner7356a312009-04-11 21:15:38 +00001043
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001044 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001045 }
1046
1047 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1048 switch (Stream.ReadSubBlockID()) {
1049 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1050 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1051 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001052 if (Stream.SkipBlock()) {
1053 Error("Malformed block record");
1054 return Failure;
1055 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001056 break;
1057
Chris Lattner7356a312009-04-11 21:15:38 +00001058 case pch::PREPROCESSOR_BLOCK_ID:
1059 // Skip the preprocessor block for now, but remember where it is. We
1060 // want to read it in after the identifier table.
1061 if (PreprocessorBlockBit) {
1062 Error("Multiple preprocessor blocks found.");
1063 return Failure;
1064 }
1065 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1066 if (Stream.SkipBlock()) {
1067 Error("Malformed block record");
1068 return Failure;
1069 }
1070 break;
1071
Douglas Gregor14f79002009-04-10 03:52:48 +00001072 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001073 switch (ReadSourceManagerBlock()) {
1074 case Success:
1075 break;
1076
1077 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001078 Error("Malformed source manager block");
1079 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001080
1081 case IgnorePCH:
1082 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001083 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001084 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001085 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001086 continue;
1087 }
1088
1089 if (Code == llvm::bitc::DEFINE_ABBREV) {
1090 Stream.ReadAbbrevRecord();
1091 continue;
1092 }
1093
1094 // Read and process a record.
1095 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001096 const char *BlobStart = 0;
1097 unsigned BlobLen = 0;
1098 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1099 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001100 default: // Default behavior: ignore.
1101 break;
1102
1103 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001104 if (!TypeOffsets.empty()) {
1105 Error("Duplicate TYPE_OFFSET record in PCH file");
1106 return Failure;
1107 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001108 TypeOffsets.swap(Record);
1109 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1110 break;
1111
1112 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001113 if (!DeclOffsets.empty()) {
1114 Error("Duplicate DECL_OFFSET record in PCH file");
1115 return Failure;
1116 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001117 DeclOffsets.swap(Record);
1118 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1119 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001120
1121 case pch::LANGUAGE_OPTIONS:
1122 if (ParseLanguageOptions(Record))
1123 return IgnorePCH;
1124 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001125
Douglas Gregorafaf3082009-04-11 00:14:32 +00001126 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001127 std::string TargetTriple(BlobStart, BlobLen);
1128 if (TargetTriple != Context.Target.getTargetTriple()) {
1129 Diag(diag::warn_pch_target_triple)
1130 << TargetTriple << Context.Target.getTargetTriple();
1131 Diag(diag::note_ignoring_pch) << FileName;
1132 return IgnorePCH;
1133 }
1134 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001135 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001136
1137 case pch::IDENTIFIER_TABLE:
1138 IdentifierTable = BlobStart;
1139 break;
1140
1141 case pch::IDENTIFIER_OFFSET:
1142 if (!IdentifierData.empty()) {
1143 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1144 return Failure;
1145 }
1146 IdentifierData.swap(Record);
1147#ifndef NDEBUG
1148 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1149 if ((IdentifierData[I] & 0x01) == 0) {
1150 Error("Malformed identifier table in the precompiled header");
1151 return Failure;
1152 }
1153 }
1154#endif
1155 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +00001156
1157 case pch::EXTERNAL_DEFINITIONS:
1158 if (!ExternalDefinitions.empty()) {
1159 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1160 return Failure;
1161 }
1162 ExternalDefinitions.swap(Record);
1163 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +00001164 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001165 }
1166
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001167 Error("Premature end of bitstream");
1168 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001169}
1170
Douglas Gregore1d918e2009-04-10 23:10:45 +00001171PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001172 // Set the PCH file name.
1173 this->FileName = FileName;
1174
Douglas Gregor2cf26342009-04-09 22:27:44 +00001175 // Open the PCH file.
1176 std::string ErrStr;
1177 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +00001178 if (!Buffer) {
1179 Error(ErrStr.c_str());
1180 return IgnorePCH;
1181 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001182
1183 // Initialize the stream
1184 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1185 (const unsigned char *)Buffer->getBufferEnd());
1186
1187 // Sniff for the signature.
1188 if (Stream.Read(8) != 'C' ||
1189 Stream.Read(8) != 'P' ||
1190 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +00001191 Stream.Read(8) != 'H') {
1192 Error("Not a PCH file");
1193 return IgnorePCH;
1194 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001195
1196 // We expect a number of well-defined blocks, though we don't necessarily
1197 // need to understand them all.
1198 while (!Stream.AtEndOfStream()) {
1199 unsigned Code = Stream.ReadCode();
1200
Douglas Gregore1d918e2009-04-10 23:10:45 +00001201 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1202 Error("Invalid record at top-level");
1203 return Failure;
1204 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001205
1206 unsigned BlockID = Stream.ReadSubBlockID();
1207
1208 // We only know the PCH subblock ID.
1209 switch (BlockID) {
1210 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001211 if (Stream.ReadBlockInfoBlock()) {
1212 Error("Malformed BlockInfoBlock");
1213 return Failure;
1214 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001215 break;
1216 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001217 switch (ReadPCHBlock()) {
1218 case Success:
1219 break;
1220
1221 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001222 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001223
1224 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00001225 // FIXME: We could consider reading through to the end of this
1226 // PCH block, skipping subblocks, to see if there are other
1227 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +00001228 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001229 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001230 break;
1231 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00001232 if (Stream.SkipBlock()) {
1233 Error("Malformed block record");
1234 return Failure;
1235 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001236 break;
1237 }
1238 }
1239
1240 // Load the translation unit declaration
1241 ReadDeclRecord(DeclOffsets[0], 0);
1242
Douglas Gregore1d918e2009-04-10 23:10:45 +00001243 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001244}
1245
Douglas Gregor0b748912009-04-14 21:18:50 +00001246namespace {
1247 /// \brief Helper class that saves the current stream position and
1248 /// then restores it when destroyed.
1249 struct VISIBILITY_HIDDEN SavedStreamPosition {
1250 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +00001251 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +00001252
1253 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +00001254 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +00001255 }
1256
1257 private:
1258 llvm::BitstreamReader &Stream;
1259 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +00001260 };
1261}
1262
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001263/// \brief Parse the record that corresponds to a LangOptions data
1264/// structure.
1265///
1266/// This routine compares the language options used to generate the
1267/// PCH file against the language options set for the current
1268/// compilation. For each option, we classify differences between the
1269/// two compiler states as either "benign" or "important". Benign
1270/// differences don't matter, and we accept them without complaint
1271/// (and without modifying the language options). Differences between
1272/// the states for important options cause the PCH file to be
1273/// unusable, so we emit a warning and return true to indicate that
1274/// there was an error.
1275///
1276/// \returns true if the PCH file is unacceptable, false otherwise.
1277bool PCHReader::ParseLanguageOptions(
1278 const llvm::SmallVectorImpl<uint64_t> &Record) {
1279 const LangOptions &LangOpts = Context.getLangOptions();
1280#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1281#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1282 if (Record[Idx] != LangOpts.Option) { \
1283 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1284 Diag(diag::note_ignoring_pch) << FileName; \
1285 return true; \
1286 } \
1287 ++Idx
1288
1289 unsigned Idx = 0;
1290 PARSE_LANGOPT_BENIGN(Trigraphs);
1291 PARSE_LANGOPT_BENIGN(BCPLComment);
1292 PARSE_LANGOPT_BENIGN(DollarIdents);
1293 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1294 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1295 PARSE_LANGOPT_BENIGN(ImplicitInt);
1296 PARSE_LANGOPT_BENIGN(Digraphs);
1297 PARSE_LANGOPT_BENIGN(HexFloats);
1298 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1299 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1300 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1301 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1302 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1303 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1304 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1305 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1306 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1307 PARSE_LANGOPT_BENIGN(PascalStrings);
1308 PARSE_LANGOPT_BENIGN(Boolean);
1309 PARSE_LANGOPT_BENIGN(WritableStrings);
1310 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1311 diag::warn_pch_lax_vector_conversions);
1312 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1313 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1314 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1315 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1316 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1317 diag::warn_pch_thread_safe_statics);
1318 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1319 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1320 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1321 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1322 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1323 diag::warn_pch_heinous_extensions);
1324 // FIXME: Most of the options below are benign if the macro wasn't
1325 // used. Unfortunately, this means that a PCH compiled without
1326 // optimization can't be used with optimization turned on, even
1327 // though the only thing that changes is whether __OPTIMIZE__ was
1328 // defined... but if __OPTIMIZE__ never showed up in the header, it
1329 // doesn't matter. We could consider making this some special kind
1330 // of check.
1331 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1332 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1333 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1334 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1335 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1336 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1337 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1338 Diag(diag::warn_pch_gc_mode)
1339 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1340 Diag(diag::note_ignoring_pch) << FileName;
1341 return true;
1342 }
1343 ++Idx;
1344 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1345 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1346#undef PARSE_LANGOPT_IRRELEVANT
1347#undef PARSE_LANGOPT_BENIGN
1348
1349 return false;
1350}
1351
Douglas Gregor2cf26342009-04-09 22:27:44 +00001352/// \brief Read and return the type at the given offset.
1353///
1354/// This routine actually reads the record corresponding to the type
1355/// at the given offset in the bitstream. It is a helper routine for
1356/// GetType, which deals with reading type IDs.
1357QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001358 // Keep track of where we are in the stream, then jump back there
1359 // after reading this type.
1360 SavedStreamPosition SavedPosition(Stream);
1361
Douglas Gregor2cf26342009-04-09 22:27:44 +00001362 Stream.JumpToBit(Offset);
1363 RecordData Record;
1364 unsigned Code = Stream.ReadCode();
1365 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor6d473962009-04-15 22:00:08 +00001366 case pch::TYPE_EXT_QUAL: {
1367 assert(Record.size() == 3 &&
1368 "Incorrect encoding of extended qualifier type");
1369 QualType Base = GetType(Record[0]);
1370 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1371 unsigned AddressSpace = Record[2];
1372
1373 QualType T = Base;
1374 if (GCAttr != QualType::GCNone)
1375 T = Context.getObjCGCQualType(T, GCAttr);
1376 if (AddressSpace)
1377 T = Context.getAddrSpaceQualType(T, AddressSpace);
1378 return T;
1379 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001380
Douglas Gregor2cf26342009-04-09 22:27:44 +00001381 case pch::TYPE_FIXED_WIDTH_INT: {
1382 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1383 return Context.getFixedWidthIntType(Record[0], Record[1]);
1384 }
1385
1386 case pch::TYPE_COMPLEX: {
1387 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1388 QualType ElemType = GetType(Record[0]);
1389 return Context.getComplexType(ElemType);
1390 }
1391
1392 case pch::TYPE_POINTER: {
1393 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1394 QualType PointeeType = GetType(Record[0]);
1395 return Context.getPointerType(PointeeType);
1396 }
1397
1398 case pch::TYPE_BLOCK_POINTER: {
1399 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1400 QualType PointeeType = GetType(Record[0]);
1401 return Context.getBlockPointerType(PointeeType);
1402 }
1403
1404 case pch::TYPE_LVALUE_REFERENCE: {
1405 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1406 QualType PointeeType = GetType(Record[0]);
1407 return Context.getLValueReferenceType(PointeeType);
1408 }
1409
1410 case pch::TYPE_RVALUE_REFERENCE: {
1411 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1412 QualType PointeeType = GetType(Record[0]);
1413 return Context.getRValueReferenceType(PointeeType);
1414 }
1415
1416 case pch::TYPE_MEMBER_POINTER: {
1417 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1418 QualType PointeeType = GetType(Record[0]);
1419 QualType ClassType = GetType(Record[1]);
1420 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1421 }
1422
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001423 case pch::TYPE_CONSTANT_ARRAY: {
1424 QualType ElementType = GetType(Record[0]);
1425 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1426 unsigned IndexTypeQuals = Record[2];
1427 unsigned Idx = 3;
1428 llvm::APInt Size = ReadAPInt(Record, Idx);
1429 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1430 }
1431
1432 case pch::TYPE_INCOMPLETE_ARRAY: {
1433 QualType ElementType = GetType(Record[0]);
1434 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1435 unsigned IndexTypeQuals = Record[2];
1436 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1437 }
1438
1439 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001440 QualType ElementType = GetType(Record[0]);
1441 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1442 unsigned IndexTypeQuals = Record[2];
1443 return Context.getVariableArrayType(ElementType, ReadExpr(),
1444 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001445 }
1446
1447 case pch::TYPE_VECTOR: {
1448 if (Record.size() != 2) {
1449 Error("Incorrect encoding of vector type in PCH file");
1450 return QualType();
1451 }
1452
1453 QualType ElementType = GetType(Record[0]);
1454 unsigned NumElements = Record[1];
1455 return Context.getVectorType(ElementType, NumElements);
1456 }
1457
1458 case pch::TYPE_EXT_VECTOR: {
1459 if (Record.size() != 2) {
1460 Error("Incorrect encoding of extended vector type in PCH file");
1461 return QualType();
1462 }
1463
1464 QualType ElementType = GetType(Record[0]);
1465 unsigned NumElements = Record[1];
1466 return Context.getExtVectorType(ElementType, NumElements);
1467 }
1468
1469 case pch::TYPE_FUNCTION_NO_PROTO: {
1470 if (Record.size() != 1) {
1471 Error("Incorrect encoding of no-proto function type");
1472 return QualType();
1473 }
1474 QualType ResultType = GetType(Record[0]);
1475 return Context.getFunctionNoProtoType(ResultType);
1476 }
1477
1478 case pch::TYPE_FUNCTION_PROTO: {
1479 QualType ResultType = GetType(Record[0]);
1480 unsigned Idx = 1;
1481 unsigned NumParams = Record[Idx++];
1482 llvm::SmallVector<QualType, 16> ParamTypes;
1483 for (unsigned I = 0; I != NumParams; ++I)
1484 ParamTypes.push_back(GetType(Record[Idx++]));
1485 bool isVariadic = Record[Idx++];
1486 unsigned Quals = Record[Idx++];
1487 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1488 isVariadic, Quals);
1489 }
1490
1491 case pch::TYPE_TYPEDEF:
1492 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1493 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1494
1495 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001496 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001497
1498 case pch::TYPE_TYPEOF: {
1499 if (Record.size() != 1) {
1500 Error("Incorrect encoding of typeof(type) in PCH file");
1501 return QualType();
1502 }
1503 QualType UnderlyingType = GetType(Record[0]);
1504 return Context.getTypeOfType(UnderlyingType);
1505 }
1506
1507 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001508 assert(Record.size() == 1 && "Incorrect encoding of record type");
1509 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001510
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001511 case pch::TYPE_ENUM:
1512 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1513 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1514
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001515 case pch::TYPE_OBJC_INTERFACE:
1516 // FIXME: Deserialize ObjCInterfaceType
1517 assert(false && "Cannot de-serialize ObjC interface types yet");
1518 return QualType();
1519
1520 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1521 // FIXME: Deserialize ObjCQualifiedInterfaceType
1522 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1523 return QualType();
1524
1525 case pch::TYPE_OBJC_QUALIFIED_ID:
1526 // FIXME: Deserialize ObjCQualifiedIdType
1527 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1528 return QualType();
1529
1530 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1531 // FIXME: Deserialize ObjCQualifiedClassType
1532 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1533 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001534 }
1535
1536 // Suppress a GCC warning
1537 return QualType();
1538}
1539
1540/// \brief Note that we have loaded the declaration with the given
1541/// Index.
1542///
1543/// This routine notes that this declaration has already been loaded,
1544/// so that future GetDecl calls will return this declaration rather
1545/// than trying to load a new declaration.
1546inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1547 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1548 DeclAlreadyLoaded[Index] = true;
1549 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1550}
1551
1552/// \brief Read the declaration at the given offset from the PCH file.
1553Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001554 // Keep track of where we are in the stream, then jump back there
1555 // after reading this declaration.
1556 SavedStreamPosition SavedPosition(Stream);
1557
Douglas Gregor2cf26342009-04-09 22:27:44 +00001558 Decl *D = 0;
1559 Stream.JumpToBit(Offset);
1560 RecordData Record;
1561 unsigned Code = Stream.ReadCode();
1562 unsigned Idx = 0;
1563 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001564
Douglas Gregor2cf26342009-04-09 22:27:44 +00001565 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001566 case pch::DECL_ATTR:
1567 case pch::DECL_CONTEXT_LEXICAL:
1568 case pch::DECL_CONTEXT_VISIBLE:
1569 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1570 break;
1571
Douglas Gregor2cf26342009-04-09 22:27:44 +00001572 case pch::DECL_TRANSLATION_UNIT:
1573 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregor2cf26342009-04-09 22:27:44 +00001574 D = Context.getTranslationUnitDecl();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001575 break;
1576
1577 case pch::DECL_TYPEDEF: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001578 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001579 break;
1580 }
1581
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001582 case pch::DECL_ENUM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001583 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001584 break;
1585 }
1586
Douglas Gregor8c700062009-04-13 21:20:57 +00001587 case pch::DECL_RECORD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001588 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1589 0, 0);
Douglas Gregor8c700062009-04-13 21:20:57 +00001590 break;
1591 }
1592
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001593 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001594 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1595 0, llvm::APSInt());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001596 break;
1597 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001598
1599 case pch::DECL_FUNCTION: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001600 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1601 QualType());
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001602 break;
1603 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001604
Douglas Gregor8c700062009-04-13 21:20:57 +00001605 case pch::DECL_FIELD: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001606 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1607 false);
Douglas Gregor8c700062009-04-13 21:20:57 +00001608 break;
1609 }
1610
Douglas Gregor2cf26342009-04-09 22:27:44 +00001611 case pch::DECL_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001612 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1613 VarDecl::None, SourceLocation());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001614 break;
1615 }
1616
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001617 case pch::DECL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001618 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1619 VarDecl::None, 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001620 break;
1621 }
1622
1623 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001624 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001625 QualType(), QualType(), VarDecl::None,
1626 0);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001627 break;
1628 }
1629
Douglas Gregor1028bc62009-04-13 22:49:25 +00001630 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001631 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor1028bc62009-04-13 22:49:25 +00001632 break;
1633 }
1634
1635 case pch::DECL_BLOCK: {
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001636 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor1028bc62009-04-13 22:49:25 +00001637 break;
1638 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001639 }
1640
Douglas Gregorcb70bb22009-04-16 22:29:51 +00001641 assert(D && "Unknown declaration creating PCH file");
1642 if (D) {
1643 LoadedDecl(Index, D);
1644 Reader.Visit(D);
1645 }
1646
Douglas Gregor2cf26342009-04-09 22:27:44 +00001647 // If this declaration is also a declaration context, get the
1648 // offsets for its tables of lexical and visible declarations.
1649 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1650 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1651 if (Offsets.first || Offsets.second) {
1652 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1653 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1654 DeclContextOffsets[DC] = Offsets;
1655 }
1656 }
1657 assert(Idx == Record.size());
1658
1659 return D;
1660}
1661
Douglas Gregor8038d512009-04-10 17:25:41 +00001662QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001663 unsigned Quals = ID & 0x07;
1664 unsigned Index = ID >> 3;
1665
1666 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1667 QualType T;
1668 switch ((pch::PredefinedTypeIDs)Index) {
1669 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1670 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1671 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1672
1673 case pch::PREDEF_TYPE_CHAR_U_ID:
1674 case pch::PREDEF_TYPE_CHAR_S_ID:
1675 // FIXME: Check that the signedness of CharTy is correct!
1676 T = Context.CharTy;
1677 break;
1678
1679 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1680 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1681 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1682 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1683 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1684 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1685 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1686 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1687 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1688 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1689 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1690 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1691 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1692 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1693 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1694 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1695 }
1696
1697 assert(!T.isNull() && "Unknown predefined type");
1698 return T.getQualifiedType(Quals);
1699 }
1700
1701 Index -= pch::NUM_PREDEF_TYPE_IDS;
1702 if (!TypeAlreadyLoaded[Index]) {
1703 // Load the type from the PCH file.
1704 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1705 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1706 TypeAlreadyLoaded[Index] = true;
1707 }
1708
1709 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1710}
1711
Douglas Gregor8038d512009-04-10 17:25:41 +00001712Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001713 if (ID == 0)
1714 return 0;
1715
1716 unsigned Index = ID - 1;
1717 if (DeclAlreadyLoaded[Index])
1718 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1719
1720 // Load the declaration from the PCH file.
1721 return ReadDeclRecord(DeclOffsets[Index], Index);
1722}
1723
1724bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001725 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001726 assert(DC->hasExternalLexicalStorage() &&
1727 "DeclContext has no lexical decls in storage");
1728 uint64_t Offset = DeclContextOffsets[DC].first;
1729 assert(Offset && "DeclContext has no lexical decls in storage");
1730
Douglas Gregor0b748912009-04-14 21:18:50 +00001731 // Keep track of where we are in the stream, then jump back there
1732 // after reading this context.
1733 SavedStreamPosition SavedPosition(Stream);
1734
Douglas Gregor2cf26342009-04-09 22:27:44 +00001735 // Load the record containing all of the declarations lexically in
1736 // this context.
1737 Stream.JumpToBit(Offset);
1738 RecordData Record;
1739 unsigned Code = Stream.ReadCode();
1740 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001741 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001742 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1743
1744 // Load all of the declaration IDs
1745 Decls.clear();
1746 Decls.insert(Decls.end(), Record.begin(), Record.end());
1747 return false;
1748}
1749
1750bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1751 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1752 assert(DC->hasExternalVisibleStorage() &&
1753 "DeclContext has no visible decls in storage");
1754 uint64_t Offset = DeclContextOffsets[DC].second;
1755 assert(Offset && "DeclContext has no visible decls in storage");
1756
Douglas Gregor0b748912009-04-14 21:18:50 +00001757 // Keep track of where we are in the stream, then jump back there
1758 // after reading this context.
1759 SavedStreamPosition SavedPosition(Stream);
1760
Douglas Gregor2cf26342009-04-09 22:27:44 +00001761 // Load the record containing all of the declarations visible in
1762 // this context.
1763 Stream.JumpToBit(Offset);
1764 RecordData Record;
1765 unsigned Code = Stream.ReadCode();
1766 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001767 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001768 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1769 if (Record.size() == 0)
1770 return false;
1771
1772 Decls.clear();
1773
1774 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001775 while (Idx < Record.size()) {
1776 Decls.push_back(VisibleDeclaration());
1777 Decls.back().Name = ReadDeclarationName(Record, Idx);
1778
Douglas Gregor2cf26342009-04-09 22:27:44 +00001779 unsigned Size = Record[Idx++];
1780 llvm::SmallVector<unsigned, 4> & LoadedDecls
1781 = Decls.back().Declarations;
1782 LoadedDecls.reserve(Size);
1783 for (unsigned I = 0; I < Size; ++I)
1784 LoadedDecls.push_back(Record[Idx++]);
1785 }
1786
1787 return false;
1788}
1789
Douglas Gregorfdd01722009-04-14 00:24:19 +00001790void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1791 if (!Consumer)
1792 return;
1793
1794 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1795 Decl *D = GetDecl(ExternalDefinitions[I]);
1796 DeclGroupRef DG(D);
1797 Consumer->HandleTopLevelDecl(DG);
1798 }
1799}
1800
Douglas Gregor2cf26342009-04-09 22:27:44 +00001801void PCHReader::PrintStats() {
1802 std::fprintf(stderr, "*** PCH Statistics:\n");
1803
1804 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1805 TypeAlreadyLoaded.end(),
1806 true);
1807 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1808 DeclAlreadyLoaded.end(),
1809 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001810 unsigned NumIdentifiersLoaded = 0;
1811 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1812 if ((IdentifierData[I] & 0x01) == 0)
1813 ++NumIdentifiersLoaded;
1814 }
1815
Douglas Gregor2cf26342009-04-09 22:27:44 +00001816 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1817 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001818 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001819 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1820 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001821 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1822 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1823 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1824 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001825 std::fprintf(stderr, "\n");
1826}
1827
Chris Lattner7356a312009-04-11 21:15:38 +00001828IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001829 if (ID == 0)
1830 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001831
Douglas Gregorafaf3082009-04-11 00:14:32 +00001832 if (!IdentifierTable || IdentifierData.empty()) {
1833 Error("No identifier table in PCH file");
1834 return 0;
1835 }
Chris Lattner7356a312009-04-11 21:15:38 +00001836
Douglas Gregorafaf3082009-04-11 00:14:32 +00001837 if (IdentifierData[ID - 1] & 0x01) {
1838 uint64_t Offset = IdentifierData[ID - 1];
1839 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001840 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001841 }
Chris Lattner7356a312009-04-11 21:15:38 +00001842
1843 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001844}
1845
1846DeclarationName
1847PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1848 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1849 switch (Kind) {
1850 case DeclarationName::Identifier:
1851 return DeclarationName(GetIdentifierInfo(Record, Idx));
1852
1853 case DeclarationName::ObjCZeroArgSelector:
1854 case DeclarationName::ObjCOneArgSelector:
1855 case DeclarationName::ObjCMultiArgSelector:
1856 assert(false && "Unable to de-serialize Objective-C selectors");
1857 break;
1858
1859 case DeclarationName::CXXConstructorName:
1860 return Context.DeclarationNames.getCXXConstructorName(
1861 GetType(Record[Idx++]));
1862
1863 case DeclarationName::CXXDestructorName:
1864 return Context.DeclarationNames.getCXXDestructorName(
1865 GetType(Record[Idx++]));
1866
1867 case DeclarationName::CXXConversionFunctionName:
1868 return Context.DeclarationNames.getCXXConversionFunctionName(
1869 GetType(Record[Idx++]));
1870
1871 case DeclarationName::CXXOperatorName:
1872 return Context.DeclarationNames.getCXXOperatorName(
1873 (OverloadedOperatorKind)Record[Idx++]);
1874
1875 case DeclarationName::CXXUsingDirective:
1876 return DeclarationName::getUsingDirectiveName();
1877 }
1878
1879 // Required to silence GCC warning
1880 return DeclarationName();
1881}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001882
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001883/// \brief Read an integral value
1884llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1885 unsigned BitWidth = Record[Idx++];
1886 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1887 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1888 Idx += NumWords;
1889 return Result;
1890}
1891
1892/// \brief Read a signed integral value
1893llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1894 bool isUnsigned = Record[Idx++];
1895 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1896}
1897
Douglas Gregor17fc2232009-04-14 21:55:33 +00001898/// \brief Read a floating-point value
1899llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00001900 return llvm::APFloat(ReadAPInt(Record, Idx));
1901}
1902
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001903// \brief Read a string
1904std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1905 unsigned Len = Record[Idx++];
1906 std::string Result(&Record[Idx], &Record[Idx] + Len);
1907 Idx += Len;
1908 return Result;
1909}
1910
1911/// \brief Reads attributes from the current stream position.
1912Attr *PCHReader::ReadAttributes() {
1913 unsigned Code = Stream.ReadCode();
1914 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1915 "Expected unabbreviated record"); (void)Code;
1916
1917 RecordData Record;
1918 unsigned Idx = 0;
1919 unsigned RecCode = Stream.ReadRecord(Code, Record);
1920 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1921 (void)RecCode;
1922
1923#define SIMPLE_ATTR(Name) \
1924 case Attr::Name: \
1925 New = ::new (Context) Name##Attr(); \
1926 break
1927
1928#define STRING_ATTR(Name) \
1929 case Attr::Name: \
1930 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1931 break
1932
1933#define UNSIGNED_ATTR(Name) \
1934 case Attr::Name: \
1935 New = ::new (Context) Name##Attr(Record[Idx++]); \
1936 break
1937
1938 Attr *Attrs = 0;
1939 while (Idx < Record.size()) {
1940 Attr *New = 0;
1941 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1942 bool IsInherited = Record[Idx++];
1943
1944 switch (Kind) {
1945 STRING_ATTR(Alias);
1946 UNSIGNED_ATTR(Aligned);
1947 SIMPLE_ATTR(AlwaysInline);
1948 SIMPLE_ATTR(AnalyzerNoReturn);
1949 STRING_ATTR(Annotate);
1950 STRING_ATTR(AsmLabel);
1951
1952 case Attr::Blocks:
1953 New = ::new (Context) BlocksAttr(
1954 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1955 break;
1956
1957 case Attr::Cleanup:
1958 New = ::new (Context) CleanupAttr(
1959 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1960 break;
1961
1962 SIMPLE_ATTR(Const);
1963 UNSIGNED_ATTR(Constructor);
1964 SIMPLE_ATTR(DLLExport);
1965 SIMPLE_ATTR(DLLImport);
1966 SIMPLE_ATTR(Deprecated);
1967 UNSIGNED_ATTR(Destructor);
1968 SIMPLE_ATTR(FastCall);
1969
1970 case Attr::Format: {
1971 std::string Type = ReadString(Record, Idx);
1972 unsigned FormatIdx = Record[Idx++];
1973 unsigned FirstArg = Record[Idx++];
1974 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1975 break;
1976 }
1977
1978 SIMPLE_ATTR(GNUCInline);
1979
1980 case Attr::IBOutletKind:
1981 New = ::new (Context) IBOutletAttr();
1982 break;
1983
1984 SIMPLE_ATTR(NoReturn);
1985 SIMPLE_ATTR(NoThrow);
1986 SIMPLE_ATTR(Nodebug);
1987 SIMPLE_ATTR(Noinline);
1988
1989 case Attr::NonNull: {
1990 unsigned Size = Record[Idx++];
1991 llvm::SmallVector<unsigned, 16> ArgNums;
1992 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1993 Idx += Size;
1994 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1995 break;
1996 }
1997
1998 SIMPLE_ATTR(ObjCException);
1999 SIMPLE_ATTR(ObjCNSObject);
2000 SIMPLE_ATTR(Overloadable);
2001 UNSIGNED_ATTR(Packed);
2002 SIMPLE_ATTR(Pure);
2003 UNSIGNED_ATTR(Regparm);
2004 STRING_ATTR(Section);
2005 SIMPLE_ATTR(StdCall);
2006 SIMPLE_ATTR(TransparentUnion);
2007 SIMPLE_ATTR(Unavailable);
2008 SIMPLE_ATTR(Unused);
2009 SIMPLE_ATTR(Used);
2010
2011 case Attr::Visibility:
2012 New = ::new (Context) VisibilityAttr(
2013 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2014 break;
2015
2016 SIMPLE_ATTR(WarnUnusedResult);
2017 SIMPLE_ATTR(Weak);
2018 SIMPLE_ATTR(WeakImport);
2019 }
2020
2021 assert(New && "Unable to decode attribute?");
2022 New->setInherited(IsInherited);
2023 New->setNext(Attrs);
2024 Attrs = New;
2025 }
2026#undef UNSIGNED_ATTR
2027#undef STRING_ATTR
2028#undef SIMPLE_ATTR
2029
2030 // The list of attributes was built backwards. Reverse the list
2031 // before returning it.
2032 Attr *PrevAttr = 0, *NextAttr = 0;
2033 while (Attrs) {
2034 NextAttr = Attrs->getNext();
2035 Attrs->setNext(PrevAttr);
2036 PrevAttr = Attrs;
2037 Attrs = NextAttr;
2038 }
2039
2040 return PrevAttr;
2041}
2042
Douglas Gregorc9490c02009-04-16 22:23:12 +00002043Stmt *PCHReader::ReadStmt() {
Douglas Gregor087fd532009-04-14 23:32:43 +00002044 // Within the bitstream, expressions are stored in Reverse Polish
2045 // Notation, with each of the subexpressions preceding the
2046 // expression they are stored in. To evaluate expressions, we
2047 // continue reading expressions and placing them on the stack, with
2048 // expressions having operands removing those operands from the
Douglas Gregorc9490c02009-04-16 22:23:12 +00002049 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregor087fd532009-04-14 23:32:43 +00002050 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00002051 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00002052 unsigned Idx;
Douglas Gregorc9490c02009-04-16 22:23:12 +00002053 llvm::SmallVector<Stmt *, 16> StmtStack;
2054 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00002055 Stmt::EmptyShell Empty;
2056
Douglas Gregor087fd532009-04-14 23:32:43 +00002057 while (true) {
2058 unsigned Code = Stream.ReadCode();
2059 if (Code == llvm::bitc::END_BLOCK) {
2060 if (Stream.ReadBlockEnd()) {
2061 Error("Error at end of Source Manager block");
2062 return 0;
2063 }
2064 break;
2065 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002066
Douglas Gregor087fd532009-04-14 23:32:43 +00002067 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2068 // No known subblocks, always skip them.
2069 Stream.ReadSubBlockID();
2070 if (Stream.SkipBlock()) {
2071 Error("Malformed block record");
2072 return 0;
2073 }
2074 continue;
2075 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00002076
Douglas Gregor087fd532009-04-14 23:32:43 +00002077 if (Code == llvm::bitc::DEFINE_ABBREV) {
2078 Stream.ReadAbbrevRecord();
2079 continue;
2080 }
Douglas Gregor0b748912009-04-14 21:18:50 +00002081
Douglas Gregorc9490c02009-04-16 22:23:12 +00002082 Stmt *S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002083 Idx = 0;
2084 Record.clear();
2085 bool Finished = false;
2086 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc9490c02009-04-16 22:23:12 +00002087 case pch::STMT_STOP:
Douglas Gregor087fd532009-04-14 23:32:43 +00002088 Finished = true;
2089 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002090
Douglas Gregorc9490c02009-04-16 22:23:12 +00002091 case pch::STMT_NULL_PTR:
2092 S = 0;
Douglas Gregor087fd532009-04-14 23:32:43 +00002093 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00002094
Douglas Gregor025452f2009-04-17 00:04:06 +00002095 case pch::STMT_NULL:
2096 S = new (Context) NullStmt(Empty);
2097 break;
2098
2099 case pch::STMT_COMPOUND:
2100 S = new (Context) CompoundStmt(Empty);
2101 break;
2102
2103 case pch::STMT_CASE:
2104 S = new (Context) CaseStmt(Empty);
2105 break;
2106
2107 case pch::STMT_DEFAULT:
2108 S = new (Context) DefaultStmt(Empty);
2109 break;
2110
2111 case pch::STMT_IF:
2112 S = new (Context) IfStmt(Empty);
2113 break;
2114
2115 case pch::STMT_SWITCH:
2116 S = new (Context) SwitchStmt(Empty);
2117 break;
2118
2119 case pch::STMT_BREAK:
2120 S = new (Context) BreakStmt(Empty);
2121 break;
2122
Douglas Gregor087fd532009-04-14 23:32:43 +00002123 case pch::EXPR_PREDEFINED:
2124 // FIXME: untested (until we can serialize function bodies).
Douglas Gregorc9490c02009-04-16 22:23:12 +00002125 S = new (Context) PredefinedExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002126 break;
2127
2128 case pch::EXPR_DECL_REF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002129 S = new (Context) DeclRefExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002130 break;
2131
2132 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002133 S = new (Context) IntegerLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002134 break;
2135
2136 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002137 S = new (Context) FloatingLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002138 break;
2139
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002140 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002141 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002142 break;
2143
Douglas Gregor673ecd62009-04-15 16:35:07 +00002144 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002145 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor673ecd62009-04-15 16:35:07 +00002146 Record[PCHStmtReader::NumExprFields + 1]);
2147 break;
2148
Douglas Gregor087fd532009-04-14 23:32:43 +00002149 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002150 S = new (Context) CharacterLiteral(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002151 break;
2152
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002153 case pch::EXPR_PAREN:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002154 S = new (Context) ParenExpr(Empty);
Douglas Gregorc04db4f2009-04-14 23:59:37 +00002155 break;
2156
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002157 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002158 S = new (Context) UnaryOperator(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002159 break;
2160
2161 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002162 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00002163 break;
2164
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002165 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002166 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregorcb2ca732009-04-15 22:19:53 +00002167 break;
2168
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002169 case pch::EXPR_CALL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002170 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002171 break;
2172
2173 case pch::EXPR_MEMBER:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002174 S = new (Context) MemberExpr(Empty);
Douglas Gregor1f0d0132009-04-15 17:43:59 +00002175 break;
2176
Douglas Gregordb600c32009-04-15 00:25:59 +00002177 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002178 S = new (Context) BinaryOperator(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002179 break;
2180
Douglas Gregorad90e962009-04-15 22:40:36 +00002181 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002182 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002183 break;
2184
2185 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002186 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorad90e962009-04-15 22:40:36 +00002187 break;
2188
Douglas Gregor087fd532009-04-14 23:32:43 +00002189 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002190 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregor087fd532009-04-14 23:32:43 +00002191 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00002192
2193 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002194 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregordb600c32009-04-15 00:25:59 +00002195 break;
Douglas Gregord3c98a02009-04-15 23:02:49 +00002196
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002197 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002198 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorba6d7e72009-04-16 02:33:48 +00002199 break;
2200
Douglas Gregord3c98a02009-04-15 23:02:49 +00002201 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002202 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002203 break;
2204
Douglas Gregord077d752009-04-16 00:55:48 +00002205 case pch::EXPR_INIT_LIST:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002206 S = new (Context) InitListExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002207 break;
2208
2209 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002210 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregord077d752009-04-16 00:55:48 +00002211 Record[PCHStmtReader::NumExprFields] - 1);
2212
2213 break;
2214
2215 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002216 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregord077d752009-04-16 00:55:48 +00002217 break;
2218
Douglas Gregord3c98a02009-04-15 23:02:49 +00002219 case pch::EXPR_VA_ARG:
2220 // FIXME: untested; we need function bodies first
Douglas Gregorc9490c02009-04-16 22:23:12 +00002221 S = new (Context) VAArgExpr(Empty);
Douglas Gregord3c98a02009-04-15 23:02:49 +00002222 break;
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002223
2224 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002225 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002226 break;
2227
2228 case pch::EXPR_CHOOSE:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002229 S = new (Context) ChooseExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002230 break;
2231
2232 case pch::EXPR_GNU_NULL:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002233 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor44cae0c2009-04-15 23:33:31 +00002234 break;
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002235
2236 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc9490c02009-04-16 22:23:12 +00002237 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002238 break;
2239
2240 case pch::EXPR_BLOCK_DECL_REF:
2241 // FIXME: untested until we have statement and block support
Douglas Gregorc9490c02009-04-16 22:23:12 +00002242 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002243 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00002244 }
2245
Douglas Gregorc9490c02009-04-16 22:23:12 +00002246 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregor087fd532009-04-14 23:32:43 +00002247 if (Finished)
2248 break;
2249
Douglas Gregorc9490c02009-04-16 22:23:12 +00002250 if (S) {
2251 unsigned NumSubStmts = Reader.Visit(S);
2252 while (NumSubStmts > 0) {
2253 StmtStack.pop_back();
2254 --NumSubStmts;
Douglas Gregor087fd532009-04-14 23:32:43 +00002255 }
2256 }
2257
2258 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc9490c02009-04-16 22:23:12 +00002259 StmtStack.push_back(S);
Douglas Gregor0b748912009-04-14 21:18:50 +00002260 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002261 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
2262 return StmtStack.back();
2263}
2264
2265Expr *PCHReader::ReadExpr() {
2266 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregor0b748912009-04-14 21:18:50 +00002267}
2268
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002269DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002270 return Diag(SourceLocation(), DiagID);
2271}
2272
2273DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2274 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002275 Context.getSourceManager()),
2276 DiagID);
2277}
Douglas Gregor025452f2009-04-17 00:04:06 +00002278
2279/// \brief Record that the given ID maps to the given switch-case
2280/// statement.
2281void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2282 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2283 SwitchCaseStmts[ID] = SC;
2284}
2285
2286/// \brief Retrieve the switch-case statement with the given ID.
2287SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2288 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2289 return SwitchCaseStmts[ID];
2290}