blob: f1cb4d3cdce1a27ff41367076eb40a50a89fd98e [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 Gregorddf4d092009-04-16 22:29:51 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattnerdb1c81b2009-04-10 21:41:48 +000023#include "clang/Lex/MacroInfo.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Lex/Preprocessor.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000029#include "llvm/Bitcode/BitstreamReader.h"
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include <algorithm>
33#include <cstdio>
34
35using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Declaration deserialization
39//===----------------------------------------------------------------------===//
40namespace {
Douglas Gregorddf4d092009-04-16 22:29:51 +000041 class VISIBILITY_HIDDEN PCHDeclReader
42 : public DeclVisitor<PCHDeclReader, void> {
Douglas Gregorc34897d2009-04-09 22:27:44 +000043 PCHReader &Reader;
44 const PCHReader::RecordData &Record;
45 unsigned &Idx;
46
47 public:
48 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
49 unsigned &Idx)
50 : Reader(Reader), Record(Record), Idx(Idx) { }
51
52 void VisitDecl(Decl *D);
53 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
54 void VisitNamedDecl(NamedDecl *ND);
55 void VisitTypeDecl(TypeDecl *TD);
56 void VisitTypedefDecl(TypedefDecl *TD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000057 void VisitTagDecl(TagDecl *TD);
58 void VisitEnumDecl(EnumDecl *ED);
Douglas Gregor982365e2009-04-13 21:20:57 +000059 void VisitRecordDecl(RecordDecl *RD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000060 void VisitValueDecl(ValueDecl *VD);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +000061 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000062 void VisitFunctionDecl(FunctionDecl *FD);
Douglas Gregor982365e2009-04-13 21:20:57 +000063 void VisitFieldDecl(FieldDecl *FD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000064 void VisitVarDecl(VarDecl *VD);
Douglas Gregor23ce3a52009-04-13 22:18:37 +000065 void VisitParmVarDecl(ParmVarDecl *PD);
66 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
Douglas Gregor2a491792009-04-13 22:49:25 +000067 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
68 void VisitBlockDecl(BlockDecl *BD);
Douglas Gregorc34897d2009-04-09 22:27:44 +000069 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Steve Naroff79ea0e02009-04-20 15:06:07 +000070 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +000071 void VisitObjCContainerDecl(ObjCContainerDecl *D);
72 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
73 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +000074 };
75}
76
77void PCHDeclReader::VisitDecl(Decl *D) {
78 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
79 D->setLexicalDeclContext(
80 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
81 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
82 D->setInvalidDecl(Record[Idx++]);
Douglas Gregor1c507882009-04-15 21:30:51 +000083 if (Record[Idx++])
84 D->addAttr(Reader.ReadAttributes());
Douglas Gregorc34897d2009-04-09 22:27:44 +000085 D->setImplicit(Record[Idx++]);
86 D->setAccess((AccessSpecifier)Record[Idx++]);
87}
88
89void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
90 VisitDecl(TU);
91}
92
93void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
94 VisitDecl(ND);
95 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
96}
97
98void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
99 VisitNamedDecl(TD);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000100 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
101}
102
103void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Douglas Gregor88fd09d2009-04-13 20:46:52 +0000104 // Note that we cannot use VisitTypeDecl here, because we need to
105 // set the underlying type of the typedef *before* we try to read
106 // the type associated with the TypedefDecl.
107 VisitNamedDecl(TD);
108 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
109 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
110 Idx += 2;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000111}
112
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000113void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
114 VisitTypeDecl(TD);
115 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
116 TD->setDefinition(Record[Idx++]);
117 TD->setTypedefForAnonDecl(
118 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
119}
120
121void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
122 VisitTagDecl(ED);
123 ED->setIntegerType(Reader.GetType(Record[Idx++]));
124}
125
Douglas Gregor982365e2009-04-13 21:20:57 +0000126void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
127 VisitTagDecl(RD);
128 RD->setHasFlexibleArrayMember(Record[Idx++]);
129 RD->setAnonymousStructOrUnion(Record[Idx++]);
130}
131
Douglas Gregorc34897d2009-04-09 22:27:44 +0000132void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
133 VisitNamedDecl(VD);
134 VD->setType(Reader.GetType(Record[Idx++]));
135}
136
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000137void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
138 VisitValueDecl(ECD);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000139 if (Record[Idx++])
140 ECD->setInitExpr(Reader.ReadExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000141 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
142}
143
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000144void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
145 VisitValueDecl(FD);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000146 if (Record[Idx++])
Douglas Gregor3b9a7c82009-04-18 00:07:54 +0000147 FD->setLazyBody(Reader.getStream().GetCurrentBitNo());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000148 FD->setPreviousDeclaration(
149 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
150 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
151 FD->setInline(Record[Idx++]);
152 FD->setVirtual(Record[Idx++]);
153 FD->setPure(Record[Idx++]);
154 FD->setInheritedPrototype(Record[Idx++]);
155 FD->setHasPrototype(Record[Idx++]);
156 FD->setDeleted(Record[Idx++]);
157 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
158 unsigned NumParams = Record[Idx++];
159 llvm::SmallVector<ParmVarDecl *, 16> Params;
160 Params.reserve(NumParams);
161 for (unsigned I = 0; I != NumParams; ++I)
162 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
163 FD->setParams(Reader.getContext(), &Params[0], NumParams);
164}
165
Steve Naroff79ea0e02009-04-20 15:06:07 +0000166void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
167 VisitNamedDecl(MD);
168 if (Record[Idx++]) {
169 // In practice, this won't be executed (since method definitions
170 // don't occur in header files).
171 MD->setBody(cast<CompoundStmt>(Reader.GetStmt(Record[Idx++])));
172 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
173 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
174 }
175 MD->setInstanceMethod(Record[Idx++]);
176 MD->setVariadic(Record[Idx++]);
177 MD->setSynthesized(Record[Idx++]);
178 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
179 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
180 MD->setResultType(Reader.GetType(Record[Idx++]));
181 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
182 unsigned NumParams = Record[Idx++];
183 llvm::SmallVector<ParmVarDecl *, 16> Params;
184 Params.reserve(NumParams);
185 for (unsigned I = 0; I != NumParams; ++I)
186 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
187 MD->setMethodParams(Reader.getContext(), &Params[0], NumParams);
188}
189
Steve Naroff7333b492009-04-20 20:09:33 +0000190void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
191 VisitNamedDecl(CD);
192 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
193}
194
195void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
196 VisitObjCContainerDecl(ID);
197 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
198 ID->setSuperClass(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
199 unsigned NumIvars = Record[Idx++];
200 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
201 IVars.reserve(NumIvars);
202 for (unsigned I = 0; I != NumIvars; ++I)
203 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
204 ID->setIVarList(&IVars[0], NumIvars, Reader.getContext());
205
206 ID->setForwardDecl(Record[Idx++]);
207 ID->setImplicitInterfaceDecl(Record[Idx++]);
208 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
209 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
210 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
211 // FIXME: add protocols, categories.
212}
213
214void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
215 VisitFieldDecl(IVD);
216 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
217}
218
Douglas Gregor982365e2009-04-13 21:20:57 +0000219void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
220 VisitValueDecl(FD);
221 FD->setMutable(Record[Idx++]);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000222 if (Record[Idx++])
223 FD->setBitWidth(Reader.ReadExpr());
Douglas Gregor982365e2009-04-13 21:20:57 +0000224}
225
Douglas Gregorc34897d2009-04-09 22:27:44 +0000226void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
227 VisitValueDecl(VD);
228 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
229 VD->setThreadSpecified(Record[Idx++]);
230 VD->setCXXDirectInitializer(Record[Idx++]);
231 VD->setDeclaredInCondition(Record[Idx++]);
232 VD->setPreviousDeclaration(
233 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
234 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000235 if (Record[Idx++])
236 VD->setInit(Reader.ReadExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000237}
238
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000239void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
240 VisitVarDecl(PD);
241 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000242 // FIXME: default argument (C++ only)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000243}
244
245void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
246 VisitParmVarDecl(PD);
247 PD->setOriginalType(Reader.GetType(Record[Idx++]));
248}
249
Douglas Gregor2a491792009-04-13 22:49:25 +0000250void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
251 VisitDecl(AD);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000252 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000253}
254
255void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
256 VisitDecl(BD);
Douglas Gregore246b742009-04-17 19:21:43 +0000257 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
Douglas Gregor2a491792009-04-13 22:49:25 +0000258 unsigned NumParams = Record[Idx++];
259 llvm::SmallVector<ParmVarDecl *, 16> Params;
260 Params.reserve(NumParams);
261 for (unsigned I = 0; I != NumParams; ++I)
262 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
263 BD->setParams(Reader.getContext(), &Params[0], NumParams);
264}
265
Douglas Gregorc34897d2009-04-09 22:27:44 +0000266std::pair<uint64_t, uint64_t>
267PCHDeclReader::VisitDeclContext(DeclContext *DC) {
268 uint64_t LexicalOffset = Record[Idx++];
269 uint64_t VisibleOffset = 0;
270 if (DC->getPrimaryContext() == DC)
271 VisibleOffset = Record[Idx++];
272 return std::make_pair(LexicalOffset, VisibleOffset);
273}
274
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000275//===----------------------------------------------------------------------===//
276// Statement/expression deserialization
277//===----------------------------------------------------------------------===//
278namespace {
279 class VISIBILITY_HIDDEN PCHStmtReader
Douglas Gregora151ba42009-04-14 23:32:43 +0000280 : public StmtVisitor<PCHStmtReader, unsigned> {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000281 PCHReader &Reader;
282 const PCHReader::RecordData &Record;
283 unsigned &Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000284 llvm::SmallVectorImpl<Stmt *> &StmtStack;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000285
286 public:
287 PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record,
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000288 unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack)
289 : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000290
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000291 /// \brief The number of record fields required for the Stmt class
292 /// itself.
293 static const unsigned NumStmtFields = 0;
294
Douglas Gregor596e0932009-04-15 16:35:07 +0000295 /// \brief The number of record fields required for the Expr class
296 /// itself.
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000297 static const unsigned NumExprFields = NumStmtFields + 3;
Douglas Gregor596e0932009-04-15 16:35:07 +0000298
Douglas Gregora151ba42009-04-14 23:32:43 +0000299 // Each of the Visit* functions reads in part of the expression
300 // from the given record and the current expression stack, then
301 // return the total number of operands that it read from the
302 // expression stack.
303
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000304 unsigned VisitStmt(Stmt *S);
305 unsigned VisitNullStmt(NullStmt *S);
306 unsigned VisitCompoundStmt(CompoundStmt *S);
307 unsigned VisitSwitchCase(SwitchCase *S);
308 unsigned VisitCaseStmt(CaseStmt *S);
309 unsigned VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000310 unsigned VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000311 unsigned VisitIfStmt(IfStmt *S);
312 unsigned VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000313 unsigned VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000314 unsigned VisitDoStmt(DoStmt *S);
315 unsigned VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000316 unsigned VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000317 unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000318 unsigned VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000319 unsigned VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000320 unsigned VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000321 unsigned VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000322 unsigned VisitAsmStmt(AsmStmt *S);
Douglas Gregora151ba42009-04-14 23:32:43 +0000323 unsigned VisitExpr(Expr *E);
324 unsigned VisitPredefinedExpr(PredefinedExpr *E);
325 unsigned VisitDeclRefExpr(DeclRefExpr *E);
326 unsigned VisitIntegerLiteral(IntegerLiteral *E);
327 unsigned VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000328 unsigned VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000329 unsigned VisitStringLiteral(StringLiteral *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000330 unsigned VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000331 unsigned VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000332 unsigned VisitUnaryOperator(UnaryOperator *E);
333 unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000334 unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000335 unsigned VisitCallExpr(CallExpr *E);
336 unsigned VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000337 unsigned VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000338 unsigned VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000339 unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E);
340 unsigned VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000341 unsigned VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000342 unsigned VisitExplicitCastExpr(ExplicitCastExpr *E);
343 unsigned VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000344 unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000345 unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000346 unsigned VisitInitListExpr(InitListExpr *E);
347 unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E);
348 unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000349 unsigned VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000350 unsigned VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000351 unsigned VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000352 unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
353 unsigned VisitChooseExpr(ChooseExpr *E);
354 unsigned VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000355 unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000356 unsigned VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000357 unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000358 };
359}
360
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000361unsigned PCHStmtReader::VisitStmt(Stmt *S) {
362 assert(Idx == NumStmtFields && "Incorrect statement field count");
363 return 0;
364}
365
366unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) {
367 VisitStmt(S);
368 S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369 return 0;
370}
371
372unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) {
373 VisitStmt(S);
374 unsigned NumStmts = Record[Idx++];
375 S->setStmts(Reader.getContext(),
376 &StmtStack[StmtStack.size() - NumStmts], NumStmts);
377 S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
378 S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
379 return NumStmts;
380}
381
382unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) {
383 VisitStmt(S);
384 Reader.RecordSwitchCaseID(S, Record[Idx++]);
385 return 0;
386}
387
388unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) {
389 VisitSwitchCase(S);
390 S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3]));
391 S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
392 S->setSubStmt(StmtStack.back());
393 S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
394 return 3;
395}
396
397unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) {
398 VisitSwitchCase(S);
399 S->setSubStmt(StmtStack.back());
400 S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
401 return 1;
402}
403
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000404unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) {
405 VisitStmt(S);
406 S->setID(Reader.GetIdentifierInfo(Record, Idx));
407 S->setSubStmt(StmtStack.back());
408 S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
409 Reader.RecordLabelStmt(S, Record[Idx++]);
410 return 1;
411}
412
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000413unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) {
414 VisitStmt(S);
415 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
416 S->setThen(StmtStack[StmtStack.size() - 2]);
417 S->setElse(StmtStack[StmtStack.size() - 1]);
418 S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
419 return 3;
420}
421
422unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) {
423 VisitStmt(S);
424 S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2]));
425 S->setBody(StmtStack.back());
426 S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
427 SwitchCase *PrevSC = 0;
428 for (unsigned N = Record.size(); Idx != N; ++Idx) {
429 SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
430 if (PrevSC)
431 PrevSC->setNextSwitchCase(SC);
432 else
433 S->setSwitchCaseList(SC);
434 PrevSC = SC;
435 }
436 return 2;
437}
438
Douglas Gregora6b503f2009-04-17 00:16:09 +0000439unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
440 VisitStmt(S);
441 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
442 S->setBody(StmtStack.back());
443 S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
444 return 2;
445}
446
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000447unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
448 VisitStmt(S);
449 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
450 S->setBody(StmtStack.back());
451 S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
452 return 2;
453}
454
455unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
456 VisitStmt(S);
457 S->setInit(StmtStack[StmtStack.size() - 4]);
458 S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
459 S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
460 S->setBody(StmtStack.back());
461 S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
462 return 4;
463}
464
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000465unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) {
466 VisitStmt(S);
467 Reader.SetLabelOf(S, Record[Idx++]);
468 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
469 S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
470 return 0;
471}
472
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000473unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
474 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000475 S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000476 S->setTarget(cast_or_null<Expr>(StmtStack.back()));
477 return 1;
478}
479
Douglas Gregora6b503f2009-04-17 00:16:09 +0000480unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
481 VisitStmt(S);
482 S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
483 return 0;
484}
485
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000486unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) {
487 VisitStmt(S);
488 S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
489 return 0;
490}
491
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000492unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) {
493 VisitStmt(S);
494 S->setRetValue(cast_or_null<Expr>(StmtStack.back()));
495 S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
496 return 1;
497}
498
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000499unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) {
500 VisitStmt(S);
501 S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
502 S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
503
504 if (Idx + 1 == Record.size()) {
505 // Single declaration
506 S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++])));
507 } else {
508 llvm::SmallVector<Decl *, 16> Decls;
509 Decls.reserve(Record.size() - Idx);
510 for (unsigned N = Record.size(); Idx != N; ++Idx)
511 Decls.push_back(Reader.GetDecl(Record[Idx]));
512 S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
513 &Decls[0], Decls.size())));
514 }
515 return 0;
516}
517
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000518unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) {
519 VisitStmt(S);
520 unsigned NumOutputs = Record[Idx++];
521 unsigned NumInputs = Record[Idx++];
522 unsigned NumClobbers = Record[Idx++];
523 S->setAsmLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
524 S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
525 S->setVolatile(Record[Idx++]);
526 S->setSimple(Record[Idx++]);
527
528 unsigned StackIdx
529 = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1);
530 S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
531
532 // Outputs and inputs
533 llvm::SmallVector<std::string, 16> Names;
534 llvm::SmallVector<StringLiteral*, 16> Constraints;
535 llvm::SmallVector<Stmt*, 16> Exprs;
536 for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
537 Names.push_back(Reader.ReadString(Record, Idx));
538 Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
539 Exprs.push_back(StmtStack[StackIdx++]);
540 }
541 S->setOutputsAndInputs(NumOutputs, NumInputs,
542 &Names[0], &Constraints[0], &Exprs[0]);
543
544 // Constraints
545 llvm::SmallVector<StringLiteral*, 16> Clobbers;
546 for (unsigned I = 0; I != NumClobbers; ++I)
547 Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++]));
548 S->setClobbers(&Clobbers[0], NumClobbers);
549
550 assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt");
551 return NumOutputs*2 + NumInputs*2 + NumClobbers + 1;
552}
553
Douglas Gregora151ba42009-04-14 23:32:43 +0000554unsigned PCHStmtReader::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000555 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000556 E->setType(Reader.GetType(Record[Idx++]));
557 E->setTypeDependent(Record[Idx++]);
558 E->setValueDependent(Record[Idx++]);
Douglas Gregor596e0932009-04-15 16:35:07 +0000559 assert(Idx == NumExprFields && "Incorrect expression field count");
Douglas Gregora151ba42009-04-14 23:32:43 +0000560 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000561}
562
Douglas Gregora151ba42009-04-14 23:32:43 +0000563unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000564 VisitExpr(E);
565 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
566 E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000567 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000568}
569
Douglas Gregora151ba42009-04-14 23:32:43 +0000570unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000571 VisitExpr(E);
572 E->setDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
573 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000574 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000575}
576
Douglas Gregora151ba42009-04-14 23:32:43 +0000577unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000578 VisitExpr(E);
579 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
580 E->setValue(Reader.ReadAPInt(Record, Idx));
Douglas Gregora151ba42009-04-14 23:32:43 +0000581 return 0;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000582}
583
Douglas Gregora151ba42009-04-14 23:32:43 +0000584unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
Douglas Gregore2f37202009-04-14 21:55:33 +0000585 VisitExpr(E);
586 E->setValue(Reader.ReadAPFloat(Record, Idx));
587 E->setExact(Record[Idx++]);
588 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregora151ba42009-04-14 23:32:43 +0000589 return 0;
Douglas Gregore2f37202009-04-14 21:55:33 +0000590}
591
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000592unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
593 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000594 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000595 return 1;
596}
597
Douglas Gregor596e0932009-04-15 16:35:07 +0000598unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) {
599 VisitExpr(E);
600 unsigned Len = Record[Idx++];
601 assert(Record[Idx] == E->getNumConcatenated() &&
602 "Wrong number of concatenated tokens!");
603 ++Idx;
604 E->setWide(Record[Idx++]);
605
606 // Read string data
607 llvm::SmallVector<char, 16> Str(&Record[Idx], &Record[Idx] + Len);
608 E->setStrData(Reader.getContext(), &Str[0], Len);
609 Idx += Len;
610
611 // Read source locations
612 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
613 E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++]));
614
615 return 0;
616}
617
Douglas Gregora151ba42009-04-14 23:32:43 +0000618unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000619 VisitExpr(E);
620 E->setValue(Record[Idx++]);
621 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
622 E->setWide(Record[Idx++]);
Douglas Gregora151ba42009-04-14 23:32:43 +0000623 return 0;
624}
625
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000626unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) {
627 VisitExpr(E);
628 E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
629 E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000630 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000631 return 1;
632}
633
Douglas Gregor12d74052009-04-15 15:58:59 +0000634unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) {
635 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000636 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000637 E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
638 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
639 return 1;
640}
641
642unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
643 VisitExpr(E);
644 E->setSizeof(Record[Idx++]);
645 if (Record[Idx] == 0) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000646 E->setArgument(cast<Expr>(StmtStack.back()));
Douglas Gregor12d74052009-04-15 15:58:59 +0000647 ++Idx;
648 } else {
649 E->setArgument(Reader.GetType(Record[Idx++]));
650 }
651 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
652 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
653 return E->isArgumentType()? 0 : 1;
654}
655
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000656unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
657 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000658 E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
659 E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 2]));
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000660 E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
661 return 2;
662}
663
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000664unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) {
665 VisitExpr(E);
666 E->setNumArgs(Reader.getContext(), Record[Idx++]);
667 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000668 E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000669 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000670 E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I]));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000671 return E->getNumArgs() + 1;
672}
673
674unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) {
675 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000676 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000677 E->setMemberDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
678 E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
679 E->setArrow(Record[Idx++]);
680 return 1;
681}
682
Douglas Gregora151ba42009-04-14 23:32:43 +0000683unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) {
684 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000685 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregora151ba42009-04-14 23:32:43 +0000686 return 1;
687}
688
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000689unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) {
690 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000691 E->setLHS(cast<Expr>(StmtStack.end()[-2]));
692 E->setRHS(cast<Expr>(StmtStack.end()[-1]));
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000693 E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
694 E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
695 return 2;
696}
697
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000698unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
699 VisitBinaryOperator(E);
700 E->setComputationLHSType(Reader.GetType(Record[Idx++]));
701 E->setComputationResultType(Reader.GetType(Record[Idx++]));
702 return 2;
703}
704
705unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
706 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000707 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
708 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
709 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000710 return 3;
711}
712
Douglas Gregora151ba42009-04-14 23:32:43 +0000713unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
714 VisitCastExpr(E);
715 E->setLvalueCast(Record[Idx++]);
716 return 1;
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000717}
718
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000719unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
720 VisitCastExpr(E);
721 E->setTypeAsWritten(Reader.GetType(Record[Idx++]));
722 return 1;
723}
724
725unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
726 VisitExplicitCastExpr(E);
727 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
728 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
729 return 1;
730}
731
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000732unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
733 VisitExpr(E);
734 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000735 E->setInitializer(cast<Expr>(StmtStack.back()));
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000736 E->setFileScope(Record[Idx++]);
737 return 1;
738}
739
Douglas Gregorec0b8292009-04-15 23:02:49 +0000740unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
741 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000742 E->setBase(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000743 E->setAccessor(Reader.GetIdentifierInfo(Record, Idx));
744 E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
745 return 1;
746}
747
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000748unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) {
749 VisitExpr(E);
750 unsigned NumInits = Record[Idx++];
751 E->reserveInits(NumInits);
752 for (unsigned I = 0; I != NumInits; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000753 E->updateInit(I,
754 cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I]));
755 E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back()));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000756 E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
757 E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
758 E->setInitializedFieldInUnion(
759 cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])));
760 E->sawArrayRangeDesignator(Record[Idx++]);
761 return NumInits + 1;
762}
763
764unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
765 typedef DesignatedInitExpr::Designator Designator;
766
767 VisitExpr(E);
768 unsigned NumSubExprs = Record[Idx++];
769 assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
770 for (unsigned I = 0; I != NumSubExprs; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000771 E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I]));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000772 E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
773 E->setGNUSyntax(Record[Idx++]);
774
775 llvm::SmallVector<Designator, 4> Designators;
776 while (Idx < Record.size()) {
777 switch ((pch::DesignatorTypes)Record[Idx++]) {
778 case pch::DESIG_FIELD_DECL: {
779 FieldDecl *Field = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
780 SourceLocation DotLoc
781 = SourceLocation::getFromRawEncoding(Record[Idx++]);
782 SourceLocation FieldLoc
783 = SourceLocation::getFromRawEncoding(Record[Idx++]);
784 Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
785 FieldLoc));
786 Designators.back().setField(Field);
787 break;
788 }
789
790 case pch::DESIG_FIELD_NAME: {
791 const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx);
792 SourceLocation DotLoc
793 = SourceLocation::getFromRawEncoding(Record[Idx++]);
794 SourceLocation FieldLoc
795 = SourceLocation::getFromRawEncoding(Record[Idx++]);
796 Designators.push_back(Designator(Name, DotLoc, FieldLoc));
797 break;
798 }
799
800 case pch::DESIG_ARRAY: {
801 unsigned Index = Record[Idx++];
802 SourceLocation LBracketLoc
803 = SourceLocation::getFromRawEncoding(Record[Idx++]);
804 SourceLocation RBracketLoc
805 = SourceLocation::getFromRawEncoding(Record[Idx++]);
806 Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
807 break;
808 }
809
810 case pch::DESIG_ARRAY_RANGE: {
811 unsigned Index = Record[Idx++];
812 SourceLocation LBracketLoc
813 = SourceLocation::getFromRawEncoding(Record[Idx++]);
814 SourceLocation EllipsisLoc
815 = SourceLocation::getFromRawEncoding(Record[Idx++]);
816 SourceLocation RBracketLoc
817 = SourceLocation::getFromRawEncoding(Record[Idx++]);
818 Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
819 RBracketLoc));
820 break;
821 }
822 }
823 }
824 E->setDesignators(&Designators[0], Designators.size());
825
826 return NumSubExprs;
827}
828
829unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
830 VisitExpr(E);
831 return 0;
832}
833
Douglas Gregorec0b8292009-04-15 23:02:49 +0000834unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) {
835 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000836 E->setSubExpr(cast<Expr>(StmtStack.back()));
Douglas Gregorec0b8292009-04-15 23:02:49 +0000837 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
838 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
839 return 1;
840}
841
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000842unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
843 VisitExpr(E);
844 E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
845 E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
846 Reader.SetLabelOf(E, Record[Idx++]);
847 return 0;
848}
849
Douglas Gregoreca12f62009-04-17 19:05:30 +0000850unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) {
851 VisitExpr(E);
852 E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
853 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
854 E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back()));
855 return 1;
856}
857
Douglas Gregor209d4622009-04-15 23:33:31 +0000858unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
859 VisitExpr(E);
860 E->setArgType1(Reader.GetType(Record[Idx++]));
861 E->setArgType2(Reader.GetType(Record[Idx++]));
862 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
863 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
864 return 0;
865}
866
867unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) {
868 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000869 E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3]));
870 E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
871 E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1]));
Douglas Gregor209d4622009-04-15 23:33:31 +0000872 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
873 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
874 return 3;
875}
876
877unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
878 VisitExpr(E);
879 E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
880 return 0;
881}
Douglas Gregorec0b8292009-04-15 23:02:49 +0000882
Douglas Gregor725e94b2009-04-16 00:01:45 +0000883unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
884 VisitExpr(E);
885 unsigned NumExprs = Record[Idx++];
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000886 E->setExprs((Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000887 E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
888 E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
889 return NumExprs;
890}
891
Douglas Gregore246b742009-04-17 19:21:43 +0000892unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
893 VisitExpr(E);
894 E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
895 E->setHasBlockDeclRefExprs(Record[Idx++]);
896 return 0;
897}
898
Douglas Gregor725e94b2009-04-16 00:01:45 +0000899unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
900 VisitExpr(E);
901 E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
902 E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
903 E->setByRef(Record[Idx++]);
904 return 0;
905}
906
Douglas Gregorc34897d2009-04-09 22:27:44 +0000907// FIXME: use the diagnostics machinery
908static bool Error(const char *Str) {
909 std::fprintf(stderr, "%s\n", Str);
910 return true;
911}
912
Douglas Gregorb3a04c82009-04-10 23:10:45 +0000913/// \brief Check the contents of the predefines buffer against the
914/// contents of the predefines buffer used to build the PCH file.
915///
916/// The contents of the two predefines buffers should be the same. If
917/// not, then some command-line option changed the preprocessor state
918/// and we must reject the PCH file.
919///
920/// \param PCHPredef The start of the predefines buffer in the PCH
921/// file.
922///
923/// \param PCHPredefLen The length of the predefines buffer in the PCH
924/// file.
925///
926/// \param PCHBufferID The FileID for the PCH predefines buffer.
927///
928/// \returns true if there was a mismatch (in which case the PCH file
929/// should be ignored), or false otherwise.
930bool PCHReader::CheckPredefinesBuffer(const char *PCHPredef,
931 unsigned PCHPredefLen,
932 FileID PCHBufferID) {
933 const char *Predef = PP.getPredefines().c_str();
934 unsigned PredefLen = PP.getPredefines().size();
935
936 // If the two predefines buffers compare equal, we're done!.
937 if (PredefLen == PCHPredefLen &&
938 strncmp(Predef, PCHPredef, PCHPredefLen) == 0)
939 return false;
940
941 // The predefines buffers are different. Produce a reasonable
942 // diagnostic showing where they are different.
943
944 // The source locations (potentially in the two different predefines
945 // buffers)
946 SourceLocation Loc1, Loc2;
947 SourceManager &SourceMgr = PP.getSourceManager();
948
949 // Create a source buffer for our predefines string, so
950 // that we can build a diagnostic that points into that
951 // source buffer.
952 FileID BufferID;
953 if (Predef && Predef[0]) {
954 llvm::MemoryBuffer *Buffer
955 = llvm::MemoryBuffer::getMemBuffer(Predef, Predef + PredefLen,
956 "<built-in>");
957 BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
958 }
959
960 unsigned MinLen = std::min(PredefLen, PCHPredefLen);
961 std::pair<const char *, const char *> Locations
962 = std::mismatch(Predef, Predef + MinLen, PCHPredef);
963
964 if (Locations.first != Predef + MinLen) {
965 // We found the location in the two buffers where there is a
966 // difference. Form source locations to point there (in both
967 // buffers).
968 unsigned Offset = Locations.first - Predef;
969 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
970 .getFileLocWithOffset(Offset);
971 Loc2 = SourceMgr.getLocForStartOfFile(PCHBufferID)
972 .getFileLocWithOffset(Offset);
973 } else if (PredefLen > PCHPredefLen) {
974 Loc1 = SourceMgr.getLocForStartOfFile(BufferID)
975 .getFileLocWithOffset(MinLen);
976 } else {
977 Loc1 = SourceMgr.getLocForStartOfFile(PCHBufferID)
978 .getFileLocWithOffset(MinLen);
979 }
980
981 Diag(Loc1, diag::warn_pch_preprocessor);
982 if (Loc2.isValid())
983 Diag(Loc2, diag::note_predef_in_pch);
984 Diag(diag::note_ignoring_pch) << FileName;
985 return true;
986}
987
Douglas Gregor635f97f2009-04-13 16:31:14 +0000988/// \brief Read the line table in the source manager block.
989/// \returns true if ther was an error.
990static bool ParseLineTable(SourceManager &SourceMgr,
991 llvm::SmallVectorImpl<uint64_t> &Record) {
992 unsigned Idx = 0;
993 LineTableInfo &LineTable = SourceMgr.getLineTable();
994
995 // Parse the file names
Douglas Gregor183ad602009-04-13 17:12:42 +0000996 std::map<int, int> FileIDs;
997 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor635f97f2009-04-13 16:31:14 +0000998 // Extract the file name
999 unsigned FilenameLen = Record[Idx++];
1000 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
1001 Idx += FilenameLen;
Douglas Gregor183ad602009-04-13 17:12:42 +00001002 FileIDs[I] = LineTable.getLineTableFilenameID(Filename.c_str(),
1003 Filename.size());
Douglas Gregor635f97f2009-04-13 16:31:14 +00001004 }
1005
1006 // Parse the line entries
1007 std::vector<LineEntry> Entries;
1008 while (Idx < Record.size()) {
Douglas Gregor183ad602009-04-13 17:12:42 +00001009 int FID = FileIDs[Record[Idx++]];
Douglas Gregor635f97f2009-04-13 16:31:14 +00001010
1011 // Extract the line entries
1012 unsigned NumEntries = Record[Idx++];
1013 Entries.clear();
1014 Entries.reserve(NumEntries);
1015 for (unsigned I = 0; I != NumEntries; ++I) {
1016 unsigned FileOffset = Record[Idx++];
1017 unsigned LineNo = Record[Idx++];
1018 int FilenameID = Record[Idx++];
1019 SrcMgr::CharacteristicKind FileKind
1020 = (SrcMgr::CharacteristicKind)Record[Idx++];
1021 unsigned IncludeOffset = Record[Idx++];
1022 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1023 FileKind, IncludeOffset));
1024 }
1025 LineTable.AddEntry(FID, Entries);
1026 }
1027
1028 return false;
1029}
1030
Douglas Gregorab1cef72009-04-10 03:52:48 +00001031/// \brief Read the source manager block
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001032PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001033 using namespace SrcMgr;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001034 if (Stream.EnterSubBlock(pch::SOURCE_MANAGER_BLOCK_ID)) {
1035 Error("Malformed source manager block record");
1036 return Failure;
1037 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001038
1039 SourceManager &SourceMgr = Context.getSourceManager();
1040 RecordData Record;
1041 while (true) {
1042 unsigned Code = Stream.ReadCode();
1043 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001044 if (Stream.ReadBlockEnd()) {
1045 Error("Error at end of Source Manager block");
1046 return Failure;
1047 }
1048
1049 return Success;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001050 }
1051
1052 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1053 // No known subblocks, always skip them.
1054 Stream.ReadSubBlockID();
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001055 if (Stream.SkipBlock()) {
1056 Error("Malformed block record");
1057 return Failure;
1058 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001059 continue;
1060 }
1061
1062 if (Code == llvm::bitc::DEFINE_ABBREV) {
1063 Stream.ReadAbbrevRecord();
1064 continue;
1065 }
1066
1067 // Read a record.
1068 const char *BlobStart;
1069 unsigned BlobLen;
1070 Record.clear();
1071 switch (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1072 default: // Default behavior: ignore.
1073 break;
1074
1075 case pch::SM_SLOC_FILE_ENTRY: {
1076 // FIXME: We would really like to delay the creation of this
1077 // FileEntry until it is actually required, e.g., when producing
1078 // a diagnostic with a source location in this file.
1079 const FileEntry *File
1080 = PP.getFileManager().getFile(BlobStart, BlobStart + BlobLen);
1081 // FIXME: Error recovery if file cannot be found.
Douglas Gregor635f97f2009-04-13 16:31:14 +00001082 FileID ID = SourceMgr.createFileID(File,
1083 SourceLocation::getFromRawEncoding(Record[1]),
1084 (CharacteristicKind)Record[2]);
1085 if (Record[3])
1086 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(ID).getFile())
1087 .setHasLineDirectives();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001088 break;
1089 }
1090
1091 case pch::SM_SLOC_BUFFER_ENTRY: {
1092 const char *Name = BlobStart;
1093 unsigned Code = Stream.ReadCode();
1094 Record.clear();
1095 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1096 assert(RecCode == pch::SM_SLOC_BUFFER_BLOB && "Ill-formed PCH file");
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001097 (void)RecCode;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001098 llvm::MemoryBuffer *Buffer
1099 = llvm::MemoryBuffer::getMemBuffer(BlobStart,
1100 BlobStart + BlobLen - 1,
1101 Name);
1102 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer);
1103
1104 if (strcmp(Name, "<built-in>") == 0
1105 && CheckPredefinesBuffer(BlobStart, BlobLen - 1, BufferID))
1106 return IgnorePCH;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001107 break;
1108 }
1109
1110 case pch::SM_SLOC_INSTANTIATION_ENTRY: {
1111 SourceLocation SpellingLoc
1112 = SourceLocation::getFromRawEncoding(Record[1]);
1113 SourceMgr.createInstantiationLoc(
1114 SpellingLoc,
1115 SourceLocation::getFromRawEncoding(Record[2]),
1116 SourceLocation::getFromRawEncoding(Record[3]),
Douglas Gregor364e5802009-04-15 18:05:10 +00001117 Record[4]);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001118 break;
1119 }
1120
Chris Lattnere1be6022009-04-14 23:22:57 +00001121 case pch::SM_LINE_TABLE:
Douglas Gregor635f97f2009-04-13 16:31:14 +00001122 if (ParseLineTable(SourceMgr, Record))
1123 return Failure;
Chris Lattnere1be6022009-04-14 23:22:57 +00001124 break;
Douglas Gregorab1cef72009-04-10 03:52:48 +00001125 }
1126 }
1127}
1128
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001129bool PCHReader::ReadPreprocessorBlock() {
1130 if (Stream.EnterSubBlock(pch::PREPROCESSOR_BLOCK_ID))
1131 return Error("Malformed preprocessor block record");
1132
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001133 RecordData Record;
1134 llvm::SmallVector<IdentifierInfo*, 16> MacroArgs;
1135 MacroInfo *LastMacro = 0;
1136
1137 while (true) {
1138 unsigned Code = Stream.ReadCode();
1139 switch (Code) {
1140 case llvm::bitc::END_BLOCK:
1141 if (Stream.ReadBlockEnd())
1142 return Error("Error at end of preprocessor block");
1143 return false;
1144
1145 case llvm::bitc::ENTER_SUBBLOCK:
1146 // No known subblocks, always skip them.
1147 Stream.ReadSubBlockID();
1148 if (Stream.SkipBlock())
1149 return Error("Malformed block record");
1150 continue;
1151
1152 case llvm::bitc::DEFINE_ABBREV:
1153 Stream.ReadAbbrevRecord();
1154 continue;
1155 default: break;
1156 }
1157
1158 // Read a record.
1159 Record.clear();
1160 pch::PreprocessorRecordTypes RecType =
1161 (pch::PreprocessorRecordTypes)Stream.ReadRecord(Code, Record);
1162 switch (RecType) {
1163 default: // Default behavior: ignore unknown records.
1164 break;
Chris Lattner4b21c202009-04-13 01:29:17 +00001165 case pch::PP_COUNTER_VALUE:
1166 if (!Record.empty())
1167 PP.setCounterValue(Record[0]);
1168 break;
1169
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001170 case pch::PP_MACRO_OBJECT_LIKE:
1171 case pch::PP_MACRO_FUNCTION_LIKE: {
Chris Lattner29241862009-04-11 21:15:38 +00001172 IdentifierInfo *II = DecodeIdentifierInfo(Record[0]);
1173 if (II == 0)
1174 return Error("Macro must have a name");
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001175 SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[1]);
1176 bool isUsed = Record[2];
1177
1178 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1179 MI->setIsUsed(isUsed);
1180
1181 if (RecType == pch::PP_MACRO_FUNCTION_LIKE) {
1182 // Decode function-like macro info.
1183 bool isC99VarArgs = Record[3];
1184 bool isGNUVarArgs = Record[4];
1185 MacroArgs.clear();
1186 unsigned NumArgs = Record[5];
1187 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattner29241862009-04-11 21:15:38 +00001188 MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i]));
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001189
1190 // Install function-like macro info.
1191 MI->setIsFunctionLike();
1192 if (isC99VarArgs) MI->setIsC99Varargs();
1193 if (isGNUVarArgs) MI->setIsGNUVarargs();
1194 MI->setArgumentList(&MacroArgs[0], MacroArgs.size(),
1195 PP.getPreprocessorAllocator());
1196 }
1197
1198 // Finally, install the macro.
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001199 PP.setMacroInfo(II, MI);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001200
1201 // Remember that we saw this macro last so that we add the tokens that
1202 // form its body to it.
1203 LastMacro = MI;
1204 break;
1205 }
1206
1207 case pch::PP_TOKEN: {
1208 // If we see a TOKEN before a PP_MACRO_*, then the file is eroneous, just
1209 // pretend we didn't see this.
1210 if (LastMacro == 0) break;
1211
1212 Token Tok;
1213 Tok.startToken();
1214 Tok.setLocation(SourceLocation::getFromRawEncoding(Record[0]));
1215 Tok.setLength(Record[1]);
Chris Lattner29241862009-04-11 21:15:38 +00001216 if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2]))
1217 Tok.setIdentifierInfo(II);
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001218 Tok.setKind((tok::TokenKind)Record[3]);
1219 Tok.setFlag((Token::TokenFlags)Record[4]);
1220 LastMacro->AddTokenToBody(Tok);
1221 break;
1222 }
1223 }
1224 }
1225}
1226
Douglas Gregor179cfb12009-04-10 20:39:37 +00001227PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
1228 if (Stream.EnterSubBlock(pch::PCH_BLOCK_ID)) {
1229 Error("Malformed block record");
1230 return Failure;
1231 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001232
Chris Lattner29241862009-04-11 21:15:38 +00001233 uint64_t PreprocessorBlockBit = 0;
Douglas Gregor456e0952009-04-17 22:13:46 +00001234
Douglas Gregorc34897d2009-04-09 22:27:44 +00001235 // Read all of the records and blocks for the PCH file.
Douglas Gregorac8f2802009-04-10 17:25:41 +00001236 RecordData Record;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001237 while (!Stream.AtEndOfStream()) {
1238 unsigned Code = Stream.ReadCode();
1239 if (Code == llvm::bitc::END_BLOCK) {
Chris Lattner29241862009-04-11 21:15:38 +00001240 // If we saw the preprocessor block, read it now.
1241 if (PreprocessorBlockBit) {
1242 uint64_t SavedPos = Stream.GetCurrentBitNo();
1243 Stream.JumpToBit(PreprocessorBlockBit);
1244 if (ReadPreprocessorBlock()) {
1245 Error("Malformed preprocessor block");
1246 return Failure;
1247 }
1248 Stream.JumpToBit(SavedPos);
1249 }
1250
Douglas Gregor179cfb12009-04-10 20:39:37 +00001251 if (Stream.ReadBlockEnd()) {
1252 Error("Error at end of module block");
1253 return Failure;
1254 }
Chris Lattner29241862009-04-11 21:15:38 +00001255
Douglas Gregor179cfb12009-04-10 20:39:37 +00001256 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001257 }
1258
1259 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1260 switch (Stream.ReadSubBlockID()) {
1261 case pch::DECLS_BLOCK_ID: // Skip decls block (lazily loaded)
1262 case pch::TYPES_BLOCK_ID: // Skip types block (lazily loaded)
1263 default: // Skip unknown content.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001264 if (Stream.SkipBlock()) {
1265 Error("Malformed block record");
1266 return Failure;
1267 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001268 break;
1269
Chris Lattner29241862009-04-11 21:15:38 +00001270 case pch::PREPROCESSOR_BLOCK_ID:
1271 // Skip the preprocessor block for now, but remember where it is. We
1272 // want to read it in after the identifier table.
1273 if (PreprocessorBlockBit) {
1274 Error("Multiple preprocessor blocks found.");
1275 return Failure;
1276 }
1277 PreprocessorBlockBit = Stream.GetCurrentBitNo();
1278 if (Stream.SkipBlock()) {
1279 Error("Malformed block record");
1280 return Failure;
1281 }
1282 break;
1283
Douglas Gregorab1cef72009-04-10 03:52:48 +00001284 case pch::SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001285 switch (ReadSourceManagerBlock()) {
1286 case Success:
1287 break;
1288
1289 case Failure:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001290 Error("Malformed source manager block");
1291 return Failure;
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001292
1293 case IgnorePCH:
1294 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001295 }
Douglas Gregorab1cef72009-04-10 03:52:48 +00001296 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001297 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001298 continue;
1299 }
1300
1301 if (Code == llvm::bitc::DEFINE_ABBREV) {
1302 Stream.ReadAbbrevRecord();
1303 continue;
1304 }
1305
1306 // Read and process a record.
1307 Record.clear();
Douglas Gregorb5887f32009-04-10 21:16:55 +00001308 const char *BlobStart = 0;
1309 unsigned BlobLen = 0;
1310 switch ((pch::PCHRecordTypes)Stream.ReadRecord(Code, Record,
1311 &BlobStart, &BlobLen)) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001312 default: // Default behavior: ignore.
1313 break;
1314
1315 case pch::TYPE_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001316 if (!TypeOffsets.empty()) {
1317 Error("Duplicate TYPE_OFFSET record in PCH file");
1318 return Failure;
1319 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001320 TypeOffsets.swap(Record);
1321 TypeAlreadyLoaded.resize(TypeOffsets.size(), false);
1322 break;
1323
1324 case pch::DECL_OFFSET:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001325 if (!DeclOffsets.empty()) {
1326 Error("Duplicate DECL_OFFSET record in PCH file");
1327 return Failure;
1328 }
Douglas Gregorac8f2802009-04-10 17:25:41 +00001329 DeclOffsets.swap(Record);
1330 DeclAlreadyLoaded.resize(DeclOffsets.size(), false);
1331 break;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001332
1333 case pch::LANGUAGE_OPTIONS:
1334 if (ParseLanguageOptions(Record))
1335 return IgnorePCH;
1336 break;
Douglas Gregorb5887f32009-04-10 21:16:55 +00001337
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001338 case pch::TARGET_TRIPLE: {
Douglas Gregorb5887f32009-04-10 21:16:55 +00001339 std::string TargetTriple(BlobStart, BlobLen);
1340 if (TargetTriple != Context.Target.getTargetTriple()) {
1341 Diag(diag::warn_pch_target_triple)
1342 << TargetTriple << Context.Target.getTargetTriple();
1343 Diag(diag::note_ignoring_pch) << FileName;
1344 return IgnorePCH;
1345 }
1346 break;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001347 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001348
1349 case pch::IDENTIFIER_TABLE:
1350 IdentifierTable = BlobStart;
1351 break;
1352
1353 case pch::IDENTIFIER_OFFSET:
1354 if (!IdentifierData.empty()) {
1355 Error("Duplicate IDENTIFIER_OFFSET record in PCH file");
1356 return Failure;
1357 }
1358 IdentifierData.swap(Record);
1359#ifndef NDEBUG
1360 for (unsigned I = 0, N = IdentifierData.size(); I != N; ++I) {
1361 if ((IdentifierData[I] & 0x01) == 0) {
1362 Error("Malformed identifier table in the precompiled header");
1363 return Failure;
1364 }
1365 }
1366#endif
1367 break;
Douglas Gregor631f6c62009-04-14 00:24:19 +00001368
1369 case pch::EXTERNAL_DEFINITIONS:
1370 if (!ExternalDefinitions.empty()) {
1371 Error("Duplicate EXTERNAL_DEFINITIONS record in PCH file");
1372 return Failure;
1373 }
1374 ExternalDefinitions.swap(Record);
1375 break;
Douglas Gregor456e0952009-04-17 22:13:46 +00001376
Douglas Gregore01ad442009-04-18 05:55:16 +00001377 case pch::SPECIAL_TYPES:
1378 SpecialTypes.swap(Record);
1379 break;
1380
Douglas Gregor456e0952009-04-17 22:13:46 +00001381 case pch::STATISTICS:
1382 TotalNumStatements = Record[0];
1383 break;
1384
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001385 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001386 }
1387
Douglas Gregor179cfb12009-04-10 20:39:37 +00001388 Error("Premature end of bitstream");
1389 return Failure;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001390}
1391
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001392PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) {
Douglas Gregor179cfb12009-04-10 20:39:37 +00001393 // Set the PCH file name.
1394 this->FileName = FileName;
1395
Douglas Gregorc34897d2009-04-09 22:27:44 +00001396 // Open the PCH file.
1397 std::string ErrStr;
1398 Buffer.reset(llvm::MemoryBuffer::getFile(FileName.c_str(), &ErrStr));
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001399 if (!Buffer) {
1400 Error(ErrStr.c_str());
1401 return IgnorePCH;
1402 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001403
1404 // Initialize the stream
1405 Stream.init((const unsigned char *)Buffer->getBufferStart(),
1406 (const unsigned char *)Buffer->getBufferEnd());
1407
1408 // Sniff for the signature.
1409 if (Stream.Read(8) != 'C' ||
1410 Stream.Read(8) != 'P' ||
1411 Stream.Read(8) != 'C' ||
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001412 Stream.Read(8) != 'H') {
1413 Error("Not a PCH file");
1414 return IgnorePCH;
1415 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001416
1417 // We expect a number of well-defined blocks, though we don't necessarily
1418 // need to understand them all.
1419 while (!Stream.AtEndOfStream()) {
1420 unsigned Code = Stream.ReadCode();
1421
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001422 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
1423 Error("Invalid record at top-level");
1424 return Failure;
1425 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001426
1427 unsigned BlockID = Stream.ReadSubBlockID();
1428
1429 // We only know the PCH subblock ID.
1430 switch (BlockID) {
1431 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001432 if (Stream.ReadBlockInfoBlock()) {
1433 Error("Malformed BlockInfoBlock");
1434 return Failure;
1435 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001436 break;
1437 case pch::PCH_BLOCK_ID:
Douglas Gregor179cfb12009-04-10 20:39:37 +00001438 switch (ReadPCHBlock()) {
1439 case Success:
1440 break;
1441
1442 case Failure:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001443 return Failure;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001444
1445 case IgnorePCH:
Douglas Gregorb5887f32009-04-10 21:16:55 +00001446 // FIXME: We could consider reading through to the end of this
1447 // PCH block, skipping subblocks, to see if there are other
1448 // PCH blocks elsewhere.
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001449 return IgnorePCH;
Douglas Gregor179cfb12009-04-10 20:39:37 +00001450 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001451 break;
1452 default:
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001453 if (Stream.SkipBlock()) {
1454 Error("Malformed block record");
1455 return Failure;
1456 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001457 break;
1458 }
1459 }
1460
1461 // Load the translation unit declaration
1462 ReadDeclRecord(DeclOffsets[0], 0);
1463
Douglas Gregore01ad442009-04-18 05:55:16 +00001464 // Load the special types.
1465 Context.setBuiltinVaListType(
1466 GetType(SpecialTypes[pch::SPECIAL_TYPE_BUILTIN_VA_LIST]));
1467
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001468 return Success;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001469}
1470
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001471namespace {
1472 /// \brief Helper class that saves the current stream position and
1473 /// then restores it when destroyed.
1474 struct VISIBILITY_HIDDEN SavedStreamPosition {
1475 explicit SavedStreamPosition(llvm::BitstreamReader &Stream)
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001476 : Stream(Stream), Offset(Stream.GetCurrentBitNo()) { }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001477
1478 ~SavedStreamPosition() {
Douglas Gregor6dc849b2009-04-15 04:54:29 +00001479 Stream.JumpToBit(Offset);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001480 }
1481
1482 private:
1483 llvm::BitstreamReader &Stream;
1484 uint64_t Offset;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001485 };
1486}
1487
Douglas Gregor179cfb12009-04-10 20:39:37 +00001488/// \brief Parse the record that corresponds to a LangOptions data
1489/// structure.
1490///
1491/// This routine compares the language options used to generate the
1492/// PCH file against the language options set for the current
1493/// compilation. For each option, we classify differences between the
1494/// two compiler states as either "benign" or "important". Benign
1495/// differences don't matter, and we accept them without complaint
1496/// (and without modifying the language options). Differences between
1497/// the states for important options cause the PCH file to be
1498/// unusable, so we emit a warning and return true to indicate that
1499/// there was an error.
1500///
1501/// \returns true if the PCH file is unacceptable, false otherwise.
1502bool PCHReader::ParseLanguageOptions(
1503 const llvm::SmallVectorImpl<uint64_t> &Record) {
1504 const LangOptions &LangOpts = Context.getLangOptions();
1505#define PARSE_LANGOPT_BENIGN(Option) ++Idx
1506#define PARSE_LANGOPT_IMPORTANT(Option, DiagID) \
1507 if (Record[Idx] != LangOpts.Option) { \
1508 Diag(DiagID) << (unsigned)Record[Idx] << LangOpts.Option; \
1509 Diag(diag::note_ignoring_pch) << FileName; \
1510 return true; \
1511 } \
1512 ++Idx
1513
1514 unsigned Idx = 0;
1515 PARSE_LANGOPT_BENIGN(Trigraphs);
1516 PARSE_LANGOPT_BENIGN(BCPLComment);
1517 PARSE_LANGOPT_BENIGN(DollarIdents);
1518 PARSE_LANGOPT_BENIGN(AsmPreprocessor);
1519 PARSE_LANGOPT_IMPORTANT(GNUMode, diag::warn_pch_gnu_extensions);
1520 PARSE_LANGOPT_BENIGN(ImplicitInt);
1521 PARSE_LANGOPT_BENIGN(Digraphs);
1522 PARSE_LANGOPT_BENIGN(HexFloats);
1523 PARSE_LANGOPT_IMPORTANT(C99, diag::warn_pch_c99);
1524 PARSE_LANGOPT_IMPORTANT(Microsoft, diag::warn_pch_microsoft_extensions);
1525 PARSE_LANGOPT_IMPORTANT(CPlusPlus, diag::warn_pch_cplusplus);
1526 PARSE_LANGOPT_IMPORTANT(CPlusPlus0x, diag::warn_pch_cplusplus0x);
1527 PARSE_LANGOPT_IMPORTANT(NoExtensions, diag::warn_pch_extensions);
1528 PARSE_LANGOPT_BENIGN(CXXOperatorName);
1529 PARSE_LANGOPT_IMPORTANT(ObjC1, diag::warn_pch_objective_c);
1530 PARSE_LANGOPT_IMPORTANT(ObjC2, diag::warn_pch_objective_c2);
1531 PARSE_LANGOPT_IMPORTANT(ObjCNonFragileABI, diag::warn_pch_nonfragile_abi);
1532 PARSE_LANGOPT_BENIGN(PascalStrings);
1533 PARSE_LANGOPT_BENIGN(Boolean);
1534 PARSE_LANGOPT_BENIGN(WritableStrings);
1535 PARSE_LANGOPT_IMPORTANT(LaxVectorConversions,
1536 diag::warn_pch_lax_vector_conversions);
1537 PARSE_LANGOPT_IMPORTANT(Exceptions, diag::warn_pch_exceptions);
1538 PARSE_LANGOPT_IMPORTANT(NeXTRuntime, diag::warn_pch_objc_runtime);
1539 PARSE_LANGOPT_IMPORTANT(Freestanding, diag::warn_pch_freestanding);
1540 PARSE_LANGOPT_IMPORTANT(NoBuiltin, diag::warn_pch_builtins);
1541 PARSE_LANGOPT_IMPORTANT(ThreadsafeStatics,
1542 diag::warn_pch_thread_safe_statics);
1543 PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks);
1544 PARSE_LANGOPT_BENIGN(EmitAllDecls);
1545 PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno);
1546 PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking);
1547 PARSE_LANGOPT_IMPORTANT(HeinousExtensions,
1548 diag::warn_pch_heinous_extensions);
1549 // FIXME: Most of the options below are benign if the macro wasn't
1550 // used. Unfortunately, this means that a PCH compiled without
1551 // optimization can't be used with optimization turned on, even
1552 // though the only thing that changes is whether __OPTIMIZE__ was
1553 // defined... but if __OPTIMIZE__ never showed up in the header, it
1554 // doesn't matter. We could consider making this some special kind
1555 // of check.
1556 PARSE_LANGOPT_IMPORTANT(Optimize, diag::warn_pch_optimize);
1557 PARSE_LANGOPT_IMPORTANT(OptimizeSize, diag::warn_pch_optimize_size);
1558 PARSE_LANGOPT_IMPORTANT(Static, diag::warn_pch_static);
1559 PARSE_LANGOPT_IMPORTANT(PICLevel, diag::warn_pch_pic_level);
1560 PARSE_LANGOPT_IMPORTANT(GNUInline, diag::warn_pch_gnu_inline);
1561 PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline);
1562 if ((LangOpts.getGCMode() != 0) != (Record[Idx] != 0)) {
1563 Diag(diag::warn_pch_gc_mode)
1564 << (unsigned)Record[Idx] << LangOpts.getGCMode();
1565 Diag(diag::note_ignoring_pch) << FileName;
1566 return true;
1567 }
1568 ++Idx;
1569 PARSE_LANGOPT_BENIGN(getVisibilityMode());
1570 PARSE_LANGOPT_BENIGN(InstantiationDepth);
1571#undef PARSE_LANGOPT_IRRELEVANT
1572#undef PARSE_LANGOPT_BENIGN
1573
1574 return false;
1575}
1576
Douglas Gregorc34897d2009-04-09 22:27:44 +00001577/// \brief Read and return the type at the given offset.
1578///
1579/// This routine actually reads the record corresponding to the type
1580/// at the given offset in the bitstream. It is a helper routine for
1581/// GetType, which deals with reading type IDs.
1582QualType PCHReader::ReadTypeRecord(uint64_t Offset) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001583 // Keep track of where we are in the stream, then jump back there
1584 // after reading this type.
1585 SavedStreamPosition SavedPosition(Stream);
1586
Douglas Gregorc34897d2009-04-09 22:27:44 +00001587 Stream.JumpToBit(Offset);
1588 RecordData Record;
1589 unsigned Code = Stream.ReadCode();
1590 switch ((pch::TypeCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorbdd4ba52009-04-15 22:00:08 +00001591 case pch::TYPE_EXT_QUAL: {
1592 assert(Record.size() == 3 &&
1593 "Incorrect encoding of extended qualifier type");
1594 QualType Base = GetType(Record[0]);
1595 QualType::GCAttrTypes GCAttr = (QualType::GCAttrTypes)Record[1];
1596 unsigned AddressSpace = Record[2];
1597
1598 QualType T = Base;
1599 if (GCAttr != QualType::GCNone)
1600 T = Context.getObjCGCQualType(T, GCAttr);
1601 if (AddressSpace)
1602 T = Context.getAddrSpaceQualType(T, AddressSpace);
1603 return T;
1604 }
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001605
Douglas Gregorc34897d2009-04-09 22:27:44 +00001606 case pch::TYPE_FIXED_WIDTH_INT: {
1607 assert(Record.size() == 2 && "Incorrect encoding of fixed-width int type");
1608 return Context.getFixedWidthIntType(Record[0], Record[1]);
1609 }
1610
1611 case pch::TYPE_COMPLEX: {
1612 assert(Record.size() == 1 && "Incorrect encoding of complex type");
1613 QualType ElemType = GetType(Record[0]);
1614 return Context.getComplexType(ElemType);
1615 }
1616
1617 case pch::TYPE_POINTER: {
1618 assert(Record.size() == 1 && "Incorrect encoding of pointer type");
1619 QualType PointeeType = GetType(Record[0]);
1620 return Context.getPointerType(PointeeType);
1621 }
1622
1623 case pch::TYPE_BLOCK_POINTER: {
1624 assert(Record.size() == 1 && "Incorrect encoding of block pointer type");
1625 QualType PointeeType = GetType(Record[0]);
1626 return Context.getBlockPointerType(PointeeType);
1627 }
1628
1629 case pch::TYPE_LVALUE_REFERENCE: {
1630 assert(Record.size() == 1 && "Incorrect encoding of lvalue reference type");
1631 QualType PointeeType = GetType(Record[0]);
1632 return Context.getLValueReferenceType(PointeeType);
1633 }
1634
1635 case pch::TYPE_RVALUE_REFERENCE: {
1636 assert(Record.size() == 1 && "Incorrect encoding of rvalue reference type");
1637 QualType PointeeType = GetType(Record[0]);
1638 return Context.getRValueReferenceType(PointeeType);
1639 }
1640
1641 case pch::TYPE_MEMBER_POINTER: {
1642 assert(Record.size() == 1 && "Incorrect encoding of member pointer type");
1643 QualType PointeeType = GetType(Record[0]);
1644 QualType ClassType = GetType(Record[1]);
1645 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
1646 }
1647
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001648 case pch::TYPE_CONSTANT_ARRAY: {
1649 QualType ElementType = GetType(Record[0]);
1650 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1651 unsigned IndexTypeQuals = Record[2];
1652 unsigned Idx = 3;
1653 llvm::APInt Size = ReadAPInt(Record, Idx);
1654 return Context.getConstantArrayType(ElementType, Size, ASM, IndexTypeQuals);
1655 }
1656
1657 case pch::TYPE_INCOMPLETE_ARRAY: {
1658 QualType ElementType = GetType(Record[0]);
1659 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1660 unsigned IndexTypeQuals = Record[2];
1661 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
1662 }
1663
1664 case pch::TYPE_VARIABLE_ARRAY: {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001665 QualType ElementType = GetType(Record[0]);
1666 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
1667 unsigned IndexTypeQuals = Record[2];
1668 return Context.getVariableArrayType(ElementType, ReadExpr(),
1669 ASM, IndexTypeQuals);
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001670 }
1671
1672 case pch::TYPE_VECTOR: {
1673 if (Record.size() != 2) {
1674 Error("Incorrect encoding of vector type in PCH file");
1675 return QualType();
1676 }
1677
1678 QualType ElementType = GetType(Record[0]);
1679 unsigned NumElements = Record[1];
1680 return Context.getVectorType(ElementType, NumElements);
1681 }
1682
1683 case pch::TYPE_EXT_VECTOR: {
1684 if (Record.size() != 2) {
1685 Error("Incorrect encoding of extended vector type in PCH file");
1686 return QualType();
1687 }
1688
1689 QualType ElementType = GetType(Record[0]);
1690 unsigned NumElements = Record[1];
1691 return Context.getExtVectorType(ElementType, NumElements);
1692 }
1693
1694 case pch::TYPE_FUNCTION_NO_PROTO: {
1695 if (Record.size() != 1) {
1696 Error("Incorrect encoding of no-proto function type");
1697 return QualType();
1698 }
1699 QualType ResultType = GetType(Record[0]);
1700 return Context.getFunctionNoProtoType(ResultType);
1701 }
1702
1703 case pch::TYPE_FUNCTION_PROTO: {
1704 QualType ResultType = GetType(Record[0]);
1705 unsigned Idx = 1;
1706 unsigned NumParams = Record[Idx++];
1707 llvm::SmallVector<QualType, 16> ParamTypes;
1708 for (unsigned I = 0; I != NumParams; ++I)
1709 ParamTypes.push_back(GetType(Record[Idx++]));
1710 bool isVariadic = Record[Idx++];
1711 unsigned Quals = Record[Idx++];
1712 return Context.getFunctionType(ResultType, &ParamTypes[0], NumParams,
1713 isVariadic, Quals);
1714 }
1715
1716 case pch::TYPE_TYPEDEF:
1717 assert(Record.size() == 1 && "Incorrect encoding of typedef type");
1718 return Context.getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0])));
1719
1720 case pch::TYPE_TYPEOF_EXPR:
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001721 return Context.getTypeOfExprType(ReadExpr());
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001722
1723 case pch::TYPE_TYPEOF: {
1724 if (Record.size() != 1) {
1725 Error("Incorrect encoding of typeof(type) in PCH file");
1726 return QualType();
1727 }
1728 QualType UnderlyingType = GetType(Record[0]);
1729 return Context.getTypeOfType(UnderlyingType);
1730 }
1731
1732 case pch::TYPE_RECORD:
Douglas Gregor982365e2009-04-13 21:20:57 +00001733 assert(Record.size() == 1 && "Incorrect encoding of record type");
1734 return Context.getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0])));
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001735
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001736 case pch::TYPE_ENUM:
1737 assert(Record.size() == 1 && "Incorrect encoding of enum type");
1738 return Context.getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0])));
1739
Douglas Gregor88fd09d2009-04-13 20:46:52 +00001740 case pch::TYPE_OBJC_INTERFACE:
1741 // FIXME: Deserialize ObjCInterfaceType
1742 assert(false && "Cannot de-serialize ObjC interface types yet");
1743 return QualType();
1744
1745 case pch::TYPE_OBJC_QUALIFIED_INTERFACE:
1746 // FIXME: Deserialize ObjCQualifiedInterfaceType
1747 assert(false && "Cannot de-serialize ObjC qualified interface types yet");
1748 return QualType();
1749
1750 case pch::TYPE_OBJC_QUALIFIED_ID:
1751 // FIXME: Deserialize ObjCQualifiedIdType
1752 assert(false && "Cannot de-serialize ObjC qualified id types yet");
1753 return QualType();
1754
1755 case pch::TYPE_OBJC_QUALIFIED_CLASS:
1756 // FIXME: Deserialize ObjCQualifiedClassType
1757 assert(false && "Cannot de-serialize ObjC qualified class types yet");
1758 return QualType();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001759 }
1760
1761 // Suppress a GCC warning
1762 return QualType();
1763}
1764
1765/// \brief Note that we have loaded the declaration with the given
1766/// Index.
1767///
1768/// This routine notes that this declaration has already been loaded,
1769/// so that future GetDecl calls will return this declaration rather
1770/// than trying to load a new declaration.
1771inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1772 assert(!DeclAlreadyLoaded[Index] && "Decl loaded twice?");
1773 DeclAlreadyLoaded[Index] = true;
1774 DeclOffsets[Index] = reinterpret_cast<uint64_t>(D);
1775}
1776
1777/// \brief Read the declaration at the given offset from the PCH file.
1778Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001779 // Keep track of where we are in the stream, then jump back there
1780 // after reading this declaration.
1781 SavedStreamPosition SavedPosition(Stream);
1782
Douglas Gregorc34897d2009-04-09 22:27:44 +00001783 Decl *D = 0;
1784 Stream.JumpToBit(Offset);
1785 RecordData Record;
1786 unsigned Code = Stream.ReadCode();
1787 unsigned Idx = 0;
1788 PCHDeclReader Reader(*this, Record, Idx);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001789
Douglas Gregorc34897d2009-04-09 22:27:44 +00001790 switch ((pch::DeclCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregor1c507882009-04-15 21:30:51 +00001791 case pch::DECL_ATTR:
1792 case pch::DECL_CONTEXT_LEXICAL:
1793 case pch::DECL_CONTEXT_VISIBLE:
1794 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1795 break;
1796
Douglas Gregorc34897d2009-04-09 22:27:44 +00001797 case pch::DECL_TRANSLATION_UNIT:
1798 assert(Index == 0 && "Translation unit must be at index 0");
Douglas Gregorc34897d2009-04-09 22:27:44 +00001799 D = Context.getTranslationUnitDecl();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001800 break;
1801
1802 case pch::DECL_TYPEDEF: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001803 D = TypedefDecl::Create(Context, 0, SourceLocation(), 0, QualType());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001804 break;
1805 }
1806
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001807 case pch::DECL_ENUM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001808 D = EnumDecl::Create(Context, 0, SourceLocation(), 0, 0);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001809 break;
1810 }
1811
Douglas Gregor982365e2009-04-13 21:20:57 +00001812 case pch::DECL_RECORD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001813 D = RecordDecl::Create(Context, TagDecl::TK_struct, 0, SourceLocation(),
1814 0, 0);
Douglas Gregor982365e2009-04-13 21:20:57 +00001815 break;
1816 }
1817
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001818 case pch::DECL_ENUM_CONSTANT: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001819 D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1820 0, llvm::APSInt());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001821 break;
1822 }
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001823
1824 case pch::DECL_FUNCTION: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001825 D = FunctionDecl::Create(Context, 0, SourceLocation(), DeclarationName(),
1826 QualType());
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001827 break;
1828 }
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001829
Steve Naroff79ea0e02009-04-20 15:06:07 +00001830 case pch::DECL_OBJC_METHOD: {
1831 D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1832 Selector(), QualType(), 0);
1833 break;
1834 }
1835
Steve Naroff7333b492009-04-20 20:09:33 +00001836 case pch::DECL_OBJC_INTERFACE_DECL: {
1837 D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
1838 break;
1839 }
1840
1841 case pch::DECL_OBJC_IVAR_DECL: {
1842 D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1843 ObjCIvarDecl::None);
1844 break;
1845 }
1846
Douglas Gregor982365e2009-04-13 21:20:57 +00001847 case pch::DECL_FIELD: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001848 D = FieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(), 0,
1849 false);
Douglas Gregor982365e2009-04-13 21:20:57 +00001850 break;
1851 }
1852
Douglas Gregorc34897d2009-04-09 22:27:44 +00001853 case pch::DECL_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001854 D = VarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1855 VarDecl::None, SourceLocation());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001856 break;
1857 }
1858
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001859 case pch::DECL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001860 D = ParmVarDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1861 VarDecl::None, 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001862 break;
1863 }
1864
1865 case pch::DECL_ORIGINAL_PARM_VAR: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001866 D = OriginalParmVarDecl::Create(Context, 0, SourceLocation(), 0,
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001867 QualType(), QualType(), VarDecl::None,
1868 0);
Douglas Gregor23ce3a52009-04-13 22:18:37 +00001869 break;
1870 }
1871
Douglas Gregor2a491792009-04-13 22:49:25 +00001872 case pch::DECL_FILE_SCOPE_ASM: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001873 D = FileScopeAsmDecl::Create(Context, 0, SourceLocation(), 0);
Douglas Gregor2a491792009-04-13 22:49:25 +00001874 break;
1875 }
1876
1877 case pch::DECL_BLOCK: {
Douglas Gregorddf4d092009-04-16 22:29:51 +00001878 D = BlockDecl::Create(Context, 0, SourceLocation());
Douglas Gregor2a491792009-04-13 22:49:25 +00001879 break;
1880 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001881 }
1882
Douglas Gregorddf4d092009-04-16 22:29:51 +00001883 assert(D && "Unknown declaration creating PCH file");
1884 if (D) {
1885 LoadedDecl(Index, D);
1886 Reader.Visit(D);
1887 }
1888
Douglas Gregorc34897d2009-04-09 22:27:44 +00001889 // If this declaration is also a declaration context, get the
1890 // offsets for its tables of lexical and visible declarations.
1891 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1892 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1893 if (Offsets.first || Offsets.second) {
1894 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1895 DC->setHasExternalVisibleStorage(Offsets.second != 0);
1896 DeclContextOffsets[DC] = Offsets;
1897 }
1898 }
1899 assert(Idx == Record.size());
1900
1901 return D;
1902}
1903
Douglas Gregorac8f2802009-04-10 17:25:41 +00001904QualType PCHReader::GetType(pch::TypeID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001905 unsigned Quals = ID & 0x07;
1906 unsigned Index = ID >> 3;
1907
1908 if (Index < pch::NUM_PREDEF_TYPE_IDS) {
1909 QualType T;
1910 switch ((pch::PredefinedTypeIDs)Index) {
1911 case pch::PREDEF_TYPE_NULL_ID: return QualType();
1912 case pch::PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
1913 case pch::PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
1914
1915 case pch::PREDEF_TYPE_CHAR_U_ID:
1916 case pch::PREDEF_TYPE_CHAR_S_ID:
1917 // FIXME: Check that the signedness of CharTy is correct!
1918 T = Context.CharTy;
1919 break;
1920
1921 case pch::PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
1922 case pch::PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
1923 case pch::PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
1924 case pch::PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
1925 case pch::PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
1926 case pch::PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
1927 case pch::PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
1928 case pch::PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
1929 case pch::PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
1930 case pch::PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
1931 case pch::PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
1932 case pch::PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
1933 case pch::PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
1934 case pch::PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
1935 case pch::PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
1936 case pch::PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
1937 }
1938
1939 assert(!T.isNull() && "Unknown predefined type");
1940 return T.getQualifiedType(Quals);
1941 }
1942
1943 Index -= pch::NUM_PREDEF_TYPE_IDS;
1944 if (!TypeAlreadyLoaded[Index]) {
1945 // Load the type from the PCH file.
1946 TypeOffsets[Index] = reinterpret_cast<uint64_t>(
1947 ReadTypeRecord(TypeOffsets[Index]).getTypePtr());
1948 TypeAlreadyLoaded[Index] = true;
1949 }
1950
1951 return QualType(reinterpret_cast<Type *>(TypeOffsets[Index]), Quals);
1952}
1953
Douglas Gregorac8f2802009-04-10 17:25:41 +00001954Decl *PCHReader::GetDecl(pch::DeclID ID) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001955 if (ID == 0)
1956 return 0;
1957
1958 unsigned Index = ID - 1;
1959 if (DeclAlreadyLoaded[Index])
1960 return reinterpret_cast<Decl *>(DeclOffsets[Index]);
1961
1962 // Load the declaration from the PCH file.
1963 return ReadDeclRecord(DeclOffsets[Index], Index);
1964}
1965
Douglas Gregor3b9a7c82009-04-18 00:07:54 +00001966Stmt *PCHReader::GetStmt(uint64_t Offset) {
1967 // Keep track of where we are in the stream, then jump back there
1968 // after reading this declaration.
1969 SavedStreamPosition SavedPosition(Stream);
1970
1971 Stream.JumpToBit(Offset);
1972 return ReadStmt();
1973}
1974
Douglas Gregorc34897d2009-04-09 22:27:44 +00001975bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC,
Douglas Gregorac8f2802009-04-10 17:25:41 +00001976 llvm::SmallVectorImpl<pch::DeclID> &Decls) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001977 assert(DC->hasExternalLexicalStorage() &&
1978 "DeclContext has no lexical decls in storage");
1979 uint64_t Offset = DeclContextOffsets[DC].first;
1980 assert(Offset && "DeclContext has no lexical decls in storage");
1981
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001982 // Keep track of where we are in the stream, then jump back there
1983 // after reading this context.
1984 SavedStreamPosition SavedPosition(Stream);
1985
Douglas Gregorc34897d2009-04-09 22:27:44 +00001986 // Load the record containing all of the declarations lexically in
1987 // this context.
1988 Stream.JumpToBit(Offset);
1989 RecordData Record;
1990 unsigned Code = Stream.ReadCode();
1991 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00001992 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001993 assert(RecCode == pch::DECL_CONTEXT_LEXICAL && "Expected lexical block");
1994
1995 // Load all of the declaration IDs
1996 Decls.clear();
1997 Decls.insert(Decls.end(), Record.begin(), Record.end());
1998 return false;
1999}
2000
2001bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC,
2002 llvm::SmallVectorImpl<VisibleDeclaration> & Decls) {
2003 assert(DC->hasExternalVisibleStorage() &&
2004 "DeclContext has no visible decls in storage");
2005 uint64_t Offset = DeclContextOffsets[DC].second;
2006 assert(Offset && "DeclContext has no visible decls in storage");
2007
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002008 // Keep track of where we are in the stream, then jump back there
2009 // after reading this context.
2010 SavedStreamPosition SavedPosition(Stream);
2011
Douglas Gregorc34897d2009-04-09 22:27:44 +00002012 // Load the record containing all of the declarations visible in
2013 // this context.
2014 Stream.JumpToBit(Offset);
2015 RecordData Record;
2016 unsigned Code = Stream.ReadCode();
2017 unsigned RecCode = Stream.ReadRecord(Code, Record);
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +00002018 (void)RecCode;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002019 assert(RecCode == pch::DECL_CONTEXT_VISIBLE && "Expected visible block");
2020 if (Record.size() == 0)
2021 return false;
2022
2023 Decls.clear();
2024
2025 unsigned Idx = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002026 while (Idx < Record.size()) {
2027 Decls.push_back(VisibleDeclaration());
2028 Decls.back().Name = ReadDeclarationName(Record, Idx);
2029
Douglas Gregorc34897d2009-04-09 22:27:44 +00002030 unsigned Size = Record[Idx++];
2031 llvm::SmallVector<unsigned, 4> & LoadedDecls
2032 = Decls.back().Declarations;
2033 LoadedDecls.reserve(Size);
2034 for (unsigned I = 0; I < Size; ++I)
2035 LoadedDecls.push_back(Record[Idx++]);
2036 }
2037
2038 return false;
2039}
2040
Douglas Gregor631f6c62009-04-14 00:24:19 +00002041void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) {
2042 if (!Consumer)
2043 return;
2044
2045 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
2046 Decl *D = GetDecl(ExternalDefinitions[I]);
2047 DeclGroupRef DG(D);
2048 Consumer->HandleTopLevelDecl(DG);
2049 }
2050}
2051
Douglas Gregorc34897d2009-04-09 22:27:44 +00002052void PCHReader::PrintStats() {
2053 std::fprintf(stderr, "*** PCH Statistics:\n");
2054
2055 unsigned NumTypesLoaded = std::count(TypeAlreadyLoaded.begin(),
2056 TypeAlreadyLoaded.end(),
2057 true);
2058 unsigned NumDeclsLoaded = std::count(DeclAlreadyLoaded.begin(),
2059 DeclAlreadyLoaded.end(),
2060 true);
Douglas Gregor9cf47422009-04-13 20:50:16 +00002061 unsigned NumIdentifiersLoaded = 0;
2062 for (unsigned I = 0; I < IdentifierData.size(); ++I) {
2063 if ((IdentifierData[I] & 0x01) == 0)
2064 ++NumIdentifiersLoaded;
2065 }
2066
Douglas Gregorc34897d2009-04-09 22:27:44 +00002067 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
2068 NumTypesLoaded, (unsigned)TypeAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002069 ((float)NumTypesLoaded/TypeAlreadyLoaded.size() * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002070 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
2071 NumDeclsLoaded, (unsigned)DeclAlreadyLoaded.size(),
Douglas Gregor9cf47422009-04-13 20:50:16 +00002072 ((float)NumDeclsLoaded/DeclAlreadyLoaded.size() * 100));
2073 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
2074 NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
2075 ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
Douglas Gregor456e0952009-04-17 22:13:46 +00002076 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
2077 NumStatementsRead, TotalNumStatements,
2078 ((float)NumStatementsRead/TotalNumStatements * 100));
Douglas Gregorc34897d2009-04-09 22:27:44 +00002079 std::fprintf(stderr, "\n");
2080}
2081
Chris Lattner29241862009-04-11 21:15:38 +00002082IdentifierInfo *PCHReader::DecodeIdentifierInfo(unsigned ID) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002083 if (ID == 0)
2084 return 0;
Chris Lattner29241862009-04-11 21:15:38 +00002085
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002086 if (!IdentifierTable || IdentifierData.empty()) {
2087 Error("No identifier table in PCH file");
2088 return 0;
2089 }
Chris Lattner29241862009-04-11 21:15:38 +00002090
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002091 if (IdentifierData[ID - 1] & 0x01) {
2092 uint64_t Offset = IdentifierData[ID - 1];
2093 IdentifierData[ID - 1] = reinterpret_cast<uint64_t>(
Chris Lattner29241862009-04-11 21:15:38 +00002094 &Context.Idents.get(IdentifierTable + Offset));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002095 }
Chris Lattner29241862009-04-11 21:15:38 +00002096
2097 return reinterpret_cast<IdentifierInfo *>(IdentifierData[ID - 1]);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002098}
2099
2100DeclarationName
2101PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) {
2102 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
2103 switch (Kind) {
2104 case DeclarationName::Identifier:
2105 return DeclarationName(GetIdentifierInfo(Record, Idx));
2106
2107 case DeclarationName::ObjCZeroArgSelector:
2108 case DeclarationName::ObjCOneArgSelector:
2109 case DeclarationName::ObjCMultiArgSelector:
2110 assert(false && "Unable to de-serialize Objective-C selectors");
2111 break;
2112
2113 case DeclarationName::CXXConstructorName:
2114 return Context.DeclarationNames.getCXXConstructorName(
2115 GetType(Record[Idx++]));
2116
2117 case DeclarationName::CXXDestructorName:
2118 return Context.DeclarationNames.getCXXDestructorName(
2119 GetType(Record[Idx++]));
2120
2121 case DeclarationName::CXXConversionFunctionName:
2122 return Context.DeclarationNames.getCXXConversionFunctionName(
2123 GetType(Record[Idx++]));
2124
2125 case DeclarationName::CXXOperatorName:
2126 return Context.DeclarationNames.getCXXOperatorName(
2127 (OverloadedOperatorKind)Record[Idx++]);
2128
2129 case DeclarationName::CXXUsingDirective:
2130 return DeclarationName::getUsingDirectiveName();
2131 }
2132
2133 // Required to silence GCC warning
2134 return DeclarationName();
2135}
Douglas Gregor179cfb12009-04-10 20:39:37 +00002136
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002137/// \brief Read an integral value
2138llvm::APInt PCHReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
2139 unsigned BitWidth = Record[Idx++];
2140 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
2141 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
2142 Idx += NumWords;
2143 return Result;
2144}
2145
2146/// \brief Read a signed integral value
2147llvm::APSInt PCHReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
2148 bool isUnsigned = Record[Idx++];
2149 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
2150}
2151
Douglas Gregore2f37202009-04-14 21:55:33 +00002152/// \brief Read a floating-point value
2153llvm::APFloat PCHReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore2f37202009-04-14 21:55:33 +00002154 return llvm::APFloat(ReadAPInt(Record, Idx));
2155}
2156
Douglas Gregor1c507882009-04-15 21:30:51 +00002157// \brief Read a string
2158std::string PCHReader::ReadString(const RecordData &Record, unsigned &Idx) {
2159 unsigned Len = Record[Idx++];
2160 std::string Result(&Record[Idx], &Record[Idx] + Len);
2161 Idx += Len;
2162 return Result;
2163}
2164
2165/// \brief Reads attributes from the current stream position.
2166Attr *PCHReader::ReadAttributes() {
2167 unsigned Code = Stream.ReadCode();
2168 assert(Code == llvm::bitc::UNABBREV_RECORD &&
2169 "Expected unabbreviated record"); (void)Code;
2170
2171 RecordData Record;
2172 unsigned Idx = 0;
2173 unsigned RecCode = Stream.ReadRecord(Code, Record);
2174 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
2175 (void)RecCode;
2176
2177#define SIMPLE_ATTR(Name) \
2178 case Attr::Name: \
2179 New = ::new (Context) Name##Attr(); \
2180 break
2181
2182#define STRING_ATTR(Name) \
2183 case Attr::Name: \
2184 New = ::new (Context) Name##Attr(ReadString(Record, Idx)); \
2185 break
2186
2187#define UNSIGNED_ATTR(Name) \
2188 case Attr::Name: \
2189 New = ::new (Context) Name##Attr(Record[Idx++]); \
2190 break
2191
2192 Attr *Attrs = 0;
2193 while (Idx < Record.size()) {
2194 Attr *New = 0;
2195 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
2196 bool IsInherited = Record[Idx++];
2197
2198 switch (Kind) {
2199 STRING_ATTR(Alias);
2200 UNSIGNED_ATTR(Aligned);
2201 SIMPLE_ATTR(AlwaysInline);
2202 SIMPLE_ATTR(AnalyzerNoReturn);
2203 STRING_ATTR(Annotate);
2204 STRING_ATTR(AsmLabel);
2205
2206 case Attr::Blocks:
2207 New = ::new (Context) BlocksAttr(
2208 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
2209 break;
2210
2211 case Attr::Cleanup:
2212 New = ::new (Context) CleanupAttr(
2213 cast<FunctionDecl>(GetDecl(Record[Idx++])));
2214 break;
2215
2216 SIMPLE_ATTR(Const);
2217 UNSIGNED_ATTR(Constructor);
2218 SIMPLE_ATTR(DLLExport);
2219 SIMPLE_ATTR(DLLImport);
2220 SIMPLE_ATTR(Deprecated);
2221 UNSIGNED_ATTR(Destructor);
2222 SIMPLE_ATTR(FastCall);
2223
2224 case Attr::Format: {
2225 std::string Type = ReadString(Record, Idx);
2226 unsigned FormatIdx = Record[Idx++];
2227 unsigned FirstArg = Record[Idx++];
2228 New = ::new (Context) FormatAttr(Type, FormatIdx, FirstArg);
2229 break;
2230 }
2231
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002232 SIMPLE_ATTR(GNUInline);
Douglas Gregor1c507882009-04-15 21:30:51 +00002233
2234 case Attr::IBOutletKind:
2235 New = ::new (Context) IBOutletAttr();
2236 break;
2237
2238 SIMPLE_ATTR(NoReturn);
2239 SIMPLE_ATTR(NoThrow);
2240 SIMPLE_ATTR(Nodebug);
2241 SIMPLE_ATTR(Noinline);
2242
2243 case Attr::NonNull: {
2244 unsigned Size = Record[Idx++];
2245 llvm::SmallVector<unsigned, 16> ArgNums;
2246 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
2247 Idx += Size;
2248 New = ::new (Context) NonNullAttr(&ArgNums[0], Size);
2249 break;
2250 }
2251
2252 SIMPLE_ATTR(ObjCException);
2253 SIMPLE_ATTR(ObjCNSObject);
2254 SIMPLE_ATTR(Overloadable);
2255 UNSIGNED_ATTR(Packed);
2256 SIMPLE_ATTR(Pure);
2257 UNSIGNED_ATTR(Regparm);
2258 STRING_ATTR(Section);
2259 SIMPLE_ATTR(StdCall);
2260 SIMPLE_ATTR(TransparentUnion);
2261 SIMPLE_ATTR(Unavailable);
2262 SIMPLE_ATTR(Unused);
2263 SIMPLE_ATTR(Used);
2264
2265 case Attr::Visibility:
2266 New = ::new (Context) VisibilityAttr(
2267 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
2268 break;
2269
2270 SIMPLE_ATTR(WarnUnusedResult);
2271 SIMPLE_ATTR(Weak);
2272 SIMPLE_ATTR(WeakImport);
2273 }
2274
2275 assert(New && "Unable to decode attribute?");
2276 New->setInherited(IsInherited);
2277 New->setNext(Attrs);
2278 Attrs = New;
2279 }
2280#undef UNSIGNED_ATTR
2281#undef STRING_ATTR
2282#undef SIMPLE_ATTR
2283
2284 // The list of attributes was built backwards. Reverse the list
2285 // before returning it.
2286 Attr *PrevAttr = 0, *NextAttr = 0;
2287 while (Attrs) {
2288 NextAttr = Attrs->getNext();
2289 Attrs->setNext(PrevAttr);
2290 PrevAttr = Attrs;
2291 Attrs = NextAttr;
2292 }
2293
2294 return PrevAttr;
2295}
2296
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002297Stmt *PCHReader::ReadStmt() {
Douglas Gregora151ba42009-04-14 23:32:43 +00002298 // Within the bitstream, expressions are stored in Reverse Polish
2299 // Notation, with each of the subexpressions preceding the
2300 // expression they are stored in. To evaluate expressions, we
2301 // continue reading expressions and placing them on the stack, with
2302 // expressions having operands removing those operands from the
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002303 // stack. Evaluation terminates when we see a STMT_STOP record, and
Douglas Gregora151ba42009-04-14 23:32:43 +00002304 // the single remaining expression on the stack is our result.
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002305 RecordData Record;
Douglas Gregora151ba42009-04-14 23:32:43 +00002306 unsigned Idx;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002307 llvm::SmallVector<Stmt *, 16> StmtStack;
2308 PCHStmtReader Reader(*this, Record, Idx, StmtStack);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002309 Stmt::EmptyShell Empty;
2310
Douglas Gregora151ba42009-04-14 23:32:43 +00002311 while (true) {
2312 unsigned Code = Stream.ReadCode();
2313 if (Code == llvm::bitc::END_BLOCK) {
2314 if (Stream.ReadBlockEnd()) {
2315 Error("Error at end of Source Manager block");
2316 return 0;
2317 }
2318 break;
2319 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002320
Douglas Gregora151ba42009-04-14 23:32:43 +00002321 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2322 // No known subblocks, always skip them.
2323 Stream.ReadSubBlockID();
2324 if (Stream.SkipBlock()) {
2325 Error("Malformed block record");
2326 return 0;
2327 }
2328 continue;
2329 }
Douglas Gregore2f37202009-04-14 21:55:33 +00002330
Douglas Gregora151ba42009-04-14 23:32:43 +00002331 if (Code == llvm::bitc::DEFINE_ABBREV) {
2332 Stream.ReadAbbrevRecord();
2333 continue;
2334 }
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002335
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002336 Stmt *S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002337 Idx = 0;
2338 Record.clear();
2339 bool Finished = false;
2340 switch ((pch::StmtCode)Stream.ReadRecord(Code, Record)) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002341 case pch::STMT_STOP:
Douglas Gregora151ba42009-04-14 23:32:43 +00002342 Finished = true;
2343 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002344
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002345 case pch::STMT_NULL_PTR:
2346 S = 0;
Douglas Gregora151ba42009-04-14 23:32:43 +00002347 break;
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002348
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002349 case pch::STMT_NULL:
2350 S = new (Context) NullStmt(Empty);
2351 break;
2352
2353 case pch::STMT_COMPOUND:
2354 S = new (Context) CompoundStmt(Empty);
2355 break;
2356
2357 case pch::STMT_CASE:
2358 S = new (Context) CaseStmt(Empty);
2359 break;
2360
2361 case pch::STMT_DEFAULT:
2362 S = new (Context) DefaultStmt(Empty);
2363 break;
2364
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002365 case pch::STMT_LABEL:
2366 S = new (Context) LabelStmt(Empty);
2367 break;
2368
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002369 case pch::STMT_IF:
2370 S = new (Context) IfStmt(Empty);
2371 break;
2372
2373 case pch::STMT_SWITCH:
2374 S = new (Context) SwitchStmt(Empty);
2375 break;
2376
Douglas Gregora6b503f2009-04-17 00:16:09 +00002377 case pch::STMT_WHILE:
2378 S = new (Context) WhileStmt(Empty);
2379 break;
2380
Douglas Gregorfb5f25b2009-04-17 00:29:51 +00002381 case pch::STMT_DO:
2382 S = new (Context) DoStmt(Empty);
2383 break;
2384
2385 case pch::STMT_FOR:
2386 S = new (Context) ForStmt(Empty);
2387 break;
2388
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002389 case pch::STMT_GOTO:
2390 S = new (Context) GotoStmt(Empty);
2391 break;
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002392
2393 case pch::STMT_INDIRECT_GOTO:
2394 S = new (Context) IndirectGotoStmt(Empty);
2395 break;
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002396
Douglas Gregora6b503f2009-04-17 00:16:09 +00002397 case pch::STMT_CONTINUE:
2398 S = new (Context) ContinueStmt(Empty);
2399 break;
2400
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002401 case pch::STMT_BREAK:
2402 S = new (Context) BreakStmt(Empty);
2403 break;
2404
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002405 case pch::STMT_RETURN:
2406 S = new (Context) ReturnStmt(Empty);
2407 break;
2408
Douglas Gregor78ff29f2009-04-17 16:55:36 +00002409 case pch::STMT_DECL:
2410 S = new (Context) DeclStmt(Empty);
2411 break;
2412
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00002413 case pch::STMT_ASM:
2414 S = new (Context) AsmStmt(Empty);
2415 break;
2416
Douglas Gregora151ba42009-04-14 23:32:43 +00002417 case pch::EXPR_PREDEFINED:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002418 S = new (Context) PredefinedExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002419 break;
2420
2421 case pch::EXPR_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002422 S = new (Context) DeclRefExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002423 break;
2424
2425 case pch::EXPR_INTEGER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002426 S = new (Context) IntegerLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002427 break;
2428
2429 case pch::EXPR_FLOATING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002430 S = new (Context) FloatingLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002431 break;
2432
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002433 case pch::EXPR_IMAGINARY_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002434 S = new (Context) ImaginaryLiteral(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002435 break;
2436
Douglas Gregor596e0932009-04-15 16:35:07 +00002437 case pch::EXPR_STRING_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002438 S = StringLiteral::CreateEmpty(Context,
Douglas Gregor596e0932009-04-15 16:35:07 +00002439 Record[PCHStmtReader::NumExprFields + 1]);
2440 break;
2441
Douglas Gregora151ba42009-04-14 23:32:43 +00002442 case pch::EXPR_CHARACTER_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002443 S = new (Context) CharacterLiteral(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002444 break;
2445
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002446 case pch::EXPR_PAREN:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002447 S = new (Context) ParenExpr(Empty);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +00002448 break;
2449
Douglas Gregor12d74052009-04-15 15:58:59 +00002450 case pch::EXPR_UNARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002451 S = new (Context) UnaryOperator(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002452 break;
2453
2454 case pch::EXPR_SIZEOF_ALIGN_OF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002455 S = new (Context) SizeOfAlignOfExpr(Empty);
Douglas Gregor12d74052009-04-15 15:58:59 +00002456 break;
2457
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002458 case pch::EXPR_ARRAY_SUBSCRIPT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002459 S = new (Context) ArraySubscriptExpr(Empty);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +00002460 break;
2461
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002462 case pch::EXPR_CALL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002463 S = new (Context) CallExpr(Context, Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002464 break;
2465
2466 case pch::EXPR_MEMBER:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002467 S = new (Context) MemberExpr(Empty);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +00002468 break;
2469
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002470 case pch::EXPR_BINARY_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002471 S = new (Context) BinaryOperator(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002472 break;
2473
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002474 case pch::EXPR_COMPOUND_ASSIGN_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002475 S = new (Context) CompoundAssignOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002476 break;
2477
2478 case pch::EXPR_CONDITIONAL_OPERATOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002479 S = new (Context) ConditionalOperator(Empty);
Douglas Gregorc599bbf2009-04-15 22:40:36 +00002480 break;
2481
Douglas Gregora151ba42009-04-14 23:32:43 +00002482 case pch::EXPR_IMPLICIT_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002483 S = new (Context) ImplicitCastExpr(Empty);
Douglas Gregora151ba42009-04-14 23:32:43 +00002484 break;
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002485
2486 case pch::EXPR_CSTYLE_CAST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002487 S = new (Context) CStyleCastExpr(Empty);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +00002488 break;
Douglas Gregorec0b8292009-04-15 23:02:49 +00002489
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002490 case pch::EXPR_COMPOUND_LITERAL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002491 S = new (Context) CompoundLiteralExpr(Empty);
Douglas Gregorb70b48f2009-04-16 02:33:48 +00002492 break;
2493
Douglas Gregorec0b8292009-04-15 23:02:49 +00002494 case pch::EXPR_EXT_VECTOR_ELEMENT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002495 S = new (Context) ExtVectorElementExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002496 break;
2497
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002498 case pch::EXPR_INIT_LIST:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002499 S = new (Context) InitListExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002500 break;
2501
2502 case pch::EXPR_DESIGNATED_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002503 S = DesignatedInitExpr::CreateEmpty(Context,
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002504 Record[PCHStmtReader::NumExprFields] - 1);
2505
2506 break;
2507
2508 case pch::EXPR_IMPLICIT_VALUE_INIT:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002509 S = new (Context) ImplicitValueInitExpr(Empty);
Douglas Gregor6710a3c2009-04-16 00:55:48 +00002510 break;
2511
Douglas Gregorec0b8292009-04-15 23:02:49 +00002512 case pch::EXPR_VA_ARG:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002513 S = new (Context) VAArgExpr(Empty);
Douglas Gregorec0b8292009-04-15 23:02:49 +00002514 break;
Douglas Gregor209d4622009-04-15 23:33:31 +00002515
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002516 case pch::EXPR_ADDR_LABEL:
2517 S = new (Context) AddrLabelExpr(Empty);
2518 break;
2519
Douglas Gregoreca12f62009-04-17 19:05:30 +00002520 case pch::EXPR_STMT:
2521 S = new (Context) StmtExpr(Empty);
2522 break;
2523
Douglas Gregor209d4622009-04-15 23:33:31 +00002524 case pch::EXPR_TYPES_COMPATIBLE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002525 S = new (Context) TypesCompatibleExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002526 break;
2527
2528 case pch::EXPR_CHOOSE:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002529 S = new (Context) ChooseExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002530 break;
2531
2532 case pch::EXPR_GNU_NULL:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002533 S = new (Context) GNUNullExpr(Empty);
Douglas Gregor209d4622009-04-15 23:33:31 +00002534 break;
Douglas Gregor725e94b2009-04-16 00:01:45 +00002535
2536 case pch::EXPR_SHUFFLE_VECTOR:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002537 S = new (Context) ShuffleVectorExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002538 break;
2539
Douglas Gregore246b742009-04-17 19:21:43 +00002540 case pch::EXPR_BLOCK:
2541 S = new (Context) BlockExpr(Empty);
2542 break;
2543
Douglas Gregor725e94b2009-04-16 00:01:45 +00002544 case pch::EXPR_BLOCK_DECL_REF:
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002545 S = new (Context) BlockDeclRefExpr(Empty);
Douglas Gregor725e94b2009-04-16 00:01:45 +00002546 break;
Douglas Gregora151ba42009-04-14 23:32:43 +00002547 }
2548
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002549 // We hit a STMT_STOP, so we're done with this expression.
Douglas Gregora151ba42009-04-14 23:32:43 +00002550 if (Finished)
2551 break;
2552
Douglas Gregor456e0952009-04-17 22:13:46 +00002553 ++NumStatementsRead;
2554
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002555 if (S) {
2556 unsigned NumSubStmts = Reader.Visit(S);
2557 while (NumSubStmts > 0) {
2558 StmtStack.pop_back();
2559 --NumSubStmts;
Douglas Gregora151ba42009-04-14 23:32:43 +00002560 }
2561 }
2562
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002563 assert(Idx == Record.size() && "Invalid deserialization of statement");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002564 StmtStack.push_back(S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002565 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002566 assert(StmtStack.size() == 1 && "Extra expressions on stack!");
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002567 SwitchCaseStmts.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002568 return StmtStack.back();
2569}
2570
2571Expr *PCHReader::ReadExpr() {
2572 return dyn_cast_or_null<Expr>(ReadStmt());
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002573}
2574
Douglas Gregor179cfb12009-04-10 20:39:37 +00002575DiagnosticBuilder PCHReader::Diag(unsigned DiagID) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002576 return Diag(SourceLocation(), DiagID);
2577}
2578
2579DiagnosticBuilder PCHReader::Diag(SourceLocation Loc, unsigned DiagID) {
2580 return PP.getDiagnostics().Report(FullSourceLoc(Loc,
Douglas Gregor179cfb12009-04-10 20:39:37 +00002581 Context.getSourceManager()),
2582 DiagID);
2583}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002584
2585/// \brief Record that the given ID maps to the given switch-case
2586/// statement.
2587void PCHReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
2588 assert(SwitchCaseStmts[ID] == 0 && "Already have a SwitchCase with this ID");
2589 SwitchCaseStmts[ID] = SC;
2590}
2591
2592/// \brief Retrieve the switch-case statement with the given ID.
2593SwitchCase *PCHReader::getSwitchCaseWithID(unsigned ID) {
2594 assert(SwitchCaseStmts[ID] != 0 && "No SwitchCase with this ID");
2595 return SwitchCaseStmts[ID];
2596}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002597
2598/// \brief Record that the given label statement has been
2599/// deserialized and has the given ID.
2600void PCHReader::RecordLabelStmt(LabelStmt *S, unsigned ID) {
2601 assert(LabelStmts.find(ID) == LabelStmts.end() &&
2602 "Deserialized label twice");
2603 LabelStmts[ID] = S;
2604
2605 // If we've already seen any goto statements that point to this
2606 // label, resolve them now.
2607 typedef std::multimap<unsigned, GotoStmt *>::iterator GotoIter;
2608 std::pair<GotoIter, GotoIter> Gotos = UnresolvedGotoStmts.equal_range(ID);
2609 for (GotoIter Goto = Gotos.first; Goto != Gotos.second; ++Goto)
2610 Goto->second->setLabel(S);
2611 UnresolvedGotoStmts.erase(Gotos.first, Gotos.second);
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002612
2613 // If we've already seen any address-label statements that point to
2614 // this label, resolve them now.
2615 typedef std::multimap<unsigned, AddrLabelExpr *>::iterator AddrLabelIter;
2616 std::pair<AddrLabelIter, AddrLabelIter> AddrLabels
2617 = UnresolvedAddrLabelExprs.equal_range(ID);
2618 for (AddrLabelIter AddrLabel = AddrLabels.first;
2619 AddrLabel != AddrLabels.second; ++AddrLabel)
2620 AddrLabel->second->setLabel(S);
2621 UnresolvedAddrLabelExprs.erase(AddrLabels.first, AddrLabels.second);
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002622}
2623
2624/// \brief Set the label of the given statement to the label
2625/// identified by ID.
2626///
2627/// Depending on the order in which the label and other statements
2628/// referencing that label occur, this operation may complete
2629/// immediately (updating the statement) or it may queue the
2630/// statement to be back-patched later.
2631void PCHReader::SetLabelOf(GotoStmt *S, unsigned ID) {
2632 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2633 if (Label != LabelStmts.end()) {
2634 // We've already seen this label, so set the label of the goto and
2635 // we're done.
2636 S->setLabel(Label->second);
2637 } else {
2638 // We haven't seen this label yet, so add this goto to the set of
2639 // unresolved goto statements.
2640 UnresolvedGotoStmts.insert(std::make_pair(ID, S));
2641 }
2642}
Douglas Gregor95a8fe32009-04-17 18:58:21 +00002643
2644/// \brief Set the label of the given expression to the label
2645/// identified by ID.
2646///
2647/// Depending on the order in which the label and other statements
2648/// referencing that label occur, this operation may complete
2649/// immediately (updating the statement) or it may queue the
2650/// statement to be back-patched later.
2651void PCHReader::SetLabelOf(AddrLabelExpr *S, unsigned ID) {
2652 std::map<unsigned, LabelStmt *>::iterator Label = LabelStmts.find(ID);
2653 if (Label != LabelStmts.end()) {
2654 // We've already seen this label, so set the label of the
2655 // label-address expression and we're done.
2656 S->setLabel(Label->second);
2657 } else {
2658 // We haven't seen this label yet, so add this label-address
2659 // expression to the set of unresolved label-address expressions.
2660 UnresolvedAddrLabelExprs.insert(std::make_pair(ID, S));
2661 }
2662}