blob: 5775ac4c135559475825a177c4bc545b4940a22f [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"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclContextInternals.h"
18#include "clang/AST/DeclVisitor.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000022#include "clang/Lex/MacroInfo.h"
23#include "clang/Lex/Preprocessor.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000024#include "clang/Basic/FileManager.h"
25#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000026#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000028#include "llvm/ADT/APFloat.h"
29#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000030#include "llvm/Bitcode/BitstreamWriter.h"
31#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000033#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Type serialization
38//===----------------------------------------------------------------------===//
39namespace {
40 class VISIBILITY_HIDDEN PCHTypeWriter {
41 PCHWriter &Writer;
42 PCHWriter::RecordData &Record;
43
44 public:
45 /// \brief Type code that corresponds to the record generated.
46 pch::TypeCode Code;
47
48 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
49 : Writer(Writer), Record(Record) { }
50
51 void VisitArrayType(const ArrayType *T);
52 void VisitFunctionType(const FunctionType *T);
53 void VisitTagType(const TagType *T);
54
55#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
56#define ABSTRACT_TYPE(Class, Base)
57#define DEPENDENT_TYPE(Class, Base)
58#include "clang/AST/TypeNodes.def"
59 };
60}
61
62void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
63 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
64 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
65 Record.push_back(T->getAddressSpace());
66 Code = pch::TYPE_EXT_QUAL;
67}
68
69void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
70 assert(false && "Built-in types are never serialized");
71}
72
73void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
74 Record.push_back(T->getWidth());
75 Record.push_back(T->isSigned());
76 Code = pch::TYPE_FIXED_WIDTH_INT;
77}
78
79void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
80 Writer.AddTypeRef(T->getElementType(), Record);
81 Code = pch::TYPE_COMPLEX;
82}
83
84void PCHTypeWriter::VisitPointerType(const PointerType *T) {
85 Writer.AddTypeRef(T->getPointeeType(), Record);
86 Code = pch::TYPE_POINTER;
87}
88
89void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
90 Writer.AddTypeRef(T->getPointeeType(), Record);
91 Code = pch::TYPE_BLOCK_POINTER;
92}
93
94void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
95 Writer.AddTypeRef(T->getPointeeType(), Record);
96 Code = pch::TYPE_LVALUE_REFERENCE;
97}
98
99void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
100 Writer.AddTypeRef(T->getPointeeType(), Record);
101 Code = pch::TYPE_RVALUE_REFERENCE;
102}
103
104void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
105 Writer.AddTypeRef(T->getPointeeType(), Record);
106 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
107 Code = pch::TYPE_MEMBER_POINTER;
108}
109
110void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
111 Writer.AddTypeRef(T->getElementType(), Record);
112 Record.push_back(T->getSizeModifier()); // FIXME: stable values
113 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
114}
115
116void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
117 VisitArrayType(T);
118 Writer.AddAPInt(T->getSize(), Record);
119 Code = pch::TYPE_CONSTANT_ARRAY;
120}
121
122void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
123 VisitArrayType(T);
124 Code = pch::TYPE_INCOMPLETE_ARRAY;
125}
126
127void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
128 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000129 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000130 Code = pch::TYPE_VARIABLE_ARRAY;
131}
132
133void PCHTypeWriter::VisitVectorType(const VectorType *T) {
134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getNumElements());
136 Code = pch::TYPE_VECTOR;
137}
138
139void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
140 VisitVectorType(T);
141 Code = pch::TYPE_EXT_VECTOR;
142}
143
144void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
145 Writer.AddTypeRef(T->getResultType(), Record);
146}
147
148void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
149 VisitFunctionType(T);
150 Code = pch::TYPE_FUNCTION_NO_PROTO;
151}
152
153void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
154 VisitFunctionType(T);
155 Record.push_back(T->getNumArgs());
156 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
157 Writer.AddTypeRef(T->getArgType(I), Record);
158 Record.push_back(T->isVariadic());
159 Record.push_back(T->getTypeQuals());
160 Code = pch::TYPE_FUNCTION_PROTO;
161}
162
163void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
164 Writer.AddDeclRef(T->getDecl(), Record);
165 Code = pch::TYPE_TYPEDEF;
166}
167
168void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000169 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000170 Code = pch::TYPE_TYPEOF_EXPR;
171}
172
173void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
174 Writer.AddTypeRef(T->getUnderlyingType(), Record);
175 Code = pch::TYPE_TYPEOF;
176}
177
178void PCHTypeWriter::VisitTagType(const TagType *T) {
179 Writer.AddDeclRef(T->getDecl(), Record);
180 assert(!T->isBeingDefined() &&
181 "Cannot serialize in the middle of a type definition");
182}
183
184void PCHTypeWriter::VisitRecordType(const RecordType *T) {
185 VisitTagType(T);
186 Code = pch::TYPE_RECORD;
187}
188
189void PCHTypeWriter::VisitEnumType(const EnumType *T) {
190 VisitTagType(T);
191 Code = pch::TYPE_ENUM;
192}
193
194void
195PCHTypeWriter::VisitTemplateSpecializationType(
196 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000197 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000198 assert(false && "Cannot serialize template specialization types");
199}
200
201void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000202 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000203 assert(false && "Cannot serialize qualified name types");
204}
205
206void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
207 Writer.AddDeclRef(T->getDecl(), Record);
208 Code = pch::TYPE_OBJC_INTERFACE;
209}
210
211void
212PCHTypeWriter::VisitObjCQualifiedInterfaceType(
213 const ObjCQualifiedInterfaceType *T) {
214 VisitObjCInterfaceType(T);
215 Record.push_back(T->getNumProtocols());
216 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
217 Writer.AddDeclRef(T->getProtocol(I), Record);
218 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
219}
220
221void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
222 Record.push_back(T->getNumProtocols());
223 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
224 Writer.AddDeclRef(T->getProtocols(I), Record);
225 Code = pch::TYPE_OBJC_QUALIFIED_ID;
226}
227
228void
229PCHTypeWriter::VisitObjCQualifiedClassType(const ObjCQualifiedClassType *T) {
230 Record.push_back(T->getNumProtocols());
231 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
232 Writer.AddDeclRef(T->getProtocols(I), Record);
233 Code = pch::TYPE_OBJC_QUALIFIED_CLASS;
234}
235
236//===----------------------------------------------------------------------===//
237// Declaration serialization
238//===----------------------------------------------------------------------===//
239namespace {
240 class VISIBILITY_HIDDEN PCHDeclWriter
241 : public DeclVisitor<PCHDeclWriter, void> {
242
243 PCHWriter &Writer;
244 PCHWriter::RecordData &Record;
245
246 public:
247 pch::DeclCode Code;
248
249 PCHDeclWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
250 : Writer(Writer), Record(Record) { }
251
252 void VisitDecl(Decl *D);
253 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
254 void VisitNamedDecl(NamedDecl *D);
255 void VisitTypeDecl(TypeDecl *D);
256 void VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000257 void VisitTagDecl(TagDecl *D);
258 void VisitEnumDecl(EnumDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000259 void VisitRecordDecl(RecordDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000260 void VisitValueDecl(ValueDecl *D);
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000261 void VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000262 void VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor982365e2009-04-13 21:20:57 +0000263 void VisitFieldDecl(FieldDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000264 void VisitVarDecl(VarDecl *D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000265 void VisitParmVarDecl(ParmVarDecl *D);
266 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
Douglas Gregor2a491792009-04-13 22:49:25 +0000267 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
268 void VisitBlockDecl(BlockDecl *D);
Douglas Gregorc34897d2009-04-09 22:27:44 +0000269 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
270 uint64_t VisibleOffset);
271 };
272}
273
274void PCHDeclWriter::VisitDecl(Decl *D) {
275 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
276 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
277 Writer.AddSourceLocation(D->getLocation(), Record);
278 Record.push_back(D->isInvalidDecl());
Douglas Gregor1c507882009-04-15 21:30:51 +0000279 Record.push_back(D->hasAttrs());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000280 Record.push_back(D->isImplicit());
281 Record.push_back(D->getAccess());
282}
283
284void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
285 VisitDecl(D);
286 Code = pch::DECL_TRANSLATION_UNIT;
287}
288
289void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
290 VisitDecl(D);
291 Writer.AddDeclarationName(D->getDeclName(), Record);
292}
293
294void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
295 VisitNamedDecl(D);
296 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
297}
298
299void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
300 VisitTypeDecl(D);
301 Writer.AddTypeRef(D->getUnderlyingType(), Record);
302 Code = pch::DECL_TYPEDEF;
303}
304
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000305void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
306 VisitTypeDecl(D);
307 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
308 Record.push_back(D->isDefinition());
309 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
310}
311
312void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
313 VisitTagDecl(D);
314 Writer.AddTypeRef(D->getIntegerType(), Record);
315 Code = pch::DECL_ENUM;
316}
317
Douglas Gregor982365e2009-04-13 21:20:57 +0000318void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
319 VisitTagDecl(D);
320 Record.push_back(D->hasFlexibleArrayMember());
321 Record.push_back(D->isAnonymousStructOrUnion());
322 Code = pch::DECL_RECORD;
323}
324
Douglas Gregorc34897d2009-04-09 22:27:44 +0000325void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
326 VisitNamedDecl(D);
327 Writer.AddTypeRef(D->getType(), Record);
328}
329
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000330void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
331 VisitValueDecl(D);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000332 Record.push_back(D->getInitExpr()? 1 : 0);
333 if (D->getInitExpr())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000334 Writer.AddStmt(D->getInitExpr());
Douglas Gregor47f1b2c2009-04-13 18:14:40 +0000335 Writer.AddAPSInt(D->getInitVal(), Record);
336 Code = pch::DECL_ENUM_CONSTANT;
337}
338
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000339void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
340 VisitValueDecl(D);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000341 Record.push_back(D->isThisDeclarationADefinition());
342 if (D->isThisDeclarationADefinition())
343 Writer.AddStmt(D->getBody());
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000344 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
345 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
346 Record.push_back(D->isInline());
347 Record.push_back(D->isVirtual());
348 Record.push_back(D->isPure());
349 Record.push_back(D->inheritedPrototype());
350 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
351 Record.push_back(D->isDeleted());
352 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
353 Record.push_back(D->param_size());
354 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
355 P != PEnd; ++P)
356 Writer.AddDeclRef(*P, Record);
357 Code = pch::DECL_FUNCTION;
358}
359
Douglas Gregor982365e2009-04-13 21:20:57 +0000360void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
361 VisitValueDecl(D);
362 Record.push_back(D->isMutable());
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000363 Record.push_back(D->getBitWidth()? 1 : 0);
364 if (D->getBitWidth())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000365 Writer.AddStmt(D->getBitWidth());
Douglas Gregor982365e2009-04-13 21:20:57 +0000366 Code = pch::DECL_FIELD;
367}
368
Douglas Gregorc34897d2009-04-09 22:27:44 +0000369void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
370 VisitValueDecl(D);
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000371 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc34897d2009-04-09 22:27:44 +0000372 Record.push_back(D->isThreadSpecified());
373 Record.push_back(D->hasCXXDirectInitializer());
374 Record.push_back(D->isDeclaredInCondition());
375 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
376 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000377 Record.push_back(D->getInit()? 1 : 0);
378 if (D->getInit())
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000379 Writer.AddStmt(D->getInit());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000380 Code = pch::DECL_VAR;
381}
382
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000383void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
384 VisitVarDecl(D);
385 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000386 // FIXME: emit default argument (C++)
Douglas Gregor23ce3a52009-04-13 22:18:37 +0000387 // FIXME: why isn't the "default argument" just stored as the initializer
388 // in VarDecl?
389 Code = pch::DECL_PARM_VAR;
390}
391
392void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
393 VisitParmVarDecl(D);
394 Writer.AddTypeRef(D->getOriginalType(), Record);
395 Code = pch::DECL_ORIGINAL_PARM_VAR;
396}
397
Douglas Gregor2a491792009-04-13 22:49:25 +0000398void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
399 VisitDecl(D);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000400 Writer.AddStmt(D->getAsmString());
Douglas Gregor2a491792009-04-13 22:49:25 +0000401 Code = pch::DECL_FILE_SCOPE_ASM;
402}
403
404void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
405 VisitDecl(D);
Douglas Gregore246b742009-04-17 19:21:43 +0000406 Writer.AddStmt(D->getBody());
Douglas Gregor2a491792009-04-13 22:49:25 +0000407 Record.push_back(D->param_size());
408 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
409 P != PEnd; ++P)
410 Writer.AddDeclRef(*P, Record);
411 Code = pch::DECL_BLOCK;
412}
413
Douglas Gregorc34897d2009-04-09 22:27:44 +0000414/// \brief Emit the DeclContext part of a declaration context decl.
415///
416/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
417/// block for this declaration context is stored. May be 0 to indicate
418/// that there are no declarations stored within this context.
419///
420/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
421/// block for this declaration context is stored. May be 0 to indicate
422/// that there are no declarations visible from this context. Note
423/// that this value will not be emitted for non-primary declaration
424/// contexts.
425void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
426 uint64_t VisibleOffset) {
427 Record.push_back(LexicalOffset);
428 if (DC->getPrimaryContext() == DC)
429 Record.push_back(VisibleOffset);
430}
431
432//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000433// Statement/expression serialization
434//===----------------------------------------------------------------------===//
435namespace {
436 class VISIBILITY_HIDDEN PCHStmtWriter
437 : public StmtVisitor<PCHStmtWriter, void> {
438
439 PCHWriter &Writer;
440 PCHWriter::RecordData &Record;
441
442 public:
443 pch::StmtCode Code;
444
445 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
446 : Writer(Writer), Record(Record) { }
447
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000448 void VisitStmt(Stmt *S);
449 void VisitNullStmt(NullStmt *S);
450 void VisitCompoundStmt(CompoundStmt *S);
451 void VisitSwitchCase(SwitchCase *S);
452 void VisitCaseStmt(CaseStmt *S);
453 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000454 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000455 void VisitIfStmt(IfStmt *S);
456 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000457 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000458 void VisitDoStmt(DoStmt *S);
459 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000460 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000461 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000462 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000463 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000464 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000465 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000466 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000467 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000468 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000469 void VisitDeclRefExpr(DeclRefExpr *E);
470 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000471 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000472 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000473 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000474 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000475 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000476 void VisitUnaryOperator(UnaryOperator *E);
477 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000478 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000479 void VisitCallExpr(CallExpr *E);
480 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000481 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000482 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000483 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
484 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000485 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000486 void VisitExplicitCastExpr(ExplicitCastExpr *E);
487 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000488 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000489 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000490 void VisitInitListExpr(InitListExpr *E);
491 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
492 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000493 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000494 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000495 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000496 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
497 void VisitChooseExpr(ChooseExpr *E);
498 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000499 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000500 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000501 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000502 };
503}
504
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000505void PCHStmtWriter::VisitStmt(Stmt *S) {
506}
507
508void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
509 VisitStmt(S);
510 Writer.AddSourceLocation(S->getSemiLoc(), Record);
511 Code = pch::STMT_NULL;
512}
513
514void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
515 VisitStmt(S);
516 Record.push_back(S->size());
517 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
518 CS != CSEnd; ++CS)
519 Writer.WriteSubStmt(*CS);
520 Writer.AddSourceLocation(S->getLBracLoc(), Record);
521 Writer.AddSourceLocation(S->getRBracLoc(), Record);
522 Code = pch::STMT_COMPOUND;
523}
524
525void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
526 VisitStmt(S);
527 Record.push_back(Writer.RecordSwitchCaseID(S));
528}
529
530void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
531 VisitSwitchCase(S);
532 Writer.WriteSubStmt(S->getLHS());
533 Writer.WriteSubStmt(S->getRHS());
534 Writer.WriteSubStmt(S->getSubStmt());
535 Writer.AddSourceLocation(S->getCaseLoc(), Record);
536 Code = pch::STMT_CASE;
537}
538
539void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
540 VisitSwitchCase(S);
541 Writer.WriteSubStmt(S->getSubStmt());
542 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
543 Code = pch::STMT_DEFAULT;
544}
545
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000546void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
547 VisitStmt(S);
548 Writer.AddIdentifierRef(S->getID(), Record);
549 Writer.WriteSubStmt(S->getSubStmt());
550 Writer.AddSourceLocation(S->getIdentLoc(), Record);
551 Record.push_back(Writer.GetLabelID(S));
552 Code = pch::STMT_LABEL;
553}
554
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000555void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
556 VisitStmt(S);
557 Writer.WriteSubStmt(S->getCond());
558 Writer.WriteSubStmt(S->getThen());
559 Writer.WriteSubStmt(S->getElse());
560 Writer.AddSourceLocation(S->getIfLoc(), Record);
561 Code = pch::STMT_IF;
562}
563
564void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
565 VisitStmt(S);
566 Writer.WriteSubStmt(S->getCond());
567 Writer.WriteSubStmt(S->getBody());
568 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
569 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
570 SC = SC->getNextSwitchCase())
571 Record.push_back(Writer.getSwitchCaseID(SC));
572 Code = pch::STMT_SWITCH;
573}
574
Douglas Gregora6b503f2009-04-17 00:16:09 +0000575void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
576 VisitStmt(S);
577 Writer.WriteSubStmt(S->getCond());
578 Writer.WriteSubStmt(S->getBody());
579 Writer.AddSourceLocation(S->getWhileLoc(), Record);
580 Code = pch::STMT_WHILE;
581}
582
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000583void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
584 VisitStmt(S);
585 Writer.WriteSubStmt(S->getCond());
586 Writer.WriteSubStmt(S->getBody());
587 Writer.AddSourceLocation(S->getDoLoc(), Record);
588 Code = pch::STMT_DO;
589}
590
591void PCHStmtWriter::VisitForStmt(ForStmt *S) {
592 VisitStmt(S);
593 Writer.WriteSubStmt(S->getInit());
594 Writer.WriteSubStmt(S->getCond());
595 Writer.WriteSubStmt(S->getInc());
596 Writer.WriteSubStmt(S->getBody());
597 Writer.AddSourceLocation(S->getForLoc(), Record);
598 Code = pch::STMT_FOR;
599}
600
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000601void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
602 VisitStmt(S);
603 Record.push_back(Writer.GetLabelID(S->getLabel()));
604 Writer.AddSourceLocation(S->getGotoLoc(), Record);
605 Writer.AddSourceLocation(S->getLabelLoc(), Record);
606 Code = pch::STMT_GOTO;
607}
608
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000609void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
610 VisitStmt(S);
611 Writer.WriteSubStmt(S->getTarget());
612 Code = pch::STMT_INDIRECT_GOTO;
613}
614
Douglas Gregora6b503f2009-04-17 00:16:09 +0000615void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
616 VisitStmt(S);
617 Writer.AddSourceLocation(S->getContinueLoc(), Record);
618 Code = pch::STMT_CONTINUE;
619}
620
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000621void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
622 VisitStmt(S);
623 Writer.AddSourceLocation(S->getBreakLoc(), Record);
624 Code = pch::STMT_BREAK;
625}
626
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000627void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
628 VisitStmt(S);
629 Writer.WriteSubStmt(S->getRetValue());
630 Writer.AddSourceLocation(S->getReturnLoc(), Record);
631 Code = pch::STMT_RETURN;
632}
633
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000634void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
635 VisitStmt(S);
636 Writer.AddSourceLocation(S->getStartLoc(), Record);
637 Writer.AddSourceLocation(S->getEndLoc(), Record);
638 DeclGroupRef DG = S->getDeclGroup();
639 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
640 Writer.AddDeclRef(*D, Record);
641 Code = pch::STMT_DECL;
642}
643
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000644void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
645 VisitStmt(S);
646 Record.push_back(S->getNumOutputs());
647 Record.push_back(S->getNumInputs());
648 Record.push_back(S->getNumClobbers());
649 Writer.AddSourceLocation(S->getAsmLoc(), Record);
650 Writer.AddSourceLocation(S->getRParenLoc(), Record);
651 Record.push_back(S->isVolatile());
652 Record.push_back(S->isSimple());
653 Writer.WriteSubStmt(S->getAsmString());
654
655 // Outputs
656 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
657 Writer.AddString(S->getOutputName(I), Record);
658 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
659 Writer.WriteSubStmt(S->getOutputExpr(I));
660 }
661
662 // Inputs
663 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
664 Writer.AddString(S->getInputName(I), Record);
665 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
666 Writer.WriteSubStmt(S->getInputExpr(I));
667 }
668
669 // Clobbers
670 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
671 Writer.WriteSubStmt(S->getClobber(I));
672
673 Code = pch::STMT_ASM;
674}
675
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000676void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000677 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000678 Writer.AddTypeRef(E->getType(), Record);
679 Record.push_back(E->isTypeDependent());
680 Record.push_back(E->isValueDependent());
681}
682
Douglas Gregore2f37202009-04-14 21:55:33 +0000683void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
684 VisitExpr(E);
685 Writer.AddSourceLocation(E->getLocation(), Record);
686 Record.push_back(E->getIdentType()); // FIXME: stable encoding
687 Code = pch::EXPR_PREDEFINED;
688}
689
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000690void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
691 VisitExpr(E);
692 Writer.AddDeclRef(E->getDecl(), Record);
693 Writer.AddSourceLocation(E->getLocation(), Record);
694 Code = pch::EXPR_DECL_REF;
695}
696
697void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
698 VisitExpr(E);
699 Writer.AddSourceLocation(E->getLocation(), Record);
700 Writer.AddAPInt(E->getValue(), Record);
701 Code = pch::EXPR_INTEGER_LITERAL;
702}
703
Douglas Gregore2f37202009-04-14 21:55:33 +0000704void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
705 VisitExpr(E);
706 Writer.AddAPFloat(E->getValue(), Record);
707 Record.push_back(E->isExact());
708 Writer.AddSourceLocation(E->getLocation(), Record);
709 Code = pch::EXPR_FLOATING_LITERAL;
710}
711
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000712void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
713 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000714 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000715 Code = pch::EXPR_IMAGINARY_LITERAL;
716}
717
Douglas Gregor596e0932009-04-15 16:35:07 +0000718void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
719 VisitExpr(E);
720 Record.push_back(E->getByteLength());
721 Record.push_back(E->getNumConcatenated());
722 Record.push_back(E->isWide());
723 // FIXME: String data should be stored as a blob at the end of the
724 // StringLiteral. However, we can't do so now because we have no
725 // provision for coping with abbreviations when we're jumping around
726 // the PCH file during deserialization.
727 Record.insert(Record.end(),
728 E->getStrData(), E->getStrData() + E->getByteLength());
729 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
730 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
731 Code = pch::EXPR_STRING_LITERAL;
732}
733
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000734void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
735 VisitExpr(E);
736 Record.push_back(E->getValue());
737 Writer.AddSourceLocation(E->getLoc(), Record);
738 Record.push_back(E->isWide());
739 Code = pch::EXPR_CHARACTER_LITERAL;
740}
741
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000742void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
743 VisitExpr(E);
744 Writer.AddSourceLocation(E->getLParen(), Record);
745 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000746 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000747 Code = pch::EXPR_PAREN;
748}
749
Douglas Gregor12d74052009-04-15 15:58:59 +0000750void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
751 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000752 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000753 Record.push_back(E->getOpcode()); // FIXME: stable encoding
754 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
755 Code = pch::EXPR_UNARY_OPERATOR;
756}
757
758void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
759 VisitExpr(E);
760 Record.push_back(E->isSizeOf());
761 if (E->isArgumentType())
762 Writer.AddTypeRef(E->getArgumentType(), Record);
763 else {
764 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000765 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000766 }
767 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
768 Writer.AddSourceLocation(E->getRParenLoc(), Record);
769 Code = pch::EXPR_SIZEOF_ALIGN_OF;
770}
771
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000772void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
773 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000774 Writer.WriteSubStmt(E->getLHS());
775 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000776 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
777 Code = pch::EXPR_ARRAY_SUBSCRIPT;
778}
779
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000780void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
781 VisitExpr(E);
782 Record.push_back(E->getNumArgs());
783 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000784 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000785 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
786 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000787 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000788 Code = pch::EXPR_CALL;
789}
790
791void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
792 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000793 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000794 Writer.AddDeclRef(E->getMemberDecl(), Record);
795 Writer.AddSourceLocation(E->getMemberLoc(), Record);
796 Record.push_back(E->isArrow());
797 Code = pch::EXPR_MEMBER;
798}
799
Douglas Gregora151ba42009-04-14 23:32:43 +0000800void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
801 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000802 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000803}
804
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000805void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
806 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000807 Writer.WriteSubStmt(E->getLHS());
808 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000809 Record.push_back(E->getOpcode()); // FIXME: stable encoding
810 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
811 Code = pch::EXPR_BINARY_OPERATOR;
812}
813
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000814void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
815 VisitBinaryOperator(E);
816 Writer.AddTypeRef(E->getComputationLHSType(), Record);
817 Writer.AddTypeRef(E->getComputationResultType(), Record);
818 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
819}
820
821void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
822 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000823 Writer.WriteSubStmt(E->getCond());
824 Writer.WriteSubStmt(E->getLHS());
825 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000826 Code = pch::EXPR_CONDITIONAL_OPERATOR;
827}
828
Douglas Gregora151ba42009-04-14 23:32:43 +0000829void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
830 VisitCastExpr(E);
831 Record.push_back(E->isLvalueCast());
832 Code = pch::EXPR_IMPLICIT_CAST;
833}
834
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000835void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
836 VisitCastExpr(E);
837 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
838}
839
840void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
841 VisitExplicitCastExpr(E);
842 Writer.AddSourceLocation(E->getLParenLoc(), Record);
843 Writer.AddSourceLocation(E->getRParenLoc(), Record);
844 Code = pch::EXPR_CSTYLE_CAST;
845}
846
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000847void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
848 VisitExpr(E);
849 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000850 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000851 Record.push_back(E->isFileScope());
852 Code = pch::EXPR_COMPOUND_LITERAL;
853}
854
Douglas Gregorec0b8292009-04-15 23:02:49 +0000855void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
856 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000857 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000858 Writer.AddIdentifierRef(&E->getAccessor(), Record);
859 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
860 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
861}
862
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000863void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
864 VisitExpr(E);
865 Record.push_back(E->getNumInits());
866 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000867 Writer.WriteSubStmt(E->getInit(I));
868 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000869 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
870 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
871 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
872 Record.push_back(E->hadArrayRangeDesignator());
873 Code = pch::EXPR_INIT_LIST;
874}
875
876void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
877 VisitExpr(E);
878 Record.push_back(E->getNumSubExprs());
879 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000880 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000881 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
882 Record.push_back(E->usesGNUSyntax());
883 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
884 DEnd = E->designators_end();
885 D != DEnd; ++D) {
886 if (D->isFieldDesignator()) {
887 if (FieldDecl *Field = D->getField()) {
888 Record.push_back(pch::DESIG_FIELD_DECL);
889 Writer.AddDeclRef(Field, Record);
890 } else {
891 Record.push_back(pch::DESIG_FIELD_NAME);
892 Writer.AddIdentifierRef(D->getFieldName(), Record);
893 }
894 Writer.AddSourceLocation(D->getDotLoc(), Record);
895 Writer.AddSourceLocation(D->getFieldLoc(), Record);
896 } else if (D->isArrayDesignator()) {
897 Record.push_back(pch::DESIG_ARRAY);
898 Record.push_back(D->getFirstExprIndex());
899 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
900 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
901 } else {
902 assert(D->isArrayRangeDesignator() && "Unknown designator");
903 Record.push_back(pch::DESIG_ARRAY_RANGE);
904 Record.push_back(D->getFirstExprIndex());
905 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
906 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
907 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
908 }
909 }
910 Code = pch::EXPR_DESIGNATED_INIT;
911}
912
913void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
914 VisitExpr(E);
915 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
916}
917
Douglas Gregorec0b8292009-04-15 23:02:49 +0000918void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
919 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000920 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000921 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
922 Writer.AddSourceLocation(E->getRParenLoc(), Record);
923 Code = pch::EXPR_VA_ARG;
924}
925
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000926void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
927 VisitExpr(E);
928 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
929 Writer.AddSourceLocation(E->getLabelLoc(), Record);
930 Record.push_back(Writer.GetLabelID(E->getLabel()));
931 Code = pch::EXPR_ADDR_LABEL;
932}
933
Douglas Gregoreca12f62009-04-17 19:05:30 +0000934void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
935 VisitExpr(E);
936 Writer.WriteSubStmt(E->getSubStmt());
937 Writer.AddSourceLocation(E->getLParenLoc(), Record);
938 Writer.AddSourceLocation(E->getRParenLoc(), Record);
939 Code = pch::EXPR_STMT;
940}
941
Douglas Gregor209d4622009-04-15 23:33:31 +0000942void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
943 VisitExpr(E);
944 Writer.AddTypeRef(E->getArgType1(), Record);
945 Writer.AddTypeRef(E->getArgType2(), Record);
946 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
947 Writer.AddSourceLocation(E->getRParenLoc(), Record);
948 Code = pch::EXPR_TYPES_COMPATIBLE;
949}
950
951void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
952 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000953 Writer.WriteSubStmt(E->getCond());
954 Writer.WriteSubStmt(E->getLHS());
955 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000956 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
957 Writer.AddSourceLocation(E->getRParenLoc(), Record);
958 Code = pch::EXPR_CHOOSE;
959}
960
961void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
962 VisitExpr(E);
963 Writer.AddSourceLocation(E->getTokenLocation(), Record);
964 Code = pch::EXPR_GNU_NULL;
965}
966
Douglas Gregor725e94b2009-04-16 00:01:45 +0000967void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
968 VisitExpr(E);
969 Record.push_back(E->getNumSubExprs());
970 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000971 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000972 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
973 Writer.AddSourceLocation(E->getRParenLoc(), Record);
974 Code = pch::EXPR_SHUFFLE_VECTOR;
975}
976
Douglas Gregore246b742009-04-17 19:21:43 +0000977void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
978 VisitExpr(E);
979 Writer.AddDeclRef(E->getBlockDecl(), Record);
980 Record.push_back(E->hasBlockDeclRefExprs());
981 Code = pch::EXPR_BLOCK;
982}
983
Douglas Gregor725e94b2009-04-16 00:01:45 +0000984void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
985 VisitExpr(E);
986 Writer.AddDeclRef(E->getDecl(), Record);
987 Writer.AddSourceLocation(E->getLocation(), Record);
988 Record.push_back(E->isByRef());
989 Code = pch::EXPR_BLOCK_DECL_REF;
990}
991
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000992//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000993// PCHWriter Implementation
994//===----------------------------------------------------------------------===//
995
Douglas Gregorb5887f32009-04-10 21:16:55 +0000996/// \brief Write the target triple (e.g., i686-apple-darwin9).
997void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
998 using namespace llvm;
999 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1000 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001002 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001003
1004 RecordData Record;
1005 Record.push_back(pch::TARGET_TRIPLE);
1006 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001007 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001008}
1009
1010/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001011void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1012 RecordData Record;
1013 Record.push_back(LangOpts.Trigraphs);
1014 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1015 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1016 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1017 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1018 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1019 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1020 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1021 Record.push_back(LangOpts.C99); // C99 Support
1022 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1023 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1024 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1025 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1026 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1027
1028 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1029 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1030 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1031
1032 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1033 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1034 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1035 Record.push_back(LangOpts.LaxVectorConversions);
1036 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1037
1038 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1039 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1040 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1041
1042 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1043 // by locks.
1044 Record.push_back(LangOpts.Blocks); // block extension to C
1045 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1046 // they are unused.
1047 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1048 // (modulo the platform support).
1049
1050 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1051 // signed integer arithmetic overflows.
1052
1053 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1054 // may be ripped out at any time.
1055
1056 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1057 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1058 // defined.
1059 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1060 // opposed to __DYNAMIC__).
1061 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1062
1063 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1064 // used (instead of C99 semantics).
1065 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1066 Record.push_back(LangOpts.getGCMode());
1067 Record.push_back(LangOpts.getVisibilityMode());
1068 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001069 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001070}
1071
Douglas Gregorab1cef72009-04-10 03:52:48 +00001072//===----------------------------------------------------------------------===//
1073// Source Manager Serialization
1074//===----------------------------------------------------------------------===//
1075
1076/// \brief Create an abbreviation for the SLocEntry that refers to a
1077/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001078static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001079 using namespace llvm;
1080 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1081 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001087 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001088}
1089
1090/// \brief Create an abbreviation for the SLocEntry that refers to a
1091/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001092static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001093 using namespace llvm;
1094 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1095 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001101 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001102}
1103
1104/// \brief Create an abbreviation for the SLocEntry that refers to a
1105/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001106static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001107 using namespace llvm;
1108 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1109 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1110 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001111 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001112}
1113
1114/// \brief Create an abbreviation for the SLocEntry that refers to an
1115/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001116static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001117 using namespace llvm;
1118 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1119 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001125 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001126}
1127
1128/// \brief Writes the block containing the serialized form of the
1129/// source manager.
1130///
1131/// TODO: We should probably use an on-disk hash table (stored in a
1132/// blob), indexed based on the file name, so that we only create
1133/// entries for files that we actually need. In the common case (no
1134/// errors), we probably won't have to create file entries for any of
1135/// the files in the AST.
1136void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001137 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001138 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001139
1140 // Abbreviations for the various kinds of source-location entries.
1141 int SLocFileAbbrv = -1;
1142 int SLocBufferAbbrv = -1;
1143 int SLocBufferBlobAbbrv = -1;
1144 int SLocInstantiationAbbrv = -1;
1145
1146 // Write out the source location entry table. We skip the first
1147 // entry, which is always the same dummy entry.
1148 RecordData Record;
1149 for (SourceManager::sloc_entry_iterator
1150 SLoc = SourceMgr.sloc_entry_begin() + 1,
1151 SLocEnd = SourceMgr.sloc_entry_end();
1152 SLoc != SLocEnd; ++SLoc) {
1153 // Figure out which record code to use.
1154 unsigned Code;
1155 if (SLoc->isFile()) {
1156 if (SLoc->getFile().getContentCache()->Entry)
1157 Code = pch::SM_SLOC_FILE_ENTRY;
1158 else
1159 Code = pch::SM_SLOC_BUFFER_ENTRY;
1160 } else
1161 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1162 Record.push_back(Code);
1163
1164 Record.push_back(SLoc->getOffset());
1165 if (SLoc->isFile()) {
1166 const SrcMgr::FileInfo &File = SLoc->getFile();
1167 Record.push_back(File.getIncludeLoc().getRawEncoding());
1168 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001169 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001170
1171 const SrcMgr::ContentCache *Content = File.getContentCache();
1172 if (Content->Entry) {
1173 // The source location entry is a file. The blob associated
1174 // with this entry is the file name.
1175 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001176 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1177 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001178 Content->Entry->getName(),
1179 strlen(Content->Entry->getName()));
1180 } else {
1181 // The source location entry is a buffer. The blob associated
1182 // with this entry contains the contents of the buffer.
1183 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001184 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1185 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001186 }
1187
1188 // We add one to the size so that we capture the trailing NULL
1189 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1190 // the reader side).
1191 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1192 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001193 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001194 Record.clear();
1195 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001196 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001197 Buffer->getBufferStart(),
1198 Buffer->getBufferSize() + 1);
1199 }
1200 } else {
1201 // The source location entry is an instantiation.
1202 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1203 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1204 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1205 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1206
Douglas Gregor364e5802009-04-15 18:05:10 +00001207 // Compute the token length for this macro expansion.
1208 unsigned NextOffset = SourceMgr.getNextOffset();
1209 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1210 if (++NextSLoc != SLocEnd)
1211 NextOffset = NextSLoc->getOffset();
1212 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1213
Douglas Gregorab1cef72009-04-10 03:52:48 +00001214 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001215 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1216 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001217 }
1218
1219 Record.clear();
1220 }
1221
Douglas Gregor635f97f2009-04-13 16:31:14 +00001222 // Write the line table.
1223 if (SourceMgr.hasLineTable()) {
1224 LineTableInfo &LineTable = SourceMgr.getLineTable();
1225
1226 // Emit the file names
1227 Record.push_back(LineTable.getNumFilenames());
1228 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1229 // Emit the file name
1230 const char *Filename = LineTable.getFilename(I);
1231 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1232 Record.push_back(FilenameLen);
1233 if (FilenameLen)
1234 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1235 }
1236
1237 // Emit the line entries
1238 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1239 L != LEnd; ++L) {
1240 // Emit the file ID
1241 Record.push_back(L->first);
1242
1243 // Emit the line entries
1244 Record.push_back(L->second.size());
1245 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1246 LEEnd = L->second.end();
1247 LE != LEEnd; ++LE) {
1248 Record.push_back(LE->FileOffset);
1249 Record.push_back(LE->LineNo);
1250 Record.push_back(LE->FilenameID);
1251 Record.push_back((unsigned)LE->FileKind);
1252 Record.push_back(LE->IncludeOffset);
1253 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001254 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001255 }
1256 }
1257
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001258 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001259}
1260
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001261/// \brief Writes the block containing the serialized form of the
1262/// preprocessor.
1263///
Chris Lattner850eabd2009-04-10 18:08:30 +00001264void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001265 // Enter the preprocessor block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001266 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 3);
Chris Lattner84b04f12009-04-10 17:16:57 +00001267
Chris Lattner1b094952009-04-10 18:00:12 +00001268 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1269 // FIXME: use diagnostics subsystem for localization etc.
1270 if (PP.SawDateOrTime())
1271 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Chris Lattner84b04f12009-04-10 17:16:57 +00001272
Chris Lattner1b094952009-04-10 18:00:12 +00001273 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001274
Chris Lattner4b21c202009-04-13 01:29:17 +00001275 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1276 if (PP.getCounterValue() != 0) {
1277 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001278 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001279 Record.clear();
1280 }
1281
Chris Lattner1b094952009-04-10 18:00:12 +00001282 // Loop over all the macro definitions that are live at the end of the file,
1283 // emitting each to the PP section.
1284 // FIXME: Eventually we want to emit an index so that we can lazily load
1285 // macros.
1286 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1287 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001288 // FIXME: This emits macros in hash table order, we should do it in a stable
1289 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001290 MacroInfo *MI = I->second;
1291
1292 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1293 // been redefined by the header (in which case they are not isBuiltinMacro).
1294 if (MI->isBuiltinMacro())
1295 continue;
1296
Chris Lattner29241862009-04-11 21:15:38 +00001297 AddIdentifierRef(I->first, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001298 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1299 Record.push_back(MI->isUsed());
1300
1301 unsigned Code;
1302 if (MI->isObjectLike()) {
1303 Code = pch::PP_MACRO_OBJECT_LIKE;
1304 } else {
1305 Code = pch::PP_MACRO_FUNCTION_LIKE;
1306
1307 Record.push_back(MI->isC99Varargs());
1308 Record.push_back(MI->isGNUVarargs());
1309 Record.push_back(MI->getNumArgs());
1310 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1311 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001312 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001313 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001314 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001315 Record.clear();
1316
Chris Lattner850eabd2009-04-10 18:08:30 +00001317 // Emit the tokens array.
1318 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1319 // Note that we know that the preprocessor does not have any annotation
1320 // tokens in it because they are created by the parser, and thus can't be
1321 // in a macro definition.
1322 const Token &Tok = MI->getReplacementToken(TokNo);
1323
1324 Record.push_back(Tok.getLocation().getRawEncoding());
1325 Record.push_back(Tok.getLength());
1326
Chris Lattner850eabd2009-04-10 18:08:30 +00001327 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1328 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001329 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001330
1331 // FIXME: Should translate token kind to a stable encoding.
1332 Record.push_back(Tok.getKind());
1333 // FIXME: Should translate token flags to a stable encoding.
1334 Record.push_back(Tok.getFlags());
1335
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001336 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001337 Record.clear();
1338 }
Chris Lattner1b094952009-04-10 18:00:12 +00001339
1340 }
1341
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001342 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001343}
1344
1345
Douglas Gregorc34897d2009-04-09 22:27:44 +00001346/// \brief Write the representation of a type to the PCH stream.
1347void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001348 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001349 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001350 ID = NextTypeID++;
1351
1352 // Record the offset for this type.
1353 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001354 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001355 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1356 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001357 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001358 }
1359
1360 RecordData Record;
1361
1362 // Emit the type's representation.
1363 PCHTypeWriter W(*this, Record);
1364 switch (T->getTypeClass()) {
1365 // For all of the concrete, non-dependent types, call the
1366 // appropriate visitor function.
1367#define TYPE(Class, Base) \
1368 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1369#define ABSTRACT_TYPE(Class, Base)
1370#define DEPENDENT_TYPE(Class, Base)
1371#include "clang/AST/TypeNodes.def"
1372
1373 // For all of the dependent type nodes (which only occur in C++
1374 // templates), produce an error.
1375#define TYPE(Class, Base)
1376#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1377#include "clang/AST/TypeNodes.def"
1378 assert(false && "Cannot serialize dependent type nodes");
1379 break;
1380 }
1381
1382 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001383 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001384
1385 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001386 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001387}
1388
1389/// \brief Write a block containing all of the types.
1390void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001391 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001392 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001393
1394 // Emit all of the types in the ASTContext
1395 for (std::vector<Type*>::const_iterator T = Context.getTypes().begin(),
1396 TEnd = Context.getTypes().end();
1397 T != TEnd; ++T) {
1398 // Builtin types are never serialized.
1399 if (isa<BuiltinType>(*T))
1400 continue;
1401
1402 WriteType(*T);
1403 }
1404
1405 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001406 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001407}
1408
1409/// \brief Write the block containing all of the declaration IDs
1410/// lexically declared within the given DeclContext.
1411///
1412/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1413/// bistream, or 0 if no block was written.
1414uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1415 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001416 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001417 return 0;
1418
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001419 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001420 RecordData Record;
1421 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1422 DEnd = DC->decls_end(Context);
1423 D != DEnd; ++D)
1424 AddDeclRef(*D, Record);
1425
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001426 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001427 return Offset;
1428}
1429
1430/// \brief Write the block containing all of the declaration IDs
1431/// visible from the given DeclContext.
1432///
1433/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1434/// bistream, or 0 if no block was written.
1435uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1436 DeclContext *DC) {
1437 if (DC->getPrimaryContext() != DC)
1438 return 0;
1439
1440 // Force the DeclContext to build a its name-lookup table.
1441 DC->lookup(Context, DeclarationName());
1442
1443 // Serialize the contents of the mapping used for lookup. Note that,
1444 // although we have two very different code paths, the serialized
1445 // representation is the same for both cases: a declaration name,
1446 // followed by a size, followed by references to the visible
1447 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001448 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001449 RecordData Record;
1450 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001451 if (!Map)
1452 return 0;
1453
Douglas Gregorc34897d2009-04-09 22:27:44 +00001454 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1455 D != DEnd; ++D) {
1456 AddDeclarationName(D->first, Record);
1457 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1458 Record.push_back(Result.second - Result.first);
1459 for(; Result.first != Result.second; ++Result.first)
1460 AddDeclRef(*Result.first, Record);
1461 }
1462
1463 if (Record.size() == 0)
1464 return 0;
1465
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001466 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001467 return Offset;
1468}
1469
1470/// \brief Write a block containing all of the declarations.
1471void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001472 // Enter the declarations block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001473 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001474
1475 // Emit all of the declarations.
1476 RecordData Record;
1477 PCHDeclWriter W(*this, Record);
1478 while (!DeclsToEmit.empty()) {
1479 // Pull the next declaration off the queue
1480 Decl *D = DeclsToEmit.front();
1481 DeclsToEmit.pop();
1482
1483 // If this declaration is also a DeclContext, write blocks for the
1484 // declarations that lexically stored inside its context and those
1485 // declarations that are visible from its context. These blocks
1486 // are written before the declaration itself so that we can put
1487 // their offsets into the record for the declaration.
1488 uint64_t LexicalOffset = 0;
1489 uint64_t VisibleOffset = 0;
1490 DeclContext *DC = dyn_cast<DeclContext>(D);
1491 if (DC) {
1492 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1493 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1494 }
1495
1496 // Determine the ID for this declaration
Douglas Gregorac8f2802009-04-10 17:25:41 +00001497 pch::DeclID ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001498 if (ID == 0)
1499 ID = DeclIDs.size();
1500
1501 unsigned Index = ID - 1;
1502
1503 // Record the offset for this declaration
1504 if (DeclOffsets.size() == Index)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001505 DeclOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001506 else if (DeclOffsets.size() < Index) {
1507 DeclOffsets.resize(Index+1);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001508 DeclOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001509 }
1510
1511 // Build and emit a record for this declaration
1512 Record.clear();
1513 W.Code = (pch::DeclCode)0;
1514 W.Visit(D);
1515 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001516 assert(W.Code && "Unhandled declaration kind while generating PCH");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001517 Stream.EmitRecord(W.Code, Record);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001518
Douglas Gregor1c507882009-04-15 21:30:51 +00001519 // If the declaration had any attributes, write them now.
1520 if (D->hasAttrs())
1521 WriteAttributeRecord(D->getAttrs());
1522
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001523 // Flush any expressions that were written as part of this declaration.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001524 FlushStmts();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001525
Douglas Gregor631f6c62009-04-14 00:24:19 +00001526 // Note external declarations so that we can add them to a record
1527 // in the PCH file later.
1528 if (isa<FileScopeAsmDecl>(D))
1529 ExternalDefinitions.push_back(ID);
1530 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
1531 if (// Non-static file-scope variables with initializers or that
1532 // are tentative definitions.
1533 (Var->isFileVarDecl() &&
1534 (Var->getInit() || Var->getStorageClass() == VarDecl::None)) ||
1535 // Out-of-line definitions of static data members (C++).
1536 (Var->getDeclContext()->isRecord() &&
1537 !Var->getLexicalDeclContext()->isRecord() &&
1538 Var->getStorageClass() == VarDecl::Static))
1539 ExternalDefinitions.push_back(ID);
1540 } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +00001541 if (Func->isThisDeclarationADefinition())
Douglas Gregor631f6c62009-04-14 00:24:19 +00001542 ExternalDefinitions.push_back(ID);
1543 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001544 }
1545
1546 // Exit the declarations block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001547 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001548}
1549
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001550/// \brief Write the identifier table into the PCH file.
1551///
1552/// The identifier table consists of a blob containing string data
1553/// (the actual identifiers themselves) and a separate "offsets" index
1554/// that maps identifier IDs to locations within the blob.
1555void PCHWriter::WriteIdentifierTable() {
1556 using namespace llvm;
1557
1558 // Create and write out the blob that contains the identifier
1559 // strings.
1560 RecordData IdentOffsets;
1561 IdentOffsets.resize(IdentifierIDs.size());
1562 {
1563 // Create the identifier string data.
1564 std::vector<char> Data;
1565 Data.push_back(0); // Data must not be empty.
1566 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1567 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1568 ID != IDEnd; ++ID) {
1569 assert(ID->first && "NULL identifier in identifier table");
1570
1571 // Make sure we're starting on an odd byte. The PCH reader
1572 // expects the low bit to be set on all of the offsets.
1573 if ((Data.size() & 0x01) == 0)
1574 Data.push_back((char)0);
1575
1576 IdentOffsets[ID->second - 1] = Data.size();
1577 Data.insert(Data.end(),
1578 ID->first->getName(),
1579 ID->first->getName() + ID->first->getLength());
1580 Data.push_back((char)0);
1581 }
1582
1583 // Create a blob abbreviation
1584 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1585 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001587 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001588
1589 // Write the identifier table
1590 RecordData Record;
1591 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001592 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, &Data.front(), Data.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001593 }
1594
1595 // Write the offsets table for identifier IDs.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001596 Stream.EmitRecord(pch::IDENTIFIER_OFFSET, IdentOffsets);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001597}
1598
Douglas Gregor1c507882009-04-15 21:30:51 +00001599/// \brief Write a record containing the given attributes.
1600void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
1601 RecordData Record;
1602 for (; Attr; Attr = Attr->getNext()) {
1603 Record.push_back(Attr->getKind()); // FIXME: stable encoding
1604 Record.push_back(Attr->isInherited());
1605 switch (Attr->getKind()) {
1606 case Attr::Alias:
1607 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
1608 break;
1609
1610 case Attr::Aligned:
1611 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
1612 break;
1613
1614 case Attr::AlwaysInline:
1615 break;
1616
1617 case Attr::AnalyzerNoReturn:
1618 break;
1619
1620 case Attr::Annotate:
1621 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
1622 break;
1623
1624 case Attr::AsmLabel:
1625 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
1626 break;
1627
1628 case Attr::Blocks:
1629 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
1630 break;
1631
1632 case Attr::Cleanup:
1633 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
1634 break;
1635
1636 case Attr::Const:
1637 break;
1638
1639 case Attr::Constructor:
1640 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
1641 break;
1642
1643 case Attr::DLLExport:
1644 case Attr::DLLImport:
1645 case Attr::Deprecated:
1646 break;
1647
1648 case Attr::Destructor:
1649 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
1650 break;
1651
1652 case Attr::FastCall:
1653 break;
1654
1655 case Attr::Format: {
1656 const FormatAttr *Format = cast<FormatAttr>(Attr);
1657 AddString(Format->getType(), Record);
1658 Record.push_back(Format->getFormatIdx());
1659 Record.push_back(Format->getFirstArg());
1660 break;
1661 }
1662
1663 case Attr::GNUCInline:
1664 case Attr::IBOutletKind:
1665 case Attr::NoReturn:
1666 case Attr::NoThrow:
1667 case Attr::Nodebug:
1668 case Attr::Noinline:
1669 break;
1670
1671 case Attr::NonNull: {
1672 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
1673 Record.push_back(NonNull->size());
1674 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
1675 break;
1676 }
1677
1678 case Attr::ObjCException:
1679 case Attr::ObjCNSObject:
1680 case Attr::Overloadable:
1681 break;
1682
1683 case Attr::Packed:
1684 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
1685 break;
1686
1687 case Attr::Pure:
1688 break;
1689
1690 case Attr::Regparm:
1691 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
1692 break;
1693
1694 case Attr::Section:
1695 AddString(cast<SectionAttr>(Attr)->getName(), Record);
1696 break;
1697
1698 case Attr::StdCall:
1699 case Attr::TransparentUnion:
1700 case Attr::Unavailable:
1701 case Attr::Unused:
1702 case Attr::Used:
1703 break;
1704
1705 case Attr::Visibility:
1706 // FIXME: stable encoding
1707 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
1708 break;
1709
1710 case Attr::WarnUnusedResult:
1711 case Attr::Weak:
1712 case Attr::WeakImport:
1713 break;
1714 }
1715 }
1716
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001717 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00001718}
1719
1720void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
1721 Record.push_back(Str.size());
1722 Record.insert(Record.end(), Str.begin(), Str.end());
1723}
1724
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001725PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
1726 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00001727
Chris Lattner850eabd2009-04-10 18:08:30 +00001728void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
Douglas Gregorc34897d2009-04-09 22:27:44 +00001729 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001730 Stream.Emit((unsigned)'C', 8);
1731 Stream.Emit((unsigned)'P', 8);
1732 Stream.Emit((unsigned)'C', 8);
1733 Stream.Emit((unsigned)'H', 8);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001734
1735 // The translation unit is the first declaration we'll emit.
1736 DeclIDs[Context.getTranslationUnitDecl()] = 1;
1737 DeclsToEmit.push(Context.getTranslationUnitDecl());
1738
1739 // Write the remaining PCH contents.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001740 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 3);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001741 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001742 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001743 WriteSourceManagerBlock(Context.getSourceManager());
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001744 WritePreprocessor(PP);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001745 WriteTypesBlock(Context);
1746 WriteDeclsBlock(Context);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001747 WriteIdentifierTable();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001748 Stream.EmitRecord(pch::TYPE_OFFSET, TypeOffsets);
1749 Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
Douglas Gregor631f6c62009-04-14 00:24:19 +00001750 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001751 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
1752 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001753}
1754
1755void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
1756 Record.push_back(Loc.getRawEncoding());
1757}
1758
1759void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
1760 Record.push_back(Value.getBitWidth());
1761 unsigned N = Value.getNumWords();
1762 const uint64_t* Words = Value.getRawData();
1763 for (unsigned I = 0; I != N; ++I)
1764 Record.push_back(Words[I]);
1765}
1766
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00001767void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
1768 Record.push_back(Value.isUnsigned());
1769 AddAPInt(Value, Record);
1770}
1771
Douglas Gregore2f37202009-04-14 21:55:33 +00001772void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
1773 AddAPInt(Value.bitcastToAPInt(), Record);
1774}
1775
Douglas Gregorc34897d2009-04-09 22:27:44 +00001776void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001777 if (II == 0) {
1778 Record.push_back(0);
1779 return;
1780 }
1781
1782 pch::IdentID &ID = IdentifierIDs[II];
1783 if (ID == 0)
1784 ID = IdentifierIDs.size();
1785
1786 Record.push_back(ID);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001787}
1788
1789void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
1790 if (T.isNull()) {
1791 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
1792 return;
1793 }
1794
1795 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00001796 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001797 switch (BT->getKind()) {
1798 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
1799 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
1800 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
1801 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
1802 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
1803 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
1804 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
1805 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
1806 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
1807 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
1808 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
1809 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
1810 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
1811 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
1812 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
1813 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
1814 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
1815 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
1816 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
1817 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
1818 }
1819
1820 Record.push_back((ID << 3) | T.getCVRQualifiers());
1821 return;
1822 }
1823
Douglas Gregorac8f2802009-04-10 17:25:41 +00001824 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001825 if (ID == 0) // we haven't seen this type before
1826 ID = NextTypeID++;
1827
1828 // Encode the type qualifiers in the type reference.
1829 Record.push_back((ID << 3) | T.getCVRQualifiers());
1830}
1831
1832void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
1833 if (D == 0) {
1834 Record.push_back(0);
1835 return;
1836 }
1837
Douglas Gregorac8f2802009-04-10 17:25:41 +00001838 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00001839 if (ID == 0) {
1840 // We haven't seen this declaration before. Give it a new ID and
1841 // enqueue it in the list of declarations to emit.
1842 ID = DeclIDs.size();
1843 DeclsToEmit.push(const_cast<Decl *>(D));
1844 }
1845
1846 Record.push_back(ID);
1847}
1848
1849void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
1850 Record.push_back(Name.getNameKind());
1851 switch (Name.getNameKind()) {
1852 case DeclarationName::Identifier:
1853 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
1854 break;
1855
1856 case DeclarationName::ObjCZeroArgSelector:
1857 case DeclarationName::ObjCOneArgSelector:
1858 case DeclarationName::ObjCMultiArgSelector:
1859 assert(false && "Serialization of Objective-C selectors unavailable");
1860 break;
1861
1862 case DeclarationName::CXXConstructorName:
1863 case DeclarationName::CXXDestructorName:
1864 case DeclarationName::CXXConversionFunctionName:
1865 AddTypeRef(Name.getCXXNameType(), Record);
1866 break;
1867
1868 case DeclarationName::CXXOperatorName:
1869 Record.push_back(Name.getCXXOverloadedOperator());
1870 break;
1871
1872 case DeclarationName::CXXUsingDirective:
1873 // No extra data to emit
1874 break;
1875 }
1876}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001877
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001878/// \brief Write the given substatement or subexpression to the
1879/// bitstream.
1880void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00001881 RecordData Record;
1882 PCHStmtWriter Writer(*this, Record);
1883
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001884 if (!S) {
1885 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001886 return;
1887 }
1888
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001889 Writer.Code = pch::STMT_NULL_PTR;
1890 Writer.Visit(S);
1891 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00001892 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001893 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001894}
1895
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001896/// \brief Flush all of the statements that have been added to the
1897/// queue via AddStmt().
1898void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001899 RecordData Record;
1900 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001901
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001902 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1903 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00001904
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001905 if (!S) {
1906 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001907 continue;
1908 }
1909
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001910 Writer.Code = pch::STMT_NULL_PTR;
1911 Writer.Visit(S);
1912 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001913 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001914 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00001915
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001916 assert(N == StmtsToEmit.size() &&
1917 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00001918
1919 // Note that we are at the end of a full expression. Any
1920 // expression records that follow this one are part of a different
1921 // expression.
1922 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001923 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001924 }
Douglas Gregora151ba42009-04-14 23:32:43 +00001925
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001926 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00001927 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001928}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00001929
1930unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
1931 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1932 "SwitchCase recorded twice");
1933 unsigned NextID = SwitchCaseIDs.size();
1934 SwitchCaseIDs[S] = NextID;
1935 return NextID;
1936}
1937
1938unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
1939 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1940 "SwitchCase hasn't been seen yet");
1941 return SwitchCaseIDs[S];
1942}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00001943
1944/// \brief Retrieve the ID for the given label statement, which may
1945/// or may not have been emitted yet.
1946unsigned PCHWriter::GetLabelID(LabelStmt *S) {
1947 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1948 if (Pos != LabelIDs.end())
1949 return Pos->second;
1950
1951 unsigned NextID = LabelIDs.size();
1952 LabelIDs[S] = NextID;
1953 return NextID;
1954}