blob: ceba88049ad1c9e05f8cc02effd5c6d24b53c870 [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 Gregorc72f6c82009-04-16 22:23:12 +0000223 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000224
225 public:
226 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000227 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
228 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
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 Gregorb70b48f2009-04-16 02:33:48 +0000260 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000261 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000262 unsigned VisitInitListExpr(InitListExpr *E);
263 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
264 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000265 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000266 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
267 unsigned VisitChooseExpr(ChooseExpr *E);
268 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000269 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
270 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000271 };
272}
273
Douglas Gregora151ba42009-04-14 23:32:43 +0000274unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000275 E->setType(Reader.GetType(Record[Idx++]));
276 E->setTypeDependent(Record[Idx++]);
277 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000278 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000279 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000280}
281
Douglas Gregora151ba42009-04-14 23:32:43 +0000282unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000283 VisitExpr(E);
284 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
285 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000286 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000287}
288
Douglas Gregora151ba42009-04-14 23:32:43 +0000289unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000290 VisitExpr(E);
291 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
292 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000293 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000294}
295
Douglas Gregora151ba42009-04-14 23:32:43 +0000296unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000297 VisitExpr(E);
298 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
299 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000300 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000301}
302
Douglas Gregora151ba42009-04-14 23:32:43 +0000303unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000304 VisitExpr(E);
305 E->setValue(Reader.ReadAPFloat(Record, Idx));
306 E->setExact(Record[Idx++]);
307 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000308 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000309}
310
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000311unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
312 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000313 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000314 return 1;
315}
316
Douglas Gregor596e0932009-04-15 16:35:07 +0000317unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
318 VisitExpr(E);
319 unsigned Len = Record[Idx++];
320 assert(Record[Idx] == E->getNumConcatenated() &&
321 "Wrong number of concatenated tokens!");
322 ++Idx;
323 E->setWide(Record[Idx++]);
324
325 // Read string data
326 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
327 E->setStrData(Reader.getContext(), &Str[0], Len);
328 Idx += Len;
329
330 // Read source locations
331 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
332 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
333
334 return 0;
335}
336
Douglas Gregora151ba42009-04-14 23:32:43 +0000337unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000338 VisitExpr(E);
339 E->setValue(Record[Idx++]);
340 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
341 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000342 return 0;
343}
344
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000345unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
346 VisitExpr(E);
347 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
348 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000349 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000350 return 1;
351}
352
Douglas Gregor12d74052009-04-15 15:58:59 +0000353unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
354 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000355 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000356 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
357 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
358 return 1;
359}
360
361unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
362 VisitExpr(E);
363 E->setSizeof(Record[Idx++]);
364 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000365 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000366 ++Idx;
367 } else {
368 E->setArgument(Reader.GetType(Record[Idx++]));
369 }
370 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
371 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
372 return E->isArgumentType()? 0 : 1;
373}
374
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000375unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
376 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000377 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
378 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000379 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
380 return 2;
381}
382
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000383unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
384 VisitExpr(E);
385 E->setNumArgs(Reader.getContext(), Record[Idx++]);
386 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000387 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000388 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000389 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000390 return E->getNumArgs() + 1;
391}
392
393unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
394 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000395 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000396 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
397 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
398 E->setArrow(Record[Idx++]);
399 return 1;
400}
401
Douglas Gregora151ba42009-04-14 23:32:43 +0000402unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
403 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000404 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000405 return 1;
406}
407
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000408unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
409 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000410 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
411 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000412 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
413 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
414 return 2;
415}
416
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000417unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
418 VisitBinaryOperator(E);
419 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
420 E->setComputationResultType(Reader.GetType(Record[Idx++]));
421 return 2;
422}
423
424unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
425 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000426 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
427 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
428 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000429 return 3;
430}
431
Douglas Gregora151ba42009-04-14 23:32:43 +0000432unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
433 VisitCastExpr(E);
434 E->setLvalueCast(Record[Idx++]);
435 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000436}
437
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000438unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
439 VisitCastExpr(E);
440 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
441 return 1;
442}
443
444unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
445 VisitExplicitCastExpr(E);
446 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
447 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
448 return 1;
449}
450
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000451unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
452 VisitExpr(E);
453 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000454 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000455 E->setFileScope(Record[Idx++]);
456 return 1;
457}
458
Douglas Gregorec0b8292009-04-15 23:02:49 +0000459unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
460 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000461 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000462 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
463 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
464 return 1;
465}
466
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000467unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
468 VisitExpr(E);
469 unsigned NumInits = Record[Idx++];
470 E->reserveInits(NumInits);
471 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000472 E->updateInit(I,
473 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
474 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000475 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
476 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
477 E->setInitializedFieldInUnion(
478 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
479 E->sawArrayRangeDesignator(Record[Idx++]);
480 return NumInits + 1;
481}
482
483unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
484 typedef DesignatedInitExpr::Designator Designator;
485
486 VisitExpr(E);
487 unsigned NumSubExprs = Record[Idx++];
488 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
489 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000490 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000491 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
492 E->setGNUSyntax(Record[Idx++]);
493
494 llvm::SmallVector<Designator, 4> Designators;
495 while (Idx < Record.size()) {
496 switch ((pch::DesignatorTypes)Record[Idx++]) {
497 case pch::DESIG_FIELD_DECL: {
498 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
499 SourceLocation DotLoc
500 = SourceLocation::getFromRawEncoding(Record[Idx++]);
501 SourceLocation FieldLoc
502 = SourceLocation::getFromRawEncoding(Record[Idx++]);
503 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
504 FieldLoc));
505 Designators.back().setField(Field);
506 break;
507 }
508
509 case pch::DESIG_FIELD_NAME: {
510 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
511 SourceLocation DotLoc
512 = SourceLocation::getFromRawEncoding(Record[Idx++]);
513 SourceLocation FieldLoc
514 = SourceLocation::getFromRawEncoding(Record[Idx++]);
515 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
516 break;
517 }
518
519 case pch::DESIG_ARRAY: {
520 unsigned Index = Record[Idx++];
521 SourceLocation LBracketLoc
522 = SourceLocation::getFromRawEncoding(Record[Idx++]);
523 SourceLocation RBracketLoc
524 = SourceLocation::getFromRawEncoding(Record[Idx++]);
525 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
526 break;
527 }
528
529 case pch::DESIG_ARRAY_RANGE: {
530 unsigned Index = Record[Idx++];
531 SourceLocation LBracketLoc
532 = SourceLocation::getFromRawEncoding(Record[Idx++]);
533 SourceLocation EllipsisLoc
534 = SourceLocation::getFromRawEncoding(Record[Idx++]);
535 SourceLocation RBracketLoc
536 = SourceLocation::getFromRawEncoding(Record[Idx++]);
537 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
538 RBracketLoc));
539 break;
540 }
541 }
542 }
543 E->setDesignators(&Designators[0], Designators.size());
544
545 return NumSubExprs;
546}
547
548unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
549 VisitExpr(E);
550 return 0;
551}
552
Douglas Gregorec0b8292009-04-15 23:02:49 +0000553unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
554 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000555 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000556 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
557 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
558 return 1;
559}
560
Douglas Gregor209d4622009-04-15 23:33:31 +0000561unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
562 VisitExpr(E);
563 E->setArgType1(Reader.GetType(Record[Idx++]));
564 E->setArgType2(Reader.GetType(Record[Idx++]));
565 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
567 return 0;
568}
569
570unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
571 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000572 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
573 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
574 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000575 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
576 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
577 return 3;
578}
579
580unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
581 VisitExpr(E);
582 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
583 return 0;
584}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000585
Douglas Gregor725e94b2009-04-16 00:01:45 +0000586unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
587 VisitExpr(E);
588 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000589 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000590 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
591 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
592 return NumExprs;
593}
594
595unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
596 VisitExpr(E);
597 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
598 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
599 E->setByRef(Record[Idx++]);
600 return 0;
601}
602
Douglas Gregorc34897d2009-04-09 22:27:44 +0000603// FIXME: use the diagnostics machinery
604static bool Error(const char *Str) {
605 std::fprintf(stderr, "%s\n", Str);
606 return true;
607}
608
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000609/// \brief Check the contents of the predefines buffer against the
610/// contents of the predefines buffer used to build the PCH file.
611///
612/// The contents of the two predefines buffers should be the same. If
613/// not, then some command-line option changed the preprocessor state
614/// and we must reject the PCH file.
615///
616/// \param PCHPredef The start of the predefines buffer in the PCH
617/// file.
618///
619/// \param PCHPredefLen The length of the predefines buffer in the PCH
620/// file.
621///
622/// \param PCHBufferID The FileID for the PCH predefines buffer.
623///
624/// \returns true if there was a mismatch (in which case the PCH file
625/// should be ignored), or false otherwise.
626bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
627 unsigned PCHPredefLen,
628 FileID PCHBufferID) {
629 const char *Predef = PP.getPredefines().c_str();
630 unsigned PredefLen = PP.getPredefines().size();
631
632 // If the two predefines buffers compare equal, we're done!.
633 if (PredefLen == PCHPredefLen &&
634 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
635 return false;
636
637 // The predefines buffers are different. Produce a reasonable
638 // diagnostic showing where they are different.
639
640 // The source locations (potentially in the two different predefines
641 // buffers)
642 SourceLocation Loc1, Loc2;
643 SourceManager &SourceMgr = PP.getSourceManager();
644
645 // Create a source buffer for our predefines string, so
646 // that we can build a diagnostic that points into that
647 // source buffer.
648 FileID BufferID;
649 if (Predef && Predef[0]) {
650 llvm::MemoryBuffer *Buffer
651 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
652 "<built-in>");
653 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
654 }
655
656 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
657 std::pair<const char *, const char *> Locations
658 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
659
660 if (Locations.first != Predef + MinLen) {
661 // We found the location in the two buffers where there is a
662 // difference. Form source locations to point there (in both
663 // buffers).
664 unsigned Offset = Locations.first - Predef;
665 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
666 .getFileLocWithOffset(Offset);
667 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
668 .getFileLocWithOffset(Offset);
669 } else if (PredefLen > PCHPredefLen) {
670 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
671 .getFileLocWithOffset(MinLen);
672 } else {
673 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
674 .getFileLocWithOffset(MinLen);
675 }
676
677 Diag(Loc1, diag::warn_pch_preprocessor);
678 if (Loc2.isValid())
679 Diag(Loc2, diag::note_predef_in_pch);
680 Diag(diag::note_ignoring_pch) << FileName;
681 return true;
682}
683
Douglas Gregor635f97f2009-04-13 16:31:14 +0000684/// \brief Read the line table in the source manager block.
685/// \returns true if ther was an error.
686static bool ParseLineTable(SourceManager &SourceMgr,
687 llvm::SmallVectorImpl<uint64_t> &Record) {
688 unsigned Idx = 0;
689 LineTableInfo &LineTable = SourceMgr.getLineTable();
690
691 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000692 std::map<int, int> FileIDs;
693 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000694 // Extract the file name
695 unsigned FilenameLen = Record[Idx++];
696 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
697 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000698 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
699 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000700 }
701
702 // Parse the line entries
703 std::vector<LineEntry> Entries;
704 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000705 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000706
707 // Extract the line entries
708 unsigned NumEntries = Record[Idx++];
709 Entries.clear();
710 Entries.reserve(NumEntries);
711 for (unsigned I = 0; I != NumEntries; ++I) {
712 unsigned FileOffset = Record[Idx++];
713 unsigned LineNo = Record[Idx++];
714 int FilenameID = Record[Idx++];
715 SrcMgr::CharacteristicKind FileKind
716 = (SrcMgr::CharacteristicKind)Record[Idx++];
717 unsigned IncludeOffset = Record[Idx++];
718 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
719 FileKind, IncludeOffset));
720 }
721 LineTable.AddEntry(FID, Entries);
722 }
723
724 return false;
725}
726
Douglas Gregorab1cef72009-04-10 03:52:48 +0000727/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000728PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000729 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000730 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
731 Error("Malformed source manager block record");
732 return Failure;
733 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000734
735 SourceManager &SourceMgr = Context.getSourceManager();
736 RecordData Record;
737 while (true) {
738 unsigned Code = Stream.ReadCode();
739 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000740 if (Stream.ReadBlockEnd()) {
741 Error("Error at end of Source Manager block");
742 return Failure;
743 }
744
745 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000746 }
747
748 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
749 // No known subblocks, always skip them.
750 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000751 if (Stream.SkipBlock()) {
752 Error("Malformed block record");
753 return Failure;
754 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000755 continue;
756 }
757
758 if (Code == llvm::bitc::DEFINE_ABBREV) {
759 Stream.ReadAbbrevRecord();
760 continue;
761 }
762
763 // Read a record.
764 const char *BlobStart;
765 unsigned BlobLen;
766 Record.clear();
767 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
768 default: // Default behavior: ignore.
769 break;
770
771 case pch::SM_SLOC_FILE_ENTRY: {
772 // FIXME: We would really like to delay the creation of this
773 // FileEntry until it is actually required, e.g., when producing
774 // a diagnostic with a source location in this file.
775 const FileEntry *File
776 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
777 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000778 FileID ID = SourceMgr.createFileID(File,
779 SourceLocation::getFromRawEncoding(Record[1]),
780 (CharacteristicKind)Record[2]);
781 if (Record[3])
782 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
783 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000784 break;
785 }
786
787 case pch::SM_SLOC_BUFFER_ENTRY: {
788 const char *Name = BlobStart;
789 unsigned Code = Stream.ReadCode();
790 Record.clear();
791 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
792 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000793 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000794 llvm::MemoryBuffer *Buffer
795 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
796 BlobStart + BlobLen - 1,
797 Name);
798 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
799
800 if (strcmp(Name, "<built-in>") == 0
801 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
802 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000803 break;
804 }
805
806 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
807 SourceLocation SpellingLoc
808 = SourceLocation::getFromRawEncoding(Record[1]);
809 SourceMgr.createInstantiationLoc(
810 SpellingLoc,
811 SourceLocation::getFromRawEncoding(Record[2]),
812 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000813 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000814 break;
815 }
816
Chris Lattnere1be6022009-04-14 23:22:57 +0000817 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000818 if (ParseLineTable(SourceMgr, Record))
819 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000820 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000821 }
822 }
823}
824
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000825bool PCHReader::ReadPreprocessorBlock() {
826 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
827 return Error("Malformed preprocessor block record");
828
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000829 RecordData Record;
830 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
831 MacroInfo *LastMacro = 0;
832
833 while (true) {
834 unsigned Code = Stream.ReadCode();
835 switch (Code) {
836 case llvm::bitc::END_BLOCK:
837 if (Stream.ReadBlockEnd())
838 return Error("Error at end of preprocessor block");
839 return false;
840
841 case llvm::bitc::ENTER_SUBBLOCK:
842 // No known subblocks, always skip them.
843 Stream.ReadSubBlockID();
844 if (Stream.SkipBlock())
845 return Error("Malformed block record");
846 continue;
847
848 case llvm::bitc::DEFINE_ABBREV:
849 Stream.ReadAbbrevRecord();
850 continue;
851 default: break;
852 }
853
854 // Read a record.
855 Record.clear();
856 pch::PreprocessorRecordTypes RecType =
857 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
858 switch (RecType) {
859 default: // Default behavior: ignore unknown records.
860 break;
Chris Lattner4b21c202009-04-13 01:29:17 +0000861 case pch::PP_COUNTER_VALUE:
862 if (!Record.empty())
863 PP.setCounterValue(Record[0]);
864 break;
865
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000866 case pch::PP_MACRO_OBJECT_LIKE:
867 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +0000868 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
869 if (II == 0)
870 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000871 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
872 bool isUsed = Record[2];
873
874 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
875 MI->setIsUsed(isUsed);
876
877 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
878 // Decode function-like macro info.
879 bool isC99VarArgs = Record[3];
880 bool isGNUVarArgs = Record[4];
881 MacroArgs.clear();
882 unsigned NumArgs = Record[5];
883 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +0000884 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000885
886 // Install function-like macro info.
887 MI->setIsFunctionLike();
888 if (isC99VarArgs) MI->setIsC99Varargs();
889 if (isGNUVarArgs) MI->setIsGNUVarargs();
890 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
891 PP.getPreprocessorAllocator());
892 }
893
894 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000895 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000896
897 // Remember that we saw this macro last so that we add the tokens that
898 // form its body to it.
899 LastMacro = MI;
900 break;
901 }
902
903 case pch::PP_TOKEN: {
904 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
905 // pretend we didn't see this.
906 if (LastMacro == 0) break;
907
908 Token Tok;
909 Tok.startToken();
910 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
911 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +0000912 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
913 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000914 Tok.setKind((tok::TokenKind)Record[3]);
915 Tok.setFlag((Token::TokenFlags)Record[4]);
916 LastMacro->AddTokenToBody(Tok);
917 break;
918 }
919 }
920 }
921}
922
Douglas Gregor179cfb12009-04-10 20:39:37 +0000923PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
924 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
925 Error("Malformed block record");
926 return Failure;
927 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000928
Chris Lattner29241862009-04-11 21:15:38 +0000929 uint64_t PreprocessorBlockBit = 0;
930
Douglas Gregorc34897d2009-04-09 22:27:44 +0000931 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +0000932 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000933 while (!Stream.AtEndOfStream()) {
934 unsigned Code = Stream.ReadCode();
935 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +0000936 // If we saw the preprocessor block, read it now.
937 if (PreprocessorBlockBit) {
938 uint64_t SavedPos = Stream.GetCurrentBitNo();
939 Stream.JumpToBit(PreprocessorBlockBit);
940 if (ReadPreprocessorBlock()) {
941 Error("Malformed preprocessor block");
942 return Failure;
943 }
944 Stream.JumpToBit(SavedPos);
945 }
946
Douglas Gregor179cfb12009-04-10 20:39:37 +0000947 if (Stream.ReadBlockEnd()) {
948 Error("Error at end of module block");
949 return Failure;
950 }
Chris Lattner29241862009-04-11 21:15:38 +0000951
Douglas Gregor179cfb12009-04-10 20:39:37 +0000952 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000953 }
954
955 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
956 switch (Stream.ReadSubBlockID()) {
957 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
958 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
959 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000960 if (Stream.SkipBlock()) {
961 Error("Malformed block record");
962 return Failure;
963 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000964 break;
965
Chris Lattner29241862009-04-11 21:15:38 +0000966 case pch::PREPROCESSOR_BLOCK_ID:
967 // Skip the preprocessor block for now, but remember where it is. We
968 // want to read it in after the identifier table.
969 if (PreprocessorBlockBit) {
970 Error("Multiple preprocessor blocks found.");
971 return Failure;
972 }
973 PreprocessorBlockBit = Stream.GetCurrentBitNo();
974 if (Stream.SkipBlock()) {
975 Error("Malformed block record");
976 return Failure;
977 }
978 break;
979
Douglas Gregorab1cef72009-04-10 03:52:48 +0000980 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000981 switch (ReadSourceManagerBlock()) {
982 case Success:
983 break;
984
985 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000986 Error("Malformed source manager block");
987 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000988
989 case IgnorePCH:
990 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000991 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000992 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000993 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000994 continue;
995 }
996
997 if (Code == llvm::bitc::DEFINE_ABBREV) {
998 Stream.ReadAbbrevRecord();
999 continue;
1000 }
1001
1002 // Read and process a record.
1003 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001004 const char *BlobStart = 0;
1005 unsigned BlobLen = 0;
1006 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1007 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001008 default: // Default behavior: ignore.
1009 break;
1010
1011 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001012 if (!TypeOffsets.empty()) {
1013 Error("Duplicate TYPE_OFFSET record in PCH file");
1014 return Failure;
1015 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001016 TypeOffsets.swap(Record);
1017 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1018 break;
1019
1020 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001021 if (!DeclOffsets.empty()) {
1022 Error("Duplicate DECL_OFFSET record in PCH file");
1023 return Failure;
1024 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001025 DeclOffsets.swap(Record);
1026 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1027 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001028
1029 case pch::LANGUAGE_OPTIONS:
1030 if (ParseLanguageOptions(Record))
1031 return IgnorePCH;
1032 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001033
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001034 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001035 std::string TargetTriple(BlobStart, BlobLen);
1036 if (TargetTriple != Context.Target.getTargetTriple()) {
1037 Diag(diag::warn_pch_target_triple)
1038 << TargetTriple << Context.Target.getTargetTriple();
1039 Diag(diag::note_ignoring_pch) << FileName;
1040 return IgnorePCH;
1041 }
1042 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001043 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001044
1045 case pch::IDENTIFIER_TABLE:
1046 IdentifierTable = BlobStart;
1047 break;
1048
1049 case pch::IDENTIFIER_OFFSET:
1050 if (!IdentifierData.empty()) {
1051 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1052 return Failure;
1053 }
1054 IdentifierData.swap(Record);
1055#ifndef NDEBUG
1056 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1057 if ((IdentifierData[I] & 0x01) == 0) {
1058 Error("Malformed identifier table in the precompiled header");
1059 return Failure;
1060 }
1061 }
1062#endif
1063 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001064
1065 case pch::EXTERNAL_DEFINITIONS:
1066 if (!ExternalDefinitions.empty()) {
1067 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1068 return Failure;
1069 }
1070 ExternalDefinitions.swap(Record);
1071 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001072 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001073 }
1074
Douglas Gregor179cfb12009-04-10 20:39:37 +00001075 Error("Premature end of bitstream");
1076 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001077}
1078
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001079PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001080 // Set the PCH file name.
1081 this->FileName = FileName;
1082
Douglas Gregorc34897d2009-04-09 22:27:44 +00001083 // Open the PCH file.
1084 std::string ErrStr;
1085 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001086 if (!Buffer) {
1087 Error(ErrStr.c_str());
1088 return IgnorePCH;
1089 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001090
1091 // Initialize the stream
1092 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1093 (const unsigned char *)Buffer->getBufferEnd());
1094
1095 // Sniff for the signature.
1096 if (Stream.Read(8) != 'C' ||
1097 Stream.Read(8) != 'P' ||
1098 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001099 Stream.Read(8) != 'H') {
1100 Error("Not a PCH file");
1101 return IgnorePCH;
1102 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001103
1104 // We expect a number of well-defined blocks, though we don't necessarily
1105 // need to understand them all.
1106 while (!Stream.AtEndOfStream()) {
1107 unsigned Code = Stream.ReadCode();
1108
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001109 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1110 Error("Invalid record at top-level");
1111 return Failure;
1112 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001113
1114 unsigned BlockID = Stream.ReadSubBlockID();
1115
1116 // We only know the PCH subblock ID.
1117 switch (BlockID) {
1118 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001119 if (Stream.ReadBlockInfoBlock()) {
1120 Error("Malformed BlockInfoBlock");
1121 return Failure;
1122 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001123 break;
1124 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001125 switch (ReadPCHBlock()) {
1126 case Success:
1127 break;
1128
1129 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001130 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001131
1132 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001133 // FIXME: We could consider reading through to the end of this
1134 // PCH block, skipping subblocks, to see if there are other
1135 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001136 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001137 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001138 break;
1139 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001140 if (Stream.SkipBlock()) {
1141 Error("Malformed block record");
1142 return Failure;
1143 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001144 break;
1145 }
1146 }
1147
1148 // Load the translation unit declaration
1149 ReadDeclRecord(DeclOffsets[0], 0);
1150
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001151 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001152}
1153
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001154namespace {
1155 /// \brief Helper class that saves the current stream position and
1156 /// then restores it when destroyed.
1157 struct VISIBILITY_HIDDEN SavedStreamPosition {
1158 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001159 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001160
1161 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001162 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001163 }
1164
1165 private:
1166 llvm::BitstreamReader &Stream;
1167 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001168 };
1169}
1170
Douglas Gregor179cfb12009-04-10 20:39:37 +00001171/// \brief Parse the record that corresponds to a LangOptions data
1172/// structure.
1173///
1174/// This routine compares the language options used to generate the
1175/// PCH file against the language options set for the current
1176/// compilation. For each option, we classify differences between the
1177/// two compiler states as either "benign" or "important". Benign
1178/// differences don't matter, and we accept them without complaint
1179/// (and without modifying the language options). Differences between
1180/// the states for important options cause the PCH file to be
1181/// unusable, so we emit a warning and return true to indicate that
1182/// there was an error.
1183///
1184/// \returns true if the PCH file is unacceptable, false otherwise.
1185bool PCHReader::ParseLanguageOptions(
1186 const llvm::SmallVectorImpl<uint64_t> &Record) {
1187 const LangOptions &LangOpts = Context.getLangOptions();
1188#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1189#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1190 if (Record[Idx] != LangOpts.Option) { \
1191 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1192 Diag(diag::note_ignoring_pch) << FileName; \
1193 return true; \
1194 } \
1195 ++Idx
1196
1197 unsigned Idx = 0;
1198 PARSE_LANGOPT_BENIGN(Trigraphs);
1199 PARSE_LANGOPT_BENIGN(BCPLComment);
1200 PARSE_LANGOPT_BENIGN(DollarIdents);
1201 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1202 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1203 PARSE_LANGOPT_BENIGN(ImplicitInt);
1204 PARSE_LANGOPT_BENIGN(Digraphs);
1205 PARSE_LANGOPT_BENIGN(HexFloats);
1206 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1207 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1208 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1209 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1210 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1211 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1212 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1213 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1214 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1215 PARSE_LANGOPT_BENIGN(PascalStrings);
1216 PARSE_LANGOPT_BENIGN(Boolean);
1217 PARSE_LANGOPT_BENIGN(WritableStrings);
1218 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1219 diag::warn_pch_lax_vector_conversions);
1220 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1221 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1222 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1223 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1224 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1225 diag::warn_pch_thread_safe_statics);
1226 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1227 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1228 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1229 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1230 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1231 diag::warn_pch_heinous_extensions);
1232 // FIXME: Most of the options below are benign if the macro wasn't
1233 // used. Unfortunately, this means that a PCH compiled without
1234 // optimization can't be used with optimization turned on, even
1235 // though the only thing that changes is whether __OPTIMIZE__ was
1236 // defined... but if __OPTIMIZE__ never showed up in the header, it
1237 // doesn't matter. We could consider making this some special kind
1238 // of check.
1239 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1240 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1241 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1242 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1243 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1244 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1245 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1246 Diag(diag::warn_pch_gc_mode)
1247 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1248 Diag(diag::note_ignoring_pch) << FileName;
1249 return true;
1250 }
1251 ++Idx;
1252 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1253 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1254#undef PARSE_LANGOPT_IRRELEVANT
1255#undef PARSE_LANGOPT_BENIGN
1256
1257 return false;
1258}
1259
Douglas Gregorc34897d2009-04-09 22:27:44 +00001260/// \brief Read and return the type at the given offset.
1261///
1262/// This routine actually reads the record corresponding to the type
1263/// at the given offset in the bitstream. It is a helper routine for
1264/// GetType, which deals with reading type IDs.
1265QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001266 // Keep track of where we are in the stream, then jump back there
1267 // after reading this type.
1268 SavedStreamPosition SavedPosition(Stream);
1269
Douglas Gregorc34897d2009-04-09 22:27:44 +00001270 Stream.JumpToBit(Offset);
1271 RecordData Record;
1272 unsigned Code = Stream.ReadCode();
1273 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001274 case pch::TYPE_EXT_QUAL: {
1275 assert(Record.size() == 3 &&
1276 "Incorrect encoding of extended qualifier type");
1277 QualType Base = GetType(Record[0]);
1278 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1279 unsigned AddressSpace = Record[2];
1280
1281 QualType T = Base;
1282 if (GCAttr != QualType::GCNone)
1283 T = Context.getObjCGCQualType(T, GCAttr);
1284 if (AddressSpace)
1285 T = Context.getAddrSpaceQualType(T, AddressSpace);
1286 return T;
1287 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001288
Douglas Gregorc34897d2009-04-09 22:27:44 +00001289 case pch::TYPE_FIXED_WIDTH_INT: {
1290 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1291 return Context.getFixedWidthIntType(Record[0], Record[1]);
1292 }
1293
1294 case pch::TYPE_COMPLEX: {
1295 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1296 QualType ElemType = GetType(Record[0]);
1297 return Context.getComplexType(ElemType);
1298 }
1299
1300 case pch::TYPE_POINTER: {
1301 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1302 QualType PointeeType = GetType(Record[0]);
1303 return Context.getPointerType(PointeeType);
1304 }
1305
1306 case pch::TYPE_BLOCK_POINTER: {
1307 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1308 QualType PointeeType = GetType(Record[0]);
1309 return Context.getBlockPointerType(PointeeType);
1310 }
1311
1312 case pch::TYPE_LVALUE_REFERENCE: {
1313 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1314 QualType PointeeType = GetType(Record[0]);
1315 return Context.getLValueReferenceType(PointeeType);
1316 }
1317
1318 case pch::TYPE_RVALUE_REFERENCE: {
1319 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1320 QualType PointeeType = GetType(Record[0]);
1321 return Context.getRValueReferenceType(PointeeType);
1322 }
1323
1324 case pch::TYPE_MEMBER_POINTER: {
1325 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1326 QualType PointeeType = GetType(Record[0]);
1327 QualType ClassType = GetType(Record[1]);
1328 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1329 }
1330
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001331 case pch::TYPE_CONSTANT_ARRAY: {
1332 QualType ElementType = GetType(Record[0]);
1333 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1334 unsigned IndexTypeQuals = Record[2];
1335 unsigned Idx = 3;
1336 llvm::APInt Size = ReadAPInt(Record, Idx);
1337 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1338 }
1339
1340 case pch::TYPE_INCOMPLETE_ARRAY: {
1341 QualType ElementType = GetType(Record[0]);
1342 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1343 unsigned IndexTypeQuals = Record[2];
1344 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1345 }
1346
1347 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001348 QualType ElementType = GetType(Record[0]);
1349 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1350 unsigned IndexTypeQuals = Record[2];
1351 return Context.getVariableArrayType(ElementType, ReadExpr(),
1352 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001353 }
1354
1355 case pch::TYPE_VECTOR: {
1356 if (Record.size() != 2) {
1357 Error("Incorrect encoding of vector type in PCH file");
1358 return QualType();
1359 }
1360
1361 QualType ElementType = GetType(Record[0]);
1362 unsigned NumElements = Record[1];
1363 return Context.getVectorType(ElementType, NumElements);
1364 }
1365
1366 case pch::TYPE_EXT_VECTOR: {
1367 if (Record.size() != 2) {
1368 Error("Incorrect encoding of extended vector type in PCH file");
1369 return QualType();
1370 }
1371
1372 QualType ElementType = GetType(Record[0]);
1373 unsigned NumElements = Record[1];
1374 return Context.getExtVectorType(ElementType, NumElements);
1375 }
1376
1377 case pch::TYPE_FUNCTION_NO_PROTO: {
1378 if (Record.size() != 1) {
1379 Error("Incorrect encoding of no-proto function type");
1380 return QualType();
1381 }
1382 QualType ResultType = GetType(Record[0]);
1383 return Context.getFunctionNoProtoType(ResultType);
1384 }
1385
1386 case pch::TYPE_FUNCTION_PROTO: {
1387 QualType ResultType = GetType(Record[0]);
1388 unsigned Idx = 1;
1389 unsigned NumParams = Record[Idx++];
1390 llvm::SmallVector<QualType, 16> ParamTypes;
1391 for (unsigned I = 0; I != NumParams; ++I)
1392 ParamTypes.push_back(GetType(Record[Idx++]));
1393 bool isVariadic = Record[Idx++];
1394 unsigned Quals = Record[Idx++];
1395 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1396 isVariadic, Quals);
1397 }
1398
1399 case pch::TYPE_TYPEDEF:
1400 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1401 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1402
1403 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001404 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001405
1406 case pch::TYPE_TYPEOF: {
1407 if (Record.size() != 1) {
1408 Error("Incorrect encoding of typeof(type) in PCH file");
1409 return QualType();
1410 }
1411 QualType UnderlyingType = GetType(Record[0]);
1412 return Context.getTypeOfType(UnderlyingType);
1413 }
1414
1415 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001416 assert(Record.size() == 1 && "Incorrect encoding of record type");
1417 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001418
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001419 case pch::TYPE_ENUM:
1420 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1421 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1422
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001423 case pch::TYPE_OBJC_INTERFACE:
1424 // FIXME: Deserialize ObjCInterfaceType
1425 assert(false && "Cannot de-serialize ObjC interface types yet");
1426 return QualType();
1427
1428 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1429 // FIXME: Deserialize ObjCQualifiedInterfaceType
1430 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1431 return QualType();
1432
1433 case pch::TYPE_OBJC_QUALIFIED_ID:
1434 // FIXME: Deserialize ObjCQualifiedIdType
1435 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1436 return QualType();
1437
1438 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1439 // FIXME: Deserialize ObjCQualifiedClassType
1440 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1441 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001442 }
1443
1444 // Suppress a GCC warning
1445 return QualType();
1446}
1447
1448/// \brief Note that we have loaded the declaration with the given
1449/// Index.
1450///
1451/// This routine notes that this declaration has already been loaded,
1452/// so that future GetDecl calls will return this declaration rather
1453/// than trying to load a new declaration.
1454inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1455 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1456 DeclAlreadyLoaded[Index] = true;
1457 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1458}
1459
1460/// \brief Read the declaration at the given offset from the PCH file.
1461Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001462 // Keep track of where we are in the stream, then jump back there
1463 // after reading this declaration.
1464 SavedStreamPosition SavedPosition(Stream);
1465
Douglas Gregorc34897d2009-04-09 22:27:44 +00001466 Decl *D = 0;
1467 Stream.JumpToBit(Offset);
1468 RecordData Record;
1469 unsigned Code = Stream.ReadCode();
1470 unsigned Idx = 0;
1471 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001472
Douglas Gregorc34897d2009-04-09 22:27:44 +00001473 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001474 case pch::DECL_ATTR:
1475 case pch::DECL_CONTEXT_LEXICAL:
1476 case pch::DECL_CONTEXT_VISIBLE:
1477 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1478 break;
1479
Douglas Gregorc34897d2009-04-09 22:27:44 +00001480 case pch::DECL_TRANSLATION_UNIT:
1481 assert(Index == 0 && "Translation unit must be at index 0");
1482 Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl());
1483 D = Context.getTranslationUnitDecl();
1484 LoadedDecl(Index, D);
1485 break;
1486
1487 case pch::DECL_TYPEDEF: {
1488 TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(),
1489 0, QualType());
1490 LoadedDecl(Index, Typedef);
1491 Reader.VisitTypedefDecl(Typedef);
1492 D = Typedef;
1493 break;
1494 }
1495
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001496 case pch::DECL_ENUM: {
1497 EnumDecl *Enum = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
1498 LoadedDecl(Index, Enum);
1499 Reader.VisitEnumDecl(Enum);
1500 D = Enum;
1501 break;
1502 }
1503
Douglas Gregor982365e2009-04-13 21:20:57 +00001504 case pch::DECL_RECORD: {
1505 RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct,
1506 0, SourceLocation(), 0, 0);
1507 LoadedDecl(Index, Record);
1508 Reader.VisitRecordDecl(Record);
1509 D = Record;
1510 break;
1511 }
1512
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001513 case pch::DECL_ENUM_CONSTANT: {
1514 EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0,
1515 SourceLocation(), 0,
1516 QualType(), 0,
1517 llvm::APSInt());
1518 LoadedDecl(Index, ECD);
1519 Reader.VisitEnumConstantDecl(ECD);
1520 D = ECD;
1521 break;
1522 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001523
1524 case pch::DECL_FUNCTION: {
1525 FunctionDecl *Function = FunctionDecl::Create(Context, 0, SourceLocation(),
1526 DeclarationName(),
1527 QualType());
1528 LoadedDecl(Index, Function);
1529 Reader.VisitFunctionDecl(Function);
1530 D = Function;
1531 break;
1532 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001533
Douglas Gregor982365e2009-04-13 21:20:57 +00001534 case pch::DECL_FIELD: {
1535 FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0,
1536 QualType(), 0, false);
1537 LoadedDecl(Index, Field);
1538 Reader.VisitFieldDecl(Field);
1539 D = Field;
1540 break;
1541 }
1542
Douglas Gregorc34897d2009-04-09 22:27:44 +00001543 case pch::DECL_VAR: {
1544 VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1545 VarDecl::None, SourceLocation());
1546 LoadedDecl(Index, Var);
1547 Reader.VisitVarDecl(Var);
1548 D = Var;
1549 break;
1550 }
1551
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001552 case pch::DECL_PARM_VAR: {
1553 ParmVarDecl *Parm = ParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1554 QualType(), VarDecl::None, 0);
1555 LoadedDecl(Index, Parm);
1556 Reader.VisitParmVarDecl(Parm);
1557 D = Parm;
1558 break;
1559 }
1560
1561 case pch::DECL_ORIGINAL_PARM_VAR: {
1562 OriginalParmVarDecl *Parm
1563 = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1564 QualType(), QualType(), VarDecl::None,
1565 0);
1566 LoadedDecl(Index, Parm);
1567 Reader.VisitOriginalParmVarDecl(Parm);
1568 D = Parm;
1569 break;
1570 }
1571
Douglas Gregor2a491792009-04-13 22:49:25 +00001572 case pch::DECL_FILE_SCOPE_ASM: {
1573 FileScopeAsmDecl *Asm = FileScopeAsmDecl::Create(Context, 0,
1574 SourceLocation(), 0);
1575 LoadedDecl(Index, Asm);
1576 Reader.VisitFileScopeAsmDecl(Asm);
1577 D = Asm;
1578 break;
1579 }
1580
1581 case pch::DECL_BLOCK: {
1582 BlockDecl *Block = BlockDecl::Create(Context, 0, SourceLocation());
1583 LoadedDecl(Index, Block);
1584 Reader.VisitBlockDecl(Block);
1585 D = Block;
1586 break;
1587 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001588 }
1589
1590 // If this declaration is also a declaration context, get the
1591 // offsets for its tables of lexical and visible declarations.
1592 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1593 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1594 if (Offsets.first || Offsets.second) {
1595 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1596 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1597 DeclContextOffsets[DC] = Offsets;
1598 }
1599 }
1600 assert(Idx == Record.size());
1601
1602 return D;
1603}
1604
Douglas Gregorac8f2802009-04-10 17:25:41 +00001605QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001606 unsigned Quals = ID & 0x07;
1607 unsigned Index = ID >> 3;
1608
1609 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1610 QualType T;
1611 switch ((pch::PredefinedTypeIDs)Index) {
1612 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1613 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1614 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1615
1616 case pch::PREDEF_TYPE_CHAR_U_ID:
1617 case pch::PREDEF_TYPE_CHAR_S_ID:
1618 // FIXME: Check that the signedness of CharTy is correct!
1619 T = Context.CharTy;
1620 break;
1621
1622 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1623 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1624 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1625 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1626 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1627 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1628 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1629 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1630 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1631 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1632 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1633 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1634 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1635 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1636 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1637 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1638 }
1639
1640 assert(!T.isNull() && "Unknown predefined type");
1641 return T.getQualifiedType(Quals);
1642 }
1643
1644 Index -= pch::NUM_PREDEF_TYPE_IDS;
1645 if (!TypeAlreadyLoaded[Index]) {
1646 // Load the type from the PCH file.
1647 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1648 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1649 TypeAlreadyLoaded[Index] = true;
1650 }
1651
1652 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1653}
1654
Douglas Gregorac8f2802009-04-10 17:25:41 +00001655Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001656 if (ID == 0)
1657 return 0;
1658
1659 unsigned Index = ID - 1;
1660 if (DeclAlreadyLoaded[Index])
1661 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1662
1663 // Load the declaration from the PCH file.
1664 return ReadDeclRecord(DeclOffsets[Index], Index);
1665}
1666
1667bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001668 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001669 assert(DC->hasExternalLexicalStorage() &&
1670 "DeclContext has no lexical decls in storage");
1671 uint64_t Offset = DeclContextOffsets[DC].first;
1672 assert(Offset && "DeclContext has no lexical decls in storage");
1673
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001674 // Keep track of where we are in the stream, then jump back there
1675 // after reading this context.
1676 SavedStreamPosition SavedPosition(Stream);
1677
Douglas Gregorc34897d2009-04-09 22:27:44 +00001678 // Load the record containing all of the declarations lexically in
1679 // this context.
1680 Stream.JumpToBit(Offset);
1681 RecordData Record;
1682 unsigned Code = Stream.ReadCode();
1683 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001684 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001685 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1686
1687 // Load all of the declaration IDs
1688 Decls.clear();
1689 Decls.insert(Decls.end(), Record.begin(), Record.end());
1690 return false;
1691}
1692
1693bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1694 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1695 assert(DC->hasExternalVisibleStorage() &&
1696 "DeclContext has no visible decls in storage");
1697 uint64_t Offset = DeclContextOffsets[DC].second;
1698 assert(Offset && "DeclContext has no visible decls in storage");
1699
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001700 // Keep track of where we are in the stream, then jump back there
1701 // after reading this context.
1702 SavedStreamPosition SavedPosition(Stream);
1703
Douglas Gregorc34897d2009-04-09 22:27:44 +00001704 // Load the record containing all of the declarations visible in
1705 // this context.
1706 Stream.JumpToBit(Offset);
1707 RecordData Record;
1708 unsigned Code = Stream.ReadCode();
1709 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001710 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001711 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1712 if (Record.size() == 0)
1713 return false;
1714
1715 Decls.clear();
1716
1717 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001718 while (Idx < Record.size()) {
1719 Decls.push_back(VisibleDeclaration());
1720 Decls.back().Name = ReadDeclarationName(Record, Idx);
1721
Douglas Gregorc34897d2009-04-09 22:27:44 +00001722 unsigned Size = Record[Idx++];
1723 llvm::SmallVector<unsigned, 4> & LoadedDecls
1724 = Decls.back().Declarations;
1725 LoadedDecls.reserve(Size);
1726 for (unsigned I = 0; I < Size; ++I)
1727 LoadedDecls.push_back(Record[Idx++]);
1728 }
1729
1730 return false;
1731}
1732
Douglas Gregor631f6c62009-04-14 00:24:19 +00001733void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1734 if (!Consumer)
1735 return;
1736
1737 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1738 Decl *D = GetDecl(ExternalDefinitions[I]);
1739 DeclGroupRef DG(D);
1740 Consumer->HandleTopLevelDecl(DG);
1741 }
1742}
1743
Douglas Gregorc34897d2009-04-09 22:27:44 +00001744void PCHReader::PrintStats() {
1745 std::fprintf(stderr, "*** PCH Statistics:\n");
1746
1747 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1748 TypeAlreadyLoaded.end(),
1749 true);
1750 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1751 DeclAlreadyLoaded.end(),
1752 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001753 unsigned NumIdentifiersLoaded = 0;
1754 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1755 if ((IdentifierData[I] & 0x01) == 0)
1756 ++NumIdentifiersLoaded;
1757 }
1758
Douglas Gregorc34897d2009-04-09 22:27:44 +00001759 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1760 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001761 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001762 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1763 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001764 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1765 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1766 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1767 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001768 std::fprintf(stderr, "\n");
1769}
1770
Chris Lattner29241862009-04-11 21:15:38 +00001771IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001772 if (ID == 0)
1773 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001774
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001775 if (!IdentifierTable || IdentifierData.empty()) {
1776 Error("No identifier table in PCH file");
1777 return 0;
1778 }
Chris Lattner29241862009-04-11 21:15:38 +00001779
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001780 if (IdentifierData[ID - 1] & 0x01) {
1781 uint64_t Offset = IdentifierData[ID - 1];
1782 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001783 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001784 }
Chris Lattner29241862009-04-11 21:15:38 +00001785
1786 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787}
1788
1789DeclarationName
1790PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1791 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1792 switch (Kind) {
1793 case DeclarationName::Identifier:
1794 return DeclarationName(GetIdentifierInfo(Record, Idx));
1795
1796 case DeclarationName::ObjCZeroArgSelector:
1797 case DeclarationName::ObjCOneArgSelector:
1798 case DeclarationName::ObjCMultiArgSelector:
1799 assert(false && "Unable to de-serialize Objective-C selectors");
1800 break;
1801
1802 case DeclarationName::CXXConstructorName:
1803 return Context.DeclarationNames.getCXXConstructorName(
1804 GetType(Record[Idx++]));
1805
1806 case DeclarationName::CXXDestructorName:
1807 return Context.DeclarationNames.getCXXDestructorName(
1808 GetType(Record[Idx++]));
1809
1810 case DeclarationName::CXXConversionFunctionName:
1811 return Context.DeclarationNames.getCXXConversionFunctionName(
1812 GetType(Record[Idx++]));
1813
1814 case DeclarationName::CXXOperatorName:
1815 return Context.DeclarationNames.getCXXOperatorName(
1816 (OverloadedOperatorKind)Record[Idx++]);
1817
1818 case DeclarationName::CXXUsingDirective:
1819 return DeclarationName::getUsingDirectiveName();
1820 }
1821
1822 // Required to silence GCC warning
1823 return DeclarationName();
1824}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001825
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001826/// \brief Read an integral value
1827llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1828 unsigned BitWidth = Record[Idx++];
1829 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1830 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1831 Idx += NumWords;
1832 return Result;
1833}
1834
1835/// \brief Read a signed integral value
1836llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1837 bool isUnsigned = Record[Idx++];
1838 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1839}
1840
Douglas Gregore2f37202009-04-14 21:55:33 +00001841/// \brief Read a floating-point value
1842llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001843 return llvm::APFloat(ReadAPInt(Record, Idx));
1844}
1845
Douglas Gregor1c507882009-04-15 21:30:51 +00001846// \brief Read a string
1847std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1848 unsigned Len = Record[Idx++];
1849 std::string Result(&Record[Idx], &Record[Idx] + Len);
1850 Idx += Len;
1851 return Result;
1852}
1853
1854/// \brief Reads attributes from the current stream position.
1855Attr *PCHReader::ReadAttributes() {
1856 unsigned Code = Stream.ReadCode();
1857 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1858 "Expected unabbreviated record"); (void)Code;
1859
1860 RecordData Record;
1861 unsigned Idx = 0;
1862 unsigned RecCode = Stream.ReadRecord(Code, Record);
1863 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1864 (void)RecCode;
1865
1866#define SIMPLE_ATTR(Name) \
1867 case Attr::Name: \
1868 New = ::new (Context) Name##Attr(); \
1869 break
1870
1871#define STRING_ATTR(Name) \
1872 case Attr::Name: \
1873 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1874 break
1875
1876#define UNSIGNED_ATTR(Name) \
1877 case Attr::Name: \
1878 New = ::new (Context) Name##Attr(Record[Idx++]); \
1879 break
1880
1881 Attr *Attrs = 0;
1882 while (Idx < Record.size()) {
1883 Attr *New = 0;
1884 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1885 bool IsInherited = Record[Idx++];
1886
1887 switch (Kind) {
1888 STRING_ATTR(Alias);
1889 UNSIGNED_ATTR(Aligned);
1890 SIMPLE_ATTR(AlwaysInline);
1891 SIMPLE_ATTR(AnalyzerNoReturn);
1892 STRING_ATTR(Annotate);
1893 STRING_ATTR(AsmLabel);
1894
1895 case Attr::Blocks:
1896 New = ::new (Context) BlocksAttr(
1897 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1898 break;
1899
1900 case Attr::Cleanup:
1901 New = ::new (Context) CleanupAttr(
1902 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1903 break;
1904
1905 SIMPLE_ATTR(Const);
1906 UNSIGNED_ATTR(Constructor);
1907 SIMPLE_ATTR(DLLExport);
1908 SIMPLE_ATTR(DLLImport);
1909 SIMPLE_ATTR(Deprecated);
1910 UNSIGNED_ATTR(Destructor);
1911 SIMPLE_ATTR(FastCall);
1912
1913 case Attr::Format: {
1914 std::string Type = ReadString(Record, Idx);
1915 unsigned FormatIdx = Record[Idx++];
1916 unsigned FirstArg = Record[Idx++];
1917 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1918 break;
1919 }
1920
1921 SIMPLE_ATTR(GNUCInline);
1922
1923 case Attr::IBOutletKind:
1924 New = ::new (Context) IBOutletAttr();
1925 break;
1926
1927 SIMPLE_ATTR(NoReturn);
1928 SIMPLE_ATTR(NoThrow);
1929 SIMPLE_ATTR(Nodebug);
1930 SIMPLE_ATTR(Noinline);
1931
1932 case Attr::NonNull: {
1933 unsigned Size = Record[Idx++];
1934 llvm::SmallVector<unsigned, 16> ArgNums;
1935 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1936 Idx += Size;
1937 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1938 break;
1939 }
1940
1941 SIMPLE_ATTR(ObjCException);
1942 SIMPLE_ATTR(ObjCNSObject);
1943 SIMPLE_ATTR(Overloadable);
1944 UNSIGNED_ATTR(Packed);
1945 SIMPLE_ATTR(Pure);
1946 UNSIGNED_ATTR(Regparm);
1947 STRING_ATTR(Section);
1948 SIMPLE_ATTR(StdCall);
1949 SIMPLE_ATTR(TransparentUnion);
1950 SIMPLE_ATTR(Unavailable);
1951 SIMPLE_ATTR(Unused);
1952 SIMPLE_ATTR(Used);
1953
1954 case Attr::Visibility:
1955 New = ::new (Context) VisibilityAttr(
1956 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1957 break;
1958
1959 SIMPLE_ATTR(WarnUnusedResult);
1960 SIMPLE_ATTR(Weak);
1961 SIMPLE_ATTR(WeakImport);
1962 }
1963
1964 assert(New && "Unable to decode attribute?");
1965 New->setInherited(IsInherited);
1966 New->setNext(Attrs);
1967 Attrs = New;
1968 }
1969#undef UNSIGNED_ATTR
1970#undef STRING_ATTR
1971#undef SIMPLE_ATTR
1972
1973 // The list of attributes was built backwards. Reverse the list
1974 // before returning it.
1975 Attr *PrevAttr = 0, *NextAttr = 0;
1976 while (Attrs) {
1977 NextAttr = Attrs->getNext();
1978 Attrs->setNext(PrevAttr);
1979 PrevAttr = Attrs;
1980 Attrs = NextAttr;
1981 }
1982
1983 return PrevAttr;
1984}
1985
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001986Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00001987 // Within the bitstream, expressions are stored in Reverse Polish
1988 // Notation, with each of the subexpressions preceding the
1989 // expression they are stored in. To evaluate expressions, we
1990 // continue reading expressions and placing them on the stack, with
1991 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001992 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00001993 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001994 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00001995 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001996 llvm::SmallVector<Stmt *, 16> StmtStack;
1997 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001998 Stmt::EmptyShell Empty;
1999
Douglas Gregora151ba42009-04-14 23:32:43 +00002000 while (true) {
2001 unsigned Code = Stream.ReadCode();
2002 if (Code == llvm::bitc::END_BLOCK) {
2003 if (Stream.ReadBlockEnd()) {
2004 Error("Error at end of Source Manager block");
2005 return 0;
2006 }
2007 break;
2008 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002009
Douglas Gregora151ba42009-04-14 23:32:43 +00002010 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2011 // No known subblocks, always skip them.
2012 Stream.ReadSubBlockID();
2013 if (Stream.SkipBlock()) {
2014 Error("Malformed block record");
2015 return 0;
2016 }
2017 continue;
2018 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002019
Douglas Gregora151ba42009-04-14 23:32:43 +00002020 if (Code == llvm::bitc::DEFINE_ABBREV) {
2021 Stream.ReadAbbrevRecord();
2022 continue;
2023 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002024
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002025 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002026 Idx = 0;
2027 Record.clear();
2028 bool Finished = false;
2029 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002030 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002031 Finished = true;
2032 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002033
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002034 case pch::STMT_NULL_PTR:
2035 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002036 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002037
Douglas Gregora151ba42009-04-14 23:32:43 +00002038 case pch::EXPR_PREDEFINED:
2039 // FIXME: untested (until we can serialize function bodies).
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002040 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002041 break;
2042
2043 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002044 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002045 break;
2046
2047 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002048 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002049 break;
2050
2051 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002052 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002053 break;
2054
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002055 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002056 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002057 break;
2058
Douglas Gregor596e0932009-04-15 16:35:07 +00002059 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002060 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002061 Record[PCHStmtReader::NumExprFields + 1]);
2062 break;
2063
Douglas Gregora151ba42009-04-14 23:32:43 +00002064 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002065 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002066 break;
2067
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002068 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002069 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002070 break;
2071
Douglas Gregor12d74052009-04-15 15:58:59 +00002072 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002073 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002074 break;
2075
2076 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002077 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002078 break;
2079
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002080 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002081 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002082 break;
2083
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002084 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002085 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002086 break;
2087
2088 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002089 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002090 break;
2091
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002092 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002093 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002094 break;
2095
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002096 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002097 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002098 break;
2099
2100 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002101 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002102 break;
2103
Douglas Gregora151ba42009-04-14 23:32:43 +00002104 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002105 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002106 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002107
2108 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002109 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002110 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002111
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002112 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002113 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002114 break;
2115
Douglas Gregorec0b8292009-04-15 23:02:49 +00002116 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002117 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002118 break;
2119
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002120 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002121 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002122 break;
2123
2124 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002125 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002126 Record[PCHStmtReader::NumExprFields] - 1);
2127
2128 break;
2129
2130 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002131 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002132 break;
2133
Douglas Gregorec0b8292009-04-15 23:02:49 +00002134 case pch::EXPR_VA_ARG:
2135 // FIXME: untested; we need function bodies first
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002136 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002137 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002138
2139 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002140 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002141 break;
2142
2143 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002144 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002145 break;
2146
2147 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002148 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002149 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002150
2151 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002152 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002153 break;
2154
2155 case pch::EXPR_BLOCK_DECL_REF:
2156 // FIXME: untested until we have statement and block support
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002157 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002158 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002159 }
2160
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002161 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002162 if (Finished)
2163 break;
2164
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002165 if (S) {
2166 unsigned NumSubStmts = Reader.Visit(S);
2167 while (NumSubStmts > 0) {
2168 StmtStack.pop_back();
2169 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002170 }
2171 }
2172
2173 assert(Idx == Record.size() && "Invalid deserialization of expression");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002174 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002175 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002176 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
2177 return StmtStack.back();
2178}
2179
2180Expr *PCHReader::ReadExpr() {
2181 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002182}
2183
Douglas Gregor179cfb12009-04-10 20:39:37 +00002184DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002185 return Diag(SourceLocation(), DiagID);
2186}
2187
2188DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2189 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002190 Context.getSourceManager()),
2191 DiagID);
2192}