blob: 22c52aa0ffaa0976ac8ef11ec306cda0a31299b1 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- 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 PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorc34897d2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
19#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 Lattner1b094952009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
26#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000027#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000029#include "llvm/ADT/APFloat.h"
30#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000031#include "llvm/Bitcode/BitstreamWriter.h"
32#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000033#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000034#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000035using namespace clang;
36
37//===----------------------------------------------------------------------===//
38// Type serialization
39//===----------------------------------------------------------------------===//
40namespace {
41 class VISIBILITY_HIDDEN PCHTypeWriter {
42 PCHWriter &Writer;
43 PCHWriter::RecordData &Record;
44
45 public:
46 /// \brief Type code that corresponds to the record generated.
47 pch::TypeCode Code;
48
49 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
50 : Writer(Writer), Record(Record) { }
51
52 void VisitArrayType(const ArrayType *T);
53 void VisitFunctionType(const FunctionType *T);
54 void VisitTagType(const TagType *T);
55
56#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
57#define ABSTRACT_TYPE(Class, Base)
58#define DEPENDENT_TYPE(Class, Base)
59#include "clang/AST/TypeNodes.def"
60 };
61}
62
63void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
64 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
65 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
66 Record.push_back(T->getAddressSpace());
67 Code = pch::TYPE_EXT_QUAL;
68}
69
70void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
71 assert(false && "Built-in types are never serialized");
72}
73
74void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
75 Record.push_back(T->getWidth());
76 Record.push_back(T->isSigned());
77 Code = pch::TYPE_FIXED_WIDTH_INT;
78}
79
80void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
81 Writer.AddTypeRef(T->getElementType(), Record);
82 Code = pch::TYPE_COMPLEX;
83}
84
85void PCHTypeWriter::VisitPointerType(const PointerType *T) {
86 Writer.AddTypeRef(T->getPointeeType(), Record);
87 Code = pch::TYPE_POINTER;
88}
89
90void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
91 Writer.AddTypeRef(T->getPointeeType(), Record);
92 Code = pch::TYPE_BLOCK_POINTER;
93}
94
95void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
96 Writer.AddTypeRef(T->getPointeeType(), Record);
97 Code = pch::TYPE_LVALUE_REFERENCE;
98}
99
100void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
101 Writer.AddTypeRef(T->getPointeeType(), Record);
102 Code = pch::TYPE_RVALUE_REFERENCE;
103}
104
105void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
106 Writer.AddTypeRef(T->getPointeeType(), Record);
107 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
108 Code = pch::TYPE_MEMBER_POINTER;
109}
110
111void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
112 Writer.AddTypeRef(T->getElementType(), Record);
113 Record.push_back(T->getSizeModifier()); // FIXME: stable values
114 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
115}
116
117void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
118 VisitArrayType(T);
119 Writer.AddAPInt(T->getSize(), Record);
120 Code = pch::TYPE_CONSTANT_ARRAY;
121}
122
123void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
124 VisitArrayType(T);
125 Code = pch::TYPE_INCOMPLETE_ARRAY;
126}
127
128void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
129 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000130 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000131 Code = pch::TYPE_VARIABLE_ARRAY;
132}
133
134void PCHTypeWriter::VisitVectorType(const VectorType *T) {
135 Writer.AddTypeRef(T->getElementType(), Record);
136 Record.push_back(T->getNumElements());
137 Code = pch::TYPE_VECTOR;
138}
139
140void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
141 VisitVectorType(T);
142 Code = pch::TYPE_EXT_VECTOR;
143}
144
145void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
146 Writer.AddTypeRef(T->getResultType(), Record);
147}
148
149void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
150 VisitFunctionType(T);
151 Code = pch::TYPE_FUNCTION_NO_PROTO;
152}
153
154void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
155 VisitFunctionType(T);
156 Record.push_back(T->getNumArgs());
157 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
158 Writer.AddTypeRef(T->getArgType(I), Record);
159 Record.push_back(T->isVariadic());
160 Record.push_back(T->getTypeQuals());
161 Code = pch::TYPE_FUNCTION_PROTO;
162}
163
164void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
165 Writer.AddDeclRef(T->getDecl(), Record);
166 Code = pch::TYPE_TYPEDEF;
167}
168
169void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000170 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000171 Code = pch::TYPE_TYPEOF_EXPR;
172}
173
174void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
175 Writer.AddTypeRef(T->getUnderlyingType(), Record);
176 Code = pch::TYPE_TYPEOF;
177}
178
179void PCHTypeWriter::VisitTagType(const TagType *T) {
180 Writer.AddDeclRef(T->getDecl(), Record);
181 assert(!T->isBeingDefined() &&
182 "Cannot serialize in the middle of a type definition");
183}
184
185void PCHTypeWriter::VisitRecordType(const RecordType *T) {
186 VisitTagType(T);
187 Code = pch::TYPE_RECORD;
188}
189
190void PCHTypeWriter::VisitEnumType(const EnumType *T) {
191 VisitTagType(T);
192 Code = pch::TYPE_ENUM;
193}
194
195void
196PCHTypeWriter::VisitTemplateSpecializationType(
197 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000198 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000199 assert(false && "Cannot serialize template specialization types");
200}
201
202void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000203 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000204 assert(false && "Cannot serialize qualified name types");
205}
206
207void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
208 Writer.AddDeclRef(T->getDecl(), Record);
209 Code = pch::TYPE_OBJC_INTERFACE;
210}
211
212void
213PCHTypeWriter::VisitObjCQualifiedInterfaceType(
214 const ObjCQualifiedInterfaceType *T) {
215 VisitObjCInterfaceType(T);
216 Record.push_back(T->getNumProtocols());
217 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
218 Writer.AddDeclRef(T->getProtocol(I), Record);
219 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
220}
221
222void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
223 Record.push_back(T->getNumProtocols());
224 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
225 Writer.AddDeclRef(T->getProtocols(I), Record);
226 Code = pch::TYPE_OBJC_QUALIFIED_ID;
227}
228
229void
230PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
231 Record.push_back(T->getNumProtocols());
232 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
233 Writer.AddDeclRef(T->getProtocols(I), Record);
234 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
235}
236
237//===----------------------------------------------------------------------===//
238// Declaration serialization
239//===----------------------------------------------------------------------===//
240namespace {
241 class VISIBILITY_HIDDEN PCHDeclWriter
242 : public DeclVisitor<PCHDeclWriter, void> {
243
244 PCHWriter &Writer;
Douglas Gregore3241e92009-04-18 00:02:19 +0000245 ASTContext &Context;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000246 PCHWriter::RecordData &Record;
247
248 public:
249 pch::DeclCode Code;
250
Douglas Gregore3241e92009-04-18 00:02:19 +0000251 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
252 PCHWriter::RecordData &Record)
253 : Writer(Writer), Context(Context), Record(Record) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +0000254
255 void VisitDecl(Decl *D);
256 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
257 void VisitNamedDecl(NamedDecl *D);
258 void VisitTypeDecl(TypeDecl *D);
259 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000260 void VisitTagDecl(TagDecl *D);
261 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000262 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000263 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000264 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000265 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000266 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000267 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000268 void VisitParmVarDecl(ParmVarDecl *D);
269 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000270 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
271 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000272 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
273 uint64_t VisibleOffset);
Steve Naroff79ea0e02009-04-20 15:06:07 +0000274 void VisitObjCMethodDecl(ObjCMethodDecl *D);
Steve Naroff7333b492009-04-20 20:09:33 +0000275 void VisitObjCContainerDecl(ObjCContainerDecl *D);
276 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
277 void VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000278 };
279}
280
281void PCHDeclWriter::VisitDecl(Decl *D) {
282 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
283 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
284 Writer.AddSourceLocation(D->getLocation(), Record);
285 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000286 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000287 Record.push_back(D->isImplicit());
288 Record.push_back(D->getAccess());
289}
290
291void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
292 VisitDecl(D);
293 Code = pch::DECL_TRANSLATION_UNIT;
294}
295
296void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
297 VisitDecl(D);
298 Writer.AddDeclarationName(D->getDeclName(), Record);
299}
300
301void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
302 VisitNamedDecl(D);
303 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
304}
305
306void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
307 VisitTypeDecl(D);
308 Writer.AddTypeRef(D->getUnderlyingType(), Record);
309 Code = pch::DECL_TYPEDEF;
310}
311
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000312void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
313 VisitTypeDecl(D);
314 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
315 Record.push_back(D->isDefinition());
316 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
317}
318
319void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
320 VisitTagDecl(D);
321 Writer.AddTypeRef(D->getIntegerType(), Record);
322 Code = pch::DECL_ENUM;
323}
324
Douglas Gregor982365e2009-04-13 21:20:57 +0000325void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
326 VisitTagDecl(D);
327 Record.push_back(D->hasFlexibleArrayMember());
328 Record.push_back(D->isAnonymousStructOrUnion());
329 Code = pch::DECL_RECORD;
330}
331
Douglas Gregorc34897d2009-04-09 22:27:44 +0000332void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
333 VisitNamedDecl(D);
334 Writer.AddTypeRef(D->getType(), Record);
335}
336
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000337void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
338 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000339 Record.push_back(D->getInitExpr()? 1 : 0);
340 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000341 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000342 Writer.AddAPSInt(D->getInitVal(), Record);
343 Code = pch::DECL_ENUM_CONSTANT;
344}
345
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000346void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
347 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000348 Record.push_back(D->isThisDeclarationADefinition());
349 if (D->isThisDeclarationADefinition())
Douglas Gregore3241e92009-04-18 00:02:19 +0000350 Writer.AddStmt(D->getBody(Context));
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000351 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
352 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
353 Record.push_back(D->isInline());
354 Record.push_back(D->isVirtual());
355 Record.push_back(D->isPure());
356 Record.push_back(D->inheritedPrototype());
357 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
358 Record.push_back(D->isDeleted());
359 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
360 Record.push_back(D->param_size());
361 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
362 P != PEnd; ++P)
363 Writer.AddDeclRef(*P, Record);
364 Code = pch::DECL_FUNCTION;
365}
366
Steve Naroff79ea0e02009-04-20 15:06:07 +0000367void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
368 VisitNamedDecl(D);
369 // FIXME: convert to LazyStmtPtr?
370 // Unlike C/C++, method bodies will never be in header files.
371 Record.push_back(D->getBody() != 0);
372 if (D->getBody() != 0) {
373 Writer.AddStmt(D->getBody(Context));
374 Writer.AddDeclRef(D->getSelfDecl(), Record);
375 Writer.AddDeclRef(D->getCmdDecl(), Record);
376 }
377 Record.push_back(D->isInstanceMethod());
378 Record.push_back(D->isVariadic());
379 Record.push_back(D->isSynthesized());
380 // FIXME: stable encoding for @required/@optional
381 Record.push_back(D->getImplementationControl());
382 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
383 Record.push_back(D->getObjCDeclQualifier());
384 Writer.AddTypeRef(D->getResultType(), Record);
385 Writer.AddSourceLocation(D->getLocEnd(), Record);
386 Record.push_back(D->param_size());
387 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
388 PEnd = D->param_end(); P != PEnd; ++P)
389 Writer.AddDeclRef(*P, Record);
390 Code = pch::DECL_OBJC_METHOD;
391}
392
Steve Naroff7333b492009-04-20 20:09:33 +0000393void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
394 VisitNamedDecl(D);
395 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
396 // Abstract class (no need to define a stable pch::DECL code).
397}
398
399void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
400 VisitObjCContainerDecl(D);
401 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
402 Writer.AddDeclRef(D->getSuperClass(), Record);
403 Record.push_back(D->ivar_size());
404 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
405 IEnd = D->ivar_end(); I != IEnd; ++I)
406 Writer.AddDeclRef(*I, Record);
407 Record.push_back(D->isForwardDecl());
408 Record.push_back(D->isImplicitInterfaceDecl());
409 Writer.AddSourceLocation(D->getClassLoc(), Record);
410 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
411 Writer.AddSourceLocation(D->getLocEnd(), Record);
412 // FIXME: add protocols, categories.
413 Code = pch::DECL_OBJC_INTERFACE_DECL;
414}
415
416void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
417 VisitFieldDecl(D);
418 // FIXME: stable encoding for @public/@private/@protected/@package
419 Record.push_back(D->getAccessControl());
420 Code = pch::DECL_OBJC_IVAR_DECL;
421}
422
Douglas Gregor982365e2009-04-13 21:20:57 +0000423void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
424 VisitValueDecl(D);
425 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000426 Record.push_back(D->getBitWidth()? 1 : 0);
427 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000428 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000429 Code = pch::DECL_FIELD;
430}
431
Douglas Gregorc34897d2009-04-09 22:27:44 +0000432void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
433 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000434 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000435 Record.push_back(D->isThreadSpecified());
436 Record.push_back(D->hasCXXDirectInitializer());
437 Record.push_back(D->isDeclaredInCondition());
438 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
439 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000440 Record.push_back(D->getInit()? 1 : 0);
441 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000442 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000443 Code = pch::DECL_VAR;
444}
445
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000446void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
447 VisitVarDecl(D);
448 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000449 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000450 // FIXME: why isn't the "default argument" just stored as the initializer
451 // in VarDecl?
452 Code = pch::DECL_PARM_VAR;
453}
454
455void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
456 VisitParmVarDecl(D);
457 Writer.AddTypeRef(D->getOriginalType(), Record);
458 Code = pch::DECL_ORIGINAL_PARM_VAR;
459}
460
Douglas Gregor2a491792009-04-13 22:49:25 +0000461void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
462 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000463 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000464 Code = pch::DECL_FILE_SCOPE_ASM;
465}
466
467void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
468 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000469 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000470 Record.push_back(D->param_size());
471 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
472 P != PEnd; ++P)
473 Writer.AddDeclRef(*P, Record);
474 Code = pch::DECL_BLOCK;
475}
476
Douglas Gregorc34897d2009-04-09 22:27:44 +0000477/// \brief Emit the DeclContext part of a declaration context decl.
478///
479/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
480/// block for this declaration context is stored. May be 0 to indicate
481/// that there are no declarations stored within this context.
482///
483/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
484/// block for this declaration context is stored. May be 0 to indicate
485/// that there are no declarations visible from this context. Note
486/// that this value will not be emitted for non-primary declaration
487/// contexts.
488void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
489 uint64_t VisibleOffset) {
490 Record.push_back(LexicalOffset);
491 if (DC->getPrimaryContext() == DC)
492 Record.push_back(VisibleOffset);
493}
494
495//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000496// Statement/expression serialization
497//===----------------------------------------------------------------------===//
498namespace {
499 class VISIBILITY_HIDDEN PCHStmtWriter
500 : public StmtVisitor<PCHStmtWriter, void> {
501
502 PCHWriter &Writer;
503 PCHWriter::RecordData &Record;
504
505 public:
506 pch::StmtCode Code;
507
508 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
509 : Writer(Writer), Record(Record) { }
510
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000511 void VisitStmt(Stmt *S);
512 void VisitNullStmt(NullStmt *S);
513 void VisitCompoundStmt(CompoundStmt *S);
514 void VisitSwitchCase(SwitchCase *S);
515 void VisitCaseStmt(CaseStmt *S);
516 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000517 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000518 void VisitIfStmt(IfStmt *S);
519 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000520 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000521 void VisitDoStmt(DoStmt *S);
522 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000523 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000524 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000525 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000526 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000527 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000528 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000529 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000530 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000531 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000532 void VisitDeclRefExpr(DeclRefExpr *E);
533 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000534 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000535 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000536 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000537 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000538 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000539 void VisitUnaryOperator(UnaryOperator *E);
540 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000541 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000542 void VisitCallExpr(CallExpr *E);
543 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000544 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000545 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000546 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
547 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000548 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000549 void VisitExplicitCastExpr(ExplicitCastExpr *E);
550 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000551 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000552 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000553 void VisitInitListExpr(InitListExpr *E);
554 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
555 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000556 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000557 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000558 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000559 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
560 void VisitChooseExpr(ChooseExpr *E);
561 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000562 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000563 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000564 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000565 };
566}
567
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000568void PCHStmtWriter::VisitStmt(Stmt *S) {
569}
570
571void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
572 VisitStmt(S);
573 Writer.AddSourceLocation(S->getSemiLoc(), Record);
574 Code = pch::STMT_NULL;
575}
576
577void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
578 VisitStmt(S);
579 Record.push_back(S->size());
580 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
581 CS != CSEnd; ++CS)
582 Writer.WriteSubStmt(*CS);
583 Writer.AddSourceLocation(S->getLBracLoc(), Record);
584 Writer.AddSourceLocation(S->getRBracLoc(), Record);
585 Code = pch::STMT_COMPOUND;
586}
587
588void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
589 VisitStmt(S);
590 Record.push_back(Writer.RecordSwitchCaseID(S));
591}
592
593void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
594 VisitSwitchCase(S);
595 Writer.WriteSubStmt(S->getLHS());
596 Writer.WriteSubStmt(S->getRHS());
597 Writer.WriteSubStmt(S->getSubStmt());
598 Writer.AddSourceLocation(S->getCaseLoc(), Record);
599 Code = pch::STMT_CASE;
600}
601
602void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
603 VisitSwitchCase(S);
604 Writer.WriteSubStmt(S->getSubStmt());
605 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
606 Code = pch::STMT_DEFAULT;
607}
608
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000609void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
610 VisitStmt(S);
611 Writer.AddIdentifierRef(S->getID(), Record);
612 Writer.WriteSubStmt(S->getSubStmt());
613 Writer.AddSourceLocation(S->getIdentLoc(), Record);
614 Record.push_back(Writer.GetLabelID(S));
615 Code = pch::STMT_LABEL;
616}
617
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000618void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
619 VisitStmt(S);
620 Writer.WriteSubStmt(S->getCond());
621 Writer.WriteSubStmt(S->getThen());
622 Writer.WriteSubStmt(S->getElse());
623 Writer.AddSourceLocation(S->getIfLoc(), Record);
624 Code = pch::STMT_IF;
625}
626
627void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
628 VisitStmt(S);
629 Writer.WriteSubStmt(S->getCond());
630 Writer.WriteSubStmt(S->getBody());
631 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
632 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
633 SC = SC->getNextSwitchCase())
634 Record.push_back(Writer.getSwitchCaseID(SC));
635 Code = pch::STMT_SWITCH;
636}
637
Douglas Gregora6b503f2009-04-17 00:16:09 +0000638void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
639 VisitStmt(S);
640 Writer.WriteSubStmt(S->getCond());
641 Writer.WriteSubStmt(S->getBody());
642 Writer.AddSourceLocation(S->getWhileLoc(), Record);
643 Code = pch::STMT_WHILE;
644}
645
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000646void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
647 VisitStmt(S);
648 Writer.WriteSubStmt(S->getCond());
649 Writer.WriteSubStmt(S->getBody());
650 Writer.AddSourceLocation(S->getDoLoc(), Record);
651 Code = pch::STMT_DO;
652}
653
654void PCHStmtWriter::VisitForStmt(ForStmt *S) {
655 VisitStmt(S);
656 Writer.WriteSubStmt(S->getInit());
657 Writer.WriteSubStmt(S->getCond());
658 Writer.WriteSubStmt(S->getInc());
659 Writer.WriteSubStmt(S->getBody());
660 Writer.AddSourceLocation(S->getForLoc(), Record);
661 Code = pch::STMT_FOR;
662}
663
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000664void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
665 VisitStmt(S);
666 Record.push_back(Writer.GetLabelID(S->getLabel()));
667 Writer.AddSourceLocation(S->getGotoLoc(), Record);
668 Writer.AddSourceLocation(S->getLabelLoc(), Record);
669 Code = pch::STMT_GOTO;
670}
671
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000672void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
673 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000674 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000675 Writer.WriteSubStmt(S->getTarget());
676 Code = pch::STMT_INDIRECT_GOTO;
677}
678
Douglas Gregora6b503f2009-04-17 00:16:09 +0000679void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
680 VisitStmt(S);
681 Writer.AddSourceLocation(S->getContinueLoc(), Record);
682 Code = pch::STMT_CONTINUE;
683}
684
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000685void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
686 VisitStmt(S);
687 Writer.AddSourceLocation(S->getBreakLoc(), Record);
688 Code = pch::STMT_BREAK;
689}
690
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000691void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
692 VisitStmt(S);
693 Writer.WriteSubStmt(S->getRetValue());
694 Writer.AddSourceLocation(S->getReturnLoc(), Record);
695 Code = pch::STMT_RETURN;
696}
697
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000698void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
699 VisitStmt(S);
700 Writer.AddSourceLocation(S->getStartLoc(), Record);
701 Writer.AddSourceLocation(S->getEndLoc(), Record);
702 DeclGroupRef DG = S->getDeclGroup();
703 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
704 Writer.AddDeclRef(*D, Record);
705 Code = pch::STMT_DECL;
706}
707
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000708void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
709 VisitStmt(S);
710 Record.push_back(S->getNumOutputs());
711 Record.push_back(S->getNumInputs());
712 Record.push_back(S->getNumClobbers());
713 Writer.AddSourceLocation(S->getAsmLoc(), Record);
714 Writer.AddSourceLocation(S->getRParenLoc(), Record);
715 Record.push_back(S->isVolatile());
716 Record.push_back(S->isSimple());
717 Writer.WriteSubStmt(S->getAsmString());
718
719 // Outputs
720 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
721 Writer.AddString(S->getOutputName(I), Record);
722 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
723 Writer.WriteSubStmt(S->getOutputExpr(I));
724 }
725
726 // Inputs
727 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
728 Writer.AddString(S->getInputName(I), Record);
729 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
730 Writer.WriteSubStmt(S->getInputExpr(I));
731 }
732
733 // Clobbers
734 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
735 Writer.WriteSubStmt(S->getClobber(I));
736
737 Code = pch::STMT_ASM;
738}
739
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000740void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000741 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000742 Writer.AddTypeRef(E->getType(), Record);
743 Record.push_back(E->isTypeDependent());
744 Record.push_back(E->isValueDependent());
745}
746
Douglas Gregore2f37202009-04-14 21:55:33 +0000747void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
748 VisitExpr(E);
749 Writer.AddSourceLocation(E->getLocation(), Record);
750 Record.push_back(E->getIdentType()); // FIXME: stable encoding
751 Code = pch::EXPR_PREDEFINED;
752}
753
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000754void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
755 VisitExpr(E);
756 Writer.AddDeclRef(E->getDecl(), Record);
757 Writer.AddSourceLocation(E->getLocation(), Record);
758 Code = pch::EXPR_DECL_REF;
759}
760
761void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
762 VisitExpr(E);
763 Writer.AddSourceLocation(E->getLocation(), Record);
764 Writer.AddAPInt(E->getValue(), Record);
765 Code = pch::EXPR_INTEGER_LITERAL;
766}
767
Douglas Gregore2f37202009-04-14 21:55:33 +0000768void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
769 VisitExpr(E);
770 Writer.AddAPFloat(E->getValue(), Record);
771 Record.push_back(E->isExact());
772 Writer.AddSourceLocation(E->getLocation(), Record);
773 Code = pch::EXPR_FLOATING_LITERAL;
774}
775
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000776void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
777 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000778 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000779 Code = pch::EXPR_IMAGINARY_LITERAL;
780}
781
Douglas Gregor596e0932009-04-15 16:35:07 +0000782void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
783 VisitExpr(E);
784 Record.push_back(E->getByteLength());
785 Record.push_back(E->getNumConcatenated());
786 Record.push_back(E->isWide());
787 // FIXME: String data should be stored as a blob at the end of the
788 // StringLiteral. However, we can't do so now because we have no
789 // provision for coping with abbreviations when we're jumping around
790 // the PCH file during deserialization.
791 Record.insert(Record.end(),
792 E->getStrData(), E->getStrData() + E->getByteLength());
793 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
794 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
795 Code = pch::EXPR_STRING_LITERAL;
796}
797
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000798void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
799 VisitExpr(E);
800 Record.push_back(E->getValue());
801 Writer.AddSourceLocation(E->getLoc(), Record);
802 Record.push_back(E->isWide());
803 Code = pch::EXPR_CHARACTER_LITERAL;
804}
805
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000806void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
807 VisitExpr(E);
808 Writer.AddSourceLocation(E->getLParen(), Record);
809 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000810 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000811 Code = pch::EXPR_PAREN;
812}
813
Douglas Gregor12d74052009-04-15 15:58:59 +0000814void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
815 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000816 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000817 Record.push_back(E->getOpcode()); // FIXME: stable encoding
818 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
819 Code = pch::EXPR_UNARY_OPERATOR;
820}
821
822void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
823 VisitExpr(E);
824 Record.push_back(E->isSizeOf());
825 if (E->isArgumentType())
826 Writer.AddTypeRef(E->getArgumentType(), Record);
827 else {
828 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000829 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000830 }
831 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
832 Writer.AddSourceLocation(E->getRParenLoc(), Record);
833 Code = pch::EXPR_SIZEOF_ALIGN_OF;
834}
835
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000836void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
837 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000838 Writer.WriteSubStmt(E->getLHS());
839 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000840 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
841 Code = pch::EXPR_ARRAY_SUBSCRIPT;
842}
843
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000844void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
845 VisitExpr(E);
846 Record.push_back(E->getNumArgs());
847 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000848 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000849 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
850 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000851 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000852 Code = pch::EXPR_CALL;
853}
854
855void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
856 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000857 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000858 Writer.AddDeclRef(E->getMemberDecl(), Record);
859 Writer.AddSourceLocation(E->getMemberLoc(), Record);
860 Record.push_back(E->isArrow());
861 Code = pch::EXPR_MEMBER;
862}
863
Douglas Gregora151ba42009-04-14 23:32:43 +0000864void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
865 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000866 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000867}
868
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000869void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
870 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000871 Writer.WriteSubStmt(E->getLHS());
872 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000873 Record.push_back(E->getOpcode()); // FIXME: stable encoding
874 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
875 Code = pch::EXPR_BINARY_OPERATOR;
876}
877
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000878void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
879 VisitBinaryOperator(E);
880 Writer.AddTypeRef(E->getComputationLHSType(), Record);
881 Writer.AddTypeRef(E->getComputationResultType(), Record);
882 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
883}
884
885void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
886 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000887 Writer.WriteSubStmt(E->getCond());
888 Writer.WriteSubStmt(E->getLHS());
889 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000890 Code = pch::EXPR_CONDITIONAL_OPERATOR;
891}
892
Douglas Gregora151ba42009-04-14 23:32:43 +0000893void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
894 VisitCastExpr(E);
895 Record.push_back(E->isLvalueCast());
896 Code = pch::EXPR_IMPLICIT_CAST;
897}
898
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000899void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
900 VisitCastExpr(E);
901 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
902}
903
904void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
905 VisitExplicitCastExpr(E);
906 Writer.AddSourceLocation(E->getLParenLoc(), Record);
907 Writer.AddSourceLocation(E->getRParenLoc(), Record);
908 Code = pch::EXPR_CSTYLE_CAST;
909}
910
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000911void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
912 VisitExpr(E);
913 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000914 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000915 Record.push_back(E->isFileScope());
916 Code = pch::EXPR_COMPOUND_LITERAL;
917}
918
Douglas Gregorec0b8292009-04-15 23:02:49 +0000919void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
920 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000921 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000922 Writer.AddIdentifierRef(&E->getAccessor(), Record);
923 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
924 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
925}
926
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000927void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
928 VisitExpr(E);
929 Record.push_back(E->getNumInits());
930 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000931 Writer.WriteSubStmt(E->getInit(I));
932 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000933 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
934 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
935 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
936 Record.push_back(E->hadArrayRangeDesignator());
937 Code = pch::EXPR_INIT_LIST;
938}
939
940void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
941 VisitExpr(E);
942 Record.push_back(E->getNumSubExprs());
943 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000944 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000945 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
946 Record.push_back(E->usesGNUSyntax());
947 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
948 DEnd = E->designators_end();
949 D != DEnd; ++D) {
950 if (D->isFieldDesignator()) {
951 if (FieldDecl *Field = D->getField()) {
952 Record.push_back(pch::DESIG_FIELD_DECL);
953 Writer.AddDeclRef(Field, Record);
954 } else {
955 Record.push_back(pch::DESIG_FIELD_NAME);
956 Writer.AddIdentifierRef(D->getFieldName(), Record);
957 }
958 Writer.AddSourceLocation(D->getDotLoc(), Record);
959 Writer.AddSourceLocation(D->getFieldLoc(), Record);
960 } else if (D->isArrayDesignator()) {
961 Record.push_back(pch::DESIG_ARRAY);
962 Record.push_back(D->getFirstExprIndex());
963 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
964 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
965 } else {
966 assert(D->isArrayRangeDesignator() && "Unknown designator");
967 Record.push_back(pch::DESIG_ARRAY_RANGE);
968 Record.push_back(D->getFirstExprIndex());
969 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
970 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
971 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
972 }
973 }
974 Code = pch::EXPR_DESIGNATED_INIT;
975}
976
977void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
978 VisitExpr(E);
979 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
980}
981
Douglas Gregorec0b8292009-04-15 23:02:49 +0000982void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
983 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000984 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000985 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
986 Writer.AddSourceLocation(E->getRParenLoc(), Record);
987 Code = pch::EXPR_VA_ARG;
988}
989
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000990void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
991 VisitExpr(E);
992 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
993 Writer.AddSourceLocation(E->getLabelLoc(), Record);
994 Record.push_back(Writer.GetLabelID(E->getLabel()));
995 Code = pch::EXPR_ADDR_LABEL;
996}
997
Douglas Gregoreca12f62009-04-17 19:05:30 +0000998void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
999 VisitExpr(E);
1000 Writer.WriteSubStmt(E->getSubStmt());
1001 Writer.AddSourceLocation(E->getLParenLoc(), Record);
1002 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1003 Code = pch::EXPR_STMT;
1004}
1005
Douglas Gregor209d4622009-04-15 23:33:31 +00001006void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
1007 VisitExpr(E);
1008 Writer.AddTypeRef(E->getArgType1(), Record);
1009 Writer.AddTypeRef(E->getArgType2(), Record);
1010 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1011 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1012 Code = pch::EXPR_TYPES_COMPATIBLE;
1013}
1014
1015void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
1016 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001017 Writer.WriteSubStmt(E->getCond());
1018 Writer.WriteSubStmt(E->getLHS());
1019 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +00001020 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1021 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1022 Code = pch::EXPR_CHOOSE;
1023}
1024
1025void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
1026 VisitExpr(E);
1027 Writer.AddSourceLocation(E->getTokenLocation(), Record);
1028 Code = pch::EXPR_GNU_NULL;
1029}
1030
Douglas Gregor725e94b2009-04-16 00:01:45 +00001031void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
1032 VisitExpr(E);
1033 Record.push_back(E->getNumSubExprs());
1034 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001035 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +00001036 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1037 Writer.AddSourceLocation(E->getRParenLoc(), Record);
1038 Code = pch::EXPR_SHUFFLE_VECTOR;
1039}
1040
Douglas Gregore246b742009-04-17 19:21:43 +00001041void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
1042 VisitExpr(E);
1043 Writer.AddDeclRef(E->getBlockDecl(), Record);
1044 Record.push_back(E->hasBlockDeclRefExprs());
1045 Code = pch::EXPR_BLOCK;
1046}
1047
Douglas Gregor725e94b2009-04-16 00:01:45 +00001048void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
1049 VisitExpr(E);
1050 Writer.AddDeclRef(E->getDecl(), Record);
1051 Writer.AddSourceLocation(E->getLocation(), Record);
1052 Record.push_back(E->isByRef());
1053 Code = pch::EXPR_BLOCK_DECL_REF;
1054}
1055
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001056//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +00001057// PCHWriter Implementation
1058//===----------------------------------------------------------------------===//
1059
Douglas Gregorb5887f32009-04-10 21:16:55 +00001060/// \brief Write the target triple (e.g., i686-apple-darwin9).
1061void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1062 using namespace llvm;
1063 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1064 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001066 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001067
1068 RecordData Record;
1069 Record.push_back(pch::TARGET_TRIPLE);
1070 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001071 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001072}
1073
1074/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001075void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1076 RecordData Record;
1077 Record.push_back(LangOpts.Trigraphs);
1078 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1079 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1080 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1081 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1082 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1083 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1084 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1085 Record.push_back(LangOpts.C99); // C99 Support
1086 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1087 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1088 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1089 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1090 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1091
1092 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1093 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1094 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1095
1096 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1097 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1098 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1099 Record.push_back(LangOpts.LaxVectorConversions);
1100 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1101
1102 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1103 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1104 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1105
1106 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1107 // by locks.
1108 Record.push_back(LangOpts.Blocks); // block extension to C
1109 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1110 // they are unused.
1111 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1112 // (modulo the platform support).
1113
1114 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1115 // signed integer arithmetic overflows.
1116
1117 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1118 // may be ripped out at any time.
1119
1120 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1121 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1122 // defined.
1123 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1124 // opposed to __DYNAMIC__).
1125 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1126
1127 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1128 // used (instead of C99 semantics).
1129 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1130 Record.push_back(LangOpts.getGCMode());
1131 Record.push_back(LangOpts.getVisibilityMode());
1132 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001133 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001134}
1135
Douglas Gregorab1cef72009-04-10 03:52:48 +00001136//===----------------------------------------------------------------------===//
1137// Source Manager Serialization
1138//===----------------------------------------------------------------------===//
1139
1140/// \brief Create an abbreviation for the SLocEntry that refers to a
1141/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001142static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001143 using namespace llvm;
1144 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1145 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1146 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1149 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001151 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001152}
1153
1154/// \brief Create an abbreviation for the SLocEntry that refers to a
1155/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001156static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001157 using namespace llvm;
1158 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1159 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001165 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001166}
1167
1168/// \brief Create an abbreviation for the SLocEntry that refers to a
1169/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001170static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001171 using namespace llvm;
1172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1173 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001175 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001176}
1177
1178/// \brief Create an abbreviation for the SLocEntry that refers to an
1179/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001180static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001181 using namespace llvm;
1182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1183 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001189 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001190}
1191
1192/// \brief Writes the block containing the serialized form of the
1193/// source manager.
1194///
1195/// TODO: We should probably use an on-disk hash table (stored in a
1196/// blob), indexed based on the file name, so that we only create
1197/// entries for files that we actually need. In the common case (no
1198/// errors), we probably won't have to create file entries for any of
1199/// the files in the AST.
1200void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001201 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001202 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001203
1204 // Abbreviations for the various kinds of source-location entries.
1205 int SLocFileAbbrv = -1;
1206 int SLocBufferAbbrv = -1;
1207 int SLocBufferBlobAbbrv = -1;
1208 int SLocInstantiationAbbrv = -1;
1209
1210 // Write out the source location entry table. We skip the first
1211 // entry, which is always the same dummy entry.
1212 RecordData Record;
1213 for (SourceManager::sloc_entry_iterator
1214 SLoc = SourceMgr.sloc_entry_begin() + 1,
1215 SLocEnd = SourceMgr.sloc_entry_end();
1216 SLoc != SLocEnd; ++SLoc) {
1217 // Figure out which record code to use.
1218 unsigned Code;
1219 if (SLoc->isFile()) {
1220 if (SLoc->getFile().getContentCache()->Entry)
1221 Code = pch::SM_SLOC_FILE_ENTRY;
1222 else
1223 Code = pch::SM_SLOC_BUFFER_ENTRY;
1224 } else
1225 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1226 Record.push_back(Code);
1227
1228 Record.push_back(SLoc->getOffset());
1229 if (SLoc->isFile()) {
1230 const SrcMgr::FileInfo &File = SLoc->getFile();
1231 Record.push_back(File.getIncludeLoc().getRawEncoding());
1232 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001233 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001234
1235 const SrcMgr::ContentCache *Content = File.getContentCache();
1236 if (Content->Entry) {
1237 // The source location entry is a file. The blob associated
1238 // with this entry is the file name.
1239 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001240 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1241 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001242 Content->Entry->getName(),
1243 strlen(Content->Entry->getName()));
1244 } else {
1245 // The source location entry is a buffer. The blob associated
1246 // with this entry contains the contents of the buffer.
1247 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001248 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1249 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001250 }
1251
1252 // We add one to the size so that we capture the trailing NULL
1253 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1254 // the reader side).
1255 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1256 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001257 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001258 Record.clear();
1259 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001260 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001261 Buffer->getBufferStart(),
1262 Buffer->getBufferSize() + 1);
1263 }
1264 } else {
1265 // The source location entry is an instantiation.
1266 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1267 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1268 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1269 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1270
Douglas Gregor364e5802009-04-15 18:05:10 +00001271 // Compute the token length for this macro expansion.
1272 unsigned NextOffset = SourceMgr.getNextOffset();
1273 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1274 if (++NextSLoc != SLocEnd)
1275 NextOffset = NextSLoc->getOffset();
1276 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1277
Douglas Gregorab1cef72009-04-10 03:52:48 +00001278 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001279 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1280 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001281 }
1282
1283 Record.clear();
1284 }
1285
Douglas Gregor635f97f2009-04-13 16:31:14 +00001286 // Write the line table.
1287 if (SourceMgr.hasLineTable()) {
1288 LineTableInfo &LineTable = SourceMgr.getLineTable();
1289
1290 // Emit the file names
1291 Record.push_back(LineTable.getNumFilenames());
1292 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1293 // Emit the file name
1294 const char *Filename = LineTable.getFilename(I);
1295 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1296 Record.push_back(FilenameLen);
1297 if (FilenameLen)
1298 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1299 }
1300
1301 // Emit the line entries
1302 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1303 L != LEnd; ++L) {
1304 // Emit the file ID
1305 Record.push_back(L->first);
1306
1307 // Emit the line entries
1308 Record.push_back(L->second.size());
1309 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1310 LEEnd = L->second.end();
1311 LE != LEEnd; ++LE) {
1312 Record.push_back(LE->FileOffset);
1313 Record.push_back(LE->LineNo);
1314 Record.push_back(LE->FilenameID);
1315 Record.push_back((unsigned)LE->FileKind);
1316 Record.push_back(LE->IncludeOffset);
1317 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001318 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001319 }
1320 }
1321
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001322 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001323}
1324
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001325/// \brief Writes the block containing the serialized form of the
1326/// preprocessor.
1327///
Chris Lattner850eabd2009-04-10 18:08:30 +00001328void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001329 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001330 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001331
Chris Lattner1b094952009-04-10 18:00:12 +00001332 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1333 // FIXME: use diagnostics subsystem for localization etc.
1334 if (PP.SawDateOrTime())
1335 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001336
Chris Lattner1b094952009-04-10 18:00:12 +00001337 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001338
Chris Lattner4b21c202009-04-13 01:29:17 +00001339 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1340 if (PP.getCounterValue() != 0) {
1341 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001343 Record.clear();
1344 }
1345
Chris Lattner1b094952009-04-10 18:00:12 +00001346 // Loop over all the macro definitions that are live at the end of the file,
1347 // emitting each to the PP section.
1348 // FIXME: Eventually we want to emit an index so that we can lazily load
1349 // macros.
1350 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1351 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001352 // FIXME: This emits macros in hash table order, we should do it in a stable
1353 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001354 MacroInfo *MI = I->second;
1355
1356 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1357 // been redefined by the header (in which case they are not isBuiltinMacro).
1358 if (MI->isBuiltinMacro())
1359 continue;
1360
Chris Lattner29241862009-04-11 21:15:38 +00001361 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001362 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1363 Record.push_back(MI->isUsed());
1364
1365 unsigned Code;
1366 if (MI->isObjectLike()) {
1367 Code = pch::PP_MACRO_OBJECT_LIKE;
1368 } else {
1369 Code = pch::PP_MACRO_FUNCTION_LIKE;
1370
1371 Record.push_back(MI->isC99Varargs());
1372 Record.push_back(MI->isGNUVarargs());
1373 Record.push_back(MI->getNumArgs());
1374 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1375 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001376 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001377 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001378 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001379 Record.clear();
1380
Chris Lattner850eabd2009-04-10 18:08:30 +00001381 // Emit the tokens array.
1382 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1383 // Note that we know that the preprocessor does not have any annotation
1384 // tokens in it because they are created by the parser, and thus can't be
1385 // in a macro definition.
1386 const Token &Tok = MI->getReplacementToken(TokNo);
1387
1388 Record.push_back(Tok.getLocation().getRawEncoding());
1389 Record.push_back(Tok.getLength());
1390
Chris Lattner850eabd2009-04-10 18:08:30 +00001391 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1392 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001393 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001394
1395 // FIXME: Should translate token kind to a stable encoding.
1396 Record.push_back(Tok.getKind());
1397 // FIXME: Should translate token flags to a stable encoding.
1398 Record.push_back(Tok.getFlags());
1399
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001400 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001401 Record.clear();
1402 }
Chris Lattner1b094952009-04-10 18:00:12 +00001403
1404 }
1405
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001406 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001407}
1408
1409
Douglas Gregorc34897d2009-04-09 22:27:44 +00001410/// \brief Write the representation of a type to the PCH stream.
1411void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001412 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001413 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001414 ID = NextTypeID++;
1415
1416 // Record the offset for this type.
1417 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001418 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001419 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1420 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001421 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001422 }
1423
1424 RecordData Record;
1425
1426 // Emit the type's representation.
1427 PCHTypeWriter W(*this, Record);
1428 switch (T->getTypeClass()) {
1429 // For all of the concrete, non-dependent types, call the
1430 // appropriate visitor function.
1431#define TYPE(Class, Base) \
1432 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1433#define ABSTRACT_TYPE(Class, Base)
1434#define DEPENDENT_TYPE(Class, Base)
1435#include "clang/AST/TypeNodes.def"
1436
1437 // For all of the dependent type nodes (which only occur in C++
1438 // templates), produce an error.
1439#define TYPE(Class, Base)
1440#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1441#include "clang/AST/TypeNodes.def"
1442 assert(false && "Cannot serialize dependent type nodes");
1443 break;
1444 }
1445
1446 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001447 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001448
1449 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001450 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001451}
1452
1453/// \brief Write a block containing all of the types.
1454void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001455 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001456 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001457
1458 // Emit all of the types in the ASTContext
1459 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1460 TEnd = Context.getTypes().end();
1461 T != TEnd; ++T) {
1462 // Builtin types are never serialized.
1463 if (isa<BuiltinType>(*T))
1464 continue;
1465
1466 WriteType(*T);
1467 }
1468
1469 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001470 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001471}
1472
1473/// \brief Write the block containing all of the declaration IDs
1474/// lexically declared within the given DeclContext.
1475///
1476/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1477/// bistream, or 0 if no block was written.
1478uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1479 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001480 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001481 return 0;
1482
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001483 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001484 RecordData Record;
1485 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1486 DEnd = DC->decls_end(Context);
1487 D != DEnd; ++D)
1488 AddDeclRef(*D, Record);
1489
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001490 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001491 return Offset;
1492}
1493
1494/// \brief Write the block containing all of the declaration IDs
1495/// visible from the given DeclContext.
1496///
1497/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1498/// bistream, or 0 if no block was written.
1499uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1500 DeclContext *DC) {
1501 if (DC->getPrimaryContext() != DC)
1502 return 0;
1503
Douglas Gregor5afd9802009-04-18 15:49:20 +00001504 // Since there is no name lookup into functions or methods, don't
1505 // bother to build a visible-declarations table.
1506 if (DC->isFunctionOrMethod())
1507 return 0;
1508
Douglas Gregorc34897d2009-04-09 22:27:44 +00001509 // Force the DeclContext to build a its name-lookup table.
1510 DC->lookup(Context, DeclarationName());
1511
1512 // Serialize the contents of the mapping used for lookup. Note that,
1513 // although we have two very different code paths, the serialized
1514 // representation is the same for both cases: a declaration name,
1515 // followed by a size, followed by references to the visible
1516 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001517 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001518 RecordData Record;
1519 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001520 if (!Map)
1521 return 0;
1522
Douglas Gregorc34897d2009-04-09 22:27:44 +00001523 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1524 D != DEnd; ++D) {
1525 AddDeclarationName(D->first, Record);
1526 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1527 Record.push_back(Result.second - Result.first);
1528 for(; Result.first != Result.second; ++Result.first)
1529 AddDeclRef(*Result.first, Record);
1530 }
1531
1532 if (Record.size() == 0)
1533 return 0;
1534
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001535 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001536 return Offset;
1537}
1538
1539/// \brief Write a block containing all of the declarations.
1540void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001541 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001542 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001543
1544 // Emit all of the declarations.
1545 RecordData Record;
Douglas Gregore3241e92009-04-18 00:02:19 +00001546 PCHDeclWriter W(*this, Context, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001547 while (!DeclsToEmit.empty()) {
1548 // Pull the next declaration off the queue
1549 Decl *D = DeclsToEmit.front();
1550 DeclsToEmit.pop();
1551
1552 // If this declaration is also a DeclContext, write blocks for the
1553 // declarations that lexically stored inside its context and those
1554 // declarations that are visible from its context. These blocks
1555 // are written before the declaration itself so that we can put
1556 // their offsets into the record for the declaration.
1557 uint64_t LexicalOffset = 0;
1558 uint64_t VisibleOffset = 0;
1559 DeclContext *DC = dyn_cast<DeclContext>(D);
1560 if (DC) {
1561 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1562 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1563 }
1564
1565 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001566 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001567 if (ID == 0)
1568 ID = DeclIDs.size();
1569
1570 unsigned Index = ID - 1;
1571
1572 // Record the offset for this declaration
1573 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001574 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001575 else if (DeclOffsets.size() < Index) {
1576 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001577 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001578 }
1579
1580 // Build and emit a record for this declaration
1581 Record.clear();
1582 W.Code = (pch::DeclCode)0;
1583 W.Visit(D);
1584 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001585 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001586 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001587
Douglas Gregor1c507882009-04-15 21:30:51 +00001588 // If the declaration had any attributes, write them now.
1589 if (D->hasAttrs())
1590 WriteAttributeRecord(D->getAttrs());
1591
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001592 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001593 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001594
Douglas Gregor631f6c62009-04-14 00:24:19 +00001595 // Note external declarations so that we can add them to a record
1596 // in the PCH file later.
1597 if (isa<FileScopeAsmDecl>(D))
1598 ExternalDefinitions.push_back(ID);
1599 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1600 if (// Non-static file-scope variables with initializers or that
1601 // are tentative definitions.
1602 (Var->isFileVarDecl() &&
1603 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1604 // Out-of-line definitions of static data members (C++).
1605 (Var->getDeclContext()->isRecord() &&
1606 !Var->getLexicalDeclContext()->isRecord() &&
1607 Var->getStorageClass() == VarDecl::Static))
1608 ExternalDefinitions.push_back(ID);
1609 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001610 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001611 ExternalDefinitions.push_back(ID);
1612 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001613 }
1614
1615 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001616 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001617}
1618
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001619/// \brief Write the identifier table into the PCH file.
1620///
1621/// The identifier table consists of a blob containing string data
1622/// (the actual identifiers themselves) and a separate "offsets" index
1623/// that maps identifier IDs to locations within the blob.
1624void PCHWriter::WriteIdentifierTable() {
1625 using namespace llvm;
1626
1627 // Create and write out the blob that contains the identifier
1628 // strings.
1629 RecordData IdentOffsets;
1630 IdentOffsets.resize(IdentifierIDs.size());
1631 {
1632 // Create the identifier string data.
1633 std::vector<char> Data;
1634 Data.push_back(0); // Data must not be empty.
1635 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1636 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1637 ID != IDEnd; ++ID) {
1638 assert(ID->first && "NULL identifier in identifier table");
1639
1640 // Make sure we're starting on an odd byte. The PCH reader
1641 // expects the low bit to be set on all of the offsets.
1642 if ((Data.size() & 0x01) == 0)
1643 Data.push_back((char)0);
1644
1645 IdentOffsets[ID->second - 1] = Data.size();
1646 Data.insert(Data.end(),
1647 ID->first->getName(),
1648 ID->first->getName() + ID->first->getLength());
1649 Data.push_back((char)0);
1650 }
1651
1652 // Create a blob abbreviation
1653 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1654 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001656 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001657
1658 // Write the identifier table
1659 RecordData Record;
1660 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001661 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001662 }
1663
1664 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001665 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001666}
1667
Douglas Gregor1c507882009-04-15 21:30:51 +00001668/// \brief Write a record containing the given attributes.
1669void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1670 RecordData Record;
1671 for (; Attr; Attr = Attr->getNext()) {
1672 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1673 Record.push_back(Attr->isInherited());
1674 switch (Attr->getKind()) {
1675 case Attr::Alias:
1676 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1677 break;
1678
1679 case Attr::Aligned:
1680 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1681 break;
1682
1683 case Attr::AlwaysInline:
1684 break;
1685
1686 case Attr::AnalyzerNoReturn:
1687 break;
1688
1689 case Attr::Annotate:
1690 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1691 break;
1692
1693 case Attr::AsmLabel:
1694 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1695 break;
1696
1697 case Attr::Blocks:
1698 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1699 break;
1700
1701 case Attr::Cleanup:
1702 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1703 break;
1704
1705 case Attr::Const:
1706 break;
1707
1708 case Attr::Constructor:
1709 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1710 break;
1711
1712 case Attr::DLLExport:
1713 case Attr::DLLImport:
1714 case Attr::Deprecated:
1715 break;
1716
1717 case Attr::Destructor:
1718 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1719 break;
1720
1721 case Attr::FastCall:
1722 break;
1723
1724 case Attr::Format: {
1725 const FormatAttr *Format = cast<FormatAttr>(Attr);
1726 AddString(Format->getType(), Record);
1727 Record.push_back(Format->getFormatIdx());
1728 Record.push_back(Format->getFirstArg());
1729 break;
1730 }
1731
Chris Lattner15ce6cc2009-04-20 19:12:28 +00001732 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00001733 case Attr::IBOutletKind:
1734 case Attr::NoReturn:
1735 case Attr::NoThrow:
1736 case Attr::Nodebug:
1737 case Attr::Noinline:
1738 break;
1739
1740 case Attr::NonNull: {
1741 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1742 Record.push_back(NonNull->size());
1743 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1744 break;
1745 }
1746
1747 case Attr::ObjCException:
1748 case Attr::ObjCNSObject:
1749 case Attr::Overloadable:
1750 break;
1751
1752 case Attr::Packed:
1753 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1754 break;
1755
1756 case Attr::Pure:
1757 break;
1758
1759 case Attr::Regparm:
1760 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1761 break;
1762
1763 case Attr::Section:
1764 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1765 break;
1766
1767 case Attr::StdCall:
1768 case Attr::TransparentUnion:
1769 case Attr::Unavailable:
1770 case Attr::Unused:
1771 case Attr::Used:
1772 break;
1773
1774 case Attr::Visibility:
1775 // FIXME: stable encoding
1776 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1777 break;
1778
1779 case Attr::WarnUnusedResult:
1780 case Attr::Weak:
1781 case Attr::WeakImport:
1782 break;
1783 }
1784 }
1785
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001786 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001787}
1788
1789void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1790 Record.push_back(Str.size());
1791 Record.insert(Record.end(), Str.begin(), Str.end());
1792}
1793
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001794PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor456e0952009-04-17 22:13:46 +00001795 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001796
Douglas Gregor87887da2009-04-20 15:53:59 +00001797void PCHWriter::WritePCH(Sema &SemaRef) {
1798 ASTContext &Context = SemaRef.Context;
1799 Preprocessor &PP = SemaRef.PP;
1800
Douglas Gregorc34897d2009-04-09 22:27:44 +00001801 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001802 Stream.Emit((unsigned)'C', 8);
1803 Stream.Emit((unsigned)'P', 8);
1804 Stream.Emit((unsigned)'C', 8);
1805 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001806
1807 // The translation unit is the first declaration we'll emit.
1808 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1809 DeclsToEmit.push(Context.getTranslationUnitDecl());
1810
1811 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00001812 RecordData Record;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001813 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001814 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001815 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001816 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001817 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001818 WriteTypesBlock(Context);
1819 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001820 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001821 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1822 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregore01ad442009-04-18 05:55:16 +00001823
1824 // Write the record of special types.
1825 Record.clear();
1826 AddTypeRef(Context.getBuiltinVaListType(), Record);
1827 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
1828
Douglas Gregor631f6c62009-04-14 00:24:19 +00001829 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001830 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor456e0952009-04-17 22:13:46 +00001831
1832 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00001833 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00001834 Record.push_back(NumStatements);
1835 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001836 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001837}
1838
1839void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1840 Record.push_back(Loc.getRawEncoding());
1841}
1842
1843void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1844 Record.push_back(Value.getBitWidth());
1845 unsigned N = Value.getNumWords();
1846 const uint64_t* Words = Value.getRawData();
1847 for (unsigned I = 0; I != N; ++I)
1848 Record.push_back(Words[I]);
1849}
1850
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001851void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1852 Record.push_back(Value.isUnsigned());
1853 AddAPInt(Value, Record);
1854}
1855
Douglas Gregore2f37202009-04-14 21:55:33 +00001856void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1857 AddAPInt(Value.bitcastToAPInt(), Record);
1858}
1859
Douglas Gregorc34897d2009-04-09 22:27:44 +00001860void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001861 if (II == 0) {
1862 Record.push_back(0);
1863 return;
1864 }
1865
1866 pch::IdentID &ID = IdentifierIDs[II];
1867 if (ID == 0)
1868 ID = IdentifierIDs.size();
1869
1870 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001871}
1872
1873void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1874 if (T.isNull()) {
1875 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1876 return;
1877 }
1878
1879 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001880 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001881 switch (BT->getKind()) {
1882 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1883 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1884 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1885 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1886 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1887 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1888 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1889 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1890 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1891 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1892 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1893 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1894 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1895 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1896 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1897 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1898 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1899 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1900 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1901 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1902 }
1903
1904 Record.push_back((ID << 3) | T.getCVRQualifiers());
1905 return;
1906 }
1907
Douglas Gregorac8f2802009-04-10 17:25:41 +00001908 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001909 if (ID == 0) // we haven't seen this type before
1910 ID = NextTypeID++;
1911
1912 // Encode the type qualifiers in the type reference.
1913 Record.push_back((ID << 3) | T.getCVRQualifiers());
1914}
1915
1916void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1917 if (D == 0) {
1918 Record.push_back(0);
1919 return;
1920 }
1921
Douglas Gregorac8f2802009-04-10 17:25:41 +00001922 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001923 if (ID == 0) {
1924 // We haven't seen this declaration before. Give it a new ID and
1925 // enqueue it in the list of declarations to emit.
1926 ID = DeclIDs.size();
1927 DeclsToEmit.push(const_cast<Decl *>(D));
1928 }
1929
1930 Record.push_back(ID);
1931}
1932
1933void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1934 Record.push_back(Name.getNameKind());
1935 switch (Name.getNameKind()) {
1936 case DeclarationName::Identifier:
1937 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1938 break;
1939
1940 case DeclarationName::ObjCZeroArgSelector:
1941 case DeclarationName::ObjCOneArgSelector:
1942 case DeclarationName::ObjCMultiArgSelector:
1943 assert(false && "Serialization of Objective-C selectors unavailable");
1944 break;
1945
1946 case DeclarationName::CXXConstructorName:
1947 case DeclarationName::CXXDestructorName:
1948 case DeclarationName::CXXConversionFunctionName:
1949 AddTypeRef(Name.getCXXNameType(), Record);
1950 break;
1951
1952 case DeclarationName::CXXOperatorName:
1953 Record.push_back(Name.getCXXOverloadedOperator());
1954 break;
1955
1956 case DeclarationName::CXXUsingDirective:
1957 // No extra data to emit
1958 break;
1959 }
1960}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001961
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001962/// \brief Write the given substatement or subexpression to the
1963/// bitstream.
1964void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001965 RecordData Record;
1966 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00001967 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00001968
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001969 if (!S) {
1970 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001971 return;
1972 }
1973
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001974 Writer.Code = pch::STMT_NULL_PTR;
1975 Writer.Visit(S);
1976 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001977 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001978 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001979}
1980
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001981/// \brief Flush all of the statements that have been added to the
1982/// queue via AddStmt().
1983void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001984 RecordData Record;
1985 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001986
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001987 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00001988 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001989 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001990
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001991 if (!S) {
1992 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001993 continue;
1994 }
1995
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001996 Writer.Code = pch::STMT_NULL_PTR;
1997 Writer.Visit(S);
1998 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001999 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002000 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002001
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002002 assert(N == StmtsToEmit.size() &&
2003 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002004
2005 // Note that we are at the end of a full expression. Any
2006 // expression records that follow this one are part of a different
2007 // expression.
2008 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002009 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002010 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002011
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002012 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002013 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002014}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002015
2016unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2017 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2018 "SwitchCase recorded twice");
2019 unsigned NextID = SwitchCaseIDs.size();
2020 SwitchCaseIDs[S] = NextID;
2021 return NextID;
2022}
2023
2024unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2025 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2026 "SwitchCase hasn't been seen yet");
2027 return SwitchCaseIDs[S];
2028}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002029
2030/// \brief Retrieve the ID for the given label statement, which may
2031/// or may not have been emitted yet.
2032unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2033 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2034 if (Pos != LabelIDs.end())
2035 return Pos->second;
2036
2037 unsigned NextID = LabelIDs.size();
2038 LabelIDs[S] = NextID;
2039 return NextID;
2040}