blob: 0bd254f1ca812a7daa0ec7b181663adebd545313 [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 Gregor0b748912009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000022#include "clang/Lex/MacroInfo.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000023#include "clang/Lex/Preprocessor.h"
24#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000025#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "llvm/Bitcode/BitstreamReader.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include <algorithm>
32#include <cstdio>
33
34using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Declaration deserialization
38//===----------------------------------------------------------------------===//
39namespace {
40 class VISIBILITY_HIDDEN PCHDeclReader {
41 PCHReader &Reader;
42 const PCHReader::RecordData &Record;
43 unsigned &Idx;
44
45 public:
46 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
47 unsigned &Idx)
48 : Reader(Reader), Record(Record), Idx(Idx) { }
49
50 void VisitDecl(Decl *D);
51 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52 void VisitNamedDecl(NamedDecl *ND);
53 void VisitTypeDecl(TypeDecl *TD);
54 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000055 void VisitTagDecl(TagDecl *TD);
56 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor8c700062009-04-13 21:20:57 +000057 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000058 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor0a2b45e2009-04-13 18:14:40 +000059 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000060 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor8c700062009-04-13 21:20:57 +000061 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000062 void VisitVarDecl(VarDecl *VD);
Douglas Gregor3a2f7e42009-04-13 22:18:37 +000063 void VisitParmVarDecl(ParmVarDecl *PD);
64 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor1028bc62009-04-13 22:49:25 +000065 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
66 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000067 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
68 };
69}
70
71void PCHDeclReader::VisitDecl(Decl *D) {
72 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
73 D->setLexicalDeclContext(
74 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
76 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor68a2eb02009-04-15 21:30:51 +000077 if (Record[Idx++])
78 D->addAttr(Reader.ReadAttributes());
Douglas Gregor2cf26342009-04-09 22:27:44 +000079 D->setImplicit(Record[Idx++]);
80 D->setAccess((AccessSpecifier)Record[Idx++]);
81}
82
83void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
84 VisitDecl(TU);
85}
86
87void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
88 VisitDecl(ND);
89 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
90}
91
92void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
93 VisitNamedDecl(TD);
Douglas Gregor2cf26342009-04-09 22:27:44 +000094 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
95}
96
97void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregorb4e715b2009-04-13 20:46:52 +000098 // Note that we cannot use VisitTypeDecl here, because we need to
99 // set the underlying type of the typedef *before* we try to read
100 // the type associated with the TypedefDecl.
101 VisitNamedDecl(TD);
102 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
103 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
104 Idx += 2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000107void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
108 VisitTypeDecl(TD);
109 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
110 TD->setDefinition(Record[Idx++]);
111 TD->setTypedefForAnonDecl(
112 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
113}
114
115void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
116 VisitTagDecl(ED);
117 ED->setIntegerType(Reader.GetType(Record[Idx++]));
118}
119
Douglas Gregor8c700062009-04-13 21:20:57 +0000120void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
121 VisitTagDecl(RD);
122 RD->setHasFlexibleArrayMember(Record[Idx++]);
123 RD->setAnonymousStructOrUnion(Record[Idx++]);
124}
125
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
127 VisitNamedDecl(VD);
128 VD->setType(Reader.GetType(Record[Idx++]));
129}
130
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000131void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
132 VisitValueDecl(ECD);
Douglas Gregor0b748912009-04-14 21:18:50 +0000133 if (Record[Idx++])
134 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor0a2b45e2009-04-13 18:14:40 +0000135 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
136}
137
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000138void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
139 VisitValueDecl(FD);
140 // FIXME: function body
141 FD->setPreviousDeclaration(
142 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
143 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
144 FD->setInline(Record[Idx++]);
145 FD->setVirtual(Record[Idx++]);
146 FD->setPure(Record[Idx++]);
147 FD->setInheritedPrototype(Record[Idx++]);
148 FD->setHasPrototype(Record[Idx++]);
149 FD->setDeleted(Record[Idx++]);
150 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
151 unsigned NumParams = Record[Idx++];
152 llvm::SmallVector<ParmVarDecl *, 16> Params;
153 Params.reserve(NumParams);
154 for (unsigned I = 0; I != NumParams; ++I)
155 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
156 FD->setParams(Reader.getContext(), &Params[0], NumParams);
157}
158
Douglas Gregor8c700062009-04-13 21:20:57 +0000159void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
160 VisitValueDecl(FD);
161 FD->setMutable(Record[Idx++]);
Douglas Gregor0b748912009-04-14 21:18:50 +0000162 if (Record[Idx++])
163 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor8c700062009-04-13 21:20:57 +0000164}
165
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
167 VisitValueDecl(VD);
168 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
169 VD->setThreadSpecified(Record[Idx++]);
170 VD->setCXXDirectInitializer(Record[Idx++]);
171 VD->setDeclaredInCondition(Record[Idx++]);
172 VD->setPreviousDeclaration(
173 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
174 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor0b748912009-04-14 21:18:50 +0000175 if (Record[Idx++])
176 VD->setInit(Reader.ReadExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177}
178
Douglas Gregor3a2f7e42009-04-13 22:18:37 +0000179void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
180 VisitVarDecl(PD);
181 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
182 // FIXME: default argument
183}
184
185void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
186 VisitParmVarDecl(PD);
187 PD->setOriginalType(Reader.GetType(Record[Idx++]));
188}
189
Douglas Gregor1028bc62009-04-13 22:49:25 +0000190void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
191 VisitDecl(AD);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000192 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor1028bc62009-04-13 22:49:25 +0000193}
194
195void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
196 VisitDecl(BD);
197 unsigned NumParams = Record[Idx++];
198 llvm::SmallVector<ParmVarDecl *, 16> Params;
199 Params.reserve(NumParams);
200 for (unsigned I = 0; I != NumParams; ++I)
201 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
202 BD->setParams(Reader.getContext(), &Params[0], NumParams);
203}
204
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205std::pair<uint64_t, uint64_t>
206PCHDeclReader::VisitDeclContext(DeclContext *DC) {
207 uint64_t LexicalOffset = Record[Idx++];
208 uint64_t VisibleOffset = 0;
209 if (DC->getPrimaryContext() == DC)
210 VisibleOffset = Record[Idx++];
211 return std::make_pair(LexicalOffset, VisibleOffset);
212}
213
Douglas Gregor0b748912009-04-14 21:18:50 +0000214//===----------------------------------------------------------------------===//
215// Statement/expression deserialization
216//===----------------------------------------------------------------------===//
217namespace {
218 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregor087fd532009-04-14 23:32:43 +0000219 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregor0b748912009-04-14 21:18:50 +0000220 PCHReader &Reader;
221 const PCHReader::RecordData &Record;
222 unsigned &Idx;
Douglas Gregor087fd532009-04-14 23:32:43 +0000223 llvm::SmallVectorImpl<Expr *> &ExprStack;
Douglas Gregor0b748912009-04-14 21:18:50 +0000224
225 public:
226 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregor087fd532009-04-14 23:32:43 +0000227 unsigned &Idx, llvm::SmallVectorImpl<Expr *> &ExprStack)
228 : Reader(Reader), Record(Record), Idx(Idx), ExprStack(ExprStack) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000229
Douglas Gregor673ecd62009-04-15 16:35:07 +0000230 /// \brief The number of record fields required for the Expr class
231 /// itself.
232 static const unsigned NumExprFields = 3;
233
Douglas Gregor087fd532009-04-14 23:32:43 +0000234 // Each of the Visit* functions reads in part of the expression
235 // from the given record and the current expression stack, then
236 // return the total number of operands that it read from the
237 // expression stack.
238
239 unsigned VisitExpr(Expr *E);
240 unsigned VisitPredefinedExpr(PredefinedExpr *E);
241 unsigned VisitDeclRefExpr(DeclRefExpr *E);
242 unsigned VisitIntegerLiteral(IntegerLiteral *E);
243 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000244 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000245 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000246 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000247 unsigned VisitUnaryOperator(UnaryOperator *E);
248 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000249 unsigned VisitCallExpr(CallExpr *E);
250 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000251 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000252 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregor087fd532009-04-14 23:32:43 +0000253 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregordb600c32009-04-15 00:25:59 +0000254 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
255 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor0b748912009-04-14 21:18:50 +0000256 };
257}
258
Douglas Gregor087fd532009-04-14 23:32:43 +0000259unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000260 E->setType(Reader.GetType(Record[Idx++]));
261 E->setTypeDependent(Record[Idx++]);
262 E->setValueDependent(Record[Idx++]);
Douglas Gregor673ecd62009-04-15 16:35:07 +0000263 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregor087fd532009-04-14 23:32:43 +0000264 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000265}
266
Douglas Gregor087fd532009-04-14 23:32:43 +0000267unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000268 VisitExpr(E);
269 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
270 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000271 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000272}
273
Douglas Gregor087fd532009-04-14 23:32:43 +0000274unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000275 VisitExpr(E);
276 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
277 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000278 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000279}
280
Douglas Gregor087fd532009-04-14 23:32:43 +0000281unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000282 VisitExpr(E);
283 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
284 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregor087fd532009-04-14 23:32:43 +0000285 return 0;
Douglas Gregor0b748912009-04-14 21:18:50 +0000286}
287
Douglas Gregor087fd532009-04-14 23:32:43 +0000288unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregor17fc2232009-04-14 21:55:33 +0000289 VisitExpr(E);
290 E->setValue(Reader.ReadAPFloat(Record, Idx));
291 E->setExact(Record[Idx++]);
292 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor087fd532009-04-14 23:32:43 +0000293 return 0;
Douglas Gregor17fc2232009-04-14 21:55:33 +0000294}
295
Douglas Gregor673ecd62009-04-15 16:35:07 +0000296unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
297 VisitExpr(E);
298 unsigned Len = Record[Idx++];
299 assert(Record[Idx] == E->getNumConcatenated() &&
300 "Wrong number of concatenated tokens!");
301 ++Idx;
302 E->setWide(Record[Idx++]);
303
304 // Read string data
305 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
306 E->setStrData(Reader.getContext(), &Str[0], Len);
307 Idx += Len;
308
309 // Read source locations
310 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
311 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
312
313 return 0;
314}
315
Douglas Gregor087fd532009-04-14 23:32:43 +0000316unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregor0b748912009-04-14 21:18:50 +0000317 VisitExpr(E);
318 E->setValue(Record[Idx++]);
319 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
320 E->setWide(Record[Idx++]);
Douglas Gregor087fd532009-04-14 23:32:43 +0000321 return 0;
322}
323
Douglas Gregorc04db4f2009-04-14 23:59:37 +0000324unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
325 VisitExpr(E);
326 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
327 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
328 E->setSubExpr(ExprStack.back());
329 return 1;
330}
331
Douglas Gregor0b0b77f2009-04-15 15:58:59 +0000332unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
333 VisitExpr(E);
334 E->setSubExpr(ExprStack.back());
335 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
336 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 return 1;
338}
339
340unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
341 VisitExpr(E);
342 E->setSizeof(Record[Idx++]);
343 if (Record[Idx] == 0) {
344 E->setArgument(ExprStack.back());
345 ++Idx;
346 } else {
347 E->setArgument(Reader.GetType(Record[Idx++]));
348 }
349 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
350 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
351 return E->isArgumentType()? 0 : 1;
352}
353
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000354unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
355 VisitExpr(E);
356 E->setNumArgs(Reader.getContext(), Record[Idx++]);
357 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
358 E->setCallee(ExprStack[ExprStack.size() - E->getNumArgs() - 1]);
359 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
360 E->setArg(I, ExprStack[ExprStack.size() - N + I]);
361 return E->getNumArgs() + 1;
362}
363
364unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
365 VisitExpr(E);
366 E->setBase(ExprStack.back());
367 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
368 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369 E->setArrow(Record[Idx++]);
370 return 1;
371}
372
Douglas Gregor087fd532009-04-14 23:32:43 +0000373unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
374 VisitExpr(E);
375 E->setSubExpr(ExprStack.back());
376 return 1;
377}
378
Douglas Gregordb600c32009-04-15 00:25:59 +0000379unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
380 VisitExpr(E);
381 E->setLHS(ExprStack.end()[-2]);
382 E->setRHS(ExprStack.end()[-1]);
383 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
384 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
385 return 2;
386}
387
Douglas Gregor087fd532009-04-14 23:32:43 +0000388unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
389 VisitCastExpr(E);
390 E->setLvalueCast(Record[Idx++]);
391 return 1;
Douglas Gregor0b748912009-04-14 21:18:50 +0000392}
393
Douglas Gregordb600c32009-04-15 00:25:59 +0000394unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
395 VisitCastExpr(E);
396 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
397 return 1;
398}
399
400unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
401 VisitExplicitCastExpr(E);
402 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
403 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
404 return 1;
405}
406
Douglas Gregor2cf26342009-04-09 22:27:44 +0000407// FIXME: use the diagnostics machinery
408static bool Error(const char *Str) {
409 std::fprintf(stderr, "%s\n", Str);
410 return true;
411}
412
Douglas Gregore1d918e2009-04-10 23:10:45 +0000413/// \brief Check the contents of the predefines buffer against the
414/// contents of the predefines buffer used to build the PCH file.
415///
416/// The contents of the two predefines buffers should be the same. If
417/// not, then some command-line option changed the preprocessor state
418/// and we must reject the PCH file.
419///
420/// \param PCHPredef The start of the predefines buffer in the PCH
421/// file.
422///
423/// \param PCHPredefLen The length of the predefines buffer in the PCH
424/// file.
425///
426/// \param PCHBufferID The FileID for the PCH predefines buffer.
427///
428/// \returns true if there was a mismatch (in which case the PCH file
429/// should be ignored), or false otherwise.
430bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
431 unsigned PCHPredefLen,
432 FileID PCHBufferID) {
433 const char *Predef = PP.getPredefines().c_str();
434 unsigned PredefLen = PP.getPredefines().size();
435
436 // If the two predefines buffers compare equal, we're done!.
437 if (PredefLen == PCHPredefLen &&
438 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
439 return false;
440
441 // The predefines buffers are different. Produce a reasonable
442 // diagnostic showing where they are different.
443
444 // The source locations (potentially in the two different predefines
445 // buffers)
446 SourceLocation Loc1, Loc2;
447 SourceManager &SourceMgr = PP.getSourceManager();
448
449 // Create a source buffer for our predefines string, so
450 // that we can build a diagnostic that points into that
451 // source buffer.
452 FileID BufferID;
453 if (Predef && Predef[0]) {
454 llvm::MemoryBuffer *Buffer
455 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
456 "<built-in>");
457 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
458 }
459
460 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
461 std::pair<const char *, const char *> Locations
462 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
463
464 if (Locations.first != Predef + MinLen) {
465 // We found the location in the two buffers where there is a
466 // difference. Form source locations to point there (in both
467 // buffers).
468 unsigned Offset = Locations.first - Predef;
469 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
470 .getFileLocWithOffset(Offset);
471 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
472 .getFileLocWithOffset(Offset);
473 } else if (PredefLen > PCHPredefLen) {
474 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
475 .getFileLocWithOffset(MinLen);
476 } else {
477 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
478 .getFileLocWithOffset(MinLen);
479 }
480
481 Diag(Loc1, diag::warn_pch_preprocessor);
482 if (Loc2.isValid())
483 Diag(Loc2, diag::note_predef_in_pch);
484 Diag(diag::note_ignoring_pch) << FileName;
485 return true;
486}
487
Douglas Gregorbd945002009-04-13 16:31:14 +0000488/// \brief Read the line table in the source manager block.
489/// \returns true if ther was an error.
490static bool ParseLineTable(SourceManager &SourceMgr,
491 llvm::SmallVectorImpl<uint64_t> &Record) {
492 unsigned Idx = 0;
493 LineTableInfo &LineTable = SourceMgr.getLineTable();
494
495 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000496 std::map<int, int> FileIDs;
497 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000498 // Extract the file name
499 unsigned FilenameLen = Record[Idx++];
500 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
501 Idx += FilenameLen;
Douglas Gregorff0a9872009-04-13 17:12:42 +0000502 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
503 Filename.size());
Douglas Gregorbd945002009-04-13 16:31:14 +0000504 }
505
506 // Parse the line entries
507 std::vector<LineEntry> Entries;
508 while (Idx < Record.size()) {
Douglas Gregorff0a9872009-04-13 17:12:42 +0000509 int FID = FileIDs[Record[Idx++]];
Douglas Gregorbd945002009-04-13 16:31:14 +0000510
511 // Extract the line entries
512 unsigned NumEntries = Record[Idx++];
513 Entries.clear();
514 Entries.reserve(NumEntries);
515 for (unsigned I = 0; I != NumEntries; ++I) {
516 unsigned FileOffset = Record[Idx++];
517 unsigned LineNo = Record[Idx++];
518 int FilenameID = Record[Idx++];
519 SrcMgr::CharacteristicKind FileKind
520 = (SrcMgr::CharacteristicKind)Record[Idx++];
521 unsigned IncludeOffset = Record[Idx++];
522 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
523 FileKind, IncludeOffset));
524 }
525 LineTable.AddEntry(FID, Entries);
526 }
527
528 return false;
529}
530
Douglas Gregor14f79002009-04-10 03:52:48 +0000531/// \brief Read the source manager block
Douglas Gregore1d918e2009-04-10 23:10:45 +0000532PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregor14f79002009-04-10 03:52:48 +0000533 using namespace SrcMgr;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000534 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
535 Error("Malformed source manager block record");
536 return Failure;
537 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000538
539 SourceManager &SourceMgr = Context.getSourceManager();
540 RecordData Record;
541 while (true) {
542 unsigned Code = Stream.ReadCode();
543 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregore1d918e2009-04-10 23:10:45 +0000544 if (Stream.ReadBlockEnd()) {
545 Error("Error at end of Source Manager block");
546 return Failure;
547 }
548
549 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000550 }
551
552 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
553 // No known subblocks, always skip them.
554 Stream.ReadSubBlockID();
Douglas Gregore1d918e2009-04-10 23:10:45 +0000555 if (Stream.SkipBlock()) {
556 Error("Malformed block record");
557 return Failure;
558 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000559 continue;
560 }
561
562 if (Code == llvm::bitc::DEFINE_ABBREV) {
563 Stream.ReadAbbrevRecord();
564 continue;
565 }
566
567 // Read a record.
568 const char *BlobStart;
569 unsigned BlobLen;
570 Record.clear();
571 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
572 default: // Default behavior: ignore.
573 break;
574
575 case pch::SM_SLOC_FILE_ENTRY: {
576 // FIXME: We would really like to delay the creation of this
577 // FileEntry until it is actually required, e.g., when producing
578 // a diagnostic with a source location in this file.
579 const FileEntry *File
580 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
581 // FIXME: Error recovery if file cannot be found.
Douglas Gregorbd945002009-04-13 16:31:14 +0000582 FileID ID = SourceMgr.createFileID(File,
583 SourceLocation::getFromRawEncoding(Record[1]),
584 (CharacteristicKind)Record[2]);
585 if (Record[3])
586 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
587 .setHasLineDirectives();
Douglas Gregor14f79002009-04-10 03:52:48 +0000588 break;
589 }
590
591 case pch::SM_SLOC_BUFFER_ENTRY: {
592 const char *Name = BlobStart;
593 unsigned Code = Stream.ReadCode();
594 Record.clear();
595 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
596 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor6a2bfb22009-04-15 18:43:11 +0000597 (void)RecCode;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000598 llvm::MemoryBuffer *Buffer
599 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
600 BlobStart + BlobLen - 1,
601 Name);
602 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
603
604 if (strcmp(Name, "<built-in>") == 0
605 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
606 return IgnorePCH;
Douglas Gregor14f79002009-04-10 03:52:48 +0000607 break;
608 }
609
610 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
611 SourceLocation SpellingLoc
612 = SourceLocation::getFromRawEncoding(Record[1]);
613 SourceMgr.createInstantiationLoc(
614 SpellingLoc,
615 SourceLocation::getFromRawEncoding(Record[2]),
616 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregorf60e9912009-04-15 18:05:10 +0000617 Record[4]);
Douglas Gregor14f79002009-04-10 03:52:48 +0000618 break;
619 }
620
Chris Lattner2c78b872009-04-14 23:22:57 +0000621 case pch::SM_LINE_TABLE:
Douglas Gregorbd945002009-04-13 16:31:14 +0000622 if (ParseLineTable(SourceMgr, Record))
623 return Failure;
Chris Lattner2c78b872009-04-14 23:22:57 +0000624 break;
Douglas Gregor14f79002009-04-10 03:52:48 +0000625 }
626 }
627}
628
Chris Lattner42d42b52009-04-10 21:41:48 +0000629bool PCHReader::ReadPreprocessorBlock() {
630 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
631 return Error("Malformed preprocessor block record");
632
Chris Lattner42d42b52009-04-10 21:41:48 +0000633 RecordData Record;
634 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
635 MacroInfo *LastMacro = 0;
636
637 while (true) {
638 unsigned Code = Stream.ReadCode();
639 switch (Code) {
640 case llvm::bitc::END_BLOCK:
641 if (Stream.ReadBlockEnd())
642 return Error("Error at end of preprocessor block");
643 return false;
644
645 case llvm::bitc::ENTER_SUBBLOCK:
646 // No known subblocks, always skip them.
647 Stream.ReadSubBlockID();
648 if (Stream.SkipBlock())
649 return Error("Malformed block record");
650 continue;
651
652 case llvm::bitc::DEFINE_ABBREV:
653 Stream.ReadAbbrevRecord();
654 continue;
655 default: break;
656 }
657
658 // Read a record.
659 Record.clear();
660 pch::PreprocessorRecordTypes RecType =
661 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
662 switch (RecType) {
663 default: // Default behavior: ignore unknown records.
664 break;
Chris Lattnerc1f9d822009-04-13 01:29:17 +0000665 case pch::PP_COUNTER_VALUE:
666 if (!Record.empty())
667 PP.setCounterValue(Record[0]);
668 break;
669
Chris Lattner42d42b52009-04-10 21:41:48 +0000670 case pch::PP_MACRO_OBJECT_LIKE:
671 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner7356a312009-04-11 21:15:38 +0000672 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
673 if (II == 0)
674 return Error("Macro must have a name");
Chris Lattner42d42b52009-04-10 21:41:48 +0000675 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
676 bool isUsed = Record[2];
677
678 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
679 MI->setIsUsed(isUsed);
680
681 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
682 // Decode function-like macro info.
683 bool isC99VarArgs = Record[3];
684 bool isGNUVarArgs = Record[4];
685 MacroArgs.clear();
686 unsigned NumArgs = Record[5];
687 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner7356a312009-04-11 21:15:38 +0000688 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattner42d42b52009-04-10 21:41:48 +0000689
690 // Install function-like macro info.
691 MI->setIsFunctionLike();
692 if (isC99VarArgs) MI->setIsC99Varargs();
693 if (isGNUVarArgs) MI->setIsGNUVarargs();
694 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
695 PP.getPreprocessorAllocator());
696 }
697
698 // Finally, install the macro.
Chris Lattner42d42b52009-04-10 21:41:48 +0000699 PP.setMacroInfo(II, MI);
Chris Lattner42d42b52009-04-10 21:41:48 +0000700
701 // Remember that we saw this macro last so that we add the tokens that
702 // form its body to it.
703 LastMacro = MI;
704 break;
705 }
706
707 case pch::PP_TOKEN: {
708 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
709 // pretend we didn't see this.
710 if (LastMacro == 0) break;
711
712 Token Tok;
713 Tok.startToken();
714 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
715 Tok.setLength(Record[1]);
Chris Lattner7356a312009-04-11 21:15:38 +0000716 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
717 Tok.setIdentifierInfo(II);
Chris Lattner42d42b52009-04-10 21:41:48 +0000718 Tok.setKind((tok::TokenKind)Record[3]);
719 Tok.setFlag((Token::TokenFlags)Record[4]);
720 LastMacro->AddTokenToBody(Tok);
721 break;
722 }
723 }
724 }
725}
726
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000727PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
728 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
729 Error("Malformed block record");
730 return Failure;
731 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000732
Chris Lattner7356a312009-04-11 21:15:38 +0000733 uint64_t PreprocessorBlockBit = 0;
734
Douglas Gregor2cf26342009-04-09 22:27:44 +0000735 // Read all of the records and blocks for the PCH file.
Douglas Gregor8038d512009-04-10 17:25:41 +0000736 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000737 while (!Stream.AtEndOfStream()) {
738 unsigned Code = Stream.ReadCode();
739 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner7356a312009-04-11 21:15:38 +0000740 // If we saw the preprocessor block, read it now.
741 if (PreprocessorBlockBit) {
742 uint64_t SavedPos = Stream.GetCurrentBitNo();
743 Stream.JumpToBit(PreprocessorBlockBit);
744 if (ReadPreprocessorBlock()) {
745 Error("Malformed preprocessor block");
746 return Failure;
747 }
748 Stream.JumpToBit(SavedPos);
749 }
750
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000751 if (Stream.ReadBlockEnd()) {
752 Error("Error at end of module block");
753 return Failure;
754 }
Chris Lattner7356a312009-04-11 21:15:38 +0000755
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000756 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000757 }
758
759 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
760 switch (Stream.ReadSubBlockID()) {
761 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
762 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
763 default: // Skip unknown content.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000764 if (Stream.SkipBlock()) {
765 Error("Malformed block record");
766 return Failure;
767 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000768 break;
769
Chris Lattner7356a312009-04-11 21:15:38 +0000770 case pch::PREPROCESSOR_BLOCK_ID:
771 // Skip the preprocessor block for now, but remember where it is. We
772 // want to read it in after the identifier table.
773 if (PreprocessorBlockBit) {
774 Error("Multiple preprocessor blocks found.");
775 return Failure;
776 }
777 PreprocessorBlockBit = Stream.GetCurrentBitNo();
778 if (Stream.SkipBlock()) {
779 Error("Malformed block record");
780 return Failure;
781 }
782 break;
783
Douglas Gregor14f79002009-04-10 03:52:48 +0000784 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000785 switch (ReadSourceManagerBlock()) {
786 case Success:
787 break;
788
789 case Failure:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000790 Error("Malformed source manager block");
791 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000792
793 case IgnorePCH:
794 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000795 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000796 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000797 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000798 continue;
799 }
800
801 if (Code == llvm::bitc::DEFINE_ABBREV) {
802 Stream.ReadAbbrevRecord();
803 continue;
804 }
805
806 // Read and process a record.
807 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +0000808 const char *BlobStart = 0;
809 unsigned BlobLen = 0;
810 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
811 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +0000812 default: // Default behavior: ignore.
813 break;
814
815 case pch::TYPE_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816 if (!TypeOffsets.empty()) {
817 Error("Duplicate TYPE_OFFSET record in PCH file");
818 return Failure;
819 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000820 TypeOffsets.swap(Record);
821 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
822 break;
823
824 case pch::DECL_OFFSET:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000825 if (!DeclOffsets.empty()) {
826 Error("Duplicate DECL_OFFSET record in PCH file");
827 return Failure;
828 }
Douglas Gregor8038d512009-04-10 17:25:41 +0000829 DeclOffsets.swap(Record);
830 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
831 break;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000832
833 case pch::LANGUAGE_OPTIONS:
834 if (ParseLanguageOptions(Record))
835 return IgnorePCH;
836 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +0000837
Douglas Gregorafaf3082009-04-11 00:14:32 +0000838 case pch::TARGET_TRIPLE: {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000839 std::string TargetTriple(BlobStart, BlobLen);
840 if (TargetTriple != Context.Target.getTargetTriple()) {
841 Diag(diag::warn_pch_target_triple)
842 << TargetTriple << Context.Target.getTargetTriple();
843 Diag(diag::note_ignoring_pch) << FileName;
844 return IgnorePCH;
845 }
846 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000847 }
Douglas Gregorafaf3082009-04-11 00:14:32 +0000848
849 case pch::IDENTIFIER_TABLE:
850 IdentifierTable = BlobStart;
851 break;
852
853 case pch::IDENTIFIER_OFFSET:
854 if (!IdentifierData.empty()) {
855 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
856 return Failure;
857 }
858 IdentifierData.swap(Record);
859#ifndef NDEBUG
860 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
861 if ((IdentifierData[I] & 0x01) == 0) {
862 Error("Malformed identifier table in the precompiled header");
863 return Failure;
864 }
865 }
866#endif
867 break;
Douglas Gregorfdd01722009-04-14 00:24:19 +0000868
869 case pch::EXTERNAL_DEFINITIONS:
870 if (!ExternalDefinitions.empty()) {
871 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
872 return Failure;
873 }
874 ExternalDefinitions.swap(Record);
875 break;
Douglas Gregorafaf3082009-04-11 00:14:32 +0000876 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000877 }
878
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000879 Error("Premature end of bitstream");
880 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000881}
882
Douglas Gregore1d918e2009-04-10 23:10:45 +0000883PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000884 // Set the PCH file name.
885 this->FileName = FileName;
886
Douglas Gregor2cf26342009-04-09 22:27:44 +0000887 // Open the PCH file.
888 std::string ErrStr;
889 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregore1d918e2009-04-10 23:10:45 +0000890 if (!Buffer) {
891 Error(ErrStr.c_str());
892 return IgnorePCH;
893 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000894
895 // Initialize the stream
896 Stream.init((const unsigned char *)Buffer->getBufferStart(),
897 (const unsigned char *)Buffer->getBufferEnd());
898
899 // Sniff for the signature.
900 if (Stream.Read(8) != 'C' ||
901 Stream.Read(8) != 'P' ||
902 Stream.Read(8) != 'C' ||
Douglas Gregore1d918e2009-04-10 23:10:45 +0000903 Stream.Read(8) != 'H') {
904 Error("Not a PCH file");
905 return IgnorePCH;
906 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000907
908 // We expect a number of well-defined blocks, though we don't necessarily
909 // need to understand them all.
910 while (!Stream.AtEndOfStream()) {
911 unsigned Code = Stream.ReadCode();
912
Douglas Gregore1d918e2009-04-10 23:10:45 +0000913 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
914 Error("Invalid record at top-level");
915 return Failure;
916 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000917
918 unsigned BlockID = Stream.ReadSubBlockID();
919
920 // We only know the PCH subblock ID.
921 switch (BlockID) {
922 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000923 if (Stream.ReadBlockInfoBlock()) {
924 Error("Malformed BlockInfoBlock");
925 return Failure;
926 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000927 break;
928 case pch::PCH_BLOCK_ID:
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000929 switch (ReadPCHBlock()) {
930 case Success:
931 break;
932
933 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000934 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000935
936 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +0000937 // FIXME: We could consider reading through to the end of this
938 // PCH block, skipping subblocks, to see if there are other
939 // PCH blocks elsewhere.
Douglas Gregore1d918e2009-04-10 23:10:45 +0000940 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000941 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000942 break;
943 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +0000944 if (Stream.SkipBlock()) {
945 Error("Malformed block record");
946 return Failure;
947 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000948 break;
949 }
950 }
951
952 // Load the translation unit declaration
953 ReadDeclRecord(DeclOffsets[0], 0);
954
Douglas Gregore1d918e2009-04-10 23:10:45 +0000955 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000956}
957
Douglas Gregor0b748912009-04-14 21:18:50 +0000958namespace {
959 /// \brief Helper class that saves the current stream position and
960 /// then restores it when destroyed.
961 struct VISIBILITY_HIDDEN SavedStreamPosition {
962 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor62e445c2009-04-15 04:54:29 +0000963 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregor0b748912009-04-14 21:18:50 +0000964
965 ~SavedStreamPosition() {
Douglas Gregor62e445c2009-04-15 04:54:29 +0000966 Stream.JumpToBit(Offset);
Douglas Gregor0b748912009-04-14 21:18:50 +0000967 }
968
969 private:
970 llvm::BitstreamReader &Stream;
971 uint64_t Offset;
Douglas Gregor0b748912009-04-14 21:18:50 +0000972 };
973}
974
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000975/// \brief Parse the record that corresponds to a LangOptions data
976/// structure.
977///
978/// This routine compares the language options used to generate the
979/// PCH file against the language options set for the current
980/// compilation. For each option, we classify differences between the
981/// two compiler states as either "benign" or "important". Benign
982/// differences don't matter, and we accept them without complaint
983/// (and without modifying the language options). Differences between
984/// the states for important options cause the PCH file to be
985/// unusable, so we emit a warning and return true to indicate that
986/// there was an error.
987///
988/// \returns true if the PCH file is unacceptable, false otherwise.
989bool PCHReader::ParseLanguageOptions(
990 const llvm::SmallVectorImpl<uint64_t> &Record) {
991 const LangOptions &LangOpts = Context.getLangOptions();
992#define PARSE_LANGOPT_BENIGN(Option) ++Idx
993#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
994 if (Record[Idx] != LangOpts.Option) { \
995 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
996 Diag(diag::note_ignoring_pch) << FileName; \
997 return true; \
998 } \
999 ++Idx
1000
1001 unsigned Idx = 0;
1002 PARSE_LANGOPT_BENIGN(Trigraphs);
1003 PARSE_LANGOPT_BENIGN(BCPLComment);
1004 PARSE_LANGOPT_BENIGN(DollarIdents);
1005 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1006 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1007 PARSE_LANGOPT_BENIGN(ImplicitInt);
1008 PARSE_LANGOPT_BENIGN(Digraphs);
1009 PARSE_LANGOPT_BENIGN(HexFloats);
1010 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1011 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1012 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1013 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1014 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1015 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1016 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1017 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1018 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1019 PARSE_LANGOPT_BENIGN(PascalStrings);
1020 PARSE_LANGOPT_BENIGN(Boolean);
1021 PARSE_LANGOPT_BENIGN(WritableStrings);
1022 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1023 diag::warn_pch_lax_vector_conversions);
1024 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1025 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1026 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1027 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1028 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1029 diag::warn_pch_thread_safe_statics);
1030 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1031 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1032 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1033 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1034 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1035 diag::warn_pch_heinous_extensions);
1036 // FIXME: Most of the options below are benign if the macro wasn't
1037 // used. Unfortunately, this means that a PCH compiled without
1038 // optimization can't be used with optimization turned on, even
1039 // though the only thing that changes is whether __OPTIMIZE__ was
1040 // defined... but if __OPTIMIZE__ never showed up in the header, it
1041 // doesn't matter. We could consider making this some special kind
1042 // of check.
1043 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1044 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1045 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1046 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1047 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1048 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1049 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1050 Diag(diag::warn_pch_gc_mode)
1051 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1052 Diag(diag::note_ignoring_pch) << FileName;
1053 return true;
1054 }
1055 ++Idx;
1056 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1057 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1058#undef PARSE_LANGOPT_IRRELEVANT
1059#undef PARSE_LANGOPT_BENIGN
1060
1061 return false;
1062}
1063
Douglas Gregor2cf26342009-04-09 22:27:44 +00001064/// \brief Read and return the type at the given offset.
1065///
1066/// This routine actually reads the record corresponding to the type
1067/// at the given offset in the bitstream. It is a helper routine for
1068/// GetType, which deals with reading type IDs.
1069QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001070 // Keep track of where we are in the stream, then jump back there
1071 // after reading this type.
1072 SavedStreamPosition SavedPosition(Stream);
1073
Douglas Gregor2cf26342009-04-09 22:27:44 +00001074 Stream.JumpToBit(Offset);
1075 RecordData Record;
1076 unsigned Code = Stream.ReadCode();
1077 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001078 case pch::TYPE_ATTR:
1079 assert(false && "Should never jump to an attribute block");
1080 return QualType();
1081
Douglas Gregor6d473962009-04-15 22:00:08 +00001082 case pch::TYPE_EXT_QUAL: {
1083 assert(Record.size() == 3 &&
1084 "Incorrect encoding of extended qualifier type");
1085 QualType Base = GetType(Record[0]);
1086 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1087 unsigned AddressSpace = Record[2];
1088
1089 QualType T = Base;
1090 if (GCAttr != QualType::GCNone)
1091 T = Context.getObjCGCQualType(T, GCAttr);
1092 if (AddressSpace)
1093 T = Context.getAddrSpaceQualType(T, AddressSpace);
1094 return T;
1095 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001096
Douglas Gregor2cf26342009-04-09 22:27:44 +00001097 case pch::TYPE_FIXED_WIDTH_INT: {
1098 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1099 return Context.getFixedWidthIntType(Record[0], Record[1]);
1100 }
1101
1102 case pch::TYPE_COMPLEX: {
1103 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1104 QualType ElemType = GetType(Record[0]);
1105 return Context.getComplexType(ElemType);
1106 }
1107
1108 case pch::TYPE_POINTER: {
1109 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1110 QualType PointeeType = GetType(Record[0]);
1111 return Context.getPointerType(PointeeType);
1112 }
1113
1114 case pch::TYPE_BLOCK_POINTER: {
1115 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1116 QualType PointeeType = GetType(Record[0]);
1117 return Context.getBlockPointerType(PointeeType);
1118 }
1119
1120 case pch::TYPE_LVALUE_REFERENCE: {
1121 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1122 QualType PointeeType = GetType(Record[0]);
1123 return Context.getLValueReferenceType(PointeeType);
1124 }
1125
1126 case pch::TYPE_RVALUE_REFERENCE: {
1127 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1128 QualType PointeeType = GetType(Record[0]);
1129 return Context.getRValueReferenceType(PointeeType);
1130 }
1131
1132 case pch::TYPE_MEMBER_POINTER: {
1133 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1134 QualType PointeeType = GetType(Record[0]);
1135 QualType ClassType = GetType(Record[1]);
1136 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1137 }
1138
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001139 case pch::TYPE_CONSTANT_ARRAY: {
1140 QualType ElementType = GetType(Record[0]);
1141 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1142 unsigned IndexTypeQuals = Record[2];
1143 unsigned Idx = 3;
1144 llvm::APInt Size = ReadAPInt(Record, Idx);
1145 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1146 }
1147
1148 case pch::TYPE_INCOMPLETE_ARRAY: {
1149 QualType ElementType = GetType(Record[0]);
1150 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1151 unsigned IndexTypeQuals = Record[2];
1152 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1153 }
1154
1155 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregor0b748912009-04-14 21:18:50 +00001156 QualType ElementType = GetType(Record[0]);
1157 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1158 unsigned IndexTypeQuals = Record[2];
1159 return Context.getVariableArrayType(ElementType, ReadExpr(),
1160 ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001161 }
1162
1163 case pch::TYPE_VECTOR: {
1164 if (Record.size() != 2) {
1165 Error("Incorrect encoding of vector type in PCH file");
1166 return QualType();
1167 }
1168
1169 QualType ElementType = GetType(Record[0]);
1170 unsigned NumElements = Record[1];
1171 return Context.getVectorType(ElementType, NumElements);
1172 }
1173
1174 case pch::TYPE_EXT_VECTOR: {
1175 if (Record.size() != 2) {
1176 Error("Incorrect encoding of extended vector type in PCH file");
1177 return QualType();
1178 }
1179
1180 QualType ElementType = GetType(Record[0]);
1181 unsigned NumElements = Record[1];
1182 return Context.getExtVectorType(ElementType, NumElements);
1183 }
1184
1185 case pch::TYPE_FUNCTION_NO_PROTO: {
1186 if (Record.size() != 1) {
1187 Error("Incorrect encoding of no-proto function type");
1188 return QualType();
1189 }
1190 QualType ResultType = GetType(Record[0]);
1191 return Context.getFunctionNoProtoType(ResultType);
1192 }
1193
1194 case pch::TYPE_FUNCTION_PROTO: {
1195 QualType ResultType = GetType(Record[0]);
1196 unsigned Idx = 1;
1197 unsigned NumParams = Record[Idx++];
1198 llvm::SmallVector<QualType, 16> ParamTypes;
1199 for (unsigned I = 0; I != NumParams; ++I)
1200 ParamTypes.push_back(GetType(Record[Idx++]));
1201 bool isVariadic = Record[Idx++];
1202 unsigned Quals = Record[Idx++];
1203 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1204 isVariadic, Quals);
1205 }
1206
1207 case pch::TYPE_TYPEDEF:
1208 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1209 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1210
1211 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregor0b748912009-04-14 21:18:50 +00001212 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001213
1214 case pch::TYPE_TYPEOF: {
1215 if (Record.size() != 1) {
1216 Error("Incorrect encoding of typeof(type) in PCH file");
1217 return QualType();
1218 }
1219 QualType UnderlyingType = GetType(Record[0]);
1220 return Context.getTypeOfType(UnderlyingType);
1221 }
1222
1223 case pch::TYPE_RECORD:
Douglas Gregor8c700062009-04-13 21:20:57 +00001224 assert(Record.size() == 1 && "Incorrect encoding of record type");
1225 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001226
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001227 case pch::TYPE_ENUM:
1228 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1229 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1230
Douglas Gregorb4e715b2009-04-13 20:46:52 +00001231 case pch::TYPE_OBJC_INTERFACE:
1232 // FIXME: Deserialize ObjCInterfaceType
1233 assert(false && "Cannot de-serialize ObjC interface types yet");
1234 return QualType();
1235
1236 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1237 // FIXME: Deserialize ObjCQualifiedInterfaceType
1238 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1239 return QualType();
1240
1241 case pch::TYPE_OBJC_QUALIFIED_ID:
1242 // FIXME: Deserialize ObjCQualifiedIdType
1243 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1244 return QualType();
1245
1246 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1247 // FIXME: Deserialize ObjCQualifiedClassType
1248 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1249 return QualType();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001250 }
1251
1252 // Suppress a GCC warning
1253 return QualType();
1254}
1255
1256/// \brief Note that we have loaded the declaration with the given
1257/// Index.
1258///
1259/// This routine notes that this declaration has already been loaded,
1260/// so that future GetDecl calls will return this declaration rather
1261/// than trying to load a new declaration.
1262inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1263 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1264 DeclAlreadyLoaded[Index] = true;
1265 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1266}
1267
1268/// \brief Read the declaration at the given offset from the PCH file.
1269Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregor0b748912009-04-14 21:18:50 +00001270 // Keep track of where we are in the stream, then jump back there
1271 // after reading this declaration.
1272 SavedStreamPosition SavedPosition(Stream);
1273
Douglas Gregor2cf26342009-04-09 22:27:44 +00001274 Decl *D = 0;
1275 Stream.JumpToBit(Offset);
1276 RecordData Record;
1277 unsigned Code = Stream.ReadCode();
1278 unsigned Idx = 0;
1279 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00001280
Douglas Gregor2cf26342009-04-09 22:27:44 +00001281 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001282 case pch::DECL_ATTR:
1283 case pch::DECL_CONTEXT_LEXICAL:
1284 case pch::DECL_CONTEXT_VISIBLE:
1285 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1286 break;
1287
Douglas Gregor2cf26342009-04-09 22:27:44 +00001288 case pch::DECL_TRANSLATION_UNIT:
1289 assert(Index == 0 && "Translation unit must be at index 0");
1290 Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl());
1291 D = Context.getTranslationUnitDecl();
1292 LoadedDecl(Index, D);
1293 break;
1294
1295 case pch::DECL_TYPEDEF: {
1296 TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(),
1297 0, QualType());
1298 LoadedDecl(Index, Typedef);
1299 Reader.VisitTypedefDecl(Typedef);
1300 D = Typedef;
1301 break;
1302 }
1303
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001304 case pch::DECL_ENUM: {
1305 EnumDecl *Enum = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
1306 LoadedDecl(Index, Enum);
1307 Reader.VisitEnumDecl(Enum);
1308 D = Enum;
1309 break;
1310 }
1311
Douglas Gregor8c700062009-04-13 21:20:57 +00001312 case pch::DECL_RECORD: {
1313 RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct,
1314 0, SourceLocation(), 0, 0);
1315 LoadedDecl(Index, Record);
1316 Reader.VisitRecordDecl(Record);
1317 D = Record;
1318 break;
1319 }
1320
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001321 case pch::DECL_ENUM_CONSTANT: {
1322 EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0,
1323 SourceLocation(), 0,
1324 QualType(), 0,
1325 llvm::APSInt());
1326 LoadedDecl(Index, ECD);
1327 Reader.VisitEnumConstantDecl(ECD);
1328 D = ECD;
1329 break;
1330 }
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001331
1332 case pch::DECL_FUNCTION: {
1333 FunctionDecl *Function = FunctionDecl::Create(Context, 0, SourceLocation(),
1334 DeclarationName(),
1335 QualType());
1336 LoadedDecl(Index, Function);
1337 Reader.VisitFunctionDecl(Function);
1338 D = Function;
1339 break;
1340 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001341
Douglas Gregor8c700062009-04-13 21:20:57 +00001342 case pch::DECL_FIELD: {
1343 FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0,
1344 QualType(), 0, false);
1345 LoadedDecl(Index, Field);
1346 Reader.VisitFieldDecl(Field);
1347 D = Field;
1348 break;
1349 }
1350
Douglas Gregor2cf26342009-04-09 22:27:44 +00001351 case pch::DECL_VAR: {
1352 VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1353 VarDecl::None, SourceLocation());
1354 LoadedDecl(Index, Var);
1355 Reader.VisitVarDecl(Var);
1356 D = Var;
1357 break;
1358 }
1359
Douglas Gregor3a2f7e42009-04-13 22:18:37 +00001360 case pch::DECL_PARM_VAR: {
1361 ParmVarDecl *Parm = ParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1362 QualType(), VarDecl::None, 0);
1363 LoadedDecl(Index, Parm);
1364 Reader.VisitParmVarDecl(Parm);
1365 D = Parm;
1366 break;
1367 }
1368
1369 case pch::DECL_ORIGINAL_PARM_VAR: {
1370 OriginalParmVarDecl *Parm
1371 = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1372 QualType(), QualType(), VarDecl::None,
1373 0);
1374 LoadedDecl(Index, Parm);
1375 Reader.VisitOriginalParmVarDecl(Parm);
1376 D = Parm;
1377 break;
1378 }
1379
Douglas Gregor1028bc62009-04-13 22:49:25 +00001380 case pch::DECL_FILE_SCOPE_ASM: {
1381 FileScopeAsmDecl *Asm = FileScopeAsmDecl::Create(Context, 0,
1382 SourceLocation(), 0);
1383 LoadedDecl(Index, Asm);
1384 Reader.VisitFileScopeAsmDecl(Asm);
1385 D = Asm;
1386 break;
1387 }
1388
1389 case pch::DECL_BLOCK: {
1390 BlockDecl *Block = BlockDecl::Create(Context, 0, SourceLocation());
1391 LoadedDecl(Index, Block);
1392 Reader.VisitBlockDecl(Block);
1393 D = Block;
1394 break;
1395 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001396 }
1397
1398 // If this declaration is also a declaration context, get the
1399 // offsets for its tables of lexical and visible declarations.
1400 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1401 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1402 if (Offsets.first || Offsets.second) {
1403 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1404 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1405 DeclContextOffsets[DC] = Offsets;
1406 }
1407 }
1408 assert(Idx == Record.size());
1409
1410 return D;
1411}
1412
Douglas Gregor8038d512009-04-10 17:25:41 +00001413QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001414 unsigned Quals = ID & 0x07;
1415 unsigned Index = ID >> 3;
1416
1417 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1418 QualType T;
1419 switch ((pch::PredefinedTypeIDs)Index) {
1420 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1421 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1422 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1423
1424 case pch::PREDEF_TYPE_CHAR_U_ID:
1425 case pch::PREDEF_TYPE_CHAR_S_ID:
1426 // FIXME: Check that the signedness of CharTy is correct!
1427 T = Context.CharTy;
1428 break;
1429
1430 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1431 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1432 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1433 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1434 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1435 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1436 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1437 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1438 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1439 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1440 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1441 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1442 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1443 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1444 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1445 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1446 }
1447
1448 assert(!T.isNull() && "Unknown predefined type");
1449 return T.getQualifiedType(Quals);
1450 }
1451
1452 Index -= pch::NUM_PREDEF_TYPE_IDS;
1453 if (!TypeAlreadyLoaded[Index]) {
1454 // Load the type from the PCH file.
1455 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1456 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1457 TypeAlreadyLoaded[Index] = true;
1458 }
1459
1460 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1461}
1462
Douglas Gregor8038d512009-04-10 17:25:41 +00001463Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001464 if (ID == 0)
1465 return 0;
1466
1467 unsigned Index = ID - 1;
1468 if (DeclAlreadyLoaded[Index])
1469 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1470
1471 // Load the declaration from the PCH file.
1472 return ReadDeclRecord(DeclOffsets[Index], Index);
1473}
1474
1475bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregor8038d512009-04-10 17:25:41 +00001476 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001477 assert(DC->hasExternalLexicalStorage() &&
1478 "DeclContext has no lexical decls in storage");
1479 uint64_t Offset = DeclContextOffsets[DC].first;
1480 assert(Offset && "DeclContext has no lexical decls in storage");
1481
Douglas Gregor0b748912009-04-14 21:18:50 +00001482 // Keep track of where we are in the stream, then jump back there
1483 // after reading this context.
1484 SavedStreamPosition SavedPosition(Stream);
1485
Douglas Gregor2cf26342009-04-09 22:27:44 +00001486 // Load the record containing all of the declarations lexically in
1487 // this context.
1488 Stream.JumpToBit(Offset);
1489 RecordData Record;
1490 unsigned Code = Stream.ReadCode();
1491 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001492 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001493 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1494
1495 // Load all of the declaration IDs
1496 Decls.clear();
1497 Decls.insert(Decls.end(), Record.begin(), Record.end());
1498 return false;
1499}
1500
1501bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1502 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1503 assert(DC->hasExternalVisibleStorage() &&
1504 "DeclContext has no visible decls in storage");
1505 uint64_t Offset = DeclContextOffsets[DC].second;
1506 assert(Offset && "DeclContext has no visible decls in storage");
1507
Douglas Gregor0b748912009-04-14 21:18:50 +00001508 // Keep track of where we are in the stream, then jump back there
1509 // after reading this context.
1510 SavedStreamPosition SavedPosition(Stream);
1511
Douglas Gregor2cf26342009-04-09 22:27:44 +00001512 // Load the record containing all of the declarations visible in
1513 // this context.
1514 Stream.JumpToBit(Offset);
1515 RecordData Record;
1516 unsigned Code = Stream.ReadCode();
1517 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor6a2bfb22009-04-15 18:43:11 +00001518 (void)RecCode;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001519 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1520 if (Record.size() == 0)
1521 return false;
1522
1523 Decls.clear();
1524
1525 unsigned Idx = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001526 while (Idx < Record.size()) {
1527 Decls.push_back(VisibleDeclaration());
1528 Decls.back().Name = ReadDeclarationName(Record, Idx);
1529
Douglas Gregor2cf26342009-04-09 22:27:44 +00001530 unsigned Size = Record[Idx++];
1531 llvm::SmallVector<unsigned, 4> & LoadedDecls
1532 = Decls.back().Declarations;
1533 LoadedDecls.reserve(Size);
1534 for (unsigned I = 0; I < Size; ++I)
1535 LoadedDecls.push_back(Record[Idx++]);
1536 }
1537
1538 return false;
1539}
1540
Douglas Gregorfdd01722009-04-14 00:24:19 +00001541void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1542 if (!Consumer)
1543 return;
1544
1545 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1546 Decl *D = GetDecl(ExternalDefinitions[I]);
1547 DeclGroupRef DG(D);
1548 Consumer->HandleTopLevelDecl(DG);
1549 }
1550}
1551
Douglas Gregor2cf26342009-04-09 22:27:44 +00001552void PCHReader::PrintStats() {
1553 std::fprintf(stderr, "*** PCH Statistics:\n");
1554
1555 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1556 TypeAlreadyLoaded.end(),
1557 true);
1558 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1559 DeclAlreadyLoaded.end(),
1560 true);
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001561 unsigned NumIdentifiersLoaded = 0;
1562 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1563 if ((IdentifierData[I] & 0x01) == 0)
1564 ++NumIdentifiersLoaded;
1565 }
1566
Douglas Gregor2cf26342009-04-09 22:27:44 +00001567 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1568 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001569 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001570 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1571 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor2d41cc12009-04-13 20:50:16 +00001572 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1573 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1574 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1575 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001576 std::fprintf(stderr, "\n");
1577}
1578
Chris Lattner7356a312009-04-11 21:15:38 +00001579IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001580 if (ID == 0)
1581 return 0;
Chris Lattner7356a312009-04-11 21:15:38 +00001582
Douglas Gregorafaf3082009-04-11 00:14:32 +00001583 if (!IdentifierTable || IdentifierData.empty()) {
1584 Error("No identifier table in PCH file");
1585 return 0;
1586 }
Chris Lattner7356a312009-04-11 21:15:38 +00001587
Douglas Gregorafaf3082009-04-11 00:14:32 +00001588 if (IdentifierData[ID - 1] & 0x01) {
1589 uint64_t Offset = IdentifierData[ID - 1];
1590 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner7356a312009-04-11 21:15:38 +00001591 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001592 }
Chris Lattner7356a312009-04-11 21:15:38 +00001593
1594 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001595}
1596
1597DeclarationName
1598PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1599 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1600 switch (Kind) {
1601 case DeclarationName::Identifier:
1602 return DeclarationName(GetIdentifierInfo(Record, Idx));
1603
1604 case DeclarationName::ObjCZeroArgSelector:
1605 case DeclarationName::ObjCOneArgSelector:
1606 case DeclarationName::ObjCMultiArgSelector:
1607 assert(false && "Unable to de-serialize Objective-C selectors");
1608 break;
1609
1610 case DeclarationName::CXXConstructorName:
1611 return Context.DeclarationNames.getCXXConstructorName(
1612 GetType(Record[Idx++]));
1613
1614 case DeclarationName::CXXDestructorName:
1615 return Context.DeclarationNames.getCXXDestructorName(
1616 GetType(Record[Idx++]));
1617
1618 case DeclarationName::CXXConversionFunctionName:
1619 return Context.DeclarationNames.getCXXConversionFunctionName(
1620 GetType(Record[Idx++]));
1621
1622 case DeclarationName::CXXOperatorName:
1623 return Context.DeclarationNames.getCXXOperatorName(
1624 (OverloadedOperatorKind)Record[Idx++]);
1625
1626 case DeclarationName::CXXUsingDirective:
1627 return DeclarationName::getUsingDirectiveName();
1628 }
1629
1630 // Required to silence GCC warning
1631 return DeclarationName();
1632}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001633
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00001634/// \brief Read an integral value
1635llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1636 unsigned BitWidth = Record[Idx++];
1637 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1638 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1639 Idx += NumWords;
1640 return Result;
1641}
1642
1643/// \brief Read a signed integral value
1644llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1645 bool isUnsigned = Record[Idx++];
1646 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1647}
1648
Douglas Gregor17fc2232009-04-14 21:55:33 +00001649/// \brief Read a floating-point value
1650llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
1651 // FIXME: is this really correct?
1652 return llvm::APFloat(ReadAPInt(Record, Idx));
1653}
1654
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001655// \brief Read a string
1656std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1657 unsigned Len = Record[Idx++];
1658 std::string Result(&Record[Idx], &Record[Idx] + Len);
1659 Idx += Len;
1660 return Result;
1661}
1662
1663/// \brief Reads attributes from the current stream position.
1664Attr *PCHReader::ReadAttributes() {
1665 unsigned Code = Stream.ReadCode();
1666 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1667 "Expected unabbreviated record"); (void)Code;
1668
1669 RecordData Record;
1670 unsigned Idx = 0;
1671 unsigned RecCode = Stream.ReadRecord(Code, Record);
1672 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1673 (void)RecCode;
1674
1675#define SIMPLE_ATTR(Name) \
1676 case Attr::Name: \
1677 New = ::new (Context) Name##Attr(); \
1678 break
1679
1680#define STRING_ATTR(Name) \
1681 case Attr::Name: \
1682 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1683 break
1684
1685#define UNSIGNED_ATTR(Name) \
1686 case Attr::Name: \
1687 New = ::new (Context) Name##Attr(Record[Idx++]); \
1688 break
1689
1690 Attr *Attrs = 0;
1691 while (Idx < Record.size()) {
1692 Attr *New = 0;
1693 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1694 bool IsInherited = Record[Idx++];
1695
1696 switch (Kind) {
1697 STRING_ATTR(Alias);
1698 UNSIGNED_ATTR(Aligned);
1699 SIMPLE_ATTR(AlwaysInline);
1700 SIMPLE_ATTR(AnalyzerNoReturn);
1701 STRING_ATTR(Annotate);
1702 STRING_ATTR(AsmLabel);
1703
1704 case Attr::Blocks:
1705 New = ::new (Context) BlocksAttr(
1706 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1707 break;
1708
1709 case Attr::Cleanup:
1710 New = ::new (Context) CleanupAttr(
1711 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1712 break;
1713
1714 SIMPLE_ATTR(Const);
1715 UNSIGNED_ATTR(Constructor);
1716 SIMPLE_ATTR(DLLExport);
1717 SIMPLE_ATTR(DLLImport);
1718 SIMPLE_ATTR(Deprecated);
1719 UNSIGNED_ATTR(Destructor);
1720 SIMPLE_ATTR(FastCall);
1721
1722 case Attr::Format: {
1723 std::string Type = ReadString(Record, Idx);
1724 unsigned FormatIdx = Record[Idx++];
1725 unsigned FirstArg = Record[Idx++];
1726 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1727 break;
1728 }
1729
1730 SIMPLE_ATTR(GNUCInline);
1731
1732 case Attr::IBOutletKind:
1733 New = ::new (Context) IBOutletAttr();
1734 break;
1735
1736 SIMPLE_ATTR(NoReturn);
1737 SIMPLE_ATTR(NoThrow);
1738 SIMPLE_ATTR(Nodebug);
1739 SIMPLE_ATTR(Noinline);
1740
1741 case Attr::NonNull: {
1742 unsigned Size = Record[Idx++];
1743 llvm::SmallVector<unsigned, 16> ArgNums;
1744 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1745 Idx += Size;
1746 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1747 break;
1748 }
1749
1750 SIMPLE_ATTR(ObjCException);
1751 SIMPLE_ATTR(ObjCNSObject);
1752 SIMPLE_ATTR(Overloadable);
1753 UNSIGNED_ATTR(Packed);
1754 SIMPLE_ATTR(Pure);
1755 UNSIGNED_ATTR(Regparm);
1756 STRING_ATTR(Section);
1757 SIMPLE_ATTR(StdCall);
1758 SIMPLE_ATTR(TransparentUnion);
1759 SIMPLE_ATTR(Unavailable);
1760 SIMPLE_ATTR(Unused);
1761 SIMPLE_ATTR(Used);
1762
1763 case Attr::Visibility:
1764 New = ::new (Context) VisibilityAttr(
1765 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1766 break;
1767
1768 SIMPLE_ATTR(WarnUnusedResult);
1769 SIMPLE_ATTR(Weak);
1770 SIMPLE_ATTR(WeakImport);
1771 }
1772
1773 assert(New && "Unable to decode attribute?");
1774 New->setInherited(IsInherited);
1775 New->setNext(Attrs);
1776 Attrs = New;
1777 }
1778#undef UNSIGNED_ATTR
1779#undef STRING_ATTR
1780#undef SIMPLE_ATTR
1781
1782 // The list of attributes was built backwards. Reverse the list
1783 // before returning it.
1784 Attr *PrevAttr = 0, *NextAttr = 0;
1785 while (Attrs) {
1786 NextAttr = Attrs->getNext();
1787 Attrs->setNext(PrevAttr);
1788 PrevAttr = Attrs;
1789 Attrs = NextAttr;
1790 }
1791
1792 return PrevAttr;
1793}
1794
Douglas Gregor0b748912009-04-14 21:18:50 +00001795Expr *PCHReader::ReadExpr() {
Douglas Gregor087fd532009-04-14 23:32:43 +00001796 // Within the bitstream, expressions are stored in Reverse Polish
1797 // Notation, with each of the subexpressions preceding the
1798 // expression they are stored in. To evaluate expressions, we
1799 // continue reading expressions and placing them on the stack, with
1800 // expressions having operands removing those operands from the
1801 // stack. Evaluation terminates when we see a EXPR_STOP record, and
1802 // the single remaining expression on the stack is our result.
Douglas Gregor0b748912009-04-14 21:18:50 +00001803 RecordData Record;
Douglas Gregor087fd532009-04-14 23:32:43 +00001804 unsigned Idx;
1805 llvm::SmallVector<Expr *, 16> ExprStack;
1806 PCHStmtReader Reader(*this, Record, Idx, ExprStack);
Douglas Gregor0b748912009-04-14 21:18:50 +00001807 Stmt::EmptyShell Empty;
1808
Douglas Gregor087fd532009-04-14 23:32:43 +00001809 while (true) {
1810 unsigned Code = Stream.ReadCode();
1811 if (Code == llvm::bitc::END_BLOCK) {
1812 if (Stream.ReadBlockEnd()) {
1813 Error("Error at end of Source Manager block");
1814 return 0;
1815 }
1816 break;
1817 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001818
Douglas Gregor087fd532009-04-14 23:32:43 +00001819 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1820 // No known subblocks, always skip them.
1821 Stream.ReadSubBlockID();
1822 if (Stream.SkipBlock()) {
1823 Error("Malformed block record");
1824 return 0;
1825 }
1826 continue;
1827 }
Douglas Gregor17fc2232009-04-14 21:55:33 +00001828
Douglas Gregor087fd532009-04-14 23:32:43 +00001829 if (Code == llvm::bitc::DEFINE_ABBREV) {
1830 Stream.ReadAbbrevRecord();
1831 continue;
1832 }
Douglas Gregor0b748912009-04-14 21:18:50 +00001833
Douglas Gregor087fd532009-04-14 23:32:43 +00001834 Expr *E = 0;
1835 Idx = 0;
1836 Record.clear();
1837 bool Finished = false;
1838 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
1839 case pch::EXPR_STOP:
1840 Finished = true;
1841 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00001842
Douglas Gregor087fd532009-04-14 23:32:43 +00001843 case pch::EXPR_NULL:
1844 E = 0;
1845 break;
Douglas Gregor0b748912009-04-14 21:18:50 +00001846
Douglas Gregor087fd532009-04-14 23:32:43 +00001847 case pch::EXPR_PREDEFINED:
1848 // FIXME: untested (until we can serialize function bodies).
1849 E = new (Context) PredefinedExpr(Empty);
1850 break;
1851
1852 case pch::EXPR_DECL_REF:
1853 E = new (Context) DeclRefExpr(Empty);
1854 break;
1855
1856 case pch::EXPR_INTEGER_LITERAL:
1857 E = new (Context) IntegerLiteral(Empty);
1858 break;
1859
1860 case pch::EXPR_FLOATING_LITERAL:
1861 E = new (Context) FloatingLiteral(Empty);
1862 break;
1863
Douglas Gregor673ecd62009-04-15 16:35:07 +00001864 case pch::EXPR_STRING_LITERAL:
1865 E = StringLiteral::CreateEmpty(Context,
1866 Record[PCHStmtReader::NumExprFields + 1]);
1867 break;
1868
Douglas Gregor087fd532009-04-14 23:32:43 +00001869 case pch::EXPR_CHARACTER_LITERAL:
1870 E = new (Context) CharacterLiteral(Empty);
1871 break;
1872
Douglas Gregorc04db4f2009-04-14 23:59:37 +00001873 case pch::EXPR_PAREN:
1874 E = new (Context) ParenExpr(Empty);
1875 break;
1876
Douglas Gregor0b0b77f2009-04-15 15:58:59 +00001877 case pch::EXPR_UNARY_OPERATOR:
1878 E = new (Context) UnaryOperator(Empty);
1879 break;
1880
1881 case pch::EXPR_SIZEOF_ALIGN_OF:
1882 E = new (Context) SizeOfAlignOfExpr(Empty);
1883 break;
1884
Douglas Gregor1f0d0132009-04-15 17:43:59 +00001885 case pch::EXPR_CALL:
1886 E = new (Context) CallExpr(Context, Empty);
1887 break;
1888
1889 case pch::EXPR_MEMBER:
1890 E = new (Context) MemberExpr(Empty);
1891 break;
1892
Douglas Gregordb600c32009-04-15 00:25:59 +00001893 case pch::EXPR_BINARY_OPERATOR:
1894 E = new (Context) BinaryOperator(Empty);
1895 break;
1896
Douglas Gregor087fd532009-04-14 23:32:43 +00001897 case pch::EXPR_IMPLICIT_CAST:
1898 E = new (Context) ImplicitCastExpr(Empty);
1899 break;
Douglas Gregordb600c32009-04-15 00:25:59 +00001900
1901 case pch::EXPR_CSTYLE_CAST:
1902 E = new (Context) CStyleCastExpr(Empty);
1903 break;
Douglas Gregor087fd532009-04-14 23:32:43 +00001904 }
1905
1906 // We hit an EXPR_STOP, so we're done with this expression.
1907 if (Finished)
1908 break;
1909
1910 if (E) {
1911 unsigned NumSubExprs = Reader.Visit(E);
1912 while (NumSubExprs > 0) {
1913 ExprStack.pop_back();
1914 --NumSubExprs;
1915 }
1916 }
1917
1918 assert(Idx == Record.size() && "Invalid deserialization of expression");
1919 ExprStack.push_back(E);
Douglas Gregor0b748912009-04-14 21:18:50 +00001920 }
Douglas Gregor087fd532009-04-14 23:32:43 +00001921 assert(ExprStack.size() == 1 && "Extra expressions on stack!");
1922 return ExprStack.back();
Douglas Gregor0b748912009-04-14 21:18:50 +00001923}
1924
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001925DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001926 return Diag(SourceLocation(), DiagID);
1927}
1928
1929DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
1930 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001931 Context.getSourceManager()),
1932 DiagID);
1933}