blob: f9290bac73fbbb74d73def0e9213e06ff4c76386 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000022#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000023#include "clang/Lex/Preprocessor.h"
24#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000025#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-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 Gregor47f1b2c2009-04-13 18:14:40 +000055 void VisitTagDecl(TagDecl *TD);
56 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000057 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000058 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000059 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000060 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000061 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000063 void VisitParmVarDecl(ParmVarDecl *PD);
64 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000065 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
66 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-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 Gregor1c507882009-04-15 21:30:51 +000077 if (Record[Idx++])
78 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-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 Gregorc34897d2009-04-09 22:27:44 +000094 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
95}
96
97void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-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 Gregorc34897d2009-04-09 22:27:44 +0000105}
106
Douglas Gregor47f1b2c2009-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 Gregor982365e2009-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 Gregorc34897d2009-04-09 22:27:44 +0000126void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
127 VisitNamedDecl(VD);
128 VD->setType(Reader.GetType(Record[Idx++]));
129}
130
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000131void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
132 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000133 if (Record[Idx++])
134 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000135 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
136}
137
Douglas Gregor23ce3a52009-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 Gregor982365e2009-04-13 21:20:57 +0000159void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
160 VisitValueDecl(FD);
161 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000162 if (Record[Idx++])
163 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000164}
165
Douglas Gregorc34897d2009-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 Gregorc10f86f2009-04-14 21:18:50 +0000175 if (Record[Idx++])
176 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000177}
178
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
180 VisitVarDecl(PD);
181 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000182 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000183}
184
185void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
186 VisitParmVarDecl(PD);
187 PD->setOriginalType(Reader.GetType(Record[Idx++]));
188}
189
Douglas Gregor2a491792009-04-13 22:49:25 +0000190void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
191 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000192 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-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 Gregorc34897d2009-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 Gregorc10f86f2009-04-14 21:18:50 +0000214//===----------------------------------------------------------------------===//
215// Statement/expression deserialization
216//===----------------------------------------------------------------------===//
217namespace {
218 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000219 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000220 PCHReader &Reader;
221 const PCHReader::RecordData &Record;
222 unsigned &Idx;
Douglas Gregora151ba42009-04-14 23:32:43 +0000223 llvm::SmallVectorImpl<Expr *> &ExprStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000224
225 public:
226 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregora151ba42009-04-14 23:32:43 +0000227 unsigned &Idx, llvm::SmallVectorImpl<Expr *> &ExprStack)
228 : Reader(Reader), Record(Record), Idx(Idx), ExprStack(ExprStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000229
Douglas Gregor596e0932009-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 Gregora151ba42009-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 Gregor21ddd8c2009-04-15 22:19:53 +0000244 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000245 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000246 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000247 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000248 unsigned VisitUnaryOperator(UnaryOperator *E);
249 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000250 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000251 unsigned VisitCallExpr(CallExpr *E);
252 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000253 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000254 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000255 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
256 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000257 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000258 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
259 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000260 };
261}
262
Douglas Gregora151ba42009-04-14 23:32:43 +0000263unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000264 E->setType(Reader.GetType(Record[Idx++]));
265 E->setTypeDependent(Record[Idx++]);
266 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000267 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000268 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000269}
270
Douglas Gregora151ba42009-04-14 23:32:43 +0000271unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000272 VisitExpr(E);
273 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
274 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000275 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000276}
277
Douglas Gregora151ba42009-04-14 23:32:43 +0000278unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000279 VisitExpr(E);
280 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
281 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000282 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000283}
284
Douglas Gregora151ba42009-04-14 23:32:43 +0000285unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000286 VisitExpr(E);
287 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
288 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000289 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000290}
291
Douglas Gregora151ba42009-04-14 23:32:43 +0000292unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000293 VisitExpr(E);
294 E->setValue(Reader.ReadAPFloat(Record, Idx));
295 E->setExact(Record[Idx++]);
296 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000297 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000298}
299
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000300unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
301 VisitExpr(E);
302 E->setSubExpr(ExprStack.back());
303 return 1;
304}
305
Douglas Gregor596e0932009-04-15 16:35:07 +0000306unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
307 VisitExpr(E);
308 unsigned Len = Record[Idx++];
309 assert(Record[Idx] == E->getNumConcatenated() &&
310 "Wrong number of concatenated tokens!");
311 ++Idx;
312 E->setWide(Record[Idx++]);
313
314 // Read string data
315 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
316 E->setStrData(Reader.getContext(), &Str[0], Len);
317 Idx += Len;
318
319 // Read source locations
320 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
321 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
322
323 return 0;
324}
325
Douglas Gregora151ba42009-04-14 23:32:43 +0000326unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000327 VisitExpr(E);
328 E->setValue(Record[Idx++]);
329 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
330 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000331 return 0;
332}
333
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000334unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
335 VisitExpr(E);
336 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
338 E->setSubExpr(ExprStack.back());
339 return 1;
340}
341
Douglas Gregor12d74052009-04-15 15:58:59 +0000342unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
343 VisitExpr(E);
344 E->setSubExpr(ExprStack.back());
345 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
346 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
347 return 1;
348}
349
350unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
351 VisitExpr(E);
352 E->setSizeof(Record[Idx++]);
353 if (Record[Idx] == 0) {
354 E->setArgument(ExprStack.back());
355 ++Idx;
356 } else {
357 E->setArgument(Reader.GetType(Record[Idx++]));
358 }
359 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
360 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
361 return E->isArgumentType()? 0 : 1;
362}
363
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000364unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
365 VisitExpr(E);
366 E->setLHS(ExprStack[ExprStack.size() - 2]);
367 E->setRHS(ExprStack[ExprStack.size() - 2]);
368 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369 return 2;
370}
371
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000372unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
373 VisitExpr(E);
374 E->setNumArgs(Reader.getContext(), Record[Idx++]);
375 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
376 E->setCallee(ExprStack[ExprStack.size() - E->getNumArgs() - 1]);
377 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
378 E->setArg(I, ExprStack[ExprStack.size() - N + I]);
379 return E->getNumArgs() + 1;
380}
381
382unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
383 VisitExpr(E);
384 E->setBase(ExprStack.back());
385 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
386 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
387 E->setArrow(Record[Idx++]);
388 return 1;
389}
390
Douglas Gregora151ba42009-04-14 23:32:43 +0000391unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
392 VisitExpr(E);
393 E->setSubExpr(ExprStack.back());
394 return 1;
395}
396
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000397unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
398 VisitExpr(E);
399 E->setLHS(ExprStack.end()[-2]);
400 E->setRHS(ExprStack.end()[-1]);
401 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
402 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
403 return 2;
404}
405
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000406unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
407 VisitBinaryOperator(E);
408 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
409 E->setComputationResultType(Reader.GetType(Record[Idx++]));
410 return 2;
411}
412
413unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
414 VisitExpr(E);
415 E->setCond(ExprStack[ExprStack.size() - 3]);
416 E->setLHS(ExprStack[ExprStack.size() - 2]);
417 E->setRHS(ExprStack[ExprStack.size() - 1]);
418 return 3;
419}
420
Douglas Gregora151ba42009-04-14 23:32:43 +0000421unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
422 VisitCastExpr(E);
423 E->setLvalueCast(Record[Idx++]);
424 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000425}
426
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000427unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
428 VisitCastExpr(E);
429 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
430 return 1;
431}
432
433unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
434 VisitExplicitCastExpr(E);
435 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
436 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
437 return 1;
438}
439
Douglas Gregorc34897d2009-04-09 22:27:44 +0000440// FIXME: use the diagnostics machinery
441static bool Error(const char *Str) {
442 std::fprintf(stderr, "%s\n", Str);
443 return true;
444}
445
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000446/// \brief Check the contents of the predefines buffer against the
447/// contents of the predefines buffer used to build the PCH file.
448///
449/// The contents of the two predefines buffers should be the same. If
450/// not, then some command-line option changed the preprocessor state
451/// and we must reject the PCH file.
452///
453/// \param PCHPredef The start of the predefines buffer in the PCH
454/// file.
455///
456/// \param PCHPredefLen The length of the predefines buffer in the PCH
457/// file.
458///
459/// \param PCHBufferID The FileID for the PCH predefines buffer.
460///
461/// \returns true if there was a mismatch (in which case the PCH file
462/// should be ignored), or false otherwise.
463bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
464 unsigned PCHPredefLen,
465 FileID PCHBufferID) {
466 const char *Predef = PP.getPredefines().c_str();
467 unsigned PredefLen = PP.getPredefines().size();
468
469 // If the two predefines buffers compare equal, we're done!.
470 if (PredefLen == PCHPredefLen &&
471 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
472 return false;
473
474 // The predefines buffers are different. Produce a reasonable
475 // diagnostic showing where they are different.
476
477 // The source locations (potentially in the two different predefines
478 // buffers)
479 SourceLocation Loc1, Loc2;
480 SourceManager &SourceMgr = PP.getSourceManager();
481
482 // Create a source buffer for our predefines string, so
483 // that we can build a diagnostic that points into that
484 // source buffer.
485 FileID BufferID;
486 if (Predef && Predef[0]) {
487 llvm::MemoryBuffer *Buffer
488 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
489 "<built-in>");
490 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
491 }
492
493 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
494 std::pair<const char *, const char *> Locations
495 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
496
497 if (Locations.first != Predef + MinLen) {
498 // We found the location in the two buffers where there is a
499 // difference. Form source locations to point there (in both
500 // buffers).
501 unsigned Offset = Locations.first - Predef;
502 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
503 .getFileLocWithOffset(Offset);
504 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
505 .getFileLocWithOffset(Offset);
506 } else if (PredefLen > PCHPredefLen) {
507 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
508 .getFileLocWithOffset(MinLen);
509 } else {
510 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
511 .getFileLocWithOffset(MinLen);
512 }
513
514 Diag(Loc1, diag::warn_pch_preprocessor);
515 if (Loc2.isValid())
516 Diag(Loc2, diag::note_predef_in_pch);
517 Diag(diag::note_ignoring_pch) << FileName;
518 return true;
519}
520
Douglas Gregor635f97f2009-04-13 16:31:14 +0000521/// \brief Read the line table in the source manager block.
522/// \returns true if ther was an error.
523static bool ParseLineTable(SourceManager &SourceMgr,
524 llvm::SmallVectorImpl<uint64_t> &Record) {
525 unsigned Idx = 0;
526 LineTableInfo &LineTable = SourceMgr.getLineTable();
527
528 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000529 std::map<int, int> FileIDs;
530 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000531 // Extract the file name
532 unsigned FilenameLen = Record[Idx++];
533 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
534 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000535 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
536 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000537 }
538
539 // Parse the line entries
540 std::vector<LineEntry> Entries;
541 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000542 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000543
544 // Extract the line entries
545 unsigned NumEntries = Record[Idx++];
546 Entries.clear();
547 Entries.reserve(NumEntries);
548 for (unsigned I = 0; I != NumEntries; ++I) {
549 unsigned FileOffset = Record[Idx++];
550 unsigned LineNo = Record[Idx++];
551 int FilenameID = Record[Idx++];
552 SrcMgr::CharacteristicKind FileKind
553 = (SrcMgr::CharacteristicKind)Record[Idx++];
554 unsigned IncludeOffset = Record[Idx++];
555 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
556 FileKind, IncludeOffset));
557 }
558 LineTable.AddEntry(FID, Entries);
559 }
560
561 return false;
562}
563
Douglas Gregorab1cef72009-04-10 03:52:48 +0000564/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000565PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000566 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000567 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
568 Error("Malformed source manager block record");
569 return Failure;
570 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000571
572 SourceManager &SourceMgr = Context.getSourceManager();
573 RecordData Record;
574 while (true) {
575 unsigned Code = Stream.ReadCode();
576 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000577 if (Stream.ReadBlockEnd()) {
578 Error("Error at end of Source Manager block");
579 return Failure;
580 }
581
582 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000583 }
584
585 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
586 // No known subblocks, always skip them.
587 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000588 if (Stream.SkipBlock()) {
589 Error("Malformed block record");
590 return Failure;
591 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000592 continue;
593 }
594
595 if (Code == llvm::bitc::DEFINE_ABBREV) {
596 Stream.ReadAbbrevRecord();
597 continue;
598 }
599
600 // Read a record.
601 const char *BlobStart;
602 unsigned BlobLen;
603 Record.clear();
604 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
605 default: // Default behavior: ignore.
606 break;
607
608 case pch::SM_SLOC_FILE_ENTRY: {
609 // FIXME: We would really like to delay the creation of this
610 // FileEntry until it is actually required, e.g., when producing
611 // a diagnostic with a source location in this file.
612 const FileEntry *File
613 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
614 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000615 FileID ID = SourceMgr.createFileID(File,
616 SourceLocation::getFromRawEncoding(Record[1]),
617 (CharacteristicKind)Record[2]);
618 if (Record[3])
619 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
620 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000621 break;
622 }
623
624 case pch::SM_SLOC_BUFFER_ENTRY: {
625 const char *Name = BlobStart;
626 unsigned Code = Stream.ReadCode();
627 Record.clear();
628 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
629 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000630 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000631 llvm::MemoryBuffer *Buffer
632 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
633 BlobStart + BlobLen - 1,
634 Name);
635 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
636
637 if (strcmp(Name, "<built-in>") == 0
638 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
639 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000640 break;
641 }
642
643 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
644 SourceLocation SpellingLoc
645 = SourceLocation::getFromRawEncoding(Record[1]);
646 SourceMgr.createInstantiationLoc(
647 SpellingLoc,
648 SourceLocation::getFromRawEncoding(Record[2]),
649 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000650 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000651 break;
652 }
653
Chris Lattnere1be6022009-04-14 23:22:57 +0000654 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000655 if (ParseLineTable(SourceMgr, Record))
656 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000657 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000658 }
659 }
660}
661
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000662bool PCHReader::ReadPreprocessorBlock() {
663 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
664 return Error("Malformed preprocessor block record");
665
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000666 RecordData Record;
667 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
668 MacroInfo *LastMacro = 0;
669
670 while (true) {
671 unsigned Code = Stream.ReadCode();
672 switch (Code) {
673 case llvm::bitc::END_BLOCK:
674 if (Stream.ReadBlockEnd())
675 return Error("Error at end of preprocessor block");
676 return false;
677
678 case llvm::bitc::ENTER_SUBBLOCK:
679 // No known subblocks, always skip them.
680 Stream.ReadSubBlockID();
681 if (Stream.SkipBlock())
682 return Error("Malformed block record");
683 continue;
684
685 case llvm::bitc::DEFINE_ABBREV:
686 Stream.ReadAbbrevRecord();
687 continue;
688 default: break;
689 }
690
691 // Read a record.
692 Record.clear();
693 pch::PreprocessorRecordTypes RecType =
694 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
695 switch (RecType) {
696 default: // Default behavior: ignore unknown records.
697 break;
Chris Lattner4b21c202009-04-13 01:29:17 +0000698 case pch::PP_COUNTER_VALUE:
699 if (!Record.empty())
700 PP.setCounterValue(Record[0]);
701 break;
702
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000703 case pch::PP_MACRO_OBJECT_LIKE:
704 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +0000705 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
706 if (II == 0)
707 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000708 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
709 bool isUsed = Record[2];
710
711 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
712 MI->setIsUsed(isUsed);
713
714 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
715 // Decode function-like macro info.
716 bool isC99VarArgs = Record[3];
717 bool isGNUVarArgs = Record[4];
718 MacroArgs.clear();
719 unsigned NumArgs = Record[5];
720 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +0000721 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000722
723 // Install function-like macro info.
724 MI->setIsFunctionLike();
725 if (isC99VarArgs) MI->setIsC99Varargs();
726 if (isGNUVarArgs) MI->setIsGNUVarargs();
727 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
728 PP.getPreprocessorAllocator());
729 }
730
731 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000732 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000733
734 // Remember that we saw this macro last so that we add the tokens that
735 // form its body to it.
736 LastMacro = MI;
737 break;
738 }
739
740 case pch::PP_TOKEN: {
741 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
742 // pretend we didn't see this.
743 if (LastMacro == 0) break;
744
745 Token Tok;
746 Tok.startToken();
747 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
748 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +0000749 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
750 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000751 Tok.setKind((tok::TokenKind)Record[3]);
752 Tok.setFlag((Token::TokenFlags)Record[4]);
753 LastMacro->AddTokenToBody(Tok);
754 break;
755 }
756 }
757 }
758}
759
Douglas Gregor179cfb12009-04-10 20:39:37 +0000760PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
761 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
762 Error("Malformed block record");
763 return Failure;
764 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000765
Chris Lattner29241862009-04-11 21:15:38 +0000766 uint64_t PreprocessorBlockBit = 0;
767
Douglas Gregorc34897d2009-04-09 22:27:44 +0000768 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +0000769 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000770 while (!Stream.AtEndOfStream()) {
771 unsigned Code = Stream.ReadCode();
772 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +0000773 // If we saw the preprocessor block, read it now.
774 if (PreprocessorBlockBit) {
775 uint64_t SavedPos = Stream.GetCurrentBitNo();
776 Stream.JumpToBit(PreprocessorBlockBit);
777 if (ReadPreprocessorBlock()) {
778 Error("Malformed preprocessor block");
779 return Failure;
780 }
781 Stream.JumpToBit(SavedPos);
782 }
783
Douglas Gregor179cfb12009-04-10 20:39:37 +0000784 if (Stream.ReadBlockEnd()) {
785 Error("Error at end of module block");
786 return Failure;
787 }
Chris Lattner29241862009-04-11 21:15:38 +0000788
Douglas Gregor179cfb12009-04-10 20:39:37 +0000789 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000790 }
791
792 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
793 switch (Stream.ReadSubBlockID()) {
794 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
795 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
796 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000797 if (Stream.SkipBlock()) {
798 Error("Malformed block record");
799 return Failure;
800 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000801 break;
802
Chris Lattner29241862009-04-11 21:15:38 +0000803 case pch::PREPROCESSOR_BLOCK_ID:
804 // Skip the preprocessor block for now, but remember where it is. We
805 // want to read it in after the identifier table.
806 if (PreprocessorBlockBit) {
807 Error("Multiple preprocessor blocks found.");
808 return Failure;
809 }
810 PreprocessorBlockBit = Stream.GetCurrentBitNo();
811 if (Stream.SkipBlock()) {
812 Error("Malformed block record");
813 return Failure;
814 }
815 break;
816
Douglas Gregorab1cef72009-04-10 03:52:48 +0000817 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000818 switch (ReadSourceManagerBlock()) {
819 case Success:
820 break;
821
822 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000823 Error("Malformed source manager block");
824 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000825
826 case IgnorePCH:
827 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000828 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000829 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000830 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000831 continue;
832 }
833
834 if (Code == llvm::bitc::DEFINE_ABBREV) {
835 Stream.ReadAbbrevRecord();
836 continue;
837 }
838
839 // Read and process a record.
840 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +0000841 const char *BlobStart = 0;
842 unsigned BlobLen = 0;
843 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
844 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000845 default: // Default behavior: ignore.
846 break;
847
848 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000849 if (!TypeOffsets.empty()) {
850 Error("Duplicate TYPE_OFFSET record in PCH file");
851 return Failure;
852 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000853 TypeOffsets.swap(Record);
854 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
855 break;
856
857 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000858 if (!DeclOffsets.empty()) {
859 Error("Duplicate DECL_OFFSET record in PCH file");
860 return Failure;
861 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000862 DeclOffsets.swap(Record);
863 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
864 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000865
866 case pch::LANGUAGE_OPTIONS:
867 if (ParseLanguageOptions(Record))
868 return IgnorePCH;
869 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +0000870
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000871 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +0000872 std::string TargetTriple(BlobStart, BlobLen);
873 if (TargetTriple != Context.Target.getTargetTriple()) {
874 Diag(diag::warn_pch_target_triple)
875 << TargetTriple << Context.Target.getTargetTriple();
876 Diag(diag::note_ignoring_pch) << FileName;
877 return IgnorePCH;
878 }
879 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000880 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000881
882 case pch::IDENTIFIER_TABLE:
883 IdentifierTable = BlobStart;
884 break;
885
886 case pch::IDENTIFIER_OFFSET:
887 if (!IdentifierData.empty()) {
888 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
889 return Failure;
890 }
891 IdentifierData.swap(Record);
892#ifndef NDEBUG
893 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
894 if ((IdentifierData[I] & 0x01) == 0) {
895 Error("Malformed identifier table in the precompiled header");
896 return Failure;
897 }
898 }
899#endif
900 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +0000901
902 case pch::EXTERNAL_DEFINITIONS:
903 if (!ExternalDefinitions.empty()) {
904 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
905 return Failure;
906 }
907 ExternalDefinitions.swap(Record);
908 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000909 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000910 }
911
Douglas Gregor179cfb12009-04-10 20:39:37 +0000912 Error("Premature end of bitstream");
913 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000914}
915
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000916PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000917 // Set the PCH file name.
918 this->FileName = FileName;
919
Douglas Gregorc34897d2009-04-09 22:27:44 +0000920 // Open the PCH file.
921 std::string ErrStr;
922 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000923 if (!Buffer) {
924 Error(ErrStr.c_str());
925 return IgnorePCH;
926 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000927
928 // Initialize the stream
929 Stream.init((const unsigned char *)Buffer->getBufferStart(),
930 (const unsigned char *)Buffer->getBufferEnd());
931
932 // Sniff for the signature.
933 if (Stream.Read(8) != 'C' ||
934 Stream.Read(8) != 'P' ||
935 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000936 Stream.Read(8) != 'H') {
937 Error("Not a PCH file");
938 return IgnorePCH;
939 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000940
941 // We expect a number of well-defined blocks, though we don't necessarily
942 // need to understand them all.
943 while (!Stream.AtEndOfStream()) {
944 unsigned Code = Stream.ReadCode();
945
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000946 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
947 Error("Invalid record at top-level");
948 return Failure;
949 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000950
951 unsigned BlockID = Stream.ReadSubBlockID();
952
953 // We only know the PCH subblock ID.
954 switch (BlockID) {
955 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000956 if (Stream.ReadBlockInfoBlock()) {
957 Error("Malformed BlockInfoBlock");
958 return Failure;
959 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000960 break;
961 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000962 switch (ReadPCHBlock()) {
963 case Success:
964 break;
965
966 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000967 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000968
969 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +0000970 // FIXME: We could consider reading through to the end of this
971 // PCH block, skipping subblocks, to see if there are other
972 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000973 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000974 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000975 break;
976 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000977 if (Stream.SkipBlock()) {
978 Error("Malformed block record");
979 return Failure;
980 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000981 break;
982 }
983 }
984
985 // Load the translation unit declaration
986 ReadDeclRecord(DeclOffsets[0], 0);
987
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000988 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000989}
990
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000991namespace {
992 /// \brief Helper class that saves the current stream position and
993 /// then restores it when destroyed.
994 struct VISIBILITY_HIDDEN SavedStreamPosition {
995 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +0000996 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000997
998 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +0000999 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001000 }
1001
1002 private:
1003 llvm::BitstreamReader &Stream;
1004 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001005 };
1006}
1007
Douglas Gregor179cfb12009-04-10 20:39:37 +00001008/// \brief Parse the record that corresponds to a LangOptions data
1009/// structure.
1010///
1011/// This routine compares the language options used to generate the
1012/// PCH file against the language options set for the current
1013/// compilation. For each option, we classify differences between the
1014/// two compiler states as either "benign" or "important". Benign
1015/// differences don't matter, and we accept them without complaint
1016/// (and without modifying the language options). Differences between
1017/// the states for important options cause the PCH file to be
1018/// unusable, so we emit a warning and return true to indicate that
1019/// there was an error.
1020///
1021/// \returns true if the PCH file is unacceptable, false otherwise.
1022bool PCHReader::ParseLanguageOptions(
1023 const llvm::SmallVectorImpl<uint64_t> &Record) {
1024 const LangOptions &LangOpts = Context.getLangOptions();
1025#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1026#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1027 if (Record[Idx] != LangOpts.Option) { \
1028 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1029 Diag(diag::note_ignoring_pch) << FileName; \
1030 return true; \
1031 } \
1032 ++Idx
1033
1034 unsigned Idx = 0;
1035 PARSE_LANGOPT_BENIGN(Trigraphs);
1036 PARSE_LANGOPT_BENIGN(BCPLComment);
1037 PARSE_LANGOPT_BENIGN(DollarIdents);
1038 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1039 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1040 PARSE_LANGOPT_BENIGN(ImplicitInt);
1041 PARSE_LANGOPT_BENIGN(Digraphs);
1042 PARSE_LANGOPT_BENIGN(HexFloats);
1043 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1044 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1045 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1046 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1047 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1048 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1049 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1050 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1051 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1052 PARSE_LANGOPT_BENIGN(PascalStrings);
1053 PARSE_LANGOPT_BENIGN(Boolean);
1054 PARSE_LANGOPT_BENIGN(WritableStrings);
1055 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1056 diag::warn_pch_lax_vector_conversions);
1057 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1058 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1059 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1060 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1061 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1062 diag::warn_pch_thread_safe_statics);
1063 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1064 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1065 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1066 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1067 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1068 diag::warn_pch_heinous_extensions);
1069 // FIXME: Most of the options below are benign if the macro wasn't
1070 // used. Unfortunately, this means that a PCH compiled without
1071 // optimization can't be used with optimization turned on, even
1072 // though the only thing that changes is whether __OPTIMIZE__ was
1073 // defined... but if __OPTIMIZE__ never showed up in the header, it
1074 // doesn't matter. We could consider making this some special kind
1075 // of check.
1076 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1077 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1078 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1079 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1080 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1081 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1082 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1083 Diag(diag::warn_pch_gc_mode)
1084 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1085 Diag(diag::note_ignoring_pch) << FileName;
1086 return true;
1087 }
1088 ++Idx;
1089 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1090 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1091#undef PARSE_LANGOPT_IRRELEVANT
1092#undef PARSE_LANGOPT_BENIGN
1093
1094 return false;
1095}
1096
Douglas Gregorc34897d2009-04-09 22:27:44 +00001097/// \brief Read and return the type at the given offset.
1098///
1099/// This routine actually reads the record corresponding to the type
1100/// at the given offset in the bitstream. It is a helper routine for
1101/// GetType, which deals with reading type IDs.
1102QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001103 // Keep track of where we are in the stream, then jump back there
1104 // after reading this type.
1105 SavedStreamPosition SavedPosition(Stream);
1106
Douglas Gregorc34897d2009-04-09 22:27:44 +00001107 Stream.JumpToBit(Offset);
1108 RecordData Record;
1109 unsigned Code = Stream.ReadCode();
1110 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001111 case pch::TYPE_ATTR:
1112 assert(false && "Should never jump to an attribute block");
1113 return QualType();
1114
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001115 case pch::TYPE_EXT_QUAL: {
1116 assert(Record.size() == 3 &&
1117 "Incorrect encoding of extended qualifier type");
1118 QualType Base = GetType(Record[0]);
1119 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1120 unsigned AddressSpace = Record[2];
1121
1122 QualType T = Base;
1123 if (GCAttr != QualType::GCNone)
1124 T = Context.getObjCGCQualType(T, GCAttr);
1125 if (AddressSpace)
1126 T = Context.getAddrSpaceQualType(T, AddressSpace);
1127 return T;
1128 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001129
Douglas Gregorc34897d2009-04-09 22:27:44 +00001130 case pch::TYPE_FIXED_WIDTH_INT: {
1131 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1132 return Context.getFixedWidthIntType(Record[0], Record[1]);
1133 }
1134
1135 case pch::TYPE_COMPLEX: {
1136 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1137 QualType ElemType = GetType(Record[0]);
1138 return Context.getComplexType(ElemType);
1139 }
1140
1141 case pch::TYPE_POINTER: {
1142 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1143 QualType PointeeType = GetType(Record[0]);
1144 return Context.getPointerType(PointeeType);
1145 }
1146
1147 case pch::TYPE_BLOCK_POINTER: {
1148 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1149 QualType PointeeType = GetType(Record[0]);
1150 return Context.getBlockPointerType(PointeeType);
1151 }
1152
1153 case pch::TYPE_LVALUE_REFERENCE: {
1154 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1155 QualType PointeeType = GetType(Record[0]);
1156 return Context.getLValueReferenceType(PointeeType);
1157 }
1158
1159 case pch::TYPE_RVALUE_REFERENCE: {
1160 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1161 QualType PointeeType = GetType(Record[0]);
1162 return Context.getRValueReferenceType(PointeeType);
1163 }
1164
1165 case pch::TYPE_MEMBER_POINTER: {
1166 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1167 QualType PointeeType = GetType(Record[0]);
1168 QualType ClassType = GetType(Record[1]);
1169 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1170 }
1171
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001172 case pch::TYPE_CONSTANT_ARRAY: {
1173 QualType ElementType = GetType(Record[0]);
1174 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1175 unsigned IndexTypeQuals = Record[2];
1176 unsigned Idx = 3;
1177 llvm::APInt Size = ReadAPInt(Record, Idx);
1178 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1179 }
1180
1181 case pch::TYPE_INCOMPLETE_ARRAY: {
1182 QualType ElementType = GetType(Record[0]);
1183 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1184 unsigned IndexTypeQuals = Record[2];
1185 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1186 }
1187
1188 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001189 QualType ElementType = GetType(Record[0]);
1190 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1191 unsigned IndexTypeQuals = Record[2];
1192 return Context.getVariableArrayType(ElementType, ReadExpr(),
1193 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001194 }
1195
1196 case pch::TYPE_VECTOR: {
1197 if (Record.size() != 2) {
1198 Error("Incorrect encoding of vector type in PCH file");
1199 return QualType();
1200 }
1201
1202 QualType ElementType = GetType(Record[0]);
1203 unsigned NumElements = Record[1];
1204 return Context.getVectorType(ElementType, NumElements);
1205 }
1206
1207 case pch::TYPE_EXT_VECTOR: {
1208 if (Record.size() != 2) {
1209 Error("Incorrect encoding of extended vector type in PCH file");
1210 return QualType();
1211 }
1212
1213 QualType ElementType = GetType(Record[0]);
1214 unsigned NumElements = Record[1];
1215 return Context.getExtVectorType(ElementType, NumElements);
1216 }
1217
1218 case pch::TYPE_FUNCTION_NO_PROTO: {
1219 if (Record.size() != 1) {
1220 Error("Incorrect encoding of no-proto function type");
1221 return QualType();
1222 }
1223 QualType ResultType = GetType(Record[0]);
1224 return Context.getFunctionNoProtoType(ResultType);
1225 }
1226
1227 case pch::TYPE_FUNCTION_PROTO: {
1228 QualType ResultType = GetType(Record[0]);
1229 unsigned Idx = 1;
1230 unsigned NumParams = Record[Idx++];
1231 llvm::SmallVector<QualType, 16> ParamTypes;
1232 for (unsigned I = 0; I != NumParams; ++I)
1233 ParamTypes.push_back(GetType(Record[Idx++]));
1234 bool isVariadic = Record[Idx++];
1235 unsigned Quals = Record[Idx++];
1236 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1237 isVariadic, Quals);
1238 }
1239
1240 case pch::TYPE_TYPEDEF:
1241 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1242 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1243
1244 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001245 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001246
1247 case pch::TYPE_TYPEOF: {
1248 if (Record.size() != 1) {
1249 Error("Incorrect encoding of typeof(type) in PCH file");
1250 return QualType();
1251 }
1252 QualType UnderlyingType = GetType(Record[0]);
1253 return Context.getTypeOfType(UnderlyingType);
1254 }
1255
1256 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001257 assert(Record.size() == 1 && "Incorrect encoding of record type");
1258 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001259
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001260 case pch::TYPE_ENUM:
1261 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1262 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1263
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001264 case pch::TYPE_OBJC_INTERFACE:
1265 // FIXME: Deserialize ObjCInterfaceType
1266 assert(false && "Cannot de-serialize ObjC interface types yet");
1267 return QualType();
1268
1269 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1270 // FIXME: Deserialize ObjCQualifiedInterfaceType
1271 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1272 return QualType();
1273
1274 case pch::TYPE_OBJC_QUALIFIED_ID:
1275 // FIXME: Deserialize ObjCQualifiedIdType
1276 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1277 return QualType();
1278
1279 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1280 // FIXME: Deserialize ObjCQualifiedClassType
1281 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1282 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001283 }
1284
1285 // Suppress a GCC warning
1286 return QualType();
1287}
1288
1289/// \brief Note that we have loaded the declaration with the given
1290/// Index.
1291///
1292/// This routine notes that this declaration has already been loaded,
1293/// so that future GetDecl calls will return this declaration rather
1294/// than trying to load a new declaration.
1295inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1296 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1297 DeclAlreadyLoaded[Index] = true;
1298 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1299}
1300
1301/// \brief Read the declaration at the given offset from the PCH file.
1302Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001303 // Keep track of where we are in the stream, then jump back there
1304 // after reading this declaration.
1305 SavedStreamPosition SavedPosition(Stream);
1306
Douglas Gregorc34897d2009-04-09 22:27:44 +00001307 Decl *D = 0;
1308 Stream.JumpToBit(Offset);
1309 RecordData Record;
1310 unsigned Code = Stream.ReadCode();
1311 unsigned Idx = 0;
1312 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001313
Douglas Gregorc34897d2009-04-09 22:27:44 +00001314 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001315 case pch::DECL_ATTR:
1316 case pch::DECL_CONTEXT_LEXICAL:
1317 case pch::DECL_CONTEXT_VISIBLE:
1318 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1319 break;
1320
Douglas Gregorc34897d2009-04-09 22:27:44 +00001321 case pch::DECL_TRANSLATION_UNIT:
1322 assert(Index == 0 && "Translation unit must be at index 0");
1323 Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl());
1324 D = Context.getTranslationUnitDecl();
1325 LoadedDecl(Index, D);
1326 break;
1327
1328 case pch::DECL_TYPEDEF: {
1329 TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(),
1330 0, QualType());
1331 LoadedDecl(Index, Typedef);
1332 Reader.VisitTypedefDecl(Typedef);
1333 D = Typedef;
1334 break;
1335 }
1336
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001337 case pch::DECL_ENUM: {
1338 EnumDecl *Enum = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
1339 LoadedDecl(Index, Enum);
1340 Reader.VisitEnumDecl(Enum);
1341 D = Enum;
1342 break;
1343 }
1344
Douglas Gregor982365e2009-04-13 21:20:57 +00001345 case pch::DECL_RECORD: {
1346 RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct,
1347 0, SourceLocation(), 0, 0);
1348 LoadedDecl(Index, Record);
1349 Reader.VisitRecordDecl(Record);
1350 D = Record;
1351 break;
1352 }
1353
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001354 case pch::DECL_ENUM_CONSTANT: {
1355 EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0,
1356 SourceLocation(), 0,
1357 QualType(), 0,
1358 llvm::APSInt());
1359 LoadedDecl(Index, ECD);
1360 Reader.VisitEnumConstantDecl(ECD);
1361 D = ECD;
1362 break;
1363 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001364
1365 case pch::DECL_FUNCTION: {
1366 FunctionDecl *Function = FunctionDecl::Create(Context, 0, SourceLocation(),
1367 DeclarationName(),
1368 QualType());
1369 LoadedDecl(Index, Function);
1370 Reader.VisitFunctionDecl(Function);
1371 D = Function;
1372 break;
1373 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001374
Douglas Gregor982365e2009-04-13 21:20:57 +00001375 case pch::DECL_FIELD: {
1376 FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0,
1377 QualType(), 0, false);
1378 LoadedDecl(Index, Field);
1379 Reader.VisitFieldDecl(Field);
1380 D = Field;
1381 break;
1382 }
1383
Douglas Gregorc34897d2009-04-09 22:27:44 +00001384 case pch::DECL_VAR: {
1385 VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1386 VarDecl::None, SourceLocation());
1387 LoadedDecl(Index, Var);
1388 Reader.VisitVarDecl(Var);
1389 D = Var;
1390 break;
1391 }
1392
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001393 case pch::DECL_PARM_VAR: {
1394 ParmVarDecl *Parm = ParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1395 QualType(), VarDecl::None, 0);
1396 LoadedDecl(Index, Parm);
1397 Reader.VisitParmVarDecl(Parm);
1398 D = Parm;
1399 break;
1400 }
1401
1402 case pch::DECL_ORIGINAL_PARM_VAR: {
1403 OriginalParmVarDecl *Parm
1404 = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1405 QualType(), QualType(), VarDecl::None,
1406 0);
1407 LoadedDecl(Index, Parm);
1408 Reader.VisitOriginalParmVarDecl(Parm);
1409 D = Parm;
1410 break;
1411 }
1412
Douglas Gregor2a491792009-04-13 22:49:25 +00001413 case pch::DECL_FILE_SCOPE_ASM: {
1414 FileScopeAsmDecl *Asm = FileScopeAsmDecl::Create(Context, 0,
1415 SourceLocation(), 0);
1416 LoadedDecl(Index, Asm);
1417 Reader.VisitFileScopeAsmDecl(Asm);
1418 D = Asm;
1419 break;
1420 }
1421
1422 case pch::DECL_BLOCK: {
1423 BlockDecl *Block = BlockDecl::Create(Context, 0, SourceLocation());
1424 LoadedDecl(Index, Block);
1425 Reader.VisitBlockDecl(Block);
1426 D = Block;
1427 break;
1428 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001429 }
1430
1431 // If this declaration is also a declaration context, get the
1432 // offsets for its tables of lexical and visible declarations.
1433 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1434 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1435 if (Offsets.first || Offsets.second) {
1436 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1437 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1438 DeclContextOffsets[DC] = Offsets;
1439 }
1440 }
1441 assert(Idx == Record.size());
1442
1443 return D;
1444}
1445
Douglas Gregorac8f2802009-04-10 17:25:41 +00001446QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001447 unsigned Quals = ID & 0x07;
1448 unsigned Index = ID >> 3;
1449
1450 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1451 QualType T;
1452 switch ((pch::PredefinedTypeIDs)Index) {
1453 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1454 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1455 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1456
1457 case pch::PREDEF_TYPE_CHAR_U_ID:
1458 case pch::PREDEF_TYPE_CHAR_S_ID:
1459 // FIXME: Check that the signedness of CharTy is correct!
1460 T = Context.CharTy;
1461 break;
1462
1463 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1464 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1465 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1466 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1467 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1468 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1469 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1470 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1471 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1472 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1473 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1474 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1475 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1476 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1477 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1478 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1479 }
1480
1481 assert(!T.isNull() && "Unknown predefined type");
1482 return T.getQualifiedType(Quals);
1483 }
1484
1485 Index -= pch::NUM_PREDEF_TYPE_IDS;
1486 if (!TypeAlreadyLoaded[Index]) {
1487 // Load the type from the PCH file.
1488 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1489 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1490 TypeAlreadyLoaded[Index] = true;
1491 }
1492
1493 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1494}
1495
Douglas Gregorac8f2802009-04-10 17:25:41 +00001496Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001497 if (ID == 0)
1498 return 0;
1499
1500 unsigned Index = ID - 1;
1501 if (DeclAlreadyLoaded[Index])
1502 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1503
1504 // Load the declaration from the PCH file.
1505 return ReadDeclRecord(DeclOffsets[Index], Index);
1506}
1507
1508bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001509 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001510 assert(DC->hasExternalLexicalStorage() &&
1511 "DeclContext has no lexical decls in storage");
1512 uint64_t Offset = DeclContextOffsets[DC].first;
1513 assert(Offset && "DeclContext has no lexical decls in storage");
1514
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001515 // Keep track of where we are in the stream, then jump back there
1516 // after reading this context.
1517 SavedStreamPosition SavedPosition(Stream);
1518
Douglas Gregorc34897d2009-04-09 22:27:44 +00001519 // Load the record containing all of the declarations lexically in
1520 // this context.
1521 Stream.JumpToBit(Offset);
1522 RecordData Record;
1523 unsigned Code = Stream.ReadCode();
1524 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001525 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001526 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1527
1528 // Load all of the declaration IDs
1529 Decls.clear();
1530 Decls.insert(Decls.end(), Record.begin(), Record.end());
1531 return false;
1532}
1533
1534bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1535 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1536 assert(DC->hasExternalVisibleStorage() &&
1537 "DeclContext has no visible decls in storage");
1538 uint64_t Offset = DeclContextOffsets[DC].second;
1539 assert(Offset && "DeclContext has no visible decls in storage");
1540
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001541 // Keep track of where we are in the stream, then jump back there
1542 // after reading this context.
1543 SavedStreamPosition SavedPosition(Stream);
1544
Douglas Gregorc34897d2009-04-09 22:27:44 +00001545 // Load the record containing all of the declarations visible in
1546 // this context.
1547 Stream.JumpToBit(Offset);
1548 RecordData Record;
1549 unsigned Code = Stream.ReadCode();
1550 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001551 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001552 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1553 if (Record.size() == 0)
1554 return false;
1555
1556 Decls.clear();
1557
1558 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001559 while (Idx < Record.size()) {
1560 Decls.push_back(VisibleDeclaration());
1561 Decls.back().Name = ReadDeclarationName(Record, Idx);
1562
Douglas Gregorc34897d2009-04-09 22:27:44 +00001563 unsigned Size = Record[Idx++];
1564 llvm::SmallVector<unsigned, 4> & LoadedDecls
1565 = Decls.back().Declarations;
1566 LoadedDecls.reserve(Size);
1567 for (unsigned I = 0; I < Size; ++I)
1568 LoadedDecls.push_back(Record[Idx++]);
1569 }
1570
1571 return false;
1572}
1573
Douglas Gregor631f6c62009-04-14 00:24:19 +00001574void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1575 if (!Consumer)
1576 return;
1577
1578 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1579 Decl *D = GetDecl(ExternalDefinitions[I]);
1580 DeclGroupRef DG(D);
1581 Consumer->HandleTopLevelDecl(DG);
1582 }
1583}
1584
Douglas Gregorc34897d2009-04-09 22:27:44 +00001585void PCHReader::PrintStats() {
1586 std::fprintf(stderr, "*** PCH Statistics:\n");
1587
1588 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1589 TypeAlreadyLoaded.end(),
1590 true);
1591 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1592 DeclAlreadyLoaded.end(),
1593 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001594 unsigned NumIdentifiersLoaded = 0;
1595 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1596 if ((IdentifierData[I] & 0x01) == 0)
1597 ++NumIdentifiersLoaded;
1598 }
1599
Douglas Gregorc34897d2009-04-09 22:27:44 +00001600 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1601 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001602 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001603 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1604 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001605 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1606 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1607 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1608 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001609 std::fprintf(stderr, "\n");
1610}
1611
Chris Lattner29241862009-04-11 21:15:38 +00001612IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001613 if (ID == 0)
1614 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001615
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001616 if (!IdentifierTable || IdentifierData.empty()) {
1617 Error("No identifier table in PCH file");
1618 return 0;
1619 }
Chris Lattner29241862009-04-11 21:15:38 +00001620
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001621 if (IdentifierData[ID - 1] & 0x01) {
1622 uint64_t Offset = IdentifierData[ID - 1];
1623 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001624 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001625 }
Chris Lattner29241862009-04-11 21:15:38 +00001626
1627 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001628}
1629
1630DeclarationName
1631PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1632 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1633 switch (Kind) {
1634 case DeclarationName::Identifier:
1635 return DeclarationName(GetIdentifierInfo(Record, Idx));
1636
1637 case DeclarationName::ObjCZeroArgSelector:
1638 case DeclarationName::ObjCOneArgSelector:
1639 case DeclarationName::ObjCMultiArgSelector:
1640 assert(false && "Unable to de-serialize Objective-C selectors");
1641 break;
1642
1643 case DeclarationName::CXXConstructorName:
1644 return Context.DeclarationNames.getCXXConstructorName(
1645 GetType(Record[Idx++]));
1646
1647 case DeclarationName::CXXDestructorName:
1648 return Context.DeclarationNames.getCXXDestructorName(
1649 GetType(Record[Idx++]));
1650
1651 case DeclarationName::CXXConversionFunctionName:
1652 return Context.DeclarationNames.getCXXConversionFunctionName(
1653 GetType(Record[Idx++]));
1654
1655 case DeclarationName::CXXOperatorName:
1656 return Context.DeclarationNames.getCXXOperatorName(
1657 (OverloadedOperatorKind)Record[Idx++]);
1658
1659 case DeclarationName::CXXUsingDirective:
1660 return DeclarationName::getUsingDirectiveName();
1661 }
1662
1663 // Required to silence GCC warning
1664 return DeclarationName();
1665}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001666
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001667/// \brief Read an integral value
1668llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1669 unsigned BitWidth = Record[Idx++];
1670 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1671 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1672 Idx += NumWords;
1673 return Result;
1674}
1675
1676/// \brief Read a signed integral value
1677llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1678 bool isUnsigned = Record[Idx++];
1679 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1680}
1681
Douglas Gregore2f37202009-04-14 21:55:33 +00001682/// \brief Read a floating-point value
1683llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001684 return llvm::APFloat(ReadAPInt(Record, Idx));
1685}
1686
Douglas Gregor1c507882009-04-15 21:30:51 +00001687// \brief Read a string
1688std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1689 unsigned Len = Record[Idx++];
1690 std::string Result(&Record[Idx], &Record[Idx] + Len);
1691 Idx += Len;
1692 return Result;
1693}
1694
1695/// \brief Reads attributes from the current stream position.
1696Attr *PCHReader::ReadAttributes() {
1697 unsigned Code = Stream.ReadCode();
1698 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1699 "Expected unabbreviated record"); (void)Code;
1700
1701 RecordData Record;
1702 unsigned Idx = 0;
1703 unsigned RecCode = Stream.ReadRecord(Code, Record);
1704 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1705 (void)RecCode;
1706
1707#define SIMPLE_ATTR(Name) \
1708 case Attr::Name: \
1709 New = ::new (Context) Name##Attr(); \
1710 break
1711
1712#define STRING_ATTR(Name) \
1713 case Attr::Name: \
1714 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1715 break
1716
1717#define UNSIGNED_ATTR(Name) \
1718 case Attr::Name: \
1719 New = ::new (Context) Name##Attr(Record[Idx++]); \
1720 break
1721
1722 Attr *Attrs = 0;
1723 while (Idx < Record.size()) {
1724 Attr *New = 0;
1725 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1726 bool IsInherited = Record[Idx++];
1727
1728 switch (Kind) {
1729 STRING_ATTR(Alias);
1730 UNSIGNED_ATTR(Aligned);
1731 SIMPLE_ATTR(AlwaysInline);
1732 SIMPLE_ATTR(AnalyzerNoReturn);
1733 STRING_ATTR(Annotate);
1734 STRING_ATTR(AsmLabel);
1735
1736 case Attr::Blocks:
1737 New = ::new (Context) BlocksAttr(
1738 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1739 break;
1740
1741 case Attr::Cleanup:
1742 New = ::new (Context) CleanupAttr(
1743 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1744 break;
1745
1746 SIMPLE_ATTR(Const);
1747 UNSIGNED_ATTR(Constructor);
1748 SIMPLE_ATTR(DLLExport);
1749 SIMPLE_ATTR(DLLImport);
1750 SIMPLE_ATTR(Deprecated);
1751 UNSIGNED_ATTR(Destructor);
1752 SIMPLE_ATTR(FastCall);
1753
1754 case Attr::Format: {
1755 std::string Type = ReadString(Record, Idx);
1756 unsigned FormatIdx = Record[Idx++];
1757 unsigned FirstArg = Record[Idx++];
1758 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1759 break;
1760 }
1761
1762 SIMPLE_ATTR(GNUCInline);
1763
1764 case Attr::IBOutletKind:
1765 New = ::new (Context) IBOutletAttr();
1766 break;
1767
1768 SIMPLE_ATTR(NoReturn);
1769 SIMPLE_ATTR(NoThrow);
1770 SIMPLE_ATTR(Nodebug);
1771 SIMPLE_ATTR(Noinline);
1772
1773 case Attr::NonNull: {
1774 unsigned Size = Record[Idx++];
1775 llvm::SmallVector<unsigned, 16> ArgNums;
1776 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1777 Idx += Size;
1778 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1779 break;
1780 }
1781
1782 SIMPLE_ATTR(ObjCException);
1783 SIMPLE_ATTR(ObjCNSObject);
1784 SIMPLE_ATTR(Overloadable);
1785 UNSIGNED_ATTR(Packed);
1786 SIMPLE_ATTR(Pure);
1787 UNSIGNED_ATTR(Regparm);
1788 STRING_ATTR(Section);
1789 SIMPLE_ATTR(StdCall);
1790 SIMPLE_ATTR(TransparentUnion);
1791 SIMPLE_ATTR(Unavailable);
1792 SIMPLE_ATTR(Unused);
1793 SIMPLE_ATTR(Used);
1794
1795 case Attr::Visibility:
1796 New = ::new (Context) VisibilityAttr(
1797 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1798 break;
1799
1800 SIMPLE_ATTR(WarnUnusedResult);
1801 SIMPLE_ATTR(Weak);
1802 SIMPLE_ATTR(WeakImport);
1803 }
1804
1805 assert(New && "Unable to decode attribute?");
1806 New->setInherited(IsInherited);
1807 New->setNext(Attrs);
1808 Attrs = New;
1809 }
1810#undef UNSIGNED_ATTR
1811#undef STRING_ATTR
1812#undef SIMPLE_ATTR
1813
1814 // The list of attributes was built backwards. Reverse the list
1815 // before returning it.
1816 Attr *PrevAttr = 0, *NextAttr = 0;
1817 while (Attrs) {
1818 NextAttr = Attrs->getNext();
1819 Attrs->setNext(PrevAttr);
1820 PrevAttr = Attrs;
1821 Attrs = NextAttr;
1822 }
1823
1824 return PrevAttr;
1825}
1826
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001827Expr *PCHReader::ReadExpr() {
Douglas Gregora151ba42009-04-14 23:32:43 +00001828 // Within the bitstream, expressions are stored in Reverse Polish
1829 // Notation, with each of the subexpressions preceding the
1830 // expression they are stored in. To evaluate expressions, we
1831 // continue reading expressions and placing them on the stack, with
1832 // expressions having operands removing those operands from the
1833 // stack. Evaluation terminates when we see a EXPR_STOP record, and
1834 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001835 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00001836 unsigned Idx;
1837 llvm::SmallVector<Expr *, 16> ExprStack;
1838 PCHStmtReader Reader(*this, Record, Idx, ExprStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001839 Stmt::EmptyShell Empty;
1840
Douglas Gregora151ba42009-04-14 23:32:43 +00001841 while (true) {
1842 unsigned Code = Stream.ReadCode();
1843 if (Code == llvm::bitc::END_BLOCK) {
1844 if (Stream.ReadBlockEnd()) {
1845 Error("Error at end of Source Manager block");
1846 return 0;
1847 }
1848 break;
1849 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001850
Douglas Gregora151ba42009-04-14 23:32:43 +00001851 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1852 // No known subblocks, always skip them.
1853 Stream.ReadSubBlockID();
1854 if (Stream.SkipBlock()) {
1855 Error("Malformed block record");
1856 return 0;
1857 }
1858 continue;
1859 }
Douglas Gregore2f37202009-04-14 21:55:33 +00001860
Douglas Gregora151ba42009-04-14 23:32:43 +00001861 if (Code == llvm::bitc::DEFINE_ABBREV) {
1862 Stream.ReadAbbrevRecord();
1863 continue;
1864 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001865
Douglas Gregora151ba42009-04-14 23:32:43 +00001866 Expr *E = 0;
1867 Idx = 0;
1868 Record.clear();
1869 bool Finished = false;
1870 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
1871 case pch::EXPR_STOP:
1872 Finished = true;
1873 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001874
Douglas Gregora151ba42009-04-14 23:32:43 +00001875 case pch::EXPR_NULL:
1876 E = 0;
1877 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001878
Douglas Gregora151ba42009-04-14 23:32:43 +00001879 case pch::EXPR_PREDEFINED:
1880 // FIXME: untested (until we can serialize function bodies).
1881 E = new (Context) PredefinedExpr(Empty);
1882 break;
1883
1884 case pch::EXPR_DECL_REF:
1885 E = new (Context) DeclRefExpr(Empty);
1886 break;
1887
1888 case pch::EXPR_INTEGER_LITERAL:
1889 E = new (Context) IntegerLiteral(Empty);
1890 break;
1891
1892 case pch::EXPR_FLOATING_LITERAL:
1893 E = new (Context) FloatingLiteral(Empty);
1894 break;
1895
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00001896 case pch::EXPR_IMAGINARY_LITERAL:
1897 E = new (Context) ImaginaryLiteral(Empty);
1898 break;
1899
Douglas Gregor596e0932009-04-15 16:35:07 +00001900 case pch::EXPR_STRING_LITERAL:
1901 E = StringLiteral::CreateEmpty(Context,
1902 Record[PCHStmtReader::NumExprFields + 1]);
1903 break;
1904
Douglas Gregora151ba42009-04-14 23:32:43 +00001905 case pch::EXPR_CHARACTER_LITERAL:
1906 E = new (Context) CharacterLiteral(Empty);
1907 break;
1908
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00001909 case pch::EXPR_PAREN:
1910 E = new (Context) ParenExpr(Empty);
1911 break;
1912
Douglas Gregor12d74052009-04-15 15:58:59 +00001913 case pch::EXPR_UNARY_OPERATOR:
1914 E = new (Context) UnaryOperator(Empty);
1915 break;
1916
1917 case pch::EXPR_SIZEOF_ALIGN_OF:
1918 E = new (Context) SizeOfAlignOfExpr(Empty);
1919 break;
1920
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00001921 case pch::EXPR_ARRAY_SUBSCRIPT:
1922 E = new (Context) ArraySubscriptExpr(Empty);
1923 break;
1924
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00001925 case pch::EXPR_CALL:
1926 E = new (Context) CallExpr(Context, Empty);
1927 break;
1928
1929 case pch::EXPR_MEMBER:
1930 E = new (Context) MemberExpr(Empty);
1931 break;
1932
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001933 case pch::EXPR_BINARY_OPERATOR:
1934 E = new (Context) BinaryOperator(Empty);
1935 break;
1936
Douglas Gregorc599bbf2009-04-15 22:40:36 +00001937 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
1938 E = new (Context) CompoundAssignOperator(Empty);
1939 break;
1940
1941 case pch::EXPR_CONDITIONAL_OPERATOR:
1942 E = new (Context) ConditionalOperator(Empty);
1943 break;
1944
Douglas Gregora151ba42009-04-14 23:32:43 +00001945 case pch::EXPR_IMPLICIT_CAST:
1946 E = new (Context) ImplicitCastExpr(Empty);
1947 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001948
1949 case pch::EXPR_CSTYLE_CAST:
1950 E = new (Context) CStyleCastExpr(Empty);
1951 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00001952 }
1953
1954 // We hit an EXPR_STOP, so we're done with this expression.
1955 if (Finished)
1956 break;
1957
1958 if (E) {
1959 unsigned NumSubExprs = Reader.Visit(E);
1960 while (NumSubExprs > 0) {
1961 ExprStack.pop_back();
1962 --NumSubExprs;
1963 }
1964 }
1965
1966 assert(Idx == Record.size() && "Invalid deserialization of expression");
1967 ExprStack.push_back(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001968 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001969 assert(ExprStack.size() == 1 && "Extra expressions on stack!");
1970 return ExprStack.back();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001971}
1972
Douglas Gregor179cfb12009-04-10 20:39:37 +00001973DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001974 return Diag(SourceLocation(), DiagID);
1975}
1976
1977DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
1978 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00001979 Context.getSourceManager()),
1980 DiagID);
1981}