Sebastian Redl | d6522cf | 2010-08-18 23:56:31 +0000 | [diff] [blame] | 1 | //===--- ASTWriter.cpp - AST File Writer ----------------------------------===// |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 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 | // |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 10 | // This file defines the ASTWriter class, which writes AST files. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 14 | #include "clang/Serialization/ASTWriter.h" |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 15 | #include "clang/Serialization/ASTSerializationListener.h" |
Argyrios Kyrtzidis | 4bd9710 | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 16 | #include "ASTCommon.h" |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Sema.h" |
| 18 | #include "clang/Sema/IdentifierResolver.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclContextInternals.h" |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
John McCall | bfd822c | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 26 | #include "clang/AST/Type.h" |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLocVisitor.h" |
Sebastian Redl | f5b1346 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 28 | #include "clang/Serialization/ASTReader.h" |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 29 | #include "clang/Lex/MacroInfo.h" |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 30 | #include "clang/Lex/PreprocessingRecord.h" |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 32 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 33 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 226efd3 | 2010-11-23 19:19:34 +0000 | [diff] [blame] | 34 | #include "clang/Basic/FileSystemStatCache.h" |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 35 | #include "clang/Basic/OnDiskHashTable.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 36 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 37 | #include "clang/Basic/SourceManagerInternals.h" |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 38 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 7b71e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 39 | #include "clang/Basic/Version.h" |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/APFloat.h" |
| 41 | #include "llvm/ADT/APInt.h" |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 43 | #include "llvm/Bitcode/BitstreamWriter.h" |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 44 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 45 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Path.h" |
Chris Lattner | 225dd6c | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 47 | #include <cstdio> |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 48 | #include <string.h> |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 49 | using namespace clang; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 50 | using namespace clang::serialization; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 51 | |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 52 | template <typename T, typename Allocator> |
| 53 | T *data(std::vector<T, Allocator> &v) { |
| 54 | return v.empty() ? 0 : &v.front(); |
| 55 | } |
| 56 | template <typename T, typename Allocator> |
| 57 | const T *data(const std::vector<T, Allocator> &v) { |
| 58 | return v.empty() ? 0 : &v.front(); |
| 59 | } |
| 60 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // Type serialization |
| 63 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7099dbc | 2009-04-27 06:16:06 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 65 | namespace { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 66 | class ASTTypeWriter { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 67 | ASTWriter &Writer; |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 68 | ASTWriter::RecordDataImpl &Record; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 69 | |
| 70 | public: |
| 71 | /// \brief Type code that corresponds to the record generated. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 72 | TypeCode Code; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 73 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 74 | ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 75 | : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 76 | |
| 77 | void VisitArrayType(const ArrayType *T); |
| 78 | void VisitFunctionType(const FunctionType *T); |
| 79 | void VisitTagType(const TagType *T); |
| 80 | |
| 81 | #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); |
| 82 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 83 | #include "clang/AST/TypeNodes.def" |
| 84 | }; |
| 85 | } |
| 86 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 87 | void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 88 | assert(false && "Built-in types are never serialized"); |
| 89 | } |
| 90 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 91 | void ASTTypeWriter::VisitComplexType(const ComplexType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 92 | Writer.AddTypeRef(T->getElementType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 93 | Code = TYPE_COMPLEX; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 96 | void ASTTypeWriter::VisitPointerType(const PointerType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 97 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 98 | Code = TYPE_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 101 | void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 103 | Code = TYPE_BLOCK_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 106 | void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 107 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 108 | Code = TYPE_LVALUE_REFERENCE; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 111 | void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 112 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 113 | Code = TYPE_RVALUE_REFERENCE; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 116 | void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | Writer.AddTypeRef(T->getPointeeType(), Record); |
| 118 | Writer.AddTypeRef(QualType(T->getClass(), 0), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 119 | Code = TYPE_MEMBER_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 122 | void ASTTypeWriter::VisitArrayType(const ArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 123 | Writer.AddTypeRef(T->getElementType(), Record); |
| 124 | Record.push_back(T->getSizeModifier()); // FIXME: stable values |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 125 | Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 128 | void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 129 | VisitArrayType(T); |
| 130 | Writer.AddAPInt(T->getSize(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 131 | Code = TYPE_CONSTANT_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 134 | void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 135 | VisitArrayType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 136 | Code = TYPE_INCOMPLETE_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 139 | void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 140 | VisitArrayType(T); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 141 | Writer.AddSourceLocation(T->getLBracketLoc(), Record); |
| 142 | Writer.AddSourceLocation(T->getRBracketLoc(), Record); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 143 | Writer.AddStmt(T->getSizeExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 144 | Code = TYPE_VARIABLE_ARRAY; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 147 | void ASTTypeWriter::VisitVectorType(const VectorType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 148 | Writer.AddTypeRef(T->getElementType(), Record); |
| 149 | Record.push_back(T->getNumElements()); |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 150 | Record.push_back(T->getVectorKind()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 151 | Code = TYPE_VECTOR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 154 | void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 155 | VisitVectorType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 156 | Code = TYPE_EXT_VECTOR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 159 | void ASTTypeWriter::VisitFunctionType(const FunctionType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 160 | Writer.AddTypeRef(T->getResultType(), Record); |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 161 | FunctionType::ExtInfo C = T->getExtInfo(); |
| 162 | Record.push_back(C.getNoReturn()); |
Rafael Espindola | 49b85ab | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 163 | Record.push_back(C.getRegParm()); |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 164 | // FIXME: need to stabilize encoding of calling convention... |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 165 | Record.push_back(C.getCC()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 168 | void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 169 | VisitFunctionType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 170 | Code = TYPE_FUNCTION_NO_PROTO; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 173 | void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 174 | VisitFunctionType(T); |
| 175 | Record.push_back(T->getNumArgs()); |
| 176 | for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I) |
| 177 | Writer.AddTypeRef(T->getArgType(I), Record); |
| 178 | Record.push_back(T->isVariadic()); |
| 179 | Record.push_back(T->getTypeQuals()); |
Douglas Gregor | db9d664 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 180 | Record.push_back(static_cast<unsigned>(T->getRefQualifier())); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 181 | Record.push_back(T->hasExceptionSpec()); |
| 182 | Record.push_back(T->hasAnyExceptionSpec()); |
| 183 | Record.push_back(T->getNumExceptions()); |
| 184 | for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I) |
| 185 | Writer.AddTypeRef(T->getExceptionType(I), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 186 | Code = TYPE_FUNCTION_PROTO; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 189 | void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 190 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 191 | Code = TYPE_UNRESOLVED_USING; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 192 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 193 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 194 | void ASTTypeWriter::VisitTypedefType(const TypedefType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 195 | Writer.AddDeclRef(T->getDecl(), Record); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 196 | assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); |
| 197 | Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 198 | Code = TYPE_TYPEDEF; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 201 | void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) { |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 202 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 203 | Code = TYPE_TYPEOF_EXPR; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 206 | void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 207 | Writer.AddTypeRef(T->getUnderlyingType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 208 | Code = TYPE_TYPEOF; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 211 | void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) { |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 212 | Writer.AddStmt(T->getUnderlyingExpr()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 213 | Code = TYPE_DECLTYPE; |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 216 | void ASTTypeWriter::VisitAutoType(const AutoType *T) { |
| 217 | Writer.AddTypeRef(T->getDeducedType(), Record); |
| 218 | Code = TYPE_AUTO; |
| 219 | } |
| 220 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 221 | void ASTTypeWriter::VisitTagType(const TagType *T) { |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 222 | Record.push_back(T->isDependentType()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 223 | Writer.AddDeclRef(T->getDecl(), Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | assert(!T->isBeingDefined() && |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 225 | "Cannot serialize in the middle of a type definition"); |
| 226 | } |
| 227 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 228 | void ASTTypeWriter::VisitRecordType(const RecordType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 229 | VisitTagType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 230 | Code = TYPE_RECORD; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 233 | void ASTTypeWriter::VisitEnumType(const EnumType *T) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 234 | VisitTagType(T); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 235 | Code = TYPE_ENUM; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 236 | } |
| 237 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 238 | void ASTTypeWriter::VisitAttributedType(const AttributedType *T) { |
| 239 | Writer.AddTypeRef(T->getModifiedType(), Record); |
| 240 | Writer.AddTypeRef(T->getEquivalentType(), Record); |
| 241 | Record.push_back(T->getAttrKind()); |
| 242 | Code = TYPE_ATTRIBUTED; |
| 243 | } |
| 244 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 246 | ASTTypeWriter::VisitSubstTemplateTypeParmType( |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 247 | const SubstTemplateTypeParmType *T) { |
| 248 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 249 | Writer.AddTypeRef(T->getReplacementType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 250 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 254 | ASTTypeWriter::VisitSubstTemplateTypeParmPackType( |
| 255 | const SubstTemplateTypeParmPackType *T) { |
| 256 | Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record); |
| 257 | Writer.AddTemplateArgument(T->getArgumentPack(), Record); |
| 258 | Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK; |
| 259 | } |
| 260 | |
| 261 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 262 | ASTTypeWriter::VisitTemplateSpecializationType( |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 263 | const TemplateSpecializationType *T) { |
Argyrios Kyrtzidis | a4ed181 | 2010-07-08 13:09:53 +0000 | [diff] [blame] | 264 | Record.push_back(T->isDependentType()); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 265 | Writer.AddTemplateName(T->getTemplateName(), Record); |
| 266 | Record.push_back(T->getNumArgs()); |
| 267 | for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); |
| 268 | ArgI != ArgE; ++ArgI) |
| 269 | Writer.AddTemplateArgument(*ArgI, Record); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 270 | Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() |
| 271 | : T->getCanonicalTypeInternal(), |
| 272 | Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 273 | Code = TYPE_TEMPLATE_SPECIALIZATION; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 277 | ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { |
Argyrios Kyrtzidis | 4a57bd0 | 2010-06-30 08:49:25 +0000 | [diff] [blame] | 278 | VisitArrayType(T); |
| 279 | Writer.AddStmt(T->getSizeExpr()); |
| 280 | Writer.AddSourceRange(T->getBracketsRange(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 281 | Code = TYPE_DEPENDENT_SIZED_ARRAY; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 285 | ASTTypeWriter::VisitDependentSizedExtVectorType( |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 286 | const DependentSizedExtVectorType *T) { |
| 287 | // FIXME: Serialize this type (C++ only) |
| 288 | assert(false && "Cannot serialize dependent sized extended vector types"); |
| 289 | } |
| 290 | |
| 291 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 292 | ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 293 | Record.push_back(T->getDepth()); |
| 294 | Record.push_back(T->getIndex()); |
| 295 | Record.push_back(T->isParameterPack()); |
| 296 | Writer.AddIdentifierRef(T->getName(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 297 | Code = TYPE_TEMPLATE_TYPE_PARM; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 301 | ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) { |
Argyrios Kyrtzidis | bfcacee | 2010-06-24 08:57:31 +0000 | [diff] [blame] | 302 | Record.push_back(T->getKeyword()); |
| 303 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 304 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
Argyrios Kyrtzidis | e929095 | 2010-07-02 11:55:24 +0000 | [diff] [blame] | 305 | Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() |
| 306 | : T->getCanonicalTypeInternal(), |
| 307 | Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 308 | Code = TYPE_DEPENDENT_NAME; |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 312 | ASTTypeWriter::VisitDependentTemplateSpecializationType( |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 313 | const DependentTemplateSpecializationType *T) { |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 314 | Record.push_back(T->getKeyword()); |
| 315 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 316 | Writer.AddIdentifierRef(T->getIdentifier(), Record); |
| 317 | Record.push_back(T->getNumArgs()); |
| 318 | for (DependentTemplateSpecializationType::iterator |
| 319 | I = T->begin(), E = T->end(); I != E; ++I) |
| 320 | Writer.AddTemplateArgument(*I, Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 321 | Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 324 | void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) { |
| 325 | Writer.AddTypeRef(T->getPattern(), Record); |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 326 | if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions()) |
| 327 | Record.push_back(*NumExpansions + 1); |
| 328 | else |
| 329 | Record.push_back(0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 330 | Code = TYPE_PACK_EXPANSION; |
| 331 | } |
| 332 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 333 | void ASTTypeWriter::VisitParenType(const ParenType *T) { |
| 334 | Writer.AddTypeRef(T->getInnerType(), Record); |
| 335 | Code = TYPE_PAREN; |
| 336 | } |
| 337 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 338 | void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) { |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 339 | Record.push_back(T->getKeyword()); |
Argyrios Kyrtzidis | f0f7a79 | 2010-06-25 16:24:58 +0000 | [diff] [blame] | 340 | Writer.AddNestedNameSpecifier(T->getQualifier(), Record); |
| 341 | Writer.AddTypeRef(T->getNamedType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 342 | Code = TYPE_ELABORATED; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 345 | void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 346 | Writer.AddDeclRef(T->getDecl(), Record); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 347 | Writer.AddTypeRef(T->getInjectedSpecializationType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 348 | Code = TYPE_INJECTED_CLASS_NAME; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 351 | void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 352 | Writer.AddDeclRef(T->getDecl(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 353 | Code = TYPE_OBJC_INTERFACE; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 356 | void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 357 | Writer.AddTypeRef(T->getBaseType(), Record); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 358 | Record.push_back(T->getNumProtocols()); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 359 | for (ObjCObjectType::qual_iterator I = T->qual_begin(), |
Steve Naroff | 4fc95aa | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 360 | E = T->qual_end(); I != E; ++I) |
| 361 | Writer.AddDeclRef(*I, Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 362 | Code = TYPE_OBJC_OBJECT; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 365 | void |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 366 | ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | Writer.AddTypeRef(T->getPointeeType(), Record); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 368 | Code = TYPE_OBJC_OBJECT_POINTER; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 369 | } |
| 370 | |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 371 | namespace { |
| 372 | |
| 373 | class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 374 | ASTWriter &Writer; |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 375 | ASTWriter::RecordDataImpl &Record; |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 376 | |
| 377 | public: |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 378 | TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 379 | : Writer(Writer), Record(Record) { } |
| 380 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 381 | #define ABSTRACT_TYPELOC(CLASS, PARENT) |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 382 | #define TYPELOC(CLASS, PARENT) \ |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 383 | void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 384 | #include "clang/AST/TypeLocNodes.def" |
| 385 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 386 | void VisitArrayTypeLoc(ArrayTypeLoc TyLoc); |
| 387 | void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
| 390 | } |
| 391 | |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 392 | void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 393 | // nothing to do |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 394 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 395 | void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 396 | Writer.AddSourceLocation(TL.getBuiltinLoc(), Record); |
| 397 | if (TL.needsExtraLocalData()) { |
| 398 | Record.push_back(TL.getWrittenTypeSpec()); |
| 399 | Record.push_back(TL.getWrittenSignSpec()); |
| 400 | Record.push_back(TL.getWrittenWidthSpec()); |
| 401 | Record.push_back(TL.hasModeAttr()); |
| 402 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 403 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 404 | void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) { |
| 405 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 406 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 407 | void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 408 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 409 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 410 | void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 411 | Writer.AddSourceLocation(TL.getCaretLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 412 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 413 | void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 414 | Writer.AddSourceLocation(TL.getAmpLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 415 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 416 | void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 417 | Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 418 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 419 | void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 420 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 421 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 422 | void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 423 | Writer.AddSourceLocation(TL.getLBracketLoc(), Record); |
| 424 | Writer.AddSourceLocation(TL.getRBracketLoc(), Record); |
| 425 | Record.push_back(TL.getSizeExpr() ? 1 : 0); |
| 426 | if (TL.getSizeExpr()) |
| 427 | Writer.AddStmt(TL.getSizeExpr()); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 428 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 429 | void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { |
| 430 | VisitArrayTypeLoc(TL); |
| 431 | } |
| 432 | void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { |
| 433 | VisitArrayTypeLoc(TL); |
| 434 | } |
| 435 | void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { |
| 436 | VisitArrayTypeLoc(TL); |
| 437 | } |
| 438 | void TypeLocWriter::VisitDependentSizedArrayTypeLoc( |
| 439 | DependentSizedArrayTypeLoc TL) { |
| 440 | VisitArrayTypeLoc(TL); |
| 441 | } |
| 442 | void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc( |
| 443 | DependentSizedExtVectorTypeLoc TL) { |
| 444 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 445 | } |
| 446 | void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) { |
| 447 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 448 | } |
| 449 | void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { |
| 450 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 451 | } |
| 452 | void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 453 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 454 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 455 | Record.push_back(TL.getTrailingReturn()); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 456 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 457 | Writer.AddDeclRef(TL.getArg(i), Record); |
| 458 | } |
| 459 | void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { |
| 460 | VisitFunctionTypeLoc(TL); |
| 461 | } |
| 462 | void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { |
| 463 | VisitFunctionTypeLoc(TL); |
| 464 | } |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 465 | void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { |
| 466 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 467 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 468 | void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 469 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 470 | } |
| 471 | void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 472 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 473 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 474 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 475 | } |
| 476 | void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 477 | Writer.AddSourceLocation(TL.getTypeofLoc(), Record); |
| 478 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 479 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 480 | Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 481 | } |
| 482 | void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { |
| 483 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 484 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 485 | void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) { |
| 486 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 487 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 488 | void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) { |
| 489 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 490 | } |
| 491 | void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) { |
| 492 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 493 | } |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 494 | void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 495 | Writer.AddSourceLocation(TL.getAttrNameLoc(), Record); |
| 496 | if (TL.hasAttrOperand()) { |
| 497 | SourceRange range = TL.getAttrOperandParensRange(); |
| 498 | Writer.AddSourceLocation(range.getBegin(), Record); |
| 499 | Writer.AddSourceLocation(range.getEnd(), Record); |
| 500 | } |
| 501 | if (TL.hasAttrExprOperand()) { |
| 502 | Expr *operand = TL.getAttrExprOperand(); |
| 503 | Record.push_back(operand ? 1 : 0); |
| 504 | if (operand) Writer.AddStmt(operand); |
| 505 | } else if (TL.hasAttrEnumOperand()) { |
| 506 | Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record); |
| 507 | } |
| 508 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 509 | void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 510 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 511 | } |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 512 | void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc( |
| 513 | SubstTemplateTypeParmTypeLoc TL) { |
| 514 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 515 | } |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 516 | void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc( |
| 517 | SubstTemplateTypeParmPackTypeLoc TL) { |
| 518 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 519 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 520 | void TypeLocWriter::VisitTemplateSpecializationTypeLoc( |
| 521 | TemplateSpecializationTypeLoc TL) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 522 | Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record); |
| 523 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 524 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 525 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 526 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), |
| 527 | TL.getArgLoc(i).getLocInfo(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 528 | } |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 529 | void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) { |
| 530 | Writer.AddSourceLocation(TL.getLParenLoc(), Record); |
| 531 | Writer.AddSourceLocation(TL.getRParenLoc(), Record); |
| 532 | } |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 533 | void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 534 | Writer.AddSourceLocation(TL.getKeywordLoc(), Record); |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame^] | 535 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 536 | } |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 537 | void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { |
| 538 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 539 | } |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 540 | void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 541 | Writer.AddSourceLocation(TL.getKeywordLoc(), Record); |
Douglas Gregor | 3d0da5f | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 542 | Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 543 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 544 | } |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 545 | void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( |
| 546 | DependentTemplateSpecializationTypeLoc TL) { |
| 547 | Writer.AddSourceLocation(TL.getKeywordLoc(), Record); |
| 548 | Writer.AddSourceRange(TL.getQualifierRange(), Record); |
| 549 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
| 550 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 551 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 552 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 553 | Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), |
| 554 | TL.getArgLoc(I).getLocInfo(), Record); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 555 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 556 | void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { |
| 557 | Writer.AddSourceLocation(TL.getEllipsisLoc(), Record); |
| 558 | } |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 559 | void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 560 | Writer.AddSourceLocation(TL.getNameLoc(), Record); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 561 | } |
| 562 | void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 563 | Record.push_back(TL.hasBaseTypeAsWritten()); |
John McCall | 1700197 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 564 | Writer.AddSourceLocation(TL.getLAngleLoc(), Record); |
| 565 | Writer.AddSourceLocation(TL.getRAngleLoc(), Record); |
| 566 | for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) |
| 567 | Writer.AddSourceLocation(TL.getProtocolLoc(i), Record); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 568 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 569 | void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 570 | Writer.AddSourceLocation(TL.getStarLoc(), Record); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 571 | } |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 19cea4e | 2009-04-22 05:57:30 +0000 | [diff] [blame] | 573 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 574 | // ASTWriter Implementation |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 575 | //===----------------------------------------------------------------------===// |
| 576 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 577 | static void EmitBlockID(unsigned ID, const char *Name, |
| 578 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 579 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 580 | Record.clear(); |
| 581 | Record.push_back(ID); |
| 582 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 583 | |
| 584 | // Emit the block name if present. |
| 585 | if (Name == 0 || Name[0] == 0) return; |
| 586 | Record.clear(); |
| 587 | while (*Name) |
| 588 | Record.push_back(*Name++); |
| 589 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 590 | } |
| 591 | |
| 592 | static void EmitRecordID(unsigned ID, const char *Name, |
| 593 | llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 594 | ASTWriter::RecordDataImpl &Record) { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 595 | Record.clear(); |
| 596 | Record.push_back(ID); |
| 597 | while (*Name) |
| 598 | Record.push_back(*Name++); |
| 599 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void AddStmtsExprs(llvm::BitstreamWriter &Stream, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 603 | ASTWriter::RecordDataImpl &Record) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 604 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 605 | RECORD(STMT_STOP); |
| 606 | RECORD(STMT_NULL_PTR); |
| 607 | RECORD(STMT_NULL); |
| 608 | RECORD(STMT_COMPOUND); |
| 609 | RECORD(STMT_CASE); |
| 610 | RECORD(STMT_DEFAULT); |
| 611 | RECORD(STMT_LABEL); |
| 612 | RECORD(STMT_IF); |
| 613 | RECORD(STMT_SWITCH); |
| 614 | RECORD(STMT_WHILE); |
| 615 | RECORD(STMT_DO); |
| 616 | RECORD(STMT_FOR); |
| 617 | RECORD(STMT_GOTO); |
| 618 | RECORD(STMT_INDIRECT_GOTO); |
| 619 | RECORD(STMT_CONTINUE); |
| 620 | RECORD(STMT_BREAK); |
| 621 | RECORD(STMT_RETURN); |
| 622 | RECORD(STMT_DECL); |
| 623 | RECORD(STMT_ASM); |
| 624 | RECORD(EXPR_PREDEFINED); |
| 625 | RECORD(EXPR_DECL_REF); |
| 626 | RECORD(EXPR_INTEGER_LITERAL); |
| 627 | RECORD(EXPR_FLOATING_LITERAL); |
| 628 | RECORD(EXPR_IMAGINARY_LITERAL); |
| 629 | RECORD(EXPR_STRING_LITERAL); |
| 630 | RECORD(EXPR_CHARACTER_LITERAL); |
| 631 | RECORD(EXPR_PAREN); |
| 632 | RECORD(EXPR_UNARY_OPERATOR); |
| 633 | RECORD(EXPR_SIZEOF_ALIGN_OF); |
| 634 | RECORD(EXPR_ARRAY_SUBSCRIPT); |
| 635 | RECORD(EXPR_CALL); |
| 636 | RECORD(EXPR_MEMBER); |
| 637 | RECORD(EXPR_BINARY_OPERATOR); |
| 638 | RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR); |
| 639 | RECORD(EXPR_CONDITIONAL_OPERATOR); |
| 640 | RECORD(EXPR_IMPLICIT_CAST); |
| 641 | RECORD(EXPR_CSTYLE_CAST); |
| 642 | RECORD(EXPR_COMPOUND_LITERAL); |
| 643 | RECORD(EXPR_EXT_VECTOR_ELEMENT); |
| 644 | RECORD(EXPR_INIT_LIST); |
| 645 | RECORD(EXPR_DESIGNATED_INIT); |
| 646 | RECORD(EXPR_IMPLICIT_VALUE_INIT); |
| 647 | RECORD(EXPR_VA_ARG); |
| 648 | RECORD(EXPR_ADDR_LABEL); |
| 649 | RECORD(EXPR_STMT); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 650 | RECORD(EXPR_CHOOSE); |
| 651 | RECORD(EXPR_GNU_NULL); |
| 652 | RECORD(EXPR_SHUFFLE_VECTOR); |
| 653 | RECORD(EXPR_BLOCK); |
| 654 | RECORD(EXPR_BLOCK_DECL_REF); |
| 655 | RECORD(EXPR_OBJC_STRING_LITERAL); |
| 656 | RECORD(EXPR_OBJC_ENCODE); |
| 657 | RECORD(EXPR_OBJC_SELECTOR_EXPR); |
| 658 | RECORD(EXPR_OBJC_PROTOCOL_EXPR); |
| 659 | RECORD(EXPR_OBJC_IVAR_REF_EXPR); |
| 660 | RECORD(EXPR_OBJC_PROPERTY_REF_EXPR); |
| 661 | RECORD(EXPR_OBJC_KVC_REF_EXPR); |
| 662 | RECORD(EXPR_OBJC_MESSAGE_EXPR); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 663 | RECORD(STMT_OBJC_FOR_COLLECTION); |
| 664 | RECORD(STMT_OBJC_CATCH); |
| 665 | RECORD(STMT_OBJC_FINALLY); |
| 666 | RECORD(STMT_OBJC_AT_TRY); |
| 667 | RECORD(STMT_OBJC_AT_SYNCHRONIZED); |
| 668 | RECORD(STMT_OBJC_AT_THROW); |
Sam Weinig | e83b3ac | 2010-02-07 06:32:43 +0000 | [diff] [blame] | 669 | RECORD(EXPR_CXX_OPERATOR_CALL); |
| 670 | RECORD(EXPR_CXX_CONSTRUCT); |
| 671 | RECORD(EXPR_CXX_STATIC_CAST); |
| 672 | RECORD(EXPR_CXX_DYNAMIC_CAST); |
| 673 | RECORD(EXPR_CXX_REINTERPRET_CAST); |
| 674 | RECORD(EXPR_CXX_CONST_CAST); |
| 675 | RECORD(EXPR_CXX_FUNCTIONAL_CAST); |
| 676 | RECORD(EXPR_CXX_BOOL_LITERAL); |
| 677 | RECORD(EXPR_CXX_NULL_PTR_LITERAL); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 678 | RECORD(EXPR_CXX_TYPEID_EXPR); |
| 679 | RECORD(EXPR_CXX_TYPEID_TYPE); |
| 680 | RECORD(EXPR_CXX_UUIDOF_EXPR); |
| 681 | RECORD(EXPR_CXX_UUIDOF_TYPE); |
| 682 | RECORD(EXPR_CXX_THIS); |
| 683 | RECORD(EXPR_CXX_THROW); |
| 684 | RECORD(EXPR_CXX_DEFAULT_ARG); |
| 685 | RECORD(EXPR_CXX_BIND_TEMPORARY); |
| 686 | RECORD(EXPR_CXX_SCALAR_VALUE_INIT); |
| 687 | RECORD(EXPR_CXX_NEW); |
| 688 | RECORD(EXPR_CXX_DELETE); |
| 689 | RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR); |
| 690 | RECORD(EXPR_EXPR_WITH_CLEANUPS); |
| 691 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER); |
| 692 | RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF); |
| 693 | RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT); |
| 694 | RECORD(EXPR_CXX_UNRESOLVED_MEMBER); |
| 695 | RECORD(EXPR_CXX_UNRESOLVED_LOOKUP); |
| 696 | RECORD(EXPR_CXX_UNARY_TYPE_TRAIT); |
| 697 | RECORD(EXPR_CXX_NOEXCEPT); |
| 698 | RECORD(EXPR_OPAQUE_VALUE); |
| 699 | RECORD(EXPR_BINARY_TYPE_TRAIT); |
| 700 | RECORD(EXPR_PACK_EXPANSION); |
| 701 | RECORD(EXPR_SIZEOF_PACK); |
| 702 | RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK); |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 703 | RECORD(EXPR_CUDA_KERNEL_CALL); |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 704 | #undef RECORD |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 707 | void ASTWriter::WriteBlockInfoBlock() { |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 708 | RecordData Record; |
| 709 | Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 711 | #define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record) |
| 712 | #define RECORD(X) EmitRecordID(X, #X, Stream, Record) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 714 | // AST Top-Level Block. |
Sebastian Redl | f164204 | 2010-08-18 23:57:22 +0000 | [diff] [blame] | 715 | BLOCK(AST_BLOCK); |
Zhongxing Xu | b027cdf | 2009-06-03 09:23:28 +0000 | [diff] [blame] | 716 | RECORD(ORIGINAL_FILE_NAME); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 717 | RECORD(TYPE_OFFSET); |
| 718 | RECORD(DECL_OFFSET); |
| 719 | RECORD(LANGUAGE_OPTIONS); |
Douglas Gregor | 7b71e63 | 2009-04-27 22:23:34 +0000 | [diff] [blame] | 720 | RECORD(METADATA); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 721 | RECORD(IDENTIFIER_OFFSET); |
| 722 | RECORD(IDENTIFIER_TABLE); |
| 723 | RECORD(EXTERNAL_DEFINITIONS); |
| 724 | RECORD(SPECIAL_TYPES); |
| 725 | RECORD(STATISTICS); |
| 726 | RECORD(TENTATIVE_DEFINITIONS); |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 727 | RECORD(UNUSED_FILESCOPED_DECLS); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 728 | RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS); |
| 729 | RECORD(SELECTOR_OFFSETS); |
| 730 | RECORD(METHOD_POOL); |
| 731 | RECORD(PP_COUNTER_VALUE); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 732 | RECORD(SOURCE_LOCATION_OFFSETS); |
| 733 | RECORD(SOURCE_LOCATION_PRELOADS); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 734 | RECORD(STAT_CACHE); |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 735 | RECORD(EXT_VECTOR_DECLS); |
Ted Kremenek | 1743713 | 2010-01-22 20:59:36 +0000 | [diff] [blame] | 736 | RECORD(VERSION_CONTROL_BRANCH_REVISION); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 737 | RECORD(MACRO_DEFINITION_OFFSETS); |
Sebastian Redl | 595c513 | 2010-07-08 22:01:51 +0000 | [diff] [blame] | 738 | RECORD(CHAINED_METADATA); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 739 | RECORD(REFERENCED_SELECTOR_POOL); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 740 | RECORD(TU_UPDATE_LEXICAL); |
| 741 | RECORD(REDECLS_UPDATE_LATEST); |
| 742 | RECORD(SEMA_DECL_REFS); |
| 743 | RECORD(WEAK_UNDECLARED_IDENTIFIERS); |
| 744 | RECORD(PENDING_IMPLICIT_INSTANTIATIONS); |
| 745 | RECORD(DECL_REPLACEMENTS); |
| 746 | RECORD(UPDATE_VISIBLE); |
| 747 | RECORD(DECL_UPDATE_OFFSETS); |
| 748 | RECORD(DECL_UPDATES); |
| 749 | RECORD(CXX_BASE_SPECIFIER_OFFSETS); |
| 750 | RECORD(DIAG_PRAGMA_MAPPINGS); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 751 | RECORD(CUDA_SPECIAL_DECL_REFS); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 752 | RECORD(HEADER_SEARCH_TABLE); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 753 | RECORD(FP_PRAGMA_OPTIONS); |
| 754 | RECORD(OPENCL_EXTENSIONS); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 756 | // SourceManager Block. |
Chris Lattner | 6403198 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 757 | BLOCK(SOURCE_MANAGER_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 758 | RECORD(SM_SLOC_FILE_ENTRY); |
| 759 | RECORD(SM_SLOC_BUFFER_ENTRY); |
| 760 | RECORD(SM_SLOC_BUFFER_BLOB); |
| 761 | RECORD(SM_SLOC_INSTANTIATION_ENTRY); |
| 762 | RECORD(SM_LINE_TABLE); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 764 | // Preprocessor Block. |
Chris Lattner | 6403198 | 2009-04-27 00:40:25 +0000 | [diff] [blame] | 765 | BLOCK(PREPROCESSOR_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 766 | RECORD(PP_MACRO_OBJECT_LIKE); |
| 767 | RECORD(PP_MACRO_FUNCTION_LIKE); |
| 768 | RECORD(PP_TOKEN); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 769 | |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 770 | // Decls and Types block. |
| 771 | BLOCK(DECLTYPES_BLOCK); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 772 | RECORD(TYPE_EXT_QUAL); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 773 | RECORD(TYPE_COMPLEX); |
| 774 | RECORD(TYPE_POINTER); |
| 775 | RECORD(TYPE_BLOCK_POINTER); |
| 776 | RECORD(TYPE_LVALUE_REFERENCE); |
| 777 | RECORD(TYPE_RVALUE_REFERENCE); |
| 778 | RECORD(TYPE_MEMBER_POINTER); |
| 779 | RECORD(TYPE_CONSTANT_ARRAY); |
| 780 | RECORD(TYPE_INCOMPLETE_ARRAY); |
| 781 | RECORD(TYPE_VARIABLE_ARRAY); |
| 782 | RECORD(TYPE_VECTOR); |
| 783 | RECORD(TYPE_EXT_VECTOR); |
| 784 | RECORD(TYPE_FUNCTION_PROTO); |
| 785 | RECORD(TYPE_FUNCTION_NO_PROTO); |
| 786 | RECORD(TYPE_TYPEDEF); |
| 787 | RECORD(TYPE_TYPEOF_EXPR); |
| 788 | RECORD(TYPE_TYPEOF); |
| 789 | RECORD(TYPE_RECORD); |
| 790 | RECORD(TYPE_ENUM); |
| 791 | RECORD(TYPE_OBJC_INTERFACE); |
John McCall | 94f619a | 2010-05-16 02:12:35 +0000 | [diff] [blame] | 792 | RECORD(TYPE_OBJC_OBJECT); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 793 | RECORD(TYPE_OBJC_OBJECT_POINTER); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 794 | RECORD(TYPE_DECLTYPE); |
| 795 | RECORD(TYPE_ELABORATED); |
| 796 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM); |
| 797 | RECORD(TYPE_UNRESOLVED_USING); |
| 798 | RECORD(TYPE_INJECTED_CLASS_NAME); |
| 799 | RECORD(TYPE_OBJC_OBJECT); |
| 800 | RECORD(TYPE_TEMPLATE_TYPE_PARM); |
| 801 | RECORD(TYPE_TEMPLATE_SPECIALIZATION); |
| 802 | RECORD(TYPE_DEPENDENT_NAME); |
| 803 | RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION); |
| 804 | RECORD(TYPE_DEPENDENT_SIZED_ARRAY); |
| 805 | RECORD(TYPE_PAREN); |
| 806 | RECORD(TYPE_PACK_EXPANSION); |
| 807 | RECORD(TYPE_ATTRIBUTED); |
| 808 | RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 809 | RECORD(DECL_TRANSLATION_UNIT); |
| 810 | RECORD(DECL_TYPEDEF); |
| 811 | RECORD(DECL_ENUM); |
| 812 | RECORD(DECL_RECORD); |
| 813 | RECORD(DECL_ENUM_CONSTANT); |
| 814 | RECORD(DECL_FUNCTION); |
| 815 | RECORD(DECL_OBJC_METHOD); |
| 816 | RECORD(DECL_OBJC_INTERFACE); |
| 817 | RECORD(DECL_OBJC_PROTOCOL); |
| 818 | RECORD(DECL_OBJC_IVAR); |
| 819 | RECORD(DECL_OBJC_AT_DEFS_FIELD); |
| 820 | RECORD(DECL_OBJC_CLASS); |
| 821 | RECORD(DECL_OBJC_FORWARD_PROTOCOL); |
| 822 | RECORD(DECL_OBJC_CATEGORY); |
| 823 | RECORD(DECL_OBJC_CATEGORY_IMPL); |
| 824 | RECORD(DECL_OBJC_IMPLEMENTATION); |
| 825 | RECORD(DECL_OBJC_COMPATIBLE_ALIAS); |
| 826 | RECORD(DECL_OBJC_PROPERTY); |
| 827 | RECORD(DECL_OBJC_PROPERTY_IMPL); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 828 | RECORD(DECL_FIELD); |
| 829 | RECORD(DECL_VAR); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 830 | RECORD(DECL_IMPLICIT_PARAM); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 831 | RECORD(DECL_PARM_VAR); |
Chris Lattner | db397b6 | 2009-04-26 22:32:16 +0000 | [diff] [blame] | 832 | RECORD(DECL_FILE_SCOPE_ASM); |
| 833 | RECORD(DECL_BLOCK); |
| 834 | RECORD(DECL_CONTEXT_LEXICAL); |
| 835 | RECORD(DECL_CONTEXT_VISIBLE); |
Douglas Gregor | 0beaec0 | 2011-02-08 16:34:17 +0000 | [diff] [blame] | 836 | RECORD(DECL_NAMESPACE); |
| 837 | RECORD(DECL_NAMESPACE_ALIAS); |
| 838 | RECORD(DECL_USING); |
| 839 | RECORD(DECL_USING_SHADOW); |
| 840 | RECORD(DECL_USING_DIRECTIVE); |
| 841 | RECORD(DECL_UNRESOLVED_USING_VALUE); |
| 842 | RECORD(DECL_UNRESOLVED_USING_TYPENAME); |
| 843 | RECORD(DECL_LINKAGE_SPEC); |
| 844 | RECORD(DECL_CXX_RECORD); |
| 845 | RECORD(DECL_CXX_METHOD); |
| 846 | RECORD(DECL_CXX_CONSTRUCTOR); |
| 847 | RECORD(DECL_CXX_DESTRUCTOR); |
| 848 | RECORD(DECL_CXX_CONVERSION); |
| 849 | RECORD(DECL_ACCESS_SPEC); |
| 850 | RECORD(DECL_FRIEND); |
| 851 | RECORD(DECL_FRIEND_TEMPLATE); |
| 852 | RECORD(DECL_CLASS_TEMPLATE); |
| 853 | RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION); |
| 854 | RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION); |
| 855 | RECORD(DECL_FUNCTION_TEMPLATE); |
| 856 | RECORD(DECL_TEMPLATE_TYPE_PARM); |
| 857 | RECORD(DECL_NON_TYPE_TEMPLATE_PARM); |
| 858 | RECORD(DECL_TEMPLATE_TEMPLATE_PARM); |
| 859 | RECORD(DECL_STATIC_ASSERT); |
| 860 | RECORD(DECL_CXX_BASE_SPECIFIERS); |
| 861 | RECORD(DECL_INDIRECTFIELD); |
| 862 | RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK); |
| 863 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 864 | BLOCK(PREPROCESSOR_DETAIL_BLOCK); |
| 865 | RECORD(PPD_MACRO_INSTANTIATION); |
| 866 | RECORD(PPD_MACRO_DEFINITION); |
| 867 | RECORD(PPD_INCLUSION_DIRECTIVE); |
| 868 | |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 869 | // Statements and Exprs can occur in the Decls and Types block. |
Chris Lattner | ccac3a6 | 2009-04-27 00:49:53 +0000 | [diff] [blame] | 870 | AddStmtsExprs(Stream, Record); |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 871 | #undef RECORD |
| 872 | #undef BLOCK |
| 873 | Stream.ExitBlock(); |
| 874 | } |
| 875 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 876 | /// \brief Adjusts the given filename to only write out the portion of the |
| 877 | /// filename that is not part of the system root directory. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | /// |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 879 | /// \param Filename the file name to adjust. |
| 880 | /// |
| 881 | /// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and |
| 882 | /// the returned filename will be adjusted by this system root. |
| 883 | /// |
| 884 | /// \returns either the original filename (if it needs no adjustment) or the |
| 885 | /// adjusted filename (which points into the @p Filename parameter). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | static const char * |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 887 | adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) { |
| 888 | assert(Filename && "No file name to adjust?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 890 | if (!isysroot) |
| 891 | return Filename; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 893 | // Verify that the filename and the system root have the same prefix. |
| 894 | unsigned Pos = 0; |
| 895 | for (; Filename[Pos] && isysroot[Pos]; ++Pos) |
| 896 | if (Filename[Pos] != isysroot[Pos]) |
| 897 | return Filename; // Prefixes don't match. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 899 | // We hit the end of the filename before we hit the end of the system root. |
| 900 | if (!Filename[Pos]) |
| 901 | return Filename; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 903 | // If the file name has a '/' at the current position, skip over the '/'. |
| 904 | // We distinguish sysroot-based includes from absolute includes by the |
| 905 | // absence of '/' at the beginning of sysroot-based includes. |
| 906 | if (Filename[Pos] == '/') |
| 907 | ++Pos; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 909 | return Filename + Pos; |
| 910 | } |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 911 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 912 | /// \brief Write the AST metadata (e.g., i686-apple-darwin9). |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 913 | void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot, |
| 914 | const std::string &OutputFile) { |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 915 | using namespace llvm; |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 917 | // Metadata |
| 918 | const TargetInfo &Target = Context.Target; |
| 919 | BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); |
Sebastian Redl | 4d3af3e | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 920 | MetaAbbrev->Add(BitCodeAbbrevOp( |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 921 | Chain ? CHAINED_METADATA : METADATA)); |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 922 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major |
| 923 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 924 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major |
| 925 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor |
| 926 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable |
Sebastian Redl | 4d3af3e | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 927 | // Target triple or chained PCH name |
| 928 | MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 929 | unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 931 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 932 | Record.push_back(Chain ? CHAINED_METADATA : METADATA); |
| 933 | Record.push_back(VERSION_MAJOR); |
| 934 | Record.push_back(VERSION_MINOR); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 935 | Record.push_back(CLANG_VERSION_MAJOR); |
| 936 | Record.push_back(CLANG_VERSION_MINOR); |
| 937 | Record.push_back(isysroot != 0); |
Sebastian Redl | 4d3af3e | 2010-07-09 21:00:24 +0000 | [diff] [blame] | 938 | // FIXME: This writes the absolute path for chained headers. |
| 939 | const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple(); |
| 940 | Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 942 | // Original file name |
| 943 | SourceManager &SM = Context.getSourceManager(); |
| 944 | if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) { |
| 945 | BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 946 | FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME)); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 947 | FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 948 | unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev); |
| 949 | |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 950 | llvm::SmallString<128> MainFilePath(MainFile->getName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 952 | llvm::sys::fs::make_absolute(MainFilePath); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 953 | |
Kovarththanan Rajaratnam | d16d38c | 2010-03-14 07:15:57 +0000 | [diff] [blame] | 954 | const char *MainFileNameStr = MainFilePath.c_str(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr, |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 956 | isysroot); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 957 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 958 | Record.push_back(ORIGINAL_FILE_NAME); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 959 | Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr); |
Douglas Gregor | 45fe036 | 2009-05-12 01:31:05 +0000 | [diff] [blame] | 960 | } |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 961 | |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 962 | // Original PCH directory |
| 963 | if (!OutputFile.empty() && OutputFile != "-") { |
| 964 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 965 | Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR)); |
| 966 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
| 967 | unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev); |
| 968 | |
| 969 | llvm::SmallString<128> OutputPath(OutputFile); |
| 970 | |
| 971 | llvm::sys::fs::make_absolute(OutputPath); |
| 972 | StringRef origDir = llvm::sys::path::parent_path(OutputPath); |
| 973 | |
| 974 | RecordData Record; |
| 975 | Record.push_back(ORIGINAL_PCH_DIR); |
| 976 | Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); |
| 977 | } |
| 978 | |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 979 | // Repository branch/version information. |
| 980 | BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 981 | RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION)); |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 982 | RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag |
| 983 | unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev); |
Douglas Gregor | d54f3a1 | 2009-10-05 21:07:28 +0000 | [diff] [blame] | 984 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 985 | Record.push_back(VERSION_CONTROL_BRANCH_REVISION); |
Ted Kremenek | 18e066f | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 986 | Stream.EmitRecordWithBlob(RepoAbbrevCode, Record, |
| 987 | getClangFullRepositoryVersion()); |
Douglas Gregor | bfbde53 | 2009-04-10 21:16:55 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /// \brief Write the LangOptions structure. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 991 | void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) { |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 992 | RecordData Record; |
| 993 | Record.push_back(LangOpts.Trigraphs); |
| 994 | Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments. |
| 995 | Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers. |
| 996 | Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode. |
| 997 | Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc) |
Chandler Carruth | e03aa55 | 2010-04-17 20:17:31 +0000 | [diff] [blame] | 998 | Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 999 | Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'. |
| 1000 | Record.push_back(LangOpts.Digraphs); // C94, C99 and C++ |
| 1001 | Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants. |
| 1002 | Record.push_back(LangOpts.C99); // C99 Support |
| 1003 | Record.push_back(LangOpts.Microsoft); // Microsoft extensions. |
Michael J. Spencer | 4992ca4b | 2010-10-21 05:21:48 +0000 | [diff] [blame] | 1004 | // LangOpts.MSCVersion is ignored because all it does it set a macro, which is |
| 1005 | // already saved elsewhere. |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1006 | Record.push_back(LangOpts.CPlusPlus); // C++ Support |
| 1007 | Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1008 | Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1010 | Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled. |
| 1011 | Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled. |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1012 | Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1013 | // modern abi enabled. |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1014 | Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1015 | // modern abi enabled. |
Fariborz Jahanian | 13f3b2f | 2011-01-07 18:59:25 +0000 | [diff] [blame] | 1016 | Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI |
Ted Kremenek | 1d56c9e | 2010-12-23 21:35:43 +0000 | [diff] [blame] | 1017 | Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized |
| 1018 | // properties enabled. |
Fariborz Jahanian | 62c5602 | 2010-04-22 21:01:59 +0000 | [diff] [blame] | 1019 | Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled.. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1021 | Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1022 | Record.push_back(LangOpts.WritableStrings); // Allow writable strings |
| 1023 | Record.push_back(LangOpts.LaxVectorConversions); |
Nate Begeman | f291166 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1024 | Record.push_back(LangOpts.AltiVec); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1025 | Record.push_back(LangOpts.Exceptions); // Support exception handling. |
Anders Carlsson | ce8dd3a | 2011-02-19 23:53:54 +0000 | [diff] [blame] | 1026 | Record.push_back(LangOpts.ObjCExceptions); |
Anders Carlsson | 6bbd268 | 2011-02-23 03:04:54 +0000 | [diff] [blame] | 1027 | Record.push_back(LangOpts.CXXExceptions); |
| 1028 | Record.push_back(LangOpts.SjLjExceptions); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | dbe3927 | 2011-02-01 15:15:22 +0000 | [diff] [blame] | 1030 | Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1031 | Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime. |
| 1032 | Record.push_back(LangOpts.Freestanding); // Freestanding implementation |
| 1033 | Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin) |
| 1034 | |
Chris Lattner | 258172e | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 1035 | // Whether static initializers are protected by locks. |
| 1036 | Record.push_back(LangOpts.ThreadsafeStatics); |
Douglas Gregor | b3286fe | 2009-09-03 14:36:33 +0000 | [diff] [blame] | 1037 | Record.push_back(LangOpts.POSIXThreads); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1038 | Record.push_back(LangOpts.Blocks); // block extension to C |
| 1039 | Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if |
| 1040 | // they are unused. |
| 1041 | Record.push_back(LangOpts.MathErrno); // Math functions must respect errno |
| 1042 | // (modulo the platform support). |
| 1043 | |
Chris Lattner | 51924e51 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1044 | Record.push_back(LangOpts.getSignedOverflowBehavior()); |
| 1045 | Record.push_back(LangOpts.HeinousExtensions); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1046 | |
| 1047 | Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1049 | // defined. |
| 1050 | Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as |
| 1051 | // opposed to __DYNAMIC__). |
| 1052 | Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero. |
| 1053 | |
| 1054 | Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be |
| 1055 | // used (instead of C99 semantics). |
| 1056 | Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined. |
Anders Carlsson | 5879fbd | 2009-05-13 19:49:53 +0000 | [diff] [blame] | 1057 | Record.push_back(LangOpts.AccessControl); // Whether C++ access control should |
| 1058 | // be enabled. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 1059 | Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or |
| 1060 | // unsigned type |
John Thompson | ed4e295 | 2009-11-05 20:14:16 +0000 | [diff] [blame] | 1061 | Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short |
Argyrios Kyrtzidis | a88942a | 2011-01-15 02:56:16 +0000 | [diff] [blame] | 1062 | Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent |
| 1063 | // to the smallest integer type with |
| 1064 | // enough room. |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1065 | Record.push_back(LangOpts.getGCMode()); |
| 1066 | Record.push_back(LangOpts.getVisibilityMode()); |
Daniel Dunbar | 143021e | 2009-09-21 04:16:19 +0000 | [diff] [blame] | 1067 | Record.push_back(LangOpts.getStackProtectorMode()); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1068 | Record.push_back(LangOpts.InstantiationDepth); |
Nate Begeman | f291166 | 2009-06-25 23:01:11 +0000 | [diff] [blame] | 1069 | Record.push_back(LangOpts.OpenCL); |
Peter Collingbourne | 546d079 | 2010-12-01 19:14:57 +0000 | [diff] [blame] | 1070 | Record.push_back(LangOpts.CUDA); |
Mike Stump | d954638 | 2009-12-12 01:27:46 +0000 | [diff] [blame] | 1071 | Record.push_back(LangOpts.CatchUndefined); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 1072 | Record.push_back(LangOpts.DefaultFPContract); |
Anders Carlsson | 9cedbef | 2009-08-22 22:30:33 +0000 | [diff] [blame] | 1073 | Record.push_back(LangOpts.ElideConstructors); |
Douglas Gregor | 8ed0c0b | 2010-07-09 17:35:33 +0000 | [diff] [blame] | 1074 | Record.push_back(LangOpts.SpellChecking); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 1075 | Record.push_back(LangOpts.MRTD); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1076 | Stream.EmitRecord(LANGUAGE_OPTIONS, Record); |
Douglas Gregor | 55abb23 | 2009-04-10 20:39:37 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1079 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1080 | // stat cache Serialization |
| 1081 | //===----------------------------------------------------------------------===// |
| 1082 | |
| 1083 | namespace { |
| 1084 | // Trait used for the on-disk hash table of stat cache results. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1085 | class ASTStatCacheTrait { |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1086 | public: |
| 1087 | typedef const char * key_type; |
| 1088 | typedef key_type key_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | |
Chris Lattner | 2a6fa47 | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1090 | typedef struct stat data_type; |
| 1091 | typedef const data_type &data_type_ref; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1092 | |
| 1093 | static unsigned ComputeHash(const char *path) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 1094 | return llvm::HashString(path); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1095 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
| 1097 | std::pair<unsigned,unsigned> |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1098 | EmitKeyDataLength(llvm::raw_ostream& Out, const char *path, |
| 1099 | data_type_ref Data) { |
| 1100 | unsigned StrLen = strlen(path); |
| 1101 | clang::io::Emit16(Out, StrLen); |
Chris Lattner | 2a6fa47 | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1102 | unsigned DataLen = 4 + 4 + 2 + 8 + 8; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1103 | clang::io::Emit8(Out, DataLen); |
| 1104 | return std::make_pair(StrLen + 1, DataLen); |
| 1105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1107 | void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) { |
| 1108 | Out.write(path, KeyLen); |
| 1109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 2a6fa47 | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1111 | void EmitData(llvm::raw_ostream &Out, key_type_ref, |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1112 | data_type_ref Data, unsigned DataLen) { |
| 1113 | using namespace clang::io; |
| 1114 | uint64_t Start = Out.tell(); (void)Start; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 2a6fa47 | 2010-11-23 19:28:12 +0000 | [diff] [blame] | 1116 | Emit32(Out, (uint32_t) Data.st_ino); |
| 1117 | Emit32(Out, (uint32_t) Data.st_dev); |
| 1118 | Emit16(Out, (uint16_t) Data.st_mode); |
| 1119 | Emit64(Out, (uint64_t) Data.st_mtime); |
| 1120 | Emit64(Out, (uint64_t) Data.st_size); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1121 | |
| 1122 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 1123 | } |
| 1124 | }; |
| 1125 | } // end anonymous namespace |
| 1126 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1127 | /// \brief Write the stat() system call cache to the AST file. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1128 | void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1129 | // Build the on-disk hash table containing information about every |
| 1130 | // stat() call. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1131 | OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1132 | unsigned NumStatEntries = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | for (MemorizeStatCalls::iterator Stat = StatCalls.begin(), |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1134 | StatEnd = StatCalls.end(); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1135 | Stat != StatEnd; ++Stat, ++NumStatEntries) { |
| 1136 | const char *Filename = Stat->first(); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1137 | Generator.insert(Filename, Stat->second); |
| 1138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1140 | // Create the on-disk hash table in a buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1141 | llvm::SmallString<4096> StatCacheData; |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1142 | uint32_t BucketOffset; |
| 1143 | { |
| 1144 | llvm::raw_svector_ostream Out(StatCacheData); |
| 1145 | // Make sure that no bucket is at offset 0 |
| 1146 | clang::io::Emit32(Out, 0); |
| 1147 | BucketOffset = Generator.Emit(Out); |
| 1148 | } |
| 1149 | |
| 1150 | // Create a blob abbreviation |
| 1151 | using namespace llvm; |
| 1152 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1153 | Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE)); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1154 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1155 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1156 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1157 | unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1158 | |
| 1159 | // Write the stat cache |
| 1160 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1161 | Record.push_back(STAT_CACHE); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1162 | Record.push_back(BucketOffset); |
| 1163 | Record.push_back(NumStatEntries); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1164 | Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str()); |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | //===----------------------------------------------------------------------===// |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1168 | // Source Manager Serialization |
| 1169 | //===----------------------------------------------------------------------===// |
| 1170 | |
| 1171 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1172 | /// file. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1173 | static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1174 | using namespace llvm; |
| 1175 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1176 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1177 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1178 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1179 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1180 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1181 | // FileEntry fields. |
| 1182 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size |
| 1183 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1184 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1185 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1189 | /// buffer. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1190 | static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1191 | using namespace llvm; |
| 1192 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1193 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1194 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1195 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location |
| 1196 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic |
| 1197 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives |
| 1198 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1199 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /// \brief Create an abbreviation for the SLocEntry that refers to a |
| 1203 | /// buffer's blob. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1204 | static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1205 | using namespace llvm; |
| 1206 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1207 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1208 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1209 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /// \brief Create an abbreviation for the SLocEntry that refers to an |
| 1213 | /// buffer. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1214 | static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) { |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1215 | using namespace llvm; |
| 1216 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1217 | Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY)); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1218 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset |
| 1219 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location |
| 1220 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location |
| 1221 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location |
Douglas Gregor | 8324327 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 1222 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1223 | return Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 1226 | namespace { |
| 1227 | // Trait used for the on-disk hash table of header search information. |
| 1228 | class HeaderFileInfoTrait { |
| 1229 | ASTWriter &Writer; |
| 1230 | HeaderSearch &HS; |
| 1231 | |
| 1232 | public: |
| 1233 | HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS) |
| 1234 | : Writer(Writer), HS(HS) { } |
| 1235 | |
| 1236 | typedef const char *key_type; |
| 1237 | typedef key_type key_type_ref; |
| 1238 | |
| 1239 | typedef HeaderFileInfo data_type; |
| 1240 | typedef const data_type &data_type_ref; |
| 1241 | |
| 1242 | static unsigned ComputeHash(const char *path) { |
| 1243 | // The hash is based only on the filename portion of the key, so that the |
| 1244 | // reader can match based on filenames when symlinking or excess path |
| 1245 | // elements ("foo/../", "../") change the form of the name. However, |
| 1246 | // complete path is still the key. |
| 1247 | return llvm::HashString(llvm::sys::path::filename(path)); |
| 1248 | } |
| 1249 | |
| 1250 | std::pair<unsigned,unsigned> |
| 1251 | EmitKeyDataLength(llvm::raw_ostream& Out, const char *path, |
| 1252 | data_type_ref Data) { |
| 1253 | unsigned StrLen = strlen(path); |
| 1254 | clang::io::Emit16(Out, StrLen); |
| 1255 | unsigned DataLen = 1 + 2 + 4; |
| 1256 | clang::io::Emit8(Out, DataLen); |
| 1257 | return std::make_pair(StrLen + 1, DataLen); |
| 1258 | } |
| 1259 | |
| 1260 | void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) { |
| 1261 | Out.write(path, KeyLen); |
| 1262 | } |
| 1263 | |
| 1264 | void EmitData(llvm::raw_ostream &Out, key_type_ref, |
| 1265 | data_type_ref Data, unsigned DataLen) { |
| 1266 | using namespace clang::io; |
| 1267 | uint64_t Start = Out.tell(); (void)Start; |
| 1268 | |
| 1269 | unsigned char Flags = (Data.isImport << 3) |
| 1270 | | (Data.DirInfo << 1) |
| 1271 | | Data.Resolved; |
| 1272 | Emit8(Out, (uint8_t)Flags); |
| 1273 | Emit16(Out, (uint16_t) Data.NumIncludes); |
| 1274 | |
| 1275 | if (!Data.ControllingMacro) |
| 1276 | Emit32(Out, (uint32_t)Data.ControllingMacroID); |
| 1277 | else |
| 1278 | Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro)); |
| 1279 | assert(Out.tell() - Start == DataLen && "Wrong data length"); |
| 1280 | } |
| 1281 | }; |
| 1282 | } // end anonymous namespace |
| 1283 | |
| 1284 | /// \brief Write the header search block for the list of files that |
| 1285 | /// |
| 1286 | /// \param HS The header search structure to save. |
| 1287 | /// |
| 1288 | /// \param Chain Whether we're creating a chained AST file. |
| 1289 | void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) { |
| 1290 | llvm::SmallVector<const FileEntry *, 16> FilesByUID; |
| 1291 | HS.getFileMgr().GetUniqueIDMapping(FilesByUID); |
| 1292 | |
| 1293 | if (FilesByUID.size() > HS.header_file_size()) |
| 1294 | FilesByUID.resize(HS.header_file_size()); |
| 1295 | |
| 1296 | HeaderFileInfoTrait GeneratorTrait(*this, HS); |
| 1297 | OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator; |
| 1298 | llvm::SmallVector<const char *, 4> SavedStrings; |
| 1299 | unsigned NumHeaderSearchEntries = 0; |
| 1300 | for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { |
| 1301 | const FileEntry *File = FilesByUID[UID]; |
| 1302 | if (!File) |
| 1303 | continue; |
| 1304 | |
| 1305 | const HeaderFileInfo &HFI = HS.header_file_begin()[UID]; |
| 1306 | if (HFI.External && Chain) |
| 1307 | continue; |
| 1308 | |
| 1309 | // Turn the file name into an absolute path, if it isn't already. |
| 1310 | const char *Filename = File->getName(); |
| 1311 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
| 1312 | |
| 1313 | // If we performed any translation on the file name at all, we need to |
| 1314 | // save this string, since the generator will refer to it later. |
| 1315 | if (Filename != File->getName()) { |
| 1316 | Filename = strdup(Filename); |
| 1317 | SavedStrings.push_back(Filename); |
| 1318 | } |
| 1319 | |
| 1320 | Generator.insert(Filename, HFI, GeneratorTrait); |
| 1321 | ++NumHeaderSearchEntries; |
| 1322 | } |
| 1323 | |
| 1324 | // Create the on-disk hash table in a buffer. |
| 1325 | llvm::SmallString<4096> TableData; |
| 1326 | uint32_t BucketOffset; |
| 1327 | { |
| 1328 | llvm::raw_svector_ostream Out(TableData); |
| 1329 | // Make sure that no bucket is at offset 0 |
| 1330 | clang::io::Emit32(Out, 0); |
| 1331 | BucketOffset = Generator.Emit(Out, GeneratorTrait); |
| 1332 | } |
| 1333 | |
| 1334 | // Create a blob abbreviation |
| 1335 | using namespace llvm; |
| 1336 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1337 | Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE)); |
| 1338 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1339 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 1340 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1341 | unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1342 | |
| 1343 | // Write the stat cache |
| 1344 | RecordData Record; |
| 1345 | Record.push_back(HEADER_SEARCH_TABLE); |
| 1346 | Record.push_back(BucketOffset); |
| 1347 | Record.push_back(NumHeaderSearchEntries); |
| 1348 | Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str()); |
| 1349 | |
| 1350 | // Free all of the strings we had to duplicate. |
| 1351 | for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I) |
| 1352 | free((void*)SavedStrings[I]); |
| 1353 | } |
| 1354 | |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1355 | /// \brief Writes the block containing the serialized form of the |
| 1356 | /// source manager. |
| 1357 | /// |
| 1358 | /// TODO: We should probably use an on-disk hash table (stored in a |
| 1359 | /// blob), indexed based on the file name, so that we only create |
| 1360 | /// entries for files that we actually need. In the common case (no |
| 1361 | /// errors), we probably won't have to create file entries for any of |
| 1362 | /// the files in the AST. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1363 | void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr, |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1364 | const Preprocessor &PP, |
| 1365 | const char *isysroot) { |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1366 | RecordData Record; |
| 1367 | |
Chris Lattner | 0910e3b | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1368 | // Enter the source manager block. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1369 | Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1370 | |
| 1371 | // Abbreviations for the various kinds of source-location entries. |
Chris Lattner | c4976c73 | 2009-04-27 19:03:22 +0000 | [diff] [blame] | 1372 | unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream); |
| 1373 | unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream); |
| 1374 | unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream); |
| 1375 | unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1377 | // Write the line table. |
| 1378 | if (SourceMgr.hasLineTable()) { |
| 1379 | LineTableInfo &LineTable = SourceMgr.getLineTable(); |
| 1380 | |
| 1381 | // Emit the file names |
| 1382 | Record.push_back(LineTable.getNumFilenames()); |
| 1383 | for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) { |
| 1384 | // Emit the file name |
| 1385 | const char *Filename = LineTable.getFilename(I); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1386 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1387 | unsigned FilenameLen = Filename? strlen(Filename) : 0; |
| 1388 | Record.push_back(FilenameLen); |
| 1389 | if (FilenameLen) |
| 1390 | Record.insert(Record.end(), Filename, Filename + FilenameLen); |
| 1391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1393 | // Emit the line entries |
| 1394 | for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end(); |
| 1395 | L != LEnd; ++L) { |
| 1396 | // Emit the file ID |
| 1397 | Record.push_back(L->first); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1399 | // Emit the line entries |
| 1400 | Record.push_back(L->second.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | for (std::vector<LineEntry>::iterator LE = L->second.begin(), |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1402 | LEEnd = L->second.end(); |
| 1403 | LE != LEEnd; ++LE) { |
| 1404 | Record.push_back(LE->FileOffset); |
| 1405 | Record.push_back(LE->LineNo); |
| 1406 | Record.push_back(LE->FilenameID); |
| 1407 | Record.push_back((unsigned)LE->FileKind); |
| 1408 | Record.push_back(LE->IncludeOffset); |
| 1409 | } |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1410 | } |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1411 | Stream.EmitRecord(SM_LINE_TABLE, Record); |
Douglas Gregor | 4c7626e | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1414 | // Write out the source location entry table. We skip the first |
| 1415 | // entry, which is always the same dummy entry. |
Chris Lattner | 12d61d3 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1416 | std::vector<uint32_t> SLocEntryOffsets; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1417 | RecordData PreloadSLocs; |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1418 | unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0; |
| 1419 | SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID); |
| 1420 | for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size(); |
| 1421 | I != N; ++I) { |
Douglas Gregor | 8655e88 | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1422 | // Get this source location entry. |
| 1423 | const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I); |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1425 | // Record the offset of this source-location entry. |
| 1426 | SLocEntryOffsets.push_back(Stream.GetCurrentBitNo()); |
| 1427 | |
| 1428 | // Figure out which record code to use. |
| 1429 | unsigned Code; |
| 1430 | if (SLoc->isFile()) { |
| 1431 | if (SLoc->getFile().getContentCache()->Entry) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1432 | Code = SM_SLOC_FILE_ENTRY; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1433 | else |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1434 | Code = SM_SLOC_BUFFER_ENTRY; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1435 | } else |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1436 | Code = SM_SLOC_INSTANTIATION_ENTRY; |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1437 | Record.clear(); |
| 1438 | Record.push_back(Code); |
| 1439 | |
| 1440 | Record.push_back(SLoc->getOffset()); |
| 1441 | if (SLoc->isFile()) { |
| 1442 | const SrcMgr::FileInfo &File = SLoc->getFile(); |
| 1443 | Record.push_back(File.getIncludeLoc().getRawEncoding()); |
| 1444 | Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding |
| 1445 | Record.push_back(File.hasLineDirectives()); |
| 1446 | |
| 1447 | const SrcMgr::ContentCache *Content = File.getContentCache(); |
| 1448 | if (Content->Entry) { |
| 1449 | // The source location entry is a file. The blob associated |
| 1450 | // with this entry is the file name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | b41ca8f | 2010-03-21 22:49:54 +0000 | [diff] [blame] | 1452 | // Emit size/modification time for this file. |
| 1453 | Record.push_back(Content->Entry->getSize()); |
| 1454 | Record.push_back(Content->Entry->getModificationTime()); |
| 1455 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1456 | // Turn the file name into an absolute path, if it isn't already. |
| 1457 | const char *Filename = Content->Entry->getName(); |
Michael J. Spencer | 740857f | 2010-12-21 16:45:57 +0000 | [diff] [blame] | 1458 | llvm::SmallString<128> FilePath(Filename); |
| 1459 | llvm::sys::fs::make_absolute(FilePath); |
Kovarththanan Rajaratnam | d16d38c | 2010-03-14 07:15:57 +0000 | [diff] [blame] | 1460 | Filename = FilePath.c_str(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 1462 | Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1463 | Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1464 | } else { |
| 1465 | // The source location entry is a buffer. The blob associated |
| 1466 | // with this entry contains the contents of the buffer. |
| 1467 | |
| 1468 | // We add one to the size so that we capture the trailing NULL |
| 1469 | // that is required by llvm::MemoryBuffer::getMemBuffer (on |
| 1470 | // the reader side). |
Douglas Gregor | 874cc62 | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 1471 | const llvm::MemoryBuffer *Buffer |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1472 | = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager()); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1473 | const char *Name = Buffer->getBufferIdentifier(); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1474 | Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, |
| 1475 | llvm::StringRef(Name, strlen(Name) + 1)); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1476 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1477 | Record.push_back(SM_SLOC_BUFFER_BLOB); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1478 | Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 1479 | llvm::StringRef(Buffer->getBufferStart(), |
| 1480 | Buffer->getBufferSize() + 1)); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1481 | |
| 1482 | if (strcmp(Name, "<built-in>") == 0) |
Sebastian Redl | 5c415f3 | 2010-07-22 17:01:13 +0000 | [diff] [blame] | 1483 | PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size()); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } else { |
| 1486 | // The source location entry is an instantiation. |
| 1487 | const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation(); |
| 1488 | Record.push_back(Inst.getSpellingLoc().getRawEncoding()); |
| 1489 | Record.push_back(Inst.getInstantiationLocStart().getRawEncoding()); |
| 1490 | Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding()); |
| 1491 | |
| 1492 | // Compute the token length for this macro expansion. |
| 1493 | unsigned NextOffset = SourceMgr.getNextOffset(); |
Douglas Gregor | 8655e88 | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1494 | if (I + 1 != N) |
| 1495 | NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset(); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1496 | Record.push_back(NextOffset - SLoc->getOffset() - 1); |
| 1497 | Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1501 | Stream.ExitBlock(); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (SLocEntryOffsets.empty()) |
| 1504 | return; |
| 1505 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1506 | // Write the source-location offsets table into the AST block. This |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1507 | // table is used for lazily loading source-location information. |
| 1508 | using namespace llvm; |
| 1509 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1510 | Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS)); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1511 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs |
| 1512 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset |
| 1513 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets |
| 1514 | unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1516 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1517 | Record.push_back(SOURCE_LOCATION_OFFSETS); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1518 | Record.push_back(SLocEntryOffsets.size()); |
Sebastian Redl | c1d035f | 2010-09-22 20:19:08 +0000 | [diff] [blame] | 1519 | unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0; |
| 1520 | Record.push_back(SourceMgr.getNextOffset() - BaseOffset); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1521 | Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 1522 | (const char *)data(SLocEntryOffsets), |
Chris Lattner | 12d61d3 | 2009-04-27 19:01:47 +0000 | [diff] [blame] | 1523 | SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0])); |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1524 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1525 | // Write the source location entry preloads array, telling the AST |
Douglas Gregor | 258ae54 | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1526 | // reader which source locations entries it should load eagerly. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1527 | Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs); |
Douglas Gregor | a7f71a9 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1530 | //===----------------------------------------------------------------------===// |
| 1531 | // Preprocessor Serialization |
| 1532 | //===----------------------------------------------------------------------===// |
| 1533 | |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1534 | static int compareMacroDefinitions(const void *XPtr, const void *YPtr) { |
| 1535 | const std::pair<const IdentifierInfo *, MacroInfo *> &X = |
| 1536 | *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr; |
| 1537 | const std::pair<const IdentifierInfo *, MacroInfo *> &Y = |
| 1538 | *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr; |
| 1539 | return X.first->getName().compare(Y.first->getName()); |
| 1540 | } |
| 1541 | |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1542 | /// \brief Writes the block containing the serialized form of the |
| 1543 | /// preprocessor. |
| 1544 | /// |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1545 | void ASTWriter::WritePreprocessor(const Preprocessor &PP) { |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1546 | RecordData Record; |
Chris Lattner | 0910e3b | 2009-04-10 17:16:57 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 0af3ba1 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1548 | // If the preprocessor __COUNTER__ value has been bumped, remember it. |
| 1549 | if (PP.getCounterValue() != 0) { |
| 1550 | Record.push_back(PP.getCounterValue()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1551 | Stream.EmitRecord(PP_COUNTER_VALUE, Record); |
Chris Lattner | 0af3ba1 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 1552 | Record.clear(); |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | // Enter the preprocessor block. |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1556 | Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1558 | // If the AST file contains __DATE__ or __TIME__ emit a warning about this. |
Douglas Gregor | eda6a89 | 2009-04-26 00:07:37 +0000 | [diff] [blame] | 1559 | // FIXME: use diagnostics subsystem for localization etc. |
| 1560 | if (PP.SawDateOrTime()) |
| 1561 | fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1564 | // Loop over all the macro definitions that are live at the end of the file, |
| 1565 | // emitting each to the PP section. |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1566 | PreprocessingRecord *PPRec = PP.getPreprocessingRecord(); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1567 | |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1568 | // Construct the list of macro definitions that need to be serialized. |
| 1569 | llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2> |
| 1570 | MacrosToEmit; |
| 1571 | llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen; |
Douglas Gregor | 68051a7 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1572 | for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0), |
| 1573 | E = PP.macro_end(Chain == 0); |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1574 | I != E; ++I) { |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1575 | MacroDefinitionsSeen.insert(I->first); |
| 1576 | MacrosToEmit.push_back(std::make_pair(I->first, I->second)); |
| 1577 | } |
| 1578 | |
| 1579 | // Sort the set of macro definitions that need to be serialized by the |
| 1580 | // name of the macro, to provide a stable ordering. |
| 1581 | llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(), |
| 1582 | &compareMacroDefinitions); |
| 1583 | |
Douglas Gregor | 68051a7 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1584 | // Resolve any identifiers that defined macros at the time they were |
| 1585 | // deserialized, adding them to the list of macros to emit (if appropriate). |
| 1586 | for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) { |
| 1587 | IdentifierInfo *Name |
| 1588 | = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]); |
| 1589 | if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name)) |
| 1590 | MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name))); |
| 1591 | } |
| 1592 | |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1593 | for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) { |
| 1594 | const IdentifierInfo *Name = MacrosToEmit[I].first; |
| 1595 | MacroInfo *MI = MacrosToEmit[I].second; |
Douglas Gregor | 68051a7 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 1596 | if (!MI) |
| 1597 | continue; |
| 1598 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1599 | // Don't emit builtin macros like __LINE__ to the AST file unless they have |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1600 | // been redefined by the header (in which case they are not isBuiltinMacro). |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1601 | // Also skip macros from a AST file if we're chaining. |
Douglas Gregor | eb114da | 2010-10-01 01:03:07 +0000 | [diff] [blame] | 1602 | |
| 1603 | // FIXME: There is a (probably minor) optimization we could do here, if |
| 1604 | // the macro comes from the original PCH but the identifier comes from a |
| 1605 | // chained PCH, by storing the offset into the original PCH rather than |
| 1606 | // writing the macro definition a second time. |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1607 | if (MI->isBuiltinMacro() || |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1608 | (Chain && Name->isFromAST() && MI->isFromAST())) |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1609 | continue; |
| 1610 | |
Douglas Gregor | 2e5571d | 2011-02-10 18:20:09 +0000 | [diff] [blame] | 1611 | AddIdentifierRef(Name, Record); |
| 1612 | MacroOffsets[Name] = Stream.GetCurrentBitNo(); |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1613 | Record.push_back(MI->getDefinitionLoc().getRawEncoding()); |
| 1614 | Record.push_back(MI->isUsed()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1616 | unsigned Code; |
| 1617 | if (MI->isObjectLike()) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1618 | Code = PP_MACRO_OBJECT_LIKE; |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1619 | } else { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1620 | Code = PP_MACRO_FUNCTION_LIKE; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1622 | Record.push_back(MI->isC99Varargs()); |
| 1623 | Record.push_back(MI->isGNUVarargs()); |
| 1624 | Record.push_back(MI->getNumArgs()); |
| 1625 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 1626 | I != E; ++I) |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1627 | AddIdentifierRef(*I, Record); |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1628 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1629 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1630 | // If we have a detailed preprocessing record, record the macro definition |
| 1631 | // ID that corresponds to this macro. |
| 1632 | if (PPRec) |
| 1633 | Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI))); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1635 | Stream.EmitRecord(Code, Record); |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1636 | Record.clear(); |
| 1637 | |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1638 | // Emit the tokens array. |
| 1639 | for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) { |
| 1640 | // Note that we know that the preprocessor does not have any annotation |
| 1641 | // tokens in it because they are created by the parser, and thus can't be |
| 1642 | // in a macro definition. |
| 1643 | const Token &Tok = MI->getReplacementToken(TokNo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1645 | Record.push_back(Tok.getLocation().getRawEncoding()); |
| 1646 | Record.push_back(Tok.getLength()); |
| 1647 | |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1648 | // FIXME: When reading literal tokens, reconstruct the literal pointer if |
| 1649 | // it is needed. |
Chris Lattner | c523d8e | 2009-04-11 21:15:38 +0000 | [diff] [blame] | 1650 | AddIdentifierRef(Tok.getIdentifierInfo(), Record); |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1651 | // FIXME: Should translate token kind to a stable encoding. |
| 1652 | Record.push_back(Tok.getKind()); |
| 1653 | // FIXME: Should translate token flags to a stable encoding. |
| 1654 | Record.push_back(Tok.getFlags()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1656 | Stream.EmitRecord(PP_TOKEN, Record); |
Chris Lattner | 2199f5b | 2009-04-10 18:08:30 +0000 | [diff] [blame] | 1657 | Record.clear(); |
| 1658 | } |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 1659 | ++NumMacros; |
Chris Lattner | baa52f4 | 2009-04-10 18:00:12 +0000 | [diff] [blame] | 1660 | } |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1661 | Stream.ExitBlock(); |
| 1662 | |
| 1663 | if (PPRec) |
| 1664 | WritePreprocessorDetail(*PPRec); |
| 1665 | } |
| 1666 | |
| 1667 | void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) { |
| 1668 | if (PPRec.begin(Chain) == PPRec.end(Chain)) |
| 1669 | return; |
| 1670 | |
| 1671 | // Enter the preprocessor block. |
| 1672 | Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1673 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1674 | // If the preprocessor has a preprocessing record, emit it. |
| 1675 | unsigned NumPreprocessingRecords = 0; |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1676 | using namespace llvm; |
| 1677 | |
| 1678 | // Set up the abbreviation for |
| 1679 | unsigned InclusionAbbrev = 0; |
| 1680 | { |
| 1681 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 1682 | Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE)); |
| 1683 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index |
| 1684 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location |
| 1685 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location |
| 1686 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length |
| 1687 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes |
| 1688 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind |
| 1689 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1690 | InclusionAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1691 | } |
| 1692 | |
| 1693 | unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0; |
| 1694 | RecordData Record; |
| 1695 | for (PreprocessingRecord::iterator E = PPRec.begin(Chain), |
| 1696 | EEnd = PPRec.end(Chain); |
| 1697 | E != EEnd; ++E) { |
| 1698 | Record.clear(); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1700 | if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) { |
| 1701 | // Record this macro definition's location. |
| 1702 | MacroID ID = getMacroDefinitionID(MD); |
| 1703 | |
| 1704 | // Don't write the macro definition if it is from another AST file. |
| 1705 | if (ID < FirstMacroID) |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1706 | continue; |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1707 | |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 1708 | // Notify the serialization listener that we're serializing this entity. |
| 1709 | if (SerializationListener) |
| 1710 | SerializationListener->SerializedPreprocessedEntity(*E, |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1711 | Stream.GetCurrentBitNo()); |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1713 | unsigned Position = ID - FirstMacroID; |
| 1714 | if (Position != MacroDefinitionOffsets.size()) { |
| 1715 | if (Position > MacroDefinitionOffsets.size()) |
| 1716 | MacroDefinitionOffsets.resize(Position + 1); |
| 1717 | |
| 1718 | MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo(); |
| 1719 | } else |
| 1720 | MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo()); |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 1721 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1722 | Record.push_back(IndexBase + NumPreprocessingRecords++); |
| 1723 | Record.push_back(ID); |
| 1724 | AddSourceLocation(MD->getSourceRange().getBegin(), Record); |
| 1725 | AddSourceLocation(MD->getSourceRange().getEnd(), Record); |
| 1726 | AddIdentifierRef(MD->getName(), Record); |
| 1727 | AddSourceLocation(MD->getLocation(), Record); |
| 1728 | Stream.EmitRecord(PPD_MACRO_DEFINITION, Record); |
| 1729 | continue; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1730 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1731 | |
Douglas Gregor | 92a96f5 | 2011-02-08 21:58:10 +0000 | [diff] [blame] | 1732 | // Notify the serialization listener that we're serializing this entity. |
| 1733 | if (SerializationListener) |
| 1734 | SerializationListener->SerializedPreprocessedEntity(*E, |
| 1735 | Stream.GetCurrentBitNo()); |
| 1736 | |
| 1737 | if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) { |
| 1738 | Record.push_back(IndexBase + NumPreprocessingRecords++); |
| 1739 | AddSourceLocation(MI->getSourceRange().getBegin(), Record); |
| 1740 | AddSourceLocation(MI->getSourceRange().getEnd(), Record); |
| 1741 | AddIdentifierRef(MI->getName(), Record); |
| 1742 | Record.push_back(getMacroDefinitionID(MI->getDefinition())); |
| 1743 | Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record); |
| 1744 | continue; |
| 1745 | } |
| 1746 | |
| 1747 | if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) { |
| 1748 | Record.push_back(PPD_INCLUSION_DIRECTIVE); |
| 1749 | Record.push_back(IndexBase + NumPreprocessingRecords++); |
| 1750 | AddSourceLocation(ID->getSourceRange().getBegin(), Record); |
| 1751 | AddSourceLocation(ID->getSourceRange().getEnd(), Record); |
| 1752 | Record.push_back(ID->getFileName().size()); |
| 1753 | Record.push_back(ID->wasInQuotes()); |
| 1754 | Record.push_back(static_cast<unsigned>(ID->getKind())); |
| 1755 | llvm::SmallString<64> Buffer; |
| 1756 | Buffer += ID->getFileName(); |
| 1757 | Buffer += ID->getFile()->getName(); |
| 1758 | Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer); |
| 1759 | continue; |
| 1760 | } |
| 1761 | |
| 1762 | llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter"); |
| 1763 | } |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1764 | Stream.ExitBlock(); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1766 | // Write the offsets table for the preprocessing record. |
| 1767 | if (NumPreprocessingRecords > 0) { |
| 1768 | // Write the offsets table for identifier IDs. |
| 1769 | using namespace llvm; |
| 1770 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1771 | Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS)); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1772 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records |
| 1773 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs |
| 1774 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 1775 | unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 1776 | |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1777 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1778 | Record.push_back(MACRO_DEFINITION_OFFSETS); |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1779 | Record.push_back(NumPreprocessingRecords); |
| 1780 | Record.push_back(MacroDefinitionOffsets.size()); |
| 1781 | Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 1782 | (const char *)data(MacroDefinitionOffsets), |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 1783 | MacroDefinitionOffsets.size() * sizeof(uint32_t)); |
| 1784 | } |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 1787 | void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) { |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 1788 | RecordData Record; |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 1789 | for (Diagnostic::DiagStatePointsTy::const_iterator |
| 1790 | I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end(); |
| 1791 | I != E; ++I) { |
| 1792 | const Diagnostic::DiagStatePoint &point = *I; |
| 1793 | if (point.Loc.isInvalid()) |
| 1794 | continue; |
| 1795 | |
| 1796 | Record.push_back(point.Loc.getRawEncoding()); |
| 1797 | for (Diagnostic::DiagState::iterator |
| 1798 | I = point.State->begin(), E = point.State->end(); I != E; ++I) { |
| 1799 | unsigned diag = I->first, map = I->second; |
| 1800 | if (map & 0x10) { // mapping from a diagnostic pragma. |
| 1801 | Record.push_back(diag); |
| 1802 | Record.push_back(map & 0x7); |
| 1803 | } |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 1804 | } |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 1805 | Record.push_back(-1); // mark the end of the diag/map pairs for this |
| 1806 | // location. |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Argyrios Kyrtzidis | b0ca9eb | 2010-11-05 22:20:49 +0000 | [diff] [blame] | 1809 | if (!Record.empty()) |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 1810 | Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record); |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1813 | //===----------------------------------------------------------------------===// |
| 1814 | // Type Serialization |
| 1815 | //===----------------------------------------------------------------------===// |
Chris Lattner | eeffaef | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1816 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1817 | /// \brief Write the representation of a type to the AST stream. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1818 | void ASTWriter::WriteType(QualType T) { |
Argyrios Kyrtzidis | a7fbbb0 | 2010-08-20 16:04:04 +0000 | [diff] [blame] | 1819 | TypeIdx &Idx = TypeIdxs[T]; |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 1820 | if (Idx.getIndex() == 0) // we haven't seen this type before. |
| 1821 | Idx = TypeIdx(NextTypeID++); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 1823 | assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST"); |
Douglas Gregor | dc72caa | 2010-10-04 18:21:45 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1825 | // Record the offset for this type. |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 1826 | unsigned Index = Idx.getIndex() - FirstTypeID; |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1827 | if (TypeOffsets.size() == Index) |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1828 | TypeOffsets.push_back(Stream.GetCurrentBitNo()); |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1829 | else if (TypeOffsets.size() < Index) { |
| 1830 | TypeOffsets.resize(Index + 1); |
| 1831 | TypeOffsets[Index] = Stream.GetCurrentBitNo(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | RecordData Record; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1836 | // Emit the type's representation. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1837 | ASTTypeWriter W(*this, Record); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1839 | if (T.hasLocalNonFastQualifiers()) { |
| 1840 | Qualifiers Qs = T.getLocalQualifiers(); |
| 1841 | AddTypeRef(T.getLocalUnqualifiedType(), Record); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1842 | Record.push_back(Qs.getAsOpaqueValue()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1843 | W.Code = TYPE_EXT_QUAL; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1844 | } else { |
| 1845 | switch (T->getTypeClass()) { |
| 1846 | // For all of the concrete, non-dependent types, call the |
| 1847 | // appropriate visitor function. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1848 | #define TYPE(Class, Base) \ |
Mike Stump | 281d6d7 | 2010-01-20 02:03:14 +0000 | [diff] [blame] | 1849 | case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1850 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1851 | #include "clang/AST/TypeNodes.def" |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1852 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | // Emit the serialized record. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1856 | Stream.EmitRecord(W.Code, Record); |
Douglas Gregor | feb84b0 | 2009-04-14 21:18:50 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Flush any expressions that were written as part of this type. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1859 | FlushStmts(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1862 | //===----------------------------------------------------------------------===// |
| 1863 | // Declaration Serialization |
| 1864 | //===----------------------------------------------------------------------===// |
| 1865 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1866 | /// \brief Write the block containing all of the declaration IDs |
| 1867 | /// lexically declared within the given DeclContext. |
| 1868 | /// |
| 1869 | /// \returns the offset of the DECL_CONTEXT_LEXICAL block within the |
| 1870 | /// bistream, or 0 if no block was written. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1871 | uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1872 | DeclContext *DC) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1873 | if (DC->decls_empty()) |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1874 | return 0; |
| 1875 | |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 1876 | uint64_t Offset = Stream.GetCurrentBitNo(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1877 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1878 | Record.push_back(DECL_CONTEXT_LEXICAL); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1879 | llvm::SmallVector<KindDeclIDPair, 64> Decls; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1880 | for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end(); |
| 1881 | D != DEnd; ++D) |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1882 | Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D))); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | a57c3ab | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 1884 | ++NumLexicalDeclContexts; |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 1885 | Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 1886 | reinterpret_cast<char*>(Decls.data()), |
| 1887 | Decls.size() * sizeof(KindDeclIDPair)); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1888 | return Offset; |
| 1889 | } |
| 1890 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1891 | void ASTWriter::WriteTypeDeclOffsets() { |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1892 | using namespace llvm; |
| 1893 | RecordData Record; |
| 1894 | |
| 1895 | // Write the type offsets array |
| 1896 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1897 | Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET)); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1898 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types |
| 1899 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block |
| 1900 | unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1901 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1902 | Record.push_back(TYPE_OFFSET); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1903 | Record.push_back(TypeOffsets.size()); |
| 1904 | Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 1905 | (const char *)data(TypeOffsets), |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1906 | TypeOffsets.size() * sizeof(TypeOffsets[0])); |
| 1907 | |
| 1908 | // Write the declaration offsets array |
| 1909 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1910 | Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET)); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1911 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations |
| 1912 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block |
| 1913 | unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 1914 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1915 | Record.push_back(DECL_OFFSET); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1916 | Record.push_back(DeclOffsets.size()); |
| 1917 | Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 1918 | (const char *)data(DeclOffsets), |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 1919 | DeclOffsets.size() * sizeof(DeclOffsets[0])); |
| 1920 | } |
| 1921 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1922 | //===----------------------------------------------------------------------===// |
| 1923 | // Global Method Pool and Selector Serialization |
| 1924 | //===----------------------------------------------------------------------===// |
| 1925 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 1926 | namespace { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1927 | // Trait used for the on-disk hash table used in the method pool. |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1928 | class ASTMethodPoolTrait { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 1929 | ASTWriter &Writer; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1930 | |
| 1931 | public: |
| 1932 | typedef Selector key_type; |
| 1933 | typedef key_type key_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1935 | struct data_type { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 1936 | SelectorID ID; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1937 | ObjCMethodList Instance, Factory; |
| 1938 | }; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1939 | typedef const data_type& data_type_ref; |
| 1940 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 1941 | explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1943 | static unsigned ComputeHash(Selector Sel) { |
Argyrios Kyrtzidis | 4bd9710 | 2010-08-20 16:03:52 +0000 | [diff] [blame] | 1944 | return serialization::ComputeHash(Sel); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1945 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
| 1947 | std::pair<unsigned,unsigned> |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1948 | EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel, |
| 1949 | data_type_ref Methods) { |
| 1950 | unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4); |
| 1951 | clang::io::Emit16(Out, KeyLen); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1952 | unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts |
| 1953 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1954 | Method = Method->Next) |
| 1955 | if (Method->Method) |
| 1956 | DataLen += 4; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1957 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1958 | Method = Method->Next) |
| 1959 | if (Method->Method) |
| 1960 | DataLen += 4; |
| 1961 | clang::io::Emit16(Out, DataLen); |
| 1962 | return std::make_pair(KeyLen, DataLen); |
| 1963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1965 | void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | uint64_t Start = Out.tell(); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 1967 | assert((Start >> 32) == 0 && "Selector key offset too large"); |
| 1968 | Writer.SetSelectorOffset(Sel, Start); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1969 | unsigned N = Sel.getNumArgs(); |
| 1970 | clang::io::Emit16(Out, N); |
| 1971 | if (N == 0) |
| 1972 | N = 1; |
| 1973 | for (unsigned I = 0; I != N; ++I) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | clang::io::Emit32(Out, |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1975 | Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I))); |
| 1976 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1978 | void EmitData(llvm::raw_ostream& Out, key_type_ref, |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 1979 | data_type_ref Methods, unsigned DataLen) { |
| 1980 | uint64_t Start = Out.tell(); (void)Start; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1981 | clang::io::Emit32(Out, Methods.ID); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1982 | unsigned NumInstanceMethods = 0; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1983 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1984 | Method = Method->Next) |
| 1985 | if (Method->Method) |
| 1986 | ++NumInstanceMethods; |
| 1987 | |
| 1988 | unsigned NumFactoryMethods = 0; |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1989 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1990 | Method = Method->Next) |
| 1991 | if (Method->Method) |
| 1992 | ++NumFactoryMethods; |
| 1993 | |
| 1994 | clang::io::Emit16(Out, NumInstanceMethods); |
| 1995 | clang::io::Emit16(Out, NumFactoryMethods); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 1996 | for (const ObjCMethodList *Method = &Methods.Instance; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1997 | Method = Method->Next) |
| 1998 | if (Method->Method) |
| 1999 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2000 | for (const ObjCMethodList *Method = &Methods.Factory; Method; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2001 | Method = Method->Next) |
| 2002 | if (Method->Method) |
| 2003 | clang::io::Emit32(Out, Writer.getDeclID(Method->Method)); |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2004 | |
| 2005 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2006 | } |
| 2007 | }; |
| 2008 | } // end anonymous namespace |
| 2009 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2010 | /// \brief Write ObjC data: selectors and the method pool. |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2011 | /// |
| 2012 | /// The method pool contains both instance and factory methods, stored |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2013 | /// in an on-disk hash table indexed by the selector. The hash table also |
| 2014 | /// contains an empty entry for every other selector known to Sema. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2015 | void ASTWriter::WriteSelectors(Sema &SemaRef) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2016 | using namespace llvm; |
| 2017 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2018 | // Do we have to do anything at all? |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2019 | if (SemaRef.MethodPool.empty() && SelectorIDs.empty()) |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2020 | return; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2021 | unsigned NumTableEntries = 0; |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2022 | // Create and write out the blob that contains selectors and the method pool. |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2023 | { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2024 | OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2025 | ASTMethodPoolTrait Trait(*this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2027 | // Create the on-disk hash table representation. We walk through every |
| 2028 | // selector we've seen and look it up in the method pool. |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2029 | SelectorOffsets.resize(NextSelectorID - FirstSelectorID); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2030 | for (llvm::DenseMap<Selector, SelectorID>::iterator |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2031 | I = SelectorIDs.begin(), E = SelectorIDs.end(); |
| 2032 | I != E; ++I) { |
| 2033 | Selector S = I->first; |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2034 | Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S); |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2035 | ASTMethodPoolTrait::data_type Data = { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 2036 | I->second, |
| 2037 | ObjCMethodList(), |
| 2038 | ObjCMethodList() |
| 2039 | }; |
| 2040 | if (F != SemaRef.MethodPool.end()) { |
| 2041 | Data.Instance = F->second.first; |
| 2042 | Data.Factory = F->second.second; |
| 2043 | } |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2044 | // Only write this selector if it's not in an existing AST or something |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2045 | // changed. |
| 2046 | if (Chain && I->second < FirstSelectorID) { |
| 2047 | // Selector already exists. Did it change? |
| 2048 | bool changed = false; |
| 2049 | for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method; |
| 2050 | M = M->Next) { |
| 2051 | if (M->Method->getPCHLevel() == 0) |
| 2052 | changed = true; |
| 2053 | } |
| 2054 | for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method; |
| 2055 | M = M->Next) { |
| 2056 | if (M->Method->getPCHLevel() == 0) |
| 2057 | changed = true; |
| 2058 | } |
| 2059 | if (!changed) |
| 2060 | continue; |
Sebastian Redl | 6e1a2a0 | 2010-08-04 21:22:45 +0000 | [diff] [blame] | 2061 | } else if (Data.Instance.Method || Data.Factory.Method) { |
| 2062 | // A new method pool entry. |
| 2063 | ++NumTableEntries; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2064 | } |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2065 | Generator.insert(S, Data, Trait); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2068 | // Create the on-disk hash table in a buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | llvm::SmallString<4096> MethodPool; |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2070 | uint32_t BucketOffset; |
| 2071 | { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2072 | ASTMethodPoolTrait Trait(*this); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2073 | llvm::raw_svector_ostream Out(MethodPool); |
| 2074 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2075 | clang::io::Emit32(Out, 0); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2076 | BucketOffset = Generator.Emit(Out, Trait); |
| 2077 | } |
| 2078 | |
| 2079 | // Create a blob abbreviation |
| 2080 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2081 | Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL)); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2082 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2083 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2084 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2085 | unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2086 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2087 | // Write the method pool |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2088 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2089 | Record.push_back(METHOD_POOL); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2090 | Record.push_back(BucketOffset); |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2091 | Record.push_back(NumTableEntries); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 2092 | Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str()); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2093 | |
| 2094 | // Create a blob abbreviation for the selector table offsets. |
| 2095 | Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2096 | Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS)); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 2097 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2098 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2099 | unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2100 | |
| 2101 | // Write the selector offsets table. |
| 2102 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2103 | Record.push_back(SELECTOR_OFFSETS); |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2104 | Record.push_back(SelectorOffsets.size()); |
| 2105 | Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 2106 | (const char *)data(SelectorOffsets), |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2107 | SelectorOffsets.size() * 4); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2111 | /// \brief Write the selectors referenced in @selector expression into AST file. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2112 | void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) { |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2113 | using namespace llvm; |
| 2114 | if (SemaRef.ReferencedSelectors.empty()) |
| 2115 | return; |
Sebastian Redl | ada023c | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 2116 | |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2117 | RecordData Record; |
Sebastian Redl | ada023c | 2010-08-04 20:40:17 +0000 | [diff] [blame] | 2118 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2119 | // Note: this writes out all references even for a dependent AST. But it is |
Sebastian Redl | 51c79d8 | 2010-08-04 22:21:29 +0000 | [diff] [blame] | 2120 | // very tricky to fix, and given that @selector shouldn't really appear in |
| 2121 | // headers, probably not worth it. It's not a correctness issue. |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2122 | for (DenseMap<Selector, SourceLocation>::iterator S = |
| 2123 | SemaRef.ReferencedSelectors.begin(), |
| 2124 | E = SemaRef.ReferencedSelectors.end(); S != E; ++S) { |
| 2125 | Selector Sel = (*S).first; |
| 2126 | SourceLocation Loc = (*S).second; |
| 2127 | AddSelectorRef(Sel, Record); |
| 2128 | AddSourceLocation(Loc, Record); |
| 2129 | } |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2130 | Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2133 | //===----------------------------------------------------------------------===// |
| 2134 | // Identifier Table Serialization |
| 2135 | //===----------------------------------------------------------------------===// |
| 2136 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2137 | namespace { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2138 | class ASTIdentifierTableTrait { |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2139 | ASTWriter &Writer; |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2140 | Preprocessor &PP; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2142 | /// \brief Determines whether this is an "interesting" identifier |
| 2143 | /// that needs a full IdentifierInfo structure written into the hash |
| 2144 | /// table. |
| 2145 | static bool isInterestingIdentifier(const IdentifierInfo *II) { |
| 2146 | return II->isPoisoned() || |
| 2147 | II->isExtensionToken() || |
| 2148 | II->hasMacroDefinition() || |
| 2149 | II->getObjCOrBuiltinID() || |
| 2150 | II->getFETokenInfo<void>(); |
| 2151 | } |
| 2152 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2153 | public: |
| 2154 | typedef const IdentifierInfo* key_type; |
| 2155 | typedef key_type key_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2157 | typedef IdentID data_type; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2158 | typedef data_type data_type_ref; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2160 | ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP) |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2161 | : Writer(Writer), PP(PP) { } |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2162 | |
| 2163 | static unsigned ComputeHash(const IdentifierInfo* II) { |
Daniel Dunbar | f8502d5 | 2009-10-17 23:52:28 +0000 | [diff] [blame] | 2164 | return llvm::HashString(II->getName()); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2165 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
| 2167 | std::pair<unsigned,unsigned> |
| 2168 | EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II, |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2169 | IdentID ID) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2170 | unsigned KeyLen = II->getLength() + 1; |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2171 | unsigned DataLen = 4; // 4 bytes for the persistent ID << 1 |
| 2172 | if (isInterestingIdentifier(II)) { |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2173 | DataLen += 2; // 2 bytes for builtin ID, flags |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | if (II->hasMacroDefinition() && |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2175 | !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro()) |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2176 | DataLen += 4; |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2177 | for (IdentifierResolver::iterator D = IdentifierResolver::begin(II), |
| 2178 | DEnd = IdentifierResolver::end(); |
| 2179 | D != DEnd; ++D) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2180 | DataLen += sizeof(DeclID); |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2181 | } |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2182 | clang::io::Emit16(Out, DataLen); |
Douglas Gregor | ab4df58 | 2009-04-28 20:01:51 +0000 | [diff] [blame] | 2183 | // We emit the key length after the data length so that every |
| 2184 | // string is preceded by a 16-bit length. This matches the PTH |
| 2185 | // format for storing identifiers. |
Douglas Gregor | 5287b4e | 2009-04-25 21:04:17 +0000 | [diff] [blame] | 2186 | clang::io::Emit16(Out, KeyLen); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2187 | return std::make_pair(KeyLen, DataLen); |
| 2188 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
| 2190 | void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II, |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2191 | unsigned KeyLen) { |
| 2192 | // Record the location of the key data. This is used when generating |
| 2193 | // the mapping from persistent IDs to strings. |
| 2194 | Writer.SetIdentifierOffset(II, Out.tell()); |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2195 | Out.write(II->getNameStart(), KeyLen); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
| 2198 | void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II, |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2199 | IdentID ID, unsigned) { |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2200 | if (!isInterestingIdentifier(II)) { |
| 2201 | clang::io::Emit32(Out, ID << 1); |
| 2202 | return; |
| 2203 | } |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | 1d583f2 | 2009-04-28 21:18:29 +0000 | [diff] [blame] | 2205 | clang::io::Emit32(Out, (ID << 1) | 0x01); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2206 | uint32_t Bits = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | bool hasMacroDefinition = |
| 2208 | II->hasMacroDefinition() && |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2209 | !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro(); |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2210 | Bits = (uint32_t)II->getObjCOrBuiltinID(); |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2211 | Bits = (Bits << 1) | unsigned(hasMacroDefinition); |
| 2212 | Bits = (Bits << 1) | unsigned(II->isExtensionToken()); |
| 2213 | Bits = (Bits << 1) | unsigned(II->isPoisoned()); |
Argyrios Kyrtzidis | 3084a61 | 2010-08-11 22:55:12 +0000 | [diff] [blame] | 2214 | Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier()); |
Daniel Dunbar | 91b640a | 2009-12-18 20:58:47 +0000 | [diff] [blame] | 2215 | Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword()); |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2216 | clang::io::Emit16(Out, Bits); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2218 | if (hasMacroDefinition) |
Douglas Gregor | b925652 | 2009-04-28 21:32:13 +0000 | [diff] [blame] | 2219 | clang::io::Emit32(Out, Writer.getMacroOffset(II)); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2221 | // Emit the declaration IDs in reverse order, because the |
| 2222 | // IdentifierResolver provides the declarations as they would be |
| 2223 | // visible (e.g., the function "stat" would come before the struct |
| 2224 | // "stat"), but IdentifierResolver::AddDeclToIdentifierChain() |
| 2225 | // adds declarations to the end of the list (so we need to see the |
| 2226 | // struct "status" before the function "status"). |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2227 | // Only emit declarations that aren't from a chained PCH, though. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II), |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2229 | IdentifierResolver::end()); |
| 2230 | for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(), |
| 2231 | DEnd = Decls.rend(); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2232 | D != DEnd; ++D) |
Sebastian Redl | 78f5177 | 2010-08-02 18:30:12 +0000 | [diff] [blame] | 2233 | clang::io::Emit32(Out, Writer.getDeclID(*D)); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2234 | } |
| 2235 | }; |
| 2236 | } // end anonymous namespace |
| 2237 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2238 | /// \brief Write the identifier table into the AST file. |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2239 | /// |
| 2240 | /// The identifier table consists of a blob containing string data |
| 2241 | /// (the actual identifiers themselves) and a separate "offsets" index |
| 2242 | /// that maps identifier IDs to locations within the blob. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2243 | void ASTWriter::WriteIdentifierTable(Preprocessor &PP) { |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2244 | using namespace llvm; |
| 2245 | |
| 2246 | // Create and write out the blob that contains the identifier |
| 2247 | // strings. |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2248 | { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2249 | OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator; |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2250 | ASTIdentifierTableTrait Trait(*this, PP); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 2252 | // Look for any identifiers that were named while processing the |
| 2253 | // headers, but are otherwise not needed. We add these to the hash |
| 2254 | // table to enable checking of the predefines buffer in the case |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2255 | // where the user adds new macro definitions when building the AST |
Douglas Gregor | e6648fb | 2009-04-28 20:33:11 +0000 | [diff] [blame] | 2256 | // file. |
| 2257 | for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(), |
| 2258 | IDEnd = PP.getIdentifierTable().end(); |
| 2259 | ID != IDEnd; ++ID) |
| 2260 | getIdentifierRef(ID->second); |
| 2261 | |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2262 | // Create the on-disk hash table representation. We only store offsets |
| 2263 | // for identifiers that appear here for the first time. |
| 2264 | IdentifierOffsets.resize(NextIdentID - FirstIdentID); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2265 | for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2266 | ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end(); |
| 2267 | ID != IDEnd; ++ID) { |
| 2268 | assert(ID->first && "NULL identifier in identifier table"); |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 2269 | if (!Chain || !ID->first->isFromAST()) |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2270 | Generator.insert(ID->first, ID->second, Trait); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2271 | } |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2272 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2273 | // Create the on-disk hash table in a buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | llvm::SmallString<4096> IdentifierTable; |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2275 | uint32_t BucketOffset; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2276 | { |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2277 | ASTIdentifierTableTrait Trait(*this, PP); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2278 | llvm::raw_svector_ostream Out(IdentifierTable); |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 2279 | // Make sure that no bucket is at offset 0 |
Douglas Gregor | 4647cfa | 2009-04-24 21:49:02 +0000 | [diff] [blame] | 2280 | clang::io::Emit32(Out, 0); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2281 | BucketOffset = Generator.Emit(Out, Trait); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | // Create a blob abbreviation |
| 2285 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2286 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE)); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2287 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2288 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2289 | unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2290 | |
| 2291 | // Write the identifier table |
| 2292 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2293 | Record.push_back(IDENTIFIER_TABLE); |
Douglas Gregor | a868bbd | 2009-04-21 22:25:48 +0000 | [diff] [blame] | 2294 | Record.push_back(BucketOffset); |
Daniel Dunbar | 8100d01 | 2009-08-24 09:31:37 +0000 | [diff] [blame] | 2295 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str()); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | // Write the offsets table for identifier IDs. |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2299 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2300 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET)); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2301 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers |
| 2302 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2303 | unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2304 | |
| 2305 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2306 | Record.push_back(IDENTIFIER_OFFSET); |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2307 | Record.push_back(IdentifierOffsets.size()); |
| 2308 | Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record, |
Sebastian Redl | 3df5a08 | 2010-07-30 17:03:48 +0000 | [diff] [blame] | 2309 | (const char *)data(IdentifierOffsets), |
Douglas Gregor | 0e14997 | 2009-04-25 19:10:14 +0000 | [diff] [blame] | 2310 | IdentifierOffsets.size() * sizeof(uint32_t)); |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2313 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2314 | // DeclContext's Name Lookup Table Serialization |
| 2315 | //===----------------------------------------------------------------------===// |
| 2316 | |
| 2317 | namespace { |
| 2318 | // Trait used for the on-disk hash table used in the method pool. |
| 2319 | class ASTDeclContextNameLookupTrait { |
| 2320 | ASTWriter &Writer; |
| 2321 | |
| 2322 | public: |
| 2323 | typedef DeclarationName key_type; |
| 2324 | typedef key_type key_type_ref; |
| 2325 | |
| 2326 | typedef DeclContext::lookup_result data_type; |
| 2327 | typedef const data_type& data_type_ref; |
| 2328 | |
| 2329 | explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { } |
| 2330 | |
| 2331 | unsigned ComputeHash(DeclarationName Name) { |
| 2332 | llvm::FoldingSetNodeID ID; |
| 2333 | ID.AddInteger(Name.getNameKind()); |
| 2334 | |
| 2335 | switch (Name.getNameKind()) { |
| 2336 | case DeclarationName::Identifier: |
| 2337 | ID.AddString(Name.getAsIdentifierInfo()->getName()); |
| 2338 | break; |
| 2339 | case DeclarationName::ObjCZeroArgSelector: |
| 2340 | case DeclarationName::ObjCOneArgSelector: |
| 2341 | case DeclarationName::ObjCMultiArgSelector: |
| 2342 | ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector())); |
| 2343 | break; |
| 2344 | case DeclarationName::CXXConstructorName: |
| 2345 | case DeclarationName::CXXDestructorName: |
| 2346 | case DeclarationName::CXXConversionFunctionName: |
| 2347 | ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType())); |
| 2348 | break; |
| 2349 | case DeclarationName::CXXOperatorName: |
| 2350 | ID.AddInteger(Name.getCXXOverloadedOperator()); |
| 2351 | break; |
| 2352 | case DeclarationName::CXXLiteralOperatorName: |
| 2353 | ID.AddString(Name.getCXXLiteralIdentifier()->getName()); |
| 2354 | case DeclarationName::CXXUsingDirective: |
| 2355 | break; |
| 2356 | } |
| 2357 | |
| 2358 | return ID.ComputeHash(); |
| 2359 | } |
| 2360 | |
| 2361 | std::pair<unsigned,unsigned> |
| 2362 | EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name, |
| 2363 | data_type_ref Lookup) { |
| 2364 | unsigned KeyLen = 1; |
| 2365 | switch (Name.getNameKind()) { |
| 2366 | case DeclarationName::Identifier: |
| 2367 | case DeclarationName::ObjCZeroArgSelector: |
| 2368 | case DeclarationName::ObjCOneArgSelector: |
| 2369 | case DeclarationName::ObjCMultiArgSelector: |
| 2370 | case DeclarationName::CXXConstructorName: |
| 2371 | case DeclarationName::CXXDestructorName: |
| 2372 | case DeclarationName::CXXConversionFunctionName: |
| 2373 | case DeclarationName::CXXLiteralOperatorName: |
| 2374 | KeyLen += 4; |
| 2375 | break; |
| 2376 | case DeclarationName::CXXOperatorName: |
| 2377 | KeyLen += 1; |
| 2378 | break; |
| 2379 | case DeclarationName::CXXUsingDirective: |
| 2380 | break; |
| 2381 | } |
| 2382 | clang::io::Emit16(Out, KeyLen); |
| 2383 | |
| 2384 | // 2 bytes for num of decls and 4 for each DeclID. |
| 2385 | unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first); |
| 2386 | clang::io::Emit16(Out, DataLen); |
| 2387 | |
| 2388 | return std::make_pair(KeyLen, DataLen); |
| 2389 | } |
| 2390 | |
| 2391 | void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) { |
| 2392 | using namespace clang::io; |
| 2393 | |
| 2394 | assert(Name.getNameKind() < 0x100 && "Invalid name kind ?"); |
| 2395 | Emit8(Out, Name.getNameKind()); |
| 2396 | switch (Name.getNameKind()) { |
| 2397 | case DeclarationName::Identifier: |
| 2398 | Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo())); |
| 2399 | break; |
| 2400 | case DeclarationName::ObjCZeroArgSelector: |
| 2401 | case DeclarationName::ObjCOneArgSelector: |
| 2402 | case DeclarationName::ObjCMultiArgSelector: |
| 2403 | Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector())); |
| 2404 | break; |
| 2405 | case DeclarationName::CXXConstructorName: |
| 2406 | case DeclarationName::CXXDestructorName: |
| 2407 | case DeclarationName::CXXConversionFunctionName: |
| 2408 | Emit32(Out, Writer.getTypeID(Name.getCXXNameType())); |
| 2409 | break; |
| 2410 | case DeclarationName::CXXOperatorName: |
| 2411 | assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?"); |
| 2412 | Emit8(Out, Name.getCXXOverloadedOperator()); |
| 2413 | break; |
| 2414 | case DeclarationName::CXXLiteralOperatorName: |
| 2415 | Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier())); |
| 2416 | break; |
| 2417 | case DeclarationName::CXXUsingDirective: |
| 2418 | break; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | void EmitData(llvm::raw_ostream& Out, key_type_ref, |
| 2423 | data_type Lookup, unsigned DataLen) { |
| 2424 | uint64_t Start = Out.tell(); (void)Start; |
| 2425 | clang::io::Emit16(Out, Lookup.second - Lookup.first); |
| 2426 | for (; Lookup.first != Lookup.second; ++Lookup.first) |
| 2427 | clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first)); |
| 2428 | |
| 2429 | assert(Out.tell() - Start == DataLen && "Data length is wrong"); |
| 2430 | } |
| 2431 | }; |
| 2432 | } // end anonymous namespace |
| 2433 | |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2434 | /// \brief Write the block containing all of the declaration IDs |
| 2435 | /// visible from the given DeclContext. |
| 2436 | /// |
| 2437 | /// \returns the offset of the DECL_CONTEXT_VISIBLE block within the |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2438 | /// bitstream, or 0 if no block was written. |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2439 | uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context, |
| 2440 | DeclContext *DC) { |
| 2441 | if (DC->getPrimaryContext() != DC) |
| 2442 | return 0; |
| 2443 | |
| 2444 | // Since there is no name lookup into functions or methods, don't bother to |
| 2445 | // build a visible-declarations table for these entities. |
| 2446 | if (DC->isFunctionOrMethod()) |
| 2447 | return 0; |
| 2448 | |
| 2449 | // If not in C++, we perform name lookup for the translation unit via the |
| 2450 | // IdentifierInfo chains, don't bother to build a visible-declarations table. |
| 2451 | // FIXME: In C++ we need the visible declarations in order to "see" the |
| 2452 | // friend declarations, is there a way to do this without writing the table ? |
| 2453 | if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus) |
| 2454 | return 0; |
| 2455 | |
| 2456 | // Force the DeclContext to build a its name-lookup table. |
Argyrios Kyrtzidis | d32ee89 | 2010-08-20 23:35:55 +0000 | [diff] [blame] | 2457 | if (DC->hasExternalVisibleStorage()) |
| 2458 | DC->MaterializeVisibleDeclsFromExternalStorage(); |
| 2459 | else |
| 2460 | DC->lookup(DeclarationName()); |
Argyrios Kyrtzidis | ba88bfa | 2010-08-20 16:04:35 +0000 | [diff] [blame] | 2461 | |
| 2462 | // Serialize the contents of the mapping used for lookup. Note that, |
| 2463 | // although we have two very different code paths, the serialized |
| 2464 | // representation is the same for both cases: a declaration name, |
| 2465 | // followed by a size, followed by references to the visible |
| 2466 | // declarations that have that name. |
| 2467 | uint64_t Offset = Stream.GetCurrentBitNo(); |
| 2468 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 2469 | if (!Map || Map->empty()) |
| 2470 | return 0; |
| 2471 | |
| 2472 | OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; |
| 2473 | ASTDeclContextNameLookupTrait Trait(*this); |
| 2474 | |
| 2475 | // Create the on-disk hash table representation. |
| 2476 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 2477 | D != DEnd; ++D) { |
| 2478 | DeclarationName Name = D->first; |
| 2479 | DeclContext::lookup_result Result = D->second.getLookupResult(); |
| 2480 | Generator.insert(Name, Result, Trait); |
| 2481 | } |
| 2482 | |
| 2483 | // Create the on-disk hash table in a buffer. |
| 2484 | llvm::SmallString<4096> LookupTable; |
| 2485 | uint32_t BucketOffset; |
| 2486 | { |
| 2487 | llvm::raw_svector_ostream Out(LookupTable); |
| 2488 | // Make sure that no bucket is at offset 0 |
| 2489 | clang::io::Emit32(Out, 0); |
| 2490 | BucketOffset = Generator.Emit(Out, Trait); |
| 2491 | } |
| 2492 | |
| 2493 | // Write the lookup table |
| 2494 | RecordData Record; |
| 2495 | Record.push_back(DECL_CONTEXT_VISIBLE); |
| 2496 | Record.push_back(BucketOffset); |
| 2497 | Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record, |
| 2498 | LookupTable.str()); |
| 2499 | |
| 2500 | Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record); |
| 2501 | ++NumVisibleDeclContexts; |
| 2502 | return Offset; |
| 2503 | } |
| 2504 | |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2505 | /// \brief Write an UPDATE_VISIBLE block for the given context. |
| 2506 | /// |
| 2507 | /// UPDATE_VISIBLE blocks contain the declarations that are added to an existing |
| 2508 | /// DeclContext in a dependent AST file. As such, they only exist for the TU |
| 2509 | /// (in C++) and for namespaces. |
| 2510 | void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2511 | StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr()); |
| 2512 | if (!Map || Map->empty()) |
| 2513 | return; |
| 2514 | |
| 2515 | OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; |
| 2516 | ASTDeclContextNameLookupTrait Trait(*this); |
| 2517 | |
| 2518 | // Create the hash table. |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2519 | for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end(); |
| 2520 | D != DEnd; ++D) { |
| 2521 | DeclarationName Name = D->first; |
| 2522 | DeclContext::lookup_result Result = D->second.getLookupResult(); |
Sebastian Redl | 9617e7e | 2010-08-24 00:50:16 +0000 | [diff] [blame] | 2523 | // For any name that appears in this table, the results are complete, i.e. |
| 2524 | // they overwrite results from previous PCHs. Merging is always a mess. |
| 2525 | Generator.insert(Name, Result, Trait); |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | // Create the on-disk hash table in a buffer. |
| 2529 | llvm::SmallString<4096> LookupTable; |
| 2530 | uint32_t BucketOffset; |
| 2531 | { |
| 2532 | llvm::raw_svector_ostream Out(LookupTable); |
| 2533 | // Make sure that no bucket is at offset 0 |
| 2534 | clang::io::Emit32(Out, 0); |
| 2535 | BucketOffset = Generator.Emit(Out, Trait); |
| 2536 | } |
| 2537 | |
| 2538 | // Write the lookup table |
| 2539 | RecordData Record; |
| 2540 | Record.push_back(UPDATE_VISIBLE); |
| 2541 | Record.push_back(getDeclID(cast<Decl>(DC))); |
| 2542 | Record.push_back(BucketOffset); |
| 2543 | Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); |
| 2544 | } |
| 2545 | |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 2546 | /// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions. |
| 2547 | void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) { |
| 2548 | RecordData Record; |
| 2549 | Record.push_back(Opts.fp_contract); |
| 2550 | Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record); |
| 2551 | } |
| 2552 | |
| 2553 | /// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions. |
| 2554 | void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) { |
| 2555 | if (!SemaRef.Context.getLangOptions().OpenCL) |
| 2556 | return; |
| 2557 | |
| 2558 | const OpenCLOptions &Opts = SemaRef.getOpenCLOptions(); |
| 2559 | RecordData Record; |
| 2560 | #define OPENCLEXT(nm) Record.push_back(Opts.nm); |
| 2561 | #include "clang/Basic/OpenCLExtensions.def" |
| 2562 | Stream.EmitRecord(OPENCL_EXTENSIONS, Record); |
| 2563 | } |
| 2564 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 2565 | //===----------------------------------------------------------------------===// |
Douglas Gregor | c504683 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 2566 | // General Serialization Routines |
| 2567 | //===----------------------------------------------------------------------===// |
| 2568 | |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2569 | /// \brief Write a record containing the given attributes. |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 2570 | void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 9beef8e | 2010-10-18 19:20:11 +0000 | [diff] [blame] | 2571 | Record.push_back(Attrs.size()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2572 | for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){ |
| 2573 | const Attr * A = *i; |
| 2574 | Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs |
| 2575 | AddSourceLocation(A->getLocation(), Record); |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2576 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2577 | #include "clang/Serialization/AttrPCHWrite.inc" |
Daniel Dunbar | fc6507e | 2010-05-27 02:25:39 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2579 | } |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 2582 | void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) { |
Douglas Gregor | bc8a78d5 | 2009-04-15 21:30:51 +0000 | [diff] [blame] | 2583 | Record.push_back(Str.size()); |
| 2584 | Record.insert(Record.end(), Str.begin(), Str.end()); |
| 2585 | } |
| 2586 | |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2587 | /// \brief Note that the identifier II occurs at the given offset |
| 2588 | /// within the identifier table. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2589 | void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) { |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2590 | IdentID ID = IdentifierIDs[II]; |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2591 | // Only store offsets new to this AST file. Other identifier names are looked |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2592 | // up earlier in the chain and thus don't need an offset. |
| 2593 | if (ID >= FirstIdentID) |
| 2594 | IdentifierOffsets[ID - FirstIdentID] = Offset; |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2597 | /// \brief Note that the selector Sel occurs at the given offset |
| 2598 | /// within the method pool/selector table. |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2599 | void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2600 | unsigned ID = SelectorIDs[Sel]; |
| 2601 | assert(ID && "Unknown selector"); |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2602 | // Don't record offsets for selectors that are also available in a different |
| 2603 | // file. |
| 2604 | if (ID < FirstSelectorID) |
| 2605 | return; |
| 2606 | SelectorOffsets[ID - FirstSelectorID] = Offset; |
Douglas Gregor | 95c13f5 | 2009-04-25 17:48:32 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2609 | ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream) |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 2610 | : Stream(Stream), Chain(0), SerializationListener(0), |
| 2611 | FirstDeclID(1), NextDeclID(FirstDeclID), |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2612 | FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID), |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2613 | FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1), |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 2614 | NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID), |
| 2615 | CollectedStmts(&StmtsToEmit), |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 2616 | NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 2617 | NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1), |
| 2618 | NextCXXBaseSpecifiersID(1) |
| 2619 | { |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2620 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2621 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2622 | void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls, |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 2623 | const std::string &OutputFile, |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2624 | const char *isysroot) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2625 | // Emit the file header. |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2626 | Stream.Emit((unsigned)'C', 8); |
| 2627 | Stream.Emit((unsigned)'P', 8); |
| 2628 | Stream.Emit((unsigned)'C', 8); |
| 2629 | Stream.Emit((unsigned)'H', 8); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2630 | |
Chris Lattner | 28fa4e6 | 2009-04-26 22:26:21 +0000 | [diff] [blame] | 2631 | WriteBlockInfoBlock(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2632 | |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2633 | if (Chain) |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2634 | WriteASTChain(SemaRef, StatCalls, isysroot); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2635 | else |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 2636 | WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2639 | void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 2640 | const char *isysroot, |
| 2641 | const std::string &OutputFile) { |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2642 | using namespace llvm; |
| 2643 | |
| 2644 | ASTContext &Context = SemaRef.Context; |
| 2645 | Preprocessor &PP = SemaRef.PP; |
| 2646 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2647 | // The translation unit is the first declaration we'll emit. |
| 2648 | DeclIDs[Context.getTranslationUnitDecl()] = 1; |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 2649 | ++NextDeclID; |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2650 | DeclTypesToEmit.push(Context.getTranslationUnitDecl()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 2652 | // Make sure that we emit IdentifierInfos (and any attached |
| 2653 | // declarations) for builtins. |
| 2654 | { |
| 2655 | IdentifierTable &Table = PP.getIdentifierTable(); |
| 2656 | llvm::SmallVector<const char *, 32> BuiltinNames; |
| 2657 | Context.BuiltinInfo.GetBuiltinNames(BuiltinNames, |
| 2658 | Context.getLangOptions().NoBuiltin); |
| 2659 | for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I) |
| 2660 | getIdentifierRef(&Table.get(BuiltinNames[I])); |
| 2661 | } |
| 2662 | |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 2663 | // Build a record containing all of the tentative definitions in this file, in |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2664 | // TentativeDefinitions order. Generally, this record will be empty for |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 2665 | // headers. |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2666 | RecordData TentativeDefinitions; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2667 | for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { |
| 2668 | AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 2669 | } |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2670 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 2671 | // Build a record containing all of the file scoped decls in this file. |
| 2672 | RecordData UnusedFileScopedDecls; |
| 2673 | for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) |
| 2674 | AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 2675 | |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 2676 | RecordData WeakUndeclaredIdentifiers; |
| 2677 | if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { |
| 2678 | WeakUndeclaredIdentifiers.push_back( |
| 2679 | SemaRef.WeakUndeclaredIdentifiers.size()); |
| 2680 | for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator |
| 2681 | I = SemaRef.WeakUndeclaredIdentifiers.begin(), |
| 2682 | E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 2683 | AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); |
| 2684 | AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); |
| 2685 | AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); |
| 2686 | WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); |
| 2687 | } |
| 2688 | } |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 2689 | |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2690 | // Build a record containing all of the locally-scoped external |
| 2691 | // declarations in this header file. Generally, this record will be |
| 2692 | // empty. |
| 2693 | RecordData LocallyScopedExternalDecls; |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2694 | // FIXME: This is filling in the AST file in densemap order which is |
Chris Lattner | 0c79736 | 2009-09-08 18:19:27 +0000 | [diff] [blame] | 2695 | // nondeterminstic! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2697 | TD = SemaRef.LocallyScopedExternalDecls.begin(), |
| 2698 | TDEnd = SemaRef.LocallyScopedExternalDecls.end(); |
| 2699 | TD != TDEnd; ++TD) |
| 2700 | AddDeclRef(TD->second, LocallyScopedExternalDecls); |
| 2701 | |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 2702 | // Build a record containing all of the ext_vector declarations. |
| 2703 | RecordData ExtVectorDecls; |
| 2704 | for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) |
| 2705 | AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); |
| 2706 | |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 2707 | // Build a record containing all of the VTable uses information. |
| 2708 | RecordData VTableUses; |
Argyrios Kyrtzidis | edee67f | 2010-08-03 17:29:52 +0000 | [diff] [blame] | 2709 | if (!SemaRef.VTableUses.empty()) { |
| 2710 | VTableUses.push_back(SemaRef.VTableUses.size()); |
| 2711 | for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { |
| 2712 | AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); |
| 2713 | AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); |
| 2714 | VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); |
| 2715 | } |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | // Build a record containing all of dynamic classes declarations. |
| 2719 | RecordData DynamicClasses; |
| 2720 | for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) |
| 2721 | AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); |
| 2722 | |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 2723 | // Build a record containing all of pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2724 | RecordData PendingInstantiations; |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 2725 | for (std::deque<Sema::PendingImplicitInstantiation>::iterator |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2726 | I = SemaRef.PendingInstantiations.begin(), |
| 2727 | N = SemaRef.PendingInstantiations.end(); I != N; ++I) { |
| 2728 | AddDeclRef(I->first, PendingInstantiations); |
| 2729 | AddSourceLocation(I->second, PendingInstantiations); |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 2730 | } |
| 2731 | assert(SemaRef.PendingLocalImplicitInstantiations.empty() && |
| 2732 | "There are local ones at end of translation unit!"); |
| 2733 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2734 | // Build a record containing some declaration references. |
| 2735 | RecordData SemaDeclRefs; |
| 2736 | if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { |
| 2737 | AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); |
| 2738 | AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); |
| 2739 | } |
| 2740 | |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 2741 | RecordData CUDASpecialDeclRefs; |
| 2742 | if (Context.getcudaConfigureCallDecl()) { |
| 2743 | AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs); |
| 2744 | } |
| 2745 | |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2746 | // Write the remaining AST contents. |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 2747 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2748 | Stream.EnterSubblock(AST_BLOCK_ID, 5); |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 2749 | WriteMetadata(Context, isysroot, OutputFile); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2750 | WriteLanguageOptions(Context.getLangOptions()); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 2751 | if (StatCalls && !isysroot) |
Douglas Gregor | 11cfd94 | 2010-07-12 23:48:14 +0000 | [diff] [blame] | 2752 | WriteStatCache(*StatCalls); |
Douglas Gregor | 0086a5a | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 2753 | WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2754 | // Write the record of special types. |
| 2755 | Record.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2757 | AddTypeRef(Context.getBuiltinVaListType(), Record); |
| 2758 | AddTypeRef(Context.getObjCIdType(), Record); |
| 2759 | AddTypeRef(Context.getObjCSelType(), Record); |
| 2760 | AddTypeRef(Context.getObjCProtoType(), Record); |
| 2761 | AddTypeRef(Context.getObjCClassType(), Record); |
| 2762 | AddTypeRef(Context.getRawCFConstantStringType(), Record); |
| 2763 | AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record); |
| 2764 | AddTypeRef(Context.getFILEType(), Record); |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 2765 | AddTypeRef(Context.getjmp_bufType(), Record); |
| 2766 | AddTypeRef(Context.getsigjmp_bufType(), Record); |
Douglas Gregor | a8eed7d | 2009-08-21 00:27:50 +0000 | [diff] [blame] | 2767 | AddTypeRef(Context.ObjCIdRedefinitionType, Record); |
| 2768 | AddTypeRef(Context.ObjCClassRedefinitionType, Record); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2769 | AddTypeRef(Context.getRawBlockdescriptorType(), Record); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2770 | AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record); |
Fariborz Jahanian | e804c28 | 2010-04-23 17:41:07 +0000 | [diff] [blame] | 2771 | AddTypeRef(Context.ObjCSelRedefinitionType, Record); |
| 2772 | AddTypeRef(Context.getRawNSConstantStringType(), Record); |
Argyrios Kyrtzidis | e862cbc | 2010-07-04 21:44:19 +0000 | [diff] [blame] | 2773 | Record.push_back(Context.isInt128Installed()); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2774 | Stream.EmitRecord(SPECIAL_TYPES, Record); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | 1970d88 | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 2776 | // Keep writing types and declarations until all types and |
| 2777 | // declarations have been written. |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2778 | Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 2779 | WriteDeclsBlockAbbrevs(); |
| 2780 | while (!DeclTypesToEmit.empty()) { |
| 2781 | DeclOrType DOT = DeclTypesToEmit.front(); |
| 2782 | DeclTypesToEmit.pop(); |
| 2783 | if (DOT.isType()) |
| 2784 | WriteType(DOT.getType()); |
| 2785 | else |
| 2786 | WriteDecl(Context, DOT.getDecl()); |
| 2787 | } |
| 2788 | Stream.ExitBlock(); |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | 4505315 | 2009-10-17 17:25:45 +0000 | [diff] [blame] | 2790 | WritePreprocessor(PP); |
Douglas Gregor | 09b6989 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 2791 | WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot); |
Sebastian Redl | a19a67f | 2010-08-03 21:58:15 +0000 | [diff] [blame] | 2792 | WriteSelectors(SemaRef); |
Fariborz Jahanian | c51609a | 2010-07-23 19:11:11 +0000 | [diff] [blame] | 2793 | WriteReferencedSelectorsPool(SemaRef); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2794 | WriteIdentifierTable(PP); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 2795 | WriteFPPragmaOptions(SemaRef.getFPOptions()); |
| 2796 | WriteOpenCLExtensions(SemaRef); |
Douglas Gregor | 745ed14 | 2009-04-25 18:35:21 +0000 | [diff] [blame] | 2797 | |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2798 | WriteTypeDeclOffsets(); |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 2799 | WritePragmaDiagnosticMappings(Context.getDiagnostics()); |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 2801 | // Write the C++ base-specifier set offsets. |
| 2802 | if (!CXXBaseSpecifiersOffsets.empty()) { |
| 2803 | // Create a blob abbreviation for the C++ base specifiers offsets. |
| 2804 | using namespace llvm; |
| 2805 | |
| 2806 | BitCodeAbbrev *Abbrev = new BitCodeAbbrev(); |
| 2807 | Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS)); |
| 2808 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size |
| 2809 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
| 2810 | unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev); |
| 2811 | |
| 2812 | // Write the selector offsets table. |
| 2813 | Record.clear(); |
| 2814 | Record.push_back(CXX_BASE_SPECIFIER_OFFSETS); |
| 2815 | Record.push_back(CXXBaseSpecifiersOffsets.size()); |
| 2816 | Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record, |
| 2817 | (const char *)CXXBaseSpecifiersOffsets.data(), |
| 2818 | CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t)); |
| 2819 | } |
| 2820 | |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2821 | // Write the record containing external, unnamed definitions. |
Douglas Gregor | 1a0d0b9 | 2009-04-14 00:24:19 +0000 | [diff] [blame] | 2822 | if (!ExternalDefinitions.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2823 | Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Douglas Gregor | d4df865 | 2009-04-22 22:02:47 +0000 | [diff] [blame] | 2824 | |
| 2825 | // Write the record containing tentative definitions. |
| 2826 | if (!TentativeDefinitions.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2827 | Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2828 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 2829 | // Write the record containing unused file scoped decls. |
| 2830 | if (!UnusedFileScopedDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2831 | Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 2832 | |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 2833 | // Write the record containing weak undeclared identifiers. |
| 2834 | if (!WeakUndeclaredIdentifiers.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2835 | Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, |
Argyrios Kyrtzidis | ee1afa3 | 2010-08-05 09:48:08 +0000 | [diff] [blame] | 2836 | WeakUndeclaredIdentifiers); |
| 2837 | |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2838 | // Write the record containing locally-scoped external definitions. |
| 2839 | if (!LocallyScopedExternalDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2840 | Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, |
Douglas Gregor | acfc76c | 2009-04-22 22:18:58 +0000 | [diff] [blame] | 2841 | LocallyScopedExternalDecls); |
Douglas Gregor | 61cac2b | 2009-04-27 20:06:05 +0000 | [diff] [blame] | 2842 | |
| 2843 | // Write the record containing ext_vector type names. |
| 2844 | if (!ExtVectorDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2845 | Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 2847 | // Write the record containing VTable uses information. |
| 2848 | if (!VTableUses.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2849 | Stream.EmitRecord(VTABLE_USES, VTableUses); |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Write the record containing dynamic classes declarations. |
| 2852 | if (!DynamicClasses.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2853 | Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); |
Argyrios Kyrtzidis | af2eac2 | 2010-07-06 15:37:04 +0000 | [diff] [blame] | 2854 | |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 2855 | // Write the record containing pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2856 | if (!PendingInstantiations.empty()) |
| 2857 | Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); |
Argyrios Kyrtzidis | 7f76d11 | 2010-08-05 09:48:16 +0000 | [diff] [blame] | 2858 | |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2859 | // Write the record containing declaration references of Sema. |
| 2860 | if (!SemaDeclRefs.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2861 | Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); |
Argyrios Kyrtzidis | 2d68810 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 2862 | |
Peter Collingbourne | 9e2c81f | 2011-02-09 21:04:32 +0000 | [diff] [blame] | 2863 | // Write the record containing CUDA-specific declaration references. |
| 2864 | if (!CUDASpecialDeclRefs.empty()) |
| 2865 | Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs); |
| 2866 | |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 2867 | // Some simple statistics |
Douglas Gregor | 652d82a | 2009-04-18 05:55:16 +0000 | [diff] [blame] | 2868 | Record.clear(); |
Douglas Gregor | 08f0129 | 2009-04-17 22:13:46 +0000 | [diff] [blame] | 2869 | Record.push_back(NumStatements); |
Douglas Gregor | c3366a5 | 2009-04-21 23:56:24 +0000 | [diff] [blame] | 2870 | Record.push_back(NumMacros); |
Douglas Gregor | a57c3ab | 2009-04-22 22:34:57 +0000 | [diff] [blame] | 2871 | Record.push_back(NumLexicalDeclContexts); |
| 2872 | Record.push_back(NumVisibleDeclContexts); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2873 | Stream.EmitRecord(STATISTICS, Record); |
Douglas Gregor | 8f45df5 | 2009-04-16 22:23:12 +0000 | [diff] [blame] | 2874 | Stream.ExitBlock(); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2877 | void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls, |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 2878 | const char *isysroot) { |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2879 | using namespace llvm; |
| 2880 | |
| 2881 | ASTContext &Context = SemaRef.Context; |
| 2882 | Preprocessor &PP = SemaRef.PP; |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2883 | |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2884 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2885 | Stream.EnterSubblock(AST_BLOCK_ID, 5); |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 2886 | WriteMetadata(Context, isysroot, ""); |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 2887 | if (StatCalls && !isysroot) |
| 2888 | WriteStatCache(*StatCalls); |
| 2889 | // FIXME: Source manager block should only write new stuff, which could be |
| 2890 | // done by tracking the largest ID in the chain |
| 2891 | WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2892 | |
| 2893 | // The special types are in the chained PCH. |
| 2894 | |
| 2895 | // We don't start with the translation unit, but with its decls that |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 2896 | // don't come from the chained PCH. |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2897 | const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2898 | llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls; |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2899 | for (DeclContext::decl_iterator I = TU->noload_decls_begin(), |
| 2900 | E = TU->noload_decls_end(); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2901 | I != E; ++I) { |
Sebastian Redl | 4b1f490 | 2010-07-27 18:24:41 +0000 | [diff] [blame] | 2902 | if ((*I)->getPCHLevel() == 0) |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2903 | NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I))); |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 2904 | else if ((*I)->isChangedSinceDeserialization()) |
| 2905 | (void)GetDeclRef(*I); // Make sure it's written, but don't record it. |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2906 | } |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 2907 | // We also need to write a lexical updates block for the TU. |
Sebastian Redl | 4b1f490 | 2010-07-27 18:24:41 +0000 | [diff] [blame] | 2908 | llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2909 | Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL)); |
Sebastian Redl | 4b1f490 | 2010-07-27 18:24:41 +0000 | [diff] [blame] | 2910 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 2911 | unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv); |
| 2912 | Record.clear(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 2913 | Record.push_back(TU_UPDATE_LEXICAL); |
Sebastian Redl | 4b1f490 | 2010-07-27 18:24:41 +0000 | [diff] [blame] | 2914 | Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record, |
| 2915 | reinterpret_cast<const char*>(NewGlobalDecls.data()), |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2916 | NewGlobalDecls.size() * sizeof(KindDeclIDPair)); |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 2917 | // And a visible updates block for the DeclContexts. |
| 2918 | Abv = new llvm::BitCodeAbbrev(); |
| 2919 | Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); |
| 2920 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); |
| 2921 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32)); |
| 2922 | Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); |
| 2923 | UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv); |
| 2924 | WriteDeclContextVisibleUpdate(TU); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 2925 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 2926 | // Build a record containing all of the new tentative definitions in this |
| 2927 | // file, in TentativeDefinitions order. |
| 2928 | RecordData TentativeDefinitions; |
| 2929 | for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) { |
| 2930 | if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0) |
| 2931 | AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions); |
| 2932 | } |
| 2933 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 2934 | // Build a record containing all of the file scoped decls in this file. |
| 2935 | RecordData UnusedFileScopedDecls; |
| 2936 | for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) { |
| 2937 | if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0) |
| 2938 | AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 2941 | // We write the entire table, overwriting the tables from the chain. |
| 2942 | RecordData WeakUndeclaredIdentifiers; |
| 2943 | if (!SemaRef.WeakUndeclaredIdentifiers.empty()) { |
| 2944 | WeakUndeclaredIdentifiers.push_back( |
| 2945 | SemaRef.WeakUndeclaredIdentifiers.size()); |
| 2946 | for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator |
| 2947 | I = SemaRef.WeakUndeclaredIdentifiers.begin(), |
| 2948 | E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) { |
| 2949 | AddIdentifierRef(I->first, WeakUndeclaredIdentifiers); |
| 2950 | AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers); |
| 2951 | AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers); |
| 2952 | WeakUndeclaredIdentifiers.push_back(I->second.getUsed()); |
| 2953 | } |
| 2954 | } |
| 2955 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 2956 | // Build a record containing all of the locally-scoped external |
| 2957 | // declarations in this header file. Generally, this record will be |
| 2958 | // empty. |
| 2959 | RecordData LocallyScopedExternalDecls; |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 2960 | // FIXME: This is filling in the AST file in densemap order which is |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 2961 | // nondeterminstic! |
| 2962 | for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator |
| 2963 | TD = SemaRef.LocallyScopedExternalDecls.begin(), |
| 2964 | TDEnd = SemaRef.LocallyScopedExternalDecls.end(); |
| 2965 | TD != TDEnd; ++TD) { |
| 2966 | if (TD->second->getPCHLevel() == 0) |
| 2967 | AddDeclRef(TD->second, LocallyScopedExternalDecls); |
| 2968 | } |
| 2969 | |
| 2970 | // Build a record containing all of the ext_vector declarations. |
| 2971 | RecordData ExtVectorDecls; |
| 2972 | for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) { |
| 2973 | if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0) |
| 2974 | AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); |
| 2975 | } |
| 2976 | |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 2977 | // Build a record containing all of the VTable uses information. |
| 2978 | // We write everything here, because it's too hard to determine whether |
| 2979 | // a use is new to this part. |
| 2980 | RecordData VTableUses; |
| 2981 | if (!SemaRef.VTableUses.empty()) { |
| 2982 | VTableUses.push_back(SemaRef.VTableUses.size()); |
| 2983 | for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { |
| 2984 | AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); |
| 2985 | AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); |
| 2986 | VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | // Build a record containing all of dynamic classes declarations. |
| 2991 | RecordData DynamicClasses; |
| 2992 | for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) |
| 2993 | if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0) |
| 2994 | AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); |
| 2995 | |
| 2996 | // Build a record containing all of pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2997 | RecordData PendingInstantiations; |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 2998 | for (std::deque<Sema::PendingImplicitInstantiation>::iterator |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2999 | I = SemaRef.PendingInstantiations.begin(), |
| 3000 | N = SemaRef.PendingInstantiations.end(); I != N; ++I) { |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3001 | if (I->first->getPCHLevel() == 0) { |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3002 | AddDeclRef(I->first, PendingInstantiations); |
| 3003 | AddSourceLocation(I->second, PendingInstantiations); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | assert(SemaRef.PendingLocalImplicitInstantiations.empty() && |
| 3007 | "There are local ones at end of translation unit!"); |
| 3008 | |
| 3009 | // Build a record containing some declaration references. |
| 3010 | // It's not worth the effort to avoid duplication here. |
| 3011 | RecordData SemaDeclRefs; |
| 3012 | if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) { |
| 3013 | AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs); |
| 3014 | AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs); |
| 3015 | } |
| 3016 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3017 | Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3018 | WriteDeclsBlockAbbrevs(); |
Argyrios Kyrtzidis | 4729972 | 2010-10-28 07:38:45 +0000 | [diff] [blame] | 3019 | for (DeclsToRewriteTy::iterator |
| 3020 | I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I) |
| 3021 | DeclTypesToEmit.push(const_cast<Decl*>(*I)); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3022 | while (!DeclTypesToEmit.empty()) { |
| 3023 | DeclOrType DOT = DeclTypesToEmit.front(); |
| 3024 | DeclTypesToEmit.pop(); |
| 3025 | if (DOT.isType()) |
| 3026 | WriteType(DOT.getType()); |
| 3027 | else |
| 3028 | WriteDecl(Context, DOT.getDecl()); |
| 3029 | } |
| 3030 | Stream.ExitBlock(); |
| 3031 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3032 | WritePreprocessor(PP); |
Sebastian Redl | 51c79d8 | 2010-08-04 22:21:29 +0000 | [diff] [blame] | 3033 | WriteSelectors(SemaRef); |
| 3034 | WriteReferencedSelectorsPool(SemaRef); |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3035 | WriteIdentifierTable(PP); |
Peter Collingbourne | 5df20e0 | 2011-02-15 19:46:30 +0000 | [diff] [blame] | 3036 | WriteFPPragmaOptions(SemaRef.getFPOptions()); |
| 3037 | WriteOpenCLExtensions(SemaRef); |
| 3038 | |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 3039 | WriteTypeDeclOffsets(); |
Argyrios Kyrtzidis | 452707c | 2010-11-05 22:10:18 +0000 | [diff] [blame] | 3040 | // FIXME: For chained PCH only write the new mappings (we currently |
| 3041 | // write all of them again). |
Argyrios Kyrtzidis | 243aedb | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 3042 | WritePragmaDiagnosticMappings(Context.getDiagnostics()); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3043 | |
Argyrios Kyrtzidis | 839bbac | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 3044 | /// Build a record containing first declarations from a chained PCH and the |
Sebastian Redl | 42a0f6a | 2010-08-18 23:56:27 +0000 | [diff] [blame] | 3045 | /// most recent declarations in this AST that they point to. |
Argyrios Kyrtzidis | 839bbac | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 3046 | RecordData FirstLatestDeclIDs; |
| 3047 | for (FirstLatestDeclMap::iterator |
| 3048 | I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) { |
| 3049 | assert(I->first->getPCHLevel() > I->second->getPCHLevel() && |
| 3050 | "Expected first & second to be in different PCHs"); |
| 3051 | AddDeclRef(I->first, FirstLatestDeclIDs); |
| 3052 | AddDeclRef(I->second, FirstLatestDeclIDs); |
| 3053 | } |
| 3054 | if (!FirstLatestDeclIDs.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3055 | Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs); |
Argyrios Kyrtzidis | 839bbac | 2010-08-03 17:30:10 +0000 | [diff] [blame] | 3056 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3057 | // Write the record containing external, unnamed definitions. |
| 3058 | if (!ExternalDefinitions.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3059 | Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3060 | |
| 3061 | // Write the record containing tentative definitions. |
| 3062 | if (!TentativeDefinitions.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3063 | Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3064 | |
Argyrios Kyrtzidis | 35672e7 | 2010-08-13 18:42:17 +0000 | [diff] [blame] | 3065 | // Write the record containing unused file scoped decls. |
| 3066 | if (!UnusedFileScopedDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3067 | Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3068 | |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3069 | // Write the record containing weak undeclared identifiers. |
| 3070 | if (!WeakUndeclaredIdentifiers.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3071 | Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS, |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3072 | WeakUndeclaredIdentifiers); |
| 3073 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3074 | // Write the record containing locally-scoped external definitions. |
| 3075 | if (!LocallyScopedExternalDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3076 | Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS, |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3077 | LocallyScopedExternalDecls); |
| 3078 | |
| 3079 | // Write the record containing ext_vector type names. |
| 3080 | if (!ExtVectorDecls.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3081 | Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3082 | |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3083 | // Write the record containing VTable uses information. |
| 3084 | if (!VTableUses.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3085 | Stream.EmitRecord(VTABLE_USES, VTableUses); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3086 | |
| 3087 | // Write the record containing dynamic classes declarations. |
| 3088 | if (!DynamicClasses.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3089 | Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3090 | |
| 3091 | // Write the record containing pending implicit instantiations. |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3092 | if (!PendingInstantiations.empty()) |
| 3093 | Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations); |
Sebastian Redl | 08aca9025 | 2010-08-05 18:21:25 +0000 | [diff] [blame] | 3094 | |
| 3095 | // Write the record containing declaration references of Sema. |
| 3096 | if (!SemaDeclRefs.empty()) |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3097 | Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs); |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3098 | |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 3099 | // Write the updates to DeclContexts. |
| 3100 | for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator |
| 3101 | I = UpdatedDeclContexts.begin(), |
| 3102 | E = UpdatedDeclContexts.end(); |
Sebastian Redl | a4071b4 | 2010-08-24 00:50:09 +0000 | [diff] [blame] | 3103 | I != E; ++I) |
| 3104 | WriteDeclContextVisibleUpdate(*I); |
| 3105 | |
Argyrios Kyrtzidis | 97bfda9 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3106 | WriteDeclUpdatesBlocks(); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3107 | |
Sebastian Redl | 9891212 | 2010-07-27 23:01:28 +0000 | [diff] [blame] | 3108 | Record.clear(); |
| 3109 | Record.push_back(NumStatements); |
| 3110 | Record.push_back(NumMacros); |
| 3111 | Record.push_back(NumLexicalDeclContexts); |
| 3112 | Record.push_back(NumVisibleDeclContexts); |
Argyrios Kyrtzidis | 97bfda9 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3113 | WriteDeclReplacementsBlock(); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3114 | Stream.EmitRecord(STATISTICS, Record); |
Sebastian Redl | 143413f | 2010-07-12 22:02:52 +0000 | [diff] [blame] | 3115 | Stream.ExitBlock(); |
| 3116 | } |
| 3117 | |
Argyrios Kyrtzidis | 97bfda9 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3118 | void ASTWriter::WriteDeclUpdatesBlocks() { |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3119 | if (DeclUpdates.empty()) |
| 3120 | return; |
| 3121 | |
| 3122 | RecordData OffsetsRecord; |
| 3123 | Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3); |
| 3124 | for (DeclUpdateMap::iterator |
| 3125 | I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) { |
| 3126 | const Decl *D = I->first; |
| 3127 | UpdateRecord &URec = I->second; |
| 3128 | |
Argyrios Kyrtzidis | 3ba70b8 | 2010-10-24 17:26:46 +0000 | [diff] [blame] | 3129 | if (DeclsToRewrite.count(D)) |
| 3130 | continue; // The decl will be written completely,no need to store updates. |
| 3131 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3132 | uint64_t Offset = Stream.GetCurrentBitNo(); |
| 3133 | Stream.EmitRecord(DECL_UPDATES, URec); |
| 3134 | |
| 3135 | OffsetsRecord.push_back(GetDeclRef(D)); |
| 3136 | OffsetsRecord.push_back(Offset); |
| 3137 | } |
| 3138 | Stream.ExitBlock(); |
| 3139 | Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord); |
| 3140 | } |
| 3141 | |
Argyrios Kyrtzidis | 97bfda9 | 2010-10-24 17:26:43 +0000 | [diff] [blame] | 3142 | void ASTWriter::WriteDeclReplacementsBlock() { |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3143 | if (ReplacedDecls.empty()) |
| 3144 | return; |
| 3145 | |
| 3146 | RecordData Record; |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3147 | for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3148 | I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) { |
| 3149 | Record.push_back(I->first); |
| 3150 | Record.push_back(I->second); |
| 3151 | } |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3152 | Stream.EmitRecord(DECL_REPLACEMENTS, Record); |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3155 | void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3156 | Record.push_back(Loc.getRawEncoding()); |
| 3157 | } |
| 3158 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3159 | void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3160 | AddSourceLocation(Range.getBegin(), Record); |
| 3161 | AddSourceLocation(Range.getEnd(), Record); |
| 3162 | } |
| 3163 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3164 | void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3165 | Record.push_back(Value.getBitWidth()); |
Benjamin Kramer | 25f9ea6 | 2010-09-06 23:43:28 +0000 | [diff] [blame] | 3166 | const uint64_t *Words = Value.getRawData(); |
| 3167 | Record.append(Words, Words + Value.getNumWords()); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3170 | void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) { |
Douglas Gregor | 1daeb69 | 2009-04-13 18:14:40 +0000 | [diff] [blame] | 3171 | Record.push_back(Value.isUnsigned()); |
| 3172 | AddAPInt(Value, Record); |
| 3173 | } |
| 3174 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3175 | void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) { |
Douglas Gregor | e0a3a51 | 2009-04-14 21:55:33 +0000 | [diff] [blame] | 3176 | AddAPInt(Value.bitcastToAPInt(), Record); |
| 3177 | } |
| 3178 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3179 | void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) { |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3180 | Record.push_back(getIdentifierRef(II)); |
| 3181 | } |
| 3182 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3183 | IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) { |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3184 | if (II == 0) |
| 3185 | return 0; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3186 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3187 | IdentID &ID = IdentifierIDs[II]; |
Douglas Gregor | 3ed42cb | 2009-04-11 00:14:32 +0000 | [diff] [blame] | 3188 | if (ID == 0) |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3189 | ID = NextIdentID++; |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 3190 | return ID; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Sebastian Redl | 50e2658 | 2010-09-15 19:54:06 +0000 | [diff] [blame] | 3193 | MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) { |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 3194 | if (MD == 0) |
| 3195 | return 0; |
Sebastian Redl | 50e2658 | 2010-09-15 19:54:06 +0000 | [diff] [blame] | 3196 | |
| 3197 | MacroID &ID = MacroDefinitions[MD]; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 3198 | if (ID == 0) |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 3199 | ID = NextMacroID++; |
Douglas Gregor | aae9224 | 2010-03-19 21:51:54 +0000 | [diff] [blame] | 3200 | return ID; |
| 3201 | } |
| 3202 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3203 | void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3204 | Record.push_back(getSelectorRef(SelRef)); |
| 3205 | } |
| 3206 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3207 | SelectorID ASTWriter::getSelectorRef(Selector Sel) { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3208 | if (Sel.getAsOpaquePtr() == 0) { |
| 3209 | return 0; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3212 | SelectorID &SID = SelectorIDs[Sel]; |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3213 | if (SID == 0 && Chain) { |
| 3214 | // This might trigger a ReadSelector callback, which will set the ID for |
| 3215 | // this selector. |
| 3216 | Chain->LoadSelector(Sel); |
| 3217 | } |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3218 | if (SID == 0) { |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3219 | SID = NextSelectorID++; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3220 | } |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3221 | return SID; |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3224 | void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) { |
Chris Lattner | cba8614 | 2010-05-10 00:25:06 +0000 | [diff] [blame] | 3225 | AddDeclRef(Temp->getDestructor(), Record); |
| 3226 | } |
| 3227 | |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3228 | void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, |
| 3229 | CXXBaseSpecifier const *BasesEnd, |
| 3230 | RecordDataImpl &Record) { |
| 3231 | assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded"); |
| 3232 | CXXBaseSpecifiersToWrite.push_back( |
| 3233 | QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID, |
| 3234 | Bases, BasesEnd)); |
| 3235 | Record.push_back(NextCXXBaseSpecifiersID++); |
| 3236 | } |
| 3237 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3238 | void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3239 | const TemplateArgumentLocInfo &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3240 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3241 | switch (Kind) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3242 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3243 | AddStmt(Arg.getAsExpr()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3244 | break; |
| 3245 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3246 | AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3247 | break; |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3248 | case TemplateArgument::Template: |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3249 | AddSourceRange(Arg.getTemplateQualifierRange(), Record); |
| 3250 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3251 | break; |
| 3252 | case TemplateArgument::TemplateExpansion: |
| 3253 | AddSourceRange(Arg.getTemplateQualifierRange(), Record); |
| 3254 | AddSourceLocation(Arg.getTemplateNameLoc(), Record); |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 3255 | AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3256 | break; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3257 | case TemplateArgument::Null: |
| 3258 | case TemplateArgument::Integral: |
| 3259 | case TemplateArgument::Declaration: |
| 3260 | case TemplateArgument::Pack: |
| 3261 | break; |
| 3262 | } |
| 3263 | } |
| 3264 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3265 | void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3266 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3267 | AddTemplateArgument(Arg.getArgument(), Record); |
Argyrios Kyrtzidis | ddf5f21 | 2010-06-28 09:31:42 +0000 | [diff] [blame] | 3268 | |
| 3269 | if (Arg.getArgument().getKind() == TemplateArgument::Expression) { |
| 3270 | bool InfoHasSameExpr |
| 3271 | = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); |
| 3272 | Record.push_back(InfoHasSameExpr); |
| 3273 | if (InfoHasSameExpr) |
| 3274 | return; // Avoid storing the same expr twice. |
| 3275 | } |
Argyrios Kyrtzidis | ae85e24 | 2010-06-22 09:54:59 +0000 | [diff] [blame] | 3276 | AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), |
| 3277 | Record); |
| 3278 | } |
| 3279 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3280 | void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, |
| 3281 | RecordDataImpl &Record) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3282 | if (TInfo == 0) { |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3283 | AddTypeRef(QualType(), Record); |
| 3284 | return; |
| 3285 | } |
| 3286 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3287 | AddTypeLoc(TInfo->getTypeLoc(), Record); |
| 3288 | } |
| 3289 | |
| 3290 | void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) { |
| 3291 | AddTypeRef(TL.getType(), Record); |
| 3292 | |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3293 | TypeLocWriter TLW(*this, Record); |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3294 | for (; !TL.isNull(); TL = TL.getNextTypeLoc()) |
Kovarththanan Rajaratnam | a9c81a8 | 2010-03-14 07:06:50 +0000 | [diff] [blame] | 3295 | TLW.Visit(TL); |
John McCall | 8f115c6 | 2009-10-16 21:56:05 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3298 | void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 9ab44ea | 2010-08-20 16:04:14 +0000 | [diff] [blame] | 3299 | Record.push_back(GetOrCreateTypeID(T)); |
| 3300 | } |
| 3301 | |
| 3302 | TypeID ASTWriter::GetOrCreateTypeID(QualType T) { |
Argyrios Kyrtzidis | 082e461 | 2010-08-20 16:04:20 +0000 | [diff] [blame] | 3303 | return MakeTypeID(T, |
| 3304 | std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this)); |
| 3305 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3306 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3307 | TypeID ASTWriter::getTypeID(QualType T) const { |
Argyrios Kyrtzidis | 082e461 | 2010-08-20 16:04:20 +0000 | [diff] [blame] | 3308 | return MakeTypeID(T, |
| 3309 | std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this)); |
Argyrios Kyrtzidis | e394f2c | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) { |
| 3313 | if (T.isNull()) |
| 3314 | return TypeIdx(); |
| 3315 | assert(!T.getLocalFastQualifiers()); |
| 3316 | |
Argyrios Kyrtzidis | a7fbbb0 | 2010-08-20 16:04:04 +0000 | [diff] [blame] | 3317 | TypeIdx &Idx = TypeIdxs[T]; |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 3318 | if (Idx.getIndex() == 0) { |
Douglas Gregor | 1970d88 | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 3319 | // We haven't seen this type before. Assign it a new ID and put it |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3320 | // into the queue of types to emit. |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 3321 | Idx = TypeIdx(NextTypeID++); |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3322 | DeclTypesToEmit.push(T); |
Douglas Gregor | 1970d88 | 2009-04-26 03:49:13 +0000 | [diff] [blame] | 3323 | } |
Argyrios Kyrtzidis | e394f2c | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3324 | return Idx; |
| 3325 | } |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3326 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3327 | TypeIdx ASTWriter::getTypeIdx(QualType T) const { |
Argyrios Kyrtzidis | e394f2c | 2010-08-20 16:04:09 +0000 | [diff] [blame] | 3328 | if (T.isNull()) |
| 3329 | return TypeIdx(); |
| 3330 | assert(!T.getLocalFastQualifiers()); |
| 3331 | |
Argyrios Kyrtzidis | 0734732 | 2010-08-20 16:04:27 +0000 | [diff] [blame] | 3332 | TypeIdxMap::const_iterator I = TypeIdxs.find(T); |
| 3333 | assert(I != TypeIdxs.end() && "Type not emitted!"); |
| 3334 | return I->second; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3337 | void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3338 | Record.push_back(GetDeclRef(D)); |
| 3339 | } |
| 3340 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3341 | DeclID ASTWriter::GetDeclRef(const Decl *D) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3342 | if (D == 0) { |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3343 | return 0; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3344 | } |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 3345 | assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer"); |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3346 | DeclID &ID = DeclIDs[D]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | if (ID == 0) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3348 | // We haven't seen this declaration before. Give it a new ID and |
| 3349 | // enqueue it in the list of declarations to emit. |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3350 | ID = NextDeclID++; |
Douglas Gregor | 12bfa38 | 2009-10-17 00:13:19 +0000 | [diff] [blame] | 3351 | DeclTypesToEmit.push(const_cast<Decl *>(D)); |
Sebastian Redl | e7c1fe6 | 2010-08-13 00:28:03 +0000 | [diff] [blame] | 3352 | } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) { |
| 3353 | // We don't add it to the replacement collection here, because we don't |
| 3354 | // have the offset yet. |
| 3355 | DeclTypesToEmit.push(const_cast<Decl *>(D)); |
| 3356 | // Reset the flag, so that we don't add this decl multiple times. |
| 3357 | const_cast<Decl *>(D)->setChangedSinceDeserialization(false); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Sebastian Redl | 66c5eef | 2010-07-27 00:17:23 +0000 | [diff] [blame] | 3360 | return ID; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3363 | DeclID ASTWriter::getDeclID(const Decl *D) { |
Douglas Gregor | e84a9da | 2009-04-20 20:36:09 +0000 | [diff] [blame] | 3364 | if (D == 0) |
| 3365 | return 0; |
| 3366 | |
| 3367 | assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!"); |
| 3368 | return DeclIDs[D]; |
| 3369 | } |
| 3370 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3371 | void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) { |
Chris Lattner | 258172e | 2009-04-27 07:35:58 +0000 | [diff] [blame] | 3372 | // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc. |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3373 | Record.push_back(Name.getNameKind()); |
| 3374 | switch (Name.getNameKind()) { |
| 3375 | case DeclarationName::Identifier: |
| 3376 | AddIdentifierRef(Name.getAsIdentifierInfo(), Record); |
| 3377 | break; |
| 3378 | |
| 3379 | case DeclarationName::ObjCZeroArgSelector: |
| 3380 | case DeclarationName::ObjCOneArgSelector: |
| 3381 | case DeclarationName::ObjCMultiArgSelector: |
Steve Naroff | 2ddea05 | 2009-04-23 10:39:46 +0000 | [diff] [blame] | 3382 | AddSelectorRef(Name.getObjCSelector(), Record); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3383 | break; |
| 3384 | |
| 3385 | case DeclarationName::CXXConstructorName: |
| 3386 | case DeclarationName::CXXDestructorName: |
| 3387 | case DeclarationName::CXXConversionFunctionName: |
| 3388 | AddTypeRef(Name.getCXXNameType(), Record); |
| 3389 | break; |
| 3390 | |
| 3391 | case DeclarationName::CXXOperatorName: |
| 3392 | Record.push_back(Name.getCXXOverloadedOperator()); |
| 3393 | break; |
| 3394 | |
Alexis Hunt | 3d221f2 | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 3395 | case DeclarationName::CXXLiteralOperatorName: |
| 3396 | AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record); |
| 3397 | break; |
| 3398 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3399 | case DeclarationName::CXXUsingDirective: |
| 3400 | // No extra data to emit |
| 3401 | break; |
| 3402 | } |
| 3403 | } |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3404 | |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3405 | void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3406 | DeclarationName Name, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3407 | switch (Name.getNameKind()) { |
| 3408 | case DeclarationName::CXXConstructorName: |
| 3409 | case DeclarationName::CXXDestructorName: |
| 3410 | case DeclarationName::CXXConversionFunctionName: |
| 3411 | AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record); |
| 3412 | break; |
| 3413 | |
| 3414 | case DeclarationName::CXXOperatorName: |
| 3415 | AddSourceLocation( |
| 3416 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc), |
| 3417 | Record); |
| 3418 | AddSourceLocation( |
| 3419 | SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc), |
| 3420 | Record); |
| 3421 | break; |
| 3422 | |
| 3423 | case DeclarationName::CXXLiteralOperatorName: |
| 3424 | AddSourceLocation( |
| 3425 | SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc), |
| 3426 | Record); |
| 3427 | break; |
| 3428 | |
| 3429 | case DeclarationName::Identifier: |
| 3430 | case DeclarationName::ObjCZeroArgSelector: |
| 3431 | case DeclarationName::ObjCOneArgSelector: |
| 3432 | case DeclarationName::ObjCMultiArgSelector: |
| 3433 | case DeclarationName::CXXUsingDirective: |
| 3434 | break; |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3439 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3440 | AddDeclarationName(NameInfo.getName(), Record); |
| 3441 | AddSourceLocation(NameInfo.getLoc(), Record); |
| 3442 | AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record); |
| 3443 | } |
| 3444 | |
| 3445 | void ASTWriter::AddQualifierInfo(const QualifierInfo &Info, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3446 | RecordDataImpl &Record) { |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3447 | AddNestedNameSpecifierLoc(Info.QualifierLoc, Record); |
Argyrios Kyrtzidis | 434383d | 2010-10-15 18:21:24 +0000 | [diff] [blame] | 3448 | Record.push_back(Info.NumTemplParamLists); |
| 3449 | for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i) |
| 3450 | AddTemplateParameterList(Info.TemplParamLists[i], Record); |
| 3451 | } |
| 3452 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3453 | void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3454 | RecordDataImpl &Record) { |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3455 | // Nested name specifiers usually aren't too long. I think that 8 would |
| 3456 | // typically accomodate the vast majority. |
| 3457 | llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames; |
| 3458 | |
| 3459 | // Push each of the NNS's onto a stack for serialization in reverse order. |
| 3460 | while (NNS) { |
| 3461 | NestedNames.push_back(NNS); |
| 3462 | NNS = NNS->getPrefix(); |
| 3463 | } |
| 3464 | |
| 3465 | Record.push_back(NestedNames.size()); |
| 3466 | while(!NestedNames.empty()) { |
| 3467 | NNS = NestedNames.pop_back_val(); |
| 3468 | NestedNameSpecifier::SpecifierKind Kind = NNS->getKind(); |
| 3469 | Record.push_back(Kind); |
| 3470 | switch (Kind) { |
| 3471 | case NestedNameSpecifier::Identifier: |
| 3472 | AddIdentifierRef(NNS->getAsIdentifier(), Record); |
| 3473 | break; |
| 3474 | |
| 3475 | case NestedNameSpecifier::Namespace: |
| 3476 | AddDeclRef(NNS->getAsNamespace(), Record); |
| 3477 | break; |
| 3478 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3479 | case NestedNameSpecifier::NamespaceAlias: |
| 3480 | AddDeclRef(NNS->getAsNamespaceAlias(), Record); |
| 3481 | break; |
| 3482 | |
Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 3483 | case NestedNameSpecifier::TypeSpec: |
| 3484 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3485 | AddTypeRef(QualType(NNS->getAsType(), 0), Record); |
| 3486 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 3487 | break; |
| 3488 | |
| 3489 | case NestedNameSpecifier::Global: |
| 3490 | // Don't need to write an associated value. |
| 3491 | break; |
| 3492 | } |
| 3493 | } |
| 3494 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3495 | |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 3496 | void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
| 3497 | RecordDataImpl &Record) { |
| 3498 | // Nested name specifiers usually aren't too long. I think that 8 would |
| 3499 | // typically accomodate the vast majority. |
| 3500 | llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames; |
| 3501 | |
| 3502 | // Push each of the nested-name-specifiers's onto a stack for |
| 3503 | // serialization in reverse order. |
| 3504 | while (NNS) { |
| 3505 | NestedNames.push_back(NNS); |
| 3506 | NNS = NNS.getPrefix(); |
| 3507 | } |
| 3508 | |
| 3509 | Record.push_back(NestedNames.size()); |
| 3510 | while(!NestedNames.empty()) { |
| 3511 | NNS = NestedNames.pop_back_val(); |
| 3512 | NestedNameSpecifier::SpecifierKind Kind |
| 3513 | = NNS.getNestedNameSpecifier()->getKind(); |
| 3514 | Record.push_back(Kind); |
| 3515 | switch (Kind) { |
| 3516 | case NestedNameSpecifier::Identifier: |
| 3517 | AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record); |
| 3518 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 3519 | break; |
| 3520 | |
| 3521 | case NestedNameSpecifier::Namespace: |
| 3522 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record); |
| 3523 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 3524 | break; |
| 3525 | |
| 3526 | case NestedNameSpecifier::NamespaceAlias: |
| 3527 | AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record); |
| 3528 | AddSourceRange(NNS.getLocalSourceRange(), Record); |
| 3529 | break; |
| 3530 | |
| 3531 | case NestedNameSpecifier::TypeSpec: |
| 3532 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3533 | Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate); |
| 3534 | AddTypeLoc(NNS.getTypeLoc(), Record); |
| 3535 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 3536 | break; |
| 3537 | |
| 3538 | case NestedNameSpecifier::Global: |
| 3539 | AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record); |
| 3540 | break; |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3545 | void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) { |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3546 | TemplateName::NameKind Kind = Name.getKind(); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3547 | Record.push_back(Kind); |
| 3548 | switch (Kind) { |
| 3549 | case TemplateName::Template: |
| 3550 | AddDeclRef(Name.getAsTemplateDecl(), Record); |
| 3551 | break; |
| 3552 | |
| 3553 | case TemplateName::OverloadedTemplate: { |
| 3554 | OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); |
| 3555 | Record.push_back(OvT->size()); |
| 3556 | for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); |
| 3557 | I != E; ++I) |
| 3558 | AddDeclRef(*I, Record); |
| 3559 | break; |
| 3560 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3561 | |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3562 | case TemplateName::QualifiedTemplate: { |
| 3563 | QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); |
| 3564 | AddNestedNameSpecifier(QualT->getQualifier(), Record); |
| 3565 | Record.push_back(QualT->hasTemplateKeyword()); |
| 3566 | AddDeclRef(QualT->getTemplateDecl(), Record); |
| 3567 | break; |
| 3568 | } |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3569 | |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3570 | case TemplateName::DependentTemplate: { |
| 3571 | DependentTemplateName *DepT = Name.getAsDependentTemplateName(); |
| 3572 | AddNestedNameSpecifier(DepT->getQualifier(), Record); |
| 3573 | Record.push_back(DepT->isIdentifier()); |
| 3574 | if (DepT->isIdentifier()) |
| 3575 | AddIdentifierRef(DepT->getIdentifier(), Record); |
| 3576 | else |
| 3577 | Record.push_back(DepT->getOperator()); |
| 3578 | break; |
| 3579 | } |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 3580 | |
| 3581 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3582 | SubstTemplateTemplateParmPackStorage *SubstPack |
| 3583 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3584 | AddDeclRef(SubstPack->getParameterPack(), Record); |
| 3585 | AddTemplateArgument(SubstPack->getArgumentPack(), Record); |
| 3586 | break; |
| 3587 | } |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3588 | } |
| 3589 | } |
| 3590 | |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3591 | void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3592 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3593 | Record.push_back(Arg.getKind()); |
| 3594 | switch (Arg.getKind()) { |
| 3595 | case TemplateArgument::Null: |
| 3596 | break; |
| 3597 | case TemplateArgument::Type: |
| 3598 | AddTypeRef(Arg.getAsType(), Record); |
| 3599 | break; |
| 3600 | case TemplateArgument::Declaration: |
| 3601 | AddDeclRef(Arg.getAsDecl(), Record); |
| 3602 | break; |
| 3603 | case TemplateArgument::Integral: |
| 3604 | AddAPSInt(*Arg.getAsIntegral(), Record); |
| 3605 | AddTypeRef(Arg.getIntegralType(), Record); |
| 3606 | break; |
| 3607 | case TemplateArgument::Template: |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3608 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
| 3609 | break; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3610 | case TemplateArgument::TemplateExpansion: |
| 3611 | AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record); |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3612 | if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions()) |
| 3613 | Record.push_back(*NumExpansions + 1); |
| 3614 | else |
| 3615 | Record.push_back(0); |
Argyrios Kyrtzidis | 106caf92 | 2010-06-19 19:28:53 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | case TemplateArgument::Expression: |
| 3618 | AddStmt(Arg.getAsExpr()); |
| 3619 | break; |
| 3620 | case TemplateArgument::Pack: |
| 3621 | Record.push_back(Arg.pack_size()); |
| 3622 | for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); |
| 3623 | I != E; ++I) |
| 3624 | AddTemplateArgument(*I, Record); |
| 3625 | break; |
| 3626 | } |
| 3627 | } |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3628 | |
| 3629 | void |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3630 | ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3631 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3632 | assert(TemplateParams && "No TemplateParams!"); |
| 3633 | AddSourceLocation(TemplateParams->getTemplateLoc(), Record); |
| 3634 | AddSourceLocation(TemplateParams->getLAngleLoc(), Record); |
| 3635 | AddSourceLocation(TemplateParams->getRAngleLoc(), Record); |
| 3636 | Record.push_back(TemplateParams->size()); |
| 3637 | for (TemplateParameterList::const_iterator |
| 3638 | P = TemplateParams->begin(), PEnd = TemplateParams->end(); |
| 3639 | P != PEnd; ++P) |
| 3640 | AddDeclRef(*P, Record); |
| 3641 | } |
| 3642 | |
| 3643 | /// \brief Emit a template argument list. |
| 3644 | void |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3645 | ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3646 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3647 | assert(TemplateArgs && "No TemplateArgs!"); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3648 | Record.push_back(TemplateArgs->size()); |
| 3649 | for (int i=0, e = TemplateArgs->size(); i != e; ++i) |
Argyrios Kyrtzidis | 818c5db | 2010-06-23 13:48:30 +0000 | [diff] [blame] | 3650 | AddTemplateArgument(TemplateArgs->get(i), Record); |
| 3651 | } |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 3652 | |
| 3653 | |
| 3654 | void |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3655 | ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 2c2167a | 2010-07-02 11:55:32 +0000 | [diff] [blame] | 3656 | Record.push_back(Set.size()); |
| 3657 | for (UnresolvedSetImpl::const_iterator |
| 3658 | I = Set.begin(), E = Set.end(); I != E; ++I) { |
| 3659 | AddDeclRef(I.getDecl(), Record); |
| 3660 | Record.push_back(I.getAccess()); |
| 3661 | } |
| 3662 | } |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3663 | |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 3664 | void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3665 | RecordDataImpl &Record) { |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3666 | Record.push_back(Base.isVirtual()); |
| 3667 | Record.push_back(Base.isBaseOfClass()); |
| 3668 | Record.push_back(Base.getAccessSpecifierAsWritten()); |
Sebastian Redl | 0890502 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 3669 | Record.push_back(Base.getInheritConstructors()); |
Nick Lewycky | 19b9f95 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 3670 | AddTypeSourceInfo(Base.getTypeSourceInfo(), Record); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3671 | AddSourceRange(Base.getSourceRange(), Record); |
Douglas Gregor | 752a595 | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 3672 | AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc() |
| 3673 | : SourceLocation(), |
| 3674 | Record); |
Argyrios Kyrtzidis | 3701fcd | 2010-07-02 23:30:27 +0000 | [diff] [blame] | 3675 | } |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3677 | void ASTWriter::FlushCXXBaseSpecifiers() { |
| 3678 | RecordData Record; |
| 3679 | for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) { |
| 3680 | Record.clear(); |
| 3681 | |
| 3682 | // Record the offset of this base-specifier set. |
| 3683 | unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID; |
| 3684 | if (Index == CXXBaseSpecifiersOffsets.size()) |
| 3685 | CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo()); |
| 3686 | else { |
| 3687 | if (Index > CXXBaseSpecifiersOffsets.size()) |
| 3688 | CXXBaseSpecifiersOffsets.resize(Index + 1); |
| 3689 | CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo(); |
| 3690 | } |
| 3691 | |
| 3692 | const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases, |
| 3693 | *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd; |
| 3694 | Record.push_back(BEnd - B); |
| 3695 | for (; B != BEnd; ++B) |
| 3696 | AddCXXBaseSpecifier(*B, Record); |
| 3697 | Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record); |
Douglas Gregor | d585304 | 2010-10-30 04:28:16 +0000 | [diff] [blame] | 3698 | |
| 3699 | // Flush any expressions that were written as part of the base specifiers. |
| 3700 | FlushStmts(); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | CXXBaseSpecifiersToWrite.clear(); |
| 3704 | } |
| 3705 | |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 3706 | void ASTWriter::AddCXXCtorInitializers( |
| 3707 | const CXXCtorInitializer * const *CtorInitializers, |
| 3708 | unsigned NumCtorInitializers, |
| 3709 | RecordDataImpl &Record) { |
| 3710 | Record.push_back(NumCtorInitializers); |
| 3711 | for (unsigned i=0; i != NumCtorInitializers; ++i) { |
| 3712 | const CXXCtorInitializer *Init = CtorInitializers[i]; |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 3713 | |
| 3714 | Record.push_back(Init->isBaseInitializer()); |
| 3715 | if (Init->isBaseInitializer()) { |
| 3716 | AddTypeSourceInfo(Init->getBaseClassInfo(), Record); |
| 3717 | Record.push_back(Init->isBaseVirtual()); |
| 3718 | } else { |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3719 | Record.push_back(Init->isIndirectMemberInitializer()); |
| 3720 | if (Init->isIndirectMemberInitializer()) |
| 3721 | AddDeclRef(Init->getIndirectMember(), Record); |
| 3722 | else |
| 3723 | AddDeclRef(Init->getMember(), Record); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 3724 | } |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 3725 | |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 3726 | AddSourceLocation(Init->getMemberLocation(), Record); |
| 3727 | AddStmt(Init->getInit()); |
Argyrios Kyrtzidis | 5b6a03f | 2010-08-09 10:54:12 +0000 | [diff] [blame] | 3728 | AddSourceLocation(Init->getLParenLoc(), Record); |
| 3729 | AddSourceLocation(Init->getRParenLoc(), Record); |
| 3730 | Record.push_back(Init->isWritten()); |
| 3731 | if (Init->isWritten()) { |
| 3732 | Record.push_back(Init->getSourceOrder()); |
| 3733 | } else { |
| 3734 | Record.push_back(Init->getNumArrayIndices()); |
| 3735 | for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) |
| 3736 | AddDeclRef(Init->getArrayIndex(i), Record); |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3741 | void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) { |
| 3742 | assert(D->DefinitionData); |
| 3743 | struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; |
| 3744 | Record.push_back(Data.UserDeclaredConstructor); |
| 3745 | Record.push_back(Data.UserDeclaredCopyConstructor); |
| 3746 | Record.push_back(Data.UserDeclaredCopyAssignment); |
| 3747 | Record.push_back(Data.UserDeclaredDestructor); |
| 3748 | Record.push_back(Data.Aggregate); |
| 3749 | Record.push_back(Data.PlainOldData); |
| 3750 | Record.push_back(Data.Empty); |
| 3751 | Record.push_back(Data.Polymorphic); |
| 3752 | Record.push_back(Data.Abstract); |
| 3753 | Record.push_back(Data.HasTrivialConstructor); |
| 3754 | Record.push_back(Data.HasTrivialCopyConstructor); |
| 3755 | Record.push_back(Data.HasTrivialCopyAssignment); |
| 3756 | Record.push_back(Data.HasTrivialDestructor); |
| 3757 | Record.push_back(Data.ComputedVisibleConversions); |
| 3758 | Record.push_back(Data.DeclaredDefaultConstructor); |
| 3759 | Record.push_back(Data.DeclaredCopyConstructor); |
| 3760 | Record.push_back(Data.DeclaredCopyAssignment); |
| 3761 | Record.push_back(Data.DeclaredDestructor); |
| 3762 | |
| 3763 | Record.push_back(Data.NumBases); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3764 | if (Data.NumBases > 0) |
| 3765 | AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases, |
| 3766 | Record); |
| 3767 | |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3768 | // FIXME: Make VBases lazily computed when needed to avoid storing them. |
| 3769 | Record.push_back(Data.NumVBases); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3770 | if (Data.NumVBases > 0) |
| 3771 | AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases, |
| 3772 | Record); |
Argyrios Kyrtzidis | eb39d9a | 2010-10-24 17:26:40 +0000 | [diff] [blame] | 3773 | |
| 3774 | AddUnresolvedSet(Data.Conversions, Record); |
| 3775 | AddUnresolvedSet(Data.VisibleConversions, Record); |
| 3776 | // Data.Definition is the owning decl, no need to write it. |
| 3777 | AddDeclRef(Data.FirstFriend, Record); |
| 3778 | } |
| 3779 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3780 | void ASTWriter::ReaderInitialized(ASTReader *Reader) { |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 3781 | assert(Reader && "Cannot remove chain"); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3782 | assert(!Chain && "Cannot replace chain"); |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 3783 | assert(FirstDeclID == NextDeclID && |
| 3784 | FirstTypeID == NextTypeID && |
| 3785 | FirstIdentID == NextIdentID && |
Sebastian Redl | d95a56e | 2010-08-04 18:21:41 +0000 | [diff] [blame] | 3786 | FirstSelectorID == NextSelectorID && |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 3787 | FirstMacroID == NextMacroID && |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3788 | FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID && |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 3789 | "Setting chain after writing has started."); |
| 3790 | Chain = Reader; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3791 | |
| 3792 | FirstDeclID += Chain->getTotalNumDecls(); |
| 3793 | FirstTypeID += Chain->getTotalNumTypes(); |
| 3794 | FirstIdentID += Chain->getTotalNumIdentifiers(); |
| 3795 | FirstSelectorID += Chain->getTotalNumSelectors(); |
| 3796 | FirstMacroID += Chain->getTotalNumMacroDefinitions(); |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3797 | FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers(); |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 3798 | NextDeclID = FirstDeclID; |
| 3799 | NextTypeID = FirstTypeID; |
| 3800 | NextIdentID = FirstIdentID; |
| 3801 | NextSelectorID = FirstSelectorID; |
| 3802 | NextMacroID = FirstMacroID; |
Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 3803 | NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID; |
Sebastian Redl | 07a89a8 | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3806 | void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) { |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3807 | IdentifierIDs[II] = ID; |
Douglas Gregor | 68051a7 | 2011-02-11 00:26:14 +0000 | [diff] [blame] | 3808 | if (II->hasMacroDefinition()) |
| 3809 | DeserializedMacroNames.push_back(II); |
Sebastian Redl | ff4a295 | 2010-07-23 23:49:55 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Argyrios Kyrtzidis | bb5c7eae | 2010-08-20 16:03:59 +0000 | [diff] [blame] | 3812 | void ASTWriter::TypeRead(TypeIdx Idx, QualType T) { |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 3813 | // Always take the highest-numbered type index. This copes with an interesting |
| 3814 | // case for chained AST writing where we schedule writing the type and then, |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3815 | // later, deserialize the type from another AST. In this case, we want to |
Douglas Gregor | 9b3932c | 2010-10-05 18:37:06 +0000 | [diff] [blame] | 3816 | // keep the higher-numbered entry so that we can properly write it out to |
| 3817 | // the AST file. |
| 3818 | TypeIdx &StoredIdx = TypeIdxs[T]; |
| 3819 | if (Idx.getIndex() >= StoredIdx.getIndex()) |
| 3820 | StoredIdx = Idx; |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3823 | void ASTWriter::DeclRead(DeclID ID, const Decl *D) { |
Sebastian Redl | 1ea025b | 2010-07-16 16:36:56 +0000 | [diff] [blame] | 3824 | DeclIDs[D] = ID; |
Sebastian Redl | 85b2a6a | 2010-07-14 23:45:08 +0000 | [diff] [blame] | 3825 | } |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3826 | |
Sebastian Redl | 539c506 | 2010-08-18 23:57:32 +0000 | [diff] [blame] | 3827 | void ASTWriter::SelectorRead(SelectorID ID, Selector S) { |
Sebastian Redl | 834bb97 | 2010-08-04 17:20:04 +0000 | [diff] [blame] | 3828 | SelectorIDs[S] = ID; |
| 3829 | } |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 3830 | |
Michael J. Spencer | 4c0ffa8 | 2010-10-21 03:16:25 +0000 | [diff] [blame] | 3831 | void ASTWriter::MacroDefinitionRead(serialization::MacroID ID, |
Douglas Gregor | 9109629 | 2010-10-02 19:29:26 +0000 | [diff] [blame] | 3832 | MacroDefinition *MD) { |
| 3833 | MacroDefinitions[MD] = ID; |
| 3834 | } |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 3835 | |
| 3836 | void ASTWriter::CompletedTagDefinition(const TagDecl *D) { |
| 3837 | assert(D->isDefinition()); |
| 3838 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 3839 | // We are interested when a PCH decl is modified. |
| 3840 | if (RD->getPCHLevel() > 0) { |
| 3841 | // A forward reference was mutated into a definition. Rewrite it. |
| 3842 | // FIXME: This happens during template instantiation, should we |
| 3843 | // have created a new definition decl instead ? |
Argyrios Kyrtzidis | 4729972 | 2010-10-28 07:38:45 +0000 | [diff] [blame] | 3844 | RewriteDecl(RD); |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | for (CXXRecordDecl::redecl_iterator |
| 3848 | I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) { |
| 3849 | CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I); |
| 3850 | if (Redecl == RD) |
| 3851 | continue; |
| 3852 | |
| 3853 | // We are interested when a PCH decl is modified. |
| 3854 | if (Redecl->getPCHLevel() > 0) { |
| 3855 | UpdateRecord &Record = DeclUpdates[Redecl]; |
| 3856 | Record.push_back(UPD_CXX_SET_DEFINITIONDATA); |
| 3857 | assert(Redecl->DefinitionData); |
| 3858 | assert(Redecl->DefinitionData->Definition == D); |
| 3859 | AddDeclRef(D, Record); // the DefinitionDecl |
| 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | } |
Argyrios Kyrtzidis | 01c2df4 | 2010-10-28 07:38:51 +0000 | [diff] [blame] | 3864 | void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { |
| 3865 | // TU and namespaces are handled elsewhere. |
| 3866 | if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC)) |
| 3867 | return; |
| 3868 | |
| 3869 | if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0)) |
| 3870 | return; // Not a source decl added to a DeclContext from PCH. |
| 3871 | |
| 3872 | AddUpdatedDeclContext(DC); |
| 3873 | } |
Argyrios Kyrtzidis | e16a530 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 3874 | |
| 3875 | void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { |
| 3876 | assert(D->isImplicit()); |
| 3877 | if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0)) |
| 3878 | return; // Not a source member added to a class from PCH. |
| 3879 | if (!isa<CXXMethodDecl>(D)) |
| 3880 | return; // We are interested in lazily declared implicit methods. |
| 3881 | |
| 3882 | // A decl coming from PCH was modified. |
| 3883 | assert(RD->isDefinition()); |
| 3884 | UpdateRecord &Record = DeclUpdates[RD]; |
| 3885 | Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); |
| 3886 | AddDeclRef(D, Record); |
| 3887 | } |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 3888 | |
| 3889 | void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, |
| 3890 | const ClassTemplateSpecializationDecl *D) { |
Argyrios Kyrtzidis | ef80a01 | 2010-10-28 07:38:47 +0000 | [diff] [blame] | 3891 | // The specializations set is kept in the canonical template. |
| 3892 | TD = TD->getCanonicalDecl(); |
Argyrios Kyrtzidis | 402dbbb | 2010-10-28 07:38:42 +0000 | [diff] [blame] | 3893 | if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0)) |
| 3894 | return; // Not a source specialization added to a template from PCH. |
| 3895 | |
| 3896 | UpdateRecord &Record = DeclUpdates[TD]; |
| 3897 | Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION); |
| 3898 | AddDeclRef(D, Record); |
| 3899 | } |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 3900 | |
| 3901 | ASTSerializationListener::~ASTSerializationListener() { } |