blob: b829075913ff3f0a37d875d4b6550aa5833df525 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHReader.cpp - Precompiled Headers Reader -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PCHReader class, which reads a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Frontend/PCHReader.h"
Douglas Gregor179cfb12009-04-10 20:39:37 +000014#include "clang/Frontend/FrontendDiagnostic.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
Douglas Gregor631f6c62009-04-14 00:24:19 +000018#include "clang/AST/DeclGroup.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000022#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000023#include "clang/Lex/Preprocessor.h"
24#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000025#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000028#include "llvm/Bitcode/BitstreamReader.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include <algorithm>
32#include <cstdio>
33
34using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Declaration deserialization
38//===----------------------------------------------------------------------===//
39namespace {
40 class VISIBILITY_HIDDEN PCHDeclReader {
41 PCHReader &Reader;
42 const PCHReader::RecordData &Record;
43 unsigned &Idx;
44
45 public:
46 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
47 unsigned &Idx)
48 : Reader(Reader), Record(Record), Idx(Idx) { }
49
50 void VisitDecl(Decl *D);
51 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52 void VisitNamedDecl(NamedDecl *ND);
53 void VisitTypeDecl(TypeDecl *TD);
54 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000055 void VisitTagDecl(TagDecl *TD);
56 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000057 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000058 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000059 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000060 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000061 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000062 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000063 void VisitParmVarDecl(ParmVarDecl *PD);
64 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000065 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
66 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000067 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
68 };
69}
70
71void PCHDeclReader::VisitDecl(Decl *D) {
72 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
73 D->setLexicalDeclContext(
74 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
75 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
76 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000077 if (Record[Idx++])
78 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000079 D->setImplicit(Record[Idx++]);
80 D->setAccess((AccessSpecifier)Record[Idx++]);
81}
82
83void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
84 VisitDecl(TU);
85}
86
87void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
88 VisitDecl(ND);
89 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
90}
91
92void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
93 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000094 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
95}
96
97void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +000098 // Note that we cannot use VisitTypeDecl here, because we need to
99 // set the underlying type of the typedef *before* we try to read
100 // the type associated with the TypedefDecl.
101 VisitNamedDecl(TD);
102 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
103 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
104 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000105}
106
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000107void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
108 VisitTypeDecl(TD);
109 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
110 TD->setDefinition(Record[Idx++]);
111 TD->setTypedefForAnonDecl(
112 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
113}
114
115void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
116 VisitTagDecl(ED);
117 ED->setIntegerType(Reader.GetType(Record[Idx++]));
118}
119
Douglas Gregor982365e2009-04-13 21:20:57 +0000120void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
121 VisitTagDecl(RD);
122 RD->setHasFlexibleArrayMember(Record[Idx++]);
123 RD->setAnonymousStructOrUnion(Record[Idx++]);
124}
125
Douglas Gregorc34897d2009-04-09 22:27:44 +0000126void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
127 VisitNamedDecl(VD);
128 VD->setType(Reader.GetType(Record[Idx++]));
129}
130
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000131void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
132 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000133 if (Record[Idx++])
134 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000135 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
136}
137
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000138void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
139 VisitValueDecl(FD);
140 // FIXME: function body
141 FD->setPreviousDeclaration(
142 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
143 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
144 FD->setInline(Record[Idx++]);
145 FD->setVirtual(Record[Idx++]);
146 FD->setPure(Record[Idx++]);
147 FD->setInheritedPrototype(Record[Idx++]);
148 FD->setHasPrototype(Record[Idx++]);
149 FD->setDeleted(Record[Idx++]);
150 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
151 unsigned NumParams = Record[Idx++];
152 llvm::SmallVector<ParmVarDecl *, 16> Params;
153 Params.reserve(NumParams);
154 for (unsigned I = 0; I != NumParams; ++I)
155 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
156 FD->setParams(Reader.getContext(), &Params[0], NumParams);
157}
158
Douglas Gregor982365e2009-04-13 21:20:57 +0000159void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
160 VisitValueDecl(FD);
161 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000162 if (Record[Idx++])
163 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000164}
165
Douglas Gregorc34897d2009-04-09 22:27:44 +0000166void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
167 VisitValueDecl(VD);
168 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
169 VD->setThreadSpecified(Record[Idx++]);
170 VD->setCXXDirectInitializer(Record[Idx++]);
171 VD->setDeclaredInCondition(Record[Idx++]);
172 VD->setPreviousDeclaration(
173 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
174 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000175 if (Record[Idx++])
176 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000177}
178
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000179void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
180 VisitVarDecl(PD);
181 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000182 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000183}
184
185void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
186 VisitParmVarDecl(PD);
187 PD->setOriginalType(Reader.GetType(Record[Idx++]));
188}
189
Douglas Gregor2a491792009-04-13 22:49:25 +0000190void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
191 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000192 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000193}
194
195void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
196 VisitDecl(BD);
197 unsigned NumParams = Record[Idx++];
198 llvm::SmallVector<ParmVarDecl *, 16> Params;
199 Params.reserve(NumParams);
200 for (unsigned I = 0; I != NumParams; ++I)
201 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
202 BD->setParams(Reader.getContext(), &Params[0], NumParams);
203}
204
Douglas Gregorc34897d2009-04-09 22:27:44 +0000205std::pair<uint64_t, uint64_t>
206PCHDeclReader::VisitDeclContext(DeclContext *DC) {
207 uint64_t LexicalOffset = Record[Idx++];
208 uint64_t VisibleOffset = 0;
209 if (DC->getPrimaryContext() == DC)
210 VisibleOffset = Record[Idx++];
211 return std::make_pair(LexicalOffset, VisibleOffset);
212}
213
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000214//===----------------------------------------------------------------------===//
215// Statement/expression deserialization
216//===----------------------------------------------------------------------===//
217namespace {
218 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000219 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000220 PCHReader &Reader;
221 const PCHReader::RecordData &Record;
222 unsigned &Idx;
Douglas Gregora151ba42009-04-14 23:32:43 +0000223 llvm::SmallVectorImpl<Expr *> &ExprStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000224
225 public:
226 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregora151ba42009-04-14 23:32:43 +0000227 unsigned &Idx, llvm::SmallVectorImpl<Expr *> &ExprStack)
228 : Reader(Reader), Record(Record), Idx(Idx), ExprStack(ExprStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000229
Douglas Gregor596e0932009-04-15 16:35:07 +0000230 /// \brief The number of record fields required for the Expr class
231 /// itself.
232 static const unsigned NumExprFields = 3;
233
Douglas Gregora151ba42009-04-14 23:32:43 +0000234 // Each of the Visit* functions reads in part of the expression
235 // from the given record and the current expression stack, then
236 // return the total number of operands that it read from the
237 // expression stack.
238
239 unsigned VisitExpr(Expr *E);
240 unsigned VisitPredefinedExpr(PredefinedExpr *E);
241 unsigned VisitDeclRefExpr(DeclRefExpr *E);
242 unsigned VisitIntegerLiteral(IntegerLiteral *E);
243 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000244 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000245 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000246 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000247 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000248 unsigned VisitUnaryOperator(UnaryOperator *E);
249 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000250 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000251 unsigned VisitCallExpr(CallExpr *E);
252 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000253 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000254 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000255 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
256 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000257 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000258 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
259 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000260 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
261 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000262 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
263 unsigned VisitChooseExpr(ChooseExpr *E);
264 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000265 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
266 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000267 };
268}
269
Douglas Gregora151ba42009-04-14 23:32:43 +0000270unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000271 E->setType(Reader.GetType(Record[Idx++]));
272 E->setTypeDependent(Record[Idx++]);
273 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000274 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000275 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000276}
277
Douglas Gregora151ba42009-04-14 23:32:43 +0000278unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000279 VisitExpr(E);
280 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
281 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000282 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000283}
284
Douglas Gregora151ba42009-04-14 23:32:43 +0000285unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000286 VisitExpr(E);
287 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
288 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000289 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000290}
291
Douglas Gregora151ba42009-04-14 23:32:43 +0000292unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000293 VisitExpr(E);
294 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
295 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000296 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000297}
298
Douglas Gregora151ba42009-04-14 23:32:43 +0000299unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000300 VisitExpr(E);
301 E->setValue(Reader.ReadAPFloat(Record, Idx));
302 E->setExact(Record[Idx++]);
303 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000304 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000305}
306
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000307unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
308 VisitExpr(E);
309 E->setSubExpr(ExprStack.back());
310 return 1;
311}
312
Douglas Gregor596e0932009-04-15 16:35:07 +0000313unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
314 VisitExpr(E);
315 unsigned Len = Record[Idx++];
316 assert(Record[Idx] == E->getNumConcatenated() &&
317 "Wrong number of concatenated tokens!");
318 ++Idx;
319 E->setWide(Record[Idx++]);
320
321 // Read string data
322 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
323 E->setStrData(Reader.getContext(), &Str[0], Len);
324 Idx += Len;
325
326 // Read source locations
327 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
328 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
329
330 return 0;
331}
332
Douglas Gregora151ba42009-04-14 23:32:43 +0000333unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000334 VisitExpr(E);
335 E->setValue(Record[Idx++]);
336 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
337 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000338 return 0;
339}
340
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000341unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
342 VisitExpr(E);
343 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
344 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
345 E->setSubExpr(ExprStack.back());
346 return 1;
347}
348
Douglas Gregor12d74052009-04-15 15:58:59 +0000349unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
350 VisitExpr(E);
351 E->setSubExpr(ExprStack.back());
352 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
353 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
354 return 1;
355}
356
357unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
358 VisitExpr(E);
359 E->setSizeof(Record[Idx++]);
360 if (Record[Idx] == 0) {
361 E->setArgument(ExprStack.back());
362 ++Idx;
363 } else {
364 E->setArgument(Reader.GetType(Record[Idx++]));
365 }
366 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
367 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
368 return E->isArgumentType()? 0 : 1;
369}
370
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000371unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
372 VisitExpr(E);
373 E->setLHS(ExprStack[ExprStack.size() - 2]);
374 E->setRHS(ExprStack[ExprStack.size() - 2]);
375 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
376 return 2;
377}
378
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000379unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
380 VisitExpr(E);
381 E->setNumArgs(Reader.getContext(), Record[Idx++]);
382 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
383 E->setCallee(ExprStack[ExprStack.size() - E->getNumArgs() - 1]);
384 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
385 E->setArg(I, ExprStack[ExprStack.size() - N + I]);
386 return E->getNumArgs() + 1;
387}
388
389unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
390 VisitExpr(E);
391 E->setBase(ExprStack.back());
392 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
393 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
394 E->setArrow(Record[Idx++]);
395 return 1;
396}
397
Douglas Gregora151ba42009-04-14 23:32:43 +0000398unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
399 VisitExpr(E);
400 E->setSubExpr(ExprStack.back());
401 return 1;
402}
403
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000404unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
405 VisitExpr(E);
406 E->setLHS(ExprStack.end()[-2]);
407 E->setRHS(ExprStack.end()[-1]);
408 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
409 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
410 return 2;
411}
412
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000413unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
414 VisitBinaryOperator(E);
415 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
416 E->setComputationResultType(Reader.GetType(Record[Idx++]));
417 return 2;
418}
419
420unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
421 VisitExpr(E);
422 E->setCond(ExprStack[ExprStack.size() - 3]);
423 E->setLHS(ExprStack[ExprStack.size() - 2]);
424 E->setRHS(ExprStack[ExprStack.size() - 1]);
425 return 3;
426}
427
Douglas Gregora151ba42009-04-14 23:32:43 +0000428unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
429 VisitCastExpr(E);
430 E->setLvalueCast(Record[Idx++]);
431 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000432}
433
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000434unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
435 VisitCastExpr(E);
436 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
437 return 1;
438}
439
440unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
441 VisitExplicitCastExpr(E);
442 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
443 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
444 return 1;
445}
446
Douglas Gregorec0b8292009-04-15 23:02:49 +0000447unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
448 VisitExpr(E);
449 E->setBase(ExprStack.back());
450 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
451 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
452 return 1;
453}
454
455unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
456 VisitExpr(E);
457 E->setSubExpr(ExprStack.back());
458 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
459 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
460 return 1;
461}
462
Douglas Gregor209d4622009-04-15 23:33:31 +0000463unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
464 VisitExpr(E);
465 E->setArgType1(Reader.GetType(Record[Idx++]));
466 E->setArgType2(Reader.GetType(Record[Idx++]));
467 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
468 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
469 return 0;
470}
471
472unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
473 VisitExpr(E);
474 E->setCond(ExprStack[ExprStack.size() - 3]);
475 E->setLHS(ExprStack[ExprStack.size() - 2]);
476 E->setRHS(ExprStack[ExprStack.size() - 1]);
477 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
478 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
479 return 3;
480}
481
482unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
483 VisitExpr(E);
484 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
485 return 0;
486}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000487
Douglas Gregor725e94b2009-04-16 00:01:45 +0000488unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
489 VisitExpr(E);
490 unsigned NumExprs = Record[Idx++];
491 E->setExprs(&ExprStack[ExprStack.size() - NumExprs], NumExprs);
492 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
493 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
494 return NumExprs;
495}
496
497unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
498 VisitExpr(E);
499 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
500 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
501 E->setByRef(Record[Idx++]);
502 return 0;
503}
504
Douglas Gregorc34897d2009-04-09 22:27:44 +0000505// FIXME: use the diagnostics machinery
506static bool Error(const char *Str) {
507 std::fprintf(stderr, "%s\n", Str);
508 return true;
509}
510
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000511/// \brief Check the contents of the predefines buffer against the
512/// contents of the predefines buffer used to build the PCH file.
513///
514/// The contents of the two predefines buffers should be the same. If
515/// not, then some command-line option changed the preprocessor state
516/// and we must reject the PCH file.
517///
518/// \param PCHPredef The start of the predefines buffer in the PCH
519/// file.
520///
521/// \param PCHPredefLen The length of the predefines buffer in the PCH
522/// file.
523///
524/// \param PCHBufferID The FileID for the PCH predefines buffer.
525///
526/// \returns true if there was a mismatch (in which case the PCH file
527/// should be ignored), or false otherwise.
528bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
529 unsigned PCHPredefLen,
530 FileID PCHBufferID) {
531 const char *Predef = PP.getPredefines().c_str();
532 unsigned PredefLen = PP.getPredefines().size();
533
534 // If the two predefines buffers compare equal, we're done!.
535 if (PredefLen == PCHPredefLen &&
536 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
537 return false;
538
539 // The predefines buffers are different. Produce a reasonable
540 // diagnostic showing where they are different.
541
542 // The source locations (potentially in the two different predefines
543 // buffers)
544 SourceLocation Loc1, Loc2;
545 SourceManager &SourceMgr = PP.getSourceManager();
546
547 // Create a source buffer for our predefines string, so
548 // that we can build a diagnostic that points into that
549 // source buffer.
550 FileID BufferID;
551 if (Predef && Predef[0]) {
552 llvm::MemoryBuffer *Buffer
553 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
554 "<built-in>");
555 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
556 }
557
558 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
559 std::pair<const char *, const char *> Locations
560 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
561
562 if (Locations.first != Predef + MinLen) {
563 // We found the location in the two buffers where there is a
564 // difference. Form source locations to point there (in both
565 // buffers).
566 unsigned Offset = Locations.first - Predef;
567 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
568 .getFileLocWithOffset(Offset);
569 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
570 .getFileLocWithOffset(Offset);
571 } else if (PredefLen > PCHPredefLen) {
572 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
573 .getFileLocWithOffset(MinLen);
574 } else {
575 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
576 .getFileLocWithOffset(MinLen);
577 }
578
579 Diag(Loc1, diag::warn_pch_preprocessor);
580 if (Loc2.isValid())
581 Diag(Loc2, diag::note_predef_in_pch);
582 Diag(diag::note_ignoring_pch) << FileName;
583 return true;
584}
585
Douglas Gregor635f97f2009-04-13 16:31:14 +0000586/// \brief Read the line table in the source manager block.
587/// \returns true if ther was an error.
588static bool ParseLineTable(SourceManager &SourceMgr,
589 llvm::SmallVectorImpl<uint64_t> &Record) {
590 unsigned Idx = 0;
591 LineTableInfo &LineTable = SourceMgr.getLineTable();
592
593 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000594 std::map<int, int> FileIDs;
595 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000596 // Extract the file name
597 unsigned FilenameLen = Record[Idx++];
598 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
599 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +0000600 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
601 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +0000602 }
603
604 // Parse the line entries
605 std::vector<LineEntry> Entries;
606 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +0000607 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +0000608
609 // Extract the line entries
610 unsigned NumEntries = Record[Idx++];
611 Entries.clear();
612 Entries.reserve(NumEntries);
613 for (unsigned I = 0; I != NumEntries; ++I) {
614 unsigned FileOffset = Record[Idx++];
615 unsigned LineNo = Record[Idx++];
616 int FilenameID = Record[Idx++];
617 SrcMgr::CharacteristicKind FileKind
618 = (SrcMgr::CharacteristicKind)Record[Idx++];
619 unsigned IncludeOffset = Record[Idx++];
620 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
621 FileKind, IncludeOffset));
622 }
623 LineTable.AddEntry(FID, Entries);
624 }
625
626 return false;
627}
628
Douglas Gregorab1cef72009-04-10 03:52:48 +0000629/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000630PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +0000631 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000632 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
633 Error("Malformed source manager block record");
634 return Failure;
635 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000636
637 SourceManager &SourceMgr = Context.getSourceManager();
638 RecordData Record;
639 while (true) {
640 unsigned Code = Stream.ReadCode();
641 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000642 if (Stream.ReadBlockEnd()) {
643 Error("Error at end of Source Manager block");
644 return Failure;
645 }
646
647 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000648 }
649
650 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
651 // No known subblocks, always skip them.
652 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000653 if (Stream.SkipBlock()) {
654 Error("Malformed block record");
655 return Failure;
656 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000657 continue;
658 }
659
660 if (Code == llvm::bitc::DEFINE_ABBREV) {
661 Stream.ReadAbbrevRecord();
662 continue;
663 }
664
665 // Read a record.
666 const char *BlobStart;
667 unsigned BlobLen;
668 Record.clear();
669 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
670 default: // Default behavior: ignore.
671 break;
672
673 case pch::SM_SLOC_FILE_ENTRY: {
674 // FIXME: We would really like to delay the creation of this
675 // FileEntry until it is actually required, e.g., when producing
676 // a diagnostic with a source location in this file.
677 const FileEntry *File
678 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
679 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +0000680 FileID ID = SourceMgr.createFileID(File,
681 SourceLocation::getFromRawEncoding(Record[1]),
682 (CharacteristicKind)Record[2]);
683 if (Record[3])
684 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
685 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +0000686 break;
687 }
688
689 case pch::SM_SLOC_BUFFER_ENTRY: {
690 const char *Name = BlobStart;
691 unsigned Code = Stream.ReadCode();
692 Record.clear();
693 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
694 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000695 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000696 llvm::MemoryBuffer *Buffer
697 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
698 BlobStart + BlobLen - 1,
699 Name);
700 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
701
702 if (strcmp(Name, "<built-in>") == 0
703 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
704 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000705 break;
706 }
707
708 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
709 SourceLocation SpellingLoc
710 = SourceLocation::getFromRawEncoding(Record[1]);
711 SourceMgr.createInstantiationLoc(
712 SpellingLoc,
713 SourceLocation::getFromRawEncoding(Record[2]),
714 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +0000715 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +0000716 break;
717 }
718
Chris Lattnere1be6022009-04-14 23:22:57 +0000719 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +0000720 if (ParseLineTable(SourceMgr, Record))
721 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +0000722 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +0000723 }
724 }
725}
726
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000727bool PCHReader::ReadPreprocessorBlock() {
728 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
729 return Error("Malformed preprocessor block record");
730
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000731 RecordData Record;
732 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
733 MacroInfo *LastMacro = 0;
734
735 while (true) {
736 unsigned Code = Stream.ReadCode();
737 switch (Code) {
738 case llvm::bitc::END_BLOCK:
739 if (Stream.ReadBlockEnd())
740 return Error("Error at end of preprocessor block");
741 return false;
742
743 case llvm::bitc::ENTER_SUBBLOCK:
744 // No known subblocks, always skip them.
745 Stream.ReadSubBlockID();
746 if (Stream.SkipBlock())
747 return Error("Malformed block record");
748 continue;
749
750 case llvm::bitc::DEFINE_ABBREV:
751 Stream.ReadAbbrevRecord();
752 continue;
753 default: break;
754 }
755
756 // Read a record.
757 Record.clear();
758 pch::PreprocessorRecordTypes RecType =
759 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
760 switch (RecType) {
761 default: // Default behavior: ignore unknown records.
762 break;
Chris Lattner4b21c202009-04-13 01:29:17 +0000763 case pch::PP_COUNTER_VALUE:
764 if (!Record.empty())
765 PP.setCounterValue(Record[0]);
766 break;
767
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000768 case pch::PP_MACRO_OBJECT_LIKE:
769 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +0000770 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
771 if (II == 0)
772 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000773 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
774 bool isUsed = Record[2];
775
776 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
777 MI->setIsUsed(isUsed);
778
779 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
780 // Decode function-like macro info.
781 bool isC99VarArgs = Record[3];
782 bool isGNUVarArgs = Record[4];
783 MacroArgs.clear();
784 unsigned NumArgs = Record[5];
785 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +0000786 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000787
788 // Install function-like macro info.
789 MI->setIsFunctionLike();
790 if (isC99VarArgs) MI->setIsC99Varargs();
791 if (isGNUVarArgs) MI->setIsGNUVarargs();
792 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
793 PP.getPreprocessorAllocator());
794 }
795
796 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000797 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000798
799 // Remember that we saw this macro last so that we add the tokens that
800 // form its body to it.
801 LastMacro = MI;
802 break;
803 }
804
805 case pch::PP_TOKEN: {
806 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
807 // pretend we didn't see this.
808 if (LastMacro == 0) break;
809
810 Token Tok;
811 Tok.startToken();
812 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
813 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +0000814 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
815 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +0000816 Tok.setKind((tok::TokenKind)Record[3]);
817 Tok.setFlag((Token::TokenFlags)Record[4]);
818 LastMacro->AddTokenToBody(Tok);
819 break;
820 }
821 }
822 }
823}
824
Douglas Gregor179cfb12009-04-10 20:39:37 +0000825PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
826 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
827 Error("Malformed block record");
828 return Failure;
829 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000830
Chris Lattner29241862009-04-11 21:15:38 +0000831 uint64_t PreprocessorBlockBit = 0;
832
Douglas Gregorc34897d2009-04-09 22:27:44 +0000833 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +0000834 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000835 while (!Stream.AtEndOfStream()) {
836 unsigned Code = Stream.ReadCode();
837 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +0000838 // If we saw the preprocessor block, read it now.
839 if (PreprocessorBlockBit) {
840 uint64_t SavedPos = Stream.GetCurrentBitNo();
841 Stream.JumpToBit(PreprocessorBlockBit);
842 if (ReadPreprocessorBlock()) {
843 Error("Malformed preprocessor block");
844 return Failure;
845 }
846 Stream.JumpToBit(SavedPos);
847 }
848
Douglas Gregor179cfb12009-04-10 20:39:37 +0000849 if (Stream.ReadBlockEnd()) {
850 Error("Error at end of module block");
851 return Failure;
852 }
Chris Lattner29241862009-04-11 21:15:38 +0000853
Douglas Gregor179cfb12009-04-10 20:39:37 +0000854 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000855 }
856
857 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
858 switch (Stream.ReadSubBlockID()) {
859 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
860 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
861 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +0000862 if (Stream.SkipBlock()) {
863 Error("Malformed block record");
864 return Failure;
865 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000866 break;
867
Chris Lattner29241862009-04-11 21:15:38 +0000868 case pch::PREPROCESSOR_BLOCK_ID:
869 // Skip the preprocessor block for now, but remember where it is. We
870 // want to read it in after the identifier table.
871 if (PreprocessorBlockBit) {
872 Error("Multiple preprocessor blocks found.");
873 return Failure;
874 }
875 PreprocessorBlockBit = Stream.GetCurrentBitNo();
876 if (Stream.SkipBlock()) {
877 Error("Malformed block record");
878 return Failure;
879 }
880 break;
881
Douglas Gregorab1cef72009-04-10 03:52:48 +0000882 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000883 switch (ReadSourceManagerBlock()) {
884 case Success:
885 break;
886
887 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000888 Error("Malformed source manager block");
889 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000890
891 case IgnorePCH:
892 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000893 }
Douglas Gregorab1cef72009-04-10 03:52:48 +0000894 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000895 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000896 continue;
897 }
898
899 if (Code == llvm::bitc::DEFINE_ABBREV) {
900 Stream.ReadAbbrevRecord();
901 continue;
902 }
903
904 // Read and process a record.
905 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +0000906 const char *BlobStart = 0;
907 unsigned BlobLen = 0;
908 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
909 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000910 default: // Default behavior: ignore.
911 break;
912
913 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000914 if (!TypeOffsets.empty()) {
915 Error("Duplicate TYPE_OFFSET record in PCH file");
916 return Failure;
917 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000918 TypeOffsets.swap(Record);
919 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
920 break;
921
922 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +0000923 if (!DeclOffsets.empty()) {
924 Error("Duplicate DECL_OFFSET record in PCH file");
925 return Failure;
926 }
Douglas Gregorac8f2802009-04-10 17:25:41 +0000927 DeclOffsets.swap(Record);
928 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
929 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +0000930
931 case pch::LANGUAGE_OPTIONS:
932 if (ParseLanguageOptions(Record))
933 return IgnorePCH;
934 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +0000935
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000936 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +0000937 std::string TargetTriple(BlobStart, BlobLen);
938 if (TargetTriple != Context.Target.getTargetTriple()) {
939 Diag(diag::warn_pch_target_triple)
940 << TargetTriple << Context.Target.getTargetTriple();
941 Diag(diag::note_ignoring_pch) << FileName;
942 return IgnorePCH;
943 }
944 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000945 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000946
947 case pch::IDENTIFIER_TABLE:
948 IdentifierTable = BlobStart;
949 break;
950
951 case pch::IDENTIFIER_OFFSET:
952 if (!IdentifierData.empty()) {
953 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
954 return Failure;
955 }
956 IdentifierData.swap(Record);
957#ifndef NDEBUG
958 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
959 if ((IdentifierData[I] & 0x01) == 0) {
960 Error("Malformed identifier table in the precompiled header");
961 return Failure;
962 }
963 }
964#endif
965 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +0000966
967 case pch::EXTERNAL_DEFINITIONS:
968 if (!ExternalDefinitions.empty()) {
969 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
970 return Failure;
971 }
972 ExternalDefinitions.swap(Record);
973 break;
Douglas Gregor7a224cf2009-04-11 00:14:32 +0000974 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000975 }
976
Douglas Gregor179cfb12009-04-10 20:39:37 +0000977 Error("Premature end of bitstream");
978 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000979}
980
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000981PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +0000982 // Set the PCH file name.
983 this->FileName = FileName;
984
Douglas Gregorc34897d2009-04-09 22:27:44 +0000985 // Open the PCH file.
986 std::string ErrStr;
987 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000988 if (!Buffer) {
989 Error(ErrStr.c_str());
990 return IgnorePCH;
991 }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000992
993 // Initialize the stream
994 Stream.init((const unsigned char *)Buffer->getBufferStart(),
995 (const unsigned char *)Buffer->getBufferEnd());
996
997 // Sniff for the signature.
998 if (Stream.Read(8) != 'C' ||
999 Stream.Read(8) != 'P' ||
1000 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001001 Stream.Read(8) != 'H') {
1002 Error("Not a PCH file");
1003 return IgnorePCH;
1004 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001005
1006 // We expect a number of well-defined blocks, though we don't necessarily
1007 // need to understand them all.
1008 while (!Stream.AtEndOfStream()) {
1009 unsigned Code = Stream.ReadCode();
1010
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001011 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1012 Error("Invalid record at top-level");
1013 return Failure;
1014 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001015
1016 unsigned BlockID = Stream.ReadSubBlockID();
1017
1018 // We only know the PCH subblock ID.
1019 switch (BlockID) {
1020 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001021 if (Stream.ReadBlockInfoBlock()) {
1022 Error("Malformed BlockInfoBlock");
1023 return Failure;
1024 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001025 break;
1026 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001027 switch (ReadPCHBlock()) {
1028 case Success:
1029 break;
1030
1031 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001032 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001033
1034 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001035 // FIXME: We could consider reading through to the end of this
1036 // PCH block, skipping subblocks, to see if there are other
1037 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001038 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001039 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001040 break;
1041 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001042 if (Stream.SkipBlock()) {
1043 Error("Malformed block record");
1044 return Failure;
1045 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001046 break;
1047 }
1048 }
1049
1050 // Load the translation unit declaration
1051 ReadDeclRecord(DeclOffsets[0], 0);
1052
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001053 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001054}
1055
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001056namespace {
1057 /// \brief Helper class that saves the current stream position and
1058 /// then restores it when destroyed.
1059 struct VISIBILITY_HIDDEN SavedStreamPosition {
1060 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001061 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001062
1063 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001064 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001065 }
1066
1067 private:
1068 llvm::BitstreamReader &Stream;
1069 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001070 };
1071}
1072
Douglas Gregor179cfb12009-04-10 20:39:37 +00001073/// \brief Parse the record that corresponds to a LangOptions data
1074/// structure.
1075///
1076/// This routine compares the language options used to generate the
1077/// PCH file against the language options set for the current
1078/// compilation. For each option, we classify differences between the
1079/// two compiler states as either "benign" or "important". Benign
1080/// differences don't matter, and we accept them without complaint
1081/// (and without modifying the language options). Differences between
1082/// the states for important options cause the PCH file to be
1083/// unusable, so we emit a warning and return true to indicate that
1084/// there was an error.
1085///
1086/// \returns true if the PCH file is unacceptable, false otherwise.
1087bool PCHReader::ParseLanguageOptions(
1088 const llvm::SmallVectorImpl<uint64_t> &Record) {
1089 const LangOptions &LangOpts = Context.getLangOptions();
1090#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1091#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1092 if (Record[Idx] != LangOpts.Option) { \
1093 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1094 Diag(diag::note_ignoring_pch) << FileName; \
1095 return true; \
1096 } \
1097 ++Idx
1098
1099 unsigned Idx = 0;
1100 PARSE_LANGOPT_BENIGN(Trigraphs);
1101 PARSE_LANGOPT_BENIGN(BCPLComment);
1102 PARSE_LANGOPT_BENIGN(DollarIdents);
1103 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1104 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1105 PARSE_LANGOPT_BENIGN(ImplicitInt);
1106 PARSE_LANGOPT_BENIGN(Digraphs);
1107 PARSE_LANGOPT_BENIGN(HexFloats);
1108 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1109 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1110 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1111 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1112 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1113 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1114 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1115 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1116 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1117 PARSE_LANGOPT_BENIGN(PascalStrings);
1118 PARSE_LANGOPT_BENIGN(Boolean);
1119 PARSE_LANGOPT_BENIGN(WritableStrings);
1120 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1121 diag::warn_pch_lax_vector_conversions);
1122 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1123 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1124 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1125 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1126 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1127 diag::warn_pch_thread_safe_statics);
1128 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1129 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1130 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1131 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1132 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1133 diag::warn_pch_heinous_extensions);
1134 // FIXME: Most of the options below are benign if the macro wasn't
1135 // used. Unfortunately, this means that a PCH compiled without
1136 // optimization can't be used with optimization turned on, even
1137 // though the only thing that changes is whether __OPTIMIZE__ was
1138 // defined... but if __OPTIMIZE__ never showed up in the header, it
1139 // doesn't matter. We could consider making this some special kind
1140 // of check.
1141 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1142 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1143 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1144 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1145 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1146 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1147 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1148 Diag(diag::warn_pch_gc_mode)
1149 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1150 Diag(diag::note_ignoring_pch) << FileName;
1151 return true;
1152 }
1153 ++Idx;
1154 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1155 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1156#undef PARSE_LANGOPT_IRRELEVANT
1157#undef PARSE_LANGOPT_BENIGN
1158
1159 return false;
1160}
1161
Douglas Gregorc34897d2009-04-09 22:27:44 +00001162/// \brief Read and return the type at the given offset.
1163///
1164/// This routine actually reads the record corresponding to the type
1165/// at the given offset in the bitstream. It is a helper routine for
1166/// GetType, which deals with reading type IDs.
1167QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001168 // Keep track of where we are in the stream, then jump back there
1169 // after reading this type.
1170 SavedStreamPosition SavedPosition(Stream);
1171
Douglas Gregorc34897d2009-04-09 22:27:44 +00001172 Stream.JumpToBit(Offset);
1173 RecordData Record;
1174 unsigned Code = Stream.ReadCode();
1175 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001176 case pch::TYPE_ATTR:
1177 assert(false && "Should never jump to an attribute block");
1178 return QualType();
1179
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001180 case pch::TYPE_EXT_QUAL: {
1181 assert(Record.size() == 3 &&
1182 "Incorrect encoding of extended qualifier type");
1183 QualType Base = GetType(Record[0]);
1184 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1185 unsigned AddressSpace = Record[2];
1186
1187 QualType T = Base;
1188 if (GCAttr != QualType::GCNone)
1189 T = Context.getObjCGCQualType(T, GCAttr);
1190 if (AddressSpace)
1191 T = Context.getAddrSpaceQualType(T, AddressSpace);
1192 return T;
1193 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001194
Douglas Gregorc34897d2009-04-09 22:27:44 +00001195 case pch::TYPE_FIXED_WIDTH_INT: {
1196 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1197 return Context.getFixedWidthIntType(Record[0], Record[1]);
1198 }
1199
1200 case pch::TYPE_COMPLEX: {
1201 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1202 QualType ElemType = GetType(Record[0]);
1203 return Context.getComplexType(ElemType);
1204 }
1205
1206 case pch::TYPE_POINTER: {
1207 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1208 QualType PointeeType = GetType(Record[0]);
1209 return Context.getPointerType(PointeeType);
1210 }
1211
1212 case pch::TYPE_BLOCK_POINTER: {
1213 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1214 QualType PointeeType = GetType(Record[0]);
1215 return Context.getBlockPointerType(PointeeType);
1216 }
1217
1218 case pch::TYPE_LVALUE_REFERENCE: {
1219 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1220 QualType PointeeType = GetType(Record[0]);
1221 return Context.getLValueReferenceType(PointeeType);
1222 }
1223
1224 case pch::TYPE_RVALUE_REFERENCE: {
1225 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1226 QualType PointeeType = GetType(Record[0]);
1227 return Context.getRValueReferenceType(PointeeType);
1228 }
1229
1230 case pch::TYPE_MEMBER_POINTER: {
1231 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1232 QualType PointeeType = GetType(Record[0]);
1233 QualType ClassType = GetType(Record[1]);
1234 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1235 }
1236
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001237 case pch::TYPE_CONSTANT_ARRAY: {
1238 QualType ElementType = GetType(Record[0]);
1239 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1240 unsigned IndexTypeQuals = Record[2];
1241 unsigned Idx = 3;
1242 llvm::APInt Size = ReadAPInt(Record, Idx);
1243 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1244 }
1245
1246 case pch::TYPE_INCOMPLETE_ARRAY: {
1247 QualType ElementType = GetType(Record[0]);
1248 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1249 unsigned IndexTypeQuals = Record[2];
1250 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1251 }
1252
1253 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001254 QualType ElementType = GetType(Record[0]);
1255 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1256 unsigned IndexTypeQuals = Record[2];
1257 return Context.getVariableArrayType(ElementType, ReadExpr(),
1258 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001259 }
1260
1261 case pch::TYPE_VECTOR: {
1262 if (Record.size() != 2) {
1263 Error("Incorrect encoding of vector type in PCH file");
1264 return QualType();
1265 }
1266
1267 QualType ElementType = GetType(Record[0]);
1268 unsigned NumElements = Record[1];
1269 return Context.getVectorType(ElementType, NumElements);
1270 }
1271
1272 case pch::TYPE_EXT_VECTOR: {
1273 if (Record.size() != 2) {
1274 Error("Incorrect encoding of extended vector type in PCH file");
1275 return QualType();
1276 }
1277
1278 QualType ElementType = GetType(Record[0]);
1279 unsigned NumElements = Record[1];
1280 return Context.getExtVectorType(ElementType, NumElements);
1281 }
1282
1283 case pch::TYPE_FUNCTION_NO_PROTO: {
1284 if (Record.size() != 1) {
1285 Error("Incorrect encoding of no-proto function type");
1286 return QualType();
1287 }
1288 QualType ResultType = GetType(Record[0]);
1289 return Context.getFunctionNoProtoType(ResultType);
1290 }
1291
1292 case pch::TYPE_FUNCTION_PROTO: {
1293 QualType ResultType = GetType(Record[0]);
1294 unsigned Idx = 1;
1295 unsigned NumParams = Record[Idx++];
1296 llvm::SmallVector<QualType, 16> ParamTypes;
1297 for (unsigned I = 0; I != NumParams; ++I)
1298 ParamTypes.push_back(GetType(Record[Idx++]));
1299 bool isVariadic = Record[Idx++];
1300 unsigned Quals = Record[Idx++];
1301 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1302 isVariadic, Quals);
1303 }
1304
1305 case pch::TYPE_TYPEDEF:
1306 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1307 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1308
1309 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001310 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001311
1312 case pch::TYPE_TYPEOF: {
1313 if (Record.size() != 1) {
1314 Error("Incorrect encoding of typeof(type) in PCH file");
1315 return QualType();
1316 }
1317 QualType UnderlyingType = GetType(Record[0]);
1318 return Context.getTypeOfType(UnderlyingType);
1319 }
1320
1321 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001322 assert(Record.size() == 1 && "Incorrect encoding of record type");
1323 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001324
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001325 case pch::TYPE_ENUM:
1326 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1327 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1328
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001329 case pch::TYPE_OBJC_INTERFACE:
1330 // FIXME: Deserialize ObjCInterfaceType
1331 assert(false && "Cannot de-serialize ObjC interface types yet");
1332 return QualType();
1333
1334 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1335 // FIXME: Deserialize ObjCQualifiedInterfaceType
1336 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1337 return QualType();
1338
1339 case pch::TYPE_OBJC_QUALIFIED_ID:
1340 // FIXME: Deserialize ObjCQualifiedIdType
1341 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1342 return QualType();
1343
1344 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1345 // FIXME: Deserialize ObjCQualifiedClassType
1346 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1347 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001348 }
1349
1350 // Suppress a GCC warning
1351 return QualType();
1352}
1353
1354/// \brief Note that we have loaded the declaration with the given
1355/// Index.
1356///
1357/// This routine notes that this declaration has already been loaded,
1358/// so that future GetDecl calls will return this declaration rather
1359/// than trying to load a new declaration.
1360inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1361 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1362 DeclAlreadyLoaded[Index] = true;
1363 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1364}
1365
1366/// \brief Read the declaration at the given offset from the PCH file.
1367Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001368 // Keep track of where we are in the stream, then jump back there
1369 // after reading this declaration.
1370 SavedStreamPosition SavedPosition(Stream);
1371
Douglas Gregorc34897d2009-04-09 22:27:44 +00001372 Decl *D = 0;
1373 Stream.JumpToBit(Offset);
1374 RecordData Record;
1375 unsigned Code = Stream.ReadCode();
1376 unsigned Idx = 0;
1377 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001378
Douglas Gregorc34897d2009-04-09 22:27:44 +00001379 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001380 case pch::DECL_ATTR:
1381 case pch::DECL_CONTEXT_LEXICAL:
1382 case pch::DECL_CONTEXT_VISIBLE:
1383 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1384 break;
1385
Douglas Gregorc34897d2009-04-09 22:27:44 +00001386 case pch::DECL_TRANSLATION_UNIT:
1387 assert(Index == 0 && "Translation unit must be at index 0");
1388 Reader.VisitTranslationUnitDecl(Context.getTranslationUnitDecl());
1389 D = Context.getTranslationUnitDecl();
1390 LoadedDecl(Index, D);
1391 break;
1392
1393 case pch::DECL_TYPEDEF: {
1394 TypedefDecl *Typedef = TypedefDecl::Create(Context, 0, SourceLocation(),
1395 0, QualType());
1396 LoadedDecl(Index, Typedef);
1397 Reader.VisitTypedefDecl(Typedef);
1398 D = Typedef;
1399 break;
1400 }
1401
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001402 case pch::DECL_ENUM: {
1403 EnumDecl *Enum = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
1404 LoadedDecl(Index, Enum);
1405 Reader.VisitEnumDecl(Enum);
1406 D = Enum;
1407 break;
1408 }
1409
Douglas Gregor982365e2009-04-13 21:20:57 +00001410 case pch::DECL_RECORD: {
1411 RecordDecl *Record = RecordDecl::Create(Context, TagDecl::TK_struct,
1412 0, SourceLocation(), 0, 0);
1413 LoadedDecl(Index, Record);
1414 Reader.VisitRecordDecl(Record);
1415 D = Record;
1416 break;
1417 }
1418
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001419 case pch::DECL_ENUM_CONSTANT: {
1420 EnumConstantDecl *ECD = EnumConstantDecl::Create(Context, 0,
1421 SourceLocation(), 0,
1422 QualType(), 0,
1423 llvm::APSInt());
1424 LoadedDecl(Index, ECD);
1425 Reader.VisitEnumConstantDecl(ECD);
1426 D = ECD;
1427 break;
1428 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001429
1430 case pch::DECL_FUNCTION: {
1431 FunctionDecl *Function = FunctionDecl::Create(Context, 0, SourceLocation(),
1432 DeclarationName(),
1433 QualType());
1434 LoadedDecl(Index, Function);
1435 Reader.VisitFunctionDecl(Function);
1436 D = Function;
1437 break;
1438 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001439
Douglas Gregor982365e2009-04-13 21:20:57 +00001440 case pch::DECL_FIELD: {
1441 FieldDecl *Field = FieldDecl::Create(Context, 0, SourceLocation(), 0,
1442 QualType(), 0, false);
1443 LoadedDecl(Index, Field);
1444 Reader.VisitFieldDecl(Field);
1445 D = Field;
1446 break;
1447 }
1448
Douglas Gregorc34897d2009-04-09 22:27:44 +00001449 case pch::DECL_VAR: {
1450 VarDecl *Var = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1451 VarDecl::None, SourceLocation());
1452 LoadedDecl(Index, Var);
1453 Reader.VisitVarDecl(Var);
1454 D = Var;
1455 break;
1456 }
1457
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001458 case pch::DECL_PARM_VAR: {
1459 ParmVarDecl *Parm = ParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1460 QualType(), VarDecl::None, 0);
1461 LoadedDecl(Index, Parm);
1462 Reader.VisitParmVarDecl(Parm);
1463 D = Parm;
1464 break;
1465 }
1466
1467 case pch::DECL_ORIGINAL_PARM_VAR: {
1468 OriginalParmVarDecl *Parm
1469 = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
1470 QualType(), QualType(), VarDecl::None,
1471 0);
1472 LoadedDecl(Index, Parm);
1473 Reader.VisitOriginalParmVarDecl(Parm);
1474 D = Parm;
1475 break;
1476 }
1477
Douglas Gregor2a491792009-04-13 22:49:25 +00001478 case pch::DECL_FILE_SCOPE_ASM: {
1479 FileScopeAsmDecl *Asm = FileScopeAsmDecl::Create(Context, 0,
1480 SourceLocation(), 0);
1481 LoadedDecl(Index, Asm);
1482 Reader.VisitFileScopeAsmDecl(Asm);
1483 D = Asm;
1484 break;
1485 }
1486
1487 case pch::DECL_BLOCK: {
1488 BlockDecl *Block = BlockDecl::Create(Context, 0, SourceLocation());
1489 LoadedDecl(Index, Block);
1490 Reader.VisitBlockDecl(Block);
1491 D = Block;
1492 break;
1493 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001494 }
1495
1496 // If this declaration is also a declaration context, get the
1497 // offsets for its tables of lexical and visible declarations.
1498 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1499 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1500 if (Offsets.first || Offsets.second) {
1501 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1502 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1503 DeclContextOffsets[DC] = Offsets;
1504 }
1505 }
1506 assert(Idx == Record.size());
1507
1508 return D;
1509}
1510
Douglas Gregorac8f2802009-04-10 17:25:41 +00001511QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001512 unsigned Quals = ID & 0x07;
1513 unsigned Index = ID >> 3;
1514
1515 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1516 QualType T;
1517 switch ((pch::PredefinedTypeIDs)Index) {
1518 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1519 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1520 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1521
1522 case pch::PREDEF_TYPE_CHAR_U_ID:
1523 case pch::PREDEF_TYPE_CHAR_S_ID:
1524 // FIXME: Check that the signedness of CharTy is correct!
1525 T = Context.CharTy;
1526 break;
1527
1528 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1529 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1530 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1531 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1532 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1533 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1534 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1535 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1536 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1537 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1538 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1539 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1540 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1541 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1542 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1543 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1544 }
1545
1546 assert(!T.isNull() && "Unknown predefined type");
1547 return T.getQualifiedType(Quals);
1548 }
1549
1550 Index -= pch::NUM_PREDEF_TYPE_IDS;
1551 if (!TypeAlreadyLoaded[Index]) {
1552 // Load the type from the PCH file.
1553 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1554 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1555 TypeAlreadyLoaded[Index] = true;
1556 }
1557
1558 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1559}
1560
Douglas Gregorac8f2802009-04-10 17:25:41 +00001561Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001562 if (ID == 0)
1563 return 0;
1564
1565 unsigned Index = ID - 1;
1566 if (DeclAlreadyLoaded[Index])
1567 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1568
1569 // Load the declaration from the PCH file.
1570 return ReadDeclRecord(DeclOffsets[Index], Index);
1571}
1572
1573bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001574 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001575 assert(DC->hasExternalLexicalStorage() &&
1576 "DeclContext has no lexical decls in storage");
1577 uint64_t Offset = DeclContextOffsets[DC].first;
1578 assert(Offset && "DeclContext has no lexical decls in storage");
1579
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001580 // Keep track of where we are in the stream, then jump back there
1581 // after reading this context.
1582 SavedStreamPosition SavedPosition(Stream);
1583
Douglas Gregorc34897d2009-04-09 22:27:44 +00001584 // Load the record containing all of the declarations lexically in
1585 // this context.
1586 Stream.JumpToBit(Offset);
1587 RecordData Record;
1588 unsigned Code = Stream.ReadCode();
1589 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001590 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001591 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1592
1593 // Load all of the declaration IDs
1594 Decls.clear();
1595 Decls.insert(Decls.end(), Record.begin(), Record.end());
1596 return false;
1597}
1598
1599bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
1600 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
1601 assert(DC->hasExternalVisibleStorage() &&
1602 "DeclContext has no visible decls in storage");
1603 uint64_t Offset = DeclContextOffsets[DC].second;
1604 assert(Offset && "DeclContext has no visible decls in storage");
1605
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001606 // Keep track of where we are in the stream, then jump back there
1607 // after reading this context.
1608 SavedStreamPosition SavedPosition(Stream);
1609
Douglas Gregorc34897d2009-04-09 22:27:44 +00001610 // Load the record containing all of the declarations visible in
1611 // this context.
1612 Stream.JumpToBit(Offset);
1613 RecordData Record;
1614 unsigned Code = Stream.ReadCode();
1615 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001616 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001617 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
1618 if (Record.size() == 0)
1619 return false;
1620
1621 Decls.clear();
1622
1623 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001624 while (Idx < Record.size()) {
1625 Decls.push_back(VisibleDeclaration());
1626 Decls.back().Name = ReadDeclarationName(Record, Idx);
1627
Douglas Gregorc34897d2009-04-09 22:27:44 +00001628 unsigned Size = Record[Idx++];
1629 llvm::SmallVector<unsigned, 4> & LoadedDecls
1630 = Decls.back().Declarations;
1631 LoadedDecls.reserve(Size);
1632 for (unsigned I = 0; I < Size; ++I)
1633 LoadedDecls.push_back(Record[Idx++]);
1634 }
1635
1636 return false;
1637}
1638
Douglas Gregor631f6c62009-04-14 00:24:19 +00001639void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
1640 if (!Consumer)
1641 return;
1642
1643 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
1644 Decl *D = GetDecl(ExternalDefinitions[I]);
1645 DeclGroupRef DG(D);
1646 Consumer->HandleTopLevelDecl(DG);
1647 }
1648}
1649
Douglas Gregorc34897d2009-04-09 22:27:44 +00001650void PCHReader::PrintStats() {
1651 std::fprintf(stderr, "*** PCH Statistics:\n");
1652
1653 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
1654 TypeAlreadyLoaded.end(),
1655 true);
1656 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
1657 DeclAlreadyLoaded.end(),
1658 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00001659 unsigned NumIdentifiersLoaded = 0;
1660 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
1661 if ((IdentifierData[I] & 0x01) == 0)
1662 ++NumIdentifiersLoaded;
1663 }
1664
Douglas Gregorc34897d2009-04-09 22:27:44 +00001665 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
1666 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001667 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001668 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
1669 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00001670 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
1671 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
1672 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
1673 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00001674 std::fprintf(stderr, "\n");
1675}
1676
Chris Lattner29241862009-04-11 21:15:38 +00001677IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001678 if (ID == 0)
1679 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00001680
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001681 if (!IdentifierTable || IdentifierData.empty()) {
1682 Error("No identifier table in PCH file");
1683 return 0;
1684 }
Chris Lattner29241862009-04-11 21:15:38 +00001685
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001686 if (IdentifierData[ID - 1] & 0x01) {
1687 uint64_t Offset = IdentifierData[ID - 1];
1688 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00001689 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001690 }
Chris Lattner29241862009-04-11 21:15:38 +00001691
1692 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001693}
1694
1695DeclarationName
1696PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
1697 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
1698 switch (Kind) {
1699 case DeclarationName::Identifier:
1700 return DeclarationName(GetIdentifierInfo(Record, Idx));
1701
1702 case DeclarationName::ObjCZeroArgSelector:
1703 case DeclarationName::ObjCOneArgSelector:
1704 case DeclarationName::ObjCMultiArgSelector:
1705 assert(false && "Unable to de-serialize Objective-C selectors");
1706 break;
1707
1708 case DeclarationName::CXXConstructorName:
1709 return Context.DeclarationNames.getCXXConstructorName(
1710 GetType(Record[Idx++]));
1711
1712 case DeclarationName::CXXDestructorName:
1713 return Context.DeclarationNames.getCXXDestructorName(
1714 GetType(Record[Idx++]));
1715
1716 case DeclarationName::CXXConversionFunctionName:
1717 return Context.DeclarationNames.getCXXConversionFunctionName(
1718 GetType(Record[Idx++]));
1719
1720 case DeclarationName::CXXOperatorName:
1721 return Context.DeclarationNames.getCXXOperatorName(
1722 (OverloadedOperatorKind)Record[Idx++]);
1723
1724 case DeclarationName::CXXUsingDirective:
1725 return DeclarationName::getUsingDirectiveName();
1726 }
1727
1728 // Required to silence GCC warning
1729 return DeclarationName();
1730}
Douglas Gregor179cfb12009-04-10 20:39:37 +00001731
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001732/// \brief Read an integral value
1733llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
1734 unsigned BitWidth = Record[Idx++];
1735 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
1736 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
1737 Idx += NumWords;
1738 return Result;
1739}
1740
1741/// \brief Read a signed integral value
1742llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
1743 bool isUnsigned = Record[Idx++];
1744 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
1745}
1746
Douglas Gregore2f37202009-04-14 21:55:33 +00001747/// \brief Read a floating-point value
1748llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00001749 return llvm::APFloat(ReadAPInt(Record, Idx));
1750}
1751
Douglas Gregor1c507882009-04-15 21:30:51 +00001752// \brief Read a string
1753std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
1754 unsigned Len = Record[Idx++];
1755 std::string Result(&Record[Idx], &Record[Idx] + Len);
1756 Idx += Len;
1757 return Result;
1758}
1759
1760/// \brief Reads attributes from the current stream position.
1761Attr *PCHReader::ReadAttributes() {
1762 unsigned Code = Stream.ReadCode();
1763 assert(Code == llvm::bitc::UNABBREV_RECORD &&
1764 "Expected unabbreviated record"); (void)Code;
1765
1766 RecordData Record;
1767 unsigned Idx = 0;
1768 unsigned RecCode = Stream.ReadRecord(Code, Record);
1769 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
1770 (void)RecCode;
1771
1772#define SIMPLE_ATTR(Name) \
1773 case Attr::Name: \
1774 New = ::new (Context) Name##Attr(); \
1775 break
1776
1777#define STRING_ATTR(Name) \
1778 case Attr::Name: \
1779 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
1780 break
1781
1782#define UNSIGNED_ATTR(Name) \
1783 case Attr::Name: \
1784 New = ::new (Context) Name##Attr(Record[Idx++]); \
1785 break
1786
1787 Attr *Attrs = 0;
1788 while (Idx < Record.size()) {
1789 Attr *New = 0;
1790 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
1791 bool IsInherited = Record[Idx++];
1792
1793 switch (Kind) {
1794 STRING_ATTR(Alias);
1795 UNSIGNED_ATTR(Aligned);
1796 SIMPLE_ATTR(AlwaysInline);
1797 SIMPLE_ATTR(AnalyzerNoReturn);
1798 STRING_ATTR(Annotate);
1799 STRING_ATTR(AsmLabel);
1800
1801 case Attr::Blocks:
1802 New = ::new (Context) BlocksAttr(
1803 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1804 break;
1805
1806 case Attr::Cleanup:
1807 New = ::new (Context) CleanupAttr(
1808 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1809 break;
1810
1811 SIMPLE_ATTR(Const);
1812 UNSIGNED_ATTR(Constructor);
1813 SIMPLE_ATTR(DLLExport);
1814 SIMPLE_ATTR(DLLImport);
1815 SIMPLE_ATTR(Deprecated);
1816 UNSIGNED_ATTR(Destructor);
1817 SIMPLE_ATTR(FastCall);
1818
1819 case Attr::Format: {
1820 std::string Type = ReadString(Record, Idx);
1821 unsigned FormatIdx = Record[Idx++];
1822 unsigned FirstArg = Record[Idx++];
1823 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
1824 break;
1825 }
1826
1827 SIMPLE_ATTR(GNUCInline);
1828
1829 case Attr::IBOutletKind:
1830 New = ::new (Context) IBOutletAttr();
1831 break;
1832
1833 SIMPLE_ATTR(NoReturn);
1834 SIMPLE_ATTR(NoThrow);
1835 SIMPLE_ATTR(Nodebug);
1836 SIMPLE_ATTR(Noinline);
1837
1838 case Attr::NonNull: {
1839 unsigned Size = Record[Idx++];
1840 llvm::SmallVector<unsigned, 16> ArgNums;
1841 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1842 Idx += Size;
1843 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
1844 break;
1845 }
1846
1847 SIMPLE_ATTR(ObjCException);
1848 SIMPLE_ATTR(ObjCNSObject);
1849 SIMPLE_ATTR(Overloadable);
1850 UNSIGNED_ATTR(Packed);
1851 SIMPLE_ATTR(Pure);
1852 UNSIGNED_ATTR(Regparm);
1853 STRING_ATTR(Section);
1854 SIMPLE_ATTR(StdCall);
1855 SIMPLE_ATTR(TransparentUnion);
1856 SIMPLE_ATTR(Unavailable);
1857 SIMPLE_ATTR(Unused);
1858 SIMPLE_ATTR(Used);
1859
1860 case Attr::Visibility:
1861 New = ::new (Context) VisibilityAttr(
1862 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1863 break;
1864
1865 SIMPLE_ATTR(WarnUnusedResult);
1866 SIMPLE_ATTR(Weak);
1867 SIMPLE_ATTR(WeakImport);
1868 }
1869
1870 assert(New && "Unable to decode attribute?");
1871 New->setInherited(IsInherited);
1872 New->setNext(Attrs);
1873 Attrs = New;
1874 }
1875#undef UNSIGNED_ATTR
1876#undef STRING_ATTR
1877#undef SIMPLE_ATTR
1878
1879 // The list of attributes was built backwards. Reverse the list
1880 // before returning it.
1881 Attr *PrevAttr = 0, *NextAttr = 0;
1882 while (Attrs) {
1883 NextAttr = Attrs->getNext();
1884 Attrs->setNext(PrevAttr);
1885 PrevAttr = Attrs;
1886 Attrs = NextAttr;
1887 }
1888
1889 return PrevAttr;
1890}
1891
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001892Expr *PCHReader::ReadExpr() {
Douglas Gregora151ba42009-04-14 23:32:43 +00001893 // Within the bitstream, expressions are stored in Reverse Polish
1894 // Notation, with each of the subexpressions preceding the
1895 // expression they are stored in. To evaluate expressions, we
1896 // continue reading expressions and placing them on the stack, with
1897 // expressions having operands removing those operands from the
1898 // stack. Evaluation terminates when we see a EXPR_STOP record, and
1899 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001900 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00001901 unsigned Idx;
1902 llvm::SmallVector<Expr *, 16> ExprStack;
1903 PCHStmtReader Reader(*this, Record, Idx, ExprStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001904 Stmt::EmptyShell Empty;
1905
Douglas Gregora151ba42009-04-14 23:32:43 +00001906 while (true) {
1907 unsigned Code = Stream.ReadCode();
1908 if (Code == llvm::bitc::END_BLOCK) {
1909 if (Stream.ReadBlockEnd()) {
1910 Error("Error at end of Source Manager block");
1911 return 0;
1912 }
1913 break;
1914 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001915
Douglas Gregora151ba42009-04-14 23:32:43 +00001916 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1917 // No known subblocks, always skip them.
1918 Stream.ReadSubBlockID();
1919 if (Stream.SkipBlock()) {
1920 Error("Malformed block record");
1921 return 0;
1922 }
1923 continue;
1924 }
Douglas Gregore2f37202009-04-14 21:55:33 +00001925
Douglas Gregora151ba42009-04-14 23:32:43 +00001926 if (Code == llvm::bitc::DEFINE_ABBREV) {
1927 Stream.ReadAbbrevRecord();
1928 continue;
1929 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001930
Douglas Gregora151ba42009-04-14 23:32:43 +00001931 Expr *E = 0;
1932 Idx = 0;
1933 Record.clear();
1934 bool Finished = false;
1935 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
1936 case pch::EXPR_STOP:
1937 Finished = true;
1938 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001939
Douglas Gregora151ba42009-04-14 23:32:43 +00001940 case pch::EXPR_NULL:
1941 E = 0;
1942 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001943
Douglas Gregora151ba42009-04-14 23:32:43 +00001944 case pch::EXPR_PREDEFINED:
1945 // FIXME: untested (until we can serialize function bodies).
1946 E = new (Context) PredefinedExpr(Empty);
1947 break;
1948
1949 case pch::EXPR_DECL_REF:
1950 E = new (Context) DeclRefExpr(Empty);
1951 break;
1952
1953 case pch::EXPR_INTEGER_LITERAL:
1954 E = new (Context) IntegerLiteral(Empty);
1955 break;
1956
1957 case pch::EXPR_FLOATING_LITERAL:
1958 E = new (Context) FloatingLiteral(Empty);
1959 break;
1960
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00001961 case pch::EXPR_IMAGINARY_LITERAL:
1962 E = new (Context) ImaginaryLiteral(Empty);
1963 break;
1964
Douglas Gregor596e0932009-04-15 16:35:07 +00001965 case pch::EXPR_STRING_LITERAL:
1966 E = StringLiteral::CreateEmpty(Context,
1967 Record[PCHStmtReader::NumExprFields + 1]);
1968 break;
1969
Douglas Gregora151ba42009-04-14 23:32:43 +00001970 case pch::EXPR_CHARACTER_LITERAL:
1971 E = new (Context) CharacterLiteral(Empty);
1972 break;
1973
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00001974 case pch::EXPR_PAREN:
1975 E = new (Context) ParenExpr(Empty);
1976 break;
1977
Douglas Gregor12d74052009-04-15 15:58:59 +00001978 case pch::EXPR_UNARY_OPERATOR:
1979 E = new (Context) UnaryOperator(Empty);
1980 break;
1981
1982 case pch::EXPR_SIZEOF_ALIGN_OF:
1983 E = new (Context) SizeOfAlignOfExpr(Empty);
1984 break;
1985
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00001986 case pch::EXPR_ARRAY_SUBSCRIPT:
1987 E = new (Context) ArraySubscriptExpr(Empty);
1988 break;
1989
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00001990 case pch::EXPR_CALL:
1991 E = new (Context) CallExpr(Context, Empty);
1992 break;
1993
1994 case pch::EXPR_MEMBER:
1995 E = new (Context) MemberExpr(Empty);
1996 break;
1997
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00001998 case pch::EXPR_BINARY_OPERATOR:
1999 E = new (Context) BinaryOperator(Empty);
2000 break;
2001
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002002 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
2003 E = new (Context) CompoundAssignOperator(Empty);
2004 break;
2005
2006 case pch::EXPR_CONDITIONAL_OPERATOR:
2007 E = new (Context) ConditionalOperator(Empty);
2008 break;
2009
Douglas Gregora151ba42009-04-14 23:32:43 +00002010 case pch::EXPR_IMPLICIT_CAST:
2011 E = new (Context) ImplicitCastExpr(Empty);
2012 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002013
2014 case pch::EXPR_CSTYLE_CAST:
2015 E = new (Context) CStyleCastExpr(Empty);
2016 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002017
2018 case pch::EXPR_EXT_VECTOR_ELEMENT:
2019 E = new (Context) ExtVectorElementExpr(Empty);
2020 break;
2021
2022 case pch::EXPR_VA_ARG:
2023 // FIXME: untested; we need function bodies first
2024 E = new (Context) VAArgExpr(Empty);
2025 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002026
2027 case pch::EXPR_TYPES_COMPATIBLE:
2028 E = new (Context) TypesCompatibleExpr(Empty);
2029 break;
2030
2031 case pch::EXPR_CHOOSE:
2032 E = new (Context) ChooseExpr(Empty);
2033 break;
2034
2035 case pch::EXPR_GNU_NULL:
2036 E = new (Context) GNUNullExpr(Empty);
2037 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002038
2039 case pch::EXPR_SHUFFLE_VECTOR:
2040 E = new (Context) ShuffleVectorExpr(Empty);
2041 break;
2042
2043 case pch::EXPR_BLOCK_DECL_REF:
2044 // FIXME: untested until we have statement and block support
2045 E = new (Context) BlockDeclRefExpr(Empty);
2046 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002047 }
2048
2049 // We hit an EXPR_STOP, so we're done with this expression.
2050 if (Finished)
2051 break;
2052
2053 if (E) {
2054 unsigned NumSubExprs = Reader.Visit(E);
2055 while (NumSubExprs > 0) {
2056 ExprStack.pop_back();
2057 --NumSubExprs;
2058 }
2059 }
2060
2061 assert(Idx == Record.size() && "Invalid deserialization of expression");
2062 ExprStack.push_back(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002063 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002064 assert(ExprStack.size() == 1 && "Extra expressions on stack!");
2065 return ExprStack.back();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002066}
2067
Douglas Gregor179cfb12009-04-10 20:39:37 +00002068DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002069 return Diag(SourceLocation(), DiagID);
2070}
2071
2072DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2073 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002074 Context.getSourceManager()),
2075 DiagID);
2076}